Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-109
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is t, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const :tag "on" t)
79                  (const some)
80                  (const invisible)
81                  number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-refer-thread-limit 200
85   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
86 If t, fetch all the available old headers."
87   :group 'gnus-thread
88   :type '(choice number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-summary-make-false-root 'adopt
92   "*nil means that Gnus won't gather loose threads.
93 If the root of a thread has expired or been read in a previous
94 session, the information necessary to build a complete thread has been
95 lost.  Instead of having many small sub-threads from this original thread
96 scattered all over the summary buffer, Gnus can gather them.
97
98 If non-nil, Gnus will try to gather all loose sub-threads from an
99 original thread into one large thread.
100
101 If this variable is non-nil, it should be one of `none', `adopt',
102 `dummy' or `empty'.
103
104 If this variable is `none', Gnus will not make a false root, but just
105 present the sub-threads after another.
106 If this variable is `dummy', Gnus will create a dummy root that will
107 have all the sub-threads as children.
108 If this variable is `adopt', Gnus will make one of the \"children\"
109 the parent and mark all the step-children as such.
110 If this variable is `empty', the \"children\" are printed with empty
111 subject fields.  (Or rather, they will be printed with a string
112 given by the `gnus-summary-same-subject' variable.)"
113   :group 'gnus-thread
114   :type '(choice (const :tag "off" nil)
115                  (const none)
116                  (const dummy)
117                  (const adopt)
118                  (const empty)))
119
120 (defcustom gnus-summary-make-false-root-always nil
121   "Always make a false dummy root."
122   :version "21.4"
123   :group 'gnus-thread
124   :type 'boolean)
125
126 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
127   "*A regexp to match subjects to be excluded from loose thread gathering.
128 As loose thread gathering is done on subjects only, that means that
129 there can be many false gatherings performed.  By rooting out certain
130 common subjects, gathering might become saner."
131   :group 'gnus-thread
132   :type 'regexp)
133
134 (defcustom gnus-summary-gather-subject-limit nil
135   "*Maximum length of subject comparisons when gathering loose threads.
136 Use nil to compare full subjects.  Setting this variable to a low
137 number will help gather threads that have been corrupted by
138 newsreaders chopping off subject lines, but it might also mean that
139 unrelated articles that have subject that happen to begin with the
140 same few characters will be incorrectly gathered.
141
142 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
143 comparing subjects."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  (const fuzzy)
147                  (sexp :menu-tag "on" t)))
148
149 (defcustom gnus-simplify-subject-functions nil
150   "List of functions taking a string argument that simplify subjects.
151 The functions are applied recursively.
152
153 Useful functions to put in this list include:
154 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
155 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
156   :group 'gnus-thread
157   :type '(repeat function))
158
159 (defcustom gnus-simplify-ignored-prefixes nil
160   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  regexp))
164
165 (defcustom gnus-build-sparse-threads nil
166   "*If non-nil, fill in the gaps in threads.
167 If `some', only fill in the gaps that are needed to tie loose threads
168 together.  If `more', fill in all leaf nodes that Gnus can find.  If
169 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
170   :group 'gnus-thread
171   :type '(choice (const :tag "off" nil)
172                  (const some)
173                  (const more)
174                  (sexp :menu-tag "all" t)))
175
176 (defcustom gnus-summary-thread-gathering-function
177   'gnus-gather-threads-by-subject
178   "*Function used for gathering loose threads.
179 There are two pre-defined functions: `gnus-gather-threads-by-subject',
180 which only takes Subjects into consideration; and
181 `gnus-gather-threads-by-references', which compared the References
182 headers of the articles to find matches."
183   :group 'gnus-thread
184   :type '(radio (function-item gnus-gather-threads-by-subject)
185                 (function-item gnus-gather-threads-by-references)
186                 (function :tag "other")))
187
188 (defcustom gnus-summary-same-subject ""
189   "*String indicating that the current article has the same subject as the previous.
190 This variable will only be used if the value of
191 `gnus-summary-make-false-root' is `empty'."
192   :group 'gnus-summary-format
193   :type 'string)
194
195 (defcustom gnus-summary-goto-unread t
196   "*If t, many commands will go to the next unread article.
197 This applies to marking commands as well as other commands that
198 \"naturally\" select the next article, like, for instance, `SPC' at
199 the end of an article.
200
201 If nil, the marking commands do NOT go to the next unread article
202 \(they go to the next article instead).  If `never', commands that
203 usually go to the next unread article, will go to the next article,
204 whether it is read or not."
205   :group 'gnus-summary-marks
206   :link '(custom-manual "(gnus)Setting Marks")
207   :type '(choice (const :tag "off" nil)
208                  (const never)
209                  (sexp :menu-tag "on" t)))
210
211 (defcustom gnus-summary-default-score 0
212   "*Default article score level.
213 All scores generated by the score files will be added to this score.
214 If this variable is nil, scoring will be disabled."
215   :group 'gnus-score-default
216   :type '(choice (const :tag "disable")
217                  integer))
218
219 (defcustom gnus-summary-default-high-score 0
220   "*Default threshold for a high scored article.
221 An article will be highlighted as high scored if its score is greater
222 than this score."
223   :version "21.4"
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-default-low-score 0
228   "*Default threshold for a low scored article.
229 An article will be highlighted as low scored if its score is smaller
230 than this score."
231   :version "21.4"
232   :group 'gnus-score-default
233   :type 'integer)
234
235 (defcustom gnus-summary-zcore-fuzz 0
236   "*Fuzziness factor for the zcore in the summary buffer.
237 Articles with scores closer than this to `gnus-summary-default-score'
238 will not be marked."
239   :group 'gnus-summary-format
240   :type 'integer)
241
242 (defcustom gnus-simplify-subject-fuzzy-regexp nil
243   "*Strings to be removed when doing fuzzy matches.
244 This can either be a regular expression or list of regular expressions
245 that will be removed from subject strings if fuzzy subject
246 simplification is selected."
247   :group 'gnus-thread
248   :type '(repeat regexp))
249
250 (defcustom gnus-show-threads t
251   "*If non-nil, display threads in summary mode."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-hide-subtree nil
256   "*If non-nil, hide all threads initially.
257 This can be a predicate specifier which says which threads to hide.
258 If threads are hidden, you have to run the command
259 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
260 to expose hidden threads."
261   :group 'gnus-thread
262   :type '(radio (sexp :format "Non-nil\n"
263                       :match (lambda (widget value)
264                                (not (or (consp value) (functionp value))))
265                       :value t)
266                 (const nil)
267                 (sexp :tag "Predicate specifier" :size 0)))
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), 'unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :version "21.4"
328   :group 'gnus-group-select
329   :type '(choice (const best)
330                  (const unread)
331                  (const first)
332                  (const unseen)
333                  (const unseen-or-unread)))
334
335 (defcustom gnus-auto-select-next t
336   "*If non-nil, offer to go to the next group from the end of the previous.
337 If the value is t and the next newsgroup is empty, Gnus will exit
338 summary mode and go back to group mode.  If the value is neither nil
339 nor t, Gnus will select the following unread newsgroup.  In
340 particular, if the value is the symbol `quietly', the next unread
341 newsgroup will be selected without any confirmation, and if it is
342 `almost-quietly', the next group will be selected without any
343 confirmation if you are located on the last article in the group.
344 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
345 will go to the next group without confirmation."
346   :group 'gnus-summary-maneuvering
347   :type '(choice (const :tag "off" nil)
348                  (const quietly)
349                  (const almost-quietly)
350                  (const slightly-quietly)
351                  (sexp :menu-tag "on" t)))
352
353 (defcustom gnus-auto-select-same nil
354   "*If non-nil, select the next article with the same subject.
355 If there are no more articles with the same subject, go to
356 the first unread article."
357   :group 'gnus-summary-maneuvering
358   :type 'boolean)
359
360 (defcustom gnus-auto-goto-ignores 'unfetched
361   "*Says how to handle unfetched articles when maneuvering.
362
363 This variable can either be the symbols nil (maneuver to any
364 article), `undownloaded' (maneuvering while unplugged ignores articles
365 that have not been fetched), `always-undownloaded' (maneuvering always
366 ignores articles that have not been fetched), `unfetched' (maneuvering
367 ignores articles whose headers have not been fetched).
368
369 NOTE: The list of unfetched articles will always be nil when plugged
370 and, when unplugged, a subset of the undownloaded article list."
371   :version "21.4"
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "None" nil)
374                  (const :tag "Undownloaded when unplugged" undownloaded)
375                  (const :tag "Undownloaded" always-undownloaded)
376                  (const :tag "Unfetched" unfetched)))
377
378 (defcustom gnus-summary-check-current nil
379   "*If non-nil, consider the current article when moving.
380 The \"unread\" movement commands will stay on the same line if the
381 current article is unread."
382   :group 'gnus-summary-maneuvering
383   :type 'boolean)
384
385 (defcustom gnus-auto-center-summary 2
386   "*If non-nil, always center the current summary buffer.
387 In particular, if `vertical' do only vertical recentering.  If non-nil
388 and non-`vertical', do both horizontal and vertical recentering."
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "none" nil)
391                  (const vertical)
392                  (integer :tag "height")
393                  (sexp :menu-tag "both" t)))
394
395 (defvar gnus-auto-center-group t
396   "*If non-nil, always center the group buffer.")
397
398 (defcustom gnus-show-all-headers nil
399   "*If non-nil, don't hide any headers."
400   :group 'gnus-article-hiding
401   :group 'gnus-article-headers
402   :type 'boolean)
403
404 (defcustom gnus-summary-ignore-duplicates nil
405   "*If non-nil, ignore articles with identical Message-ID headers."
406   :group 'gnus-summary
407   :type 'boolean)
408
409 (defcustom gnus-single-article-buffer t
410   "*If non-nil, display all articles in the same buffer.
411 If nil, each group will get its own article buffer."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-break-pages t
416   "*If non-nil, do page breaking on articles.
417 The page delimiter is specified by the `gnus-page-delimiter'
418 variable."
419   :group 'gnus-article-various
420   :type 'boolean)
421
422 (defcustom gnus-move-split-methods nil
423   "*Variable used to suggest where articles are to be moved to.
424 It uses the same syntax as the `gnus-split-methods' variable.
425 However, whereas `gnus-split-methods' specifies file names as targets,
426 this variable specifies group names."
427   :group 'gnus-summary-mail
428   :type '(repeat (choice (list :value (fun) function)
429                          (cons :value ("" "") regexp (repeat string))
430                          (sexp :value nil))))
431
432 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
433   "Function used to compute default prefix for article move/copy/etc prompts.
434 The function should take one argument, a group name, and return a
435 string with the suggested prefix."
436   :group 'gnus-summary-mail
437   :type 'function)
438
439 (defcustom gnus-unread-mark ?           ;Whitespace
440   "*Mark used for unread articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-ticked-mark ?!
445   "*Mark used for ticked articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-dormant-mark ??
450   "*Mark used for dormant articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-del-mark ?r
455   "*Mark used for del'd articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-read-mark ?R
460   "*Mark used for read articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-expirable-mark ?E
465   "*Mark used for expirable articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-killed-mark ?K
470   "*Mark used for killed articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-spam-mark ?$
475   "*Mark used for spam articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-souped-mark ?F
480   "*Mark used for souped articles."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-kill-file-mark ?X
485   "*Mark used for articles killed by kill files."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-low-score-mark ?Y
490   "*Mark used for articles with a low score."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-catchup-mark ?C
495   "*Mark used for articles that are caught up."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-replied-mark ?A
500   "*Mark used for articles that have been replied to."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-forwarded-mark ?F
505   "*Mark used for articles that have been forwarded."
506   :version "21.4"
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-recent-mark ?N
511   "*Mark used for articles that are recent."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-cached-mark ?*
516   "*Mark used for articles that are in the cache."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-saved-mark ?S
521   "*Mark used for articles that have been saved."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unseen-mark ?.
526   "*Mark used for articles that haven't been seen."
527   :version "21.4"
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-no-mark ?               ;Whitespace
532   "*Mark used for articles that have no other secondary mark."
533   :version "21.4"
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-ancient-mark ?O
538   "*Mark used for ancient articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-sparse-mark ?Q
543   "*Mark used for sparsely reffed articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-canceled-mark ?G
548   "*Mark used for canceled articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-duplicate-mark ?M
553   "*Mark used for duplicate articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-undownloaded-mark ?-
558   "*Mark used for articles that weren't downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-downloaded-mark ?+
563   "*Mark used for articles that were downloaded."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-downloadable-mark ?%
568   "*Mark used for articles that are to be downloaded."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-unsendable-mark ?=
573   "*Mark used for articles that won't be sent."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-score-over-mark ?+
578   "*Score mark used for articles with high scores."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-score-below-mark ?-
583   "*Score mark used for articles with low scores."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-empty-thread-mark ?     ;Whitespace
588   "*There is no thread under the article."
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-not-empty-thread-mark ?=
593   "*There is a thread under the article."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-view-pseudo-asynchronously nil
598   "*If non-nil, Gnus will view pseudo-articles asynchronously."
599   :group 'gnus-extract-view
600   :type 'boolean)
601
602 (defcustom gnus-auto-expirable-marks
603   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
604         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
605         gnus-souped-mark gnus-duplicate-mark)
606   "*The list of marks converted into expiration if a group is auto-expirable."
607   :version "21.1"
608   :group 'gnus-summary
609   :type '(repeat character))
610
611 (defcustom gnus-inhibit-user-auto-expire t
612   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
613   :version "21.1"
614   :group 'gnus-summary
615   :type 'boolean)
616
617 (defcustom gnus-view-pseudos nil
618   "*If `automatic', pseudo-articles will be viewed automatically.
619 If `not-confirm', pseudos will be viewed automatically, and the user
620 will not be asked to confirm the command."
621   :group 'gnus-extract-view
622   :type '(choice (const :tag "off" nil)
623                  (const automatic)
624                  (const not-confirm)))
625
626 (defcustom gnus-view-pseudos-separately t
627   "*If non-nil, one pseudo-article will be created for each file to be viewed.
628 If nil, all files that use the same viewing command will be given as a
629 list of parameters to that command."
630   :group 'gnus-extract-view
631   :type 'boolean)
632
633 (defcustom gnus-insert-pseudo-articles t
634   "*If non-nil, insert pseudo-articles when decoding articles."
635   :group 'gnus-extract-view
636   :type 'boolean)
637
638 (defcustom gnus-summary-dummy-line-format
639   "   %(:                             :%) %S\n"
640   "*The format specification for the dummy roots in the summary buffer.
641 It works along the same lines as a normal formatting string,
642 with some simple extensions.
643
644 %S  The subject
645
646 General format specifiers can also be used.
647 See `(gnus)Formatting Variables'."
648   :link '(custom-manual "(gnus)Formatting Variables")
649   :group 'gnus-threading
650   :type 'string)
651
652 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
653   "*The format specification for the summary mode line.
654 It works along the same lines as a normal formatting string,
655 with some simple extensions:
656
657 %G  Group name
658 %p  Unprefixed group name
659 %A  Current article number
660 %z  Current article score
661 %V  Gnus version
662 %U  Number of unread articles in the group
663 %e  Number of unselected articles in the group
664 %Z  A string with unread/unselected article counts
665 %g  Shortish group name
666 %S  Subject of the current article
667 %u  User-defined spec
668 %s  Current score file name
669 %d  Number of dormant articles
670 %r  Number of articles that have been marked as read in this session
671 %E  Number of articles expunged by the score files"
672   :group 'gnus-summary-format
673   :type 'string)
674
675 (defcustom gnus-list-identifiers nil
676   "Regexp that matches list identifiers to be removed from subject.
677 This can also be a list of regexps."
678   :version "21.1"
679   :group 'gnus-summary-format
680   :group 'gnus-article-hiding
681   :type '(choice (const :tag "none" nil)
682                  (regexp :value ".*")
683                  (repeat :value (".*") regexp)))
684
685 (defcustom gnus-summary-mark-below 0
686   "*Mark all articles with a score below this variable as read.
687 This variable is local to each summary buffer and usually set by the
688 score file."
689   :group 'gnus-score-default
690   :type 'integer)
691
692 (defun gnus-widget-reversible-match (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
695   ;; (debug value)
696   (or (symbolp value)
697       (and (listp value)
698            (eq (length value) 2)
699            (eq (nth 0 value) 'not)
700            (symbolp (nth 1 value)))))
701
702 (defun gnus-widget-reversible-to-internal (widget value)
703   "Ignoring WIDGET, convert VALUE to internal form.
704 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
705 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
706   ;; (debug value)
707   (if (atom value)
708       (list value nil)
709     (list (nth 1 value) t)))
710
711 (defun gnus-widget-reversible-to-external (widget value)
712   "Ignoring WIDGET, convert VALUE to external form.
713 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
714 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
715   ;; (debug value)
716   (if (nth 1 value)
717       (list 'not (nth 0 value))
718     (nth 0 value)))
719
720 (define-widget 'gnus-widget-reversible 'group
721   "A `group' that convert values."
722   :match 'gnus-widget-reversible-match
723   :value-to-internal 'gnus-widget-reversible-to-internal
724   :value-to-external 'gnus-widget-reversible-to-external)
725
726 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
727   "*List of functions used for sorting articles in the summary buffer.
728
729 Each function takes two articles and returns non-nil if the first
730 article should be sorted before the other.  If you use more than one
731 function, the primary sort function should be the last.  You should
732 probably always include `gnus-article-sort-by-number' in the list of
733 sorting functions -- preferably first.  Also note that sorting by date
734 is often much slower than sorting by number, and the sorting order is
735 very similar.  (Sorting by date means sorting by the time the message
736 was sent, sorting by number means sorting by arrival time.)
737
738 Each item can also be a list `(not F)' where F is a function;
739 this reverses the sort order.
740
741 Ready-made functions include `gnus-article-sort-by-number',
742 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
743 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
744 and `gnus-article-sort-by-score'.
745
746 When threading is turned on, the variable `gnus-thread-sort-functions'
747 controls how articles are sorted."
748   :group 'gnus-summary-sort
749   :type '(repeat (gnus-widget-reversible
750                   (choice (function-item gnus-article-sort-by-number)
751                           (function-item gnus-article-sort-by-author)
752                           (function-item gnus-article-sort-by-subject)
753                           (function-item gnus-article-sort-by-date)
754                           (function-item gnus-article-sort-by-score)
755                           (function-item gnus-article-sort-by-random)
756                           (function :tag "other"))
757                   (boolean :tag "Reverse order"))))
758
759
760 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
761   "*List of functions used for sorting threads in the summary buffer.
762 By default, threads are sorted by article number.
763
764 Each function takes two threads and returns non-nil if the first
765 thread should be sorted before the other.  If you use more than one
766 function, the primary sort function should be the last.  You should
767 probably always include `gnus-thread-sort-by-number' in the list of
768 sorting functions -- preferably first.  Also note that sorting by date
769 is often much slower than sorting by number, and the sorting order is
770 very similar.  (Sorting by date means sorting by the time the message
771 was sent, sorting by number means sorting by arrival time.)
772
773 Each list item can also be a list `(not F)' where F is a
774 function; this specifies reversed sort order.
775
776 Ready-made functions include `gnus-thread-sort-by-number',
777 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
778 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
779 `gnus-thread-sort-by-most-recent-number',
780 `gnus-thread-sort-by-most-recent-date',
781 `gnus-thread-sort-by-random', and
782 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
783
784 When threading is turned off, the variable
785 `gnus-article-sort-functions' controls how articles are sorted."
786   :group 'gnus-summary-sort
787   :type '(repeat 
788           (gnus-widget-reversible
789            (choice (function-item gnus-thread-sort-by-number)
790                    (function-item gnus-thread-sort-by-author)
791                    (function-item gnus-thread-sort-by-subject)
792                    (function-item gnus-thread-sort-by-date)
793                    (function-item gnus-thread-sort-by-score)
794                    (function-item gnus-thread-sort-by-most-recent-number)
795                    (function-item gnus-thread-sort-by-most-recent-date)
796                    (function-item gnus-thread-sort-by-random)
797                    (function-item gnus-thread-sort-by-total-score)
798                    (function :tag "other"))
799            (boolean :tag "Reverse order"))))
800
801 (defcustom gnus-thread-score-function '+
802   "*Function used for calculating the total score of a thread.
803
804 The function is called with the scores of the article and each
805 subthread and should then return the score of the thread.
806
807 Some functions you can use are `+', `max', or `min'."
808   :group 'gnus-summary-sort
809   :type 'function)
810
811 (defcustom gnus-summary-expunge-below nil
812   "All articles that have a score less than this variable will be expunged.
813 This variable is local to the summary buffers."
814   :group 'gnus-score-default
815   :type '(choice (const :tag "off" nil)
816                  integer))
817
818 (defcustom gnus-thread-expunge-below nil
819   "All threads that have a total score less than this variable will be expunged.
820 See `gnus-thread-score-function' for en explanation of what a
821 \"thread score\" is.
822
823 This variable is local to the summary buffers."
824   :group 'gnus-threading
825   :group 'gnus-score-default
826   :type '(choice (const :tag "off" nil)
827                  integer))
828
829 (defcustom gnus-summary-mode-hook nil
830   "*A hook for Gnus summary mode.
831 This hook is run before any variables are set in the summary buffer."
832   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
833   :group 'gnus-summary-various
834   :type 'hook)
835
836 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
837 (when (featurep 'xemacs)
838   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
839   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
840   (add-hook 'gnus-summary-mode-hook
841             'gnus-xmas-switch-horizontal-scrollbar-off))
842
843 (defcustom gnus-summary-menu-hook nil
844   "*Hook run after the creation of the summary mode menu."
845   :group 'gnus-summary-visual
846   :type 'hook)
847
848 (defcustom gnus-summary-exit-hook nil
849   "*A hook called on exit from the summary buffer.
850 It will be called with point in the group buffer."
851   :group 'gnus-summary-exit
852   :type 'hook)
853
854 (defcustom gnus-summary-prepare-hook nil
855   "*A hook called after the summary buffer has been generated.
856 If you want to modify the summary buffer, you can use this hook."
857   :group 'gnus-summary-various
858   :type 'hook)
859
860 (defcustom gnus-summary-prepared-hook nil
861   "*A hook called as the last thing after the summary buffer has been generated."
862   :group 'gnus-summary-various
863   :type 'hook)
864
865 (defcustom gnus-summary-generate-hook nil
866   "*A hook run just before generating the summary buffer.
867 This hook is commonly used to customize threading variables and the
868 like."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-select-group-hook nil
873   "*A hook called when a newsgroup is selected.
874
875 If you'd like to simplify subjects like the
876 `gnus-summary-next-same-subject' command does, you can use the
877 following hook:
878
879  (add-hook gnus-select-group-hook
880            (lambda ()
881              (mapcar (lambda (header)
882                        (mail-header-set-subject
883                         header
884                         (gnus-simplify-subject
885                          (mail-header-subject header) 're-only)))
886                      gnus-newsgroup-headers)))"
887   :group 'gnus-group-select
888   :type 'hook)
889
890 (defcustom gnus-select-article-hook nil
891   "*A hook called when an article is selected."
892   :group 'gnus-summary-choose
893   :options '(gnus-agent-fetch-selected-article)
894   :type 'hook)
895
896 (defcustom gnus-visual-mark-article-hook
897   (list 'gnus-highlight-selected-summary)
898   "*Hook run after selecting an article in the summary buffer.
899 It is meant to be used for highlighting the article in some way.  It
900 is not run if `gnus-visual' is nil."
901   :group 'gnus-summary-visual
902   :type 'hook)
903
904 (defcustom gnus-parse-headers-hook nil
905   "*A hook called before parsing the headers."
906   :group 'gnus-various
907   :type 'hook)
908
909 (defcustom gnus-exit-group-hook nil
910   "*A hook called when exiting summary mode.
911 This hook is not called from the non-updating exit commands like `Q'."
912   :group 'gnus-various
913   :type 'hook)
914
915 (defcustom gnus-summary-update-hook
916   (list 'gnus-summary-highlight-line)
917   "*A hook called when a summary line is changed.
918 The hook will not be called if `gnus-visual' is nil.
919
920 The default function `gnus-summary-highlight-line' will
921 highlight the line according to the `gnus-summary-highlight'
922 variable."
923   :group 'gnus-summary-visual
924   :type 'hook)
925
926 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
927   "*A hook called when an article is selected for the first time.
928 The hook is intended to mark an article as read (or unread)
929 automatically when it is selected."
930   :group 'gnus-summary-choose
931   :type 'hook)
932
933 (defcustom gnus-group-no-more-groups-hook nil
934   "*A hook run when returning to group mode having no more (unread) groups."
935   :group 'gnus-group-select
936   :type 'hook)
937
938 (defcustom gnus-ps-print-hook nil
939   "*A hook run before ps-printing something from Gnus."
940   :group 'gnus-summary
941   :type 'hook)
942
943 (defcustom gnus-summary-article-move-hook nil
944   "*A hook called after an article is moved, copied, respooled, or crossposted."
945   :group 'gnus-summary
946   :type 'hook)
947
948 (defcustom gnus-summary-article-delete-hook nil
949   "*A hook called after an article is deleted."
950   :group 'gnus-summary
951   :type 'hook)
952
953 (defcustom gnus-summary-article-expire-hook nil
954   "*A hook called after an article is expired."
955   :group 'gnus-summary
956   :type 'hook)
957
958 (defcustom gnus-summary-display-arrow
959   (and (fboundp 'display-graphic-p)
960        (display-graphic-p))
961   "*If non-nil, display an arrow highlighting the current article."
962   :version "21.4"
963   :group 'gnus-summary
964   :type 'boolean)
965
966 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
967   "Face used for highlighting the current article in the summary buffer."
968   :group 'gnus-summary-visual
969   :type 'face)
970
971 (defvar gnus-tmp-downloaded nil)
972
973 (defcustom gnus-summary-highlight
974   '(((eq mark gnus-canceled-mark)
975      . gnus-summary-cancelled-face)
976     ((and uncached (> score default-high))
977      . gnus-summary-high-undownloaded-face)
978     ((and uncached (< score default-low))
979      . gnus-summary-low-undownloaded-face)
980     (uncached
981      . gnus-summary-normal-undownloaded-face)
982     ((and (> score default-high)
983           (or (eq mark gnus-dormant-mark)
984               (eq mark gnus-ticked-mark)))
985      . gnus-summary-high-ticked-face)
986     ((and (< score default-low)
987           (or (eq mark gnus-dormant-mark)
988               (eq mark gnus-ticked-mark)))
989      . gnus-summary-low-ticked-face)
990     ((or (eq mark gnus-dormant-mark)
991          (eq mark gnus-ticked-mark))
992      . gnus-summary-normal-ticked-face)
993     ((and (> score default-high) (eq mark gnus-ancient-mark))
994      . gnus-summary-high-ancient-face)
995     ((and (< score default-low) (eq mark gnus-ancient-mark))
996      . gnus-summary-low-ancient-face)
997     ((eq mark gnus-ancient-mark)
998      . gnus-summary-normal-ancient-face)
999     ((and (> score default-high) (eq mark gnus-unread-mark))
1000      . gnus-summary-high-unread-face)
1001     ((and (< score default-low) (eq mark gnus-unread-mark))
1002      . gnus-summary-low-unread-face)
1003     ((eq mark gnus-unread-mark)
1004      . gnus-summary-normal-unread-face)
1005     ((> score default-high)
1006      . gnus-summary-high-read-face)
1007     ((< score default-low)
1008      . gnus-summary-low-read-face)
1009     (t
1010      . gnus-summary-normal-read-face))
1011   "*Controls the highlighting of summary buffer lines.
1012
1013 A list of (FORM . FACE) pairs.  When deciding how a a particular
1014 summary line should be displayed, each form is evaluated.  The content
1015 of the face field after the first true form is used.  You can change
1016 how those summary lines are displayed, by editing the face field.
1017
1018 You can use the following variables in the FORM field.
1019
1020 score:        The article's score
1021 default:      The default article score.
1022 default-high: The default score for high scored articles.
1023 default-low:  The default score for low scored articles.
1024 below:        The score below which articles are automatically marked as read.
1025 mark:         The article's mark.
1026 uncached:     Non-nil if the article is uncached."
1027   :group 'gnus-summary-visual
1028   :type '(repeat (cons (sexp :tag "Form" nil)
1029                        face)))
1030
1031 (defcustom gnus-alter-header-function nil
1032   "Function called to allow alteration of article header structures.
1033 The function is called with one parameter, the article header vector,
1034 which it may alter in any way."
1035   :type '(choice (const :tag "None" nil)
1036                  function)
1037   :group 'gnus-summary)
1038
1039 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1040   "Variable that says which function should be used to decode a string with encoded words.")
1041
1042 (defcustom gnus-extra-headers '(To Newsgroups)
1043   "*Extra headers to parse."
1044   :version "21.1"
1045   :group 'gnus-summary
1046   :type '(repeat symbol))
1047
1048 (defcustom gnus-ignored-from-addresses
1049   (and user-mail-address (regexp-quote user-mail-address))
1050   "*Regexp of From headers that may be suppressed in favor of To headers."
1051   :version "21.1"
1052   :group 'gnus-summary
1053   :type 'regexp)
1054
1055 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1056   "List of charsets that should be ignored.
1057 When these charsets are used in the \"charset\" parameter, the
1058 default charset will be used instead."
1059   :version "21.1"
1060   :type '(repeat symbol)
1061   :group 'gnus-charset)
1062
1063 (gnus-define-group-parameter
1064  ignored-charsets
1065  :type list
1066  :function-document
1067  "Return the ignored charsets of GROUP."
1068  :variable gnus-group-ignored-charsets-alist
1069  :variable-default
1070  '(("alt\\.chinese\\.text" iso-8859-1))
1071  :variable-document
1072  "Alist of regexps (to match group names) and charsets that should be ignored.
1073 When these charsets are used in the \"charset\" parameter, the
1074 default charset will be used instead."
1075  :variable-group gnus-charset
1076  :variable-type '(repeat (cons (regexp :tag "Group")
1077                                (repeat symbol)))
1078  :parameter-type '(choice :tag "Ignored charsets"
1079                           :value nil
1080                           (repeat (symbol)))
1081  :parameter-document       "\
1082 List of charsets that should be ignored.
1083
1084 When these charsets are used in the \"charset\" parameter, the
1085 default charset will be used instead.")
1086
1087 (defcustom gnus-group-highlight-words-alist nil
1088   "Alist of group regexps and highlight regexps.
1089 This variable uses the same syntax as `gnus-emphasis-alist'."
1090   :version "21.1"
1091   :type '(repeat (cons (regexp :tag "Group")
1092                        (repeat (list (regexp :tag "Highlight regexp")
1093                                      (number :tag "Group for entire word" 0)
1094                                      (number :tag "Group for displayed part" 0)
1095                                      (symbol :tag "Face"
1096                                              gnus-emphasis-highlight-words)))))
1097   :group 'gnus-summary-visual)
1098
1099 (defcustom gnus-summary-show-article-charset-alist
1100   nil
1101   "Alist of number and charset.
1102 The article will be shown with the charset corresponding to the
1103 numbered argument.
1104 For example: ((1 . cn-gb-2312) (2 . big5))."
1105   :version "21.1"
1106   :type '(repeat (cons (number :tag "Argument" 1)
1107                        (symbol :tag "Charset")))
1108   :group 'gnus-charset)
1109
1110 (defcustom gnus-preserve-marks t
1111   "Whether marks are preserved when moving, copying and respooling messages."
1112   :version "21.1"
1113   :type 'boolean
1114   :group 'gnus-summary-marks)
1115
1116 (defcustom gnus-alter-articles-to-read-function nil
1117   "Function to be called to alter the list of articles to be selected."
1118   :type '(choice (const nil) function)
1119   :group 'gnus-summary)
1120
1121 (defcustom gnus-orphan-score nil
1122   "*All orphans get this score added.  Set in the score file."
1123   :group 'gnus-score-default
1124   :type '(choice (const nil)
1125                  integer))
1126
1127 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1128   "*A regexp to match MIME parts when saving multiple parts of a
1129 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1130 This regexp will be used by default when prompting the user for which
1131 type of files to save."
1132   :group 'gnus-summary
1133   :type 'regexp)
1134
1135 (defcustom gnus-read-all-available-headers nil
1136   "Whether Gnus should parse all headers made available to it.
1137 This is mostly relevant for slow back ends where the user may
1138 wish to widen the summary buffer to include all headers
1139 that were fetched.  Say, for nnultimate groups."
1140   :version "21.4"
1141   :group 'gnus-summary
1142   :type '(choice boolean regexp))
1143
1144 (defcustom gnus-summary-muttprint-program "muttprint"
1145   "Command (and optional arguments) used to run Muttprint."
1146   :version "21.4"
1147   :group 'gnus-summary
1148   :type 'string)
1149
1150 (defcustom gnus-article-loose-mime t
1151   "If non-nil, don't require MIME-Version header.
1152 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1153 supply the MIME-Version header or deliberately strip it from the mail.
1154 If non-nil (the default), Gnus will treat some articles as MIME
1155 even if the MIME-Version header is missing."
1156   :version "21.4"
1157   :type 'boolean
1158   :group 'gnus-article-mime)
1159
1160 (defcustom gnus-article-emulate-mime t
1161   "If non-nil, use MIME emulation for uuencode and the like.
1162 This means that Gnus will search message bodies for text that look
1163 like uuencoded bits, yEncoded bits, and so on, and present that using
1164 the normal Gnus MIME machinery."
1165   :version "21.4"
1166   :type 'boolean
1167   :group 'gnus-article-mime)
1168
1169 ;;; Internal variables
1170
1171 (defvar gnus-summary-display-cache nil)
1172 (defvar gnus-article-mime-handles nil)
1173 (defvar gnus-article-decoded-p nil)
1174 (defvar gnus-article-charset nil)
1175 (defvar gnus-article-ignored-charsets nil)
1176 (defvar gnus-scores-exclude-files nil)
1177 (defvar gnus-page-broken nil)
1178
1179 (defvar gnus-original-article nil)
1180 (defvar gnus-article-internal-prepare-hook nil)
1181 (defvar gnus-newsgroup-process-stack nil)
1182
1183 (defvar gnus-thread-indent-array nil)
1184 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1185 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1186   "Function called to sort the articles within a thread after it has been gathered together.")
1187
1188 (defvar gnus-summary-save-parts-type-history nil)
1189 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1190
1191 ;; Avoid highlighting in kill files.
1192 (defvar gnus-summary-inhibit-highlight nil)
1193 (defvar gnus-newsgroup-selected-overlay nil)
1194 (defvar gnus-inhibit-limiting nil)
1195 (defvar gnus-newsgroup-adaptive-score-file nil)
1196 (defvar gnus-current-score-file nil)
1197 (defvar gnus-current-move-group nil)
1198 (defvar gnus-current-copy-group nil)
1199 (defvar gnus-current-crosspost-group nil)
1200 (defvar gnus-newsgroup-display nil)
1201
1202 (defvar gnus-newsgroup-dependencies nil)
1203 (defvar gnus-newsgroup-adaptive nil)
1204 (defvar gnus-summary-display-article-function nil)
1205 (defvar gnus-summary-highlight-line-function nil
1206   "Function called after highlighting a summary line.")
1207
1208 (defvar gnus-summary-line-format-alist
1209   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1210     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1211     (?s gnus-tmp-subject-or-nil ?s)
1212     (?n gnus-tmp-name ?s)
1213     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1214         ?s)
1215     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1216             gnus-tmp-from) ?s)
1217     (?F gnus-tmp-from ?s)
1218     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1219     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1220     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1221     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1222     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1223     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1224     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1225     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1226     (?L gnus-tmp-lines ?s)
1227     (?O gnus-tmp-downloaded ?c)
1228     (?I gnus-tmp-indentation ?s)
1229     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1230     (?R gnus-tmp-replied ?c)
1231     (?\[ gnus-tmp-opening-bracket ?c)
1232     (?\] gnus-tmp-closing-bracket ?c)
1233     (?\> (make-string gnus-tmp-level ? ) ?s)
1234     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1235     (?i gnus-tmp-score ?d)
1236     (?z gnus-tmp-score-char ?c)
1237     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1238     (?U gnus-tmp-unread ?c)
1239     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1240         ?s)
1241     (?t (gnus-summary-number-of-articles-in-thread
1242          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1243         ?d)
1244     (?e (gnus-summary-number-of-articles-in-thread
1245          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1246         ?c)
1247     (?u gnus-tmp-user-defined ?s)
1248     (?P (gnus-pick-line-number) ?d)
1249     (?B gnus-tmp-thread-tree-header-string ?s)
1250     (user-date (gnus-user-date
1251                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1252   "An alist of format specifications that can appear in summary lines.
1253 These are paired with what variables they correspond with, along with
1254 the type of the variable (string, integer, character, etc).")
1255
1256 (defvar gnus-summary-dummy-line-format-alist
1257   `((?S gnus-tmp-subject ?s)
1258     (?N gnus-tmp-number ?d)
1259     (?u gnus-tmp-user-defined ?s)))
1260
1261 (defvar gnus-summary-mode-line-format-alist
1262   `((?G gnus-tmp-group-name ?s)
1263     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1264     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1265     (?A gnus-tmp-article-number ?d)
1266     (?Z gnus-tmp-unread-and-unselected ?s)
1267     (?V gnus-version ?s)
1268     (?U gnus-tmp-unread-and-unticked ?d)
1269     (?S gnus-tmp-subject ?s)
1270     (?e gnus-tmp-unselected ?d)
1271     (?u gnus-tmp-user-defined ?s)
1272     (?d (length gnus-newsgroup-dormant) ?d)
1273     (?t (length gnus-newsgroup-marked) ?d)
1274     (?h (length gnus-newsgroup-spam-marked) ?d)
1275     (?r (length gnus-newsgroup-reads) ?d)
1276     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1277     (?E gnus-newsgroup-expunged-tally ?d)
1278     (?s (gnus-current-score-file-nondirectory) ?s)))
1279
1280 (defvar gnus-last-search-regexp nil
1281   "Default regexp for article search command.")
1282
1283 (defvar gnus-last-shell-command nil
1284   "Default shell command on article.")
1285
1286 (defvar gnus-newsgroup-agentized nil
1287   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1288 (defvar gnus-newsgroup-begin nil)
1289 (defvar gnus-newsgroup-end nil)
1290 (defvar gnus-newsgroup-last-rmail nil)
1291 (defvar gnus-newsgroup-last-mail nil)
1292 (defvar gnus-newsgroup-last-folder nil)
1293 (defvar gnus-newsgroup-last-file nil)
1294 (defvar gnus-newsgroup-auto-expire nil)
1295 (defvar gnus-newsgroup-active nil)
1296
1297 (defvar gnus-newsgroup-data nil)
1298 (defvar gnus-newsgroup-data-reverse nil)
1299 (defvar gnus-newsgroup-limit nil)
1300 (defvar gnus-newsgroup-limits nil)
1301 (defvar gnus-summary-use-undownloaded-faces nil)
1302
1303 (defvar gnus-newsgroup-unreads nil
1304   "Sorted list of unread articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-unselected nil
1307   "Sorted list of unselected unread articles in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-reads nil
1310   "Alist of read articles and article marks in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-expunged-tally nil)
1313
1314 (defvar gnus-newsgroup-marked nil
1315   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1316
1317 (defvar gnus-newsgroup-spam-marked nil
1318   "List of ranges of articles that have been marked as spam.")
1319
1320 (defvar gnus-newsgroup-killed nil
1321   "List of ranges of articles that have been through the scoring process.")
1322
1323 (defvar gnus-newsgroup-cached nil
1324   "Sorted list of articles that come from the article cache.")
1325
1326 (defvar gnus-newsgroup-saved nil
1327   "List of articles that have been saved.")
1328
1329 (defvar gnus-newsgroup-kill-headers nil)
1330
1331 (defvar gnus-newsgroup-replied nil
1332   "List of articles that have been replied to in the current newsgroup.")
1333
1334 (defvar gnus-newsgroup-forwarded nil
1335   "List of articles that have been forwarded in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-recent nil
1338   "List of articles that have are recent in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-expirable nil
1341   "Sorted list of articles in the current newsgroup that can be expired.")
1342
1343 (defvar gnus-newsgroup-processable nil
1344   "List of articles in the current newsgroup that can be processed.")
1345
1346 (defvar gnus-newsgroup-downloadable nil
1347   "Sorted list of articles in the current newsgroup that can be processed.")
1348
1349 (defvar gnus-newsgroup-unfetched nil
1350   "Sorted list of articles in the current newsgroup whose headers have
1351 not been fetched into the agent.
1352
1353 This list will always be a subset of gnus-newsgroup-undownloaded.")
1354
1355 (defvar gnus-newsgroup-undownloaded nil
1356   "List of articles in the current newsgroup that haven't been downloaded.")
1357
1358 (defvar gnus-newsgroup-unsendable nil
1359   "List of articles in the current newsgroup that won't be sent.")
1360
1361 (defvar gnus-newsgroup-bookmarks nil
1362   "List of articles in the current newsgroup that have bookmarks.")
1363
1364 (defvar gnus-newsgroup-dormant nil
1365   "Sorted list of dormant articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-unseen nil
1368   "List of unseen articles in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-seen nil
1371   "Range of seen articles in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-articles nil
1374   "List of articles in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-scored nil
1377   "List of scored articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-headers nil
1380   "List of article headers in the current newsgroup.")
1381
1382 (defvar gnus-newsgroup-threads nil)
1383
1384 (defvar gnus-newsgroup-prepared nil
1385   "Whether the current group has been prepared properly.")
1386
1387 (defvar gnus-newsgroup-ancient nil
1388   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1389
1390 (defvar gnus-newsgroup-sparse nil)
1391
1392 (defvar gnus-current-article nil)
1393 (defvar gnus-article-current nil)
1394 (defvar gnus-current-headers nil)
1395 (defvar gnus-have-all-headers nil)
1396 (defvar gnus-last-article nil)
1397 (defvar gnus-newsgroup-history nil)
1398 (defvar gnus-newsgroup-charset nil)
1399 (defvar gnus-newsgroup-ephemeral-charset nil)
1400 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1401
1402 (defvar gnus-article-before-search nil)
1403
1404 (defvar gnus-summary-local-variables
1405   '(gnus-newsgroup-name
1406     gnus-newsgroup-begin gnus-newsgroup-end
1407     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1408     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1409     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1410     gnus-newsgroup-unselected gnus-newsgroup-marked
1411     gnus-newsgroup-spam-marked
1412     gnus-newsgroup-reads gnus-newsgroup-saved
1413     gnus-newsgroup-replied gnus-newsgroup-forwarded
1414     gnus-newsgroup-recent
1415     gnus-newsgroup-expirable
1416     gnus-newsgroup-processable gnus-newsgroup-killed
1417     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1418     gnus-newsgroup-unfetched
1419     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1420     gnus-newsgroup-seen gnus-newsgroup-articles
1421     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1422     gnus-newsgroup-headers gnus-newsgroup-threads
1423     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1424     gnus-current-article gnus-current-headers gnus-have-all-headers
1425     gnus-last-article gnus-article-internal-prepare-hook
1426     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1427     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1428     gnus-thread-expunge-below
1429     gnus-score-alist gnus-current-score-file
1430     (gnus-summary-expunge-below . global)
1431     (gnus-summary-mark-below . global)
1432     (gnus-orphan-score . global)
1433     gnus-newsgroup-active gnus-scores-exclude-files
1434     gnus-newsgroup-history gnus-newsgroup-ancient
1435     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1436     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1437     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1438     (gnus-newsgroup-expunged-tally . 0)
1439     gnus-cache-removable-articles gnus-newsgroup-cached
1440     gnus-newsgroup-data gnus-newsgroup-data-reverse
1441     gnus-newsgroup-limit gnus-newsgroup-limits
1442     gnus-newsgroup-charset gnus-newsgroup-display
1443     gnus-summary-use-undownloaded-faces)
1444   "Variables that are buffer-local to the summary buffers.")
1445
1446 (defvar gnus-newsgroup-variables nil
1447   "A list of variables that have separate values in different newsgroups.
1448 A list of newsgroup (summary buffer) local variables, or cons of
1449 variables and their default expressions to be evalled (when the default
1450 values are not nil), that should be made global while the summary buffer
1451 is active.
1452
1453 Note: The default expressions will be evaluated (using function `eval')
1454 before assignment to the local variable rather than just assigned to it.
1455 If the default expression is the symbol `global', that symbol will not
1456 be evaluated but the global value of the local variable will be used
1457 instead.
1458
1459 These variables can be used to set variables in the group parameters
1460 while still allowing them to affect operations done in other buffers.
1461 For example:
1462
1463 \(setq gnus-newsgroup-variables
1464      '(message-use-followup-to
1465        (gnus-visible-headers .
1466          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1467 ")
1468
1469 ;; Byte-compiler warning.
1470 (eval-when-compile
1471   ;; Bind features so that require will believe that gnus-sum has
1472   ;; already been loaded (avoids infinite recursion)
1473   (let ((features (cons 'gnus-sum features)))
1474     ;; Several of the declarations in gnus-sum are needed to load the
1475     ;; following files. Right now, these definitions have been
1476     ;; compiled but not defined (evaluated).  We could either do a
1477     ;; eval-and-compile about all of the declarations or evaluate the
1478     ;; source file.
1479     (if (boundp 'gnus-newsgroup-variables)
1480         nil
1481       (load "gnus-sum.el" t t t))
1482     (require 'gnus)
1483     (require 'gnus-art)))
1484
1485 ;; MIME stuff.
1486
1487 (defvar gnus-decode-encoded-word-methods
1488   '(mail-decode-encoded-word-string)
1489   "List of methods used to decode encoded words.
1490
1491 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1492 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1493 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1494 whose names match REGEXP.
1495
1496 For example:
1497 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1498  mail-decode-encoded-word-string
1499  (\"chinese\" . rfc1843-decode-string))")
1500
1501 (defvar gnus-decode-encoded-word-methods-cache nil)
1502
1503 (defun gnus-multi-decode-encoded-word-string (string)
1504   "Apply the functions from `gnus-encoded-word-methods' that match."
1505   (unless (and gnus-decode-encoded-word-methods-cache
1506                (eq gnus-newsgroup-name
1507                    (car gnus-decode-encoded-word-methods-cache)))
1508     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1509     (mapcar (lambda (x)
1510               (if (symbolp x)
1511                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1512                 (if (and gnus-newsgroup-name
1513                          (string-match (car x) gnus-newsgroup-name))
1514                     (nconc gnus-decode-encoded-word-methods-cache
1515                            (list (cdr x))))))
1516             gnus-decode-encoded-word-methods))
1517   (let ((xlist gnus-decode-encoded-word-methods-cache))
1518     (pop xlist)
1519     (while xlist
1520       (setq string (funcall (pop xlist) string))))
1521   string)
1522
1523 ;; Subject simplification.
1524
1525 (defun gnus-simplify-whitespace (str)
1526   "Remove excessive whitespace from STR."
1527   ;; Multiple spaces.
1528   (while (string-match "[ \t][ \t]+" str)
1529     (setq str (concat (substring str 0 (match-beginning 0))
1530                         " "
1531                         (substring str (match-end 0)))))
1532   ;; Leading spaces.
1533   (when (string-match "^[ \t]+" str)
1534     (setq str (substring str (match-end 0))))
1535   ;; Trailing spaces.
1536   (when (string-match "[ \t]+$" str)
1537     (setq str (substring str 0 (match-beginning 0))))
1538   str)
1539
1540 (defun gnus-simplify-all-whitespace (str)
1541   "Remove all whitespace from STR."
1542   (while (string-match "[ \t\n]+" str)
1543     (setq str (replace-match "" nil nil str)))
1544   str)
1545
1546 (defsubst gnus-simplify-subject-re (subject)
1547   "Remove \"Re:\" from subject lines."
1548   (if (string-match message-subject-re-regexp subject)
1549       (substring subject (match-end 0))
1550     subject))
1551
1552 (defun gnus-simplify-subject (subject &optional re-only)
1553   "Remove `Re:' and words in parentheses.
1554 If RE-ONLY is non-nil, strip leading `Re:'s only."
1555   (let ((case-fold-search t))           ;Ignore case.
1556     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1557     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1558       (setq subject (substring subject (match-end 0))))
1559     ;; Remove uninteresting prefixes.
1560     (when (and (not re-only)
1561                gnus-simplify-ignored-prefixes
1562                (string-match gnus-simplify-ignored-prefixes subject))
1563       (setq subject (substring subject (match-end 0))))
1564     ;; Remove words in parentheses from end.
1565     (unless re-only
1566       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1567         (setq subject (substring subject 0 (match-beginning 0)))))
1568     ;; Return subject string.
1569     subject))
1570
1571 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1572 ;; all whitespace.
1573 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1574   (goto-char (point-min))
1575   (while (re-search-forward regexp nil t)
1576     (replace-match (or newtext ""))))
1577
1578 (defun gnus-simplify-buffer-fuzzy ()
1579   "Simplify string in the buffer fuzzily.
1580 The string in the accessible portion of the current buffer is simplified.
1581 It is assumed to be a single-line subject.
1582 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1583 matter is removed.  Additional things can be deleted by setting
1584 `gnus-simplify-subject-fuzzy-regexp'."
1585   (let ((case-fold-search t)
1586         (modified-tick))
1587     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1588
1589     (while (not (eq modified-tick (buffer-modified-tick)))
1590       (setq modified-tick (buffer-modified-tick))
1591       (cond
1592        ((listp gnus-simplify-subject-fuzzy-regexp)
1593         (mapcar 'gnus-simplify-buffer-fuzzy-step
1594                 gnus-simplify-subject-fuzzy-regexp))
1595        (gnus-simplify-subject-fuzzy-regexp
1596         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1597       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1598       (gnus-simplify-buffer-fuzzy-step
1599        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1600       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1601
1602     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1603     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1604     (gnus-simplify-buffer-fuzzy-step " $")
1605     (gnus-simplify-buffer-fuzzy-step "^ +")))
1606
1607 (defun gnus-simplify-subject-fuzzy (subject)
1608   "Simplify a subject string fuzzily.
1609 See `gnus-simplify-buffer-fuzzy' for details."
1610   (save-excursion
1611     (gnus-set-work-buffer)
1612     (let ((case-fold-search t))
1613       ;; Remove uninteresting prefixes.
1614       (when (and gnus-simplify-ignored-prefixes
1615                  (string-match gnus-simplify-ignored-prefixes subject))
1616         (setq subject (substring subject (match-end 0))))
1617       (insert subject)
1618       (inline (gnus-simplify-buffer-fuzzy))
1619       (buffer-string))))
1620
1621 (defsubst gnus-simplify-subject-fully (subject)
1622   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1623   (cond
1624    (gnus-simplify-subject-functions
1625     (gnus-map-function gnus-simplify-subject-functions subject))
1626    ((null gnus-summary-gather-subject-limit)
1627     (gnus-simplify-subject-re subject))
1628    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1629     (gnus-simplify-subject-fuzzy subject))
1630    ((numberp gnus-summary-gather-subject-limit)
1631     (gnus-limit-string (gnus-simplify-subject-re subject)
1632                        gnus-summary-gather-subject-limit))
1633    (t
1634     subject)))
1635
1636 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1637   "Check whether two subjects are equal.
1638 If optional argument SIMPLE-FIRST is t, first argument is already
1639 simplified."
1640   (cond
1641    ((null simple-first)
1642     (equal (gnus-simplify-subject-fully s1)
1643            (gnus-simplify-subject-fully s2)))
1644    (t
1645     (equal s1
1646            (gnus-simplify-subject-fully s2)))))
1647
1648 (defun gnus-summary-bubble-group ()
1649   "Increase the score of the current group.
1650 This is a handy function to add to `gnus-summary-exit-hook' to
1651 increase the score of each group you read."
1652   (gnus-group-add-score gnus-newsgroup-name))
1653
1654 \f
1655 ;;;
1656 ;;; Gnus summary mode
1657 ;;;
1658
1659 (put 'gnus-summary-mode 'mode-class 'special)
1660
1661 (defvar gnus-article-commands-menu)
1662
1663 ;; Non-orthogonal keys
1664
1665 (gnus-define-keys gnus-summary-mode-map
1666   " " gnus-summary-next-page
1667   "\177" gnus-summary-prev-page
1668   [delete] gnus-summary-prev-page
1669   [backspace] gnus-summary-prev-page
1670   "\r" gnus-summary-scroll-up
1671   "\M-\r" gnus-summary-scroll-down
1672   "n" gnus-summary-next-unread-article
1673   "p" gnus-summary-prev-unread-article
1674   "N" gnus-summary-next-article
1675   "P" gnus-summary-prev-article
1676   "\M-\C-n" gnus-summary-next-same-subject
1677   "\M-\C-p" gnus-summary-prev-same-subject
1678   "\M-n" gnus-summary-next-unread-subject
1679   "\M-p" gnus-summary-prev-unread-subject
1680   "." gnus-summary-first-unread-article
1681   "," gnus-summary-best-unread-article
1682   "\M-s" gnus-summary-search-article-forward
1683   "\M-r" gnus-summary-search-article-backward
1684   "<" gnus-summary-beginning-of-article
1685   ">" gnus-summary-end-of-article
1686   "j" gnus-summary-goto-article
1687   "^" gnus-summary-refer-parent-article
1688   "\M-^" gnus-summary-refer-article
1689   "u" gnus-summary-tick-article-forward
1690   "!" gnus-summary-tick-article-forward
1691   "U" gnus-summary-tick-article-backward
1692   "d" gnus-summary-mark-as-read-forward
1693   "D" gnus-summary-mark-as-read-backward
1694   "E" gnus-summary-mark-as-expirable
1695   "\M-u" gnus-summary-clear-mark-forward
1696   "\M-U" gnus-summary-clear-mark-backward
1697   "k" gnus-summary-kill-same-subject-and-select
1698   "\C-k" gnus-summary-kill-same-subject
1699   "\M-\C-k" gnus-summary-kill-thread
1700   "\M-\C-l" gnus-summary-lower-thread
1701   "e" gnus-summary-edit-article
1702   "#" gnus-summary-mark-as-processable
1703   "\M-#" gnus-summary-unmark-as-processable
1704   "\M-\C-t" gnus-summary-toggle-threads
1705   "\M-\C-s" gnus-summary-show-thread
1706   "\M-\C-h" gnus-summary-hide-thread
1707   "\M-\C-f" gnus-summary-next-thread
1708   "\M-\C-b" gnus-summary-prev-thread
1709   [(meta down)] gnus-summary-next-thread
1710   [(meta up)] gnus-summary-prev-thread
1711   "\M-\C-u" gnus-summary-up-thread
1712   "\M-\C-d" gnus-summary-down-thread
1713   "&" gnus-summary-execute-command
1714   "c" gnus-summary-catchup-and-exit
1715   "\C-w" gnus-summary-mark-region-as-read
1716   "\C-t" gnus-summary-toggle-truncation
1717   "?" gnus-summary-mark-as-dormant
1718   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1719   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1720   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1721   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1722   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1723   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1724   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1725   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1726   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1727   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1728   "=" gnus-summary-expand-window
1729   "\C-x\C-s" gnus-summary-reselect-current-group
1730   "\M-g" gnus-summary-rescan-group
1731   "w" gnus-summary-stop-page-breaking
1732   "\C-c\C-r" gnus-summary-caesar-message
1733   "f" gnus-summary-followup
1734   "F" gnus-summary-followup-with-original
1735   "C" gnus-summary-cancel-article
1736   "r" gnus-summary-reply
1737   "R" gnus-summary-reply-with-original
1738   "\C-c\C-f" gnus-summary-mail-forward
1739   "o" gnus-summary-save-article
1740   "\C-o" gnus-summary-save-article-mail
1741   "|" gnus-summary-pipe-output
1742   "\M-k" gnus-summary-edit-local-kill
1743   "\M-K" gnus-summary-edit-global-kill
1744   ;; "V" gnus-version
1745   "\C-c\C-d" gnus-summary-describe-group
1746   "q" gnus-summary-exit
1747   "Q" gnus-summary-exit-no-update
1748   "\C-c\C-i" gnus-info-find-node
1749   gnus-mouse-2 gnus-mouse-pick-article
1750   "m" gnus-summary-mail-other-window
1751   "a" gnus-summary-post-news
1752   "i" gnus-summary-news-other-window
1753   "x" gnus-summary-limit-to-unread
1754   "s" gnus-summary-isearch-article
1755   "t" gnus-summary-toggle-header
1756   "g" gnus-summary-show-article
1757   "l" gnus-summary-goto-last-article
1758   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1759   "\C-d" gnus-summary-enter-digest-group
1760   "\M-\C-d" gnus-summary-read-document
1761   "\M-\C-e" gnus-summary-edit-parameters
1762   "\M-\C-a" gnus-summary-customize-parameters
1763   "\C-c\C-b" gnus-bug
1764   "*" gnus-cache-enter-article
1765   "\M-*" gnus-cache-remove-article
1766   "\M-&" gnus-summary-universal-argument
1767   "\C-l" gnus-recenter
1768   "I" gnus-summary-increase-score
1769   "L" gnus-summary-lower-score
1770   "\M-i" gnus-symbolic-argument
1771   "h" gnus-summary-select-article-buffer
1772
1773   "b" gnus-article-view-part
1774   "\M-t" gnus-summary-toggle-display-buttonized
1775
1776   "V" gnus-summary-score-map
1777   "X" gnus-uu-extract-map
1778   "S" gnus-summary-send-map)
1779
1780 ;; Sort of orthogonal keymap
1781 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1782   "t" gnus-summary-tick-article-forward
1783   "!" gnus-summary-tick-article-forward
1784   "d" gnus-summary-mark-as-read-forward
1785   "r" gnus-summary-mark-as-read-forward
1786   "c" gnus-summary-clear-mark-forward
1787   " " gnus-summary-clear-mark-forward
1788   "e" gnus-summary-mark-as-expirable
1789   "x" gnus-summary-mark-as-expirable
1790   "?" gnus-summary-mark-as-dormant
1791   "b" gnus-summary-set-bookmark
1792   "B" gnus-summary-remove-bookmark
1793   "#" gnus-summary-mark-as-processable
1794   "\M-#" gnus-summary-unmark-as-processable
1795   "S" gnus-summary-limit-include-expunged
1796   "C" gnus-summary-catchup
1797   "H" gnus-summary-catchup-to-here
1798   "h" gnus-summary-catchup-from-here
1799   "\C-c" gnus-summary-catchup-all
1800   "k" gnus-summary-kill-same-subject-and-select
1801   "K" gnus-summary-kill-same-subject
1802   "P" gnus-uu-mark-map)
1803
1804 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1805   "c" gnus-summary-clear-above
1806   "u" gnus-summary-tick-above
1807   "m" gnus-summary-mark-above
1808   "k" gnus-summary-kill-below)
1809
1810 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1811   "/" gnus-summary-limit-to-subject
1812   "n" gnus-summary-limit-to-articles
1813   "w" gnus-summary-pop-limit
1814   "s" gnus-summary-limit-to-subject
1815   "a" gnus-summary-limit-to-author
1816   "u" gnus-summary-limit-to-unread
1817   "m" gnus-summary-limit-to-marks
1818   "M" gnus-summary-limit-exclude-marks
1819   "v" gnus-summary-limit-to-score
1820   "*" gnus-summary-limit-include-cached
1821   "D" gnus-summary-limit-include-dormant
1822   "T" gnus-summary-limit-include-thread
1823   "d" gnus-summary-limit-exclude-dormant
1824   "t" gnus-summary-limit-to-age
1825   "." gnus-summary-limit-to-unseen
1826   "x" gnus-summary-limit-to-extra
1827   "p" gnus-summary-limit-to-display-predicate
1828   "E" gnus-summary-limit-include-expunged
1829   "c" gnus-summary-limit-exclude-childless-dormant
1830   "C" gnus-summary-limit-mark-excluded-as-read
1831   "o" gnus-summary-insert-old-articles
1832   "N" gnus-summary-insert-new-articles
1833   "r" gnus-summary-limit-to-replied)
1834
1835 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1836   "n" gnus-summary-next-unread-article
1837   "p" gnus-summary-prev-unread-article
1838   "N" gnus-summary-next-article
1839   "P" gnus-summary-prev-article
1840   "\C-n" gnus-summary-next-same-subject
1841   "\C-p" gnus-summary-prev-same-subject
1842   "\M-n" gnus-summary-next-unread-subject
1843   "\M-p" gnus-summary-prev-unread-subject
1844   "f" gnus-summary-first-unread-article
1845   "b" gnus-summary-best-unread-article
1846   "j" gnus-summary-goto-article
1847   "g" gnus-summary-goto-subject
1848   "l" gnus-summary-goto-last-article
1849   "o" gnus-summary-pop-article)
1850
1851 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1852   "k" gnus-summary-kill-thread
1853   "l" gnus-summary-lower-thread
1854   "i" gnus-summary-raise-thread
1855   "T" gnus-summary-toggle-threads
1856   "t" gnus-summary-rethread-current
1857   "^" gnus-summary-reparent-thread
1858   "s" gnus-summary-show-thread
1859   "S" gnus-summary-show-all-threads
1860   "h" gnus-summary-hide-thread
1861   "H" gnus-summary-hide-all-threads
1862   "n" gnus-summary-next-thread
1863   "p" gnus-summary-prev-thread
1864   "u" gnus-summary-up-thread
1865   "o" gnus-summary-top-thread
1866   "d" gnus-summary-down-thread
1867   "#" gnus-uu-mark-thread
1868   "\M-#" gnus-uu-unmark-thread)
1869
1870 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1871   "g" gnus-summary-prepare
1872   "c" gnus-summary-insert-cached-articles
1873   "d" gnus-summary-insert-dormant-articles)
1874
1875 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1876   "c" gnus-summary-catchup-and-exit
1877   "C" gnus-summary-catchup-all-and-exit
1878   "E" gnus-summary-exit-no-update
1879   "Q" gnus-summary-exit
1880   "Z" gnus-summary-exit
1881   "n" gnus-summary-catchup-and-goto-next-group
1882   "R" gnus-summary-reselect-current-group
1883   "G" gnus-summary-rescan-group
1884   "N" gnus-summary-next-group
1885   "s" gnus-summary-save-newsrc
1886   "P" gnus-summary-prev-group)
1887
1888 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1889   " " gnus-summary-next-page
1890   "n" gnus-summary-next-page
1891   "\177" gnus-summary-prev-page
1892   [delete] gnus-summary-prev-page
1893   "p" gnus-summary-prev-page
1894   "\r" gnus-summary-scroll-up
1895   "\M-\r" gnus-summary-scroll-down
1896   "<" gnus-summary-beginning-of-article
1897   ">" gnus-summary-end-of-article
1898   "b" gnus-summary-beginning-of-article
1899   "e" gnus-summary-end-of-article
1900   "^" gnus-summary-refer-parent-article
1901   "r" gnus-summary-refer-parent-article
1902   "D" gnus-summary-enter-digest-group
1903   "R" gnus-summary-refer-references
1904   "T" gnus-summary-refer-thread
1905   "g" gnus-summary-show-article
1906   "s" gnus-summary-isearch-article
1907   "P" gnus-summary-print-article
1908   "M" gnus-mailing-list-insinuate
1909   "t" gnus-article-babel)
1910
1911 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1912   "b" gnus-article-add-buttons
1913   "B" gnus-article-add-buttons-to-head
1914   "o" gnus-article-treat-overstrike
1915   "e" gnus-article-emphasize
1916   "w" gnus-article-fill-cited-article
1917   "Q" gnus-article-fill-long-lines
1918   "C" gnus-article-capitalize-sentences
1919   "c" gnus-article-remove-cr
1920   "q" gnus-article-de-quoted-unreadable
1921   "6" gnus-article-de-base64-unreadable
1922   "Z" gnus-article-decode-HZ
1923   "A" gnus-article-treat-ansi-sequences
1924   "h" gnus-article-wash-html
1925   "u" gnus-article-unsplit-urls
1926   "s" gnus-summary-force-verify-and-decrypt
1927   "f" gnus-article-display-x-face
1928   "l" gnus-summary-stop-page-breaking
1929   "r" gnus-summary-caesar-message
1930   "m" gnus-summary-morse-message
1931   "t" gnus-summary-toggle-header
1932   "g" gnus-treat-smiley
1933   "v" gnus-summary-verbose-headers
1934   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1935   "p" gnus-article-verify-x-pgp-sig
1936   "d" gnus-article-treat-dumbquotes)
1937
1938 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1939   ;; mnemonic: deuglif*Y*
1940   "u" gnus-article-outlook-unwrap-lines
1941   "a" gnus-article-outlook-repair-attribution
1942   "c" gnus-article-outlook-rearrange-citation
1943   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1944
1945 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1946   "a" gnus-article-hide
1947   "h" gnus-article-hide-headers
1948   "b" gnus-article-hide-boring-headers
1949   "s" gnus-article-hide-signature
1950   "c" gnus-article-hide-citation
1951   "C" gnus-article-hide-citation-in-followups
1952   "l" gnus-article-hide-list-identifiers
1953   "B" gnus-article-strip-banner
1954   "P" gnus-article-hide-pem
1955   "\C-c" gnus-article-hide-citation-maybe)
1956
1957 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1958   "a" gnus-article-highlight
1959   "h" gnus-article-highlight-headers
1960   "c" gnus-article-highlight-citation
1961   "s" gnus-article-highlight-signature)
1962
1963 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1964   "f" gnus-article-treat-fold-headers
1965   "u" gnus-article-treat-unfold-headers
1966   "n" gnus-article-treat-fold-newsgroups)
1967
1968 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1969   "x" gnus-article-display-x-face
1970   "d" gnus-article-display-face
1971   "s" gnus-treat-smiley
1972   "D" gnus-article-remove-images
1973   "f" gnus-treat-from-picon
1974   "m" gnus-treat-mail-picon
1975   "n" gnus-treat-newsgroups-picon)
1976
1977 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1978   "w" gnus-article-decode-mime-words
1979   "c" gnus-article-decode-charset
1980   "v" gnus-mime-view-all-parts
1981   "b" gnus-article-view-part)
1982
1983 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1984   "z" gnus-article-date-ut
1985   "u" gnus-article-date-ut
1986   "l" gnus-article-date-local
1987   "p" gnus-article-date-english
1988   "e" gnus-article-date-lapsed
1989   "o" gnus-article-date-original
1990   "i" gnus-article-date-iso8601
1991   "s" gnus-article-date-user)
1992
1993 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1994   "t" gnus-article-remove-trailing-blank-lines
1995   "l" gnus-article-strip-leading-blank-lines
1996   "m" gnus-article-strip-multiple-blank-lines
1997   "a" gnus-article-strip-blank-lines
1998   "A" gnus-article-strip-all-blank-lines
1999   "s" gnus-article-strip-leading-space
2000   "e" gnus-article-strip-trailing-space
2001   "w" gnus-article-remove-leading-whitespace)
2002
2003 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2004   "v" gnus-version
2005   "f" gnus-summary-fetch-faq
2006   "d" gnus-summary-describe-group
2007   "h" gnus-summary-describe-briefly
2008   "i" gnus-info-find-node
2009   "c" gnus-group-fetch-charter
2010   "C" gnus-group-fetch-control)
2011
2012 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2013   "e" gnus-summary-expire-articles
2014   "\M-\C-e" gnus-summary-expire-articles-now
2015   "\177" gnus-summary-delete-article
2016   [delete] gnus-summary-delete-article
2017   [backspace] gnus-summary-delete-article
2018   "m" gnus-summary-move-article
2019   "r" gnus-summary-respool-article
2020   "w" gnus-summary-edit-article
2021   "c" gnus-summary-copy-article
2022   "B" gnus-summary-crosspost-article
2023   "q" gnus-summary-respool-query
2024   "t" gnus-summary-respool-trace
2025   "i" gnus-summary-import-article
2026   "I" gnus-summary-create-article
2027   "p" gnus-summary-article-posted-p)
2028
2029 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2030   "o" gnus-summary-save-article
2031   "m" gnus-summary-save-article-mail
2032   "F" gnus-summary-write-article-file
2033   "r" gnus-summary-save-article-rmail
2034   "f" gnus-summary-save-article-file
2035   "b" gnus-summary-save-article-body-file
2036   "h" gnus-summary-save-article-folder
2037   "v" gnus-summary-save-article-vm
2038   "p" gnus-summary-pipe-output
2039   "P" gnus-summary-muttprint
2040   "s" gnus-soup-add-article)
2041
2042 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2043   "b" gnus-summary-display-buttonized
2044   "m" gnus-summary-repair-multipart
2045   "v" gnus-article-view-part
2046   "o" gnus-article-save-part
2047   "c" gnus-article-copy-part
2048   "C" gnus-article-view-part-as-charset
2049   "e" gnus-article-view-part-externally
2050   "E" gnus-article-encrypt-body
2051   "i" gnus-article-inline-part
2052   "|" gnus-article-pipe-part)
2053
2054 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2055   "p" gnus-summary-mark-as-processable
2056   "u" gnus-summary-unmark-as-processable
2057   "U" gnus-summary-unmark-all-processable
2058   "v" gnus-uu-mark-over
2059   "s" gnus-uu-mark-series
2060   "r" gnus-uu-mark-region
2061   "g" gnus-uu-unmark-region
2062   "R" gnus-uu-mark-by-regexp
2063   "G" gnus-uu-unmark-by-regexp
2064   "t" gnus-uu-mark-thread
2065   "T" gnus-uu-unmark-thread
2066   "a" gnus-uu-mark-all
2067   "b" gnus-uu-mark-buffer
2068   "S" gnus-uu-mark-sparse
2069   "k" gnus-summary-kill-process-mark
2070   "y" gnus-summary-yank-process-mark
2071   "w" gnus-summary-save-process-mark
2072   "i" gnus-uu-invert-processable)
2073
2074 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2075   ;;"x" gnus-uu-extract-any
2076   "m" gnus-summary-save-parts
2077   "u" gnus-uu-decode-uu
2078   "U" gnus-uu-decode-uu-and-save
2079   "s" gnus-uu-decode-unshar
2080   "S" gnus-uu-decode-unshar-and-save
2081   "o" gnus-uu-decode-save
2082   "O" gnus-uu-decode-save
2083   "b" gnus-uu-decode-binhex
2084   "B" gnus-uu-decode-binhex
2085   "p" gnus-uu-decode-postscript
2086   "P" gnus-uu-decode-postscript-and-save)
2087
2088 (gnus-define-keys
2089     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2090   "u" gnus-uu-decode-uu-view
2091   "U" gnus-uu-decode-uu-and-save-view
2092   "s" gnus-uu-decode-unshar-view
2093   "S" gnus-uu-decode-unshar-and-save-view
2094   "o" gnus-uu-decode-save-view
2095   "O" gnus-uu-decode-save-view
2096   "b" gnus-uu-decode-binhex-view
2097   "B" gnus-uu-decode-binhex-view
2098   "p" gnus-uu-decode-postscript-view
2099   "P" gnus-uu-decode-postscript-and-save-view)
2100
2101 (defvar gnus-article-post-menu nil)
2102
2103 (defconst gnus-summary-menu-maxlen 20)
2104
2105 (defun gnus-summary-menu-split (menu)
2106   ;; If we have lots of elements, divide them into groups of 20
2107   ;; and make a pane (or submenu) for each one.
2108   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2109       (let ((menu menu) sublists next
2110             (i 1))
2111         (while menu
2112           ;; Pull off the next gnus-summary-menu-maxlen elements
2113           ;; and make them the next element of sublist.
2114           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2115           (if next
2116               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2117                       nil))
2118           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2119                                              (aref (car (last menu)) 0)) menu)
2120                                sublists))
2121           (setq i (1+ i))
2122           (setq menu next))
2123         (nreverse sublists))
2124     ;; Few elements--put them all in one pane.
2125     menu))
2126
2127 (defun gnus-summary-make-menu-bar ()
2128   (gnus-turn-off-edit-menu 'summary)
2129
2130   (unless (boundp 'gnus-summary-misc-menu)
2131
2132     (easy-menu-define
2133       gnus-summary-kill-menu gnus-summary-mode-map ""
2134       (cons
2135        "Score"
2136        (nconc
2137         (list
2138          ["Customize" gnus-score-customize t])
2139         (gnus-make-score-map 'increase)
2140         (gnus-make-score-map 'lower)
2141         '(("Mark"
2142            ["Kill below" gnus-summary-kill-below t]
2143            ["Mark above" gnus-summary-mark-above t]
2144            ["Tick above" gnus-summary-tick-above t]
2145            ["Clear above" gnus-summary-clear-above t])
2146           ["Current score" gnus-summary-current-score t]
2147           ["Set score" gnus-summary-set-score t]
2148           ["Switch current score file..." gnus-score-change-score-file t]
2149           ["Set mark below..." gnus-score-set-mark-below t]
2150           ["Set expunge below..." gnus-score-set-expunge-below t]
2151           ["Edit current score file" gnus-score-edit-current-scores t]
2152           ["Edit score file" gnus-score-edit-file t]
2153           ["Trace score" gnus-score-find-trace t]
2154           ["Find words" gnus-score-find-favourite-words t]
2155           ["Rescore buffer" gnus-summary-rescore t]
2156           ["Increase score..." gnus-summary-increase-score t]
2157           ["Lower score..." gnus-summary-lower-score t]))))
2158
2159     ;; Define both the Article menu in the summary buffer and the
2160     ;; equivalent Commands menu in the article buffer here for
2161     ;; consistency.
2162     (let ((innards
2163            `(("Hide"
2164               ["All" gnus-article-hide t]
2165               ["Headers" gnus-article-hide-headers t]
2166               ["Signature" gnus-article-hide-signature t]
2167               ["Citation" gnus-article-hide-citation t]
2168               ["List identifiers" gnus-article-hide-list-identifiers t]
2169               ["Banner" gnus-article-strip-banner t]
2170               ["Boring headers" gnus-article-hide-boring-headers t])
2171              ("Highlight"
2172               ["All" gnus-article-highlight t]
2173               ["Headers" gnus-article-highlight-headers t]
2174               ["Signature" gnus-article-highlight-signature t]
2175               ["Citation" gnus-article-highlight-citation t])
2176              ("MIME"
2177               ["Words" gnus-article-decode-mime-words t]
2178               ["Charset" gnus-article-decode-charset t]
2179               ["QP" gnus-article-de-quoted-unreadable t]
2180               ["Base64" gnus-article-de-base64-unreadable t]
2181               ["View MIME buttons" gnus-summary-display-buttonized t]
2182               ["View all" gnus-mime-view-all-parts t]
2183               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2184               ["Encrypt body" gnus-article-encrypt-body
2185                :active (not (gnus-group-read-only-p))
2186                ,@(if (featurep 'xemacs) nil
2187                    '(:help "Encrypt the message body on disk"))]
2188               ["Extract all parts..." gnus-summary-save-parts t]
2189               ("Multipart"
2190                ["Repair multipart" gnus-summary-repair-multipart t]
2191                ["Pipe part..." gnus-article-pipe-part t]
2192                ["Inline part" gnus-article-inline-part t]
2193                ["Encrypt body" gnus-article-encrypt-body
2194                 :active (not (gnus-group-read-only-p))
2195                ,@(if (featurep 'xemacs) nil
2196                    '(:help "Encrypt the message body on disk"))]
2197                ["View part externally" gnus-article-view-part-externally t]
2198                ["View part with charset..." gnus-article-view-part-as-charset t]
2199                ["Copy part" gnus-article-copy-part t]
2200                ["Save part..." gnus-article-save-part t]
2201                ["View part" gnus-article-view-part t]))
2202              ("Date"
2203               ["Local" gnus-article-date-local t]
2204               ["ISO8601" gnus-article-date-iso8601 t]
2205               ["UT" gnus-article-date-ut t]
2206               ["Original" gnus-article-date-original t]
2207               ["Lapsed" gnus-article-date-lapsed t]
2208               ["User-defined" gnus-article-date-user t])
2209              ("Display"
2210               ["Remove images" gnus-article-remove-images t]
2211               ["Toggle smiley" gnus-treat-smiley t]
2212               ["Show X-Face" gnus-article-display-x-face t]
2213               ["Show picons in From" gnus-treat-from-picon t]
2214               ["Show picons in mail headers" gnus-treat-mail-picon t]
2215               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2216               ("View as different encoding"
2217                ,@(gnus-summary-menu-split
2218                   (mapcar
2219                    (lambda (cs)
2220                      ;; Since easymenu under Emacs doesn't allow
2221                      ;; lambda forms for menu commands, we should
2222                      ;; provide intern'ed function symbols.
2223                      (let ((command (intern (format "\
2224 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2225                        (fset command
2226                              `(lambda ()
2227                                 (interactive)
2228                                 (let ((gnus-summary-show-article-charset-alist
2229                                        '((1 . ,cs))))
2230                                   (gnus-summary-show-article 1))))
2231                        `[,(symbol-name cs) ,command t]))
2232                    (sort (if (fboundp 'coding-system-list)
2233                              (coding-system-list)
2234                            (mapcar 'car mm-mime-mule-charset-alist))
2235                          'string<)))))
2236              ("Washing"
2237               ("Remove Blanks"
2238                ["Leading" gnus-article-strip-leading-blank-lines t]
2239                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2240                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2241                ["All of the above" gnus-article-strip-blank-lines t]
2242                ["All" gnus-article-strip-all-blank-lines t]
2243                ["Leading space" gnus-article-strip-leading-space t]
2244                ["Trailing space" gnus-article-strip-trailing-space t]
2245                ["Leading space in headers"
2246                 gnus-article-remove-leading-whitespace t])
2247               ["Overstrike" gnus-article-treat-overstrike t]
2248               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2249               ["Emphasis" gnus-article-emphasize t]
2250               ["Word wrap" gnus-article-fill-cited-article t]
2251               ["Fill long lines" gnus-article-fill-long-lines t]
2252               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2253               ["Remove CR" gnus-article-remove-cr t]
2254               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2255               ["Base64" gnus-article-de-base64-unreadable t]
2256               ["Rot 13" gnus-summary-caesar-message
2257                ,@(if (featurep 'xemacs) '(t)
2258                    '(:help "\"Caesar rotate\" article by 13"))]
2259               ["Morse decode" gnus-summary-morse-message t]
2260               ["Unix pipe..." gnus-summary-pipe-message t]
2261               ["Add buttons" gnus-article-add-buttons t]
2262               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2263               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2264               ["Verbose header" gnus-summary-verbose-headers t]
2265               ["Toggle header" gnus-summary-toggle-header t]
2266               ["Unfold headers" gnus-article-treat-unfold-headers t]
2267               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2268               ["Html" gnus-article-wash-html t]
2269               ["Unsplit URLs" gnus-article-unsplit-urls t]
2270               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2271               ["Decode HZ" gnus-article-decode-HZ t]
2272               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2273               ("(Outlook) Deuglify"
2274                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2275                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2276                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2277                ["Full (Outlook) deuglify"
2278                 gnus-article-outlook-deuglify-article t])
2279               )
2280              ("Output"
2281               ["Save in default format..." gnus-summary-save-article
2282                ,@(if (featurep 'xemacs) '(t)
2283                    '(:help "Save article using default method"))]
2284               ["Save in file..." gnus-summary-save-article-file
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "Save article in file"))]
2287               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2288               ["Save in MH folder..." gnus-summary-save-article-folder t]
2289               ["Save in VM folder..." gnus-summary-save-article-vm t]
2290               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2291               ["Save body in file..." gnus-summary-save-article-body-file t]
2292               ["Pipe through a filter..." gnus-summary-pipe-output t]
2293               ["Add to SOUP packet" gnus-soup-add-article t]
2294               ["Print with Muttprint..." gnus-summary-muttprint t]
2295               ["Print" gnus-summary-print-article
2296                ,@(if (featurep 'xemacs) '(t)
2297                    '(:help "Generate and print a PostScript image"))])
2298              ("Copy, move,... (Backend)"
2299               ,@(if (featurep 'xemacs) nil
2300                   '(:help "Copying, moving, expiring articles..."))
2301               ["Respool article..." gnus-summary-respool-article t]
2302               ["Move article..." gnus-summary-move-article
2303                (gnus-check-backend-function
2304                 'request-move-article gnus-newsgroup-name)]
2305               ["Copy article..." gnus-summary-copy-article t]
2306               ["Crosspost article..." gnus-summary-crosspost-article
2307                (gnus-check-backend-function
2308                 'request-replace-article gnus-newsgroup-name)]
2309               ["Import file..." gnus-summary-import-article
2310                (gnus-check-backend-function
2311                 'request-accept-article gnus-newsgroup-name)]
2312               ["Create article..." gnus-summary-create-article
2313                (gnus-check-backend-function
2314                 'request-accept-article gnus-newsgroup-name)]
2315               ["Check if posted" gnus-summary-article-posted-p t]
2316               ["Edit article" gnus-summary-edit-article
2317                (not (gnus-group-read-only-p))]
2318               ["Delete article" gnus-summary-delete-article
2319                (gnus-check-backend-function
2320                 'request-expire-articles gnus-newsgroup-name)]
2321               ["Query respool" gnus-summary-respool-query t]
2322               ["Trace respool" gnus-summary-respool-trace t]
2323               ["Delete expirable articles" gnus-summary-expire-articles-now
2324                (gnus-check-backend-function
2325                 'request-expire-articles gnus-newsgroup-name)])
2326              ("Extract"
2327               ["Uudecode" gnus-uu-decode-uu
2328                ,@(if (featurep 'xemacs) '(t)
2329                    '(:help "Decode uuencoded article(s)"))]
2330               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2331               ["Unshar" gnus-uu-decode-unshar t]
2332               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2333               ["Save" gnus-uu-decode-save t]
2334               ["Binhex" gnus-uu-decode-binhex t]
2335               ["Postscript" gnus-uu-decode-postscript t]
2336               ["All MIME parts" gnus-summary-save-parts t])
2337              ("Cache"
2338               ["Enter article" gnus-cache-enter-article t]
2339               ["Remove article" gnus-cache-remove-article t])
2340              ["Translate" gnus-article-babel t]
2341              ["Select article buffer" gnus-summary-select-article-buffer t]
2342              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2343              ["Isearch article..." gnus-summary-isearch-article t]
2344              ["Beginning of the article" gnus-summary-beginning-of-article t]
2345              ["End of the article" gnus-summary-end-of-article t]
2346              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2347              ["Fetch referenced articles" gnus-summary-refer-references t]
2348              ["Fetch current thread" gnus-summary-refer-thread t]
2349              ["Fetch article with id..." gnus-summary-refer-article t]
2350              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2351              ["Redisplay" gnus-summary-show-article t]
2352              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2353       (easy-menu-define
2354         gnus-summary-article-menu gnus-summary-mode-map ""
2355         (cons "Article" innards))
2356
2357       (if (not (keymapp gnus-summary-article-menu))
2358           (easy-menu-define
2359             gnus-article-commands-menu gnus-article-mode-map ""
2360             (cons "Commands" innards))
2361         ;; in Emacs, don't share menu.
2362         (setq gnus-article-commands-menu
2363               (copy-keymap gnus-summary-article-menu))
2364         (define-key gnus-article-mode-map [menu-bar commands]
2365           (cons "Commands" gnus-article-commands-menu))))
2366
2367     (easy-menu-define
2368       gnus-summary-thread-menu gnus-summary-mode-map ""
2369       '("Threads"
2370         ["Find all messages in thread" gnus-summary-refer-thread t]
2371         ["Toggle threading" gnus-summary-toggle-threads t]
2372         ["Hide threads" gnus-summary-hide-all-threads t]
2373         ["Show threads" gnus-summary-show-all-threads t]
2374         ["Hide thread" gnus-summary-hide-thread t]
2375         ["Show thread" gnus-summary-show-thread t]
2376         ["Go to next thread" gnus-summary-next-thread t]
2377         ["Go to previous thread" gnus-summary-prev-thread t]
2378         ["Go down thread" gnus-summary-down-thread t]
2379         ["Go up thread" gnus-summary-up-thread t]
2380         ["Top of thread" gnus-summary-top-thread t]
2381         ["Mark thread as read" gnus-summary-kill-thread t]
2382         ["Lower thread score" gnus-summary-lower-thread t]
2383         ["Raise thread score" gnus-summary-raise-thread t]
2384         ["Rethread current" gnus-summary-rethread-current t]))
2385
2386     (easy-menu-define
2387       gnus-summary-post-menu gnus-summary-mode-map ""
2388       `("Post"
2389         ["Send a message (mail or news)" gnus-summary-post-news
2390          ,@(if (featurep 'xemacs) '(t)
2391              '(:help "Compose a new message (mail or news)"))]
2392         ["Followup" gnus-summary-followup
2393          ,@(if (featurep 'xemacs) '(t)
2394              '(:help "Post followup to this article"))]
2395         ["Followup and yank" gnus-summary-followup-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Post followup to this article, quoting its contents"))]
2398         ["Supersede article" gnus-summary-supersede-article t]
2399         ["Cancel article" gnus-summary-cancel-article
2400          ,@(if (featurep 'xemacs) '(t)
2401              '(:help "Cancel an article you posted"))]
2402         ["Reply" gnus-summary-reply t]
2403         ["Reply and yank" gnus-summary-reply-with-original t]
2404         ["Wide reply" gnus-summary-wide-reply t]
2405         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2406          ,@(if (featurep 'xemacs) '(t)
2407              '(:help "Mail a reply, quoting this article"))]
2408         ["Very wide reply" gnus-summary-very-wide-reply t]
2409         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2410          ,@(if (featurep 'xemacs) '(t)
2411              '(:help "Mail a very wide reply, quoting this article"))]
2412         ["Mail forward" gnus-summary-mail-forward t]
2413         ["Post forward" gnus-summary-post-forward t]
2414         ["Digest and mail" gnus-uu-digest-mail-forward t]
2415         ["Digest and post" gnus-uu-digest-post-forward t]
2416         ["Resend message" gnus-summary-resend-message t]
2417         ["Resend message edit" gnus-summary-resend-message-edit t]
2418         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2419         ["Send a mail" gnus-summary-mail-other-window t]
2420         ["Create a local message" gnus-summary-news-other-window t]
2421         ["Uuencode and post" gnus-uu-post-news
2422          ,@(if (featurep 'xemacs) '(t)
2423              '(:help "Post a uuencoded article"))]
2424         ["Followup via news" gnus-summary-followup-to-mail t]
2425         ["Followup via news and yank"
2426          gnus-summary-followup-to-mail-with-original t]
2427         ;;("Draft"
2428         ;;["Send" gnus-summary-send-draft t]
2429         ;;["Send bounced" gnus-resend-bounced-mail t])
2430         ))
2431
2432     (cond
2433      ((not (keymapp gnus-summary-post-menu))
2434       (setq gnus-article-post-menu gnus-summary-post-menu))
2435      ((not gnus-article-post-menu)
2436       ;; Don't share post menu.
2437       (setq gnus-article-post-menu
2438             (copy-keymap gnus-summary-post-menu))))
2439     (define-key gnus-article-mode-map [menu-bar post]
2440       (cons "Post" gnus-article-post-menu))
2441
2442     (easy-menu-define
2443       gnus-summary-misc-menu gnus-summary-mode-map ""
2444       `("Gnus"
2445         ("Mark Read"
2446          ["Mark as read" gnus-summary-mark-as-read-forward t]
2447          ["Mark same subject and select"
2448           gnus-summary-kill-same-subject-and-select t]
2449          ["Mark same subject" gnus-summary-kill-same-subject t]
2450          ["Catchup" gnus-summary-catchup
2451           ,@(if (featurep 'xemacs) '(t)
2452               '(:help "Mark unread articles in this group as read"))]
2453          ["Catchup all" gnus-summary-catchup-all t]
2454          ["Catchup to here" gnus-summary-catchup-to-here t]
2455          ["Catchup from here" gnus-summary-catchup-from-here t]
2456          ["Catchup region" gnus-summary-mark-region-as-read
2457           (gnus-mark-active-p)]
2458          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2459         ("Mark Various"
2460          ["Tick" gnus-summary-tick-article-forward t]
2461          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2462          ["Remove marks" gnus-summary-clear-mark-forward t]
2463          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2464          ["Set bookmark" gnus-summary-set-bookmark t]
2465          ["Remove bookmark" gnus-summary-remove-bookmark t])
2466         ("Limit to"
2467          ["Marks..." gnus-summary-limit-to-marks t]
2468          ["Subject..." gnus-summary-limit-to-subject t]
2469          ["Author..." gnus-summary-limit-to-author t]
2470          ["Age..." gnus-summary-limit-to-age t]
2471          ["Extra..." gnus-summary-limit-to-extra t]
2472          ["Score..." gnus-summary-limit-to-score t]
2473          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2474          ["Unread" gnus-summary-limit-to-unread t]
2475          ["Unseen" gnus-summary-limit-to-unseen t]
2476          ["Replied" gnus-summary-limit-to-replied t]
2477          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2478          ["Next articles" gnus-summary-limit-to-articles t]
2479          ["Pop limit" gnus-summary-pop-limit t]
2480          ["Show dormant" gnus-summary-limit-include-dormant t]
2481          ["Hide childless dormant"
2482           gnus-summary-limit-exclude-childless-dormant t]
2483          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2484          ["Hide marked" gnus-summary-limit-exclude-marks t]
2485          ["Show expunged" gnus-summary-limit-include-expunged t])
2486         ("Process Mark"
2487          ["Set mark" gnus-summary-mark-as-processable t]
2488          ["Remove mark" gnus-summary-unmark-as-processable t]
2489          ["Remove all marks" gnus-summary-unmark-all-processable t]
2490          ["Invert marks" gnus-uu-invert-processable t]
2491          ["Mark above" gnus-uu-mark-over t]
2492          ["Mark series" gnus-uu-mark-series t]
2493          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2494          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2495          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2496          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2497          ["Mark all" gnus-uu-mark-all t]
2498          ["Mark buffer" gnus-uu-mark-buffer t]
2499          ["Mark sparse" gnus-uu-mark-sparse t]
2500          ["Mark thread" gnus-uu-mark-thread t]
2501          ["Unmark thread" gnus-uu-unmark-thread t]
2502          ("Process Mark Sets"
2503           ["Kill" gnus-summary-kill-process-mark t]
2504           ["Yank" gnus-summary-yank-process-mark
2505            gnus-newsgroup-process-stack]
2506           ["Save" gnus-summary-save-process-mark t]
2507           ["Run command on marked..." gnus-summary-universal-argument t]))
2508         ("Scroll article"
2509          ["Page forward" gnus-summary-next-page
2510           ,@(if (featurep 'xemacs) '(t)
2511               '(:help "Show next page of article"))]
2512          ["Page backward" gnus-summary-prev-page
2513           ,@(if (featurep 'xemacs) '(t)
2514               '(:help "Show previous page of article"))]
2515          ["Line forward" gnus-summary-scroll-up t])
2516         ("Move"
2517          ["Next unread article" gnus-summary-next-unread-article t]
2518          ["Previous unread article" gnus-summary-prev-unread-article t]
2519          ["Next article" gnus-summary-next-article t]
2520          ["Previous article" gnus-summary-prev-article t]
2521          ["Next unread subject" gnus-summary-next-unread-subject t]
2522          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2523          ["Next article same subject" gnus-summary-next-same-subject t]
2524          ["Previous article same subject" gnus-summary-prev-same-subject t]
2525          ["First unread article" gnus-summary-first-unread-article t]
2526          ["Best unread article" gnus-summary-best-unread-article t]
2527          ["Go to subject number..." gnus-summary-goto-subject t]
2528          ["Go to article number..." gnus-summary-goto-article t]
2529          ["Go to the last article" gnus-summary-goto-last-article t]
2530          ["Pop article off history" gnus-summary-pop-article t])
2531         ("Sort"
2532          ["Sort by number" gnus-summary-sort-by-number t]
2533          ["Sort by author" gnus-summary-sort-by-author t]
2534          ["Sort by subject" gnus-summary-sort-by-subject t]
2535          ["Sort by date" gnus-summary-sort-by-date t]
2536          ["Sort by score" gnus-summary-sort-by-score t]
2537          ["Sort by lines" gnus-summary-sort-by-lines t]
2538          ["Sort by characters" gnus-summary-sort-by-chars t]
2539          ["Randomize" gnus-summary-sort-by-random t]
2540          ["Original sort" gnus-summary-sort-by-original t])
2541         ("Help"
2542          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2543          ["Describe group" gnus-summary-describe-group t]
2544          ["Fetch charter" gnus-group-fetch-charter
2545           ,@(if (featurep 'xemacs) nil
2546               '(:help "Display the charter of the current group"))]
2547          ["Fetch control message" gnus-group-fetch-control
2548           ,@(if (featurep 'xemacs) nil
2549               '(:help "Display the archived control message for the current group"))]
2550          ["Read manual" gnus-info-find-node t])
2551         ("Modes"
2552          ["Pick and read" gnus-pick-mode t]
2553          ["Binary" gnus-binary-mode t])
2554         ("Regeneration"
2555          ["Regenerate" gnus-summary-prepare t]
2556          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2557          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2558          ["Toggle threading" gnus-summary-toggle-threads t])
2559         ["See old articles" gnus-summary-insert-old-articles t]
2560         ["See new articles" gnus-summary-insert-new-articles t]
2561         ["Filter articles..." gnus-summary-execute-command t]
2562         ["Run command on articles..." gnus-summary-universal-argument t]
2563         ["Search articles forward..." gnus-summary-search-article-forward t]
2564         ["Search articles backward..." gnus-summary-search-article-backward t]
2565         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2566         ["Expand window" gnus-summary-expand-window t]
2567         ["Expire expirable articles" gnus-summary-expire-articles
2568          (gnus-check-backend-function
2569           'request-expire-articles gnus-newsgroup-name)]
2570         ["Edit local kill file" gnus-summary-edit-local-kill t]
2571         ["Edit main kill file" gnus-summary-edit-global-kill t]
2572         ["Edit group parameters" gnus-summary-edit-parameters t]
2573         ["Customize group parameters" gnus-summary-customize-parameters t]
2574         ["Send a bug report" gnus-bug t]
2575         ("Exit"
2576          ["Catchup and exit" gnus-summary-catchup-and-exit
2577           ,@(if (featurep 'xemacs) '(t)
2578               '(:help "Mark unread articles in this group as read, then exit"))]
2579          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2580          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2581          ["Exit group" gnus-summary-exit
2582           ,@(if (featurep 'xemacs) '(t)
2583               '(:help "Exit current group, return to group selection mode"))]
2584          ["Exit group without updating" gnus-summary-exit-no-update t]
2585          ["Exit and goto next group" gnus-summary-next-group t]
2586          ["Exit and goto prev group" gnus-summary-prev-group t]
2587          ["Reselect group" gnus-summary-reselect-current-group t]
2588          ["Rescan group" gnus-summary-rescan-group t]
2589          ["Update dribble" gnus-summary-save-newsrc t])))
2590
2591     (gnus-run-hooks 'gnus-summary-menu-hook)))
2592
2593 (defvar gnus-summary-tool-bar-map nil)
2594
2595 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2596 (defun gnus-summary-make-tool-bar ()
2597   (if (and (fboundp 'tool-bar-add-item-from-menu)
2598            (default-value 'tool-bar-mode)
2599            (not gnus-summary-tool-bar-map))
2600       (setq gnus-summary-tool-bar-map
2601             (let ((tool-bar-map (make-sparse-keymap))
2602                   (load-path (mm-image-load-path)))
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-summary-post-news "post" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-summary-followup "followup" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-summary-reply "reply" gnus-summary-mode-map)
2617               (tool-bar-add-item-from-menu
2618                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2619               (tool-bar-add-item-from-menu
2620                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2621               (tool-bar-add-item-from-menu
2622                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2623               (tool-bar-add-item-from-menu
2624                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2625               (tool-bar-add-item-from-menu
2626                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2627               (tool-bar-add-item-from-menu
2628                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2629               (tool-bar-add-item-from-menu
2630                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2631               (tool-bar-add-item-from-menu
2632                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2633               (tool-bar-add-item-from-menu
2634                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2635               tool-bar-map)))
2636   (if gnus-summary-tool-bar-map
2637       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2638
2639 (defun gnus-score-set-default (var value)
2640   "A version of set that updates the GNU Emacs menu-bar."
2641   (set var value)
2642   ;; It is the message that forces the active status to be updated.
2643   (message ""))
2644
2645 (defun gnus-make-score-map (type)
2646   "Make a summary score map of type TYPE."
2647   (if t
2648       nil
2649     (let ((headers '(("author" "from" string)
2650                      ("subject" "subject" string)
2651                      ("article body" "body" string)
2652                      ("article head" "head" string)
2653                      ("xref" "xref" string)
2654                      ("extra header" "extra" string)
2655                      ("lines" "lines" number)
2656                      ("followups to author" "followup" string)))
2657           (types '((number ("less than" <)
2658                            ("greater than" >)
2659                            ("equal" =))
2660                    (string ("substring" s)
2661                            ("exact string" e)
2662                            ("fuzzy string" f)
2663                            ("regexp" r))))
2664           (perms '(("temporary" (current-time-string))
2665                    ("permanent" nil)
2666                    ("immediate" now)))
2667           header)
2668       (list
2669        (apply
2670         'nconc
2671         (list
2672          (if (eq type 'lower)
2673              "Lower score"
2674            "Increase score"))
2675         (let (outh)
2676           (while headers
2677             (setq header (car headers))
2678             (setq outh
2679                   (cons
2680                    (apply
2681                     'nconc
2682                     (list (car header))
2683                     (let ((ts (cdr (assoc (nth 2 header) types)))
2684                           outt)
2685                       (while ts
2686                         (setq outt
2687                               (cons
2688                                (apply
2689                                 'nconc
2690                                 (list (caar ts))
2691                                 (let ((ps perms)
2692                                       outp)
2693                                   (while ps
2694                                     (setq outp
2695                                           (cons
2696                                            (vector
2697                                             (caar ps)
2698                                             (list
2699                                              'gnus-summary-score-entry
2700                                              (nth 1 header)
2701                                              (if (or (string= (nth 1 header)
2702                                                               "head")
2703                                                      (string= (nth 1 header)
2704                                                               "body"))
2705                                                  ""
2706                                                (list 'gnus-summary-header
2707                                                      (nth 1 header)))
2708                                              (list 'quote (nth 1 (car ts)))
2709                                              (list 'gnus-score-delta-default
2710                                                    nil)
2711                                              (nth 1 (car ps))
2712                                              t)
2713                                             t)
2714                                            outp))
2715                                     (setq ps (cdr ps)))
2716                                   (list (nreverse outp))))
2717                                outt))
2718                         (setq ts (cdr ts)))
2719                       (list (nreverse outt))))
2720                    outh))
2721             (setq headers (cdr headers)))
2722           (list (nreverse outh))))))))
2723
2724 \f
2725
2726 (defun gnus-summary-mode (&optional group)
2727   "Major mode for reading articles.
2728
2729 All normal editing commands are switched off.
2730 \\<gnus-summary-mode-map>
2731 Each line in this buffer represents one article.  To read an
2732 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2733 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2734 respectively.
2735
2736 You can also post articles and send mail from this buffer.  To
2737 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2738 of an article, type `\\[gnus-summary-reply]'.
2739
2740 There are approx. one gazillion commands you can execute in this
2741 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2742
2743 The following commands are available:
2744
2745 \\{gnus-summary-mode-map}"
2746   (interactive)
2747   (kill-all-local-variables)
2748   (when (gnus-visual-p 'summary-menu 'menu)
2749     (gnus-summary-make-menu-bar)
2750     (gnus-summary-make-tool-bar))
2751   (gnus-summary-make-local-variables)
2752   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2753     (gnus-summary-make-local-variables))
2754   (gnus-make-thread-indent-array)
2755   (gnus-simplify-mode-line)
2756   (setq major-mode 'gnus-summary-mode)
2757   (setq mode-name "Summary")
2758   (make-local-variable 'minor-mode-alist)
2759   (use-local-map gnus-summary-mode-map)
2760   (buffer-disable-undo)
2761   (setq buffer-read-only t              ;Disable modification
2762         show-trailing-whitespace nil)
2763   (setq truncate-lines t)
2764   (setq selective-display t)
2765   (setq selective-display-ellipses t)   ;Display `...'
2766   (gnus-summary-set-display-table)
2767   (gnus-set-default-directory)
2768   (setq gnus-newsgroup-name group)
2769   (make-local-variable 'gnus-summary-line-format)
2770   (make-local-variable 'gnus-summary-line-format-spec)
2771   (make-local-variable 'gnus-summary-dummy-line-format)
2772   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2773   (make-local-variable 'gnus-summary-mark-positions)
2774   (gnus-make-local-hook 'pre-command-hook)
2775   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2776   (gnus-run-hooks 'gnus-summary-mode-hook)
2777   (turn-on-gnus-mailing-list-mode)
2778   (mm-enable-multibyte)
2779   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2780   (gnus-update-summary-mark-positions))
2781
2782 (defun gnus-summary-make-local-variables ()
2783   "Make all the local summary buffer variables."
2784   (let (global)
2785     (dolist (local gnus-summary-local-variables)
2786       (if (consp local)
2787           (progn
2788             (if (eq (cdr local) 'global)
2789                 ;; Copy the global value of the variable.
2790                 (setq global (symbol-value (car local)))
2791               ;; Use the value from the list.
2792               (setq global (eval (cdr local))))
2793             (set (make-local-variable (car local)) global))
2794         ;; Simple nil-valued local variable.
2795         (set (make-local-variable local) nil)))))
2796
2797 (defun gnus-summary-clear-local-variables ()
2798   (let ((locals gnus-summary-local-variables))
2799     (while locals
2800       (if (consp (car locals))
2801           (and (vectorp (caar locals))
2802                (set (caar locals) nil))
2803         (and (vectorp (car locals))
2804              (set (car locals) nil)))
2805       (setq locals (cdr locals)))))
2806
2807 ;; Summary data functions.
2808
2809 (defmacro gnus-data-number (data)
2810   `(car ,data))
2811
2812 (defmacro gnus-data-set-number (data number)
2813   `(setcar ,data ,number))
2814
2815 (defmacro gnus-data-mark (data)
2816   `(nth 1 ,data))
2817
2818 (defmacro gnus-data-set-mark (data mark)
2819   `(setcar (nthcdr 1 ,data) ,mark))
2820
2821 (defmacro gnus-data-pos (data)
2822   `(nth 2 ,data))
2823
2824 (defmacro gnus-data-set-pos (data pos)
2825   `(setcar (nthcdr 2 ,data) ,pos))
2826
2827 (defmacro gnus-data-header (data)
2828   `(nth 3 ,data))
2829
2830 (defmacro gnus-data-set-header (data header)
2831   `(setf (nth 3 ,data) ,header))
2832
2833 (defmacro gnus-data-level (data)
2834   `(nth 4 ,data))
2835
2836 (defmacro gnus-data-unread-p (data)
2837   `(= (nth 1 ,data) gnus-unread-mark))
2838
2839 (defmacro gnus-data-read-p (data)
2840   `(/= (nth 1 ,data) gnus-unread-mark))
2841
2842 (defmacro gnus-data-pseudo-p (data)
2843   `(consp (nth 3 ,data)))
2844
2845 (defmacro gnus-data-find (number)
2846   `(assq ,number gnus-newsgroup-data))
2847
2848 (defmacro gnus-data-find-list (number &optional data)
2849   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2850      (memq (assq ,number bdata)
2851            bdata)))
2852
2853 (defmacro gnus-data-make (number mark pos header level)
2854   `(list ,number ,mark ,pos ,header ,level))
2855
2856 (defun gnus-data-enter (after-article number mark pos header level offset)
2857   (let ((data (gnus-data-find-list after-article)))
2858     (unless data
2859       (error "No such article: %d" after-article))
2860     (setcdr data (cons (gnus-data-make number mark pos header level)
2861                        (cdr data)))
2862     (setq gnus-newsgroup-data-reverse nil)
2863     (gnus-data-update-list (cddr data) offset)))
2864
2865 (defun gnus-data-enter-list (after-article list &optional offset)
2866   (when list
2867     (let ((data (and after-article (gnus-data-find-list after-article)))
2868           (ilist list))
2869       (if (not (or data
2870                    after-article))
2871           (let ((odata gnus-newsgroup-data))
2872             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2873             (when offset
2874               (gnus-data-update-list odata offset)))
2875       ;; Find the last element in the list to be spliced into the main
2876         ;; list.
2877         (while (cdr list)
2878           (setq list (cdr list)))
2879         (if (not data)
2880             (progn
2881               (setcdr list gnus-newsgroup-data)
2882               (setq gnus-newsgroup-data ilist)
2883               (when offset
2884                 (gnus-data-update-list (cdr list) offset)))
2885           (setcdr list (cdr data))
2886           (setcdr data ilist)
2887           (when offset
2888             (gnus-data-update-list (cdr list) offset))))
2889       (setq gnus-newsgroup-data-reverse nil))))
2890
2891 (defun gnus-data-remove (article &optional offset)
2892   (let ((data gnus-newsgroup-data))
2893     (if (= (gnus-data-number (car data)) article)
2894         (progn
2895           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2896                 gnus-newsgroup-data-reverse nil)
2897           (when offset
2898             (gnus-data-update-list gnus-newsgroup-data offset)))
2899       (while (cdr data)
2900         (when (= (gnus-data-number (cadr data)) article)
2901           (setcdr data (cddr data))
2902           (when offset
2903             (gnus-data-update-list (cdr data) offset))
2904           (setq data nil
2905                 gnus-newsgroup-data-reverse nil))
2906         (setq data (cdr data))))))
2907
2908 (defmacro gnus-data-list (backward)
2909   `(if ,backward
2910        (or gnus-newsgroup-data-reverse
2911            (setq gnus-newsgroup-data-reverse
2912                  (reverse gnus-newsgroup-data)))
2913      gnus-newsgroup-data))
2914
2915 (defun gnus-data-update-list (data offset)
2916   "Add OFFSET to the POS of all data entries in DATA."
2917   (setq gnus-newsgroup-data-reverse nil)
2918   (while data
2919     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2920     (setq data (cdr data))))
2921
2922 (defun gnus-summary-article-pseudo-p (article)
2923   "Say whether this article is a pseudo article or not."
2924   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2925
2926 (defmacro gnus-summary-article-sparse-p (article)
2927   "Say whether this article is a sparse article or not."
2928   `(memq ,article gnus-newsgroup-sparse))
2929
2930 (defmacro gnus-summary-article-ancient-p (article)
2931   "Say whether this article is a sparse article or not."
2932   `(memq ,article gnus-newsgroup-ancient))
2933
2934 (defun gnus-article-parent-p (number)
2935   "Say whether this article is a parent or not."
2936   (let ((data (gnus-data-find-list number)))
2937     (and (cdr data)              ; There has to be an article after...
2938          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2939             (gnus-data-level (nth 1 data))))))
2940
2941 (defun gnus-article-children (number)
2942   "Return a list of all children to NUMBER."
2943   (let* ((data (gnus-data-find-list number))
2944          (level (gnus-data-level (car data)))
2945          children)
2946     (setq data (cdr data))
2947     (while (and data
2948                 (= (gnus-data-level (car data)) (1+ level)))
2949       (push (gnus-data-number (car data)) children)
2950       (setq data (cdr data)))
2951     children))
2952
2953 (defmacro gnus-summary-skip-intangible ()
2954   "If the current article is intangible, then jump to a different article."
2955   '(let ((to (get-text-property (point) 'gnus-intangible)))
2956      (and to (gnus-summary-goto-subject to))))
2957
2958 (defmacro gnus-summary-article-intangible-p ()
2959   "Say whether this article is intangible or not."
2960   '(get-text-property (point) 'gnus-intangible))
2961
2962 (defun gnus-article-read-p (article)
2963   "Say whether ARTICLE is read or not."
2964   (not (or (memq article gnus-newsgroup-marked)
2965            (memq article gnus-newsgroup-spam-marked)
2966            (memq article gnus-newsgroup-unreads)
2967            (memq article gnus-newsgroup-unselected)
2968            (memq article gnus-newsgroup-dormant))))
2969
2970 ;; Some summary mode macros.
2971
2972 (defmacro gnus-summary-article-number ()
2973   "The article number of the article on the current line.
2974 If there isn't an article number here, then we return the current
2975 article number."
2976   '(progn
2977      (gnus-summary-skip-intangible)
2978      (or (get-text-property (point) 'gnus-number)
2979          (gnus-summary-last-subject))))
2980
2981 (defmacro gnus-summary-article-header (&optional number)
2982   "Return the header of article NUMBER."
2983   `(gnus-data-header (gnus-data-find
2984                       ,(or number '(gnus-summary-article-number)))))
2985
2986 (defmacro gnus-summary-thread-level (&optional number)
2987   "Return the level of thread that starts with article NUMBER."
2988   `(if (and (eq gnus-summary-make-false-root 'dummy)
2989             (get-text-property (point) 'gnus-intangible))
2990        0
2991      (gnus-data-level (gnus-data-find
2992                        ,(or number '(gnus-summary-article-number))))))
2993
2994 (defmacro gnus-summary-article-mark (&optional number)
2995   "Return the mark of article NUMBER."
2996   `(gnus-data-mark (gnus-data-find
2997                     ,(or number '(gnus-summary-article-number)))))
2998
2999 (defmacro gnus-summary-article-pos (&optional number)
3000   "Return the position of the line of article NUMBER."
3001   `(gnus-data-pos (gnus-data-find
3002                    ,(or number '(gnus-summary-article-number)))))
3003
3004 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3005 (defmacro gnus-summary-article-subject (&optional number)
3006   "Return current subject string or nil if nothing."
3007   `(let ((headers
3008           ,(if number
3009                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3010              '(gnus-data-header (assq (gnus-summary-article-number)
3011                                       gnus-newsgroup-data)))))
3012      (and headers
3013           (vectorp headers)
3014           (mail-header-subject headers))))
3015
3016 (defmacro gnus-summary-article-score (&optional number)
3017   "Return current article score."
3018   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3019                   gnus-newsgroup-scored))
3020        gnus-summary-default-score 0))
3021
3022 (defun gnus-summary-article-children (&optional number)
3023   "Return a list of article numbers that are children of article NUMBER."
3024   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3025          (level (gnus-data-level (car data)))
3026          l children)
3027     (while (and (setq data (cdr data))
3028                 (> (setq l (gnus-data-level (car data))) level))
3029       (and (= (1+ level) l)
3030            (push (gnus-data-number (car data))
3031                  children)))
3032     (nreverse children)))
3033
3034 (defun gnus-summary-article-parent (&optional number)
3035   "Return the article number of the parent of article NUMBER."
3036   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3037                                     (gnus-data-list t)))
3038          (level (gnus-data-level (car data))))
3039     (if (zerop level)
3040         ()                              ; This is a root.
3041       ;; We search until we find an article with a level less than
3042       ;; this one.  That function has to be the parent.
3043       (while (and (setq data (cdr data))
3044                   (not (< (gnus-data-level (car data)) level))))
3045       (and data (gnus-data-number (car data))))))
3046
3047 (defun gnus-unread-mark-p (mark)
3048   "Say whether MARK is the unread mark."
3049   (= mark gnus-unread-mark))
3050
3051 (defun gnus-read-mark-p (mark)
3052   "Say whether MARK is one of the marks that mark as read.
3053 This is all marks except unread, ticked, dormant, and expirable."
3054   (not (or (= mark gnus-unread-mark)
3055            (= mark gnus-ticked-mark)
3056            (= mark gnus-spam-mark)
3057            (= mark gnus-dormant-mark)
3058            (= mark gnus-expirable-mark))))
3059
3060 (defmacro gnus-article-mark (number)
3061   "Return the MARK of article NUMBER.
3062 This macro should only be used when computing the mark the \"first\"
3063 time; i.e., when generating the summary lines.  After that,
3064 `gnus-summary-article-mark' should be used to examine the
3065 marks of articles."
3066   `(cond
3067     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3068     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3069     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3070     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3071     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3072     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3073     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3074     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3075            gnus-ancient-mark))))
3076
3077 ;; Saving hidden threads.
3078
3079 (defmacro gnus-save-hidden-threads (&rest forms)
3080   "Save hidden threads, eval FORMS, and restore the hidden threads."
3081   (let ((config (make-symbol "config")))
3082     `(let ((,config (gnus-hidden-threads-configuration)))
3083        (unwind-protect
3084            (save-excursion
3085              ,@forms)
3086          (gnus-restore-hidden-threads-configuration ,config)))))
3087 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3088 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3089
3090 (defun gnus-data-compute-positions ()
3091   "Compute the positions of all articles."
3092   (setq gnus-newsgroup-data-reverse nil)
3093   (let ((data gnus-newsgroup-data))
3094     (save-excursion
3095       (gnus-save-hidden-threads
3096         (gnus-summary-show-all-threads)
3097         (goto-char (point-min))
3098         (while data
3099           (while (get-text-property (point) 'gnus-intangible)
3100             (forward-line 1))
3101           (gnus-data-set-pos (car data) (+ (point) 3))
3102           (setq data (cdr data))
3103           (forward-line 1))))))
3104
3105 (defun gnus-hidden-threads-configuration ()
3106   "Return the current hidden threads configuration."
3107   (save-excursion
3108     (let (config)
3109       (goto-char (point-min))
3110       (while (search-forward "\r" nil t)
3111         (push (1- (point)) config))
3112       config)))
3113
3114 (defun gnus-restore-hidden-threads-configuration (config)
3115   "Restore hidden threads configuration from CONFIG."
3116   (save-excursion
3117     (let (point buffer-read-only)
3118       (while (setq point (pop config))
3119         (when (and (< point (point-max))
3120                    (goto-char point)
3121                    (eq (char-after) ?\n))
3122           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3123
3124 ;; Various summary mode internalish functions.
3125
3126 (defun gnus-mouse-pick-article (e)
3127   (interactive "e")
3128   (mouse-set-point e)
3129   (gnus-summary-next-page nil t))
3130
3131 (defun gnus-summary-set-display-table ()
3132   "Change the display table.
3133 Odd characters have a tendency to mess
3134 up nicely formatted displays - we make all possible glyphs
3135 display only a single character."
3136
3137   ;; We start from the standard display table, if any.
3138   (let ((table (or (copy-sequence standard-display-table)
3139                    (make-display-table)))
3140         (i 32))
3141     ;; Nix out all the control chars...
3142     (while (>= (setq i (1- i)) 0)
3143       (aset table i [??]))
3144    ;; ... but not newline and cr, of course.  (cr is necessary for the
3145     ;; selective display).
3146     (aset table ?\n nil)
3147     (aset table ?\r nil)
3148     ;; We keep TAB as well.
3149     (aset table ?\t nil)
3150     ;; We nix out any glyphs over 126 that are not set already.
3151     (let ((i 256))
3152       (while (>= (setq i (1- i)) 127)
3153         ;; Only modify if the entry is nil.
3154         (unless (aref table i)
3155           (aset table i [??]))))
3156     (setq buffer-display-table table)))
3157
3158 (defun gnus-summary-set-article-display-arrow (pos)
3159   "Update the overlay arrow to point to line at position POS."
3160   (when (and gnus-summary-display-arrow
3161              (boundp 'overlay-arrow-position)
3162              (boundp 'overlay-arrow-string))
3163     (save-excursion
3164       (goto-char pos)
3165       (beginning-of-line)
3166       (unless overlay-arrow-position
3167         (setq overlay-arrow-position (make-marker)))
3168       (setq overlay-arrow-string "=>"
3169             overlay-arrow-position (set-marker overlay-arrow-position
3170                                                (point)
3171                                                (current-buffer))))))
3172
3173 (defun gnus-summary-setup-buffer (group)
3174   "Initialize summary buffer."
3175   (let ((buffer (gnus-summary-buffer-name group))
3176         (dead-name (concat "*Dead Summary "
3177                            (gnus-group-decoded-name group) "*")))
3178     ;; If a dead summary buffer exists, we kill it.
3179     (when (gnus-buffer-live-p dead-name)
3180       (gnus-kill-buffer dead-name))
3181     (if (get-buffer buffer)
3182         (progn
3183           (set-buffer buffer)
3184           (setq gnus-summary-buffer (current-buffer))
3185           (not gnus-newsgroup-prepared))
3186       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3187       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3188       (gnus-summary-mode group)
3189       (when gnus-carpal
3190         (gnus-carpal-setup-buffer 'summary))
3191       (unless gnus-single-article-buffer
3192         (make-local-variable 'gnus-article-buffer)
3193         (make-local-variable 'gnus-article-current)
3194         (make-local-variable 'gnus-original-article-buffer))
3195       (setq gnus-newsgroup-name group)
3196       ;; Set any local variables in the group parameters.
3197       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3198       t)))
3199
3200 (defun gnus-set-global-variables ()
3201   "Set the global equivalents of the buffer-local variables.
3202 They are set to the latest values they had.  These reflect the summary
3203 buffer that was in action when the last article was fetched."
3204   (when (eq major-mode 'gnus-summary-mode)
3205     (setq gnus-summary-buffer (current-buffer))
3206     (let ((name gnus-newsgroup-name)
3207           (marked gnus-newsgroup-marked)
3208           (spam gnus-newsgroup-spam-marked)
3209           (unread gnus-newsgroup-unreads)
3210           (headers gnus-current-headers)
3211           (data gnus-newsgroup-data)
3212           (summary gnus-summary-buffer)
3213           (article-buffer gnus-article-buffer)
3214           (original gnus-original-article-buffer)
3215           (gac gnus-article-current)
3216           (reffed gnus-reffed-article-number)
3217           (score-file gnus-current-score-file)
3218           (default-charset gnus-newsgroup-charset)
3219           vlist)
3220       (let ((locals gnus-newsgroup-variables))
3221         (while locals
3222           (if (consp (car locals))
3223               (push (eval (caar locals)) vlist)
3224             (push (eval (car locals)) vlist))
3225           (setq locals (cdr locals)))
3226         (setq vlist (nreverse vlist)))
3227       (with-current-buffer gnus-group-buffer
3228         (setq gnus-newsgroup-name name
3229               gnus-newsgroup-marked marked
3230               gnus-newsgroup-spam-marked spam
3231               gnus-newsgroup-unreads unread
3232               gnus-current-headers headers
3233               gnus-newsgroup-data data
3234               gnus-article-current gac
3235               gnus-summary-buffer summary
3236               gnus-article-buffer article-buffer
3237               gnus-original-article-buffer original
3238               gnus-reffed-article-number reffed
3239               gnus-current-score-file score-file
3240               gnus-newsgroup-charset default-charset)
3241         (let ((locals gnus-newsgroup-variables))
3242           (while locals
3243             (if (consp (car locals))
3244                 (set (caar locals) (pop vlist))
3245               (set (car locals) (pop vlist)))
3246             (setq locals (cdr locals))))
3247         ;; The article buffer also has local variables.
3248         (when (gnus-buffer-live-p gnus-article-buffer)
3249           (set-buffer gnus-article-buffer)
3250           (setq gnus-summary-buffer summary))))))
3251
3252 (defun gnus-summary-article-unread-p (article)
3253   "Say whether ARTICLE is unread or not."
3254   (memq article gnus-newsgroup-unreads))
3255
3256 (defun gnus-summary-first-article-p (&optional article)
3257   "Return whether ARTICLE is the first article in the buffer."
3258   (if (not (setq article (or article (gnus-summary-article-number))))
3259       nil
3260     (eq article (caar gnus-newsgroup-data))))
3261
3262 (defun gnus-summary-last-article-p (&optional article)
3263   "Return whether ARTICLE is the last article in the buffer."
3264   (if (not (setq article (or article (gnus-summary-article-number))))
3265       ;; All non-existent numbers are the last article.  :-)
3266       t
3267     (not (cdr (gnus-data-find-list article)))))
3268
3269 (defun gnus-make-thread-indent-array ()
3270   (let ((n 200))
3271     (unless (and gnus-thread-indent-array
3272                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3273       (setq gnus-thread-indent-array (make-vector 201 "")
3274             gnus-thread-indent-array-level gnus-thread-indent-level)
3275       (while (>= n 0)
3276         (aset gnus-thread-indent-array n
3277               (make-string (* n gnus-thread-indent-level) ? ))
3278         (setq n (1- n))))))
3279
3280 (defun gnus-update-summary-mark-positions ()
3281   "Compute where the summary marks are to go."
3282   (save-excursion
3283     (when (gnus-buffer-exists-p gnus-summary-buffer)
3284       (set-buffer gnus-summary-buffer))
3285     (let ((gnus-replied-mark 129)
3286           (gnus-score-below-mark 130)
3287           (gnus-score-over-mark 130)
3288           (gnus-undownloaded-mark 131)
3289           (spec gnus-summary-line-format-spec)
3290           gnus-visual pos)
3291       (save-excursion
3292         (gnus-set-work-buffer)
3293         (let ((gnus-summary-line-format-spec spec)
3294               (gnus-newsgroup-downloadable '(0)))
3295           (gnus-summary-insert-line
3296            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3297            0 nil t 128 t nil "" nil 1)
3298           (goto-char (point-min))
3299           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3300                                              (- (point) (point-min) 1)))))
3301           (goto-char (point-min))
3302           (push (cons 'replied (and (search-forward "\201" nil t)
3303                                     (- (point) (point-min) 1)))
3304                 pos)
3305           (goto-char (point-min))
3306           (push (cons 'score (and (search-forward "\202" nil t)
3307                                   (- (point) (point-min) 1)))
3308                 pos)
3309           (goto-char (point-min))
3310           (push (cons 'download
3311                       (and (search-forward "\203" nil t)
3312                            (- (point) (point-min) 1)))
3313                 pos)))
3314       (setq gnus-summary-mark-positions pos))))
3315
3316 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3317   "Insert a dummy root in the summary buffer."
3318   (beginning-of-line)
3319   (gnus-add-text-properties
3320    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3321    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3322
3323 (defun gnus-summary-extract-address-component (from)
3324   (or (car (funcall gnus-extract-address-components from))
3325       from))
3326
3327 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3328   (let ((mail-parse-charset gnus-newsgroup-charset)
3329         ; Is it really necessary to do this next part for each summary line?
3330         ; Luckily, doesn't seem to slow things down much.
3331         (mail-parse-ignored-charsets
3332          (with-current-buffer gnus-summary-buffer
3333            gnus-newsgroup-ignored-charsets)))
3334     (or
3335      (and gnus-ignored-from-addresses
3336           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3337           (let ((extra-headers (mail-header-extra header))
3338                 to
3339                 newsgroups)
3340             (cond
3341              ((setq to (cdr (assq 'To extra-headers)))
3342               (concat "-> "
3343                       (inline
3344                         (gnus-summary-extract-address-component
3345                          (funcall gnus-decode-encoded-word-function to)))))
3346              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3347               (concat "=> " newsgroups)))))
3348      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3349
3350 (defun gnus-summary-insert-line (gnus-tmp-header
3351                                  gnus-tmp-level gnus-tmp-current
3352                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3353                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3354                                  &optional gnus-tmp-dummy gnus-tmp-score
3355                                  gnus-tmp-process)
3356   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3357          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3358          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3359          (gnus-tmp-score-char
3360           (if (or (null gnus-summary-default-score)
3361                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3362                       gnus-summary-zcore-fuzz))
3363               ?                         ;Whitespace
3364             (if (< gnus-tmp-score gnus-summary-default-score)
3365                 gnus-score-below-mark gnus-score-over-mark)))
3366          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3367          (gnus-tmp-replied
3368           (cond (gnus-tmp-process gnus-process-mark)
3369                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3370                  gnus-cached-mark)
3371                 (gnus-tmp-replied gnus-replied-mark)
3372                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3373                  gnus-forwarded-mark)
3374                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3375                  gnus-saved-mark)
3376                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3377                  gnus-recent-mark)
3378                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3379                  gnus-unseen-mark)
3380                 (t gnus-no-mark)))
3381          (gnus-tmp-downloaded
3382           (cond (undownloaded
3383                  gnus-undownloaded-mark)
3384                 (gnus-newsgroup-agentized
3385                  gnus-downloaded-mark)
3386                 (t
3387                  gnus-no-mark)))
3388          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3389          (gnus-tmp-name
3390           (cond
3391            ((string-match "<[^>]+> *$" gnus-tmp-from)
3392             (let ((beg (match-beginning 0)))
3393               (or (and (string-match "^\".+\"" gnus-tmp-from)
3394                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3395                   (substring gnus-tmp-from 0 beg))))
3396            ((string-match "(.+)" gnus-tmp-from)
3397             (substring gnus-tmp-from
3398                        (1+ (match-beginning 0)) (1- (match-end 0))))
3399            (t gnus-tmp-from)))
3400          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3401          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3402          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3403          (buffer-read-only nil))
3404     (when (string= gnus-tmp-name "")
3405       (setq gnus-tmp-name gnus-tmp-from))
3406     (unless (numberp gnus-tmp-lines)
3407       (setq gnus-tmp-lines -1))
3408     (if (= gnus-tmp-lines -1)
3409         (setq gnus-tmp-lines "?")
3410       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3411       (gnus-put-text-property
3412      (point)
3413      (progn (eval gnus-summary-line-format-spec) (point))
3414        'gnus-number gnus-tmp-number)
3415     (when (gnus-visual-p 'summary-highlight 'highlight)
3416       (forward-line -1)
3417       (gnus-run-hooks 'gnus-summary-update-hook)
3418       (forward-line 1))))
3419
3420 (defun gnus-summary-update-line (&optional dont-update)
3421   "Update summary line after change."
3422   (when (and gnus-summary-default-score
3423              (not gnus-summary-inhibit-highlight))
3424     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3425            (article (gnus-summary-article-number))
3426            (score (gnus-summary-article-score article)))
3427       (unless dont-update
3428         (if (and gnus-summary-mark-below
3429                  (< (gnus-summary-article-score)
3430                     gnus-summary-mark-below))
3431             ;; This article has a low score, so we mark it as read.
3432             (when (memq article gnus-newsgroup-unreads)
3433               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3434           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3435             ;; This article was previously marked as read on account
3436             ;; of a low score, but now it has risen, so we mark it as
3437             ;; unread.
3438             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3439         (gnus-summary-update-mark
3440          (if (or (null gnus-summary-default-score)
3441                  (<= (abs (- score gnus-summary-default-score))
3442                      gnus-summary-zcore-fuzz))
3443              ?                          ;Whitespace
3444            (if (< score gnus-summary-default-score)
3445                gnus-score-below-mark gnus-score-over-mark))
3446          'score))
3447       ;; Do visual highlighting.
3448       (when (gnus-visual-p 'summary-highlight 'highlight)
3449         (gnus-run-hooks 'gnus-summary-update-hook)))))
3450
3451 (defvar gnus-tmp-new-adopts nil)
3452
3453 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3454   "Return the number of articles in THREAD.
3455 This may be 0 in some cases -- if none of the articles in
3456 the thread are to be displayed."
3457   (let* ((number
3458          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3459           (cond
3460            ((not (listp thread))
3461             1)
3462            ((and (consp thread) (cdr thread))
3463             (apply
3464              '+ 1 (mapcar
3465                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3466            ((null thread)
3467             1)
3468            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3469             1)
3470            (t 0))))
3471     (when (and level (zerop level) gnus-tmp-new-adopts)
3472       (incf number
3473             (apply '+ (mapcar
3474                        'gnus-summary-number-of-articles-in-thread
3475                        gnus-tmp-new-adopts))))
3476     (if char
3477         (if (> number 1) gnus-not-empty-thread-mark
3478           gnus-empty-thread-mark)
3479       number)))
3480
3481 (defsubst gnus-summary-line-message-size (head)
3482   "Return pretty-printed version of message size.
3483 This function is intended to be used in
3484 `gnus-summary-line-format-alist'."
3485   (let ((c (or (mail-header-chars head) -1)))
3486     (cond ((< c 0) "n/a")               ; chars not available
3487           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3488           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3489           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3490           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3491
3492
3493 (defun gnus-summary-set-local-parameters (group)
3494   "Go through the local params of GROUP and set all variable specs in that list."
3495   (let ((params (gnus-group-find-parameter group))
3496         (vars '(quit-config))           ; Ignore quit-config.
3497         elem)
3498     (while params
3499       (setq elem (car params)
3500             params (cdr params))
3501       (and (consp elem)                 ; Has to be a cons.
3502            (consp (cdr elem))           ; The cdr has to be a list.
3503            (symbolp (car elem))         ; Has to be a symbol in there.
3504            (not (memq (car elem) vars))
3505            (ignore-errors               ; So we set it.
3506              (push (car elem) vars)
3507              (make-local-variable (car elem))
3508              (set (car elem) (eval (nth 1 elem))))))))
3509
3510 (defun gnus-summary-read-group (group &optional show-all no-article
3511                                       kill-buffer no-display backward
3512                                       select-articles)
3513   "Start reading news in newsgroup GROUP.
3514 If SHOW-ALL is non-nil, already read articles are also listed.
3515 If NO-ARTICLE is non-nil, no article is selected initially.
3516 If NO-DISPLAY, don't generate a summary buffer."
3517   (let (result)
3518     (while (and group
3519                 (null (setq result
3520                             (let ((gnus-auto-select-next nil))
3521                               (or (gnus-summary-read-group-1
3522                                    group show-all no-article
3523                                    kill-buffer no-display
3524                                    select-articles)
3525                                   (setq show-all nil
3526                                         select-articles nil)))))
3527                 (eq gnus-auto-select-next 'quietly))
3528       (set-buffer gnus-group-buffer)
3529       ;; The entry function called above goes to the next
3530       ;; group automatically, so we go two groups back
3531       ;; if we are searching for the previous group.
3532       (when backward
3533         (gnus-group-prev-unread-group 2))
3534       (if (not (equal group (gnus-group-group-name)))
3535           (setq group (gnus-group-group-name))
3536         (setq group nil)))
3537     result))
3538
3539 (defun gnus-summary-read-group-1 (group show-all no-article
3540                                         kill-buffer no-display
3541                                         &optional select-articles)
3542   ;; Killed foreign groups can't be entered.
3543   ;;  (when (and (not (gnus-group-native-p group))
3544   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3545   ;;    (error "Dead non-native groups can't be entered"))
3546   (gnus-message 5 "Retrieving newsgroup: %s..."
3547                 (gnus-group-decoded-name group))
3548   (let* ((new-group (gnus-summary-setup-buffer group))
3549          (quit-config (gnus-group-quit-config group))
3550          (did-select (and new-group (gnus-select-newsgroup
3551                                      group show-all select-articles))))
3552     (cond
3553      ;; This summary buffer exists already, so we just select it.
3554      ((not new-group)
3555       (gnus-set-global-variables)
3556       (when kill-buffer
3557         (gnus-kill-or-deaden-summary kill-buffer))
3558       (gnus-configure-windows 'summary 'force)
3559       (gnus-set-mode-line 'summary)
3560       (gnus-summary-position-point)
3561       (message "")
3562       t)
3563      ;; We couldn't select this group.
3564      ((null did-select)
3565       (when (and (eq major-mode 'gnus-summary-mode)
3566                  (not (equal (current-buffer) kill-buffer)))
3567         (kill-buffer (current-buffer))
3568         (if (not quit-config)
3569             (progn
3570               ;; Update the info -- marks might need to be removed,
3571               ;; for instance.
3572               (gnus-summary-update-info)
3573               (set-buffer gnus-group-buffer)
3574               (gnus-group-jump-to-group group)
3575               (gnus-group-next-unread-group 1))
3576           (gnus-handle-ephemeral-exit quit-config)))
3577       (let ((grpinfo (gnus-get-info group)))
3578         (if (null (gnus-info-read grpinfo))
3579             (gnus-message 3 "Group %s contains no messages"
3580                           (gnus-group-decoded-name group))
3581           (gnus-message 3 "Can't select group")))
3582       nil)
3583      ;; The user did a `C-g' while prompting for number of articles,
3584      ;; so we exit this group.
3585      ((eq did-select 'quit)
3586       (and (eq major-mode 'gnus-summary-mode)
3587            (not (equal (current-buffer) kill-buffer))
3588            (kill-buffer (current-buffer)))
3589       (when kill-buffer
3590         (gnus-kill-or-deaden-summary kill-buffer))
3591       (if (not quit-config)
3592           (progn
3593             (set-buffer gnus-group-buffer)
3594             (gnus-group-jump-to-group group)
3595             (gnus-group-next-unread-group 1)
3596             (gnus-configure-windows 'group 'force))
3597         (gnus-handle-ephemeral-exit quit-config))
3598       ;; Finally signal the quit.
3599       (signal 'quit nil))
3600      ;; The group was successfully selected.
3601      (t
3602       (gnus-set-global-variables)
3603       ;; Save the active value in effect when the group was entered.
3604       (setq gnus-newsgroup-active
3605             (gnus-copy-sequence
3606              (gnus-active gnus-newsgroup-name)))
3607       ;; You can change the summary buffer in some way with this hook.
3608       (gnus-run-hooks 'gnus-select-group-hook)
3609       (gnus-update-format-specifications
3610        nil 'summary 'summary-mode 'summary-dummy)
3611       (gnus-update-summary-mark-positions)
3612       ;; Do score processing.
3613       (when gnus-use-scoring
3614         (gnus-possibly-score-headers))
3615       ;; Check whether to fill in the gaps in the threads.
3616       (when gnus-build-sparse-threads
3617         (gnus-build-sparse-threads))
3618       ;; Find the initial limit.
3619       (if show-all
3620           (let ((gnus-newsgroup-dormant nil))
3621             (gnus-summary-initial-limit show-all))
3622         (gnus-summary-initial-limit show-all))
3623       ;; Generate the summary buffer.
3624       (unless no-display
3625         (gnus-summary-prepare))
3626       (when gnus-use-trees
3627         (gnus-tree-open group)
3628         (setq gnus-summary-highlight-line-function
3629               'gnus-tree-highlight-article))
3630       ;; If the summary buffer is empty, but there are some low-scored
3631       ;; articles or some excluded dormants, we include these in the
3632       ;; buffer.
3633       (when (and (zerop (buffer-size))
3634                  (not no-display))
3635         (cond (gnus-newsgroup-dormant
3636                (gnus-summary-limit-include-dormant))
3637               ((and gnus-newsgroup-scored show-all)
3638                (gnus-summary-limit-include-expunged t))))
3639       ;; Function `gnus-apply-kill-file' must be called in this hook.
3640       (gnus-run-hooks 'gnus-apply-kill-hook)
3641       (if (and (zerop (buffer-size))
3642                (not no-display))
3643           (progn
3644             ;; This newsgroup is empty.
3645             (gnus-summary-catchup-and-exit nil t)
3646             (gnus-message 6 "No unread news")
3647             (when kill-buffer
3648               (gnus-kill-or-deaden-summary kill-buffer))
3649             ;; Return nil from this function.
3650             nil)
3651         ;; Hide conversation thread subtrees.  We cannot do this in
3652         ;; gnus-summary-prepare-hook since kill processing may not
3653         ;; work with hidden articles.
3654         (gnus-summary-maybe-hide-threads)
3655         (when kill-buffer
3656           (gnus-kill-or-deaden-summary kill-buffer))
3657         (gnus-summary-auto-select-subject)
3658         ;; Show first unread article if requested.
3659         (if (and (not no-article)
3660                  (not no-display)
3661                  gnus-newsgroup-unreads
3662                  gnus-auto-select-first)
3663             (progn
3664               (gnus-configure-windows 'summary)
3665               (let ((art (gnus-summary-article-number)))
3666                 (unless (and (not gnus-plugged)
3667                              (or (memq art gnus-newsgroup-undownloaded)
3668                                  (memq art gnus-newsgroup-downloadable)))
3669                   (gnus-summary-goto-article art))))
3670           ;; Don't select any articles.
3671           (gnus-summary-position-point)
3672           (gnus-configure-windows 'summary 'force)
3673           (gnus-set-mode-line 'summary))
3674         (when (and gnus-auto-center-group
3675                    (get-buffer-window gnus-group-buffer t))
3676           ;; Gotta use windows, because recenter does weird stuff if
3677           ;; the current buffer ain't the displayed window.
3678           (let ((owin (selected-window)))
3679             (select-window (get-buffer-window gnus-group-buffer t))
3680             (when (gnus-group-goto-group group)
3681               (recenter))
3682             (select-window owin)))
3683         ;; Mark this buffer as "prepared".
3684         (setq gnus-newsgroup-prepared t)
3685         (gnus-run-hooks 'gnus-summary-prepared-hook)
3686         (unless (gnus-ephemeral-group-p group)
3687           (gnus-group-update-group group))
3688         t)))))
3689
3690 (defun gnus-summary-auto-select-subject ()
3691   "Select the subject line on initial group entry."
3692   (goto-char (point-min))
3693   (cond
3694    ((eq gnus-auto-select-subject 'best)
3695     (gnus-summary-best-unread-subject))
3696    ((eq gnus-auto-select-subject 'unread)
3697     (gnus-summary-first-unread-subject))
3698    ((eq gnus-auto-select-subject 'unseen)
3699     (gnus-summary-first-unseen-subject))
3700    ((eq gnus-auto-select-subject 'unseen-or-unread)
3701     (gnus-summary-first-unseen-or-unread-subject))
3702    ((eq gnus-auto-select-subject 'first)
3703     ;; Do nothing.
3704     )
3705    ((functionp gnus-auto-select-subject)
3706     (funcall gnus-auto-select-subject))))
3707
3708 (defun gnus-summary-prepare ()
3709   "Generate the summary buffer."
3710   (interactive)
3711   (let ((buffer-read-only nil))
3712     (erase-buffer)
3713     (setq gnus-newsgroup-data nil
3714           gnus-newsgroup-data-reverse nil)
3715     (gnus-run-hooks 'gnus-summary-generate-hook)
3716     ;; Generate the buffer, either with threads or without.
3717     (when gnus-newsgroup-headers
3718       (gnus-summary-prepare-threads
3719        (if gnus-show-threads
3720            (gnus-sort-gathered-threads
3721             (funcall gnus-summary-thread-gathering-function
3722                      (gnus-sort-threads
3723                       (gnus-cut-threads (gnus-make-threads)))))
3724          ;; Unthreaded display.
3725          (gnus-sort-articles gnus-newsgroup-headers))))
3726     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3727     ;; Call hooks for modifying summary buffer.
3728     (goto-char (point-min))
3729     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3730
3731 (defsubst gnus-general-simplify-subject (subject)
3732   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3733   (setq subject
3734         (cond
3735          ;; Truncate the subject.
3736          (gnus-simplify-subject-functions
3737           (gnus-map-function gnus-simplify-subject-functions subject))
3738          ((numberp gnus-summary-gather-subject-limit)
3739           (setq subject (gnus-simplify-subject-re subject))
3740           (if (> (length subject) gnus-summary-gather-subject-limit)
3741               (substring subject 0 gnus-summary-gather-subject-limit)
3742             subject))
3743          ;; Fuzzily simplify it.
3744          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3745           (gnus-simplify-subject-fuzzy subject))
3746          ;; Just remove the leading "Re:".
3747          (t
3748           (gnus-simplify-subject-re subject))))
3749
3750   (if (and gnus-summary-gather-exclude-subject
3751            (string-match gnus-summary-gather-exclude-subject subject))
3752       nil                         ; This article shouldn't be gathered
3753     subject))
3754
3755 (defun gnus-summary-simplify-subject-query ()
3756   "Query where the respool algorithm would put this article."
3757   (interactive)
3758   (gnus-summary-select-article)
3759   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3760
3761 (defun gnus-gather-threads-by-subject (threads)
3762   "Gather threads by looking at Subject headers."
3763   (if (not gnus-summary-make-false-root)
3764       threads
3765     (let ((hashtb (gnus-make-hashtable 1024))
3766           (prev threads)
3767           (result threads)
3768           subject hthread whole-subject)
3769       (while threads
3770         (setq subject (gnus-general-simplify-subject
3771                        (setq whole-subject (mail-header-subject
3772                                             (caar threads)))))
3773         (when subject
3774           (if (setq hthread (gnus-gethash subject hashtb))
3775               (progn
3776                 ;; We enter a dummy root into the thread, if we
3777                 ;; haven't done that already.
3778                 (unless (stringp (caar hthread))
3779                   (setcar hthread (list whole-subject (car hthread))))
3780                 ;; We add this new gathered thread to this gathered
3781                 ;; thread.
3782                 (setcdr (car hthread)
3783                         (nconc (cdar hthread) (list (car threads))))
3784                 ;; Remove it from the list of threads.
3785                 (setcdr prev (cdr threads))
3786                 (setq threads prev))
3787             ;; Enter this thread into the hash table.
3788             (gnus-sethash subject
3789                           (if gnus-summary-make-false-root-always
3790                               (progn
3791                                 ;; If you want a dummy root above all
3792                                 ;; threads...
3793                                 (setcar threads (list whole-subject
3794                                                       (car threads)))
3795                                 threads)
3796                             threads)
3797                           hashtb)))
3798         (setq prev threads)
3799         (setq threads (cdr threads)))
3800       result)))
3801
3802 (defun gnus-gather-threads-by-references (threads)
3803   "Gather threads by looking at References headers."
3804   (let ((idhashtb (gnus-make-hashtable 1024))
3805         (thhashtb (gnus-make-hashtable 1024))
3806         (prev threads)
3807         (result threads)
3808         ids references id gthread gid entered ref)
3809     (while threads
3810       (when (setq references (mail-header-references (caar threads)))
3811         (setq id (mail-header-id (caar threads))
3812               ids (inline (gnus-split-references references))
3813               entered nil)
3814         (while (setq ref (pop ids))
3815           (setq ids (delete ref ids))
3816           (if (not (setq gid (gnus-gethash ref idhashtb)))
3817               (progn
3818                 (gnus-sethash ref id idhashtb)
3819                 (gnus-sethash id threads thhashtb))
3820             (setq gthread (gnus-gethash gid thhashtb))
3821             (unless entered
3822               ;; We enter a dummy root into the thread, if we
3823               ;; haven't done that already.
3824               (unless (stringp (caar gthread))
3825                 (setcar gthread (list (mail-header-subject (caar gthread))
3826                                       (car gthread))))
3827               ;; We add this new gathered thread to this gathered
3828               ;; thread.
3829               (setcdr (car gthread)
3830                       (nconc (cdar gthread) (list (car threads)))))
3831             ;; Add it into the thread hash table.
3832             (gnus-sethash id gthread thhashtb)
3833             (setq entered t)
3834             ;; Remove it from the list of threads.
3835             (setcdr prev (cdr threads))
3836             (setq threads prev))))
3837       (setq prev threads)
3838       (setq threads (cdr threads)))
3839     result))
3840
3841 (defun gnus-sort-gathered-threads (threads)
3842   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3843   (let ((result threads))
3844     (while threads
3845       (when (stringp (caar threads))
3846         (setcdr (car threads)
3847                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3848       (setq threads (cdr threads)))
3849     result))
3850
3851 (defun gnus-thread-loop-p (root thread)
3852   "Say whether ROOT is in THREAD."
3853   (let ((stack (list thread))
3854         (infloop 0)
3855         th)
3856     (while (setq thread (pop stack))
3857       (setq th (cdr thread))
3858       (while (and th
3859                   (not (eq (caar th) root)))
3860         (pop th))
3861       (if th
3862           ;; We have found a loop.
3863           (let (ref-dep)
3864             (setcdr thread (delq (car th) (cdr thread)))
3865             (if (boundp (setq ref-dep (intern "none"
3866                                               gnus-newsgroup-dependencies)))
3867                 (setcdr (symbol-value ref-dep)
3868                         (nconc (cdr (symbol-value ref-dep))
3869                                (list (car th))))
3870               (set ref-dep (list nil (car th))))
3871             (setq infloop 1
3872                   stack nil))
3873         ;; Push all the subthreads onto the stack.
3874         (push (cdr thread) stack)))
3875     infloop))
3876
3877 (defun gnus-make-threads ()
3878   "Go through the dependency hashtb and find the roots.  Return all threads."
3879   (let (threads)
3880     (while (catch 'infloop
3881              (mapatoms
3882               (lambda (refs)
3883                 ;; Deal with self-referencing References loops.
3884                 (when (and (car (symbol-value refs))
3885                            (not (zerop
3886                                  (apply
3887                                   '+
3888                                   (mapcar
3889                                    (lambda (thread)
3890                                      (gnus-thread-loop-p
3891                                       (car (symbol-value refs)) thread))
3892                                    (cdr (symbol-value refs)))))))
3893                   (setq threads nil)
3894                   (throw 'infloop t))
3895                 (unless (car (symbol-value refs))
3896                   ;; These threads do not refer back to any other
3897                   ;; articles, so they're roots.
3898                   (setq threads (append (cdr (symbol-value refs)) threads))))
3899               gnus-newsgroup-dependencies)))
3900     threads))
3901
3902 ;; Build the thread tree.
3903 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3904   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3905
3906 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3907 if it was already present.
3908
3909 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3910 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3911 Message-IDs will be renamed to a unique Message-ID before being
3912 entered.
3913
3914 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3915   (let* ((id (mail-header-id header))
3916          (id-dep (and id (intern id dependencies)))
3917          parent-id ref ref-dep ref-header replaced)
3918     ;; Enter this `header' in the `dependencies' table.
3919     (cond
3920      ((not id-dep)
3921       (setq header nil))
3922      ;; The first two cases do the normal part: enter a new `header'
3923      ;; in the `dependencies' table.
3924      ((not (boundp id-dep))
3925       (set id-dep (list header)))
3926      ((null (car (symbol-value id-dep)))
3927       (setcar (symbol-value id-dep) header))
3928
3929      ;; From here the `header' was already present in the
3930      ;; `dependencies' table.
3931      (force-new
3932       ;; Overrides an existing entry;
3933       ;; just set the header part of the entry.
3934       (setcar (symbol-value id-dep) header)
3935       (setq replaced t))
3936
3937      ;; Renames the existing `header' to a unique Message-ID.
3938      ((not gnus-summary-ignore-duplicates)
3939       ;; An article with this Message-ID has already been seen.
3940       ;; We rename the Message-ID.
3941       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3942            (list header))
3943       (mail-header-set-id header id))
3944
3945      ;; The last case ignores an existing entry, except it adds any
3946      ;; additional Xrefs (in case the two articles came from different
3947      ;; servers.
3948      ;; Also sets `header' to `nil' meaning that the `dependencies'
3949      ;; table was *not* modified.
3950      (t
3951       (mail-header-set-xref
3952        (car (symbol-value id-dep))
3953        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3954                    "")
3955                (or (mail-header-xref header) "")))
3956       (setq header nil)))
3957
3958     (when (and header (not replaced))
3959       ;; First check that we are not creating a References loop.
3960       (setq parent-id (gnus-parent-id (mail-header-references header)))
3961       (setq ref parent-id)
3962       (while (and ref
3963                   (setq ref-dep (intern-soft ref dependencies))
3964                   (boundp ref-dep)
3965                   (setq ref-header (car (symbol-value ref-dep))))
3966         (if (string= id ref)
3967             ;; Yuk!  This is a reference loop.  Make the article be a
3968             ;; root article.
3969             (progn
3970               (mail-header-set-references (car (symbol-value id-dep)) "none")
3971               (setq ref nil)
3972               (setq parent-id nil))
3973           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3974       (setq ref-dep (intern (or parent-id "none") dependencies))
3975       (if (boundp ref-dep)
3976           (setcdr (symbol-value ref-dep)
3977                   (nconc (cdr (symbol-value ref-dep))
3978                          (list (symbol-value id-dep))))
3979         (set ref-dep (list nil (symbol-value id-dep)))))
3980     header))
3981
3982 (defun gnus-extract-message-id-from-in-reply-to (string)
3983   (if (string-match "<[^>]+>" string)
3984       (substring string (match-beginning 0) (match-end 0))
3985     nil))
3986
3987 (defun gnus-build-sparse-threads ()
3988   (let ((headers gnus-newsgroup-headers)
3989         (mail-parse-charset gnus-newsgroup-charset)
3990         (gnus-summary-ignore-duplicates t)
3991         header references generation relations
3992         subject child end new-child date)
3993     ;; First we create an alist of generations/relations, where
3994     ;; generations is how much we trust the relation, and the relation
3995     ;; is parent/child.
3996     (gnus-message 7 "Making sparse threads...")
3997     (save-excursion
3998       (nnheader-set-temp-buffer " *gnus sparse threads*")
3999       (while (setq header (pop headers))
4000         (when (and (setq references (mail-header-references header))
4001                    (not (string= references "")))
4002           (insert references)
4003           (setq child (mail-header-id header)
4004                 subject (mail-header-subject header)
4005                 date (mail-header-date header)
4006                 generation 0)
4007           (while (search-backward ">" nil t)
4008             (setq end (1+ (point)))
4009             (when (search-backward "<" nil t)
4010               (setq new-child (buffer-substring (point) end))
4011               (push (list (incf generation)
4012                           child (setq child new-child)
4013                           subject date)
4014                     relations)))
4015           (when child
4016             (push (list (1+ generation) child nil subject) relations))
4017           (erase-buffer)))
4018       (kill-buffer (current-buffer)))
4019     ;; Sort over trustworthiness.
4020     (mapcar
4021      (lambda (relation)
4022        (when (gnus-dependencies-add-header
4023               (make-full-mail-header
4024                gnus-reffed-article-number
4025                (nth 3 relation) "" (or (nth 4 relation) "")
4026                (nth 1 relation)
4027                (or (nth 2 relation) "") 0 0 "")
4028               gnus-newsgroup-dependencies nil)
4029          (push gnus-reffed-article-number gnus-newsgroup-limit)
4030          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4031          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4032                gnus-newsgroup-reads)
4033          (decf gnus-reffed-article-number)))
4034      (sort relations 'car-less-than-car))
4035     (gnus-message 7 "Making sparse threads...done")))
4036
4037 (defun gnus-build-old-threads ()
4038   ;; Look at all the articles that refer back to old articles, and
4039   ;; fetch the headers for the articles that aren't there.  This will
4040   ;; build complete threads - if the roots haven't been expired by the
4041   ;; server, that is.
4042   (let ((mail-parse-charset gnus-newsgroup-charset)
4043         id heads)
4044     (mapatoms
4045      (lambda (refs)
4046        (when (not (car (symbol-value refs)))
4047          (setq heads (cdr (symbol-value refs)))
4048          (while heads
4049            (if (memq (mail-header-number (caar heads))
4050                      gnus-newsgroup-dormant)
4051                (setq heads (cdr heads))
4052              (setq id (symbol-name refs))
4053              (while (and (setq id (gnus-build-get-header id))
4054                          (not (car (gnus-id-to-thread id)))))
4055              (setq heads nil)))))
4056      gnus-newsgroup-dependencies)))
4057
4058 (defsubst gnus-remove-odd-characters (string)
4059   "Translate STRING into something that doesn't contain weird characters."
4060   (mm-subst-char-in-string
4061    ?\r ?\-
4062    (mm-subst-char-in-string ?\n ?\- string t) t))
4063
4064 ;; This function has to be called with point after the article number
4065 ;; on the beginning of the line.
4066 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4067   (let ((eol (point-at-eol))
4068         (buffer (current-buffer))
4069         header references in-reply-to)
4070
4071     ;; overview: [num subject from date id refs chars lines misc]
4072     (unwind-protect
4073         (let (x)
4074           (narrow-to-region (point) eol)
4075           (unless (eobp)
4076             (forward-char))
4077
4078           (setq header
4079                 (make-full-mail-header
4080                  number                 ; number
4081                  (condition-case ()     ; subject
4082                      (gnus-remove-odd-characters
4083                       (funcall gnus-decode-encoded-word-function
4084                                (setq x (nnheader-nov-field))))
4085                    (error x))
4086                  (condition-case ()     ; from
4087                      (gnus-remove-odd-characters
4088                       (funcall gnus-decode-encoded-word-function
4089                                (setq x (nnheader-nov-field))))
4090                    (error x))
4091                  (nnheader-nov-field)   ; date
4092                  (nnheader-nov-read-message-id number)  ; id
4093                  (setq references (nnheader-nov-field)) ; refs
4094                  (nnheader-nov-read-integer) ; chars
4095                  (nnheader-nov-read-integer) ; lines
4096                  (unless (eobp)
4097                    (if (looking-at "Xref: ")
4098                        (goto-char (match-end 0)))
4099                    (nnheader-nov-field)) ; Xref
4100                  (nnheader-nov-parse-extra)))) ; extra
4101
4102       (widen))
4103
4104     (when (and (string= references "")
4105                (setq in-reply-to (mail-header-extra header))
4106                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4107       (mail-header-set-references
4108        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4109
4110     (when gnus-alter-header-function
4111       (funcall gnus-alter-header-function header))
4112     (gnus-dependencies-add-header header dependencies force-new)))
4113
4114 (defun gnus-build-get-header (id)
4115   "Look through the buffer of NOV lines and find the header to ID.
4116 Enter this line into the dependencies hash table, and return
4117 the id of the parent article (if any)."
4118   (let ((deps gnus-newsgroup-dependencies)
4119         found header)
4120     (prog1
4121         (save-excursion
4122           (set-buffer nntp-server-buffer)
4123           (let ((case-fold-search nil))
4124             (goto-char (point-min))
4125             (while (and (not found)
4126                         (search-forward id nil t))
4127               (beginning-of-line)
4128               (setq found (looking-at
4129                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4130                                    (regexp-quote id))))
4131               (or found (beginning-of-line 2)))
4132             (when found
4133               (beginning-of-line)
4134               (and
4135                (setq header (gnus-nov-parse-line
4136                              (read (current-buffer)) deps))
4137                (gnus-parent-id (mail-header-references header))))))
4138       (when header
4139         (let ((number (mail-header-number header)))
4140           (push number gnus-newsgroup-limit)
4141           (push header gnus-newsgroup-headers)
4142           (if (memq number gnus-newsgroup-unselected)
4143               (progn
4144                 (setq gnus-newsgroup-unreads
4145                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4146                                                number))
4147                 (setq gnus-newsgroup-unselected
4148                       (delq number gnus-newsgroup-unselected)))
4149             (push number gnus-newsgroup-ancient)))))))
4150
4151 (defun gnus-build-all-threads ()
4152   "Read all the headers."
4153   (let ((gnus-summary-ignore-duplicates t)
4154         (mail-parse-charset gnus-newsgroup-charset)
4155         (dependencies gnus-newsgroup-dependencies)
4156         header article)
4157     (save-excursion
4158       (set-buffer nntp-server-buffer)
4159       (let ((case-fold-search nil))
4160         (goto-char (point-min))
4161         (while (not (eobp))
4162           (ignore-errors
4163             (setq article (read (current-buffer))
4164                   header (gnus-nov-parse-line article dependencies)))
4165           (when header
4166             (with-current-buffer gnus-summary-buffer
4167               (push header gnus-newsgroup-headers)
4168               (if (memq (setq article (mail-header-number header))
4169                         gnus-newsgroup-unselected)
4170                   (progn
4171                     (setq gnus-newsgroup-unreads
4172                           (gnus-add-to-sorted-list
4173                            gnus-newsgroup-unreads article))
4174                     (setq gnus-newsgroup-unselected
4175                           (delq article gnus-newsgroup-unselected)))
4176                 (push article gnus-newsgroup-ancient)))
4177             (forward-line 1)))))))
4178
4179 (defun gnus-summary-update-article-line (article header)
4180   "Update the line for ARTICLE using HEADER."
4181   (let* ((id (mail-header-id header))
4182          (thread (gnus-id-to-thread id)))
4183     (unless thread
4184       (error "Article in no thread"))
4185     ;; Update the thread.
4186     (setcar thread header)
4187     (gnus-summary-goto-subject article)
4188     (let* ((datal (gnus-data-find-list article))
4189            (data (car datal))
4190            (buffer-read-only nil)
4191            (level (gnus-summary-thread-level)))
4192       (gnus-delete-line)
4193       (let ((inserted (- (point)
4194                          (progn
4195                            (gnus-summary-insert-line
4196                             header level nil
4197                             (memq article gnus-newsgroup-undownloaded)
4198                             (gnus-article-mark article)
4199                             (memq article gnus-newsgroup-replied)
4200                             (memq article gnus-newsgroup-expirable)
4201                             ;; Only insert the Subject string when it's different
4202                             ;; from the previous Subject string.
4203                             (if (and
4204                                  gnus-show-threads
4205                                  (gnus-subject-equal
4206                                   (condition-case ()
4207                                       (mail-header-subject
4208                                        (gnus-data-header
4209                                         (cadr
4210                                          (gnus-data-find-list
4211                                           article
4212                                           (gnus-data-list t)))))
4213                                     ;; Error on the side of excessive subjects.
4214                                     (error ""))
4215                                   (mail-header-subject header)))
4216                                 ""
4217                               (mail-header-subject header))
4218                             nil (cdr (assq article gnus-newsgroup-scored))
4219                             (memq article gnus-newsgroup-processable))
4220                            (point)))))
4221         (when (cdr datal)
4222           (gnus-data-update-list
4223            (cdr datal)
4224            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4225
4226 (defun gnus-summary-update-article (article &optional iheader)
4227   "Update ARTICLE in the summary buffer."
4228   (set-buffer gnus-summary-buffer)
4229   (let* ((header (gnus-summary-article-header article))
4230          (id (mail-header-id header))
4231          (data (gnus-data-find article))
4232          (thread (gnus-id-to-thread id))
4233          (references (mail-header-references header))
4234          (parent
4235           (gnus-id-to-thread
4236            (or (gnus-parent-id
4237                 (when (and references
4238                            (not (equal "" references)))
4239                   references))
4240                "none")))
4241          (buffer-read-only nil)
4242          (old (car thread)))
4243     (when thread
4244       (unless iheader
4245         (setcar thread nil)
4246         (when parent
4247           (delq thread parent)))
4248       (if (gnus-summary-insert-subject id header)
4249           ;; Set the (possibly) new article number in the data structure.
4250           (gnus-data-set-number data (gnus-id-to-article id))
4251         (setcar thread old)
4252         nil))))
4253
4254 (defun gnus-rebuild-thread (id &optional line)
4255   "Rebuild the thread containing ID.
4256 If LINE, insert the rebuilt thread starting on line LINE."
4257   (let ((buffer-read-only nil)
4258         old-pos current thread data)
4259     (if (not gnus-show-threads)
4260         (setq thread (list (car (gnus-id-to-thread id))))
4261       ;; Get the thread this article is part of.
4262       (setq thread (gnus-remove-thread id)))
4263     (setq old-pos (point-at-bol))
4264     (setq current (save-excursion
4265                     (and (re-search-backward "[\r\n]" nil t)
4266                          (gnus-summary-article-number))))
4267     ;; If this is a gathered thread, we have to go some re-gathering.
4268     (when (stringp (car thread))
4269       (let ((subject (car thread))
4270             roots thr)
4271         (setq thread (cdr thread))
4272         (while thread
4273           (unless (memq (setq thr (gnus-id-to-thread
4274                                    (gnus-root-id
4275                                     (mail-header-id (caar thread)))))
4276                         roots)
4277             (push thr roots))
4278           (setq thread (cdr thread)))
4279         ;; We now have all (unique) roots.
4280         (if (= (length roots) 1)
4281             ;; All the loose roots are now one solid root.
4282             (setq thread (car roots))
4283           (setq thread (cons subject (gnus-sort-threads roots))))))
4284     (let (threads)
4285       ;; We then insert this thread into the summary buffer.
4286       (when line
4287         (goto-char (point-min))
4288         (forward-line (1- line)))
4289       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4290         (if gnus-show-threads
4291             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4292           (gnus-summary-prepare-unthreaded thread))
4293         (setq data (nreverse gnus-newsgroup-data))
4294         (setq threads gnus-newsgroup-threads))
4295       ;; We splice the new data into the data structure.
4296       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4297       ;;!!! then we want to insert at the beginning of the buffer.
4298       ;;!!! That happens to be true with Gnus now, but that may
4299       ;;!!! change in the future.  Perhaps.
4300       (gnus-data-enter-list
4301        (if line nil current) data (- (point) old-pos))
4302       (setq gnus-newsgroup-threads
4303             (nconc threads gnus-newsgroup-threads))
4304       (gnus-data-compute-positions))))
4305
4306 (defun gnus-number-to-header (number)
4307   "Return the header for article NUMBER."
4308   (let ((headers gnus-newsgroup-headers))
4309     (while (and headers
4310                 (not (= number (mail-header-number (car headers)))))
4311       (pop headers))
4312     (when headers
4313       (car headers))))
4314
4315 (defun gnus-parent-headers (in-headers &optional generation)
4316   "Return the headers of the GENERATIONeth parent of HEADERS."
4317   (unless generation
4318     (setq generation 1))
4319   (let ((parent t)
4320         (headers in-headers)
4321         references)
4322     (while (and parent
4323                 (not (zerop generation))
4324                 (setq references (mail-header-references headers)))
4325       (setq headers (if (and references
4326                              (setq parent (gnus-parent-id references)))
4327                         (car (gnus-id-to-thread parent))
4328                       nil))
4329       (decf generation))
4330     (and (not (eq headers in-headers))
4331          headers)))
4332
4333 (defun gnus-id-to-thread (id)
4334   "Return the (sub-)thread where ID appears."
4335   (gnus-gethash id gnus-newsgroup-dependencies))
4336
4337 (defun gnus-id-to-article (id)
4338   "Return the article number of ID."
4339   (let ((thread (gnus-id-to-thread id)))
4340     (when (and thread
4341                (car thread))
4342       (mail-header-number (car thread)))))
4343
4344 (defun gnus-id-to-header (id)
4345   "Return the article headers of ID."
4346   (car (gnus-id-to-thread id)))
4347
4348 (defun gnus-article-displayed-root-p (article)
4349   "Say whether ARTICLE is a root(ish) article."
4350   (let ((level (gnus-summary-thread-level article))
4351         (refs (mail-header-references  (gnus-summary-article-header article)))
4352         particle)
4353     (cond
4354      ((null level) nil)
4355      ((zerop level) t)
4356      ((null refs) t)
4357      ((null (gnus-parent-id refs)) t)
4358      ((and (= 1 level)
4359            (null (setq particle (gnus-id-to-article
4360                                  (gnus-parent-id refs))))
4361            (null (gnus-summary-thread-level particle)))))))
4362
4363 (defun gnus-root-id (id)
4364   "Return the id of the root of the thread where ID appears."
4365   (let (last-id prev)
4366     (while (and id (setq prev (car (gnus-id-to-thread id))))
4367       (setq last-id id
4368             id (gnus-parent-id (mail-header-references prev))))
4369     last-id))
4370
4371 (defun gnus-articles-in-thread (thread)
4372   "Return the list of articles in THREAD."
4373   (cons (mail-header-number (car thread))
4374         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4375
4376 (defun gnus-remove-thread (id &optional dont-remove)
4377   "Remove the thread that has ID in it."
4378   (let (headers thread last-id)
4379     ;; First go up in this thread until we find the root.
4380     (setq last-id (gnus-root-id id)
4381           headers (message-flatten-list (gnus-id-to-thread last-id)))
4382     ;; We have now found the real root of this thread.  It might have
4383     ;; been gathered into some loose thread, so we have to search
4384     ;; through the threads to find the thread we wanted.
4385     (let ((threads gnus-newsgroup-threads)
4386           sub)
4387       (while threads
4388         (setq sub (car threads))
4389         (if (stringp (car sub))
4390             ;; This is a gathered thread, so we look at the roots
4391             ;; below it to find whether this article is in this
4392             ;; gathered root.
4393             (progn
4394               (setq sub (cdr sub))
4395               (while sub
4396                 (when (member (caar sub) headers)
4397                   (setq thread (car threads)
4398                         threads nil
4399                         sub nil))
4400                 (setq sub (cdr sub))))
4401           ;; It's an ordinary thread, so we check it.
4402           (when (eq (car sub) (car headers))
4403             (setq thread sub
4404                   threads nil)))
4405         (setq threads (cdr threads)))
4406       ;; If this article is in no thread, then it's a root.
4407       (if thread
4408           (unless dont-remove
4409             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4410         (setq thread (gnus-id-to-thread last-id)))
4411       (when thread
4412         (prog1
4413             thread                      ; We return this thread.
4414           (unless dont-remove
4415             (if (stringp (car thread))
4416                 (progn
4417                   ;; If we use dummy roots, then we have to remove the
4418                   ;; dummy root as well.
4419                   (when (eq gnus-summary-make-false-root 'dummy)
4420                     ;; We go to the dummy root by going to
4421                     ;; the first sub-"thread", and then one line up.
4422                     (gnus-summary-goto-article
4423                      (mail-header-number (caadr thread)))
4424                     (forward-line -1)
4425                     (gnus-delete-line)
4426                     (gnus-data-compute-positions))
4427                   (setq thread (cdr thread))
4428                   (while thread
4429                     (gnus-remove-thread-1 (car thread))
4430                     (setq thread (cdr thread))))
4431               (gnus-remove-thread-1 thread))))))))
4432
4433 (defun gnus-remove-thread-1 (thread)
4434   "Remove the thread THREAD recursively."
4435   (let ((number (mail-header-number (pop thread)))
4436         d)
4437     (setq thread (reverse thread))
4438     (while thread
4439       (gnus-remove-thread-1 (pop thread)))
4440     (when (setq d (gnus-data-find number))
4441       (goto-char (gnus-data-pos d))
4442       (gnus-summary-show-thread)
4443       (gnus-data-remove
4444        number
4445        (- (point-at-bol)
4446           (prog1
4447               (1+ (point-at-eol))
4448             (gnus-delete-line)))))))
4449
4450 (defun gnus-sort-threads-1 (threads func)
4451   (sort (mapcar (lambda (thread)
4452                   (cons (car thread)
4453                         (and (cdr thread)
4454                              (gnus-sort-threads-1 (cdr thread) func))))
4455                 threads) func))
4456
4457 (defun gnus-sort-threads (threads)
4458   "Sort THREADS."
4459   (if (not gnus-thread-sort-functions)
4460       threads
4461     (gnus-message 8 "Sorting threads...")
4462     (let ((max-lisp-eval-depth 5000))
4463       (prog1 (gnus-sort-threads-1
4464          threads
4465          (gnus-make-sort-function gnus-thread-sort-functions))
4466         (gnus-message 8 "Sorting threads...done")))))
4467
4468 (defun gnus-sort-articles (articles)
4469   "Sort ARTICLES."
4470   (when gnus-article-sort-functions
4471     (gnus-message 7 "Sorting articles...")
4472     (prog1
4473         (setq gnus-newsgroup-headers
4474               (sort articles (gnus-make-sort-function
4475                               gnus-article-sort-functions)))
4476       (gnus-message 7 "Sorting articles...done"))))
4477
4478 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4479 (defmacro gnus-thread-header (thread)
4480   "Return header of first article in THREAD.
4481 Note that THREAD must never, ever be anything else than a variable -
4482 using some other form will lead to serious barfage."
4483   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4484   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4485   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4486         (vector thread) 2))
4487
4488 (defsubst gnus-article-sort-by-number (h1 h2)
4489   "Sort articles by article number."
4490   (< (mail-header-number h1)
4491      (mail-header-number h2)))
4492
4493 (defun gnus-thread-sort-by-number (h1 h2)
4494   "Sort threads by root article number."
4495   (gnus-article-sort-by-number
4496    (gnus-thread-header h1) (gnus-thread-header h2)))
4497
4498 (defsubst gnus-article-sort-by-random (h1 h2)
4499   "Sort articles by article number."
4500   (zerop (random 2)))
4501
4502 (defun gnus-thread-sort-by-random (h1 h2)
4503   "Sort threads by root article number."
4504   (gnus-article-sort-by-random
4505    (gnus-thread-header h1) (gnus-thread-header h2)))
4506
4507 (defsubst gnus-article-sort-by-lines (h1 h2)
4508   "Sort articles by article Lines header."
4509   (< (mail-header-lines h1)
4510      (mail-header-lines h2)))
4511
4512 (defun gnus-thread-sort-by-lines (h1 h2)
4513   "Sort threads by root article Lines header."
4514   (gnus-article-sort-by-lines
4515    (gnus-thread-header h1) (gnus-thread-header h2)))
4516
4517 (defsubst gnus-article-sort-by-chars (h1 h2)
4518   "Sort articles by octet length."
4519   (< (mail-header-chars h1)
4520      (mail-header-chars h2)))
4521
4522 (defun gnus-thread-sort-by-chars (h1 h2)
4523   "Sort threads by root article octet length."
4524   (gnus-article-sort-by-chars
4525    (gnus-thread-header h1) (gnus-thread-header h2)))
4526
4527 (defsubst gnus-article-sort-by-author (h1 h2)
4528   "Sort articles by root author."
4529   (string-lessp
4530    (let ((extract (funcall
4531                    gnus-extract-address-components
4532                    (mail-header-from h1))))
4533      (or (car extract) (cadr extract) ""))
4534    (let ((extract (funcall
4535                    gnus-extract-address-components
4536                    (mail-header-from h2))))
4537      (or (car extract) (cadr extract) ""))))
4538
4539 (defun gnus-thread-sort-by-author (h1 h2)
4540   "Sort threads by root author."
4541   (gnus-article-sort-by-author
4542    (gnus-thread-header h1)  (gnus-thread-header h2)))
4543
4544 (defsubst gnus-article-sort-by-subject (h1 h2)
4545   "Sort articles by root subject."
4546   (string-lessp
4547    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4548    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4549
4550 (defun gnus-thread-sort-by-subject (h1 h2)
4551   "Sort threads by root subject."
4552   (gnus-article-sort-by-subject
4553    (gnus-thread-header h1) (gnus-thread-header h2)))
4554
4555 (defsubst gnus-article-sort-by-date (h1 h2)
4556   "Sort articles by root article date."
4557   (time-less-p
4558    (gnus-date-get-time (mail-header-date h1))
4559    (gnus-date-get-time (mail-header-date h2))))
4560
4561 (defun gnus-thread-sort-by-date (h1 h2)
4562   "Sort threads by root article date."
4563   (gnus-article-sort-by-date
4564    (gnus-thread-header h1) (gnus-thread-header h2)))
4565
4566 (defsubst gnus-article-sort-by-score (h1 h2)
4567   "Sort articles by root article score.
4568 Unscored articles will be counted as having a score of zero."
4569   (> (or (cdr (assq (mail-header-number h1)
4570                     gnus-newsgroup-scored))
4571          gnus-summary-default-score 0)
4572      (or (cdr (assq (mail-header-number h2)
4573                     gnus-newsgroup-scored))
4574          gnus-summary-default-score 0)))
4575
4576 (defun gnus-thread-sort-by-score (h1 h2)
4577   "Sort threads by root article score."
4578   (gnus-article-sort-by-score
4579    (gnus-thread-header h1) (gnus-thread-header h2)))
4580
4581 (defun gnus-thread-sort-by-total-score (h1 h2)
4582   "Sort threads by the sum of all scores in the thread.
4583 Unscored articles will be counted as having a score of zero."
4584   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4585
4586 (defun gnus-thread-total-score (thread)
4587   ;; This function find the total score of THREAD.
4588   (cond
4589    ((null thread)
4590     0)
4591    ((consp thread)
4592     (if (stringp (car thread))
4593         (apply gnus-thread-score-function 0
4594                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4595       (gnus-thread-total-score-1 thread)))
4596    (t
4597     (gnus-thread-total-score-1 (list thread)))))
4598
4599 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4600   "Sort threads such that the thread with the most recently arrived article comes first."
4601   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4602
4603 (defun gnus-thread-highest-number (thread)
4604   "Return the highest article number in THREAD."
4605   (apply 'max (mapcar (lambda (header)
4606                         (mail-header-number header))
4607                       (message-flatten-list thread))))
4608
4609 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4610   "Sort threads such that the thread with the most recently dated article comes first."
4611   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4612
4613 (defun gnus-thread-latest-date (thread)
4614   "Return the highest article date in THREAD."
4615   (let ((previous-time 0))
4616     (apply 'max
4617            (mapcar
4618             (lambda (header)
4619               (setq previous-time
4620                     (condition-case ()
4621                         (time-to-seconds (mail-header-parse-date
4622                                           (mail-header-date header)))
4623                       (error previous-time))))
4624             (sort
4625              (message-flatten-list thread)
4626              (lambda (h1 h2)
4627                (< (mail-header-number h1)
4628                   (mail-header-number h2))))))))
4629
4630 (defun gnus-thread-total-score-1 (root)
4631   ;; This function find the total score of the thread below ROOT.
4632   (setq root (car root))
4633   (apply gnus-thread-score-function
4634          (or (append
4635               (mapcar 'gnus-thread-total-score
4636                       (cdr (gnus-id-to-thread (mail-header-id root))))
4637               (when (> (mail-header-number root) 0)
4638                 (list (or (cdr (assq (mail-header-number root)
4639                                      gnus-newsgroup-scored))
4640                           gnus-summary-default-score 0))))
4641              (list gnus-summary-default-score)
4642              '(0))))
4643
4644 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4645 (defvar gnus-tmp-prev-subject nil)
4646 (defvar gnus-tmp-false-parent nil)
4647 (defvar gnus-tmp-root-expunged nil)
4648 (defvar gnus-tmp-dummy-line nil)
4649
4650 (eval-when-compile (defvar gnus-tmp-header))
4651 (defun gnus-extra-header (type &optional header)
4652   "Return the extra header of TYPE."
4653   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4654       ""))
4655
4656 (defvar gnus-tmp-thread-tree-header-string "")
4657
4658 (defcustom gnus-sum-thread-tree-root "> "
4659   "With %B spec, used for the root of a thread.
4660 If nil, use subject instead."
4661   :version "21.4"
4662   :type '(radio (const :format "%v  " nil) (string :size 0))
4663   :group 'gnus-thread)
4664 (defcustom gnus-sum-thread-tree-false-root "> "
4665   "With %B spec, used for a false root of a thread.
4666 If nil, use subject instead."
4667   :version "21.4"
4668   :type '(radio (const :format "%v  " nil) (string :size 0))
4669   :group 'gnus-thread)
4670 (defcustom gnus-sum-thread-tree-single-indent ""
4671   "With %B spec, used for a thread with just one message.
4672 If nil, use subject instead."
4673   :version "21.4"
4674   :type '(radio (const :format "%v  " nil) (string :size 0))
4675   :group 'gnus-thread)
4676 (defcustom gnus-sum-thread-tree-vertical "| "
4677   "With %B spec, used for drawing a vertical line."
4678   :version "21.4"
4679   :type 'string
4680   :group 'gnus-thread)
4681 (defcustom gnus-sum-thread-tree-indent "  "
4682   "With %B spec, used for indenting."
4683   :version "21.4"
4684   :type 'string
4685   :group 'gnus-thread)
4686 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4687   "With %B spec, used for a leaf with brothers."
4688   :version "21.4"
4689   :type 'string
4690   :group 'gnus-thread)
4691 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4692   "With %B spec, used for a leaf without brothers."
4693   :version "21.4"
4694   :type 'string
4695   :group 'gnus-thread)
4696
4697 (defun gnus-summary-prepare-threads (threads)
4698   "Prepare summary buffer from THREADS and indentation LEVEL.
4699 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4700 or a straight list of headers."
4701   (gnus-message 7 "Generating summary...")
4702
4703   (setq gnus-newsgroup-threads threads)
4704   (beginning-of-line)
4705
4706   (let ((gnus-tmp-level 0)
4707         (default-score (or gnus-summary-default-score 0))
4708         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4709         (building-line-count gnus-summary-display-while-building)
4710         (building-count (integerp gnus-summary-display-while-building))
4711         thread number subject stack state gnus-tmp-gathered beg-match
4712         new-roots gnus-tmp-new-adopts thread-end simp-subject
4713         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4714         gnus-tmp-replied gnus-tmp-subject-or-nil
4715         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4716         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4717         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4718         tree-stack)
4719
4720     (setq gnus-tmp-prev-subject nil
4721           gnus-tmp-thread-tree-header-string "")
4722
4723     (if (vectorp (car threads))
4724         ;; If this is a straight (sic) list of headers, then a
4725         ;; threaded summary display isn't required, so we just create
4726         ;; an unthreaded one.
4727         (gnus-summary-prepare-unthreaded threads)
4728
4729       ;; Do the threaded display.
4730
4731       (if gnus-summary-display-while-building
4732           (switch-to-buffer (buffer-name)))
4733       (while (or threads stack gnus-tmp-new-adopts new-roots)
4734
4735         (if (and (= gnus-tmp-level 0)
4736                  (or (not stack)
4737                      (= (caar stack) 0))
4738                  (not gnus-tmp-false-parent)
4739                  (or gnus-tmp-new-adopts new-roots))
4740             (if gnus-tmp-new-adopts
4741                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4742                       thread (list (car gnus-tmp-new-adopts))
4743                       gnus-tmp-header (caar thread)
4744                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4745               (when new-roots
4746                 (setq thread (list (car new-roots))
4747                       gnus-tmp-header (caar thread)
4748                       new-roots (cdr new-roots))))
4749
4750           (if threads
4751               ;; If there are some threads, we do them before the
4752               ;; threads on the stack.
4753               (setq thread threads
4754                     gnus-tmp-header (caar thread))
4755             ;; There were no current threads, so we pop something off
4756             ;; the stack.
4757             (setq state (car stack)
4758                   gnus-tmp-level (car state)
4759                   tree-stack (cadr state)
4760                   thread (caddr state)
4761                   stack (cdr stack)
4762                   gnus-tmp-header (caar thread))))
4763
4764         (setq gnus-tmp-false-parent nil)
4765         (setq gnus-tmp-root-expunged nil)
4766         (setq thread-end nil)
4767
4768         (if (stringp gnus-tmp-header)
4769             ;; The header is a dummy root.
4770             (cond
4771              ((eq gnus-summary-make-false-root 'adopt)
4772               ;; We let the first article adopt the rest.
4773               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4774                                                (cddar thread)))
4775               (setq gnus-tmp-gathered
4776                     (nconc (mapcar
4777                             (lambda (h) (mail-header-number (car h)))
4778                             (cddar thread))
4779                            gnus-tmp-gathered))
4780               (setq thread (cons (list (caar thread)
4781                                        (cadar thread))
4782                                  (cdr thread)))
4783               (setq gnus-tmp-level -1
4784                     gnus-tmp-false-parent t))
4785              ((eq gnus-summary-make-false-root 'empty)
4786               ;; We print adopted articles with empty subject fields.
4787               (setq gnus-tmp-gathered
4788                     (nconc (mapcar
4789                             (lambda (h) (mail-header-number (car h)))
4790                             (cddar thread))
4791                            gnus-tmp-gathered))
4792               (setq gnus-tmp-level -1))
4793              ((eq gnus-summary-make-false-root 'dummy)
4794               ;; We remember that we probably want to output a dummy
4795               ;; root.
4796               (setq gnus-tmp-dummy-line gnus-tmp-header)
4797               (setq gnus-tmp-prev-subject gnus-tmp-header))
4798              (t
4799               ;; We do not make a root for the gathered
4800               ;; sub-threads at all.
4801               (setq gnus-tmp-level -1)))
4802
4803           (setq number (mail-header-number gnus-tmp-header)
4804                 subject (mail-header-subject gnus-tmp-header)
4805                 simp-subject (gnus-simplify-subject-fully subject))
4806
4807           (cond
4808            ;; If the thread has changed subject, we might want to make
4809            ;; this subthread into a root.
4810            ((and (null gnus-thread-ignore-subject)
4811                  (not (zerop gnus-tmp-level))
4812                  gnus-tmp-prev-subject
4813                  (not (string= gnus-tmp-prev-subject simp-subject)))
4814             (setq new-roots (nconc new-roots (list (car thread)))
4815                   thread-end t
4816                   gnus-tmp-header nil))
4817            ;; If the article lies outside the current limit,
4818            ;; then we do not display it.
4819            ((not (memq number gnus-newsgroup-limit))
4820             (setq gnus-tmp-gathered
4821                   (nconc (mapcar
4822                           (lambda (h) (mail-header-number (car h)))
4823                           (cdar thread))
4824                          gnus-tmp-gathered))
4825             (setq gnus-tmp-new-adopts (if (cdar thread)
4826                                           (append gnus-tmp-new-adopts
4827                                                   (cdar thread))
4828                                         gnus-tmp-new-adopts)
4829                   thread-end t
4830                   gnus-tmp-header nil)
4831             (when (zerop gnus-tmp-level)
4832               (setq gnus-tmp-root-expunged t)))
4833            ;; Perhaps this article is to be marked as read?
4834            ((and gnus-summary-mark-below
4835                  (< (or (cdr (assq number gnus-newsgroup-scored))
4836                         default-score)
4837                     gnus-summary-mark-below)
4838                  ;; Don't touch sparse articles.
4839                  (not (gnus-summary-article-sparse-p number))
4840                  (not (gnus-summary-article-ancient-p number)))
4841             (setq gnus-newsgroup-unreads
4842                   (delq number gnus-newsgroup-unreads))
4843             (if gnus-newsgroup-auto-expire
4844                 (setq gnus-newsgroup-expirable
4845                       (gnus-add-to-sorted-list
4846                        gnus-newsgroup-expirable number))
4847               (push (cons number gnus-low-score-mark)
4848                     gnus-newsgroup-reads))))
4849
4850           (when gnus-tmp-header
4851             ;; We may have an old dummy line to output before this
4852             ;; article.
4853             (when (and gnus-tmp-dummy-line
4854                        (gnus-subject-equal
4855                         gnus-tmp-dummy-line
4856                         (mail-header-subject gnus-tmp-header)))
4857               (gnus-summary-insert-dummy-line
4858                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4859               (setq gnus-tmp-dummy-line nil))
4860
4861             ;; Compute the mark.
4862             (setq gnus-tmp-unread (gnus-article-mark number))
4863
4864             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4865                                   gnus-tmp-header gnus-tmp-level)
4866                   gnus-newsgroup-data)
4867
4868             ;; Actually insert the line.
4869             (setq
4870              gnus-tmp-subject-or-nil
4871              (cond
4872               ((and gnus-thread-ignore-subject
4873                     gnus-tmp-prev-subject
4874                     (not (string= gnus-tmp-prev-subject simp-subject)))
4875                subject)
4876               ((zerop gnus-tmp-level)
4877                (if (and (eq gnus-summary-make-false-root 'empty)
4878                         (memq number gnus-tmp-gathered)
4879                         gnus-tmp-prev-subject
4880                         (string= gnus-tmp-prev-subject simp-subject))
4881                    gnus-summary-same-subject
4882                  subject))
4883               (t gnus-summary-same-subject)))
4884             (if (and (eq gnus-summary-make-false-root 'adopt)
4885                      (= gnus-tmp-level 1)
4886                      (memq number gnus-tmp-gathered))
4887                 (setq gnus-tmp-opening-bracket ?\<
4888                       gnus-tmp-closing-bracket ?\>)
4889               (setq gnus-tmp-opening-bracket ?\[
4890                     gnus-tmp-closing-bracket ?\]))
4891             (setq
4892              gnus-tmp-indentation
4893              (aref gnus-thread-indent-array gnus-tmp-level)
4894              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4895              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4896                                 gnus-summary-default-score 0)
4897              gnus-tmp-score-char
4898              (if (or (null gnus-summary-default-score)
4899                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4900                          gnus-summary-zcore-fuzz))
4901                  ?                      ;Whitespace
4902                (if (< gnus-tmp-score gnus-summary-default-score)
4903                    gnus-score-below-mark gnus-score-over-mark))
4904              gnus-tmp-replied
4905              (cond ((memq number gnus-newsgroup-processable)
4906                     gnus-process-mark)
4907                    ((memq number gnus-newsgroup-cached)
4908                     gnus-cached-mark)
4909                    ((memq number gnus-newsgroup-replied)
4910                     gnus-replied-mark)
4911                    ((memq number gnus-newsgroup-forwarded)
4912                     gnus-forwarded-mark)
4913                    ((memq number gnus-newsgroup-saved)
4914                     gnus-saved-mark)
4915                    ((memq number gnus-newsgroup-recent)
4916                     gnus-recent-mark)
4917                    ((memq number gnus-newsgroup-unseen)
4918                     gnus-unseen-mark)
4919                    (t gnus-no-mark))
4920              gnus-tmp-downloaded
4921              (cond ((memq number gnus-newsgroup-undownloaded)
4922                     gnus-undownloaded-mark)
4923                    (gnus-newsgroup-agentized
4924                     gnus-downloaded-mark)
4925                    (t
4926                     gnus-no-mark))
4927              gnus-tmp-from (mail-header-from gnus-tmp-header)
4928              gnus-tmp-name
4929              (cond
4930               ((string-match "<[^>]+> *$" gnus-tmp-from)
4931                (setq beg-match (match-beginning 0))
4932                (or (and (string-match "^\".+\"" gnus-tmp-from)
4933                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4934                    (substring gnus-tmp-from 0 beg-match)))
4935               ((string-match "(.+)" gnus-tmp-from)
4936                (substring gnus-tmp-from
4937                           (1+ (match-beginning 0)) (1- (match-end 0))))
4938               (t gnus-tmp-from))
4939
4940              ;; Do the %B string
4941              gnus-tmp-thread-tree-header-string
4942              (cond
4943               ((not gnus-show-threads) "")
4944               ((zerop gnus-tmp-level)
4945                (cond ((cdar thread)
4946                       (or gnus-sum-thread-tree-root subject))
4947                      (gnus-tmp-new-adopts
4948                       (or gnus-sum-thread-tree-false-root subject))
4949                      (t
4950                       (or gnus-sum-thread-tree-single-indent subject))))
4951               (t
4952                (concat (apply 'concat
4953                               (mapcar (lambda (item)
4954                                         (if (= item 1)
4955                                             gnus-sum-thread-tree-vertical
4956                                           gnus-sum-thread-tree-indent))
4957                                       (cdr (reverse tree-stack))))
4958                        (if (nth 1 thread)
4959                            gnus-sum-thread-tree-leaf-with-other
4960                          gnus-sum-thread-tree-single-leaf)))))
4961             (when (string= gnus-tmp-name "")
4962               (setq gnus-tmp-name gnus-tmp-from))
4963             (unless (numberp gnus-tmp-lines)
4964               (setq gnus-tmp-lines -1))
4965             (if (= gnus-tmp-lines -1)
4966                 (setq gnus-tmp-lines "?")
4967               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4968               (gnus-put-text-property
4969              (point)
4970              (progn (eval gnus-summary-line-format-spec) (point))
4971                'gnus-number number)
4972             (when gnus-visual-p
4973               (forward-line -1)
4974               (gnus-run-hooks 'gnus-summary-update-hook)
4975               (forward-line 1))
4976
4977             (setq gnus-tmp-prev-subject simp-subject)))
4978
4979         (when (nth 1 thread)
4980           (push (list (max 0 gnus-tmp-level)
4981                       (copy-sequence tree-stack)
4982                       (nthcdr 1 thread))
4983                 stack))
4984         (push (if (nth 1 thread) 1 0) tree-stack)
4985         (incf gnus-tmp-level)
4986         (setq threads (if thread-end nil (cdar thread)))
4987         (if gnus-summary-display-while-building
4988             (if building-count
4989                 (progn
4990                   ;; use a set frequency
4991                   (setq building-line-count (1- building-line-count))
4992                   (when (= building-line-count 0)
4993                     (sit-for 0)
4994                     (setq building-line-count
4995                           gnus-summary-display-while-building)))
4996               ;; always
4997               (sit-for 0)))
4998         (unless threads
4999           (setq gnus-tmp-level 0)))))
5000   (gnus-message 7 "Generating summary...done"))
5001
5002 (defun gnus-summary-prepare-unthreaded (headers)
5003   "Generate an unthreaded summary buffer based on HEADERS."
5004   (let (header number mark)
5005
5006     (beginning-of-line)
5007
5008     (while headers
5009       ;; We may have to root out some bad articles...
5010       (when (memq (setq number (mail-header-number
5011                                 (setq header (pop headers))))
5012                   gnus-newsgroup-limit)
5013         ;; Mark article as read when it has a low score.
5014         (when (and gnus-summary-mark-below
5015                    (< (or (cdr (assq number gnus-newsgroup-scored))
5016                           gnus-summary-default-score 0)
5017                       gnus-summary-mark-below)
5018                    (not (gnus-summary-article-ancient-p number)))
5019           (setq gnus-newsgroup-unreads
5020                 (delq number gnus-newsgroup-unreads))
5021           (if gnus-newsgroup-auto-expire
5022               (push number gnus-newsgroup-expirable)
5023             (push (cons number gnus-low-score-mark)
5024                   gnus-newsgroup-reads)))
5025
5026         (setq mark (gnus-article-mark number))
5027         (push (gnus-data-make number mark (1+ (point)) header 0)
5028               gnus-newsgroup-data)
5029         (gnus-summary-insert-line
5030          header 0 number
5031          (memq number gnus-newsgroup-undownloaded)
5032          mark (memq number gnus-newsgroup-replied)
5033          (memq number gnus-newsgroup-expirable)
5034          (mail-header-subject header) nil
5035          (cdr (assq number gnus-newsgroup-scored))
5036          (memq number gnus-newsgroup-processable))))))
5037
5038 (defun gnus-summary-remove-list-identifiers ()
5039   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5040   (let ((regexp (if (consp gnus-list-identifiers)
5041                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5042                   gnus-list-identifiers))
5043         changed subject)
5044     (when regexp
5045       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5046       (dolist (header gnus-newsgroup-headers)
5047         (setq subject (mail-header-subject header)
5048               changed nil)
5049         (while (string-match regexp subject)
5050           (setq subject
5051                 (concat (substring subject 0 (match-beginning 1))
5052                         (substring subject (match-end 0)))
5053                 changed t))
5054         (when changed
5055           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5056             (setq subject
5057                   (concat (substring subject 0 (match-beginning 1))
5058                           (substring subject (match-end 1)))))
5059           (mail-header-set-subject header subject))))))
5060
5061 (defun gnus-fetch-headers (articles)
5062   "Fetch headers of ARTICLES."
5063   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5064     (gnus-message 5 "Fetching headers for %s..." name)
5065     (prog1
5066         (if (eq 'nov
5067                 (setq gnus-headers-retrieved-by
5068                       (gnus-retrieve-headers
5069                        articles gnus-newsgroup-name
5070                        ;; We might want to fetch old headers, but
5071                        ;; not if there is only 1 article.
5072                        (and (or (and
5073                                  (not (eq gnus-fetch-old-headers 'some))
5074                                  (not (numberp gnus-fetch-old-headers)))
5075                                 (> (length articles) 1))
5076                             gnus-fetch-old-headers))))
5077             (gnus-get-newsgroup-headers-xover
5078              articles nil nil gnus-newsgroup-name t)
5079           (gnus-get-newsgroup-headers))
5080       (gnus-message 5 "Fetching headers for %s...done" name))))
5081
5082 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5083   "Select newsgroup GROUP.
5084 If READ-ALL is non-nil, all articles in the group are selected.
5085 If SELECT-ARTICLES, only select those articles from GROUP."
5086   (let* ((entry (gnus-group-entry group))
5087          ;;!!! Dirty hack; should be removed.
5088          (gnus-summary-ignore-duplicates
5089           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5090               t
5091             gnus-summary-ignore-duplicates))
5092          (info (nth 2 entry))
5093          articles fetched-articles cached)
5094
5095     (unless (gnus-check-server
5096              (set (make-local-variable 'gnus-current-select-method)
5097                   (gnus-find-method-for-group group)))
5098       (error "Couldn't open server"))
5099
5100     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5101         (gnus-activate-group group)     ; Or we can activate it...
5102         (progn                          ; Or we bug out.
5103           (when (equal major-mode 'gnus-summary-mode)
5104             (gnus-kill-buffer (current-buffer)))
5105           (error "Couldn't activate group %s: %s"
5106                  group (gnus-status-message group))))
5107
5108     (unless (gnus-request-group group t)
5109       (when (equal major-mode 'gnus-summary-mode)
5110         (gnus-kill-buffer (current-buffer)))
5111       (error "Couldn't request group %s: %s"
5112              group (gnus-status-message group)))
5113
5114     (when gnus-agent
5115       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5116       
5117       (setq gnus-summary-use-undownloaded-faces
5118             (gnus-agent-find-parameter
5119              group
5120              'agent-enable-undownloaded-faces)))
5121
5122     (setq gnus-newsgroup-name group
5123           gnus-newsgroup-unselected nil
5124           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5125
5126     (let ((display (gnus-group-find-parameter group 'display)))
5127       (setq gnus-newsgroup-display
5128             (cond
5129              ((not (zerop (or (car-safe read-all) 0)))
5130               ;; The user entered the group with C-u SPC/RET, let's show
5131               ;; all articles.
5132               'gnus-not-ignore)
5133              ((eq display 'all)
5134               'gnus-not-ignore)
5135              ((arrayp display)
5136               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5137              ((numberp display)
5138               ;; The following is probably the "correct" solution, but
5139               ;; it makes Gnus fetch all headers and then limit the
5140               ;; articles (which is slow), so instead we hack the
5141               ;; select-articles parameter instead. -- Simon Josefsson
5142               ;; <jas@kth.se>
5143               ;;
5144               ;; (gnus-byte-compile
5145               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5146               ;;                         display)))))
5147               (setq select-articles
5148                     (gnus-uncompress-range
5149                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5150                              (if (> tmp 0)
5151                                  tmp
5152                                1))
5153                            (cdr (gnus-active group)))))
5154               nil)
5155              (t
5156               nil))))
5157
5158     (gnus-summary-setup-default-charset)
5159
5160     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5161     (when (gnus-virtual-group-p group)
5162       (setq cached gnus-newsgroup-cached))
5163
5164     (setq gnus-newsgroup-unreads
5165           (gnus-sorted-ndifference
5166            (gnus-sorted-ndifference gnus-newsgroup-unreads
5167                                     gnus-newsgroup-marked)
5168            gnus-newsgroup-dormant))
5169
5170     (setq gnus-newsgroup-processable nil)
5171
5172     (gnus-update-read-articles group gnus-newsgroup-unreads)
5173
5174     ;; Adjust and set lists of article marks.
5175     (when info
5176       (gnus-adjust-marked-articles info))
5177     (if (setq articles select-articles)
5178         (setq gnus-newsgroup-unselected
5179               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5180       (setq articles (gnus-articles-to-read group read-all)))
5181
5182     (cond
5183      ((null articles)
5184       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5185       'quit)
5186      ((eq articles 0) nil)
5187      (t
5188       ;; Init the dependencies hash table.
5189       (setq gnus-newsgroup-dependencies
5190             (gnus-make-hashtable (length articles)))
5191       (gnus-set-global-variables)
5192       ;; Retrieve the headers and read them in.
5193
5194       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5195
5196       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5197       (when cached
5198         (setq gnus-newsgroup-cached cached))
5199
5200       ;; Suppress duplicates?
5201       (when gnus-suppress-duplicates
5202         (gnus-dup-suppress-articles))
5203
5204       ;; Set the initial limit.
5205       (setq gnus-newsgroup-limit (copy-sequence articles))
5206       ;; Remove canceled articles from the list of unread articles.
5207       (setq fetched-articles
5208             (mapcar (lambda (headers) (mail-header-number headers))
5209                     gnus-newsgroup-headers))
5210       (setq gnus-newsgroup-articles fetched-articles)
5211       (setq gnus-newsgroup-unreads
5212             (gnus-sorted-nintersection
5213              gnus-newsgroup-unreads fetched-articles))
5214       (gnus-compute-unseen-list)
5215
5216       ;; Removed marked articles that do not exist.
5217       (gnus-update-missing-marks
5218        (gnus-sorted-difference articles fetched-articles))
5219       ;; We might want to build some more threads first.
5220       (when (and gnus-fetch-old-headers
5221                  (eq gnus-headers-retrieved-by 'nov))
5222         (if (eq gnus-fetch-old-headers 'invisible)
5223             (gnus-build-all-threads)
5224           (gnus-build-old-threads)))
5225       ;; Let the Gnus agent mark articles as read.
5226       (when gnus-agent
5227         (gnus-agent-get-undownloaded-list))
5228       ;; Remove list identifiers from subject
5229       (when gnus-list-identifiers
5230         (gnus-summary-remove-list-identifiers))
5231       ;; Check whether auto-expire is to be done in this group.
5232       (setq gnus-newsgroup-auto-expire
5233             (gnus-group-auto-expirable-p group))
5234       ;; Set up the article buffer now, if necessary.
5235       (unless gnus-single-article-buffer
5236         (gnus-article-setup-buffer))
5237       ;; First and last article in this newsgroup.
5238       (when gnus-newsgroup-headers
5239         (setq gnus-newsgroup-begin
5240               (mail-header-number (car gnus-newsgroup-headers))
5241               gnus-newsgroup-end
5242               (mail-header-number
5243                (gnus-last-element gnus-newsgroup-headers))))
5244       ;; GROUP is successfully selected.
5245       (or gnus-newsgroup-headers t)))))
5246
5247 (defun gnus-compute-unseen-list ()
5248   ;; The `seen' marks are treated specially.
5249   (if (not gnus-newsgroup-seen)
5250       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5251     (setq gnus-newsgroup-unseen
5252           (gnus-inverse-list-range-intersection
5253            gnus-newsgroup-articles gnus-newsgroup-seen))))
5254
5255 (defun gnus-summary-display-make-predicate (display)
5256   (require 'gnus-agent)
5257   (when (= (length display) 1)
5258     (setq display (car display)))
5259   (unless gnus-summary-display-cache
5260     (dolist (elem (append '((unread . unread)
5261                             (read . read)
5262                             (unseen . unseen))
5263                           gnus-article-mark-lists))
5264       (push (cons (cdr elem)
5265                   (gnus-byte-compile
5266                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5267             gnus-summary-display-cache)))
5268   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5269         (gnus-category-predicate-cache gnus-summary-display-cache))
5270     (gnus-get-predicate display)))
5271
5272 ;; Uses the dynamically bound `number' variable.
5273 (eval-when-compile
5274   (defvar number))
5275 (defun gnus-article-marked-p (type &optional article)
5276   (let ((article (or article number)))
5277     (cond
5278      ((eq type 'tick)
5279       (memq article gnus-newsgroup-marked))
5280      ((eq type 'spam)
5281       (memq article gnus-newsgroup-spam-marked))
5282      ((eq type 'unsend)
5283       (memq article gnus-newsgroup-unsendable))
5284      ((eq type 'undownload)
5285       (memq article gnus-newsgroup-undownloaded))
5286      ((eq type 'download)
5287       (memq article gnus-newsgroup-downloadable))
5288      ((eq type 'unread)
5289       (memq article gnus-newsgroup-unreads))
5290      ((eq type 'read)
5291       (memq article gnus-newsgroup-reads))
5292      ((eq type 'dormant)
5293       (memq article gnus-newsgroup-dormant) )
5294      ((eq type 'expire)
5295       (memq article gnus-newsgroup-expirable))
5296      ((eq type 'reply)
5297       (memq article gnus-newsgroup-replied))
5298      ((eq type 'killed)
5299       (memq article gnus-newsgroup-killed))
5300      ((eq type 'bookmark)
5301       (assq article gnus-newsgroup-bookmarks))
5302      ((eq type 'score)
5303       (assq article gnus-newsgroup-scored))
5304      ((eq type 'save)
5305       (memq article gnus-newsgroup-saved))
5306      ((eq type 'cache)
5307       (memq article gnus-newsgroup-cached))
5308      ((eq type 'forward)
5309       (memq article gnus-newsgroup-forwarded))
5310      ((eq type 'seen)
5311       (not (memq article gnus-newsgroup-unseen)))
5312      ((eq type 'recent)
5313       (memq article gnus-newsgroup-recent))
5314      (t t))))
5315
5316 (defun gnus-articles-to-read (group &optional read-all)
5317   "Find out what articles the user wants to read."
5318   (let* ((articles
5319           ;; Select all articles if `read-all' is non-nil, or if there
5320           ;; are no unread articles.
5321           (if (or read-all
5322                   (and (zerop (length gnus-newsgroup-marked))
5323                        (zerop (length gnus-newsgroup-unreads)))
5324                   ;; Fetch all if the predicate is non-nil.
5325                   gnus-newsgroup-display)
5326               ;; We want to select the headers for all the articles in
5327               ;; the group, so we select either all the active
5328               ;; articles in the group, or (if that's nil), the
5329               ;; articles in the cache.
5330               (or
5331                (gnus-uncompress-range (gnus-active group))
5332                (gnus-cache-articles-in-group group))
5333             ;; Select only the "normal" subset of articles.
5334             (gnus-sorted-nunion
5335              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5336              gnus-newsgroup-unreads)))
5337          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5338          (scored (length scored-list))
5339          (number (length articles))
5340          (marked (+ (length gnus-newsgroup-marked)
5341                     (length gnus-newsgroup-dormant)))
5342          (select
5343           (cond
5344            ((numberp read-all)
5345             read-all)
5346            ((numberp gnus-newsgroup-display)
5347             gnus-newsgroup-display)
5348            (t
5349             (condition-case ()
5350                 (cond
5351                  ((and (or (<= scored marked) (= scored number))
5352                        (numberp gnus-large-newsgroup)
5353                        (> number gnus-large-newsgroup))
5354                   (let* ((cursor-in-echo-area nil)
5355                          (initial (gnus-parameter-large-newsgroup-initial
5356                                    gnus-newsgroup-name))
5357                          (input
5358                           (read-string
5359                            (format
5360                             "How many articles from %s (%s %d): "
5361                             (gnus-limit-string
5362                              (gnus-group-decoded-name gnus-newsgroup-name)
5363                              35)
5364                             (if initial "max" "default")
5365                             number)
5366                            (if initial
5367                                (cons (number-to-string initial)
5368                                      0)))))
5369                     (if (string-match "^[ \t]*$" input) number input)))
5370                  ((and (> scored marked) (< scored number)
5371                        (> (- scored number) 20))
5372                   (let ((input
5373                          (read-string
5374                           (format "%s %s (%d scored, %d total): "
5375                                   "How many articles from"
5376                                   (gnus-group-decoded-name group)
5377                                   scored number))))
5378                     (if (string-match "^[ \t]*$" input)
5379                         number input)))
5380                  (t number))
5381               (quit
5382                (message "Quit getting the articles to read")
5383                nil))))))
5384     (setq select (if (stringp select) (string-to-number select) select))
5385     (if (or (null select) (zerop select))
5386         select
5387       (if (and (not (zerop scored)) (<= (abs select) scored))
5388           (progn
5389             (setq articles (sort scored-list '<))
5390             (setq number (length articles)))
5391         (setq articles (copy-sequence articles)))
5392
5393       (when (< (abs select) number)
5394         (if (< select 0)
5395             ;; Select the N oldest articles.
5396             (setcdr (nthcdr (1- (abs select)) articles) nil)
5397           ;; Select the N most recent articles.
5398           (setq articles (nthcdr (- number select) articles))))
5399       (setq gnus-newsgroup-unselected
5400             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5401       (when gnus-alter-articles-to-read-function
5402         (setq articles
5403               (sort
5404                (funcall gnus-alter-articles-to-read-function
5405                         gnus-newsgroup-name articles)
5406                '<)))
5407       articles)))
5408
5409 (defun gnus-killed-articles (killed articles)
5410   (let (out)
5411     (while articles
5412       (when (inline (gnus-member-of-range (car articles) killed))
5413         (push (car articles) out))
5414       (setq articles (cdr articles)))
5415     out))
5416
5417 (defun gnus-uncompress-marks (marks)
5418   "Uncompress the mark ranges in MARKS."
5419   (let ((uncompressed '(score bookmark))
5420         out)
5421     (while marks
5422       (if (memq (caar marks) uncompressed)
5423           (push (car marks) out)
5424         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5425       (setq marks (cdr marks)))
5426     out))
5427
5428 (defun gnus-article-mark-to-type (mark)
5429   "Return the type of MARK."
5430   (or (cadr (assq mark gnus-article-special-mark-lists))
5431       'list))
5432
5433 (defun gnus-article-unpropagatable-p (mark)
5434   "Return whether MARK should be propagated to back end."
5435   (memq mark gnus-article-unpropagated-mark-lists))
5436
5437 (defun gnus-adjust-marked-articles (info)
5438   "Set all article lists and remove all marks that are no longer valid."
5439   (let* ((marked-lists (gnus-info-marks info))
5440          (active (gnus-active (gnus-info-group info)))
5441          (min (car active))
5442          (max (cdr active))
5443          (types gnus-article-mark-lists)
5444          marks var articles article mark mark-type
5445          bgn end)
5446
5447     (dolist (marks marked-lists)
5448       (setq mark (car marks)
5449             mark-type (gnus-article-mark-to-type mark)
5450             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5451
5452       ;; We set the variable according to the type of the marks list,
5453       ;; and then adjust the marks to a subset of the active articles.
5454       (cond
5455        ;; Adjust "simple" lists - compressed yet unsorted
5456        ((eq mark-type 'list)
5457         ;; Simultaneously uncompress and clip to active range
5458         ;; See gnus-uncompress-range for a description of possible marks
5459         (let (l lh)
5460           (if (not (cadr marks))
5461               (set var nil)
5462             (setq articles (if (numberp (cddr marks))
5463                                (list (cdr marks))
5464                              (cdr marks))
5465                   lh (cons nil nil)
5466                   l lh)
5467
5468             (while (setq article (pop articles))
5469               (cond ((consp article)
5470                      (setq bgn (max (car article) min)
5471                            end (min (cdr article) max))
5472                      (while (<= bgn end)
5473                        (setq l (setcdr l (cons bgn nil))
5474                              bgn (1+ bgn))))
5475                     ((and (<= min article)
5476                           (>= max article))
5477                      (setq l (setcdr l (cons article nil))))))
5478             (set var (cdr lh)))))
5479        ;; Adjust assocs.
5480        ((eq mark-type 'tuple)
5481         (set var (setq articles (cdr marks)))
5482         (when (not (listp (cdr (symbol-value var))))
5483           (set var (list (symbol-value var))))
5484         (when (not (listp (cdr articles)))
5485           (setq articles (list articles)))
5486         (while articles
5487           (when (or (not (consp (setq article (pop articles))))
5488                     (< (car article) min)
5489                     (> (car article) max))
5490             (set var (delq article (symbol-value var))))))
5491        ;; Adjust ranges (sloppily).
5492        ((eq mark-type 'range)
5493         (cond
5494          ((eq mark 'seen)
5495           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5496           ;; It should be (seen (NUM1 . NUM2)).
5497           (when (numberp (cddr marks))
5498             (setcdr marks (list (cdr marks))))
5499           (setq articles (cdr marks))
5500           (while (and articles
5501                       (or (and (consp (car articles))
5502                                (> min (cdar articles)))
5503                           (and (numberp (car articles))
5504                                (> min (car articles)))))
5505             (pop articles))
5506           (set var articles))))))))
5507
5508 (defun gnus-update-missing-marks (missing)
5509   "Go through the list of MISSING articles and remove them from the mark lists."
5510   (when missing
5511     (let (var m)
5512       ;; Go through all types.
5513       (dolist (elem gnus-article-mark-lists)
5514         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5515           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5516           (when (symbol-value var)
5517             ;; This list has articles.  So we delete all missing
5518             ;; articles from it.
5519             (setq m missing)
5520             (while m
5521               (set var (delq (pop m) (symbol-value var))))))))))
5522
5523 (defun gnus-update-marks ()
5524   "Enter the various lists of marked articles into the newsgroup info list."
5525   (let ((types gnus-article-mark-lists)
5526         (info (gnus-get-info gnus-newsgroup-name))
5527         type list newmarked symbol delta-marks)
5528     (when info
5529       ;; Add all marks lists to the list of marks lists.
5530       (while (setq type (pop types))
5531         (setq list (symbol-value
5532                     (setq symbol
5533                           (intern (format "gnus-newsgroup-%s" (car type))))))
5534
5535         (when list
5536           ;; Get rid of the entries of the articles that have the
5537           ;; default score.
5538           (when (and (eq (cdr type) 'score)
5539                      gnus-save-score
5540                      list)
5541             (let* ((arts list)
5542                    (prev (cons nil list))
5543                    (all prev))
5544               (while arts
5545                 (if (or (not (consp (car arts)))
5546                         (= (cdar arts) gnus-summary-default-score))
5547                     (setcdr prev (cdr arts))
5548                   (setq prev arts))
5549                 (setq arts (cdr arts)))
5550               (setq list (cdr all)))))
5551
5552         (when (eq (cdr type) 'seen)
5553           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5554
5555         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5556           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5557
5558         (when (and (gnus-check-backend-function
5559                     'request-set-mark gnus-newsgroup-name)
5560                    (not (gnus-article-unpropagatable-p (cdr type))))
5561           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5562                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5563                  (add (gnus-remove-from-range
5564                        (gnus-copy-sequence list) old)))
5565             (when add
5566               (push (list add 'add (list (cdr type))) delta-marks))
5567             (when del
5568               (push (list del 'del (list (cdr type))) delta-marks))))
5569
5570         (when list
5571           (push (cons (cdr type) list) newmarked)))
5572
5573       (when delta-marks
5574         (unless (gnus-check-group gnus-newsgroup-name)
5575           (error "Can't open server for %s" gnus-newsgroup-name))
5576         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5577
5578       ;; Enter these new marks into the info of the group.
5579       (if (nthcdr 3 info)
5580           (setcar (nthcdr 3 info) newmarked)
5581         ;; Add the marks lists to the end of the info.
5582         (when newmarked
5583           (setcdr (nthcdr 2 info) (list newmarked))))
5584
5585       ;; Cut off the end of the info if there's nothing else there.
5586       (let ((i 5))
5587         (while (and (> i 2)
5588                     (not (nth i info)))
5589           (when (nthcdr (decf i) info)
5590             (setcdr (nthcdr i info) nil)))))))
5591
5592 (defun gnus-set-mode-line (where)
5593   "Set the mode line of the article or summary buffers.
5594 If WHERE is `summary', the summary mode line format will be used."
5595   ;; Is this mode line one we keep updated?
5596   (when (and (memq where gnus-updated-mode-lines)
5597              (symbol-value
5598               (intern (format "gnus-%s-mode-line-format-spec" where))))
5599     (let (mode-string)
5600       (save-excursion
5601         ;; We evaluate this in the summary buffer since these
5602         ;; variables are buffer-local to that buffer.
5603         (set-buffer gnus-summary-buffer)
5604        ;; We bind all these variables that are used in the `eval' form
5605         ;; below.
5606         (let* ((mformat (symbol-value
5607                          (intern
5608                           (format "gnus-%s-mode-line-format-spec" where))))
5609                (gnus-tmp-group-name (gnus-group-decoded-name
5610                                      gnus-newsgroup-name))
5611                (gnus-tmp-article-number (or gnus-current-article 0))
5612                (gnus-tmp-unread gnus-newsgroup-unreads)
5613                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5614                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5615                (gnus-tmp-unread-and-unselected
5616                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5617                             (zerop gnus-tmp-unselected))
5618                        "")
5619                       ((zerop gnus-tmp-unselected)
5620                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5621                       (t (format "{%d(+%d) more}"
5622                                  gnus-tmp-unread-and-unticked
5623                                  gnus-tmp-unselected))))
5624                (gnus-tmp-subject
5625                 (if (and gnus-current-headers
5626                          (vectorp gnus-current-headers))
5627                     (gnus-mode-string-quote
5628                      (mail-header-subject gnus-current-headers))
5629                   ""))
5630                bufname-length max-len
5631                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5632           (setq mode-string (eval mformat))
5633           (setq bufname-length (if (string-match "%b" mode-string)
5634                                    (- (length
5635                                        (buffer-name
5636                                         (if (eq where 'summary)
5637                                             nil
5638                                           (get-buffer gnus-article-buffer))))
5639                                       2)
5640                                  0))
5641           (setq max-len (max 4 (if gnus-mode-non-string-length
5642                                    (- (window-width)
5643                                       gnus-mode-non-string-length
5644                                       bufname-length)
5645                                  (length mode-string))))
5646           ;; We might have to chop a bit of the string off...
5647           (when (> (length mode-string) max-len)
5648             (setq mode-string
5649                   (concat (truncate-string-to-width mode-string (- max-len 3))
5650                           "...")))
5651           ;; Pad the mode string a bit.
5652           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5653       ;; Update the mode line.
5654       (setq mode-line-buffer-identification
5655             (gnus-mode-line-buffer-identification (list mode-string)))
5656       (set-buffer-modified-p t))))
5657
5658 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5659   "Go through the HEADERS list and add all Xrefs to a hash table.
5660 The resulting hash table is returned, or nil if no Xrefs were found."
5661   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5662          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5663          (xref-hashtb (gnus-make-hashtable))
5664          start group entry number xrefs header)
5665     (while headers
5666       (setq header (pop headers))
5667       (when (and (setq xrefs (mail-header-xref header))
5668                  (not (memq (setq number (mail-header-number header))
5669                             unreads)))
5670         (setq start 0)
5671         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5672           (setq start (match-end 0))
5673           (setq group (if prefix
5674                           (concat prefix (substring xrefs (match-beginning 1)
5675                                                     (match-end 1)))
5676                         (substring xrefs (match-beginning 1) (match-end 1))))
5677           (setq number
5678                 (string-to-int (substring xrefs (match-beginning 2)
5679                                           (match-end 2))))
5680           (if (setq entry (gnus-gethash group xref-hashtb))
5681               (setcdr entry (cons number (cdr entry)))
5682             (gnus-sethash group (cons number nil) xref-hashtb)))))
5683     (and start xref-hashtb)))
5684
5685 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5686   "Look through all the headers and mark the Xrefs as read."
5687   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5688         name info xref-hashtb idlist method nth4)
5689     (save-excursion
5690       (set-buffer gnus-group-buffer)
5691       (when (setq xref-hashtb
5692                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5693         (mapatoms
5694          (lambda (group)
5695            (unless (string= from-newsgroup (setq name (symbol-name group)))
5696              (setq idlist (symbol-value group))
5697              ;; Dead groups are not updated.
5698              (and (prog1
5699                       (setq info (gnus-get-info name))
5700                     (when (stringp (setq nth4 (gnus-info-method info)))
5701                       (setq nth4 (gnus-server-to-method nth4))))
5702                   ;; Only do the xrefs if the group has the same
5703                   ;; select method as the group we have just read.
5704                   (or (gnus-methods-equal-p
5705                        nth4 (gnus-find-method-for-group from-newsgroup))
5706                       virtual
5707                       (equal nth4 (setq method (gnus-find-method-for-group
5708                                                 from-newsgroup)))
5709                       (and (equal (car nth4) (car method))
5710                            (equal (nth 1 nth4) (nth 1 method))))
5711                   gnus-use-cross-reference
5712                   (or (not (eq gnus-use-cross-reference t))
5713                       virtual
5714                       ;; Only do cross-references on subscribed
5715                       ;; groups, if that is what is wanted.
5716                       (<= (gnus-info-level info) gnus-level-subscribed))
5717                   (gnus-group-make-articles-read name idlist))))
5718          xref-hashtb)))))
5719
5720 (defun gnus-compute-read-articles (group articles)
5721   (let* ((entry (gnus-group-entry group))
5722          (info (nth 2 entry))
5723          (active (gnus-active group))
5724          ninfo)
5725     (when entry
5726       ;; First peel off all invalid article numbers.
5727       (when active
5728         (let ((ids articles)
5729               id first)
5730           (while (setq id (pop ids))
5731             (when (and first (> id (cdr active)))
5732               ;; We'll end up in this situation in one particular
5733               ;; obscure situation.  If you re-scan a group and get
5734               ;; a new article that is cross-posted to a different
5735               ;; group that has not been re-scanned, you might get
5736               ;; crossposted article that has a higher number than
5737               ;; Gnus believes possible.  So we re-activate this
5738               ;; group as well.  This might mean doing the
5739               ;; crossposting thingy will *increase* the number
5740               ;; of articles in some groups.  Tsk, tsk.
5741               (setq active (or (gnus-activate-group group) active)))
5742             (when (or (> id (cdr active))
5743                       (< id (car active)))
5744               (setq articles (delq id articles))))))
5745       ;; If the read list is nil, we init it.
5746       (if (and active
5747                (null (gnus-info-read info))
5748                (> (car active) 1))
5749           (setq ninfo (cons 1 (1- (car active))))
5750         (setq ninfo (gnus-info-read info)))
5751       ;; Then we add the read articles to the range.
5752       (gnus-add-to-range
5753        ninfo (setq articles (sort articles '<))))))
5754
5755 (defun gnus-group-make-articles-read (group articles)
5756   "Update the info of GROUP to say that ARTICLES are read."
5757   (let* ((num 0)
5758          (entry (gnus-group-entry group))
5759          (info (nth 2 entry))
5760          (active (gnus-active group))
5761          range)
5762     (when entry
5763       (setq range (gnus-compute-read-articles group articles))
5764       (with-current-buffer gnus-group-buffer
5765         (gnus-undo-register
5766           `(progn
5767              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5768              (gnus-info-set-read ',info ',(gnus-info-read info))
5769              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5770              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5771              (gnus-group-update-group ,group t))))
5772       ;; Add the read articles to the range.
5773       (gnus-info-set-read info range)
5774       (gnus-request-set-mark group (list (list range 'add '(read))))
5775       ;; Then we have to re-compute how many unread
5776       ;; articles there are in this group.
5777       (when active
5778         (cond
5779          ((not range)
5780           (setq num (- (1+ (cdr active)) (car active))))
5781          ((not (listp (cdr range)))
5782           (setq num (- (cdr active) (- (1+ (cdr range))
5783                                        (car range)))))
5784          (t
5785           (while range
5786             (if (numberp (car range))
5787                 (setq num (1+ num))
5788               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5789             (setq range (cdr range)))
5790           (setq num (- (cdr active) num))))
5791         ;; Update the number of unread articles.
5792         (setcar entry num)
5793         ;; Update the group buffer.
5794         (unless (gnus-ephemeral-group-p group)
5795           (gnus-group-update-group group t))))))
5796
5797 (defvar gnus-newsgroup-none-id 0)
5798
5799 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5800   (let ((cur nntp-server-buffer)
5801         (dependencies
5802          (or dependencies
5803              (with-current-buffer gnus-summary-buffer
5804                gnus-newsgroup-dependencies)))
5805         headers id end ref number
5806         (mail-parse-charset gnus-newsgroup-charset)
5807         (mail-parse-ignored-charsets
5808          (save-excursion (condition-case nil
5809                              (set-buffer gnus-summary-buffer)
5810                            (error))
5811                          gnus-newsgroup-ignored-charsets)))
5812     (save-excursion
5813       (set-buffer nntp-server-buffer)
5814       ;; Translate all TAB characters into SPACE characters.
5815       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5816       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5817       (ietf-drums-unfold-fws)
5818       (gnus-run-hooks 'gnus-parse-headers-hook)
5819       (let ((case-fold-search t)
5820             in-reply-to header p lines chars)
5821         (goto-char (point-min))
5822         ;; Search to the beginning of the next header.  Error messages
5823         ;; do not begin with 2 or 3.
5824         (while (re-search-forward "^[23][0-9]+ " nil t)
5825           (setq id nil
5826                 ref nil)
5827           ;; This implementation of this function, with nine
5828           ;; search-forwards instead of the one re-search-forward and
5829           ;; a case (which basically was the old function) is actually
5830           ;; about twice as fast, even though it looks messier.  You
5831           ;; can't have everything, I guess.  Speed and elegance
5832           ;; doesn't always go hand in hand.
5833           (setq
5834            header
5835            (vector
5836             ;; Number.
5837             (prog1
5838                 (setq number (read cur))
5839               (end-of-line)
5840               (setq p (point))
5841               (narrow-to-region (point)
5842                                 (or (and (search-forward "\n.\n" nil t)
5843                                          (- (point) 2))
5844                                     (point))))
5845             ;; Subject.
5846             (progn
5847               (goto-char p)
5848               (if (search-forward "\nsubject:" nil t)
5849                   (funcall gnus-decode-encoded-word-function
5850                            (nnheader-header-value))
5851                 "(none)"))
5852             ;; From.
5853             (progn
5854               (goto-char p)
5855               (if (search-forward "\nfrom:" nil t)
5856                   (funcall gnus-decode-encoded-word-function
5857                            (nnheader-header-value))
5858                 "(nobody)"))
5859             ;; Date.
5860             (progn
5861               (goto-char p)
5862               (if (search-forward "\ndate:" nil t)
5863                   (nnheader-header-value) ""))
5864             ;; Message-ID.
5865             (progn
5866               (goto-char p)
5867               (setq id (if (re-search-forward
5868                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5869                            ;; We do it this way to make sure the Message-ID
5870                            ;; is (somewhat) syntactically valid.
5871                            (buffer-substring (match-beginning 1)
5872                                              (match-end 1))
5873                          ;; If there was no message-id, we just fake one
5874                          ;; to make subsequent routines simpler.
5875                          (nnheader-generate-fake-message-id number))))
5876             ;; References.
5877             (progn
5878               (goto-char p)
5879               (if (search-forward "\nreferences:" nil t)
5880                   (progn
5881                     (setq end (point))
5882                     (prog1
5883                         (nnheader-header-value)
5884                       (setq ref
5885                             (buffer-substring
5886                              (progn
5887                                (end-of-line)
5888                                (search-backward ">" end t)
5889                                (1+ (point)))
5890                              (progn
5891                                (search-backward "<" end t)
5892                                (point))))))
5893                 ;; Get the references from the in-reply-to header if there
5894                 ;; were no references and the in-reply-to header looks
5895                 ;; promising.
5896                 (if (and (search-forward "\nin-reply-to:" nil t)
5897                          (setq in-reply-to (nnheader-header-value))
5898                          (string-match "<[^>]+>" in-reply-to))
5899                     (let (ref2)
5900                       (setq ref (substring in-reply-to (match-beginning 0)
5901                                            (match-end 0)))
5902                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5903                         (setq ref2 (substring in-reply-to (match-beginning 0)
5904                                               (match-end 0)))
5905                         (when (> (length ref2) (length ref))
5906                           (setq ref ref2)))
5907                       ref)
5908                   (setq ref nil))))
5909             ;; Chars.
5910             (progn
5911               (goto-char p)
5912               (if (search-forward "\nchars: " nil t)
5913                   (if (numberp (setq chars (ignore-errors (read cur))))
5914                       chars -1)
5915                 -1))
5916             ;; Lines.
5917             (progn
5918               (goto-char p)
5919               (if (search-forward "\nlines: " nil t)
5920                   (if (numberp (setq lines (ignore-errors (read cur))))
5921                       lines -1)
5922                 -1))
5923             ;; Xref.
5924             (progn
5925               (goto-char p)
5926               (and (search-forward "\nxref:" nil t)
5927                    (nnheader-header-value)))
5928             ;; Extra.
5929             (when gnus-extra-headers
5930               (let ((extra gnus-extra-headers)
5931                     out)
5932                 (while extra
5933                   (goto-char p)
5934                   (when (search-forward
5935                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5936                     (push (cons (car extra) (nnheader-header-value))
5937                           out))
5938                   (pop extra))
5939                 out))))
5940           (when (equal id ref)
5941             (setq ref nil))
5942
5943           (when gnus-alter-header-function
5944             (funcall gnus-alter-header-function header)
5945             (setq id (mail-header-id header)
5946                   ref (gnus-parent-id (mail-header-references header))))
5947
5948           (when (setq header
5949                       (gnus-dependencies-add-header
5950                        header dependencies force-new))
5951             (push header headers))
5952           (goto-char (point-max))
5953           (widen))
5954         (nreverse headers)))))
5955
5956 ;; Goes through the xover lines and returns a list of vectors
5957 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5958                                                   force-new dependencies
5959                                                   group also-fetch-heads)
5960   "Parse the news overview data in the server buffer.
5961 Return a list of headers that match SEQUENCE (see
5962 `nntp-retrieve-headers')."
5963   ;; Get the Xref when the users reads the articles since most/some
5964   ;; NNTP servers do not include Xrefs when using XOVER.
5965   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5966   (let ((mail-parse-charset gnus-newsgroup-charset)
5967         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5968         (cur nntp-server-buffer)
5969         (dependencies (or dependencies gnus-newsgroup-dependencies))
5970         (allp (cond
5971                ((eq gnus-read-all-available-headers t)
5972                 t)
5973                ((stringp gnus-read-all-available-headers)
5974                 (string-match gnus-read-all-available-headers group))
5975                (t
5976                 nil)))
5977         number headers header)
5978     (save-excursion
5979       (set-buffer nntp-server-buffer)
5980       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5981       ;; Allow the user to mangle the headers before parsing them.
5982       (gnus-run-hooks 'gnus-parse-headers-hook)
5983       (goto-char (point-min))
5984       (gnus-parse-without-error
5985         (while (and (or sequence allp)
5986                     (not (eobp)))
5987           (setq number (read cur))
5988           (when (not allp)
5989             (while (and sequence
5990                         (< (car sequence) number))
5991               (setq sequence (cdr sequence))))
5992           (when (and (or allp
5993                          (and sequence
5994                               (eq number (car sequence))))
5995                      (progn
5996                        (setq sequence (cdr sequence))
5997                        (setq header (inline
5998                                       (gnus-nov-parse-line
5999                                        number dependencies force-new)))))
6000             (push header headers))
6001           (forward-line 1)))
6002       ;; A common bug in inn is that if you have posted an article and
6003       ;; then retrieves the active file, it will answer correctly --
6004       ;; the new article is included.  However, a NOV entry for the
6005       ;; article may not have been generated yet, so this may fail.
6006       ;; We work around this problem by retrieving the last few
6007       ;; headers using HEAD.
6008       (if (or (not also-fetch-heads)
6009               (not sequence))
6010           ;; We (probably) got all the headers.
6011           (nreverse headers)
6012         (let ((gnus-nov-is-evil t))
6013           (nconc
6014            (nreverse headers)
6015            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6016              (gnus-get-newsgroup-headers))))))))
6017
6018 (defun gnus-article-get-xrefs ()
6019   "Fill in the Xref value in `gnus-current-headers', if necessary.
6020 This is meant to be called in `gnus-article-internal-prepare-hook'."
6021   (let ((headers (with-current-buffer gnus-summary-buffer
6022                    gnus-current-headers)))
6023     (or (not gnus-use-cross-reference)
6024         (not headers)
6025         (and (mail-header-xref headers)
6026              (not (string= (mail-header-xref headers) "")))
6027         (let ((case-fold-search t)
6028               xref)
6029           (save-restriction
6030             (nnheader-narrow-to-headers)
6031             (goto-char (point-min))
6032             (when (or (and (not (eobp))
6033                            (eq (downcase (char-after)) ?x)
6034                            (looking-at "Xref:"))
6035                       (search-forward "\nXref:" nil t))
6036               (goto-char (1+ (match-end 0)))
6037               (setq xref (buffer-substring (point) (point-at-eol)))
6038               (mail-header-set-xref headers xref)))))))
6039
6040 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6041   "Find article ID and insert the summary line for that article.
6042 OLD-HEADER can either be a header or a line number to insert
6043 the subject line on."
6044   (let* ((line (and (numberp old-header) old-header))
6045          (old-header (and (vectorp old-header) old-header))
6046          (header (cond ((and old-header use-old-header)
6047                         old-header)
6048                        ((and (numberp id)
6049                              (gnus-number-to-header id))
6050                         (gnus-number-to-header id))
6051                        (t
6052                         (gnus-read-header id))))
6053          (number (and (numberp id) id))
6054          d)
6055     (when header
6056       ;; Rebuild the thread that this article is part of and go to the
6057       ;; article we have fetched.
6058       (when (and (not gnus-show-threads)
6059                  old-header)
6060         (when (and number
6061                    (setq d (gnus-data-find (mail-header-number old-header))))
6062           (goto-char (gnus-data-pos d))
6063           (gnus-data-remove
6064            number
6065            (- (point-at-bol)
6066               (prog1
6067                   (1+ (point-at-eol))
6068                 (gnus-delete-line))))))
6069       ;; Remove list identifiers from subject.
6070       (when gnus-list-identifiers
6071         (let ((gnus-newsgroup-headers (list header)))
6072           (gnus-summary-remove-list-identifiers)
6073           (setq header (car gnus-newsgroup-headers))))
6074       (when old-header
6075         (mail-header-set-number header (mail-header-number old-header)))
6076       (setq gnus-newsgroup-sparse
6077             (delq (setq number (mail-header-number header))
6078                   gnus-newsgroup-sparse))
6079       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6080       (push number gnus-newsgroup-limit)
6081       (gnus-rebuild-thread (mail-header-id header) line)
6082       (gnus-summary-goto-subject number nil t))
6083     (when (and (numberp number)
6084                (> number 0))
6085       ;; We have to update the boundaries even if we can't fetch the
6086       ;; article if ID is a number -- so that the next `P' or `N'
6087       ;; command will fetch the previous (or next) article even
6088       ;; if the one we tried to fetch this time has been canceled.
6089       (when (> number gnus-newsgroup-end)
6090         (setq gnus-newsgroup-end number))
6091       (when (< number gnus-newsgroup-begin)
6092         (setq gnus-newsgroup-begin number))
6093       (setq gnus-newsgroup-unselected
6094             (delq number gnus-newsgroup-unselected)))
6095     ;; Report back a success?
6096     (and header (mail-header-number header))))
6097
6098 ;;; Process/prefix in the summary buffer
6099
6100 (defun gnus-summary-work-articles (n)
6101   "Return a list of articles to be worked upon.
6102 The prefix argument, the list of process marked articles, and the
6103 current article will be taken into consideration."
6104   (save-excursion
6105     (set-buffer gnus-summary-buffer)
6106     (cond
6107      (n
6108       ;; A numerical prefix has been given.
6109       (setq n (prefix-numeric-value n))
6110       (let ((backward (< n 0))
6111             (n (abs (prefix-numeric-value n)))
6112             articles article)
6113         (save-excursion
6114           (while
6115               (and (> n 0)
6116                    (push (setq article (gnus-summary-article-number))
6117                          articles)
6118                    (if backward
6119                        (gnus-summary-find-prev nil article)
6120                      (gnus-summary-find-next nil article)))
6121             (decf n)))
6122         (nreverse articles)))
6123      ((and (gnus-region-active-p) (mark))
6124       (message "region active")
6125       ;; Work on the region between point and mark.
6126       (let ((max (max (point) (mark)))
6127             articles article)
6128         (save-excursion
6129           (goto-char (min (min (point) (mark))))
6130           (while
6131               (and
6132                (push (setq article (gnus-summary-article-number)) articles)
6133                (gnus-summary-find-next nil article)
6134                (< (point) max)))
6135           (nreverse articles))))
6136      (gnus-newsgroup-processable
6137       ;; There are process-marked articles present.
6138       ;; Save current state.
6139       (gnus-summary-save-process-mark)
6140       ;; Return the list.
6141       (reverse gnus-newsgroup-processable))
6142      (t
6143       ;; Just return the current article.
6144       (list (gnus-summary-article-number))))))
6145
6146 (defmacro gnus-summary-iterate (arg &rest forms)
6147   "Iterate over the process/prefixed articles and do FORMS.
6148 ARG is the interactive prefix given to the command.  FORMS will be
6149 executed with point over the summary line of the articles."
6150   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6151     `(let ((,articles (gnus-summary-work-articles ,arg)))
6152        (while ,articles
6153          (gnus-summary-goto-subject (car ,articles))
6154          ,@forms
6155          (pop ,articles)))))
6156
6157 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6158 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6159
6160 (defun gnus-summary-save-process-mark ()
6161   "Push the current set of process marked articles on the stack."
6162   (interactive)
6163   (push (copy-sequence gnus-newsgroup-processable)
6164         gnus-newsgroup-process-stack))
6165
6166 (defun gnus-summary-kill-process-mark ()
6167   "Push the current set of process marked articles on the stack and unmark."
6168   (interactive)
6169   (gnus-summary-save-process-mark)
6170   (gnus-summary-unmark-all-processable))
6171
6172 (defun gnus-summary-yank-process-mark ()
6173   "Pop the last process mark state off the stack and restore it."
6174   (interactive)
6175   (unless gnus-newsgroup-process-stack
6176     (error "Empty mark stack"))
6177   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6178
6179 (defun gnus-summary-process-mark-set (set)
6180   "Make SET into the current process marked articles."
6181   (gnus-summary-unmark-all-processable)
6182   (while set
6183     (gnus-summary-set-process-mark (pop set))))
6184
6185 ;;; Searching and stuff
6186
6187 (defun gnus-summary-search-group (&optional backward use-level)
6188   "Search for next unread newsgroup.
6189 If optional argument BACKWARD is non-nil, search backward instead."
6190   (save-excursion
6191     (set-buffer gnus-group-buffer)
6192     (when (gnus-group-search-forward
6193            backward nil (if use-level (gnus-group-group-level) nil))
6194       (gnus-group-group-name))))
6195
6196 (defun gnus-summary-best-group (&optional exclude-group)
6197   "Find the name of the best unread group.
6198 If EXCLUDE-GROUP, do not go to this group."
6199   (with-current-buffer gnus-group-buffer
6200     (save-excursion
6201       (gnus-group-best-unread-group exclude-group))))
6202
6203 (defun gnus-summary-find-next (&optional unread article backward)
6204   (if backward
6205       (gnus-summary-find-prev unread article)
6206     (let* ((dummy (gnus-summary-article-intangible-p))
6207            (article (or article (gnus-summary-article-number)))
6208            (data (gnus-data-find-list article))
6209            result)
6210       (when (and (not dummy)
6211                  (or (not gnus-summary-check-current)
6212                      (not unread)
6213                      (not (gnus-data-unread-p (car data)))))
6214         (setq data (cdr data)))
6215       (when (setq result
6216                   (if unread
6217                       (progn
6218                         (while data
6219                           (unless (memq (gnus-data-number (car data))
6220                                         (cond
6221                                          ((eq gnus-auto-goto-ignores
6222                                               'always-undownloaded)
6223                                           gnus-newsgroup-undownloaded)
6224                                          (gnus-plugged
6225                                           nil)
6226                                          ((eq gnus-auto-goto-ignores
6227                                               'unfetched)
6228                                           gnus-newsgroup-unfetched)
6229                                          ((eq gnus-auto-goto-ignores
6230                                               'undownloaded)
6231                                           gnus-newsgroup-undownloaded)))
6232                             (when (gnus-data-unread-p (car data))
6233                               (setq result (car data)
6234                                     data nil)))
6235                           (setq data (cdr data)))
6236                         result)
6237                     (car data)))
6238         (goto-char (gnus-data-pos result))
6239         (gnus-data-number result)))))
6240
6241 (defun gnus-summary-find-prev (&optional unread article)
6242   (let* ((eobp (eobp))
6243          (article (or article (gnus-summary-article-number)))
6244          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6245          result)
6246     (when (and (not eobp)
6247                (or (not gnus-summary-check-current)
6248                    (not unread)
6249                    (not (gnus-data-unread-p (car data)))))
6250       (setq data (cdr data)))
6251     (when (setq result
6252                 (if unread
6253                     (progn
6254                       (while data
6255                         (unless (memq (gnus-data-number (car data))
6256                                       (cond
6257                                        ((eq gnus-auto-goto-ignores
6258                                             'always-undownloaded)
6259                                         gnus-newsgroup-undownloaded)
6260                                        (gnus-plugged
6261                                         nil)
6262                                        ((eq gnus-auto-goto-ignores
6263                                             'unfetched)
6264                                         gnus-newsgroup-unfetched)
6265                                        ((eq gnus-auto-goto-ignores
6266                                             'undownloaded)
6267                                         gnus-newsgroup-undownloaded)))
6268                           (when (gnus-data-unread-p (car data))
6269                             (setq result (car data)
6270                                   data nil)))
6271                         (setq data (cdr data)))
6272                       result)
6273                   (car data)))
6274       (goto-char (gnus-data-pos result))
6275       (gnus-data-number result))))
6276
6277 (defun gnus-summary-find-subject (subject &optional unread backward article)
6278   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6279          (article (or article (gnus-summary-article-number)))
6280          (articles (gnus-data-list backward))
6281          (arts (gnus-data-find-list article articles))
6282          result)
6283     (when (or (not gnus-summary-check-current)
6284               (not unread)
6285               (not (gnus-data-unread-p (car arts))))
6286       (setq arts (cdr arts)))
6287     (while arts
6288       (and (or (not unread)
6289                (gnus-data-unread-p (car arts)))
6290            (vectorp (gnus-data-header (car arts)))
6291            (gnus-subject-equal
6292             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6293            (setq result (car arts)
6294                  arts nil))
6295       (setq arts (cdr arts)))
6296     (and result
6297          (goto-char (gnus-data-pos result))
6298          (gnus-data-number result))))
6299
6300 (defun gnus-summary-search-forward (&optional unread subject backward)
6301   "Search forward for an article.
6302 If UNREAD, look for unread articles.  If SUBJECT, look for
6303 articles with that subject.  If BACKWARD, search backward instead."
6304   (cond (subject (gnus-summary-find-subject subject unread backward))
6305         (backward (gnus-summary-find-prev unread))
6306         (t (gnus-summary-find-next unread))))
6307
6308 (defun gnus-recenter (&optional n)
6309   "Center point in window and redisplay frame.
6310 Also do horizontal recentering."
6311   (interactive "P")
6312   (when (and gnus-auto-center-summary
6313              (not (eq gnus-auto-center-summary 'vertical)))
6314     (gnus-horizontal-recenter))
6315   (recenter n))
6316
6317 (defun gnus-summary-recenter ()
6318   "Center point in the summary window.
6319 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6320 displayed, no centering will be performed."
6321   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6322   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6323   (interactive)
6324   ;; The user has to want it.
6325   (when gnus-auto-center-summary
6326     (let* ((top (cond ((< (window-height) 4) 0)
6327                       ((< (window-height) 7) 1)
6328                       (t (if (numberp gnus-auto-center-summary)
6329                              gnus-auto-center-summary
6330                            (/ (1- (window-height)) 2)))))
6331            (height (1- (window-height)))
6332            (bottom (save-excursion (goto-char (point-max))
6333                                    (forward-line (- height))
6334                                    (point)))
6335            (window (get-buffer-window (current-buffer))))
6336       (when (get-buffer-window gnus-article-buffer)
6337         ;; Only do recentering when the article buffer is displayed,
6338         ;; Set the window start to either `bottom', which is the biggest
6339         ;; possible valid number, or the second line from the top,
6340         ;; whichever is the least.
6341         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6342           (if (> bottom top-pos)
6343               ;; Keep the second line from the top visible
6344               (set-window-start window top-pos t)
6345             ;; Try to keep the bottom line visible; if it's partially
6346             ;; obscured, either scroll one more line to make it fully
6347             ;; visible, or revert to using TOP-POS.
6348             (save-excursion
6349               (goto-char (point-max))
6350               (forward-line -1)
6351               (let ((last-line-start (point)))
6352                 (goto-char bottom)
6353                 (set-window-start window (point) t)
6354                 (when (not (pos-visible-in-window-p last-line-start window))
6355                   (forward-line 1)
6356                   (set-window-start window (min (point) top-pos) t)))))))
6357       ;; Do horizontal recentering while we're at it.
6358       (when (and (get-buffer-window (current-buffer) t)
6359                  (not (eq gnus-auto-center-summary 'vertical)))
6360         (let ((selected (selected-window)))
6361           (select-window (get-buffer-window (current-buffer) t))
6362           (gnus-summary-position-point)
6363           (gnus-horizontal-recenter)
6364           (select-window selected))))))
6365
6366 (defun gnus-summary-jump-to-group (newsgroup)
6367   "Move point to NEWSGROUP in group mode buffer."
6368   ;; Keep update point of group mode buffer if visible.
6369   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6370       (save-window-excursion
6371         ;; Take care of tree window mode.
6372         (when (get-buffer-window gnus-group-buffer)
6373           (pop-to-buffer gnus-group-buffer))
6374         (gnus-group-jump-to-group newsgroup))
6375     (save-excursion
6376       ;; Take care of tree window mode.
6377       (if (get-buffer-window gnus-group-buffer)
6378           (pop-to-buffer gnus-group-buffer)
6379         (set-buffer gnus-group-buffer))
6380       (gnus-group-jump-to-group newsgroup))))
6381
6382 ;; This function returns a list of article numbers based on the
6383 ;; difference between the ranges of read articles in this group and
6384 ;; the range of active articles.
6385 (defun gnus-list-of-unread-articles (group)
6386   (let* ((read (gnus-info-read (gnus-get-info group)))
6387          (active (or (gnus-active group) (gnus-activate-group group)))
6388          (last (cdr active))
6389          first nlast unread)
6390     ;; If none are read, then all are unread.
6391     (if (not read)
6392         (setq first (car active))
6393       ;; If the range of read articles is a single range, then the
6394       ;; first unread article is the article after the last read
6395       ;; article.  Sounds logical, doesn't it?
6396       (if (and (not (listp (cdr read)))
6397                (or (< (car read) (car active))
6398                    (progn (setq read (list read))
6399                           nil)))
6400           (setq first (max (car active) (1+ (cdr read))))
6401         ;; `read' is a list of ranges.
6402         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6403                                   (caar read)))
6404                   1)
6405           (setq first (car active)))
6406         (while read
6407           (when first
6408             (while (< first nlast)
6409               (setq unread (cons first unread)
6410                     first (1+ first))))
6411           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6412           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6413           (setq read (cdr read)))))
6414     ;; And add the last unread articles.
6415     (while (<= first last)
6416       (setq unread (cons first unread)
6417             first (1+ first)))
6418     ;; Return the list of unread articles.
6419     (delq 0 (nreverse unread))))
6420
6421 (defun gnus-list-of-read-articles (group)
6422   "Return a list of unread, unticked and non-dormant articles."
6423   (let* ((info (gnus-get-info group))
6424          (marked (gnus-info-marks info))
6425          (active (gnus-active group)))
6426     (and info active
6427          (gnus-list-range-difference
6428           (gnus-list-range-difference
6429            (gnus-sorted-complement
6430             (gnus-uncompress-range active)
6431             (gnus-list-of-unread-articles group))
6432            (cdr (assq 'dormant marked)))
6433           (cdr (assq 'tick marked))))))
6434
6435 ;; This function returns a sequence of article numbers based on the
6436 ;; difference between the ranges of read articles in this group and
6437 ;; the range of active articles.
6438 (defun gnus-sequence-of-unread-articles (group)
6439   (let* ((read (gnus-info-read (gnus-get-info group)))
6440          (active (or (gnus-active group) (gnus-activate-group group)))
6441          (last (cdr active))
6442          first nlast unread)
6443     ;; If none are read, then all are unread.
6444     (if (not read)
6445         (setq first (car active))
6446       ;; If the range of read articles is a single range, then the
6447       ;; first unread article is the article after the last read
6448       ;; article.  Sounds logical, doesn't it?
6449       (if (and (not (listp (cdr read)))
6450                (or (< (car read) (car active))
6451                    (progn (setq read (list read))
6452                           nil)))
6453           (setq first (max (car active) (1+ (cdr read))))
6454         ;; `read' is a list of ranges.
6455         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6456                                   (caar read)))
6457                   1)
6458           (setq first (car active)))
6459         (while read
6460           (when first
6461             (push (cons first nlast) unread))
6462           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6463           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6464           (setq read (cdr read)))))
6465     ;; And add the last unread articles.
6466     (cond ((< first last)
6467            (push (cons first last) unread))
6468           ((= first last)
6469            (push first unread)))
6470     ;; Return the sequence of unread articles.
6471     (delq 0 (nreverse unread))))
6472
6473 ;; Various summary commands
6474
6475 (defun gnus-summary-select-article-buffer ()
6476   "Reconfigure windows to show article buffer."
6477   (interactive)
6478   (if (not (gnus-buffer-live-p gnus-article-buffer))
6479       (error "There is no article buffer for this summary buffer")
6480     (gnus-configure-windows 'article)
6481     (select-window (get-buffer-window gnus-article-buffer))))
6482
6483 (defun gnus-summary-universal-argument (arg)
6484   "Perform any operation on all articles that are process/prefixed."
6485   (interactive "P")
6486   (let ((articles (gnus-summary-work-articles arg))
6487         func article)
6488     (if (eq
6489          (setq
6490           func
6491           (key-binding
6492            (read-key-sequence
6493             (substitute-command-keys
6494              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6495          'undefined)
6496         (gnus-error 1 "Undefined key")
6497       (save-excursion
6498         (while articles
6499           (gnus-summary-goto-subject (setq article (pop articles)))
6500           (let (gnus-newsgroup-processable)
6501             (command-execute func))
6502           (gnus-summary-remove-process-mark article)))))
6503   (gnus-summary-position-point))
6504
6505 (defun gnus-summary-toggle-truncation (&optional arg)
6506   "Toggle truncation of summary lines.
6507 With ARG, turn line truncation on if ARG is positive."
6508   (interactive "P")
6509   (setq truncate-lines
6510         (if (null arg) (not truncate-lines)
6511           (> (prefix-numeric-value arg) 0)))
6512   (redraw-display))
6513
6514 (defun gnus-summary-find-for-reselect ()
6515   "Return the number of an article to stay on across a reselect.
6516 The current article is considered, then following articles, then previous
6517 articles.  An article is sought which is not cancelled and isn't a temporary
6518 insertion from another group.  If there's no such then return a dummy 0."
6519   (let (found)
6520     (dolist (rev '(nil t))
6521       (unless found      ; don't demand the reverse list if we don't need it
6522         (let ((data (gnus-data-find-list
6523                      (gnus-summary-article-number) (gnus-data-list rev))))
6524           (while (and data (not found))
6525             (if (and (< 0 (gnus-data-number (car data)))
6526                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6527                 (setq found (gnus-data-number (car data))))
6528             (setq data (cdr data))))))
6529     (or found 0)))
6530
6531 (defun gnus-summary-reselect-current-group (&optional all rescan)
6532   "Exit and then reselect the current newsgroup.
6533 The prefix argument ALL means to select all articles."
6534   (interactive "P")
6535   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6536     (error "Ephemeral groups can't be reselected"))
6537   (let ((current-subject (gnus-summary-find-for-reselect))
6538         (group gnus-newsgroup-name))
6539     (setq gnus-newsgroup-begin nil)
6540     (gnus-summary-exit nil 'leave-hidden)
6541     ;; We have to adjust the point of group mode buffer because
6542     ;; point was moved to the next unread newsgroup by exiting.
6543     (gnus-summary-jump-to-group group)
6544     (when rescan
6545       (save-excursion
6546         (gnus-group-get-new-news-this-group 1)))
6547     (gnus-group-read-group all t)
6548     (gnus-summary-goto-subject current-subject nil t)))
6549
6550 (defun gnus-summary-rescan-group (&optional all)
6551   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6552   (interactive "P")
6553   (gnus-summary-reselect-current-group all t))
6554
6555 (defun gnus-summary-update-info (&optional non-destructive)
6556   (save-excursion
6557     (let ((group gnus-newsgroup-name))
6558       (when group
6559         (when gnus-newsgroup-kill-headers
6560           (setq gnus-newsgroup-killed
6561                 (gnus-compress-sequence
6562                  (gnus-sorted-union
6563                   (gnus-list-range-intersection
6564                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6565                   gnus-newsgroup-unreads)
6566                  t)))
6567         (unless (listp (cdr gnus-newsgroup-killed))
6568           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6569         (let ((headers gnus-newsgroup-headers))
6570           ;; Set the new ranges of read articles.
6571           (with-current-buffer gnus-group-buffer
6572             (gnus-undo-force-boundary))
6573           (gnus-update-read-articles
6574            group (gnus-sorted-union
6575                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6576           ;; Set the current article marks.
6577           (let ((gnus-newsgroup-scored
6578                  (if (and (not gnus-save-score)
6579                           (not non-destructive))
6580                      nil
6581                    gnus-newsgroup-scored)))
6582             (save-excursion
6583               (gnus-update-marks)))
6584           ;; Do the cross-ref thing.
6585           (when gnus-use-cross-reference
6586             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6587           ;; Do not switch windows but change the buffer to work.
6588           (set-buffer gnus-group-buffer)
6589           (unless (gnus-ephemeral-group-p group)
6590             (gnus-group-update-group group)))))))
6591
6592 (defun gnus-summary-save-newsrc (&optional force)
6593   "Save the current number of read/marked articles in the dribble buffer.
6594 The dribble buffer will then be saved.
6595 If FORCE (the prefix), also save the .newsrc file(s)."
6596   (interactive "P")
6597   (gnus-summary-update-info t)
6598   (if force
6599       (gnus-save-newsrc-file)
6600     (gnus-dribble-save)))
6601
6602 (defun gnus-summary-exit (&optional temporary leave-hidden)
6603   "Exit reading current newsgroup, and then return to group selection mode.
6604 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6605   (interactive)
6606   (gnus-set-global-variables)
6607   (when (gnus-buffer-live-p gnus-article-buffer)
6608     (save-excursion
6609       (set-buffer gnus-article-buffer)
6610       (mm-destroy-parts gnus-article-mime-handles)
6611       ;; Set it to nil for safety reason.
6612       (setq gnus-article-mime-handle-alist nil)
6613       (setq gnus-article-mime-handles nil)))
6614   (gnus-kill-save-kill-buffer)
6615   (gnus-async-halt-prefetch)
6616   (let* ((group gnus-newsgroup-name)
6617          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6618          (gnus-group-is-exiting-p t)
6619          (mode major-mode)
6620          (group-point nil)
6621          (buf (current-buffer)))
6622     (unless quit-config
6623       ;; Do adaptive scoring, and possibly save score files.
6624       (when gnus-newsgroup-adaptive
6625         (gnus-score-adaptive))
6626       (when gnus-use-scoring
6627         (gnus-score-save)))
6628     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6629     ;; If we have several article buffers, we kill them at exit.
6630     (unless gnus-single-article-buffer
6631       (gnus-kill-buffer gnus-original-article-buffer)
6632       (setq gnus-article-current nil))
6633     (when gnus-use-cache
6634       (gnus-cache-possibly-remove-articles)
6635       (gnus-cache-save-buffers))
6636     (gnus-async-prefetch-remove-group group)
6637     (when gnus-suppress-duplicates
6638       (gnus-dup-enter-articles))
6639     (when gnus-use-trees
6640       (gnus-tree-close group))
6641     (when gnus-use-cache
6642       (gnus-cache-write-active))
6643     ;; Remove entries for this group.
6644     (nnmail-purge-split-history (gnus-group-real-name group))
6645     ;; Make all changes in this group permanent.
6646     (unless quit-config
6647       (gnus-run-hooks 'gnus-exit-group-hook)
6648       (gnus-summary-update-info))
6649     (gnus-close-group group)
6650     ;; Make sure where we were, and go to next newsgroup.
6651     (set-buffer gnus-group-buffer)
6652     (unless quit-config
6653       (gnus-group-jump-to-group group))
6654     (gnus-run-hooks 'gnus-summary-exit-hook)
6655     (unless (or quit-config
6656                 ;; If this group has disappeared from the summary
6657                 ;; buffer, don't skip forwards.
6658                 (not (string= group (gnus-group-group-name))))
6659       (gnus-group-next-unread-group 1))
6660     (setq group-point (point))
6661     (if temporary
6662         nil                             ;Nothing to do.
6663       ;; If we have several article buffers, we kill them at exit.
6664       (unless gnus-single-article-buffer
6665         (gnus-kill-buffer gnus-article-buffer)
6666         (gnus-kill-buffer gnus-original-article-buffer)
6667         (setq gnus-article-current nil))
6668       (set-buffer buf)
6669       (if (not gnus-kill-summary-on-exit)
6670           (progn
6671             (gnus-deaden-summary)
6672             (setq mode nil))
6673         ;; We set all buffer-local variables to nil.  It is unclear why
6674         ;; this is needed, but if we don't, buffer-local variables are
6675         ;; not garbage-collected, it seems.  This would the lead to en
6676         ;; ever-growing Emacs.
6677         (gnus-summary-clear-local-variables)
6678         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6679           (gnus-summary-clear-local-variables))
6680         (when (get-buffer gnus-article-buffer)
6681           (bury-buffer gnus-article-buffer))
6682         ;; We clear the global counterparts of the buffer-local
6683         ;; variables as well, just to be on the safe side.
6684         (set-buffer gnus-group-buffer)
6685         (gnus-summary-clear-local-variables)
6686         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6687           (gnus-summary-clear-local-variables))
6688         ;; Return to group mode buffer.
6689         (when (eq mode 'gnus-summary-mode)
6690           (gnus-kill-buffer buf)))
6691       (setq gnus-current-select-method gnus-select-method)
6692       (if leave-hidden
6693           (set-buffer gnus-group-buffer)
6694         (pop-to-buffer gnus-group-buffer))
6695       (if (not quit-config)
6696           (progn
6697             (goto-char group-point)
6698             (unless leave-hidden
6699               (gnus-configure-windows 'group 'force)))
6700         (gnus-handle-ephemeral-exit quit-config))
6701       ;; Clear the current group name.
6702       (unless quit-config
6703         (setq gnus-newsgroup-name nil)))))
6704
6705 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6706 (defun gnus-summary-exit-no-update (&optional no-questions)
6707   "Quit reading current newsgroup without updating read article info."
6708   (interactive)
6709   (let* ((group gnus-newsgroup-name)
6710          (gnus-group-is-exiting-p t)
6711          (gnus-group-is-exiting-without-update-p t)
6712          (quit-config (gnus-group-quit-config group)))
6713     (when (or no-questions
6714               gnus-expert-user
6715               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6716       (gnus-async-halt-prefetch)
6717       (run-hooks 'gnus-summary-prepare-exit-hook)
6718       (when (gnus-buffer-live-p gnus-article-buffer)
6719         (save-excursion
6720           (set-buffer gnus-article-buffer)
6721           (mm-destroy-parts gnus-article-mime-handles)
6722           ;; Set it to nil for safety reason.
6723           (setq gnus-article-mime-handle-alist nil)
6724           (setq gnus-article-mime-handles nil)))
6725       ;; If we have several article buffers, we kill them at exit.
6726       (unless gnus-single-article-buffer
6727         (gnus-kill-buffer gnus-article-buffer)
6728         (gnus-kill-buffer gnus-original-article-buffer)
6729         (setq gnus-article-current nil))
6730       (if (not gnus-kill-summary-on-exit)
6731           (gnus-deaden-summary)
6732         (gnus-close-group group)
6733         (gnus-summary-clear-local-variables)
6734         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6735           (gnus-summary-clear-local-variables))
6736         (set-buffer gnus-group-buffer)
6737         (gnus-summary-clear-local-variables)
6738         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6739           (gnus-summary-clear-local-variables))
6740         (gnus-kill-buffer gnus-summary-buffer))
6741       (unless gnus-single-article-buffer
6742         (setq gnus-article-current nil))
6743       (when gnus-use-trees
6744         (gnus-tree-close group))
6745       (gnus-async-prefetch-remove-group group)
6746       (when (get-buffer gnus-article-buffer)
6747         (bury-buffer gnus-article-buffer))
6748       ;; Return to the group buffer.
6749       (gnus-configure-windows 'group 'force)
6750       ;; Clear the current group name.
6751       (setq gnus-newsgroup-name nil)
6752       (unless (gnus-ephemeral-group-p group)
6753         (gnus-group-update-group group))
6754       (when (equal (gnus-group-group-name) group)
6755         (gnus-group-next-unread-group 1))
6756       (when quit-config
6757         (gnus-handle-ephemeral-exit quit-config)))))
6758
6759 (defun gnus-handle-ephemeral-exit (quit-config)
6760   "Handle movement when leaving an ephemeral group.
6761 The state which existed when entering the ephemeral is reset."
6762   (if (not (buffer-name (car quit-config)))
6763       (gnus-configure-windows 'group 'force)
6764     (set-buffer (car quit-config))
6765     (cond ((eq major-mode 'gnus-summary-mode)
6766            (gnus-set-global-variables))
6767           ((eq major-mode 'gnus-article-mode)
6768            (save-excursion
6769              ;; The `gnus-summary-buffer' variable may point
6770              ;; to the old summary buffer when using a single
6771              ;; article buffer.
6772              (unless (gnus-buffer-live-p gnus-summary-buffer)
6773                (set-buffer gnus-group-buffer))
6774              (set-buffer gnus-summary-buffer)
6775              (gnus-set-global-variables))))
6776     (if (or (eq (cdr quit-config) 'article)
6777             (eq (cdr quit-config) 'pick))
6778         (progn
6779           ;; The current article may be from the ephemeral group
6780           ;; thus it is best that we reload this article
6781           ;;
6782           ;; If we're exiting from a large digest, this can be
6783           ;; extremely slow.  So, it's better not to reload it. -- jh.
6784           ;;(gnus-summary-show-article)
6785           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6786               (gnus-configure-windows 'pick 'force)
6787             (gnus-configure-windows (cdr quit-config) 'force)))
6788       (gnus-configure-windows (cdr quit-config) 'force))
6789     (when (eq major-mode 'gnus-summary-mode)
6790       (gnus-summary-next-subject 1 nil t)
6791       (gnus-summary-recenter)
6792       (gnus-summary-position-point))))
6793
6794 ;;; Dead summaries.
6795
6796 (defvar gnus-dead-summary-mode-map nil)
6797
6798 (unless gnus-dead-summary-mode-map
6799   (setq gnus-dead-summary-mode-map (make-keymap))
6800   (suppress-keymap gnus-dead-summary-mode-map)
6801   (substitute-key-definition
6802    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6803   (dolist (key '("\C-d" "\r" "\177" [delete]))
6804     (define-key gnus-dead-summary-mode-map
6805       key 'gnus-summary-wake-up-the-dead))
6806   (dolist (key '("q" "Q"))
6807     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6808
6809 (defvar gnus-dead-summary-mode nil
6810   "Minor mode for Gnus summary buffers.")
6811
6812 (defun gnus-dead-summary-mode (&optional arg)
6813   "Minor mode for Gnus summary buffers."
6814   (interactive "P")
6815   (when (eq major-mode 'gnus-summary-mode)
6816     (make-local-variable 'gnus-dead-summary-mode)
6817     (setq gnus-dead-summary-mode
6818           (if (null arg) (not gnus-dead-summary-mode)
6819             (> (prefix-numeric-value arg) 0)))
6820     (when gnus-dead-summary-mode
6821       (add-minor-mode
6822        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6823
6824 (defun gnus-deaden-summary ()
6825   "Make the current summary buffer into a dead summary buffer."
6826   ;; Kill any previous dead summary buffer.
6827   (when (and gnus-dead-summary
6828              (buffer-name gnus-dead-summary))
6829     (with-current-buffer gnus-dead-summary
6830       (when gnus-dead-summary-mode
6831         (kill-buffer (current-buffer)))))
6832   ;; Make this the current dead summary.
6833   (setq gnus-dead-summary (current-buffer))
6834   (gnus-dead-summary-mode 1)
6835   (let ((name (buffer-name)))
6836     (when (string-match "Summary" name)
6837       (rename-buffer
6838        (concat (substring name 0 (match-beginning 0)) "Dead "
6839                (substring name (match-beginning 0)))
6840        t)
6841       (bury-buffer))))
6842
6843 (defun gnus-kill-or-deaden-summary (buffer)
6844   "Kill or deaden the summary BUFFER."
6845   (save-excursion
6846     (when (and (buffer-name buffer)
6847                (not gnus-single-article-buffer))
6848       (with-current-buffer buffer
6849         (gnus-kill-buffer gnus-article-buffer)
6850         (gnus-kill-buffer gnus-original-article-buffer)))
6851     (cond
6852      ;; Kill the buffer.
6853      (gnus-kill-summary-on-exit
6854       (when (and gnus-use-trees
6855                  (gnus-buffer-exists-p buffer))
6856         (save-excursion
6857           (set-buffer buffer)
6858           (gnus-tree-close gnus-newsgroup-name)))
6859       (gnus-kill-buffer buffer))
6860      ;; Deaden the buffer.
6861      ((gnus-buffer-exists-p buffer)
6862       (save-excursion
6863         (set-buffer buffer)
6864         (gnus-deaden-summary))))))
6865
6866 (defun gnus-summary-wake-up-the-dead (&rest args)
6867   "Wake up the dead summary buffer."
6868   (interactive)
6869   (gnus-dead-summary-mode -1)
6870   (let ((name (buffer-name)))
6871     (when (string-match "Dead " name)
6872       (rename-buffer
6873        (concat (substring name 0 (match-beginning 0))
6874                (substring name (match-end 0)))
6875        t)))
6876   (gnus-message 3 "This dead summary is now alive again"))
6877
6878 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6879 (defun gnus-summary-fetch-faq (&optional faq-dir)
6880   "Fetch the FAQ for the current group.
6881 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6882 in."
6883   (interactive
6884    (list
6885     (when current-prefix-arg
6886       (completing-read
6887        "FAQ dir: " (and (listp gnus-group-faq-directory)
6888                         (mapcar 'list
6889                                 gnus-group-faq-directory))))))
6890   (let (gnus-faq-buffer)
6891     (when (setq gnus-faq-buffer
6892                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6893       (gnus-configure-windows 'summary-faq))))
6894
6895 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6896 (defun gnus-summary-describe-group (&optional force)
6897   "Describe the current newsgroup."
6898   (interactive "P")
6899   (gnus-group-describe-group force gnus-newsgroup-name))
6900
6901 (defun gnus-summary-describe-briefly ()
6902   "Describe summary mode commands briefly."
6903   (interactive)
6904   (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")))
6905
6906 ;; Walking around group mode buffer from summary mode.
6907
6908 (defun gnus-summary-next-group (&optional no-article target-group backward)
6909   "Exit current newsgroup and then select next unread newsgroup.
6910 If prefix argument NO-ARTICLE is non-nil, no article is selected
6911 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6912 previous group instead."
6913   (interactive "P")
6914   ;; Stop pre-fetching.
6915   (gnus-async-halt-prefetch)
6916   (let ((current-group gnus-newsgroup-name)
6917         (current-buffer (current-buffer))
6918         entered)
6919     ;; First we semi-exit this group to update Xrefs and all variables.
6920     ;; We can't do a real exit, because the window conf must remain
6921     ;; the same in case the user is prompted for info, and we don't
6922     ;; want the window conf to change before that...
6923     (gnus-summary-exit t)
6924     (while (not entered)
6925       ;; Then we find what group we are supposed to enter.
6926       (set-buffer gnus-group-buffer)
6927       (gnus-group-jump-to-group current-group)
6928       (setq target-group
6929             (or target-group
6930                 (if (eq gnus-keep-same-level 'best)
6931                     (gnus-summary-best-group gnus-newsgroup-name)
6932                   (gnus-summary-search-group backward gnus-keep-same-level))))
6933       (if (not target-group)
6934           ;; There are no further groups, so we return to the group
6935           ;; buffer.
6936           (progn
6937             (gnus-message 5 "Returning to the group buffer")
6938             (setq entered t)
6939             (when (gnus-buffer-live-p current-buffer)
6940               (set-buffer current-buffer)
6941               (gnus-summary-exit))
6942             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6943         ;; We try to enter the target group.
6944         (gnus-group-jump-to-group target-group)
6945         (let ((unreads (gnus-group-group-unread)))
6946           (if (and (or (eq t unreads)
6947                        (and unreads (not (zerop unreads))))
6948                    (gnus-summary-read-group
6949                     target-group nil no-article
6950                     (and (buffer-name current-buffer) current-buffer)
6951                     nil backward))
6952               (setq entered t)
6953             (setq current-group target-group
6954                   target-group nil)))))))
6955
6956 (defun gnus-summary-prev-group (&optional no-article)
6957   "Exit current newsgroup and then select previous unread newsgroup.
6958 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6959   (interactive "P")
6960   (gnus-summary-next-group no-article nil t))
6961
6962 ;; Walking around summary lines.
6963
6964 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6965   "Go to the first subject satisfying any non-nil constraint.
6966 If UNREAD is non-nil, the article should be unread.
6967 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6968 If UNSEEN is non-nil, the article should be unseen.
6969 Returns the article selected or nil if there are no matching articles."
6970   (interactive "P")
6971   (cond
6972    ;; Empty summary.
6973    ((null gnus-newsgroup-data)
6974     (gnus-message 3 "No articles in the group")
6975     nil)
6976    ;; Pick the first article.
6977    ((not (or unread undownloaded unseen))
6978     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6979     (gnus-data-number (car gnus-newsgroup-data)))
6980    ;; Find the first unread article.
6981    (t
6982     (let ((data gnus-newsgroup-data))
6983       (while (and data
6984                   (let ((num (gnus-data-number (car data))))
6985                     (or (memq num gnus-newsgroup-unfetched)
6986                         (not (or (and unread
6987                                       (memq num gnus-newsgroup-unreads))
6988                                  (and undownloaded
6989                                       (memq num gnus-newsgroup-undownloaded))
6990                                  (and unseen
6991                                       (memq num gnus-newsgroup-unseen)))))))
6992         (setq data (cdr data)))
6993       (prog1
6994           (if data
6995               (progn
6996                 (goto-char (gnus-data-pos (car data)))
6997                 (gnus-data-number (car data)))
6998             (gnus-message 3 "No more%s articles"
6999                           (let* ((r (when unread " unread"))
7000                                  (d (when undownloaded " undownloaded"))
7001                                  (s (when unseen " unseen"))
7002                                  (l (delq nil (list r d s))))
7003                             (cond ((= 3 (length l))
7004                                    (concat r "," d ", or" s))
7005                                   ((= 2 (length l))
7006                                    (concat (car l) ", or" (cadr l)))
7007                                   ((= 1 (length l))
7008                                    (car l))
7009                                   (t
7010                                    ""))))
7011             nil
7012             )
7013         (gnus-summary-position-point))))))
7014
7015 (defun gnus-summary-next-subject (n &optional unread dont-display)
7016   "Go to next N'th summary line.
7017 If N is negative, go to the previous N'th subject line.
7018 If UNREAD is non-nil, only unread articles are selected.
7019 The difference between N and the actual number of steps taken is
7020 returned."
7021   (interactive "p")
7022   (let ((backward (< n 0))
7023         (n (abs n)))
7024     (while (and (> n 0)
7025                 (if backward
7026                     (gnus-summary-find-prev unread)
7027                   (gnus-summary-find-next unread)))
7028       (unless (zerop (setq n (1- n)))
7029         (gnus-summary-show-thread)))
7030     (when (/= 0 n)
7031       (gnus-message 7 "No more%s articles"
7032                     (if unread " unread" "")))
7033     (unless dont-display
7034       (gnus-summary-recenter)
7035       (gnus-summary-position-point))
7036     n))
7037
7038 (defun gnus-summary-next-unread-subject (n)
7039   "Go to next N'th unread summary line."
7040   (interactive "p")
7041   (gnus-summary-next-subject n t))
7042
7043 (defun gnus-summary-prev-subject (n &optional unread)
7044   "Go to previous N'th summary line.
7045 If optional argument UNREAD is non-nil, only unread article is selected."
7046   (interactive "p")
7047   (gnus-summary-next-subject (- n) unread))
7048
7049 (defun gnus-summary-prev-unread-subject (n)
7050   "Go to previous N'th unread summary line."
7051   (interactive "p")
7052   (gnus-summary-next-subject (- n) t))
7053
7054 (defun gnus-summary-goto-subjects (articles)
7055   "Insert the subject header for ARTICLES in the current buffer."
7056   (save-excursion
7057     (dolist (article articles)
7058       (gnus-summary-goto-subject article t)))
7059   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7060   (gnus-summary-position-point))
7061  
7062 (defun gnus-summary-goto-subject (article &optional force silent)
7063   "Go the subject line of ARTICLE.
7064 If FORCE, also allow jumping to articles not currently shown."
7065   (interactive "nArticle number: ")
7066   (unless (numberp article)
7067     (error "Article %s is not a number" article))
7068   (let ((b (point))
7069         (data (gnus-data-find article)))
7070     ;; We read in the article if we have to.
7071     (and (not data)
7072          force
7073          (gnus-summary-insert-subject
7074           article
7075           (if (or (numberp force) (vectorp force)) force)
7076           t)
7077          (setq data (gnus-data-find article)))
7078     (goto-char b)
7079     (if (not data)
7080         (progn
7081           (unless silent
7082             (gnus-message 3 "Can't find article %d" article))
7083           nil)
7084       (let ((pt (gnus-data-pos data)))
7085         (goto-char pt)
7086         (gnus-summary-set-article-display-arrow pt))
7087       (gnus-summary-position-point)
7088       article)))
7089
7090 ;; Walking around summary lines with displaying articles.
7091
7092 (defun gnus-summary-expand-window (&optional arg)
7093   "Make the summary buffer take up the entire Emacs frame.
7094 Given a prefix, will force an `article' buffer configuration."
7095   (interactive "P")
7096   (if arg
7097       (gnus-configure-windows 'article 'force)
7098     (gnus-configure-windows 'summary 'force)))
7099
7100 (defun gnus-summary-display-article (article &optional all-header)
7101   "Display ARTICLE in article buffer."
7102   (when (gnus-buffer-live-p gnus-article-buffer)
7103     (with-current-buffer gnus-article-buffer
7104       (mm-enable-multibyte)))
7105   (gnus-set-global-variables)
7106   (when (gnus-buffer-live-p gnus-article-buffer)
7107     (with-current-buffer gnus-article-buffer
7108       (setq gnus-article-charset gnus-newsgroup-charset)
7109       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7110       (mm-enable-multibyte)))
7111   (if (null article)
7112       nil
7113     (prog1
7114         (if gnus-summary-display-article-function
7115             (funcall gnus-summary-display-article-function article all-header)
7116           (gnus-article-prepare article all-header))
7117       (gnus-run-hooks 'gnus-select-article-hook)
7118       (when (and gnus-current-article
7119                  (not (zerop gnus-current-article)))
7120         (gnus-summary-goto-subject gnus-current-article))
7121       (gnus-summary-recenter)
7122       (when (and gnus-use-trees gnus-show-threads)
7123         (gnus-possibly-generate-tree article)
7124         (gnus-highlight-selected-tree article))
7125       ;; Successfully display article.
7126       (gnus-article-set-window-start
7127        (cdr (assq article gnus-newsgroup-bookmarks))))))
7128
7129 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7130   "Select the current article.
7131 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7132 non-nil, the article will be re-fetched even if it already present in
7133 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7134 be displayed."
7135   ;; Make sure we are in the summary buffer to work around bbdb bug.
7136   (unless (eq major-mode 'gnus-summary-mode)
7137     (set-buffer gnus-summary-buffer))
7138   (let ((article (or article (gnus-summary-article-number)))
7139         (all-headers (not (not all-headers))) ;Must be t or nil.
7140         gnus-summary-display-article-function)
7141     (and (not pseudo)
7142          (gnus-summary-article-pseudo-p article)
7143          (error "This is a pseudo-article"))
7144     (save-excursion
7145       (set-buffer gnus-summary-buffer)
7146       (if (or (and gnus-single-article-buffer
7147                    (or (null gnus-current-article)
7148                        (null gnus-article-current)
7149                        (null (get-buffer gnus-article-buffer))
7150                        (not (eq article (cdr gnus-article-current)))
7151                        (not (equal (car gnus-article-current)
7152                                    gnus-newsgroup-name))))
7153               (and (not gnus-single-article-buffer)
7154                    (or (null gnus-current-article)
7155                        (not (eq gnus-current-article article))))
7156               force)
7157           ;; The requested article is different from the current article.
7158           (progn
7159             (gnus-summary-display-article article all-headers)
7160             (when (gnus-buffer-live-p gnus-article-buffer)
7161               (with-current-buffer gnus-article-buffer
7162                 (if (not gnus-article-decoded-p) ;; a local variable
7163                     (mm-disable-multibyte))))
7164             (gnus-article-set-window-start
7165              (cdr (assq article gnus-newsgroup-bookmarks)))
7166             article)
7167         'old))))
7168
7169 (defun gnus-summary-force-verify-and-decrypt ()
7170   "Display buttons for signed/encrypted parts and verify/decrypt them."
7171   (interactive)
7172   (let ((mm-verify-option 'known)
7173         (mm-decrypt-option 'known)
7174         (gnus-article-emulate-mime t)
7175         (mm-fill-flowed nil)
7176         (gnus-buttonized-mime-types (append (list "multipart/signed"
7177                                                   "multipart/encrypted")
7178                                             gnus-buttonized-mime-types)))
7179     (gnus-summary-select-article nil 'force)))
7180
7181 (defun gnus-summary-set-current-mark (&optional current-mark)
7182   "Obsolete function."
7183   nil)
7184
7185 (defun gnus-summary-next-article (&optional unread subject backward push)
7186   "Select the next article.
7187 If UNREAD, only unread articles are selected.
7188 If SUBJECT, only articles with SUBJECT are selected.
7189 If BACKWARD, the previous article is selected instead of the next."
7190   (interactive "P")
7191   (cond
7192    ;; Is there such an article?
7193    ((and (gnus-summary-search-forward unread subject backward)
7194          (or (gnus-summary-display-article (gnus-summary-article-number))
7195              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7196     (gnus-summary-position-point))
7197    ;; If not, we try the first unread, if that is wanted.
7198    ((and subject
7199          gnus-auto-select-same
7200          (gnus-summary-first-unread-article))
7201     (gnus-summary-position-point)
7202     (gnus-message 6 "Wrapped"))
7203    ;; Try to get next/previous article not displayed in this group.
7204    ((and gnus-auto-extend-newsgroup
7205          (not unread) (not subject))
7206     (gnus-summary-goto-article
7207      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7208      nil (count-lines (point-min) (point))))
7209    ;; Go to next/previous group.
7210    (t
7211     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7212       (gnus-summary-jump-to-group gnus-newsgroup-name))
7213     (let ((cmd last-command-char)
7214           (point
7215            (with-current-buffer gnus-group-buffer
7216              (point)))
7217           (group
7218            (if (eq gnus-keep-same-level 'best)
7219                (gnus-summary-best-group gnus-newsgroup-name)
7220              (gnus-summary-search-group backward gnus-keep-same-level))))
7221       ;; For some reason, the group window gets selected.  We change
7222       ;; it back.
7223       (select-window (get-buffer-window (current-buffer)))
7224       ;; Select next unread newsgroup automagically.
7225       (cond
7226        ((or (not gnus-auto-select-next)
7227             (not cmd))
7228         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7229        ((or (eq gnus-auto-select-next 'quietly)
7230             (and (eq gnus-auto-select-next 'slightly-quietly)
7231                  push)
7232             (and (eq gnus-auto-select-next 'almost-quietly)
7233                  (gnus-summary-last-article-p)))
7234         ;; Select quietly.
7235         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7236             (gnus-summary-exit)
7237           (gnus-message 7 "No more%s articles (%s)..."
7238                         (if unread " unread" "")
7239                         (if group (concat "selecting " group)
7240                           "exiting"))
7241           (gnus-summary-next-group nil group backward)))
7242        (t
7243         (when (gnus-key-press-event-p last-input-event)
7244           (gnus-summary-walk-group-buffer
7245            gnus-newsgroup-name cmd unread backward point))))))))
7246
7247 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7248   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7249                       (?\C-p (gnus-group-prev-unread-group 1))))
7250         (cursor-in-echo-area t)
7251         keve key group ended prompt)
7252     (save-excursion
7253       (set-buffer gnus-group-buffer)
7254       (goto-char start)
7255       (setq group
7256             (if (eq gnus-keep-same-level 'best)
7257                 (gnus-summary-best-group gnus-newsgroup-name)
7258               (gnus-summary-search-group backward gnus-keep-same-level))))
7259     (while (not ended)
7260       (setq prompt
7261             (format
7262              "No more%s articles%s " (if unread " unread" "")
7263              (if (and group
7264                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7265                  (format " (Type %s for %s [%s])"
7266                          (single-key-description cmd) group
7267                          (gnus-group-unread group))
7268                (format " (Type %s to exit %s)"
7269                        (single-key-description cmd)
7270                        gnus-newsgroup-name))))
7271       ;; Confirm auto selection.
7272       (setq key (car (setq keve (gnus-read-event-char prompt)))
7273             ended t)
7274       (cond
7275        ((assq key keystrokes)
7276         (let ((obuf (current-buffer)))
7277           (switch-to-buffer gnus-group-buffer)
7278           (when group
7279             (gnus-group-jump-to-group group))
7280           (eval (cadr (assq key keystrokes)))
7281           (setq group (gnus-group-group-name))
7282           (switch-to-buffer obuf))
7283         (setq ended nil))
7284        ((equal key cmd)
7285         (if (or (not group)
7286                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7287             (gnus-summary-exit)
7288           (gnus-summary-next-group nil group backward)))
7289        (t
7290         (push (cdr keve) unread-command-events))))))
7291
7292 (defun gnus-summary-next-unread-article ()
7293   "Select unread article after current one."
7294   (interactive)
7295   (gnus-summary-next-article
7296    (or (not (eq gnus-summary-goto-unread 'never))
7297        (gnus-summary-last-article-p (gnus-summary-article-number)))
7298    (and gnus-auto-select-same
7299         (gnus-summary-article-subject))))
7300
7301 (defun gnus-summary-prev-article (&optional unread subject)
7302   "Select the article after the current one.
7303 If UNREAD is non-nil, only unread articles are selected."
7304   (interactive "P")
7305   (gnus-summary-next-article unread subject t))
7306
7307 (defun gnus-summary-prev-unread-article ()
7308   "Select unread article before current one."
7309   (interactive)
7310   (gnus-summary-prev-article
7311    (or (not (eq gnus-summary-goto-unread 'never))
7312        (gnus-summary-first-article-p (gnus-summary-article-number)))
7313    (and gnus-auto-select-same
7314         (gnus-summary-article-subject))))
7315
7316 (defun gnus-summary-next-page (&optional lines circular stop)
7317   "Show next page of the selected article.
7318 If at the end of the current article, select the next article.
7319 LINES says how many lines should be scrolled up.
7320
7321 If CIRCULAR is non-nil, go to the start of the article instead of
7322 selecting the next article when reaching the end of the current
7323 article.
7324
7325 If STOP is non-nil, just stop when reaching the end of the message.
7326
7327 Also see the variable `gnus-article-skip-boring'."
7328   (interactive "P")
7329   (setq gnus-summary-buffer (current-buffer))
7330   (gnus-set-global-variables)
7331   (let ((article (gnus-summary-article-number))
7332         (article-window (get-buffer-window gnus-article-buffer t))
7333         endp)
7334     ;; If the buffer is empty, we have no article.
7335     (unless article
7336       (error "No article to select"))
7337     (gnus-configure-windows 'article)
7338     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7339         (if (and (eq gnus-summary-goto-unread 'never)
7340                  (not (gnus-summary-last-article-p article)))
7341             (gnus-summary-next-article)
7342           (gnus-summary-next-unread-article))
7343       (if (or (null gnus-current-article)
7344               (null gnus-article-current)
7345               (/= article (cdr gnus-article-current))
7346               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7347           ;; Selected subject is different from current article's.
7348           (gnus-summary-display-article article)
7349         (when article-window
7350           (gnus-eval-in-buffer-window gnus-article-buffer
7351             (setq endp (or (gnus-article-next-page lines)
7352                            (gnus-article-only-boring-p))))
7353           (when endp
7354             (cond (stop
7355                    (gnus-message 3 "End of message"))
7356                   (circular
7357                    (gnus-summary-beginning-of-article))
7358                   (lines
7359                    (gnus-message 3 "End of message"))
7360                   ((null lines)
7361                    (if (and (eq gnus-summary-goto-unread 'never)
7362                             (not (gnus-summary-last-article-p article)))
7363                        (gnus-summary-next-article)
7364                      (gnus-summary-next-unread-article))))))))
7365     (gnus-summary-recenter)
7366     (gnus-summary-position-point)))
7367
7368 (defun gnus-summary-prev-page (&optional lines move)
7369   "Show previous page of selected article.
7370 Argument LINES specifies lines to be scrolled down.
7371 If MOVE, move to the previous unread article if point is at
7372 the beginning of the buffer."
7373   (interactive "P")
7374   (let ((article (gnus-summary-article-number))
7375         (article-window (get-buffer-window gnus-article-buffer t))
7376         endp)
7377     (gnus-configure-windows 'article)
7378     (if (or (null gnus-current-article)
7379             (null gnus-article-current)
7380             (/= article (cdr gnus-article-current))
7381             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7382         ;; Selected subject is different from current article's.
7383         (gnus-summary-display-article article)
7384       (gnus-summary-recenter)
7385       (when article-window
7386         (gnus-eval-in-buffer-window gnus-article-buffer
7387           (setq endp (gnus-article-prev-page lines)))
7388         (when (and move endp)
7389           (cond (lines
7390                  (gnus-message 3 "Beginning of message"))
7391                 ((null lines)
7392                  (if (and (eq gnus-summary-goto-unread 'never)
7393                           (not (gnus-summary-first-article-p article)))
7394                      (gnus-summary-prev-article)
7395                    (gnus-summary-prev-unread-article))))))))
7396   (gnus-summary-position-point))
7397
7398 (defun gnus-summary-prev-page-or-article (&optional lines)
7399   "Show previous page of selected article.
7400 Argument LINES specifies lines to be scrolled down.
7401 If at the beginning of the article, go to the next article."
7402   (interactive "P")
7403   (gnus-summary-prev-page lines t))
7404
7405 (defun gnus-summary-scroll-up (lines)
7406   "Scroll up (or down) one line current article.
7407 Argument LINES specifies lines to be scrolled up (or down if negative)."
7408   (interactive "p")
7409   (gnus-configure-windows 'article)
7410   (gnus-summary-show-thread)
7411   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7412     (gnus-eval-in-buffer-window gnus-article-buffer
7413       (cond ((> lines 0)
7414              (when (gnus-article-next-page lines)
7415                (gnus-message 3 "End of message")))
7416             ((< lines 0)
7417              (gnus-article-prev-page (- lines))))))
7418   (gnus-summary-recenter)
7419   (gnus-summary-position-point))
7420
7421 (defun gnus-summary-scroll-down (lines)
7422   "Scroll down (or up) one line current article.
7423 Argument LINES specifies lines to be scrolled down (or up if negative)."
7424   (interactive "p")
7425   (gnus-summary-scroll-up (- lines)))
7426
7427 (defun gnus-summary-next-same-subject ()
7428   "Select next article which has the same subject as current one."
7429   (interactive)
7430   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7431
7432 (defun gnus-summary-prev-same-subject ()
7433   "Select previous article which has the same subject as current one."
7434   (interactive)
7435   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7436
7437 (defun gnus-summary-next-unread-same-subject ()
7438   "Select next unread article which has the same subject as current one."
7439   (interactive)
7440   (gnus-summary-next-article t (gnus-summary-article-subject)))
7441
7442 (defun gnus-summary-prev-unread-same-subject ()
7443   "Select previous unread article which has the same subject as current one."
7444   (interactive)
7445   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7446
7447 (defun gnus-summary-first-unread-article ()
7448   "Select the first unread article.
7449 Return nil if there are no unread articles."
7450   (interactive)
7451   (prog1
7452       (when (gnus-summary-first-subject t)
7453         (gnus-summary-show-thread)
7454         (gnus-summary-first-subject t)
7455         (gnus-summary-display-article (gnus-summary-article-number)))
7456     (gnus-summary-position-point)))
7457
7458 (defun gnus-summary-first-unread-subject ()
7459   "Place the point on the subject line of the first unread article.
7460 Return nil if there are no unread articles."
7461   (interactive)
7462   (prog1
7463       (when (gnus-summary-first-subject t)
7464         (gnus-summary-show-thread)
7465         (gnus-summary-first-subject t))
7466     (gnus-summary-position-point)))
7467
7468 (defun gnus-summary-first-unseen-subject ()
7469   "Place the point on the subject line of the first unseen article.
7470 Return nil if there are no unseen articles."
7471   (interactive)
7472   (prog1
7473       (when (gnus-summary-first-subject nil nil t)
7474         (gnus-summary-show-thread)
7475         (gnus-summary-first-subject nil nil t))
7476     (gnus-summary-position-point)))
7477
7478 (defun gnus-summary-first-unseen-or-unread-subject ()
7479   "Place the point on the subject line of the first unseen article or,
7480 if all article have been seen, on the subject line of the first unread
7481 article."
7482   (interactive)
7483   (prog1
7484       (unless (when (gnus-summary-first-subject nil nil t)
7485                 (gnus-summary-show-thread)
7486                 (gnus-summary-first-subject nil nil t))
7487         (when (gnus-summary-first-subject t)
7488           (gnus-summary-show-thread)
7489           (gnus-summary-first-subject t)))
7490     (gnus-summary-position-point)))
7491
7492 (defun gnus-summary-first-article ()
7493   "Select the first article.
7494 Return nil if there are no articles."
7495   (interactive)
7496   (prog1
7497       (when (gnus-summary-first-subject)
7498         (gnus-summary-show-thread)
7499         (gnus-summary-first-subject)
7500         (gnus-summary-display-article (gnus-summary-article-number)))
7501     (gnus-summary-position-point)))
7502
7503 (defun gnus-summary-best-unread-article (&optional arg)
7504   "Select the unread article with the highest score.
7505 If given a prefix argument, select the next unread article that has a
7506 score higher than the default score."
7507   (interactive "P")
7508   (let ((article (if arg
7509                      (gnus-summary-better-unread-subject)
7510                    (gnus-summary-best-unread-subject))))
7511     (if article
7512         (gnus-summary-goto-article article)
7513       (error "No unread articles"))))
7514
7515 (defun gnus-summary-best-unread-subject ()
7516   "Select the unread subject with the highest score."
7517   (interactive)
7518   (let ((best -1000000)
7519         (data gnus-newsgroup-data)
7520         article score)
7521     (while data
7522       (and (gnus-data-unread-p (car data))
7523            (> (setq score
7524                     (gnus-summary-article-score (gnus-data-number (car data))))
7525               best)
7526            (setq best score
7527                  article (gnus-data-number (car data))))
7528       (setq data (cdr data)))
7529     (when article
7530       (gnus-summary-goto-subject article))
7531     (gnus-summary-position-point)
7532     article))
7533
7534 (defun gnus-summary-better-unread-subject ()
7535   "Select the first unread subject that has a score over the default score."
7536   (interactive)
7537   (let ((data gnus-newsgroup-data)
7538         article score)
7539     (while (and (setq article (gnus-data-number (car data)))
7540                 (or (gnus-data-read-p (car data))
7541                     (not (> (gnus-summary-article-score article)
7542                             gnus-summary-default-score))))
7543       (setq data (cdr data)))
7544     (when article
7545       (gnus-summary-goto-subject article))
7546     (gnus-summary-position-point)
7547     article))
7548
7549 (defun gnus-summary-last-subject ()
7550   "Go to the last displayed subject line in the group."
7551   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7552     (when article
7553       (gnus-summary-goto-subject article))))
7554
7555 (defun gnus-summary-goto-article (article &optional all-headers force)
7556   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7557 If ALL-HEADERS is non-nil, no header lines are hidden.
7558 If FORCE, go to the article even if it isn't displayed.  If FORCE
7559 is a number, it is the line the article is to be displayed on."
7560   (interactive
7561    (list
7562     (completing-read
7563      "Article number or Message-ID: "
7564      (mapcar (lambda (number) (list (int-to-string number)))
7565              gnus-newsgroup-limit))
7566     current-prefix-arg
7567     t))
7568   (prog1
7569       (if (and (stringp article)
7570                (string-match "@\\|%40" article))
7571           (gnus-summary-refer-article article)
7572         (when (stringp article)
7573           (setq article (string-to-number article)))
7574         (if (gnus-summary-goto-subject article force)
7575             (gnus-summary-display-article article all-headers)
7576           (gnus-message 4 "Couldn't go to article %s" article) nil))
7577     (gnus-summary-position-point)))
7578
7579 (defun gnus-summary-goto-last-article ()
7580   "Go to the previously read article."
7581   (interactive)
7582   (prog1
7583       (when gnus-last-article
7584         (gnus-summary-goto-article gnus-last-article nil t))
7585     (gnus-summary-position-point)))
7586
7587 (defun gnus-summary-pop-article (number)
7588   "Pop one article off the history and go to the previous.
7589 NUMBER articles will be popped off."
7590   (interactive "p")
7591   (let (to)
7592     (setq gnus-newsgroup-history
7593           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7594     (if to
7595         (gnus-summary-goto-article (car to) nil t)
7596       (error "Article history empty")))
7597   (gnus-summary-position-point))
7598
7599 ;; Summary commands and functions for limiting the summary buffer.
7600
7601 (defun gnus-summary-limit-to-articles (n)
7602   "Limit the summary buffer to the next N articles.
7603 If not given a prefix, use the process marked articles instead."
7604   (interactive "P")
7605   (prog1
7606       (let ((articles (gnus-summary-work-articles n)))
7607         (setq gnus-newsgroup-processable nil)
7608         (gnus-summary-limit articles))
7609     (gnus-summary-position-point)))
7610
7611 (defun gnus-summary-pop-limit (&optional total)
7612   "Restore the previous limit.
7613 If given a prefix, remove all limits."
7614   (interactive "P")
7615   (when total
7616     (setq gnus-newsgroup-limits
7617           (list (mapcar (lambda (h) (mail-header-number h))
7618                         gnus-newsgroup-headers))))
7619   (unless gnus-newsgroup-limits
7620     (error "No limit to pop"))
7621   (prog1
7622       (gnus-summary-limit nil 'pop)
7623     (gnus-summary-position-point)))
7624
7625 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7626   "Limit the summary buffer to articles that have subjects that match a regexp.
7627 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7628   (interactive
7629    (list (read-string (if current-prefix-arg
7630                           "Exclude subject (regexp): "
7631                         "Limit to subject (regexp): "))
7632          nil current-prefix-arg))
7633   (unless header
7634     (setq header "subject"))
7635   (when (not (equal "" subject))
7636     (prog1
7637         (let ((articles (gnus-summary-find-matching
7638                          (or header "subject") subject 'all nil nil
7639                          not-matching)))
7640           (unless articles
7641             (error "Found no matches for \"%s\"" subject))
7642           (gnus-summary-limit articles))
7643       (gnus-summary-position-point))))
7644
7645 (defun gnus-summary-limit-to-author (from &optional not-matching)
7646   "Limit the summary buffer to articles that have authors that match a regexp.
7647 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7648   (interactive
7649    (list (read-string (if current-prefix-arg
7650                           "Exclude author (regexp): "
7651                         "Limit to author (regexp): "))
7652          current-prefix-arg))
7653   (gnus-summary-limit-to-subject from "from" not-matching))
7654
7655 (defun gnus-summary-limit-to-age (age &optional younger-p)
7656   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7657 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7658 articles that are younger than AGE days."
7659   (interactive
7660    (let ((younger current-prefix-arg)
7661          (days-got nil)
7662          days)
7663      (while (not days-got)
7664        (setq days (if younger
7665                       (read-string "Limit to articles younger than (in days, older when negative): ")
7666                     (read-string
7667                      "Limit to articles older than (in days, younger when negative): ")))
7668        (when (> (length days) 0)
7669          (setq days (read days)))
7670        (if (numberp days)
7671            (progn
7672              (setq days-got t)
7673              (when (< days 0)
7674                (setq younger (not younger))
7675                (setq days (* days -1))))
7676          (message "Please enter a number.")
7677          (sleep-for 1)))
7678      (list days younger)))
7679   (prog1
7680       (let ((data gnus-newsgroup-data)
7681             (cutoff (days-to-time age))
7682             articles d date is-younger)
7683         (while (setq d (pop data))
7684           (when (and (vectorp (gnus-data-header d))
7685                      (setq date (mail-header-date (gnus-data-header d))))
7686             (setq is-younger (time-less-p
7687                               (time-since (condition-case ()
7688                                               (date-to-time date)
7689                                             (error '(0 0))))
7690                               cutoff))
7691             (when (if younger-p
7692                       is-younger
7693                     (not is-younger))
7694               (push (gnus-data-number d) articles))))
7695         (gnus-summary-limit (nreverse articles)))
7696     (gnus-summary-position-point)))
7697
7698 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7699   "Limit the summary buffer to articles that match an 'extra' header."
7700   (interactive
7701    (let ((header
7702           (intern
7703            (gnus-completing-read-with-default
7704             (symbol-name (car gnus-extra-headers))
7705             (if current-prefix-arg
7706                 "Exclude extra header:"
7707               "Limit extra header:")
7708             (mapcar (lambda (x)
7709                       (cons (symbol-name x) x))
7710                     gnus-extra-headers)
7711             nil
7712             t))))
7713      (list header
7714            (read-string (format "%s header %s (regexp): "
7715                                 (if current-prefix-arg "Exclude" "Limit to")
7716                                 header))
7717            current-prefix-arg)))
7718   (when (not (equal "" regexp))
7719     (prog1
7720         (let ((articles (gnus-summary-find-matching
7721                          (cons 'extra header) regexp 'all nil nil
7722                          not-matching)))
7723           (unless articles
7724             (error "Found no matches for \"%s\"" regexp))
7725           (gnus-summary-limit articles))
7726       (gnus-summary-position-point))))
7727
7728 (defun gnus-summary-limit-to-display-predicate ()
7729   "Limit the summary buffer to the predicated in the `display' group parameter."
7730   (interactive)
7731   (unless gnus-newsgroup-display
7732     (error "There is no `display' group parameter"))
7733   (let (articles)
7734     (dolist (number gnus-newsgroup-articles)
7735       (when (funcall gnus-newsgroup-display)
7736         (push number articles)))
7737     (gnus-summary-limit articles))
7738   (gnus-summary-position-point))
7739
7740 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7741 (make-obsolete
7742  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7743
7744 (defun gnus-summary-limit-to-unread (&optional all)
7745   "Limit the summary buffer to articles that are not marked as read.
7746 If ALL is non-nil, limit strictly to unread articles."
7747   (interactive "P")
7748   (if all
7749       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7750     (gnus-summary-limit-to-marks
7751      ;; Concat all the marks that say that an article is read and have
7752      ;; those removed.
7753      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7754            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7755            gnus-low-score-mark gnus-expirable-mark
7756            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7757            gnus-duplicate-mark gnus-souped-mark)
7758      'reverse)))
7759
7760 (defun gnus-summary-limit-to-replied (&optional unreplied)
7761   "Limit the summary buffer to replied articles.
7762 If UNREPLIED (the prefix), limit to unreplied articles."
7763   (interactive "P")
7764   (if unreplied
7765       (gnus-summary-limit
7766        (gnus-set-difference gnus-newsgroup-articles
7767         gnus-newsgroup-replied))
7768     (gnus-summary-limit gnus-newsgroup-replied))
7769   (gnus-summary-position-point))
7770
7771 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7772 (make-obsolete 'gnus-summary-delete-marked-with
7773                'gnus-summary-limit-exclude-marks)
7774
7775 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7776   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7777 If REVERSE, limit the summary buffer to articles that are marked
7778 with MARKS.  MARKS can either be a string of marks or a list of marks.
7779 Returns how many articles were removed."
7780   (interactive "sMarks: ")
7781   (gnus-summary-limit-to-marks marks t))
7782
7783 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7784   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7785 If REVERSE (the prefix), limit the summary buffer to articles that are
7786 not marked with MARKS.  MARKS can either be a string of marks or a
7787 list of marks.
7788 Returns how many articles were removed."
7789   (interactive "sMarks: \nP")
7790   (prog1
7791       (let ((data gnus-newsgroup-data)
7792             (marks (if (listp marks) marks
7793                      (append marks nil))) ; Transform to list.
7794             articles)
7795         (while data
7796           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7797                   (memq (gnus-data-mark (car data)) marks))
7798             (push (gnus-data-number (car data)) articles))
7799           (setq data (cdr data)))
7800         (gnus-summary-limit articles))
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-limit-to-score (score)
7804   "Limit to articles with score at or above SCORE."
7805   (interactive "NLimit to articles with score of at least: ")
7806   (let ((data gnus-newsgroup-data)
7807         articles)
7808     (while data
7809       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7810                 score)
7811         (push (gnus-data-number (car data)) articles))
7812       (setq data (cdr data)))
7813     (prog1
7814         (gnus-summary-limit articles)
7815       (gnus-summary-position-point))))
7816
7817 (defun gnus-summary-limit-to-unseen ()
7818   "Limit to unseen articles."
7819   (interactive)
7820   (prog1
7821       (gnus-summary-limit gnus-newsgroup-unseen)
7822     (gnus-summary-position-point)))
7823
7824 (defun gnus-summary-limit-include-thread (id)
7825   "Display all the hidden articles that is in the thread with ID in it.
7826 When called interactively, ID is the Message-ID of the current
7827 article."
7828   (interactive (list (mail-header-id (gnus-summary-article-header))))
7829   (let ((articles (gnus-articles-in-thread
7830                    (gnus-id-to-thread (gnus-root-id id)))))
7831     (prog1
7832         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7833       (gnus-summary-limit-include-matching-articles
7834        "subject"
7835        (regexp-quote (gnus-simplify-subject-re
7836                       (mail-header-subject (gnus-id-to-header id)))))
7837       (gnus-summary-position-point))))
7838
7839 (defun gnus-summary-limit-include-matching-articles (header regexp)
7840   "Display all the hidden articles that have HEADERs that match REGEXP."
7841   (interactive (list (read-string "Match on header: ")
7842                      (read-string "Regexp: ")))
7843   (let ((articles (gnus-find-matching-articles header regexp)))
7844     (prog1
7845         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7846       (gnus-summary-position-point))))
7847
7848 (defun gnus-summary-insert-dormant-articles ()
7849   "Insert all the dormant articles for this group into the current buffer."
7850   (interactive)
7851   (let ((gnus-verbose (max 6 gnus-verbose)))
7852     (if (not gnus-newsgroup-dormant)
7853         (gnus-message 3 "No cached articles for this group")
7854       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7855
7856 (defun gnus-summary-limit-include-dormant ()
7857   "Display all the hidden articles that are marked as dormant.
7858 Note that this command only works on a subset of the articles currently
7859 fetched for this group."
7860   (interactive)
7861   (unless gnus-newsgroup-dormant
7862     (error "There are no dormant articles in this group"))
7863   (prog1
7864       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7865     (gnus-summary-position-point)))
7866
7867 (defun gnus-summary-limit-exclude-dormant ()
7868   "Hide all dormant articles."
7869   (interactive)
7870   (prog1
7871       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7872     (gnus-summary-position-point)))
7873
7874 (defun gnus-summary-limit-exclude-childless-dormant ()
7875   "Hide all dormant articles that have no children."
7876   (interactive)
7877   (let ((data (gnus-data-list t))
7878         articles d children)
7879     ;; Find all articles that are either not dormant or have
7880     ;; children.
7881     (while (setq d (pop data))
7882       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7883                 (and (setq children
7884                            (gnus-article-children (gnus-data-number d)))
7885                      (let (found)
7886                        (while children
7887                          (when (memq (car children) articles)
7888                            (setq children nil
7889                                  found t))
7890                          (pop children))
7891                        found)))
7892         (push (gnus-data-number d) articles)))
7893     ;; Do the limiting.
7894     (prog1
7895         (gnus-summary-limit articles)
7896       (gnus-summary-position-point))))
7897
7898 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7899   "Mark all unread excluded articles as read.
7900 If ALL, mark even excluded ticked and dormants as read."
7901   (interactive "P")
7902   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7903   (let ((articles (gnus-sorted-ndifference
7904                    (sort
7905                     (mapcar (lambda (h) (mail-header-number h))
7906                             gnus-newsgroup-headers)
7907                     '<)
7908                    gnus-newsgroup-limit))
7909         article)
7910     (setq gnus-newsgroup-unreads
7911           (gnus-sorted-intersection gnus-newsgroup-unreads
7912                                     gnus-newsgroup-limit))
7913     (if all
7914         (setq gnus-newsgroup-dormant nil
7915               gnus-newsgroup-marked nil
7916               gnus-newsgroup-reads
7917               (nconc
7918                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7919                gnus-newsgroup-reads))
7920       (while (setq article (pop articles))
7921         (unless (or (memq article gnus-newsgroup-dormant)
7922                     (memq article gnus-newsgroup-marked))
7923           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7924
7925 (defun gnus-summary-limit (articles &optional pop)
7926   (if pop
7927       ;; We pop the previous limit off the stack and use that.
7928       (setq articles (car gnus-newsgroup-limits)
7929             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7930     ;; We use the new limit, so we push the old limit on the stack.
7931     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7932   ;; Set the limit.
7933   (setq gnus-newsgroup-limit articles)
7934   (let ((total (length gnus-newsgroup-data))
7935         (data (gnus-data-find-list (gnus-summary-article-number)))
7936         (gnus-summary-mark-below nil)   ; Inhibit this.
7937         found)
7938     ;; This will do all the work of generating the new summary buffer
7939     ;; according to the new limit.
7940     (gnus-summary-prepare)
7941     ;; Hide any threads, possibly.
7942     (gnus-summary-maybe-hide-threads)
7943     ;; Try to return to the article you were at, or one in the
7944     ;; neighborhood.
7945     (when data
7946       ;; We try to find some article after the current one.
7947       (while data
7948         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7949           (setq data nil
7950                 found t))
7951         (setq data (cdr data))))
7952     (unless found
7953       ;; If there is no data, that means that we were after the last
7954       ;; article.  The same goes when we can't find any articles
7955       ;; after the current one.
7956       (goto-char (point-max))
7957       (gnus-summary-find-prev))
7958     (gnus-set-mode-line 'summary)
7959     ;; We return how many articles were removed from the summary
7960     ;; buffer as a result of the new limit.
7961     (- total (length gnus-newsgroup-data))))
7962
7963 (defsubst gnus-invisible-cut-children (threads)
7964   (let ((num 0))
7965     (while threads
7966       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7967         (incf num))
7968       (pop threads))
7969     (< num 2)))
7970
7971 (defsubst gnus-cut-thread (thread)
7972   "Go forwards in the thread until we find an article that we want to display."
7973   (when (or (eq gnus-fetch-old-headers 'some)
7974             (eq gnus-fetch-old-headers 'invisible)
7975             (numberp gnus-fetch-old-headers)
7976             (eq gnus-build-sparse-threads 'some)
7977             (eq gnus-build-sparse-threads 'more))
7978     ;; Deal with old-fetched headers and sparse threads.
7979     (while (and
7980             thread
7981             (or
7982              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7983              (gnus-summary-article-ancient-p
7984               (mail-header-number (car thread))))
7985             (if (or (<= (length (cdr thread)) 1)
7986                     (eq gnus-fetch-old-headers 'invisible))
7987                 (setq gnus-newsgroup-limit
7988                       (delq (mail-header-number (car thread))
7989                             gnus-newsgroup-limit)
7990                       thread (cadr thread))
7991               (when (gnus-invisible-cut-children (cdr thread))
7992                 (let ((th (cdr thread)))
7993                   (while th
7994                     (if (memq (mail-header-number (caar th))
7995                               gnus-newsgroup-limit)
7996                         (setq thread (car th)
7997                               th nil)
7998                       (setq th (cdr th))))))))))
7999   thread)
8000
8001 (defun gnus-cut-threads (threads)
8002   "Cut off all uninteresting articles from the beginning of THREADS."
8003   (when (or (eq gnus-fetch-old-headers 'some)
8004             (eq gnus-fetch-old-headers 'invisible)
8005             (numberp gnus-fetch-old-headers)
8006             (eq gnus-build-sparse-threads 'some)
8007             (eq gnus-build-sparse-threads 'more))
8008     (let ((th threads))
8009       (while th
8010         (setcar th (gnus-cut-thread (car th)))
8011         (setq th (cdr th)))))
8012   ;; Remove nixed out threads.
8013   (delq nil threads))
8014
8015 (defun gnus-summary-initial-limit (&optional show-if-empty)
8016   "Figure out what the initial limit is supposed to be on group entry.
8017 This entails weeding out unwanted dormants, low-scored articles,
8018 fetch-old-headers verbiage, and so on."
8019   ;; Most groups have nothing to remove.
8020   (if (or gnus-inhibit-limiting
8021           (and (null gnus-newsgroup-dormant)
8022                (eq gnus-newsgroup-display 'gnus-not-ignore)
8023                (not (eq gnus-fetch-old-headers 'some))
8024                (not (numberp gnus-fetch-old-headers))
8025                (not (eq gnus-fetch-old-headers 'invisible))
8026                (null gnus-summary-expunge-below)
8027                (not (eq gnus-build-sparse-threads 'some))
8028                (not (eq gnus-build-sparse-threads 'more))
8029                (null gnus-thread-expunge-below)
8030                (not gnus-use-nocem)))
8031       ()                                ; Do nothing.
8032     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8033     (setq gnus-newsgroup-limit nil)
8034     (mapatoms
8035      (lambda (node)
8036        (unless (car (symbol-value node))
8037          ;; These threads have no parents -- they are roots.
8038          (let ((nodes (cdr (symbol-value node)))
8039                thread)
8040            (while nodes
8041              (if (and gnus-thread-expunge-below
8042                       (< (gnus-thread-total-score (car nodes))
8043                          gnus-thread-expunge-below))
8044                  (gnus-expunge-thread (pop nodes))
8045                (setq thread (pop nodes))
8046                (gnus-summary-limit-children thread))))))
8047      gnus-newsgroup-dependencies)
8048     ;; If this limitation resulted in an empty group, we might
8049     ;; pop the previous limit and use it instead.
8050     (when (and (not gnus-newsgroup-limit)
8051                show-if-empty)
8052       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8053     gnus-newsgroup-limit))
8054
8055 (defun gnus-summary-limit-children (thread)
8056   "Return 1 if this subthread is visible and 0 if it is not."
8057   ;; First we get the number of visible children to this thread.  This
8058   ;; is done by recursing down the thread using this function, so this
8059   ;; will really go down to a leaf article first, before slowly
8060   ;; working its way up towards the root.
8061   (when thread
8062     (let* ((max-lisp-eval-depth 5000)
8063            (children
8064            (if (cdr thread)
8065                (apply '+ (mapcar 'gnus-summary-limit-children
8066                                  (cdr thread)))
8067              0))
8068           (number (mail-header-number (car thread)))
8069           score)
8070       (if (and
8071            (not (memq number gnus-newsgroup-marked))
8072            (or
8073             ;; If this article is dormant and has absolutely no visible
8074             ;; children, then this article isn't visible.
8075             (and (memq number gnus-newsgroup-dormant)
8076                  (zerop children))
8077             ;; If this is "fetch-old-headered" and there is no
8078             ;; visible children, then we don't want this article.
8079             (and (or (eq gnus-fetch-old-headers 'some)
8080                      (numberp gnus-fetch-old-headers))
8081                  (gnus-summary-article-ancient-p number)
8082                  (zerop children))
8083             ;; If this is "fetch-old-headered" and `invisible', then
8084             ;; we don't want this article.
8085             (and (eq gnus-fetch-old-headers 'invisible)
8086                  (gnus-summary-article-ancient-p number))
8087             ;; If this is a sparsely inserted article with no children,
8088             ;; we don't want it.
8089             (and (eq gnus-build-sparse-threads 'some)
8090                  (gnus-summary-article-sparse-p number)
8091                  (zerop children))
8092             ;; If we use expunging, and this article is really
8093             ;; low-scored, then we don't want this article.
8094             (when (and gnus-summary-expunge-below
8095                        (< (setq score
8096                                 (or (cdr (assq number gnus-newsgroup-scored))
8097                                     gnus-summary-default-score))
8098                           gnus-summary-expunge-below))
8099               ;; We increase the expunge-tally here, but that has
8100               ;; nothing to do with the limits, really.
8101               (incf gnus-newsgroup-expunged-tally)
8102               ;; We also mark as read here, if that's wanted.
8103               (when (and gnus-summary-mark-below
8104                          (< score gnus-summary-mark-below))
8105                 (setq gnus-newsgroup-unreads
8106                       (delq number gnus-newsgroup-unreads))
8107                 (if gnus-newsgroup-auto-expire
8108                     (push number gnus-newsgroup-expirable)
8109                   (push (cons number gnus-low-score-mark)
8110                         gnus-newsgroup-reads)))
8111               t)
8112             ;; Do the `display' group parameter.
8113             (and gnus-newsgroup-display
8114                  (not (funcall gnus-newsgroup-display)))
8115             ;; Check NoCeM things.
8116             (when (and gnus-use-nocem
8117                        (gnus-nocem-unwanted-article-p
8118                         (mail-header-id (car thread))))
8119               (setq gnus-newsgroup-unreads
8120                     (delq number gnus-newsgroup-unreads))
8121               t)))
8122           ;; Nope, invisible article.
8123           0
8124         ;; Ok, this article is to be visible, so we add it to the limit
8125         ;; and return 1.
8126         (push number gnus-newsgroup-limit)
8127         1))))
8128
8129 (defun gnus-expunge-thread (thread)
8130   "Mark all articles in THREAD as read."
8131   (let* ((number (mail-header-number (car thread))))
8132     (incf gnus-newsgroup-expunged-tally)
8133     ;; We also mark as read here, if that's wanted.
8134     (setq gnus-newsgroup-unreads
8135           (delq number gnus-newsgroup-unreads))
8136     (if gnus-newsgroup-auto-expire
8137         (push number gnus-newsgroup-expirable)
8138       (push (cons number gnus-low-score-mark)
8139             gnus-newsgroup-reads)))
8140   ;; Go recursively through all subthreads.
8141   (mapcar 'gnus-expunge-thread (cdr thread)))
8142
8143 ;; Summary article oriented commands
8144
8145 (defun gnus-summary-refer-parent-article (n)
8146   "Refer parent article N times.
8147 If N is negative, go to ancestor -N instead.
8148 The difference between N and the number of articles fetched is returned."
8149   (interactive "p")
8150   (let ((skip 1)
8151         error header ref)
8152     (when (not (natnump n))
8153       (setq skip (abs n)
8154             n 1))
8155     (while (and (> n 0)
8156                 (not error))
8157       (setq header (gnus-summary-article-header))
8158       (if (and (eq (mail-header-number header)
8159                    (cdr gnus-article-current))
8160                (equal gnus-newsgroup-name
8161                       (car gnus-article-current)))
8162           ;; If we try to find the parent of the currently
8163           ;; displayed article, then we take a look at the actual
8164           ;; References header, since this is slightly more
8165           ;; reliable than the References field we got from the
8166           ;; server.
8167           (save-excursion
8168             (set-buffer gnus-original-article-buffer)
8169             (nnheader-narrow-to-headers)
8170             (unless (setq ref (message-fetch-field "references"))
8171               (when (setq ref (message-fetch-field "in-reply-to"))
8172                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8173             (widen))
8174         (setq ref
8175               ;; It's not the current article, so we take a bet on
8176               ;; the value we got from the server.
8177               (mail-header-references header)))
8178       (if (and ref
8179                (not (equal ref "")))
8180           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8181             (gnus-message 1 "Couldn't find parent"))
8182         (gnus-message 1 "No references in article %d"
8183                       (gnus-summary-article-number))
8184         (setq error t))
8185       (decf n))
8186     (gnus-summary-position-point)
8187     n))
8188
8189 (defun gnus-summary-refer-references ()
8190   "Fetch all articles mentioned in the References header.
8191 Return the number of articles fetched."
8192   (interactive)
8193   (let ((ref (mail-header-references (gnus-summary-article-header)))
8194         (current (gnus-summary-article-number))
8195         (n 0))
8196     (if (or (not ref)
8197             (equal ref ""))
8198         (error "No References in the current article")
8199       ;; For each Message-ID in the References header...
8200       (while (string-match "<[^>]*>" ref)
8201         (incf n)
8202         ;; ... fetch that article.
8203         (gnus-summary-refer-article
8204          (prog1 (match-string 0 ref)
8205            (setq ref (substring ref (match-end 0))))))
8206       (gnus-summary-goto-subject current)
8207       (gnus-summary-position-point)
8208       n)))
8209
8210 (defun gnus-summary-refer-thread (&optional limit)
8211   "Fetch all articles in the current thread.
8212 If LIMIT (the numerical prefix), fetch that many old headers instead
8213 of what's specified by the `gnus-refer-thread-limit' variable."
8214   (interactive "P")
8215   (let ((id (mail-header-id (gnus-summary-article-header)))
8216         (limit (if limit (prefix-numeric-value limit)
8217                  gnus-refer-thread-limit)))
8218     (unless (eq gnus-fetch-old-headers 'invisible)
8219       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8220       ;; Retrieve the headers and read them in.
8221       (if (eq (if (numberp limit)
8222                   (gnus-retrieve-headers
8223                    (list (min
8224                           (+ (mail-header-number
8225                               (gnus-summary-article-header))
8226                              limit)
8227                           gnus-newsgroup-end))
8228                    gnus-newsgroup-name (* limit 2))
8229                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8230                 ;; headers.
8231                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8232                                        gnus-newsgroup-name limit))
8233               'nov)
8234           (gnus-build-all-threads)
8235         (error "Can't fetch thread from back ends that don't support NOV"))
8236       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8237     (gnus-summary-limit-include-thread id)))
8238
8239 (defun gnus-summary-refer-article (message-id)
8240   "Fetch an article specified by MESSAGE-ID."
8241   (interactive "sMessage-ID: ")
8242   (when (and (stringp message-id)
8243              (not (zerop (length message-id))))
8244     (setq message-id (gnus-replace-in-string message-id " " ""))
8245     ;; Construct the correct Message-ID if necessary.
8246     ;; Suggested by tale@pawl.rpi.edu.
8247     (unless (string-match "^<" message-id)
8248       (setq message-id (concat "<" message-id)))
8249     (unless (string-match ">$" message-id)
8250       (setq message-id (concat message-id ">")))
8251     ;; People often post MIDs from URLs, so unhex it:
8252     (unless (string-match "@" message-id)
8253       (setq message-id (gnus-url-unhex-string message-id)))
8254     (let* ((header (gnus-id-to-header message-id))
8255            (sparse (and header
8256                         (gnus-summary-article-sparse-p
8257                          (mail-header-number header))
8258                         (memq (mail-header-number header)
8259                               gnus-newsgroup-limit)))
8260            number)
8261       (cond
8262        ;; If the article is present in the buffer we just go to it.
8263        ((and header
8264              (or (not (gnus-summary-article-sparse-p
8265                        (mail-header-number header)))
8266                  sparse))
8267         (prog1
8268             (gnus-summary-goto-article
8269              (mail-header-number header) nil t)
8270           (when sparse
8271             (gnus-summary-update-article (mail-header-number header)))))
8272        (t
8273         ;; We fetch the article.
8274         (catch 'found
8275           (dolist (gnus-override-method (gnus-refer-article-methods))
8276             (when (and (gnus-check-server gnus-override-method)
8277                        ;; Fetch the header,
8278                        (setq number (gnus-summary-insert-subject message-id)))
8279               ;; and display the article.
8280               (gnus-summary-select-article nil nil nil number)
8281               (throw 'found t)))
8282           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8283
8284 (defun gnus-refer-article-methods ()
8285   "Return a list of referable methods."
8286   (cond
8287    ;; No method, so we default to current and native.
8288    ((null gnus-refer-article-method)
8289     (list gnus-current-select-method gnus-select-method))
8290    ;; Current.
8291    ((eq 'current gnus-refer-article-method)
8292     (list gnus-current-select-method))
8293    ;; List of select methods.
8294    ((not (and (symbolp (car gnus-refer-article-method))
8295               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8296     (let (out)
8297       (dolist (method gnus-refer-article-method)
8298         (push (if (eq 'current method)
8299                   gnus-current-select-method
8300                 method)
8301               out))
8302       (nreverse out)))
8303    ;; One single select method.
8304    (t
8305     (list gnus-refer-article-method))))
8306
8307 (defun gnus-summary-edit-parameters ()
8308   "Edit the group parameters of the current group."
8309   (interactive)
8310   (gnus-group-edit-group gnus-newsgroup-name 'params))
8311
8312 (defun gnus-summary-customize-parameters ()
8313   "Customize the group parameters of the current group."
8314   (interactive)
8315   (gnus-group-customize gnus-newsgroup-name))
8316
8317 (defun gnus-summary-enter-digest-group (&optional force)
8318   "Enter an nndoc group based on the current article.
8319 If FORCE, force a digest interpretation.  If not, try
8320 to guess what the document format is."
8321   (interactive "P")
8322   (let ((conf gnus-current-window-configuration))
8323     (save-window-excursion
8324       (save-excursion
8325         (let (gnus-article-prepare-hook
8326               gnus-display-mime-function
8327               gnus-break-pages)
8328           (gnus-summary-select-article))))
8329     (setq gnus-current-window-configuration conf)
8330     (let* ((name (format "%s-%d"
8331                          (gnus-group-prefixed-name
8332                           gnus-newsgroup-name (list 'nndoc ""))
8333                          (with-current-buffer gnus-summary-buffer
8334                            gnus-current-article)))
8335            (ogroup gnus-newsgroup-name)
8336            (params (append (gnus-info-params (gnus-get-info ogroup))
8337                            (list (cons 'to-group ogroup))
8338                            (list (cons 'parent-group ogroup))
8339                            (list (cons 'save-article-group ogroup))))
8340            (case-fold-search t)
8341            (buf (current-buffer))
8342            dig to-address)
8343       (save-excursion
8344         (set-buffer gnus-original-article-buffer)
8345         ;; Have the digest group inherit the main mail address of
8346         ;; the parent article.
8347         (when (setq to-address (or (gnus-fetch-field "reply-to")
8348                                    (gnus-fetch-field "from")))
8349           (setq params (append
8350                         (list (cons 'to-address
8351                                     (funcall gnus-decode-encoded-word-function
8352                                              to-address))))))
8353         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8354         (insert-buffer-substring gnus-original-article-buffer)
8355         ;; Remove lines that may lead nndoc to misinterpret the
8356         ;; document type.
8357         (narrow-to-region
8358          (goto-char (point-min))
8359          (or (search-forward "\n\n" nil t) (point)))
8360         (goto-char (point-min))
8361         (delete-matching-lines "^Path:\\|^From ")
8362         (widen))
8363       (unwind-protect
8364           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8365                     (gnus-newsgroup-ephemeral-ignored-charsets
8366                      gnus-newsgroup-ignored-charsets))
8367                 (gnus-group-read-ephemeral-group
8368                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8369                               (nndoc-article-type
8370                                ,(if force 'mbox 'guess)))
8371                  t nil nil nil
8372                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8373                                                         "ADAPT")))))
8374               ;; Make all postings to this group go to the parent group.
8375               (nconc (gnus-info-params (gnus-get-info name))
8376                      params)
8377             ;; Couldn't select this doc group.
8378             (switch-to-buffer buf)
8379             (gnus-set-global-variables)
8380             (gnus-configure-windows 'summary)
8381             (gnus-message 3 "Article couldn't be entered?"))
8382         (kill-buffer dig)))))
8383
8384 (defun gnus-summary-read-document (n)
8385   "Open a new group based on the current article(s).
8386 This will allow you to read digests and other similar
8387 documents as newsgroups.
8388 Obeys the standard process/prefix convention."
8389   (interactive "P")
8390   (let* ((articles (gnus-summary-work-articles n))
8391          (ogroup gnus-newsgroup-name)
8392          (params (append (gnus-info-params (gnus-get-info ogroup))
8393                          (list (cons 'to-group ogroup))))
8394          article group egroup groups vgroup)
8395     (while (setq article (pop articles))
8396       (setq group (format "%s-%d" gnus-newsgroup-name article))
8397       (gnus-summary-remove-process-mark article)
8398       (when (gnus-summary-display-article article)
8399         (save-excursion
8400           (with-temp-buffer
8401             (insert-buffer-substring gnus-original-article-buffer)
8402             ;; Remove some headers that may lead nndoc to make
8403             ;; the wrong guess.
8404             (message-narrow-to-head)
8405             (goto-char (point-min))
8406             (delete-matching-lines "^Path:\\|^From ")
8407             (widen)
8408             (if (setq egroup
8409                       (gnus-group-read-ephemeral-group
8410                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8411                                      (nndoc-article-type guess))
8412                        t nil t))
8413                 (progn
8414             ;; Make all postings to this group go to the parent group.
8415                   (nconc (gnus-info-params (gnus-get-info egroup))
8416                          params)
8417                   (push egroup groups))
8418               ;; Couldn't select this doc group.
8419               (gnus-error 3 "Article couldn't be entered"))))))
8420     ;; Now we have selected all the documents.
8421     (cond
8422      ((not groups)
8423       (error "None of the articles could be interpreted as documents"))
8424      ((gnus-group-read-ephemeral-group
8425        (setq vgroup (format
8426                      "nnvirtual:%s-%s" gnus-newsgroup-name
8427                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8428        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8429        t
8430        (cons (current-buffer) 'summary)))
8431      (t
8432       (error "Couldn't select virtual nndoc group")))))
8433
8434 (defun gnus-summary-isearch-article (&optional regexp-p)
8435   "Do incremental search forward on the current article.
8436 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8437   (interactive "P")
8438   (gnus-summary-select-article)
8439   (gnus-configure-windows 'article)
8440   (gnus-eval-in-buffer-window gnus-article-buffer
8441     (save-restriction
8442       (widen)
8443       (isearch-forward regexp-p))))
8444
8445 (defun gnus-summary-search-article-forward (regexp &optional backward)
8446   "Search for an article containing REGEXP forward.
8447 If BACKWARD, search backward instead."
8448   (interactive
8449    (list (read-string
8450           (format "Search article %s (regexp%s): "
8451                   (if current-prefix-arg "backward" "forward")
8452                   (if gnus-last-search-regexp
8453                       (concat ", default " gnus-last-search-regexp)
8454                     "")))
8455          current-prefix-arg))
8456   (if (string-equal regexp "")
8457       (setq regexp (or gnus-last-search-regexp ""))
8458     (setq gnus-last-search-regexp regexp)
8459     (setq gnus-article-before-search gnus-current-article))
8460   ;; Intentionally set gnus-last-article.
8461   (setq gnus-last-article gnus-article-before-search)
8462   (let ((gnus-last-article gnus-last-article))
8463     (if (gnus-summary-search-article regexp backward)
8464         (gnus-summary-show-thread)
8465       (signal 'search-failed (list regexp)))))
8466
8467 (defun gnus-summary-search-article-backward (regexp)
8468   "Search for an article containing REGEXP backward."
8469   (interactive
8470    (list (read-string
8471           (format "Search article backward (regexp%s): "
8472                   (if gnus-last-search-regexp
8473                       (concat ", default " gnus-last-search-regexp)
8474                     "")))))
8475   (gnus-summary-search-article-forward regexp 'backward))
8476
8477 (defun gnus-summary-search-article (regexp &optional backward)
8478   "Search for an article containing REGEXP.
8479 Optional argument BACKWARD means do search for backward.
8480 `gnus-select-article-hook' is not called during the search."
8481   ;; We have to require this here to make sure that the following
8482   ;; dynamic binding isn't shadowed by autoloading.
8483   (require 'gnus-async)
8484   (require 'gnus-art)
8485   (let ((gnus-select-article-hook nil)  ;Disable hook.
8486         (gnus-article-prepare-hook nil)
8487         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8488         (gnus-use-article-prefetch nil)
8489         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8490         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8491         (gnus-visual nil)
8492         (gnus-keep-backlog nil)
8493         (gnus-break-pages nil)
8494         (gnus-summary-display-arrow nil)
8495         (gnus-updated-mode-lines nil)
8496         (gnus-auto-center-summary nil)
8497         (sum (current-buffer))
8498         (gnus-display-mime-function nil)
8499         (found nil)
8500         point)
8501     (gnus-save-hidden-threads
8502       (gnus-summary-select-article)
8503       (set-buffer gnus-article-buffer)
8504       (goto-char (window-point (get-buffer-window (current-buffer))))
8505       (when backward
8506         (forward-line -1))
8507       (while (not found)
8508         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8509         (if (if backward
8510                 (re-search-backward regexp nil t)
8511               (re-search-forward regexp nil t))
8512             ;; We found the regexp.
8513             (progn
8514               (setq found 'found)
8515               (beginning-of-line)
8516               (set-window-start
8517                (get-buffer-window (current-buffer))
8518                (point))
8519               (forward-line 1)
8520               (set-window-point
8521                (get-buffer-window (current-buffer))
8522                (point))
8523               (set-buffer sum)
8524               (setq point (point)))
8525           ;; We didn't find it, so we go to the next article.
8526           (set-buffer sum)
8527           (setq found 'not)
8528           (while (eq found 'not)
8529             (if (not (if backward (gnus-summary-find-prev)
8530                        (gnus-summary-find-next)))
8531                 ;; No more articles.
8532                 (setq found t)
8533               ;; Select the next article and adjust point.
8534               (unless (gnus-summary-article-sparse-p
8535                        (gnus-summary-article-number))
8536                 (setq found nil)
8537                 (gnus-summary-select-article)
8538                 (set-buffer gnus-article-buffer)
8539                 (widen)
8540                 (goto-char (if backward (point-max) (point-min))))))))
8541       (gnus-message 7 ""))
8542     ;; Return whether we found the regexp.
8543     (when (eq found 'found)
8544       (goto-char point)
8545       (gnus-summary-show-thread)
8546       (gnus-summary-goto-subject gnus-current-article)
8547       (gnus-summary-position-point)
8548       t)))
8549
8550 (defun gnus-find-matching-articles (header regexp)
8551   "Return a list of all articles that match REGEXP on HEADER.
8552 This search includes all articles in the current group that Gnus has
8553 fetched headers for, whether they are displayed or not."
8554   (let ((articles nil)
8555         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8556         (case-fold-search t))
8557     (dolist (header gnus-newsgroup-headers)
8558       (when (string-match regexp (funcall func header))
8559         (push (mail-header-number header) articles)))
8560     (nreverse articles)))
8561
8562 (defun gnus-summary-find-matching (header regexp &optional backward unread
8563                                           not-case-fold not-matching)
8564   "Return a list of all articles that match REGEXP on HEADER.
8565 The search stars on the current article and goes forwards unless
8566 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8567 If UNREAD is non-nil, only unread articles will
8568 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8569 in the comparisons. If NOT-MATCHING, return a list of all articles that
8570 not match REGEXP on HEADER."
8571   (let ((case-fold-search (not not-case-fold))
8572         articles d func)
8573     (if (consp header)
8574         (if (eq (car header) 'extra)
8575             (setq func
8576                   `(lambda (h)
8577                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8578                          "")))
8579           (error "%s is an invalid header" header))
8580       (unless (fboundp (intern (concat "mail-header-" header)))
8581         (error "%s is not a valid header" header))
8582       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8583     (dolist (d (if (eq backward 'all)
8584                    gnus-newsgroup-data
8585                  (gnus-data-find-list
8586                   (gnus-summary-article-number)
8587                   (gnus-data-list backward))))
8588       (when (and (or (not unread)       ; We want all articles...
8589                      (gnus-data-unread-p d)) ; Or just unreads.
8590                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8591                  (if not-matching
8592                      (not (string-match
8593                            regexp
8594                            (funcall func (gnus-data-header d))))
8595                    (string-match regexp
8596                                  (funcall func (gnus-data-header d)))))
8597         (push (gnus-data-number d) articles))) ; Success!
8598     (nreverse articles)))
8599
8600 (defun gnus-summary-execute-command (header regexp command &optional backward)
8601   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8602 If HEADER is an empty string (or nil), the match is done on the entire
8603 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8604   (interactive
8605    (list (let ((completion-ignore-case t))
8606            (completing-read
8607             "Header name: "
8608             (mapcar (lambda (header) (list (format "%s" header)))
8609                     (append
8610                      '("Number" "Subject" "From" "Lines" "Date"
8611                        "Message-ID" "Xref" "References" "Body")
8612                      gnus-extra-headers))
8613             nil 'require-match))
8614          (read-string "Regexp: ")
8615          (read-key-sequence "Command: ")
8616          current-prefix-arg))
8617   (when (equal header "Body")
8618     (setq header ""))
8619   ;; Hidden thread subtrees must be searched as well.
8620   (gnus-summary-show-all-threads)
8621   ;; We don't want to change current point nor window configuration.
8622   (save-excursion
8623     (save-window-excursion
8624       (let (gnus-visual
8625             gnus-treat-strip-trailing-blank-lines
8626             gnus-treat-strip-leading-blank-lines
8627             gnus-treat-strip-multiple-blank-lines
8628             gnus-treat-hide-boring-headers
8629             gnus-treat-fold-newsgroups
8630             gnus-article-prepare-hook)
8631         (gnus-message 6 "Executing %s..." (key-description command))
8632         ;; We'd like to execute COMMAND interactively so as to give arguments.
8633         (gnus-execute header regexp
8634                       `(call-interactively ',(key-binding command))
8635                       backward)
8636         (gnus-message 6 "Executing %s...done" (key-description command))))))
8637
8638 (defun gnus-summary-beginning-of-article ()
8639   "Scroll the article back to the beginning."
8640   (interactive)
8641   (gnus-summary-select-article)
8642   (gnus-configure-windows 'article)
8643   (gnus-eval-in-buffer-window gnus-article-buffer
8644     (widen)
8645     (goto-char (point-min))
8646     (when gnus-break-pages
8647       (gnus-narrow-to-page))))
8648
8649 (defun gnus-summary-end-of-article ()
8650   "Scroll to the end of the article."
8651   (interactive)
8652   (gnus-summary-select-article)
8653   (gnus-configure-windows 'article)
8654   (gnus-eval-in-buffer-window gnus-article-buffer
8655     (widen)
8656     (goto-char (point-max))
8657     (recenter -3)
8658     (when gnus-break-pages
8659       (when (re-search-backward page-delimiter nil t)
8660         (narrow-to-region (match-end 0) (point-max)))
8661       (gnus-narrow-to-page))))
8662
8663 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8664   "Truncate to LEN and quote all \"(\"'s in STRING."
8665   (gnus-replace-in-string (if (and len (> (length string) len))
8666                               (substring string 0 len)
8667                             string)
8668                           "[()]" "\\\\\\&"))
8669
8670 (defun gnus-summary-print-article (&optional filename n)
8671   "Generate and print a PostScript image of the process-marked (mail) articles.
8672
8673 If used interactively, print the current article if none are
8674 process-marked.  With prefix arg, prompt the user for the name of the
8675 file to save in.
8676
8677 When used from Lisp, accept two optional args FILENAME and N.  N means
8678 to print the next N articles.  If N is negative, print the N previous
8679 articles.  If N is nil and articles have been marked with the process
8680 mark, print these instead.
8681
8682 If the optional first argument FILENAME is nil, send the image to the
8683 printer.  If FILENAME is a string, save the PostScript image in a file with
8684 that name.  If FILENAME is a number, prompt the user for the name of the file
8685 to save in."
8686   (interactive (list (ps-print-preprint current-prefix-arg)))
8687   (dolist (article (gnus-summary-work-articles n))
8688     (gnus-summary-select-article nil nil 'pseudo article)
8689     (gnus-eval-in-buffer-window gnus-article-buffer
8690       (gnus-print-buffer))
8691     (gnus-summary-remove-process-mark article))
8692   (ps-despool filename))
8693
8694 (defun gnus-print-buffer ()
8695   (let ((buffer (generate-new-buffer " *print*")))
8696     (unwind-protect
8697         (progn
8698           (copy-to-buffer buffer (point-min) (point-max))
8699           (set-buffer buffer)
8700           (gnus-remove-text-with-property 'gnus-decoration)
8701           (when (gnus-visual-p 'article-highlight 'highlight)
8702             ;; Copy-to-buffer doesn't copy overlay.  So redo
8703             ;; highlight.
8704             (let ((gnus-article-buffer buffer))
8705               (gnus-article-highlight-citation t)
8706               (gnus-article-highlight-signature)
8707               (gnus-article-emphasize)
8708               (gnus-article-delete-invisible-text)))
8709           (let ((ps-left-header
8710                  (list
8711                   (concat "("
8712                           (gnus-summary-print-truncate-and-quote
8713                            (mail-header-subject gnus-current-headers)
8714                            66) ")")
8715                   (concat "("
8716                           (gnus-summary-print-truncate-and-quote
8717                            (mail-header-from gnus-current-headers)
8718                            45) ")")))
8719                 (ps-right-header
8720                  (list
8721                   "/pagenumberstring load"
8722                   (concat "("
8723                           (mail-header-date gnus-current-headers) ")"))))
8724             (gnus-run-hooks 'gnus-ps-print-hook)
8725             (save-excursion
8726               (if window-system
8727                   (ps-spool-buffer-with-faces)
8728                 (ps-spool-buffer)))))
8729       (kill-buffer buffer))))
8730
8731 (defun gnus-summary-show-article (&optional arg)
8732   "Force redisplaying of the current article.
8733 If ARG (the prefix) is a number, show the article with the charset
8734 defined in `gnus-summary-show-article-charset-alist', or the charset
8735 input.
8736 If ARG (the prefix) is non-nil and not a number, show the raw article
8737 without any article massaging functions being run.  Normally, the key
8738 strokes are `C-u g'."
8739   (interactive "P")
8740   (cond
8741    ((numberp arg)
8742     (gnus-summary-show-article t)
8743     (let ((gnus-newsgroup-charset
8744            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8745                (mm-read-coding-system
8746                 "View as charset: " ;; actually it is coding system.
8747                 (save-excursion
8748                   (set-buffer gnus-article-buffer)
8749                   (mm-detect-coding-region (point) (point-max))))))
8750           (gnus-newsgroup-ignored-charsets 'gnus-all))
8751       (gnus-summary-select-article nil 'force)
8752       (let ((deps gnus-newsgroup-dependencies)
8753             head header lines)
8754         (save-excursion
8755           (set-buffer gnus-original-article-buffer)
8756           (save-restriction
8757             (message-narrow-to-head)
8758             (setq head (buffer-string))
8759             (goto-char (point-min))
8760             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8761               (goto-char (point-max))
8762               (widen)
8763               (setq lines (1- (count-lines (point) (point-max))))))
8764           (with-temp-buffer
8765             (insert (format "211 %d Article retrieved.\n"
8766                             (cdr gnus-article-current)))
8767             (insert head)
8768             (if lines (insert (format "Lines: %d\n" lines)))
8769             (insert ".\n")
8770             (let ((nntp-server-buffer (current-buffer)))
8771               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8772         (gnus-data-set-header
8773          (gnus-data-find (cdr gnus-article-current))
8774          header)
8775         (gnus-summary-update-article-line
8776          (cdr gnus-article-current) header)
8777         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8778           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8779    ((not arg)
8780     ;; Select the article the normal way.
8781     (gnus-summary-select-article nil 'force))
8782    (t
8783     ;; We have to require this here to make sure that the following
8784     ;; dynamic binding isn't shadowed by autoloading.
8785     (require 'gnus-async)
8786     (require 'gnus-art)
8787     ;; Bind the article treatment functions to nil.
8788     (let ((gnus-have-all-headers t)
8789           gnus-article-prepare-hook
8790           gnus-article-decode-hook
8791           gnus-display-mime-function
8792           gnus-break-pages)
8793       ;; Destroy any MIME parts.
8794       (when (gnus-buffer-live-p gnus-article-buffer)
8795         (save-excursion
8796           (set-buffer gnus-article-buffer)
8797           (mm-destroy-parts gnus-article-mime-handles)
8798           ;; Set it to nil for safety reason.
8799           (setq gnus-article-mime-handle-alist nil)
8800           (setq gnus-article-mime-handles nil)))
8801       (gnus-summary-select-article nil 'force))))
8802   (gnus-summary-goto-subject gnus-current-article)
8803   (gnus-summary-position-point))
8804
8805 (defun gnus-summary-show-raw-article ()
8806   "Show the raw article without any article massaging functions being run."
8807   (interactive)
8808   (gnus-summary-show-article t))
8809
8810 (defun gnus-summary-verbose-headers (&optional arg)
8811   "Toggle permanent full header display.
8812 If ARG is a positive number, turn header display on.
8813 If ARG is a negative number, turn header display off."
8814   (interactive "P")
8815   (setq gnus-show-all-headers
8816         (cond ((or (not (numberp arg))
8817                    (zerop arg))
8818                (not gnus-show-all-headers))
8819               ((natnump arg)
8820                t)))
8821   (gnus-summary-show-article))
8822
8823 (defun gnus-summary-toggle-header (&optional arg)
8824   "Show the headers if they are hidden, or hide them if they are shown.
8825 If ARG is a positive number, show the entire header.
8826 If ARG is a negative number, hide the unwanted header lines."
8827   (interactive "P")
8828   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8829                      (get-buffer-window gnus-article-buffer t))))
8830     (with-current-buffer gnus-article-buffer
8831       (widen)
8832       (article-narrow-to-head)
8833       (let* ((buffer-read-only nil)
8834              (inhibit-point-motion-hooks t)
8835              (hidden (if (numberp arg)
8836                          (>= arg 0)
8837                        (gnus-article-hidden-text-p 'headers)))
8838              s e)
8839         (delete-region (point-min) (point-max))
8840         (with-current-buffer gnus-original-article-buffer
8841           (goto-char (setq s (point-min)))
8842           (setq e (if (search-forward "\n\n" nil t)
8843                       (1- (point))
8844                     (point-max))))
8845         (insert-buffer-substring gnus-original-article-buffer s e)
8846         (run-hooks 'gnus-article-decode-hook)
8847         (if hidden
8848             (let ((gnus-treat-hide-headers nil)
8849                   (gnus-treat-hide-boring-headers nil))
8850               (gnus-delete-wash-type 'headers)
8851               (gnus-treat-article 'head))
8852           (gnus-treat-article 'head))
8853         (widen)
8854         (if window
8855             (set-window-start window (goto-char (point-min))))
8856         (if gnus-break-pages
8857             (gnus-narrow-to-page)
8858           (when (gnus-visual-p 'page-marker)
8859             (let ((buffer-read-only nil))
8860               (gnus-remove-text-with-property 'gnus-prev)
8861               (gnus-remove-text-with-property 'gnus-next))))
8862         (gnus-set-mode-line 'article)))))
8863
8864 (defun gnus-summary-show-all-headers ()
8865   "Make all header lines visible."
8866   (interactive)
8867   (gnus-summary-toggle-header 1))
8868
8869 (defun gnus-summary-caesar-message (&optional arg)
8870   "Caesar rotate the current article by 13.
8871 The numerical prefix specifies how many places to rotate each letter
8872 forward."
8873   (interactive "P")
8874   (gnus-summary-select-article)
8875   (let ((mail-header-separator ""))
8876     (gnus-eval-in-buffer-window gnus-article-buffer
8877       (save-restriction
8878         (widen)
8879         (let ((start (window-start))
8880               buffer-read-only)
8881           (message-caesar-buffer-body arg)
8882           (set-window-start (get-buffer-window (current-buffer)) start))))))
8883
8884 (autoload 'unmorse-region "morse"
8885   "Convert morse coded text in region to ordinary ASCII text."
8886   t)
8887
8888 (defun gnus-summary-morse-message (&optional arg)
8889   "Morse decode the current article."
8890   (interactive "P")
8891   (gnus-summary-select-article)
8892   (let ((mail-header-separator ""))
8893     (gnus-eval-in-buffer-window gnus-article-buffer
8894       (save-excursion
8895         (save-restriction
8896           (widen)
8897           (let ((pos (window-start))
8898                 buffer-read-only)
8899             (goto-char (point-min))
8900             (when (message-goto-body)
8901               (gnus-narrow-to-body))
8902             (goto-char (point-min))
8903             (while (search-forward "·" (point-max) t)
8904               (replace-match "."))
8905             (unmorse-region (point-min) (point-max))
8906             (widen)
8907             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8908
8909 (defun gnus-summary-stop-page-breaking ()
8910   "Stop page breaking in the current article."
8911   (interactive)
8912   (gnus-summary-select-article)
8913   (gnus-eval-in-buffer-window gnus-article-buffer
8914     (widen)
8915     (when (gnus-visual-p 'page-marker)
8916       (let ((buffer-read-only nil))
8917         (gnus-remove-text-with-property 'gnus-prev)
8918         (gnus-remove-text-with-property 'gnus-next))
8919       (setq gnus-page-broken nil))))
8920
8921 (defun gnus-summary-move-article (&optional n to-newsgroup
8922                                             select-method action)
8923   "Move the current article to a different newsgroup.
8924 If N is a positive number, move the N next articles.
8925 If N is a negative number, move the N previous articles.
8926 If N is nil and any articles have been marked with the process mark,
8927 move those articles instead.
8928 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8929 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8930 re-spool using this method.
8931
8932 When called interactively with TO-NEWSGROUP being nil, the value of
8933 the variable `gnus-move-split-methods' is used for finding a default
8934 for the target newsgroup.
8935
8936 For this function to work, both the current newsgroup and the
8937 newsgroup that you want to move to have to support the `request-move'
8938 and `request-accept' functions.
8939
8940 ACTION can be either `move' (the default), `crosspost' or `copy'."
8941   (interactive "P")
8942   (unless action
8943     (setq action 'move))
8944   ;; Check whether the source group supports the required functions.
8945   (cond ((and (eq action 'move)
8946               (not (gnus-check-backend-function
8947                     'request-move-article gnus-newsgroup-name)))
8948          (error "The current group does not support article moving"))
8949         ((and (eq action 'crosspost)
8950               (not (gnus-check-backend-function
8951                     'request-replace-article gnus-newsgroup-name)))
8952          (error "The current group does not support article editing")))
8953   (let ((articles (gnus-summary-work-articles n))
8954         (prefix (if (gnus-check-backend-function
8955                      'request-move-article gnus-newsgroup-name)
8956                     (funcall gnus-move-group-prefix-function
8957                              gnus-newsgroup-name)
8958                   ""))
8959         (names '((move "Move" "Moving")
8960                  (copy "Copy" "Copying")
8961                  (crosspost "Crosspost" "Crossposting")))
8962         (copy-buf (save-excursion
8963                     (nnheader-set-temp-buffer " *copy article*")))
8964         art-group to-method new-xref article to-groups)
8965     (unless (assq action names)
8966       (error "Unknown action %s" action))
8967     ;; Read the newsgroup name.
8968     (when (and (not to-newsgroup)
8969                (not select-method))
8970       (if (and gnus-move-split-methods
8971                (not
8972                 (and (memq gnus-current-article articles)
8973                      (gnus-buffer-live-p gnus-original-article-buffer))))
8974           ;; When `gnus-move-split-methods' is non-nil, we have to
8975           ;; select an article to give `gnus-read-move-group-name' an
8976           ;; opportunity to suggest an appropriate default.  However,
8977           ;; we needn't render or mark the article.
8978           (let ((gnus-display-mime-function nil)
8979                 (gnus-article-prepare-hook nil)
8980                 (gnus-mark-article-hook nil))
8981             (gnus-summary-select-article nil nil nil (car articles))))
8982       (setq to-newsgroup
8983             (gnus-read-move-group-name
8984              (cadr (assq action names))
8985              (symbol-value (intern (format "gnus-current-%s-group" action)))
8986              articles prefix))
8987       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8988     (setq to-method (or select-method
8989                         (gnus-server-to-method
8990                          (gnus-group-method to-newsgroup))))
8991     ;; Check the method we are to move this article to...
8992     (unless (gnus-check-backend-function
8993              'request-accept-article (car to-method))
8994       (error "%s does not support article copying" (car to-method)))
8995     (unless (gnus-check-server to-method)
8996       (error "Can't open server %s" (car to-method)))
8997     (gnus-message 6 "%s to %s: %s..."
8998                   (caddr (assq action names))
8999                   (or (car select-method) to-newsgroup) articles)
9000     (while articles
9001       (setq article (pop articles))
9002       (setq
9003        art-group
9004        (cond
9005         ;; Move the article.
9006         ((eq action 'move)
9007          ;; Remove this article from future suppression.
9008          (gnus-dup-unsuppress-article article)
9009          (gnus-request-move-article
9010           article                       ; Article to move
9011           gnus-newsgroup-name           ; From newsgroup
9012           (nth 1 (gnus-find-method-for-group
9013                   gnus-newsgroup-name)) ; Server
9014           (list 'gnus-request-accept-article
9015                 to-newsgroup (list 'quote select-method)
9016                 (not articles) t)       ; Accept form
9017           (not articles)))              ; Only save nov last time
9018         ;; Copy the article.
9019         ((eq action 'copy)
9020          (save-excursion
9021            (set-buffer copy-buf)
9022            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9023              (gnus-request-accept-article
9024               to-newsgroup select-method (not articles) t))))
9025         ;; Crosspost the article.
9026         ((eq action 'crosspost)
9027          (let ((xref (message-tokenize-header
9028                       (mail-header-xref (gnus-summary-article-header article))
9029                       " ")))
9030            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9031                                   ":" (number-to-string article)))
9032            (unless xref
9033              (setq xref (list (system-name))))
9034            (setq new-xref
9035                  (concat
9036                   (mapconcat 'identity
9037                              (delete "Xref:" (delete new-xref xref))
9038                              " ")
9039                   " " new-xref))
9040            (save-excursion
9041              (set-buffer copy-buf)
9042              ;; First put the article in the destination group.
9043              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9044              (when (consp (setq art-group
9045                                 (gnus-request-accept-article
9046                                  to-newsgroup select-method (not articles))))
9047                (setq new-xref (concat new-xref " " (car art-group)
9048                                       ":"
9049                                       (number-to-string (cdr art-group))))
9050                ;; Now we have the new Xrefs header, so we insert
9051                ;; it and replace the new article.
9052                (nnheader-replace-header "Xref" new-xref)
9053                (gnus-request-replace-article
9054                 (cdr art-group) to-newsgroup (current-buffer))
9055                art-group))))))
9056       (cond
9057        ((not art-group)
9058         (gnus-message 1 "Couldn't %s article %s: %s"
9059                       (cadr (assq action names)) article
9060                       (nnheader-get-report (car to-method))))
9061        ((eq art-group 'junk)
9062         (when (eq action 'move)
9063           (gnus-summary-mark-article article gnus-canceled-mark)
9064           (gnus-message 4 "Deleted article %s" article)
9065           ;; run the delete hook
9066           (run-hook-with-args 'gnus-summary-article-delete-hook
9067                               action
9068                               (gnus-data-header
9069                                (assoc article (gnus-data-list nil)))
9070                               gnus-newsgroup-name nil
9071                               select-method)))
9072        (t
9073         (let* ((pto-group (gnus-group-prefixed-name
9074                            (car art-group) to-method))
9075                (info (gnus-get-info pto-group))
9076                (to-group (gnus-info-group info))
9077                to-marks)
9078           ;; Update the group that has been moved to.
9079           (when (and info
9080                      (memq action '(move copy)))
9081             (unless (member to-group to-groups)
9082               (push to-group to-groups))
9083
9084             (unless (memq article gnus-newsgroup-unreads)
9085               (push 'read to-marks)
9086               (gnus-info-set-read
9087                info (gnus-add-to-range (gnus-info-read info)
9088                                        (list (cdr art-group)))))
9089
9090             ;; See whether the article is to be put in the cache.
9091             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9092                              gnus-article-mark-lists
9093                            (delete '(expirable . expire)
9094                                    (copy-sequence gnus-article-mark-lists))))
9095                   (to-article (cdr art-group)))
9096
9097               ;; Enter the article into the cache in the new group,
9098               ;; if that is required.
9099               (when gnus-use-cache
9100                 (gnus-cache-possibly-enter-article
9101                  to-group to-article
9102                  (memq article gnus-newsgroup-marked)
9103                  (memq article gnus-newsgroup-dormant)
9104                  (memq article gnus-newsgroup-unreads)))
9105
9106               (when gnus-preserve-marks
9107                 ;; Copy any marks over to the new group.
9108                 (when (and (equal to-group gnus-newsgroup-name)
9109                            (not (memq article gnus-newsgroup-unreads)))
9110                   ;; Mark this article as read in this group.
9111                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9112                   (setcdr (gnus-active to-group) to-article)
9113                   (setcdr gnus-newsgroup-active to-article))
9114
9115                 (while marks
9116                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9117                     (when (memq article (symbol-value
9118                                          (intern (format "gnus-newsgroup-%s"
9119                                                          (caar marks)))))
9120                       (push (cdar marks) to-marks)
9121                       ;; If the other group is the same as this group,
9122                       ;; then we have to add the mark to the list.
9123                       (when (equal to-group gnus-newsgroup-name)
9124                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9125                              (cons to-article
9126                                    (symbol-value
9127                                     (intern (format "gnus-newsgroup-%s"
9128                                                     (caar marks)))))))
9129                       ;; Copy the marks to other group.
9130                       (gnus-add-marked-articles
9131                        to-group (cdar marks) (list to-article) info)))
9132                   (setq marks (cdr marks)))
9133
9134                 (gnus-request-set-mark
9135                  to-group (list (list (list to-article) 'add to-marks))))
9136
9137               (gnus-dribble-enter
9138                (concat "(gnus-group-set-info '"
9139                        (gnus-prin1-to-string (gnus-get-info to-group))
9140                        ")"))))
9141
9142           ;; Update the Xref header in this article to point to
9143           ;; the new crossposted article we have just created.
9144           (when (eq action 'crosspost)
9145             (save-excursion
9146               (set-buffer copy-buf)
9147               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9148               (nnheader-replace-header "Xref" new-xref)
9149               (gnus-request-replace-article
9150                article gnus-newsgroup-name (current-buffer))))
9151
9152           ;; run the move/copy/crosspost/respool hook
9153           (run-hook-with-args 'gnus-summary-article-move-hook
9154                               action
9155                               (gnus-data-header
9156                                (assoc article (gnus-data-list nil)))
9157                               gnus-newsgroup-name
9158                               to-newsgroup
9159                               select-method))
9160
9161         ;;;!!!Why is this necessary?
9162         (set-buffer gnus-summary-buffer)
9163         
9164         (gnus-summary-goto-subject article)
9165         (when (eq action 'move)
9166           (gnus-summary-mark-article article gnus-canceled-mark))))
9167       (gnus-summary-remove-process-mark article))
9168     ;; Re-activate all groups that have been moved to.
9169     (save-excursion
9170       (set-buffer gnus-group-buffer)
9171       (let ((gnus-group-marked to-groups))
9172         (gnus-group-get-new-news-this-group nil t)))
9173
9174     (gnus-kill-buffer copy-buf)
9175     (gnus-summary-position-point)
9176     (gnus-set-mode-line 'summary)))
9177
9178 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9179   "Copy the current article to some other group.
9180 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9181 When called interactively, if TO-NEWSGROUP is nil, use the value of
9182 the variable `gnus-move-split-methods' for finding a default target
9183 newsgroup.
9184 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9185 re-spool using this method."
9186   (interactive "P")
9187   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9188
9189 (defun gnus-summary-crosspost-article (&optional n)
9190   "Crosspost the current article to some other group."
9191   (interactive "P")
9192   (gnus-summary-move-article n nil nil 'crosspost))
9193
9194 (defcustom gnus-summary-respool-default-method nil
9195   "Default method type for respooling an article.
9196 If nil, use to the current newsgroup method."
9197   :type 'symbol
9198   :group 'gnus-summary-mail)
9199
9200 (defcustom gnus-summary-display-while-building nil
9201   "If non-nil, show and update the summary buffer as it's being built.
9202 If the value is t, update the buffer after every line is inserted.  If
9203 the value is an integer (N), update the display every N lines."
9204   :group 'gnus-thread
9205   :type '(choice (const :tag "off" nil)
9206                  number
9207                  (const :tag "frequently" t)))
9208
9209 (defun gnus-summary-respool-article (&optional n method)
9210   "Respool the current article.
9211 The article will be squeezed through the mail spooling process again,
9212 which means that it will be put in some mail newsgroup or other
9213 depending on `nnmail-split-methods'.
9214 If N is a positive number, respool the N next articles.
9215 If N is a negative number, respool the N previous articles.
9216 If N is nil and any articles have been marked with the process mark,
9217 respool those articles instead.
9218
9219 Respooling can be done both from mail groups and \"real\" newsgroups.
9220 In the former case, the articles in question will be moved from the
9221 current group into whatever groups they are destined to.  In the
9222 latter case, they will be copied into the relevant groups."
9223   (interactive
9224    (list current-prefix-arg
9225          (let* ((methods (gnus-methods-using 'respool))
9226                 (methname
9227                  (symbol-name (or gnus-summary-respool-default-method
9228                                   (car (gnus-find-method-for-group
9229                                         gnus-newsgroup-name)))))
9230                 (method
9231                  (gnus-completing-read-with-default
9232                   methname "What backend do you want to use when respooling?"
9233                   methods nil t nil 'gnus-mail-method-history))
9234                 ms)
9235            (cond
9236             ((zerop (length (setq ms (gnus-servers-using-backend
9237                                       (intern method)))))
9238              (list (intern method) ""))
9239             ((= 1 (length ms))
9240              (car ms))
9241             (t
9242              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9243                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9244                            ms-alist))))))))
9245   (unless method
9246     (error "No method given for respooling"))
9247   (if (assoc (symbol-name
9248               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9249              (gnus-methods-using 'respool))
9250       (gnus-summary-move-article n nil method)
9251     (gnus-summary-copy-article n nil method)))
9252
9253 (defun gnus-summary-import-article (file &optional edit)
9254   "Import an arbitrary file into a mail newsgroup."
9255   (interactive "fImport file: \nP")
9256   (let ((group gnus-newsgroup-name)
9257         (now (current-time))
9258         atts lines group-art)
9259     (unless (gnus-check-backend-function 'request-accept-article group)
9260       (error "%s does not support article importing" group))
9261     (or (file-readable-p file)
9262         (not (file-regular-p file))
9263         (error "Can't read %s" file))
9264     (save-excursion
9265       (set-buffer (gnus-get-buffer-create " *import file*"))
9266       (erase-buffer)
9267       (nnheader-insert-file-contents file)
9268       (goto-char (point-min))
9269       (if (nnheader-article-p)
9270           (save-restriction
9271             (goto-char (point-min))
9272             (search-forward "\n\n" nil t)
9273             (narrow-to-region (point-min) (1- (point)))
9274             (goto-char (point-min))
9275             (unless (re-search-forward "^date:" nil t)
9276               (goto-char (point-max))
9277               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9278        ;; This doesn't look like an article, so we fudge some headers.
9279         (setq atts (file-attributes file)
9280               lines (count-lines (point-min) (point-max)))
9281         (insert "From: " (read-string "From: ") "\n"
9282                 "Subject: " (read-string "Subject: ") "\n"
9283                 "Date: " (message-make-date (nth 5 atts)) "\n"
9284                 "Message-ID: " (message-make-message-id) "\n"
9285                 "Lines: " (int-to-string lines) "\n"
9286                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9287       (setq group-art (gnus-request-accept-article group nil t))
9288       (kill-buffer (current-buffer)))
9289     (setq gnus-newsgroup-active (gnus-activate-group group))
9290     (forward-line 1)
9291     (gnus-summary-goto-article (cdr group-art) nil t)
9292     (when edit
9293       (gnus-summary-edit-article))))
9294
9295 (defun gnus-summary-create-article ()
9296   "Create an article in a mail newsgroup."
9297   (interactive)
9298   (let ((group gnus-newsgroup-name)
9299         (now (current-time))
9300         group-art)
9301     (unless (gnus-check-backend-function 'request-accept-article group)
9302       (error "%s does not support article importing" group))
9303     (save-excursion
9304       (set-buffer (gnus-get-buffer-create " *import file*"))
9305       (erase-buffer)
9306       (goto-char (point-min))
9307       ;; This doesn't look like an article, so we fudge some headers.
9308       (insert "From: " (read-string "From: ") "\n"
9309               "Subject: " (read-string "Subject: ") "\n"
9310               "Date: " (message-make-date now) "\n"
9311               "Message-ID: " (message-make-message-id) "\n")
9312       (setq group-art (gnus-request-accept-article group nil t))
9313       (kill-buffer (current-buffer)))
9314     (setq gnus-newsgroup-active (gnus-activate-group group))
9315     (forward-line 1)
9316     (gnus-summary-goto-article (cdr group-art) nil t)
9317     (gnus-summary-edit-article)))
9318
9319 (defun gnus-summary-article-posted-p ()
9320   "Say whether the current (mail) article is available from news as well.
9321 This will be the case if the article has both been mailed and posted."
9322   (interactive)
9323   (let ((id (mail-header-references (gnus-summary-article-header)))
9324         (gnus-override-method (car (gnus-refer-article-methods))))
9325     (if (gnus-request-head id "")
9326         (gnus-message 2 "The current message was found on %s"
9327                       gnus-override-method)
9328       (gnus-message 2 "The current message couldn't be found on %s"
9329                     gnus-override-method)
9330       nil)))
9331
9332 (defun gnus-summary-expire-articles (&optional now)
9333   "Expire all articles that are marked as expirable in the current group."
9334   (interactive)
9335   (when (and (not gnus-group-is-exiting-without-update-p)
9336              (gnus-check-backend-function
9337               'request-expire-articles gnus-newsgroup-name))
9338     ;; This backend supports expiry.
9339     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9340            (expirable (if total
9341                           (progn
9342                             ;; We need to update the info for
9343                             ;; this group for `gnus-list-of-read-articles'
9344                             ;; to give us the right answer.
9345                             (gnus-run-hooks 'gnus-exit-group-hook)
9346                             (gnus-summary-update-info)
9347                             (gnus-list-of-read-articles gnus-newsgroup-name))
9348                         (setq gnus-newsgroup-expirable
9349                               (sort gnus-newsgroup-expirable '<))))
9350            (expiry-wait (if now 'immediate
9351                           (gnus-group-find-parameter
9352                            gnus-newsgroup-name 'expiry-wait)))
9353            (nnmail-expiry-target
9354             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9355                 nnmail-expiry-target))
9356            es)
9357       (when expirable
9358         ;; There are expirable articles in this group, so we run them
9359         ;; through the expiry process.
9360         (gnus-message 6 "Expiring articles...")
9361         (unless (gnus-check-group gnus-newsgroup-name)
9362           (error "Can't open server for %s" gnus-newsgroup-name))
9363         ;; The list of articles that weren't expired is returned.
9364         (save-excursion
9365           (if expiry-wait
9366               (let ((nnmail-expiry-wait-function nil)
9367                     (nnmail-expiry-wait expiry-wait))
9368                 (setq es (gnus-request-expire-articles
9369                           expirable gnus-newsgroup-name)))
9370             (setq es (gnus-request-expire-articles
9371                       expirable gnus-newsgroup-name)))
9372           (unless total
9373             (setq gnus-newsgroup-expirable es))
9374           ;; We go through the old list of expirable, and mark all
9375           ;; really expired articles as nonexistent.
9376           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9377             (let ((gnus-use-cache nil))
9378               (dolist (article expirable)
9379                 (when (and (not (memq article es))
9380                            (gnus-data-find article))
9381                   (gnus-summary-mark-article article gnus-canceled-mark)
9382                   (run-hook-with-args 'gnus-summary-article-expire-hook
9383                                       'delete
9384                                       (gnus-data-header
9385                                        (assoc article (gnus-data-list nil)))
9386                                       gnus-newsgroup-name
9387                                       nil
9388                                       nil))))))
9389         (gnus-message 6 "Expiring articles...done")))))
9390
9391 (defun gnus-summary-expire-articles-now ()
9392   "Expunge all expirable articles in the current group.
9393 This means that *all* articles that are marked as expirable will be
9394 deleted forever, right now."
9395   (interactive)
9396   (or gnus-expert-user
9397       (gnus-yes-or-no-p
9398        "Are you really, really, really sure you want to delete all these messages? ")
9399       (error "Phew!"))
9400   (gnus-summary-expire-articles t))
9401
9402 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9403 (defun gnus-summary-delete-article (&optional n)
9404   "Delete the N next (mail) articles.
9405 This command actually deletes articles.  This is not a marking
9406 command.  The article will disappear forever from your life, never to
9407 return.
9408
9409 If N is negative, delete backwards.
9410 If N is nil and articles have been marked with the process mark,
9411 delete these instead.
9412
9413 If `gnus-novice-user' is non-nil you will be asked for
9414 confirmation before the articles are deleted."
9415   (interactive "P")
9416   (unless (gnus-check-backend-function 'request-expire-articles
9417                                        gnus-newsgroup-name)
9418     (error "The current newsgroup does not support article deletion"))
9419   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9420     (error "Couldn't open server"))
9421   ;; Compute the list of articles to delete.
9422   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9423         (nnmail-expiry-target 'delete)
9424         not-deleted)
9425     (if (and gnus-novice-user
9426              (not (gnus-yes-or-no-p
9427                    (format "Do you really want to delete %s forever? "
9428                            (if (> (length articles) 1)
9429                                (format "these %s articles" (length articles))
9430                              "this article")))))
9431         ()
9432       ;; Delete the articles.
9433       (setq not-deleted (gnus-request-expire-articles
9434                          articles gnus-newsgroup-name 'force))
9435       (while articles
9436         (gnus-summary-remove-process-mark (car articles))
9437         ;; The backend might not have been able to delete the article
9438         ;; after all.
9439         (unless (memq (car articles) not-deleted)
9440           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9441         (let* ((article (car articles))
9442                (ghead  (gnus-data-header
9443                                     (assoc article (gnus-data-list nil)))))
9444           (run-hook-with-args 'gnus-summary-article-delete-hook
9445                               'delete ghead gnus-newsgroup-name nil
9446                               nil))
9447         (setq articles (cdr articles)))
9448       (when not-deleted
9449         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9450     (gnus-summary-position-point)
9451     (gnus-set-mode-line 'summary)
9452     not-deleted))
9453
9454 (defun gnus-summary-edit-article (&optional arg)
9455   "Edit the current article.
9456 This will have permanent effect only in mail groups.
9457 If ARG is nil, edit the decoded articles.
9458 If ARG is 1, edit the raw articles.
9459 If ARG is 2, edit the raw articles even in read-only groups.
9460 If ARG is 3, edit the articles with the current handles.
9461 Otherwise, allow editing of articles even in read-only
9462 groups."
9463   (interactive "P")
9464   (let (force raw current-handles)
9465     (cond
9466      ((null arg))
9467      ((eq arg 1)
9468       (setq raw t))
9469      ((eq arg 2)
9470       (setq raw t
9471             force t))
9472      ((eq arg 3)
9473       (setq current-handles
9474             (and (gnus-buffer-live-p gnus-article-buffer)
9475                  (with-current-buffer gnus-article-buffer
9476                    (prog1
9477                        gnus-article-mime-handles
9478                      (setq gnus-article-mime-handles nil))))))
9479      (t
9480       (setq force t)))
9481     (when (and raw (not force)
9482                (member gnus-newsgroup-name '("nndraft:delayed"
9483                                              "nndraft:drafts"
9484                                              "nndraft:queue")))
9485       (error "Can't edit the raw article in group %s"
9486              gnus-newsgroup-name))
9487     (save-excursion
9488       (set-buffer gnus-summary-buffer)
9489       (let ((mail-parse-charset gnus-newsgroup-charset)
9490             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9491         (gnus-set-global-variables)
9492         (when (and (not force)
9493                    (gnus-group-read-only-p))
9494           (error "The current newsgroup does not support article editing"))
9495         (gnus-summary-show-article t)
9496         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9497           (with-current-buffer gnus-article-buffer
9498             (mm-enable-multibyte)))
9499         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9500             (setq raw t))
9501         (gnus-article-edit-article
9502          (if raw 'ignore
9503            `(lambda ()
9504               (let ((mbl mml-buffer-list))
9505                 (setq mml-buffer-list nil)
9506                 (mime-to-mml ,'current-handles)
9507                 (let ((mbl1 mml-buffer-list))
9508                   (setq mml-buffer-list mbl)
9509                   (set (make-local-variable 'mml-buffer-list) mbl1))
9510                 (gnus-make-local-hook 'kill-buffer-hook)
9511                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9512          `(lambda (no-highlight)
9513             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9514                   (message-options message-options)
9515                   (message-options-set-recipient)
9516                   (mail-parse-ignored-charsets
9517                    ',gnus-newsgroup-ignored-charsets))
9518               ,(if (not raw) '(progn
9519                                 (mml-to-mime)
9520                                 (mml-destroy-buffers)
9521                                 (remove-hook 'kill-buffer-hook
9522                                              'mml-destroy-buffers t)
9523                                 (kill-local-variable 'mml-buffer-list)))
9524               (gnus-summary-edit-article-done
9525                ,(or (mail-header-references gnus-current-headers) "")
9526                ,(gnus-group-read-only-p)
9527                ,gnus-summary-buffer no-highlight))))))))
9528
9529 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9530
9531 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9532                                                  no-highlight)
9533   "Make edits to the current article permanent."
9534   (interactive)
9535   (save-excursion
9536    ;; The buffer restriction contains the entire article if it exists.
9537     (when (article-goto-body)
9538       (let ((lines (count-lines (point) (point-max)))
9539             (length (- (point-max) (point)))
9540             (case-fold-search t)
9541             (body (copy-marker (point))))
9542         (goto-char (point-min))
9543         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9544           (delete-region (match-beginning 1) (match-end 1))
9545           (insert (number-to-string length)))
9546         (goto-char (point-min))
9547         (when (re-search-forward
9548                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9549           (delete-region (match-beginning 1) (match-end 1))
9550           (insert (number-to-string length)))
9551         (goto-char (point-min))
9552         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9553           (delete-region (match-beginning 1) (match-end 1))
9554           (insert (number-to-string lines))))))
9555   ;; Replace the article.
9556   (let ((buf (current-buffer)))
9557     (with-temp-buffer
9558       (insert-buffer-substring buf)
9559
9560       (if (and (not read-only)
9561                (not (gnus-request-replace-article
9562                      (cdr gnus-article-current) (car gnus-article-current)
9563                      (current-buffer) t)))
9564           (error "Couldn't replace article")
9565         ;; Update the summary buffer.
9566         (if (and references
9567                  (equal (message-tokenize-header references " ")
9568                         (message-tokenize-header
9569                          (or (message-fetch-field "references") "") " ")))
9570             ;; We only have to update this line.
9571             (save-excursion
9572               (save-restriction
9573                 (message-narrow-to-head)
9574                 (let ((head (buffer-string))
9575                       header)
9576                   (with-temp-buffer
9577                     (insert (format "211 %d Article retrieved.\n"
9578                                     (cdr gnus-article-current)))
9579                     (insert head)
9580                     (insert ".\n")
9581                     (let ((nntp-server-buffer (current-buffer)))
9582                       (setq header (car (gnus-get-newsgroup-headers
9583                                          nil t))))
9584                     (save-excursion
9585                       (set-buffer gnus-summary-buffer)
9586                       (gnus-data-set-header
9587                        (gnus-data-find (cdr gnus-article-current))
9588                        header)
9589                       (gnus-summary-update-article-line
9590                        (cdr gnus-article-current) header)
9591                       (if (gnus-summary-goto-subject
9592                            (cdr gnus-article-current) nil t)
9593                           (gnus-summary-update-secondary-mark
9594                            (cdr gnus-article-current))))))))
9595           ;; Update threads.
9596           (set-buffer (or buffer gnus-summary-buffer))
9597           (gnus-summary-update-article (cdr gnus-article-current))
9598           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9599               (gnus-summary-update-secondary-mark
9600                (cdr gnus-article-current))))
9601         ;; Prettify the article buffer again.
9602         (unless no-highlight
9603           (save-excursion
9604             (set-buffer gnus-article-buffer)
9605             ;;;!!! Fix this -- article should be rehighlighted.
9606             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9607             (set-buffer gnus-original-article-buffer)
9608             (gnus-request-article
9609              (cdr gnus-article-current)
9610              (car gnus-article-current) (current-buffer))))
9611         ;; Prettify the summary buffer line.
9612         (when (gnus-visual-p 'summary-highlight 'highlight)
9613           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9614
9615 (defun gnus-summary-edit-wash (key)
9616   "Perform editing command KEY in the article buffer."
9617   (interactive
9618    (list
9619     (progn
9620       (message "%s" (concat (this-command-keys) "- "))
9621       (read-char))))
9622   (message "")
9623   (gnus-summary-edit-article)
9624   (execute-kbd-macro (concat (this-command-keys) key))
9625   (gnus-article-edit-done))
9626
9627 ;;; Respooling
9628
9629 (defun gnus-summary-respool-query (&optional silent trace)
9630   "Query where the respool algorithm would put this article."
9631   (interactive)
9632   (let (gnus-mark-article-hook)
9633     (gnus-summary-select-article)
9634     (save-excursion
9635       (set-buffer gnus-original-article-buffer)
9636       (let ((groups (nnmail-article-group 'identity trace)))
9637         (unless silent
9638           (if groups
9639               (message "This message would go to %s"
9640                        (mapconcat 'car groups ", "))
9641             (message "This message would go to no groups"))
9642           groups)))))
9643
9644 (defun gnus-summary-respool-trace ()
9645   "Trace where the respool algorithm would put this article.
9646 Display a buffer showing all fancy splitting patterns which matched."
9647   (interactive)
9648   (gnus-summary-respool-query nil t))
9649
9650 ;; Summary marking commands.
9651
9652 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9653   "Mark articles which has the same subject as read, and then select the next.
9654 If UNMARK is positive, remove any kind of mark.
9655 If UNMARK is negative, tick articles."
9656   (interactive "P")
9657   (when unmark
9658     (setq unmark (prefix-numeric-value unmark)))
9659   (let ((count
9660          (gnus-summary-mark-same-subject
9661           (gnus-summary-article-subject) unmark)))
9662     ;; Select next unread article.  If auto-select-same mode, should
9663     ;; select the first unread article.
9664     (gnus-summary-next-article t (and gnus-auto-select-same
9665                                       (gnus-summary-article-subject)))
9666     (gnus-message 7 "%d article%s marked as %s"
9667                   count (if (= count 1) " is" "s are")
9668                   (if unmark "unread" "read"))))
9669
9670 (defun gnus-summary-kill-same-subject (&optional unmark)
9671   "Mark articles which has the same subject as read.
9672 If UNMARK is positive, remove any kind of mark.
9673 If UNMARK is negative, tick articles."
9674   (interactive "P")
9675   (when unmark
9676     (setq unmark (prefix-numeric-value unmark)))
9677   (let ((count
9678          (gnus-summary-mark-same-subject
9679           (gnus-summary-article-subject) unmark)))
9680     ;; If marked as read, go to next unread subject.
9681     (when (null unmark)
9682       ;; Go to next unread subject.
9683       (gnus-summary-next-subject 1 t))
9684     (gnus-message 7 "%d articles are marked as %s"
9685                   count (if unmark "unread" "read"))))
9686
9687 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9688   "Mark articles with same SUBJECT as read, and return marked number.
9689 If optional argument UNMARK is positive, remove any kinds of marks.
9690 If optional argument UNMARK is negative, mark articles as unread instead."
9691   (let ((count 1))
9692     (save-excursion
9693       (cond
9694        ((null unmark)                   ; Mark as read.
9695         (while (and
9696                 (progn
9697                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9698                   (gnus-summary-show-thread) t)
9699                 (gnus-summary-find-subject subject))
9700           (setq count (1+ count))))
9701        ((> unmark 0)                    ; Tick.
9702         (while (and
9703                 (progn
9704                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9705                   (gnus-summary-show-thread) t)
9706                 (gnus-summary-find-subject subject))
9707           (setq count (1+ count))))
9708        (t                               ; Mark as unread.
9709         (while (and
9710                 (progn
9711                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9712                   (gnus-summary-show-thread) t)
9713                 (gnus-summary-find-subject subject))
9714           (setq count (1+ count)))))
9715       (gnus-set-mode-line 'summary)
9716       ;; Return the number of marked articles.
9717       count)))
9718
9719 (defun gnus-summary-mark-as-processable (n &optional unmark)
9720   "Set the process mark on the next N articles.
9721 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9722 the process mark instead.  The difference between N and the actual
9723 number of articles marked is returned."
9724   (interactive "P")
9725   (if (and (null n) (gnus-region-active-p))
9726       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9727     (setq n (prefix-numeric-value n))
9728     (let ((backward (< n 0))
9729           (n (abs n)))
9730       (while (and
9731               (> n 0)
9732               (if unmark
9733                   (gnus-summary-remove-process-mark
9734                    (gnus-summary-article-number))
9735                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9736               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9737         (setq n (1- n)))
9738       (when (/= 0 n)
9739         (gnus-message 7 "No more articles"))
9740       (gnus-summary-recenter)
9741       (gnus-summary-position-point)
9742       n)))
9743
9744 (defun gnus-summary-unmark-as-processable (n)
9745   "Remove the process mark from the next N articles.
9746 If N is negative, unmark backward instead.  The difference between N and
9747 the actual number of articles unmarked is returned."
9748   (interactive "P")
9749   (gnus-summary-mark-as-processable n t))
9750
9751 (defun gnus-summary-unmark-all-processable ()
9752   "Remove the process mark from all articles."
9753   (interactive)
9754   (save-excursion
9755     (while gnus-newsgroup-processable
9756       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9757   (gnus-summary-position-point))
9758
9759 (defun gnus-summary-add-mark (article type)
9760   "Mark ARTICLE with a mark of TYPE."
9761   (let ((vtype (car (assq type gnus-article-mark-lists)))
9762         var)
9763     (if (not vtype)
9764         (error "No such mark type: %s" type)
9765       (setq var (intern (format "gnus-newsgroup-%s" type)))
9766       (set var (cons article (symbol-value var)))
9767       (if (memq type '(processable cached replied forwarded recent saved))
9768           (gnus-summary-update-secondary-mark article)
9769         ;;; !!! This is bogus.  We should find out what primary
9770         ;;; !!! mark we want to set.
9771         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9772
9773 (defun gnus-summary-mark-as-expirable (n)
9774   "Mark N articles forward as expirable.
9775 If N is negative, mark backward instead.  The difference between N and
9776 the actual number of articles marked is returned."
9777   (interactive "p")
9778   (gnus-summary-mark-forward n gnus-expirable-mark))
9779
9780 (defun gnus-summary-mark-as-spam (n)
9781   "Mark N articles forward as spam.
9782 If N is negative, mark backward instead.  The difference between N and
9783 the actual number of articles marked is returned."
9784   (interactive "p")
9785   (gnus-summary-mark-forward n gnus-spam-mark))
9786
9787 (defun gnus-summary-mark-article-as-replied (article)
9788   "Mark ARTICLE as replied to and update the summary line.
9789 ARTICLE can also be a list of articles."
9790   (interactive (list (gnus-summary-article-number)))
9791   (let ((articles (if (listp article) article (list article))))
9792     (dolist (article articles)
9793       (unless (numberp article)
9794         (error "%s is not a number" article))
9795       (push article gnus-newsgroup-replied)
9796       (let ((buffer-read-only nil))
9797         (when (gnus-summary-goto-subject article nil t)
9798           (gnus-summary-update-secondary-mark article))))))
9799
9800 (defun gnus-summary-mark-article-as-forwarded (article)
9801   "Mark ARTICLE as forwarded and update the summary line.
9802 ARTICLE can also be a list of articles."
9803   (let ((articles (if (listp article) article (list article))))
9804     (dolist (article articles)
9805       (push article gnus-newsgroup-forwarded)
9806       (let ((buffer-read-only nil))
9807         (when (gnus-summary-goto-subject article nil t)
9808           (gnus-summary-update-secondary-mark article))))))
9809
9810 (defun gnus-summary-set-bookmark (article)
9811   "Set a bookmark in current article."
9812   (interactive (list (gnus-summary-article-number)))
9813   (when (or (not (get-buffer gnus-article-buffer))
9814             (not gnus-current-article)
9815             (not gnus-article-current)
9816             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9817     (error "No current article selected"))
9818   ;; Remove old bookmark, if one exists.
9819   (gnus-pull article gnus-newsgroup-bookmarks)
9820   ;; Set the new bookmark, which is on the form
9821   ;; (article-number . line-number-in-body).
9822   (push
9823    (cons article
9824          (with-current-buffer gnus-article-buffer
9825            (count-lines
9826             (min (point)
9827                  (save-excursion
9828                    (article-goto-body)
9829                    (point)))
9830             (point))))
9831    gnus-newsgroup-bookmarks)
9832   (gnus-message 6 "A bookmark has been added to the current article."))
9833
9834 (defun gnus-summary-remove-bookmark (article)
9835   "Remove the bookmark from the current article."
9836   (interactive (list (gnus-summary-article-number)))
9837   ;; Remove old bookmark, if one exists.
9838   (if (not (assq article gnus-newsgroup-bookmarks))
9839       (gnus-message 6 "No bookmark in current article.")
9840     (gnus-pull article gnus-newsgroup-bookmarks)
9841     (gnus-message 6 "Removed bookmark.")))
9842
9843 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9844 (defun gnus-summary-mark-as-dormant (n)
9845   "Mark N articles forward as dormant.
9846 If N is negative, mark backward instead.  The difference between N and
9847 the actual number of articles marked is returned."
9848   (interactive "p")
9849   (gnus-summary-mark-forward n gnus-dormant-mark))
9850
9851 (defun gnus-summary-set-process-mark (article)
9852   "Set the process mark on ARTICLE and update the summary line."
9853   (setq gnus-newsgroup-processable
9854         (cons article
9855               (delq article gnus-newsgroup-processable)))
9856   (when (gnus-summary-goto-subject article)
9857     (gnus-summary-show-thread)
9858     (gnus-summary-goto-subject article)
9859     (gnus-summary-update-secondary-mark article)))
9860
9861 (defun gnus-summary-remove-process-mark (article)
9862   "Remove the process mark from ARTICLE and update the summary line."
9863   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9864   (when (gnus-summary-goto-subject article)
9865     (gnus-summary-show-thread)
9866     (gnus-summary-goto-subject article)
9867     (gnus-summary-update-secondary-mark article)))
9868
9869 (defun gnus-summary-set-saved-mark (article)
9870   "Set the process mark on ARTICLE and update the summary line."
9871   (push article gnus-newsgroup-saved)
9872   (when (gnus-summary-goto-subject article)
9873     (gnus-summary-update-secondary-mark article)))
9874
9875 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9876   "Mark N articles as read forwards.
9877 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9878 The difference between N and the actual number of articles marked is
9879 returned.
9880 If NO-EXPIRE, auto-expiry will be inhibited."
9881   (interactive "p")
9882   (gnus-summary-show-thread)
9883   (let ((backward (< n 0))
9884         (gnus-summary-goto-unread
9885          (and gnus-summary-goto-unread
9886               (not (eq gnus-summary-goto-unread 'never))
9887               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9888                                     gnus-ticked-mark gnus-dormant-mark)))))
9889         (n (abs n))
9890         (mark (or mark gnus-del-mark)))
9891     (while (and (> n 0)
9892                 (gnus-summary-mark-article nil mark no-expire)
9893                 (zerop (gnus-summary-next-subject
9894                         (if backward -1 1)
9895                         (and gnus-summary-goto-unread
9896                              (not (eq gnus-summary-goto-unread 'never)))
9897                         t)))
9898       (setq n (1- n)))
9899     (when (/= 0 n)
9900       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9901     (gnus-summary-recenter)
9902     (gnus-summary-position-point)
9903     (gnus-set-mode-line 'summary)
9904     n))
9905
9906 (defun gnus-summary-mark-article-as-read (mark)
9907   "Mark the current article quickly as read with MARK."
9908   (let ((article (gnus-summary-article-number)))
9909     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9910     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9911     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9912     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9913     (push (cons article mark) gnus-newsgroup-reads)
9914     ;; Possibly remove from cache, if that is used.
9915     (when gnus-use-cache
9916       (gnus-cache-enter-remove-article article))
9917     ;; Allow the backend to change the mark.
9918     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9919     ;; Check for auto-expiry.
9920     (when (and gnus-newsgroup-auto-expire
9921                (memq mark gnus-auto-expirable-marks))
9922       (setq mark gnus-expirable-mark)
9923       ;; Let the backend know about the mark change.
9924       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9925       (push article gnus-newsgroup-expirable))
9926     ;; Set the mark in the buffer.
9927     (gnus-summary-update-mark mark 'unread)
9928     t))
9929
9930 (defun gnus-summary-mark-article-as-unread (mark)
9931   "Mark the current article quickly as unread with MARK."
9932   (let* ((article (gnus-summary-article-number))
9933          (old-mark (gnus-summary-article-mark article)))
9934     ;; Allow the backend to change the mark.
9935     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9936     (if (eq mark old-mark)
9937         t
9938       (if (<= article 0)
9939           (progn
9940             (gnus-error 1 "Can't mark negative article numbers")
9941             nil)
9942         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9943         (setq gnus-newsgroup-spam-marked
9944               (delq article gnus-newsgroup-spam-marked))
9945         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9946         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9947         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9948         (cond ((= mark gnus-ticked-mark)
9949                (setq gnus-newsgroup-marked
9950                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9951                                               article)))
9952               ((= mark gnus-spam-mark)
9953                (setq gnus-newsgroup-spam-marked
9954                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9955                                               article)))
9956               ((= mark gnus-dormant-mark)
9957                (setq gnus-newsgroup-dormant
9958                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9959                                               article)))
9960               (t
9961                (setq gnus-newsgroup-unreads
9962                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9963                                               article))))
9964         (gnus-pull article gnus-newsgroup-reads)
9965
9966         ;; See whether the article is to be put in the cache.
9967         (and gnus-use-cache
9968              (vectorp (gnus-summary-article-header article))
9969              (save-excursion
9970                (gnus-cache-possibly-enter-article
9971                 gnus-newsgroup-name article
9972                 (= mark gnus-ticked-mark)
9973                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9974
9975         ;; Fix the mark.
9976         (gnus-summary-update-mark mark 'unread)
9977         t))))
9978
9979 (defun gnus-summary-mark-article (&optional article mark no-expire)
9980   "Mark ARTICLE with MARK.  MARK can be any character.
9981 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9982 `??' (dormant) and `?E' (expirable).
9983 If MARK is nil, then the default character `?r' is used.
9984 If ARTICLE is nil, then the article on the current line will be
9985 marked.
9986 If NO-EXPIRE, auto-expiry will be inhibited."
9987   ;; The mark might be a string.
9988   (when (stringp mark)
9989     (setq mark (aref mark 0)))
9990   ;; If no mark is given, then we check auto-expiring.
9991   (when (null mark)
9992     (setq mark gnus-del-mark))
9993   (when (and (not no-expire)
9994              gnus-newsgroup-auto-expire
9995              (memq mark gnus-auto-expirable-marks))
9996     (setq mark gnus-expirable-mark))
9997   (let ((article (or article (gnus-summary-article-number)))
9998         (old-mark (gnus-summary-article-mark article)))
9999     ;; Allow the backend to change the mark.
10000     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10001     (if (eq mark old-mark)
10002         t
10003       (unless article
10004         (error "No article on current line"))
10005       (if (not (if (or (= mark gnus-unread-mark)
10006                        (= mark gnus-ticked-mark)
10007                        (= mark gnus-spam-mark)
10008                        (= mark gnus-dormant-mark))
10009                    (gnus-mark-article-as-unread article mark)
10010                  (gnus-mark-article-as-read article mark)))
10011           t
10012         ;; See whether the article is to be put in the cache.
10013         (and gnus-use-cache
10014              (not (= mark gnus-canceled-mark))
10015              (vectorp (gnus-summary-article-header article))
10016              (save-excursion
10017                (gnus-cache-possibly-enter-article
10018                 gnus-newsgroup-name article
10019                 (= mark gnus-ticked-mark)
10020                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10021
10022         (when (gnus-summary-goto-subject article nil t)
10023           (let ((buffer-read-only nil))
10024             (gnus-summary-show-thread)
10025             ;; Fix the mark.
10026             (gnus-summary-update-mark mark 'unread)
10027             t))))))
10028
10029 (defun gnus-summary-update-secondary-mark (article)
10030   "Update the secondary (read, process, cache) mark."
10031   (gnus-summary-update-mark
10032    (cond ((memq article gnus-newsgroup-processable)
10033           gnus-process-mark)
10034          ((memq article gnus-newsgroup-cached)
10035           gnus-cached-mark)
10036          ((memq article gnus-newsgroup-replied)
10037           gnus-replied-mark)
10038          ((memq article gnus-newsgroup-forwarded)
10039           gnus-forwarded-mark)
10040          ((memq article gnus-newsgroup-saved)
10041           gnus-saved-mark)
10042          ((memq article gnus-newsgroup-recent)
10043           gnus-recent-mark)
10044          ((memq article gnus-newsgroup-unseen)
10045           gnus-unseen-mark)
10046          (t gnus-no-mark))
10047    'replied)
10048   (when (gnus-visual-p 'summary-highlight 'highlight)
10049     (gnus-run-hooks 'gnus-summary-update-hook))
10050   t)
10051
10052 (defun gnus-summary-update-download-mark (article)
10053   "Update the download mark."
10054   (gnus-summary-update-mark
10055    (cond ((memq article gnus-newsgroup-undownloaded)
10056           gnus-undownloaded-mark)
10057          (gnus-newsgroup-agentized
10058           gnus-downloaded-mark)
10059          (t
10060           gnus-no-mark))
10061    'download)
10062   (gnus-summary-update-line t)
10063   t)
10064
10065 (defun gnus-summary-update-mark (mark type)
10066   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10067         (buffer-read-only nil))
10068     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10069     (when forward
10070       (when (looking-at "\r")
10071         (incf forward))
10072       (when (<= (+ forward (point)) (point-max))
10073         ;; Go to the right position on the line.
10074         (goto-char (+ forward (point)))
10075         ;; Replace the old mark with the new mark.
10076         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10077         ;; Optionally update the marks by some user rule.
10078         (when (eq type 'unread)
10079           (gnus-data-set-mark
10080            (gnus-data-find (gnus-summary-article-number)) mark)
10081           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10082
10083 (defun gnus-mark-article-as-read (article &optional mark)
10084   "Enter ARTICLE in the pertinent lists and remove it from others."
10085   ;; Make the article expirable.
10086   (let ((mark (or mark gnus-del-mark)))
10087     (setq gnus-newsgroup-expirable
10088           (if (= mark gnus-expirable-mark)
10089               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10090             (delq article gnus-newsgroup-expirable)))
10091     ;; Remove from unread and marked lists.
10092     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10093     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10094     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10095     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10096     (push (cons article mark) gnus-newsgroup-reads)
10097     ;; Possibly remove from cache, if that is used.
10098     (when gnus-use-cache
10099       (gnus-cache-enter-remove-article article))
10100     t))
10101
10102 (defun gnus-mark-article-as-unread (article &optional mark)
10103   "Enter ARTICLE in the pertinent lists and remove it from others."
10104   (let ((mark (or mark gnus-ticked-mark)))
10105     (if (<= article 0)
10106         (progn
10107           (gnus-error 1 "Can't mark negative article numbers")
10108           nil)
10109       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10110             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10111             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10112             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10113             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10114
10115       ;; Unsuppress duplicates?
10116       (when gnus-suppress-duplicates
10117         (gnus-dup-unsuppress-article article))
10118
10119       (cond ((= mark gnus-ticked-mark)
10120              (setq gnus-newsgroup-marked
10121                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10122             ((= mark gnus-spam-mark)
10123              (setq gnus-newsgroup-spam-marked
10124                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10125                                             article)))
10126             ((= mark gnus-dormant-mark)
10127              (setq gnus-newsgroup-dormant
10128                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10129             (t
10130              (setq gnus-newsgroup-unreads
10131                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10132       (gnus-pull article gnus-newsgroup-reads)
10133       t)))
10134
10135 (defalias 'gnus-summary-mark-as-unread-forward
10136   'gnus-summary-tick-article-forward)
10137 (make-obsolete 'gnus-summary-mark-as-unread-forward
10138                'gnus-summary-tick-article-forward)
10139 (defun gnus-summary-tick-article-forward (n)
10140   "Tick N articles forwards.
10141 If N is negative, tick backwards instead.
10142 The difference between N and the number of articles ticked is returned."
10143   (interactive "p")
10144   (gnus-summary-mark-forward n gnus-ticked-mark))
10145
10146 (defalias 'gnus-summary-mark-as-unread-backward
10147   'gnus-summary-tick-article-backward)
10148 (make-obsolete 'gnus-summary-mark-as-unread-backward
10149                'gnus-summary-tick-article-backward)
10150 (defun gnus-summary-tick-article-backward (n)
10151   "Tick N articles backwards.
10152 The difference between N and the number of articles ticked is returned."
10153   (interactive "p")
10154   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10155
10156 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10157 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10158 (defun gnus-summary-tick-article (&optional article clear-mark)
10159   "Mark current article as unread.
10160 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10161 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10162   (interactive)
10163   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10164                                        gnus-ticked-mark)))
10165
10166 (defun gnus-summary-mark-as-read-forward (n)
10167   "Mark N articles as read forwards.
10168 If N is negative, mark backwards instead.
10169 The difference between N and the actual number of articles marked is
10170 returned."
10171   (interactive "p")
10172   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10173
10174 (defun gnus-summary-mark-as-read-backward (n)
10175   "Mark the N articles as read backwards.
10176 The difference between N and the actual number of articles marked is
10177 returned."
10178   (interactive "p")
10179   (gnus-summary-mark-forward
10180    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10181
10182 (defun gnus-summary-mark-as-read (&optional article mark)
10183   "Mark current article as read.
10184 ARTICLE specifies the article to be marked as read.
10185 MARK specifies a string to be inserted at the beginning of the line."
10186   (gnus-summary-mark-article article mark))
10187
10188 (defun gnus-summary-clear-mark-forward (n)
10189   "Clear marks from N articles forward.
10190 If N is negative, clear backward instead.
10191 The difference between N and the number of marks cleared is returned."
10192   (interactive "p")
10193   (gnus-summary-mark-forward n gnus-unread-mark))
10194
10195 (defun gnus-summary-clear-mark-backward (n)
10196   "Clear marks from N articles backward.
10197 The difference between N and the number of marks cleared is returned."
10198   (interactive "p")
10199   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10200
10201 (defun gnus-summary-mark-unread-as-read ()
10202   "Intended to be used by `gnus-summary-mark-article-hook'."
10203   (when (memq gnus-current-article gnus-newsgroup-unreads)
10204     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10205
10206 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10207   "Intended to be used by `gnus-summary-mark-article-hook'."
10208   (let ((mark (gnus-summary-article-mark)))
10209     (when (or (gnus-unread-mark-p mark)
10210               (gnus-read-mark-p mark))
10211       (gnus-summary-mark-article gnus-current-article
10212                                  (or new-mark gnus-read-mark)))))
10213
10214 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10215   "Intended to be used by `gnus-summary-mark-article-hook'."
10216   (let ((mark (gnus-summary-article-mark)))
10217     (when (or (gnus-unread-mark-p mark)
10218               (gnus-read-mark-p mark))
10219       (gnus-summary-mark-article (gnus-summary-article-number)
10220                                  (or new-mark gnus-read-mark)))))
10221
10222 (defun gnus-summary-mark-unread-as-ticked ()
10223   "Intended to be used by `gnus-summary-mark-article-hook'."
10224   (when (memq gnus-current-article gnus-newsgroup-unreads)
10225     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10226
10227 (defun gnus-summary-mark-region-as-read (point mark all)
10228   "Mark all unread articles between point and mark as read.
10229 If given a prefix, mark all articles between point and mark as read,
10230 even ticked and dormant ones."
10231   (interactive "r\nP")
10232   (save-excursion
10233     (let (article)
10234       (goto-char point)
10235       (beginning-of-line)
10236       (while (and
10237               (< (point) mark)
10238               (progn
10239                 (when (or all
10240                           (memq (setq article (gnus-summary-article-number))
10241                                 gnus-newsgroup-unreads))
10242                   (gnus-summary-mark-article article gnus-del-mark))
10243                 t)
10244               (gnus-summary-find-next))))))
10245
10246 (defun gnus-summary-mark-below (score mark)
10247   "Mark articles with score less than SCORE with MARK."
10248   (interactive "P\ncMark: ")
10249   (setq score (if score
10250                   (prefix-numeric-value score)
10251                 (or gnus-summary-default-score 0)))
10252   (save-excursion
10253     (set-buffer gnus-summary-buffer)
10254     (goto-char (point-min))
10255     (while
10256         (progn
10257           (and (< (gnus-summary-article-score) score)
10258                (gnus-summary-mark-article nil mark))
10259           (gnus-summary-find-next)))))
10260
10261 (defun gnus-summary-kill-below (&optional score)
10262   "Mark articles with score below SCORE as read."
10263   (interactive "P")
10264   (gnus-summary-mark-below score gnus-killed-mark))
10265
10266 (defun gnus-summary-clear-above (&optional score)
10267   "Clear all marks from articles with score above SCORE."
10268   (interactive "P")
10269   (gnus-summary-mark-above score gnus-unread-mark))
10270
10271 (defun gnus-summary-tick-above (&optional score)
10272   "Tick all articles with score above SCORE."
10273   (interactive "P")
10274   (gnus-summary-mark-above score gnus-ticked-mark))
10275
10276 (defun gnus-summary-mark-above (score mark)
10277   "Mark articles with score over SCORE with MARK."
10278   (interactive "P\ncMark: ")
10279   (setq score (if score
10280                   (prefix-numeric-value score)
10281                 (or gnus-summary-default-score 0)))
10282   (save-excursion
10283     (set-buffer gnus-summary-buffer)
10284     (goto-char (point-min))
10285     (while (and (progn
10286                   (when (> (gnus-summary-article-score) score)
10287                     (gnus-summary-mark-article nil mark))
10288                   t)
10289                 (gnus-summary-find-next)))))
10290
10291 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10292 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10293 (defun gnus-summary-limit-include-expunged (&optional no-error)
10294   "Display all the hidden articles that were expunged for low scores."
10295   (interactive)
10296   (let ((buffer-read-only nil))
10297     (let ((scored gnus-newsgroup-scored)
10298           headers h)
10299       (while scored
10300         (unless (gnus-summary-article-header (caar scored))
10301           (and (setq h (gnus-number-to-header (caar scored)))
10302                (< (cdar scored) gnus-summary-expunge-below)
10303                (push h headers)))
10304         (setq scored (cdr scored)))
10305       (if (not headers)
10306           (when (not no-error)
10307             (error "No expunged articles hidden"))
10308         (goto-char (point-min))
10309         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10310         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10311         (mapcar (lambda (x) (push (mail-header-number x)
10312                                   gnus-newsgroup-limit))
10313                 headers)
10314         (gnus-summary-prepare-unthreaded (nreverse headers))
10315         (goto-char (point-min))
10316         (gnus-summary-position-point)
10317         t))))
10318
10319 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10320   "Mark all unread articles in this newsgroup as read.
10321 If prefix argument ALL is non-nil, ticked and dormant articles will
10322 also be marked as read.
10323 If QUIETLY is non-nil, no questions will be asked.
10324
10325 If TO-HERE is non-nil, it should be a point in the buffer.  All
10326 articles before (after, if REVERSE is set) this point will be marked
10327 as read.
10328
10329 Note that this function will only catch up the unread article
10330 in the current summary buffer limitation.
10331
10332 The number of articles marked as read is returned."
10333   (interactive "P")
10334   (prog1
10335       (save-excursion
10336         (when (or quietly
10337                   (not gnus-interactive-catchup) ;Without confirmation?
10338                   gnus-expert-user
10339                   (gnus-y-or-n-p
10340                    (if all
10341                        "Mark absolutely all articles as read? "
10342                      "Mark all unread articles as read? ")))
10343           (if (and not-mark
10344                    (not gnus-newsgroup-adaptive)
10345                    (not gnus-newsgroup-auto-expire)
10346                    (not gnus-suppress-duplicates)
10347                    (or (not gnus-use-cache)
10348                        (eq gnus-use-cache 'passive)))
10349               (progn
10350                 (when all
10351                   (setq gnus-newsgroup-marked nil
10352                         gnus-newsgroup-spam-marked nil
10353                         gnus-newsgroup-dormant nil))
10354                 (setq gnus-newsgroup-unreads
10355                       (gnus-sorted-nunion
10356                        (gnus-intersection gnus-newsgroup-unreads
10357                                           gnus-newsgroup-downloadable)
10358                        gnus-newsgroup-unfetched)))
10359             ;; We actually mark all articles as canceled, which we
10360             ;; have to do when using auto-expiry or adaptive scoring.
10361             (gnus-summary-show-all-threads)
10362             (if (and to-here reverse)
10363                 (progn
10364                   (goto-char to-here)
10365                   (gnus-summary-mark-current-read-and-unread-as-read
10366                    gnus-catchup-mark)
10367                   (while (gnus-summary-find-next (not all))
10368                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10369               (when (gnus-summary-first-subject (not all))
10370                 (while (and
10371                         (if to-here (< (point) to-here) t)
10372                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10373                         (gnus-summary-find-next (not all))))))
10374             (gnus-set-mode-line 'summary))
10375           t))
10376     (gnus-summary-position-point)))
10377
10378 (defun gnus-summary-catchup-to-here (&optional all)
10379   "Mark all unticked articles before the current one as read.
10380 If ALL is non-nil, also mark ticked and dormant articles as read."
10381   (interactive "P")
10382   (save-excursion
10383     (gnus-save-hidden-threads
10384       (let ((beg (point)))
10385         ;; We check that there are unread articles.
10386         (when (or all (gnus-summary-find-prev))
10387           (gnus-summary-catchup all t beg)))))
10388   (gnus-summary-position-point))
10389
10390 (defun gnus-summary-catchup-from-here (&optional all)
10391   "Mark all unticked articles after (and including) the current one as read.
10392 If ALL is non-nil, also mark ticked and dormant articles as read."
10393   (interactive "P")
10394   (save-excursion
10395     (gnus-save-hidden-threads
10396       (let ((beg (point)))
10397         ;; We check that there are unread articles.
10398         (when (or all (gnus-summary-find-next))
10399           (gnus-summary-catchup all t beg nil t)))))
10400   (gnus-summary-position-point))
10401
10402 (defun gnus-summary-catchup-all (&optional quietly)
10403   "Mark all articles in this newsgroup as read.
10404 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10405 instead, which marks only unread articles as read."
10406   (interactive "P")
10407   (gnus-summary-catchup t quietly))
10408
10409 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10410   "Mark all unread articles in this group as read, then exit.
10411 If prefix argument ALL is non-nil, all articles are marked as read.
10412 If QUIETLY is non-nil, no questions will be asked."
10413   (interactive "P")
10414   (when (gnus-summary-catchup all quietly nil 'fast)
10415     ;; Select next newsgroup or exit.
10416     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10417              (eq gnus-auto-select-next 'quietly))
10418         (gnus-summary-next-group nil)
10419       (gnus-summary-exit))))
10420
10421 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10422   "Mark all articles in this newsgroup as read, and then exit.
10423 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10424 instead, which marks only unread articles as read."
10425   (interactive "P")
10426   (gnus-summary-catchup-and-exit t quietly))
10427
10428 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10429   "Mark all articles in this group as read and select the next group.
10430 If given a prefix, mark all articles, unread as well as ticked, as
10431 read."
10432   (interactive "P")
10433   (save-excursion
10434     (gnus-summary-catchup all))
10435   (gnus-summary-next-group))
10436
10437 ;;;
10438 ;;; with article
10439 ;;;
10440
10441 (defmacro gnus-with-article (article &rest forms)
10442   "Select ARTICLE and perform FORMS in the original article buffer.
10443 Then replace the article with the result."
10444   `(progn
10445      ;; We don't want the article to be marked as read.
10446      (let (gnus-mark-article-hook)
10447        (gnus-summary-select-article t t nil ,article))
10448      (set-buffer gnus-original-article-buffer)
10449      ,@forms
10450      (if (not (gnus-check-backend-function
10451                'request-replace-article (car gnus-article-current)))
10452          (gnus-message 5 "Read-only group; not replacing")
10453        (unless (gnus-request-replace-article
10454                 ,article (car gnus-article-current)
10455                 (current-buffer) t)
10456          (error "Couldn't replace article")))
10457      ;; The cache and backlog have to be flushed somewhat.
10458      (when gnus-keep-backlog
10459        (gnus-backlog-remove-article
10460         (car gnus-article-current) (cdr gnus-article-current)))
10461      (when gnus-use-cache
10462        (gnus-cache-update-article
10463         (car gnus-article-current) (cdr gnus-article-current)))))
10464
10465 (put 'gnus-with-article 'lisp-indent-function 1)
10466 (put 'gnus-with-article 'edebug-form-spec '(form body))
10467
10468 ;; Thread-based commands.
10469
10470 (defun gnus-summary-articles-in-thread (&optional article)
10471   "Return a list of all articles in the current thread.
10472 If ARTICLE is non-nil, return all articles in the thread that starts
10473 with that article."
10474   (let* ((article (or article (gnus-summary-article-number)))
10475          (data (gnus-data-find-list article))
10476          (top-level (gnus-data-level (car data)))
10477          (top-subject
10478           (cond ((null gnus-thread-operation-ignore-subject)
10479                  (gnus-simplify-subject-re
10480                   (mail-header-subject (gnus-data-header (car data)))))
10481                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10482                  (gnus-simplify-subject-fuzzy
10483                   (mail-header-subject (gnus-data-header (car data)))))
10484                 (t nil)))
10485          (end-point (save-excursion
10486                       (if (gnus-summary-go-to-next-thread)
10487                           (point) (point-max))))
10488          articles)
10489     (while (and data
10490                 (< (gnus-data-pos (car data)) end-point))
10491       (when (or (not top-subject)
10492                 (string= top-subject
10493                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10494                              (gnus-simplify-subject-fuzzy
10495                               (mail-header-subject
10496                                (gnus-data-header (car data))))
10497                            (gnus-simplify-subject-re
10498                             (mail-header-subject
10499                              (gnus-data-header (car data)))))))
10500         (push (gnus-data-number (car data)) articles))
10501       (unless (and (setq data (cdr data))
10502                    (> (gnus-data-level (car data)) top-level))
10503         (setq data nil)))
10504     ;; Return the list of articles.
10505     (nreverse articles)))
10506
10507 (defun gnus-summary-rethread-current ()
10508   "Rethread the thread the current article is part of."
10509   (interactive)
10510   (let* ((gnus-show-threads t)
10511          (article (gnus-summary-article-number))
10512          (id (mail-header-id (gnus-summary-article-header)))
10513          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10514     (unless id
10515       (error "No article on the current line"))
10516     (gnus-rebuild-thread id)
10517     (gnus-summary-goto-subject article)))
10518
10519 (defun gnus-summary-reparent-thread ()
10520   "Make the current article child of the marked (or previous) article.
10521
10522 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10523 is non-nil or the Subject: of both articles are the same."
10524   (interactive)
10525   (unless (not (gnus-group-read-only-p))
10526     (error "The current newsgroup does not support article editing"))
10527   (unless (<= (length gnus-newsgroup-processable) 1)
10528     (error "No more than one article may be marked"))
10529   (save-window-excursion
10530     (let ((gnus-article-buffer " *reparent*")
10531           (current-article (gnus-summary-article-number))
10532           ;; First grab the marked article, otherwise one line up.
10533           (parent-article (if (not (null gnus-newsgroup-processable))
10534                               (car gnus-newsgroup-processable)
10535                             (save-excursion
10536                               (if (eq (forward-line -1) 0)
10537                                   (gnus-summary-article-number)
10538                                 (error "Beginning of summary buffer"))))))
10539       (unless (not (eq current-article parent-article))
10540         (error "An article may not be self-referential"))
10541       (let ((message-id (mail-header-id
10542                          (gnus-summary-article-header parent-article))))
10543         (unless (and message-id (not (equal message-id "")))
10544           (error "No message-id in desired parent"))
10545         (gnus-with-article current-article
10546           (save-restriction
10547             (goto-char (point-min))
10548             (message-narrow-to-head)
10549             (if (re-search-forward "^References: " nil t)
10550                 (progn
10551                   (re-search-forward "^[^ \t]" nil t)
10552                   (forward-line -1)
10553                   (end-of-line)
10554                   (insert " " message-id))
10555               (insert "References: " message-id "\n"))))
10556         (set-buffer gnus-summary-buffer)
10557         (gnus-summary-unmark-all-processable)
10558         (gnus-summary-update-article current-article)
10559         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10560             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10561         (gnus-summary-rethread-current)
10562         (gnus-message 3 "Article %d is now the child of article %d"
10563                       current-article parent-article)))))
10564
10565 (defun gnus-summary-toggle-threads (&optional arg)
10566   "Toggle showing conversation threads.
10567 If ARG is positive number, turn showing conversation threads on."
10568   (interactive "P")
10569   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10570     (setq gnus-show-threads
10571           (if (null arg) (not gnus-show-threads)
10572             (> (prefix-numeric-value arg) 0)))
10573     (gnus-summary-prepare)
10574     (gnus-summary-goto-subject current)
10575     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10576     (gnus-summary-position-point)))
10577
10578 (defun gnus-summary-show-all-threads ()
10579   "Show all threads."
10580   (interactive)
10581   (save-excursion
10582     (let ((buffer-read-only nil))
10583       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10584   (gnus-summary-position-point))
10585
10586 (defun gnus-summary-show-thread ()
10587   "Show thread subtrees.
10588 Returns nil if no thread was there to be shown."
10589   (interactive)
10590   (let ((buffer-read-only nil)
10591         (orig (point))
10592         (end (point-at-eol))
10593         ;; Leave point at bol
10594         (beg (progn (beginning-of-line) (point))))
10595     (prog1
10596         ;; Any hidden lines here?
10597         (search-forward "\r" end t)
10598       (subst-char-in-region beg end ?\^M ?\n t)
10599       (goto-char orig)
10600       (gnus-summary-position-point))))
10601
10602 (defun gnus-summary-maybe-hide-threads ()
10603   "If requested, hide the threads that should be hidden."
10604   (when (and gnus-show-threads
10605              gnus-thread-hide-subtree)
10606     (gnus-summary-hide-all-threads
10607      (if (or (consp gnus-thread-hide-subtree)
10608              (functionp gnus-thread-hide-subtree))
10609          (gnus-make-predicate gnus-thread-hide-subtree)
10610        nil))))
10611
10612 ;;; Hiding predicates.
10613
10614 (defun gnus-article-unread-p (header)
10615   (memq (mail-header-number header) gnus-newsgroup-unreads))
10616
10617 (defun gnus-article-unseen-p (header)
10618   (memq (mail-header-number header) gnus-newsgroup-unseen))
10619
10620 (defun gnus-map-articles (predicate articles)
10621   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10622   (apply 'gnus-or (mapcar predicate
10623                           (mapcar 'gnus-summary-article-header articles))))
10624
10625 (defun gnus-summary-hide-all-threads (&optional predicate)
10626   "Hide all thread subtrees.
10627 If PREDICATE is supplied, threads that satisfy this predicate
10628 will not be hidden."
10629   (interactive)
10630   (save-excursion
10631     (goto-char (point-min))
10632     (let ((end nil))
10633       (while (not end)
10634         (when (or (not predicate)
10635                   (gnus-map-articles
10636                    predicate (gnus-summary-article-children)))
10637             (gnus-summary-hide-thread))
10638         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10639   (gnus-summary-position-point))
10640
10641 (defun gnus-summary-hide-thread ()
10642   "Hide thread subtrees.
10643 If PREDICATE is supplied, threads that satisfy this predicate
10644 will not be hidden.
10645 Returns nil if no threads were there to be hidden."
10646   (interactive)
10647   (let ((buffer-read-only nil)
10648         (start (point))
10649         (article (gnus-summary-article-number)))
10650     (goto-char start)
10651     ;; Go forward until either the buffer ends or the subthread
10652     ;; ends.
10653     (when (and (not (eobp))
10654                (or (zerop (gnus-summary-next-thread 1 t))
10655                    (goto-char (point-max))))
10656       (prog1
10657           (if (and (> (point) start)
10658                    (search-backward "\n" start t))
10659               (progn
10660                 (subst-char-in-region start (point) ?\n ?\^M)
10661                 (gnus-summary-goto-subject article))
10662             (goto-char start)
10663             nil)))))
10664
10665 (defun gnus-summary-go-to-next-thread (&optional previous)
10666   "Go to the same level (or less) next thread.
10667 If PREVIOUS is non-nil, go to previous thread instead.
10668 Return the article number moved to, or nil if moving was impossible."
10669   (let ((level (gnus-summary-thread-level))
10670         (way (if previous -1 1))
10671         (beg (point)))
10672     (forward-line way)
10673     (while (and (not (eobp))
10674                 (< level (gnus-summary-thread-level)))
10675       (forward-line way))
10676     (if (eobp)
10677         (progn
10678           (goto-char beg)
10679           nil)
10680       (setq beg (point))
10681       (prog1
10682           (gnus-summary-article-number)
10683         (goto-char beg)))))
10684
10685 (defun gnus-summary-next-thread (n &optional silent)
10686   "Go to the same level next N'th thread.
10687 If N is negative, search backward instead.
10688 Returns the difference between N and the number of skips actually
10689 done.
10690
10691 If SILENT, don't output messages."
10692   (interactive "p")
10693   (let ((backward (< n 0))
10694         (n (abs n)))
10695     (while (and (> n 0)
10696                 (gnus-summary-go-to-next-thread backward))
10697       (decf n))
10698     (unless silent
10699       (gnus-summary-position-point))
10700     (when (and (not silent) (/= 0 n))
10701       (gnus-message 7 "No more threads"))
10702     n))
10703
10704 (defun gnus-summary-prev-thread (n)
10705   "Go to the same level previous N'th thread.
10706 Returns the difference between N and the number of skips actually
10707 done."
10708   (interactive "p")
10709   (gnus-summary-next-thread (- n)))
10710
10711 (defun gnus-summary-go-down-thread ()
10712   "Go down one level in the current thread."
10713   (let ((children (gnus-summary-article-children)))
10714     (when children
10715       (gnus-summary-goto-subject (car children)))))
10716
10717 (defun gnus-summary-go-up-thread ()
10718   "Go up one level in the current thread."
10719   (let ((parent (gnus-summary-article-parent)))
10720     (when parent
10721       (gnus-summary-goto-subject parent))))
10722
10723 (defun gnus-summary-down-thread (n)
10724   "Go down thread N steps.
10725 If N is negative, go up instead.
10726 Returns the difference between N and how many steps down that were
10727 taken."
10728   (interactive "p")
10729   (let ((up (< n 0))
10730         (n (abs n)))
10731     (while (and (> n 0)
10732                 (if up (gnus-summary-go-up-thread)
10733                   (gnus-summary-go-down-thread)))
10734       (setq n (1- n)))
10735     (gnus-summary-position-point)
10736     (when (/= 0 n)
10737       (gnus-message 7 "Can't go further"))
10738     n))
10739
10740 (defun gnus-summary-up-thread (n)
10741   "Go up thread N steps.
10742 If N is negative, go down instead.
10743 Returns the difference between N and how many steps down that were
10744 taken."
10745   (interactive "p")
10746   (gnus-summary-down-thread (- n)))
10747
10748 (defun gnus-summary-top-thread ()
10749   "Go to the top of the thread."
10750   (interactive)
10751   (while (gnus-summary-go-up-thread))
10752   (gnus-summary-article-number))
10753
10754 (defun gnus-summary-kill-thread (&optional unmark)
10755   "Mark articles under current thread as read.
10756 If the prefix argument is positive, remove any kinds of marks.
10757 If the prefix argument is negative, tick articles instead."
10758   (interactive "P")
10759   (when unmark
10760     (setq unmark (prefix-numeric-value unmark)))
10761   (let ((articles (gnus-summary-articles-in-thread)))
10762     (save-excursion
10763       ;; Expand the thread.
10764       (gnus-summary-show-thread)
10765       ;; Mark all the articles.
10766       (while articles
10767         (gnus-summary-goto-subject (car articles))
10768         (cond ((null unmark)
10769                (gnus-summary-mark-article-as-read gnus-killed-mark))
10770               ((> unmark 0)
10771                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10772               (t
10773                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10774         (setq articles (cdr articles))))
10775     ;; Hide killed subtrees.
10776     (and (null unmark)
10777          gnus-thread-hide-killed
10778          (gnus-summary-hide-thread))
10779     ;; If marked as read, go to next unread subject.
10780     (when (null unmark)
10781       ;; Go to next unread subject.
10782       (gnus-summary-next-subject 1 t)))
10783   (gnus-set-mode-line 'summary))
10784
10785 ;; Summary sorting commands
10786
10787 (defun gnus-summary-sort-by-number (&optional reverse)
10788   "Sort the summary buffer by article number.
10789 Argument REVERSE means reverse order."
10790   (interactive "P")
10791   (gnus-summary-sort 'number reverse))
10792
10793 (defun gnus-summary-sort-by-random (&optional reverse)
10794   "Randomize the order in the summary buffer.
10795 Argument REVERSE means to randomize in reverse order."
10796   (interactive "P")
10797   (gnus-summary-sort 'random reverse))
10798
10799 (defun gnus-summary-sort-by-author (&optional reverse)
10800   "Sort the summary buffer by author name alphabetically.
10801 If `case-fold-search' is non-nil, case of letters is ignored.
10802 Argument REVERSE means reverse order."
10803   (interactive "P")
10804   (gnus-summary-sort 'author reverse))
10805
10806 (defun gnus-summary-sort-by-subject (&optional reverse)
10807   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10808 If `case-fold-search' is non-nil, case of letters is ignored.
10809 Argument REVERSE means reverse order."
10810   (interactive "P")
10811   (gnus-summary-sort 'subject reverse))
10812
10813 (defun gnus-summary-sort-by-date (&optional reverse)
10814   "Sort the summary buffer by date.
10815 Argument REVERSE means reverse order."
10816   (interactive "P")
10817   (gnus-summary-sort 'date reverse))
10818
10819 (defun gnus-summary-sort-by-score (&optional reverse)
10820   "Sort the summary buffer by score.
10821 Argument REVERSE means reverse order."
10822   (interactive "P")
10823   (gnus-summary-sort 'score reverse))
10824
10825 (defun gnus-summary-sort-by-lines (&optional reverse)
10826   "Sort the summary buffer by the number of lines.
10827 Argument REVERSE means reverse order."
10828   (interactive "P")
10829   (gnus-summary-sort 'lines reverse))
10830
10831 (defun gnus-summary-sort-by-chars (&optional reverse)
10832   "Sort the summary buffer by article length.
10833 Argument REVERSE means reverse order."
10834   (interactive "P")
10835   (gnus-summary-sort 'chars reverse))
10836
10837 (defun gnus-summary-sort-by-original (&optional reverse)
10838   "Sort the summary buffer using the default sorting method.
10839 Argument REVERSE means reverse order."
10840   (interactive "P")
10841   (let* ((buffer-read-only)
10842          (gnus-summary-prepare-hook nil))
10843     ;; We do the sorting by regenerating the threads.
10844     (gnus-summary-prepare)
10845     ;; Hide subthreads if needed.
10846     (gnus-summary-maybe-hide-threads)))
10847
10848 (defun gnus-summary-sort (predicate reverse)
10849   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10850   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10851          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10852          (gnus-thread-sort-functions
10853           (if (not reverse)
10854               thread
10855             `(lambda (t1 t2)
10856                (,thread t2 t1))))
10857          (gnus-sort-gathered-threads-function
10858           gnus-thread-sort-functions)
10859          (gnus-article-sort-functions
10860           (if (not reverse)
10861               article
10862             `(lambda (t1 t2)
10863                (,article t2 t1))))
10864          (buffer-read-only)
10865          (gnus-summary-prepare-hook nil))
10866     ;; We do the sorting by regenerating the threads.
10867     (gnus-summary-prepare)
10868     ;; Hide subthreads if needed.
10869     (gnus-summary-maybe-hide-threads)))
10870
10871 ;; Summary saving commands.
10872
10873 (defun gnus-summary-save-article (&optional n not-saved)
10874   "Save the current article using the default saver function.
10875 If N is a positive number, save the N next articles.
10876 If N is a negative number, save the N previous articles.
10877 If N is nil and any articles have been marked with the process mark,
10878 save those articles instead.
10879 The variable `gnus-default-article-saver' specifies the saver function."
10880   (interactive "P")
10881   (let* ((articles (gnus-summary-work-articles n))
10882          (save-buffer (save-excursion
10883                         (nnheader-set-temp-buffer " *Gnus Save*")))
10884          (num (length articles))
10885          header file)
10886     (dolist (article articles)
10887       (setq header (gnus-summary-article-header article))
10888       (if (not (vectorp header))
10889           ;; This is a pseudo-article.
10890           (if (assq 'name header)
10891               (gnus-copy-file (cdr (assq 'name header)))
10892             (gnus-message 1 "Article %d is unsaveable" article))
10893         ;; This is a real article.
10894         (save-window-excursion
10895           (let ((gnus-display-mime-function nil)
10896                 (gnus-article-prepare-hook nil))
10897             (gnus-summary-select-article t nil nil article)))
10898         (save-excursion
10899           (set-buffer save-buffer)
10900           (erase-buffer)
10901           (insert-buffer-substring gnus-original-article-buffer))
10902         (setq file (gnus-article-save save-buffer file num))
10903         (gnus-summary-remove-process-mark article)
10904         (unless not-saved
10905           (gnus-summary-set-saved-mark article))))
10906     (gnus-kill-buffer save-buffer)
10907     (gnus-summary-position-point)
10908     (gnus-set-mode-line 'summary)
10909     n))
10910
10911 (defun gnus-summary-pipe-output (&optional arg headers)
10912   "Pipe the current article to a subprocess.
10913 If N is a positive number, pipe the N next articles.
10914 If N is a negative number, pipe the N previous articles.
10915 If N is nil and any articles have been marked with the process mark,
10916 pipe those articles instead.
10917 If HEADERS (the symbolic prefix), include the headers, too."
10918   (interactive (gnus-interactive "P\ny"))
10919   (require 'gnus-art)
10920   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10921         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10922     (gnus-summary-save-article arg t))
10923   (let ((buffer (get-buffer "*Shell Command Output*")))
10924     (when (and buffer
10925                (not (zerop (buffer-size buffer))))
10926       (gnus-configure-windows 'pipe))))
10927
10928 (defun gnus-summary-save-article-mail (&optional arg)
10929   "Append the current article to an mail file.
10930 If N is a positive number, save the N next articles.
10931 If N is a negative number, save the N previous articles.
10932 If N is nil and any articles have been marked with the process mark,
10933 save those articles instead."
10934   (interactive "P")
10935   (require 'gnus-art)
10936   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10937     (gnus-summary-save-article arg)))
10938
10939 (defun gnus-summary-save-article-rmail (&optional arg)
10940   "Append the current article to an rmail file.
10941 If N is a positive number, save the N next articles.
10942 If N is a negative number, save the N previous articles.
10943 If N is nil and any articles have been marked with the process mark,
10944 save those articles instead."
10945   (interactive "P")
10946   (require 'gnus-art)
10947   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10948     (gnus-summary-save-article arg)))
10949
10950 (defun gnus-summary-save-article-file (&optional arg)
10951   "Append the current article to a file.
10952 If N is a positive number, save the N next articles.
10953 If N is a negative number, save the N previous articles.
10954 If N is nil and any articles have been marked with the process mark,
10955 save those articles instead."
10956   (interactive "P")
10957   (require 'gnus-art)
10958   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10959     (gnus-summary-save-article arg)))
10960
10961 (defun gnus-summary-write-article-file (&optional arg)
10962   "Write the current article to a file, deleting the previous file.
10963 If N is a positive number, save the N next articles.
10964 If N is a negative number, save the N previous articles.
10965 If N is nil and any articles have been marked with the process mark,
10966 save those articles instead."
10967   (interactive "P")
10968   (require 'gnus-art)
10969   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10970     (gnus-summary-save-article arg)))
10971
10972 (defun gnus-summary-save-article-body-file (&optional arg)
10973   "Append the current article body to a file.
10974 If N is a positive number, save the N next articles.
10975 If N is a negative number, save the N previous articles.
10976 If N is nil and any articles have been marked with the process mark,
10977 save those articles instead."
10978   (interactive "P")
10979   (require 'gnus-art)
10980   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10981     (gnus-summary-save-article arg)))
10982
10983 (defun gnus-summary-muttprint (&optional arg)
10984   "Print the current article using Muttprint.
10985 If N is a positive number, save the N next articles.
10986 If N is a negative number, save the N previous articles.
10987 If N is nil and any articles have been marked with the process mark,
10988 save those articles instead."
10989   (interactive "P")
10990   (require 'gnus-art)
10991   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10992     (gnus-summary-save-article arg t)))
10993
10994 (defun gnus-summary-pipe-message (program)
10995   "Pipe the current article through PROGRAM."
10996   (interactive "sProgram: ")
10997   (gnus-summary-select-article)
10998   (let ((mail-header-separator ""))
10999     (gnus-eval-in-buffer-window gnus-article-buffer
11000       (save-restriction
11001         (widen)
11002         (let ((start (window-start))
11003               buffer-read-only)
11004           (message-pipe-buffer-body program)
11005           (set-window-start (get-buffer-window (current-buffer)) start))))))
11006
11007 (defun gnus-get-split-value (methods)
11008   "Return a value based on the split METHODS."
11009   (let (split-name method result match)
11010     (when methods
11011       (save-excursion
11012         (set-buffer gnus-original-article-buffer)
11013         (save-restriction
11014           (nnheader-narrow-to-headers)
11015           (while (and methods (not split-name))
11016             (goto-char (point-min))
11017             (setq method (pop methods))
11018             (setq match (car method))
11019             (when (cond
11020                    ((stringp match)
11021                     ;; Regular expression.
11022                     (ignore-errors
11023                       (re-search-forward match nil t)))
11024                    ((functionp match)
11025                     ;; Function.
11026                     (save-restriction
11027                       (widen)
11028                       (setq result (funcall match gnus-newsgroup-name))))
11029                    ((consp match)
11030                     ;; Form.
11031                     (save-restriction
11032                       (widen)
11033                       (setq result (eval match)))))
11034               (setq split-name (cdr method))
11035               (cond ((stringp result)
11036                      (push (expand-file-name
11037                             result gnus-article-save-directory)
11038                            split-name))
11039                     ((consp result)
11040                      (setq split-name (append result split-name)))))))))
11041     (nreverse split-name)))
11042
11043 (defun gnus-valid-move-group-p (group)
11044   (and (boundp group)
11045        (symbol-name group)
11046        (symbol-value group)
11047        (gnus-get-function (gnus-find-method-for-group
11048                            (symbol-name group)) 'request-accept-article t)))
11049
11050 (defun gnus-read-move-group-name (prompt default articles prefix)
11051   "Read a group name."
11052   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11053          (minibuffer-confirm-incomplete nil) ; XEmacs
11054          (prom
11055           (format "%s %s to:"
11056                   prompt
11057                   (if (> (length articles) 1)
11058                       (format "these %d articles" (length articles))
11059                     "this article")))
11060          (to-newsgroup
11061           (cond
11062            ((null split-name)
11063             (gnus-completing-read-with-default
11064              default prom
11065              gnus-active-hashtb
11066              'gnus-valid-move-group-p
11067              nil prefix
11068              'gnus-group-history))
11069            ((= 1 (length split-name))
11070             (gnus-completing-read-with-default
11071              (car split-name) prom
11072              gnus-active-hashtb
11073              'gnus-valid-move-group-p
11074              nil nil
11075              'gnus-group-history))
11076            (t
11077             (gnus-completing-read-with-default
11078              nil prom
11079              (mapcar 'list (nreverse split-name))
11080              nil nil nil
11081              'gnus-group-history))))
11082          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11083     (when to-newsgroup
11084       (if (or (string= to-newsgroup "")
11085               (string= to-newsgroup prefix))
11086           (setq to-newsgroup default))
11087       (unless to-newsgroup
11088         (error "No group name entered"))
11089       (or (gnus-active to-newsgroup)
11090           (gnus-activate-group to-newsgroup nil nil to-method)
11091           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11092                                      to-newsgroup))
11093               (or (and (gnus-request-create-group to-newsgroup to-method)
11094                        (gnus-activate-group
11095                         to-newsgroup nil nil to-method)
11096                        (gnus-subscribe-group to-newsgroup))
11097                   (error "Couldn't create group %s" to-newsgroup)))
11098           (error "No such group: %s" to-newsgroup)))
11099     to-newsgroup))
11100
11101 (defun gnus-summary-save-parts (type dir n &optional reverse)
11102   "Save parts matching TYPE to DIR.
11103 If REVERSE, save parts that do not match TYPE."
11104   (interactive
11105    (list (read-string "Save parts of type: "
11106                       (or (car gnus-summary-save-parts-type-history)
11107                           gnus-summary-save-parts-default-mime)
11108                       'gnus-summary-save-parts-type-history)
11109          (setq gnus-summary-save-parts-last-directory
11110                (read-file-name "Save to directory: "
11111                                gnus-summary-save-parts-last-directory
11112                                nil t))
11113          current-prefix-arg))
11114   (gnus-summary-iterate n
11115     (let ((gnus-display-mime-function nil)
11116           (gnus-inhibit-treatment t))
11117       (gnus-summary-select-article))
11118     (save-excursion
11119       (set-buffer gnus-article-buffer)
11120       (let ((handles (or gnus-article-mime-handles
11121                          (mm-dissect-buffer nil gnus-article-loose-mime)
11122                          (and gnus-article-emulate-mime
11123                               (mm-uu-dissect)))))
11124         (when handles
11125           (gnus-summary-save-parts-1 type dir handles reverse)
11126           (unless gnus-article-mime-handles ;; Don't destroy this case.
11127             (mm-destroy-parts handles)))))))
11128
11129 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11130   (if (stringp (car handle))
11131       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11132               (cdr handle))
11133     (when (if reverse
11134               (not (string-match type (mm-handle-media-type handle)))
11135             (string-match type (mm-handle-media-type handle)))
11136       (let ((file (expand-file-name
11137                    (gnus-map-function
11138                     mm-file-name-rewrite-functions
11139                     (file-name-nondirectory
11140                      (or
11141                       (mail-content-type-get
11142                        (mm-handle-disposition handle) 'filename)
11143                       (mail-content-type-get
11144                        (mm-handle-type handle) 'name)
11145                       (concat gnus-newsgroup-name
11146                               "." (number-to-string
11147                                    (cdr gnus-article-current))))))
11148                    dir)))
11149         (unless (file-exists-p file)
11150           (mm-save-part-to-file handle file))))))
11151
11152 ;; Summary extract commands
11153
11154 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11155   (let ((buffer-read-only nil)
11156         (article (gnus-summary-article-number))
11157         after-article b e)
11158     (unless (gnus-summary-goto-subject article)
11159       (error "No such article: %d" article))
11160     (gnus-summary-position-point)
11161     ;; If all commands are to be bunched up on one line, we collect
11162     ;; them here.
11163     (unless gnus-view-pseudos-separately
11164       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11165             files action)
11166         (while ps
11167           (setq action (cdr (assq 'action (car ps))))
11168           (setq files (list (cdr (assq 'name (car ps)))))
11169           (while (and ps (cdr ps)
11170                       (string= (or action "1")
11171                                (or (cdr (assq 'action (cadr ps))) "2")))
11172             (push (cdr (assq 'name (cadr ps))) files)
11173             (setcdr ps (cddr ps)))
11174           (when files
11175             (when (not (string-match "%s" action))
11176               (push " " files))
11177             (push " " files)
11178             (when (assq 'execute (car ps))
11179               (setcdr (assq 'execute (car ps))
11180                       (funcall (if (string-match "%s" action)
11181                                    'format 'concat)
11182                                action
11183                                (mapconcat
11184                                 (lambda (f)
11185                                   (if (equal f " ")
11186                                       f
11187                                     (shell-quote-argument f)))
11188                                 files " ")))))
11189           (setq ps (cdr ps)))))
11190     (if (and gnus-view-pseudos (not not-view))
11191         (while pslist
11192           (when (assq 'execute (car pslist))
11193             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11194                                   (eq gnus-view-pseudos 'not-confirm)))
11195           (setq pslist (cdr pslist)))
11196       (save-excursion
11197         (while pslist
11198           (setq after-article (or (cdr (assq 'article (car pslist)))
11199                                   (gnus-summary-article-number)))
11200           (gnus-summary-goto-subject after-article)
11201           (forward-line 1)
11202           (setq b (point))
11203           (insert "    " (file-name-nondirectory
11204                           (cdr (assq 'name (car pslist))))
11205                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11206           (setq e (point))
11207           (forward-line -1)             ; back to `b'
11208           (gnus-add-text-properties
11209            b (1- e) (list 'gnus-number gnus-reffed-article-number
11210                           gnus-mouse-face-prop gnus-mouse-face))
11211           (gnus-data-enter
11212            after-article gnus-reffed-article-number
11213            gnus-unread-mark b (car pslist) 0 (- e b))
11214           (setq gnus-newsgroup-unreads
11215                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11216                                          gnus-reffed-article-number))
11217           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11218           (setq pslist (cdr pslist)))))))
11219
11220 (defun gnus-pseudos< (p1 p2)
11221   (let ((c1 (cdr (assq 'action p1)))
11222         (c2 (cdr (assq 'action p2))))
11223     (and c1 c2 (string< c1 c2))))
11224
11225 (defun gnus-request-pseudo-article (props)
11226   (cond ((assq 'execute props)
11227          (gnus-execute-command (cdr (assq 'execute props)))))
11228   (let ((gnus-current-article (gnus-summary-article-number)))
11229     (gnus-run-hooks 'gnus-mark-article-hook)))
11230
11231 (defun gnus-execute-command (command &optional automatic)
11232   (save-excursion
11233     (gnus-article-setup-buffer)
11234     (set-buffer gnus-article-buffer)
11235     (setq buffer-read-only nil)
11236     (let ((command (if automatic command
11237                      (read-string "Command: " (cons command 0)))))
11238       (erase-buffer)
11239       (insert "$ " command "\n\n")
11240       (if gnus-view-pseudo-asynchronously
11241           (start-process "gnus-execute" (current-buffer) shell-file-name
11242                          shell-command-switch command)
11243         (call-process shell-file-name nil t nil
11244                       shell-command-switch command)))))
11245
11246 ;; Summary kill commands.
11247
11248 (defun gnus-summary-edit-global-kill (article)
11249   "Edit the \"global\" kill file."
11250   (interactive (list (gnus-summary-article-number)))
11251   (gnus-group-edit-global-kill article))
11252
11253 (defun gnus-summary-edit-local-kill ()
11254   "Edit a local kill file applied to the current newsgroup."
11255   (interactive)
11256   (setq gnus-current-headers (gnus-summary-article-header))
11257   (gnus-group-edit-local-kill
11258    (gnus-summary-article-number) gnus-newsgroup-name))
11259
11260 ;;; Header reading.
11261
11262 (defun gnus-read-header (id &optional header)
11263   "Read the headers of article ID and enter them into the Gnus system."
11264   (let ((group gnus-newsgroup-name)
11265         (gnus-override-method
11266          (or
11267           gnus-override-method
11268           (and (gnus-news-group-p gnus-newsgroup-name)
11269                (car (gnus-refer-article-methods)))))
11270         where)
11271     ;; First we check to see whether the header in question is already
11272     ;; fetched.
11273     (if (stringp id)
11274         ;; This is a Message-ID.
11275         (setq header (or header (gnus-id-to-header id)))
11276       ;; This is an article number.
11277       (setq header (or header (gnus-summary-article-header id))))
11278     (if (and header
11279              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11280         ;; We have found the header.
11281         header
11282       ;; We have to really fetch the header to this article.
11283       (save-excursion
11284         (set-buffer nntp-server-buffer)
11285         (when (setq where (gnus-request-head id group))
11286           (nnheader-fold-continuation-lines)
11287           (goto-char (point-max))
11288           (insert ".\n")
11289           (goto-char (point-min))
11290           (insert "211 ")
11291           (princ (cond
11292                   ((numberp id) id)
11293                   ((cdr where) (cdr where))
11294                   (header (mail-header-number header))
11295                   (t gnus-reffed-article-number))
11296                  (current-buffer))
11297           (insert " Article retrieved.\n"))
11298         (if (or (not where)
11299                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11300             ()                          ; Malformed head.
11301           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11302             (when (and (stringp id)
11303                        (not (string= (gnus-group-real-name group)
11304                                      (car where))))
11305               ;; If we fetched by Message-ID and the article came
11306               ;; from a different group, we fudge some bogus article
11307               ;; numbers for this article.
11308               (mail-header-set-number header gnus-reffed-article-number))
11309             (save-excursion
11310               (set-buffer gnus-summary-buffer)
11311               (decf gnus-reffed-article-number)
11312               (gnus-remove-header (mail-header-number header))
11313               (push header gnus-newsgroup-headers)
11314               (setq gnus-current-headers header)
11315               (push (mail-header-number header) gnus-newsgroup-limit)))
11316           header)))))
11317
11318 (defun gnus-remove-header (number)
11319   "Remove header NUMBER from `gnus-newsgroup-headers'."
11320   (if (and gnus-newsgroup-headers
11321            (= number (mail-header-number (car gnus-newsgroup-headers))))
11322       (pop gnus-newsgroup-headers)
11323     (let ((headers gnus-newsgroup-headers))
11324       (while (and (cdr headers)
11325                   (not (= number (mail-header-number (cadr headers)))))
11326         (pop headers))
11327       (when (cdr headers)
11328         (setcdr headers (cddr headers))))))
11329
11330 ;;;
11331 ;;; summary highlights
11332 ;;;
11333
11334 (defun gnus-highlight-selected-summary ()
11335   "Highlight selected article in summary buffer."
11336   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11337   (when gnus-summary-selected-face
11338     (save-excursion
11339       (let* ((beg (point-at-bol))
11340              (end (point-at-eol))
11341              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11342              (from (if (get-text-property beg gnus-mouse-face-prop)
11343                        beg
11344                      (or (next-single-property-change
11345                           beg gnus-mouse-face-prop nil end)
11346                          beg)))
11347              (to
11348               (if (= from end)
11349                   (- from 2)
11350                 (or (next-single-property-change
11351                      from gnus-mouse-face-prop nil end)
11352                     end))))
11353         ;; If no mouse-face prop on line we will have to = from = end,
11354         ;; so we highlight the entire line instead.
11355         (when (= (+ to 2) from)
11356           (setq from beg)
11357           (setq to end))
11358         (if gnus-newsgroup-selected-overlay
11359             ;; Move old overlay.
11360             (gnus-move-overlay
11361              gnus-newsgroup-selected-overlay from to (current-buffer))
11362           ;; Create new overlay.
11363           (gnus-overlay-put
11364            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11365            'face gnus-summary-selected-face))))))
11366
11367 (defvar gnus-summary-highlight-line-cached nil)
11368 (defvar gnus-summary-highlight-line-trigger nil)
11369
11370 (defun gnus-summary-highlight-line-0 ()
11371   (if (and (eq gnus-summary-highlight-line-trigger
11372                gnus-summary-highlight)
11373            gnus-summary-highlight-line-cached)
11374       gnus-summary-highlight-line-cached
11375     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11376           gnus-summary-highlight-line-cached
11377           (let* ((cond (list 'cond))
11378                  (c cond)
11379                  (list gnus-summary-highlight))
11380             (while list
11381               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11382                               nil))
11383               (setq c (cdr c)
11384                     list (cdr list)))
11385             (gnus-byte-compile (list 'lambda nil cond))))))
11386
11387 (defun gnus-summary-highlight-line ()
11388   "Highlight current line according to `gnus-summary-highlight'."
11389   (let* ((beg (point-at-bol))
11390          (article (or (gnus-summary-article-number) gnus-current-article))
11391          (score (or (cdr (assq article
11392                                gnus-newsgroup-scored))
11393                     gnus-summary-default-score 0))
11394          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11395          (inhibit-read-only t)
11396          (default gnus-summary-default-score)
11397          (default-high gnus-summary-default-high-score)
11398          (default-low gnus-summary-default-low-score)
11399          (uncached (and gnus-summary-use-undownloaded-faces
11400                         (memq article gnus-newsgroup-undownloaded)
11401                         (not (memq article gnus-newsgroup-cached)))))
11402     (let ((face (funcall (gnus-summary-highlight-line-0))))
11403       (unless (eq face (get-text-property beg 'face))
11404         (gnus-put-text-property-excluding-characters-with-faces
11405          beg (point-at-eol) 'face
11406          (setq face (if (boundp face) (symbol-value face) face)))
11407         (when gnus-summary-highlight-line-function
11408           (funcall gnus-summary-highlight-line-function article face))))))
11409
11410 (defun gnus-update-read-articles (group unread &optional compute)
11411   "Update the list of read articles in GROUP.
11412 UNREAD is a sorted list."
11413   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11414         (info (gnus-get-info group))
11415         (prev 1)
11416         read)
11417     (if (or (not info) (not active))
11418         ;; There is no info on this group if it was, in fact,
11419         ;; killed.  Gnus stores no information on killed groups, so
11420         ;; there's nothing to be done.
11421         ;; One could store the information somewhere temporarily,
11422         ;; perhaps...  Hmmm...
11423         ()
11424       ;; Remove any negative articles numbers.
11425       (while (and unread (< (car unread) 0))
11426         (setq unread (cdr unread)))
11427       ;; Remove any expired article numbers
11428       (while (and unread (< (car unread) (car active)))
11429         (setq unread (cdr unread)))
11430       ;; Compute the ranges of read articles by looking at the list of
11431       ;; unread articles.
11432       (while unread
11433         (when (/= (car unread) prev)
11434           (push (if (= prev (1- (car unread))) prev
11435                   (cons prev (1- (car unread))))
11436                 read))
11437         (setq prev (1+ (car unread)))
11438         (setq unread (cdr unread)))
11439       (when (<= prev (cdr active))
11440         (push (cons prev (cdr active)) read))
11441       (setq read (if (> (length read) 1) (nreverse read) read))
11442       (if compute
11443           read
11444         (save-excursion
11445           (let (setmarkundo)
11446             ;; Propagate the read marks to the backend.
11447             (when (gnus-check-backend-function 'request-set-mark group)
11448               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11449                     (add (gnus-remove-from-range read (gnus-info-read info))))
11450                 (when (or add del)
11451                   (unless (gnus-check-group group)
11452                     (error "Can't open server for %s" group))
11453                   (gnus-request-set-mark
11454                    group (delq nil (list (if add (list add 'add '(read)))
11455                                          (if del (list del 'del '(read))))))
11456                   (setq setmarkundo
11457                         `(gnus-request-set-mark
11458                           ,group
11459                           ',(delq nil (list
11460                                        (if del (list del 'add '(read)))
11461                                        (if add (list add 'del '(read))))))))))
11462             (set-buffer gnus-group-buffer)
11463             (gnus-undo-register
11464               `(progn
11465                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11466                  (gnus-info-set-read ',info ',(gnus-info-read info))
11467                  (gnus-get-unread-articles-in-group ',info
11468                                                     (gnus-active ,group))
11469                  (gnus-group-update-group ,group t)
11470                  ,setmarkundo))))
11471         ;; Enter this list into the group info.
11472         (gnus-info-set-read info read)
11473         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11474         (gnus-get-unread-articles-in-group info (gnus-active group))
11475         t))))
11476
11477 (defun gnus-offer-save-summaries ()
11478   "Offer to save all active summary buffers."
11479   (let (buffers)
11480     ;; Go through all buffers and find all summaries.
11481     (dolist (buffer (buffer-list))
11482       (when (and (setq buffer (buffer-name buffer))
11483                  (string-match "Summary" buffer)
11484                  (with-current-buffer buffer
11485                    ;; We check that this is, indeed, a summary buffer.
11486                    (and (eq major-mode 'gnus-summary-mode)
11487                         ;; Also make sure this isn't bogus.
11488                         gnus-newsgroup-prepared
11489                         ;; Also make sure that this isn't a
11490                         ;; dead summary buffer.
11491                         (not gnus-dead-summary-mode))))
11492         (push buffer buffers)))
11493     ;; Go through all these summary buffers and offer to save them.
11494     (when buffers
11495       (save-excursion
11496         (map-y-or-n-p
11497          "Update summary buffer %s? "
11498          (lambda (buf)
11499            (switch-to-buffer buf)
11500            (gnus-summary-exit))
11501          buffers)))))
11502
11503 (defun gnus-summary-setup-default-charset ()
11504   "Setup newsgroup default charset."
11505   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11506       (setq gnus-newsgroup-charset nil)
11507     (let* ((ignored-charsets
11508             (or gnus-newsgroup-ephemeral-ignored-charsets
11509                 (append
11510                  (and gnus-newsgroup-name
11511                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11512                  gnus-newsgroup-ignored-charsets))))
11513       (setq gnus-newsgroup-charset
11514             (or gnus-newsgroup-ephemeral-charset
11515                 (and gnus-newsgroup-name
11516                      (gnus-parameter-charset gnus-newsgroup-name))
11517                 gnus-default-charset))
11518       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11519            ignored-charsets))))
11520
11521 ;;;
11522 ;;; Mime Commands
11523 ;;;
11524
11525 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11526   "Display the current article buffer fully MIME-buttonized.
11527 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11528 treated as multipart/mixed."
11529   (interactive "P")
11530   (require 'gnus-art)
11531   (let ((gnus-unbuttonized-mime-types nil)
11532         (gnus-mime-display-multipart-as-mixed show-all-parts))
11533     (gnus-summary-show-article)))
11534
11535 (defun gnus-summary-repair-multipart (article)
11536   "Add a Content-Type header to a multipart article without one."
11537   (interactive (list (gnus-summary-article-number)))
11538   (gnus-with-article article
11539     (message-narrow-to-head)
11540     (message-remove-header "Mime-Version")
11541     (goto-char (point-max))
11542     (insert "Mime-Version: 1.0\n")
11543     (widen)
11544     (when (search-forward "\n--" nil t)
11545       (let ((separator (buffer-substring (point) (point-at-eol))))
11546         (message-narrow-to-head)
11547         (message-remove-header "Content-Type")
11548         (goto-char (point-max))
11549         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11550                         separator))
11551         (widen))))
11552   (let (gnus-mark-article-hook)
11553     (gnus-summary-select-article t t nil article)))
11554
11555 (defun gnus-summary-toggle-display-buttonized ()
11556   "Toggle the buttonizing of the article buffer."
11557   (interactive)
11558   (require 'gnus-art)
11559   (if (setq gnus-inhibit-mime-unbuttonizing
11560             (not gnus-inhibit-mime-unbuttonizing))
11561       (let ((gnus-unbuttonized-mime-types nil))
11562         (gnus-summary-show-article))
11563     (gnus-summary-show-article)))
11564
11565 ;;;
11566 ;;; Generic summary marking commands
11567 ;;;
11568
11569 (defvar gnus-summary-marking-alist
11570   '((read gnus-del-mark "d")
11571     (unread gnus-unread-mark "u")
11572     (ticked gnus-ticked-mark "!")
11573     (dormant gnus-dormant-mark "?")
11574     (expirable gnus-expirable-mark "e"))
11575   "An alist of names/marks/keystrokes.")
11576
11577 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11578 (defvar gnus-summary-mark-map)
11579
11580 (defun gnus-summary-make-all-marking-commands ()
11581   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11582   (dolist (elem gnus-summary-marking-alist)
11583     (apply 'gnus-summary-make-marking-command elem)))
11584
11585 (defun gnus-summary-make-marking-command (name mark keystroke)
11586   (let ((map (make-sparse-keymap)))
11587     (define-key gnus-summary-generic-mark-map keystroke map)
11588     (dolist (lway `((next "next" next nil "n")
11589                     (next-unread "next unread" next t "N")
11590                     (prev "previous" prev nil "p")
11591                     (prev-unread "previous unread" prev t "P")
11592                     (nomove "" nil nil ,keystroke)))
11593       (let ((func (gnus-summary-make-marking-command-1
11594                    mark (car lway) lway name)))
11595         (setq func (eval func))
11596         (define-key map (nth 4 lway) func)))))
11597
11598 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11599   `(defun ,(intern
11600             (format "gnus-summary-put-mark-as-%s%s"
11601                     name (if (eq way 'nomove)
11602                              ""
11603                            (concat "-" (symbol-name way)))))
11604      (n)
11605      ,(format
11606        "Mark the current article as %s%s.
11607 If N, the prefix, then repeat N times.
11608 If N is negative, move in reverse order.
11609 The difference between N and the actual number of articles marked is
11610 returned."
11611        name (cadr lway))
11612      (interactive "p")
11613      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11614
11615 (defun gnus-summary-generic-mark (n mark move unread)
11616   "Mark N articles with MARK."
11617   (unless (eq major-mode 'gnus-summary-mode)
11618     (error "This command can only be used in the summary buffer"))
11619   (gnus-summary-show-thread)
11620   (let ((nummove
11621          (cond
11622           ((eq move 'next) 1)
11623           ((eq move 'prev) -1)
11624           (t 0))))
11625     (if (zerop nummove)
11626         (setq n 1)
11627       (when (< n 0)
11628         (setq n (abs n)
11629               nummove (* -1 nummove))))
11630     (while (and (> n 0)
11631                 (gnus-summary-mark-article nil mark)
11632                 (zerop (gnus-summary-next-subject nummove unread t)))
11633       (setq n (1- n)))
11634     (when (/= 0 n)
11635       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11636     (gnus-summary-recenter)
11637     (gnus-summary-position-point)
11638     (gnus-set-mode-line 'summary)
11639     n))
11640
11641 (defun gnus-summary-insert-articles (articles)
11642   (when (setq articles
11643               (gnus-sorted-difference articles
11644                                       (mapcar (lambda (h)
11645                                                 (mail-header-number h))
11646                                               gnus-newsgroup-headers)))
11647     (setq gnus-newsgroup-headers
11648           (gnus-merge 'list
11649                       gnus-newsgroup-headers
11650                       (gnus-fetch-headers articles)
11651                       'gnus-article-sort-by-number))
11652     ;; Suppress duplicates?
11653     (when gnus-suppress-duplicates
11654       (gnus-dup-suppress-articles))
11655
11656     ;; We might want to build some more threads first.
11657     (when (and gnus-fetch-old-headers
11658                (eq gnus-headers-retrieved-by 'nov))
11659       (if (eq gnus-fetch-old-headers 'invisible)
11660           (gnus-build-all-threads)
11661         (gnus-build-old-threads)))
11662     ;; Let the Gnus agent mark articles as read.
11663     (when gnus-agent
11664       (gnus-agent-get-undownloaded-list))
11665     ;; Remove list identifiers from subject
11666     (when gnus-list-identifiers
11667       (gnus-summary-remove-list-identifiers))
11668     ;; First and last article in this newsgroup.
11669     (when gnus-newsgroup-headers
11670       (setq gnus-newsgroup-begin
11671             (mail-header-number (car gnus-newsgroup-headers))
11672             gnus-newsgroup-end
11673             (mail-header-number
11674              (gnus-last-element gnus-newsgroup-headers))))
11675     (when gnus-use-scoring
11676       (gnus-possibly-score-headers))))
11677
11678 (defun gnus-summary-insert-old-articles (&optional all)
11679   "Insert all old articles in this group.
11680 If ALL is non-nil, already read articles become readable.
11681 If ALL is a number, fetch this number of articles."
11682   (interactive "P")
11683   (prog1
11684       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11685             older len)
11686         (setq older
11687               ;; Some nntp servers lie about their active range.  When
11688               ;; this happens, the active range can be in the millions.
11689               ;; Use a compressed range to avoid creating a huge list.
11690               (gnus-range-difference (list gnus-newsgroup-active) old))
11691         (setq len (gnus-range-length older))
11692         (cond
11693          ((null older) nil)
11694          ((numberp all)
11695           (if (< all len)
11696               (let ((older-range (nreverse older)))
11697                 (setq older nil)
11698
11699                 (while (> all 0)
11700                   (let* ((r (pop older-range))
11701                          (min (if (numberp r) r (car r)))
11702                          (max (if (numberp r) r (cdr r))))
11703                     (while (and (<= min max)
11704                                 (> all 0))
11705                       (push max older)
11706                       (setq all (1- all)
11707                             max (1- max))))))
11708             (setq older (gnus-uncompress-range older))))
11709          (all
11710           (setq older (gnus-uncompress-range older)))
11711          (t
11712           (when (and (numberp gnus-large-newsgroup)
11713                    (> len gnus-large-newsgroup))
11714               (let* ((cursor-in-echo-area nil)
11715                      (initial (gnus-parameter-large-newsgroup-initial
11716                                gnus-newsgroup-name))
11717                      (input
11718                       (read-string
11719                        (format
11720                         "How many articles from %s (%s %d): "
11721                         (gnus-limit-string
11722                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11723                         (if initial "max" "default")
11724                         len)
11725                        (if initial
11726                            (cons (number-to-string initial)
11727                                  0)))))
11728                 (unless (string-match "^[ \t]*$" input)
11729                   (setq all (string-to-number input))
11730                   (if (< all len)
11731                       (let ((older-range (nreverse older)))
11732                         (setq older nil)
11733
11734                         (while (> all 0)
11735                           (let* ((r (pop older-range))
11736                                  (min (if (numberp r) r (car r)))
11737                                  (max (if (numberp r) r (cdr r))))
11738                             (while (and (<= min max)
11739                                         (> all 0))
11740                               (push max older)
11741                               (setq all (1- all)
11742                                     max (1- max))))))))))
11743           (setq older (gnus-uncompress-range older))))
11744         (if (not older)
11745             (message "No old news.")
11746           (gnus-summary-insert-articles older)
11747           (gnus-summary-limit (gnus-sorted-nunion old older))))
11748     (gnus-summary-position-point)))
11749
11750 (defun gnus-summary-insert-new-articles ()
11751   "Insert all new articles in this group."
11752   (interactive)
11753   (prog1
11754       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11755             (old-active gnus-newsgroup-active)
11756             (nnmail-fetched-sources (list t))
11757             i new)
11758         (setq gnus-newsgroup-active
11759               (gnus-activate-group gnus-newsgroup-name 'scan))
11760         (setq i (cdr gnus-newsgroup-active))
11761         (while (> i (cdr old-active))
11762           (push i new)
11763           (decf i))
11764         (if (not new)
11765             (message "No gnus is bad news")
11766           (gnus-summary-insert-articles new)
11767           (setq gnus-newsgroup-unreads
11768                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11769           (gnus-summary-limit (gnus-sorted-nunion old new))))
11770     (gnus-summary-position-point)))
11771
11772 (gnus-summary-make-all-marking-commands)
11773
11774 (gnus-ems-redefine)
11775
11776 (provide 'gnus-sum)
11777
11778 (run-hooks 'gnus-sum-load-hook)
11779
11780 ;; Local Variables:
11781 ;; coding: iso-8859-1
11782 ;; End:
11783
11784 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11785 ;;; gnus-sum.el ends here