d9fe72491ac79bb4d3da6225e2fa0a4960932fd3
[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 t
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
717 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
718   "*List of functions used for sorting articles in the summary buffer.
719
720 Each function takes two articles and returns non-nil if the first
721 article should be sorted before the other.  If you use more than one
722 function, the primary sort function should be the last.  You should
723 probably always include `gnus-article-sort-by-number' in the list of
724 sorting functions -- preferably first.  Also note that sorting by date
725 is often much slower than sorting by number, and the sorting order is
726 very similar.  (Sorting by date means sorting by the time the message
727 was sent, sorting by number means sorting by arrival time.)
728
729 Each item can also be a list `(not F)' where F is a function;
730 this reverses the sort order.
731
732 Ready-made functions include `gnus-article-sort-by-number',
733 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
734 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
735 and `gnus-article-sort-by-score'.
736
737 When threading is turned on, the variable `gnus-thread-sort-functions'
738 controls how articles are sorted."
739   :group 'gnus-summary-sort
740   :type '(repeat (gnus-widget-reversible
741                   (choice (function-item gnus-article-sort-by-number)
742                           (function-item gnus-article-sort-by-author)
743                           (function-item gnus-article-sort-by-subject)
744                           (function-item gnus-article-sort-by-date)
745                           (function-item gnus-article-sort-by-score)
746                           (function-item gnus-article-sort-by-random)
747                           (function :tag "other"))
748                   (boolean :tag "Reverse order"))))
749
750
751 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
752   "*List of functions used for sorting threads in the summary buffer.
753 By default, threads are sorted by article number.
754
755 Each function takes two threads and returns non-nil if the first
756 thread should be sorted before the other.  If you use more than one
757 function, the primary sort function should be the last.  You should
758 probably always include `gnus-thread-sort-by-number' in the list of
759 sorting functions -- preferably first.  Also note that sorting by date
760 is often much slower than sorting by number, and the sorting order is
761 very similar.  (Sorting by date means sorting by the time the message
762 was sent, sorting by number means sorting by arrival time.)
763
764 Each list item can also be a list `(not F)' where F is a
765 function; this specifies reversed sort order.
766
767 Ready-made functions include `gnus-thread-sort-by-number',
768 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
769 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
770 `gnus-thread-sort-by-most-recent-number',
771 `gnus-thread-sort-by-most-recent-date',
772 `gnus-thread-sort-by-random', and
773 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
774
775 When threading is turned off, the variable
776 `gnus-article-sort-functions' controls how articles are sorted."
777   :group 'gnus-summary-sort
778   :type '(repeat 
779           (gnus-widget-reversible
780            (choice (function-item gnus-thread-sort-by-number)
781                    (function-item gnus-thread-sort-by-author)
782                    (function-item gnus-thread-sort-by-subject)
783                    (function-item gnus-thread-sort-by-date)
784                    (function-item gnus-thread-sort-by-score)
785                    (function-item gnus-thread-sort-by-most-recent-number)
786                    (function-item gnus-thread-sort-by-most-recent-date)
787                    (function-item gnus-thread-sort-by-random)
788                    (function-item gnus-thread-sort-by-total-score)
789                    (function :tag "other"))
790            (boolean :tag "Reverse order"))))
791
792 (defcustom gnus-thread-score-function '+
793   "*Function used for calculating the total score of a thread.
794
795 The function is called with the scores of the article and each
796 subthread and should then return the score of the thread.
797
798 Some functions you can use are `+', `max', or `min'."
799   :group 'gnus-summary-sort
800   :type 'function)
801
802 (defcustom gnus-summary-expunge-below nil
803   "All articles that have a score less than this variable will be expunged.
804 This variable is local to the summary buffers."
805   :group 'gnus-score-default
806   :type '(choice (const :tag "off" nil)
807                  integer))
808
809 (defcustom gnus-thread-expunge-below nil
810   "All threads that have a total score less than this variable will be expunged.
811 See `gnus-thread-score-function' for en explanation of what a
812 \"thread score\" is.
813
814 This variable is local to the summary buffers."
815   :group 'gnus-threading
816   :group 'gnus-score-default
817   :type '(choice (const :tag "off" nil)
818                  integer))
819
820 (defcustom gnus-summary-mode-hook nil
821   "*A hook for Gnus summary mode.
822 This hook is run before any variables are set in the summary buffer."
823   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
824   :group 'gnus-summary-various
825   :type 'hook)
826
827 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
828 (when (featurep 'xemacs)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
830   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
831   (add-hook 'gnus-summary-mode-hook
832             'gnus-xmas-switch-horizontal-scrollbar-off))
833
834 (defcustom gnus-summary-menu-hook nil
835   "*Hook run after the creation of the summary mode menu."
836   :group 'gnus-summary-visual
837   :type 'hook)
838
839 (defcustom gnus-summary-exit-hook nil
840   "*A hook called on exit from the summary buffer.
841 It will be called with point in the group buffer."
842   :group 'gnus-summary-exit
843   :type 'hook)
844
845 (defcustom gnus-summary-prepare-hook nil
846   "*A hook called after the summary buffer has been generated.
847 If you want to modify the summary buffer, you can use this hook."
848   :group 'gnus-summary-various
849   :type 'hook)
850
851 (defcustom gnus-summary-prepared-hook nil
852   "*A hook called as the last thing after the summary buffer has been generated."
853   :group 'gnus-summary-various
854   :type 'hook)
855
856 (defcustom gnus-summary-generate-hook nil
857   "*A hook run just before generating the summary buffer.
858 This hook is commonly used to customize threading variables and the
859 like."
860   :group 'gnus-summary-various
861   :type 'hook)
862
863 (defcustom gnus-select-group-hook nil
864   "*A hook called when a newsgroup is selected.
865
866 If you'd like to simplify subjects like the
867 `gnus-summary-next-same-subject' command does, you can use the
868 following hook:
869
870  (add-hook gnus-select-group-hook
871            (lambda ()
872              (mapcar (lambda (header)
873                        (mail-header-set-subject
874                         header
875                         (gnus-simplify-subject
876                          (mail-header-subject header) 're-only)))
877                      gnus-newsgroup-headers)))"
878   :group 'gnus-group-select
879   :type 'hook)
880
881 (defcustom gnus-select-article-hook nil
882   "*A hook called when an article is selected."
883   :group 'gnus-summary-choose
884   :options '(gnus-agent-fetch-selected-article)
885   :type 'hook)
886
887 (defcustom gnus-visual-mark-article-hook
888   (list 'gnus-highlight-selected-summary)
889   "*Hook run after selecting an article in the summary buffer.
890 It is meant to be used for highlighting the article in some way.  It
891 is not run if `gnus-visual' is nil."
892   :group 'gnus-summary-visual
893   :type 'hook)
894
895 (defcustom gnus-parse-headers-hook nil
896   "*A hook called before parsing the headers."
897   :group 'gnus-various
898   :type 'hook)
899
900 (defcustom gnus-exit-group-hook nil
901   "*A hook called when exiting summary mode.
902 This hook is not called from the non-updating exit commands like `Q'."
903   :group 'gnus-various
904   :type 'hook)
905
906 (defcustom gnus-summary-update-hook
907   (list 'gnus-summary-highlight-line)
908   "*A hook called when a summary line is changed.
909 The hook will not be called if `gnus-visual' is nil.
910
911 The default function `gnus-summary-highlight-line' will
912 highlight the line according to the `gnus-summary-highlight'
913 variable."
914   :group 'gnus-summary-visual
915   :type 'hook)
916
917 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
918   "*A hook called when an article is selected for the first time.
919 The hook is intended to mark an article as read (or unread)
920 automatically when it is selected."
921   :group 'gnus-summary-choose
922   :type 'hook)
923
924 (defcustom gnus-group-no-more-groups-hook nil
925   "*A hook run when returning to group mode having no more (unread) groups."
926   :group 'gnus-group-select
927   :type 'hook)
928
929 (defcustom gnus-ps-print-hook nil
930   "*A hook run before ps-printing something from Gnus."
931   :group 'gnus-summary
932   :type 'hook)
933
934 (defcustom gnus-summary-article-move-hook nil
935   "*A hook called after an article is moved, copied, respooled, or crossposted."
936   :group 'gnus-summary
937   :type 'hook)
938
939 (defcustom gnus-summary-article-delete-hook nil
940   "*A hook called after an article is deleted."
941   :group 'gnus-summary
942   :type 'hook)
943
944 (defcustom gnus-summary-article-expire-hook nil
945   "*A hook called after an article is expired."
946   :group 'gnus-summary
947   :type 'hook)
948
949 (defcustom gnus-summary-display-arrow
950   (and (fboundp 'display-graphic-p)
951        (display-graphic-p))
952   "*If non-nil, display an arrow highlighting the current article."
953   :version "21.1"
954   :group 'gnus-summary
955   :type 'boolean)
956
957 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
958   "Face used for highlighting the current article in the summary buffer."
959   :group 'gnus-summary-visual
960   :type 'face)
961
962 (defvar gnus-tmp-downloaded nil)
963
964 (defcustom gnus-summary-highlight
965   '(((eq mark gnus-canceled-mark)
966      . gnus-summary-cancelled-face)
967     ((and uncached (> score default-high))
968      . gnus-summary-high-undownloaded-face)
969     ((and uncached (< score default-low))
970      . gnus-summary-low-undownloaded-face)
971     (uncached
972      . gnus-summary-normal-undownloaded-face)
973     ((and (> score default-high)
974           (or (eq mark gnus-dormant-mark)
975               (eq mark gnus-ticked-mark)))
976      . gnus-summary-high-ticked-face)
977     ((and (< score default-low)
978           (or (eq mark gnus-dormant-mark)
979               (eq mark gnus-ticked-mark)))
980      . gnus-summary-low-ticked-face)
981     ((or (eq mark gnus-dormant-mark)
982          (eq mark gnus-ticked-mark))
983      . gnus-summary-normal-ticked-face)
984     ((and (> score default-high) (eq mark gnus-ancient-mark))
985      . gnus-summary-high-ancient-face)
986     ((and (< score default-low) (eq mark gnus-ancient-mark))
987      . gnus-summary-low-ancient-face)
988     ((eq mark gnus-ancient-mark)
989      . gnus-summary-normal-ancient-face)
990     ((and (> score default-high) (eq mark gnus-unread-mark))
991      . gnus-summary-high-unread-face)
992     ((and (< score default-low) (eq mark gnus-unread-mark))
993      . gnus-summary-low-unread-face)
994     ((eq mark gnus-unread-mark)
995      . gnus-summary-normal-unread-face)
996     ((> score default-high)
997      . gnus-summary-high-read-face)
998     ((< score default-low)
999      . gnus-summary-low-read-face)
1000     (t
1001      . gnus-summary-normal-read-face))
1002   "*Controls the highlighting of summary buffer lines.
1003
1004 A list of (FORM . FACE) pairs.  When deciding how a a particular
1005 summary line should be displayed, each form is evaluated.  The content
1006 of the face field after the first true form is used.  You can change
1007 how those summary lines are displayed, by editing the face field.
1008
1009 You can use the following variables in the FORM field.
1010
1011 score:        The article's score
1012 default:      The default article score.
1013 default-high: The default score for high scored articles.
1014 default-low:  The default score for low scored articles.
1015 below:        The score below which articles are automatically marked as read.
1016 mark:         The article's mark.
1017 uncached:     Non-nil if the article is uncached."
1018   :group 'gnus-summary-visual
1019   :type '(repeat (cons (sexp :tag "Form" nil)
1020                        face)))
1021
1022 (defcustom gnus-alter-header-function nil
1023   "Function called to allow alteration of article header structures.
1024 The function is called with one parameter, the article header vector,
1025 which it may alter in any way."
1026   :type '(choice (const :tag "None" nil)
1027                  function)
1028   :group 'gnus-summary)
1029
1030 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1031   "Variable that says which function should be used to decode a string with encoded words.")
1032
1033 (defcustom gnus-extra-headers '(To Newsgroups)
1034   "*Extra headers to parse."
1035   :version "21.1"
1036   :group 'gnus-summary
1037   :type '(repeat symbol))
1038
1039 (defcustom gnus-ignored-from-addresses
1040   (and user-mail-address (regexp-quote user-mail-address))
1041   "*Regexp of From headers that may be suppressed in favor of To headers."
1042   :version "21.1"
1043   :group 'gnus-summary
1044   :type 'regexp)
1045
1046 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1047   "List of charsets that should be ignored.
1048 When these charsets are used in the \"charset\" parameter, the
1049 default charset will be used instead."
1050   :version "21.1"
1051   :type '(repeat symbol)
1052   :group 'gnus-charset)
1053
1054 (gnus-define-group-parameter
1055  ignored-charsets
1056  :type list
1057  :function-document
1058  "Return the ignored charsets of GROUP."
1059  :variable gnus-group-ignored-charsets-alist
1060  :variable-default
1061  '(("alt\\.chinese\\.text" iso-8859-1))
1062  :variable-document
1063  "Alist of regexps (to match group names) and charsets that should be ignored.
1064 When these charsets are used in the \"charset\" parameter, the
1065 default charset will be used instead."
1066  :variable-group gnus-charset
1067  :variable-type '(repeat (cons (regexp :tag "Group")
1068                                (repeat symbol)))
1069  :parameter-type '(choice :tag "Ignored charsets"
1070                           :value nil
1071                           (repeat (symbol)))
1072  :parameter-document       "\
1073 List of charsets that should be ignored.
1074
1075 When these charsets are used in the \"charset\" parameter, the
1076 default charset will be used instead.")
1077
1078 (defcustom gnus-group-highlight-words-alist nil
1079   "Alist of group regexps and highlight regexps.
1080 This variable uses the same syntax as `gnus-emphasis-alist'."
1081   :version "21.1"
1082   :type '(repeat (cons (regexp :tag "Group")
1083                        (repeat (list (regexp :tag "Highlight regexp")
1084                                      (number :tag "Group for entire word" 0)
1085                                      (number :tag "Group for displayed part" 0)
1086                                      (symbol :tag "Face"
1087                                              gnus-emphasis-highlight-words)))))
1088   :group 'gnus-summary-visual)
1089
1090 (defcustom gnus-summary-show-article-charset-alist
1091   nil
1092   "Alist of number and charset.
1093 The article will be shown with the charset corresponding to the
1094 numbered argument.
1095 For example: ((1 . cn-gb-2312) (2 . big5))."
1096   :version "21.1"
1097   :type '(repeat (cons (number :tag "Argument" 1)
1098                        (symbol :tag "Charset")))
1099   :group 'gnus-charset)
1100
1101 (defcustom gnus-preserve-marks t
1102   "Whether marks are preserved when moving, copying and respooling messages."
1103   :version "21.1"
1104   :type 'boolean
1105   :group 'gnus-summary-marks)
1106
1107 (defcustom gnus-alter-articles-to-read-function nil
1108   "Function to be called to alter the list of articles to be selected."
1109   :type '(choice (const nil) function)
1110   :group 'gnus-summary)
1111
1112 (defcustom gnus-orphan-score nil
1113   "*All orphans get this score added.  Set in the score file."
1114   :group 'gnus-score-default
1115   :type '(choice (const nil)
1116                  integer))
1117
1118 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1119   "*A regexp to match MIME parts when saving multiple parts of a
1120 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1121 This regexp will be used by default when prompting the user for which
1122 type of files to save."
1123   :group 'gnus-summary
1124   :type 'regexp)
1125
1126 (defcustom gnus-read-all-available-headers nil
1127   "Whether Gnus should parse all headers made available to it.
1128 This is mostly relevant for slow back ends where the user may
1129 wish to widen the summary buffer to include all headers
1130 that were fetched.  Say, for nnultimate groups."
1131   :group 'gnus-summary
1132   :type '(choice boolean regexp))
1133
1134 (defcustom gnus-summary-muttprint-program "muttprint"
1135   "Command (and optional arguments) used to run Muttprint."
1136   :version "21.3"
1137   :group 'gnus-summary
1138   :type 'string)
1139
1140 (defcustom gnus-article-loose-mime nil
1141   "If non-nil, don't require MIME-Version header.
1142 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1143 supply the MIME-Version header or deliberately strip it From the mail.
1144 Set it to non-nil, Gnus will treat some articles as MIME even if
1145 the MIME-Version header is missed."
1146   :version "21.3"
1147   :type 'boolean
1148   :group 'gnus-article-mime)
1149
1150 (defcustom gnus-article-emulate-mime t
1151   "If non-nil, use MIME emulation for uuencode and the like.
1152 This means that Gnus will search message bodies for text that look
1153 like uuencoded bits, yEncoded bits, and so on, and present that using
1154 the normal Gnus MIME machinery."
1155   :type 'boolean
1156   :group 'gnus-article-mime)
1157
1158 ;;; Internal variables
1159
1160 (defvar gnus-summary-display-cache nil)
1161 (defvar gnus-article-mime-handles nil)
1162 (defvar gnus-article-decoded-p nil)
1163 (defvar gnus-article-charset nil)
1164 (defvar gnus-article-ignored-charsets nil)
1165 (defvar gnus-scores-exclude-files nil)
1166 (defvar gnus-page-broken nil)
1167
1168 (defvar gnus-original-article nil)
1169 (defvar gnus-article-internal-prepare-hook nil)
1170 (defvar gnus-newsgroup-process-stack nil)
1171
1172 (defvar gnus-thread-indent-array nil)
1173 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1174 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1175   "Function called to sort the articles within a thread after it has been gathered together.")
1176
1177 (defvar gnus-summary-save-parts-type-history nil)
1178 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1179
1180 ;; Avoid highlighting in kill files.
1181 (defvar gnus-summary-inhibit-highlight nil)
1182 (defvar gnus-newsgroup-selected-overlay nil)
1183 (defvar gnus-inhibit-limiting nil)
1184 (defvar gnus-newsgroup-adaptive-score-file nil)
1185 (defvar gnus-current-score-file nil)
1186 (defvar gnus-current-move-group nil)
1187 (defvar gnus-current-copy-group nil)
1188 (defvar gnus-current-crosspost-group nil)
1189 (defvar gnus-newsgroup-display nil)
1190
1191 (defvar gnus-newsgroup-dependencies nil)
1192 (defvar gnus-newsgroup-adaptive nil)
1193 (defvar gnus-summary-display-article-function nil)
1194 (defvar gnus-summary-highlight-line-function nil
1195   "Function called after highlighting a summary line.")
1196
1197 (defvar gnus-summary-line-format-alist
1198   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1199     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1200     (?s gnus-tmp-subject-or-nil ?s)
1201     (?n gnus-tmp-name ?s)
1202     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1203         ?s)
1204     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1205             gnus-tmp-from) ?s)
1206     (?F gnus-tmp-from ?s)
1207     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1208     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1209     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1210     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1211     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1212     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1213     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1214     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1215     (?L gnus-tmp-lines ?s)
1216     (?O gnus-tmp-downloaded ?c)
1217     (?I gnus-tmp-indentation ?s)
1218     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1219     (?R gnus-tmp-replied ?c)
1220     (?\[ gnus-tmp-opening-bracket ?c)
1221     (?\] gnus-tmp-closing-bracket ?c)
1222     (?\> (make-string gnus-tmp-level ? ) ?s)
1223     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1224     (?i gnus-tmp-score ?d)
1225     (?z gnus-tmp-score-char ?c)
1226     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1227     (?U gnus-tmp-unread ?c)
1228     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1229         ?s)
1230     (?t (gnus-summary-number-of-articles-in-thread
1231          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1232         ?d)
1233     (?e (gnus-summary-number-of-articles-in-thread
1234          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1235         ?c)
1236     (?u gnus-tmp-user-defined ?s)
1237     (?P (gnus-pick-line-number) ?d)
1238     (?B gnus-tmp-thread-tree-header-string ?s)
1239     (user-date (gnus-user-date
1240                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1241   "An alist of format specifications that can appear in summary lines.
1242 These are paired with what variables they correspond with, along with
1243 the type of the variable (string, integer, character, etc).")
1244
1245 (defvar gnus-summary-dummy-line-format-alist
1246   `((?S gnus-tmp-subject ?s)
1247     (?N gnus-tmp-number ?d)
1248     (?u gnus-tmp-user-defined ?s)))
1249
1250 (defvar gnus-summary-mode-line-format-alist
1251   `((?G gnus-tmp-group-name ?s)
1252     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1253     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1254     (?A gnus-tmp-article-number ?d)
1255     (?Z gnus-tmp-unread-and-unselected ?s)
1256     (?V gnus-version ?s)
1257     (?U gnus-tmp-unread-and-unticked ?d)
1258     (?S gnus-tmp-subject ?s)
1259     (?e gnus-tmp-unselected ?d)
1260     (?u gnus-tmp-user-defined ?s)
1261     (?d (length gnus-newsgroup-dormant) ?d)
1262     (?t (length gnus-newsgroup-marked) ?d)
1263     (?h (length gnus-newsgroup-spam-marked) ?d)
1264     (?r (length gnus-newsgroup-reads) ?d)
1265     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1266     (?E gnus-newsgroup-expunged-tally ?d)
1267     (?s (gnus-current-score-file-nondirectory) ?s)))
1268
1269 (defvar gnus-last-search-regexp nil
1270   "Default regexp for article search command.")
1271
1272 (defvar gnus-last-shell-command nil
1273   "Default shell command on article.")
1274
1275 (defvar gnus-newsgroup-agentized nil
1276   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1277 (defvar gnus-newsgroup-begin nil)
1278 (defvar gnus-newsgroup-end nil)
1279 (defvar gnus-newsgroup-last-rmail nil)
1280 (defvar gnus-newsgroup-last-mail nil)
1281 (defvar gnus-newsgroup-last-folder nil)
1282 (defvar gnus-newsgroup-last-file nil)
1283 (defvar gnus-newsgroup-auto-expire nil)
1284 (defvar gnus-newsgroup-active nil)
1285
1286 (defvar gnus-newsgroup-data nil)
1287 (defvar gnus-newsgroup-data-reverse nil)
1288 (defvar gnus-newsgroup-limit nil)
1289 (defvar gnus-newsgroup-limits nil)
1290 (defvar gnus-summary-use-undownloaded-faces nil)
1291
1292 (defvar gnus-newsgroup-unreads nil
1293   "Sorted list of unread articles in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-unselected nil
1296   "Sorted list of unselected unread articles in the current newsgroup.")
1297
1298 (defvar gnus-newsgroup-reads nil
1299   "Alist of read articles and article marks in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-expunged-tally nil)
1302
1303 (defvar gnus-newsgroup-marked nil
1304   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1305
1306 (defvar gnus-newsgroup-spam-marked nil
1307   "List of ranges of articles that have been marked as spam.")
1308
1309 (defvar gnus-newsgroup-killed nil
1310   "List of ranges of articles that have been through the scoring process.")
1311
1312 (defvar gnus-newsgroup-cached nil
1313   "Sorted list of articles that come from the article cache.")
1314
1315 (defvar gnus-newsgroup-saved nil
1316   "List of articles that have been saved.")
1317
1318 (defvar gnus-newsgroup-kill-headers nil)
1319
1320 (defvar gnus-newsgroup-replied nil
1321   "List of articles that have been replied to in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-forwarded nil
1324   "List of articles that have been forwarded in the current newsgroup.")
1325
1326 (defvar gnus-newsgroup-recent nil
1327   "List of articles that have are recent in the current newsgroup.")
1328
1329 (defvar gnus-newsgroup-expirable nil
1330   "Sorted list of articles in the current newsgroup that can be expired.")
1331
1332 (defvar gnus-newsgroup-processable nil
1333   "List of articles in the current newsgroup that can be processed.")
1334
1335 (defvar gnus-newsgroup-downloadable nil
1336   "Sorted list of articles in the current newsgroup that can be processed.")
1337
1338 (defvar gnus-newsgroup-unfetched nil
1339   "Sorted list of articles in the current newsgroup whose headers have
1340 not been fetched into the agent.
1341
1342 This list will always be a subset of gnus-newsgroup-undownloaded.")
1343
1344 (defvar gnus-newsgroup-undownloaded nil
1345   "List of articles in the current newsgroup that haven't been downloaded.")
1346
1347 (defvar gnus-newsgroup-unsendable nil
1348   "List of articles in the current newsgroup that won't be sent.")
1349
1350 (defvar gnus-newsgroup-bookmarks nil
1351   "List of articles in the current newsgroup that have bookmarks.")
1352
1353 (defvar gnus-newsgroup-dormant nil
1354   "Sorted list of dormant articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-unseen nil
1357   "List of unseen articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-seen nil
1360   "Range of seen articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-articles nil
1363   "List of articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-scored nil
1366   "List of scored articles in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-headers nil
1369   "List of article headers in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-threads nil)
1372
1373 (defvar gnus-newsgroup-prepared nil
1374   "Whether the current group has been prepared properly.")
1375
1376 (defvar gnus-newsgroup-ancient nil
1377   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-sparse nil)
1380
1381 (defvar gnus-current-article nil)
1382 (defvar gnus-article-current nil)
1383 (defvar gnus-current-headers nil)
1384 (defvar gnus-have-all-headers nil)
1385 (defvar gnus-last-article nil)
1386 (defvar gnus-newsgroup-history nil)
1387 (defvar gnus-newsgroup-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-charset nil)
1389 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1390
1391 (defvar gnus-article-before-search nil)
1392
1393 (defvar gnus-summary-local-variables
1394   '(gnus-newsgroup-name
1395     gnus-newsgroup-begin gnus-newsgroup-end
1396     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1397     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1398     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1399     gnus-newsgroup-unselected gnus-newsgroup-marked
1400     gnus-newsgroup-spam-marked
1401     gnus-newsgroup-reads gnus-newsgroup-saved
1402     gnus-newsgroup-replied gnus-newsgroup-forwarded
1403     gnus-newsgroup-recent
1404     gnus-newsgroup-expirable
1405     gnus-newsgroup-processable gnus-newsgroup-killed
1406     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1407     gnus-newsgroup-unfetched
1408     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1409     gnus-newsgroup-seen gnus-newsgroup-articles
1410     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1411     gnus-newsgroup-headers gnus-newsgroup-threads
1412     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1413     gnus-current-article gnus-current-headers gnus-have-all-headers
1414     gnus-last-article gnus-article-internal-prepare-hook
1415     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1416     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1417     gnus-thread-expunge-below
1418     gnus-score-alist gnus-current-score-file
1419     (gnus-summary-expunge-below . global)
1420     (gnus-summary-mark-below . global)
1421     (gnus-orphan-score . global)
1422     gnus-newsgroup-active gnus-scores-exclude-files
1423     gnus-newsgroup-history gnus-newsgroup-ancient
1424     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1425     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1426     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1427     (gnus-newsgroup-expunged-tally . 0)
1428     gnus-cache-removable-articles gnus-newsgroup-cached
1429     gnus-newsgroup-data gnus-newsgroup-data-reverse
1430     gnus-newsgroup-limit gnus-newsgroup-limits
1431     gnus-newsgroup-charset gnus-newsgroup-display
1432     gnus-summary-use-undownloaded-faces)
1433   "Variables that are buffer-local to the summary buffers.")
1434
1435 (defvar gnus-newsgroup-variables nil
1436   "A list of variables that have separate values in different newsgroups.
1437 A list of newsgroup (summary buffer) local variables, or cons of
1438 variables and their default expressions to be evalled (when the default
1439 values are not nil), that should be made global while the summary buffer
1440 is active.
1441
1442 Note: The default expressions will be evaluated (using function `eval')
1443 before assignment to the local variable rather than just assigned to it.
1444 If the default expression is the symbol `global', that symbol will not
1445 be evaluated but the global value of the local variable will be used
1446 instead.
1447
1448 These variables can be used to set variables in the group parameters
1449 while still allowing them to affect operations done in other buffers.
1450 For example:
1451
1452 \(setq gnus-newsgroup-variables
1453      '(message-use-followup-to
1454        (gnus-visible-headers .
1455          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1456 ")
1457
1458 ;; Byte-compiler warning.
1459 (eval-when-compile
1460   ;; Bind features so that require will believe that gnus-sum has
1461   ;; already been loaded (avoids infinite recursion)
1462   (let ((features (cons 'gnus-sum features)))
1463     ;; Several of the declarations in gnus-sum are needed to load the
1464     ;; following files. Right now, these definitions have been
1465     ;; compiled but not defined (evaluated).  We could either do a
1466     ;; eval-and-compile about all of the declarations or evaluate the
1467     ;; source file.
1468     (if (boundp 'gnus-newsgroup-variables)
1469         nil
1470       (load "gnus-sum.el" t t t))
1471     (require 'gnus)
1472     (require 'gnus-agent)
1473     (require 'gnus-art)))
1474
1475 ;; MIME stuff.
1476
1477 (defvar gnus-decode-encoded-word-methods
1478   '(mail-decode-encoded-word-string)
1479   "List of methods used to decode encoded words.
1480
1481 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1482 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1483 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1484 whose names match REGEXP.
1485
1486 For example:
1487 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1488  mail-decode-encoded-word-string
1489  (\"chinese\" . rfc1843-decode-string))")
1490
1491 (defvar gnus-decode-encoded-word-methods-cache nil)
1492
1493 (defun gnus-multi-decode-encoded-word-string (string)
1494   "Apply the functions from `gnus-encoded-word-methods' that match."
1495   (unless (and gnus-decode-encoded-word-methods-cache
1496                (eq gnus-newsgroup-name
1497                    (car gnus-decode-encoded-word-methods-cache)))
1498     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1499     (mapcar (lambda (x)
1500               (if (symbolp x)
1501                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1502                 (if (and gnus-newsgroup-name
1503                          (string-match (car x) gnus-newsgroup-name))
1504                     (nconc gnus-decode-encoded-word-methods-cache
1505                            (list (cdr x))))))
1506             gnus-decode-encoded-word-methods))
1507   (let ((xlist gnus-decode-encoded-word-methods-cache))
1508     (pop xlist)
1509     (while xlist
1510       (setq string (funcall (pop xlist) string))))
1511   string)
1512
1513 ;; Subject simplification.
1514
1515 (defun gnus-simplify-whitespace (str)
1516   "Remove excessive whitespace from STR."
1517   ;; Multiple spaces.
1518   (while (string-match "[ \t][ \t]+" str)
1519     (setq str (concat (substring str 0 (match-beginning 0))
1520                         " "
1521                         (substring str (match-end 0)))))
1522   ;; Leading spaces.
1523   (when (string-match "^[ \t]+" str)
1524     (setq str (substring str (match-end 0))))
1525   ;; Trailing spaces.
1526   (when (string-match "[ \t]+$" str)
1527     (setq str (substring str 0 (match-beginning 0))))
1528   str)
1529
1530 (defun gnus-simplify-all-whitespace (str)
1531   "Remove all whitespace from STR."
1532   (while (string-match "[ \t\n]+" str)
1533     (setq str (replace-match "" nil nil str)))
1534   str)
1535
1536 (defsubst gnus-simplify-subject-re (subject)
1537   "Remove \"Re:\" from subject lines."
1538   (if (string-match message-subject-re-regexp subject)
1539       (substring subject (match-end 0))
1540     subject))
1541
1542 (defun gnus-simplify-subject (subject &optional re-only)
1543   "Remove `Re:' and words in parentheses.
1544 If RE-ONLY is non-nil, strip leading `Re:'s only."
1545   (let ((case-fold-search t))           ;Ignore case.
1546     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1547     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1548       (setq subject (substring subject (match-end 0))))
1549     ;; Remove uninteresting prefixes.
1550     (when (and (not re-only)
1551                gnus-simplify-ignored-prefixes
1552                (string-match gnus-simplify-ignored-prefixes subject))
1553       (setq subject (substring subject (match-end 0))))
1554     ;; Remove words in parentheses from end.
1555     (unless re-only
1556       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1557         (setq subject (substring subject 0 (match-beginning 0)))))
1558     ;; Return subject string.
1559     subject))
1560
1561 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1562 ;; all whitespace.
1563 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1564   (goto-char (point-min))
1565   (while (re-search-forward regexp nil t)
1566     (replace-match (or newtext ""))))
1567
1568 (defun gnus-simplify-buffer-fuzzy ()
1569   "Simplify string in the buffer fuzzily.
1570 The string in the accessible portion of the current buffer is simplified.
1571 It is assumed to be a single-line subject.
1572 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1573 matter is removed.  Additional things can be deleted by setting
1574 `gnus-simplify-subject-fuzzy-regexp'."
1575   (let ((case-fold-search t)
1576         (modified-tick))
1577     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1578
1579     (while (not (eq modified-tick (buffer-modified-tick)))
1580       (setq modified-tick (buffer-modified-tick))
1581       (cond
1582        ((listp gnus-simplify-subject-fuzzy-regexp)
1583         (mapcar 'gnus-simplify-buffer-fuzzy-step
1584                 gnus-simplify-subject-fuzzy-regexp))
1585        (gnus-simplify-subject-fuzzy-regexp
1586         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1587       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1588       (gnus-simplify-buffer-fuzzy-step
1589        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1590       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1591
1592     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1593     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1594     (gnus-simplify-buffer-fuzzy-step " $")
1595     (gnus-simplify-buffer-fuzzy-step "^ +")))
1596
1597 (defun gnus-simplify-subject-fuzzy (subject)
1598   "Simplify a subject string fuzzily.
1599 See `gnus-simplify-buffer-fuzzy' for details."
1600   (save-excursion
1601     (gnus-set-work-buffer)
1602     (let ((case-fold-search t))
1603       ;; Remove uninteresting prefixes.
1604       (when (and gnus-simplify-ignored-prefixes
1605                  (string-match gnus-simplify-ignored-prefixes subject))
1606         (setq subject (substring subject (match-end 0))))
1607       (insert subject)
1608       (inline (gnus-simplify-buffer-fuzzy))
1609       (buffer-string))))
1610
1611 (defsubst gnus-simplify-subject-fully (subject)
1612   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1613   (cond
1614    (gnus-simplify-subject-functions
1615     (gnus-map-function gnus-simplify-subject-functions subject))
1616    ((null gnus-summary-gather-subject-limit)
1617     (gnus-simplify-subject-re subject))
1618    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1619     (gnus-simplify-subject-fuzzy subject))
1620    ((numberp gnus-summary-gather-subject-limit)
1621     (gnus-limit-string (gnus-simplify-subject-re subject)
1622                        gnus-summary-gather-subject-limit))
1623    (t
1624     subject)))
1625
1626 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1627   "Check whether two subjects are equal.
1628 If optional argument SIMPLE-FIRST is t, first argument is already
1629 simplified."
1630   (cond
1631    ((null simple-first)
1632     (equal (gnus-simplify-subject-fully s1)
1633            (gnus-simplify-subject-fully s2)))
1634    (t
1635     (equal s1
1636            (gnus-simplify-subject-fully s2)))))
1637
1638 (defun gnus-summary-bubble-group ()
1639   "Increase the score of the current group.
1640 This is a handy function to add to `gnus-summary-exit-hook' to
1641 increase the score of each group you read."
1642   (gnus-group-add-score gnus-newsgroup-name))
1643
1644 \f
1645 ;;;
1646 ;;; Gnus summary mode
1647 ;;;
1648
1649 (put 'gnus-summary-mode 'mode-class 'special)
1650
1651 (defvar gnus-article-commands-menu)
1652
1653 ;; Non-orthogonal keys
1654
1655 (gnus-define-keys gnus-summary-mode-map
1656   " " gnus-summary-next-page
1657   "\177" gnus-summary-prev-page
1658   [delete] gnus-summary-prev-page
1659   [backspace] gnus-summary-prev-page
1660   "\r" gnus-summary-scroll-up
1661   "\M-\r" gnus-summary-scroll-down
1662   "n" gnus-summary-next-unread-article
1663   "p" gnus-summary-prev-unread-article
1664   "N" gnus-summary-next-article
1665   "P" gnus-summary-prev-article
1666   "\M-\C-n" gnus-summary-next-same-subject
1667   "\M-\C-p" gnus-summary-prev-same-subject
1668   "\M-n" gnus-summary-next-unread-subject
1669   "\M-p" gnus-summary-prev-unread-subject
1670   "." gnus-summary-first-unread-article
1671   "," gnus-summary-best-unread-article
1672   "\M-s" gnus-summary-search-article-forward
1673   "\M-r" gnus-summary-search-article-backward
1674   "<" gnus-summary-beginning-of-article
1675   ">" gnus-summary-end-of-article
1676   "j" gnus-summary-goto-article
1677   "^" gnus-summary-refer-parent-article
1678   "\M-^" gnus-summary-refer-article
1679   "u" gnus-summary-tick-article-forward
1680   "!" gnus-summary-tick-article-forward
1681   "U" gnus-summary-tick-article-backward
1682   "d" gnus-summary-mark-as-read-forward
1683   "D" gnus-summary-mark-as-read-backward
1684   "E" gnus-summary-mark-as-expirable
1685   "\M-u" gnus-summary-clear-mark-forward
1686   "\M-U" gnus-summary-clear-mark-backward
1687   "k" gnus-summary-kill-same-subject-and-select
1688   "\C-k" gnus-summary-kill-same-subject
1689   "\M-\C-k" gnus-summary-kill-thread
1690   "\M-\C-l" gnus-summary-lower-thread
1691   "e" gnus-summary-edit-article
1692   "#" gnus-summary-mark-as-processable
1693   "\M-#" gnus-summary-unmark-as-processable
1694   "\M-\C-t" gnus-summary-toggle-threads
1695   "\M-\C-s" gnus-summary-show-thread
1696   "\M-\C-h" gnus-summary-hide-thread
1697   "\M-\C-f" gnus-summary-next-thread
1698   "\M-\C-b" gnus-summary-prev-thread
1699   [(meta down)] gnus-summary-next-thread
1700   [(meta up)] gnus-summary-prev-thread
1701   "\M-\C-u" gnus-summary-up-thread
1702   "\M-\C-d" gnus-summary-down-thread
1703   "&" gnus-summary-execute-command
1704   "c" gnus-summary-catchup-and-exit
1705   "\C-w" gnus-summary-mark-region-as-read
1706   "\C-t" gnus-summary-toggle-truncation
1707   "?" gnus-summary-mark-as-dormant
1708   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1709   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1710   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1711   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1712   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1713   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1714   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1715   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1716   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1717   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1718   "=" gnus-summary-expand-window
1719   "\C-x\C-s" gnus-summary-reselect-current-group
1720   "\M-g" gnus-summary-rescan-group
1721   "w" gnus-summary-stop-page-breaking
1722   "\C-c\C-r" gnus-summary-caesar-message
1723   "f" gnus-summary-followup
1724   "F" gnus-summary-followup-with-original
1725   "C" gnus-summary-cancel-article
1726   "r" gnus-summary-reply
1727   "R" gnus-summary-reply-with-original
1728   "\C-c\C-f" gnus-summary-mail-forward
1729   "o" gnus-summary-save-article
1730   "\C-o" gnus-summary-save-article-mail
1731   "|" gnus-summary-pipe-output
1732   "\M-k" gnus-summary-edit-local-kill
1733   "\M-K" gnus-summary-edit-global-kill
1734   ;; "V" gnus-version
1735   "\C-c\C-d" gnus-summary-describe-group
1736   "q" gnus-summary-exit
1737   "Q" gnus-summary-exit-no-update
1738   "\C-c\C-i" gnus-info-find-node
1739   gnus-mouse-2 gnus-mouse-pick-article
1740   "m" gnus-summary-mail-other-window
1741   "a" gnus-summary-post-news
1742   "i" gnus-summary-news-other-window
1743   "x" gnus-summary-limit-to-unread
1744   "s" gnus-summary-isearch-article
1745   "t" gnus-summary-toggle-header
1746   "g" gnus-summary-show-article
1747   "l" gnus-summary-goto-last-article
1748   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1749   "\C-d" gnus-summary-enter-digest-group
1750   "\M-\C-d" gnus-summary-read-document
1751   "\M-\C-e" gnus-summary-edit-parameters
1752   "\M-\C-a" gnus-summary-customize-parameters
1753   "\C-c\C-b" gnus-bug
1754   "*" gnus-cache-enter-article
1755   "\M-*" gnus-cache-remove-article
1756   "\M-&" gnus-summary-universal-argument
1757   "\C-l" gnus-recenter
1758   "I" gnus-summary-increase-score
1759   "L" gnus-summary-lower-score
1760   "\M-i" gnus-symbolic-argument
1761   "h" gnus-summary-select-article-buffer
1762
1763   "b" gnus-article-view-part
1764   "\M-t" gnus-summary-toggle-display-buttonized
1765
1766   "V" gnus-summary-score-map
1767   "X" gnus-uu-extract-map
1768   "S" gnus-summary-send-map)
1769
1770 ;; Sort of orthogonal keymap
1771 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1772   "t" gnus-summary-tick-article-forward
1773   "!" gnus-summary-tick-article-forward
1774   "d" gnus-summary-mark-as-read-forward
1775   "r" gnus-summary-mark-as-read-forward
1776   "c" gnus-summary-clear-mark-forward
1777   " " gnus-summary-clear-mark-forward
1778   "e" gnus-summary-mark-as-expirable
1779   "x" gnus-summary-mark-as-expirable
1780   "?" gnus-summary-mark-as-dormant
1781   "b" gnus-summary-set-bookmark
1782   "B" gnus-summary-remove-bookmark
1783   "#" gnus-summary-mark-as-processable
1784   "\M-#" gnus-summary-unmark-as-processable
1785   "S" gnus-summary-limit-include-expunged
1786   "C" gnus-summary-catchup
1787   "H" gnus-summary-catchup-to-here
1788   "h" gnus-summary-catchup-from-here
1789   "\C-c" gnus-summary-catchup-all
1790   "k" gnus-summary-kill-same-subject-and-select
1791   "K" gnus-summary-kill-same-subject
1792   "P" gnus-uu-mark-map)
1793
1794 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1795   "c" gnus-summary-clear-above
1796   "u" gnus-summary-tick-above
1797   "m" gnus-summary-mark-above
1798   "k" gnus-summary-kill-below)
1799
1800 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1801   "/" gnus-summary-limit-to-subject
1802   "n" gnus-summary-limit-to-articles
1803   "w" gnus-summary-pop-limit
1804   "s" gnus-summary-limit-to-subject
1805   "a" gnus-summary-limit-to-author
1806   "u" gnus-summary-limit-to-unread
1807   "m" gnus-summary-limit-to-marks
1808   "M" gnus-summary-limit-exclude-marks
1809   "v" gnus-summary-limit-to-score
1810   "*" gnus-summary-limit-include-cached
1811   "D" gnus-summary-limit-include-dormant
1812   "T" gnus-summary-limit-include-thread
1813   "d" gnus-summary-limit-exclude-dormant
1814   "t" gnus-summary-limit-to-age
1815   "." gnus-summary-limit-to-unseen
1816   "x" gnus-summary-limit-to-extra
1817   "p" gnus-summary-limit-to-display-predicate
1818   "E" gnus-summary-limit-include-expunged
1819   "c" gnus-summary-limit-exclude-childless-dormant
1820   "C" gnus-summary-limit-mark-excluded-as-read
1821   "o" gnus-summary-insert-old-articles
1822   "N" gnus-summary-insert-new-articles
1823   "r" gnus-summary-limit-to-replied)
1824
1825 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1826   "n" gnus-summary-next-unread-article
1827   "p" gnus-summary-prev-unread-article
1828   "N" gnus-summary-next-article
1829   "P" gnus-summary-prev-article
1830   "\C-n" gnus-summary-next-same-subject
1831   "\C-p" gnus-summary-prev-same-subject
1832   "\M-n" gnus-summary-next-unread-subject
1833   "\M-p" gnus-summary-prev-unread-subject
1834   "f" gnus-summary-first-unread-article
1835   "b" gnus-summary-best-unread-article
1836   "j" gnus-summary-goto-article
1837   "g" gnus-summary-goto-subject
1838   "l" gnus-summary-goto-last-article
1839   "o" gnus-summary-pop-article)
1840
1841 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1842   "k" gnus-summary-kill-thread
1843   "l" gnus-summary-lower-thread
1844   "i" gnus-summary-raise-thread
1845   "T" gnus-summary-toggle-threads
1846   "t" gnus-summary-rethread-current
1847   "^" gnus-summary-reparent-thread
1848   "s" gnus-summary-show-thread
1849   "S" gnus-summary-show-all-threads
1850   "h" gnus-summary-hide-thread
1851   "H" gnus-summary-hide-all-threads
1852   "n" gnus-summary-next-thread
1853   "p" gnus-summary-prev-thread
1854   "u" gnus-summary-up-thread
1855   "o" gnus-summary-top-thread
1856   "d" gnus-summary-down-thread
1857   "#" gnus-uu-mark-thread
1858   "\M-#" gnus-uu-unmark-thread)
1859
1860 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1861   "g" gnus-summary-prepare
1862   "c" gnus-summary-insert-cached-articles
1863   "d" gnus-summary-insert-dormant-articles)
1864
1865 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1866   "c" gnus-summary-catchup-and-exit
1867   "C" gnus-summary-catchup-all-and-exit
1868   "E" gnus-summary-exit-no-update
1869   "Q" gnus-summary-exit
1870   "Z" gnus-summary-exit
1871   "n" gnus-summary-catchup-and-goto-next-group
1872   "R" gnus-summary-reselect-current-group
1873   "G" gnus-summary-rescan-group
1874   "N" gnus-summary-next-group
1875   "s" gnus-summary-save-newsrc
1876   "P" gnus-summary-prev-group)
1877
1878 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1879   " " gnus-summary-next-page
1880   "n" gnus-summary-next-page
1881   "\177" gnus-summary-prev-page
1882   [delete] gnus-summary-prev-page
1883   "p" gnus-summary-prev-page
1884   "\r" gnus-summary-scroll-up
1885   "\M-\r" gnus-summary-scroll-down
1886   "<" gnus-summary-beginning-of-article
1887   ">" gnus-summary-end-of-article
1888   "b" gnus-summary-beginning-of-article
1889   "e" gnus-summary-end-of-article
1890   "^" gnus-summary-refer-parent-article
1891   "r" gnus-summary-refer-parent-article
1892   "D" gnus-summary-enter-digest-group
1893   "R" gnus-summary-refer-references
1894   "T" gnus-summary-refer-thread
1895   "g" gnus-summary-show-article
1896   "s" gnus-summary-isearch-article
1897   "P" gnus-summary-print-article
1898   "M" gnus-mailing-list-insinuate
1899   "t" gnus-article-babel)
1900
1901 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1902   "b" gnus-article-add-buttons
1903   "B" gnus-article-add-buttons-to-head
1904   "o" gnus-article-treat-overstrike
1905   "e" gnus-article-emphasize
1906   "w" gnus-article-fill-cited-article
1907   "Q" gnus-article-fill-long-lines
1908   "C" gnus-article-capitalize-sentences
1909   "c" gnus-article-remove-cr
1910   "q" gnus-article-de-quoted-unreadable
1911   "6" gnus-article-de-base64-unreadable
1912   "Z" gnus-article-decode-HZ
1913   "A" gnus-article-treat-ansi-sequences
1914   "h" gnus-article-wash-html
1915   "u" gnus-article-unsplit-urls
1916   "s" gnus-summary-force-verify-and-decrypt
1917   "f" gnus-article-display-x-face
1918   "l" gnus-summary-stop-page-breaking
1919   "r" gnus-summary-caesar-message
1920   "m" gnus-summary-morse-message
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-treat-smiley
1923   "v" gnus-summary-verbose-headers
1924   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1925   "p" gnus-article-verify-x-pgp-sig
1926   "d" gnus-article-treat-dumbquotes)
1927
1928 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1929   ;; mnemonic: deuglif*Y*
1930   "u" gnus-article-outlook-unwrap-lines
1931   "a" gnus-article-outlook-repair-attribution
1932   "c" gnus-article-outlook-rearrange-citation
1933   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1934
1935 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1936   "a" gnus-article-hide
1937   "h" gnus-article-hide-headers
1938   "b" gnus-article-hide-boring-headers
1939   "s" gnus-article-hide-signature
1940   "c" gnus-article-hide-citation
1941   "C" gnus-article-hide-citation-in-followups
1942   "l" gnus-article-hide-list-identifiers
1943   "B" gnus-article-strip-banner
1944   "P" gnus-article-hide-pem
1945   "\C-c" gnus-article-hide-citation-maybe)
1946
1947 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1948   "a" gnus-article-highlight
1949   "h" gnus-article-highlight-headers
1950   "c" gnus-article-highlight-citation
1951   "s" gnus-article-highlight-signature)
1952
1953 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1954   "f" gnus-article-treat-fold-headers
1955   "u" gnus-article-treat-unfold-headers
1956   "n" gnus-article-treat-fold-newsgroups)
1957
1958 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1959   "x" gnus-article-display-x-face
1960   "d" gnus-article-display-face
1961   "s" gnus-treat-smiley
1962   "D" gnus-article-remove-images
1963   "f" gnus-treat-from-picon
1964   "m" gnus-treat-mail-picon
1965   "n" gnus-treat-newsgroups-picon)
1966
1967 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1968   "w" gnus-article-decode-mime-words
1969   "c" gnus-article-decode-charset
1970   "v" gnus-mime-view-all-parts
1971   "b" gnus-article-view-part)
1972
1973 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1974   "z" gnus-article-date-ut
1975   "u" gnus-article-date-ut
1976   "l" gnus-article-date-local
1977   "p" gnus-article-date-english
1978   "e" gnus-article-date-lapsed
1979   "o" gnus-article-date-original
1980   "i" gnus-article-date-iso8601
1981   "s" gnus-article-date-user)
1982
1983 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1984   "t" gnus-article-remove-trailing-blank-lines
1985   "l" gnus-article-strip-leading-blank-lines
1986   "m" gnus-article-strip-multiple-blank-lines
1987   "a" gnus-article-strip-blank-lines
1988   "A" gnus-article-strip-all-blank-lines
1989   "s" gnus-article-strip-leading-space
1990   "e" gnus-article-strip-trailing-space
1991   "w" gnus-article-remove-leading-whitespace)
1992
1993 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1994   "v" gnus-version
1995   "f" gnus-summary-fetch-faq
1996   "d" gnus-summary-describe-group
1997   "h" gnus-summary-describe-briefly
1998   "i" gnus-info-find-node
1999   "c" gnus-group-fetch-charter
2000   "C" gnus-group-fetch-control)
2001
2002 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2003   "e" gnus-summary-expire-articles
2004   "\M-\C-e" gnus-summary-expire-articles-now
2005   "\177" gnus-summary-delete-article
2006   [delete] gnus-summary-delete-article
2007   [backspace] gnus-summary-delete-article
2008   "m" gnus-summary-move-article
2009   "r" gnus-summary-respool-article
2010   "w" gnus-summary-edit-article
2011   "c" gnus-summary-copy-article
2012   "B" gnus-summary-crosspost-article
2013   "q" gnus-summary-respool-query
2014   "t" gnus-summary-respool-trace
2015   "i" gnus-summary-import-article
2016   "I" gnus-summary-create-article
2017   "p" gnus-summary-article-posted-p)
2018
2019 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2020   "o" gnus-summary-save-article
2021   "m" gnus-summary-save-article-mail
2022   "F" gnus-summary-write-article-file
2023   "r" gnus-summary-save-article-rmail
2024   "f" gnus-summary-save-article-file
2025   "b" gnus-summary-save-article-body-file
2026   "h" gnus-summary-save-article-folder
2027   "v" gnus-summary-save-article-vm
2028   "p" gnus-summary-pipe-output
2029   "P" gnus-summary-muttprint
2030   "s" gnus-soup-add-article)
2031
2032 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2033   "b" gnus-summary-display-buttonized
2034   "m" gnus-summary-repair-multipart
2035   "v" gnus-article-view-part
2036   "o" gnus-article-save-part
2037   "c" gnus-article-copy-part
2038   "C" gnus-article-view-part-as-charset
2039   "e" gnus-article-view-part-externally
2040   "E" gnus-article-encrypt-body
2041   "i" gnus-article-inline-part
2042   "|" gnus-article-pipe-part)
2043
2044 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2045   "p" gnus-summary-mark-as-processable
2046   "u" gnus-summary-unmark-as-processable
2047   "U" gnus-summary-unmark-all-processable
2048   "v" gnus-uu-mark-over
2049   "s" gnus-uu-mark-series
2050   "r" gnus-uu-mark-region
2051   "g" gnus-uu-unmark-region
2052   "R" gnus-uu-mark-by-regexp
2053   "G" gnus-uu-unmark-by-regexp
2054   "t" gnus-uu-mark-thread
2055   "T" gnus-uu-unmark-thread
2056   "a" gnus-uu-mark-all
2057   "b" gnus-uu-mark-buffer
2058   "S" gnus-uu-mark-sparse
2059   "k" gnus-summary-kill-process-mark
2060   "y" gnus-summary-yank-process-mark
2061   "w" gnus-summary-save-process-mark
2062   "i" gnus-uu-invert-processable)
2063
2064 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2065   ;;"x" gnus-uu-extract-any
2066   "m" gnus-summary-save-parts
2067   "u" gnus-uu-decode-uu
2068   "U" gnus-uu-decode-uu-and-save
2069   "s" gnus-uu-decode-unshar
2070   "S" gnus-uu-decode-unshar-and-save
2071   "o" gnus-uu-decode-save
2072   "O" gnus-uu-decode-save
2073   "b" gnus-uu-decode-binhex
2074   "B" gnus-uu-decode-binhex
2075   "p" gnus-uu-decode-postscript
2076   "P" gnus-uu-decode-postscript-and-save)
2077
2078 (gnus-define-keys
2079     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2080   "u" gnus-uu-decode-uu-view
2081   "U" gnus-uu-decode-uu-and-save-view
2082   "s" gnus-uu-decode-unshar-view
2083   "S" gnus-uu-decode-unshar-and-save-view
2084   "o" gnus-uu-decode-save-view
2085   "O" gnus-uu-decode-save-view
2086   "b" gnus-uu-decode-binhex-view
2087   "B" gnus-uu-decode-binhex-view
2088   "p" gnus-uu-decode-postscript-view
2089   "P" gnus-uu-decode-postscript-and-save-view)
2090
2091 (defvar gnus-article-post-menu nil)
2092
2093 (defconst gnus-summary-menu-maxlen 20)
2094
2095 (defun gnus-summary-menu-split (menu)
2096   ;; If we have lots of elements, divide them into groups of 20
2097   ;; and make a pane (or submenu) for each one.
2098   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2099       (let ((menu menu) sublists next
2100             (i 1))
2101         (while menu
2102           ;; Pull off the next gnus-summary-menu-maxlen elements
2103           ;; and make them the next element of sublist.
2104           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2105           (if next
2106               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2107                       nil))
2108           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2109                                              (aref (car (last menu)) 0)) menu)
2110                                sublists))
2111           (setq i (1+ i))
2112           (setq menu next))
2113         (nreverse sublists))
2114     ;; Few elements--put them all in one pane.
2115     menu))
2116
2117 (defun gnus-summary-make-menu-bar ()
2118   (gnus-turn-off-edit-menu 'summary)
2119
2120   (unless (boundp 'gnus-summary-misc-menu)
2121
2122     (easy-menu-define
2123       gnus-summary-kill-menu gnus-summary-mode-map ""
2124       (cons
2125        "Score"
2126        (nconc
2127         (list
2128          ["Customize" gnus-score-customize t])
2129         (gnus-make-score-map 'increase)
2130         (gnus-make-score-map 'lower)
2131         '(("Mark"
2132            ["Kill below" gnus-summary-kill-below t]
2133            ["Mark above" gnus-summary-mark-above t]
2134            ["Tick above" gnus-summary-tick-above t]
2135            ["Clear above" gnus-summary-clear-above t])
2136           ["Current score" gnus-summary-current-score t]
2137           ["Set score" gnus-summary-set-score t]
2138           ["Switch current score file..." gnus-score-change-score-file t]
2139           ["Set mark below..." gnus-score-set-mark-below t]
2140           ["Set expunge below..." gnus-score-set-expunge-below t]
2141           ["Edit current score file" gnus-score-edit-current-scores t]
2142           ["Edit score file" gnus-score-edit-file t]
2143           ["Trace score" gnus-score-find-trace t]
2144           ["Find words" gnus-score-find-favourite-words t]
2145           ["Rescore buffer" gnus-summary-rescore t]
2146           ["Increase score..." gnus-summary-increase-score t]
2147           ["Lower score..." gnus-summary-lower-score t]))))
2148
2149     ;; Define both the Article menu in the summary buffer and the
2150     ;; equivalent Commands menu in the article buffer here for
2151     ;; consistency.
2152     (let ((innards
2153            `(("Hide"
2154               ["All" gnus-article-hide t]
2155               ["Headers" gnus-article-hide-headers t]
2156               ["Signature" gnus-article-hide-signature t]
2157               ["Citation" gnus-article-hide-citation t]
2158               ["List identifiers" gnus-article-hide-list-identifiers t]
2159               ["Banner" gnus-article-strip-banner t]
2160               ["Boring headers" gnus-article-hide-boring-headers t])
2161              ("Highlight"
2162               ["All" gnus-article-highlight t]
2163               ["Headers" gnus-article-highlight-headers t]
2164               ["Signature" gnus-article-highlight-signature t]
2165               ["Citation" gnus-article-highlight-citation t])
2166              ("MIME"
2167               ["Words" gnus-article-decode-mime-words t]
2168               ["Charset" gnus-article-decode-charset t]
2169               ["QP" gnus-article-de-quoted-unreadable t]
2170               ["Base64" gnus-article-de-base64-unreadable t]
2171               ["View MIME buttons" gnus-summary-display-buttonized t]
2172               ["View all" gnus-mime-view-all-parts t]
2173               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2174               ["Encrypt body" gnus-article-encrypt-body
2175                :active (not (gnus-group-read-only-p))
2176                ,@(if (featurep 'xemacs) nil
2177                    '(:help "Encrypt the message body on disk"))]
2178               ["Extract all parts..." gnus-summary-save-parts t]
2179               ("Multipart"
2180                ["Repair multipart" gnus-summary-repair-multipart t]
2181                ["Pipe part..." gnus-article-pipe-part t]
2182                ["Inline part" gnus-article-inline-part t]
2183                ["Encrypt body" gnus-article-encrypt-body
2184                 :active (not (gnus-group-read-only-p))
2185                ,@(if (featurep 'xemacs) nil
2186                    '(:help "Encrypt the message body on disk"))]
2187                ["View part externally" gnus-article-view-part-externally t]
2188                ["View part with charset..." gnus-article-view-part-as-charset t]
2189                ["Copy part" gnus-article-copy-part t]
2190                ["Save part..." gnus-article-save-part t]
2191                ["View part" gnus-article-view-part t]))
2192              ("Date"
2193               ["Local" gnus-article-date-local t]
2194               ["ISO8601" gnus-article-date-iso8601 t]
2195               ["UT" gnus-article-date-ut t]
2196               ["Original" gnus-article-date-original t]
2197               ["Lapsed" gnus-article-date-lapsed t]
2198               ["User-defined" gnus-article-date-user t])
2199              ("Display"
2200               ["Remove images" gnus-article-remove-images t]
2201               ["Toggle smiley" gnus-treat-smiley t]
2202               ["Show X-Face" gnus-article-display-x-face t]
2203               ["Show picons in From" gnus-treat-from-picon t]
2204               ["Show picons in mail headers" gnus-treat-mail-picon t]
2205               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2206               ("View as different encoding"
2207                ,@(gnus-summary-menu-split
2208                   (mapcar
2209                    (lambda (cs)
2210                      ;; Since easymenu under Emacs doesn't allow
2211                      ;; lambda forms for menu commands, we should
2212                      ;; provide intern'ed function symbols.
2213                      (let ((command (intern (format "\
2214 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2215                        (fset command
2216                              `(lambda ()
2217                                 (interactive)
2218                                 (let ((gnus-summary-show-article-charset-alist
2219                                        '((1 . ,cs))))
2220                                   (gnus-summary-show-article 1))))
2221                        `[,(symbol-name cs) ,command t]))
2222                    (sort (if (fboundp 'coding-system-list)
2223                              (coding-system-list)
2224                            (mapcar 'car mm-mime-mule-charset-alist))
2225                          'string<)))))
2226              ("Washing"
2227               ("Remove Blanks"
2228                ["Leading" gnus-article-strip-leading-blank-lines t]
2229                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2230                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2231                ["All of the above" gnus-article-strip-blank-lines t]
2232                ["All" gnus-article-strip-all-blank-lines t]
2233                ["Leading space" gnus-article-strip-leading-space t]
2234                ["Trailing space" gnus-article-strip-trailing-space t]
2235                ["Leading space in headers"
2236                 gnus-article-remove-leading-whitespace t])
2237               ["Overstrike" gnus-article-treat-overstrike t]
2238               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2239               ["Emphasis" gnus-article-emphasize t]
2240               ["Word wrap" gnus-article-fill-cited-article t]
2241               ["Fill long lines" gnus-article-fill-long-lines t]
2242               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2243               ["Remove CR" gnus-article-remove-cr t]
2244               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2245               ["Base64" gnus-article-de-base64-unreadable t]
2246               ["Rot 13" gnus-summary-caesar-message
2247                ,@(if (featurep 'xemacs) '(t)
2248                    '(:help "\"Caesar rotate\" article by 13"))]
2249               ["Morse decode" gnus-summary-morse-message t]
2250               ["Unix pipe..." gnus-summary-pipe-message t]
2251               ["Add buttons" gnus-article-add-buttons t]
2252               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2253               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2254               ["Verbose header" gnus-summary-verbose-headers t]
2255               ["Toggle header" gnus-summary-toggle-header t]
2256               ["Unfold headers" gnus-article-treat-unfold-headers t]
2257               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2258               ["Html" gnus-article-wash-html t]
2259               ["Unsplit URLs" gnus-article-unsplit-urls t]
2260               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2261               ["Decode HZ" gnus-article-decode-HZ t]
2262               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2263               ("(Outlook) Deuglify"
2264                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2265                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2266                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2267                ["Full (Outlook) deuglify"
2268                 gnus-article-outlook-deuglify-article t])
2269               )
2270              ("Output"
2271               ["Save in default format..." gnus-summary-save-article
2272                ,@(if (featurep 'xemacs) '(t)
2273                    '(:help "Save article using default method"))]
2274               ["Save in file..." gnus-summary-save-article-file
2275                ,@(if (featurep 'xemacs) '(t)
2276                    '(:help "Save article in file"))]
2277               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2278               ["Save in MH folder..." gnus-summary-save-article-folder t]
2279               ["Save in VM folder..." gnus-summary-save-article-vm t]
2280               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2281               ["Save body in file..." gnus-summary-save-article-body-file t]
2282               ["Pipe through a filter..." gnus-summary-pipe-output t]
2283               ["Add to SOUP packet" gnus-soup-add-article t]
2284               ["Print with Muttprint..." gnus-summary-muttprint t]
2285               ["Print" gnus-summary-print-article t])
2286              ("Backend"
2287               ["Respool article..." gnus-summary-respool-article t]
2288               ["Move article..." gnus-summary-move-article
2289                (gnus-check-backend-function
2290                 'request-move-article gnus-newsgroup-name)]
2291               ["Copy article..." gnus-summary-copy-article t]
2292               ["Crosspost article..." gnus-summary-crosspost-article
2293                (gnus-check-backend-function
2294                 'request-replace-article gnus-newsgroup-name)]
2295               ["Import file..." gnus-summary-import-article
2296                (gnus-check-backend-function
2297                 'request-accept-article gnus-newsgroup-name)]
2298               ["Create article..." gnus-summary-create-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Check if posted" gnus-summary-article-posted-p t]
2302               ["Edit article" gnus-summary-edit-article
2303                (not (gnus-group-read-only-p))]
2304               ["Delete article" gnus-summary-delete-article
2305                (gnus-check-backend-function
2306                 'request-expire-articles gnus-newsgroup-name)]
2307               ["Query respool" gnus-summary-respool-query t]
2308               ["Trace respool" gnus-summary-respool-trace t]
2309               ["Delete expirable articles" gnus-summary-expire-articles-now
2310                (gnus-check-backend-function
2311                 'request-expire-articles gnus-newsgroup-name)])
2312              ("Extract"
2313               ["Uudecode" gnus-uu-decode-uu
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Decode uuencoded article(s)"))]
2316               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2317               ["Unshar" gnus-uu-decode-unshar t]
2318               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2319               ["Save" gnus-uu-decode-save t]
2320               ["Binhex" gnus-uu-decode-binhex t]
2321               ["Postscript" gnus-uu-decode-postscript t]
2322               ["All MIME parts" gnus-summary-save-parts t])
2323              ("Cache"
2324               ["Enter article" gnus-cache-enter-article t]
2325               ["Remove article" gnus-cache-remove-article t])
2326              ["Translate" gnus-article-babel t]
2327              ["Select article buffer" gnus-summary-select-article-buffer t]
2328              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2329              ["Isearch article..." gnus-summary-isearch-article t]
2330              ["Beginning of the article" gnus-summary-beginning-of-article t]
2331              ["End of the article" gnus-summary-end-of-article t]
2332              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2333              ["Fetch referenced articles" gnus-summary-refer-references t]
2334              ["Fetch current thread" gnus-summary-refer-thread t]
2335              ["Fetch article with id..." gnus-summary-refer-article t]
2336              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2337              ["Redisplay" gnus-summary-show-article t]
2338              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2339       (easy-menu-define
2340         gnus-summary-article-menu gnus-summary-mode-map ""
2341         (cons "Article" innards))
2342
2343       (if (not (keymapp gnus-summary-article-menu))
2344           (easy-menu-define
2345             gnus-article-commands-menu gnus-article-mode-map ""
2346             (cons "Commands" innards))
2347         ;; in Emacs, don't share menu.
2348         (setq gnus-article-commands-menu
2349               (copy-keymap gnus-summary-article-menu))
2350         (define-key gnus-article-mode-map [menu-bar commands]
2351           (cons "Commands" gnus-article-commands-menu))))
2352
2353     (easy-menu-define
2354       gnus-summary-thread-menu gnus-summary-mode-map ""
2355       '("Threads"
2356         ["Find all messages in thread" gnus-summary-refer-thread t]
2357         ["Toggle threading" gnus-summary-toggle-threads t]
2358         ["Hide threads" gnus-summary-hide-all-threads t]
2359         ["Show threads" gnus-summary-show-all-threads t]
2360         ["Hide thread" gnus-summary-hide-thread t]
2361         ["Show thread" gnus-summary-show-thread t]
2362         ["Go to next thread" gnus-summary-next-thread t]
2363         ["Go to previous thread" gnus-summary-prev-thread t]
2364         ["Go down thread" gnus-summary-down-thread t]
2365         ["Go up thread" gnus-summary-up-thread t]
2366         ["Top of thread" gnus-summary-top-thread t]
2367         ["Mark thread as read" gnus-summary-kill-thread t]
2368         ["Lower thread score" gnus-summary-lower-thread t]
2369         ["Raise thread score" gnus-summary-raise-thread t]
2370         ["Rethread current" gnus-summary-rethread-current t]))
2371
2372     (easy-menu-define
2373       gnus-summary-post-menu gnus-summary-mode-map ""
2374       `("Post"
2375         ["Send a message (mail or news)" gnus-summary-post-news
2376          ,@(if (featurep 'xemacs) '(t)
2377              '(:help "Post an article"))]
2378         ["Followup" gnus-summary-followup
2379          ,@(if (featurep 'xemacs) '(t)
2380              '(:help "Post followup to this article"))]
2381         ["Followup and yank" gnus-summary-followup-with-original
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Post followup to this article, quoting its contents"))]
2384         ["Supersede article" gnus-summary-supersede-article t]
2385         ["Cancel article" gnus-summary-cancel-article
2386          ,@(if (featurep 'xemacs) '(t)
2387              '(:help "Cancel an article you posted"))]
2388         ["Reply" gnus-summary-reply t]
2389         ["Reply and yank" gnus-summary-reply-with-original t]
2390         ["Wide reply" gnus-summary-wide-reply t]
2391         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Mail a reply, quoting this article"))]
2394         ["Very wide reply" gnus-summary-very-wide-reply t]
2395         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Mail a very wide reply, quoting this article"))]
2398         ["Mail forward" gnus-summary-mail-forward t]
2399         ["Post forward" gnus-summary-post-forward t]
2400         ["Digest and mail" gnus-uu-digest-mail-forward t]
2401         ["Digest and post" gnus-uu-digest-post-forward t]
2402         ["Resend message" gnus-summary-resend-message t]
2403         ["Resend message edit" gnus-summary-resend-message-edit t]
2404         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2405         ["Send a mail" gnus-summary-mail-other-window t]
2406         ["Create a local message" gnus-summary-news-other-window t]
2407         ["Uuencode and post" gnus-uu-post-news
2408          ,@(if (featurep 'xemacs) '(t)
2409              '(:help "Post a uuencoded article"))]
2410         ["Followup via news" gnus-summary-followup-to-mail t]
2411         ["Followup via news and yank"
2412          gnus-summary-followup-to-mail-with-original t]
2413         ;;("Draft"
2414         ;;["Send" gnus-summary-send-draft t]
2415         ;;["Send bounced" gnus-resend-bounced-mail t])
2416         ))
2417
2418     (cond
2419      ((not (keymapp gnus-summary-post-menu))
2420       (setq gnus-article-post-menu gnus-summary-post-menu))
2421      ((not gnus-article-post-menu)
2422       ;; Don't share post menu.
2423       (setq gnus-article-post-menu
2424             (copy-keymap gnus-summary-post-menu))))
2425     (define-key gnus-article-mode-map [menu-bar post]
2426       (cons "Post" gnus-article-post-menu))
2427
2428     (easy-menu-define
2429       gnus-summary-misc-menu gnus-summary-mode-map ""
2430       `("Gnus"
2431         ("Mark Read"
2432          ["Mark as read" gnus-summary-mark-as-read-forward t]
2433          ["Mark same subject and select"
2434           gnus-summary-kill-same-subject-and-select t]
2435          ["Mark same subject" gnus-summary-kill-same-subject t]
2436          ["Catchup" gnus-summary-catchup
2437           ,@(if (featurep 'xemacs) '(t)
2438               '(:help "Mark unread articles in this group as read"))]
2439          ["Catchup all" gnus-summary-catchup-all t]
2440          ["Catchup to here" gnus-summary-catchup-to-here t]
2441          ["Catchup from here" gnus-summary-catchup-from-here t]
2442          ["Catchup region" gnus-summary-mark-region-as-read
2443           (gnus-mark-active-p)]
2444          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2445         ("Mark Various"
2446          ["Tick" gnus-summary-tick-article-forward t]
2447          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2448          ["Remove marks" gnus-summary-clear-mark-forward t]
2449          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2450          ["Set bookmark" gnus-summary-set-bookmark t]
2451          ["Remove bookmark" gnus-summary-remove-bookmark t])
2452         ("Limit to"
2453          ["Marks..." gnus-summary-limit-to-marks t]
2454          ["Subject..." gnus-summary-limit-to-subject t]
2455          ["Author..." gnus-summary-limit-to-author t]
2456          ["Age..." gnus-summary-limit-to-age t]
2457          ["Extra..." gnus-summary-limit-to-extra t]
2458          ["Score..." gnus-summary-limit-to-score t]
2459          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2460          ["Unread" gnus-summary-limit-to-unread t]
2461          ["Unseen" gnus-summary-limit-to-unseen t]
2462          ["Replied" gnus-summary-limit-to-replied t]
2463          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2464          ["Next articles" gnus-summary-limit-to-articles t]
2465          ["Pop limit" gnus-summary-pop-limit t]
2466          ["Show dormant" gnus-summary-limit-include-dormant t]
2467          ["Hide childless dormant"
2468           gnus-summary-limit-exclude-childless-dormant t]
2469          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2470          ["Hide marked" gnus-summary-limit-exclude-marks t]
2471          ["Show expunged" gnus-summary-limit-include-expunged t])
2472         ("Process Mark"
2473          ["Set mark" gnus-summary-mark-as-processable t]
2474          ["Remove mark" gnus-summary-unmark-as-processable t]
2475          ["Remove all marks" gnus-summary-unmark-all-processable t]
2476          ["Mark above" gnus-uu-mark-over t]
2477          ["Mark series" gnus-uu-mark-series t]
2478          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2479          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2480          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2481          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2482          ["Mark all" gnus-uu-mark-all t]
2483          ["Mark buffer" gnus-uu-mark-buffer t]
2484          ["Mark sparse" gnus-uu-mark-sparse t]
2485          ["Mark thread" gnus-uu-mark-thread t]
2486          ["Unmark thread" gnus-uu-unmark-thread t]
2487          ("Process Mark Sets"
2488           ["Kill" gnus-summary-kill-process-mark t]
2489           ["Yank" gnus-summary-yank-process-mark
2490            gnus-newsgroup-process-stack]
2491           ["Save" gnus-summary-save-process-mark t]
2492           ["Run command on marked..." gnus-summary-universal-argument t]))
2493         ("Scroll article"
2494          ["Page forward" gnus-summary-next-page
2495           ,@(if (featurep 'xemacs) '(t)
2496               '(:help "Show next page of article"))]
2497          ["Page backward" gnus-summary-prev-page
2498           ,@(if (featurep 'xemacs) '(t)
2499               '(:help "Show previous page of article"))]
2500          ["Line forward" gnus-summary-scroll-up t])
2501         ("Move"
2502          ["Next unread article" gnus-summary-next-unread-article t]
2503          ["Previous unread article" gnus-summary-prev-unread-article t]
2504          ["Next article" gnus-summary-next-article t]
2505          ["Previous article" gnus-summary-prev-article t]
2506          ["Next unread subject" gnus-summary-next-unread-subject t]
2507          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2508          ["Next article same subject" gnus-summary-next-same-subject t]
2509          ["Previous article same subject" gnus-summary-prev-same-subject t]
2510          ["First unread article" gnus-summary-first-unread-article t]
2511          ["Best unread article" gnus-summary-best-unread-article t]
2512          ["Go to subject number..." gnus-summary-goto-subject t]
2513          ["Go to article number..." gnus-summary-goto-article t]
2514          ["Go to the last article" gnus-summary-goto-last-article t]
2515          ["Pop article off history" gnus-summary-pop-article t])
2516         ("Sort"
2517          ["Sort by number" gnus-summary-sort-by-number t]
2518          ["Sort by author" gnus-summary-sort-by-author t]
2519          ["Sort by subject" gnus-summary-sort-by-subject t]
2520          ["Sort by date" gnus-summary-sort-by-date t]
2521          ["Sort by score" gnus-summary-sort-by-score t]
2522          ["Sort by lines" gnus-summary-sort-by-lines t]
2523          ["Sort by characters" gnus-summary-sort-by-chars t]
2524          ["Randomize" gnus-summary-sort-by-random t]
2525          ["Original sort" gnus-summary-sort-by-original t])
2526         ("Help"
2527          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2528          ["Describe group" gnus-summary-describe-group t]
2529          ["Fetch charter" gnus-group-fetch-charter
2530           ,@(if (featurep 'xemacs) nil
2531               '(:help "Display the charter of the current group"))]
2532          ["Fetch control message" gnus-group-fetch-control
2533           ,@(if (featurep 'xemacs) nil
2534               '(:help "Display the archived control message for the current group"))]
2535          ["Read manual" gnus-info-find-node t])
2536         ("Modes"
2537          ["Pick and read" gnus-pick-mode t]
2538          ["Binary" gnus-binary-mode t])
2539         ("Regeneration"
2540          ["Regenerate" gnus-summary-prepare t]
2541          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2542          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2543          ["Toggle threading" gnus-summary-toggle-threads t])
2544         ["See old articles" gnus-summary-insert-old-articles t]
2545         ["See new articles" gnus-summary-insert-new-articles t]
2546         ["Filter articles..." gnus-summary-execute-command t]
2547         ["Run command on articles..." gnus-summary-universal-argument t]
2548         ["Search articles forward..." gnus-summary-search-article-forward t]
2549         ["Search articles backward..." gnus-summary-search-article-backward t]
2550         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2551         ["Expand window" gnus-summary-expand-window t]
2552         ["Expire expirable articles" gnus-summary-expire-articles
2553          (gnus-check-backend-function
2554           'request-expire-articles gnus-newsgroup-name)]
2555         ["Edit local kill file" gnus-summary-edit-local-kill t]
2556         ["Edit main kill file" gnus-summary-edit-global-kill t]
2557         ["Edit group parameters" gnus-summary-edit-parameters t]
2558         ["Customize group parameters" gnus-summary-customize-parameters t]
2559         ["Send a bug report" gnus-bug t]
2560         ("Exit"
2561          ["Catchup and exit" gnus-summary-catchup-and-exit
2562           ,@(if (featurep 'xemacs) '(t)
2563               '(:help "Mark unread articles in this group as read, then exit"))]
2564          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2565          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2566          ["Exit group" gnus-summary-exit
2567           ,@(if (featurep 'xemacs) '(t)
2568               '(:help "Exit current group, return to group selection mode"))]
2569          ["Exit group without updating" gnus-summary-exit-no-update t]
2570          ["Exit and goto next group" gnus-summary-next-group t]
2571          ["Exit and goto prev group" gnus-summary-prev-group t]
2572          ["Reselect group" gnus-summary-reselect-current-group t]
2573          ["Rescan group" gnus-summary-rescan-group t]
2574          ["Update dribble" gnus-summary-save-newsrc t])))
2575
2576     (gnus-run-hooks 'gnus-summary-menu-hook)))
2577
2578 (defvar gnus-summary-tool-bar-map nil)
2579
2580 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2581 (defun gnus-summary-make-tool-bar ()
2582   (if (and (fboundp 'tool-bar-add-item-from-menu)
2583            (default-value 'tool-bar-mode)
2584            (not gnus-summary-tool-bar-map))
2585       (setq gnus-summary-tool-bar-map
2586             (let ((tool-bar-map (make-sparse-keymap))
2587                   (load-path (mm-image-load-path)))
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-post-news "post" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-followup "followup" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-reply "reply" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2620               tool-bar-map)))
2621   (if gnus-summary-tool-bar-map
2622       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2623
2624 (defun gnus-score-set-default (var value)
2625   "A version of set that updates the GNU Emacs menu-bar."
2626   (set var value)
2627   ;; It is the message that forces the active status to be updated.
2628   (message ""))
2629
2630 (defun gnus-make-score-map (type)
2631   "Make a summary score map of type TYPE."
2632   (if t
2633       nil
2634     (let ((headers '(("author" "from" string)
2635                      ("subject" "subject" string)
2636                      ("article body" "body" string)
2637                      ("article head" "head" string)
2638                      ("xref" "xref" string)
2639                      ("extra header" "extra" string)
2640                      ("lines" "lines" number)
2641                      ("followups to author" "followup" string)))
2642           (types '((number ("less than" <)
2643                            ("greater than" >)
2644                            ("equal" =))
2645                    (string ("substring" s)
2646                            ("exact string" e)
2647                            ("fuzzy string" f)
2648                            ("regexp" r))))
2649           (perms '(("temporary" (current-time-string))
2650                    ("permanent" nil)
2651                    ("immediate" now)))
2652           header)
2653       (list
2654        (apply
2655         'nconc
2656         (list
2657          (if (eq type 'lower)
2658              "Lower score"
2659            "Increase score"))
2660         (let (outh)
2661           (while headers
2662             (setq header (car headers))
2663             (setq outh
2664                   (cons
2665                    (apply
2666                     'nconc
2667                     (list (car header))
2668                     (let ((ts (cdr (assoc (nth 2 header) types)))
2669                           outt)
2670                       (while ts
2671                         (setq outt
2672                               (cons
2673                                (apply
2674                                 'nconc
2675                                 (list (caar ts))
2676                                 (let ((ps perms)
2677                                       outp)
2678                                   (while ps
2679                                     (setq outp
2680                                           (cons
2681                                            (vector
2682                                             (caar ps)
2683                                             (list
2684                                              'gnus-summary-score-entry
2685                                              (nth 1 header)
2686                                              (if (or (string= (nth 1 header)
2687                                                               "head")
2688                                                      (string= (nth 1 header)
2689                                                               "body"))
2690                                                  ""
2691                                                (list 'gnus-summary-header
2692                                                      (nth 1 header)))
2693                                              (list 'quote (nth 1 (car ts)))
2694                                              (list 'gnus-score-delta-default
2695                                                    nil)
2696                                              (nth 1 (car ps))
2697                                              t)
2698                                             t)
2699                                            outp))
2700                                     (setq ps (cdr ps)))
2701                                   (list (nreverse outp))))
2702                                outt))
2703                         (setq ts (cdr ts)))
2704                       (list (nreverse outt))))
2705                    outh))
2706             (setq headers (cdr headers)))
2707           (list (nreverse outh))))))))
2708
2709 \f
2710
2711 (defun gnus-summary-mode (&optional group)
2712   "Major mode for reading articles.
2713
2714 All normal editing commands are switched off.
2715 \\<gnus-summary-mode-map>
2716 Each line in this buffer represents one article.  To read an
2717 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2718 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2719 respectively.
2720
2721 You can also post articles and send mail from this buffer.  To
2722 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2723 of an article, type `\\[gnus-summary-reply]'.
2724
2725 There are approx. one gazillion commands you can execute in this
2726 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2727
2728 The following commands are available:
2729
2730 \\{gnus-summary-mode-map}"
2731   (interactive)
2732   (kill-all-local-variables)
2733   (when (gnus-visual-p 'summary-menu 'menu)
2734     (gnus-summary-make-menu-bar)
2735     (gnus-summary-make-tool-bar))
2736   (gnus-summary-make-local-variables)
2737   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2738     (gnus-summary-make-local-variables))
2739   (gnus-make-thread-indent-array)
2740   (gnus-simplify-mode-line)
2741   (setq major-mode 'gnus-summary-mode)
2742   (setq mode-name "Summary")
2743   (make-local-variable 'minor-mode-alist)
2744   (use-local-map gnus-summary-mode-map)
2745   (buffer-disable-undo)
2746   (setq buffer-read-only t              ;Disable modification
2747         show-trailing-whitespace nil)
2748   (setq truncate-lines t)
2749   (setq selective-display t)
2750   (setq selective-display-ellipses t)   ;Display `...'
2751   (gnus-summary-set-display-table)
2752   (gnus-set-default-directory)
2753   (setq gnus-newsgroup-name group)
2754   (make-local-variable 'gnus-summary-line-format)
2755   (make-local-variable 'gnus-summary-line-format-spec)
2756   (make-local-variable 'gnus-summary-dummy-line-format)
2757   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2758   (make-local-variable 'gnus-summary-mark-positions)
2759   (gnus-make-local-hook 'pre-command-hook)
2760   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2761   (gnus-run-hooks 'gnus-summary-mode-hook)
2762   (turn-on-gnus-mailing-list-mode)
2763   (mm-enable-multibyte)
2764   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2765   (gnus-update-summary-mark-positions))
2766
2767 (defun gnus-summary-make-local-variables ()
2768   "Make all the local summary buffer variables."
2769   (let (global)
2770     (dolist (local gnus-summary-local-variables)
2771       (if (consp local)
2772           (progn
2773             (if (eq (cdr local) 'global)
2774                 ;; Copy the global value of the variable.
2775                 (setq global (symbol-value (car local)))
2776               ;; Use the value from the list.
2777               (setq global (eval (cdr local))))
2778             (set (make-local-variable (car local)) global))
2779         ;; Simple nil-valued local variable.
2780         (set (make-local-variable local) nil)))))
2781
2782 (defun gnus-summary-clear-local-variables ()
2783   (let ((locals gnus-summary-local-variables))
2784     (while locals
2785       (if (consp (car locals))
2786           (and (vectorp (caar locals))
2787                (set (caar locals) nil))
2788         (and (vectorp (car locals))
2789              (set (car locals) nil)))
2790       (setq locals (cdr locals)))))
2791
2792 ;; Summary data functions.
2793
2794 (defmacro gnus-data-number (data)
2795   `(car ,data))
2796
2797 (defmacro gnus-data-set-number (data number)
2798   `(setcar ,data ,number))
2799
2800 (defmacro gnus-data-mark (data)
2801   `(nth 1 ,data))
2802
2803 (defmacro gnus-data-set-mark (data mark)
2804   `(setcar (nthcdr 1 ,data) ,mark))
2805
2806 (defmacro gnus-data-pos (data)
2807   `(nth 2 ,data))
2808
2809 (defmacro gnus-data-set-pos (data pos)
2810   `(setcar (nthcdr 2 ,data) ,pos))
2811
2812 (defmacro gnus-data-header (data)
2813   `(nth 3 ,data))
2814
2815 (defmacro gnus-data-set-header (data header)
2816   `(setf (nth 3 ,data) ,header))
2817
2818 (defmacro gnus-data-level (data)
2819   `(nth 4 ,data))
2820
2821 (defmacro gnus-data-unread-p (data)
2822   `(= (nth 1 ,data) gnus-unread-mark))
2823
2824 (defmacro gnus-data-read-p (data)
2825   `(/= (nth 1 ,data) gnus-unread-mark))
2826
2827 (defmacro gnus-data-pseudo-p (data)
2828   `(consp (nth 3 ,data)))
2829
2830 (defmacro gnus-data-find (number)
2831   `(assq ,number gnus-newsgroup-data))
2832
2833 (defmacro gnus-data-find-list (number &optional data)
2834   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2835      (memq (assq ,number bdata)
2836            bdata)))
2837
2838 (defmacro gnus-data-make (number mark pos header level)
2839   `(list ,number ,mark ,pos ,header ,level))
2840
2841 (defun gnus-data-enter (after-article number mark pos header level offset)
2842   (let ((data (gnus-data-find-list after-article)))
2843     (unless data
2844       (error "No such article: %d" after-article))
2845     (setcdr data (cons (gnus-data-make number mark pos header level)
2846                        (cdr data)))
2847     (setq gnus-newsgroup-data-reverse nil)
2848     (gnus-data-update-list (cddr data) offset)))
2849
2850 (defun gnus-data-enter-list (after-article list &optional offset)
2851   (when list
2852     (let ((data (and after-article (gnus-data-find-list after-article)))
2853           (ilist list))
2854       (if (not (or data
2855                    after-article))
2856           (let ((odata gnus-newsgroup-data))
2857             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2858             (when offset
2859               (gnus-data-update-list odata offset)))
2860       ;; Find the last element in the list to be spliced into the main
2861         ;; list.
2862         (while (cdr list)
2863           (setq list (cdr list)))
2864         (if (not data)
2865             (progn
2866               (setcdr list gnus-newsgroup-data)
2867               (setq gnus-newsgroup-data ilist)
2868               (when offset
2869                 (gnus-data-update-list (cdr list) offset)))
2870           (setcdr list (cdr data))
2871           (setcdr data ilist)
2872           (when offset
2873             (gnus-data-update-list (cdr list) offset))))
2874       (setq gnus-newsgroup-data-reverse nil))))
2875
2876 (defun gnus-data-remove (article &optional offset)
2877   (let ((data gnus-newsgroup-data))
2878     (if (= (gnus-data-number (car data)) article)
2879         (progn
2880           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2881                 gnus-newsgroup-data-reverse nil)
2882           (when offset
2883             (gnus-data-update-list gnus-newsgroup-data offset)))
2884       (while (cdr data)
2885         (when (= (gnus-data-number (cadr data)) article)
2886           (setcdr data (cddr data))
2887           (when offset
2888             (gnus-data-update-list (cdr data) offset))
2889           (setq data nil
2890                 gnus-newsgroup-data-reverse nil))
2891         (setq data (cdr data))))))
2892
2893 (defmacro gnus-data-list (backward)
2894   `(if ,backward
2895        (or gnus-newsgroup-data-reverse
2896            (setq gnus-newsgroup-data-reverse
2897                  (reverse gnus-newsgroup-data)))
2898      gnus-newsgroup-data))
2899
2900 (defun gnus-data-update-list (data offset)
2901   "Add OFFSET to the POS of all data entries in DATA."
2902   (setq gnus-newsgroup-data-reverse nil)
2903   (while data
2904     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2905     (setq data (cdr data))))
2906
2907 (defun gnus-summary-article-pseudo-p (article)
2908   "Say whether this article is a pseudo article or not."
2909   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2910
2911 (defmacro gnus-summary-article-sparse-p (article)
2912   "Say whether this article is a sparse article or not."
2913   `(memq ,article gnus-newsgroup-sparse))
2914
2915 (defmacro gnus-summary-article-ancient-p (article)
2916   "Say whether this article is a sparse article or not."
2917   `(memq ,article gnus-newsgroup-ancient))
2918
2919 (defun gnus-article-parent-p (number)
2920   "Say whether this article is a parent or not."
2921   (let ((data (gnus-data-find-list number)))
2922     (and (cdr data)              ; There has to be an article after...
2923          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2924             (gnus-data-level (nth 1 data))))))
2925
2926 (defun gnus-article-children (number)
2927   "Return a list of all children to NUMBER."
2928   (let* ((data (gnus-data-find-list number))
2929          (level (gnus-data-level (car data)))
2930          children)
2931     (setq data (cdr data))
2932     (while (and data
2933                 (= (gnus-data-level (car data)) (1+ level)))
2934       (push (gnus-data-number (car data)) children)
2935       (setq data (cdr data)))
2936     children))
2937
2938 (defmacro gnus-summary-skip-intangible ()
2939   "If the current article is intangible, then jump to a different article."
2940   '(let ((to (get-text-property (point) 'gnus-intangible)))
2941      (and to (gnus-summary-goto-subject to))))
2942
2943 (defmacro gnus-summary-article-intangible-p ()
2944   "Say whether this article is intangible or not."
2945   '(get-text-property (point) 'gnus-intangible))
2946
2947 (defun gnus-article-read-p (article)
2948   "Say whether ARTICLE is read or not."
2949   (not (or (memq article gnus-newsgroup-marked)
2950            (memq article gnus-newsgroup-spam-marked)
2951            (memq article gnus-newsgroup-unreads)
2952            (memq article gnus-newsgroup-unselected)
2953            (memq article gnus-newsgroup-dormant))))
2954
2955 ;; Some summary mode macros.
2956
2957 (defmacro gnus-summary-article-number ()
2958   "The article number of the article on the current line.
2959 If there isn't an article number here, then we return the current
2960 article number."
2961   '(progn
2962      (gnus-summary-skip-intangible)
2963      (or (get-text-property (point) 'gnus-number)
2964          (gnus-summary-last-subject))))
2965
2966 (defmacro gnus-summary-article-header (&optional number)
2967   "Return the header of article NUMBER."
2968   `(gnus-data-header (gnus-data-find
2969                       ,(or number '(gnus-summary-article-number)))))
2970
2971 (defmacro gnus-summary-thread-level (&optional number)
2972   "Return the level of thread that starts with article NUMBER."
2973   `(if (and (eq gnus-summary-make-false-root 'dummy)
2974             (get-text-property (point) 'gnus-intangible))
2975        0
2976      (gnus-data-level (gnus-data-find
2977                        ,(or number '(gnus-summary-article-number))))))
2978
2979 (defmacro gnus-summary-article-mark (&optional number)
2980   "Return the mark of article NUMBER."
2981   `(gnus-data-mark (gnus-data-find
2982                     ,(or number '(gnus-summary-article-number)))))
2983
2984 (defmacro gnus-summary-article-pos (&optional number)
2985   "Return the position of the line of article NUMBER."
2986   `(gnus-data-pos (gnus-data-find
2987                    ,(or number '(gnus-summary-article-number)))))
2988
2989 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2990 (defmacro gnus-summary-article-subject (&optional number)
2991   "Return current subject string or nil if nothing."
2992   `(let ((headers
2993           ,(if number
2994                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2995              '(gnus-data-header (assq (gnus-summary-article-number)
2996                                       gnus-newsgroup-data)))))
2997      (and headers
2998           (vectorp headers)
2999           (mail-header-subject headers))))
3000
3001 (defmacro gnus-summary-article-score (&optional number)
3002   "Return current article score."
3003   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3004                   gnus-newsgroup-scored))
3005        gnus-summary-default-score 0))
3006
3007 (defun gnus-summary-article-children (&optional number)
3008   "Return a list of article numbers that are children of article NUMBER."
3009   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3010          (level (gnus-data-level (car data)))
3011          l children)
3012     (while (and (setq data (cdr data))
3013                 (> (setq l (gnus-data-level (car data))) level))
3014       (and (= (1+ level) l)
3015            (push (gnus-data-number (car data))
3016                  children)))
3017     (nreverse children)))
3018
3019 (defun gnus-summary-article-parent (&optional number)
3020   "Return the article number of the parent of article NUMBER."
3021   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3022                                     (gnus-data-list t)))
3023          (level (gnus-data-level (car data))))
3024     (if (zerop level)
3025         ()                              ; This is a root.
3026       ;; We search until we find an article with a level less than
3027       ;; this one.  That function has to be the parent.
3028       (while (and (setq data (cdr data))
3029                   (not (< (gnus-data-level (car data)) level))))
3030       (and data (gnus-data-number (car data))))))
3031
3032 (defun gnus-unread-mark-p (mark)
3033   "Say whether MARK is the unread mark."
3034   (= mark gnus-unread-mark))
3035
3036 (defun gnus-read-mark-p (mark)
3037   "Say whether MARK is one of the marks that mark as read.
3038 This is all marks except unread, ticked, dormant, and expirable."
3039   (not (or (= mark gnus-unread-mark)
3040            (= mark gnus-ticked-mark)
3041            (= mark gnus-spam-mark)
3042            (= mark gnus-dormant-mark)
3043            (= mark gnus-expirable-mark))))
3044
3045 (defmacro gnus-article-mark (number)
3046   "Return the MARK of article NUMBER.
3047 This macro should only be used when computing the mark the \"first\"
3048 time; i.e., when generating the summary lines.  After that,
3049 `gnus-summary-article-mark' should be used to examine the
3050 marks of articles."
3051   `(cond
3052     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3053     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3054     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3055     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3056     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3057     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3058     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3059     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3060            gnus-ancient-mark))))
3061
3062 ;; Saving hidden threads.
3063
3064 (defmacro gnus-save-hidden-threads (&rest forms)
3065   "Save hidden threads, eval FORMS, and restore the hidden threads."
3066   (let ((config (make-symbol "config")))
3067     `(let ((,config (gnus-hidden-threads-configuration)))
3068        (unwind-protect
3069            (save-excursion
3070              ,@forms)
3071          (gnus-restore-hidden-threads-configuration ,config)))))
3072 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3073 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3074
3075 (defun gnus-data-compute-positions ()
3076   "Compute the positions of all articles."
3077   (setq gnus-newsgroup-data-reverse nil)
3078   (let ((data gnus-newsgroup-data))
3079     (save-excursion
3080       (gnus-save-hidden-threads
3081         (gnus-summary-show-all-threads)
3082         (goto-char (point-min))
3083         (while data
3084           (while (get-text-property (point) 'gnus-intangible)
3085             (forward-line 1))
3086           (gnus-data-set-pos (car data) (+ (point) 3))
3087           (setq data (cdr data))
3088           (forward-line 1))))))
3089
3090 (defun gnus-hidden-threads-configuration ()
3091   "Return the current hidden threads configuration."
3092   (save-excursion
3093     (let (config)
3094       (goto-char (point-min))
3095       (while (search-forward "\r" nil t)
3096         (push (1- (point)) config))
3097       config)))
3098
3099 (defun gnus-restore-hidden-threads-configuration (config)
3100   "Restore hidden threads configuration from CONFIG."
3101   (save-excursion
3102     (let (point buffer-read-only)
3103       (while (setq point (pop config))
3104         (when (and (< point (point-max))
3105                    (goto-char point)
3106                    (eq (char-after) ?\n))
3107           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3108
3109 ;; Various summary mode internalish functions.
3110
3111 (defun gnus-mouse-pick-article (e)
3112   (interactive "e")
3113   (mouse-set-point e)
3114   (gnus-summary-next-page nil t))
3115
3116 (defun gnus-summary-set-display-table ()
3117   "Change the display table.
3118 Odd characters have a tendency to mess
3119 up nicely formatted displays - we make all possible glyphs
3120 display only a single character."
3121
3122   ;; We start from the standard display table, if any.
3123   (let ((table (or (copy-sequence standard-display-table)
3124                    (make-display-table)))
3125         (i 32))
3126     ;; Nix out all the control chars...
3127     (while (>= (setq i (1- i)) 0)
3128       (aset table i [??]))
3129    ;; ... but not newline and cr, of course.  (cr is necessary for the
3130     ;; selective display).
3131     (aset table ?\n nil)
3132     (aset table ?\r nil)
3133     ;; We keep TAB as well.
3134     (aset table ?\t nil)
3135     ;; We nix out any glyphs over 126 that are not set already.
3136     (let ((i 256))
3137       (while (>= (setq i (1- i)) 127)
3138         ;; Only modify if the entry is nil.
3139         (unless (aref table i)
3140           (aset table i [??]))))
3141     (setq buffer-display-table table)))
3142
3143 (defun gnus-summary-set-article-display-arrow (pos)
3144   "Update the overlay arrow to point to line at position POS."
3145   (when (and gnus-summary-display-arrow
3146              (boundp 'overlay-arrow-position)
3147              (boundp 'overlay-arrow-string))
3148     (save-excursion
3149       (goto-char pos)
3150       (beginning-of-line)
3151       (unless overlay-arrow-position
3152         (setq overlay-arrow-position (make-marker)))
3153       (setq overlay-arrow-string "=>"
3154             overlay-arrow-position (set-marker overlay-arrow-position
3155                                                (point)
3156                                                (current-buffer))))))
3157
3158 (defun gnus-summary-setup-buffer (group)
3159   "Initialize summary buffer."
3160   (let ((buffer (gnus-summary-buffer-name group))
3161         (dead-name (concat "*Dead Summary "
3162                            (gnus-group-decoded-name group) "*")))
3163     ;; If a dead summary buffer exists, we kill it.
3164     (when (gnus-buffer-live-p dead-name)
3165       (gnus-kill-buffer dead-name))
3166     (if (get-buffer buffer)
3167         (progn
3168           (set-buffer buffer)
3169           (setq gnus-summary-buffer (current-buffer))
3170           (not gnus-newsgroup-prepared))
3171       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3172       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3173       (gnus-summary-mode group)
3174       (when gnus-carpal
3175         (gnus-carpal-setup-buffer 'summary))
3176       (unless gnus-single-article-buffer
3177         (make-local-variable 'gnus-article-buffer)
3178         (make-local-variable 'gnus-article-current)
3179         (make-local-variable 'gnus-original-article-buffer))
3180       (setq gnus-newsgroup-name group)
3181       ;; Set any local variables in the group parameters.
3182       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3183       t)))
3184
3185 (defun gnus-set-global-variables ()
3186   "Set the global equivalents of the buffer-local variables.
3187 They are set to the latest values they had.  These reflect the summary
3188 buffer that was in action when the last article was fetched."
3189   (when (eq major-mode 'gnus-summary-mode)
3190     (setq gnus-summary-buffer (current-buffer))
3191     (let ((name gnus-newsgroup-name)
3192           (marked gnus-newsgroup-marked)
3193           (spam gnus-newsgroup-spam-marked)
3194           (unread gnus-newsgroup-unreads)
3195           (headers gnus-current-headers)
3196           (data gnus-newsgroup-data)
3197           (summary gnus-summary-buffer)
3198           (article-buffer gnus-article-buffer)
3199           (original gnus-original-article-buffer)
3200           (gac gnus-article-current)
3201           (reffed gnus-reffed-article-number)
3202           (score-file gnus-current-score-file)
3203           (default-charset gnus-newsgroup-charset)
3204           vlist)
3205       (let ((locals gnus-newsgroup-variables))
3206         (while locals
3207           (if (consp (car locals))
3208               (push (eval (caar locals)) vlist)
3209             (push (eval (car locals)) vlist))
3210           (setq locals (cdr locals)))
3211         (setq vlist (nreverse vlist)))
3212       (with-current-buffer gnus-group-buffer
3213         (setq gnus-newsgroup-name name
3214               gnus-newsgroup-marked marked
3215               gnus-newsgroup-spam-marked spam
3216               gnus-newsgroup-unreads unread
3217               gnus-current-headers headers
3218               gnus-newsgroup-data data
3219               gnus-article-current gac
3220               gnus-summary-buffer summary
3221               gnus-article-buffer article-buffer
3222               gnus-original-article-buffer original
3223               gnus-reffed-article-number reffed
3224               gnus-current-score-file score-file
3225               gnus-newsgroup-charset default-charset)
3226         (let ((locals gnus-newsgroup-variables))
3227           (while locals
3228             (if (consp (car locals))
3229                 (set (caar locals) (pop vlist))
3230               (set (car locals) (pop vlist)))
3231             (setq locals (cdr locals))))
3232         ;; The article buffer also has local variables.
3233         (when (gnus-buffer-live-p gnus-article-buffer)
3234           (set-buffer gnus-article-buffer)
3235           (setq gnus-summary-buffer summary))))))
3236
3237 (defun gnus-summary-article-unread-p (article)
3238   "Say whether ARTICLE is unread or not."
3239   (memq article gnus-newsgroup-unreads))
3240
3241 (defun gnus-summary-first-article-p (&optional article)
3242   "Return whether ARTICLE is the first article in the buffer."
3243   (if (not (setq article (or article (gnus-summary-article-number))))
3244       nil
3245     (eq article (caar gnus-newsgroup-data))))
3246
3247 (defun gnus-summary-last-article-p (&optional article)
3248   "Return whether ARTICLE is the last article in the buffer."
3249   (if (not (setq article (or article (gnus-summary-article-number))))
3250       ;; All non-existent numbers are the last article.  :-)
3251       t
3252     (not (cdr (gnus-data-find-list article)))))
3253
3254 (defun gnus-make-thread-indent-array ()
3255   (let ((n 200))
3256     (unless (and gnus-thread-indent-array
3257                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3258       (setq gnus-thread-indent-array (make-vector 201 "")
3259             gnus-thread-indent-array-level gnus-thread-indent-level)
3260       (while (>= n 0)
3261         (aset gnus-thread-indent-array n
3262               (make-string (* n gnus-thread-indent-level) ? ))
3263         (setq n (1- n))))))
3264
3265 (defun gnus-update-summary-mark-positions ()
3266   "Compute where the summary marks are to go."
3267   (save-excursion
3268     (when (gnus-buffer-exists-p gnus-summary-buffer)
3269       (set-buffer gnus-summary-buffer))
3270     (let ((gnus-replied-mark 129)
3271           (gnus-score-below-mark 130)
3272           (gnus-score-over-mark 130)
3273           (gnus-undownloaded-mark 131)
3274           (spec gnus-summary-line-format-spec)
3275           gnus-visual pos)
3276       (save-excursion
3277         (gnus-set-work-buffer)
3278         (let ((gnus-summary-line-format-spec spec)
3279               (gnus-newsgroup-downloadable '(0)))
3280           (gnus-summary-insert-line
3281            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3282            0 nil t 128 t nil "" nil 1)
3283           (goto-char (point-min))
3284           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3285                                              (- (point) (point-min) 1)))))
3286           (goto-char (point-min))
3287           (push (cons 'replied (and (search-forward "\201" nil t)
3288                                     (- (point) (point-min) 1)))
3289                 pos)
3290           (goto-char (point-min))
3291           (push (cons 'score (and (search-forward "\202" nil t)
3292                                   (- (point) (point-min) 1)))
3293                 pos)
3294           (goto-char (point-min))
3295           (push (cons 'download
3296                       (and (search-forward "\203" nil t)
3297                            (- (point) (point-min) 1)))
3298                 pos)))
3299       (setq gnus-summary-mark-positions pos))))
3300
3301 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3302   "Insert a dummy root in the summary buffer."
3303   (beginning-of-line)
3304   (gnus-add-text-properties
3305    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3306    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3307
3308 (defun gnus-summary-extract-address-component (from)
3309   (or (car (funcall gnus-extract-address-components from))
3310       from))
3311
3312 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3313   (let ((mail-parse-charset gnus-newsgroup-charset)
3314         ; Is it really necessary to do this next part for each summary line?
3315         ; Luckily, doesn't seem to slow things down much.
3316         (mail-parse-ignored-charsets
3317          (with-current-buffer gnus-summary-buffer
3318            gnus-newsgroup-ignored-charsets)))
3319     (or
3320      (and gnus-ignored-from-addresses
3321           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3322           (let ((extra-headers (mail-header-extra header))
3323                 to
3324                 newsgroups)
3325             (cond
3326              ((setq to (cdr (assq 'To extra-headers)))
3327               (concat "-> "
3328                       (inline
3329                         (gnus-summary-extract-address-component
3330                          (funcall gnus-decode-encoded-word-function to)))))
3331              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3332               (concat "=> " newsgroups)))))
3333      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3334
3335 (defun gnus-summary-insert-line (gnus-tmp-header
3336                                  gnus-tmp-level gnus-tmp-current
3337                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3338                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3339                                  &optional gnus-tmp-dummy gnus-tmp-score
3340                                  gnus-tmp-process)
3341   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3342          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3343          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3344          (gnus-tmp-score-char
3345           (if (or (null gnus-summary-default-score)
3346                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3347                       gnus-summary-zcore-fuzz))
3348               ?                         ;Whitespace
3349             (if (< gnus-tmp-score gnus-summary-default-score)
3350                 gnus-score-below-mark gnus-score-over-mark)))
3351          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3352          (gnus-tmp-replied
3353           (cond (gnus-tmp-process gnus-process-mark)
3354                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3355                  gnus-cached-mark)
3356                 (gnus-tmp-replied gnus-replied-mark)
3357                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3358                  gnus-forwarded-mark)
3359                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3360                  gnus-saved-mark)
3361                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3362                  gnus-recent-mark)
3363                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3364                  gnus-unseen-mark)
3365                 (t gnus-no-mark)))
3366          (gnus-tmp-downloaded
3367           (cond (undownloaded
3368                  gnus-undownloaded-mark)
3369                 (gnus-newsgroup-agentized
3370                  gnus-downloaded-mark)
3371                 (t
3372                  gnus-no-mark)))
3373          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3374          (gnus-tmp-name
3375           (cond
3376            ((string-match "<[^>]+> *$" gnus-tmp-from)
3377             (let ((beg (match-beginning 0)))
3378               (or (and (string-match "^\".+\"" gnus-tmp-from)
3379                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3380                   (substring gnus-tmp-from 0 beg))))
3381            ((string-match "(.+)" gnus-tmp-from)
3382             (substring gnus-tmp-from
3383                        (1+ (match-beginning 0)) (1- (match-end 0))))
3384            (t gnus-tmp-from)))
3385          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3386          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3387          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3388          (buffer-read-only nil))
3389     (when (string= gnus-tmp-name "")
3390       (setq gnus-tmp-name gnus-tmp-from))
3391     (unless (numberp gnus-tmp-lines)
3392       (setq gnus-tmp-lines -1))
3393     (if (= gnus-tmp-lines -1)
3394         (setq gnus-tmp-lines "?")
3395       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3396       (gnus-put-text-property
3397      (point)
3398      (progn (eval gnus-summary-line-format-spec) (point))
3399        'gnus-number gnus-tmp-number)
3400     (when (gnus-visual-p 'summary-highlight 'highlight)
3401       (forward-line -1)
3402       (gnus-run-hooks 'gnus-summary-update-hook)
3403       (forward-line 1))))
3404
3405 (defun gnus-summary-update-line (&optional dont-update)
3406   "Update summary line after change."
3407   (when (and gnus-summary-default-score
3408              (not gnus-summary-inhibit-highlight))
3409     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3410            (article (gnus-summary-article-number))
3411            (score (gnus-summary-article-score article)))
3412       (unless dont-update
3413         (if (and gnus-summary-mark-below
3414                  (< (gnus-summary-article-score)
3415                     gnus-summary-mark-below))
3416             ;; This article has a low score, so we mark it as read.
3417             (when (memq article gnus-newsgroup-unreads)
3418               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3419           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3420             ;; This article was previously marked as read on account
3421             ;; of a low score, but now it has risen, so we mark it as
3422             ;; unread.
3423             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3424         (gnus-summary-update-mark
3425          (if (or (null gnus-summary-default-score)
3426                  (<= (abs (- score gnus-summary-default-score))
3427                      gnus-summary-zcore-fuzz))
3428              ?                          ;Whitespace
3429            (if (< score gnus-summary-default-score)
3430                gnus-score-below-mark gnus-score-over-mark))
3431          'score))
3432       ;; Do visual highlighting.
3433       (when (gnus-visual-p 'summary-highlight 'highlight)
3434         (gnus-run-hooks 'gnus-summary-update-hook)))))
3435
3436 (defvar gnus-tmp-new-adopts nil)
3437
3438 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3439   "Return the number of articles in THREAD.
3440 This may be 0 in some cases -- if none of the articles in
3441 the thread are to be displayed."
3442   (let* ((number
3443          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3444           (cond
3445            ((not (listp thread))
3446             1)
3447            ((and (consp thread) (cdr thread))
3448             (apply
3449              '+ 1 (mapcar
3450                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3451            ((null thread)
3452             1)
3453            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3454             1)
3455            (t 0))))
3456     (when (and level (zerop level) gnus-tmp-new-adopts)
3457       (incf number
3458             (apply '+ (mapcar
3459                        'gnus-summary-number-of-articles-in-thread
3460                        gnus-tmp-new-adopts))))
3461     (if char
3462         (if (> number 1) gnus-not-empty-thread-mark
3463           gnus-empty-thread-mark)
3464       number)))
3465
3466 (defsubst gnus-summary-line-message-size (head)
3467   "Return pretty-printed version of message size.
3468 This function is intended to be used in
3469 `gnus-summary-line-format-alist'."
3470   (let ((c (or (mail-header-chars head) -1)))
3471     (cond ((< c 0) "n/a")               ; chars not available
3472           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3473           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3474           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3475           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3476
3477
3478 (defun gnus-summary-set-local-parameters (group)
3479   "Go through the local params of GROUP and set all variable specs in that list."
3480   (let ((params (gnus-group-find-parameter group))
3481         (vars '(quit-config))           ; Ignore quit-config.
3482         elem)
3483     (while params
3484       (setq elem (car params)
3485             params (cdr params))
3486       (and (consp elem)                 ; Has to be a cons.
3487            (consp (cdr elem))           ; The cdr has to be a list.
3488            (symbolp (car elem))         ; Has to be a symbol in there.
3489            (not (memq (car elem) vars))
3490            (ignore-errors               ; So we set it.
3491              (push (car elem) vars)
3492              (make-local-variable (car elem))
3493              (set (car elem) (eval (nth 1 elem))))))))
3494
3495 (defun gnus-summary-read-group (group &optional show-all no-article
3496                                       kill-buffer no-display backward
3497                                       select-articles)
3498   "Start reading news in newsgroup GROUP.
3499 If SHOW-ALL is non-nil, already read articles are also listed.
3500 If NO-ARTICLE is non-nil, no article is selected initially.
3501 If NO-DISPLAY, don't generate a summary buffer."
3502   (let (result)
3503     (while (and group
3504                 (null (setq result
3505                             (let ((gnus-auto-select-next nil))
3506                               (or (gnus-summary-read-group-1
3507                                    group show-all no-article
3508                                    kill-buffer no-display
3509                                    select-articles)
3510                                   (setq show-all nil
3511                                         select-articles nil)))))
3512                 (eq gnus-auto-select-next 'quietly))
3513       (set-buffer gnus-group-buffer)
3514       ;; The entry function called above goes to the next
3515       ;; group automatically, so we go two groups back
3516       ;; if we are searching for the previous group.
3517       (when backward
3518         (gnus-group-prev-unread-group 2))
3519       (if (not (equal group (gnus-group-group-name)))
3520           (setq group (gnus-group-group-name))
3521         (setq group nil)))
3522     result))
3523
3524 (defun gnus-summary-read-group-1 (group show-all no-article
3525                                         kill-buffer no-display
3526                                         &optional select-articles)
3527   ;; Killed foreign groups can't be entered.
3528   ;;  (when (and (not (gnus-group-native-p group))
3529   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3530   ;;    (error "Dead non-native groups can't be entered"))
3531   (gnus-message 5 "Retrieving newsgroup: %s..."
3532                 (gnus-group-decoded-name group))
3533   (let* ((new-group (gnus-summary-setup-buffer group))
3534          (quit-config (gnus-group-quit-config group))
3535          (did-select (and new-group (gnus-select-newsgroup
3536                                      group show-all select-articles))))
3537     (cond
3538      ;; This summary buffer exists already, so we just select it.
3539      ((not new-group)
3540       (gnus-set-global-variables)
3541       (when kill-buffer
3542         (gnus-kill-or-deaden-summary kill-buffer))
3543       (gnus-configure-windows 'summary 'force)
3544       (gnus-set-mode-line 'summary)
3545       (gnus-summary-position-point)
3546       (message "")
3547       t)
3548      ;; We couldn't select this group.
3549      ((null did-select)
3550       (when (and (eq major-mode 'gnus-summary-mode)
3551                  (not (equal (current-buffer) kill-buffer)))
3552         (kill-buffer (current-buffer))
3553         (if (not quit-config)
3554             (progn
3555               ;; Update the info -- marks might need to be removed,
3556               ;; for instance.
3557               (gnus-summary-update-info)
3558               (set-buffer gnus-group-buffer)
3559               (gnus-group-jump-to-group group)
3560               (gnus-group-next-unread-group 1))
3561           (gnus-handle-ephemeral-exit quit-config)))
3562       (let ((grpinfo (gnus-get-info group)))
3563         (if (null (gnus-info-read grpinfo))
3564             (gnus-message 3 "Group %s contains no messages"
3565                           (gnus-group-decoded-name group))
3566           (gnus-message 3 "Can't select group")))
3567       nil)
3568      ;; The user did a `C-g' while prompting for number of articles,
3569      ;; so we exit this group.
3570      ((eq did-select 'quit)
3571       (and (eq major-mode 'gnus-summary-mode)
3572            (not (equal (current-buffer) kill-buffer))
3573            (kill-buffer (current-buffer)))
3574       (when kill-buffer
3575         (gnus-kill-or-deaden-summary kill-buffer))
3576       (if (not quit-config)
3577           (progn
3578             (set-buffer gnus-group-buffer)
3579             (gnus-group-jump-to-group group)
3580             (gnus-group-next-unread-group 1)
3581             (gnus-configure-windows 'group 'force))
3582         (gnus-handle-ephemeral-exit quit-config))
3583       ;; Finally signal the quit.
3584       (signal 'quit nil))
3585      ;; The group was successfully selected.
3586      (t
3587       (gnus-set-global-variables)
3588       ;; Save the active value in effect when the group was entered.
3589       (setq gnus-newsgroup-active
3590             (gnus-copy-sequence
3591              (gnus-active gnus-newsgroup-name)))
3592       ;; You can change the summary buffer in some way with this hook.
3593       (gnus-run-hooks 'gnus-select-group-hook)
3594       (gnus-update-format-specifications
3595        nil 'summary 'summary-mode 'summary-dummy)
3596       (gnus-update-summary-mark-positions)
3597       ;; Do score processing.
3598       (when gnus-use-scoring
3599         (gnus-possibly-score-headers))
3600       ;; Check whether to fill in the gaps in the threads.
3601       (when gnus-build-sparse-threads
3602         (gnus-build-sparse-threads))
3603       ;; Find the initial limit.
3604       (if gnus-show-threads
3605           (if show-all
3606               (let ((gnus-newsgroup-dormant nil))
3607                 (gnus-summary-initial-limit show-all))
3608             (gnus-summary-initial-limit show-all))
3609         ;; When unthreaded, all articles are always shown.
3610         (setq gnus-newsgroup-limit
3611               (mapcar
3612                (lambda (header) (mail-header-number header))
3613                gnus-newsgroup-headers)))
3614       ;; Generate the summary buffer.
3615       (unless no-display
3616         (gnus-summary-prepare))
3617       (when gnus-use-trees
3618         (gnus-tree-open group)
3619         (setq gnus-summary-highlight-line-function
3620               'gnus-tree-highlight-article))
3621       ;; If the summary buffer is empty, but there are some low-scored
3622       ;; articles or some excluded dormants, we include these in the
3623       ;; buffer.
3624       (when (and (zerop (buffer-size))
3625                  (not no-display))
3626         (cond (gnus-newsgroup-dormant
3627                (gnus-summary-limit-include-dormant))
3628               ((and gnus-newsgroup-scored show-all)
3629                (gnus-summary-limit-include-expunged t))))
3630       ;; Function `gnus-apply-kill-file' must be called in this hook.
3631       (gnus-run-hooks 'gnus-apply-kill-hook)
3632       (if (and (zerop (buffer-size))
3633                (not no-display))
3634           (progn
3635             ;; This newsgroup is empty.
3636             (gnus-summary-catchup-and-exit nil t)
3637             (gnus-message 6 "No unread news")
3638             (when kill-buffer
3639               (gnus-kill-or-deaden-summary kill-buffer))
3640             ;; Return nil from this function.
3641             nil)
3642         ;; Hide conversation thread subtrees.  We cannot do this in
3643         ;; gnus-summary-prepare-hook since kill processing may not
3644         ;; work with hidden articles.
3645         (gnus-summary-maybe-hide-threads)
3646         (when kill-buffer
3647           (gnus-kill-or-deaden-summary kill-buffer))
3648         (gnus-summary-auto-select-subject)
3649         ;; Show first unread article if requested.
3650         (if (and (not no-article)
3651                  (not no-display)
3652                  gnus-newsgroup-unreads
3653                  gnus-auto-select-first)
3654             (progn
3655               (gnus-configure-windows 'summary)
3656               (let ((art (gnus-summary-article-number)))
3657                 (unless (and (not gnus-plugged)
3658                              (or (memq art gnus-newsgroup-undownloaded)
3659                                  (memq art gnus-newsgroup-downloadable)))
3660                   (gnus-summary-goto-article art))))
3661           ;; Don't select any articles.
3662           (gnus-summary-position-point)
3663           (gnus-configure-windows 'summary 'force)
3664           (gnus-set-mode-line 'summary))
3665         (when (and gnus-auto-center-group
3666                    (get-buffer-window gnus-group-buffer t))
3667           ;; Gotta use windows, because recenter does weird stuff if
3668           ;; the current buffer ain't the displayed window.
3669           (let ((owin (selected-window)))
3670             (select-window (get-buffer-window gnus-group-buffer t))
3671             (when (gnus-group-goto-group group)
3672               (recenter))
3673             (select-window owin)))
3674         ;; Mark this buffer as "prepared".
3675         (setq gnus-newsgroup-prepared t)
3676         (gnus-run-hooks 'gnus-summary-prepared-hook)
3677         (unless (gnus-ephemeral-group-p group)
3678           (gnus-group-update-group group))
3679         t)))))
3680
3681 (defun gnus-summary-auto-select-subject ()
3682   "Select the subject line on initial group entry."
3683   (goto-char (point-min))
3684   (cond
3685    ((eq gnus-auto-select-subject 'best)
3686     (gnus-summary-best-unread-subject))
3687    ((eq gnus-auto-select-subject 'unread)
3688     (gnus-summary-first-unread-subject))
3689    ((eq gnus-auto-select-subject 'unseen)
3690     (gnus-summary-first-unseen-subject))
3691    ((eq gnus-auto-select-subject 'unseen-or-unread)
3692     (gnus-summary-first-unseen-or-unread-subject))
3693    ((eq gnus-auto-select-subject 'first)
3694     ;; Do nothing.
3695     )
3696    ((functionp gnus-auto-select-subject)
3697     (funcall gnus-auto-select-subject))))
3698
3699 (defun gnus-summary-prepare ()
3700   "Generate the summary buffer."
3701   (interactive)
3702   (let ((buffer-read-only nil))
3703     (erase-buffer)
3704     (setq gnus-newsgroup-data nil
3705           gnus-newsgroup-data-reverse nil)
3706     (gnus-run-hooks 'gnus-summary-generate-hook)
3707     ;; Generate the buffer, either with threads or without.
3708     (when gnus-newsgroup-headers
3709       (gnus-summary-prepare-threads
3710        (if gnus-show-threads
3711            (gnus-sort-gathered-threads
3712             (funcall gnus-summary-thread-gathering-function
3713                      (gnus-sort-threads
3714                       (gnus-cut-threads (gnus-make-threads)))))
3715          ;; Unthreaded display.
3716          (gnus-sort-articles gnus-newsgroup-headers))))
3717     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3718     ;; Call hooks for modifying summary buffer.
3719     (goto-char (point-min))
3720     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3721
3722 (defsubst gnus-general-simplify-subject (subject)
3723   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3724   (setq subject
3725         (cond
3726          ;; Truncate the subject.
3727          (gnus-simplify-subject-functions
3728           (gnus-map-function gnus-simplify-subject-functions subject))
3729          ((numberp gnus-summary-gather-subject-limit)
3730           (setq subject (gnus-simplify-subject-re subject))
3731           (if (> (length subject) gnus-summary-gather-subject-limit)
3732               (substring subject 0 gnus-summary-gather-subject-limit)
3733             subject))
3734          ;; Fuzzily simplify it.
3735          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3736           (gnus-simplify-subject-fuzzy subject))
3737          ;; Just remove the leading "Re:".
3738          (t
3739           (gnus-simplify-subject-re subject))))
3740
3741   (if (and gnus-summary-gather-exclude-subject
3742            (string-match gnus-summary-gather-exclude-subject subject))
3743       nil                         ; This article shouldn't be gathered
3744     subject))
3745
3746 (defun gnus-summary-simplify-subject-query ()
3747   "Query where the respool algorithm would put this article."
3748   (interactive)
3749   (gnus-summary-select-article)
3750   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3751
3752 (defun gnus-gather-threads-by-subject (threads)
3753   "Gather threads by looking at Subject headers."
3754   (if (not gnus-summary-make-false-root)
3755       threads
3756     (let ((hashtb (gnus-make-hashtable 1024))
3757           (prev threads)
3758           (result threads)
3759           subject hthread whole-subject)
3760       (while threads
3761         (setq subject (gnus-general-simplify-subject
3762                        (setq whole-subject (mail-header-subject
3763                                             (caar threads)))))
3764         (when subject
3765           (if (setq hthread (gnus-gethash subject hashtb))
3766               (progn
3767                 ;; We enter a dummy root into the thread, if we
3768                 ;; haven't done that already.
3769                 (unless (stringp (caar hthread))
3770                   (setcar hthread (list whole-subject (car hthread))))
3771                 ;; We add this new gathered thread to this gathered
3772                 ;; thread.
3773                 (setcdr (car hthread)
3774                         (nconc (cdar hthread) (list (car threads))))
3775                 ;; Remove it from the list of threads.
3776                 (setcdr prev (cdr threads))
3777                 (setq threads prev))
3778             ;; Enter this thread into the hash table.
3779             (gnus-sethash subject
3780                           (if gnus-summary-make-false-root-always
3781                               (progn
3782                                 ;; If you want a dummy root above all
3783                                 ;; threads...
3784                                 (setcar threads (list whole-subject
3785                                                       (car threads)))
3786                                 threads)
3787                             threads)
3788                           hashtb)))
3789         (setq prev threads)
3790         (setq threads (cdr threads)))
3791       result)))
3792
3793 (defun gnus-gather-threads-by-references (threads)
3794   "Gather threads by looking at References headers."
3795   (let ((idhashtb (gnus-make-hashtable 1024))
3796         (thhashtb (gnus-make-hashtable 1024))
3797         (prev threads)
3798         (result threads)
3799         ids references id gthread gid entered ref)
3800     (while threads
3801       (when (setq references (mail-header-references (caar threads)))
3802         (setq id (mail-header-id (caar threads))
3803               ids (inline (gnus-split-references references))
3804               entered nil)
3805         (while (setq ref (pop ids))
3806           (setq ids (delete ref ids))
3807           (if (not (setq gid (gnus-gethash ref idhashtb)))
3808               (progn
3809                 (gnus-sethash ref id idhashtb)
3810                 (gnus-sethash id threads thhashtb))
3811             (setq gthread (gnus-gethash gid thhashtb))
3812             (unless entered
3813               ;; We enter a dummy root into the thread, if we
3814               ;; haven't done that already.
3815               (unless (stringp (caar gthread))
3816                 (setcar gthread (list (mail-header-subject (caar gthread))
3817                                       (car gthread))))
3818               ;; We add this new gathered thread to this gathered
3819               ;; thread.
3820               (setcdr (car gthread)
3821                       (nconc (cdar gthread) (list (car threads)))))
3822             ;; Add it into the thread hash table.
3823             (gnus-sethash id gthread thhashtb)
3824             (setq entered t)
3825             ;; Remove it from the list of threads.
3826             (setcdr prev (cdr threads))
3827             (setq threads prev))))
3828       (setq prev threads)
3829       (setq threads (cdr threads)))
3830     result))
3831
3832 (defun gnus-sort-gathered-threads (threads)
3833   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3834   (let ((result threads))
3835     (while threads
3836       (when (stringp (caar threads))
3837         (setcdr (car threads)
3838                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3839       (setq threads (cdr threads)))
3840     result))
3841
3842 (defun gnus-thread-loop-p (root thread)
3843   "Say whether ROOT is in THREAD."
3844   (let ((stack (list thread))
3845         (infloop 0)
3846         th)
3847     (while (setq thread (pop stack))
3848       (setq th (cdr thread))
3849       (while (and th
3850                   (not (eq (caar th) root)))
3851         (pop th))
3852       (if th
3853           ;; We have found a loop.
3854           (let (ref-dep)
3855             (setcdr thread (delq (car th) (cdr thread)))
3856             (if (boundp (setq ref-dep (intern "none"
3857                                               gnus-newsgroup-dependencies)))
3858                 (setcdr (symbol-value ref-dep)
3859                         (nconc (cdr (symbol-value ref-dep))
3860                                (list (car th))))
3861               (set ref-dep (list nil (car th))))
3862             (setq infloop 1
3863                   stack nil))
3864         ;; Push all the subthreads onto the stack.
3865         (push (cdr thread) stack)))
3866     infloop))
3867
3868 (defun gnus-make-threads ()
3869   "Go through the dependency hashtb and find the roots.  Return all threads."
3870   (let (threads)
3871     (while (catch 'infloop
3872              (mapatoms
3873               (lambda (refs)
3874                 ;; Deal with self-referencing References loops.
3875                 (when (and (car (symbol-value refs))
3876                            (not (zerop
3877                                  (apply
3878                                   '+
3879                                   (mapcar
3880                                    (lambda (thread)
3881                                      (gnus-thread-loop-p
3882                                       (car (symbol-value refs)) thread))
3883                                    (cdr (symbol-value refs)))))))
3884                   (setq threads nil)
3885                   (throw 'infloop t))
3886                 (unless (car (symbol-value refs))
3887                   ;; These threads do not refer back to any other
3888                   ;; articles, so they're roots.
3889                   (setq threads (append (cdr (symbol-value refs)) threads))))
3890               gnus-newsgroup-dependencies)))
3891     threads))
3892
3893 ;; Build the thread tree.
3894 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3895   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3896
3897 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3898 if it was already present.
3899
3900 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3901 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3902 Message-IDs will be renamed to a unique Message-ID before being
3903 entered.
3904
3905 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3906   (let* ((id (mail-header-id header))
3907          (id-dep (and id (intern id dependencies)))
3908          parent-id ref ref-dep ref-header replaced)
3909     ;; Enter this `header' in the `dependencies' table.
3910     (cond
3911      ((not id-dep)
3912       (setq header nil))
3913      ;; The first two cases do the normal part: enter a new `header'
3914      ;; in the `dependencies' table.
3915      ((not (boundp id-dep))
3916       (set id-dep (list header)))
3917      ((null (car (symbol-value id-dep)))
3918       (setcar (symbol-value id-dep) header))
3919
3920      ;; From here the `header' was already present in the
3921      ;; `dependencies' table.
3922      (force-new
3923       ;; Overrides an existing entry;
3924       ;; just set the header part of the entry.
3925       (setcar (symbol-value id-dep) header)
3926       (setq replaced t))
3927
3928      ;; Renames the existing `header' to a unique Message-ID.
3929      ((not gnus-summary-ignore-duplicates)
3930       ;; An article with this Message-ID has already been seen.
3931       ;; We rename the Message-ID.
3932       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3933            (list header))
3934       (mail-header-set-id header id))
3935
3936      ;; The last case ignores an existing entry, except it adds any
3937      ;; additional Xrefs (in case the two articles came from different
3938      ;; servers.
3939      ;; Also sets `header' to `nil' meaning that the `dependencies'
3940      ;; table was *not* modified.
3941      (t
3942       (mail-header-set-xref
3943        (car (symbol-value id-dep))
3944        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3945                    "")
3946                (or (mail-header-xref header) "")))
3947       (setq header nil)))
3948
3949     (when (and header (not replaced))
3950       ;; First check that we are not creating a References loop.
3951       (setq parent-id (gnus-parent-id (mail-header-references header)))
3952       (setq ref parent-id)
3953       (while (and ref
3954                   (setq ref-dep (intern-soft ref dependencies))
3955                   (boundp ref-dep)
3956                   (setq ref-header (car (symbol-value ref-dep))))
3957         (if (string= id ref)
3958             ;; Yuk!  This is a reference loop.  Make the article be a
3959             ;; root article.
3960             (progn
3961               (mail-header-set-references (car (symbol-value id-dep)) "none")
3962               (setq ref nil)
3963               (setq parent-id nil))
3964           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3965       (setq ref-dep (intern (or parent-id "none") dependencies))
3966       (if (boundp ref-dep)
3967           (setcdr (symbol-value ref-dep)
3968                   (nconc (cdr (symbol-value ref-dep))
3969                          (list (symbol-value id-dep))))
3970         (set ref-dep (list nil (symbol-value id-dep)))))
3971     header))
3972
3973 (defun gnus-extract-message-id-from-in-reply-to (string)
3974   (if (string-match "<[^>]+>" string)
3975       (substring string (match-beginning 0) (match-end 0))
3976     nil))
3977
3978 (defun gnus-build-sparse-threads ()
3979   (let ((headers gnus-newsgroup-headers)
3980         (mail-parse-charset gnus-newsgroup-charset)
3981         (gnus-summary-ignore-duplicates t)
3982         header references generation relations
3983         subject child end new-child date)
3984     ;; First we create an alist of generations/relations, where
3985     ;; generations is how much we trust the relation, and the relation
3986     ;; is parent/child.
3987     (gnus-message 7 "Making sparse threads...")
3988     (save-excursion
3989       (nnheader-set-temp-buffer " *gnus sparse threads*")
3990       (while (setq header (pop headers))
3991         (when (and (setq references (mail-header-references header))
3992                    (not (string= references "")))
3993           (insert references)
3994           (setq child (mail-header-id header)
3995                 subject (mail-header-subject header)
3996                 date (mail-header-date header)
3997                 generation 0)
3998           (while (search-backward ">" nil t)
3999             (setq end (1+ (point)))
4000             (when (search-backward "<" nil t)
4001               (setq new-child (buffer-substring (point) end))
4002               (push (list (incf generation)
4003                           child (setq child new-child)
4004                           subject date)
4005                     relations)))
4006           (when child
4007             (push (list (1+ generation) child nil subject) relations))
4008           (erase-buffer)))
4009       (kill-buffer (current-buffer)))
4010     ;; Sort over trustworthiness.
4011     (mapcar
4012      (lambda (relation)
4013        (when (gnus-dependencies-add-header
4014               (make-full-mail-header
4015                gnus-reffed-article-number
4016                (nth 3 relation) "" (or (nth 4 relation) "")
4017                (nth 1 relation)
4018                (or (nth 2 relation) "") 0 0 "")
4019               gnus-newsgroup-dependencies nil)
4020          (push gnus-reffed-article-number gnus-newsgroup-limit)
4021          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4022          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4023                gnus-newsgroup-reads)
4024          (decf gnus-reffed-article-number)))
4025      (sort relations 'car-less-than-car))
4026     (gnus-message 7 "Making sparse threads...done")))
4027
4028 (defun gnus-build-old-threads ()
4029   ;; Look at all the articles that refer back to old articles, and
4030   ;; fetch the headers for the articles that aren't there.  This will
4031   ;; build complete threads - if the roots haven't been expired by the
4032   ;; server, that is.
4033   (let ((mail-parse-charset gnus-newsgroup-charset)
4034         id heads)
4035     (mapatoms
4036      (lambda (refs)
4037        (when (not (car (symbol-value refs)))
4038          (setq heads (cdr (symbol-value refs)))
4039          (while heads
4040            (if (memq (mail-header-number (caar heads))
4041                      gnus-newsgroup-dormant)
4042                (setq heads (cdr heads))
4043              (setq id (symbol-name refs))
4044              (while (and (setq id (gnus-build-get-header id))
4045                          (not (car (gnus-id-to-thread id)))))
4046              (setq heads nil)))))
4047      gnus-newsgroup-dependencies)))
4048
4049 (defsubst gnus-remove-odd-characters (string)
4050   "Translate STRING into something that doesn't contain weird characters."
4051   (mm-subst-char-in-string
4052    ?\r ?\-
4053    (mm-subst-char-in-string ?\n ?\- string t) t))
4054
4055 ;; This function has to be called with point after the article number
4056 ;; on the beginning of the line.
4057 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4058   (let ((eol (point-at-eol))
4059         (buffer (current-buffer))
4060         header references in-reply-to)
4061
4062     ;; overview: [num subject from date id refs chars lines misc]
4063     (unwind-protect
4064         (let (x)
4065           (narrow-to-region (point) eol)
4066           (unless (eobp)
4067             (forward-char))
4068
4069           (setq header
4070                 (make-full-mail-header
4071                  number                 ; number
4072                  (condition-case ()     ; subject
4073                      (gnus-remove-odd-characters
4074                       (funcall gnus-decode-encoded-word-function
4075                                (setq x (nnheader-nov-field))))
4076                    (error x))
4077                  (condition-case ()     ; from
4078                      (gnus-remove-odd-characters
4079                       (funcall gnus-decode-encoded-word-function
4080                                (setq x (nnheader-nov-field))))
4081                    (error x))
4082                  (nnheader-nov-field)   ; date
4083                  (nnheader-nov-read-message-id) ; id
4084                  (setq references (nnheader-nov-field)) ; refs
4085                  (nnheader-nov-read-integer) ; chars
4086                  (nnheader-nov-read-integer) ; lines
4087                  (unless (eobp)
4088                    (if (looking-at "Xref: ")
4089                        (goto-char (match-end 0)))
4090                    (nnheader-nov-field)) ; Xref
4091                  (nnheader-nov-parse-extra)))) ; extra
4092
4093       (widen))
4094
4095     (when (and (string= references "")
4096                (setq in-reply-to (mail-header-extra header))
4097                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4098       (mail-header-set-references
4099        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4100
4101     (when gnus-alter-header-function
4102       (funcall gnus-alter-header-function header))
4103     (gnus-dependencies-add-header header dependencies force-new)))
4104
4105 (defun gnus-build-get-header (id)
4106   "Look through the buffer of NOV lines and find the header to ID.
4107 Enter this line into the dependencies hash table, and return
4108 the id of the parent article (if any)."
4109   (let ((deps gnus-newsgroup-dependencies)
4110         found header)
4111     (prog1
4112         (save-excursion
4113           (set-buffer nntp-server-buffer)
4114           (let ((case-fold-search nil))
4115             (goto-char (point-min))
4116             (while (and (not found)
4117                         (search-forward id nil t))
4118               (beginning-of-line)
4119               (setq found (looking-at
4120                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4121                                    (regexp-quote id))))
4122               (or found (beginning-of-line 2)))
4123             (when found
4124               (beginning-of-line)
4125               (and
4126                (setq header (gnus-nov-parse-line
4127                              (read (current-buffer)) deps))
4128                (gnus-parent-id (mail-header-references header))))))
4129       (when header
4130         (let ((number (mail-header-number header)))
4131           (push number gnus-newsgroup-limit)
4132           (push header gnus-newsgroup-headers)
4133           (if (memq number gnus-newsgroup-unselected)
4134               (progn
4135                 (setq gnus-newsgroup-unreads
4136                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4137                                                number))
4138                 (setq gnus-newsgroup-unselected
4139                       (delq number gnus-newsgroup-unselected)))
4140             (push number gnus-newsgroup-ancient)))))))
4141
4142 (defun gnus-build-all-threads ()
4143   "Read all the headers."
4144   (let ((gnus-summary-ignore-duplicates t)
4145         (mail-parse-charset gnus-newsgroup-charset)
4146         (dependencies gnus-newsgroup-dependencies)
4147         header article)
4148     (save-excursion
4149       (set-buffer nntp-server-buffer)
4150       (let ((case-fold-search nil))
4151         (goto-char (point-min))
4152         (while (not (eobp))
4153           (ignore-errors
4154             (setq article (read (current-buffer))
4155                   header (gnus-nov-parse-line article dependencies)))
4156           (when header
4157             (with-current-buffer gnus-summary-buffer
4158               (push header gnus-newsgroup-headers)
4159               (if (memq (setq article (mail-header-number header))
4160                         gnus-newsgroup-unselected)
4161                   (progn
4162                     (setq gnus-newsgroup-unreads
4163                           (gnus-add-to-sorted-list
4164                            gnus-newsgroup-unreads article))
4165                     (setq gnus-newsgroup-unselected
4166                           (delq article gnus-newsgroup-unselected)))
4167                 (push article gnus-newsgroup-ancient)))
4168             (forward-line 1)))))))
4169
4170 (defun gnus-summary-update-article-line (article header)
4171   "Update the line for ARTICLE using HEADER."
4172   (let* ((id (mail-header-id header))
4173          (thread (gnus-id-to-thread id)))
4174     (unless thread
4175       (error "Article in no thread"))
4176     ;; Update the thread.
4177     (setcar thread header)
4178     (gnus-summary-goto-subject article)
4179     (let* ((datal (gnus-data-find-list article))
4180            (data (car datal))
4181            (buffer-read-only nil)
4182            (level (gnus-summary-thread-level)))
4183       (gnus-delete-line)
4184       (let ((inserted (- (point)
4185                          (progn
4186                            (gnus-summary-insert-line
4187                             header level nil
4188                             (memq article gnus-newsgroup-undownloaded)
4189                             (gnus-article-mark article)
4190                             (memq article gnus-newsgroup-replied)
4191                             (memq article gnus-newsgroup-expirable)
4192                             ;; Only insert the Subject string when it's different
4193                             ;; from the previous Subject string.
4194                             (if (and
4195                                  gnus-show-threads
4196                                  (gnus-subject-equal
4197                                   (condition-case ()
4198                                       (mail-header-subject
4199                                        (gnus-data-header
4200                                         (cadr
4201                                          (gnus-data-find-list
4202                                           article
4203                                           (gnus-data-list t)))))
4204                                     ;; Error on the side of excessive subjects.
4205                                     (error ""))
4206                                   (mail-header-subject header)))
4207                                 ""
4208                               (mail-header-subject header))
4209                             nil (cdr (assq article gnus-newsgroup-scored))
4210                             (memq article gnus-newsgroup-processable))
4211                            (point)))))
4212         (when (cdr datal)
4213           (gnus-data-update-list
4214            (cdr datal)
4215            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4216
4217 (defun gnus-summary-update-article (article &optional iheader)
4218   "Update ARTICLE in the summary buffer."
4219   (set-buffer gnus-summary-buffer)
4220   (let* ((header (gnus-summary-article-header article))
4221          (id (mail-header-id header))
4222          (data (gnus-data-find article))
4223          (thread (gnus-id-to-thread id))
4224          (references (mail-header-references header))
4225          (parent
4226           (gnus-id-to-thread
4227            (or (gnus-parent-id
4228                 (when (and references
4229                            (not (equal "" references)))
4230                   references))
4231                "none")))
4232          (buffer-read-only nil)
4233          (old (car thread)))
4234     (when thread
4235       (unless iheader
4236         (setcar thread nil)
4237         (when parent
4238           (delq thread parent)))
4239       (if (gnus-summary-insert-subject id header)
4240           ;; Set the (possibly) new article number in the data structure.
4241           (gnus-data-set-number data (gnus-id-to-article id))
4242         (setcar thread old)
4243         nil))))
4244
4245 (defun gnus-rebuild-thread (id &optional line)
4246   "Rebuild the thread containing ID.
4247 If LINE, insert the rebuilt thread starting on line LINE."
4248   (let ((buffer-read-only nil)
4249         old-pos current thread data)
4250     (if (not gnus-show-threads)
4251         (setq thread (list (car (gnus-id-to-thread id))))
4252       ;; Get the thread this article is part of.
4253       (setq thread (gnus-remove-thread id)))
4254     (setq old-pos (point-at-bol))
4255     (setq current (save-excursion
4256                     (and (re-search-backward "[\r\n]" nil t)
4257                          (gnus-summary-article-number))))
4258     ;; If this is a gathered thread, we have to go some re-gathering.
4259     (when (stringp (car thread))
4260       (let ((subject (car thread))
4261             roots thr)
4262         (setq thread (cdr thread))
4263         (while thread
4264           (unless (memq (setq thr (gnus-id-to-thread
4265                                    (gnus-root-id
4266                                     (mail-header-id (caar thread)))))
4267                         roots)
4268             (push thr roots))
4269           (setq thread (cdr thread)))
4270         ;; We now have all (unique) roots.
4271         (if (= (length roots) 1)
4272             ;; All the loose roots are now one solid root.
4273             (setq thread (car roots))
4274           (setq thread (cons subject (gnus-sort-threads roots))))))
4275     (let (threads)
4276       ;; We then insert this thread into the summary buffer.
4277       (when line
4278         (goto-char (point-min))
4279         (forward-line (1- line)))
4280       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4281         (if gnus-show-threads
4282             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4283           (gnus-summary-prepare-unthreaded thread))
4284         (setq data (nreverse gnus-newsgroup-data))
4285         (setq threads gnus-newsgroup-threads))
4286       ;; We splice the new data into the data structure.
4287       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4288       ;;!!! then we want to insert at the beginning of the buffer.
4289       ;;!!! That happens to be true with Gnus now, but that may
4290       ;;!!! change in the future.  Perhaps.
4291       (gnus-data-enter-list
4292        (if line nil current) data (- (point) old-pos))
4293       (setq gnus-newsgroup-threads
4294             (nconc threads gnus-newsgroup-threads))
4295       (gnus-data-compute-positions))))
4296
4297 (defun gnus-number-to-header (number)
4298   "Return the header for article NUMBER."
4299   (let ((headers gnus-newsgroup-headers))
4300     (while (and headers
4301                 (not (= number (mail-header-number (car headers)))))
4302       (pop headers))
4303     (when headers
4304       (car headers))))
4305
4306 (defun gnus-parent-headers (in-headers &optional generation)
4307   "Return the headers of the GENERATIONeth parent of HEADERS."
4308   (unless generation
4309     (setq generation 1))
4310   (let ((parent t)
4311         (headers in-headers)
4312         references)
4313     (while (and parent
4314                 (not (zerop generation))
4315                 (setq references (mail-header-references headers)))
4316       (setq headers (if (and references
4317                              (setq parent (gnus-parent-id references)))
4318                         (car (gnus-id-to-thread parent))
4319                       nil))
4320       (decf generation))
4321     (and (not (eq headers in-headers))
4322          headers)))
4323
4324 (defun gnus-id-to-thread (id)
4325   "Return the (sub-)thread where ID appears."
4326   (gnus-gethash id gnus-newsgroup-dependencies))
4327
4328 (defun gnus-id-to-article (id)
4329   "Return the article number of ID."
4330   (let ((thread (gnus-id-to-thread id)))
4331     (when (and thread
4332                (car thread))
4333       (mail-header-number (car thread)))))
4334
4335 (defun gnus-id-to-header (id)
4336   "Return the article headers of ID."
4337   (car (gnus-id-to-thread id)))
4338
4339 (defun gnus-article-displayed-root-p (article)
4340   "Say whether ARTICLE is a root(ish) article."
4341   (let ((level (gnus-summary-thread-level article))
4342         (refs (mail-header-references  (gnus-summary-article-header article)))
4343         particle)
4344     (cond
4345      ((null level) nil)
4346      ((zerop level) t)
4347      ((null refs) t)
4348      ((null (gnus-parent-id refs)) t)
4349      ((and (= 1 level)
4350            (null (setq particle (gnus-id-to-article
4351                                  (gnus-parent-id refs))))
4352            (null (gnus-summary-thread-level particle)))))))
4353
4354 (defun gnus-root-id (id)
4355   "Return the id of the root of the thread where ID appears."
4356   (let (last-id prev)
4357     (while (and id (setq prev (car (gnus-id-to-thread id))))
4358       (setq last-id id
4359             id (gnus-parent-id (mail-header-references prev))))
4360     last-id))
4361
4362 (defun gnus-articles-in-thread (thread)
4363   "Return the list of articles in THREAD."
4364   (cons (mail-header-number (car thread))
4365         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4366
4367 (defun gnus-remove-thread (id &optional dont-remove)
4368   "Remove the thread that has ID in it."
4369   (let (headers thread last-id)
4370     ;; First go up in this thread until we find the root.
4371     (setq last-id (gnus-root-id id)
4372           headers (message-flatten-list (gnus-id-to-thread last-id)))
4373     ;; We have now found the real root of this thread.  It might have
4374     ;; been gathered into some loose thread, so we have to search
4375     ;; through the threads to find the thread we wanted.
4376     (let ((threads gnus-newsgroup-threads)
4377           sub)
4378       (while threads
4379         (setq sub (car threads))
4380         (if (stringp (car sub))
4381             ;; This is a gathered thread, so we look at the roots
4382             ;; below it to find whether this article is in this
4383             ;; gathered root.
4384             (progn
4385               (setq sub (cdr sub))
4386               (while sub
4387                 (when (member (caar sub) headers)
4388                   (setq thread (car threads)
4389                         threads nil
4390                         sub nil))
4391                 (setq sub (cdr sub))))
4392           ;; It's an ordinary thread, so we check it.
4393           (when (eq (car sub) (car headers))
4394             (setq thread sub
4395                   threads nil)))
4396         (setq threads (cdr threads)))
4397       ;; If this article is in no thread, then it's a root.
4398       (if thread
4399           (unless dont-remove
4400             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4401         (setq thread (gnus-id-to-thread last-id)))
4402       (when thread
4403         (prog1
4404             thread                      ; We return this thread.
4405           (unless dont-remove
4406             (if (stringp (car thread))
4407                 (progn
4408                   ;; If we use dummy roots, then we have to remove the
4409                   ;; dummy root as well.
4410                   (when (eq gnus-summary-make-false-root 'dummy)
4411                     ;; We go to the dummy root by going to
4412                     ;; the first sub-"thread", and then one line up.
4413                     (gnus-summary-goto-article
4414                      (mail-header-number (caadr thread)))
4415                     (forward-line -1)
4416                     (gnus-delete-line)
4417                     (gnus-data-compute-positions))
4418                   (setq thread (cdr thread))
4419                   (while thread
4420                     (gnus-remove-thread-1 (car thread))
4421                     (setq thread (cdr thread))))
4422               (gnus-remove-thread-1 thread))))))))
4423
4424 (defun gnus-remove-thread-1 (thread)
4425   "Remove the thread THREAD recursively."
4426   (let ((number (mail-header-number (pop thread)))
4427         d)
4428     (setq thread (reverse thread))
4429     (while thread
4430       (gnus-remove-thread-1 (pop thread)))
4431     (when (setq d (gnus-data-find number))
4432       (goto-char (gnus-data-pos d))
4433       (gnus-summary-show-thread)
4434       (gnus-data-remove
4435        number
4436        (- (point-at-bol)
4437           (prog1
4438               (1+ (point-at-eol))
4439             (gnus-delete-line)))))))
4440
4441 (defun gnus-sort-threads-1 (threads func)
4442   (sort (mapcar (lambda (thread)
4443                   (cons (car thread)
4444                         (and (cdr thread)
4445                              (gnus-sort-threads-1 (cdr thread) func))))
4446                 threads) func))
4447
4448 (defun gnus-sort-threads (threads)
4449   "Sort THREADS."
4450   (if (not gnus-thread-sort-functions)
4451       threads
4452     (gnus-message 8 "Sorting threads...")
4453     (let ((max-lisp-eval-depth 5000))
4454       (prog1 (gnus-sort-threads-1
4455          threads
4456          (gnus-make-sort-function gnus-thread-sort-functions))
4457         (gnus-message 8 "Sorting threads...done")))))
4458
4459 (defun gnus-sort-articles (articles)
4460   "Sort ARTICLES."
4461   (when gnus-article-sort-functions
4462     (gnus-message 7 "Sorting articles...")
4463     (prog1
4464         (setq gnus-newsgroup-headers
4465               (sort articles (gnus-make-sort-function
4466                               gnus-article-sort-functions)))
4467       (gnus-message 7 "Sorting articles...done"))))
4468
4469 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4470 (defmacro gnus-thread-header (thread)
4471   "Return header of first article in THREAD.
4472 Note that THREAD must never, ever be anything else than a variable -
4473 using some other form will lead to serious barfage."
4474   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4475   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4476   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4477         (vector thread) 2))
4478
4479 (defsubst gnus-article-sort-by-number (h1 h2)
4480   "Sort articles by article number."
4481   (< (mail-header-number h1)
4482      (mail-header-number h2)))
4483
4484 (defun gnus-thread-sort-by-number (h1 h2)
4485   "Sort threads by root article number."
4486   (gnus-article-sort-by-number
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defsubst gnus-article-sort-by-random (h1 h2)
4490   "Sort articles by article number."
4491   (zerop (random 2)))
4492
4493 (defun gnus-thread-sort-by-random (h1 h2)
4494   "Sort threads by root article number."
4495   (gnus-article-sort-by-random
4496    (gnus-thread-header h1) (gnus-thread-header h2)))
4497
4498 (defsubst gnus-article-sort-by-lines (h1 h2)
4499   "Sort articles by article Lines header."
4500   (< (mail-header-lines h1)
4501      (mail-header-lines h2)))
4502
4503 (defun gnus-thread-sort-by-lines (h1 h2)
4504   "Sort threads by root article Lines header."
4505   (gnus-article-sort-by-lines
4506    (gnus-thread-header h1) (gnus-thread-header h2)))
4507
4508 (defsubst gnus-article-sort-by-chars (h1 h2)
4509   "Sort articles by octet length."
4510   (< (mail-header-chars h1)
4511      (mail-header-chars h2)))
4512
4513 (defun gnus-thread-sort-by-chars (h1 h2)
4514   "Sort threads by root article octet length."
4515   (gnus-article-sort-by-chars
4516    (gnus-thread-header h1) (gnus-thread-header h2)))
4517
4518 (defsubst gnus-article-sort-by-author (h1 h2)
4519   "Sort articles by root author."
4520   (string-lessp
4521    (let ((extract (funcall
4522                    gnus-extract-address-components
4523                    (mail-header-from h1))))
4524      (or (car extract) (cadr extract) ""))
4525    (let ((extract (funcall
4526                    gnus-extract-address-components
4527                    (mail-header-from h2))))
4528      (or (car extract) (cadr extract) ""))))
4529
4530 (defun gnus-thread-sort-by-author (h1 h2)
4531   "Sort threads by root author."
4532   (gnus-article-sort-by-author
4533    (gnus-thread-header h1)  (gnus-thread-header h2)))
4534
4535 (defsubst gnus-article-sort-by-subject (h1 h2)
4536   "Sort articles by root subject."
4537   (string-lessp
4538    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4539    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4540
4541 (defun gnus-thread-sort-by-subject (h1 h2)
4542   "Sort threads by root subject."
4543   (gnus-article-sort-by-subject
4544    (gnus-thread-header h1) (gnus-thread-header h2)))
4545
4546 (defsubst gnus-article-sort-by-date (h1 h2)
4547   "Sort articles by root article date."
4548   (time-less-p
4549    (gnus-date-get-time (mail-header-date h1))
4550    (gnus-date-get-time (mail-header-date h2))))
4551
4552 (defun gnus-thread-sort-by-date (h1 h2)
4553   "Sort threads by root article date."
4554   (gnus-article-sort-by-date
4555    (gnus-thread-header h1) (gnus-thread-header h2)))
4556
4557 (defsubst gnus-article-sort-by-score (h1 h2)
4558   "Sort articles by root article score.
4559 Unscored articles will be counted as having a score of zero."
4560   (> (or (cdr (assq (mail-header-number h1)
4561                     gnus-newsgroup-scored))
4562          gnus-summary-default-score 0)
4563      (or (cdr (assq (mail-header-number h2)
4564                     gnus-newsgroup-scored))
4565          gnus-summary-default-score 0)))
4566
4567 (defun gnus-thread-sort-by-score (h1 h2)
4568   "Sort threads by root article score."
4569   (gnus-article-sort-by-score
4570    (gnus-thread-header h1) (gnus-thread-header h2)))
4571
4572 (defun gnus-thread-sort-by-total-score (h1 h2)
4573   "Sort threads by the sum of all scores in the thread.
4574 Unscored articles will be counted as having a score of zero."
4575   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4576
4577 (defun gnus-thread-total-score (thread)
4578   ;; This function find the total score of THREAD.
4579   (cond
4580    ((null thread)
4581     0)
4582    ((consp thread)
4583     (if (stringp (car thread))
4584         (apply gnus-thread-score-function 0
4585                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4586       (gnus-thread-total-score-1 thread)))
4587    (t
4588     (gnus-thread-total-score-1 (list thread)))))
4589
4590 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4591   "Sort threads such that the thread with the most recently arrived article comes first."
4592   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4593
4594 (defun gnus-thread-highest-number (thread)
4595   "Return the highest article number in THREAD."
4596   (apply 'max (mapcar (lambda (header)
4597                         (mail-header-number header))
4598                       (message-flatten-list thread))))
4599
4600 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4601   "Sort threads such that the thread with the most recently dated article comes first."
4602   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4603
4604 (defun gnus-thread-latest-date (thread)
4605   "Return the highest article date in THREAD."
4606   (let ((previous-time 0))
4607     (apply 'max
4608            (mapcar
4609             (lambda (header)
4610               (setq previous-time
4611                     (condition-case ()
4612                         (time-to-seconds (mail-header-parse-date
4613                                           (mail-header-date header)))
4614                       (error previous-time))))
4615             (sort
4616              (message-flatten-list thread)
4617              (lambda (h1 h2)
4618                (< (mail-header-number h1)
4619                   (mail-header-number h2))))))))
4620
4621 (defun gnus-thread-total-score-1 (root)
4622   ;; This function find the total score of the thread below ROOT.
4623   (setq root (car root))
4624   (apply gnus-thread-score-function
4625          (or (append
4626               (mapcar 'gnus-thread-total-score
4627                       (cdr (gnus-id-to-thread (mail-header-id root))))
4628               (when (> (mail-header-number root) 0)
4629                 (list (or (cdr (assq (mail-header-number root)
4630                                      gnus-newsgroup-scored))
4631                           gnus-summary-default-score 0))))
4632              (list gnus-summary-default-score)
4633              '(0))))
4634
4635 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4636 (defvar gnus-tmp-prev-subject nil)
4637 (defvar gnus-tmp-false-parent nil)
4638 (defvar gnus-tmp-root-expunged nil)
4639 (defvar gnus-tmp-dummy-line nil)
4640
4641 (eval-when-compile (defvar gnus-tmp-header))
4642 (defun gnus-extra-header (type &optional header)
4643   "Return the extra header of TYPE."
4644   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4645       ""))
4646
4647 (defvar gnus-tmp-thread-tree-header-string "")
4648
4649 (defcustom gnus-sum-thread-tree-root "> "
4650   "With %B spec, used for the root of a thread.
4651 If nil, use subject instead."
4652   :type '(radio (const :format "%v  " nil) (string :size 0))
4653   :group 'gnus-thread)
4654 (defcustom gnus-sum-thread-tree-false-root "> "
4655   "With %B spec, used for a false root of a thread.
4656 If nil, use subject instead."
4657   :type '(radio (const :format "%v  " nil) (string :size 0))
4658   :group 'gnus-thread)
4659 (defcustom gnus-sum-thread-tree-single-indent ""
4660   "With %B spec, used for a thread with just one message.
4661 If nil, use subject instead."
4662   :type '(radio (const :format "%v  " nil) (string :size 0))
4663   :group 'gnus-thread)
4664 (defcustom gnus-sum-thread-tree-vertical "| "
4665   "With %B spec, used for drawing a vertical line."
4666   :type 'string
4667   :group 'gnus-thread)
4668 (defcustom gnus-sum-thread-tree-indent "  "
4669   "With %B spec, used for indenting."
4670   :type 'string
4671   :group 'gnus-thread)
4672 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4673   "With %B spec, used for a leaf with brothers."
4674   :type 'string
4675   :group 'gnus-thread)
4676 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4677   "With %B spec, used for a leaf without brothers."
4678   :type 'string
4679   :group 'gnus-thread)
4680
4681 (defun gnus-summary-prepare-threads (threads)
4682   "Prepare summary buffer from THREADS and indentation LEVEL.
4683 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4684 or a straight list of headers."
4685   (gnus-message 7 "Generating summary...")
4686
4687   (setq gnus-newsgroup-threads threads)
4688   (beginning-of-line)
4689
4690   (let ((gnus-tmp-level 0)
4691         (default-score (or gnus-summary-default-score 0))
4692         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4693         (building-line-count gnus-summary-display-while-building)
4694         (building-count (integerp gnus-summary-display-while-building))
4695         thread number subject stack state gnus-tmp-gathered beg-match
4696         new-roots gnus-tmp-new-adopts thread-end simp-subject
4697         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4698         gnus-tmp-replied gnus-tmp-subject-or-nil
4699         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4700         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4701         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4702         tree-stack)
4703
4704     (setq gnus-tmp-prev-subject nil
4705           gnus-tmp-thread-tree-header-string "")
4706
4707     (if (vectorp (car threads))
4708         ;; If this is a straight (sic) list of headers, then a
4709         ;; threaded summary display isn't required, so we just create
4710         ;; an unthreaded one.
4711         (gnus-summary-prepare-unthreaded threads)
4712
4713       ;; Do the threaded display.
4714
4715       (if gnus-summary-display-while-building
4716           (switch-to-buffer (buffer-name)))
4717       (while (or threads stack gnus-tmp-new-adopts new-roots)
4718
4719         (if (and (= gnus-tmp-level 0)
4720                  (or (not stack)
4721                      (= (caar stack) 0))
4722                  (not gnus-tmp-false-parent)
4723                  (or gnus-tmp-new-adopts new-roots))
4724             (if gnus-tmp-new-adopts
4725                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4726                       thread (list (car gnus-tmp-new-adopts))
4727                       gnus-tmp-header (caar thread)
4728                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4729               (when new-roots
4730                 (setq thread (list (car new-roots))
4731                       gnus-tmp-header (caar thread)
4732                       new-roots (cdr new-roots))))
4733
4734           (if threads
4735               ;; If there are some threads, we do them before the
4736               ;; threads on the stack.
4737               (setq thread threads
4738                     gnus-tmp-header (caar thread))
4739             ;; There were no current threads, so we pop something off
4740             ;; the stack.
4741             (setq state (car stack)
4742                   gnus-tmp-level (car state)
4743                   tree-stack (cadr state)
4744                   thread (caddr state)
4745                   stack (cdr stack)
4746                   gnus-tmp-header (caar thread))))
4747
4748         (setq gnus-tmp-false-parent nil)
4749         (setq gnus-tmp-root-expunged nil)
4750         (setq thread-end nil)
4751
4752         (if (stringp gnus-tmp-header)
4753             ;; The header is a dummy root.
4754             (cond
4755              ((eq gnus-summary-make-false-root 'adopt)
4756               ;; We let the first article adopt the rest.
4757               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4758                                                (cddar thread)))
4759               (setq gnus-tmp-gathered
4760                     (nconc (mapcar
4761                             (lambda (h) (mail-header-number (car h)))
4762                             (cddar thread))
4763                            gnus-tmp-gathered))
4764               (setq thread (cons (list (caar thread)
4765                                        (cadar thread))
4766                                  (cdr thread)))
4767               (setq gnus-tmp-level -1
4768                     gnus-tmp-false-parent t))
4769              ((eq gnus-summary-make-false-root 'empty)
4770               ;; We print adopted articles with empty subject fields.
4771               (setq gnus-tmp-gathered
4772                     (nconc (mapcar
4773                             (lambda (h) (mail-header-number (car h)))
4774                             (cddar thread))
4775                            gnus-tmp-gathered))
4776               (setq gnus-tmp-level -1))
4777              ((eq gnus-summary-make-false-root 'dummy)
4778               ;; We remember that we probably want to output a dummy
4779               ;; root.
4780               (setq gnus-tmp-dummy-line gnus-tmp-header)
4781               (setq gnus-tmp-prev-subject gnus-tmp-header))
4782              (t
4783               ;; We do not make a root for the gathered
4784               ;; sub-threads at all.
4785               (setq gnus-tmp-level -1)))
4786
4787           (setq number (mail-header-number gnus-tmp-header)
4788                 subject (mail-header-subject gnus-tmp-header)
4789                 simp-subject (gnus-simplify-subject-fully subject))
4790
4791           (cond
4792            ;; If the thread has changed subject, we might want to make
4793            ;; this subthread into a root.
4794            ((and (null gnus-thread-ignore-subject)
4795                  (not (zerop gnus-tmp-level))
4796                  gnus-tmp-prev-subject
4797                  (not (string= gnus-tmp-prev-subject simp-subject)))
4798             (setq new-roots (nconc new-roots (list (car thread)))
4799                   thread-end t
4800                   gnus-tmp-header nil))
4801            ;; If the article lies outside the current limit,
4802            ;; then we do not display it.
4803            ((not (memq number gnus-newsgroup-limit))
4804             (setq gnus-tmp-gathered
4805                   (nconc (mapcar
4806                           (lambda (h) (mail-header-number (car h)))
4807                           (cdar thread))
4808                          gnus-tmp-gathered))
4809             (setq gnus-tmp-new-adopts (if (cdar thread)
4810                                           (append gnus-tmp-new-adopts
4811                                                   (cdar thread))
4812                                         gnus-tmp-new-adopts)
4813                   thread-end t
4814                   gnus-tmp-header nil)
4815             (when (zerop gnus-tmp-level)
4816               (setq gnus-tmp-root-expunged t)))
4817            ;; Perhaps this article is to be marked as read?
4818            ((and gnus-summary-mark-below
4819                  (< (or (cdr (assq number gnus-newsgroup-scored))
4820                         default-score)
4821                     gnus-summary-mark-below)
4822                  ;; Don't touch sparse articles.
4823                  (not (gnus-summary-article-sparse-p number))
4824                  (not (gnus-summary-article-ancient-p number)))
4825             (setq gnus-newsgroup-unreads
4826                   (delq number gnus-newsgroup-unreads))
4827             (if gnus-newsgroup-auto-expire
4828                 (setq gnus-newsgroup-expirable
4829                       (gnus-add-to-sorted-list
4830                        gnus-newsgroup-expirable number))
4831               (push (cons number gnus-low-score-mark)
4832                     gnus-newsgroup-reads))))
4833
4834           (when gnus-tmp-header
4835             ;; We may have an old dummy line to output before this
4836             ;; article.
4837             (when (and gnus-tmp-dummy-line
4838                        (gnus-subject-equal
4839                         gnus-tmp-dummy-line
4840                         (mail-header-subject gnus-tmp-header)))
4841               (gnus-summary-insert-dummy-line
4842                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4843               (setq gnus-tmp-dummy-line nil))
4844
4845             ;; Compute the mark.
4846             (setq gnus-tmp-unread (gnus-article-mark number))
4847
4848             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4849                                   gnus-tmp-header gnus-tmp-level)
4850                   gnus-newsgroup-data)
4851
4852             ;; Actually insert the line.
4853             (setq
4854              gnus-tmp-subject-or-nil
4855              (cond
4856               ((and gnus-thread-ignore-subject
4857                     gnus-tmp-prev-subject
4858                     (not (string= gnus-tmp-prev-subject simp-subject)))
4859                subject)
4860               ((zerop gnus-tmp-level)
4861                (if (and (eq gnus-summary-make-false-root 'empty)
4862                         (memq number gnus-tmp-gathered)
4863                         gnus-tmp-prev-subject
4864                         (string= gnus-tmp-prev-subject simp-subject))
4865                    gnus-summary-same-subject
4866                  subject))
4867               (t gnus-summary-same-subject)))
4868             (if (and (eq gnus-summary-make-false-root 'adopt)
4869                      (= gnus-tmp-level 1)
4870                      (memq number gnus-tmp-gathered))
4871                 (setq gnus-tmp-opening-bracket ?\<
4872                       gnus-tmp-closing-bracket ?\>)
4873               (setq gnus-tmp-opening-bracket ?\[
4874                     gnus-tmp-closing-bracket ?\]))
4875             (setq
4876              gnus-tmp-indentation
4877              (aref gnus-thread-indent-array gnus-tmp-level)
4878              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4879              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4880                                 gnus-summary-default-score 0)
4881              gnus-tmp-score-char
4882              (if (or (null gnus-summary-default-score)
4883                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4884                          gnus-summary-zcore-fuzz))
4885                  ?                      ;Whitespace
4886                (if (< gnus-tmp-score gnus-summary-default-score)
4887                    gnus-score-below-mark gnus-score-over-mark))
4888              gnus-tmp-replied
4889              (cond ((memq number gnus-newsgroup-processable)
4890                     gnus-process-mark)
4891                    ((memq number gnus-newsgroup-cached)
4892                     gnus-cached-mark)
4893                    ((memq number gnus-newsgroup-replied)
4894                     gnus-replied-mark)
4895                    ((memq number gnus-newsgroup-forwarded)
4896                     gnus-forwarded-mark)
4897                    ((memq number gnus-newsgroup-saved)
4898                     gnus-saved-mark)
4899                    ((memq number gnus-newsgroup-recent)
4900                     gnus-recent-mark)
4901                    ((memq number gnus-newsgroup-unseen)
4902                     gnus-unseen-mark)
4903                    (t gnus-no-mark))
4904              gnus-tmp-downloaded
4905              (cond ((memq number gnus-newsgroup-undownloaded)
4906                     gnus-undownloaded-mark)
4907                    (gnus-newsgroup-agentized
4908                     gnus-downloaded-mark)
4909                    (t
4910                     gnus-no-mark))
4911              gnus-tmp-from (mail-header-from gnus-tmp-header)
4912              gnus-tmp-name
4913              (cond
4914               ((string-match "<[^>]+> *$" gnus-tmp-from)
4915                (setq beg-match (match-beginning 0))
4916                (or (and (string-match "^\".+\"" gnus-tmp-from)
4917                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4918                    (substring gnus-tmp-from 0 beg-match)))
4919               ((string-match "(.+)" gnus-tmp-from)
4920                (substring gnus-tmp-from
4921                           (1+ (match-beginning 0)) (1- (match-end 0))))
4922               (t gnus-tmp-from))
4923
4924              ;; Do the %B string
4925              gnus-tmp-thread-tree-header-string
4926              (cond
4927               ((not gnus-show-threads) "")
4928               ((zerop gnus-tmp-level)
4929                (cond ((cdar thread)
4930                       (or gnus-sum-thread-tree-root subject))
4931                      (gnus-tmp-new-adopts
4932                       (or gnus-sum-thread-tree-false-root subject))
4933                      (t
4934                       (or gnus-sum-thread-tree-single-indent subject))))
4935               (t
4936                (concat (apply 'concat
4937                               (mapcar (lambda (item)
4938                                         (if (= item 1)
4939                                             gnus-sum-thread-tree-vertical
4940                                           gnus-sum-thread-tree-indent))
4941                                       (cdr (reverse tree-stack))))
4942                        (if (nth 1 thread)
4943                            gnus-sum-thread-tree-leaf-with-other
4944                          gnus-sum-thread-tree-single-leaf)))))
4945             (when (string= gnus-tmp-name "")
4946               (setq gnus-tmp-name gnus-tmp-from))
4947             (unless (numberp gnus-tmp-lines)
4948               (setq gnus-tmp-lines -1))
4949             (if (= gnus-tmp-lines -1)
4950                 (setq gnus-tmp-lines "?")
4951               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4952               (gnus-put-text-property
4953              (point)
4954              (progn (eval gnus-summary-line-format-spec) (point))
4955                'gnus-number number)
4956             (when gnus-visual-p
4957               (forward-line -1)
4958               (gnus-run-hooks 'gnus-summary-update-hook)
4959               (forward-line 1))
4960
4961             (setq gnus-tmp-prev-subject simp-subject)))
4962
4963         (when (nth 1 thread)
4964           (push (list (max 0 gnus-tmp-level)
4965                       (copy-sequence tree-stack)
4966                       (nthcdr 1 thread))
4967                 stack))
4968         (push (if (nth 1 thread) 1 0) tree-stack)
4969         (incf gnus-tmp-level)
4970         (setq threads (if thread-end nil (cdar thread)))
4971         (if gnus-summary-display-while-building
4972             (if building-count
4973                 (progn
4974                   ;; use a set frequency
4975                   (setq building-line-count (1- building-line-count))
4976                   (when (= building-line-count 0)
4977                     (sit-for 0)
4978                     (setq building-line-count
4979                           gnus-summary-display-while-building)))
4980               ;; always
4981               (sit-for 0)))
4982         (unless threads
4983           (setq gnus-tmp-level 0)))))
4984   (gnus-message 7 "Generating summary...done"))
4985
4986 (defun gnus-summary-prepare-unthreaded (headers)
4987   "Generate an unthreaded summary buffer based on HEADERS."
4988   (let (header number mark)
4989
4990     (beginning-of-line)
4991
4992     (while headers
4993       ;; We may have to root out some bad articles...
4994       (when (memq (setq number (mail-header-number
4995                                 (setq header (pop headers))))
4996                   gnus-newsgroup-limit)
4997         ;; Mark article as read when it has a low score.
4998         (when (and gnus-summary-mark-below
4999                    (< (or (cdr (assq number gnus-newsgroup-scored))
5000                           gnus-summary-default-score 0)
5001                       gnus-summary-mark-below)
5002                    (not (gnus-summary-article-ancient-p number)))
5003           (setq gnus-newsgroup-unreads
5004                 (delq number gnus-newsgroup-unreads))
5005           (if gnus-newsgroup-auto-expire
5006               (push number gnus-newsgroup-expirable)
5007             (push (cons number gnus-low-score-mark)
5008                   gnus-newsgroup-reads)))
5009
5010         (setq mark (gnus-article-mark number))
5011         (push (gnus-data-make number mark (1+ (point)) header 0)
5012               gnus-newsgroup-data)
5013         (gnus-summary-insert-line
5014          header 0 number
5015          (memq number gnus-newsgroup-undownloaded)
5016          mark (memq number gnus-newsgroup-replied)
5017          (memq number gnus-newsgroup-expirable)
5018          (mail-header-subject header) nil
5019          (cdr (assq number gnus-newsgroup-scored))
5020          (memq number gnus-newsgroup-processable))))))
5021
5022 (defun gnus-summary-remove-list-identifiers ()
5023   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5024   (let ((regexp (if (consp gnus-list-identifiers)
5025                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5026                   gnus-list-identifiers))
5027         changed subject)
5028     (when regexp
5029       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5030       (dolist (header gnus-newsgroup-headers)
5031         (setq subject (mail-header-subject header)
5032               changed nil)
5033         (while (string-match regexp subject)
5034           (setq subject
5035                 (concat (substring subject 0 (match-beginning 1))
5036                         (substring subject (match-end 0)))
5037                 changed t))
5038         (when changed
5039           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5040             (setq subject
5041                   (concat (substring subject 0 (match-beginning 1))
5042                           (substring subject (match-end 1)))))
5043           (mail-header-set-subject header subject))))))
5044
5045 (defun gnus-fetch-headers (articles)
5046   "Fetch headers of ARTICLES."
5047   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5048     (gnus-message 5 "Fetching headers for %s..." name)
5049     (prog1
5050         (if (eq 'nov
5051                 (setq gnus-headers-retrieved-by
5052                       (gnus-retrieve-headers
5053                        articles gnus-newsgroup-name
5054                        ;; We might want to fetch old headers, but
5055                        ;; not if there is only 1 article.
5056                        (and (or (and
5057                                  (not (eq gnus-fetch-old-headers 'some))
5058                                  (not (numberp gnus-fetch-old-headers)))
5059                                 (> (length articles) 1))
5060                             gnus-fetch-old-headers))))
5061             (gnus-get-newsgroup-headers-xover
5062              articles nil nil gnus-newsgroup-name t)
5063           (gnus-get-newsgroup-headers))
5064       (gnus-message 5 "Fetching headers for %s...done" name))))
5065
5066 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5067   "Select newsgroup GROUP.
5068 If READ-ALL is non-nil, all articles in the group are selected.
5069 If SELECT-ARTICLES, only select those articles from GROUP."
5070   (let* ((entry (gnus-group-entry group))
5071          ;;!!! Dirty hack; should be removed.
5072          (gnus-summary-ignore-duplicates
5073           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5074               t
5075             gnus-summary-ignore-duplicates))
5076          (info (nth 2 entry))
5077          articles fetched-articles cached)
5078
5079     (unless (gnus-check-server
5080              (set (make-local-variable 'gnus-current-select-method)
5081                   (gnus-find-method-for-group group)))
5082       (error "Couldn't open server"))
5083
5084     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5085         (gnus-activate-group group)     ; Or we can activate it...
5086         (progn                          ; Or we bug out.
5087           (when (equal major-mode 'gnus-summary-mode)
5088             (gnus-kill-buffer (current-buffer)))
5089           (error "Couldn't activate group %s: %s"
5090                  group (gnus-status-message group))))
5091
5092     (unless (gnus-request-group group t)
5093       (when (equal major-mode 'gnus-summary-mode)
5094         (gnus-kill-buffer (current-buffer)))
5095       (error "Couldn't request group %s: %s"
5096              group (gnus-status-message group)))
5097
5098     (when gnus-agent
5099       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5100       
5101       (setq gnus-summary-use-undownloaded-faces
5102             (gnus-agent-find-parameter
5103              group
5104              'agent-enable-undownloaded-faces)))
5105
5106     (setq gnus-newsgroup-name group
5107           gnus-newsgroup-unselected nil
5108           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5109
5110     (let ((display (gnus-group-find-parameter group 'display)))
5111       (setq gnus-newsgroup-display
5112             (cond
5113              ((not (zerop (or (car-safe read-all) 0)))
5114               ;; The user entered the group with C-u SPC/RET, let's show
5115               ;; all articles.
5116               'gnus-not-ignore)
5117              ((eq display 'all)
5118               'gnus-not-ignore)
5119              ((arrayp display)
5120               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5121              ((numberp display)
5122               ;; The following is probably the "correct" solution, but
5123               ;; it makes Gnus fetch all headers and then limit the
5124               ;; articles (which is slow), so instead we hack the
5125               ;; select-articles parameter instead. -- Simon Josefsson
5126               ;; <jas@kth.se>
5127               ;;
5128               ;; (gnus-byte-compile
5129               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5130               ;;                         display)))))
5131               (setq select-articles
5132                     (gnus-uncompress-range
5133                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5134                              (if (> tmp 0)
5135                                  tmp
5136                                1))
5137                            (cdr (gnus-active group)))))
5138               nil)
5139              (t
5140               nil))))
5141
5142     (gnus-summary-setup-default-charset)
5143
5144     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5145     (when (gnus-virtual-group-p group)
5146       (setq cached gnus-newsgroup-cached))
5147
5148     (setq gnus-newsgroup-unreads
5149           (gnus-sorted-ndifference
5150            (gnus-sorted-ndifference gnus-newsgroup-unreads
5151                                     gnus-newsgroup-marked)
5152            gnus-newsgroup-dormant))
5153
5154     (setq gnus-newsgroup-processable nil)
5155
5156     (gnus-update-read-articles group gnus-newsgroup-unreads)
5157
5158     ;; Adjust and set lists of article marks.
5159     (when info
5160       (gnus-adjust-marked-articles info))
5161     (if (setq articles select-articles)
5162         (setq gnus-newsgroup-unselected
5163               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5164       (setq articles (gnus-articles-to-read group read-all)))
5165
5166     (cond
5167      ((null articles)
5168       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5169       'quit)
5170      ((eq articles 0) nil)
5171      (t
5172       ;; Init the dependencies hash table.
5173       (setq gnus-newsgroup-dependencies
5174             (gnus-make-hashtable (length articles)))
5175       (gnus-set-global-variables)
5176       ;; Retrieve the headers and read them in.
5177
5178       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5179
5180       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5181       (when cached
5182         (setq gnus-newsgroup-cached cached))
5183
5184       ;; Suppress duplicates?
5185       (when gnus-suppress-duplicates
5186         (gnus-dup-suppress-articles))
5187
5188       ;; Set the initial limit.
5189       (setq gnus-newsgroup-limit (copy-sequence articles))
5190       ;; Remove canceled articles from the list of unread articles.
5191       (setq fetched-articles
5192             (mapcar (lambda (headers) (mail-header-number headers))
5193                     gnus-newsgroup-headers))
5194       (setq gnus-newsgroup-articles fetched-articles)
5195       (setq gnus-newsgroup-unreads
5196             (gnus-sorted-nintersection
5197              gnus-newsgroup-unreads fetched-articles))
5198       (gnus-compute-unseen-list)
5199
5200       ;; Removed marked articles that do not exist.
5201       (gnus-update-missing-marks
5202        (gnus-sorted-difference articles fetched-articles))
5203       ;; We might want to build some more threads first.
5204       (when (and gnus-fetch-old-headers
5205                  (eq gnus-headers-retrieved-by 'nov))
5206         (if (eq gnus-fetch-old-headers 'invisible)
5207             (gnus-build-all-threads)
5208           (gnus-build-old-threads)))
5209       ;; Let the Gnus agent mark articles as read.
5210       (when gnus-agent
5211         (gnus-agent-get-undownloaded-list))
5212       ;; Remove list identifiers from subject
5213       (when gnus-list-identifiers
5214         (gnus-summary-remove-list-identifiers))
5215       ;; Check whether auto-expire is to be done in this group.
5216       (setq gnus-newsgroup-auto-expire
5217             (gnus-group-auto-expirable-p group))
5218       ;; Set up the article buffer now, if necessary.
5219       (unless gnus-single-article-buffer
5220         (gnus-article-setup-buffer))
5221       ;; First and last article in this newsgroup.
5222       (when gnus-newsgroup-headers
5223         (setq gnus-newsgroup-begin
5224               (mail-header-number (car gnus-newsgroup-headers))
5225               gnus-newsgroup-end
5226               (mail-header-number
5227                (gnus-last-element gnus-newsgroup-headers))))
5228       ;; GROUP is successfully selected.
5229       (or gnus-newsgroup-headers t)))))
5230
5231 (defun gnus-compute-unseen-list ()
5232   ;; The `seen' marks are treated specially.
5233   (if (not gnus-newsgroup-seen)
5234       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5235     (setq gnus-newsgroup-unseen
5236           (gnus-inverse-list-range-intersection
5237            gnus-newsgroup-articles gnus-newsgroup-seen))))
5238
5239 (defun gnus-summary-display-make-predicate (display)
5240   (require 'gnus-agent)
5241   (when (= (length display) 1)
5242     (setq display (car display)))
5243   (unless gnus-summary-display-cache
5244     (dolist (elem (append '((unread . unread)
5245                             (read . read)
5246                             (unseen . unseen))
5247                           gnus-article-mark-lists))
5248       (push (cons (cdr elem)
5249                   (gnus-byte-compile
5250                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5251             gnus-summary-display-cache)))
5252   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5253         (gnus-category-predicate-cache gnus-summary-display-cache))
5254     (gnus-get-predicate display)))
5255
5256 ;; Uses the dynamically bound `number' variable.
5257 (eval-when-compile
5258   (defvar number))
5259 (defun gnus-article-marked-p (type &optional article)
5260   (let ((article (or article number)))
5261     (cond
5262      ((eq type 'tick)
5263       (memq article gnus-newsgroup-marked))
5264      ((eq type 'spam)
5265       (memq article gnus-newsgroup-spam-marked))
5266      ((eq type 'unsend)
5267       (memq article gnus-newsgroup-unsendable))
5268      ((eq type 'undownload)
5269       (memq article gnus-newsgroup-undownloaded))
5270      ((eq type 'download)
5271       (memq article gnus-newsgroup-downloadable))
5272      ((eq type 'unread)
5273       (memq article gnus-newsgroup-unreads))
5274      ((eq type 'read)
5275       (memq article gnus-newsgroup-reads))
5276      ((eq type 'dormant)
5277       (memq article gnus-newsgroup-dormant) )
5278      ((eq type 'expire)
5279       (memq article gnus-newsgroup-expirable))
5280      ((eq type 'reply)
5281       (memq article gnus-newsgroup-replied))
5282      ((eq type 'killed)
5283       (memq article gnus-newsgroup-killed))
5284      ((eq type 'bookmark)
5285       (assq article gnus-newsgroup-bookmarks))
5286      ((eq type 'score)
5287       (assq article gnus-newsgroup-scored))
5288      ((eq type 'save)
5289       (memq article gnus-newsgroup-saved))
5290      ((eq type 'cache)
5291       (memq article gnus-newsgroup-cached))
5292      ((eq type 'forward)
5293       (memq article gnus-newsgroup-forwarded))
5294      ((eq type 'seen)
5295       (not (memq article gnus-newsgroup-unseen)))
5296      ((eq type 'recent)
5297       (memq article gnus-newsgroup-recent))
5298      (t t))))
5299
5300 (defun gnus-articles-to-read (group &optional read-all)
5301   "Find out what articles the user wants to read."
5302   (let* ((display (gnus-group-find-parameter group 'display))
5303          (articles
5304           ;; Select all articles if `read-all' is non-nil, or if there
5305           ;; are no unread articles.
5306           (if (or read-all
5307                   (and (zerop (length gnus-newsgroup-marked))
5308                        (zerop (length gnus-newsgroup-unreads)))
5309                   ;; Fetch all if the predicate is non-nil.
5310                   gnus-newsgroup-display)
5311               ;; We want to select the headers for all the articles in
5312               ;; the group, so we select either all the active
5313               ;; articles in the group, or (if that's nil), the
5314               ;; articles in the cache.
5315               (or
5316                (gnus-uncompress-range (gnus-active group))
5317                (gnus-cache-articles-in-group group))
5318             ;; Select only the "normal" subset of articles.
5319             (gnus-sorted-nunion
5320              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5321              gnus-newsgroup-unreads)))
5322          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5323          (scored (length scored-list))
5324          (number (length articles))
5325          (marked (+ (length gnus-newsgroup-marked)
5326                     (length gnus-newsgroup-dormant)))
5327          (select
5328           (cond
5329            ((numberp read-all)
5330             read-all)
5331            ((numberp gnus-newsgroup-display)
5332             gnus-newsgroup-display)
5333            (t
5334             (condition-case ()
5335                 (cond
5336                  ((and (or (<= scored marked) (= scored number))
5337                        (numberp gnus-large-newsgroup)
5338                        (> number gnus-large-newsgroup))
5339                   (let* ((cursor-in-echo-area nil)
5340                          (initial (gnus-parameter-large-newsgroup-initial
5341                                    gnus-newsgroup-name))
5342                          (input
5343                           (read-string
5344                            (format
5345                             "How many articles from %s (%s %d): "
5346                             (gnus-limit-string
5347                              (gnus-group-decoded-name gnus-newsgroup-name)
5348                              35)
5349                             (if initial "max" "default")
5350                             number)
5351                            (if initial
5352                                (cons (number-to-string initial)
5353                                      0)))))
5354                     (if (string-match "^[ \t]*$" input) number input)))
5355                  ((and (> scored marked) (< scored number)
5356                        (> (- scored number) 20))
5357                   (let ((input
5358                          (read-string
5359                           (format "%s %s (%d scored, %d total): "
5360                                   "How many articles from"
5361                                   (gnus-group-decoded-name group)
5362                                   scored number))))
5363                     (if (string-match "^[ \t]*$" input)
5364                         number input)))
5365                  (t number))
5366               (quit
5367                (message "Quit getting the articles to read")
5368                nil))))))
5369     (setq select (if (stringp select) (string-to-number select) select))
5370     (if (or (null select) (zerop select))
5371         select
5372       (if (and (not (zerop scored)) (<= (abs select) scored))
5373           (progn
5374             (setq articles (sort scored-list '<))
5375             (setq number (length articles)))
5376         (setq articles (copy-sequence articles)))
5377
5378       (when (< (abs select) number)
5379         (if (< select 0)
5380             ;; Select the N oldest articles.
5381             (setcdr (nthcdr (1- (abs select)) articles) nil)
5382           ;; Select the N most recent articles.
5383           (setq articles (nthcdr (- number select) articles))))
5384       (setq gnus-newsgroup-unselected
5385             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5386       (when gnus-alter-articles-to-read-function
5387         (setq articles
5388               (sort
5389                (funcall gnus-alter-articles-to-read-function
5390                         gnus-newsgroup-name articles)
5391                '<)))
5392       articles)))
5393
5394 (defun gnus-killed-articles (killed articles)
5395   (let (out)
5396     (while articles
5397       (when (inline (gnus-member-of-range (car articles) killed))
5398         (push (car articles) out))
5399       (setq articles (cdr articles)))
5400     out))
5401
5402 (defun gnus-uncompress-marks (marks)
5403   "Uncompress the mark ranges in MARKS."
5404   (let ((uncompressed '(score bookmark))
5405         out)
5406     (while marks
5407       (if (memq (caar marks) uncompressed)
5408           (push (car marks) out)
5409         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5410       (setq marks (cdr marks)))
5411     out))
5412
5413 (defun gnus-article-mark-to-type (mark)
5414   "Return the type of MARK."
5415   (or (cadr (assq mark gnus-article-special-mark-lists))
5416       'list))
5417
5418 (defun gnus-article-unpropagatable-p (mark)
5419   "Return whether MARK should be propagated to back end."
5420   (memq mark gnus-article-unpropagated-mark-lists))
5421
5422 (defun gnus-adjust-marked-articles (info)
5423   "Set all article lists and remove all marks that are no longer valid."
5424   (let* ((marked-lists (gnus-info-marks info))
5425          (active (gnus-active (gnus-info-group info)))
5426          (min (car active))
5427          (max (cdr active))
5428          (types gnus-article-mark-lists)
5429          marks var articles article mark mark-type
5430          bgn end)
5431
5432     (dolist (marks marked-lists)
5433       (setq mark (car marks)
5434             mark-type (gnus-article-mark-to-type mark)
5435             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5436
5437       ;; We set the variable according to the type of the marks list,
5438       ;; and then adjust the marks to a subset of the active articles.
5439       (cond
5440        ;; Adjust "simple" lists - compressed yet unsorted
5441        ((eq mark-type 'list)
5442         ;; Simultaneously uncompress and clip to active range
5443         ;; See gnus-uncompress-range for a description of possible marks
5444         (let (l lh)
5445           (if (not (cadr marks))
5446               (set var nil)
5447             (setq articles (if (numberp (cddr marks))
5448                                (list (cdr marks))
5449                              (cdr marks))
5450                   lh (cons nil nil)
5451                   l lh)
5452
5453             (while (setq article (pop articles))
5454               (cond ((consp article)
5455                      (setq bgn (max (car article) min)
5456                            end (min (cdr article) max))
5457                      (while (<= bgn end)
5458                        (setq l (setcdr l (cons bgn nil))
5459                              bgn (1+ bgn))))
5460                     ((and (<= min article)
5461                           (>= max article))
5462                      (setq l (setcdr l (cons article nil))))))
5463             (set var (cdr lh)))))
5464        ;; Adjust assocs.
5465        ((eq mark-type 'tuple)
5466         (set var (setq articles (cdr marks)))
5467         (when (not (listp (cdr (symbol-value var))))
5468           (set var (list (symbol-value var))))
5469         (when (not (listp (cdr articles)))
5470           (setq articles (list articles)))
5471         (while articles
5472           (when (or (not (consp (setq article (pop articles))))
5473                     (< (car article) min)
5474                     (> (car article) max))
5475             (set var (delq article (symbol-value var))))))
5476        ;; Adjust ranges (sloppily).
5477        ((eq mark-type 'range)
5478         (cond
5479          ((eq mark 'seen)
5480           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5481           ;; It should be (seen (NUM1 . NUM2)).
5482           (when (numberp (cddr marks))
5483             (setcdr marks (list (cdr marks))))
5484           (setq articles (cdr marks))
5485           (while (and articles
5486                       (or (and (consp (car articles))
5487                                (> min (cdar articles)))
5488                           (and (numberp (car articles))
5489                                (> min (car articles)))))
5490             (pop articles))
5491           (set var articles))))))))
5492
5493 (defun gnus-update-missing-marks (missing)
5494   "Go through the list of MISSING articles and remove them from the mark lists."
5495   (when missing
5496     (let (var m)
5497       ;; Go through all types.
5498       (dolist (elem gnus-article-mark-lists)
5499         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5500           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5501           (when (symbol-value var)
5502             ;; This list has articles.  So we delete all missing
5503             ;; articles from it.
5504             (setq m missing)
5505             (while m
5506               (set var (delq (pop m) (symbol-value var))))))))))
5507
5508 (defun gnus-update-marks ()
5509   "Enter the various lists of marked articles into the newsgroup info list."
5510   (let ((types gnus-article-mark-lists)
5511         (info (gnus-get-info gnus-newsgroup-name))
5512         type list newmarked symbol delta-marks)
5513     (when info
5514       ;; Add all marks lists to the list of marks lists.
5515       (while (setq type (pop types))
5516         (setq list (symbol-value
5517                     (setq symbol
5518                           (intern (format "gnus-newsgroup-%s" (car type))))))
5519
5520         (when list
5521           ;; Get rid of the entries of the articles that have the
5522           ;; default score.
5523           (when (and (eq (cdr type) 'score)
5524                      gnus-save-score
5525                      list)
5526             (let* ((arts list)
5527                    (prev (cons nil list))
5528                    (all prev))
5529               (while arts
5530                 (if (or (not (consp (car arts)))
5531                         (= (cdar arts) gnus-summary-default-score))
5532                     (setcdr prev (cdr arts))
5533                   (setq prev arts))
5534                 (setq arts (cdr arts)))
5535               (setq list (cdr all)))))
5536
5537         (when (eq (cdr type) 'seen)
5538           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5539
5540         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5541           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5542
5543         (when (and (gnus-check-backend-function
5544                     'request-set-mark gnus-newsgroup-name)
5545                    (not (gnus-article-unpropagatable-p (cdr type))))
5546           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5547                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5548                  (add (gnus-remove-from-range
5549                        (gnus-copy-sequence list) old)))
5550             (when add
5551               (push (list add 'add (list (cdr type))) delta-marks))
5552             (when del
5553               (push (list del 'del (list (cdr type))) delta-marks))))
5554
5555         (when list
5556           (push (cons (cdr type) list) newmarked)))
5557
5558       (when delta-marks
5559         (unless (gnus-check-group gnus-newsgroup-name)
5560           (error "Can't open server for %s" gnus-newsgroup-name))
5561         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5562
5563       ;; Enter these new marks into the info of the group.
5564       (if (nthcdr 3 info)
5565           (setcar (nthcdr 3 info) newmarked)
5566         ;; Add the marks lists to the end of the info.
5567         (when newmarked
5568           (setcdr (nthcdr 2 info) (list newmarked))))
5569
5570       ;; Cut off the end of the info if there's nothing else there.
5571       (let ((i 5))
5572         (while (and (> i 2)
5573                     (not (nth i info)))
5574           (when (nthcdr (decf i) info)
5575             (setcdr (nthcdr i info) nil)))))))
5576
5577 (defun gnus-set-mode-line (where)
5578   "Set the mode line of the article or summary buffers.
5579 If WHERE is `summary', the summary mode line format will be used."
5580   ;; Is this mode line one we keep updated?
5581   (when (and (memq where gnus-updated-mode-lines)
5582              (symbol-value
5583               (intern (format "gnus-%s-mode-line-format-spec" where))))
5584     (let (mode-string)
5585       (save-excursion
5586         ;; We evaluate this in the summary buffer since these
5587         ;; variables are buffer-local to that buffer.
5588         (set-buffer gnus-summary-buffer)
5589        ;; We bind all these variables that are used in the `eval' form
5590         ;; below.
5591         (let* ((mformat (symbol-value
5592                          (intern
5593                           (format "gnus-%s-mode-line-format-spec" where))))
5594                (gnus-tmp-group-name (gnus-group-decoded-name
5595                                      gnus-newsgroup-name))
5596                (gnus-tmp-article-number (or gnus-current-article 0))
5597                (gnus-tmp-unread gnus-newsgroup-unreads)
5598                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5599                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5600                (gnus-tmp-unread-and-unselected
5601                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5602                             (zerop gnus-tmp-unselected))
5603                        "")
5604                       ((zerop gnus-tmp-unselected)
5605                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5606                       (t (format "{%d(+%d) more}"
5607                                  gnus-tmp-unread-and-unticked
5608                                  gnus-tmp-unselected))))
5609                (gnus-tmp-subject
5610                 (if (and gnus-current-headers
5611                          (vectorp gnus-current-headers))
5612                     (gnus-mode-string-quote
5613                      (mail-header-subject gnus-current-headers))
5614                   ""))
5615                bufname-length max-len
5616                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5617           (setq mode-string (eval mformat))
5618           (setq bufname-length (if (string-match "%b" mode-string)
5619                                    (- (length
5620                                        (buffer-name
5621                                         (if (eq where 'summary)
5622                                             nil
5623                                           (get-buffer gnus-article-buffer))))
5624                                       2)
5625                                  0))
5626           (setq max-len (max 4 (if gnus-mode-non-string-length
5627                                    (- (window-width)
5628                                       gnus-mode-non-string-length
5629                                       bufname-length)
5630                                  (length mode-string))))
5631           ;; We might have to chop a bit of the string off...
5632           (when (> (length mode-string) max-len)
5633             (setq mode-string
5634                   (concat (truncate-string-to-width mode-string (- max-len 3))
5635                           "...")))
5636           ;; Pad the mode string a bit.
5637           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5638       ;; Update the mode line.
5639       (setq mode-line-buffer-identification
5640             (gnus-mode-line-buffer-identification (list mode-string)))
5641       (set-buffer-modified-p t))))
5642
5643 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5644   "Go through the HEADERS list and add all Xrefs to a hash table.
5645 The resulting hash table is returned, or nil if no Xrefs were found."
5646   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5647          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5648          (xref-hashtb (gnus-make-hashtable))
5649          start group entry number xrefs header)
5650     (while headers
5651       (setq header (pop headers))
5652       (when (and (setq xrefs (mail-header-xref header))
5653                  (not (memq (setq number (mail-header-number header))
5654                             unreads)))
5655         (setq start 0)
5656         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5657           (setq start (match-end 0))
5658           (setq group (if prefix
5659                           (concat prefix (substring xrefs (match-beginning 1)
5660                                                     (match-end 1)))
5661                         (substring xrefs (match-beginning 1) (match-end 1))))
5662           (setq number
5663                 (string-to-int (substring xrefs (match-beginning 2)
5664                                           (match-end 2))))
5665           (if (setq entry (gnus-gethash group xref-hashtb))
5666               (setcdr entry (cons number (cdr entry)))
5667             (gnus-sethash group (cons number nil) xref-hashtb)))))
5668     (and start xref-hashtb)))
5669
5670 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5671   "Look through all the headers and mark the Xrefs as read."
5672   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5673         name info xref-hashtb idlist method nth4)
5674     (save-excursion
5675       (set-buffer gnus-group-buffer)
5676       (when (setq xref-hashtb
5677                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5678         (mapatoms
5679          (lambda (group)
5680            (unless (string= from-newsgroup (setq name (symbol-name group)))
5681              (setq idlist (symbol-value group))
5682              ;; Dead groups are not updated.
5683              (and (prog1
5684                       (setq info (gnus-get-info name))
5685                     (when (stringp (setq nth4 (gnus-info-method info)))
5686                       (setq nth4 (gnus-server-to-method nth4))))
5687                   ;; Only do the xrefs if the group has the same
5688                   ;; select method as the group we have just read.
5689                   (or (gnus-methods-equal-p
5690                        nth4 (gnus-find-method-for-group from-newsgroup))
5691                       virtual
5692                       (equal nth4 (setq method (gnus-find-method-for-group
5693                                                 from-newsgroup)))
5694                       (and (equal (car nth4) (car method))
5695                            (equal (nth 1 nth4) (nth 1 method))))
5696                   gnus-use-cross-reference
5697                   (or (not (eq gnus-use-cross-reference t))
5698                       virtual
5699                       ;; Only do cross-references on subscribed
5700                       ;; groups, if that is what is wanted.
5701                       (<= (gnus-info-level info) gnus-level-subscribed))
5702                   (gnus-group-make-articles-read name idlist))))
5703          xref-hashtb)))))
5704
5705 (defun gnus-compute-read-articles (group articles)
5706   (let* ((entry (gnus-group-entry group))
5707          (info (nth 2 entry))
5708          (active (gnus-active group))
5709          ninfo)
5710     (when entry
5711       ;; First peel off all invalid article numbers.
5712       (when active
5713         (let ((ids articles)
5714               id first)
5715           (while (setq id (pop ids))
5716             (when (and first (> id (cdr active)))
5717               ;; We'll end up in this situation in one particular
5718               ;; obscure situation.  If you re-scan a group and get
5719               ;; a new article that is cross-posted to a different
5720               ;; group that has not been re-scanned, you might get
5721               ;; crossposted article that has a higher number than
5722               ;; Gnus believes possible.  So we re-activate this
5723               ;; group as well.  This might mean doing the
5724               ;; crossposting thingy will *increase* the number
5725               ;; of articles in some groups.  Tsk, tsk.
5726               (setq active (or (gnus-activate-group group) active)))
5727             (when (or (> id (cdr active))
5728                       (< id (car active)))
5729               (setq articles (delq id articles))))))
5730       ;; If the read list is nil, we init it.
5731       (if (and active
5732                (null (gnus-info-read info))
5733                (> (car active) 1))
5734           (setq ninfo (cons 1 (1- (car active))))
5735         (setq ninfo (gnus-info-read info)))
5736       ;; Then we add the read articles to the range.
5737       (gnus-add-to-range
5738        ninfo (setq articles (sort articles '<))))))
5739
5740 (defun gnus-group-make-articles-read (group articles)
5741   "Update the info of GROUP to say that ARTICLES are read."
5742   (let* ((num 0)
5743          (entry (gnus-group-entry group))
5744          (info (nth 2 entry))
5745          (active (gnus-active group))
5746          range)
5747     (when entry
5748       (setq range (gnus-compute-read-articles group articles))
5749       (with-current-buffer gnus-group-buffer
5750         (gnus-undo-register
5751           `(progn
5752              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5753              (gnus-info-set-read ',info ',(gnus-info-read info))
5754              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5755              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5756              (gnus-group-update-group ,group t))))
5757       ;; Add the read articles to the range.
5758       (gnus-info-set-read info range)
5759       (gnus-request-set-mark group (list (list range 'add '(read))))
5760       ;; Then we have to re-compute how many unread
5761       ;; articles there are in this group.
5762       (when active
5763         (cond
5764          ((not range)
5765           (setq num (- (1+ (cdr active)) (car active))))
5766          ((not (listp (cdr range)))
5767           (setq num (- (cdr active) (- (1+ (cdr range))
5768                                        (car range)))))
5769          (t
5770           (while range
5771             (if (numberp (car range))
5772                 (setq num (1+ num))
5773               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5774             (setq range (cdr range)))
5775           (setq num (- (cdr active) num))))
5776         ;; Update the number of unread articles.
5777         (setcar entry num)
5778         ;; Update the group buffer.
5779         (unless (gnus-ephemeral-group-p group)
5780           (gnus-group-update-group group t))))))
5781
5782 (defvar gnus-newsgroup-none-id 0)
5783
5784 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5785   (let ((cur nntp-server-buffer)
5786         (dependencies
5787          (or dependencies
5788              (with-current-buffer gnus-summary-buffer
5789                gnus-newsgroup-dependencies)))
5790         headers id end ref
5791         (mail-parse-charset gnus-newsgroup-charset)
5792         (mail-parse-ignored-charsets
5793          (save-excursion (condition-case nil
5794                              (set-buffer gnus-summary-buffer)
5795                            (error))
5796                          gnus-newsgroup-ignored-charsets)))
5797     (save-excursion
5798       (set-buffer nntp-server-buffer)
5799       ;; Translate all TAB characters into SPACE characters.
5800       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5801       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5802       (ietf-drums-unfold-fws)
5803       (gnus-run-hooks 'gnus-parse-headers-hook)
5804       (let ((case-fold-search t)
5805             in-reply-to header p lines chars)
5806         (goto-char (point-min))
5807         ;; Search to the beginning of the next header.  Error messages
5808         ;; do not begin with 2 or 3.
5809         (while (re-search-forward "^[23][0-9]+ " nil t)
5810           (setq id nil
5811                 ref nil)
5812           ;; This implementation of this function, with nine
5813           ;; search-forwards instead of the one re-search-forward and
5814           ;; a case (which basically was the old function) is actually
5815           ;; about twice as fast, even though it looks messier.  You
5816           ;; can't have everything, I guess.  Speed and elegance
5817           ;; doesn't always go hand in hand.
5818           (setq
5819            header
5820            (vector
5821             ;; Number.
5822             (prog1
5823                 (read cur)
5824               (end-of-line)
5825               (setq p (point))
5826               (narrow-to-region (point)
5827                                 (or (and (search-forward "\n.\n" nil t)
5828                                          (- (point) 2))
5829                                     (point))))
5830             ;; Subject.
5831             (progn
5832               (goto-char p)
5833               (if (search-forward "\nsubject:" nil t)
5834                   (funcall gnus-decode-encoded-word-function
5835                            (nnheader-header-value))
5836                 "(none)"))
5837             ;; From.
5838             (progn
5839               (goto-char p)
5840               (if (search-forward "\nfrom:" nil t)
5841                   (funcall gnus-decode-encoded-word-function
5842                            (nnheader-header-value))
5843                 "(nobody)"))
5844             ;; Date.
5845             (progn
5846               (goto-char p)
5847               (if (search-forward "\ndate:" nil t)
5848                   (nnheader-header-value) ""))
5849             ;; Message-ID.
5850             (progn
5851               (goto-char p)
5852               (setq id (if (re-search-forward
5853                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5854                            ;; We do it this way to make sure the Message-ID
5855                            ;; is (somewhat) syntactically valid.
5856                            (buffer-substring (match-beginning 1)
5857                                              (match-end 1))
5858                          ;; If there was no message-id, we just fake one
5859                          ;; to make subsequent routines simpler.
5860                          (nnheader-generate-fake-message-id))))
5861             ;; References.
5862             (progn
5863               (goto-char p)
5864               (if (search-forward "\nreferences:" nil t)
5865                   (progn
5866                     (setq end (point))
5867                     (prog1
5868                         (nnheader-header-value)
5869                       (setq ref
5870                             (buffer-substring
5871                              (progn
5872                                (end-of-line)
5873                                (search-backward ">" end t)
5874                                (1+ (point)))
5875                              (progn
5876                                (search-backward "<" end t)
5877                                (point))))))
5878                 ;; Get the references from the in-reply-to header if there
5879                 ;; were no references and the in-reply-to header looks
5880                 ;; promising.
5881                 (if (and (search-forward "\nin-reply-to:" nil t)
5882                          (setq in-reply-to (nnheader-header-value))
5883                          (string-match "<[^>]+>" in-reply-to))
5884                     (let (ref2)
5885                       (setq ref (substring in-reply-to (match-beginning 0)
5886                                            (match-end 0)))
5887                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5888                         (setq ref2 (substring in-reply-to (match-beginning 0)
5889                                               (match-end 0)))
5890                         (when (> (length ref2) (length ref))
5891                           (setq ref ref2)))
5892                       ref)
5893                   (setq ref nil))))
5894             ;; Chars.
5895             (progn
5896               (goto-char p)
5897               (if (search-forward "\nchars: " nil t)
5898                   (if (numberp (setq chars (ignore-errors (read cur))))
5899                       chars -1)
5900                 -1))
5901             ;; Lines.
5902             (progn
5903               (goto-char p)
5904               (if (search-forward "\nlines: " nil t)
5905                   (if (numberp (setq lines (ignore-errors (read cur))))
5906                       lines -1)
5907                 -1))
5908             ;; Xref.
5909             (progn
5910               (goto-char p)
5911               (and (search-forward "\nxref:" nil t)
5912                    (nnheader-header-value)))
5913             ;; Extra.
5914             (when gnus-extra-headers
5915               (let ((extra gnus-extra-headers)
5916                     out)
5917                 (while extra
5918                   (goto-char p)
5919                   (when (search-forward
5920                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5921                     (push (cons (car extra) (nnheader-header-value))
5922                           out))
5923                   (pop extra))
5924                 out))))
5925           (when (equal id ref)
5926             (setq ref nil))
5927
5928           (when gnus-alter-header-function
5929             (funcall gnus-alter-header-function header)
5930             (setq id (mail-header-id header)
5931                   ref (gnus-parent-id (mail-header-references header))))
5932
5933           (when (setq header
5934                       (gnus-dependencies-add-header
5935                        header dependencies force-new))
5936             (push header headers))
5937           (goto-char (point-max))
5938           (widen))
5939         (nreverse headers)))))
5940
5941 ;; Goes through the xover lines and returns a list of vectors
5942 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5943                                                   force-new dependencies
5944                                                   group also-fetch-heads)
5945   "Parse the news overview data in the server buffer.
5946 Return a list of headers that match SEQUENCE (see
5947 `nntp-retrieve-headers')."
5948   ;; Get the Xref when the users reads the articles since most/some
5949   ;; NNTP servers do not include Xrefs when using XOVER.
5950   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5951   (let ((mail-parse-charset gnus-newsgroup-charset)
5952         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5953         (cur nntp-server-buffer)
5954         (dependencies (or dependencies gnus-newsgroup-dependencies))
5955         (allp (cond
5956                ((eq gnus-read-all-available-headers t)
5957                 t)
5958                ((stringp gnus-read-all-available-headers)
5959                 (string-match gnus-read-all-available-headers group))
5960                (t
5961                 nil)))
5962         number headers header)
5963     (save-excursion
5964       (set-buffer nntp-server-buffer)
5965       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5966       ;; Allow the user to mangle the headers before parsing them.
5967       (gnus-run-hooks 'gnus-parse-headers-hook)
5968       (goto-char (point-min))
5969       (gnus-parse-without-error
5970         (while (and (or sequence allp)
5971                     (not (eobp)))
5972           (setq number (read cur))
5973           (when (not allp)
5974             (while (and sequence
5975                         (< (car sequence) number))
5976               (setq sequence (cdr sequence))))
5977           (when (and (or allp
5978                          (and sequence
5979                               (eq number (car sequence))))
5980                      (progn
5981                        (setq sequence (cdr sequence))
5982                        (setq header (inline
5983                                       (gnus-nov-parse-line
5984                                        number dependencies force-new)))))
5985             (push header headers))
5986           (forward-line 1)))
5987       ;; A common bug in inn is that if you have posted an article and
5988       ;; then retrieves the active file, it will answer correctly --
5989       ;; the new article is included.  However, a NOV entry for the
5990       ;; article may not have been generated yet, so this may fail.
5991       ;; We work around this problem by retrieving the last few
5992       ;; headers using HEAD.
5993       (if (or (not also-fetch-heads)
5994               (not sequence))
5995           ;; We (probably) got all the headers.
5996           (nreverse headers)
5997         (let ((gnus-nov-is-evil t))
5998           (nconc
5999            (nreverse headers)
6000            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6001              (gnus-get-newsgroup-headers))))))))
6002
6003 (defun gnus-article-get-xrefs ()
6004   "Fill in the Xref value in `gnus-current-headers', if necessary.
6005 This is meant to be called in `gnus-article-internal-prepare-hook'."
6006   (let ((headers (with-current-buffer gnus-summary-buffer
6007                    gnus-current-headers)))
6008     (or (not gnus-use-cross-reference)
6009         (not headers)
6010         (and (mail-header-xref headers)
6011              (not (string= (mail-header-xref headers) "")))
6012         (let ((case-fold-search t)
6013               xref)
6014           (save-restriction
6015             (nnheader-narrow-to-headers)
6016             (goto-char (point-min))
6017             (when (or (and (not (eobp))
6018                            (eq (downcase (char-after)) ?x)
6019                            (looking-at "Xref:"))
6020                       (search-forward "\nXref:" nil t))
6021               (goto-char (1+ (match-end 0)))
6022               (setq xref (buffer-substring (point) (point-at-eol)))
6023               (mail-header-set-xref headers xref)))))))
6024
6025 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6026   "Find article ID and insert the summary line for that article.
6027 OLD-HEADER can either be a header or a line number to insert
6028 the subject line on."
6029   (let* ((line (and (numberp old-header) old-header))
6030          (old-header (and (vectorp old-header) old-header))
6031          (header (cond ((and old-header use-old-header)
6032                         old-header)
6033                        ((and (numberp id)
6034                              (gnus-number-to-header id))
6035                         (gnus-number-to-header id))
6036                        (t
6037                         (gnus-read-header id))))
6038          (number (and (numberp id) id))
6039          d)
6040     (when header
6041       ;; Rebuild the thread that this article is part of and go to the
6042       ;; article we have fetched.
6043       (when (and (not gnus-show-threads)
6044                  old-header)
6045         (when (and number
6046                    (setq d (gnus-data-find (mail-header-number old-header))))
6047           (goto-char (gnus-data-pos d))
6048           (gnus-data-remove
6049            number
6050            (- (point-at-bol)
6051               (prog1
6052                   (1+ (point-at-eol))
6053                 (gnus-delete-line))))))
6054       (when old-header
6055         (mail-header-set-number header (mail-header-number old-header)))
6056       (setq gnus-newsgroup-sparse
6057             (delq (setq number (mail-header-number header))
6058                   gnus-newsgroup-sparse))
6059       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6060       (push number gnus-newsgroup-limit)
6061       (gnus-rebuild-thread (mail-header-id header) line)
6062       (gnus-summary-goto-subject number nil t))
6063     (when (and (numberp number)
6064                (> number 0))
6065       ;; We have to update the boundaries even if we can't fetch the
6066       ;; article if ID is a number -- so that the next `P' or `N'
6067       ;; command will fetch the previous (or next) article even
6068       ;; if the one we tried to fetch this time has been canceled.
6069       (when (> number gnus-newsgroup-end)
6070         (setq gnus-newsgroup-end number))
6071       (when (< number gnus-newsgroup-begin)
6072         (setq gnus-newsgroup-begin number))
6073       (setq gnus-newsgroup-unselected
6074             (delq number gnus-newsgroup-unselected)))
6075     ;; Report back a success?
6076     (and header (mail-header-number header))))
6077
6078 ;;; Process/prefix in the summary buffer
6079
6080 (defun gnus-summary-work-articles (n)
6081   "Return a list of articles to be worked upon.
6082 The prefix argument, the list of process marked articles, and the
6083 current article will be taken into consideration."
6084   (save-excursion
6085     (set-buffer gnus-summary-buffer)
6086     (cond
6087      (n
6088       ;; A numerical prefix has been given.
6089       (setq n (prefix-numeric-value n))
6090       (let ((backward (< n 0))
6091             (n (abs (prefix-numeric-value n)))
6092             articles article)
6093         (save-excursion
6094           (while
6095               (and (> n 0)
6096                    (push (setq article (gnus-summary-article-number))
6097                          articles)
6098                    (if backward
6099                        (gnus-summary-find-prev nil article)
6100                      (gnus-summary-find-next nil article)))
6101             (decf n)))
6102         (nreverse articles)))
6103      ((and (gnus-region-active-p) (mark))
6104       (message "region active")
6105       ;; Work on the region between point and mark.
6106       (let ((max (max (point) (mark)))
6107             articles article)
6108         (save-excursion
6109           (goto-char (min (min (point) (mark))))
6110           (while
6111               (and
6112                (push (setq article (gnus-summary-article-number)) articles)
6113                (gnus-summary-find-next nil article)
6114                (< (point) max)))
6115           (nreverse articles))))
6116      (gnus-newsgroup-processable
6117       ;; There are process-marked articles present.
6118       ;; Save current state.
6119       (gnus-summary-save-process-mark)
6120       ;; Return the list.
6121       (reverse gnus-newsgroup-processable))
6122      (t
6123       ;; Just return the current article.
6124       (list (gnus-summary-article-number))))))
6125
6126 (defmacro gnus-summary-iterate (arg &rest forms)
6127   "Iterate over the process/prefixed articles and do FORMS.
6128 ARG is the interactive prefix given to the command.  FORMS will be
6129 executed with point over the summary line of the articles."
6130   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6131     `(let ((,articles (gnus-summary-work-articles ,arg)))
6132        (while ,articles
6133          (gnus-summary-goto-subject (car ,articles))
6134          ,@forms
6135          (pop ,articles)))))
6136
6137 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6138 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6139
6140 (defun gnus-summary-save-process-mark ()
6141   "Push the current set of process marked articles on the stack."
6142   (interactive)
6143   (push (copy-sequence gnus-newsgroup-processable)
6144         gnus-newsgroup-process-stack))
6145
6146 (defun gnus-summary-kill-process-mark ()
6147   "Push the current set of process marked articles on the stack and unmark."
6148   (interactive)
6149   (gnus-summary-save-process-mark)
6150   (gnus-summary-unmark-all-processable))
6151
6152 (defun gnus-summary-yank-process-mark ()
6153   "Pop the last process mark state off the stack and restore it."
6154   (interactive)
6155   (unless gnus-newsgroup-process-stack
6156     (error "Empty mark stack"))
6157   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6158
6159 (defun gnus-summary-process-mark-set (set)
6160   "Make SET into the current process marked articles."
6161   (gnus-summary-unmark-all-processable)
6162   (while set
6163     (gnus-summary-set-process-mark (pop set))))
6164
6165 ;;; Searching and stuff
6166
6167 (defun gnus-summary-search-group (&optional backward use-level)
6168   "Search for next unread newsgroup.
6169 If optional argument BACKWARD is non-nil, search backward instead."
6170   (save-excursion
6171     (set-buffer gnus-group-buffer)
6172     (when (gnus-group-search-forward
6173            backward nil (if use-level (gnus-group-group-level) nil))
6174       (gnus-group-group-name))))
6175
6176 (defun gnus-summary-best-group (&optional exclude-group)
6177   "Find the name of the best unread group.
6178 If EXCLUDE-GROUP, do not go to this group."
6179   (with-current-buffer gnus-group-buffer
6180     (save-excursion
6181       (gnus-group-best-unread-group exclude-group))))
6182
6183 (defun gnus-summary-find-next (&optional unread article backward)
6184   (if backward
6185       (gnus-summary-find-prev unread article)
6186     (let* ((dummy (gnus-summary-article-intangible-p))
6187            (article (or article (gnus-summary-article-number)))
6188            (data (gnus-data-find-list article))
6189            result)
6190       (when (and (not dummy)
6191                  (or (not gnus-summary-check-current)
6192                      (not unread)
6193                      (not (gnus-data-unread-p (car data)))))
6194         (setq data (cdr data)))
6195       (when (setq result
6196                   (if unread
6197                       (progn
6198                         (while data
6199                           (unless (memq (gnus-data-number (car data))
6200                                         (cond
6201                                          ((eq gnus-auto-goto-ignores
6202                                               'always-undownloaded)
6203                                           gnus-newsgroup-undownloaded)
6204                                          (gnus-plugged
6205                                           nil)
6206                                          ((eq gnus-auto-goto-ignores
6207                                               'unfetched)
6208                                           gnus-newsgroup-unfetched)
6209                                          ((eq gnus-auto-goto-ignores
6210                                               'undownloaded)
6211                                           gnus-newsgroup-undownloaded)))
6212                             (when (gnus-data-unread-p (car data))
6213                               (setq result (car data)
6214                                     data nil)))
6215                           (setq data (cdr data)))
6216                         result)
6217                     (car data)))
6218         (goto-char (gnus-data-pos result))
6219         (gnus-data-number result)))))
6220
6221 (defun gnus-summary-find-prev (&optional unread article)
6222   (let* ((eobp (eobp))
6223          (article (or article (gnus-summary-article-number)))
6224          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6225          result)
6226     (when (and (not eobp)
6227                (or (not gnus-summary-check-current)
6228                    (not unread)
6229                    (not (gnus-data-unread-p (car data)))))
6230       (setq data (cdr data)))
6231     (when (setq result
6232                 (if unread
6233                     (progn
6234                       (while data
6235                         (unless (memq (gnus-data-number (car data))
6236                                       (cond
6237                                        ((eq gnus-auto-goto-ignores
6238                                             'always-undownloaded)
6239                                         gnus-newsgroup-undownloaded)
6240                                        (gnus-plugged
6241                                         nil)
6242                                        ((eq gnus-auto-goto-ignores
6243                                             'unfetched)
6244                                         gnus-newsgroup-unfetched)
6245                                        ((eq gnus-auto-goto-ignores
6246                                             'undownloaded)
6247                                         gnus-newsgroup-undownloaded)))
6248                           (when (gnus-data-unread-p (car data))
6249                             (setq result (car data)
6250                                   data nil)))
6251                         (setq data (cdr data)))
6252                       result)
6253                   (car data)))
6254       (goto-char (gnus-data-pos result))
6255       (gnus-data-number result))))
6256
6257 (defun gnus-summary-find-subject (subject &optional unread backward article)
6258   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6259          (article (or article (gnus-summary-article-number)))
6260          (articles (gnus-data-list backward))
6261          (arts (gnus-data-find-list article articles))
6262          result)
6263     (when (or (not gnus-summary-check-current)
6264               (not unread)
6265               (not (gnus-data-unread-p (car arts))))
6266       (setq arts (cdr arts)))
6267     (while arts
6268       (and (or (not unread)
6269                (gnus-data-unread-p (car arts)))
6270            (vectorp (gnus-data-header (car arts)))
6271            (gnus-subject-equal
6272             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6273            (setq result (car arts)
6274                  arts nil))
6275       (setq arts (cdr arts)))
6276     (and result
6277          (goto-char (gnus-data-pos result))
6278          (gnus-data-number result))))
6279
6280 (defun gnus-summary-search-forward (&optional unread subject backward)
6281   "Search forward for an article.
6282 If UNREAD, look for unread articles.  If SUBJECT, look for
6283 articles with that subject.  If BACKWARD, search backward instead."
6284   (cond (subject (gnus-summary-find-subject subject unread backward))
6285         (backward (gnus-summary-find-prev unread))
6286         (t (gnus-summary-find-next unread))))
6287
6288 (defun gnus-recenter (&optional n)
6289   "Center point in window and redisplay frame.
6290 Also do horizontal recentering."
6291   (interactive "P")
6292   (when (and gnus-auto-center-summary
6293              (not (eq gnus-auto-center-summary 'vertical)))
6294     (gnus-horizontal-recenter))
6295   (recenter n))
6296
6297 (defun gnus-summary-recenter ()
6298   "Center point in the summary window.
6299 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6300 displayed, no centering will be performed."
6301   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6302   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6303   (interactive)
6304   ;; The user has to want it.
6305   (when gnus-auto-center-summary
6306     (let* ((top (cond ((< (window-height) 4) 0)
6307                       ((< (window-height) 7) 1)
6308                       (t (if (numberp gnus-auto-center-summary)
6309                              gnus-auto-center-summary
6310                            2))))
6311            (height (1- (window-height)))
6312            (bottom (save-excursion (goto-char (point-max))
6313                                    (forward-line (- height))
6314                                    (point)))
6315            (window (get-buffer-window (current-buffer))))
6316       (when (get-buffer-window gnus-article-buffer)
6317         ;; Only do recentering when the article buffer is displayed,
6318         ;; Set the window start to either `bottom', which is the biggest
6319         ;; possible valid number, or the second line from the top,
6320         ;; whichever is the least.
6321         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6322           (if (> bottom top-pos)
6323               ;; Keep the second line from the top visible
6324               (set-window-start window top-pos t)
6325             ;; Try to keep the bottom line visible; if it's partially
6326             ;; obscured, either scroll one more line to make it fully
6327             ;; visible, or revert to using TOP-POS.
6328             (save-excursion
6329               (goto-char (point-max))
6330               (forward-line -1)
6331               (let ((last-line-start (point)))
6332                 (goto-char bottom)
6333                 (set-window-start window (point) t)
6334                 (when (not (pos-visible-in-window-p last-line-start window))
6335                   (forward-line 1)
6336                   (set-window-start window (min (point) top-pos) t)))))))
6337       ;; Do horizontal recentering while we're at it.
6338       (when (and (get-buffer-window (current-buffer) t)
6339                  (not (eq gnus-auto-center-summary 'vertical)))
6340         (let ((selected (selected-window)))
6341           (select-window (get-buffer-window (current-buffer) t))
6342           (gnus-summary-position-point)
6343           (gnus-horizontal-recenter)
6344           (select-window selected))))))
6345
6346 (defun gnus-summary-jump-to-group (newsgroup)
6347   "Move point to NEWSGROUP in group mode buffer."
6348   ;; Keep update point of group mode buffer if visible.
6349   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6350       (save-window-excursion
6351         ;; Take care of tree window mode.
6352         (when (get-buffer-window gnus-group-buffer)
6353           (pop-to-buffer gnus-group-buffer))
6354         (gnus-group-jump-to-group newsgroup))
6355     (save-excursion
6356       ;; Take care of tree window mode.
6357       (if (get-buffer-window gnus-group-buffer)
6358           (pop-to-buffer gnus-group-buffer)
6359         (set-buffer gnus-group-buffer))
6360       (gnus-group-jump-to-group newsgroup))))
6361
6362 ;; This function returns a list of article numbers based on the
6363 ;; difference between the ranges of read articles in this group and
6364 ;; the range of active articles.
6365 (defun gnus-list-of-unread-articles (group)
6366   (let* ((read (gnus-info-read (gnus-get-info group)))
6367          (active (or (gnus-active group) (gnus-activate-group group)))
6368          (last (cdr active))
6369          first nlast unread)
6370     ;; If none are read, then all are unread.
6371     (if (not read)
6372         (setq first (car active))
6373       ;; If the range of read articles is a single range, then the
6374       ;; first unread article is the article after the last read
6375       ;; article.  Sounds logical, doesn't it?
6376       (if (and (not (listp (cdr read)))
6377                (or (< (car read) (car active))
6378                    (progn (setq read (list read))
6379                           nil)))
6380           (setq first (max (car active) (1+ (cdr read))))
6381         ;; `read' is a list of ranges.
6382         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6383                                   (caar read)))
6384                   1)
6385           (setq first (car active)))
6386         (while read
6387           (when first
6388             (while (< first nlast)
6389               (setq unread (cons first unread)
6390                     first (1+ first))))
6391           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6392           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6393           (setq read (cdr read)))))
6394     ;; And add the last unread articles.
6395     (while (<= first last)
6396       (setq unread (cons first unread)
6397             first (1+ first)))
6398     ;; Return the list of unread articles.
6399     (delq 0 (nreverse unread))))
6400
6401 (defun gnus-list-of-read-articles (group)
6402   "Return a list of unread, unticked and non-dormant articles."
6403   (let* ((info (gnus-get-info group))
6404          (marked (gnus-info-marks info))
6405          (active (gnus-active group)))
6406     (and info active
6407          (gnus-list-range-difference
6408           (gnus-list-range-difference
6409            (gnus-sorted-complement
6410             (gnus-uncompress-range active)
6411             (gnus-list-of-unread-articles group))
6412            (cdr (assq 'dormant marked)))
6413           (cdr (assq 'tick marked))))))
6414
6415 ;; This function returns a sequence of article numbers based on the
6416 ;; difference between the ranges of read articles in this group and
6417 ;; the range of active articles.
6418 (defun gnus-sequence-of-unread-articles (group)
6419   (let* ((read (gnus-info-read (gnus-get-info group)))
6420          (active (or (gnus-active group) (gnus-activate-group group)))
6421          (last (cdr active))
6422          first nlast unread)
6423     ;; If none are read, then all are unread.
6424     (if (not read)
6425         (setq first (car active))
6426       ;; If the range of read articles is a single range, then the
6427       ;; first unread article is the article after the last read
6428       ;; article.  Sounds logical, doesn't it?
6429       (if (and (not (listp (cdr read)))
6430                (or (< (car read) (car active))
6431                    (progn (setq read (list read))
6432                           nil)))
6433           (setq first (max (car active) (1+ (cdr read))))
6434         ;; `read' is a list of ranges.
6435         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6436                                   (caar read)))
6437                   1)
6438           (setq first (car active)))
6439         (while read
6440           (when first
6441             (push (cons first nlast) unread))
6442           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6443           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6444           (setq read (cdr read)))))
6445     ;; And add the last unread articles.
6446     (cond ((< first last)
6447            (push (cons first last) unread))
6448           ((= first last)
6449            (push first unread)))
6450     ;; Return the sequence of unread articles.
6451     (delq 0 (nreverse unread))))
6452
6453 ;; Various summary commands
6454
6455 (defun gnus-summary-select-article-buffer ()
6456   "Reconfigure windows to show article buffer."
6457   (interactive)
6458   (if (not (gnus-buffer-live-p gnus-article-buffer))
6459       (error "There is no article buffer for this summary buffer")
6460     (gnus-configure-windows 'article)
6461     (select-window (get-buffer-window gnus-article-buffer))))
6462
6463 (defun gnus-summary-universal-argument (arg)
6464   "Perform any operation on all articles that are process/prefixed."
6465   (interactive "P")
6466   (let ((articles (gnus-summary-work-articles arg))
6467         func article)
6468     (if (eq
6469          (setq
6470           func
6471           (key-binding
6472            (read-key-sequence
6473             (substitute-command-keys
6474              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6475          'undefined)
6476         (gnus-error 1 "Undefined key")
6477       (save-excursion
6478         (while articles
6479           (gnus-summary-goto-subject (setq article (pop articles)))
6480           (let (gnus-newsgroup-processable)
6481             (command-execute func))
6482           (gnus-summary-remove-process-mark article)))))
6483   (gnus-summary-position-point))
6484
6485 (defun gnus-summary-toggle-truncation (&optional arg)
6486   "Toggle truncation of summary lines.
6487 With ARG, turn line truncation on if ARG is positive."
6488   (interactive "P")
6489   (setq truncate-lines
6490         (if (null arg) (not truncate-lines)
6491           (> (prefix-numeric-value arg) 0)))
6492   (redraw-display))
6493
6494 (defun gnus-summary-find-for-reselect ()
6495   "Return the number of an article to stay on across a reselect.
6496 The current article is considered, then following articles, then previous
6497 articles.  An article is sought which is not cancelled and isn't a temporary
6498 insertion from another group.  If there's no such then return a dummy 0."
6499   (let (found)
6500     (dolist (rev '(nil t))
6501       (unless found      ; don't demand the reverse list if we don't need it
6502         (let ((data (gnus-data-find-list
6503                      (gnus-summary-article-number) (gnus-data-list rev))))
6504           (while (and data (not found))
6505             (if (and (< 0 (gnus-data-number (car data)))
6506                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6507                 (setq found (gnus-data-number (car data))))
6508             (setq data (cdr data))))))
6509     (or found 0)))
6510
6511 (defun gnus-summary-reselect-current-group (&optional all rescan)
6512   "Exit and then reselect the current newsgroup.
6513 The prefix argument ALL means to select all articles."
6514   (interactive "P")
6515   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6516     (error "Ephemeral groups can't be reselected"))
6517   (let ((current-subject (gnus-summary-find-for-reselect))
6518         (group gnus-newsgroup-name))
6519     (setq gnus-newsgroup-begin nil)
6520     (gnus-summary-exit nil 'leave-hidden)
6521     ;; We have to adjust the point of group mode buffer because
6522     ;; point was moved to the next unread newsgroup by exiting.
6523     (gnus-summary-jump-to-group group)
6524     (when rescan
6525       (save-excursion
6526         (gnus-group-get-new-news-this-group 1)))
6527     (gnus-group-read-group all t)
6528     (gnus-summary-goto-subject current-subject nil t)))
6529
6530 (defun gnus-summary-rescan-group (&optional all)
6531   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6532   (interactive "P")
6533   (gnus-summary-reselect-current-group all t))
6534
6535 (defun gnus-summary-update-info (&optional non-destructive)
6536   (save-excursion
6537     (let ((group gnus-newsgroup-name))
6538       (when group
6539         (when gnus-newsgroup-kill-headers
6540           (setq gnus-newsgroup-killed
6541                 (gnus-compress-sequence
6542                  (gnus-sorted-union
6543                   (gnus-list-range-intersection
6544                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6545                   gnus-newsgroup-unreads)
6546                  t)))
6547         (unless (listp (cdr gnus-newsgroup-killed))
6548           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6549         (let ((headers gnus-newsgroup-headers))
6550           ;; Set the new ranges of read articles.
6551           (with-current-buffer gnus-group-buffer
6552             (gnus-undo-force-boundary))
6553           (gnus-update-read-articles
6554            group (gnus-sorted-union
6555                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6556           ;; Set the current article marks.
6557           (let ((gnus-newsgroup-scored
6558                  (if (and (not gnus-save-score)
6559                           (not non-destructive))
6560                      nil
6561                    gnus-newsgroup-scored)))
6562             (save-excursion
6563               (gnus-update-marks)))
6564           ;; Do the cross-ref thing.
6565           (when gnus-use-cross-reference
6566             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6567           ;; Do not switch windows but change the buffer to work.
6568           (set-buffer gnus-group-buffer)
6569           (unless (gnus-ephemeral-group-p group)
6570             (gnus-group-update-group group)))))))
6571
6572 (defun gnus-summary-save-newsrc (&optional force)
6573   "Save the current number of read/marked articles in the dribble buffer.
6574 The dribble buffer will then be saved.
6575 If FORCE (the prefix), also save the .newsrc file(s)."
6576   (interactive "P")
6577   (gnus-summary-update-info t)
6578   (if force
6579       (gnus-save-newsrc-file)
6580     (gnus-dribble-save)))
6581
6582 (defun gnus-summary-exit (&optional temporary leave-hidden)
6583   "Exit reading current newsgroup, and then return to group selection mode.
6584 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6585   (interactive)
6586   (gnus-set-global-variables)
6587   (when (gnus-buffer-live-p gnus-article-buffer)
6588     (save-excursion
6589       (set-buffer gnus-article-buffer)
6590       (mm-destroy-parts gnus-article-mime-handles)
6591       ;; Set it to nil for safety reason.
6592       (setq gnus-article-mime-handle-alist nil)
6593       (setq gnus-article-mime-handles nil)))
6594   (gnus-kill-save-kill-buffer)
6595   (gnus-async-halt-prefetch)
6596   (let* ((group gnus-newsgroup-name)
6597          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6598          (gnus-group-is-exiting-p t)
6599          (mode major-mode)
6600          (group-point nil)
6601          (buf (current-buffer)))
6602     (unless quit-config
6603       ;; Do adaptive scoring, and possibly save score files.
6604       (when gnus-newsgroup-adaptive
6605         (gnus-score-adaptive))
6606       (when gnus-use-scoring
6607         (gnus-score-save)))
6608     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6609     ;; If we have several article buffers, we kill them at exit.
6610     (unless gnus-single-article-buffer
6611       (gnus-kill-buffer gnus-original-article-buffer)
6612       (setq gnus-article-current nil))
6613     (when gnus-use-cache
6614       (gnus-cache-possibly-remove-articles)
6615       (gnus-cache-save-buffers))
6616     (gnus-async-prefetch-remove-group group)
6617     (when gnus-suppress-duplicates
6618       (gnus-dup-enter-articles))
6619     (when gnus-use-trees
6620       (gnus-tree-close group))
6621     (when gnus-use-cache
6622       (gnus-cache-write-active))
6623     ;; Remove entries for this group.
6624     (nnmail-purge-split-history (gnus-group-real-name group))
6625     ;; Make all changes in this group permanent.
6626     (unless quit-config
6627       (gnus-run-hooks 'gnus-exit-group-hook)
6628       (gnus-summary-update-info))
6629     (gnus-close-group group)
6630     ;; Make sure where we were, and go to next newsgroup.
6631     (set-buffer gnus-group-buffer)
6632     (unless quit-config
6633       (gnus-group-jump-to-group group))
6634     (gnus-run-hooks 'gnus-summary-exit-hook)
6635     (unless (or quit-config
6636                 ;; If this group has disappeared from the summary
6637                 ;; buffer, don't skip forwards.
6638                 (not (string= group (gnus-group-group-name))))
6639       (gnus-group-next-unread-group 1))
6640     (setq group-point (point))
6641     (if temporary
6642         nil                             ;Nothing to do.
6643       ;; If we have several article buffers, we kill them at exit.
6644       (unless gnus-single-article-buffer
6645         (gnus-kill-buffer gnus-article-buffer)
6646         (gnus-kill-buffer gnus-original-article-buffer)
6647         (setq gnus-article-current nil))
6648       (set-buffer buf)
6649       (if (not gnus-kill-summary-on-exit)
6650           (progn
6651             (gnus-deaden-summary)
6652             (setq mode nil))
6653         ;; We set all buffer-local variables to nil.  It is unclear why
6654         ;; this is needed, but if we don't, buffer-local variables are
6655         ;; not garbage-collected, it seems.  This would the lead to en
6656         ;; ever-growing Emacs.
6657         (gnus-summary-clear-local-variables)
6658         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6659           (gnus-summary-clear-local-variables))
6660         (when (get-buffer gnus-article-buffer)
6661           (bury-buffer gnus-article-buffer))
6662         ;; We clear the global counterparts of the buffer-local
6663         ;; variables as well, just to be on the safe side.
6664         (set-buffer gnus-group-buffer)
6665         (gnus-summary-clear-local-variables)
6666         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6667           (gnus-summary-clear-local-variables))
6668         ;; Return to group mode buffer.
6669         (when (eq mode 'gnus-summary-mode)
6670           (gnus-kill-buffer buf)))
6671       (setq gnus-current-select-method gnus-select-method)
6672       (if leave-hidden
6673           (set-buffer gnus-group-buffer)
6674         (pop-to-buffer gnus-group-buffer))
6675       (if (not quit-config)
6676           (progn
6677             (goto-char group-point)
6678             (unless leave-hidden
6679               (gnus-configure-windows 'group 'force)))
6680         (gnus-handle-ephemeral-exit quit-config))
6681       ;; Clear the current group name.
6682       (unless quit-config
6683         (setq gnus-newsgroup-name nil)))))
6684
6685 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6686 (defun gnus-summary-exit-no-update (&optional no-questions)
6687   "Quit reading current newsgroup without updating read article info."
6688   (interactive)
6689   (let* ((group gnus-newsgroup-name)
6690          (gnus-group-is-exiting-p t)
6691          (gnus-group-is-exiting-without-update-p t)
6692          (quit-config (gnus-group-quit-config group)))
6693     (when (or no-questions
6694               gnus-expert-user
6695               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6696       (gnus-async-halt-prefetch)
6697       (run-hooks 'gnus-summary-prepare-exit-hook)
6698       (when (gnus-buffer-live-p gnus-article-buffer)
6699         (save-excursion
6700           (set-buffer gnus-article-buffer)
6701           (mm-destroy-parts gnus-article-mime-handles)
6702           ;; Set it to nil for safety reason.
6703           (setq gnus-article-mime-handle-alist nil)
6704           (setq gnus-article-mime-handles nil)))
6705       ;; If we have several article buffers, we kill them at exit.
6706       (unless gnus-single-article-buffer
6707         (gnus-kill-buffer gnus-article-buffer)
6708         (gnus-kill-buffer gnus-original-article-buffer)
6709         (setq gnus-article-current nil))
6710       (if (not gnus-kill-summary-on-exit)
6711           (gnus-deaden-summary)
6712         (gnus-close-group group)
6713         (gnus-summary-clear-local-variables)
6714         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6715           (gnus-summary-clear-local-variables))
6716         (set-buffer gnus-group-buffer)
6717         (gnus-summary-clear-local-variables)
6718         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6719           (gnus-summary-clear-local-variables))
6720         (gnus-kill-buffer gnus-summary-buffer))
6721       (unless gnus-single-article-buffer
6722         (setq gnus-article-current nil))
6723       (when gnus-use-trees
6724         (gnus-tree-close group))
6725       (gnus-async-prefetch-remove-group group)
6726       (when (get-buffer gnus-article-buffer)
6727         (bury-buffer gnus-article-buffer))
6728       ;; Return to the group buffer.
6729       (gnus-configure-windows 'group 'force)
6730       ;; Clear the current group name.
6731       (setq gnus-newsgroup-name nil)
6732       (unless (gnus-ephemeral-group-p group)
6733         (gnus-group-update-group group))
6734       (when (equal (gnus-group-group-name) group)
6735         (gnus-group-next-unread-group 1))
6736       (when quit-config
6737         (gnus-handle-ephemeral-exit quit-config)))))
6738
6739 (defun gnus-handle-ephemeral-exit (quit-config)
6740   "Handle movement when leaving an ephemeral group.
6741 The state which existed when entering the ephemeral is reset."
6742   (if (not (buffer-name (car quit-config)))
6743       (gnus-configure-windows 'group 'force)
6744     (set-buffer (car quit-config))
6745     (cond ((eq major-mode 'gnus-summary-mode)
6746            (gnus-set-global-variables))
6747           ((eq major-mode 'gnus-article-mode)
6748            (save-excursion
6749              ;; The `gnus-summary-buffer' variable may point
6750              ;; to the old summary buffer when using a single
6751              ;; article buffer.
6752              (unless (gnus-buffer-live-p gnus-summary-buffer)
6753                (set-buffer gnus-group-buffer))
6754              (set-buffer gnus-summary-buffer)
6755              (gnus-set-global-variables))))
6756     (if (or (eq (cdr quit-config) 'article)
6757             (eq (cdr quit-config) 'pick))
6758         (progn
6759           ;; The current article may be from the ephemeral group
6760           ;; thus it is best that we reload this article
6761           ;;
6762           ;; If we're exiting from a large digest, this can be
6763           ;; extremely slow.  So, it's better not to reload it. -- jh.
6764           ;;(gnus-summary-show-article)
6765           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6766               (gnus-configure-windows 'pick 'force)
6767             (gnus-configure-windows (cdr quit-config) 'force)))
6768       (gnus-configure-windows (cdr quit-config) 'force))
6769     (when (eq major-mode 'gnus-summary-mode)
6770       (gnus-summary-next-subject 1 nil t)
6771       (gnus-summary-recenter)
6772       (gnus-summary-position-point))))
6773
6774 ;;; Dead summaries.
6775
6776 (defvar gnus-dead-summary-mode-map nil)
6777
6778 (unless gnus-dead-summary-mode-map
6779   (setq gnus-dead-summary-mode-map (make-keymap))
6780   (suppress-keymap gnus-dead-summary-mode-map)
6781   (substitute-key-definition
6782    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6783   (dolist (key '("\C-d" "\r" "\177" [delete]))
6784     (define-key gnus-dead-summary-mode-map
6785       key 'gnus-summary-wake-up-the-dead))
6786   (dolist (key '("q" "Q"))
6787     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6788
6789 (defvar gnus-dead-summary-mode nil
6790   "Minor mode for Gnus summary buffers.")
6791
6792 (defun gnus-dead-summary-mode (&optional arg)
6793   "Minor mode for Gnus summary buffers."
6794   (interactive "P")
6795   (when (eq major-mode 'gnus-summary-mode)
6796     (make-local-variable 'gnus-dead-summary-mode)
6797     (setq gnus-dead-summary-mode
6798           (if (null arg) (not gnus-dead-summary-mode)
6799             (> (prefix-numeric-value arg) 0)))
6800     (when gnus-dead-summary-mode
6801       (add-minor-mode
6802        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6803
6804 (defun gnus-deaden-summary ()
6805   "Make the current summary buffer into a dead summary buffer."
6806   ;; Kill any previous dead summary buffer.
6807   (when (and gnus-dead-summary
6808              (buffer-name gnus-dead-summary))
6809     (with-current-buffer gnus-dead-summary
6810       (when gnus-dead-summary-mode
6811         (kill-buffer (current-buffer)))))
6812   ;; Make this the current dead summary.
6813   (setq gnus-dead-summary (current-buffer))
6814   (gnus-dead-summary-mode 1)
6815   (let ((name (buffer-name)))
6816     (when (string-match "Summary" name)
6817       (rename-buffer
6818        (concat (substring name 0 (match-beginning 0)) "Dead "
6819                (substring name (match-beginning 0)))
6820        t)
6821       (bury-buffer))))
6822
6823 (defun gnus-kill-or-deaden-summary (buffer)
6824   "Kill or deaden the summary BUFFER."
6825   (save-excursion
6826     (when (and (buffer-name buffer)
6827                (not gnus-single-article-buffer))
6828       (with-current-buffer buffer
6829         (gnus-kill-buffer gnus-article-buffer)
6830         (gnus-kill-buffer gnus-original-article-buffer)))
6831     (cond
6832      ;; Kill the buffer.
6833      (gnus-kill-summary-on-exit
6834       (when (and gnus-use-trees
6835                  (gnus-buffer-exists-p buffer))
6836         (save-excursion
6837           (set-buffer buffer)
6838           (gnus-tree-close gnus-newsgroup-name)))
6839       (gnus-kill-buffer buffer))
6840      ;; Deaden the buffer.
6841      ((gnus-buffer-exists-p buffer)
6842       (save-excursion
6843         (set-buffer buffer)
6844         (gnus-deaden-summary))))))
6845
6846 (defun gnus-summary-wake-up-the-dead (&rest args)
6847   "Wake up the dead summary buffer."
6848   (interactive)
6849   (gnus-dead-summary-mode -1)
6850   (let ((name (buffer-name)))
6851     (when (string-match "Dead " name)
6852       (rename-buffer
6853        (concat (substring name 0 (match-beginning 0))
6854                (substring name (match-end 0)))
6855        t)))
6856   (gnus-message 3 "This dead summary is now alive again"))
6857
6858 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6859 (defun gnus-summary-fetch-faq (&optional faq-dir)
6860   "Fetch the FAQ for the current group.
6861 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6862 in."
6863   (interactive
6864    (list
6865     (when current-prefix-arg
6866       (completing-read
6867        "FAQ dir: " (and (listp gnus-group-faq-directory)
6868                         (mapcar 'list
6869                                 gnus-group-faq-directory))))))
6870   (let (gnus-faq-buffer)
6871     (when (setq gnus-faq-buffer
6872                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6873       (gnus-configure-windows 'summary-faq))))
6874
6875 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6876 (defun gnus-summary-describe-group (&optional force)
6877   "Describe the current newsgroup."
6878   (interactive "P")
6879   (gnus-group-describe-group force gnus-newsgroup-name))
6880
6881 (defun gnus-summary-describe-briefly ()
6882   "Describe summary mode commands briefly."
6883   (interactive)
6884   (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")))
6885
6886 ;; Walking around group mode buffer from summary mode.
6887
6888 (defun gnus-summary-next-group (&optional no-article target-group backward)
6889   "Exit current newsgroup and then select next unread newsgroup.
6890 If prefix argument NO-ARTICLE is non-nil, no article is selected
6891 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6892 previous group instead."
6893   (interactive "P")
6894   ;; Stop pre-fetching.
6895   (gnus-async-halt-prefetch)
6896   (let ((current-group gnus-newsgroup-name)
6897         (current-buffer (current-buffer))
6898         entered)
6899     ;; First we semi-exit this group to update Xrefs and all variables.
6900     ;; We can't do a real exit, because the window conf must remain
6901     ;; the same in case the user is prompted for info, and we don't
6902     ;; want the window conf to change before that...
6903     (gnus-summary-exit t)
6904     (while (not entered)
6905       ;; Then we find what group we are supposed to enter.
6906       (set-buffer gnus-group-buffer)
6907       (gnus-group-jump-to-group current-group)
6908       (setq target-group
6909             (or target-group
6910                 (if (eq gnus-keep-same-level 'best)
6911                     (gnus-summary-best-group gnus-newsgroup-name)
6912                   (gnus-summary-search-group backward gnus-keep-same-level))))
6913       (if (not target-group)
6914           ;; There are no further groups, so we return to the group
6915           ;; buffer.
6916           (progn
6917             (gnus-message 5 "Returning to the group buffer")
6918             (setq entered t)
6919             (when (gnus-buffer-live-p current-buffer)
6920               (set-buffer current-buffer)
6921               (gnus-summary-exit))
6922             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6923         ;; We try to enter the target group.
6924         (gnus-group-jump-to-group target-group)
6925         (let ((unreads (gnus-group-group-unread)))
6926           (if (and (or (eq t unreads)
6927                        (and unreads (not (zerop unreads))))
6928                    (gnus-summary-read-group
6929                     target-group nil no-article
6930                     (and (buffer-name current-buffer) current-buffer)
6931                     nil backward))
6932               (setq entered t)
6933             (setq current-group target-group
6934                   target-group nil)))))))
6935
6936 (defun gnus-summary-prev-group (&optional no-article)
6937   "Exit current newsgroup and then select previous unread newsgroup.
6938 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6939   (interactive "P")
6940   (gnus-summary-next-group no-article nil t))
6941
6942 ;; Walking around summary lines.
6943
6944 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6945   "Go to the first subject satisfying any non-nil constraint.
6946 If UNREAD is non-nil, the article should be unread.
6947 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6948 If UNSEEN is non-nil, the article should be unseen.
6949 Returns the article selected or nil if there are no matching articles."
6950   (interactive "P")
6951   (cond
6952    ;; Empty summary.
6953    ((null gnus-newsgroup-data)
6954     (gnus-message 3 "No articles in the group")
6955     nil)
6956    ;; Pick the first article.
6957    ((not (or unread undownloaded unseen))
6958     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6959     (gnus-data-number (car gnus-newsgroup-data)))
6960    ;; Find the first unread article.
6961    (t
6962     (let ((data gnus-newsgroup-data))
6963       (while (and data
6964                   (let ((num (gnus-data-number (car data))))
6965                     (or (memq num gnus-newsgroup-unfetched)
6966                         (not (or (and unread
6967                                       (memq num gnus-newsgroup-unreads))
6968                                  (and undownloaded
6969                                       (memq num gnus-newsgroup-undownloaded))
6970                                  (and unseen
6971                                       (memq num gnus-newsgroup-unseen)))))))
6972         (setq data (cdr data)))
6973       (prog1
6974           (if data
6975               (progn
6976                 (goto-char (gnus-data-pos (car data)))
6977                 (gnus-data-number (car data)))
6978             (gnus-message 3 "No more%s articles"
6979                           (let* ((r (when unread " unread"))
6980                                  (d (when undownloaded " undownloaded"))
6981                                  (s (when unseen " unseen"))
6982                                  (l (delq nil (list r d s))))
6983                             (cond ((= 3 (length l))
6984                                    (concat r "," d ", or" s))
6985                                   ((= 2 (length l))
6986                                    (concat (car l) ", or" (cadr l)))
6987                                   ((= 1 (length l))
6988                                    (car l))
6989                                   (t
6990                                    ""))))
6991             nil
6992             )
6993         (gnus-summary-position-point))))))
6994
6995 (defun gnus-summary-next-subject (n &optional unread dont-display)
6996   "Go to next N'th summary line.
6997 If N is negative, go to the previous N'th subject line.
6998 If UNREAD is non-nil, only unread articles are selected.
6999 The difference between N and the actual number of steps taken is
7000 returned."
7001   (interactive "p")
7002   (let ((backward (< n 0))
7003         (n (abs n)))
7004     (while (and (> n 0)
7005                 (if backward
7006                     (gnus-summary-find-prev unread)
7007                   (gnus-summary-find-next unread)))
7008       (unless (zerop (setq n (1- n)))
7009         (gnus-summary-show-thread)))
7010     (when (/= 0 n)
7011       (gnus-message 7 "No more%s articles"
7012                     (if unread " unread" "")))
7013     (unless dont-display
7014       (gnus-summary-recenter)
7015       (gnus-summary-position-point))
7016     n))
7017
7018 (defun gnus-summary-next-unread-subject (n)
7019   "Go to next N'th unread summary line."
7020   (interactive "p")
7021   (gnus-summary-next-subject n t))
7022
7023 (defun gnus-summary-prev-subject (n &optional unread)
7024   "Go to previous N'th summary line.
7025 If optional argument UNREAD is non-nil, only unread article is selected."
7026   (interactive "p")
7027   (gnus-summary-next-subject (- n) unread))
7028
7029 (defun gnus-summary-prev-unread-subject (n)
7030   "Go to previous N'th unread summary line."
7031   (interactive "p")
7032   (gnus-summary-next-subject (- n) t))
7033
7034 (defun gnus-summary-goto-subjects (articles)
7035   "Insert the subject header for ARTICLES in the current buffer."
7036   (save-excursion
7037     (dolist (article articles)
7038       (gnus-summary-goto-subject article t)))
7039   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7040   (gnus-summary-position-point))
7041  
7042 (defun gnus-summary-goto-subject (article &optional force silent)
7043   "Go the subject line of ARTICLE.
7044 If FORCE, also allow jumping to articles not currently shown."
7045   (interactive "nArticle number: ")
7046   (unless (numberp article)
7047     (error "Article %s is not a number" article))
7048   (let ((b (point))
7049         (data (gnus-data-find article)))
7050     ;; We read in the article if we have to.
7051     (and (not data)
7052          force
7053          (gnus-summary-insert-subject
7054           article
7055           (if (or (numberp force) (vectorp force)) force)
7056           t)
7057          (setq data (gnus-data-find article)))
7058     (goto-char b)
7059     (if (not data)
7060         (progn
7061           (unless silent
7062             (gnus-message 3 "Can't find article %d" article))
7063           nil)
7064       (let ((pt (gnus-data-pos data)))
7065         (goto-char pt)
7066         (gnus-summary-set-article-display-arrow pt))
7067       (gnus-summary-position-point)
7068       article)))
7069
7070 ;; Walking around summary lines with displaying articles.
7071
7072 (defun gnus-summary-expand-window (&optional arg)
7073   "Make the summary buffer take up the entire Emacs frame.
7074 Given a prefix, will force an `article' buffer configuration."
7075   (interactive "P")
7076   (if arg
7077       (gnus-configure-windows 'article 'force)
7078     (gnus-configure-windows 'summary 'force)))
7079
7080 (defun gnus-summary-display-article (article &optional all-header)
7081   "Display ARTICLE in article buffer."
7082   (when (gnus-buffer-live-p gnus-article-buffer)
7083     (with-current-buffer gnus-article-buffer
7084       (mm-enable-multibyte)))
7085   (gnus-set-global-variables)
7086   (when (gnus-buffer-live-p gnus-article-buffer)
7087     (with-current-buffer gnus-article-buffer
7088       (setq gnus-article-charset gnus-newsgroup-charset)
7089       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7090       (mm-enable-multibyte)))
7091   (if (null article)
7092       nil
7093     (prog1
7094         (if gnus-summary-display-article-function
7095             (funcall gnus-summary-display-article-function article all-header)
7096           (gnus-article-prepare article all-header))
7097       (gnus-run-hooks 'gnus-select-article-hook)
7098       (when (and gnus-current-article
7099                  (not (zerop gnus-current-article)))
7100         (gnus-summary-goto-subject gnus-current-article))
7101       (gnus-summary-recenter)
7102       (when (and gnus-use-trees gnus-show-threads)
7103         (gnus-possibly-generate-tree article)
7104         (gnus-highlight-selected-tree article))
7105       ;; Successfully display article.
7106       (gnus-article-set-window-start
7107        (cdr (assq article gnus-newsgroup-bookmarks))))))
7108
7109 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7110   "Select the current article.
7111 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7112 non-nil, the article will be re-fetched even if it already present in
7113 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7114 be displayed."
7115   ;; Make sure we are in the summary buffer to work around bbdb bug.
7116   (unless (eq major-mode 'gnus-summary-mode)
7117     (set-buffer gnus-summary-buffer))
7118   (let ((article (or article (gnus-summary-article-number)))
7119         (all-headers (not (not all-headers))) ;Must be t or nil.
7120         gnus-summary-display-article-function)
7121     (and (not pseudo)
7122          (gnus-summary-article-pseudo-p article)
7123          (error "This is a pseudo-article"))
7124     (save-excursion
7125       (set-buffer gnus-summary-buffer)
7126       (if (or (and gnus-single-article-buffer
7127                    (or (null gnus-current-article)
7128                        (null gnus-article-current)
7129                        (null (get-buffer gnus-article-buffer))
7130                        (not (eq article (cdr gnus-article-current)))
7131                        (not (equal (car gnus-article-current)
7132                                    gnus-newsgroup-name))))
7133               (and (not gnus-single-article-buffer)
7134                    (or (null gnus-current-article)
7135                        (not (eq gnus-current-article article))))
7136               force)
7137           ;; The requested article is different from the current article.
7138           (progn
7139             (gnus-summary-display-article article all-headers)
7140             (when (gnus-buffer-live-p gnus-article-buffer)
7141               (with-current-buffer gnus-article-buffer
7142                 (if (not gnus-article-decoded-p) ;; a local variable
7143                     (mm-disable-multibyte))))
7144             (gnus-article-set-window-start
7145              (cdr (assq article gnus-newsgroup-bookmarks)))
7146             article)
7147         'old))))
7148
7149 (defun gnus-summary-force-verify-and-decrypt ()
7150   "Display buttons for signed/encrypted parts and verify/decrypt them."
7151   (interactive)
7152   (let ((mm-verify-option 'known)
7153         (mm-decrypt-option 'known)
7154         (gnus-article-emulate-mime t)
7155         (gnus-buttonized-mime-types (append (list "multipart/signed"
7156                                                   "multipart/encrypted")
7157                                             gnus-buttonized-mime-types)))
7158     (gnus-summary-select-article nil 'force)))
7159
7160 (defun gnus-summary-set-current-mark (&optional current-mark)
7161   "Obsolete function."
7162   nil)
7163
7164 (defun gnus-summary-next-article (&optional unread subject backward push)
7165   "Select the next article.
7166 If UNREAD, only unread articles are selected.
7167 If SUBJECT, only articles with SUBJECT are selected.
7168 If BACKWARD, the previous article is selected instead of the next."
7169   (interactive "P")
7170   (cond
7171    ;; Is there such an article?
7172    ((and (gnus-summary-search-forward unread subject backward)
7173          (or (gnus-summary-display-article (gnus-summary-article-number))
7174              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7175     (gnus-summary-position-point))
7176    ;; If not, we try the first unread, if that is wanted.
7177    ((and subject
7178          gnus-auto-select-same
7179          (gnus-summary-first-unread-article))
7180     (gnus-summary-position-point)
7181     (gnus-message 6 "Wrapped"))
7182    ;; Try to get next/previous article not displayed in this group.
7183    ((and gnus-auto-extend-newsgroup
7184          (not unread) (not subject))
7185     (gnus-summary-goto-article
7186      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7187      nil (count-lines (point-min) (point))))
7188    ;; Go to next/previous group.
7189    (t
7190     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7191       (gnus-summary-jump-to-group gnus-newsgroup-name))
7192     (let ((cmd last-command-char)
7193           (point
7194            (with-current-buffer gnus-group-buffer
7195              (point)))
7196           (group
7197            (if (eq gnus-keep-same-level 'best)
7198                (gnus-summary-best-group gnus-newsgroup-name)
7199              (gnus-summary-search-group backward gnus-keep-same-level))))
7200       ;; For some reason, the group window gets selected.  We change
7201       ;; it back.
7202       (select-window (get-buffer-window (current-buffer)))
7203       ;; Select next unread newsgroup automagically.
7204       (cond
7205        ((or (not gnus-auto-select-next)
7206             (not cmd))
7207         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7208        ((or (eq gnus-auto-select-next 'quietly)
7209             (and (eq gnus-auto-select-next 'slightly-quietly)
7210                  push)
7211             (and (eq gnus-auto-select-next 'almost-quietly)
7212                  (gnus-summary-last-article-p)))
7213         ;; Select quietly.
7214         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7215             (gnus-summary-exit)
7216           (gnus-message 7 "No more%s articles (%s)..."
7217                         (if unread " unread" "")
7218                         (if group (concat "selecting " group)
7219                           "exiting"))
7220           (gnus-summary-next-group nil group backward)))
7221        (t
7222         (when (gnus-key-press-event-p last-input-event)
7223           (gnus-summary-walk-group-buffer
7224            gnus-newsgroup-name cmd unread backward point))))))))
7225
7226 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7227   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7228                       (?\C-p (gnus-group-prev-unread-group 1))))
7229         (cursor-in-echo-area t)
7230         keve key group ended prompt)
7231     (save-excursion
7232       (set-buffer gnus-group-buffer)
7233       (goto-char start)
7234       (setq group
7235             (if (eq gnus-keep-same-level 'best)
7236                 (gnus-summary-best-group gnus-newsgroup-name)
7237               (gnus-summary-search-group backward gnus-keep-same-level))))
7238     (while (not ended)
7239       (setq prompt
7240             (format
7241              "No more%s articles%s " (if unread " unread" "")
7242              (if (and group
7243                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7244                  (format " (Type %s for %s [%s])"
7245                          (single-key-description cmd) group
7246                          (gnus-group-unread group))
7247                (format " (Type %s to exit %s)"
7248                        (single-key-description cmd)
7249                        gnus-newsgroup-name))))
7250       ;; Confirm auto selection.
7251       (setq key (car (setq keve (gnus-read-event-char prompt)))
7252             ended t)
7253       (cond
7254        ((assq key keystrokes)
7255         (let ((obuf (current-buffer)))
7256           (switch-to-buffer gnus-group-buffer)
7257           (when group
7258             (gnus-group-jump-to-group group))
7259           (eval (cadr (assq key keystrokes)))
7260           (setq group (gnus-group-group-name))
7261           (switch-to-buffer obuf))
7262         (setq ended nil))
7263        ((equal key cmd)
7264         (if (or (not group)
7265                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7266             (gnus-summary-exit)
7267           (gnus-summary-next-group nil group backward)))
7268        (t
7269         (push (cdr keve) unread-command-events))))))
7270
7271 (defun gnus-summary-next-unread-article ()
7272   "Select unread article after current one."
7273   (interactive)
7274   (gnus-summary-next-article
7275    (or (not (eq gnus-summary-goto-unread 'never))
7276        (gnus-summary-last-article-p (gnus-summary-article-number)))
7277    (and gnus-auto-select-same
7278         (gnus-summary-article-subject))))
7279
7280 (defun gnus-summary-prev-article (&optional unread subject)
7281   "Select the article after the current one.
7282 If UNREAD is non-nil, only unread articles are selected."
7283   (interactive "P")
7284   (gnus-summary-next-article unread subject t))
7285
7286 (defun gnus-summary-prev-unread-article ()
7287   "Select unread article before current one."
7288   (interactive)
7289   (gnus-summary-prev-article
7290    (or (not (eq gnus-summary-goto-unread 'never))
7291        (gnus-summary-first-article-p (gnus-summary-article-number)))
7292    (and gnus-auto-select-same
7293         (gnus-summary-article-subject))))
7294
7295 (defun gnus-summary-next-page (&optional lines circular stop)
7296   "Show next page of the selected article.
7297 If at the end of the current article, select the next article.
7298 LINES says how many lines should be scrolled up.
7299
7300 If CIRCULAR is non-nil, go to the start of the article instead of
7301 selecting the next article when reaching the end of the current
7302 article.
7303
7304 If STOP is non-nil, just stop when reaching the end of the message.
7305
7306 Also see the variable `gnus-article-skip-boring'."
7307   (interactive "P")
7308   (setq gnus-summary-buffer (current-buffer))
7309   (gnus-set-global-variables)
7310   (let ((article (gnus-summary-article-number))
7311         (article-window (get-buffer-window gnus-article-buffer t))
7312         endp)
7313     ;; If the buffer is empty, we have no article.
7314     (unless article
7315       (error "No article to select"))
7316     (gnus-configure-windows 'article)
7317     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7318         (if (and (eq gnus-summary-goto-unread 'never)
7319                  (not (gnus-summary-last-article-p article)))
7320             (gnus-summary-next-article)
7321           (gnus-summary-next-unread-article))
7322       (if (or (null gnus-current-article)
7323               (null gnus-article-current)
7324               (/= article (cdr gnus-article-current))
7325               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7326           ;; Selected subject is different from current article's.
7327           (gnus-summary-display-article article)
7328         (when article-window
7329           (gnus-eval-in-buffer-window gnus-article-buffer
7330             (setq endp (or (gnus-article-next-page lines)
7331                            (gnus-article-only-boring-p))))
7332           (when endp
7333             (cond (stop
7334                    (gnus-message 3 "End of message"))
7335                   (circular
7336                    (gnus-summary-beginning-of-article))
7337                   (lines
7338                    (gnus-message 3 "End of message"))
7339                   ((null lines)
7340                    (if (and (eq gnus-summary-goto-unread 'never)
7341                             (not (gnus-summary-last-article-p article)))
7342                        (gnus-summary-next-article)
7343                      (gnus-summary-next-unread-article))))))))
7344     (gnus-summary-recenter)
7345     (gnus-summary-position-point)))
7346
7347 (defun gnus-summary-prev-page (&optional lines move)
7348   "Show previous page of selected article.
7349 Argument LINES specifies lines to be scrolled down.
7350 If MOVE, move to the previous unread article if point is at
7351 the beginning of the buffer."
7352   (interactive "P")
7353   (let ((article (gnus-summary-article-number))
7354         (article-window (get-buffer-window gnus-article-buffer t))
7355         endp)
7356     (gnus-configure-windows 'article)
7357     (if (or (null gnus-current-article)
7358             (null gnus-article-current)
7359             (/= article (cdr gnus-article-current))
7360             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7361         ;; Selected subject is different from current article's.
7362         (gnus-summary-display-article article)
7363       (gnus-summary-recenter)
7364       (when article-window
7365         (gnus-eval-in-buffer-window gnus-article-buffer
7366           (setq endp (gnus-article-prev-page lines)))
7367         (when (and move endp)
7368           (cond (lines
7369                  (gnus-message 3 "Beginning of message"))
7370                 ((null lines)
7371                  (if (and (eq gnus-summary-goto-unread 'never)
7372                           (not (gnus-summary-first-article-p article)))
7373                      (gnus-summary-prev-article)
7374                    (gnus-summary-prev-unread-article))))))))
7375   (gnus-summary-position-point))
7376
7377 (defun gnus-summary-prev-page-or-article (&optional lines)
7378   "Show previous page of selected article.
7379 Argument LINES specifies lines to be scrolled down.
7380 If at the beginning of the article, go to the next article."
7381   (interactive "P")
7382   (gnus-summary-prev-page lines t))
7383
7384 (defun gnus-summary-scroll-up (lines)
7385   "Scroll up (or down) one line current article.
7386 Argument LINES specifies lines to be scrolled up (or down if negative)."
7387   (interactive "p")
7388   (gnus-configure-windows 'article)
7389   (gnus-summary-show-thread)
7390   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7391     (gnus-eval-in-buffer-window gnus-article-buffer
7392       (cond ((> lines 0)
7393              (when (gnus-article-next-page lines)
7394                (gnus-message 3 "End of message")))
7395             ((< lines 0)
7396              (gnus-article-prev-page (- lines))))))
7397   (gnus-summary-recenter)
7398   (gnus-summary-position-point))
7399
7400 (defun gnus-summary-scroll-down (lines)
7401   "Scroll down (or up) one line current article.
7402 Argument LINES specifies lines to be scrolled down (or up if negative)."
7403   (interactive "p")
7404   (gnus-summary-scroll-up (- lines)))
7405
7406 (defun gnus-summary-next-same-subject ()
7407   "Select next article which has the same subject as current one."
7408   (interactive)
7409   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7410
7411 (defun gnus-summary-prev-same-subject ()
7412   "Select previous article which has the same subject as current one."
7413   (interactive)
7414   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7415
7416 (defun gnus-summary-next-unread-same-subject ()
7417   "Select next unread article which has the same subject as current one."
7418   (interactive)
7419   (gnus-summary-next-article t (gnus-summary-article-subject)))
7420
7421 (defun gnus-summary-prev-unread-same-subject ()
7422   "Select previous unread article which has the same subject as current one."
7423   (interactive)
7424   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7425
7426 (defun gnus-summary-first-unread-article ()
7427   "Select the first unread article.
7428 Return nil if there are no unread articles."
7429   (interactive)
7430   (prog1
7431       (when (gnus-summary-first-subject t)
7432         (gnus-summary-show-thread)
7433         (gnus-summary-first-subject t)
7434         (gnus-summary-display-article (gnus-summary-article-number)))
7435     (gnus-summary-position-point)))
7436
7437 (defun gnus-summary-first-unread-subject ()
7438   "Place the point on the subject line of the first unread article.
7439 Return nil if there are no unread articles."
7440   (interactive)
7441   (prog1
7442       (when (gnus-summary-first-subject t)
7443         (gnus-summary-show-thread)
7444         (gnus-summary-first-subject t))
7445     (gnus-summary-position-point)))
7446
7447 (defun gnus-summary-first-unseen-subject ()
7448   "Place the point on the subject line of the first unseen article.
7449 Return nil if there are no unseen articles."
7450   (interactive)
7451   (prog1
7452       (when (gnus-summary-first-subject nil nil t)
7453         (gnus-summary-show-thread)
7454         (gnus-summary-first-subject nil nil t))
7455     (gnus-summary-position-point)))
7456
7457 (defun gnus-summary-first-unseen-or-unread-subject ()
7458   "Place the point on the subject line of the first unseen article or,
7459 if all article have been seen, on the subject line of the first unread
7460 article."
7461   (interactive)
7462   (prog1
7463       (unless (when (gnus-summary-first-subject nil nil t)
7464                 (gnus-summary-show-thread)
7465                 (gnus-summary-first-subject nil nil t))
7466         (when (gnus-summary-first-subject t)
7467           (gnus-summary-show-thread)
7468           (gnus-summary-first-subject t)))
7469     (gnus-summary-position-point)))
7470
7471 (defun gnus-summary-first-article ()
7472   "Select the first article.
7473 Return nil if there are no articles."
7474   (interactive)
7475   (prog1
7476       (when (gnus-summary-first-subject)
7477         (gnus-summary-show-thread)
7478         (gnus-summary-first-subject)
7479         (gnus-summary-display-article (gnus-summary-article-number)))
7480     (gnus-summary-position-point)))
7481
7482 (defun gnus-summary-best-unread-article (&optional arg)
7483   "Select the unread article with the highest score.
7484 If given a prefix argument, select the next unread article that has a
7485 score higher than the default score."
7486   (interactive "P")
7487   (let ((article (if arg
7488                      (gnus-summary-better-unread-subject)
7489                    (gnus-summary-best-unread-subject))))
7490     (if article
7491         (gnus-summary-goto-article article)
7492       (error "No unread articles"))))
7493
7494 (defun gnus-summary-best-unread-subject ()
7495   "Select the unread subject with the highest score."
7496   (interactive)
7497   (let ((best -1000000)
7498         (data gnus-newsgroup-data)
7499         article score)
7500     (while data
7501       (and (gnus-data-unread-p (car data))
7502            (> (setq score
7503                     (gnus-summary-article-score (gnus-data-number (car data))))
7504               best)
7505            (setq best score
7506                  article (gnus-data-number (car data))))
7507       (setq data (cdr data)))
7508     (when article
7509       (gnus-summary-goto-subject article))
7510     (gnus-summary-position-point)
7511     article))
7512
7513 (defun gnus-summary-better-unread-subject ()
7514   "Select the first unread subject that has a score over the default score."
7515   (interactive)
7516   (let ((data gnus-newsgroup-data)
7517         article score)
7518     (while (and (setq article (gnus-data-number (car data)))
7519                 (or (gnus-data-read-p (car data))
7520                     (not (> (gnus-summary-article-score article)
7521                             gnus-summary-default-score))))
7522       (setq data (cdr data)))
7523     (when article
7524       (gnus-summary-goto-subject article))
7525     (gnus-summary-position-point)
7526     article))
7527
7528 (defun gnus-summary-last-subject ()
7529   "Go to the last displayed subject line in the group."
7530   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7531     (when article
7532       (gnus-summary-goto-subject article))))
7533
7534 (defun gnus-summary-goto-article (article &optional all-headers force)
7535   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7536 If ALL-HEADERS is non-nil, no header lines are hidden.
7537 If FORCE, go to the article even if it isn't displayed.  If FORCE
7538 is a number, it is the line the article is to be displayed on."
7539   (interactive
7540    (list
7541     (completing-read
7542      "Article number or Message-ID: "
7543      (mapcar (lambda (number) (list (int-to-string number)))
7544              gnus-newsgroup-limit))
7545     current-prefix-arg
7546     t))
7547   (prog1
7548       (if (and (stringp article)
7549                (string-match "@\\|%40" article))
7550           (gnus-summary-refer-article article)
7551         (when (stringp article)
7552           (setq article (string-to-number article)))
7553         (if (gnus-summary-goto-subject article force)
7554             (gnus-summary-display-article article all-headers)
7555           (gnus-message 4 "Couldn't go to article %s" article) nil))
7556     (gnus-summary-position-point)))
7557
7558 (defun gnus-summary-goto-last-article ()
7559   "Go to the previously read article."
7560   (interactive)
7561   (prog1
7562       (when gnus-last-article
7563         (gnus-summary-goto-article gnus-last-article nil t))
7564     (gnus-summary-position-point)))
7565
7566 (defun gnus-summary-pop-article (number)
7567   "Pop one article off the history and go to the previous.
7568 NUMBER articles will be popped off."
7569   (interactive "p")
7570   (let (to)
7571     (setq gnus-newsgroup-history
7572           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7573     (if to
7574         (gnus-summary-goto-article (car to) nil t)
7575       (error "Article history empty")))
7576   (gnus-summary-position-point))
7577
7578 ;; Summary commands and functions for limiting the summary buffer.
7579
7580 (defun gnus-summary-limit-to-articles (n)
7581   "Limit the summary buffer to the next N articles.
7582 If not given a prefix, use the process marked articles instead."
7583   (interactive "P")
7584   (prog1
7585       (let ((articles (gnus-summary-work-articles n)))
7586         (setq gnus-newsgroup-processable nil)
7587         (gnus-summary-limit articles))
7588     (gnus-summary-position-point)))
7589
7590 (defun gnus-summary-pop-limit (&optional total)
7591   "Restore the previous limit.
7592 If given a prefix, remove all limits."
7593   (interactive "P")
7594   (when total
7595     (setq gnus-newsgroup-limits
7596           (list (mapcar (lambda (h) (mail-header-number h))
7597                         gnus-newsgroup-headers))))
7598   (unless gnus-newsgroup-limits
7599     (error "No limit to pop"))
7600   (prog1
7601       (gnus-summary-limit nil 'pop)
7602     (gnus-summary-position-point)))
7603
7604 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7605   "Limit the summary buffer to articles that have subjects that match a regexp.
7606 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7607   (interactive
7608    (list (read-string (if current-prefix-arg
7609                           "Exclude subject (regexp): "
7610                         "Limit to subject (regexp): "))
7611          nil current-prefix-arg))
7612   (unless header
7613     (setq header "subject"))
7614   (when (not (equal "" subject))
7615     (prog1
7616         (let ((articles (gnus-summary-find-matching
7617                          (or header "subject") subject 'all nil nil
7618                          not-matching)))
7619           (unless articles
7620             (error "Found no matches for \"%s\"" subject))
7621           (gnus-summary-limit articles))
7622       (gnus-summary-position-point))))
7623
7624 (defun gnus-summary-limit-to-author (from &optional not-matching)
7625   "Limit the summary buffer to articles that have authors that match a regexp.
7626 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7627   (interactive
7628    (list (read-string (if current-prefix-arg
7629                           "Exclude author (regexp): "
7630                         "Limit to author (regexp): "))
7631          current-prefix-arg))
7632   (gnus-summary-limit-to-subject from "from" not-matching))
7633
7634 (defun gnus-summary-limit-to-age (age &optional younger-p)
7635   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7636 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7637 articles that are younger than AGE days."
7638   (interactive
7639    (let ((younger current-prefix-arg)
7640          (days-got nil)
7641          days)
7642      (while (not days-got)
7643        (setq days (if younger
7644                       (read-string "Limit to articles younger than (in days, older when negative): ")
7645                     (read-string
7646                      "Limit to articles older than (in days, younger when negative): ")))
7647        (when (> (length days) 0)
7648          (setq days (read days)))
7649        (if (numberp days)
7650            (progn
7651              (setq days-got t)
7652              (when (< days 0)
7653                (setq younger (not younger))
7654                (setq days (* days -1))))
7655          (message "Please enter a number.")
7656          (sleep-for 1)))
7657      (list days younger)))
7658   (prog1
7659       (let ((data gnus-newsgroup-data)
7660             (cutoff (days-to-time age))
7661             articles d date is-younger)
7662         (while (setq d (pop data))
7663           (when (and (vectorp (gnus-data-header d))
7664                      (setq date (mail-header-date (gnus-data-header d))))
7665             (setq is-younger (time-less-p
7666                               (time-since (condition-case ()
7667                                               (date-to-time date)
7668                                             (error '(0 0))))
7669                               cutoff))
7670             (when (if younger-p
7671                       is-younger
7672                     (not is-younger))
7673               (push (gnus-data-number d) articles))))
7674         (gnus-summary-limit (nreverse articles)))
7675     (gnus-summary-position-point)))
7676
7677 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7678   "Limit the summary buffer to articles that match an 'extra' header."
7679   (interactive
7680    (let ((header
7681           (intern
7682            (gnus-completing-read-with-default
7683             (symbol-name (car gnus-extra-headers))
7684             (if current-prefix-arg
7685                 "Exclude extra header:"
7686               "Limit extra header:")
7687             (mapcar (lambda (x)
7688                       (cons (symbol-name x) x))
7689                     gnus-extra-headers)
7690             nil
7691             t))))
7692      (list header
7693            (read-string (format "%s header %s (regexp): "
7694                                 (if current-prefix-arg "Exclude" "Limit to")
7695                                 header))
7696            current-prefix-arg)))
7697   (when (not (equal "" regexp))
7698     (prog1
7699         (let ((articles (gnus-summary-find-matching
7700                          (cons 'extra header) regexp 'all nil nil
7701                          not-matching)))
7702           (unless articles
7703             (error "Found no matches for \"%s\"" regexp))
7704           (gnus-summary-limit articles))
7705       (gnus-summary-position-point))))
7706
7707 (defun gnus-summary-limit-to-display-predicate ()
7708   "Limit the summary buffer to the predicated in the `display' group parameter."
7709   (interactive)
7710   (unless gnus-newsgroup-display
7711     (error "There is no `display' group parameter"))
7712   (let (articles)
7713     (dolist (number gnus-newsgroup-articles)
7714       (when (funcall gnus-newsgroup-display)
7715         (push number articles)))
7716     (gnus-summary-limit articles))
7717   (gnus-summary-position-point))
7718
7719 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7720 (make-obsolete
7721  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7722
7723 (defun gnus-summary-limit-to-unread (&optional all)
7724   "Limit the summary buffer to articles that are not marked as read.
7725 If ALL is non-nil, limit strictly to unread articles."
7726   (interactive "P")
7727   (if all
7728       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7729     (gnus-summary-limit-to-marks
7730      ;; Concat all the marks that say that an article is read and have
7731      ;; those removed.
7732      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7733            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7734            gnus-low-score-mark gnus-expirable-mark
7735            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7736            gnus-duplicate-mark gnus-souped-mark)
7737      'reverse)))
7738
7739 (defun gnus-summary-limit-to-replied (&optional unreplied)
7740   "Limit the summary buffer to replied articles.
7741 If UNREPLIED (the prefix), limit to unreplied articles."
7742   (interactive "P")
7743   (if unreplied
7744       (gnus-summary-limit
7745        (gnus-set-difference gnus-newsgroup-articles
7746         gnus-newsgroup-replied))
7747     (gnus-summary-limit gnus-newsgroup-replied))
7748   (gnus-summary-position-point))
7749
7750 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7751 (make-obsolete 'gnus-summary-delete-marked-with
7752                'gnus-summary-limit-exclude-marks)
7753
7754 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7755   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7756 If REVERSE, limit the summary buffer to articles that are marked
7757 with MARKS.  MARKS can either be a string of marks or a list of marks.
7758 Returns how many articles were removed."
7759   (interactive "sMarks: ")
7760   (gnus-summary-limit-to-marks marks t))
7761
7762 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7763   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7764 If REVERSE (the prefix), limit the summary buffer to articles that are
7765 not marked with MARKS.  MARKS can either be a string of marks or a
7766 list of marks.
7767 Returns how many articles were removed."
7768   (interactive "sMarks: \nP")
7769   (prog1
7770       (let ((data gnus-newsgroup-data)
7771             (marks (if (listp marks) marks
7772                      (append marks nil))) ; Transform to list.
7773             articles)
7774         (while data
7775           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7776                   (memq (gnus-data-mark (car data)) marks))
7777             (push (gnus-data-number (car data)) articles))
7778           (setq data (cdr data)))
7779         (gnus-summary-limit articles))
7780     (gnus-summary-position-point)))
7781
7782 (defun gnus-summary-limit-to-score (score)
7783   "Limit to articles with score at or above SCORE."
7784   (interactive "NLimit to articles with score of at least: ")
7785   (let ((data gnus-newsgroup-data)
7786         articles)
7787     (while data
7788       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7789                 score)
7790         (push (gnus-data-number (car data)) articles))
7791       (setq data (cdr data)))
7792     (prog1
7793         (gnus-summary-limit articles)
7794       (gnus-summary-position-point))))
7795
7796 (defun gnus-summary-limit-to-unseen ()
7797   "Limit to unseen articles."
7798   (interactive)
7799   (prog1
7800       (gnus-summary-limit gnus-newsgroup-unseen)
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-limit-include-thread (id)
7804   "Display all the hidden articles that is in the thread with ID in it.
7805 When called interactively, ID is the Message-ID of the current
7806 article."
7807   (interactive (list (mail-header-id (gnus-summary-article-header))))
7808   (let ((articles (gnus-articles-in-thread
7809                    (gnus-id-to-thread (gnus-root-id id)))))
7810     (prog1
7811         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7812       (gnus-summary-limit-include-matching-articles
7813        "subject"
7814        (regexp-quote (gnus-simplify-subject-re
7815                       (mail-header-subject (gnus-id-to-header id)))))
7816       (gnus-summary-position-point))))
7817
7818 (defun gnus-summary-limit-include-matching-articles (header regexp)
7819   "Display all the hidden articles that have HEADERs that match REGEXP."
7820   (interactive (list (read-string "Match on header: ")
7821                      (read-string "Regexp: ")))
7822   (let ((articles (gnus-find-matching-articles header regexp)))
7823     (prog1
7824         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7825       (gnus-summary-position-point))))
7826
7827 (defun gnus-summary-insert-dormant-articles ()
7828   "Insert all the dormant articles for this group into the current buffer."
7829   (interactive)
7830   (let ((gnus-verbose (max 6 gnus-verbose)))
7831     (if (not gnus-newsgroup-dormant)
7832         (gnus-message 3 "No cached articles for this group")
7833       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7834
7835 (defun gnus-summary-limit-include-dormant ()
7836   "Display all the hidden articles that are marked as dormant.
7837 Note that this command only works on a subset of the articles currently
7838 fetched for this group."
7839   (interactive)
7840   (unless gnus-newsgroup-dormant
7841     (error "There are no dormant articles in this group"))
7842   (prog1
7843       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7844     (gnus-summary-position-point)))
7845
7846 (defun gnus-summary-limit-exclude-dormant ()
7847   "Hide all dormant articles."
7848   (interactive)
7849   (prog1
7850       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7851     (gnus-summary-position-point)))
7852
7853 (defun gnus-summary-limit-exclude-childless-dormant ()
7854   "Hide all dormant articles that have no children."
7855   (interactive)
7856   (let ((data (gnus-data-list t))
7857         articles d children)
7858     ;; Find all articles that are either not dormant or have
7859     ;; children.
7860     (while (setq d (pop data))
7861       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7862                 (and (setq children
7863                            (gnus-article-children (gnus-data-number d)))
7864                      (let (found)
7865                        (while children
7866                          (when (memq (car children) articles)
7867                            (setq children nil
7868                                  found t))
7869                          (pop children))
7870                        found)))
7871         (push (gnus-data-number d) articles)))
7872     ;; Do the limiting.
7873     (prog1
7874         (gnus-summary-limit articles)
7875       (gnus-summary-position-point))))
7876
7877 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7878   "Mark all unread excluded articles as read.
7879 If ALL, mark even excluded ticked and dormants as read."
7880   (interactive "P")
7881   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7882   (let ((articles (gnus-sorted-ndifference
7883                    (sort
7884                     (mapcar (lambda (h) (mail-header-number h))
7885                             gnus-newsgroup-headers)
7886                     '<)
7887                    gnus-newsgroup-limit))
7888         article)
7889     (setq gnus-newsgroup-unreads
7890           (gnus-sorted-intersection gnus-newsgroup-unreads
7891                                     gnus-newsgroup-limit))
7892     (if all
7893         (setq gnus-newsgroup-dormant nil
7894               gnus-newsgroup-marked nil
7895               gnus-newsgroup-reads
7896               (nconc
7897                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7898                gnus-newsgroup-reads))
7899       (while (setq article (pop articles))
7900         (unless (or (memq article gnus-newsgroup-dormant)
7901                     (memq article gnus-newsgroup-marked))
7902           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7903
7904 (defun gnus-summary-limit (articles &optional pop)
7905   (if pop
7906       ;; We pop the previous limit off the stack and use that.
7907       (setq articles (car gnus-newsgroup-limits)
7908             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7909     ;; We use the new limit, so we push the old limit on the stack.
7910     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7911   ;; Set the limit.
7912   (setq gnus-newsgroup-limit articles)
7913   (let ((total (length gnus-newsgroup-data))
7914         (data (gnus-data-find-list (gnus-summary-article-number)))
7915         (gnus-summary-mark-below nil)   ; Inhibit this.
7916         found)
7917     ;; This will do all the work of generating the new summary buffer
7918     ;; according to the new limit.
7919     (gnus-summary-prepare)
7920     ;; Hide any threads, possibly.
7921     (gnus-summary-maybe-hide-threads)
7922     ;; Try to return to the article you were at, or one in the
7923     ;; neighborhood.
7924     (when data
7925       ;; We try to find some article after the current one.
7926       (while data
7927         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7928           (setq data nil
7929                 found t))
7930         (setq data (cdr data))))
7931     (unless found
7932       ;; If there is no data, that means that we were after the last
7933       ;; article.  The same goes when we can't find any articles
7934       ;; after the current one.
7935       (goto-char (point-max))
7936       (gnus-summary-find-prev))
7937     (gnus-set-mode-line 'summary)
7938     ;; We return how many articles were removed from the summary
7939     ;; buffer as a result of the new limit.
7940     (- total (length gnus-newsgroup-data))))
7941
7942 (defsubst gnus-invisible-cut-children (threads)
7943   (let ((num 0))
7944     (while threads
7945       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7946         (incf num))
7947       (pop threads))
7948     (< num 2)))
7949
7950 (defsubst gnus-cut-thread (thread)
7951   "Go forwards in the thread until we find an article that we want to display."
7952   (when (or (eq gnus-fetch-old-headers 'some)
7953             (eq gnus-fetch-old-headers 'invisible)
7954             (numberp gnus-fetch-old-headers)
7955             (eq gnus-build-sparse-threads 'some)
7956             (eq gnus-build-sparse-threads 'more))
7957     ;; Deal with old-fetched headers and sparse threads.
7958     (while (and
7959             thread
7960             (or
7961              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7962              (gnus-summary-article-ancient-p
7963               (mail-header-number (car thread))))
7964             (if (or (<= (length (cdr thread)) 1)
7965                     (eq gnus-fetch-old-headers 'invisible))
7966                 (setq gnus-newsgroup-limit
7967                       (delq (mail-header-number (car thread))
7968                             gnus-newsgroup-limit)
7969                       thread (cadr thread))
7970               (when (gnus-invisible-cut-children (cdr thread))
7971                 (let ((th (cdr thread)))
7972                   (while th
7973                     (if (memq (mail-header-number (caar th))
7974                               gnus-newsgroup-limit)
7975                         (setq thread (car th)
7976                               th nil)
7977                       (setq th (cdr th))))))))))
7978   thread)
7979
7980 (defun gnus-cut-threads (threads)
7981   "Cut off all uninteresting articles from the beginning of THREADS."
7982   (when (or (eq gnus-fetch-old-headers 'some)
7983             (eq gnus-fetch-old-headers 'invisible)
7984             (numberp gnus-fetch-old-headers)
7985             (eq gnus-build-sparse-threads 'some)
7986             (eq gnus-build-sparse-threads 'more))
7987     (let ((th threads))
7988       (while th
7989         (setcar th (gnus-cut-thread (car th)))
7990         (setq th (cdr th)))))
7991   ;; Remove nixed out threads.
7992   (delq nil threads))
7993
7994 (defun gnus-summary-initial-limit (&optional show-if-empty)
7995   "Figure out what the initial limit is supposed to be on group entry.
7996 This entails weeding out unwanted dormants, low-scored articles,
7997 fetch-old-headers verbiage, and so on."
7998   ;; Most groups have nothing to remove.
7999   (if (or gnus-inhibit-limiting
8000           (and (null gnus-newsgroup-dormant)
8001                (eq gnus-newsgroup-display 'gnus-not-ignore)
8002                (not (eq gnus-fetch-old-headers 'some))
8003                (not (numberp gnus-fetch-old-headers))
8004                (not (eq gnus-fetch-old-headers 'invisible))
8005                (null gnus-summary-expunge-below)
8006                (not (eq gnus-build-sparse-threads 'some))
8007                (not (eq gnus-build-sparse-threads 'more))
8008                (null gnus-thread-expunge-below)
8009                (not gnus-use-nocem)))
8010       ()                                ; Do nothing.
8011     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8012     (setq gnus-newsgroup-limit nil)
8013     (mapatoms
8014      (lambda (node)
8015        (unless (car (symbol-value node))
8016          ;; These threads have no parents -- they are roots.
8017          (let ((nodes (cdr (symbol-value node)))
8018                thread)
8019            (while nodes
8020              (if (and gnus-thread-expunge-below
8021                       (< (gnus-thread-total-score (car nodes))
8022                          gnus-thread-expunge-below))
8023                  (gnus-expunge-thread (pop nodes))
8024                (setq thread (pop nodes))
8025                (gnus-summary-limit-children thread))))))
8026      gnus-newsgroup-dependencies)
8027     ;; If this limitation resulted in an empty group, we might
8028     ;; pop the previous limit and use it instead.
8029     (when (and (not gnus-newsgroup-limit)
8030                show-if-empty)
8031       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8032     gnus-newsgroup-limit))
8033
8034 (defun gnus-summary-limit-children (thread)
8035   "Return 1 if this subthread is visible and 0 if it is not."
8036   ;; First we get the number of visible children to this thread.  This
8037   ;; is done by recursing down the thread using this function, so this
8038   ;; will really go down to a leaf article first, before slowly
8039   ;; working its way up towards the root.
8040   (when thread
8041     (let* ((max-lisp-eval-depth 5000)
8042            (children
8043            (if (cdr thread)
8044                (apply '+ (mapcar 'gnus-summary-limit-children
8045                                  (cdr thread)))
8046              0))
8047           (number (mail-header-number (car thread)))
8048           score)
8049       (if (and
8050            (not (memq number gnus-newsgroup-marked))
8051            (or
8052             ;; If this article is dormant and has absolutely no visible
8053             ;; children, then this article isn't visible.
8054             (and (memq number gnus-newsgroup-dormant)
8055                  (zerop children))
8056             ;; If this is "fetch-old-headered" and there is no
8057             ;; visible children, then we don't want this article.
8058             (and (or (eq gnus-fetch-old-headers 'some)
8059                      (numberp gnus-fetch-old-headers))
8060                  (gnus-summary-article-ancient-p number)
8061                  (zerop children))
8062             ;; If this is "fetch-old-headered" and `invisible', then
8063             ;; we don't want this article.
8064             (and (eq gnus-fetch-old-headers 'invisible)
8065                  (gnus-summary-article-ancient-p number))
8066             ;; If this is a sparsely inserted article with no children,
8067             ;; we don't want it.
8068             (and (eq gnus-build-sparse-threads 'some)
8069                  (gnus-summary-article-sparse-p number)
8070                  (zerop children))
8071             ;; If we use expunging, and this article is really
8072             ;; low-scored, then we don't want this article.
8073             (when (and gnus-summary-expunge-below
8074                        (< (setq score
8075                                 (or (cdr (assq number gnus-newsgroup-scored))
8076                                     gnus-summary-default-score))
8077                           gnus-summary-expunge-below))
8078               ;; We increase the expunge-tally here, but that has
8079               ;; nothing to do with the limits, really.
8080               (incf gnus-newsgroup-expunged-tally)
8081               ;; We also mark as read here, if that's wanted.
8082               (when (and gnus-summary-mark-below
8083                          (< score gnus-summary-mark-below))
8084                 (setq gnus-newsgroup-unreads
8085                       (delq number gnus-newsgroup-unreads))
8086                 (if gnus-newsgroup-auto-expire
8087                     (push number gnus-newsgroup-expirable)
8088                   (push (cons number gnus-low-score-mark)
8089                         gnus-newsgroup-reads)))
8090               t)
8091             ;; Do the `display' group parameter.
8092             (and gnus-newsgroup-display
8093                  (not (funcall gnus-newsgroup-display)))
8094             ;; Check NoCeM things.
8095             (when (and gnus-use-nocem
8096                        (gnus-nocem-unwanted-article-p
8097                         (mail-header-id (car thread))))
8098               (setq gnus-newsgroup-unreads
8099                     (delq number gnus-newsgroup-unreads))
8100               t)))
8101           ;; Nope, invisible article.
8102           0
8103         ;; Ok, this article is to be visible, so we add it to the limit
8104         ;; and return 1.
8105         (push number gnus-newsgroup-limit)
8106         1))))
8107
8108 (defun gnus-expunge-thread (thread)
8109   "Mark all articles in THREAD as read."
8110   (let* ((number (mail-header-number (car thread))))
8111     (incf gnus-newsgroup-expunged-tally)
8112     ;; We also mark as read here, if that's wanted.
8113     (setq gnus-newsgroup-unreads
8114           (delq number gnus-newsgroup-unreads))
8115     (if gnus-newsgroup-auto-expire
8116         (push number gnus-newsgroup-expirable)
8117       (push (cons number gnus-low-score-mark)
8118             gnus-newsgroup-reads)))
8119   ;; Go recursively through all subthreads.
8120   (mapcar 'gnus-expunge-thread (cdr thread)))
8121
8122 ;; Summary article oriented commands
8123
8124 (defun gnus-summary-refer-parent-article (n)
8125   "Refer parent article N times.
8126 If N is negative, go to ancestor -N instead.
8127 The difference between N and the number of articles fetched is returned."
8128   (interactive "p")
8129   (let ((skip 1)
8130         error header ref)
8131     (when (not (natnump n))
8132       (setq skip (abs n)
8133             n 1))
8134     (while (and (> n 0)
8135                 (not error))
8136       (setq header (gnus-summary-article-header))
8137       (if (and (eq (mail-header-number header)
8138                    (cdr gnus-article-current))
8139                (equal gnus-newsgroup-name
8140                       (car gnus-article-current)))
8141           ;; If we try to find the parent of the currently
8142           ;; displayed article, then we take a look at the actual
8143           ;; References header, since this is slightly more
8144           ;; reliable than the References field we got from the
8145           ;; server.
8146           (save-excursion
8147             (set-buffer gnus-original-article-buffer)
8148             (nnheader-narrow-to-headers)
8149             (unless (setq ref (message-fetch-field "references"))
8150               (when (setq ref (message-fetch-field "in-reply-to"))
8151                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8152             (widen))
8153         (setq ref
8154               ;; It's not the current article, so we take a bet on
8155               ;; the value we got from the server.
8156               (mail-header-references header)))
8157       (if (and ref
8158                (not (equal ref "")))
8159           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8160             (gnus-message 1 "Couldn't find parent"))
8161         (gnus-message 1 "No references in article %d"
8162                       (gnus-summary-article-number))
8163         (setq error t))
8164       (decf n))
8165     (gnus-summary-position-point)
8166     n))
8167
8168 (defun gnus-summary-refer-references ()
8169   "Fetch all articles mentioned in the References header.
8170 Return the number of articles fetched."
8171   (interactive)
8172   (let ((ref (mail-header-references (gnus-summary-article-header)))
8173         (current (gnus-summary-article-number))
8174         (n 0))
8175     (if (or (not ref)
8176             (equal ref ""))
8177         (error "No References in the current article")
8178       ;; For each Message-ID in the References header...
8179       (while (string-match "<[^>]*>" ref)
8180         (incf n)
8181         ;; ... fetch that article.
8182         (gnus-summary-refer-article
8183          (prog1 (match-string 0 ref)
8184            (setq ref (substring ref (match-end 0))))))
8185       (gnus-summary-goto-subject current)
8186       (gnus-summary-position-point)
8187       n)))
8188
8189 (defun gnus-summary-refer-thread (&optional limit)
8190   "Fetch all articles in the current thread.
8191 If LIMIT (the numerical prefix), fetch that many old headers instead
8192 of what's specified by the `gnus-refer-thread-limit' variable."
8193   (interactive "P")
8194   (let ((id (mail-header-id (gnus-summary-article-header)))
8195         (limit (if limit (prefix-numeric-value limit)
8196                  gnus-refer-thread-limit)))
8197     (unless (eq gnus-fetch-old-headers 'invisible)
8198       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8199       ;; Retrieve the headers and read them in.
8200       (if (eq (if (numberp limit)
8201                   (gnus-retrieve-headers
8202                    (list (min
8203                           (+ (mail-header-number
8204                               (gnus-summary-article-header))
8205                              limit)
8206                           gnus-newsgroup-end))
8207                    gnus-newsgroup-name (* limit 2))
8208                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8209                 ;; headers.
8210                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8211                                        gnus-newsgroup-name limit))
8212               'nov)
8213           (gnus-build-all-threads)
8214         (error "Can't fetch thread from back ends that don't support NOV"))
8215       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8216     (gnus-summary-limit-include-thread id)))
8217
8218 (defun gnus-summary-refer-article (message-id)
8219   "Fetch an article specified by MESSAGE-ID."
8220   (interactive "sMessage-ID: ")
8221   (when (and (stringp message-id)
8222              (not (zerop (length message-id))))
8223     (setq message-id (gnus-replace-in-string message-id " " ""))
8224     ;; Construct the correct Message-ID if necessary.
8225     ;; Suggested by tale@pawl.rpi.edu.
8226     (unless (string-match "^<" message-id)
8227       (setq message-id (concat "<" message-id)))
8228     (unless (string-match ">$" message-id)
8229       (setq message-id (concat message-id ">")))
8230     ;; People often post MIDs from URLs, so unhex it:
8231     (unless (string-match "@" message-id)
8232       (setq message-id (gnus-url-unhex-string message-id)))
8233     (let* ((header (gnus-id-to-header message-id))
8234            (sparse (and header
8235                         (gnus-summary-article-sparse-p
8236                          (mail-header-number header))
8237                         (memq (mail-header-number header)
8238                               gnus-newsgroup-limit)))
8239            number)
8240       (cond
8241        ;; If the article is present in the buffer we just go to it.
8242        ((and header
8243              (or (not (gnus-summary-article-sparse-p
8244                        (mail-header-number header)))
8245                  sparse))
8246         (prog1
8247             (gnus-summary-goto-article
8248              (mail-header-number header) nil t)
8249           (when sparse
8250             (gnus-summary-update-article (mail-header-number header)))))
8251        (t
8252         ;; We fetch the article.
8253         (catch 'found
8254           (dolist (gnus-override-method (gnus-refer-article-methods))
8255             (when (and (gnus-check-server gnus-override-method)
8256                        ;; Fetch the header,
8257                        (setq number (gnus-summary-insert-subject message-id)))
8258               ;; and display the article.
8259               (gnus-summary-select-article nil nil nil number)
8260               (throw 'found t)))
8261           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8262
8263 (defun gnus-refer-article-methods ()
8264   "Return a list of referable methods."
8265   (cond
8266    ;; No method, so we default to current and native.
8267    ((null gnus-refer-article-method)
8268     (list gnus-current-select-method gnus-select-method))
8269    ;; Current.
8270    ((eq 'current gnus-refer-article-method)
8271     (list gnus-current-select-method))
8272    ;; List of select methods.
8273    ((not (and (symbolp (car gnus-refer-article-method))
8274               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8275     (let (out)
8276       (dolist (method gnus-refer-article-method)
8277         (push (if (eq 'current method)
8278                   gnus-current-select-method
8279                 method)
8280               out))
8281       (nreverse out)))
8282    ;; One single select method.
8283    (t
8284     (list gnus-refer-article-method))))
8285
8286 (defun gnus-summary-edit-parameters ()
8287   "Edit the group parameters of the current group."
8288   (interactive)
8289   (gnus-group-edit-group gnus-newsgroup-name 'params))
8290
8291 (defun gnus-summary-customize-parameters ()
8292   "Customize the group parameters of the current group."
8293   (interactive)
8294   (gnus-group-customize gnus-newsgroup-name))
8295
8296 (defun gnus-summary-enter-digest-group (&optional force)
8297   "Enter an nndoc group based on the current article.
8298 If FORCE, force a digest interpretation.  If not, try
8299 to guess what the document format is."
8300   (interactive "P")
8301   (let ((conf gnus-current-window-configuration))
8302     (save-window-excursion
8303       (save-excursion
8304         (let (gnus-article-prepare-hook
8305               gnus-display-mime-function
8306               gnus-break-pages)
8307           (gnus-summary-select-article))))
8308     (setq gnus-current-window-configuration conf)
8309     (let* ((name (format "%s-%d"
8310                          (gnus-group-prefixed-name
8311                           gnus-newsgroup-name (list 'nndoc ""))
8312                          (with-current-buffer gnus-summary-buffer
8313                            gnus-current-article)))
8314            (ogroup gnus-newsgroup-name)
8315            (params (append (gnus-info-params (gnus-get-info ogroup))
8316                            (list (cons 'to-group ogroup))
8317                            (list (cons 'parent-group ogroup))
8318                            (list (cons 'save-article-group ogroup))))
8319            (case-fold-search t)
8320            (buf (current-buffer))
8321            dig to-address)
8322       (save-excursion
8323         (set-buffer gnus-original-article-buffer)
8324         ;; Have the digest group inherit the main mail address of
8325         ;; the parent article.
8326         (when (setq to-address (or (gnus-fetch-field "reply-to")
8327                                    (gnus-fetch-field "from")))
8328           (setq params (append
8329                         (list (cons 'to-address
8330                                     (funcall gnus-decode-encoded-word-function
8331                                              to-address))))))
8332         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8333         (insert-buffer-substring gnus-original-article-buffer)
8334         ;; Remove lines that may lead nndoc to misinterpret the
8335         ;; document type.
8336         (narrow-to-region
8337          (goto-char (point-min))
8338          (or (search-forward "\n\n" nil t) (point)))
8339         (goto-char (point-min))
8340         (delete-matching-lines "^Path:\\|^From ")
8341         (widen))
8342       (unwind-protect
8343           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8344                     (gnus-newsgroup-ephemeral-ignored-charsets
8345                      gnus-newsgroup-ignored-charsets))
8346                 (gnus-group-read-ephemeral-group
8347                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8348                               (nndoc-article-type
8349                                ,(if force 'mbox 'guess)))
8350                  t nil nil nil
8351                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8352                                                         "ADAPT")))))
8353               ;; Make all postings to this group go to the parent group.
8354               (nconc (gnus-info-params (gnus-get-info name))
8355                      params)
8356             ;; Couldn't select this doc group.
8357             (switch-to-buffer buf)
8358             (gnus-set-global-variables)
8359             (gnus-configure-windows 'summary)
8360             (gnus-message 3 "Article couldn't be entered?"))
8361         (kill-buffer dig)))))
8362
8363 (defun gnus-summary-read-document (n)
8364   "Open a new group based on the current article(s).
8365 This will allow you to read digests and other similar
8366 documents as newsgroups.
8367 Obeys the standard process/prefix convention."
8368   (interactive "P")
8369   (let* ((articles (gnus-summary-work-articles n))
8370          (ogroup gnus-newsgroup-name)
8371          (params (append (gnus-info-params (gnus-get-info ogroup))
8372                          (list (cons 'to-group ogroup))))
8373          article group egroup groups vgroup)
8374     (while (setq article (pop articles))
8375       (setq group (format "%s-%d" gnus-newsgroup-name article))
8376       (gnus-summary-remove-process-mark article)
8377       (when (gnus-summary-display-article article)
8378         (save-excursion
8379           (with-temp-buffer
8380             (insert-buffer-substring gnus-original-article-buffer)
8381             ;; Remove some headers that may lead nndoc to make
8382             ;; the wrong guess.
8383             (message-narrow-to-head)
8384             (goto-char (point-min))
8385             (delete-matching-lines "^Path:\\|^From ")
8386             (widen)
8387             (if (setq egroup
8388                       (gnus-group-read-ephemeral-group
8389                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8390                                      (nndoc-article-type guess))
8391                        t nil t))
8392                 (progn
8393             ;; Make all postings to this group go to the parent group.
8394                   (nconc (gnus-info-params (gnus-get-info egroup))
8395                          params)
8396                   (push egroup groups))
8397               ;; Couldn't select this doc group.
8398               (gnus-error 3 "Article couldn't be entered"))))))
8399     ;; Now we have selected all the documents.
8400     (cond
8401      ((not groups)
8402       (error "None of the articles could be interpreted as documents"))
8403      ((gnus-group-read-ephemeral-group
8404        (setq vgroup (format
8405                      "nnvirtual:%s-%s" gnus-newsgroup-name
8406                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8407        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8408        t
8409        (cons (current-buffer) 'summary)))
8410      (t
8411       (error "Couldn't select virtual nndoc group")))))
8412
8413 (defun gnus-summary-isearch-article (&optional regexp-p)
8414   "Do incremental search forward on the current article.
8415 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8416   (interactive "P")
8417   (gnus-summary-select-article)
8418   (gnus-configure-windows 'article)
8419   (gnus-eval-in-buffer-window gnus-article-buffer
8420     (save-restriction
8421       (widen)
8422       (isearch-forward regexp-p))))
8423
8424 (defun gnus-summary-search-article-forward (regexp &optional backward)
8425   "Search for an article containing REGEXP forward.
8426 If BACKWARD, search backward instead."
8427   (interactive
8428    (list (read-string
8429           (format "Search article %s (regexp%s): "
8430                   (if current-prefix-arg "backward" "forward")
8431                   (if gnus-last-search-regexp
8432                       (concat ", default " gnus-last-search-regexp)
8433                     "")))
8434          current-prefix-arg))
8435   (if (string-equal regexp "")
8436       (setq regexp (or gnus-last-search-regexp ""))
8437     (setq gnus-last-search-regexp regexp)
8438     (setq gnus-article-before-search gnus-current-article))
8439   ;; Intentionally set gnus-last-article.
8440   (setq gnus-last-article gnus-article-before-search)
8441   (let ((gnus-last-article gnus-last-article))
8442     (if (gnus-summary-search-article regexp backward)
8443         (gnus-summary-show-thread)
8444       (error "Search failed: \"%s\"" regexp))))
8445
8446 (defun gnus-summary-search-article-backward (regexp)
8447   "Search for an article containing REGEXP backward."
8448   (interactive
8449    (list (read-string
8450           (format "Search article backward (regexp%s): "
8451                   (if gnus-last-search-regexp
8452                       (concat ", default " gnus-last-search-regexp)
8453                     "")))))
8454   (gnus-summary-search-article-forward regexp 'backward))
8455
8456 (defun gnus-summary-search-article (regexp &optional backward)
8457   "Search for an article containing REGEXP.
8458 Optional argument BACKWARD means do search for backward.
8459 `gnus-select-article-hook' is not called during the search."
8460   ;; We have to require this here to make sure that the following
8461   ;; dynamic binding isn't shadowed by autoloading.
8462   (require 'gnus-async)
8463   (require 'gnus-art)
8464   (let ((gnus-select-article-hook nil)  ;Disable hook.
8465         (gnus-article-prepare-hook nil)
8466         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8467         (gnus-use-article-prefetch nil)
8468         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8469         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8470         (gnus-visual nil)
8471         (gnus-keep-backlog nil)
8472         (gnus-break-pages nil)
8473         (gnus-summary-display-arrow nil)
8474         (gnus-updated-mode-lines nil)
8475         (gnus-auto-center-summary nil)
8476         (sum (current-buffer))
8477         (gnus-display-mime-function nil)
8478         (found nil)
8479         point)
8480     (gnus-save-hidden-threads
8481       (gnus-summary-select-article)
8482       (set-buffer gnus-article-buffer)
8483       (goto-char (window-point (get-buffer-window (current-buffer))))
8484       (when backward
8485         (forward-line -1))
8486       (while (not found)
8487         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8488         (if (if backward
8489                 (re-search-backward regexp nil t)
8490               (re-search-forward regexp nil t))
8491             ;; We found the regexp.
8492             (progn
8493               (setq found 'found)
8494               (beginning-of-line)
8495               (set-window-start
8496                (get-buffer-window (current-buffer))
8497                (point))
8498               (forward-line 1)
8499               (set-window-point
8500                (get-buffer-window (current-buffer))
8501                (point))
8502               (set-buffer sum)
8503               (setq point (point)))
8504           ;; We didn't find it, so we go to the next article.
8505           (set-buffer sum)
8506           (setq found 'not)
8507           (while (eq found 'not)
8508             (if (not (if backward (gnus-summary-find-prev)
8509                        (gnus-summary-find-next)))
8510                 ;; No more articles.
8511                 (setq found t)
8512               ;; Select the next article and adjust point.
8513               (unless (gnus-summary-article-sparse-p
8514                        (gnus-summary-article-number))
8515                 (setq found nil)
8516                 (gnus-summary-select-article)
8517                 (set-buffer gnus-article-buffer)
8518                 (widen)
8519                 (goto-char (if backward (point-max) (point-min))))))))
8520       (gnus-message 7 ""))
8521     ;; Return whether we found the regexp.
8522     (when (eq found 'found)
8523       (goto-char point)
8524       (gnus-summary-show-thread)
8525       (gnus-summary-goto-subject gnus-current-article)
8526       (gnus-summary-position-point)
8527       t)))
8528
8529 (defun gnus-find-matching-articles (header regexp)
8530   "Return a list of all articles that match REGEXP on HEADER.
8531 This search includes all articles in the current group that Gnus has
8532 fetched headers for, whether they are displayed or not."
8533   (let ((articles nil)
8534         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8535         (case-fold-search t))
8536     (dolist (header gnus-newsgroup-headers)
8537       (when (string-match regexp (funcall func header))
8538         (push (mail-header-number header) articles)))
8539     (nreverse articles)))
8540
8541 (defun gnus-summary-find-matching (header regexp &optional backward unread
8542                                           not-case-fold not-matching)
8543   "Return a list of all articles that match REGEXP on HEADER.
8544 The search stars on the current article and goes forwards unless
8545 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8546 If UNREAD is non-nil, only unread articles will
8547 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8548 in the comparisons. If NOT-MATCHING, return a list of all articles that
8549 not match REGEXP on HEADER."
8550   (let ((case-fold-search (not not-case-fold))
8551         articles d func)
8552     (if (consp header)
8553         (if (eq (car header) 'extra)
8554             (setq func
8555                   `(lambda (h)
8556                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8557                          "")))
8558           (error "%s is an invalid header" header))
8559       (unless (fboundp (intern (concat "mail-header-" header)))
8560         (error "%s is not a valid header" header))
8561       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8562     (dolist (d (if (eq backward 'all)
8563                    gnus-newsgroup-data
8564                  (gnus-data-find-list
8565                   (gnus-summary-article-number)
8566                   (gnus-data-list backward))))
8567       (when (and (or (not unread)       ; We want all articles...
8568                      (gnus-data-unread-p d)) ; Or just unreads.
8569                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8570                  (if not-matching
8571                      (not (string-match
8572                            regexp
8573                            (funcall func (gnus-data-header d))))
8574                    (string-match regexp
8575                                  (funcall func (gnus-data-header d)))))
8576         (push (gnus-data-number d) articles))) ; Success!
8577     (nreverse articles)))
8578
8579 (defun gnus-summary-execute-command (header regexp command &optional backward)
8580   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8581 If HEADER is an empty string (or nil), the match is done on the entire
8582 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8583   (interactive
8584    (list (let ((completion-ignore-case t))
8585            (completing-read
8586             "Header name: "
8587             (mapcar (lambda (header) (list (format "%s" header)))
8588                     (append
8589                      '("Number" "Subject" "From" "Lines" "Date"
8590                        "Message-ID" "Xref" "References" "Body")
8591                      gnus-extra-headers))
8592             nil 'require-match))
8593          (read-string "Regexp: ")
8594          (read-key-sequence "Command: ")
8595          current-prefix-arg))
8596   (when (equal header "Body")
8597     (setq header ""))
8598   ;; Hidden thread subtrees must be searched as well.
8599   (gnus-summary-show-all-threads)
8600   ;; We don't want to change current point nor window configuration.
8601   (save-excursion
8602     (save-window-excursion
8603       (let (gnus-visual
8604             gnus-treat-strip-trailing-blank-lines
8605             gnus-treat-strip-leading-blank-lines
8606             gnus-treat-strip-multiple-blank-lines
8607             gnus-treat-hide-boring-headers
8608             gnus-treat-fold-newsgroups
8609             gnus-article-prepare-hook)
8610         (gnus-message 6 "Executing %s..." (key-description command))
8611         ;; We'd like to execute COMMAND interactively so as to give arguments.
8612         (gnus-execute header regexp
8613                       `(call-interactively ',(key-binding command))
8614                       backward)
8615         (gnus-message 6 "Executing %s...done" (key-description command))))))
8616
8617 (defun gnus-summary-beginning-of-article ()
8618   "Scroll the article back to the beginning."
8619   (interactive)
8620   (gnus-summary-select-article)
8621   (gnus-configure-windows 'article)
8622   (gnus-eval-in-buffer-window gnus-article-buffer
8623     (widen)
8624     (goto-char (point-min))
8625     (when gnus-break-pages
8626       (gnus-narrow-to-page))))
8627
8628 (defun gnus-summary-end-of-article ()
8629   "Scroll to the end of the article."
8630   (interactive)
8631   (gnus-summary-select-article)
8632   (gnus-configure-windows 'article)
8633   (gnus-eval-in-buffer-window gnus-article-buffer
8634     (widen)
8635     (goto-char (point-max))
8636     (recenter -3)
8637     (when gnus-break-pages
8638       (when (re-search-backward page-delimiter nil t)
8639         (narrow-to-region (match-end 0) (point-max)))
8640       (gnus-narrow-to-page))))
8641
8642 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8643   "Truncate to LEN and quote all \"(\"'s in STRING."
8644   (gnus-replace-in-string (if (and len (> (length string) len))
8645                               (substring string 0 len)
8646                             string)
8647                           "[()]" "\\\\\\&"))
8648
8649 (defun gnus-summary-print-article (&optional filename n)
8650   "Generate and print a PostScript image of the process-marked (mail) articles.
8651
8652 If used interactively, print the current article if none are
8653 process-marked.  With prefix arg, prompt the user for the name of the
8654 file to save in.
8655
8656 When used from Lisp, accept two optional args FILENAME and N.  N means
8657 to print the next N articles.  If N is negative, print the N previous
8658 articles.  If N is nil and articles have been marked with the process
8659 mark, print these instead.
8660
8661 If the optional first argument FILENAME is nil, send the image to the
8662 printer.  If FILENAME is a string, save the PostScript image in a file with
8663 that name.  If FILENAME is a number, prompt the user for the name of the file
8664 to save in."
8665   (interactive (list (ps-print-preprint current-prefix-arg)))
8666   (dolist (article (gnus-summary-work-articles n))
8667     (gnus-summary-select-article nil nil 'pseudo article)
8668     (gnus-eval-in-buffer-window gnus-article-buffer
8669       (gnus-print-buffer))
8670     (gnus-summary-remove-process-mark article))
8671   (ps-despool filename))
8672
8673 (defun gnus-print-buffer ()
8674   (let ((buffer (generate-new-buffer " *print*")))
8675     (unwind-protect
8676         (progn
8677           (copy-to-buffer buffer (point-min) (point-max))
8678           (set-buffer buffer)
8679           (gnus-remove-text-with-property 'gnus-decoration)
8680           (when (gnus-visual-p 'article-highlight 'highlight)
8681             ;; Copy-to-buffer doesn't copy overlay.  So redo
8682             ;; highlight.
8683             (let ((gnus-article-buffer buffer))
8684               (gnus-article-highlight-citation t)
8685               (gnus-article-highlight-signature)
8686               (gnus-article-emphasize)
8687               (gnus-article-delete-invisible-text)))
8688           (let ((ps-left-header
8689                  (list
8690                   (concat "("
8691                           (gnus-summary-print-truncate-and-quote
8692                            (mail-header-subject gnus-current-headers)
8693                            66) ")")
8694                   (concat "("
8695                           (gnus-summary-print-truncate-and-quote
8696                            (mail-header-from gnus-current-headers)
8697                            45) ")")))
8698                 (ps-right-header
8699                  (list
8700                   "/pagenumberstring load"
8701                   (concat "("
8702                           (mail-header-date gnus-current-headers) ")"))))
8703             (gnus-run-hooks 'gnus-ps-print-hook)
8704             (save-excursion
8705               (if window-system
8706                   (ps-spool-buffer-with-faces)
8707                 (ps-spool-buffer)))))
8708       (kill-buffer buffer))))
8709
8710 (defun gnus-summary-show-article (&optional arg)
8711   "Force redisplaying of the current article.
8712 If ARG (the prefix) is a number, show the article with the charset
8713 defined in `gnus-summary-show-article-charset-alist', or the charset
8714 input.
8715 If ARG (the prefix) is non-nil and not a number, show the raw article
8716 without any article massaging functions being run.  Normally, the key
8717 strokes are `C-u g'."
8718   (interactive "P")
8719   (cond
8720    ((numberp arg)
8721     (gnus-summary-show-article t)
8722     (let ((gnus-newsgroup-charset
8723            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8724                (mm-read-coding-system
8725                 "View as charset: " ;; actually it is coding system.
8726                 (save-excursion
8727                   (set-buffer gnus-article-buffer)
8728                   (mm-detect-coding-region (point) (point-max))))))
8729           (gnus-newsgroup-ignored-charsets 'gnus-all))
8730       (gnus-summary-select-article nil 'force)
8731       (let ((deps gnus-newsgroup-dependencies)
8732             head header lines)
8733         (save-excursion
8734           (set-buffer gnus-original-article-buffer)
8735           (save-restriction
8736             (message-narrow-to-head)
8737             (setq head (buffer-string))
8738             (goto-char (point-min))
8739             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8740               (goto-char (point-max))
8741               (widen)
8742               (setq lines (1- (count-lines (point) (point-max))))))
8743           (with-temp-buffer
8744             (insert (format "211 %d Article retrieved.\n"
8745                             (cdr gnus-article-current)))
8746             (insert head)
8747             (if lines (insert (format "Lines: %d\n" lines)))
8748             (insert ".\n")
8749             (let ((nntp-server-buffer (current-buffer)))
8750               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8751         (gnus-data-set-header
8752          (gnus-data-find (cdr gnus-article-current))
8753          header)
8754         (gnus-summary-update-article-line
8755          (cdr gnus-article-current) header)
8756         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8757           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8758    ((not arg)
8759     ;; Select the article the normal way.
8760     (gnus-summary-select-article nil 'force))
8761    (t
8762     ;; We have to require this here to make sure that the following
8763     ;; dynamic binding isn't shadowed by autoloading.
8764     (require 'gnus-async)
8765     (require 'gnus-art)
8766     ;; Bind the article treatment functions to nil.
8767     (let ((gnus-have-all-headers t)
8768           gnus-article-prepare-hook
8769           gnus-article-decode-hook
8770           gnus-display-mime-function
8771           gnus-break-pages)
8772       ;; Destroy any MIME parts.
8773       (when (gnus-buffer-live-p gnus-article-buffer)
8774         (save-excursion
8775           (set-buffer gnus-article-buffer)
8776           (mm-destroy-parts gnus-article-mime-handles)
8777           ;; Set it to nil for safety reason.
8778           (setq gnus-article-mime-handle-alist nil)
8779           (setq gnus-article-mime-handles nil)))
8780       (gnus-summary-select-article nil 'force))))
8781   (gnus-summary-goto-subject gnus-current-article)
8782   (gnus-summary-position-point))
8783
8784 (defun gnus-summary-show-raw-article ()
8785   "Show the raw article without any article massaging functions being run."
8786   (interactive)
8787   (gnus-summary-show-article t))
8788
8789 (defun gnus-summary-verbose-headers (&optional arg)
8790   "Toggle permanent full header display.
8791 If ARG is a positive number, turn header display on.
8792 If ARG is a negative number, turn header display off."
8793   (interactive "P")
8794   (setq gnus-show-all-headers
8795         (cond ((or (not (numberp arg))
8796                    (zerop arg))
8797                (not gnus-show-all-headers))
8798               ((natnump arg)
8799                t)))
8800   (gnus-summary-show-article))
8801
8802 (defun gnus-summary-toggle-header (&optional arg)
8803   "Show the headers if they are hidden, or hide them if they are shown.
8804 If ARG is a positive number, show the entire header.
8805 If ARG is a negative number, hide the unwanted header lines."
8806   (interactive "P")
8807   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8808                      (get-buffer-window gnus-article-buffer t))))
8809     (with-current-buffer gnus-article-buffer
8810       (widen)
8811       (article-narrow-to-head)
8812       (let* ((buffer-read-only nil)
8813              (inhibit-point-motion-hooks t)
8814              (hidden (if (numberp arg)
8815                          (>= arg 0)
8816                        (gnus-article-hidden-text-p 'headers)))
8817              s e)
8818         (delete-region (point-min) (point-max))
8819         (with-current-buffer gnus-original-article-buffer
8820           (goto-char (setq s (point-min)))
8821           (setq e (if (search-forward "\n\n" nil t)
8822                       (1- (point))
8823                     (point-max))))
8824         (insert-buffer-substring gnus-original-article-buffer s e)
8825         (run-hooks 'gnus-article-decode-hook)
8826         (if hidden
8827             (let ((gnus-treat-hide-headers nil)
8828                   (gnus-treat-hide-boring-headers nil))
8829               (gnus-delete-wash-type 'headers)
8830               (gnus-treat-article 'head))
8831           (gnus-treat-article 'head))
8832         (widen)
8833         (if window
8834             (set-window-start window (goto-char (point-min))))
8835         (if gnus-break-pages
8836             (gnus-narrow-to-page)
8837           (when (gnus-visual-p 'page-marker)
8838             (let ((buffer-read-only nil))
8839               (gnus-remove-text-with-property 'gnus-prev)
8840               (gnus-remove-text-with-property 'gnus-next))))
8841         (gnus-set-mode-line 'article)))))
8842
8843 (defun gnus-summary-show-all-headers ()
8844   "Make all header lines visible."
8845   (interactive)
8846   (gnus-summary-toggle-header 1))
8847
8848 (defun gnus-summary-caesar-message (&optional arg)
8849   "Caesar rotate the current article by 13.
8850 The numerical prefix specifies how many places to rotate each letter
8851 forward."
8852   (interactive "P")
8853   (gnus-summary-select-article)
8854   (let ((mail-header-separator ""))
8855     (gnus-eval-in-buffer-window gnus-article-buffer
8856       (save-restriction
8857         (widen)
8858         (let ((start (window-start))
8859               buffer-read-only)
8860           (message-caesar-buffer-body arg)
8861           (set-window-start (get-buffer-window (current-buffer)) start))))))
8862
8863 (autoload 'unmorse-region "morse"
8864   "Convert morse coded text in region to ordinary ASCII text."
8865   t)
8866
8867 (defun gnus-summary-morse-message (&optional arg)
8868   "Morse decode the current article."
8869   (interactive "P")
8870   (gnus-summary-select-article)
8871   (let ((mail-header-separator ""))
8872     (gnus-eval-in-buffer-window gnus-article-buffer
8873       (save-excursion
8874         (save-restriction
8875           (widen)
8876           (let ((pos (window-start))
8877                 buffer-read-only)
8878             (goto-char (point-min))
8879             (when (message-goto-body)
8880               (gnus-narrow-to-body))
8881             (goto-char (point-min))
8882             (while (re-search-forward "·" (point-max) t)
8883               (replace-match "."))
8884             (unmorse-region (point-min) (point-max))
8885             (widen)
8886             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8887
8888 (defun gnus-summary-stop-page-breaking ()
8889   "Stop page breaking in the current article."
8890   (interactive)
8891   (gnus-summary-select-article)
8892   (gnus-eval-in-buffer-window gnus-article-buffer
8893     (widen)
8894     (when (gnus-visual-p 'page-marker)
8895       (let ((buffer-read-only nil))
8896         (gnus-remove-text-with-property 'gnus-prev)
8897         (gnus-remove-text-with-property 'gnus-next))
8898       (setq gnus-page-broken nil))))
8899
8900 (defun gnus-summary-move-article (&optional n to-newsgroup
8901                                             select-method action)
8902   "Move the current article to a different newsgroup.
8903 If N is a positive number, move the N next articles.
8904 If N is a negative number, move the N previous articles.
8905 If N is nil and any articles have been marked with the process mark,
8906 move those articles instead.
8907 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8908 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8909 re-spool using this method.
8910
8911 When called interactively with TO-NEWSGROUP being nil, the value of
8912 the variable `gnus-move-split-methods' is used for finding a default
8913 for the target newsgroup.
8914
8915 For this function to work, both the current newsgroup and the
8916 newsgroup that you want to move to have to support the `request-move'
8917 and `request-accept' functions.
8918
8919 ACTION can be either `move' (the default), `crosspost' or `copy'."
8920   (interactive "P")
8921   (unless action
8922     (setq action 'move))
8923   ;; Check whether the source group supports the required functions.
8924   (cond ((and (eq action 'move)
8925               (not (gnus-check-backend-function
8926                     'request-move-article gnus-newsgroup-name)))
8927          (error "The current group does not support article moving"))
8928         ((and (eq action 'crosspost)
8929               (not (gnus-check-backend-function
8930                     'request-replace-article gnus-newsgroup-name)))
8931          (error "The current group does not support article editing")))
8932   (let ((articles (gnus-summary-work-articles n))
8933         (prefix (if (gnus-check-backend-function
8934                      'request-move-article gnus-newsgroup-name)
8935                     (funcall gnus-move-group-prefix-function
8936                              gnus-newsgroup-name)
8937                   ""))
8938         (names '((move "Move" "Moving")
8939                  (copy "Copy" "Copying")
8940                  (crosspost "Crosspost" "Crossposting")))
8941         (copy-buf (save-excursion
8942                     (nnheader-set-temp-buffer " *copy article*")))
8943         art-group to-method new-xref article to-groups)
8944     (unless (assq action names)
8945       (error "Unknown action %s" action))
8946     ;; Read the newsgroup name.
8947     (when (and (not to-newsgroup)
8948                (not select-method))
8949       (if (and gnus-move-split-methods
8950                (not
8951                 (and (memq gnus-current-article articles)
8952                      (gnus-buffer-live-p gnus-original-article-buffer))))
8953           ;; When `gnus-move-split-methods' is non-nil, we have to
8954           ;; select an article to give `gnus-read-move-group-name' an
8955           ;; opportunity to suggest an appropriate default.  However,
8956           ;; we needn't render or mark the article.
8957           (let ((gnus-display-mime-function nil)
8958                 (gnus-article-prepare-hook nil)
8959                 (gnus-mark-article-hook nil))
8960             (gnus-summary-select-article nil nil nil (car articles))))
8961       (setq to-newsgroup
8962             (gnus-read-move-group-name
8963              (cadr (assq action names))
8964              (symbol-value (intern (format "gnus-current-%s-group" action)))
8965              articles prefix))
8966       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8967     (setq to-method (or select-method
8968                         (gnus-server-to-method
8969                          (gnus-group-method to-newsgroup))))
8970     ;; Check the method we are to move this article to...
8971     (unless (gnus-check-backend-function
8972              'request-accept-article (car to-method))
8973       (error "%s does not support article copying" (car to-method)))
8974     (unless (gnus-check-server to-method)
8975       (error "Can't open server %s" (car to-method)))
8976     (gnus-message 6 "%s to %s: %s..."
8977                   (caddr (assq action names))
8978                   (or (car select-method) to-newsgroup) articles)
8979     (while articles
8980       (setq article (pop articles))
8981       (setq
8982        art-group
8983        (cond
8984         ;; Move the article.
8985         ((eq action 'move)
8986          ;; Remove this article from future suppression.
8987          (gnus-dup-unsuppress-article article)
8988          (gnus-request-move-article
8989           article                       ; Article to move
8990           gnus-newsgroup-name           ; From newsgroup
8991           (nth 1 (gnus-find-method-for-group
8992                   gnus-newsgroup-name)) ; Server
8993           (list 'gnus-request-accept-article
8994                 to-newsgroup (list 'quote select-method)
8995                 (not articles) t)       ; Accept form
8996           (not articles)))              ; Only save nov last time
8997         ;; Copy the article.
8998         ((eq action 'copy)
8999          (save-excursion
9000            (set-buffer copy-buf)
9001            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9002              (gnus-request-accept-article
9003               to-newsgroup select-method (not articles) t))))
9004         ;; Crosspost the article.
9005         ((eq action 'crosspost)
9006          (let ((xref (message-tokenize-header
9007                       (mail-header-xref (gnus-summary-article-header article))
9008                       " ")))
9009            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9010                                   ":" (number-to-string article)))
9011            (unless xref
9012              (setq xref (list (system-name))))
9013            (setq new-xref
9014                  (concat
9015                   (mapconcat 'identity
9016                              (delete "Xref:" (delete new-xref xref))
9017                              " ")
9018                   " " new-xref))
9019            (save-excursion
9020              (set-buffer copy-buf)
9021              ;; First put the article in the destination group.
9022              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9023              (when (consp (setq art-group
9024                                 (gnus-request-accept-article
9025                                  to-newsgroup select-method (not articles))))
9026                (setq new-xref (concat new-xref " " (car art-group)
9027                                       ":"
9028                                       (number-to-string (cdr art-group))))
9029                ;; Now we have the new Xrefs header, so we insert
9030                ;; it and replace the new article.
9031                (nnheader-replace-header "Xref" new-xref)
9032                (gnus-request-replace-article
9033                 (cdr art-group) to-newsgroup (current-buffer))
9034                art-group))))))
9035       (cond
9036        ((not art-group)
9037         (gnus-message 1 "Couldn't %s article %s: %s"
9038                       (cadr (assq action names)) article
9039                       (nnheader-get-report (car to-method))))
9040        ((eq art-group 'junk)
9041         (when (eq action 'move)
9042           (gnus-summary-mark-article article gnus-canceled-mark)
9043           (gnus-message 4 "Deleted article %s" article)
9044           ;; run the delete hook
9045           (run-hook-with-args 'gnus-summary-article-delete-hook
9046                               action
9047                               (gnus-data-header
9048                                (assoc article (gnus-data-list nil)))
9049                               gnus-newsgroup-name nil
9050                               select-method)))
9051        (t
9052         (let* ((pto-group (gnus-group-prefixed-name
9053                            (car art-group) to-method))
9054                (info (gnus-get-info pto-group))
9055                (to-group (gnus-info-group info))
9056                to-marks)
9057           ;; Update the group that has been moved to.
9058           (when (and info
9059                      (memq action '(move copy)))
9060             (unless (member to-group to-groups)
9061               (push to-group to-groups))
9062
9063             (unless (memq article gnus-newsgroup-unreads)
9064               (push 'read to-marks)
9065               (gnus-info-set-read
9066                info (gnus-add-to-range (gnus-info-read info)
9067                                        (list (cdr art-group)))))
9068
9069             ;; See whether the article is to be put in the cache.
9070             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9071                              gnus-article-mark-lists
9072                            (delete '(expirable . expire)
9073                                    (copy-sequence gnus-article-mark-lists))))
9074                   (to-article (cdr art-group)))
9075
9076               ;; Enter the article into the cache in the new group,
9077               ;; if that is required.
9078               (when gnus-use-cache
9079                 (gnus-cache-possibly-enter-article
9080                  to-group to-article
9081                  (memq article gnus-newsgroup-marked)
9082                  (memq article gnus-newsgroup-dormant)
9083                  (memq article gnus-newsgroup-unreads)))
9084
9085               (when gnus-preserve-marks
9086                 ;; Copy any marks over to the new group.
9087                 (when (and (equal to-group gnus-newsgroup-name)
9088                            (not (memq article gnus-newsgroup-unreads)))
9089                   ;; Mark this article as read in this group.
9090                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9091                   (setcdr (gnus-active to-group) to-article)
9092                   (setcdr gnus-newsgroup-active to-article))
9093
9094                 (while marks
9095                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9096                     (when (memq article (symbol-value
9097                                          (intern (format "gnus-newsgroup-%s"
9098                                                          (caar marks)))))
9099                       (push (cdar marks) to-marks)
9100                       ;; If the other group is the same as this group,
9101                       ;; then we have to add the mark to the list.
9102                       (when (equal to-group gnus-newsgroup-name)
9103                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9104                              (cons to-article
9105                                    (symbol-value
9106                                     (intern (format "gnus-newsgroup-%s"
9107                                                     (caar marks)))))))
9108                       ;; Copy the marks to other group.
9109                       (gnus-add-marked-articles
9110                        to-group (cdar marks) (list to-article) info)))
9111                   (setq marks (cdr marks)))
9112
9113                 (gnus-request-set-mark
9114                  to-group (list (list (list to-article) 'add to-marks))))
9115
9116               (gnus-dribble-enter
9117                (concat "(gnus-group-set-info '"
9118                        (gnus-prin1-to-string (gnus-get-info to-group))
9119                        ")"))))
9120
9121           ;; Update the Xref header in this article to point to
9122           ;; the new crossposted article we have just created.
9123           (when (eq action 'crosspost)
9124             (save-excursion
9125               (set-buffer copy-buf)
9126               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9127               (nnheader-replace-header "Xref" new-xref)
9128               (gnus-request-replace-article
9129                article gnus-newsgroup-name (current-buffer))))
9130
9131           ;; run the move/copy/crosspost/respool hook
9132           (run-hook-with-args 'gnus-summary-article-move-hook
9133                               action
9134                               (gnus-data-header
9135                                (assoc article (gnus-data-list nil)))
9136                               gnus-newsgroup-name
9137                               to-newsgroup
9138                               select-method))
9139
9140         ;;;!!!Why is this necessary?
9141         (set-buffer gnus-summary-buffer)
9142         
9143         (gnus-summary-goto-subject article)
9144         (when (eq action 'move)
9145           (gnus-summary-mark-article article gnus-canceled-mark))))
9146       (gnus-summary-remove-process-mark article))
9147     ;; Re-activate all groups that have been moved to.
9148     (save-excursion
9149       (set-buffer gnus-group-buffer)
9150       (let ((gnus-group-marked to-groups))
9151         (gnus-group-get-new-news-this-group nil t)))
9152
9153     (gnus-kill-buffer copy-buf)
9154     (gnus-summary-position-point)
9155     (gnus-set-mode-line 'summary)))
9156
9157 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9158   "Move the current article to a different newsgroup.
9159 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9160 When called interactively, if TO-NEWSGROUP is nil, use the value of
9161 the variable `gnus-move-split-methods' for finding a default target
9162 newsgroup.
9163 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9164 re-spool using this method."
9165   (interactive "P")
9166   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9167
9168 (defun gnus-summary-crosspost-article (&optional n)
9169   "Crosspost the current article to some other group."
9170   (interactive "P")
9171   (gnus-summary-move-article n nil nil 'crosspost))
9172
9173 (defcustom gnus-summary-respool-default-method nil
9174   "Default method type for respooling an article.
9175 If nil, use to the current newsgroup method."
9176   :type 'symbol
9177   :group 'gnus-summary-mail)
9178
9179 (defcustom gnus-summary-display-while-building nil
9180   "If non-nil, show and update the summary buffer as it's being built.
9181 If the value is t, update the buffer after every line is inserted.  If
9182 the value is an integer (N), update the display every N lines."
9183   :group 'gnus-thread
9184   :type '(choice (const :tag "off" nil)
9185                  number
9186                  (const :tag "frequently" t)))
9187
9188 (defun gnus-summary-respool-article (&optional n method)
9189   "Respool the current article.
9190 The article will be squeezed through the mail spooling process again,
9191 which means that it will be put in some mail newsgroup or other
9192 depending on `nnmail-split-methods'.
9193 If N is a positive number, respool the N next articles.
9194 If N is a negative number, respool the N previous articles.
9195 If N is nil and any articles have been marked with the process mark,
9196 respool those articles instead.
9197
9198 Respooling can be done both from mail groups and \"real\" newsgroups.
9199 In the former case, the articles in question will be moved from the
9200 current group into whatever groups they are destined to.  In the
9201 latter case, they will be copied into the relevant groups."
9202   (interactive
9203    (list current-prefix-arg
9204          (let* ((methods (gnus-methods-using 'respool))
9205                 (methname
9206                  (symbol-name (or gnus-summary-respool-default-method
9207                                   (car (gnus-find-method-for-group
9208                                         gnus-newsgroup-name)))))
9209                 (method
9210                  (gnus-completing-read-with-default
9211                   methname "What backend do you want to use when respooling?"
9212                   methods nil t nil 'gnus-mail-method-history))
9213                 ms)
9214            (cond
9215             ((zerop (length (setq ms (gnus-servers-using-backend
9216                                       (intern method)))))
9217              (list (intern method) ""))
9218             ((= 1 (length ms))
9219              (car ms))
9220             (t
9221              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9222                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9223                            ms-alist))))))))
9224   (unless method
9225     (error "No method given for respooling"))
9226   (if (assoc (symbol-name
9227               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9228              (gnus-methods-using 'respool))
9229       (gnus-summary-move-article n nil method)
9230     (gnus-summary-copy-article n nil method)))
9231
9232 (defun gnus-summary-import-article (file &optional edit)
9233   "Import an arbitrary file into a mail newsgroup."
9234   (interactive "fImport file: \nP")
9235   (let ((group gnus-newsgroup-name)
9236         (now (current-time))
9237         atts lines group-art)
9238     (unless (gnus-check-backend-function 'request-accept-article group)
9239       (error "%s does not support article importing" group))
9240     (or (file-readable-p file)
9241         (not (file-regular-p file))
9242         (error "Can't read %s" file))
9243     (save-excursion
9244       (set-buffer (gnus-get-buffer-create " *import file*"))
9245       (erase-buffer)
9246       (nnheader-insert-file-contents file)
9247       (goto-char (point-min))
9248       (if (nnheader-article-p)
9249           (save-restriction
9250             (goto-char (point-min))
9251             (search-forward "\n\n" nil t)
9252             (narrow-to-region (point-min) (1- (point)))
9253             (goto-char (point-min))
9254             (unless (re-search-forward "^date:" nil t)
9255               (goto-char (point-max))
9256               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9257        ;; This doesn't look like an article, so we fudge some headers.
9258         (setq atts (file-attributes file)
9259               lines (count-lines (point-min) (point-max)))
9260         (insert "From: " (read-string "From: ") "\n"
9261                 "Subject: " (read-string "Subject: ") "\n"
9262                 "Date: " (message-make-date (nth 5 atts)) "\n"
9263                 "Message-ID: " (message-make-message-id) "\n"
9264                 "Lines: " (int-to-string lines) "\n"
9265                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9266       (setq group-art (gnus-request-accept-article group nil t))
9267       (kill-buffer (current-buffer)))
9268     (setq gnus-newsgroup-active (gnus-activate-group group))
9269     (forward-line 1)
9270     (gnus-summary-goto-article (cdr group-art) nil t)
9271     (when edit
9272       (gnus-summary-edit-article))))
9273
9274 (defun gnus-summary-create-article ()
9275   "Create an article in a mail newsgroup."
9276   (interactive)
9277   (let ((group gnus-newsgroup-name)
9278         (now (current-time))
9279         group-art)
9280     (unless (gnus-check-backend-function 'request-accept-article group)
9281       (error "%s does not support article importing" group))
9282     (save-excursion
9283       (set-buffer (gnus-get-buffer-create " *import file*"))
9284       (erase-buffer)
9285       (goto-char (point-min))
9286       ;; This doesn't look like an article, so we fudge some headers.
9287       (insert "From: " (read-string "From: ") "\n"
9288               "Subject: " (read-string "Subject: ") "\n"
9289               "Date: " (message-make-date now) "\n"
9290               "Message-ID: " (message-make-message-id) "\n")
9291       (setq group-art (gnus-request-accept-article group nil t))
9292       (kill-buffer (current-buffer)))
9293     (setq gnus-newsgroup-active (gnus-activate-group group))
9294     (forward-line 1)
9295     (gnus-summary-goto-article (cdr group-art) nil t)
9296     (gnus-summary-edit-article)))
9297
9298 (defun gnus-summary-article-posted-p ()
9299   "Say whether the current (mail) article is available from news as well.
9300 This will be the case if the article has both been mailed and posted."
9301   (interactive)
9302   (let ((id (mail-header-references (gnus-summary-article-header)))
9303         (gnus-override-method (car (gnus-refer-article-methods))))
9304     (if (gnus-request-head id "")
9305         (gnus-message 2 "The current message was found on %s"
9306                       gnus-override-method)
9307       (gnus-message 2 "The current message couldn't be found on %s"
9308                     gnus-override-method)
9309       nil)))
9310
9311 (defun gnus-summary-expire-articles (&optional now)
9312   "Expire all articles that are marked as expirable in the current group."
9313   (interactive)
9314   (when (and (not gnus-group-is-exiting-without-update-p)
9315              (gnus-check-backend-function
9316               'request-expire-articles gnus-newsgroup-name))
9317     ;; This backend supports expiry.
9318     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9319            (expirable (if total
9320                           (progn
9321                             ;; We need to update the info for
9322                             ;; this group for `gnus-list-of-read-articles'
9323                             ;; to give us the right answer.
9324                             (gnus-run-hooks 'gnus-exit-group-hook)
9325                             (gnus-summary-update-info)
9326                             (gnus-list-of-read-articles gnus-newsgroup-name))
9327                         (setq gnus-newsgroup-expirable
9328                               (sort gnus-newsgroup-expirable '<))))
9329            (expiry-wait (if now 'immediate
9330                           (gnus-group-find-parameter
9331                            gnus-newsgroup-name 'expiry-wait)))
9332            (nnmail-expiry-target
9333             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9334                 nnmail-expiry-target))
9335            es)
9336       (when expirable
9337         ;; There are expirable articles in this group, so we run them
9338         ;; through the expiry process.
9339         (gnus-message 6 "Expiring articles...")
9340         (unless (gnus-check-group gnus-newsgroup-name)
9341           (error "Can't open server for %s" gnus-newsgroup-name))
9342         ;; The list of articles that weren't expired is returned.
9343         (save-excursion
9344           (if expiry-wait
9345               (let ((nnmail-expiry-wait-function nil)
9346                     (nnmail-expiry-wait expiry-wait))
9347                 (setq es (gnus-request-expire-articles
9348                           expirable gnus-newsgroup-name)))
9349             (setq es (gnus-request-expire-articles
9350                       expirable gnus-newsgroup-name)))
9351           (unless total
9352             (setq gnus-newsgroup-expirable es))
9353           ;; We go through the old list of expirable, and mark all
9354           ;; really expired articles as nonexistent.
9355           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9356             (let ((gnus-use-cache nil))
9357               (dolist (article expirable)
9358                 (when (and (not (memq article es))
9359                            (gnus-data-find article))
9360                   (gnus-summary-mark-article article gnus-canceled-mark)
9361                   (run-hook-with-args 'gnus-summary-article-expire-hook
9362                                       'delete
9363                                       (gnus-data-header
9364                                        (assoc article (gnus-data-list nil)))
9365                                       gnus-newsgroup-name
9366                                       nil
9367                                       nil))))))
9368         (gnus-message 6 "Expiring articles...done")))))
9369
9370 (defun gnus-summary-expire-articles-now ()
9371   "Expunge all expirable articles in the current group.
9372 This means that *all* articles that are marked as expirable will be
9373 deleted forever, right now."
9374   (interactive)
9375   (or gnus-expert-user
9376       (gnus-yes-or-no-p
9377        "Are you really, really, really sure you want to delete all these messages? ")
9378       (error "Phew!"))
9379   (gnus-summary-expire-articles t))
9380
9381 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9382 (defun gnus-summary-delete-article (&optional n)
9383   "Delete the N next (mail) articles.
9384 This command actually deletes articles.  This is not a marking
9385 command.  The article will disappear forever from your life, never to
9386 return.
9387
9388 If N is negative, delete backwards.
9389 If N is nil and articles have been marked with the process mark,
9390 delete these instead.
9391
9392 If `gnus-novice-user' is non-nil you will be asked for
9393 confirmation before the articles are deleted."
9394   (interactive "P")
9395   (unless (gnus-check-backend-function 'request-expire-articles
9396                                        gnus-newsgroup-name)
9397     (error "The current newsgroup does not support article deletion"))
9398   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9399     (error "Couldn't open server"))
9400   ;; Compute the list of articles to delete.
9401   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9402         (nnmail-expiry-target 'delete)
9403         not-deleted)
9404     (if (and gnus-novice-user
9405              (not (gnus-yes-or-no-p
9406                    (format "Do you really want to delete %s forever? "
9407                            (if (> (length articles) 1)
9408                                (format "these %s articles" (length articles))
9409                              "this article")))))
9410         ()
9411       ;; Delete the articles.
9412       (setq not-deleted (gnus-request-expire-articles
9413                          articles gnus-newsgroup-name 'force))
9414       (while articles
9415         (gnus-summary-remove-process-mark (car articles))
9416         ;; The backend might not have been able to delete the article
9417         ;; after all.
9418         (unless (memq (car articles) not-deleted)
9419           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9420         (let* ((article (car articles))
9421                (id (mail-header-id (gnus-data-header
9422                                     (assoc article (gnus-data-list nil))))))
9423           (run-hook-with-args 'gnus-summary-article-delete-hook
9424                               'delete id gnus-newsgroup-name nil
9425                               nil))
9426         (setq articles (cdr articles)))
9427       (when not-deleted
9428         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9429     (gnus-summary-position-point)
9430     (gnus-set-mode-line 'summary)
9431     not-deleted))
9432
9433 (defun gnus-summary-edit-article (&optional arg)
9434   "Edit the current article.
9435 This will have permanent effect only in mail groups.
9436 If ARG is nil, edit the decoded articles.
9437 If ARG is 1, edit the raw articles.
9438 If ARG is 2, edit the raw articles even in read-only groups.
9439 If ARG is 3, edit the articles with the current handles.
9440 Otherwise, allow editing of articles even in read-only
9441 groups."
9442   (interactive "P")
9443   (let (force raw current-handles)
9444     (cond
9445      ((null arg))
9446      ((eq arg 1)
9447       (setq raw t))
9448      ((eq arg 2)
9449       (setq raw t
9450             force t))
9451      ((eq arg 3)
9452       (setq current-handles
9453             (and (gnus-buffer-live-p gnus-article-buffer)
9454                  (with-current-buffer gnus-article-buffer
9455                    (prog1
9456                        gnus-article-mime-handles
9457                      (setq gnus-article-mime-handles nil))))))
9458      (t
9459       (setq force t)))
9460     (when (and raw (not force)
9461                (member gnus-newsgroup-name '("nndraft:delayed"
9462                                              "nndraft:drafts"
9463                                              "nndraft:queue")))
9464       (error "Can't edit the raw article in group %s"
9465              gnus-newsgroup-name))
9466     (save-excursion
9467       (set-buffer gnus-summary-buffer)
9468       (let ((mail-parse-charset gnus-newsgroup-charset)
9469             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9470         (gnus-set-global-variables)
9471         (when (and (not force)
9472                    (gnus-group-read-only-p))
9473           (error "The current newsgroup does not support article editing"))
9474         (gnus-summary-show-article t)
9475         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9476           (with-current-buffer gnus-article-buffer
9477             (mm-enable-multibyte)))
9478         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9479             (setq raw t))
9480         (gnus-article-edit-article
9481          (if raw 'ignore
9482            `(lambda ()
9483               (let ((mbl mml-buffer-list))
9484                 (setq mml-buffer-list nil)
9485                 (mime-to-mml ,'current-handles)
9486                 (let ((mbl1 mml-buffer-list))
9487                   (setq mml-buffer-list mbl)
9488                   (set (make-local-variable 'mml-buffer-list) mbl1))
9489                 (gnus-make-local-hook 'kill-buffer-hook)
9490                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9491          `(lambda (no-highlight)
9492             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9493                   (message-options message-options)
9494                   (message-options-set-recipient)
9495                   (mail-parse-ignored-charsets
9496                    ',gnus-newsgroup-ignored-charsets))
9497               ,(if (not raw) '(progn
9498                                 (mml-to-mime)
9499                                 (mml-destroy-buffers)
9500                                 (remove-hook 'kill-buffer-hook
9501                                              'mml-destroy-buffers t)
9502                                 (kill-local-variable 'mml-buffer-list)))
9503               (gnus-summary-edit-article-done
9504                ,(or (mail-header-references gnus-current-headers) "")
9505                ,(gnus-group-read-only-p)
9506                ,gnus-summary-buffer no-highlight))))))))
9507
9508 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9509
9510 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9511                                                  no-highlight)
9512   "Make edits to the current article permanent."
9513   (interactive)
9514   (save-excursion
9515    ;; The buffer restriction contains the entire article if it exists.
9516     (when (article-goto-body)
9517       (let ((lines (count-lines (point) (point-max)))
9518             (length (- (point-max) (point)))
9519             (case-fold-search t)
9520             (body (copy-marker (point))))
9521         (goto-char (point-min))
9522         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9523           (delete-region (match-beginning 1) (match-end 1))
9524           (insert (number-to-string length)))
9525         (goto-char (point-min))
9526         (when (re-search-forward
9527                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9528           (delete-region (match-beginning 1) (match-end 1))
9529           (insert (number-to-string length)))
9530         (goto-char (point-min))
9531         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9532           (delete-region (match-beginning 1) (match-end 1))
9533           (insert (number-to-string lines))))))
9534   ;; Replace the article.
9535   (let ((buf (current-buffer)))
9536     (with-temp-buffer
9537       (insert-buffer-substring buf)
9538
9539       (if (and (not read-only)
9540                (not (gnus-request-replace-article
9541                      (cdr gnus-article-current) (car gnus-article-current)
9542                      (current-buffer) t)))
9543           (error "Couldn't replace article")
9544         ;; Update the summary buffer.
9545         (if (and references
9546                  (equal (message-tokenize-header references " ")
9547                         (message-tokenize-header
9548                          (or (message-fetch-field "references") "") " ")))
9549             ;; We only have to update this line.
9550             (save-excursion
9551               (save-restriction
9552                 (message-narrow-to-head)
9553                 (let ((head (buffer-string))
9554                       header)
9555                   (with-temp-buffer
9556                     (insert (format "211 %d Article retrieved.\n"
9557                                     (cdr gnus-article-current)))
9558                     (insert head)
9559                     (insert ".\n")
9560                     (let ((nntp-server-buffer (current-buffer)))
9561                       (setq header (car (gnus-get-newsgroup-headers
9562                                          nil t))))
9563                     (save-excursion
9564                       (set-buffer gnus-summary-buffer)
9565                       (gnus-data-set-header
9566                        (gnus-data-find (cdr gnus-article-current))
9567                        header)
9568                       (gnus-summary-update-article-line
9569                        (cdr gnus-article-current) header)
9570                       (if (gnus-summary-goto-subject
9571                            (cdr gnus-article-current) nil t)
9572                           (gnus-summary-update-secondary-mark
9573                            (cdr gnus-article-current))))))))
9574           ;; Update threads.
9575           (set-buffer (or buffer gnus-summary-buffer))
9576           (gnus-summary-update-article (cdr gnus-article-current))
9577           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9578               (gnus-summary-update-secondary-mark
9579                (cdr gnus-article-current))))
9580         ;; Prettify the article buffer again.
9581         (unless no-highlight
9582           (save-excursion
9583             (set-buffer gnus-article-buffer)
9584             ;;;!!! Fix this -- article should be rehighlighted.
9585             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9586             (set-buffer gnus-original-article-buffer)
9587             (gnus-request-article
9588              (cdr gnus-article-current)
9589              (car gnus-article-current) (current-buffer))))
9590         ;; Prettify the summary buffer line.
9591         (when (gnus-visual-p 'summary-highlight 'highlight)
9592           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9593
9594 (defun gnus-summary-edit-wash (key)
9595   "Perform editing command KEY in the article buffer."
9596   (interactive
9597    (list
9598     (progn
9599       (message "%s" (concat (this-command-keys) "- "))
9600       (read-char))))
9601   (message "")
9602   (gnus-summary-edit-article)
9603   (execute-kbd-macro (concat (this-command-keys) key))
9604   (gnus-article-edit-done))
9605
9606 ;;; Respooling
9607
9608 (defun gnus-summary-respool-query (&optional silent trace)
9609   "Query where the respool algorithm would put this article."
9610   (interactive)
9611   (let (gnus-mark-article-hook)
9612     (gnus-summary-select-article)
9613     (save-excursion
9614       (set-buffer gnus-original-article-buffer)
9615       (let ((groups (nnmail-article-group 'identity trace)))
9616         (unless silent
9617           (if groups
9618               (message "This message would go to %s"
9619                        (mapconcat 'car groups ", "))
9620             (message "This message would go to no groups"))
9621           groups)))))
9622
9623 (defun gnus-summary-respool-trace ()
9624   "Trace where the respool algorithm would put this article.
9625 Display a buffer showing all fancy splitting patterns which matched."
9626   (interactive)
9627   (gnus-summary-respool-query nil t))
9628
9629 ;; Summary marking commands.
9630
9631 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9632   "Mark articles which has the same subject as read, and then select the next.
9633 If UNMARK is positive, remove any kind of mark.
9634 If UNMARK is negative, tick articles."
9635   (interactive "P")
9636   (when unmark
9637     (setq unmark (prefix-numeric-value unmark)))
9638   (let ((count
9639          (gnus-summary-mark-same-subject
9640           (gnus-summary-article-subject) unmark)))
9641     ;; Select next unread article.  If auto-select-same mode, should
9642     ;; select the first unread article.
9643     (gnus-summary-next-article t (and gnus-auto-select-same
9644                                       (gnus-summary-article-subject)))
9645     (gnus-message 7 "%d article%s marked as %s"
9646                   count (if (= count 1) " is" "s are")
9647                   (if unmark "unread" "read"))))
9648
9649 (defun gnus-summary-kill-same-subject (&optional unmark)
9650   "Mark articles which has the same subject as read.
9651 If UNMARK is positive, remove any kind of mark.
9652 If UNMARK is negative, tick articles."
9653   (interactive "P")
9654   (when unmark
9655     (setq unmark (prefix-numeric-value unmark)))
9656   (let ((count
9657          (gnus-summary-mark-same-subject
9658           (gnus-summary-article-subject) unmark)))
9659     ;; If marked as read, go to next unread subject.
9660     (when (null unmark)
9661       ;; Go to next unread subject.
9662       (gnus-summary-next-subject 1 t))
9663     (gnus-message 7 "%d articles are marked as %s"
9664                   count (if unmark "unread" "read"))))
9665
9666 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9667   "Mark articles with same SUBJECT as read, and return marked number.
9668 If optional argument UNMARK is positive, remove any kinds of marks.
9669 If optional argument UNMARK is negative, mark articles as unread instead."
9670   (let ((count 1))
9671     (save-excursion
9672       (cond
9673        ((null unmark)                   ; Mark as read.
9674         (while (and
9675                 (progn
9676                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9677                   (gnus-summary-show-thread) t)
9678                 (gnus-summary-find-subject subject))
9679           (setq count (1+ count))))
9680        ((> unmark 0)                    ; Tick.
9681         (while (and
9682                 (progn
9683                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9684                   (gnus-summary-show-thread) t)
9685                 (gnus-summary-find-subject subject))
9686           (setq count (1+ count))))
9687        (t                               ; Mark as unread.
9688         (while (and
9689                 (progn
9690                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9691                   (gnus-summary-show-thread) t)
9692                 (gnus-summary-find-subject subject))
9693           (setq count (1+ count)))))
9694       (gnus-set-mode-line 'summary)
9695       ;; Return the number of marked articles.
9696       count)))
9697
9698 (defun gnus-summary-mark-as-processable (n &optional unmark)
9699   "Set the process mark on the next N articles.
9700 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9701 the process mark instead.  The difference between N and the actual
9702 number of articles marked is returned."
9703   (interactive "P")
9704   (if (and (null n) (gnus-region-active-p))
9705       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9706     (setq n (prefix-numeric-value n))
9707     (let ((backward (< n 0))
9708           (n (abs n)))
9709       (while (and
9710               (> n 0)
9711               (if unmark
9712                   (gnus-summary-remove-process-mark
9713                    (gnus-summary-article-number))
9714                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9715               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9716         (setq n (1- n)))
9717       (when (/= 0 n)
9718         (gnus-message 7 "No more articles"))
9719       (gnus-summary-recenter)
9720       (gnus-summary-position-point)
9721       n)))
9722
9723 (defun gnus-summary-unmark-as-processable (n)
9724   "Remove the process mark from the next N articles.
9725 If N is negative, unmark backward instead.  The difference between N and
9726 the actual number of articles unmarked is returned."
9727   (interactive "P")
9728   (gnus-summary-mark-as-processable n t))
9729
9730 (defun gnus-summary-unmark-all-processable ()
9731   "Remove the process mark from all articles."
9732   (interactive)
9733   (save-excursion
9734     (while gnus-newsgroup-processable
9735       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9736   (gnus-summary-position-point))
9737
9738 (defun gnus-summary-add-mark (article type)
9739   "Mark ARTICLE with a mark of TYPE."
9740   (let ((vtype (car (assq type gnus-article-mark-lists)))
9741         var)
9742     (if (not vtype)
9743         (error "No such mark type: %s" type)
9744       (setq var (intern (format "gnus-newsgroup-%s" type)))
9745       (set var (cons article (symbol-value var)))
9746       (if (memq type '(processable cached replied forwarded recent saved))
9747           (gnus-summary-update-secondary-mark article)
9748         ;;; !!! This is bogus.  We should find out what primary
9749         ;;; !!! mark we want to set.
9750         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9751
9752 (defun gnus-summary-mark-as-expirable (n)
9753   "Mark N articles forward as expirable.
9754 If N is negative, mark backward instead.  The difference between N and
9755 the actual number of articles marked is returned."
9756   (interactive "p")
9757   (gnus-summary-mark-forward n gnus-expirable-mark))
9758
9759 (defun gnus-summary-mark-as-spam (n)
9760   "Mark N articles forward as spam.
9761 If N is negative, mark backward instead.  The difference between N and
9762 the actual number of articles marked is returned."
9763   (interactive "p")
9764   (gnus-summary-mark-forward n gnus-spam-mark))
9765
9766 (defun gnus-summary-mark-article-as-replied (article)
9767   "Mark ARTICLE as replied to and update the summary line.
9768 ARTICLE can also be a list of articles."
9769   (interactive (list (gnus-summary-article-number)))
9770   (let ((articles (if (listp article) article (list article))))
9771     (dolist (article articles)
9772       (unless (numberp article)
9773         (error "%s is not a number" article))
9774       (push article gnus-newsgroup-replied)
9775       (let ((buffer-read-only nil))
9776         (when (gnus-summary-goto-subject article nil t)
9777           (gnus-summary-update-secondary-mark article))))))
9778
9779 (defun gnus-summary-mark-article-as-forwarded (article)
9780   "Mark ARTICLE as forwarded and update the summary line.
9781 ARTICLE can also be a list of articles."
9782   (let ((articles (if (listp article) article (list article))))
9783     (dolist (article articles)
9784       (push article gnus-newsgroup-forwarded)
9785       (let ((buffer-read-only nil))
9786         (when (gnus-summary-goto-subject article nil t)
9787           (gnus-summary-update-secondary-mark article))))))
9788
9789 (defun gnus-summary-set-bookmark (article)
9790   "Set a bookmark in current article."
9791   (interactive (list (gnus-summary-article-number)))
9792   (when (or (not (get-buffer gnus-article-buffer))
9793             (not gnus-current-article)
9794             (not gnus-article-current)
9795             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9796     (error "No current article selected"))
9797   ;; Remove old bookmark, if one exists.
9798   (gnus-pull article gnus-newsgroup-bookmarks)
9799   ;; Set the new bookmark, which is on the form
9800   ;; (article-number . line-number-in-body).
9801   (push
9802    (cons article
9803          (with-current-buffer gnus-article-buffer
9804            (count-lines
9805             (min (point)
9806                  (save-excursion
9807                    (article-goto-body)
9808                    (point)))
9809             (point))))
9810    gnus-newsgroup-bookmarks)
9811   (gnus-message 6 "A bookmark has been added to the current article."))
9812
9813 (defun gnus-summary-remove-bookmark (article)
9814   "Remove the bookmark from the current article."
9815   (interactive (list (gnus-summary-article-number)))
9816   ;; Remove old bookmark, if one exists.
9817   (if (not (assq article gnus-newsgroup-bookmarks))
9818       (gnus-message 6 "No bookmark in current article.")
9819     (gnus-pull article gnus-newsgroup-bookmarks)
9820     (gnus-message 6 "Removed bookmark.")))
9821
9822 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9823 (defun gnus-summary-mark-as-dormant (n)
9824   "Mark N articles forward as dormant.
9825 If N is negative, mark backward instead.  The difference between N and
9826 the actual number of articles marked is returned."
9827   (interactive "p")
9828   (gnus-summary-mark-forward n gnus-dormant-mark))
9829
9830 (defun gnus-summary-set-process-mark (article)
9831   "Set the process mark on ARTICLE and update the summary line."
9832   (setq gnus-newsgroup-processable
9833         (cons article
9834               (delq article gnus-newsgroup-processable)))
9835   (when (gnus-summary-goto-subject article)
9836     (gnus-summary-show-thread)
9837     (gnus-summary-goto-subject article)
9838     (gnus-summary-update-secondary-mark article)))
9839
9840 (defun gnus-summary-remove-process-mark (article)
9841   "Remove the process mark from ARTICLE and update the summary line."
9842   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9843   (when (gnus-summary-goto-subject article)
9844     (gnus-summary-show-thread)
9845     (gnus-summary-goto-subject article)
9846     (gnus-summary-update-secondary-mark article)))
9847
9848 (defun gnus-summary-set-saved-mark (article)
9849   "Set the process mark on ARTICLE and update the summary line."
9850   (push article gnus-newsgroup-saved)
9851   (when (gnus-summary-goto-subject article)
9852     (gnus-summary-update-secondary-mark article)))
9853
9854 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9855   "Mark N articles as read forwards.
9856 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9857 The difference between N and the actual number of articles marked is
9858 returned.
9859 If NO-EXPIRE, auto-expiry will be inhibited."
9860   (interactive "p")
9861   (gnus-summary-show-thread)
9862   (let ((backward (< n 0))
9863         (gnus-summary-goto-unread
9864          (and gnus-summary-goto-unread
9865               (not (eq gnus-summary-goto-unread 'never))
9866               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9867                                     gnus-ticked-mark gnus-dormant-mark)))))
9868         (n (abs n))
9869         (mark (or mark gnus-del-mark)))
9870     (while (and (> n 0)
9871                 (gnus-summary-mark-article nil mark no-expire)
9872                 (zerop (gnus-summary-next-subject
9873                         (if backward -1 1)
9874                         (and gnus-summary-goto-unread
9875                              (not (eq gnus-summary-goto-unread 'never)))
9876                         t)))
9877       (setq n (1- n)))
9878     (when (/= 0 n)
9879       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9880     (gnus-summary-recenter)
9881     (gnus-summary-position-point)
9882     (gnus-set-mode-line 'summary)
9883     n))
9884
9885 (defun gnus-summary-mark-article-as-read (mark)
9886   "Mark the current article quickly as read with MARK."
9887   (let ((article (gnus-summary-article-number)))
9888     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9889     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9890     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9891     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9892     (push (cons article mark) gnus-newsgroup-reads)
9893     ;; Possibly remove from cache, if that is used.
9894     (when gnus-use-cache
9895       (gnus-cache-enter-remove-article article))
9896     ;; Allow the backend to change the mark.
9897     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9898     ;; Check for auto-expiry.
9899     (when (and gnus-newsgroup-auto-expire
9900                (memq mark gnus-auto-expirable-marks))
9901       (setq mark gnus-expirable-mark)
9902       ;; Let the backend know about the mark change.
9903       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9904       (push article gnus-newsgroup-expirable))
9905     ;; Set the mark in the buffer.
9906     (gnus-summary-update-mark mark 'unread)
9907     t))
9908
9909 (defun gnus-summary-mark-article-as-unread (mark)
9910   "Mark the current article quickly as unread with MARK."
9911   (let* ((article (gnus-summary-article-number))
9912          (old-mark (gnus-summary-article-mark article)))
9913     ;; Allow the backend to change the mark.
9914     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9915     (if (eq mark old-mark)
9916         t
9917       (if (<= article 0)
9918           (progn
9919             (gnus-error 1 "Can't mark negative article numbers")
9920             nil)
9921         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9922         (setq gnus-newsgroup-spam-marked
9923               (delq article gnus-newsgroup-spam-marked))
9924         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9925         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9926         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9927         (cond ((= mark gnus-ticked-mark)
9928                (setq gnus-newsgroup-marked
9929                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9930                                               article)))
9931               ((= mark gnus-spam-mark)
9932                (setq gnus-newsgroup-spam-marked
9933                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9934                                               article)))
9935               ((= mark gnus-dormant-mark)
9936                (setq gnus-newsgroup-dormant
9937                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9938                                               article)))
9939               (t
9940                (setq gnus-newsgroup-unreads
9941                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9942                                               article))))
9943         (gnus-pull article gnus-newsgroup-reads)
9944
9945         ;; See whether the article is to be put in the cache.
9946         (and gnus-use-cache
9947              (vectorp (gnus-summary-article-header article))
9948              (save-excursion
9949                (gnus-cache-possibly-enter-article
9950                 gnus-newsgroup-name article
9951                 (= mark gnus-ticked-mark)
9952                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9953
9954         ;; Fix the mark.
9955         (gnus-summary-update-mark mark 'unread)
9956         t))))
9957
9958 (defun gnus-summary-mark-article (&optional article mark no-expire)
9959   "Mark ARTICLE with MARK.  MARK can be any character.
9960 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9961 `??' (dormant) and `?E' (expirable).
9962 If MARK is nil, then the default character `?r' is used.
9963 If ARTICLE is nil, then the article on the current line will be
9964 marked.
9965 If NO-EXPIRE, auto-expiry will be inhibited."
9966   ;; The mark might be a string.
9967   (when (stringp mark)
9968     (setq mark (aref mark 0)))
9969   ;; If no mark is given, then we check auto-expiring.
9970   (when (null mark)
9971     (setq mark gnus-del-mark))
9972   (when (and (not no-expire)
9973              gnus-newsgroup-auto-expire
9974              (memq mark gnus-auto-expirable-marks))
9975     (setq mark gnus-expirable-mark))
9976   (let ((article (or article (gnus-summary-article-number)))
9977         (old-mark (gnus-summary-article-mark article)))
9978     ;; Allow the backend to change the mark.
9979     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9980     (if (eq mark old-mark)
9981         t
9982       (unless article
9983         (error "No article on current line"))
9984       (if (not (if (or (= mark gnus-unread-mark)
9985                        (= mark gnus-ticked-mark)
9986                        (= mark gnus-spam-mark)
9987                        (= mark gnus-dormant-mark))
9988                    (gnus-mark-article-as-unread article mark)
9989                  (gnus-mark-article-as-read article mark)))
9990           t
9991         ;; See whether the article is to be put in the cache.
9992         (and gnus-use-cache
9993              (not (= mark gnus-canceled-mark))
9994              (vectorp (gnus-summary-article-header article))
9995              (save-excursion
9996                (gnus-cache-possibly-enter-article
9997                 gnus-newsgroup-name article
9998                 (= mark gnus-ticked-mark)
9999                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10000
10001         (when (gnus-summary-goto-subject article nil t)
10002           (let ((buffer-read-only nil))
10003             (gnus-summary-show-thread)
10004             ;; Fix the mark.
10005             (gnus-summary-update-mark mark 'unread)
10006             t))))))
10007
10008 (defun gnus-summary-update-secondary-mark (article)
10009   "Update the secondary (read, process, cache) mark."
10010   (gnus-summary-update-mark
10011    (cond ((memq article gnus-newsgroup-processable)
10012           gnus-process-mark)
10013          ((memq article gnus-newsgroup-cached)
10014           gnus-cached-mark)
10015          ((memq article gnus-newsgroup-replied)
10016           gnus-replied-mark)
10017          ((memq article gnus-newsgroup-forwarded)
10018           gnus-forwarded-mark)
10019          ((memq article gnus-newsgroup-saved)
10020           gnus-saved-mark)
10021          ((memq article gnus-newsgroup-recent)
10022           gnus-recent-mark)
10023          ((memq article gnus-newsgroup-unseen)
10024           gnus-unseen-mark)
10025          (t gnus-no-mark))
10026    'replied)
10027   (when (gnus-visual-p 'summary-highlight 'highlight)
10028     (gnus-run-hooks 'gnus-summary-update-hook))
10029   t)
10030
10031 (defun gnus-summary-update-download-mark (article)
10032   "Update the download mark."
10033   (gnus-summary-update-mark
10034    (cond ((memq article gnus-newsgroup-undownloaded)
10035           gnus-undownloaded-mark)
10036          (gnus-newsgroup-agentized
10037           gnus-downloaded-mark)
10038          (t
10039           gnus-no-mark))
10040    'download)
10041   (gnus-summary-update-line t)
10042   t)
10043
10044 (defun gnus-summary-update-mark (mark type)
10045   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10046         (buffer-read-only nil))
10047     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10048     (when forward
10049       (when (looking-at "\r")
10050         (incf forward))
10051       (when (<= (+ forward (point)) (point-max))
10052         ;; Go to the right position on the line.
10053         (goto-char (+ forward (point)))
10054         ;; Replace the old mark with the new mark.
10055         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10056         ;; Optionally update the marks by some user rule.
10057         (when (eq type 'unread)
10058           (gnus-data-set-mark
10059            (gnus-data-find (gnus-summary-article-number)) mark)
10060           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10061
10062 (defun gnus-mark-article-as-read (article &optional mark)
10063   "Enter ARTICLE in the pertinent lists and remove it from others."
10064   ;; Make the article expirable.
10065   (let ((mark (or mark gnus-del-mark)))
10066     (setq gnus-newsgroup-expirable
10067           (if (= mark gnus-expirable-mark)
10068               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10069             (delq article gnus-newsgroup-expirable)))
10070     ;; Remove from unread and marked lists.
10071     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10072     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10073     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10074     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10075     (push (cons article mark) gnus-newsgroup-reads)
10076     ;; Possibly remove from cache, if that is used.
10077     (when gnus-use-cache
10078       (gnus-cache-enter-remove-article article))
10079     t))
10080
10081 (defun gnus-mark-article-as-unread (article &optional mark)
10082   "Enter ARTICLE in the pertinent lists and remove it from others."
10083   (let ((mark (or mark gnus-ticked-mark)))
10084     (if (<= article 0)
10085         (progn
10086           (gnus-error 1 "Can't mark negative article numbers")
10087           nil)
10088       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10089             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10090             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10091             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10092             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10093
10094       ;; Unsuppress duplicates?
10095       (when gnus-suppress-duplicates
10096         (gnus-dup-unsuppress-article article))
10097
10098       (cond ((= mark gnus-ticked-mark)
10099              (setq gnus-newsgroup-marked
10100                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10101             ((= mark gnus-spam-mark)
10102              (setq gnus-newsgroup-spam-marked
10103                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10104                                             article)))
10105             ((= mark gnus-dormant-mark)
10106              (setq gnus-newsgroup-dormant
10107                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10108             (t
10109              (setq gnus-newsgroup-unreads
10110                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10111       (gnus-pull article gnus-newsgroup-reads)
10112       t)))
10113
10114 (defalias 'gnus-summary-mark-as-unread-forward
10115   'gnus-summary-tick-article-forward)
10116 (make-obsolete 'gnus-summary-mark-as-unread-forward
10117                'gnus-summary-tick-article-forward)
10118 (defun gnus-summary-tick-article-forward (n)
10119   "Tick N articles forwards.
10120 If N is negative, tick backwards instead.
10121 The difference between N and the number of articles ticked is returned."
10122   (interactive "p")
10123   (gnus-summary-mark-forward n gnus-ticked-mark))
10124
10125 (defalias 'gnus-summary-mark-as-unread-backward
10126   'gnus-summary-tick-article-backward)
10127 (make-obsolete 'gnus-summary-mark-as-unread-backward
10128                'gnus-summary-tick-article-backward)
10129 (defun gnus-summary-tick-article-backward (n)
10130   "Tick N articles backwards.
10131 The difference between N and the number of articles ticked is returned."
10132   (interactive "p")
10133   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10134
10135 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10136 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10137 (defun gnus-summary-tick-article (&optional article clear-mark)
10138   "Mark current article as unread.
10139 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10140 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10141   (interactive)
10142   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10143                                        gnus-ticked-mark)))
10144
10145 (defun gnus-summary-mark-as-read-forward (n)
10146   "Mark N articles as read forwards.
10147 If N is negative, mark backwards instead.
10148 The difference between N and the actual number of articles marked is
10149 returned."
10150   (interactive "p")
10151   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10152
10153 (defun gnus-summary-mark-as-read-backward (n)
10154   "Mark the N articles as read backwards.
10155 The difference between N and the actual number of articles marked is
10156 returned."
10157   (interactive "p")
10158   (gnus-summary-mark-forward
10159    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10160
10161 (defun gnus-summary-mark-as-read (&optional article mark)
10162   "Mark current article as read.
10163 ARTICLE specifies the article to be marked as read.
10164 MARK specifies a string to be inserted at the beginning of the line."
10165   (gnus-summary-mark-article article mark))
10166
10167 (defun gnus-summary-clear-mark-forward (n)
10168   "Clear marks from N articles forward.
10169 If N is negative, clear backward instead.
10170 The difference between N and the number of marks cleared is returned."
10171   (interactive "p")
10172   (gnus-summary-mark-forward n gnus-unread-mark))
10173
10174 (defun gnus-summary-clear-mark-backward (n)
10175   "Clear marks from N articles backward.
10176 The difference between N and the number of marks cleared is returned."
10177   (interactive "p")
10178   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10179
10180 (defun gnus-summary-mark-unread-as-read ()
10181   "Intended to be used by `gnus-summary-mark-article-hook'."
10182   (when (memq gnus-current-article gnus-newsgroup-unreads)
10183     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10184
10185 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10186   "Intended to be used by `gnus-summary-mark-article-hook'."
10187   (let ((mark (gnus-summary-article-mark)))
10188     (when (or (gnus-unread-mark-p mark)
10189               (gnus-read-mark-p mark))
10190       (gnus-summary-mark-article gnus-current-article
10191                                  (or new-mark gnus-read-mark)))))
10192
10193 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10194   "Intended to be used by `gnus-summary-mark-article-hook'."
10195   (let ((mark (gnus-summary-article-mark)))
10196     (when (or (gnus-unread-mark-p mark)
10197               (gnus-read-mark-p mark))
10198       (gnus-summary-mark-article (gnus-summary-article-number)
10199                                  (or new-mark gnus-read-mark)))))
10200
10201 (defun gnus-summary-mark-unread-as-ticked ()
10202   "Intended to be used by `gnus-summary-mark-article-hook'."
10203   (when (memq gnus-current-article gnus-newsgroup-unreads)
10204     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10205
10206 (defun gnus-summary-mark-region-as-read (point mark all)
10207   "Mark all unread articles between point and mark as read.
10208 If given a prefix, mark all articles between point and mark as read,
10209 even ticked and dormant ones."
10210   (interactive "r\nP")
10211   (save-excursion
10212     (let (article)
10213       (goto-char point)
10214       (beginning-of-line)
10215       (while (and
10216               (< (point) mark)
10217               (progn
10218                 (when (or all
10219                           (memq (setq article (gnus-summary-article-number))
10220                                 gnus-newsgroup-unreads))
10221                   (gnus-summary-mark-article article gnus-del-mark))
10222                 t)
10223               (gnus-summary-find-next))))))
10224
10225 (defun gnus-summary-mark-below (score mark)
10226   "Mark articles with score less than SCORE with MARK."
10227   (interactive "P\ncMark: ")
10228   (setq score (if score
10229                   (prefix-numeric-value score)
10230                 (or gnus-summary-default-score 0)))
10231   (save-excursion
10232     (set-buffer gnus-summary-buffer)
10233     (goto-char (point-min))
10234     (while
10235         (progn
10236           (and (< (gnus-summary-article-score) score)
10237                (gnus-summary-mark-article nil mark))
10238           (gnus-summary-find-next)))))
10239
10240 (defun gnus-summary-kill-below (&optional score)
10241   "Mark articles with score below SCORE as read."
10242   (interactive "P")
10243   (gnus-summary-mark-below score gnus-killed-mark))
10244
10245 (defun gnus-summary-clear-above (&optional score)
10246   "Clear all marks from articles with score above SCORE."
10247   (interactive "P")
10248   (gnus-summary-mark-above score gnus-unread-mark))
10249
10250 (defun gnus-summary-tick-above (&optional score)
10251   "Tick all articles with score above SCORE."
10252   (interactive "P")
10253   (gnus-summary-mark-above score gnus-ticked-mark))
10254
10255 (defun gnus-summary-mark-above (score mark)
10256   "Mark articles with score over SCORE with MARK."
10257   (interactive "P\ncMark: ")
10258   (setq score (if score
10259                   (prefix-numeric-value score)
10260                 (or gnus-summary-default-score 0)))
10261   (save-excursion
10262     (set-buffer gnus-summary-buffer)
10263     (goto-char (point-min))
10264     (while (and (progn
10265                   (when (> (gnus-summary-article-score) score)
10266                     (gnus-summary-mark-article nil mark))
10267                   t)
10268                 (gnus-summary-find-next)))))
10269
10270 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10271 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10272 (defun gnus-summary-limit-include-expunged (&optional no-error)
10273   "Display all the hidden articles that were expunged for low scores."
10274   (interactive)
10275   (let ((buffer-read-only nil))
10276     (let ((scored gnus-newsgroup-scored)
10277           headers h)
10278       (while scored
10279         (unless (gnus-summary-article-header (caar scored))
10280           (and (setq h (gnus-number-to-header (caar scored)))
10281                (< (cdar scored) gnus-summary-expunge-below)
10282                (push h headers)))
10283         (setq scored (cdr scored)))
10284       (if (not headers)
10285           (when (not no-error)
10286             (error "No expunged articles hidden"))
10287         (goto-char (point-min))
10288         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10289         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10290         (mapcar (lambda (x) (push (mail-header-number x)
10291                                   gnus-newsgroup-limit))
10292                 headers)
10293         (gnus-summary-prepare-unthreaded (nreverse headers))
10294         (goto-char (point-min))
10295         (gnus-summary-position-point)
10296         t))))
10297
10298 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10299   "Mark all unread articles in this newsgroup as read.
10300 If prefix argument ALL is non-nil, ticked and dormant articles will
10301 also be marked as read.
10302 If QUIETLY is non-nil, no questions will be asked.
10303
10304 If TO-HERE is non-nil, it should be a point in the buffer.  All
10305 articles before (after, if REVERSE is set) this point will be marked
10306 as read.
10307
10308 Note that this function will only catch up the unread article
10309 in the current summary buffer limitation.
10310
10311 The number of articles marked as read is returned."
10312   (interactive "P")
10313   (prog1
10314       (save-excursion
10315         (when (or quietly
10316                   (not gnus-interactive-catchup) ;Without confirmation?
10317                   gnus-expert-user
10318                   (gnus-y-or-n-p
10319                    (if all
10320                        "Mark absolutely all articles as read? "
10321                      "Mark all unread articles as read? ")))
10322           (if (and not-mark
10323                    (not gnus-newsgroup-adaptive)
10324                    (not gnus-newsgroup-auto-expire)
10325                    (not gnus-suppress-duplicates)
10326                    (or (not gnus-use-cache)
10327                        (eq gnus-use-cache 'passive)))
10328               (progn
10329                 (when all
10330                   (setq gnus-newsgroup-marked nil
10331                         gnus-newsgroup-spam-marked nil
10332                         gnus-newsgroup-dormant nil))
10333                 (setq gnus-newsgroup-unreads
10334                       (gnus-sorted-nunion
10335                        (gnus-intersection gnus-newsgroup-unreads
10336                                           gnus-newsgroup-downloadable)
10337                        gnus-newsgroup-unfetched)))
10338             ;; We actually mark all articles as canceled, which we
10339             ;; have to do when using auto-expiry or adaptive scoring.
10340             (gnus-summary-show-all-threads)
10341             (if (and to-here reverse)
10342                 (progn
10343                   (goto-char to-here)
10344                   (gnus-summary-mark-current-read-and-unread-as-read
10345                    gnus-catchup-mark)
10346                   (while (gnus-summary-find-next (not all))
10347                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10348               (when (gnus-summary-first-subject (not all))
10349                 (while (and
10350                         (if to-here (< (point) to-here) t)
10351                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10352                         (gnus-summary-find-next (not all))))))
10353             (gnus-set-mode-line 'summary))
10354           t))
10355     (gnus-summary-position-point)))
10356
10357 (defun gnus-summary-catchup-to-here (&optional all)
10358   "Mark all unticked articles before the current one as read.
10359 If ALL is non-nil, also mark ticked and dormant articles as read."
10360   (interactive "P")
10361   (save-excursion
10362     (gnus-save-hidden-threads
10363       (let ((beg (point)))
10364         ;; We check that there are unread articles.
10365         (when (or all (gnus-summary-find-prev))
10366           (gnus-summary-catchup all t beg)))))
10367   (gnus-summary-position-point))
10368
10369 (defun gnus-summary-catchup-from-here (&optional all)
10370   "Mark all unticked articles after (and including) the current one as read.
10371 If ALL is non-nil, also mark ticked and dormant articles as read."
10372   (interactive "P")
10373   (save-excursion
10374     (gnus-save-hidden-threads
10375       (let ((beg (point)))
10376         ;; We check that there are unread articles.
10377         (when (or all (gnus-summary-find-next))
10378           (gnus-summary-catchup all t beg nil t)))))
10379   (gnus-summary-position-point))
10380
10381 (defun gnus-summary-catchup-all (&optional quietly)
10382   "Mark all articles in this newsgroup as read.
10383 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10384 instead, which marks only unread articles as read."
10385   (interactive "P")
10386   (gnus-summary-catchup t quietly))
10387
10388 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10389   "Mark all unread articles in this group as read, then exit.
10390 If prefix argument ALL is non-nil, all articles are marked as read.
10391 If QUIETLY is non-nil, no questions will be asked."
10392   (interactive "P")
10393   (when (gnus-summary-catchup all quietly nil 'fast)
10394     ;; Select next newsgroup or exit.
10395     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10396              (eq gnus-auto-select-next 'quietly))
10397         (gnus-summary-next-group nil)
10398       (gnus-summary-exit))))
10399
10400 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10401   "Mark all articles in this newsgroup as read, and then exit.
10402 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10403 instead, which marks only unread articles as read."
10404   (interactive "P")
10405   (gnus-summary-catchup-and-exit t quietly))
10406
10407 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10408   "Mark all articles in this group as read and select the next group.
10409 If given a prefix, mark all articles, unread as well as ticked, as
10410 read."
10411   (interactive "P")
10412   (save-excursion
10413     (gnus-summary-catchup all))
10414   (gnus-summary-next-group))
10415
10416 ;;;
10417 ;;; with article
10418 ;;;
10419
10420 (defmacro gnus-with-article (article &rest forms)
10421   "Select ARTICLE and perform FORMS in the original article buffer.
10422 Then replace the article with the result."
10423   `(progn
10424      ;; We don't want the article to be marked as read.
10425      (let (gnus-mark-article-hook)
10426        (gnus-summary-select-article t t nil ,article))
10427      (set-buffer gnus-original-article-buffer)
10428      ,@forms
10429      (if (not (gnus-check-backend-function
10430                'request-replace-article (car gnus-article-current)))
10431          (gnus-message 5 "Read-only group; not replacing")
10432        (unless (gnus-request-replace-article
10433                 ,article (car gnus-article-current)
10434                 (current-buffer) t)
10435          (error "Couldn't replace article")))
10436      ;; The cache and backlog have to be flushed somewhat.
10437      (when gnus-keep-backlog
10438        (gnus-backlog-remove-article
10439         (car gnus-article-current) (cdr gnus-article-current)))
10440      (when gnus-use-cache
10441        (gnus-cache-update-article
10442         (car gnus-article-current) (cdr gnus-article-current)))))
10443
10444 (put 'gnus-with-article 'lisp-indent-function 1)
10445 (put 'gnus-with-article 'edebug-form-spec '(form body))
10446
10447 ;; Thread-based commands.
10448
10449 (defun gnus-summary-articles-in-thread (&optional article)
10450   "Return a list of all articles in the current thread.
10451 If ARTICLE is non-nil, return all articles in the thread that starts
10452 with that article."
10453   (let* ((article (or article (gnus-summary-article-number)))
10454          (data (gnus-data-find-list article))
10455          (top-level (gnus-data-level (car data)))
10456          (top-subject
10457           (cond ((null gnus-thread-operation-ignore-subject)
10458                  (gnus-simplify-subject-re
10459                   (mail-header-subject (gnus-data-header (car data)))))
10460                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10461                  (gnus-simplify-subject-fuzzy
10462                   (mail-header-subject (gnus-data-header (car data)))))
10463                 (t nil)))
10464          (end-point (save-excursion
10465                       (if (gnus-summary-go-to-next-thread)
10466                           (point) (point-max))))
10467          articles)
10468     (while (and data
10469                 (< (gnus-data-pos (car data)) end-point))
10470       (when (or (not top-subject)
10471                 (string= top-subject
10472                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10473                              (gnus-simplify-subject-fuzzy
10474                               (mail-header-subject
10475                                (gnus-data-header (car data))))
10476                            (gnus-simplify-subject-re
10477                             (mail-header-subject
10478                              (gnus-data-header (car data)))))))
10479         (push (gnus-data-number (car data)) articles))
10480       (unless (and (setq data (cdr data))
10481                    (> (gnus-data-level (car data)) top-level))
10482         (setq data nil)))
10483     ;; Return the list of articles.
10484     (nreverse articles)))
10485
10486 (defun gnus-summary-rethread-current ()
10487   "Rethread the thread the current article is part of."
10488   (interactive)
10489   (let* ((gnus-show-threads t)
10490          (article (gnus-summary-article-number))
10491          (id (mail-header-id (gnus-summary-article-header)))
10492          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10493     (unless id
10494       (error "No article on the current line"))
10495     (gnus-rebuild-thread id)
10496     (gnus-summary-goto-subject article)))
10497
10498 (defun gnus-summary-reparent-thread ()
10499   "Make the current article child of the marked (or previous) article.
10500
10501 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10502 is non-nil or the Subject: of both articles are the same."
10503   (interactive)
10504   (unless (not (gnus-group-read-only-p))
10505     (error "The current newsgroup does not support article editing"))
10506   (unless (<= (length gnus-newsgroup-processable) 1)
10507     (error "No more than one article may be marked"))
10508   (save-window-excursion
10509     (let ((gnus-article-buffer " *reparent*")
10510           (current-article (gnus-summary-article-number))
10511           ;; First grab the marked article, otherwise one line up.
10512           (parent-article (if (not (null gnus-newsgroup-processable))
10513                               (car gnus-newsgroup-processable)
10514                             (save-excursion
10515                               (if (eq (forward-line -1) 0)
10516                                   (gnus-summary-article-number)
10517                                 (error "Beginning of summary buffer"))))))
10518       (unless (not (eq current-article parent-article))
10519         (error "An article may not be self-referential"))
10520       (let ((message-id (mail-header-id
10521                          (gnus-summary-article-header parent-article))))
10522         (unless (and message-id (not (equal message-id "")))
10523           (error "No message-id in desired parent"))
10524         (gnus-with-article current-article
10525           (save-restriction
10526             (goto-char (point-min))
10527             (message-narrow-to-head)
10528             (if (re-search-forward "^References: " nil t)
10529                 (progn
10530                   (re-search-forward "^[^ \t]" nil t)
10531                   (forward-line -1)
10532                   (end-of-line)
10533                   (insert " " message-id))
10534               (insert "References: " message-id "\n"))))
10535         (set-buffer gnus-summary-buffer)
10536         (gnus-summary-unmark-all-processable)
10537         (gnus-summary-update-article current-article)
10538         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10539             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10540         (gnus-summary-rethread-current)
10541         (gnus-message 3 "Article %d is now the child of article %d"
10542                       current-article parent-article)))))
10543
10544 (defun gnus-summary-toggle-threads (&optional arg)
10545   "Toggle showing conversation threads.
10546 If ARG is positive number, turn showing conversation threads on."
10547   (interactive "P")
10548   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10549     (setq gnus-show-threads
10550           (if (null arg) (not gnus-show-threads)
10551             (> (prefix-numeric-value arg) 0)))
10552     (gnus-summary-prepare)
10553     (gnus-summary-goto-subject current)
10554     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10555     (gnus-summary-position-point)))
10556
10557 (defun gnus-summary-show-all-threads ()
10558   "Show all threads."
10559   (interactive)
10560   (save-excursion
10561     (let ((buffer-read-only nil))
10562       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10563   (gnus-summary-position-point))
10564
10565 (defun gnus-summary-show-thread ()
10566   "Show thread subtrees.
10567 Returns nil if no thread was there to be shown."
10568   (interactive)
10569   (let ((buffer-read-only nil)
10570         (orig (point))
10571         (end (point-at-eol))
10572         ;; Leave point at bol
10573         (beg (progn (beginning-of-line) (point))))
10574     (prog1
10575         ;; Any hidden lines here?
10576         (search-forward "\r" end t)
10577       (subst-char-in-region beg end ?\^M ?\n t)
10578       (goto-char orig)
10579       (gnus-summary-position-point))))
10580
10581 (defun gnus-summary-maybe-hide-threads ()
10582   "If requested, hide the threads that should be hidden."
10583   (when (and gnus-show-threads
10584              gnus-thread-hide-subtree)
10585     (gnus-summary-hide-all-threads
10586      (if (or (consp gnus-thread-hide-subtree)
10587              (functionp gnus-thread-hide-subtree))
10588          (gnus-make-predicate gnus-thread-hide-subtree)
10589        nil))))
10590
10591 ;;; Hiding predicates.
10592
10593 (defun gnus-article-unread-p (header)
10594   (memq (mail-header-number header) gnus-newsgroup-unreads))
10595
10596 (defun gnus-article-unseen-p (header)
10597   (memq (mail-header-number header) gnus-newsgroup-unseen))
10598
10599 (defun gnus-map-articles (predicate articles)
10600   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10601   (apply 'gnus-or (mapcar predicate
10602                           (mapcar 'gnus-summary-article-header articles))))
10603
10604 (defun gnus-summary-hide-all-threads (&optional predicate)
10605   "Hide all thread subtrees.
10606 If PREDICATE is supplied, threads that satisfy this predicate
10607 will not be hidden."
10608   (interactive)
10609   (save-excursion
10610     (goto-char (point-min))
10611     (let ((end nil))
10612       (while (not end)
10613         (when (or (not predicate)
10614                   (gnus-map-articles
10615                    predicate (gnus-summary-article-children)))
10616             (gnus-summary-hide-thread))
10617         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10618   (gnus-summary-position-point))
10619
10620 (defun gnus-summary-hide-thread ()
10621   "Hide thread subtrees.
10622 If PREDICATE is supplied, threads that satisfy this predicate
10623 will not be hidden.
10624 Returns nil if no threads were there to be hidden."
10625   (interactive)
10626   (let ((buffer-read-only nil)
10627         (start (point))
10628         (article (gnus-summary-article-number)))
10629     (goto-char start)
10630     ;; Go forward until either the buffer ends or the subthread
10631     ;; ends.
10632     (when (and (not (eobp))
10633                (or (zerop (gnus-summary-next-thread 1 t))
10634                    (goto-char (point-max))))
10635       (prog1
10636           (if (and (> (point) start)
10637                    (search-backward "\n" start t))
10638               (progn
10639                 (subst-char-in-region start (point) ?\n ?\^M)
10640                 (gnus-summary-goto-subject article))
10641             (goto-char start)
10642             nil)))))
10643
10644 (defun gnus-summary-go-to-next-thread (&optional previous)
10645   "Go to the same level (or less) next thread.
10646 If PREVIOUS is non-nil, go to previous thread instead.
10647 Return the article number moved to, or nil if moving was impossible."
10648   (let ((level (gnus-summary-thread-level))
10649         (way (if previous -1 1))
10650         (beg (point)))
10651     (forward-line way)
10652     (while (and (not (eobp))
10653                 (< level (gnus-summary-thread-level)))
10654       (forward-line way))
10655     (if (eobp)
10656         (progn
10657           (goto-char beg)
10658           nil)
10659       (setq beg (point))
10660       (prog1
10661           (gnus-summary-article-number)
10662         (goto-char beg)))))
10663
10664 (defun gnus-summary-next-thread (n &optional silent)
10665   "Go to the same level next N'th thread.
10666 If N is negative, search backward instead.
10667 Returns the difference between N and the number of skips actually
10668 done.
10669
10670 If SILENT, don't output messages."
10671   (interactive "p")
10672   (let ((backward (< n 0))
10673         (n (abs n)))
10674     (while (and (> n 0)
10675                 (gnus-summary-go-to-next-thread backward))
10676       (decf n))
10677     (unless silent
10678       (gnus-summary-position-point))
10679     (when (and (not silent) (/= 0 n))
10680       (gnus-message 7 "No more threads"))
10681     n))
10682
10683 (defun gnus-summary-prev-thread (n)
10684   "Go to the same level previous N'th thread.
10685 Returns the difference between N and the number of skips actually
10686 done."
10687   (interactive "p")
10688   (gnus-summary-next-thread (- n)))
10689
10690 (defun gnus-summary-go-down-thread ()
10691   "Go down one level in the current thread."
10692   (let ((children (gnus-summary-article-children)))
10693     (when children
10694       (gnus-summary-goto-subject (car children)))))
10695
10696 (defun gnus-summary-go-up-thread ()
10697   "Go up one level in the current thread."
10698   (let ((parent (gnus-summary-article-parent)))
10699     (when parent
10700       (gnus-summary-goto-subject parent))))
10701
10702 (defun gnus-summary-down-thread (n)
10703   "Go down thread N steps.
10704 If N is negative, go up instead.
10705 Returns the difference between N and how many steps down that were
10706 taken."
10707   (interactive "p")
10708   (let ((up (< n 0))
10709         (n (abs n)))
10710     (while (and (> n 0)
10711                 (if up (gnus-summary-go-up-thread)
10712                   (gnus-summary-go-down-thread)))
10713       (setq n (1- n)))
10714     (gnus-summary-position-point)
10715     (when (/= 0 n)
10716       (gnus-message 7 "Can't go further"))
10717     n))
10718
10719 (defun gnus-summary-up-thread (n)
10720   "Go up thread N steps.
10721 If N is negative, go down instead.
10722 Returns the difference between N and how many steps down that were
10723 taken."
10724   (interactive "p")
10725   (gnus-summary-down-thread (- n)))
10726
10727 (defun gnus-summary-top-thread ()
10728   "Go to the top of the thread."
10729   (interactive)
10730   (while (gnus-summary-go-up-thread))
10731   (gnus-summary-article-number))
10732
10733 (defun gnus-summary-kill-thread (&optional unmark)
10734   "Mark articles under current thread as read.
10735 If the prefix argument is positive, remove any kinds of marks.
10736 If the prefix argument is negative, tick articles instead."
10737   (interactive "P")
10738   (when unmark
10739     (setq unmark (prefix-numeric-value unmark)))
10740   (let ((articles (gnus-summary-articles-in-thread)))
10741     (save-excursion
10742       ;; Expand the thread.
10743       (gnus-summary-show-thread)
10744       ;; Mark all the articles.
10745       (while articles
10746         (gnus-summary-goto-subject (car articles))
10747         (cond ((null unmark)
10748                (gnus-summary-mark-article-as-read gnus-killed-mark))
10749               ((> unmark 0)
10750                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10751               (t
10752                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10753         (setq articles (cdr articles))))
10754     ;; Hide killed subtrees.
10755     (and (null unmark)
10756          gnus-thread-hide-killed
10757          (gnus-summary-hide-thread))
10758     ;; If marked as read, go to next unread subject.
10759     (when (null unmark)
10760       ;; Go to next unread subject.
10761       (gnus-summary-next-subject 1 t)))
10762   (gnus-set-mode-line 'summary))
10763
10764 ;; Summary sorting commands
10765
10766 (defun gnus-summary-sort-by-number (&optional reverse)
10767   "Sort the summary buffer by article number.
10768 Argument REVERSE means reverse order."
10769   (interactive "P")
10770   (gnus-summary-sort 'number reverse))
10771
10772 (defun gnus-summary-sort-by-random (&optional reverse)
10773   "Randomize the order in the summary buffer.
10774 Argument REVERSE means to randomize in reverse order."
10775   (interactive "P")
10776   (gnus-summary-sort 'random reverse))
10777
10778 (defun gnus-summary-sort-by-author (&optional reverse)
10779   "Sort the summary buffer by author name alphabetically.
10780 If `case-fold-search' is non-nil, case of letters is ignored.
10781 Argument REVERSE means reverse order."
10782   (interactive "P")
10783   (gnus-summary-sort 'author reverse))
10784
10785 (defun gnus-summary-sort-by-subject (&optional reverse)
10786   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10787 If `case-fold-search' is non-nil, case of letters is ignored.
10788 Argument REVERSE means reverse order."
10789   (interactive "P")
10790   (gnus-summary-sort 'subject reverse))
10791
10792 (defun gnus-summary-sort-by-date (&optional reverse)
10793   "Sort the summary buffer by date.
10794 Argument REVERSE means reverse order."
10795   (interactive "P")
10796   (gnus-summary-sort 'date reverse))
10797
10798 (defun gnus-summary-sort-by-score (&optional reverse)
10799   "Sort the summary buffer by score.
10800 Argument REVERSE means reverse order."
10801   (interactive "P")
10802   (gnus-summary-sort 'score reverse))
10803
10804 (defun gnus-summary-sort-by-lines (&optional reverse)
10805   "Sort the summary buffer by the number of lines.
10806 Argument REVERSE means reverse order."
10807   (interactive "P")
10808   (gnus-summary-sort 'lines reverse))
10809
10810 (defun gnus-summary-sort-by-chars (&optional reverse)
10811   "Sort the summary buffer by article length.
10812 Argument REVERSE means reverse order."
10813   (interactive "P")
10814   (gnus-summary-sort 'chars reverse))
10815
10816 (defun gnus-summary-sort-by-original (&optional reverse)
10817   "Sort the summary buffer using the default sorting method.
10818 Argument REVERSE means reverse order."
10819   (interactive "P")
10820   (let* ((buffer-read-only)
10821          (gnus-summary-prepare-hook nil))
10822     ;; We do the sorting by regenerating the threads.
10823     (gnus-summary-prepare)
10824     ;; Hide subthreads if needed.
10825     (gnus-summary-maybe-hide-threads)))
10826
10827 (defun gnus-summary-sort (predicate reverse)
10828   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10829   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10830          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10831          (gnus-thread-sort-functions
10832           (if (not reverse)
10833               thread
10834             `(lambda (t1 t2)
10835                (,thread t2 t1))))
10836          (gnus-sort-gathered-threads-function
10837           gnus-thread-sort-functions)
10838          (gnus-article-sort-functions
10839           (if (not reverse)
10840               article
10841             `(lambda (t1 t2)
10842                (,article t2 t1))))
10843          (buffer-read-only)
10844          (gnus-summary-prepare-hook nil))
10845     ;; We do the sorting by regenerating the threads.
10846     (gnus-summary-prepare)
10847     ;; Hide subthreads if needed.
10848     (gnus-summary-maybe-hide-threads)))
10849
10850 ;; Summary saving commands.
10851
10852 (defun gnus-summary-save-article (&optional n not-saved)
10853   "Save the current article using the default saver function.
10854 If N is a positive number, save the N next articles.
10855 If N is a negative number, save the N previous articles.
10856 If N is nil and any articles have been marked with the process mark,
10857 save those articles instead.
10858 The variable `gnus-default-article-saver' specifies the saver function."
10859   (interactive "P")
10860   (let* ((articles (gnus-summary-work-articles n))
10861          (save-buffer (save-excursion
10862                         (nnheader-set-temp-buffer " *Gnus Save*")))
10863          (num (length articles))
10864          header file)
10865     (dolist (article articles)
10866       (setq header (gnus-summary-article-header article))
10867       (if (not (vectorp header))
10868           ;; This is a pseudo-article.
10869           (if (assq 'name header)
10870               (gnus-copy-file (cdr (assq 'name header)))
10871             (gnus-message 1 "Article %d is unsaveable" article))
10872         ;; This is a real article.
10873         (save-window-excursion
10874           (let ((gnus-display-mime-function nil)
10875                 (gnus-article-prepare-hook nil))
10876             (gnus-summary-select-article t nil nil article)))
10877         (save-excursion
10878           (set-buffer save-buffer)
10879           (erase-buffer)
10880           (insert-buffer-substring gnus-original-article-buffer))
10881         (setq file (gnus-article-save save-buffer file num))
10882         (gnus-summary-remove-process-mark article)
10883         (unless not-saved
10884           (gnus-summary-set-saved-mark article))))
10885     (gnus-kill-buffer save-buffer)
10886     (gnus-summary-position-point)
10887     (gnus-set-mode-line 'summary)
10888     n))
10889
10890 (defun gnus-summary-pipe-output (&optional arg headers)
10891   "Pipe the current article to a subprocess.
10892 If N is a positive number, pipe the N next articles.
10893 If N is a negative number, pipe the N previous articles.
10894 If N is nil and any articles have been marked with the process mark,
10895 pipe those articles instead.
10896 If HEADERS (the symbolic prefix), include the headers, too."
10897   (interactive (gnus-interactive "P\ny"))
10898   (require 'gnus-art)
10899   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10900         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10901     (gnus-summary-save-article arg t))
10902   (let ((buffer (get-buffer "*Shell Command Output*")))
10903     (when (and buffer
10904                (not (zerop (buffer-size buffer))))
10905       (gnus-configure-windows 'pipe))))
10906
10907 (defun gnus-summary-save-article-mail (&optional arg)
10908   "Append the current article to an mail file.
10909 If N is a positive number, save the N next articles.
10910 If N is a negative number, save the N previous articles.
10911 If N is nil and any articles have been marked with the process mark,
10912 save those articles instead."
10913   (interactive "P")
10914   (require 'gnus-art)
10915   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10916     (gnus-summary-save-article arg)))
10917
10918 (defun gnus-summary-save-article-rmail (&optional arg)
10919   "Append the current article to an rmail file.
10920 If N is a positive number, save the N next articles.
10921 If N is a negative number, save the N previous articles.
10922 If N is nil and any articles have been marked with the process mark,
10923 save those articles instead."
10924   (interactive "P")
10925   (require 'gnus-art)
10926   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10927     (gnus-summary-save-article arg)))
10928
10929 (defun gnus-summary-save-article-file (&optional arg)
10930   "Append the current article to a file.
10931 If N is a positive number, save the N next articles.
10932 If N is a negative number, save the N previous articles.
10933 If N is nil and any articles have been marked with the process mark,
10934 save those articles instead."
10935   (interactive "P")
10936   (require 'gnus-art)
10937   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10938     (gnus-summary-save-article arg)))
10939
10940 (defun gnus-summary-write-article-file (&optional arg)
10941   "Write the current article to a file, deleting the previous file.
10942 If N is a positive number, save the N next articles.
10943 If N is a negative number, save the N previous articles.
10944 If N is nil and any articles have been marked with the process mark,
10945 save those articles instead."
10946   (interactive "P")
10947   (require 'gnus-art)
10948   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10949     (gnus-summary-save-article arg)))
10950
10951 (defun gnus-summary-save-article-body-file (&optional arg)
10952   "Append the current article body to a file.
10953 If N is a positive number, save the N next articles.
10954 If N is a negative number, save the N previous articles.
10955 If N is nil and any articles have been marked with the process mark,
10956 save those articles instead."
10957   (interactive "P")
10958   (require 'gnus-art)
10959   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10960     (gnus-summary-save-article arg)))
10961
10962 (defun gnus-summary-muttprint (&optional arg)
10963   "Print the current article using Muttprint.
10964 If N is a positive number, save the N next articles.
10965 If N is a negative number, save the N previous articles.
10966 If N is nil and any articles have been marked with the process mark,
10967 save those articles instead."
10968   (interactive "P")
10969   (require 'gnus-art)
10970   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10971     (gnus-summary-save-article arg t)))
10972
10973 (defun gnus-summary-pipe-message (program)
10974   "Pipe the current article through PROGRAM."
10975   (interactive "sProgram: ")
10976   (gnus-summary-select-article)
10977   (let ((mail-header-separator ""))
10978     (gnus-eval-in-buffer-window gnus-article-buffer
10979       (save-restriction
10980         (widen)
10981         (let ((start (window-start))
10982               buffer-read-only)
10983           (message-pipe-buffer-body program)
10984           (set-window-start (get-buffer-window (current-buffer)) start))))))
10985
10986 (defun gnus-get-split-value (methods)
10987   "Return a value based on the split METHODS."
10988   (let (split-name method result match)
10989     (when methods
10990       (save-excursion
10991         (set-buffer gnus-original-article-buffer)
10992         (save-restriction
10993           (nnheader-narrow-to-headers)
10994           (while (and methods (not split-name))
10995             (goto-char (point-min))
10996             (setq method (pop methods))
10997             (setq match (car method))
10998             (when (cond
10999                    ((stringp match)
11000                     ;; Regular expression.
11001                     (ignore-errors
11002                       (re-search-forward match nil t)))
11003                    ((functionp match)
11004                     ;; Function.
11005                     (save-restriction
11006                       (widen)
11007                       (setq result (funcall match gnus-newsgroup-name))))
11008                    ((consp match)
11009                     ;; Form.
11010                     (save-restriction
11011                       (widen)
11012                       (setq result (eval match)))))
11013               (setq split-name (cdr method))
11014               (cond ((stringp result)
11015                      (push (expand-file-name
11016                             result gnus-article-save-directory)
11017                            split-name))
11018                     ((consp result)
11019                      (setq split-name (append result split-name)))))))))
11020     (nreverse split-name)))
11021
11022 (defun gnus-valid-move-group-p (group)
11023   (and (boundp group)
11024        (symbol-name group)
11025        (symbol-value group)
11026        (gnus-get-function (gnus-find-method-for-group
11027                            (symbol-name group)) 'request-accept-article t)))
11028
11029 (defun gnus-read-move-group-name (prompt default articles prefix)
11030   "Read a group name."
11031   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11032          (minibuffer-confirm-incomplete nil) ; XEmacs
11033          (prom
11034           (format "%s %s to:"
11035                   prompt
11036                   (if (> (length articles) 1)
11037                       (format "these %d articles" (length articles))
11038                     "this article")))
11039          (to-newsgroup
11040           (cond
11041            ((null split-name)
11042             (gnus-completing-read-with-default
11043              default prom
11044              gnus-active-hashtb
11045              'gnus-valid-move-group-p
11046              nil prefix
11047              'gnus-group-history))
11048            ((= 1 (length split-name))
11049             (gnus-completing-read-with-default
11050              (car split-name) prom
11051              gnus-active-hashtb
11052              'gnus-valid-move-group-p
11053              nil nil
11054              'gnus-group-history))
11055            (t
11056             (gnus-completing-read-with-default
11057              nil prom
11058              (mapcar 'list (nreverse split-name))
11059              nil nil nil
11060              'gnus-group-history))))
11061          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11062     (when to-newsgroup
11063       (if (or (string= to-newsgroup "")
11064               (string= to-newsgroup prefix))
11065           (setq to-newsgroup default))
11066       (unless to-newsgroup
11067         (error "No group name entered"))
11068       (or (gnus-active to-newsgroup)
11069           (gnus-activate-group to-newsgroup nil nil to-method)
11070           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11071                                      to-newsgroup))
11072               (or (and (gnus-request-create-group to-newsgroup to-method)
11073                        (gnus-activate-group
11074                         to-newsgroup nil nil to-method)
11075                        (gnus-subscribe-group to-newsgroup))
11076                   (error "Couldn't create group %s" to-newsgroup)))
11077           (error "No such group: %s" to-newsgroup)))
11078     to-newsgroup))
11079
11080 (defun gnus-summary-save-parts (type dir n &optional reverse)
11081   "Save parts matching TYPE to DIR.
11082 If REVERSE, save parts that do not match TYPE."
11083   (interactive
11084    (list (read-string "Save parts of type: "
11085                       (or (car gnus-summary-save-parts-type-history)
11086                           gnus-summary-save-parts-default-mime)
11087                       'gnus-summary-save-parts-type-history)
11088          (setq gnus-summary-save-parts-last-directory
11089                (read-file-name "Save to directory: "
11090                                gnus-summary-save-parts-last-directory
11091                                nil t))
11092          current-prefix-arg))
11093   (gnus-summary-iterate n
11094     (let ((gnus-display-mime-function nil)
11095           (gnus-inhibit-treatment t))
11096       (gnus-summary-select-article))
11097     (save-excursion
11098       (set-buffer gnus-article-buffer)
11099       (let ((handles (or gnus-article-mime-handles
11100                          (mm-dissect-buffer nil gnus-article-loose-mime)
11101                          (and gnus-article-emulate-mime
11102                               (mm-uu-dissect)))))
11103         (when handles
11104           (gnus-summary-save-parts-1 type dir handles reverse)
11105           (unless gnus-article-mime-handles ;; Don't destroy this case.
11106             (mm-destroy-parts handles)))))))
11107
11108 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11109   (if (stringp (car handle))
11110       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11111               (cdr handle))
11112     (when (if reverse
11113               (not (string-match type (mm-handle-media-type handle)))
11114             (string-match type (mm-handle-media-type handle)))
11115       (let ((file (expand-file-name
11116                    (gnus-map-function
11117                     mm-file-name-rewrite-functions
11118                     (file-name-nondirectory
11119                      (or
11120                       (mail-content-type-get
11121                        (mm-handle-disposition handle) 'filename)
11122                       (mail-content-type-get
11123                        (mm-handle-type handle) 'name)
11124                       (concat gnus-newsgroup-name
11125                               "." (number-to-string
11126                                    (cdr gnus-article-current))))))
11127                    dir)))
11128         (unless (file-exists-p file)
11129           (mm-save-part-to-file handle file))))))
11130
11131 ;; Summary extract commands
11132
11133 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11134   (let ((buffer-read-only nil)
11135         (article (gnus-summary-article-number))
11136         after-article b e)
11137     (unless (gnus-summary-goto-subject article)
11138       (error "No such article: %d" article))
11139     (gnus-summary-position-point)
11140     ;; If all commands are to be bunched up on one line, we collect
11141     ;; them here.
11142     (unless gnus-view-pseudos-separately
11143       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11144             files action)
11145         (while ps
11146           (setq action (cdr (assq 'action (car ps))))
11147           (setq files (list (cdr (assq 'name (car ps)))))
11148           (while (and ps (cdr ps)
11149                       (string= (or action "1")
11150                                (or (cdr (assq 'action (cadr ps))) "2")))
11151             (push (cdr (assq 'name (cadr ps))) files)
11152             (setcdr ps (cddr ps)))
11153           (when files
11154             (when (not (string-match "%s" action))
11155               (push " " files))
11156             (push " " files)
11157             (when (assq 'execute (car ps))
11158               (setcdr (assq 'execute (car ps))
11159                       (funcall (if (string-match "%s" action)
11160                                    'format 'concat)
11161                                action
11162                                (mapconcat
11163                                 (lambda (f)
11164                                   (if (equal f " ")
11165                                       f
11166                                     (shell-quote-argument f)))
11167                                 files " ")))))
11168           (setq ps (cdr ps)))))
11169     (if (and gnus-view-pseudos (not not-view))
11170         (while pslist
11171           (when (assq 'execute (car pslist))
11172             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11173                                   (eq gnus-view-pseudos 'not-confirm)))
11174           (setq pslist (cdr pslist)))
11175       (save-excursion
11176         (while pslist
11177           (setq after-article (or (cdr (assq 'article (car pslist)))
11178                                   (gnus-summary-article-number)))
11179           (gnus-summary-goto-subject after-article)
11180           (forward-line 1)
11181           (setq b (point))
11182           (insert "    " (file-name-nondirectory
11183                           (cdr (assq 'name (car pslist))))
11184                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11185           (setq e (point))
11186           (forward-line -1)             ; back to `b'
11187           (gnus-add-text-properties
11188            b (1- e) (list 'gnus-number gnus-reffed-article-number
11189                           gnus-mouse-face-prop gnus-mouse-face))
11190           (gnus-data-enter
11191            after-article gnus-reffed-article-number
11192            gnus-unread-mark b (car pslist) 0 (- e b))
11193           (setq gnus-newsgroup-unreads
11194                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11195                                          gnus-reffed-article-number))
11196           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11197           (setq pslist (cdr pslist)))))))
11198
11199 (defun gnus-pseudos< (p1 p2)
11200   (let ((c1 (cdr (assq 'action p1)))
11201         (c2 (cdr (assq 'action p2))))
11202     (and c1 c2 (string< c1 c2))))
11203
11204 (defun gnus-request-pseudo-article (props)
11205   (cond ((assq 'execute props)
11206          (gnus-execute-command (cdr (assq 'execute props)))))
11207   (let ((gnus-current-article (gnus-summary-article-number)))
11208     (gnus-run-hooks 'gnus-mark-article-hook)))
11209
11210 (defun gnus-execute-command (command &optional automatic)
11211   (save-excursion
11212     (gnus-article-setup-buffer)
11213     (set-buffer gnus-article-buffer)
11214     (setq buffer-read-only nil)
11215     (let ((command (if automatic command
11216                      (read-string "Command: " (cons command 0)))))
11217       (erase-buffer)
11218       (insert "$ " command "\n\n")
11219       (if gnus-view-pseudo-asynchronously
11220           (start-process "gnus-execute" (current-buffer) shell-file-name
11221                          shell-command-switch command)
11222         (call-process shell-file-name nil t nil
11223                       shell-command-switch command)))))
11224
11225 ;; Summary kill commands.
11226
11227 (defun gnus-summary-edit-global-kill (article)
11228   "Edit the \"global\" kill file."
11229   (interactive (list (gnus-summary-article-number)))
11230   (gnus-group-edit-global-kill article))
11231
11232 (defun gnus-summary-edit-local-kill ()
11233   "Edit a local kill file applied to the current newsgroup."
11234   (interactive)
11235   (setq gnus-current-headers (gnus-summary-article-header))
11236   (gnus-group-edit-local-kill
11237    (gnus-summary-article-number) gnus-newsgroup-name))
11238
11239 ;;; Header reading.
11240
11241 (defun gnus-read-header (id &optional header)
11242   "Read the headers of article ID and enter them into the Gnus system."
11243   (let ((group gnus-newsgroup-name)
11244         (gnus-override-method
11245          (or
11246           gnus-override-method
11247           (and (gnus-news-group-p gnus-newsgroup-name)
11248                (car (gnus-refer-article-methods)))))
11249         where)
11250     ;; First we check to see whether the header in question is already
11251     ;; fetched.
11252     (if (stringp id)
11253         ;; This is a Message-ID.
11254         (setq header (or header (gnus-id-to-header id)))
11255       ;; This is an article number.
11256       (setq header (or header (gnus-summary-article-header id))))
11257     (if (and header
11258              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11259         ;; We have found the header.
11260         header
11261       ;; We have to really fetch the header to this article.
11262       (save-excursion
11263         (set-buffer nntp-server-buffer)
11264         (when (setq where (gnus-request-head id group))
11265           (nnheader-fold-continuation-lines)
11266           (goto-char (point-max))
11267           (insert ".\n")
11268           (goto-char (point-min))
11269           (insert "211 ")
11270           (princ (cond
11271                   ((numberp id) id)
11272                   ((cdr where) (cdr where))
11273                   (header (mail-header-number header))
11274                   (t gnus-reffed-article-number))
11275                  (current-buffer))
11276           (insert " Article retrieved.\n"))
11277         (if (or (not where)
11278                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11279             ()                          ; Malformed head.
11280           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11281             (when (and (stringp id)
11282                        (not (string= (gnus-group-real-name group)
11283                                      (car where))))
11284               ;; If we fetched by Message-ID and the article came
11285               ;; from a different group, we fudge some bogus article
11286               ;; numbers for this article.
11287               (mail-header-set-number header gnus-reffed-article-number))
11288             (save-excursion
11289               (set-buffer gnus-summary-buffer)
11290               (decf gnus-reffed-article-number)
11291               (gnus-remove-header (mail-header-number header))
11292               (push header gnus-newsgroup-headers)
11293               (setq gnus-current-headers header)
11294               (push (mail-header-number header) gnus-newsgroup-limit)))
11295           header)))))
11296
11297 (defun gnus-remove-header (number)
11298   "Remove header NUMBER from `gnus-newsgroup-headers'."
11299   (if (and gnus-newsgroup-headers
11300            (= number (mail-header-number (car gnus-newsgroup-headers))))
11301       (pop gnus-newsgroup-headers)
11302     (let ((headers gnus-newsgroup-headers))
11303       (while (and (cdr headers)
11304                   (not (= number (mail-header-number (cadr headers)))))
11305         (pop headers))
11306       (when (cdr headers)
11307         (setcdr headers (cddr headers))))))
11308
11309 ;;;
11310 ;;; summary highlights
11311 ;;;
11312
11313 (defun gnus-highlight-selected-summary ()
11314   "Highlight selected article in summary buffer."
11315   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11316   (when gnus-summary-selected-face
11317     (save-excursion
11318       (let* ((beg (point-at-bol))
11319              (end (point-at-eol))
11320              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11321              (from (if (get-text-property beg gnus-mouse-face-prop)
11322                        beg
11323                      (or (next-single-property-change
11324                           beg gnus-mouse-face-prop nil end)
11325                          beg)))
11326              (to
11327               (if (= from end)
11328                   (- from 2)
11329                 (or (next-single-property-change
11330                      from gnus-mouse-face-prop nil end)
11331                     end))))
11332         ;; If no mouse-face prop on line we will have to = from = end,
11333         ;; so we highlight the entire line instead.
11334         (when (= (+ to 2) from)
11335           (setq from beg)
11336           (setq to end))
11337         (if gnus-newsgroup-selected-overlay
11338             ;; Move old overlay.
11339             (gnus-move-overlay
11340              gnus-newsgroup-selected-overlay from to (current-buffer))
11341           ;; Create new overlay.
11342           (gnus-overlay-put
11343            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11344            'face gnus-summary-selected-face))))))
11345
11346 (defvar gnus-summary-highlight-line-cached nil)
11347 (defvar gnus-summary-highlight-line-trigger nil)
11348
11349 (defun gnus-summary-highlight-line-0 ()
11350   (if (and (eq gnus-summary-highlight-line-trigger
11351                gnus-summary-highlight)
11352            gnus-summary-highlight-line-cached)
11353       gnus-summary-highlight-line-cached
11354     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11355           gnus-summary-highlight-line-cached
11356           (let* ((cond (list 'cond))
11357                  (c cond)
11358                  (list gnus-summary-highlight))
11359             (while list
11360               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11361                               nil))
11362               (setq c (cdr c)
11363                     list (cdr list)))
11364             (gnus-byte-compile (list 'lambda nil cond))))))
11365
11366 (defun gnus-summary-highlight-line ()
11367   "Highlight current line according to `gnus-summary-highlight'."
11368   (let* ((beg (point-at-bol))
11369          (article (or (gnus-summary-article-number) gnus-current-article))
11370          (score (or (cdr (assq article
11371                                gnus-newsgroup-scored))
11372                     gnus-summary-default-score 0))
11373          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11374          (inhibit-read-only t)
11375          (default gnus-summary-default-score)
11376          (default-high gnus-summary-default-high-score)
11377          (default-low gnus-summary-default-low-score)
11378          (uncached (and gnus-summary-use-undownloaded-faces
11379                         (memq article gnus-newsgroup-undownloaded)
11380                         (not (memq article gnus-newsgroup-cached)))))
11381     (let ((face (funcall (gnus-summary-highlight-line-0))))
11382       (unless (eq face (get-text-property beg 'face))
11383         (gnus-put-text-property-excluding-characters-with-faces
11384          beg (point-at-eol) 'face
11385          (setq face (if (boundp face) (symbol-value face) face)))
11386         (when gnus-summary-highlight-line-function
11387           (funcall gnus-summary-highlight-line-function article face))))))
11388
11389 (defun gnus-update-read-articles (group unread &optional compute)
11390   "Update the list of read articles in GROUP.
11391 UNREAD is a sorted list."
11392   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11393         (info (gnus-get-info group))
11394         (prev 1)
11395         read)
11396     (if (or (not info) (not active))
11397         ;; There is no info on this group if it was, in fact,
11398         ;; killed.  Gnus stores no information on killed groups, so
11399         ;; there's nothing to be done.
11400         ;; One could store the information somewhere temporarily,
11401         ;; perhaps...  Hmmm...
11402         ()
11403       ;; Remove any negative articles numbers.
11404       (while (and unread (< (car unread) 0))
11405         (setq unread (cdr unread)))
11406       ;; Remove any expired article numbers
11407       (while (and unread (< (car unread) (car active)))
11408         (setq unread (cdr unread)))
11409       ;; Compute the ranges of read articles by looking at the list of
11410       ;; unread articles.
11411       (while unread
11412         (when (/= (car unread) prev)
11413           (push (if (= prev (1- (car unread))) prev
11414                   (cons prev (1- (car unread))))
11415                 read))
11416         (setq prev (1+ (car unread)))
11417         (setq unread (cdr unread)))
11418       (when (<= prev (cdr active))
11419         (push (cons prev (cdr active)) read))
11420       (setq read (if (> (length read) 1) (nreverse read) read))
11421       (if compute
11422           read
11423         (save-excursion
11424           (let (setmarkundo)
11425             ;; Propagate the read marks to the backend.
11426             (when (gnus-check-backend-function 'request-set-mark group)
11427               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11428                     (add (gnus-remove-from-range read (gnus-info-read info))))
11429                 (when (or add del)
11430                   (unless (gnus-check-group group)
11431                     (error "Can't open server for %s" group))
11432                   (gnus-request-set-mark
11433                    group (delq nil (list (if add (list add 'add '(read)))
11434                                          (if del (list del 'del '(read))))))
11435                   (setq setmarkundo
11436                         `(gnus-request-set-mark
11437                           ,group
11438                           ',(delq nil (list
11439                                        (if del (list del 'add '(read)))
11440                                        (if add (list add 'del '(read))))))))))
11441             (set-buffer gnus-group-buffer)
11442             (gnus-undo-register
11443               `(progn
11444                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11445                  (gnus-info-set-read ',info ',(gnus-info-read info))
11446                  (gnus-get-unread-articles-in-group ',info
11447                                                     (gnus-active ,group))
11448                  (gnus-group-update-group ,group t)
11449                  ,setmarkundo))))
11450         ;; Enter this list into the group info.
11451         (gnus-info-set-read info read)
11452         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11453         (gnus-get-unread-articles-in-group info (gnus-active group))
11454         t))))
11455
11456 (defun gnus-offer-save-summaries ()
11457   "Offer to save all active summary buffers."
11458   (let (buffers)
11459     ;; Go through all buffers and find all summaries.
11460     (dolist (buffer (buffer-list))
11461       (when (and (setq buffer (buffer-name buffer))
11462                  (string-match "Summary" buffer)
11463                  (with-current-buffer buffer
11464                    ;; We check that this is, indeed, a summary buffer.
11465                    (and (eq major-mode 'gnus-summary-mode)
11466                         ;; Also make sure this isn't bogus.
11467                         gnus-newsgroup-prepared
11468                         ;; Also make sure that this isn't a
11469                         ;; dead summary buffer.
11470                         (not gnus-dead-summary-mode))))
11471         (push buffer buffers)))
11472     ;; Go through all these summary buffers and offer to save them.
11473     (when buffers
11474       (save-excursion
11475         (map-y-or-n-p
11476          "Update summary buffer %s? "
11477          (lambda (buf)
11478            (switch-to-buffer buf)
11479            (gnus-summary-exit))
11480          buffers)))))
11481
11482 (defun gnus-summary-setup-default-charset ()
11483   "Setup newsgroup default charset."
11484   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11485       (setq gnus-newsgroup-charset nil)
11486     (let* ((ignored-charsets
11487             (or gnus-newsgroup-ephemeral-ignored-charsets
11488                 (append
11489                  (and gnus-newsgroup-name
11490                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11491                  gnus-newsgroup-ignored-charsets))))
11492       (setq gnus-newsgroup-charset
11493             (or gnus-newsgroup-ephemeral-charset
11494                 (and gnus-newsgroup-name
11495                      (gnus-parameter-charset gnus-newsgroup-name))
11496                 gnus-default-charset))
11497       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11498            ignored-charsets))))
11499
11500 ;;;
11501 ;;; Mime Commands
11502 ;;;
11503
11504 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11505   "Display the current article buffer fully MIME-buttonized.
11506 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11507 treated as multipart/mixed."
11508   (interactive "P")
11509   (require 'gnus-art)
11510   (let ((gnus-unbuttonized-mime-types nil)
11511         (gnus-mime-display-multipart-as-mixed show-all-parts))
11512     (gnus-summary-show-article)))
11513
11514 (defun gnus-summary-repair-multipart (article)
11515   "Add a Content-Type header to a multipart article without one."
11516   (interactive (list (gnus-summary-article-number)))
11517   (gnus-with-article article
11518     (message-narrow-to-head)
11519     (message-remove-header "Mime-Version")
11520     (goto-char (point-max))
11521     (insert "Mime-Version: 1.0\n")
11522     (widen)
11523     (when (search-forward "\n--" nil t)
11524       (let ((separator (buffer-substring (point) (point-at-eol))))
11525         (message-narrow-to-head)
11526         (message-remove-header "Content-Type")
11527         (goto-char (point-max))
11528         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11529                         separator))
11530         (widen))))
11531   (let (gnus-mark-article-hook)
11532     (gnus-summary-select-article t t nil article)))
11533
11534 (defun gnus-summary-toggle-display-buttonized ()
11535   "Toggle the buttonizing of the article buffer."
11536   (interactive)
11537   (require 'gnus-art)
11538   (if (setq gnus-inhibit-mime-unbuttonizing
11539             (not gnus-inhibit-mime-unbuttonizing))
11540       (let ((gnus-unbuttonized-mime-types nil))
11541         (gnus-summary-show-article))
11542     (gnus-summary-show-article)))
11543
11544 ;;;
11545 ;;; Generic summary marking commands
11546 ;;;
11547
11548 (defvar gnus-summary-marking-alist
11549   '((read gnus-del-mark "d")
11550     (unread gnus-unread-mark "u")
11551     (ticked gnus-ticked-mark "!")
11552     (dormant gnus-dormant-mark "?")
11553     (expirable gnus-expirable-mark "e"))
11554   "An alist of names/marks/keystrokes.")
11555
11556 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11557 (defvar gnus-summary-mark-map)
11558
11559 (defun gnus-summary-make-all-marking-commands ()
11560   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11561   (dolist (elem gnus-summary-marking-alist)
11562     (apply 'gnus-summary-make-marking-command elem)))
11563
11564 (defun gnus-summary-make-marking-command (name mark keystroke)
11565   (let ((map (make-sparse-keymap)))
11566     (define-key gnus-summary-generic-mark-map keystroke map)
11567     (dolist (lway `((next "next" next nil "n")
11568                     (next-unread "next unread" next t "N")
11569                     (prev "previous" prev nil "p")
11570                     (prev-unread "previous unread" prev t "P")
11571                     (nomove "" nil nil ,keystroke)))
11572       (let ((func (gnus-summary-make-marking-command-1
11573                    mark (car lway) lway name)))
11574         (setq func (eval func))
11575         (define-key map (nth 4 lway) func)))))
11576
11577 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11578   `(defun ,(intern
11579             (format "gnus-summary-put-mark-as-%s%s"
11580                     name (if (eq way 'nomove)
11581                              ""
11582                            (concat "-" (symbol-name way)))))
11583      (n)
11584      ,(format
11585        "Mark the current article as %s%s.
11586 If N, the prefix, then repeat N times.
11587 If N is negative, move in reverse order.
11588 The difference between N and the actual number of articles marked is
11589 returned."
11590        name (cadr lway))
11591      (interactive "p")
11592      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11593
11594 (defun gnus-summary-generic-mark (n mark move unread)
11595   "Mark N articles with MARK."
11596   (unless (eq major-mode 'gnus-summary-mode)
11597     (error "This command can only be used in the summary buffer"))
11598   (gnus-summary-show-thread)
11599   (let ((nummove
11600          (cond
11601           ((eq move 'next) 1)
11602           ((eq move 'prev) -1)
11603           (t 0))))
11604     (if (zerop nummove)
11605         (setq n 1)
11606       (when (< n 0)
11607         (setq n (abs n)
11608               nummove (* -1 nummove))))
11609     (while (and (> n 0)
11610                 (gnus-summary-mark-article nil mark)
11611                 (zerop (gnus-summary-next-subject nummove unread t)))
11612       (setq n (1- n)))
11613     (when (/= 0 n)
11614       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11615     (gnus-summary-recenter)
11616     (gnus-summary-position-point)
11617     (gnus-set-mode-line 'summary)
11618     n))
11619
11620 (defun gnus-summary-insert-articles (articles)
11621   (when (setq articles
11622               (gnus-sorted-difference articles
11623                                       (mapcar (lambda (h)
11624                                                 (mail-header-number h))
11625                                               gnus-newsgroup-headers)))
11626     (setq gnus-newsgroup-headers
11627           (gnus-merge 'list
11628                       gnus-newsgroup-headers
11629                       (gnus-fetch-headers articles)
11630                       'gnus-article-sort-by-number))
11631     ;; Suppress duplicates?
11632     (when gnus-suppress-duplicates
11633       (gnus-dup-suppress-articles))
11634
11635     ;; We might want to build some more threads first.
11636     (when (and gnus-fetch-old-headers
11637                (eq gnus-headers-retrieved-by 'nov))
11638       (if (eq gnus-fetch-old-headers 'invisible)
11639           (gnus-build-all-threads)
11640         (gnus-build-old-threads)))
11641     ;; Let the Gnus agent mark articles as read.
11642     (when gnus-agent
11643       (gnus-agent-get-undownloaded-list))
11644     ;; Remove list identifiers from subject
11645     (when gnus-list-identifiers
11646       (gnus-summary-remove-list-identifiers))
11647     ;; First and last article in this newsgroup.
11648     (when gnus-newsgroup-headers
11649       (setq gnus-newsgroup-begin
11650             (mail-header-number (car gnus-newsgroup-headers))
11651             gnus-newsgroup-end
11652             (mail-header-number
11653              (gnus-last-element gnus-newsgroup-headers))))
11654     (when gnus-use-scoring
11655       (gnus-possibly-score-headers))))
11656
11657 (defun gnus-summary-insert-old-articles (&optional all)
11658   "Insert all old articles in this group.
11659 If ALL is non-nil, already read articles become readable.
11660 If ALL is a number, fetch this number of articles."
11661   (interactive "P")
11662   (prog1
11663       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11664             older len)
11665         (setq older
11666               ;; Some nntp servers lie about their active range.  When
11667               ;; this happens, the active range can be in the millions.
11668               ;; Use a compressed range to avoid creating a huge list.
11669               (gnus-range-difference (list gnus-newsgroup-active) old))
11670         (setq len (gnus-range-length older))
11671         (cond
11672          ((null older) nil)
11673          ((numberp all)
11674           (if (< all len)
11675               (let ((older-range (nreverse older)))
11676                 (setq older nil)
11677
11678                 (while (> all 0)
11679                   (let* ((r (pop older-range))
11680                          (min (if (numberp r) r (car r)))
11681                          (max (if (numberp r) r (cdr r))))
11682                     (while (and (<= min max)
11683                                 (> all 0))
11684                       (push max older)
11685                       (setq all (1- all)
11686                             max (1- max))))))
11687             (setq older (gnus-uncompress-range older))))
11688          (all
11689           (setq older (gnus-uncompress-range older)))
11690          (t
11691           (when (and (numberp gnus-large-newsgroup)
11692                    (> len gnus-large-newsgroup))
11693               (let* ((cursor-in-echo-area nil)
11694                      (initial (gnus-parameter-large-newsgroup-initial
11695                                gnus-newsgroup-name))
11696                      (input
11697                       (read-string
11698                        (format
11699                         "How many articles from %s (%s %d): "
11700                         (gnus-limit-string
11701                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11702                         (if initial "max" "default")
11703                         len)
11704                        (if initial
11705                            (cons (number-to-string initial)
11706                                  0)))))
11707                 (unless (string-match "^[ \t]*$" input)
11708                   (setq all (string-to-number input))
11709                   (if (< all len)
11710                       (let ((older-range (nreverse older)))
11711                         (setq older nil)
11712
11713                         (while (> all 0)
11714                           (let* ((r (pop older-range))
11715                                  (min (if (numberp r) r (car r)))
11716                                  (max (if (numberp r) r (cdr r))))
11717                             (while (and (<= min max)
11718                                         (> all 0))
11719                               (push max older)
11720                               (setq all (1- all)
11721                                     max (1- max))))))))))
11722           (setq older (gnus-uncompress-range older))))
11723         (if (not older)
11724             (message "No old news.")
11725           (gnus-summary-insert-articles older)
11726           (gnus-summary-limit (gnus-sorted-nunion old older))))
11727     (gnus-summary-position-point)))
11728
11729 (defun gnus-summary-insert-new-articles ()
11730   "Insert all new articles in this group."
11731   (interactive)
11732   (prog1
11733       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11734             (old-active gnus-newsgroup-active)
11735             (nnmail-fetched-sources (list t))
11736             i new)
11737         (setq gnus-newsgroup-active
11738               (gnus-activate-group gnus-newsgroup-name 'scan))
11739         (setq i (cdr gnus-newsgroup-active))
11740         (while (> i (cdr old-active))
11741           (push i new)
11742           (decf i))
11743         (if (not new)
11744             (message "No gnus is bad news")
11745           (gnus-summary-insert-articles new)
11746           (setq gnus-newsgroup-unreads
11747                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11748           (gnus-summary-limit (gnus-sorted-nunion old new))))
11749     (gnus-summary-position-point)))
11750
11751 (gnus-summary-make-all-marking-commands)
11752
11753 (gnus-ems-redefine)
11754
11755 (provide 'gnus-sum)
11756
11757 (run-hooks 'gnus-sum-load-hook)
11758
11759 ;; Local Variables:
11760 ;; coding: iso-8859-1
11761 ;; End:
11762
11763 ;;; gnus-sum.el ends here