(gnus-summary-make-menu-bar): Avoid the "Unrecognized menu descriptor" error
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary 2
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715
716 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
717   "*List of functions used for sorting articles in the summary buffer.
718
719 Each function takes two articles and returns non-nil if the first
720 article should be sorted before the other.  If you use more than one
721 function, the primary sort function should be the last.  You should
722 probably always include `gnus-article-sort-by-number' in the list of
723 sorting functions -- preferably first.  Also note that sorting by date
724 is often much slower than sorting by number, and the sorting order is
725 very similar.  (Sorting by date means sorting by the time the message
726 was sent, sorting by number means sorting by arrival time.)
727
728 Each item can also be a list `(not F)' where F is a function;
729 this reverses the sort order.
730
731 Ready-made functions include `gnus-article-sort-by-number',
732 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
733 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
734 and `gnus-article-sort-by-score'.
735
736 When threading is turned on, the variable `gnus-thread-sort-functions'
737 controls how articles are sorted."
738   :group 'gnus-summary-sort
739   :type '(repeat (gnus-widget-reversible
740                   (choice (function-item gnus-article-sort-by-number)
741                           (function-item gnus-article-sort-by-author)
742                           (function-item gnus-article-sort-by-subject)
743                           (function-item gnus-article-sort-by-date)
744                           (function-item gnus-article-sort-by-score)
745                           (function-item gnus-article-sort-by-random)
746                           (function :tag "other"))
747                   (boolean :tag "Reverse order"))))
748
749
750 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
751   "*List of functions used for sorting threads in the summary buffer.
752 By default, threads are sorted by article number.
753
754 Each function takes two threads and returns non-nil if the first
755 thread should be sorted before the other.  If you use more than one
756 function, the primary sort function should be the last.  You should
757 probably always include `gnus-thread-sort-by-number' in the list of
758 sorting functions -- preferably first.  Also note that sorting by date
759 is often much slower than sorting by number, and the sorting order is
760 very similar.  (Sorting by date means sorting by the time the message
761 was sent, sorting by number means sorting by arrival time.)
762
763 Each list item can also be a list `(not F)' where F is a
764 function; this specifies reversed sort order.
765
766 Ready-made functions include `gnus-thread-sort-by-number',
767 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
768 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
769 `gnus-thread-sort-by-most-recent-number',
770 `gnus-thread-sort-by-most-recent-date',
771 `gnus-thread-sort-by-random', and
772 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
773
774 When threading is turned off, the variable
775 `gnus-article-sort-functions' controls how articles are sorted."
776   :group 'gnus-summary-sort
777   :type '(repeat 
778           (gnus-widget-reversible
779            (choice (function-item gnus-thread-sort-by-number)
780                    (function-item gnus-thread-sort-by-author)
781                    (function-item gnus-thread-sort-by-subject)
782                    (function-item gnus-thread-sort-by-date)
783                    (function-item gnus-thread-sort-by-score)
784                    (function-item gnus-thread-sort-by-most-recent-number)
785                    (function-item gnus-thread-sort-by-most-recent-date)
786                    (function-item gnus-thread-sort-by-random)
787                    (function-item gnus-thread-sort-by-total-score)
788                    (function :tag "other"))
789            (boolean :tag "Reverse order"))))
790
791 (defcustom gnus-thread-score-function '+
792   "*Function used for calculating the total score of a thread.
793
794 The function is called with the scores of the article and each
795 subthread and should then return the score of the thread.
796
797 Some functions you can use are `+', `max', or `min'."
798   :group 'gnus-summary-sort
799   :type 'function)
800
801 (defcustom gnus-summary-expunge-below nil
802   "All articles that have a score less than this variable will be expunged.
803 This variable is local to the summary buffers."
804   :group 'gnus-score-default
805   :type '(choice (const :tag "off" nil)
806                  integer))
807
808 (defcustom gnus-thread-expunge-below nil
809   "All threads that have a total score less than this variable will be expunged.
810 See `gnus-thread-score-function' for en explanation of what a
811 \"thread score\" is.
812
813 This variable is local to the summary buffers."
814   :group 'gnus-threading
815   :group 'gnus-score-default
816   :type '(choice (const :tag "off" nil)
817                  integer))
818
819 (defcustom gnus-summary-mode-hook nil
820   "*A hook for Gnus summary mode.
821 This hook is run before any variables are set in the summary buffer."
822   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
823   :group 'gnus-summary-various
824   :type 'hook)
825
826 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
827 (when (featurep 'xemacs)
828   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
830   (add-hook 'gnus-summary-mode-hook
831             'gnus-xmas-switch-horizontal-scrollbar-off))
832
833 (defcustom gnus-summary-menu-hook nil
834   "*Hook run after the creation of the summary mode menu."
835   :group 'gnus-summary-visual
836   :type 'hook)
837
838 (defcustom gnus-summary-exit-hook nil
839   "*A hook called on exit from the summary buffer.
840 It will be called with point in the group buffer."
841   :group 'gnus-summary-exit
842   :type 'hook)
843
844 (defcustom gnus-summary-prepare-hook nil
845   "*A hook called after the summary buffer has been generated.
846 If you want to modify the summary buffer, you can use this hook."
847   :group 'gnus-summary-various
848   :type 'hook)
849
850 (defcustom gnus-summary-prepared-hook nil
851   "*A hook called as the last thing after the summary buffer has been generated."
852   :group 'gnus-summary-various
853   :type 'hook)
854
855 (defcustom gnus-summary-generate-hook nil
856   "*A hook run just before generating the summary buffer.
857 This hook is commonly used to customize threading variables and the
858 like."
859   :group 'gnus-summary-various
860   :type 'hook)
861
862 (defcustom gnus-select-group-hook nil
863   "*A hook called when a newsgroup is selected.
864
865 If you'd like to simplify subjects like the
866 `gnus-summary-next-same-subject' command does, you can use the
867 following hook:
868
869  (add-hook gnus-select-group-hook
870            (lambda ()
871              (mapcar (lambda (header)
872                        (mail-header-set-subject
873                         header
874                         (gnus-simplify-subject
875                          (mail-header-subject header) 're-only)))
876                      gnus-newsgroup-headers)))"
877   :group 'gnus-group-select
878   :type 'hook)
879
880 (defcustom gnus-select-article-hook nil
881   "*A hook called when an article is selected."
882   :group 'gnus-summary-choose
883   :options '(gnus-agent-fetch-selected-article)
884   :type 'hook)
885
886 (defcustom gnus-visual-mark-article-hook
887   (list 'gnus-highlight-selected-summary)
888   "*Hook run after selecting an article in the summary buffer.
889 It is meant to be used for highlighting the article in some way.  It
890 is not run if `gnus-visual' is nil."
891   :group 'gnus-summary-visual
892   :type 'hook)
893
894 (defcustom gnus-parse-headers-hook nil
895   "*A hook called before parsing the headers."
896   :group 'gnus-various
897   :type 'hook)
898
899 (defcustom gnus-exit-group-hook nil
900   "*A hook called when exiting summary mode.
901 This hook is not called from the non-updating exit commands like `Q'."
902   :group 'gnus-various
903   :type 'hook)
904
905 (defcustom gnus-summary-update-hook
906   (list 'gnus-summary-highlight-line)
907   "*A hook called when a summary line is changed.
908 The hook will not be called if `gnus-visual' is nil.
909
910 The default function `gnus-summary-highlight-line' will
911 highlight the line according to the `gnus-summary-highlight'
912 variable."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
917   "*A hook called when an article is selected for the first time.
918 The hook is intended to mark an article as read (or unread)
919 automatically when it is selected."
920   :group 'gnus-summary-choose
921   :type 'hook)
922
923 (defcustom gnus-group-no-more-groups-hook nil
924   "*A hook run when returning to group mode having no more (unread) groups."
925   :group 'gnus-group-select
926   :type 'hook)
927
928 (defcustom gnus-ps-print-hook nil
929   "*A hook run before ps-printing something from Gnus."
930   :group 'gnus-summary
931   :type 'hook)
932
933 (defcustom gnus-summary-article-move-hook nil
934   "*A hook called after an article is moved, copied, respooled, or crossposted."
935   :group 'gnus-summary
936   :type 'hook)
937
938 (defcustom gnus-summary-article-delete-hook nil
939   "*A hook called after an article is deleted."
940   :group 'gnus-summary
941   :type 'hook)
942
943 (defcustom gnus-summary-article-expire-hook nil
944   "*A hook called after an article is expired."
945   :group 'gnus-summary
946   :type 'hook)
947
948 (defcustom gnus-summary-display-arrow
949   (and (fboundp 'display-graphic-p)
950        (display-graphic-p))
951   "*If non-nil, display an arrow highlighting the current article."
952   :version "21.1"
953   :group 'gnus-summary
954   :type 'boolean)
955
956 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
957   "Face used for highlighting the current article in the summary buffer."
958   :group 'gnus-summary-visual
959   :type 'face)
960
961 (defvar gnus-tmp-downloaded nil)
962
963 (defcustom gnus-summary-highlight
964   '(((eq mark gnus-canceled-mark)
965      . gnus-summary-cancelled-face)
966     ((and uncached (> score default-high))
967      . gnus-summary-high-undownloaded-face)
968     ((and uncached (< score default-low))
969      . gnus-summary-low-undownloaded-face)
970     (uncached
971      . gnus-summary-normal-undownloaded-face)
972     ((and (> score default-high)
973           (or (eq mark gnus-dormant-mark)
974               (eq mark gnus-ticked-mark)))
975      . gnus-summary-high-ticked-face)
976     ((and (< score default-low)
977           (or (eq mark gnus-dormant-mark)
978               (eq mark gnus-ticked-mark)))
979      . gnus-summary-low-ticked-face)
980     ((or (eq mark gnus-dormant-mark)
981          (eq mark gnus-ticked-mark))
982      . gnus-summary-normal-ticked-face)
983     ((and (> score default-high) (eq mark gnus-ancient-mark))
984      . gnus-summary-high-ancient-face)
985     ((and (< score default-low) (eq mark gnus-ancient-mark))
986      . gnus-summary-low-ancient-face)
987     ((eq mark gnus-ancient-mark)
988      . gnus-summary-normal-ancient-face)
989     ((and (> score default-high) (eq mark gnus-unread-mark))
990      . gnus-summary-high-unread-face)
991     ((and (< score default-low) (eq mark gnus-unread-mark))
992      . gnus-summary-low-unread-face)
993     ((eq mark gnus-unread-mark)
994      . gnus-summary-normal-unread-face)
995     ((> score default-high)
996      . gnus-summary-high-read-face)
997     ((< score default-low)
998      . gnus-summary-low-read-face)
999     (t
1000      . gnus-summary-normal-read-face))
1001   "*Controls the highlighting of summary buffer lines.
1002
1003 A list of (FORM . FACE) pairs.  When deciding how a a particular
1004 summary line should be displayed, each form is evaluated.  The content
1005 of the face field after the first true form is used.  You can change
1006 how those summary lines are displayed, by editing the face field.
1007
1008 You can use the following variables in the FORM field.
1009
1010 score:        The article's score
1011 default:      The default article score.
1012 default-high: The default score for high scored articles.
1013 default-low:  The default score for low scored articles.
1014 below:        The score below which articles are automatically marked as read.
1015 mark:         The article's mark.
1016 uncached:     Non-nil if the article is uncached."
1017   :group 'gnus-summary-visual
1018   :type '(repeat (cons (sexp :tag "Form" nil)
1019                        face)))
1020
1021 (defcustom gnus-alter-header-function nil
1022   "Function called to allow alteration of article header structures.
1023 The function is called with one parameter, the article header vector,
1024 which it may alter in any way."
1025   :type '(choice (const :tag "None" nil)
1026                  function)
1027   :group 'gnus-summary)
1028
1029 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1030   "Variable that says which function should be used to decode a string with encoded words.")
1031
1032 (defcustom gnus-extra-headers '(To Newsgroups)
1033   "*Extra headers to parse."
1034   :version "21.1"
1035   :group 'gnus-summary
1036   :type '(repeat symbol))
1037
1038 (defcustom gnus-ignored-from-addresses
1039   (and user-mail-address (regexp-quote user-mail-address))
1040   "*Regexp of From headers that may be suppressed in favor of To headers."
1041   :version "21.1"
1042   :group 'gnus-summary
1043   :type 'regexp)
1044
1045 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1046   "List of charsets that should be ignored.
1047 When these charsets are used in the \"charset\" parameter, the
1048 default charset will be used instead."
1049   :version "21.1"
1050   :type '(repeat symbol)
1051   :group 'gnus-charset)
1052
1053 (gnus-define-group-parameter
1054  ignored-charsets
1055  :type list
1056  :function-document
1057  "Return the ignored charsets of GROUP."
1058  :variable gnus-group-ignored-charsets-alist
1059  :variable-default
1060  '(("alt\\.chinese\\.text" iso-8859-1))
1061  :variable-document
1062  "Alist of regexps (to match group names) and charsets that should be ignored.
1063 When these charsets are used in the \"charset\" parameter, the
1064 default charset will be used instead."
1065  :variable-group gnus-charset
1066  :variable-type '(repeat (cons (regexp :tag "Group")
1067                                (repeat symbol)))
1068  :parameter-type '(choice :tag "Ignored charsets"
1069                           :value nil
1070                           (repeat (symbol)))
1071  :parameter-document       "\
1072 List of charsets that should be ignored.
1073
1074 When these charsets are used in the \"charset\" parameter, the
1075 default charset will be used instead.")
1076
1077 (defcustom gnus-group-highlight-words-alist nil
1078   "Alist of group regexps and highlight regexps.
1079 This variable uses the same syntax as `gnus-emphasis-alist'."
1080   :version "21.1"
1081   :type '(repeat (cons (regexp :tag "Group")
1082                        (repeat (list (regexp :tag "Highlight regexp")
1083                                      (number :tag "Group for entire word" 0)
1084                                      (number :tag "Group for displayed part" 0)
1085                                      (symbol :tag "Face"
1086                                              gnus-emphasis-highlight-words)))))
1087   :group 'gnus-summary-visual)
1088
1089 (defcustom gnus-summary-show-article-charset-alist
1090   nil
1091   "Alist of number and charset.
1092 The article will be shown with the charset corresponding to the
1093 numbered argument.
1094 For example: ((1 . cn-gb-2312) (2 . big5))."
1095   :version "21.1"
1096   :type '(repeat (cons (number :tag "Argument" 1)
1097                        (symbol :tag "Charset")))
1098   :group 'gnus-charset)
1099
1100 (defcustom gnus-preserve-marks t
1101   "Whether marks are preserved when moving, copying and respooling messages."
1102   :version "21.1"
1103   :type 'boolean
1104   :group 'gnus-summary-marks)
1105
1106 (defcustom gnus-alter-articles-to-read-function nil
1107   "Function to be called to alter the list of articles to be selected."
1108   :type '(choice (const nil) function)
1109   :group 'gnus-summary)
1110
1111 (defcustom gnus-orphan-score nil
1112   "*All orphans get this score added.  Set in the score file."
1113   :group 'gnus-score-default
1114   :type '(choice (const nil)
1115                  integer))
1116
1117 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1118   "*A regexp to match MIME parts when saving multiple parts of a
1119 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1120 This regexp will be used by default when prompting the user for which
1121 type of files to save."
1122   :group 'gnus-summary
1123   :type 'regexp)
1124
1125 (defcustom gnus-read-all-available-headers nil
1126   "Whether Gnus should parse all headers made available to it.
1127 This is mostly relevant for slow back ends where the user may
1128 wish to widen the summary buffer to include all headers
1129 that were fetched.  Say, for nnultimate groups."
1130   :group 'gnus-summary
1131   :type '(choice boolean regexp))
1132
1133 (defcustom gnus-summary-muttprint-program "muttprint"
1134   "Command (and optional arguments) used to run Muttprint."
1135   :version "21.3"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-article-loose-mime t
1140   "If non-nil, don't require MIME-Version header.
1141 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1142 supply the MIME-Version header or deliberately strip it from the mail.
1143 If non-nil (the default), Gnus will treat some articles as MIME
1144 even if the MIME-Version header is missing."
1145   :version "21.3"
1146   :type 'boolean
1147   :group 'gnus-article-mime)
1148
1149 (defcustom gnus-article-emulate-mime t
1150   "If non-nil, use MIME emulation for uuencode and the like.
1151 This means that Gnus will search message bodies for text that look
1152 like uuencoded bits, yEncoded bits, and so on, and present that using
1153 the normal Gnus MIME machinery."
1154   :type 'boolean
1155   :group 'gnus-article-mime)
1156
1157 ;;; Internal variables
1158
1159 (defvar gnus-summary-display-cache nil)
1160 (defvar gnus-article-mime-handles nil)
1161 (defvar gnus-article-decoded-p nil)
1162 (defvar gnus-article-charset nil)
1163 (defvar gnus-article-ignored-charsets nil)
1164 (defvar gnus-scores-exclude-files nil)
1165 (defvar gnus-page-broken nil)
1166
1167 (defvar gnus-original-article nil)
1168 (defvar gnus-article-internal-prepare-hook nil)
1169 (defvar gnus-newsgroup-process-stack nil)
1170
1171 (defvar gnus-thread-indent-array nil)
1172 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1173 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1174   "Function called to sort the articles within a thread after it has been gathered together.")
1175
1176 (defvar gnus-summary-save-parts-type-history nil)
1177 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1178
1179 ;; Avoid highlighting in kill files.
1180 (defvar gnus-summary-inhibit-highlight nil)
1181 (defvar gnus-newsgroup-selected-overlay nil)
1182 (defvar gnus-inhibit-limiting nil)
1183 (defvar gnus-newsgroup-adaptive-score-file nil)
1184 (defvar gnus-current-score-file nil)
1185 (defvar gnus-current-move-group nil)
1186 (defvar gnus-current-copy-group nil)
1187 (defvar gnus-current-crosspost-group nil)
1188 (defvar gnus-newsgroup-display nil)
1189
1190 (defvar gnus-newsgroup-dependencies nil)
1191 (defvar gnus-newsgroup-adaptive nil)
1192 (defvar gnus-summary-display-article-function nil)
1193 (defvar gnus-summary-highlight-line-function nil
1194   "Function called after highlighting a summary line.")
1195
1196 (defvar gnus-summary-line-format-alist
1197   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1198     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1199     (?s gnus-tmp-subject-or-nil ?s)
1200     (?n gnus-tmp-name ?s)
1201     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1202         ?s)
1203     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1204             gnus-tmp-from) ?s)
1205     (?F gnus-tmp-from ?s)
1206     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1207     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1208     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1209     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1210     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1211     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1212     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1213     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1214     (?L gnus-tmp-lines ?s)
1215     (?O gnus-tmp-downloaded ?c)
1216     (?I gnus-tmp-indentation ?s)
1217     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1218     (?R gnus-tmp-replied ?c)
1219     (?\[ gnus-tmp-opening-bracket ?c)
1220     (?\] gnus-tmp-closing-bracket ?c)
1221     (?\> (make-string gnus-tmp-level ? ) ?s)
1222     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1223     (?i gnus-tmp-score ?d)
1224     (?z gnus-tmp-score-char ?c)
1225     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1226     (?U gnus-tmp-unread ?c)
1227     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1228         ?s)
1229     (?t (gnus-summary-number-of-articles-in-thread
1230          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1231         ?d)
1232     (?e (gnus-summary-number-of-articles-in-thread
1233          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1234         ?c)
1235     (?u gnus-tmp-user-defined ?s)
1236     (?P (gnus-pick-line-number) ?d)
1237     (?B gnus-tmp-thread-tree-header-string ?s)
1238     (user-date (gnus-user-date
1239                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1240   "An alist of format specifications that can appear in summary lines.
1241 These are paired with what variables they correspond with, along with
1242 the type of the variable (string, integer, character, etc).")
1243
1244 (defvar gnus-summary-dummy-line-format-alist
1245   `((?S gnus-tmp-subject ?s)
1246     (?N gnus-tmp-number ?d)
1247     (?u gnus-tmp-user-defined ?s)))
1248
1249 (defvar gnus-summary-mode-line-format-alist
1250   `((?G gnus-tmp-group-name ?s)
1251     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1252     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1253     (?A gnus-tmp-article-number ?d)
1254     (?Z gnus-tmp-unread-and-unselected ?s)
1255     (?V gnus-version ?s)
1256     (?U gnus-tmp-unread-and-unticked ?d)
1257     (?S gnus-tmp-subject ?s)
1258     (?e gnus-tmp-unselected ?d)
1259     (?u gnus-tmp-user-defined ?s)
1260     (?d (length gnus-newsgroup-dormant) ?d)
1261     (?t (length gnus-newsgroup-marked) ?d)
1262     (?h (length gnus-newsgroup-spam-marked) ?d)
1263     (?r (length gnus-newsgroup-reads) ?d)
1264     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1265     (?E gnus-newsgroup-expunged-tally ?d)
1266     (?s (gnus-current-score-file-nondirectory) ?s)))
1267
1268 (defvar gnus-last-search-regexp nil
1269   "Default regexp for article search command.")
1270
1271 (defvar gnus-last-shell-command nil
1272   "Default shell command on article.")
1273
1274 (defvar gnus-newsgroup-agentized nil
1275   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1276 (defvar gnus-newsgroup-begin nil)
1277 (defvar gnus-newsgroup-end nil)
1278 (defvar gnus-newsgroup-last-rmail nil)
1279 (defvar gnus-newsgroup-last-mail nil)
1280 (defvar gnus-newsgroup-last-folder nil)
1281 (defvar gnus-newsgroup-last-file nil)
1282 (defvar gnus-newsgroup-auto-expire nil)
1283 (defvar gnus-newsgroup-active nil)
1284
1285 (defvar gnus-newsgroup-data nil)
1286 (defvar gnus-newsgroup-data-reverse nil)
1287 (defvar gnus-newsgroup-limit nil)
1288 (defvar gnus-newsgroup-limits nil)
1289 (defvar gnus-summary-use-undownloaded-faces nil)
1290
1291 (defvar gnus-newsgroup-unreads nil
1292   "Sorted list of unread articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unselected nil
1295   "Sorted list of unselected unread articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-reads nil
1298   "Alist of read articles and article marks in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-expunged-tally nil)
1301
1302 (defvar gnus-newsgroup-marked nil
1303   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1304
1305 (defvar gnus-newsgroup-spam-marked nil
1306   "List of ranges of articles that have been marked as spam.")
1307
1308 (defvar gnus-newsgroup-killed nil
1309   "List of ranges of articles that have been through the scoring process.")
1310
1311 (defvar gnus-newsgroup-cached nil
1312   "Sorted list of articles that come from the article cache.")
1313
1314 (defvar gnus-newsgroup-saved nil
1315   "List of articles that have been saved.")
1316
1317 (defvar gnus-newsgroup-kill-headers nil)
1318
1319 (defvar gnus-newsgroup-replied nil
1320   "List of articles that have been replied to in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-forwarded nil
1323   "List of articles that have been forwarded in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-recent nil
1326   "List of articles that have are recent in the current newsgroup.")
1327
1328 (defvar gnus-newsgroup-expirable nil
1329   "Sorted list of articles in the current newsgroup that can be expired.")
1330
1331 (defvar gnus-newsgroup-processable nil
1332   "List of articles in the current newsgroup that can be processed.")
1333
1334 (defvar gnus-newsgroup-downloadable nil
1335   "Sorted list of articles in the current newsgroup that can be processed.")
1336
1337 (defvar gnus-newsgroup-unfetched nil
1338   "Sorted list of articles in the current newsgroup whose headers have
1339 not been fetched into the agent.
1340
1341 This list will always be a subset of gnus-newsgroup-undownloaded.")
1342
1343 (defvar gnus-newsgroup-undownloaded nil
1344   "List of articles in the current newsgroup that haven't been downloaded.")
1345
1346 (defvar gnus-newsgroup-unsendable nil
1347   "List of articles in the current newsgroup that won't be sent.")
1348
1349 (defvar gnus-newsgroup-bookmarks nil
1350   "List of articles in the current newsgroup that have bookmarks.")
1351
1352 (defvar gnus-newsgroup-dormant nil
1353   "Sorted list of dormant articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-unseen nil
1356   "List of unseen articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-seen nil
1359   "Range of seen articles in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-articles nil
1362   "List of articles in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-scored nil
1365   "List of scored articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-headers nil
1368   "List of article headers in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-threads nil)
1371
1372 (defvar gnus-newsgroup-prepared nil
1373   "Whether the current group has been prepared properly.")
1374
1375 (defvar gnus-newsgroup-ancient nil
1376   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-sparse nil)
1379
1380 (defvar gnus-current-article nil)
1381 (defvar gnus-article-current nil)
1382 (defvar gnus-current-headers nil)
1383 (defvar gnus-have-all-headers nil)
1384 (defvar gnus-last-article nil)
1385 (defvar gnus-newsgroup-history nil)
1386 (defvar gnus-newsgroup-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1389
1390 (defvar gnus-article-before-search nil)
1391
1392 (defvar gnus-summary-local-variables
1393   '(gnus-newsgroup-name
1394     gnus-newsgroup-begin gnus-newsgroup-end
1395     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1396     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1397     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1398     gnus-newsgroup-unselected gnus-newsgroup-marked
1399     gnus-newsgroup-spam-marked
1400     gnus-newsgroup-reads gnus-newsgroup-saved
1401     gnus-newsgroup-replied gnus-newsgroup-forwarded
1402     gnus-newsgroup-recent
1403     gnus-newsgroup-expirable
1404     gnus-newsgroup-processable gnus-newsgroup-killed
1405     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1406     gnus-newsgroup-unfetched
1407     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1408     gnus-newsgroup-seen gnus-newsgroup-articles
1409     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1410     gnus-newsgroup-headers gnus-newsgroup-threads
1411     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1412     gnus-current-article gnus-current-headers gnus-have-all-headers
1413     gnus-last-article gnus-article-internal-prepare-hook
1414     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1415     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1416     gnus-thread-expunge-below
1417     gnus-score-alist gnus-current-score-file
1418     (gnus-summary-expunge-below . global)
1419     (gnus-summary-mark-below . global)
1420     (gnus-orphan-score . global)
1421     gnus-newsgroup-active gnus-scores-exclude-files
1422     gnus-newsgroup-history gnus-newsgroup-ancient
1423     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1424     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1425     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1426     (gnus-newsgroup-expunged-tally . 0)
1427     gnus-cache-removable-articles gnus-newsgroup-cached
1428     gnus-newsgroup-data gnus-newsgroup-data-reverse
1429     gnus-newsgroup-limit gnus-newsgroup-limits
1430     gnus-newsgroup-charset gnus-newsgroup-display
1431     gnus-summary-use-undownloaded-faces)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default expressions to be evalled (when the default
1438 values are not nil), that should be made global while the summary buffer
1439 is active.
1440
1441 Note: The default expressions will be evaluated (using function `eval')
1442 before assignment to the local variable rather than just assigned to it.
1443 If the default expression is the symbol `global', that symbol will not
1444 be evaluated but the global value of the local variable will be used
1445 instead.
1446
1447 These variables can be used to set variables in the group parameters
1448 while still allowing them to affect operations done in other buffers.
1449 For example:
1450
1451 \(setq gnus-newsgroup-variables
1452      '(message-use-followup-to
1453        (gnus-visible-headers .
1454          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1455 ")
1456
1457 ;; Byte-compiler warning.
1458 (eval-when-compile
1459   ;; Bind features so that require will believe that gnus-sum has
1460   ;; already been loaded (avoids infinite recursion)
1461   (let ((features (cons 'gnus-sum features)))
1462     ;; Several of the declarations in gnus-sum are needed to load the
1463     ;; following files. Right now, these definitions have been
1464     ;; compiled but not defined (evaluated).  We could either do a
1465     ;; eval-and-compile about all of the declarations or evaluate the
1466     ;; source file.
1467     (if (boundp 'gnus-newsgroup-variables)
1468         nil
1469       (load "gnus-sum.el" t t t))
1470     (require 'gnus)
1471     (require 'gnus-agent)
1472     (require 'gnus-art)))
1473
1474 ;; MIME stuff.
1475
1476 (defvar gnus-decode-encoded-word-methods
1477   '(mail-decode-encoded-word-string)
1478   "List of methods used to decode encoded words.
1479
1480 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1481 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1482 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1483 whose names match REGEXP.
1484
1485 For example:
1486 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1487  mail-decode-encoded-word-string
1488  (\"chinese\" . rfc1843-decode-string))")
1489
1490 (defvar gnus-decode-encoded-word-methods-cache nil)
1491
1492 (defun gnus-multi-decode-encoded-word-string (string)
1493   "Apply the functions from `gnus-encoded-word-methods' that match."
1494   (unless (and gnus-decode-encoded-word-methods-cache
1495                (eq gnus-newsgroup-name
1496                    (car gnus-decode-encoded-word-methods-cache)))
1497     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1498     (mapcar (lambda (x)
1499               (if (symbolp x)
1500                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1501                 (if (and gnus-newsgroup-name
1502                          (string-match (car x) gnus-newsgroup-name))
1503                     (nconc gnus-decode-encoded-word-methods-cache
1504                            (list (cdr x))))))
1505             gnus-decode-encoded-word-methods))
1506   (let ((xlist gnus-decode-encoded-word-methods-cache))
1507     (pop xlist)
1508     (while xlist
1509       (setq string (funcall (pop xlist) string))))
1510   string)
1511
1512 ;; Subject simplification.
1513
1514 (defun gnus-simplify-whitespace (str)
1515   "Remove excessive whitespace from STR."
1516   ;; Multiple spaces.
1517   (while (string-match "[ \t][ \t]+" str)
1518     (setq str (concat (substring str 0 (match-beginning 0))
1519                         " "
1520                         (substring str (match-end 0)))))
1521   ;; Leading spaces.
1522   (when (string-match "^[ \t]+" str)
1523     (setq str (substring str (match-end 0))))
1524   ;; Trailing spaces.
1525   (when (string-match "[ \t]+$" str)
1526     (setq str (substring str 0 (match-beginning 0))))
1527   str)
1528
1529 (defun gnus-simplify-all-whitespace (str)
1530   "Remove all whitespace from STR."
1531   (while (string-match "[ \t\n]+" str)
1532     (setq str (replace-match "" nil nil str)))
1533   str)
1534
1535 (defsubst gnus-simplify-subject-re (subject)
1536   "Remove \"Re:\" from subject lines."
1537   (if (string-match message-subject-re-regexp subject)
1538       (substring subject (match-end 0))
1539     subject))
1540
1541 (defun gnus-simplify-subject (subject &optional re-only)
1542   "Remove `Re:' and words in parentheses.
1543 If RE-ONLY is non-nil, strip leading `Re:'s only."
1544   (let ((case-fold-search t))           ;Ignore case.
1545     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1546     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1547       (setq subject (substring subject (match-end 0))))
1548     ;; Remove uninteresting prefixes.
1549     (when (and (not re-only)
1550                gnus-simplify-ignored-prefixes
1551                (string-match gnus-simplify-ignored-prefixes subject))
1552       (setq subject (substring subject (match-end 0))))
1553     ;; Remove words in parentheses from end.
1554     (unless re-only
1555       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1556         (setq subject (substring subject 0 (match-beginning 0)))))
1557     ;; Return subject string.
1558     subject))
1559
1560 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1561 ;; all whitespace.
1562 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1563   (goto-char (point-min))
1564   (while (re-search-forward regexp nil t)
1565     (replace-match (or newtext ""))))
1566
1567 (defun gnus-simplify-buffer-fuzzy ()
1568   "Simplify string in the buffer fuzzily.
1569 The string in the accessible portion of the current buffer is simplified.
1570 It is assumed to be a single-line subject.
1571 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1572 matter is removed.  Additional things can be deleted by setting
1573 `gnus-simplify-subject-fuzzy-regexp'."
1574   (let ((case-fold-search t)
1575         (modified-tick))
1576     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1577
1578     (while (not (eq modified-tick (buffer-modified-tick)))
1579       (setq modified-tick (buffer-modified-tick))
1580       (cond
1581        ((listp gnus-simplify-subject-fuzzy-regexp)
1582         (mapcar 'gnus-simplify-buffer-fuzzy-step
1583                 gnus-simplify-subject-fuzzy-regexp))
1584        (gnus-simplify-subject-fuzzy-regexp
1585         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1586       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1587       (gnus-simplify-buffer-fuzzy-step
1588        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1589       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1590
1591     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1592     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1593     (gnus-simplify-buffer-fuzzy-step " $")
1594     (gnus-simplify-buffer-fuzzy-step "^ +")))
1595
1596 (defun gnus-simplify-subject-fuzzy (subject)
1597   "Simplify a subject string fuzzily.
1598 See `gnus-simplify-buffer-fuzzy' for details."
1599   (save-excursion
1600     (gnus-set-work-buffer)
1601     (let ((case-fold-search t))
1602       ;; Remove uninteresting prefixes.
1603       (when (and gnus-simplify-ignored-prefixes
1604                  (string-match gnus-simplify-ignored-prefixes subject))
1605         (setq subject (substring subject (match-end 0))))
1606       (insert subject)
1607       (inline (gnus-simplify-buffer-fuzzy))
1608       (buffer-string))))
1609
1610 (defsubst gnus-simplify-subject-fully (subject)
1611   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1612   (cond
1613    (gnus-simplify-subject-functions
1614     (gnus-map-function gnus-simplify-subject-functions subject))
1615    ((null gnus-summary-gather-subject-limit)
1616     (gnus-simplify-subject-re subject))
1617    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1618     (gnus-simplify-subject-fuzzy subject))
1619    ((numberp gnus-summary-gather-subject-limit)
1620     (gnus-limit-string (gnus-simplify-subject-re subject)
1621                        gnus-summary-gather-subject-limit))
1622    (t
1623     subject)))
1624
1625 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1626   "Check whether two subjects are equal.
1627 If optional argument SIMPLE-FIRST is t, first argument is already
1628 simplified."
1629   (cond
1630    ((null simple-first)
1631     (equal (gnus-simplify-subject-fully s1)
1632            (gnus-simplify-subject-fully s2)))
1633    (t
1634     (equal s1
1635            (gnus-simplify-subject-fully s2)))))
1636
1637 (defun gnus-summary-bubble-group ()
1638   "Increase the score of the current group.
1639 This is a handy function to add to `gnus-summary-exit-hook' to
1640 increase the score of each group you read."
1641   (gnus-group-add-score gnus-newsgroup-name))
1642
1643 \f
1644 ;;;
1645 ;;; Gnus summary mode
1646 ;;;
1647
1648 (put 'gnus-summary-mode 'mode-class 'special)
1649
1650 (defvar gnus-article-commands-menu)
1651
1652 ;; Non-orthogonal keys
1653
1654 (gnus-define-keys gnus-summary-mode-map
1655   " " gnus-summary-next-page
1656   "\177" gnus-summary-prev-page
1657   [delete] gnus-summary-prev-page
1658   [backspace] gnus-summary-prev-page
1659   "\r" gnus-summary-scroll-up
1660   "\M-\r" gnus-summary-scroll-down
1661   "n" gnus-summary-next-unread-article
1662   "p" gnus-summary-prev-unread-article
1663   "N" gnus-summary-next-article
1664   "P" gnus-summary-prev-article
1665   "\M-\C-n" gnus-summary-next-same-subject
1666   "\M-\C-p" gnus-summary-prev-same-subject
1667   "\M-n" gnus-summary-next-unread-subject
1668   "\M-p" gnus-summary-prev-unread-subject
1669   "." gnus-summary-first-unread-article
1670   "," gnus-summary-best-unread-article
1671   "\M-s" gnus-summary-search-article-forward
1672   "\M-r" gnus-summary-search-article-backward
1673   "<" gnus-summary-beginning-of-article
1674   ">" gnus-summary-end-of-article
1675   "j" gnus-summary-goto-article
1676   "^" gnus-summary-refer-parent-article
1677   "\M-^" gnus-summary-refer-article
1678   "u" gnus-summary-tick-article-forward
1679   "!" gnus-summary-tick-article-forward
1680   "U" gnus-summary-tick-article-backward
1681   "d" gnus-summary-mark-as-read-forward
1682   "D" gnus-summary-mark-as-read-backward
1683   "E" gnus-summary-mark-as-expirable
1684   "\M-u" gnus-summary-clear-mark-forward
1685   "\M-U" gnus-summary-clear-mark-backward
1686   "k" gnus-summary-kill-same-subject-and-select
1687   "\C-k" gnus-summary-kill-same-subject
1688   "\M-\C-k" gnus-summary-kill-thread
1689   "\M-\C-l" gnus-summary-lower-thread
1690   "e" gnus-summary-edit-article
1691   "#" gnus-summary-mark-as-processable
1692   "\M-#" gnus-summary-unmark-as-processable
1693   "\M-\C-t" gnus-summary-toggle-threads
1694   "\M-\C-s" gnus-summary-show-thread
1695   "\M-\C-h" gnus-summary-hide-thread
1696   "\M-\C-f" gnus-summary-next-thread
1697   "\M-\C-b" gnus-summary-prev-thread
1698   [(meta down)] gnus-summary-next-thread
1699   [(meta up)] gnus-summary-prev-thread
1700   "\M-\C-u" gnus-summary-up-thread
1701   "\M-\C-d" gnus-summary-down-thread
1702   "&" gnus-summary-execute-command
1703   "c" gnus-summary-catchup-and-exit
1704   "\C-w" gnus-summary-mark-region-as-read
1705   "\C-t" gnus-summary-toggle-truncation
1706   "?" gnus-summary-mark-as-dormant
1707   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1708   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1709   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1710   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1711   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1712   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1713   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1714   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1715   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1716   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1717   "=" gnus-summary-expand-window
1718   "\C-x\C-s" gnus-summary-reselect-current-group
1719   "\M-g" gnus-summary-rescan-group
1720   "w" gnus-summary-stop-page-breaking
1721   "\C-c\C-r" gnus-summary-caesar-message
1722   "f" gnus-summary-followup
1723   "F" gnus-summary-followup-with-original
1724   "C" gnus-summary-cancel-article
1725   "r" gnus-summary-reply
1726   "R" gnus-summary-reply-with-original
1727   "\C-c\C-f" gnus-summary-mail-forward
1728   "o" gnus-summary-save-article
1729   "\C-o" gnus-summary-save-article-mail
1730   "|" gnus-summary-pipe-output
1731   "\M-k" gnus-summary-edit-local-kill
1732   "\M-K" gnus-summary-edit-global-kill
1733   ;; "V" gnus-version
1734   "\C-c\C-d" gnus-summary-describe-group
1735   "q" gnus-summary-exit
1736   "Q" gnus-summary-exit-no-update
1737   "\C-c\C-i" gnus-info-find-node
1738   gnus-mouse-2 gnus-mouse-pick-article
1739   "m" gnus-summary-mail-other-window
1740   "a" gnus-summary-post-news
1741   "i" gnus-summary-news-other-window
1742   "x" gnus-summary-limit-to-unread
1743   "s" gnus-summary-isearch-article
1744   "t" gnus-summary-toggle-header
1745   "g" gnus-summary-show-article
1746   "l" gnus-summary-goto-last-article
1747   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1748   "\C-d" gnus-summary-enter-digest-group
1749   "\M-\C-d" gnus-summary-read-document
1750   "\M-\C-e" gnus-summary-edit-parameters
1751   "\M-\C-a" gnus-summary-customize-parameters
1752   "\C-c\C-b" gnus-bug
1753   "*" gnus-cache-enter-article
1754   "\M-*" gnus-cache-remove-article
1755   "\M-&" gnus-summary-universal-argument
1756   "\C-l" gnus-recenter
1757   "I" gnus-summary-increase-score
1758   "L" gnus-summary-lower-score
1759   "\M-i" gnus-symbolic-argument
1760   "h" gnus-summary-select-article-buffer
1761
1762   "b" gnus-article-view-part
1763   "\M-t" gnus-summary-toggle-display-buttonized
1764
1765   "V" gnus-summary-score-map
1766   "X" gnus-uu-extract-map
1767   "S" gnus-summary-send-map)
1768
1769 ;; Sort of orthogonal keymap
1770 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1771   "t" gnus-summary-tick-article-forward
1772   "!" gnus-summary-tick-article-forward
1773   "d" gnus-summary-mark-as-read-forward
1774   "r" gnus-summary-mark-as-read-forward
1775   "c" gnus-summary-clear-mark-forward
1776   " " gnus-summary-clear-mark-forward
1777   "e" gnus-summary-mark-as-expirable
1778   "x" gnus-summary-mark-as-expirable
1779   "?" gnus-summary-mark-as-dormant
1780   "b" gnus-summary-set-bookmark
1781   "B" gnus-summary-remove-bookmark
1782   "#" gnus-summary-mark-as-processable
1783   "\M-#" gnus-summary-unmark-as-processable
1784   "S" gnus-summary-limit-include-expunged
1785   "C" gnus-summary-catchup
1786   "H" gnus-summary-catchup-to-here
1787   "h" gnus-summary-catchup-from-here
1788   "\C-c" gnus-summary-catchup-all
1789   "k" gnus-summary-kill-same-subject-and-select
1790   "K" gnus-summary-kill-same-subject
1791   "P" gnus-uu-mark-map)
1792
1793 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1794   "c" gnus-summary-clear-above
1795   "u" gnus-summary-tick-above
1796   "m" gnus-summary-mark-above
1797   "k" gnus-summary-kill-below)
1798
1799 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1800   "/" gnus-summary-limit-to-subject
1801   "n" gnus-summary-limit-to-articles
1802   "w" gnus-summary-pop-limit
1803   "s" gnus-summary-limit-to-subject
1804   "a" gnus-summary-limit-to-author
1805   "u" gnus-summary-limit-to-unread
1806   "m" gnus-summary-limit-to-marks
1807   "M" gnus-summary-limit-exclude-marks
1808   "v" gnus-summary-limit-to-score
1809   "*" gnus-summary-limit-include-cached
1810   "D" gnus-summary-limit-include-dormant
1811   "T" gnus-summary-limit-include-thread
1812   "d" gnus-summary-limit-exclude-dormant
1813   "t" gnus-summary-limit-to-age
1814   "." gnus-summary-limit-to-unseen
1815   "x" gnus-summary-limit-to-extra
1816   "p" gnus-summary-limit-to-display-predicate
1817   "E" gnus-summary-limit-include-expunged
1818   "c" gnus-summary-limit-exclude-childless-dormant
1819   "C" gnus-summary-limit-mark-excluded-as-read
1820   "o" gnus-summary-insert-old-articles
1821   "N" gnus-summary-insert-new-articles
1822   "r" gnus-summary-limit-to-replied)
1823
1824 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1825   "n" gnus-summary-next-unread-article
1826   "p" gnus-summary-prev-unread-article
1827   "N" gnus-summary-next-article
1828   "P" gnus-summary-prev-article
1829   "\C-n" gnus-summary-next-same-subject
1830   "\C-p" gnus-summary-prev-same-subject
1831   "\M-n" gnus-summary-next-unread-subject
1832   "\M-p" gnus-summary-prev-unread-subject
1833   "f" gnus-summary-first-unread-article
1834   "b" gnus-summary-best-unread-article
1835   "j" gnus-summary-goto-article
1836   "g" gnus-summary-goto-subject
1837   "l" gnus-summary-goto-last-article
1838   "o" gnus-summary-pop-article)
1839
1840 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1841   "k" gnus-summary-kill-thread
1842   "l" gnus-summary-lower-thread
1843   "i" gnus-summary-raise-thread
1844   "T" gnus-summary-toggle-threads
1845   "t" gnus-summary-rethread-current
1846   "^" gnus-summary-reparent-thread
1847   "s" gnus-summary-show-thread
1848   "S" gnus-summary-show-all-threads
1849   "h" gnus-summary-hide-thread
1850   "H" gnus-summary-hide-all-threads
1851   "n" gnus-summary-next-thread
1852   "p" gnus-summary-prev-thread
1853   "u" gnus-summary-up-thread
1854   "o" gnus-summary-top-thread
1855   "d" gnus-summary-down-thread
1856   "#" gnus-uu-mark-thread
1857   "\M-#" gnus-uu-unmark-thread)
1858
1859 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1860   "g" gnus-summary-prepare
1861   "c" gnus-summary-insert-cached-articles
1862   "d" gnus-summary-insert-dormant-articles)
1863
1864 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1865   "c" gnus-summary-catchup-and-exit
1866   "C" gnus-summary-catchup-all-and-exit
1867   "E" gnus-summary-exit-no-update
1868   "Q" gnus-summary-exit
1869   "Z" gnus-summary-exit
1870   "n" gnus-summary-catchup-and-goto-next-group
1871   "R" gnus-summary-reselect-current-group
1872   "G" gnus-summary-rescan-group
1873   "N" gnus-summary-next-group
1874   "s" gnus-summary-save-newsrc
1875   "P" gnus-summary-prev-group)
1876
1877 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1878   " " gnus-summary-next-page
1879   "n" gnus-summary-next-page
1880   "\177" gnus-summary-prev-page
1881   [delete] gnus-summary-prev-page
1882   "p" gnus-summary-prev-page
1883   "\r" gnus-summary-scroll-up
1884   "\M-\r" gnus-summary-scroll-down
1885   "<" gnus-summary-beginning-of-article
1886   ">" gnus-summary-end-of-article
1887   "b" gnus-summary-beginning-of-article
1888   "e" gnus-summary-end-of-article
1889   "^" gnus-summary-refer-parent-article
1890   "r" gnus-summary-refer-parent-article
1891   "D" gnus-summary-enter-digest-group
1892   "R" gnus-summary-refer-references
1893   "T" gnus-summary-refer-thread
1894   "g" gnus-summary-show-article
1895   "s" gnus-summary-isearch-article
1896   "P" gnus-summary-print-article
1897   "M" gnus-mailing-list-insinuate
1898   "t" gnus-article-babel)
1899
1900 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1901   "b" gnus-article-add-buttons
1902   "B" gnus-article-add-buttons-to-head
1903   "o" gnus-article-treat-overstrike
1904   "e" gnus-article-emphasize
1905   "w" gnus-article-fill-cited-article
1906   "Q" gnus-article-fill-long-lines
1907   "C" gnus-article-capitalize-sentences
1908   "c" gnus-article-remove-cr
1909   "q" gnus-article-de-quoted-unreadable
1910   "6" gnus-article-de-base64-unreadable
1911   "Z" gnus-article-decode-HZ
1912   "A" gnus-article-treat-ansi-sequences
1913   "h" gnus-article-wash-html
1914   "u" gnus-article-unsplit-urls
1915   "s" gnus-summary-force-verify-and-decrypt
1916   "f" gnus-article-display-x-face
1917   "l" gnus-summary-stop-page-breaking
1918   "r" gnus-summary-caesar-message
1919   "m" gnus-summary-morse-message
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-treat-smiley
1922   "v" gnus-summary-verbose-headers
1923   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1924   "p" gnus-article-verify-x-pgp-sig
1925   "d" gnus-article-treat-dumbquotes)
1926
1927 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1928   ;; mnemonic: deuglif*Y*
1929   "u" gnus-article-outlook-unwrap-lines
1930   "a" gnus-article-outlook-repair-attribution
1931   "c" gnus-article-outlook-rearrange-citation
1932   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1933
1934 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1935   "a" gnus-article-hide
1936   "h" gnus-article-hide-headers
1937   "b" gnus-article-hide-boring-headers
1938   "s" gnus-article-hide-signature
1939   "c" gnus-article-hide-citation
1940   "C" gnus-article-hide-citation-in-followups
1941   "l" gnus-article-hide-list-identifiers
1942   "B" gnus-article-strip-banner
1943   "P" gnus-article-hide-pem
1944   "\C-c" gnus-article-hide-citation-maybe)
1945
1946 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1947   "a" gnus-article-highlight
1948   "h" gnus-article-highlight-headers
1949   "c" gnus-article-highlight-citation
1950   "s" gnus-article-highlight-signature)
1951
1952 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1953   "f" gnus-article-treat-fold-headers
1954   "u" gnus-article-treat-unfold-headers
1955   "n" gnus-article-treat-fold-newsgroups)
1956
1957 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1958   "x" gnus-article-display-x-face
1959   "d" gnus-article-display-face
1960   "s" gnus-treat-smiley
1961   "D" gnus-article-remove-images
1962   "f" gnus-treat-from-picon
1963   "m" gnus-treat-mail-picon
1964   "n" gnus-treat-newsgroups-picon)
1965
1966 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1967   "w" gnus-article-decode-mime-words
1968   "c" gnus-article-decode-charset
1969   "v" gnus-mime-view-all-parts
1970   "b" gnus-article-view-part)
1971
1972 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1973   "z" gnus-article-date-ut
1974   "u" gnus-article-date-ut
1975   "l" gnus-article-date-local
1976   "p" gnus-article-date-english
1977   "e" gnus-article-date-lapsed
1978   "o" gnus-article-date-original
1979   "i" gnus-article-date-iso8601
1980   "s" gnus-article-date-user)
1981
1982 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1983   "t" gnus-article-remove-trailing-blank-lines
1984   "l" gnus-article-strip-leading-blank-lines
1985   "m" gnus-article-strip-multiple-blank-lines
1986   "a" gnus-article-strip-blank-lines
1987   "A" gnus-article-strip-all-blank-lines
1988   "s" gnus-article-strip-leading-space
1989   "e" gnus-article-strip-trailing-space
1990   "w" gnus-article-remove-leading-whitespace)
1991
1992 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1993   "v" gnus-version
1994   "f" gnus-summary-fetch-faq
1995   "d" gnus-summary-describe-group
1996   "h" gnus-summary-describe-briefly
1997   "i" gnus-info-find-node
1998   "c" gnus-group-fetch-charter
1999   "C" gnus-group-fetch-control)
2000
2001 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2002   "e" gnus-summary-expire-articles
2003   "\M-\C-e" gnus-summary-expire-articles-now
2004   "\177" gnus-summary-delete-article
2005   [delete] gnus-summary-delete-article
2006   [backspace] gnus-summary-delete-article
2007   "m" gnus-summary-move-article
2008   "r" gnus-summary-respool-article
2009   "w" gnus-summary-edit-article
2010   "c" gnus-summary-copy-article
2011   "B" gnus-summary-crosspost-article
2012   "q" gnus-summary-respool-query
2013   "t" gnus-summary-respool-trace
2014   "i" gnus-summary-import-article
2015   "I" gnus-summary-create-article
2016   "p" gnus-summary-article-posted-p)
2017
2018 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2019   "o" gnus-summary-save-article
2020   "m" gnus-summary-save-article-mail
2021   "F" gnus-summary-write-article-file
2022   "r" gnus-summary-save-article-rmail
2023   "f" gnus-summary-save-article-file
2024   "b" gnus-summary-save-article-body-file
2025   "h" gnus-summary-save-article-folder
2026   "v" gnus-summary-save-article-vm
2027   "p" gnus-summary-pipe-output
2028   "P" gnus-summary-muttprint
2029   "s" gnus-soup-add-article)
2030
2031 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2032   "b" gnus-summary-display-buttonized
2033   "m" gnus-summary-repair-multipart
2034   "v" gnus-article-view-part
2035   "o" gnus-article-save-part
2036   "c" gnus-article-copy-part
2037   "C" gnus-article-view-part-as-charset
2038   "e" gnus-article-view-part-externally
2039   "E" gnus-article-encrypt-body
2040   "i" gnus-article-inline-part
2041   "|" gnus-article-pipe-part)
2042
2043 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2044   "p" gnus-summary-mark-as-processable
2045   "u" gnus-summary-unmark-as-processable
2046   "U" gnus-summary-unmark-all-processable
2047   "v" gnus-uu-mark-over
2048   "s" gnus-uu-mark-series
2049   "r" gnus-uu-mark-region
2050   "g" gnus-uu-unmark-region
2051   "R" gnus-uu-mark-by-regexp
2052   "G" gnus-uu-unmark-by-regexp
2053   "t" gnus-uu-mark-thread
2054   "T" gnus-uu-unmark-thread
2055   "a" gnus-uu-mark-all
2056   "b" gnus-uu-mark-buffer
2057   "S" gnus-uu-mark-sparse
2058   "k" gnus-summary-kill-process-mark
2059   "y" gnus-summary-yank-process-mark
2060   "w" gnus-summary-save-process-mark
2061   "i" gnus-uu-invert-processable)
2062
2063 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2064   ;;"x" gnus-uu-extract-any
2065   "m" gnus-summary-save-parts
2066   "u" gnus-uu-decode-uu
2067   "U" gnus-uu-decode-uu-and-save
2068   "s" gnus-uu-decode-unshar
2069   "S" gnus-uu-decode-unshar-and-save
2070   "o" gnus-uu-decode-save
2071   "O" gnus-uu-decode-save
2072   "b" gnus-uu-decode-binhex
2073   "B" gnus-uu-decode-binhex
2074   "p" gnus-uu-decode-postscript
2075   "P" gnus-uu-decode-postscript-and-save)
2076
2077 (gnus-define-keys
2078     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2079   "u" gnus-uu-decode-uu-view
2080   "U" gnus-uu-decode-uu-and-save-view
2081   "s" gnus-uu-decode-unshar-view
2082   "S" gnus-uu-decode-unshar-and-save-view
2083   "o" gnus-uu-decode-save-view
2084   "O" gnus-uu-decode-save-view
2085   "b" gnus-uu-decode-binhex-view
2086   "B" gnus-uu-decode-binhex-view
2087   "p" gnus-uu-decode-postscript-view
2088   "P" gnus-uu-decode-postscript-and-save-view)
2089
2090 (defvar gnus-article-post-menu nil)
2091
2092 (defconst gnus-summary-menu-maxlen 20)
2093
2094 (defun gnus-summary-menu-split (menu)
2095   ;; If we have lots of elements, divide them into groups of 20
2096   ;; and make a pane (or submenu) for each one.
2097   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2098       (let ((menu menu) sublists next
2099             (i 1))
2100         (while menu
2101           ;; Pull off the next gnus-summary-menu-maxlen elements
2102           ;; and make them the next element of sublist.
2103           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2104           (if next
2105               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2106                       nil))
2107           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2108                                              (aref (car (last menu)) 0)) menu)
2109                                sublists))
2110           (setq i (1+ i))
2111           (setq menu next))
2112         (nreverse sublists))
2113     ;; Few elements--put them all in one pane.
2114     menu))
2115
2116 (defun gnus-summary-make-menu-bar ()
2117   (gnus-turn-off-edit-menu 'summary)
2118
2119   (unless (boundp 'gnus-summary-misc-menu)
2120
2121     (easy-menu-define
2122       gnus-summary-kill-menu gnus-summary-mode-map ""
2123       (cons
2124        "Score"
2125        (nconc
2126         (list
2127          ["Customize" gnus-score-customize t])
2128         (gnus-make-score-map 'increase)
2129         (gnus-make-score-map 'lower)
2130         '(("Mark"
2131            ["Kill below" gnus-summary-kill-below t]
2132            ["Mark above" gnus-summary-mark-above t]
2133            ["Tick above" gnus-summary-tick-above t]
2134            ["Clear above" gnus-summary-clear-above t])
2135           ["Current score" gnus-summary-current-score t]
2136           ["Set score" gnus-summary-set-score t]
2137           ["Switch current score file..." gnus-score-change-score-file t]
2138           ["Set mark below..." gnus-score-set-mark-below t]
2139           ["Set expunge below..." gnus-score-set-expunge-below t]
2140           ["Edit current score file" gnus-score-edit-current-scores t]
2141           ["Edit score file" gnus-score-edit-file t]
2142           ["Trace score" gnus-score-find-trace t]
2143           ["Find words" gnus-score-find-favourite-words t]
2144           ["Rescore buffer" gnus-summary-rescore t]
2145           ["Increase score..." gnus-summary-increase-score t]
2146           ["Lower score..." gnus-summary-lower-score t]))))
2147
2148     ;; Define both the Article menu in the summary buffer and the
2149     ;; equivalent Commands menu in the article buffer here for
2150     ;; consistency.
2151     (let ((innards
2152            `(("Hide"
2153               ["All" gnus-article-hide t]
2154               ["Headers" gnus-article-hide-headers t]
2155               ["Signature" gnus-article-hide-signature t]
2156               ["Citation" gnus-article-hide-citation t]
2157               ["List identifiers" gnus-article-hide-list-identifiers t]
2158               ["Banner" gnus-article-strip-banner t]
2159               ["Boring headers" gnus-article-hide-boring-headers t])
2160              ("Highlight"
2161               ["All" gnus-article-highlight t]
2162               ["Headers" gnus-article-highlight-headers t]
2163               ["Signature" gnus-article-highlight-signature t]
2164               ["Citation" gnus-article-highlight-citation t])
2165              ("MIME"
2166               ["Words" gnus-article-decode-mime-words t]
2167               ["Charset" gnus-article-decode-charset t]
2168               ["QP" gnus-article-de-quoted-unreadable t]
2169               ["Base64" gnus-article-de-base64-unreadable t]
2170               ["View MIME buttons" gnus-summary-display-buttonized t]
2171               ["View all" gnus-mime-view-all-parts t]
2172               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2173               ["Encrypt body" gnus-article-encrypt-body
2174                :active (not (gnus-group-read-only-p))
2175                ,@(if (featurep 'xemacs) nil
2176                    '(:help "Encrypt the message body on disk"))]
2177               ["Extract all parts..." gnus-summary-save-parts t]
2178               ("Multipart"
2179                ["Repair multipart" gnus-summary-repair-multipart t]
2180                ["Pipe part..." gnus-article-pipe-part t]
2181                ["Inline part" gnus-article-inline-part t]
2182                ["Encrypt body" gnus-article-encrypt-body
2183                 :active (not (gnus-group-read-only-p))
2184                ,@(if (featurep 'xemacs) nil
2185                    '(:help "Encrypt the message body on disk"))]
2186                ["View part externally" gnus-article-view-part-externally t]
2187                ["View part with charset..." gnus-article-view-part-as-charset t]
2188                ["Copy part" gnus-article-copy-part t]
2189                ["Save part..." gnus-article-save-part t]
2190                ["View part" gnus-article-view-part t]))
2191              ("Date"
2192               ["Local" gnus-article-date-local t]
2193               ["ISO8601" gnus-article-date-iso8601 t]
2194               ["UT" gnus-article-date-ut t]
2195               ["Original" gnus-article-date-original t]
2196               ["Lapsed" gnus-article-date-lapsed t]
2197               ["User-defined" gnus-article-date-user t])
2198              ("Display"
2199               ["Remove images" gnus-article-remove-images t]
2200               ["Toggle smiley" gnus-treat-smiley t]
2201               ["Show X-Face" gnus-article-display-x-face t]
2202               ["Show picons in From" gnus-treat-from-picon t]
2203               ["Show picons in mail headers" gnus-treat-mail-picon t]
2204               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2205               ("View as different encoding"
2206                ,@(gnus-summary-menu-split
2207                   (mapcar
2208                    (lambda (cs)
2209                      ;; Since easymenu under Emacs doesn't allow
2210                      ;; lambda forms for menu commands, we should
2211                      ;; provide intern'ed function symbols.
2212                      (let ((command (intern (format "\
2213 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2214                        (fset command
2215                              `(lambda ()
2216                                 (interactive)
2217                                 (let ((gnus-summary-show-article-charset-alist
2218                                        '((1 . ,cs))))
2219                                   (gnus-summary-show-article 1))))
2220                        `[,(symbol-name cs) ,command t]))
2221                    (sort (if (fboundp 'coding-system-list)
2222                              (coding-system-list)
2223                            (mapcar 'car mm-mime-mule-charset-alist))
2224                          'string<)))))
2225              ("Washing"
2226               ("Remove Blanks"
2227                ["Leading" gnus-article-strip-leading-blank-lines t]
2228                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2229                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2230                ["All of the above" gnus-article-strip-blank-lines t]
2231                ["All" gnus-article-strip-all-blank-lines t]
2232                ["Leading space" gnus-article-strip-leading-space t]
2233                ["Trailing space" gnus-article-strip-trailing-space t]
2234                ["Leading space in headers"
2235                 gnus-article-remove-leading-whitespace t])
2236               ["Overstrike" gnus-article-treat-overstrike t]
2237               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2238               ["Emphasis" gnus-article-emphasize t]
2239               ["Word wrap" gnus-article-fill-cited-article t]
2240               ["Fill long lines" gnus-article-fill-long-lines t]
2241               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2242               ["Remove CR" gnus-article-remove-cr t]
2243               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2244               ["Base64" gnus-article-de-base64-unreadable t]
2245               ["Rot 13" gnus-summary-caesar-message
2246                ,@(if (featurep 'xemacs) '(t)
2247                    '(:help "\"Caesar rotate\" article by 13"))]
2248               ["Morse decode" gnus-summary-morse-message t]
2249               ["Unix pipe..." gnus-summary-pipe-message t]
2250               ["Add buttons" gnus-article-add-buttons t]
2251               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2252               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2253               ["Verbose header" gnus-summary-verbose-headers t]
2254               ["Toggle header" gnus-summary-toggle-header t]
2255               ["Unfold headers" gnus-article-treat-unfold-headers t]
2256               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2257               ["Html" gnus-article-wash-html t]
2258               ["Unsplit URLs" gnus-article-unsplit-urls t]
2259               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2260               ["Decode HZ" gnus-article-decode-HZ t]
2261               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2262               ("(Outlook) Deuglify"
2263                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2264                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2265                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2266                ["Full (Outlook) deuglify"
2267                 gnus-article-outlook-deuglify-article t])
2268               )
2269              ("Output"
2270               ["Save in default format..." gnus-summary-save-article
2271                ,@(if (featurep 'xemacs) '(t)
2272                    '(:help "Save article using default method"))]
2273               ["Save in file..." gnus-summary-save-article-file
2274                ,@(if (featurep 'xemacs) '(t)
2275                    '(:help "Save article in file"))]
2276               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2277               ["Save in MH folder..." gnus-summary-save-article-folder t]
2278               ["Save in VM folder..." gnus-summary-save-article-vm t]
2279               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2280               ["Save body in file..." gnus-summary-save-article-body-file t]
2281               ["Pipe through a filter..." gnus-summary-pipe-output t]
2282               ["Add to SOUP packet" gnus-soup-add-article t]
2283               ["Print with Muttprint..." gnus-summary-muttprint t]
2284               ["Print" gnus-summary-print-article
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "Generate and print a PostScript image"))])
2287              ("Copy, move,... (Backend)"
2288               ,@(if (featurep 'xemacs) nil
2289                   '(:help "Copying, moving, expiring articles..."))
2290               ["Respool article..." gnus-summary-respool-article t]
2291               ["Move article..." gnus-summary-move-article
2292                (gnus-check-backend-function
2293                 'request-move-article gnus-newsgroup-name)]
2294               ["Copy article..." gnus-summary-copy-article t]
2295               ["Crosspost article..." gnus-summary-crosspost-article
2296                (gnus-check-backend-function
2297                 'request-replace-article gnus-newsgroup-name)]
2298               ["Import file..." gnus-summary-import-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Create article..." gnus-summary-create-article
2302                (gnus-check-backend-function
2303                 'request-accept-article gnus-newsgroup-name)]
2304               ["Check if posted" gnus-summary-article-posted-p t]
2305               ["Edit article" gnus-summary-edit-article
2306                (not (gnus-group-read-only-p))]
2307               ["Delete article" gnus-summary-delete-article
2308                (gnus-check-backend-function
2309                 'request-expire-articles gnus-newsgroup-name)]
2310               ["Query respool" gnus-summary-respool-query t]
2311               ["Trace respool" gnus-summary-respool-trace t]
2312               ["Delete expirable articles" gnus-summary-expire-articles-now
2313                (gnus-check-backend-function
2314                 'request-expire-articles gnus-newsgroup-name)])
2315              ("Extract"
2316               ["Uudecode" gnus-uu-decode-uu
2317                ,@(if (featurep 'xemacs) '(t)
2318                    '(:help "Decode uuencoded article(s)"))]
2319               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2320               ["Unshar" gnus-uu-decode-unshar t]
2321               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2322               ["Save" gnus-uu-decode-save t]
2323               ["Binhex" gnus-uu-decode-binhex t]
2324               ["Postscript" gnus-uu-decode-postscript t]
2325               ["All MIME parts" gnus-summary-save-parts t])
2326              ("Cache"
2327               ["Enter article" gnus-cache-enter-article t]
2328               ["Remove article" gnus-cache-remove-article t])
2329              ["Translate" gnus-article-babel t]
2330              ["Select article buffer" gnus-summary-select-article-buffer t]
2331              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2332              ["Isearch article..." gnus-summary-isearch-article t]
2333              ["Beginning of the article" gnus-summary-beginning-of-article t]
2334              ["End of the article" gnus-summary-end-of-article t]
2335              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2336              ["Fetch referenced articles" gnus-summary-refer-references t]
2337              ["Fetch current thread" gnus-summary-refer-thread t]
2338              ["Fetch article with id..." gnus-summary-refer-article t]
2339              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2340              ["Redisplay" gnus-summary-show-article t]
2341              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2342       (easy-menu-define
2343         gnus-summary-article-menu gnus-summary-mode-map ""
2344         (cons "Article" innards))
2345
2346       (if (not (keymapp gnus-summary-article-menu))
2347           (easy-menu-define
2348             gnus-article-commands-menu gnus-article-mode-map ""
2349             (cons "Commands" innards))
2350         ;; in Emacs, don't share menu.
2351         (setq gnus-article-commands-menu
2352               (copy-keymap gnus-summary-article-menu))
2353         (define-key gnus-article-mode-map [menu-bar commands]
2354           (cons "Commands" gnus-article-commands-menu))))
2355
2356     (easy-menu-define
2357       gnus-summary-thread-menu gnus-summary-mode-map ""
2358       '("Threads"
2359         ["Find all messages in thread" gnus-summary-refer-thread t]
2360         ["Toggle threading" gnus-summary-toggle-threads t]
2361         ["Hide threads" gnus-summary-hide-all-threads t]
2362         ["Show threads" gnus-summary-show-all-threads t]
2363         ["Hide thread" gnus-summary-hide-thread t]
2364         ["Show thread" gnus-summary-show-thread t]
2365         ["Go to next thread" gnus-summary-next-thread t]
2366         ["Go to previous thread" gnus-summary-prev-thread t]
2367         ["Go down thread" gnus-summary-down-thread t]
2368         ["Go up thread" gnus-summary-up-thread t]
2369         ["Top of thread" gnus-summary-top-thread t]
2370         ["Mark thread as read" gnus-summary-kill-thread t]
2371         ["Lower thread score" gnus-summary-lower-thread t]
2372         ["Raise thread score" gnus-summary-raise-thread t]
2373         ["Rethread current" gnus-summary-rethread-current t]))
2374
2375     (easy-menu-define
2376       gnus-summary-post-menu gnus-summary-mode-map ""
2377       `("Post"
2378         ["Send a message (mail or news)" gnus-summary-post-news
2379          ,@(if (featurep 'xemacs) '(t)
2380              '(:help "Compose a new message (mail or news)"))]
2381         ["Followup" gnus-summary-followup
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Post followup to this article"))]
2384         ["Followup and yank" gnus-summary-followup-with-original
2385          ,@(if (featurep 'xemacs) '(t)
2386              '(:help "Post followup to this article, quoting its contents"))]
2387         ["Supersede article" gnus-summary-supersede-article t]
2388         ["Cancel article" gnus-summary-cancel-article
2389          ,@(if (featurep 'xemacs) '(t)
2390              '(:help "Cancel an article you posted"))]
2391         ["Reply" gnus-summary-reply t]
2392         ["Reply and yank" gnus-summary-reply-with-original t]
2393         ["Wide reply" gnus-summary-wide-reply t]
2394         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2395          ,@(if (featurep 'xemacs) '(t)
2396              '(:help "Mail a reply, quoting this article"))]
2397         ["Very wide reply" gnus-summary-very-wide-reply t]
2398         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2399          ,@(if (featurep 'xemacs) '(t)
2400              '(:help "Mail a very wide reply, quoting this article"))]
2401         ["Mail forward" gnus-summary-mail-forward t]
2402         ["Post forward" gnus-summary-post-forward t]
2403         ["Digest and mail" gnus-uu-digest-mail-forward t]
2404         ["Digest and post" gnus-uu-digest-post-forward t]
2405         ["Resend message" gnus-summary-resend-message t]
2406         ["Resend message edit" gnus-summary-resend-message-edit t]
2407         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2408         ["Send a mail" gnus-summary-mail-other-window t]
2409         ["Create a local message" gnus-summary-news-other-window t]
2410         ["Uuencode and post" gnus-uu-post-news
2411          ,@(if (featurep 'xemacs) '(t)
2412              '(:help "Post a uuencoded article"))]
2413         ["Followup via news" gnus-summary-followup-to-mail t]
2414         ["Followup via news and yank"
2415          gnus-summary-followup-to-mail-with-original t]
2416         ;;("Draft"
2417         ;;["Send" gnus-summary-send-draft t]
2418         ;;["Send bounced" gnus-resend-bounced-mail t])
2419         ))
2420
2421     (cond
2422      ((not (keymapp gnus-summary-post-menu))
2423       (setq gnus-article-post-menu gnus-summary-post-menu))
2424      ((not gnus-article-post-menu)
2425       ;; Don't share post menu.
2426       (setq gnus-article-post-menu
2427             (copy-keymap gnus-summary-post-menu))))
2428     (define-key gnus-article-mode-map [menu-bar post]
2429       (cons "Post" gnus-article-post-menu))
2430
2431     (easy-menu-define
2432       gnus-summary-misc-menu gnus-summary-mode-map ""
2433       `("Gnus"
2434         ("Mark Read"
2435          ["Mark as read" gnus-summary-mark-as-read-forward t]
2436          ["Mark same subject and select"
2437           gnus-summary-kill-same-subject-and-select t]
2438          ["Mark same subject" gnus-summary-kill-same-subject t]
2439          ["Catchup" gnus-summary-catchup
2440           ,@(if (featurep 'xemacs) '(t)
2441               '(:help "Mark unread articles in this group as read"))]
2442          ["Catchup all" gnus-summary-catchup-all t]
2443          ["Catchup to here" gnus-summary-catchup-to-here t]
2444          ["Catchup from here" gnus-summary-catchup-from-here t]
2445          ["Catchup region" gnus-summary-mark-region-as-read
2446           (gnus-mark-active-p)]
2447          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2448         ("Mark Various"
2449          ["Tick" gnus-summary-tick-article-forward t]
2450          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2451          ["Remove marks" gnus-summary-clear-mark-forward t]
2452          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2453          ["Set bookmark" gnus-summary-set-bookmark t]
2454          ["Remove bookmark" gnus-summary-remove-bookmark t])
2455         ("Limit to"
2456          ["Marks..." gnus-summary-limit-to-marks t]
2457          ["Subject..." gnus-summary-limit-to-subject t]
2458          ["Author..." gnus-summary-limit-to-author t]
2459          ["Age..." gnus-summary-limit-to-age t]
2460          ["Extra..." gnus-summary-limit-to-extra t]
2461          ["Score..." gnus-summary-limit-to-score t]
2462          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2463          ["Unread" gnus-summary-limit-to-unread t]
2464          ["Unseen" gnus-summary-limit-to-unseen t]
2465          ["Replied" gnus-summary-limit-to-replied t]
2466          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2467          ["Next articles" gnus-summary-limit-to-articles t]
2468          ["Pop limit" gnus-summary-pop-limit t]
2469          ["Show dormant" gnus-summary-limit-include-dormant t]
2470          ["Hide childless dormant"
2471           gnus-summary-limit-exclude-childless-dormant t]
2472          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2473          ["Hide marked" gnus-summary-limit-exclude-marks t]
2474          ["Show expunged" gnus-summary-limit-include-expunged t])
2475         ("Process Mark"
2476          ["Set mark" gnus-summary-mark-as-processable t]
2477          ["Remove mark" gnus-summary-unmark-as-processable t]
2478          ["Remove all marks" gnus-summary-unmark-all-processable t]
2479          ["Invert marks" gnus-uu-invert-processable t]
2480          ["Mark above" gnus-uu-mark-over t]
2481          ["Mark series" gnus-uu-mark-series t]
2482          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2483          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2484          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2485          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2486          ["Mark all" gnus-uu-mark-all t]
2487          ["Mark buffer" gnus-uu-mark-buffer t]
2488          ["Mark sparse" gnus-uu-mark-sparse t]
2489          ["Mark thread" gnus-uu-mark-thread t]
2490          ["Unmark thread" gnus-uu-unmark-thread t]
2491          ("Process Mark Sets"
2492           ["Kill" gnus-summary-kill-process-mark t]
2493           ["Yank" gnus-summary-yank-process-mark
2494            gnus-newsgroup-process-stack]
2495           ["Save" gnus-summary-save-process-mark t]
2496           ["Run command on marked..." gnus-summary-universal-argument t]))
2497         ("Scroll article"
2498          ["Page forward" gnus-summary-next-page
2499           ,@(if (featurep 'xemacs) '(t)
2500               '(:help "Show next page of article"))]
2501          ["Page backward" gnus-summary-prev-page
2502           ,@(if (featurep 'xemacs) '(t)
2503               '(:help "Show previous page of article"))]
2504          ["Line forward" gnus-summary-scroll-up t])
2505         ("Move"
2506          ["Next unread article" gnus-summary-next-unread-article t]
2507          ["Previous unread article" gnus-summary-prev-unread-article t]
2508          ["Next article" gnus-summary-next-article t]
2509          ["Previous article" gnus-summary-prev-article t]
2510          ["Next unread subject" gnus-summary-next-unread-subject t]
2511          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2512          ["Next article same subject" gnus-summary-next-same-subject t]
2513          ["Previous article same subject" gnus-summary-prev-same-subject t]
2514          ["First unread article" gnus-summary-first-unread-article t]
2515          ["Best unread article" gnus-summary-best-unread-article t]
2516          ["Go to subject number..." gnus-summary-goto-subject t]
2517          ["Go to article number..." gnus-summary-goto-article t]
2518          ["Go to the last article" gnus-summary-goto-last-article t]
2519          ["Pop article off history" gnus-summary-pop-article t])
2520         ("Sort"
2521          ["Sort by number" gnus-summary-sort-by-number t]
2522          ["Sort by author" gnus-summary-sort-by-author t]
2523          ["Sort by subject" gnus-summary-sort-by-subject t]
2524          ["Sort by date" gnus-summary-sort-by-date t]
2525          ["Sort by score" gnus-summary-sort-by-score t]
2526          ["Sort by lines" gnus-summary-sort-by-lines t]
2527          ["Sort by characters" gnus-summary-sort-by-chars t]
2528          ["Randomize" gnus-summary-sort-by-random t]
2529          ["Original sort" gnus-summary-sort-by-original t])
2530         ("Help"
2531          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2532          ["Describe group" gnus-summary-describe-group t]
2533          ["Fetch charter" gnus-group-fetch-charter
2534           ,@(if (featurep 'xemacs) nil
2535               '(:help "Display the charter of the current group"))]
2536          ["Fetch control message" gnus-group-fetch-control
2537           ,@(if (featurep 'xemacs) nil
2538               '(:help "Display the archived control message for the current group"))]
2539          ["Read manual" gnus-info-find-node t])
2540         ("Modes"
2541          ["Pick and read" gnus-pick-mode t]
2542          ["Binary" gnus-binary-mode t])
2543         ("Regeneration"
2544          ["Regenerate" gnus-summary-prepare t]
2545          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2546          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2547          ["Toggle threading" gnus-summary-toggle-threads t])
2548         ["See old articles" gnus-summary-insert-old-articles t]
2549         ["See new articles" gnus-summary-insert-new-articles t]
2550         ["Filter articles..." gnus-summary-execute-command t]
2551         ["Run command on articles..." gnus-summary-universal-argument t]
2552         ["Search articles forward..." gnus-summary-search-article-forward t]
2553         ["Search articles backward..." gnus-summary-search-article-backward t]
2554         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2555         ["Expand window" gnus-summary-expand-window t]
2556         ["Expire expirable articles" gnus-summary-expire-articles
2557          (gnus-check-backend-function
2558           'request-expire-articles gnus-newsgroup-name)]
2559         ["Edit local kill file" gnus-summary-edit-local-kill t]
2560         ["Edit main kill file" gnus-summary-edit-global-kill t]
2561         ["Edit group parameters" gnus-summary-edit-parameters t]
2562         ["Customize group parameters" gnus-summary-customize-parameters t]
2563         ["Send a bug report" gnus-bug t]
2564         ("Exit"
2565          ["Catchup and exit" gnus-summary-catchup-and-exit
2566           ,@(if (featurep 'xemacs) '(t)
2567               '(:help "Mark unread articles in this group as read, then exit"))]
2568          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2569          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2570          ["Exit group" gnus-summary-exit
2571           ,@(if (featurep 'xemacs) '(t)
2572               '(:help "Exit current group, return to group selection mode"))]
2573          ["Exit group without updating" gnus-summary-exit-no-update t]
2574          ["Exit and goto next group" gnus-summary-next-group t]
2575          ["Exit and goto prev group" gnus-summary-prev-group t]
2576          ["Reselect group" gnus-summary-reselect-current-group t]
2577          ["Rescan group" gnus-summary-rescan-group t]
2578          ["Update dribble" gnus-summary-save-newsrc t])))
2579
2580     (gnus-run-hooks 'gnus-summary-menu-hook)))
2581
2582 (defvar gnus-summary-tool-bar-map nil)
2583
2584 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2585 (defun gnus-summary-make-tool-bar ()
2586   (if (and (fboundp 'tool-bar-add-item-from-menu)
2587            (default-value 'tool-bar-mode)
2588            (not gnus-summary-tool-bar-map))
2589       (setq gnus-summary-tool-bar-map
2590             (let ((tool-bar-map (make-sparse-keymap))
2591                   (load-path (mm-image-load-path)))
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-post-news "post" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-followup "followup" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-summary-reply "reply" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2620               (tool-bar-add-item-from-menu
2621                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2622               (tool-bar-add-item-from-menu
2623                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2624               tool-bar-map)))
2625   (if gnus-summary-tool-bar-map
2626       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2627
2628 (defun gnus-score-set-default (var value)
2629   "A version of set that updates the GNU Emacs menu-bar."
2630   (set var value)
2631   ;; It is the message that forces the active status to be updated.
2632   (message ""))
2633
2634 (defun gnus-make-score-map (type)
2635   "Make a summary score map of type TYPE."
2636   (if t
2637       nil
2638     (let ((headers '(("author" "from" string)
2639                      ("subject" "subject" string)
2640                      ("article body" "body" string)
2641                      ("article head" "head" string)
2642                      ("xref" "xref" string)
2643                      ("extra header" "extra" string)
2644                      ("lines" "lines" number)
2645                      ("followups to author" "followup" string)))
2646           (types '((number ("less than" <)
2647                            ("greater than" >)
2648                            ("equal" =))
2649                    (string ("substring" s)
2650                            ("exact string" e)
2651                            ("fuzzy string" f)
2652                            ("regexp" r))))
2653           (perms '(("temporary" (current-time-string))
2654                    ("permanent" nil)
2655                    ("immediate" now)))
2656           header)
2657       (list
2658        (apply
2659         'nconc
2660         (list
2661          (if (eq type 'lower)
2662              "Lower score"
2663            "Increase score"))
2664         (let (outh)
2665           (while headers
2666             (setq header (car headers))
2667             (setq outh
2668                   (cons
2669                    (apply
2670                     'nconc
2671                     (list (car header))
2672                     (let ((ts (cdr (assoc (nth 2 header) types)))
2673                           outt)
2674                       (while ts
2675                         (setq outt
2676                               (cons
2677                                (apply
2678                                 'nconc
2679                                 (list (caar ts))
2680                                 (let ((ps perms)
2681                                       outp)
2682                                   (while ps
2683                                     (setq outp
2684                                           (cons
2685                                            (vector
2686                                             (caar ps)
2687                                             (list
2688                                              'gnus-summary-score-entry
2689                                              (nth 1 header)
2690                                              (if (or (string= (nth 1 header)
2691                                                               "head")
2692                                                      (string= (nth 1 header)
2693                                                               "body"))
2694                                                  ""
2695                                                (list 'gnus-summary-header
2696                                                      (nth 1 header)))
2697                                              (list 'quote (nth 1 (car ts)))
2698                                              (list 'gnus-score-delta-default
2699                                                    nil)
2700                                              (nth 1 (car ps))
2701                                              t)
2702                                             t)
2703                                            outp))
2704                                     (setq ps (cdr ps)))
2705                                   (list (nreverse outp))))
2706                                outt))
2707                         (setq ts (cdr ts)))
2708                       (list (nreverse outt))))
2709                    outh))
2710             (setq headers (cdr headers)))
2711           (list (nreverse outh))))))))
2712
2713 \f
2714
2715 (defun gnus-summary-mode (&optional group)
2716   "Major mode for reading articles.
2717
2718 All normal editing commands are switched off.
2719 \\<gnus-summary-mode-map>
2720 Each line in this buffer represents one article.  To read an
2721 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2722 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2723 respectively.
2724
2725 You can also post articles and send mail from this buffer.  To
2726 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2727 of an article, type `\\[gnus-summary-reply]'.
2728
2729 There are approx. one gazillion commands you can execute in this
2730 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2731
2732 The following commands are available:
2733
2734 \\{gnus-summary-mode-map}"
2735   (interactive)
2736   (kill-all-local-variables)
2737   (when (gnus-visual-p 'summary-menu 'menu)
2738     (gnus-summary-make-menu-bar)
2739     (gnus-summary-make-tool-bar))
2740   (gnus-summary-make-local-variables)
2741   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2742     (gnus-summary-make-local-variables))
2743   (gnus-make-thread-indent-array)
2744   (gnus-simplify-mode-line)
2745   (setq major-mode 'gnus-summary-mode)
2746   (setq mode-name "Summary")
2747   (make-local-variable 'minor-mode-alist)
2748   (use-local-map gnus-summary-mode-map)
2749   (buffer-disable-undo)
2750   (setq buffer-read-only t              ;Disable modification
2751         show-trailing-whitespace nil)
2752   (setq truncate-lines t)
2753   (setq selective-display t)
2754   (setq selective-display-ellipses t)   ;Display `...'
2755   (gnus-summary-set-display-table)
2756   (gnus-set-default-directory)
2757   (setq gnus-newsgroup-name group)
2758   (make-local-variable 'gnus-summary-line-format)
2759   (make-local-variable 'gnus-summary-line-format-spec)
2760   (make-local-variable 'gnus-summary-dummy-line-format)
2761   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2762   (make-local-variable 'gnus-summary-mark-positions)
2763   (gnus-make-local-hook 'pre-command-hook)
2764   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2765   (gnus-run-hooks 'gnus-summary-mode-hook)
2766   (turn-on-gnus-mailing-list-mode)
2767   (mm-enable-multibyte)
2768   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2769   (gnus-update-summary-mark-positions))
2770
2771 (defun gnus-summary-make-local-variables ()
2772   "Make all the local summary buffer variables."
2773   (let (global)
2774     (dolist (local gnus-summary-local-variables)
2775       (if (consp local)
2776           (progn
2777             (if (eq (cdr local) 'global)
2778                 ;; Copy the global value of the variable.
2779                 (setq global (symbol-value (car local)))
2780               ;; Use the value from the list.
2781               (setq global (eval (cdr local))))
2782             (set (make-local-variable (car local)) global))
2783         ;; Simple nil-valued local variable.
2784         (set (make-local-variable local) nil)))))
2785
2786 (defun gnus-summary-clear-local-variables ()
2787   (let ((locals gnus-summary-local-variables))
2788     (while locals
2789       (if (consp (car locals))
2790           (and (vectorp (caar locals))
2791                (set (caar locals) nil))
2792         (and (vectorp (car locals))
2793              (set (car locals) nil)))
2794       (setq locals (cdr locals)))))
2795
2796 ;; Summary data functions.
2797
2798 (defmacro gnus-data-number (data)
2799   `(car ,data))
2800
2801 (defmacro gnus-data-set-number (data number)
2802   `(setcar ,data ,number))
2803
2804 (defmacro gnus-data-mark (data)
2805   `(nth 1 ,data))
2806
2807 (defmacro gnus-data-set-mark (data mark)
2808   `(setcar (nthcdr 1 ,data) ,mark))
2809
2810 (defmacro gnus-data-pos (data)
2811   `(nth 2 ,data))
2812
2813 (defmacro gnus-data-set-pos (data pos)
2814   `(setcar (nthcdr 2 ,data) ,pos))
2815
2816 (defmacro gnus-data-header (data)
2817   `(nth 3 ,data))
2818
2819 (defmacro gnus-data-set-header (data header)
2820   `(setf (nth 3 ,data) ,header))
2821
2822 (defmacro gnus-data-level (data)
2823   `(nth 4 ,data))
2824
2825 (defmacro gnus-data-unread-p (data)
2826   `(= (nth 1 ,data) gnus-unread-mark))
2827
2828 (defmacro gnus-data-read-p (data)
2829   `(/= (nth 1 ,data) gnus-unread-mark))
2830
2831 (defmacro gnus-data-pseudo-p (data)
2832   `(consp (nth 3 ,data)))
2833
2834 (defmacro gnus-data-find (number)
2835   `(assq ,number gnus-newsgroup-data))
2836
2837 (defmacro gnus-data-find-list (number &optional data)
2838   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2839      (memq (assq ,number bdata)
2840            bdata)))
2841
2842 (defmacro gnus-data-make (number mark pos header level)
2843   `(list ,number ,mark ,pos ,header ,level))
2844
2845 (defun gnus-data-enter (after-article number mark pos header level offset)
2846   (let ((data (gnus-data-find-list after-article)))
2847     (unless data
2848       (error "No such article: %d" after-article))
2849     (setcdr data (cons (gnus-data-make number mark pos header level)
2850                        (cdr data)))
2851     (setq gnus-newsgroup-data-reverse nil)
2852     (gnus-data-update-list (cddr data) offset)))
2853
2854 (defun gnus-data-enter-list (after-article list &optional offset)
2855   (when list
2856     (let ((data (and after-article (gnus-data-find-list after-article)))
2857           (ilist list))
2858       (if (not (or data
2859                    after-article))
2860           (let ((odata gnus-newsgroup-data))
2861             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2862             (when offset
2863               (gnus-data-update-list odata offset)))
2864       ;; Find the last element in the list to be spliced into the main
2865         ;; list.
2866         (while (cdr list)
2867           (setq list (cdr list)))
2868         (if (not data)
2869             (progn
2870               (setcdr list gnus-newsgroup-data)
2871               (setq gnus-newsgroup-data ilist)
2872               (when offset
2873                 (gnus-data-update-list (cdr list) offset)))
2874           (setcdr list (cdr data))
2875           (setcdr data ilist)
2876           (when offset
2877             (gnus-data-update-list (cdr list) offset))))
2878       (setq gnus-newsgroup-data-reverse nil))))
2879
2880 (defun gnus-data-remove (article &optional offset)
2881   (let ((data gnus-newsgroup-data))
2882     (if (= (gnus-data-number (car data)) article)
2883         (progn
2884           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2885                 gnus-newsgroup-data-reverse nil)
2886           (when offset
2887             (gnus-data-update-list gnus-newsgroup-data offset)))
2888       (while (cdr data)
2889         (when (= (gnus-data-number (cadr data)) article)
2890           (setcdr data (cddr data))
2891           (when offset
2892             (gnus-data-update-list (cdr data) offset))
2893           (setq data nil
2894                 gnus-newsgroup-data-reverse nil))
2895         (setq data (cdr data))))))
2896
2897 (defmacro gnus-data-list (backward)
2898   `(if ,backward
2899        (or gnus-newsgroup-data-reverse
2900            (setq gnus-newsgroup-data-reverse
2901                  (reverse gnus-newsgroup-data)))
2902      gnus-newsgroup-data))
2903
2904 (defun gnus-data-update-list (data offset)
2905   "Add OFFSET to the POS of all data entries in DATA."
2906   (setq gnus-newsgroup-data-reverse nil)
2907   (while data
2908     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2909     (setq data (cdr data))))
2910
2911 (defun gnus-summary-article-pseudo-p (article)
2912   "Say whether this article is a pseudo article or not."
2913   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2914
2915 (defmacro gnus-summary-article-sparse-p (article)
2916   "Say whether this article is a sparse article or not."
2917   `(memq ,article gnus-newsgroup-sparse))
2918
2919 (defmacro gnus-summary-article-ancient-p (article)
2920   "Say whether this article is a sparse article or not."
2921   `(memq ,article gnus-newsgroup-ancient))
2922
2923 (defun gnus-article-parent-p (number)
2924   "Say whether this article is a parent or not."
2925   (let ((data (gnus-data-find-list number)))
2926     (and (cdr data)              ; There has to be an article after...
2927          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2928             (gnus-data-level (nth 1 data))))))
2929
2930 (defun gnus-article-children (number)
2931   "Return a list of all children to NUMBER."
2932   (let* ((data (gnus-data-find-list number))
2933          (level (gnus-data-level (car data)))
2934          children)
2935     (setq data (cdr data))
2936     (while (and data
2937                 (= (gnus-data-level (car data)) (1+ level)))
2938       (push (gnus-data-number (car data)) children)
2939       (setq data (cdr data)))
2940     children))
2941
2942 (defmacro gnus-summary-skip-intangible ()
2943   "If the current article is intangible, then jump to a different article."
2944   '(let ((to (get-text-property (point) 'gnus-intangible)))
2945      (and to (gnus-summary-goto-subject to))))
2946
2947 (defmacro gnus-summary-article-intangible-p ()
2948   "Say whether this article is intangible or not."
2949   '(get-text-property (point) 'gnus-intangible))
2950
2951 (defun gnus-article-read-p (article)
2952   "Say whether ARTICLE is read or not."
2953   (not (or (memq article gnus-newsgroup-marked)
2954            (memq article gnus-newsgroup-spam-marked)
2955            (memq article gnus-newsgroup-unreads)
2956            (memq article gnus-newsgroup-unselected)
2957            (memq article gnus-newsgroup-dormant))))
2958
2959 ;; Some summary mode macros.
2960
2961 (defmacro gnus-summary-article-number ()
2962   "The article number of the article on the current line.
2963 If there isn't an article number here, then we return the current
2964 article number."
2965   '(progn
2966      (gnus-summary-skip-intangible)
2967      (or (get-text-property (point) 'gnus-number)
2968          (gnus-summary-last-subject))))
2969
2970 (defmacro gnus-summary-article-header (&optional number)
2971   "Return the header of article NUMBER."
2972   `(gnus-data-header (gnus-data-find
2973                       ,(or number '(gnus-summary-article-number)))))
2974
2975 (defmacro gnus-summary-thread-level (&optional number)
2976   "Return the level of thread that starts with article NUMBER."
2977   `(if (and (eq gnus-summary-make-false-root 'dummy)
2978             (get-text-property (point) 'gnus-intangible))
2979        0
2980      (gnus-data-level (gnus-data-find
2981                        ,(or number '(gnus-summary-article-number))))))
2982
2983 (defmacro gnus-summary-article-mark (&optional number)
2984   "Return the mark of article NUMBER."
2985   `(gnus-data-mark (gnus-data-find
2986                     ,(or number '(gnus-summary-article-number)))))
2987
2988 (defmacro gnus-summary-article-pos (&optional number)
2989   "Return the position of the line of article NUMBER."
2990   `(gnus-data-pos (gnus-data-find
2991                    ,(or number '(gnus-summary-article-number)))))
2992
2993 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2994 (defmacro gnus-summary-article-subject (&optional number)
2995   "Return current subject string or nil if nothing."
2996   `(let ((headers
2997           ,(if number
2998                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2999              '(gnus-data-header (assq (gnus-summary-article-number)
3000                                       gnus-newsgroup-data)))))
3001      (and headers
3002           (vectorp headers)
3003           (mail-header-subject headers))))
3004
3005 (defmacro gnus-summary-article-score (&optional number)
3006   "Return current article score."
3007   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3008                   gnus-newsgroup-scored))
3009        gnus-summary-default-score 0))
3010
3011 (defun gnus-summary-article-children (&optional number)
3012   "Return a list of article numbers that are children of article NUMBER."
3013   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3014          (level (gnus-data-level (car data)))
3015          l children)
3016     (while (and (setq data (cdr data))
3017                 (> (setq l (gnus-data-level (car data))) level))
3018       (and (= (1+ level) l)
3019            (push (gnus-data-number (car data))
3020                  children)))
3021     (nreverse children)))
3022
3023 (defun gnus-summary-article-parent (&optional number)
3024   "Return the article number of the parent of article NUMBER."
3025   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3026                                     (gnus-data-list t)))
3027          (level (gnus-data-level (car data))))
3028     (if (zerop level)
3029         ()                              ; This is a root.
3030       ;; We search until we find an article with a level less than
3031       ;; this one.  That function has to be the parent.
3032       (while (and (setq data (cdr data))
3033                   (not (< (gnus-data-level (car data)) level))))
3034       (and data (gnus-data-number (car data))))))
3035
3036 (defun gnus-unread-mark-p (mark)
3037   "Say whether MARK is the unread mark."
3038   (= mark gnus-unread-mark))
3039
3040 (defun gnus-read-mark-p (mark)
3041   "Say whether MARK is one of the marks that mark as read.
3042 This is all marks except unread, ticked, dormant, and expirable."
3043   (not (or (= mark gnus-unread-mark)
3044            (= mark gnus-ticked-mark)
3045            (= mark gnus-spam-mark)
3046            (= mark gnus-dormant-mark)
3047            (= mark gnus-expirable-mark))))
3048
3049 (defmacro gnus-article-mark (number)
3050   "Return the MARK of article NUMBER.
3051 This macro should only be used when computing the mark the \"first\"
3052 time; i.e., when generating the summary lines.  After that,
3053 `gnus-summary-article-mark' should be used to examine the
3054 marks of articles."
3055   `(cond
3056     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3057     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3058     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3059     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3060     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3061     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3062     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3063     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3064            gnus-ancient-mark))))
3065
3066 ;; Saving hidden threads.
3067
3068 (defmacro gnus-save-hidden-threads (&rest forms)
3069   "Save hidden threads, eval FORMS, and restore the hidden threads."
3070   (let ((config (make-symbol "config")))
3071     `(let ((,config (gnus-hidden-threads-configuration)))
3072        (unwind-protect
3073            (save-excursion
3074              ,@forms)
3075          (gnus-restore-hidden-threads-configuration ,config)))))
3076 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3077 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3078
3079 (defun gnus-data-compute-positions ()
3080   "Compute the positions of all articles."
3081   (setq gnus-newsgroup-data-reverse nil)
3082   (let ((data gnus-newsgroup-data))
3083     (save-excursion
3084       (gnus-save-hidden-threads
3085         (gnus-summary-show-all-threads)
3086         (goto-char (point-min))
3087         (while data
3088           (while (get-text-property (point) 'gnus-intangible)
3089             (forward-line 1))
3090           (gnus-data-set-pos (car data) (+ (point) 3))
3091           (setq data (cdr data))
3092           (forward-line 1))))))
3093
3094 (defun gnus-hidden-threads-configuration ()
3095   "Return the current hidden threads configuration."
3096   (save-excursion
3097     (let (config)
3098       (goto-char (point-min))
3099       (while (search-forward "\r" nil t)
3100         (push (1- (point)) config))
3101       config)))
3102
3103 (defun gnus-restore-hidden-threads-configuration (config)
3104   "Restore hidden threads configuration from CONFIG."
3105   (save-excursion
3106     (let (point buffer-read-only)
3107       (while (setq point (pop config))
3108         (when (and (< point (point-max))
3109                    (goto-char point)
3110                    (eq (char-after) ?\n))
3111           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3112
3113 ;; Various summary mode internalish functions.
3114
3115 (defun gnus-mouse-pick-article (e)
3116   (interactive "e")
3117   (mouse-set-point e)
3118   (gnus-summary-next-page nil t))
3119
3120 (defun gnus-summary-set-display-table ()
3121   "Change the display table.
3122 Odd characters have a tendency to mess
3123 up nicely formatted displays - we make all possible glyphs
3124 display only a single character."
3125
3126   ;; We start from the standard display table, if any.
3127   (let ((table (or (copy-sequence standard-display-table)
3128                    (make-display-table)))
3129         (i 32))
3130     ;; Nix out all the control chars...
3131     (while (>= (setq i (1- i)) 0)
3132       (aset table i [??]))
3133    ;; ... but not newline and cr, of course.  (cr is necessary for the
3134     ;; selective display).
3135     (aset table ?\n nil)
3136     (aset table ?\r nil)
3137     ;; We keep TAB as well.
3138     (aset table ?\t nil)
3139     ;; We nix out any glyphs over 126 that are not set already.
3140     (let ((i 256))
3141       (while (>= (setq i (1- i)) 127)
3142         ;; Only modify if the entry is nil.
3143         (unless (aref table i)
3144           (aset table i [??]))))
3145     (setq buffer-display-table table)))
3146
3147 (defun gnus-summary-set-article-display-arrow (pos)
3148   "Update the overlay arrow to point to line at position POS."
3149   (when (and gnus-summary-display-arrow
3150              (boundp 'overlay-arrow-position)
3151              (boundp 'overlay-arrow-string))
3152     (save-excursion
3153       (goto-char pos)
3154       (beginning-of-line)
3155       (unless overlay-arrow-position
3156         (setq overlay-arrow-position (make-marker)))
3157       (setq overlay-arrow-string "=>"
3158             overlay-arrow-position (set-marker overlay-arrow-position
3159                                                (point)
3160                                                (current-buffer))))))
3161
3162 (defun gnus-summary-setup-buffer (group)
3163   "Initialize summary buffer."
3164   (let ((buffer (gnus-summary-buffer-name group))
3165         (dead-name (concat "*Dead Summary "
3166                            (gnus-group-decoded-name group) "*")))
3167     ;; If a dead summary buffer exists, we kill it.
3168     (when (gnus-buffer-live-p dead-name)
3169       (gnus-kill-buffer dead-name))
3170     (if (get-buffer buffer)
3171         (progn
3172           (set-buffer buffer)
3173           (setq gnus-summary-buffer (current-buffer))
3174           (not gnus-newsgroup-prepared))
3175       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3176       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3177       (gnus-summary-mode group)
3178       (when gnus-carpal
3179         (gnus-carpal-setup-buffer 'summary))
3180       (unless gnus-single-article-buffer
3181         (make-local-variable 'gnus-article-buffer)
3182         (make-local-variable 'gnus-article-current)
3183         (make-local-variable 'gnus-original-article-buffer))
3184       (setq gnus-newsgroup-name group)
3185       ;; Set any local variables in the group parameters.
3186       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3187       t)))
3188
3189 (defun gnus-set-global-variables ()
3190   "Set the global equivalents of the buffer-local variables.
3191 They are set to the latest values they had.  These reflect the summary
3192 buffer that was in action when the last article was fetched."
3193   (when (eq major-mode 'gnus-summary-mode)
3194     (setq gnus-summary-buffer (current-buffer))
3195     (let ((name gnus-newsgroup-name)
3196           (marked gnus-newsgroup-marked)
3197           (spam gnus-newsgroup-spam-marked)
3198           (unread gnus-newsgroup-unreads)
3199           (headers gnus-current-headers)
3200           (data gnus-newsgroup-data)
3201           (summary gnus-summary-buffer)
3202           (article-buffer gnus-article-buffer)
3203           (original gnus-original-article-buffer)
3204           (gac gnus-article-current)
3205           (reffed gnus-reffed-article-number)
3206           (score-file gnus-current-score-file)
3207           (default-charset gnus-newsgroup-charset)
3208           vlist)
3209       (let ((locals gnus-newsgroup-variables))
3210         (while locals
3211           (if (consp (car locals))
3212               (push (eval (caar locals)) vlist)
3213             (push (eval (car locals)) vlist))
3214           (setq locals (cdr locals)))
3215         (setq vlist (nreverse vlist)))
3216       (with-current-buffer gnus-group-buffer
3217         (setq gnus-newsgroup-name name
3218               gnus-newsgroup-marked marked
3219               gnus-newsgroup-spam-marked spam
3220               gnus-newsgroup-unreads unread
3221               gnus-current-headers headers
3222               gnus-newsgroup-data data
3223               gnus-article-current gac
3224               gnus-summary-buffer summary
3225               gnus-article-buffer article-buffer
3226               gnus-original-article-buffer original
3227               gnus-reffed-article-number reffed
3228               gnus-current-score-file score-file
3229               gnus-newsgroup-charset default-charset)
3230         (let ((locals gnus-newsgroup-variables))
3231           (while locals
3232             (if (consp (car locals))
3233                 (set (caar locals) (pop vlist))
3234               (set (car locals) (pop vlist)))
3235             (setq locals (cdr locals))))
3236         ;; The article buffer also has local variables.
3237         (when (gnus-buffer-live-p gnus-article-buffer)
3238           (set-buffer gnus-article-buffer)
3239           (setq gnus-summary-buffer summary))))))
3240
3241 (defun gnus-summary-article-unread-p (article)
3242   "Say whether ARTICLE is unread or not."
3243   (memq article gnus-newsgroup-unreads))
3244
3245 (defun gnus-summary-first-article-p (&optional article)
3246   "Return whether ARTICLE is the first article in the buffer."
3247   (if (not (setq article (or article (gnus-summary-article-number))))
3248       nil
3249     (eq article (caar gnus-newsgroup-data))))
3250
3251 (defun gnus-summary-last-article-p (&optional article)
3252   "Return whether ARTICLE is the last article in the buffer."
3253   (if (not (setq article (or article (gnus-summary-article-number))))
3254       ;; All non-existent numbers are the last article.  :-)
3255       t
3256     (not (cdr (gnus-data-find-list article)))))
3257
3258 (defun gnus-make-thread-indent-array ()
3259   (let ((n 200))
3260     (unless (and gnus-thread-indent-array
3261                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3262       (setq gnus-thread-indent-array (make-vector 201 "")
3263             gnus-thread-indent-array-level gnus-thread-indent-level)
3264       (while (>= n 0)
3265         (aset gnus-thread-indent-array n
3266               (make-string (* n gnus-thread-indent-level) ? ))
3267         (setq n (1- n))))))
3268
3269 (defun gnus-update-summary-mark-positions ()
3270   "Compute where the summary marks are to go."
3271   (save-excursion
3272     (when (gnus-buffer-exists-p gnus-summary-buffer)
3273       (set-buffer gnus-summary-buffer))
3274     (let ((gnus-replied-mark 129)
3275           (gnus-score-below-mark 130)
3276           (gnus-score-over-mark 130)
3277           (gnus-undownloaded-mark 131)
3278           (spec gnus-summary-line-format-spec)
3279           gnus-visual pos)
3280       (save-excursion
3281         (gnus-set-work-buffer)
3282         (let ((gnus-summary-line-format-spec spec)
3283               (gnus-newsgroup-downloadable '(0)))
3284           (gnus-summary-insert-line
3285            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3286            0 nil t 128 t nil "" nil 1)
3287           (goto-char (point-min))
3288           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3289                                              (- (point) (point-min) 1)))))
3290           (goto-char (point-min))
3291           (push (cons 'replied (and (search-forward "\201" nil t)
3292                                     (- (point) (point-min) 1)))
3293                 pos)
3294           (goto-char (point-min))
3295           (push (cons 'score (and (search-forward "\202" nil t)
3296                                   (- (point) (point-min) 1)))
3297                 pos)
3298           (goto-char (point-min))
3299           (push (cons 'download
3300                       (and (search-forward "\203" nil t)
3301                            (- (point) (point-min) 1)))
3302                 pos)))
3303       (setq gnus-summary-mark-positions pos))))
3304
3305 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3306   "Insert a dummy root in the summary buffer."
3307   (beginning-of-line)
3308   (gnus-add-text-properties
3309    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3310    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3311
3312 (defun gnus-summary-extract-address-component (from)
3313   (or (car (funcall gnus-extract-address-components from))
3314       from))
3315
3316 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3317   (let ((mail-parse-charset gnus-newsgroup-charset)
3318         ; Is it really necessary to do this next part for each summary line?
3319         ; Luckily, doesn't seem to slow things down much.
3320         (mail-parse-ignored-charsets
3321          (with-current-buffer gnus-summary-buffer
3322            gnus-newsgroup-ignored-charsets)))
3323     (or
3324      (and gnus-ignored-from-addresses
3325           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3326           (let ((extra-headers (mail-header-extra header))
3327                 to
3328                 newsgroups)
3329             (cond
3330              ((setq to (cdr (assq 'To extra-headers)))
3331               (concat "-> "
3332                       (inline
3333                         (gnus-summary-extract-address-component
3334                          (funcall gnus-decode-encoded-word-function to)))))
3335              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3336               (concat "=> " newsgroups)))))
3337      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3338
3339 (defun gnus-summary-insert-line (gnus-tmp-header
3340                                  gnus-tmp-level gnus-tmp-current
3341                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3342                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3343                                  &optional gnus-tmp-dummy gnus-tmp-score
3344                                  gnus-tmp-process)
3345   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3346          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3347          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3348          (gnus-tmp-score-char
3349           (if (or (null gnus-summary-default-score)
3350                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3351                       gnus-summary-zcore-fuzz))
3352               ?                         ;Whitespace
3353             (if (< gnus-tmp-score gnus-summary-default-score)
3354                 gnus-score-below-mark gnus-score-over-mark)))
3355          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3356          (gnus-tmp-replied
3357           (cond (gnus-tmp-process gnus-process-mark)
3358                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3359                  gnus-cached-mark)
3360                 (gnus-tmp-replied gnus-replied-mark)
3361                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3362                  gnus-forwarded-mark)
3363                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3364                  gnus-saved-mark)
3365                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3366                  gnus-recent-mark)
3367                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3368                  gnus-unseen-mark)
3369                 (t gnus-no-mark)))
3370          (gnus-tmp-downloaded
3371           (cond (undownloaded
3372                  gnus-undownloaded-mark)
3373                 (gnus-newsgroup-agentized
3374                  gnus-downloaded-mark)
3375                 (t
3376                  gnus-no-mark)))
3377          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3378          (gnus-tmp-name
3379           (cond
3380            ((string-match "<[^>]+> *$" gnus-tmp-from)
3381             (let ((beg (match-beginning 0)))
3382               (or (and (string-match "^\".+\"" gnus-tmp-from)
3383                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3384                   (substring gnus-tmp-from 0 beg))))
3385            ((string-match "(.+)" gnus-tmp-from)
3386             (substring gnus-tmp-from
3387                        (1+ (match-beginning 0)) (1- (match-end 0))))
3388            (t gnus-tmp-from)))
3389          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3390          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3391          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3392          (buffer-read-only nil))
3393     (when (string= gnus-tmp-name "")
3394       (setq gnus-tmp-name gnus-tmp-from))
3395     (unless (numberp gnus-tmp-lines)
3396       (setq gnus-tmp-lines -1))
3397     (if (= gnus-tmp-lines -1)
3398         (setq gnus-tmp-lines "?")
3399       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3400       (gnus-put-text-property
3401      (point)
3402      (progn (eval gnus-summary-line-format-spec) (point))
3403        'gnus-number gnus-tmp-number)
3404     (when (gnus-visual-p 'summary-highlight 'highlight)
3405       (forward-line -1)
3406       (gnus-run-hooks 'gnus-summary-update-hook)
3407       (forward-line 1))))
3408
3409 (defun gnus-summary-update-line (&optional dont-update)
3410   "Update summary line after change."
3411   (when (and gnus-summary-default-score
3412              (not gnus-summary-inhibit-highlight))
3413     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3414            (article (gnus-summary-article-number))
3415            (score (gnus-summary-article-score article)))
3416       (unless dont-update
3417         (if (and gnus-summary-mark-below
3418                  (< (gnus-summary-article-score)
3419                     gnus-summary-mark-below))
3420             ;; This article has a low score, so we mark it as read.
3421             (when (memq article gnus-newsgroup-unreads)
3422               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3423           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3424             ;; This article was previously marked as read on account
3425             ;; of a low score, but now it has risen, so we mark it as
3426             ;; unread.
3427             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3428         (gnus-summary-update-mark
3429          (if (or (null gnus-summary-default-score)
3430                  (<= (abs (- score gnus-summary-default-score))
3431                      gnus-summary-zcore-fuzz))
3432              ?                          ;Whitespace
3433            (if (< score gnus-summary-default-score)
3434                gnus-score-below-mark gnus-score-over-mark))
3435          'score))
3436       ;; Do visual highlighting.
3437       (when (gnus-visual-p 'summary-highlight 'highlight)
3438         (gnus-run-hooks 'gnus-summary-update-hook)))))
3439
3440 (defvar gnus-tmp-new-adopts nil)
3441
3442 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3443   "Return the number of articles in THREAD.
3444 This may be 0 in some cases -- if none of the articles in
3445 the thread are to be displayed."
3446   (let* ((number
3447          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3448           (cond
3449            ((not (listp thread))
3450             1)
3451            ((and (consp thread) (cdr thread))
3452             (apply
3453              '+ 1 (mapcar
3454                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3455            ((null thread)
3456             1)
3457            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3458             1)
3459            (t 0))))
3460     (when (and level (zerop level) gnus-tmp-new-adopts)
3461       (incf number
3462             (apply '+ (mapcar
3463                        'gnus-summary-number-of-articles-in-thread
3464                        gnus-tmp-new-adopts))))
3465     (if char
3466         (if (> number 1) gnus-not-empty-thread-mark
3467           gnus-empty-thread-mark)
3468       number)))
3469
3470 (defsubst gnus-summary-line-message-size (head)
3471   "Return pretty-printed version of message size.
3472 This function is intended to be used in
3473 `gnus-summary-line-format-alist'."
3474   (let ((c (or (mail-header-chars head) -1)))
3475     (cond ((< c 0) "n/a")               ; chars not available
3476           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3477           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3478           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3479           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3480
3481
3482 (defun gnus-summary-set-local-parameters (group)
3483   "Go through the local params of GROUP and set all variable specs in that list."
3484   (let ((params (gnus-group-find-parameter group))
3485         (vars '(quit-config))           ; Ignore quit-config.
3486         elem)
3487     (while params
3488       (setq elem (car params)
3489             params (cdr params))
3490       (and (consp elem)                 ; Has to be a cons.
3491            (consp (cdr elem))           ; The cdr has to be a list.
3492            (symbolp (car elem))         ; Has to be a symbol in there.
3493            (not (memq (car elem) vars))
3494            (ignore-errors               ; So we set it.
3495              (push (car elem) vars)
3496              (make-local-variable (car elem))
3497              (set (car elem) (eval (nth 1 elem))))))))
3498
3499 (defun gnus-summary-read-group (group &optional show-all no-article
3500                                       kill-buffer no-display backward
3501                                       select-articles)
3502   "Start reading news in newsgroup GROUP.
3503 If SHOW-ALL is non-nil, already read articles are also listed.
3504 If NO-ARTICLE is non-nil, no article is selected initially.
3505 If NO-DISPLAY, don't generate a summary buffer."
3506   (let (result)
3507     (while (and group
3508                 (null (setq result
3509                             (let ((gnus-auto-select-next nil))
3510                               (or (gnus-summary-read-group-1
3511                                    group show-all no-article
3512                                    kill-buffer no-display
3513                                    select-articles)
3514                                   (setq show-all nil
3515                                         select-articles nil)))))
3516                 (eq gnus-auto-select-next 'quietly))
3517       (set-buffer gnus-group-buffer)
3518       ;; The entry function called above goes to the next
3519       ;; group automatically, so we go two groups back
3520       ;; if we are searching for the previous group.
3521       (when backward
3522         (gnus-group-prev-unread-group 2))
3523       (if (not (equal group (gnus-group-group-name)))
3524           (setq group (gnus-group-group-name))
3525         (setq group nil)))
3526     result))
3527
3528 (defun gnus-summary-read-group-1 (group show-all no-article
3529                                         kill-buffer no-display
3530                                         &optional select-articles)
3531   ;; Killed foreign groups can't be entered.
3532   ;;  (when (and (not (gnus-group-native-p group))
3533   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3534   ;;    (error "Dead non-native groups can't be entered"))
3535   (gnus-message 5 "Retrieving newsgroup: %s..."
3536                 (gnus-group-decoded-name group))
3537   (let* ((new-group (gnus-summary-setup-buffer group))
3538          (quit-config (gnus-group-quit-config group))
3539          (did-select (and new-group (gnus-select-newsgroup
3540                                      group show-all select-articles))))
3541     (cond
3542      ;; This summary buffer exists already, so we just select it.
3543      ((not new-group)
3544       (gnus-set-global-variables)
3545       (when kill-buffer
3546         (gnus-kill-or-deaden-summary kill-buffer))
3547       (gnus-configure-windows 'summary 'force)
3548       (gnus-set-mode-line 'summary)
3549       (gnus-summary-position-point)
3550       (message "")
3551       t)
3552      ;; We couldn't select this group.
3553      ((null did-select)
3554       (when (and (eq major-mode 'gnus-summary-mode)
3555                  (not (equal (current-buffer) kill-buffer)))
3556         (kill-buffer (current-buffer))
3557         (if (not quit-config)
3558             (progn
3559               ;; Update the info -- marks might need to be removed,
3560               ;; for instance.
3561               (gnus-summary-update-info)
3562               (set-buffer gnus-group-buffer)
3563               (gnus-group-jump-to-group group)
3564               (gnus-group-next-unread-group 1))
3565           (gnus-handle-ephemeral-exit quit-config)))
3566       (let ((grpinfo (gnus-get-info group)))
3567         (if (null (gnus-info-read grpinfo))
3568             (gnus-message 3 "Group %s contains no messages"
3569                           (gnus-group-decoded-name group))
3570           (gnus-message 3 "Can't select group")))
3571       nil)
3572      ;; The user did a `C-g' while prompting for number of articles,
3573      ;; so we exit this group.
3574      ((eq did-select 'quit)
3575       (and (eq major-mode 'gnus-summary-mode)
3576            (not (equal (current-buffer) kill-buffer))
3577            (kill-buffer (current-buffer)))
3578       (when kill-buffer
3579         (gnus-kill-or-deaden-summary kill-buffer))
3580       (if (not quit-config)
3581           (progn
3582             (set-buffer gnus-group-buffer)
3583             (gnus-group-jump-to-group group)
3584             (gnus-group-next-unread-group 1)
3585             (gnus-configure-windows 'group 'force))
3586         (gnus-handle-ephemeral-exit quit-config))
3587       ;; Finally signal the quit.
3588       (signal 'quit nil))
3589      ;; The group was successfully selected.
3590      (t
3591       (gnus-set-global-variables)
3592       ;; Save the active value in effect when the group was entered.
3593       (setq gnus-newsgroup-active
3594             (gnus-copy-sequence
3595              (gnus-active gnus-newsgroup-name)))
3596       ;; You can change the summary buffer in some way with this hook.
3597       (gnus-run-hooks 'gnus-select-group-hook)
3598       (gnus-update-format-specifications
3599        nil 'summary 'summary-mode 'summary-dummy)
3600       (gnus-update-summary-mark-positions)
3601       ;; Do score processing.
3602       (when gnus-use-scoring
3603         (gnus-possibly-score-headers))
3604       ;; Check whether to fill in the gaps in the threads.
3605       (when gnus-build-sparse-threads
3606         (gnus-build-sparse-threads))
3607       ;; Find the initial limit.
3608       (if show-all
3609           (let ((gnus-newsgroup-dormant nil))
3610             (gnus-summary-initial-limit show-all))
3611         (gnus-summary-initial-limit show-all))
3612       ;; Generate the summary buffer.
3613       (unless no-display
3614         (gnus-summary-prepare))
3615       (when gnus-use-trees
3616         (gnus-tree-open group)
3617         (setq gnus-summary-highlight-line-function
3618               'gnus-tree-highlight-article))
3619       ;; If the summary buffer is empty, but there are some low-scored
3620       ;; articles or some excluded dormants, we include these in the
3621       ;; buffer.
3622       (when (and (zerop (buffer-size))
3623                  (not no-display))
3624         (cond (gnus-newsgroup-dormant
3625                (gnus-summary-limit-include-dormant))
3626               ((and gnus-newsgroup-scored show-all)
3627                (gnus-summary-limit-include-expunged t))))
3628       ;; Function `gnus-apply-kill-file' must be called in this hook.
3629       (gnus-run-hooks 'gnus-apply-kill-hook)
3630       (if (and (zerop (buffer-size))
3631                (not no-display))
3632           (progn
3633             ;; This newsgroup is empty.
3634             (gnus-summary-catchup-and-exit nil t)
3635             (gnus-message 6 "No unread news")
3636             (when kill-buffer
3637               (gnus-kill-or-deaden-summary kill-buffer))
3638             ;; Return nil from this function.
3639             nil)
3640         ;; Hide conversation thread subtrees.  We cannot do this in
3641         ;; gnus-summary-prepare-hook since kill processing may not
3642         ;; work with hidden articles.
3643         (gnus-summary-maybe-hide-threads)
3644         (when kill-buffer
3645           (gnus-kill-or-deaden-summary kill-buffer))
3646         (gnus-summary-auto-select-subject)
3647         ;; Show first unread article if requested.
3648         (if (and (not no-article)
3649                  (not no-display)
3650                  gnus-newsgroup-unreads
3651                  gnus-auto-select-first)
3652             (progn
3653               (gnus-configure-windows 'summary)
3654               (let ((art (gnus-summary-article-number)))
3655                 (unless (and (not gnus-plugged)
3656                              (or (memq art gnus-newsgroup-undownloaded)
3657                                  (memq art gnus-newsgroup-downloadable)))
3658                   (gnus-summary-goto-article art))))
3659           ;; Don't select any articles.
3660           (gnus-summary-position-point)
3661           (gnus-configure-windows 'summary 'force)
3662           (gnus-set-mode-line 'summary))
3663         (when (and gnus-auto-center-group
3664                    (get-buffer-window gnus-group-buffer t))
3665           ;; Gotta use windows, because recenter does weird stuff if
3666           ;; the current buffer ain't the displayed window.
3667           (let ((owin (selected-window)))
3668             (select-window (get-buffer-window gnus-group-buffer t))
3669             (when (gnus-group-goto-group group)
3670               (recenter))
3671             (select-window owin)))
3672         ;; Mark this buffer as "prepared".
3673         (setq gnus-newsgroup-prepared t)
3674         (gnus-run-hooks 'gnus-summary-prepared-hook)
3675         (unless (gnus-ephemeral-group-p group)
3676           (gnus-group-update-group group))
3677         t)))))
3678
3679 (defun gnus-summary-auto-select-subject ()
3680   "Select the subject line on initial group entry."
3681   (goto-char (point-min))
3682   (cond
3683    ((eq gnus-auto-select-subject 'best)
3684     (gnus-summary-best-unread-subject))
3685    ((eq gnus-auto-select-subject 'unread)
3686     (gnus-summary-first-unread-subject))
3687    ((eq gnus-auto-select-subject 'unseen)
3688     (gnus-summary-first-unseen-subject))
3689    ((eq gnus-auto-select-subject 'unseen-or-unread)
3690     (gnus-summary-first-unseen-or-unread-subject))
3691    ((eq gnus-auto-select-subject 'first)
3692     ;; Do nothing.
3693     )
3694    ((functionp gnus-auto-select-subject)
3695     (funcall gnus-auto-select-subject))))
3696
3697 (defun gnus-summary-prepare ()
3698   "Generate the summary buffer."
3699   (interactive)
3700   (let ((buffer-read-only nil))
3701     (erase-buffer)
3702     (setq gnus-newsgroup-data nil
3703           gnus-newsgroup-data-reverse nil)
3704     (gnus-run-hooks 'gnus-summary-generate-hook)
3705     ;; Generate the buffer, either with threads or without.
3706     (when gnus-newsgroup-headers
3707       (gnus-summary-prepare-threads
3708        (if gnus-show-threads
3709            (gnus-sort-gathered-threads
3710             (funcall gnus-summary-thread-gathering-function
3711                      (gnus-sort-threads
3712                       (gnus-cut-threads (gnus-make-threads)))))
3713          ;; Unthreaded display.
3714          (gnus-sort-articles gnus-newsgroup-headers))))
3715     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3716     ;; Call hooks for modifying summary buffer.
3717     (goto-char (point-min))
3718     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3719
3720 (defsubst gnus-general-simplify-subject (subject)
3721   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3722   (setq subject
3723         (cond
3724          ;; Truncate the subject.
3725          (gnus-simplify-subject-functions
3726           (gnus-map-function gnus-simplify-subject-functions subject))
3727          ((numberp gnus-summary-gather-subject-limit)
3728           (setq subject (gnus-simplify-subject-re subject))
3729           (if (> (length subject) gnus-summary-gather-subject-limit)
3730               (substring subject 0 gnus-summary-gather-subject-limit)
3731             subject))
3732          ;; Fuzzily simplify it.
3733          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3734           (gnus-simplify-subject-fuzzy subject))
3735          ;; Just remove the leading "Re:".
3736          (t
3737           (gnus-simplify-subject-re subject))))
3738
3739   (if (and gnus-summary-gather-exclude-subject
3740            (string-match gnus-summary-gather-exclude-subject subject))
3741       nil                         ; This article shouldn't be gathered
3742     subject))
3743
3744 (defun gnus-summary-simplify-subject-query ()
3745   "Query where the respool algorithm would put this article."
3746   (interactive)
3747   (gnus-summary-select-article)
3748   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3749
3750 (defun gnus-gather-threads-by-subject (threads)
3751   "Gather threads by looking at Subject headers."
3752   (if (not gnus-summary-make-false-root)
3753       threads
3754     (let ((hashtb (gnus-make-hashtable 1024))
3755           (prev threads)
3756           (result threads)
3757           subject hthread whole-subject)
3758       (while threads
3759         (setq subject (gnus-general-simplify-subject
3760                        (setq whole-subject (mail-header-subject
3761                                             (caar threads)))))
3762         (when subject
3763           (if (setq hthread (gnus-gethash subject hashtb))
3764               (progn
3765                 ;; We enter a dummy root into the thread, if we
3766                 ;; haven't done that already.
3767                 (unless (stringp (caar hthread))
3768                   (setcar hthread (list whole-subject (car hthread))))
3769                 ;; We add this new gathered thread to this gathered
3770                 ;; thread.
3771                 (setcdr (car hthread)
3772                         (nconc (cdar hthread) (list (car threads))))
3773                 ;; Remove it from the list of threads.
3774                 (setcdr prev (cdr threads))
3775                 (setq threads prev))
3776             ;; Enter this thread into the hash table.
3777             (gnus-sethash subject
3778                           (if gnus-summary-make-false-root-always
3779                               (progn
3780                                 ;; If you want a dummy root above all
3781                                 ;; threads...
3782                                 (setcar threads (list whole-subject
3783                                                       (car threads)))
3784                                 threads)
3785                             threads)
3786                           hashtb)))
3787         (setq prev threads)
3788         (setq threads (cdr threads)))
3789       result)))
3790
3791 (defun gnus-gather-threads-by-references (threads)
3792   "Gather threads by looking at References headers."
3793   (let ((idhashtb (gnus-make-hashtable 1024))
3794         (thhashtb (gnus-make-hashtable 1024))
3795         (prev threads)
3796         (result threads)
3797         ids references id gthread gid entered ref)
3798     (while threads
3799       (when (setq references (mail-header-references (caar threads)))
3800         (setq id (mail-header-id (caar threads))
3801               ids (inline (gnus-split-references references))
3802               entered nil)
3803         (while (setq ref (pop ids))
3804           (setq ids (delete ref ids))
3805           (if (not (setq gid (gnus-gethash ref idhashtb)))
3806               (progn
3807                 (gnus-sethash ref id idhashtb)
3808                 (gnus-sethash id threads thhashtb))
3809             (setq gthread (gnus-gethash gid thhashtb))
3810             (unless entered
3811               ;; We enter a dummy root into the thread, if we
3812               ;; haven't done that already.
3813               (unless (stringp (caar gthread))
3814                 (setcar gthread (list (mail-header-subject (caar gthread))
3815                                       (car gthread))))
3816               ;; We add this new gathered thread to this gathered
3817               ;; thread.
3818               (setcdr (car gthread)
3819                       (nconc (cdar gthread) (list (car threads)))))
3820             ;; Add it into the thread hash table.
3821             (gnus-sethash id gthread thhashtb)
3822             (setq entered t)
3823             ;; Remove it from the list of threads.
3824             (setcdr prev (cdr threads))
3825             (setq threads prev))))
3826       (setq prev threads)
3827       (setq threads (cdr threads)))
3828     result))
3829
3830 (defun gnus-sort-gathered-threads (threads)
3831   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3832   (let ((result threads))
3833     (while threads
3834       (when (stringp (caar threads))
3835         (setcdr (car threads)
3836                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3837       (setq threads (cdr threads)))
3838     result))
3839
3840 (defun gnus-thread-loop-p (root thread)
3841   "Say whether ROOT is in THREAD."
3842   (let ((stack (list thread))
3843         (infloop 0)
3844         th)
3845     (while (setq thread (pop stack))
3846       (setq th (cdr thread))
3847       (while (and th
3848                   (not (eq (caar th) root)))
3849         (pop th))
3850       (if th
3851           ;; We have found a loop.
3852           (let (ref-dep)
3853             (setcdr thread (delq (car th) (cdr thread)))
3854             (if (boundp (setq ref-dep (intern "none"
3855                                               gnus-newsgroup-dependencies)))
3856                 (setcdr (symbol-value ref-dep)
3857                         (nconc (cdr (symbol-value ref-dep))
3858                                (list (car th))))
3859               (set ref-dep (list nil (car th))))
3860             (setq infloop 1
3861                   stack nil))
3862         ;; Push all the subthreads onto the stack.
3863         (push (cdr thread) stack)))
3864     infloop))
3865
3866 (defun gnus-make-threads ()
3867   "Go through the dependency hashtb and find the roots.  Return all threads."
3868   (let (threads)
3869     (while (catch 'infloop
3870              (mapatoms
3871               (lambda (refs)
3872                 ;; Deal with self-referencing References loops.
3873                 (when (and (car (symbol-value refs))
3874                            (not (zerop
3875                                  (apply
3876                                   '+
3877                                   (mapcar
3878                                    (lambda (thread)
3879                                      (gnus-thread-loop-p
3880                                       (car (symbol-value refs)) thread))
3881                                    (cdr (symbol-value refs)))))))
3882                   (setq threads nil)
3883                   (throw 'infloop t))
3884                 (unless (car (symbol-value refs))
3885                   ;; These threads do not refer back to any other
3886                   ;; articles, so they're roots.
3887                   (setq threads (append (cdr (symbol-value refs)) threads))))
3888               gnus-newsgroup-dependencies)))
3889     threads))
3890
3891 ;; Build the thread tree.
3892 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3893   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3894
3895 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3896 if it was already present.
3897
3898 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3899 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3900 Message-IDs will be renamed to a unique Message-ID before being
3901 entered.
3902
3903 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3904   (let* ((id (mail-header-id header))
3905          (id-dep (and id (intern id dependencies)))
3906          parent-id ref ref-dep ref-header replaced)
3907     ;; Enter this `header' in the `dependencies' table.
3908     (cond
3909      ((not id-dep)
3910       (setq header nil))
3911      ;; The first two cases do the normal part: enter a new `header'
3912      ;; in the `dependencies' table.
3913      ((not (boundp id-dep))
3914       (set id-dep (list header)))
3915      ((null (car (symbol-value id-dep)))
3916       (setcar (symbol-value id-dep) header))
3917
3918      ;; From here the `header' was already present in the
3919      ;; `dependencies' table.
3920      (force-new
3921       ;; Overrides an existing entry;
3922       ;; just set the header part of the entry.
3923       (setcar (symbol-value id-dep) header)
3924       (setq replaced t))
3925
3926      ;; Renames the existing `header' to a unique Message-ID.
3927      ((not gnus-summary-ignore-duplicates)
3928       ;; An article with this Message-ID has already been seen.
3929       ;; We rename the Message-ID.
3930       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3931            (list header))
3932       (mail-header-set-id header id))
3933
3934      ;; The last case ignores an existing entry, except it adds any
3935      ;; additional Xrefs (in case the two articles came from different
3936      ;; servers.
3937      ;; Also sets `header' to `nil' meaning that the `dependencies'
3938      ;; table was *not* modified.
3939      (t
3940       (mail-header-set-xref
3941        (car (symbol-value id-dep))
3942        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3943                    "")
3944                (or (mail-header-xref header) "")))
3945       (setq header nil)))
3946
3947     (when (and header (not replaced))
3948       ;; First check that we are not creating a References loop.
3949       (setq parent-id (gnus-parent-id (mail-header-references header)))
3950       (setq ref parent-id)
3951       (while (and ref
3952                   (setq ref-dep (intern-soft ref dependencies))
3953                   (boundp ref-dep)
3954                   (setq ref-header (car (symbol-value ref-dep))))
3955         (if (string= id ref)
3956             ;; Yuk!  This is a reference loop.  Make the article be a
3957             ;; root article.
3958             (progn
3959               (mail-header-set-references (car (symbol-value id-dep)) "none")
3960               (setq ref nil)
3961               (setq parent-id nil))
3962           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3963       (setq ref-dep (intern (or parent-id "none") dependencies))
3964       (if (boundp ref-dep)
3965           (setcdr (symbol-value ref-dep)
3966                   (nconc (cdr (symbol-value ref-dep))
3967                          (list (symbol-value id-dep))))
3968         (set ref-dep (list nil (symbol-value id-dep)))))
3969     header))
3970
3971 (defun gnus-extract-message-id-from-in-reply-to (string)
3972   (if (string-match "<[^>]+>" string)
3973       (substring string (match-beginning 0) (match-end 0))
3974     nil))
3975
3976 (defun gnus-build-sparse-threads ()
3977   (let ((headers gnus-newsgroup-headers)
3978         (mail-parse-charset gnus-newsgroup-charset)
3979         (gnus-summary-ignore-duplicates t)
3980         header references generation relations
3981         subject child end new-child date)
3982     ;; First we create an alist of generations/relations, where
3983     ;; generations is how much we trust the relation, and the relation
3984     ;; is parent/child.
3985     (gnus-message 7 "Making sparse threads...")
3986     (save-excursion
3987       (nnheader-set-temp-buffer " *gnus sparse threads*")
3988       (while (setq header (pop headers))
3989         (when (and (setq references (mail-header-references header))
3990                    (not (string= references "")))
3991           (insert references)
3992           (setq child (mail-header-id header)
3993                 subject (mail-header-subject header)
3994                 date (mail-header-date header)
3995                 generation 0)
3996           (while (search-backward ">" nil t)
3997             (setq end (1+ (point)))
3998             (when (search-backward "<" nil t)
3999               (setq new-child (buffer-substring (point) end))
4000               (push (list (incf generation)
4001                           child (setq child new-child)
4002                           subject date)
4003                     relations)))
4004           (when child
4005             (push (list (1+ generation) child nil subject) relations))
4006           (erase-buffer)))
4007       (kill-buffer (current-buffer)))
4008     ;; Sort over trustworthiness.
4009     (mapcar
4010      (lambda (relation)
4011        (when (gnus-dependencies-add-header
4012               (make-full-mail-header
4013                gnus-reffed-article-number
4014                (nth 3 relation) "" (or (nth 4 relation) "")
4015                (nth 1 relation)
4016                (or (nth 2 relation) "") 0 0 "")
4017               gnus-newsgroup-dependencies nil)
4018          (push gnus-reffed-article-number gnus-newsgroup-limit)
4019          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4020          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4021                gnus-newsgroup-reads)
4022          (decf gnus-reffed-article-number)))
4023      (sort relations 'car-less-than-car))
4024     (gnus-message 7 "Making sparse threads...done")))
4025
4026 (defun gnus-build-old-threads ()
4027   ;; Look at all the articles that refer back to old articles, and
4028   ;; fetch the headers for the articles that aren't there.  This will
4029   ;; build complete threads - if the roots haven't been expired by the
4030   ;; server, that is.
4031   (let ((mail-parse-charset gnus-newsgroup-charset)
4032         id heads)
4033     (mapatoms
4034      (lambda (refs)
4035        (when (not (car (symbol-value refs)))
4036          (setq heads (cdr (symbol-value refs)))
4037          (while heads
4038            (if (memq (mail-header-number (caar heads))
4039                      gnus-newsgroup-dormant)
4040                (setq heads (cdr heads))
4041              (setq id (symbol-name refs))
4042              (while (and (setq id (gnus-build-get-header id))
4043                          (not (car (gnus-id-to-thread id)))))
4044              (setq heads nil)))))
4045      gnus-newsgroup-dependencies)))
4046
4047 (defsubst gnus-remove-odd-characters (string)
4048   "Translate STRING into something that doesn't contain weird characters."
4049   (mm-subst-char-in-string
4050    ?\r ?\-
4051    (mm-subst-char-in-string ?\n ?\- string t) t))
4052
4053 ;; This function has to be called with point after the article number
4054 ;; on the beginning of the line.
4055 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4056   (let ((eol (point-at-eol))
4057         (buffer (current-buffer))
4058         header references in-reply-to)
4059
4060     ;; overview: [num subject from date id refs chars lines misc]
4061     (unwind-protect
4062         (let (x)
4063           (narrow-to-region (point) eol)
4064           (unless (eobp)
4065             (forward-char))
4066
4067           (setq header
4068                 (make-full-mail-header
4069                  number                 ; number
4070                  (condition-case ()     ; subject
4071                      (gnus-remove-odd-characters
4072                       (funcall gnus-decode-encoded-word-function
4073                                (setq x (nnheader-nov-field))))
4074                    (error x))
4075                  (condition-case ()     ; from
4076                      (gnus-remove-odd-characters
4077                       (funcall gnus-decode-encoded-word-function
4078                                (setq x (nnheader-nov-field))))
4079                    (error x))
4080                  (nnheader-nov-field)   ; date
4081                  (nnheader-nov-read-message-id number)  ; id
4082                  (setq references (nnheader-nov-field)) ; refs
4083                  (nnheader-nov-read-integer) ; chars
4084                  (nnheader-nov-read-integer) ; lines
4085                  (unless (eobp)
4086                    (if (looking-at "Xref: ")
4087                        (goto-char (match-end 0)))
4088                    (nnheader-nov-field)) ; Xref
4089                  (nnheader-nov-parse-extra)))) ; extra
4090
4091       (widen))
4092
4093     (when (and (string= references "")
4094                (setq in-reply-to (mail-header-extra header))
4095                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4096       (mail-header-set-references
4097        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4098
4099     (when gnus-alter-header-function
4100       (funcall gnus-alter-header-function header))
4101     (gnus-dependencies-add-header header dependencies force-new)))
4102
4103 (defun gnus-build-get-header (id)
4104   "Look through the buffer of NOV lines and find the header to ID.
4105 Enter this line into the dependencies hash table, and return
4106 the id of the parent article (if any)."
4107   (let ((deps gnus-newsgroup-dependencies)
4108         found header)
4109     (prog1
4110         (save-excursion
4111           (set-buffer nntp-server-buffer)
4112           (let ((case-fold-search nil))
4113             (goto-char (point-min))
4114             (while (and (not found)
4115                         (search-forward id nil t))
4116               (beginning-of-line)
4117               (setq found (looking-at
4118                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4119                                    (regexp-quote id))))
4120               (or found (beginning-of-line 2)))
4121             (when found
4122               (beginning-of-line)
4123               (and
4124                (setq header (gnus-nov-parse-line
4125                              (read (current-buffer)) deps))
4126                (gnus-parent-id (mail-header-references header))))))
4127       (when header
4128         (let ((number (mail-header-number header)))
4129           (push number gnus-newsgroup-limit)
4130           (push header gnus-newsgroup-headers)
4131           (if (memq number gnus-newsgroup-unselected)
4132               (progn
4133                 (setq gnus-newsgroup-unreads
4134                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4135                                                number))
4136                 (setq gnus-newsgroup-unselected
4137                       (delq number gnus-newsgroup-unselected)))
4138             (push number gnus-newsgroup-ancient)))))))
4139
4140 (defun gnus-build-all-threads ()
4141   "Read all the headers."
4142   (let ((gnus-summary-ignore-duplicates t)
4143         (mail-parse-charset gnus-newsgroup-charset)
4144         (dependencies gnus-newsgroup-dependencies)
4145         header article)
4146     (save-excursion
4147       (set-buffer nntp-server-buffer)
4148       (let ((case-fold-search nil))
4149         (goto-char (point-min))
4150         (while (not (eobp))
4151           (ignore-errors
4152             (setq article (read (current-buffer))
4153                   header (gnus-nov-parse-line article dependencies)))
4154           (when header
4155             (with-current-buffer gnus-summary-buffer
4156               (push header gnus-newsgroup-headers)
4157               (if (memq (setq article (mail-header-number header))
4158                         gnus-newsgroup-unselected)
4159                   (progn
4160                     (setq gnus-newsgroup-unreads
4161                           (gnus-add-to-sorted-list
4162                            gnus-newsgroup-unreads article))
4163                     (setq gnus-newsgroup-unselected
4164                           (delq article gnus-newsgroup-unselected)))
4165                 (push article gnus-newsgroup-ancient)))
4166             (forward-line 1)))))))
4167
4168 (defun gnus-summary-update-article-line (article header)
4169   "Update the line for ARTICLE using HEADER."
4170   (let* ((id (mail-header-id header))
4171          (thread (gnus-id-to-thread id)))
4172     (unless thread
4173       (error "Article in no thread"))
4174     ;; Update the thread.
4175     (setcar thread header)
4176     (gnus-summary-goto-subject article)
4177     (let* ((datal (gnus-data-find-list article))
4178            (data (car datal))
4179            (buffer-read-only nil)
4180            (level (gnus-summary-thread-level)))
4181       (gnus-delete-line)
4182       (let ((inserted (- (point)
4183                          (progn
4184                            (gnus-summary-insert-line
4185                             header level nil
4186                             (memq article gnus-newsgroup-undownloaded)
4187                             (gnus-article-mark article)
4188                             (memq article gnus-newsgroup-replied)
4189                             (memq article gnus-newsgroup-expirable)
4190                             ;; Only insert the Subject string when it's different
4191                             ;; from the previous Subject string.
4192                             (if (and
4193                                  gnus-show-threads
4194                                  (gnus-subject-equal
4195                                   (condition-case ()
4196                                       (mail-header-subject
4197                                        (gnus-data-header
4198                                         (cadr
4199                                          (gnus-data-find-list
4200                                           article
4201                                           (gnus-data-list t)))))
4202                                     ;; Error on the side of excessive subjects.
4203                                     (error ""))
4204                                   (mail-header-subject header)))
4205                                 ""
4206                               (mail-header-subject header))
4207                             nil (cdr (assq article gnus-newsgroup-scored))
4208                             (memq article gnus-newsgroup-processable))
4209                            (point)))))
4210         (when (cdr datal)
4211           (gnus-data-update-list
4212            (cdr datal)
4213            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4214
4215 (defun gnus-summary-update-article (article &optional iheader)
4216   "Update ARTICLE in the summary buffer."
4217   (set-buffer gnus-summary-buffer)
4218   (let* ((header (gnus-summary-article-header article))
4219          (id (mail-header-id header))
4220          (data (gnus-data-find article))
4221          (thread (gnus-id-to-thread id))
4222          (references (mail-header-references header))
4223          (parent
4224           (gnus-id-to-thread
4225            (or (gnus-parent-id
4226                 (when (and references
4227                            (not (equal "" references)))
4228                   references))
4229                "none")))
4230          (buffer-read-only nil)
4231          (old (car thread)))
4232     (when thread
4233       (unless iheader
4234         (setcar thread nil)
4235         (when parent
4236           (delq thread parent)))
4237       (if (gnus-summary-insert-subject id header)
4238           ;; Set the (possibly) new article number in the data structure.
4239           (gnus-data-set-number data (gnus-id-to-article id))
4240         (setcar thread old)
4241         nil))))
4242
4243 (defun gnus-rebuild-thread (id &optional line)
4244   "Rebuild the thread containing ID.
4245 If LINE, insert the rebuilt thread starting on line LINE."
4246   (let ((buffer-read-only nil)
4247         old-pos current thread data)
4248     (if (not gnus-show-threads)
4249         (setq thread (list (car (gnus-id-to-thread id))))
4250       ;; Get the thread this article is part of.
4251       (setq thread (gnus-remove-thread id)))
4252     (setq old-pos (point-at-bol))
4253     (setq current (save-excursion
4254                     (and (re-search-backward "[\r\n]" nil t)
4255                          (gnus-summary-article-number))))
4256     ;; If this is a gathered thread, we have to go some re-gathering.
4257     (when (stringp (car thread))
4258       (let ((subject (car thread))
4259             roots thr)
4260         (setq thread (cdr thread))
4261         (while thread
4262           (unless (memq (setq thr (gnus-id-to-thread
4263                                    (gnus-root-id
4264                                     (mail-header-id (caar thread)))))
4265                         roots)
4266             (push thr roots))
4267           (setq thread (cdr thread)))
4268         ;; We now have all (unique) roots.
4269         (if (= (length roots) 1)
4270             ;; All the loose roots are now one solid root.
4271             (setq thread (car roots))
4272           (setq thread (cons subject (gnus-sort-threads roots))))))
4273     (let (threads)
4274       ;; We then insert this thread into the summary buffer.
4275       (when line
4276         (goto-char (point-min))
4277         (forward-line (1- line)))
4278       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4279         (if gnus-show-threads
4280             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4281           (gnus-summary-prepare-unthreaded thread))
4282         (setq data (nreverse gnus-newsgroup-data))
4283         (setq threads gnus-newsgroup-threads))
4284       ;; We splice the new data into the data structure.
4285       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4286       ;;!!! then we want to insert at the beginning of the buffer.
4287       ;;!!! That happens to be true with Gnus now, but that may
4288       ;;!!! change in the future.  Perhaps.
4289       (gnus-data-enter-list
4290        (if line nil current) data (- (point) old-pos))
4291       (setq gnus-newsgroup-threads
4292             (nconc threads gnus-newsgroup-threads))
4293       (gnus-data-compute-positions))))
4294
4295 (defun gnus-number-to-header (number)
4296   "Return the header for article NUMBER."
4297   (let ((headers gnus-newsgroup-headers))
4298     (while (and headers
4299                 (not (= number (mail-header-number (car headers)))))
4300       (pop headers))
4301     (when headers
4302       (car headers))))
4303
4304 (defun gnus-parent-headers (in-headers &optional generation)
4305   "Return the headers of the GENERATIONeth parent of HEADERS."
4306   (unless generation
4307     (setq generation 1))
4308   (let ((parent t)
4309         (headers in-headers)
4310         references)
4311     (while (and parent
4312                 (not (zerop generation))
4313                 (setq references (mail-header-references headers)))
4314       (setq headers (if (and references
4315                              (setq parent (gnus-parent-id references)))
4316                         (car (gnus-id-to-thread parent))
4317                       nil))
4318       (decf generation))
4319     (and (not (eq headers in-headers))
4320          headers)))
4321
4322 (defun gnus-id-to-thread (id)
4323   "Return the (sub-)thread where ID appears."
4324   (gnus-gethash id gnus-newsgroup-dependencies))
4325
4326 (defun gnus-id-to-article (id)
4327   "Return the article number of ID."
4328   (let ((thread (gnus-id-to-thread id)))
4329     (when (and thread
4330                (car thread))
4331       (mail-header-number (car thread)))))
4332
4333 (defun gnus-id-to-header (id)
4334   "Return the article headers of ID."
4335   (car (gnus-id-to-thread id)))
4336
4337 (defun gnus-article-displayed-root-p (article)
4338   "Say whether ARTICLE is a root(ish) article."
4339   (let ((level (gnus-summary-thread-level article))
4340         (refs (mail-header-references  (gnus-summary-article-header article)))
4341         particle)
4342     (cond
4343      ((null level) nil)
4344      ((zerop level) t)
4345      ((null refs) t)
4346      ((null (gnus-parent-id refs)) t)
4347      ((and (= 1 level)
4348            (null (setq particle (gnus-id-to-article
4349                                  (gnus-parent-id refs))))
4350            (null (gnus-summary-thread-level particle)))))))
4351
4352 (defun gnus-root-id (id)
4353   "Return the id of the root of the thread where ID appears."
4354   (let (last-id prev)
4355     (while (and id (setq prev (car (gnus-id-to-thread id))))
4356       (setq last-id id
4357             id (gnus-parent-id (mail-header-references prev))))
4358     last-id))
4359
4360 (defun gnus-articles-in-thread (thread)
4361   "Return the list of articles in THREAD."
4362   (cons (mail-header-number (car thread))
4363         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4364
4365 (defun gnus-remove-thread (id &optional dont-remove)
4366   "Remove the thread that has ID in it."
4367   (let (headers thread last-id)
4368     ;; First go up in this thread until we find the root.
4369     (setq last-id (gnus-root-id id)
4370           headers (message-flatten-list (gnus-id-to-thread last-id)))
4371     ;; We have now found the real root of this thread.  It might have
4372     ;; been gathered into some loose thread, so we have to search
4373     ;; through the threads to find the thread we wanted.
4374     (let ((threads gnus-newsgroup-threads)
4375           sub)
4376       (while threads
4377         (setq sub (car threads))
4378         (if (stringp (car sub))
4379             ;; This is a gathered thread, so we look at the roots
4380             ;; below it to find whether this article is in this
4381             ;; gathered root.
4382             (progn
4383               (setq sub (cdr sub))
4384               (while sub
4385                 (when (member (caar sub) headers)
4386                   (setq thread (car threads)
4387                         threads nil
4388                         sub nil))
4389                 (setq sub (cdr sub))))
4390           ;; It's an ordinary thread, so we check it.
4391           (when (eq (car sub) (car headers))
4392             (setq thread sub
4393                   threads nil)))
4394         (setq threads (cdr threads)))
4395       ;; If this article is in no thread, then it's a root.
4396       (if thread
4397           (unless dont-remove
4398             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4399         (setq thread (gnus-id-to-thread last-id)))
4400       (when thread
4401         (prog1
4402             thread                      ; We return this thread.
4403           (unless dont-remove
4404             (if (stringp (car thread))
4405                 (progn
4406                   ;; If we use dummy roots, then we have to remove the
4407                   ;; dummy root as well.
4408                   (when (eq gnus-summary-make-false-root 'dummy)
4409                     ;; We go to the dummy root by going to
4410                     ;; the first sub-"thread", and then one line up.
4411                     (gnus-summary-goto-article
4412                      (mail-header-number (caadr thread)))
4413                     (forward-line -1)
4414                     (gnus-delete-line)
4415                     (gnus-data-compute-positions))
4416                   (setq thread (cdr thread))
4417                   (while thread
4418                     (gnus-remove-thread-1 (car thread))
4419                     (setq thread (cdr thread))))
4420               (gnus-remove-thread-1 thread))))))))
4421
4422 (defun gnus-remove-thread-1 (thread)
4423   "Remove the thread THREAD recursively."
4424   (let ((number (mail-header-number (pop thread)))
4425         d)
4426     (setq thread (reverse thread))
4427     (while thread
4428       (gnus-remove-thread-1 (pop thread)))
4429     (when (setq d (gnus-data-find number))
4430       (goto-char (gnus-data-pos d))
4431       (gnus-summary-show-thread)
4432       (gnus-data-remove
4433        number
4434        (- (point-at-bol)
4435           (prog1
4436               (1+ (point-at-eol))
4437             (gnus-delete-line)))))))
4438
4439 (defun gnus-sort-threads-1 (threads func)
4440   (sort (mapcar (lambda (thread)
4441                   (cons (car thread)
4442                         (and (cdr thread)
4443                              (gnus-sort-threads-1 (cdr thread) func))))
4444                 threads) func))
4445
4446 (defun gnus-sort-threads (threads)
4447   "Sort THREADS."
4448   (if (not gnus-thread-sort-functions)
4449       threads
4450     (gnus-message 8 "Sorting threads...")
4451     (let ((max-lisp-eval-depth 5000))
4452       (prog1 (gnus-sort-threads-1
4453          threads
4454          (gnus-make-sort-function gnus-thread-sort-functions))
4455         (gnus-message 8 "Sorting threads...done")))))
4456
4457 (defun gnus-sort-articles (articles)
4458   "Sort ARTICLES."
4459   (when gnus-article-sort-functions
4460     (gnus-message 7 "Sorting articles...")
4461     (prog1
4462         (setq gnus-newsgroup-headers
4463               (sort articles (gnus-make-sort-function
4464                               gnus-article-sort-functions)))
4465       (gnus-message 7 "Sorting articles...done"))))
4466
4467 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4468 (defmacro gnus-thread-header (thread)
4469   "Return header of first article in THREAD.
4470 Note that THREAD must never, ever be anything else than a variable -
4471 using some other form will lead to serious barfage."
4472   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4473   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4474   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4475         (vector thread) 2))
4476
4477 (defsubst gnus-article-sort-by-number (h1 h2)
4478   "Sort articles by article number."
4479   (< (mail-header-number h1)
4480      (mail-header-number h2)))
4481
4482 (defun gnus-thread-sort-by-number (h1 h2)
4483   "Sort threads by root article number."
4484   (gnus-article-sort-by-number
4485    (gnus-thread-header h1) (gnus-thread-header h2)))
4486
4487 (defsubst gnus-article-sort-by-random (h1 h2)
4488   "Sort articles by article number."
4489   (zerop (random 2)))
4490
4491 (defun gnus-thread-sort-by-random (h1 h2)
4492   "Sort threads by root article number."
4493   (gnus-article-sort-by-random
4494    (gnus-thread-header h1) (gnus-thread-header h2)))
4495
4496 (defsubst gnus-article-sort-by-lines (h1 h2)
4497   "Sort articles by article Lines header."
4498   (< (mail-header-lines h1)
4499      (mail-header-lines h2)))
4500
4501 (defun gnus-thread-sort-by-lines (h1 h2)
4502   "Sort threads by root article Lines header."
4503   (gnus-article-sort-by-lines
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defsubst gnus-article-sort-by-chars (h1 h2)
4507   "Sort articles by octet length."
4508   (< (mail-header-chars h1)
4509      (mail-header-chars h2)))
4510
4511 (defun gnus-thread-sort-by-chars (h1 h2)
4512   "Sort threads by root article octet length."
4513   (gnus-article-sort-by-chars
4514    (gnus-thread-header h1) (gnus-thread-header h2)))
4515
4516 (defsubst gnus-article-sort-by-author (h1 h2)
4517   "Sort articles by root author."
4518   (string-lessp
4519    (let ((extract (funcall
4520                    gnus-extract-address-components
4521                    (mail-header-from h1))))
4522      (or (car extract) (cadr extract) ""))
4523    (let ((extract (funcall
4524                    gnus-extract-address-components
4525                    (mail-header-from h2))))
4526      (or (car extract) (cadr extract) ""))))
4527
4528 (defun gnus-thread-sort-by-author (h1 h2)
4529   "Sort threads by root author."
4530   (gnus-article-sort-by-author
4531    (gnus-thread-header h1)  (gnus-thread-header h2)))
4532
4533 (defsubst gnus-article-sort-by-subject (h1 h2)
4534   "Sort articles by root subject."
4535   (string-lessp
4536    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4537    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4538
4539 (defun gnus-thread-sort-by-subject (h1 h2)
4540   "Sort threads by root subject."
4541   (gnus-article-sort-by-subject
4542    (gnus-thread-header h1) (gnus-thread-header h2)))
4543
4544 (defsubst gnus-article-sort-by-date (h1 h2)
4545   "Sort articles by root article date."
4546   (time-less-p
4547    (gnus-date-get-time (mail-header-date h1))
4548    (gnus-date-get-time (mail-header-date h2))))
4549
4550 (defun gnus-thread-sort-by-date (h1 h2)
4551   "Sort threads by root article date."
4552   (gnus-article-sort-by-date
4553    (gnus-thread-header h1) (gnus-thread-header h2)))
4554
4555 (defsubst gnus-article-sort-by-score (h1 h2)
4556   "Sort articles by root article score.
4557 Unscored articles will be counted as having a score of zero."
4558   (> (or (cdr (assq (mail-header-number h1)
4559                     gnus-newsgroup-scored))
4560          gnus-summary-default-score 0)
4561      (or (cdr (assq (mail-header-number h2)
4562                     gnus-newsgroup-scored))
4563          gnus-summary-default-score 0)))
4564
4565 (defun gnus-thread-sort-by-score (h1 h2)
4566   "Sort threads by root article score."
4567   (gnus-article-sort-by-score
4568    (gnus-thread-header h1) (gnus-thread-header h2)))
4569
4570 (defun gnus-thread-sort-by-total-score (h1 h2)
4571   "Sort threads by the sum of all scores in the thread.
4572 Unscored articles will be counted as having a score of zero."
4573   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4574
4575 (defun gnus-thread-total-score (thread)
4576   ;; This function find the total score of THREAD.
4577   (cond
4578    ((null thread)
4579     0)
4580    ((consp thread)
4581     (if (stringp (car thread))
4582         (apply gnus-thread-score-function 0
4583                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4584       (gnus-thread-total-score-1 thread)))
4585    (t
4586     (gnus-thread-total-score-1 (list thread)))))
4587
4588 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4589   "Sort threads such that the thread with the most recently arrived article comes first."
4590   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4591
4592 (defun gnus-thread-highest-number (thread)
4593   "Return the highest article number in THREAD."
4594   (apply 'max (mapcar (lambda (header)
4595                         (mail-header-number header))
4596                       (message-flatten-list thread))))
4597
4598 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4599   "Sort threads such that the thread with the most recently dated article comes first."
4600   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4601
4602 (defun gnus-thread-latest-date (thread)
4603   "Return the highest article date in THREAD."
4604   (let ((previous-time 0))
4605     (apply 'max
4606            (mapcar
4607             (lambda (header)
4608               (setq previous-time
4609                     (condition-case ()
4610                         (time-to-seconds (mail-header-parse-date
4611                                           (mail-header-date header)))
4612                       (error previous-time))))
4613             (sort
4614              (message-flatten-list thread)
4615              (lambda (h1 h2)
4616                (< (mail-header-number h1)
4617                   (mail-header-number h2))))))))
4618
4619 (defun gnus-thread-total-score-1 (root)
4620   ;; This function find the total score of the thread below ROOT.
4621   (setq root (car root))
4622   (apply gnus-thread-score-function
4623          (or (append
4624               (mapcar 'gnus-thread-total-score
4625                       (cdr (gnus-id-to-thread (mail-header-id root))))
4626               (when (> (mail-header-number root) 0)
4627                 (list (or (cdr (assq (mail-header-number root)
4628                                      gnus-newsgroup-scored))
4629                           gnus-summary-default-score 0))))
4630              (list gnus-summary-default-score)
4631              '(0))))
4632
4633 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4634 (defvar gnus-tmp-prev-subject nil)
4635 (defvar gnus-tmp-false-parent nil)
4636 (defvar gnus-tmp-root-expunged nil)
4637 (defvar gnus-tmp-dummy-line nil)
4638
4639 (eval-when-compile (defvar gnus-tmp-header))
4640 (defun gnus-extra-header (type &optional header)
4641   "Return the extra header of TYPE."
4642   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4643       ""))
4644
4645 (defvar gnus-tmp-thread-tree-header-string "")
4646
4647 (defcustom gnus-sum-thread-tree-root "> "
4648   "With %B spec, used for the root of a thread.
4649 If nil, use subject instead."
4650   :type '(radio (const :format "%v  " nil) (string :size 0))
4651   :group 'gnus-thread)
4652 (defcustom gnus-sum-thread-tree-false-root "> "
4653   "With %B spec, used for a false root of a thread.
4654 If nil, use subject instead."
4655   :type '(radio (const :format "%v  " nil) (string :size 0))
4656   :group 'gnus-thread)
4657 (defcustom gnus-sum-thread-tree-single-indent ""
4658   "With %B spec, used for a thread with just one message.
4659 If nil, use subject instead."
4660   :type '(radio (const :format "%v  " nil) (string :size 0))
4661   :group 'gnus-thread)
4662 (defcustom gnus-sum-thread-tree-vertical "| "
4663   "With %B spec, used for drawing a vertical line."
4664   :type 'string
4665   :group 'gnus-thread)
4666 (defcustom gnus-sum-thread-tree-indent "  "
4667   "With %B spec, used for indenting."
4668   :type 'string
4669   :group 'gnus-thread)
4670 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4671   "With %B spec, used for a leaf with brothers."
4672   :type 'string
4673   :group 'gnus-thread)
4674 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4675   "With %B spec, used for a leaf without brothers."
4676   :type 'string
4677   :group 'gnus-thread)
4678
4679 (defun gnus-summary-prepare-threads (threads)
4680   "Prepare summary buffer from THREADS and indentation LEVEL.
4681 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4682 or a straight list of headers."
4683   (gnus-message 7 "Generating summary...")
4684
4685   (setq gnus-newsgroup-threads threads)
4686   (beginning-of-line)
4687
4688   (let ((gnus-tmp-level 0)
4689         (default-score (or gnus-summary-default-score 0))
4690         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4691         (building-line-count gnus-summary-display-while-building)
4692         (building-count (integerp gnus-summary-display-while-building))
4693         thread number subject stack state gnus-tmp-gathered beg-match
4694         new-roots gnus-tmp-new-adopts thread-end simp-subject
4695         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4696         gnus-tmp-replied gnus-tmp-subject-or-nil
4697         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4698         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4699         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4700         tree-stack)
4701
4702     (setq gnus-tmp-prev-subject nil
4703           gnus-tmp-thread-tree-header-string "")
4704
4705     (if (vectorp (car threads))
4706         ;; If this is a straight (sic) list of headers, then a
4707         ;; threaded summary display isn't required, so we just create
4708         ;; an unthreaded one.
4709         (gnus-summary-prepare-unthreaded threads)
4710
4711       ;; Do the threaded display.
4712
4713       (if gnus-summary-display-while-building
4714           (switch-to-buffer (buffer-name)))
4715       (while (or threads stack gnus-tmp-new-adopts new-roots)
4716
4717         (if (and (= gnus-tmp-level 0)
4718                  (or (not stack)
4719                      (= (caar stack) 0))
4720                  (not gnus-tmp-false-parent)
4721                  (or gnus-tmp-new-adopts new-roots))
4722             (if gnus-tmp-new-adopts
4723                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4724                       thread (list (car gnus-tmp-new-adopts))
4725                       gnus-tmp-header (caar thread)
4726                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4727               (when new-roots
4728                 (setq thread (list (car new-roots))
4729                       gnus-tmp-header (caar thread)
4730                       new-roots (cdr new-roots))))
4731
4732           (if threads
4733               ;; If there are some threads, we do them before the
4734               ;; threads on the stack.
4735               (setq thread threads
4736                     gnus-tmp-header (caar thread))
4737             ;; There were no current threads, so we pop something off
4738             ;; the stack.
4739             (setq state (car stack)
4740                   gnus-tmp-level (car state)
4741                   tree-stack (cadr state)
4742                   thread (caddr state)
4743                   stack (cdr stack)
4744                   gnus-tmp-header (caar thread))))
4745
4746         (setq gnus-tmp-false-parent nil)
4747         (setq gnus-tmp-root-expunged nil)
4748         (setq thread-end nil)
4749
4750         (if (stringp gnus-tmp-header)
4751             ;; The header is a dummy root.
4752             (cond
4753              ((eq gnus-summary-make-false-root 'adopt)
4754               ;; We let the first article adopt the rest.
4755               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4756                                                (cddar thread)))
4757               (setq gnus-tmp-gathered
4758                     (nconc (mapcar
4759                             (lambda (h) (mail-header-number (car h)))
4760                             (cddar thread))
4761                            gnus-tmp-gathered))
4762               (setq thread (cons (list (caar thread)
4763                                        (cadar thread))
4764                                  (cdr thread)))
4765               (setq gnus-tmp-level -1
4766                     gnus-tmp-false-parent t))
4767              ((eq gnus-summary-make-false-root 'empty)
4768               ;; We print adopted articles with empty subject fields.
4769               (setq gnus-tmp-gathered
4770                     (nconc (mapcar
4771                             (lambda (h) (mail-header-number (car h)))
4772                             (cddar thread))
4773                            gnus-tmp-gathered))
4774               (setq gnus-tmp-level -1))
4775              ((eq gnus-summary-make-false-root 'dummy)
4776               ;; We remember that we probably want to output a dummy
4777               ;; root.
4778               (setq gnus-tmp-dummy-line gnus-tmp-header)
4779               (setq gnus-tmp-prev-subject gnus-tmp-header))
4780              (t
4781               ;; We do not make a root for the gathered
4782               ;; sub-threads at all.
4783               (setq gnus-tmp-level -1)))
4784
4785           (setq number (mail-header-number gnus-tmp-header)
4786                 subject (mail-header-subject gnus-tmp-header)
4787                 simp-subject (gnus-simplify-subject-fully subject))
4788
4789           (cond
4790            ;; If the thread has changed subject, we might want to make
4791            ;; this subthread into a root.
4792            ((and (null gnus-thread-ignore-subject)
4793                  (not (zerop gnus-tmp-level))
4794                  gnus-tmp-prev-subject
4795                  (not (string= gnus-tmp-prev-subject simp-subject)))
4796             (setq new-roots (nconc new-roots (list (car thread)))
4797                   thread-end t
4798                   gnus-tmp-header nil))
4799            ;; If the article lies outside the current limit,
4800            ;; then we do not display it.
4801            ((not (memq number gnus-newsgroup-limit))
4802             (setq gnus-tmp-gathered
4803                   (nconc (mapcar
4804                           (lambda (h) (mail-header-number (car h)))
4805                           (cdar thread))
4806                          gnus-tmp-gathered))
4807             (setq gnus-tmp-new-adopts (if (cdar thread)
4808                                           (append gnus-tmp-new-adopts
4809                                                   (cdar thread))
4810                                         gnus-tmp-new-adopts)
4811                   thread-end t
4812                   gnus-tmp-header nil)
4813             (when (zerop gnus-tmp-level)
4814               (setq gnus-tmp-root-expunged t)))
4815            ;; Perhaps this article is to be marked as read?
4816            ((and gnus-summary-mark-below
4817                  (< (or (cdr (assq number gnus-newsgroup-scored))
4818                         default-score)
4819                     gnus-summary-mark-below)
4820                  ;; Don't touch sparse articles.
4821                  (not (gnus-summary-article-sparse-p number))
4822                  (not (gnus-summary-article-ancient-p number)))
4823             (setq gnus-newsgroup-unreads
4824                   (delq number gnus-newsgroup-unreads))
4825             (if gnus-newsgroup-auto-expire
4826                 (setq gnus-newsgroup-expirable
4827                       (gnus-add-to-sorted-list
4828                        gnus-newsgroup-expirable number))
4829               (push (cons number gnus-low-score-mark)
4830                     gnus-newsgroup-reads))))
4831
4832           (when gnus-tmp-header
4833             ;; We may have an old dummy line to output before this
4834             ;; article.
4835             (when (and gnus-tmp-dummy-line
4836                        (gnus-subject-equal
4837                         gnus-tmp-dummy-line
4838                         (mail-header-subject gnus-tmp-header)))
4839               (gnus-summary-insert-dummy-line
4840                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4841               (setq gnus-tmp-dummy-line nil))
4842
4843             ;; Compute the mark.
4844             (setq gnus-tmp-unread (gnus-article-mark number))
4845
4846             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4847                                   gnus-tmp-header gnus-tmp-level)
4848                   gnus-newsgroup-data)
4849
4850             ;; Actually insert the line.
4851             (setq
4852              gnus-tmp-subject-or-nil
4853              (cond
4854               ((and gnus-thread-ignore-subject
4855                     gnus-tmp-prev-subject
4856                     (not (string= gnus-tmp-prev-subject simp-subject)))
4857                subject)
4858               ((zerop gnus-tmp-level)
4859                (if (and (eq gnus-summary-make-false-root 'empty)
4860                         (memq number gnus-tmp-gathered)
4861                         gnus-tmp-prev-subject
4862                         (string= gnus-tmp-prev-subject simp-subject))
4863                    gnus-summary-same-subject
4864                  subject))
4865               (t gnus-summary-same-subject)))
4866             (if (and (eq gnus-summary-make-false-root 'adopt)
4867                      (= gnus-tmp-level 1)
4868                      (memq number gnus-tmp-gathered))
4869                 (setq gnus-tmp-opening-bracket ?\<
4870                       gnus-tmp-closing-bracket ?\>)
4871               (setq gnus-tmp-opening-bracket ?\[
4872                     gnus-tmp-closing-bracket ?\]))
4873             (setq
4874              gnus-tmp-indentation
4875              (aref gnus-thread-indent-array gnus-tmp-level)
4876              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4877              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4878                                 gnus-summary-default-score 0)
4879              gnus-tmp-score-char
4880              (if (or (null gnus-summary-default-score)
4881                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4882                          gnus-summary-zcore-fuzz))
4883                  ?                      ;Whitespace
4884                (if (< gnus-tmp-score gnus-summary-default-score)
4885                    gnus-score-below-mark gnus-score-over-mark))
4886              gnus-tmp-replied
4887              (cond ((memq number gnus-newsgroup-processable)
4888                     gnus-process-mark)
4889                    ((memq number gnus-newsgroup-cached)
4890                     gnus-cached-mark)
4891                    ((memq number gnus-newsgroup-replied)
4892                     gnus-replied-mark)
4893                    ((memq number gnus-newsgroup-forwarded)
4894                     gnus-forwarded-mark)
4895                    ((memq number gnus-newsgroup-saved)
4896                     gnus-saved-mark)
4897                    ((memq number gnus-newsgroup-recent)
4898                     gnus-recent-mark)
4899                    ((memq number gnus-newsgroup-unseen)
4900                     gnus-unseen-mark)
4901                    (t gnus-no-mark))
4902              gnus-tmp-downloaded
4903              (cond ((memq number gnus-newsgroup-undownloaded)
4904                     gnus-undownloaded-mark)
4905                    (gnus-newsgroup-agentized
4906                     gnus-downloaded-mark)
4907                    (t
4908                     gnus-no-mark))
4909              gnus-tmp-from (mail-header-from gnus-tmp-header)
4910              gnus-tmp-name
4911              (cond
4912               ((string-match "<[^>]+> *$" gnus-tmp-from)
4913                (setq beg-match (match-beginning 0))
4914                (or (and (string-match "^\".+\"" gnus-tmp-from)
4915                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4916                    (substring gnus-tmp-from 0 beg-match)))
4917               ((string-match "(.+)" gnus-tmp-from)
4918                (substring gnus-tmp-from
4919                           (1+ (match-beginning 0)) (1- (match-end 0))))
4920               (t gnus-tmp-from))
4921
4922              ;; Do the %B string
4923              gnus-tmp-thread-tree-header-string
4924              (cond
4925               ((not gnus-show-threads) "")
4926               ((zerop gnus-tmp-level)
4927                (cond ((cdar thread)
4928                       (or gnus-sum-thread-tree-root subject))
4929                      (gnus-tmp-new-adopts
4930                       (or gnus-sum-thread-tree-false-root subject))
4931                      (t
4932                       (or gnus-sum-thread-tree-single-indent subject))))
4933               (t
4934                (concat (apply 'concat
4935                               (mapcar (lambda (item)
4936                                         (if (= item 1)
4937                                             gnus-sum-thread-tree-vertical
4938                                           gnus-sum-thread-tree-indent))
4939                                       (cdr (reverse tree-stack))))
4940                        (if (nth 1 thread)
4941                            gnus-sum-thread-tree-leaf-with-other
4942                          gnus-sum-thread-tree-single-leaf)))))
4943             (when (string= gnus-tmp-name "")
4944               (setq gnus-tmp-name gnus-tmp-from))
4945             (unless (numberp gnus-tmp-lines)
4946               (setq gnus-tmp-lines -1))
4947             (if (= gnus-tmp-lines -1)
4948                 (setq gnus-tmp-lines "?")
4949               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4950               (gnus-put-text-property
4951              (point)
4952              (progn (eval gnus-summary-line-format-spec) (point))
4953                'gnus-number number)
4954             (when gnus-visual-p
4955               (forward-line -1)
4956               (gnus-run-hooks 'gnus-summary-update-hook)
4957               (forward-line 1))
4958
4959             (setq gnus-tmp-prev-subject simp-subject)))
4960
4961         (when (nth 1 thread)
4962           (push (list (max 0 gnus-tmp-level)
4963                       (copy-sequence tree-stack)
4964                       (nthcdr 1 thread))
4965                 stack))
4966         (push (if (nth 1 thread) 1 0) tree-stack)
4967         (incf gnus-tmp-level)
4968         (setq threads (if thread-end nil (cdar thread)))
4969         (if gnus-summary-display-while-building
4970             (if building-count
4971                 (progn
4972                   ;; use a set frequency
4973                   (setq building-line-count (1- building-line-count))
4974                   (when (= building-line-count 0)
4975                     (sit-for 0)
4976                     (setq building-line-count
4977                           gnus-summary-display-while-building)))
4978               ;; always
4979               (sit-for 0)))
4980         (unless threads
4981           (setq gnus-tmp-level 0)))))
4982   (gnus-message 7 "Generating summary...done"))
4983
4984 (defun gnus-summary-prepare-unthreaded (headers)
4985   "Generate an unthreaded summary buffer based on HEADERS."
4986   (let (header number mark)
4987
4988     (beginning-of-line)
4989
4990     (while headers
4991       ;; We may have to root out some bad articles...
4992       (when (memq (setq number (mail-header-number
4993                                 (setq header (pop headers))))
4994                   gnus-newsgroup-limit)
4995         ;; Mark article as read when it has a low score.
4996         (when (and gnus-summary-mark-below
4997                    (< (or (cdr (assq number gnus-newsgroup-scored))
4998                           gnus-summary-default-score 0)
4999                       gnus-summary-mark-below)
5000                    (not (gnus-summary-article-ancient-p number)))
5001           (setq gnus-newsgroup-unreads
5002                 (delq number gnus-newsgroup-unreads))
5003           (if gnus-newsgroup-auto-expire
5004               (push number gnus-newsgroup-expirable)
5005             (push (cons number gnus-low-score-mark)
5006                   gnus-newsgroup-reads)))
5007
5008         (setq mark (gnus-article-mark number))
5009         (push (gnus-data-make number mark (1+ (point)) header 0)
5010               gnus-newsgroup-data)
5011         (gnus-summary-insert-line
5012          header 0 number
5013          (memq number gnus-newsgroup-undownloaded)
5014          mark (memq number gnus-newsgroup-replied)
5015          (memq number gnus-newsgroup-expirable)
5016          (mail-header-subject header) nil
5017          (cdr (assq number gnus-newsgroup-scored))
5018          (memq number gnus-newsgroup-processable))))))
5019
5020 (defun gnus-summary-remove-list-identifiers ()
5021   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5022   (let ((regexp (if (consp gnus-list-identifiers)
5023                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5024                   gnus-list-identifiers))
5025         changed subject)
5026     (when regexp
5027       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5028       (dolist (header gnus-newsgroup-headers)
5029         (setq subject (mail-header-subject header)
5030               changed nil)
5031         (while (string-match regexp subject)
5032           (setq subject
5033                 (concat (substring subject 0 (match-beginning 1))
5034                         (substring subject (match-end 0)))
5035                 changed t))
5036         (when changed
5037           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5038             (setq subject
5039                   (concat (substring subject 0 (match-beginning 1))
5040                           (substring subject (match-end 1)))))
5041           (mail-header-set-subject header subject))))))
5042
5043 (defun gnus-fetch-headers (articles)
5044   "Fetch headers of ARTICLES."
5045   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5046     (gnus-message 5 "Fetching headers for %s..." name)
5047     (prog1
5048         (if (eq 'nov
5049                 (setq gnus-headers-retrieved-by
5050                       (gnus-retrieve-headers
5051                        articles gnus-newsgroup-name
5052                        ;; We might want to fetch old headers, but
5053                        ;; not if there is only 1 article.
5054                        (and (or (and
5055                                  (not (eq gnus-fetch-old-headers 'some))
5056                                  (not (numberp gnus-fetch-old-headers)))
5057                                 (> (length articles) 1))
5058                             gnus-fetch-old-headers))))
5059             (gnus-get-newsgroup-headers-xover
5060              articles nil nil gnus-newsgroup-name t)
5061           (gnus-get-newsgroup-headers))
5062       (gnus-message 5 "Fetching headers for %s...done" name))))
5063
5064 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5065   "Select newsgroup GROUP.
5066 If READ-ALL is non-nil, all articles in the group are selected.
5067 If SELECT-ARTICLES, only select those articles from GROUP."
5068   (let* ((entry (gnus-group-entry group))
5069          ;;!!! Dirty hack; should be removed.
5070          (gnus-summary-ignore-duplicates
5071           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5072               t
5073             gnus-summary-ignore-duplicates))
5074          (info (nth 2 entry))
5075          articles fetched-articles cached)
5076
5077     (unless (gnus-check-server
5078              (set (make-local-variable 'gnus-current-select-method)
5079                   (gnus-find-method-for-group group)))
5080       (error "Couldn't open server"))
5081
5082     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5083         (gnus-activate-group group)     ; Or we can activate it...
5084         (progn                          ; Or we bug out.
5085           (when (equal major-mode 'gnus-summary-mode)
5086             (gnus-kill-buffer (current-buffer)))
5087           (error "Couldn't activate group %s: %s"
5088                  group (gnus-status-message group))))
5089
5090     (unless (gnus-request-group group t)
5091       (when (equal major-mode 'gnus-summary-mode)
5092         (gnus-kill-buffer (current-buffer)))
5093       (error "Couldn't request group %s: %s"
5094              group (gnus-status-message group)))
5095
5096     (when gnus-agent
5097       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5098       
5099       (setq gnus-summary-use-undownloaded-faces
5100             (gnus-agent-find-parameter
5101              group
5102              'agent-enable-undownloaded-faces)))
5103
5104     (setq gnus-newsgroup-name group
5105           gnus-newsgroup-unselected nil
5106           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5107
5108     (let ((display (gnus-group-find-parameter group 'display)))
5109       (setq gnus-newsgroup-display
5110             (cond
5111              ((not (zerop (or (car-safe read-all) 0)))
5112               ;; The user entered the group with C-u SPC/RET, let's show
5113               ;; all articles.
5114               'gnus-not-ignore)
5115              ((eq display 'all)
5116               'gnus-not-ignore)
5117              ((arrayp display)
5118               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5119              ((numberp display)
5120               ;; The following is probably the "correct" solution, but
5121               ;; it makes Gnus fetch all headers and then limit the
5122               ;; articles (which is slow), so instead we hack the
5123               ;; select-articles parameter instead. -- Simon Josefsson
5124               ;; <jas@kth.se>
5125               ;;
5126               ;; (gnus-byte-compile
5127               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5128               ;;                         display)))))
5129               (setq select-articles
5130                     (gnus-uncompress-range
5131                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5132                              (if (> tmp 0)
5133                                  tmp
5134                                1))
5135                            (cdr (gnus-active group)))))
5136               nil)
5137              (t
5138               nil))))
5139
5140     (gnus-summary-setup-default-charset)
5141
5142     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5143     (when (gnus-virtual-group-p group)
5144       (setq cached gnus-newsgroup-cached))
5145
5146     (setq gnus-newsgroup-unreads
5147           (gnus-sorted-ndifference
5148            (gnus-sorted-ndifference gnus-newsgroup-unreads
5149                                     gnus-newsgroup-marked)
5150            gnus-newsgroup-dormant))
5151
5152     (setq gnus-newsgroup-processable nil)
5153
5154     (gnus-update-read-articles group gnus-newsgroup-unreads)
5155
5156     ;; Adjust and set lists of article marks.
5157     (when info
5158       (gnus-adjust-marked-articles info))
5159     (if (setq articles select-articles)
5160         (setq gnus-newsgroup-unselected
5161               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5162       (setq articles (gnus-articles-to-read group read-all)))
5163
5164     (cond
5165      ((null articles)
5166       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5167       'quit)
5168      ((eq articles 0) nil)
5169      (t
5170       ;; Init the dependencies hash table.
5171       (setq gnus-newsgroup-dependencies
5172             (gnus-make-hashtable (length articles)))
5173       (gnus-set-global-variables)
5174       ;; Retrieve the headers and read them in.
5175
5176       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5177
5178       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5179       (when cached
5180         (setq gnus-newsgroup-cached cached))
5181
5182       ;; Suppress duplicates?
5183       (when gnus-suppress-duplicates
5184         (gnus-dup-suppress-articles))
5185
5186       ;; Set the initial limit.
5187       (setq gnus-newsgroup-limit (copy-sequence articles))
5188       ;; Remove canceled articles from the list of unread articles.
5189       (setq fetched-articles
5190             (mapcar (lambda (headers) (mail-header-number headers))
5191                     gnus-newsgroup-headers))
5192       (setq gnus-newsgroup-articles fetched-articles)
5193       (setq gnus-newsgroup-unreads
5194             (gnus-sorted-nintersection
5195              gnus-newsgroup-unreads fetched-articles))
5196       (gnus-compute-unseen-list)
5197
5198       ;; Removed marked articles that do not exist.
5199       (gnus-update-missing-marks
5200        (gnus-sorted-difference articles fetched-articles))
5201       ;; We might want to build some more threads first.
5202       (when (and gnus-fetch-old-headers
5203                  (eq gnus-headers-retrieved-by 'nov))
5204         (if (eq gnus-fetch-old-headers 'invisible)
5205             (gnus-build-all-threads)
5206           (gnus-build-old-threads)))
5207       ;; Let the Gnus agent mark articles as read.
5208       (when gnus-agent
5209         (gnus-agent-get-undownloaded-list))
5210       ;; Remove list identifiers from subject
5211       (when gnus-list-identifiers
5212         (gnus-summary-remove-list-identifiers))
5213       ;; Check whether auto-expire is to be done in this group.
5214       (setq gnus-newsgroup-auto-expire
5215             (gnus-group-auto-expirable-p group))
5216       ;; Set up the article buffer now, if necessary.
5217       (unless gnus-single-article-buffer
5218         (gnus-article-setup-buffer))
5219       ;; First and last article in this newsgroup.
5220       (when gnus-newsgroup-headers
5221         (setq gnus-newsgroup-begin
5222               (mail-header-number (car gnus-newsgroup-headers))
5223               gnus-newsgroup-end
5224               (mail-header-number
5225                (gnus-last-element gnus-newsgroup-headers))))
5226       ;; GROUP is successfully selected.
5227       (or gnus-newsgroup-headers t)))))
5228
5229 (defun gnus-compute-unseen-list ()
5230   ;; The `seen' marks are treated specially.
5231   (if (not gnus-newsgroup-seen)
5232       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5233     (setq gnus-newsgroup-unseen
5234           (gnus-inverse-list-range-intersection
5235            gnus-newsgroup-articles gnus-newsgroup-seen))))
5236
5237 (defun gnus-summary-display-make-predicate (display)
5238   (require 'gnus-agent)
5239   (when (= (length display) 1)
5240     (setq display (car display)))
5241   (unless gnus-summary-display-cache
5242     (dolist (elem (append '((unread . unread)
5243                             (read . read)
5244                             (unseen . unseen))
5245                           gnus-article-mark-lists))
5246       (push (cons (cdr elem)
5247                   (gnus-byte-compile
5248                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5249             gnus-summary-display-cache)))
5250   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5251         (gnus-category-predicate-cache gnus-summary-display-cache))
5252     (gnus-get-predicate display)))
5253
5254 ;; Uses the dynamically bound `number' variable.
5255 (eval-when-compile
5256   (defvar number))
5257 (defun gnus-article-marked-p (type &optional article)
5258   (let ((article (or article number)))
5259     (cond
5260      ((eq type 'tick)
5261       (memq article gnus-newsgroup-marked))
5262      ((eq type 'spam)
5263       (memq article gnus-newsgroup-spam-marked))
5264      ((eq type 'unsend)
5265       (memq article gnus-newsgroup-unsendable))
5266      ((eq type 'undownload)
5267       (memq article gnus-newsgroup-undownloaded))
5268      ((eq type 'download)
5269       (memq article gnus-newsgroup-downloadable))
5270      ((eq type 'unread)
5271       (memq article gnus-newsgroup-unreads))
5272      ((eq type 'read)
5273       (memq article gnus-newsgroup-reads))
5274      ((eq type 'dormant)
5275       (memq article gnus-newsgroup-dormant) )
5276      ((eq type 'expire)
5277       (memq article gnus-newsgroup-expirable))
5278      ((eq type 'reply)
5279       (memq article gnus-newsgroup-replied))
5280      ((eq type 'killed)
5281       (memq article gnus-newsgroup-killed))
5282      ((eq type 'bookmark)
5283       (assq article gnus-newsgroup-bookmarks))
5284      ((eq type 'score)
5285       (assq article gnus-newsgroup-scored))
5286      ((eq type 'save)
5287       (memq article gnus-newsgroup-saved))
5288      ((eq type 'cache)
5289       (memq article gnus-newsgroup-cached))
5290      ((eq type 'forward)
5291       (memq article gnus-newsgroup-forwarded))
5292      ((eq type 'seen)
5293       (not (memq article gnus-newsgroup-unseen)))
5294      ((eq type 'recent)
5295       (memq article gnus-newsgroup-recent))
5296      (t t))))
5297
5298 (defun gnus-articles-to-read (group &optional read-all)
5299   "Find out what articles the user wants to read."
5300   (let* ((articles
5301           ;; Select all articles if `read-all' is non-nil, or if there
5302           ;; are no unread articles.
5303           (if (or read-all
5304                   (and (zerop (length gnus-newsgroup-marked))
5305                        (zerop (length gnus-newsgroup-unreads)))
5306                   ;; Fetch all if the predicate is non-nil.
5307                   gnus-newsgroup-display)
5308               ;; We want to select the headers for all the articles in
5309               ;; the group, so we select either all the active
5310               ;; articles in the group, or (if that's nil), the
5311               ;; articles in the cache.
5312               (or
5313                (gnus-uncompress-range (gnus-active group))
5314                (gnus-cache-articles-in-group group))
5315             ;; Select only the "normal" subset of articles.
5316             (gnus-sorted-nunion
5317              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5318              gnus-newsgroup-unreads)))
5319          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5320          (scored (length scored-list))
5321          (number (length articles))
5322          (marked (+ (length gnus-newsgroup-marked)
5323                     (length gnus-newsgroup-dormant)))
5324          (select
5325           (cond
5326            ((numberp read-all)
5327             read-all)
5328            ((numberp gnus-newsgroup-display)
5329             gnus-newsgroup-display)
5330            (t
5331             (condition-case ()
5332                 (cond
5333                  ((and (or (<= scored marked) (= scored number))
5334                        (numberp gnus-large-newsgroup)
5335                        (> number gnus-large-newsgroup))
5336                   (let* ((cursor-in-echo-area nil)
5337                          (initial (gnus-parameter-large-newsgroup-initial
5338                                    gnus-newsgroup-name))
5339                          (input
5340                           (read-string
5341                            (format
5342                             "How many articles from %s (%s %d): "
5343                             (gnus-limit-string
5344                              (gnus-group-decoded-name gnus-newsgroup-name)
5345                              35)
5346                             (if initial "max" "default")
5347                             number)
5348                            (if initial
5349                                (cons (number-to-string initial)
5350                                      0)))))
5351                     (if (string-match "^[ \t]*$" input) number input)))
5352                  ((and (> scored marked) (< scored number)
5353                        (> (- scored number) 20))
5354                   (let ((input
5355                          (read-string
5356                           (format "%s %s (%d scored, %d total): "
5357                                   "How many articles from"
5358                                   (gnus-group-decoded-name group)
5359                                   scored number))))
5360                     (if (string-match "^[ \t]*$" input)
5361                         number input)))
5362                  (t number))
5363               (quit
5364                (message "Quit getting the articles to read")
5365                nil))))))
5366     (setq select (if (stringp select) (string-to-number select) select))
5367     (if (or (null select) (zerop select))
5368         select
5369       (if (and (not (zerop scored)) (<= (abs select) scored))
5370           (progn
5371             (setq articles (sort scored-list '<))
5372             (setq number (length articles)))
5373         (setq articles (copy-sequence articles)))
5374
5375       (when (< (abs select) number)
5376         (if (< select 0)
5377             ;; Select the N oldest articles.
5378             (setcdr (nthcdr (1- (abs select)) articles) nil)
5379           ;; Select the N most recent articles.
5380           (setq articles (nthcdr (- number select) articles))))
5381       (setq gnus-newsgroup-unselected
5382             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5383       (when gnus-alter-articles-to-read-function
5384         (setq articles
5385               (sort
5386                (funcall gnus-alter-articles-to-read-function
5387                         gnus-newsgroup-name articles)
5388                '<)))
5389       articles)))
5390
5391 (defun gnus-killed-articles (killed articles)
5392   (let (out)
5393     (while articles
5394       (when (inline (gnus-member-of-range (car articles) killed))
5395         (push (car articles) out))
5396       (setq articles (cdr articles)))
5397     out))
5398
5399 (defun gnus-uncompress-marks (marks)
5400   "Uncompress the mark ranges in MARKS."
5401   (let ((uncompressed '(score bookmark))
5402         out)
5403     (while marks
5404       (if (memq (caar marks) uncompressed)
5405           (push (car marks) out)
5406         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5407       (setq marks (cdr marks)))
5408     out))
5409
5410 (defun gnus-article-mark-to-type (mark)
5411   "Return the type of MARK."
5412   (or (cadr (assq mark gnus-article-special-mark-lists))
5413       'list))
5414
5415 (defun gnus-article-unpropagatable-p (mark)
5416   "Return whether MARK should be propagated to back end."
5417   (memq mark gnus-article-unpropagated-mark-lists))
5418
5419 (defun gnus-adjust-marked-articles (info)
5420   "Set all article lists and remove all marks that are no longer valid."
5421   (let* ((marked-lists (gnus-info-marks info))
5422          (active (gnus-active (gnus-info-group info)))
5423          (min (car active))
5424          (max (cdr active))
5425          (types gnus-article-mark-lists)
5426          marks var articles article mark mark-type
5427          bgn end)
5428
5429     (dolist (marks marked-lists)
5430       (setq mark (car marks)
5431             mark-type (gnus-article-mark-to-type mark)
5432             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5433
5434       ;; We set the variable according to the type of the marks list,
5435       ;; and then adjust the marks to a subset of the active articles.
5436       (cond
5437        ;; Adjust "simple" lists - compressed yet unsorted
5438        ((eq mark-type 'list)
5439         ;; Simultaneously uncompress and clip to active range
5440         ;; See gnus-uncompress-range for a description of possible marks
5441         (let (l lh)
5442           (if (not (cadr marks))
5443               (set var nil)
5444             (setq articles (if (numberp (cddr marks))
5445                                (list (cdr marks))
5446                              (cdr marks))
5447                   lh (cons nil nil)
5448                   l lh)
5449
5450             (while (setq article (pop articles))
5451               (cond ((consp article)
5452                      (setq bgn (max (car article) min)
5453                            end (min (cdr article) max))
5454                      (while (<= bgn end)
5455                        (setq l (setcdr l (cons bgn nil))
5456                              bgn (1+ bgn))))
5457                     ((and (<= min article)
5458                           (>= max article))
5459                      (setq l (setcdr l (cons article nil))))))
5460             (set var (cdr lh)))))
5461        ;; Adjust assocs.
5462        ((eq mark-type 'tuple)
5463         (set var (setq articles (cdr marks)))
5464         (when (not (listp (cdr (symbol-value var))))
5465           (set var (list (symbol-value var))))
5466         (when (not (listp (cdr articles)))
5467           (setq articles (list articles)))
5468         (while articles
5469           (when (or (not (consp (setq article (pop articles))))
5470                     (< (car article) min)
5471                     (> (car article) max))
5472             (set var (delq article (symbol-value var))))))
5473        ;; Adjust ranges (sloppily).
5474        ((eq mark-type 'range)
5475         (cond
5476          ((eq mark 'seen)
5477           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5478           ;; It should be (seen (NUM1 . NUM2)).
5479           (when (numberp (cddr marks))
5480             (setcdr marks (list (cdr marks))))
5481           (setq articles (cdr marks))
5482           (while (and articles
5483                       (or (and (consp (car articles))
5484                                (> min (cdar articles)))
5485                           (and (numberp (car articles))
5486                                (> min (car articles)))))
5487             (pop articles))
5488           (set var articles))))))))
5489
5490 (defun gnus-update-missing-marks (missing)
5491   "Go through the list of MISSING articles and remove them from the mark lists."
5492   (when missing
5493     (let (var m)
5494       ;; Go through all types.
5495       (dolist (elem gnus-article-mark-lists)
5496         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5497           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5498           (when (symbol-value var)
5499             ;; This list has articles.  So we delete all missing
5500             ;; articles from it.
5501             (setq m missing)
5502             (while m
5503               (set var (delq (pop m) (symbol-value var))))))))))
5504
5505 (defun gnus-update-marks ()
5506   "Enter the various lists of marked articles into the newsgroup info list."
5507   (let ((types gnus-article-mark-lists)
5508         (info (gnus-get-info gnus-newsgroup-name))
5509         type list newmarked symbol delta-marks)
5510     (when info
5511       ;; Add all marks lists to the list of marks lists.
5512       (while (setq type (pop types))
5513         (setq list (symbol-value
5514                     (setq symbol
5515                           (intern (format "gnus-newsgroup-%s" (car type))))))
5516
5517         (when list
5518           ;; Get rid of the entries of the articles that have the
5519           ;; default score.
5520           (when (and (eq (cdr type) 'score)
5521                      gnus-save-score
5522                      list)
5523             (let* ((arts list)
5524                    (prev (cons nil list))
5525                    (all prev))
5526               (while arts
5527                 (if (or (not (consp (car arts)))
5528                         (= (cdar arts) gnus-summary-default-score))
5529                     (setcdr prev (cdr arts))
5530                   (setq prev arts))
5531                 (setq arts (cdr arts)))
5532               (setq list (cdr all)))))
5533
5534         (when (eq (cdr type) 'seen)
5535           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5536
5537         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5538           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5539
5540         (when (and (gnus-check-backend-function
5541                     'request-set-mark gnus-newsgroup-name)
5542                    (not (gnus-article-unpropagatable-p (cdr type))))
5543           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5544                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5545                  (add (gnus-remove-from-range
5546                        (gnus-copy-sequence list) old)))
5547             (when add
5548               (push (list add 'add (list (cdr type))) delta-marks))
5549             (when del
5550               (push (list del 'del (list (cdr type))) delta-marks))))
5551
5552         (when list
5553           (push (cons (cdr type) list) newmarked)))
5554
5555       (when delta-marks
5556         (unless (gnus-check-group gnus-newsgroup-name)
5557           (error "Can't open server for %s" gnus-newsgroup-name))
5558         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5559
5560       ;; Enter these new marks into the info of the group.
5561       (if (nthcdr 3 info)
5562           (setcar (nthcdr 3 info) newmarked)
5563         ;; Add the marks lists to the end of the info.
5564         (when newmarked
5565           (setcdr (nthcdr 2 info) (list newmarked))))
5566
5567       ;; Cut off the end of the info if there's nothing else there.
5568       (let ((i 5))
5569         (while (and (> i 2)
5570                     (not (nth i info)))
5571           (when (nthcdr (decf i) info)
5572             (setcdr (nthcdr i info) nil)))))))
5573
5574 (defun gnus-set-mode-line (where)
5575   "Set the mode line of the article or summary buffers.
5576 If WHERE is `summary', the summary mode line format will be used."
5577   ;; Is this mode line one we keep updated?
5578   (when (and (memq where gnus-updated-mode-lines)
5579              (symbol-value
5580               (intern (format "gnus-%s-mode-line-format-spec" where))))
5581     (let (mode-string)
5582       (save-excursion
5583         ;; We evaluate this in the summary buffer since these
5584         ;; variables are buffer-local to that buffer.
5585         (set-buffer gnus-summary-buffer)
5586        ;; We bind all these variables that are used in the `eval' form
5587         ;; below.
5588         (let* ((mformat (symbol-value
5589                          (intern
5590                           (format "gnus-%s-mode-line-format-spec" where))))
5591                (gnus-tmp-group-name (gnus-group-decoded-name
5592                                      gnus-newsgroup-name))
5593                (gnus-tmp-article-number (or gnus-current-article 0))
5594                (gnus-tmp-unread gnus-newsgroup-unreads)
5595                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5596                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5597                (gnus-tmp-unread-and-unselected
5598                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5599                             (zerop gnus-tmp-unselected))
5600                        "")
5601                       ((zerop gnus-tmp-unselected)
5602                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5603                       (t (format "{%d(+%d) more}"
5604                                  gnus-tmp-unread-and-unticked
5605                                  gnus-tmp-unselected))))
5606                (gnus-tmp-subject
5607                 (if (and gnus-current-headers
5608                          (vectorp gnus-current-headers))
5609                     (gnus-mode-string-quote
5610                      (mail-header-subject gnus-current-headers))
5611                   ""))
5612                bufname-length max-len
5613                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5614           (setq mode-string (eval mformat))
5615           (setq bufname-length (if (string-match "%b" mode-string)
5616                                    (- (length
5617                                        (buffer-name
5618                                         (if (eq where 'summary)
5619                                             nil
5620                                           (get-buffer gnus-article-buffer))))
5621                                       2)
5622                                  0))
5623           (setq max-len (max 4 (if gnus-mode-non-string-length
5624                                    (- (window-width)
5625                                       gnus-mode-non-string-length
5626                                       bufname-length)
5627                                  (length mode-string))))
5628           ;; We might have to chop a bit of the string off...
5629           (when (> (length mode-string) max-len)
5630             (setq mode-string
5631                   (concat (truncate-string-to-width mode-string (- max-len 3))
5632                           "...")))
5633           ;; Pad the mode string a bit.
5634           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5635       ;; Update the mode line.
5636       (setq mode-line-buffer-identification
5637             (gnus-mode-line-buffer-identification (list mode-string)))
5638       (set-buffer-modified-p t))))
5639
5640 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5641   "Go through the HEADERS list and add all Xrefs to a hash table.
5642 The resulting hash table is returned, or nil if no Xrefs were found."
5643   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5644          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5645          (xref-hashtb (gnus-make-hashtable))
5646          start group entry number xrefs header)
5647     (while headers
5648       (setq header (pop headers))
5649       (when (and (setq xrefs (mail-header-xref header))
5650                  (not (memq (setq number (mail-header-number header))
5651                             unreads)))
5652         (setq start 0)
5653         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5654           (setq start (match-end 0))
5655           (setq group (if prefix
5656                           (concat prefix (substring xrefs (match-beginning 1)
5657                                                     (match-end 1)))
5658                         (substring xrefs (match-beginning 1) (match-end 1))))
5659           (setq number
5660                 (string-to-int (substring xrefs (match-beginning 2)
5661                                           (match-end 2))))
5662           (if (setq entry (gnus-gethash group xref-hashtb))
5663               (setcdr entry (cons number (cdr entry)))
5664             (gnus-sethash group (cons number nil) xref-hashtb)))))
5665     (and start xref-hashtb)))
5666
5667 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5668   "Look through all the headers and mark the Xrefs as read."
5669   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5670         name info xref-hashtb idlist method nth4)
5671     (save-excursion
5672       (set-buffer gnus-group-buffer)
5673       (when (setq xref-hashtb
5674                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5675         (mapatoms
5676          (lambda (group)
5677            (unless (string= from-newsgroup (setq name (symbol-name group)))
5678              (setq idlist (symbol-value group))
5679              ;; Dead groups are not updated.
5680              (and (prog1
5681                       (setq info (gnus-get-info name))
5682                     (when (stringp (setq nth4 (gnus-info-method info)))
5683                       (setq nth4 (gnus-server-to-method nth4))))
5684                   ;; Only do the xrefs if the group has the same
5685                   ;; select method as the group we have just read.
5686                   (or (gnus-methods-equal-p
5687                        nth4 (gnus-find-method-for-group from-newsgroup))
5688                       virtual
5689                       (equal nth4 (setq method (gnus-find-method-for-group
5690                                                 from-newsgroup)))
5691                       (and (equal (car nth4) (car method))
5692                            (equal (nth 1 nth4) (nth 1 method))))
5693                   gnus-use-cross-reference
5694                   (or (not (eq gnus-use-cross-reference t))
5695                       virtual
5696                       ;; Only do cross-references on subscribed
5697                       ;; groups, if that is what is wanted.
5698                       (<= (gnus-info-level info) gnus-level-subscribed))
5699                   (gnus-group-make-articles-read name idlist))))
5700          xref-hashtb)))))
5701
5702 (defun gnus-compute-read-articles (group articles)
5703   (let* ((entry (gnus-group-entry group))
5704          (info (nth 2 entry))
5705          (active (gnus-active group))
5706          ninfo)
5707     (when entry
5708       ;; First peel off all invalid article numbers.
5709       (when active
5710         (let ((ids articles)
5711               id first)
5712           (while (setq id (pop ids))
5713             (when (and first (> id (cdr active)))
5714               ;; We'll end up in this situation in one particular
5715               ;; obscure situation.  If you re-scan a group and get
5716               ;; a new article that is cross-posted to a different
5717               ;; group that has not been re-scanned, you might get
5718               ;; crossposted article that has a higher number than
5719               ;; Gnus believes possible.  So we re-activate this
5720               ;; group as well.  This might mean doing the
5721               ;; crossposting thingy will *increase* the number
5722               ;; of articles in some groups.  Tsk, tsk.
5723               (setq active (or (gnus-activate-group group) active)))
5724             (when (or (> id (cdr active))
5725                       (< id (car active)))
5726               (setq articles (delq id articles))))))
5727       ;; If the read list is nil, we init it.
5728       (if (and active
5729                (null (gnus-info-read info))
5730                (> (car active) 1))
5731           (setq ninfo (cons 1 (1- (car active))))
5732         (setq ninfo (gnus-info-read info)))
5733       ;; Then we add the read articles to the range.
5734       (gnus-add-to-range
5735        ninfo (setq articles (sort articles '<))))))
5736
5737 (defun gnus-group-make-articles-read (group articles)
5738   "Update the info of GROUP to say that ARTICLES are read."
5739   (let* ((num 0)
5740          (entry (gnus-group-entry group))
5741          (info (nth 2 entry))
5742          (active (gnus-active group))
5743          range)
5744     (when entry
5745       (setq range (gnus-compute-read-articles group articles))
5746       (with-current-buffer gnus-group-buffer
5747         (gnus-undo-register
5748           `(progn
5749              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5750              (gnus-info-set-read ',info ',(gnus-info-read info))
5751              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5752              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5753              (gnus-group-update-group ,group t))))
5754       ;; Add the read articles to the range.
5755       (gnus-info-set-read info range)
5756       (gnus-request-set-mark group (list (list range 'add '(read))))
5757       ;; Then we have to re-compute how many unread
5758       ;; articles there are in this group.
5759       (when active
5760         (cond
5761          ((not range)
5762           (setq num (- (1+ (cdr active)) (car active))))
5763          ((not (listp (cdr range)))
5764           (setq num (- (cdr active) (- (1+ (cdr range))
5765                                        (car range)))))
5766          (t
5767           (while range
5768             (if (numberp (car range))
5769                 (setq num (1+ num))
5770               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5771             (setq range (cdr range)))
5772           (setq num (- (cdr active) num))))
5773         ;; Update the number of unread articles.
5774         (setcar entry num)
5775         ;; Update the group buffer.
5776         (unless (gnus-ephemeral-group-p group)
5777           (gnus-group-update-group group t))))))
5778
5779 (defvar gnus-newsgroup-none-id 0)
5780
5781 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5782   (let ((cur nntp-server-buffer)
5783         (dependencies
5784          (or dependencies
5785              (with-current-buffer gnus-summary-buffer
5786                gnus-newsgroup-dependencies)))
5787         headers id end ref number
5788         (mail-parse-charset gnus-newsgroup-charset)
5789         (mail-parse-ignored-charsets
5790          (save-excursion (condition-case nil
5791                              (set-buffer gnus-summary-buffer)
5792                            (error))
5793                          gnus-newsgroup-ignored-charsets)))
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       ;; Translate all TAB characters into SPACE characters.
5797       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5799       (ietf-drums-unfold-fws)
5800       (gnus-run-hooks 'gnus-parse-headers-hook)
5801       (let ((case-fold-search t)
5802             in-reply-to header p lines chars)
5803         (goto-char (point-min))
5804         ;; Search to the beginning of the next header.  Error messages
5805         ;; do not begin with 2 or 3.
5806         (while (re-search-forward "^[23][0-9]+ " nil t)
5807           (setq id nil
5808                 ref nil)
5809           ;; This implementation of this function, with nine
5810           ;; search-forwards instead of the one re-search-forward and
5811           ;; a case (which basically was the old function) is actually
5812           ;; about twice as fast, even though it looks messier.  You
5813           ;; can't have everything, I guess.  Speed and elegance
5814           ;; doesn't always go hand in hand.
5815           (setq
5816            header
5817            (vector
5818             ;; Number.
5819             (prog1
5820                 (setq number (read cur))
5821               (end-of-line)
5822               (setq p (point))
5823               (narrow-to-region (point)
5824                                 (or (and (search-forward "\n.\n" nil t)
5825                                          (- (point) 2))
5826                                     (point))))
5827             ;; Subject.
5828             (progn
5829               (goto-char p)
5830               (if (search-forward "\nsubject:" nil t)
5831                   (funcall gnus-decode-encoded-word-function
5832                            (nnheader-header-value))
5833                 "(none)"))
5834             ;; From.
5835             (progn
5836               (goto-char p)
5837               (if (search-forward "\nfrom:" nil t)
5838                   (funcall gnus-decode-encoded-word-function
5839                            (nnheader-header-value))
5840                 "(nobody)"))
5841             ;; Date.
5842             (progn
5843               (goto-char p)
5844               (if (search-forward "\ndate:" nil t)
5845                   (nnheader-header-value) ""))
5846             ;; Message-ID.
5847             (progn
5848               (goto-char p)
5849               (setq id (if (re-search-forward
5850                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5851                            ;; We do it this way to make sure the Message-ID
5852                            ;; is (somewhat) syntactically valid.
5853                            (buffer-substring (match-beginning 1)
5854                                              (match-end 1))
5855                          ;; If there was no message-id, we just fake one
5856                          ;; to make subsequent routines simpler.
5857                          (nnheader-generate-fake-message-id number))))
5858             ;; References.
5859             (progn
5860               (goto-char p)
5861               (if (search-forward "\nreferences:" nil t)
5862                   (progn
5863                     (setq end (point))
5864                     (prog1
5865                         (nnheader-header-value)
5866                       (setq ref
5867                             (buffer-substring
5868                              (progn
5869                                (end-of-line)
5870                                (search-backward ">" end t)
5871                                (1+ (point)))
5872                              (progn
5873                                (search-backward "<" end t)
5874                                (point))))))
5875                 ;; Get the references from the in-reply-to header if there
5876                 ;; were no references and the in-reply-to header looks
5877                 ;; promising.
5878                 (if (and (search-forward "\nin-reply-to:" nil t)
5879                          (setq in-reply-to (nnheader-header-value))
5880                          (string-match "<[^>]+>" in-reply-to))
5881                     (let (ref2)
5882                       (setq ref (substring in-reply-to (match-beginning 0)
5883                                            (match-end 0)))
5884                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5885                         (setq ref2 (substring in-reply-to (match-beginning 0)
5886                                               (match-end 0)))
5887                         (when (> (length ref2) (length ref))
5888                           (setq ref ref2)))
5889                       ref)
5890                   (setq ref nil))))
5891             ;; Chars.
5892             (progn
5893               (goto-char p)
5894               (if (search-forward "\nchars: " nil t)
5895                   (if (numberp (setq chars (ignore-errors (read cur))))
5896                       chars -1)
5897                 -1))
5898             ;; Lines.
5899             (progn
5900               (goto-char p)
5901               (if (search-forward "\nlines: " nil t)
5902                   (if (numberp (setq lines (ignore-errors (read cur))))
5903                       lines -1)
5904                 -1))
5905             ;; Xref.
5906             (progn
5907               (goto-char p)
5908               (and (search-forward "\nxref:" nil t)
5909                    (nnheader-header-value)))
5910             ;; Extra.
5911             (when gnus-extra-headers
5912               (let ((extra gnus-extra-headers)
5913                     out)
5914                 (while extra
5915                   (goto-char p)
5916                   (when (search-forward
5917                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5918                     (push (cons (car extra) (nnheader-header-value))
5919                           out))
5920                   (pop extra))
5921                 out))))
5922           (when (equal id ref)
5923             (setq ref nil))
5924
5925           (when gnus-alter-header-function
5926             (funcall gnus-alter-header-function header)
5927             (setq id (mail-header-id header)
5928                   ref (gnus-parent-id (mail-header-references header))))
5929
5930           (when (setq header
5931                       (gnus-dependencies-add-header
5932                        header dependencies force-new))
5933             (push header headers))
5934           (goto-char (point-max))
5935           (widen))
5936         (nreverse headers)))))
5937
5938 ;; Goes through the xover lines and returns a list of vectors
5939 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5940                                                   force-new dependencies
5941                                                   group also-fetch-heads)
5942   "Parse the news overview data in the server buffer.
5943 Return a list of headers that match SEQUENCE (see
5944 `nntp-retrieve-headers')."
5945   ;; Get the Xref when the users reads the articles since most/some
5946   ;; NNTP servers do not include Xrefs when using XOVER.
5947   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5948   (let ((mail-parse-charset gnus-newsgroup-charset)
5949         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5950         (cur nntp-server-buffer)
5951         (dependencies (or dependencies gnus-newsgroup-dependencies))
5952         (allp (cond
5953                ((eq gnus-read-all-available-headers t)
5954                 t)
5955                ((stringp gnus-read-all-available-headers)
5956                 (string-match gnus-read-all-available-headers group))
5957                (t
5958                 nil)))
5959         number headers header)
5960     (save-excursion
5961       (set-buffer nntp-server-buffer)
5962       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5963       ;; Allow the user to mangle the headers before parsing them.
5964       (gnus-run-hooks 'gnus-parse-headers-hook)
5965       (goto-char (point-min))
5966       (gnus-parse-without-error
5967         (while (and (or sequence allp)
5968                     (not (eobp)))
5969           (setq number (read cur))
5970           (when (not allp)
5971             (while (and sequence
5972                         (< (car sequence) number))
5973               (setq sequence (cdr sequence))))
5974           (when (and (or allp
5975                          (and sequence
5976                               (eq number (car sequence))))
5977                      (progn
5978                        (setq sequence (cdr sequence))
5979                        (setq header (inline
5980                                       (gnus-nov-parse-line
5981                                        number dependencies force-new)))))
5982             (push header headers))
5983           (forward-line 1)))
5984       ;; A common bug in inn is that if you have posted an article and
5985       ;; then retrieves the active file, it will answer correctly --
5986       ;; the new article is included.  However, a NOV entry for the
5987       ;; article may not have been generated yet, so this may fail.
5988       ;; We work around this problem by retrieving the last few
5989       ;; headers using HEAD.
5990       (if (or (not also-fetch-heads)
5991               (not sequence))
5992           ;; We (probably) got all the headers.
5993           (nreverse headers)
5994         (let ((gnus-nov-is-evil t))
5995           (nconc
5996            (nreverse headers)
5997            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5998              (gnus-get-newsgroup-headers))))))))
5999
6000 (defun gnus-article-get-xrefs ()
6001   "Fill in the Xref value in `gnus-current-headers', if necessary.
6002 This is meant to be called in `gnus-article-internal-prepare-hook'."
6003   (let ((headers (with-current-buffer gnus-summary-buffer
6004                    gnus-current-headers)))
6005     (or (not gnus-use-cross-reference)
6006         (not headers)
6007         (and (mail-header-xref headers)
6008              (not (string= (mail-header-xref headers) "")))
6009         (let ((case-fold-search t)
6010               xref)
6011           (save-restriction
6012             (nnheader-narrow-to-headers)
6013             (goto-char (point-min))
6014             (when (or (and (not (eobp))
6015                            (eq (downcase (char-after)) ?x)
6016                            (looking-at "Xref:"))
6017                       (search-forward "\nXref:" nil t))
6018               (goto-char (1+ (match-end 0)))
6019               (setq xref (buffer-substring (point) (point-at-eol)))
6020               (mail-header-set-xref headers xref)))))))
6021
6022 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6023   "Find article ID and insert the summary line for that article.
6024 OLD-HEADER can either be a header or a line number to insert
6025 the subject line on."
6026   (let* ((line (and (numberp old-header) old-header))
6027          (old-header (and (vectorp old-header) old-header))
6028          (header (cond ((and old-header use-old-header)
6029                         old-header)
6030                        ((and (numberp id)
6031                              (gnus-number-to-header id))
6032                         (gnus-number-to-header id))
6033                        (t
6034                         (gnus-read-header id))))
6035          (number (and (numberp id) id))
6036          d)
6037     (when header
6038       ;; Rebuild the thread that this article is part of and go to the
6039       ;; article we have fetched.
6040       (when (and (not gnus-show-threads)
6041                  old-header)
6042         (when (and number
6043                    (setq d (gnus-data-find (mail-header-number old-header))))
6044           (goto-char (gnus-data-pos d))
6045           (gnus-data-remove
6046            number
6047            (- (point-at-bol)
6048               (prog1
6049                   (1+ (point-at-eol))
6050                 (gnus-delete-line))))))
6051       (when old-header
6052         (mail-header-set-number header (mail-header-number old-header)))
6053       (setq gnus-newsgroup-sparse
6054             (delq (setq number (mail-header-number header))
6055                   gnus-newsgroup-sparse))
6056       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6057       (push number gnus-newsgroup-limit)
6058       (gnus-rebuild-thread (mail-header-id header) line)
6059       (gnus-summary-goto-subject number nil t))
6060     (when (and (numberp number)
6061                (> number 0))
6062       ;; We have to update the boundaries even if we can't fetch the
6063       ;; article if ID is a number -- so that the next `P' or `N'
6064       ;; command will fetch the previous (or next) article even
6065       ;; if the one we tried to fetch this time has been canceled.
6066       (when (> number gnus-newsgroup-end)
6067         (setq gnus-newsgroup-end number))
6068       (when (< number gnus-newsgroup-begin)
6069         (setq gnus-newsgroup-begin number))
6070       (setq gnus-newsgroup-unselected
6071             (delq number gnus-newsgroup-unselected)))
6072     ;; Report back a success?
6073     (and header (mail-header-number header))))
6074
6075 ;;; Process/prefix in the summary buffer
6076
6077 (defun gnus-summary-work-articles (n)
6078   "Return a list of articles to be worked upon.
6079 The prefix argument, the list of process marked articles, and the
6080 current article will be taken into consideration."
6081   (save-excursion
6082     (set-buffer gnus-summary-buffer)
6083     (cond
6084      (n
6085       ;; A numerical prefix has been given.
6086       (setq n (prefix-numeric-value n))
6087       (let ((backward (< n 0))
6088             (n (abs (prefix-numeric-value n)))
6089             articles article)
6090         (save-excursion
6091           (while
6092               (and (> n 0)
6093                    (push (setq article (gnus-summary-article-number))
6094                          articles)
6095                    (if backward
6096                        (gnus-summary-find-prev nil article)
6097                      (gnus-summary-find-next nil article)))
6098             (decf n)))
6099         (nreverse articles)))
6100      ((and (gnus-region-active-p) (mark))
6101       (message "region active")
6102       ;; Work on the region between point and mark.
6103       (let ((max (max (point) (mark)))
6104             articles article)
6105         (save-excursion
6106           (goto-char (min (min (point) (mark))))
6107           (while
6108               (and
6109                (push (setq article (gnus-summary-article-number)) articles)
6110                (gnus-summary-find-next nil article)
6111                (< (point) max)))
6112           (nreverse articles))))
6113      (gnus-newsgroup-processable
6114       ;; There are process-marked articles present.
6115       ;; Save current state.
6116       (gnus-summary-save-process-mark)
6117       ;; Return the list.
6118       (reverse gnus-newsgroup-processable))
6119      (t
6120       ;; Just return the current article.
6121       (list (gnus-summary-article-number))))))
6122
6123 (defmacro gnus-summary-iterate (arg &rest forms)
6124   "Iterate over the process/prefixed articles and do FORMS.
6125 ARG is the interactive prefix given to the command.  FORMS will be
6126 executed with point over the summary line of the articles."
6127   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6128     `(let ((,articles (gnus-summary-work-articles ,arg)))
6129        (while ,articles
6130          (gnus-summary-goto-subject (car ,articles))
6131          ,@forms
6132          (pop ,articles)))))
6133
6134 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6135 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6136
6137 (defun gnus-summary-save-process-mark ()
6138   "Push the current set of process marked articles on the stack."
6139   (interactive)
6140   (push (copy-sequence gnus-newsgroup-processable)
6141         gnus-newsgroup-process-stack))
6142
6143 (defun gnus-summary-kill-process-mark ()
6144   "Push the current set of process marked articles on the stack and unmark."
6145   (interactive)
6146   (gnus-summary-save-process-mark)
6147   (gnus-summary-unmark-all-processable))
6148
6149 (defun gnus-summary-yank-process-mark ()
6150   "Pop the last process mark state off the stack and restore it."
6151   (interactive)
6152   (unless gnus-newsgroup-process-stack
6153     (error "Empty mark stack"))
6154   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6155
6156 (defun gnus-summary-process-mark-set (set)
6157   "Make SET into the current process marked articles."
6158   (gnus-summary-unmark-all-processable)
6159   (while set
6160     (gnus-summary-set-process-mark (pop set))))
6161
6162 ;;; Searching and stuff
6163
6164 (defun gnus-summary-search-group (&optional backward use-level)
6165   "Search for next unread newsgroup.
6166 If optional argument BACKWARD is non-nil, search backward instead."
6167   (save-excursion
6168     (set-buffer gnus-group-buffer)
6169     (when (gnus-group-search-forward
6170            backward nil (if use-level (gnus-group-group-level) nil))
6171       (gnus-group-group-name))))
6172
6173 (defun gnus-summary-best-group (&optional exclude-group)
6174   "Find the name of the best unread group.
6175 If EXCLUDE-GROUP, do not go to this group."
6176   (with-current-buffer gnus-group-buffer
6177     (save-excursion
6178       (gnus-group-best-unread-group exclude-group))))
6179
6180 (defun gnus-summary-find-next (&optional unread article backward)
6181   (if backward
6182       (gnus-summary-find-prev unread article)
6183     (let* ((dummy (gnus-summary-article-intangible-p))
6184            (article (or article (gnus-summary-article-number)))
6185            (data (gnus-data-find-list article))
6186            result)
6187       (when (and (not dummy)
6188                  (or (not gnus-summary-check-current)
6189                      (not unread)
6190                      (not (gnus-data-unread-p (car data)))))
6191         (setq data (cdr data)))
6192       (when (setq result
6193                   (if unread
6194                       (progn
6195                         (while data
6196                           (unless (memq (gnus-data-number (car data))
6197                                         (cond
6198                                          ((eq gnus-auto-goto-ignores
6199                                               'always-undownloaded)
6200                                           gnus-newsgroup-undownloaded)
6201                                          (gnus-plugged
6202                                           nil)
6203                                          ((eq gnus-auto-goto-ignores
6204                                               'unfetched)
6205                                           gnus-newsgroup-unfetched)
6206                                          ((eq gnus-auto-goto-ignores
6207                                               'undownloaded)
6208                                           gnus-newsgroup-undownloaded)))
6209                             (when (gnus-data-unread-p (car data))
6210                               (setq result (car data)
6211                                     data nil)))
6212                           (setq data (cdr data)))
6213                         result)
6214                     (car data)))
6215         (goto-char (gnus-data-pos result))
6216         (gnus-data-number result)))))
6217
6218 (defun gnus-summary-find-prev (&optional unread article)
6219   (let* ((eobp (eobp))
6220          (article (or article (gnus-summary-article-number)))
6221          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6222          result)
6223     (when (and (not eobp)
6224                (or (not gnus-summary-check-current)
6225                    (not unread)
6226                    (not (gnus-data-unread-p (car data)))))
6227       (setq data (cdr data)))
6228     (when (setq result
6229                 (if unread
6230                     (progn
6231                       (while data
6232                         (unless (memq (gnus-data-number (car data))
6233                                       (cond
6234                                        ((eq gnus-auto-goto-ignores
6235                                             'always-undownloaded)
6236                                         gnus-newsgroup-undownloaded)
6237                                        (gnus-plugged
6238                                         nil)
6239                                        ((eq gnus-auto-goto-ignores
6240                                             'unfetched)
6241                                         gnus-newsgroup-unfetched)
6242                                        ((eq gnus-auto-goto-ignores
6243                                             'undownloaded)
6244                                         gnus-newsgroup-undownloaded)))
6245                           (when (gnus-data-unread-p (car data))
6246                             (setq result (car data)
6247                                   data nil)))
6248                         (setq data (cdr data)))
6249                       result)
6250                   (car data)))
6251       (goto-char (gnus-data-pos result))
6252       (gnus-data-number result))))
6253
6254 (defun gnus-summary-find-subject (subject &optional unread backward article)
6255   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6256          (article (or article (gnus-summary-article-number)))
6257          (articles (gnus-data-list backward))
6258          (arts (gnus-data-find-list article articles))
6259          result)
6260     (when (or (not gnus-summary-check-current)
6261               (not unread)
6262               (not (gnus-data-unread-p (car arts))))
6263       (setq arts (cdr arts)))
6264     (while arts
6265       (and (or (not unread)
6266                (gnus-data-unread-p (car arts)))
6267            (vectorp (gnus-data-header (car arts)))
6268            (gnus-subject-equal
6269             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6270            (setq result (car arts)
6271                  arts nil))
6272       (setq arts (cdr arts)))
6273     (and result
6274          (goto-char (gnus-data-pos result))
6275          (gnus-data-number result))))
6276
6277 (defun gnus-summary-search-forward (&optional unread subject backward)
6278   "Search forward for an article.
6279 If UNREAD, look for unread articles.  If SUBJECT, look for
6280 articles with that subject.  If BACKWARD, search backward instead."
6281   (cond (subject (gnus-summary-find-subject subject unread backward))
6282         (backward (gnus-summary-find-prev unread))
6283         (t (gnus-summary-find-next unread))))
6284
6285 (defun gnus-recenter (&optional n)
6286   "Center point in window and redisplay frame.
6287 Also do horizontal recentering."
6288   (interactive "P")
6289   (when (and gnus-auto-center-summary
6290              (not (eq gnus-auto-center-summary 'vertical)))
6291     (gnus-horizontal-recenter))
6292   (recenter n))
6293
6294 (defun gnus-summary-recenter ()
6295   "Center point in the summary window.
6296 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6297 displayed, no centering will be performed."
6298   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6299   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6300   (interactive)
6301   ;; The user has to want it.
6302   (when gnus-auto-center-summary
6303     (let* ((top (cond ((< (window-height) 4) 0)
6304                       ((< (window-height) 7) 1)
6305                       (t (if (numberp gnus-auto-center-summary)
6306                              gnus-auto-center-summary
6307                            (/ (1- (window-height)) 2)))))
6308            (height (1- (window-height)))
6309            (bottom (save-excursion (goto-char (point-max))
6310                                    (forward-line (- height))
6311                                    (point)))
6312            (window (get-buffer-window (current-buffer))))
6313       (when (get-buffer-window gnus-article-buffer)
6314         ;; Only do recentering when the article buffer is displayed,
6315         ;; Set the window start to either `bottom', which is the biggest
6316         ;; possible valid number, or the second line from the top,
6317         ;; whichever is the least.
6318         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6319           (if (> bottom top-pos)
6320               ;; Keep the second line from the top visible
6321               (set-window-start window top-pos t)
6322             ;; Try to keep the bottom line visible; if it's partially
6323             ;; obscured, either scroll one more line to make it fully
6324             ;; visible, or revert to using TOP-POS.
6325             (save-excursion
6326               (goto-char (point-max))
6327               (forward-line -1)
6328               (let ((last-line-start (point)))
6329                 (goto-char bottom)
6330                 (set-window-start window (point) t)
6331                 (when (not (pos-visible-in-window-p last-line-start window))
6332                   (forward-line 1)
6333                   (set-window-start window (min (point) top-pos) t)))))))
6334       ;; Do horizontal recentering while we're at it.
6335       (when (and (get-buffer-window (current-buffer) t)
6336                  (not (eq gnus-auto-center-summary 'vertical)))
6337         (let ((selected (selected-window)))
6338           (select-window (get-buffer-window (current-buffer) t))
6339           (gnus-summary-position-point)
6340           (gnus-horizontal-recenter)
6341           (select-window selected))))))
6342
6343 (defun gnus-summary-jump-to-group (newsgroup)
6344   "Move point to NEWSGROUP in group mode buffer."
6345   ;; Keep update point of group mode buffer if visible.
6346   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6347       (save-window-excursion
6348         ;; Take care of tree window mode.
6349         (when (get-buffer-window gnus-group-buffer)
6350           (pop-to-buffer gnus-group-buffer))
6351         (gnus-group-jump-to-group newsgroup))
6352     (save-excursion
6353       ;; Take care of tree window mode.
6354       (if (get-buffer-window gnus-group-buffer)
6355           (pop-to-buffer gnus-group-buffer)
6356         (set-buffer gnus-group-buffer))
6357       (gnus-group-jump-to-group newsgroup))))
6358
6359 ;; This function returns a list of article numbers based on the
6360 ;; difference between the ranges of read articles in this group and
6361 ;; the range of active articles.
6362 (defun gnus-list-of-unread-articles (group)
6363   (let* ((read (gnus-info-read (gnus-get-info group)))
6364          (active (or (gnus-active group) (gnus-activate-group group)))
6365          (last (cdr active))
6366          first nlast unread)
6367     ;; If none are read, then all are unread.
6368     (if (not read)
6369         (setq first (car active))
6370       ;; If the range of read articles is a single range, then the
6371       ;; first unread article is the article after the last read
6372       ;; article.  Sounds logical, doesn't it?
6373       (if (and (not (listp (cdr read)))
6374                (or (< (car read) (car active))
6375                    (progn (setq read (list read))
6376                           nil)))
6377           (setq first (max (car active) (1+ (cdr read))))
6378         ;; `read' is a list of ranges.
6379         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6380                                   (caar read)))
6381                   1)
6382           (setq first (car active)))
6383         (while read
6384           (when first
6385             (while (< first nlast)
6386               (setq unread (cons first unread)
6387                     first (1+ first))))
6388           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6389           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6390           (setq read (cdr read)))))
6391     ;; And add the last unread articles.
6392     (while (<= first last)
6393       (setq unread (cons first unread)
6394             first (1+ first)))
6395     ;; Return the list of unread articles.
6396     (delq 0 (nreverse unread))))
6397
6398 (defun gnus-list-of-read-articles (group)
6399   "Return a list of unread, unticked and non-dormant articles."
6400   (let* ((info (gnus-get-info group))
6401          (marked (gnus-info-marks info))
6402          (active (gnus-active group)))
6403     (and info active
6404          (gnus-list-range-difference
6405           (gnus-list-range-difference
6406            (gnus-sorted-complement
6407             (gnus-uncompress-range active)
6408             (gnus-list-of-unread-articles group))
6409            (cdr (assq 'dormant marked)))
6410           (cdr (assq 'tick marked))))))
6411
6412 ;; This function returns a sequence of article numbers based on the
6413 ;; difference between the ranges of read articles in this group and
6414 ;; the range of active articles.
6415 (defun gnus-sequence-of-unread-articles (group)
6416   (let* ((read (gnus-info-read (gnus-get-info group)))
6417          (active (or (gnus-active group) (gnus-activate-group group)))
6418          (last (cdr active))
6419          first nlast unread)
6420     ;; If none are read, then all are unread.
6421     (if (not read)
6422         (setq first (car active))
6423       ;; If the range of read articles is a single range, then the
6424       ;; first unread article is the article after the last read
6425       ;; article.  Sounds logical, doesn't it?
6426       (if (and (not (listp (cdr read)))
6427                (or (< (car read) (car active))
6428                    (progn (setq read (list read))
6429                           nil)))
6430           (setq first (max (car active) (1+ (cdr read))))
6431         ;; `read' is a list of ranges.
6432         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6433                                   (caar read)))
6434                   1)
6435           (setq first (car active)))
6436         (while read
6437           (when first
6438             (push (cons first nlast) unread))
6439           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6440           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6441           (setq read (cdr read)))))
6442     ;; And add the last unread articles.
6443     (cond ((< first last)
6444            (push (cons first last) unread))
6445           ((= first last)
6446            (push first unread)))
6447     ;; Return the sequence of unread articles.
6448     (delq 0 (nreverse unread))))
6449
6450 ;; Various summary commands
6451
6452 (defun gnus-summary-select-article-buffer ()
6453   "Reconfigure windows to show article buffer."
6454   (interactive)
6455   (if (not (gnus-buffer-live-p gnus-article-buffer))
6456       (error "There is no article buffer for this summary buffer")
6457     (gnus-configure-windows 'article)
6458     (select-window (get-buffer-window gnus-article-buffer))))
6459
6460 (defun gnus-summary-universal-argument (arg)
6461   "Perform any operation on all articles that are process/prefixed."
6462   (interactive "P")
6463   (let ((articles (gnus-summary-work-articles arg))
6464         func article)
6465     (if (eq
6466          (setq
6467           func
6468           (key-binding
6469            (read-key-sequence
6470             (substitute-command-keys
6471              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6472          'undefined)
6473         (gnus-error 1 "Undefined key")
6474       (save-excursion
6475         (while articles
6476           (gnus-summary-goto-subject (setq article (pop articles)))
6477           (let (gnus-newsgroup-processable)
6478             (command-execute func))
6479           (gnus-summary-remove-process-mark article)))))
6480   (gnus-summary-position-point))
6481
6482 (defun gnus-summary-toggle-truncation (&optional arg)
6483   "Toggle truncation of summary lines.
6484 With ARG, turn line truncation on if ARG is positive."
6485   (interactive "P")
6486   (setq truncate-lines
6487         (if (null arg) (not truncate-lines)
6488           (> (prefix-numeric-value arg) 0)))
6489   (redraw-display))
6490
6491 (defun gnus-summary-find-for-reselect ()
6492   "Return the number of an article to stay on across a reselect.
6493 The current article is considered, then following articles, then previous
6494 articles.  An article is sought which is not cancelled and isn't a temporary
6495 insertion from another group.  If there's no such then return a dummy 0."
6496   (let (found)
6497     (dolist (rev '(nil t))
6498       (unless found      ; don't demand the reverse list if we don't need it
6499         (let ((data (gnus-data-find-list
6500                      (gnus-summary-article-number) (gnus-data-list rev))))
6501           (while (and data (not found))
6502             (if (and (< 0 (gnus-data-number (car data)))
6503                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6504                 (setq found (gnus-data-number (car data))))
6505             (setq data (cdr data))))))
6506     (or found 0)))
6507
6508 (defun gnus-summary-reselect-current-group (&optional all rescan)
6509   "Exit and then reselect the current newsgroup.
6510 The prefix argument ALL means to select all articles."
6511   (interactive "P")
6512   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6513     (error "Ephemeral groups can't be reselected"))
6514   (let ((current-subject (gnus-summary-find-for-reselect))
6515         (group gnus-newsgroup-name))
6516     (setq gnus-newsgroup-begin nil)
6517     (gnus-summary-exit nil 'leave-hidden)
6518     ;; We have to adjust the point of group mode buffer because
6519     ;; point was moved to the next unread newsgroup by exiting.
6520     (gnus-summary-jump-to-group group)
6521     (when rescan
6522       (save-excursion
6523         (gnus-group-get-new-news-this-group 1)))
6524     (gnus-group-read-group all t)
6525     (gnus-summary-goto-subject current-subject nil t)))
6526
6527 (defun gnus-summary-rescan-group (&optional all)
6528   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6529   (interactive "P")
6530   (gnus-summary-reselect-current-group all t))
6531
6532 (defun gnus-summary-update-info (&optional non-destructive)
6533   (save-excursion
6534     (let ((group gnus-newsgroup-name))
6535       (when group
6536         (when gnus-newsgroup-kill-headers
6537           (setq gnus-newsgroup-killed
6538                 (gnus-compress-sequence
6539                  (gnus-sorted-union
6540                   (gnus-list-range-intersection
6541                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6542                   gnus-newsgroup-unreads)
6543                  t)))
6544         (unless (listp (cdr gnus-newsgroup-killed))
6545           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6546         (let ((headers gnus-newsgroup-headers))
6547           ;; Set the new ranges of read articles.
6548           (with-current-buffer gnus-group-buffer
6549             (gnus-undo-force-boundary))
6550           (gnus-update-read-articles
6551            group (gnus-sorted-union
6552                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6553           ;; Set the current article marks.
6554           (let ((gnus-newsgroup-scored
6555                  (if (and (not gnus-save-score)
6556                           (not non-destructive))
6557                      nil
6558                    gnus-newsgroup-scored)))
6559             (save-excursion
6560               (gnus-update-marks)))
6561           ;; Do the cross-ref thing.
6562           (when gnus-use-cross-reference
6563             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6564           ;; Do not switch windows but change the buffer to work.
6565           (set-buffer gnus-group-buffer)
6566           (unless (gnus-ephemeral-group-p group)
6567             (gnus-group-update-group group)))))))
6568
6569 (defun gnus-summary-save-newsrc (&optional force)
6570   "Save the current number of read/marked articles in the dribble buffer.
6571 The dribble buffer will then be saved.
6572 If FORCE (the prefix), also save the .newsrc file(s)."
6573   (interactive "P")
6574   (gnus-summary-update-info t)
6575   (if force
6576       (gnus-save-newsrc-file)
6577     (gnus-dribble-save)))
6578
6579 (defun gnus-summary-exit (&optional temporary leave-hidden)
6580   "Exit reading current newsgroup, and then return to group selection mode.
6581 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6582   (interactive)
6583   (gnus-set-global-variables)
6584   (when (gnus-buffer-live-p gnus-article-buffer)
6585     (save-excursion
6586       (set-buffer gnus-article-buffer)
6587       (mm-destroy-parts gnus-article-mime-handles)
6588       ;; Set it to nil for safety reason.
6589       (setq gnus-article-mime-handle-alist nil)
6590       (setq gnus-article-mime-handles nil)))
6591   (gnus-kill-save-kill-buffer)
6592   (gnus-async-halt-prefetch)
6593   (let* ((group gnus-newsgroup-name)
6594          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6595          (gnus-group-is-exiting-p t)
6596          (mode major-mode)
6597          (group-point nil)
6598          (buf (current-buffer)))
6599     (unless quit-config
6600       ;; Do adaptive scoring, and possibly save score files.
6601       (when gnus-newsgroup-adaptive
6602         (gnus-score-adaptive))
6603       (when gnus-use-scoring
6604         (gnus-score-save)))
6605     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6606     ;; If we have several article buffers, we kill them at exit.
6607     (unless gnus-single-article-buffer
6608       (gnus-kill-buffer gnus-original-article-buffer)
6609       (setq gnus-article-current nil))
6610     (when gnus-use-cache
6611       (gnus-cache-possibly-remove-articles)
6612       (gnus-cache-save-buffers))
6613     (gnus-async-prefetch-remove-group group)
6614     (when gnus-suppress-duplicates
6615       (gnus-dup-enter-articles))
6616     (when gnus-use-trees
6617       (gnus-tree-close group))
6618     (when gnus-use-cache
6619       (gnus-cache-write-active))
6620     ;; Remove entries for this group.
6621     (nnmail-purge-split-history (gnus-group-real-name group))
6622     ;; Make all changes in this group permanent.
6623     (unless quit-config
6624       (gnus-run-hooks 'gnus-exit-group-hook)
6625       (gnus-summary-update-info))
6626     (gnus-close-group group)
6627     ;; Make sure where we were, and go to next newsgroup.
6628     (set-buffer gnus-group-buffer)
6629     (unless quit-config
6630       (gnus-group-jump-to-group group))
6631     (gnus-run-hooks 'gnus-summary-exit-hook)
6632     (unless (or quit-config
6633                 ;; If this group has disappeared from the summary
6634                 ;; buffer, don't skip forwards.
6635                 (not (string= group (gnus-group-group-name))))
6636       (gnus-group-next-unread-group 1))
6637     (setq group-point (point))
6638     (if temporary
6639         nil                             ;Nothing to do.
6640       ;; If we have several article buffers, we kill them at exit.
6641       (unless gnus-single-article-buffer
6642         (gnus-kill-buffer gnus-article-buffer)
6643         (gnus-kill-buffer gnus-original-article-buffer)
6644         (setq gnus-article-current nil))
6645       (set-buffer buf)
6646       (if (not gnus-kill-summary-on-exit)
6647           (progn
6648             (gnus-deaden-summary)
6649             (setq mode nil))
6650         ;; We set all buffer-local variables to nil.  It is unclear why
6651         ;; this is needed, but if we don't, buffer-local variables are
6652         ;; not garbage-collected, it seems.  This would the lead to en
6653         ;; ever-growing Emacs.
6654         (gnus-summary-clear-local-variables)
6655         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6656           (gnus-summary-clear-local-variables))
6657         (when (get-buffer gnus-article-buffer)
6658           (bury-buffer gnus-article-buffer))
6659         ;; We clear the global counterparts of the buffer-local
6660         ;; variables as well, just to be on the safe side.
6661         (set-buffer gnus-group-buffer)
6662         (gnus-summary-clear-local-variables)
6663         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6664           (gnus-summary-clear-local-variables))
6665         ;; Return to group mode buffer.
6666         (when (eq mode 'gnus-summary-mode)
6667           (gnus-kill-buffer buf)))
6668       (setq gnus-current-select-method gnus-select-method)
6669       (if leave-hidden
6670           (set-buffer gnus-group-buffer)
6671         (pop-to-buffer gnus-group-buffer))
6672       (if (not quit-config)
6673           (progn
6674             (goto-char group-point)
6675             (unless leave-hidden
6676               (gnus-configure-windows 'group 'force)))
6677         (gnus-handle-ephemeral-exit quit-config))
6678       ;; Clear the current group name.
6679       (unless quit-config
6680         (setq gnus-newsgroup-name nil)))))
6681
6682 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6683 (defun gnus-summary-exit-no-update (&optional no-questions)
6684   "Quit reading current newsgroup without updating read article info."
6685   (interactive)
6686   (let* ((group gnus-newsgroup-name)
6687          (gnus-group-is-exiting-p t)
6688          (gnus-group-is-exiting-without-update-p t)
6689          (quit-config (gnus-group-quit-config group)))
6690     (when (or no-questions
6691               gnus-expert-user
6692               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6693       (gnus-async-halt-prefetch)
6694       (run-hooks 'gnus-summary-prepare-exit-hook)
6695       (when (gnus-buffer-live-p gnus-article-buffer)
6696         (save-excursion
6697           (set-buffer gnus-article-buffer)
6698           (mm-destroy-parts gnus-article-mime-handles)
6699           ;; Set it to nil for safety reason.
6700           (setq gnus-article-mime-handle-alist nil)
6701           (setq gnus-article-mime-handles nil)))
6702       ;; If we have several article buffers, we kill them at exit.
6703       (unless gnus-single-article-buffer
6704         (gnus-kill-buffer gnus-article-buffer)
6705         (gnus-kill-buffer gnus-original-article-buffer)
6706         (setq gnus-article-current nil))
6707       (if (not gnus-kill-summary-on-exit)
6708           (gnus-deaden-summary)
6709         (gnus-close-group group)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (set-buffer gnus-group-buffer)
6714         (gnus-summary-clear-local-variables)
6715         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6716           (gnus-summary-clear-local-variables))
6717         (gnus-kill-buffer gnus-summary-buffer))
6718       (unless gnus-single-article-buffer
6719         (setq gnus-article-current nil))
6720       (when gnus-use-trees
6721         (gnus-tree-close group))
6722       (gnus-async-prefetch-remove-group group)
6723       (when (get-buffer gnus-article-buffer)
6724         (bury-buffer gnus-article-buffer))
6725       ;; Return to the group buffer.
6726       (gnus-configure-windows 'group 'force)
6727       ;; Clear the current group name.
6728       (setq gnus-newsgroup-name nil)
6729       (unless (gnus-ephemeral-group-p group)
6730         (gnus-group-update-group group))
6731       (when (equal (gnus-group-group-name) group)
6732         (gnus-group-next-unread-group 1))
6733       (when quit-config
6734         (gnus-handle-ephemeral-exit quit-config)))))
6735
6736 (defun gnus-handle-ephemeral-exit (quit-config)
6737   "Handle movement when leaving an ephemeral group.
6738 The state which existed when entering the ephemeral is reset."
6739   (if (not (buffer-name (car quit-config)))
6740       (gnus-configure-windows 'group 'force)
6741     (set-buffer (car quit-config))
6742     (cond ((eq major-mode 'gnus-summary-mode)
6743            (gnus-set-global-variables))
6744           ((eq major-mode 'gnus-article-mode)
6745            (save-excursion
6746              ;; The `gnus-summary-buffer' variable may point
6747              ;; to the old summary buffer when using a single
6748              ;; article buffer.
6749              (unless (gnus-buffer-live-p gnus-summary-buffer)
6750                (set-buffer gnus-group-buffer))
6751              (set-buffer gnus-summary-buffer)
6752              (gnus-set-global-variables))))
6753     (if (or (eq (cdr quit-config) 'article)
6754             (eq (cdr quit-config) 'pick))
6755         (progn
6756           ;; The current article may be from the ephemeral group
6757           ;; thus it is best that we reload this article
6758           ;;
6759           ;; If we're exiting from a large digest, this can be
6760           ;; extremely slow.  So, it's better not to reload it. -- jh.
6761           ;;(gnus-summary-show-article)
6762           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6763               (gnus-configure-windows 'pick 'force)
6764             (gnus-configure-windows (cdr quit-config) 'force)))
6765       (gnus-configure-windows (cdr quit-config) 'force))
6766     (when (eq major-mode 'gnus-summary-mode)
6767       (gnus-summary-next-subject 1 nil t)
6768       (gnus-summary-recenter)
6769       (gnus-summary-position-point))))
6770
6771 ;;; Dead summaries.
6772
6773 (defvar gnus-dead-summary-mode-map nil)
6774
6775 (unless gnus-dead-summary-mode-map
6776   (setq gnus-dead-summary-mode-map (make-keymap))
6777   (suppress-keymap gnus-dead-summary-mode-map)
6778   (substitute-key-definition
6779    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6780   (dolist (key '("\C-d" "\r" "\177" [delete]))
6781     (define-key gnus-dead-summary-mode-map
6782       key 'gnus-summary-wake-up-the-dead))
6783   (dolist (key '("q" "Q"))
6784     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6785
6786 (defvar gnus-dead-summary-mode nil
6787   "Minor mode for Gnus summary buffers.")
6788
6789 (defun gnus-dead-summary-mode (&optional arg)
6790   "Minor mode for Gnus summary buffers."
6791   (interactive "P")
6792   (when (eq major-mode 'gnus-summary-mode)
6793     (make-local-variable 'gnus-dead-summary-mode)
6794     (setq gnus-dead-summary-mode
6795           (if (null arg) (not gnus-dead-summary-mode)
6796             (> (prefix-numeric-value arg) 0)))
6797     (when gnus-dead-summary-mode
6798       (add-minor-mode
6799        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6800
6801 (defun gnus-deaden-summary ()
6802   "Make the current summary buffer into a dead summary buffer."
6803   ;; Kill any previous dead summary buffer.
6804   (when (and gnus-dead-summary
6805              (buffer-name gnus-dead-summary))
6806     (with-current-buffer gnus-dead-summary
6807       (when gnus-dead-summary-mode
6808         (kill-buffer (current-buffer)))))
6809   ;; Make this the current dead summary.
6810   (setq gnus-dead-summary (current-buffer))
6811   (gnus-dead-summary-mode 1)
6812   (let ((name (buffer-name)))
6813     (when (string-match "Summary" name)
6814       (rename-buffer
6815        (concat (substring name 0 (match-beginning 0)) "Dead "
6816                (substring name (match-beginning 0)))
6817        t)
6818       (bury-buffer))))
6819
6820 (defun gnus-kill-or-deaden-summary (buffer)
6821   "Kill or deaden the summary BUFFER."
6822   (save-excursion
6823     (when (and (buffer-name buffer)
6824                (not gnus-single-article-buffer))
6825       (with-current-buffer buffer
6826         (gnus-kill-buffer gnus-article-buffer)
6827         (gnus-kill-buffer gnus-original-article-buffer)))
6828     (cond
6829      ;; Kill the buffer.
6830      (gnus-kill-summary-on-exit
6831       (when (and gnus-use-trees
6832                  (gnus-buffer-exists-p buffer))
6833         (save-excursion
6834           (set-buffer buffer)
6835           (gnus-tree-close gnus-newsgroup-name)))
6836       (gnus-kill-buffer buffer))
6837      ;; Deaden the buffer.
6838      ((gnus-buffer-exists-p buffer)
6839       (save-excursion
6840         (set-buffer buffer)
6841         (gnus-deaden-summary))))))
6842
6843 (defun gnus-summary-wake-up-the-dead (&rest args)
6844   "Wake up the dead summary buffer."
6845   (interactive)
6846   (gnus-dead-summary-mode -1)
6847   (let ((name (buffer-name)))
6848     (when (string-match "Dead " name)
6849       (rename-buffer
6850        (concat (substring name 0 (match-beginning 0))
6851                (substring name (match-end 0)))
6852        t)))
6853   (gnus-message 3 "This dead summary is now alive again"))
6854
6855 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6856 (defun gnus-summary-fetch-faq (&optional faq-dir)
6857   "Fetch the FAQ for the current group.
6858 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6859 in."
6860   (interactive
6861    (list
6862     (when current-prefix-arg
6863       (completing-read
6864        "FAQ dir: " (and (listp gnus-group-faq-directory)
6865                         (mapcar 'list
6866                                 gnus-group-faq-directory))))))
6867   (let (gnus-faq-buffer)
6868     (when (setq gnus-faq-buffer
6869                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6870       (gnus-configure-windows 'summary-faq))))
6871
6872 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6873 (defun gnus-summary-describe-group (&optional force)
6874   "Describe the current newsgroup."
6875   (interactive "P")
6876   (gnus-group-describe-group force gnus-newsgroup-name))
6877
6878 (defun gnus-summary-describe-briefly ()
6879   "Describe summary mode commands briefly."
6880   (interactive)
6881   (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")))
6882
6883 ;; Walking around group mode buffer from summary mode.
6884
6885 (defun gnus-summary-next-group (&optional no-article target-group backward)
6886   "Exit current newsgroup and then select next unread newsgroup.
6887 If prefix argument NO-ARTICLE is non-nil, no article is selected
6888 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6889 previous group instead."
6890   (interactive "P")
6891   ;; Stop pre-fetching.
6892   (gnus-async-halt-prefetch)
6893   (let ((current-group gnus-newsgroup-name)
6894         (current-buffer (current-buffer))
6895         entered)
6896     ;; First we semi-exit this group to update Xrefs and all variables.
6897     ;; We can't do a real exit, because the window conf must remain
6898     ;; the same in case the user is prompted for info, and we don't
6899     ;; want the window conf to change before that...
6900     (gnus-summary-exit t)
6901     (while (not entered)
6902       ;; Then we find what group we are supposed to enter.
6903       (set-buffer gnus-group-buffer)
6904       (gnus-group-jump-to-group current-group)
6905       (setq target-group
6906             (or target-group
6907                 (if (eq gnus-keep-same-level 'best)
6908                     (gnus-summary-best-group gnus-newsgroup-name)
6909                   (gnus-summary-search-group backward gnus-keep-same-level))))
6910       (if (not target-group)
6911           ;; There are no further groups, so we return to the group
6912           ;; buffer.
6913           (progn
6914             (gnus-message 5 "Returning to the group buffer")
6915             (setq entered t)
6916             (when (gnus-buffer-live-p current-buffer)
6917               (set-buffer current-buffer)
6918               (gnus-summary-exit))
6919             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6920         ;; We try to enter the target group.
6921         (gnus-group-jump-to-group target-group)
6922         (let ((unreads (gnus-group-group-unread)))
6923           (if (and (or (eq t unreads)
6924                        (and unreads (not (zerop unreads))))
6925                    (gnus-summary-read-group
6926                     target-group nil no-article
6927                     (and (buffer-name current-buffer) current-buffer)
6928                     nil backward))
6929               (setq entered t)
6930             (setq current-group target-group
6931                   target-group nil)))))))
6932
6933 (defun gnus-summary-prev-group (&optional no-article)
6934   "Exit current newsgroup and then select previous unread newsgroup.
6935 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6936   (interactive "P")
6937   (gnus-summary-next-group no-article nil t))
6938
6939 ;; Walking around summary lines.
6940
6941 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6942   "Go to the first subject satisfying any non-nil constraint.
6943 If UNREAD is non-nil, the article should be unread.
6944 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6945 If UNSEEN is non-nil, the article should be unseen.
6946 Returns the article selected or nil if there are no matching articles."
6947   (interactive "P")
6948   (cond
6949    ;; Empty summary.
6950    ((null gnus-newsgroup-data)
6951     (gnus-message 3 "No articles in the group")
6952     nil)
6953    ;; Pick the first article.
6954    ((not (or unread undownloaded unseen))
6955     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6956     (gnus-data-number (car gnus-newsgroup-data)))
6957    ;; Find the first unread article.
6958    (t
6959     (let ((data gnus-newsgroup-data))
6960       (while (and data
6961                   (let ((num (gnus-data-number (car data))))
6962                     (or (memq num gnus-newsgroup-unfetched)
6963                         (not (or (and unread
6964                                       (memq num gnus-newsgroup-unreads))
6965                                  (and undownloaded
6966                                       (memq num gnus-newsgroup-undownloaded))
6967                                  (and unseen
6968                                       (memq num gnus-newsgroup-unseen)))))))
6969         (setq data (cdr data)))
6970       (prog1
6971           (if data
6972               (progn
6973                 (goto-char (gnus-data-pos (car data)))
6974                 (gnus-data-number (car data)))
6975             (gnus-message 3 "No more%s articles"
6976                           (let* ((r (when unread " unread"))
6977                                  (d (when undownloaded " undownloaded"))
6978                                  (s (when unseen " unseen"))
6979                                  (l (delq nil (list r d s))))
6980                             (cond ((= 3 (length l))
6981                                    (concat r "," d ", or" s))
6982                                   ((= 2 (length l))
6983                                    (concat (car l) ", or" (cadr l)))
6984                                   ((= 1 (length l))
6985                                    (car l))
6986                                   (t
6987                                    ""))))
6988             nil
6989             )
6990         (gnus-summary-position-point))))))
6991
6992 (defun gnus-summary-next-subject (n &optional unread dont-display)
6993   "Go to next N'th summary line.
6994 If N is negative, go to the previous N'th subject line.
6995 If UNREAD is non-nil, only unread articles are selected.
6996 The difference between N and the actual number of steps taken is
6997 returned."
6998   (interactive "p")
6999   (let ((backward (< n 0))
7000         (n (abs n)))
7001     (while (and (> n 0)
7002                 (if backward
7003                     (gnus-summary-find-prev unread)
7004                   (gnus-summary-find-next unread)))
7005       (unless (zerop (setq n (1- n)))
7006         (gnus-summary-show-thread)))
7007     (when (/= 0 n)
7008       (gnus-message 7 "No more%s articles"
7009                     (if unread " unread" "")))
7010     (unless dont-display
7011       (gnus-summary-recenter)
7012       (gnus-summary-position-point))
7013     n))
7014
7015 (defun gnus-summary-next-unread-subject (n)
7016   "Go to next N'th unread summary line."
7017   (interactive "p")
7018   (gnus-summary-next-subject n t))
7019
7020 (defun gnus-summary-prev-subject (n &optional unread)
7021   "Go to previous N'th summary line.
7022 If optional argument UNREAD is non-nil, only unread article is selected."
7023   (interactive "p")
7024   (gnus-summary-next-subject (- n) unread))
7025
7026 (defun gnus-summary-prev-unread-subject (n)
7027   "Go to previous N'th unread summary line."
7028   (interactive "p")
7029   (gnus-summary-next-subject (- n) t))
7030
7031 (defun gnus-summary-goto-subjects (articles)
7032   "Insert the subject header for ARTICLES in the current buffer."
7033   (save-excursion
7034     (dolist (article articles)
7035       (gnus-summary-goto-subject article t)))
7036   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7037   (gnus-summary-position-point))
7038  
7039 (defun gnus-summary-goto-subject (article &optional force silent)
7040   "Go the subject line of ARTICLE.
7041 If FORCE, also allow jumping to articles not currently shown."
7042   (interactive "nArticle number: ")
7043   (unless (numberp article)
7044     (error "Article %s is not a number" article))
7045   (let ((b (point))
7046         (data (gnus-data-find article)))
7047     ;; We read in the article if we have to.
7048     (and (not data)
7049          force
7050          (gnus-summary-insert-subject
7051           article
7052           (if (or (numberp force) (vectorp force)) force)
7053           t)
7054          (setq data (gnus-data-find article)))
7055     (goto-char b)
7056     (if (not data)
7057         (progn
7058           (unless silent
7059             (gnus-message 3 "Can't find article %d" article))
7060           nil)
7061       (let ((pt (gnus-data-pos data)))
7062         (goto-char pt)
7063         (gnus-summary-set-article-display-arrow pt))
7064       (gnus-summary-position-point)
7065       article)))
7066
7067 ;; Walking around summary lines with displaying articles.
7068
7069 (defun gnus-summary-expand-window (&optional arg)
7070   "Make the summary buffer take up the entire Emacs frame.
7071 Given a prefix, will force an `article' buffer configuration."
7072   (interactive "P")
7073   (if arg
7074       (gnus-configure-windows 'article 'force)
7075     (gnus-configure-windows 'summary 'force)))
7076
7077 (defun gnus-summary-display-article (article &optional all-header)
7078   "Display ARTICLE in article buffer."
7079   (when (gnus-buffer-live-p gnus-article-buffer)
7080     (with-current-buffer gnus-article-buffer
7081       (mm-enable-multibyte)))
7082   (gnus-set-global-variables)
7083   (when (gnus-buffer-live-p gnus-article-buffer)
7084     (with-current-buffer gnus-article-buffer
7085       (setq gnus-article-charset gnus-newsgroup-charset)
7086       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7087       (mm-enable-multibyte)))
7088   (if (null article)
7089       nil
7090     (prog1
7091         (if gnus-summary-display-article-function
7092             (funcall gnus-summary-display-article-function article all-header)
7093           (gnus-article-prepare article all-header))
7094       (gnus-run-hooks 'gnus-select-article-hook)
7095       (when (and gnus-current-article
7096                  (not (zerop gnus-current-article)))
7097         (gnus-summary-goto-subject gnus-current-article))
7098       (gnus-summary-recenter)
7099       (when (and gnus-use-trees gnus-show-threads)
7100         (gnus-possibly-generate-tree article)
7101         (gnus-highlight-selected-tree article))
7102       ;; Successfully display article.
7103       (gnus-article-set-window-start
7104        (cdr (assq article gnus-newsgroup-bookmarks))))))
7105
7106 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7107   "Select the current article.
7108 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7109 non-nil, the article will be re-fetched even if it already present in
7110 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7111 be displayed."
7112   ;; Make sure we are in the summary buffer to work around bbdb bug.
7113   (unless (eq major-mode 'gnus-summary-mode)
7114     (set-buffer gnus-summary-buffer))
7115   (let ((article (or article (gnus-summary-article-number)))
7116         (all-headers (not (not all-headers))) ;Must be t or nil.
7117         gnus-summary-display-article-function)
7118     (and (not pseudo)
7119          (gnus-summary-article-pseudo-p article)
7120          (error "This is a pseudo-article"))
7121     (save-excursion
7122       (set-buffer gnus-summary-buffer)
7123       (if (or (and gnus-single-article-buffer
7124                    (or (null gnus-current-article)
7125                        (null gnus-article-current)
7126                        (null (get-buffer gnus-article-buffer))
7127                        (not (eq article (cdr gnus-article-current)))
7128                        (not (equal (car gnus-article-current)
7129                                    gnus-newsgroup-name))))
7130               (and (not gnus-single-article-buffer)
7131                    (or (null gnus-current-article)
7132                        (not (eq gnus-current-article article))))
7133               force)
7134           ;; The requested article is different from the current article.
7135           (progn
7136             (gnus-summary-display-article article all-headers)
7137             (when (gnus-buffer-live-p gnus-article-buffer)
7138               (with-current-buffer gnus-article-buffer
7139                 (if (not gnus-article-decoded-p) ;; a local variable
7140                     (mm-disable-multibyte))))
7141             (gnus-article-set-window-start
7142              (cdr (assq article gnus-newsgroup-bookmarks)))
7143             article)
7144         'old))))
7145
7146 (defun gnus-summary-force-verify-and-decrypt ()
7147   "Display buttons for signed/encrypted parts and verify/decrypt them."
7148   (interactive)
7149   (let ((mm-verify-option 'known)
7150         (mm-decrypt-option 'known)
7151         (gnus-article-emulate-mime t)
7152         (mm-fill-flowed nil)
7153         (gnus-buttonized-mime-types (append (list "multipart/signed"
7154                                                   "multipart/encrypted")
7155                                             gnus-buttonized-mime-types)))
7156     (gnus-summary-select-article nil 'force)))
7157
7158 (defun gnus-summary-set-current-mark (&optional current-mark)
7159   "Obsolete function."
7160   nil)
7161
7162 (defun gnus-summary-next-article (&optional unread subject backward push)
7163   "Select the next article.
7164 If UNREAD, only unread articles are selected.
7165 If SUBJECT, only articles with SUBJECT are selected.
7166 If BACKWARD, the previous article is selected instead of the next."
7167   (interactive "P")
7168   (cond
7169    ;; Is there such an article?
7170    ((and (gnus-summary-search-forward unread subject backward)
7171          (or (gnus-summary-display-article (gnus-summary-article-number))
7172              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7173     (gnus-summary-position-point))
7174    ;; If not, we try the first unread, if that is wanted.
7175    ((and subject
7176          gnus-auto-select-same
7177          (gnus-summary-first-unread-article))
7178     (gnus-summary-position-point)
7179     (gnus-message 6 "Wrapped"))
7180    ;; Try to get next/previous article not displayed in this group.
7181    ((and gnus-auto-extend-newsgroup
7182          (not unread) (not subject))
7183     (gnus-summary-goto-article
7184      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7185      nil (count-lines (point-min) (point))))
7186    ;; Go to next/previous group.
7187    (t
7188     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7189       (gnus-summary-jump-to-group gnus-newsgroup-name))
7190     (let ((cmd last-command-char)
7191           (point
7192            (with-current-buffer gnus-group-buffer
7193              (point)))
7194           (group
7195            (if (eq gnus-keep-same-level 'best)
7196                (gnus-summary-best-group gnus-newsgroup-name)
7197              (gnus-summary-search-group backward gnus-keep-same-level))))
7198       ;; For some reason, the group window gets selected.  We change
7199       ;; it back.
7200       (select-window (get-buffer-window (current-buffer)))
7201       ;; Select next unread newsgroup automagically.
7202       (cond
7203        ((or (not gnus-auto-select-next)
7204             (not cmd))
7205         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7206        ((or (eq gnus-auto-select-next 'quietly)
7207             (and (eq gnus-auto-select-next 'slightly-quietly)
7208                  push)
7209             (and (eq gnus-auto-select-next 'almost-quietly)
7210                  (gnus-summary-last-article-p)))
7211         ;; Select quietly.
7212         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7213             (gnus-summary-exit)
7214           (gnus-message 7 "No more%s articles (%s)..."
7215                         (if unread " unread" "")
7216                         (if group (concat "selecting " group)
7217                           "exiting"))
7218           (gnus-summary-next-group nil group backward)))
7219        (t
7220         (when (gnus-key-press-event-p last-input-event)
7221           (gnus-summary-walk-group-buffer
7222            gnus-newsgroup-name cmd unread backward point))))))))
7223
7224 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7225   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7226                       (?\C-p (gnus-group-prev-unread-group 1))))
7227         (cursor-in-echo-area t)
7228         keve key group ended prompt)
7229     (save-excursion
7230       (set-buffer gnus-group-buffer)
7231       (goto-char start)
7232       (setq group
7233             (if (eq gnus-keep-same-level 'best)
7234                 (gnus-summary-best-group gnus-newsgroup-name)
7235               (gnus-summary-search-group backward gnus-keep-same-level))))
7236     (while (not ended)
7237       (setq prompt
7238             (format
7239              "No more%s articles%s " (if unread " unread" "")
7240              (if (and group
7241                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7242                  (format " (Type %s for %s [%s])"
7243                          (single-key-description cmd) group
7244                          (gnus-group-unread group))
7245                (format " (Type %s to exit %s)"
7246                        (single-key-description cmd)
7247                        gnus-newsgroup-name))))
7248       ;; Confirm auto selection.
7249       (setq key (car (setq keve (gnus-read-event-char prompt)))
7250             ended t)
7251       (cond
7252        ((assq key keystrokes)
7253         (let ((obuf (current-buffer)))
7254           (switch-to-buffer gnus-group-buffer)
7255           (when group
7256             (gnus-group-jump-to-group group))
7257           (eval (cadr (assq key keystrokes)))
7258           (setq group (gnus-group-group-name))
7259           (switch-to-buffer obuf))
7260         (setq ended nil))
7261        ((equal key cmd)
7262         (if (or (not group)
7263                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7264             (gnus-summary-exit)
7265           (gnus-summary-next-group nil group backward)))
7266        (t
7267         (push (cdr keve) unread-command-events))))))
7268
7269 (defun gnus-summary-next-unread-article ()
7270   "Select unread article after current one."
7271   (interactive)
7272   (gnus-summary-next-article
7273    (or (not (eq gnus-summary-goto-unread 'never))
7274        (gnus-summary-last-article-p (gnus-summary-article-number)))
7275    (and gnus-auto-select-same
7276         (gnus-summary-article-subject))))
7277
7278 (defun gnus-summary-prev-article (&optional unread subject)
7279   "Select the article after the current one.
7280 If UNREAD is non-nil, only unread articles are selected."
7281   (interactive "P")
7282   (gnus-summary-next-article unread subject t))
7283
7284 (defun gnus-summary-prev-unread-article ()
7285   "Select unread article before current one."
7286   (interactive)
7287   (gnus-summary-prev-article
7288    (or (not (eq gnus-summary-goto-unread 'never))
7289        (gnus-summary-first-article-p (gnus-summary-article-number)))
7290    (and gnus-auto-select-same
7291         (gnus-summary-article-subject))))
7292
7293 (defun gnus-summary-next-page (&optional lines circular stop)
7294   "Show next page of the selected article.
7295 If at the end of the current article, select the next article.
7296 LINES says how many lines should be scrolled up.
7297
7298 If CIRCULAR is non-nil, go to the start of the article instead of
7299 selecting the next article when reaching the end of the current
7300 article.
7301
7302 If STOP is non-nil, just stop when reaching the end of the message.
7303
7304 Also see the variable `gnus-article-skip-boring'."
7305   (interactive "P")
7306   (setq gnus-summary-buffer (current-buffer))
7307   (gnus-set-global-variables)
7308   (let ((article (gnus-summary-article-number))
7309         (article-window (get-buffer-window gnus-article-buffer t))
7310         endp)
7311     ;; If the buffer is empty, we have no article.
7312     (unless article
7313       (error "No article to select"))
7314     (gnus-configure-windows 'article)
7315     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7316         (if (and (eq gnus-summary-goto-unread 'never)
7317                  (not (gnus-summary-last-article-p article)))
7318             (gnus-summary-next-article)
7319           (gnus-summary-next-unread-article))
7320       (if (or (null gnus-current-article)
7321               (null gnus-article-current)
7322               (/= article (cdr gnus-article-current))
7323               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7324           ;; Selected subject is different from current article's.
7325           (gnus-summary-display-article article)
7326         (when article-window
7327           (gnus-eval-in-buffer-window gnus-article-buffer
7328             (setq endp (or (gnus-article-next-page lines)
7329                            (gnus-article-only-boring-p))))
7330           (when endp
7331             (cond (stop
7332                    (gnus-message 3 "End of message"))
7333                   (circular
7334                    (gnus-summary-beginning-of-article))
7335                   (lines
7336                    (gnus-message 3 "End of message"))
7337                   ((null lines)
7338                    (if (and (eq gnus-summary-goto-unread 'never)
7339                             (not (gnus-summary-last-article-p article)))
7340                        (gnus-summary-next-article)
7341                      (gnus-summary-next-unread-article))))))))
7342     (gnus-summary-recenter)
7343     (gnus-summary-position-point)))
7344
7345 (defun gnus-summary-prev-page (&optional lines move)
7346   "Show previous page of selected article.
7347 Argument LINES specifies lines to be scrolled down.
7348 If MOVE, move to the previous unread article if point is at
7349 the beginning of the buffer."
7350   (interactive "P")
7351   (let ((article (gnus-summary-article-number))
7352         (article-window (get-buffer-window gnus-article-buffer t))
7353         endp)
7354     (gnus-configure-windows 'article)
7355     (if (or (null gnus-current-article)
7356             (null gnus-article-current)
7357             (/= article (cdr gnus-article-current))
7358             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7359         ;; Selected subject is different from current article's.
7360         (gnus-summary-display-article article)
7361       (gnus-summary-recenter)
7362       (when article-window
7363         (gnus-eval-in-buffer-window gnus-article-buffer
7364           (setq endp (gnus-article-prev-page lines)))
7365         (when (and move endp)
7366           (cond (lines
7367                  (gnus-message 3 "Beginning of message"))
7368                 ((null lines)
7369                  (if (and (eq gnus-summary-goto-unread 'never)
7370                           (not (gnus-summary-first-article-p article)))
7371                      (gnus-summary-prev-article)
7372                    (gnus-summary-prev-unread-article))))))))
7373   (gnus-summary-position-point))
7374
7375 (defun gnus-summary-prev-page-or-article (&optional lines)
7376   "Show previous page of selected article.
7377 Argument LINES specifies lines to be scrolled down.
7378 If at the beginning of the article, go to the next article."
7379   (interactive "P")
7380   (gnus-summary-prev-page lines t))
7381
7382 (defun gnus-summary-scroll-up (lines)
7383   "Scroll up (or down) one line current article.
7384 Argument LINES specifies lines to be scrolled up (or down if negative)."
7385   (interactive "p")
7386   (gnus-configure-windows 'article)
7387   (gnus-summary-show-thread)
7388   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7389     (gnus-eval-in-buffer-window gnus-article-buffer
7390       (cond ((> lines 0)
7391              (when (gnus-article-next-page lines)
7392                (gnus-message 3 "End of message")))
7393             ((< lines 0)
7394              (gnus-article-prev-page (- lines))))))
7395   (gnus-summary-recenter)
7396   (gnus-summary-position-point))
7397
7398 (defun gnus-summary-scroll-down (lines)
7399   "Scroll down (or up) one line current article.
7400 Argument LINES specifies lines to be scrolled down (or up if negative)."
7401   (interactive "p")
7402   (gnus-summary-scroll-up (- lines)))
7403
7404 (defun gnus-summary-next-same-subject ()
7405   "Select next article which has the same subject as current one."
7406   (interactive)
7407   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7408
7409 (defun gnus-summary-prev-same-subject ()
7410   "Select previous article which has the same subject as current one."
7411   (interactive)
7412   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7413
7414 (defun gnus-summary-next-unread-same-subject ()
7415   "Select next unread article which has the same subject as current one."
7416   (interactive)
7417   (gnus-summary-next-article t (gnus-summary-article-subject)))
7418
7419 (defun gnus-summary-prev-unread-same-subject ()
7420   "Select previous unread article which has the same subject as current one."
7421   (interactive)
7422   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7423
7424 (defun gnus-summary-first-unread-article ()
7425   "Select the first unread article.
7426 Return nil if there are no unread articles."
7427   (interactive)
7428   (prog1
7429       (when (gnus-summary-first-subject t)
7430         (gnus-summary-show-thread)
7431         (gnus-summary-first-subject t)
7432         (gnus-summary-display-article (gnus-summary-article-number)))
7433     (gnus-summary-position-point)))
7434
7435 (defun gnus-summary-first-unread-subject ()
7436   "Place the point on the subject line of the first unread article.
7437 Return nil if there are no unread articles."
7438   (interactive)
7439   (prog1
7440       (when (gnus-summary-first-subject t)
7441         (gnus-summary-show-thread)
7442         (gnus-summary-first-subject t))
7443     (gnus-summary-position-point)))
7444
7445 (defun gnus-summary-first-unseen-subject ()
7446   "Place the point on the subject line of the first unseen article.
7447 Return nil if there are no unseen articles."
7448   (interactive)
7449   (prog1
7450       (when (gnus-summary-first-subject nil nil t)
7451         (gnus-summary-show-thread)
7452         (gnus-summary-first-subject nil nil t))
7453     (gnus-summary-position-point)))
7454
7455 (defun gnus-summary-first-unseen-or-unread-subject ()
7456   "Place the point on the subject line of the first unseen article or,
7457 if all article have been seen, on the subject line of the first unread
7458 article."
7459   (interactive)
7460   (prog1
7461       (unless (when (gnus-summary-first-subject nil nil t)
7462                 (gnus-summary-show-thread)
7463                 (gnus-summary-first-subject nil nil t))
7464         (when (gnus-summary-first-subject t)
7465           (gnus-summary-show-thread)
7466           (gnus-summary-first-subject t)))
7467     (gnus-summary-position-point)))
7468
7469 (defun gnus-summary-first-article ()
7470   "Select the first article.
7471 Return nil if there are no articles."
7472   (interactive)
7473   (prog1
7474       (when (gnus-summary-first-subject)
7475         (gnus-summary-show-thread)
7476         (gnus-summary-first-subject)
7477         (gnus-summary-display-article (gnus-summary-article-number)))
7478     (gnus-summary-position-point)))
7479
7480 (defun gnus-summary-best-unread-article (&optional arg)
7481   "Select the unread article with the highest score.
7482 If given a prefix argument, select the next unread article that has a
7483 score higher than the default score."
7484   (interactive "P")
7485   (let ((article (if arg
7486                      (gnus-summary-better-unread-subject)
7487                    (gnus-summary-best-unread-subject))))
7488     (if article
7489         (gnus-summary-goto-article article)
7490       (error "No unread articles"))))
7491
7492 (defun gnus-summary-best-unread-subject ()
7493   "Select the unread subject with the highest score."
7494   (interactive)
7495   (let ((best -1000000)
7496         (data gnus-newsgroup-data)
7497         article score)
7498     (while data
7499       (and (gnus-data-unread-p (car data))
7500            (> (setq score
7501                     (gnus-summary-article-score (gnus-data-number (car data))))
7502               best)
7503            (setq best score
7504                  article (gnus-data-number (car data))))
7505       (setq data (cdr data)))
7506     (when article
7507       (gnus-summary-goto-subject article))
7508     (gnus-summary-position-point)
7509     article))
7510
7511 (defun gnus-summary-better-unread-subject ()
7512   "Select the first unread subject that has a score over the default score."
7513   (interactive)
7514   (let ((data gnus-newsgroup-data)
7515         article score)
7516     (while (and (setq article (gnus-data-number (car data)))
7517                 (or (gnus-data-read-p (car data))
7518                     (not (> (gnus-summary-article-score article)
7519                             gnus-summary-default-score))))
7520       (setq data (cdr data)))
7521     (when article
7522       (gnus-summary-goto-subject article))
7523     (gnus-summary-position-point)
7524     article))
7525
7526 (defun gnus-summary-last-subject ()
7527   "Go to the last displayed subject line in the group."
7528   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7529     (when article
7530       (gnus-summary-goto-subject article))))
7531
7532 (defun gnus-summary-goto-article (article &optional all-headers force)
7533   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7534 If ALL-HEADERS is non-nil, no header lines are hidden.
7535 If FORCE, go to the article even if it isn't displayed.  If FORCE
7536 is a number, it is the line the article is to be displayed on."
7537   (interactive
7538    (list
7539     (completing-read
7540      "Article number or Message-ID: "
7541      (mapcar (lambda (number) (list (int-to-string number)))
7542              gnus-newsgroup-limit))
7543     current-prefix-arg
7544     t))
7545   (prog1
7546       (if (and (stringp article)
7547                (string-match "@\\|%40" article))
7548           (gnus-summary-refer-article article)
7549         (when (stringp article)
7550           (setq article (string-to-number article)))
7551         (if (gnus-summary-goto-subject article force)
7552             (gnus-summary-display-article article all-headers)
7553           (gnus-message 4 "Couldn't go to article %s" article) nil))
7554     (gnus-summary-position-point)))
7555
7556 (defun gnus-summary-goto-last-article ()
7557   "Go to the previously read article."
7558   (interactive)
7559   (prog1
7560       (when gnus-last-article
7561         (gnus-summary-goto-article gnus-last-article nil t))
7562     (gnus-summary-position-point)))
7563
7564 (defun gnus-summary-pop-article (number)
7565   "Pop one article off the history and go to the previous.
7566 NUMBER articles will be popped off."
7567   (interactive "p")
7568   (let (to)
7569     (setq gnus-newsgroup-history
7570           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7571     (if to
7572         (gnus-summary-goto-article (car to) nil t)
7573       (error "Article history empty")))
7574   (gnus-summary-position-point))
7575
7576 ;; Summary commands and functions for limiting the summary buffer.
7577
7578 (defun gnus-summary-limit-to-articles (n)
7579   "Limit the summary buffer to the next N articles.
7580 If not given a prefix, use the process marked articles instead."
7581   (interactive "P")
7582   (prog1
7583       (let ((articles (gnus-summary-work-articles n)))
7584         (setq gnus-newsgroup-processable nil)
7585         (gnus-summary-limit articles))
7586     (gnus-summary-position-point)))
7587
7588 (defun gnus-summary-pop-limit (&optional total)
7589   "Restore the previous limit.
7590 If given a prefix, remove all limits."
7591   (interactive "P")
7592   (when total
7593     (setq gnus-newsgroup-limits
7594           (list (mapcar (lambda (h) (mail-header-number h))
7595                         gnus-newsgroup-headers))))
7596   (unless gnus-newsgroup-limits
7597     (error "No limit to pop"))
7598   (prog1
7599       (gnus-summary-limit nil 'pop)
7600     (gnus-summary-position-point)))
7601
7602 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7603   "Limit the summary buffer to articles that have subjects that match a regexp.
7604 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7605   (interactive
7606    (list (read-string (if current-prefix-arg
7607                           "Exclude subject (regexp): "
7608                         "Limit to subject (regexp): "))
7609          nil current-prefix-arg))
7610   (unless header
7611     (setq header "subject"))
7612   (when (not (equal "" subject))
7613     (prog1
7614         (let ((articles (gnus-summary-find-matching
7615                          (or header "subject") subject 'all nil nil
7616                          not-matching)))
7617           (unless articles
7618             (error "Found no matches for \"%s\"" subject))
7619           (gnus-summary-limit articles))
7620       (gnus-summary-position-point))))
7621
7622 (defun gnus-summary-limit-to-author (from &optional not-matching)
7623   "Limit the summary buffer to articles that have authors that match a regexp.
7624 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7625   (interactive
7626    (list (read-string (if current-prefix-arg
7627                           "Exclude author (regexp): "
7628                         "Limit to author (regexp): "))
7629          current-prefix-arg))
7630   (gnus-summary-limit-to-subject from "from" not-matching))
7631
7632 (defun gnus-summary-limit-to-age (age &optional younger-p)
7633   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7634 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7635 articles that are younger than AGE days."
7636   (interactive
7637    (let ((younger current-prefix-arg)
7638          (days-got nil)
7639          days)
7640      (while (not days-got)
7641        (setq days (if younger
7642                       (read-string "Limit to articles younger than (in days, older when negative): ")
7643                     (read-string
7644                      "Limit to articles older than (in days, younger when negative): ")))
7645        (when (> (length days) 0)
7646          (setq days (read days)))
7647        (if (numberp days)
7648            (progn
7649              (setq days-got t)
7650              (when (< days 0)
7651                (setq younger (not younger))
7652                (setq days (* days -1))))
7653          (message "Please enter a number.")
7654          (sleep-for 1)))
7655      (list days younger)))
7656   (prog1
7657       (let ((data gnus-newsgroup-data)
7658             (cutoff (days-to-time age))
7659             articles d date is-younger)
7660         (while (setq d (pop data))
7661           (when (and (vectorp (gnus-data-header d))
7662                      (setq date (mail-header-date (gnus-data-header d))))
7663             (setq is-younger (time-less-p
7664                               (time-since (condition-case ()
7665                                               (date-to-time date)
7666                                             (error '(0 0))))
7667                               cutoff))
7668             (when (if younger-p
7669                       is-younger
7670                     (not is-younger))
7671               (push (gnus-data-number d) articles))))
7672         (gnus-summary-limit (nreverse articles)))
7673     (gnus-summary-position-point)))
7674
7675 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7676   "Limit the summary buffer to articles that match an 'extra' header."
7677   (interactive
7678    (let ((header
7679           (intern
7680            (gnus-completing-read-with-default
7681             (symbol-name (car gnus-extra-headers))
7682             (if current-prefix-arg
7683                 "Exclude extra header:"
7684               "Limit extra header:")
7685             (mapcar (lambda (x)
7686                       (cons (symbol-name x) x))
7687                     gnus-extra-headers)
7688             nil
7689             t))))
7690      (list header
7691            (read-string (format "%s header %s (regexp): "
7692                                 (if current-prefix-arg "Exclude" "Limit to")
7693                                 header))
7694            current-prefix-arg)))
7695   (when (not (equal "" regexp))
7696     (prog1
7697         (let ((articles (gnus-summary-find-matching
7698                          (cons 'extra header) regexp 'all nil nil
7699                          not-matching)))
7700           (unless articles
7701             (error "Found no matches for \"%s\"" regexp))
7702           (gnus-summary-limit articles))
7703       (gnus-summary-position-point))))
7704
7705 (defun gnus-summary-limit-to-display-predicate ()
7706   "Limit the summary buffer to the predicated in the `display' group parameter."
7707   (interactive)
7708   (unless gnus-newsgroup-display
7709     (error "There is no `display' group parameter"))
7710   (let (articles)
7711     (dolist (number gnus-newsgroup-articles)
7712       (when (funcall gnus-newsgroup-display)
7713         (push number articles)))
7714     (gnus-summary-limit articles))
7715   (gnus-summary-position-point))
7716
7717 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7718 (make-obsolete
7719  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7720
7721 (defun gnus-summary-limit-to-unread (&optional all)
7722   "Limit the summary buffer to articles that are not marked as read.
7723 If ALL is non-nil, limit strictly to unread articles."
7724   (interactive "P")
7725   (if all
7726       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7727     (gnus-summary-limit-to-marks
7728      ;; Concat all the marks that say that an article is read and have
7729      ;; those removed.
7730      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7731            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7732            gnus-low-score-mark gnus-expirable-mark
7733            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7734            gnus-duplicate-mark gnus-souped-mark)
7735      'reverse)))
7736
7737 (defun gnus-summary-limit-to-replied (&optional unreplied)
7738   "Limit the summary buffer to replied articles.
7739 If UNREPLIED (the prefix), limit to unreplied articles."
7740   (interactive "P")
7741   (if unreplied
7742       (gnus-summary-limit
7743        (gnus-set-difference gnus-newsgroup-articles
7744         gnus-newsgroup-replied))
7745     (gnus-summary-limit gnus-newsgroup-replied))
7746   (gnus-summary-position-point))
7747
7748 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7749 (make-obsolete 'gnus-summary-delete-marked-with
7750                'gnus-summary-limit-exclude-marks)
7751
7752 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7753   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7754 If REVERSE, limit the summary buffer to articles that are marked
7755 with MARKS.  MARKS can either be a string of marks or a list of marks.
7756 Returns how many articles were removed."
7757   (interactive "sMarks: ")
7758   (gnus-summary-limit-to-marks marks t))
7759
7760 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7761   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7762 If REVERSE (the prefix), limit the summary buffer to articles that are
7763 not marked with MARKS.  MARKS can either be a string of marks or a
7764 list of marks.
7765 Returns how many articles were removed."
7766   (interactive "sMarks: \nP")
7767   (prog1
7768       (let ((data gnus-newsgroup-data)
7769             (marks (if (listp marks) marks
7770                      (append marks nil))) ; Transform to list.
7771             articles)
7772         (while data
7773           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7774                   (memq (gnus-data-mark (car data)) marks))
7775             (push (gnus-data-number (car data)) articles))
7776           (setq data (cdr data)))
7777         (gnus-summary-limit articles))
7778     (gnus-summary-position-point)))
7779
7780 (defun gnus-summary-limit-to-score (score)
7781   "Limit to articles with score at or above SCORE."
7782   (interactive "NLimit to articles with score of at least: ")
7783   (let ((data gnus-newsgroup-data)
7784         articles)
7785     (while data
7786       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7787                 score)
7788         (push (gnus-data-number (car data)) articles))
7789       (setq data (cdr data)))
7790     (prog1
7791         (gnus-summary-limit articles)
7792       (gnus-summary-position-point))))
7793
7794 (defun gnus-summary-limit-to-unseen ()
7795   "Limit to unseen articles."
7796   (interactive)
7797   (prog1
7798       (gnus-summary-limit gnus-newsgroup-unseen)
7799     (gnus-summary-position-point)))
7800
7801 (defun gnus-summary-limit-include-thread (id)
7802   "Display all the hidden articles that is in the thread with ID in it.
7803 When called interactively, ID is the Message-ID of the current
7804 article."
7805   (interactive (list (mail-header-id (gnus-summary-article-header))))
7806   (let ((articles (gnus-articles-in-thread
7807                    (gnus-id-to-thread (gnus-root-id id)))))
7808     (prog1
7809         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7810       (gnus-summary-limit-include-matching-articles
7811        "subject"
7812        (regexp-quote (gnus-simplify-subject-re
7813                       (mail-header-subject (gnus-id-to-header id)))))
7814       (gnus-summary-position-point))))
7815
7816 (defun gnus-summary-limit-include-matching-articles (header regexp)
7817   "Display all the hidden articles that have HEADERs that match REGEXP."
7818   (interactive (list (read-string "Match on header: ")
7819                      (read-string "Regexp: ")))
7820   (let ((articles (gnus-find-matching-articles header regexp)))
7821     (prog1
7822         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7823       (gnus-summary-position-point))))
7824
7825 (defun gnus-summary-insert-dormant-articles ()
7826   "Insert all the dormant articles for this group into the current buffer."
7827   (interactive)
7828   (let ((gnus-verbose (max 6 gnus-verbose)))
7829     (if (not gnus-newsgroup-dormant)
7830         (gnus-message 3 "No cached articles for this group")
7831       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7832
7833 (defun gnus-summary-limit-include-dormant ()
7834   "Display all the hidden articles that are marked as dormant.
7835 Note that this command only works on a subset of the articles currently
7836 fetched for this group."
7837   (interactive)
7838   (unless gnus-newsgroup-dormant
7839     (error "There are no dormant articles in this group"))
7840   (prog1
7841       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7842     (gnus-summary-position-point)))
7843
7844 (defun gnus-summary-limit-exclude-dormant ()
7845   "Hide all dormant articles."
7846   (interactive)
7847   (prog1
7848       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7849     (gnus-summary-position-point)))
7850
7851 (defun gnus-summary-limit-exclude-childless-dormant ()
7852   "Hide all dormant articles that have no children."
7853   (interactive)
7854   (let ((data (gnus-data-list t))
7855         articles d children)
7856     ;; Find all articles that are either not dormant or have
7857     ;; children.
7858     (while (setq d (pop data))
7859       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7860                 (and (setq children
7861                            (gnus-article-children (gnus-data-number d)))
7862                      (let (found)
7863                        (while children
7864                          (when (memq (car children) articles)
7865                            (setq children nil
7866                                  found t))
7867                          (pop children))
7868                        found)))
7869         (push (gnus-data-number d) articles)))
7870     ;; Do the limiting.
7871     (prog1
7872         (gnus-summary-limit articles)
7873       (gnus-summary-position-point))))
7874
7875 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7876   "Mark all unread excluded articles as read.
7877 If ALL, mark even excluded ticked and dormants as read."
7878   (interactive "P")
7879   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7880   (let ((articles (gnus-sorted-ndifference
7881                    (sort
7882                     (mapcar (lambda (h) (mail-header-number h))
7883                             gnus-newsgroup-headers)
7884                     '<)
7885                    gnus-newsgroup-limit))
7886         article)
7887     (setq gnus-newsgroup-unreads
7888           (gnus-sorted-intersection gnus-newsgroup-unreads
7889                                     gnus-newsgroup-limit))
7890     (if all
7891         (setq gnus-newsgroup-dormant nil
7892               gnus-newsgroup-marked nil
7893               gnus-newsgroup-reads
7894               (nconc
7895                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7896                gnus-newsgroup-reads))
7897       (while (setq article (pop articles))
7898         (unless (or (memq article gnus-newsgroup-dormant)
7899                     (memq article gnus-newsgroup-marked))
7900           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7901
7902 (defun gnus-summary-limit (articles &optional pop)
7903   (if pop
7904       ;; We pop the previous limit off the stack and use that.
7905       (setq articles (car gnus-newsgroup-limits)
7906             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7907     ;; We use the new limit, so we push the old limit on the stack.
7908     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7909   ;; Set the limit.
7910   (setq gnus-newsgroup-limit articles)
7911   (let ((total (length gnus-newsgroup-data))
7912         (data (gnus-data-find-list (gnus-summary-article-number)))
7913         (gnus-summary-mark-below nil)   ; Inhibit this.
7914         found)
7915     ;; This will do all the work of generating the new summary buffer
7916     ;; according to the new limit.
7917     (gnus-summary-prepare)
7918     ;; Hide any threads, possibly.
7919     (gnus-summary-maybe-hide-threads)
7920     ;; Try to return to the article you were at, or one in the
7921     ;; neighborhood.
7922     (when data
7923       ;; We try to find some article after the current one.
7924       (while data
7925         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7926           (setq data nil
7927                 found t))
7928         (setq data (cdr data))))
7929     (unless found
7930       ;; If there is no data, that means that we were after the last
7931       ;; article.  The same goes when we can't find any articles
7932       ;; after the current one.
7933       (goto-char (point-max))
7934       (gnus-summary-find-prev))
7935     (gnus-set-mode-line 'summary)
7936     ;; We return how many articles were removed from the summary
7937     ;; buffer as a result of the new limit.
7938     (- total (length gnus-newsgroup-data))))
7939
7940 (defsubst gnus-invisible-cut-children (threads)
7941   (let ((num 0))
7942     (while threads
7943       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7944         (incf num))
7945       (pop threads))
7946     (< num 2)))
7947
7948 (defsubst gnus-cut-thread (thread)
7949   "Go forwards in the thread until we find an article that we want to display."
7950   (when (or (eq gnus-fetch-old-headers 'some)
7951             (eq gnus-fetch-old-headers 'invisible)
7952             (numberp gnus-fetch-old-headers)
7953             (eq gnus-build-sparse-threads 'some)
7954             (eq gnus-build-sparse-threads 'more))
7955     ;; Deal with old-fetched headers and sparse threads.
7956     (while (and
7957             thread
7958             (or
7959              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7960              (gnus-summary-article-ancient-p
7961               (mail-header-number (car thread))))
7962             (if (or (<= (length (cdr thread)) 1)
7963                     (eq gnus-fetch-old-headers 'invisible))
7964                 (setq gnus-newsgroup-limit
7965                       (delq (mail-header-number (car thread))
7966                             gnus-newsgroup-limit)
7967                       thread (cadr thread))
7968               (when (gnus-invisible-cut-children (cdr thread))
7969                 (let ((th (cdr thread)))
7970                   (while th
7971                     (if (memq (mail-header-number (caar th))
7972                               gnus-newsgroup-limit)
7973                         (setq thread (car th)
7974                               th nil)
7975                       (setq th (cdr th))))))))))
7976   thread)
7977
7978 (defun gnus-cut-threads (threads)
7979   "Cut off all uninteresting articles from the beginning of THREADS."
7980   (when (or (eq gnus-fetch-old-headers 'some)
7981             (eq gnus-fetch-old-headers 'invisible)
7982             (numberp gnus-fetch-old-headers)
7983             (eq gnus-build-sparse-threads 'some)
7984             (eq gnus-build-sparse-threads 'more))
7985     (let ((th threads))
7986       (while th
7987         (setcar th (gnus-cut-thread (car th)))
7988         (setq th (cdr th)))))
7989   ;; Remove nixed out threads.
7990   (delq nil threads))
7991
7992 (defun gnus-summary-initial-limit (&optional show-if-empty)
7993   "Figure out what the initial limit is supposed to be on group entry.
7994 This entails weeding out unwanted dormants, low-scored articles,
7995 fetch-old-headers verbiage, and so on."
7996   ;; Most groups have nothing to remove.
7997   (if (or gnus-inhibit-limiting
7998           (and (null gnus-newsgroup-dormant)
7999                (eq gnus-newsgroup-display 'gnus-not-ignore)
8000                (not (eq gnus-fetch-old-headers 'some))
8001                (not (numberp gnus-fetch-old-headers))
8002                (not (eq gnus-fetch-old-headers 'invisible))
8003                (null gnus-summary-expunge-below)
8004                (not (eq gnus-build-sparse-threads 'some))
8005                (not (eq gnus-build-sparse-threads 'more))
8006                (null gnus-thread-expunge-below)
8007                (not gnus-use-nocem)))
8008       ()                                ; Do nothing.
8009     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8010     (setq gnus-newsgroup-limit nil)
8011     (mapatoms
8012      (lambda (node)
8013        (unless (car (symbol-value node))
8014          ;; These threads have no parents -- they are roots.
8015          (let ((nodes (cdr (symbol-value node)))
8016                thread)
8017            (while nodes
8018              (if (and gnus-thread-expunge-below
8019                       (< (gnus-thread-total-score (car nodes))
8020                          gnus-thread-expunge-below))
8021                  (gnus-expunge-thread (pop nodes))
8022                (setq thread (pop nodes))
8023                (gnus-summary-limit-children thread))))))
8024      gnus-newsgroup-dependencies)
8025     ;; If this limitation resulted in an empty group, we might
8026     ;; pop the previous limit and use it instead.
8027     (when (and (not gnus-newsgroup-limit)
8028                show-if-empty)
8029       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8030     gnus-newsgroup-limit))
8031
8032 (defun gnus-summary-limit-children (thread)
8033   "Return 1 if this subthread is visible and 0 if it is not."
8034   ;; First we get the number of visible children to this thread.  This
8035   ;; is done by recursing down the thread using this function, so this
8036   ;; will really go down to a leaf article first, before slowly
8037   ;; working its way up towards the root.
8038   (when thread
8039     (let* ((max-lisp-eval-depth 5000)
8040            (children
8041            (if (cdr thread)
8042                (apply '+ (mapcar 'gnus-summary-limit-children
8043                                  (cdr thread)))
8044              0))
8045           (number (mail-header-number (car thread)))
8046           score)
8047       (if (and
8048            (not (memq number gnus-newsgroup-marked))
8049            (or
8050             ;; If this article is dormant and has absolutely no visible
8051             ;; children, then this article isn't visible.
8052             (and (memq number gnus-newsgroup-dormant)
8053                  (zerop children))
8054             ;; If this is "fetch-old-headered" and there is no
8055             ;; visible children, then we don't want this article.
8056             (and (or (eq gnus-fetch-old-headers 'some)
8057                      (numberp gnus-fetch-old-headers))
8058                  (gnus-summary-article-ancient-p number)
8059                  (zerop children))
8060             ;; If this is "fetch-old-headered" and `invisible', then
8061             ;; we don't want this article.
8062             (and (eq gnus-fetch-old-headers 'invisible)
8063                  (gnus-summary-article-ancient-p number))
8064             ;; If this is a sparsely inserted article with no children,
8065             ;; we don't want it.
8066             (and (eq gnus-build-sparse-threads 'some)
8067                  (gnus-summary-article-sparse-p number)
8068                  (zerop children))
8069             ;; If we use expunging, and this article is really
8070             ;; low-scored, then we don't want this article.
8071             (when (and gnus-summary-expunge-below
8072                        (< (setq score
8073                                 (or (cdr (assq number gnus-newsgroup-scored))
8074                                     gnus-summary-default-score))
8075                           gnus-summary-expunge-below))
8076               ;; We increase the expunge-tally here, but that has
8077               ;; nothing to do with the limits, really.
8078               (incf gnus-newsgroup-expunged-tally)
8079               ;; We also mark as read here, if that's wanted.
8080               (when (and gnus-summary-mark-below
8081                          (< score gnus-summary-mark-below))
8082                 (setq gnus-newsgroup-unreads
8083                       (delq number gnus-newsgroup-unreads))
8084                 (if gnus-newsgroup-auto-expire
8085                     (push number gnus-newsgroup-expirable)
8086                   (push (cons number gnus-low-score-mark)
8087                         gnus-newsgroup-reads)))
8088               t)
8089             ;; Do the `display' group parameter.
8090             (and gnus-newsgroup-display
8091                  (not (funcall gnus-newsgroup-display)))
8092             ;; Check NoCeM things.
8093             (when (and gnus-use-nocem
8094                        (gnus-nocem-unwanted-article-p
8095                         (mail-header-id (car thread))))
8096               (setq gnus-newsgroup-unreads
8097                     (delq number gnus-newsgroup-unreads))
8098               t)))
8099           ;; Nope, invisible article.
8100           0
8101         ;; Ok, this article is to be visible, so we add it to the limit
8102         ;; and return 1.
8103         (push number gnus-newsgroup-limit)
8104         1))))
8105
8106 (defun gnus-expunge-thread (thread)
8107   "Mark all articles in THREAD as read."
8108   (let* ((number (mail-header-number (car thread))))
8109     (incf gnus-newsgroup-expunged-tally)
8110     ;; We also mark as read here, if that's wanted.
8111     (setq gnus-newsgroup-unreads
8112           (delq number gnus-newsgroup-unreads))
8113     (if gnus-newsgroup-auto-expire
8114         (push number gnus-newsgroup-expirable)
8115       (push (cons number gnus-low-score-mark)
8116             gnus-newsgroup-reads)))
8117   ;; Go recursively through all subthreads.
8118   (mapcar 'gnus-expunge-thread (cdr thread)))
8119
8120 ;; Summary article oriented commands
8121
8122 (defun gnus-summary-refer-parent-article (n)
8123   "Refer parent article N times.
8124 If N is negative, go to ancestor -N instead.
8125 The difference between N and the number of articles fetched is returned."
8126   (interactive "p")
8127   (let ((skip 1)
8128         error header ref)
8129     (when (not (natnump n))
8130       (setq skip (abs n)
8131             n 1))
8132     (while (and (> n 0)
8133                 (not error))
8134       (setq header (gnus-summary-article-header))
8135       (if (and (eq (mail-header-number header)
8136                    (cdr gnus-article-current))
8137                (equal gnus-newsgroup-name
8138                       (car gnus-article-current)))
8139           ;; If we try to find the parent of the currently
8140           ;; displayed article, then we take a look at the actual
8141           ;; References header, since this is slightly more
8142           ;; reliable than the References field we got from the
8143           ;; server.
8144           (save-excursion
8145             (set-buffer gnus-original-article-buffer)
8146             (nnheader-narrow-to-headers)
8147             (unless (setq ref (message-fetch-field "references"))
8148               (when (setq ref (message-fetch-field "in-reply-to"))
8149                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8150             (widen))
8151         (setq ref
8152               ;; It's not the current article, so we take a bet on
8153               ;; the value we got from the server.
8154               (mail-header-references header)))
8155       (if (and ref
8156                (not (equal ref "")))
8157           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8158             (gnus-message 1 "Couldn't find parent"))
8159         (gnus-message 1 "No references in article %d"
8160                       (gnus-summary-article-number))
8161         (setq error t))
8162       (decf n))
8163     (gnus-summary-position-point)
8164     n))
8165
8166 (defun gnus-summary-refer-references ()
8167   "Fetch all articles mentioned in the References header.
8168 Return the number of articles fetched."
8169   (interactive)
8170   (let ((ref (mail-header-references (gnus-summary-article-header)))
8171         (current (gnus-summary-article-number))
8172         (n 0))
8173     (if (or (not ref)
8174             (equal ref ""))
8175         (error "No References in the current article")
8176       ;; For each Message-ID in the References header...
8177       (while (string-match "<[^>]*>" ref)
8178         (incf n)
8179         ;; ... fetch that article.
8180         (gnus-summary-refer-article
8181          (prog1 (match-string 0 ref)
8182            (setq ref (substring ref (match-end 0))))))
8183       (gnus-summary-goto-subject current)
8184       (gnus-summary-position-point)
8185       n)))
8186
8187 (defun gnus-summary-refer-thread (&optional limit)
8188   "Fetch all articles in the current thread.
8189 If LIMIT (the numerical prefix), fetch that many old headers instead
8190 of what's specified by the `gnus-refer-thread-limit' variable."
8191   (interactive "P")
8192   (let ((id (mail-header-id (gnus-summary-article-header)))
8193         (limit (if limit (prefix-numeric-value limit)
8194                  gnus-refer-thread-limit)))
8195     (unless (eq gnus-fetch-old-headers 'invisible)
8196       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8197       ;; Retrieve the headers and read them in.
8198       (if (eq (if (numberp limit)
8199                   (gnus-retrieve-headers
8200                    (list (min
8201                           (+ (mail-header-number
8202                               (gnus-summary-article-header))
8203                              limit)
8204                           gnus-newsgroup-end))
8205                    gnus-newsgroup-name (* limit 2))
8206                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8207                 ;; headers.
8208                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8209                                        gnus-newsgroup-name limit))
8210               'nov)
8211           (gnus-build-all-threads)
8212         (error "Can't fetch thread from back ends that don't support NOV"))
8213       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8214     (gnus-summary-limit-include-thread id)))
8215
8216 (defun gnus-summary-refer-article (message-id)
8217   "Fetch an article specified by MESSAGE-ID."
8218   (interactive "sMessage-ID: ")
8219   (when (and (stringp message-id)
8220              (not (zerop (length message-id))))
8221     (setq message-id (gnus-replace-in-string message-id " " ""))
8222     ;; Construct the correct Message-ID if necessary.
8223     ;; Suggested by tale@pawl.rpi.edu.
8224     (unless (string-match "^<" message-id)
8225       (setq message-id (concat "<" message-id)))
8226     (unless (string-match ">$" message-id)
8227       (setq message-id (concat message-id ">")))
8228     ;; People often post MIDs from URLs, so unhex it:
8229     (unless (string-match "@" message-id)
8230       (setq message-id (gnus-url-unhex-string message-id)))
8231     (let* ((header (gnus-id-to-header message-id))
8232            (sparse (and header
8233                         (gnus-summary-article-sparse-p
8234                          (mail-header-number header))
8235                         (memq (mail-header-number header)
8236                               gnus-newsgroup-limit)))
8237            number)
8238       (cond
8239        ;; If the article is present in the buffer we just go to it.
8240        ((and header
8241              (or (not (gnus-summary-article-sparse-p
8242                        (mail-header-number header)))
8243                  sparse))
8244         (prog1
8245             (gnus-summary-goto-article
8246              (mail-header-number header) nil t)
8247           (when sparse
8248             (gnus-summary-update-article (mail-header-number header)))))
8249        (t
8250         ;; We fetch the article.
8251         (catch 'found
8252           (dolist (gnus-override-method (gnus-refer-article-methods))
8253             (when (and (gnus-check-server gnus-override-method)
8254                        ;; Fetch the header,
8255                        (setq number (gnus-summary-insert-subject message-id)))
8256               ;; and display the article.
8257               (gnus-summary-select-article nil nil nil number)
8258               (throw 'found t)))
8259           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8260
8261 (defun gnus-refer-article-methods ()
8262   "Return a list of referable methods."
8263   (cond
8264    ;; No method, so we default to current and native.
8265    ((null gnus-refer-article-method)
8266     (list gnus-current-select-method gnus-select-method))
8267    ;; Current.
8268    ((eq 'current gnus-refer-article-method)
8269     (list gnus-current-select-method))
8270    ;; List of select methods.
8271    ((not (and (symbolp (car gnus-refer-article-method))
8272               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8273     (let (out)
8274       (dolist (method gnus-refer-article-method)
8275         (push (if (eq 'current method)
8276                   gnus-current-select-method
8277                 method)
8278               out))
8279       (nreverse out)))
8280    ;; One single select method.
8281    (t
8282     (list gnus-refer-article-method))))
8283
8284 (defun gnus-summary-edit-parameters ()
8285   "Edit the group parameters of the current group."
8286   (interactive)
8287   (gnus-group-edit-group gnus-newsgroup-name 'params))
8288
8289 (defun gnus-summary-customize-parameters ()
8290   "Customize the group parameters of the current group."
8291   (interactive)
8292   (gnus-group-customize gnus-newsgroup-name))
8293
8294 (defun gnus-summary-enter-digest-group (&optional force)
8295   "Enter an nndoc group based on the current article.
8296 If FORCE, force a digest interpretation.  If not, try
8297 to guess what the document format is."
8298   (interactive "P")
8299   (let ((conf gnus-current-window-configuration))
8300     (save-window-excursion
8301       (save-excursion
8302         (let (gnus-article-prepare-hook
8303               gnus-display-mime-function
8304               gnus-break-pages)
8305           (gnus-summary-select-article))))
8306     (setq gnus-current-window-configuration conf)
8307     (let* ((name (format "%s-%d"
8308                          (gnus-group-prefixed-name
8309                           gnus-newsgroup-name (list 'nndoc ""))
8310                          (with-current-buffer gnus-summary-buffer
8311                            gnus-current-article)))
8312            (ogroup gnus-newsgroup-name)
8313            (params (append (gnus-info-params (gnus-get-info ogroup))
8314                            (list (cons 'to-group ogroup))
8315                            (list (cons 'parent-group ogroup))
8316                            (list (cons 'save-article-group ogroup))))
8317            (case-fold-search t)
8318            (buf (current-buffer))
8319            dig to-address)
8320       (save-excursion
8321         (set-buffer gnus-original-article-buffer)
8322         ;; Have the digest group inherit the main mail address of
8323         ;; the parent article.
8324         (when (setq to-address (or (gnus-fetch-field "reply-to")
8325                                    (gnus-fetch-field "from")))
8326           (setq params (append
8327                         (list (cons 'to-address
8328                                     (funcall gnus-decode-encoded-word-function
8329                                              to-address))))))
8330         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8331         (insert-buffer-substring gnus-original-article-buffer)
8332         ;; Remove lines that may lead nndoc to misinterpret the
8333         ;; document type.
8334         (narrow-to-region
8335          (goto-char (point-min))
8336          (or (search-forward "\n\n" nil t) (point)))
8337         (goto-char (point-min))
8338         (delete-matching-lines "^Path:\\|^From ")
8339         (widen))
8340       (unwind-protect
8341           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8342                     (gnus-newsgroup-ephemeral-ignored-charsets
8343                      gnus-newsgroup-ignored-charsets))
8344                 (gnus-group-read-ephemeral-group
8345                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8346                               (nndoc-article-type
8347                                ,(if force 'mbox 'guess)))
8348                  t nil nil nil
8349                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8350                                                         "ADAPT")))))
8351               ;; Make all postings to this group go to the parent group.
8352               (nconc (gnus-info-params (gnus-get-info name))
8353                      params)
8354             ;; Couldn't select this doc group.
8355             (switch-to-buffer buf)
8356             (gnus-set-global-variables)
8357             (gnus-configure-windows 'summary)
8358             (gnus-message 3 "Article couldn't be entered?"))
8359         (kill-buffer dig)))))
8360
8361 (defun gnus-summary-read-document (n)
8362   "Open a new group based on the current article(s).
8363 This will allow you to read digests and other similar
8364 documents as newsgroups.
8365 Obeys the standard process/prefix convention."
8366   (interactive "P")
8367   (let* ((articles (gnus-summary-work-articles n))
8368          (ogroup gnus-newsgroup-name)
8369          (params (append (gnus-info-params (gnus-get-info ogroup))
8370                          (list (cons 'to-group ogroup))))
8371          article group egroup groups vgroup)
8372     (while (setq article (pop articles))
8373       (setq group (format "%s-%d" gnus-newsgroup-name article))
8374       (gnus-summary-remove-process-mark article)
8375       (when (gnus-summary-display-article article)
8376         (save-excursion
8377           (with-temp-buffer
8378             (insert-buffer-substring gnus-original-article-buffer)
8379             ;; Remove some headers that may lead nndoc to make
8380             ;; the wrong guess.
8381             (message-narrow-to-head)
8382             (goto-char (point-min))
8383             (delete-matching-lines "^Path:\\|^From ")
8384             (widen)
8385             (if (setq egroup
8386                       (gnus-group-read-ephemeral-group
8387                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8388                                      (nndoc-article-type guess))
8389                        t nil t))
8390                 (progn
8391             ;; Make all postings to this group go to the parent group.
8392                   (nconc (gnus-info-params (gnus-get-info egroup))
8393                          params)
8394                   (push egroup groups))
8395               ;; Couldn't select this doc group.
8396               (gnus-error 3 "Article couldn't be entered"))))))
8397     ;; Now we have selected all the documents.
8398     (cond
8399      ((not groups)
8400       (error "None of the articles could be interpreted as documents"))
8401      ((gnus-group-read-ephemeral-group
8402        (setq vgroup (format
8403                      "nnvirtual:%s-%s" gnus-newsgroup-name
8404                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8405        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8406        t
8407        (cons (current-buffer) 'summary)))
8408      (t
8409       (error "Couldn't select virtual nndoc group")))))
8410
8411 (defun gnus-summary-isearch-article (&optional regexp-p)
8412   "Do incremental search forward on the current article.
8413 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8414   (interactive "P")
8415   (gnus-summary-select-article)
8416   (gnus-configure-windows 'article)
8417   (gnus-eval-in-buffer-window gnus-article-buffer
8418     (save-restriction
8419       (widen)
8420       (isearch-forward regexp-p))))
8421
8422 (defun gnus-summary-search-article-forward (regexp &optional backward)
8423   "Search for an article containing REGEXP forward.
8424 If BACKWARD, search backward instead."
8425   (interactive
8426    (list (read-string
8427           (format "Search article %s (regexp%s): "
8428                   (if current-prefix-arg "backward" "forward")
8429                   (if gnus-last-search-regexp
8430                       (concat ", default " gnus-last-search-regexp)
8431                     "")))
8432          current-prefix-arg))
8433   (if (string-equal regexp "")
8434       (setq regexp (or gnus-last-search-regexp ""))
8435     (setq gnus-last-search-regexp regexp)
8436     (setq gnus-article-before-search gnus-current-article))
8437   ;; Intentionally set gnus-last-article.
8438   (setq gnus-last-article gnus-article-before-search)
8439   (let ((gnus-last-article gnus-last-article))
8440     (if (gnus-summary-search-article regexp backward)
8441         (gnus-summary-show-thread)
8442       (error "Search failed: \"%s\"" regexp))))
8443
8444 (defun gnus-summary-search-article-backward (regexp)
8445   "Search for an article containing REGEXP backward."
8446   (interactive
8447    (list (read-string
8448           (format "Search article backward (regexp%s): "
8449                   (if gnus-last-search-regexp
8450                       (concat ", default " gnus-last-search-regexp)
8451                     "")))))
8452   (gnus-summary-search-article-forward regexp 'backward))
8453
8454 (defun gnus-summary-search-article (regexp &optional backward)
8455   "Search for an article containing REGEXP.
8456 Optional argument BACKWARD means do search for backward.
8457 `gnus-select-article-hook' is not called during the search."
8458   ;; We have to require this here to make sure that the following
8459   ;; dynamic binding isn't shadowed by autoloading.
8460   (require 'gnus-async)
8461   (require 'gnus-art)
8462   (let ((gnus-select-article-hook nil)  ;Disable hook.
8463         (gnus-article-prepare-hook nil)
8464         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8465         (gnus-use-article-prefetch nil)
8466         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8467         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8468         (gnus-visual nil)
8469         (gnus-keep-backlog nil)
8470         (gnus-break-pages nil)
8471         (gnus-summary-display-arrow nil)
8472         (gnus-updated-mode-lines nil)
8473         (gnus-auto-center-summary nil)
8474         (sum (current-buffer))
8475         (gnus-display-mime-function nil)
8476         (found nil)
8477         point)
8478     (gnus-save-hidden-threads
8479       (gnus-summary-select-article)
8480       (set-buffer gnus-article-buffer)
8481       (goto-char (window-point (get-buffer-window (current-buffer))))
8482       (when backward
8483         (forward-line -1))
8484       (while (not found)
8485         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8486         (if (if backward
8487                 (re-search-backward regexp nil t)
8488               (re-search-forward regexp nil t))
8489             ;; We found the regexp.
8490             (progn
8491               (setq found 'found)
8492               (beginning-of-line)
8493               (set-window-start
8494                (get-buffer-window (current-buffer))
8495                (point))
8496               (forward-line 1)
8497               (set-window-point
8498                (get-buffer-window (current-buffer))
8499                (point))
8500               (set-buffer sum)
8501               (setq point (point)))
8502           ;; We didn't find it, so we go to the next article.
8503           (set-buffer sum)
8504           (setq found 'not)
8505           (while (eq found 'not)
8506             (if (not (if backward (gnus-summary-find-prev)
8507                        (gnus-summary-find-next)))
8508                 ;; No more articles.
8509                 (setq found t)
8510               ;; Select the next article and adjust point.
8511               (unless (gnus-summary-article-sparse-p
8512                        (gnus-summary-article-number))
8513                 (setq found nil)
8514                 (gnus-summary-select-article)
8515                 (set-buffer gnus-article-buffer)
8516                 (widen)
8517                 (goto-char (if backward (point-max) (point-min))))))))
8518       (gnus-message 7 ""))
8519     ;; Return whether we found the regexp.
8520     (when (eq found 'found)
8521       (goto-char point)
8522       (gnus-summary-show-thread)
8523       (gnus-summary-goto-subject gnus-current-article)
8524       (gnus-summary-position-point)
8525       t)))
8526
8527 (defun gnus-find-matching-articles (header regexp)
8528   "Return a list of all articles that match REGEXP on HEADER.
8529 This search includes all articles in the current group that Gnus has
8530 fetched headers for, whether they are displayed or not."
8531   (let ((articles nil)
8532         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8533         (case-fold-search t))
8534     (dolist (header gnus-newsgroup-headers)
8535       (when (string-match regexp (funcall func header))
8536         (push (mail-header-number header) articles)))
8537     (nreverse articles)))
8538
8539 (defun gnus-summary-find-matching (header regexp &optional backward unread
8540                                           not-case-fold not-matching)
8541   "Return a list of all articles that match REGEXP on HEADER.
8542 The search stars on the current article and goes forwards unless
8543 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8544 If UNREAD is non-nil, only unread articles will
8545 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8546 in the comparisons. If NOT-MATCHING, return a list of all articles that
8547 not match REGEXP on HEADER."
8548   (let ((case-fold-search (not not-case-fold))
8549         articles d func)
8550     (if (consp header)
8551         (if (eq (car header) 'extra)
8552             (setq func
8553                   `(lambda (h)
8554                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8555                          "")))
8556           (error "%s is an invalid header" header))
8557       (unless (fboundp (intern (concat "mail-header-" header)))
8558         (error "%s is not a valid header" header))
8559       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8560     (dolist (d (if (eq backward 'all)
8561                    gnus-newsgroup-data
8562                  (gnus-data-find-list
8563                   (gnus-summary-article-number)
8564                   (gnus-data-list backward))))
8565       (when (and (or (not unread)       ; We want all articles...
8566                      (gnus-data-unread-p d)) ; Or just unreads.
8567                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8568                  (if not-matching
8569                      (not (string-match
8570                            regexp
8571                            (funcall func (gnus-data-header d))))
8572                    (string-match regexp
8573                                  (funcall func (gnus-data-header d)))))
8574         (push (gnus-data-number d) articles))) ; Success!
8575     (nreverse articles)))
8576
8577 (defun gnus-summary-execute-command (header regexp command &optional backward)
8578   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8579 If HEADER is an empty string (or nil), the match is done on the entire
8580 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8581   (interactive
8582    (list (let ((completion-ignore-case t))
8583            (completing-read
8584             "Header name: "
8585             (mapcar (lambda (header) (list (format "%s" header)))
8586                     (append
8587                      '("Number" "Subject" "From" "Lines" "Date"
8588                        "Message-ID" "Xref" "References" "Body")
8589                      gnus-extra-headers))
8590             nil 'require-match))
8591          (read-string "Regexp: ")
8592          (read-key-sequence "Command: ")
8593          current-prefix-arg))
8594   (when (equal header "Body")
8595     (setq header ""))
8596   ;; Hidden thread subtrees must be searched as well.
8597   (gnus-summary-show-all-threads)
8598   ;; We don't want to change current point nor window configuration.
8599   (save-excursion
8600     (save-window-excursion
8601       (let (gnus-visual
8602             gnus-treat-strip-trailing-blank-lines
8603             gnus-treat-strip-leading-blank-lines
8604             gnus-treat-strip-multiple-blank-lines
8605             gnus-treat-hide-boring-headers
8606             gnus-treat-fold-newsgroups
8607             gnus-article-prepare-hook)
8608         (gnus-message 6 "Executing %s..." (key-description command))
8609         ;; We'd like to execute COMMAND interactively so as to give arguments.
8610         (gnus-execute header regexp
8611                       `(call-interactively ',(key-binding command))
8612                       backward)
8613         (gnus-message 6 "Executing %s...done" (key-description command))))))
8614
8615 (defun gnus-summary-beginning-of-article ()
8616   "Scroll the article back to the beginning."
8617   (interactive)
8618   (gnus-summary-select-article)
8619   (gnus-configure-windows 'article)
8620   (gnus-eval-in-buffer-window gnus-article-buffer
8621     (widen)
8622     (goto-char (point-min))
8623     (when gnus-break-pages
8624       (gnus-narrow-to-page))))
8625
8626 (defun gnus-summary-end-of-article ()
8627   "Scroll to the end of the article."
8628   (interactive)
8629   (gnus-summary-select-article)
8630   (gnus-configure-windows 'article)
8631   (gnus-eval-in-buffer-window gnus-article-buffer
8632     (widen)
8633     (goto-char (point-max))
8634     (recenter -3)
8635     (when gnus-break-pages
8636       (when (re-search-backward page-delimiter nil t)
8637         (narrow-to-region (match-end 0) (point-max)))
8638       (gnus-narrow-to-page))))
8639
8640 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8641   "Truncate to LEN and quote all \"(\"'s in STRING."
8642   (gnus-replace-in-string (if (and len (> (length string) len))
8643                               (substring string 0 len)
8644                             string)
8645                           "[()]" "\\\\\\&"))
8646
8647 (defun gnus-summary-print-article (&optional filename n)
8648   "Generate and print a PostScript image of the process-marked (mail) articles.
8649
8650 If used interactively, print the current article if none are
8651 process-marked.  With prefix arg, prompt the user for the name of the
8652 file to save in.
8653
8654 When used from Lisp, accept two optional args FILENAME and N.  N means
8655 to print the next N articles.  If N is negative, print the N previous
8656 articles.  If N is nil and articles have been marked with the process
8657 mark, print these instead.
8658
8659 If the optional first argument FILENAME is nil, send the image to the
8660 printer.  If FILENAME is a string, save the PostScript image in a file with
8661 that name.  If FILENAME is a number, prompt the user for the name of the file
8662 to save in."
8663   (interactive (list (ps-print-preprint current-prefix-arg)))
8664   (dolist (article (gnus-summary-work-articles n))
8665     (gnus-summary-select-article nil nil 'pseudo article)
8666     (gnus-eval-in-buffer-window gnus-article-buffer
8667       (gnus-print-buffer))
8668     (gnus-summary-remove-process-mark article))
8669   (ps-despool filename))
8670
8671 (defun gnus-print-buffer ()
8672   (let ((buffer (generate-new-buffer " *print*")))
8673     (unwind-protect
8674         (progn
8675           (copy-to-buffer buffer (point-min) (point-max))
8676           (set-buffer buffer)
8677           (gnus-remove-text-with-property 'gnus-decoration)
8678           (when (gnus-visual-p 'article-highlight 'highlight)
8679             ;; Copy-to-buffer doesn't copy overlay.  So redo
8680             ;; highlight.
8681             (let ((gnus-article-buffer buffer))
8682               (gnus-article-highlight-citation t)
8683               (gnus-article-highlight-signature)
8684               (gnus-article-emphasize)
8685               (gnus-article-delete-invisible-text)))
8686           (let ((ps-left-header
8687                  (list
8688                   (concat "("
8689                           (gnus-summary-print-truncate-and-quote
8690                            (mail-header-subject gnus-current-headers)
8691                            66) ")")
8692                   (concat "("
8693                           (gnus-summary-print-truncate-and-quote
8694                            (mail-header-from gnus-current-headers)
8695                            45) ")")))
8696                 (ps-right-header
8697                  (list
8698                   "/pagenumberstring load"
8699                   (concat "("
8700                           (mail-header-date gnus-current-headers) ")"))))
8701             (gnus-run-hooks 'gnus-ps-print-hook)
8702             (save-excursion
8703               (if window-system
8704                   (ps-spool-buffer-with-faces)
8705                 (ps-spool-buffer)))))
8706       (kill-buffer buffer))))
8707
8708 (defun gnus-summary-show-article (&optional arg)
8709   "Force redisplaying of the current article.
8710 If ARG (the prefix) is a number, show the article with the charset
8711 defined in `gnus-summary-show-article-charset-alist', or the charset
8712 input.
8713 If ARG (the prefix) is non-nil and not a number, show the raw article
8714 without any article massaging functions being run.  Normally, the key
8715 strokes are `C-u g'."
8716   (interactive "P")
8717   (cond
8718    ((numberp arg)
8719     (gnus-summary-show-article t)
8720     (let ((gnus-newsgroup-charset
8721            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8722                (mm-read-coding-system
8723                 "View as charset: " ;; actually it is coding system.
8724                 (save-excursion
8725                   (set-buffer gnus-article-buffer)
8726                   (mm-detect-coding-region (point) (point-max))))))
8727           (gnus-newsgroup-ignored-charsets 'gnus-all))
8728       (gnus-summary-select-article nil 'force)
8729       (let ((deps gnus-newsgroup-dependencies)
8730             head header lines)
8731         (save-excursion
8732           (set-buffer gnus-original-article-buffer)
8733           (save-restriction
8734             (message-narrow-to-head)
8735             (setq head (buffer-string))
8736             (goto-char (point-min))
8737             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8738               (goto-char (point-max))
8739               (widen)
8740               (setq lines (1- (count-lines (point) (point-max))))))
8741           (with-temp-buffer
8742             (insert (format "211 %d Article retrieved.\n"
8743                             (cdr gnus-article-current)))
8744             (insert head)
8745             (if lines (insert (format "Lines: %d\n" lines)))
8746             (insert ".\n")
8747             (let ((nntp-server-buffer (current-buffer)))
8748               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8749         (gnus-data-set-header
8750          (gnus-data-find (cdr gnus-article-current))
8751          header)
8752         (gnus-summary-update-article-line
8753          (cdr gnus-article-current) header)
8754         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8755           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8756    ((not arg)
8757     ;; Select the article the normal way.
8758     (gnus-summary-select-article nil 'force))
8759    (t
8760     ;; We have to require this here to make sure that the following
8761     ;; dynamic binding isn't shadowed by autoloading.
8762     (require 'gnus-async)
8763     (require 'gnus-art)
8764     ;; Bind the article treatment functions to nil.
8765     (let ((gnus-have-all-headers t)
8766           gnus-article-prepare-hook
8767           gnus-article-decode-hook
8768           gnus-display-mime-function
8769           gnus-break-pages)
8770       ;; Destroy any MIME parts.
8771       (when (gnus-buffer-live-p gnus-article-buffer)
8772         (save-excursion
8773           (set-buffer gnus-article-buffer)
8774           (mm-destroy-parts gnus-article-mime-handles)
8775           ;; Set it to nil for safety reason.
8776           (setq gnus-article-mime-handle-alist nil)
8777           (setq gnus-article-mime-handles nil)))
8778       (gnus-summary-select-article nil 'force))))
8779   (gnus-summary-goto-subject gnus-current-article)
8780   (gnus-summary-position-point))
8781
8782 (defun gnus-summary-show-raw-article ()
8783   "Show the raw article without any article massaging functions being run."
8784   (interactive)
8785   (gnus-summary-show-article t))
8786
8787 (defun gnus-summary-verbose-headers (&optional arg)
8788   "Toggle permanent full header display.
8789 If ARG is a positive number, turn header display on.
8790 If ARG is a negative number, turn header display off."
8791   (interactive "P")
8792   (setq gnus-show-all-headers
8793         (cond ((or (not (numberp arg))
8794                    (zerop arg))
8795                (not gnus-show-all-headers))
8796               ((natnump arg)
8797                t)))
8798   (gnus-summary-show-article))
8799
8800 (defun gnus-summary-toggle-header (&optional arg)
8801   "Show the headers if they are hidden, or hide them if they are shown.
8802 If ARG is a positive number, show the entire header.
8803 If ARG is a negative number, hide the unwanted header lines."
8804   (interactive "P")
8805   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8806                      (get-buffer-window gnus-article-buffer t))))
8807     (with-current-buffer gnus-article-buffer
8808       (widen)
8809       (article-narrow-to-head)
8810       (let* ((buffer-read-only nil)
8811              (inhibit-point-motion-hooks t)
8812              (hidden (if (numberp arg)
8813                          (>= arg 0)
8814                        (gnus-article-hidden-text-p 'headers)))
8815              s e)
8816         (delete-region (point-min) (point-max))
8817         (with-current-buffer gnus-original-article-buffer
8818           (goto-char (setq s (point-min)))
8819           (setq e (if (search-forward "\n\n" nil t)
8820                       (1- (point))
8821                     (point-max))))
8822         (insert-buffer-substring gnus-original-article-buffer s e)
8823         (run-hooks 'gnus-article-decode-hook)
8824         (if hidden
8825             (let ((gnus-treat-hide-headers nil)
8826                   (gnus-treat-hide-boring-headers nil))
8827               (gnus-delete-wash-type 'headers)
8828               (gnus-treat-article 'head))
8829           (gnus-treat-article 'head))
8830         (widen)
8831         (if window
8832             (set-window-start window (goto-char (point-min))))
8833         (if gnus-break-pages
8834             (gnus-narrow-to-page)
8835           (when (gnus-visual-p 'page-marker)
8836             (let ((buffer-read-only nil))
8837               (gnus-remove-text-with-property 'gnus-prev)
8838               (gnus-remove-text-with-property 'gnus-next))))
8839         (gnus-set-mode-line 'article)))))
8840
8841 (defun gnus-summary-show-all-headers ()
8842   "Make all header lines visible."
8843   (interactive)
8844   (gnus-summary-toggle-header 1))
8845
8846 (defun gnus-summary-caesar-message (&optional arg)
8847   "Caesar rotate the current article by 13.
8848 The numerical prefix specifies how many places to rotate each letter
8849 forward."
8850   (interactive "P")
8851   (gnus-summary-select-article)
8852   (let ((mail-header-separator ""))
8853     (gnus-eval-in-buffer-window gnus-article-buffer
8854       (save-restriction
8855         (widen)
8856         (let ((start (window-start))
8857               buffer-read-only)
8858           (message-caesar-buffer-body arg)
8859           (set-window-start (get-buffer-window (current-buffer)) start))))))
8860
8861 (autoload 'unmorse-region "morse"
8862   "Convert morse coded text in region to ordinary ASCII text."
8863   t)
8864
8865 (defun gnus-summary-morse-message (&optional arg)
8866   "Morse decode the current article."
8867   (interactive "P")
8868   (gnus-summary-select-article)
8869   (let ((mail-header-separator ""))
8870     (gnus-eval-in-buffer-window gnus-article-buffer
8871       (save-excursion
8872         (save-restriction
8873           (widen)
8874           (let ((pos (window-start))
8875                 buffer-read-only)
8876             (goto-char (point-min))
8877             (when (message-goto-body)
8878               (gnus-narrow-to-body))
8879             (goto-char (point-min))
8880             (while (re-search-forward "·" (point-max) t)
8881               (replace-match "."))
8882             (unmorse-region (point-min) (point-max))
8883             (widen)
8884             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8885
8886 (defun gnus-summary-stop-page-breaking ()
8887   "Stop page breaking in the current article."
8888   (interactive)
8889   (gnus-summary-select-article)
8890   (gnus-eval-in-buffer-window gnus-article-buffer
8891     (widen)
8892     (when (gnus-visual-p 'page-marker)
8893       (let ((buffer-read-only nil))
8894         (gnus-remove-text-with-property 'gnus-prev)
8895         (gnus-remove-text-with-property 'gnus-next))
8896       (setq gnus-page-broken nil))))
8897
8898 (defun gnus-summary-move-article (&optional n to-newsgroup
8899                                             select-method action)
8900   "Move the current article to a different newsgroup.
8901 If N is a positive number, move the N next articles.
8902 If N is a negative number, move the N previous articles.
8903 If N is nil and any articles have been marked with the process mark,
8904 move those articles instead.
8905 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8906 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8907 re-spool using this method.
8908
8909 When called interactively with TO-NEWSGROUP being nil, the value of
8910 the variable `gnus-move-split-methods' is used for finding a default
8911 for the target newsgroup.
8912
8913 For this function to work, both the current newsgroup and the
8914 newsgroup that you want to move to have to support the `request-move'
8915 and `request-accept' functions.
8916
8917 ACTION can be either `move' (the default), `crosspost' or `copy'."
8918   (interactive "P")
8919   (unless action
8920     (setq action 'move))
8921   ;; Check whether the source group supports the required functions.
8922   (cond ((and (eq action 'move)
8923               (not (gnus-check-backend-function
8924                     'request-move-article gnus-newsgroup-name)))
8925          (error "The current group does not support article moving"))
8926         ((and (eq action 'crosspost)
8927               (not (gnus-check-backend-function
8928                     'request-replace-article gnus-newsgroup-name)))
8929          (error "The current group does not support article editing")))
8930   (let ((articles (gnus-summary-work-articles n))
8931         (prefix (if (gnus-check-backend-function
8932                      'request-move-article gnus-newsgroup-name)
8933                     (funcall gnus-move-group-prefix-function
8934                              gnus-newsgroup-name)
8935                   ""))
8936         (names '((move "Move" "Moving")
8937                  (copy "Copy" "Copying")
8938                  (crosspost "Crosspost" "Crossposting")))
8939         (copy-buf (save-excursion
8940                     (nnheader-set-temp-buffer " *copy article*")))
8941         art-group to-method new-xref article to-groups)
8942     (unless (assq action names)
8943       (error "Unknown action %s" action))
8944     ;; Read the newsgroup name.
8945     (when (and (not to-newsgroup)
8946                (not select-method))
8947       (if (and gnus-move-split-methods
8948                (not
8949                 (and (memq gnus-current-article articles)
8950                      (gnus-buffer-live-p gnus-original-article-buffer))))
8951           ;; When `gnus-move-split-methods' is non-nil, we have to
8952           ;; select an article to give `gnus-read-move-group-name' an
8953           ;; opportunity to suggest an appropriate default.  However,
8954           ;; we needn't render or mark the article.
8955           (let ((gnus-display-mime-function nil)
8956                 (gnus-article-prepare-hook nil)
8957                 (gnus-mark-article-hook nil))
8958             (gnus-summary-select-article nil nil nil (car articles))))
8959       (setq to-newsgroup
8960             (gnus-read-move-group-name
8961              (cadr (assq action names))
8962              (symbol-value (intern (format "gnus-current-%s-group" action)))
8963              articles prefix))
8964       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8965     (setq to-method (or select-method
8966                         (gnus-server-to-method
8967                          (gnus-group-method to-newsgroup))))
8968     ;; Check the method we are to move this article to...
8969     (unless (gnus-check-backend-function
8970              'request-accept-article (car to-method))
8971       (error "%s does not support article copying" (car to-method)))
8972     (unless (gnus-check-server to-method)
8973       (error "Can't open server %s" (car to-method)))
8974     (gnus-message 6 "%s to %s: %s..."
8975                   (caddr (assq action names))
8976                   (or (car select-method) to-newsgroup) articles)
8977     (while articles
8978       (setq article (pop articles))
8979       (setq
8980        art-group
8981        (cond
8982         ;; Move the article.
8983         ((eq action 'move)
8984          ;; Remove this article from future suppression.
8985          (gnus-dup-unsuppress-article article)
8986          (gnus-request-move-article
8987           article                       ; Article to move
8988           gnus-newsgroup-name           ; From newsgroup
8989           (nth 1 (gnus-find-method-for-group
8990                   gnus-newsgroup-name)) ; Server
8991           (list 'gnus-request-accept-article
8992                 to-newsgroup (list 'quote select-method)
8993                 (not articles) t)       ; Accept form
8994           (not articles)))              ; Only save nov last time
8995         ;; Copy the article.
8996         ((eq action 'copy)
8997          (save-excursion
8998            (set-buffer copy-buf)
8999            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9000              (gnus-request-accept-article
9001               to-newsgroup select-method (not articles) t))))
9002         ;; Crosspost the article.
9003         ((eq action 'crosspost)
9004          (let ((xref (message-tokenize-header
9005                       (mail-header-xref (gnus-summary-article-header article))
9006                       " ")))
9007            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9008                                   ":" (number-to-string article)))
9009            (unless xref
9010              (setq xref (list (system-name))))
9011            (setq new-xref
9012                  (concat
9013                   (mapconcat 'identity
9014                              (delete "Xref:" (delete new-xref xref))
9015                              " ")
9016                   " " new-xref))
9017            (save-excursion
9018              (set-buffer copy-buf)
9019              ;; First put the article in the destination group.
9020              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9021              (when (consp (setq art-group
9022                                 (gnus-request-accept-article
9023                                  to-newsgroup select-method (not articles))))
9024                (setq new-xref (concat new-xref " " (car art-group)
9025                                       ":"
9026                                       (number-to-string (cdr art-group))))
9027                ;; Now we have the new Xrefs header, so we insert
9028                ;; it and replace the new article.
9029                (nnheader-replace-header "Xref" new-xref)
9030                (gnus-request-replace-article
9031                 (cdr art-group) to-newsgroup (current-buffer))
9032                art-group))))))
9033       (cond
9034        ((not art-group)
9035         (gnus-message 1 "Couldn't %s article %s: %s"
9036                       (cadr (assq action names)) article
9037                       (nnheader-get-report (car to-method))))
9038        ((eq art-group 'junk)
9039         (when (eq action 'move)
9040           (gnus-summary-mark-article article gnus-canceled-mark)
9041           (gnus-message 4 "Deleted article %s" article)
9042           ;; run the delete hook
9043           (run-hook-with-args 'gnus-summary-article-delete-hook
9044                               action
9045                               (gnus-data-header
9046                                (assoc article (gnus-data-list nil)))
9047                               gnus-newsgroup-name nil
9048                               select-method)))
9049        (t
9050         (let* ((pto-group (gnus-group-prefixed-name
9051                            (car art-group) to-method))
9052                (info (gnus-get-info pto-group))
9053                (to-group (gnus-info-group info))
9054                to-marks)
9055           ;; Update the group that has been moved to.
9056           (when (and info
9057                      (memq action '(move copy)))
9058             (unless (member to-group to-groups)
9059               (push to-group to-groups))
9060
9061             (unless (memq article gnus-newsgroup-unreads)
9062               (push 'read to-marks)
9063               (gnus-info-set-read
9064                info (gnus-add-to-range (gnus-info-read info)
9065                                        (list (cdr art-group)))))
9066
9067             ;; See whether the article is to be put in the cache.
9068             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9069                              gnus-article-mark-lists
9070                            (delete '(expirable . expire)
9071                                    (copy-sequence gnus-article-mark-lists))))
9072                   (to-article (cdr art-group)))
9073
9074               ;; Enter the article into the cache in the new group,
9075               ;; if that is required.
9076               (when gnus-use-cache
9077                 (gnus-cache-possibly-enter-article
9078                  to-group to-article
9079                  (memq article gnus-newsgroup-marked)
9080                  (memq article gnus-newsgroup-dormant)
9081                  (memq article gnus-newsgroup-unreads)))
9082
9083               (when gnus-preserve-marks
9084                 ;; Copy any marks over to the new group.
9085                 (when (and (equal to-group gnus-newsgroup-name)
9086                            (not (memq article gnus-newsgroup-unreads)))
9087                   ;; Mark this article as read in this group.
9088                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9089                   (setcdr (gnus-active to-group) to-article)
9090                   (setcdr gnus-newsgroup-active to-article))
9091
9092                 (while marks
9093                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9094                     (when (memq article (symbol-value
9095                                          (intern (format "gnus-newsgroup-%s"
9096                                                          (caar marks)))))
9097                       (push (cdar marks) to-marks)
9098                       ;; If the other group is the same as this group,
9099                       ;; then we have to add the mark to the list.
9100                       (when (equal to-group gnus-newsgroup-name)
9101                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9102                              (cons to-article
9103                                    (symbol-value
9104                                     (intern (format "gnus-newsgroup-%s"
9105                                                     (caar marks)))))))
9106                       ;; Copy the marks to other group.
9107                       (gnus-add-marked-articles
9108                        to-group (cdar marks) (list to-article) info)))
9109                   (setq marks (cdr marks)))
9110
9111                 (gnus-request-set-mark
9112                  to-group (list (list (list to-article) 'add to-marks))))
9113
9114               (gnus-dribble-enter
9115                (concat "(gnus-group-set-info '"
9116                        (gnus-prin1-to-string (gnus-get-info to-group))
9117                        ")"))))
9118
9119           ;; Update the Xref header in this article to point to
9120           ;; the new crossposted article we have just created.
9121           (when (eq action 'crosspost)
9122             (save-excursion
9123               (set-buffer copy-buf)
9124               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9125               (nnheader-replace-header "Xref" new-xref)
9126               (gnus-request-replace-article
9127                article gnus-newsgroup-name (current-buffer))))
9128
9129           ;; run the move/copy/crosspost/respool hook
9130           (run-hook-with-args 'gnus-summary-article-move-hook
9131                               action
9132                               (gnus-data-header
9133                                (assoc article (gnus-data-list nil)))
9134                               gnus-newsgroup-name
9135                               to-newsgroup
9136                               select-method))
9137
9138         ;;;!!!Why is this necessary?
9139         (set-buffer gnus-summary-buffer)
9140         
9141         (gnus-summary-goto-subject article)
9142         (when (eq action 'move)
9143           (gnus-summary-mark-article article gnus-canceled-mark))))
9144       (gnus-summary-remove-process-mark article))
9145     ;; Re-activate all groups that have been moved to.
9146     (save-excursion
9147       (set-buffer gnus-group-buffer)
9148       (let ((gnus-group-marked to-groups))
9149         (gnus-group-get-new-news-this-group nil t)))
9150
9151     (gnus-kill-buffer copy-buf)
9152     (gnus-summary-position-point)
9153     (gnus-set-mode-line 'summary)))
9154
9155 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9156   "Move the current article to a different newsgroup.
9157 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9158 When called interactively, if TO-NEWSGROUP is nil, use the value of
9159 the variable `gnus-move-split-methods' for finding a default target
9160 newsgroup.
9161 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9162 re-spool using this method."
9163   (interactive "P")
9164   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9165
9166 (defun gnus-summary-crosspost-article (&optional n)
9167   "Crosspost the current article to some other group."
9168   (interactive "P")
9169   (gnus-summary-move-article n nil nil 'crosspost))
9170
9171 (defcustom gnus-summary-respool-default-method nil
9172   "Default method type for respooling an article.
9173 If nil, use to the current newsgroup method."
9174   :type 'symbol
9175   :group 'gnus-summary-mail)
9176
9177 (defcustom gnus-summary-display-while-building nil
9178   "If non-nil, show and update the summary buffer as it's being built.
9179 If the value is t, update the buffer after every line is inserted.  If
9180 the value is an integer (N), update the display every N lines."
9181   :group 'gnus-thread
9182   :type '(choice (const :tag "off" nil)
9183                  number
9184                  (const :tag "frequently" t)))
9185
9186 (defun gnus-summary-respool-article (&optional n method)
9187   "Respool the current article.
9188 The article will be squeezed through the mail spooling process again,
9189 which means that it will be put in some mail newsgroup or other
9190 depending on `nnmail-split-methods'.
9191 If N is a positive number, respool the N next articles.
9192 If N is a negative number, respool the N previous articles.
9193 If N is nil and any articles have been marked with the process mark,
9194 respool those articles instead.
9195
9196 Respooling can be done both from mail groups and \"real\" newsgroups.
9197 In the former case, the articles in question will be moved from the
9198 current group into whatever groups they are destined to.  In the
9199 latter case, they will be copied into the relevant groups."
9200   (interactive
9201    (list current-prefix-arg
9202          (let* ((methods (gnus-methods-using 'respool))
9203                 (methname
9204                  (symbol-name (or gnus-summary-respool-default-method
9205                                   (car (gnus-find-method-for-group
9206                                         gnus-newsgroup-name)))))
9207                 (method
9208                  (gnus-completing-read-with-default
9209                   methname "What backend do you want to use when respooling?"
9210                   methods nil t nil 'gnus-mail-method-history))
9211                 ms)
9212            (cond
9213             ((zerop (length (setq ms (gnus-servers-using-backend
9214                                       (intern method)))))
9215              (list (intern method) ""))
9216             ((= 1 (length ms))
9217              (car ms))
9218             (t
9219              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9220                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9221                            ms-alist))))))))
9222   (unless method
9223     (error "No method given for respooling"))
9224   (if (assoc (symbol-name
9225               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9226              (gnus-methods-using 'respool))
9227       (gnus-summary-move-article n nil method)
9228     (gnus-summary-copy-article n nil method)))
9229
9230 (defun gnus-summary-import-article (file &optional edit)
9231   "Import an arbitrary file into a mail newsgroup."
9232   (interactive "fImport file: \nP")
9233   (let ((group gnus-newsgroup-name)
9234         (now (current-time))
9235         atts lines group-art)
9236     (unless (gnus-check-backend-function 'request-accept-article group)
9237       (error "%s does not support article importing" group))
9238     (or (file-readable-p file)
9239         (not (file-regular-p file))
9240         (error "Can't read %s" file))
9241     (save-excursion
9242       (set-buffer (gnus-get-buffer-create " *import file*"))
9243       (erase-buffer)
9244       (nnheader-insert-file-contents file)
9245       (goto-char (point-min))
9246       (if (nnheader-article-p)
9247           (save-restriction
9248             (goto-char (point-min))
9249             (search-forward "\n\n" nil t)
9250             (narrow-to-region (point-min) (1- (point)))
9251             (goto-char (point-min))
9252             (unless (re-search-forward "^date:" nil t)
9253               (goto-char (point-max))
9254               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9255        ;; This doesn't look like an article, so we fudge some headers.
9256         (setq atts (file-attributes file)
9257               lines (count-lines (point-min) (point-max)))
9258         (insert "From: " (read-string "From: ") "\n"
9259                 "Subject: " (read-string "Subject: ") "\n"
9260                 "Date: " (message-make-date (nth 5 atts)) "\n"
9261                 "Message-ID: " (message-make-message-id) "\n"
9262                 "Lines: " (int-to-string lines) "\n"
9263                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9264       (setq group-art (gnus-request-accept-article group nil t))
9265       (kill-buffer (current-buffer)))
9266     (setq gnus-newsgroup-active (gnus-activate-group group))
9267     (forward-line 1)
9268     (gnus-summary-goto-article (cdr group-art) nil t)
9269     (when edit
9270       (gnus-summary-edit-article))))
9271
9272 (defun gnus-summary-create-article ()
9273   "Create an article in a mail newsgroup."
9274   (interactive)
9275   (let ((group gnus-newsgroup-name)
9276         (now (current-time))
9277         group-art)
9278     (unless (gnus-check-backend-function 'request-accept-article group)
9279       (error "%s does not support article importing" group))
9280     (save-excursion
9281       (set-buffer (gnus-get-buffer-create " *import file*"))
9282       (erase-buffer)
9283       (goto-char (point-min))
9284       ;; This doesn't look like an article, so we fudge some headers.
9285       (insert "From: " (read-string "From: ") "\n"
9286               "Subject: " (read-string "Subject: ") "\n"
9287               "Date: " (message-make-date now) "\n"
9288               "Message-ID: " (message-make-message-id) "\n")
9289       (setq group-art (gnus-request-accept-article group nil t))
9290       (kill-buffer (current-buffer)))
9291     (setq gnus-newsgroup-active (gnus-activate-group group))
9292     (forward-line 1)
9293     (gnus-summary-goto-article (cdr group-art) nil t)
9294     (gnus-summary-edit-article)))
9295
9296 (defun gnus-summary-article-posted-p ()
9297   "Say whether the current (mail) article is available from news as well.
9298 This will be the case if the article has both been mailed and posted."
9299   (interactive)
9300   (let ((id (mail-header-references (gnus-summary-article-header)))
9301         (gnus-override-method (car (gnus-refer-article-methods))))
9302     (if (gnus-request-head id "")
9303         (gnus-message 2 "The current message was found on %s"
9304                       gnus-override-method)
9305       (gnus-message 2 "The current message couldn't be found on %s"
9306                     gnus-override-method)
9307       nil)))
9308
9309 (defun gnus-summary-expire-articles (&optional now)
9310   "Expire all articles that are marked as expirable in the current group."
9311   (interactive)
9312   (when (and (not gnus-group-is-exiting-without-update-p)
9313              (gnus-check-backend-function
9314               'request-expire-articles gnus-newsgroup-name))
9315     ;; This backend supports expiry.
9316     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9317            (expirable (if total
9318                           (progn
9319                             ;; We need to update the info for
9320                             ;; this group for `gnus-list-of-read-articles'
9321                             ;; to give us the right answer.
9322                             (gnus-run-hooks 'gnus-exit-group-hook)
9323                             (gnus-summary-update-info)
9324                             (gnus-list-of-read-articles gnus-newsgroup-name))
9325                         (setq gnus-newsgroup-expirable
9326                               (sort gnus-newsgroup-expirable '<))))
9327            (expiry-wait (if now 'immediate
9328                           (gnus-group-find-parameter
9329                            gnus-newsgroup-name 'expiry-wait)))
9330            (nnmail-expiry-target
9331             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9332                 nnmail-expiry-target))
9333            es)
9334       (when expirable
9335         ;; There are expirable articles in this group, so we run them
9336         ;; through the expiry process.
9337         (gnus-message 6 "Expiring articles...")
9338         (unless (gnus-check-group gnus-newsgroup-name)
9339           (error "Can't open server for %s" gnus-newsgroup-name))
9340         ;; The list of articles that weren't expired is returned.
9341         (save-excursion
9342           (if expiry-wait
9343               (let ((nnmail-expiry-wait-function nil)
9344                     (nnmail-expiry-wait expiry-wait))
9345                 (setq es (gnus-request-expire-articles
9346                           expirable gnus-newsgroup-name)))
9347             (setq es (gnus-request-expire-articles
9348                       expirable gnus-newsgroup-name)))
9349           (unless total
9350             (setq gnus-newsgroup-expirable es))
9351           ;; We go through the old list of expirable, and mark all
9352           ;; really expired articles as nonexistent.
9353           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9354             (let ((gnus-use-cache nil))
9355               (dolist (article expirable)
9356                 (when (and (not (memq article es))
9357                            (gnus-data-find article))
9358                   (gnus-summary-mark-article article gnus-canceled-mark)
9359                   (run-hook-with-args 'gnus-summary-article-expire-hook
9360                                       'delete
9361                                       (gnus-data-header
9362                                        (assoc article (gnus-data-list nil)))
9363                                       gnus-newsgroup-name
9364                                       nil
9365                                       nil))))))
9366         (gnus-message 6 "Expiring articles...done")))))
9367
9368 (defun gnus-summary-expire-articles-now ()
9369   "Expunge all expirable articles in the current group.
9370 This means that *all* articles that are marked as expirable will be
9371 deleted forever, right now."
9372   (interactive)
9373   (or gnus-expert-user
9374       (gnus-yes-or-no-p
9375        "Are you really, really, really sure you want to delete all these messages? ")
9376       (error "Phew!"))
9377   (gnus-summary-expire-articles t))
9378
9379 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9380 (defun gnus-summary-delete-article (&optional n)
9381   "Delete the N next (mail) articles.
9382 This command actually deletes articles.  This is not a marking
9383 command.  The article will disappear forever from your life, never to
9384 return.
9385
9386 If N is negative, delete backwards.
9387 If N is nil and articles have been marked with the process mark,
9388 delete these instead.
9389
9390 If `gnus-novice-user' is non-nil you will be asked for
9391 confirmation before the articles are deleted."
9392   (interactive "P")
9393   (unless (gnus-check-backend-function 'request-expire-articles
9394                                        gnus-newsgroup-name)
9395     (error "The current newsgroup does not support article deletion"))
9396   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9397     (error "Couldn't open server"))
9398   ;; Compute the list of articles to delete.
9399   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9400         (nnmail-expiry-target 'delete)
9401         not-deleted)
9402     (if (and gnus-novice-user
9403              (not (gnus-yes-or-no-p
9404                    (format "Do you really want to delete %s forever? "
9405                            (if (> (length articles) 1)
9406                                (format "these %s articles" (length articles))
9407                              "this article")))))
9408         ()
9409       ;; Delete the articles.
9410       (setq not-deleted (gnus-request-expire-articles
9411                          articles gnus-newsgroup-name 'force))
9412       (while articles
9413         (gnus-summary-remove-process-mark (car articles))
9414         ;; The backend might not have been able to delete the article
9415         ;; after all.
9416         (unless (memq (car articles) not-deleted)
9417           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9418         (let* ((article (car articles))
9419                (ghead  (gnus-data-header
9420                                     (assoc article (gnus-data-list nil)))))
9421           (run-hook-with-args 'gnus-summary-article-delete-hook
9422                               'delete ghead gnus-newsgroup-name nil
9423                               nil))
9424         (setq articles (cdr articles)))
9425       (when not-deleted
9426         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9427     (gnus-summary-position-point)
9428     (gnus-set-mode-line 'summary)
9429     not-deleted))
9430
9431 (defun gnus-summary-edit-article (&optional arg)
9432   "Edit the current article.
9433 This will have permanent effect only in mail groups.
9434 If ARG is nil, edit the decoded articles.
9435 If ARG is 1, edit the raw articles.
9436 If ARG is 2, edit the raw articles even in read-only groups.
9437 If ARG is 3, edit the articles with the current handles.
9438 Otherwise, allow editing of articles even in read-only
9439 groups."
9440   (interactive "P")
9441   (let (force raw current-handles)
9442     (cond
9443      ((null arg))
9444      ((eq arg 1)
9445       (setq raw t))
9446      ((eq arg 2)
9447       (setq raw t
9448             force t))
9449      ((eq arg 3)
9450       (setq current-handles
9451             (and (gnus-buffer-live-p gnus-article-buffer)
9452                  (with-current-buffer gnus-article-buffer
9453                    (prog1
9454                        gnus-article-mime-handles
9455                      (setq gnus-article-mime-handles nil))))))
9456      (t
9457       (setq force t)))
9458     (when (and raw (not force)
9459                (member gnus-newsgroup-name '("nndraft:delayed"
9460                                              "nndraft:drafts"
9461                                              "nndraft:queue")))
9462       (error "Can't edit the raw article in group %s"
9463              gnus-newsgroup-name))
9464     (save-excursion
9465       (set-buffer gnus-summary-buffer)
9466       (let ((mail-parse-charset gnus-newsgroup-charset)
9467             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9468         (gnus-set-global-variables)
9469         (when (and (not force)
9470                    (gnus-group-read-only-p))
9471           (error "The current newsgroup does not support article editing"))
9472         (gnus-summary-show-article t)
9473         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9474           (with-current-buffer gnus-article-buffer
9475             (mm-enable-multibyte)))
9476         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9477             (setq raw t))
9478         (gnus-article-edit-article
9479          (if raw 'ignore
9480            `(lambda ()
9481               (let ((mbl mml-buffer-list))
9482                 (setq mml-buffer-list nil)
9483                 (mime-to-mml ,'current-handles)
9484                 (let ((mbl1 mml-buffer-list))
9485                   (setq mml-buffer-list mbl)
9486                   (set (make-local-variable 'mml-buffer-list) mbl1))
9487                 (gnus-make-local-hook 'kill-buffer-hook)
9488                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9489          `(lambda (no-highlight)
9490             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9491                   (message-options message-options)
9492                   (message-options-set-recipient)
9493                   (mail-parse-ignored-charsets
9494                    ',gnus-newsgroup-ignored-charsets))
9495               ,(if (not raw) '(progn
9496                                 (mml-to-mime)
9497                                 (mml-destroy-buffers)
9498                                 (remove-hook 'kill-buffer-hook
9499                                              'mml-destroy-buffers t)
9500                                 (kill-local-variable 'mml-buffer-list)))
9501               (gnus-summary-edit-article-done
9502                ,(or (mail-header-references gnus-current-headers) "")
9503                ,(gnus-group-read-only-p)
9504                ,gnus-summary-buffer no-highlight))))))))
9505
9506 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9507
9508 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9509                                                  no-highlight)
9510   "Make edits to the current article permanent."
9511   (interactive)
9512   (save-excursion
9513    ;; The buffer restriction contains the entire article if it exists.
9514     (when (article-goto-body)
9515       (let ((lines (count-lines (point) (point-max)))
9516             (length (- (point-max) (point)))
9517             (case-fold-search t)
9518             (body (copy-marker (point))))
9519         (goto-char (point-min))
9520         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9521           (delete-region (match-beginning 1) (match-end 1))
9522           (insert (number-to-string length)))
9523         (goto-char (point-min))
9524         (when (re-search-forward
9525                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9526           (delete-region (match-beginning 1) (match-end 1))
9527           (insert (number-to-string length)))
9528         (goto-char (point-min))
9529         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9530           (delete-region (match-beginning 1) (match-end 1))
9531           (insert (number-to-string lines))))))
9532   ;; Replace the article.
9533   (let ((buf (current-buffer)))
9534     (with-temp-buffer
9535       (insert-buffer-substring buf)
9536
9537       (if (and (not read-only)
9538                (not (gnus-request-replace-article
9539                      (cdr gnus-article-current) (car gnus-article-current)
9540                      (current-buffer) t)))
9541           (error "Couldn't replace article")
9542         ;; Update the summary buffer.
9543         (if (and references
9544                  (equal (message-tokenize-header references " ")
9545                         (message-tokenize-header
9546                          (or (message-fetch-field "references") "") " ")))
9547             ;; We only have to update this line.
9548             (save-excursion
9549               (save-restriction
9550                 (message-narrow-to-head)
9551                 (let ((head (buffer-string))
9552                       header)
9553                   (with-temp-buffer
9554                     (insert (format "211 %d Article retrieved.\n"
9555                                     (cdr gnus-article-current)))
9556                     (insert head)
9557                     (insert ".\n")
9558                     (let ((nntp-server-buffer (current-buffer)))
9559                       (setq header (car (gnus-get-newsgroup-headers
9560                                          nil t))))
9561                     (save-excursion
9562                       (set-buffer gnus-summary-buffer)
9563                       (gnus-data-set-header
9564                        (gnus-data-find (cdr gnus-article-current))
9565                        header)
9566                       (gnus-summary-update-article-line
9567                        (cdr gnus-article-current) header)
9568                       (if (gnus-summary-goto-subject
9569                            (cdr gnus-article-current) nil t)
9570                           (gnus-summary-update-secondary-mark
9571                            (cdr gnus-article-current))))))))
9572           ;; Update threads.
9573           (set-buffer (or buffer gnus-summary-buffer))
9574           (gnus-summary-update-article (cdr gnus-article-current))
9575           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9576               (gnus-summary-update-secondary-mark
9577                (cdr gnus-article-current))))
9578         ;; Prettify the article buffer again.
9579         (unless no-highlight
9580           (save-excursion
9581             (set-buffer gnus-article-buffer)
9582             ;;;!!! Fix this -- article should be rehighlighted.
9583             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9584             (set-buffer gnus-original-article-buffer)
9585             (gnus-request-article
9586              (cdr gnus-article-current)
9587              (car gnus-article-current) (current-buffer))))
9588         ;; Prettify the summary buffer line.
9589         (when (gnus-visual-p 'summary-highlight 'highlight)
9590           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9591
9592 (defun gnus-summary-edit-wash (key)
9593   "Perform editing command KEY in the article buffer."
9594   (interactive
9595    (list
9596     (progn
9597       (message "%s" (concat (this-command-keys) "- "))
9598       (read-char))))
9599   (message "")
9600   (gnus-summary-edit-article)
9601   (execute-kbd-macro (concat (this-command-keys) key))
9602   (gnus-article-edit-done))
9603
9604 ;;; Respooling
9605
9606 (defun gnus-summary-respool-query (&optional silent trace)
9607   "Query where the respool algorithm would put this article."
9608   (interactive)
9609   (let (gnus-mark-article-hook)
9610     (gnus-summary-select-article)
9611     (save-excursion
9612       (set-buffer gnus-original-article-buffer)
9613       (let ((groups (nnmail-article-group 'identity trace)))
9614         (unless silent
9615           (if groups
9616               (message "This message would go to %s"
9617                        (mapconcat 'car groups ", "))
9618             (message "This message would go to no groups"))
9619           groups)))))
9620
9621 (defun gnus-summary-respool-trace ()
9622   "Trace where the respool algorithm would put this article.
9623 Display a buffer showing all fancy splitting patterns which matched."
9624   (interactive)
9625   (gnus-summary-respool-query nil t))
9626
9627 ;; Summary marking commands.
9628
9629 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9630   "Mark articles which has the same subject as read, and then select the next.
9631 If UNMARK is positive, remove any kind of mark.
9632 If UNMARK is negative, tick articles."
9633   (interactive "P")
9634   (when unmark
9635     (setq unmark (prefix-numeric-value unmark)))
9636   (let ((count
9637          (gnus-summary-mark-same-subject
9638           (gnus-summary-article-subject) unmark)))
9639     ;; Select next unread article.  If auto-select-same mode, should
9640     ;; select the first unread article.
9641     (gnus-summary-next-article t (and gnus-auto-select-same
9642                                       (gnus-summary-article-subject)))
9643     (gnus-message 7 "%d article%s marked as %s"
9644                   count (if (= count 1) " is" "s are")
9645                   (if unmark "unread" "read"))))
9646
9647 (defun gnus-summary-kill-same-subject (&optional unmark)
9648   "Mark articles which has the same subject as read.
9649 If UNMARK is positive, remove any kind of mark.
9650 If UNMARK is negative, tick articles."
9651   (interactive "P")
9652   (when unmark
9653     (setq unmark (prefix-numeric-value unmark)))
9654   (let ((count
9655          (gnus-summary-mark-same-subject
9656           (gnus-summary-article-subject) unmark)))
9657     ;; If marked as read, go to next unread subject.
9658     (when (null unmark)
9659       ;; Go to next unread subject.
9660       (gnus-summary-next-subject 1 t))
9661     (gnus-message 7 "%d articles are marked as %s"
9662                   count (if unmark "unread" "read"))))
9663
9664 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9665   "Mark articles with same SUBJECT as read, and return marked number.
9666 If optional argument UNMARK is positive, remove any kinds of marks.
9667 If optional argument UNMARK is negative, mark articles as unread instead."
9668   (let ((count 1))
9669     (save-excursion
9670       (cond
9671        ((null unmark)                   ; Mark as read.
9672         (while (and
9673                 (progn
9674                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9675                   (gnus-summary-show-thread) t)
9676                 (gnus-summary-find-subject subject))
9677           (setq count (1+ count))))
9678        ((> unmark 0)                    ; Tick.
9679         (while (and
9680                 (progn
9681                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9682                   (gnus-summary-show-thread) t)
9683                 (gnus-summary-find-subject subject))
9684           (setq count (1+ count))))
9685        (t                               ; Mark as unread.
9686         (while (and
9687                 (progn
9688                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9689                   (gnus-summary-show-thread) t)
9690                 (gnus-summary-find-subject subject))
9691           (setq count (1+ count)))))
9692       (gnus-set-mode-line 'summary)
9693       ;; Return the number of marked articles.
9694       count)))
9695
9696 (defun gnus-summary-mark-as-processable (n &optional unmark)
9697   "Set the process mark on the next N articles.
9698 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9699 the process mark instead.  The difference between N and the actual
9700 number of articles marked is returned."
9701   (interactive "P")
9702   (if (and (null n) (gnus-region-active-p))
9703       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9704     (setq n (prefix-numeric-value n))
9705     (let ((backward (< n 0))
9706           (n (abs n)))
9707       (while (and
9708               (> n 0)
9709               (if unmark
9710                   (gnus-summary-remove-process-mark
9711                    (gnus-summary-article-number))
9712                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9713               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9714         (setq n (1- n)))
9715       (when (/= 0 n)
9716         (gnus-message 7 "No more articles"))
9717       (gnus-summary-recenter)
9718       (gnus-summary-position-point)
9719       n)))
9720
9721 (defun gnus-summary-unmark-as-processable (n)
9722   "Remove the process mark from the next N articles.
9723 If N is negative, unmark backward instead.  The difference between N and
9724 the actual number of articles unmarked is returned."
9725   (interactive "P")
9726   (gnus-summary-mark-as-processable n t))
9727
9728 (defun gnus-summary-unmark-all-processable ()
9729   "Remove the process mark from all articles."
9730   (interactive)
9731   (save-excursion
9732     (while gnus-newsgroup-processable
9733       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9734   (gnus-summary-position-point))
9735
9736 (defun gnus-summary-add-mark (article type)
9737   "Mark ARTICLE with a mark of TYPE."
9738   (let ((vtype (car (assq type gnus-article-mark-lists)))
9739         var)
9740     (if (not vtype)
9741         (error "No such mark type: %s" type)
9742       (setq var (intern (format "gnus-newsgroup-%s" type)))
9743       (set var (cons article (symbol-value var)))
9744       (if (memq type '(processable cached replied forwarded recent saved))
9745           (gnus-summary-update-secondary-mark article)
9746         ;;; !!! This is bogus.  We should find out what primary
9747         ;;; !!! mark we want to set.
9748         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9749
9750 (defun gnus-summary-mark-as-expirable (n)
9751   "Mark N articles forward as expirable.
9752 If N is negative, mark backward instead.  The difference between N and
9753 the actual number of articles marked is returned."
9754   (interactive "p")
9755   (gnus-summary-mark-forward n gnus-expirable-mark))
9756
9757 (defun gnus-summary-mark-as-spam (n)
9758   "Mark N articles forward as spam.
9759 If N is negative, mark backward instead.  The difference between N and
9760 the actual number of articles marked is returned."
9761   (interactive "p")
9762   (gnus-summary-mark-forward n gnus-spam-mark))
9763
9764 (defun gnus-summary-mark-article-as-replied (article)
9765   "Mark ARTICLE as replied to and update the summary line.
9766 ARTICLE can also be a list of articles."
9767   (interactive (list (gnus-summary-article-number)))
9768   (let ((articles (if (listp article) article (list article))))
9769     (dolist (article articles)
9770       (unless (numberp article)
9771         (error "%s is not a number" article))
9772       (push article gnus-newsgroup-replied)
9773       (let ((buffer-read-only nil))
9774         (when (gnus-summary-goto-subject article nil t)
9775           (gnus-summary-update-secondary-mark article))))))
9776
9777 (defun gnus-summary-mark-article-as-forwarded (article)
9778   "Mark ARTICLE as forwarded and update the summary line.
9779 ARTICLE can also be a list of articles."
9780   (let ((articles (if (listp article) article (list article))))
9781     (dolist (article articles)
9782       (push article gnus-newsgroup-forwarded)
9783       (let ((buffer-read-only nil))
9784         (when (gnus-summary-goto-subject article nil t)
9785           (gnus-summary-update-secondary-mark article))))))
9786
9787 (defun gnus-summary-set-bookmark (article)
9788   "Set a bookmark in current article."
9789   (interactive (list (gnus-summary-article-number)))
9790   (when (or (not (get-buffer gnus-article-buffer))
9791             (not gnus-current-article)
9792             (not gnus-article-current)
9793             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9794     (error "No current article selected"))
9795   ;; Remove old bookmark, if one exists.
9796   (gnus-pull article gnus-newsgroup-bookmarks)
9797   ;; Set the new bookmark, which is on the form
9798   ;; (article-number . line-number-in-body).
9799   (push
9800    (cons article
9801          (with-current-buffer gnus-article-buffer
9802            (count-lines
9803             (min (point)
9804                  (save-excursion
9805                    (article-goto-body)
9806                    (point)))
9807             (point))))
9808    gnus-newsgroup-bookmarks)
9809   (gnus-message 6 "A bookmark has been added to the current article."))
9810
9811 (defun gnus-summary-remove-bookmark (article)
9812   "Remove the bookmark from the current article."
9813   (interactive (list (gnus-summary-article-number)))
9814   ;; Remove old bookmark, if one exists.
9815   (if (not (assq article gnus-newsgroup-bookmarks))
9816       (gnus-message 6 "No bookmark in current article.")
9817     (gnus-pull article gnus-newsgroup-bookmarks)
9818     (gnus-message 6 "Removed bookmark.")))
9819
9820 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9821 (defun gnus-summary-mark-as-dormant (n)
9822   "Mark N articles forward as dormant.
9823 If N is negative, mark backward instead.  The difference between N and
9824 the actual number of articles marked is returned."
9825   (interactive "p")
9826   (gnus-summary-mark-forward n gnus-dormant-mark))
9827
9828 (defun gnus-summary-set-process-mark (article)
9829   "Set the process mark on ARTICLE and update the summary line."
9830   (setq gnus-newsgroup-processable
9831         (cons article
9832               (delq article gnus-newsgroup-processable)))
9833   (when (gnus-summary-goto-subject article)
9834     (gnus-summary-show-thread)
9835     (gnus-summary-goto-subject article)
9836     (gnus-summary-update-secondary-mark article)))
9837
9838 (defun gnus-summary-remove-process-mark (article)
9839   "Remove the process mark from ARTICLE and update the summary line."
9840   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9841   (when (gnus-summary-goto-subject article)
9842     (gnus-summary-show-thread)
9843     (gnus-summary-goto-subject article)
9844     (gnus-summary-update-secondary-mark article)))
9845
9846 (defun gnus-summary-set-saved-mark (article)
9847   "Set the process mark on ARTICLE and update the summary line."
9848   (push article gnus-newsgroup-saved)
9849   (when (gnus-summary-goto-subject article)
9850     (gnus-summary-update-secondary-mark article)))
9851
9852 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9853   "Mark N articles as read forwards.
9854 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9855 The difference between N and the actual number of articles marked is
9856 returned.
9857 If NO-EXPIRE, auto-expiry will be inhibited."
9858   (interactive "p")
9859   (gnus-summary-show-thread)
9860   (let ((backward (< n 0))
9861         (gnus-summary-goto-unread
9862          (and gnus-summary-goto-unread
9863               (not (eq gnus-summary-goto-unread 'never))
9864               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9865                                     gnus-ticked-mark gnus-dormant-mark)))))
9866         (n (abs n))
9867         (mark (or mark gnus-del-mark)))
9868     (while (and (> n 0)
9869                 (gnus-summary-mark-article nil mark no-expire)
9870                 (zerop (gnus-summary-next-subject
9871                         (if backward -1 1)
9872                         (and gnus-summary-goto-unread
9873                              (not (eq gnus-summary-goto-unread 'never)))
9874                         t)))
9875       (setq n (1- n)))
9876     (when (/= 0 n)
9877       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9878     (gnus-summary-recenter)
9879     (gnus-summary-position-point)
9880     (gnus-set-mode-line 'summary)
9881     n))
9882
9883 (defun gnus-summary-mark-article-as-read (mark)
9884   "Mark the current article quickly as read with MARK."
9885   (let ((article (gnus-summary-article-number)))
9886     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9887     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9888     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9890     (push (cons article mark) gnus-newsgroup-reads)
9891     ;; Possibly remove from cache, if that is used.
9892     (when gnus-use-cache
9893       (gnus-cache-enter-remove-article article))
9894     ;; Allow the backend to change the mark.
9895     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9896     ;; Check for auto-expiry.
9897     (when (and gnus-newsgroup-auto-expire
9898                (memq mark gnus-auto-expirable-marks))
9899       (setq mark gnus-expirable-mark)
9900       ;; Let the backend know about the mark change.
9901       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9902       (push article gnus-newsgroup-expirable))
9903     ;; Set the mark in the buffer.
9904     (gnus-summary-update-mark mark 'unread)
9905     t))
9906
9907 (defun gnus-summary-mark-article-as-unread (mark)
9908   "Mark the current article quickly as unread with MARK."
9909   (let* ((article (gnus-summary-article-number))
9910          (old-mark (gnus-summary-article-mark article)))
9911     ;; Allow the backend to change the mark.
9912     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9913     (if (eq mark old-mark)
9914         t
9915       (if (<= article 0)
9916           (progn
9917             (gnus-error 1 "Can't mark negative article numbers")
9918             nil)
9919         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9920         (setq gnus-newsgroup-spam-marked
9921               (delq article gnus-newsgroup-spam-marked))
9922         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9923         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9924         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9925         (cond ((= mark gnus-ticked-mark)
9926                (setq gnus-newsgroup-marked
9927                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9928                                               article)))
9929               ((= mark gnus-spam-mark)
9930                (setq gnus-newsgroup-spam-marked
9931                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9932                                               article)))
9933               ((= mark gnus-dormant-mark)
9934                (setq gnus-newsgroup-dormant
9935                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9936                                               article)))
9937               (t
9938                (setq gnus-newsgroup-unreads
9939                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9940                                               article))))
9941         (gnus-pull article gnus-newsgroup-reads)
9942
9943         ;; See whether the article is to be put in the cache.
9944         (and gnus-use-cache
9945              (vectorp (gnus-summary-article-header article))
9946              (save-excursion
9947                (gnus-cache-possibly-enter-article
9948                 gnus-newsgroup-name article
9949                 (= mark gnus-ticked-mark)
9950                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9951
9952         ;; Fix the mark.
9953         (gnus-summary-update-mark mark 'unread)
9954         t))))
9955
9956 (defun gnus-summary-mark-article (&optional article mark no-expire)
9957   "Mark ARTICLE with MARK.  MARK can be any character.
9958 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9959 `??' (dormant) and `?E' (expirable).
9960 If MARK is nil, then the default character `?r' is used.
9961 If ARTICLE is nil, then the article on the current line will be
9962 marked.
9963 If NO-EXPIRE, auto-expiry will be inhibited."
9964   ;; The mark might be a string.
9965   (when (stringp mark)
9966     (setq mark (aref mark 0)))
9967   ;; If no mark is given, then we check auto-expiring.
9968   (when (null mark)
9969     (setq mark gnus-del-mark))
9970   (when (and (not no-expire)
9971              gnus-newsgroup-auto-expire
9972              (memq mark gnus-auto-expirable-marks))
9973     (setq mark gnus-expirable-mark))
9974   (let ((article (or article (gnus-summary-article-number)))
9975         (old-mark (gnus-summary-article-mark article)))
9976     ;; Allow the backend to change the mark.
9977     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9978     (if (eq mark old-mark)
9979         t
9980       (unless article
9981         (error "No article on current line"))
9982       (if (not (if (or (= mark gnus-unread-mark)
9983                        (= mark gnus-ticked-mark)
9984                        (= mark gnus-spam-mark)
9985                        (= mark gnus-dormant-mark))
9986                    (gnus-mark-article-as-unread article mark)
9987                  (gnus-mark-article-as-read article mark)))
9988           t
9989         ;; See whether the article is to be put in the cache.
9990         (and gnus-use-cache
9991              (not (= mark gnus-canceled-mark))
9992              (vectorp (gnus-summary-article-header article))
9993              (save-excursion
9994                (gnus-cache-possibly-enter-article
9995                 gnus-newsgroup-name article
9996                 (= mark gnus-ticked-mark)
9997                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9998
9999         (when (gnus-summary-goto-subject article nil t)
10000           (let ((buffer-read-only nil))
10001             (gnus-summary-show-thread)
10002             ;; Fix the mark.
10003             (gnus-summary-update-mark mark 'unread)
10004             t))))))
10005
10006 (defun gnus-summary-update-secondary-mark (article)
10007   "Update the secondary (read, process, cache) mark."
10008   (gnus-summary-update-mark
10009    (cond ((memq article gnus-newsgroup-processable)
10010           gnus-process-mark)
10011          ((memq article gnus-newsgroup-cached)
10012           gnus-cached-mark)
10013          ((memq article gnus-newsgroup-replied)
10014           gnus-replied-mark)
10015          ((memq article gnus-newsgroup-forwarded)
10016           gnus-forwarded-mark)
10017          ((memq article gnus-newsgroup-saved)
10018           gnus-saved-mark)
10019          ((memq article gnus-newsgroup-recent)
10020           gnus-recent-mark)
10021          ((memq article gnus-newsgroup-unseen)
10022           gnus-unseen-mark)
10023          (t gnus-no-mark))
10024    'replied)
10025   (when (gnus-visual-p 'summary-highlight 'highlight)
10026     (gnus-run-hooks 'gnus-summary-update-hook))
10027   t)
10028
10029 (defun gnus-summary-update-download-mark (article)
10030   "Update the download mark."
10031   (gnus-summary-update-mark
10032    (cond ((memq article gnus-newsgroup-undownloaded)
10033           gnus-undownloaded-mark)
10034          (gnus-newsgroup-agentized
10035           gnus-downloaded-mark)
10036          (t
10037           gnus-no-mark))
10038    'download)
10039   (gnus-summary-update-line t)
10040   t)
10041
10042 (defun gnus-summary-update-mark (mark type)
10043   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10044         (buffer-read-only nil))
10045     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10046     (when forward
10047       (when (looking-at "\r")
10048         (incf forward))
10049       (when (<= (+ forward (point)) (point-max))
10050         ;; Go to the right position on the line.
10051         (goto-char (+ forward (point)))
10052         ;; Replace the old mark with the new mark.
10053         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10054         ;; Optionally update the marks by some user rule.
10055         (when (eq type 'unread)
10056           (gnus-data-set-mark
10057            (gnus-data-find (gnus-summary-article-number)) mark)
10058           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10059
10060 (defun gnus-mark-article-as-read (article &optional mark)
10061   "Enter ARTICLE in the pertinent lists and remove it from others."
10062   ;; Make the article expirable.
10063   (let ((mark (or mark gnus-del-mark)))
10064     (setq gnus-newsgroup-expirable
10065           (if (= mark gnus-expirable-mark)
10066               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10067             (delq article gnus-newsgroup-expirable)))
10068     ;; Remove from unread and marked lists.
10069     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10070     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10071     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10072     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10073     (push (cons article mark) gnus-newsgroup-reads)
10074     ;; Possibly remove from cache, if that is used.
10075     (when gnus-use-cache
10076       (gnus-cache-enter-remove-article article))
10077     t))
10078
10079 (defun gnus-mark-article-as-unread (article &optional mark)
10080   "Enter ARTICLE in the pertinent lists and remove it from others."
10081   (let ((mark (or mark gnus-ticked-mark)))
10082     (if (<= article 0)
10083         (progn
10084           (gnus-error 1 "Can't mark negative article numbers")
10085           nil)
10086       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10087             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10088             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10089             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10090             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10091
10092       ;; Unsuppress duplicates?
10093       (when gnus-suppress-duplicates
10094         (gnus-dup-unsuppress-article article))
10095
10096       (cond ((= mark gnus-ticked-mark)
10097              (setq gnus-newsgroup-marked
10098                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10099             ((= mark gnus-spam-mark)
10100              (setq gnus-newsgroup-spam-marked
10101                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10102                                             article)))
10103             ((= mark gnus-dormant-mark)
10104              (setq gnus-newsgroup-dormant
10105                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10106             (t
10107              (setq gnus-newsgroup-unreads
10108                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10109       (gnus-pull article gnus-newsgroup-reads)
10110       t)))
10111
10112 (defalias 'gnus-summary-mark-as-unread-forward
10113   'gnus-summary-tick-article-forward)
10114 (make-obsolete 'gnus-summary-mark-as-unread-forward
10115                'gnus-summary-tick-article-forward)
10116 (defun gnus-summary-tick-article-forward (n)
10117   "Tick N articles forwards.
10118 If N is negative, tick backwards instead.
10119 The difference between N and the number of articles ticked is returned."
10120   (interactive "p")
10121   (gnus-summary-mark-forward n gnus-ticked-mark))
10122
10123 (defalias 'gnus-summary-mark-as-unread-backward
10124   'gnus-summary-tick-article-backward)
10125 (make-obsolete 'gnus-summary-mark-as-unread-backward
10126                'gnus-summary-tick-article-backward)
10127 (defun gnus-summary-tick-article-backward (n)
10128   "Tick N articles backwards.
10129 The difference between N and the number of articles ticked is returned."
10130   (interactive "p")
10131   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10132
10133 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10134 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10135 (defun gnus-summary-tick-article (&optional article clear-mark)
10136   "Mark current article as unread.
10137 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10138 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10139   (interactive)
10140   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10141                                        gnus-ticked-mark)))
10142
10143 (defun gnus-summary-mark-as-read-forward (n)
10144   "Mark N articles as read forwards.
10145 If N is negative, mark backwards instead.
10146 The difference between N and the actual number of articles marked is
10147 returned."
10148   (interactive "p")
10149   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10150
10151 (defun gnus-summary-mark-as-read-backward (n)
10152   "Mark the N articles as read backwards.
10153 The difference between N and the actual number of articles marked is
10154 returned."
10155   (interactive "p")
10156   (gnus-summary-mark-forward
10157    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10158
10159 (defun gnus-summary-mark-as-read (&optional article mark)
10160   "Mark current article as read.
10161 ARTICLE specifies the article to be marked as read.
10162 MARK specifies a string to be inserted at the beginning of the line."
10163   (gnus-summary-mark-article article mark))
10164
10165 (defun gnus-summary-clear-mark-forward (n)
10166   "Clear marks from N articles forward.
10167 If N is negative, clear backward instead.
10168 The difference between N and the number of marks cleared is returned."
10169   (interactive "p")
10170   (gnus-summary-mark-forward n gnus-unread-mark))
10171
10172 (defun gnus-summary-clear-mark-backward (n)
10173   "Clear marks from N articles backward.
10174 The difference between N and the number of marks cleared is returned."
10175   (interactive "p")
10176   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10177
10178 (defun gnus-summary-mark-unread-as-read ()
10179   "Intended to be used by `gnus-summary-mark-article-hook'."
10180   (when (memq gnus-current-article gnus-newsgroup-unreads)
10181     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10182
10183 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10184   "Intended to be used by `gnus-summary-mark-article-hook'."
10185   (let ((mark (gnus-summary-article-mark)))
10186     (when (or (gnus-unread-mark-p mark)
10187               (gnus-read-mark-p mark))
10188       (gnus-summary-mark-article gnus-current-article
10189                                  (or new-mark gnus-read-mark)))))
10190
10191 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10192   "Intended to be used by `gnus-summary-mark-article-hook'."
10193   (let ((mark (gnus-summary-article-mark)))
10194     (when (or (gnus-unread-mark-p mark)
10195               (gnus-read-mark-p mark))
10196       (gnus-summary-mark-article (gnus-summary-article-number)
10197                                  (or new-mark gnus-read-mark)))))
10198
10199 (defun gnus-summary-mark-unread-as-ticked ()
10200   "Intended to be used by `gnus-summary-mark-article-hook'."
10201   (when (memq gnus-current-article gnus-newsgroup-unreads)
10202     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10203
10204 (defun gnus-summary-mark-region-as-read (point mark all)
10205   "Mark all unread articles between point and mark as read.
10206 If given a prefix, mark all articles between point and mark as read,
10207 even ticked and dormant ones."
10208   (interactive "r\nP")
10209   (save-excursion
10210     (let (article)
10211       (goto-char point)
10212       (beginning-of-line)
10213       (while (and
10214               (< (point) mark)
10215               (progn
10216                 (when (or all
10217                           (memq (setq article (gnus-summary-article-number))
10218                                 gnus-newsgroup-unreads))
10219                   (gnus-summary-mark-article article gnus-del-mark))
10220                 t)
10221               (gnus-summary-find-next))))))
10222
10223 (defun gnus-summary-mark-below (score mark)
10224   "Mark articles with score less than SCORE with MARK."
10225   (interactive "P\ncMark: ")
10226   (setq score (if score
10227                   (prefix-numeric-value score)
10228                 (or gnus-summary-default-score 0)))
10229   (save-excursion
10230     (set-buffer gnus-summary-buffer)
10231     (goto-char (point-min))
10232     (while
10233         (progn
10234           (and (< (gnus-summary-article-score) score)
10235                (gnus-summary-mark-article nil mark))
10236           (gnus-summary-find-next)))))
10237
10238 (defun gnus-summary-kill-below (&optional score)
10239   "Mark articles with score below SCORE as read."
10240   (interactive "P")
10241   (gnus-summary-mark-below score gnus-killed-mark))
10242
10243 (defun gnus-summary-clear-above (&optional score)
10244   "Clear all marks from articles with score above SCORE."
10245   (interactive "P")
10246   (gnus-summary-mark-above score gnus-unread-mark))
10247
10248 (defun gnus-summary-tick-above (&optional score)
10249   "Tick all articles with score above SCORE."
10250   (interactive "P")
10251   (gnus-summary-mark-above score gnus-ticked-mark))
10252
10253 (defun gnus-summary-mark-above (score mark)
10254   "Mark articles with score over SCORE with MARK."
10255   (interactive "P\ncMark: ")
10256   (setq score (if score
10257                   (prefix-numeric-value score)
10258                 (or gnus-summary-default-score 0)))
10259   (save-excursion
10260     (set-buffer gnus-summary-buffer)
10261     (goto-char (point-min))
10262     (while (and (progn
10263                   (when (> (gnus-summary-article-score) score)
10264                     (gnus-summary-mark-article nil mark))
10265                   t)
10266                 (gnus-summary-find-next)))))
10267
10268 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10269 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10270 (defun gnus-summary-limit-include-expunged (&optional no-error)
10271   "Display all the hidden articles that were expunged for low scores."
10272   (interactive)
10273   (let ((buffer-read-only nil))
10274     (let ((scored gnus-newsgroup-scored)
10275           headers h)
10276       (while scored
10277         (unless (gnus-summary-article-header (caar scored))
10278           (and (setq h (gnus-number-to-header (caar scored)))
10279                (< (cdar scored) gnus-summary-expunge-below)
10280                (push h headers)))
10281         (setq scored (cdr scored)))
10282       (if (not headers)
10283           (when (not no-error)
10284             (error "No expunged articles hidden"))
10285         (goto-char (point-min))
10286         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10287         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10288         (mapcar (lambda (x) (push (mail-header-number x)
10289                                   gnus-newsgroup-limit))
10290                 headers)
10291         (gnus-summary-prepare-unthreaded (nreverse headers))
10292         (goto-char (point-min))
10293         (gnus-summary-position-point)
10294         t))))
10295
10296 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10297   "Mark all unread articles in this newsgroup as read.
10298 If prefix argument ALL is non-nil, ticked and dormant articles will
10299 also be marked as read.
10300 If QUIETLY is non-nil, no questions will be asked.
10301
10302 If TO-HERE is non-nil, it should be a point in the buffer.  All
10303 articles before (after, if REVERSE is set) this point will be marked
10304 as read.
10305
10306 Note that this function will only catch up the unread article
10307 in the current summary buffer limitation.
10308
10309 The number of articles marked as read is returned."
10310   (interactive "P")
10311   (prog1
10312       (save-excursion
10313         (when (or quietly
10314                   (not gnus-interactive-catchup) ;Without confirmation?
10315                   gnus-expert-user
10316                   (gnus-y-or-n-p
10317                    (if all
10318                        "Mark absolutely all articles as read? "
10319                      "Mark all unread articles as read? ")))
10320           (if (and not-mark
10321                    (not gnus-newsgroup-adaptive)
10322                    (not gnus-newsgroup-auto-expire)
10323                    (not gnus-suppress-duplicates)
10324                    (or (not gnus-use-cache)
10325                        (eq gnus-use-cache 'passive)))
10326               (progn
10327                 (when all
10328                   (setq gnus-newsgroup-marked nil
10329                         gnus-newsgroup-spam-marked nil
10330                         gnus-newsgroup-dormant nil))
10331                 (setq gnus-newsgroup-unreads
10332                       (gnus-sorted-nunion
10333                        (gnus-intersection gnus-newsgroup-unreads
10334                                           gnus-newsgroup-downloadable)
10335                        gnus-newsgroup-unfetched)))
10336             ;; We actually mark all articles as canceled, which we
10337             ;; have to do when using auto-expiry or adaptive scoring.
10338             (gnus-summary-show-all-threads)
10339             (if (and to-here reverse)
10340                 (progn
10341                   (goto-char to-here)
10342                   (gnus-summary-mark-current-read-and-unread-as-read
10343                    gnus-catchup-mark)
10344                   (while (gnus-summary-find-next (not all))
10345                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10346               (when (gnus-summary-first-subject (not all))
10347                 (while (and
10348                         (if to-here (< (point) to-here) t)
10349                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10350                         (gnus-summary-find-next (not all))))))
10351             (gnus-set-mode-line 'summary))
10352           t))
10353     (gnus-summary-position-point)))
10354
10355 (defun gnus-summary-catchup-to-here (&optional all)
10356   "Mark all unticked articles before the current one as read.
10357 If ALL is non-nil, also mark ticked and dormant articles as read."
10358   (interactive "P")
10359   (save-excursion
10360     (gnus-save-hidden-threads
10361       (let ((beg (point)))
10362         ;; We check that there are unread articles.
10363         (when (or all (gnus-summary-find-prev))
10364           (gnus-summary-catchup all t beg)))))
10365   (gnus-summary-position-point))
10366
10367 (defun gnus-summary-catchup-from-here (&optional all)
10368   "Mark all unticked articles after (and including) the current one as read.
10369 If ALL is non-nil, also mark ticked and dormant articles as read."
10370   (interactive "P")
10371   (save-excursion
10372     (gnus-save-hidden-threads
10373       (let ((beg (point)))
10374         ;; We check that there are unread articles.
10375         (when (or all (gnus-summary-find-next))
10376           (gnus-summary-catchup all t beg nil t)))))
10377   (gnus-summary-position-point))
10378
10379 (defun gnus-summary-catchup-all (&optional quietly)
10380   "Mark all articles in this newsgroup as read.
10381 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10382 instead, which marks only unread articles as read."
10383   (interactive "P")
10384   (gnus-summary-catchup t quietly))
10385
10386 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10387   "Mark all unread articles in this group as read, then exit.
10388 If prefix argument ALL is non-nil, all articles are marked as read.
10389 If QUIETLY is non-nil, no questions will be asked."
10390   (interactive "P")
10391   (when (gnus-summary-catchup all quietly nil 'fast)
10392     ;; Select next newsgroup or exit.
10393     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10394              (eq gnus-auto-select-next 'quietly))
10395         (gnus-summary-next-group nil)
10396       (gnus-summary-exit))))
10397
10398 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10399   "Mark all articles in this newsgroup as read, and then exit.
10400 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10401 instead, which marks only unread articles as read."
10402   (interactive "P")
10403   (gnus-summary-catchup-and-exit t quietly))
10404
10405 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10406   "Mark all articles in this group as read and select the next group.
10407 If given a prefix, mark all articles, unread as well as ticked, as
10408 read."
10409   (interactive "P")
10410   (save-excursion
10411     (gnus-summary-catchup all))
10412   (gnus-summary-next-group))
10413
10414 ;;;
10415 ;;; with article
10416 ;;;
10417
10418 (defmacro gnus-with-article (article &rest forms)
10419   "Select ARTICLE and perform FORMS in the original article buffer.
10420 Then replace the article with the result."
10421   `(progn
10422      ;; We don't want the article to be marked as read.
10423      (let (gnus-mark-article-hook)
10424        (gnus-summary-select-article t t nil ,article))
10425      (set-buffer gnus-original-article-buffer)
10426      ,@forms
10427      (if (not (gnus-check-backend-function
10428                'request-replace-article (car gnus-article-current)))
10429          (gnus-message 5 "Read-only group; not replacing")
10430        (unless (gnus-request-replace-article
10431                 ,article (car gnus-article-current)
10432                 (current-buffer) t)
10433          (error "Couldn't replace article")))
10434      ;; The cache and backlog have to be flushed somewhat.
10435      (when gnus-keep-backlog
10436        (gnus-backlog-remove-article
10437         (car gnus-article-current) (cdr gnus-article-current)))
10438      (when gnus-use-cache
10439        (gnus-cache-update-article
10440         (car gnus-article-current) (cdr gnus-article-current)))))
10441
10442 (put 'gnus-with-article 'lisp-indent-function 1)
10443 (put 'gnus-with-article 'edebug-form-spec '(form body))
10444
10445 ;; Thread-based commands.
10446
10447 (defun gnus-summary-articles-in-thread (&optional article)
10448   "Return a list of all articles in the current thread.
10449 If ARTICLE is non-nil, return all articles in the thread that starts
10450 with that article."
10451   (let* ((article (or article (gnus-summary-article-number)))
10452          (data (gnus-data-find-list article))
10453          (top-level (gnus-data-level (car data)))
10454          (top-subject
10455           (cond ((null gnus-thread-operation-ignore-subject)
10456                  (gnus-simplify-subject-re
10457                   (mail-header-subject (gnus-data-header (car data)))))
10458                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10459                  (gnus-simplify-subject-fuzzy
10460                   (mail-header-subject (gnus-data-header (car data)))))
10461                 (t nil)))
10462          (end-point (save-excursion
10463                       (if (gnus-summary-go-to-next-thread)
10464                           (point) (point-max))))
10465          articles)
10466     (while (and data
10467                 (< (gnus-data-pos (car data)) end-point))
10468       (when (or (not top-subject)
10469                 (string= top-subject
10470                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10471                              (gnus-simplify-subject-fuzzy
10472                               (mail-header-subject
10473                                (gnus-data-header (car data))))
10474                            (gnus-simplify-subject-re
10475                             (mail-header-subject
10476                              (gnus-data-header (car data)))))))
10477         (push (gnus-data-number (car data)) articles))
10478       (unless (and (setq data (cdr data))
10479                    (> (gnus-data-level (car data)) top-level))
10480         (setq data nil)))
10481     ;; Return the list of articles.
10482     (nreverse articles)))
10483
10484 (defun gnus-summary-rethread-current ()
10485   "Rethread the thread the current article is part of."
10486   (interactive)
10487   (let* ((gnus-show-threads t)
10488          (article (gnus-summary-article-number))
10489          (id (mail-header-id (gnus-summary-article-header)))
10490          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10491     (unless id
10492       (error "No article on the current line"))
10493     (gnus-rebuild-thread id)
10494     (gnus-summary-goto-subject article)))
10495
10496 (defun gnus-summary-reparent-thread ()
10497   "Make the current article child of the marked (or previous) article.
10498
10499 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10500 is non-nil or the Subject: of both articles are the same."
10501   (interactive)
10502   (unless (not (gnus-group-read-only-p))
10503     (error "The current newsgroup does not support article editing"))
10504   (unless (<= (length gnus-newsgroup-processable) 1)
10505     (error "No more than one article may be marked"))
10506   (save-window-excursion
10507     (let ((gnus-article-buffer " *reparent*")
10508           (current-article (gnus-summary-article-number))
10509           ;; First grab the marked article, otherwise one line up.
10510           (parent-article (if (not (null gnus-newsgroup-processable))
10511                               (car gnus-newsgroup-processable)
10512                             (save-excursion
10513                               (if (eq (forward-line -1) 0)
10514                                   (gnus-summary-article-number)
10515                                 (error "Beginning of summary buffer"))))))
10516       (unless (not (eq current-article parent-article))
10517         (error "An article may not be self-referential"))
10518       (let ((message-id (mail-header-id
10519                          (gnus-summary-article-header parent-article))))
10520         (unless (and message-id (not (equal message-id "")))
10521           (error "No message-id in desired parent"))
10522         (gnus-with-article current-article
10523           (save-restriction
10524             (goto-char (point-min))
10525             (message-narrow-to-head)
10526             (if (re-search-forward "^References: " nil t)
10527                 (progn
10528                   (re-search-forward "^[^ \t]" nil t)
10529                   (forward-line -1)
10530                   (end-of-line)
10531                   (insert " " message-id))
10532               (insert "References: " message-id "\n"))))
10533         (set-buffer gnus-summary-buffer)
10534         (gnus-summary-unmark-all-processable)
10535         (gnus-summary-update-article current-article)
10536         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10537             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10538         (gnus-summary-rethread-current)
10539         (gnus-message 3 "Article %d is now the child of article %d"
10540                       current-article parent-article)))))
10541
10542 (defun gnus-summary-toggle-threads (&optional arg)
10543   "Toggle showing conversation threads.
10544 If ARG is positive number, turn showing conversation threads on."
10545   (interactive "P")
10546   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10547     (setq gnus-show-threads
10548           (if (null arg) (not gnus-show-threads)
10549             (> (prefix-numeric-value arg) 0)))
10550     (gnus-summary-prepare)
10551     (gnus-summary-goto-subject current)
10552     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10553     (gnus-summary-position-point)))
10554
10555 (defun gnus-summary-show-all-threads ()
10556   "Show all threads."
10557   (interactive)
10558   (save-excursion
10559     (let ((buffer-read-only nil))
10560       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10561   (gnus-summary-position-point))
10562
10563 (defun gnus-summary-show-thread ()
10564   "Show thread subtrees.
10565 Returns nil if no thread was there to be shown."
10566   (interactive)
10567   (let ((buffer-read-only nil)
10568         (orig (point))
10569         (end (point-at-eol))
10570         ;; Leave point at bol
10571         (beg (progn (beginning-of-line) (point))))
10572     (prog1
10573         ;; Any hidden lines here?
10574         (search-forward "\r" end t)
10575       (subst-char-in-region beg end ?\^M ?\n t)
10576       (goto-char orig)
10577       (gnus-summary-position-point))))
10578
10579 (defun gnus-summary-maybe-hide-threads ()
10580   "If requested, hide the threads that should be hidden."
10581   (when (and gnus-show-threads
10582              gnus-thread-hide-subtree)
10583     (gnus-summary-hide-all-threads
10584      (if (or (consp gnus-thread-hide-subtree)
10585              (functionp gnus-thread-hide-subtree))
10586          (gnus-make-predicate gnus-thread-hide-subtree)
10587        nil))))
10588
10589 ;;; Hiding predicates.
10590
10591 (defun gnus-article-unread-p (header)
10592   (memq (mail-header-number header) gnus-newsgroup-unreads))
10593
10594 (defun gnus-article-unseen-p (header)
10595   (memq (mail-header-number header) gnus-newsgroup-unseen))
10596
10597 (defun gnus-map-articles (predicate articles)
10598   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10599   (apply 'gnus-or (mapcar predicate
10600                           (mapcar 'gnus-summary-article-header articles))))
10601
10602 (defun gnus-summary-hide-all-threads (&optional predicate)
10603   "Hide all thread subtrees.
10604 If PREDICATE is supplied, threads that satisfy this predicate
10605 will not be hidden."
10606   (interactive)
10607   (save-excursion
10608     (goto-char (point-min))
10609     (let ((end nil))
10610       (while (not end)
10611         (when (or (not predicate)
10612                   (gnus-map-articles
10613                    predicate (gnus-summary-article-children)))
10614             (gnus-summary-hide-thread))
10615         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10616   (gnus-summary-position-point))
10617
10618 (defun gnus-summary-hide-thread ()
10619   "Hide thread subtrees.
10620 If PREDICATE is supplied, threads that satisfy this predicate
10621 will not be hidden.
10622 Returns nil if no threads were there to be hidden."
10623   (interactive)
10624   (let ((buffer-read-only nil)
10625         (start (point))
10626         (article (gnus-summary-article-number)))
10627     (goto-char start)
10628     ;; Go forward until either the buffer ends or the subthread
10629     ;; ends.
10630     (when (and (not (eobp))
10631                (or (zerop (gnus-summary-next-thread 1 t))
10632                    (goto-char (point-max))))
10633       (prog1
10634           (if (and (> (point) start)
10635                    (search-backward "\n" start t))
10636               (progn
10637                 (subst-char-in-region start (point) ?\n ?\^M)
10638                 (gnus-summary-goto-subject article))
10639             (goto-char start)
10640             nil)))))
10641
10642 (defun gnus-summary-go-to-next-thread (&optional previous)
10643   "Go to the same level (or less) next thread.
10644 If PREVIOUS is non-nil, go to previous thread instead.
10645 Return the article number moved to, or nil if moving was impossible."
10646   (let ((level (gnus-summary-thread-level))
10647         (way (if previous -1 1))
10648         (beg (point)))
10649     (forward-line way)
10650     (while (and (not (eobp))
10651                 (< level (gnus-summary-thread-level)))
10652       (forward-line way))
10653     (if (eobp)
10654         (progn
10655           (goto-char beg)
10656           nil)
10657       (setq beg (point))
10658       (prog1
10659           (gnus-summary-article-number)
10660         (goto-char beg)))))
10661
10662 (defun gnus-summary-next-thread (n &optional silent)
10663   "Go to the same level next N'th thread.
10664 If N is negative, search backward instead.
10665 Returns the difference between N and the number of skips actually
10666 done.
10667
10668 If SILENT, don't output messages."
10669   (interactive "p")
10670   (let ((backward (< n 0))
10671         (n (abs n)))
10672     (while (and (> n 0)
10673                 (gnus-summary-go-to-next-thread backward))
10674       (decf n))
10675     (unless silent
10676       (gnus-summary-position-point))
10677     (when (and (not silent) (/= 0 n))
10678       (gnus-message 7 "No more threads"))
10679     n))
10680
10681 (defun gnus-summary-prev-thread (n)
10682   "Go to the same level previous N'th thread.
10683 Returns the difference between N and the number of skips actually
10684 done."
10685   (interactive "p")
10686   (gnus-summary-next-thread (- n)))
10687
10688 (defun gnus-summary-go-down-thread ()
10689   "Go down one level in the current thread."
10690   (let ((children (gnus-summary-article-children)))
10691     (when children
10692       (gnus-summary-goto-subject (car children)))))
10693
10694 (defun gnus-summary-go-up-thread ()
10695   "Go up one level in the current thread."
10696   (let ((parent (gnus-summary-article-parent)))
10697     (when parent
10698       (gnus-summary-goto-subject parent))))
10699
10700 (defun gnus-summary-down-thread (n)
10701   "Go down thread N steps.
10702 If N is negative, go up instead.
10703 Returns the difference between N and how many steps down that were
10704 taken."
10705   (interactive "p")
10706   (let ((up (< n 0))
10707         (n (abs n)))
10708     (while (and (> n 0)
10709                 (if up (gnus-summary-go-up-thread)
10710                   (gnus-summary-go-down-thread)))
10711       (setq n (1- n)))
10712     (gnus-summary-position-point)
10713     (when (/= 0 n)
10714       (gnus-message 7 "Can't go further"))
10715     n))
10716
10717 (defun gnus-summary-up-thread (n)
10718   "Go up thread N steps.
10719 If N is negative, go down instead.
10720 Returns the difference between N and how many steps down that were
10721 taken."
10722   (interactive "p")
10723   (gnus-summary-down-thread (- n)))
10724
10725 (defun gnus-summary-top-thread ()
10726   "Go to the top of the thread."
10727   (interactive)
10728   (while (gnus-summary-go-up-thread))
10729   (gnus-summary-article-number))
10730
10731 (defun gnus-summary-kill-thread (&optional unmark)
10732   "Mark articles under current thread as read.
10733 If the prefix argument is positive, remove any kinds of marks.
10734 If the prefix argument is negative, tick articles instead."
10735   (interactive "P")
10736   (when unmark
10737     (setq unmark (prefix-numeric-value unmark)))
10738   (let ((articles (gnus-summary-articles-in-thread)))
10739     (save-excursion
10740       ;; Expand the thread.
10741       (gnus-summary-show-thread)
10742       ;; Mark all the articles.
10743       (while articles
10744         (gnus-summary-goto-subject (car articles))
10745         (cond ((null unmark)
10746                (gnus-summary-mark-article-as-read gnus-killed-mark))
10747               ((> unmark 0)
10748                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10749               (t
10750                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10751         (setq articles (cdr articles))))
10752     ;; Hide killed subtrees.
10753     (and (null unmark)
10754          gnus-thread-hide-killed
10755          (gnus-summary-hide-thread))
10756     ;; If marked as read, go to next unread subject.
10757     (when (null unmark)
10758       ;; Go to next unread subject.
10759       (gnus-summary-next-subject 1 t)))
10760   (gnus-set-mode-line 'summary))
10761
10762 ;; Summary sorting commands
10763
10764 (defun gnus-summary-sort-by-number (&optional reverse)
10765   "Sort the summary buffer by article number.
10766 Argument REVERSE means reverse order."
10767   (interactive "P")
10768   (gnus-summary-sort 'number reverse))
10769
10770 (defun gnus-summary-sort-by-random (&optional reverse)
10771   "Randomize the order in the summary buffer.
10772 Argument REVERSE means to randomize in reverse order."
10773   (interactive "P")
10774   (gnus-summary-sort 'random reverse))
10775
10776 (defun gnus-summary-sort-by-author (&optional reverse)
10777   "Sort the summary buffer by author name alphabetically.
10778 If `case-fold-search' is non-nil, case of letters is ignored.
10779 Argument REVERSE means reverse order."
10780   (interactive "P")
10781   (gnus-summary-sort 'author reverse))
10782
10783 (defun gnus-summary-sort-by-subject (&optional reverse)
10784   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10785 If `case-fold-search' is non-nil, case of letters is ignored.
10786 Argument REVERSE means reverse order."
10787   (interactive "P")
10788   (gnus-summary-sort 'subject reverse))
10789
10790 (defun gnus-summary-sort-by-date (&optional reverse)
10791   "Sort the summary buffer by date.
10792 Argument REVERSE means reverse order."
10793   (interactive "P")
10794   (gnus-summary-sort 'date reverse))
10795
10796 (defun gnus-summary-sort-by-score (&optional reverse)
10797   "Sort the summary buffer by score.
10798 Argument REVERSE means reverse order."
10799   (interactive "P")
10800   (gnus-summary-sort 'score reverse))
10801
10802 (defun gnus-summary-sort-by-lines (&optional reverse)
10803   "Sort the summary buffer by the number of lines.
10804 Argument REVERSE means reverse order."
10805   (interactive "P")
10806   (gnus-summary-sort 'lines reverse))
10807
10808 (defun gnus-summary-sort-by-chars (&optional reverse)
10809   "Sort the summary buffer by article length.
10810 Argument REVERSE means reverse order."
10811   (interactive "P")
10812   (gnus-summary-sort 'chars reverse))
10813
10814 (defun gnus-summary-sort-by-original (&optional reverse)
10815   "Sort the summary buffer using the default sorting method.
10816 Argument REVERSE means reverse order."
10817   (interactive "P")
10818   (let* ((buffer-read-only)
10819          (gnus-summary-prepare-hook nil))
10820     ;; We do the sorting by regenerating the threads.
10821     (gnus-summary-prepare)
10822     ;; Hide subthreads if needed.
10823     (gnus-summary-maybe-hide-threads)))
10824
10825 (defun gnus-summary-sort (predicate reverse)
10826   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10827   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10828          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10829          (gnus-thread-sort-functions
10830           (if (not reverse)
10831               thread
10832             `(lambda (t1 t2)
10833                (,thread t2 t1))))
10834          (gnus-sort-gathered-threads-function
10835           gnus-thread-sort-functions)
10836          (gnus-article-sort-functions
10837           (if (not reverse)
10838               article
10839             `(lambda (t1 t2)
10840                (,article t2 t1))))
10841          (buffer-read-only)
10842          (gnus-summary-prepare-hook nil))
10843     ;; We do the sorting by regenerating the threads.
10844     (gnus-summary-prepare)
10845     ;; Hide subthreads if needed.
10846     (gnus-summary-maybe-hide-threads)))
10847
10848 ;; Summary saving commands.
10849
10850 (defun gnus-summary-save-article (&optional n not-saved)
10851   "Save the current article using the default saver function.
10852 If N is a positive number, save the N next articles.
10853 If N is a negative number, save the N previous articles.
10854 If N is nil and any articles have been marked with the process mark,
10855 save those articles instead.
10856 The variable `gnus-default-article-saver' specifies the saver function."
10857   (interactive "P")
10858   (let* ((articles (gnus-summary-work-articles n))
10859          (save-buffer (save-excursion
10860                         (nnheader-set-temp-buffer " *Gnus Save*")))
10861          (num (length articles))
10862          header file)
10863     (dolist (article articles)
10864       (setq header (gnus-summary-article-header article))
10865       (if (not (vectorp header))
10866           ;; This is a pseudo-article.
10867           (if (assq 'name header)
10868               (gnus-copy-file (cdr (assq 'name header)))
10869             (gnus-message 1 "Article %d is unsaveable" article))
10870         ;; This is a real article.
10871         (save-window-excursion
10872           (let ((gnus-display-mime-function nil)
10873                 (gnus-article-prepare-hook nil))
10874             (gnus-summary-select-article t nil nil article)))
10875         (save-excursion
10876           (set-buffer save-buffer)
10877           (erase-buffer)
10878           (insert-buffer-substring gnus-original-article-buffer))
10879         (setq file (gnus-article-save save-buffer file num))
10880         (gnus-summary-remove-process-mark article)
10881         (unless not-saved
10882           (gnus-summary-set-saved-mark article))))
10883     (gnus-kill-buffer save-buffer)
10884     (gnus-summary-position-point)
10885     (gnus-set-mode-line 'summary)
10886     n))
10887
10888 (defun gnus-summary-pipe-output (&optional arg headers)
10889   "Pipe the current article to a subprocess.
10890 If N is a positive number, pipe the N next articles.
10891 If N is a negative number, pipe the N previous articles.
10892 If N is nil and any articles have been marked with the process mark,
10893 pipe those articles instead.
10894 If HEADERS (the symbolic prefix), include the headers, too."
10895   (interactive (gnus-interactive "P\ny"))
10896   (require 'gnus-art)
10897   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10898         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10899     (gnus-summary-save-article arg t))
10900   (let ((buffer (get-buffer "*Shell Command Output*")))
10901     (when (and buffer
10902                (not (zerop (buffer-size buffer))))
10903       (gnus-configure-windows 'pipe))))
10904
10905 (defun gnus-summary-save-article-mail (&optional arg)
10906   "Append the current article to an mail file.
10907 If N is a positive number, save the N next articles.
10908 If N is a negative number, save the N previous articles.
10909 If N is nil and any articles have been marked with the process mark,
10910 save those articles instead."
10911   (interactive "P")
10912   (require 'gnus-art)
10913   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10914     (gnus-summary-save-article arg)))
10915
10916 (defun gnus-summary-save-article-rmail (&optional arg)
10917   "Append the current article to an rmail file.
10918 If N is a positive number, save the N next articles.
10919 If N is a negative number, save the N previous articles.
10920 If N is nil and any articles have been marked with the process mark,
10921 save those articles instead."
10922   (interactive "P")
10923   (require 'gnus-art)
10924   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10925     (gnus-summary-save-article arg)))
10926
10927 (defun gnus-summary-save-article-file (&optional arg)
10928   "Append the current article to a file.
10929 If N is a positive number, save the N next articles.
10930 If N is a negative number, save the N previous articles.
10931 If N is nil and any articles have been marked with the process mark,
10932 save those articles instead."
10933   (interactive "P")
10934   (require 'gnus-art)
10935   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10936     (gnus-summary-save-article arg)))
10937
10938 (defun gnus-summary-write-article-file (&optional arg)
10939   "Write the current article to a file, deleting the previous file.
10940 If N is a positive number, save the N next articles.
10941 If N is a negative number, save the N previous articles.
10942 If N is nil and any articles have been marked with the process mark,
10943 save those articles instead."
10944   (interactive "P")
10945   (require 'gnus-art)
10946   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10947     (gnus-summary-save-article arg)))
10948
10949 (defun gnus-summary-save-article-body-file (&optional arg)
10950   "Append the current article body to a file.
10951 If N is a positive number, save the N next articles.
10952 If N is a negative number, save the N previous articles.
10953 If N is nil and any articles have been marked with the process mark,
10954 save those articles instead."
10955   (interactive "P")
10956   (require 'gnus-art)
10957   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10958     (gnus-summary-save-article arg)))
10959
10960 (defun gnus-summary-muttprint (&optional arg)
10961   "Print the current article using Muttprint.
10962 If N is a positive number, save the N next articles.
10963 If N is a negative number, save the N previous articles.
10964 If N is nil and any articles have been marked with the process mark,
10965 save those articles instead."
10966   (interactive "P")
10967   (require 'gnus-art)
10968   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10969     (gnus-summary-save-article arg t)))
10970
10971 (defun gnus-summary-pipe-message (program)
10972   "Pipe the current article through PROGRAM."
10973   (interactive "sProgram: ")
10974   (gnus-summary-select-article)
10975   (let ((mail-header-separator ""))
10976     (gnus-eval-in-buffer-window gnus-article-buffer
10977       (save-restriction
10978         (widen)
10979         (let ((start (window-start))
10980               buffer-read-only)
10981           (message-pipe-buffer-body program)
10982           (set-window-start (get-buffer-window (current-buffer)) start))))))
10983
10984 (defun gnus-get-split-value (methods)
10985   "Return a value based on the split METHODS."
10986   (let (split-name method result match)
10987     (when methods
10988       (save-excursion
10989         (set-buffer gnus-original-article-buffer)
10990         (save-restriction
10991           (nnheader-narrow-to-headers)
10992           (while (and methods (not split-name))
10993             (goto-char (point-min))
10994             (setq method (pop methods))
10995             (setq match (car method))
10996             (when (cond
10997                    ((stringp match)
10998                     ;; Regular expression.
10999                     (ignore-errors
11000                       (re-search-forward match nil t)))
11001                    ((functionp match)
11002                     ;; Function.
11003                     (save-restriction
11004                       (widen)
11005                       (setq result (funcall match gnus-newsgroup-name))))
11006                    ((consp match)
11007                     ;; Form.
11008                     (save-restriction
11009                       (widen)
11010                       (setq result (eval match)))))
11011               (setq split-name (cdr method))
11012               (cond ((stringp result)
11013                      (push (expand-file-name
11014                             result gnus-article-save-directory)
11015                            split-name))
11016                     ((consp result)
11017                      (setq split-name (append result split-name)))))))))
11018     (nreverse split-name)))
11019
11020 (defun gnus-valid-move-group-p (group)
11021   (and (boundp group)
11022        (symbol-name group)
11023        (symbol-value group)
11024        (gnus-get-function (gnus-find-method-for-group
11025                            (symbol-name group)) 'request-accept-article t)))
11026
11027 (defun gnus-read-move-group-name (prompt default articles prefix)
11028   "Read a group name."
11029   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11030          (minibuffer-confirm-incomplete nil) ; XEmacs
11031          (prom
11032           (format "%s %s to:"
11033                   prompt
11034                   (if (> (length articles) 1)
11035                       (format "these %d articles" (length articles))
11036                     "this article")))
11037          (to-newsgroup
11038           (cond
11039            ((null split-name)
11040             (gnus-completing-read-with-default
11041              default prom
11042              gnus-active-hashtb
11043              'gnus-valid-move-group-p
11044              nil prefix
11045              'gnus-group-history))
11046            ((= 1 (length split-name))
11047             (gnus-completing-read-with-default
11048              (car split-name) prom
11049              gnus-active-hashtb
11050              'gnus-valid-move-group-p
11051              nil nil
11052              'gnus-group-history))
11053            (t
11054             (gnus-completing-read-with-default
11055              nil prom
11056              (mapcar 'list (nreverse split-name))
11057              nil nil nil
11058              'gnus-group-history))))
11059          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11060     (when to-newsgroup
11061       (if (or (string= to-newsgroup "")
11062               (string= to-newsgroup prefix))
11063           (setq to-newsgroup default))
11064       (unless to-newsgroup
11065         (error "No group name entered"))
11066       (or (gnus-active to-newsgroup)
11067           (gnus-activate-group to-newsgroup nil nil to-method)
11068           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11069                                      to-newsgroup))
11070               (or (and (gnus-request-create-group to-newsgroup to-method)
11071                        (gnus-activate-group
11072                         to-newsgroup nil nil to-method)
11073                        (gnus-subscribe-group to-newsgroup))
11074                   (error "Couldn't create group %s" to-newsgroup)))
11075           (error "No such group: %s" to-newsgroup)))
11076     to-newsgroup))
11077
11078 (defun gnus-summary-save-parts (type dir n &optional reverse)
11079   "Save parts matching TYPE to DIR.
11080 If REVERSE, save parts that do not match TYPE."
11081   (interactive
11082    (list (read-string "Save parts of type: "
11083                       (or (car gnus-summary-save-parts-type-history)
11084                           gnus-summary-save-parts-default-mime)
11085                       'gnus-summary-save-parts-type-history)
11086          (setq gnus-summary-save-parts-last-directory
11087                (read-file-name "Save to directory: "
11088                                gnus-summary-save-parts-last-directory
11089                                nil t))
11090          current-prefix-arg))
11091   (gnus-summary-iterate n
11092     (let ((gnus-display-mime-function nil)
11093           (gnus-inhibit-treatment t))
11094       (gnus-summary-select-article))
11095     (save-excursion
11096       (set-buffer gnus-article-buffer)
11097       (let ((handles (or gnus-article-mime-handles
11098                          (mm-dissect-buffer nil gnus-article-loose-mime)
11099                          (and gnus-article-emulate-mime
11100                               (mm-uu-dissect)))))
11101         (when handles
11102           (gnus-summary-save-parts-1 type dir handles reverse)
11103           (unless gnus-article-mime-handles ;; Don't destroy this case.
11104             (mm-destroy-parts handles)))))))
11105
11106 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11107   (if (stringp (car handle))
11108       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11109               (cdr handle))
11110     (when (if reverse
11111               (not (string-match type (mm-handle-media-type handle)))
11112             (string-match type (mm-handle-media-type handle)))
11113       (let ((file (expand-file-name
11114                    (gnus-map-function
11115                     mm-file-name-rewrite-functions
11116                     (file-name-nondirectory
11117                      (or
11118                       (mail-content-type-get
11119                        (mm-handle-disposition handle) 'filename)
11120                       (mail-content-type-get
11121                        (mm-handle-type handle) 'name)
11122                       (concat gnus-newsgroup-name
11123                               "." (number-to-string
11124                                    (cdr gnus-article-current))))))
11125                    dir)))
11126         (unless (file-exists-p file)
11127           (mm-save-part-to-file handle file))))))
11128
11129 ;; Summary extract commands
11130
11131 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11132   (let ((buffer-read-only nil)
11133         (article (gnus-summary-article-number))
11134         after-article b e)
11135     (unless (gnus-summary-goto-subject article)
11136       (error "No such article: %d" article))
11137     (gnus-summary-position-point)
11138     ;; If all commands are to be bunched up on one line, we collect
11139     ;; them here.
11140     (unless gnus-view-pseudos-separately
11141       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11142             files action)
11143         (while ps
11144           (setq action (cdr (assq 'action (car ps))))
11145           (setq files (list (cdr (assq 'name (car ps)))))
11146           (while (and ps (cdr ps)
11147                       (string= (or action "1")
11148                                (or (cdr (assq 'action (cadr ps))) "2")))
11149             (push (cdr (assq 'name (cadr ps))) files)
11150             (setcdr ps (cddr ps)))
11151           (when files
11152             (when (not (string-match "%s" action))
11153               (push " " files))
11154             (push " " files)
11155             (when (assq 'execute (car ps))
11156               (setcdr (assq 'execute (car ps))
11157                       (funcall (if (string-match "%s" action)
11158                                    'format 'concat)
11159                                action
11160                                (mapconcat
11161                                 (lambda (f)
11162                                   (if (equal f " ")
11163                                       f
11164                                     (shell-quote-argument f)))
11165                                 files " ")))))
11166           (setq ps (cdr ps)))))
11167     (if (and gnus-view-pseudos (not not-view))
11168         (while pslist
11169           (when (assq 'execute (car pslist))
11170             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11171                                   (eq gnus-view-pseudos 'not-confirm)))
11172           (setq pslist (cdr pslist)))
11173       (save-excursion
11174         (while pslist
11175           (setq after-article (or (cdr (assq 'article (car pslist)))
11176                                   (gnus-summary-article-number)))
11177           (gnus-summary-goto-subject after-article)
11178           (forward-line 1)
11179           (setq b (point))
11180           (insert "    " (file-name-nondirectory
11181                           (cdr (assq 'name (car pslist))))
11182                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11183           (setq e (point))
11184           (forward-line -1)             ; back to `b'
11185           (gnus-add-text-properties
11186            b (1- e) (list 'gnus-number gnus-reffed-article-number
11187                           gnus-mouse-face-prop gnus-mouse-face))
11188           (gnus-data-enter
11189            after-article gnus-reffed-article-number
11190            gnus-unread-mark b (car pslist) 0 (- e b))
11191           (setq gnus-newsgroup-unreads
11192                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11193                                          gnus-reffed-article-number))
11194           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11195           (setq pslist (cdr pslist)))))))
11196
11197 (defun gnus-pseudos< (p1 p2)
11198   (let ((c1 (cdr (assq 'action p1)))
11199         (c2 (cdr (assq 'action p2))))
11200     (and c1 c2 (string< c1 c2))))
11201
11202 (defun gnus-request-pseudo-article (props)
11203   (cond ((assq 'execute props)
11204          (gnus-execute-command (cdr (assq 'execute props)))))
11205   (let ((gnus-current-article (gnus-summary-article-number)))
11206     (gnus-run-hooks 'gnus-mark-article-hook)))
11207
11208 (defun gnus-execute-command (command &optional automatic)
11209   (save-excursion
11210     (gnus-article-setup-buffer)
11211     (set-buffer gnus-article-buffer)
11212     (setq buffer-read-only nil)
11213     (let ((command (if automatic command
11214                      (read-string "Command: " (cons command 0)))))
11215       (erase-buffer)
11216       (insert "$ " command "\n\n")
11217       (if gnus-view-pseudo-asynchronously
11218           (start-process "gnus-execute" (current-buffer) shell-file-name
11219                          shell-command-switch command)
11220         (call-process shell-file-name nil t nil
11221                       shell-command-switch command)))))
11222
11223 ;; Summary kill commands.
11224
11225 (defun gnus-summary-edit-global-kill (article)
11226   "Edit the \"global\" kill file."
11227   (interactive (list (gnus-summary-article-number)))
11228   (gnus-group-edit-global-kill article))
11229
11230 (defun gnus-summary-edit-local-kill ()
11231   "Edit a local kill file applied to the current newsgroup."
11232   (interactive)
11233   (setq gnus-current-headers (gnus-summary-article-header))
11234   (gnus-group-edit-local-kill
11235    (gnus-summary-article-number) gnus-newsgroup-name))
11236
11237 ;;; Header reading.
11238
11239 (defun gnus-read-header (id &optional header)
11240   "Read the headers of article ID and enter them into the Gnus system."
11241   (let ((group gnus-newsgroup-name)
11242         (gnus-override-method
11243          (or
11244           gnus-override-method
11245           (and (gnus-news-group-p gnus-newsgroup-name)
11246                (car (gnus-refer-article-methods)))))
11247         where)
11248     ;; First we check to see whether the header in question is already
11249     ;; fetched.
11250     (if (stringp id)
11251         ;; This is a Message-ID.
11252         (setq header (or header (gnus-id-to-header id)))
11253       ;; This is an article number.
11254       (setq header (or header (gnus-summary-article-header id))))
11255     (if (and header
11256              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11257         ;; We have found the header.
11258         header
11259       ;; We have to really fetch the header to this article.
11260       (save-excursion
11261         (set-buffer nntp-server-buffer)
11262         (when (setq where (gnus-request-head id group))
11263           (nnheader-fold-continuation-lines)
11264           (goto-char (point-max))
11265           (insert ".\n")
11266           (goto-char (point-min))
11267           (insert "211 ")
11268           (princ (cond
11269                   ((numberp id) id)
11270                   ((cdr where) (cdr where))
11271                   (header (mail-header-number header))
11272                   (t gnus-reffed-article-number))
11273                  (current-buffer))
11274           (insert " Article retrieved.\n"))
11275         (if (or (not where)
11276                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11277             ()                          ; Malformed head.
11278           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11279             (when (and (stringp id)
11280                        (not (string= (gnus-group-real-name group)
11281                                      (car where))))
11282               ;; If we fetched by Message-ID and the article came
11283               ;; from a different group, we fudge some bogus article
11284               ;; numbers for this article.
11285               (mail-header-set-number header gnus-reffed-article-number))
11286             (save-excursion
11287               (set-buffer gnus-summary-buffer)
11288               (decf gnus-reffed-article-number)
11289               (gnus-remove-header (mail-header-number header))
11290               (push header gnus-newsgroup-headers)
11291               (setq gnus-current-headers header)
11292               (push (mail-header-number header) gnus-newsgroup-limit)))
11293           header)))))
11294
11295 (defun gnus-remove-header (number)
11296   "Remove header NUMBER from `gnus-newsgroup-headers'."
11297   (if (and gnus-newsgroup-headers
11298            (= number (mail-header-number (car gnus-newsgroup-headers))))
11299       (pop gnus-newsgroup-headers)
11300     (let ((headers gnus-newsgroup-headers))
11301       (while (and (cdr headers)
11302                   (not (= number (mail-header-number (cadr headers)))))
11303         (pop headers))
11304       (when (cdr headers)
11305         (setcdr headers (cddr headers))))))
11306
11307 ;;;
11308 ;;; summary highlights
11309 ;;;
11310
11311 (defun gnus-highlight-selected-summary ()
11312   "Highlight selected article in summary buffer."
11313   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11314   (when gnus-summary-selected-face
11315     (save-excursion
11316       (let* ((beg (point-at-bol))
11317              (end (point-at-eol))
11318              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11319              (from (if (get-text-property beg gnus-mouse-face-prop)
11320                        beg
11321                      (or (next-single-property-change
11322                           beg gnus-mouse-face-prop nil end)
11323                          beg)))
11324              (to
11325               (if (= from end)
11326                   (- from 2)
11327                 (or (next-single-property-change
11328                      from gnus-mouse-face-prop nil end)
11329                     end))))
11330         ;; If no mouse-face prop on line we will have to = from = end,
11331         ;; so we highlight the entire line instead.
11332         (when (= (+ to 2) from)
11333           (setq from beg)
11334           (setq to end))
11335         (if gnus-newsgroup-selected-overlay
11336             ;; Move old overlay.
11337             (gnus-move-overlay
11338              gnus-newsgroup-selected-overlay from to (current-buffer))
11339           ;; Create new overlay.
11340           (gnus-overlay-put
11341            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11342            'face gnus-summary-selected-face))))))
11343
11344 (defvar gnus-summary-highlight-line-cached nil)
11345 (defvar gnus-summary-highlight-line-trigger nil)
11346
11347 (defun gnus-summary-highlight-line-0 ()
11348   (if (and (eq gnus-summary-highlight-line-trigger
11349                gnus-summary-highlight)
11350            gnus-summary-highlight-line-cached)
11351       gnus-summary-highlight-line-cached
11352     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11353           gnus-summary-highlight-line-cached
11354           (let* ((cond (list 'cond))
11355                  (c cond)
11356                  (list gnus-summary-highlight))
11357             (while list
11358               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11359                               nil))
11360               (setq c (cdr c)
11361                     list (cdr list)))
11362             (gnus-byte-compile (list 'lambda nil cond))))))
11363
11364 (defun gnus-summary-highlight-line ()
11365   "Highlight current line according to `gnus-summary-highlight'."
11366   (let* ((beg (point-at-bol))
11367          (article (or (gnus-summary-article-number) gnus-current-article))
11368          (score (or (cdr (assq article
11369                                gnus-newsgroup-scored))
11370                     gnus-summary-default-score 0))
11371          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11372          (inhibit-read-only t)
11373          (default gnus-summary-default-score)
11374          (default-high gnus-summary-default-high-score)
11375          (default-low gnus-summary-default-low-score)
11376          (uncached (and gnus-summary-use-undownloaded-faces
11377                         (memq article gnus-newsgroup-undownloaded)
11378                         (not (memq article gnus-newsgroup-cached)))))
11379     (let ((face (funcall (gnus-summary-highlight-line-0))))
11380       (unless (eq face (get-text-property beg 'face))
11381         (gnus-put-text-property-excluding-characters-with-faces
11382          beg (point-at-eol) 'face
11383          (setq face (if (boundp face) (symbol-value face) face)))
11384         (when gnus-summary-highlight-line-function
11385           (funcall gnus-summary-highlight-line-function article face))))))
11386
11387 (defun gnus-update-read-articles (group unread &optional compute)
11388   "Update the list of read articles in GROUP.
11389 UNREAD is a sorted list."
11390   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11391         (info (gnus-get-info group))
11392         (prev 1)
11393         read)
11394     (if (or (not info) (not active))
11395         ;; There is no info on this group if it was, in fact,
11396         ;; killed.  Gnus stores no information on killed groups, so
11397         ;; there's nothing to be done.
11398         ;; One could store the information somewhere temporarily,
11399         ;; perhaps...  Hmmm...
11400         ()
11401       ;; Remove any negative articles numbers.
11402       (while (and unread (< (car unread) 0))
11403         (setq unread (cdr unread)))
11404       ;; Remove any expired article numbers
11405       (while (and unread (< (car unread) (car active)))
11406         (setq unread (cdr unread)))
11407       ;; Compute the ranges of read articles by looking at the list of
11408       ;; unread articles.
11409       (while unread
11410         (when (/= (car unread) prev)
11411           (push (if (= prev (1- (car unread))) prev
11412                   (cons prev (1- (car unread))))
11413                 read))
11414         (setq prev (1+ (car unread)))
11415         (setq unread (cdr unread)))
11416       (when (<= prev (cdr active))
11417         (push (cons prev (cdr active)) read))
11418       (setq read (if (> (length read) 1) (nreverse read) read))
11419       (if compute
11420           read
11421         (save-excursion
11422           (let (setmarkundo)
11423             ;; Propagate the read marks to the backend.
11424             (when (gnus-check-backend-function 'request-set-mark group)
11425               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11426                     (add (gnus-remove-from-range read (gnus-info-read info))))
11427                 (when (or add del)
11428                   (unless (gnus-check-group group)
11429                     (error "Can't open server for %s" group))
11430                   (gnus-request-set-mark
11431                    group (delq nil (list (if add (list add 'add '(read)))
11432                                          (if del (list del 'del '(read))))))
11433                   (setq setmarkundo
11434                         `(gnus-request-set-mark
11435                           ,group
11436                           ',(delq nil (list
11437                                        (if del (list del 'add '(read)))
11438                                        (if add (list add 'del '(read))))))))))
11439             (set-buffer gnus-group-buffer)
11440             (gnus-undo-register
11441               `(progn
11442                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11443                  (gnus-info-set-read ',info ',(gnus-info-read info))
11444                  (gnus-get-unread-articles-in-group ',info
11445                                                     (gnus-active ,group))
11446                  (gnus-group-update-group ,group t)
11447                  ,setmarkundo))))
11448         ;; Enter this list into the group info.
11449         (gnus-info-set-read info read)
11450         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11451         (gnus-get-unread-articles-in-group info (gnus-active group))
11452         t))))
11453
11454 (defun gnus-offer-save-summaries ()
11455   "Offer to save all active summary buffers."
11456   (let (buffers)
11457     ;; Go through all buffers and find all summaries.
11458     (dolist (buffer (buffer-list))
11459       (when (and (setq buffer (buffer-name buffer))
11460                  (string-match "Summary" buffer)
11461                  (with-current-buffer buffer
11462                    ;; We check that this is, indeed, a summary buffer.
11463                    (and (eq major-mode 'gnus-summary-mode)
11464                         ;; Also make sure this isn't bogus.
11465                         gnus-newsgroup-prepared
11466                         ;; Also make sure that this isn't a
11467                         ;; dead summary buffer.
11468                         (not gnus-dead-summary-mode))))
11469         (push buffer buffers)))
11470     ;; Go through all these summary buffers and offer to save them.
11471     (when buffers
11472       (save-excursion
11473         (map-y-or-n-p
11474          "Update summary buffer %s? "
11475          (lambda (buf)
11476            (switch-to-buffer buf)
11477            (gnus-summary-exit))
11478          buffers)))))
11479
11480 (defun gnus-summary-setup-default-charset ()
11481   "Setup newsgroup default charset."
11482   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11483       (setq gnus-newsgroup-charset nil)
11484     (let* ((ignored-charsets
11485             (or gnus-newsgroup-ephemeral-ignored-charsets
11486                 (append
11487                  (and gnus-newsgroup-name
11488                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11489                  gnus-newsgroup-ignored-charsets))))
11490       (setq gnus-newsgroup-charset
11491             (or gnus-newsgroup-ephemeral-charset
11492                 (and gnus-newsgroup-name
11493                      (gnus-parameter-charset gnus-newsgroup-name))
11494                 gnus-default-charset))
11495       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11496            ignored-charsets))))
11497
11498 ;;;
11499 ;;; Mime Commands
11500 ;;;
11501
11502 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11503   "Display the current article buffer fully MIME-buttonized.
11504 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11505 treated as multipart/mixed."
11506   (interactive "P")
11507   (require 'gnus-art)
11508   (let ((gnus-unbuttonized-mime-types nil)
11509         (gnus-mime-display-multipart-as-mixed show-all-parts))
11510     (gnus-summary-show-article)))
11511
11512 (defun gnus-summary-repair-multipart (article)
11513   "Add a Content-Type header to a multipart article without one."
11514   (interactive (list (gnus-summary-article-number)))
11515   (gnus-with-article article
11516     (message-narrow-to-head)
11517     (message-remove-header "Mime-Version")
11518     (goto-char (point-max))
11519     (insert "Mime-Version: 1.0\n")
11520     (widen)
11521     (when (search-forward "\n--" nil t)
11522       (let ((separator (buffer-substring (point) (point-at-eol))))
11523         (message-narrow-to-head)
11524         (message-remove-header "Content-Type")
11525         (goto-char (point-max))
11526         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11527                         separator))
11528         (widen))))
11529   (let (gnus-mark-article-hook)
11530     (gnus-summary-select-article t t nil article)))
11531
11532 (defun gnus-summary-toggle-display-buttonized ()
11533   "Toggle the buttonizing of the article buffer."
11534   (interactive)
11535   (require 'gnus-art)
11536   (if (setq gnus-inhibit-mime-unbuttonizing
11537             (not gnus-inhibit-mime-unbuttonizing))
11538       (let ((gnus-unbuttonized-mime-types nil))
11539         (gnus-summary-show-article))
11540     (gnus-summary-show-article)))
11541
11542 ;;;
11543 ;;; Generic summary marking commands
11544 ;;;
11545
11546 (defvar gnus-summary-marking-alist
11547   '((read gnus-del-mark "d")
11548     (unread gnus-unread-mark "u")
11549     (ticked gnus-ticked-mark "!")
11550     (dormant gnus-dormant-mark "?")
11551     (expirable gnus-expirable-mark "e"))
11552   "An alist of names/marks/keystrokes.")
11553
11554 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11555 (defvar gnus-summary-mark-map)
11556
11557 (defun gnus-summary-make-all-marking-commands ()
11558   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11559   (dolist (elem gnus-summary-marking-alist)
11560     (apply 'gnus-summary-make-marking-command elem)))
11561
11562 (defun gnus-summary-make-marking-command (name mark keystroke)
11563   (let ((map (make-sparse-keymap)))
11564     (define-key gnus-summary-generic-mark-map keystroke map)
11565     (dolist (lway `((next "next" next nil "n")
11566                     (next-unread "next unread" next t "N")
11567                     (prev "previous" prev nil "p")
11568                     (prev-unread "previous unread" prev t "P")
11569                     (nomove "" nil nil ,keystroke)))
11570       (let ((func (gnus-summary-make-marking-command-1
11571                    mark (car lway) lway name)))
11572         (setq func (eval func))
11573         (define-key map (nth 4 lway) func)))))
11574
11575 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11576   `(defun ,(intern
11577             (format "gnus-summary-put-mark-as-%s%s"
11578                     name (if (eq way 'nomove)
11579                              ""
11580                            (concat "-" (symbol-name way)))))
11581      (n)
11582      ,(format
11583        "Mark the current article as %s%s.
11584 If N, the prefix, then repeat N times.
11585 If N is negative, move in reverse order.
11586 The difference between N and the actual number of articles marked is
11587 returned."
11588        name (cadr lway))
11589      (interactive "p")
11590      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11591
11592 (defun gnus-summary-generic-mark (n mark move unread)
11593   "Mark N articles with MARK."
11594   (unless (eq major-mode 'gnus-summary-mode)
11595     (error "This command can only be used in the summary buffer"))
11596   (gnus-summary-show-thread)
11597   (let ((nummove
11598          (cond
11599           ((eq move 'next) 1)
11600           ((eq move 'prev) -1)
11601           (t 0))))
11602     (if (zerop nummove)
11603         (setq n 1)
11604       (when (< n 0)
11605         (setq n (abs n)
11606               nummove (* -1 nummove))))
11607     (while (and (> n 0)
11608                 (gnus-summary-mark-article nil mark)
11609                 (zerop (gnus-summary-next-subject nummove unread t)))
11610       (setq n (1- n)))
11611     (when (/= 0 n)
11612       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11613     (gnus-summary-recenter)
11614     (gnus-summary-position-point)
11615     (gnus-set-mode-line 'summary)
11616     n))
11617
11618 (defun gnus-summary-insert-articles (articles)
11619   (when (setq articles
11620               (gnus-sorted-difference articles
11621                                       (mapcar (lambda (h)
11622                                                 (mail-header-number h))
11623                                               gnus-newsgroup-headers)))
11624     (setq gnus-newsgroup-headers
11625           (gnus-merge 'list
11626                       gnus-newsgroup-headers
11627                       (gnus-fetch-headers articles)
11628                       'gnus-article-sort-by-number))
11629     ;; Suppress duplicates?
11630     (when gnus-suppress-duplicates
11631       (gnus-dup-suppress-articles))
11632
11633     ;; We might want to build some more threads first.
11634     (when (and gnus-fetch-old-headers
11635                (eq gnus-headers-retrieved-by 'nov))
11636       (if (eq gnus-fetch-old-headers 'invisible)
11637           (gnus-build-all-threads)
11638         (gnus-build-old-threads)))
11639     ;; Let the Gnus agent mark articles as read.
11640     (when gnus-agent
11641       (gnus-agent-get-undownloaded-list))
11642     ;; Remove list identifiers from subject
11643     (when gnus-list-identifiers
11644       (gnus-summary-remove-list-identifiers))
11645     ;; First and last article in this newsgroup.
11646     (when gnus-newsgroup-headers
11647       (setq gnus-newsgroup-begin
11648             (mail-header-number (car gnus-newsgroup-headers))
11649             gnus-newsgroup-end
11650             (mail-header-number
11651              (gnus-last-element gnus-newsgroup-headers))))
11652     (when gnus-use-scoring
11653       (gnus-possibly-score-headers))))
11654
11655 (defun gnus-summary-insert-old-articles (&optional all)
11656   "Insert all old articles in this group.
11657 If ALL is non-nil, already read articles become readable.
11658 If ALL is a number, fetch this number of articles."
11659   (interactive "P")
11660   (prog1
11661       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11662             older len)
11663         (setq older
11664               ;; Some nntp servers lie about their active range.  When
11665               ;; this happens, the active range can be in the millions.
11666               ;; Use a compressed range to avoid creating a huge list.
11667               (gnus-range-difference (list gnus-newsgroup-active) old))
11668         (setq len (gnus-range-length older))
11669         (cond
11670          ((null older) nil)
11671          ((numberp all)
11672           (if (< all len)
11673               (let ((older-range (nreverse older)))
11674                 (setq older nil)
11675
11676                 (while (> all 0)
11677                   (let* ((r (pop older-range))
11678                          (min (if (numberp r) r (car r)))
11679                          (max (if (numberp r) r (cdr r))))
11680                     (while (and (<= min max)
11681                                 (> all 0))
11682                       (push max older)
11683                       (setq all (1- all)
11684                             max (1- max))))))
11685             (setq older (gnus-uncompress-range older))))
11686          (all
11687           (setq older (gnus-uncompress-range older)))
11688          (t
11689           (when (and (numberp gnus-large-newsgroup)
11690                    (> len gnus-large-newsgroup))
11691               (let* ((cursor-in-echo-area nil)
11692                      (initial (gnus-parameter-large-newsgroup-initial
11693                                gnus-newsgroup-name))
11694                      (input
11695                       (read-string
11696                        (format
11697                         "How many articles from %s (%s %d): "
11698                         (gnus-limit-string
11699                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11700                         (if initial "max" "default")
11701                         len)
11702                        (if initial
11703                            (cons (number-to-string initial)
11704                                  0)))))
11705                 (unless (string-match "^[ \t]*$" input)
11706                   (setq all (string-to-number input))
11707                   (if (< all len)
11708                       (let ((older-range (nreverse older)))
11709                         (setq older nil)
11710
11711                         (while (> all 0)
11712                           (let* ((r (pop older-range))
11713                                  (min (if (numberp r) r (car r)))
11714                                  (max (if (numberp r) r (cdr r))))
11715                             (while (and (<= min max)
11716                                         (> all 0))
11717                               (push max older)
11718                               (setq all (1- all)
11719                                     max (1- max))))))))))
11720           (setq older (gnus-uncompress-range older))))
11721         (if (not older)
11722             (message "No old news.")
11723           (gnus-summary-insert-articles older)
11724           (gnus-summary-limit (gnus-sorted-nunion old older))))
11725     (gnus-summary-position-point)))
11726
11727 (defun gnus-summary-insert-new-articles ()
11728   "Insert all new articles in this group."
11729   (interactive)
11730   (prog1
11731       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11732             (old-active gnus-newsgroup-active)
11733             (nnmail-fetched-sources (list t))
11734             i new)
11735         (setq gnus-newsgroup-active
11736               (gnus-activate-group gnus-newsgroup-name 'scan))
11737         (setq i (cdr gnus-newsgroup-active))
11738         (while (> i (cdr old-active))
11739           (push i new)
11740           (decf i))
11741         (if (not new)
11742             (message "No gnus is bad news")
11743           (gnus-summary-insert-articles new)
11744           (setq gnus-newsgroup-unreads
11745                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11746           (gnus-summary-limit (gnus-sorted-nunion old new))))
11747     (gnus-summary-position-point)))
11748
11749 (gnus-summary-make-all-marking-commands)
11750
11751 (gnus-ems-redefine)
11752
11753 (provide 'gnus-sum)
11754
11755 (run-hooks 'gnus-sum-load-hook)
11756
11757 ;; Local Variables:
11758 ;; coding: iso-8859-1
11759 ;; End:
11760
11761 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11762 ;;; gnus-sum.el ends here