Fix translations.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
9 ;; Keywords: mail, news, MIME
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (eval-when-compile
33   (require 'cl)
34   (defvar tool-bar-map))
35
36 (require 'gnus)
37 (require 'gnus-group)
38 (require 'gnus-spec)
39 (require 'gnus-range)
40 (require 'gnus-int)
41 (require 'gnus-undo)
42 (require 'gnus-util)
43 (require 'nnoo)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'pgg-decrypt-region "pgg" nil t)
51   (autoload 'pgg-verify-region "pgg" nil t))
52
53 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
54 (autoload 'gnus-cache-write-active "gnus-cache")
55 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
56 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
57 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
58 (autoload 'mm-uu-dissect "mm-uu")
59 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
60   "Deuglify broken Outlook (Express) articles and redisplay."
61   t)
62 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
63 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
64 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
65
66 (defcustom gnus-kill-summary-on-exit t
67   "*If non-nil, kill the summary buffer when you exit from it.
68 If nil, the summary will become a \"*Dead Summary*\" buffer, and
69 it will be killed sometime later."
70   :group 'gnus-summary-exit
71   :type 'boolean)
72
73 (defcustom gnus-fetch-old-headers nil
74   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
75 If an unread article in the group refers to an older, already read (or
76 just marked as read) article, the old article will not normally be
77 displayed in the Summary buffer.  If this variable is t, Gnus
78 will attempt to grab the headers to the old articles, and thereby
79 build complete threads.  If it has the value `some', only enough
80 headers to connect otherwise loose threads will be displayed.  This
81 variable can also be a number.  In that case, no more than that number
82 of old headers will be fetched.  If it has the value `invisible', all
83 old headers will be fetched, but none will be displayed.
84
85 The server has to support NOV for any of this to work."
86   :group 'gnus-thread
87   :type '(choice (const :tag "off" nil)
88                  (const :tag "on" t)
89                  (const some)
90                  (const invisible)
91                  number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-refer-thread-limit 200
95   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
96 If t, fetch all the available old headers."
97   :group 'gnus-thread
98   :type '(choice number
99                  (sexp :menu-tag "other" t)))
100
101 (defcustom gnus-summary-make-false-root 'adopt
102   "*nil means that Gnus won't gather loose threads.
103 If the root of a thread has expired or been read in a previous
104 session, the information necessary to build a complete thread has been
105 lost.  Instead of having many small sub-threads from this original thread
106 scattered all over the summary buffer, Gnus can gather them.
107
108 If non-nil, Gnus will try to gather all loose sub-threads from an
109 original thread into one large thread.
110
111 If this variable is non-nil, it should be one of `none', `adopt',
112 `dummy' or `empty'.
113
114 If this variable is `none', Gnus will not make a false root, but just
115 present the sub-threads after another.
116 If this variable is `dummy', Gnus will create a dummy root that will
117 have all the sub-threads as children.
118 If this variable is `adopt', Gnus will make one of the \"children\"
119 the parent and mark all the step-children as such.
120 If this variable is `empty', the \"children\" are printed with empty
121 subject fields.  (Or rather, they will be printed with a string
122 given by the `gnus-summary-same-subject' variable.)"
123   :group 'gnus-thread
124   :type '(choice (const :tag "off" nil)
125                  (const none)
126                  (const dummy)
127                  (const adopt)
128                  (const empty)))
129
130 (defcustom gnus-summary-make-false-root-always nil
131   "Always make a false dummy root."
132   :version "22.1"
133   :group 'gnus-thread
134   :type 'boolean)
135
136 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
137   "*A regexp to match subjects to be excluded from loose thread gathering.
138 As loose thread gathering is done on subjects only, that means that
139 there can be many false gatherings performed.  By rooting out certain
140 common subjects, gathering might become saner."
141   :group 'gnus-thread
142   :type 'regexp)
143
144 (defcustom gnus-summary-gather-subject-limit nil
145   "*Maximum length of subject comparisons when gathering loose threads.
146 Use nil to compare full subjects.  Setting this variable to a low
147 number will help gather threads that have been corrupted by
148 newsreaders chopping off subject lines, but it might also mean that
149 unrelated articles that have subject that happen to begin with the
150 same few characters will be incorrectly gathered.
151
152 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
153 comparing subjects."
154   :group 'gnus-thread
155   :type '(choice (const :tag "off" nil)
156                  (const fuzzy)
157                  (sexp :menu-tag "on" t)))
158
159 (defcustom gnus-simplify-subject-functions nil
160   "List of functions taking a string argument that simplify subjects.
161 The functions are applied recursively.
162
163 Useful functions to put in this list include:
164 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
165 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
166   :group 'gnus-thread
167   :type '(repeat function))
168
169 (defcustom gnus-simplify-ignored-prefixes nil
170   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
171   :group 'gnus-thread
172   :type '(choice (const :tag "off" nil)
173                  regexp))
174
175 (defcustom gnus-build-sparse-threads nil
176   "*If non-nil, fill in the gaps in threads.
177 If `some', only fill in the gaps that are needed to tie loose threads
178 together.  If `more', fill in all leaf nodes that Gnus can find.  If
179 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
180   :group 'gnus-thread
181   :type '(choice (const :tag "off" nil)
182                  (const some)
183                  (const more)
184                  (sexp :menu-tag "all" t)))
185
186 (defcustom gnus-summary-thread-gathering-function
187   'gnus-gather-threads-by-subject
188   "*Function used for gathering loose threads.
189 There are two pre-defined functions: `gnus-gather-threads-by-subject',
190 which only takes Subjects into consideration; and
191 `gnus-gather-threads-by-references', which compared the References
192 headers of the articles to find matches."
193   :group 'gnus-thread
194   :type '(radio (function-item gnus-gather-threads-by-subject)
195                 (function-item gnus-gather-threads-by-references)
196                 (function :tag "other")))
197
198 (defcustom gnus-summary-same-subject ""
199   "*String indicating that the current article has the same subject as the previous.
200 This variable will only be used if the value of
201 `gnus-summary-make-false-root' is `empty'."
202   :group 'gnus-summary-format
203   :type 'string)
204
205 (defcustom gnus-summary-goto-unread t
206   "*If t, many commands will go to the next unread article.
207 This applies to marking commands as well as other commands that
208 \"naturally\" select the next article, like, for instance, `SPC' at
209 the end of an article.
210
211 If nil, the marking commands do NOT go to the next unread article
212 \(they go to the next article instead).  If `never', commands that
213 usually go to the next unread article, will go to the next article,
214 whether it is read or not."
215   :group 'gnus-summary-marks
216   :link '(custom-manual "(gnus)Setting Marks")
217   :type '(choice (const :tag "off" nil)
218                  (const never)
219                  (sexp :menu-tag "on" t)))
220
221 (defcustom gnus-summary-default-score 0
222   "*Default article score level.
223 All scores generated by the score files will be added to this score.
224 If this variable is nil, scoring will be disabled."
225   :group 'gnus-score-default
226   :type '(choice (const :tag "disable")
227                  integer))
228
229 (defcustom gnus-summary-default-high-score 0
230   "*Default threshold for a high scored article.
231 An article will be highlighted as high scored if its score is greater
232 than this score."
233   :version "22.1"
234   :group 'gnus-score-default
235   :type 'integer)
236
237 (defcustom gnus-summary-default-low-score 0
238   "*Default threshold for a low scored article.
239 An article will be highlighted as low scored if its score is smaller
240 than this score."
241   :version "22.1"
242   :group 'gnus-score-default
243   :type 'integer)
244
245 (defcustom gnus-summary-zcore-fuzz 0
246   "*Fuzziness factor for the zcore in the summary buffer.
247 Articles with scores closer than this to `gnus-summary-default-score'
248 will not be marked."
249   :group 'gnus-summary-format
250   :type 'integer)
251
252 (defcustom gnus-simplify-subject-fuzzy-regexp nil
253   "*Strings to be removed when doing fuzzy matches.
254 This can either be a regular expression or list of regular expressions
255 that will be removed from subject strings if fuzzy subject
256 simplification is selected."
257   :group 'gnus-thread
258   :type '(repeat regexp))
259
260 (defcustom gnus-show-threads t
261   "*If non-nil, display threads in summary mode."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-hide-subtree nil
266   "*If non-nil, hide all threads initially.
267 This can be a predicate specifier which says which threads to hide.
268 If threads are hidden, you have to run the command
269 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
270 to expose hidden threads."
271   :group 'gnus-thread
272   :type '(radio (sexp :format "Non-nil\n"
273                       :match (lambda (widget value)
274                                (not (or (consp value) (functionp value))))
275                       :value t)
276                 (const nil)
277                 (sexp :tag "Predicate specifier")))
278
279 (defcustom gnus-thread-hide-killed t
280   "*If non-nil, hide killed threads automatically."
281   :group 'gnus-thread
282   :type 'boolean)
283
284 (defcustom gnus-thread-ignore-subject t
285   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
286 If nil, articles that have different subjects from their parents will
287 start separate threads."
288   :group 'gnus-thread
289   :type 'boolean)
290
291 (defcustom gnus-thread-operation-ignore-subject t
292   "*If non-nil, subjects will be ignored when doing thread commands.
293 This affects commands like `gnus-summary-kill-thread' and
294 `gnus-summary-lower-thread'.
295
296 If this variable is nil, articles in the same thread with different
297 subjects will not be included in the operation in question.  If this
298 variable is `fuzzy', only articles that have subjects that are fuzzily
299 equal will be included."
300   :group 'gnus-thread
301   :type '(choice (const :tag "off" nil)
302                  (const fuzzy)
303                  (sexp :tag "on" t)))
304
305 (defcustom gnus-thread-indent-level 4
306   "*Number that says how much each sub-thread should be indented."
307   :group 'gnus-thread
308   :type 'integer)
309
310 (defcustom gnus-auto-extend-newsgroup t
311   "*If non-nil, extend newsgroup forward and backward when requested."
312   :group 'gnus-summary-choose
313   :type 'boolean)
314
315 (defcustom gnus-auto-select-first t
316   "*If non-nil, select the article under point.
317 Which article this is is controlled by the `gnus-auto-select-subject'
318 variable.
319
320 If you want to prevent automatic selection of articles in some
321 newsgroups, set the variable to nil in `gnus-select-group-hook'."
322   :group 'gnus-group-select
323   :type '(choice (const :tag "none" nil)
324                  (sexp :menu-tag "first" t)))
325
326 (defcustom gnus-auto-select-subject 'unread
327   "*Says what subject to place under point when entering a group.
328
329 This variable can either be the symbols `first' (place point on the
330 first subject), `unread' (place point on the subject line of the first
331 unread article), `best' (place point on the subject line of the
332 higest-scored article), `unseen' (place point on the subject line of
333 the first unseen article), `unseen-or-unread' (place point on the subject
334 line of the first unseen article or, if all article have been seen, on the
335 subject line of the first unread article), or a function to be called to
336 place point on some subject line."
337   :version "22.1"
338   :group 'gnus-group-select
339   :type '(choice (const best)
340                  (const unread)
341                  (const first)
342                  (const unseen)
343                  (const unseen-or-unread)))
344
345 (defcustom gnus-dont-select-after-jump-to-other-group nil
346   "If non-nil, don't select the first unread article after entering the
347 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
348 it is depend on the value of `gnus-auto-select-first' whether to select
349 or not."
350   :group 'gnus-group-select
351   :type 'boolean)
352
353 (defcustom gnus-auto-select-next t
354   "*If non-nil, offer to go to the next group from the end of the previous.
355 If the value is t and the next newsgroup is empty, Gnus will exit
356 summary mode and go back to group mode.  If the value is neither nil
357 nor t, Gnus will select the following unread newsgroup.  In
358 particular, if the value is the symbol `quietly', the next unread
359 newsgroup will be selected without any confirmation, and if it is
360 `almost-quietly', the next group will be selected without any
361 confirmation if you are located on the last article in the group.
362 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
363 will go to the next group without confirmation."
364   :group 'gnus-summary-maneuvering
365   :type '(choice (const :tag "off" nil)
366                  (const quietly)
367                  (const almost-quietly)
368                  (const slightly-quietly)
369                  (sexp :menu-tag "on" t)))
370
371 (defcustom gnus-auto-select-same nil
372   "*If non-nil, select the next article with the same subject.
373 If there are no more articles with the same subject, go to
374 the first unread article."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-goto-ignores 'unfetched
379   "*Says how to handle unfetched articles when maneuvering.
380
381 This variable can either be the symbols nil (maneuver to any
382 article), `undownloaded' (maneuvering while unplugged ignores articles
383 that have not been fetched), `always-undownloaded' (maneuvering always
384 ignores articles that have not been fetched), `unfetched' (maneuvering
385 ignores articles whose headers have not been fetched).
386
387 NOTE: The list of unfetched articles will always be nil when plugged
388 and, when unplugged, a subset of the undownloaded article list."
389   :version "22.1"
390   :group 'gnus-summary-maneuvering
391   :type '(choice (const :tag "None" nil)
392                  (const :tag "Undownloaded when unplugged" undownloaded)
393                  (const :tag "Undownloaded" always-undownloaded)
394                  (const :tag "Unfetched" unfetched)))
395
396 (defcustom gnus-summary-check-current nil
397   "*If non-nil, consider the current article when moving.
398 The \"unread\" movement commands will stay on the same line if the
399 current article is unread."
400   :group 'gnus-summary-maneuvering
401   :type 'boolean)
402
403 (defcustom gnus-auto-center-summary 2
404   "*If non-nil, always center the current summary buffer.
405 In particular, if `vertical' do only vertical recentering.  If non-nil
406 and non-`vertical', do both horizontal and vertical recentering."
407   :group 'gnus-summary-maneuvering
408   :type '(choice (const :tag "none" nil)
409                  (const vertical)
410                  (integer :tag "height")
411                  (sexp :menu-tag "both" t)))
412
413 (defvar gnus-auto-center-group t
414   "*If non-nil, always center the group buffer.")
415
416 (defcustom gnus-show-all-headers nil
417   "*If non-nil, don't hide any headers."
418   :group 'gnus-article-hiding
419   :group 'gnus-article-headers
420   :type 'boolean)
421
422 (defcustom gnus-summary-ignore-duplicates nil
423   "*If non-nil, ignore articles with identical Message-ID headers."
424   :group 'gnus-summary
425   :type 'boolean)
426
427 (defcustom gnus-single-article-buffer t
428   "*If non-nil, display all articles in the same buffer.
429 If nil, each group will get its own article buffer."
430   :group 'gnus-article-various
431   :type 'boolean)
432
433 (defcustom gnus-break-pages t
434   "*If non-nil, do page breaking on articles.
435 The page delimiter is specified by the `gnus-page-delimiter'
436 variable."
437   :group 'gnus-article-various
438   :type 'boolean)
439
440 (defcustom gnus-show-mime t
441   "*If non-nil, do mime processing of articles.
442 The articles will simply be fed to the function given by
443 `gnus-article-display-method-for-mime'."
444   :group 'gnus-article-mime
445   :type 'boolean)
446
447 (defcustom gnus-move-split-methods nil
448   "*Variable used to suggest where articles are to be moved to.
449 It uses the same syntax as the `gnus-split-methods' variable.
450 However, whereas `gnus-split-methods' specifies file names as targets,
451 this variable specifies group names."
452   :group 'gnus-summary-mail
453   :type '(repeat (choice (list :value (fun) function)
454                          (cons :value ("" "") regexp (repeat string))
455                          (sexp :value nil))))
456
457 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
458   "Function used to compute default prefix for article move/copy/etc prompts.
459 The function should take one argument, a group name, and return a
460 string with the suggested prefix."
461   :group 'gnus-summary-mail
462   :type 'function)
463
464 ;; FIXME: Although the custom type is `character' for the following variables,
465 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
466
467 (defcustom gnus-unread-mark ?\ ;;;Whitespace
468   "*Mark used for unread articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-ticked-mark ?!
473   "*Mark used for ticked articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-dormant-mark ??
478   "*Mark used for dormant articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-del-mark ?r
483   "*Mark used for del'd articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-read-mark ?R
488   "*Mark used for read articles."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-expirable-mark ?E
493   "*Mark used for expirable articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-killed-mark ?K
498   "*Mark used for killed articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-spam-mark ?$
503   "*Mark used for spam articles."
504   :version "22.1"
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-souped-mark ?F
509   "*Mark used for souped articles."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-kill-file-mark ?X
514   "*Mark used for articles killed by kill files."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-low-score-mark ?Y
519   "*Mark used for articles with a low score."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-catchup-mark ?C
524   "*Mark used for articles that are caught up."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-replied-mark ?A
529   "*Mark used for articles that have been replied to."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-forwarded-mark ?F
534   "*Mark used for articles that have been forwarded."
535   :version "22.1"
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-recent-mark ?N
540   "*Mark used for articles that are recent."
541   :version "22.1"
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-cached-mark ?*
546   "*Mark used for articles that are in the cache."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-saved-mark ?S
551   "*Mark used for articles that have been saved."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-unseen-mark ?.
556   "*Mark used for articles that haven't been seen."
557   :version "22.1"
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-no-mark ?\ ;;;Whitespace
562   "*Mark used for articles that have no other secondary mark."
563   :version "22.1"
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-ancient-mark ?O
568   "*Mark used for ancient articles."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-sparse-mark ?Q
573   "*Mark used for sparsely reffed articles."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-canceled-mark ?G
578   "*Mark used for canceled articles."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-duplicate-mark ?M
583   "*Mark used for duplicate articles."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-undownloaded-mark ?-
588   "*Mark used for articles that weren't downloaded."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-downloaded-mark ?+
594   "*Mark used for articles that were downloaded."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-downloadable-mark ?%
599   "*Mark used for articles that are to be downloaded."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-unsendable-mark ?=
604   "*Mark used for articles that won't be sent."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-score-over-mark ?+
609   "*Score mark used for articles with high scores."
610   :group 'gnus-summary-marks
611   :type 'character)
612
613 (defcustom gnus-score-below-mark ?-
614   "*Score mark used for articles with low scores."
615   :group 'gnus-summary-marks
616   :type 'character)
617
618 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
619   "*There is no thread under the article."
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-not-empty-thread-mark ?=
624   "*There is a thread under the article."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-view-pseudo-asynchronously nil
629   "*If non-nil, Gnus will view pseudo-articles asynchronously."
630   :group 'gnus-extract-view
631   :type 'boolean)
632
633 (defcustom gnus-auto-expirable-marks
634   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
635         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
636         gnus-souped-mark gnus-duplicate-mark)
637   "*The list of marks converted into expiration if a group is auto-expirable."
638   :version "21.1"
639   :group 'gnus-summary
640   :type '(repeat character))
641
642 (defcustom gnus-inhibit-user-auto-expire t
643   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
644   :version "21.1"
645   :group 'gnus-summary
646   :type 'boolean)
647
648 (defcustom gnus-view-pseudos nil
649   "*If `automatic', pseudo-articles will be viewed automatically.
650 If `not-confirm', pseudos will be viewed automatically, and the user
651 will not be asked to confirm the command."
652   :group 'gnus-extract-view
653   :type '(choice (const :tag "off" nil)
654                  (const automatic)
655                  (const not-confirm)))
656
657 (defcustom gnus-view-pseudos-separately t
658   "*If non-nil, one pseudo-article will be created for each file to be viewed.
659 If nil, all files that use the same viewing command will be given as a
660 list of parameters to that command."
661   :group 'gnus-extract-view
662   :type 'boolean)
663
664 (defcustom gnus-insert-pseudo-articles t
665   "*If non-nil, insert pseudo-articles when decoding articles."
666   :group 'gnus-extract-view
667   :type 'boolean)
668
669 (defcustom gnus-summary-dummy-line-format
670   "   %(:                             :%) %S\n"
671   "*The format specification for the dummy roots in the summary buffer.
672 It works along the same lines as a normal formatting string,
673 with some simple extensions.
674
675 %S  The subject
676
677 General format specifiers can also be used.
678 See `(gnus)Formatting Variables'."
679   :link '(custom-manual "(gnus)Formatting Variables")
680   :group 'gnus-threading
681   :type 'string)
682
683 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
684   "*The format specification for the summary mode line.
685 It works along the same lines as a normal formatting string,
686 with some simple extensions:
687
688 %G  Group name
689 %p  Unprefixed group name
690 %A  Current article number
691 %z  Current article score
692 %V  Gnus version
693 %U  Number of unread articles in the group
694 %e  Number of unselected articles in the group
695 %Z  A string with unread/unselected article counts
696 %g  Shortish group name
697 %S  Subject of the current article
698 %u  User-defined spec
699 %s  Current score file name
700 %d  Number of dormant articles
701 %r  Number of articles that have been marked as read in this session
702 %E  Number of articles expunged by the score files"
703   :group 'gnus-summary-format
704   :type 'string)
705
706 (defcustom gnus-list-identifiers nil
707   "Regexp that matches list identifiers to be removed from subject.
708 This can also be a list of regexps."
709   :version "21.1"
710   :group 'gnus-summary-format
711   :group 'gnus-article-hiding
712   :type '(choice (const :tag "none" nil)
713                  (regexp :value ".*")
714                  (repeat :value (".*") regexp)))
715
716 (defcustom gnus-summary-mark-below 0
717   "*Mark all articles with a score below this variable as read.
718 This variable is local to each summary buffer and usually set by the
719 score file."
720   :group 'gnus-score-default
721   :type 'integer)
722
723 (defun gnus-widget-reversible-match (widget value)
724   "Ignoring WIDGET, convert VALUE to internal form.
725 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
726   ;; (debug value)
727   (or (symbolp value)
728       (and (listp value)
729            (eq (length value) 2)
730            (eq (nth 0 value) 'not)
731            (symbolp (nth 1 value)))))
732
733 (defun gnus-widget-reversible-to-internal (widget value)
734   "Ignoring WIDGET, convert VALUE to internal form.
735 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
736 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
737   ;; (debug value)
738   (if (atom value)
739       (list value nil)
740     (list (nth 1 value) t)))
741
742 (defun gnus-widget-reversible-to-external (widget value)
743   "Ignoring WIDGET, convert VALUE to external form.
744 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
745 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
746   ;; (debug value)
747   (if (nth 1 value)
748       (list 'not (nth 0 value))
749     (nth 0 value)))
750
751 (define-widget 'gnus-widget-reversible 'group
752   "A `group' that convert values."
753   :match 'gnus-widget-reversible-match
754   :value-to-internal 'gnus-widget-reversible-to-internal
755   :value-to-external 'gnus-widget-reversible-to-external)
756
757 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
758   "*List of functions used for sorting articles in the summary buffer.
759
760 Each function takes two articles and returns non-nil if the first
761 article should be sorted before the other.  If you use more than one
762 function, the primary sort function should be the last.  You should
763 probably always include `gnus-article-sort-by-number' in the list of
764 sorting functions -- preferably first.  Also note that sorting by date
765 is often much slower than sorting by number, and the sorting order is
766 very similar.  (Sorting by date means sorting by the time the message
767 was sent, sorting by number means sorting by arrival time.)
768
769 Each item can also be a list `(not F)' where F is a function;
770 this reverses the sort order.
771
772 Ready-made functions include `gnus-article-sort-by-number',
773 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
774 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
775 and `gnus-article-sort-by-score'.
776
777 When threading is turned on, the variable `gnus-thread-sort-functions'
778 controls how articles are sorted."
779   :group 'gnus-summary-sort
780   :type '(repeat (gnus-widget-reversible
781                   (choice (function-item gnus-article-sort-by-number)
782                           (function-item gnus-article-sort-by-author)
783                           (function-item gnus-article-sort-by-subject)
784                           (function-item gnus-article-sort-by-date)
785                           (function-item gnus-article-sort-by-score)
786                           (function-item gnus-article-sort-by-random)
787                           (function :tag "other"))
788                   (boolean :tag "Reverse order"))))
789
790
791 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
792   "*List of functions used for sorting threads in the summary buffer.
793 By default, threads are sorted by article number.
794
795 Each function takes two threads and returns non-nil if the first
796 thread should be sorted before the other.  If you use more than one
797 function, the primary sort function should be the last.  You should
798 probably always include `gnus-thread-sort-by-number' in the list of
799 sorting functions -- preferably first.  Also note that sorting by date
800 is often much slower than sorting by number, and the sorting order is
801 very similar.  (Sorting by date means sorting by the time the message
802 was sent, sorting by number means sorting by arrival time.)
803
804 Each list item can also be a list `(not F)' where F is a
805 function; this specifies reversed sort order.
806
807 Ready-made functions include `gnus-thread-sort-by-number',
808 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
809 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
810 `gnus-thread-sort-by-most-recent-number',
811 `gnus-thread-sort-by-most-recent-date',
812 `gnus-thread-sort-by-random', and
813 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
814
815 When threading is turned off, the variable
816 `gnus-article-sort-functions' controls how articles are sorted."
817   :group 'gnus-summary-sort
818   :type '(repeat 
819           (gnus-widget-reversible
820            (choice (function-item gnus-thread-sort-by-number)
821                    (function-item gnus-thread-sort-by-author)
822                    (function-item gnus-thread-sort-by-subject)
823                    (function-item gnus-thread-sort-by-date)
824                    (function-item gnus-thread-sort-by-score)
825                    (function-item gnus-thread-sort-by-most-recent-number)
826                    (function-item gnus-thread-sort-by-most-recent-date)
827                    (function-item gnus-thread-sort-by-random)
828                    (function-item gnus-thread-sort-by-total-score)
829                    (function :tag "other"))
830            (boolean :tag "Reverse order"))))
831
832 (defcustom gnus-thread-score-function '+
833   "*Function used for calculating the total score of a thread.
834
835 The function is called with the scores of the article and each
836 subthread and should then return the score of the thread.
837
838 Some functions you can use are `+', `max', or `min'."
839   :group 'gnus-summary-sort
840   :type 'function)
841
842 (defcustom gnus-summary-expunge-below nil
843   "All articles that have a score less than this variable will be expunged.
844 This variable is local to the summary buffers."
845   :group 'gnus-score-default
846   :type '(choice (const :tag "off" nil)
847                  integer))
848
849 (defcustom gnus-thread-expunge-below nil
850   "All threads that have a total score less than this variable will be expunged.
851 See `gnus-thread-score-function' for en explanation of what a
852 \"thread score\" is.
853
854 This variable is local to the summary buffers."
855   :group 'gnus-threading
856   :group 'gnus-score-default
857   :type '(choice (const :tag "off" nil)
858                  integer))
859
860 (defcustom gnus-summary-mode-hook nil
861   "*A hook for Gnus summary mode.
862 This hook is run before any variables are set in the summary buffer."
863   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
864   :group 'gnus-summary-various
865   :type 'hook)
866
867 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
868 (when (featurep 'xemacs)
869   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
870   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
871   (add-hook 'gnus-summary-mode-hook
872             'gnus-xmas-switch-horizontal-scrollbar-off))
873
874 (defcustom gnus-summary-menu-hook nil
875   "*Hook run after the creation of the summary mode menu."
876   :group 'gnus-summary-visual
877   :type 'hook)
878
879 (defcustom gnus-summary-exit-hook nil
880   "*A hook called on exit from the summary buffer.
881 It will be called with point in the group buffer."
882   :group 'gnus-summary-exit
883   :type 'hook)
884
885 (defcustom gnus-summary-prepare-hook nil
886   "*A hook called after the summary buffer has been generated.
887 If you want to modify the summary buffer, you can use this hook."
888   :group 'gnus-summary-various
889   :type 'hook)
890
891 (defcustom gnus-summary-prepared-hook nil
892   "*A hook called as the last thing after the summary buffer has been generated."
893   :group 'gnus-summary-various
894   :type 'hook)
895
896 (defcustom gnus-summary-generate-hook nil
897   "*A hook run just before generating the summary buffer.
898 This hook is commonly used to customize threading variables and the
899 like."
900   :group 'gnus-summary-various
901   :type 'hook)
902
903 (defcustom gnus-select-group-hook nil
904   "*A hook called when a newsgroup is selected.
905
906 If you'd like to simplify subjects like the
907 `gnus-summary-next-same-subject' command does, you can use the
908 following hook:
909
910  (add-hook gnus-select-group-hook
911            (lambda ()
912              (mapcar (lambda (header)
913                        (mail-header-set-subject
914                         header
915                         (gnus-simplify-subject
916                          (mail-header-subject header) 're-only)))
917                      gnus-newsgroup-headers)))"
918   :group 'gnus-group-select
919   :type 'hook)
920
921 (defcustom gnus-select-article-hook nil
922   "*A hook called when an article is selected."
923   :group 'gnus-summary-choose
924   :options '(gnus-agent-fetch-selected-article)
925   :type 'hook)
926
927 (defcustom gnus-visual-mark-article-hook
928   (list 'gnus-highlight-selected-summary)
929   "*Hook run after selecting an article in the summary buffer.
930 It is meant to be used for highlighting the article in some way.  It
931 is not run if `gnus-visual' is nil."
932   :group 'gnus-summary-visual
933   :type 'hook)
934
935 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
936   "*A hook called before parsing the headers."
937   :group 'gnus-various
938   :type 'hook)
939
940 (defcustom gnus-exit-group-hook nil
941   "*A hook called when exiting summary mode.
942 This hook is not called from the non-updating exit commands like `Q'."
943   :group 'gnus-various
944   :type 'hook)
945
946 (defcustom gnus-summary-update-hook
947   (list 'gnus-summary-highlight-line)
948   "*A hook called when a summary line is changed.
949 The hook will not be called if `gnus-visual' is nil.
950
951 The default function `gnus-summary-highlight-line' will
952 highlight the line according to the `gnus-summary-highlight'
953 variable."
954   :group 'gnus-summary-visual
955   :type 'hook)
956
957 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
958   "*A hook called when an article is selected for the first time.
959 The hook is intended to mark an article as read (or unread)
960 automatically when it is selected."
961   :group 'gnus-summary-choose
962   :type 'hook)
963
964 (defcustom gnus-group-no-more-groups-hook nil
965   "*A hook run when returning to group mode having no more (unread) groups."
966   :group 'gnus-group-select
967   :type 'hook)
968
969 (defcustom gnus-ps-print-hook nil
970   "*A hook run before ps-printing something from Gnus."
971   :group 'gnus-summary
972   :type 'hook)
973
974 (defcustom gnus-summary-article-move-hook nil
975   "*A hook called after an article is moved, copied, respooled, or crossposted."
976   :version "22.1"
977   :group 'gnus-summary
978   :type 'hook)
979
980 (defcustom gnus-summary-article-delete-hook nil
981   "*A hook called after an article is deleted."
982   :version "22.1"
983   :group 'gnus-summary
984   :type 'hook)
985
986 (defcustom gnus-summary-article-expire-hook nil
987   "*A hook called after an article is expired."
988   :version "22.1"
989   :group 'gnus-summary
990   :type 'hook)
991
992 (defcustom gnus-summary-display-arrow
993   (and (fboundp 'display-graphic-p)
994        (display-graphic-p))
995   "*If non-nil, display an arrow highlighting the current article."
996   :version "22.1"
997   :group 'gnus-summary
998   :type 'boolean)
999
1000 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1001   "Face used for highlighting the current article in the summary buffer."
1002   :group 'gnus-summary-visual
1003   :type 'face)
1004
1005 (defvar gnus-tmp-downloaded nil)
1006
1007 (defcustom gnus-summary-highlight
1008   '(((eq mark gnus-canceled-mark)
1009      . gnus-summary-cancelled)
1010     ((and uncached (> score default-high))
1011      . gnus-summary-high-undownloaded)
1012     ((and uncached (< score default-low))
1013      . gnus-summary-low-undownloaded)
1014     (uncached
1015      . gnus-summary-normal-undownloaded)
1016     ((and (> score default-high)
1017           (or (eq mark gnus-dormant-mark)
1018               (eq mark gnus-ticked-mark)))
1019      . gnus-summary-high-ticked)
1020     ((and (< score default-low)
1021           (or (eq mark gnus-dormant-mark)
1022               (eq mark gnus-ticked-mark)))
1023      . gnus-summary-low-ticked)
1024     ((or (eq mark gnus-dormant-mark)
1025          (eq mark gnus-ticked-mark))
1026      . gnus-summary-normal-ticked)
1027     ((and (> score default-high) (eq mark gnus-ancient-mark))
1028      . gnus-summary-high-ancient)
1029     ((and (< score default-low) (eq mark gnus-ancient-mark))
1030      . gnus-summary-low-ancient)
1031     ((eq mark gnus-ancient-mark)
1032      . gnus-summary-normal-ancient)
1033     ((and (> score default-high) (eq mark gnus-unread-mark))
1034      . gnus-summary-high-unread)
1035     ((and (< score default-low) (eq mark gnus-unread-mark))
1036      . gnus-summary-low-unread)
1037     ((eq mark gnus-unread-mark)
1038      . gnus-summary-normal-unread)
1039     ((> score default-high)
1040      . gnus-summary-high-read)
1041     ((< score default-low)
1042      . gnus-summary-low-read)
1043     (t
1044      . gnus-summary-normal-read))
1045   "*Controls the highlighting of summary buffer lines.
1046
1047 A list of (FORM . FACE) pairs.  When deciding how a a particular
1048 summary line should be displayed, each form is evaluated.  The content
1049 of the face field after the first true form is used.  You can change
1050 how those summary lines are displayed, by editing the face field.
1051
1052 You can use the following variables in the FORM field.
1053
1054 score:        The article's score
1055 default:      The default article score.
1056 default-high: The default score for high scored articles.
1057 default-low:  The default score for low scored articles.
1058 below:        The score below which articles are automatically marked as read.
1059 mark:         The article's mark.
1060 uncached:     Non-nil if the article is uncached."
1061   :group 'gnus-summary-visual
1062   :type '(repeat (cons (sexp :tag "Form" nil)
1063                        face)))
1064
1065 (defcustom gnus-alter-header-function nil
1066   "Function called to allow alteration of article header structures.
1067 The function is called with one parameter, the article header vector,
1068 which it may alter in any way."
1069   :type '(choice (const :tag "None" nil)
1070                  function)
1071   :group 'gnus-summary)
1072
1073 (defvar gnus-decode-encoded-word-function
1074   (mime-find-field-decoder 'From 'nov)
1075   "Variable that says which function should be used to decode a string with encoded words.")
1076
1077 (defcustom gnus-extra-headers '(To Newsgroups)
1078   "*Extra headers to parse."
1079   :version "21.1"
1080   :group 'gnus-summary
1081   :type '(repeat symbol))
1082
1083 (defcustom gnus-ignored-from-addresses
1084   (and user-mail-address (regexp-quote user-mail-address))
1085   "*Regexp of From headers that may be suppressed in favor of To headers."
1086   :version "21.1"
1087   :group 'gnus-summary
1088   :type 'regexp)
1089
1090 (defcustom gnus-summary-to-prefix "-> "
1091   "*String prefixed to the To field in the summary line when
1092 using `gnus-ignored-from-addresses'."
1093   :version "22.1"
1094   :group 'gnus-summary
1095   :type 'string)
1096
1097 (defcustom gnus-summary-newsgroup-prefix "=> "
1098   "*String prefixed to the Newsgroup field in the summary
1099 line when using `gnus-ignored-from-addresses'."
1100   :version "22.1"
1101   :group 'gnus-summary
1102   :type 'string)
1103
1104 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1105   "List of charsets that should be ignored.
1106 When these charsets are used in the \"charset\" parameter, the
1107 default charset will be used instead."
1108   :version "21.1"
1109   :type '(repeat symbol)
1110   :group 'gnus-charset)
1111
1112 (gnus-define-group-parameter
1113  ignored-charsets
1114  :type list
1115  :function-document
1116  "Return the ignored charsets of GROUP."
1117  :variable gnus-group-ignored-charsets-alist
1118  :variable-default
1119  '(("alt\\.chinese\\.text" iso-8859-1))
1120  :variable-document
1121  "Alist of regexps (to match group names) and charsets that should be ignored.
1122 When these charsets are used in the \"charset\" parameter, the
1123 default charset will be used instead."
1124  :variable-group gnus-charset
1125  :variable-type '(repeat (cons (regexp :tag "Group")
1126                                (repeat symbol)))
1127  :parameter-type '(choice :tag "Ignored charsets"
1128                           :value nil
1129                           (repeat (symbol)))
1130  :parameter-document       "\
1131 List of charsets that should be ignored.
1132
1133 When these charsets are used in the \"charset\" parameter, the
1134 default charset will be used instead.")
1135
1136 (defcustom gnus-group-highlight-words-alist nil
1137   "Alist of group regexps and highlight regexps.
1138 This variable uses the same syntax as `gnus-emphasis-alist'."
1139   :version "21.1"
1140   :type '(repeat (cons (regexp :tag "Group")
1141                        (repeat (list (regexp :tag "Highlight regexp")
1142                                      (number :tag "Group for entire word" 0)
1143                                      (number :tag "Group for displayed part" 0)
1144                                      (symbol :tag "Face"
1145                                              gnus-emphasis-highlight-words)))))
1146   :group 'gnus-summary-visual)
1147
1148 (defcustom gnus-use-wheel nil
1149   "Use Intelli-mouse on summary movement"
1150   :type 'boolean
1151   :group 'gnus-summary-maneuvering)
1152
1153 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1154   "Amount to scroll messages by spinning the mouse wheel.
1155 This is actually a cons cell, where the first item is the amount to scroll
1156 on a normal wheel event, and the second is the amount to scroll when the
1157 wheel is moved with the shift key depressed."
1158   :type '(cons (integer :tag "Shift") integer)
1159   :group 'gnus-summary-maneuvering)
1160
1161 (defcustom gnus-wheel-edge-resistance 2
1162   "How hard it should be to change the current article
1163 by moving the mouse over the edge of the article window."
1164   :type 'integer
1165   :group 'gnus-summary-maneuvering)
1166
1167 (defcustom gnus-summary-show-article-charset-alist
1168   nil
1169   "Alist of number and charset.
1170 The article will be shown with the charset corresponding to the
1171 numbered argument.
1172 For example: ((1 . cn-gb-2312) (2 . big5))."
1173   :version "21.1"
1174   :type '(repeat (cons (number :tag "Argument" 1)
1175                        (symbol :tag "Charset")))
1176   :group 'gnus-charset)
1177
1178 (defcustom gnus-preserve-marks t
1179   "Whether marks are preserved when moving, copying and respooling messages."
1180   :version "21.1"
1181   :type 'boolean
1182   :group 'gnus-summary-marks)
1183
1184 (defcustom gnus-alter-articles-to-read-function nil
1185   "Function to be called to alter the list of articles to be selected."
1186   :type '(choice (const nil) function)
1187   :group 'gnus-summary)
1188
1189 (defcustom gnus-orphan-score nil
1190   "*All orphans get this score added.  Set in the score file."
1191   :group 'gnus-score-default
1192   :type '(choice (const nil)
1193                  integer))
1194
1195 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1196   "*A regexp to match MIME parts when saving multiple parts of a
1197 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1198 This regexp will be used by default when prompting the user for which
1199 type of files to save."
1200   :group 'gnus-summary
1201   :type 'regexp)
1202
1203 (defcustom gnus-read-all-available-headers nil
1204   "Whether Gnus should parse all headers made available to it.
1205 This is mostly relevant for slow back ends where the user may
1206 wish to widen the summary buffer to include all headers
1207 that were fetched.  Say, for nnultimate groups."
1208   :version "22.1"
1209   :group 'gnus-summary
1210   :type '(choice boolean regexp))
1211
1212 (defcustom gnus-summary-muttprint-program "muttprint"
1213   "Command (and optional arguments) used to run Muttprint."
1214   :version "22.1"
1215   :group 'gnus-summary
1216   :type 'string)
1217
1218 (defcustom gnus-article-loose-mime t
1219   "If non-nil, don't require MIME-Version header.
1220 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1221 supply the MIME-Version header or deliberately strip it from the mail.
1222 If non-nil (the default), Gnus will treat some articles as MIME
1223 even if the MIME-Version header is missing."
1224   :version "22.1"
1225   :type 'boolean
1226   :group 'gnus-article-mime)
1227
1228 (defcustom gnus-article-emulate-mime t
1229   "If non-nil, use MIME emulation for uuencode and the like.
1230 This means that Gnus will search message bodies for text that look
1231 like uuencoded bits, yEncoded bits, and so on, and present that using
1232 the normal Gnus MIME machinery."
1233   :version "22.1"
1234   :type 'boolean
1235   :group 'gnus-article-mime)
1236
1237 ;;; Internal variables
1238
1239 (defvar gnus-summary-display-cache nil)
1240 (defvar gnus-article-mime-handles nil)
1241 (defvar gnus-article-decoded-p nil)
1242 (defvar gnus-article-charset nil)
1243 (defvar gnus-article-ignored-charsets nil)
1244 (defvar gnus-scores-exclude-files nil)
1245 (defvar gnus-page-broken nil)
1246
1247 (defvar gnus-original-article nil)
1248 (defvar gnus-article-internal-prepare-hook nil)
1249 (defvar gnus-newsgroup-process-stack nil)
1250
1251 (defvar gnus-thread-indent-array nil)
1252 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1253 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1254   "Function called to sort the articles within a thread after it has been gathered together.")
1255
1256 (defvar gnus-summary-save-parts-type-history nil)
1257 (defvar gnus-summary-save-parts-last-directory nil)
1258
1259 ;; Avoid highlighting in kill files.
1260 (defvar gnus-summary-inhibit-highlight nil)
1261 (defvar gnus-newsgroup-selected-overlay nil)
1262 (defvar gnus-inhibit-limiting nil)
1263 (defvar gnus-newsgroup-adaptive-score-file nil)
1264 (defvar gnus-current-score-file nil)
1265 (defvar gnus-current-move-group nil)
1266 (defvar gnus-current-copy-group nil)
1267 (defvar gnus-current-crosspost-group nil)
1268 (defvar gnus-newsgroup-display nil)
1269
1270 (defvar gnus-newsgroup-dependencies nil)
1271 (defvar gnus-newsgroup-adaptive nil)
1272 (defvar gnus-summary-display-article-function nil)
1273 (defvar gnus-summary-highlight-line-function nil
1274   "Function called after highlighting a summary line.")
1275
1276 (defvar gnus-summary-line-format-alist
1277   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1278     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1279     (?s gnus-tmp-subject-or-nil ?s)
1280     (?n gnus-tmp-name ?s)
1281     (?A (std11-address-string
1282          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1283     (?a (or (std11-full-name-string
1284              (car (mime-entity-read-field gnus-tmp-header 'From)))
1285             gnus-tmp-from) ?s)
1286     (?F gnus-tmp-from ?s)
1287     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1288     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1289     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1290     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1291     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1292     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1293     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1294     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1295     (?L gnus-tmp-lines ?s)
1296     (?O gnus-tmp-downloaded ?c)
1297     (?I gnus-tmp-indentation ?s)
1298     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1299     (?R gnus-tmp-replied ?c)
1300     (?\[ gnus-tmp-opening-bracket ?c)
1301     (?\] gnus-tmp-closing-bracket ?c)
1302     (?\> (make-string gnus-tmp-level ? ) ?s)
1303     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1304     (?i gnus-tmp-score ?d)
1305     (?z gnus-tmp-score-char ?c)
1306     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1307     (?U gnus-tmp-unread ?c)
1308     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1309         ?s)
1310     (?t (gnus-summary-number-of-articles-in-thread
1311          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1312         ?d)
1313     (?e (gnus-summary-number-of-articles-in-thread
1314          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1315         ?c)
1316     (?u gnus-tmp-user-defined ?s)
1317     (?P (gnus-pick-line-number) ?d)
1318     (?B gnus-tmp-thread-tree-header-string ?s)
1319     (user-date (gnus-user-date
1320                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1321   "An alist of format specifications that can appear in summary lines.
1322 These are paired with what variables they correspond with, along with
1323 the type of the variable (string, integer, character, etc).")
1324
1325 (defvar gnus-summary-dummy-line-format-alist
1326   `((?S gnus-tmp-subject ?s)
1327     (?N gnus-tmp-number ?d)
1328     (?u gnus-tmp-user-defined ?s)))
1329
1330 (defvar gnus-summary-mode-line-format-alist
1331   `((?G gnus-tmp-group-name ?s)
1332     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1333     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1334     (?A gnus-tmp-article-number ?d)
1335     (?Z gnus-tmp-unread-and-unselected ?s)
1336     (?V gnus-version ?s)
1337     (?U gnus-tmp-unread-and-unticked ?d)
1338     (?S gnus-tmp-subject ?s)
1339     (?e gnus-tmp-unselected ?d)
1340     (?u gnus-tmp-user-defined ?s)
1341     (?d (length gnus-newsgroup-dormant) ?d)
1342     (?t (length gnus-newsgroup-marked) ?d)
1343     (?h (length gnus-newsgroup-spam-marked) ?d)
1344     (?r (length gnus-newsgroup-reads) ?d)
1345     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1346     (?E gnus-newsgroup-expunged-tally ?d)
1347     (?s (gnus-current-score-file-nondirectory) ?s)))
1348
1349 (defvar gnus-last-search-regexp nil
1350   "Default regexp for article search command.")
1351
1352 (defvar gnus-summary-search-article-matched-data nil
1353   "Last matched data of article search command.  It is the local variable
1354 in `gnus-article-buffer' which consists of the list of start position,
1355 end position and text.")
1356
1357 (defvar gnus-last-shell-command nil
1358   "Default shell command on article.")
1359
1360 (defvar gnus-newsgroup-agentized nil
1361   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1362 (defvar gnus-newsgroup-begin nil)
1363 (defvar gnus-newsgroup-end nil)
1364 (defvar gnus-newsgroup-last-rmail nil)
1365 (defvar gnus-newsgroup-last-mail nil)
1366 (defvar gnus-newsgroup-last-folder nil)
1367 (defvar gnus-newsgroup-last-file nil)
1368 (defvar gnus-newsgroup-auto-expire nil)
1369 (defvar gnus-newsgroup-active nil)
1370
1371 (defvar gnus-newsgroup-data nil)
1372 (defvar gnus-newsgroup-data-reverse nil)
1373 (defvar gnus-newsgroup-limit nil)
1374 (defvar gnus-newsgroup-limits nil)
1375 (defvar gnus-summary-use-undownloaded-faces nil)
1376
1377 (defvar gnus-newsgroup-unreads nil
1378   "Sorted list of unread articles in the current newsgroup.")
1379
1380 (defvar gnus-newsgroup-unselected nil
1381   "Sorted list of unselected unread articles in the current newsgroup.")
1382
1383 (defvar gnus-newsgroup-reads nil
1384   "Alist of read articles and article marks in the current newsgroup.")
1385
1386 (defvar gnus-newsgroup-expunged-tally nil)
1387
1388 (defvar gnus-newsgroup-marked nil
1389   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1390
1391 (defvar gnus-newsgroup-spam-marked nil
1392   "List of ranges of articles that have been marked as spam.")
1393
1394 (defvar gnus-newsgroup-killed nil
1395   "List of ranges of articles that have been through the scoring process.")
1396
1397 (defvar gnus-newsgroup-cached nil
1398   "Sorted list of articles that come from the article cache.")
1399
1400 (defvar gnus-newsgroup-saved nil
1401   "List of articles that have been saved.")
1402
1403 (defvar gnus-newsgroup-kill-headers nil)
1404
1405 (defvar gnus-newsgroup-replied nil
1406   "List of articles that have been replied to in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-forwarded nil
1409   "List of articles that have been forwarded in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-recent nil
1412   "List of articles that have are recent in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-expirable nil
1415   "Sorted list of articles in the current newsgroup that can be expired.")
1416
1417 (defvar gnus-newsgroup-processable nil
1418   "List of articles in the current newsgroup that can be processed.")
1419
1420 (defvar gnus-newsgroup-downloadable nil
1421   "Sorted list of articles in the current newsgroup that can be processed.")
1422
1423 (defvar gnus-newsgroup-unfetched nil
1424   "Sorted list of articles in the current newsgroup whose headers have
1425 not been fetched into the agent.
1426
1427 This list will always be a subset of gnus-newsgroup-undownloaded.")
1428
1429 (defvar gnus-newsgroup-undownloaded nil
1430   "List of articles in the current newsgroup that haven't been downloaded.")
1431
1432 (defvar gnus-newsgroup-unsendable nil
1433   "List of articles in the current newsgroup that won't be sent.")
1434
1435 (defvar gnus-newsgroup-bookmarks nil
1436   "List of articles in the current newsgroup that have bookmarks.")
1437
1438 (defvar gnus-newsgroup-dormant nil
1439   "Sorted list of dormant articles in the current newsgroup.")
1440
1441 (defvar gnus-newsgroup-unseen nil
1442   "List of unseen articles in the current newsgroup.")
1443
1444 (defvar gnus-newsgroup-seen nil
1445   "Range of seen articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-articles nil
1448   "List of articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-scored nil
1451   "List of scored articles in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-incorporated nil
1454   "List of incorporated articles in the current newsgroup.")
1455
1456 (defvar gnus-newsgroup-headers nil
1457   "List of article headers in the current newsgroup.")
1458
1459 (defvar gnus-newsgroup-threads nil)
1460
1461 (defvar gnus-newsgroup-prepared nil
1462   "Whether the current group has been prepared properly.")
1463
1464 (defvar gnus-newsgroup-ancient nil
1465   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1466
1467 (defvar gnus-newsgroup-sparse nil)
1468
1469 (defvar gnus-current-article nil)
1470 (defvar gnus-article-current nil)
1471 (defvar gnus-current-headers nil)
1472 (defvar gnus-have-all-headers nil)
1473 (defvar gnus-last-article nil)
1474 (defvar gnus-newsgroup-history nil)
1475 (defvar gnus-newsgroup-charset nil)
1476 (defvar gnus-newsgroup-ephemeral-charset nil)
1477 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1478
1479 (defvar gnus-article-before-search nil)
1480
1481 (defvar gnus-summary-local-variables
1482   '(gnus-newsgroup-name
1483     gnus-newsgroup-begin gnus-newsgroup-end
1484     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1485     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1486     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1487     gnus-newsgroup-unselected gnus-newsgroup-marked
1488     gnus-newsgroup-spam-marked
1489     gnus-newsgroup-reads gnus-newsgroup-saved
1490     gnus-newsgroup-replied gnus-newsgroup-forwarded
1491     gnus-newsgroup-recent
1492     gnus-newsgroup-expirable
1493     gnus-newsgroup-processable gnus-newsgroup-killed
1494     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1495     gnus-newsgroup-unfetched
1496     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1497     gnus-newsgroup-seen gnus-newsgroup-articles
1498     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1499     gnus-newsgroup-headers gnus-newsgroup-threads
1500     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1501     gnus-current-article gnus-current-headers gnus-have-all-headers
1502     gnus-last-article gnus-article-internal-prepare-hook
1503     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1504     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1505     gnus-thread-expunge-below
1506     gnus-score-alist gnus-current-score-file
1507     (gnus-summary-expunge-below . global)
1508     (gnus-summary-mark-below . global)
1509     (gnus-orphan-score . global)
1510     gnus-newsgroup-active gnus-scores-exclude-files
1511     gnus-newsgroup-history gnus-newsgroup-ancient
1512     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1513     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1514     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1515     (gnus-newsgroup-expunged-tally . 0)
1516     gnus-cache-removable-articles gnus-newsgroup-cached
1517     gnus-newsgroup-data gnus-newsgroup-data-reverse
1518     gnus-newsgroup-limit gnus-newsgroup-limits
1519     gnus-newsgroup-charset gnus-newsgroup-display
1520     gnus-summary-use-undownloaded-faces
1521     gnus-newsgroup-incorporated)
1522   "Variables that are buffer-local to the summary buffers.")
1523
1524 (defvar gnus-newsgroup-variables nil
1525   "A list of variables that have separate values in different newsgroups.
1526 A list of newsgroup (summary buffer) local variables, or cons of
1527 variables and their default expressions to be evalled (when the default
1528 values are not nil), that should be made global while the summary buffer
1529 is active.
1530
1531 Note: The default expressions will be evaluated (using function `eval')
1532 before assignment to the local variable rather than just assigned to it.
1533 If the default expression is the symbol `global', that symbol will not
1534 be evaluated but the global value of the local variable will be used
1535 instead.
1536
1537 These variables can be used to set variables in the group parameters
1538 while still allowing them to affect operations done in other buffers.
1539 For example:
1540
1541 \(setq gnus-newsgroup-variables
1542      '(message-use-followup-to
1543        (gnus-visible-headers .
1544          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1545 ")
1546
1547 ;; Byte-compiler warning.
1548 (eval-when-compile
1549   ;; Bind features so that require will believe that gnus-sum has
1550   ;; already been loaded (avoids infinite recursion)
1551   (let ((features (cons 'gnus-sum features)))
1552     ;; Several of the declarations in gnus-sum are needed to load the
1553     ;; following files. Right now, these definitions have been
1554     ;; compiled but not defined (evaluated).  We could either do a
1555     ;; eval-and-compile about all of the declarations or evaluate the
1556     ;; source file.
1557     (if (boundp 'gnus-newsgroup-variables)
1558         nil
1559       (load "gnus-sum.el" t t t))
1560     (require 'gnus)
1561     (require 'gnus-art)))
1562
1563 ;; Subject simplification.
1564
1565 (defun gnus-simplify-whitespace (str)
1566   "Remove excessive whitespace from STR."
1567   ;; Multiple spaces.
1568   (while (string-match "[ \t][ \t]+" str)
1569     (setq str (concat (substring str 0 (match-beginning 0))
1570                         " "
1571                         (substring str (match-end 0)))))
1572   ;; Leading spaces.
1573   (when (string-match "^[ \t]+" str)
1574     (setq str (substring str (match-end 0))))
1575   ;; Trailing spaces.
1576   (when (string-match "[ \t]+$" str)
1577     (setq str (substring str 0 (match-beginning 0))))
1578   str)
1579
1580 (defun gnus-simplify-all-whitespace (str)
1581   "Remove all whitespace from STR."
1582   (while (string-match "[ \t\n]+" str)
1583     (setq str (replace-match "" nil nil str)))
1584   str)
1585
1586 (defsubst gnus-simplify-subject-re (subject)
1587   "Remove \"Re:\" from subject lines."
1588   (if (string-match message-subject-re-regexp subject)
1589       (substring subject (match-end 0))
1590     subject))
1591
1592 (defun gnus-simplify-subject (subject &optional re-only)
1593   "Remove `Re:' and words in parentheses.
1594 If RE-ONLY is non-nil, strip leading `Re:'s only."
1595   (let ((case-fold-search t))           ;Ignore case.
1596     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1597     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1598       (setq subject (substring subject (match-end 0))))
1599     ;; Remove uninteresting prefixes.
1600     (when (and (not re-only)
1601                gnus-simplify-ignored-prefixes
1602                (string-match gnus-simplify-ignored-prefixes subject))
1603       (setq subject (substring subject (match-end 0))))
1604     ;; Remove words in parentheses from end.
1605     (unless re-only
1606       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1607         (setq subject (substring subject 0 (match-beginning 0)))))
1608     ;; Return subject string.
1609     subject))
1610
1611 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1612 ;; all whitespace.
1613 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1614   (goto-char (point-min))
1615   (while (re-search-forward regexp nil t)
1616     (replace-match (or newtext ""))))
1617
1618 (defun gnus-simplify-buffer-fuzzy ()
1619   "Simplify string in the buffer fuzzily.
1620 The string in the accessible portion of the current buffer is simplified.
1621 It is assumed to be a single-line subject.
1622 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1623 matter is removed.  Additional things can be deleted by setting
1624 `gnus-simplify-subject-fuzzy-regexp'."
1625   (let ((case-fold-search t)
1626         (modified-tick))
1627     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1628
1629     (while (not (eq modified-tick (buffer-modified-tick)))
1630       (setq modified-tick (buffer-modified-tick))
1631       (cond
1632        ((listp gnus-simplify-subject-fuzzy-regexp)
1633         (mapcar 'gnus-simplify-buffer-fuzzy-step
1634                 gnus-simplify-subject-fuzzy-regexp))
1635        (gnus-simplify-subject-fuzzy-regexp
1636         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1637       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1638       (gnus-simplify-buffer-fuzzy-step
1639        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1640       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1641
1642     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1643     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1644     (gnus-simplify-buffer-fuzzy-step " $")
1645     (gnus-simplify-buffer-fuzzy-step "^ +")))
1646
1647 (defun gnus-simplify-subject-fuzzy (subject)
1648   "Simplify a subject string fuzzily.
1649 See `gnus-simplify-buffer-fuzzy' for details."
1650   (save-excursion
1651     (gnus-set-work-buffer)
1652     (let ((case-fold-search t))
1653       ;; Remove uninteresting prefixes.
1654       (when (and gnus-simplify-ignored-prefixes
1655                  (string-match gnus-simplify-ignored-prefixes subject))
1656         (setq subject (substring subject (match-end 0))))
1657       (insert subject)
1658       (inline (gnus-simplify-buffer-fuzzy))
1659       (buffer-string))))
1660
1661 (defsubst gnus-simplify-subject-fully (subject)
1662   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1663   (cond
1664    (gnus-simplify-subject-functions
1665     (gnus-map-function gnus-simplify-subject-functions subject))
1666    ((null gnus-summary-gather-subject-limit)
1667     (gnus-simplify-subject-re subject))
1668    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1669     (gnus-simplify-subject-fuzzy subject))
1670    ((numberp gnus-summary-gather-subject-limit)
1671     (gnus-limit-string (gnus-simplify-subject-re subject)
1672                        gnus-summary-gather-subject-limit))
1673    (t
1674     subject)))
1675
1676 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1677   "Check whether two subjects are equal.
1678 If optional argument SIMPLE-FIRST is t, first argument is already
1679 simplified."
1680   (cond
1681    ((null simple-first)
1682     (equal (gnus-simplify-subject-fully s1)
1683            (gnus-simplify-subject-fully s2)))
1684    (t
1685     (equal s1
1686            (gnus-simplify-subject-fully s2)))))
1687
1688 (defun gnus-summary-bubble-group ()
1689   "Increase the score of the current group.
1690 This is a handy function to add to `gnus-summary-exit-hook' to
1691 increase the score of each group you read."
1692   (gnus-group-add-score gnus-newsgroup-name))
1693
1694 \f
1695 ;;;
1696 ;;; Gnus summary mode
1697 ;;;
1698
1699 (put 'gnus-summary-mode 'mode-class 'special)
1700
1701 (defvar gnus-article-commands-menu)
1702
1703 ;; Non-orthogonal keys
1704
1705 (gnus-define-keys gnus-summary-mode-map
1706   " " gnus-summary-next-page
1707   "\177" gnus-summary-prev-page
1708   [delete] gnus-summary-prev-page
1709   [backspace] gnus-summary-prev-page
1710   "\r" gnus-summary-scroll-up
1711   "\M-\r" gnus-summary-scroll-down
1712   "n" gnus-summary-next-unread-article
1713   "p" gnus-summary-prev-unread-article
1714   "N" gnus-summary-next-article
1715   "P" gnus-summary-prev-article
1716   "\M-\C-n" gnus-summary-next-same-subject
1717   "\M-\C-p" gnus-summary-prev-same-subject
1718   "\M-n" gnus-summary-next-unread-subject
1719   "\M-p" gnus-summary-prev-unread-subject
1720   "." gnus-summary-first-unread-article
1721   "," gnus-summary-best-unread-article
1722   "\M-s" gnus-summary-search-article-forward
1723   "\M-r" gnus-summary-search-article-backward
1724   "<" gnus-summary-beginning-of-article
1725   ">" gnus-summary-end-of-article
1726   "j" gnus-summary-goto-article
1727   "^" gnus-summary-refer-parent-article
1728   "\M-^" gnus-summary-refer-article
1729   "u" gnus-summary-tick-article-forward
1730   "!" gnus-summary-tick-article-forward
1731   "U" gnus-summary-tick-article-backward
1732   "d" gnus-summary-mark-as-read-forward
1733   "D" gnus-summary-mark-as-read-backward
1734   "E" gnus-summary-mark-as-expirable
1735   "\M-u" gnus-summary-clear-mark-forward
1736   "\M-U" gnus-summary-clear-mark-backward
1737   "k" gnus-summary-kill-same-subject-and-select
1738   "\C-k" gnus-summary-kill-same-subject
1739   "\M-\C-k" gnus-summary-kill-thread
1740   "\M-\C-l" gnus-summary-lower-thread
1741   "e" gnus-summary-edit-article
1742   "#" gnus-summary-mark-as-processable
1743   "\M-#" gnus-summary-unmark-as-processable
1744   "\M-\C-t" gnus-summary-toggle-threads
1745   "\M-\C-s" gnus-summary-show-thread
1746   "\M-\C-h" gnus-summary-hide-thread
1747   "\M-\C-f" gnus-summary-next-thread
1748   "\M-\C-b" gnus-summary-prev-thread
1749   [(meta down)] gnus-summary-next-thread
1750   [(meta up)] gnus-summary-prev-thread
1751   "\M-\C-u" gnus-summary-up-thread
1752   "\M-\C-d" gnus-summary-down-thread
1753   "&" gnus-summary-execute-command
1754   "c" gnus-summary-catchup-and-exit
1755   "\C-w" gnus-summary-mark-region-as-read
1756   "\C-t" gnus-summary-toggle-truncation
1757   "?" gnus-summary-mark-as-dormant
1758   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1759   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1760   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1761   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1762   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1763   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1764   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1765   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1766   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1767   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1768   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1769   "=" gnus-summary-expand-window
1770   "\C-x\C-s" gnus-summary-reselect-current-group
1771   "\M-g" gnus-summary-rescan-group
1772   "w" gnus-summary-stop-page-breaking
1773   "\C-c\C-r" gnus-summary-caesar-message
1774   "\M-t" gnus-summary-toggle-mime
1775   "f" gnus-summary-followup
1776   "F" gnus-summary-followup-with-original
1777   "C" gnus-summary-cancel-article
1778   "r" gnus-summary-reply
1779   "R" gnus-summary-reply-with-original
1780   "\C-c\C-f" gnus-summary-mail-forward
1781   "o" gnus-summary-save-article
1782   "\C-o" gnus-summary-save-article-mail
1783   "|" gnus-summary-pipe-output
1784   "\M-k" gnus-summary-edit-local-kill
1785   "\M-K" gnus-summary-edit-global-kill
1786   ;; "V" gnus-version
1787   "\C-c\C-d" gnus-summary-describe-group
1788   "q" gnus-summary-exit
1789   "Q" gnus-summary-exit-no-update
1790   "\C-c\C-i" gnus-info-find-node
1791   gnus-mouse-2 gnus-mouse-pick-article
1792   [follow-link] mouse-face
1793   "m" gnus-summary-mail-other-window
1794   "a" gnus-summary-post-news
1795   "i" gnus-summary-news-other-window
1796   "x" gnus-summary-limit-to-unread
1797   "s" gnus-summary-isearch-article
1798   "t" gnus-summary-toggle-header
1799   "g" gnus-summary-show-article
1800   "l" gnus-summary-goto-last-article
1801   "v" gnus-summary-preview-mime-message
1802   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1803   "\C-d" gnus-summary-enter-digest-group
1804   "\M-\C-d" gnus-summary-read-document
1805   "\M-\C-e" gnus-summary-edit-parameters
1806   "\M-\C-a" gnus-summary-customize-parameters
1807   "\C-c\C-b" gnus-bug
1808   "\C-c\C-n" gnus-namazu-search
1809   "*" gnus-cache-enter-article
1810   "\M-*" gnus-cache-remove-article
1811   "\M-&" gnus-summary-universal-argument
1812   "\C-l" gnus-recenter
1813   "I" gnus-summary-increase-score
1814   "L" gnus-summary-lower-score
1815   "\M-i" gnus-symbolic-argument
1816   "h" gnus-summary-select-article-buffer
1817
1818   "V" gnus-summary-score-map
1819   "X" gnus-uu-extract-map
1820   "S" gnus-summary-send-map)
1821
1822   ;; Sort of orthogonal keymap
1823 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1824   "t" gnus-summary-tick-article-forward
1825   "!" gnus-summary-tick-article-forward
1826   "d" gnus-summary-mark-as-read-forward
1827   "r" gnus-summary-mark-as-read-forward
1828   "c" gnus-summary-clear-mark-forward
1829   " " gnus-summary-clear-mark-forward
1830   "e" gnus-summary-mark-as-expirable
1831   "x" gnus-summary-mark-as-expirable
1832   "?" gnus-summary-mark-as-dormant
1833   "b" gnus-summary-set-bookmark
1834   "B" gnus-summary-remove-bookmark
1835   "#" gnus-summary-mark-as-processable
1836   "\M-#" gnus-summary-unmark-as-processable
1837   "S" gnus-summary-limit-include-expunged
1838   "C" gnus-summary-catchup
1839   "H" gnus-summary-catchup-to-here
1840   "h" gnus-summary-catchup-from-here
1841   "\C-c" gnus-summary-catchup-all
1842   "k" gnus-summary-kill-same-subject-and-select
1843   "K" gnus-summary-kill-same-subject
1844   "P" gnus-uu-mark-map)
1845
1846 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1847   "c" gnus-summary-clear-above
1848   "u" gnus-summary-tick-above
1849   "m" gnus-summary-mark-above
1850   "k" gnus-summary-kill-below)
1851
1852 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1853   "/" gnus-summary-limit-to-subject
1854   "n" gnus-summary-limit-to-articles
1855   "w" gnus-summary-pop-limit
1856   "s" gnus-summary-limit-to-subject
1857   "a" gnus-summary-limit-to-author
1858   "u" gnus-summary-limit-to-unread
1859   "m" gnus-summary-limit-to-marks
1860   "M" gnus-summary-limit-exclude-marks
1861   "v" gnus-summary-limit-to-score
1862   "*" gnus-summary-limit-include-cached
1863   "D" gnus-summary-limit-include-dormant
1864   "T" gnus-summary-limit-include-thread
1865   "d" gnus-summary-limit-exclude-dormant
1866   "t" gnus-summary-limit-to-age
1867   "." gnus-summary-limit-to-unseen
1868   "x" gnus-summary-limit-to-extra
1869   "p" gnus-summary-limit-to-display-predicate
1870   "E" gnus-summary-limit-include-expunged
1871   "c" gnus-summary-limit-exclude-childless-dormant
1872   "C" gnus-summary-limit-mark-excluded-as-read
1873   "o" gnus-summary-insert-old-articles
1874   "N" gnus-summary-insert-new-articles
1875   "r" gnus-summary-limit-to-replied
1876   "R" gnus-summary-limit-to-recipient)
1877
1878 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1879   "n" gnus-summary-next-unread-article
1880   "p" gnus-summary-prev-unread-article
1881   "N" gnus-summary-next-article
1882   "P" gnus-summary-prev-article
1883   "\C-n" gnus-summary-next-same-subject
1884   "\C-p" gnus-summary-prev-same-subject
1885   "\M-n" gnus-summary-next-unread-subject
1886   "\M-p" gnus-summary-prev-unread-subject
1887   "f" gnus-summary-first-unread-article
1888   "b" gnus-summary-best-unread-article
1889   "j" gnus-summary-goto-article
1890   "g" gnus-summary-goto-subject
1891   "l" gnus-summary-goto-last-article
1892   "o" gnus-summary-pop-article)
1893
1894 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1895   "k" gnus-summary-kill-thread
1896   "l" gnus-summary-lower-thread
1897   "i" gnus-summary-raise-thread
1898   "T" gnus-summary-toggle-threads
1899   "t" gnus-summary-rethread-current
1900   "^" gnus-summary-reparent-thread
1901   "s" gnus-summary-show-thread
1902   "S" gnus-summary-show-all-threads
1903   "h" gnus-summary-hide-thread
1904   "H" gnus-summary-hide-all-threads
1905   "n" gnus-summary-next-thread
1906   "p" gnus-summary-prev-thread
1907   "u" gnus-summary-up-thread
1908   "o" gnus-summary-top-thread
1909   "d" gnus-summary-down-thread
1910   "#" gnus-uu-mark-thread
1911   "\M-#" gnus-uu-unmark-thread)
1912
1913 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1914   "g" gnus-summary-prepare
1915   "c" gnus-summary-insert-cached-articles
1916   "d" gnus-summary-insert-dormant-articles)
1917
1918 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1919   "c" gnus-summary-catchup-and-exit
1920   "C" gnus-summary-catchup-all-and-exit
1921   "E" gnus-summary-exit-no-update
1922   "J" gnus-summary-jump-to-other-group
1923   "Q" gnus-summary-exit
1924   "Z" gnus-summary-exit
1925   "n" gnus-summary-catchup-and-goto-next-group
1926   "R" gnus-summary-reselect-current-group
1927   "G" gnus-summary-rescan-group
1928   "N" gnus-summary-next-group
1929   "s" gnus-summary-save-newsrc
1930   "P" gnus-summary-prev-group)
1931
1932 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1933   " " gnus-summary-next-page
1934   "n" gnus-summary-next-page
1935   "\177" gnus-summary-prev-page
1936   [delete] gnus-summary-prev-page
1937   "p" gnus-summary-prev-page
1938   "\r" gnus-summary-scroll-up
1939   "\M-\r" gnus-summary-scroll-down
1940   "<" gnus-summary-beginning-of-article
1941   ">" gnus-summary-end-of-article
1942   "b" gnus-summary-beginning-of-article
1943   "e" gnus-summary-end-of-article
1944   "^" gnus-summary-refer-parent-article
1945   "r" gnus-summary-refer-parent-article
1946   "D" gnus-summary-enter-digest-group
1947   "R" gnus-summary-refer-references
1948   "T" gnus-summary-refer-thread
1949   "g" gnus-summary-show-article
1950   "s" gnus-summary-isearch-article
1951   "P" gnus-summary-print-article
1952   "M" gnus-mailing-list-insinuate
1953   "t" gnus-article-babel)
1954
1955 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1956   "b" gnus-article-add-buttons
1957   "B" gnus-article-add-buttons-to-head
1958   "o" gnus-article-treat-overstrike
1959   "e" gnus-article-emphasize
1960   "w" gnus-article-fill-cited-article
1961   "Q" gnus-article-fill-long-lines
1962   "C" gnus-article-capitalize-sentences
1963   "c" gnus-article-remove-cr
1964   "Z" gnus-article-decode-HZ
1965   "A" gnus-article-treat-ansi-sequences
1966   "h" gnus-article-wash-html
1967   "u" gnus-article-unsplit-urls
1968   "f" gnus-article-display-x-face
1969   "l" gnus-summary-stop-page-breaking
1970   "r" gnus-summary-caesar-message
1971   "m" gnus-summary-morse-message
1972   "t" gnus-summary-toggle-header
1973   "g" gnus-treat-smiley
1974   "v" gnus-summary-verbose-headers
1975   "m" gnus-summary-toggle-mime
1976   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1977   "p" gnus-article-verify-x-pgp-sig
1978   "d" gnus-article-treat-dumbquotes
1979   "i" gnus-summary-idna-message)
1980
1981 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1982   ;; mnemonic: deuglif*Y*
1983   "u" gnus-article-outlook-unwrap-lines
1984   "a" gnus-article-outlook-repair-attribution
1985   "c" gnus-article-outlook-rearrange-citation
1986   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1987
1988 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1989   "a" gnus-article-hide
1990   "h" gnus-article-hide-headers
1991   "b" gnus-article-hide-boring-headers
1992   "s" gnus-article-hide-signature
1993   "c" gnus-article-hide-citation
1994   "C" gnus-article-hide-citation-in-followups
1995   "l" gnus-article-hide-list-identifiers
1996   "B" gnus-article-strip-banner
1997   "P" gnus-article-hide-pem
1998   "\C-c" gnus-article-hide-citation-maybe)
1999
2000 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2001   "a" gnus-article-highlight
2002   "h" gnus-article-highlight-headers
2003   "c" gnus-article-highlight-citation
2004   "s" gnus-article-highlight-signature)
2005
2006 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2007   "f" gnus-article-treat-fold-headers
2008   "u" gnus-article-treat-unfold-headers
2009   "n" gnus-article-treat-fold-newsgroups)
2010
2011 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2012   "x" gnus-article-display-x-face
2013   "d" gnus-article-display-face
2014   "s" gnus-treat-smiley
2015   "D" gnus-article-remove-images
2016   "f" gnus-treat-from-picon
2017   "m" gnus-treat-mail-picon
2018   "n" gnus-treat-newsgroups-picon)
2019
2020 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2021   "z" gnus-article-date-ut
2022   "u" gnus-article-date-ut
2023   "l" gnus-article-date-local
2024   "p" gnus-article-date-english
2025   "e" gnus-article-date-lapsed
2026   "o" gnus-article-date-original
2027   "i" gnus-article-date-iso8601
2028   "s" gnus-article-date-user)
2029
2030 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2031   "t" gnus-article-remove-trailing-blank-lines
2032   "l" gnus-article-strip-leading-blank-lines
2033   "m" gnus-article-strip-multiple-blank-lines
2034   "a" gnus-article-strip-blank-lines
2035   "A" gnus-article-strip-all-blank-lines
2036   "s" gnus-article-strip-leading-space
2037   "e" gnus-article-strip-trailing-space
2038   "w" gnus-article-remove-leading-whitespace)
2039
2040 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2041   "v" gnus-version
2042   "f" gnus-summary-fetch-faq
2043   "d" gnus-summary-describe-group
2044   "h" gnus-summary-describe-briefly
2045   "i" gnus-info-find-node
2046   "c" gnus-group-fetch-charter
2047   "C" gnus-group-fetch-control)
2048
2049 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2050   "e" gnus-summary-expire-articles
2051   "\M-\C-e" gnus-summary-expire-articles-now
2052   "\177" gnus-summary-delete-article
2053   [delete] gnus-summary-delete-article
2054   [backspace] gnus-summary-delete-article
2055   "m" gnus-summary-move-article
2056   "r" gnus-summary-respool-article
2057   "w" gnus-summary-edit-article
2058   "c" gnus-summary-copy-article
2059   "B" gnus-summary-crosspost-article
2060   "q" gnus-summary-respool-query
2061   "t" gnus-summary-respool-trace
2062   "i" gnus-summary-import-article
2063   "I" gnus-summary-create-article
2064   "p" gnus-summary-article-posted-p)
2065
2066 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2067   "o" gnus-summary-save-article
2068   "m" gnus-summary-save-article-mail
2069   "F" gnus-summary-write-article-file
2070   "r" gnus-summary-save-article-rmail
2071   "f" gnus-summary-save-article-file
2072   "b" gnus-summary-save-article-body-file
2073   "h" gnus-summary-save-article-folder
2074   "v" gnus-summary-save-article-vm
2075   "p" gnus-summary-pipe-output
2076   "P" gnus-summary-muttprint
2077   "s" gnus-soup-add-article)
2078
2079 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2080   "b" gnus-summary-display-buttonized
2081   "m" gnus-summary-repair-multipart
2082   "v" gnus-article-view-part
2083   "o" gnus-article-save-part
2084   "O" gnus-article-save-part-and-strip
2085   "d" gnus-article-delete-part
2086   "j" gnus-article-jump-to-part
2087   "c" gnus-article-copy-part
2088   "C" gnus-article-view-part-as-charset
2089   "e" gnus-article-view-part-externally
2090   "E" gnus-article-encrypt-body
2091   "i" gnus-article-inline-part
2092   "|" gnus-article-pipe-part)
2093
2094 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2095   "p" gnus-summary-mark-as-processable
2096   "u" gnus-summary-unmark-as-processable
2097   "U" gnus-summary-unmark-all-processable
2098   "v" gnus-uu-mark-over
2099   "s" gnus-uu-mark-series
2100   "r" gnus-uu-mark-region
2101   "g" gnus-uu-unmark-region
2102   "R" gnus-uu-mark-by-regexp
2103   "G" gnus-uu-unmark-by-regexp
2104   "t" gnus-uu-mark-thread
2105   "T" gnus-uu-unmark-thread
2106   "a" gnus-uu-mark-all
2107   "b" gnus-uu-mark-buffer
2108   "S" gnus-uu-mark-sparse
2109   "k" gnus-summary-kill-process-mark
2110   "y" gnus-summary-yank-process-mark
2111   "w" gnus-summary-save-process-mark
2112   "i" gnus-uu-invert-processable)
2113
2114 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2115   ;;"x" gnus-uu-extract-any
2116   "m" gnus-summary-save-parts
2117   "u" gnus-uu-decode-uu
2118   "U" gnus-uu-decode-uu-and-save
2119   "s" gnus-uu-decode-unshar
2120   "S" gnus-uu-decode-unshar-and-save
2121   "o" gnus-uu-decode-save
2122   "O" gnus-uu-decode-save
2123   "b" gnus-uu-decode-binhex
2124   "B" gnus-uu-decode-binhex
2125   "p" gnus-uu-decode-postscript
2126   "P" gnus-uu-decode-postscript-and-save)
2127
2128 (gnus-define-keys
2129     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2130   "u" gnus-uu-decode-uu-view
2131   "U" gnus-uu-decode-uu-and-save-view
2132   "s" gnus-uu-decode-unshar-view
2133   "S" gnus-uu-decode-unshar-and-save-view
2134   "o" gnus-uu-decode-save-view
2135   "O" gnus-uu-decode-save-view
2136   "b" gnus-uu-decode-binhex-view
2137   "B" gnus-uu-decode-binhex-view
2138   "p" gnus-uu-decode-postscript-view
2139   "P" gnus-uu-decode-postscript-and-save-view)
2140
2141 (defvar gnus-article-post-menu nil)
2142
2143 (defconst gnus-summary-menu-maxlen 20)
2144
2145 (defun gnus-summary-menu-split (menu)
2146   ;; If we have lots of elements, divide them into groups of 20
2147   ;; and make a pane (or submenu) for each one.
2148   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2149       (let ((menu menu) sublists next
2150             (i 1))
2151         (while menu
2152           ;; Pull off the next gnus-summary-menu-maxlen elements
2153           ;; and make them the next element of sublist.
2154           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2155           (if next
2156               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2157                       nil))
2158           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2159                                              (aref (car (last menu)) 0)) menu)
2160                                sublists))
2161           (setq i (1+ i))
2162           (setq menu next))
2163         (nreverse sublists))
2164     ;; Few elements--put them all in one pane.
2165     menu))
2166
2167 (defun gnus-summary-make-menu-bar ()
2168   (gnus-turn-off-edit-menu 'summary)
2169
2170   (unless (boundp 'gnus-summary-misc-menu)
2171
2172     (easy-menu-define
2173       gnus-summary-kill-menu gnus-summary-mode-map ""
2174       (cons
2175        "Score"
2176        (nconc
2177         (list
2178          ["Customize" gnus-score-customize t])
2179         (gnus-make-score-map 'increase)
2180         (gnus-make-score-map 'lower)
2181         '(("Mark"
2182            ["Kill below" gnus-summary-kill-below t]
2183            ["Mark above" gnus-summary-mark-above t]
2184            ["Tick above" gnus-summary-tick-above t]
2185            ["Clear above" gnus-summary-clear-above t])
2186           ["Current score" gnus-summary-current-score t]
2187           ["Set score" gnus-summary-set-score t]
2188           ["Switch current score file..." gnus-score-change-score-file t]
2189           ["Set mark below..." gnus-score-set-mark-below t]
2190           ["Set expunge below..." gnus-score-set-expunge-below t]
2191           ["Edit current score file" gnus-score-edit-current-scores t]
2192           ["Edit score file" gnus-score-edit-file t]
2193           ["Trace score" gnus-score-find-trace t]
2194           ["Find words" gnus-score-find-favourite-words t]
2195           ["Rescore buffer" gnus-summary-rescore t]
2196           ["Increase score..." gnus-summary-increase-score t]
2197           ["Lower score..." gnus-summary-lower-score t]))))
2198
2199     ;; Define both the Article menu in the summary buffer and the
2200     ;; equivalent Commands menu in the article buffer here for
2201     ;; consistency.
2202     (let ((innards
2203            `(("Hide"
2204               ["All" gnus-article-hide t]
2205               ["Headers" gnus-article-hide-headers t]
2206               ["Signature" gnus-article-hide-signature t]
2207               ["Citation" gnus-article-hide-citation t]
2208               ["List identifiers" gnus-article-hide-list-identifiers t]
2209               ["Banner" gnus-article-strip-banner t]
2210               ["Boring headers" gnus-article-hide-boring-headers t])
2211              ("Highlight"
2212               ["All" gnus-article-highlight t]
2213               ["Headers" gnus-article-highlight-headers t]
2214               ["Signature" gnus-article-highlight-signature t]
2215               ["Citation" gnus-article-highlight-citation t])
2216              ("Date"
2217               ["Local" gnus-article-date-local t]
2218               ["ISO8601" gnus-article-date-iso8601 t]
2219               ["UT" gnus-article-date-ut t]
2220               ["Original" gnus-article-date-original t]
2221               ["Lapsed" gnus-article-date-lapsed t]
2222               ["User-defined" gnus-article-date-user t])
2223              ("Display"
2224               ["Remove images" gnus-article-remove-images t]
2225               ["Toggle smiley" gnus-treat-smiley t]
2226               ["Show X-Face" gnus-article-display-x-face t]
2227               ["Show picons in From" gnus-treat-from-picon t]
2228               ["Show picons in mail headers" gnus-treat-mail-picon t]
2229               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2230               ("View as different encoding"
2231                ,@(gnus-summary-menu-split
2232                   (mapcar
2233                    (lambda (cs)
2234                      ;; Since easymenu under Emacs doesn't allow
2235                      ;; lambda forms for menu commands, we should
2236                      ;; provide intern'ed function symbols.
2237                      (let ((command (intern (format "\
2238 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2239                        (fset command
2240                              `(lambda ()
2241                                 (interactive)
2242                                 (let ((gnus-summary-show-article-charset-alist
2243                                        '((1 . ,cs))))
2244                                   (gnus-summary-show-article 1))))
2245                        `[,(symbol-name cs) ,command t]))
2246                    (sort (if (fboundp 'coding-system-list)
2247                              (coding-system-list)
2248                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2249                            )
2250                          'string<)))))
2251              ("Washing"
2252               ("Remove Blanks"
2253                ["Leading" gnus-article-strip-leading-blank-lines t]
2254                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2255                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2256                ["All of the above" gnus-article-strip-blank-lines t]
2257                ["All" gnus-article-strip-all-blank-lines t]
2258                ["Leading space" gnus-article-strip-leading-space t]
2259                ["Trailing space" gnus-article-strip-trailing-space t]
2260                ["Leading space in headers"
2261                 gnus-article-remove-leading-whitespace t])
2262               ["Overstrike" gnus-article-treat-overstrike t]
2263               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2264               ["Emphasis" gnus-article-emphasize t]
2265               ["Word wrap" gnus-article-fill-cited-article t]
2266               ["Fill long lines" gnus-article-fill-long-lines t]
2267               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2268               ["Remove CR" gnus-article-remove-cr t]
2269               ["Rot 13" gnus-summary-caesar-message
2270                ,@(if (featurep 'xemacs) '(t)
2271                    '(:help "\"Caesar rotate\" article by 13"))]
2272               ["De-IDNA" gnus-summary-idna-message t]
2273               ["Morse decode" gnus-summary-morse-message t]
2274               ["Unix pipe..." gnus-summary-pipe-message t]
2275               ["Add buttons" gnus-article-add-buttons t]
2276               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2277               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2278               ["Toggle MIME" gnus-summary-toggle-mime t]
2279               ["Verbose header" gnus-summary-verbose-headers t]
2280               ["Toggle header" gnus-summary-toggle-header t]
2281               ["Unfold headers" gnus-article-treat-unfold-headers t]
2282               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2283               ["Html" gnus-article-wash-html t]
2284               ["Unsplit URLs" gnus-article-unsplit-urls t]
2285               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2286               ["Decode HZ" gnus-article-decode-HZ t]
2287               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2288               ("(Outlook) Deuglify"
2289                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2290                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2291                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2292                ["Full (Outlook) deuglify"
2293                 gnus-article-outlook-deuglify-article t])
2294               )
2295              ("Output"
2296               ["Save in default format..." gnus-summary-save-article
2297                ,@(if (featurep 'xemacs) '(t)
2298                    '(:help "Save article using default method"))]
2299               ["Save in file..." gnus-summary-save-article-file
2300                ,@(if (featurep 'xemacs) '(t)
2301                    '(:help "Save article in file"))]
2302               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2303               ["Save in MH folder..." gnus-summary-save-article-folder t]
2304               ["Save in VM folder..." gnus-summary-save-article-vm t]
2305               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2306               ["Save body in file..." gnus-summary-save-article-body-file t]
2307               ["Pipe through a filter..." gnus-summary-pipe-output t]
2308               ["Add to SOUP packet" gnus-soup-add-article t]
2309               ["Print with Muttprint..." gnus-summary-muttprint t]
2310               ["Print" gnus-summary-print-article
2311                ,@(if (featurep 'xemacs) '(t)
2312                    '(:help "Generate and print a PostScript image"))])
2313              ("Copy, move,... (Backend)"
2314               ,@(if (featurep 'xemacs) nil
2315                   '(:help "Copying, moving, expiring articles..."))
2316               ["Respool article..." gnus-summary-respool-article t]
2317               ["Move article..." gnus-summary-move-article
2318                (gnus-check-backend-function
2319                 'request-move-article gnus-newsgroup-name)]
2320               ["Copy article..." gnus-summary-copy-article t]
2321               ["Crosspost article..." gnus-summary-crosspost-article
2322                (gnus-check-backend-function
2323                 'request-replace-article gnus-newsgroup-name)]
2324               ["Import file..." gnus-summary-import-article
2325                (gnus-check-backend-function
2326                 'request-accept-article gnus-newsgroup-name)]
2327               ["Create article..." gnus-summary-create-article
2328                (gnus-check-backend-function
2329                 'request-accept-article gnus-newsgroup-name)]
2330               ["Check if posted" gnus-summary-article-posted-p t]
2331               ["Edit article" gnus-summary-edit-article
2332                (not (gnus-group-read-only-p))]
2333               ["Delete article" gnus-summary-delete-article
2334                (gnus-check-backend-function
2335                 'request-expire-articles gnus-newsgroup-name)]
2336               ["Query respool" gnus-summary-respool-query t]
2337               ["Trace respool" gnus-summary-respool-trace t]
2338               ["Delete expirable articles" gnus-summary-expire-articles-now
2339                (gnus-check-backend-function
2340                 'request-expire-articles gnus-newsgroup-name)])
2341              ("Extract"
2342               ["Uudecode" gnus-uu-decode-uu
2343                ,@(if (featurep 'xemacs) '(t)
2344                    '(:help "Decode uuencoded article(s)"))]
2345               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2346               ["Unshar" gnus-uu-decode-unshar t]
2347               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2348               ["Save" gnus-uu-decode-save t]
2349               ["Binhex" gnus-uu-decode-binhex t]
2350               ["Postscript" gnus-uu-decode-postscript t])
2351              ("Cache"
2352               ["Enter article" gnus-cache-enter-article t]
2353               ["Remove article" gnus-cache-remove-article t])
2354              ["Translate" gnus-article-babel t]
2355              ["Select article buffer" gnus-summary-select-article-buffer t]
2356              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2357              ["Isearch article..." gnus-summary-isearch-article t]
2358              ["Beginning of the article" gnus-summary-beginning-of-article t]
2359              ["End of the article" gnus-summary-end-of-article t]
2360              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2361              ["Fetch referenced articles" gnus-summary-refer-references t]
2362              ["Fetch current thread" gnus-summary-refer-thread t]
2363              ["Fetch article with id..." gnus-summary-refer-article t]
2364              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2365              ["Redisplay" gnus-summary-show-article t]
2366              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2367       (easy-menu-define
2368         gnus-summary-article-menu gnus-summary-mode-map ""
2369         (cons "Article" innards))
2370
2371       (if (not (keymapp gnus-summary-article-menu))
2372           (easy-menu-define
2373             gnus-article-commands-menu gnus-article-mode-map ""
2374             (cons "Commands" innards))
2375         ;; in Emacs, don't share menu.
2376         (setq gnus-article-commands-menu
2377               (copy-keymap gnus-summary-article-menu))
2378         (define-key gnus-article-mode-map [menu-bar commands]
2379           (cons "Commands" gnus-article-commands-menu))))
2380
2381     (easy-menu-define
2382       gnus-summary-thread-menu gnus-summary-mode-map ""
2383       '("Threads"
2384         ["Find all messages in thread" gnus-summary-refer-thread t]
2385         ["Toggle threading" gnus-summary-toggle-threads t]
2386         ["Hide threads" gnus-summary-hide-all-threads t]
2387         ["Show threads" gnus-summary-show-all-threads t]
2388         ["Hide thread" gnus-summary-hide-thread t]
2389         ["Show thread" gnus-summary-show-thread t]
2390         ["Go to next thread" gnus-summary-next-thread t]
2391         ["Go to previous thread" gnus-summary-prev-thread t]
2392         ["Go down thread" gnus-summary-down-thread t]
2393         ["Go up thread" gnus-summary-up-thread t]
2394         ["Top of thread" gnus-summary-top-thread t]
2395         ["Mark thread as read" gnus-summary-kill-thread t]
2396         ["Lower thread score" gnus-summary-lower-thread t]
2397         ["Raise thread score" gnus-summary-raise-thread t]
2398         ["Rethread current" gnus-summary-rethread-current t]))
2399
2400     (easy-menu-define
2401       gnus-summary-post-menu gnus-summary-mode-map ""
2402       `("Post"
2403         ["Send a message (mail or news)" gnus-summary-post-news
2404          ,@(if (featurep 'xemacs) '(t)
2405              '(:help "Compose a new message (mail or news)"))]
2406         ["Followup" gnus-summary-followup
2407          ,@(if (featurep 'xemacs) '(t)
2408              '(:help "Post followup to this article"))]
2409         ["Followup and yank" gnus-summary-followup-with-original
2410          ,@(if (featurep 'xemacs) '(t)
2411              '(:help "Post followup to this article, quoting its contents"))]
2412         ["Supersede article" gnus-summary-supersede-article t]
2413         ["Cancel article" gnus-summary-cancel-article
2414          ,@(if (featurep 'xemacs) '(t)
2415              '(:help "Cancel an article you posted"))]
2416         ["Reply" gnus-summary-reply t]
2417         ["Reply and yank" gnus-summary-reply-with-original t]
2418         ["Wide reply" gnus-summary-wide-reply t]
2419         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2420          ,@(if (featurep 'xemacs) '(t)
2421              '(:help "Mail a reply, quoting this article"))]
2422         ["Very wide reply" gnus-summary-very-wide-reply t]
2423         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2424          ,@(if (featurep 'xemacs) '(t)
2425              '(:help "Mail a very wide reply, quoting this article"))]
2426         ["Mail forward" gnus-summary-mail-forward t]
2427         ["Post forward" gnus-summary-post-forward t]
2428         ["Digest and mail" gnus-summary-digest-mail-forward t]
2429         ["Digest and post" gnus-summary-digest-post-forward t]
2430         ["Resend message" gnus-summary-resend-message t]
2431         ["Resend message edit" gnus-summary-resend-message-edit t]
2432         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2433         ["Send a mail" gnus-summary-mail-other-window t]
2434         ["Create a local message" gnus-summary-news-other-window t]
2435         ["Uuencode and post" gnus-uu-post-news
2436          ,@(if (featurep 'xemacs) '(t)
2437              '(:help "Post a uuencoded article"))]
2438         ["Followup via news" gnus-summary-followup-to-mail t]
2439         ["Followup via news and yank"
2440          gnus-summary-followup-to-mail-with-original t]
2441         ;;("Draft"
2442         ;;["Send" gnus-summary-send-draft t]
2443         ;;["Send bounced" gnus-resend-bounced-mail t])
2444         ))
2445
2446     (cond
2447      ((not (keymapp gnus-summary-post-menu))
2448       (setq gnus-article-post-menu gnus-summary-post-menu))
2449      ((not gnus-article-post-menu)
2450       ;; Don't share post menu.
2451       (setq gnus-article-post-menu
2452             (copy-keymap gnus-summary-post-menu))))
2453     (define-key gnus-article-mode-map [menu-bar post]
2454       (cons "Post" gnus-article-post-menu))
2455
2456     (easy-menu-define
2457       gnus-summary-misc-menu gnus-summary-mode-map ""
2458       `("Gnus"
2459         ("Mark Read"
2460          ["Mark as read" gnus-summary-mark-as-read-forward t]
2461          ["Mark same subject and select"
2462           gnus-summary-kill-same-subject-and-select t]
2463          ["Mark same subject" gnus-summary-kill-same-subject t]
2464          ["Catchup" gnus-summary-catchup
2465           ,@(if (featurep 'xemacs) '(t)
2466               '(:help "Mark unread articles in this group as read"))]
2467          ["Catchup all" gnus-summary-catchup-all t]
2468          ["Catchup to here" gnus-summary-catchup-to-here t]
2469          ["Catchup from here" gnus-summary-catchup-from-here t]
2470          ["Catchup region" gnus-summary-mark-region-as-read
2471           (gnus-mark-active-p)]
2472          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2473         ("Mark Various"
2474          ["Tick" gnus-summary-tick-article-forward t]
2475          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2476          ["Remove marks" gnus-summary-clear-mark-forward t]
2477          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2478          ["Set bookmark" gnus-summary-set-bookmark t]
2479          ["Remove bookmark" gnus-summary-remove-bookmark t])
2480         ("Limit to"
2481          ["Marks..." gnus-summary-limit-to-marks t]
2482          ["Subject..." gnus-summary-limit-to-subject t]
2483          ["Author..." gnus-summary-limit-to-author t]
2484          ["Recipient..." gnus-summary-limit-to-recipient t]
2485          ["Age..." gnus-summary-limit-to-age t]
2486          ["Extra..." gnus-summary-limit-to-extra t]
2487          ["Score..." gnus-summary-limit-to-score t]
2488          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2489          ["Unread" gnus-summary-limit-to-unread t]
2490          ["Unseen" gnus-summary-limit-to-unseen t]
2491          ["Replied" gnus-summary-limit-to-replied t]
2492          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2493          ["Next articles" gnus-summary-limit-to-articles t]
2494          ["Pop limit" gnus-summary-pop-limit t]
2495          ["Show dormant" gnus-summary-limit-include-dormant t]
2496          ["Hide childless dormant"
2497           gnus-summary-limit-exclude-childless-dormant t]
2498          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2499          ["Hide marked" gnus-summary-limit-exclude-marks t]
2500          ["Show expunged" gnus-summary-limit-include-expunged t])
2501         ("Process Mark"
2502          ["Set mark" gnus-summary-mark-as-processable t]
2503          ["Remove mark" gnus-summary-unmark-as-processable t]
2504          ["Remove all marks" gnus-summary-unmark-all-processable t]
2505          ["Invert marks" gnus-uu-invert-processable t]
2506          ["Mark above" gnus-uu-mark-over t]
2507          ["Mark series" gnus-uu-mark-series t]
2508          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2509          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2510          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2511          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2512          ["Mark all" gnus-uu-mark-all t]
2513          ["Mark buffer" gnus-uu-mark-buffer t]
2514          ["Mark sparse" gnus-uu-mark-sparse t]
2515          ["Mark thread" gnus-uu-mark-thread t]
2516          ["Unmark thread" gnus-uu-unmark-thread t]
2517          ("Process Mark Sets"
2518           ["Kill" gnus-summary-kill-process-mark t]
2519           ["Yank" gnus-summary-yank-process-mark
2520            gnus-newsgroup-process-stack]
2521           ["Save" gnus-summary-save-process-mark t]
2522           ["Run command on marked..." gnus-summary-universal-argument t]))
2523         ("Scroll article"
2524          ["Page forward" gnus-summary-next-page
2525           ,@(if (featurep 'xemacs) '(t)
2526               '(:help "Show next page of article"))]
2527          ["Page backward" gnus-summary-prev-page
2528           ,@(if (featurep 'xemacs) '(t)
2529               '(:help "Show previous page of article"))]
2530          ["Line forward" gnus-summary-scroll-up t])
2531         ("Move"
2532          ["Next unread article" gnus-summary-next-unread-article t]
2533          ["Previous unread article" gnus-summary-prev-unread-article t]
2534          ["Next article" gnus-summary-next-article t]
2535          ["Previous article" gnus-summary-prev-article t]
2536          ["Next unread subject" gnus-summary-next-unread-subject t]
2537          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2538          ["Next article same subject" gnus-summary-next-same-subject t]
2539          ["Previous article same subject" gnus-summary-prev-same-subject t]
2540          ["First unread article" gnus-summary-first-unread-article t]
2541          ["Best unread article" gnus-summary-best-unread-article t]
2542          ["Go to subject number..." gnus-summary-goto-subject t]
2543          ["Go to article number..." gnus-summary-goto-article t]
2544          ["Go to the last article" gnus-summary-goto-last-article t]
2545          ["Pop article off history" gnus-summary-pop-article t])
2546         ("Sort"
2547          ["Sort by number" gnus-summary-sort-by-number t]
2548          ["Sort by author" gnus-summary-sort-by-author t]
2549          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2550          ["Sort by subject" gnus-summary-sort-by-subject t]
2551          ["Sort by date" gnus-summary-sort-by-date t]
2552          ["Sort by score" gnus-summary-sort-by-score t]
2553          ["Sort by lines" gnus-summary-sort-by-lines t]
2554          ["Sort by characters" gnus-summary-sort-by-chars t]
2555          ["Randomize" gnus-summary-sort-by-random t]
2556          ["Original sort" gnus-summary-sort-by-original t])
2557         ("Help"
2558          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2559          ["Describe group" gnus-summary-describe-group t]
2560          ["Fetch charter" gnus-group-fetch-charter
2561           ,@(if (featurep 'xemacs) nil
2562               '(:help "Display the charter of the current group"))]
2563          ["Fetch control message" gnus-group-fetch-control
2564           ,@(if (featurep 'xemacs) nil
2565               '(:help "Display the archived control message for the current group"))]
2566          ["Read manual" gnus-info-find-node t])
2567         ("Modes"
2568          ["Pick and read" gnus-pick-mode t]
2569          ["Binary" gnus-binary-mode t])
2570         ("Regeneration"
2571          ["Regenerate" gnus-summary-prepare t]
2572          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2573          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2574          ["Toggle threading" gnus-summary-toggle-threads t])
2575         ["See old articles" gnus-summary-insert-old-articles t]
2576         ["See new articles" gnus-summary-insert-new-articles t]
2577         ["Filter articles..." gnus-summary-execute-command t]
2578         ["Run command on articles..." gnus-summary-universal-argument t]
2579         ["Search articles forward..." gnus-summary-search-article-forward t]
2580         ["Search articles backward..." gnus-summary-search-article-backward t]
2581         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2582         ["Expand window" gnus-summary-expand-window t]
2583         ["Expire expirable articles" gnus-summary-expire-articles
2584          (gnus-check-backend-function
2585           'request-expire-articles gnus-newsgroup-name)]
2586         ["Edit local kill file" gnus-summary-edit-local-kill t]
2587         ["Edit main kill file" gnus-summary-edit-global-kill t]
2588         ["Edit group parameters" gnus-summary-edit-parameters t]
2589         ["Customize group parameters" gnus-summary-customize-parameters t]
2590         ["Send a bug report" gnus-bug t]
2591         ("Exit"
2592          ["Catchup and exit" gnus-summary-catchup-and-exit
2593           ,@(if (featurep 'xemacs) '(t)
2594               '(:help "Mark unread articles in this group as read, then exit"))]
2595          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2596          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2597          ["Exit group" gnus-summary-exit
2598           ,@(if (featurep 'xemacs) '(t)
2599               '(:help "Exit current group, return to group selection mode"))]
2600          ["Exit group without updating" gnus-summary-exit-no-update t]
2601          ["Exit and goto next group" gnus-summary-next-group t]
2602          ["Exit and goto prev group" gnus-summary-prev-group t]
2603          ["Reselect group" gnus-summary-reselect-current-group t]
2604          ["Rescan group" gnus-summary-rescan-group t]
2605          ["Update dribble" gnus-summary-save-newsrc t])))
2606
2607     (gnus-run-hooks 'gnus-summary-menu-hook)))
2608
2609 (defvar gnus-summary-tool-bar-map nil)
2610
2611 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2612 (defun gnus-summary-make-tool-bar ()
2613   (if (and (fboundp 'tool-bar-add-item-from-menu)
2614            (default-value 'tool-bar-mode)
2615            (not gnus-summary-tool-bar-map))
2616       (setq gnus-summary-tool-bar-map
2617             (let ((tool-bar-map (make-sparse-keymap))
2618                   (load-path (mm-image-load-path)))
2619               (tool-bar-add-item-from-menu
2620                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2621               (tool-bar-add-item-from-menu
2622                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2623               (tool-bar-add-item-from-menu
2624                'gnus-summary-post-news "post" gnus-summary-mode-map)
2625               (tool-bar-add-item-from-menu
2626                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2627               (tool-bar-add-item-from-menu
2628                'gnus-summary-followup "followup" gnus-summary-mode-map)
2629               (tool-bar-add-item-from-menu
2630                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2631               (tool-bar-add-item-from-menu
2632                'gnus-summary-reply "reply" gnus-summary-mode-map)
2633               (tool-bar-add-item-from-menu
2634                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2635               (tool-bar-add-item-from-menu
2636                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2637               (tool-bar-add-item-from-menu
2638                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2639               (tool-bar-add-item-from-menu
2640                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2641               (tool-bar-add-item-from-menu
2642                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2643               (tool-bar-add-item-from-menu
2644                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2645               (tool-bar-add-item-from-menu
2646                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2647               (tool-bar-add-item-from-menu
2648                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2649               (tool-bar-add-item-from-menu
2650                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2651               tool-bar-map)))
2652   (if gnus-summary-tool-bar-map
2653       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2654
2655 (defun gnus-score-set-default (var value)
2656   "A version of set that updates the GNU Emacs menu-bar."
2657   (set var value)
2658   ;; It is the message that forces the active status to be updated.
2659   (message ""))
2660
2661 (defun gnus-make-score-map (type)
2662   "Make a summary score map of type TYPE."
2663   (if t
2664       nil
2665     (let ((headers '(("author" "from" string)
2666                      ("subject" "subject" string)
2667                      ("article body" "body" string)
2668                      ("article head" "head" string)
2669                      ("xref" "xref" string)
2670                      ("extra header" "extra" string)
2671                      ("lines" "lines" number)
2672                      ("followups to author" "followup" string)))
2673           (types '((number ("less than" <)
2674                            ("greater than" >)
2675                            ("equal" =))
2676                    (string ("substring" s)
2677                            ("exact string" e)
2678                            ("fuzzy string" f)
2679                            ("regexp" r))))
2680           (perms '(("temporary" (current-time-string))
2681                    ("permanent" nil)
2682                    ("immediate" now)))
2683           header)
2684       (list
2685        (apply
2686         'nconc
2687         (list
2688          (if (eq type 'lower)
2689              "Lower score"
2690            "Increase score"))
2691         (let (outh)
2692           (while headers
2693             (setq header (car headers))
2694             (setq outh
2695                   (cons
2696                    (apply
2697                     'nconc
2698                     (list (car header))
2699                     (let ((ts (cdr (assoc (nth 2 header) types)))
2700                           outt)
2701                       (while ts
2702                         (setq outt
2703                               (cons
2704                                (apply
2705                                 'nconc
2706                                 (list (caar ts))
2707                                 (let ((ps perms)
2708                                       outp)
2709                                   (while ps
2710                                     (setq outp
2711                                           (cons
2712                                            (vector
2713                                             (caar ps)
2714                                             (list
2715                                              'gnus-summary-score-entry
2716                                              (nth 1 header)
2717                                              (if (or (string= (nth 1 header)
2718                                                               "head")
2719                                                      (string= (nth 1 header)
2720                                                               "body"))
2721                                                  ""
2722                                                (list 'gnus-summary-header
2723                                                      (nth 1 header)))
2724                                              (list 'quote (nth 1 (car ts)))
2725                                              (list 'gnus-score-delta-default
2726                                                    nil)
2727                                              (nth 1 (car ps))
2728                                              t)
2729                                             t)
2730                                            outp))
2731                                     (setq ps (cdr ps)))
2732                                   (list (nreverse outp))))
2733                                outt))
2734                         (setq ts (cdr ts)))
2735                       (list (nreverse outt))))
2736                    outh))
2737             (setq headers (cdr headers)))
2738           (list (nreverse outh))))))))
2739
2740 \f
2741
2742 (defun gnus-summary-mode (&optional group)
2743   "Major mode for reading articles.
2744
2745 All normal editing commands are switched off.
2746 \\<gnus-summary-mode-map>
2747 Each line in this buffer represents one article.  To read an
2748 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2749 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2750 respectively.
2751
2752 You can also post articles and send mail from this buffer.  To
2753 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2754 of an article, type `\\[gnus-summary-reply]'.
2755
2756 There are approx. one gazillion commands you can execute in this
2757 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2758
2759 The following commands are available:
2760
2761 \\{gnus-summary-mode-map}"
2762   (interactive)
2763   (kill-all-local-variables)
2764   (when (gnus-visual-p 'summary-menu 'menu)
2765     (gnus-summary-make-menu-bar)
2766     (gnus-summary-make-tool-bar))
2767   (gnus-summary-make-local-variables)
2768   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2769     (gnus-summary-make-local-variables))
2770   (gnus-make-thread-indent-array)
2771   (gnus-simplify-mode-line)
2772   (setq major-mode 'gnus-summary-mode)
2773   (setq mode-name "Summary")
2774   (make-local-variable 'minor-mode-alist)
2775   (use-local-map gnus-summary-mode-map)
2776   (buffer-disable-undo)
2777   (setq buffer-read-only t              ;Disable modification
2778         show-trailing-whitespace nil)
2779   (setq truncate-lines t)
2780   (setq selective-display t)
2781   (setq selective-display-ellipses t)   ;Display `...'
2782   (gnus-summary-set-display-table)
2783   (gnus-set-default-directory)
2784   (setq gnus-newsgroup-name group)
2785   (unless (gnus-news-group-p group)
2786     (setq gnus-newsgroup-incorporated
2787           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2788   (make-local-variable 'gnus-summary-line-format)
2789   (make-local-variable 'gnus-summary-line-format-spec)
2790   (make-local-variable 'gnus-summary-dummy-line-format)
2791   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2792   (make-local-variable 'gnus-summary-mark-positions)
2793   (gnus-make-local-hook 'pre-command-hook)
2794   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2795   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2796   (turn-on-gnus-mailing-list-mode)
2797   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2798   (gnus-update-summary-mark-positions))
2799
2800 (defun gnus-summary-make-local-variables ()
2801   "Make all the local summary buffer variables."
2802   (let (global)
2803     (dolist (local gnus-summary-local-variables)
2804       (if (consp local)
2805           (progn
2806             (if (eq (cdr local) 'global)
2807                 ;; Copy the global value of the variable.
2808                 (setq global (symbol-value (car local)))
2809               ;; Use the value from the list.
2810               (setq global (eval (cdr local))))
2811             (set (make-local-variable (car local)) global))
2812         ;; Simple nil-valued local variable.
2813         (set (make-local-variable local) nil)))))
2814
2815 (defun gnus-summary-clear-local-variables ()
2816   (let ((locals gnus-summary-local-variables))
2817     (while locals
2818       (if (consp (car locals))
2819           (and (symbolp (caar locals))
2820                (set (caar locals) nil))
2821         (and (symbolp (car locals))
2822              (set (car locals) nil)))
2823       (setq locals (cdr locals)))))
2824
2825 ;; Summary data functions.
2826
2827 (defmacro gnus-data-number (data)
2828   `(car ,data))
2829
2830 (defmacro gnus-data-set-number (data number)
2831   `(setcar ,data ,number))
2832
2833 (defmacro gnus-data-mark (data)
2834   `(nth 1 ,data))
2835
2836 (defmacro gnus-data-set-mark (data mark)
2837   `(setcar (nthcdr 1 ,data) ,mark))
2838
2839 (defmacro gnus-data-pos (data)
2840   `(nth 2 ,data))
2841
2842 (defmacro gnus-data-set-pos (data pos)
2843   `(setcar (nthcdr 2 ,data) ,pos))
2844
2845 (defmacro gnus-data-header (data)
2846   `(nth 3 ,data))
2847
2848 (defmacro gnus-data-set-header (data header)
2849   `(setcar (nthcdr 3 ,data) ,header))
2850
2851 (defmacro gnus-data-level (data)
2852   `(nth 4 ,data))
2853
2854 (defmacro gnus-data-unread-p (data)
2855   `(= (nth 1 ,data) gnus-unread-mark))
2856
2857 (defmacro gnus-data-read-p (data)
2858   `(/= (nth 1 ,data) gnus-unread-mark))
2859
2860 (defmacro gnus-data-pseudo-p (data)
2861   `(consp (nth 3 ,data)))
2862
2863 (defmacro gnus-data-find (number)
2864   `(assq ,number gnus-newsgroup-data))
2865
2866 (defmacro gnus-data-find-list (number &optional data)
2867   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2868      (memq (assq ,number bdata)
2869            bdata)))
2870
2871 (defmacro gnus-data-make (number mark pos header level)
2872   `(list ,number ,mark ,pos ,header ,level))
2873
2874 (defun gnus-data-enter (after-article number mark pos header level offset)
2875   (let ((data (gnus-data-find-list after-article)))
2876     (unless data
2877       (error "No such article: %d" after-article))
2878     (setcdr data (cons (gnus-data-make number mark pos header level)
2879                        (cdr data)))
2880     (setq gnus-newsgroup-data-reverse nil)
2881     (gnus-data-update-list (cddr data) offset)))
2882
2883 (defun gnus-data-enter-list (after-article list &optional offset)
2884   (when list
2885     (let ((data (and after-article (gnus-data-find-list after-article)))
2886           (ilist list))
2887       (if (not (or data
2888                    after-article))
2889           (let ((odata gnus-newsgroup-data))
2890             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2891             (when offset
2892               (gnus-data-update-list odata offset)))
2893         ;; Find the last element in the list to be spliced into the main
2894         ;; list.
2895         (setq list (last list))
2896         (if (not data)
2897             (progn
2898               (setcdr list gnus-newsgroup-data)
2899               (setq gnus-newsgroup-data ilist)
2900               (when offset
2901                 (gnus-data-update-list (cdr list) offset)))
2902           (setcdr list (cdr data))
2903           (setcdr data ilist)
2904           (when offset
2905             (gnus-data-update-list (cdr list) offset))))
2906       (setq gnus-newsgroup-data-reverse nil))))
2907
2908 (defun gnus-data-remove (article &optional offset)
2909   (let ((data gnus-newsgroup-data))
2910     (if (= (gnus-data-number (car data)) article)
2911         (progn
2912           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2913                 gnus-newsgroup-data-reverse nil)
2914           (when offset
2915             (gnus-data-update-list gnus-newsgroup-data offset)))
2916       (while (cdr data)
2917         (when (= (gnus-data-number (cadr data)) article)
2918           (setcdr data (cddr data))
2919           (when offset
2920             (gnus-data-update-list (cdr data) offset))
2921           (setq data nil
2922                 gnus-newsgroup-data-reverse nil))
2923         (setq data (cdr data))))))
2924
2925 (defmacro gnus-data-list (backward)
2926   `(if ,backward
2927        (or gnus-newsgroup-data-reverse
2928            (setq gnus-newsgroup-data-reverse
2929                  (reverse gnus-newsgroup-data)))
2930      gnus-newsgroup-data))
2931
2932 (defun gnus-data-update-list (data offset)
2933   "Add OFFSET to the POS of all data entries in DATA."
2934   (setq gnus-newsgroup-data-reverse nil)
2935   (while data
2936     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2937     (setq data (cdr data))))
2938
2939 (defun gnus-summary-article-pseudo-p (article)
2940   "Say whether this article is a pseudo article or not."
2941   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2942
2943 (defmacro gnus-summary-article-sparse-p (article)
2944   "Say whether this article is a sparse article or not."
2945   `(memq ,article gnus-newsgroup-sparse))
2946
2947 (defmacro gnus-summary-article-ancient-p (article)
2948   "Say whether this article is a sparse article or not."
2949   `(memq ,article gnus-newsgroup-ancient))
2950
2951 (defun gnus-article-parent-p (number)
2952   "Say whether this article is a parent or not."
2953   (let ((data (gnus-data-find-list number)))
2954     (and (cdr data)                     ; There has to be an article after...
2955          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2956             (gnus-data-level (nth 1 data))))))
2957
2958 (defun gnus-article-children (number)
2959   "Return a list of all children to NUMBER."
2960   (let* ((data (gnus-data-find-list number))
2961          (level (gnus-data-level (car data)))
2962          children)
2963     (setq data (cdr data))
2964     (while (and data
2965                 (= (gnus-data-level (car data)) (1+ level)))
2966       (push (gnus-data-number (car data)) children)
2967       (setq data (cdr data)))
2968     children))
2969
2970 (defmacro gnus-summary-skip-intangible ()
2971   "If the current article is intangible, then jump to a different article."
2972   '(let ((to (get-text-property (point) 'gnus-intangible)))
2973      (and to (gnus-summary-goto-subject to))))
2974
2975 (defmacro gnus-summary-article-intangible-p ()
2976   "Say whether this article is intangible or not."
2977   '(get-text-property (point) 'gnus-intangible))
2978
2979 (defun gnus-article-read-p (article)
2980   "Say whether ARTICLE is read or not."
2981   (not (or (memq article gnus-newsgroup-marked)
2982            (memq article gnus-newsgroup-spam-marked)
2983            (memq article gnus-newsgroup-unreads)
2984            (memq article gnus-newsgroup-unselected)
2985            (memq article gnus-newsgroup-dormant))))
2986
2987 ;; Some summary mode macros.
2988
2989 (defmacro gnus-summary-article-number ()
2990   "The article number of the article on the current line.
2991 If there isn't an article number here, then we return the current
2992 article number."
2993   '(progn
2994      (gnus-summary-skip-intangible)
2995      (or (get-text-property (point) 'gnus-number)
2996          (gnus-summary-last-subject))))
2997
2998 (defmacro gnus-summary-article-header (&optional number)
2999   "Return the header of article NUMBER."
3000   `(gnus-data-header (gnus-data-find
3001                       ,(or number '(gnus-summary-article-number)))))
3002
3003 (defmacro gnus-summary-thread-level (&optional number)
3004   "Return the level of thread that starts with article NUMBER."
3005   `(if (and (eq gnus-summary-make-false-root 'dummy)
3006             (get-text-property (point) 'gnus-intangible))
3007        0
3008      (gnus-data-level (gnus-data-find
3009                        ,(or number '(gnus-summary-article-number))))))
3010
3011 (defmacro gnus-summary-article-mark (&optional number)
3012   "Return the mark of article NUMBER."
3013   `(gnus-data-mark (gnus-data-find
3014                     ,(or number '(gnus-summary-article-number)))))
3015
3016 (defmacro gnus-summary-article-pos (&optional number)
3017   "Return the position of the line of article NUMBER."
3018   `(gnus-data-pos (gnus-data-find
3019                    ,(or number '(gnus-summary-article-number)))))
3020
3021 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3022 (defmacro gnus-summary-article-subject (&optional number)
3023   "Return current subject string or nil if nothing."
3024   `(let ((headers
3025           ,(if number
3026                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3027              '(gnus-data-header (assq (gnus-summary-article-number)
3028                                       gnus-newsgroup-data)))))
3029      (and headers
3030           (vectorp headers)
3031           (mail-header-subject headers))))
3032
3033 (defmacro gnus-summary-article-score (&optional number)
3034   "Return current article score."
3035   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3036                   gnus-newsgroup-scored))
3037        gnus-summary-default-score 0))
3038
3039 (defun gnus-summary-article-children (&optional number)
3040   "Return a list of article numbers that are children of article NUMBER."
3041   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3042          (level (gnus-data-level (car data)))
3043          l children)
3044     (while (and (setq data (cdr data))
3045                 (> (setq l (gnus-data-level (car data))) level))
3046       (and (= (1+ level) l)
3047            (push (gnus-data-number (car data))
3048                  children)))
3049     (nreverse children)))
3050
3051 (defun gnus-summary-article-parent (&optional number)
3052   "Return the article number of the parent of article NUMBER."
3053   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3054                                     (gnus-data-list t)))
3055          (level (gnus-data-level (car data))))
3056     (if (zerop level)
3057         ()                              ; This is a root.
3058       ;; We search until we find an article with a level less than
3059       ;; this one.  That function has to be the parent.
3060       (while (and (setq data (cdr data))
3061                   (not (< (gnus-data-level (car data)) level))))
3062       (and data (gnus-data-number (car data))))))
3063
3064 (defun gnus-unread-mark-p (mark)
3065   "Say whether MARK is the unread mark."
3066   (= mark gnus-unread-mark))
3067
3068 (defun gnus-read-mark-p (mark)
3069   "Say whether MARK is one of the marks that mark as read.
3070 This is all marks except unread, ticked, dormant, and expirable."
3071   (not (or (= mark gnus-unread-mark)
3072            (= mark gnus-ticked-mark)
3073            (= mark gnus-spam-mark)
3074            (= mark gnus-dormant-mark)
3075            (= mark gnus-expirable-mark))))
3076
3077 (defmacro gnus-article-mark (number)
3078   "Return the MARK of article NUMBER.
3079 This macro should only be used when computing the mark the \"first\"
3080 time; i.e., when generating the summary lines.  After that,
3081 `gnus-summary-article-mark' should be used to examine the
3082 marks of articles."
3083   `(cond
3084     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3085     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3086     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3087     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3088     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3089     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3090     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3091     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3092            gnus-ancient-mark))))
3093
3094 ;; Saving hidden threads.
3095
3096 (defmacro gnus-save-hidden-threads (&rest forms)
3097   "Save hidden threads, eval FORMS, and restore the hidden threads."
3098   (let ((config (make-symbol "config")))
3099     `(let ((,config (gnus-hidden-threads-configuration)))
3100        (unwind-protect
3101            (save-excursion
3102              ,@forms)
3103          (gnus-restore-hidden-threads-configuration ,config)))))
3104 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3105 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3106
3107 (defun gnus-data-compute-positions ()
3108   "Compute the positions of all articles."
3109   (setq gnus-newsgroup-data-reverse nil)
3110   (let ((data gnus-newsgroup-data))
3111     (save-excursion
3112       (gnus-save-hidden-threads
3113         (gnus-summary-show-all-threads)
3114         (goto-char (point-min))
3115         (while data
3116           (while (get-text-property (point) 'gnus-intangible)
3117             (forward-line 1))
3118           (gnus-data-set-pos (car data) (+ (point) 3))
3119           (setq data (cdr data))
3120           (forward-line 1))))))
3121
3122 (defun gnus-hidden-threads-configuration ()
3123   "Return the current hidden threads configuration."
3124   (save-excursion
3125     (let (config)
3126       (goto-char (point-min))
3127       (while (search-forward "\r" nil t)
3128         (push (1- (point)) config))
3129       config)))
3130
3131 (defun gnus-restore-hidden-threads-configuration (config)
3132   "Restore hidden threads configuration from CONFIG."
3133   (save-excursion
3134     (let (point buffer-read-only)
3135       (while (setq point (pop config))
3136         (when (and (< point (point-max))
3137                    (goto-char point)
3138                    (eq (char-after) ?\n))
3139           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3140
3141 ;; Various summary mode internalish functions.
3142
3143 (defun gnus-mouse-pick-article (e)
3144   (interactive "e")
3145   (mouse-set-point e)
3146   (gnus-summary-next-page nil t))
3147
3148 (defun gnus-summary-set-display-table ()
3149   "Change the display table.
3150 Odd characters have a tendency to mess
3151 up nicely formatted displays - we make all possible glyphs
3152 display only a single character."
3153
3154   ;; We start from the standard display table, if any.
3155   (let ((table (or (copy-sequence standard-display-table)
3156                    (make-display-table)))
3157         (i 32))
3158     ;; Nix out all the control chars...
3159     (while (>= (setq i (1- i)) 0)
3160       (aset table i [??]))
3161     ;; ... but not newline and cr, of course.  (cr is necessary for the
3162     ;; selective display).
3163     (aset table ?\n nil)
3164     (aset table ?\r nil)
3165     ;; We keep TAB as well.
3166     (aset table ?\t nil)
3167     ;; We nix out any glyphs over 126 that are not set already.
3168     (let ((i 256))
3169       (while (>= (setq i (1- i)) 127)
3170         ;; Only modify if the entry is nil.
3171         (unless (aref table i)
3172           (aset table i [??]))))
3173     (setq buffer-display-table table)))
3174
3175 (defun gnus-summary-set-article-display-arrow (pos)
3176   "Update the overlay arrow to point to line at position POS."
3177   (when (and gnus-summary-display-arrow
3178              (boundp 'overlay-arrow-position)
3179              (boundp 'overlay-arrow-string))
3180     (save-excursion
3181       (goto-char pos)
3182       (beginning-of-line)
3183       (unless overlay-arrow-position
3184         (setq overlay-arrow-position (make-marker)))
3185       (setq overlay-arrow-string "=>"
3186             overlay-arrow-position (set-marker overlay-arrow-position
3187                                                (point)
3188                                                (current-buffer))))))
3189
3190 (defun gnus-summary-setup-buffer (group)
3191   "Initialize summary buffer."
3192   (let ((buffer (gnus-summary-buffer-name group))
3193         (dead-name (concat "*Dead Summary "
3194                            (gnus-group-decoded-name group) "*")))
3195     ;; If a dead summary buffer exists, we kill it.
3196     (when (gnus-buffer-live-p dead-name)
3197       (gnus-kill-buffer dead-name))
3198     (if (get-buffer buffer)
3199         (progn
3200           (set-buffer buffer)
3201           (setq gnus-summary-buffer (current-buffer))
3202           (not gnus-newsgroup-prepared))
3203       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3204       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3205       (gnus-summary-mode group)
3206       (when gnus-carpal
3207         (gnus-carpal-setup-buffer 'summary))
3208       (unless gnus-single-article-buffer
3209         (make-local-variable 'gnus-article-buffer)
3210         (make-local-variable 'gnus-article-current)
3211         (make-local-variable 'gnus-original-article-buffer))
3212       (setq gnus-newsgroup-name group)
3213       ;; Set any local variables in the group parameters.
3214       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3215       t)))
3216
3217 (defun gnus-set-global-variables ()
3218   "Set the global equivalents of the buffer-local variables.
3219 They are set to the latest values they had.  These reflect the summary
3220 buffer that was in action when the last article was fetched."
3221   (when (eq major-mode 'gnus-summary-mode)
3222     (setq gnus-summary-buffer (current-buffer))
3223     (let ((name gnus-newsgroup-name)
3224           (marked gnus-newsgroup-marked)
3225           (spam gnus-newsgroup-spam-marked)
3226           (unread gnus-newsgroup-unreads)
3227           (headers gnus-current-headers)
3228           (data gnus-newsgroup-data)
3229           (summary gnus-summary-buffer)
3230           (article-buffer gnus-article-buffer)
3231           (original gnus-original-article-buffer)
3232           (gac gnus-article-current)
3233           (reffed gnus-reffed-article-number)
3234           (score-file gnus-current-score-file)
3235           (default-charset gnus-newsgroup-charset)
3236           vlist)
3237       (let ((locals gnus-newsgroup-variables))
3238         (while locals
3239           (if (consp (car locals))
3240               (push (eval (caar locals)) vlist)
3241             (push (eval (car locals)) vlist))
3242           (setq locals (cdr locals)))
3243         (setq vlist (nreverse vlist)))
3244       (with-current-buffer gnus-group-buffer
3245         (setq gnus-newsgroup-name name
3246               gnus-newsgroup-marked marked
3247               gnus-newsgroup-spam-marked spam
3248               gnus-newsgroup-unreads unread
3249               gnus-current-headers headers
3250               gnus-newsgroup-data data
3251               gnus-article-current gac
3252               gnus-summary-buffer summary
3253               gnus-article-buffer article-buffer
3254               gnus-original-article-buffer original
3255               gnus-reffed-article-number reffed
3256               gnus-current-score-file score-file
3257               gnus-newsgroup-charset default-charset)
3258         (let ((locals gnus-newsgroup-variables))
3259           (while locals
3260             (if (consp (car locals))
3261                 (set (caar locals) (pop vlist))
3262               (set (car locals) (pop vlist)))
3263             (setq locals (cdr locals))))
3264         ;; The article buffer also has local variables.
3265         (when (gnus-buffer-live-p gnus-article-buffer)
3266           (set-buffer gnus-article-buffer)
3267           (setq gnus-summary-buffer summary))))))
3268
3269 (defun gnus-summary-article-unread-p (article)
3270   "Say whether ARTICLE is unread or not."
3271   (memq article gnus-newsgroup-unreads))
3272
3273 (defun gnus-summary-first-article-p (&optional article)
3274   "Return whether ARTICLE is the first article in the buffer."
3275   (if (not (setq article (or article (gnus-summary-article-number))))
3276       nil
3277     (eq article (caar gnus-newsgroup-data))))
3278
3279 (defun gnus-summary-last-article-p (&optional article)
3280   "Return whether ARTICLE is the last article in the buffer."
3281   (if (not (setq article (or article (gnus-summary-article-number))))
3282       ;; All non-existent numbers are the last article.  :-)
3283       t
3284     (not (cdr (gnus-data-find-list article)))))
3285
3286 (defun gnus-make-thread-indent-array ()
3287   (let ((n 200))
3288     (unless (and gnus-thread-indent-array
3289                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3290       (setq gnus-thread-indent-array (make-vector 201 "")
3291             gnus-thread-indent-array-level gnus-thread-indent-level)
3292       (while (>= n 0)
3293         (aset gnus-thread-indent-array n
3294               (make-string (* n gnus-thread-indent-level) ? ))
3295         (setq n (1- n))))))
3296
3297 (defun gnus-update-summary-mark-positions ()
3298   "Compute where the summary marks are to go."
3299   (save-excursion
3300     (when (gnus-buffer-exists-p gnus-summary-buffer)
3301       (set-buffer gnus-summary-buffer))
3302     (let ((spec gnus-summary-line-format-spec)
3303           pos)
3304       (save-excursion
3305         (gnus-set-work-buffer)
3306         (let ((gnus-tmp-unread ?Z)
3307               (gnus-replied-mark ?Z)
3308               (gnus-score-below-mark ?Z)
3309               (gnus-score-over-mark ?Z)
3310               (gnus-undownloaded-mark ?Z)
3311               (gnus-summary-line-format-spec spec)
3312               (gnus-newsgroup-downloadable '(0))
3313               (header
3314                (make-full-mail-header
3315                 0 "" "nobody" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil))
3316               case-fold-search ignores)
3317           ;; Here, all marks are bound to Z.
3318           (gnus-summary-insert-line header
3319                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3320           (goto-char (point-min))
3321           ;; Memorize the positions of the same characters as dummy marks.
3322           (while (re-search-forward "[A-D]" nil t)
3323             (push (point) ignores))
3324           (erase-buffer)
3325           ;; We use A-D as dummy marks in order to know column positions
3326           ;; where marks should be inserted.
3327           (setq gnus-tmp-unread ?A
3328                 gnus-replied-mark ?B
3329                 gnus-score-below-mark ?C
3330                 gnus-score-over-mark ?C
3331                 gnus-undownloaded-mark ?D)
3332           (gnus-summary-insert-line header
3333                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3334           ;; Ignore characters which aren't dummy marks.
3335           (dolist (p ignores)
3336             (delete-region (goto-char (1- p)) p)
3337             (insert ?Z))
3338           (goto-char (point-min))
3339           (setq pos (list (cons 'unread
3340                                 (and (search-forward "A" nil t)
3341                                      (- (point) (point-min) 1)))))
3342           (goto-char (point-min))
3343           (push (cons 'replied (and (search-forward "B" nil t)
3344                                     (- (point) (point-min) 1)))
3345                 pos)
3346           (goto-char (point-min))
3347           (push (cons 'score (and (search-forward "C" nil t)
3348                                   (- (point) (point-min) 1)))
3349                 pos)
3350           (goto-char (point-min))
3351           (push (cons 'download (and (search-forward "D" nil t)
3352                                      (- (point) (point-min) 1)))
3353                 pos)))
3354       (setq gnus-summary-mark-positions pos))))
3355
3356 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3357   "Insert a dummy root in the summary buffer."
3358   (beginning-of-line)
3359   (gnus-add-text-properties
3360    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3361    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3362
3363 (defun gnus-summary-extract-address-component (from)
3364   (or (car (funcall gnus-extract-address-components from))
3365       from))
3366
3367 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3368   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3369                                 default-mime-charset)))
3370     ;; Is it really necessary to do this next part for each summary line?
3371     ;; Luckily, doesn't seem to slow things down much.
3372     (or
3373      (and gnus-ignored-from-addresses
3374           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3375           (let ((extra-headers (mail-header-extra header))
3376                 to
3377                 newsgroups)
3378             (cond
3379              ((setq to (cdr (assq 'To extra-headers)))
3380               (concat gnus-summary-to-prefix
3381                       (inline
3382                         (gnus-summary-extract-address-component
3383                          (funcall gnus-decode-encoded-word-function to)))))
3384              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3385               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3386      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3387
3388 (defun gnus-summary-insert-line (gnus-tmp-header
3389                                  gnus-tmp-level gnus-tmp-current
3390                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3391                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3392                                  &optional gnus-tmp-dummy gnus-tmp-score
3393                                  gnus-tmp-process)
3394   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3395          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3396          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3397          (gnus-tmp-score-char
3398           (if (or (null gnus-summary-default-score)
3399                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3400                       gnus-summary-zcore-fuzz))
3401               ?\ ;;;Whitespace
3402             (if (< gnus-tmp-score gnus-summary-default-score)
3403                 gnus-score-below-mark gnus-score-over-mark)))
3404          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3405          (gnus-tmp-replied
3406           (cond (gnus-tmp-process gnus-process-mark)
3407                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3408                  gnus-cached-mark)
3409                 (gnus-tmp-replied gnus-replied-mark)
3410                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3411                  gnus-forwarded-mark)
3412                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3413                  gnus-saved-mark)
3414                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3415                  gnus-recent-mark)
3416                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3417                  gnus-unseen-mark)
3418                 (t gnus-no-mark)))
3419          (gnus-tmp-downloaded
3420           (cond (undownloaded
3421                  gnus-undownloaded-mark)
3422                 (gnus-newsgroup-agentized
3423                  gnus-downloaded-mark)
3424                 (t
3425                  gnus-no-mark)))
3426          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3427          (gnus-tmp-name
3428           (cond
3429            ((string-match "<[^>]+> *$" gnus-tmp-from)
3430             (let ((beg (match-beginning 0)))
3431               (or (and (string-match "^\".+\"" gnus-tmp-from)
3432                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3433                   (substring gnus-tmp-from 0 beg))))
3434            ((string-match "(.+)" gnus-tmp-from)
3435             (substring gnus-tmp-from
3436                        (1+ (match-beginning 0)) (1- (match-end 0))))
3437            (t gnus-tmp-from)))
3438          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3439          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3440          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3441          (buffer-read-only nil))
3442     (when (string= gnus-tmp-name "")
3443       (setq gnus-tmp-name gnus-tmp-from))
3444     (unless (numberp gnus-tmp-lines)
3445       (setq gnus-tmp-lines -1))
3446     (if (= gnus-tmp-lines -1)
3447         (setq gnus-tmp-lines "?")
3448       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3449     (gnus-put-text-property-excluding-characters-with-faces
3450      (point)
3451      (progn (eval gnus-summary-line-format-spec) (point))
3452      'gnus-number gnus-tmp-number)
3453     (when (gnus-visual-p 'summary-highlight 'highlight)
3454       (forward-line -1)
3455       (gnus-run-hooks 'gnus-summary-update-hook)
3456       (forward-line 1))))
3457
3458 (defun gnus-summary-update-line (&optional dont-update)
3459   "Update summary line after change."
3460   (when (and gnus-summary-default-score
3461              (not gnus-summary-inhibit-highlight))
3462     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3463            (article (gnus-summary-article-number))
3464            (score (gnus-summary-article-score article)))
3465       (unless dont-update
3466         (if (and gnus-summary-mark-below
3467                  (< (gnus-summary-article-score)
3468                     gnus-summary-mark-below))
3469             ;; This article has a low score, so we mark it as read.
3470             (when (memq article gnus-newsgroup-unreads)
3471               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3472           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3473             ;; This article was previously marked as read on account
3474             ;; of a low score, but now it has risen, so we mark it as
3475             ;; unread.
3476             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3477         (gnus-summary-update-mark
3478          (if (or (null gnus-summary-default-score)
3479                  (<= (abs (- score gnus-summary-default-score))
3480                      gnus-summary-zcore-fuzz))
3481              ?\ ;;;Whitespace
3482            (if (< score gnus-summary-default-score)
3483                gnus-score-below-mark gnus-score-over-mark))
3484          'score))
3485       ;; Do visual highlighting.
3486       (when (gnus-visual-p 'summary-highlight 'highlight)
3487         (gnus-run-hooks 'gnus-summary-update-hook)))))
3488
3489 (defvar gnus-tmp-new-adopts nil)
3490
3491 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3492   "Return the number of articles in THREAD.
3493 This may be 0 in some cases -- if none of the articles in
3494 the thread are to be displayed."
3495   (let* ((number
3496           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3497           (cond
3498            ((not (listp thread))
3499             1)
3500            ((and (consp thread) (cdr thread))
3501             (apply
3502              '+ 1 (mapcar
3503                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3504            ((null thread)
3505             1)
3506            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3507             1)
3508            (t 0))))
3509     (when (and level (zerop level) gnus-tmp-new-adopts)
3510       (incf number
3511             (apply '+ (mapcar
3512                        'gnus-summary-number-of-articles-in-thread
3513                        gnus-tmp-new-adopts))))
3514     (if char
3515         (if (> number 1) gnus-not-empty-thread-mark
3516           gnus-empty-thread-mark)
3517       number)))
3518
3519 (defsubst gnus-summary-line-message-size (head)
3520   "Return pretty-printed version of message size.
3521 This function is intended to be used in
3522 `gnus-summary-line-format-alist'."
3523   (let ((c (or (mail-header-chars head) -1)))
3524     (cond ((< c 0) "n/a")               ; chars not available
3525           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3526           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3527           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3528           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3529
3530
3531 (defun gnus-summary-set-local-parameters (group)
3532   "Go through the local params of GROUP and set all variable specs in that list."
3533   (let ((vars '(quit-config)))          ; Ignore quit-config.
3534     (dolist (elem (gnus-group-find-parameter group))
3535       (and (consp elem)                 ; Has to be a cons.
3536            (consp (cdr elem))           ; The cdr has to be a list.
3537            (symbolp (car elem))         ; Has to be a symbol in there.
3538            (not (memq (car elem) vars))
3539            (ignore-errors               ; So we set it.
3540              (push (car elem) vars)
3541              (make-local-variable (car elem))
3542              (set (car elem) (eval (nth 1 elem))))))))
3543
3544 (defun gnus-summary-read-group (group &optional show-all no-article
3545                                       kill-buffer no-display backward
3546                                       select-articles)
3547   "Start reading news in newsgroup GROUP.
3548 If SHOW-ALL is non-nil, already read articles are also listed.
3549 If NO-ARTICLE is non-nil, no article is selected initially.
3550 If NO-DISPLAY, don't generate a summary buffer."
3551   (let (result)
3552     (while (and group
3553                 (null (setq result
3554                             (let ((gnus-auto-select-next nil))
3555                               (or (gnus-summary-read-group-1
3556                                    group show-all no-article
3557                                    kill-buffer no-display
3558                                    select-articles)
3559                                   (setq show-all nil
3560                                         select-articles nil)))))
3561                 (eq gnus-auto-select-next 'quietly))
3562       (set-buffer gnus-group-buffer)
3563       ;; The entry function called above goes to the next
3564       ;; group automatically, so we go two groups back
3565       ;; if we are searching for the previous group.
3566       (when backward
3567         (gnus-group-prev-unread-group 2))
3568       (if (not (equal group (gnus-group-group-name)))
3569           (setq group (gnus-group-group-name))
3570         (setq group nil)))
3571     result))
3572
3573 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3574   "Directly jump to the other GROUP from summary buffer.
3575 If SHOW-ALL is non-nil, already read articles are also listed."
3576   (interactive
3577    (if (eq gnus-summary-buffer (current-buffer))
3578        (list (completing-read
3579               "Group: " gnus-active-hashtb nil t
3580               (when (and gnus-newsgroup-name
3581                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3582                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3583               'gnus-group-history)
3584              current-prefix-arg)
3585      (error "%s must be invoked from a gnus summary buffer." this-command)))
3586   (unless (or (zerop (length group))
3587               (and gnus-newsgroup-name
3588                    (string-equal gnus-newsgroup-name group)))
3589     (gnus-summary-exit)
3590     (gnus-summary-read-group group show-all
3591                              gnus-dont-select-after-jump-to-other-group)))
3592
3593 (defun gnus-summary-read-group-1 (group show-all no-article
3594                                         kill-buffer no-display
3595                                         &optional select-articles)
3596   ;; Killed foreign groups can't be entered.
3597   ;;  (when (and (not (gnus-group-native-p group))
3598   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3599   ;;    (error "Dead non-native groups can't be entered"))
3600   (gnus-message 5 "Retrieving newsgroup: %s..."
3601                 (gnus-group-decoded-name group))
3602   (let* ((new-group (gnus-summary-setup-buffer group))
3603          (quit-config (gnus-group-quit-config group))
3604          (did-select (and new-group (gnus-select-newsgroup
3605                                      group show-all select-articles))))
3606     (cond
3607      ;; This summary buffer exists already, so we just select it.
3608      ((not new-group)
3609       (gnus-set-global-variables)
3610       (when kill-buffer
3611         (gnus-kill-or-deaden-summary kill-buffer))
3612       (gnus-configure-windows 'summary 'force)
3613       (gnus-set-mode-line 'summary)
3614       (gnus-summary-position-point)
3615       (message "")
3616       t)
3617      ;; We couldn't select this group.
3618      ((null did-select)
3619       (when (and (eq major-mode 'gnus-summary-mode)
3620                  (not (equal (current-buffer) kill-buffer)))
3621         (kill-buffer (current-buffer))
3622         (if (not quit-config)
3623             (progn
3624               ;; Update the info -- marks might need to be removed,
3625               ;; for instance.
3626               (gnus-summary-update-info)
3627               (set-buffer gnus-group-buffer)
3628               (gnus-group-jump-to-group group)
3629               (gnus-group-next-unread-group 1))
3630           (gnus-handle-ephemeral-exit quit-config)))
3631       (let ((grpinfo (gnus-get-info group)))
3632         (if (null (gnus-info-read grpinfo))
3633             (gnus-message 3 "Group %s contains no messages"
3634                           (gnus-group-decoded-name group))
3635           (gnus-message 3 "Can't select group")))
3636       nil)
3637      ;; The user did a `C-g' while prompting for number of articles,
3638      ;; so we exit this group.
3639      ((eq did-select 'quit)
3640       (and (eq major-mode 'gnus-summary-mode)
3641            (not (equal (current-buffer) kill-buffer))
3642            (kill-buffer (current-buffer)))
3643       (when kill-buffer
3644         (gnus-kill-or-deaden-summary kill-buffer))
3645       (if (not quit-config)
3646           (progn
3647             (set-buffer gnus-group-buffer)
3648             (gnus-group-jump-to-group group)
3649             (gnus-group-next-unread-group 1)
3650             (gnus-configure-windows 'group 'force))
3651         (gnus-handle-ephemeral-exit quit-config))
3652       ;; Finally signal the quit.
3653       (signal 'quit nil))
3654      ;; The group was successfully selected.
3655      (t
3656       (gnus-set-global-variables)
3657       ;; Save the active value in effect when the group was entered.
3658       (setq gnus-newsgroup-active
3659             (gnus-copy-sequence
3660              (gnus-active gnus-newsgroup-name)))
3661       ;; You can change the summary buffer in some way with this hook.
3662       (gnus-run-hooks 'gnus-select-group-hook)
3663       (when (memq 'summary (gnus-update-format-specifications
3664                             nil 'summary 'summary-mode 'summary-dummy))
3665         ;; The format specification for the summary line was updated,
3666         ;; so we need to update the mark positions as well.
3667         (gnus-update-summary-mark-positions))
3668       ;; Do score processing.
3669       (when gnus-use-scoring
3670         (gnus-possibly-score-headers))
3671       ;; Check whether to fill in the gaps in the threads.
3672       (when gnus-build-sparse-threads
3673         (gnus-build-sparse-threads))
3674       ;; Find the initial limit.
3675       (if show-all
3676           (let ((gnus-newsgroup-dormant nil))
3677             (gnus-summary-initial-limit show-all))
3678         (gnus-summary-initial-limit show-all))
3679       ;; Generate the summary buffer.
3680       (unless no-display
3681         (gnus-summary-prepare))
3682       (when gnus-use-trees
3683         (gnus-tree-open group)
3684         (setq gnus-summary-highlight-line-function
3685               'gnus-tree-highlight-article))
3686       ;; If the summary buffer is empty, but there are some low-scored
3687       ;; articles or some excluded dormants, we include these in the
3688       ;; buffer.
3689       (when (and (zerop (buffer-size))
3690                  (not no-display))
3691         (cond (gnus-newsgroup-dormant
3692                (gnus-summary-limit-include-dormant))
3693               ((and gnus-newsgroup-scored show-all)
3694                (gnus-summary-limit-include-expunged t))))
3695       ;; Function `gnus-apply-kill-file' must be called in this hook.
3696       (gnus-run-hooks 'gnus-apply-kill-hook)
3697       (if (and (zerop (buffer-size))
3698                (not no-display))
3699           (progn
3700             ;; This newsgroup is empty.
3701             (gnus-summary-catchup-and-exit nil t)
3702             (gnus-message 6 "No unread news")
3703             (when kill-buffer
3704               (gnus-kill-or-deaden-summary kill-buffer))
3705             ;; Return nil from this function.
3706             nil)
3707         ;; Hide conversation thread subtrees.  We cannot do this in
3708         ;; gnus-summary-prepare-hook since kill processing may not
3709         ;; work with hidden articles.
3710         (gnus-summary-maybe-hide-threads)
3711         (when kill-buffer
3712           (gnus-kill-or-deaden-summary kill-buffer))
3713         (gnus-summary-auto-select-subject)
3714         ;; Show first unread article if requested.
3715         (if (and (not no-article)
3716                  (not no-display)
3717                  gnus-newsgroup-unreads
3718                  gnus-auto-select-first)
3719             (progn
3720               (gnus-configure-windows 'summary)
3721               (let ((art (gnus-summary-article-number)))
3722                 (unless (and (not gnus-plugged)
3723                              (or (memq art gnus-newsgroup-undownloaded)
3724                                  (memq art gnus-newsgroup-downloadable)))
3725                   (gnus-summary-goto-article art))))
3726           ;; Don't select any articles.
3727           (gnus-summary-position-point)
3728           (gnus-configure-windows 'summary 'force)
3729           (gnus-set-mode-line 'summary))
3730         (when (and gnus-auto-center-group
3731                    (get-buffer-window gnus-group-buffer t))
3732           ;; Gotta use windows, because recenter does weird stuff if
3733           ;; the current buffer ain't the displayed window.
3734           (let ((owin (selected-window)))
3735             (select-window (get-buffer-window gnus-group-buffer t))
3736             (when (gnus-group-goto-group group)
3737               (recenter))
3738             (select-window owin)))
3739         ;; Mark this buffer as "prepared".
3740         (setq gnus-newsgroup-prepared t)
3741         (gnus-run-hooks 'gnus-summary-prepared-hook)
3742         (unless (gnus-ephemeral-group-p group)
3743           (gnus-group-update-group group))
3744         t)))))
3745
3746 (defun gnus-summary-auto-select-subject ()
3747   "Select the subject line on initial group entry."
3748   (goto-char (point-min))
3749   (cond
3750    ((eq gnus-auto-select-subject 'best)
3751     (gnus-summary-best-unread-subject))
3752    ((eq gnus-auto-select-subject 'unread)
3753     (gnus-summary-first-unread-subject))
3754    ((eq gnus-auto-select-subject 'unseen)
3755     (gnus-summary-first-unseen-subject))
3756    ((eq gnus-auto-select-subject 'unseen-or-unread)
3757     (gnus-summary-first-unseen-or-unread-subject))
3758    ((eq gnus-auto-select-subject 'first)
3759     ;; Do nothing.
3760     )
3761    ((functionp gnus-auto-select-subject)
3762     (funcall gnus-auto-select-subject))))
3763
3764 (defun gnus-summary-prepare ()
3765   "Generate the summary buffer."
3766   (interactive)
3767   (let ((buffer-read-only nil))
3768     (erase-buffer)
3769     (setq gnus-newsgroup-data nil
3770           gnus-newsgroup-data-reverse nil)
3771     (gnus-run-hooks 'gnus-summary-generate-hook)
3772     ;; Generate the buffer, either with threads or without.
3773     (when gnus-newsgroup-headers
3774       (gnus-summary-prepare-threads
3775        (if gnus-show-threads
3776            (gnus-sort-gathered-threads
3777             (funcall gnus-summary-thread-gathering-function
3778                      (gnus-sort-threads
3779                       (gnus-cut-threads (gnus-make-threads)))))
3780          ;; Unthreaded display.
3781          (gnus-sort-articles gnus-newsgroup-headers))))
3782     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3783     ;; Call hooks for modifying summary buffer.
3784     (goto-char (point-min))
3785     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3786
3787 (defsubst gnus-general-simplify-subject (subject)
3788   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3789   (setq subject
3790         (cond
3791          ;; Truncate the subject.
3792          (gnus-simplify-subject-functions
3793           (gnus-map-function gnus-simplify-subject-functions subject))
3794          ((numberp gnus-summary-gather-subject-limit)
3795           (setq subject (gnus-simplify-subject-re subject))
3796           (if (> (length subject) gnus-summary-gather-subject-limit)
3797               (substring subject 0 gnus-summary-gather-subject-limit)
3798             subject))
3799          ;; Fuzzily simplify it.
3800          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3801           (gnus-simplify-subject-fuzzy subject))
3802          ;; Just remove the leading "Re:".
3803          (t
3804           (gnus-simplify-subject-re subject))))
3805
3806   (if (and gnus-summary-gather-exclude-subject
3807            (string-match gnus-summary-gather-exclude-subject subject))
3808       nil                               ; This article shouldn't be gathered
3809     subject))
3810
3811 (defun gnus-summary-simplify-subject-query ()
3812   "Query where the respool algorithm would put this article."
3813   (interactive)
3814   (gnus-summary-select-article)
3815   (message "%s"
3816            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3817
3818 (defun gnus-gather-threads-by-subject (threads)
3819   "Gather threads by looking at Subject headers."
3820   (if (not gnus-summary-make-false-root)
3821       threads
3822     (let ((hashtb (gnus-make-hashtable 1024))
3823           (prev threads)
3824           (result threads)
3825           subject hthread whole-subject)
3826       (while threads
3827         (setq subject (gnus-general-simplify-subject
3828                        (setq whole-subject (mail-header-subject
3829                                             (caar threads)))))
3830         (when subject
3831           (if (setq hthread (gnus-gethash subject hashtb))
3832               (progn
3833                 ;; We enter a dummy root into the thread, if we
3834                 ;; haven't done that already.
3835                 (unless (stringp (caar hthread))
3836                   (setcar hthread (list whole-subject (car hthread))))
3837                 ;; We add this new gathered thread to this gathered
3838                 ;; thread.
3839                 (setcdr (car hthread)
3840                         (nconc (cdar hthread) (list (car threads))))
3841                 ;; Remove it from the list of threads.
3842                 (setcdr prev (cdr threads))
3843                 (setq threads prev))
3844             ;; Enter this thread into the hash table.
3845             (gnus-sethash subject
3846                           (if gnus-summary-make-false-root-always
3847                               (progn
3848                                 ;; If you want a dummy root above all
3849                                 ;; threads...
3850                                 (setcar threads (list whole-subject
3851                                                       (car threads)))
3852                                 threads)
3853                             threads)
3854                           hashtb)))
3855         (setq prev threads)
3856         (setq threads (cdr threads)))
3857       result)))
3858
3859 (defun gnus-gather-threads-by-references (threads)
3860   "Gather threads by looking at References headers."
3861   (let ((idhashtb (gnus-make-hashtable 1024))
3862         (thhashtb (gnus-make-hashtable 1024))
3863         (prev threads)
3864         (result threads)
3865         ids references id gthread gid entered ref)
3866     (while threads
3867       (when (setq references (mail-header-references (caar threads)))
3868         (setq id (mail-header-id (caar threads))
3869               ids (inline (gnus-split-references references))
3870               entered nil)
3871         (while (setq ref (pop ids))
3872           (setq ids (delete ref ids))
3873           (if (not (setq gid (gnus-gethash ref idhashtb)))
3874               (progn
3875                 (gnus-sethash ref id idhashtb)
3876                 (gnus-sethash id threads thhashtb))
3877             (setq gthread (gnus-gethash gid thhashtb))
3878             (unless entered
3879               ;; We enter a dummy root into the thread, if we
3880               ;; haven't done that already.
3881               (unless (stringp (caar gthread))
3882                 (setcar gthread (list (mail-header-subject (caar gthread))
3883                                       (car gthread))))
3884               ;; We add this new gathered thread to this gathered
3885               ;; thread.
3886               (setcdr (car gthread)
3887                       (nconc (cdar gthread) (list (car threads)))))
3888             ;; Add it into the thread hash table.
3889             (gnus-sethash id gthread thhashtb)
3890             (setq entered t)
3891             ;; Remove it from the list of threads.
3892             (setcdr prev (cdr threads))
3893             (setq threads prev))))
3894       (setq prev threads)
3895       (setq threads (cdr threads)))
3896     result))
3897
3898 (defun gnus-sort-gathered-threads (threads)
3899   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3900   (let ((result threads))
3901     (while threads
3902       (when (stringp (caar threads))
3903         (setcdr (car threads)
3904                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3905       (setq threads (cdr threads)))
3906     result))
3907
3908 (defun gnus-thread-loop-p (root thread)
3909   "Say whether ROOT is in THREAD."
3910   (let ((stack (list thread))
3911         (infloop 0)
3912         th)
3913     (while (setq thread (pop stack))
3914       (setq th (cdr thread))
3915       (while (and th
3916                   (not (eq (caar th) root)))
3917         (pop th))
3918       (if th
3919           ;; We have found a loop.
3920           (let (ref-dep)
3921             (setcdr thread (delq (car th) (cdr thread)))
3922             (if (boundp (setq ref-dep (intern "none"
3923                                               gnus-newsgroup-dependencies)))
3924                 (setcdr (symbol-value ref-dep)
3925                         (nconc (cdr (symbol-value ref-dep))
3926                                (list (car th))))
3927               (set ref-dep (list nil (car th))))
3928             (setq infloop 1
3929                   stack nil))
3930         ;; Push all the subthreads onto the stack.
3931         (push (cdr thread) stack)))
3932     infloop))
3933
3934 (defun gnus-make-threads ()
3935   "Go through the dependency hashtb and find the roots.  Return all threads."
3936   (let (threads)
3937     (while (catch 'infloop
3938              (mapatoms
3939               (lambda (refs)
3940                 ;; Deal with self-referencing References loops.
3941                 (when (and (car (symbol-value refs))
3942                            (not (zerop
3943                                  (apply
3944                                   '+
3945                                   (mapcar
3946                                    (lambda (thread)
3947                                      (gnus-thread-loop-p
3948                                       (car (symbol-value refs)) thread))
3949                                    (cdr (symbol-value refs)))))))
3950                   (setq threads nil)
3951                   (throw 'infloop t))
3952                 (unless (car (symbol-value refs))
3953                   ;; These threads do not refer back to any other articles,
3954                   ;; so they're roots.
3955                   (setq threads (append (cdr (symbol-value refs)) threads))))
3956               gnus-newsgroup-dependencies)))
3957     threads))
3958
3959 ;; Build the thread tree.
3960 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3961   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3962
3963 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3964 if it was already present.
3965
3966 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3967 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3968 Message-IDs will be renamed to a unique Message-ID before being
3969 entered.
3970
3971 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3972   (let* ((id (mail-header-id header))
3973          (id-dep (and id (intern id dependencies)))
3974          parent-id ref ref-dep ref-header replaced)
3975     ;; Enter this `header' in the `dependencies' table.
3976     (cond
3977      ((not id-dep)
3978       (setq header nil))
3979      ;; The first two cases do the normal part: enter a new `header'
3980      ;; in the `dependencies' table.
3981      ((not (boundp id-dep))
3982       (set id-dep (list header)))
3983      ((null (car (symbol-value id-dep)))
3984       (setcar (symbol-value id-dep) header))
3985
3986      ;; From here the `header' was already present in the
3987      ;; `dependencies' table.
3988      (force-new
3989       ;; Overrides an existing entry;
3990       ;; just set the header part of the entry.
3991       (setcar (symbol-value id-dep) header)
3992       (setq replaced t))
3993
3994      ;; Renames the existing `header' to a unique Message-ID.
3995      ((not gnus-summary-ignore-duplicates)
3996       ;; An article with this Message-ID has already been seen.
3997       ;; We rename the Message-ID.
3998       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3999            (list header))
4000       (mail-header-set-id header id))
4001
4002      ;; The last case ignores an existing entry, except it adds any
4003      ;; additional Xrefs (in case the two articles came from different
4004      ;; servers.
4005      ;; Also sets `header' to `nil' meaning that the `dependencies'
4006      ;; table was *not* modified.
4007      (t
4008       (mail-header-set-xref
4009        (car (symbol-value id-dep))
4010        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4011                    "")
4012                (or (mail-header-xref header) "")))
4013       (setq header nil)))
4014
4015     (when (and header (not replaced))
4016       ;; First check that we are not creating a References loop.
4017       (setq parent-id (gnus-parent-id (mail-header-references header)))
4018       (setq ref parent-id)
4019       (while (and ref
4020                   (setq ref-dep (intern-soft ref dependencies))
4021                   (boundp ref-dep)
4022                   (setq ref-header (car (symbol-value ref-dep))))
4023         (if (string= id ref)
4024             ;; Yuk!  This is a reference loop.  Make the article be a
4025             ;; root article.
4026             (progn
4027               (mail-header-set-references (car (symbol-value id-dep)) "none")
4028               (setq ref nil)
4029               (setq parent-id nil))
4030           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4031       (setq ref-dep (intern (or parent-id "none") dependencies))
4032       (if (boundp ref-dep)
4033           (setcdr (symbol-value ref-dep)
4034                   (nconc (cdr (symbol-value ref-dep))
4035                          (list (symbol-value id-dep))))
4036         (set ref-dep (list nil (symbol-value id-dep)))))
4037     header))
4038
4039 (defun gnus-extract-message-id-from-in-reply-to (string)
4040   (if (string-match "<[^>]+>" string)
4041       (substring string (match-beginning 0) (match-end 0))
4042     nil))
4043
4044 (defun gnus-build-sparse-threads ()
4045   (let ((headers gnus-newsgroup-headers)
4046         (mail-parse-charset gnus-newsgroup-charset)
4047         (gnus-summary-ignore-duplicates t)
4048         header references generation relations
4049         subject child end new-child date)
4050     ;; First we create an alist of generations/relations, where
4051     ;; generations is how much we trust the relation, and the relation
4052     ;; is parent/child.
4053     (gnus-message 7 "Making sparse threads...")
4054     (save-excursion
4055       (nnheader-set-temp-buffer " *gnus sparse threads*")
4056       (while (setq header (pop headers))
4057         (when (and (setq references (mail-header-references header))
4058                    (not (string= references "")))
4059           (insert references)
4060           (setq child (mail-header-id header)
4061                 subject (mail-header-subject header)
4062                 date (mail-header-date header)
4063                 generation 0)
4064           (while (search-backward ">" nil t)
4065             (setq end (1+ (point)))
4066             (when (search-backward "<" nil t)
4067               (setq new-child (buffer-substring (point) end))
4068               (push (list (incf generation)
4069                           child (setq child new-child)
4070                           subject date)
4071                     relations)))
4072           (when child
4073             (push (list (1+ generation) child nil subject) relations))
4074           (erase-buffer)))
4075       (kill-buffer (current-buffer)))
4076     ;; Sort over trustworthiness.
4077     (mapcar
4078      (lambda (relation)
4079        (when (gnus-dependencies-add-header
4080               (make-full-mail-header-from-decoded-header
4081                gnus-reffed-article-number
4082                (nth 3 relation) "" (or (nth 4 relation) "")
4083                (nth 1 relation)
4084                (or (nth 2 relation) "") 0 0 "")
4085               gnus-newsgroup-dependencies nil)
4086          (push gnus-reffed-article-number gnus-newsgroup-limit)
4087          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4088          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4089                gnus-newsgroup-reads)
4090          (decf gnus-reffed-article-number)))
4091      (sort relations 'car-less-than-car))
4092     (gnus-message 7 "Making sparse threads...done")))
4093
4094 (defun gnus-build-old-threads ()
4095   ;; Look at all the articles that refer back to old articles, and
4096   ;; fetch the headers for the articles that aren't there.  This will
4097   ;; build complete threads - if the roots haven't been expired by the
4098   ;; server, that is.
4099   (let ((mail-parse-charset gnus-newsgroup-charset)
4100         id heads)
4101     (mapatoms
4102      (lambda (refs)
4103        (when (not (car (symbol-value refs)))
4104          (setq heads (cdr (symbol-value refs)))
4105          (while heads
4106            (if (memq (mail-header-number (caar heads))
4107                      gnus-newsgroup-dormant)
4108                (setq heads (cdr heads))
4109              (setq id (symbol-name refs))
4110              (while (and (setq id (gnus-build-get-header id))
4111                          (not (car (gnus-id-to-thread id)))))
4112              (setq heads nil)))))
4113      gnus-newsgroup-dependencies)))
4114
4115 (defsubst gnus-remove-odd-characters (string)
4116   "Translate STRING into something that doesn't contain weird characters."
4117   (mm-subst-char-in-string
4118    ?\r ?\-
4119    (mm-subst-char-in-string ?\n ?\- string t) t))
4120
4121 ;; This function has to be called with point after the article number
4122 ;; on the beginning of the line.
4123 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4124   (let ((eol (point-at-eol))
4125         (buffer (current-buffer))
4126         header references in-reply-to)
4127
4128     ;; overview: [num subject from date id refs chars lines misc]
4129     (unwind-protect
4130         (progn
4131           (narrow-to-region (point) eol)
4132           (unless (eobp)
4133             (forward-char))
4134
4135           (setq header
4136                 (make-full-mail-header
4137                  number                         ; number
4138                  (nnheader-nov-field)           ; subject
4139                  (nnheader-nov-field)           ; from
4140                  (nnheader-nov-field)           ; date
4141                  (nnheader-nov-read-message-id number)  ; id
4142                  (setq references (nnheader-nov-field)) ; refs
4143                  (nnheader-nov-read-integer)    ; chars
4144                  (nnheader-nov-read-integer)    ; lines
4145                  (unless (eobp)
4146                    (if (looking-at "Xref: ")
4147                        (goto-char (match-end 0)))
4148                    (nnheader-nov-field))        ; Xref
4149                  (nnheader-nov-parse-extra))))  ; extra
4150
4151       (widen))
4152
4153     (when (and (string= references "")
4154                (setq in-reply-to (mail-header-extra header))
4155                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4156       (mail-header-set-references
4157        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4158
4159     (when gnus-alter-header-function
4160       (funcall gnus-alter-header-function header))
4161     (gnus-dependencies-add-header header dependencies force-new)))
4162
4163 (defun gnus-build-get-header (id)
4164   "Look through the buffer of NOV lines and find the header to ID.
4165 Enter this line into the dependencies hash table, and return
4166 the id of the parent article (if any)."
4167   (let ((deps gnus-newsgroup-dependencies)
4168         found header)
4169     (prog1
4170         (save-excursion
4171           (set-buffer nntp-server-buffer)
4172           (let ((case-fold-search nil))
4173             (goto-char (point-min))
4174             (while (and (not found)
4175                         (search-forward id nil t))
4176               (beginning-of-line)
4177               (setq found (looking-at
4178                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4179                                    (regexp-quote id))))
4180               (or found (beginning-of-line 2)))
4181             (when found
4182               (beginning-of-line)
4183               (and
4184                (setq header (gnus-nov-parse-line
4185                              (read (current-buffer)) deps))
4186                (gnus-parent-id (mail-header-references header))))))
4187       (when header
4188         (let ((number (mail-header-number header)))
4189           (push number gnus-newsgroup-limit)
4190           (push header gnus-newsgroup-headers)
4191           (if (memq number gnus-newsgroup-unselected)
4192               (progn
4193                 (setq gnus-newsgroup-unreads
4194                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4195                                                number))
4196                 (setq gnus-newsgroup-unselected
4197                       (delq number gnus-newsgroup-unselected)))
4198             (push number gnus-newsgroup-ancient)))))))
4199
4200 (defun gnus-build-all-threads ()
4201   "Read all the headers."
4202   (let ((gnus-summary-ignore-duplicates t)
4203         (mail-parse-charset gnus-newsgroup-charset)
4204         (dependencies gnus-newsgroup-dependencies)
4205         header article)
4206     (save-excursion
4207       (set-buffer nntp-server-buffer)
4208       (let ((case-fold-search nil))
4209         (goto-char (point-min))
4210         (while (not (eobp))
4211           (ignore-errors
4212             (setq article (read (current-buffer))
4213                   header (gnus-nov-parse-line article dependencies)))
4214           (when header
4215             (with-current-buffer gnus-summary-buffer
4216               (push header gnus-newsgroup-headers)
4217               (if (memq (setq article (mail-header-number header))
4218                         gnus-newsgroup-unselected)
4219                   (progn
4220                     (setq gnus-newsgroup-unreads
4221                           (gnus-add-to-sorted-list
4222                            gnus-newsgroup-unreads article))
4223                     (setq gnus-newsgroup-unselected
4224                           (delq article gnus-newsgroup-unselected)))
4225                 (push article gnus-newsgroup-ancient)))
4226             (forward-line 1)))))))
4227
4228 (defun gnus-summary-update-article-line (article header)
4229   "Update the line for ARTICLE using HEADER."
4230   (let* ((id (mail-header-id header))
4231          (thread (gnus-id-to-thread id)))
4232     (unless thread
4233       (error "Article in no thread"))
4234     ;; Update the thread.
4235     (setcar thread header)
4236     (gnus-summary-goto-subject article)
4237     (let* ((datal (gnus-data-find-list article))
4238            (data (car datal))
4239            (buffer-read-only nil)
4240            (level (gnus-summary-thread-level)))
4241       (gnus-delete-line)
4242       (let ((inserted (- (point)
4243                          (progn
4244                            (gnus-summary-insert-line
4245                             header level nil
4246                             (memq article gnus-newsgroup-undownloaded)
4247                             (gnus-article-mark article)
4248                             (memq article gnus-newsgroup-replied)
4249                             (memq article gnus-newsgroup-expirable)
4250                             ;; Only insert the Subject string when it's different
4251                             ;; from the previous Subject string.
4252                             (if (and
4253                                  gnus-show-threads
4254                                  (gnus-subject-equal
4255                                   (condition-case ()
4256                                       (mail-header-subject
4257                                        (gnus-data-header
4258                                         (cadr
4259                                          (gnus-data-find-list
4260                                           article
4261                                           (gnus-data-list t)))))
4262                                     ;; Error on the side of excessive subjects.
4263                                     (error ""))
4264                                   (mail-header-subject header)))
4265                                 ""
4266                               (mail-header-subject header))
4267                             nil (cdr (assq article gnus-newsgroup-scored))
4268                             (memq article gnus-newsgroup-processable))
4269                            (point)))))
4270         (when (cdr datal)
4271           (gnus-data-update-list
4272            (cdr datal)
4273            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4274
4275 (defun gnus-summary-update-article (article &optional iheader)
4276   "Update ARTICLE in the summary buffer."
4277   (set-buffer gnus-summary-buffer)
4278   (let* ((header (gnus-summary-article-header article))
4279          (id (mail-header-id header))
4280          (data (gnus-data-find article))
4281          (thread (gnus-id-to-thread id))
4282          (references (mail-header-references header))
4283          (parent
4284           (gnus-id-to-thread
4285            (or (gnus-parent-id
4286                 (when (and references
4287                            (not (equal "" references)))
4288                   references))
4289                "none")))
4290          (buffer-read-only nil)
4291          (old (car thread)))
4292     (when thread
4293       (unless iheader
4294         (setcar thread nil)
4295         (when parent
4296           (delq thread parent)))
4297       (if (gnus-summary-insert-subject id header)
4298           ;; Set the (possibly) new article number in the data structure.
4299           (gnus-data-set-number data (gnus-id-to-article id))
4300         (setcar thread old)
4301         nil))))
4302
4303 (defun gnus-rebuild-thread (id &optional line)
4304   "Rebuild the thread containing ID.
4305 If LINE, insert the rebuilt thread starting on line LINE."
4306   (let ((buffer-read-only nil)
4307         old-pos current thread data)
4308     (if (not gnus-show-threads)
4309         (setq thread (list (car (gnus-id-to-thread id))))
4310       ;; Get the thread this article is part of.
4311       (setq thread (gnus-remove-thread id)))
4312     (setq old-pos (point-at-bol))
4313     (setq current (save-excursion
4314                     (and (re-search-backward "[\r\n]" nil t)
4315                          (gnus-summary-article-number))))
4316     ;; If this is a gathered thread, we have to go some re-gathering.
4317     (when (stringp (car thread))
4318       (let ((subject (car thread))
4319             roots thr)
4320         (setq thread (cdr thread))
4321         (while thread
4322           (unless (memq (setq thr (gnus-id-to-thread
4323                                    (gnus-root-id
4324                                     (mail-header-id (caar thread)))))
4325                         roots)
4326             (push thr roots))
4327           (setq thread (cdr thread)))
4328         ;; We now have all (unique) roots.
4329         (if (= (length roots) 1)
4330             ;; All the loose roots are now one solid root.
4331             (setq thread (car roots))
4332           (setq thread (cons subject (gnus-sort-threads roots))))))
4333     (let (threads)
4334       ;; We then insert this thread into the summary buffer.
4335       (when line
4336         (goto-char (point-min))
4337         (forward-line (1- line)))
4338       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4339         (if gnus-show-threads
4340             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4341           (gnus-summary-prepare-unthreaded thread))
4342         (setq data (nreverse gnus-newsgroup-data))
4343         (setq threads gnus-newsgroup-threads))
4344       ;; We splice the new data into the data structure.
4345       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4346       ;;!!! then we want to insert at the beginning of the buffer.
4347       ;;!!! That happens to be true with Gnus now, but that may
4348       ;;!!! change in the future.  Perhaps.
4349       (gnus-data-enter-list
4350        (if line nil current) data (- (point) old-pos))
4351       (setq gnus-newsgroup-threads
4352             (nconc threads gnus-newsgroup-threads))
4353       (gnus-data-compute-positions))))
4354
4355 (defun gnus-number-to-header (number)
4356   "Return the header for article NUMBER."
4357   (let ((headers gnus-newsgroup-headers))
4358     (while (and headers
4359                 (not (= number (mail-header-number (car headers)))))
4360       (pop headers))
4361     (when headers
4362       (car headers))))
4363
4364 (defun gnus-parent-headers (in-headers &optional generation)
4365   "Return the headers of the GENERATIONeth parent of HEADERS."
4366   (unless generation
4367     (setq generation 1))
4368   (let ((parent t)
4369         (headers in-headers)
4370         references)
4371     (while (and parent
4372                 (not (zerop generation))
4373                 (setq references (mail-header-references headers)))
4374       (setq headers (if (and references
4375                              (setq parent (gnus-parent-id references)))
4376                         (car (gnus-id-to-thread parent))
4377                       nil))
4378       (decf generation))
4379     (and (not (eq headers in-headers))
4380          headers)))
4381
4382 (defun gnus-id-to-thread (id)
4383   "Return the (sub-)thread where ID appears."
4384   (gnus-gethash id gnus-newsgroup-dependencies))
4385
4386 (defun gnus-id-to-article (id)
4387   "Return the article number of ID."
4388   (let ((thread (gnus-id-to-thread id)))
4389     (when (and thread
4390                (car thread))
4391       (mail-header-number (car thread)))))
4392
4393 (defun gnus-id-to-header (id)
4394   "Return the article headers of ID."
4395   (car (gnus-id-to-thread id)))
4396
4397 (defun gnus-article-displayed-root-p (article)
4398   "Say whether ARTICLE is a root(ish) article."
4399   (let ((level (gnus-summary-thread-level article))
4400         (refs (mail-header-references  (gnus-summary-article-header article)))
4401         particle)
4402     (cond
4403      ((null level) nil)
4404      ((zerop level) t)
4405      ((null refs) t)
4406      ((null (gnus-parent-id refs)) t)
4407      ((and (= 1 level)
4408            (null (setq particle (gnus-id-to-article
4409                                  (gnus-parent-id refs))))
4410            (null (gnus-summary-thread-level particle)))))))
4411
4412 (defun gnus-root-id (id)
4413   "Return the id of the root of the thread where ID appears."
4414   (let (last-id prev)
4415     (while (and id (setq prev (car (gnus-id-to-thread id))))
4416       (setq last-id id
4417             id (gnus-parent-id (mail-header-references prev))))
4418     last-id))
4419
4420 (defun gnus-articles-in-thread (thread)
4421   "Return the list of articles in THREAD."
4422   (cons (mail-header-number (car thread))
4423         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4424
4425 (defun gnus-remove-thread (id &optional dont-remove)
4426   "Remove the thread that has ID in it."
4427   (let (headers thread last-id)
4428     ;; First go up in this thread until we find the root.
4429     (setq last-id (gnus-root-id id)
4430           headers (message-flatten-list (gnus-id-to-thread last-id)))
4431     ;; We have now found the real root of this thread.  It might have
4432     ;; been gathered into some loose thread, so we have to search
4433     ;; through the threads to find the thread we wanted.
4434     (let ((threads gnus-newsgroup-threads)
4435           sub)
4436       (while threads
4437         (setq sub (car threads))
4438         (if (stringp (car sub))
4439             ;; This is a gathered thread, so we look at the roots
4440             ;; below it to find whether this article is in this
4441             ;; gathered root.
4442             (progn
4443               (setq sub (cdr sub))
4444               (while sub
4445                 (when (member (caar sub) headers)
4446                   (setq thread (car threads)
4447                         threads nil
4448                         sub nil))
4449                 (setq sub (cdr sub))))
4450           ;; It's an ordinary thread, so we check it.
4451           (when (eq (car sub) (car headers))
4452             (setq thread sub
4453                   threads nil)))
4454         (setq threads (cdr threads)))
4455       ;; If this article is in no thread, then it's a root.
4456       (if thread
4457           (unless dont-remove
4458             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4459         (setq thread (gnus-id-to-thread last-id)))
4460       (when thread
4461         (prog1
4462             thread                      ; We return this thread.
4463           (unless dont-remove
4464             (if (stringp (car thread))
4465                 (progn
4466                   ;; If we use dummy roots, then we have to remove the
4467                   ;; dummy root as well.
4468                   (when (eq gnus-summary-make-false-root 'dummy)
4469                     ;; We go to the dummy root by going to
4470                     ;; the first sub-"thread", and then one line up.
4471                     (gnus-summary-goto-article
4472                      (mail-header-number (caadr thread)))
4473                     (forward-line -1)
4474                     (gnus-delete-line)
4475                     (gnus-data-compute-positions))
4476                   (setq thread (cdr thread))
4477                   (while thread
4478                     (gnus-remove-thread-1 (car thread))
4479                     (setq thread (cdr thread))))
4480               (gnus-remove-thread-1 thread))))))))
4481
4482 (defun gnus-remove-thread-1 (thread)
4483   "Remove the thread THREAD recursively."
4484   (let ((number (mail-header-number (pop thread)))
4485         d)
4486     (setq thread (reverse thread))
4487     (while thread
4488       (gnus-remove-thread-1 (pop thread)))
4489     (when (setq d (gnus-data-find number))
4490       (goto-char (gnus-data-pos d))
4491       (gnus-summary-show-thread)
4492       (gnus-data-remove
4493        number
4494        (- (point-at-bol)
4495           (prog1
4496               (1+ (point-at-eol))
4497             (gnus-delete-line)))))))
4498
4499 (defun gnus-sort-threads-1 (threads func)
4500   (sort (mapcar (lambda (thread)
4501                   (cons (car thread)
4502                         (and (cdr thread)
4503                              (gnus-sort-threads-1 (cdr thread) func))))
4504                 threads) func))
4505
4506 (defun gnus-sort-threads (threads)
4507   "Sort THREADS."
4508   (if (not gnus-thread-sort-functions)
4509       threads
4510     (gnus-message 8 "Sorting threads...")
4511     (let ((max-lisp-eval-depth 5000))
4512       (prog1 (gnus-sort-threads-1
4513          threads
4514          (gnus-make-sort-function gnus-thread-sort-functions))
4515         (gnus-message 8 "Sorting threads...done")))))
4516
4517 (defun gnus-sort-articles (articles)
4518   "Sort ARTICLES."
4519   (when gnus-article-sort-functions
4520     (gnus-message 7 "Sorting articles...")
4521     (prog1
4522         (setq gnus-newsgroup-headers
4523               (sort articles (gnus-make-sort-function
4524                               gnus-article-sort-functions)))
4525       (gnus-message 7 "Sorting articles...done"))))
4526
4527 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4528 (defmacro gnus-thread-header (thread)
4529   "Return header of first article in THREAD.
4530 Note that THREAD must never, ever be anything else than a variable -
4531 using some other form will lead to serious barfage."
4532   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4533   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4534   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4535         (vector thread) 2))
4536
4537 (defsubst gnus-article-sort-by-number (h1 h2)
4538   "Sort articles by article number."
4539   (< (mail-header-number h1)
4540      (mail-header-number h2)))
4541
4542 (defun gnus-thread-sort-by-number (h1 h2)
4543   "Sort threads by root article number."
4544   (gnus-article-sort-by-number
4545    (gnus-thread-header h1) (gnus-thread-header h2)))
4546
4547 (defsubst gnus-article-sort-by-random (h1 h2)
4548   "Sort articles by article number."
4549   (zerop (random 2)))
4550
4551 (defun gnus-thread-sort-by-random (h1 h2)
4552   "Sort threads by root article number."
4553   (gnus-article-sort-by-random
4554    (gnus-thread-header h1) (gnus-thread-header h2)))
4555
4556 (defsubst gnus-article-sort-by-lines (h1 h2)
4557   "Sort articles by article Lines header."
4558   (< (mail-header-lines h1)
4559      (mail-header-lines h2)))
4560
4561 (defun gnus-thread-sort-by-lines (h1 h2)
4562   "Sort threads by root article Lines header."
4563   (gnus-article-sort-by-lines
4564    (gnus-thread-header h1) (gnus-thread-header h2)))
4565
4566 (defsubst gnus-article-sort-by-chars (h1 h2)
4567   "Sort articles by octet length."
4568   (< (mail-header-chars h1)
4569      (mail-header-chars h2)))
4570
4571 (defun gnus-thread-sort-by-chars (h1 h2)
4572   "Sort threads by root article octet length."
4573   (gnus-article-sort-by-chars
4574    (gnus-thread-header h1) (gnus-thread-header h2)))
4575
4576 (defsubst gnus-article-sort-by-author (h1 h2)
4577   "Sort articles by root author."
4578   (string-lessp
4579    (let ((addr (car (mime-entity-read-field h1 'From))))
4580      (or (std11-full-name-string addr)
4581          (std11-address-string addr)
4582          ""))
4583    (let ((addr (car (mime-entity-read-field h2 'From))))
4584      (or (std11-full-name-string addr)
4585          (std11-address-string addr)
4586          ""))
4587    ))
4588
4589 (defun gnus-thread-sort-by-author (h1 h2)
4590   "Sort threads by root author."
4591   (gnus-article-sort-by-author
4592    (gnus-thread-header h1)  (gnus-thread-header h2)))
4593
4594 (defsubst gnus-article-sort-by-recipient (h1 h2)
4595   "Sort articles by recipient."
4596   (string-lessp
4597    (let ((extract (funcall
4598                    gnus-extract-address-components
4599                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4600      (or (car extract) (cadr extract)))
4601    (let ((extract (funcall
4602                    gnus-extract-address-components
4603                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4604      (or (car extract) (cadr extract)))))
4605
4606 (defsubst gnus-article-sort-by-subject (h1 h2)
4607   "Sort articles by root subject."
4608   (string-lessp
4609    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4610    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4611
4612 (defun gnus-thread-sort-by-subject (h1 h2)
4613   "Sort threads by root subject."
4614   (gnus-article-sort-by-subject
4615    (gnus-thread-header h1) (gnus-thread-header h2)))
4616
4617 (defsubst gnus-article-sort-by-date (h1 h2)
4618   "Sort articles by root article date."
4619   (time-less-p
4620    (gnus-date-get-time (mail-header-date h1))
4621    (gnus-date-get-time (mail-header-date h2))))
4622
4623 (defun gnus-thread-sort-by-date (h1 h2)
4624   "Sort threads by root article date."
4625   (gnus-article-sort-by-date
4626    (gnus-thread-header h1) (gnus-thread-header h2)))
4627
4628 (defsubst gnus-article-sort-by-score (h1 h2)
4629   "Sort articles by root article score.
4630 Unscored articles will be counted as having a score of zero."
4631   (> (or (cdr (assq (mail-header-number h1)
4632                     gnus-newsgroup-scored))
4633          gnus-summary-default-score 0)
4634      (or (cdr (assq (mail-header-number h2)
4635                     gnus-newsgroup-scored))
4636          gnus-summary-default-score 0)))
4637
4638 (defun gnus-thread-sort-by-score (h1 h2)
4639   "Sort threads by root article score."
4640   (gnus-article-sort-by-score
4641    (gnus-thread-header h1) (gnus-thread-header h2)))
4642
4643 (defun gnus-thread-sort-by-total-score (h1 h2)
4644   "Sort threads by the sum of all scores in the thread.
4645 Unscored articles will be counted as having a score of zero."
4646   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4647
4648 (defun gnus-thread-total-score (thread)
4649   ;; This function find the total score of THREAD.
4650   (cond
4651    ((null thread)
4652     0)
4653    ((consp thread)
4654     (if (stringp (car thread))
4655         (apply gnus-thread-score-function 0
4656                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4657       (gnus-thread-total-score-1 thread)))
4658    (t
4659     (gnus-thread-total-score-1 (list thread)))))
4660
4661 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4662   "Sort threads such that the thread with the most recently arrived article comes first."
4663   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4664
4665 (defun gnus-thread-highest-number (thread)
4666   "Return the highest article number in THREAD."
4667   (apply 'max (mapcar (lambda (header)
4668                         (mail-header-number header))
4669                       (message-flatten-list thread))))
4670
4671 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4672   "Sort threads such that the thread with the most recently dated article comes first."
4673   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4674
4675 (defun gnus-thread-latest-date (thread)
4676   "Return the highest article date in THREAD."
4677   (let ((previous-time 0))
4678     (apply 'max
4679            (mapcar
4680             (lambda (header)
4681               (setq previous-time
4682                     (condition-case ()
4683                         (time-to-seconds (mail-header-parse-date
4684                                           (mail-header-date header)))
4685                       (error previous-time))))
4686             (sort
4687              (message-flatten-list thread)
4688              (lambda (h1 h2)
4689                (< (mail-header-number h1)
4690                   (mail-header-number h2))))))))
4691
4692 (defun gnus-thread-total-score-1 (root)
4693   ;; This function find the total score of the thread below ROOT.
4694   (setq root (car root))
4695   (apply gnus-thread-score-function
4696          (or (append
4697               (mapcar 'gnus-thread-total-score
4698                       (cdr (gnus-id-to-thread (mail-header-id root))))
4699               (when (> (mail-header-number root) 0)
4700                 (list (or (cdr (assq (mail-header-number root)
4701                                      gnus-newsgroup-scored))
4702                           gnus-summary-default-score 0))))
4703              (list gnus-summary-default-score)
4704              '(0))))
4705
4706 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4707 (defvar gnus-tmp-prev-subject nil)
4708 (defvar gnus-tmp-false-parent nil)
4709 (defvar gnus-tmp-root-expunged nil)
4710 (defvar gnus-tmp-dummy-line nil)
4711
4712 (eval-when-compile (defvar gnus-tmp-header))
4713 (defun gnus-extra-header (type &optional header)
4714   "Return the extra header of TYPE."
4715   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4716       ""))
4717
4718 (defvar gnus-tmp-thread-tree-header-string "")
4719
4720 (defcustom gnus-sum-thread-tree-root "> "
4721   "With %B spec, used for the root of a thread.
4722 If nil, use subject instead."
4723   :version "22.1"
4724   :type '(radio (const :format "%v  " nil) string)
4725   :group 'gnus-thread)
4726 (defcustom gnus-sum-thread-tree-false-root "> "
4727   "With %B spec, used for a false root of a thread.
4728 If nil, use subject instead."
4729   :version "22.1"
4730   :type '(radio (const :format "%v  " nil) string)
4731   :group 'gnus-thread)
4732 (defcustom gnus-sum-thread-tree-single-indent ""
4733   "With %B spec, used for a thread with just one message.
4734 If nil, use subject instead."
4735   :version "22.1"
4736   :type '(radio (const :format "%v  " nil) string)
4737   :group 'gnus-thread)
4738 (defcustom gnus-sum-thread-tree-vertical "| "
4739   "With %B spec, used for drawing a vertical line."
4740   :version "22.1"
4741   :type 'string
4742   :group 'gnus-thread)
4743 (defcustom gnus-sum-thread-tree-indent "  "
4744   "With %B spec, used for indenting."
4745   :version "22.1"
4746   :type 'string
4747   :group 'gnus-thread)
4748 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4749   "With %B spec, used for a leaf with brothers."
4750   :version "22.1"
4751   :type 'string
4752   :group 'gnus-thread)
4753 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4754   "With %B spec, used for a leaf without brothers."
4755   :version "22.1"
4756   :type 'string
4757   :group 'gnus-thread)
4758
4759 (defun gnus-summary-prepare-threads (threads)
4760   "Prepare summary buffer from THREADS and indentation LEVEL.
4761 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4762 or a straight list of headers."
4763   (gnus-message 7 "Generating summary...")
4764
4765   (setq gnus-newsgroup-threads threads)
4766   (beginning-of-line)
4767
4768   (let ((gnus-tmp-level 0)
4769         (default-score (or gnus-summary-default-score 0))
4770         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4771         (building-line-count gnus-summary-display-while-building)
4772         (building-count (integerp gnus-summary-display-while-building))
4773         thread number subject stack state gnus-tmp-gathered beg-match
4774         new-roots gnus-tmp-new-adopts thread-end simp-subject
4775         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4776         gnus-tmp-replied gnus-tmp-subject-or-nil
4777         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4778         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4779         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4780         tree-stack)
4781
4782     (setq gnus-tmp-prev-subject nil
4783           gnus-tmp-thread-tree-header-string "")
4784
4785     (if (vectorp (car threads))
4786         ;; If this is a straight (sic) list of headers, then a
4787         ;; threaded summary display isn't required, so we just create
4788         ;; an unthreaded one.
4789         (gnus-summary-prepare-unthreaded threads)
4790
4791       ;; Do the threaded display.
4792
4793       (if gnus-summary-display-while-building
4794           (switch-to-buffer (buffer-name)))
4795       (while (or threads stack gnus-tmp-new-adopts new-roots)
4796
4797         (if (and (= gnus-tmp-level 0)
4798                  (or (not stack)
4799                      (= (caar stack) 0))
4800                  (not gnus-tmp-false-parent)
4801                  (or gnus-tmp-new-adopts new-roots))
4802             (if gnus-tmp-new-adopts
4803                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4804                       thread (list (car gnus-tmp-new-adopts))
4805                       gnus-tmp-header (caar thread)
4806                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4807               (when new-roots
4808                 (setq thread (list (car new-roots))
4809                       gnus-tmp-header (caar thread)
4810                       new-roots (cdr new-roots))))
4811
4812           (if threads
4813               ;; If there are some threads, we do them before the
4814               ;; threads on the stack.
4815               (setq thread threads
4816                     gnus-tmp-header (caar thread))
4817             ;; There were no current threads, so we pop something off
4818             ;; the stack.
4819             (setq state (car stack)
4820                   gnus-tmp-level (car state)
4821                   tree-stack (cadr state)
4822                   thread (caddr state)
4823                   stack (cdr stack)
4824                   gnus-tmp-header (caar thread))))
4825
4826         (setq gnus-tmp-false-parent nil)
4827         (setq gnus-tmp-root-expunged nil)
4828         (setq thread-end nil)
4829
4830         (if (stringp gnus-tmp-header)
4831             ;; The header is a dummy root.
4832             (cond
4833              ((eq gnus-summary-make-false-root 'adopt)
4834               ;; We let the first article adopt the rest.
4835               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4836                                                (cddar thread)))
4837               (setq gnus-tmp-gathered
4838                     (nconc (mapcar
4839                             (lambda (h) (mail-header-number (car h)))
4840                             (cddar thread))
4841                            gnus-tmp-gathered))
4842               (setq thread (cons (list (caar thread)
4843                                        (cadar thread))
4844                                  (cdr thread)))
4845               (setq gnus-tmp-level -1
4846                     gnus-tmp-false-parent t))
4847              ((eq gnus-summary-make-false-root 'empty)
4848               ;; We print adopted articles with empty subject fields.
4849               (setq gnus-tmp-gathered
4850                     (nconc (mapcar
4851                             (lambda (h) (mail-header-number (car h)))
4852                             (cddar thread))
4853                            gnus-tmp-gathered))
4854               (setq gnus-tmp-level -1))
4855              ((eq gnus-summary-make-false-root 'dummy)
4856               ;; We remember that we probably want to output a dummy
4857               ;; root.
4858               (setq gnus-tmp-dummy-line gnus-tmp-header)
4859               (setq gnus-tmp-prev-subject gnus-tmp-header))
4860              (t
4861               ;; We do not make a root for the gathered
4862               ;; sub-threads at all.
4863               (setq gnus-tmp-level -1)))
4864
4865           (setq number (mail-header-number gnus-tmp-header)
4866                 subject (mail-header-subject gnus-tmp-header)
4867                 simp-subject (gnus-simplify-subject-fully subject))
4868
4869           (cond
4870            ;; If the thread has changed subject, we might want to make
4871            ;; this subthread into a root.
4872            ((and (null gnus-thread-ignore-subject)
4873                  (not (zerop gnus-tmp-level))
4874                  gnus-tmp-prev-subject
4875                  (not (string= gnus-tmp-prev-subject simp-subject)))
4876             (setq new-roots (nconc new-roots (list (car thread)))
4877                   thread-end t
4878                   gnus-tmp-header nil))
4879            ;; If the article lies outside the current limit,
4880            ;; then we do not display it.
4881            ((not (memq number gnus-newsgroup-limit))
4882             (setq gnus-tmp-gathered
4883                   (nconc (mapcar
4884                           (lambda (h) (mail-header-number (car h)))
4885                           (cdar thread))
4886                          gnus-tmp-gathered))
4887             (setq gnus-tmp-new-adopts (if (cdar thread)
4888                                           (append gnus-tmp-new-adopts
4889                                                   (cdar thread))
4890                                         gnus-tmp-new-adopts)
4891                   thread-end t
4892                   gnus-tmp-header nil)
4893             (when (zerop gnus-tmp-level)
4894               (setq gnus-tmp-root-expunged t)))
4895            ;; Perhaps this article is to be marked as read?
4896            ((and gnus-summary-mark-below
4897                  (< (or (cdr (assq number gnus-newsgroup-scored))
4898                         default-score)
4899                     gnus-summary-mark-below)
4900                  ;; Don't touch sparse articles.
4901                  (not (gnus-summary-article-sparse-p number))
4902                  (not (gnus-summary-article-ancient-p number)))
4903             (setq gnus-newsgroup-unreads
4904                   (delq number gnus-newsgroup-unreads))
4905             (if gnus-newsgroup-auto-expire
4906                 (setq gnus-newsgroup-expirable
4907                       (gnus-add-to-sorted-list
4908                        gnus-newsgroup-expirable number))
4909               (push (cons number gnus-low-score-mark)
4910                     gnus-newsgroup-reads))))
4911
4912           (when gnus-tmp-header
4913             ;; We may have an old dummy line to output before this
4914             ;; article.
4915             (when (and gnus-tmp-dummy-line
4916                        (gnus-subject-equal
4917                         gnus-tmp-dummy-line
4918                         (mail-header-subject gnus-tmp-header)))
4919               (gnus-summary-insert-dummy-line
4920                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4921               (setq gnus-tmp-dummy-line nil))
4922
4923             ;; Compute the mark.
4924             (setq gnus-tmp-unread (gnus-article-mark number))
4925
4926             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4927                                   gnus-tmp-header gnus-tmp-level)
4928                   gnus-newsgroup-data)
4929
4930             ;; Actually insert the line.
4931             (setq
4932              gnus-tmp-subject-or-nil
4933              (cond
4934               ((and gnus-thread-ignore-subject
4935                     gnus-tmp-prev-subject
4936                     (not (string= gnus-tmp-prev-subject simp-subject)))
4937                subject)
4938               ((zerop gnus-tmp-level)
4939                (if (and (eq gnus-summary-make-false-root 'empty)
4940                         (memq number gnus-tmp-gathered)
4941                         gnus-tmp-prev-subject
4942                         (string= gnus-tmp-prev-subject simp-subject))
4943                    gnus-summary-same-subject
4944                  subject))
4945               (t gnus-summary-same-subject)))
4946             (if (and (eq gnus-summary-make-false-root 'adopt)
4947                      (= gnus-tmp-level 1)
4948                      (memq number gnus-tmp-gathered))
4949                 (setq gnus-tmp-opening-bracket ?\<
4950                       gnus-tmp-closing-bracket ?\>)
4951               (setq gnus-tmp-opening-bracket ?\[
4952                     gnus-tmp-closing-bracket ?\]))
4953             (setq
4954              gnus-tmp-indentation
4955              (aref gnus-thread-indent-array gnus-tmp-level)
4956              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4957              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4958                                 gnus-summary-default-score 0)
4959              gnus-tmp-score-char
4960              (if (or (null gnus-summary-default-score)
4961                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4962                          gnus-summary-zcore-fuzz))
4963                  ?\ ;;;Whitespace
4964                (if (< gnus-tmp-score gnus-summary-default-score)
4965                    gnus-score-below-mark gnus-score-over-mark))
4966              gnus-tmp-replied
4967              (cond ((memq number gnus-newsgroup-processable)
4968                     gnus-process-mark)
4969                    ((memq number gnus-newsgroup-cached)
4970                     gnus-cached-mark)
4971                    ((memq number gnus-newsgroup-replied)
4972                     gnus-replied-mark)
4973                    ((memq number gnus-newsgroup-forwarded)
4974                     gnus-forwarded-mark)
4975                    ((memq number gnus-newsgroup-saved)
4976                     gnus-saved-mark)
4977                    ((memq number gnus-newsgroup-recent)
4978                     gnus-recent-mark)
4979                    ((memq number gnus-newsgroup-unseen)
4980                     gnus-unseen-mark)
4981                    (t gnus-no-mark))
4982              gnus-tmp-downloaded
4983              (cond ((memq number gnus-newsgroup-undownloaded)
4984                     gnus-undownloaded-mark)
4985                    (gnus-newsgroup-agentized
4986                     gnus-downloaded-mark)
4987                    (t
4988                     gnus-no-mark))
4989              gnus-tmp-from (mail-header-from gnus-tmp-header)
4990              gnus-tmp-name
4991              (cond
4992               ((string-match "<[^>]+> *$" gnus-tmp-from)
4993                (setq beg-match (match-beginning 0))
4994                (or (and (string-match "^\".+\"" gnus-tmp-from)
4995                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4996                    (substring gnus-tmp-from 0 beg-match)))
4997               ((string-match "(.+)" gnus-tmp-from)
4998                (substring gnus-tmp-from
4999                           (1+ (match-beginning 0)) (1- (match-end 0))))
5000               (t gnus-tmp-from))
5001
5002              ;; Do the %B string
5003              gnus-tmp-thread-tree-header-string
5004              (cond
5005               ((not gnus-show-threads) "")
5006               ((zerop gnus-tmp-level)
5007                (cond ((cdar thread)
5008                       (or gnus-sum-thread-tree-root subject))
5009                      (gnus-tmp-new-adopts
5010                       (or gnus-sum-thread-tree-false-root subject))
5011                      (t
5012                       (or gnus-sum-thread-tree-single-indent subject))))
5013               (t
5014                (concat (apply 'concat
5015                               (mapcar (lambda (item)
5016                                         (if (= item 1)
5017                                             gnus-sum-thread-tree-vertical
5018                                           gnus-sum-thread-tree-indent))
5019                                       (cdr (reverse tree-stack))))
5020                        (if (nth 1 thread)
5021                            gnus-sum-thread-tree-leaf-with-other
5022                          gnus-sum-thread-tree-single-leaf)))))
5023             (when (string= gnus-tmp-name "")
5024               (setq gnus-tmp-name gnus-tmp-from))
5025             (unless (numberp gnus-tmp-lines)
5026               (setq gnus-tmp-lines -1))
5027             (if (= gnus-tmp-lines -1)
5028                 (setq gnus-tmp-lines "?")
5029               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5030               (gnus-put-text-property
5031              (point)
5032              (progn (eval gnus-summary-line-format-spec) (point))
5033                'gnus-number number)
5034             (when gnus-visual-p
5035               (forward-line -1)
5036               (gnus-run-hooks 'gnus-summary-update-hook)
5037               (forward-line 1))
5038
5039             (setq gnus-tmp-prev-subject simp-subject)))
5040
5041         (when (nth 1 thread)
5042           (push (list (max 0 gnus-tmp-level)
5043                       (copy-sequence tree-stack)
5044                       (nthcdr 1 thread))
5045                 stack))
5046         (push (if (nth 1 thread) 1 0) tree-stack)
5047         (incf gnus-tmp-level)
5048         (setq threads (if thread-end nil (cdar thread)))
5049         (if gnus-summary-display-while-building
5050             (if building-count
5051                 (progn
5052                   ;; use a set frequency
5053                   (setq building-line-count (1- building-line-count))
5054                   (when (= building-line-count 0)
5055                     (sit-for 0)
5056                     (setq building-line-count
5057                           gnus-summary-display-while-building)))
5058               ;; always
5059               (sit-for 0)))
5060         (unless threads
5061           (setq gnus-tmp-level 0)))))
5062   (gnus-message 7 "Generating summary...done"))
5063
5064 (defun gnus-summary-prepare-unthreaded (headers)
5065   "Generate an unthreaded summary buffer based on HEADERS."
5066   (let (header number mark)
5067
5068     (beginning-of-line)
5069
5070     (while headers
5071       ;; We may have to root out some bad articles...
5072       (when (memq (setq number (mail-header-number
5073                                 (setq header (pop headers))))
5074                   gnus-newsgroup-limit)
5075         ;; Mark article as read when it has a low score.
5076         (when (and gnus-summary-mark-below
5077                    (< (or (cdr (assq number gnus-newsgroup-scored))
5078                           gnus-summary-default-score 0)
5079                       gnus-summary-mark-below)
5080                    (not (gnus-summary-article-ancient-p number)))
5081           (setq gnus-newsgroup-unreads
5082                 (delq number gnus-newsgroup-unreads))
5083           (if gnus-newsgroup-auto-expire
5084               (push number gnus-newsgroup-expirable)
5085             (push (cons number gnus-low-score-mark)
5086                   gnus-newsgroup-reads)))
5087
5088         (setq mark (gnus-article-mark number))
5089         (push (gnus-data-make number mark (1+ (point)) header 0)
5090               gnus-newsgroup-data)
5091         (gnus-summary-insert-line
5092          header 0 number
5093          (memq number gnus-newsgroup-undownloaded)
5094          mark (memq number gnus-newsgroup-replied)
5095          (memq number gnus-newsgroup-expirable)
5096          (mail-header-subject header) nil
5097          (cdr (assq number gnus-newsgroup-scored))
5098          (memq number gnus-newsgroup-processable))))))
5099
5100 (defun gnus-summary-remove-list-identifiers ()
5101   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5102   (let ((regexp (if (consp gnus-list-identifiers)
5103                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5104                   gnus-list-identifiers))
5105         changed subject)
5106     (when regexp
5107       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5108       (dolist (header gnus-newsgroup-headers)
5109         (setq subject (mail-header-subject header)
5110               changed nil)
5111         (while (string-match regexp subject)
5112           (setq subject
5113                 (concat (substring subject 0 (match-beginning 1))
5114                         (substring subject (match-end 0)))
5115                 changed t))
5116         (when changed
5117           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5118             (setq subject
5119                   (concat (substring subject 0 (match-beginning 1))
5120                           (substring subject (match-end 1)))))
5121           (mail-header-set-subject header subject))))))
5122
5123 (defun gnus-fetch-headers (articles)
5124   "Fetch headers of ARTICLES."
5125   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5126     (gnus-message 5 "Fetching headers for %s..." name)
5127     (prog1
5128         (if (eq 'nov
5129                 (setq gnus-headers-retrieved-by
5130                       (gnus-retrieve-headers
5131                        articles gnus-newsgroup-name
5132                        ;; We might want to fetch old headers, but
5133                        ;; not if there is only 1 article.
5134                        (and (or (and
5135                                  (not (eq gnus-fetch-old-headers 'some))
5136                                  (not (numberp gnus-fetch-old-headers)))
5137                                 (> (length articles) 1))
5138                             gnus-fetch-old-headers))))
5139             (gnus-get-newsgroup-headers-xover
5140              articles nil nil gnus-newsgroup-name t)
5141           (gnus-get-newsgroup-headers))
5142       (gnus-message 5 "Fetching headers for %s...done" name))))
5143
5144 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5145   "Select newsgroup GROUP.
5146 If READ-ALL is non-nil, all articles in the group are selected.
5147 If SELECT-ARTICLES, only select those articles from GROUP."
5148   (let* ((entry (gnus-group-entry group))
5149          ;;!!! Dirty hack; should be removed.
5150          (gnus-summary-ignore-duplicates
5151           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5152               t
5153             gnus-summary-ignore-duplicates))
5154          (info (nth 2 entry))
5155          articles fetched-articles cached)
5156
5157     (unless (gnus-check-server
5158              (set (make-local-variable 'gnus-current-select-method)
5159                   (gnus-find-method-for-group group)))
5160       (error "Couldn't open server"))
5161
5162     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5163         (gnus-activate-group group)     ; Or we can activate it...
5164         (progn                          ; Or we bug out.
5165           (when (equal major-mode 'gnus-summary-mode)
5166             (gnus-kill-buffer (current-buffer)))
5167           (error "Couldn't activate group %s: %s"
5168                  group (gnus-status-message group))))
5169
5170     (unless (gnus-request-group group t)
5171       (when (equal major-mode 'gnus-summary-mode)
5172         (gnus-kill-buffer (current-buffer)))
5173       (error "Couldn't request group %s: %s"
5174              group (gnus-status-message group)))
5175
5176     (when gnus-agent
5177       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5178
5179       (setq gnus-summary-use-undownloaded-faces
5180             (gnus-agent-find-parameter
5181              group
5182              'agent-enable-undownloaded-faces)))
5183
5184     (setq gnus-newsgroup-name group
5185           gnus-newsgroup-unselected nil
5186           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5187
5188     (let ((display (gnus-group-find-parameter group 'display)))
5189       (setq gnus-newsgroup-display
5190             (cond
5191              ((not (zerop (or (car-safe read-all) 0)))
5192               ;; The user entered the group with C-u SPC/RET, let's show
5193               ;; all articles.
5194               'gnus-not-ignore)
5195              ((eq display 'all)
5196               'gnus-not-ignore)
5197              ((arrayp display)
5198               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5199              ((numberp display)
5200               ;; The following is probably the "correct" solution, but
5201               ;; it makes Gnus fetch all headers and then limit the
5202               ;; articles (which is slow), so instead we hack the
5203               ;; select-articles parameter instead. -- Simon Josefsson
5204               ;; <jas@kth.se>
5205               ;;
5206               ;; (gnus-byte-compile
5207               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5208               ;;                         display)))))
5209               (setq select-articles
5210                     (gnus-uncompress-range
5211                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5212                              (if (> tmp 0)
5213                                  tmp
5214                                1))
5215                            (cdr (gnus-active group)))))
5216               nil)
5217              (t
5218               nil))))
5219
5220     (gnus-summary-setup-default-charset)
5221
5222     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5223     (when (gnus-virtual-group-p group)
5224       (setq cached gnus-newsgroup-cached))
5225
5226     (setq gnus-newsgroup-unreads
5227           (gnus-sorted-ndifference
5228            (gnus-sorted-ndifference gnus-newsgroup-unreads
5229                                     gnus-newsgroup-marked)
5230            gnus-newsgroup-dormant))
5231
5232     (setq gnus-newsgroup-processable nil)
5233
5234     (gnus-update-read-articles group gnus-newsgroup-unreads)
5235
5236     ;; Adjust and set lists of article marks.
5237     (when info
5238       (gnus-adjust-marked-articles info))
5239     (if (setq articles select-articles)
5240         (setq gnus-newsgroup-unselected
5241               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5242       (setq articles (gnus-articles-to-read group read-all)))
5243
5244     (cond
5245      ((null articles)
5246       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5247       'quit)
5248      ((eq articles 0) nil)
5249      (t
5250       ;; Init the dependencies hash table.
5251       (setq gnus-newsgroup-dependencies
5252             (gnus-make-hashtable (length articles)))
5253       (gnus-set-global-variables)
5254       ;; Retrieve the headers and read them in.
5255
5256       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5257
5258       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5259       (when cached
5260         (setq gnus-newsgroup-cached cached))
5261
5262       ;; Suppress duplicates?
5263       (when gnus-suppress-duplicates
5264         (gnus-dup-suppress-articles))
5265
5266       ;; Set the initial limit.
5267       (setq gnus-newsgroup-limit (copy-sequence articles))
5268       ;; Remove canceled articles from the list of unread articles.
5269       (setq fetched-articles
5270             (mapcar (lambda (headers) (mail-header-number headers))
5271                     gnus-newsgroup-headers))
5272       (setq gnus-newsgroup-articles fetched-articles)
5273       (setq gnus-newsgroup-unreads
5274             (gnus-sorted-nintersection
5275              gnus-newsgroup-unreads fetched-articles))
5276       (gnus-compute-unseen-list)
5277
5278       ;; Removed marked articles that do not exist.
5279       (gnus-update-missing-marks
5280        (gnus-sorted-difference articles fetched-articles))
5281       ;; We might want to build some more threads first.
5282       (when (and gnus-fetch-old-headers
5283                  (eq gnus-headers-retrieved-by 'nov))
5284         (if (eq gnus-fetch-old-headers 'invisible)
5285             (gnus-build-all-threads)
5286           (gnus-build-old-threads)))
5287       ;; Let the Gnus agent mark articles as read.
5288       (when gnus-agent
5289         (gnus-agent-get-undownloaded-list))
5290       ;; Remove list identifiers from subject
5291       (when gnus-list-identifiers
5292         (gnus-summary-remove-list-identifiers))
5293       ;; Check whether auto-expire is to be done in this group.
5294       (setq gnus-newsgroup-auto-expire
5295             (gnus-group-auto-expirable-p group))
5296       ;; Set up the article buffer now, if necessary.
5297       (unless gnus-single-article-buffer
5298         (gnus-article-setup-buffer))
5299       ;; First and last article in this newsgroup.
5300       (when gnus-newsgroup-headers
5301         (setq gnus-newsgroup-begin
5302               (mail-header-number (car gnus-newsgroup-headers))
5303               gnus-newsgroup-end
5304               (mail-header-number
5305                (gnus-last-element gnus-newsgroup-headers))))
5306       ;; GROUP is successfully selected.
5307       (or gnus-newsgroup-headers t)))))
5308
5309 (defun gnus-compute-unseen-list ()
5310   ;; The `seen' marks are treated specially.
5311   (if (not gnus-newsgroup-seen)
5312       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5313     (setq gnus-newsgroup-unseen
5314           (gnus-inverse-list-range-intersection
5315            gnus-newsgroup-articles gnus-newsgroup-seen))))
5316
5317 (defun gnus-summary-display-make-predicate (display)
5318   (require 'gnus-agent)
5319   (when (= (length display) 1)
5320     (setq display (car display)))
5321   (unless gnus-summary-display-cache
5322     (dolist (elem (append '((unread . unread)
5323                             (read . read)
5324                             (unseen . unseen))
5325                           gnus-article-mark-lists))
5326       (push (cons (cdr elem)
5327                   (gnus-byte-compile
5328                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5329             gnus-summary-display-cache)))
5330   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5331         (gnus-category-predicate-cache gnus-summary-display-cache))
5332     (gnus-get-predicate display)))
5333
5334 ;; Uses the dynamically bound `number' variable.
5335 (eval-when-compile
5336   (defvar number))
5337 (defun gnus-article-marked-p (type &optional article)
5338   (let ((article (or article number)))
5339     (cond
5340      ((eq type 'tick)
5341       (memq article gnus-newsgroup-marked))
5342      ((eq type 'spam)
5343       (memq article gnus-newsgroup-spam-marked))
5344      ((eq type 'unsend)
5345       (memq article gnus-newsgroup-unsendable))
5346      ((eq type 'undownload)
5347       (memq article gnus-newsgroup-undownloaded))
5348      ((eq type 'download)
5349       (memq article gnus-newsgroup-downloadable))
5350      ((eq type 'unread)
5351       (memq article gnus-newsgroup-unreads))
5352      ((eq type 'read)
5353       (memq article gnus-newsgroup-reads))
5354      ((eq type 'dormant)
5355       (memq article gnus-newsgroup-dormant) )
5356      ((eq type 'expire)
5357       (memq article gnus-newsgroup-expirable))
5358      ((eq type 'reply)
5359       (memq article gnus-newsgroup-replied))
5360      ((eq type 'killed)
5361       (memq article gnus-newsgroup-killed))
5362      ((eq type 'bookmark)
5363       (assq article gnus-newsgroup-bookmarks))
5364      ((eq type 'score)
5365       (assq article gnus-newsgroup-scored))
5366      ((eq type 'save)
5367       (memq article gnus-newsgroup-saved))
5368      ((eq type 'cache)
5369       (memq article gnus-newsgroup-cached))
5370      ((eq type 'forward)
5371       (memq article gnus-newsgroup-forwarded))
5372      ((eq type 'seen)
5373       (not (memq article gnus-newsgroup-unseen)))
5374      ((eq type 'recent)
5375       (memq article gnus-newsgroup-recent))
5376      (t t))))
5377
5378 (defun gnus-articles-to-read (group &optional read-all)
5379   "Find out what articles the user wants to read."
5380   (let* ((articles
5381           ;; Select all articles if `read-all' is non-nil, or if there
5382           ;; are no unread articles.
5383           (if (or read-all
5384                   (and (zerop (length gnus-newsgroup-marked))
5385                        (zerop (length gnus-newsgroup-unreads)))
5386                   ;; Fetch all if the predicate is non-nil.
5387                   gnus-newsgroup-display)
5388               ;; We want to select the headers for all the articles in
5389               ;; the group, so we select either all the active
5390               ;; articles in the group, or (if that's nil), the
5391               ;; articles in the cache.
5392               (or
5393                (gnus-uncompress-range (gnus-active group))
5394                (gnus-cache-articles-in-group group))
5395             ;; Select only the "normal" subset of articles.
5396             (gnus-sorted-nunion
5397              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5398              gnus-newsgroup-unreads)))
5399          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5400          (scored (length scored-list))
5401          (number (length articles))
5402          (marked (+ (length gnus-newsgroup-marked)
5403                     (length gnus-newsgroup-dormant)))
5404          (select
5405           (cond
5406            ((numberp read-all)
5407             read-all)
5408            ((numberp gnus-newsgroup-display)
5409             gnus-newsgroup-display)
5410            (t
5411             (condition-case ()
5412                 (cond
5413                  ((and (or (<= scored marked) (= scored number))
5414                        (numberp gnus-large-newsgroup)
5415                        (> number gnus-large-newsgroup))
5416                   (let* ((cursor-in-echo-area nil)
5417                          (initial (gnus-parameter-large-newsgroup-initial
5418                                    gnus-newsgroup-name))
5419                          (input
5420                           (read-string
5421                            (format
5422                             "How many articles from %s (%s %d): "
5423                             (gnus-limit-string
5424                              (gnus-group-decoded-name gnus-newsgroup-name)
5425                              35)
5426                             (if initial "max" "default")
5427                             number)
5428                            (if initial
5429                                (cons (number-to-string initial)
5430                                      0)))))
5431                     (if (string-match "^[ \t]*$" input) number input)))
5432                  ((and (> scored marked) (< scored number)
5433                        (> (- scored number) 20))
5434                   (let ((input
5435                          (read-string
5436                           (format "%s %s (%d scored, %d total): "
5437                                   "How many articles from"
5438                                   (gnus-group-decoded-name group)
5439                                   scored number))))
5440                     (if (string-match "^[ \t]*$" input)
5441                         number input)))
5442                  (t number))
5443               (quit
5444                (message "Quit getting the articles to read")
5445                nil))))))
5446     (setq select (if (stringp select) (string-to-number select) select))
5447     (if (or (null select) (zerop select))
5448         select
5449       (if (and (not (zerop scored)) (<= (abs select) scored))
5450           (progn
5451             (setq articles (sort scored-list '<))
5452             (setq number (length articles)))
5453         (setq articles (copy-sequence articles)))
5454
5455       (when (< (abs select) number)
5456         (if (< select 0)
5457             ;; Select the N oldest articles.
5458             (setcdr (nthcdr (1- (abs select)) articles) nil)
5459           ;; Select the N most recent articles.
5460           (setq articles (nthcdr (- number select) articles))))
5461       (setq gnus-newsgroup-unselected
5462             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5463       (when gnus-alter-articles-to-read-function
5464         (setq articles
5465               (sort
5466                (funcall gnus-alter-articles-to-read-function
5467                         gnus-newsgroup-name articles)
5468                '<)))
5469       articles)))
5470
5471 (defun gnus-killed-articles (killed articles)
5472   (let (out)
5473     (while articles
5474       (when (inline (gnus-member-of-range (car articles) killed))
5475         (push (car articles) out))
5476       (setq articles (cdr articles)))
5477     out))
5478
5479 (defun gnus-uncompress-marks (marks)
5480   "Uncompress the mark ranges in MARKS."
5481   (let ((uncompressed '(score bookmark))
5482         out)
5483     (while marks
5484       (if (memq (caar marks) uncompressed)
5485           (push (car marks) out)
5486         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5487       (setq marks (cdr marks)))
5488     out))
5489
5490 (defun gnus-article-mark-to-type (mark)
5491   "Return the type of MARK."
5492   (or (cadr (assq mark gnus-article-special-mark-lists))
5493       'list))
5494
5495 (defun gnus-article-unpropagatable-p (mark)
5496   "Return whether MARK should be propagated to back end."
5497   (memq mark gnus-article-unpropagated-mark-lists))
5498
5499 (defun gnus-adjust-marked-articles (info)
5500   "Set all article lists and remove all marks that are no longer valid."
5501   (let* ((marked-lists (gnus-info-marks info))
5502          (active (gnus-active (gnus-info-group info)))
5503          (min (car active))
5504          (max (cdr active))
5505          (types gnus-article-mark-lists)
5506          marks var articles article mark mark-type
5507          bgn end)
5508
5509     (dolist (marks marked-lists)
5510       (setq mark (car marks)
5511             mark-type (gnus-article-mark-to-type mark)
5512             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5513
5514       ;; We set the variable according to the type of the marks list,
5515       ;; and then adjust the marks to a subset of the active articles.
5516       (cond
5517        ;; Adjust "simple" lists - compressed yet unsorted
5518        ((eq mark-type 'list)
5519         ;; Simultaneously uncompress and clip to active range
5520         ;; See gnus-uncompress-range for a description of possible marks
5521         (let (l lh)
5522           (if (not (cadr marks))
5523               (set var nil)
5524             (setq articles (if (numberp (cddr marks))
5525                                (list (cdr marks))
5526                              (cdr marks))
5527                   lh (cons nil nil)
5528                   l lh)
5529
5530             (while (setq article (pop articles))
5531               (cond ((consp article)
5532                      (setq bgn (max (car article) min)
5533                            end (min (cdr article) max))
5534                      (while (<= bgn end)
5535                        (setq l (setcdr l (cons bgn nil))
5536                              bgn (1+ bgn))))
5537                     ((and (<= min article)
5538                           (>= max article))
5539                      (setq l (setcdr l (cons article nil))))))
5540             (set var (cdr lh)))))
5541        ;; Adjust assocs.
5542        ((eq mark-type 'tuple)
5543         (set var (setq articles (cdr marks)))
5544         (when (not (listp (cdr (symbol-value var))))
5545           (set var (list (symbol-value var))))
5546         (when (not (listp (cdr articles)))
5547           (setq articles (list articles)))
5548         (while articles
5549           (when (or (not (consp (setq article (pop articles))))
5550                     (< (car article) min)
5551                     (> (car article) max))
5552             (set var (delq article (symbol-value var))))))
5553        ;; Adjust ranges (sloppily).
5554        ((eq mark-type 'range)
5555         (cond
5556          ((eq mark 'seen)
5557           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5558           ;; It should be (seen (NUM1 . NUM2)).
5559           (when (numberp (cddr marks))
5560             (setcdr marks (list (cdr marks))))
5561           (setq articles (cdr marks))
5562           (while (and articles
5563                       (or (and (consp (car articles))
5564                                (> min (cdar articles)))
5565                           (and (numberp (car articles))
5566                                (> min (car articles)))))
5567             (pop articles))
5568           (set var articles))))))))
5569
5570 (defun gnus-update-missing-marks (missing)
5571   "Go through the list of MISSING articles and remove them from the mark lists."
5572   (when missing
5573     (let (var m)
5574       ;; Go through all types.
5575       (dolist (elem gnus-article-mark-lists)
5576         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5577           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5578           (when (symbol-value var)
5579             ;; This list has articles.  So we delete all missing
5580             ;; articles from it.
5581             (setq m missing)
5582             (while m
5583               (set var (delq (pop m) (symbol-value var))))))))))
5584
5585 (defun gnus-update-marks ()
5586   "Enter the various lists of marked articles into the newsgroup info list."
5587   (let ((types gnus-article-mark-lists)
5588         (info (gnus-get-info gnus-newsgroup-name))
5589         type list newmarked symbol delta-marks)
5590     (when info
5591       ;; Add all marks lists to the list of marks lists.
5592       (while (setq type (pop types))
5593         (setq list (symbol-value
5594                     (setq symbol
5595                           (intern (format "gnus-newsgroup-%s" (car type))))))
5596
5597         (when list
5598           ;; Get rid of the entries of the articles that have the
5599           ;; default score.
5600           (when (and (eq (cdr type) 'score)
5601                      gnus-save-score
5602                      list)
5603             (let* ((arts list)
5604                    (prev (cons nil list))
5605                    (all prev))
5606               (while arts
5607                 (if (or (not (consp (car arts)))
5608                         (= (cdar arts) gnus-summary-default-score))
5609                     (setcdr prev (cdr arts))
5610                   (setq prev arts))
5611                 (setq arts (cdr arts)))
5612               (setq list (cdr all)))))
5613
5614         (when (eq (cdr type) 'seen)
5615           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5616
5617         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5618           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5619
5620         (when (and (gnus-check-backend-function
5621                     'request-set-mark gnus-newsgroup-name)
5622                    (not (gnus-article-unpropagatable-p (cdr type))))
5623           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5624                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5625                  (add (gnus-remove-from-range
5626                        (gnus-copy-sequence list) old)))
5627             (when add
5628               (push (list add 'add (list (cdr type))) delta-marks))
5629             (when del
5630               (push (list del 'del (list (cdr type))) delta-marks))))
5631
5632         (when list
5633           (push (cons (cdr type) list) newmarked)))
5634
5635       (when delta-marks
5636         (unless (gnus-check-group gnus-newsgroup-name)
5637           (error "Can't open server for %s" gnus-newsgroup-name))
5638         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5639
5640       ;; Enter these new marks into the info of the group.
5641       (if (nthcdr 3 info)
5642           (setcar (nthcdr 3 info) newmarked)
5643         ;; Add the marks lists to the end of the info.
5644         (when newmarked
5645           (setcdr (nthcdr 2 info) (list newmarked))))
5646
5647       ;; Cut off the end of the info if there's nothing else there.
5648       (let ((i 5))
5649         (while (and (> i 2)
5650                     (not (nth i info)))
5651           (when (nthcdr (decf i) info)
5652             (setcdr (nthcdr i info) nil)))))))
5653
5654 (defun gnus-set-mode-line (where)
5655   "Set the mode line of the article or summary buffers.
5656 If WHERE is `summary', the summary mode line format will be used."
5657   ;; Is this mode line one we keep updated?
5658   (when (and (memq where gnus-updated-mode-lines)
5659              (symbol-value
5660               (intern (format "gnus-%s-mode-line-format-spec" where))))
5661     (let (mode-string)
5662       (save-excursion
5663         ;; We evaluate this in the summary buffer since these
5664         ;; variables are buffer-local to that buffer.
5665         (set-buffer gnus-summary-buffer)
5666         ;; We bind all these variables that are used in the `eval' form
5667         ;; below.
5668         (let* ((mformat (symbol-value
5669                          (intern
5670                           (format "gnus-%s-mode-line-format-spec" where))))
5671                (gnus-tmp-group-name (gnus-group-decoded-name
5672                                      gnus-newsgroup-name))
5673                (gnus-tmp-article-number (or gnus-current-article 0))
5674                (gnus-tmp-unread gnus-newsgroup-unreads)
5675                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5676                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5677                (gnus-tmp-unread-and-unselected
5678                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5679                             (zerop gnus-tmp-unselected))
5680                        "")
5681                       ((zerop gnus-tmp-unselected)
5682                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5683                       (t (format "{%d(+%d) more}"
5684                                  gnus-tmp-unread-and-unticked
5685                                  gnus-tmp-unselected))))
5686                (gnus-tmp-subject
5687                 (if (and gnus-current-headers
5688                          (vectorp gnus-current-headers))
5689                     (gnus-mode-string-quote
5690                      (mail-header-subject gnus-current-headers))
5691                   ""))
5692                bufname-length max-len
5693                gnus-tmp-header);; passed as argument to any user-format-funcs
5694           (setq mode-string (eval mformat))
5695           (setq bufname-length (if (string-match "%b" mode-string)
5696                                    (- (length
5697                                        (buffer-name
5698                                         (if (eq where 'summary)
5699                                             nil
5700                                           (get-buffer gnus-article-buffer))))
5701                                       2)
5702                                  0))
5703           (setq max-len (max 4 (if gnus-mode-non-string-length
5704                                    (- (window-width)
5705                                       gnus-mode-non-string-length
5706                                       bufname-length)
5707                                  (length mode-string))))
5708           ;; We might have to chop a bit of the string off...
5709           (when (> (length mode-string) max-len)
5710             (setq mode-string
5711                   (concat (gnus-truncate-string mode-string (- max-len 3))
5712                           "...")))
5713           ;; Pad the mode string a bit.
5714           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5715       ;; Update the mode line.
5716       (setq mode-line-buffer-identification
5717             (gnus-mode-line-buffer-identification (list mode-string)))
5718       (set-buffer-modified-p t))))
5719
5720 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5721   "Go through the HEADERS list and add all Xrefs to a hash table.
5722 The resulting hash table is returned, or nil if no Xrefs were found."
5723   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5724          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5725          (xref-hashtb (gnus-make-hashtable))
5726          start group entry number xrefs header)
5727     (while headers
5728       (setq header (pop headers))
5729       (when (and (setq xrefs (mail-header-xref header))
5730                  (not (memq (setq number (mail-header-number header))
5731                             unreads)))
5732         (setq start 0)
5733         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5734           (setq start (match-end 0))
5735           (setq group (if prefix
5736                           (concat prefix (substring xrefs (match-beginning 1)
5737                                                     (match-end 1)))
5738                         (substring xrefs (match-beginning 1) (match-end 1))))
5739           (setq number
5740                 (string-to-number (substring xrefs (match-beginning 2)
5741                                           (match-end 2))))
5742           (if (setq entry (gnus-gethash group xref-hashtb))
5743               (setcdr entry (cons number (cdr entry)))
5744             (gnus-sethash group (cons number nil) xref-hashtb)))))
5745     (and start xref-hashtb)))
5746
5747 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5748   "Look through all the headers and mark the Xrefs as read."
5749   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5750         name info xref-hashtb idlist method nth4)
5751     (save-excursion
5752       (set-buffer gnus-group-buffer)
5753       (when (setq xref-hashtb
5754                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5755         (mapatoms
5756          (lambda (group)
5757            (unless (string= from-newsgroup (setq name (symbol-name group)))
5758              (setq idlist (symbol-value group))
5759              ;; Dead groups are not updated.
5760              (and (prog1
5761                       (setq info (gnus-get-info name))
5762                     (when (stringp (setq nth4 (gnus-info-method info)))
5763                       (setq nth4 (gnus-server-to-method nth4))))
5764                   ;; Only do the xrefs if the group has the same
5765                   ;; select method as the group we have just read.
5766                   (or (gnus-methods-equal-p
5767                        nth4 (gnus-find-method-for-group from-newsgroup))
5768                       virtual
5769                       (equal nth4 (setq method (gnus-find-method-for-group
5770                                                 from-newsgroup)))
5771                       (and (equal (car nth4) (car method))
5772                            (equal (nth 1 nth4) (nth 1 method))))
5773                   gnus-use-cross-reference
5774                   (or (not (eq gnus-use-cross-reference t))
5775                       virtual
5776                       ;; Only do cross-references on subscribed
5777                       ;; groups, if that is what is wanted.
5778                       (<= (gnus-info-level info) gnus-level-subscribed))
5779                   (gnus-group-make-articles-read name idlist))))
5780          xref-hashtb)))))
5781
5782 (defun gnus-compute-read-articles (group articles)
5783   (let* ((entry (gnus-group-entry group))
5784          (info (nth 2 entry))
5785          (active (gnus-active group))
5786          ninfo)
5787     (when entry
5788       ;; First peel off all invalid article numbers.
5789       (when active
5790         (let ((ids articles)
5791               id first)
5792           (while (setq id (pop ids))
5793             (when (and first (> id (cdr active)))
5794               ;; We'll end up in this situation in one particular
5795               ;; obscure situation.  If you re-scan a group and get
5796               ;; a new article that is cross-posted to a different
5797               ;; group that has not been re-scanned, you might get
5798               ;; crossposted article that has a higher number than
5799               ;; Gnus believes possible.  So we re-activate this
5800               ;; group as well.  This might mean doing the
5801               ;; crossposting thingy will *increase* the number
5802               ;; of articles in some groups.  Tsk, tsk.
5803               (setq active (or (gnus-activate-group group) active)))
5804             (when (or (> id (cdr active))
5805                       (< id (car active)))
5806               (setq articles (delq id articles))))))
5807       ;; If the read list is nil, we init it.
5808       (if (and active
5809                (null (gnus-info-read info))
5810                (> (car active) 1))
5811           (setq ninfo (cons 1 (1- (car active))))
5812         (setq ninfo (gnus-info-read info)))
5813       ;; Then we add the read articles to the range.
5814       (gnus-add-to-range
5815        ninfo (setq articles (sort articles '<))))))
5816
5817 (defun gnus-group-make-articles-read (group articles)
5818   "Update the info of GROUP to say that ARTICLES are read."
5819   (let* ((num 0)
5820          (entry (gnus-group-entry group))
5821          (info (nth 2 entry))
5822          (active (gnus-active group))
5823          range)
5824     (when entry
5825       (setq range (gnus-compute-read-articles group articles))
5826       (with-current-buffer gnus-group-buffer
5827         (gnus-undo-register
5828           `(progn
5829              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5830              (gnus-info-set-read ',info ',(gnus-info-read info))
5831              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5832              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5833              (gnus-group-update-group ,group t))))
5834       ;; Add the read articles to the range.
5835       (gnus-info-set-read info range)
5836       (gnus-request-set-mark group (list (list range 'add '(read))))
5837       ;; Then we have to re-compute how many unread
5838       ;; articles there are in this group.
5839       (when active
5840         (cond
5841          ((not range)
5842           (setq num (- (1+ (cdr active)) (car active))))
5843          ((not (listp (cdr range)))
5844           (setq num (- (cdr active) (- (1+ (cdr range))
5845                                        (car range)))))
5846          (t
5847           (while range
5848             (if (numberp (car range))
5849                 (setq num (1+ num))
5850               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5851             (setq range (cdr range)))
5852           (setq num (- (cdr active) num))))
5853         ;; Update the number of unread articles.
5854         (setcar entry num)
5855         ;; Update the group buffer.
5856         (unless (gnus-ephemeral-group-p group)
5857           (gnus-group-update-group group t))))))
5858
5859 (defvar gnus-newsgroup-none-id 0)
5860
5861 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5862   (let ((cur nntp-server-buffer)
5863         (dependencies
5864          (or dependencies
5865              (with-current-buffer gnus-summary-buffer
5866                gnus-newsgroup-dependencies)))
5867         headers id end ref number
5868         (mail-parse-charset gnus-newsgroup-charset)
5869         (mail-parse-ignored-charsets
5870          (save-excursion (condition-case nil
5871                              (set-buffer gnus-summary-buffer)
5872                            (error))
5873                          gnus-newsgroup-ignored-charsets)))
5874     (save-excursion
5875       (set-buffer nntp-server-buffer)
5876       ;; Translate all TAB characters into SPACE characters.
5877       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5878       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5879       (ietf-drums-unfold-fws)
5880       (gnus-run-hooks 'gnus-parse-headers-hook)
5881       (let ((case-fold-search t)
5882             in-reply-to header p lines chars ctype)
5883         (goto-char (point-min))
5884         ;; Search to the beginning of the next header.  Error messages
5885         ;; do not begin with 2 or 3.
5886         (while (re-search-forward "^[23][0-9]+ " nil t)
5887           (setq id nil
5888                 ref nil)
5889           ;; This implementation of this function, with nine
5890           ;; search-forwards instead of the one re-search-forward and
5891           ;; a case (which basically was the old function) is actually
5892           ;; about twice as fast, even though it looks messier.  You
5893           ;; can't have everything, I guess.  Speed and elegance
5894           ;; doesn't always go hand in hand.
5895           (setq
5896            header
5897            (make-full-mail-header
5898             ;; Number.
5899             (prog1
5900                 (setq number (read cur))
5901               (end-of-line)
5902               (setq p (point))
5903               (narrow-to-region (point)
5904                                 (or (and (search-forward "\n.\n" nil t)
5905                                          (- (point) 2))
5906                                     (point))))
5907             ;; Subject.
5908             (progn
5909               (goto-char p)
5910               (if (search-forward "\nsubject:" nil t)
5911                   (nnheader-header-value)
5912                 "(none)"))
5913             ;; From.
5914             (progn
5915               (goto-char p)
5916               (if (search-forward "\nfrom:" nil t)
5917                   (nnheader-header-value)
5918                 "(nobody)"))
5919             ;; Date.
5920             (progn
5921               (goto-char p)
5922               (if (search-forward "\ndate:" nil t)
5923                   (nnheader-header-value) ""))
5924             ;; Message-ID.
5925             (progn
5926               (goto-char p)
5927               (setq id (if (re-search-forward
5928                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5929                            ;; We do it this way to make sure the Message-ID
5930                            ;; is (somewhat) syntactically valid.
5931                            (buffer-substring (match-beginning 1)
5932                                              (match-end 1))
5933                          ;; If there was no message-id, we just fake one
5934                          ;; to make subsequent routines simpler.
5935                          (nnheader-generate-fake-message-id number))))
5936             ;; References.
5937             (progn
5938               (goto-char p)
5939               (if (search-forward "\nreferences:" nil t)
5940                   (progn
5941                     (setq end (point))
5942                     (prog1
5943                         (nnheader-header-value)
5944                       (setq ref
5945                             (buffer-substring
5946                              (progn
5947                                ;; (end-of-line)
5948                                (search-backward ">" end t)
5949                                (1+ (point)))
5950                              (progn
5951                                (search-backward "<" end t)
5952                                (point))))))
5953                 ;; Get the references from the in-reply-to header if there
5954                 ;; were no references and the in-reply-to header looks
5955                 ;; promising.
5956                 (if (and (search-forward "\nin-reply-to:" nil t)
5957                          (setq in-reply-to (nnheader-header-value))
5958                          (string-match "<[^>]+>" in-reply-to))
5959                     (let (ref2)
5960                       (setq ref (substring in-reply-to (match-beginning 0)
5961                                            (match-end 0)))
5962                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5963                         (setq ref2 (substring in-reply-to (match-beginning 0)
5964                                               (match-end 0)))
5965                         (when (> (length ref2) (length ref))
5966                           (setq ref ref2)))
5967                       ref)
5968                   (setq ref nil))))
5969             ;; Chars.
5970             (progn
5971               (goto-char p)
5972               (if (search-forward "\nchars: " nil t)
5973                   (if (numberp (setq chars (ignore-errors (read cur))))
5974                       chars -1)
5975                 -1))
5976             ;; Lines.
5977             (progn
5978               (goto-char p)
5979               (if (search-forward "\nlines: " nil t)
5980                   (if (numberp (setq lines (ignore-errors (read cur))))
5981                       lines -1)
5982                 -1))
5983             ;; Xref.
5984             (progn
5985               (goto-char p)
5986               (and (search-forward "\nxref:" nil t)
5987                    (nnheader-header-value)))
5988             ;; Extra.
5989             (when gnus-extra-headers
5990               (let ((extra gnus-extra-headers)
5991                     out)
5992                 (while extra
5993                   (goto-char p)
5994                   (when (search-forward
5995                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5996                     (push (cons (car extra) (nnheader-header-value)) out))
5997                   (pop extra))
5998                 out))))
5999           (goto-char p)
6000           (if (and (search-forward "\ncontent-type: " nil t)
6001                    (setq ctype (nnheader-header-value)))
6002               (mime-entity-set-content-type-internal
6003                header (mime-parse-Content-Type ctype)))
6004           (when (equal id ref)
6005             (setq ref nil))
6006
6007           (when gnus-alter-header-function
6008             (funcall gnus-alter-header-function header)
6009             (setq id (mail-header-id header)
6010                   ref (gnus-parent-id (mail-header-references header))))
6011
6012           (when (setq header
6013                       (gnus-dependencies-add-header
6014                        header dependencies force-new))
6015             (push header headers))
6016           (goto-char (point-max))
6017           (widen))
6018         (nreverse headers)))))
6019
6020 ;; Goes through the xover lines and returns a list of vectors
6021 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6022                                                   force-new dependencies
6023                                                   group also-fetch-heads)
6024   "Parse the news overview data in the server buffer.
6025 Return a list of headers that match SEQUENCE (see
6026 `nntp-retrieve-headers')."
6027   ;; Get the Xref when the users reads the articles since most/some
6028   ;; NNTP servers do not include Xrefs when using XOVER.
6029   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6030   (let ((mail-parse-charset gnus-newsgroup-charset)
6031         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6032         (cur nntp-server-buffer)
6033         (dependencies (or dependencies gnus-newsgroup-dependencies))
6034         (allp (cond
6035                ((eq gnus-read-all-available-headers t)
6036                 t)
6037                ((stringp gnus-read-all-available-headers)
6038                 (string-match gnus-read-all-available-headers group))
6039                (t
6040                 nil)))
6041         number headers header)
6042     (save-excursion
6043       (set-buffer nntp-server-buffer)
6044       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6045       ;; Allow the user to mangle the headers before parsing them.
6046       (gnus-run-hooks 'gnus-parse-headers-hook)
6047       (goto-char (point-min))
6048       (gnus-parse-without-error
6049         (while (and (or sequence allp)
6050                     (not (eobp)))
6051           (setq number (read cur))
6052           (when (not allp)
6053             (while (and sequence
6054                         (< (car sequence) number))
6055               (setq sequence (cdr sequence))))
6056           (when (and (or allp
6057                          (and sequence
6058                               (eq number (car sequence))))
6059                      (progn
6060                        (setq sequence (cdr sequence))
6061                        (setq header (inline
6062                                       (gnus-nov-parse-line
6063                                        number dependencies force-new)))))
6064             (push header headers))
6065           (forward-line 1)))
6066       ;; A common bug in inn is that if you have posted an article and
6067       ;; then retrieves the active file, it will answer correctly --
6068       ;; the new article is included.  However, a NOV entry for the
6069       ;; article may not have been generated yet, so this may fail.
6070       ;; We work around this problem by retrieving the last few
6071       ;; headers using HEAD.
6072       (if (or (not also-fetch-heads)
6073               (not sequence))
6074           ;; We (probably) got all the headers.
6075           (nreverse headers)
6076         (let ((gnus-nov-is-evil t))
6077           (nconc
6078            (nreverse headers)
6079            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6080              (gnus-get-newsgroup-headers))))))))
6081
6082 (defun gnus-article-get-xrefs ()
6083   "Fill in the Xref value in `gnus-current-headers', if necessary.
6084 This is meant to be called in `gnus-article-internal-prepare-hook'."
6085   (let ((headers (with-current-buffer gnus-summary-buffer
6086                    gnus-current-headers)))
6087     (or (not gnus-use-cross-reference)
6088         (not headers)
6089         (and (mail-header-xref headers)
6090              (not (string= (mail-header-xref headers) "")))
6091         (let ((case-fold-search t)
6092               xref)
6093           (save-restriction
6094             (nnheader-narrow-to-headers)
6095             (goto-char (point-min))
6096             (when (or (and (not (eobp))
6097                            (eq (downcase (char-after)) ?x)
6098                            (looking-at "Xref:"))
6099                       (search-forward "\nXref:" nil t))
6100               (goto-char (1+ (match-end 0)))
6101               (setq xref (buffer-substring (point) (point-at-eol)))
6102               (mail-header-set-xref headers xref)))))))
6103
6104 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6105   "Find article ID and insert the summary line for that article.
6106 OLD-HEADER can either be a header or a line number to insert
6107 the subject line on."
6108   (let* ((line (and (numberp old-header) old-header))
6109          (old-header (and (vectorp old-header) old-header))
6110          (header (cond ((and old-header use-old-header)
6111                         old-header)
6112                        ((and (numberp id)
6113                              (gnus-number-to-header id))
6114                         (gnus-number-to-header id))
6115                        (t
6116                         (gnus-read-header id))))
6117          (number (and (numberp id) id))
6118          d)
6119     (when header
6120       ;; Rebuild the thread that this article is part of and go to the
6121       ;; article we have fetched.
6122       (when (and (not gnus-show-threads)
6123                  old-header)
6124         (when (and number
6125                    (setq d (gnus-data-find (mail-header-number old-header))))
6126           (goto-char (gnus-data-pos d))
6127           (gnus-data-remove
6128            number
6129            (- (point-at-bol)
6130               (prog1
6131                   (1+ (point-at-eol))
6132                 (gnus-delete-line))))))
6133       ;; Remove list identifiers from subject.
6134       (when gnus-list-identifiers
6135         (let ((gnus-newsgroup-headers (list header)))
6136           (gnus-summary-remove-list-identifiers)))
6137       (when old-header
6138         (mail-header-set-number header (mail-header-number old-header)))
6139       (setq gnus-newsgroup-sparse
6140             (delq (setq number (mail-header-number header))
6141                   gnus-newsgroup-sparse))
6142       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6143       (push number gnus-newsgroup-limit)
6144       (gnus-rebuild-thread (mail-header-id header) line)
6145       (gnus-summary-goto-subject number nil t))
6146     (when (and (numberp number)
6147                (> number 0))
6148       ;; We have to update the boundaries even if we can't fetch the
6149       ;; article if ID is a number -- so that the next `P' or `N'
6150       ;; command will fetch the previous (or next) article even
6151       ;; if the one we tried to fetch this time has been canceled.
6152       (when (> number gnus-newsgroup-end)
6153         (setq gnus-newsgroup-end number))
6154       (when (< number gnus-newsgroup-begin)
6155         (setq gnus-newsgroup-begin number))
6156       (setq gnus-newsgroup-unselected
6157             (delq number gnus-newsgroup-unselected)))
6158     ;; Report back a success?
6159     (and header (mail-header-number header))))
6160
6161 ;;; Process/prefix in the summary buffer
6162
6163 (defun gnus-summary-work-articles (n)
6164   "Return a list of articles to be worked upon.
6165 The prefix argument, the list of process marked articles, and the
6166 current article will be taken into consideration."
6167   (save-excursion
6168     (set-buffer gnus-summary-buffer)
6169     (cond
6170      (n
6171       ;; A numerical prefix has been given.
6172       (setq n (prefix-numeric-value n))
6173       (let ((backward (< n 0))
6174             (n (abs (prefix-numeric-value n)))
6175             articles article)
6176         (save-excursion
6177           (while
6178               (and (> n 0)
6179                    (push (setq article (gnus-summary-article-number))
6180                          articles)
6181                    (if backward
6182                        (gnus-summary-find-prev nil article)
6183                      (gnus-summary-find-next nil article)))
6184             (decf n)))
6185         (nreverse articles)))
6186      ((and (gnus-region-active-p) (mark))
6187       (message "region active")
6188       ;; Work on the region between point and mark.
6189       (let ((max (max (point) (mark)))
6190             articles article)
6191         (save-excursion
6192           (goto-char (min (point) (mark)))
6193           (while
6194               (and
6195                (push (setq article (gnus-summary-article-number)) articles)
6196                (gnus-summary-find-next nil article)
6197                (< (point) max)))
6198           (nreverse articles))))
6199      (gnus-newsgroup-processable
6200       ;; There are process-marked articles present.
6201       ;; Save current state.
6202       (gnus-summary-save-process-mark)
6203       ;; Return the list.
6204       (reverse gnus-newsgroup-processable))
6205      (t
6206       ;; Just return the current article.
6207       (list (gnus-summary-article-number))))))
6208
6209 (defmacro gnus-summary-iterate (arg &rest forms)
6210   "Iterate over the process/prefixed articles and do FORMS.
6211 ARG is the interactive prefix given to the command.  FORMS will be
6212 executed with point over the summary line of the articles."
6213   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6214     `(let ((,articles (gnus-summary-work-articles ,arg)))
6215        (while ,articles
6216          (gnus-summary-goto-subject (car ,articles))
6217          ,@forms
6218          (pop ,articles)))))
6219
6220 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6221 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6222
6223 (defun gnus-summary-save-process-mark ()
6224   "Push the current set of process marked articles on the stack."
6225   (interactive)
6226   (push (copy-sequence gnus-newsgroup-processable)
6227         gnus-newsgroup-process-stack))
6228
6229 (defun gnus-summary-kill-process-mark ()
6230   "Push the current set of process marked articles on the stack and unmark."
6231   (interactive)
6232   (gnus-summary-save-process-mark)
6233   (gnus-summary-unmark-all-processable))
6234
6235 (defun gnus-summary-yank-process-mark ()
6236   "Pop the last process mark state off the stack and restore it."
6237   (interactive)
6238   (unless gnus-newsgroup-process-stack
6239     (error "Empty mark stack"))
6240   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6241
6242 (defun gnus-summary-process-mark-set (set)
6243   "Make SET into the current process marked articles."
6244   (gnus-summary-unmark-all-processable)
6245   (mapc 'gnus-summary-set-process-mark set))
6246
6247 ;;; Searching and stuff
6248
6249 (defun gnus-summary-search-group (&optional backward use-level)
6250   "Search for next unread newsgroup.
6251 If optional argument BACKWARD is non-nil, search backward instead."
6252   (save-excursion
6253     (set-buffer gnus-group-buffer)
6254     (when (gnus-group-search-forward
6255            backward nil (if use-level (gnus-group-group-level) nil))
6256       (gnus-group-group-name))))
6257
6258 (defun gnus-summary-best-group (&optional exclude-group)
6259   "Find the name of the best unread group.
6260 If EXCLUDE-GROUP, do not go to this group."
6261   (with-current-buffer gnus-group-buffer
6262     (save-excursion
6263       (gnus-group-best-unread-group exclude-group))))
6264
6265 (defun gnus-summary-find-next (&optional unread article backward)
6266   (if backward
6267       (gnus-summary-find-prev unread article)
6268     (let* ((dummy (gnus-summary-article-intangible-p))
6269            (article (or article (gnus-summary-article-number)))
6270            (data (gnus-data-find-list article))
6271            result)
6272       (when (and (not dummy)
6273                  (or (not gnus-summary-check-current)
6274                      (not unread)
6275                      (not (gnus-data-unread-p (car data)))))
6276         (setq data (cdr data)))
6277       (when (setq result
6278                   (if unread
6279                       (progn
6280                         (while data
6281                           (unless (memq (gnus-data-number (car data))
6282                                         (cond
6283                                          ((eq gnus-auto-goto-ignores
6284                                               'always-undownloaded)
6285                                           gnus-newsgroup-undownloaded)
6286                                          (gnus-plugged
6287                                           nil)
6288                                          ((eq gnus-auto-goto-ignores
6289                                               'unfetched)
6290                                           gnus-newsgroup-unfetched)
6291                                          ((eq gnus-auto-goto-ignores
6292                                               'undownloaded)
6293                                           gnus-newsgroup-undownloaded)))
6294                             (when (gnus-data-unread-p (car data))
6295                               (setq result (car data)
6296                                     data nil)))
6297                           (setq data (cdr data)))
6298                         result)
6299                     (car data)))
6300         (goto-char (gnus-data-pos result))
6301         (gnus-data-number result)))))
6302
6303 (defun gnus-summary-find-prev (&optional unread article)
6304   (let* ((eobp (eobp))
6305          (article (or article (gnus-summary-article-number)))
6306          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6307          result)
6308     (when (and (not eobp)
6309                (or (not gnus-summary-check-current)
6310                    (not unread)
6311                    (not (gnus-data-unread-p (car data)))))
6312       (setq data (cdr data)))
6313     (when (setq result
6314                 (if unread
6315                     (progn
6316                       (while data
6317                         (unless (memq (gnus-data-number (car data))
6318                                       (cond
6319                                        ((eq gnus-auto-goto-ignores
6320                                             'always-undownloaded)
6321                                         gnus-newsgroup-undownloaded)
6322                                        (gnus-plugged
6323                                         nil)
6324                                        ((eq gnus-auto-goto-ignores
6325                                             'unfetched)
6326                                         gnus-newsgroup-unfetched)
6327                                        ((eq gnus-auto-goto-ignores
6328                                             'undownloaded)
6329                                         gnus-newsgroup-undownloaded)))
6330                           (when (gnus-data-unread-p (car data))
6331                             (setq result (car data)
6332                                   data nil)))
6333                         (setq data (cdr data)))
6334                       result)
6335                   (car data)))
6336       (goto-char (gnus-data-pos result))
6337       (gnus-data-number result))))
6338
6339 (defun gnus-summary-find-subject (subject &optional unread backward article)
6340   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6341          (article (or article (gnus-summary-article-number)))
6342          (articles (gnus-data-list backward))
6343          (arts (gnus-data-find-list article articles))
6344          result)
6345     (when (or (not gnus-summary-check-current)
6346               (not unread)
6347               (not (gnus-data-unread-p (car arts))))
6348       (setq arts (cdr arts)))
6349     (while arts
6350       (and (or (not unread)
6351                (gnus-data-unread-p (car arts)))
6352            (vectorp (gnus-data-header (car arts)))
6353            (gnus-subject-equal
6354             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6355            (setq result (car arts)
6356                  arts nil))
6357       (setq arts (cdr arts)))
6358     (and result
6359          (goto-char (gnus-data-pos result))
6360          (gnus-data-number result))))
6361
6362 (defun gnus-summary-search-forward (&optional unread subject backward)
6363   "Search forward for an article.
6364 If UNREAD, look for unread articles.  If SUBJECT, look for
6365 articles with that subject.  If BACKWARD, search backward instead."
6366   (cond (subject (gnus-summary-find-subject subject unread backward))
6367         (backward (gnus-summary-find-prev unread))
6368         (t (gnus-summary-find-next unread))))
6369
6370 (defun gnus-recenter (&optional n)
6371   "Center point in window and redisplay frame.
6372 Also do horizontal recentering."
6373   (interactive "P")
6374   (when (and gnus-auto-center-summary
6375              (not (eq gnus-auto-center-summary 'vertical)))
6376     (gnus-horizontal-recenter))
6377   (recenter n))
6378
6379 (defun gnus-summary-recenter ()
6380   "Center point in the summary window.
6381 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6382 displayed, no centering will be performed."
6383   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6384   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6385   (interactive)
6386   ;; The user has to want it.
6387   (when gnus-auto-center-summary
6388     (let* ((top (cond ((< (window-height) 4) 0)
6389                       ((< (window-height) 7) 1)
6390                       (t (if (numberp gnus-auto-center-summary)
6391                              gnus-auto-center-summary
6392                            (/ (1- (window-height)) 2)))))
6393            (height (1- (window-height)))
6394            (bottom (save-excursion (goto-char (point-max))
6395                                    (forward-line (- height))
6396                                    (point)))
6397            (window (get-buffer-window (current-buffer))))
6398       (when (get-buffer-window gnus-article-buffer)
6399         ;; Only do recentering when the article buffer is displayed,
6400         ;; Set the window start to either `bottom', which is the biggest
6401         ;; possible valid number, or the second line from the top,
6402         ;; whichever is the least.
6403         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6404           (if (> bottom top-pos)
6405               ;; Keep the second line from the top visible
6406               (set-window-start window top-pos t)
6407             ;; Try to keep the bottom line visible; if it's partially
6408             ;; obscured, either scroll one more line to make it fully
6409             ;; visible, or revert to using TOP-POS.
6410             (save-excursion
6411               (goto-char (point-max))
6412               (forward-line -1)
6413               (let ((last-line-start (point)))
6414                 (goto-char bottom)
6415                 (set-window-start window (point) t)
6416                 (when (not (pos-visible-in-window-p last-line-start window))
6417                   (forward-line 1)
6418                   (set-window-start window (min (point) top-pos) t)))))))
6419       ;; Do horizontal recentering while we're at it.
6420       (when (and (get-buffer-window (current-buffer) t)
6421                  (not (eq gnus-auto-center-summary 'vertical)))
6422         (let ((selected (selected-window)))
6423           (select-window (get-buffer-window (current-buffer) t))
6424           (gnus-summary-position-point)
6425           (gnus-horizontal-recenter)
6426           (select-window selected))))))
6427
6428 (defun gnus-summary-jump-to-group (newsgroup)
6429   "Move point to NEWSGROUP in group mode buffer."
6430   ;; Keep update point of group mode buffer if visible.
6431   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6432       (save-window-excursion
6433         ;; Take care of tree window mode.
6434         (when (get-buffer-window gnus-group-buffer)
6435           (pop-to-buffer gnus-group-buffer))
6436         (gnus-group-jump-to-group newsgroup))
6437     (save-excursion
6438       ;; Take care of tree window mode.
6439       (if (get-buffer-window gnus-group-buffer)
6440           (pop-to-buffer gnus-group-buffer)
6441         (set-buffer gnus-group-buffer))
6442       (gnus-group-jump-to-group newsgroup))))
6443
6444 ;; This function returns a list of article numbers based on the
6445 ;; difference between the ranges of read articles in this group and
6446 ;; the range of active articles.
6447 (defun gnus-list-of-unread-articles (group)
6448   (let* ((read (gnus-info-read (gnus-get-info group)))
6449          (active (or (gnus-active group) (gnus-activate-group group)))
6450          (last (or (cdr active)
6451                    (error "Group %s couldn't be activated " group)))
6452          first nlast unread)
6453     ;; If none are read, then all are unread.
6454     (if (not read)
6455         (setq first (car active))
6456       ;; If the range of read articles is a single range, then the
6457       ;; first unread article is the article after the last read
6458       ;; article.  Sounds logical, doesn't it?
6459       (if (and (not (listp (cdr read)))
6460                (or (< (car read) (car active))
6461                    (progn (setq read (list read))
6462                           nil)))
6463           (setq first (max (car active) (1+ (cdr read))))
6464         ;; `read' is a list of ranges.
6465         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6466                                   (caar read)))
6467                   1)
6468           (setq first (car active)))
6469         (while read
6470           (when first
6471             (while (< first nlast)
6472               (setq unread (cons first unread)
6473                     first (1+ first))))
6474           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6475           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6476           (setq read (cdr read)))))
6477     ;; And add the last unread articles.
6478     (while (<= first last)
6479       (setq unread (cons first unread)
6480             first (1+ first)))
6481     ;; Return the list of unread articles.
6482     (delq 0 (nreverse unread))))
6483
6484 (defun gnus-list-of-read-articles (group)
6485   "Return a list of unread, unticked and non-dormant articles."
6486   (let* ((info (gnus-get-info group))
6487          (marked (gnus-info-marks info))
6488          (active (gnus-active group)))
6489     (and info active
6490          (gnus-list-range-difference
6491           (gnus-list-range-difference
6492            (gnus-sorted-complement
6493             (gnus-uncompress-range active)
6494             (gnus-list-of-unread-articles group))
6495            (cdr (assq 'dormant marked)))
6496           (cdr (assq 'tick marked))))))
6497
6498 ;; This function returns a sequence of article numbers based on the
6499 ;; difference between the ranges of read articles in this group and
6500 ;; the range of active articles.
6501 (defun gnus-sequence-of-unread-articles (group)
6502   (let* ((read (gnus-info-read (gnus-get-info group)))
6503          (active (or (gnus-active group) (gnus-activate-group group)))
6504          (last (cdr active))
6505          first nlast unread)
6506     ;; If none are read, then all are unread.
6507     (if (not read)
6508         (setq first (car active))
6509       ;; If the range of read articles is a single range, then the
6510       ;; first unread article is the article after the last read
6511       ;; article.  Sounds logical, doesn't it?
6512       (if (and (not (listp (cdr read)))
6513                (or (< (car read) (car active))
6514                    (progn (setq read (list read))
6515                           nil)))
6516           (setq first (max (car active) (1+ (cdr read))))
6517         ;; `read' is a list of ranges.
6518         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6519                                   (caar read)))
6520                   1)
6521           (setq first (car active)))
6522         (while read
6523           (when first
6524             (push (cons first nlast) unread))
6525           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6526           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6527           (setq read (cdr read)))))
6528     ;; And add the last unread articles.
6529     (cond ((< first last)
6530            (push (cons first last) unread))
6531           ((= first last)
6532            (push first unread)))
6533     ;; Return the sequence of unread articles.
6534     (delq 0 (nreverse unread))))
6535
6536 ;; Various summary commands
6537
6538 (defun gnus-summary-select-article-buffer ()
6539   "Reconfigure windows to show article buffer."
6540   (interactive)
6541   (if (not (gnus-buffer-live-p gnus-article-buffer))
6542       (error "There is no article buffer for this summary buffer")
6543     (gnus-configure-windows 'article)
6544     (select-window (get-buffer-window gnus-article-buffer))))
6545
6546 (defun gnus-summary-universal-argument (arg)
6547   "Perform any operation on all articles that are process/prefixed."
6548   (interactive "P")
6549   (let ((articles (gnus-summary-work-articles arg))
6550         func article)
6551     (if (eq
6552          (setq
6553           func
6554           (key-binding
6555            (read-key-sequence
6556             (substitute-command-keys
6557              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6558          'undefined)
6559         (gnus-error 1 "Undefined key")
6560       (save-excursion
6561         (while articles
6562           (gnus-summary-goto-subject (setq article (pop articles)))
6563           (let (gnus-newsgroup-processable)
6564             (command-execute func))
6565           (gnus-summary-remove-process-mark article)))))
6566   (gnus-summary-position-point))
6567
6568 (defun gnus-summary-toggle-truncation (&optional arg)
6569   "Toggle truncation of summary lines.
6570 With ARG, turn line truncation on if ARG is positive."
6571   (interactive "P")
6572   (setq truncate-lines
6573         (if (null arg) (not truncate-lines)
6574           (> (prefix-numeric-value arg) 0)))
6575   (redraw-display))
6576
6577 (defun gnus-summary-find-for-reselect ()
6578   "Return the number of an article to stay on across a reselect.
6579 The current article is considered, then following articles, then previous
6580 articles.  An article is sought which is not cancelled and isn't a temporary
6581 insertion from another group.  If there's no such then return a dummy 0."
6582   (let (found)
6583     (dolist (rev '(nil t))
6584       (unless found      ; don't demand the reverse list if we don't need it
6585         (let ((data (gnus-data-find-list
6586                      (gnus-summary-article-number) (gnus-data-list rev))))
6587           (while (and data (not found))
6588             (if (and (< 0 (gnus-data-number (car data)))
6589                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6590                 (setq found (gnus-data-number (car data))))
6591             (setq data (cdr data))))))
6592     (or found 0)))
6593
6594 (defun gnus-summary-reselect-current-group (&optional all rescan)
6595   "Exit and then reselect the current newsgroup.
6596 The prefix argument ALL means to select all articles."
6597   (interactive "P")
6598   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6599     (error "Ephemeral groups can't be reselected"))
6600   (let ((current-subject (gnus-summary-find-for-reselect))
6601         (group gnus-newsgroup-name))
6602     (setq gnus-newsgroup-begin nil)
6603     (gnus-summary-exit nil 'leave-hidden)
6604     ;; We have to adjust the point of group mode buffer because
6605     ;; point was moved to the next unread newsgroup by exiting.
6606     (gnus-summary-jump-to-group group)
6607     (when rescan
6608       (save-excursion
6609         (save-window-excursion
6610           ;; Don't show group contents.
6611           (set-window-start (selected-window) (point-max))
6612           (gnus-group-get-new-news-this-group 1))))
6613     (gnus-group-read-group all t)
6614     (gnus-summary-goto-subject current-subject nil t)))
6615
6616 (defun gnus-summary-rescan-group (&optional all)
6617   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6618   (interactive "P")
6619   (gnus-summary-reselect-current-group all t))
6620
6621 (defun gnus-summary-update-info (&optional non-destructive)
6622   (save-excursion
6623     (let ((group gnus-newsgroup-name))
6624       (when group
6625         (when gnus-newsgroup-kill-headers
6626           (setq gnus-newsgroup-killed
6627                 (gnus-compress-sequence
6628                  (gnus-sorted-union
6629                   (gnus-list-range-intersection
6630                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6631                   gnus-newsgroup-unreads)
6632                  t)))
6633         (unless (listp (cdr gnus-newsgroup-killed))
6634           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6635         (let ((headers gnus-newsgroup-headers))
6636           ;; Set the new ranges of read articles.
6637           (with-current-buffer gnus-group-buffer
6638             (gnus-undo-force-boundary))
6639           (gnus-update-read-articles
6640            group (gnus-sorted-union
6641                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6642           ;; Set the current article marks.
6643           (let ((gnus-newsgroup-scored
6644                  (if (and (not gnus-save-score)
6645                           (not non-destructive))
6646                      nil
6647                    gnus-newsgroup-scored)))
6648             (save-excursion
6649               (gnus-update-marks)))
6650           ;; Do the cross-ref thing.
6651           (when gnus-use-cross-reference
6652             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6653           ;; Do not switch windows but change the buffer to work.
6654           (set-buffer gnus-group-buffer)
6655           (unless (gnus-ephemeral-group-p group)
6656             (gnus-group-update-group group)))))))
6657
6658 (defun gnus-summary-save-newsrc (&optional force)
6659   "Save the current number of read/marked articles in the dribble buffer.
6660 The dribble buffer will then be saved.
6661 If FORCE (the prefix), also save the .newsrc file(s)."
6662   (interactive "P")
6663   (gnus-summary-update-info t)
6664   (if force
6665       (gnus-save-newsrc-file)
6666     (gnus-dribble-save)))
6667
6668 (defun gnus-summary-exit (&optional temporary leave-hidden)
6669   "Exit reading current newsgroup, and then return to group selection mode.
6670 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6671   (interactive)
6672   (gnus-set-global-variables)
6673   (gnus-kill-save-kill-buffer)
6674   (gnus-async-halt-prefetch)
6675   (let* ((group gnus-newsgroup-name)
6676          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6677          (gnus-group-is-exiting-p t)
6678          (mode major-mode)
6679          (group-point nil)
6680          (buf (current-buffer)))
6681     (unless quit-config
6682       ;; Do adaptive scoring, and possibly save score files.
6683       (when gnus-newsgroup-adaptive
6684         (gnus-score-adaptive))
6685       (when gnus-use-scoring
6686         (gnus-score-save)))
6687     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6688     ;; If we have several article buffers, we kill them at exit.
6689     (unless gnus-single-article-buffer
6690       (gnus-kill-buffer gnus-original-article-buffer)
6691       (setq gnus-article-current nil))
6692     (when gnus-use-cache
6693       (gnus-cache-possibly-remove-articles)
6694       (gnus-cache-save-buffers))
6695     (gnus-async-prefetch-remove-group group)
6696     (when gnus-suppress-duplicates
6697       (gnus-dup-enter-articles))
6698     (when gnus-use-trees
6699       (gnus-tree-close group))
6700     (when gnus-use-cache
6701       (gnus-cache-write-active))
6702     ;; Remove entries for this group.
6703     (nnmail-purge-split-history (gnus-group-real-name group))
6704     ;; Make all changes in this group permanent.
6705     (unless quit-config
6706       (gnus-run-hooks 'gnus-exit-group-hook)
6707       (gnus-summary-update-info))
6708     (gnus-close-group group)
6709     ;; Make sure where we were, and go to next newsgroup.
6710     (set-buffer gnus-group-buffer)
6711     (unless quit-config
6712       (gnus-group-jump-to-group group))
6713     (gnus-run-hooks 'gnus-summary-exit-hook)
6714     (unless (or quit-config
6715                 ;; If this group has disappeared from the summary
6716                 ;; buffer, don't skip forwards.
6717                 (not (string= group (gnus-group-group-name))))
6718       (gnus-group-next-unread-group 1))
6719     (setq group-point (point))
6720     (if temporary
6721         nil                             ;Nothing to do.
6722       ;; If we have several article buffers, we kill them at exit.
6723       (unless gnus-single-article-buffer
6724         (gnus-kill-buffer gnus-article-buffer)
6725         (gnus-kill-buffer gnus-original-article-buffer)
6726         (setq gnus-article-current nil))
6727       (set-buffer buf)
6728       (if (not gnus-kill-summary-on-exit)
6729           (progn
6730             (gnus-deaden-summary)
6731             (setq mode nil))
6732         ;; We set all buffer-local variables to nil.  It is unclear why
6733         ;; this is needed, but if we don't, buffer-local variables are
6734         ;; not garbage-collected, it seems.  This would the lead to en
6735         ;; ever-growing Emacs.
6736         (gnus-summary-clear-local-variables)
6737         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6738           (gnus-summary-clear-local-variables))
6739         (when (get-buffer gnus-article-buffer)
6740           (bury-buffer gnus-article-buffer))
6741         ;; Return to group mode buffer.
6742         (when (eq mode 'gnus-summary-mode)
6743           (gnus-kill-buffer buf)))
6744       (setq gnus-current-select-method gnus-select-method)
6745       (set-buffer gnus-group-buffer)
6746       (if quit-config
6747           (gnus-handle-ephemeral-exit quit-config)
6748         (goto-char group-point)
6749         ;; If gnus-group-buffer is already displayed, make sure we also move
6750         ;; the cursor in the window that displays it.
6751         (let ((win (get-buffer-window (current-buffer) 0)))
6752           (if win (set-window-point win (point))))
6753         (unless leave-hidden
6754           (gnus-configure-windows 'group 'force))
6755         (unless (pos-visible-in-window-p)
6756           (forward-line (/ (static-if (featurep 'xemacs)
6757                                (window-displayed-height)
6758                              (1- (window-height)))
6759                            -2))
6760           (set-window-start (selected-window) (point))
6761           (goto-char group-point)))
6762       ;; Clear the current group name.
6763       (unless quit-config
6764         (setq gnus-newsgroup-name nil)))))
6765
6766 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6767 (defun gnus-summary-exit-no-update (&optional no-questions)
6768   "Quit reading current newsgroup without updating read article info."
6769   (interactive)
6770   (let* ((group gnus-newsgroup-name)
6771          (gnus-group-is-exiting-p t)
6772          (gnus-group-is-exiting-without-update-p t)
6773          (quit-config (gnus-group-quit-config group)))
6774     (when (or no-questions
6775               gnus-expert-user
6776               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6777       (gnus-async-halt-prefetch)
6778       (run-hooks 'gnus-summary-prepare-exit-hook)
6779       ;; If we have several article buffers, we kill them at exit.
6780       (unless gnus-single-article-buffer
6781         (gnus-kill-buffer gnus-article-buffer)
6782         (gnus-kill-buffer gnus-original-article-buffer)
6783         (setq gnus-article-current nil))
6784       (if (not gnus-kill-summary-on-exit)
6785           (gnus-deaden-summary)
6786         (gnus-close-group group)
6787         (gnus-summary-clear-local-variables)
6788         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6789           (gnus-summary-clear-local-variables))
6790         (gnus-kill-buffer gnus-summary-buffer))
6791       (unless gnus-single-article-buffer
6792         (setq gnus-article-current nil))
6793       (when gnus-use-trees
6794         (gnus-tree-close group))
6795       (gnus-async-prefetch-remove-group group)
6796       (when (get-buffer gnus-article-buffer)
6797         (bury-buffer gnus-article-buffer))
6798       ;; Return to the group buffer.
6799       (gnus-configure-windows 'group 'force)
6800       ;; Clear the current group name.
6801       (setq gnus-newsgroup-name nil)
6802       (unless (gnus-ephemeral-group-p group)
6803         (gnus-group-update-group group))
6804       (when (equal (gnus-group-group-name) group)
6805         (gnus-group-next-unread-group 1))
6806       (when quit-config
6807         (gnus-handle-ephemeral-exit quit-config)))))
6808
6809 (defun gnus-handle-ephemeral-exit (quit-config)
6810   "Handle movement when leaving an ephemeral group.
6811 The state which existed when entering the ephemeral is reset."
6812   (if (not (buffer-name (car quit-config)))
6813       (gnus-configure-windows 'group 'force)
6814     (set-buffer (car quit-config))
6815     (cond ((eq major-mode 'gnus-summary-mode)
6816            (gnus-set-global-variables))
6817           ((eq major-mode 'gnus-article-mode)
6818            (save-excursion
6819              ;; The `gnus-summary-buffer' variable may point
6820              ;; to the old summary buffer when using a single
6821              ;; article buffer.
6822              (unless (gnus-buffer-live-p gnus-summary-buffer)
6823                (set-buffer gnus-group-buffer))
6824              (set-buffer gnus-summary-buffer)
6825              (gnus-set-global-variables))))
6826     (if (or (eq (cdr quit-config) 'article)
6827             (eq (cdr quit-config) 'pick))
6828         (progn
6829           ;; The current article may be from the ephemeral group
6830           ;; thus it is best that we reload this article
6831           ;;
6832           ;; If we're exiting from a large digest, this can be
6833           ;; extremely slow.  So, it's better not to reload it. -- jh.
6834           ;;(gnus-summary-show-article)
6835           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6836               (gnus-configure-windows 'pick 'force)
6837             (gnus-configure-windows (cdr quit-config) 'force)))
6838       (gnus-configure-windows (cdr quit-config) 'force))
6839     (when (eq major-mode 'gnus-summary-mode)
6840       (gnus-summary-next-subject 1 nil t)
6841       (gnus-summary-recenter)
6842       (gnus-summary-position-point))))
6843
6844 (defun gnus-summary-preview-mime-message ()
6845   "MIME decode and play this message."
6846   (interactive)
6847   (let ((gnus-break-pages nil)
6848         (gnus-show-mime t))
6849     (gnus-summary-select-article gnus-show-all-headers t))
6850   (let ((w (get-buffer-window gnus-article-buffer)))
6851     (when w
6852       (select-window (get-buffer-window gnus-article-buffer)))))
6853
6854 ;;; Dead summaries.
6855
6856 (defvar gnus-dead-summary-mode-map nil)
6857
6858 (unless gnus-dead-summary-mode-map
6859   (setq gnus-dead-summary-mode-map (make-keymap))
6860   (suppress-keymap gnus-dead-summary-mode-map)
6861   (substitute-key-definition
6862    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6863   (dolist (key '("\C-d" "\r" "\177" [delete]))
6864     (define-key gnus-dead-summary-mode-map
6865       key 'gnus-summary-wake-up-the-dead))
6866   (dolist (key '("q" "Q"))
6867     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6868
6869 (defvar gnus-dead-summary-mode nil
6870   "Minor mode for Gnus summary buffers.")
6871
6872 (defun gnus-dead-summary-mode (&optional arg)
6873   "Minor mode for Gnus summary buffers."
6874   (interactive "P")
6875   (when (eq major-mode 'gnus-summary-mode)
6876     (make-local-variable 'gnus-dead-summary-mode)
6877     (setq gnus-dead-summary-mode
6878           (if (null arg) (not gnus-dead-summary-mode)
6879             (> (prefix-numeric-value arg) 0)))
6880     (when gnus-dead-summary-mode
6881       (add-minor-mode
6882        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6883
6884 (defun gnus-deaden-summary ()
6885   "Make the current summary buffer into a dead summary buffer."
6886   ;; Kill any previous dead summary buffer.
6887   (when (and gnus-dead-summary
6888              (buffer-name gnus-dead-summary))
6889     (with-current-buffer gnus-dead-summary
6890       (when gnus-dead-summary-mode
6891         (kill-buffer (current-buffer)))))
6892   ;; Make this the current dead summary.
6893   (setq gnus-dead-summary (current-buffer))
6894   (gnus-dead-summary-mode 1)
6895   (let ((name (buffer-name)))
6896     (when (string-match "Summary" name)
6897       (rename-buffer
6898        (concat (substring name 0 (match-beginning 0)) "Dead "
6899                (substring name (match-beginning 0)))
6900        t)
6901       (bury-buffer))))
6902
6903 (defun gnus-kill-or-deaden-summary (buffer)
6904   "Kill or deaden the summary BUFFER."
6905   (save-excursion
6906     (when (and (buffer-name buffer)
6907                (not gnus-single-article-buffer))
6908       (with-current-buffer buffer
6909         (gnus-kill-buffer gnus-article-buffer)
6910         (gnus-kill-buffer gnus-original-article-buffer)))
6911     (cond
6912      ;; Kill the buffer.
6913      (gnus-kill-summary-on-exit
6914       (when (and gnus-use-trees
6915                  (gnus-buffer-exists-p buffer))
6916         (save-excursion
6917           (set-buffer buffer)
6918           (gnus-tree-close gnus-newsgroup-name)))
6919       (gnus-kill-buffer buffer))
6920      ;; Deaden the buffer.
6921      ((gnus-buffer-exists-p buffer)
6922       (save-excursion
6923         (set-buffer buffer)
6924         (gnus-deaden-summary))))))
6925
6926 (defun gnus-summary-wake-up-the-dead (&rest args)
6927   "Wake up the dead summary buffer."
6928   (interactive)
6929   (gnus-dead-summary-mode -1)
6930   (let ((name (buffer-name)))
6931     (when (string-match "Dead " name)
6932       (rename-buffer
6933        (concat (substring name 0 (match-beginning 0))
6934                (substring name (match-end 0)))
6935        t)))
6936   (gnus-message 3 "This dead summary is now alive again"))
6937
6938 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6939 (defun gnus-summary-fetch-faq (&optional faq-dir)
6940   "Fetch the FAQ for the current group.
6941 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6942 in."
6943   (interactive
6944    (list
6945     (when current-prefix-arg
6946       (completing-read
6947        "FAQ dir: " (and (listp gnus-group-faq-directory)
6948                         (mapcar 'list
6949                                 gnus-group-faq-directory))))))
6950   (let (gnus-faq-buffer)
6951     (when (setq gnus-faq-buffer
6952                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6953       (gnus-configure-windows 'summary-faq))))
6954
6955 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6956 (defun gnus-summary-describe-group (&optional force)
6957   "Describe the current newsgroup."
6958   (interactive "P")
6959   (gnus-group-describe-group force gnus-newsgroup-name))
6960
6961 (defun gnus-summary-describe-briefly ()
6962   "Describe summary mode commands briefly."
6963   (interactive)
6964   (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")))
6965
6966 ;; Walking around group mode buffer from summary mode.
6967
6968 (defun gnus-summary-next-group (&optional no-article target-group backward)
6969   "Exit current newsgroup and then select next unread newsgroup.
6970 If prefix argument NO-ARTICLE is non-nil, no article is selected
6971 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6972 previous group instead."
6973   (interactive "P")
6974   ;; Stop pre-fetching.
6975   (gnus-async-halt-prefetch)
6976   (let ((current-group gnus-newsgroup-name)
6977         (current-buffer (current-buffer))
6978         entered)
6979     ;; First we semi-exit this group to update Xrefs and all variables.
6980     ;; We can't do a real exit, because the window conf must remain
6981     ;; the same in case the user is prompted for info, and we don't
6982     ;; want the window conf to change before that...
6983     (gnus-summary-exit t)
6984     (while (not entered)
6985       ;; Then we find what group we are supposed to enter.
6986       (set-buffer gnus-group-buffer)
6987       (gnus-group-jump-to-group current-group)
6988       (setq target-group
6989             (or target-group
6990                 (if (eq gnus-keep-same-level 'best)
6991                     (gnus-summary-best-group gnus-newsgroup-name)
6992                   (gnus-summary-search-group backward gnus-keep-same-level))))
6993       (if (not target-group)
6994           ;; There are no further groups, so we return to the group
6995           ;; buffer.
6996           (progn
6997             (gnus-message 5 "Returning to the group buffer")
6998             (setq entered t)
6999             (when (gnus-buffer-live-p current-buffer)
7000               (set-buffer current-buffer)
7001               (gnus-summary-exit))
7002             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7003         ;; We try to enter the target group.
7004         (gnus-group-jump-to-group target-group)
7005         (let ((unreads (gnus-group-group-unread)))
7006           (if (and (or (eq t unreads)
7007                        (and unreads (not (zerop unreads))))
7008                    (gnus-summary-read-group
7009                     target-group nil no-article
7010                     (and (buffer-name current-buffer) current-buffer)
7011                     nil backward))
7012               (setq entered t)
7013             (setq current-group target-group
7014                   target-group nil)))))))
7015
7016 (defun gnus-summary-prev-group (&optional no-article)
7017   "Exit current newsgroup and then select previous unread newsgroup.
7018 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7019   (interactive "P")
7020   (gnus-summary-next-group no-article nil t))
7021
7022 ;; Walking around summary lines.
7023
7024 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7025   "Go to the first subject satisfying any non-nil constraint.
7026 If UNREAD is non-nil, the article should be unread.
7027 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7028 If UNSEEN is non-nil, the article should be unseen.
7029 Returns the article selected or nil if there are no matching articles."
7030   (interactive "P")
7031   (cond
7032    ;; Empty summary.
7033    ((null gnus-newsgroup-data)
7034     (gnus-message 3 "No articles in the group")
7035     nil)
7036    ;; Pick the first article.
7037    ((not (or unread undownloaded unseen))
7038     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7039     (gnus-data-number (car gnus-newsgroup-data)))
7040    ;; Find the first unread article.
7041    (t
7042     (let ((data gnus-newsgroup-data))
7043       (while (and data
7044                   (let ((num (gnus-data-number (car data))))
7045                     (or (memq num gnus-newsgroup-unfetched)
7046                         (not (or (and unread
7047                                       (memq num gnus-newsgroup-unreads))
7048                                  (and undownloaded
7049                                       (memq num gnus-newsgroup-undownloaded))
7050                                  (and unseen
7051                                       (memq num gnus-newsgroup-unseen)))))))
7052         (setq data (cdr data)))
7053       (prog1
7054           (if data
7055               (progn
7056                 (goto-char (gnus-data-pos (car data)))
7057                 (gnus-data-number (car data)))
7058             (gnus-message 3 "No more%s articles"
7059                           (let* ((r (when unread " unread"))
7060                                  (d (when undownloaded " undownloaded"))
7061                                  (s (when unseen " unseen"))
7062                                  (l (delq nil (list r d s))))
7063                             (cond ((= 3 (length l))
7064                                    (concat r "," d ", or" s))
7065                                   ((= 2 (length l))
7066                                    (concat (car l) ", or" (cadr l)))
7067                                   ((= 1 (length l))
7068                                    (car l))
7069                                   (t
7070                                    ""))))
7071             nil
7072             )
7073         (gnus-summary-position-point))))))
7074
7075 (defun gnus-summary-next-subject (n &optional unread dont-display)
7076   "Go to next N'th summary line.
7077 If N is negative, go to the previous N'th subject line.
7078 If UNREAD is non-nil, only unread articles are selected.
7079 The difference between N and the actual number of steps taken is
7080 returned."
7081   (interactive "p")
7082   (let ((backward (< n 0))
7083         (n (abs n)))
7084     (while (and (> n 0)
7085                 (if backward
7086                     (gnus-summary-find-prev unread)
7087                   (gnus-summary-find-next unread)))
7088       (unless (zerop (setq n (1- n)))
7089         (gnus-summary-show-thread)))
7090     (when (/= 0 n)
7091       (gnus-message 7 "No more%s articles"
7092                     (if unread " unread" "")))
7093     (unless dont-display
7094       (gnus-summary-recenter)
7095       (gnus-summary-position-point))
7096     n))
7097
7098 (defun gnus-summary-next-unread-subject (n)
7099   "Go to next N'th unread summary line."
7100   (interactive "p")
7101   (gnus-summary-next-subject n t))
7102
7103 (defun gnus-summary-prev-subject (n &optional unread)
7104   "Go to previous N'th summary line.
7105 If optional argument UNREAD is non-nil, only unread article is selected."
7106   (interactive "p")
7107   (gnus-summary-next-subject (- n) unread))
7108
7109 (defun gnus-summary-prev-unread-subject (n)
7110   "Go to previous N'th unread summary line."
7111   (interactive "p")
7112   (gnus-summary-next-subject (- n) t))
7113
7114 (defun gnus-summary-goto-subjects (articles)
7115   "Insert the subject header for ARTICLES in the current buffer."
7116   (save-excursion
7117     (dolist (article articles)
7118       (gnus-summary-goto-subject article t)))
7119   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7120   (gnus-summary-position-point))
7121
7122 (defun gnus-summary-goto-subject (article &optional force silent)
7123   "Go the subject line of ARTICLE.
7124 If FORCE, also allow jumping to articles not currently shown."
7125   (interactive "nArticle number: ")
7126   (unless (numberp article)
7127     (error "Article %s is not a number" article))
7128   (let ((b (point))
7129         (data (gnus-data-find article)))
7130     ;; We read in the article if we have to.
7131     (and (not data)
7132          force
7133          (gnus-summary-insert-subject
7134           article
7135           (if (or (numberp force) (vectorp force)) force)
7136           t)
7137          (setq data (gnus-data-find article)))
7138     (goto-char b)
7139     (if (not data)
7140         (progn
7141           (unless silent
7142             (gnus-message 3 "Can't find article %d" article))
7143           nil)
7144       (let ((pt (gnus-data-pos data)))
7145         (goto-char pt)
7146         (gnus-summary-set-article-display-arrow pt))
7147       (gnus-summary-position-point)
7148       article)))
7149
7150 ;; Walking around summary lines with displaying articles.
7151
7152 (defun gnus-summary-expand-window (&optional arg)
7153   "Make the summary buffer take up the entire Emacs frame.
7154 Given a prefix, will force an `article' buffer configuration."
7155   (interactive "P")
7156   (if arg
7157       (gnus-configure-windows 'article 'force)
7158     (gnus-configure-windows 'summary 'force)))
7159
7160 (defun gnus-summary-display-article (article &optional all-header)
7161   "Display ARTICLE in article buffer."
7162   (when (gnus-buffer-live-p gnus-article-buffer)
7163     (with-current-buffer gnus-article-buffer
7164       (set-buffer-multibyte t)))
7165   (gnus-set-global-variables)
7166   (when (gnus-buffer-live-p gnus-article-buffer)
7167     (with-current-buffer gnus-article-buffer
7168       (setq gnus-article-charset gnus-newsgroup-charset)
7169       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7170       (set-buffer-multibyte t)))
7171   (if (null article)
7172       nil
7173     (prog1
7174         (if gnus-summary-display-article-function
7175             (funcall gnus-summary-display-article-function article all-header)
7176           (gnus-article-prepare article all-header))
7177       (with-current-buffer gnus-article-buffer
7178         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7179              nil))
7180       (gnus-run-hooks 'gnus-select-article-hook)
7181       (when (and gnus-current-article
7182                  (not (zerop gnus-current-article)))
7183         (gnus-summary-goto-subject gnus-current-article))
7184       (gnus-summary-recenter)
7185       (when (and gnus-use-trees gnus-show-threads)
7186         (gnus-possibly-generate-tree article)
7187         (gnus-highlight-selected-tree article))
7188       ;; Successfully display article.
7189       (gnus-article-set-window-start
7190        (cdr (assq article gnus-newsgroup-bookmarks))))))
7191
7192 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7193   "Select the current article.
7194 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7195 non-nil, the article will be re-fetched even if it already present in
7196 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7197 be displayed."
7198   ;; Make sure we are in the summary buffer to work around bbdb bug.
7199   (unless (eq major-mode 'gnus-summary-mode)
7200     (set-buffer gnus-summary-buffer))
7201   (let ((article (or article (gnus-summary-article-number)))
7202         (all-headers (not (not all-headers))) ;Must be T or NIL.
7203         gnus-summary-display-article-function)
7204     (and (not pseudo)
7205          (gnus-summary-article-pseudo-p article)
7206          (error "This is a pseudo-article"))
7207     (save-excursion
7208       (set-buffer gnus-summary-buffer)
7209       (if (or (and gnus-single-article-buffer
7210                    (or (null gnus-current-article)
7211                        (null gnus-article-current)
7212                        (null (get-buffer gnus-article-buffer))
7213                        (not (eq article (cdr gnus-article-current)))
7214                        (not (equal (car gnus-article-current)
7215                                    gnus-newsgroup-name))))
7216               (and (not gnus-single-article-buffer)
7217                    (or (null gnus-current-article)
7218                        (not (eq gnus-current-article article))))
7219               force)
7220           ;; The requested article is different from the current article.
7221           (progn
7222             (gnus-summary-display-article article all-headers)
7223             (gnus-article-set-window-start
7224              (cdr (assq article gnus-newsgroup-bookmarks)))
7225             article)
7226         'old))))
7227
7228 (defun gnus-summary-force-verify-and-decrypt ()
7229   "Display buttons for signed/encrypted parts and verify/decrypt them."
7230   (interactive)
7231   (let ((mm-verify-option 'known)
7232         (mm-decrypt-option 'known)
7233         (gnus-article-emulate-mime t)
7234         (gnus-buttonized-mime-types (append (list "multipart/signed"
7235                                                   "multipart/encrypted")
7236                                             gnus-buttonized-mime-types)))
7237     (gnus-summary-select-article nil 'force)))
7238
7239 (defun gnus-summary-set-current-mark (&optional current-mark)
7240   "Obsolete function."
7241   nil)
7242
7243 (defun gnus-summary-next-article (&optional unread subject backward push)
7244   "Select the next article.
7245 If UNREAD, only unread articles are selected.
7246 If SUBJECT, only articles with SUBJECT are selected.
7247 If BACKWARD, the previous article is selected instead of the next."
7248   (interactive "P")
7249   (cond
7250    ;; Is there such an article?
7251    ((and (gnus-summary-search-forward unread subject backward)
7252          (or (gnus-summary-display-article (gnus-summary-article-number))
7253              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7254     (gnus-summary-position-point))
7255    ;; If not, we try the first unread, if that is wanted.
7256    ((and subject
7257          gnus-auto-select-same
7258          (gnus-summary-first-unread-article))
7259     (gnus-summary-position-point)
7260     (gnus-message 6 "Wrapped"))
7261    ;; Try to get next/previous article not displayed in this group.
7262    ((and gnus-auto-extend-newsgroup
7263          (not unread) (not subject))
7264     (gnus-summary-goto-article
7265      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7266      nil (count-lines (point-min) (point))))
7267    ;; Go to next/previous group.
7268    (t
7269     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7270       (gnus-summary-jump-to-group gnus-newsgroup-name))
7271     (let ((cmd last-command-char)
7272           (point
7273            (with-current-buffer gnus-group-buffer
7274              (point)))
7275           (group
7276            (if (eq gnus-keep-same-level 'best)
7277                (gnus-summary-best-group gnus-newsgroup-name)
7278              (gnus-summary-search-group backward gnus-keep-same-level))))
7279       ;; For some reason, the group window gets selected.  We change
7280       ;; it back.
7281       (select-window (get-buffer-window (current-buffer)))
7282       ;; Select next unread newsgroup automagically.
7283       (cond
7284        ((or (not gnus-auto-select-next)
7285             (not cmd))
7286         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7287        ((or (eq gnus-auto-select-next 'quietly)
7288             (and (eq gnus-auto-select-next 'slightly-quietly)
7289                  push)
7290             (and (eq gnus-auto-select-next 'almost-quietly)
7291                  (gnus-summary-last-article-p)))
7292         ;; Select quietly.
7293         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7294             (gnus-summary-exit)
7295           (gnus-message 7 "No more%s articles (%s)..."
7296                         (if unread " unread" "")
7297                         (if group (concat "selecting " group)
7298                           "exiting"))
7299           (gnus-summary-next-group nil group backward)))
7300        (t
7301         (when (gnus-key-press-event-p last-input-event)
7302           (gnus-summary-walk-group-buffer
7303            gnus-newsgroup-name cmd unread backward point))))))))
7304
7305 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7306   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7307                       (?\C-p (gnus-group-prev-unread-group 1))))
7308         (cursor-in-echo-area t)
7309         keve key group ended prompt)
7310     (save-excursion
7311       (set-buffer gnus-group-buffer)
7312       (goto-char start)
7313       (setq group
7314             (if (eq gnus-keep-same-level 'best)
7315                 (gnus-summary-best-group gnus-newsgroup-name)
7316               (gnus-summary-search-group backward gnus-keep-same-level))))
7317     (while (not ended)
7318       (setq prompt
7319             (format
7320              "No more%s articles%s " (if unread " unread" "")
7321              (if (and group
7322                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7323                  (format " (Type %s for %s [%s])"
7324                          (single-key-description cmd)
7325                          (gnus-group-decoded-name group)
7326                          (gnus-group-unread group))
7327                (format " (Type %s to exit %s)"
7328                        (single-key-description cmd)
7329                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7330       ;; Confirm auto selection.
7331       (setq key (car (setq keve (gnus-read-event-char prompt)))
7332             ended t)
7333       (cond
7334        ((assq key keystrokes)
7335         (let ((obuf (current-buffer)))
7336           (switch-to-buffer gnus-group-buffer)
7337           (when group
7338             (gnus-group-jump-to-group group))
7339           (eval (cadr (assq key keystrokes)))
7340           (setq group (gnus-group-group-name))
7341           (switch-to-buffer obuf))
7342         (setq ended nil))
7343        ((equal key cmd)
7344         (if (or (not group)
7345                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7346             (gnus-summary-exit)
7347           (gnus-summary-next-group nil group backward)))
7348        (t
7349         (push (cdr keve) unread-command-events))))))
7350
7351 (defun gnus-summary-next-unread-article ()
7352   "Select unread article after current one."
7353   (interactive)
7354   (gnus-summary-next-article
7355    (or (not (eq gnus-summary-goto-unread 'never))
7356        (gnus-summary-last-article-p (gnus-summary-article-number)))
7357    (and gnus-auto-select-same
7358         (gnus-summary-article-subject))))
7359
7360 (defun gnus-summary-prev-article (&optional unread subject)
7361   "Select the article after the current one.
7362 If UNREAD is non-nil, only unread articles are selected."
7363   (interactive "P")
7364   (gnus-summary-next-article unread subject t))
7365
7366 (defun gnus-summary-prev-unread-article ()
7367   "Select unread article before current one."
7368   (interactive)
7369   (gnus-summary-prev-article
7370    (or (not (eq gnus-summary-goto-unread 'never))
7371        (gnus-summary-first-article-p (gnus-summary-article-number)))
7372    (and gnus-auto-select-same
7373         (gnus-summary-article-subject))))
7374
7375 (defun gnus-summary-next-page (&optional lines circular stop)
7376   "Show next page of the selected article.
7377 If at the end of the current article, select the next article.
7378 LINES says how many lines should be scrolled up.
7379
7380 If CIRCULAR is non-nil, go to the start of the article instead of
7381 selecting the next article when reaching the end of the current
7382 article.
7383
7384 If STOP is non-nil, just stop when reaching the end of the message.
7385
7386 Also see the variable `gnus-article-skip-boring'."
7387   (interactive "P")
7388   (setq gnus-summary-buffer (current-buffer))
7389   (gnus-set-global-variables)
7390   (let ((article (gnus-summary-article-number))
7391         (article-window (get-buffer-window gnus-article-buffer t))
7392         endp)
7393     ;; If the buffer is empty, we have no article.
7394     (unless article
7395       (error "No article to select"))
7396     (gnus-configure-windows 'article)
7397     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7398         (if (and (eq gnus-summary-goto-unread 'never)
7399                  (not (gnus-summary-last-article-p article)))
7400             (gnus-summary-next-article)
7401           (gnus-summary-next-unread-article))
7402       (if (or (null gnus-current-article)
7403               (null gnus-article-current)
7404               (/= article (cdr gnus-article-current))
7405               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7406           ;; Selected subject is different from current article's.
7407           (gnus-summary-display-article article)
7408         (when article-window
7409           (gnus-eval-in-buffer-window gnus-article-buffer
7410             (setq endp (or (gnus-article-next-page lines)
7411                            (gnus-article-only-boring-p))))
7412           (when endp
7413             (cond (stop
7414                    (gnus-message 3 "End of message"))
7415                   (circular
7416                    (gnus-summary-beginning-of-article))
7417                   (lines
7418                    (gnus-message 3 "End of message"))
7419                   ((null lines)
7420                    (if (and (eq gnus-summary-goto-unread 'never)
7421                             (not (gnus-summary-last-article-p article)))
7422                        (gnus-summary-next-article)
7423                      (gnus-summary-next-unread-article))))))))
7424     (gnus-summary-recenter)
7425     (gnus-summary-position-point)))
7426
7427 (defun gnus-summary-prev-page (&optional lines move)
7428   "Show previous page of selected article.
7429 Argument LINES specifies lines to be scrolled down.
7430 If MOVE, move to the previous unread article if point is at
7431 the beginning of the buffer."
7432   (interactive "P")
7433   (let ((article (gnus-summary-article-number))
7434         (article-window (get-buffer-window gnus-article-buffer t))
7435         endp)
7436     (gnus-configure-windows 'article)
7437     (if (or (null gnus-current-article)
7438             (null gnus-article-current)
7439             (/= article (cdr gnus-article-current))
7440             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7441         ;; Selected subject is different from current article's.
7442         (gnus-summary-display-article article)
7443       (gnus-summary-recenter)
7444       (when article-window
7445         (gnus-eval-in-buffer-window gnus-article-buffer
7446           (setq endp (gnus-article-prev-page lines)))
7447         (when (and move endp)
7448           (cond (lines
7449                  (gnus-message 3 "Beginning of message"))
7450                 ((null lines)
7451                  (if (and (eq gnus-summary-goto-unread 'never)
7452                           (not (gnus-summary-first-article-p article)))
7453                      (gnus-summary-prev-article)
7454                    (gnus-summary-prev-unread-article))))))))
7455   (gnus-summary-position-point))
7456
7457 (defun gnus-summary-prev-page-or-article (&optional lines)
7458   "Show previous page of selected article.
7459 Argument LINES specifies lines to be scrolled down.
7460 If at the beginning of the article, go to the next article."
7461   (interactive "P")
7462   (gnus-summary-prev-page lines t))
7463
7464 (defun gnus-summary-scroll-up (lines)
7465   "Scroll up (or down) one line current article.
7466 Argument LINES specifies lines to be scrolled up (or down if negative)."
7467   (interactive "p")
7468   (gnus-configure-windows 'article)
7469   (gnus-summary-show-thread)
7470   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7471     (gnus-eval-in-buffer-window gnus-article-buffer
7472       (cond ((> lines 0)
7473              (when (gnus-article-next-page lines)
7474                (gnus-message 3 "End of message")))
7475             ((< lines 0)
7476              (gnus-article-prev-page (- lines))))))
7477   (gnus-summary-recenter)
7478   (gnus-summary-position-point))
7479
7480 (defun gnus-summary-scroll-down (lines)
7481   "Scroll down (or up) one line current article.
7482 Argument LINES specifies lines to be scrolled down (or up if negative)."
7483   (interactive "p")
7484   (gnus-summary-scroll-up (- lines)))
7485
7486 (defun gnus-summary-next-same-subject ()
7487   "Select next article which has the same subject as current one."
7488   (interactive)
7489   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7490
7491 (defun gnus-summary-prev-same-subject ()
7492   "Select previous article which has the same subject as current one."
7493   (interactive)
7494   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7495
7496 (defun gnus-summary-next-unread-same-subject ()
7497   "Select next unread article which has the same subject as current one."
7498   (interactive)
7499   (gnus-summary-next-article t (gnus-summary-article-subject)))
7500
7501 (defun gnus-summary-prev-unread-same-subject ()
7502   "Select previous unread article which has the same subject as current one."
7503   (interactive)
7504   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7505
7506 (defun gnus-summary-first-unread-article ()
7507   "Select the first unread article.
7508 Return nil if there are no unread articles."
7509   (interactive)
7510   (prog1
7511       (when (gnus-summary-first-subject t)
7512         (gnus-summary-show-thread)
7513         (gnus-summary-first-subject t)
7514         (gnus-summary-display-article (gnus-summary-article-number)))
7515     (gnus-summary-position-point)))
7516
7517 (defun gnus-summary-first-unread-subject ()
7518   "Place the point on the subject line of the first unread article.
7519 Return nil if there are no unread articles."
7520   (interactive)
7521   (prog1
7522       (when (gnus-summary-first-subject t)
7523         (gnus-summary-show-thread)
7524         (gnus-summary-first-subject t))
7525     (gnus-summary-position-point)))
7526
7527 (defun gnus-summary-first-unseen-subject ()
7528   "Place the point on the subject line of the first unseen article.
7529 Return nil if there are no unseen articles."
7530   (interactive)
7531   (prog1
7532       (when (gnus-summary-first-subject nil nil t)
7533         (gnus-summary-show-thread)
7534         (gnus-summary-first-subject nil nil t))
7535     (gnus-summary-position-point)))
7536
7537 (defun gnus-summary-first-unseen-or-unread-subject ()
7538   "Place the point on the subject line of the first unseen article or,
7539 if all article have been seen, on the subject line of the first unread
7540 article."
7541   (interactive)
7542   (prog1
7543       (unless (when (gnus-summary-first-subject nil nil t)
7544                 (gnus-summary-show-thread)
7545                 (gnus-summary-first-subject nil nil t))
7546         (when (gnus-summary-first-subject t)
7547           (gnus-summary-show-thread)
7548           (gnus-summary-first-subject t)))
7549     (gnus-summary-position-point)))
7550
7551 (defun gnus-summary-first-article ()
7552   "Select the first article.
7553 Return nil if there are no articles."
7554   (interactive)
7555   (prog1
7556       (when (gnus-summary-first-subject)
7557         (gnus-summary-show-thread)
7558         (gnus-summary-first-subject)
7559         (gnus-summary-display-article (gnus-summary-article-number)))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-best-unread-article (&optional arg)
7563   "Select the unread article with the highest score.
7564 If given a prefix argument, select the next unread article that has a
7565 score higher than the default score."
7566   (interactive "P")
7567   (let ((article (if arg
7568                      (gnus-summary-better-unread-subject)
7569                    (gnus-summary-best-unread-subject))))
7570     (if article
7571         (gnus-summary-goto-article article)
7572       (error "No unread articles"))))
7573
7574 (defun gnus-summary-best-unread-subject ()
7575   "Select the unread subject with the highest score."
7576   (interactive)
7577   (let ((best -1000000)
7578         (data gnus-newsgroup-data)
7579         article score)
7580     (while data
7581       (and (gnus-data-unread-p (car data))
7582            (> (setq score
7583                     (gnus-summary-article-score (gnus-data-number (car data))))
7584               best)
7585            (setq best score
7586                  article (gnus-data-number (car data))))
7587       (setq data (cdr data)))
7588     (when article
7589       (gnus-summary-goto-subject article))
7590     (gnus-summary-position-point)
7591     article))
7592
7593 (defun gnus-summary-better-unread-subject ()
7594   "Select the first unread subject that has a score over the default score."
7595   (interactive)
7596   (let ((data gnus-newsgroup-data)
7597         article score)
7598     (while (and (setq article (gnus-data-number (car data)))
7599                 (or (gnus-data-read-p (car data))
7600                     (not (> (gnus-summary-article-score article)
7601                             gnus-summary-default-score))))
7602       (setq data (cdr data)))
7603     (when article
7604       (gnus-summary-goto-subject article))
7605     (gnus-summary-position-point)
7606     article))
7607
7608 (defun gnus-summary-last-subject ()
7609   "Go to the last displayed subject line in the group."
7610   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7611     (when article
7612       (gnus-summary-goto-subject article))))
7613
7614 (defun gnus-summary-goto-article (article &optional all-headers force)
7615   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7616 If ALL-HEADERS is non-nil, no header lines are hidden.
7617 If FORCE, go to the article even if it isn't displayed.  If FORCE
7618 is a number, it is the line the article is to be displayed on."
7619   (interactive
7620    (list
7621     (completing-read
7622      "Article number or Message-ID: "
7623      (mapcar (lambda (number) (list (int-to-string number)))
7624              gnus-newsgroup-limit))
7625     current-prefix-arg
7626     t))
7627   (prog1
7628       (if (and (stringp article)
7629                (string-match "@\\|%40" article))
7630           (gnus-summary-refer-article article)
7631         (when (stringp article)
7632           (setq article (string-to-number article)))
7633         (if (gnus-summary-goto-subject article force)
7634             (gnus-summary-display-article article all-headers)
7635           (gnus-message 4 "Couldn't go to article %s" article) nil))
7636     (gnus-summary-position-point)))
7637
7638 (defun gnus-summary-goto-last-article ()
7639   "Go to the previously read article."
7640   (interactive)
7641   (prog1
7642       (when gnus-last-article
7643         (gnus-summary-goto-article gnus-last-article nil t))
7644     (gnus-summary-position-point)))
7645
7646 (defun gnus-summary-pop-article (number)
7647   "Pop one article off the history and go to the previous.
7648 NUMBER articles will be popped off."
7649   (interactive "p")
7650   (let (to)
7651     (setq gnus-newsgroup-history
7652           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7653     (if to
7654         (gnus-summary-goto-article (car to) nil t)
7655       (error "Article history empty")))
7656   (gnus-summary-position-point))
7657
7658 ;; Summary commands and functions for limiting the summary buffer.
7659
7660 (defun gnus-summary-limit-to-articles (n)
7661   "Limit the summary buffer to the next N articles.
7662 If not given a prefix, use the process marked articles instead."
7663   (interactive "P")
7664   (prog1
7665       (let ((articles (gnus-summary-work-articles n)))
7666         (setq gnus-newsgroup-processable nil)
7667         (gnus-summary-limit articles))
7668     (gnus-summary-position-point)))
7669
7670 (defun gnus-summary-pop-limit (&optional total)
7671   "Restore the previous limit.
7672 If given a prefix, remove all limits."
7673   (interactive "P")
7674   (when total
7675     (setq gnus-newsgroup-limits
7676           (list (mapcar (lambda (h) (mail-header-number h))
7677                         gnus-newsgroup-headers))))
7678   (unless gnus-newsgroup-limits
7679     (error "No limit to pop"))
7680   (prog1
7681       (gnus-summary-limit nil 'pop)
7682     (gnus-summary-position-point)))
7683
7684 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7685   "Limit the summary buffer to articles that have subjects that match a regexp.
7686 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7687   (interactive
7688    (list (read-string (if current-prefix-arg
7689                           "Exclude subject (regexp): "
7690                         "Limit to subject (regexp): "))
7691          nil current-prefix-arg))
7692   (unless header
7693     (setq header "subject"))
7694   (when (not (equal "" subject))
7695     (prog1
7696         (let ((articles (gnus-summary-find-matching
7697                          (or header "subject") subject 'all nil nil
7698                          not-matching)))
7699           (unless articles
7700             (error "Found no matches for \"%s\"" subject))
7701           (gnus-summary-limit articles))
7702       (gnus-summary-position-point))))
7703
7704 (defun gnus-summary-limit-to-author (from &optional not-matching)
7705   "Limit the summary buffer to articles that have authors that match a regexp.
7706 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7707   (interactive
7708    (list (read-string (if current-prefix-arg
7709                           "Exclude author (regexp): "
7710                         "Limit to author (regexp): "))
7711          current-prefix-arg))
7712   (gnus-summary-limit-to-subject from "from" not-matching))
7713
7714 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7715   "Limit the summary buffer to articles with the given RECIPIENT.
7716
7717 If NOT-MATCHING, exclude RECIPIENT.
7718
7719 To and Cc headers are checked.  You need to include them in
7720 `nnmail-extra-headers'."
7721   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7722   (interactive
7723    (list (read-string (format "%s recipient (regexp): "
7724                               (if current-prefix-arg "Exclude" "Limit to")))
7725          current-prefix-arg))
7726   (when (not (equal "" recipient))
7727     (prog1 (let* ((to
7728                    (if (memq 'To nnmail-extra-headers)
7729                        (gnus-summary-find-matching
7730                         (cons 'extra 'To) recipient 'all nil nil
7731                         not-matching)
7732                      (gnus-message
7733                       1 "`To' isn't present in `nnmail-extra-headers'")
7734                      (sit-for 1)
7735                      nil))
7736                   (cc
7737                    (if (memq 'Cc nnmail-extra-headers)
7738                        (gnus-summary-find-matching
7739                         (cons 'extra 'Cc) recipient 'all nil nil
7740                         not-matching)
7741                      (gnus-message
7742                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7743                      (sit-for 1)
7744                      nil))
7745                   (articles
7746                    (if not-matching
7747                        ;; We need the numbers that are in both lists:
7748                        (mapcar (lambda (a)
7749                                  (and (memq a to) a))
7750                                cc)
7751                      (nconc to cc))))
7752              (unless articles
7753                (error "Found no matches for \"%s\"" recipient))
7754              (gnus-summary-limit articles))
7755       (gnus-summary-position-point))))
7756
7757 (defun gnus-summary-limit-to-age (age &optional younger-p)
7758   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7759 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7760 articles that are younger than AGE days."
7761   (interactive
7762    (let ((younger current-prefix-arg)
7763          (days-got nil)
7764          days)
7765      (while (not days-got)
7766        (setq days (if younger
7767                       (read-string "Limit to articles younger than (in days, older when negative): ")
7768                     (read-string
7769                      "Limit to articles older than (in days, younger when negative): ")))
7770        (when (> (length days) 0)
7771          (setq days (read days)))
7772        (if (numberp days)
7773            (progn
7774              (setq days-got t)
7775              (when (< days 0)
7776                (setq younger (not younger))
7777                (setq days (* days -1))))
7778          (message "Please enter a number.")
7779          (sleep-for 1)))
7780      (list days younger)))
7781   (prog1
7782       (let ((data gnus-newsgroup-data)
7783             (cutoff (days-to-time age))
7784             articles d date is-younger)
7785         (while (setq d (pop data))
7786           (when (and (vectorp (gnus-data-header d))
7787                      (setq date (mail-header-date (gnus-data-header d))))
7788             (setq is-younger (time-less-p
7789                               (time-since (condition-case ()
7790                                               (date-to-time date)
7791                                             (error '(0 0))))
7792                               cutoff))
7793             (when (if younger-p
7794                       is-younger
7795                     (not is-younger))
7796               (push (gnus-data-number d) articles))))
7797         (gnus-summary-limit (nreverse articles)))
7798     (gnus-summary-position-point)))
7799
7800 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7801   "Limit the summary buffer to articles that match an 'extra' header."
7802   (interactive
7803    (let ((header
7804           (intern
7805            (gnus-completing-read-with-default
7806             (symbol-name (car gnus-extra-headers))
7807             (if current-prefix-arg
7808                 "Exclude extra header:"
7809               "Limit extra header:")
7810             (mapcar (lambda (x)
7811                       (cons (symbol-name x) x))
7812                     gnus-extra-headers)
7813             nil
7814             t))))
7815      (list header
7816            (read-string (format "%s header %s (regexp): "
7817                                 (if current-prefix-arg "Exclude" "Limit to")
7818                                 header))
7819            current-prefix-arg)))
7820   (when (not (equal "" regexp))
7821     (prog1
7822         (let ((articles (gnus-summary-find-matching
7823                          (cons 'extra header) regexp 'all nil nil
7824                          not-matching)))
7825           (unless articles
7826             (error "Found no matches for \"%s\"" regexp))
7827           (gnus-summary-limit articles))
7828       (gnus-summary-position-point))))
7829
7830 (defun gnus-summary-limit-to-display-predicate ()
7831   "Limit the summary buffer to the predicated in the `display' group parameter."
7832   (interactive)
7833   (unless gnus-newsgroup-display
7834     (error "There is no `display' group parameter"))
7835   (let (articles)
7836     (dolist (number gnus-newsgroup-articles)
7837       (when (funcall gnus-newsgroup-display)
7838         (push number articles)))
7839     (gnus-summary-limit articles))
7840   (gnus-summary-position-point))
7841
7842 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7843 (make-obsolete
7844  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7845
7846 (defun gnus-summary-limit-to-unread (&optional all)
7847   "Limit the summary buffer to articles that are not marked as read.
7848 If ALL is non-nil, limit strictly to unread articles."
7849   (interactive "P")
7850   (if all
7851       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7852     (gnus-summary-limit-to-marks
7853      ;; Concat all the marks that say that an article is read and have
7854      ;; those removed.
7855      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7856            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7857            gnus-low-score-mark gnus-expirable-mark
7858            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7859            gnus-duplicate-mark gnus-souped-mark)
7860      'reverse)))
7861
7862 (defun gnus-summary-limit-to-replied (&optional unreplied)
7863   "Limit the summary buffer to replied articles.
7864 If UNREPLIED (the prefix), limit to unreplied articles."
7865   (interactive "P")
7866   (if unreplied
7867       (gnus-summary-limit
7868        (gnus-set-difference gnus-newsgroup-articles
7869         gnus-newsgroup-replied))
7870     (gnus-summary-limit gnus-newsgroup-replied))
7871   (gnus-summary-position-point))
7872
7873 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7874 (make-obsolete 'gnus-summary-delete-marked-with
7875                'gnus-summary-limit-exclude-marks)
7876
7877 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7878   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7879 If REVERSE, limit the summary buffer to articles that are marked
7880 with MARKS.  MARKS can either be a string of marks or a list of marks.
7881 Returns how many articles were removed."
7882   (interactive "sMarks: ")
7883   (gnus-summary-limit-to-marks marks t))
7884
7885 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7886   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7887 If REVERSE (the prefix), limit the summary buffer to articles that are
7888 not marked with MARKS.  MARKS can either be a string of marks or a
7889 list of marks.
7890 Returns how many articles were removed."
7891   (interactive "sMarks: \nP")
7892   (prog1
7893       (let ((data gnus-newsgroup-data)
7894             (marks (if (listp marks) marks
7895                      (append marks nil))) ; Transform to list.
7896             articles)
7897         (while data
7898           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7899                   (memq (gnus-data-mark (car data)) marks))
7900             (push (gnus-data-number (car data)) articles))
7901           (setq data (cdr data)))
7902         (gnus-summary-limit articles))
7903     (gnus-summary-position-point)))
7904
7905 (defun gnus-summary-limit-to-score (score)
7906   "Limit to articles with score at or above SCORE."
7907   (interactive "NLimit to articles with score of at least: ")
7908   (let ((data gnus-newsgroup-data)
7909         articles)
7910     (while data
7911       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7912                 score)
7913         (push (gnus-data-number (car data)) articles))
7914       (setq data (cdr data)))
7915     (prog1
7916         (gnus-summary-limit articles)
7917       (gnus-summary-position-point))))
7918
7919 (defun gnus-summary-limit-to-unseen ()
7920   "Limit to unseen articles."
7921   (interactive)
7922   (prog1
7923       (gnus-summary-limit gnus-newsgroup-unseen)
7924     (gnus-summary-position-point)))
7925
7926 (defun gnus-summary-limit-include-thread (id)
7927   "Display all the hidden articles that is in the thread with ID in it.
7928 When called interactively, ID is the Message-ID of the current
7929 article."
7930   (interactive (list (mail-header-id (gnus-summary-article-header))))
7931   (let ((articles (gnus-articles-in-thread
7932                    (gnus-id-to-thread (gnus-root-id id)))))
7933     (prog1
7934         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7935       (gnus-summary-limit-include-matching-articles
7936        "subject"
7937        (regexp-quote (gnus-simplify-subject-re
7938                       (mail-header-subject (gnus-id-to-header id)))))
7939       (gnus-summary-position-point))))
7940
7941 (defun gnus-summary-limit-include-matching-articles (header regexp)
7942   "Display all the hidden articles that have HEADERs that match REGEXP."
7943   (interactive (list (read-string "Match on header: ")
7944                      (read-string "Regexp: ")))
7945   (let ((articles (gnus-find-matching-articles header regexp)))
7946     (prog1
7947         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7948       (gnus-summary-position-point))))
7949
7950 (defun gnus-summary-insert-dormant-articles ()
7951   "Insert all the dormant articles for this group into the current buffer."
7952   (interactive)
7953   (let ((gnus-verbose (max 6 gnus-verbose)))
7954     (if (not gnus-newsgroup-dormant)
7955         (gnus-message 3 "No cached articles for this group")
7956       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7957
7958 (defun gnus-summary-limit-include-dormant ()
7959   "Display all the hidden articles that are marked as dormant.
7960 Note that this command only works on a subset of the articles currently
7961 fetched for this group."
7962   (interactive)
7963   (unless gnus-newsgroup-dormant
7964     (error "There are no dormant articles in this group"))
7965   (prog1
7966       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7967     (gnus-summary-position-point)))
7968
7969 (defun gnus-summary-limit-exclude-dormant ()
7970   "Hide all dormant articles."
7971   (interactive)
7972   (prog1
7973       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7974     (gnus-summary-position-point)))
7975
7976 (defun gnus-summary-limit-exclude-childless-dormant ()
7977   "Hide all dormant articles that have no children."
7978   (interactive)
7979   (let ((data (gnus-data-list t))
7980         articles d children)
7981     ;; Find all articles that are either not dormant or have
7982     ;; children.
7983     (while (setq d (pop data))
7984       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7985                 (and (setq children
7986                            (gnus-article-children (gnus-data-number d)))
7987                      (let (found)
7988                        (while children
7989                          (when (memq (car children) articles)
7990                            (setq children nil
7991                                  found t))
7992                          (pop children))
7993                        found)))
7994         (push (gnus-data-number d) articles)))
7995     ;; Do the limiting.
7996     (prog1
7997         (gnus-summary-limit articles)
7998       (gnus-summary-position-point))))
7999
8000 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8001   "Mark all unread excluded articles as read.
8002 If ALL, mark even excluded ticked and dormants as read."
8003   (interactive "P")
8004   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8005   (let ((articles (gnus-sorted-ndifference
8006                    (sort
8007                     (mapcar (lambda (h) (mail-header-number h))
8008                             gnus-newsgroup-headers)
8009                     '<)
8010                    gnus-newsgroup-limit))
8011         article)
8012     (setq gnus-newsgroup-unreads
8013           (gnus-sorted-intersection gnus-newsgroup-unreads
8014                                     gnus-newsgroup-limit))
8015     (if all
8016         (setq gnus-newsgroup-dormant nil
8017               gnus-newsgroup-marked nil
8018               gnus-newsgroup-reads
8019               (nconc
8020                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8021                gnus-newsgroup-reads))
8022       (while (setq article (pop articles))
8023         (unless (or (memq article gnus-newsgroup-dormant)
8024                     (memq article gnus-newsgroup-marked))
8025           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8026
8027 (defun gnus-summary-limit (articles &optional pop)
8028   (if pop
8029       ;; We pop the previous limit off the stack and use that.
8030       (setq articles (car gnus-newsgroup-limits)
8031             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8032     ;; We use the new limit, so we push the old limit on the stack.
8033     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8034   ;; Set the limit.
8035   (setq gnus-newsgroup-limit articles)
8036   (let ((total (length gnus-newsgroup-data))
8037         (data (gnus-data-find-list (gnus-summary-article-number)))
8038         (gnus-summary-mark-below nil)   ; Inhibit this.
8039         found)
8040     ;; This will do all the work of generating the new summary buffer
8041     ;; according to the new limit.
8042     (gnus-summary-prepare)
8043     ;; Hide any threads, possibly.
8044     (gnus-summary-maybe-hide-threads)
8045     ;; Try to return to the article you were at, or one in the
8046     ;; neighborhood.
8047     (when data
8048       ;; We try to find some article after the current one.
8049       (while data
8050         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8051           (setq data nil
8052                 found t))
8053         (setq data (cdr data))))
8054     (unless found
8055       ;; If there is no data, that means that we were after the last
8056       ;; article.  The same goes when we can't find any articles
8057       ;; after the current one.
8058       (goto-char (point-max))
8059       (gnus-summary-find-prev))
8060     (gnus-set-mode-line 'summary)
8061     ;; We return how many articles were removed from the summary
8062     ;; buffer as a result of the new limit.
8063     (- total (length gnus-newsgroup-data))))
8064
8065 (defsubst gnus-invisible-cut-children (threads)
8066   (let ((num 0))
8067     (while threads
8068       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8069         (incf num))
8070       (pop threads))
8071     (< num 2)))
8072
8073 (defsubst gnus-cut-thread (thread)
8074   "Go forwards in the thread until we find an article that we want to display."
8075   (when (or (eq gnus-fetch-old-headers 'some)
8076             (eq gnus-fetch-old-headers 'invisible)
8077             (numberp gnus-fetch-old-headers)
8078             (eq gnus-build-sparse-threads 'some)
8079             (eq gnus-build-sparse-threads 'more))
8080     ;; Deal with old-fetched headers and sparse threads.
8081     (while (and
8082             thread
8083             (or
8084              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8085              (gnus-summary-article-ancient-p
8086               (mail-header-number (car thread))))
8087             (if (or (<= (length (cdr thread)) 1)
8088                     (eq gnus-fetch-old-headers 'invisible))
8089                 (setq gnus-newsgroup-limit
8090                       (delq (mail-header-number (car thread))
8091                             gnus-newsgroup-limit)
8092                       thread (cadr thread))
8093               (when (gnus-invisible-cut-children (cdr thread))
8094                 (let ((th (cdr thread)))
8095                   (while th
8096                     (if (memq (mail-header-number (caar th))
8097                               gnus-newsgroup-limit)
8098                         (setq thread (car th)
8099                               th nil)
8100                       (setq th (cdr th))))))))))
8101   thread)
8102
8103 (defun gnus-cut-threads (threads)
8104   "Cut off all uninteresting articles from the beginning of THREADS."
8105   (when (or (eq gnus-fetch-old-headers 'some)
8106             (eq gnus-fetch-old-headers 'invisible)
8107             (numberp gnus-fetch-old-headers)
8108             (eq gnus-build-sparse-threads 'some)
8109             (eq gnus-build-sparse-threads 'more))
8110     (let ((th threads))
8111       (while th
8112         (setcar th (gnus-cut-thread (car th)))
8113         (setq th (cdr th)))))
8114   ;; Remove nixed out threads.
8115   (delq nil threads))
8116
8117 (defun gnus-summary-initial-limit (&optional show-if-empty)
8118   "Figure out what the initial limit is supposed to be on group entry.
8119 This entails weeding out unwanted dormants, low-scored articles,
8120 fetch-old-headers verbiage, and so on."
8121   ;; Most groups have nothing to remove.
8122   (if (or gnus-inhibit-limiting
8123           (and (null gnus-newsgroup-dormant)
8124                (eq gnus-newsgroup-display 'gnus-not-ignore)
8125                (not (eq gnus-fetch-old-headers 'some))
8126                (not (numberp gnus-fetch-old-headers))
8127                (not (eq gnus-fetch-old-headers 'invisible))
8128                (null gnus-summary-expunge-below)
8129                (not (eq gnus-build-sparse-threads 'some))
8130                (not (eq gnus-build-sparse-threads 'more))
8131                (null gnus-thread-expunge-below)
8132                (not gnus-use-nocem)))
8133       ()                                ; Do nothing.
8134     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8135     (setq gnus-newsgroup-limit nil)
8136     (mapatoms
8137      (lambda (node)
8138        (unless (car (symbol-value node))
8139          ;; These threads have no parents -- they are roots.
8140          (let ((nodes (cdr (symbol-value node)))
8141                thread)
8142            (while nodes
8143              (if (and gnus-thread-expunge-below
8144                       (< (gnus-thread-total-score (car nodes))
8145                          gnus-thread-expunge-below))
8146                  (gnus-expunge-thread (pop nodes))
8147                (setq thread (pop nodes))
8148                (gnus-summary-limit-children thread))))))
8149      gnus-newsgroup-dependencies)
8150     ;; If this limitation resulted in an empty group, we might
8151     ;; pop the previous limit and use it instead.
8152     (when (and (not gnus-newsgroup-limit)
8153                show-if-empty)
8154       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8155     gnus-newsgroup-limit))
8156
8157 (defun gnus-summary-limit-children (thread)
8158   "Return 1 if this subthread is visible and 0 if it is not."
8159   ;; First we get the number of visible children to this thread.  This
8160   ;; is done by recursing down the thread using this function, so this
8161   ;; will really go down to a leaf article first, before slowly
8162   ;; working its way up towards the root.
8163   (when thread
8164     (let* ((max-lisp-eval-depth 5000)
8165            (children
8166            (if (cdr thread)
8167                (apply '+ (mapcar 'gnus-summary-limit-children
8168                                  (cdr thread)))
8169              0))
8170           (number (mail-header-number (car thread)))
8171           score)
8172       (if (and
8173            (not (memq number gnus-newsgroup-marked))
8174            (or
8175             ;; If this article is dormant and has absolutely no visible
8176             ;; children, then this article isn't visible.
8177             (and (memq number gnus-newsgroup-dormant)
8178                  (zerop children))
8179             ;; If this is "fetch-old-headered" and there is no
8180             ;; visible children, then we don't want this article.
8181             (and (or (eq gnus-fetch-old-headers 'some)
8182                      (numberp gnus-fetch-old-headers))
8183                  (gnus-summary-article-ancient-p number)
8184                  (zerop children))
8185             ;; If this is "fetch-old-headered" and `invisible', then
8186             ;; we don't want this article.
8187             (and (eq gnus-fetch-old-headers 'invisible)
8188                  (gnus-summary-article-ancient-p number))
8189             ;; If this is a sparsely inserted article with no children,
8190             ;; we don't want it.
8191             (and (eq gnus-build-sparse-threads 'some)
8192                  (gnus-summary-article-sparse-p number)
8193                  (zerop children))
8194             ;; If we use expunging, and this article is really
8195             ;; low-scored, then we don't want this article.
8196             (when (and gnus-summary-expunge-below
8197                        (< (setq score
8198                                 (or (cdr (assq number gnus-newsgroup-scored))
8199                                     gnus-summary-default-score))
8200                           gnus-summary-expunge-below))
8201               ;; We increase the expunge-tally here, but that has
8202               ;; nothing to do with the limits, really.
8203               (incf gnus-newsgroup-expunged-tally)
8204               ;; We also mark as read here, if that's wanted.
8205               (when (and gnus-summary-mark-below
8206                          (< score gnus-summary-mark-below))
8207                 (setq gnus-newsgroup-unreads
8208                       (delq number gnus-newsgroup-unreads))
8209                 (if gnus-newsgroup-auto-expire
8210                     (push number gnus-newsgroup-expirable)
8211                   (push (cons number gnus-low-score-mark)
8212                         gnus-newsgroup-reads)))
8213               t)
8214             ;; Do the `display' group parameter.
8215             (and gnus-newsgroup-display
8216                  (not (funcall gnus-newsgroup-display)))
8217             ;; Check NoCeM things.
8218             (when (and gnus-use-nocem
8219                        (gnus-nocem-unwanted-article-p
8220                         (mail-header-id (car thread))))
8221               (setq gnus-newsgroup-unreads
8222                     (delq number gnus-newsgroup-unreads))
8223               t)))
8224           ;; Nope, invisible article.
8225           0
8226         ;; Ok, this article is to be visible, so we add it to the limit
8227         ;; and return 1.
8228         (push number gnus-newsgroup-limit)
8229         1))))
8230
8231 (defun gnus-expunge-thread (thread)
8232   "Mark all articles in THREAD as read."
8233   (let* ((number (mail-header-number (car thread))))
8234     (incf gnus-newsgroup-expunged-tally)
8235     ;; We also mark as read here, if that's wanted.
8236     (setq gnus-newsgroup-unreads
8237           (delq number gnus-newsgroup-unreads))
8238     (if gnus-newsgroup-auto-expire
8239         (push number gnus-newsgroup-expirable)
8240       (push (cons number gnus-low-score-mark)
8241             gnus-newsgroup-reads)))
8242   ;; Go recursively through all subthreads.
8243   (mapcar 'gnus-expunge-thread (cdr thread)))
8244
8245 ;; Summary article oriented commands
8246
8247 (defun gnus-summary-refer-parent-article (n)
8248   "Refer parent article N times.
8249 If N is negative, go to ancestor -N instead.
8250 The difference between N and the number of articles fetched is returned."
8251   (interactive "p")
8252   (let ((skip 1)
8253         error header ref)
8254     (when (not (natnump n))
8255       (setq skip (abs n)
8256             n 1))
8257     (while (and (> n 0)
8258                 (not error))
8259       (setq header (gnus-summary-article-header))
8260       (if (and (eq (mail-header-number header)
8261                    (cdr gnus-article-current))
8262                (equal gnus-newsgroup-name
8263                       (car gnus-article-current)))
8264           ;; If we try to find the parent of the currently
8265           ;; displayed article, then we take a look at the actual
8266           ;; References header, since this is slightly more
8267           ;; reliable than the References field we got from the
8268           ;; server.
8269           (save-excursion
8270             (set-buffer gnus-original-article-buffer)
8271             (nnheader-narrow-to-headers)
8272             (unless (setq ref (message-fetch-field "references"))
8273               (when (setq ref (message-fetch-field "in-reply-to"))
8274                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8275             (widen))
8276         (setq ref
8277               ;; It's not the current article, so we take a bet on
8278               ;; the value we got from the server.
8279               (mail-header-references header)))
8280       (if (and ref
8281                (not (equal ref "")))
8282           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8283             (gnus-message 1 "Couldn't find parent"))
8284         (gnus-message 1 "No references in article %d"
8285                       (gnus-summary-article-number))
8286         (setq error t))
8287       (decf n))
8288     (gnus-summary-position-point)
8289     n))
8290
8291 (defun gnus-summary-refer-references ()
8292   "Fetch all articles mentioned in the References header.
8293 Return the number of articles fetched."
8294   (interactive)
8295   (let ((ref (mail-header-references (gnus-summary-article-header)))
8296         (current (gnus-summary-article-number))
8297         (n 0))
8298     (if (or (not ref)
8299             (equal ref ""))
8300         (error "No References in the current article")
8301       ;; For each Message-ID in the References header...
8302       (while (string-match "<[^>]*>" ref)
8303         (incf n)
8304         ;; ... fetch that article.
8305         (gnus-summary-refer-article
8306          (prog1 (match-string 0 ref)
8307            (setq ref (substring ref (match-end 0))))))
8308       (gnus-summary-goto-subject current)
8309       (gnus-summary-position-point)
8310       n)))
8311
8312 (defun gnus-summary-refer-thread (&optional limit)
8313   "Fetch all articles in the current thread.
8314 If LIMIT (the numerical prefix), fetch that many old headers instead
8315 of what's specified by the `gnus-refer-thread-limit' variable."
8316   (interactive "P")
8317   (let ((id (mail-header-id (gnus-summary-article-header)))
8318         (limit (if limit (prefix-numeric-value limit)
8319                  gnus-refer-thread-limit)))
8320     (unless (eq gnus-fetch-old-headers 'invisible)
8321       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8322       ;; Retrieve the headers and read them in.
8323       (if (eq (if (numberp limit)
8324                   (gnus-retrieve-headers
8325                    (list (min
8326                           (+ (mail-header-number
8327                               (gnus-summary-article-header))
8328                              limit)
8329                           gnus-newsgroup-end))
8330                    gnus-newsgroup-name (* limit 2))
8331                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8332                 ;; headers.
8333                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8334                                        gnus-newsgroup-name limit))
8335               'nov)
8336           (gnus-build-all-threads)
8337         (error "Can't fetch thread from back ends that don't support NOV"))
8338       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8339     (gnus-summary-limit-include-thread id)))
8340
8341 (defun gnus-summary-refer-article (message-id)
8342   "Fetch an article specified by MESSAGE-ID."
8343   (interactive "sMessage-ID: ")
8344   (when (and (stringp message-id)
8345              (not (zerop (length message-id))))
8346     (setq message-id (gnus-replace-in-string message-id " " ""))
8347     ;; Construct the correct Message-ID if necessary.
8348     ;; Suggested by tale@pawl.rpi.edu.
8349     (unless (string-match "^<" message-id)
8350       (setq message-id (concat "<" message-id)))
8351     (unless (string-match ">$" message-id)
8352       (setq message-id (concat message-id ">")))
8353     ;; People often post MIDs from URLs, so unhex it:
8354     (unless (string-match "@" message-id)
8355       (setq message-id (gnus-url-unhex-string message-id)))
8356     (let* ((header (gnus-id-to-header message-id))
8357            (sparse (and header
8358                         (gnus-summary-article-sparse-p
8359                          (mail-header-number header))
8360                         (memq (mail-header-number header)
8361                               gnus-newsgroup-limit)))
8362            number)
8363       (cond
8364        ;; If the article is present in the buffer we just go to it.
8365        ((and header
8366              (or (not (gnus-summary-article-sparse-p
8367                        (mail-header-number header)))
8368                  sparse))
8369         (prog1
8370             (gnus-summary-goto-article
8371              (mail-header-number header) nil t)
8372           (when sparse
8373             (gnus-summary-update-article (mail-header-number header)))))
8374        (t
8375         ;; We fetch the article.
8376         (catch 'found
8377           (dolist (gnus-override-method (gnus-refer-article-methods))
8378             (when (and (gnus-check-server gnus-override-method)
8379                        ;; Fetch the header,
8380                        (setq number (gnus-summary-insert-subject message-id)))
8381               ;; and display the article.
8382               (gnus-summary-select-article nil nil nil number)
8383               (throw 'found t)))
8384           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8385
8386 (defun gnus-refer-article-methods ()
8387   "Return a list of referable methods."
8388   (cond
8389    ;; No method, so we default to current and native.
8390    ((null gnus-refer-article-method)
8391     (list gnus-current-select-method gnus-select-method))
8392    ;; Current.
8393    ((eq 'current gnus-refer-article-method)
8394     (list gnus-current-select-method))
8395    ;; List of select methods.
8396    ((not (and (symbolp (car gnus-refer-article-method))
8397               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8398     (let (out)
8399       (dolist (method gnus-refer-article-method)
8400         (push (if (eq 'current method)
8401                   gnus-current-select-method
8402                 method)
8403               out))
8404       (nreverse out)))
8405    ;; One single select method.
8406    (t
8407     (list gnus-refer-article-method))))
8408
8409 (defun gnus-summary-edit-parameters ()
8410   "Edit the group parameters of the current group."
8411   (interactive)
8412   (gnus-group-edit-group gnus-newsgroup-name 'params))
8413
8414 (defun gnus-summary-customize-parameters ()
8415   "Customize the group parameters of the current group."
8416   (interactive)
8417   (gnus-group-customize gnus-newsgroup-name))
8418
8419 (defun gnus-summary-enter-digest-group (&optional force)
8420   "Enter an nndoc group based on the current article.
8421 If FORCE, force a digest interpretation.  If not, try
8422 to guess what the document format is."
8423   (interactive "P")
8424   (let ((conf gnus-current-window-configuration))
8425     (save-window-excursion
8426       (save-excursion
8427         (let (gnus-article-prepare-hook
8428               gnus-display-mime-function
8429               gnus-break-pages)
8430           (gnus-summary-select-article))))
8431     (setq gnus-current-window-configuration conf)
8432     (let* ((name (format "%s-%d"
8433                          (gnus-group-prefixed-name
8434                           gnus-newsgroup-name (list 'nndoc ""))
8435                          (with-current-buffer gnus-summary-buffer
8436                            gnus-current-article)))
8437            (ogroup gnus-newsgroup-name)
8438            (params (append (gnus-info-params (gnus-get-info ogroup))
8439                            (list (cons 'to-group ogroup))
8440                            (list (cons 'parent-group ogroup))
8441                            (list (cons 'save-article-group ogroup))))
8442            (case-fold-search t)
8443            (buf (current-buffer))
8444            dig to-address)
8445       (save-excursion
8446         (set-buffer gnus-original-article-buffer)
8447         ;; Have the digest group inherit the main mail address of
8448         ;; the parent article.
8449         (when (setq to-address (or (gnus-fetch-field "reply-to")
8450                                    (gnus-fetch-field "from")))
8451           (setq params (append
8452                         (list (cons 'to-address
8453                                     (funcall gnus-decode-encoded-word-function
8454                                              to-address))))))
8455         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8456         (insert-buffer-substring gnus-original-article-buffer)
8457         ;; Remove lines that may lead nndoc to misinterpret the
8458         ;; document type.
8459         (narrow-to-region
8460          (goto-char (point-min))
8461          (or (search-forward "\n\n" nil t) (point)))
8462         (goto-char (point-min))
8463         (delete-matching-lines "^Path:\\|^From ")
8464         (widen))
8465       (unwind-protect
8466           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8467                     (gnus-newsgroup-ephemeral-ignored-charsets
8468                      gnus-newsgroup-ignored-charsets))
8469                 (gnus-group-read-ephemeral-group
8470                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8471                               (nndoc-article-type
8472                                ,(if force 'mbox 'guess)))
8473                  t nil nil nil
8474                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8475                                                         "ADAPT")))))
8476               ;; Make all postings to this group go to the parent group.
8477               (nconc (gnus-info-params (gnus-get-info name))
8478                      params)
8479             ;; Couldn't select this doc group.
8480             (switch-to-buffer buf)
8481             (gnus-set-global-variables)
8482             (gnus-configure-windows 'summary)
8483             (gnus-message 3 "Article couldn't be entered?"))
8484         (kill-buffer dig)))))
8485
8486 (defun gnus-summary-read-document (n)
8487   "Open a new group based on the current article(s).
8488 This will allow you to read digests and other similar
8489 documents as newsgroups.
8490 Obeys the standard process/prefix convention."
8491   (interactive "P")
8492   (let* ((ogroup gnus-newsgroup-name)
8493          (params (append (gnus-info-params (gnus-get-info ogroup))
8494                          (list (cons 'to-group ogroup))))
8495          group egroup groups vgroup)
8496     (dolist (article (gnus-summary-work-articles n))
8497       (setq group (format "%s-%d" gnus-newsgroup-name article))
8498       (gnus-summary-remove-process-mark article)
8499       (when (gnus-summary-display-article article)
8500         (save-excursion
8501           (with-temp-buffer
8502             (insert-buffer-substring gnus-original-article-buffer)
8503             ;; Remove some headers that may lead nndoc to make
8504             ;; the wrong guess.
8505             (message-narrow-to-head)
8506             (goto-char (point-min))
8507             (delete-matching-lines "^Path:\\|^From ")
8508             (widen)
8509             (if (setq egroup
8510                       (gnus-group-read-ephemeral-group
8511                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8512                                      (nndoc-article-type guess))
8513                        t nil t))
8514                 (progn
8515                   ;; Make all postings to this group go to the parent group.
8516                   (nconc (gnus-info-params (gnus-get-info egroup))
8517                          params)
8518                   (push egroup groups))
8519               ;; Couldn't select this doc group.
8520               (gnus-error 3 "Article couldn't be entered"))))))
8521     ;; Now we have selected all the documents.
8522     (cond
8523      ((not groups)
8524       (error "None of the articles could be interpreted as documents"))
8525      ((gnus-group-read-ephemeral-group
8526        (setq vgroup (format
8527                      "nnvirtual:%s-%s" gnus-newsgroup-name
8528                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8529        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8530        t
8531        (cons (current-buffer) 'summary)))
8532      (t
8533       (error "Couldn't select virtual nndoc group")))))
8534
8535 (defun gnus-summary-isearch-article (&optional regexp-p)
8536   "Do incremental search forward on the current article.
8537 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8538   (interactive "P")
8539   (let* ((gnus-inhibit-treatment t)
8540          (old (gnus-summary-select-article)))
8541     (gnus-configure-windows 'article)
8542     (gnus-eval-in-buffer-window gnus-article-buffer
8543       (save-restriction
8544         (widen)
8545         (when (eq 'old old)
8546           (gnus-article-show-all-headers))
8547         (goto-char (point-min))
8548         (isearch-forward regexp-p)))))
8549
8550 (defun gnus-summary-search-article-forward (regexp &optional backward)
8551   "Search for an article containing REGEXP forward.
8552 If BACKWARD, search backward instead."
8553   (interactive
8554    (list (read-string
8555           (format "Search article %s (regexp%s): "
8556                   (if current-prefix-arg "backward" "forward")
8557                   (if gnus-last-search-regexp
8558                       (concat ", default " gnus-last-search-regexp)
8559                     "")))
8560          current-prefix-arg))
8561   (if (string-equal regexp "")
8562       (setq regexp (or gnus-last-search-regexp ""))
8563     (setq gnus-last-search-regexp regexp)
8564     (setq gnus-article-before-search gnus-current-article))
8565   ;; Intentionally set gnus-last-article.
8566   (setq gnus-last-article gnus-article-before-search)
8567   (let ((gnus-last-article gnus-last-article))
8568     (if (gnus-summary-search-article regexp backward)
8569         (gnus-summary-show-thread)
8570       (signal 'search-failed (list regexp)))))
8571
8572 (defun gnus-summary-search-article-backward (regexp)
8573   "Search for an article containing REGEXP backward."
8574   (interactive
8575    (list (read-string
8576           (format "Search article backward (regexp%s): "
8577                   (if gnus-last-search-regexp
8578                       (concat ", default " gnus-last-search-regexp)
8579                     "")))))
8580   (gnus-summary-search-article-forward regexp 'backward))
8581
8582 (eval-when-compile
8583   (defmacro gnus-summary-search-article-position-point (regexp backward)
8584     "Dehighlight the last matched text and goto the beginning position."
8585     (` (if (and gnus-summary-search-article-matched-data
8586                 (let ((text (caddr gnus-summary-search-article-matched-data))
8587                       (inhibit-read-only t)
8588                       buffer-read-only)
8589                   (delete-region
8590                    (goto-char (car gnus-summary-search-article-matched-data))
8591                    (cadr gnus-summary-search-article-matched-data))
8592                   (insert text)
8593                   (string-match (, regexp) text)))
8594            (if (, backward) (beginning-of-line) (end-of-line))
8595          (goto-char (if (, backward) (point-max) (point-min))))))
8596
8597   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8598     "Place point where X-Face image is displayed."
8599     (if (featurep 'xemacs)
8600         (` (let ((end (if (search-forward "\n\n" nil t)
8601                           (goto-char (1- (point)))
8602                         (point-min)))
8603                  extent)
8604              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8605              (unless (and (re-search-forward "^From:" end t)
8606                           (setq extent (extent-at (point)))
8607                           (extent-begin-glyph extent))
8608                (goto-char (, opoint)))))
8609       (` (let ((end (if (search-forward "\n\n" nil t)
8610                         (goto-char (1- (point)))
8611                       (point-min)))
8612                (start (or (search-backward "\n\n" nil t) (point-min))))
8613            (goto-char
8614             (or (text-property-any start end 'x-face-image t);; x-face-e21
8615                 (, opoint)))))))
8616
8617   (defmacro gnus-summary-search-article-highlight-matched-text
8618     (backward treated x-face)
8619     "Highlight matched text in the function `gnus-summary-search-article'."
8620     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8621              (end (set-marker (make-marker) (match-end 0)))
8622              (inhibit-read-only t)
8623              buffer-read-only)
8624          (unless treated
8625            (let ((,@
8626                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8627                     (mapcar
8628                      (lambda (item) (setq items (delq item items)))
8629                      '(gnus-treat-buttonize
8630                        gnus-treat-fill-article
8631                        gnus-treat-fill-long-lines
8632                        gnus-treat-emphasize
8633                        gnus-treat-highlight-headers
8634                        gnus-treat-highlight-citation
8635                        gnus-treat-highlight-signature
8636                        gnus-treat-overstrike
8637                        gnus-treat-display-x-face
8638                        gnus-treat-buttonize-head
8639                        gnus-treat-decode-article-as-default-mime-charset))
8640                     items))
8641                  (gnus-treat-display-x-face
8642                   (when (, x-face) gnus-treat-display-x-face)))
8643              (gnus-article-prepare-mime-display)))
8644          (goto-char (if (, backward) start end))
8645          (when (, x-face)
8646            (gnus-summary-search-article-highlight-goto-x-face (point)))
8647          (setq gnus-summary-search-article-matched-data
8648                (list start end (buffer-substring start end)))
8649          (unless (eq start end);; matched text has been deleted. :-<
8650            (put-text-property start end 'face
8651                               (or (find-face 'isearch)
8652                                   'secondary-selection))))))
8653   )
8654
8655 (defun gnus-summary-search-article (regexp &optional backward)
8656   "Search for an article containing REGEXP.
8657 Optional argument BACKWARD means do search for backward.
8658 `gnus-select-article-hook' is not called during the search."
8659   ;; We have to require this here to make sure that the following
8660   ;; dynamic binding isn't shadowed by autoloading.
8661   (require 'gnus-async)
8662   (require 'gnus-art)
8663   (let ((gnus-select-article-hook nil)  ;Disable hook.
8664         (gnus-article-prepare-hook nil)
8665         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8666         (gnus-use-article-prefetch nil)
8667         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8668         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8669         (gnus-visual nil)
8670         (gnus-keep-backlog nil)
8671         (gnus-break-pages nil)
8672         (gnus-summary-display-arrow nil)
8673         (gnus-updated-mode-lines nil)
8674         (gnus-auto-center-summary nil)
8675         (sum (current-buffer))
8676         (found nil)
8677         point treated)
8678     (gnus-save-hidden-threads
8679       (static-if (featurep 'xemacs)
8680           (let ((gnus-inhibit-treatment t))
8681             (setq treated (eq 'old (gnus-summary-select-article)))
8682             (when (and treated
8683                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8684                                  (window-live-p (get-buffer-window
8685                                                  gnus-article-buffer t)))))
8686               (gnus-summary-select-article nil t)
8687               (setq treated nil)))
8688         (let ((gnus-inhibit-treatment t))
8689           (setq treated (eq 'old (gnus-summary-select-article)))
8690           (when (and treated
8691                      (not
8692                       (and (gnus-buffer-live-p gnus-article-buffer)
8693                            (window-live-p (get-buffer-window
8694                                            gnus-article-buffer t))
8695                            (or (not (string-match "^\\^X-Face:" regexp))
8696                                (with-current-buffer gnus-article-buffer
8697                                  gnus-summary-search-article-matched-data)))))
8698             (gnus-summary-select-article nil t)
8699             (setq treated nil))))
8700       (set-buffer gnus-article-buffer)
8701       (widen)
8702       (if treated
8703           (progn
8704             (gnus-article-show-all-headers)
8705             (gnus-summary-search-article-position-point regexp backward))
8706         (goto-char (if backward (point-max) (point-min))))
8707       (while (not found)
8708         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8709         (if (if backward
8710                 (re-search-backward regexp nil t)
8711               (re-search-forward regexp nil t))
8712             ;; We found the regexp.
8713             (progn
8714               (gnus-summary-search-article-highlight-matched-text
8715                backward treated (string-match "^\\^X-Face:" regexp))
8716               (setq found 'found)
8717               (forward-line
8718                (/ (- 2 (window-height
8719                         (get-buffer-window gnus-article-buffer t)))
8720                   2))
8721               (set-window-start
8722                (get-buffer-window (current-buffer))
8723                (point))
8724               (set-buffer sum)
8725               (setq point (point)))
8726           ;; We didn't find it, so we go to the next article.
8727           (set-buffer sum)
8728           (setq found 'not)
8729           (while (eq found 'not)
8730             (if (not (if backward (gnus-summary-find-prev)
8731                        (gnus-summary-find-next)))
8732                 ;; No more articles.
8733                 (setq found t)
8734               ;; Select the next article and adjust point.
8735               (unless (gnus-summary-article-sparse-p
8736                        (gnus-summary-article-number))
8737                 (setq found nil)
8738                 (let ((gnus-inhibit-treatment t))
8739                   (gnus-summary-select-article))
8740                 (setq treated nil)
8741                 (set-buffer gnus-article-buffer)
8742                 (widen)
8743                 (goto-char (if backward (point-max) (point-min))))))))
8744       (gnus-message 7 ""))
8745     ;; Return whether we found the regexp.
8746     (when (eq found 'found)
8747       (goto-char point)
8748       (gnus-summary-show-thread)
8749       (gnus-summary-goto-subject gnus-current-article)
8750       (gnus-summary-position-point)
8751       t)))
8752
8753 (defun gnus-find-matching-articles (header regexp)
8754   "Return a list of all articles that match REGEXP on HEADER.
8755 This search includes all articles in the current group that Gnus has
8756 fetched headers for, whether they are displayed or not."
8757   (let ((articles nil)
8758         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8759         (case-fold-search t))
8760     (dolist (header gnus-newsgroup-headers)
8761       (when (string-match regexp (funcall func header))
8762         (push (mail-header-number header) articles)))
8763     (nreverse articles)))
8764
8765 (defun gnus-summary-find-matching (header regexp &optional backward unread
8766                                           not-case-fold not-matching)
8767   "Return a list of all articles that match REGEXP on HEADER.
8768 The search stars on the current article and goes forwards unless
8769 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8770 If UNREAD is non-nil, only unread articles will
8771 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8772 in the comparisons. If NOT-MATCHING, return a list of all articles that
8773 not match REGEXP on HEADER."
8774   (let ((case-fold-search (not not-case-fold))
8775         articles d func)
8776     (if (consp header)
8777         (if (eq (car header) 'extra)
8778             (setq func
8779                   `(lambda (h)
8780                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8781                          "")))
8782           (error "%s is an invalid header" header))
8783       (unless (fboundp (intern (concat "mail-header-" header)))
8784         (error "%s is not a valid header" header))
8785       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8786     (dolist (d (if (eq backward 'all)
8787                    gnus-newsgroup-data
8788                  (gnus-data-find-list
8789                   (gnus-summary-article-number)
8790                   (gnus-data-list backward))))
8791       (when (and (or (not unread)       ; We want all articles...
8792                      (gnus-data-unread-p d)) ; Or just unreads.
8793                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8794                  (if not-matching
8795                      (not (string-match
8796                            regexp
8797                            (funcall func (gnus-data-header d))))
8798                    (string-match regexp
8799                                  (funcall func (gnus-data-header d)))))
8800         (push (gnus-data-number d) articles))) ; Success!
8801     (nreverse articles)))
8802
8803 (defun gnus-summary-execute-command (header regexp command &optional backward)
8804   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8805 If HEADER is an empty string (or nil), the match is done on the entire
8806 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8807   (interactive
8808    (list (let ((completion-ignore-case t))
8809            (completing-read
8810             "Header name: "
8811             (mapcar (lambda (header) (list (format "%s" header)))
8812                     (append
8813                      '("Number" "Subject" "From" "Lines" "Date"
8814                        "Message-ID" "Xref" "References" "Body")
8815                      gnus-extra-headers))
8816             nil 'require-match))
8817          (read-string "Regexp: ")
8818          (read-key-sequence "Command: ")
8819          current-prefix-arg))
8820   (when (equal header "Body")
8821     (setq header ""))
8822   ;; Hidden thread subtrees must be searched as well.
8823   (gnus-summary-show-all-threads)
8824   ;; We don't want to change current point nor window configuration.
8825   (save-excursion
8826     (save-window-excursion
8827       (let (gnus-visual
8828             gnus-treat-strip-trailing-blank-lines
8829             gnus-treat-strip-leading-blank-lines
8830             gnus-treat-strip-multiple-blank-lines
8831             gnus-treat-hide-boring-headers
8832             gnus-treat-fold-newsgroups
8833             gnus-article-prepare-hook)
8834         (gnus-message 6 "Executing %s..." (key-description command))
8835         ;; We'd like to execute COMMAND interactively so as to give arguments.
8836         (gnus-execute header regexp
8837                       `(call-interactively ',(key-binding command))
8838                       backward)
8839         (gnus-message 6 "Executing %s...done" (key-description command))))))
8840
8841 (defun gnus-summary-beginning-of-article ()
8842   "Scroll the article back to the beginning."
8843   (interactive)
8844   (gnus-summary-select-article)
8845   (gnus-configure-windows 'article)
8846   (gnus-eval-in-buffer-window gnus-article-buffer
8847     (widen)
8848     (goto-char (point-min))
8849     (when gnus-break-pages
8850       (gnus-narrow-to-page))))
8851
8852 (defun gnus-summary-end-of-article ()
8853   "Scroll to the end of the article."
8854   (interactive)
8855   (gnus-summary-select-article)
8856   (gnus-configure-windows 'article)
8857   (gnus-eval-in-buffer-window gnus-article-buffer
8858     (widen)
8859     (goto-char (point-max))
8860     (recenter -3)
8861     (when gnus-break-pages
8862       (when (re-search-backward page-delimiter nil t)
8863         (narrow-to-region (match-end 0) (point-max)))
8864       (gnus-narrow-to-page))))
8865
8866 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8867   "Truncate to LEN and quote all \"(\"'s in STRING."
8868   (gnus-replace-in-string (if (and len (> (length string) len))
8869                               (substring string 0 len)
8870                             string)
8871                           "[()]" "\\\\\\&"))
8872
8873 (defun gnus-summary-print-article (&optional filename n)
8874   "Generate and print a PostScript image of the process-marked (mail) articles.
8875
8876 If used interactively, print the current article if none are
8877 process-marked.  With prefix arg, prompt the user for the name of the
8878 file to save in.
8879
8880 When used from Lisp, accept two optional args FILENAME and N.  N means
8881 to print the next N articles.  If N is negative, print the N previous
8882 articles.  If N is nil and articles have been marked with the process
8883 mark, print these instead.
8884
8885 If the optional first argument FILENAME is nil, send the image to the
8886 printer.  If FILENAME is a string, save the PostScript image in a file with
8887 that name.  If FILENAME is a number, prompt the user for the name of the file
8888 to save in."
8889   (interactive (list (ps-print-preprint current-prefix-arg)))
8890   (dolist (article (gnus-summary-work-articles n))
8891     (gnus-summary-select-article nil nil 'pseudo article)
8892     (gnus-eval-in-buffer-window gnus-article-buffer
8893       (gnus-print-buffer))
8894     (gnus-summary-remove-process-mark article))
8895   (ps-despool filename))
8896
8897 (defun gnus-print-buffer ()
8898   (let ((buffer (generate-new-buffer " *print*")))
8899     (unwind-protect
8900         (progn
8901           (copy-to-buffer buffer (point-min) (point-max))
8902           (set-buffer buffer)
8903           (gnus-remove-text-with-property 'gnus-decoration)
8904           (when (gnus-visual-p 'article-highlight 'highlight)
8905             ;; Copy-to-buffer doesn't copy overlay.  So redo
8906             ;; highlight.
8907             (let ((gnus-article-buffer buffer))
8908               (gnus-article-highlight-citation t)
8909               (gnus-article-highlight-signature)
8910               (gnus-article-emphasize)
8911               (gnus-article-delete-invisible-text)))
8912           (let ((ps-left-header
8913                  (list
8914                   (concat "("
8915                           (gnus-summary-print-truncate-and-quote
8916                            (mail-header-subject gnus-current-headers)
8917                            66) ")")
8918                   (concat "("
8919                           (gnus-summary-print-truncate-and-quote
8920                            (mail-header-from gnus-current-headers)
8921                            45) ")")))
8922                 (ps-right-header
8923                  (list
8924                   "/pagenumberstring load"
8925                   (concat "("
8926                           (mail-header-date gnus-current-headers) ")"))))
8927             (gnus-run-hooks 'gnus-ps-print-hook)
8928             (save-excursion
8929               (if window-system
8930                   (ps-spool-buffer-with-faces)
8931                 (ps-spool-buffer)))))
8932       (kill-buffer buffer))))
8933
8934 (defun gnus-summary-show-article (&optional arg)
8935   "Force redisplaying of the current article.
8936 If ARG (the prefix) is a number, show the article with the charset
8937 defined in `gnus-summary-show-article-charset-alist', or the charset
8938 input.
8939 If ARG (the prefix) is non-nil and not a number, show the raw article
8940 without any article massaging functions being run.  Normally, the key
8941 strokes are `C-u g'."
8942   (interactive "P")
8943   (cond
8944    ((numberp arg)
8945     (gnus-summary-show-article t)
8946     (let* ((gnus-newsgroup-charset
8947             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8948                 (mm-read-coding-system
8949                  "View as charset: " ;; actually it is coding system.
8950                  (with-current-buffer gnus-article-buffer
8951                    (mm-detect-coding-region (point) (point-max))))))
8952            (default-mime-charset gnus-newsgroup-charset)
8953            (gnus-newsgroup-ignored-charsets 'gnus-all))
8954       (gnus-summary-select-article nil 'force)
8955       (let ((deps gnus-newsgroup-dependencies)
8956             head header lines)
8957         (save-excursion
8958           (set-buffer gnus-original-article-buffer)
8959           (save-restriction
8960             (message-narrow-to-head)
8961             (setq head (buffer-string))
8962             (goto-char (point-min))
8963             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8964               (goto-char (point-max))
8965               (widen)
8966               (setq lines (1- (count-lines (point) (point-max))))))
8967           (with-temp-buffer
8968             (insert (format "211 %d Article retrieved.\n"
8969                             (cdr gnus-article-current)))
8970             (insert head)
8971             (if lines (insert (format "Lines: %d\n" lines)))
8972             (insert ".\n")
8973             (let ((nntp-server-buffer (current-buffer)))
8974               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8975         (gnus-data-set-header
8976          (gnus-data-find (cdr gnus-article-current))
8977          header)
8978         (gnus-summary-update-article-line
8979          (cdr gnus-article-current) header)
8980         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8981           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8982    ((not arg)
8983     ;; Select the article the normal way.
8984     (gnus-summary-select-article nil 'force))
8985    (t
8986     ;; We have to require this here to make sure that the following
8987     ;; dynamic binding isn't shadowed by autoloading.
8988     (require 'gnus-async)
8989     (require 'gnus-art)
8990     ;; Bind the article treatment functions to nil.
8991     (let ((gnus-have-all-headers t)
8992           gnus-article-prepare-hook
8993           gnus-article-decode-hook
8994           gnus-break-pages
8995           gnus-show-mime
8996           (gnus-inhibit-treatment t))
8997       (gnus-summary-select-article nil 'force))))
8998   (gnus-summary-goto-subject gnus-current-article)
8999   (gnus-summary-position-point))
9000
9001 (defun gnus-summary-show-raw-article ()
9002   "Show the raw article without any article massaging functions being run."
9003   (interactive)
9004   (gnus-summary-show-article t))
9005
9006 (defun gnus-summary-verbose-headers (&optional arg)
9007   "Toggle permanent full header display.
9008 If ARG is a positive number, turn header display on.
9009 If ARG is a negative number, turn header display off."
9010   (interactive "P")
9011   (setq gnus-show-all-headers
9012         (cond ((or (not (numberp arg))
9013                    (zerop arg))
9014                (not gnus-show-all-headers))
9015               ((natnump arg)
9016                t)))
9017   (gnus-summary-show-article))
9018
9019 (defun gnus-summary-toggle-header (&optional arg)
9020   "Show the headers if they are hidden, or hide them if they are shown.
9021 If ARG is a positive number, show the entire header.
9022 If ARG is a negative number, hide the unwanted header lines."
9023   (interactive "P")
9024   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9025                      (get-buffer-window gnus-article-buffer t))))
9026     (with-current-buffer gnus-article-buffer
9027       (widen)
9028       (article-narrow-to-head)
9029       (let* ((buffer-read-only nil)
9030              (inhibit-point-motion-hooks t)
9031              (hidden (if (numberp arg)
9032                          (>= arg 0)
9033                        (or (not (looking-at "[^ \t\n]+:"))
9034                            (gnus-article-hidden-text-p 'headers))))
9035              s e)
9036         (delete-region (point-min) (point-max))
9037         (with-current-buffer gnus-original-article-buffer
9038           (goto-char (setq s (point-min)))
9039           (setq e (if (search-forward "\n\n" nil t)
9040                       (1- (point))
9041                     (point-max))))
9042         (insert-buffer-substring gnus-original-article-buffer s e)
9043         ;; In T-gnus, gnus-article-decode-hook doesn't contain
9044         ;; article-decode-encoded-words by default.
9045         (article-decode-encoded-words)
9046         (run-hooks 'gnus-article-decode-hook)
9047         (if hidden
9048             (let ((gnus-treat-hide-headers nil)
9049                   (gnus-treat-hide-boring-headers nil))
9050               (gnus-delete-wash-type 'headers)
9051               (gnus-treat-article 'head))
9052           (gnus-treat-article 'head))
9053         (widen)
9054         (if window
9055             (set-window-start window (goto-char (point-min))))
9056         (if gnus-break-pages
9057             (gnus-narrow-to-page)
9058           (when (gnus-visual-p 'page-marker)
9059             (let ((buffer-read-only nil))
9060               (gnus-remove-text-with-property 'gnus-prev)
9061               (gnus-remove-text-with-property 'gnus-next))))
9062         (gnus-set-mode-line 'article)))))
9063
9064 (defun gnus-summary-show-all-headers ()
9065   "Make all header lines visible."
9066   (interactive)
9067   (gnus-summary-toggle-header 1))
9068
9069 (defun gnus-summary-toggle-mime (&optional arg)
9070   "Toggle MIME processing.
9071 If ARG is a positive number, turn MIME processing on."
9072   (interactive "P")
9073   (setq gnus-show-mime
9074         (if (null arg)
9075             (not gnus-show-mime)
9076           (> (prefix-numeric-value arg) 0)))
9077   (gnus-summary-select-article t 'force))
9078
9079 (defun gnus-summary-caesar-message (&optional arg)
9080   "Caesar rotate the current article by 13.
9081 The numerical prefix specifies how many places to rotate each letter
9082 forward."
9083   (interactive "P")
9084   (gnus-summary-select-article)
9085   (let ((mail-header-separator ""))
9086     (gnus-eval-in-buffer-window gnus-article-buffer
9087       (save-restriction
9088         (widen)
9089         (let ((start (window-start))
9090               buffer-read-only)
9091           (message-caesar-buffer-body arg)
9092           (set-window-start (get-buffer-window (current-buffer)) start)))))
9093   ;; Create buttons and stuff...
9094   (gnus-treat-article nil))
9095
9096 (defun gnus-summary-idna-message (&optional arg)
9097   "Decode IDNA encoded domain names in the current articles.
9098 IDNA encoded domain names looks like `xn--bar'.  If a string
9099 remain unencoded after running this function, it is likely an
9100 invalid IDNA string (`xn--bar' is invalid).
9101
9102 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9103 installed for this command to work."
9104   (interactive "P")
9105   (if (not (and (condition-case nil (require 'idna)
9106                   (file-error))
9107                 (mm-coding-system-p 'utf-8)
9108                 (executable-find (symbol-value 'idna-program))))
9109       (gnus-message
9110        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9111     (gnus-summary-select-article)
9112     (let ((mail-header-separator ""))
9113       (gnus-eval-in-buffer-window gnus-article-buffer
9114         (save-restriction
9115           (widen)
9116           (let ((start (window-start))
9117                 buffer-read-only)
9118             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9119               (replace-match (idna-to-unicode (match-string 1))))
9120             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9121
9122 (autoload 'unmorse-region "morse"
9123   "Convert morse coded text in region to ordinary ASCII text."
9124   t)
9125
9126 (defun gnus-summary-morse-message (&optional arg)
9127   "Morse decode the current article."
9128   (interactive "P")
9129   (gnus-summary-select-article)
9130   (let ((mail-header-separator ""))
9131     (gnus-eval-in-buffer-window gnus-article-buffer
9132       (save-excursion
9133         (save-restriction
9134           (widen)
9135           (let ((pos (window-start))
9136                 buffer-read-only)
9137             (goto-char (point-min))
9138             (when (message-goto-body)
9139               (gnus-narrow-to-body))
9140             (goto-char (point-min))
9141             (while (search-forward "·" (point-max) t)
9142               (replace-match "."))
9143             (unmorse-region (point-min) (point-max))
9144             (widen)
9145             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9146
9147 (defun gnus-summary-stop-page-breaking ()
9148   "Stop page breaking in the current article."
9149   (interactive)
9150   (gnus-summary-select-article)
9151   (gnus-eval-in-buffer-window gnus-article-buffer
9152     (widen)
9153     (when (gnus-visual-p 'page-marker)
9154       (let ((buffer-read-only nil))
9155         (gnus-remove-text-with-property 'gnus-prev)
9156         (gnus-remove-text-with-property 'gnus-next))
9157       (setq gnus-page-broken nil))))
9158
9159 (defun gnus-summary-move-article (&optional n to-newsgroup
9160                                             select-method action)
9161   "Move the current article to a different newsgroup.
9162 If N is a positive number, move the N next articles.
9163 If N is a negative number, move the N previous articles.
9164 If N is nil and any articles have been marked with the process mark,
9165 move those articles instead.
9166 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9167 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9168 re-spool using this method.
9169
9170 When called interactively with TO-NEWSGROUP being nil, the value of
9171 the variable `gnus-move-split-methods' is used for finding a default
9172 for the target newsgroup.
9173
9174 For this function to work, both the current newsgroup and the
9175 newsgroup that you want to move to have to support the `request-move'
9176 and `request-accept' functions.
9177
9178 ACTION can be either `move' (the default), `crosspost' or `copy'."
9179   (interactive "P")
9180   (unless action
9181     (setq action 'move))
9182   ;; Check whether the source group supports the required functions.
9183   (cond ((and (eq action 'move)
9184               (not (gnus-check-backend-function
9185                     'request-move-article gnus-newsgroup-name)))
9186          (error "The current group does not support article moving"))
9187         ((and (eq action 'crosspost)
9188               (not (gnus-check-backend-function
9189                     'request-replace-article gnus-newsgroup-name)))
9190          (error "The current group does not support article editing")))
9191   (let ((articles (gnus-summary-work-articles n))
9192         (prefix (if (gnus-check-backend-function
9193                      'request-move-article gnus-newsgroup-name)
9194                     (funcall gnus-move-group-prefix-function
9195                              gnus-newsgroup-name)
9196                   ""))
9197         (names '((move "Move" "Moving")
9198                  (copy "Copy" "Copying")
9199                  (crosspost "Crosspost" "Crossposting")))
9200         (copy-buf (save-excursion
9201                     (nnheader-set-temp-buffer " *copy article*")))
9202         art-group to-method new-xref article to-groups articles-to-update-marks)
9203     (unless (assq action names)
9204       (error "Unknown action %s" action))
9205     ;; Read the newsgroup name.
9206     (when (and (not to-newsgroup)
9207                (not select-method))
9208       (if (and gnus-move-split-methods
9209                (not
9210                 (and (memq gnus-current-article articles)
9211                      (gnus-buffer-live-p gnus-original-article-buffer))))
9212           ;; When `gnus-move-split-methods' is non-nil, we have to
9213           ;; select an article to give `gnus-read-move-group-name' an
9214           ;; opportunity to suggest an appropriate default.  However,
9215           ;; we needn't render or mark the article.
9216           (let ((gnus-display-mime-function nil)
9217                 (gnus-article-prepare-hook nil)
9218                 (gnus-mark-article-hook nil))
9219             (gnus-summary-select-article nil nil nil (car articles))))
9220       (setq to-newsgroup
9221             (gnus-read-move-group-name
9222              (cadr (assq action names))
9223              (symbol-value (intern (format "gnus-current-%s-group" action)))
9224              articles prefix))
9225       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9226     (setq to-method (or select-method
9227                         (gnus-server-to-method
9228                          (gnus-group-method to-newsgroup))))
9229     ;; Check the method we are to move this article to...
9230     (unless (gnus-check-backend-function
9231              'request-accept-article (car to-method))
9232       (error "%s does not support article copying" (car to-method)))
9233     (unless (gnus-check-server to-method)
9234       (error "Can't open server %s" (car to-method)))
9235     (gnus-message 6 "%s to %s: %s..."
9236                   (caddr (assq action names))
9237                   (or (car select-method) to-newsgroup) articles)
9238     (while articles
9239       (setq article (pop articles))
9240       (setq
9241        art-group
9242        (cond
9243         ;; Move the article.
9244         ((eq action 'move)
9245          ;; Remove this article from future suppression.
9246          (gnus-dup-unsuppress-article article)
9247          (let* ((from-method (gnus-find-method-for-group
9248                               gnus-newsgroup-name))
9249                 (to-method (gnus-find-method-for-group
9250                             to-newsgroup))
9251                 (move-is-internal (gnus-method-equal from-method to-method)))
9252          (gnus-request-move-article
9253           article                       ; Article to move
9254           gnus-newsgroup-name           ; From newsgroup
9255           (nth 1 (gnus-find-method-for-group
9256                   gnus-newsgroup-name)) ; Server
9257           (list 'gnus-request-accept-article
9258                 to-newsgroup (list 'quote select-method)
9259                 (not articles) t)       ; Accept form
9260           (not articles)                ; Only save nov last time
9261           move-is-internal)))           ; is this move internal?
9262         ;; Copy the article.
9263         ((eq action 'copy)
9264          (save-excursion
9265            (set-buffer copy-buf)
9266            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9267              (save-restriction
9268                (nnheader-narrow-to-headers)
9269                (dolist (hdr gnus-copy-article-ignored-headers)
9270                  (message-remove-header hdr t)))
9271              (gnus-request-accept-article
9272               to-newsgroup select-method (not articles) t))))
9273         ;; Crosspost the article.
9274         ((eq action 'crosspost)
9275          (let ((xref (message-tokenize-header
9276                       (mail-header-xref (gnus-summary-article-header article))
9277                       " ")))
9278            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9279                                   ":" (number-to-string article)))
9280            (unless xref
9281              (setq xref (list (system-name))))
9282            (setq new-xref
9283                  (concat
9284                   (mapconcat 'identity
9285                              (delete "Xref:" (delete new-xref xref))
9286                              " ")
9287                   " " new-xref))
9288            (save-excursion
9289              (set-buffer copy-buf)
9290              ;; First put the article in the destination group.
9291              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9292              (when (consp (setq art-group
9293                                 (gnus-request-accept-article
9294                                  to-newsgroup select-method (not articles))))
9295                (setq new-xref (concat new-xref " " (car art-group)
9296                                       ":"
9297                                       (number-to-string (cdr art-group))))
9298                ;; Now we have the new Xrefs header, so we insert
9299                ;; it and replace the new article.
9300                (nnheader-replace-header "Xref" new-xref)
9301                (gnus-request-replace-article
9302                 (cdr art-group) to-newsgroup (current-buffer))
9303                art-group))))))
9304       (cond
9305        ((not art-group)
9306         (gnus-message 1 "Couldn't %s article %s: %s"
9307                       (cadr (assq action names)) article
9308                       (nnheader-get-report (car to-method))))
9309        ((eq art-group 'junk)
9310         (when (eq action 'move)
9311           (gnus-summary-mark-article article gnus-canceled-mark)
9312           (gnus-message 4 "Deleted article %s" article)
9313           ;; run the delete hook
9314           (run-hook-with-args 'gnus-summary-article-delete-hook
9315                               action
9316                               (gnus-data-header
9317                                (assoc article (gnus-data-list nil)))
9318                               gnus-newsgroup-name nil
9319                               select-method)))
9320        (t
9321         (let* ((pto-group (gnus-group-prefixed-name
9322                            (car art-group) to-method))
9323                (info (gnus-get-info pto-group))
9324                (to-group (gnus-info-group info))
9325                to-marks)
9326           ;; Update the group that has been moved to.
9327           (when (and info
9328                      (memq action '(move copy)))
9329             (unless (member to-group to-groups)
9330               (push to-group to-groups))
9331
9332             (unless (memq article gnus-newsgroup-unreads)
9333               (push 'read to-marks)
9334               (gnus-info-set-read
9335                info (gnus-add-to-range (gnus-info-read info)
9336                                        (list (cdr art-group)))))
9337
9338             ;; See whether the article is to be put in the cache.
9339             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9340                              gnus-article-mark-lists
9341                            (delete '(expirable . expire)
9342                                    (copy-sequence gnus-article-mark-lists))))
9343                   (to-article (cdr art-group)))
9344
9345               ;; Enter the article into the cache in the new group,
9346               ;; if that is required.
9347               (when gnus-use-cache
9348                 (gnus-cache-possibly-enter-article
9349                  to-group to-article
9350                  (let ((header (copy-sequence
9351                                 (gnus-summary-article-header article))))
9352                    (mail-header-set-number header to-article)
9353                    header)
9354                  (memq article gnus-newsgroup-marked)
9355                  (memq article gnus-newsgroup-dormant)
9356                  (memq article gnus-newsgroup-unreads)))
9357
9358               (when gnus-preserve-marks
9359                 ;; Copy any marks over to the new group.
9360                 (when (and (equal to-group gnus-newsgroup-name)
9361                            (not (memq article gnus-newsgroup-unreads)))
9362                   ;; Mark this article as read in this group.
9363                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9364                   (setcdr (gnus-active to-group) to-article)
9365                   (setcdr gnus-newsgroup-active to-article))
9366
9367                 (while marks
9368                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9369                     (when (memq article (symbol-value
9370                                          (intern (format "gnus-newsgroup-%s"
9371                                                          (caar marks)))))
9372                       (push (cdar marks) to-marks)
9373                       ;; If the other group is the same as this group,
9374                       ;; then we have to add the mark to the list.
9375                       (when (equal to-group gnus-newsgroup-name)
9376                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9377                              (cons to-article
9378                                    (symbol-value
9379                                     (intern (format "gnus-newsgroup-%s"
9380                                                     (caar marks)))))))
9381                       ;; Copy the marks to other group.
9382                       (gnus-add-marked-articles
9383                        to-group (cdar marks) (list to-article) info)))
9384                   (setq marks (cdr marks)))
9385
9386                 (gnus-request-set-mark
9387                  to-group (list (list (list to-article) 'add to-marks))))
9388
9389               (gnus-dribble-enter
9390                (concat "(gnus-group-set-info '"
9391                        (gnus-prin1-to-string (gnus-get-info to-group))
9392                        ")"))))
9393
9394           ;; Update the Xref header in this article to point to
9395           ;; the new crossposted article we have just created.
9396           (when (eq action 'crosspost)
9397             (save-excursion
9398               (set-buffer copy-buf)
9399               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9400               (nnheader-replace-header "Xref" new-xref)
9401               (gnus-request-replace-article
9402                article gnus-newsgroup-name (current-buffer))))
9403
9404           ;; run the move/copy/crosspost/respool hook
9405           (run-hook-with-args 'gnus-summary-article-move-hook
9406                               action
9407                               (gnus-data-header
9408                                (assoc article (gnus-data-list nil)))
9409                               gnus-newsgroup-name
9410                               to-newsgroup
9411                               select-method))
9412
9413         ;;;!!!Why is this necessary?
9414         (set-buffer gnus-summary-buffer)
9415         
9416         (gnus-summary-goto-subject article)
9417         (when (eq action 'move)
9418           (gnus-summary-mark-article article gnus-canceled-mark))))
9419       (push article articles-to-update-marks))
9420
9421     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9422     ;; Re-activate all groups that have been moved to.
9423     (save-excursion
9424       (set-buffer gnus-group-buffer)
9425       (let ((gnus-group-marked to-groups))
9426         (gnus-group-get-new-news-this-group nil t)))
9427     
9428     (gnus-kill-buffer copy-buf)
9429     (gnus-summary-position-point)
9430     (gnus-set-mode-line 'summary)))
9431
9432 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9433   "Copy the current article to some other group.
9434 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9435 When called interactively, if TO-NEWSGROUP is nil, use the value of
9436 the variable `gnus-move-split-methods' for finding a default target
9437 newsgroup.
9438 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9439 re-spool using this method."
9440   (interactive "P")
9441   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9442
9443 (defun gnus-summary-crosspost-article (&optional n)
9444   "Crosspost the current article to some other group."
9445   (interactive "P")
9446   (gnus-summary-move-article n nil nil 'crosspost))
9447
9448 (defcustom gnus-summary-respool-default-method nil
9449   "Default method type for respooling an article.
9450 If nil, use to the current newsgroup method."
9451   :type 'symbol
9452   :group 'gnus-summary-mail)
9453
9454 (defcustom gnus-summary-display-while-building nil
9455   "If non-nil, show and update the summary buffer as it's being built.
9456 If the value is t, update the buffer after every line is inserted.  If
9457 the value is an integer (N), update the display every N lines."
9458   :version "22.1"
9459   :group 'gnus-thread
9460   :type '(choice (const :tag "off" nil)
9461                  number
9462                  (const :tag "frequently" t)))
9463
9464 (defun gnus-summary-respool-article (&optional n method)
9465   "Respool the current article.
9466 The article will be squeezed through the mail spooling process again,
9467 which means that it will be put in some mail newsgroup or other
9468 depending on `nnmail-split-methods'.
9469 If N is a positive number, respool the N next articles.
9470 If N is a negative number, respool the N previous articles.
9471 If N is nil and any articles have been marked with the process mark,
9472 respool those articles instead.
9473
9474 Respooling can be done both from mail groups and \"real\" newsgroups.
9475 In the former case, the articles in question will be moved from the
9476 current group into whatever groups they are destined to.  In the
9477 latter case, they will be copied into the relevant groups."
9478   (interactive
9479    (list current-prefix-arg
9480          (let* ((methods (gnus-methods-using 'respool))
9481                 (methname
9482                  (symbol-name (or gnus-summary-respool-default-method
9483                                   (car (gnus-find-method-for-group
9484                                         gnus-newsgroup-name)))))
9485                 (method
9486                  (gnus-completing-read-with-default
9487                   methname "What backend do you want to use when respooling?"
9488                   methods nil t nil 'gnus-mail-method-history))
9489                 ms)
9490            (cond
9491             ((zerop (length (setq ms (gnus-servers-using-backend
9492                                       (intern method)))))
9493              (list (intern method) ""))
9494             ((= 1 (length ms))
9495              (car ms))
9496             (t
9497              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9498                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9499                            ms-alist))))))))
9500   (unless method
9501     (error "No method given for respooling"))
9502   (if (assoc (symbol-name
9503               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9504              (gnus-methods-using 'respool))
9505       (gnus-summary-move-article n nil method)
9506     (gnus-summary-copy-article n nil method)))
9507
9508 (defun gnus-summary-import-article (file &optional edit)
9509   "Import an arbitrary file into a mail newsgroup."
9510   (interactive "fImport file: \nP")
9511   (let ((group gnus-newsgroup-name)
9512         (now (current-time))
9513         atts lines group-art)
9514     (unless (gnus-check-backend-function 'request-accept-article group)
9515       (error "%s does not support article importing" group))
9516     (or (file-readable-p file)
9517         (not (file-regular-p file))
9518         (error "Can't read %s" file))
9519     (save-excursion
9520       (set-buffer (gnus-get-buffer-create " *import file*"))
9521       (erase-buffer)
9522       (nnheader-insert-file-contents file)
9523       (goto-char (point-min))
9524       (if (nnheader-article-p)
9525           (save-restriction
9526             (goto-char (point-min))
9527             (search-forward "\n\n" nil t)
9528             (narrow-to-region (point-min) (1- (point)))
9529             (goto-char (point-min))
9530             (unless (re-search-forward "^date:" nil t)
9531               (goto-char (point-max))
9532               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9533         ;; This doesn't look like an article, so we fudge some headers.
9534         (setq atts (file-attributes file)
9535               lines (count-lines (point-min) (point-max)))
9536         (insert "From: " (read-string "From: ") "\n"
9537                 "Subject: " (read-string "Subject: ") "\n"
9538                 "Date: " (message-make-date (nth 5 atts)) "\n"
9539                 "Message-ID: " (message-make-message-id) "\n"
9540                 "Lines: " (int-to-string lines) "\n"
9541                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9542       (setq group-art (gnus-request-accept-article group nil t))
9543       (kill-buffer (current-buffer)))
9544     (setq gnus-newsgroup-active (gnus-activate-group group))
9545     (forward-line 1)
9546     (gnus-summary-goto-article (cdr group-art) nil t)
9547     (when edit
9548       (gnus-summary-edit-article))))
9549
9550 (defun gnus-summary-create-article ()
9551   "Create an article in a mail newsgroup."
9552   (interactive)
9553   (let ((group gnus-newsgroup-name)
9554         (now (current-time))
9555         group-art)
9556     (unless (gnus-check-backend-function 'request-accept-article group)
9557       (error "%s does not support article importing" group))
9558     (save-excursion
9559       (set-buffer (gnus-get-buffer-create " *import file*"))
9560       (erase-buffer)
9561       (goto-char (point-min))
9562       ;; This doesn't look like an article, so we fudge some headers.
9563       (insert "From: " (read-string "From: ") "\n"
9564               "Subject: " (read-string "Subject: ") "\n"
9565               "Date: " (message-make-date now) "\n"
9566               "Message-ID: " (message-make-message-id) "\n")
9567       (setq group-art (gnus-request-accept-article group nil t))
9568       (kill-buffer (current-buffer)))
9569     (setq gnus-newsgroup-active (gnus-activate-group group))
9570     (forward-line 1)
9571     (gnus-summary-goto-article (cdr group-art) nil t)
9572     (gnus-summary-edit-article)))
9573
9574 (defun gnus-summary-article-posted-p ()
9575   "Say whether the current (mail) article is available from news as well.
9576 This will be the case if the article has both been mailed and posted."
9577   (interactive)
9578   (let ((id (mail-header-references (gnus-summary-article-header)))
9579         (gnus-override-method (car (gnus-refer-article-methods))))
9580     (if (gnus-request-head id "")
9581         (gnus-message 2 "The current message was found on %s"
9582                       gnus-override-method)
9583       (gnus-message 2 "The current message couldn't be found on %s"
9584                     gnus-override-method)
9585       nil)))
9586
9587 (defun gnus-summary-expire-articles (&optional now)
9588   "Expire all articles that are marked as expirable in the current group."
9589   (interactive)
9590   (when (and (not gnus-group-is-exiting-without-update-p)
9591              (gnus-check-backend-function
9592               'request-expire-articles gnus-newsgroup-name))
9593     ;; This backend supports expiry.
9594     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9595            (expirable (if total
9596                           (progn
9597                             ;; We need to update the info for
9598                             ;; this group for `gnus-list-of-read-articles'
9599                             ;; to give us the right answer.
9600                             (gnus-run-hooks 'gnus-exit-group-hook)
9601                             (gnus-summary-update-info)
9602                             (gnus-list-of-read-articles gnus-newsgroup-name))
9603                         (setq gnus-newsgroup-expirable
9604                               (sort gnus-newsgroup-expirable '<))))
9605            (expiry-wait (if now 'immediate
9606                           (gnus-group-find-parameter
9607                            gnus-newsgroup-name 'expiry-wait)))
9608            (nnmail-expiry-target
9609             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9610                 nnmail-expiry-target))
9611            es)
9612       (when expirable
9613         ;; There are expirable articles in this group, so we run them
9614         ;; through the expiry process.
9615         (gnus-message 6 "Expiring articles...")
9616         (unless (gnus-check-group gnus-newsgroup-name)
9617           (error "Can't open server for %s" gnus-newsgroup-name))
9618         ;; The list of articles that weren't expired is returned.
9619         (save-excursion
9620           (if expiry-wait
9621               (let ((nnmail-expiry-wait-function nil)
9622                     (nnmail-expiry-wait expiry-wait))
9623                 (setq es (gnus-request-expire-articles
9624                           expirable gnus-newsgroup-name)))
9625             (setq es (gnus-request-expire-articles
9626                       expirable gnus-newsgroup-name)))
9627           (unless total
9628             (setq gnus-newsgroup-expirable es))
9629           ;; We go through the old list of expirable, and mark all
9630           ;; really expired articles as nonexistent.
9631           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9632             (let ((gnus-use-cache nil))
9633               (dolist (article expirable)
9634                 (when (and (not (memq article es))
9635                            (gnus-data-find article))
9636                   (gnus-summary-mark-article article gnus-canceled-mark)
9637                   (run-hook-with-args 'gnus-summary-article-expire-hook
9638                                       'delete
9639                                       (gnus-data-header
9640                                        (assoc article (gnus-data-list nil)))
9641                                       gnus-newsgroup-name
9642                                       nil
9643                                       nil))))))
9644         (gnus-message 6 "Expiring articles...done")))))
9645
9646 (defun gnus-summary-expire-articles-now ()
9647   "Expunge all expirable articles in the current group.
9648 This means that *all* articles that are marked as expirable will be
9649 deleted forever, right now."
9650   (interactive)
9651   (or gnus-expert-user
9652       (gnus-yes-or-no-p
9653        "Are you really, really, really sure you want to delete all these messages? ")
9654       (error "Phew!"))
9655   (gnus-summary-expire-articles t))
9656
9657 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9658 (defun gnus-summary-delete-article (&optional n)
9659   "Delete the N next (mail) articles.
9660 This command actually deletes articles.  This is not a marking
9661 command.  The article will disappear forever from your life, never to
9662 return.
9663
9664 If N is negative, delete backwards.
9665 If N is nil and articles have been marked with the process mark,
9666 delete these instead.
9667
9668 If `gnus-novice-user' is non-nil you will be asked for
9669 confirmation before the articles are deleted."
9670   (interactive "P")
9671   (unless (gnus-check-backend-function 'request-expire-articles
9672                                        gnus-newsgroup-name)
9673     (error "The current newsgroup does not support article deletion"))
9674   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9675     (error "Couldn't open server"))
9676   ;; Compute the list of articles to delete.
9677   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9678         (nnmail-expiry-target 'delete)
9679         not-deleted)
9680     (if (and gnus-novice-user
9681              (not (gnus-yes-or-no-p
9682                    (format "Do you really want to delete %s forever? "
9683                            (if (> (length articles) 1)
9684                                (format "these %s articles" (length articles))
9685                              "this article")))))
9686         ()
9687       ;; Delete the articles.
9688       (setq not-deleted (gnus-request-expire-articles
9689                          articles gnus-newsgroup-name 'force))
9690       (while articles
9691         (gnus-summary-remove-process-mark (car articles))
9692         ;; The backend might not have been able to delete the article
9693         ;; after all.
9694         (unless (memq (car articles) not-deleted)
9695           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9696         (let* ((article (car articles))
9697                (ghead  (gnus-data-header
9698                                     (assoc article (gnus-data-list nil)))))
9699           (run-hook-with-args 'gnus-summary-article-delete-hook
9700                               'delete ghead gnus-newsgroup-name nil
9701                               nil))
9702         (setq articles (cdr articles)))
9703       (when not-deleted
9704         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9705     (gnus-summary-position-point)
9706     (gnus-set-mode-line 'summary)
9707     not-deleted))
9708
9709 (defun gnus-summary-edit-article (&optional force)
9710   "Edit the current article.
9711 This will have permanent effect only in mail groups.
9712 If FORCE is non-nil, allow editing of articles even in read-only
9713 groups."
9714   (interactive "P")
9715   (save-excursion
9716     (set-buffer gnus-summary-buffer)
9717     (let ((mail-parse-charset gnus-newsgroup-charset)
9718           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9719       (gnus-set-global-variables)
9720       (when (and (not force)
9721                  (gnus-group-read-only-p))
9722         (error "The current newsgroup does not support article editing"))
9723       (gnus-summary-show-article t)
9724       (gnus-article-edit-article
9725        'ignore
9726        `(lambda (no-highlight)
9727           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9728                 (message-options message-options)
9729                 (message-options-set-recipient)
9730                 (mail-parse-ignored-charsets
9731                  ',gnus-newsgroup-ignored-charsets))
9732             (gnus-summary-edit-article-done
9733              ,(or (mail-header-references gnus-current-headers) "")
9734              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9735
9736 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9737
9738 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9739                                                  no-highlight)
9740   "Make edits to the current article permanent."
9741   (interactive)
9742   (save-excursion
9743     ;; The buffer restriction contains the entire article if it exists.
9744     (when (article-goto-body)
9745       (let ((lines (count-lines (point) (point-max)))
9746             (length (- (point-max) (point)))
9747             (case-fold-search t)
9748             (body (copy-marker (point))))
9749         (goto-char (point-min))
9750         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9751           (delete-region (match-beginning 1) (match-end 1))
9752           (insert (number-to-string length)))
9753         (goto-char (point-min))
9754         (when (re-search-forward
9755                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9756           (delete-region (match-beginning 1) (match-end 1))
9757           (insert (number-to-string length)))
9758         (goto-char (point-min))
9759         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9760           (delete-region (match-beginning 1) (match-end 1))
9761           (insert (number-to-string lines))))))
9762   ;; Replace the article.
9763   (let ((buf (current-buffer)))
9764     (with-temp-buffer
9765       (insert-buffer-substring buf)
9766
9767       (if (and (not read-only)
9768                (not (gnus-request-replace-article
9769                      (cdr gnus-article-current) (car gnus-article-current)
9770                      (current-buffer) t)))
9771           (error "Couldn't replace article")
9772         ;; Update the summary buffer.
9773         (if (and references
9774                  (equal (message-tokenize-header references " ")
9775                         (message-tokenize-header
9776                          (or (message-fetch-field "references") "") " ")))
9777             ;; We only have to update this line.
9778             (save-excursion
9779               (save-restriction
9780                 (message-narrow-to-head)
9781                 (let ((head (buffer-string))
9782                       header)
9783                   (with-temp-buffer
9784                     (insert (format "211 %d Article retrieved.\n"
9785                                     (cdr gnus-article-current)))
9786                     (insert head)
9787                     (insert ".\n")
9788                     (let ((nntp-server-buffer (current-buffer)))
9789                       (setq header (car (gnus-get-newsgroup-headers
9790                                          nil t))))
9791                     (save-excursion
9792                       (set-buffer gnus-summary-buffer)
9793                       (gnus-data-set-header
9794                        (gnus-data-find (cdr gnus-article-current))
9795                        header)
9796                       (gnus-summary-update-article-line
9797                        (cdr gnus-article-current) header)
9798                       (if (gnus-summary-goto-subject
9799                            (cdr gnus-article-current) nil t)
9800                           (gnus-summary-update-secondary-mark
9801                            (cdr gnus-article-current))))))))
9802           ;; Update threads.
9803           (set-buffer (or buffer gnus-summary-buffer))
9804           (gnus-summary-update-article (cdr gnus-article-current))
9805           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9806               (gnus-summary-update-secondary-mark
9807                (cdr gnus-article-current))))
9808         ;; Prettify the article buffer again.
9809         (unless no-highlight
9810           (save-excursion
9811             (set-buffer gnus-article-buffer)
9812             ;;;!!! Fix this -- article should be rehighlighted.
9813             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9814             (set-buffer gnus-original-article-buffer)
9815             (gnus-request-article
9816              (cdr gnus-article-current)
9817              (car gnus-article-current) (current-buffer))))
9818         ;; Prettify the summary buffer line.
9819         (when (gnus-visual-p 'summary-highlight 'highlight)
9820           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9821
9822 (defun gnus-summary-edit-wash (key)
9823   "Perform editing command KEY in the article buffer."
9824   (interactive
9825    (list
9826     (progn
9827       (message "%s" (concat (this-command-keys) "- "))
9828       (read-char))))
9829   (message "")
9830   (gnus-summary-edit-article)
9831   (execute-kbd-macro (concat (this-command-keys) key))
9832   (gnus-article-edit-done))
9833
9834 ;;; Respooling
9835
9836 (defun gnus-summary-respool-query (&optional silent trace)
9837   "Query where the respool algorithm would put this article."
9838   (interactive)
9839   (let (gnus-mark-article-hook)
9840     (gnus-summary-select-article)
9841     (save-excursion
9842       (set-buffer gnus-original-article-buffer)
9843       (let ((groups (nnmail-article-group 'identity trace)))
9844         (unless silent
9845           (if groups
9846               (message "This message would go to %s"
9847                        (mapconcat 'car groups ", "))
9848             (message "This message would go to no groups"))
9849           groups)))))
9850
9851 (defun gnus-summary-respool-trace ()
9852   "Trace where the respool algorithm would put this article.
9853 Display a buffer showing all fancy splitting patterns which matched."
9854   (interactive)
9855   (gnus-summary-respool-query nil t))
9856
9857 ;; Summary marking commands.
9858
9859 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9860   "Mark articles which has the same subject as read, and then select the next.
9861 If UNMARK is positive, remove any kind of mark.
9862 If UNMARK is negative, tick articles."
9863   (interactive "P")
9864   (when unmark
9865     (setq unmark (prefix-numeric-value unmark)))
9866   (let ((count
9867          (gnus-summary-mark-same-subject
9868           (gnus-summary-article-subject) unmark)))
9869     ;; Select next unread article.  If auto-select-same mode, should
9870     ;; select the first unread article.
9871     (gnus-summary-next-article t (and gnus-auto-select-same
9872                                       (gnus-summary-article-subject)))
9873     (gnus-message 7 "%d article%s marked as %s"
9874                   count (if (= count 1) " is" "s are")
9875                   (if unmark "unread" "read"))))
9876
9877 (defun gnus-summary-kill-same-subject (&optional unmark)
9878   "Mark articles which has the same subject as read.
9879 If UNMARK is positive, remove any kind of mark.
9880 If UNMARK is negative, tick articles."
9881   (interactive "P")
9882   (when unmark
9883     (setq unmark (prefix-numeric-value unmark)))
9884   (let ((count
9885          (gnus-summary-mark-same-subject
9886           (gnus-summary-article-subject) unmark)))
9887     ;; If marked as read, go to next unread subject.
9888     (when (null unmark)
9889       ;; Go to next unread subject.
9890       (gnus-summary-next-subject 1 t))
9891     (gnus-message 7 "%d articles are marked as %s"
9892                   count (if unmark "unread" "read"))))
9893
9894 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9895   "Mark articles with same SUBJECT as read, and return marked number.
9896 If optional argument UNMARK is positive, remove any kinds of marks.
9897 If optional argument UNMARK is negative, mark articles as unread instead."
9898   (let ((count 1))
9899     (save-excursion
9900       (cond
9901        ((null unmark)                   ; Mark as read.
9902         (while (and
9903                 (progn
9904                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9905                   (gnus-summary-show-thread) t)
9906                 (gnus-summary-find-subject subject))
9907           (setq count (1+ count))))
9908        ((> unmark 0)                    ; Tick.
9909         (while (and
9910                 (progn
9911                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9912                   (gnus-summary-show-thread) t)
9913                 (gnus-summary-find-subject subject))
9914           (setq count (1+ count))))
9915        (t                               ; Mark as unread.
9916         (while (and
9917                 (progn
9918                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9919                   (gnus-summary-show-thread) t)
9920                 (gnus-summary-find-subject subject))
9921           (setq count (1+ count)))))
9922       (gnus-set-mode-line 'summary)
9923       ;; Return the number of marked articles.
9924       count)))
9925
9926 (defun gnus-summary-mark-as-processable (n &optional unmark)
9927   "Set the process mark on the next N articles.
9928 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9929 the process mark instead.  The difference between N and the actual
9930 number of articles marked is returned."
9931   (interactive "P")
9932   (if (and (null n) (gnus-region-active-p))
9933       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9934     (setq n (prefix-numeric-value n))
9935     (let ((backward (< n 0))
9936           (n (abs n)))
9937       (while (and
9938               (> n 0)
9939               (if unmark
9940                   (gnus-summary-remove-process-mark
9941                    (gnus-summary-article-number))
9942                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9943               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9944         (setq n (1- n)))
9945       (when (/= 0 n)
9946         (gnus-message 7 "No more articles"))
9947       (gnus-summary-recenter)
9948       (gnus-summary-position-point)
9949       n)))
9950
9951 (defun gnus-summary-unmark-as-processable (n)
9952   "Remove the process mark from the next N articles.
9953 If N is negative, unmark backward instead.  The difference between N and
9954 the actual number of articles unmarked is returned."
9955   (interactive "P")
9956   (gnus-summary-mark-as-processable n t))
9957
9958 (defun gnus-summary-unmark-all-processable ()
9959   "Remove the process mark from all articles."
9960   (interactive)
9961   (save-excursion
9962     (while gnus-newsgroup-processable
9963       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9964   (gnus-summary-position-point))
9965
9966 (defun gnus-summary-add-mark (article type)
9967   "Mark ARTICLE with a mark of TYPE."
9968   (let ((vtype (car (assq type gnus-article-mark-lists)))
9969         var)
9970     (if (not vtype)
9971         (error "No such mark type: %s" type)
9972       (setq var (intern (format "gnus-newsgroup-%s" type)))
9973       (set var (cons article (symbol-value var)))
9974       (if (memq type '(processable cached replied forwarded recent saved))
9975           (gnus-summary-update-secondary-mark article)
9976         ;;; !!! This is bogus.  We should find out what primary
9977         ;;; !!! mark we want to set.
9978         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9979
9980 (defun gnus-summary-mark-as-expirable (n)
9981   "Mark N articles forward as expirable.
9982 If N is negative, mark backward instead.  The difference between N and
9983 the actual number of articles marked is returned."
9984   (interactive "p")
9985   (gnus-summary-mark-forward n gnus-expirable-mark))
9986
9987 (defun gnus-summary-mark-as-spam (n)
9988   "Mark N articles forward as spam.
9989 If N is negative, mark backward instead.  The difference between N and
9990 the actual number of articles marked is returned."
9991   (interactive "p")
9992   (gnus-summary-mark-forward n gnus-spam-mark))
9993
9994 (defun gnus-summary-mark-article-as-replied (article)
9995   "Mark ARTICLE as replied to and update the summary line.
9996 ARTICLE can also be a list of articles."
9997   (interactive (list (gnus-summary-article-number)))
9998   (let ((articles (if (listp article) article (list article))))
9999     (dolist (article articles)
10000       (unless (numberp article)
10001         (error "%s is not a number" article))
10002       (push article gnus-newsgroup-replied)
10003       (let ((buffer-read-only nil))
10004         (when (gnus-summary-goto-subject article nil t)
10005           (gnus-summary-update-secondary-mark article))))))
10006
10007 (defun gnus-summary-mark-article-as-forwarded (article)
10008   "Mark ARTICLE as forwarded and update the summary line.
10009 ARTICLE can also be a list of articles."
10010   (let ((articles (if (listp article) article (list article))))
10011     (dolist (article articles)
10012       (push article gnus-newsgroup-forwarded)
10013       (let ((buffer-read-only nil))
10014         (when (gnus-summary-goto-subject article nil t)
10015           (gnus-summary-update-secondary-mark article))))))
10016
10017 (defun gnus-summary-set-bookmark (article)
10018   "Set a bookmark in current article."
10019   (interactive (list (gnus-summary-article-number)))
10020   (when (or (not (get-buffer gnus-article-buffer))
10021             (not gnus-current-article)
10022             (not gnus-article-current)
10023             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10024     (error "No current article selected"))
10025   ;; Remove old bookmark, if one exists.
10026   (gnus-pull article gnus-newsgroup-bookmarks)
10027   ;; Set the new bookmark, which is on the form
10028   ;; (article-number . line-number-in-body).
10029   (push
10030    (cons article
10031          (with-current-buffer gnus-article-buffer
10032            (count-lines
10033             (min (point)
10034                  (save-excursion
10035                    (article-goto-body)
10036                    (point)))
10037             (point))))
10038    gnus-newsgroup-bookmarks)
10039   (gnus-message 6 "A bookmark has been added to the current article."))
10040
10041 (defun gnus-summary-remove-bookmark (article)
10042   "Remove the bookmark from the current article."
10043   (interactive (list (gnus-summary-article-number)))
10044   ;; Remove old bookmark, if one exists.
10045   (if (not (assq article gnus-newsgroup-bookmarks))
10046       (gnus-message 6 "No bookmark in current article.")
10047     (gnus-pull article gnus-newsgroup-bookmarks)
10048     (gnus-message 6 "Removed bookmark.")))
10049
10050 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10051 (defun gnus-summary-mark-as-dormant (n)
10052   "Mark N articles forward as dormant.
10053 If N is negative, mark backward instead.  The difference between N and
10054 the actual number of articles marked is returned."
10055   (interactive "p")
10056   (gnus-summary-mark-forward n gnus-dormant-mark))
10057
10058 (defun gnus-summary-set-process-mark (article)
10059   "Set the process mark on ARTICLE and update the summary line."
10060   (setq gnus-newsgroup-processable
10061         (cons article
10062               (delq article gnus-newsgroup-processable)))
10063   (when (gnus-summary-goto-subject article)
10064     (gnus-summary-show-thread)
10065     (gnus-summary-goto-subject article)
10066     (gnus-summary-update-secondary-mark article)))
10067
10068 (defun gnus-summary-remove-process-mark (&rest articles)
10069   "Remove the process mark from ARTICLES and update the summary line."
10070   (dolist (article articles)
10071     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10072     (when (gnus-summary-goto-subject article)
10073       (gnus-summary-show-thread)
10074       (gnus-summary-goto-subject article)
10075       (gnus-summary-update-secondary-mark article))))
10076
10077 (defun gnus-summary-set-saved-mark (article)
10078   "Set the process mark on ARTICLE and update the summary line."
10079   (push article gnus-newsgroup-saved)
10080   (when (gnus-summary-goto-subject article)
10081     (gnus-summary-update-secondary-mark article)))
10082
10083 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10084   "Mark N articles as read forwards.
10085 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10086 The difference between N and the actual number of articles marked is
10087 returned.
10088 If NO-EXPIRE, auto-expiry will be inhibited."
10089   (interactive "p")
10090   (gnus-summary-show-thread)
10091   (let ((backward (< n 0))
10092         (gnus-summary-goto-unread
10093          (and gnus-summary-goto-unread
10094               (not (eq gnus-summary-goto-unread 'never))
10095               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10096                                     gnus-ticked-mark gnus-dormant-mark)))))
10097         (n (abs n))
10098         (mark (or mark gnus-del-mark)))
10099     (while (and (> n 0)
10100                 (gnus-summary-mark-article nil mark no-expire)
10101                 (zerop (gnus-summary-next-subject
10102                         (if backward -1 1)
10103                         (and gnus-summary-goto-unread
10104                              (not (eq gnus-summary-goto-unread 'never)))
10105                         t)))
10106       (setq n (1- n)))
10107     (when (/= 0 n)
10108       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10109     (gnus-summary-recenter)
10110     (gnus-summary-position-point)
10111     (gnus-set-mode-line 'summary)
10112     n))
10113
10114 (defun gnus-summary-mark-article-as-read (mark)
10115   "Mark the current article quickly as read with MARK."
10116   (let ((article (gnus-summary-article-number)))
10117     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10118     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10119     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10120     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10121     (push (cons article mark) gnus-newsgroup-reads)
10122     ;; Possibly remove from cache, if that is used.
10123     (when gnus-use-cache
10124       (gnus-cache-enter-remove-article article))
10125     ;; Allow the backend to change the mark.
10126     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10127     ;; Check for auto-expiry.
10128     (when (and gnus-newsgroup-auto-expire
10129                (memq mark gnus-auto-expirable-marks))
10130       (setq mark gnus-expirable-mark)
10131       ;; Let the backend know about the mark change.
10132       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10133       (push article gnus-newsgroup-expirable))
10134     ;; Set the mark in the buffer.
10135     (gnus-summary-update-mark mark 'unread)
10136     t))
10137
10138 (defun gnus-summary-mark-article-as-unread (mark)
10139   "Mark the current article quickly as unread with MARK."
10140   (let* ((article (gnus-summary-article-number))
10141          (old-mark (gnus-summary-article-mark article)))
10142     ;; Allow the backend to change the mark.
10143     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10144     (if (eq mark old-mark)
10145         t
10146       (if (<= article 0)
10147           (progn
10148             (gnus-error 1 "Can't mark negative article numbers")
10149             nil)
10150         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10151         (setq gnus-newsgroup-spam-marked
10152               (delq article gnus-newsgroup-spam-marked))
10153         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10154         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10155         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10156         (cond ((= mark gnus-ticked-mark)
10157                (setq gnus-newsgroup-marked
10158                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10159                                               article)))
10160               ((= mark gnus-spam-mark)
10161                (setq gnus-newsgroup-spam-marked
10162                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10163                                               article)))
10164               ((= mark gnus-dormant-mark)
10165                (setq gnus-newsgroup-dormant
10166                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10167                                               article)))
10168               (t
10169                (setq gnus-newsgroup-unreads
10170                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10171                                               article))))
10172         (gnus-pull article gnus-newsgroup-reads)
10173
10174         ;; See whether the article is to be put in the cache.
10175         (and gnus-use-cache
10176              (vectorp (gnus-summary-article-header article))
10177              (save-excursion
10178                (gnus-cache-possibly-enter-article
10179                 gnus-newsgroup-name article
10180                 (gnus-summary-article-header article)
10181                 (= mark gnus-ticked-mark)
10182                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10183
10184         ;; Fix the mark.
10185         (gnus-summary-update-mark mark 'unread)
10186         t))))
10187
10188 (defun gnus-summary-mark-article (&optional article mark no-expire)
10189   "Mark ARTICLE with MARK.  MARK can be any character.
10190 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10191 `??' (dormant) and `?E' (expirable).
10192 If MARK is nil, then the default character `?r' is used.
10193 If ARTICLE is nil, then the article on the current line will be
10194 marked.
10195 If NO-EXPIRE, auto-expiry will be inhibited."
10196   ;; The mark might be a string.
10197   (when (stringp mark)
10198     (setq mark (aref mark 0)))
10199   ;; If no mark is given, then we check auto-expiring.
10200   (when (null mark)
10201     (setq mark gnus-del-mark))
10202   (when (and (not no-expire)
10203              gnus-newsgroup-auto-expire
10204              (memq mark gnus-auto-expirable-marks))
10205     (setq mark gnus-expirable-mark))
10206   (let ((article (or article (gnus-summary-article-number)))
10207         (old-mark (gnus-summary-article-mark article)))
10208     ;; Allow the backend to change the mark.
10209     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10210     (if (eq mark old-mark)
10211         t
10212       (unless article
10213         (error "No article on current line"))
10214       (if (not (if (or (= mark gnus-unread-mark)
10215                        (= mark gnus-ticked-mark)
10216                        (= mark gnus-spam-mark)
10217                        (= mark gnus-dormant-mark))
10218                    (gnus-mark-article-as-unread article mark)
10219                  (gnus-mark-article-as-read article mark)))
10220           t
10221         ;; See whether the article is to be put in the cache.
10222         (and gnus-use-cache
10223              (not (= mark gnus-canceled-mark))
10224              (vectorp (gnus-summary-article-header article))
10225              (save-excursion
10226                (gnus-cache-possibly-enter-article
10227                 gnus-newsgroup-name article
10228                 (gnus-summary-article-header article)
10229                 (= mark gnus-ticked-mark)
10230                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10231
10232         (when (gnus-summary-goto-subject article nil t)
10233           (let ((buffer-read-only nil))
10234             (gnus-summary-show-thread)
10235             ;; Fix the mark.
10236             (gnus-summary-update-mark mark 'unread)
10237             t))))))
10238
10239 (defun gnus-summary-update-secondary-mark (article)
10240   "Update the secondary (read, process, cache) mark."
10241   (gnus-summary-update-mark
10242    (cond ((memq article gnus-newsgroup-processable)
10243           gnus-process-mark)
10244          ((memq article gnus-newsgroup-cached)
10245           gnus-cached-mark)
10246          ((memq article gnus-newsgroup-replied)
10247           gnus-replied-mark)
10248          ((memq article gnus-newsgroup-forwarded)
10249           gnus-forwarded-mark)
10250          ((memq article gnus-newsgroup-saved)
10251           gnus-saved-mark)
10252          ((memq article gnus-newsgroup-recent)
10253           gnus-recent-mark)
10254          ((memq article gnus-newsgroup-unseen)
10255           gnus-unseen-mark)
10256          (t gnus-no-mark))
10257    'replied)
10258   (when (gnus-visual-p 'summary-highlight 'highlight)
10259     (gnus-run-hooks 'gnus-summary-update-hook))
10260   t)
10261
10262 (defun gnus-summary-update-download-mark (article)
10263   "Update the download mark."
10264   (gnus-summary-update-mark
10265    (cond ((memq article gnus-newsgroup-undownloaded)
10266           gnus-undownloaded-mark)
10267          (gnus-newsgroup-agentized
10268           gnus-downloaded-mark)
10269          (t
10270           gnus-no-mark))
10271    'download)
10272   (gnus-summary-update-line t)
10273   t)
10274
10275 (defun gnus-summary-update-mark (mark type)
10276   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10277         (buffer-read-only nil))
10278     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10279     (when forward
10280       (when (looking-at "\r")
10281         (incf forward))
10282       (when (<= (+ forward (point)) (point-max))
10283         ;; Go to the right position on the line.
10284         (goto-char (+ forward (point)))
10285         ;; Replace the old mark with the new mark.
10286         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10287         ;; Optionally update the marks by some user rule.
10288         (when (eq type 'unread)
10289           (gnus-data-set-mark
10290            (gnus-data-find (gnus-summary-article-number)) mark)
10291           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10292
10293 (defun gnus-mark-article-as-read (article &optional mark)
10294   "Enter ARTICLE in the pertinent lists and remove it from others."
10295   ;; Make the article expirable.
10296   (let ((mark (or mark gnus-del-mark)))
10297     (setq gnus-newsgroup-expirable
10298           (if (= mark gnus-expirable-mark)
10299               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10300             (delq article gnus-newsgroup-expirable)))
10301     ;; Remove from unread and marked lists.
10302     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10303     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10304     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10305     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10306     (push (cons article mark) gnus-newsgroup-reads)
10307     ;; Possibly remove from cache, if that is used.
10308     (when gnus-use-cache
10309       (gnus-cache-enter-remove-article article))
10310     t))
10311
10312 (defun gnus-mark-article-as-unread (article &optional mark)
10313   "Enter ARTICLE in the pertinent lists and remove it from others."
10314   (let ((mark (or mark gnus-ticked-mark)))
10315     (if (<= article 0)
10316         (progn
10317           (gnus-error 1 "Can't mark negative article numbers")
10318           nil)
10319       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10320             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10321             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10322             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10323             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10324
10325       ;; Unsuppress duplicates?
10326       (when gnus-suppress-duplicates
10327         (gnus-dup-unsuppress-article article))
10328
10329       (cond ((= mark gnus-ticked-mark)
10330              (setq gnus-newsgroup-marked
10331                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10332             ((= mark gnus-spam-mark)
10333              (setq gnus-newsgroup-spam-marked
10334                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10335                                             article)))
10336             ((= mark gnus-dormant-mark)
10337              (setq gnus-newsgroup-dormant
10338                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10339             (t
10340              (setq gnus-newsgroup-unreads
10341                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10342       (gnus-pull article gnus-newsgroup-reads)
10343       t)))
10344
10345 (defalias 'gnus-summary-mark-as-unread-forward
10346   'gnus-summary-tick-article-forward)
10347 (make-obsolete 'gnus-summary-mark-as-unread-forward
10348                'gnus-summary-tick-article-forward)
10349 (defun gnus-summary-tick-article-forward (n)
10350   "Tick N articles forwards.
10351 If N is negative, tick backwards instead.
10352 The difference between N and the number of articles ticked is returned."
10353   (interactive "p")
10354   (gnus-summary-mark-forward n gnus-ticked-mark))
10355
10356 (defalias 'gnus-summary-mark-as-unread-backward
10357   'gnus-summary-tick-article-backward)
10358 (make-obsolete 'gnus-summary-mark-as-unread-backward
10359                'gnus-summary-tick-article-backward)
10360 (defun gnus-summary-tick-article-backward (n)
10361   "Tick N articles backwards.
10362 The difference between N and the number of articles ticked is returned."
10363   (interactive "p")
10364   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10365
10366 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10367 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10368 (defun gnus-summary-tick-article (&optional article clear-mark)
10369   "Mark current article as unread.
10370 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10371 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10372   (interactive)
10373   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10374                                        gnus-ticked-mark)))
10375
10376 (defun gnus-summary-mark-as-read-forward (n)
10377   "Mark N articles as read forwards.
10378 If N is negative, mark backwards instead.
10379 The difference between N and the actual number of articles marked is
10380 returned."
10381   (interactive "p")
10382   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10383
10384 (defun gnus-summary-mark-as-read-backward (n)
10385   "Mark the N articles as read backwards.
10386 The difference between N and the actual number of articles marked is
10387 returned."
10388   (interactive "p")
10389   (gnus-summary-mark-forward
10390    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10391
10392 (defun gnus-summary-mark-as-read (&optional article mark)
10393   "Mark current article as read.
10394 ARTICLE specifies the article to be marked as read.
10395 MARK specifies a string to be inserted at the beginning of the line."
10396   (gnus-summary-mark-article article mark))
10397
10398 (defun gnus-summary-clear-mark-forward (n)
10399   "Clear marks from N articles forward.
10400 If N is negative, clear backward instead.
10401 The difference between N and the number of marks cleared is returned."
10402   (interactive "p")
10403   (gnus-summary-mark-forward n gnus-unread-mark))
10404
10405 (defun gnus-summary-clear-mark-backward (n)
10406   "Clear marks from N articles backward.
10407 The difference between N and the number of marks cleared is returned."
10408   (interactive "p")
10409   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10410
10411 (defun gnus-summary-mark-unread-as-read ()
10412   "Intended to be used by `gnus-summary-mark-article-hook'."
10413   (when (memq gnus-current-article gnus-newsgroup-unreads)
10414     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10415
10416 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10417   "Intended to be used by `gnus-summary-mark-article-hook'."
10418   (let ((mark (gnus-summary-article-mark)))
10419     (when (or (gnus-unread-mark-p mark)
10420               (gnus-read-mark-p mark))
10421       (gnus-summary-mark-article gnus-current-article
10422                                  (or new-mark gnus-read-mark)))))
10423
10424 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10425   "Intended to be used by `gnus-summary-mark-article-hook'."
10426   (let ((mark (gnus-summary-article-mark)))
10427     (when (or (gnus-unread-mark-p mark)
10428               (gnus-read-mark-p mark))
10429       (gnus-summary-mark-article (gnus-summary-article-number)
10430                                  (or new-mark gnus-read-mark)))))
10431
10432 (defun gnus-summary-mark-unread-as-ticked ()
10433   "Intended to be used by `gnus-summary-mark-article-hook'."
10434   (when (memq gnus-current-article gnus-newsgroup-unreads)
10435     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10436
10437 (defun gnus-summary-mark-region-as-read (point mark all)
10438   "Mark all unread articles between point and mark as read.
10439 If given a prefix, mark all articles between point and mark as read,
10440 even ticked and dormant ones."
10441   (interactive "r\nP")
10442   (save-excursion
10443     (let (article)
10444       (goto-char point)
10445       (beginning-of-line)
10446       (while (and
10447               (< (point) mark)
10448               (progn
10449                 (when (or all
10450                           (memq (setq article (gnus-summary-article-number))
10451                                 gnus-newsgroup-unreads))
10452                   (gnus-summary-mark-article article gnus-del-mark))
10453                 t)
10454               (gnus-summary-find-next))))))
10455
10456 (defun gnus-summary-mark-below (score mark)
10457   "Mark articles with score less than SCORE with MARK."
10458   (interactive "P\ncMark: ")
10459   (setq score (if score
10460                   (prefix-numeric-value score)
10461                 (or gnus-summary-default-score 0)))
10462   (save-excursion
10463     (set-buffer gnus-summary-buffer)
10464     (goto-char (point-min))
10465     (while
10466         (progn
10467           (and (< (gnus-summary-article-score) score)
10468                (gnus-summary-mark-article nil mark))
10469           (gnus-summary-find-next)))))
10470
10471 (defun gnus-summary-kill-below (&optional score)
10472   "Mark articles with score below SCORE as read."
10473   (interactive "P")
10474   (gnus-summary-mark-below score gnus-killed-mark))
10475
10476 (defun gnus-summary-clear-above (&optional score)
10477   "Clear all marks from articles with score above SCORE."
10478   (interactive "P")
10479   (gnus-summary-mark-above score gnus-unread-mark))
10480
10481 (defun gnus-summary-tick-above (&optional score)
10482   "Tick all articles with score above SCORE."
10483   (interactive "P")
10484   (gnus-summary-mark-above score gnus-ticked-mark))
10485
10486 (defun gnus-summary-mark-above (score mark)
10487   "Mark articles with score over SCORE with MARK."
10488   (interactive "P\ncMark: ")
10489   (setq score (if score
10490                   (prefix-numeric-value score)
10491                 (or gnus-summary-default-score 0)))
10492   (save-excursion
10493     (set-buffer gnus-summary-buffer)
10494     (goto-char (point-min))
10495     (while (and (progn
10496                   (when (> (gnus-summary-article-score) score)
10497                     (gnus-summary-mark-article nil mark))
10498                   t)
10499                 (gnus-summary-find-next)))))
10500
10501 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10502 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10503 (defun gnus-summary-limit-include-expunged (&optional no-error)
10504   "Display all the hidden articles that were expunged for low scores."
10505   (interactive)
10506   (let ((buffer-read-only nil))
10507     (let ((scored gnus-newsgroup-scored)
10508           headers h)
10509       (while scored
10510         (unless (gnus-summary-article-header (caar scored))
10511           (and (setq h (gnus-number-to-header (caar scored)))
10512                (< (cdar scored) gnus-summary-expunge-below)
10513                (push h headers)))
10514         (setq scored (cdr scored)))
10515       (if (not headers)
10516           (when (not no-error)
10517             (error "No expunged articles hidden"))
10518         (goto-char (point-min))
10519         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10520         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10521         (mapcar (lambda (x) (push (mail-header-number x)
10522                                   gnus-newsgroup-limit))
10523                 headers)
10524         (gnus-summary-prepare-unthreaded (nreverse headers))
10525         (goto-char (point-min))
10526         (gnus-summary-position-point)
10527         t))))
10528
10529 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10530   "Mark all unread articles in this newsgroup as read.
10531 If prefix argument ALL is non-nil, ticked and dormant articles will
10532 also be marked as read.
10533 If QUIETLY is non-nil, no questions will be asked.
10534
10535 If TO-HERE is non-nil, it should be a point in the buffer.  All
10536 articles before (after, if REVERSE is set) this point will be marked
10537 as read.
10538
10539 Note that this function will only catch up the unread article
10540 in the current summary buffer limitation.
10541
10542 The number of articles marked as read is returned."
10543   (interactive "P")
10544   (prog1
10545       (save-excursion
10546         (when (or quietly
10547                   (not gnus-interactive-catchup) ;Without confirmation?
10548                   gnus-expert-user
10549                   (gnus-y-or-n-p
10550                    (if all
10551                        "Mark absolutely all articles as read? "
10552                      "Mark all unread articles as read? ")))
10553           (if (and not-mark
10554                    (not gnus-newsgroup-adaptive)
10555                    (not gnus-newsgroup-auto-expire)
10556                    (not gnus-suppress-duplicates)
10557                    (or (not gnus-use-cache)
10558                        (eq gnus-use-cache 'passive)))
10559               (progn
10560                 (when all
10561                   (setq gnus-newsgroup-marked nil
10562                         gnus-newsgroup-spam-marked nil
10563                         gnus-newsgroup-dormant nil))
10564                 (setq gnus-newsgroup-unreads
10565                       (gnus-sorted-nunion
10566                        (gnus-intersection gnus-newsgroup-unreads
10567                                           gnus-newsgroup-downloadable)
10568                        gnus-newsgroup-unfetched)))
10569             ;; We actually mark all articles as canceled, which we
10570             ;; have to do when using auto-expiry or adaptive scoring.
10571             (gnus-summary-show-all-threads)
10572             (if (and to-here reverse)
10573                 (progn
10574                   (goto-char to-here)
10575                   (gnus-summary-mark-current-read-and-unread-as-read
10576                    gnus-catchup-mark)
10577                   (while (gnus-summary-find-next (not all))
10578                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10579               (when (gnus-summary-first-subject (not all))
10580                 (while (and
10581                         (if to-here (< (point) to-here) t)
10582                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10583                         (gnus-summary-find-next (not all))))))
10584             (gnus-set-mode-line 'summary))
10585           t))
10586     (gnus-summary-position-point)))
10587
10588 (defun gnus-summary-catchup-to-here (&optional all)
10589   "Mark all unticked articles before the current one as read.
10590 If ALL is non-nil, also mark ticked and dormant articles as read."
10591   (interactive "P")
10592   (save-excursion
10593     (gnus-save-hidden-threads
10594       (let ((beg (point)))
10595         ;; We check that there are unread articles.
10596         (when (or all (gnus-summary-find-prev))
10597           (gnus-summary-catchup all t beg)))))
10598   (gnus-summary-position-point))
10599
10600 (defun gnus-summary-catchup-from-here (&optional all)
10601   "Mark all unticked articles after (and including) the current one as read.
10602 If ALL is non-nil, also mark ticked and dormant articles as read."
10603   (interactive "P")
10604   (save-excursion
10605     (gnus-save-hidden-threads
10606       (let ((beg (point)))
10607         ;; We check that there are unread articles.
10608         (when (or all (gnus-summary-find-next))
10609           (gnus-summary-catchup all t beg nil t)))))
10610   (gnus-summary-position-point))
10611
10612 (defun gnus-summary-catchup-all (&optional quietly)
10613   "Mark all articles in this newsgroup as read.
10614 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10615 instead, which marks only unread articles as read."
10616   (interactive "P")
10617   (gnus-summary-catchup t quietly))
10618
10619 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10620   "Mark all unread articles in this group as read, then exit.
10621 If prefix argument ALL is non-nil, all articles are marked as read.
10622 If QUIETLY is non-nil, no questions will be asked."
10623   (interactive "P")
10624   (when (gnus-summary-catchup all quietly nil 'fast)
10625     ;; Select next newsgroup or exit.
10626     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10627              (eq gnus-auto-select-next 'quietly))
10628         (gnus-summary-next-group nil)
10629       (gnus-summary-exit))))
10630
10631 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10632   "Mark all articles in this newsgroup as read, and then exit.
10633 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10634 instead, which marks only unread articles as read."
10635   (interactive "P")
10636   (gnus-summary-catchup-and-exit t quietly))
10637
10638 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10639   "Mark all articles in this group as read and select the next group.
10640 If given a prefix, mark all articles, unread as well as ticked, as
10641 read."
10642   (interactive "P")
10643   (save-excursion
10644     (gnus-summary-catchup all))
10645   (gnus-summary-next-group))
10646
10647 ;;;
10648 ;;; with article
10649 ;;;
10650
10651 (defmacro gnus-with-article (article &rest forms)
10652   "Select ARTICLE and perform FORMS in the original article buffer.
10653 Then replace the article with the result."
10654   `(progn
10655      ;; We don't want the article to be marked as read.
10656      (let (gnus-mark-article-hook)
10657        (gnus-summary-select-article t t nil ,article))
10658      (set-buffer gnus-original-article-buffer)
10659      ,@forms
10660      (if (not (gnus-check-backend-function
10661                'request-replace-article (car gnus-article-current)))
10662          (gnus-message 5 "Read-only group; not replacing")
10663        (unless (gnus-request-replace-article
10664                 ,article (car gnus-article-current)
10665                 (current-buffer) t)
10666          (error "Couldn't replace article")))
10667      ;; The cache and backlog have to be flushed somewhat.
10668      (when gnus-keep-backlog
10669        (gnus-backlog-remove-article
10670         (car gnus-article-current) (cdr gnus-article-current)))
10671      (when gnus-use-cache
10672        (gnus-cache-update-article
10673         (car gnus-article-current) (cdr gnus-article-current)))))
10674
10675 (put 'gnus-with-article 'lisp-indent-function 1)
10676 (put 'gnus-with-article 'edebug-form-spec '(form body))
10677
10678 ;; Thread-based commands.
10679
10680 (defun gnus-summary-articles-in-thread (&optional article)
10681   "Return a list of all articles in the current thread.
10682 If ARTICLE is non-nil, return all articles in the thread that starts
10683 with that article."
10684   (let* ((article (or article (gnus-summary-article-number)))
10685          (data (gnus-data-find-list article))
10686          (top-level (gnus-data-level (car data)))
10687          (top-subject
10688           (cond ((null gnus-thread-operation-ignore-subject)
10689                  (gnus-simplify-subject-re
10690                   (mail-header-subject (gnus-data-header (car data)))))
10691                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10692                  (gnus-simplify-subject-fuzzy
10693                   (mail-header-subject (gnus-data-header (car data)))))
10694                 (t nil)))
10695          (end-point (save-excursion
10696                       (if (gnus-summary-go-to-next-thread)
10697                           (point) (point-max))))
10698          articles)
10699     (while (and data
10700                 (< (gnus-data-pos (car data)) end-point))
10701       (when (or (not top-subject)
10702                 (string= top-subject
10703                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10704                              (gnus-simplify-subject-fuzzy
10705                               (mail-header-subject
10706                                (gnus-data-header (car data))))
10707                            (gnus-simplify-subject-re
10708                             (mail-header-subject
10709                              (gnus-data-header (car data)))))))
10710         (push (gnus-data-number (car data)) articles))
10711       (unless (and (setq data (cdr data))
10712                    (> (gnus-data-level (car data)) top-level))
10713         (setq data nil)))
10714     ;; Return the list of articles.
10715     (nreverse articles)))
10716
10717 (defun gnus-summary-rethread-current ()
10718   "Rethread the thread the current article is part of."
10719   (interactive)
10720   (let* ((gnus-show-threads t)
10721          (article (gnus-summary-article-number))
10722          (id (mail-header-id (gnus-summary-article-header)))
10723          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10724     (unless id
10725       (error "No article on the current line"))
10726     (gnus-rebuild-thread id)
10727     (gnus-summary-goto-subject article)))
10728
10729 (defun gnus-summary-reparent-thread ()
10730   "Make the current article child of the marked (or previous) article.
10731
10732 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10733 is non-nil or the Subject: of both articles are the same."
10734   (interactive)
10735   (unless (not (gnus-group-read-only-p))
10736     (error "The current newsgroup does not support article editing"))
10737   (unless (<= (length gnus-newsgroup-processable) 1)
10738     (error "No more than one article may be marked"))
10739   (save-window-excursion
10740     (let ((gnus-article-buffer " *reparent*")
10741           (current-article (gnus-summary-article-number))
10742           ;; First grab the marked article, otherwise one line up.
10743           (parent-article (if (not (null gnus-newsgroup-processable))
10744                               (car gnus-newsgroup-processable)
10745                             (save-excursion
10746                               (if (eq (forward-line -1) 0)
10747                                   (gnus-summary-article-number)
10748                                 (error "Beginning of summary buffer"))))))
10749       (unless (not (eq current-article parent-article))
10750         (error "An article may not be self-referential"))
10751       (let ((message-id (mail-header-id
10752                          (gnus-summary-article-header parent-article))))
10753         (unless (and message-id (not (equal message-id "")))
10754           (error "No message-id in desired parent"))
10755         (gnus-with-article current-article
10756           (save-restriction
10757             (goto-char (point-min))
10758             (message-narrow-to-head)
10759             (if (re-search-forward "^References: " nil t)
10760                 (progn
10761                   (re-search-forward "^[^ \t]" nil t)
10762                   (forward-line -1)
10763                   (end-of-line)
10764                   (insert " " message-id))
10765               (insert "References: " message-id "\n"))))
10766         (set-buffer gnus-summary-buffer)
10767         (gnus-summary-unmark-all-processable)
10768         (gnus-summary-update-article current-article)
10769         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10770             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10771         (gnus-summary-rethread-current)
10772         (gnus-message 3 "Article %d is now the child of article %d"
10773                       current-article parent-article)))))
10774
10775 (defun gnus-summary-toggle-threads (&optional arg)
10776   "Toggle showing conversation threads.
10777 If ARG is positive number, turn showing conversation threads on."
10778   (interactive "P")
10779   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10780     (setq gnus-show-threads
10781           (if (null arg) (not gnus-show-threads)
10782             (> (prefix-numeric-value arg) 0)))
10783     (gnus-summary-prepare)
10784     (gnus-summary-goto-subject current)
10785     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10786     (gnus-summary-position-point)))
10787
10788 (defun gnus-summary-show-all-threads ()
10789   "Show all threads."
10790   (interactive)
10791   (save-excursion
10792     (let ((buffer-read-only nil))
10793       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10794   (gnus-summary-position-point))
10795
10796 (defun gnus-summary-show-thread ()
10797   "Show thread subtrees.
10798 Returns nil if no thread was there to be shown."
10799   (interactive)
10800   (let ((buffer-read-only nil)
10801         (orig (point))
10802         (end (point-at-eol))
10803         ;; Leave point at bol
10804         (beg (progn (beginning-of-line) (point))))
10805     (prog1
10806         ;; Any hidden lines here?
10807         (search-forward "\r" end t)
10808       (subst-char-in-region beg end ?\^M ?\n t)
10809       (goto-char orig)
10810       (gnus-summary-position-point))))
10811
10812 (defun gnus-summary-maybe-hide-threads ()
10813   "If requested, hide the threads that should be hidden."
10814   (when (and gnus-show-threads
10815              gnus-thread-hide-subtree)
10816     (gnus-summary-hide-all-threads
10817      (if (or (consp gnus-thread-hide-subtree)
10818              (functionp gnus-thread-hide-subtree))
10819          (gnus-make-predicate gnus-thread-hide-subtree)
10820        nil))))
10821
10822 ;;; Hiding predicates.
10823
10824 (defun gnus-article-unread-p (header)
10825   (memq (mail-header-number header) gnus-newsgroup-unreads))
10826
10827 (defun gnus-article-unseen-p (header)
10828   (memq (mail-header-number header) gnus-newsgroup-unseen))
10829
10830 (defun gnus-map-articles (predicate articles)
10831   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10832   (apply 'gnus-or (mapcar predicate
10833                           (mapcar 'gnus-summary-article-header articles))))
10834
10835 (defun gnus-summary-hide-all-threads (&optional predicate)
10836   "Hide all thread subtrees.
10837 If PREDICATE is supplied, threads that satisfy this predicate
10838 will not be hidden."
10839   (interactive)
10840   (save-excursion
10841     (goto-char (point-min))
10842     (let ((end nil))
10843       (while (not end)
10844         (when (or (not predicate)
10845                   (gnus-map-articles
10846                    predicate (gnus-summary-article-children)))
10847             (gnus-summary-hide-thread))
10848         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10849   (gnus-summary-position-point))
10850
10851 (defun gnus-summary-hide-thread ()
10852   "Hide thread subtrees.
10853 If PREDICATE is supplied, threads that satisfy this predicate
10854 will not be hidden.
10855 Returns nil if no threads were there to be hidden."
10856   (interactive)
10857   (let ((buffer-read-only nil)
10858         (start (point))
10859         (article (gnus-summary-article-number)))
10860     (goto-char start)
10861     ;; Go forward until either the buffer ends or the subthread
10862     ;; ends.
10863     (when (and (not (eobp))
10864                (or (zerop (gnus-summary-next-thread 1 t))
10865                    (goto-char (point-max))))
10866       (prog1
10867           (if (and (> (point) start)
10868                    (search-backward "\n" start t))
10869               (progn
10870                 (subst-char-in-region start (point) ?\n ?\^M)
10871                 (gnus-summary-goto-subject article))
10872             (goto-char start)
10873             nil)))))
10874
10875 (defun gnus-summary-go-to-next-thread (&optional previous)
10876   "Go to the same level (or less) next thread.
10877 If PREVIOUS is non-nil, go to previous thread instead.
10878 Return the article number moved to, or nil if moving was impossible."
10879   (let ((level (gnus-summary-thread-level))
10880         (way (if previous -1 1))
10881         (beg (point)))
10882     (forward-line way)
10883     (while (and (not (eobp))
10884                 (< level (gnus-summary-thread-level)))
10885       (forward-line way))
10886     (if (eobp)
10887         (progn
10888           (goto-char beg)
10889           nil)
10890       (setq beg (point))
10891       (prog1
10892           (gnus-summary-article-number)
10893         (goto-char beg)))))
10894
10895 (defun gnus-summary-next-thread (n &optional silent)
10896   "Go to the same level next N'th thread.
10897 If N is negative, search backward instead.
10898 Returns the difference between N and the number of skips actually
10899 done.
10900
10901 If SILENT, don't output messages."
10902   (interactive "p")
10903   (let ((backward (< n 0))
10904         (n (abs n)))
10905     (while (and (> n 0)
10906                 (gnus-summary-go-to-next-thread backward))
10907       (decf n))
10908     (unless silent
10909       (gnus-summary-position-point))
10910     (when (and (not silent) (/= 0 n))
10911       (gnus-message 7 "No more threads"))
10912     n))
10913
10914 (defun gnus-summary-prev-thread (n)
10915   "Go to the same level previous N'th thread.
10916 Returns the difference between N and the number of skips actually
10917 done."
10918   (interactive "p")
10919   (gnus-summary-next-thread (- n)))
10920
10921 (defun gnus-summary-go-down-thread ()
10922   "Go down one level in the current thread."
10923   (let ((children (gnus-summary-article-children)))
10924     (when children
10925       (gnus-summary-goto-subject (car children)))))
10926
10927 (defun gnus-summary-go-up-thread ()
10928   "Go up one level in the current thread."
10929   (let ((parent (gnus-summary-article-parent)))
10930     (when parent
10931       (gnus-summary-goto-subject parent))))
10932
10933 (defun gnus-summary-down-thread (n)
10934   "Go down thread N steps.
10935 If N is negative, go up instead.
10936 Returns the difference between N and how many steps down that were
10937 taken."
10938   (interactive "p")
10939   (let ((up (< n 0))
10940         (n (abs n)))
10941     (while (and (> n 0)
10942                 (if up (gnus-summary-go-up-thread)
10943                   (gnus-summary-go-down-thread)))
10944       (setq n (1- n)))
10945     (gnus-summary-position-point)
10946     (when (/= 0 n)
10947       (gnus-message 7 "Can't go further"))
10948     n))
10949
10950 (defun gnus-summary-up-thread (n)
10951   "Go up thread N steps.
10952 If N is negative, go down instead.
10953 Returns the difference between N and how many steps down that were
10954 taken."
10955   (interactive "p")
10956   (gnus-summary-down-thread (- n)))
10957
10958 (defun gnus-summary-top-thread ()
10959   "Go to the top of the thread."
10960   (interactive)
10961   (while (gnus-summary-go-up-thread))
10962   (gnus-summary-article-number))
10963
10964 (defun gnus-summary-kill-thread (&optional unmark)
10965   "Mark articles under current thread as read.
10966 If the prefix argument is positive, remove any kinds of marks.
10967 If the prefix argument is negative, tick articles instead."
10968   (interactive "P")
10969   (when unmark
10970     (setq unmark (prefix-numeric-value unmark)))
10971   (let ((articles (gnus-summary-articles-in-thread)))
10972     (save-excursion
10973       ;; Expand the thread.
10974       (gnus-summary-show-thread)
10975       ;; Mark all the articles.
10976       (while articles
10977         (gnus-summary-goto-subject (car articles))
10978         (cond ((null unmark)
10979                (gnus-summary-mark-article-as-read gnus-killed-mark))
10980               ((> unmark 0)
10981                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10982               (t
10983                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10984         (setq articles (cdr articles))))
10985     ;; Hide killed subtrees.
10986     (and (null unmark)
10987          gnus-thread-hide-killed
10988          (gnus-summary-hide-thread))
10989     ;; If marked as read, go to next unread subject.
10990     (when (null unmark)
10991       ;; Go to next unread subject.
10992       (gnus-summary-next-subject 1 t)))
10993   (gnus-set-mode-line 'summary))
10994
10995 ;; Summary sorting commands
10996
10997 (defun gnus-summary-sort-by-number (&optional reverse)
10998   "Sort the summary buffer by article number.
10999 Argument REVERSE means reverse order."
11000   (interactive "P")
11001   (gnus-summary-sort 'number reverse))
11002
11003 (defun gnus-summary-sort-by-random (&optional reverse)
11004   "Randomize the order in the summary buffer.
11005 Argument REVERSE means to randomize in reverse order."
11006   (interactive "P")
11007   (gnus-summary-sort 'random reverse))
11008
11009 (defun gnus-summary-sort-by-author (&optional reverse)
11010   "Sort the summary buffer by author name alphabetically.
11011 If `case-fold-search' is non-nil, case of letters is ignored.
11012 Argument REVERSE means reverse order."
11013   (interactive "P")
11014   (gnus-summary-sort 'author reverse))
11015
11016 (defun gnus-summary-sort-by-recipient (&optional reverse)
11017   "Sort the summary buffer by recipient name alphabetically.
11018 If `case-fold-search' is non-nil, case of letters is ignored.
11019 Argument REVERSE means reverse order."
11020   (interactive "P")
11021   (gnus-summary-sort 'recipient reverse))
11022
11023 (defun gnus-summary-sort-by-subject (&optional reverse)
11024   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11025 If `case-fold-search' is non-nil, case of letters is ignored.
11026 Argument REVERSE means reverse order."
11027   (interactive "P")
11028   (gnus-summary-sort 'subject reverse))
11029
11030 (defun gnus-summary-sort-by-date (&optional reverse)
11031   "Sort the summary buffer by date.
11032 Argument REVERSE means reverse order."
11033   (interactive "P")
11034   (gnus-summary-sort 'date reverse))
11035
11036 (defun gnus-summary-sort-by-score (&optional reverse)
11037   "Sort the summary buffer by score.
11038 Argument REVERSE means reverse order."
11039   (interactive "P")
11040   (gnus-summary-sort 'score reverse))
11041
11042 (defun gnus-summary-sort-by-lines (&optional reverse)
11043   "Sort the summary buffer by the number of lines.
11044 Argument REVERSE means reverse order."
11045   (interactive "P")
11046   (gnus-summary-sort 'lines reverse))
11047
11048 (defun gnus-summary-sort-by-chars (&optional reverse)
11049   "Sort the summary buffer by article length.
11050 Argument REVERSE means reverse order."
11051   (interactive "P")
11052   (gnus-summary-sort 'chars reverse))
11053
11054 (defun gnus-summary-sort-by-original (&optional reverse)
11055   "Sort the summary buffer using the default sorting method.
11056 Argument REVERSE means reverse order."
11057   (interactive "P")
11058   (let* ((buffer-read-only)
11059          (gnus-summary-prepare-hook nil))
11060     ;; We do the sorting by regenerating the threads.
11061     (gnus-summary-prepare)
11062     ;; Hide subthreads if needed.
11063     (gnus-summary-maybe-hide-threads)))
11064
11065 (defun gnus-summary-sort (predicate reverse)
11066   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11067   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11068          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11069          (gnus-thread-sort-functions
11070           (if (not reverse)
11071               thread
11072             `(lambda (t1 t2)
11073                (,thread t2 t1))))
11074          (gnus-sort-gathered-threads-function
11075           gnus-thread-sort-functions)
11076          (gnus-article-sort-functions
11077           (if (not reverse)
11078               article
11079             `(lambda (t1 t2)
11080                (,article t2 t1))))
11081          (buffer-read-only)
11082          (gnus-summary-prepare-hook nil))
11083     ;; We do the sorting by regenerating the threads.
11084     (gnus-summary-prepare)
11085     ;; Hide subthreads if needed.
11086     (gnus-summary-maybe-hide-threads)))
11087
11088 ;; Summary saving commands.
11089
11090 (defun gnus-summary-save-article (&optional n not-saved)
11091   "Save the current article using the default saver function.
11092 If N is a positive number, save the N next articles.
11093 If N is a negative number, save the N previous articles.
11094 If N is nil and any articles have been marked with the process mark,
11095 save those articles instead.
11096 The variable `gnus-default-article-saver' specifies the saver function."
11097   (interactive "P")
11098   (let* ((articles (gnus-summary-work-articles n))
11099          (save-buffer (save-excursion
11100                         (nnheader-set-temp-buffer " *Gnus Save*")))
11101          (num (length articles))
11102          header file)
11103     (dolist (article articles)
11104       (setq header (gnus-summary-article-header article))
11105       (if (not (vectorp header))
11106           ;; This is a pseudo-article.
11107           (if (assq 'name header)
11108               (gnus-copy-file (cdr (assq 'name header)))
11109             (gnus-message 1 "Article %d is unsaveable" article))
11110         ;; This is a real article.
11111         (save-window-excursion
11112           (let ((gnus-display-mime-function nil)
11113                 (gnus-article-prepare-hook nil))
11114             (gnus-summary-select-article t nil nil article)))
11115         (save-excursion
11116           (set-buffer save-buffer)
11117           (erase-buffer)
11118           (insert-buffer-substring gnus-original-article-buffer))
11119         (setq file (gnus-article-save save-buffer file num))
11120         (gnus-summary-remove-process-mark article)
11121         (unless not-saved
11122           (gnus-summary-set-saved-mark article))))
11123     (gnus-kill-buffer save-buffer)
11124     (gnus-summary-position-point)
11125     (gnus-set-mode-line 'summary)
11126     n))
11127
11128 (defun gnus-summary-pipe-output (&optional arg headers)
11129   "Pipe the current article to a subprocess.
11130 If N is a positive number, pipe the N next articles.
11131 If N is a negative number, pipe the N previous articles.
11132 If N is nil and any articles have been marked with the process mark,
11133 pipe those articles instead.
11134 If HEADERS (the symbolic prefix), include the headers, too."
11135   (interactive (gnus-interactive "P\ny"))
11136   (require 'gnus-art)
11137   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11138         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11139     (gnus-summary-save-article arg t))
11140   (let ((buffer (get-buffer "*Shell Command Output*")))
11141     (when (and buffer
11142                (not (zerop (buffer-size buffer))))
11143       (gnus-configure-windows 'pipe))))
11144
11145 (defun gnus-summary-save-article-mail (&optional arg)
11146   "Append the current article to an mail file.
11147 If N is a positive number, save the N next articles.
11148 If N is a negative number, save the N previous articles.
11149 If N is nil and any articles have been marked with the process mark,
11150 save those articles instead."
11151   (interactive "P")
11152   (require 'gnus-art)
11153   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11154     (gnus-summary-save-article arg)))
11155
11156 (defun gnus-summary-save-article-rmail (&optional arg)
11157   "Append the current article to an rmail file.
11158 If N is a positive number, save the N next articles.
11159 If N is a negative number, save the N previous articles.
11160 If N is nil and any articles have been marked with the process mark,
11161 save those articles instead."
11162   (interactive "P")
11163   (require 'gnus-art)
11164   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11165     (gnus-summary-save-article arg)))
11166
11167 (defun gnus-summary-save-article-file (&optional arg)
11168   "Append the current article to a file.
11169 If N is a positive number, save the N next articles.
11170 If N is a negative number, save the N previous articles.
11171 If N is nil and any articles have been marked with the process mark,
11172 save those articles instead."
11173   (interactive "P")
11174   (require 'gnus-art)
11175   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11176     (gnus-summary-save-article arg)))
11177
11178 (defun gnus-summary-write-article-file (&optional arg)
11179   "Write the current article to a file, deleting the previous file.
11180 If N is a positive number, save the N next articles.
11181 If N is a negative number, save the N previous articles.
11182 If N is nil and any articles have been marked with the process mark,
11183 save those articles instead."
11184   (interactive "P")
11185   (require 'gnus-art)
11186   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11187     (gnus-summary-save-article arg)))
11188
11189 (defun gnus-summary-save-article-body-file (&optional arg)
11190   "Append the current article body to a file.
11191 If N is a positive number, save the N next articles.
11192 If N is a negative number, save the N previous articles.
11193 If N is nil and any articles have been marked with the process mark,
11194 save those articles instead."
11195   (interactive "P")
11196   (require 'gnus-art)
11197   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11198     (gnus-summary-save-article arg)))
11199
11200 (defun gnus-summary-muttprint (&optional arg)
11201   "Print the current article using Muttprint.
11202 If N is a positive number, save the N next articles.
11203 If N is a negative number, save the N previous articles.
11204 If N is nil and any articles have been marked with the process mark,
11205 save those articles instead."
11206   (interactive "P")
11207   (require 'gnus-art)
11208   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11209     (gnus-summary-save-article arg t)))
11210
11211 (defun gnus-summary-pipe-message (program)
11212   "Pipe the current article through PROGRAM."
11213   (interactive "sProgram: ")
11214   (gnus-summary-select-article)
11215   (let ((mail-header-separator ""))
11216     (gnus-eval-in-buffer-window gnus-article-buffer
11217       (save-restriction
11218         (widen)
11219         (let ((start (window-start))
11220               buffer-read-only)
11221           (message-pipe-buffer-body program)
11222           (set-window-start (get-buffer-window (current-buffer)) start))))))
11223
11224 (defun gnus-get-split-value (methods)
11225   "Return a value based on the split METHODS."
11226   (let (split-name method result match)
11227     (when methods
11228       (save-excursion
11229         (set-buffer gnus-original-article-buffer)
11230         (save-restriction
11231           (nnheader-narrow-to-headers)
11232           (while (and methods (not split-name))
11233             (goto-char (point-min))
11234             (setq method (pop methods))
11235             (setq match (car method))
11236             (when (cond
11237                    ((stringp match)
11238                     ;; Regular expression.
11239                     (ignore-errors
11240                       (re-search-forward match nil t)))
11241                    ((functionp match)
11242                     ;; Function.
11243                     (save-restriction
11244                       (widen)
11245                       (setq result (funcall match gnus-newsgroup-name))))
11246                    ((consp match)
11247                     ;; Form.
11248                     (save-restriction
11249                       (widen)
11250                       (setq result (eval match)))))
11251               (setq split-name (cdr method))
11252               (cond ((stringp result)
11253                      (push (expand-file-name
11254                             result gnus-article-save-directory)
11255                            split-name))
11256                     ((consp result)
11257                      (setq split-name (append result split-name)))))))))
11258     (nreverse split-name)))
11259
11260 (defun gnus-valid-move-group-p (group)
11261   (and (boundp group)
11262        (symbol-name group)
11263        (symbol-value group)
11264        (gnus-get-function (gnus-find-method-for-group
11265                            (symbol-name group)) 'request-accept-article t)))
11266
11267 (defun gnus-read-move-group-name (prompt default articles prefix)
11268   "Read a group name."
11269   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11270          (minibuffer-confirm-incomplete nil) ; XEmacs
11271          (prom
11272           (format "%s %s to:"
11273                   prompt
11274                   (if (> (length articles) 1)
11275                       (format "these %d articles" (length articles))
11276                     "this article")))
11277          (to-newsgroup
11278           (cond
11279            ((null split-name)
11280             (gnus-completing-read-with-default
11281              default prom
11282              gnus-active-hashtb
11283              'gnus-valid-move-group-p
11284              nil prefix
11285              'gnus-group-history))
11286            ((= 1 (length split-name))
11287             (gnus-completing-read-with-default
11288              (car split-name) prom
11289              gnus-active-hashtb
11290              'gnus-valid-move-group-p
11291              nil nil
11292              'gnus-group-history))
11293            (t
11294             (gnus-completing-read-with-default
11295              nil prom
11296              (mapcar 'list (nreverse split-name))
11297              nil nil nil
11298              'gnus-group-history))))
11299          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11300     (when to-newsgroup
11301       (if (or (string= to-newsgroup "")
11302               (string= to-newsgroup prefix))
11303           (setq to-newsgroup default))
11304       (unless to-newsgroup
11305         (error "No group name entered"))
11306       (or (gnus-active to-newsgroup)
11307           (gnus-activate-group to-newsgroup nil nil to-method)
11308           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11309                                      to-newsgroup))
11310               (or (and (gnus-request-create-group to-newsgroup to-method)
11311                        (gnus-activate-group
11312                         to-newsgroup nil nil to-method)
11313                        (gnus-subscribe-group to-newsgroup))
11314                   (error "Couldn't create group %s" to-newsgroup)))
11315           (error "No such group: %s" to-newsgroup)))
11316     to-newsgroup))
11317
11318 (defun gnus-summary-save-parts (type dir n &optional reverse)
11319   "Save parts matching TYPE to DIR.
11320 If REVERSE, save parts that do not match TYPE."
11321   (interactive
11322    (list (read-string "Save parts of type: "
11323                       (or (car gnus-summary-save-parts-type-history)
11324                           gnus-summary-save-parts-default-mime)
11325                       'gnus-summary-save-parts-type-history)
11326          (setq gnus-summary-save-parts-last-directory
11327                (read-file-name "Save to directory: "
11328                                gnus-summary-save-parts-last-directory
11329                                nil t))
11330          current-prefix-arg))
11331   (gnus-summary-iterate n
11332     (let ((gnus-display-mime-function nil)
11333           (gnus-inhibit-treatment t))
11334       (gnus-summary-select-article))
11335     (save-excursion
11336       (set-buffer gnus-article-buffer)
11337       (let ((handles (or gnus-article-mime-handles
11338                          (mm-dissect-buffer nil gnus-article-loose-mime)
11339                          (and gnus-article-emulate-mime
11340                               (mm-uu-dissect)))))
11341         (when handles
11342           (gnus-summary-save-parts-1 type dir handles reverse)
11343           (unless gnus-article-mime-handles ;; Don't destroy this case.
11344             (mm-destroy-parts handles)))))))
11345
11346 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11347   (if (stringp (car handle))
11348       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11349               (cdr handle))
11350     (when (if reverse
11351               (not (string-match type (mm-handle-media-type handle)))
11352             (string-match type (mm-handle-media-type handle)))
11353       (let ((file (expand-file-name
11354                    (gnus-map-function
11355                     mm-file-name-rewrite-functions
11356                     (file-name-nondirectory
11357                      (or
11358                       (mail-content-type-get
11359                        (mm-handle-disposition handle) 'filename)
11360                       (mail-content-type-get
11361                        (mm-handle-type handle) 'name)
11362                       (concat gnus-newsgroup-name
11363                               "." (number-to-string
11364                                    (cdr gnus-article-current))))))
11365                    dir)))
11366         (unless (file-exists-p file)
11367           (mm-save-part-to-file handle file))))))
11368
11369 ;; Summary extract commands
11370
11371 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11372   (let ((buffer-read-only nil)
11373         (article (gnus-summary-article-number))
11374         after-article b e)
11375     (unless (gnus-summary-goto-subject article)
11376       (error "No such article: %d" article))
11377     (gnus-summary-position-point)
11378     ;; If all commands are to be bunched up on one line, we collect
11379     ;; them here.
11380     (unless gnus-view-pseudos-separately
11381       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11382             files action)
11383         (while ps
11384           (setq action (cdr (assq 'action (car ps))))
11385           (setq files (list (cdr (assq 'name (car ps)))))
11386           (while (and ps (cdr ps)
11387                       (string= (or action "1")
11388                                (or (cdr (assq 'action (cadr ps))) "2")))
11389             (push (cdr (assq 'name (cadr ps))) files)
11390             (setcdr ps (cddr ps)))
11391           (when files
11392             (when (not (string-match "%s" action))
11393               (push " " files))
11394             (push " " files)
11395             (when (assq 'execute (car ps))
11396               (setcdr (assq 'execute (car ps))
11397                       (funcall (if (string-match "%s" action)
11398                                    'format 'concat)
11399                                action
11400                                (mapconcat
11401                                 (lambda (f)
11402                                   (if (equal f " ")
11403                                       f
11404                                     (shell-quote-argument f)))
11405                                 files " ")))))
11406           (setq ps (cdr ps)))))
11407     (if (and gnus-view-pseudos (not not-view))
11408         (while pslist
11409           (when (assq 'execute (car pslist))
11410             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11411                                   (eq gnus-view-pseudos 'not-confirm)))
11412           (setq pslist (cdr pslist)))
11413       (save-excursion
11414         (while pslist
11415           (setq after-article (or (cdr (assq 'article (car pslist)))
11416                                   (gnus-summary-article-number)))
11417           (gnus-summary-goto-subject after-article)
11418           (forward-line 1)
11419           (setq b (point))
11420           (insert "    " (file-name-nondirectory
11421                           (cdr (assq 'name (car pslist))))
11422                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11423           (setq e (point))
11424           (forward-line -1)             ; back to `b'
11425           (gnus-add-text-properties
11426            b (1- e) (list 'gnus-number gnus-reffed-article-number
11427                           gnus-mouse-face-prop gnus-mouse-face))
11428           (gnus-data-enter
11429            after-article gnus-reffed-article-number
11430            gnus-unread-mark b (car pslist) 0 (- e b))
11431           (setq gnus-newsgroup-unreads
11432                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11433                                          gnus-reffed-article-number))
11434           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11435           (setq pslist (cdr pslist)))))))
11436
11437 (defun gnus-pseudos< (p1 p2)
11438   (let ((c1 (cdr (assq 'action p1)))
11439         (c2 (cdr (assq 'action p2))))
11440     (and c1 c2 (string< c1 c2))))
11441
11442 (defun gnus-request-pseudo-article (props)
11443   (cond ((assq 'execute props)
11444          (gnus-execute-command (cdr (assq 'execute props)))))
11445   (let ((gnus-current-article (gnus-summary-article-number)))
11446     (gnus-run-hooks 'gnus-mark-article-hook)))
11447
11448 (defun gnus-execute-command (command &optional automatic)
11449   (save-excursion
11450     (gnus-article-setup-buffer)
11451     (set-buffer gnus-article-buffer)
11452     (setq buffer-read-only nil)
11453     (let ((command (if automatic command
11454                      (read-string "Command: " (cons command 0)))))
11455       (erase-buffer)
11456       (insert "$ " command "\n\n")
11457       (if gnus-view-pseudo-asynchronously
11458           (start-process "gnus-execute" (current-buffer) shell-file-name
11459                          shell-command-switch command)
11460         (call-process shell-file-name nil t nil
11461                       shell-command-switch command)))))
11462
11463 ;; Summary kill commands.
11464
11465 (defun gnus-summary-edit-global-kill (article)
11466   "Edit the \"global\" kill file."
11467   (interactive (list (gnus-summary-article-number)))
11468   (gnus-group-edit-global-kill article))
11469
11470 (defun gnus-summary-edit-local-kill ()
11471   "Edit a local kill file applied to the current newsgroup."
11472   (interactive)
11473   (setq gnus-current-headers (gnus-summary-article-header))
11474   (gnus-group-edit-local-kill
11475    (gnus-summary-article-number) gnus-newsgroup-name))
11476
11477 ;;; Header reading.
11478
11479 (defun gnus-read-header (id &optional header)
11480   "Read the headers of article ID and enter them into the Gnus system."
11481   (let ((group gnus-newsgroup-name)
11482         (gnus-override-method
11483          (or
11484           gnus-override-method
11485           (and (gnus-news-group-p gnus-newsgroup-name)
11486                (car (gnus-refer-article-methods)))))
11487         where)
11488     ;; First we check to see whether the header in question is already
11489     ;; fetched.
11490     (if (stringp id)
11491         ;; This is a Message-ID.
11492         (setq header (or header (gnus-id-to-header id)))
11493       ;; This is an article number.
11494       (setq header (or header (gnus-summary-article-header id))))
11495     (if (and header
11496              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11497         ;; We have found the header.
11498         header
11499       ;; We have to really fetch the header to this article.
11500       (save-excursion
11501         (set-buffer nntp-server-buffer)
11502         (when (setq where (gnus-request-head id group))
11503           (nnheader-fold-continuation-lines)
11504           (goto-char (point-max))
11505           (insert ".\n")
11506           (goto-char (point-min))
11507           (insert "211 ")
11508           (princ (cond
11509                   ((numberp id) id)
11510                   ((cdr where) (cdr where))
11511                   (header (mail-header-number header))
11512                   (t gnus-reffed-article-number))
11513                  (current-buffer))
11514           (insert " Article retrieved.\n"))
11515         (if (or (not where)
11516                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11517             ()                          ; Malformed head.
11518           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11519             (when (and (stringp id)
11520                        (not (string= (gnus-group-real-name group)
11521                                      (car where))))
11522               ;; If we fetched by Message-ID and the article came
11523               ;; from a different group, we fudge some bogus article
11524               ;; numbers for this article.
11525               (mail-header-set-number header gnus-reffed-article-number))
11526             (save-excursion
11527               (set-buffer gnus-summary-buffer)
11528               (decf gnus-reffed-article-number)
11529               (gnus-remove-header (mail-header-number header))
11530               (push header gnus-newsgroup-headers)
11531               (setq gnus-current-headers header)
11532               (push (mail-header-number header) gnus-newsgroup-limit)))
11533           header)))))
11534
11535 (defun gnus-remove-header (number)
11536   "Remove header NUMBER from `gnus-newsgroup-headers'."
11537   (if (and gnus-newsgroup-headers
11538            (= number (mail-header-number (car gnus-newsgroup-headers))))
11539       (pop gnus-newsgroup-headers)
11540     (let ((headers gnus-newsgroup-headers))
11541       (while (and (cdr headers)
11542                   (not (= number (mail-header-number (cadr headers)))))
11543         (pop headers))
11544       (when (cdr headers)
11545         (setcdr headers (cddr headers))))))
11546
11547 ;;;
11548 ;;; summary highlights
11549 ;;;
11550
11551 (defun gnus-highlight-selected-summary ()
11552   "Highlight selected article in summary buffer."
11553   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11554   (when gnus-summary-selected-face
11555     (save-excursion
11556       (let* ((beg (point-at-bol))
11557              (end (point-at-eol))
11558              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11559              (from (if (get-text-property beg gnus-mouse-face-prop)
11560                        beg
11561                      (or (next-single-property-change
11562                           beg gnus-mouse-face-prop nil end)
11563                          beg)))
11564              (to
11565               (if (= from end)
11566                   (- from 2)
11567                 (or (next-single-property-change
11568                      from gnus-mouse-face-prop nil end)
11569                     end))))
11570         ;; If no mouse-face prop on line we will have to = from = end,
11571         ;; so we highlight the entire line instead.
11572         (when (= (+ to 2) from)
11573           (setq from beg)
11574           (setq to end))
11575         (if gnus-newsgroup-selected-overlay
11576             ;; Move old overlay.
11577             (gnus-move-overlay
11578              gnus-newsgroup-selected-overlay from to (current-buffer))
11579           ;; Create new overlay.
11580           (gnus-overlay-put
11581            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11582            'face gnus-summary-selected-face))))))
11583
11584 (defvar gnus-summary-highlight-line-cached nil)
11585 (defvar gnus-summary-highlight-line-trigger nil)
11586
11587 (defun gnus-summary-highlight-line-0 ()
11588   (if (and (eq gnus-summary-highlight-line-trigger
11589                gnus-summary-highlight)
11590            gnus-summary-highlight-line-cached)
11591       gnus-summary-highlight-line-cached
11592     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11593           gnus-summary-highlight-line-cached
11594           (let* ((cond (list 'cond))
11595                  (c cond)
11596                  (list gnus-summary-highlight))
11597             (while list
11598               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11599                               nil))
11600               (setq c (cdr c)
11601                     list (cdr list)))
11602             (gnus-byte-compile (list 'lambda nil cond))))))
11603
11604 (defun gnus-summary-highlight-line ()
11605   "Highlight current line according to `gnus-summary-highlight'."
11606   (let* ((beg (point-at-bol))
11607          (article (or (gnus-summary-article-number) gnus-current-article))
11608          (score (or (cdr (assq article
11609                                gnus-newsgroup-scored))
11610                     gnus-summary-default-score 0))
11611          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11612          (inhibit-read-only t)
11613          (default gnus-summary-default-score)
11614          (default-high gnus-summary-default-high-score)
11615          (default-low gnus-summary-default-low-score)
11616          (uncached (and gnus-summary-use-undownloaded-faces
11617                         (memq article gnus-newsgroup-undownloaded)
11618                         (not (memq article gnus-newsgroup-cached)))))
11619     (let ((face (funcall (gnus-summary-highlight-line-0))))
11620       (unless (eq face (get-text-property beg 'face))
11621         (gnus-put-text-property-excluding-characters-with-faces
11622          beg (point-at-eol) 'face
11623          (setq face (if (boundp face) (symbol-value face) face)))
11624         (when gnus-summary-highlight-line-function
11625           (funcall gnus-summary-highlight-line-function article face))))))
11626
11627 (defun gnus-update-read-articles (group unread &optional compute)
11628   "Update the list of read articles in GROUP.
11629 UNREAD is a sorted list."
11630   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11631         (info (gnus-get-info group))
11632         (prev 1)
11633         read)
11634     (if (or (not info) (not active))
11635         ;; There is no info on this group if it was, in fact,
11636         ;; killed.  Gnus stores no information on killed groups, so
11637         ;; there's nothing to be done.
11638         ;; One could store the information somewhere temporarily,
11639         ;; perhaps...  Hmmm...
11640         ()
11641       ;; Remove any negative articles numbers.
11642       (while (and unread (< (car unread) 0))
11643         (setq unread (cdr unread)))
11644       ;; Remove any expired article numbers
11645       (while (and unread (< (car unread) (car active)))
11646         (setq unread (cdr unread)))
11647       ;; Compute the ranges of read articles by looking at the list of
11648       ;; unread articles.
11649       (while unread
11650         (when (/= (car unread) prev)
11651           (push (if (= prev (1- (car unread))) prev
11652                   (cons prev (1- (car unread))))
11653                 read))
11654         (setq prev (1+ (car unread)))
11655         (setq unread (cdr unread)))
11656       (when (<= prev (cdr active))
11657         (push (cons prev (cdr active)) read))
11658       (setq read (if (> (length read) 1) (nreverse read) read))
11659       (if compute
11660           read
11661         (save-excursion
11662           (let (setmarkundo)
11663             ;; Propagate the read marks to the backend.
11664             (when (gnus-check-backend-function 'request-set-mark group)
11665               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11666                     (add (gnus-remove-from-range read (gnus-info-read info))))
11667                 (when (or add del)
11668                   (unless (gnus-check-group group)
11669                     (error "Can't open server for %s" group))
11670                   (gnus-request-set-mark
11671                    group (delq nil (list (if add (list add 'add '(read)))
11672                                          (if del (list del 'del '(read))))))
11673                   (setq setmarkundo
11674                         `(gnus-request-set-mark
11675                           ,group
11676                           ',(delq nil (list
11677                                        (if del (list del 'add '(read)))
11678                                        (if add (list add 'del '(read))))))))))
11679             (set-buffer gnus-group-buffer)
11680             (gnus-undo-register
11681               `(progn
11682                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11683                  (gnus-info-set-read ',info ',(gnus-info-read info))
11684                  (gnus-get-unread-articles-in-group ',info
11685                                                     (gnus-active ,group))
11686                  (gnus-group-update-group ,group t)
11687                  ,setmarkundo))))
11688         ;; Enter this list into the group info.
11689         (gnus-info-set-read info read)
11690         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11691         (gnus-get-unread-articles-in-group info (gnus-active group))
11692         t))))
11693
11694 (defun gnus-offer-save-summaries ()
11695   "Offer to save all active summary buffers."
11696   (let (buffers)
11697     ;; Go through all buffers and find all summaries.
11698     (dolist (buffer (buffer-list))
11699       (when (and (setq buffer (buffer-name buffer))
11700                  (string-match "Summary" buffer)
11701                  (with-current-buffer buffer
11702                    ;; We check that this is, indeed, a summary buffer.
11703                    (and (eq major-mode 'gnus-summary-mode)
11704                         ;; Also make sure this isn't bogus.
11705                         gnus-newsgroup-prepared
11706                         ;; Also make sure that this isn't a
11707                         ;; dead summary buffer.
11708                         (not gnus-dead-summary-mode))))
11709         (push buffer buffers)))
11710     ;; Go through all these summary buffers and offer to save them.
11711     (when buffers
11712       (save-excursion
11713         (map-y-or-n-p
11714          "Update summary buffer %s? "
11715          (lambda (buf)
11716            (switch-to-buffer buf)
11717            (gnus-summary-exit))
11718          buffers)))))
11719
11720
11721 ;;; @ for mime-partial
11722 ;;;
11723
11724 (defun gnus-request-partial-message ()
11725   (save-excursion
11726     (let ((number (gnus-summary-article-number))
11727           (group gnus-newsgroup-name)
11728           (mother gnus-article-buffer))
11729       (set-buffer (get-buffer-create " *Partial Article*"))
11730       (erase-buffer)
11731       (setq mime-preview-buffer mother)
11732       (gnus-request-article-this-buffer number group)
11733       (mime-parse-buffer)
11734       )))
11735
11736 (autoload 'mime-combine-message/partial-pieces-automatically
11737   "mime-partial"
11738   "Internal method to combine message/partial messages automatically.")
11739
11740 (mime-add-condition
11741  'action '((type . message)(subtype . partial)
11742            (major-mode . gnus-original-article-mode)
11743            (method . mime-combine-message/partial-pieces-automatically)
11744            (summary-buffer-exp . gnus-summary-buffer)
11745            (request-partial-message-method . gnus-request-partial-message)
11746            ))
11747
11748
11749 ;;; @ for message/rfc822
11750 ;;;
11751
11752 (defun gnus-mime-extract-message/rfc822 (entity situation)
11753   "Burst a forwarded article."
11754   (save-excursion
11755     (set-buffer gnus-summary-buffer)
11756     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11757                                    gnus-newsgroup-name 'gnus-group-history))
11758            (gnus-group-marked (list group))
11759            article info)
11760       (with-temp-buffer
11761         (mime-insert-entity-content entity)
11762         (setq article (gnus-request-accept-article group)))
11763       (when (and (consp article)
11764                  (numberp (setq article (cdr article))))
11765         (setq info (gnus-get-info group))
11766         (gnus-info-set-read info
11767                             (gnus-remove-from-range (gnus-info-read info)
11768                                                     (list article)))
11769         (when (string-equal group gnus-newsgroup-name)
11770           (forward-line 1)
11771           (let (gnus-show-threads)
11772             (gnus-summary-goto-subject article t))
11773           (gnus-summary-clear-mark-forward 1))
11774         (set-buffer gnus-group-buffer)
11775         (gnus-group-get-new-news-this-group nil t)))))
11776
11777 (mime-add-condition
11778  'action '((type . message)(subtype . rfc822)
11779            (major-mode . gnus-original-article-mode)
11780            (method . gnus-mime-extract-message/rfc822)
11781            (mode . "extract")
11782            ))
11783
11784 (mime-add-condition
11785  'action '((type . message)(subtype . news)
11786            (major-mode . gnus-original-article-mode)
11787            (method . gnus-mime-extract-message/rfc822)
11788            (mode . "extract")
11789            ))
11790
11791 (defun gnus-mime-extract-multipart (entity situation)
11792   (let ((children (mime-entity-children entity))
11793         mime-acting-situation-to-override
11794         f)
11795     (while children
11796       (mime-play-entity (car children)
11797                         (cons (assq 'mode situation)
11798                               mime-acting-situation-to-override))
11799       (setq children (cdr children)))
11800     (if (setq f (cdr (assq 'after-method
11801                            mime-acting-situation-to-override)))
11802         (eval f)
11803       )))
11804
11805 (mime-add-condition
11806  'action '((type . multipart)
11807            (method . gnus-mime-extract-multipart)
11808            (mode . "extract")
11809            )
11810  'with-default)
11811
11812
11813 ;;; @ end
11814 ;;;
11815
11816 (defun gnus-summary-inherit-default-charset ()
11817   "Import `default-mime-charset' from summary buffer.
11818 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11819 of FLIM is used."
11820   (if (buffer-live-p gnus-summary-buffer)
11821       (let (d-m-c d-m-c-u)
11822         (with-current-buffer gnus-summary-buffer
11823           (setq d-m-c (if (local-variable-p 'default-mime-charset
11824                                             gnus-summary-buffer)
11825                           default-mime-charset
11826                         t)
11827                 ;; LIMIT
11828                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11829                                               gnus-summary-buffer)
11830                             (symbol-value 'default-mime-charset-unlimited)
11831                           t)))
11832         (if (eq t d-m-c)
11833             (kill-local-variable 'default-mime-charset)
11834           (set (make-local-variable 'default-mime-charset) d-m-c))
11835         (if (eq t d-m-c-u)
11836             (kill-local-variable 'default-mime-charset-unlimited)
11837           (set (make-local-variable 'default-mime-charset-unlimited)
11838                d-m-c-u)))))
11839
11840 (defun gnus-summary-setup-default-charset ()
11841   "Setup newsgroup default charset."
11842   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11843       (progn
11844         (setq gnus-newsgroup-charset nil)
11845         (set (make-local-variable 'default-mime-charset) nil)
11846         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11847           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11848     (let ((ignored-charsets
11849            (or gnus-newsgroup-ephemeral-ignored-charsets
11850                (append
11851                 (and gnus-newsgroup-name
11852                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11853                 gnus-newsgroup-ignored-charsets)))
11854           charset)
11855       (setq gnus-newsgroup-charset
11856             (or gnus-newsgroup-ephemeral-charset
11857                 (when (and gnus-newsgroup-name
11858                            (setq charset (gnus-parameter-charset
11859                                           gnus-newsgroup-name)))
11860                   (make-local-variable 'default-mime-charset)
11861                   (setq default-mime-charset charset))
11862                 gnus-default-charset))
11863       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11864            ignored-charsets))))
11865
11866 ;;;
11867 ;;; Mime Commands
11868 ;;;
11869
11870 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11871   "Display the current article buffer fully MIME-buttonized.
11872 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11873 treated as multipart/mixed."
11874   (interactive "P")
11875   (require 'gnus-art)
11876   (let ((gnus-unbuttonized-mime-types nil)
11877         (gnus-mime-display-multipart-as-mixed show-all-parts))
11878     (gnus-summary-show-article)))
11879
11880 (defun gnus-summary-repair-multipart (article)
11881   "Add a Content-Type header to a multipart article without one."
11882   (interactive (list (gnus-summary-article-number)))
11883   (gnus-with-article article
11884     (message-narrow-to-head)
11885     (message-remove-header "Mime-Version")
11886     (goto-char (point-max))
11887     (insert "Mime-Version: 1.0\n")
11888     (widen)
11889     (when (search-forward "\n--" nil t)
11890       (let ((separator (buffer-substring (point) (point-at-eol))))
11891         (message-narrow-to-head)
11892         (message-remove-header "Content-Type")
11893         (goto-char (point-max))
11894         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11895                         separator))
11896         (widen))))
11897   (let (gnus-mark-article-hook)
11898     (gnus-summary-select-article t t nil article)))
11899
11900 (defun gnus-summary-toggle-display-buttonized ()
11901   "Toggle the buttonizing of the article buffer."
11902   (interactive)
11903   (require 'gnus-art)
11904   (if (setq gnus-inhibit-mime-unbuttonizing
11905             (not gnus-inhibit-mime-unbuttonizing))
11906       (let ((gnus-unbuttonized-mime-types nil))
11907         (gnus-summary-show-article))
11908     (gnus-summary-show-article)))
11909
11910 ;;;
11911 ;;; Intelli-mouse commmands
11912 ;;;
11913
11914 (defun gnus-wheel-summary-scroll (event)
11915   (interactive "e")
11916   (let ((amount (if (memq 'shift (event-modifiers event))
11917                     (car gnus-wheel-scroll-amount)
11918                   (cdr gnus-wheel-scroll-amount)))
11919         (direction (- (* (static-if (featurep 'xemacs)
11920                              (event-button event)
11921                            (cond ((eq 'mouse-4 (event-basic-type event))
11922                                   4)
11923                                  ((eq 'mouse-5 (event-basic-type event))
11924                                   5)))
11925                          2) 9))
11926         edge)
11927     (gnus-summary-scroll-up (* amount direction))
11928     (when (gnus-eval-in-buffer-window gnus-article-buffer
11929             (save-restriction
11930               (widen)
11931               (and (if (< 0 direction)
11932                        (gnus-article-next-page 0)
11933                      (gnus-article-prev-page 0)
11934                      (bobp))
11935                    (if (setq edge (get-text-property
11936                                    (point-min) 'gnus-wheel-edge))
11937                        (setq edge (* edge direction))
11938                      (setq edge -1))
11939                    (or (plusp edge)
11940                        (let ((buffer-read-only nil)
11941                              (inhibit-read-only t))
11942                          (put-text-property (point-min) (point-max)
11943                                             'gnus-wheel-edge direction)
11944                          nil))
11945                    (or (> edge gnus-wheel-edge-resistance)
11946                        (let ((buffer-read-only nil)
11947                              (inhibit-read-only t))
11948                          (put-text-property (point-min) (point-max)
11949                                             'gnus-wheel-edge
11950                                             (* (1+ edge) direction))
11951                          nil))
11952                    (eq last-command 'gnus-wheel-summary-scroll))))
11953       (gnus-summary-next-article nil nil (minusp direction)))))
11954
11955 (defun gnus-wheel-install ()
11956   "Enable mouse wheel support on summary window."
11957   (when gnus-use-wheel
11958     (let ((keys
11959            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11960       (dolist (key keys)
11961         (define-key gnus-summary-mode-map key
11962           'gnus-wheel-summary-scroll)))))
11963
11964 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11965
11966 ;;;
11967 ;;; Traditional PGP commmands
11968 ;;;
11969
11970 (defun gnus-summary-decrypt-article (&optional force)
11971   "Decrypt the current article in traditional PGP way.
11972 This will have permanent effect only in mail groups.
11973 If FORCE is non-nil, allow editing of articles even in read-only
11974 groups."
11975   (interactive "P")
11976   (gnus-summary-select-article t)
11977   (gnus-eval-in-buffer-window gnus-article-buffer
11978     (save-excursion
11979       (save-restriction
11980         (widen)
11981         (goto-char (point-min))
11982         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11983           (error "Not a traditional PGP message!"))
11984         (let ((armor-start (match-beginning 0)))
11985           (if (and (pgg-decrypt-region armor-start (point-max))
11986                    (or force (not (gnus-group-read-only-p))))
11987               (let ((inhibit-read-only t)
11988                     buffer-read-only)
11989                 (delete-region armor-start
11990                                (progn
11991                                  (re-search-forward "^-+END PGP" nil t)
11992                                  (beginning-of-line 2)
11993                                  (point)))
11994                 (insert-buffer-substring pgg-output-buffer))))))))
11995
11996 (defun gnus-summary-verify-article ()
11997   "Verify the current article in traditional PGP way."
11998   (interactive)
11999   (save-excursion
12000     (set-buffer gnus-original-article-buffer)
12001     (goto-char (point-min))
12002     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
12003       (error "Not a traditional PGP message!"))
12004     (re-search-forward "^-+END PGP" nil t)
12005     (beginning-of-line 2)
12006     (call-interactively (function pgg-verify-region))))
12007
12008 ;;;
12009 ;;; Generic summary marking commands
12010 ;;;
12011
12012 (defvar gnus-summary-marking-alist
12013   '((read gnus-del-mark "d")
12014     (unread gnus-unread-mark "u")
12015     (ticked gnus-ticked-mark "!")
12016     (dormant gnus-dormant-mark "?")
12017     (expirable gnus-expirable-mark "e"))
12018   "An alist of names/marks/keystrokes.")
12019
12020 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12021 (defvar gnus-summary-mark-map)
12022
12023 (defun gnus-summary-make-all-marking-commands ()
12024   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12025   (dolist (elem gnus-summary-marking-alist)
12026     (apply 'gnus-summary-make-marking-command elem)))
12027
12028 (defun gnus-summary-make-marking-command (name mark keystroke)
12029   (let ((map (make-sparse-keymap)))
12030     (define-key gnus-summary-generic-mark-map keystroke map)
12031     (dolist (lway `((next "next" next nil "n")
12032                     (next-unread "next unread" next t "N")
12033                     (prev "previous" prev nil "p")
12034                     (prev-unread "previous unread" prev t "P")
12035                     (nomove "" nil nil ,keystroke)))
12036       (let ((func (gnus-summary-make-marking-command-1
12037                    mark (car lway) lway name)))
12038         (setq func (eval func))
12039         (define-key map (nth 4 lway) func)))))
12040
12041 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12042   `(defun ,(intern
12043             (format "gnus-summary-put-mark-as-%s%s"
12044                     name (if (eq way 'nomove)
12045                              ""
12046                            (concat "-" (symbol-name way)))))
12047      (n)
12048      ,(format
12049        "Mark the current article as %s%s.
12050 If N, the prefix, then repeat N times.
12051 If N is negative, move in reverse order.
12052 The difference between N and the actual number of articles marked is
12053 returned."
12054        name (car (cdr lway)))
12055      (interactive "p")
12056      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12057
12058 (defun gnus-summary-generic-mark (n mark move unread)
12059   "Mark N articles with MARK."
12060   (unless (eq major-mode 'gnus-summary-mode)
12061     (error "This command can only be used in the summary buffer"))
12062   (gnus-summary-show-thread)
12063   (let ((nummove
12064          (cond
12065           ((eq move 'next) 1)
12066           ((eq move 'prev) -1)
12067           (t 0))))
12068     (if (zerop nummove)
12069         (setq n 1)
12070       (when (< n 0)
12071         (setq n (abs n)
12072               nummove (* -1 nummove))))
12073     (while (and (> n 0)
12074                 (gnus-summary-mark-article nil mark)
12075                 (zerop (gnus-summary-next-subject nummove unread t)))
12076       (setq n (1- n)))
12077     (when (/= 0 n)
12078       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12079     (gnus-summary-recenter)
12080     (gnus-summary-position-point)
12081     (gnus-set-mode-line 'summary)
12082     n))
12083
12084 (defun gnus-summary-insert-articles (articles)
12085   (when (setq articles
12086               (gnus-sorted-difference articles
12087                                       (mapcar (lambda (h)
12088                                                 (mail-header-number h))
12089                                               gnus-newsgroup-headers)))
12090     (setq gnus-newsgroup-headers
12091           (gnus-merge 'list
12092                       gnus-newsgroup-headers
12093                       (gnus-fetch-headers articles)
12094                       'gnus-article-sort-by-number))
12095     ;; Suppress duplicates?
12096     (when gnus-suppress-duplicates
12097       (gnus-dup-suppress-articles))
12098
12099     ;; We might want to build some more threads first.
12100     (when (and gnus-fetch-old-headers
12101                (eq gnus-headers-retrieved-by 'nov))
12102       (if (eq gnus-fetch-old-headers 'invisible)
12103           (gnus-build-all-threads)
12104         (gnus-build-old-threads)))
12105     ;; Let the Gnus agent mark articles as read.
12106     (when gnus-agent
12107       (gnus-agent-get-undownloaded-list))
12108     ;; Remove list identifiers from subject
12109     (when gnus-list-identifiers
12110       (gnus-summary-remove-list-identifiers))
12111     ;; First and last article in this newsgroup.
12112     (when gnus-newsgroup-headers
12113       (setq gnus-newsgroup-begin
12114             (mail-header-number (car gnus-newsgroup-headers))
12115             gnus-newsgroup-end
12116             (mail-header-number
12117              (gnus-last-element gnus-newsgroup-headers))))
12118     (when gnus-use-scoring
12119       (gnus-possibly-score-headers))))
12120
12121 (defun gnus-summary-insert-old-articles (&optional all)
12122   "Insert all old articles in this group.
12123 If ALL is non-nil, already read articles become readable.
12124 If ALL is a number, fetch this number of articles."
12125   (interactive "P")
12126   (prog1
12127       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12128             older len)
12129         (setq older
12130               ;; Some nntp servers lie about their active range.  When
12131               ;; this happens, the active range can be in the millions.
12132               ;; Use a compressed range to avoid creating a huge list.
12133               (gnus-range-difference (list gnus-newsgroup-active) old))
12134         (setq len (gnus-range-length older))
12135         (cond
12136          ((null older) nil)
12137          ((numberp all)
12138           (if (< all len)
12139               (let ((older-range (nreverse older)))
12140                 (setq older nil)
12141
12142                 (while (> all 0)
12143                   (let* ((r (pop older-range))
12144                          (min (if (numberp r) r (car r)))
12145                          (max (if (numberp r) r (cdr r))))
12146                     (while (and (<= min max)
12147                                 (> all 0))
12148                       (push max older)
12149                       (setq all (1- all)
12150                             max (1- max))))))
12151             (setq older (gnus-uncompress-range older))))
12152          (all
12153           (setq older (gnus-uncompress-range older)))
12154          (t
12155           (when (and (numberp gnus-large-newsgroup)
12156                    (> len gnus-large-newsgroup))
12157               (let* ((cursor-in-echo-area nil)
12158                      (initial (gnus-parameter-large-newsgroup-initial
12159                                gnus-newsgroup-name))
12160                      (input
12161                       (read-string
12162                        (format
12163                         "How many articles from %s (%s %d): "
12164                         (gnus-limit-string
12165                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12166                         (if initial "max" "default")
12167                         len)
12168                        (if initial
12169                            (cons (number-to-string initial)
12170                                  0)))))
12171                 (unless (string-match "^[ \t]*$" input)
12172                   (setq all (string-to-number input))
12173                   (if (< all len)
12174                       (let ((older-range (nreverse older)))
12175                         (setq older nil)
12176
12177                         (while (> all 0)
12178                           (let* ((r (pop older-range))
12179                                  (min (if (numberp r) r (car r)))
12180                                  (max (if (numberp r) r (cdr r))))
12181                             (while (and (<= min max)
12182                                         (> all 0))
12183                               (push max older)
12184                               (setq all (1- all)
12185                                     max (1- max))))))))))
12186           (setq older (gnus-uncompress-range older))))
12187         (if (not older)
12188             (message "No old news.")
12189           (gnus-summary-insert-articles older)
12190           (gnus-summary-limit (gnus-sorted-nunion old older))))
12191     (gnus-summary-position-point)))
12192
12193 (defun gnus-summary-insert-new-articles ()
12194   "Insert all new articles in this group."
12195   (interactive)
12196   (prog1
12197       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12198             (old-active gnus-newsgroup-active)
12199             (nnmail-fetched-sources (list t))
12200             i new)
12201         (setq gnus-newsgroup-active
12202               (gnus-activate-group gnus-newsgroup-name 'scan))
12203         (setq i (cdr gnus-newsgroup-active))
12204         (while (> i (cdr old-active))
12205           (push i new)
12206           (decf i))
12207         (if (not new)
12208             (message "No gnus is bad news")
12209           (gnus-summary-insert-articles new)
12210           (setq gnus-newsgroup-unreads
12211                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12212           (gnus-summary-limit (gnus-sorted-nunion old new))))
12213     (gnus-summary-position-point)))
12214
12215 (gnus-summary-make-all-marking-commands)
12216
12217 (gnus-ems-redefine)
12218
12219 (provide 'gnus-sum)
12220
12221 (run-hooks 'gnus-sum-load-hook)
12222
12223 ;; Local Variables:
12224 ;; coding: iso-8859-1
12225 ;; End:
12226
12227 ;;; gnus-sum.el ends here