0ad7eb1c4893f215992a0a1a2d1e61ac243d335e
[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   :group 'gnus-summary
1141   :type '(choice boolean regexp))
1142
1143 (defcustom gnus-summary-muttprint-program "muttprint"
1144   "Command (and optional arguments) used to run Muttprint."
1145   :version "21.4"
1146   :group 'gnus-summary
1147   :type 'string)
1148
1149 (defcustom gnus-article-loose-mime t
1150   "If non-nil, don't require MIME-Version header.
1151 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1152 supply the MIME-Version header or deliberately strip it from the mail.
1153 If non-nil (the default), Gnus will treat some articles as MIME
1154 even if the MIME-Version header is missing."
1155   :version "21.4"
1156   :type 'boolean
1157   :group 'gnus-article-mime)
1158
1159 (defcustom gnus-article-emulate-mime t
1160   "If non-nil, use MIME emulation for uuencode and the like.
1161 This means that Gnus will search message bodies for text that look
1162 like uuencoded bits, yEncoded bits, and so on, and present that using
1163 the normal Gnus MIME machinery."
1164   :version "21.4"
1165   :type 'boolean
1166   :group 'gnus-article-mime)
1167
1168 ;;; Internal variables
1169
1170 (defvar gnus-summary-display-cache nil)
1171 (defvar gnus-article-mime-handles nil)
1172 (defvar gnus-article-decoded-p nil)
1173 (defvar gnus-article-charset nil)
1174 (defvar gnus-article-ignored-charsets nil)
1175 (defvar gnus-scores-exclude-files nil)
1176 (defvar gnus-page-broken nil)
1177
1178 (defvar gnus-original-article nil)
1179 (defvar gnus-article-internal-prepare-hook nil)
1180 (defvar gnus-newsgroup-process-stack nil)
1181
1182 (defvar gnus-thread-indent-array nil)
1183 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1184 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1185   "Function called to sort the articles within a thread after it has been gathered together.")
1186
1187 (defvar gnus-summary-save-parts-type-history nil)
1188 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1189
1190 ;; Avoid highlighting in kill files.
1191 (defvar gnus-summary-inhibit-highlight nil)
1192 (defvar gnus-newsgroup-selected-overlay nil)
1193 (defvar gnus-inhibit-limiting nil)
1194 (defvar gnus-newsgroup-adaptive-score-file nil)
1195 (defvar gnus-current-score-file nil)
1196 (defvar gnus-current-move-group nil)
1197 (defvar gnus-current-copy-group nil)
1198 (defvar gnus-current-crosspost-group nil)
1199 (defvar gnus-newsgroup-display nil)
1200
1201 (defvar gnus-newsgroup-dependencies nil)
1202 (defvar gnus-newsgroup-adaptive nil)
1203 (defvar gnus-summary-display-article-function nil)
1204 (defvar gnus-summary-highlight-line-function nil
1205   "Function called after highlighting a summary line.")
1206
1207 (defvar gnus-summary-line-format-alist
1208   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1209     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1210     (?s gnus-tmp-subject-or-nil ?s)
1211     (?n gnus-tmp-name ?s)
1212     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1213         ?s)
1214     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1215             gnus-tmp-from) ?s)
1216     (?F gnus-tmp-from ?s)
1217     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1218     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1219     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1220     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1221     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1222     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1223     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1224     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1225     (?L gnus-tmp-lines ?s)
1226     (?O gnus-tmp-downloaded ?c)
1227     (?I gnus-tmp-indentation ?s)
1228     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1229     (?R gnus-tmp-replied ?c)
1230     (?\[ gnus-tmp-opening-bracket ?c)
1231     (?\] gnus-tmp-closing-bracket ?c)
1232     (?\> (make-string gnus-tmp-level ? ) ?s)
1233     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1234     (?i gnus-tmp-score ?d)
1235     (?z gnus-tmp-score-char ?c)
1236     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1237     (?U gnus-tmp-unread ?c)
1238     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1239         ?s)
1240     (?t (gnus-summary-number-of-articles-in-thread
1241          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1242         ?d)
1243     (?e (gnus-summary-number-of-articles-in-thread
1244          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1245         ?c)
1246     (?u gnus-tmp-user-defined ?s)
1247     (?P (gnus-pick-line-number) ?d)
1248     (?B gnus-tmp-thread-tree-header-string ?s)
1249     (user-date (gnus-user-date
1250                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1251   "An alist of format specifications that can appear in summary lines.
1252 These are paired with what variables they correspond with, along with
1253 the type of the variable (string, integer, character, etc).")
1254
1255 (defvar gnus-summary-dummy-line-format-alist
1256   `((?S gnus-tmp-subject ?s)
1257     (?N gnus-tmp-number ?d)
1258     (?u gnus-tmp-user-defined ?s)))
1259
1260 (defvar gnus-summary-mode-line-format-alist
1261   `((?G gnus-tmp-group-name ?s)
1262     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1263     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1264     (?A gnus-tmp-article-number ?d)
1265     (?Z gnus-tmp-unread-and-unselected ?s)
1266     (?V gnus-version ?s)
1267     (?U gnus-tmp-unread-and-unticked ?d)
1268     (?S gnus-tmp-subject ?s)
1269     (?e gnus-tmp-unselected ?d)
1270     (?u gnus-tmp-user-defined ?s)
1271     (?d (length gnus-newsgroup-dormant) ?d)
1272     (?t (length gnus-newsgroup-marked) ?d)
1273     (?h (length gnus-newsgroup-spam-marked) ?d)
1274     (?r (length gnus-newsgroup-reads) ?d)
1275     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1276     (?E gnus-newsgroup-expunged-tally ?d)
1277     (?s (gnus-current-score-file-nondirectory) ?s)))
1278
1279 (defvar gnus-last-search-regexp nil
1280   "Default regexp for article search command.")
1281
1282 (defvar gnus-last-shell-command nil
1283   "Default shell command on article.")
1284
1285 (defvar gnus-newsgroup-agentized nil
1286   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1287 (defvar gnus-newsgroup-begin nil)
1288 (defvar gnus-newsgroup-end nil)
1289 (defvar gnus-newsgroup-last-rmail nil)
1290 (defvar gnus-newsgroup-last-mail nil)
1291 (defvar gnus-newsgroup-last-folder nil)
1292 (defvar gnus-newsgroup-last-file nil)
1293 (defvar gnus-newsgroup-auto-expire nil)
1294 (defvar gnus-newsgroup-active nil)
1295
1296 (defvar gnus-newsgroup-data nil)
1297 (defvar gnus-newsgroup-data-reverse nil)
1298 (defvar gnus-newsgroup-limit nil)
1299 (defvar gnus-newsgroup-limits nil)
1300 (defvar gnus-summary-use-undownloaded-faces nil)
1301
1302 (defvar gnus-newsgroup-unreads nil
1303   "Sorted list of unread articles in the current newsgroup.")
1304
1305 (defvar gnus-newsgroup-unselected nil
1306   "Sorted list of unselected unread articles in the current newsgroup.")
1307
1308 (defvar gnus-newsgroup-reads nil
1309   "Alist of read articles and article marks in the current newsgroup.")
1310
1311 (defvar gnus-newsgroup-expunged-tally nil)
1312
1313 (defvar gnus-newsgroup-marked nil
1314   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1315
1316 (defvar gnus-newsgroup-spam-marked nil
1317   "List of ranges of articles that have been marked as spam.")
1318
1319 (defvar gnus-newsgroup-killed nil
1320   "List of ranges of articles that have been through the scoring process.")
1321
1322 (defvar gnus-newsgroup-cached nil
1323   "Sorted list of articles that come from the article cache.")
1324
1325 (defvar gnus-newsgroup-saved nil
1326   "List of articles that have been saved.")
1327
1328 (defvar gnus-newsgroup-kill-headers nil)
1329
1330 (defvar gnus-newsgroup-replied nil
1331   "List of articles that have been replied to in the current newsgroup.")
1332
1333 (defvar gnus-newsgroup-forwarded nil
1334   "List of articles that have been forwarded in the current newsgroup.")
1335
1336 (defvar gnus-newsgroup-recent nil
1337   "List of articles that have are recent in the current newsgroup.")
1338
1339 (defvar gnus-newsgroup-expirable nil
1340   "Sorted list of articles in the current newsgroup that can be expired.")
1341
1342 (defvar gnus-newsgroup-processable nil
1343   "List of articles in the current newsgroup that can be processed.")
1344
1345 (defvar gnus-newsgroup-downloadable nil
1346   "Sorted list of articles in the current newsgroup that can be processed.")
1347
1348 (defvar gnus-newsgroup-unfetched nil
1349   "Sorted list of articles in the current newsgroup whose headers have
1350 not been fetched into the agent.
1351
1352 This list will always be a subset of gnus-newsgroup-undownloaded.")
1353
1354 (defvar gnus-newsgroup-undownloaded nil
1355   "List of articles in the current newsgroup that haven't been downloaded.")
1356
1357 (defvar gnus-newsgroup-unsendable nil
1358   "List of articles in the current newsgroup that won't be sent.")
1359
1360 (defvar gnus-newsgroup-bookmarks nil
1361   "List of articles in the current newsgroup that have bookmarks.")
1362
1363 (defvar gnus-newsgroup-dormant nil
1364   "Sorted list of dormant articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-unseen nil
1367   "List of unseen articles in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-seen nil
1370   "Range of seen articles in the current newsgroup.")
1371
1372 (defvar gnus-newsgroup-articles nil
1373   "List of articles in the current newsgroup.")
1374
1375 (defvar gnus-newsgroup-scored nil
1376   "List of scored articles in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-headers nil
1379   "List of article headers in the current newsgroup.")
1380
1381 (defvar gnus-newsgroup-threads nil)
1382
1383 (defvar gnus-newsgroup-prepared nil
1384   "Whether the current group has been prepared properly.")
1385
1386 (defvar gnus-newsgroup-ancient nil
1387   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1388
1389 (defvar gnus-newsgroup-sparse nil)
1390
1391 (defvar gnus-current-article nil)
1392 (defvar gnus-article-current nil)
1393 (defvar gnus-current-headers nil)
1394 (defvar gnus-have-all-headers nil)
1395 (defvar gnus-last-article nil)
1396 (defvar gnus-newsgroup-history nil)
1397 (defvar gnus-newsgroup-charset nil)
1398 (defvar gnus-newsgroup-ephemeral-charset nil)
1399 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1400
1401 (defvar gnus-article-before-search nil)
1402
1403 (defvar gnus-summary-local-variables
1404   '(gnus-newsgroup-name
1405     gnus-newsgroup-begin gnus-newsgroup-end
1406     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1407     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1408     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1409     gnus-newsgroup-unselected gnus-newsgroup-marked
1410     gnus-newsgroup-spam-marked
1411     gnus-newsgroup-reads gnus-newsgroup-saved
1412     gnus-newsgroup-replied gnus-newsgroup-forwarded
1413     gnus-newsgroup-recent
1414     gnus-newsgroup-expirable
1415     gnus-newsgroup-processable gnus-newsgroup-killed
1416     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1417     gnus-newsgroup-unfetched
1418     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1419     gnus-newsgroup-seen gnus-newsgroup-articles
1420     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1421     gnus-newsgroup-headers gnus-newsgroup-threads
1422     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1423     gnus-current-article gnus-current-headers gnus-have-all-headers
1424     gnus-last-article gnus-article-internal-prepare-hook
1425     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1426     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1427     gnus-thread-expunge-below
1428     gnus-score-alist gnus-current-score-file
1429     (gnus-summary-expunge-below . global)
1430     (gnus-summary-mark-below . global)
1431     (gnus-orphan-score . global)
1432     gnus-newsgroup-active gnus-scores-exclude-files
1433     gnus-newsgroup-history gnus-newsgroup-ancient
1434     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1435     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1436     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1437     (gnus-newsgroup-expunged-tally . 0)
1438     gnus-cache-removable-articles gnus-newsgroup-cached
1439     gnus-newsgroup-data gnus-newsgroup-data-reverse
1440     gnus-newsgroup-limit gnus-newsgroup-limits
1441     gnus-newsgroup-charset gnus-newsgroup-display
1442     gnus-summary-use-undownloaded-faces)
1443   "Variables that are buffer-local to the summary buffers.")
1444
1445 (defvar gnus-newsgroup-variables nil
1446   "A list of variables that have separate values in different newsgroups.
1447 A list of newsgroup (summary buffer) local variables, or cons of
1448 variables and their default expressions to be evalled (when the default
1449 values are not nil), that should be made global while the summary buffer
1450 is active.
1451
1452 Note: The default expressions will be evaluated (using function `eval')
1453 before assignment to the local variable rather than just assigned to it.
1454 If the default expression is the symbol `global', that symbol will not
1455 be evaluated but the global value of the local variable will be used
1456 instead.
1457
1458 These variables can be used to set variables in the group parameters
1459 while still allowing them to affect operations done in other buffers.
1460 For example:
1461
1462 \(setq gnus-newsgroup-variables
1463      '(message-use-followup-to
1464        (gnus-visible-headers .
1465          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1466 ")
1467
1468 ;; Byte-compiler warning.
1469 (eval-when-compile
1470   ;; Bind features so that require will believe that gnus-sum has
1471   ;; already been loaded (avoids infinite recursion)
1472   (let ((features (cons 'gnus-sum features)))
1473     ;; Several of the declarations in gnus-sum are needed to load the
1474     ;; following files. Right now, these definitions have been
1475     ;; compiled but not defined (evaluated).  We could either do a
1476     ;; eval-and-compile about all of the declarations or evaluate the
1477     ;; source file.
1478     (if (boundp 'gnus-newsgroup-variables)
1479         nil
1480       (load "gnus-sum.el" t t t))
1481     (require 'gnus)
1482     (require 'gnus-art)))
1483
1484 ;; MIME stuff.
1485
1486 (defvar gnus-decode-encoded-word-methods
1487   '(mail-decode-encoded-word-string)
1488   "List of methods used to decode encoded words.
1489
1490 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1491 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1492 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1493 whose names match REGEXP.
1494
1495 For example:
1496 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1497  mail-decode-encoded-word-string
1498  (\"chinese\" . rfc1843-decode-string))")
1499
1500 (defvar gnus-decode-encoded-word-methods-cache nil)
1501
1502 (defun gnus-multi-decode-encoded-word-string (string)
1503   "Apply the functions from `gnus-encoded-word-methods' that match."
1504   (unless (and gnus-decode-encoded-word-methods-cache
1505                (eq gnus-newsgroup-name
1506                    (car gnus-decode-encoded-word-methods-cache)))
1507     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1508     (mapcar (lambda (x)
1509               (if (symbolp x)
1510                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1511                 (if (and gnus-newsgroup-name
1512                          (string-match (car x) gnus-newsgroup-name))
1513                     (nconc gnus-decode-encoded-word-methods-cache
1514                            (list (cdr x))))))
1515             gnus-decode-encoded-word-methods))
1516   (let ((xlist gnus-decode-encoded-word-methods-cache))
1517     (pop xlist)
1518     (while xlist
1519       (setq string (funcall (pop xlist) string))))
1520   string)
1521
1522 ;; Subject simplification.
1523
1524 (defun gnus-simplify-whitespace (str)
1525   "Remove excessive whitespace from STR."
1526   ;; Multiple spaces.
1527   (while (string-match "[ \t][ \t]+" str)
1528     (setq str (concat (substring str 0 (match-beginning 0))
1529                         " "
1530                         (substring str (match-end 0)))))
1531   ;; Leading spaces.
1532   (when (string-match "^[ \t]+" str)
1533     (setq str (substring str (match-end 0))))
1534   ;; Trailing spaces.
1535   (when (string-match "[ \t]+$" str)
1536     (setq str (substring str 0 (match-beginning 0))))
1537   str)
1538
1539 (defun gnus-simplify-all-whitespace (str)
1540   "Remove all whitespace from STR."
1541   (while (string-match "[ \t\n]+" str)
1542     (setq str (replace-match "" nil nil str)))
1543   str)
1544
1545 (defsubst gnus-simplify-subject-re (subject)
1546   "Remove \"Re:\" from subject lines."
1547   (if (string-match message-subject-re-regexp subject)
1548       (substring subject (match-end 0))
1549     subject))
1550
1551 (defun gnus-simplify-subject (subject &optional re-only)
1552   "Remove `Re:' and words in parentheses.
1553 If RE-ONLY is non-nil, strip leading `Re:'s only."
1554   (let ((case-fold-search t))           ;Ignore case.
1555     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1556     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1557       (setq subject (substring subject (match-end 0))))
1558     ;; Remove uninteresting prefixes.
1559     (when (and (not re-only)
1560                gnus-simplify-ignored-prefixes
1561                (string-match gnus-simplify-ignored-prefixes subject))
1562       (setq subject (substring subject (match-end 0))))
1563     ;; Remove words in parentheses from end.
1564     (unless re-only
1565       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1566         (setq subject (substring subject 0 (match-beginning 0)))))
1567     ;; Return subject string.
1568     subject))
1569
1570 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1571 ;; all whitespace.
1572 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1573   (goto-char (point-min))
1574   (while (re-search-forward regexp nil t)
1575     (replace-match (or newtext ""))))
1576
1577 (defun gnus-simplify-buffer-fuzzy ()
1578   "Simplify string in the buffer fuzzily.
1579 The string in the accessible portion of the current buffer is simplified.
1580 It is assumed to be a single-line subject.
1581 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1582 matter is removed.  Additional things can be deleted by setting
1583 `gnus-simplify-subject-fuzzy-regexp'."
1584   (let ((case-fold-search t)
1585         (modified-tick))
1586     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1587
1588     (while (not (eq modified-tick (buffer-modified-tick)))
1589       (setq modified-tick (buffer-modified-tick))
1590       (cond
1591        ((listp gnus-simplify-subject-fuzzy-regexp)
1592         (mapcar 'gnus-simplify-buffer-fuzzy-step
1593                 gnus-simplify-subject-fuzzy-regexp))
1594        (gnus-simplify-subject-fuzzy-regexp
1595         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1596       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1597       (gnus-simplify-buffer-fuzzy-step
1598        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1599       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1600
1601     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1602     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1603     (gnus-simplify-buffer-fuzzy-step " $")
1604     (gnus-simplify-buffer-fuzzy-step "^ +")))
1605
1606 (defun gnus-simplify-subject-fuzzy (subject)
1607   "Simplify a subject string fuzzily.
1608 See `gnus-simplify-buffer-fuzzy' for details."
1609   (save-excursion
1610     (gnus-set-work-buffer)
1611     (let ((case-fold-search t))
1612       ;; Remove uninteresting prefixes.
1613       (when (and gnus-simplify-ignored-prefixes
1614                  (string-match gnus-simplify-ignored-prefixes subject))
1615         (setq subject (substring subject (match-end 0))))
1616       (insert subject)
1617       (inline (gnus-simplify-buffer-fuzzy))
1618       (buffer-string))))
1619
1620 (defsubst gnus-simplify-subject-fully (subject)
1621   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1622   (cond
1623    (gnus-simplify-subject-functions
1624     (gnus-map-function gnus-simplify-subject-functions subject))
1625    ((null gnus-summary-gather-subject-limit)
1626     (gnus-simplify-subject-re subject))
1627    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1628     (gnus-simplify-subject-fuzzy subject))
1629    ((numberp gnus-summary-gather-subject-limit)
1630     (gnus-limit-string (gnus-simplify-subject-re subject)
1631                        gnus-summary-gather-subject-limit))
1632    (t
1633     subject)))
1634
1635 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1636   "Check whether two subjects are equal.
1637 If optional argument SIMPLE-FIRST is t, first argument is already
1638 simplified."
1639   (cond
1640    ((null simple-first)
1641     (equal (gnus-simplify-subject-fully s1)
1642            (gnus-simplify-subject-fully s2)))
1643    (t
1644     (equal s1
1645            (gnus-simplify-subject-fully s2)))))
1646
1647 (defun gnus-summary-bubble-group ()
1648   "Increase the score of the current group.
1649 This is a handy function to add to `gnus-summary-exit-hook' to
1650 increase the score of each group you read."
1651   (gnus-group-add-score gnus-newsgroup-name))
1652
1653 \f
1654 ;;;
1655 ;;; Gnus summary mode
1656 ;;;
1657
1658 (put 'gnus-summary-mode 'mode-class 'special)
1659
1660 (defvar gnus-article-commands-menu)
1661
1662 ;; Non-orthogonal keys
1663
1664 (gnus-define-keys gnus-summary-mode-map
1665   " " gnus-summary-next-page
1666   "\177" gnus-summary-prev-page
1667   [delete] gnus-summary-prev-page
1668   [backspace] gnus-summary-prev-page
1669   "\r" gnus-summary-scroll-up
1670   "\M-\r" gnus-summary-scroll-down
1671   "n" gnus-summary-next-unread-article
1672   "p" gnus-summary-prev-unread-article
1673   "N" gnus-summary-next-article
1674   "P" gnus-summary-prev-article
1675   "\M-\C-n" gnus-summary-next-same-subject
1676   "\M-\C-p" gnus-summary-prev-same-subject
1677   "\M-n" gnus-summary-next-unread-subject
1678   "\M-p" gnus-summary-prev-unread-subject
1679   "." gnus-summary-first-unread-article
1680   "," gnus-summary-best-unread-article
1681   "\M-s" gnus-summary-search-article-forward
1682   "\M-r" gnus-summary-search-article-backward
1683   "<" gnus-summary-beginning-of-article
1684   ">" gnus-summary-end-of-article
1685   "j" gnus-summary-goto-article
1686   "^" gnus-summary-refer-parent-article
1687   "\M-^" gnus-summary-refer-article
1688   "u" gnus-summary-tick-article-forward
1689   "!" gnus-summary-tick-article-forward
1690   "U" gnus-summary-tick-article-backward
1691   "d" gnus-summary-mark-as-read-forward
1692   "D" gnus-summary-mark-as-read-backward
1693   "E" gnus-summary-mark-as-expirable
1694   "\M-u" gnus-summary-clear-mark-forward
1695   "\M-U" gnus-summary-clear-mark-backward
1696   "k" gnus-summary-kill-same-subject-and-select
1697   "\C-k" gnus-summary-kill-same-subject
1698   "\M-\C-k" gnus-summary-kill-thread
1699   "\M-\C-l" gnus-summary-lower-thread
1700   "e" gnus-summary-edit-article
1701   "#" gnus-summary-mark-as-processable
1702   "\M-#" gnus-summary-unmark-as-processable
1703   "\M-\C-t" gnus-summary-toggle-threads
1704   "\M-\C-s" gnus-summary-show-thread
1705   "\M-\C-h" gnus-summary-hide-thread
1706   "\M-\C-f" gnus-summary-next-thread
1707   "\M-\C-b" gnus-summary-prev-thread
1708   [(meta down)] gnus-summary-next-thread
1709   [(meta up)] gnus-summary-prev-thread
1710   "\M-\C-u" gnus-summary-up-thread
1711   "\M-\C-d" gnus-summary-down-thread
1712   "&" gnus-summary-execute-command
1713   "c" gnus-summary-catchup-and-exit
1714   "\C-w" gnus-summary-mark-region-as-read
1715   "\C-t" gnus-summary-toggle-truncation
1716   "?" gnus-summary-mark-as-dormant
1717   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1718   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1719   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1720   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1721   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1722   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1723   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1724   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1725   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1726   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1727   "=" gnus-summary-expand-window
1728   "\C-x\C-s" gnus-summary-reselect-current-group
1729   "\M-g" gnus-summary-rescan-group
1730   "w" gnus-summary-stop-page-breaking
1731   "\C-c\C-r" gnus-summary-caesar-message
1732   "f" gnus-summary-followup
1733   "F" gnus-summary-followup-with-original
1734   "C" gnus-summary-cancel-article
1735   "r" gnus-summary-reply
1736   "R" gnus-summary-reply-with-original
1737   "\C-c\C-f" gnus-summary-mail-forward
1738   "o" gnus-summary-save-article
1739   "\C-o" gnus-summary-save-article-mail
1740   "|" gnus-summary-pipe-output
1741   "\M-k" gnus-summary-edit-local-kill
1742   "\M-K" gnus-summary-edit-global-kill
1743   ;; "V" gnus-version
1744   "\C-c\C-d" gnus-summary-describe-group
1745   "q" gnus-summary-exit
1746   "Q" gnus-summary-exit-no-update
1747   "\C-c\C-i" gnus-info-find-node
1748   gnus-mouse-2 gnus-mouse-pick-article
1749   "m" gnus-summary-mail-other-window
1750   "a" gnus-summary-post-news
1751   "i" gnus-summary-news-other-window
1752   "x" gnus-summary-limit-to-unread
1753   "s" gnus-summary-isearch-article
1754   "t" gnus-summary-toggle-header
1755   "g" gnus-summary-show-article
1756   "l" gnus-summary-goto-last-article
1757   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1758   "\C-d" gnus-summary-enter-digest-group
1759   "\M-\C-d" gnus-summary-read-document
1760   "\M-\C-e" gnus-summary-edit-parameters
1761   "\M-\C-a" gnus-summary-customize-parameters
1762   "\C-c\C-b" gnus-bug
1763   "*" gnus-cache-enter-article
1764   "\M-*" gnus-cache-remove-article
1765   "\M-&" gnus-summary-universal-argument
1766   "\C-l" gnus-recenter
1767   "I" gnus-summary-increase-score
1768   "L" gnus-summary-lower-score
1769   "\M-i" gnus-symbolic-argument
1770   "h" gnus-summary-select-article-buffer
1771
1772   "b" gnus-article-view-part
1773   "\M-t" gnus-summary-toggle-display-buttonized
1774
1775   "V" gnus-summary-score-map
1776   "X" gnus-uu-extract-map
1777   "S" gnus-summary-send-map)
1778
1779 ;; Sort of orthogonal keymap
1780 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1781   "t" gnus-summary-tick-article-forward
1782   "!" gnus-summary-tick-article-forward
1783   "d" gnus-summary-mark-as-read-forward
1784   "r" gnus-summary-mark-as-read-forward
1785   "c" gnus-summary-clear-mark-forward
1786   " " gnus-summary-clear-mark-forward
1787   "e" gnus-summary-mark-as-expirable
1788   "x" gnus-summary-mark-as-expirable
1789   "?" gnus-summary-mark-as-dormant
1790   "b" gnus-summary-set-bookmark
1791   "B" gnus-summary-remove-bookmark
1792   "#" gnus-summary-mark-as-processable
1793   "\M-#" gnus-summary-unmark-as-processable
1794   "S" gnus-summary-limit-include-expunged
1795   "C" gnus-summary-catchup
1796   "H" gnus-summary-catchup-to-here
1797   "h" gnus-summary-catchup-from-here
1798   "\C-c" gnus-summary-catchup-all
1799   "k" gnus-summary-kill-same-subject-and-select
1800   "K" gnus-summary-kill-same-subject
1801   "P" gnus-uu-mark-map)
1802
1803 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1804   "c" gnus-summary-clear-above
1805   "u" gnus-summary-tick-above
1806   "m" gnus-summary-mark-above
1807   "k" gnus-summary-kill-below)
1808
1809 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1810   "/" gnus-summary-limit-to-subject
1811   "n" gnus-summary-limit-to-articles
1812   "w" gnus-summary-pop-limit
1813   "s" gnus-summary-limit-to-subject
1814   "a" gnus-summary-limit-to-author
1815   "u" gnus-summary-limit-to-unread
1816   "m" gnus-summary-limit-to-marks
1817   "M" gnus-summary-limit-exclude-marks
1818   "v" gnus-summary-limit-to-score
1819   "*" gnus-summary-limit-include-cached
1820   "D" gnus-summary-limit-include-dormant
1821   "T" gnus-summary-limit-include-thread
1822   "d" gnus-summary-limit-exclude-dormant
1823   "t" gnus-summary-limit-to-age
1824   "." gnus-summary-limit-to-unseen
1825   "x" gnus-summary-limit-to-extra
1826   "p" gnus-summary-limit-to-display-predicate
1827   "E" gnus-summary-limit-include-expunged
1828   "c" gnus-summary-limit-exclude-childless-dormant
1829   "C" gnus-summary-limit-mark-excluded-as-read
1830   "o" gnus-summary-insert-old-articles
1831   "N" gnus-summary-insert-new-articles
1832   "r" gnus-summary-limit-to-replied)
1833
1834 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1835   "n" gnus-summary-next-unread-article
1836   "p" gnus-summary-prev-unread-article
1837   "N" gnus-summary-next-article
1838   "P" gnus-summary-prev-article
1839   "\C-n" gnus-summary-next-same-subject
1840   "\C-p" gnus-summary-prev-same-subject
1841   "\M-n" gnus-summary-next-unread-subject
1842   "\M-p" gnus-summary-prev-unread-subject
1843   "f" gnus-summary-first-unread-article
1844   "b" gnus-summary-best-unread-article
1845   "j" gnus-summary-goto-article
1846   "g" gnus-summary-goto-subject
1847   "l" gnus-summary-goto-last-article
1848   "o" gnus-summary-pop-article)
1849
1850 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1851   "k" gnus-summary-kill-thread
1852   "l" gnus-summary-lower-thread
1853   "i" gnus-summary-raise-thread
1854   "T" gnus-summary-toggle-threads
1855   "t" gnus-summary-rethread-current
1856   "^" gnus-summary-reparent-thread
1857   "s" gnus-summary-show-thread
1858   "S" gnus-summary-show-all-threads
1859   "h" gnus-summary-hide-thread
1860   "H" gnus-summary-hide-all-threads
1861   "n" gnus-summary-next-thread
1862   "p" gnus-summary-prev-thread
1863   "u" gnus-summary-up-thread
1864   "o" gnus-summary-top-thread
1865   "d" gnus-summary-down-thread
1866   "#" gnus-uu-mark-thread
1867   "\M-#" gnus-uu-unmark-thread)
1868
1869 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1870   "g" gnus-summary-prepare
1871   "c" gnus-summary-insert-cached-articles
1872   "d" gnus-summary-insert-dormant-articles)
1873
1874 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1875   "c" gnus-summary-catchup-and-exit
1876   "C" gnus-summary-catchup-all-and-exit
1877   "E" gnus-summary-exit-no-update
1878   "Q" gnus-summary-exit
1879   "Z" gnus-summary-exit
1880   "n" gnus-summary-catchup-and-goto-next-group
1881   "R" gnus-summary-reselect-current-group
1882   "G" gnus-summary-rescan-group
1883   "N" gnus-summary-next-group
1884   "s" gnus-summary-save-newsrc
1885   "P" gnus-summary-prev-group)
1886
1887 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1888   " " gnus-summary-next-page
1889   "n" gnus-summary-next-page
1890   "\177" gnus-summary-prev-page
1891   [delete] gnus-summary-prev-page
1892   "p" gnus-summary-prev-page
1893   "\r" gnus-summary-scroll-up
1894   "\M-\r" gnus-summary-scroll-down
1895   "<" gnus-summary-beginning-of-article
1896   ">" gnus-summary-end-of-article
1897   "b" gnus-summary-beginning-of-article
1898   "e" gnus-summary-end-of-article
1899   "^" gnus-summary-refer-parent-article
1900   "r" gnus-summary-refer-parent-article
1901   "D" gnus-summary-enter-digest-group
1902   "R" gnus-summary-refer-references
1903   "T" gnus-summary-refer-thread
1904   "g" gnus-summary-show-article
1905   "s" gnus-summary-isearch-article
1906   "P" gnus-summary-print-article
1907   "M" gnus-mailing-list-insinuate
1908   "t" gnus-article-babel)
1909
1910 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1911   "b" gnus-article-add-buttons
1912   "B" gnus-article-add-buttons-to-head
1913   "o" gnus-article-treat-overstrike
1914   "e" gnus-article-emphasize
1915   "w" gnus-article-fill-cited-article
1916   "Q" gnus-article-fill-long-lines
1917   "C" gnus-article-capitalize-sentences
1918   "c" gnus-article-remove-cr
1919   "q" gnus-article-de-quoted-unreadable
1920   "6" gnus-article-de-base64-unreadable
1921   "Z" gnus-article-decode-HZ
1922   "A" gnus-article-treat-ansi-sequences
1923   "h" gnus-article-wash-html
1924   "u" gnus-article-unsplit-urls
1925   "s" gnus-summary-force-verify-and-decrypt
1926   "f" gnus-article-display-x-face
1927   "l" gnus-summary-stop-page-breaking
1928   "r" gnus-summary-caesar-message
1929   "m" gnus-summary-morse-message
1930   "t" gnus-summary-toggle-header
1931   "g" gnus-treat-smiley
1932   "v" gnus-summary-verbose-headers
1933   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1934   "p" gnus-article-verify-x-pgp-sig
1935   "d" gnus-article-treat-dumbquotes)
1936
1937 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1938   ;; mnemonic: deuglif*Y*
1939   "u" gnus-article-outlook-unwrap-lines
1940   "a" gnus-article-outlook-repair-attribution
1941   "c" gnus-article-outlook-rearrange-citation
1942   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1943
1944 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1945   "a" gnus-article-hide
1946   "h" gnus-article-hide-headers
1947   "b" gnus-article-hide-boring-headers
1948   "s" gnus-article-hide-signature
1949   "c" gnus-article-hide-citation
1950   "C" gnus-article-hide-citation-in-followups
1951   "l" gnus-article-hide-list-identifiers
1952   "B" gnus-article-strip-banner
1953   "P" gnus-article-hide-pem
1954   "\C-c" gnus-article-hide-citation-maybe)
1955
1956 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1957   "a" gnus-article-highlight
1958   "h" gnus-article-highlight-headers
1959   "c" gnus-article-highlight-citation
1960   "s" gnus-article-highlight-signature)
1961
1962 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1963   "f" gnus-article-treat-fold-headers
1964   "u" gnus-article-treat-unfold-headers
1965   "n" gnus-article-treat-fold-newsgroups)
1966
1967 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1968   "x" gnus-article-display-x-face
1969   "d" gnus-article-display-face
1970   "s" gnus-treat-smiley
1971   "D" gnus-article-remove-images
1972   "f" gnus-treat-from-picon
1973   "m" gnus-treat-mail-picon
1974   "n" gnus-treat-newsgroups-picon)
1975
1976 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1977   "w" gnus-article-decode-mime-words
1978   "c" gnus-article-decode-charset
1979   "v" gnus-mime-view-all-parts
1980   "b" gnus-article-view-part)
1981
1982 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1983   "z" gnus-article-date-ut
1984   "u" gnus-article-date-ut
1985   "l" gnus-article-date-local
1986   "p" gnus-article-date-english
1987   "e" gnus-article-date-lapsed
1988   "o" gnus-article-date-original
1989   "i" gnus-article-date-iso8601
1990   "s" gnus-article-date-user)
1991
1992 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1993   "t" gnus-article-remove-trailing-blank-lines
1994   "l" gnus-article-strip-leading-blank-lines
1995   "m" gnus-article-strip-multiple-blank-lines
1996   "a" gnus-article-strip-blank-lines
1997   "A" gnus-article-strip-all-blank-lines
1998   "s" gnus-article-strip-leading-space
1999   "e" gnus-article-strip-trailing-space
2000   "w" gnus-article-remove-leading-whitespace)
2001
2002 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2003   "v" gnus-version
2004   "f" gnus-summary-fetch-faq
2005   "d" gnus-summary-describe-group
2006   "h" gnus-summary-describe-briefly
2007   "i" gnus-info-find-node
2008   "c" gnus-group-fetch-charter
2009   "C" gnus-group-fetch-control)
2010
2011 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2012   "e" gnus-summary-expire-articles
2013   "\M-\C-e" gnus-summary-expire-articles-now
2014   "\177" gnus-summary-delete-article
2015   [delete] gnus-summary-delete-article
2016   [backspace] gnus-summary-delete-article
2017   "m" gnus-summary-move-article
2018   "r" gnus-summary-respool-article
2019   "w" gnus-summary-edit-article
2020   "c" gnus-summary-copy-article
2021   "B" gnus-summary-crosspost-article
2022   "q" gnus-summary-respool-query
2023   "t" gnus-summary-respool-trace
2024   "i" gnus-summary-import-article
2025   "I" gnus-summary-create-article
2026   "p" gnus-summary-article-posted-p)
2027
2028 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2029   "o" gnus-summary-save-article
2030   "m" gnus-summary-save-article-mail
2031   "F" gnus-summary-write-article-file
2032   "r" gnus-summary-save-article-rmail
2033   "f" gnus-summary-save-article-file
2034   "b" gnus-summary-save-article-body-file
2035   "h" gnus-summary-save-article-folder
2036   "v" gnus-summary-save-article-vm
2037   "p" gnus-summary-pipe-output
2038   "P" gnus-summary-muttprint
2039   "s" gnus-soup-add-article)
2040
2041 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2042   "b" gnus-summary-display-buttonized
2043   "m" gnus-summary-repair-multipart
2044   "v" gnus-article-view-part
2045   "o" gnus-article-save-part
2046   "c" gnus-article-copy-part
2047   "C" gnus-article-view-part-as-charset
2048   "e" gnus-article-view-part-externally
2049   "E" gnus-article-encrypt-body
2050   "i" gnus-article-inline-part
2051   "|" gnus-article-pipe-part)
2052
2053 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2054   "p" gnus-summary-mark-as-processable
2055   "u" gnus-summary-unmark-as-processable
2056   "U" gnus-summary-unmark-all-processable
2057   "v" gnus-uu-mark-over
2058   "s" gnus-uu-mark-series
2059   "r" gnus-uu-mark-region
2060   "g" gnus-uu-unmark-region
2061   "R" gnus-uu-mark-by-regexp
2062   "G" gnus-uu-unmark-by-regexp
2063   "t" gnus-uu-mark-thread
2064   "T" gnus-uu-unmark-thread
2065   "a" gnus-uu-mark-all
2066   "b" gnus-uu-mark-buffer
2067   "S" gnus-uu-mark-sparse
2068   "k" gnus-summary-kill-process-mark
2069   "y" gnus-summary-yank-process-mark
2070   "w" gnus-summary-save-process-mark
2071   "i" gnus-uu-invert-processable)
2072
2073 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2074   ;;"x" gnus-uu-extract-any
2075   "m" gnus-summary-save-parts
2076   "u" gnus-uu-decode-uu
2077   "U" gnus-uu-decode-uu-and-save
2078   "s" gnus-uu-decode-unshar
2079   "S" gnus-uu-decode-unshar-and-save
2080   "o" gnus-uu-decode-save
2081   "O" gnus-uu-decode-save
2082   "b" gnus-uu-decode-binhex
2083   "B" gnus-uu-decode-binhex
2084   "p" gnus-uu-decode-postscript
2085   "P" gnus-uu-decode-postscript-and-save)
2086
2087 (gnus-define-keys
2088     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2089   "u" gnus-uu-decode-uu-view
2090   "U" gnus-uu-decode-uu-and-save-view
2091   "s" gnus-uu-decode-unshar-view
2092   "S" gnus-uu-decode-unshar-and-save-view
2093   "o" gnus-uu-decode-save-view
2094   "O" gnus-uu-decode-save-view
2095   "b" gnus-uu-decode-binhex-view
2096   "B" gnus-uu-decode-binhex-view
2097   "p" gnus-uu-decode-postscript-view
2098   "P" gnus-uu-decode-postscript-and-save-view)
2099
2100 (defvar gnus-article-post-menu nil)
2101
2102 (defconst gnus-summary-menu-maxlen 20)
2103
2104 (defun gnus-summary-menu-split (menu)
2105   ;; If we have lots of elements, divide them into groups of 20
2106   ;; and make a pane (or submenu) for each one.
2107   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2108       (let ((menu menu) sublists next
2109             (i 1))
2110         (while menu
2111           ;; Pull off the next gnus-summary-menu-maxlen elements
2112           ;; and make them the next element of sublist.
2113           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2114           (if next
2115               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2116                       nil))
2117           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2118                                              (aref (car (last menu)) 0)) menu)
2119                                sublists))
2120           (setq i (1+ i))
2121           (setq menu next))
2122         (nreverse sublists))
2123     ;; Few elements--put them all in one pane.
2124     menu))
2125
2126 (defun gnus-summary-make-menu-bar ()
2127   (gnus-turn-off-edit-menu 'summary)
2128
2129   (unless (boundp 'gnus-summary-misc-menu)
2130
2131     (easy-menu-define
2132       gnus-summary-kill-menu gnus-summary-mode-map ""
2133       (cons
2134        "Score"
2135        (nconc
2136         (list
2137          ["Customize" gnus-score-customize t])
2138         (gnus-make-score-map 'increase)
2139         (gnus-make-score-map 'lower)
2140         '(("Mark"
2141            ["Kill below" gnus-summary-kill-below t]
2142            ["Mark above" gnus-summary-mark-above t]
2143            ["Tick above" gnus-summary-tick-above t]
2144            ["Clear above" gnus-summary-clear-above t])
2145           ["Current score" gnus-summary-current-score t]
2146           ["Set score" gnus-summary-set-score t]
2147           ["Switch current score file..." gnus-score-change-score-file t]
2148           ["Set mark below..." gnus-score-set-mark-below t]
2149           ["Set expunge below..." gnus-score-set-expunge-below t]
2150           ["Edit current score file" gnus-score-edit-current-scores t]
2151           ["Edit score file" gnus-score-edit-file t]
2152           ["Trace score" gnus-score-find-trace t]
2153           ["Find words" gnus-score-find-favourite-words t]
2154           ["Rescore buffer" gnus-summary-rescore t]
2155           ["Increase score..." gnus-summary-increase-score t]
2156           ["Lower score..." gnus-summary-lower-score t]))))
2157
2158     ;; Define both the Article menu in the summary buffer and the
2159     ;; equivalent Commands menu in the article buffer here for
2160     ;; consistency.
2161     (let ((innards
2162            `(("Hide"
2163               ["All" gnus-article-hide t]
2164               ["Headers" gnus-article-hide-headers t]
2165               ["Signature" gnus-article-hide-signature t]
2166               ["Citation" gnus-article-hide-citation t]
2167               ["List identifiers" gnus-article-hide-list-identifiers t]
2168               ["Banner" gnus-article-strip-banner t]
2169               ["Boring headers" gnus-article-hide-boring-headers t])
2170              ("Highlight"
2171               ["All" gnus-article-highlight t]
2172               ["Headers" gnus-article-highlight-headers t]
2173               ["Signature" gnus-article-highlight-signature t]
2174               ["Citation" gnus-article-highlight-citation t])
2175              ("MIME"
2176               ["Words" gnus-article-decode-mime-words t]
2177               ["Charset" gnus-article-decode-charset t]
2178               ["QP" gnus-article-de-quoted-unreadable t]
2179               ["Base64" gnus-article-de-base64-unreadable t]
2180               ["View MIME buttons" gnus-summary-display-buttonized t]
2181               ["View all" gnus-mime-view-all-parts t]
2182               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2183               ["Encrypt body" gnus-article-encrypt-body
2184                :active (not (gnus-group-read-only-p))
2185                ,@(if (featurep 'xemacs) nil
2186                    '(:help "Encrypt the message body on disk"))]
2187               ["Extract all parts..." gnus-summary-save-parts t]
2188               ("Multipart"
2189                ["Repair multipart" gnus-summary-repair-multipart t]
2190                ["Pipe part..." gnus-article-pipe-part t]
2191                ["Inline part" gnus-article-inline-part t]
2192                ["Encrypt body" gnus-article-encrypt-body
2193                 :active (not (gnus-group-read-only-p))
2194                ,@(if (featurep 'xemacs) nil
2195                    '(:help "Encrypt the message body on disk"))]
2196                ["View part externally" gnus-article-view-part-externally t]
2197                ["View part with charset..." gnus-article-view-part-as-charset t]
2198                ["Copy part" gnus-article-copy-part t]
2199                ["Save part..." gnus-article-save-part t]
2200                ["View part" gnus-article-view-part t]))
2201              ("Date"
2202               ["Local" gnus-article-date-local t]
2203               ["ISO8601" gnus-article-date-iso8601 t]
2204               ["UT" gnus-article-date-ut t]
2205               ["Original" gnus-article-date-original t]
2206               ["Lapsed" gnus-article-date-lapsed t]
2207               ["User-defined" gnus-article-date-user t])
2208              ("Display"
2209               ["Remove images" gnus-article-remove-images t]
2210               ["Toggle smiley" gnus-treat-smiley t]
2211               ["Show X-Face" gnus-article-display-x-face t]
2212               ["Show picons in From" gnus-treat-from-picon t]
2213               ["Show picons in mail headers" gnus-treat-mail-picon t]
2214               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2215               ("View as different encoding"
2216                ,@(gnus-summary-menu-split
2217                   (mapcar
2218                    (lambda (cs)
2219                      ;; Since easymenu under Emacs doesn't allow
2220                      ;; lambda forms for menu commands, we should
2221                      ;; provide intern'ed function symbols.
2222                      (let ((command (intern (format "\
2223 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2224                        (fset command
2225                              `(lambda ()
2226                                 (interactive)
2227                                 (let ((gnus-summary-show-article-charset-alist
2228                                        '((1 . ,cs))))
2229                                   (gnus-summary-show-article 1))))
2230                        `[,(symbol-name cs) ,command t]))
2231                    (sort (if (fboundp 'coding-system-list)
2232                              (coding-system-list)
2233                            (mapcar 'car mm-mime-mule-charset-alist))
2234                          'string<)))))
2235              ("Washing"
2236               ("Remove Blanks"
2237                ["Leading" gnus-article-strip-leading-blank-lines t]
2238                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2239                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2240                ["All of the above" gnus-article-strip-blank-lines t]
2241                ["All" gnus-article-strip-all-blank-lines t]
2242                ["Leading space" gnus-article-strip-leading-space t]
2243                ["Trailing space" gnus-article-strip-trailing-space t]
2244                ["Leading space in headers"
2245                 gnus-article-remove-leading-whitespace t])
2246               ["Overstrike" gnus-article-treat-overstrike t]
2247               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2248               ["Emphasis" gnus-article-emphasize t]
2249               ["Word wrap" gnus-article-fill-cited-article t]
2250               ["Fill long lines" gnus-article-fill-long-lines t]
2251               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2252               ["Remove CR" gnus-article-remove-cr t]
2253               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2254               ["Base64" gnus-article-de-base64-unreadable t]
2255               ["Rot 13" gnus-summary-caesar-message
2256                ,@(if (featurep 'xemacs) '(t)
2257                    '(:help "\"Caesar rotate\" article by 13"))]
2258               ["Morse decode" gnus-summary-morse-message t]
2259               ["Unix pipe..." gnus-summary-pipe-message t]
2260               ["Add buttons" gnus-article-add-buttons t]
2261               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2262               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2263               ["Verbose header" gnus-summary-verbose-headers t]
2264               ["Toggle header" gnus-summary-toggle-header t]
2265               ["Unfold headers" gnus-article-treat-unfold-headers t]
2266               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2267               ["Html" gnus-article-wash-html t]
2268               ["Unsplit URLs" gnus-article-unsplit-urls t]
2269               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2270               ["Decode HZ" gnus-article-decode-HZ t]
2271               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2272               ("(Outlook) Deuglify"
2273                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2274                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2275                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2276                ["Full (Outlook) deuglify"
2277                 gnus-article-outlook-deuglify-article t])
2278               )
2279              ("Output"
2280               ["Save in default format..." gnus-summary-save-article
2281                ,@(if (featurep 'xemacs) '(t)
2282                    '(:help "Save article using default method"))]
2283               ["Save in file..." gnus-summary-save-article-file
2284                ,@(if (featurep 'xemacs) '(t)
2285                    '(:help "Save article in file"))]
2286               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2287               ["Save in MH folder..." gnus-summary-save-article-folder t]
2288               ["Save in VM folder..." gnus-summary-save-article-vm t]
2289               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2290               ["Save body in file..." gnus-summary-save-article-body-file t]
2291               ["Pipe through a filter..." gnus-summary-pipe-output t]
2292               ["Add to SOUP packet" gnus-soup-add-article t]
2293               ["Print with Muttprint..." gnus-summary-muttprint t]
2294               ["Print" gnus-summary-print-article
2295                ,@(if (featurep 'xemacs) '(t)
2296                    '(:help "Generate and print a PostScript image"))])
2297              ("Copy, move,... (Backend)"
2298               ,@(if (featurep 'xemacs) nil
2299                   '(:help "Copying, moving, expiring articles..."))
2300               ["Respool article..." gnus-summary-respool-article t]
2301               ["Move article..." gnus-summary-move-article
2302                (gnus-check-backend-function
2303                 'request-move-article gnus-newsgroup-name)]
2304               ["Copy article..." gnus-summary-copy-article t]
2305               ["Crosspost article..." gnus-summary-crosspost-article
2306                (gnus-check-backend-function
2307                 'request-replace-article gnus-newsgroup-name)]
2308               ["Import file..." gnus-summary-import-article
2309                (gnus-check-backend-function
2310                 'request-accept-article gnus-newsgroup-name)]
2311               ["Create article..." gnus-summary-create-article
2312                (gnus-check-backend-function
2313                 'request-accept-article gnus-newsgroup-name)]
2314               ["Check if posted" gnus-summary-article-posted-p t]
2315               ["Edit article" gnus-summary-edit-article
2316                (not (gnus-group-read-only-p))]
2317               ["Delete article" gnus-summary-delete-article
2318                (gnus-check-backend-function
2319                 'request-expire-articles gnus-newsgroup-name)]
2320               ["Query respool" gnus-summary-respool-query t]
2321               ["Trace respool" gnus-summary-respool-trace t]
2322               ["Delete expirable articles" gnus-summary-expire-articles-now
2323                (gnus-check-backend-function
2324                 'request-expire-articles gnus-newsgroup-name)])
2325              ("Extract"
2326               ["Uudecode" gnus-uu-decode-uu
2327                ,@(if (featurep 'xemacs) '(t)
2328                    '(:help "Decode uuencoded article(s)"))]
2329               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2330               ["Unshar" gnus-uu-decode-unshar t]
2331               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2332               ["Save" gnus-uu-decode-save t]
2333               ["Binhex" gnus-uu-decode-binhex t]
2334               ["Postscript" gnus-uu-decode-postscript t]
2335               ["All MIME parts" gnus-summary-save-parts t])
2336              ("Cache"
2337               ["Enter article" gnus-cache-enter-article t]
2338               ["Remove article" gnus-cache-remove-article t])
2339              ["Translate" gnus-article-babel t]
2340              ["Select article buffer" gnus-summary-select-article-buffer t]
2341              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2342              ["Isearch article..." gnus-summary-isearch-article t]
2343              ["Beginning of the article" gnus-summary-beginning-of-article t]
2344              ["End of the article" gnus-summary-end-of-article t]
2345              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2346              ["Fetch referenced articles" gnus-summary-refer-references t]
2347              ["Fetch current thread" gnus-summary-refer-thread t]
2348              ["Fetch article with id..." gnus-summary-refer-article t]
2349              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2350              ["Redisplay" gnus-summary-show-article t]
2351              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2352       (easy-menu-define
2353         gnus-summary-article-menu gnus-summary-mode-map ""
2354         (cons "Article" innards))
2355
2356       (if (not (keymapp gnus-summary-article-menu))
2357           (easy-menu-define
2358             gnus-article-commands-menu gnus-article-mode-map ""
2359             (cons "Commands" innards))
2360         ;; in Emacs, don't share menu.
2361         (setq gnus-article-commands-menu
2362               (copy-keymap gnus-summary-article-menu))
2363         (define-key gnus-article-mode-map [menu-bar commands]
2364           (cons "Commands" gnus-article-commands-menu))))
2365
2366     (easy-menu-define
2367       gnus-summary-thread-menu gnus-summary-mode-map ""
2368       '("Threads"
2369         ["Find all messages in thread" gnus-summary-refer-thread t]
2370         ["Toggle threading" gnus-summary-toggle-threads t]
2371         ["Hide threads" gnus-summary-hide-all-threads t]
2372         ["Show threads" gnus-summary-show-all-threads t]
2373         ["Hide thread" gnus-summary-hide-thread t]
2374         ["Show thread" gnus-summary-show-thread t]
2375         ["Go to next thread" gnus-summary-next-thread t]
2376         ["Go to previous thread" gnus-summary-prev-thread t]
2377         ["Go down thread" gnus-summary-down-thread t]
2378         ["Go up thread" gnus-summary-up-thread t]
2379         ["Top of thread" gnus-summary-top-thread t]
2380         ["Mark thread as read" gnus-summary-kill-thread t]
2381         ["Lower thread score" gnus-summary-lower-thread t]
2382         ["Raise thread score" gnus-summary-raise-thread t]
2383         ["Rethread current" gnus-summary-rethread-current t]))
2384
2385     (easy-menu-define
2386       gnus-summary-post-menu gnus-summary-mode-map ""
2387       `("Post"
2388         ["Send a message (mail or news)" gnus-summary-post-news
2389          ,@(if (featurep 'xemacs) '(t)
2390              '(:help "Compose a new message (mail or news)"))]
2391         ["Followup" gnus-summary-followup
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Post followup to this article"))]
2394         ["Followup and yank" gnus-summary-followup-with-original
2395          ,@(if (featurep 'xemacs) '(t)
2396              '(:help "Post followup to this article, quoting its contents"))]
2397         ["Supersede article" gnus-summary-supersede-article t]
2398         ["Cancel article" gnus-summary-cancel-article
2399          ,@(if (featurep 'xemacs) '(t)
2400              '(:help "Cancel an article you posted"))]
2401         ["Reply" gnus-summary-reply t]
2402         ["Reply and yank" gnus-summary-reply-with-original t]
2403         ["Wide reply" gnus-summary-wide-reply t]
2404         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2405          ,@(if (featurep 'xemacs) '(t)
2406              '(:help "Mail a reply, quoting this article"))]
2407         ["Very wide reply" gnus-summary-very-wide-reply t]
2408         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2409          ,@(if (featurep 'xemacs) '(t)
2410              '(:help "Mail a very wide reply, quoting this article"))]
2411         ["Mail forward" gnus-summary-mail-forward t]
2412         ["Post forward" gnus-summary-post-forward t]
2413         ["Digest and mail" gnus-uu-digest-mail-forward t]
2414         ["Digest and post" gnus-uu-digest-post-forward t]
2415         ["Resend message" gnus-summary-resend-message t]
2416         ["Resend message edit" gnus-summary-resend-message-edit t]
2417         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2418         ["Send a mail" gnus-summary-mail-other-window t]
2419         ["Create a local message" gnus-summary-news-other-window t]
2420         ["Uuencode and post" gnus-uu-post-news
2421          ,@(if (featurep 'xemacs) '(t)
2422              '(:help "Post a uuencoded article"))]
2423         ["Followup via news" gnus-summary-followup-to-mail t]
2424         ["Followup via news and yank"
2425          gnus-summary-followup-to-mail-with-original t]
2426         ;;("Draft"
2427         ;;["Send" gnus-summary-send-draft t]
2428         ;;["Send bounced" gnus-resend-bounced-mail t])
2429         ))
2430
2431     (cond
2432      ((not (keymapp gnus-summary-post-menu))
2433       (setq gnus-article-post-menu gnus-summary-post-menu))
2434      ((not gnus-article-post-menu)
2435       ;; Don't share post menu.
2436       (setq gnus-article-post-menu
2437             (copy-keymap gnus-summary-post-menu))))
2438     (define-key gnus-article-mode-map [menu-bar post]
2439       (cons "Post" gnus-article-post-menu))
2440
2441     (easy-menu-define
2442       gnus-summary-misc-menu gnus-summary-mode-map ""
2443       `("Gnus"
2444         ("Mark Read"
2445          ["Mark as read" gnus-summary-mark-as-read-forward t]
2446          ["Mark same subject and select"
2447           gnus-summary-kill-same-subject-and-select t]
2448          ["Mark same subject" gnus-summary-kill-same-subject t]
2449          ["Catchup" gnus-summary-catchup
2450           ,@(if (featurep 'xemacs) '(t)
2451               '(:help "Mark unread articles in this group as read"))]
2452          ["Catchup all" gnus-summary-catchup-all t]
2453          ["Catchup to here" gnus-summary-catchup-to-here t]
2454          ["Catchup from here" gnus-summary-catchup-from-here t]
2455          ["Catchup region" gnus-summary-mark-region-as-read
2456           (gnus-mark-active-p)]
2457          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2458         ("Mark Various"
2459          ["Tick" gnus-summary-tick-article-forward t]
2460          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2461          ["Remove marks" gnus-summary-clear-mark-forward t]
2462          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2463          ["Set bookmark" gnus-summary-set-bookmark t]
2464          ["Remove bookmark" gnus-summary-remove-bookmark t])
2465         ("Limit to"
2466          ["Marks..." gnus-summary-limit-to-marks t]
2467          ["Subject..." gnus-summary-limit-to-subject t]
2468          ["Author..." gnus-summary-limit-to-author t]
2469          ["Age..." gnus-summary-limit-to-age t]
2470          ["Extra..." gnus-summary-limit-to-extra t]
2471          ["Score..." gnus-summary-limit-to-score t]
2472          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2473          ["Unread" gnus-summary-limit-to-unread t]
2474          ["Unseen" gnus-summary-limit-to-unseen t]
2475          ["Replied" gnus-summary-limit-to-replied t]
2476          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2477          ["Next articles" gnus-summary-limit-to-articles t]
2478          ["Pop limit" gnus-summary-pop-limit t]
2479          ["Show dormant" gnus-summary-limit-include-dormant t]
2480          ["Hide childless dormant"
2481           gnus-summary-limit-exclude-childless-dormant t]
2482          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2483          ["Hide marked" gnus-summary-limit-exclude-marks t]
2484          ["Show expunged" gnus-summary-limit-include-expunged t])
2485         ("Process Mark"
2486          ["Set mark" gnus-summary-mark-as-processable t]
2487          ["Remove mark" gnus-summary-unmark-as-processable t]
2488          ["Remove all marks" gnus-summary-unmark-all-processable t]
2489          ["Invert marks" gnus-uu-invert-processable t]
2490          ["Mark above" gnus-uu-mark-over t]
2491          ["Mark series" gnus-uu-mark-series t]
2492          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2493          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2494          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2495          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2496          ["Mark all" gnus-uu-mark-all t]
2497          ["Mark buffer" gnus-uu-mark-buffer t]
2498          ["Mark sparse" gnus-uu-mark-sparse t]
2499          ["Mark thread" gnus-uu-mark-thread t]
2500          ["Unmark thread" gnus-uu-unmark-thread t]
2501          ("Process Mark Sets"
2502           ["Kill" gnus-summary-kill-process-mark t]
2503           ["Yank" gnus-summary-yank-process-mark
2504            gnus-newsgroup-process-stack]
2505           ["Save" gnus-summary-save-process-mark t]
2506           ["Run command on marked..." gnus-summary-universal-argument t]))
2507         ("Scroll article"
2508          ["Page forward" gnus-summary-next-page
2509           ,@(if (featurep 'xemacs) '(t)
2510               '(:help "Show next page of article"))]
2511          ["Page backward" gnus-summary-prev-page
2512           ,@(if (featurep 'xemacs) '(t)
2513               '(:help "Show previous page of article"))]
2514          ["Line forward" gnus-summary-scroll-up t])
2515         ("Move"
2516          ["Next unread article" gnus-summary-next-unread-article t]
2517          ["Previous unread article" gnus-summary-prev-unread-article t]
2518          ["Next article" gnus-summary-next-article t]
2519          ["Previous article" gnus-summary-prev-article t]
2520          ["Next unread subject" gnus-summary-next-unread-subject t]
2521          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2522          ["Next article same subject" gnus-summary-next-same-subject t]
2523          ["Previous article same subject" gnus-summary-prev-same-subject t]
2524          ["First unread article" gnus-summary-first-unread-article t]
2525          ["Best unread article" gnus-summary-best-unread-article t]
2526          ["Go to subject number..." gnus-summary-goto-subject t]
2527          ["Go to article number..." gnus-summary-goto-article t]
2528          ["Go to the last article" gnus-summary-goto-last-article t]
2529          ["Pop article off history" gnus-summary-pop-article t])
2530         ("Sort"
2531          ["Sort by number" gnus-summary-sort-by-number t]
2532          ["Sort by author" gnus-summary-sort-by-author t]
2533          ["Sort by subject" gnus-summary-sort-by-subject t]
2534          ["Sort by date" gnus-summary-sort-by-date t]
2535          ["Sort by score" gnus-summary-sort-by-score t]
2536          ["Sort by lines" gnus-summary-sort-by-lines t]
2537          ["Sort by characters" gnus-summary-sort-by-chars t]
2538          ["Randomize" gnus-summary-sort-by-random t]
2539          ["Original sort" gnus-summary-sort-by-original t])
2540         ("Help"
2541          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2542          ["Describe group" gnus-summary-describe-group t]
2543          ["Fetch charter" gnus-group-fetch-charter
2544           ,@(if (featurep 'xemacs) nil
2545               '(:help "Display the charter of the current group"))]
2546          ["Fetch control message" gnus-group-fetch-control
2547           ,@(if (featurep 'xemacs) nil
2548               '(:help "Display the archived control message for the current group"))]
2549          ["Read manual" gnus-info-find-node t])
2550         ("Modes"
2551          ["Pick and read" gnus-pick-mode t]
2552          ["Binary" gnus-binary-mode t])
2553         ("Regeneration"
2554          ["Regenerate" gnus-summary-prepare t]
2555          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2556          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2557          ["Toggle threading" gnus-summary-toggle-threads t])
2558         ["See old articles" gnus-summary-insert-old-articles t]
2559         ["See new articles" gnus-summary-insert-new-articles t]
2560         ["Filter articles..." gnus-summary-execute-command t]
2561         ["Run command on articles..." gnus-summary-universal-argument t]
2562         ["Search articles forward..." gnus-summary-search-article-forward t]
2563         ["Search articles backward..." gnus-summary-search-article-backward t]
2564         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2565         ["Expand window" gnus-summary-expand-window t]
2566         ["Expire expirable articles" gnus-summary-expire-articles
2567          (gnus-check-backend-function
2568           'request-expire-articles gnus-newsgroup-name)]
2569         ["Edit local kill file" gnus-summary-edit-local-kill t]
2570         ["Edit main kill file" gnus-summary-edit-global-kill t]
2571         ["Edit group parameters" gnus-summary-edit-parameters t]
2572         ["Customize group parameters" gnus-summary-customize-parameters t]
2573         ["Send a bug report" gnus-bug t]
2574         ("Exit"
2575          ["Catchup and exit" gnus-summary-catchup-and-exit
2576           ,@(if (featurep 'xemacs) '(t)
2577               '(:help "Mark unread articles in this group as read, then exit"))]
2578          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2579          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2580          ["Exit group" gnus-summary-exit
2581           ,@(if (featurep 'xemacs) '(t)
2582               '(:help "Exit current group, return to group selection mode"))]
2583          ["Exit group without updating" gnus-summary-exit-no-update t]
2584          ["Exit and goto next group" gnus-summary-next-group t]
2585          ["Exit and goto prev group" gnus-summary-prev-group t]
2586          ["Reselect group" gnus-summary-reselect-current-group t]
2587          ["Rescan group" gnus-summary-rescan-group t]
2588          ["Update dribble" gnus-summary-save-newsrc t])))
2589
2590     (gnus-run-hooks 'gnus-summary-menu-hook)))
2591
2592 (defvar gnus-summary-tool-bar-map nil)
2593
2594 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2595 (defun gnus-summary-make-tool-bar ()
2596   (if (and (fboundp 'tool-bar-add-item-from-menu)
2597            (default-value 'tool-bar-mode)
2598            (not gnus-summary-tool-bar-map))
2599       (setq gnus-summary-tool-bar-map
2600             (let ((tool-bar-map (make-sparse-keymap))
2601                   (load-path (mm-image-load-path)))
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-post-news "post" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-summary-followup "followup" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-reply "reply" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2620               (tool-bar-add-item-from-menu
2621                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2622               (tool-bar-add-item-from-menu
2623                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2624               (tool-bar-add-item-from-menu
2625                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2626               (tool-bar-add-item-from-menu
2627                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2628               (tool-bar-add-item-from-menu
2629                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2630               (tool-bar-add-item-from-menu
2631                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2632               (tool-bar-add-item-from-menu
2633                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2634               tool-bar-map)))
2635   (if gnus-summary-tool-bar-map
2636       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2637
2638 (defun gnus-score-set-default (var value)
2639   "A version of set that updates the GNU Emacs menu-bar."
2640   (set var value)
2641   ;; It is the message that forces the active status to be updated.
2642   (message ""))
2643
2644 (defun gnus-make-score-map (type)
2645   "Make a summary score map of type TYPE."
2646   (if t
2647       nil
2648     (let ((headers '(("author" "from" string)
2649                      ("subject" "subject" string)
2650                      ("article body" "body" string)
2651                      ("article head" "head" string)
2652                      ("xref" "xref" string)
2653                      ("extra header" "extra" string)
2654                      ("lines" "lines" number)
2655                      ("followups to author" "followup" string)))
2656           (types '((number ("less than" <)
2657                            ("greater than" >)
2658                            ("equal" =))
2659                    (string ("substring" s)
2660                            ("exact string" e)
2661                            ("fuzzy string" f)
2662                            ("regexp" r))))
2663           (perms '(("temporary" (current-time-string))
2664                    ("permanent" nil)
2665                    ("immediate" now)))
2666           header)
2667       (list
2668        (apply
2669         'nconc
2670         (list
2671          (if (eq type 'lower)
2672              "Lower score"
2673            "Increase score"))
2674         (let (outh)
2675           (while headers
2676             (setq header (car headers))
2677             (setq outh
2678                   (cons
2679                    (apply
2680                     'nconc
2681                     (list (car header))
2682                     (let ((ts (cdr (assoc (nth 2 header) types)))
2683                           outt)
2684                       (while ts
2685                         (setq outt
2686                               (cons
2687                                (apply
2688                                 'nconc
2689                                 (list (caar ts))
2690                                 (let ((ps perms)
2691                                       outp)
2692                                   (while ps
2693                                     (setq outp
2694                                           (cons
2695                                            (vector
2696                                             (caar ps)
2697                                             (list
2698                                              'gnus-summary-score-entry
2699                                              (nth 1 header)
2700                                              (if (or (string= (nth 1 header)
2701                                                               "head")
2702                                                      (string= (nth 1 header)
2703                                                               "body"))
2704                                                  ""
2705                                                (list 'gnus-summary-header
2706                                                      (nth 1 header)))
2707                                              (list 'quote (nth 1 (car ts)))
2708                                              (list 'gnus-score-delta-default
2709                                                    nil)
2710                                              (nth 1 (car ps))
2711                                              t)
2712                                             t)
2713                                            outp))
2714                                     (setq ps (cdr ps)))
2715                                   (list (nreverse outp))))
2716                                outt))
2717                         (setq ts (cdr ts)))
2718                       (list (nreverse outt))))
2719                    outh))
2720             (setq headers (cdr headers)))
2721           (list (nreverse outh))))))))
2722
2723 \f
2724
2725 (defun gnus-summary-mode (&optional group)
2726   "Major mode for reading articles.
2727
2728 All normal editing commands are switched off.
2729 \\<gnus-summary-mode-map>
2730 Each line in this buffer represents one article.  To read an
2731 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2732 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2733 respectively.
2734
2735 You can also post articles and send mail from this buffer.  To
2736 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2737 of an article, type `\\[gnus-summary-reply]'.
2738
2739 There are approx. one gazillion commands you can execute in this
2740 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2741
2742 The following commands are available:
2743
2744 \\{gnus-summary-mode-map}"
2745   (interactive)
2746   (kill-all-local-variables)
2747   (when (gnus-visual-p 'summary-menu 'menu)
2748     (gnus-summary-make-menu-bar)
2749     (gnus-summary-make-tool-bar))
2750   (gnus-summary-make-local-variables)
2751   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2752     (gnus-summary-make-local-variables))
2753   (gnus-make-thread-indent-array)
2754   (gnus-simplify-mode-line)
2755   (setq major-mode 'gnus-summary-mode)
2756   (setq mode-name "Summary")
2757   (make-local-variable 'minor-mode-alist)
2758   (use-local-map gnus-summary-mode-map)
2759   (buffer-disable-undo)
2760   (setq buffer-read-only t              ;Disable modification
2761         show-trailing-whitespace nil)
2762   (setq truncate-lines t)
2763   (setq selective-display t)
2764   (setq selective-display-ellipses t)   ;Display `...'
2765   (gnus-summary-set-display-table)
2766   (gnus-set-default-directory)
2767   (setq gnus-newsgroup-name group)
2768   (make-local-variable 'gnus-summary-line-format)
2769   (make-local-variable 'gnus-summary-line-format-spec)
2770   (make-local-variable 'gnus-summary-dummy-line-format)
2771   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2772   (make-local-variable 'gnus-summary-mark-positions)
2773   (gnus-make-local-hook 'pre-command-hook)
2774   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2775   (gnus-run-hooks 'gnus-summary-mode-hook)
2776   (turn-on-gnus-mailing-list-mode)
2777   (mm-enable-multibyte)
2778   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2779   (gnus-update-summary-mark-positions))
2780
2781 (defun gnus-summary-make-local-variables ()
2782   "Make all the local summary buffer variables."
2783   (let (global)
2784     (dolist (local gnus-summary-local-variables)
2785       (if (consp local)
2786           (progn
2787             (if (eq (cdr local) 'global)
2788                 ;; Copy the global value of the variable.
2789                 (setq global (symbol-value (car local)))
2790               ;; Use the value from the list.
2791               (setq global (eval (cdr local))))
2792             (set (make-local-variable (car local)) global))
2793         ;; Simple nil-valued local variable.
2794         (set (make-local-variable local) nil)))))
2795
2796 (defun gnus-summary-clear-local-variables ()
2797   (let ((locals gnus-summary-local-variables))
2798     (while locals
2799       (if (consp (car locals))
2800           (and (vectorp (caar locals))
2801                (set (caar locals) nil))
2802         (and (vectorp (car locals))
2803              (set (car locals) nil)))
2804       (setq locals (cdr locals)))))
2805
2806 ;; Summary data functions.
2807
2808 (defmacro gnus-data-number (data)
2809   `(car ,data))
2810
2811 (defmacro gnus-data-set-number (data number)
2812   `(setcar ,data ,number))
2813
2814 (defmacro gnus-data-mark (data)
2815   `(nth 1 ,data))
2816
2817 (defmacro gnus-data-set-mark (data mark)
2818   `(setcar (nthcdr 1 ,data) ,mark))
2819
2820 (defmacro gnus-data-pos (data)
2821   `(nth 2 ,data))
2822
2823 (defmacro gnus-data-set-pos (data pos)
2824   `(setcar (nthcdr 2 ,data) ,pos))
2825
2826 (defmacro gnus-data-header (data)
2827   `(nth 3 ,data))
2828
2829 (defmacro gnus-data-set-header (data header)
2830   `(setf (nth 3 ,data) ,header))
2831
2832 (defmacro gnus-data-level (data)
2833   `(nth 4 ,data))
2834
2835 (defmacro gnus-data-unread-p (data)
2836   `(= (nth 1 ,data) gnus-unread-mark))
2837
2838 (defmacro gnus-data-read-p (data)
2839   `(/= (nth 1 ,data) gnus-unread-mark))
2840
2841 (defmacro gnus-data-pseudo-p (data)
2842   `(consp (nth 3 ,data)))
2843
2844 (defmacro gnus-data-find (number)
2845   `(assq ,number gnus-newsgroup-data))
2846
2847 (defmacro gnus-data-find-list (number &optional data)
2848   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2849      (memq (assq ,number bdata)
2850            bdata)))
2851
2852 (defmacro gnus-data-make (number mark pos header level)
2853   `(list ,number ,mark ,pos ,header ,level))
2854
2855 (defun gnus-data-enter (after-article number mark pos header level offset)
2856   (let ((data (gnus-data-find-list after-article)))
2857     (unless data
2858       (error "No such article: %d" after-article))
2859     (setcdr data (cons (gnus-data-make number mark pos header level)
2860                        (cdr data)))
2861     (setq gnus-newsgroup-data-reverse nil)
2862     (gnus-data-update-list (cddr data) offset)))
2863
2864 (defun gnus-data-enter-list (after-article list &optional offset)
2865   (when list
2866     (let ((data (and after-article (gnus-data-find-list after-article)))
2867           (ilist list))
2868       (if (not (or data
2869                    after-article))
2870           (let ((odata gnus-newsgroup-data))
2871             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2872             (when offset
2873               (gnus-data-update-list odata offset)))
2874       ;; Find the last element in the list to be spliced into the main
2875         ;; list.
2876         (while (cdr list)
2877           (setq list (cdr list)))
2878         (if (not data)
2879             (progn
2880               (setcdr list gnus-newsgroup-data)
2881               (setq gnus-newsgroup-data ilist)
2882               (when offset
2883                 (gnus-data-update-list (cdr list) offset)))
2884           (setcdr list (cdr data))
2885           (setcdr data ilist)
2886           (when offset
2887             (gnus-data-update-list (cdr list) offset))))
2888       (setq gnus-newsgroup-data-reverse nil))))
2889
2890 (defun gnus-data-remove (article &optional offset)
2891   (let ((data gnus-newsgroup-data))
2892     (if (= (gnus-data-number (car data)) article)
2893         (progn
2894           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2895                 gnus-newsgroup-data-reverse nil)
2896           (when offset
2897             (gnus-data-update-list gnus-newsgroup-data offset)))
2898       (while (cdr data)
2899         (when (= (gnus-data-number (cadr data)) article)
2900           (setcdr data (cddr data))
2901           (when offset
2902             (gnus-data-update-list (cdr data) offset))
2903           (setq data nil
2904                 gnus-newsgroup-data-reverse nil))
2905         (setq data (cdr data))))))
2906
2907 (defmacro gnus-data-list (backward)
2908   `(if ,backward
2909        (or gnus-newsgroup-data-reverse
2910            (setq gnus-newsgroup-data-reverse
2911                  (reverse gnus-newsgroup-data)))
2912      gnus-newsgroup-data))
2913
2914 (defun gnus-data-update-list (data offset)
2915   "Add OFFSET to the POS of all data entries in DATA."
2916   (setq gnus-newsgroup-data-reverse nil)
2917   (while data
2918     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2919     (setq data (cdr data))))
2920
2921 (defun gnus-summary-article-pseudo-p (article)
2922   "Say whether this article is a pseudo article or not."
2923   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2924
2925 (defmacro gnus-summary-article-sparse-p (article)
2926   "Say whether this article is a sparse article or not."
2927   `(memq ,article gnus-newsgroup-sparse))
2928
2929 (defmacro gnus-summary-article-ancient-p (article)
2930   "Say whether this article is a sparse article or not."
2931   `(memq ,article gnus-newsgroup-ancient))
2932
2933 (defun gnus-article-parent-p (number)
2934   "Say whether this article is a parent or not."
2935   (let ((data (gnus-data-find-list number)))
2936     (and (cdr data)              ; There has to be an article after...
2937          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2938             (gnus-data-level (nth 1 data))))))
2939
2940 (defun gnus-article-children (number)
2941   "Return a list of all children to NUMBER."
2942   (let* ((data (gnus-data-find-list number))
2943          (level (gnus-data-level (car data)))
2944          children)
2945     (setq data (cdr data))
2946     (while (and data
2947                 (= (gnus-data-level (car data)) (1+ level)))
2948       (push (gnus-data-number (car data)) children)
2949       (setq data (cdr data)))
2950     children))
2951
2952 (defmacro gnus-summary-skip-intangible ()
2953   "If the current article is intangible, then jump to a different article."
2954   '(let ((to (get-text-property (point) 'gnus-intangible)))
2955      (and to (gnus-summary-goto-subject to))))
2956
2957 (defmacro gnus-summary-article-intangible-p ()
2958   "Say whether this article is intangible or not."
2959   '(get-text-property (point) 'gnus-intangible))
2960
2961 (defun gnus-article-read-p (article)
2962   "Say whether ARTICLE is read or not."
2963   (not (or (memq article gnus-newsgroup-marked)
2964            (memq article gnus-newsgroup-spam-marked)
2965            (memq article gnus-newsgroup-unreads)
2966            (memq article gnus-newsgroup-unselected)
2967            (memq article gnus-newsgroup-dormant))))
2968
2969 ;; Some summary mode macros.
2970
2971 (defmacro gnus-summary-article-number ()
2972   "The article number of the article on the current line.
2973 If there isn't an article number here, then we return the current
2974 article number."
2975   '(progn
2976      (gnus-summary-skip-intangible)
2977      (or (get-text-property (point) 'gnus-number)
2978          (gnus-summary-last-subject))))
2979
2980 (defmacro gnus-summary-article-header (&optional number)
2981   "Return the header of article NUMBER."
2982   `(gnus-data-header (gnus-data-find
2983                       ,(or number '(gnus-summary-article-number)))))
2984
2985 (defmacro gnus-summary-thread-level (&optional number)
2986   "Return the level of thread that starts with article NUMBER."
2987   `(if (and (eq gnus-summary-make-false-root 'dummy)
2988             (get-text-property (point) 'gnus-intangible))
2989        0
2990      (gnus-data-level (gnus-data-find
2991                        ,(or number '(gnus-summary-article-number))))))
2992
2993 (defmacro gnus-summary-article-mark (&optional number)
2994   "Return the mark of article NUMBER."
2995   `(gnus-data-mark (gnus-data-find
2996                     ,(or number '(gnus-summary-article-number)))))
2997
2998 (defmacro gnus-summary-article-pos (&optional number)
2999   "Return the position of the line of article NUMBER."
3000   `(gnus-data-pos (gnus-data-find
3001                    ,(or number '(gnus-summary-article-number)))))
3002
3003 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3004 (defmacro gnus-summary-article-subject (&optional number)
3005   "Return current subject string or nil if nothing."
3006   `(let ((headers
3007           ,(if number
3008                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3009              '(gnus-data-header (assq (gnus-summary-article-number)
3010                                       gnus-newsgroup-data)))))
3011      (and headers
3012           (vectorp headers)
3013           (mail-header-subject headers))))
3014
3015 (defmacro gnus-summary-article-score (&optional number)
3016   "Return current article score."
3017   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3018                   gnus-newsgroup-scored))
3019        gnus-summary-default-score 0))
3020
3021 (defun gnus-summary-article-children (&optional number)
3022   "Return a list of article numbers that are children of article NUMBER."
3023   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3024          (level (gnus-data-level (car data)))
3025          l children)
3026     (while (and (setq data (cdr data))
3027                 (> (setq l (gnus-data-level (car data))) level))
3028       (and (= (1+ level) l)
3029            (push (gnus-data-number (car data))
3030                  children)))
3031     (nreverse children)))
3032
3033 (defun gnus-summary-article-parent (&optional number)
3034   "Return the article number of the parent of article NUMBER."
3035   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3036                                     (gnus-data-list t)))
3037          (level (gnus-data-level (car data))))
3038     (if (zerop level)
3039         ()                              ; This is a root.
3040       ;; We search until we find an article with a level less than
3041       ;; this one.  That function has to be the parent.
3042       (while (and (setq data (cdr data))
3043                   (not (< (gnus-data-level (car data)) level))))
3044       (and data (gnus-data-number (car data))))))
3045
3046 (defun gnus-unread-mark-p (mark)
3047   "Say whether MARK is the unread mark."
3048   (= mark gnus-unread-mark))
3049
3050 (defun gnus-read-mark-p (mark)
3051   "Say whether MARK is one of the marks that mark as read.
3052 This is all marks except unread, ticked, dormant, and expirable."
3053   (not (or (= mark gnus-unread-mark)
3054            (= mark gnus-ticked-mark)
3055            (= mark gnus-spam-mark)
3056            (= mark gnus-dormant-mark)
3057            (= mark gnus-expirable-mark))))
3058
3059 (defmacro gnus-article-mark (number)
3060   "Return the MARK of article NUMBER.
3061 This macro should only be used when computing the mark the \"first\"
3062 time; i.e., when generating the summary lines.  After that,
3063 `gnus-summary-article-mark' should be used to examine the
3064 marks of articles."
3065   `(cond
3066     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3067     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3068     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3069     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3070     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3071     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3072     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3073     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3074            gnus-ancient-mark))))
3075
3076 ;; Saving hidden threads.
3077
3078 (defmacro gnus-save-hidden-threads (&rest forms)
3079   "Save hidden threads, eval FORMS, and restore the hidden threads."
3080   (let ((config (make-symbol "config")))
3081     `(let ((,config (gnus-hidden-threads-configuration)))
3082        (unwind-protect
3083            (save-excursion
3084              ,@forms)
3085          (gnus-restore-hidden-threads-configuration ,config)))))
3086 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3087 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3088
3089 (defun gnus-data-compute-positions ()
3090   "Compute the positions of all articles."
3091   (setq gnus-newsgroup-data-reverse nil)
3092   (let ((data gnus-newsgroup-data))
3093     (save-excursion
3094       (gnus-save-hidden-threads
3095         (gnus-summary-show-all-threads)
3096         (goto-char (point-min))
3097         (while data
3098           (while (get-text-property (point) 'gnus-intangible)
3099             (forward-line 1))
3100           (gnus-data-set-pos (car data) (+ (point) 3))
3101           (setq data (cdr data))
3102           (forward-line 1))))))
3103
3104 (defun gnus-hidden-threads-configuration ()
3105   "Return the current hidden threads configuration."
3106   (save-excursion
3107     (let (config)
3108       (goto-char (point-min))
3109       (while (search-forward "\r" nil t)
3110         (push (1- (point)) config))
3111       config)))
3112
3113 (defun gnus-restore-hidden-threads-configuration (config)
3114   "Restore hidden threads configuration from CONFIG."
3115   (save-excursion
3116     (let (point buffer-read-only)
3117       (while (setq point (pop config))
3118         (when (and (< point (point-max))
3119                    (goto-char point)
3120                    (eq (char-after) ?\n))
3121           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3122
3123 ;; Various summary mode internalish functions.
3124
3125 (defun gnus-mouse-pick-article (e)
3126   (interactive "e")
3127   (mouse-set-point e)
3128   (gnus-summary-next-page nil t))
3129
3130 (defun gnus-summary-set-display-table ()
3131   "Change the display table.
3132 Odd characters have a tendency to mess
3133 up nicely formatted displays - we make all possible glyphs
3134 display only a single character."
3135
3136   ;; We start from the standard display table, if any.
3137   (let ((table (or (copy-sequence standard-display-table)
3138                    (make-display-table)))
3139         (i 32))
3140     ;; Nix out all the control chars...
3141     (while (>= (setq i (1- i)) 0)
3142       (aset table i [??]))
3143    ;; ... but not newline and cr, of course.  (cr is necessary for the
3144     ;; selective display).
3145     (aset table ?\n nil)
3146     (aset table ?\r nil)
3147     ;; We keep TAB as well.
3148     (aset table ?\t nil)
3149     ;; We nix out any glyphs over 126 that are not set already.
3150     (let ((i 256))
3151       (while (>= (setq i (1- i)) 127)
3152         ;; Only modify if the entry is nil.
3153         (unless (aref table i)
3154           (aset table i [??]))))
3155     (setq buffer-display-table table)))
3156
3157 (defun gnus-summary-set-article-display-arrow (pos)
3158   "Update the overlay arrow to point to line at position POS."
3159   (when (and gnus-summary-display-arrow
3160              (boundp 'overlay-arrow-position)
3161              (boundp 'overlay-arrow-string))
3162     (save-excursion
3163       (goto-char pos)
3164       (beginning-of-line)
3165       (unless overlay-arrow-position
3166         (setq overlay-arrow-position (make-marker)))
3167       (setq overlay-arrow-string "=>"
3168             overlay-arrow-position (set-marker overlay-arrow-position
3169                                                (point)
3170                                                (current-buffer))))))
3171
3172 (defun gnus-summary-setup-buffer (group)
3173   "Initialize summary buffer."
3174   (let ((buffer (gnus-summary-buffer-name group))
3175         (dead-name (concat "*Dead Summary "
3176                            (gnus-group-decoded-name group) "*")))
3177     ;; If a dead summary buffer exists, we kill it.
3178     (when (gnus-buffer-live-p dead-name)
3179       (gnus-kill-buffer dead-name))
3180     (if (get-buffer buffer)
3181         (progn
3182           (set-buffer buffer)
3183           (setq gnus-summary-buffer (current-buffer))
3184           (not gnus-newsgroup-prepared))
3185       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3186       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3187       (gnus-summary-mode group)
3188       (when gnus-carpal
3189         (gnus-carpal-setup-buffer 'summary))
3190       (unless gnus-single-article-buffer
3191         (make-local-variable 'gnus-article-buffer)
3192         (make-local-variable 'gnus-article-current)
3193         (make-local-variable 'gnus-original-article-buffer))
3194       (setq gnus-newsgroup-name group)
3195       ;; Set any local variables in the group parameters.
3196       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3197       t)))
3198
3199 (defun gnus-set-global-variables ()
3200   "Set the global equivalents of the buffer-local variables.
3201 They are set to the latest values they had.  These reflect the summary
3202 buffer that was in action when the last article was fetched."
3203   (when (eq major-mode 'gnus-summary-mode)
3204     (setq gnus-summary-buffer (current-buffer))
3205     (let ((name gnus-newsgroup-name)
3206           (marked gnus-newsgroup-marked)
3207           (spam gnus-newsgroup-spam-marked)
3208           (unread gnus-newsgroup-unreads)
3209           (headers gnus-current-headers)
3210           (data gnus-newsgroup-data)
3211           (summary gnus-summary-buffer)
3212           (article-buffer gnus-article-buffer)
3213           (original gnus-original-article-buffer)
3214           (gac gnus-article-current)
3215           (reffed gnus-reffed-article-number)
3216           (score-file gnus-current-score-file)
3217           (default-charset gnus-newsgroup-charset)
3218           vlist)
3219       (let ((locals gnus-newsgroup-variables))
3220         (while locals
3221           (if (consp (car locals))
3222               (push (eval (caar locals)) vlist)
3223             (push (eval (car locals)) vlist))
3224           (setq locals (cdr locals)))
3225         (setq vlist (nreverse vlist)))
3226       (with-current-buffer gnus-group-buffer
3227         (setq gnus-newsgroup-name name
3228               gnus-newsgroup-marked marked
3229               gnus-newsgroup-spam-marked spam
3230               gnus-newsgroup-unreads unread
3231               gnus-current-headers headers
3232               gnus-newsgroup-data data
3233               gnus-article-current gac
3234               gnus-summary-buffer summary
3235               gnus-article-buffer article-buffer
3236               gnus-original-article-buffer original
3237               gnus-reffed-article-number reffed
3238               gnus-current-score-file score-file
3239               gnus-newsgroup-charset default-charset)
3240         (let ((locals gnus-newsgroup-variables))
3241           (while locals
3242             (if (consp (car locals))
3243                 (set (caar locals) (pop vlist))
3244               (set (car locals) (pop vlist)))
3245             (setq locals (cdr locals))))
3246         ;; The article buffer also has local variables.
3247         (when (gnus-buffer-live-p gnus-article-buffer)
3248           (set-buffer gnus-article-buffer)
3249           (setq gnus-summary-buffer summary))))))
3250
3251 (defun gnus-summary-article-unread-p (article)
3252   "Say whether ARTICLE is unread or not."
3253   (memq article gnus-newsgroup-unreads))
3254
3255 (defun gnus-summary-first-article-p (&optional article)
3256   "Return whether ARTICLE is the first article in the buffer."
3257   (if (not (setq article (or article (gnus-summary-article-number))))
3258       nil
3259     (eq article (caar gnus-newsgroup-data))))
3260
3261 (defun gnus-summary-last-article-p (&optional article)
3262   "Return whether ARTICLE is the last article in the buffer."
3263   (if (not (setq article (or article (gnus-summary-article-number))))
3264       ;; All non-existent numbers are the last article.  :-)
3265       t
3266     (not (cdr (gnus-data-find-list article)))))
3267
3268 (defun gnus-make-thread-indent-array ()
3269   (let ((n 200))
3270     (unless (and gnus-thread-indent-array
3271                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3272       (setq gnus-thread-indent-array (make-vector 201 "")
3273             gnus-thread-indent-array-level gnus-thread-indent-level)
3274       (while (>= n 0)
3275         (aset gnus-thread-indent-array n
3276               (make-string (* n gnus-thread-indent-level) ? ))
3277         (setq n (1- n))))))
3278
3279 (defun gnus-update-summary-mark-positions ()
3280   "Compute where the summary marks are to go."
3281   (save-excursion
3282     (when (gnus-buffer-exists-p gnus-summary-buffer)
3283       (set-buffer gnus-summary-buffer))
3284     (let ((gnus-replied-mark 129)
3285           (gnus-score-below-mark 130)
3286           (gnus-score-over-mark 130)
3287           (gnus-undownloaded-mark 131)
3288           (spec gnus-summary-line-format-spec)
3289           gnus-visual pos)
3290       (save-excursion
3291         (gnus-set-work-buffer)
3292         (let ((gnus-summary-line-format-spec spec)
3293               (gnus-newsgroup-downloadable '(0)))
3294           (gnus-summary-insert-line
3295            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3296            0 nil t 128 t nil "" nil 1)
3297           (goto-char (point-min))
3298           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3299                                              (- (point) (point-min) 1)))))
3300           (goto-char (point-min))
3301           (push (cons 'replied (and (search-forward "\201" nil t)
3302                                     (- (point) (point-min) 1)))
3303                 pos)
3304           (goto-char (point-min))
3305           (push (cons 'score (and (search-forward "\202" nil t)
3306                                   (- (point) (point-min) 1)))
3307                 pos)
3308           (goto-char (point-min))
3309           (push (cons 'download
3310                       (and (search-forward "\203" nil t)
3311                            (- (point) (point-min) 1)))
3312                 pos)))
3313       (setq gnus-summary-mark-positions pos))))
3314
3315 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3316   "Insert a dummy root in the summary buffer."
3317   (beginning-of-line)
3318   (gnus-add-text-properties
3319    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3320    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3321
3322 (defun gnus-summary-extract-address-component (from)
3323   (or (car (funcall gnus-extract-address-components from))
3324       from))
3325
3326 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3327   (let ((mail-parse-charset gnus-newsgroup-charset)
3328         ; Is it really necessary to do this next part for each summary line?
3329         ; Luckily, doesn't seem to slow things down much.
3330         (mail-parse-ignored-charsets
3331          (with-current-buffer gnus-summary-buffer
3332            gnus-newsgroup-ignored-charsets)))
3333     (or
3334      (and gnus-ignored-from-addresses
3335           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3336           (let ((extra-headers (mail-header-extra header))
3337                 to
3338                 newsgroups)
3339             (cond
3340              ((setq to (cdr (assq 'To extra-headers)))
3341               (concat "-> "
3342                       (inline
3343                         (gnus-summary-extract-address-component
3344                          (funcall gnus-decode-encoded-word-function to)))))
3345              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3346               (concat "=> " newsgroups)))))
3347      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3348
3349 (defun gnus-summary-insert-line (gnus-tmp-header
3350                                  gnus-tmp-level gnus-tmp-current
3351                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3352                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3353                                  &optional gnus-tmp-dummy gnus-tmp-score
3354                                  gnus-tmp-process)
3355   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3356          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3357          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3358          (gnus-tmp-score-char
3359           (if (or (null gnus-summary-default-score)
3360                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3361                       gnus-summary-zcore-fuzz))
3362               ?                         ;Whitespace
3363             (if (< gnus-tmp-score gnus-summary-default-score)
3364                 gnus-score-below-mark gnus-score-over-mark)))
3365          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3366          (gnus-tmp-replied
3367           (cond (gnus-tmp-process gnus-process-mark)
3368                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3369                  gnus-cached-mark)
3370                 (gnus-tmp-replied gnus-replied-mark)
3371                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3372                  gnus-forwarded-mark)
3373                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3374                  gnus-saved-mark)
3375                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3376                  gnus-recent-mark)
3377                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3378                  gnus-unseen-mark)
3379                 (t gnus-no-mark)))
3380          (gnus-tmp-downloaded
3381           (cond (undownloaded
3382                  gnus-undownloaded-mark)
3383                 (gnus-newsgroup-agentized
3384                  gnus-downloaded-mark)
3385                 (t
3386                  gnus-no-mark)))
3387          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3388          (gnus-tmp-name
3389           (cond
3390            ((string-match "<[^>]+> *$" gnus-tmp-from)
3391             (let ((beg (match-beginning 0)))
3392               (or (and (string-match "^\".+\"" gnus-tmp-from)
3393                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3394                   (substring gnus-tmp-from 0 beg))))
3395            ((string-match "(.+)" gnus-tmp-from)
3396             (substring gnus-tmp-from
3397                        (1+ (match-beginning 0)) (1- (match-end 0))))
3398            (t gnus-tmp-from)))
3399          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3400          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3401          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3402          (buffer-read-only nil))
3403     (when (string= gnus-tmp-name "")
3404       (setq gnus-tmp-name gnus-tmp-from))
3405     (unless (numberp gnus-tmp-lines)
3406       (setq gnus-tmp-lines -1))
3407     (if (= gnus-tmp-lines -1)
3408         (setq gnus-tmp-lines "?")
3409       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3410       (gnus-put-text-property
3411      (point)
3412      (progn (eval gnus-summary-line-format-spec) (point))
3413        'gnus-number gnus-tmp-number)
3414     (when (gnus-visual-p 'summary-highlight 'highlight)
3415       (forward-line -1)
3416       (gnus-run-hooks 'gnus-summary-update-hook)
3417       (forward-line 1))))
3418
3419 (defun gnus-summary-update-line (&optional dont-update)
3420   "Update summary line after change."
3421   (when (and gnus-summary-default-score
3422              (not gnus-summary-inhibit-highlight))
3423     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3424            (article (gnus-summary-article-number))
3425            (score (gnus-summary-article-score article)))
3426       (unless dont-update
3427         (if (and gnus-summary-mark-below
3428                  (< (gnus-summary-article-score)
3429                     gnus-summary-mark-below))
3430             ;; This article has a low score, so we mark it as read.
3431             (when (memq article gnus-newsgroup-unreads)
3432               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3433           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3434             ;; This article was previously marked as read on account
3435             ;; of a low score, but now it has risen, so we mark it as
3436             ;; unread.
3437             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3438         (gnus-summary-update-mark
3439          (if (or (null gnus-summary-default-score)
3440                  (<= (abs (- score gnus-summary-default-score))
3441                      gnus-summary-zcore-fuzz))
3442              ?                          ;Whitespace
3443            (if (< score gnus-summary-default-score)
3444                gnus-score-below-mark gnus-score-over-mark))
3445          'score))
3446       ;; Do visual highlighting.
3447       (when (gnus-visual-p 'summary-highlight 'highlight)
3448         (gnus-run-hooks 'gnus-summary-update-hook)))))
3449
3450 (defvar gnus-tmp-new-adopts nil)
3451
3452 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3453   "Return the number of articles in THREAD.
3454 This may be 0 in some cases -- if none of the articles in
3455 the thread are to be displayed."
3456   (let* ((number
3457          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3458           (cond
3459            ((not (listp thread))
3460             1)
3461            ((and (consp thread) (cdr thread))
3462             (apply
3463              '+ 1 (mapcar
3464                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3465            ((null thread)
3466             1)
3467            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3468             1)
3469            (t 0))))
3470     (when (and level (zerop level) gnus-tmp-new-adopts)
3471       (incf number
3472             (apply '+ (mapcar
3473                        'gnus-summary-number-of-articles-in-thread
3474                        gnus-tmp-new-adopts))))
3475     (if char
3476         (if (> number 1) gnus-not-empty-thread-mark
3477           gnus-empty-thread-mark)
3478       number)))
3479
3480 (defsubst gnus-summary-line-message-size (head)
3481   "Return pretty-printed version of message size.
3482 This function is intended to be used in
3483 `gnus-summary-line-format-alist'."
3484   (let ((c (or (mail-header-chars head) -1)))
3485     (cond ((< c 0) "n/a")               ; chars not available
3486           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3487           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3488           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3489           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3490
3491
3492 (defun gnus-summary-set-local-parameters (group)
3493   "Go through the local params of GROUP and set all variable specs in that list."
3494   (let ((params (gnus-group-find-parameter group))
3495         (vars '(quit-config))           ; Ignore quit-config.
3496         elem)
3497     (while params
3498       (setq elem (car params)
3499             params (cdr params))
3500       (and (consp elem)                 ; Has to be a cons.
3501            (consp (cdr elem))           ; The cdr has to be a list.
3502            (symbolp (car elem))         ; Has to be a symbol in there.
3503            (not (memq (car elem) vars))
3504            (ignore-errors               ; So we set it.
3505              (push (car elem) vars)
3506              (make-local-variable (car elem))
3507              (set (car elem) (eval (nth 1 elem))))))))
3508
3509 (defun gnus-summary-read-group (group &optional show-all no-article
3510                                       kill-buffer no-display backward
3511                                       select-articles)
3512   "Start reading news in newsgroup GROUP.
3513 If SHOW-ALL is non-nil, already read articles are also listed.
3514 If NO-ARTICLE is non-nil, no article is selected initially.
3515 If NO-DISPLAY, don't generate a summary buffer."
3516   (let (result)
3517     (while (and group
3518                 (null (setq result
3519                             (let ((gnus-auto-select-next nil))
3520                               (or (gnus-summary-read-group-1
3521                                    group show-all no-article
3522                                    kill-buffer no-display
3523                                    select-articles)
3524                                   (setq show-all nil
3525                                         select-articles nil)))))
3526                 (eq gnus-auto-select-next 'quietly))
3527       (set-buffer gnus-group-buffer)
3528       ;; The entry function called above goes to the next
3529       ;; group automatically, so we go two groups back
3530       ;; if we are searching for the previous group.
3531       (when backward
3532         (gnus-group-prev-unread-group 2))
3533       (if (not (equal group (gnus-group-group-name)))
3534           (setq group (gnus-group-group-name))
3535         (setq group nil)))
3536     result))
3537
3538 (defun gnus-summary-read-group-1 (group show-all no-article
3539                                         kill-buffer no-display
3540                                         &optional select-articles)
3541   ;; Killed foreign groups can't be entered.
3542   ;;  (when (and (not (gnus-group-native-p group))
3543   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3544   ;;    (error "Dead non-native groups can't be entered"))
3545   (gnus-message 5 "Retrieving newsgroup: %s..."
3546                 (gnus-group-decoded-name group))
3547   (let* ((new-group (gnus-summary-setup-buffer group))
3548          (quit-config (gnus-group-quit-config group))
3549          (did-select (and new-group (gnus-select-newsgroup
3550                                      group show-all select-articles))))
3551     (cond
3552      ;; This summary buffer exists already, so we just select it.
3553      ((not new-group)
3554       (gnus-set-global-variables)
3555       (when kill-buffer
3556         (gnus-kill-or-deaden-summary kill-buffer))
3557       (gnus-configure-windows 'summary 'force)
3558       (gnus-set-mode-line 'summary)
3559       (gnus-summary-position-point)
3560       (message "")
3561       t)
3562      ;; We couldn't select this group.
3563      ((null did-select)
3564       (when (and (eq major-mode 'gnus-summary-mode)
3565                  (not (equal (current-buffer) kill-buffer)))
3566         (kill-buffer (current-buffer))
3567         (if (not quit-config)
3568             (progn
3569               ;; Update the info -- marks might need to be removed,
3570               ;; for instance.
3571               (gnus-summary-update-info)
3572               (set-buffer gnus-group-buffer)
3573               (gnus-group-jump-to-group group)
3574               (gnus-group-next-unread-group 1))
3575           (gnus-handle-ephemeral-exit quit-config)))
3576       (let ((grpinfo (gnus-get-info group)))
3577         (if (null (gnus-info-read grpinfo))
3578             (gnus-message 3 "Group %s contains no messages"
3579                           (gnus-group-decoded-name group))
3580           (gnus-message 3 "Can't select group")))
3581       nil)
3582      ;; The user did a `C-g' while prompting for number of articles,
3583      ;; so we exit this group.
3584      ((eq did-select 'quit)
3585       (and (eq major-mode 'gnus-summary-mode)
3586            (not (equal (current-buffer) kill-buffer))
3587            (kill-buffer (current-buffer)))
3588       (when kill-buffer
3589         (gnus-kill-or-deaden-summary kill-buffer))
3590       (if (not quit-config)
3591           (progn
3592             (set-buffer gnus-group-buffer)
3593             (gnus-group-jump-to-group group)
3594             (gnus-group-next-unread-group 1)
3595             (gnus-configure-windows 'group 'force))
3596         (gnus-handle-ephemeral-exit quit-config))
3597       ;; Finally signal the quit.
3598       (signal 'quit nil))
3599      ;; The group was successfully selected.
3600      (t
3601       (gnus-set-global-variables)
3602       ;; Save the active value in effect when the group was entered.
3603       (setq gnus-newsgroup-active
3604             (gnus-copy-sequence
3605              (gnus-active gnus-newsgroup-name)))
3606       ;; You can change the summary buffer in some way with this hook.
3607       (gnus-run-hooks 'gnus-select-group-hook)
3608       (gnus-update-format-specifications
3609        nil 'summary 'summary-mode 'summary-dummy)
3610       (gnus-update-summary-mark-positions)
3611       ;; Do score processing.
3612       (when gnus-use-scoring
3613         (gnus-possibly-score-headers))
3614       ;; Check whether to fill in the gaps in the threads.
3615       (when gnus-build-sparse-threads
3616         (gnus-build-sparse-threads))
3617       ;; Find the initial limit.
3618       (if show-all
3619           (let ((gnus-newsgroup-dormant nil))
3620             (gnus-summary-initial-limit show-all))
3621         (gnus-summary-initial-limit show-all))
3622       ;; Generate the summary buffer.
3623       (unless no-display
3624         (gnus-summary-prepare))
3625       (when gnus-use-trees
3626         (gnus-tree-open group)
3627         (setq gnus-summary-highlight-line-function
3628               'gnus-tree-highlight-article))
3629       ;; If the summary buffer is empty, but there are some low-scored
3630       ;; articles or some excluded dormants, we include these in the
3631       ;; buffer.
3632       (when (and (zerop (buffer-size))
3633                  (not no-display))
3634         (cond (gnus-newsgroup-dormant
3635                (gnus-summary-limit-include-dormant))
3636               ((and gnus-newsgroup-scored show-all)
3637                (gnus-summary-limit-include-expunged t))))
3638       ;; Function `gnus-apply-kill-file' must be called in this hook.
3639       (gnus-run-hooks 'gnus-apply-kill-hook)
3640       (if (and (zerop (buffer-size))
3641                (not no-display))
3642           (progn
3643             ;; This newsgroup is empty.
3644             (gnus-summary-catchup-and-exit nil t)
3645             (gnus-message 6 "No unread news")
3646             (when kill-buffer
3647               (gnus-kill-or-deaden-summary kill-buffer))
3648             ;; Return nil from this function.
3649             nil)
3650         ;; Hide conversation thread subtrees.  We cannot do this in
3651         ;; gnus-summary-prepare-hook since kill processing may not
3652         ;; work with hidden articles.
3653         (gnus-summary-maybe-hide-threads)
3654         (when kill-buffer
3655           (gnus-kill-or-deaden-summary kill-buffer))
3656         (gnus-summary-auto-select-subject)
3657         ;; Show first unread article if requested.
3658         (if (and (not no-article)
3659                  (not no-display)
3660                  gnus-newsgroup-unreads
3661                  gnus-auto-select-first)
3662             (progn
3663               (gnus-configure-windows 'summary)
3664               (let ((art (gnus-summary-article-number)))
3665                 (unless (and (not gnus-plugged)
3666                              (or (memq art gnus-newsgroup-undownloaded)
3667                                  (memq art gnus-newsgroup-downloadable)))
3668                   (gnus-summary-goto-article art))))
3669           ;; Don't select any articles.
3670           (gnus-summary-position-point)
3671           (gnus-configure-windows 'summary 'force)
3672           (gnus-set-mode-line 'summary))
3673         (when (and gnus-auto-center-group
3674                    (get-buffer-window gnus-group-buffer t))
3675           ;; Gotta use windows, because recenter does weird stuff if
3676           ;; the current buffer ain't the displayed window.
3677           (let ((owin (selected-window)))
3678             (select-window (get-buffer-window gnus-group-buffer t))
3679             (when (gnus-group-goto-group group)
3680               (recenter))
3681             (select-window owin)))
3682         ;; Mark this buffer as "prepared".
3683         (setq gnus-newsgroup-prepared t)
3684         (gnus-run-hooks 'gnus-summary-prepared-hook)
3685         (unless (gnus-ephemeral-group-p group)
3686           (gnus-group-update-group group))
3687         t)))))
3688
3689 (defun gnus-summary-auto-select-subject ()
3690   "Select the subject line on initial group entry."
3691   (goto-char (point-min))
3692   (cond
3693    ((eq gnus-auto-select-subject 'best)
3694     (gnus-summary-best-unread-subject))
3695    ((eq gnus-auto-select-subject 'unread)
3696     (gnus-summary-first-unread-subject))
3697    ((eq gnus-auto-select-subject 'unseen)
3698     (gnus-summary-first-unseen-subject))
3699    ((eq gnus-auto-select-subject 'unseen-or-unread)
3700     (gnus-summary-first-unseen-or-unread-subject))
3701    ((eq gnus-auto-select-subject 'first)
3702     ;; Do nothing.
3703     )
3704    ((functionp gnus-auto-select-subject)
3705     (funcall gnus-auto-select-subject))))
3706
3707 (defun gnus-summary-prepare ()
3708   "Generate the summary buffer."
3709   (interactive)
3710   (let ((buffer-read-only nil))
3711     (erase-buffer)
3712     (setq gnus-newsgroup-data nil
3713           gnus-newsgroup-data-reverse nil)
3714     (gnus-run-hooks 'gnus-summary-generate-hook)
3715     ;; Generate the buffer, either with threads or without.
3716     (when gnus-newsgroup-headers
3717       (gnus-summary-prepare-threads
3718        (if gnus-show-threads
3719            (gnus-sort-gathered-threads
3720             (funcall gnus-summary-thread-gathering-function
3721                      (gnus-sort-threads
3722                       (gnus-cut-threads (gnus-make-threads)))))
3723          ;; Unthreaded display.
3724          (gnus-sort-articles gnus-newsgroup-headers))))
3725     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3726     ;; Call hooks for modifying summary buffer.
3727     (goto-char (point-min))
3728     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3729
3730 (defsubst gnus-general-simplify-subject (subject)
3731   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3732   (setq subject
3733         (cond
3734          ;; Truncate the subject.
3735          (gnus-simplify-subject-functions
3736           (gnus-map-function gnus-simplify-subject-functions subject))
3737          ((numberp gnus-summary-gather-subject-limit)
3738           (setq subject (gnus-simplify-subject-re subject))
3739           (if (> (length subject) gnus-summary-gather-subject-limit)
3740               (substring subject 0 gnus-summary-gather-subject-limit)
3741             subject))
3742          ;; Fuzzily simplify it.
3743          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3744           (gnus-simplify-subject-fuzzy subject))
3745          ;; Just remove the leading "Re:".
3746          (t
3747           (gnus-simplify-subject-re subject))))
3748
3749   (if (and gnus-summary-gather-exclude-subject
3750            (string-match gnus-summary-gather-exclude-subject subject))
3751       nil                         ; This article shouldn't be gathered
3752     subject))
3753
3754 (defun gnus-summary-simplify-subject-query ()
3755   "Query where the respool algorithm would put this article."
3756   (interactive)
3757   (gnus-summary-select-article)
3758   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3759
3760 (defun gnus-gather-threads-by-subject (threads)
3761   "Gather threads by looking at Subject headers."
3762   (if (not gnus-summary-make-false-root)
3763       threads
3764     (let ((hashtb (gnus-make-hashtable 1024))
3765           (prev threads)
3766           (result threads)
3767           subject hthread whole-subject)
3768       (while threads
3769         (setq subject (gnus-general-simplify-subject
3770                        (setq whole-subject (mail-header-subject
3771                                             (caar threads)))))
3772         (when subject
3773           (if (setq hthread (gnus-gethash subject hashtb))
3774               (progn
3775                 ;; We enter a dummy root into the thread, if we
3776                 ;; haven't done that already.
3777                 (unless (stringp (caar hthread))
3778                   (setcar hthread (list whole-subject (car hthread))))
3779                 ;; We add this new gathered thread to this gathered
3780                 ;; thread.
3781                 (setcdr (car hthread)
3782                         (nconc (cdar hthread) (list (car threads))))
3783                 ;; Remove it from the list of threads.
3784                 (setcdr prev (cdr threads))
3785                 (setq threads prev))
3786             ;; Enter this thread into the hash table.
3787             (gnus-sethash subject
3788                           (if gnus-summary-make-false-root-always
3789                               (progn
3790                                 ;; If you want a dummy root above all
3791                                 ;; threads...
3792                                 (setcar threads (list whole-subject
3793                                                       (car threads)))
3794                                 threads)
3795                             threads)
3796                           hashtb)))
3797         (setq prev threads)
3798         (setq threads (cdr threads)))
3799       result)))
3800
3801 (defun gnus-gather-threads-by-references (threads)
3802   "Gather threads by looking at References headers."
3803   (let ((idhashtb (gnus-make-hashtable 1024))
3804         (thhashtb (gnus-make-hashtable 1024))
3805         (prev threads)
3806         (result threads)
3807         ids references id gthread gid entered ref)
3808     (while threads
3809       (when (setq references (mail-header-references (caar threads)))
3810         (setq id (mail-header-id (caar threads))
3811               ids (inline (gnus-split-references references))
3812               entered nil)
3813         (while (setq ref (pop ids))
3814           (setq ids (delete ref ids))
3815           (if (not (setq gid (gnus-gethash ref idhashtb)))
3816               (progn
3817                 (gnus-sethash ref id idhashtb)
3818                 (gnus-sethash id threads thhashtb))
3819             (setq gthread (gnus-gethash gid thhashtb))
3820             (unless entered
3821               ;; We enter a dummy root into the thread, if we
3822               ;; haven't done that already.
3823               (unless (stringp (caar gthread))
3824                 (setcar gthread (list (mail-header-subject (caar gthread))
3825                                       (car gthread))))
3826               ;; We add this new gathered thread to this gathered
3827               ;; thread.
3828               (setcdr (car gthread)
3829                       (nconc (cdar gthread) (list (car threads)))))
3830             ;; Add it into the thread hash table.
3831             (gnus-sethash id gthread thhashtb)
3832             (setq entered t)
3833             ;; Remove it from the list of threads.
3834             (setcdr prev (cdr threads))
3835             (setq threads prev))))
3836       (setq prev threads)
3837       (setq threads (cdr threads)))
3838     result))
3839
3840 (defun gnus-sort-gathered-threads (threads)
3841   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3842   (let ((result threads))
3843     (while threads
3844       (when (stringp (caar threads))
3845         (setcdr (car threads)
3846                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3847       (setq threads (cdr threads)))
3848     result))
3849
3850 (defun gnus-thread-loop-p (root thread)
3851   "Say whether ROOT is in THREAD."
3852   (let ((stack (list thread))
3853         (infloop 0)
3854         th)
3855     (while (setq thread (pop stack))
3856       (setq th (cdr thread))
3857       (while (and th
3858                   (not (eq (caar th) root)))
3859         (pop th))
3860       (if th
3861           ;; We have found a loop.
3862           (let (ref-dep)
3863             (setcdr thread (delq (car th) (cdr thread)))
3864             (if (boundp (setq ref-dep (intern "none"
3865                                               gnus-newsgroup-dependencies)))
3866                 (setcdr (symbol-value ref-dep)
3867                         (nconc (cdr (symbol-value ref-dep))
3868                                (list (car th))))
3869               (set ref-dep (list nil (car th))))
3870             (setq infloop 1
3871                   stack nil))
3872         ;; Push all the subthreads onto the stack.
3873         (push (cdr thread) stack)))
3874     infloop))
3875
3876 (defun gnus-make-threads ()
3877   "Go through the dependency hashtb and find the roots.  Return all threads."
3878   (let (threads)
3879     (while (catch 'infloop
3880              (mapatoms
3881               (lambda (refs)
3882                 ;; Deal with self-referencing References loops.
3883                 (when (and (car (symbol-value refs))
3884                            (not (zerop
3885                                  (apply
3886                                   '+
3887                                   (mapcar
3888                                    (lambda (thread)
3889                                      (gnus-thread-loop-p
3890                                       (car (symbol-value refs)) thread))
3891                                    (cdr (symbol-value refs)))))))
3892                   (setq threads nil)
3893                   (throw 'infloop t))
3894                 (unless (car (symbol-value refs))
3895                   ;; These threads do not refer back to any other
3896                   ;; articles, so they're roots.
3897                   (setq threads (append (cdr (symbol-value refs)) threads))))
3898               gnus-newsgroup-dependencies)))
3899     threads))
3900
3901 ;; Build the thread tree.
3902 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3903   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3904
3905 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3906 if it was already present.
3907
3908 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3909 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3910 Message-IDs will be renamed to a unique Message-ID before being
3911 entered.
3912
3913 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3914   (let* ((id (mail-header-id header))
3915          (id-dep (and id (intern id dependencies)))
3916          parent-id ref ref-dep ref-header replaced)
3917     ;; Enter this `header' in the `dependencies' table.
3918     (cond
3919      ((not id-dep)
3920       (setq header nil))
3921      ;; The first two cases do the normal part: enter a new `header'
3922      ;; in the `dependencies' table.
3923      ((not (boundp id-dep))
3924       (set id-dep (list header)))
3925      ((null (car (symbol-value id-dep)))
3926       (setcar (symbol-value id-dep) header))
3927
3928      ;; From here the `header' was already present in the
3929      ;; `dependencies' table.
3930      (force-new
3931       ;; Overrides an existing entry;
3932       ;; just set the header part of the entry.
3933       (setcar (symbol-value id-dep) header)
3934       (setq replaced t))
3935
3936      ;; Renames the existing `header' to a unique Message-ID.
3937      ((not gnus-summary-ignore-duplicates)
3938       ;; An article with this Message-ID has already been seen.
3939       ;; We rename the Message-ID.
3940       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3941            (list header))
3942       (mail-header-set-id header id))
3943
3944      ;; The last case ignores an existing entry, except it adds any
3945      ;; additional Xrefs (in case the two articles came from different
3946      ;; servers.
3947      ;; Also sets `header' to `nil' meaning that the `dependencies'
3948      ;; table was *not* modified.
3949      (t
3950       (mail-header-set-xref
3951        (car (symbol-value id-dep))
3952        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3953                    "")
3954                (or (mail-header-xref header) "")))
3955       (setq header nil)))
3956
3957     (when (and header (not replaced))
3958       ;; First check that we are not creating a References loop.
3959       (setq parent-id (gnus-parent-id (mail-header-references header)))
3960       (setq ref parent-id)
3961       (while (and ref
3962                   (setq ref-dep (intern-soft ref dependencies))
3963                   (boundp ref-dep)
3964                   (setq ref-header (car (symbol-value ref-dep))))
3965         (if (string= id ref)
3966             ;; Yuk!  This is a reference loop.  Make the article be a
3967             ;; root article.
3968             (progn
3969               (mail-header-set-references (car (symbol-value id-dep)) "none")
3970               (setq ref nil)
3971               (setq parent-id nil))
3972           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3973       (setq ref-dep (intern (or parent-id "none") dependencies))
3974       (if (boundp ref-dep)
3975           (setcdr (symbol-value ref-dep)
3976                   (nconc (cdr (symbol-value ref-dep))
3977                          (list (symbol-value id-dep))))
3978         (set ref-dep (list nil (symbol-value id-dep)))))
3979     header))
3980
3981 (defun gnus-extract-message-id-from-in-reply-to (string)
3982   (if (string-match "<[^>]+>" string)
3983       (substring string (match-beginning 0) (match-end 0))
3984     nil))
3985
3986 (defun gnus-build-sparse-threads ()
3987   (let ((headers gnus-newsgroup-headers)
3988         (mail-parse-charset gnus-newsgroup-charset)
3989         (gnus-summary-ignore-duplicates t)
3990         header references generation relations
3991         subject child end new-child date)
3992     ;; First we create an alist of generations/relations, where
3993     ;; generations is how much we trust the relation, and the relation
3994     ;; is parent/child.
3995     (gnus-message 7 "Making sparse threads...")
3996     (save-excursion
3997       (nnheader-set-temp-buffer " *gnus sparse threads*")
3998       (while (setq header (pop headers))
3999         (when (and (setq references (mail-header-references header))
4000                    (not (string= references "")))
4001           (insert references)
4002           (setq child (mail-header-id header)
4003                 subject (mail-header-subject header)
4004                 date (mail-header-date header)
4005                 generation 0)
4006           (while (search-backward ">" nil t)
4007             (setq end (1+ (point)))
4008             (when (search-backward "<" nil t)
4009               (setq new-child (buffer-substring (point) end))
4010               (push (list (incf generation)
4011                           child (setq child new-child)
4012                           subject date)
4013                     relations)))
4014           (when child
4015             (push (list (1+ generation) child nil subject) relations))
4016           (erase-buffer)))
4017       (kill-buffer (current-buffer)))
4018     ;; Sort over trustworthiness.
4019     (mapcar
4020      (lambda (relation)
4021        (when (gnus-dependencies-add-header
4022               (make-full-mail-header
4023                gnus-reffed-article-number
4024                (nth 3 relation) "" (or (nth 4 relation) "")
4025                (nth 1 relation)
4026                (or (nth 2 relation) "") 0 0 "")
4027               gnus-newsgroup-dependencies nil)
4028          (push gnus-reffed-article-number gnus-newsgroup-limit)
4029          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4030          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4031                gnus-newsgroup-reads)
4032          (decf gnus-reffed-article-number)))
4033      (sort relations 'car-less-than-car))
4034     (gnus-message 7 "Making sparse threads...done")))
4035
4036 (defun gnus-build-old-threads ()
4037   ;; Look at all the articles that refer back to old articles, and
4038   ;; fetch the headers for the articles that aren't there.  This will
4039   ;; build complete threads - if the roots haven't been expired by the
4040   ;; server, that is.
4041   (let ((mail-parse-charset gnus-newsgroup-charset)
4042         id heads)
4043     (mapatoms
4044      (lambda (refs)
4045        (when (not (car (symbol-value refs)))
4046          (setq heads (cdr (symbol-value refs)))
4047          (while heads
4048            (if (memq (mail-header-number (caar heads))
4049                      gnus-newsgroup-dormant)
4050                (setq heads (cdr heads))
4051              (setq id (symbol-name refs))
4052              (while (and (setq id (gnus-build-get-header id))
4053                          (not (car (gnus-id-to-thread id)))))
4054              (setq heads nil)))))
4055      gnus-newsgroup-dependencies)))
4056
4057 (defsubst gnus-remove-odd-characters (string)
4058   "Translate STRING into something that doesn't contain weird characters."
4059   (mm-subst-char-in-string
4060    ?\r ?\-
4061    (mm-subst-char-in-string ?\n ?\- string t) t))
4062
4063 ;; This function has to be called with point after the article number
4064 ;; on the beginning of the line.
4065 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4066   (let ((eol (point-at-eol))
4067         (buffer (current-buffer))
4068         header references in-reply-to)
4069
4070     ;; overview: [num subject from date id refs chars lines misc]
4071     (unwind-protect
4072         (let (x)
4073           (narrow-to-region (point) eol)
4074           (unless (eobp)
4075             (forward-char))
4076
4077           (setq header
4078                 (make-full-mail-header
4079                  number                 ; number
4080                  (condition-case ()     ; subject
4081                      (gnus-remove-odd-characters
4082                       (funcall gnus-decode-encoded-word-function
4083                                (setq x (nnheader-nov-field))))
4084                    (error x))
4085                  (condition-case ()     ; from
4086                      (gnus-remove-odd-characters
4087                       (funcall gnus-decode-encoded-word-function
4088                                (setq x (nnheader-nov-field))))
4089                    (error x))
4090                  (nnheader-nov-field)   ; date
4091                  (nnheader-nov-read-message-id number)  ; id
4092                  (setq references (nnheader-nov-field)) ; refs
4093                  (nnheader-nov-read-integer) ; chars
4094                  (nnheader-nov-read-integer) ; lines
4095                  (unless (eobp)
4096                    (if (looking-at "Xref: ")
4097                        (goto-char (match-end 0)))
4098                    (nnheader-nov-field)) ; Xref
4099                  (nnheader-nov-parse-extra)))) ; extra
4100
4101       (widen))
4102
4103     (when (and (string= references "")
4104                (setq in-reply-to (mail-header-extra header))
4105                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4106       (mail-header-set-references
4107        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4108
4109     (when gnus-alter-header-function
4110       (funcall gnus-alter-header-function header))
4111     (gnus-dependencies-add-header header dependencies force-new)))
4112
4113 (defun gnus-build-get-header (id)
4114   "Look through the buffer of NOV lines and find the header to ID.
4115 Enter this line into the dependencies hash table, and return
4116 the id of the parent article (if any)."
4117   (let ((deps gnus-newsgroup-dependencies)
4118         found header)
4119     (prog1
4120         (save-excursion
4121           (set-buffer nntp-server-buffer)
4122           (let ((case-fold-search nil))
4123             (goto-char (point-min))
4124             (while (and (not found)
4125                         (search-forward id nil t))
4126               (beginning-of-line)
4127               (setq found (looking-at
4128                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4129                                    (regexp-quote id))))
4130               (or found (beginning-of-line 2)))
4131             (when found
4132               (beginning-of-line)
4133               (and
4134                (setq header (gnus-nov-parse-line
4135                              (read (current-buffer)) deps))
4136                (gnus-parent-id (mail-header-references header))))))
4137       (when header
4138         (let ((number (mail-header-number header)))
4139           (push number gnus-newsgroup-limit)
4140           (push header gnus-newsgroup-headers)
4141           (if (memq number gnus-newsgroup-unselected)
4142               (progn
4143                 (setq gnus-newsgroup-unreads
4144                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4145                                                number))
4146                 (setq gnus-newsgroup-unselected
4147                       (delq number gnus-newsgroup-unselected)))
4148             (push number gnus-newsgroup-ancient)))))))
4149
4150 (defun gnus-build-all-threads ()
4151   "Read all the headers."
4152   (let ((gnus-summary-ignore-duplicates t)
4153         (mail-parse-charset gnus-newsgroup-charset)
4154         (dependencies gnus-newsgroup-dependencies)
4155         header article)
4156     (save-excursion
4157       (set-buffer nntp-server-buffer)
4158       (let ((case-fold-search nil))
4159         (goto-char (point-min))
4160         (while (not (eobp))
4161           (ignore-errors
4162             (setq article (read (current-buffer))
4163                   header (gnus-nov-parse-line article dependencies)))
4164           (when header
4165             (with-current-buffer gnus-summary-buffer
4166               (push header gnus-newsgroup-headers)
4167               (if (memq (setq article (mail-header-number header))
4168                         gnus-newsgroup-unselected)
4169                   (progn
4170                     (setq gnus-newsgroup-unreads
4171                           (gnus-add-to-sorted-list
4172                            gnus-newsgroup-unreads article))
4173                     (setq gnus-newsgroup-unselected
4174                           (delq article gnus-newsgroup-unselected)))
4175                 (push article gnus-newsgroup-ancient)))
4176             (forward-line 1)))))))
4177
4178 (defun gnus-summary-update-article-line (article header)
4179   "Update the line for ARTICLE using HEADER."
4180   (let* ((id (mail-header-id header))
4181          (thread (gnus-id-to-thread id)))
4182     (unless thread
4183       (error "Article in no thread"))
4184     ;; Update the thread.
4185     (setcar thread header)
4186     (gnus-summary-goto-subject article)
4187     (let* ((datal (gnus-data-find-list article))
4188            (data (car datal))
4189            (buffer-read-only nil)
4190            (level (gnus-summary-thread-level)))
4191       (gnus-delete-line)
4192       (let ((inserted (- (point)
4193                          (progn
4194                            (gnus-summary-insert-line
4195                             header level nil
4196                             (memq article gnus-newsgroup-undownloaded)
4197                             (gnus-article-mark article)
4198                             (memq article gnus-newsgroup-replied)
4199                             (memq article gnus-newsgroup-expirable)
4200                             ;; Only insert the Subject string when it's different
4201                             ;; from the previous Subject string.
4202                             (if (and
4203                                  gnus-show-threads
4204                                  (gnus-subject-equal
4205                                   (condition-case ()
4206                                       (mail-header-subject
4207                                        (gnus-data-header
4208                                         (cadr
4209                                          (gnus-data-find-list
4210                                           article
4211                                           (gnus-data-list t)))))
4212                                     ;; Error on the side of excessive subjects.
4213                                     (error ""))
4214                                   (mail-header-subject header)))
4215                                 ""
4216                               (mail-header-subject header))
4217                             nil (cdr (assq article gnus-newsgroup-scored))
4218                             (memq article gnus-newsgroup-processable))
4219                            (point)))))
4220         (when (cdr datal)
4221           (gnus-data-update-list
4222            (cdr datal)
4223            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4224
4225 (defun gnus-summary-update-article (article &optional iheader)
4226   "Update ARTICLE in the summary buffer."
4227   (set-buffer gnus-summary-buffer)
4228   (let* ((header (gnus-summary-article-header article))
4229          (id (mail-header-id header))
4230          (data (gnus-data-find article))
4231          (thread (gnus-id-to-thread id))
4232          (references (mail-header-references header))
4233          (parent
4234           (gnus-id-to-thread
4235            (or (gnus-parent-id
4236                 (when (and references
4237                            (not (equal "" references)))
4238                   references))
4239                "none")))
4240          (buffer-read-only nil)
4241          (old (car thread)))
4242     (when thread
4243       (unless iheader
4244         (setcar thread nil)
4245         (when parent
4246           (delq thread parent)))
4247       (if (gnus-summary-insert-subject id header)
4248           ;; Set the (possibly) new article number in the data structure.
4249           (gnus-data-set-number data (gnus-id-to-article id))
4250         (setcar thread old)
4251         nil))))
4252
4253 (defun gnus-rebuild-thread (id &optional line)
4254   "Rebuild the thread containing ID.
4255 If LINE, insert the rebuilt thread starting on line LINE."
4256   (let ((buffer-read-only nil)
4257         old-pos current thread data)
4258     (if (not gnus-show-threads)
4259         (setq thread (list (car (gnus-id-to-thread id))))
4260       ;; Get the thread this article is part of.
4261       (setq thread (gnus-remove-thread id)))
4262     (setq old-pos (point-at-bol))
4263     (setq current (save-excursion
4264                     (and (re-search-backward "[\r\n]" nil t)
4265                          (gnus-summary-article-number))))
4266     ;; If this is a gathered thread, we have to go some re-gathering.
4267     (when (stringp (car thread))
4268       (let ((subject (car thread))
4269             roots thr)
4270         (setq thread (cdr thread))
4271         (while thread
4272           (unless (memq (setq thr (gnus-id-to-thread
4273                                    (gnus-root-id
4274                                     (mail-header-id (caar thread)))))
4275                         roots)
4276             (push thr roots))
4277           (setq thread (cdr thread)))
4278         ;; We now have all (unique) roots.
4279         (if (= (length roots) 1)
4280             ;; All the loose roots are now one solid root.
4281             (setq thread (car roots))
4282           (setq thread (cons subject (gnus-sort-threads roots))))))
4283     (let (threads)
4284       ;; We then insert this thread into the summary buffer.
4285       (when line
4286         (goto-char (point-min))
4287         (forward-line (1- line)))
4288       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4289         (if gnus-show-threads
4290             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4291           (gnus-summary-prepare-unthreaded thread))
4292         (setq data (nreverse gnus-newsgroup-data))
4293         (setq threads gnus-newsgroup-threads))
4294       ;; We splice the new data into the data structure.
4295       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4296       ;;!!! then we want to insert at the beginning of the buffer.
4297       ;;!!! That happens to be true with Gnus now, but that may
4298       ;;!!! change in the future.  Perhaps.
4299       (gnus-data-enter-list
4300        (if line nil current) data (- (point) old-pos))
4301       (setq gnus-newsgroup-threads
4302             (nconc threads gnus-newsgroup-threads))
4303       (gnus-data-compute-positions))))
4304
4305 (defun gnus-number-to-header (number)
4306   "Return the header for article NUMBER."
4307   (let ((headers gnus-newsgroup-headers))
4308     (while (and headers
4309                 (not (= number (mail-header-number (car headers)))))
4310       (pop headers))
4311     (when headers
4312       (car headers))))
4313
4314 (defun gnus-parent-headers (in-headers &optional generation)
4315   "Return the headers of the GENERATIONeth parent of HEADERS."
4316   (unless generation
4317     (setq generation 1))
4318   (let ((parent t)
4319         (headers in-headers)
4320         references)
4321     (while (and parent
4322                 (not (zerop generation))
4323                 (setq references (mail-header-references headers)))
4324       (setq headers (if (and references
4325                              (setq parent (gnus-parent-id references)))
4326                         (car (gnus-id-to-thread parent))
4327                       nil))
4328       (decf generation))
4329     (and (not (eq headers in-headers))
4330          headers)))
4331
4332 (defun gnus-id-to-thread (id)
4333   "Return the (sub-)thread where ID appears."
4334   (gnus-gethash id gnus-newsgroup-dependencies))
4335
4336 (defun gnus-id-to-article (id)
4337   "Return the article number of ID."
4338   (let ((thread (gnus-id-to-thread id)))
4339     (when (and thread
4340                (car thread))
4341       (mail-header-number (car thread)))))
4342
4343 (defun gnus-id-to-header (id)
4344   "Return the article headers of ID."
4345   (car (gnus-id-to-thread id)))
4346
4347 (defun gnus-article-displayed-root-p (article)
4348   "Say whether ARTICLE is a root(ish) article."
4349   (let ((level (gnus-summary-thread-level article))
4350         (refs (mail-header-references  (gnus-summary-article-header article)))
4351         particle)
4352     (cond
4353      ((null level) nil)
4354      ((zerop level) t)
4355      ((null refs) t)
4356      ((null (gnus-parent-id refs)) t)
4357      ((and (= 1 level)
4358            (null (setq particle (gnus-id-to-article
4359                                  (gnus-parent-id refs))))
4360            (null (gnus-summary-thread-level particle)))))))
4361
4362 (defun gnus-root-id (id)
4363   "Return the id of the root of the thread where ID appears."
4364   (let (last-id prev)
4365     (while (and id (setq prev (car (gnus-id-to-thread id))))
4366       (setq last-id id
4367             id (gnus-parent-id (mail-header-references prev))))
4368     last-id))
4369
4370 (defun gnus-articles-in-thread (thread)
4371   "Return the list of articles in THREAD."
4372   (cons (mail-header-number (car thread))
4373         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4374
4375 (defun gnus-remove-thread (id &optional dont-remove)
4376   "Remove the thread that has ID in it."
4377   (let (headers thread last-id)
4378     ;; First go up in this thread until we find the root.
4379     (setq last-id (gnus-root-id id)
4380           headers (message-flatten-list (gnus-id-to-thread last-id)))
4381     ;; We have now found the real root of this thread.  It might have
4382     ;; been gathered into some loose thread, so we have to search
4383     ;; through the threads to find the thread we wanted.
4384     (let ((threads gnus-newsgroup-threads)
4385           sub)
4386       (while threads
4387         (setq sub (car threads))
4388         (if (stringp (car sub))
4389             ;; This is a gathered thread, so we look at the roots
4390             ;; below it to find whether this article is in this
4391             ;; gathered root.
4392             (progn
4393               (setq sub (cdr sub))
4394               (while sub
4395                 (when (member (caar sub) headers)
4396                   (setq thread (car threads)
4397                         threads nil
4398                         sub nil))
4399                 (setq sub (cdr sub))))
4400           ;; It's an ordinary thread, so we check it.
4401           (when (eq (car sub) (car headers))
4402             (setq thread sub
4403                   threads nil)))
4404         (setq threads (cdr threads)))
4405       ;; If this article is in no thread, then it's a root.
4406       (if thread
4407           (unless dont-remove
4408             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4409         (setq thread (gnus-id-to-thread last-id)))
4410       (when thread
4411         (prog1
4412             thread                      ; We return this thread.
4413           (unless dont-remove
4414             (if (stringp (car thread))
4415                 (progn
4416                   ;; If we use dummy roots, then we have to remove the
4417                   ;; dummy root as well.
4418                   (when (eq gnus-summary-make-false-root 'dummy)
4419                     ;; We go to the dummy root by going to
4420                     ;; the first sub-"thread", and then one line up.
4421                     (gnus-summary-goto-article
4422                      (mail-header-number (caadr thread)))
4423                     (forward-line -1)
4424                     (gnus-delete-line)
4425                     (gnus-data-compute-positions))
4426                   (setq thread (cdr thread))
4427                   (while thread
4428                     (gnus-remove-thread-1 (car thread))
4429                     (setq thread (cdr thread))))
4430               (gnus-remove-thread-1 thread))))))))
4431
4432 (defun gnus-remove-thread-1 (thread)
4433   "Remove the thread THREAD recursively."
4434   (let ((number (mail-header-number (pop thread)))
4435         d)
4436     (setq thread (reverse thread))
4437     (while thread
4438       (gnus-remove-thread-1 (pop thread)))
4439     (when (setq d (gnus-data-find number))
4440       (goto-char (gnus-data-pos d))
4441       (gnus-summary-show-thread)
4442       (gnus-data-remove
4443        number
4444        (- (point-at-bol)
4445           (prog1
4446               (1+ (point-at-eol))
4447             (gnus-delete-line)))))))
4448
4449 (defun gnus-sort-threads-1 (threads func)
4450   (sort (mapcar (lambda (thread)
4451                   (cons (car thread)
4452                         (and (cdr thread)
4453                              (gnus-sort-threads-1 (cdr thread) func))))
4454                 threads) func))
4455
4456 (defun gnus-sort-threads (threads)
4457   "Sort THREADS."
4458   (if (not gnus-thread-sort-functions)
4459       threads
4460     (gnus-message 8 "Sorting threads...")
4461     (let ((max-lisp-eval-depth 5000))
4462       (prog1 (gnus-sort-threads-1
4463          threads
4464          (gnus-make-sort-function gnus-thread-sort-functions))
4465         (gnus-message 8 "Sorting threads...done")))))
4466
4467 (defun gnus-sort-articles (articles)
4468   "Sort ARTICLES."
4469   (when gnus-article-sort-functions
4470     (gnus-message 7 "Sorting articles...")
4471     (prog1
4472         (setq gnus-newsgroup-headers
4473               (sort articles (gnus-make-sort-function
4474                               gnus-article-sort-functions)))
4475       (gnus-message 7 "Sorting articles...done"))))
4476
4477 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4478 (defmacro gnus-thread-header (thread)
4479   "Return header of first article in THREAD.
4480 Note that THREAD must never, ever be anything else than a variable -
4481 using some other form will lead to serious barfage."
4482   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4483   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4484   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4485         (vector thread) 2))
4486
4487 (defsubst gnus-article-sort-by-number (h1 h2)
4488   "Sort articles by article number."
4489   (< (mail-header-number h1)
4490      (mail-header-number h2)))
4491
4492 (defun gnus-thread-sort-by-number (h1 h2)
4493   "Sort threads by root article number."
4494   (gnus-article-sort-by-number
4495    (gnus-thread-header h1) (gnus-thread-header h2)))
4496
4497 (defsubst gnus-article-sort-by-random (h1 h2)
4498   "Sort articles by article number."
4499   (zerop (random 2)))
4500
4501 (defun gnus-thread-sort-by-random (h1 h2)
4502   "Sort threads by root article number."
4503   (gnus-article-sort-by-random
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defsubst gnus-article-sort-by-lines (h1 h2)
4507   "Sort articles by article Lines header."
4508   (< (mail-header-lines h1)
4509      (mail-header-lines h2)))
4510
4511 (defun gnus-thread-sort-by-lines (h1 h2)
4512   "Sort threads by root article Lines header."
4513   (gnus-article-sort-by-lines
4514    (gnus-thread-header h1) (gnus-thread-header h2)))
4515
4516 (defsubst gnus-article-sort-by-chars (h1 h2)
4517   "Sort articles by octet length."
4518   (< (mail-header-chars h1)
4519      (mail-header-chars h2)))
4520
4521 (defun gnus-thread-sort-by-chars (h1 h2)
4522   "Sort threads by root article octet length."
4523   (gnus-article-sort-by-chars
4524    (gnus-thread-header h1) (gnus-thread-header h2)))
4525
4526 (defsubst gnus-article-sort-by-author (h1 h2)
4527   "Sort articles by root author."
4528   (string-lessp
4529    (let ((extract (funcall
4530                    gnus-extract-address-components
4531                    (mail-header-from h1))))
4532      (or (car extract) (cadr extract) ""))
4533    (let ((extract (funcall
4534                    gnus-extract-address-components
4535                    (mail-header-from h2))))
4536      (or (car extract) (cadr extract) ""))))
4537
4538 (defun gnus-thread-sort-by-author (h1 h2)
4539   "Sort threads by root author."
4540   (gnus-article-sort-by-author
4541    (gnus-thread-header h1)  (gnus-thread-header h2)))
4542
4543 (defsubst gnus-article-sort-by-subject (h1 h2)
4544   "Sort articles by root subject."
4545   (string-lessp
4546    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4547    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4548
4549 (defun gnus-thread-sort-by-subject (h1 h2)
4550   "Sort threads by root subject."
4551   (gnus-article-sort-by-subject
4552    (gnus-thread-header h1) (gnus-thread-header h2)))
4553
4554 (defsubst gnus-article-sort-by-date (h1 h2)
4555   "Sort articles by root article date."
4556   (time-less-p
4557    (gnus-date-get-time (mail-header-date h1))
4558    (gnus-date-get-time (mail-header-date h2))))
4559
4560 (defun gnus-thread-sort-by-date (h1 h2)
4561   "Sort threads by root article date."
4562   (gnus-article-sort-by-date
4563    (gnus-thread-header h1) (gnus-thread-header h2)))
4564
4565 (defsubst gnus-article-sort-by-score (h1 h2)
4566   "Sort articles by root article score.
4567 Unscored articles will be counted as having a score of zero."
4568   (> (or (cdr (assq (mail-header-number h1)
4569                     gnus-newsgroup-scored))
4570          gnus-summary-default-score 0)
4571      (or (cdr (assq (mail-header-number h2)
4572                     gnus-newsgroup-scored))
4573          gnus-summary-default-score 0)))
4574
4575 (defun gnus-thread-sort-by-score (h1 h2)
4576   "Sort threads by root article score."
4577   (gnus-article-sort-by-score
4578    (gnus-thread-header h1) (gnus-thread-header h2)))
4579
4580 (defun gnus-thread-sort-by-total-score (h1 h2)
4581   "Sort threads by the sum of all scores in the thread.
4582 Unscored articles will be counted as having a score of zero."
4583   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4584
4585 (defun gnus-thread-total-score (thread)
4586   ;; This function find the total score of THREAD.
4587   (cond
4588    ((null thread)
4589     0)
4590    ((consp thread)
4591     (if (stringp (car thread))
4592         (apply gnus-thread-score-function 0
4593                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4594       (gnus-thread-total-score-1 thread)))
4595    (t
4596     (gnus-thread-total-score-1 (list thread)))))
4597
4598 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4599   "Sort threads such that the thread with the most recently arrived article comes first."
4600   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4601
4602 (defun gnus-thread-highest-number (thread)
4603   "Return the highest article number in THREAD."
4604   (apply 'max (mapcar (lambda (header)
4605                         (mail-header-number header))
4606                       (message-flatten-list thread))))
4607
4608 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4609   "Sort threads such that the thread with the most recently dated article comes first."
4610   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4611
4612 (defun gnus-thread-latest-date (thread)
4613   "Return the highest article date in THREAD."
4614   (let ((previous-time 0))
4615     (apply 'max
4616            (mapcar
4617             (lambda (header)
4618               (setq previous-time
4619                     (condition-case ()
4620                         (time-to-seconds (mail-header-parse-date
4621                                           (mail-header-date header)))
4622                       (error previous-time))))
4623             (sort
4624              (message-flatten-list thread)
4625              (lambda (h1 h2)
4626                (< (mail-header-number h1)
4627                   (mail-header-number h2))))))))
4628
4629 (defun gnus-thread-total-score-1 (root)
4630   ;; This function find the total score of the thread below ROOT.
4631   (setq root (car root))
4632   (apply gnus-thread-score-function
4633          (or (append
4634               (mapcar 'gnus-thread-total-score
4635                       (cdr (gnus-id-to-thread (mail-header-id root))))
4636               (when (> (mail-header-number root) 0)
4637                 (list (or (cdr (assq (mail-header-number root)
4638                                      gnus-newsgroup-scored))
4639                           gnus-summary-default-score 0))))
4640              (list gnus-summary-default-score)
4641              '(0))))
4642
4643 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4644 (defvar gnus-tmp-prev-subject nil)
4645 (defvar gnus-tmp-false-parent nil)
4646 (defvar gnus-tmp-root-expunged nil)
4647 (defvar gnus-tmp-dummy-line nil)
4648
4649 (eval-when-compile (defvar gnus-tmp-header))
4650 (defun gnus-extra-header (type &optional header)
4651   "Return the extra header of TYPE."
4652   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4653       ""))
4654
4655 (defvar gnus-tmp-thread-tree-header-string "")
4656
4657 (defcustom gnus-sum-thread-tree-root "> "
4658   "With %B spec, used for the root of a thread.
4659 If nil, use subject instead."
4660   :version "21.4"
4661   :type '(radio (const :format "%v  " nil) (string :size 0))
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-false-root "> "
4664   "With %B spec, used for a false root of a thread.
4665 If nil, use subject instead."
4666   :version "21.4"
4667   :type '(radio (const :format "%v  " nil) (string :size 0))
4668   :group 'gnus-thread)
4669 (defcustom gnus-sum-thread-tree-single-indent ""
4670   "With %B spec, used for a thread with just one message.
4671 If nil, use subject instead."
4672   :version "21.4"
4673   :type '(radio (const :format "%v  " nil) (string :size 0))
4674   :group 'gnus-thread)
4675 (defcustom gnus-sum-thread-tree-vertical "| "
4676   "With %B spec, used for drawing a vertical line."
4677   :version "21.4"
4678   :type 'string
4679   :group 'gnus-thread)
4680 (defcustom gnus-sum-thread-tree-indent "  "
4681   "With %B spec, used for indenting."
4682   :version "21.4"
4683   :type 'string
4684   :group 'gnus-thread)
4685 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4686   "With %B spec, used for a leaf with brothers."
4687   :version "21.4"
4688   :type 'string
4689   :group 'gnus-thread)
4690 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4691   "With %B spec, used for a leaf without brothers."
4692   :version "21.4"
4693   :type 'string
4694   :group 'gnus-thread)
4695
4696 (defun gnus-summary-prepare-threads (threads)
4697   "Prepare summary buffer from THREADS and indentation LEVEL.
4698 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4699 or a straight list of headers."
4700   (gnus-message 7 "Generating summary...")
4701
4702   (setq gnus-newsgroup-threads threads)
4703   (beginning-of-line)
4704
4705   (let ((gnus-tmp-level 0)
4706         (default-score (or gnus-summary-default-score 0))
4707         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4708         (building-line-count gnus-summary-display-while-building)
4709         (building-count (integerp gnus-summary-display-while-building))
4710         thread number subject stack state gnus-tmp-gathered beg-match
4711         new-roots gnus-tmp-new-adopts thread-end simp-subject
4712         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4713         gnus-tmp-replied gnus-tmp-subject-or-nil
4714         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4715         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4716         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4717         tree-stack)
4718
4719     (setq gnus-tmp-prev-subject nil
4720           gnus-tmp-thread-tree-header-string "")
4721
4722     (if (vectorp (car threads))
4723         ;; If this is a straight (sic) list of headers, then a
4724         ;; threaded summary display isn't required, so we just create
4725         ;; an unthreaded one.
4726         (gnus-summary-prepare-unthreaded threads)
4727
4728       ;; Do the threaded display.
4729
4730       (if gnus-summary-display-while-building
4731           (switch-to-buffer (buffer-name)))
4732       (while (or threads stack gnus-tmp-new-adopts new-roots)
4733
4734         (if (and (= gnus-tmp-level 0)
4735                  (or (not stack)
4736                      (= (caar stack) 0))
4737                  (not gnus-tmp-false-parent)
4738                  (or gnus-tmp-new-adopts new-roots))
4739             (if gnus-tmp-new-adopts
4740                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4741                       thread (list (car gnus-tmp-new-adopts))
4742                       gnus-tmp-header (caar thread)
4743                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4744               (when new-roots
4745                 (setq thread (list (car new-roots))
4746                       gnus-tmp-header (caar thread)
4747                       new-roots (cdr new-roots))))
4748
4749           (if threads
4750               ;; If there are some threads, we do them before the
4751               ;; threads on the stack.
4752               (setq thread threads
4753                     gnus-tmp-header (caar thread))
4754             ;; There were no current threads, so we pop something off
4755             ;; the stack.
4756             (setq state (car stack)
4757                   gnus-tmp-level (car state)
4758                   tree-stack (cadr state)
4759                   thread (caddr state)
4760                   stack (cdr stack)
4761                   gnus-tmp-header (caar thread))))
4762
4763         (setq gnus-tmp-false-parent nil)
4764         (setq gnus-tmp-root-expunged nil)
4765         (setq thread-end nil)
4766
4767         (if (stringp gnus-tmp-header)
4768             ;; The header is a dummy root.
4769             (cond
4770              ((eq gnus-summary-make-false-root 'adopt)
4771               ;; We let the first article adopt the rest.
4772               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4773                                                (cddar thread)))
4774               (setq gnus-tmp-gathered
4775                     (nconc (mapcar
4776                             (lambda (h) (mail-header-number (car h)))
4777                             (cddar thread))
4778                            gnus-tmp-gathered))
4779               (setq thread (cons (list (caar thread)
4780                                        (cadar thread))
4781                                  (cdr thread)))
4782               (setq gnus-tmp-level -1
4783                     gnus-tmp-false-parent t))
4784              ((eq gnus-summary-make-false-root 'empty)
4785               ;; We print adopted articles with empty subject fields.
4786               (setq gnus-tmp-gathered
4787                     (nconc (mapcar
4788                             (lambda (h) (mail-header-number (car h)))
4789                             (cddar thread))
4790                            gnus-tmp-gathered))
4791               (setq gnus-tmp-level -1))
4792              ((eq gnus-summary-make-false-root 'dummy)
4793               ;; We remember that we probably want to output a dummy
4794               ;; root.
4795               (setq gnus-tmp-dummy-line gnus-tmp-header)
4796               (setq gnus-tmp-prev-subject gnus-tmp-header))
4797              (t
4798               ;; We do not make a root for the gathered
4799               ;; sub-threads at all.
4800               (setq gnus-tmp-level -1)))
4801
4802           (setq number (mail-header-number gnus-tmp-header)
4803                 subject (mail-header-subject gnus-tmp-header)
4804                 simp-subject (gnus-simplify-subject-fully subject))
4805
4806           (cond
4807            ;; If the thread has changed subject, we might want to make
4808            ;; this subthread into a root.
4809            ((and (null gnus-thread-ignore-subject)
4810                  (not (zerop gnus-tmp-level))
4811                  gnus-tmp-prev-subject
4812                  (not (string= gnus-tmp-prev-subject simp-subject)))
4813             (setq new-roots (nconc new-roots (list (car thread)))
4814                   thread-end t
4815                   gnus-tmp-header nil))
4816            ;; If the article lies outside the current limit,
4817            ;; then we do not display it.
4818            ((not (memq number gnus-newsgroup-limit))
4819             (setq gnus-tmp-gathered
4820                   (nconc (mapcar
4821                           (lambda (h) (mail-header-number (car h)))
4822                           (cdar thread))
4823                          gnus-tmp-gathered))
4824             (setq gnus-tmp-new-adopts (if (cdar thread)
4825                                           (append gnus-tmp-new-adopts
4826                                                   (cdar thread))
4827                                         gnus-tmp-new-adopts)
4828                   thread-end t
4829                   gnus-tmp-header nil)
4830             (when (zerop gnus-tmp-level)
4831               (setq gnus-tmp-root-expunged t)))
4832            ;; Perhaps this article is to be marked as read?
4833            ((and gnus-summary-mark-below
4834                  (< (or (cdr (assq number gnus-newsgroup-scored))
4835                         default-score)
4836                     gnus-summary-mark-below)
4837                  ;; Don't touch sparse articles.
4838                  (not (gnus-summary-article-sparse-p number))
4839                  (not (gnus-summary-article-ancient-p number)))
4840             (setq gnus-newsgroup-unreads
4841                   (delq number gnus-newsgroup-unreads))
4842             (if gnus-newsgroup-auto-expire
4843                 (setq gnus-newsgroup-expirable
4844                       (gnus-add-to-sorted-list
4845                        gnus-newsgroup-expirable number))
4846               (push (cons number gnus-low-score-mark)
4847                     gnus-newsgroup-reads))))
4848
4849           (when gnus-tmp-header
4850             ;; We may have an old dummy line to output before this
4851             ;; article.
4852             (when (and gnus-tmp-dummy-line
4853                        (gnus-subject-equal
4854                         gnus-tmp-dummy-line
4855                         (mail-header-subject gnus-tmp-header)))
4856               (gnus-summary-insert-dummy-line
4857                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4858               (setq gnus-tmp-dummy-line nil))
4859
4860             ;; Compute the mark.
4861             (setq gnus-tmp-unread (gnus-article-mark number))
4862
4863             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4864                                   gnus-tmp-header gnus-tmp-level)
4865                   gnus-newsgroup-data)
4866
4867             ;; Actually insert the line.
4868             (setq
4869              gnus-tmp-subject-or-nil
4870              (cond
4871               ((and gnus-thread-ignore-subject
4872                     gnus-tmp-prev-subject
4873                     (not (string= gnus-tmp-prev-subject simp-subject)))
4874                subject)
4875               ((zerop gnus-tmp-level)
4876                (if (and (eq gnus-summary-make-false-root 'empty)
4877                         (memq number gnus-tmp-gathered)
4878                         gnus-tmp-prev-subject
4879                         (string= gnus-tmp-prev-subject simp-subject))
4880                    gnus-summary-same-subject
4881                  subject))
4882               (t gnus-summary-same-subject)))
4883             (if (and (eq gnus-summary-make-false-root 'adopt)
4884                      (= gnus-tmp-level 1)
4885                      (memq number gnus-tmp-gathered))
4886                 (setq gnus-tmp-opening-bracket ?\<
4887                       gnus-tmp-closing-bracket ?\>)
4888               (setq gnus-tmp-opening-bracket ?\[
4889                     gnus-tmp-closing-bracket ?\]))
4890             (setq
4891              gnus-tmp-indentation
4892              (aref gnus-thread-indent-array gnus-tmp-level)
4893              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4894              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4895                                 gnus-summary-default-score 0)
4896              gnus-tmp-score-char
4897              (if (or (null gnus-summary-default-score)
4898                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4899                          gnus-summary-zcore-fuzz))
4900                  ?                      ;Whitespace
4901                (if (< gnus-tmp-score gnus-summary-default-score)
4902                    gnus-score-below-mark gnus-score-over-mark))
4903              gnus-tmp-replied
4904              (cond ((memq number gnus-newsgroup-processable)
4905                     gnus-process-mark)
4906                    ((memq number gnus-newsgroup-cached)
4907                     gnus-cached-mark)
4908                    ((memq number gnus-newsgroup-replied)
4909                     gnus-replied-mark)
4910                    ((memq number gnus-newsgroup-forwarded)
4911                     gnus-forwarded-mark)
4912                    ((memq number gnus-newsgroup-saved)
4913                     gnus-saved-mark)
4914                    ((memq number gnus-newsgroup-recent)
4915                     gnus-recent-mark)
4916                    ((memq number gnus-newsgroup-unseen)
4917                     gnus-unseen-mark)
4918                    (t gnus-no-mark))
4919              gnus-tmp-downloaded
4920              (cond ((memq number gnus-newsgroup-undownloaded)
4921                     gnus-undownloaded-mark)
4922                    (gnus-newsgroup-agentized
4923                     gnus-downloaded-mark)
4924                    (t
4925                     gnus-no-mark))
4926              gnus-tmp-from (mail-header-from gnus-tmp-header)
4927              gnus-tmp-name
4928              (cond
4929               ((string-match "<[^>]+> *$" gnus-tmp-from)
4930                (setq beg-match (match-beginning 0))
4931                (or (and (string-match "^\".+\"" gnus-tmp-from)
4932                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4933                    (substring gnus-tmp-from 0 beg-match)))
4934               ((string-match "(.+)" gnus-tmp-from)
4935                (substring gnus-tmp-from
4936                           (1+ (match-beginning 0)) (1- (match-end 0))))
4937               (t gnus-tmp-from))
4938
4939              ;; Do the %B string
4940              gnus-tmp-thread-tree-header-string
4941              (cond
4942               ((not gnus-show-threads) "")
4943               ((zerop gnus-tmp-level)
4944                (cond ((cdar thread)
4945                       (or gnus-sum-thread-tree-root subject))
4946                      (gnus-tmp-new-adopts
4947                       (or gnus-sum-thread-tree-false-root subject))
4948                      (t
4949                       (or gnus-sum-thread-tree-single-indent subject))))
4950               (t
4951                (concat (apply 'concat
4952                               (mapcar (lambda (item)
4953                                         (if (= item 1)
4954                                             gnus-sum-thread-tree-vertical
4955                                           gnus-sum-thread-tree-indent))
4956                                       (cdr (reverse tree-stack))))
4957                        (if (nth 1 thread)
4958                            gnus-sum-thread-tree-leaf-with-other
4959                          gnus-sum-thread-tree-single-leaf)))))
4960             (when (string= gnus-tmp-name "")
4961               (setq gnus-tmp-name gnus-tmp-from))
4962             (unless (numberp gnus-tmp-lines)
4963               (setq gnus-tmp-lines -1))
4964             (if (= gnus-tmp-lines -1)
4965                 (setq gnus-tmp-lines "?")
4966               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4967               (gnus-put-text-property
4968              (point)
4969              (progn (eval gnus-summary-line-format-spec) (point))
4970                'gnus-number number)
4971             (when gnus-visual-p
4972               (forward-line -1)
4973               (gnus-run-hooks 'gnus-summary-update-hook)
4974               (forward-line 1))
4975
4976             (setq gnus-tmp-prev-subject simp-subject)))
4977
4978         (when (nth 1 thread)
4979           (push (list (max 0 gnus-tmp-level)
4980                       (copy-sequence tree-stack)
4981                       (nthcdr 1 thread))
4982                 stack))
4983         (push (if (nth 1 thread) 1 0) tree-stack)
4984         (incf gnus-tmp-level)
4985         (setq threads (if thread-end nil (cdar thread)))
4986         (if gnus-summary-display-while-building
4987             (if building-count
4988                 (progn
4989                   ;; use a set frequency
4990                   (setq building-line-count (1- building-line-count))
4991                   (when (= building-line-count 0)
4992                     (sit-for 0)
4993                     (setq building-line-count
4994                           gnus-summary-display-while-building)))
4995               ;; always
4996               (sit-for 0)))
4997         (unless threads
4998           (setq gnus-tmp-level 0)))))
4999   (gnus-message 7 "Generating summary...done"))
5000
5001 (defun gnus-summary-prepare-unthreaded (headers)
5002   "Generate an unthreaded summary buffer based on HEADERS."
5003   (let (header number mark)
5004
5005     (beginning-of-line)
5006
5007     (while headers
5008       ;; We may have to root out some bad articles...
5009       (when (memq (setq number (mail-header-number
5010                                 (setq header (pop headers))))
5011                   gnus-newsgroup-limit)
5012         ;; Mark article as read when it has a low score.
5013         (when (and gnus-summary-mark-below
5014                    (< (or (cdr (assq number gnus-newsgroup-scored))
5015                           gnus-summary-default-score 0)
5016                       gnus-summary-mark-below)
5017                    (not (gnus-summary-article-ancient-p number)))
5018           (setq gnus-newsgroup-unreads
5019                 (delq number gnus-newsgroup-unreads))
5020           (if gnus-newsgroup-auto-expire
5021               (push number gnus-newsgroup-expirable)
5022             (push (cons number gnus-low-score-mark)
5023                   gnus-newsgroup-reads)))
5024
5025         (setq mark (gnus-article-mark number))
5026         (push (gnus-data-make number mark (1+ (point)) header 0)
5027               gnus-newsgroup-data)
5028         (gnus-summary-insert-line
5029          header 0 number
5030          (memq number gnus-newsgroup-undownloaded)
5031          mark (memq number gnus-newsgroup-replied)
5032          (memq number gnus-newsgroup-expirable)
5033          (mail-header-subject header) nil
5034          (cdr (assq number gnus-newsgroup-scored))
5035          (memq number gnus-newsgroup-processable))))))
5036
5037 (defun gnus-summary-remove-list-identifiers ()
5038   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5039   (let ((regexp (if (consp gnus-list-identifiers)
5040                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5041                   gnus-list-identifiers))
5042         changed subject)
5043     (when regexp
5044       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5045       (dolist (header gnus-newsgroup-headers)
5046         (setq subject (mail-header-subject header)
5047               changed nil)
5048         (while (string-match regexp subject)
5049           (setq subject
5050                 (concat (substring subject 0 (match-beginning 1))
5051                         (substring subject (match-end 0)))
5052                 changed t))
5053         (when changed
5054           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5055             (setq subject
5056                   (concat (substring subject 0 (match-beginning 1))
5057                           (substring subject (match-end 1)))))
5058           (mail-header-set-subject header subject))))))
5059
5060 (defun gnus-fetch-headers (articles)
5061   "Fetch headers of ARTICLES."
5062   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5063     (gnus-message 5 "Fetching headers for %s..." name)
5064     (prog1
5065         (if (eq 'nov
5066                 (setq gnus-headers-retrieved-by
5067                       (gnus-retrieve-headers
5068                        articles gnus-newsgroup-name
5069                        ;; We might want to fetch old headers, but
5070                        ;; not if there is only 1 article.
5071                        (and (or (and
5072                                  (not (eq gnus-fetch-old-headers 'some))
5073                                  (not (numberp gnus-fetch-old-headers)))
5074                                 (> (length articles) 1))
5075                             gnus-fetch-old-headers))))
5076             (gnus-get-newsgroup-headers-xover
5077              articles nil nil gnus-newsgroup-name t)
5078           (gnus-get-newsgroup-headers))
5079       (gnus-message 5 "Fetching headers for %s...done" name))))
5080
5081 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5082   "Select newsgroup GROUP.
5083 If READ-ALL is non-nil, all articles in the group are selected.
5084 If SELECT-ARTICLES, only select those articles from GROUP."
5085   (let* ((entry (gnus-group-entry group))
5086          ;;!!! Dirty hack; should be removed.
5087          (gnus-summary-ignore-duplicates
5088           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5089               t
5090             gnus-summary-ignore-duplicates))
5091          (info (nth 2 entry))
5092          articles fetched-articles cached)
5093
5094     (unless (gnus-check-server
5095              (set (make-local-variable 'gnus-current-select-method)
5096                   (gnus-find-method-for-group group)))
5097       (error "Couldn't open server"))
5098
5099     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5100         (gnus-activate-group group)     ; Or we can activate it...
5101         (progn                          ; Or we bug out.
5102           (when (equal major-mode 'gnus-summary-mode)
5103             (gnus-kill-buffer (current-buffer)))
5104           (error "Couldn't activate group %s: %s"
5105                  group (gnus-status-message group))))
5106
5107     (unless (gnus-request-group group t)
5108       (when (equal major-mode 'gnus-summary-mode)
5109         (gnus-kill-buffer (current-buffer)))
5110       (error "Couldn't request group %s: %s"
5111              group (gnus-status-message group)))
5112
5113     (when gnus-agent
5114       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5115       
5116       (setq gnus-summary-use-undownloaded-faces
5117             (gnus-agent-find-parameter
5118              group
5119              'agent-enable-undownloaded-faces)))
5120
5121     (setq gnus-newsgroup-name group
5122           gnus-newsgroup-unselected nil
5123           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5124
5125     (let ((display (gnus-group-find-parameter group 'display)))
5126       (setq gnus-newsgroup-display
5127             (cond
5128              ((not (zerop (or (car-safe read-all) 0)))
5129               ;; The user entered the group with C-u SPC/RET, let's show
5130               ;; all articles.
5131               'gnus-not-ignore)
5132              ((eq display 'all)
5133               'gnus-not-ignore)
5134              ((arrayp display)
5135               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5136              ((numberp display)
5137               ;; The following is probably the "correct" solution, but
5138               ;; it makes Gnus fetch all headers and then limit the
5139               ;; articles (which is slow), so instead we hack the
5140               ;; select-articles parameter instead. -- Simon Josefsson
5141               ;; <jas@kth.se>
5142               ;;
5143               ;; (gnus-byte-compile
5144               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5145               ;;                         display)))))
5146               (setq select-articles
5147                     (gnus-uncompress-range
5148                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5149                              (if (> tmp 0)
5150                                  tmp
5151                                1))
5152                            (cdr (gnus-active group)))))
5153               nil)
5154              (t
5155               nil))))
5156
5157     (gnus-summary-setup-default-charset)
5158
5159     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5160     (when (gnus-virtual-group-p group)
5161       (setq cached gnus-newsgroup-cached))
5162
5163     (setq gnus-newsgroup-unreads
5164           (gnus-sorted-ndifference
5165            (gnus-sorted-ndifference gnus-newsgroup-unreads
5166                                     gnus-newsgroup-marked)
5167            gnus-newsgroup-dormant))
5168
5169     (setq gnus-newsgroup-processable nil)
5170
5171     (gnus-update-read-articles group gnus-newsgroup-unreads)
5172
5173     ;; Adjust and set lists of article marks.
5174     (when info
5175       (gnus-adjust-marked-articles info))
5176     (if (setq articles select-articles)
5177         (setq gnus-newsgroup-unselected
5178               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5179       (setq articles (gnus-articles-to-read group read-all)))
5180
5181     (cond
5182      ((null articles)
5183       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5184       'quit)
5185      ((eq articles 0) nil)
5186      (t
5187       ;; Init the dependencies hash table.
5188       (setq gnus-newsgroup-dependencies
5189             (gnus-make-hashtable (length articles)))
5190       (gnus-set-global-variables)
5191       ;; Retrieve the headers and read them in.
5192
5193       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5194
5195       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5196       (when cached
5197         (setq gnus-newsgroup-cached cached))
5198
5199       ;; Suppress duplicates?
5200       (when gnus-suppress-duplicates
5201         (gnus-dup-suppress-articles))
5202
5203       ;; Set the initial limit.
5204       (setq gnus-newsgroup-limit (copy-sequence articles))
5205       ;; Remove canceled articles from the list of unread articles.
5206       (setq fetched-articles
5207             (mapcar (lambda (headers) (mail-header-number headers))
5208                     gnus-newsgroup-headers))
5209       (setq gnus-newsgroup-articles fetched-articles)
5210       (setq gnus-newsgroup-unreads
5211             (gnus-sorted-nintersection
5212              gnus-newsgroup-unreads fetched-articles))
5213       (gnus-compute-unseen-list)
5214
5215       ;; Removed marked articles that do not exist.
5216       (gnus-update-missing-marks
5217        (gnus-sorted-difference articles fetched-articles))
5218       ;; We might want to build some more threads first.
5219       (when (and gnus-fetch-old-headers
5220                  (eq gnus-headers-retrieved-by 'nov))
5221         (if (eq gnus-fetch-old-headers 'invisible)
5222             (gnus-build-all-threads)
5223           (gnus-build-old-threads)))
5224       ;; Let the Gnus agent mark articles as read.
5225       (when gnus-agent
5226         (gnus-agent-get-undownloaded-list))
5227       ;; Remove list identifiers from subject
5228       (when gnus-list-identifiers
5229         (gnus-summary-remove-list-identifiers))
5230       ;; Check whether auto-expire is to be done in this group.
5231       (setq gnus-newsgroup-auto-expire
5232             (gnus-group-auto-expirable-p group))
5233       ;; Set up the article buffer now, if necessary.
5234       (unless gnus-single-article-buffer
5235         (gnus-article-setup-buffer))
5236       ;; First and last article in this newsgroup.
5237       (when gnus-newsgroup-headers
5238         (setq gnus-newsgroup-begin
5239               (mail-header-number (car gnus-newsgroup-headers))
5240               gnus-newsgroup-end
5241               (mail-header-number
5242                (gnus-last-element gnus-newsgroup-headers))))
5243       ;; GROUP is successfully selected.
5244       (or gnus-newsgroup-headers t)))))
5245
5246 (defun gnus-compute-unseen-list ()
5247   ;; The `seen' marks are treated specially.
5248   (if (not gnus-newsgroup-seen)
5249       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5250     (setq gnus-newsgroup-unseen
5251           (gnus-inverse-list-range-intersection
5252            gnus-newsgroup-articles gnus-newsgroup-seen))))
5253
5254 (defun gnus-summary-display-make-predicate (display)
5255   (require 'gnus-agent)
5256   (when (= (length display) 1)
5257     (setq display (car display)))
5258   (unless gnus-summary-display-cache
5259     (dolist (elem (append '((unread . unread)
5260                             (read . read)
5261                             (unseen . unseen))
5262                           gnus-article-mark-lists))
5263       (push (cons (cdr elem)
5264                   (gnus-byte-compile
5265                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5266             gnus-summary-display-cache)))
5267   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5268         (gnus-category-predicate-cache gnus-summary-display-cache))
5269     (gnus-get-predicate display)))
5270
5271 ;; Uses the dynamically bound `number' variable.
5272 (eval-when-compile
5273   (defvar number))
5274 (defun gnus-article-marked-p (type &optional article)
5275   (let ((article (or article number)))
5276     (cond
5277      ((eq type 'tick)
5278       (memq article gnus-newsgroup-marked))
5279      ((eq type 'spam)
5280       (memq article gnus-newsgroup-spam-marked))
5281      ((eq type 'unsend)
5282       (memq article gnus-newsgroup-unsendable))
5283      ((eq type 'undownload)
5284       (memq article gnus-newsgroup-undownloaded))
5285      ((eq type 'download)
5286       (memq article gnus-newsgroup-downloadable))
5287      ((eq type 'unread)
5288       (memq article gnus-newsgroup-unreads))
5289      ((eq type 'read)
5290       (memq article gnus-newsgroup-reads))
5291      ((eq type 'dormant)
5292       (memq article gnus-newsgroup-dormant) )
5293      ((eq type 'expire)
5294       (memq article gnus-newsgroup-expirable))
5295      ((eq type 'reply)
5296       (memq article gnus-newsgroup-replied))
5297      ((eq type 'killed)
5298       (memq article gnus-newsgroup-killed))
5299      ((eq type 'bookmark)
5300       (assq article gnus-newsgroup-bookmarks))
5301      ((eq type 'score)
5302       (assq article gnus-newsgroup-scored))
5303      ((eq type 'save)
5304       (memq article gnus-newsgroup-saved))
5305      ((eq type 'cache)
5306       (memq article gnus-newsgroup-cached))
5307      ((eq type 'forward)
5308       (memq article gnus-newsgroup-forwarded))
5309      ((eq type 'seen)
5310       (not (memq article gnus-newsgroup-unseen)))
5311      ((eq type 'recent)
5312       (memq article gnus-newsgroup-recent))
5313      (t t))))
5314
5315 (defun gnus-articles-to-read (group &optional read-all)
5316   "Find out what articles the user wants to read."
5317   (let* ((articles
5318           ;; Select all articles if `read-all' is non-nil, or if there
5319           ;; are no unread articles.
5320           (if (or read-all
5321                   (and (zerop (length gnus-newsgroup-marked))
5322                        (zerop (length gnus-newsgroup-unreads)))
5323                   ;; Fetch all if the predicate is non-nil.
5324                   gnus-newsgroup-display)
5325               ;; We want to select the headers for all the articles in
5326               ;; the group, so we select either all the active
5327               ;; articles in the group, or (if that's nil), the
5328               ;; articles in the cache.
5329               (or
5330                (gnus-uncompress-range (gnus-active group))
5331                (gnus-cache-articles-in-group group))
5332             ;; Select only the "normal" subset of articles.
5333             (gnus-sorted-nunion
5334              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5335              gnus-newsgroup-unreads)))
5336          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5337          (scored (length scored-list))
5338          (number (length articles))
5339          (marked (+ (length gnus-newsgroup-marked)
5340                     (length gnus-newsgroup-dormant)))
5341          (select
5342           (cond
5343            ((numberp read-all)
5344             read-all)
5345            ((numberp gnus-newsgroup-display)
5346             gnus-newsgroup-display)
5347            (t
5348             (condition-case ()
5349                 (cond
5350                  ((and (or (<= scored marked) (= scored number))
5351                        (numberp gnus-large-newsgroup)
5352                        (> number gnus-large-newsgroup))
5353                   (let* ((cursor-in-echo-area nil)
5354                          (initial (gnus-parameter-large-newsgroup-initial
5355                                    gnus-newsgroup-name))
5356                          (input
5357                           (read-string
5358                            (format
5359                             "How many articles from %s (%s %d): "
5360                             (gnus-limit-string
5361                              (gnus-group-decoded-name gnus-newsgroup-name)
5362                              35)
5363                             (if initial "max" "default")
5364                             number)
5365                            (if initial
5366                                (cons (number-to-string initial)
5367                                      0)))))
5368                     (if (string-match "^[ \t]*$" input) number input)))
5369                  ((and (> scored marked) (< scored number)
5370                        (> (- scored number) 20))
5371                   (let ((input
5372                          (read-string
5373                           (format "%s %s (%d scored, %d total): "
5374                                   "How many articles from"
5375                                   (gnus-group-decoded-name group)
5376                                   scored number))))
5377                     (if (string-match "^[ \t]*$" input)
5378                         number input)))
5379                  (t number))
5380               (quit
5381                (message "Quit getting the articles to read")
5382                nil))))))
5383     (setq select (if (stringp select) (string-to-number select) select))
5384     (if (or (null select) (zerop select))
5385         select
5386       (if (and (not (zerop scored)) (<= (abs select) scored))
5387           (progn
5388             (setq articles (sort scored-list '<))
5389             (setq number (length articles)))
5390         (setq articles (copy-sequence articles)))
5391
5392       (when (< (abs select) number)
5393         (if (< select 0)
5394             ;; Select the N oldest articles.
5395             (setcdr (nthcdr (1- (abs select)) articles) nil)
5396           ;; Select the N most recent articles.
5397           (setq articles (nthcdr (- number select) articles))))
5398       (setq gnus-newsgroup-unselected
5399             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5400       (when gnus-alter-articles-to-read-function
5401         (setq articles
5402               (sort
5403                (funcall gnus-alter-articles-to-read-function
5404                         gnus-newsgroup-name articles)
5405                '<)))
5406       articles)))
5407
5408 (defun gnus-killed-articles (killed articles)
5409   (let (out)
5410     (while articles
5411       (when (inline (gnus-member-of-range (car articles) killed))
5412         (push (car articles) out))
5413       (setq articles (cdr articles)))
5414     out))
5415
5416 (defun gnus-uncompress-marks (marks)
5417   "Uncompress the mark ranges in MARKS."
5418   (let ((uncompressed '(score bookmark))
5419         out)
5420     (while marks
5421       (if (memq (caar marks) uncompressed)
5422           (push (car marks) out)
5423         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5424       (setq marks (cdr marks)))
5425     out))
5426
5427 (defun gnus-article-mark-to-type (mark)
5428   "Return the type of MARK."
5429   (or (cadr (assq mark gnus-article-special-mark-lists))
5430       'list))
5431
5432 (defun gnus-article-unpropagatable-p (mark)
5433   "Return whether MARK should be propagated to back end."
5434   (memq mark gnus-article-unpropagated-mark-lists))
5435
5436 (defun gnus-adjust-marked-articles (info)
5437   "Set all article lists and remove all marks that are no longer valid."
5438   (let* ((marked-lists (gnus-info-marks info))
5439          (active (gnus-active (gnus-info-group info)))
5440          (min (car active))
5441          (max (cdr active))
5442          (types gnus-article-mark-lists)
5443          marks var articles article mark mark-type
5444          bgn end)
5445
5446     (dolist (marks marked-lists)
5447       (setq mark (car marks)
5448             mark-type (gnus-article-mark-to-type mark)
5449             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5450
5451       ;; We set the variable according to the type of the marks list,
5452       ;; and then adjust the marks to a subset of the active articles.
5453       (cond
5454        ;; Adjust "simple" lists - compressed yet unsorted
5455        ((eq mark-type 'list)
5456         ;; Simultaneously uncompress and clip to active range
5457         ;; See gnus-uncompress-range for a description of possible marks
5458         (let (l lh)
5459           (if (not (cadr marks))
5460               (set var nil)
5461             (setq articles (if (numberp (cddr marks))
5462                                (list (cdr marks))
5463                              (cdr marks))
5464                   lh (cons nil nil)
5465                   l lh)
5466
5467             (while (setq article (pop articles))
5468               (cond ((consp article)
5469                      (setq bgn (max (car article) min)
5470                            end (min (cdr article) max))
5471                      (while (<= bgn end)
5472                        (setq l (setcdr l (cons bgn nil))
5473                              bgn (1+ bgn))))
5474                     ((and (<= min article)
5475                           (>= max article))
5476                      (setq l (setcdr l (cons article nil))))))
5477             (set var (cdr lh)))))
5478        ;; Adjust assocs.
5479        ((eq mark-type 'tuple)
5480         (set var (setq articles (cdr marks)))
5481         (when (not (listp (cdr (symbol-value var))))
5482           (set var (list (symbol-value var))))
5483         (when (not (listp (cdr articles)))
5484           (setq articles (list articles)))
5485         (while articles
5486           (when (or (not (consp (setq article (pop articles))))
5487                     (< (car article) min)
5488                     (> (car article) max))
5489             (set var (delq article (symbol-value var))))))
5490        ;; Adjust ranges (sloppily).
5491        ((eq mark-type 'range)
5492         (cond
5493          ((eq mark 'seen)
5494           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5495           ;; It should be (seen (NUM1 . NUM2)).
5496           (when (numberp (cddr marks))
5497             (setcdr marks (list (cdr marks))))
5498           (setq articles (cdr marks))
5499           (while (and articles
5500                       (or (and (consp (car articles))
5501                                (> min (cdar articles)))
5502                           (and (numberp (car articles))
5503                                (> min (car articles)))))
5504             (pop articles))
5505           (set var articles))))))))
5506
5507 (defun gnus-update-missing-marks (missing)
5508   "Go through the list of MISSING articles and remove them from the mark lists."
5509   (when missing
5510     (let (var m)
5511       ;; Go through all types.
5512       (dolist (elem gnus-article-mark-lists)
5513         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5514           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5515           (when (symbol-value var)
5516             ;; This list has articles.  So we delete all missing
5517             ;; articles from it.
5518             (setq m missing)
5519             (while m
5520               (set var (delq (pop m) (symbol-value var))))))))))
5521
5522 (defun gnus-update-marks ()
5523   "Enter the various lists of marked articles into the newsgroup info list."
5524   (let ((types gnus-article-mark-lists)
5525         (info (gnus-get-info gnus-newsgroup-name))
5526         type list newmarked symbol delta-marks)
5527     (when info
5528       ;; Add all marks lists to the list of marks lists.
5529       (while (setq type (pop types))
5530         (setq list (symbol-value
5531                     (setq symbol
5532                           (intern (format "gnus-newsgroup-%s" (car type))))))
5533
5534         (when list
5535           ;; Get rid of the entries of the articles that have the
5536           ;; default score.
5537           (when (and (eq (cdr type) 'score)
5538                      gnus-save-score
5539                      list)
5540             (let* ((arts list)
5541                    (prev (cons nil list))
5542                    (all prev))
5543               (while arts
5544                 (if (or (not (consp (car arts)))
5545                         (= (cdar arts) gnus-summary-default-score))
5546                     (setcdr prev (cdr arts))
5547                   (setq prev arts))
5548                 (setq arts (cdr arts)))
5549               (setq list (cdr all)))))
5550
5551         (when (eq (cdr type) 'seen)
5552           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5553
5554         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5555           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5556
5557         (when (and (gnus-check-backend-function
5558                     'request-set-mark gnus-newsgroup-name)
5559                    (not (gnus-article-unpropagatable-p (cdr type))))
5560           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5561                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5562                  (add (gnus-remove-from-range
5563                        (gnus-copy-sequence list) old)))
5564             (when add
5565               (push (list add 'add (list (cdr type))) delta-marks))
5566             (when del
5567               (push (list del 'del (list (cdr type))) delta-marks))))
5568
5569         (when list
5570           (push (cons (cdr type) list) newmarked)))
5571
5572       (when delta-marks
5573         (unless (gnus-check-group gnus-newsgroup-name)
5574           (error "Can't open server for %s" gnus-newsgroup-name))
5575         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5576
5577       ;; Enter these new marks into the info of the group.
5578       (if (nthcdr 3 info)
5579           (setcar (nthcdr 3 info) newmarked)
5580         ;; Add the marks lists to the end of the info.
5581         (when newmarked
5582           (setcdr (nthcdr 2 info) (list newmarked))))
5583
5584       ;; Cut off the end of the info if there's nothing else there.
5585       (let ((i 5))
5586         (while (and (> i 2)
5587                     (not (nth i info)))
5588           (when (nthcdr (decf i) info)
5589             (setcdr (nthcdr i info) nil)))))))
5590
5591 (defun gnus-set-mode-line (where)
5592   "Set the mode line of the article or summary buffers.
5593 If WHERE is `summary', the summary mode line format will be used."
5594   ;; Is this mode line one we keep updated?
5595   (when (and (memq where gnus-updated-mode-lines)
5596              (symbol-value
5597               (intern (format "gnus-%s-mode-line-format-spec" where))))
5598     (let (mode-string)
5599       (save-excursion
5600         ;; We evaluate this in the summary buffer since these
5601         ;; variables are buffer-local to that buffer.
5602         (set-buffer gnus-summary-buffer)
5603        ;; We bind all these variables that are used in the `eval' form
5604         ;; below.
5605         (let* ((mformat (symbol-value
5606                          (intern
5607                           (format "gnus-%s-mode-line-format-spec" where))))
5608                (gnus-tmp-group-name (gnus-group-decoded-name
5609                                      gnus-newsgroup-name))
5610                (gnus-tmp-article-number (or gnus-current-article 0))
5611                (gnus-tmp-unread gnus-newsgroup-unreads)
5612                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5613                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5614                (gnus-tmp-unread-and-unselected
5615                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5616                             (zerop gnus-tmp-unselected))
5617                        "")
5618                       ((zerop gnus-tmp-unselected)
5619                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5620                       (t (format "{%d(+%d) more}"
5621                                  gnus-tmp-unread-and-unticked
5622                                  gnus-tmp-unselected))))
5623                (gnus-tmp-subject
5624                 (if (and gnus-current-headers
5625                          (vectorp gnus-current-headers))
5626                     (gnus-mode-string-quote
5627                      (mail-header-subject gnus-current-headers))
5628                   ""))
5629                bufname-length max-len
5630                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5631           (setq mode-string (eval mformat))
5632           (setq bufname-length (if (string-match "%b" mode-string)
5633                                    (- (length
5634                                        (buffer-name
5635                                         (if (eq where 'summary)
5636                                             nil
5637                                           (get-buffer gnus-article-buffer))))
5638                                       2)
5639                                  0))
5640           (setq max-len (max 4 (if gnus-mode-non-string-length
5641                                    (- (window-width)
5642                                       gnus-mode-non-string-length
5643                                       bufname-length)
5644                                  (length mode-string))))
5645           ;; We might have to chop a bit of the string off...
5646           (when (> (length mode-string) max-len)
5647             (setq mode-string
5648                   (concat (truncate-string-to-width mode-string (- max-len 3))
5649                           "...")))
5650           ;; Pad the mode string a bit.
5651           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5652       ;; Update the mode line.
5653       (setq mode-line-buffer-identification
5654             (gnus-mode-line-buffer-identification (list mode-string)))
5655       (set-buffer-modified-p t))))
5656
5657 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5658   "Go through the HEADERS list and add all Xrefs to a hash table.
5659 The resulting hash table is returned, or nil if no Xrefs were found."
5660   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5661          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5662          (xref-hashtb (gnus-make-hashtable))
5663          start group entry number xrefs header)
5664     (while headers
5665       (setq header (pop headers))
5666       (when (and (setq xrefs (mail-header-xref header))
5667                  (not (memq (setq number (mail-header-number header))
5668                             unreads)))
5669         (setq start 0)
5670         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5671           (setq start (match-end 0))
5672           (setq group (if prefix
5673                           (concat prefix (substring xrefs (match-beginning 1)
5674                                                     (match-end 1)))
5675                         (substring xrefs (match-beginning 1) (match-end 1))))
5676           (setq number
5677                 (string-to-int (substring xrefs (match-beginning 2)
5678                                           (match-end 2))))
5679           (if (setq entry (gnus-gethash group xref-hashtb))
5680               (setcdr entry (cons number (cdr entry)))
5681             (gnus-sethash group (cons number nil) xref-hashtb)))))
5682     (and start xref-hashtb)))
5683
5684 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5685   "Look through all the headers and mark the Xrefs as read."
5686   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5687         name info xref-hashtb idlist method nth4)
5688     (save-excursion
5689       (set-buffer gnus-group-buffer)
5690       (when (setq xref-hashtb
5691                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5692         (mapatoms
5693          (lambda (group)
5694            (unless (string= from-newsgroup (setq name (symbol-name group)))
5695              (setq idlist (symbol-value group))
5696              ;; Dead groups are not updated.
5697              (and (prog1
5698                       (setq info (gnus-get-info name))
5699                     (when (stringp (setq nth4 (gnus-info-method info)))
5700                       (setq nth4 (gnus-server-to-method nth4))))
5701                   ;; Only do the xrefs if the group has the same
5702                   ;; select method as the group we have just read.
5703                   (or (gnus-methods-equal-p
5704                        nth4 (gnus-find-method-for-group from-newsgroup))
5705                       virtual
5706                       (equal nth4 (setq method (gnus-find-method-for-group
5707                                                 from-newsgroup)))
5708                       (and (equal (car nth4) (car method))
5709                            (equal (nth 1 nth4) (nth 1 method))))
5710                   gnus-use-cross-reference
5711                   (or (not (eq gnus-use-cross-reference t))
5712                       virtual
5713                       ;; Only do cross-references on subscribed
5714                       ;; groups, if that is what is wanted.
5715                       (<= (gnus-info-level info) gnus-level-subscribed))
5716                   (gnus-group-make-articles-read name idlist))))
5717          xref-hashtb)))))
5718
5719 (defun gnus-compute-read-articles (group articles)
5720   (let* ((entry (gnus-group-entry group))
5721          (info (nth 2 entry))
5722          (active (gnus-active group))
5723          ninfo)
5724     (when entry
5725       ;; First peel off all invalid article numbers.
5726       (when active
5727         (let ((ids articles)
5728               id first)
5729           (while (setq id (pop ids))
5730             (when (and first (> id (cdr active)))
5731               ;; We'll end up in this situation in one particular
5732               ;; obscure situation.  If you re-scan a group and get
5733               ;; a new article that is cross-posted to a different
5734               ;; group that has not been re-scanned, you might get
5735               ;; crossposted article that has a higher number than
5736               ;; Gnus believes possible.  So we re-activate this
5737               ;; group as well.  This might mean doing the
5738               ;; crossposting thingy will *increase* the number
5739               ;; of articles in some groups.  Tsk, tsk.
5740               (setq active (or (gnus-activate-group group) active)))
5741             (when (or (> id (cdr active))
5742                       (< id (car active)))
5743               (setq articles (delq id articles))))))
5744       ;; If the read list is nil, we init it.
5745       (if (and active
5746                (null (gnus-info-read info))
5747                (> (car active) 1))
5748           (setq ninfo (cons 1 (1- (car active))))
5749         (setq ninfo (gnus-info-read info)))
5750       ;; Then we add the read articles to the range.
5751       (gnus-add-to-range
5752        ninfo (setq articles (sort articles '<))))))
5753
5754 (defun gnus-group-make-articles-read (group articles)
5755   "Update the info of GROUP to say that ARTICLES are read."
5756   (let* ((num 0)
5757          (entry (gnus-group-entry group))
5758          (info (nth 2 entry))
5759          (active (gnus-active group))
5760          range)
5761     (when entry
5762       (setq range (gnus-compute-read-articles group articles))
5763       (with-current-buffer gnus-group-buffer
5764         (gnus-undo-register
5765           `(progn
5766              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5767              (gnus-info-set-read ',info ',(gnus-info-read info))
5768              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5769              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5770              (gnus-group-update-group ,group t))))
5771       ;; Add the read articles to the range.
5772       (gnus-info-set-read info range)
5773       (gnus-request-set-mark group (list (list range 'add '(read))))
5774       ;; Then we have to re-compute how many unread
5775       ;; articles there are in this group.
5776       (when active
5777         (cond
5778          ((not range)
5779           (setq num (- (1+ (cdr active)) (car active))))
5780          ((not (listp (cdr range)))
5781           (setq num (- (cdr active) (- (1+ (cdr range))
5782                                        (car range)))))
5783          (t
5784           (while range
5785             (if (numberp (car range))
5786                 (setq num (1+ num))
5787               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5788             (setq range (cdr range)))
5789           (setq num (- (cdr active) num))))
5790         ;; Update the number of unread articles.
5791         (setcar entry num)
5792         ;; Update the group buffer.
5793         (unless (gnus-ephemeral-group-p group)
5794           (gnus-group-update-group group t))))))
5795
5796 (defvar gnus-newsgroup-none-id 0)
5797
5798 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5799   (let ((cur nntp-server-buffer)
5800         (dependencies
5801          (or dependencies
5802              (with-current-buffer gnus-summary-buffer
5803                gnus-newsgroup-dependencies)))
5804         headers id end ref number
5805         (mail-parse-charset gnus-newsgroup-charset)
5806         (mail-parse-ignored-charsets
5807          (save-excursion (condition-case nil
5808                              (set-buffer gnus-summary-buffer)
5809                            (error))
5810                          gnus-newsgroup-ignored-charsets)))
5811     (save-excursion
5812       (set-buffer nntp-server-buffer)
5813       ;; Translate all TAB characters into SPACE characters.
5814       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5815       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5816       (ietf-drums-unfold-fws)
5817       (gnus-run-hooks 'gnus-parse-headers-hook)
5818       (let ((case-fold-search t)
5819             in-reply-to header p lines chars)
5820         (goto-char (point-min))
5821         ;; Search to the beginning of the next header.  Error messages
5822         ;; do not begin with 2 or 3.
5823         (while (re-search-forward "^[23][0-9]+ " nil t)
5824           (setq id nil
5825                 ref nil)
5826           ;; This implementation of this function, with nine
5827           ;; search-forwards instead of the one re-search-forward and
5828           ;; a case (which basically was the old function) is actually
5829           ;; about twice as fast, even though it looks messier.  You
5830           ;; can't have everything, I guess.  Speed and elegance
5831           ;; doesn't always go hand in hand.
5832           (setq
5833            header
5834            (vector
5835             ;; Number.
5836             (prog1
5837                 (setq number (read cur))
5838               (end-of-line)
5839               (setq p (point))
5840               (narrow-to-region (point)
5841                                 (or (and (search-forward "\n.\n" nil t)
5842                                          (- (point) 2))
5843                                     (point))))
5844             ;; Subject.
5845             (progn
5846               (goto-char p)
5847               (if (search-forward "\nsubject:" nil t)
5848                   (funcall gnus-decode-encoded-word-function
5849                            (nnheader-header-value))
5850                 "(none)"))
5851             ;; From.
5852             (progn
5853               (goto-char p)
5854               (if (search-forward "\nfrom:" nil t)
5855                   (funcall gnus-decode-encoded-word-function
5856                            (nnheader-header-value))
5857                 "(nobody)"))
5858             ;; Date.
5859             (progn
5860               (goto-char p)
5861               (if (search-forward "\ndate:" nil t)
5862                   (nnheader-header-value) ""))
5863             ;; Message-ID.
5864             (progn
5865               (goto-char p)
5866               (setq id (if (re-search-forward
5867                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5868                            ;; We do it this way to make sure the Message-ID
5869                            ;; is (somewhat) syntactically valid.
5870                            (buffer-substring (match-beginning 1)
5871                                              (match-end 1))
5872                          ;; If there was no message-id, we just fake one
5873                          ;; to make subsequent routines simpler.
5874                          (nnheader-generate-fake-message-id number))))
5875             ;; References.
5876             (progn
5877               (goto-char p)
5878               (if (search-forward "\nreferences:" nil t)
5879                   (progn
5880                     (setq end (point))
5881                     (prog1
5882                         (nnheader-header-value)
5883                       (setq ref
5884                             (buffer-substring
5885                              (progn
5886                                (end-of-line)
5887                                (search-backward ">" end t)
5888                                (1+ (point)))
5889                              (progn
5890                                (search-backward "<" end t)
5891                                (point))))))
5892                 ;; Get the references from the in-reply-to header if there
5893                 ;; were no references and the in-reply-to header looks
5894                 ;; promising.
5895                 (if (and (search-forward "\nin-reply-to:" nil t)
5896                          (setq in-reply-to (nnheader-header-value))
5897                          (string-match "<[^>]+>" in-reply-to))
5898                     (let (ref2)
5899                       (setq ref (substring in-reply-to (match-beginning 0)
5900                                            (match-end 0)))
5901                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5902                         (setq ref2 (substring in-reply-to (match-beginning 0)
5903                                               (match-end 0)))
5904                         (when (> (length ref2) (length ref))
5905                           (setq ref ref2)))
5906                       ref)
5907                   (setq ref nil))))
5908             ;; Chars.
5909             (progn
5910               (goto-char p)
5911               (if (search-forward "\nchars: " nil t)
5912                   (if (numberp (setq chars (ignore-errors (read cur))))
5913                       chars -1)
5914                 -1))
5915             ;; Lines.
5916             (progn
5917               (goto-char p)
5918               (if (search-forward "\nlines: " nil t)
5919                   (if (numberp (setq lines (ignore-errors (read cur))))
5920                       lines -1)
5921                 -1))
5922             ;; Xref.
5923             (progn
5924               (goto-char p)
5925               (and (search-forward "\nxref:" nil t)
5926                    (nnheader-header-value)))
5927             ;; Extra.
5928             (when gnus-extra-headers
5929               (let ((extra gnus-extra-headers)
5930                     out)
5931                 (while extra
5932                   (goto-char p)
5933                   (when (search-forward
5934                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5935                     (push (cons (car extra) (nnheader-header-value))
5936                           out))
5937                   (pop extra))
5938                 out))))
5939           (when (equal id ref)
5940             (setq ref nil))
5941
5942           (when gnus-alter-header-function
5943             (funcall gnus-alter-header-function header)
5944             (setq id (mail-header-id header)
5945                   ref (gnus-parent-id (mail-header-references header))))
5946
5947           (when (setq header
5948                       (gnus-dependencies-add-header
5949                        header dependencies force-new))
5950             (push header headers))
5951           (goto-char (point-max))
5952           (widen))
5953         (nreverse headers)))))
5954
5955 ;; Goes through the xover lines and returns a list of vectors
5956 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5957                                                   force-new dependencies
5958                                                   group also-fetch-heads)
5959   "Parse the news overview data in the server buffer.
5960 Return a list of headers that match SEQUENCE (see
5961 `nntp-retrieve-headers')."
5962   ;; Get the Xref when the users reads the articles since most/some
5963   ;; NNTP servers do not include Xrefs when using XOVER.
5964   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5965   (let ((mail-parse-charset gnus-newsgroup-charset)
5966         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5967         (cur nntp-server-buffer)
5968         (dependencies (or dependencies gnus-newsgroup-dependencies))
5969         (allp (cond
5970                ((eq gnus-read-all-available-headers t)
5971                 t)
5972                ((stringp gnus-read-all-available-headers)
5973                 (string-match gnus-read-all-available-headers group))
5974                (t
5975                 nil)))
5976         number headers header)
5977     (save-excursion
5978       (set-buffer nntp-server-buffer)
5979       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5980       ;; Allow the user to mangle the headers before parsing them.
5981       (gnus-run-hooks 'gnus-parse-headers-hook)
5982       (goto-char (point-min))
5983       (gnus-parse-without-error
5984         (while (and (or sequence allp)
5985                     (not (eobp)))
5986           (setq number (read cur))
5987           (when (not allp)
5988             (while (and sequence
5989                         (< (car sequence) number))
5990               (setq sequence (cdr sequence))))
5991           (when (and (or allp
5992                          (and sequence
5993                               (eq number (car sequence))))
5994                      (progn
5995                        (setq sequence (cdr sequence))
5996                        (setq header (inline
5997                                       (gnus-nov-parse-line
5998                                        number dependencies force-new)))))
5999             (push header headers))
6000           (forward-line 1)))
6001       ;; A common bug in inn is that if you have posted an article and
6002       ;; then retrieves the active file, it will answer correctly --
6003       ;; the new article is included.  However, a NOV entry for the
6004       ;; article may not have been generated yet, so this may fail.
6005       ;; We work around this problem by retrieving the last few
6006       ;; headers using HEAD.
6007       (if (or (not also-fetch-heads)
6008               (not sequence))
6009           ;; We (probably) got all the headers.
6010           (nreverse headers)
6011         (let ((gnus-nov-is-evil t))
6012           (nconc
6013            (nreverse headers)
6014            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6015              (gnus-get-newsgroup-headers))))))))
6016
6017 (defun gnus-article-get-xrefs ()
6018   "Fill in the Xref value in `gnus-current-headers', if necessary.
6019 This is meant to be called in `gnus-article-internal-prepare-hook'."
6020   (let ((headers (with-current-buffer gnus-summary-buffer
6021                    gnus-current-headers)))
6022     (or (not gnus-use-cross-reference)
6023         (not headers)
6024         (and (mail-header-xref headers)
6025              (not (string= (mail-header-xref headers) "")))
6026         (let ((case-fold-search t)
6027               xref)
6028           (save-restriction
6029             (nnheader-narrow-to-headers)
6030             (goto-char (point-min))
6031             (when (or (and (not (eobp))
6032                            (eq (downcase (char-after)) ?x)
6033                            (looking-at "Xref:"))
6034                       (search-forward "\nXref:" nil t))
6035               (goto-char (1+ (match-end 0)))
6036               (setq xref (buffer-substring (point) (point-at-eol)))
6037               (mail-header-set-xref headers xref)))))))
6038
6039 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6040   "Find article ID and insert the summary line for that article.
6041 OLD-HEADER can either be a header or a line number to insert
6042 the subject line on."
6043   (let* ((line (and (numberp old-header) old-header))
6044          (old-header (and (vectorp old-header) old-header))
6045          (header (cond ((and old-header use-old-header)
6046                         old-header)
6047                        ((and (numberp id)
6048                              (gnus-number-to-header id))
6049                         (gnus-number-to-header id))
6050                        (t
6051                         (gnus-read-header id))))
6052          (number (and (numberp id) id))
6053          d)
6054     (when header
6055       ;; Rebuild the thread that this article is part of and go to the
6056       ;; article we have fetched.
6057       (when (and (not gnus-show-threads)
6058                  old-header)
6059         (when (and number
6060                    (setq d (gnus-data-find (mail-header-number old-header))))
6061           (goto-char (gnus-data-pos d))
6062           (gnus-data-remove
6063            number
6064            (- (point-at-bol)
6065               (prog1
6066                   (1+ (point-at-eol))
6067                 (gnus-delete-line))))))
6068       ;; Remove list identifiers from subject.
6069       (when gnus-list-identifiers
6070         (let ((gnus-newsgroup-headers (list header)))
6071           (gnus-summary-remove-list-identifiers)
6072           (setq header (car gnus-newsgroup-headers))))
6073       (when old-header
6074         (mail-header-set-number header (mail-header-number old-header)))
6075       (setq gnus-newsgroup-sparse
6076             (delq (setq number (mail-header-number header))
6077                   gnus-newsgroup-sparse))
6078       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6079       (push number gnus-newsgroup-limit)
6080       (gnus-rebuild-thread (mail-header-id header) line)
6081       (gnus-summary-goto-subject number nil t))
6082     (when (and (numberp number)
6083                (> number 0))
6084       ;; We have to update the boundaries even if we can't fetch the
6085       ;; article if ID is a number -- so that the next `P' or `N'
6086       ;; command will fetch the previous (or next) article even
6087       ;; if the one we tried to fetch this time has been canceled.
6088       (when (> number gnus-newsgroup-end)
6089         (setq gnus-newsgroup-end number))
6090       (when (< number gnus-newsgroup-begin)
6091         (setq gnus-newsgroup-begin number))
6092       (setq gnus-newsgroup-unselected
6093             (delq number gnus-newsgroup-unselected)))
6094     ;; Report back a success?
6095     (and header (mail-header-number header))))
6096
6097 ;;; Process/prefix in the summary buffer
6098
6099 (defun gnus-summary-work-articles (n)
6100   "Return a list of articles to be worked upon.
6101 The prefix argument, the list of process marked articles, and the
6102 current article will be taken into consideration."
6103   (save-excursion
6104     (set-buffer gnus-summary-buffer)
6105     (cond
6106      (n
6107       ;; A numerical prefix has been given.
6108       (setq n (prefix-numeric-value n))
6109       (let ((backward (< n 0))
6110             (n (abs (prefix-numeric-value n)))
6111             articles article)
6112         (save-excursion
6113           (while
6114               (and (> n 0)
6115                    (push (setq article (gnus-summary-article-number))
6116                          articles)
6117                    (if backward
6118                        (gnus-summary-find-prev nil article)
6119                      (gnus-summary-find-next nil article)))
6120             (decf n)))
6121         (nreverse articles)))
6122      ((and (gnus-region-active-p) (mark))
6123       (message "region active")
6124       ;; Work on the region between point and mark.
6125       (let ((max (max (point) (mark)))
6126             articles article)
6127         (save-excursion
6128           (goto-char (min (min (point) (mark))))
6129           (while
6130               (and
6131                (push (setq article (gnus-summary-article-number)) articles)
6132                (gnus-summary-find-next nil article)
6133                (< (point) max)))
6134           (nreverse articles))))
6135      (gnus-newsgroup-processable
6136       ;; There are process-marked articles present.
6137       ;; Save current state.
6138       (gnus-summary-save-process-mark)
6139       ;; Return the list.
6140       (reverse gnus-newsgroup-processable))
6141      (t
6142       ;; Just return the current article.
6143       (list (gnus-summary-article-number))))))
6144
6145 (defmacro gnus-summary-iterate (arg &rest forms)
6146   "Iterate over the process/prefixed articles and do FORMS.
6147 ARG is the interactive prefix given to the command.  FORMS will be
6148 executed with point over the summary line of the articles."
6149   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6150     `(let ((,articles (gnus-summary-work-articles ,arg)))
6151        (while ,articles
6152          (gnus-summary-goto-subject (car ,articles))
6153          ,@forms
6154          (pop ,articles)))))
6155
6156 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6157 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6158
6159 (defun gnus-summary-save-process-mark ()
6160   "Push the current set of process marked articles on the stack."
6161   (interactive)
6162   (push (copy-sequence gnus-newsgroup-processable)
6163         gnus-newsgroup-process-stack))
6164
6165 (defun gnus-summary-kill-process-mark ()
6166   "Push the current set of process marked articles on the stack and unmark."
6167   (interactive)
6168   (gnus-summary-save-process-mark)
6169   (gnus-summary-unmark-all-processable))
6170
6171 (defun gnus-summary-yank-process-mark ()
6172   "Pop the last process mark state off the stack and restore it."
6173   (interactive)
6174   (unless gnus-newsgroup-process-stack
6175     (error "Empty mark stack"))
6176   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6177
6178 (defun gnus-summary-process-mark-set (set)
6179   "Make SET into the current process marked articles."
6180   (gnus-summary-unmark-all-processable)
6181   (while set
6182     (gnus-summary-set-process-mark (pop set))))
6183
6184 ;;; Searching and stuff
6185
6186 (defun gnus-summary-search-group (&optional backward use-level)
6187   "Search for next unread newsgroup.
6188 If optional argument BACKWARD is non-nil, search backward instead."
6189   (save-excursion
6190     (set-buffer gnus-group-buffer)
6191     (when (gnus-group-search-forward
6192            backward nil (if use-level (gnus-group-group-level) nil))
6193       (gnus-group-group-name))))
6194
6195 (defun gnus-summary-best-group (&optional exclude-group)
6196   "Find the name of the best unread group.
6197 If EXCLUDE-GROUP, do not go to this group."
6198   (with-current-buffer gnus-group-buffer
6199     (save-excursion
6200       (gnus-group-best-unread-group exclude-group))))
6201
6202 (defun gnus-summary-find-next (&optional unread article backward)
6203   (if backward
6204       (gnus-summary-find-prev unread article)
6205     (let* ((dummy (gnus-summary-article-intangible-p))
6206            (article (or article (gnus-summary-article-number)))
6207            (data (gnus-data-find-list article))
6208            result)
6209       (when (and (not dummy)
6210                  (or (not gnus-summary-check-current)
6211                      (not unread)
6212                      (not (gnus-data-unread-p (car data)))))
6213         (setq data (cdr data)))
6214       (when (setq result
6215                   (if unread
6216                       (progn
6217                         (while data
6218                           (unless (memq (gnus-data-number (car data))
6219                                         (cond
6220                                          ((eq gnus-auto-goto-ignores
6221                                               'always-undownloaded)
6222                                           gnus-newsgroup-undownloaded)
6223                                          (gnus-plugged
6224                                           nil)
6225                                          ((eq gnus-auto-goto-ignores
6226                                               'unfetched)
6227                                           gnus-newsgroup-unfetched)
6228                                          ((eq gnus-auto-goto-ignores
6229                                               'undownloaded)
6230                                           gnus-newsgroup-undownloaded)))
6231                             (when (gnus-data-unread-p (car data))
6232                               (setq result (car data)
6233                                     data nil)))
6234                           (setq data (cdr data)))
6235                         result)
6236                     (car data)))
6237         (goto-char (gnus-data-pos result))
6238         (gnus-data-number result)))))
6239
6240 (defun gnus-summary-find-prev (&optional unread article)
6241   (let* ((eobp (eobp))
6242          (article (or article (gnus-summary-article-number)))
6243          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6244          result)
6245     (when (and (not eobp)
6246                (or (not gnus-summary-check-current)
6247                    (not unread)
6248                    (not (gnus-data-unread-p (car data)))))
6249       (setq data (cdr data)))
6250     (when (setq result
6251                 (if unread
6252                     (progn
6253                       (while data
6254                         (unless (memq (gnus-data-number (car data))
6255                                       (cond
6256                                        ((eq gnus-auto-goto-ignores
6257                                             'always-undownloaded)
6258                                         gnus-newsgroup-undownloaded)
6259                                        (gnus-plugged
6260                                         nil)
6261                                        ((eq gnus-auto-goto-ignores
6262                                             'unfetched)
6263                                         gnus-newsgroup-unfetched)
6264                                        ((eq gnus-auto-goto-ignores
6265                                             'undownloaded)
6266                                         gnus-newsgroup-undownloaded)))
6267                           (when (gnus-data-unread-p (car data))
6268                             (setq result (car data)
6269                                   data nil)))
6270                         (setq data (cdr data)))
6271                       result)
6272                   (car data)))
6273       (goto-char (gnus-data-pos result))
6274       (gnus-data-number result))))
6275
6276 (defun gnus-summary-find-subject (subject &optional unread backward article)
6277   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6278          (article (or article (gnus-summary-article-number)))
6279          (articles (gnus-data-list backward))
6280          (arts (gnus-data-find-list article articles))
6281          result)
6282     (when (or (not gnus-summary-check-current)
6283               (not unread)
6284               (not (gnus-data-unread-p (car arts))))
6285       (setq arts (cdr arts)))
6286     (while arts
6287       (and (or (not unread)
6288                (gnus-data-unread-p (car arts)))
6289            (vectorp (gnus-data-header (car arts)))
6290            (gnus-subject-equal
6291             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6292            (setq result (car arts)
6293                  arts nil))
6294       (setq arts (cdr arts)))
6295     (and result
6296          (goto-char (gnus-data-pos result))
6297          (gnus-data-number result))))
6298
6299 (defun gnus-summary-search-forward (&optional unread subject backward)
6300   "Search forward for an article.
6301 If UNREAD, look for unread articles.  If SUBJECT, look for
6302 articles with that subject.  If BACKWARD, search backward instead."
6303   (cond (subject (gnus-summary-find-subject subject unread backward))
6304         (backward (gnus-summary-find-prev unread))
6305         (t (gnus-summary-find-next unread))))
6306
6307 (defun gnus-recenter (&optional n)
6308   "Center point in window and redisplay frame.
6309 Also do horizontal recentering."
6310   (interactive "P")
6311   (when (and gnus-auto-center-summary
6312              (not (eq gnus-auto-center-summary 'vertical)))
6313     (gnus-horizontal-recenter))
6314   (recenter n))
6315
6316 (defun gnus-summary-recenter ()
6317   "Center point in the summary window.
6318 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6319 displayed, no centering will be performed."
6320   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6321   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6322   (interactive)
6323   ;; The user has to want it.
6324   (when gnus-auto-center-summary
6325     (let* ((top (cond ((< (window-height) 4) 0)
6326                       ((< (window-height) 7) 1)
6327                       (t (if (numberp gnus-auto-center-summary)
6328                              gnus-auto-center-summary
6329                            (/ (1- (window-height)) 2)))))
6330            (height (1- (window-height)))
6331            (bottom (save-excursion (goto-char (point-max))
6332                                    (forward-line (- height))
6333                                    (point)))
6334            (window (get-buffer-window (current-buffer))))
6335       (when (get-buffer-window gnus-article-buffer)
6336         ;; Only do recentering when the article buffer is displayed,
6337         ;; Set the window start to either `bottom', which is the biggest
6338         ;; possible valid number, or the second line from the top,
6339         ;; whichever is the least.
6340         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6341           (if (> bottom top-pos)
6342               ;; Keep the second line from the top visible
6343               (set-window-start window top-pos t)
6344             ;; Try to keep the bottom line visible; if it's partially
6345             ;; obscured, either scroll one more line to make it fully
6346             ;; visible, or revert to using TOP-POS.
6347             (save-excursion
6348               (goto-char (point-max))
6349               (forward-line -1)
6350               (let ((last-line-start (point)))
6351                 (goto-char bottom)
6352                 (set-window-start window (point) t)
6353                 (when (not (pos-visible-in-window-p last-line-start window))
6354                   (forward-line 1)
6355                   (set-window-start window (min (point) top-pos) t)))))))
6356       ;; Do horizontal recentering while we're at it.
6357       (when (and (get-buffer-window (current-buffer) t)
6358                  (not (eq gnus-auto-center-summary 'vertical)))
6359         (let ((selected (selected-window)))
6360           (select-window (get-buffer-window (current-buffer) t))
6361           (gnus-summary-position-point)
6362           (gnus-horizontal-recenter)
6363           (select-window selected))))))
6364
6365 (defun gnus-summary-jump-to-group (newsgroup)
6366   "Move point to NEWSGROUP in group mode buffer."
6367   ;; Keep update point of group mode buffer if visible.
6368   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6369       (save-window-excursion
6370         ;; Take care of tree window mode.
6371         (when (get-buffer-window gnus-group-buffer)
6372           (pop-to-buffer gnus-group-buffer))
6373         (gnus-group-jump-to-group newsgroup))
6374     (save-excursion
6375       ;; Take care of tree window mode.
6376       (if (get-buffer-window gnus-group-buffer)
6377           (pop-to-buffer gnus-group-buffer)
6378         (set-buffer gnus-group-buffer))
6379       (gnus-group-jump-to-group newsgroup))))
6380
6381 ;; This function returns a list of article numbers based on the
6382 ;; difference between the ranges of read articles in this group and
6383 ;; the range of active articles.
6384 (defun gnus-list-of-unread-articles (group)
6385   (let* ((read (gnus-info-read (gnus-get-info group)))
6386          (active (or (gnus-active group) (gnus-activate-group group)))
6387          (last (cdr active))
6388          first nlast unread)
6389     ;; If none are read, then all are unread.
6390     (if (not read)
6391         (setq first (car active))
6392       ;; If the range of read articles is a single range, then the
6393       ;; first unread article is the article after the last read
6394       ;; article.  Sounds logical, doesn't it?
6395       (if (and (not (listp (cdr read)))
6396                (or (< (car read) (car active))
6397                    (progn (setq read (list read))
6398                           nil)))
6399           (setq first (max (car active) (1+ (cdr read))))
6400         ;; `read' is a list of ranges.
6401         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6402                                   (caar read)))
6403                   1)
6404           (setq first (car active)))
6405         (while read
6406           (when first
6407             (while (< first nlast)
6408               (setq unread (cons first unread)
6409                     first (1+ first))))
6410           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6411           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6412           (setq read (cdr read)))))
6413     ;; And add the last unread articles.
6414     (while (<= first last)
6415       (setq unread (cons first unread)
6416             first (1+ first)))
6417     ;; Return the list of unread articles.
6418     (delq 0 (nreverse unread))))
6419
6420 (defun gnus-list-of-read-articles (group)
6421   "Return a list of unread, unticked and non-dormant articles."
6422   (let* ((info (gnus-get-info group))
6423          (marked (gnus-info-marks info))
6424          (active (gnus-active group)))
6425     (and info active
6426          (gnus-list-range-difference
6427           (gnus-list-range-difference
6428            (gnus-sorted-complement
6429             (gnus-uncompress-range active)
6430             (gnus-list-of-unread-articles group))
6431            (cdr (assq 'dormant marked)))
6432           (cdr (assq 'tick marked))))))
6433
6434 ;; This function returns a sequence of article numbers based on the
6435 ;; difference between the ranges of read articles in this group and
6436 ;; the range of active articles.
6437 (defun gnus-sequence-of-unread-articles (group)
6438   (let* ((read (gnus-info-read (gnus-get-info group)))
6439          (active (or (gnus-active group) (gnus-activate-group group)))
6440          (last (cdr active))
6441          first nlast unread)
6442     ;; If none are read, then all are unread.
6443     (if (not read)
6444         (setq first (car active))
6445       ;; If the range of read articles is a single range, then the
6446       ;; first unread article is the article after the last read
6447       ;; article.  Sounds logical, doesn't it?
6448       (if (and (not (listp (cdr read)))
6449                (or (< (car read) (car active))
6450                    (progn (setq read (list read))
6451                           nil)))
6452           (setq first (max (car active) (1+ (cdr read))))
6453         ;; `read' is a list of ranges.
6454         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6455                                   (caar read)))
6456                   1)
6457           (setq first (car active)))
6458         (while read
6459           (when first
6460             (push (cons first nlast) unread))
6461           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6462           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6463           (setq read (cdr read)))))
6464     ;; And add the last unread articles.
6465     (cond ((< first last)
6466            (push (cons first last) unread))
6467           ((= first last)
6468            (push first unread)))
6469     ;; Return the sequence of unread articles.
6470     (delq 0 (nreverse unread))))
6471
6472 ;; Various summary commands
6473
6474 (defun gnus-summary-select-article-buffer ()
6475   "Reconfigure windows to show article buffer."
6476   (interactive)
6477   (if (not (gnus-buffer-live-p gnus-article-buffer))
6478       (error "There is no article buffer for this summary buffer")
6479     (gnus-configure-windows 'article)
6480     (select-window (get-buffer-window gnus-article-buffer))))
6481
6482 (defun gnus-summary-universal-argument (arg)
6483   "Perform any operation on all articles that are process/prefixed."
6484   (interactive "P")
6485   (let ((articles (gnus-summary-work-articles arg))
6486         func article)
6487     (if (eq
6488          (setq
6489           func
6490           (key-binding
6491            (read-key-sequence
6492             (substitute-command-keys
6493              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6494          'undefined)
6495         (gnus-error 1 "Undefined key")
6496       (save-excursion
6497         (while articles
6498           (gnus-summary-goto-subject (setq article (pop articles)))
6499           (let (gnus-newsgroup-processable)
6500             (command-execute func))
6501           (gnus-summary-remove-process-mark article)))))
6502   (gnus-summary-position-point))
6503
6504 (defun gnus-summary-toggle-truncation (&optional arg)
6505   "Toggle truncation of summary lines.
6506 With ARG, turn line truncation on if ARG is positive."
6507   (interactive "P")
6508   (setq truncate-lines
6509         (if (null arg) (not truncate-lines)
6510           (> (prefix-numeric-value arg) 0)))
6511   (redraw-display))
6512
6513 (defun gnus-summary-find-for-reselect ()
6514   "Return the number of an article to stay on across a reselect.
6515 The current article is considered, then following articles, then previous
6516 articles.  An article is sought which is not cancelled and isn't a temporary
6517 insertion from another group.  If there's no such then return a dummy 0."
6518   (let (found)
6519     (dolist (rev '(nil t))
6520       (unless found      ; don't demand the reverse list if we don't need it
6521         (let ((data (gnus-data-find-list
6522                      (gnus-summary-article-number) (gnus-data-list rev))))
6523           (while (and data (not found))
6524             (if (and (< 0 (gnus-data-number (car data)))
6525                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6526                 (setq found (gnus-data-number (car data))))
6527             (setq data (cdr data))))))
6528     (or found 0)))
6529
6530 (defun gnus-summary-reselect-current-group (&optional all rescan)
6531   "Exit and then reselect the current newsgroup.
6532 The prefix argument ALL means to select all articles."
6533   (interactive "P")
6534   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6535     (error "Ephemeral groups can't be reselected"))
6536   (let ((current-subject (gnus-summary-find-for-reselect))
6537         (group gnus-newsgroup-name))
6538     (setq gnus-newsgroup-begin nil)
6539     (gnus-summary-exit nil 'leave-hidden)
6540     ;; We have to adjust the point of group mode buffer because
6541     ;; point was moved to the next unread newsgroup by exiting.
6542     (gnus-summary-jump-to-group group)
6543     (when rescan
6544       (save-excursion
6545         (gnus-group-get-new-news-this-group 1)))
6546     (gnus-group-read-group all t)
6547     (gnus-summary-goto-subject current-subject nil t)))
6548
6549 (defun gnus-summary-rescan-group (&optional all)
6550   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6551   (interactive "P")
6552   (gnus-summary-reselect-current-group all t))
6553
6554 (defun gnus-summary-update-info (&optional non-destructive)
6555   (save-excursion
6556     (let ((group gnus-newsgroup-name))
6557       (when group
6558         (when gnus-newsgroup-kill-headers
6559           (setq gnus-newsgroup-killed
6560                 (gnus-compress-sequence
6561                  (gnus-sorted-union
6562                   (gnus-list-range-intersection
6563                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6564                   gnus-newsgroup-unreads)
6565                  t)))
6566         (unless (listp (cdr gnus-newsgroup-killed))
6567           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6568         (let ((headers gnus-newsgroup-headers))
6569           ;; Set the new ranges of read articles.
6570           (with-current-buffer gnus-group-buffer
6571             (gnus-undo-force-boundary))
6572           (gnus-update-read-articles
6573            group (gnus-sorted-union
6574                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6575           ;; Set the current article marks.
6576           (let ((gnus-newsgroup-scored
6577                  (if (and (not gnus-save-score)
6578                           (not non-destructive))
6579                      nil
6580                    gnus-newsgroup-scored)))
6581             (save-excursion
6582               (gnus-update-marks)))
6583           ;; Do the cross-ref thing.
6584           (when gnus-use-cross-reference
6585             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6586           ;; Do not switch windows but change the buffer to work.
6587           (set-buffer gnus-group-buffer)
6588           (unless (gnus-ephemeral-group-p group)
6589             (gnus-group-update-group group)))))))
6590
6591 (defun gnus-summary-save-newsrc (&optional force)
6592   "Save the current number of read/marked articles in the dribble buffer.
6593 The dribble buffer will then be saved.
6594 If FORCE (the prefix), also save the .newsrc file(s)."
6595   (interactive "P")
6596   (gnus-summary-update-info t)
6597   (if force
6598       (gnus-save-newsrc-file)
6599     (gnus-dribble-save)))
6600
6601 (defun gnus-summary-exit (&optional temporary leave-hidden)
6602   "Exit reading current newsgroup, and then return to group selection mode.
6603 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6604   (interactive)
6605   (gnus-set-global-variables)
6606   (when (gnus-buffer-live-p gnus-article-buffer)
6607     (save-excursion
6608       (set-buffer gnus-article-buffer)
6609       (mm-destroy-parts gnus-article-mime-handles)
6610       ;; Set it to nil for safety reason.
6611       (setq gnus-article-mime-handle-alist nil)
6612       (setq gnus-article-mime-handles nil)))
6613   (gnus-kill-save-kill-buffer)
6614   (gnus-async-halt-prefetch)
6615   (let* ((group gnus-newsgroup-name)
6616          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6617          (gnus-group-is-exiting-p t)
6618          (mode major-mode)
6619          (group-point nil)
6620          (buf (current-buffer)))
6621     (unless quit-config
6622       ;; Do adaptive scoring, and possibly save score files.
6623       (when gnus-newsgroup-adaptive
6624         (gnus-score-adaptive))
6625       (when gnus-use-scoring
6626         (gnus-score-save)))
6627     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6628     ;; If we have several article buffers, we kill them at exit.
6629     (unless gnus-single-article-buffer
6630       (gnus-kill-buffer gnus-original-article-buffer)
6631       (setq gnus-article-current nil))
6632     (when gnus-use-cache
6633       (gnus-cache-possibly-remove-articles)
6634       (gnus-cache-save-buffers))
6635     (gnus-async-prefetch-remove-group group)
6636     (when gnus-suppress-duplicates
6637       (gnus-dup-enter-articles))
6638     (when gnus-use-trees
6639       (gnus-tree-close group))
6640     (when gnus-use-cache
6641       (gnus-cache-write-active))
6642     ;; Remove entries for this group.
6643     (nnmail-purge-split-history (gnus-group-real-name group))
6644     ;; Make all changes in this group permanent.
6645     (unless quit-config
6646       (gnus-run-hooks 'gnus-exit-group-hook)
6647       (gnus-summary-update-info))
6648     (gnus-close-group group)
6649     ;; Make sure where we were, and go to next newsgroup.
6650     (set-buffer gnus-group-buffer)
6651     (unless quit-config
6652       (gnus-group-jump-to-group group))
6653     (gnus-run-hooks 'gnus-summary-exit-hook)
6654     (unless (or quit-config
6655                 ;; If this group has disappeared from the summary
6656                 ;; buffer, don't skip forwards.
6657                 (not (string= group (gnus-group-group-name))))
6658       (gnus-group-next-unread-group 1))
6659     (setq group-point (point))
6660     (if temporary
6661         nil                             ;Nothing to do.
6662       ;; If we have several article buffers, we kill them at exit.
6663       (unless gnus-single-article-buffer
6664         (gnus-kill-buffer gnus-article-buffer)
6665         (gnus-kill-buffer gnus-original-article-buffer)
6666         (setq gnus-article-current nil))
6667       (set-buffer buf)
6668       (if (not gnus-kill-summary-on-exit)
6669           (progn
6670             (gnus-deaden-summary)
6671             (setq mode nil))
6672         ;; We set all buffer-local variables to nil.  It is unclear why
6673         ;; this is needed, but if we don't, buffer-local variables are
6674         ;; not garbage-collected, it seems.  This would the lead to en
6675         ;; ever-growing Emacs.
6676         (gnus-summary-clear-local-variables)
6677         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6678           (gnus-summary-clear-local-variables))
6679         (when (get-buffer gnus-article-buffer)
6680           (bury-buffer gnus-article-buffer))
6681         ;; We clear the global counterparts of the buffer-local
6682         ;; variables as well, just to be on the safe side.
6683         (set-buffer gnus-group-buffer)
6684         (gnus-summary-clear-local-variables)
6685         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6686           (gnus-summary-clear-local-variables))
6687         ;; Return to group mode buffer.
6688         (when (eq mode 'gnus-summary-mode)
6689           (gnus-kill-buffer buf)))
6690       (setq gnus-current-select-method gnus-select-method)
6691       (if leave-hidden
6692           (set-buffer gnus-group-buffer)
6693         (pop-to-buffer gnus-group-buffer))
6694       (if (not quit-config)
6695           (progn
6696             (goto-char group-point)
6697             (unless leave-hidden
6698               (gnus-configure-windows 'group 'force)))
6699         (gnus-handle-ephemeral-exit quit-config))
6700       ;; Clear the current group name.
6701       (unless quit-config
6702         (setq gnus-newsgroup-name nil)))))
6703
6704 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6705 (defun gnus-summary-exit-no-update (&optional no-questions)
6706   "Quit reading current newsgroup without updating read article info."
6707   (interactive)
6708   (let* ((group gnus-newsgroup-name)
6709          (gnus-group-is-exiting-p t)
6710          (gnus-group-is-exiting-without-update-p t)
6711          (quit-config (gnus-group-quit-config group)))
6712     (when (or no-questions
6713               gnus-expert-user
6714               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6715       (gnus-async-halt-prefetch)
6716       (run-hooks 'gnus-summary-prepare-exit-hook)
6717       (when (gnus-buffer-live-p gnus-article-buffer)
6718         (save-excursion
6719           (set-buffer gnus-article-buffer)
6720           (mm-destroy-parts gnus-article-mime-handles)
6721           ;; Set it to nil for safety reason.
6722           (setq gnus-article-mime-handle-alist nil)
6723           (setq gnus-article-mime-handles nil)))
6724       ;; If we have several article buffers, we kill them at exit.
6725       (unless gnus-single-article-buffer
6726         (gnus-kill-buffer gnus-article-buffer)
6727         (gnus-kill-buffer gnus-original-article-buffer)
6728         (setq gnus-article-current nil))
6729       (if (not gnus-kill-summary-on-exit)
6730           (gnus-deaden-summary)
6731         (gnus-close-group group)
6732         (gnus-summary-clear-local-variables)
6733         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6734           (gnus-summary-clear-local-variables))
6735         (set-buffer gnus-group-buffer)
6736         (gnus-summary-clear-local-variables)
6737         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6738           (gnus-summary-clear-local-variables))
6739         (gnus-kill-buffer gnus-summary-buffer))
6740       (unless gnus-single-article-buffer
6741         (setq gnus-article-current nil))
6742       (when gnus-use-trees
6743         (gnus-tree-close group))
6744       (gnus-async-prefetch-remove-group group)
6745       (when (get-buffer gnus-article-buffer)
6746         (bury-buffer gnus-article-buffer))
6747       ;; Return to the group buffer.
6748       (gnus-configure-windows 'group 'force)
6749       ;; Clear the current group name.
6750       (setq gnus-newsgroup-name nil)
6751       (unless (gnus-ephemeral-group-p group)
6752         (gnus-group-update-group group))
6753       (when (equal (gnus-group-group-name) group)
6754         (gnus-group-next-unread-group 1))
6755       (when quit-config
6756         (gnus-handle-ephemeral-exit quit-config)))))
6757
6758 (defun gnus-handle-ephemeral-exit (quit-config)
6759   "Handle movement when leaving an ephemeral group.
6760 The state which existed when entering the ephemeral is reset."
6761   (if (not (buffer-name (car quit-config)))
6762       (gnus-configure-windows 'group 'force)
6763     (set-buffer (car quit-config))
6764     (cond ((eq major-mode 'gnus-summary-mode)
6765            (gnus-set-global-variables))
6766           ((eq major-mode 'gnus-article-mode)
6767            (save-excursion
6768              ;; The `gnus-summary-buffer' variable may point
6769              ;; to the old summary buffer when using a single
6770              ;; article buffer.
6771              (unless (gnus-buffer-live-p gnus-summary-buffer)
6772                (set-buffer gnus-group-buffer))
6773              (set-buffer gnus-summary-buffer)
6774              (gnus-set-global-variables))))
6775     (if (or (eq (cdr quit-config) 'article)
6776             (eq (cdr quit-config) 'pick))
6777         (progn
6778           ;; The current article may be from the ephemeral group
6779           ;; thus it is best that we reload this article
6780           ;;
6781           ;; If we're exiting from a large digest, this can be
6782           ;; extremely slow.  So, it's better not to reload it. -- jh.
6783           ;;(gnus-summary-show-article)
6784           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6785               (gnus-configure-windows 'pick 'force)
6786             (gnus-configure-windows (cdr quit-config) 'force)))
6787       (gnus-configure-windows (cdr quit-config) 'force))
6788     (when (eq major-mode 'gnus-summary-mode)
6789       (gnus-summary-next-subject 1 nil t)
6790       (gnus-summary-recenter)
6791       (gnus-summary-position-point))))
6792
6793 ;;; Dead summaries.
6794
6795 (defvar gnus-dead-summary-mode-map nil)
6796
6797 (unless gnus-dead-summary-mode-map
6798   (setq gnus-dead-summary-mode-map (make-keymap))
6799   (suppress-keymap gnus-dead-summary-mode-map)
6800   (substitute-key-definition
6801    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6802   (dolist (key '("\C-d" "\r" "\177" [delete]))
6803     (define-key gnus-dead-summary-mode-map
6804       key 'gnus-summary-wake-up-the-dead))
6805   (dolist (key '("q" "Q"))
6806     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6807
6808 (defvar gnus-dead-summary-mode nil
6809   "Minor mode for Gnus summary buffers.")
6810
6811 (defun gnus-dead-summary-mode (&optional arg)
6812   "Minor mode for Gnus summary buffers."
6813   (interactive "P")
6814   (when (eq major-mode 'gnus-summary-mode)
6815     (make-local-variable 'gnus-dead-summary-mode)
6816     (setq gnus-dead-summary-mode
6817           (if (null arg) (not gnus-dead-summary-mode)
6818             (> (prefix-numeric-value arg) 0)))
6819     (when gnus-dead-summary-mode
6820       (add-minor-mode
6821        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6822
6823 (defun gnus-deaden-summary ()
6824   "Make the current summary buffer into a dead summary buffer."
6825   ;; Kill any previous dead summary buffer.
6826   (when (and gnus-dead-summary
6827              (buffer-name gnus-dead-summary))
6828     (with-current-buffer gnus-dead-summary
6829       (when gnus-dead-summary-mode
6830         (kill-buffer (current-buffer)))))
6831   ;; Make this the current dead summary.
6832   (setq gnus-dead-summary (current-buffer))
6833   (gnus-dead-summary-mode 1)
6834   (let ((name (buffer-name)))
6835     (when (string-match "Summary" name)
6836       (rename-buffer
6837        (concat (substring name 0 (match-beginning 0)) "Dead "
6838                (substring name (match-beginning 0)))
6839        t)
6840       (bury-buffer))))
6841
6842 (defun gnus-kill-or-deaden-summary (buffer)
6843   "Kill or deaden the summary BUFFER."
6844   (save-excursion
6845     (when (and (buffer-name buffer)
6846                (not gnus-single-article-buffer))
6847       (with-current-buffer buffer
6848         (gnus-kill-buffer gnus-article-buffer)
6849         (gnus-kill-buffer gnus-original-article-buffer)))
6850     (cond
6851      ;; Kill the buffer.
6852      (gnus-kill-summary-on-exit
6853       (when (and gnus-use-trees
6854                  (gnus-buffer-exists-p buffer))
6855         (save-excursion
6856           (set-buffer buffer)
6857           (gnus-tree-close gnus-newsgroup-name)))
6858       (gnus-kill-buffer buffer))
6859      ;; Deaden the buffer.
6860      ((gnus-buffer-exists-p buffer)
6861       (save-excursion
6862         (set-buffer buffer)
6863         (gnus-deaden-summary))))))
6864
6865 (defun gnus-summary-wake-up-the-dead (&rest args)
6866   "Wake up the dead summary buffer."
6867   (interactive)
6868   (gnus-dead-summary-mode -1)
6869   (let ((name (buffer-name)))
6870     (when (string-match "Dead " name)
6871       (rename-buffer
6872        (concat (substring name 0 (match-beginning 0))
6873                (substring name (match-end 0)))
6874        t)))
6875   (gnus-message 3 "This dead summary is now alive again"))
6876
6877 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6878 (defun gnus-summary-fetch-faq (&optional faq-dir)
6879   "Fetch the FAQ for the current group.
6880 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6881 in."
6882   (interactive
6883    (list
6884     (when current-prefix-arg
6885       (completing-read
6886        "FAQ dir: " (and (listp gnus-group-faq-directory)
6887                         (mapcar 'list
6888                                 gnus-group-faq-directory))))))
6889   (let (gnus-faq-buffer)
6890     (when (setq gnus-faq-buffer
6891                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6892       (gnus-configure-windows 'summary-faq))))
6893
6894 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6895 (defun gnus-summary-describe-group (&optional force)
6896   "Describe the current newsgroup."
6897   (interactive "P")
6898   (gnus-group-describe-group force gnus-newsgroup-name))
6899
6900 (defun gnus-summary-describe-briefly ()
6901   "Describe summary mode commands briefly."
6902   (interactive)
6903   (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")))
6904
6905 ;; Walking around group mode buffer from summary mode.
6906
6907 (defun gnus-summary-next-group (&optional no-article target-group backward)
6908   "Exit current newsgroup and then select next unread newsgroup.
6909 If prefix argument NO-ARTICLE is non-nil, no article is selected
6910 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6911 previous group instead."
6912   (interactive "P")
6913   ;; Stop pre-fetching.
6914   (gnus-async-halt-prefetch)
6915   (let ((current-group gnus-newsgroup-name)
6916         (current-buffer (current-buffer))
6917         entered)
6918     ;; First we semi-exit this group to update Xrefs and all variables.
6919     ;; We can't do a real exit, because the window conf must remain
6920     ;; the same in case the user is prompted for info, and we don't
6921     ;; want the window conf to change before that...
6922     (gnus-summary-exit t)
6923     (while (not entered)
6924       ;; Then we find what group we are supposed to enter.
6925       (set-buffer gnus-group-buffer)
6926       (gnus-group-jump-to-group current-group)
6927       (setq target-group
6928             (or target-group
6929                 (if (eq gnus-keep-same-level 'best)
6930                     (gnus-summary-best-group gnus-newsgroup-name)
6931                   (gnus-summary-search-group backward gnus-keep-same-level))))
6932       (if (not target-group)
6933           ;; There are no further groups, so we return to the group
6934           ;; buffer.
6935           (progn
6936             (gnus-message 5 "Returning to the group buffer")
6937             (setq entered t)
6938             (when (gnus-buffer-live-p current-buffer)
6939               (set-buffer current-buffer)
6940               (gnus-summary-exit))
6941             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6942         ;; We try to enter the target group.
6943         (gnus-group-jump-to-group target-group)
6944         (let ((unreads (gnus-group-group-unread)))
6945           (if (and (or (eq t unreads)
6946                        (and unreads (not (zerop unreads))))
6947                    (gnus-summary-read-group
6948                     target-group nil no-article
6949                     (and (buffer-name current-buffer) current-buffer)
6950                     nil backward))
6951               (setq entered t)
6952             (setq current-group target-group
6953                   target-group nil)))))))
6954
6955 (defun gnus-summary-prev-group (&optional no-article)
6956   "Exit current newsgroup and then select previous unread newsgroup.
6957 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6958   (interactive "P")
6959   (gnus-summary-next-group no-article nil t))
6960
6961 ;; Walking around summary lines.
6962
6963 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6964   "Go to the first subject satisfying any non-nil constraint.
6965 If UNREAD is non-nil, the article should be unread.
6966 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6967 If UNSEEN is non-nil, the article should be unseen.
6968 Returns the article selected or nil if there are no matching articles."
6969   (interactive "P")
6970   (cond
6971    ;; Empty summary.
6972    ((null gnus-newsgroup-data)
6973     (gnus-message 3 "No articles in the group")
6974     nil)
6975    ;; Pick the first article.
6976    ((not (or unread undownloaded unseen))
6977     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6978     (gnus-data-number (car gnus-newsgroup-data)))
6979    ;; Find the first unread article.
6980    (t
6981     (let ((data gnus-newsgroup-data))
6982       (while (and data
6983                   (let ((num (gnus-data-number (car data))))
6984                     (or (memq num gnus-newsgroup-unfetched)
6985                         (not (or (and unread
6986                                       (memq num gnus-newsgroup-unreads))
6987                                  (and undownloaded
6988                                       (memq num gnus-newsgroup-undownloaded))
6989                                  (and unseen
6990                                       (memq num gnus-newsgroup-unseen)))))))
6991         (setq data (cdr data)))
6992       (prog1
6993           (if data
6994               (progn
6995                 (goto-char (gnus-data-pos (car data)))
6996                 (gnus-data-number (car data)))
6997             (gnus-message 3 "No more%s articles"
6998                           (let* ((r (when unread " unread"))
6999                                  (d (when undownloaded " undownloaded"))
7000                                  (s (when unseen " unseen"))
7001                                  (l (delq nil (list r d s))))
7002                             (cond ((= 3 (length l))
7003                                    (concat r "," d ", or" s))
7004                                   ((= 2 (length l))
7005                                    (concat (car l) ", or" (cadr l)))
7006                                   ((= 1 (length l))
7007                                    (car l))
7008                                   (t
7009                                    ""))))
7010             nil
7011             )
7012         (gnus-summary-position-point))))))
7013
7014 (defun gnus-summary-next-subject (n &optional unread dont-display)
7015   "Go to next N'th summary line.
7016 If N is negative, go to the previous N'th subject line.
7017 If UNREAD is non-nil, only unread articles are selected.
7018 The difference between N and the actual number of steps taken is
7019 returned."
7020   (interactive "p")
7021   (let ((backward (< n 0))
7022         (n (abs n)))
7023     (while (and (> n 0)
7024                 (if backward
7025                     (gnus-summary-find-prev unread)
7026                   (gnus-summary-find-next unread)))
7027       (unless (zerop (setq n (1- n)))
7028         (gnus-summary-show-thread)))
7029     (when (/= 0 n)
7030       (gnus-message 7 "No more%s articles"
7031                     (if unread " unread" "")))
7032     (unless dont-display
7033       (gnus-summary-recenter)
7034       (gnus-summary-position-point))
7035     n))
7036
7037 (defun gnus-summary-next-unread-subject (n)
7038   "Go to next N'th unread summary line."
7039   (interactive "p")
7040   (gnus-summary-next-subject n t))
7041
7042 (defun gnus-summary-prev-subject (n &optional unread)
7043   "Go to previous N'th summary line.
7044 If optional argument UNREAD is non-nil, only unread article is selected."
7045   (interactive "p")
7046   (gnus-summary-next-subject (- n) unread))
7047
7048 (defun gnus-summary-prev-unread-subject (n)
7049   "Go to previous N'th unread summary line."
7050   (interactive "p")
7051   (gnus-summary-next-subject (- n) t))
7052
7053 (defun gnus-summary-goto-subjects (articles)
7054   "Insert the subject header for ARTICLES in the current buffer."
7055   (save-excursion
7056     (dolist (article articles)
7057       (gnus-summary-goto-subject article t)))
7058   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7059   (gnus-summary-position-point))
7060  
7061 (defun gnus-summary-goto-subject (article &optional force silent)
7062   "Go the subject line of ARTICLE.
7063 If FORCE, also allow jumping to articles not currently shown."
7064   (interactive "nArticle number: ")
7065   (unless (numberp article)
7066     (error "Article %s is not a number" article))
7067   (let ((b (point))
7068         (data (gnus-data-find article)))
7069     ;; We read in the article if we have to.
7070     (and (not data)
7071          force
7072          (gnus-summary-insert-subject
7073           article
7074           (if (or (numberp force) (vectorp force)) force)
7075           t)
7076          (setq data (gnus-data-find article)))
7077     (goto-char b)
7078     (if (not data)
7079         (progn
7080           (unless silent
7081             (gnus-message 3 "Can't find article %d" article))
7082           nil)
7083       (let ((pt (gnus-data-pos data)))
7084         (goto-char pt)
7085         (gnus-summary-set-article-display-arrow pt))
7086       (gnus-summary-position-point)
7087       article)))
7088
7089 ;; Walking around summary lines with displaying articles.
7090
7091 (defun gnus-summary-expand-window (&optional arg)
7092   "Make the summary buffer take up the entire Emacs frame.
7093 Given a prefix, will force an `article' buffer configuration."
7094   (interactive "P")
7095   (if arg
7096       (gnus-configure-windows 'article 'force)
7097     (gnus-configure-windows 'summary 'force)))
7098
7099 (defun gnus-summary-display-article (article &optional all-header)
7100   "Display ARTICLE in article buffer."
7101   (when (gnus-buffer-live-p gnus-article-buffer)
7102     (with-current-buffer gnus-article-buffer
7103       (mm-enable-multibyte)))
7104   (gnus-set-global-variables)
7105   (when (gnus-buffer-live-p gnus-article-buffer)
7106     (with-current-buffer gnus-article-buffer
7107       (setq gnus-article-charset gnus-newsgroup-charset)
7108       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7109       (mm-enable-multibyte)))
7110   (if (null article)
7111       nil
7112     (prog1
7113         (if gnus-summary-display-article-function
7114             (funcall gnus-summary-display-article-function article all-header)
7115           (gnus-article-prepare article all-header))
7116       (gnus-run-hooks 'gnus-select-article-hook)
7117       (when (and gnus-current-article
7118                  (not (zerop gnus-current-article)))
7119         (gnus-summary-goto-subject gnus-current-article))
7120       (gnus-summary-recenter)
7121       (when (and gnus-use-trees gnus-show-threads)
7122         (gnus-possibly-generate-tree article)
7123         (gnus-highlight-selected-tree article))
7124       ;; Successfully display article.
7125       (gnus-article-set-window-start
7126        (cdr (assq article gnus-newsgroup-bookmarks))))))
7127
7128 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7129   "Select the current article.
7130 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7131 non-nil, the article will be re-fetched even if it already present in
7132 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7133 be displayed."
7134   ;; Make sure we are in the summary buffer to work around bbdb bug.
7135   (unless (eq major-mode 'gnus-summary-mode)
7136     (set-buffer gnus-summary-buffer))
7137   (let ((article (or article (gnus-summary-article-number)))
7138         (all-headers (not (not all-headers))) ;Must be t or nil.
7139         gnus-summary-display-article-function)
7140     (and (not pseudo)
7141          (gnus-summary-article-pseudo-p article)
7142          (error "This is a pseudo-article"))
7143     (save-excursion
7144       (set-buffer gnus-summary-buffer)
7145       (if (or (and gnus-single-article-buffer
7146                    (or (null gnus-current-article)
7147                        (null gnus-article-current)
7148                        (null (get-buffer gnus-article-buffer))
7149                        (not (eq article (cdr gnus-article-current)))
7150                        (not (equal (car gnus-article-current)
7151                                    gnus-newsgroup-name))))
7152               (and (not gnus-single-article-buffer)
7153                    (or (null gnus-current-article)
7154                        (not (eq gnus-current-article article))))
7155               force)
7156           ;; The requested article is different from the current article.
7157           (progn
7158             (gnus-summary-display-article article all-headers)
7159             (when (gnus-buffer-live-p gnus-article-buffer)
7160               (with-current-buffer gnus-article-buffer
7161                 (if (not gnus-article-decoded-p) ;; a local variable
7162                     (mm-disable-multibyte))))
7163             (gnus-article-set-window-start
7164              (cdr (assq article gnus-newsgroup-bookmarks)))
7165             article)
7166         'old))))
7167
7168 (defun gnus-summary-force-verify-and-decrypt ()
7169   "Display buttons for signed/encrypted parts and verify/decrypt them."
7170   (interactive)
7171   (let ((mm-verify-option 'known)
7172         (mm-decrypt-option 'known)
7173         (gnus-article-emulate-mime t)
7174         (mm-fill-flowed nil)
7175         (gnus-buttonized-mime-types (append (list "multipart/signed"
7176                                                   "multipart/encrypted")
7177                                             gnus-buttonized-mime-types)))
7178     (gnus-summary-select-article nil 'force)))
7179
7180 (defun gnus-summary-set-current-mark (&optional current-mark)
7181   "Obsolete function."
7182   nil)
7183
7184 (defun gnus-summary-next-article (&optional unread subject backward push)
7185   "Select the next article.
7186 If UNREAD, only unread articles are selected.
7187 If SUBJECT, only articles with SUBJECT are selected.
7188 If BACKWARD, the previous article is selected instead of the next."
7189   (interactive "P")
7190   (cond
7191    ;; Is there such an article?
7192    ((and (gnus-summary-search-forward unread subject backward)
7193          (or (gnus-summary-display-article (gnus-summary-article-number))
7194              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7195     (gnus-summary-position-point))
7196    ;; If not, we try the first unread, if that is wanted.
7197    ((and subject
7198          gnus-auto-select-same
7199          (gnus-summary-first-unread-article))
7200     (gnus-summary-position-point)
7201     (gnus-message 6 "Wrapped"))
7202    ;; Try to get next/previous article not displayed in this group.
7203    ((and gnus-auto-extend-newsgroup
7204          (not unread) (not subject))
7205     (gnus-summary-goto-article
7206      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7207      nil (count-lines (point-min) (point))))
7208    ;; Go to next/previous group.
7209    (t
7210     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7211       (gnus-summary-jump-to-group gnus-newsgroup-name))
7212     (let ((cmd last-command-char)
7213           (point
7214            (with-current-buffer gnus-group-buffer
7215              (point)))
7216           (group
7217            (if (eq gnus-keep-same-level 'best)
7218                (gnus-summary-best-group gnus-newsgroup-name)
7219              (gnus-summary-search-group backward gnus-keep-same-level))))
7220       ;; For some reason, the group window gets selected.  We change
7221       ;; it back.
7222       (select-window (get-buffer-window (current-buffer)))
7223       ;; Select next unread newsgroup automagically.
7224       (cond
7225        ((or (not gnus-auto-select-next)
7226             (not cmd))
7227         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7228        ((or (eq gnus-auto-select-next 'quietly)
7229             (and (eq gnus-auto-select-next 'slightly-quietly)
7230                  push)
7231             (and (eq gnus-auto-select-next 'almost-quietly)
7232                  (gnus-summary-last-article-p)))
7233         ;; Select quietly.
7234         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7235             (gnus-summary-exit)
7236           (gnus-message 7 "No more%s articles (%s)..."
7237                         (if unread " unread" "")
7238                         (if group (concat "selecting " group)
7239                           "exiting"))
7240           (gnus-summary-next-group nil group backward)))
7241        (t
7242         (when (gnus-key-press-event-p last-input-event)
7243           (gnus-summary-walk-group-buffer
7244            gnus-newsgroup-name cmd unread backward point))))))))
7245
7246 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7247   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7248                       (?\C-p (gnus-group-prev-unread-group 1))))
7249         (cursor-in-echo-area t)
7250         keve key group ended prompt)
7251     (save-excursion
7252       (set-buffer gnus-group-buffer)
7253       (goto-char start)
7254       (setq group
7255             (if (eq gnus-keep-same-level 'best)
7256                 (gnus-summary-best-group gnus-newsgroup-name)
7257               (gnus-summary-search-group backward gnus-keep-same-level))))
7258     (while (not ended)
7259       (setq prompt
7260             (format
7261              "No more%s articles%s " (if unread " unread" "")
7262              (if (and group
7263                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7264                  (format " (Type %s for %s [%s])"
7265                          (single-key-description cmd) group
7266                          (gnus-group-unread group))
7267                (format " (Type %s to exit %s)"
7268                        (single-key-description cmd)
7269                        gnus-newsgroup-name))))
7270       ;; Confirm auto selection.
7271       (setq key (car (setq keve (gnus-read-event-char prompt)))
7272             ended t)
7273       (cond
7274        ((assq key keystrokes)
7275         (let ((obuf (current-buffer)))
7276           (switch-to-buffer gnus-group-buffer)
7277           (when group
7278             (gnus-group-jump-to-group group))
7279           (eval (cadr (assq key keystrokes)))
7280           (setq group (gnus-group-group-name))
7281           (switch-to-buffer obuf))
7282         (setq ended nil))
7283        ((equal key cmd)
7284         (if (or (not group)
7285                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7286             (gnus-summary-exit)
7287           (gnus-summary-next-group nil group backward)))
7288        (t
7289         (push (cdr keve) unread-command-events))))))
7290
7291 (defun gnus-summary-next-unread-article ()
7292   "Select unread article after current one."
7293   (interactive)
7294   (gnus-summary-next-article
7295    (or (not (eq gnus-summary-goto-unread 'never))
7296        (gnus-summary-last-article-p (gnus-summary-article-number)))
7297    (and gnus-auto-select-same
7298         (gnus-summary-article-subject))))
7299
7300 (defun gnus-summary-prev-article (&optional unread subject)
7301   "Select the article after the current one.
7302 If UNREAD is non-nil, only unread articles are selected."
7303   (interactive "P")
7304   (gnus-summary-next-article unread subject t))
7305
7306 (defun gnus-summary-prev-unread-article ()
7307   "Select unread article before current one."
7308   (interactive)
7309   (gnus-summary-prev-article
7310    (or (not (eq gnus-summary-goto-unread 'never))
7311        (gnus-summary-first-article-p (gnus-summary-article-number)))
7312    (and gnus-auto-select-same
7313         (gnus-summary-article-subject))))
7314
7315 (defun gnus-summary-next-page (&optional lines circular stop)
7316   "Show next page of the selected article.
7317 If at the end of the current article, select the next article.
7318 LINES says how many lines should be scrolled up.
7319
7320 If CIRCULAR is non-nil, go to the start of the article instead of
7321 selecting the next article when reaching the end of the current
7322 article.
7323
7324 If STOP is non-nil, just stop when reaching the end of the message.
7325
7326 Also see the variable `gnus-article-skip-boring'."
7327   (interactive "P")
7328   (setq gnus-summary-buffer (current-buffer))
7329   (gnus-set-global-variables)
7330   (let ((article (gnus-summary-article-number))
7331         (article-window (get-buffer-window gnus-article-buffer t))
7332         endp)
7333     ;; If the buffer is empty, we have no article.
7334     (unless article
7335       (error "No article to select"))
7336     (gnus-configure-windows 'article)
7337     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7338         (if (and (eq gnus-summary-goto-unread 'never)
7339                  (not (gnus-summary-last-article-p article)))
7340             (gnus-summary-next-article)
7341           (gnus-summary-next-unread-article))
7342       (if (or (null gnus-current-article)
7343               (null gnus-article-current)
7344               (/= article (cdr gnus-article-current))
7345               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7346           ;; Selected subject is different from current article's.
7347           (gnus-summary-display-article article)
7348         (when article-window
7349           (gnus-eval-in-buffer-window gnus-article-buffer
7350             (setq endp (or (gnus-article-next-page lines)
7351                            (gnus-article-only-boring-p))))
7352           (when endp
7353             (cond (stop
7354                    (gnus-message 3 "End of message"))
7355                   (circular
7356                    (gnus-summary-beginning-of-article))
7357                   (lines
7358                    (gnus-message 3 "End of message"))
7359                   ((null lines)
7360                    (if (and (eq gnus-summary-goto-unread 'never)
7361                             (not (gnus-summary-last-article-p article)))
7362                        (gnus-summary-next-article)
7363                      (gnus-summary-next-unread-article))))))))
7364     (gnus-summary-recenter)
7365     (gnus-summary-position-point)))
7366
7367 (defun gnus-summary-prev-page (&optional lines move)
7368   "Show previous page of selected article.
7369 Argument LINES specifies lines to be scrolled down.
7370 If MOVE, move to the previous unread article if point is at
7371 the beginning of the buffer."
7372   (interactive "P")
7373   (let ((article (gnus-summary-article-number))
7374         (article-window (get-buffer-window gnus-article-buffer t))
7375         endp)
7376     (gnus-configure-windows 'article)
7377     (if (or (null gnus-current-article)
7378             (null gnus-article-current)
7379             (/= article (cdr gnus-article-current))
7380             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7381         ;; Selected subject is different from current article's.
7382         (gnus-summary-display-article article)
7383       (gnus-summary-recenter)
7384       (when article-window
7385         (gnus-eval-in-buffer-window gnus-article-buffer
7386           (setq endp (gnus-article-prev-page lines)))
7387         (when (and move endp)
7388           (cond (lines
7389                  (gnus-message 3 "Beginning of message"))
7390                 ((null lines)
7391                  (if (and (eq gnus-summary-goto-unread 'never)
7392                           (not (gnus-summary-first-article-p article)))
7393                      (gnus-summary-prev-article)
7394                    (gnus-summary-prev-unread-article))))))))
7395   (gnus-summary-position-point))
7396
7397 (defun gnus-summary-prev-page-or-article (&optional lines)
7398   "Show previous page of selected article.
7399 Argument LINES specifies lines to be scrolled down.
7400 If at the beginning of the article, go to the next article."
7401   (interactive "P")
7402   (gnus-summary-prev-page lines t))
7403
7404 (defun gnus-summary-scroll-up (lines)
7405   "Scroll up (or down) one line current article.
7406 Argument LINES specifies lines to be scrolled up (or down if negative)."
7407   (interactive "p")
7408   (gnus-configure-windows 'article)
7409   (gnus-summary-show-thread)
7410   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7411     (gnus-eval-in-buffer-window gnus-article-buffer
7412       (cond ((> lines 0)
7413              (when (gnus-article-next-page lines)
7414                (gnus-message 3 "End of message")))
7415             ((< lines 0)
7416              (gnus-article-prev-page (- lines))))))
7417   (gnus-summary-recenter)
7418   (gnus-summary-position-point))
7419
7420 (defun gnus-summary-scroll-down (lines)
7421   "Scroll down (or up) one line current article.
7422 Argument LINES specifies lines to be scrolled down (or up if negative)."
7423   (interactive "p")
7424   (gnus-summary-scroll-up (- lines)))
7425
7426 (defun gnus-summary-next-same-subject ()
7427   "Select next article which has the same subject as current one."
7428   (interactive)
7429   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7430
7431 (defun gnus-summary-prev-same-subject ()
7432   "Select previous article which has the same subject as current one."
7433   (interactive)
7434   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7435
7436 (defun gnus-summary-next-unread-same-subject ()
7437   "Select next unread article which has the same subject as current one."
7438   (interactive)
7439   (gnus-summary-next-article t (gnus-summary-article-subject)))
7440
7441 (defun gnus-summary-prev-unread-same-subject ()
7442   "Select previous unread article which has the same subject as current one."
7443   (interactive)
7444   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7445
7446 (defun gnus-summary-first-unread-article ()
7447   "Select the first unread article.
7448 Return nil if there are no unread articles."
7449   (interactive)
7450   (prog1
7451       (when (gnus-summary-first-subject t)
7452         (gnus-summary-show-thread)
7453         (gnus-summary-first-subject t)
7454         (gnus-summary-display-article (gnus-summary-article-number)))
7455     (gnus-summary-position-point)))
7456
7457 (defun gnus-summary-first-unread-subject ()
7458   "Place the point on the subject line of the first unread article.
7459 Return nil if there are no unread articles."
7460   (interactive)
7461   (prog1
7462       (when (gnus-summary-first-subject t)
7463         (gnus-summary-show-thread)
7464         (gnus-summary-first-subject t))
7465     (gnus-summary-position-point)))
7466
7467 (defun gnus-summary-first-unseen-subject ()
7468   "Place the point on the subject line of the first unseen article.
7469 Return nil if there are no unseen articles."
7470   (interactive)
7471   (prog1
7472       (when (gnus-summary-first-subject nil nil t)
7473         (gnus-summary-show-thread)
7474         (gnus-summary-first-subject nil nil t))
7475     (gnus-summary-position-point)))
7476
7477 (defun gnus-summary-first-unseen-or-unread-subject ()
7478   "Place the point on the subject line of the first unseen article or,
7479 if all article have been seen, on the subject line of the first unread
7480 article."
7481   (interactive)
7482   (prog1
7483       (unless (when (gnus-summary-first-subject nil nil t)
7484                 (gnus-summary-show-thread)
7485                 (gnus-summary-first-subject nil nil t))
7486         (when (gnus-summary-first-subject t)
7487           (gnus-summary-show-thread)
7488           (gnus-summary-first-subject t)))
7489     (gnus-summary-position-point)))
7490
7491 (defun gnus-summary-first-article ()
7492   "Select the first article.
7493 Return nil if there are no articles."
7494   (interactive)
7495   (prog1
7496       (when (gnus-summary-first-subject)
7497         (gnus-summary-show-thread)
7498         (gnus-summary-first-subject)
7499         (gnus-summary-display-article (gnus-summary-article-number)))
7500     (gnus-summary-position-point)))
7501
7502 (defun gnus-summary-best-unread-article (&optional arg)
7503   "Select the unread article with the highest score.
7504 If given a prefix argument, select the next unread article that has a
7505 score higher than the default score."
7506   (interactive "P")
7507   (let ((article (if arg
7508                      (gnus-summary-better-unread-subject)
7509                    (gnus-summary-best-unread-subject))))
7510     (if article
7511         (gnus-summary-goto-article article)
7512       (error "No unread articles"))))
7513
7514 (defun gnus-summary-best-unread-subject ()
7515   "Select the unread subject with the highest score."
7516   (interactive)
7517   (let ((best -1000000)
7518         (data gnus-newsgroup-data)
7519         article score)
7520     (while data
7521       (and (gnus-data-unread-p (car data))
7522            (> (setq score
7523                     (gnus-summary-article-score (gnus-data-number (car data))))
7524               best)
7525            (setq best score
7526                  article (gnus-data-number (car data))))
7527       (setq data (cdr data)))
7528     (when article
7529       (gnus-summary-goto-subject article))
7530     (gnus-summary-position-point)
7531     article))
7532
7533 (defun gnus-summary-better-unread-subject ()
7534   "Select the first unread subject that has a score over the default score."
7535   (interactive)
7536   (let ((data gnus-newsgroup-data)
7537         article score)
7538     (while (and (setq article (gnus-data-number (car data)))
7539                 (or (gnus-data-read-p (car data))
7540                     (not (> (gnus-summary-article-score article)
7541                             gnus-summary-default-score))))
7542       (setq data (cdr data)))
7543     (when article
7544       (gnus-summary-goto-subject article))
7545     (gnus-summary-position-point)
7546     article))
7547
7548 (defun gnus-summary-last-subject ()
7549   "Go to the last displayed subject line in the group."
7550   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7551     (when article
7552       (gnus-summary-goto-subject article))))
7553
7554 (defun gnus-summary-goto-article (article &optional all-headers force)
7555   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7556 If ALL-HEADERS is non-nil, no header lines are hidden.
7557 If FORCE, go to the article even if it isn't displayed.  If FORCE
7558 is a number, it is the line the article is to be displayed on."
7559   (interactive
7560    (list
7561     (completing-read
7562      "Article number or Message-ID: "
7563      (mapcar (lambda (number) (list (int-to-string number)))
7564              gnus-newsgroup-limit))
7565     current-prefix-arg
7566     t))
7567   (prog1
7568       (if (and (stringp article)
7569                (string-match "@\\|%40" article))
7570           (gnus-summary-refer-article article)
7571         (when (stringp article)
7572           (setq article (string-to-number article)))
7573         (if (gnus-summary-goto-subject article force)
7574             (gnus-summary-display-article article all-headers)
7575           (gnus-message 4 "Couldn't go to article %s" article) nil))
7576     (gnus-summary-position-point)))
7577
7578 (defun gnus-summary-goto-last-article ()
7579   "Go to the previously read article."
7580   (interactive)
7581   (prog1
7582       (when gnus-last-article
7583         (gnus-summary-goto-article gnus-last-article nil t))
7584     (gnus-summary-position-point)))
7585
7586 (defun gnus-summary-pop-article (number)
7587   "Pop one article off the history and go to the previous.
7588 NUMBER articles will be popped off."
7589   (interactive "p")
7590   (let (to)
7591     (setq gnus-newsgroup-history
7592           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7593     (if to
7594         (gnus-summary-goto-article (car to) nil t)
7595       (error "Article history empty")))
7596   (gnus-summary-position-point))
7597
7598 ;; Summary commands and functions for limiting the summary buffer.
7599
7600 (defun gnus-summary-limit-to-articles (n)
7601   "Limit the summary buffer to the next N articles.
7602 If not given a prefix, use the process marked articles instead."
7603   (interactive "P")
7604   (prog1
7605       (let ((articles (gnus-summary-work-articles n)))
7606         (setq gnus-newsgroup-processable nil)
7607         (gnus-summary-limit articles))
7608     (gnus-summary-position-point)))
7609
7610 (defun gnus-summary-pop-limit (&optional total)
7611   "Restore the previous limit.
7612 If given a prefix, remove all limits."
7613   (interactive "P")
7614   (when total
7615     (setq gnus-newsgroup-limits
7616           (list (mapcar (lambda (h) (mail-header-number h))
7617                         gnus-newsgroup-headers))))
7618   (unless gnus-newsgroup-limits
7619     (error "No limit to pop"))
7620   (prog1
7621       (gnus-summary-limit nil 'pop)
7622     (gnus-summary-position-point)))
7623
7624 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7625   "Limit the summary buffer to articles that have subjects that match a regexp.
7626 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7627   (interactive
7628    (list (read-string (if current-prefix-arg
7629                           "Exclude subject (regexp): "
7630                         "Limit to subject (regexp): "))
7631          nil current-prefix-arg))
7632   (unless header
7633     (setq header "subject"))
7634   (when (not (equal "" subject))
7635     (prog1
7636         (let ((articles (gnus-summary-find-matching
7637                          (or header "subject") subject 'all nil nil
7638                          not-matching)))
7639           (unless articles
7640             (error "Found no matches for \"%s\"" subject))
7641           (gnus-summary-limit articles))
7642       (gnus-summary-position-point))))
7643
7644 (defun gnus-summary-limit-to-author (from &optional not-matching)
7645   "Limit the summary buffer to articles that have authors that match a regexp.
7646 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7647   (interactive
7648    (list (read-string (if current-prefix-arg
7649                           "Exclude author (regexp): "
7650                         "Limit to author (regexp): "))
7651          current-prefix-arg))
7652   (gnus-summary-limit-to-subject from "from" not-matching))
7653
7654 (defun gnus-summary-limit-to-age (age &optional younger-p)
7655   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7656 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7657 articles that are younger than AGE days."
7658   (interactive
7659    (let ((younger current-prefix-arg)
7660          (days-got nil)
7661          days)
7662      (while (not days-got)
7663        (setq days (if younger
7664                       (read-string "Limit to articles younger than (in days, older when negative): ")
7665                     (read-string
7666                      "Limit to articles older than (in days, younger when negative): ")))
7667        (when (> (length days) 0)
7668          (setq days (read days)))
7669        (if (numberp days)
7670            (progn
7671              (setq days-got t)
7672              (when (< days 0)
7673                (setq younger (not younger))
7674                (setq days (* days -1))))
7675          (message "Please enter a number.")
7676          (sleep-for 1)))
7677      (list days younger)))
7678   (prog1
7679       (let ((data gnus-newsgroup-data)
7680             (cutoff (days-to-time age))
7681             articles d date is-younger)
7682         (while (setq d (pop data))
7683           (when (and (vectorp (gnus-data-header d))
7684                      (setq date (mail-header-date (gnus-data-header d))))
7685             (setq is-younger (time-less-p
7686                               (time-since (condition-case ()
7687                                               (date-to-time date)
7688                                             (error '(0 0))))
7689                               cutoff))
7690             (when (if younger-p
7691                       is-younger
7692                     (not is-younger))
7693               (push (gnus-data-number d) articles))))
7694         (gnus-summary-limit (nreverse articles)))
7695     (gnus-summary-position-point)))
7696
7697 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7698   "Limit the summary buffer to articles that match an 'extra' header."
7699   (interactive
7700    (let ((header
7701           (intern
7702            (gnus-completing-read-with-default
7703             (symbol-name (car gnus-extra-headers))
7704             (if current-prefix-arg
7705                 "Exclude extra header:"
7706               "Limit extra header:")
7707             (mapcar (lambda (x)
7708                       (cons (symbol-name x) x))
7709                     gnus-extra-headers)
7710             nil
7711             t))))
7712      (list header
7713            (read-string (format "%s header %s (regexp): "
7714                                 (if current-prefix-arg "Exclude" "Limit to")
7715                                 header))
7716            current-prefix-arg)))
7717   (when (not (equal "" regexp))
7718     (prog1
7719         (let ((articles (gnus-summary-find-matching
7720                          (cons 'extra header) regexp 'all nil nil
7721                          not-matching)))
7722           (unless articles
7723             (error "Found no matches for \"%s\"" regexp))
7724           (gnus-summary-limit articles))
7725       (gnus-summary-position-point))))
7726
7727 (defun gnus-summary-limit-to-display-predicate ()
7728   "Limit the summary buffer to the predicated in the `display' group parameter."
7729   (interactive)
7730   (unless gnus-newsgroup-display
7731     (error "There is no `display' group parameter"))
7732   (let (articles)
7733     (dolist (number gnus-newsgroup-articles)
7734       (when (funcall gnus-newsgroup-display)
7735         (push number articles)))
7736     (gnus-summary-limit articles))
7737   (gnus-summary-position-point))
7738
7739 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7740 (make-obsolete
7741  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7742
7743 (defun gnus-summary-limit-to-unread (&optional all)
7744   "Limit the summary buffer to articles that are not marked as read.
7745 If ALL is non-nil, limit strictly to unread articles."
7746   (interactive "P")
7747   (if all
7748       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7749     (gnus-summary-limit-to-marks
7750      ;; Concat all the marks that say that an article is read and have
7751      ;; those removed.
7752      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7753            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7754            gnus-low-score-mark gnus-expirable-mark
7755            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7756            gnus-duplicate-mark gnus-souped-mark)
7757      'reverse)))
7758
7759 (defun gnus-summary-limit-to-replied (&optional unreplied)
7760   "Limit the summary buffer to replied articles.
7761 If UNREPLIED (the prefix), limit to unreplied articles."
7762   (interactive "P")
7763   (if unreplied
7764       (gnus-summary-limit
7765        (gnus-set-difference gnus-newsgroup-articles
7766         gnus-newsgroup-replied))
7767     (gnus-summary-limit gnus-newsgroup-replied))
7768   (gnus-summary-position-point))
7769
7770 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7771 (make-obsolete 'gnus-summary-delete-marked-with
7772                'gnus-summary-limit-exclude-marks)
7773
7774 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7775   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7776 If REVERSE, limit the summary buffer to articles that are marked
7777 with MARKS.  MARKS can either be a string of marks or a list of marks.
7778 Returns how many articles were removed."
7779   (interactive "sMarks: ")
7780   (gnus-summary-limit-to-marks marks t))
7781
7782 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7783   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7784 If REVERSE (the prefix), limit the summary buffer to articles that are
7785 not marked with MARKS.  MARKS can either be a string of marks or a
7786 list of marks.
7787 Returns how many articles were removed."
7788   (interactive "sMarks: \nP")
7789   (prog1
7790       (let ((data gnus-newsgroup-data)
7791             (marks (if (listp marks) marks
7792                      (append marks nil))) ; Transform to list.
7793             articles)
7794         (while data
7795           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7796                   (memq (gnus-data-mark (car data)) marks))
7797             (push (gnus-data-number (car data)) articles))
7798           (setq data (cdr data)))
7799         (gnus-summary-limit articles))
7800     (gnus-summary-position-point)))
7801
7802 (defun gnus-summary-limit-to-score (score)
7803   "Limit to articles with score at or above SCORE."
7804   (interactive "NLimit to articles with score of at least: ")
7805   (let ((data gnus-newsgroup-data)
7806         articles)
7807     (while data
7808       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7809                 score)
7810         (push (gnus-data-number (car data)) articles))
7811       (setq data (cdr data)))
7812     (prog1
7813         (gnus-summary-limit articles)
7814       (gnus-summary-position-point))))
7815
7816 (defun gnus-summary-limit-to-unseen ()
7817   "Limit to unseen articles."
7818   (interactive)
7819   (prog1
7820       (gnus-summary-limit gnus-newsgroup-unseen)
7821     (gnus-summary-position-point)))
7822
7823 (defun gnus-summary-limit-include-thread (id)
7824   "Display all the hidden articles that is in the thread with ID in it.
7825 When called interactively, ID is the Message-ID of the current
7826 article."
7827   (interactive (list (mail-header-id (gnus-summary-article-header))))
7828   (let ((articles (gnus-articles-in-thread
7829                    (gnus-id-to-thread (gnus-root-id id)))))
7830     (prog1
7831         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7832       (gnus-summary-limit-include-matching-articles
7833        "subject"
7834        (regexp-quote (gnus-simplify-subject-re
7835                       (mail-header-subject (gnus-id-to-header id)))))
7836       (gnus-summary-position-point))))
7837
7838 (defun gnus-summary-limit-include-matching-articles (header regexp)
7839   "Display all the hidden articles that have HEADERs that match REGEXP."
7840   (interactive (list (read-string "Match on header: ")
7841                      (read-string "Regexp: ")))
7842   (let ((articles (gnus-find-matching-articles header regexp)))
7843     (prog1
7844         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7845       (gnus-summary-position-point))))
7846
7847 (defun gnus-summary-insert-dormant-articles ()
7848   "Insert all the dormant articles for this group into the current buffer."
7849   (interactive)
7850   (let ((gnus-verbose (max 6 gnus-verbose)))
7851     (if (not gnus-newsgroup-dormant)
7852         (gnus-message 3 "No cached articles for this group")
7853       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7854
7855 (defun gnus-summary-limit-include-dormant ()
7856   "Display all the hidden articles that are marked as dormant.
7857 Note that this command only works on a subset of the articles currently
7858 fetched for this group."
7859   (interactive)
7860   (unless gnus-newsgroup-dormant
7861     (error "There are no dormant articles in this group"))
7862   (prog1
7863       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7864     (gnus-summary-position-point)))
7865
7866 (defun gnus-summary-limit-exclude-dormant ()
7867   "Hide all dormant articles."
7868   (interactive)
7869   (prog1
7870       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7871     (gnus-summary-position-point)))
7872
7873 (defun gnus-summary-limit-exclude-childless-dormant ()
7874   "Hide all dormant articles that have no children."
7875   (interactive)
7876   (let ((data (gnus-data-list t))
7877         articles d children)
7878     ;; Find all articles that are either not dormant or have
7879     ;; children.
7880     (while (setq d (pop data))
7881       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7882                 (and (setq children
7883                            (gnus-article-children (gnus-data-number d)))
7884                      (let (found)
7885                        (while children
7886                          (when (memq (car children) articles)
7887                            (setq children nil
7888                                  found t))
7889                          (pop children))
7890                        found)))
7891         (push (gnus-data-number d) articles)))
7892     ;; Do the limiting.
7893     (prog1
7894         (gnus-summary-limit articles)
7895       (gnus-summary-position-point))))
7896
7897 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7898   "Mark all unread excluded articles as read.
7899 If ALL, mark even excluded ticked and dormants as read."
7900   (interactive "P")
7901   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7902   (let ((articles (gnus-sorted-ndifference
7903                    (sort
7904                     (mapcar (lambda (h) (mail-header-number h))
7905                             gnus-newsgroup-headers)
7906                     '<)
7907                    gnus-newsgroup-limit))
7908         article)
7909     (setq gnus-newsgroup-unreads
7910           (gnus-sorted-intersection gnus-newsgroup-unreads
7911                                     gnus-newsgroup-limit))
7912     (if all
7913         (setq gnus-newsgroup-dormant nil
7914               gnus-newsgroup-marked nil
7915               gnus-newsgroup-reads
7916               (nconc
7917                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7918                gnus-newsgroup-reads))
7919       (while (setq article (pop articles))
7920         (unless (or (memq article gnus-newsgroup-dormant)
7921                     (memq article gnus-newsgroup-marked))
7922           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7923
7924 (defun gnus-summary-limit (articles &optional pop)
7925   (if pop
7926       ;; We pop the previous limit off the stack and use that.
7927       (setq articles (car gnus-newsgroup-limits)
7928             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7929     ;; We use the new limit, so we push the old limit on the stack.
7930     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7931   ;; Set the limit.
7932   (setq gnus-newsgroup-limit articles)
7933   (let ((total (length gnus-newsgroup-data))
7934         (data (gnus-data-find-list (gnus-summary-article-number)))
7935         (gnus-summary-mark-below nil)   ; Inhibit this.
7936         found)
7937     ;; This will do all the work of generating the new summary buffer
7938     ;; according to the new limit.
7939     (gnus-summary-prepare)
7940     ;; Hide any threads, possibly.
7941     (gnus-summary-maybe-hide-threads)
7942     ;; Try to return to the article you were at, or one in the
7943     ;; neighborhood.
7944     (when data
7945       ;; We try to find some article after the current one.
7946       (while data
7947         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7948           (setq data nil
7949                 found t))
7950         (setq data (cdr data))))
7951     (unless found
7952       ;; If there is no data, that means that we were after the last
7953       ;; article.  The same goes when we can't find any articles
7954       ;; after the current one.
7955       (goto-char (point-max))
7956       (gnus-summary-find-prev))
7957     (gnus-set-mode-line 'summary)
7958     ;; We return how many articles were removed from the summary
7959     ;; buffer as a result of the new limit.
7960     (- total (length gnus-newsgroup-data))))
7961
7962 (defsubst gnus-invisible-cut-children (threads)
7963   (let ((num 0))
7964     (while threads
7965       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7966         (incf num))
7967       (pop threads))
7968     (< num 2)))
7969
7970 (defsubst gnus-cut-thread (thread)
7971   "Go forwards in the thread until we find an article that we want to display."
7972   (when (or (eq gnus-fetch-old-headers 'some)
7973             (eq gnus-fetch-old-headers 'invisible)
7974             (numberp gnus-fetch-old-headers)
7975             (eq gnus-build-sparse-threads 'some)
7976             (eq gnus-build-sparse-threads 'more))
7977     ;; Deal with old-fetched headers and sparse threads.
7978     (while (and
7979             thread
7980             (or
7981              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7982              (gnus-summary-article-ancient-p
7983               (mail-header-number (car thread))))
7984             (if (or (<= (length (cdr thread)) 1)
7985                     (eq gnus-fetch-old-headers 'invisible))
7986                 (setq gnus-newsgroup-limit
7987                       (delq (mail-header-number (car thread))
7988                             gnus-newsgroup-limit)
7989                       thread (cadr thread))
7990               (when (gnus-invisible-cut-children (cdr thread))
7991                 (let ((th (cdr thread)))
7992                   (while th
7993                     (if (memq (mail-header-number (caar th))
7994                               gnus-newsgroup-limit)
7995                         (setq thread (car th)
7996                               th nil)
7997                       (setq th (cdr th))))))))))
7998   thread)
7999
8000 (defun gnus-cut-threads (threads)
8001   "Cut off all uninteresting articles from the beginning of THREADS."
8002   (when (or (eq gnus-fetch-old-headers 'some)
8003             (eq gnus-fetch-old-headers 'invisible)
8004             (numberp gnus-fetch-old-headers)
8005             (eq gnus-build-sparse-threads 'some)
8006             (eq gnus-build-sparse-threads 'more))
8007     (let ((th threads))
8008       (while th
8009         (setcar th (gnus-cut-thread (car th)))
8010         (setq th (cdr th)))))
8011   ;; Remove nixed out threads.
8012   (delq nil threads))
8013
8014 (defun gnus-summary-initial-limit (&optional show-if-empty)
8015   "Figure out what the initial limit is supposed to be on group entry.
8016 This entails weeding out unwanted dormants, low-scored articles,
8017 fetch-old-headers verbiage, and so on."
8018   ;; Most groups have nothing to remove.
8019   (if (or gnus-inhibit-limiting
8020           (and (null gnus-newsgroup-dormant)
8021                (eq gnus-newsgroup-display 'gnus-not-ignore)
8022                (not (eq gnus-fetch-old-headers 'some))
8023                (not (numberp gnus-fetch-old-headers))
8024                (not (eq gnus-fetch-old-headers 'invisible))
8025                (null gnus-summary-expunge-below)
8026                (not (eq gnus-build-sparse-threads 'some))
8027                (not (eq gnus-build-sparse-threads 'more))
8028                (null gnus-thread-expunge-below)
8029                (not gnus-use-nocem)))
8030       ()                                ; Do nothing.
8031     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8032     (setq gnus-newsgroup-limit nil)
8033     (mapatoms
8034      (lambda (node)
8035        (unless (car (symbol-value node))
8036          ;; These threads have no parents -- they are roots.
8037          (let ((nodes (cdr (symbol-value node)))
8038                thread)
8039            (while nodes
8040              (if (and gnus-thread-expunge-below
8041                       (< (gnus-thread-total-score (car nodes))
8042                          gnus-thread-expunge-below))
8043                  (gnus-expunge-thread (pop nodes))
8044                (setq thread (pop nodes))
8045                (gnus-summary-limit-children thread))))))
8046      gnus-newsgroup-dependencies)
8047     ;; If this limitation resulted in an empty group, we might
8048     ;; pop the previous limit and use it instead.
8049     (when (and (not gnus-newsgroup-limit)
8050                show-if-empty)
8051       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8052     gnus-newsgroup-limit))
8053
8054 (defun gnus-summary-limit-children (thread)
8055   "Return 1 if this subthread is visible and 0 if it is not."
8056   ;; First we get the number of visible children to this thread.  This
8057   ;; is done by recursing down the thread using this function, so this
8058   ;; will really go down to a leaf article first, before slowly
8059   ;; working its way up towards the root.
8060   (when thread
8061     (let* ((max-lisp-eval-depth 5000)
8062            (children
8063            (if (cdr thread)
8064                (apply '+ (mapcar 'gnus-summary-limit-children
8065                                  (cdr thread)))
8066              0))
8067           (number (mail-header-number (car thread)))
8068           score)
8069       (if (and
8070            (not (memq number gnus-newsgroup-marked))
8071            (or
8072             ;; If this article is dormant and has absolutely no visible
8073             ;; children, then this article isn't visible.
8074             (and (memq number gnus-newsgroup-dormant)
8075                  (zerop children))
8076             ;; If this is "fetch-old-headered" and there is no
8077             ;; visible children, then we don't want this article.
8078             (and (or (eq gnus-fetch-old-headers 'some)
8079                      (numberp gnus-fetch-old-headers))
8080                  (gnus-summary-article-ancient-p number)
8081                  (zerop children))
8082             ;; If this is "fetch-old-headered" and `invisible', then
8083             ;; we don't want this article.
8084             (and (eq gnus-fetch-old-headers 'invisible)
8085                  (gnus-summary-article-ancient-p number))
8086             ;; If this is a sparsely inserted article with no children,
8087             ;; we don't want it.
8088             (and (eq gnus-build-sparse-threads 'some)
8089                  (gnus-summary-article-sparse-p number)
8090                  (zerop children))
8091             ;; If we use expunging, and this article is really
8092             ;; low-scored, then we don't want this article.
8093             (when (and gnus-summary-expunge-below
8094                        (< (setq score
8095                                 (or (cdr (assq number gnus-newsgroup-scored))
8096                                     gnus-summary-default-score))
8097                           gnus-summary-expunge-below))
8098               ;; We increase the expunge-tally here, but that has
8099               ;; nothing to do with the limits, really.
8100               (incf gnus-newsgroup-expunged-tally)
8101               ;; We also mark as read here, if that's wanted.
8102               (when (and gnus-summary-mark-below
8103                          (< score gnus-summary-mark-below))
8104                 (setq gnus-newsgroup-unreads
8105                       (delq number gnus-newsgroup-unreads))
8106                 (if gnus-newsgroup-auto-expire
8107                     (push number gnus-newsgroup-expirable)
8108                   (push (cons number gnus-low-score-mark)
8109                         gnus-newsgroup-reads)))
8110               t)
8111             ;; Do the `display' group parameter.
8112             (and gnus-newsgroup-display
8113                  (not (funcall gnus-newsgroup-display)))
8114             ;; Check NoCeM things.
8115             (when (and gnus-use-nocem
8116                        (gnus-nocem-unwanted-article-p
8117                         (mail-header-id (car thread))))
8118               (setq gnus-newsgroup-unreads
8119                     (delq number gnus-newsgroup-unreads))
8120               t)))
8121           ;; Nope, invisible article.
8122           0
8123         ;; Ok, this article is to be visible, so we add it to the limit
8124         ;; and return 1.
8125         (push number gnus-newsgroup-limit)
8126         1))))
8127
8128 (defun gnus-expunge-thread (thread)
8129   "Mark all articles in THREAD as read."
8130   (let* ((number (mail-header-number (car thread))))
8131     (incf gnus-newsgroup-expunged-tally)
8132     ;; We also mark as read here, if that's wanted.
8133     (setq gnus-newsgroup-unreads
8134           (delq number gnus-newsgroup-unreads))
8135     (if gnus-newsgroup-auto-expire
8136         (push number gnus-newsgroup-expirable)
8137       (push (cons number gnus-low-score-mark)
8138             gnus-newsgroup-reads)))
8139   ;; Go recursively through all subthreads.
8140   (mapcar 'gnus-expunge-thread (cdr thread)))
8141
8142 ;; Summary article oriented commands
8143
8144 (defun gnus-summary-refer-parent-article (n)
8145   "Refer parent article N times.
8146 If N is negative, go to ancestor -N instead.
8147 The difference between N and the number of articles fetched is returned."
8148   (interactive "p")
8149   (let ((skip 1)
8150         error header ref)
8151     (when (not (natnump n))
8152       (setq skip (abs n)
8153             n 1))
8154     (while (and (> n 0)
8155                 (not error))
8156       (setq header (gnus-summary-article-header))
8157       (if (and (eq (mail-header-number header)
8158                    (cdr gnus-article-current))
8159                (equal gnus-newsgroup-name
8160                       (car gnus-article-current)))
8161           ;; If we try to find the parent of the currently
8162           ;; displayed article, then we take a look at the actual
8163           ;; References header, since this is slightly more
8164           ;; reliable than the References field we got from the
8165           ;; server.
8166           (save-excursion
8167             (set-buffer gnus-original-article-buffer)
8168             (nnheader-narrow-to-headers)
8169             (unless (setq ref (message-fetch-field "references"))
8170               (when (setq ref (message-fetch-field "in-reply-to"))
8171                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8172             (widen))
8173         (setq ref
8174               ;; It's not the current article, so we take a bet on
8175               ;; the value we got from the server.
8176               (mail-header-references header)))
8177       (if (and ref
8178                (not (equal ref "")))
8179           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8180             (gnus-message 1 "Couldn't find parent"))
8181         (gnus-message 1 "No references in article %d"
8182                       (gnus-summary-article-number))
8183         (setq error t))
8184       (decf n))
8185     (gnus-summary-position-point)
8186     n))
8187
8188 (defun gnus-summary-refer-references ()
8189   "Fetch all articles mentioned in the References header.
8190 Return the number of articles fetched."
8191   (interactive)
8192   (let ((ref (mail-header-references (gnus-summary-article-header)))
8193         (current (gnus-summary-article-number))
8194         (n 0))
8195     (if (or (not ref)
8196             (equal ref ""))
8197         (error "No References in the current article")
8198       ;; For each Message-ID in the References header...
8199       (while (string-match "<[^>]*>" ref)
8200         (incf n)
8201         ;; ... fetch that article.
8202         (gnus-summary-refer-article
8203          (prog1 (match-string 0 ref)
8204            (setq ref (substring ref (match-end 0))))))
8205       (gnus-summary-goto-subject current)
8206       (gnus-summary-position-point)
8207       n)))
8208
8209 (defun gnus-summary-refer-thread (&optional limit)
8210   "Fetch all articles in the current thread.
8211 If LIMIT (the numerical prefix), fetch that many old headers instead
8212 of what's specified by the `gnus-refer-thread-limit' variable."
8213   (interactive "P")
8214   (let ((id (mail-header-id (gnus-summary-article-header)))
8215         (limit (if limit (prefix-numeric-value limit)
8216                  gnus-refer-thread-limit)))
8217     (unless (eq gnus-fetch-old-headers 'invisible)
8218       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8219       ;; Retrieve the headers and read them in.
8220       (if (eq (if (numberp limit)
8221                   (gnus-retrieve-headers
8222                    (list (min
8223                           (+ (mail-header-number
8224                               (gnus-summary-article-header))
8225                              limit)
8226                           gnus-newsgroup-end))
8227                    gnus-newsgroup-name (* limit 2))
8228                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8229                 ;; headers.
8230                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8231                                        gnus-newsgroup-name limit))
8232               'nov)
8233           (gnus-build-all-threads)
8234         (error "Can't fetch thread from back ends that don't support NOV"))
8235       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8236     (gnus-summary-limit-include-thread id)))
8237
8238 (defun gnus-summary-refer-article (message-id)
8239   "Fetch an article specified by MESSAGE-ID."
8240   (interactive "sMessage-ID: ")
8241   (when (and (stringp message-id)
8242              (not (zerop (length message-id))))
8243     (setq message-id (gnus-replace-in-string message-id " " ""))
8244     ;; Construct the correct Message-ID if necessary.
8245     ;; Suggested by tale@pawl.rpi.edu.
8246     (unless (string-match "^<" message-id)
8247       (setq message-id (concat "<" message-id)))
8248     (unless (string-match ">$" message-id)
8249       (setq message-id (concat message-id ">")))
8250     ;; People often post MIDs from URLs, so unhex it:
8251     (unless (string-match "@" message-id)
8252       (setq message-id (gnus-url-unhex-string message-id)))
8253     (let* ((header (gnus-id-to-header message-id))
8254            (sparse (and header
8255                         (gnus-summary-article-sparse-p
8256                          (mail-header-number header))
8257                         (memq (mail-header-number header)
8258                               gnus-newsgroup-limit)))
8259            number)
8260       (cond
8261        ;; If the article is present in the buffer we just go to it.
8262        ((and header
8263              (or (not (gnus-summary-article-sparse-p
8264                        (mail-header-number header)))
8265                  sparse))
8266         (prog1
8267             (gnus-summary-goto-article
8268              (mail-header-number header) nil t)
8269           (when sparse
8270             (gnus-summary-update-article (mail-header-number header)))))
8271        (t
8272         ;; We fetch the article.
8273         (catch 'found
8274           (dolist (gnus-override-method (gnus-refer-article-methods))
8275             (when (and (gnus-check-server gnus-override-method)
8276                        ;; Fetch the header,
8277                        (setq number (gnus-summary-insert-subject message-id)))
8278               ;; and display the article.
8279               (gnus-summary-select-article nil nil nil number)
8280               (throw 'found t)))
8281           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8282
8283 (defun gnus-refer-article-methods ()
8284   "Return a list of referable methods."
8285   (cond
8286    ;; No method, so we default to current and native.
8287    ((null gnus-refer-article-method)
8288     (list gnus-current-select-method gnus-select-method))
8289    ;; Current.
8290    ((eq 'current gnus-refer-article-method)
8291     (list gnus-current-select-method))
8292    ;; List of select methods.
8293    ((not (and (symbolp (car gnus-refer-article-method))
8294               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8295     (let (out)
8296       (dolist (method gnus-refer-article-method)
8297         (push (if (eq 'current method)
8298                   gnus-current-select-method
8299                 method)
8300               out))
8301       (nreverse out)))
8302    ;; One single select method.
8303    (t
8304     (list gnus-refer-article-method))))
8305
8306 (defun gnus-summary-edit-parameters ()
8307   "Edit the group parameters of the current group."
8308   (interactive)
8309   (gnus-group-edit-group gnus-newsgroup-name 'params))
8310
8311 (defun gnus-summary-customize-parameters ()
8312   "Customize the group parameters of the current group."
8313   (interactive)
8314   (gnus-group-customize gnus-newsgroup-name))
8315
8316 (defun gnus-summary-enter-digest-group (&optional force)
8317   "Enter an nndoc group based on the current article.
8318 If FORCE, force a digest interpretation.  If not, try
8319 to guess what the document format is."
8320   (interactive "P")
8321   (let ((conf gnus-current-window-configuration))
8322     (save-window-excursion
8323       (save-excursion
8324         (let (gnus-article-prepare-hook
8325               gnus-display-mime-function
8326               gnus-break-pages)
8327           (gnus-summary-select-article))))
8328     (setq gnus-current-window-configuration conf)
8329     (let* ((name (format "%s-%d"
8330                          (gnus-group-prefixed-name
8331                           gnus-newsgroup-name (list 'nndoc ""))
8332                          (with-current-buffer gnus-summary-buffer
8333                            gnus-current-article)))
8334            (ogroup gnus-newsgroup-name)
8335            (params (append (gnus-info-params (gnus-get-info ogroup))
8336                            (list (cons 'to-group ogroup))
8337                            (list (cons 'parent-group ogroup))
8338                            (list (cons 'save-article-group ogroup))))
8339            (case-fold-search t)
8340            (buf (current-buffer))
8341            dig to-address)
8342       (save-excursion
8343         (set-buffer gnus-original-article-buffer)
8344         ;; Have the digest group inherit the main mail address of
8345         ;; the parent article.
8346         (when (setq to-address (or (gnus-fetch-field "reply-to")
8347                                    (gnus-fetch-field "from")))
8348           (setq params (append
8349                         (list (cons 'to-address
8350                                     (funcall gnus-decode-encoded-word-function
8351                                              to-address))))))
8352         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8353         (insert-buffer-substring gnus-original-article-buffer)
8354         ;; Remove lines that may lead nndoc to misinterpret the
8355         ;; document type.
8356         (narrow-to-region
8357          (goto-char (point-min))
8358          (or (search-forward "\n\n" nil t) (point)))
8359         (goto-char (point-min))
8360         (delete-matching-lines "^Path:\\|^From ")
8361         (widen))
8362       (unwind-protect
8363           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8364                     (gnus-newsgroup-ephemeral-ignored-charsets
8365                      gnus-newsgroup-ignored-charsets))
8366                 (gnus-group-read-ephemeral-group
8367                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8368                               (nndoc-article-type
8369                                ,(if force 'mbox 'guess)))
8370                  t nil nil nil
8371                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8372                                                         "ADAPT")))))
8373               ;; Make all postings to this group go to the parent group.
8374               (nconc (gnus-info-params (gnus-get-info name))
8375                      params)
8376             ;; Couldn't select this doc group.
8377             (switch-to-buffer buf)
8378             (gnus-set-global-variables)
8379             (gnus-configure-windows 'summary)
8380             (gnus-message 3 "Article couldn't be entered?"))
8381         (kill-buffer dig)))))
8382
8383 (defun gnus-summary-read-document (n)
8384   "Open a new group based on the current article(s).
8385 This will allow you to read digests and other similar
8386 documents as newsgroups.
8387 Obeys the standard process/prefix convention."
8388   (interactive "P")
8389   (let* ((articles (gnus-summary-work-articles n))
8390          (ogroup gnus-newsgroup-name)
8391          (params (append (gnus-info-params (gnus-get-info ogroup))
8392                          (list (cons 'to-group ogroup))))
8393          article group egroup groups vgroup)
8394     (while (setq article (pop articles))
8395       (setq group (format "%s-%d" gnus-newsgroup-name article))
8396       (gnus-summary-remove-process-mark article)
8397       (when (gnus-summary-display-article article)
8398         (save-excursion
8399           (with-temp-buffer
8400             (insert-buffer-substring gnus-original-article-buffer)
8401             ;; Remove some headers that may lead nndoc to make
8402             ;; the wrong guess.
8403             (message-narrow-to-head)
8404             (goto-char (point-min))
8405             (delete-matching-lines "^Path:\\|^From ")
8406             (widen)
8407             (if (setq egroup
8408                       (gnus-group-read-ephemeral-group
8409                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8410                                      (nndoc-article-type guess))
8411                        t nil t))
8412                 (progn
8413             ;; Make all postings to this group go to the parent group.
8414                   (nconc (gnus-info-params (gnus-get-info egroup))
8415                          params)
8416                   (push egroup groups))
8417               ;; Couldn't select this doc group.
8418               (gnus-error 3 "Article couldn't be entered"))))))
8419     ;; Now we have selected all the documents.
8420     (cond
8421      ((not groups)
8422       (error "None of the articles could be interpreted as documents"))
8423      ((gnus-group-read-ephemeral-group
8424        (setq vgroup (format
8425                      "nnvirtual:%s-%s" gnus-newsgroup-name
8426                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8427        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8428        t
8429        (cons (current-buffer) 'summary)))
8430      (t
8431       (error "Couldn't select virtual nndoc group")))))
8432
8433 (defun gnus-summary-isearch-article (&optional regexp-p)
8434   "Do incremental search forward on the current article.
8435 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8436   (interactive "P")
8437   (gnus-summary-select-article)
8438   (gnus-configure-windows 'article)
8439   (gnus-eval-in-buffer-window gnus-article-buffer
8440     (save-restriction
8441       (widen)
8442       (isearch-forward regexp-p))))
8443
8444 (defun gnus-summary-search-article-forward (regexp &optional backward)
8445   "Search for an article containing REGEXP forward.
8446 If BACKWARD, search backward instead."
8447   (interactive
8448    (list (read-string
8449           (format "Search article %s (regexp%s): "
8450                   (if current-prefix-arg "backward" "forward")
8451                   (if gnus-last-search-regexp
8452                       (concat ", default " gnus-last-search-regexp)
8453                     "")))
8454          current-prefix-arg))
8455   (if (string-equal regexp "")
8456       (setq regexp (or gnus-last-search-regexp ""))
8457     (setq gnus-last-search-regexp regexp)
8458     (setq gnus-article-before-search gnus-current-article))
8459   ;; Intentionally set gnus-last-article.
8460   (setq gnus-last-article gnus-article-before-search)
8461   (let ((gnus-last-article gnus-last-article))
8462     (if (gnus-summary-search-article regexp backward)
8463         (gnus-summary-show-thread)
8464       (signal 'search-failed (list regexp)))))
8465
8466 (defun gnus-summary-search-article-backward (regexp)
8467   "Search for an article containing REGEXP backward."
8468   (interactive
8469    (list (read-string
8470           (format "Search article backward (regexp%s): "
8471                   (if gnus-last-search-regexp
8472                       (concat ", default " gnus-last-search-regexp)
8473                     "")))))
8474   (gnus-summary-search-article-forward regexp 'backward))
8475
8476 (defun gnus-summary-search-article (regexp &optional backward)
8477   "Search for an article containing REGEXP.
8478 Optional argument BACKWARD means do search for backward.
8479 `gnus-select-article-hook' is not called during the search."
8480   ;; We have to require this here to make sure that the following
8481   ;; dynamic binding isn't shadowed by autoloading.
8482   (require 'gnus-async)
8483   (require 'gnus-art)
8484   (let ((gnus-select-article-hook nil)  ;Disable hook.
8485         (gnus-article-prepare-hook nil)
8486         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8487         (gnus-use-article-prefetch nil)
8488         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8489         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8490         (gnus-visual nil)
8491         (gnus-keep-backlog nil)
8492         (gnus-break-pages nil)
8493         (gnus-summary-display-arrow nil)
8494         (gnus-updated-mode-lines nil)
8495         (gnus-auto-center-summary nil)
8496         (sum (current-buffer))
8497         (gnus-display-mime-function nil)
8498         (found nil)
8499         point)
8500     (gnus-save-hidden-threads
8501       (gnus-summary-select-article)
8502       (set-buffer gnus-article-buffer)
8503       (goto-char (window-point (get-buffer-window (current-buffer))))
8504       (when backward
8505         (forward-line -1))
8506       (while (not found)
8507         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8508         (if (if backward
8509                 (re-search-backward regexp nil t)
8510               (re-search-forward regexp nil t))
8511             ;; We found the regexp.
8512             (progn
8513               (setq found 'found)
8514               (beginning-of-line)
8515               (set-window-start
8516                (get-buffer-window (current-buffer))
8517                (point))
8518               (forward-line 1)
8519               (set-window-point
8520                (get-buffer-window (current-buffer))
8521                (point))
8522               (set-buffer sum)
8523               (setq point (point)))
8524           ;; We didn't find it, so we go to the next article.
8525           (set-buffer sum)
8526           (setq found 'not)
8527           (while (eq found 'not)
8528             (if (not (if backward (gnus-summary-find-prev)
8529                        (gnus-summary-find-next)))
8530                 ;; No more articles.
8531                 (setq found t)
8532               ;; Select the next article and adjust point.
8533               (unless (gnus-summary-article-sparse-p
8534                        (gnus-summary-article-number))
8535                 (setq found nil)
8536                 (gnus-summary-select-article)
8537                 (set-buffer gnus-article-buffer)
8538                 (widen)
8539                 (goto-char (if backward (point-max) (point-min))))))))
8540       (gnus-message 7 ""))
8541     ;; Return whether we found the regexp.
8542     (when (eq found 'found)
8543       (goto-char point)
8544       (gnus-summary-show-thread)
8545       (gnus-summary-goto-subject gnus-current-article)
8546       (gnus-summary-position-point)
8547       t)))
8548
8549 (defun gnus-find-matching-articles (header regexp)
8550   "Return a list of all articles that match REGEXP on HEADER.
8551 This search includes all articles in the current group that Gnus has
8552 fetched headers for, whether they are displayed or not."
8553   (let ((articles nil)
8554         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8555         (case-fold-search t))
8556     (dolist (header gnus-newsgroup-headers)
8557       (when (string-match regexp (funcall func header))
8558         (push (mail-header-number header) articles)))
8559     (nreverse articles)))
8560
8561 (defun gnus-summary-find-matching (header regexp &optional backward unread
8562                                           not-case-fold not-matching)
8563   "Return a list of all articles that match REGEXP on HEADER.
8564 The search stars on the current article and goes forwards unless
8565 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8566 If UNREAD is non-nil, only unread articles will
8567 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8568 in the comparisons. If NOT-MATCHING, return a list of all articles that
8569 not match REGEXP on HEADER."
8570   (let ((case-fold-search (not not-case-fold))
8571         articles d func)
8572     (if (consp header)
8573         (if (eq (car header) 'extra)
8574             (setq func
8575                   `(lambda (h)
8576                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8577                          "")))
8578           (error "%s is an invalid header" header))
8579       (unless (fboundp (intern (concat "mail-header-" header)))
8580         (error "%s is not a valid header" header))
8581       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8582     (dolist (d (if (eq backward 'all)
8583                    gnus-newsgroup-data
8584                  (gnus-data-find-list
8585                   (gnus-summary-article-number)
8586                   (gnus-data-list backward))))
8587       (when (and (or (not unread)       ; We want all articles...
8588                      (gnus-data-unread-p d)) ; Or just unreads.
8589                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8590                  (if not-matching
8591                      (not (string-match
8592                            regexp
8593                            (funcall func (gnus-data-header d))))
8594                    (string-match regexp
8595                                  (funcall func (gnus-data-header d)))))
8596         (push (gnus-data-number d) articles))) ; Success!
8597     (nreverse articles)))
8598
8599 (defun gnus-summary-execute-command (header regexp command &optional backward)
8600   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8601 If HEADER is an empty string (or nil), the match is done on the entire
8602 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8603   (interactive
8604    (list (let ((completion-ignore-case t))
8605            (completing-read
8606             "Header name: "
8607             (mapcar (lambda (header) (list (format "%s" header)))
8608                     (append
8609                      '("Number" "Subject" "From" "Lines" "Date"
8610                        "Message-ID" "Xref" "References" "Body")
8611                      gnus-extra-headers))
8612             nil 'require-match))
8613          (read-string "Regexp: ")
8614          (read-key-sequence "Command: ")
8615          current-prefix-arg))
8616   (when (equal header "Body")
8617     (setq header ""))
8618   ;; Hidden thread subtrees must be searched as well.
8619   (gnus-summary-show-all-threads)
8620   ;; We don't want to change current point nor window configuration.
8621   (save-excursion
8622     (save-window-excursion
8623       (let (gnus-visual
8624             gnus-treat-strip-trailing-blank-lines
8625             gnus-treat-strip-leading-blank-lines
8626             gnus-treat-strip-multiple-blank-lines
8627             gnus-treat-hide-boring-headers
8628             gnus-treat-fold-newsgroups
8629             gnus-article-prepare-hook)
8630         (gnus-message 6 "Executing %s..." (key-description command))
8631         ;; We'd like to execute COMMAND interactively so as to give arguments.
8632         (gnus-execute header regexp
8633                       `(call-interactively ',(key-binding command))
8634                       backward)
8635         (gnus-message 6 "Executing %s...done" (key-description command))))))
8636
8637 (defun gnus-summary-beginning-of-article ()
8638   "Scroll the article back to the beginning."
8639   (interactive)
8640   (gnus-summary-select-article)
8641   (gnus-configure-windows 'article)
8642   (gnus-eval-in-buffer-window gnus-article-buffer
8643     (widen)
8644     (goto-char (point-min))
8645     (when gnus-break-pages
8646       (gnus-narrow-to-page))))
8647
8648 (defun gnus-summary-end-of-article ()
8649   "Scroll to the end of the article."
8650   (interactive)
8651   (gnus-summary-select-article)
8652   (gnus-configure-windows 'article)
8653   (gnus-eval-in-buffer-window gnus-article-buffer
8654     (widen)
8655     (goto-char (point-max))
8656     (recenter -3)
8657     (when gnus-break-pages
8658       (when (re-search-backward page-delimiter nil t)
8659         (narrow-to-region (match-end 0) (point-max)))
8660       (gnus-narrow-to-page))))
8661
8662 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8663   "Truncate to LEN and quote all \"(\"'s in STRING."
8664   (gnus-replace-in-string (if (and len (> (length string) len))
8665                               (substring string 0 len)
8666                             string)
8667                           "[()]" "\\\\\\&"))
8668
8669 (defun gnus-summary-print-article (&optional filename n)
8670   "Generate and print a PostScript image of the process-marked (mail) articles.
8671
8672 If used interactively, print the current article if none are
8673 process-marked.  With prefix arg, prompt the user for the name of the
8674 file to save in.
8675
8676 When used from Lisp, accept two optional args FILENAME and N.  N means
8677 to print the next N articles.  If N is negative, print the N previous
8678 articles.  If N is nil and articles have been marked with the process
8679 mark, print these instead.
8680
8681 If the optional first argument FILENAME is nil, send the image to the
8682 printer.  If FILENAME is a string, save the PostScript image in a file with
8683 that name.  If FILENAME is a number, prompt the user for the name of the file
8684 to save in."
8685   (interactive (list (ps-print-preprint current-prefix-arg)))
8686   (dolist (article (gnus-summary-work-articles n))
8687     (gnus-summary-select-article nil nil 'pseudo article)
8688     (gnus-eval-in-buffer-window gnus-article-buffer
8689       (gnus-print-buffer))
8690     (gnus-summary-remove-process-mark article))
8691   (ps-despool filename))
8692
8693 (defun gnus-print-buffer ()
8694   (let ((buffer (generate-new-buffer " *print*")))
8695     (unwind-protect
8696         (progn
8697           (copy-to-buffer buffer (point-min) (point-max))
8698           (set-buffer buffer)
8699           (gnus-remove-text-with-property 'gnus-decoration)
8700           (when (gnus-visual-p 'article-highlight 'highlight)
8701             ;; Copy-to-buffer doesn't copy overlay.  So redo
8702             ;; highlight.
8703             (let ((gnus-article-buffer buffer))
8704               (gnus-article-highlight-citation t)
8705               (gnus-article-highlight-signature)
8706               (gnus-article-emphasize)
8707               (gnus-article-delete-invisible-text)))
8708           (let ((ps-left-header
8709                  (list
8710                   (concat "("
8711                           (gnus-summary-print-truncate-and-quote
8712                            (mail-header-subject gnus-current-headers)
8713                            66) ")")
8714                   (concat "("
8715                           (gnus-summary-print-truncate-and-quote
8716                            (mail-header-from gnus-current-headers)
8717                            45) ")")))
8718                 (ps-right-header
8719                  (list
8720                   "/pagenumberstring load"
8721                   (concat "("
8722                           (mail-header-date gnus-current-headers) ")"))))
8723             (gnus-run-hooks 'gnus-ps-print-hook)
8724             (save-excursion
8725               (if window-system
8726                   (ps-spool-buffer-with-faces)
8727                 (ps-spool-buffer)))))
8728       (kill-buffer buffer))))
8729
8730 (defun gnus-summary-show-article (&optional arg)
8731   "Force redisplaying of the current article.
8732 If ARG (the prefix) is a number, show the article with the charset
8733 defined in `gnus-summary-show-article-charset-alist', or the charset
8734 input.
8735 If ARG (the prefix) is non-nil and not a number, show the raw article
8736 without any article massaging functions being run.  Normally, the key
8737 strokes are `C-u g'."
8738   (interactive "P")
8739   (cond
8740    ((numberp arg)
8741     (gnus-summary-show-article t)
8742     (let ((gnus-newsgroup-charset
8743            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8744                (mm-read-coding-system
8745                 "View as charset: " ;; actually it is coding system.
8746                 (save-excursion
8747                   (set-buffer gnus-article-buffer)
8748                   (mm-detect-coding-region (point) (point-max))))))
8749           (gnus-newsgroup-ignored-charsets 'gnus-all))
8750       (gnus-summary-select-article nil 'force)
8751       (let ((deps gnus-newsgroup-dependencies)
8752             head header lines)
8753         (save-excursion
8754           (set-buffer gnus-original-article-buffer)
8755           (save-restriction
8756             (message-narrow-to-head)
8757             (setq head (buffer-string))
8758             (goto-char (point-min))
8759             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8760               (goto-char (point-max))
8761               (widen)
8762               (setq lines (1- (count-lines (point) (point-max))))))
8763           (with-temp-buffer
8764             (insert (format "211 %d Article retrieved.\n"
8765                             (cdr gnus-article-current)))
8766             (insert head)
8767             (if lines (insert (format "Lines: %d\n" lines)))
8768             (insert ".\n")
8769             (let ((nntp-server-buffer (current-buffer)))
8770               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8771         (gnus-data-set-header
8772          (gnus-data-find (cdr gnus-article-current))
8773          header)
8774         (gnus-summary-update-article-line
8775          (cdr gnus-article-current) header)
8776         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8777           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8778    ((not arg)
8779     ;; Select the article the normal way.
8780     (gnus-summary-select-article nil 'force))
8781    (t
8782     ;; We have to require this here to make sure that the following
8783     ;; dynamic binding isn't shadowed by autoloading.
8784     (require 'gnus-async)
8785     (require 'gnus-art)
8786     ;; Bind the article treatment functions to nil.
8787     (let ((gnus-have-all-headers t)
8788           gnus-article-prepare-hook
8789           gnus-article-decode-hook
8790           gnus-display-mime-function
8791           gnus-break-pages)
8792       ;; Destroy any MIME parts.
8793       (when (gnus-buffer-live-p gnus-article-buffer)
8794         (save-excursion
8795           (set-buffer gnus-article-buffer)
8796           (mm-destroy-parts gnus-article-mime-handles)
8797           ;; Set it to nil for safety reason.
8798           (setq gnus-article-mime-handle-alist nil)
8799           (setq gnus-article-mime-handles nil)))
8800       (gnus-summary-select-article nil 'force))))
8801   (gnus-summary-goto-subject gnus-current-article)
8802   (gnus-summary-position-point))
8803
8804 (defun gnus-summary-show-raw-article ()
8805   "Show the raw article without any article massaging functions being run."
8806   (interactive)
8807   (gnus-summary-show-article t))
8808
8809 (defun gnus-summary-verbose-headers (&optional arg)
8810   "Toggle permanent full header display.
8811 If ARG is a positive number, turn header display on.
8812 If ARG is a negative number, turn header display off."
8813   (interactive "P")
8814   (setq gnus-show-all-headers
8815         (cond ((or (not (numberp arg))
8816                    (zerop arg))
8817                (not gnus-show-all-headers))
8818               ((natnump arg)
8819                t)))
8820   (gnus-summary-show-article))
8821
8822 (defun gnus-summary-toggle-header (&optional arg)
8823   "Show the headers if they are hidden, or hide them if they are shown.
8824 If ARG is a positive number, show the entire header.
8825 If ARG is a negative number, hide the unwanted header lines."
8826   (interactive "P")
8827   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8828                      (get-buffer-window gnus-article-buffer t))))
8829     (with-current-buffer gnus-article-buffer
8830       (widen)
8831       (article-narrow-to-head)
8832       (let* ((buffer-read-only nil)
8833              (inhibit-point-motion-hooks t)
8834              (hidden (if (numberp arg)
8835                          (>= arg 0)
8836                        (gnus-article-hidden-text-p 'headers)))
8837              s e)
8838         (delete-region (point-min) (point-max))
8839         (with-current-buffer gnus-original-article-buffer
8840           (goto-char (setq s (point-min)))
8841           (setq e (if (search-forward "\n\n" nil t)
8842                       (1- (point))
8843                     (point-max))))
8844         (insert-buffer-substring gnus-original-article-buffer s e)
8845         (run-hooks 'gnus-article-decode-hook)
8846         (if hidden
8847             (let ((gnus-treat-hide-headers nil)
8848                   (gnus-treat-hide-boring-headers nil))
8849               (gnus-delete-wash-type 'headers)
8850               (gnus-treat-article 'head))
8851           (gnus-treat-article 'head))
8852         (widen)
8853         (if window
8854             (set-window-start window (goto-char (point-min))))
8855         (if gnus-break-pages
8856             (gnus-narrow-to-page)
8857           (when (gnus-visual-p 'page-marker)
8858             (let ((buffer-read-only nil))
8859               (gnus-remove-text-with-property 'gnus-prev)
8860               (gnus-remove-text-with-property 'gnus-next))))
8861         (gnus-set-mode-line 'article)))))
8862
8863 (defun gnus-summary-show-all-headers ()
8864   "Make all header lines visible."
8865   (interactive)
8866   (gnus-summary-toggle-header 1))
8867
8868 (defun gnus-summary-caesar-message (&optional arg)
8869   "Caesar rotate the current article by 13.
8870 The numerical prefix specifies how many places to rotate each letter
8871 forward."
8872   (interactive "P")
8873   (gnus-summary-select-article)
8874   (let ((mail-header-separator ""))
8875     (gnus-eval-in-buffer-window gnus-article-buffer
8876       (save-restriction
8877         (widen)
8878         (let ((start (window-start))
8879               buffer-read-only)
8880           (message-caesar-buffer-body arg)
8881           (set-window-start (get-buffer-window (current-buffer)) start))))))
8882
8883 (autoload 'unmorse-region "morse"
8884   "Convert morse coded text in region to ordinary ASCII text."
8885   t)
8886
8887 (defun gnus-summary-morse-message (&optional arg)
8888   "Morse decode the current article."
8889   (interactive "P")
8890   (gnus-summary-select-article)
8891   (let ((mail-header-separator ""))
8892     (gnus-eval-in-buffer-window gnus-article-buffer
8893       (save-excursion
8894         (save-restriction
8895           (widen)
8896           (let ((pos (window-start))
8897                 buffer-read-only)
8898             (goto-char (point-min))
8899             (when (message-goto-body)
8900               (gnus-narrow-to-body))
8901             (goto-char (point-min))
8902             (while (search-forward "·" (point-max) t)
8903               (replace-match "."))
8904             (unmorse-region (point-min) (point-max))
8905             (widen)
8906             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8907
8908 (defun gnus-summary-stop-page-breaking ()
8909   "Stop page breaking in the current article."
8910   (interactive)
8911   (gnus-summary-select-article)
8912   (gnus-eval-in-buffer-window gnus-article-buffer
8913     (widen)
8914     (when (gnus-visual-p 'page-marker)
8915       (let ((buffer-read-only nil))
8916         (gnus-remove-text-with-property 'gnus-prev)
8917         (gnus-remove-text-with-property 'gnus-next))
8918       (setq gnus-page-broken nil))))
8919
8920 (defun gnus-summary-move-article (&optional n to-newsgroup
8921                                             select-method action)
8922   "Move the current article to a different newsgroup.
8923 If N is a positive number, move the N next articles.
8924 If N is a negative number, move the N previous articles.
8925 If N is nil and any articles have been marked with the process mark,
8926 move those articles instead.
8927 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8928 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8929 re-spool using this method.
8930
8931 When called interactively with TO-NEWSGROUP being nil, the value of
8932 the variable `gnus-move-split-methods' is used for finding a default
8933 for the target newsgroup.
8934
8935 For this function to work, both the current newsgroup and the
8936 newsgroup that you want to move to have to support the `request-move'
8937 and `request-accept' functions.
8938
8939 ACTION can be either `move' (the default), `crosspost' or `copy'."
8940   (interactive "P")
8941   (unless action
8942     (setq action 'move))
8943   ;; Check whether the source group supports the required functions.
8944   (cond ((and (eq action 'move)
8945               (not (gnus-check-backend-function
8946                     'request-move-article gnus-newsgroup-name)))
8947          (error "The current group does not support article moving"))
8948         ((and (eq action 'crosspost)
8949               (not (gnus-check-backend-function
8950                     'request-replace-article gnus-newsgroup-name)))
8951          (error "The current group does not support article editing")))
8952   (let ((articles (gnus-summary-work-articles n))
8953         (prefix (if (gnus-check-backend-function
8954                      'request-move-article gnus-newsgroup-name)
8955                     (funcall gnus-move-group-prefix-function
8956                              gnus-newsgroup-name)
8957                   ""))
8958         (names '((move "Move" "Moving")
8959                  (copy "Copy" "Copying")
8960                  (crosspost "Crosspost" "Crossposting")))
8961         (copy-buf (save-excursion
8962                     (nnheader-set-temp-buffer " *copy article*")))
8963         art-group to-method new-xref article to-groups)
8964     (unless (assq action names)
8965       (error "Unknown action %s" action))
8966     ;; Read the newsgroup name.
8967     (when (and (not to-newsgroup)
8968                (not select-method))
8969       (if (and gnus-move-split-methods
8970                (not
8971                 (and (memq gnus-current-article articles)
8972                      (gnus-buffer-live-p gnus-original-article-buffer))))
8973           ;; When `gnus-move-split-methods' is non-nil, we have to
8974           ;; select an article to give `gnus-read-move-group-name' an
8975           ;; opportunity to suggest an appropriate default.  However,
8976           ;; we needn't render or mark the article.
8977           (let ((gnus-display-mime-function nil)
8978                 (gnus-article-prepare-hook nil)
8979                 (gnus-mark-article-hook nil))
8980             (gnus-summary-select-article nil nil nil (car articles))))
8981       (setq to-newsgroup
8982             (gnus-read-move-group-name
8983              (cadr (assq action names))
8984              (symbol-value (intern (format "gnus-current-%s-group" action)))
8985              articles prefix))
8986       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8987     (setq to-method (or select-method
8988                         (gnus-server-to-method
8989                          (gnus-group-method to-newsgroup))))
8990     ;; Check the method we are to move this article to...
8991     (unless (gnus-check-backend-function
8992              'request-accept-article (car to-method))
8993       (error "%s does not support article copying" (car to-method)))
8994     (unless (gnus-check-server to-method)
8995       (error "Can't open server %s" (car to-method)))
8996     (gnus-message 6 "%s to %s: %s..."
8997                   (caddr (assq action names))
8998                   (or (car select-method) to-newsgroup) articles)
8999     (while articles
9000       (setq article (pop articles))
9001       (setq
9002        art-group
9003        (cond
9004         ;; Move the article.
9005         ((eq action 'move)
9006          ;; Remove this article from future suppression.
9007          (gnus-dup-unsuppress-article article)
9008          (gnus-request-move-article
9009           article                       ; Article to move
9010           gnus-newsgroup-name           ; From newsgroup
9011           (nth 1 (gnus-find-method-for-group
9012                   gnus-newsgroup-name)) ; Server
9013           (list 'gnus-request-accept-article
9014                 to-newsgroup (list 'quote select-method)
9015                 (not articles) t)       ; Accept form
9016           (not articles)))              ; Only save nov last time
9017         ;; Copy the article.
9018         ((eq action 'copy)
9019          (save-excursion
9020            (set-buffer copy-buf)
9021            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9022              (gnus-request-accept-article
9023               to-newsgroup select-method (not articles) t))))
9024         ;; Crosspost the article.
9025         ((eq action 'crosspost)
9026          (let ((xref (message-tokenize-header
9027                       (mail-header-xref (gnus-summary-article-header article))
9028                       " ")))
9029            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9030                                   ":" (number-to-string article)))
9031            (unless xref
9032              (setq xref (list (system-name))))
9033            (setq new-xref
9034                  (concat
9035                   (mapconcat 'identity
9036                              (delete "Xref:" (delete new-xref xref))
9037                              " ")
9038                   " " new-xref))
9039            (save-excursion
9040              (set-buffer copy-buf)
9041              ;; First put the article in the destination group.
9042              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9043              (when (consp (setq art-group
9044                                 (gnus-request-accept-article
9045                                  to-newsgroup select-method (not articles))))
9046                (setq new-xref (concat new-xref " " (car art-group)
9047                                       ":"
9048                                       (number-to-string (cdr art-group))))
9049                ;; Now we have the new Xrefs header, so we insert
9050                ;; it and replace the new article.
9051                (nnheader-replace-header "Xref" new-xref)
9052                (gnus-request-replace-article
9053                 (cdr art-group) to-newsgroup (current-buffer))
9054                art-group))))))
9055       (cond
9056        ((not art-group)
9057         (gnus-message 1 "Couldn't %s article %s: %s"
9058                       (cadr (assq action names)) article
9059                       (nnheader-get-report (car to-method))))
9060        ((eq art-group 'junk)
9061         (when (eq action 'move)
9062           (gnus-summary-mark-article article gnus-canceled-mark)
9063           (gnus-message 4 "Deleted article %s" article)
9064           ;; run the delete hook
9065           (run-hook-with-args 'gnus-summary-article-delete-hook
9066                               action
9067                               (gnus-data-header
9068                                (assoc article (gnus-data-list nil)))
9069                               gnus-newsgroup-name nil
9070                               select-method)))
9071        (t
9072         (let* ((pto-group (gnus-group-prefixed-name
9073                            (car art-group) to-method))
9074                (info (gnus-get-info pto-group))
9075                (to-group (gnus-info-group info))
9076                to-marks)
9077           ;; Update the group that has been moved to.
9078           (when (and info
9079                      (memq action '(move copy)))
9080             (unless (member to-group to-groups)
9081               (push to-group to-groups))
9082
9083             (unless (memq article gnus-newsgroup-unreads)
9084               (push 'read to-marks)
9085               (gnus-info-set-read
9086                info (gnus-add-to-range (gnus-info-read info)
9087                                        (list (cdr art-group)))))
9088
9089             ;; See whether the article is to be put in the cache.
9090             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9091                              gnus-article-mark-lists
9092                            (delete '(expirable . expire)
9093                                    (copy-sequence gnus-article-mark-lists))))
9094                   (to-article (cdr art-group)))
9095
9096               ;; Enter the article into the cache in the new group,
9097               ;; if that is required.
9098               (when gnus-use-cache
9099                 (gnus-cache-possibly-enter-article
9100                  to-group to-article
9101                  (memq article gnus-newsgroup-marked)
9102                  (memq article gnus-newsgroup-dormant)
9103                  (memq article gnus-newsgroup-unreads)))
9104
9105               (when gnus-preserve-marks
9106                 ;; Copy any marks over to the new group.
9107                 (when (and (equal to-group gnus-newsgroup-name)
9108                            (not (memq article gnus-newsgroup-unreads)))
9109                   ;; Mark this article as read in this group.
9110                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9111                   (setcdr (gnus-active to-group) to-article)
9112                   (setcdr gnus-newsgroup-active to-article))
9113
9114                 (while marks
9115                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9116                     (when (memq article (symbol-value
9117                                          (intern (format "gnus-newsgroup-%s"
9118                                                          (caar marks)))))
9119                       (push (cdar marks) to-marks)
9120                       ;; If the other group is the same as this group,
9121                       ;; then we have to add the mark to the list.
9122                       (when (equal to-group gnus-newsgroup-name)
9123                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9124                              (cons to-article
9125                                    (symbol-value
9126                                     (intern (format "gnus-newsgroup-%s"
9127                                                     (caar marks)))))))
9128                       ;; Copy the marks to other group.
9129                       (gnus-add-marked-articles
9130                        to-group (cdar marks) (list to-article) info)))
9131                   (setq marks (cdr marks)))
9132
9133                 (gnus-request-set-mark
9134                  to-group (list (list (list to-article) 'add to-marks))))
9135
9136               (gnus-dribble-enter
9137                (concat "(gnus-group-set-info '"
9138                        (gnus-prin1-to-string (gnus-get-info to-group))
9139                        ")"))))
9140
9141           ;; Update the Xref header in this article to point to
9142           ;; the new crossposted article we have just created.
9143           (when (eq action 'crosspost)
9144             (save-excursion
9145               (set-buffer copy-buf)
9146               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9147               (nnheader-replace-header "Xref" new-xref)
9148               (gnus-request-replace-article
9149                article gnus-newsgroup-name (current-buffer))))
9150
9151           ;; run the move/copy/crosspost/respool hook
9152           (run-hook-with-args 'gnus-summary-article-move-hook
9153                               action
9154                               (gnus-data-header
9155                                (assoc article (gnus-data-list nil)))
9156                               gnus-newsgroup-name
9157                               to-newsgroup
9158                               select-method))
9159
9160         ;;;!!!Why is this necessary?
9161         (set-buffer gnus-summary-buffer)
9162         
9163         (gnus-summary-goto-subject article)
9164         (when (eq action 'move)
9165           (gnus-summary-mark-article article gnus-canceled-mark))))
9166       (gnus-summary-remove-process-mark article))
9167     ;; Re-activate all groups that have been moved to.
9168     (save-excursion
9169       (set-buffer gnus-group-buffer)
9170       (let ((gnus-group-marked to-groups))
9171         (gnus-group-get-new-news-this-group nil t)))
9172
9173     (gnus-kill-buffer copy-buf)
9174     (gnus-summary-position-point)
9175     (gnus-set-mode-line 'summary)))
9176
9177 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9178   "Copy the current article to some other group.
9179 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9180 When called interactively, if TO-NEWSGROUP is nil, use the value of
9181 the variable `gnus-move-split-methods' for finding a default target
9182 newsgroup.
9183 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9184 re-spool using this method."
9185   (interactive "P")
9186   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9187
9188 (defun gnus-summary-crosspost-article (&optional n)
9189   "Crosspost the current article to some other group."
9190   (interactive "P")
9191   (gnus-summary-move-article n nil nil 'crosspost))
9192
9193 (defcustom gnus-summary-respool-default-method nil
9194   "Default method type for respooling an article.
9195 If nil, use to the current newsgroup method."
9196   :type 'symbol
9197   :group 'gnus-summary-mail)
9198
9199 (defcustom gnus-summary-display-while-building nil
9200   "If non-nil, show and update the summary buffer as it's being built.
9201 If the value is t, update the buffer after every line is inserted.  If
9202 the value is an integer (N), update the display every N lines."
9203   :group 'gnus-thread
9204   :type '(choice (const :tag "off" nil)
9205                  number
9206                  (const :tag "frequently" t)))
9207
9208 (defun gnus-summary-respool-article (&optional n method)
9209   "Respool the current article.
9210 The article will be squeezed through the mail spooling process again,
9211 which means that it will be put in some mail newsgroup or other
9212 depending on `nnmail-split-methods'.
9213 If N is a positive number, respool the N next articles.
9214 If N is a negative number, respool the N previous articles.
9215 If N is nil and any articles have been marked with the process mark,
9216 respool those articles instead.
9217
9218 Respooling can be done both from mail groups and \"real\" newsgroups.
9219 In the former case, the articles in question will be moved from the
9220 current group into whatever groups they are destined to.  In the
9221 latter case, they will be copied into the relevant groups."
9222   (interactive
9223    (list current-prefix-arg
9224          (let* ((methods (gnus-methods-using 'respool))
9225                 (methname
9226                  (symbol-name (or gnus-summary-respool-default-method
9227                                   (car (gnus-find-method-for-group
9228                                         gnus-newsgroup-name)))))
9229                 (method
9230                  (gnus-completing-read-with-default
9231                   methname "What backend do you want to use when respooling?"
9232                   methods nil t nil 'gnus-mail-method-history))
9233                 ms)
9234            (cond
9235             ((zerop (length (setq ms (gnus-servers-using-backend
9236                                       (intern method)))))
9237              (list (intern method) ""))
9238             ((= 1 (length ms))
9239              (car ms))
9240             (t
9241              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9242                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9243                            ms-alist))))))))
9244   (unless method
9245     (error "No method given for respooling"))
9246   (if (assoc (symbol-name
9247               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9248              (gnus-methods-using 'respool))
9249       (gnus-summary-move-article n nil method)
9250     (gnus-summary-copy-article n nil method)))
9251
9252 (defun gnus-summary-import-article (file &optional edit)
9253   "Import an arbitrary file into a mail newsgroup."
9254   (interactive "fImport file: \nP")
9255   (let ((group gnus-newsgroup-name)
9256         (now (current-time))
9257         atts lines group-art)
9258     (unless (gnus-check-backend-function 'request-accept-article group)
9259       (error "%s does not support article importing" group))
9260     (or (file-readable-p file)
9261         (not (file-regular-p file))
9262         (error "Can't read %s" file))
9263     (save-excursion
9264       (set-buffer (gnus-get-buffer-create " *import file*"))
9265       (erase-buffer)
9266       (nnheader-insert-file-contents file)
9267       (goto-char (point-min))
9268       (if (nnheader-article-p)
9269           (save-restriction
9270             (goto-char (point-min))
9271             (search-forward "\n\n" nil t)
9272             (narrow-to-region (point-min) (1- (point)))
9273             (goto-char (point-min))
9274             (unless (re-search-forward "^date:" nil t)
9275               (goto-char (point-max))
9276               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9277        ;; This doesn't look like an article, so we fudge some headers.
9278         (setq atts (file-attributes file)
9279               lines (count-lines (point-min) (point-max)))
9280         (insert "From: " (read-string "From: ") "\n"
9281                 "Subject: " (read-string "Subject: ") "\n"
9282                 "Date: " (message-make-date (nth 5 atts)) "\n"
9283                 "Message-ID: " (message-make-message-id) "\n"
9284                 "Lines: " (int-to-string lines) "\n"
9285                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9286       (setq group-art (gnus-request-accept-article group nil t))
9287       (kill-buffer (current-buffer)))
9288     (setq gnus-newsgroup-active (gnus-activate-group group))
9289     (forward-line 1)
9290     (gnus-summary-goto-article (cdr group-art) nil t)
9291     (when edit
9292       (gnus-summary-edit-article))))
9293
9294 (defun gnus-summary-create-article ()
9295   "Create an article in a mail newsgroup."
9296   (interactive)
9297   (let ((group gnus-newsgroup-name)
9298         (now (current-time))
9299         group-art)
9300     (unless (gnus-check-backend-function 'request-accept-article group)
9301       (error "%s does not support article importing" group))
9302     (save-excursion
9303       (set-buffer (gnus-get-buffer-create " *import file*"))
9304       (erase-buffer)
9305       (goto-char (point-min))
9306       ;; This doesn't look like an article, so we fudge some headers.
9307       (insert "From: " (read-string "From: ") "\n"
9308               "Subject: " (read-string "Subject: ") "\n"
9309               "Date: " (message-make-date now) "\n"
9310               "Message-ID: " (message-make-message-id) "\n")
9311       (setq group-art (gnus-request-accept-article group nil t))
9312       (kill-buffer (current-buffer)))
9313     (setq gnus-newsgroup-active (gnus-activate-group group))
9314     (forward-line 1)
9315     (gnus-summary-goto-article (cdr group-art) nil t)
9316     (gnus-summary-edit-article)))
9317
9318 (defun gnus-summary-article-posted-p ()
9319   "Say whether the current (mail) article is available from news as well.
9320 This will be the case if the article has both been mailed and posted."
9321   (interactive)
9322   (let ((id (mail-header-references (gnus-summary-article-header)))
9323         (gnus-override-method (car (gnus-refer-article-methods))))
9324     (if (gnus-request-head id "")
9325         (gnus-message 2 "The current message was found on %s"
9326                       gnus-override-method)
9327       (gnus-message 2 "The current message couldn't be found on %s"
9328                     gnus-override-method)
9329       nil)))
9330
9331 (defun gnus-summary-expire-articles (&optional now)
9332   "Expire all articles that are marked as expirable in the current group."
9333   (interactive)
9334   (when (and (not gnus-group-is-exiting-without-update-p)
9335              (gnus-check-backend-function
9336               'request-expire-articles gnus-newsgroup-name))
9337     ;; This backend supports expiry.
9338     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9339            (expirable (if total
9340                           (progn
9341                             ;; We need to update the info for
9342                             ;; this group for `gnus-list-of-read-articles'
9343                             ;; to give us the right answer.
9344                             (gnus-run-hooks 'gnus-exit-group-hook)
9345                             (gnus-summary-update-info)
9346                             (gnus-list-of-read-articles gnus-newsgroup-name))
9347                         (setq gnus-newsgroup-expirable
9348                               (sort gnus-newsgroup-expirable '<))))
9349            (expiry-wait (if now 'immediate
9350                           (gnus-group-find-parameter
9351                            gnus-newsgroup-name 'expiry-wait)))
9352            (nnmail-expiry-target
9353             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9354                 nnmail-expiry-target))
9355            es)
9356       (when expirable
9357         ;; There are expirable articles in this group, so we run them
9358         ;; through the expiry process.
9359         (gnus-message 6 "Expiring articles...")
9360         (unless (gnus-check-group gnus-newsgroup-name)
9361           (error "Can't open server for %s" gnus-newsgroup-name))
9362         ;; The list of articles that weren't expired is returned.
9363         (save-excursion
9364           (if expiry-wait
9365               (let ((nnmail-expiry-wait-function nil)
9366                     (nnmail-expiry-wait expiry-wait))
9367                 (setq es (gnus-request-expire-articles
9368                           expirable gnus-newsgroup-name)))
9369             (setq es (gnus-request-expire-articles
9370                       expirable gnus-newsgroup-name)))
9371           (unless total
9372             (setq gnus-newsgroup-expirable es))
9373           ;; We go through the old list of expirable, and mark all
9374           ;; really expired articles as nonexistent.
9375           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9376             (let ((gnus-use-cache nil))
9377               (dolist (article expirable)
9378                 (when (and (not (memq article es))
9379                            (gnus-data-find article))
9380                   (gnus-summary-mark-article article gnus-canceled-mark)
9381                   (run-hook-with-args 'gnus-summary-article-expire-hook
9382                                       'delete
9383                                       (gnus-data-header
9384                                        (assoc article (gnus-data-list nil)))
9385                                       gnus-newsgroup-name
9386                                       nil
9387                                       nil))))))
9388         (gnus-message 6 "Expiring articles...done")))))
9389
9390 (defun gnus-summary-expire-articles-now ()
9391   "Expunge all expirable articles in the current group.
9392 This means that *all* articles that are marked as expirable will be
9393 deleted forever, right now."
9394   (interactive)
9395   (or gnus-expert-user
9396       (gnus-yes-or-no-p
9397        "Are you really, really, really sure you want to delete all these messages? ")
9398       (error "Phew!"))
9399   (gnus-summary-expire-articles t))
9400
9401 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9402 (defun gnus-summary-delete-article (&optional n)
9403   "Delete the N next (mail) articles.
9404 This command actually deletes articles.  This is not a marking
9405 command.  The article will disappear forever from your life, never to
9406 return.
9407
9408 If N is negative, delete backwards.
9409 If N is nil and articles have been marked with the process mark,
9410 delete these instead.
9411
9412 If `gnus-novice-user' is non-nil you will be asked for
9413 confirmation before the articles are deleted."
9414   (interactive "P")
9415   (unless (gnus-check-backend-function 'request-expire-articles
9416                                        gnus-newsgroup-name)
9417     (error "The current newsgroup does not support article deletion"))
9418   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9419     (error "Couldn't open server"))
9420   ;; Compute the list of articles to delete.
9421   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9422         (nnmail-expiry-target 'delete)
9423         not-deleted)
9424     (if (and gnus-novice-user
9425              (not (gnus-yes-or-no-p
9426                    (format "Do you really want to delete %s forever? "
9427                            (if (> (length articles) 1)
9428                                (format "these %s articles" (length articles))
9429                              "this article")))))
9430         ()
9431       ;; Delete the articles.
9432       (setq not-deleted (gnus-request-expire-articles
9433                          articles gnus-newsgroup-name 'force))
9434       (while articles
9435         (gnus-summary-remove-process-mark (car articles))
9436         ;; The backend might not have been able to delete the article
9437         ;; after all.
9438         (unless (memq (car articles) not-deleted)
9439           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9440         (let* ((article (car articles))
9441                (ghead  (gnus-data-header
9442                                     (assoc article (gnus-data-list nil)))))
9443           (run-hook-with-args 'gnus-summary-article-delete-hook
9444                               'delete ghead gnus-newsgroup-name nil
9445                               nil))
9446         (setq articles (cdr articles)))
9447       (when not-deleted
9448         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9449     (gnus-summary-position-point)
9450     (gnus-set-mode-line 'summary)
9451     not-deleted))
9452
9453 (defun gnus-summary-edit-article (&optional arg)
9454   "Edit the current article.
9455 This will have permanent effect only in mail groups.
9456 If ARG is nil, edit the decoded articles.
9457 If ARG is 1, edit the raw articles.
9458 If ARG is 2, edit the raw articles even in read-only groups.
9459 If ARG is 3, edit the articles with the current handles.
9460 Otherwise, allow editing of articles even in read-only
9461 groups."
9462   (interactive "P")
9463   (let (force raw current-handles)
9464     (cond
9465      ((null arg))
9466      ((eq arg 1)
9467       (setq raw t))
9468      ((eq arg 2)
9469       (setq raw t
9470             force t))
9471      ((eq arg 3)
9472       (setq current-handles
9473             (and (gnus-buffer-live-p gnus-article-buffer)
9474                  (with-current-buffer gnus-article-buffer
9475                    (prog1
9476                        gnus-article-mime-handles
9477                      (setq gnus-article-mime-handles nil))))))
9478      (t
9479       (setq force t)))
9480     (when (and raw (not force)
9481                (member gnus-newsgroup-name '("nndraft:delayed"
9482                                              "nndraft:drafts"
9483                                              "nndraft:queue")))
9484       (error "Can't edit the raw article in group %s"
9485              gnus-newsgroup-name))
9486     (save-excursion
9487       (set-buffer gnus-summary-buffer)
9488       (let ((mail-parse-charset gnus-newsgroup-charset)
9489             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9490         (gnus-set-global-variables)
9491         (when (and (not force)
9492                    (gnus-group-read-only-p))
9493           (error "The current newsgroup does not support article editing"))
9494         (gnus-summary-show-article t)
9495         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9496           (with-current-buffer gnus-article-buffer
9497             (mm-enable-multibyte)))
9498         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9499             (setq raw t))
9500         (gnus-article-edit-article
9501          (if raw 'ignore
9502            `(lambda ()
9503               (let ((mbl mml-buffer-list))
9504                 (setq mml-buffer-list nil)
9505                 (mime-to-mml ,'current-handles)
9506                 (let ((mbl1 mml-buffer-list))
9507                   (setq mml-buffer-list mbl)
9508                   (set (make-local-variable 'mml-buffer-list) mbl1))
9509                 (gnus-make-local-hook 'kill-buffer-hook)
9510                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9511          `(lambda (no-highlight)
9512             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9513                   (message-options message-options)
9514                   (message-options-set-recipient)
9515                   (mail-parse-ignored-charsets
9516                    ',gnus-newsgroup-ignored-charsets))
9517               ,(if (not raw) '(progn
9518                                 (mml-to-mime)
9519                                 (mml-destroy-buffers)
9520                                 (remove-hook 'kill-buffer-hook
9521                                              'mml-destroy-buffers t)
9522                                 (kill-local-variable 'mml-buffer-list)))
9523               (gnus-summary-edit-article-done
9524                ,(or (mail-header-references gnus-current-headers) "")
9525                ,(gnus-group-read-only-p)
9526                ,gnus-summary-buffer no-highlight))))))))
9527
9528 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9529
9530 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9531                                                  no-highlight)
9532   "Make edits to the current article permanent."
9533   (interactive)
9534   (save-excursion
9535    ;; The buffer restriction contains the entire article if it exists.
9536     (when (article-goto-body)
9537       (let ((lines (count-lines (point) (point-max)))
9538             (length (- (point-max) (point)))
9539             (case-fold-search t)
9540             (body (copy-marker (point))))
9541         (goto-char (point-min))
9542         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9543           (delete-region (match-beginning 1) (match-end 1))
9544           (insert (number-to-string length)))
9545         (goto-char (point-min))
9546         (when (re-search-forward
9547                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9548           (delete-region (match-beginning 1) (match-end 1))
9549           (insert (number-to-string length)))
9550         (goto-char (point-min))
9551         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9552           (delete-region (match-beginning 1) (match-end 1))
9553           (insert (number-to-string lines))))))
9554   ;; Replace the article.
9555   (let ((buf (current-buffer)))
9556     (with-temp-buffer
9557       (insert-buffer-substring buf)
9558
9559       (if (and (not read-only)
9560                (not (gnus-request-replace-article
9561                      (cdr gnus-article-current) (car gnus-article-current)
9562                      (current-buffer) t)))
9563           (error "Couldn't replace article")
9564         ;; Update the summary buffer.
9565         (if (and references
9566                  (equal (message-tokenize-header references " ")
9567                         (message-tokenize-header
9568                          (or (message-fetch-field "references") "") " ")))
9569             ;; We only have to update this line.
9570             (save-excursion
9571               (save-restriction
9572                 (message-narrow-to-head)
9573                 (let ((head (buffer-string))
9574                       header)
9575                   (with-temp-buffer
9576                     (insert (format "211 %d Article retrieved.\n"
9577                                     (cdr gnus-article-current)))
9578                     (insert head)
9579                     (insert ".\n")
9580                     (let ((nntp-server-buffer (current-buffer)))
9581                       (setq header (car (gnus-get-newsgroup-headers
9582                                          nil t))))
9583                     (save-excursion
9584                       (set-buffer gnus-summary-buffer)
9585                       (gnus-data-set-header
9586                        (gnus-data-find (cdr gnus-article-current))
9587                        header)
9588                       (gnus-summary-update-article-line
9589                        (cdr gnus-article-current) header)
9590                       (if (gnus-summary-goto-subject
9591                            (cdr gnus-article-current) nil t)
9592                           (gnus-summary-update-secondary-mark
9593                            (cdr gnus-article-current))))))))
9594           ;; Update threads.
9595           (set-buffer (or buffer gnus-summary-buffer))
9596           (gnus-summary-update-article (cdr gnus-article-current))
9597           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9598               (gnus-summary-update-secondary-mark
9599                (cdr gnus-article-current))))
9600         ;; Prettify the article buffer again.
9601         (unless no-highlight
9602           (save-excursion
9603             (set-buffer gnus-article-buffer)
9604             ;;;!!! Fix this -- article should be rehighlighted.
9605             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9606             (set-buffer gnus-original-article-buffer)
9607             (gnus-request-article
9608              (cdr gnus-article-current)
9609              (car gnus-article-current) (current-buffer))))
9610         ;; Prettify the summary buffer line.
9611         (when (gnus-visual-p 'summary-highlight 'highlight)
9612           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9613
9614 (defun gnus-summary-edit-wash (key)
9615   "Perform editing command KEY in the article buffer."
9616   (interactive
9617    (list
9618     (progn
9619       (message "%s" (concat (this-command-keys) "- "))
9620       (read-char))))
9621   (message "")
9622   (gnus-summary-edit-article)
9623   (execute-kbd-macro (concat (this-command-keys) key))
9624   (gnus-article-edit-done))
9625
9626 ;;; Respooling
9627
9628 (defun gnus-summary-respool-query (&optional silent trace)
9629   "Query where the respool algorithm would put this article."
9630   (interactive)
9631   (let (gnus-mark-article-hook)
9632     (gnus-summary-select-article)
9633     (save-excursion
9634       (set-buffer gnus-original-article-buffer)
9635       (let ((groups (nnmail-article-group 'identity trace)))
9636         (unless silent
9637           (if groups
9638               (message "This message would go to %s"
9639                        (mapconcat 'car groups ", "))
9640             (message "This message would go to no groups"))
9641           groups)))))
9642
9643 (defun gnus-summary-respool-trace ()
9644   "Trace where the respool algorithm would put this article.
9645 Display a buffer showing all fancy splitting patterns which matched."
9646   (interactive)
9647   (gnus-summary-respool-query nil t))
9648
9649 ;; Summary marking commands.
9650
9651 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9652   "Mark articles which has the same subject as read, and then select the next.
9653 If UNMARK is positive, remove any kind of mark.
9654 If UNMARK is negative, tick articles."
9655   (interactive "P")
9656   (when unmark
9657     (setq unmark (prefix-numeric-value unmark)))
9658   (let ((count
9659          (gnus-summary-mark-same-subject
9660           (gnus-summary-article-subject) unmark)))
9661     ;; Select next unread article.  If auto-select-same mode, should
9662     ;; select the first unread article.
9663     (gnus-summary-next-article t (and gnus-auto-select-same
9664                                       (gnus-summary-article-subject)))
9665     (gnus-message 7 "%d article%s marked as %s"
9666                   count (if (= count 1) " is" "s are")
9667                   (if unmark "unread" "read"))))
9668
9669 (defun gnus-summary-kill-same-subject (&optional unmark)
9670   "Mark articles which has the same subject as read.
9671 If UNMARK is positive, remove any kind of mark.
9672 If UNMARK is negative, tick articles."
9673   (interactive "P")
9674   (when unmark
9675     (setq unmark (prefix-numeric-value unmark)))
9676   (let ((count
9677          (gnus-summary-mark-same-subject
9678           (gnus-summary-article-subject) unmark)))
9679     ;; If marked as read, go to next unread subject.
9680     (when (null unmark)
9681       ;; Go to next unread subject.
9682       (gnus-summary-next-subject 1 t))
9683     (gnus-message 7 "%d articles are marked as %s"
9684                   count (if unmark "unread" "read"))))
9685
9686 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9687   "Mark articles with same SUBJECT as read, and return marked number.
9688 If optional argument UNMARK is positive, remove any kinds of marks.
9689 If optional argument UNMARK is negative, mark articles as unread instead."
9690   (let ((count 1))
9691     (save-excursion
9692       (cond
9693        ((null unmark)                   ; Mark as read.
9694         (while (and
9695                 (progn
9696                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9697                   (gnus-summary-show-thread) t)
9698                 (gnus-summary-find-subject subject))
9699           (setq count (1+ count))))
9700        ((> unmark 0)                    ; Tick.
9701         (while (and
9702                 (progn
9703                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9704                   (gnus-summary-show-thread) t)
9705                 (gnus-summary-find-subject subject))
9706           (setq count (1+ count))))
9707        (t                               ; Mark as unread.
9708         (while (and
9709                 (progn
9710                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9711                   (gnus-summary-show-thread) t)
9712                 (gnus-summary-find-subject subject))
9713           (setq count (1+ count)))))
9714       (gnus-set-mode-line 'summary)
9715       ;; Return the number of marked articles.
9716       count)))
9717
9718 (defun gnus-summary-mark-as-processable (n &optional unmark)
9719   "Set the process mark on the next N articles.
9720 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9721 the process mark instead.  The difference between N and the actual
9722 number of articles marked is returned."
9723   (interactive "P")
9724   (if (and (null n) (gnus-region-active-p))
9725       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9726     (setq n (prefix-numeric-value n))
9727     (let ((backward (< n 0))
9728           (n (abs n)))
9729       (while (and
9730               (> n 0)
9731               (if unmark
9732                   (gnus-summary-remove-process-mark
9733                    (gnus-summary-article-number))
9734                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9735               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9736         (setq n (1- n)))
9737       (when (/= 0 n)
9738         (gnus-message 7 "No more articles"))
9739       (gnus-summary-recenter)
9740       (gnus-summary-position-point)
9741       n)))
9742
9743 (defun gnus-summary-unmark-as-processable (n)
9744   "Remove the process mark from the next N articles.
9745 If N is negative, unmark backward instead.  The difference between N and
9746 the actual number of articles unmarked is returned."
9747   (interactive "P")
9748   (gnus-summary-mark-as-processable n t))
9749
9750 (defun gnus-summary-unmark-all-processable ()
9751   "Remove the process mark from all articles."
9752   (interactive)
9753   (save-excursion
9754     (while gnus-newsgroup-processable
9755       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9756   (gnus-summary-position-point))
9757
9758 (defun gnus-summary-add-mark (article type)
9759   "Mark ARTICLE with a mark of TYPE."
9760   (let ((vtype (car (assq type gnus-article-mark-lists)))
9761         var)
9762     (if (not vtype)
9763         (error "No such mark type: %s" type)
9764       (setq var (intern (format "gnus-newsgroup-%s" type)))
9765       (set var (cons article (symbol-value var)))
9766       (if (memq type '(processable cached replied forwarded recent saved))
9767           (gnus-summary-update-secondary-mark article)
9768         ;;; !!! This is bogus.  We should find out what primary
9769         ;;; !!! mark we want to set.
9770         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9771
9772 (defun gnus-summary-mark-as-expirable (n)
9773   "Mark N articles forward as expirable.
9774 If N is negative, mark backward instead.  The difference between N and
9775 the actual number of articles marked is returned."
9776   (interactive "p")
9777   (gnus-summary-mark-forward n gnus-expirable-mark))
9778
9779 (defun gnus-summary-mark-as-spam (n)
9780   "Mark N articles forward as spam.
9781 If N is negative, mark backward instead.  The difference between N and
9782 the actual number of articles marked is returned."
9783   (interactive "p")
9784   (gnus-summary-mark-forward n gnus-spam-mark))
9785
9786 (defun gnus-summary-mark-article-as-replied (article)
9787   "Mark ARTICLE as replied to and update the summary line.
9788 ARTICLE can also be a list of articles."
9789   (interactive (list (gnus-summary-article-number)))
9790   (let ((articles (if (listp article) article (list article))))
9791     (dolist (article articles)
9792       (unless (numberp article)
9793         (error "%s is not a number" article))
9794       (push article gnus-newsgroup-replied)
9795       (let ((buffer-read-only nil))
9796         (when (gnus-summary-goto-subject article nil t)
9797           (gnus-summary-update-secondary-mark article))))))
9798
9799 (defun gnus-summary-mark-article-as-forwarded (article)
9800   "Mark ARTICLE as forwarded and update the summary line.
9801 ARTICLE can also be a list of articles."
9802   (let ((articles (if (listp article) article (list article))))
9803     (dolist (article articles)
9804       (push article gnus-newsgroup-forwarded)
9805       (let ((buffer-read-only nil))
9806         (when (gnus-summary-goto-subject article nil t)
9807           (gnus-summary-update-secondary-mark article))))))
9808
9809 (defun gnus-summary-set-bookmark (article)
9810   "Set a bookmark in current article."
9811   (interactive (list (gnus-summary-article-number)))
9812   (when (or (not (get-buffer gnus-article-buffer))
9813             (not gnus-current-article)
9814             (not gnus-article-current)
9815             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9816     (error "No current article selected"))
9817   ;; Remove old bookmark, if one exists.
9818   (gnus-pull article gnus-newsgroup-bookmarks)
9819   ;; Set the new bookmark, which is on the form
9820   ;; (article-number . line-number-in-body).
9821   (push
9822    (cons article
9823          (with-current-buffer gnus-article-buffer
9824            (count-lines
9825             (min (point)
9826                  (save-excursion
9827                    (article-goto-body)
9828                    (point)))
9829             (point))))
9830    gnus-newsgroup-bookmarks)
9831   (gnus-message 6 "A bookmark has been added to the current article."))
9832
9833 (defun gnus-summary-remove-bookmark (article)
9834   "Remove the bookmark from the current article."
9835   (interactive (list (gnus-summary-article-number)))
9836   ;; Remove old bookmark, if one exists.
9837   (if (not (assq article gnus-newsgroup-bookmarks))
9838       (gnus-message 6 "No bookmark in current article.")
9839     (gnus-pull article gnus-newsgroup-bookmarks)
9840     (gnus-message 6 "Removed bookmark.")))
9841
9842 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9843 (defun gnus-summary-mark-as-dormant (n)
9844   "Mark N articles forward as dormant.
9845 If N is negative, mark backward instead.  The difference between N and
9846 the actual number of articles marked is returned."
9847   (interactive "p")
9848   (gnus-summary-mark-forward n gnus-dormant-mark))
9849
9850 (defun gnus-summary-set-process-mark (article)
9851   "Set the process mark on ARTICLE and update the summary line."
9852   (setq gnus-newsgroup-processable
9853         (cons article
9854               (delq article gnus-newsgroup-processable)))
9855   (when (gnus-summary-goto-subject article)
9856     (gnus-summary-show-thread)
9857     (gnus-summary-goto-subject article)
9858     (gnus-summary-update-secondary-mark article)))
9859
9860 (defun gnus-summary-remove-process-mark (article)
9861   "Remove the process mark from ARTICLE and update the summary line."
9862   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9863   (when (gnus-summary-goto-subject article)
9864     (gnus-summary-show-thread)
9865     (gnus-summary-goto-subject article)
9866     (gnus-summary-update-secondary-mark article)))
9867
9868 (defun gnus-summary-set-saved-mark (article)
9869   "Set the process mark on ARTICLE and update the summary line."
9870   (push article gnus-newsgroup-saved)
9871   (when (gnus-summary-goto-subject article)
9872     (gnus-summary-update-secondary-mark article)))
9873
9874 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9875   "Mark N articles as read forwards.
9876 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9877 The difference between N and the actual number of articles marked is
9878 returned.
9879 If NO-EXPIRE, auto-expiry will be inhibited."
9880   (interactive "p")
9881   (gnus-summary-show-thread)
9882   (let ((backward (< n 0))
9883         (gnus-summary-goto-unread
9884          (and gnus-summary-goto-unread
9885               (not (eq gnus-summary-goto-unread 'never))
9886               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9887                                     gnus-ticked-mark gnus-dormant-mark)))))
9888         (n (abs n))
9889         (mark (or mark gnus-del-mark)))
9890     (while (and (> n 0)
9891                 (gnus-summary-mark-article nil mark no-expire)
9892                 (zerop (gnus-summary-next-subject
9893                         (if backward -1 1)
9894                         (and gnus-summary-goto-unread
9895                              (not (eq gnus-summary-goto-unread 'never)))
9896                         t)))
9897       (setq n (1- n)))
9898     (when (/= 0 n)
9899       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9900     (gnus-summary-recenter)
9901     (gnus-summary-position-point)
9902     (gnus-set-mode-line 'summary)
9903     n))
9904
9905 (defun gnus-summary-mark-article-as-read (mark)
9906   "Mark the current article quickly as read with MARK."
9907   (let ((article (gnus-summary-article-number)))
9908     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9909     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9910     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9911     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9912     (push (cons article mark) gnus-newsgroup-reads)
9913     ;; Possibly remove from cache, if that is used.
9914     (when gnus-use-cache
9915       (gnus-cache-enter-remove-article article))
9916     ;; Allow the backend to change the mark.
9917     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9918     ;; Check for auto-expiry.
9919     (when (and gnus-newsgroup-auto-expire
9920                (memq mark gnus-auto-expirable-marks))
9921       (setq mark gnus-expirable-mark)
9922       ;; Let the backend know about the mark change.
9923       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9924       (push article gnus-newsgroup-expirable))
9925     ;; Set the mark in the buffer.
9926     (gnus-summary-update-mark mark 'unread)
9927     t))
9928
9929 (defun gnus-summary-mark-article-as-unread (mark)
9930   "Mark the current article quickly as unread with MARK."
9931   (let* ((article (gnus-summary-article-number))
9932          (old-mark (gnus-summary-article-mark article)))
9933     ;; Allow the backend to change the mark.
9934     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9935     (if (eq mark old-mark)
9936         t
9937       (if (<= article 0)
9938           (progn
9939             (gnus-error 1 "Can't mark negative article numbers")
9940             nil)
9941         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9942         (setq gnus-newsgroup-spam-marked
9943               (delq article gnus-newsgroup-spam-marked))
9944         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9945         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9946         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9947         (cond ((= mark gnus-ticked-mark)
9948                (setq gnus-newsgroup-marked
9949                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9950                                               article)))
9951               ((= mark gnus-spam-mark)
9952                (setq gnus-newsgroup-spam-marked
9953                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9954                                               article)))
9955               ((= mark gnus-dormant-mark)
9956                (setq gnus-newsgroup-dormant
9957                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9958                                               article)))
9959               (t
9960                (setq gnus-newsgroup-unreads
9961                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9962                                               article))))
9963         (gnus-pull article gnus-newsgroup-reads)
9964
9965         ;; See whether the article is to be put in the cache.
9966         (and gnus-use-cache
9967              (vectorp (gnus-summary-article-header article))
9968              (save-excursion
9969                (gnus-cache-possibly-enter-article
9970                 gnus-newsgroup-name article
9971                 (= mark gnus-ticked-mark)
9972                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9973
9974         ;; Fix the mark.
9975         (gnus-summary-update-mark mark 'unread)
9976         t))))
9977
9978 (defun gnus-summary-mark-article (&optional article mark no-expire)
9979   "Mark ARTICLE with MARK.  MARK can be any character.
9980 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9981 `??' (dormant) and `?E' (expirable).
9982 If MARK is nil, then the default character `?r' is used.
9983 If ARTICLE is nil, then the article on the current line will be
9984 marked.
9985 If NO-EXPIRE, auto-expiry will be inhibited."
9986   ;; The mark might be a string.
9987   (when (stringp mark)
9988     (setq mark (aref mark 0)))
9989   ;; If no mark is given, then we check auto-expiring.
9990   (when (null mark)
9991     (setq mark gnus-del-mark))
9992   (when (and (not no-expire)
9993              gnus-newsgroup-auto-expire
9994              (memq mark gnus-auto-expirable-marks))
9995     (setq mark gnus-expirable-mark))
9996   (let ((article (or article (gnus-summary-article-number)))
9997         (old-mark (gnus-summary-article-mark article)))
9998     ;; Allow the backend to change the mark.
9999     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10000     (if (eq mark old-mark)
10001         t
10002       (unless article
10003         (error "No article on current line"))
10004       (if (not (if (or (= mark gnus-unread-mark)
10005                        (= mark gnus-ticked-mark)
10006                        (= mark gnus-spam-mark)
10007                        (= mark gnus-dormant-mark))
10008                    (gnus-mark-article-as-unread article mark)
10009                  (gnus-mark-article-as-read article mark)))
10010           t
10011         ;; See whether the article is to be put in the cache.
10012         (and gnus-use-cache
10013              (not (= mark gnus-canceled-mark))
10014              (vectorp (gnus-summary-article-header article))
10015              (save-excursion
10016                (gnus-cache-possibly-enter-article
10017                 gnus-newsgroup-name article
10018                 (= mark gnus-ticked-mark)
10019                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10020
10021         (when (gnus-summary-goto-subject article nil t)
10022           (let ((buffer-read-only nil))
10023             (gnus-summary-show-thread)
10024             ;; Fix the mark.
10025             (gnus-summary-update-mark mark 'unread)
10026             t))))))
10027
10028 (defun gnus-summary-update-secondary-mark (article)
10029   "Update the secondary (read, process, cache) mark."
10030   (gnus-summary-update-mark
10031    (cond ((memq article gnus-newsgroup-processable)
10032           gnus-process-mark)
10033          ((memq article gnus-newsgroup-cached)
10034           gnus-cached-mark)
10035          ((memq article gnus-newsgroup-replied)
10036           gnus-replied-mark)
10037          ((memq article gnus-newsgroup-forwarded)
10038           gnus-forwarded-mark)
10039          ((memq article gnus-newsgroup-saved)
10040           gnus-saved-mark)
10041          ((memq article gnus-newsgroup-recent)
10042           gnus-recent-mark)
10043          ((memq article gnus-newsgroup-unseen)
10044           gnus-unseen-mark)
10045          (t gnus-no-mark))
10046    'replied)
10047   (when (gnus-visual-p 'summary-highlight 'highlight)
10048     (gnus-run-hooks 'gnus-summary-update-hook))
10049   t)
10050
10051 (defun gnus-summary-update-download-mark (article)
10052   "Update the download mark."
10053   (gnus-summary-update-mark
10054    (cond ((memq article gnus-newsgroup-undownloaded)
10055           gnus-undownloaded-mark)
10056          (gnus-newsgroup-agentized
10057           gnus-downloaded-mark)
10058          (t
10059           gnus-no-mark))
10060    'download)
10061   (gnus-summary-update-line t)
10062   t)
10063
10064 (defun gnus-summary-update-mark (mark type)
10065   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10066         (buffer-read-only nil))
10067     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10068     (when forward
10069       (when (looking-at "\r")
10070         (incf forward))
10071       (when (<= (+ forward (point)) (point-max))
10072         ;; Go to the right position on the line.
10073         (goto-char (+ forward (point)))
10074         ;; Replace the old mark with the new mark.
10075         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10076         ;; Optionally update the marks by some user rule.
10077         (when (eq type 'unread)
10078           (gnus-data-set-mark
10079            (gnus-data-find (gnus-summary-article-number)) mark)
10080           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10081
10082 (defun gnus-mark-article-as-read (article &optional mark)
10083   "Enter ARTICLE in the pertinent lists and remove it from others."
10084   ;; Make the article expirable.
10085   (let ((mark (or mark gnus-del-mark)))
10086     (setq gnus-newsgroup-expirable
10087           (if (= mark gnus-expirable-mark)
10088               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10089             (delq article gnus-newsgroup-expirable)))
10090     ;; Remove from unread and marked lists.
10091     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10092     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10093     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10094     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10095     (push (cons article mark) gnus-newsgroup-reads)
10096     ;; Possibly remove from cache, if that is used.
10097     (when gnus-use-cache
10098       (gnus-cache-enter-remove-article article))
10099     t))
10100
10101 (defun gnus-mark-article-as-unread (article &optional mark)
10102   "Enter ARTICLE in the pertinent lists and remove it from others."
10103   (let ((mark (or mark gnus-ticked-mark)))
10104     (if (<= article 0)
10105         (progn
10106           (gnus-error 1 "Can't mark negative article numbers")
10107           nil)
10108       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10109             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10110             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10111             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10112             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10113
10114       ;; Unsuppress duplicates?
10115       (when gnus-suppress-duplicates
10116         (gnus-dup-unsuppress-article article))
10117
10118       (cond ((= mark gnus-ticked-mark)
10119              (setq gnus-newsgroup-marked
10120                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10121             ((= mark gnus-spam-mark)
10122              (setq gnus-newsgroup-spam-marked
10123                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10124                                             article)))
10125             ((= mark gnus-dormant-mark)
10126              (setq gnus-newsgroup-dormant
10127                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10128             (t
10129              (setq gnus-newsgroup-unreads
10130                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10131       (gnus-pull article gnus-newsgroup-reads)
10132       t)))
10133
10134 (defalias 'gnus-summary-mark-as-unread-forward
10135   'gnus-summary-tick-article-forward)
10136 (make-obsolete 'gnus-summary-mark-as-unread-forward
10137                'gnus-summary-tick-article-forward)
10138 (defun gnus-summary-tick-article-forward (n)
10139   "Tick N articles forwards.
10140 If N is negative, tick backwards instead.
10141 The difference between N and the number of articles ticked is returned."
10142   (interactive "p")
10143   (gnus-summary-mark-forward n gnus-ticked-mark))
10144
10145 (defalias 'gnus-summary-mark-as-unread-backward
10146   'gnus-summary-tick-article-backward)
10147 (make-obsolete 'gnus-summary-mark-as-unread-backward
10148                'gnus-summary-tick-article-backward)
10149 (defun gnus-summary-tick-article-backward (n)
10150   "Tick N articles backwards.
10151 The difference between N and the number of articles ticked is returned."
10152   (interactive "p")
10153   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10154
10155 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10156 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10157 (defun gnus-summary-tick-article (&optional article clear-mark)
10158   "Mark current article as unread.
10159 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10160 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10161   (interactive)
10162   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10163                                        gnus-ticked-mark)))
10164
10165 (defun gnus-summary-mark-as-read-forward (n)
10166   "Mark N articles as read forwards.
10167 If N is negative, mark backwards instead.
10168 The difference between N and the actual number of articles marked is
10169 returned."
10170   (interactive "p")
10171   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10172
10173 (defun gnus-summary-mark-as-read-backward (n)
10174   "Mark the N articles as read backwards.
10175 The difference between N and the actual number of articles marked is
10176 returned."
10177   (interactive "p")
10178   (gnus-summary-mark-forward
10179    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10180
10181 (defun gnus-summary-mark-as-read (&optional article mark)
10182   "Mark current article as read.
10183 ARTICLE specifies the article to be marked as read.
10184 MARK specifies a string to be inserted at the beginning of the line."
10185   (gnus-summary-mark-article article mark))
10186
10187 (defun gnus-summary-clear-mark-forward (n)
10188   "Clear marks from N articles forward.
10189 If N is negative, clear backward instead.
10190 The difference between N and the number of marks cleared is returned."
10191   (interactive "p")
10192   (gnus-summary-mark-forward n gnus-unread-mark))
10193
10194 (defun gnus-summary-clear-mark-backward (n)
10195   "Clear marks from N articles backward.
10196 The difference between N and the number of marks cleared is returned."
10197   (interactive "p")
10198   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10199
10200 (defun gnus-summary-mark-unread-as-read ()
10201   "Intended to be used by `gnus-summary-mark-article-hook'."
10202   (when (memq gnus-current-article gnus-newsgroup-unreads)
10203     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10204
10205 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10206   "Intended to be used by `gnus-summary-mark-article-hook'."
10207   (let ((mark (gnus-summary-article-mark)))
10208     (when (or (gnus-unread-mark-p mark)
10209               (gnus-read-mark-p mark))
10210       (gnus-summary-mark-article gnus-current-article
10211                                  (or new-mark gnus-read-mark)))))
10212
10213 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10214   "Intended to be used by `gnus-summary-mark-article-hook'."
10215   (let ((mark (gnus-summary-article-mark)))
10216     (when (or (gnus-unread-mark-p mark)
10217               (gnus-read-mark-p mark))
10218       (gnus-summary-mark-article (gnus-summary-article-number)
10219                                  (or new-mark gnus-read-mark)))))
10220
10221 (defun gnus-summary-mark-unread-as-ticked ()
10222   "Intended to be used by `gnus-summary-mark-article-hook'."
10223   (when (memq gnus-current-article gnus-newsgroup-unreads)
10224     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10225
10226 (defun gnus-summary-mark-region-as-read (point mark all)
10227   "Mark all unread articles between point and mark as read.
10228 If given a prefix, mark all articles between point and mark as read,
10229 even ticked and dormant ones."
10230   (interactive "r\nP")
10231   (save-excursion
10232     (let (article)
10233       (goto-char point)
10234       (beginning-of-line)
10235       (while (and
10236               (< (point) mark)
10237               (progn
10238                 (when (or all
10239                           (memq (setq article (gnus-summary-article-number))
10240                                 gnus-newsgroup-unreads))
10241                   (gnus-summary-mark-article article gnus-del-mark))
10242                 t)
10243               (gnus-summary-find-next))))))
10244
10245 (defun gnus-summary-mark-below (score mark)
10246   "Mark articles with score less than SCORE with MARK."
10247   (interactive "P\ncMark: ")
10248   (setq score (if score
10249                   (prefix-numeric-value score)
10250                 (or gnus-summary-default-score 0)))
10251   (save-excursion
10252     (set-buffer gnus-summary-buffer)
10253     (goto-char (point-min))
10254     (while
10255         (progn
10256           (and (< (gnus-summary-article-score) score)
10257                (gnus-summary-mark-article nil mark))
10258           (gnus-summary-find-next)))))
10259
10260 (defun gnus-summary-kill-below (&optional score)
10261   "Mark articles with score below SCORE as read."
10262   (interactive "P")
10263   (gnus-summary-mark-below score gnus-killed-mark))
10264
10265 (defun gnus-summary-clear-above (&optional score)
10266   "Clear all marks from articles with score above SCORE."
10267   (interactive "P")
10268   (gnus-summary-mark-above score gnus-unread-mark))
10269
10270 (defun gnus-summary-tick-above (&optional score)
10271   "Tick all articles with score above SCORE."
10272   (interactive "P")
10273   (gnus-summary-mark-above score gnus-ticked-mark))
10274
10275 (defun gnus-summary-mark-above (score mark)
10276   "Mark articles with score over SCORE with MARK."
10277   (interactive "P\ncMark: ")
10278   (setq score (if score
10279                   (prefix-numeric-value score)
10280                 (or gnus-summary-default-score 0)))
10281   (save-excursion
10282     (set-buffer gnus-summary-buffer)
10283     (goto-char (point-min))
10284     (while (and (progn
10285                   (when (> (gnus-summary-article-score) score)
10286                     (gnus-summary-mark-article nil mark))
10287                   t)
10288                 (gnus-summary-find-next)))))
10289
10290 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10291 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10292 (defun gnus-summary-limit-include-expunged (&optional no-error)
10293   "Display all the hidden articles that were expunged for low scores."
10294   (interactive)
10295   (let ((buffer-read-only nil))
10296     (let ((scored gnus-newsgroup-scored)
10297           headers h)
10298       (while scored
10299         (unless (gnus-summary-article-header (caar scored))
10300           (and (setq h (gnus-number-to-header (caar scored)))
10301                (< (cdar scored) gnus-summary-expunge-below)
10302                (push h headers)))
10303         (setq scored (cdr scored)))
10304       (if (not headers)
10305           (when (not no-error)
10306             (error "No expunged articles hidden"))
10307         (goto-char (point-min))
10308         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10309         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10310         (mapcar (lambda (x) (push (mail-header-number x)
10311                                   gnus-newsgroup-limit))
10312                 headers)
10313         (gnus-summary-prepare-unthreaded (nreverse headers))
10314         (goto-char (point-min))
10315         (gnus-summary-position-point)
10316         t))))
10317
10318 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10319   "Mark all unread articles in this newsgroup as read.
10320 If prefix argument ALL is non-nil, ticked and dormant articles will
10321 also be marked as read.
10322 If QUIETLY is non-nil, no questions will be asked.
10323
10324 If TO-HERE is non-nil, it should be a point in the buffer.  All
10325 articles before (after, if REVERSE is set) this point will be marked
10326 as read.
10327
10328 Note that this function will only catch up the unread article
10329 in the current summary buffer limitation.
10330
10331 The number of articles marked as read is returned."
10332   (interactive "P")
10333   (prog1
10334       (save-excursion
10335         (when (or quietly
10336                   (not gnus-interactive-catchup) ;Without confirmation?
10337                   gnus-expert-user
10338                   (gnus-y-or-n-p
10339                    (if all
10340                        "Mark absolutely all articles as read? "
10341                      "Mark all unread articles as read? ")))
10342           (if (and not-mark
10343                    (not gnus-newsgroup-adaptive)
10344                    (not gnus-newsgroup-auto-expire)
10345                    (not gnus-suppress-duplicates)
10346                    (or (not gnus-use-cache)
10347                        (eq gnus-use-cache 'passive)))
10348               (progn
10349                 (when all
10350                   (setq gnus-newsgroup-marked nil
10351                         gnus-newsgroup-spam-marked nil
10352                         gnus-newsgroup-dormant nil))
10353                 (setq gnus-newsgroup-unreads
10354                       (gnus-sorted-nunion
10355                        (gnus-intersection gnus-newsgroup-unreads
10356                                           gnus-newsgroup-downloadable)
10357                        gnus-newsgroup-unfetched)))
10358             ;; We actually mark all articles as canceled, which we
10359             ;; have to do when using auto-expiry or adaptive scoring.
10360             (gnus-summary-show-all-threads)
10361             (if (and to-here reverse)
10362                 (progn
10363                   (goto-char to-here)
10364                   (gnus-summary-mark-current-read-and-unread-as-read
10365                    gnus-catchup-mark)
10366                   (while (gnus-summary-find-next (not all))
10367                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10368               (when (gnus-summary-first-subject (not all))
10369                 (while (and
10370                         (if to-here (< (point) to-here) t)
10371                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10372                         (gnus-summary-find-next (not all))))))
10373             (gnus-set-mode-line 'summary))
10374           t))
10375     (gnus-summary-position-point)))
10376
10377 (defun gnus-summary-catchup-to-here (&optional all)
10378   "Mark all unticked articles before the current one as read.
10379 If ALL is non-nil, also mark ticked and dormant articles as read."
10380   (interactive "P")
10381   (save-excursion
10382     (gnus-save-hidden-threads
10383       (let ((beg (point)))
10384         ;; We check that there are unread articles.
10385         (when (or all (gnus-summary-find-prev))
10386           (gnus-summary-catchup all t beg)))))
10387   (gnus-summary-position-point))
10388
10389 (defun gnus-summary-catchup-from-here (&optional all)
10390   "Mark all unticked articles after (and including) the current one as read.
10391 If ALL is non-nil, also mark ticked and dormant articles as read."
10392   (interactive "P")
10393   (save-excursion
10394     (gnus-save-hidden-threads
10395       (let ((beg (point)))
10396         ;; We check that there are unread articles.
10397         (when (or all (gnus-summary-find-next))
10398           (gnus-summary-catchup all t beg nil t)))))
10399   (gnus-summary-position-point))
10400
10401 (defun gnus-summary-catchup-all (&optional quietly)
10402   "Mark all articles in this newsgroup as read.
10403 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10404 instead, which marks only unread articles as read."
10405   (interactive "P")
10406   (gnus-summary-catchup t quietly))
10407
10408 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10409   "Mark all unread articles in this group as read, then exit.
10410 If prefix argument ALL is non-nil, all articles are marked as read.
10411 If QUIETLY is non-nil, no questions will be asked."
10412   (interactive "P")
10413   (when (gnus-summary-catchup all quietly nil 'fast)
10414     ;; Select next newsgroup or exit.
10415     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10416              (eq gnus-auto-select-next 'quietly))
10417         (gnus-summary-next-group nil)
10418       (gnus-summary-exit))))
10419
10420 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10421   "Mark all articles in this newsgroup as read, and then exit.
10422 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10423 instead, which marks only unread articles as read."
10424   (interactive "P")
10425   (gnus-summary-catchup-and-exit t quietly))
10426
10427 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10428   "Mark all articles in this group as read and select the next group.
10429 If given a prefix, mark all articles, unread as well as ticked, as
10430 read."
10431   (interactive "P")
10432   (save-excursion
10433     (gnus-summary-catchup all))
10434   (gnus-summary-next-group))
10435
10436 ;;;
10437 ;;; with article
10438 ;;;
10439
10440 (defmacro gnus-with-article (article &rest forms)
10441   "Select ARTICLE and perform FORMS in the original article buffer.
10442 Then replace the article with the result."
10443   `(progn
10444      ;; We don't want the article to be marked as read.
10445      (let (gnus-mark-article-hook)
10446        (gnus-summary-select-article t t nil ,article))
10447      (set-buffer gnus-original-article-buffer)
10448      ,@forms
10449      (if (not (gnus-check-backend-function
10450                'request-replace-article (car gnus-article-current)))
10451          (gnus-message 5 "Read-only group; not replacing")
10452        (unless (gnus-request-replace-article
10453                 ,article (car gnus-article-current)
10454                 (current-buffer) t)
10455          (error "Couldn't replace article")))
10456      ;; The cache and backlog have to be flushed somewhat.
10457      (when gnus-keep-backlog
10458        (gnus-backlog-remove-article
10459         (car gnus-article-current) (cdr gnus-article-current)))
10460      (when gnus-use-cache
10461        (gnus-cache-update-article
10462         (car gnus-article-current) (cdr gnus-article-current)))))
10463
10464 (put 'gnus-with-article 'lisp-indent-function 1)
10465 (put 'gnus-with-article 'edebug-form-spec '(form body))
10466
10467 ;; Thread-based commands.
10468
10469 (defun gnus-summary-articles-in-thread (&optional article)
10470   "Return a list of all articles in the current thread.
10471 If ARTICLE is non-nil, return all articles in the thread that starts
10472 with that article."
10473   (let* ((article (or article (gnus-summary-article-number)))
10474          (data (gnus-data-find-list article))
10475          (top-level (gnus-data-level (car data)))
10476          (top-subject
10477           (cond ((null gnus-thread-operation-ignore-subject)
10478                  (gnus-simplify-subject-re
10479                   (mail-header-subject (gnus-data-header (car data)))))
10480                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10481                  (gnus-simplify-subject-fuzzy
10482                   (mail-header-subject (gnus-data-header (car data)))))
10483                 (t nil)))
10484          (end-point (save-excursion
10485                       (if (gnus-summary-go-to-next-thread)
10486                           (point) (point-max))))
10487          articles)
10488     (while (and data
10489                 (< (gnus-data-pos (car data)) end-point))
10490       (when (or (not top-subject)
10491                 (string= top-subject
10492                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10493                              (gnus-simplify-subject-fuzzy
10494                               (mail-header-subject
10495                                (gnus-data-header (car data))))
10496                            (gnus-simplify-subject-re
10497                             (mail-header-subject
10498                              (gnus-data-header (car data)))))))
10499         (push (gnus-data-number (car data)) articles))
10500       (unless (and (setq data (cdr data))
10501                    (> (gnus-data-level (car data)) top-level))
10502         (setq data nil)))
10503     ;; Return the list of articles.
10504     (nreverse articles)))
10505
10506 (defun gnus-summary-rethread-current ()
10507   "Rethread the thread the current article is part of."
10508   (interactive)
10509   (let* ((gnus-show-threads t)
10510          (article (gnus-summary-article-number))
10511          (id (mail-header-id (gnus-summary-article-header)))
10512          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10513     (unless id
10514       (error "No article on the current line"))
10515     (gnus-rebuild-thread id)
10516     (gnus-summary-goto-subject article)))
10517
10518 (defun gnus-summary-reparent-thread ()
10519   "Make the current article child of the marked (or previous) article.
10520
10521 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10522 is non-nil or the Subject: of both articles are the same."
10523   (interactive)
10524   (unless (not (gnus-group-read-only-p))
10525     (error "The current newsgroup does not support article editing"))
10526   (unless (<= (length gnus-newsgroup-processable) 1)
10527     (error "No more than one article may be marked"))
10528   (save-window-excursion
10529     (let ((gnus-article-buffer " *reparent*")
10530           (current-article (gnus-summary-article-number))
10531           ;; First grab the marked article, otherwise one line up.
10532           (parent-article (if (not (null gnus-newsgroup-processable))
10533                               (car gnus-newsgroup-processable)
10534                             (save-excursion
10535                               (if (eq (forward-line -1) 0)
10536                                   (gnus-summary-article-number)
10537                                 (error "Beginning of summary buffer"))))))
10538       (unless (not (eq current-article parent-article))
10539         (error "An article may not be self-referential"))
10540       (let ((message-id (mail-header-id
10541                          (gnus-summary-article-header parent-article))))
10542         (unless (and message-id (not (equal message-id "")))
10543           (error "No message-id in desired parent"))
10544         (gnus-with-article current-article
10545           (save-restriction
10546             (goto-char (point-min))
10547             (message-narrow-to-head)
10548             (if (re-search-forward "^References: " nil t)
10549                 (progn
10550                   (re-search-forward "^[^ \t]" nil t)
10551                   (forward-line -1)
10552                   (end-of-line)
10553                   (insert " " message-id))
10554               (insert "References: " message-id "\n"))))
10555         (set-buffer gnus-summary-buffer)
10556         (gnus-summary-unmark-all-processable)
10557         (gnus-summary-update-article current-article)
10558         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10559             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10560         (gnus-summary-rethread-current)
10561         (gnus-message 3 "Article %d is now the child of article %d"
10562                       current-article parent-article)))))
10563
10564 (defun gnus-summary-toggle-threads (&optional arg)
10565   "Toggle showing conversation threads.
10566 If ARG is positive number, turn showing conversation threads on."
10567   (interactive "P")
10568   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10569     (setq gnus-show-threads
10570           (if (null arg) (not gnus-show-threads)
10571             (> (prefix-numeric-value arg) 0)))
10572     (gnus-summary-prepare)
10573     (gnus-summary-goto-subject current)
10574     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10575     (gnus-summary-position-point)))
10576
10577 (defun gnus-summary-show-all-threads ()
10578   "Show all threads."
10579   (interactive)
10580   (save-excursion
10581     (let ((buffer-read-only nil))
10582       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10583   (gnus-summary-position-point))
10584
10585 (defun gnus-summary-show-thread ()
10586   "Show thread subtrees.
10587 Returns nil if no thread was there to be shown."
10588   (interactive)
10589   (let ((buffer-read-only nil)
10590         (orig (point))
10591         (end (point-at-eol))
10592         ;; Leave point at bol
10593         (beg (progn (beginning-of-line) (point))))
10594     (prog1
10595         ;; Any hidden lines here?
10596         (search-forward "\r" end t)
10597       (subst-char-in-region beg end ?\^M ?\n t)
10598       (goto-char orig)
10599       (gnus-summary-position-point))))
10600
10601 (defun gnus-summary-maybe-hide-threads ()
10602   "If requested, hide the threads that should be hidden."
10603   (when (and gnus-show-threads
10604              gnus-thread-hide-subtree)
10605     (gnus-summary-hide-all-threads
10606      (if (or (consp gnus-thread-hide-subtree)
10607              (functionp gnus-thread-hide-subtree))
10608          (gnus-make-predicate gnus-thread-hide-subtree)
10609        nil))))
10610
10611 ;;; Hiding predicates.
10612
10613 (defun gnus-article-unread-p (header)
10614   (memq (mail-header-number header) gnus-newsgroup-unreads))
10615
10616 (defun gnus-article-unseen-p (header)
10617   (memq (mail-header-number header) gnus-newsgroup-unseen))
10618
10619 (defun gnus-map-articles (predicate articles)
10620   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10621   (apply 'gnus-or (mapcar predicate
10622                           (mapcar 'gnus-summary-article-header articles))))
10623
10624 (defun gnus-summary-hide-all-threads (&optional predicate)
10625   "Hide all thread subtrees.
10626 If PREDICATE is supplied, threads that satisfy this predicate
10627 will not be hidden."
10628   (interactive)
10629   (save-excursion
10630     (goto-char (point-min))
10631     (let ((end nil))
10632       (while (not end)
10633         (when (or (not predicate)
10634                   (gnus-map-articles
10635                    predicate (gnus-summary-article-children)))
10636             (gnus-summary-hide-thread))
10637         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10638   (gnus-summary-position-point))
10639
10640 (defun gnus-summary-hide-thread ()
10641   "Hide thread subtrees.
10642 If PREDICATE is supplied, threads that satisfy this predicate
10643 will not be hidden.
10644 Returns nil if no threads were there to be hidden."
10645   (interactive)
10646   (let ((buffer-read-only nil)
10647         (start (point))
10648         (article (gnus-summary-article-number)))
10649     (goto-char start)
10650     ;; Go forward until either the buffer ends or the subthread
10651     ;; ends.
10652     (when (and (not (eobp))
10653                (or (zerop (gnus-summary-next-thread 1 t))
10654                    (goto-char (point-max))))
10655       (prog1
10656           (if (and (> (point) start)
10657                    (search-backward "\n" start t))
10658               (progn
10659                 (subst-char-in-region start (point) ?\n ?\^M)
10660                 (gnus-summary-goto-subject article))
10661             (goto-char start)
10662             nil)))))
10663
10664 (defun gnus-summary-go-to-next-thread (&optional previous)
10665   "Go to the same level (or less) next thread.
10666 If PREVIOUS is non-nil, go to previous thread instead.
10667 Return the article number moved to, or nil if moving was impossible."
10668   (let ((level (gnus-summary-thread-level))
10669         (way (if previous -1 1))
10670         (beg (point)))
10671     (forward-line way)
10672     (while (and (not (eobp))
10673                 (< level (gnus-summary-thread-level)))
10674       (forward-line way))
10675     (if (eobp)
10676         (progn
10677           (goto-char beg)
10678           nil)
10679       (setq beg (point))
10680       (prog1
10681           (gnus-summary-article-number)
10682         (goto-char beg)))))
10683
10684 (defun gnus-summary-next-thread (n &optional silent)
10685   "Go to the same level next N'th thread.
10686 If N is negative, search backward instead.
10687 Returns the difference between N and the number of skips actually
10688 done.
10689
10690 If SILENT, don't output messages."
10691   (interactive "p")
10692   (let ((backward (< n 0))
10693         (n (abs n)))
10694     (while (and (> n 0)
10695                 (gnus-summary-go-to-next-thread backward))
10696       (decf n))
10697     (unless silent
10698       (gnus-summary-position-point))
10699     (when (and (not silent) (/= 0 n))
10700       (gnus-message 7 "No more threads"))
10701     n))
10702
10703 (defun gnus-summary-prev-thread (n)
10704   "Go to the same level previous N'th thread.
10705 Returns the difference between N and the number of skips actually
10706 done."
10707   (interactive "p")
10708   (gnus-summary-next-thread (- n)))
10709
10710 (defun gnus-summary-go-down-thread ()
10711   "Go down one level in the current thread."
10712   (let ((children (gnus-summary-article-children)))
10713     (when children
10714       (gnus-summary-goto-subject (car children)))))
10715
10716 (defun gnus-summary-go-up-thread ()
10717   "Go up one level in the current thread."
10718   (let ((parent (gnus-summary-article-parent)))
10719     (when parent
10720       (gnus-summary-goto-subject parent))))
10721
10722 (defun gnus-summary-down-thread (n)
10723   "Go down thread N steps.
10724 If N is negative, go up instead.
10725 Returns the difference between N and how many steps down that were
10726 taken."
10727   (interactive "p")
10728   (let ((up (< n 0))
10729         (n (abs n)))
10730     (while (and (> n 0)
10731                 (if up (gnus-summary-go-up-thread)
10732                   (gnus-summary-go-down-thread)))
10733       (setq n (1- n)))
10734     (gnus-summary-position-point)
10735     (when (/= 0 n)
10736       (gnus-message 7 "Can't go further"))
10737     n))
10738
10739 (defun gnus-summary-up-thread (n)
10740   "Go up thread N steps.
10741 If N is negative, go down instead.
10742 Returns the difference between N and how many steps down that were
10743 taken."
10744   (interactive "p")
10745   (gnus-summary-down-thread (- n)))
10746
10747 (defun gnus-summary-top-thread ()
10748   "Go to the top of the thread."
10749   (interactive)
10750   (while (gnus-summary-go-up-thread))
10751   (gnus-summary-article-number))
10752
10753 (defun gnus-summary-kill-thread (&optional unmark)
10754   "Mark articles under current thread as read.
10755 If the prefix argument is positive, remove any kinds of marks.
10756 If the prefix argument is negative, tick articles instead."
10757   (interactive "P")
10758   (when unmark
10759     (setq unmark (prefix-numeric-value unmark)))
10760   (let ((articles (gnus-summary-articles-in-thread)))
10761     (save-excursion
10762       ;; Expand the thread.
10763       (gnus-summary-show-thread)
10764       ;; Mark all the articles.
10765       (while articles
10766         (gnus-summary-goto-subject (car articles))
10767         (cond ((null unmark)
10768                (gnus-summary-mark-article-as-read gnus-killed-mark))
10769               ((> unmark 0)
10770                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10771               (t
10772                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10773         (setq articles (cdr articles))))
10774     ;; Hide killed subtrees.
10775     (and (null unmark)
10776          gnus-thread-hide-killed
10777          (gnus-summary-hide-thread))
10778     ;; If marked as read, go to next unread subject.
10779     (when (null unmark)
10780       ;; Go to next unread subject.
10781       (gnus-summary-next-subject 1 t)))
10782   (gnus-set-mode-line 'summary))
10783
10784 ;; Summary sorting commands
10785
10786 (defun gnus-summary-sort-by-number (&optional reverse)
10787   "Sort the summary buffer by article number.
10788 Argument REVERSE means reverse order."
10789   (interactive "P")
10790   (gnus-summary-sort 'number reverse))
10791
10792 (defun gnus-summary-sort-by-random (&optional reverse)
10793   "Randomize the order in the summary buffer.
10794 Argument REVERSE means to randomize in reverse order."
10795   (interactive "P")
10796   (gnus-summary-sort 'random reverse))
10797
10798 (defun gnus-summary-sort-by-author (&optional reverse)
10799   "Sort the summary buffer by author name alphabetically.
10800 If `case-fold-search' is non-nil, case of letters is ignored.
10801 Argument REVERSE means reverse order."
10802   (interactive "P")
10803   (gnus-summary-sort 'author reverse))
10804
10805 (defun gnus-summary-sort-by-subject (&optional reverse)
10806   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10807 If `case-fold-search' is non-nil, case of letters is ignored.
10808 Argument REVERSE means reverse order."
10809   (interactive "P")
10810   (gnus-summary-sort 'subject reverse))
10811
10812 (defun gnus-summary-sort-by-date (&optional reverse)
10813   "Sort the summary buffer by date.
10814 Argument REVERSE means reverse order."
10815   (interactive "P")
10816   (gnus-summary-sort 'date reverse))
10817
10818 (defun gnus-summary-sort-by-score (&optional reverse)
10819   "Sort the summary buffer by score.
10820 Argument REVERSE means reverse order."
10821   (interactive "P")
10822   (gnus-summary-sort 'score reverse))
10823
10824 (defun gnus-summary-sort-by-lines (&optional reverse)
10825   "Sort the summary buffer by the number of lines.
10826 Argument REVERSE means reverse order."
10827   (interactive "P")
10828   (gnus-summary-sort 'lines reverse))
10829
10830 (defun gnus-summary-sort-by-chars (&optional reverse)
10831   "Sort the summary buffer by article length.
10832 Argument REVERSE means reverse order."
10833   (interactive "P")
10834   (gnus-summary-sort 'chars reverse))
10835
10836 (defun gnus-summary-sort-by-original (&optional reverse)
10837   "Sort the summary buffer using the default sorting method.
10838 Argument REVERSE means reverse order."
10839   (interactive "P")
10840   (let* ((buffer-read-only)
10841          (gnus-summary-prepare-hook nil))
10842     ;; We do the sorting by regenerating the threads.
10843     (gnus-summary-prepare)
10844     ;; Hide subthreads if needed.
10845     (gnus-summary-maybe-hide-threads)))
10846
10847 (defun gnus-summary-sort (predicate reverse)
10848   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10849   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10850          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10851          (gnus-thread-sort-functions
10852           (if (not reverse)
10853               thread
10854             `(lambda (t1 t2)
10855                (,thread t2 t1))))
10856          (gnus-sort-gathered-threads-function
10857           gnus-thread-sort-functions)
10858          (gnus-article-sort-functions
10859           (if (not reverse)
10860               article
10861             `(lambda (t1 t2)
10862                (,article t2 t1))))
10863          (buffer-read-only)
10864          (gnus-summary-prepare-hook nil))
10865     ;; We do the sorting by regenerating the threads.
10866     (gnus-summary-prepare)
10867     ;; Hide subthreads if needed.
10868     (gnus-summary-maybe-hide-threads)))
10869
10870 ;; Summary saving commands.
10871
10872 (defun gnus-summary-save-article (&optional n not-saved)
10873   "Save the current article using the default saver function.
10874 If N is a positive number, save the N next articles.
10875 If N is a negative number, save the N previous articles.
10876 If N is nil and any articles have been marked with the process mark,
10877 save those articles instead.
10878 The variable `gnus-default-article-saver' specifies the saver function."
10879   (interactive "P")
10880   (let* ((articles (gnus-summary-work-articles n))
10881          (save-buffer (save-excursion
10882                         (nnheader-set-temp-buffer " *Gnus Save*")))
10883          (num (length articles))
10884          header file)
10885     (dolist (article articles)
10886       (setq header (gnus-summary-article-header article))
10887       (if (not (vectorp header))
10888           ;; This is a pseudo-article.
10889           (if (assq 'name header)
10890               (gnus-copy-file (cdr (assq 'name header)))
10891             (gnus-message 1 "Article %d is unsaveable" article))
10892         ;; This is a real article.
10893         (save-window-excursion
10894           (let ((gnus-display-mime-function nil)
10895                 (gnus-article-prepare-hook nil))
10896             (gnus-summary-select-article t nil nil article)))
10897         (save-excursion
10898           (set-buffer save-buffer)
10899           (erase-buffer)
10900           (insert-buffer-substring gnus-original-article-buffer))
10901         (setq file (gnus-article-save save-buffer file num))
10902         (gnus-summary-remove-process-mark article)
10903         (unless not-saved
10904           (gnus-summary-set-saved-mark article))))
10905     (gnus-kill-buffer save-buffer)
10906     (gnus-summary-position-point)
10907     (gnus-set-mode-line 'summary)
10908     n))
10909
10910 (defun gnus-summary-pipe-output (&optional arg headers)
10911   "Pipe the current article to a subprocess.
10912 If N is a positive number, pipe the N next articles.
10913 If N is a negative number, pipe the N previous articles.
10914 If N is nil and any articles have been marked with the process mark,
10915 pipe those articles instead.
10916 If HEADERS (the symbolic prefix), include the headers, too."
10917   (interactive (gnus-interactive "P\ny"))
10918   (require 'gnus-art)
10919   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10920         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10921     (gnus-summary-save-article arg t))
10922   (let ((buffer (get-buffer "*Shell Command Output*")))
10923     (when (and buffer
10924                (not (zerop (buffer-size buffer))))
10925       (gnus-configure-windows 'pipe))))
10926
10927 (defun gnus-summary-save-article-mail (&optional arg)
10928   "Append the current article to an mail file.
10929 If N is a positive number, save the N next articles.
10930 If N is a negative number, save the N previous articles.
10931 If N is nil and any articles have been marked with the process mark,
10932 save those articles instead."
10933   (interactive "P")
10934   (require 'gnus-art)
10935   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10936     (gnus-summary-save-article arg)))
10937
10938 (defun gnus-summary-save-article-rmail (&optional arg)
10939   "Append the current article to an rmail file.
10940 If N is a positive number, save the N next articles.
10941 If N is a negative number, save the N previous articles.
10942 If N is nil and any articles have been marked with the process mark,
10943 save those articles instead."
10944   (interactive "P")
10945   (require 'gnus-art)
10946   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10947     (gnus-summary-save-article arg)))
10948
10949 (defun gnus-summary-save-article-file (&optional arg)
10950   "Append the current article to a file.
10951 If N is a positive number, save the N next articles.
10952 If N is a negative number, save the N previous articles.
10953 If N is nil and any articles have been marked with the process mark,
10954 save those articles instead."
10955   (interactive "P")
10956   (require 'gnus-art)
10957   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10958     (gnus-summary-save-article arg)))
10959
10960 (defun gnus-summary-write-article-file (&optional arg)
10961   "Write the current article to a file, deleting the previous file.
10962 If N is a positive number, save the N next articles.
10963 If N is a negative number, save the N previous articles.
10964 If N is nil and any articles have been marked with the process mark,
10965 save those articles instead."
10966   (interactive "P")
10967   (require 'gnus-art)
10968   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10969     (gnus-summary-save-article arg)))
10970
10971 (defun gnus-summary-save-article-body-file (&optional arg)
10972   "Append the current article body to a file.
10973 If N is a positive number, save the N next articles.
10974 If N is a negative number, save the N previous articles.
10975 If N is nil and any articles have been marked with the process mark,
10976 save those articles instead."
10977   (interactive "P")
10978   (require 'gnus-art)
10979   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10980     (gnus-summary-save-article arg)))
10981
10982 (defun gnus-summary-muttprint (&optional arg)
10983   "Print the current article using Muttprint.
10984 If N is a positive number, save the N next articles.
10985 If N is a negative number, save the N previous articles.
10986 If N is nil and any articles have been marked with the process mark,
10987 save those articles instead."
10988   (interactive "P")
10989   (require 'gnus-art)
10990   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10991     (gnus-summary-save-article arg t)))
10992
10993 (defun gnus-summary-pipe-message (program)
10994   "Pipe the current article through PROGRAM."
10995   (interactive "sProgram: ")
10996   (gnus-summary-select-article)
10997   (let ((mail-header-separator ""))
10998     (gnus-eval-in-buffer-window gnus-article-buffer
10999       (save-restriction
11000         (widen)
11001         (let ((start (window-start))
11002               buffer-read-only)
11003           (message-pipe-buffer-body program)
11004           (set-window-start (get-buffer-window (current-buffer)) start))))))
11005
11006 (defun gnus-get-split-value (methods)
11007   "Return a value based on the split METHODS."
11008   (let (split-name method result match)
11009     (when methods
11010       (save-excursion
11011         (set-buffer gnus-original-article-buffer)
11012         (save-restriction
11013           (nnheader-narrow-to-headers)
11014           (while (and methods (not split-name))
11015             (goto-char (point-min))
11016             (setq method (pop methods))
11017             (setq match (car method))
11018             (when (cond
11019                    ((stringp match)
11020                     ;; Regular expression.
11021                     (ignore-errors
11022                       (re-search-forward match nil t)))
11023                    ((functionp match)
11024                     ;; Function.
11025                     (save-restriction
11026                       (widen)
11027                       (setq result (funcall match gnus-newsgroup-name))))
11028                    ((consp match)
11029                     ;; Form.
11030                     (save-restriction
11031                       (widen)
11032                       (setq result (eval match)))))
11033               (setq split-name (cdr method))
11034               (cond ((stringp result)
11035                      (push (expand-file-name
11036                             result gnus-article-save-directory)
11037                            split-name))
11038                     ((consp result)
11039                      (setq split-name (append result split-name)))))))))
11040     (nreverse split-name)))
11041
11042 (defun gnus-valid-move-group-p (group)
11043   (and (boundp group)
11044        (symbol-name group)
11045        (symbol-value group)
11046        (gnus-get-function (gnus-find-method-for-group
11047                            (symbol-name group)) 'request-accept-article t)))
11048
11049 (defun gnus-read-move-group-name (prompt default articles prefix)
11050   "Read a group name."
11051   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11052          (minibuffer-confirm-incomplete nil) ; XEmacs
11053          (prom
11054           (format "%s %s to:"
11055                   prompt
11056                   (if (> (length articles) 1)
11057                       (format "these %d articles" (length articles))
11058                     "this article")))
11059          (to-newsgroup
11060           (cond
11061            ((null split-name)
11062             (gnus-completing-read-with-default
11063              default prom
11064              gnus-active-hashtb
11065              'gnus-valid-move-group-p
11066              nil prefix
11067              'gnus-group-history))
11068            ((= 1 (length split-name))
11069             (gnus-completing-read-with-default
11070              (car split-name) prom
11071              gnus-active-hashtb
11072              'gnus-valid-move-group-p
11073              nil nil
11074              'gnus-group-history))
11075            (t
11076             (gnus-completing-read-with-default
11077              nil prom
11078              (mapcar 'list (nreverse split-name))
11079              nil nil nil
11080              'gnus-group-history))))
11081          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11082     (when to-newsgroup
11083       (if (or (string= to-newsgroup "")
11084               (string= to-newsgroup prefix))
11085           (setq to-newsgroup default))
11086       (unless to-newsgroup
11087         (error "No group name entered"))
11088       (or (gnus-active to-newsgroup)
11089           (gnus-activate-group to-newsgroup nil nil to-method)
11090           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11091                                      to-newsgroup))
11092               (or (and (gnus-request-create-group to-newsgroup to-method)
11093                        (gnus-activate-group
11094                         to-newsgroup nil nil to-method)
11095                        (gnus-subscribe-group to-newsgroup))
11096                   (error "Couldn't create group %s" to-newsgroup)))
11097           (error "No such group: %s" to-newsgroup)))
11098     to-newsgroup))
11099
11100 (defun gnus-summary-save-parts (type dir n &optional reverse)
11101   "Save parts matching TYPE to DIR.
11102 If REVERSE, save parts that do not match TYPE."
11103   (interactive
11104    (list (read-string "Save parts of type: "
11105                       (or (car gnus-summary-save-parts-type-history)
11106                           gnus-summary-save-parts-default-mime)
11107                       'gnus-summary-save-parts-type-history)
11108          (setq gnus-summary-save-parts-last-directory
11109                (read-file-name "Save to directory: "
11110                                gnus-summary-save-parts-last-directory
11111                                nil t))
11112          current-prefix-arg))
11113   (gnus-summary-iterate n
11114     (let ((gnus-display-mime-function nil)
11115           (gnus-inhibit-treatment t))
11116       (gnus-summary-select-article))
11117     (save-excursion
11118       (set-buffer gnus-article-buffer)
11119       (let ((handles (or gnus-article-mime-handles
11120                          (mm-dissect-buffer nil gnus-article-loose-mime)
11121                          (and gnus-article-emulate-mime
11122                               (mm-uu-dissect)))))
11123         (when handles
11124           (gnus-summary-save-parts-1 type dir handles reverse)
11125           (unless gnus-article-mime-handles ;; Don't destroy this case.
11126             (mm-destroy-parts handles)))))))
11127
11128 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11129   (if (stringp (car handle))
11130       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11131               (cdr handle))
11132     (when (if reverse
11133               (not (string-match type (mm-handle-media-type handle)))
11134             (string-match type (mm-handle-media-type handle)))
11135       (let ((file (expand-file-name
11136                    (gnus-map-function
11137                     mm-file-name-rewrite-functions
11138                     (file-name-nondirectory
11139                      (or
11140                       (mail-content-type-get
11141                        (mm-handle-disposition handle) 'filename)
11142                       (mail-content-type-get
11143                        (mm-handle-type handle) 'name)
11144                       (concat gnus-newsgroup-name
11145                               "." (number-to-string
11146                                    (cdr gnus-article-current))))))
11147                    dir)))
11148         (unless (file-exists-p file)
11149           (mm-save-part-to-file handle file))))))
11150
11151 ;; Summary extract commands
11152
11153 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11154   (let ((buffer-read-only nil)
11155         (article (gnus-summary-article-number))
11156         after-article b e)
11157     (unless (gnus-summary-goto-subject article)
11158       (error "No such article: %d" article))
11159     (gnus-summary-position-point)
11160     ;; If all commands are to be bunched up on one line, we collect
11161     ;; them here.
11162     (unless gnus-view-pseudos-separately
11163       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11164             files action)
11165         (while ps
11166           (setq action (cdr (assq 'action (car ps))))
11167           (setq files (list (cdr (assq 'name (car ps)))))
11168           (while (and ps (cdr ps)
11169                       (string= (or action "1")
11170                                (or (cdr (assq 'action (cadr ps))) "2")))
11171             (push (cdr (assq 'name (cadr ps))) files)
11172             (setcdr ps (cddr ps)))
11173           (when files
11174             (when (not (string-match "%s" action))
11175               (push " " files))
11176             (push " " files)
11177             (when (assq 'execute (car ps))
11178               (setcdr (assq 'execute (car ps))
11179                       (funcall (if (string-match "%s" action)
11180                                    'format 'concat)
11181                                action
11182                                (mapconcat
11183                                 (lambda (f)
11184                                   (if (equal f " ")
11185                                       f
11186                                     (shell-quote-argument f)))
11187                                 files " ")))))
11188           (setq ps (cdr ps)))))
11189     (if (and gnus-view-pseudos (not not-view))
11190         (while pslist
11191           (when (assq 'execute (car pslist))
11192             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11193                                   (eq gnus-view-pseudos 'not-confirm)))
11194           (setq pslist (cdr pslist)))
11195       (save-excursion
11196         (while pslist
11197           (setq after-article (or (cdr (assq 'article (car pslist)))
11198                                   (gnus-summary-article-number)))
11199           (gnus-summary-goto-subject after-article)
11200           (forward-line 1)
11201           (setq b (point))
11202           (insert "    " (file-name-nondirectory
11203                           (cdr (assq 'name (car pslist))))
11204                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11205           (setq e (point))
11206           (forward-line -1)             ; back to `b'
11207           (gnus-add-text-properties
11208            b (1- e) (list 'gnus-number gnus-reffed-article-number
11209                           gnus-mouse-face-prop gnus-mouse-face))
11210           (gnus-data-enter
11211            after-article gnus-reffed-article-number
11212            gnus-unread-mark b (car pslist) 0 (- e b))
11213           (setq gnus-newsgroup-unreads
11214                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11215                                          gnus-reffed-article-number))
11216           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11217           (setq pslist (cdr pslist)))))))
11218
11219 (defun gnus-pseudos< (p1 p2)
11220   (let ((c1 (cdr (assq 'action p1)))
11221         (c2 (cdr (assq 'action p2))))
11222     (and c1 c2 (string< c1 c2))))
11223
11224 (defun gnus-request-pseudo-article (props)
11225   (cond ((assq 'execute props)
11226          (gnus-execute-command (cdr (assq 'execute props)))))
11227   (let ((gnus-current-article (gnus-summary-article-number)))
11228     (gnus-run-hooks 'gnus-mark-article-hook)))
11229
11230 (defun gnus-execute-command (command &optional automatic)
11231   (save-excursion
11232     (gnus-article-setup-buffer)
11233     (set-buffer gnus-article-buffer)
11234     (setq buffer-read-only nil)
11235     (let ((command (if automatic command
11236                      (read-string "Command: " (cons command 0)))))
11237       (erase-buffer)
11238       (insert "$ " command "\n\n")
11239       (if gnus-view-pseudo-asynchronously
11240           (start-process "gnus-execute" (current-buffer) shell-file-name
11241                          shell-command-switch command)
11242         (call-process shell-file-name nil t nil
11243                       shell-command-switch command)))))
11244
11245 ;; Summary kill commands.
11246
11247 (defun gnus-summary-edit-global-kill (article)
11248   "Edit the \"global\" kill file."
11249   (interactive (list (gnus-summary-article-number)))
11250   (gnus-group-edit-global-kill article))
11251
11252 (defun gnus-summary-edit-local-kill ()
11253   "Edit a local kill file applied to the current newsgroup."
11254   (interactive)
11255   (setq gnus-current-headers (gnus-summary-article-header))
11256   (gnus-group-edit-local-kill
11257    (gnus-summary-article-number) gnus-newsgroup-name))
11258
11259 ;;; Header reading.
11260
11261 (defun gnus-read-header (id &optional header)
11262   "Read the headers of article ID and enter them into the Gnus system."
11263   (let ((group gnus-newsgroup-name)
11264         (gnus-override-method
11265          (or
11266           gnus-override-method
11267           (and (gnus-news-group-p gnus-newsgroup-name)
11268                (car (gnus-refer-article-methods)))))
11269         where)
11270     ;; First we check to see whether the header in question is already
11271     ;; fetched.
11272     (if (stringp id)
11273         ;; This is a Message-ID.
11274         (setq header (or header (gnus-id-to-header id)))
11275       ;; This is an article number.
11276       (setq header (or header (gnus-summary-article-header id))))
11277     (if (and header
11278              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11279         ;; We have found the header.
11280         header
11281       ;; We have to really fetch the header to this article.
11282       (save-excursion
11283         (set-buffer nntp-server-buffer)
11284         (when (setq where (gnus-request-head id group))
11285           (nnheader-fold-continuation-lines)
11286           (goto-char (point-max))
11287           (insert ".\n")
11288           (goto-char (point-min))
11289           (insert "211 ")
11290           (princ (cond
11291                   ((numberp id) id)
11292                   ((cdr where) (cdr where))
11293                   (header (mail-header-number header))
11294                   (t gnus-reffed-article-number))
11295                  (current-buffer))
11296           (insert " Article retrieved.\n"))
11297         (if (or (not where)
11298                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11299             ()                          ; Malformed head.
11300           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11301             (when (and (stringp id)
11302                        (not (string= (gnus-group-real-name group)
11303                                      (car where))))
11304               ;; If we fetched by Message-ID and the article came
11305               ;; from a different group, we fudge some bogus article
11306               ;; numbers for this article.
11307               (mail-header-set-number header gnus-reffed-article-number))
11308             (save-excursion
11309               (set-buffer gnus-summary-buffer)
11310               (decf gnus-reffed-article-number)
11311               (gnus-remove-header (mail-header-number header))
11312               (push header gnus-newsgroup-headers)
11313               (setq gnus-current-headers header)
11314               (push (mail-header-number header) gnus-newsgroup-limit)))
11315           header)))))
11316
11317 (defun gnus-remove-header (number)
11318   "Remove header NUMBER from `gnus-newsgroup-headers'."
11319   (if (and gnus-newsgroup-headers
11320            (= number (mail-header-number (car gnus-newsgroup-headers))))
11321       (pop gnus-newsgroup-headers)
11322     (let ((headers gnus-newsgroup-headers))
11323       (while (and (cdr headers)
11324                   (not (= number (mail-header-number (cadr headers)))))
11325         (pop headers))
11326       (when (cdr headers)
11327         (setcdr headers (cddr headers))))))
11328
11329 ;;;
11330 ;;; summary highlights
11331 ;;;
11332
11333 (defun gnus-highlight-selected-summary ()
11334   "Highlight selected article in summary buffer."
11335   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11336   (when gnus-summary-selected-face
11337     (save-excursion
11338       (let* ((beg (point-at-bol))
11339              (end (point-at-eol))
11340              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11341              (from (if (get-text-property beg gnus-mouse-face-prop)
11342                        beg
11343                      (or (next-single-property-change
11344                           beg gnus-mouse-face-prop nil end)
11345                          beg)))
11346              (to
11347               (if (= from end)
11348                   (- from 2)
11349                 (or (next-single-property-change
11350                      from gnus-mouse-face-prop nil end)
11351                     end))))
11352         ;; If no mouse-face prop on line we will have to = from = end,
11353         ;; so we highlight the entire line instead.
11354         (when (= (+ to 2) from)
11355           (setq from beg)
11356           (setq to end))
11357         (if gnus-newsgroup-selected-overlay
11358             ;; Move old overlay.
11359             (gnus-move-overlay
11360              gnus-newsgroup-selected-overlay from to (current-buffer))
11361           ;; Create new overlay.
11362           (gnus-overlay-put
11363            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11364            'face gnus-summary-selected-face))))))
11365
11366 (defvar gnus-summary-highlight-line-cached nil)
11367 (defvar gnus-summary-highlight-line-trigger nil)
11368
11369 (defun gnus-summary-highlight-line-0 ()
11370   (if (and (eq gnus-summary-highlight-line-trigger
11371                gnus-summary-highlight)
11372            gnus-summary-highlight-line-cached)
11373       gnus-summary-highlight-line-cached
11374     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11375           gnus-summary-highlight-line-cached
11376           (let* ((cond (list 'cond))
11377                  (c cond)
11378                  (list gnus-summary-highlight))
11379             (while list
11380               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11381                               nil))
11382               (setq c (cdr c)
11383                     list (cdr list)))
11384             (gnus-byte-compile (list 'lambda nil cond))))))
11385
11386 (defun gnus-summary-highlight-line ()
11387   "Highlight current line according to `gnus-summary-highlight'."
11388   (let* ((beg (point-at-bol))
11389          (article (or (gnus-summary-article-number) gnus-current-article))
11390          (score (or (cdr (assq article
11391                                gnus-newsgroup-scored))
11392                     gnus-summary-default-score 0))
11393          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11394          (inhibit-read-only t)
11395          (default gnus-summary-default-score)
11396          (default-high gnus-summary-default-high-score)
11397          (default-low gnus-summary-default-low-score)
11398          (uncached (and gnus-summary-use-undownloaded-faces
11399                         (memq article gnus-newsgroup-undownloaded)
11400                         (not (memq article gnus-newsgroup-cached)))))
11401     (let ((face (funcall (gnus-summary-highlight-line-0))))
11402       (unless (eq face (get-text-property beg 'face))
11403         (gnus-put-text-property-excluding-characters-with-faces
11404          beg (point-at-eol) 'face
11405          (setq face (if (boundp face) (symbol-value face) face)))
11406         (when gnus-summary-highlight-line-function
11407           (funcall gnus-summary-highlight-line-function article face))))))
11408
11409 (defun gnus-update-read-articles (group unread &optional compute)
11410   "Update the list of read articles in GROUP.
11411 UNREAD is a sorted list."
11412   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11413         (info (gnus-get-info group))
11414         (prev 1)
11415         read)
11416     (if (or (not info) (not active))
11417         ;; There is no info on this group if it was, in fact,
11418         ;; killed.  Gnus stores no information on killed groups, so
11419         ;; there's nothing to be done.
11420         ;; One could store the information somewhere temporarily,
11421         ;; perhaps...  Hmmm...
11422         ()
11423       ;; Remove any negative articles numbers.
11424       (while (and unread (< (car unread) 0))
11425         (setq unread (cdr unread)))
11426       ;; Remove any expired article numbers
11427       (while (and unread (< (car unread) (car active)))
11428         (setq unread (cdr unread)))
11429       ;; Compute the ranges of read articles by looking at the list of
11430       ;; unread articles.
11431       (while unread
11432         (when (/= (car unread) prev)
11433           (push (if (= prev (1- (car unread))) prev
11434                   (cons prev (1- (car unread))))
11435                 read))
11436         (setq prev (1+ (car unread)))
11437         (setq unread (cdr unread)))
11438       (when (<= prev (cdr active))
11439         (push (cons prev (cdr active)) read))
11440       (setq read (if (> (length read) 1) (nreverse read) read))
11441       (if compute
11442           read
11443         (save-excursion
11444           (let (setmarkundo)
11445             ;; Propagate the read marks to the backend.
11446             (when (gnus-check-backend-function 'request-set-mark group)
11447               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11448                     (add (gnus-remove-from-range read (gnus-info-read info))))
11449                 (when (or add del)
11450                   (unless (gnus-check-group group)
11451                     (error "Can't open server for %s" group))
11452                   (gnus-request-set-mark
11453                    group (delq nil (list (if add (list add 'add '(read)))
11454                                          (if del (list del 'del '(read))))))
11455                   (setq setmarkundo
11456                         `(gnus-request-set-mark
11457                           ,group
11458                           ',(delq nil (list
11459                                        (if del (list del 'add '(read)))
11460                                        (if add (list add 'del '(read))))))))))
11461             (set-buffer gnus-group-buffer)
11462             (gnus-undo-register
11463               `(progn
11464                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11465                  (gnus-info-set-read ',info ',(gnus-info-read info))
11466                  (gnus-get-unread-articles-in-group ',info
11467                                                     (gnus-active ,group))
11468                  (gnus-group-update-group ,group t)
11469                  ,setmarkundo))))
11470         ;; Enter this list into the group info.
11471         (gnus-info-set-read info read)
11472         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11473         (gnus-get-unread-articles-in-group info (gnus-active group))
11474         t))))
11475
11476 (defun gnus-offer-save-summaries ()
11477   "Offer to save all active summary buffers."
11478   (let (buffers)
11479     ;; Go through all buffers and find all summaries.
11480     (dolist (buffer (buffer-list))
11481       (when (and (setq buffer (buffer-name buffer))
11482                  (string-match "Summary" buffer)
11483                  (with-current-buffer buffer
11484                    ;; We check that this is, indeed, a summary buffer.
11485                    (and (eq major-mode 'gnus-summary-mode)
11486                         ;; Also make sure this isn't bogus.
11487                         gnus-newsgroup-prepared
11488                         ;; Also make sure that this isn't a
11489                         ;; dead summary buffer.
11490                         (not gnus-dead-summary-mode))))
11491         (push buffer buffers)))
11492     ;; Go through all these summary buffers and offer to save them.
11493     (when buffers
11494       (save-excursion
11495         (map-y-or-n-p
11496          "Update summary buffer %s? "
11497          (lambda (buf)
11498            (switch-to-buffer buf)
11499            (gnus-summary-exit))
11500          buffers)))))
11501
11502 (defun gnus-summary-setup-default-charset ()
11503   "Setup newsgroup default charset."
11504   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11505       (setq gnus-newsgroup-charset nil)
11506     (let* ((ignored-charsets
11507             (or gnus-newsgroup-ephemeral-ignored-charsets
11508                 (append
11509                  (and gnus-newsgroup-name
11510                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11511                  gnus-newsgroup-ignored-charsets))))
11512       (setq gnus-newsgroup-charset
11513             (or gnus-newsgroup-ephemeral-charset
11514                 (and gnus-newsgroup-name
11515                      (gnus-parameter-charset gnus-newsgroup-name))
11516                 gnus-default-charset))
11517       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11518            ignored-charsets))))
11519
11520 ;;;
11521 ;;; Mime Commands
11522 ;;;
11523
11524 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11525   "Display the current article buffer fully MIME-buttonized.
11526 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11527 treated as multipart/mixed."
11528   (interactive "P")
11529   (require 'gnus-art)
11530   (let ((gnus-unbuttonized-mime-types nil)
11531         (gnus-mime-display-multipart-as-mixed show-all-parts))
11532     (gnus-summary-show-article)))
11533
11534 (defun gnus-summary-repair-multipart (article)
11535   "Add a Content-Type header to a multipart article without one."
11536   (interactive (list (gnus-summary-article-number)))
11537   (gnus-with-article article
11538     (message-narrow-to-head)
11539     (message-remove-header "Mime-Version")
11540     (goto-char (point-max))
11541     (insert "Mime-Version: 1.0\n")
11542     (widen)
11543     (when (search-forward "\n--" nil t)
11544       (let ((separator (buffer-substring (point) (point-at-eol))))
11545         (message-narrow-to-head)
11546         (message-remove-header "Content-Type")
11547         (goto-char (point-max))
11548         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11549                         separator))
11550         (widen))))
11551   (let (gnus-mark-article-hook)
11552     (gnus-summary-select-article t t nil article)))
11553
11554 (defun gnus-summary-toggle-display-buttonized ()
11555   "Toggle the buttonizing of the article buffer."
11556   (interactive)
11557   (require 'gnus-art)
11558   (if (setq gnus-inhibit-mime-unbuttonizing
11559             (not gnus-inhibit-mime-unbuttonizing))
11560       (let ((gnus-unbuttonized-mime-types nil))
11561         (gnus-summary-show-article))
11562     (gnus-summary-show-article)))
11563
11564 ;;;
11565 ;;; Generic summary marking commands
11566 ;;;
11567
11568 (defvar gnus-summary-marking-alist
11569   '((read gnus-del-mark "d")
11570     (unread gnus-unread-mark "u")
11571     (ticked gnus-ticked-mark "!")
11572     (dormant gnus-dormant-mark "?")
11573     (expirable gnus-expirable-mark "e"))
11574   "An alist of names/marks/keystrokes.")
11575
11576 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11577 (defvar gnus-summary-mark-map)
11578
11579 (defun gnus-summary-make-all-marking-commands ()
11580   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11581   (dolist (elem gnus-summary-marking-alist)
11582     (apply 'gnus-summary-make-marking-command elem)))
11583
11584 (defun gnus-summary-make-marking-command (name mark keystroke)
11585   (let ((map (make-sparse-keymap)))
11586     (define-key gnus-summary-generic-mark-map keystroke map)
11587     (dolist (lway `((next "next" next nil "n")
11588                     (next-unread "next unread" next t "N")
11589                     (prev "previous" prev nil "p")
11590                     (prev-unread "previous unread" prev t "P")
11591                     (nomove "" nil nil ,keystroke)))
11592       (let ((func (gnus-summary-make-marking-command-1
11593                    mark (car lway) lway name)))
11594         (setq func (eval func))
11595         (define-key map (nth 4 lway) func)))))
11596
11597 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11598   `(defun ,(intern
11599             (format "gnus-summary-put-mark-as-%s%s"
11600                     name (if (eq way 'nomove)
11601                              ""
11602                            (concat "-" (symbol-name way)))))
11603      (n)
11604      ,(format
11605        "Mark the current article as %s%s.
11606 If N, the prefix, then repeat N times.
11607 If N is negative, move in reverse order.
11608 The difference between N and the actual number of articles marked is
11609 returned."
11610        name (cadr lway))
11611      (interactive "p")
11612      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11613
11614 (defun gnus-summary-generic-mark (n mark move unread)
11615   "Mark N articles with MARK."
11616   (unless (eq major-mode 'gnus-summary-mode)
11617     (error "This command can only be used in the summary buffer"))
11618   (gnus-summary-show-thread)
11619   (let ((nummove
11620          (cond
11621           ((eq move 'next) 1)
11622           ((eq move 'prev) -1)
11623           (t 0))))
11624     (if (zerop nummove)
11625         (setq n 1)
11626       (when (< n 0)
11627         (setq n (abs n)
11628               nummove (* -1 nummove))))
11629     (while (and (> n 0)
11630                 (gnus-summary-mark-article nil mark)
11631                 (zerop (gnus-summary-next-subject nummove unread t)))
11632       (setq n (1- n)))
11633     (when (/= 0 n)
11634       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11635     (gnus-summary-recenter)
11636     (gnus-summary-position-point)
11637     (gnus-set-mode-line 'summary)
11638     n))
11639
11640 (defun gnus-summary-insert-articles (articles)
11641   (when (setq articles
11642               (gnus-sorted-difference articles
11643                                       (mapcar (lambda (h)
11644                                                 (mail-header-number h))
11645                                               gnus-newsgroup-headers)))
11646     (setq gnus-newsgroup-headers
11647           (gnus-merge 'list
11648                       gnus-newsgroup-headers
11649                       (gnus-fetch-headers articles)
11650                       'gnus-article-sort-by-number))
11651     ;; Suppress duplicates?
11652     (when gnus-suppress-duplicates
11653       (gnus-dup-suppress-articles))
11654
11655     ;; We might want to build some more threads first.
11656     (when (and gnus-fetch-old-headers
11657                (eq gnus-headers-retrieved-by 'nov))
11658       (if (eq gnus-fetch-old-headers 'invisible)
11659           (gnus-build-all-threads)
11660         (gnus-build-old-threads)))
11661     ;; Let the Gnus agent mark articles as read.
11662     (when gnus-agent
11663       (gnus-agent-get-undownloaded-list))
11664     ;; Remove list identifiers from subject
11665     (when gnus-list-identifiers
11666       (gnus-summary-remove-list-identifiers))
11667     ;; First and last article in this newsgroup.
11668     (when gnus-newsgroup-headers
11669       (setq gnus-newsgroup-begin
11670             (mail-header-number (car gnus-newsgroup-headers))
11671             gnus-newsgroup-end
11672             (mail-header-number
11673              (gnus-last-element gnus-newsgroup-headers))))
11674     (when gnus-use-scoring
11675       (gnus-possibly-score-headers))))
11676
11677 (defun gnus-summary-insert-old-articles (&optional all)
11678   "Insert all old articles in this group.
11679 If ALL is non-nil, already read articles become readable.
11680 If ALL is a number, fetch this number of articles."
11681   (interactive "P")
11682   (prog1
11683       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11684             older len)
11685         (setq older
11686               ;; Some nntp servers lie about their active range.  When
11687               ;; this happens, the active range can be in the millions.
11688               ;; Use a compressed range to avoid creating a huge list.
11689               (gnus-range-difference (list gnus-newsgroup-active) old))
11690         (setq len (gnus-range-length older))
11691         (cond
11692          ((null older) nil)
11693          ((numberp all)
11694           (if (< all len)
11695               (let ((older-range (nreverse older)))
11696                 (setq older nil)
11697
11698                 (while (> all 0)
11699                   (let* ((r (pop older-range))
11700                          (min (if (numberp r) r (car r)))
11701                          (max (if (numberp r) r (cdr r))))
11702                     (while (and (<= min max)
11703                                 (> all 0))
11704                       (push max older)
11705                       (setq all (1- all)
11706                             max (1- max))))))
11707             (setq older (gnus-uncompress-range older))))
11708          (all
11709           (setq older (gnus-uncompress-range older)))
11710          (t
11711           (when (and (numberp gnus-large-newsgroup)
11712                    (> len gnus-large-newsgroup))
11713               (let* ((cursor-in-echo-area nil)
11714                      (initial (gnus-parameter-large-newsgroup-initial
11715                                gnus-newsgroup-name))
11716                      (input
11717                       (read-string
11718                        (format
11719                         "How many articles from %s (%s %d): "
11720                         (gnus-limit-string
11721                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11722                         (if initial "max" "default")
11723                         len)
11724                        (if initial
11725                            (cons (number-to-string initial)
11726                                  0)))))
11727                 (unless (string-match "^[ \t]*$" input)
11728                   (setq all (string-to-number input))
11729                   (if (< all len)
11730                       (let ((older-range (nreverse older)))
11731                         (setq older nil)
11732
11733                         (while (> all 0)
11734                           (let* ((r (pop older-range))
11735                                  (min (if (numberp r) r (car r)))
11736                                  (max (if (numberp r) r (cdr r))))
11737                             (while (and (<= min max)
11738                                         (> all 0))
11739                               (push max older)
11740                               (setq all (1- all)
11741                                     max (1- max))))))))))
11742           (setq older (gnus-uncompress-range older))))
11743         (if (not older)
11744             (message "No old news.")
11745           (gnus-summary-insert-articles older)
11746           (gnus-summary-limit (gnus-sorted-nunion old older))))
11747     (gnus-summary-position-point)))
11748
11749 (defun gnus-summary-insert-new-articles ()
11750   "Insert all new articles in this group."
11751   (interactive)
11752   (prog1
11753       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11754             (old-active gnus-newsgroup-active)
11755             (nnmail-fetched-sources (list t))
11756             i new)
11757         (setq gnus-newsgroup-active
11758               (gnus-activate-group gnus-newsgroup-name 'scan))
11759         (setq i (cdr gnus-newsgroup-active))
11760         (while (> i (cdr old-active))
11761           (push i new)
11762           (decf i))
11763         (if (not new)
11764             (message "No gnus is bad news")
11765           (gnus-summary-insert-articles new)
11766           (setq gnus-newsgroup-unreads
11767                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11768           (gnus-summary-limit (gnus-sorted-nunion old new))))
11769     (gnus-summary-position-point)))
11770
11771 (gnus-summary-make-all-marking-commands)
11772
11773 (gnus-ems-redefine)
11774
11775 (provide 'gnus-sum)
11776
11777 (run-hooks 'gnus-sum-load-hook)
11778
11779 ;; Local Variables:
11780 ;; coding: iso-8859-1
11781 ;; End:
11782
11783 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11784 ;;; gnus-sum.el ends here