69b36a71de868e4d0d13db8494f86bb98faa9b0a
[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-summary-catchup "catchup" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2618               tool-bar-map)))
2619   (if gnus-summary-tool-bar-map
2620       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2621
2622 (defun gnus-score-set-default (var value)
2623   "A version of set that updates the GNU Emacs menu-bar."
2624   (set var value)
2625   ;; It is the message that forces the active status to be updated.
2626   (message ""))
2627
2628 (defun gnus-make-score-map (type)
2629   "Make a summary score map of type TYPE."
2630   (if t
2631       nil
2632     (let ((headers '(("author" "from" string)
2633                      ("subject" "subject" string)
2634                      ("article body" "body" string)
2635                      ("article head" "head" string)
2636                      ("xref" "xref" string)
2637                      ("extra header" "extra" string)
2638                      ("lines" "lines" number)
2639                      ("followups to author" "followup" string)))
2640           (types '((number ("less than" <)
2641                            ("greater than" >)
2642                            ("equal" =))
2643                    (string ("substring" s)
2644                            ("exact string" e)
2645                            ("fuzzy string" f)
2646                            ("regexp" r))))
2647           (perms '(("temporary" (current-time-string))
2648                    ("permanent" nil)
2649                    ("immediate" now)))
2650           header)
2651       (list
2652        (apply
2653         'nconc
2654         (list
2655          (if (eq type 'lower)
2656              "Lower score"
2657            "Increase score"))
2658         (let (outh)
2659           (while headers
2660             (setq header (car headers))
2661             (setq outh
2662                   (cons
2663                    (apply
2664                     'nconc
2665                     (list (car header))
2666                     (let ((ts (cdr (assoc (nth 2 header) types)))
2667                           outt)
2668                       (while ts
2669                         (setq outt
2670                               (cons
2671                                (apply
2672                                 'nconc
2673                                 (list (caar ts))
2674                                 (let ((ps perms)
2675                                       outp)
2676                                   (while ps
2677                                     (setq outp
2678                                           (cons
2679                                            (vector
2680                                             (caar ps)
2681                                             (list
2682                                              'gnus-summary-score-entry
2683                                              (nth 1 header)
2684                                              (if (or (string= (nth 1 header)
2685                                                               "head")
2686                                                      (string= (nth 1 header)
2687                                                               "body"))
2688                                                  ""
2689                                                (list 'gnus-summary-header
2690                                                      (nth 1 header)))
2691                                              (list 'quote (nth 1 (car ts)))
2692                                              (list 'gnus-score-delta-default
2693                                                    nil)
2694                                              (nth 1 (car ps))
2695                                              t)
2696                                             t)
2697                                            outp))
2698                                     (setq ps (cdr ps)))
2699                                   (list (nreverse outp))))
2700                                outt))
2701                         (setq ts (cdr ts)))
2702                       (list (nreverse outt))))
2703                    outh))
2704             (setq headers (cdr headers)))
2705           (list (nreverse outh))))))))
2706
2707 \f
2708
2709 (defun gnus-summary-mode (&optional group)
2710   "Major mode for reading articles.
2711
2712 All normal editing commands are switched off.
2713 \\<gnus-summary-mode-map>
2714 Each line in this buffer represents one article.  To read an
2715 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2716 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2717 respectively.
2718
2719 You can also post articles and send mail from this buffer.  To
2720 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2721 of an article, type `\\[gnus-summary-reply]'.
2722
2723 There are approx. one gazillion commands you can execute in this
2724 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2725
2726 The following commands are available:
2727
2728 \\{gnus-summary-mode-map}"
2729   (interactive)
2730   (kill-all-local-variables)
2731   (when (gnus-visual-p 'summary-menu 'menu)
2732     (gnus-summary-make-menu-bar)
2733     (gnus-summary-make-tool-bar))
2734   (gnus-summary-make-local-variables)
2735   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2736     (gnus-summary-make-local-variables))
2737   (gnus-make-thread-indent-array)
2738   (gnus-simplify-mode-line)
2739   (setq major-mode 'gnus-summary-mode)
2740   (setq mode-name "Summary")
2741   (make-local-variable 'minor-mode-alist)
2742   (use-local-map gnus-summary-mode-map)
2743   (buffer-disable-undo)
2744   (setq buffer-read-only t)             ;Disable modification
2745   (setq truncate-lines t)
2746   (setq selective-display t)
2747   (setq selective-display-ellipses t)   ;Display `...'
2748   (gnus-summary-set-display-table)
2749   (gnus-set-default-directory)
2750   (setq gnus-newsgroup-name group)
2751   (make-local-variable 'gnus-summary-line-format)
2752   (make-local-variable 'gnus-summary-line-format-spec)
2753   (make-local-variable 'gnus-summary-dummy-line-format)
2754   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2755   (make-local-variable 'gnus-summary-mark-positions)
2756   (gnus-make-local-hook 'pre-command-hook)
2757   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2758   (gnus-run-hooks 'gnus-summary-mode-hook)
2759   (turn-on-gnus-mailing-list-mode)
2760   (mm-enable-multibyte)
2761   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2762   (gnus-update-summary-mark-positions))
2763
2764 (defun gnus-summary-make-local-variables ()
2765   "Make all the local summary buffer variables."
2766   (let (global)
2767     (dolist (local gnus-summary-local-variables)
2768       (if (consp local)
2769           (progn
2770             (if (eq (cdr local) 'global)
2771                 ;; Copy the global value of the variable.
2772                 (setq global (symbol-value (car local)))
2773               ;; Use the value from the list.
2774               (setq global (eval (cdr local))))
2775             (set (make-local-variable (car local)) global))
2776         ;; Simple nil-valued local variable.
2777         (set (make-local-variable local) nil)))))
2778
2779 (defun gnus-summary-clear-local-variables ()
2780   (let ((locals gnus-summary-local-variables))
2781     (while locals
2782       (if (consp (car locals))
2783           (and (vectorp (caar locals))
2784                (set (caar locals) nil))
2785         (and (vectorp (car locals))
2786              (set (car locals) nil)))
2787       (setq locals (cdr locals)))))
2788
2789 ;; Summary data functions.
2790
2791 (defmacro gnus-data-number (data)
2792   `(car ,data))
2793
2794 (defmacro gnus-data-set-number (data number)
2795   `(setcar ,data ,number))
2796
2797 (defmacro gnus-data-mark (data)
2798   `(nth 1 ,data))
2799
2800 (defmacro gnus-data-set-mark (data mark)
2801   `(setcar (nthcdr 1 ,data) ,mark))
2802
2803 (defmacro gnus-data-pos (data)
2804   `(nth 2 ,data))
2805
2806 (defmacro gnus-data-set-pos (data pos)
2807   `(setcar (nthcdr 2 ,data) ,pos))
2808
2809 (defmacro gnus-data-header (data)
2810   `(nth 3 ,data))
2811
2812 (defmacro gnus-data-set-header (data header)
2813   `(setf (nth 3 ,data) ,header))
2814
2815 (defmacro gnus-data-level (data)
2816   `(nth 4 ,data))
2817
2818 (defmacro gnus-data-unread-p (data)
2819   `(= (nth 1 ,data) gnus-unread-mark))
2820
2821 (defmacro gnus-data-read-p (data)
2822   `(/= (nth 1 ,data) gnus-unread-mark))
2823
2824 (defmacro gnus-data-pseudo-p (data)
2825   `(consp (nth 3 ,data)))
2826
2827 (defmacro gnus-data-find (number)
2828   `(assq ,number gnus-newsgroup-data))
2829
2830 (defmacro gnus-data-find-list (number &optional data)
2831   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2832      (memq (assq ,number bdata)
2833            bdata)))
2834
2835 (defmacro gnus-data-make (number mark pos header level)
2836   `(list ,number ,mark ,pos ,header ,level))
2837
2838 (defun gnus-data-enter (after-article number mark pos header level offset)
2839   (let ((data (gnus-data-find-list after-article)))
2840     (unless data
2841       (error "No such article: %d" after-article))
2842     (setcdr data (cons (gnus-data-make number mark pos header level)
2843                        (cdr data)))
2844     (setq gnus-newsgroup-data-reverse nil)
2845     (gnus-data-update-list (cddr data) offset)))
2846
2847 (defun gnus-data-enter-list (after-article list &optional offset)
2848   (when list
2849     (let ((data (and after-article (gnus-data-find-list after-article)))
2850           (ilist list))
2851       (if (not (or data
2852                    after-article))
2853           (let ((odata gnus-newsgroup-data))
2854             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2855             (when offset
2856               (gnus-data-update-list odata offset)))
2857       ;; Find the last element in the list to be spliced into the main
2858         ;; list.
2859         (while (cdr list)
2860           (setq list (cdr list)))
2861         (if (not data)
2862             (progn
2863               (setcdr list gnus-newsgroup-data)
2864               (setq gnus-newsgroup-data ilist)
2865               (when offset
2866                 (gnus-data-update-list (cdr list) offset)))
2867           (setcdr list (cdr data))
2868           (setcdr data ilist)
2869           (when offset
2870             (gnus-data-update-list (cdr list) offset))))
2871       (setq gnus-newsgroup-data-reverse nil))))
2872
2873 (defun gnus-data-remove (article &optional offset)
2874   (let ((data gnus-newsgroup-data))
2875     (if (= (gnus-data-number (car data)) article)
2876         (progn
2877           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2878                 gnus-newsgroup-data-reverse nil)
2879           (when offset
2880             (gnus-data-update-list gnus-newsgroup-data offset)))
2881       (while (cdr data)
2882         (when (= (gnus-data-number (cadr data)) article)
2883           (setcdr data (cddr data))
2884           (when offset
2885             (gnus-data-update-list (cdr data) offset))
2886           (setq data nil
2887                 gnus-newsgroup-data-reverse nil))
2888         (setq data (cdr data))))))
2889
2890 (defmacro gnus-data-list (backward)
2891   `(if ,backward
2892        (or gnus-newsgroup-data-reverse
2893            (setq gnus-newsgroup-data-reverse
2894                  (reverse gnus-newsgroup-data)))
2895      gnus-newsgroup-data))
2896
2897 (defun gnus-data-update-list (data offset)
2898   "Add OFFSET to the POS of all data entries in DATA."
2899   (setq gnus-newsgroup-data-reverse nil)
2900   (while data
2901     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2902     (setq data (cdr data))))
2903
2904 (defun gnus-summary-article-pseudo-p (article)
2905   "Say whether this article is a pseudo article or not."
2906   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2907
2908 (defmacro gnus-summary-article-sparse-p (article)
2909   "Say whether this article is a sparse article or not."
2910   `(memq ,article gnus-newsgroup-sparse))
2911
2912 (defmacro gnus-summary-article-ancient-p (article)
2913   "Say whether this article is a sparse article or not."
2914   `(memq ,article gnus-newsgroup-ancient))
2915
2916 (defun gnus-article-parent-p (number)
2917   "Say whether this article is a parent or not."
2918   (let ((data (gnus-data-find-list number)))
2919     (and (cdr data)              ; There has to be an article after...
2920          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2921             (gnus-data-level (nth 1 data))))))
2922
2923 (defun gnus-article-children (number)
2924   "Return a list of all children to NUMBER."
2925   (let* ((data (gnus-data-find-list number))
2926          (level (gnus-data-level (car data)))
2927          children)
2928     (setq data (cdr data))
2929     (while (and data
2930                 (= (gnus-data-level (car data)) (1+ level)))
2931       (push (gnus-data-number (car data)) children)
2932       (setq data (cdr data)))
2933     children))
2934
2935 (defmacro gnus-summary-skip-intangible ()
2936   "If the current article is intangible, then jump to a different article."
2937   '(let ((to (get-text-property (point) 'gnus-intangible)))
2938      (and to (gnus-summary-goto-subject to))))
2939
2940 (defmacro gnus-summary-article-intangible-p ()
2941   "Say whether this article is intangible or not."
2942   '(get-text-property (point) 'gnus-intangible))
2943
2944 (defun gnus-article-read-p (article)
2945   "Say whether ARTICLE is read or not."
2946   (not (or (memq article gnus-newsgroup-marked)
2947            (memq article gnus-newsgroup-spam-marked)
2948            (memq article gnus-newsgroup-unreads)
2949            (memq article gnus-newsgroup-unselected)
2950            (memq article gnus-newsgroup-dormant))))
2951
2952 ;; Some summary mode macros.
2953
2954 (defmacro gnus-summary-article-number ()
2955   "The article number of the article on the current line.
2956 If there isn't an article number here, then we return the current
2957 article number."
2958   '(progn
2959      (gnus-summary-skip-intangible)
2960      (or (get-text-property (point) 'gnus-number)
2961          (gnus-summary-last-subject))))
2962
2963 (defmacro gnus-summary-article-header (&optional number)
2964   "Return the header of article NUMBER."
2965   `(gnus-data-header (gnus-data-find
2966                       ,(or number '(gnus-summary-article-number)))))
2967
2968 (defmacro gnus-summary-thread-level (&optional number)
2969   "Return the level of thread that starts with article NUMBER."
2970   `(if (and (eq gnus-summary-make-false-root 'dummy)
2971             (get-text-property (point) 'gnus-intangible))
2972        0
2973      (gnus-data-level (gnus-data-find
2974                        ,(or number '(gnus-summary-article-number))))))
2975
2976 (defmacro gnus-summary-article-mark (&optional number)
2977   "Return the mark of article NUMBER."
2978   `(gnus-data-mark (gnus-data-find
2979                     ,(or number '(gnus-summary-article-number)))))
2980
2981 (defmacro gnus-summary-article-pos (&optional number)
2982   "Return the position of the line of article NUMBER."
2983   `(gnus-data-pos (gnus-data-find
2984                    ,(or number '(gnus-summary-article-number)))))
2985
2986 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2987 (defmacro gnus-summary-article-subject (&optional number)
2988   "Return current subject string or nil if nothing."
2989   `(let ((headers
2990           ,(if number
2991                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2992              '(gnus-data-header (assq (gnus-summary-article-number)
2993                                       gnus-newsgroup-data)))))
2994      (and headers
2995           (vectorp headers)
2996           (mail-header-subject headers))))
2997
2998 (defmacro gnus-summary-article-score (&optional number)
2999   "Return current article score."
3000   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3001                   gnus-newsgroup-scored))
3002        gnus-summary-default-score 0))
3003
3004 (defun gnus-summary-article-children (&optional number)
3005   "Return a list of article numbers that are children of article NUMBER."
3006   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3007          (level (gnus-data-level (car data)))
3008          l children)
3009     (while (and (setq data (cdr data))
3010                 (> (setq l (gnus-data-level (car data))) level))
3011       (and (= (1+ level) l)
3012            (push (gnus-data-number (car data))
3013                  children)))
3014     (nreverse children)))
3015
3016 (defun gnus-summary-article-parent (&optional number)
3017   "Return the article number of the parent of article NUMBER."
3018   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3019                                     (gnus-data-list t)))
3020          (level (gnus-data-level (car data))))
3021     (if (zerop level)
3022         ()                              ; This is a root.
3023       ;; We search until we find an article with a level less than
3024       ;; this one.  That function has to be the parent.
3025       (while (and (setq data (cdr data))
3026                   (not (< (gnus-data-level (car data)) level))))
3027       (and data (gnus-data-number (car data))))))
3028
3029 (defun gnus-unread-mark-p (mark)
3030   "Say whether MARK is the unread mark."
3031   (= mark gnus-unread-mark))
3032
3033 (defun gnus-read-mark-p (mark)
3034   "Say whether MARK is one of the marks that mark as read.
3035 This is all marks except unread, ticked, dormant, and expirable."
3036   (not (or (= mark gnus-unread-mark)
3037            (= mark gnus-ticked-mark)
3038            (= mark gnus-spam-mark)
3039            (= mark gnus-dormant-mark)
3040            (= mark gnus-expirable-mark))))
3041
3042 (defmacro gnus-article-mark (number)
3043   "Return the MARK of article NUMBER.
3044 This macro should only be used when computing the mark the \"first\"
3045 time; i.e., when generating the summary lines.  After that,
3046 `gnus-summary-article-mark' should be used to examine the
3047 marks of articles."
3048   `(cond
3049     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3050     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3051     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3052     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3053     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3054     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3055     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3056     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3057            gnus-ancient-mark))))
3058
3059 ;; Saving hidden threads.
3060
3061 (defmacro gnus-save-hidden-threads (&rest forms)
3062   "Save hidden threads, eval FORMS, and restore the hidden threads."
3063   (let ((config (make-symbol "config")))
3064     `(let ((,config (gnus-hidden-threads-configuration)))
3065        (unwind-protect
3066            (save-excursion
3067              ,@forms)
3068          (gnus-restore-hidden-threads-configuration ,config)))))
3069 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3070 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3071
3072 (defun gnus-data-compute-positions ()
3073   "Compute the positions of all articles."
3074   (setq gnus-newsgroup-data-reverse nil)
3075   (let ((data gnus-newsgroup-data))
3076     (save-excursion
3077       (gnus-save-hidden-threads
3078         (gnus-summary-show-all-threads)
3079         (goto-char (point-min))
3080         (while data
3081           (while (get-text-property (point) 'gnus-intangible)
3082             (forward-line 1))
3083           (gnus-data-set-pos (car data) (+ (point) 3))
3084           (setq data (cdr data))
3085           (forward-line 1))))))
3086
3087 (defun gnus-hidden-threads-configuration ()
3088   "Return the current hidden threads configuration."
3089   (save-excursion
3090     (let (config)
3091       (goto-char (point-min))
3092       (while (search-forward "\r" nil t)
3093         (push (1- (point)) config))
3094       config)))
3095
3096 (defun gnus-restore-hidden-threads-configuration (config)
3097   "Restore hidden threads configuration from CONFIG."
3098   (save-excursion
3099     (let (point buffer-read-only)
3100       (while (setq point (pop config))
3101         (when (and (< point (point-max))
3102                    (goto-char point)
3103                    (eq (char-after) ?\n))
3104           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3105
3106 ;; Various summary mode internalish functions.
3107
3108 (defun gnus-mouse-pick-article (e)
3109   (interactive "e")
3110   (mouse-set-point e)
3111   (gnus-summary-next-page nil t))
3112
3113 (defun gnus-summary-set-display-table ()
3114   "Change the display table.
3115 Odd characters have a tendency to mess
3116 up nicely formatted displays - we make all possible glyphs
3117 display only a single character."
3118
3119   ;; We start from the standard display table, if any.
3120   (let ((table (or (copy-sequence standard-display-table)
3121                    (make-display-table)))
3122         (i 32))
3123     ;; Nix out all the control chars...
3124     (while (>= (setq i (1- i)) 0)
3125       (aset table i [??]))
3126    ;; ... but not newline and cr, of course.  (cr is necessary for the
3127     ;; selective display).
3128     (aset table ?\n nil)
3129     (aset table ?\r nil)
3130     ;; We keep TAB as well.
3131     (aset table ?\t nil)
3132     ;; We nix out any glyphs over 126 that are not set already.
3133     (let ((i 256))
3134       (while (>= (setq i (1- i)) 127)
3135         ;; Only modify if the entry is nil.
3136         (unless (aref table i)
3137           (aset table i [??]))))
3138     (setq buffer-display-table table)))
3139
3140 (defun gnus-summary-set-article-display-arrow (pos)
3141   "Update the overlay arrow to point to line at position POS."
3142   (when (and gnus-summary-display-arrow
3143              (boundp 'overlay-arrow-position)
3144              (boundp 'overlay-arrow-string))
3145     (save-excursion
3146       (goto-char pos)
3147       (beginning-of-line)
3148       (unless overlay-arrow-position
3149         (setq overlay-arrow-position (make-marker)))
3150       (setq overlay-arrow-string "=>"
3151             overlay-arrow-position (set-marker overlay-arrow-position
3152                                                (point)
3153                                                (current-buffer))))))
3154
3155 (defun gnus-summary-setup-buffer (group)
3156   "Initialize summary buffer."
3157   (let ((buffer (gnus-summary-buffer-name group))
3158         (dead-name (concat "*Dead Summary "
3159                            (gnus-group-decoded-name group) "*")))
3160     ;; If a dead summary buffer exists, we kill it.
3161     (when (gnus-buffer-live-p dead-name)
3162       (gnus-kill-buffer dead-name))
3163     (if (get-buffer buffer)
3164         (progn
3165           (set-buffer buffer)
3166           (setq gnus-summary-buffer (current-buffer))
3167           (not gnus-newsgroup-prepared))
3168       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3169       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3170       (gnus-summary-mode group)
3171       (when gnus-carpal
3172         (gnus-carpal-setup-buffer 'summary))
3173       (unless gnus-single-article-buffer
3174         (make-local-variable 'gnus-article-buffer)
3175         (make-local-variable 'gnus-article-current)
3176         (make-local-variable 'gnus-original-article-buffer))
3177       (setq gnus-newsgroup-name group)
3178       ;; Set any local variables in the group parameters.
3179       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3180       t)))
3181
3182 (defun gnus-set-global-variables ()
3183   "Set the global equivalents of the buffer-local variables.
3184 They are set to the latest values they had.  These reflect the summary
3185 buffer that was in action when the last article was fetched."
3186   (when (eq major-mode 'gnus-summary-mode)
3187     (setq gnus-summary-buffer (current-buffer))
3188     (let ((name gnus-newsgroup-name)
3189           (marked gnus-newsgroup-marked)
3190           (spam gnus-newsgroup-spam-marked)
3191           (unread gnus-newsgroup-unreads)
3192           (headers gnus-current-headers)
3193           (data gnus-newsgroup-data)
3194           (summary gnus-summary-buffer)
3195           (article-buffer gnus-article-buffer)
3196           (original gnus-original-article-buffer)
3197           (gac gnus-article-current)
3198           (reffed gnus-reffed-article-number)
3199           (score-file gnus-current-score-file)
3200           (default-charset gnus-newsgroup-charset)
3201           vlist)
3202       (let ((locals gnus-newsgroup-variables))
3203         (while locals
3204           (if (consp (car locals))
3205               (push (eval (caar locals)) vlist)
3206             (push (eval (car locals)) vlist))
3207           (setq locals (cdr locals)))
3208         (setq vlist (nreverse vlist)))
3209       (save-excursion
3210         (set-buffer gnus-group-buffer)
3211         (setq gnus-newsgroup-name name
3212               gnus-newsgroup-marked marked
3213               gnus-newsgroup-spam-marked spam
3214               gnus-newsgroup-unreads unread
3215               gnus-current-headers headers
3216               gnus-newsgroup-data data
3217               gnus-article-current gac
3218               gnus-summary-buffer summary
3219               gnus-article-buffer article-buffer
3220               gnus-original-article-buffer original
3221               gnus-reffed-article-number reffed
3222               gnus-current-score-file score-file
3223               gnus-newsgroup-charset default-charset)
3224         (let ((locals gnus-newsgroup-variables))
3225           (while locals
3226             (if (consp (car locals))
3227                 (set (caar locals) (pop vlist))
3228               (set (car locals) (pop vlist)))
3229             (setq locals (cdr locals))))
3230         ;; The article buffer also has local variables.
3231         (when (gnus-buffer-live-p gnus-article-buffer)
3232           (set-buffer gnus-article-buffer)
3233           (setq gnus-summary-buffer summary))))))
3234
3235 (defun gnus-summary-article-unread-p (article)
3236   "Say whether ARTICLE is unread or not."
3237   (memq article gnus-newsgroup-unreads))
3238
3239 (defun gnus-summary-first-article-p (&optional article)
3240   "Return whether ARTICLE is the first article in the buffer."
3241   (if (not (setq article (or article (gnus-summary-article-number))))
3242       nil
3243     (eq article (caar gnus-newsgroup-data))))
3244
3245 (defun gnus-summary-last-article-p (&optional article)
3246   "Return whether ARTICLE is the last article in the buffer."
3247   (if (not (setq article (or article (gnus-summary-article-number))))
3248       ;; All non-existent numbers are the last article.  :-)
3249       t
3250     (not (cdr (gnus-data-find-list article)))))
3251
3252 (defun gnus-make-thread-indent-array ()
3253   (let ((n 200))
3254     (unless (and gnus-thread-indent-array
3255                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3256       (setq gnus-thread-indent-array (make-vector 201 "")
3257             gnus-thread-indent-array-level gnus-thread-indent-level)
3258       (while (>= n 0)
3259         (aset gnus-thread-indent-array n
3260               (make-string (* n gnus-thread-indent-level) ? ))
3261         (setq n (1- n))))))
3262
3263 (defun gnus-update-summary-mark-positions ()
3264   "Compute where the summary marks are to go."
3265   (save-excursion
3266     (when (gnus-buffer-exists-p gnus-summary-buffer)
3267       (set-buffer gnus-summary-buffer))
3268     (let ((gnus-replied-mark 129)
3269           (gnus-score-below-mark 130)
3270           (gnus-score-over-mark 130)
3271           (gnus-undownloaded-mark 131)
3272           (spec gnus-summary-line-format-spec)
3273           gnus-visual pos)
3274       (save-excursion
3275         (gnus-set-work-buffer)
3276         (let ((gnus-summary-line-format-spec spec)
3277               (gnus-newsgroup-downloadable '(0)))
3278           (gnus-summary-insert-line
3279            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3280            0 nil t 128 t nil "" nil 1)
3281           (goto-char (point-min))
3282           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3283                                              (- (point) (point-min) 1)))))
3284           (goto-char (point-min))
3285           (push (cons 'replied (and (search-forward "\201" nil t)
3286                                     (- (point) (point-min) 1)))
3287                 pos)
3288           (goto-char (point-min))
3289           (push (cons 'score (and (search-forward "\202" nil t)
3290                                   (- (point) (point-min) 1)))
3291                 pos)
3292           (goto-char (point-min))
3293           (push (cons 'download
3294                       (and (search-forward "\203" nil t)
3295                            (- (point) (point-min) 1)))
3296                 pos)))
3297       (setq gnus-summary-mark-positions pos))))
3298
3299 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3300   "Insert a dummy root in the summary buffer."
3301   (beginning-of-line)
3302   (gnus-add-text-properties
3303    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3304    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3305
3306 (defun gnus-summary-extract-address-component (from)
3307   (or (car (funcall gnus-extract-address-components from))
3308       from))
3309
3310 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3311   (let ((mail-parse-charset gnus-newsgroup-charset)
3312         ; Is it really necessary to do this next part for each summary line?
3313         ; Luckily, doesn't seem to slow things down much.
3314         (mail-parse-ignored-charsets
3315          (with-current-buffer gnus-summary-buffer
3316            gnus-newsgroup-ignored-charsets)))
3317     (or
3318      (and gnus-ignored-from-addresses
3319           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3320           (let ((extra-headers (mail-header-extra header))
3321                 to
3322                 newsgroups)
3323             (cond
3324              ((setq to (cdr (assq 'To extra-headers)))
3325               (concat "-> "
3326                       (inline
3327                         (gnus-summary-extract-address-component
3328                          (funcall gnus-decode-encoded-word-function to)))))
3329              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3330               (concat "=> " newsgroups)))))
3331      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3332
3333 (defun gnus-summary-insert-line (gnus-tmp-header
3334                                  gnus-tmp-level gnus-tmp-current
3335                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3336                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3337                                  &optional gnus-tmp-dummy gnus-tmp-score
3338                                  gnus-tmp-process)
3339   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3340          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3341          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3342          (gnus-tmp-score-char
3343           (if (or (null gnus-summary-default-score)
3344                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3345                       gnus-summary-zcore-fuzz))
3346               ?                         ;Whitespace
3347             (if (< gnus-tmp-score gnus-summary-default-score)
3348                 gnus-score-below-mark gnus-score-over-mark)))
3349          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3350          (gnus-tmp-replied
3351           (cond (gnus-tmp-process gnus-process-mark)
3352                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3353                  gnus-cached-mark)
3354                 (gnus-tmp-replied gnus-replied-mark)
3355                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3356                  gnus-forwarded-mark)
3357                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3358                  gnus-saved-mark)
3359                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3360                  gnus-recent-mark)
3361                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3362                  gnus-unseen-mark)
3363                 (t gnus-no-mark)))
3364          (gnus-tmp-downloaded
3365           (cond (undownloaded
3366                  gnus-undownloaded-mark)
3367                 (gnus-newsgroup-agentized
3368                  gnus-downloaded-mark)
3369                 (t
3370                  gnus-no-mark)))
3371          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3372          (gnus-tmp-name
3373           (cond
3374            ((string-match "<[^>]+> *$" gnus-tmp-from)
3375             (let ((beg (match-beginning 0)))
3376               (or (and (string-match "^\".+\"" gnus-tmp-from)
3377                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3378                   (substring gnus-tmp-from 0 beg))))
3379            ((string-match "(.+)" gnus-tmp-from)
3380             (substring gnus-tmp-from
3381                        (1+ (match-beginning 0)) (1- (match-end 0))))
3382            (t gnus-tmp-from)))
3383          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3384          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3385          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3386          (buffer-read-only nil))
3387     (when (string= gnus-tmp-name "")
3388       (setq gnus-tmp-name gnus-tmp-from))
3389     (unless (numberp gnus-tmp-lines)
3390       (setq gnus-tmp-lines -1))
3391     (if (= gnus-tmp-lines -1)
3392         (setq gnus-tmp-lines "?")
3393       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3394       (gnus-put-text-property
3395      (point)
3396      (progn (eval gnus-summary-line-format-spec) (point))
3397        'gnus-number gnus-tmp-number)
3398     (when (gnus-visual-p 'summary-highlight 'highlight)
3399       (forward-line -1)
3400       (gnus-run-hooks 'gnus-summary-update-hook)
3401       (forward-line 1))))
3402
3403 (defun gnus-summary-update-line (&optional dont-update)
3404   "Update summary line after change."
3405   (when (and gnus-summary-default-score
3406              (not gnus-summary-inhibit-highlight))
3407     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3408            (article (gnus-summary-article-number))
3409            (score (gnus-summary-article-score article)))
3410       (unless dont-update
3411         (if (and gnus-summary-mark-below
3412                  (< (gnus-summary-article-score)
3413                     gnus-summary-mark-below))
3414             ;; This article has a low score, so we mark it as read.
3415             (when (memq article gnus-newsgroup-unreads)
3416               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3417           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3418             ;; This article was previously marked as read on account
3419             ;; of a low score, but now it has risen, so we mark it as
3420             ;; unread.
3421             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3422         (gnus-summary-update-mark
3423          (if (or (null gnus-summary-default-score)
3424                  (<= (abs (- score gnus-summary-default-score))
3425                      gnus-summary-zcore-fuzz))
3426              ?                          ;Whitespace
3427            (if (< score gnus-summary-default-score)
3428                gnus-score-below-mark gnus-score-over-mark))
3429          'score))
3430       ;; Do visual highlighting.
3431       (when (gnus-visual-p 'summary-highlight 'highlight)
3432         (gnus-run-hooks 'gnus-summary-update-hook)))))
3433
3434 (defvar gnus-tmp-new-adopts nil)
3435
3436 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3437   "Return the number of articles in THREAD.
3438 This may be 0 in some cases -- if none of the articles in
3439 the thread are to be displayed."
3440   (let* ((number
3441          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3442           (cond
3443            ((not (listp thread))
3444             1)
3445            ((and (consp thread) (cdr thread))
3446             (apply
3447              '+ 1 (mapcar
3448                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3449            ((null thread)
3450             1)
3451            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3452             1)
3453            (t 0))))
3454     (when (and level (zerop level) gnus-tmp-new-adopts)
3455       (incf number
3456             (apply '+ (mapcar
3457                        'gnus-summary-number-of-articles-in-thread
3458                        gnus-tmp-new-adopts))))
3459     (if char
3460         (if (> number 1) gnus-not-empty-thread-mark
3461           gnus-empty-thread-mark)
3462       number)))
3463
3464 (defsubst gnus-summary-line-message-size (head)
3465   "Return pretty-printed version of message size.
3466 This function is intended to be used in
3467 `gnus-summary-line-format-alist'."
3468   (let ((c (or (mail-header-chars head) -1)))
3469     (cond ((< c 0) "n/a")               ; chars not available
3470           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3471           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3472           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3473           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3474
3475
3476 (defun gnus-summary-set-local-parameters (group)
3477   "Go through the local params of GROUP and set all variable specs in that list."
3478   (let ((params (gnus-group-find-parameter group))
3479         (vars '(quit-config))           ; Ignore quit-config.
3480         elem)
3481     (while params
3482       (setq elem (car params)
3483             params (cdr params))
3484       (and (consp elem)                 ; Has to be a cons.
3485            (consp (cdr elem))           ; The cdr has to be a list.
3486            (symbolp (car elem))         ; Has to be a symbol in there.
3487            (not (memq (car elem) vars))
3488            (ignore-errors               ; So we set it.
3489              (push (car elem) vars)
3490              (make-local-variable (car elem))
3491              (set (car elem) (eval (nth 1 elem))))))))
3492
3493 (defun gnus-summary-read-group (group &optional show-all no-article
3494                                       kill-buffer no-display backward
3495                                       select-articles)
3496   "Start reading news in newsgroup GROUP.
3497 If SHOW-ALL is non-nil, already read articles are also listed.
3498 If NO-ARTICLE is non-nil, no article is selected initially.
3499 If NO-DISPLAY, don't generate a summary buffer."
3500   (let (result)
3501     (while (and group
3502                 (null (setq result
3503                             (let ((gnus-auto-select-next nil))
3504                               (or (gnus-summary-read-group-1
3505                                    group show-all no-article
3506                                    kill-buffer no-display
3507                                    select-articles)
3508                                   (setq show-all nil
3509                                         select-articles nil)))))
3510                 (eq gnus-auto-select-next 'quietly))
3511       (set-buffer gnus-group-buffer)
3512       ;; The entry function called above goes to the next
3513       ;; group automatically, so we go two groups back
3514       ;; if we are searching for the previous group.
3515       (when backward
3516         (gnus-group-prev-unread-group 2))
3517       (if (not (equal group (gnus-group-group-name)))
3518           (setq group (gnus-group-group-name))
3519         (setq group nil)))
3520     result))
3521
3522 (defun gnus-summary-read-group-1 (group show-all no-article
3523                                         kill-buffer no-display
3524                                         &optional select-articles)
3525   ;; Killed foreign groups can't be entered.
3526   ;;  (when (and (not (gnus-group-native-p group))
3527   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3528   ;;    (error "Dead non-native groups can't be entered"))
3529   (gnus-message 5 "Retrieving newsgroup: %s..."
3530                 (gnus-group-decoded-name group))
3531   (let* ((new-group (gnus-summary-setup-buffer group))
3532          (quit-config (gnus-group-quit-config group))
3533          (did-select (and new-group (gnus-select-newsgroup
3534                                      group show-all select-articles))))
3535     (cond
3536      ;; This summary buffer exists already, so we just select it.
3537      ((not new-group)
3538       (gnus-set-global-variables)
3539       (when kill-buffer
3540         (gnus-kill-or-deaden-summary kill-buffer))
3541       (gnus-configure-windows 'summary 'force)
3542       (gnus-set-mode-line 'summary)
3543       (gnus-summary-position-point)
3544       (message "")
3545       t)
3546      ;; We couldn't select this group.
3547      ((null did-select)
3548       (when (and (eq major-mode 'gnus-summary-mode)
3549                  (not (equal (current-buffer) kill-buffer)))
3550         (kill-buffer (current-buffer))
3551         (if (not quit-config)
3552             (progn
3553               ;; Update the info -- marks might need to be removed,
3554               ;; for instance.
3555               (gnus-summary-update-info)
3556               (set-buffer gnus-group-buffer)
3557               (gnus-group-jump-to-group group)
3558               (gnus-group-next-unread-group 1))
3559           (gnus-handle-ephemeral-exit quit-config)))
3560       (let ((grpinfo (gnus-get-info group)))
3561         (if (null (gnus-info-read grpinfo))
3562             (gnus-message 3 "Group %s contains no messages"
3563                           (gnus-group-decoded-name group))
3564           (gnus-message 3 "Can't select group")))
3565       nil)
3566      ;; The user did a `C-g' while prompting for number of articles,
3567      ;; so we exit this group.
3568      ((eq did-select 'quit)
3569       (and (eq major-mode 'gnus-summary-mode)
3570            (not (equal (current-buffer) kill-buffer))
3571            (kill-buffer (current-buffer)))
3572       (when kill-buffer
3573         (gnus-kill-or-deaden-summary kill-buffer))
3574       (if (not quit-config)
3575           (progn
3576             (set-buffer gnus-group-buffer)
3577             (gnus-group-jump-to-group group)
3578             (gnus-group-next-unread-group 1)
3579             (gnus-configure-windows 'group 'force))
3580         (gnus-handle-ephemeral-exit quit-config))
3581       ;; Finally signal the quit.
3582       (signal 'quit nil))
3583      ;; The group was successfully selected.
3584      (t
3585       (gnus-set-global-variables)
3586       ;; Save the active value in effect when the group was entered.
3587       (setq gnus-newsgroup-active
3588             (gnus-copy-sequence
3589              (gnus-active gnus-newsgroup-name)))
3590       ;; You can change the summary buffer in some way with this hook.
3591       (gnus-run-hooks 'gnus-select-group-hook)
3592       (gnus-update-format-specifications
3593        nil 'summary 'summary-mode 'summary-dummy)
3594       (gnus-update-summary-mark-positions)
3595       ;; Do score processing.
3596       (when gnus-use-scoring
3597         (gnus-possibly-score-headers))
3598       ;; Check whether to fill in the gaps in the threads.
3599       (when gnus-build-sparse-threads
3600         (gnus-build-sparse-threads))
3601       ;; Find the initial limit.
3602       (if gnus-show-threads
3603           (if show-all
3604               (let ((gnus-newsgroup-dormant nil))
3605                 (gnus-summary-initial-limit show-all))
3606             (gnus-summary-initial-limit show-all))
3607         ;; When unthreaded, all articles are always shown.
3608         (setq gnus-newsgroup-limit
3609               (mapcar
3610                (lambda (header) (mail-header-number header))
3611                gnus-newsgroup-headers)))
3612       ;; Generate the summary buffer.
3613       (unless no-display
3614         (gnus-summary-prepare))
3615       (when gnus-use-trees
3616         (gnus-tree-open group)
3617         (setq gnus-summary-highlight-line-function
3618               'gnus-tree-highlight-article))
3619       ;; If the summary buffer is empty, but there are some low-scored
3620       ;; articles or some excluded dormants, we include these in the
3621       ;; buffer.
3622       (when (and (zerop (buffer-size))
3623                  (not no-display))
3624         (cond (gnus-newsgroup-dormant
3625                (gnus-summary-limit-include-dormant))
3626               ((and gnus-newsgroup-scored show-all)
3627                (gnus-summary-limit-include-expunged t))))
3628       ;; Function `gnus-apply-kill-file' must be called in this hook.
3629       (gnus-run-hooks 'gnus-apply-kill-hook)
3630       (if (and (zerop (buffer-size))
3631                (not no-display))
3632           (progn
3633             ;; This newsgroup is empty.
3634             (gnus-summary-catchup-and-exit nil t)
3635             (gnus-message 6 "No unread news")
3636             (when kill-buffer
3637               (gnus-kill-or-deaden-summary kill-buffer))
3638             ;; Return nil from this function.
3639             nil)
3640         ;; Hide conversation thread subtrees.  We cannot do this in
3641         ;; gnus-summary-prepare-hook since kill processing may not
3642         ;; work with hidden articles.
3643         (gnus-summary-maybe-hide-threads)
3644         (when kill-buffer
3645           (gnus-kill-or-deaden-summary kill-buffer))
3646         (gnus-summary-auto-select-subject)
3647         ;; Show first unread article if requested.
3648         (if (and (not no-article)
3649                  (not no-display)
3650                  gnus-newsgroup-unreads
3651                  gnus-auto-select-first)
3652             (progn
3653               (gnus-configure-windows 'summary)
3654               (let ((art (gnus-summary-article-number)))
3655                 (unless (and (not gnus-plugged)
3656                              (or (memq art gnus-newsgroup-undownloaded)
3657                                  (memq art gnus-newsgroup-downloadable)))
3658                   (gnus-summary-goto-article art))))
3659           ;; Don't select any articles.
3660           (gnus-summary-position-point)
3661           (gnus-configure-windows 'summary 'force)
3662           (gnus-set-mode-line 'summary))
3663         (when (and gnus-auto-center-group
3664                    (get-buffer-window gnus-group-buffer t))
3665           ;; Gotta use windows, because recenter does weird stuff if
3666           ;; the current buffer ain't the displayed window.
3667           (let ((owin (selected-window)))
3668             (select-window (get-buffer-window gnus-group-buffer t))
3669             (when (gnus-group-goto-group group)
3670               (recenter))
3671             (select-window owin)))
3672         ;; Mark this buffer as "prepared".
3673         (setq gnus-newsgroup-prepared t)
3674         (gnus-run-hooks 'gnus-summary-prepared-hook)
3675         (unless (gnus-ephemeral-group-p group)
3676           (gnus-group-update-group group))
3677         t)))))
3678
3679 (defun gnus-summary-auto-select-subject ()
3680   "Select the subject line on initial group entry."
3681   (goto-char (point-min))
3682   (cond
3683    ((eq gnus-auto-select-subject 'best)
3684     (gnus-summary-best-unread-subject))
3685    ((eq gnus-auto-select-subject 'unread)
3686     (gnus-summary-first-unread-subject))
3687    ((eq gnus-auto-select-subject 'unseen)
3688     (gnus-summary-first-unseen-subject))
3689    ((eq gnus-auto-select-subject 'unseen-or-unread)
3690     (gnus-summary-first-unseen-or-unread-subject))
3691    ((eq gnus-auto-select-subject 'first)
3692     ;; Do nothing.
3693     )
3694    ((functionp gnus-auto-select-subject)
3695     (funcall gnus-auto-select-subject))))
3696
3697 (defun gnus-summary-prepare ()
3698   "Generate the summary buffer."
3699   (interactive)
3700   (let ((buffer-read-only nil))
3701     (erase-buffer)
3702     (setq gnus-newsgroup-data nil
3703           gnus-newsgroup-data-reverse nil)
3704     (gnus-run-hooks 'gnus-summary-generate-hook)
3705     ;; Generate the buffer, either with threads or without.
3706     (when gnus-newsgroup-headers
3707       (gnus-summary-prepare-threads
3708        (if gnus-show-threads
3709            (gnus-sort-gathered-threads
3710             (funcall gnus-summary-thread-gathering-function
3711                      (gnus-sort-threads
3712                       (gnus-cut-threads (gnus-make-threads)))))
3713          ;; Unthreaded display.
3714          (gnus-sort-articles gnus-newsgroup-headers))))
3715     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3716     ;; Call hooks for modifying summary buffer.
3717     (goto-char (point-min))
3718     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3719
3720 (defsubst gnus-general-simplify-subject (subject)
3721   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3722   (setq subject
3723         (cond
3724          ;; Truncate the subject.
3725          (gnus-simplify-subject-functions
3726           (gnus-map-function gnus-simplify-subject-functions subject))
3727          ((numberp gnus-summary-gather-subject-limit)
3728           (setq subject (gnus-simplify-subject-re subject))
3729           (if (> (length subject) gnus-summary-gather-subject-limit)
3730               (substring subject 0 gnus-summary-gather-subject-limit)
3731             subject))
3732          ;; Fuzzily simplify it.
3733          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3734           (gnus-simplify-subject-fuzzy subject))
3735          ;; Just remove the leading "Re:".
3736          (t
3737           (gnus-simplify-subject-re subject))))
3738
3739   (if (and gnus-summary-gather-exclude-subject
3740            (string-match gnus-summary-gather-exclude-subject subject))
3741       nil                         ; This article shouldn't be gathered
3742     subject))
3743
3744 (defun gnus-summary-simplify-subject-query ()
3745   "Query where the respool algorithm would put this article."
3746   (interactive)
3747   (gnus-summary-select-article)
3748   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3749
3750 (defun gnus-gather-threads-by-subject (threads)
3751   "Gather threads by looking at Subject headers."
3752   (if (not gnus-summary-make-false-root)
3753       threads
3754     (let ((hashtb (gnus-make-hashtable 1024))
3755           (prev threads)
3756           (result threads)
3757           subject hthread whole-subject)
3758       (while threads
3759         (setq subject (gnus-general-simplify-subject
3760                        (setq whole-subject (mail-header-subject
3761                                             (caar threads)))))
3762         (when subject
3763           (if (setq hthread (gnus-gethash subject hashtb))
3764               (progn
3765                 ;; We enter a dummy root into the thread, if we
3766                 ;; haven't done that already.
3767                 (unless (stringp (caar hthread))
3768                   (setcar hthread (list whole-subject (car hthread))))
3769                 ;; We add this new gathered thread to this gathered
3770                 ;; thread.
3771                 (setcdr (car hthread)
3772                         (nconc (cdar hthread) (list (car threads))))
3773                 ;; Remove it from the list of threads.
3774                 (setcdr prev (cdr threads))
3775                 (setq threads prev))
3776             ;; Enter this thread into the hash table.
3777             (gnus-sethash subject
3778                           (if gnus-summary-make-false-root-always
3779                               (progn
3780                                 ;; If you want a dummy root above all
3781                                 ;; threads...
3782                                 (setcar threads (list whole-subject
3783                                                       (car threads)))
3784                                 threads)
3785                             threads)
3786                           hashtb)))
3787         (setq prev threads)
3788         (setq threads (cdr threads)))
3789       result)))
3790
3791 (defun gnus-gather-threads-by-references (threads)
3792   "Gather threads by looking at References headers."
3793   (let ((idhashtb (gnus-make-hashtable 1024))
3794         (thhashtb (gnus-make-hashtable 1024))
3795         (prev threads)
3796         (result threads)
3797         ids references id gthread gid entered ref)
3798     (while threads
3799       (when (setq references (mail-header-references (caar threads)))
3800         (setq id (mail-header-id (caar threads))
3801               ids (inline (gnus-split-references references))
3802               entered nil)
3803         (while (setq ref (pop ids))
3804           (setq ids (delete ref ids))
3805           (if (not (setq gid (gnus-gethash ref idhashtb)))
3806               (progn
3807                 (gnus-sethash ref id idhashtb)
3808                 (gnus-sethash id threads thhashtb))
3809             (setq gthread (gnus-gethash gid thhashtb))
3810             (unless entered
3811               ;; We enter a dummy root into the thread, if we
3812               ;; haven't done that already.
3813               (unless (stringp (caar gthread))
3814                 (setcar gthread (list (mail-header-subject (caar gthread))
3815                                       (car gthread))))
3816               ;; We add this new gathered thread to this gathered
3817               ;; thread.
3818               (setcdr (car gthread)
3819                       (nconc (cdar gthread) (list (car threads)))))
3820             ;; Add it into the thread hash table.
3821             (gnus-sethash id gthread thhashtb)
3822             (setq entered t)
3823             ;; Remove it from the list of threads.
3824             (setcdr prev (cdr threads))
3825             (setq threads prev))))
3826       (setq prev threads)
3827       (setq threads (cdr threads)))
3828     result))
3829
3830 (defun gnus-sort-gathered-threads (threads)
3831   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3832   (let ((result threads))
3833     (while threads
3834       (when (stringp (caar threads))
3835         (setcdr (car threads)
3836                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3837       (setq threads (cdr threads)))
3838     result))
3839
3840 (defun gnus-thread-loop-p (root thread)
3841   "Say whether ROOT is in THREAD."
3842   (let ((stack (list thread))
3843         (infloop 0)
3844         th)
3845     (while (setq thread (pop stack))
3846       (setq th (cdr thread))
3847       (while (and th
3848                   (not (eq (caar th) root)))
3849         (pop th))
3850       (if th
3851           ;; We have found a loop.
3852           (let (ref-dep)
3853             (setcdr thread (delq (car th) (cdr thread)))
3854             (if (boundp (setq ref-dep (intern "none"
3855                                               gnus-newsgroup-dependencies)))
3856                 (setcdr (symbol-value ref-dep)
3857                         (nconc (cdr (symbol-value ref-dep))
3858                                (list (car th))))
3859               (set ref-dep (list nil (car th))))
3860             (setq infloop 1
3861                   stack nil))
3862         ;; Push all the subthreads onto the stack.
3863         (push (cdr thread) stack)))
3864     infloop))
3865
3866 (defun gnus-make-threads ()
3867   "Go through the dependency hashtb and find the roots.  Return all threads."
3868   (let (threads)
3869     (while (catch 'infloop
3870              (mapatoms
3871               (lambda (refs)
3872                 ;; Deal with self-referencing References loops.
3873                 (when (and (car (symbol-value refs))
3874                            (not (zerop
3875                                  (apply
3876                                   '+
3877                                   (mapcar
3878                                    (lambda (thread)
3879                                      (gnus-thread-loop-p
3880                                       (car (symbol-value refs)) thread))
3881                                    (cdr (symbol-value refs)))))))
3882                   (setq threads nil)
3883                   (throw 'infloop t))
3884                 (unless (car (symbol-value refs))
3885                   ;; These threads do not refer back to any other
3886                   ;; articles, so they're roots.
3887                   (setq threads (append (cdr (symbol-value refs)) threads))))
3888               gnus-newsgroup-dependencies)))
3889     threads))
3890
3891 ;; Build the thread tree.
3892 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3893   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3894
3895 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3896 if it was already present.
3897
3898 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3899 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3900 Message-IDs will be renamed to a unique Message-ID before being
3901 entered.
3902
3903 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3904   (let* ((id (mail-header-id header))
3905          (id-dep (and id (intern id dependencies)))
3906          parent-id ref ref-dep ref-header replaced)
3907     ;; Enter this `header' in the `dependencies' table.
3908     (cond
3909      ((not id-dep)
3910       (setq header nil))
3911      ;; The first two cases do the normal part: enter a new `header'
3912      ;; in the `dependencies' table.
3913      ((not (boundp id-dep))
3914       (set id-dep (list header)))
3915      ((null (car (symbol-value id-dep)))
3916       (setcar (symbol-value id-dep) header))
3917
3918      ;; From here the `header' was already present in the
3919      ;; `dependencies' table.
3920      (force-new
3921       ;; Overrides an existing entry;
3922       ;; just set the header part of the entry.
3923       (setcar (symbol-value id-dep) header)
3924       (setq replaced t))
3925
3926      ;; Renames the existing `header' to a unique Message-ID.
3927      ((not gnus-summary-ignore-duplicates)
3928       ;; An article with this Message-ID has already been seen.
3929       ;; We rename the Message-ID.
3930       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3931            (list header))
3932       (mail-header-set-id header id))
3933
3934      ;; The last case ignores an existing entry, except it adds any
3935      ;; additional Xrefs (in case the two articles came from different
3936      ;; servers.
3937      ;; Also sets `header' to `nil' meaning that the `dependencies'
3938      ;; table was *not* modified.
3939      (t
3940       (mail-header-set-xref
3941        (car (symbol-value id-dep))
3942        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3943                    "")
3944                (or (mail-header-xref header) "")))
3945       (setq header nil)))
3946
3947     (when (and header (not replaced))
3948       ;; First check that we are not creating a References loop.
3949       (setq parent-id (gnus-parent-id (mail-header-references header)))
3950       (setq ref parent-id)
3951       (while (and ref
3952                   (setq ref-dep (intern-soft ref dependencies))
3953                   (boundp ref-dep)
3954                   (setq ref-header (car (symbol-value ref-dep))))
3955         (if (string= id ref)
3956             ;; Yuk!  This is a reference loop.  Make the article be a
3957             ;; root article.
3958             (progn
3959               (mail-header-set-references (car (symbol-value id-dep)) "none")
3960               (setq ref nil)
3961               (setq parent-id nil))
3962           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3963       (setq ref-dep (intern (or parent-id "none") dependencies))
3964       (if (boundp ref-dep)
3965           (setcdr (symbol-value ref-dep)
3966                   (nconc (cdr (symbol-value ref-dep))
3967                          (list (symbol-value id-dep))))
3968         (set ref-dep (list nil (symbol-value id-dep)))))
3969     header))
3970
3971 (defun gnus-extract-message-id-from-in-reply-to (string)
3972   (if (string-match "<[^>]+>" string)
3973       (substring string (match-beginning 0) (match-end 0))
3974     nil))
3975
3976 (defun gnus-build-sparse-threads ()
3977   (let ((headers gnus-newsgroup-headers)
3978         (mail-parse-charset gnus-newsgroup-charset)
3979         (gnus-summary-ignore-duplicates t)
3980         header references generation relations
3981         subject child end new-child date)
3982     ;; First we create an alist of generations/relations, where
3983     ;; generations is how much we trust the relation, and the relation
3984     ;; is parent/child.
3985     (gnus-message 7 "Making sparse threads...")
3986     (save-excursion
3987       (nnheader-set-temp-buffer " *gnus sparse threads*")
3988       (while (setq header (pop headers))
3989         (when (and (setq references (mail-header-references header))
3990                    (not (string= references "")))
3991           (insert references)
3992           (setq child (mail-header-id header)
3993                 subject (mail-header-subject header)
3994                 date (mail-header-date header)
3995                 generation 0)
3996           (while (search-backward ">" nil t)
3997             (setq end (1+ (point)))
3998             (when (search-backward "<" nil t)
3999               (setq new-child (buffer-substring (point) end))
4000               (push (list (incf generation)
4001                           child (setq child new-child)
4002                           subject date)
4003                     relations)))
4004           (when child
4005             (push (list (1+ generation) child nil subject) relations))
4006           (erase-buffer)))
4007       (kill-buffer (current-buffer)))
4008     ;; Sort over trustworthiness.
4009     (mapcar
4010      (lambda (relation)
4011        (when (gnus-dependencies-add-header
4012               (make-full-mail-header
4013                gnus-reffed-article-number
4014                (nth 3 relation) "" (or (nth 4 relation) "")
4015                (nth 1 relation)
4016                (or (nth 2 relation) "") 0 0 "")
4017               gnus-newsgroup-dependencies nil)
4018          (push gnus-reffed-article-number gnus-newsgroup-limit)
4019          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4020          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4021                gnus-newsgroup-reads)
4022          (decf gnus-reffed-article-number)))
4023      (sort relations 'car-less-than-car))
4024     (gnus-message 7 "Making sparse threads...done")))
4025
4026 (defun gnus-build-old-threads ()
4027   ;; Look at all the articles that refer back to old articles, and
4028   ;; fetch the headers for the articles that aren't there.  This will
4029   ;; build complete threads - if the roots haven't been expired by the
4030   ;; server, that is.
4031   (let ((mail-parse-charset gnus-newsgroup-charset)
4032         id heads)
4033     (mapatoms
4034      (lambda (refs)
4035        (when (not (car (symbol-value refs)))
4036          (setq heads (cdr (symbol-value refs)))
4037          (while heads
4038            (if (memq (mail-header-number (caar heads))
4039                      gnus-newsgroup-dormant)
4040                (setq heads (cdr heads))
4041              (setq id (symbol-name refs))
4042              (while (and (setq id (gnus-build-get-header id))
4043                          (not (car (gnus-id-to-thread id)))))
4044              (setq heads nil)))))
4045      gnus-newsgroup-dependencies)))
4046
4047 (defsubst gnus-remove-odd-characters (string)
4048   "Translate STRING into something that doesn't contain weird characters."
4049   (mm-subst-char-in-string
4050    ?\r ?\-
4051    (mm-subst-char-in-string ?\n ?\- string t) t))
4052
4053 ;; This function has to be called with point after the article number
4054 ;; on the beginning of the line.
4055 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4056   (let ((eol (point-at-eol))
4057         (buffer (current-buffer))
4058         header references in-reply-to)
4059
4060     ;; overview: [num subject from date id refs chars lines misc]
4061     (unwind-protect
4062         (let (x)
4063           (narrow-to-region (point) eol)
4064           (unless (eobp)
4065             (forward-char))
4066
4067           (setq header
4068                 (make-full-mail-header
4069                  number                 ; number
4070                  (condition-case ()     ; subject
4071                      (gnus-remove-odd-characters
4072                       (funcall gnus-decode-encoded-word-function
4073                                (setq x (nnheader-nov-field))))
4074                    (error x))
4075                  (condition-case ()     ; from
4076                      (gnus-remove-odd-characters
4077                       (funcall gnus-decode-encoded-word-function
4078                                (setq x (nnheader-nov-field))))
4079                    (error x))
4080                  (nnheader-nov-field)   ; date
4081                  (nnheader-nov-read-message-id) ; id
4082                  (setq references (nnheader-nov-field)) ; refs
4083                  (nnheader-nov-read-integer) ; chars
4084                  (nnheader-nov-read-integer) ; lines
4085                  (unless (eobp)
4086                    (if (looking-at "Xref: ")
4087                        (goto-char (match-end 0)))
4088                    (nnheader-nov-field)) ; Xref
4089                  (nnheader-nov-parse-extra)))) ; extra
4090
4091       (widen))
4092
4093     (when (and (string= references "")
4094                (setq in-reply-to (mail-header-extra header))
4095                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4096       (mail-header-set-references
4097        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4098
4099     (when gnus-alter-header-function
4100       (funcall gnus-alter-header-function header))
4101     (gnus-dependencies-add-header header dependencies force-new)))
4102
4103 (defun gnus-build-get-header (id)
4104   "Look through the buffer of NOV lines and find the header to ID.
4105 Enter this line into the dependencies hash table, and return
4106 the id of the parent article (if any)."
4107   (let ((deps gnus-newsgroup-dependencies)
4108         found header)
4109     (prog1
4110         (save-excursion
4111           (set-buffer nntp-server-buffer)
4112           (let ((case-fold-search nil))
4113             (goto-char (point-min))
4114             (while (and (not found)
4115                         (search-forward id nil t))
4116               (beginning-of-line)
4117               (setq found (looking-at
4118                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4119                                    (regexp-quote id))))
4120               (or found (beginning-of-line 2)))
4121             (when found
4122               (beginning-of-line)
4123               (and
4124                (setq header (gnus-nov-parse-line
4125                              (read (current-buffer)) deps))
4126                (gnus-parent-id (mail-header-references header))))))
4127       (when header
4128         (let ((number (mail-header-number header)))
4129           (push number gnus-newsgroup-limit)
4130           (push header gnus-newsgroup-headers)
4131           (if (memq number gnus-newsgroup-unselected)
4132               (progn
4133                 (setq gnus-newsgroup-unreads
4134                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4135                                                number))
4136                 (setq gnus-newsgroup-unselected
4137                       (delq number gnus-newsgroup-unselected)))
4138             (push number gnus-newsgroup-ancient)))))))
4139
4140 (defun gnus-build-all-threads ()
4141   "Read all the headers."
4142   (let ((gnus-summary-ignore-duplicates t)
4143         (mail-parse-charset gnus-newsgroup-charset)
4144         (dependencies gnus-newsgroup-dependencies)
4145         header article)
4146     (save-excursion
4147       (set-buffer nntp-server-buffer)
4148       (let ((case-fold-search nil))
4149         (goto-char (point-min))
4150         (while (not (eobp))
4151           (ignore-errors
4152             (setq article (read (current-buffer))
4153                   header (gnus-nov-parse-line article dependencies)))
4154           (when header
4155             (save-excursion
4156               (set-buffer gnus-summary-buffer)
4157               (push header gnus-newsgroup-headers)
4158               (if (memq (setq article (mail-header-number header))
4159                         gnus-newsgroup-unselected)
4160                   (progn
4161                     (setq gnus-newsgroup-unreads
4162                           (gnus-add-to-sorted-list
4163                            gnus-newsgroup-unreads article))
4164                     (setq gnus-newsgroup-unselected
4165                           (delq article gnus-newsgroup-unselected)))
4166                 (push article gnus-newsgroup-ancient)))
4167             (forward-line 1)))))))
4168
4169 (defun gnus-summary-update-article-line (article header)
4170   "Update the line for ARTICLE using HEADER."
4171   (let* ((id (mail-header-id header))
4172          (thread (gnus-id-to-thread id)))
4173     (unless thread
4174       (error "Article in no thread"))
4175     ;; Update the thread.
4176     (setcar thread header)
4177     (gnus-summary-goto-subject article)
4178     (let* ((datal (gnus-data-find-list article))
4179            (data (car datal))
4180            (buffer-read-only nil)
4181            (level (gnus-summary-thread-level)))
4182       (gnus-delete-line)
4183       (let ((inserted (- (point)
4184                          (progn
4185                            (gnus-summary-insert-line
4186                             header level nil
4187                             (memq article gnus-newsgroup-undownloaded)
4188                             (gnus-article-mark article)
4189                             (memq article gnus-newsgroup-replied)
4190                             (memq article gnus-newsgroup-expirable)
4191                             ;; Only insert the Subject string when it's different
4192                             ;; from the previous Subject string.
4193                             (if (and
4194                                  gnus-show-threads
4195                                  (gnus-subject-equal
4196                                   (condition-case ()
4197                                       (mail-header-subject
4198                                        (gnus-data-header
4199                                         (cadr
4200                                          (gnus-data-find-list
4201                                           article
4202                                           (gnus-data-list t)))))
4203                                     ;; Error on the side of excessive subjects.
4204                                     (error ""))
4205                                   (mail-header-subject header)))
4206                                 ""
4207                               (mail-header-subject header))
4208                             nil (cdr (assq article gnus-newsgroup-scored))
4209                             (memq article gnus-newsgroup-processable))
4210                            (point)))))
4211         (when (cdr datal)
4212           (gnus-data-update-list
4213            (cdr datal)
4214            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4215
4216 (defun gnus-summary-update-article (article &optional iheader)
4217   "Update ARTICLE in the summary buffer."
4218   (set-buffer gnus-summary-buffer)
4219   (let* ((header (gnus-summary-article-header article))
4220          (id (mail-header-id header))
4221          (data (gnus-data-find article))
4222          (thread (gnus-id-to-thread id))
4223          (references (mail-header-references header))
4224          (parent
4225           (gnus-id-to-thread
4226            (or (gnus-parent-id
4227                 (when (and references
4228                            (not (equal "" references)))
4229                   references))
4230                "none")))
4231          (buffer-read-only nil)
4232          (old (car thread)))
4233     (when thread
4234       (unless iheader
4235         (setcar thread nil)
4236         (when parent
4237           (delq thread parent)))
4238       (if (gnus-summary-insert-subject id header)
4239           ;; Set the (possibly) new article number in the data structure.
4240           (gnus-data-set-number data (gnus-id-to-article id))
4241         (setcar thread old)
4242         nil))))
4243
4244 (defun gnus-rebuild-thread (id &optional line)
4245   "Rebuild the thread containing ID.
4246 If LINE, insert the rebuilt thread starting on line LINE."
4247   (let ((buffer-read-only nil)
4248         old-pos current thread data)
4249     (if (not gnus-show-threads)
4250         (setq thread (list (car (gnus-id-to-thread id))))
4251       ;; Get the thread this article is part of.
4252       (setq thread (gnus-remove-thread id)))
4253     (setq old-pos (point-at-bol))
4254     (setq current (save-excursion
4255                     (and (re-search-backward "[\r\n]" nil t)
4256                          (gnus-summary-article-number))))
4257     ;; If this is a gathered thread, we have to go some re-gathering.
4258     (when (stringp (car thread))
4259       (let ((subject (car thread))
4260             roots thr)
4261         (setq thread (cdr thread))
4262         (while thread
4263           (unless (memq (setq thr (gnus-id-to-thread
4264                                    (gnus-root-id
4265                                     (mail-header-id (caar thread)))))
4266                         roots)
4267             (push thr roots))
4268           (setq thread (cdr thread)))
4269         ;; We now have all (unique) roots.
4270         (if (= (length roots) 1)
4271             ;; All the loose roots are now one solid root.
4272             (setq thread (car roots))
4273           (setq thread (cons subject (gnus-sort-threads roots))))))
4274     (let (threads)
4275       ;; We then insert this thread into the summary buffer.
4276       (when line
4277         (goto-char (point-min))
4278         (forward-line (1- line)))
4279       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4280         (if gnus-show-threads
4281             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4282           (gnus-summary-prepare-unthreaded thread))
4283         (setq data (nreverse gnus-newsgroup-data))
4284         (setq threads gnus-newsgroup-threads))
4285       ;; We splice the new data into the data structure.
4286       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4287       ;;!!! then we want to insert at the beginning of the buffer.
4288       ;;!!! That happens to be true with Gnus now, but that may
4289       ;;!!! change in the future.  Perhaps.
4290       (gnus-data-enter-list
4291        (if line nil current) data (- (point) old-pos))
4292       (setq gnus-newsgroup-threads
4293             (nconc threads gnus-newsgroup-threads))
4294       (gnus-data-compute-positions))))
4295
4296 (defun gnus-number-to-header (number)
4297   "Return the header for article NUMBER."
4298   (let ((headers gnus-newsgroup-headers))
4299     (while (and headers
4300                 (not (= number (mail-header-number (car headers)))))
4301       (pop headers))
4302     (when headers
4303       (car headers))))
4304
4305 (defun gnus-parent-headers (in-headers &optional generation)
4306   "Return the headers of the GENERATIONeth parent of HEADERS."
4307   (unless generation
4308     (setq generation 1))
4309   (let ((parent t)
4310         (headers in-headers)
4311         references)
4312     (while (and parent
4313                 (not (zerop generation))
4314                 (setq references (mail-header-references headers)))
4315       (setq headers (if (and references
4316                              (setq parent (gnus-parent-id references)))
4317                         (car (gnus-id-to-thread parent))
4318                       nil))
4319       (decf generation))
4320     (and (not (eq headers in-headers))
4321          headers)))
4322
4323 (defun gnus-id-to-thread (id)
4324   "Return the (sub-)thread where ID appears."
4325   (gnus-gethash id gnus-newsgroup-dependencies))
4326
4327 (defun gnus-id-to-article (id)
4328   "Return the article number of ID."
4329   (let ((thread (gnus-id-to-thread id)))
4330     (when (and thread
4331                (car thread))
4332       (mail-header-number (car thread)))))
4333
4334 (defun gnus-id-to-header (id)
4335   "Return the article headers of ID."
4336   (car (gnus-id-to-thread id)))
4337
4338 (defun gnus-article-displayed-root-p (article)
4339   "Say whether ARTICLE is a root(ish) article."
4340   (let ((level (gnus-summary-thread-level article))
4341         (refs (mail-header-references  (gnus-summary-article-header article)))
4342         particle)
4343     (cond
4344      ((null level) nil)
4345      ((zerop level) t)
4346      ((null refs) t)
4347      ((null (gnus-parent-id refs)) t)
4348      ((and (= 1 level)
4349            (null (setq particle (gnus-id-to-article
4350                                  (gnus-parent-id refs))))
4351            (null (gnus-summary-thread-level particle)))))))
4352
4353 (defun gnus-root-id (id)
4354   "Return the id of the root of the thread where ID appears."
4355   (let (last-id prev)
4356     (while (and id (setq prev (car (gnus-id-to-thread id))))
4357       (setq last-id id
4358             id (gnus-parent-id (mail-header-references prev))))
4359     last-id))
4360
4361 (defun gnus-articles-in-thread (thread)
4362   "Return the list of articles in THREAD."
4363   (cons (mail-header-number (car thread))
4364         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4365
4366 (defun gnus-remove-thread (id &optional dont-remove)
4367   "Remove the thread that has ID in it."
4368   (let (headers thread last-id)
4369     ;; First go up in this thread until we find the root.
4370     (setq last-id (gnus-root-id id)
4371           headers (message-flatten-list (gnus-id-to-thread last-id)))
4372     ;; We have now found the real root of this thread.  It might have
4373     ;; been gathered into some loose thread, so we have to search
4374     ;; through the threads to find the thread we wanted.
4375     (let ((threads gnus-newsgroup-threads)
4376           sub)
4377       (while threads
4378         (setq sub (car threads))
4379         (if (stringp (car sub))
4380             ;; This is a gathered thread, so we look at the roots
4381             ;; below it to find whether this article is in this
4382             ;; gathered root.
4383             (progn
4384               (setq sub (cdr sub))
4385               (while sub
4386                 (when (member (caar sub) headers)
4387                   (setq thread (car threads)
4388                         threads nil
4389                         sub nil))
4390                 (setq sub (cdr sub))))
4391           ;; It's an ordinary thread, so we check it.
4392           (when (eq (car sub) (car headers))
4393             (setq thread sub
4394                   threads nil)))
4395         (setq threads (cdr threads)))
4396       ;; If this article is in no thread, then it's a root.
4397       (if thread
4398           (unless dont-remove
4399             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4400         (setq thread (gnus-id-to-thread last-id)))
4401       (when thread
4402         (prog1
4403             thread                      ; We return this thread.
4404           (unless dont-remove
4405             (if (stringp (car thread))
4406                 (progn
4407                   ;; If we use dummy roots, then we have to remove the
4408                   ;; dummy root as well.
4409                   (when (eq gnus-summary-make-false-root 'dummy)
4410                     ;; We go to the dummy root by going to
4411                     ;; the first sub-"thread", and then one line up.
4412                     (gnus-summary-goto-article
4413                      (mail-header-number (caadr thread)))
4414                     (forward-line -1)
4415                     (gnus-delete-line)
4416                     (gnus-data-compute-positions))
4417                   (setq thread (cdr thread))
4418                   (while thread
4419                     (gnus-remove-thread-1 (car thread))
4420                     (setq thread (cdr thread))))
4421               (gnus-remove-thread-1 thread))))))))
4422
4423 (defun gnus-remove-thread-1 (thread)
4424   "Remove the thread THREAD recursively."
4425   (let ((number (mail-header-number (pop thread)))
4426         d)
4427     (setq thread (reverse thread))
4428     (while thread
4429       (gnus-remove-thread-1 (pop thread)))
4430     (when (setq d (gnus-data-find number))
4431       (goto-char (gnus-data-pos d))
4432       (gnus-summary-show-thread)
4433       (gnus-data-remove
4434        number
4435        (- (point-at-bol)
4436           (prog1
4437               (1+ (point-at-eol))
4438             (gnus-delete-line)))))))
4439
4440 (defun gnus-sort-threads-1 (threads func)
4441   (sort (mapcar (lambda (thread)
4442                   (cons (car thread)
4443                         (and (cdr thread)
4444                              (gnus-sort-threads-1 (cdr thread) func))))
4445                 threads) func))
4446
4447 (defun gnus-sort-threads (threads)
4448   "Sort THREADS."
4449   (if (not gnus-thread-sort-functions)
4450       threads
4451     (gnus-message 8 "Sorting threads...")
4452     (let ((max-lisp-eval-depth 5000))
4453       (prog1 (gnus-sort-threads-1
4454          threads
4455          (gnus-make-sort-function gnus-thread-sort-functions))
4456         (gnus-message 8 "Sorting threads...done")))))
4457
4458 (defun gnus-sort-articles (articles)
4459   "Sort ARTICLES."
4460   (when gnus-article-sort-functions
4461     (gnus-message 7 "Sorting articles...")
4462     (prog1
4463         (setq gnus-newsgroup-headers
4464               (sort articles (gnus-make-sort-function
4465                               gnus-article-sort-functions)))
4466       (gnus-message 7 "Sorting articles...done"))))
4467
4468 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4469 (defmacro gnus-thread-header (thread)
4470   "Return header of first article in THREAD.
4471 Note that THREAD must never, ever be anything else than a variable -
4472 using some other form will lead to serious barfage."
4473   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4474   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4475   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4476         (vector thread) 2))
4477
4478 (defsubst gnus-article-sort-by-number (h1 h2)
4479   "Sort articles by article number."
4480   (< (mail-header-number h1)
4481      (mail-header-number h2)))
4482
4483 (defun gnus-thread-sort-by-number (h1 h2)
4484   "Sort threads by root article number."
4485   (gnus-article-sort-by-number
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defsubst gnus-article-sort-by-random (h1 h2)
4489   "Sort articles by article number."
4490   (zerop (random 2)))
4491
4492 (defun gnus-thread-sort-by-random (h1 h2)
4493   "Sort threads by root article number."
4494   (gnus-article-sort-by-random
4495    (gnus-thread-header h1) (gnus-thread-header h2)))
4496
4497 (defsubst gnus-article-sort-by-lines (h1 h2)
4498   "Sort articles by article Lines header."
4499   (< (mail-header-lines h1)
4500      (mail-header-lines h2)))
4501
4502 (defun gnus-thread-sort-by-lines (h1 h2)
4503   "Sort threads by root article Lines header."
4504   (gnus-article-sort-by-lines
4505    (gnus-thread-header h1) (gnus-thread-header h2)))
4506
4507 (defsubst gnus-article-sort-by-chars (h1 h2)
4508   "Sort articles by octet length."
4509   (< (mail-header-chars h1)
4510      (mail-header-chars h2)))
4511
4512 (defun gnus-thread-sort-by-chars (h1 h2)
4513   "Sort threads by root article octet length."
4514   (gnus-article-sort-by-chars
4515    (gnus-thread-header h1) (gnus-thread-header h2)))
4516
4517 (defsubst gnus-article-sort-by-author (h1 h2)
4518   "Sort articles by root author."
4519   (string-lessp
4520    (let ((extract (funcall
4521                    gnus-extract-address-components
4522                    (mail-header-from h1))))
4523      (or (car extract) (cadr extract) ""))
4524    (let ((extract (funcall
4525                    gnus-extract-address-components
4526                    (mail-header-from h2))))
4527      (or (car extract) (cadr extract) ""))))
4528
4529 (defun gnus-thread-sort-by-author (h1 h2)
4530   "Sort threads by root author."
4531   (gnus-article-sort-by-author
4532    (gnus-thread-header h1)  (gnus-thread-header h2)))
4533
4534 (defsubst gnus-article-sort-by-subject (h1 h2)
4535   "Sort articles by root subject."
4536   (string-lessp
4537    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4538    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4539
4540 (defun gnus-thread-sort-by-subject (h1 h2)
4541   "Sort threads by root subject."
4542   (gnus-article-sort-by-subject
4543    (gnus-thread-header h1) (gnus-thread-header h2)))
4544
4545 (defsubst gnus-article-sort-by-date (h1 h2)
4546   "Sort articles by root article date."
4547   (time-less-p
4548    (gnus-date-get-time (mail-header-date h1))
4549    (gnus-date-get-time (mail-header-date h2))))
4550
4551 (defun gnus-thread-sort-by-date (h1 h2)
4552   "Sort threads by root article date."
4553   (gnus-article-sort-by-date
4554    (gnus-thread-header h1) (gnus-thread-header h2)))
4555
4556 (defsubst gnus-article-sort-by-score (h1 h2)
4557   "Sort articles by root article score.
4558 Unscored articles will be counted as having a score of zero."
4559   (> (or (cdr (assq (mail-header-number h1)
4560                     gnus-newsgroup-scored))
4561          gnus-summary-default-score 0)
4562      (or (cdr (assq (mail-header-number h2)
4563                     gnus-newsgroup-scored))
4564          gnus-summary-default-score 0)))
4565
4566 (defun gnus-thread-sort-by-score (h1 h2)
4567   "Sort threads by root article score."
4568   (gnus-article-sort-by-score
4569    (gnus-thread-header h1) (gnus-thread-header h2)))
4570
4571 (defun gnus-thread-sort-by-total-score (h1 h2)
4572   "Sort threads by the sum of all scores in the thread.
4573 Unscored articles will be counted as having a score of zero."
4574   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4575
4576 (defun gnus-thread-total-score (thread)
4577   ;; This function find the total score of THREAD.
4578   (cond
4579    ((null thread)
4580     0)
4581    ((consp thread)
4582     (if (stringp (car thread))
4583         (apply gnus-thread-score-function 0
4584                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4585       (gnus-thread-total-score-1 thread)))
4586    (t
4587     (gnus-thread-total-score-1 (list thread)))))
4588
4589 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4590   "Sort threads such that the thread with the most recently arrived article comes first."
4591   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4592
4593 (defun gnus-thread-highest-number (thread)
4594   "Return the highest article number in THREAD."
4595   (apply 'max (mapcar (lambda (header)
4596                         (mail-header-number header))
4597                       (message-flatten-list thread))))
4598
4599 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4600   "Sort threads such that the thread with the most recently dated article comes first."
4601   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4602
4603 (defun gnus-thread-latest-date (thread)
4604   "Return the highest article date in THREAD."
4605   (let ((previous-time 0))
4606     (apply 'max
4607            (mapcar
4608             (lambda (header)
4609               (setq previous-time
4610                     (condition-case ()
4611                         (time-to-seconds (mail-header-parse-date
4612                                           (mail-header-date header)))
4613                       (error previous-time))))
4614             (sort
4615              (message-flatten-list thread)
4616              (lambda (h1 h2)
4617                (< (mail-header-number h1)
4618                   (mail-header-number h2))))))))
4619
4620 (defun gnus-thread-total-score-1 (root)
4621   ;; This function find the total score of the thread below ROOT.
4622   (setq root (car root))
4623   (apply gnus-thread-score-function
4624          (or (append
4625               (mapcar 'gnus-thread-total-score
4626                       (cdr (gnus-id-to-thread (mail-header-id root))))
4627               (when (> (mail-header-number root) 0)
4628                 (list (or (cdr (assq (mail-header-number root)
4629                                      gnus-newsgroup-scored))
4630                           gnus-summary-default-score 0))))
4631              (list gnus-summary-default-score)
4632              '(0))))
4633
4634 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4635 (defvar gnus-tmp-prev-subject nil)
4636 (defvar gnus-tmp-false-parent nil)
4637 (defvar gnus-tmp-root-expunged nil)
4638 (defvar gnus-tmp-dummy-line nil)
4639
4640 (eval-when-compile (defvar gnus-tmp-header))
4641 (defun gnus-extra-header (type &optional header)
4642   "Return the extra header of TYPE."
4643   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4644       ""))
4645
4646 (defvar gnus-tmp-thread-tree-header-string "")
4647
4648 (defcustom gnus-sum-thread-tree-root "> "
4649   "With %B spec, used for the root of a thread.
4650 If nil, use subject instead."
4651   :type '(radio (const :format "%v  " nil) (string :size 0))
4652   :group 'gnus-thread)
4653 (defcustom gnus-sum-thread-tree-false-root "> "
4654   "With %B spec, used for a false root of a thread.
4655 If nil, use subject instead."
4656   :type '(radio (const :format "%v  " nil) (string :size 0))
4657   :group 'gnus-thread)
4658 (defcustom gnus-sum-thread-tree-single-indent ""
4659   "With %B spec, used for a thread with just one message.
4660 If nil, use subject instead."
4661   :type '(radio (const :format "%v  " nil) (string :size 0))
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-vertical "| "
4664   "With %B spec, used for drawing a vertical line."
4665   :type 'string
4666   :group 'gnus-thread)
4667 (defcustom gnus-sum-thread-tree-indent "  "
4668   "With %B spec, used for indenting."
4669   :type 'string
4670   :group 'gnus-thread)
4671 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4672   "With %B spec, used for a leaf with brothers."
4673   :type 'string
4674   :group 'gnus-thread)
4675 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4676   "With %B spec, used for a leaf without brothers."
4677   :type 'string
4678   :group 'gnus-thread)
4679
4680 (defun gnus-summary-prepare-threads (threads)
4681   "Prepare summary buffer from THREADS and indentation LEVEL.
4682 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4683 or a straight list of headers."
4684   (gnus-message 7 "Generating summary...")
4685
4686   (setq gnus-newsgroup-threads threads)
4687   (beginning-of-line)
4688
4689   (let ((gnus-tmp-level 0)
4690         (default-score (or gnus-summary-default-score 0))
4691         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4692         (building-line-count gnus-summary-display-while-building)
4693         (building-count (integerp gnus-summary-display-while-building))
4694         thread number subject stack state gnus-tmp-gathered beg-match
4695         new-roots gnus-tmp-new-adopts thread-end simp-subject
4696         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4697         gnus-tmp-replied gnus-tmp-subject-or-nil
4698         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4699         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4700         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4701         tree-stack)
4702
4703     (setq gnus-tmp-prev-subject nil
4704           gnus-tmp-thread-tree-header-string "")
4705
4706     (if (vectorp (car threads))
4707         ;; If this is a straight (sic) list of headers, then a
4708         ;; threaded summary display isn't required, so we just create
4709         ;; an unthreaded one.
4710         (gnus-summary-prepare-unthreaded threads)
4711
4712       ;; Do the threaded display.
4713
4714       (if gnus-summary-display-while-building
4715           (switch-to-buffer (buffer-name)))
4716       (while (or threads stack gnus-tmp-new-adopts new-roots)
4717
4718         (if (and (= gnus-tmp-level 0)
4719                  (or (not stack)
4720                      (= (caar stack) 0))
4721                  (not gnus-tmp-false-parent)
4722                  (or gnus-tmp-new-adopts new-roots))
4723             (if gnus-tmp-new-adopts
4724                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4725                       thread (list (car gnus-tmp-new-adopts))
4726                       gnus-tmp-header (caar thread)
4727                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4728               (when new-roots
4729                 (setq thread (list (car new-roots))
4730                       gnus-tmp-header (caar thread)
4731                       new-roots (cdr new-roots))))
4732
4733           (if threads
4734               ;; If there are some threads, we do them before the
4735               ;; threads on the stack.
4736               (setq thread threads
4737                     gnus-tmp-header (caar thread))
4738             ;; There were no current threads, so we pop something off
4739             ;; the stack.
4740             (setq state (car stack)
4741                   gnus-tmp-level (car state)
4742                   tree-stack (cadr state)
4743                   thread (caddr state)
4744                   stack (cdr stack)
4745                   gnus-tmp-header (caar thread))))
4746
4747         (setq gnus-tmp-false-parent nil)
4748         (setq gnus-tmp-root-expunged nil)
4749         (setq thread-end nil)
4750
4751         (if (stringp gnus-tmp-header)
4752             ;; The header is a dummy root.
4753             (cond
4754              ((eq gnus-summary-make-false-root 'adopt)
4755               ;; We let the first article adopt the rest.
4756               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4757                                                (cddar thread)))
4758               (setq gnus-tmp-gathered
4759                     (nconc (mapcar
4760                             (lambda (h) (mail-header-number (car h)))
4761                             (cddar thread))
4762                            gnus-tmp-gathered))
4763               (setq thread (cons (list (caar thread)
4764                                        (cadar thread))
4765                                  (cdr thread)))
4766               (setq gnus-tmp-level -1
4767                     gnus-tmp-false-parent t))
4768              ((eq gnus-summary-make-false-root 'empty)
4769               ;; We print adopted articles with empty subject fields.
4770               (setq gnus-tmp-gathered
4771                     (nconc (mapcar
4772                             (lambda (h) (mail-header-number (car h)))
4773                             (cddar thread))
4774                            gnus-tmp-gathered))
4775               (setq gnus-tmp-level -1))
4776              ((eq gnus-summary-make-false-root 'dummy)
4777               ;; We remember that we probably want to output a dummy
4778               ;; root.
4779               (setq gnus-tmp-dummy-line gnus-tmp-header)
4780               (setq gnus-tmp-prev-subject gnus-tmp-header))
4781              (t
4782               ;; We do not make a root for the gathered
4783               ;; sub-threads at all.
4784               (setq gnus-tmp-level -1)))
4785
4786           (setq number (mail-header-number gnus-tmp-header)
4787                 subject (mail-header-subject gnus-tmp-header)
4788                 simp-subject (gnus-simplify-subject-fully subject))
4789
4790           (cond
4791            ;; If the thread has changed subject, we might want to make
4792            ;; this subthread into a root.
4793            ((and (null gnus-thread-ignore-subject)
4794                  (not (zerop gnus-tmp-level))
4795                  gnus-tmp-prev-subject
4796                  (not (string= gnus-tmp-prev-subject simp-subject)))
4797             (setq new-roots (nconc new-roots (list (car thread)))
4798                   thread-end t
4799                   gnus-tmp-header nil))
4800            ;; If the article lies outside the current limit,
4801            ;; then we do not display it.
4802            ((not (memq number gnus-newsgroup-limit))
4803             (setq gnus-tmp-gathered
4804                   (nconc (mapcar
4805                           (lambda (h) (mail-header-number (car h)))
4806                           (cdar thread))
4807                          gnus-tmp-gathered))
4808             (setq gnus-tmp-new-adopts (if (cdar thread)
4809                                           (append gnus-tmp-new-adopts
4810                                                   (cdar thread))
4811                                         gnus-tmp-new-adopts)
4812                   thread-end t
4813                   gnus-tmp-header nil)
4814             (when (zerop gnus-tmp-level)
4815               (setq gnus-tmp-root-expunged t)))
4816            ;; Perhaps this article is to be marked as read?
4817            ((and gnus-summary-mark-below
4818                  (< (or (cdr (assq number gnus-newsgroup-scored))
4819                         default-score)
4820                     gnus-summary-mark-below)
4821                  ;; Don't touch sparse articles.
4822                  (not (gnus-summary-article-sparse-p number))
4823                  (not (gnus-summary-article-ancient-p number)))
4824             (setq gnus-newsgroup-unreads
4825                   (delq number gnus-newsgroup-unreads))
4826             (if gnus-newsgroup-auto-expire
4827                 (setq gnus-newsgroup-expirable
4828                       (gnus-add-to-sorted-list
4829                        gnus-newsgroup-expirable number))
4830               (push (cons number gnus-low-score-mark)
4831                     gnus-newsgroup-reads))))
4832
4833           (when gnus-tmp-header
4834             ;; We may have an old dummy line to output before this
4835             ;; article.
4836             (when (and gnus-tmp-dummy-line
4837                        (gnus-subject-equal
4838                         gnus-tmp-dummy-line
4839                         (mail-header-subject gnus-tmp-header)))
4840               (gnus-summary-insert-dummy-line
4841                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4842               (setq gnus-tmp-dummy-line nil))
4843
4844             ;; Compute the mark.
4845             (setq gnus-tmp-unread (gnus-article-mark number))
4846
4847             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4848                                   gnus-tmp-header gnus-tmp-level)
4849                   gnus-newsgroup-data)
4850
4851             ;; Actually insert the line.
4852             (setq
4853              gnus-tmp-subject-or-nil
4854              (cond
4855               ((and gnus-thread-ignore-subject
4856                     gnus-tmp-prev-subject
4857                     (not (string= gnus-tmp-prev-subject simp-subject)))
4858                subject)
4859               ((zerop gnus-tmp-level)
4860                (if (and (eq gnus-summary-make-false-root 'empty)
4861                         (memq number gnus-tmp-gathered)
4862                         gnus-tmp-prev-subject
4863                         (string= gnus-tmp-prev-subject simp-subject))
4864                    gnus-summary-same-subject
4865                  subject))
4866               (t gnus-summary-same-subject)))
4867             (if (and (eq gnus-summary-make-false-root 'adopt)
4868                      (= gnus-tmp-level 1)
4869                      (memq number gnus-tmp-gathered))
4870                 (setq gnus-tmp-opening-bracket ?\<
4871                       gnus-tmp-closing-bracket ?\>)
4872               (setq gnus-tmp-opening-bracket ?\[
4873                     gnus-tmp-closing-bracket ?\]))
4874             (setq
4875              gnus-tmp-indentation
4876              (aref gnus-thread-indent-array gnus-tmp-level)
4877              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4878              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4879                                 gnus-summary-default-score 0)
4880              gnus-tmp-score-char
4881              (if (or (null gnus-summary-default-score)
4882                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4883                          gnus-summary-zcore-fuzz))
4884                  ?                      ;Whitespace
4885                (if (< gnus-tmp-score gnus-summary-default-score)
4886                    gnus-score-below-mark gnus-score-over-mark))
4887              gnus-tmp-replied
4888              (cond ((memq number gnus-newsgroup-processable)
4889                     gnus-process-mark)
4890                    ((memq number gnus-newsgroup-cached)
4891                     gnus-cached-mark)
4892                    ((memq number gnus-newsgroup-replied)
4893                     gnus-replied-mark)
4894                    ((memq number gnus-newsgroup-forwarded)
4895                     gnus-forwarded-mark)
4896                    ((memq number gnus-newsgroup-saved)
4897                     gnus-saved-mark)
4898                    ((memq number gnus-newsgroup-recent)
4899                     gnus-recent-mark)
4900                    ((memq number gnus-newsgroup-unseen)
4901                     gnus-unseen-mark)
4902                    (t gnus-no-mark))
4903              gnus-tmp-downloaded
4904              (cond ((memq number gnus-newsgroup-undownloaded)
4905                     gnus-undownloaded-mark)
4906                    (gnus-newsgroup-agentized
4907                     gnus-downloaded-mark)
4908                    (t
4909                     gnus-no-mark))
4910              gnus-tmp-from (mail-header-from gnus-tmp-header)
4911              gnus-tmp-name
4912              (cond
4913               ((string-match "<[^>]+> *$" gnus-tmp-from)
4914                (setq beg-match (match-beginning 0))
4915                (or (and (string-match "^\".+\"" gnus-tmp-from)
4916                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4917                    (substring gnus-tmp-from 0 beg-match)))
4918               ((string-match "(.+)" gnus-tmp-from)
4919                (substring gnus-tmp-from
4920                           (1+ (match-beginning 0)) (1- (match-end 0))))
4921               (t gnus-tmp-from))
4922
4923              ;; Do the %B string
4924              gnus-tmp-thread-tree-header-string
4925              (cond
4926               ((not gnus-show-threads) "")
4927               ((zerop gnus-tmp-level)
4928                (cond ((cdar thread)
4929                       (or gnus-sum-thread-tree-root subject))
4930                      (gnus-tmp-new-adopts
4931                       (or gnus-sum-thread-tree-false-root subject))
4932                      (t
4933                       (or gnus-sum-thread-tree-single-indent subject))))
4934               (t
4935                (concat (apply 'concat
4936                               (mapcar (lambda (item)
4937                                         (if (= item 1)
4938                                             gnus-sum-thread-tree-vertical
4939                                           gnus-sum-thread-tree-indent))
4940                                       (cdr (reverse tree-stack))))
4941                        (if (nth 1 thread)
4942                            gnus-sum-thread-tree-leaf-with-other
4943                          gnus-sum-thread-tree-single-leaf)))))
4944             (when (string= gnus-tmp-name "")
4945               (setq gnus-tmp-name gnus-tmp-from))
4946             (unless (numberp gnus-tmp-lines)
4947               (setq gnus-tmp-lines -1))
4948             (if (= gnus-tmp-lines -1)
4949                 (setq gnus-tmp-lines "?")
4950               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4951               (gnus-put-text-property
4952              (point)
4953              (progn (eval gnus-summary-line-format-spec) (point))
4954                'gnus-number number)
4955             (when gnus-visual-p
4956               (forward-line -1)
4957               (gnus-run-hooks 'gnus-summary-update-hook)
4958               (forward-line 1))
4959
4960             (setq gnus-tmp-prev-subject simp-subject)))
4961
4962         (when (nth 1 thread)
4963           (push (list (max 0 gnus-tmp-level)
4964                       (copy-sequence tree-stack)
4965                       (nthcdr 1 thread))
4966                 stack))
4967         (push (if (nth 1 thread) 1 0) tree-stack)
4968         (incf gnus-tmp-level)
4969         (setq threads (if thread-end nil (cdar thread)))
4970         (if gnus-summary-display-while-building
4971             (if building-count
4972                 (progn
4973                   ;; use a set frequency
4974                   (setq building-line-count (1- building-line-count))
4975                   (when (= building-line-count 0)
4976                     (sit-for 0)
4977                     (setq building-line-count
4978                           gnus-summary-display-while-building)))
4979               ;; always
4980               (sit-for 0)))
4981         (unless threads
4982           (setq gnus-tmp-level 0)))))
4983   (gnus-message 7 "Generating summary...done"))
4984
4985 (defun gnus-summary-prepare-unthreaded (headers)
4986   "Generate an unthreaded summary buffer based on HEADERS."
4987   (let (header number mark)
4988
4989     (beginning-of-line)
4990
4991     (while headers
4992       ;; We may have to root out some bad articles...
4993       (when (memq (setq number (mail-header-number
4994                                 (setq header (pop headers))))
4995                   gnus-newsgroup-limit)
4996         ;; Mark article as read when it has a low score.
4997         (when (and gnus-summary-mark-below
4998                    (< (or (cdr (assq number gnus-newsgroup-scored))
4999                           gnus-summary-default-score 0)
5000                       gnus-summary-mark-below)
5001                    (not (gnus-summary-article-ancient-p number)))
5002           (setq gnus-newsgroup-unreads
5003                 (delq number gnus-newsgroup-unreads))
5004           (if gnus-newsgroup-auto-expire
5005               (push number gnus-newsgroup-expirable)
5006             (push (cons number gnus-low-score-mark)
5007                   gnus-newsgroup-reads)))
5008
5009         (setq mark (gnus-article-mark number))
5010         (push (gnus-data-make number mark (1+ (point)) header 0)
5011               gnus-newsgroup-data)
5012         (gnus-summary-insert-line
5013          header 0 number
5014          (memq number gnus-newsgroup-undownloaded)
5015          mark (memq number gnus-newsgroup-replied)
5016          (memq number gnus-newsgroup-expirable)
5017          (mail-header-subject header) nil
5018          (cdr (assq number gnus-newsgroup-scored))
5019          (memq number gnus-newsgroup-processable))))))
5020
5021 (defun gnus-summary-remove-list-identifiers ()
5022   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5023   (let ((regexp (if (consp gnus-list-identifiers)
5024                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5025                   gnus-list-identifiers))
5026         changed subject)
5027     (when regexp
5028       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5029       (dolist (header gnus-newsgroup-headers)
5030         (setq subject (mail-header-subject header)
5031               changed nil)
5032         (while (string-match regexp subject)
5033           (setq subject
5034                 (concat (substring subject 0 (match-beginning 1))
5035                         (substring subject (match-end 0)))
5036                 changed t))
5037         (when changed
5038           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5039             (setq subject
5040                   (concat (substring subject 0 (match-beginning 1))
5041                           (substring subject (match-end 1)))))
5042           (mail-header-set-subject header subject))))))
5043
5044 (defun gnus-fetch-headers (articles)
5045   "Fetch headers of ARTICLES."
5046   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5047     (gnus-message 5 "Fetching headers for %s..." name)
5048     (prog1
5049         (if (eq 'nov
5050                 (setq gnus-headers-retrieved-by
5051                       (gnus-retrieve-headers
5052                        articles gnus-newsgroup-name
5053                        ;; We might want to fetch old headers, but
5054                        ;; not if there is only 1 article.
5055                        (and (or (and
5056                                  (not (eq gnus-fetch-old-headers 'some))
5057                                  (not (numberp gnus-fetch-old-headers)))
5058                                 (> (length articles) 1))
5059                             gnus-fetch-old-headers))))
5060             (gnus-get-newsgroup-headers-xover
5061              articles nil nil gnus-newsgroup-name t)
5062           (gnus-get-newsgroup-headers))
5063       (gnus-message 5 "Fetching headers for %s...done" name))))
5064
5065 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5066   "Select newsgroup GROUP.
5067 If READ-ALL is non-nil, all articles in the group are selected.
5068 If SELECT-ARTICLES, only select those articles from GROUP."
5069   (let* ((entry (gnus-group-entry group))
5070          ;;!!! Dirty hack; should be removed.
5071          (gnus-summary-ignore-duplicates
5072           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5073               t
5074             gnus-summary-ignore-duplicates))
5075          (info (nth 2 entry))
5076          articles fetched-articles cached)
5077
5078     (unless (gnus-check-server
5079              (set (make-local-variable 'gnus-current-select-method)
5080                   (gnus-find-method-for-group group)))
5081       (error "Couldn't open server"))
5082
5083     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5084         (gnus-activate-group group)     ; Or we can activate it...
5085         (progn                          ; Or we bug out.
5086           (when (equal major-mode 'gnus-summary-mode)
5087             (gnus-kill-buffer (current-buffer)))
5088           (error "Couldn't activate group %s: %s"
5089                  group (gnus-status-message group))))
5090
5091     (unless (gnus-request-group group t)
5092       (when (equal major-mode 'gnus-summary-mode)
5093         (gnus-kill-buffer (current-buffer)))
5094       (error "Couldn't request group %s: %s"
5095              group (gnus-status-message group)))
5096
5097     (when gnus-agent
5098       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5099       
5100       (setq gnus-summary-use-undownloaded-faces
5101             (gnus-agent-find-parameter
5102              group
5103              'agent-enable-undownloaded-faces)))
5104
5105     (setq gnus-newsgroup-name group
5106           gnus-newsgroup-unselected nil
5107           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5108
5109     (let ((display (gnus-group-find-parameter group 'display)))
5110       (setq gnus-newsgroup-display
5111             (cond
5112              ((not (zerop (or (car-safe read-all) 0)))
5113               ;; The user entered the group with C-u SPC/RET, let's show
5114               ;; all articles.
5115               'gnus-not-ignore)
5116              ((eq display 'all)
5117               'gnus-not-ignore)
5118              ((arrayp display)
5119               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5120              ((numberp display)
5121               ;; The following is probably the "correct" solution, but
5122               ;; it makes Gnus fetch all headers and then limit the
5123               ;; articles (which is slow), so instead we hack the
5124               ;; select-articles parameter instead. -- Simon Josefsson
5125               ;; <jas@kth.se>
5126               ;;
5127               ;; (gnus-byte-compile
5128               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5129               ;;                         display)))))
5130               (setq select-articles
5131                     (gnus-uncompress-range
5132                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5133                              (if (> tmp 0)
5134                                  tmp
5135                                1))
5136                            (cdr (gnus-active group)))))
5137               nil)
5138              (t
5139               nil))))
5140
5141     (gnus-summary-setup-default-charset)
5142
5143     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5144     (when (gnus-virtual-group-p group)
5145       (setq cached gnus-newsgroup-cached))
5146
5147     (setq gnus-newsgroup-unreads
5148           (gnus-sorted-ndifference
5149            (gnus-sorted-ndifference gnus-newsgroup-unreads
5150                                     gnus-newsgroup-marked)
5151            gnus-newsgroup-dormant))
5152
5153     (setq gnus-newsgroup-processable nil)
5154
5155     (gnus-update-read-articles group gnus-newsgroup-unreads)
5156
5157     ;; Adjust and set lists of article marks.
5158     (when info
5159       (gnus-adjust-marked-articles info))
5160     (if (setq articles select-articles)
5161         (setq gnus-newsgroup-unselected
5162               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5163       (setq articles (gnus-articles-to-read group read-all)))
5164
5165     (cond
5166      ((null articles)
5167       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5168       'quit)
5169      ((eq articles 0) nil)
5170      (t
5171       ;; Init the dependencies hash table.
5172       (setq gnus-newsgroup-dependencies
5173             (gnus-make-hashtable (length articles)))
5174       (gnus-set-global-variables)
5175       ;; Retrieve the headers and read them in.
5176
5177       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5178
5179       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5180       (when cached
5181         (setq gnus-newsgroup-cached cached))
5182
5183       ;; Suppress duplicates?
5184       (when gnus-suppress-duplicates
5185         (gnus-dup-suppress-articles))
5186
5187       ;; Set the initial limit.
5188       (setq gnus-newsgroup-limit (copy-sequence articles))
5189       ;; Remove canceled articles from the list of unread articles.
5190       (setq fetched-articles
5191             (mapcar (lambda (headers) (mail-header-number headers))
5192                     gnus-newsgroup-headers))
5193       (setq gnus-newsgroup-articles fetched-articles)
5194       (setq gnus-newsgroup-unreads
5195             (gnus-sorted-nintersection
5196              gnus-newsgroup-unreads fetched-articles))
5197       (gnus-compute-unseen-list)
5198
5199       ;; Removed marked articles that do not exist.
5200       (gnus-update-missing-marks
5201        (gnus-sorted-difference articles fetched-articles))
5202       ;; We might want to build some more threads first.
5203       (when (and gnus-fetch-old-headers
5204                  (eq gnus-headers-retrieved-by 'nov))
5205         (if (eq gnus-fetch-old-headers 'invisible)
5206             (gnus-build-all-threads)
5207           (gnus-build-old-threads)))
5208       ;; Let the Gnus agent mark articles as read.
5209       (when gnus-agent
5210         (gnus-agent-get-undownloaded-list))
5211       ;; Remove list identifiers from subject
5212       (when gnus-list-identifiers
5213         (gnus-summary-remove-list-identifiers))
5214       ;; Check whether auto-expire is to be done in this group.
5215       (setq gnus-newsgroup-auto-expire
5216             (gnus-group-auto-expirable-p group))
5217       ;; Set up the article buffer now, if necessary.
5218       (unless gnus-single-article-buffer
5219         (gnus-article-setup-buffer))
5220       ;; First and last article in this newsgroup.
5221       (when gnus-newsgroup-headers
5222         (setq gnus-newsgroup-begin
5223               (mail-header-number (car gnus-newsgroup-headers))
5224               gnus-newsgroup-end
5225               (mail-header-number
5226                (gnus-last-element gnus-newsgroup-headers))))
5227       ;; GROUP is successfully selected.
5228       (or gnus-newsgroup-headers t)))))
5229
5230 (defun gnus-compute-unseen-list ()
5231   ;; The `seen' marks are treated specially.
5232   (if (not gnus-newsgroup-seen)
5233       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5234     (setq gnus-newsgroup-unseen
5235           (gnus-inverse-list-range-intersection
5236            gnus-newsgroup-articles gnus-newsgroup-seen))))
5237
5238 (defun gnus-summary-display-make-predicate (display)
5239   (require 'gnus-agent)
5240   (when (= (length display) 1)
5241     (setq display (car display)))
5242   (unless gnus-summary-display-cache
5243     (dolist (elem (append '((unread . unread)
5244                             (read . read)
5245                             (unseen . unseen))
5246                           gnus-article-mark-lists))
5247       (push (cons (cdr elem)
5248                   (gnus-byte-compile
5249                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5250             gnus-summary-display-cache)))
5251   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5252         (gnus-category-predicate-cache gnus-summary-display-cache))
5253     (gnus-get-predicate display)))
5254
5255 ;; Uses the dynamically bound `number' variable.
5256 (eval-when-compile
5257   (defvar number))
5258 (defun gnus-article-marked-p (type &optional article)
5259   (let ((article (or article number)))
5260     (cond
5261      ((eq type 'tick)
5262       (memq article gnus-newsgroup-marked))
5263      ((eq type 'spam)
5264       (memq article gnus-newsgroup-spam-marked))
5265      ((eq type 'unsend)
5266       (memq article gnus-newsgroup-unsendable))
5267      ((eq type 'undownload)
5268       (memq article gnus-newsgroup-undownloaded))
5269      ((eq type 'download)
5270       (memq article gnus-newsgroup-downloadable))
5271      ((eq type 'unread)
5272       (memq article gnus-newsgroup-unreads))
5273      ((eq type 'read)
5274       (memq article gnus-newsgroup-reads))
5275      ((eq type 'dormant)
5276       (memq article gnus-newsgroup-dormant) )
5277      ((eq type 'expire)
5278       (memq article gnus-newsgroup-expirable))
5279      ((eq type 'reply)
5280       (memq article gnus-newsgroup-replied))
5281      ((eq type 'killed)
5282       (memq article gnus-newsgroup-killed))
5283      ((eq type 'bookmark)
5284       (assq article gnus-newsgroup-bookmarks))
5285      ((eq type 'score)
5286       (assq article gnus-newsgroup-scored))
5287      ((eq type 'save)
5288       (memq article gnus-newsgroup-saved))
5289      ((eq type 'cache)
5290       (memq article gnus-newsgroup-cached))
5291      ((eq type 'forward)
5292       (memq article gnus-newsgroup-forwarded))
5293      ((eq type 'seen)
5294       (not (memq article gnus-newsgroup-unseen)))
5295      ((eq type 'recent)
5296       (memq article gnus-newsgroup-recent))
5297      (t t))))
5298
5299 (defun gnus-articles-to-read (group &optional read-all)
5300   "Find out what articles the user wants to read."
5301   (let* ((display (gnus-group-find-parameter group 'display))
5302          (articles
5303           ;; Select all articles if `read-all' is non-nil, or if there
5304           ;; are no unread articles.
5305           (if (or read-all
5306                   (and (zerop (length gnus-newsgroup-marked))
5307                        (zerop (length gnus-newsgroup-unreads)))
5308                   ;; Fetch all if the predicate is non-nil.
5309                   gnus-newsgroup-display)
5310               ;; We want to select the headers for all the articles in
5311               ;; the group, so we select either all the active
5312               ;; articles in the group, or (if that's nil), the
5313               ;; articles in the cache.
5314               (or
5315                (gnus-uncompress-range (gnus-active group))
5316                (gnus-cache-articles-in-group group))
5317             ;; Select only the "normal" subset of articles.
5318             (gnus-sorted-nunion
5319              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5320              gnus-newsgroup-unreads)))
5321          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5322          (scored (length scored-list))
5323          (number (length articles))
5324          (marked (+ (length gnus-newsgroup-marked)
5325                     (length gnus-newsgroup-dormant)))
5326          (select
5327           (cond
5328            ((numberp read-all)
5329             read-all)
5330            ((numberp gnus-newsgroup-display)
5331             gnus-newsgroup-display)
5332            (t
5333             (condition-case ()
5334                 (cond
5335                  ((and (or (<= scored marked) (= scored number))
5336                        (numberp gnus-large-newsgroup)
5337                        (> number gnus-large-newsgroup))
5338                   (let* ((cursor-in-echo-area nil)
5339                          (initial (gnus-parameter-large-newsgroup-initial
5340                                    gnus-newsgroup-name))
5341                          (input
5342                           (read-string
5343                            (format
5344                             "How many articles from %s (%s %d): "
5345                             (gnus-limit-string
5346                              (gnus-group-decoded-name gnus-newsgroup-name)
5347                              35)
5348                             (if initial "max" "default")
5349                             number)
5350                            (if initial
5351                                (cons (number-to-string initial)
5352                                      0)))))
5353                     (if (string-match "^[ \t]*$" input) number input)))
5354                  ((and (> scored marked) (< scored number)
5355                        (> (- scored number) 20))
5356                   (let ((input
5357                          (read-string
5358                           (format "%s %s (%d scored, %d total): "
5359                                   "How many articles from"
5360                                   (gnus-group-decoded-name group)
5361                                   scored number))))
5362                     (if (string-match "^[ \t]*$" input)
5363                         number input)))
5364                  (t number))
5365               (quit
5366                (message "Quit getting the articles to read")
5367                nil))))))
5368     (setq select (if (stringp select) (string-to-number select) select))
5369     (if (or (null select) (zerop select))
5370         select
5371       (if (and (not (zerop scored)) (<= (abs select) scored))
5372           (progn
5373             (setq articles (sort scored-list '<))
5374             (setq number (length articles)))
5375         (setq articles (copy-sequence articles)))
5376
5377       (when (< (abs select) number)
5378         (if (< select 0)
5379             ;; Select the N oldest articles.
5380             (setcdr (nthcdr (1- (abs select)) articles) nil)
5381           ;; Select the N most recent articles.
5382           (setq articles (nthcdr (- number select) articles))))
5383       (setq gnus-newsgroup-unselected
5384             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5385       (when gnus-alter-articles-to-read-function
5386         (setq articles
5387               (sort
5388                (funcall gnus-alter-articles-to-read-function
5389                         gnus-newsgroup-name articles)
5390                '<)))
5391       articles)))
5392
5393 (defun gnus-killed-articles (killed articles)
5394   (let (out)
5395     (while articles
5396       (when (inline (gnus-member-of-range (car articles) killed))
5397         (push (car articles) out))
5398       (setq articles (cdr articles)))
5399     out))
5400
5401 (defun gnus-uncompress-marks (marks)
5402   "Uncompress the mark ranges in MARKS."
5403   (let ((uncompressed '(score bookmark))
5404         out)
5405     (while marks
5406       (if (memq (caar marks) uncompressed)
5407           (push (car marks) out)
5408         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5409       (setq marks (cdr marks)))
5410     out))
5411
5412 (defun gnus-article-mark-to-type (mark)
5413   "Return the type of MARK."
5414   (or (cadr (assq mark gnus-article-special-mark-lists))
5415       'list))
5416
5417 (defun gnus-article-unpropagatable-p (mark)
5418   "Return whether MARK should be propagated to back end."
5419   (memq mark gnus-article-unpropagated-mark-lists))
5420
5421 (defun gnus-adjust-marked-articles (info)
5422   "Set all article lists and remove all marks that are no longer valid."
5423   (let* ((marked-lists (gnus-info-marks info))
5424          (active (gnus-active (gnus-info-group info)))
5425          (min (car active))
5426          (max (cdr active))
5427          (types gnus-article-mark-lists)
5428          marks var articles article mark mark-type
5429          bgn end)
5430
5431     (dolist (marks marked-lists)
5432       (setq mark (car marks)
5433             mark-type (gnus-article-mark-to-type mark)
5434             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5435
5436       ;; We set the variable according to the type of the marks list,
5437       ;; and then adjust the marks to a subset of the active articles.
5438       (cond
5439        ;; Adjust "simple" lists - compressed yet unsorted
5440        ((eq mark-type 'list)
5441         ;; Simultaneously uncompress and clip to active range
5442         ;; See gnus-uncompress-range for a description of possible marks
5443         (let (l lh)
5444           (if (not (cadr marks))
5445               (set var nil)
5446             (setq articles (if (numberp (cddr marks))
5447                                (list (cdr marks))
5448                              (cdr marks))
5449                   lh (cons nil nil)
5450                   l lh)
5451
5452             (while (setq article (pop articles))
5453               (cond ((consp article)
5454                      (setq bgn (max (car article) min)
5455                            end (min (cdr article) max))
5456                      (while (<= bgn end)
5457                        (setq l (setcdr l (cons bgn nil))
5458                              bgn (1+ bgn))))
5459                     ((and (<= min article)
5460                           (>= max article))
5461                      (setq l (setcdr l (cons article nil))))))
5462             (set var (cdr lh)))))
5463        ;; Adjust assocs.
5464        ((eq mark-type 'tuple)
5465         (set var (setq articles (cdr marks)))
5466         (when (not (listp (cdr (symbol-value var))))
5467           (set var (list (symbol-value var))))
5468         (when (not (listp (cdr articles)))
5469           (setq articles (list articles)))
5470         (while articles
5471           (when (or (not (consp (setq article (pop articles))))
5472                     (< (car article) min)
5473                     (> (car article) max))
5474             (set var (delq article (symbol-value var))))))
5475        ;; Adjust ranges (sloppily).
5476        ((eq mark-type 'range)
5477         (cond
5478          ((eq mark 'seen)
5479           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5480           ;; It should be (seen (NUM1 . NUM2)).
5481           (when (numberp (cddr marks))
5482             (setcdr marks (list (cdr marks))))
5483           (setq articles (cdr marks))
5484           (while (and articles
5485                       (or (and (consp (car articles))
5486                                (> min (cdar articles)))
5487                           (and (numberp (car articles))
5488                                (> min (car articles)))))
5489             (pop articles))
5490           (set var articles))))))))
5491
5492 (defun gnus-update-missing-marks (missing)
5493   "Go through the list of MISSING articles and remove them from the mark lists."
5494   (when missing
5495     (let (var m)
5496       ;; Go through all types.
5497       (dolist (elem gnus-article-mark-lists)
5498         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5499           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5500           (when (symbol-value var)
5501             ;; This list has articles.  So we delete all missing
5502             ;; articles from it.
5503             (setq m missing)
5504             (while m
5505               (set var (delq (pop m) (symbol-value var))))))))))
5506
5507 (defun gnus-update-marks ()
5508   "Enter the various lists of marked articles into the newsgroup info list."
5509   (let ((types gnus-article-mark-lists)
5510         (info (gnus-get-info gnus-newsgroup-name))
5511         type list newmarked symbol delta-marks)
5512     (when info
5513       ;; Add all marks lists to the list of marks lists.
5514       (while (setq type (pop types))
5515         (setq list (symbol-value
5516                     (setq symbol
5517                           (intern (format "gnus-newsgroup-%s" (car type))))))
5518
5519         (when list
5520           ;; Get rid of the entries of the articles that have the
5521           ;; default score.
5522           (when (and (eq (cdr type) 'score)
5523                      gnus-save-score
5524                      list)
5525             (let* ((arts list)
5526                    (prev (cons nil list))
5527                    (all prev))
5528               (while arts
5529                 (if (or (not (consp (car arts)))
5530                         (= (cdar arts) gnus-summary-default-score))
5531                     (setcdr prev (cdr arts))
5532                   (setq prev arts))
5533                 (setq arts (cdr arts)))
5534               (setq list (cdr all)))))
5535
5536         (when (eq (cdr type) 'seen)
5537           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5538
5539         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5540           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5541
5542         (when (and (gnus-check-backend-function
5543                     'request-set-mark gnus-newsgroup-name)
5544                    (not (gnus-article-unpropagatable-p (cdr type))))
5545           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5546                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5547                  (add (gnus-remove-from-range
5548                        (gnus-copy-sequence list) old)))
5549             (when add
5550               (push (list add 'add (list (cdr type))) delta-marks))
5551             (when del
5552               (push (list del 'del (list (cdr type))) delta-marks))))
5553
5554         (when list
5555           (push (cons (cdr type) list) newmarked)))
5556
5557       (when delta-marks
5558         (unless (gnus-check-group gnus-newsgroup-name)
5559           (error "Can't open server for %s" gnus-newsgroup-name))
5560         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5561
5562       ;; Enter these new marks into the info of the group.
5563       (if (nthcdr 3 info)
5564           (setcar (nthcdr 3 info) newmarked)
5565         ;; Add the marks lists to the end of the info.
5566         (when newmarked
5567           (setcdr (nthcdr 2 info) (list newmarked))))
5568
5569       ;; Cut off the end of the info if there's nothing else there.
5570       (let ((i 5))
5571         (while (and (> i 2)
5572                     (not (nth i info)))
5573           (when (nthcdr (decf i) info)
5574             (setcdr (nthcdr i info) nil)))))))
5575
5576 (defun gnus-set-mode-line (where)
5577   "Set the mode line of the article or summary buffers.
5578 If WHERE is `summary', the summary mode line format will be used."
5579   ;; Is this mode line one we keep updated?
5580   (when (and (memq where gnus-updated-mode-lines)
5581              (symbol-value
5582               (intern (format "gnus-%s-mode-line-format-spec" where))))
5583     (let (mode-string)
5584       (save-excursion
5585         ;; We evaluate this in the summary buffer since these
5586         ;; variables are buffer-local to that buffer.
5587         (set-buffer gnus-summary-buffer)
5588        ;; We bind all these variables that are used in the `eval' form
5589         ;; below.
5590         (let* ((mformat (symbol-value
5591                          (intern
5592                           (format "gnus-%s-mode-line-format-spec" where))))
5593                (gnus-tmp-group-name (gnus-group-decoded-name
5594                                      gnus-newsgroup-name))
5595                (gnus-tmp-article-number (or gnus-current-article 0))
5596                (gnus-tmp-unread gnus-newsgroup-unreads)
5597                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5598                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5599                (gnus-tmp-unread-and-unselected
5600                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5601                             (zerop gnus-tmp-unselected))
5602                        "")
5603                       ((zerop gnus-tmp-unselected)
5604                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5605                       (t (format "{%d(+%d) more}"
5606                                  gnus-tmp-unread-and-unticked
5607                                  gnus-tmp-unselected))))
5608                (gnus-tmp-subject
5609                 (if (and gnus-current-headers
5610                          (vectorp gnus-current-headers))
5611                     (gnus-mode-string-quote
5612                      (mail-header-subject gnus-current-headers))
5613                   ""))
5614                bufname-length max-len
5615                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5616           (setq mode-string (eval mformat))
5617           (setq bufname-length (if (string-match "%b" mode-string)
5618                                    (- (length
5619                                        (buffer-name
5620                                         (if (eq where 'summary)
5621                                             nil
5622                                           (get-buffer gnus-article-buffer))))
5623                                       2)
5624                                  0))
5625           (setq max-len (max 4 (if gnus-mode-non-string-length
5626                                    (- (window-width)
5627                                       gnus-mode-non-string-length
5628                                       bufname-length)
5629                                  (length mode-string))))
5630           ;; We might have to chop a bit of the string off...
5631           (when (> (length mode-string) max-len)
5632             (setq mode-string
5633                   (concat (truncate-string-to-width mode-string (- max-len 3))
5634                           "...")))
5635           ;; Pad the mode string a bit.
5636           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5637       ;; Update the mode line.
5638       (setq mode-line-buffer-identification
5639             (gnus-mode-line-buffer-identification (list mode-string)))
5640       (set-buffer-modified-p t))))
5641
5642 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5643   "Go through the HEADERS list and add all Xrefs to a hash table.
5644 The resulting hash table is returned, or nil if no Xrefs were found."
5645   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5646          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5647          (xref-hashtb (gnus-make-hashtable))
5648          start group entry number xrefs header)
5649     (while headers
5650       (setq header (pop headers))
5651       (when (and (setq xrefs (mail-header-xref header))
5652                  (not (memq (setq number (mail-header-number header))
5653                             unreads)))
5654         (setq start 0)
5655         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5656           (setq start (match-end 0))
5657           (setq group (if prefix
5658                           (concat prefix (substring xrefs (match-beginning 1)
5659                                                     (match-end 1)))
5660                         (substring xrefs (match-beginning 1) (match-end 1))))
5661           (setq number
5662                 (string-to-int (substring xrefs (match-beginning 2)
5663                                           (match-end 2))))
5664           (if (setq entry (gnus-gethash group xref-hashtb))
5665               (setcdr entry (cons number (cdr entry)))
5666             (gnus-sethash group (cons number nil) xref-hashtb)))))
5667     (and start xref-hashtb)))
5668
5669 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5670   "Look through all the headers and mark the Xrefs as read."
5671   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5672         name info xref-hashtb idlist method nth4)
5673     (save-excursion
5674       (set-buffer gnus-group-buffer)
5675       (when (setq xref-hashtb
5676                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5677         (mapatoms
5678          (lambda (group)
5679            (unless (string= from-newsgroup (setq name (symbol-name group)))
5680              (setq idlist (symbol-value group))
5681              ;; Dead groups are not updated.
5682              (and (prog1
5683                       (setq info (gnus-get-info name))
5684                     (when (stringp (setq nth4 (gnus-info-method info)))
5685                       (setq nth4 (gnus-server-to-method nth4))))
5686                   ;; Only do the xrefs if the group has the same
5687                   ;; select method as the group we have just read.
5688                   (or (gnus-methods-equal-p
5689                        nth4 (gnus-find-method-for-group from-newsgroup))
5690                       virtual
5691                       (equal nth4 (setq method (gnus-find-method-for-group
5692                                                 from-newsgroup)))
5693                       (and (equal (car nth4) (car method))
5694                            (equal (nth 1 nth4) (nth 1 method))))
5695                   gnus-use-cross-reference
5696                   (or (not (eq gnus-use-cross-reference t))
5697                       virtual
5698                       ;; Only do cross-references on subscribed
5699                       ;; groups, if that is what is wanted.
5700                       (<= (gnus-info-level info) gnus-level-subscribed))
5701                   (gnus-group-make-articles-read name idlist))))
5702          xref-hashtb)))))
5703
5704 (defun gnus-compute-read-articles (group articles)
5705   (let* ((entry (gnus-group-entry group))
5706          (info (nth 2 entry))
5707          (active (gnus-active group))
5708          ninfo)
5709     (when entry
5710       ;; First peel off all invalid article numbers.
5711       (when active
5712         (let ((ids articles)
5713               id first)
5714           (while (setq id (pop ids))
5715             (when (and first (> id (cdr active)))
5716               ;; We'll end up in this situation in one particular
5717               ;; obscure situation.  If you re-scan a group and get
5718               ;; a new article that is cross-posted to a different
5719               ;; group that has not been re-scanned, you might get
5720               ;; crossposted article that has a higher number than
5721               ;; Gnus believes possible.  So we re-activate this
5722               ;; group as well.  This might mean doing the
5723               ;; crossposting thingy will *increase* the number
5724               ;; of articles in some groups.  Tsk, tsk.
5725               (setq active (or (gnus-activate-group group) active)))
5726             (when (or (> id (cdr active))
5727                       (< id (car active)))
5728               (setq articles (delq id articles))))))
5729       ;; If the read list is nil, we init it.
5730       (if (and active
5731                (null (gnus-info-read info))
5732                (> (car active) 1))
5733           (setq ninfo (cons 1 (1- (car active))))
5734         (setq ninfo (gnus-info-read info)))
5735       ;; Then we add the read articles to the range.
5736       (gnus-add-to-range
5737        ninfo (setq articles (sort articles '<))))))
5738
5739 (defun gnus-group-make-articles-read (group articles)
5740   "Update the info of GROUP to say that ARTICLES are read."
5741   (let* ((num 0)
5742          (entry (gnus-group-entry group))
5743          (info (nth 2 entry))
5744          (active (gnus-active group))
5745          range)
5746     (when entry
5747       (setq range (gnus-compute-read-articles group articles))
5748       (with-current-buffer gnus-group-buffer
5749         (gnus-undo-register
5750           `(progn
5751              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5752              (gnus-info-set-read ',info ',(gnus-info-read info))
5753              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5754              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5755              (gnus-group-update-group ,group t))))
5756       ;; Add the read articles to the range.
5757       (gnus-info-set-read info range)
5758       (gnus-request-set-mark group (list (list range 'add '(read))))
5759       ;; Then we have to re-compute how many unread
5760       ;; articles there are in this group.
5761       (when active
5762         (cond
5763          ((not range)
5764           (setq num (- (1+ (cdr active)) (car active))))
5765          ((not (listp (cdr range)))
5766           (setq num (- (cdr active) (- (1+ (cdr range))
5767                                        (car range)))))
5768          (t
5769           (while range
5770             (if (numberp (car range))
5771                 (setq num (1+ num))
5772               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5773             (setq range (cdr range)))
5774           (setq num (- (cdr active) num))))
5775         ;; Update the number of unread articles.
5776         (setcar entry num)
5777         ;; Update the group buffer.
5778         (unless (gnus-ephemeral-group-p group)
5779           (gnus-group-update-group group t))))))
5780
5781 (defvar gnus-newsgroup-none-id 0)
5782
5783 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5784   (let ((cur nntp-server-buffer)
5785         (dependencies
5786          (or dependencies
5787              (save-excursion (set-buffer gnus-summary-buffer)
5788                              gnus-newsgroup-dependencies)))
5789         headers id end ref
5790         (mail-parse-charset gnus-newsgroup-charset)
5791         (mail-parse-ignored-charsets
5792          (save-excursion (condition-case nil
5793                              (set-buffer gnus-summary-buffer)
5794                            (error))
5795                          gnus-newsgroup-ignored-charsets)))
5796     (save-excursion
5797       (set-buffer nntp-server-buffer)
5798       ;; Translate all TAB characters into SPACE characters.
5799       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5800       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5801       (ietf-drums-unfold-fws)
5802       (gnus-run-hooks 'gnus-parse-headers-hook)
5803       (let ((case-fold-search t)
5804             in-reply-to header p lines chars)
5805         (goto-char (point-min))
5806         ;; Search to the beginning of the next header.  Error messages
5807         ;; do not begin with 2 or 3.
5808         (while (re-search-forward "^[23][0-9]+ " nil t)
5809           (setq id nil
5810                 ref nil)
5811           ;; This implementation of this function, with nine
5812           ;; search-forwards instead of the one re-search-forward and
5813           ;; a case (which basically was the old function) is actually
5814           ;; about twice as fast, even though it looks messier.  You
5815           ;; can't have everything, I guess.  Speed and elegance
5816           ;; doesn't always go hand in hand.
5817           (setq
5818            header
5819            (vector
5820             ;; Number.
5821             (prog1
5822                 (read cur)
5823               (end-of-line)
5824               (setq p (point))
5825               (narrow-to-region (point)
5826                                 (or (and (search-forward "\n.\n" nil t)
5827                                          (- (point) 2))
5828                                     (point))))
5829             ;; Subject.
5830             (progn
5831               (goto-char p)
5832               (if (search-forward "\nsubject:" nil t)
5833                   (funcall gnus-decode-encoded-word-function
5834                            (nnheader-header-value))
5835                 "(none)"))
5836             ;; From.
5837             (progn
5838               (goto-char p)
5839               (if (search-forward "\nfrom:" nil t)
5840                   (funcall gnus-decode-encoded-word-function
5841                            (nnheader-header-value))
5842                 "(nobody)"))
5843             ;; Date.
5844             (progn
5845               (goto-char p)
5846               (if (search-forward "\ndate:" nil t)
5847                   (nnheader-header-value) ""))
5848             ;; Message-ID.
5849             (progn
5850               (goto-char p)
5851               (setq id (if (re-search-forward
5852                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5853                            ;; We do it this way to make sure the Message-ID
5854                            ;; is (somewhat) syntactically valid.
5855                            (buffer-substring (match-beginning 1)
5856                                              (match-end 1))
5857                          ;; If there was no message-id, we just fake one
5858                          ;; to make subsequent routines simpler.
5859                          (nnheader-generate-fake-message-id))))
5860             ;; References.
5861             (progn
5862               (goto-char p)
5863               (if (search-forward "\nreferences:" nil t)
5864                   (progn
5865                     (setq end (point))
5866                     (prog1
5867                         (nnheader-header-value)
5868                       (setq ref
5869                             (buffer-substring
5870                              (progn
5871                                (end-of-line)
5872                                (search-backward ">" end t)
5873                                (1+ (point)))
5874                              (progn
5875                                (search-backward "<" end t)
5876                                (point))))))
5877                 ;; Get the references from the in-reply-to header if there
5878                 ;; were no references and the in-reply-to header looks
5879                 ;; promising.
5880                 (if (and (search-forward "\nin-reply-to:" nil t)
5881                          (setq in-reply-to (nnheader-header-value))
5882                          (string-match "<[^>]+>" in-reply-to))
5883                     (let (ref2)
5884                       (setq ref (substring in-reply-to (match-beginning 0)
5885                                            (match-end 0)))
5886                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5887                         (setq ref2 (substring in-reply-to (match-beginning 0)
5888                                               (match-end 0)))
5889                         (when (> (length ref2) (length ref))
5890                           (setq ref ref2)))
5891                       ref)
5892                   (setq ref nil))))
5893             ;; Chars.
5894             (progn
5895               (goto-char p)
5896               (if (search-forward "\nchars: " nil t)
5897                   (if (numberp (setq chars (ignore-errors (read cur))))
5898                       chars -1)
5899                 -1))
5900             ;; Lines.
5901             (progn
5902               (goto-char p)
5903               (if (search-forward "\nlines: " nil t)
5904                   (if (numberp (setq lines (ignore-errors (read cur))))
5905                       lines -1)
5906                 -1))
5907             ;; Xref.
5908             (progn
5909               (goto-char p)
5910               (and (search-forward "\nxref:" nil t)
5911                    (nnheader-header-value)))
5912             ;; Extra.
5913             (when gnus-extra-headers
5914               (let ((extra gnus-extra-headers)
5915                     out)
5916                 (while extra
5917                   (goto-char p)
5918                   (when (search-forward
5919                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5920                     (push (cons (car extra) (nnheader-header-value))
5921                           out))
5922                   (pop extra))
5923                 out))))
5924           (when (equal id ref)
5925             (setq ref nil))
5926
5927           (when gnus-alter-header-function
5928             (funcall gnus-alter-header-function header)
5929             (setq id (mail-header-id header)
5930                   ref (gnus-parent-id (mail-header-references header))))
5931
5932           (when (setq header
5933                       (gnus-dependencies-add-header
5934                        header dependencies force-new))
5935             (push header headers))
5936           (goto-char (point-max))
5937           (widen))
5938         (nreverse headers)))))
5939
5940 ;; Goes through the xover lines and returns a list of vectors
5941 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5942                                                   force-new dependencies
5943                                                   group also-fetch-heads)
5944   "Parse the news overview data in the server buffer.
5945 Return a list of headers that match SEQUENCE (see
5946 `nntp-retrieve-headers')."
5947   ;; Get the Xref when the users reads the articles since most/some
5948   ;; NNTP servers do not include Xrefs when using XOVER.
5949   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5950   (let ((mail-parse-charset gnus-newsgroup-charset)
5951         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5952         (cur nntp-server-buffer)
5953         (dependencies (or dependencies gnus-newsgroup-dependencies))
5954         (allp (cond
5955                ((eq gnus-read-all-available-headers t)
5956                 t)
5957                ((stringp gnus-read-all-available-headers)
5958                 (string-match gnus-read-all-available-headers group))
5959                (t
5960                 nil)))
5961         number headers header)
5962     (save-excursion
5963       (set-buffer nntp-server-buffer)
5964       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5965       ;; Allow the user to mangle the headers before parsing them.
5966       (gnus-run-hooks 'gnus-parse-headers-hook)
5967       (goto-char (point-min))
5968       (gnus-parse-without-error
5969         (while (and (or sequence allp)
5970                     (not (eobp)))
5971           (setq number (read cur))
5972           (when (not allp)
5973             (while (and sequence
5974                         (< (car sequence) number))
5975               (setq sequence (cdr sequence))))
5976           (when (and (or allp
5977                          (and sequence
5978                               (eq number (car sequence))))
5979                      (progn
5980                        (setq sequence (cdr sequence))
5981                        (setq header (inline
5982                                       (gnus-nov-parse-line
5983                                        number dependencies force-new)))))
5984             (push header headers))
5985           (forward-line 1)))
5986       ;; A common bug in inn is that if you have posted an article and
5987       ;; then retrieves the active file, it will answer correctly --
5988       ;; the new article is included.  However, a NOV entry for the
5989       ;; article may not have been generated yet, so this may fail.
5990       ;; We work around this problem by retrieving the last few
5991       ;; headers using HEAD.
5992       (if (or (not also-fetch-heads)
5993               (not sequence))
5994           ;; We (probably) got all the headers.
5995           (nreverse headers)
5996         (let ((gnus-nov-is-evil t))
5997           (nconc
5998            (nreverse headers)
5999            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6000              (gnus-get-newsgroup-headers))))))))
6001
6002 (defun gnus-article-get-xrefs ()
6003   "Fill in the Xref value in `gnus-current-headers', if necessary.
6004 This is meant to be called in `gnus-article-internal-prepare-hook'."
6005   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6006                                  gnus-current-headers)))
6007     (or (not gnus-use-cross-reference)
6008         (not headers)
6009         (and (mail-header-xref headers)
6010              (not (string= (mail-header-xref headers) "")))
6011         (let ((case-fold-search t)
6012               xref)
6013           (save-restriction
6014             (nnheader-narrow-to-headers)
6015             (goto-char (point-min))
6016             (when (or (and (not (eobp))
6017                            (eq (downcase (char-after)) ?x)
6018                            (looking-at "Xref:"))
6019                       (search-forward "\nXref:" nil t))
6020               (goto-char (1+ (match-end 0)))
6021               (setq xref (buffer-substring (point) (point-at-eol)))
6022               (mail-header-set-xref headers xref)))))))
6023
6024 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6025   "Find article ID and insert the summary line for that article.
6026 OLD-HEADER can either be a header or a line number to insert
6027 the subject line on."
6028   (let* ((line (and (numberp old-header) old-header))
6029          (old-header (and (vectorp old-header) old-header))
6030          (header (cond ((and old-header use-old-header)
6031                         old-header)
6032                        ((and (numberp id)
6033                              (gnus-number-to-header id))
6034                         (gnus-number-to-header id))
6035                        (t
6036                         (gnus-read-header id))))
6037          (number (and (numberp id) id))
6038          d)
6039     (when header
6040       ;; Rebuild the thread that this article is part of and go to the
6041       ;; article we have fetched.
6042       (when (and (not gnus-show-threads)
6043                  old-header)
6044         (when (and number
6045                    (setq d (gnus-data-find (mail-header-number old-header))))
6046           (goto-char (gnus-data-pos d))
6047           (gnus-data-remove
6048            number
6049            (- (point-at-bol)
6050               (prog1
6051                   (1+ (point-at-eol))
6052                 (gnus-delete-line))))))
6053       (when old-header
6054         (mail-header-set-number header (mail-header-number old-header)))
6055       (setq gnus-newsgroup-sparse
6056             (delq (setq number (mail-header-number header))
6057                   gnus-newsgroup-sparse))
6058       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6059       (push number gnus-newsgroup-limit)
6060       (gnus-rebuild-thread (mail-header-id header) line)
6061       (gnus-summary-goto-subject number nil t))
6062     (when (and (numberp number)
6063                (> number 0))
6064       ;; We have to update the boundaries even if we can't fetch the
6065       ;; article if ID is a number -- so that the next `P' or `N'
6066       ;; command will fetch the previous (or next) article even
6067       ;; if the one we tried to fetch this time has been canceled.
6068       (when (> number gnus-newsgroup-end)
6069         (setq gnus-newsgroup-end number))
6070       (when (< number gnus-newsgroup-begin)
6071         (setq gnus-newsgroup-begin number))
6072       (setq gnus-newsgroup-unselected
6073             (delq number gnus-newsgroup-unselected)))
6074     ;; Report back a success?
6075     (and header (mail-header-number header))))
6076
6077 ;;; Process/prefix in the summary buffer
6078
6079 (defun gnus-summary-work-articles (n)
6080   "Return a list of articles to be worked upon.
6081 The prefix argument, the list of process marked articles, and the
6082 current article will be taken into consideration."
6083   (save-excursion
6084     (set-buffer gnus-summary-buffer)
6085     (cond
6086      (n
6087       ;; A numerical prefix has been given.
6088       (setq n (prefix-numeric-value n))
6089       (let ((backward (< n 0))
6090             (n (abs (prefix-numeric-value n)))
6091             articles article)
6092         (save-excursion
6093           (while
6094               (and (> n 0)
6095                    (push (setq article (gnus-summary-article-number))
6096                          articles)
6097                    (if backward
6098                        (gnus-summary-find-prev nil article)
6099                      (gnus-summary-find-next nil article)))
6100             (decf n)))
6101         (nreverse articles)))
6102      ((and (gnus-region-active-p) (mark))
6103       (message "region active")
6104       ;; Work on the region between point and mark.
6105       (let ((max (max (point) (mark)))
6106             articles article)
6107         (save-excursion
6108           (goto-char (min (min (point) (mark))))
6109           (while
6110               (and
6111                (push (setq article (gnus-summary-article-number)) articles)
6112                (gnus-summary-find-next nil article)
6113                (< (point) max)))
6114           (nreverse articles))))
6115      (gnus-newsgroup-processable
6116       ;; There are process-marked articles present.
6117       ;; Save current state.
6118       (gnus-summary-save-process-mark)
6119       ;; Return the list.
6120       (reverse gnus-newsgroup-processable))
6121      (t
6122       ;; Just return the current article.
6123       (list (gnus-summary-article-number))))))
6124
6125 (defmacro gnus-summary-iterate (arg &rest forms)
6126   "Iterate over the process/prefixed articles and do FORMS.
6127 ARG is the interactive prefix given to the command.  FORMS will be
6128 executed with point over the summary line of the articles."
6129   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6130     `(let ((,articles (gnus-summary-work-articles ,arg)))
6131        (while ,articles
6132          (gnus-summary-goto-subject (car ,articles))
6133          ,@forms
6134          (pop ,articles)))))
6135
6136 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6137 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6138
6139 (defun gnus-summary-save-process-mark ()
6140   "Push the current set of process marked articles on the stack."
6141   (interactive)
6142   (push (copy-sequence gnus-newsgroup-processable)
6143         gnus-newsgroup-process-stack))
6144
6145 (defun gnus-summary-kill-process-mark ()
6146   "Push the current set of process marked articles on the stack and unmark."
6147   (interactive)
6148   (gnus-summary-save-process-mark)
6149   (gnus-summary-unmark-all-processable))
6150
6151 (defun gnus-summary-yank-process-mark ()
6152   "Pop the last process mark state off the stack and restore it."
6153   (interactive)
6154   (unless gnus-newsgroup-process-stack
6155     (error "Empty mark stack"))
6156   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6157
6158 (defun gnus-summary-process-mark-set (set)
6159   "Make SET into the current process marked articles."
6160   (gnus-summary-unmark-all-processable)
6161   (while set
6162     (gnus-summary-set-process-mark (pop set))))
6163
6164 ;;; Searching and stuff
6165
6166 (defun gnus-summary-search-group (&optional backward use-level)
6167   "Search for next unread newsgroup.
6168 If optional argument BACKWARD is non-nil, search backward instead."
6169   (save-excursion
6170     (set-buffer gnus-group-buffer)
6171     (when (gnus-group-search-forward
6172            backward nil (if use-level (gnus-group-group-level) nil))
6173       (gnus-group-group-name))))
6174
6175 (defun gnus-summary-best-group (&optional exclude-group)
6176   "Find the name of the best unread group.
6177 If EXCLUDE-GROUP, do not go to this group."
6178   (save-excursion
6179     (set-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           (save-excursion
6552             (set-buffer gnus-group-buffer)
6553             (gnus-undo-force-boundary))
6554           (gnus-update-read-articles
6555            group (gnus-sorted-union
6556                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6557           ;; Set the current article marks.
6558           (let ((gnus-newsgroup-scored
6559                  (if (and (not gnus-save-score)
6560                           (not non-destructive))
6561                      nil
6562                    gnus-newsgroup-scored)))
6563             (save-excursion
6564               (gnus-update-marks)))
6565           ;; Do the cross-ref thing.
6566           (when gnus-use-cross-reference
6567             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6568           ;; Do not switch windows but change the buffer to work.
6569           (set-buffer gnus-group-buffer)
6570           (unless (gnus-ephemeral-group-p group)
6571             (gnus-group-update-group group)))))))
6572
6573 (defun gnus-summary-save-newsrc (&optional force)
6574   "Save the current number of read/marked articles in the dribble buffer.
6575 The dribble buffer will then be saved.
6576 If FORCE (the prefix), also save the .newsrc file(s)."
6577   (interactive "P")
6578   (gnus-summary-update-info t)
6579   (if force
6580       (gnus-save-newsrc-file)
6581     (gnus-dribble-save)))
6582
6583 (defun gnus-summary-exit (&optional temporary leave-hidden)
6584   "Exit reading current newsgroup, and then return to group selection mode.
6585 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6586   (interactive)
6587   (gnus-set-global-variables)
6588   (when (gnus-buffer-live-p gnus-article-buffer)
6589     (save-excursion
6590       (set-buffer gnus-article-buffer)
6591       (mm-destroy-parts gnus-article-mime-handles)
6592       ;; Set it to nil for safety reason.
6593       (setq gnus-article-mime-handle-alist nil)
6594       (setq gnus-article-mime-handles nil)))
6595   (gnus-kill-save-kill-buffer)
6596   (gnus-async-halt-prefetch)
6597   (let* ((group gnus-newsgroup-name)
6598          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6599          (gnus-group-is-exiting-p t)
6600          (mode major-mode)
6601          (group-point nil)
6602          (buf (current-buffer)))
6603     (unless quit-config
6604       ;; Do adaptive scoring, and possibly save score files.
6605       (when gnus-newsgroup-adaptive
6606         (gnus-score-adaptive))
6607       (when gnus-use-scoring
6608         (gnus-score-save)))
6609     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6610     ;; If we have several article buffers, we kill them at exit.
6611     (unless gnus-single-article-buffer
6612       (gnus-kill-buffer gnus-original-article-buffer)
6613       (setq gnus-article-current nil))
6614     (when gnus-use-cache
6615       (gnus-cache-possibly-remove-articles)
6616       (gnus-cache-save-buffers))
6617     (gnus-async-prefetch-remove-group group)
6618     (when gnus-suppress-duplicates
6619       (gnus-dup-enter-articles))
6620     (when gnus-use-trees
6621       (gnus-tree-close group))
6622     (when gnus-use-cache
6623       (gnus-cache-write-active))
6624     ;; Remove entries for this group.
6625     (nnmail-purge-split-history (gnus-group-real-name group))
6626     ;; Make all changes in this group permanent.
6627     (unless quit-config
6628       (gnus-run-hooks 'gnus-exit-group-hook)
6629       (gnus-summary-update-info))
6630     (gnus-close-group group)
6631     ;; Make sure where we were, and go to next newsgroup.
6632     (set-buffer gnus-group-buffer)
6633     (unless quit-config
6634       (gnus-group-jump-to-group group))
6635     (gnus-run-hooks 'gnus-summary-exit-hook)
6636     (unless (or quit-config
6637                 ;; If this group has disappeared from the summary
6638                 ;; buffer, don't skip forwards.
6639                 (not (string= group (gnus-group-group-name))))
6640       (gnus-group-next-unread-group 1))
6641     (setq group-point (point))
6642     (if temporary
6643         nil                             ;Nothing to do.
6644       ;; If we have several article buffers, we kill them at exit.
6645       (unless gnus-single-article-buffer
6646         (gnus-kill-buffer gnus-article-buffer)
6647         (gnus-kill-buffer gnus-original-article-buffer)
6648         (setq gnus-article-current nil))
6649       (set-buffer buf)
6650       (if (not gnus-kill-summary-on-exit)
6651           (progn
6652             (gnus-deaden-summary)
6653             (setq mode nil))
6654         ;; We set all buffer-local variables to nil.  It is unclear why
6655         ;; this is needed, but if we don't, buffer-local variables are
6656         ;; not garbage-collected, it seems.  This would the lead to en
6657         ;; ever-growing Emacs.
6658         (gnus-summary-clear-local-variables)
6659         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6660           (gnus-summary-clear-local-variables))
6661         (when (get-buffer gnus-article-buffer)
6662           (bury-buffer gnus-article-buffer))
6663         ;; We clear the global counterparts of the buffer-local
6664         ;; variables as well, just to be on the safe side.
6665         (set-buffer gnus-group-buffer)
6666         (gnus-summary-clear-local-variables)
6667         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6668           (gnus-summary-clear-local-variables))
6669         ;; Return to group mode buffer.
6670         (when (eq mode 'gnus-summary-mode)
6671           (gnus-kill-buffer buf)))
6672       (setq gnus-current-select-method gnus-select-method)
6673       (if leave-hidden
6674           (set-buffer gnus-group-buffer)
6675         (pop-to-buffer gnus-group-buffer))
6676       (if (not quit-config)
6677           (progn
6678             (goto-char group-point)
6679             (unless leave-hidden
6680               (gnus-configure-windows 'group 'force)))
6681         (gnus-handle-ephemeral-exit quit-config))
6682       ;; Clear the current group name.
6683       (unless quit-config
6684         (setq gnus-newsgroup-name nil)))))
6685
6686 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6687 (defun gnus-summary-exit-no-update (&optional no-questions)
6688   "Quit reading current newsgroup without updating read article info."
6689   (interactive)
6690   (let* ((group gnus-newsgroup-name)
6691          (gnus-group-is-exiting-p t)
6692          (gnus-group-is-exiting-without-update-p t)
6693          (quit-config (gnus-group-quit-config group)))
6694     (when (or no-questions
6695               gnus-expert-user
6696               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6697       (gnus-async-halt-prefetch)
6698       (run-hooks 'gnus-summary-prepare-exit-hook)
6699       (when (gnus-buffer-live-p gnus-article-buffer)
6700         (save-excursion
6701           (set-buffer gnus-article-buffer)
6702           (mm-destroy-parts gnus-article-mime-handles)
6703           ;; Set it to nil for safety reason.
6704           (setq gnus-article-mime-handle-alist nil)
6705           (setq gnus-article-mime-handles nil)))
6706       ;; If we have several article buffers, we kill them at exit.
6707       (unless gnus-single-article-buffer
6708         (gnus-kill-buffer gnus-article-buffer)
6709         (gnus-kill-buffer gnus-original-article-buffer)
6710         (setq gnus-article-current nil))
6711       (if (not gnus-kill-summary-on-exit)
6712           (gnus-deaden-summary)
6713         (gnus-close-group group)
6714         (gnus-summary-clear-local-variables)
6715         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6716           (gnus-summary-clear-local-variables))
6717         (set-buffer gnus-group-buffer)
6718         (gnus-summary-clear-local-variables)
6719         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6720           (gnus-summary-clear-local-variables))
6721         (gnus-kill-buffer gnus-summary-buffer))
6722       (unless gnus-single-article-buffer
6723         (setq gnus-article-current nil))
6724       (when gnus-use-trees
6725         (gnus-tree-close group))
6726       (gnus-async-prefetch-remove-group group)
6727       (when (get-buffer gnus-article-buffer)
6728         (bury-buffer gnus-article-buffer))
6729       ;; Return to the group buffer.
6730       (gnus-configure-windows 'group 'force)
6731       ;; Clear the current group name.
6732       (setq gnus-newsgroup-name nil)
6733       (unless (gnus-ephemeral-group-p group)
6734         (gnus-group-update-group group))
6735       (when (equal (gnus-group-group-name) group)
6736         (gnus-group-next-unread-group 1))
6737       (when quit-config
6738         (gnus-handle-ephemeral-exit quit-config)))))
6739
6740 (defun gnus-handle-ephemeral-exit (quit-config)
6741   "Handle movement when leaving an ephemeral group.
6742 The state which existed when entering the ephemeral is reset."
6743   (if (not (buffer-name (car quit-config)))
6744       (gnus-configure-windows 'group 'force)
6745     (set-buffer (car quit-config))
6746     (cond ((eq major-mode 'gnus-summary-mode)
6747            (gnus-set-global-variables))
6748           ((eq major-mode 'gnus-article-mode)
6749            (save-excursion
6750              ;; The `gnus-summary-buffer' variable may point
6751              ;; to the old summary buffer when using a single
6752              ;; article buffer.
6753              (unless (gnus-buffer-live-p gnus-summary-buffer)
6754                (set-buffer gnus-group-buffer))
6755              (set-buffer gnus-summary-buffer)
6756              (gnus-set-global-variables))))
6757     (if (or (eq (cdr quit-config) 'article)
6758             (eq (cdr quit-config) 'pick))
6759         (progn
6760           ;; The current article may be from the ephemeral group
6761           ;; thus it is best that we reload this article
6762           ;;
6763           ;; If we're exiting from a large digest, this can be
6764           ;; extremely slow.  So, it's better not to reload it. -- jh.
6765           ;;(gnus-summary-show-article)
6766           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6767               (gnus-configure-windows 'pick 'force)
6768             (gnus-configure-windows (cdr quit-config) 'force)))
6769       (gnus-configure-windows (cdr quit-config) 'force))
6770     (when (eq major-mode 'gnus-summary-mode)
6771       (gnus-summary-next-subject 1 nil t)
6772       (gnus-summary-recenter)
6773       (gnus-summary-position-point))))
6774
6775 ;;; Dead summaries.
6776
6777 (defvar gnus-dead-summary-mode-map nil)
6778
6779 (unless gnus-dead-summary-mode-map
6780   (setq gnus-dead-summary-mode-map (make-keymap))
6781   (suppress-keymap gnus-dead-summary-mode-map)
6782   (substitute-key-definition
6783    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6784   (dolist (key '("\C-d" "\r" "\177" [delete]))
6785     (define-key gnus-dead-summary-mode-map
6786       key 'gnus-summary-wake-up-the-dead))
6787   (dolist (key '("q" "Q"))
6788     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6789
6790 (defvar gnus-dead-summary-mode nil
6791   "Minor mode for Gnus summary buffers.")
6792
6793 (defun gnus-dead-summary-mode (&optional arg)
6794   "Minor mode for Gnus summary buffers."
6795   (interactive "P")
6796   (when (eq major-mode 'gnus-summary-mode)
6797     (make-local-variable 'gnus-dead-summary-mode)
6798     (setq gnus-dead-summary-mode
6799           (if (null arg) (not gnus-dead-summary-mode)
6800             (> (prefix-numeric-value arg) 0)))
6801     (when gnus-dead-summary-mode
6802       (add-minor-mode
6803        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6804
6805 (defun gnus-deaden-summary ()
6806   "Make the current summary buffer into a dead summary buffer."
6807   ;; Kill any previous dead summary buffer.
6808   (when (and gnus-dead-summary
6809              (buffer-name gnus-dead-summary))
6810     (save-excursion
6811       (set-buffer gnus-dead-summary)
6812       (when gnus-dead-summary-mode
6813         (kill-buffer (current-buffer)))))
6814   ;; Make this the current dead summary.
6815   (setq gnus-dead-summary (current-buffer))
6816   (gnus-dead-summary-mode 1)
6817   (let ((name (buffer-name)))
6818     (when (string-match "Summary" name)
6819       (rename-buffer
6820        (concat (substring name 0 (match-beginning 0)) "Dead "
6821                (substring name (match-beginning 0)))
6822        t)
6823       (bury-buffer))))
6824
6825 (defun gnus-kill-or-deaden-summary (buffer)
6826   "Kill or deaden the summary BUFFER."
6827   (save-excursion
6828     (when (and (buffer-name buffer)
6829                (not gnus-single-article-buffer))
6830       (save-excursion
6831         (set-buffer buffer)
6832         (gnus-kill-buffer gnus-article-buffer)
6833         (gnus-kill-buffer gnus-original-article-buffer)))
6834     (cond
6835      ;; Kill the buffer.
6836      (gnus-kill-summary-on-exit
6837       (when (and gnus-use-trees
6838                  (gnus-buffer-exists-p buffer))
6839         (save-excursion
6840           (set-buffer buffer)
6841           (gnus-tree-close gnus-newsgroup-name)))
6842       (gnus-kill-buffer buffer))
6843      ;; Deaden the buffer.
6844      ((gnus-buffer-exists-p buffer)
6845       (save-excursion
6846         (set-buffer buffer)
6847         (gnus-deaden-summary))))))
6848
6849 (defun gnus-summary-wake-up-the-dead (&rest args)
6850   "Wake up the dead summary buffer."
6851   (interactive)
6852   (gnus-dead-summary-mode -1)
6853   (let ((name (buffer-name)))
6854     (when (string-match "Dead " name)
6855       (rename-buffer
6856        (concat (substring name 0 (match-beginning 0))
6857                (substring name (match-end 0)))
6858        t)))
6859   (gnus-message 3 "This dead summary is now alive again"))
6860
6861 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6862 (defun gnus-summary-fetch-faq (&optional faq-dir)
6863   "Fetch the FAQ for the current group.
6864 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6865 in."
6866   (interactive
6867    (list
6868     (when current-prefix-arg
6869       (completing-read
6870        "FAQ dir: " (and (listp gnus-group-faq-directory)
6871                         (mapcar (lambda (file) (list file))
6872                                 gnus-group-faq-directory))))))
6873   (let (gnus-faq-buffer)
6874     (when (setq gnus-faq-buffer
6875                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6876       (gnus-configure-windows 'summary-faq))))
6877
6878 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6879 (defun gnus-summary-describe-group (&optional force)
6880   "Describe the current newsgroup."
6881   (interactive "P")
6882   (gnus-group-describe-group force gnus-newsgroup-name))
6883
6884 (defun gnus-summary-describe-briefly ()
6885   "Describe summary mode commands briefly."
6886   (interactive)
6887   (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")))
6888
6889 ;; Walking around group mode buffer from summary mode.
6890
6891 (defun gnus-summary-next-group (&optional no-article target-group backward)
6892   "Exit current newsgroup and then select next unread newsgroup.
6893 If prefix argument NO-ARTICLE is non-nil, no article is selected
6894 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6895 previous group instead."
6896   (interactive "P")
6897   ;; Stop pre-fetching.
6898   (gnus-async-halt-prefetch)
6899   (let ((current-group gnus-newsgroup-name)
6900         (current-buffer (current-buffer))
6901         entered)
6902     ;; First we semi-exit this group to update Xrefs and all variables.
6903     ;; We can't do a real exit, because the window conf must remain
6904     ;; the same in case the user is prompted for info, and we don't
6905     ;; want the window conf to change before that...
6906     (gnus-summary-exit t)
6907     (while (not entered)
6908       ;; Then we find what group we are supposed to enter.
6909       (set-buffer gnus-group-buffer)
6910       (gnus-group-jump-to-group current-group)
6911       (setq target-group
6912             (or target-group
6913                 (if (eq gnus-keep-same-level 'best)
6914                     (gnus-summary-best-group gnus-newsgroup-name)
6915                   (gnus-summary-search-group backward gnus-keep-same-level))))
6916       (if (not target-group)
6917           ;; There are no further groups, so we return to the group
6918           ;; buffer.
6919           (progn
6920             (gnus-message 5 "Returning to the group buffer")
6921             (setq entered t)
6922             (when (gnus-buffer-live-p current-buffer)
6923               (set-buffer current-buffer)
6924               (gnus-summary-exit))
6925             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6926         ;; We try to enter the target group.
6927         (gnus-group-jump-to-group target-group)
6928         (let ((unreads (gnus-group-group-unread)))
6929           (if (and (or (eq t unreads)
6930                        (and unreads (not (zerop unreads))))
6931                    (gnus-summary-read-group
6932                     target-group nil no-article
6933                     (and (buffer-name current-buffer) current-buffer)
6934                     nil backward))
6935               (setq entered t)
6936             (setq current-group target-group
6937                   target-group nil)))))))
6938
6939 (defun gnus-summary-prev-group (&optional no-article)
6940   "Exit current newsgroup and then select previous unread newsgroup.
6941 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6942   (interactive "P")
6943   (gnus-summary-next-group no-article nil t))
6944
6945 ;; Walking around summary lines.
6946
6947 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6948   "Go to the first subject satisfying any non-nil constraint.
6949 If UNREAD is non-nil, the article should be unread.
6950 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6951 If UNSEEN is non-nil, the article should be unseen.
6952 Returns the article selected or nil if there are no matching articles."
6953   (interactive "P")
6954   (cond
6955    ;; Empty summary.
6956    ((null gnus-newsgroup-data)
6957     (gnus-message 3 "No articles in the group")
6958     nil)
6959    ;; Pick the first article.
6960    ((not (or unread undownloaded unseen))
6961     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6962     (gnus-data-number (car gnus-newsgroup-data)))
6963    ;; Find the first unread article.
6964    (t
6965     (let ((data gnus-newsgroup-data))
6966       (while (and data
6967                   (let ((num (gnus-data-number (car data))))
6968                     (or (memq num gnus-newsgroup-unfetched)
6969                         (not (or (and unread
6970                                       (memq num gnus-newsgroup-unreads))
6971                                  (and undownloaded
6972                                       (memq num gnus-newsgroup-undownloaded))
6973                                  (and unseen
6974                                       (memq num gnus-newsgroup-unseen)))))))
6975         (setq data (cdr data)))
6976       (prog1
6977           (if data
6978               (progn
6979                 (goto-char (gnus-data-pos (car data)))
6980                 (gnus-data-number (car data)))
6981             (gnus-message 3 "No more%s articles"
6982                           (let* ((r (when unread " unread"))
6983                                  (d (when undownloaded " undownloaded"))
6984                                  (s (when unseen " unseen"))
6985                                  (l (delq nil (list r d s))))
6986                             (cond ((= 3 (length l))
6987                                    (concat r "," d ", or" s))
6988                                   ((= 2 (length l))
6989                                    (concat (car l) ", or" (cadr l)))
6990                                   ((= 1 (length l))
6991                                    (car l))
6992                                   (t
6993                                    ""))))
6994             nil
6995             )
6996         (gnus-summary-position-point))))))
6997
6998 (defun gnus-summary-next-subject (n &optional unread dont-display)
6999   "Go to next N'th summary line.
7000 If N is negative, go to the previous N'th subject line.
7001 If UNREAD is non-nil, only unread articles are selected.
7002 The difference between N and the actual number of steps taken is
7003 returned."
7004   (interactive "p")
7005   (let ((backward (< n 0))
7006         (n (abs n)))
7007     (while (and (> n 0)
7008                 (if backward
7009                     (gnus-summary-find-prev unread)
7010                   (gnus-summary-find-next unread)))
7011       (unless (zerop (setq n (1- n)))
7012         (gnus-summary-show-thread)))
7013     (when (/= 0 n)
7014       (gnus-message 7 "No more%s articles"
7015                     (if unread " unread" "")))
7016     (unless dont-display
7017       (gnus-summary-recenter)
7018       (gnus-summary-position-point))
7019     n))
7020
7021 (defun gnus-summary-next-unread-subject (n)
7022   "Go to next N'th unread summary line."
7023   (interactive "p")
7024   (gnus-summary-next-subject n t))
7025
7026 (defun gnus-summary-prev-subject (n &optional unread)
7027   "Go to previous N'th summary line.
7028 If optional argument UNREAD is non-nil, only unread article is selected."
7029   (interactive "p")
7030   (gnus-summary-next-subject (- n) unread))
7031
7032 (defun gnus-summary-prev-unread-subject (n)
7033   "Go to previous N'th unread summary line."
7034   (interactive "p")
7035   (gnus-summary-next-subject (- n) t))
7036
7037 (defun gnus-summary-goto-subjects (articles)
7038   "Insert the subject header for ARTICLES in the current buffer."
7039   (save-excursion
7040     (dolist (article articles)
7041       (gnus-summary-goto-subject article t)))
7042   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7043   (gnus-summary-position-point))
7044  
7045 (defun gnus-summary-goto-subject (article &optional force silent)
7046   "Go the subject line of ARTICLE.
7047 If FORCE, also allow jumping to articles not currently shown."
7048   (interactive "nArticle number: ")
7049   (unless (numberp article)
7050     (error "Article %s is not a number" article))
7051   (let ((b (point))
7052         (data (gnus-data-find article)))
7053     ;; We read in the article if we have to.
7054     (and (not data)
7055          force
7056          (gnus-summary-insert-subject
7057           article
7058           (if (or (numberp force) (vectorp force)) force)
7059           t)
7060          (setq data (gnus-data-find article)))
7061     (goto-char b)
7062     (if (not data)
7063         (progn
7064           (unless silent
7065             (gnus-message 3 "Can't find article %d" article))
7066           nil)
7067       (let ((pt (gnus-data-pos data)))
7068         (goto-char pt)
7069         (gnus-summary-set-article-display-arrow pt))
7070       (gnus-summary-position-point)
7071       article)))
7072
7073 ;; Walking around summary lines with displaying articles.
7074
7075 (defun gnus-summary-expand-window (&optional arg)
7076   "Make the summary buffer take up the entire Emacs frame.
7077 Given a prefix, will force an `article' buffer configuration."
7078   (interactive "P")
7079   (if arg
7080       (gnus-configure-windows 'article 'force)
7081     (gnus-configure-windows 'summary 'force)))
7082
7083 (defun gnus-summary-display-article (article &optional all-header)
7084   "Display ARTICLE in article buffer."
7085   (when (gnus-buffer-live-p gnus-article-buffer)
7086     (with-current-buffer gnus-article-buffer
7087       (mm-enable-multibyte)))
7088   (gnus-set-global-variables)
7089   (when (gnus-buffer-live-p gnus-article-buffer)
7090     (with-current-buffer gnus-article-buffer
7091       (setq gnus-article-charset gnus-newsgroup-charset)
7092       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7093       (mm-enable-multibyte)))
7094   (if (null article)
7095       nil
7096     (prog1
7097         (if gnus-summary-display-article-function
7098             (funcall gnus-summary-display-article-function article all-header)
7099           (gnus-article-prepare article all-header))
7100       (gnus-run-hooks 'gnus-select-article-hook)
7101       (when (and gnus-current-article
7102                  (not (zerop gnus-current-article)))
7103         (gnus-summary-goto-subject gnus-current-article))
7104       (gnus-summary-recenter)
7105       (when (and gnus-use-trees gnus-show-threads)
7106         (gnus-possibly-generate-tree article)
7107         (gnus-highlight-selected-tree article))
7108       ;; Successfully display article.
7109       (gnus-article-set-window-start
7110        (cdr (assq article gnus-newsgroup-bookmarks))))))
7111
7112 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7113   "Select the current article.
7114 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7115 non-nil, the article will be re-fetched even if it already present in
7116 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7117 be displayed."
7118   ;; Make sure we are in the summary buffer to work around bbdb bug.
7119   (unless (eq major-mode 'gnus-summary-mode)
7120     (set-buffer gnus-summary-buffer))
7121   (let ((article (or article (gnus-summary-article-number)))
7122         (all-headers (not (not all-headers))) ;Must be t or nil.
7123         gnus-summary-display-article-function)
7124     (and (not pseudo)
7125          (gnus-summary-article-pseudo-p article)
7126          (error "This is a pseudo-article"))
7127     (save-excursion
7128       (set-buffer gnus-summary-buffer)
7129       (if (or (and gnus-single-article-buffer
7130                    (or (null gnus-current-article)
7131                        (null gnus-article-current)
7132                        (null (get-buffer gnus-article-buffer))
7133                        (not (eq article (cdr gnus-article-current)))
7134                        (not (equal (car gnus-article-current)
7135                                    gnus-newsgroup-name))))
7136               (and (not gnus-single-article-buffer)
7137                    (or (null gnus-current-article)
7138                        (not (eq gnus-current-article article))))
7139               force)
7140           ;; The requested article is different from the current article.
7141           (progn
7142             (gnus-summary-display-article article all-headers)
7143             (when (gnus-buffer-live-p gnus-article-buffer)
7144               (with-current-buffer gnus-article-buffer
7145                 (if (not gnus-article-decoded-p) ;; a local variable
7146                     (mm-disable-multibyte))))
7147             (gnus-article-set-window-start
7148              (cdr (assq article gnus-newsgroup-bookmarks)))
7149             article)
7150         'old))))
7151
7152 (defun gnus-summary-force-verify-and-decrypt ()
7153   "Display buttons for signed/encrypted parts and verify/decrypt them."
7154   (interactive)
7155   (let ((mm-verify-option 'known)
7156         (mm-decrypt-option 'known)
7157         (gnus-article-emulate-mime t)
7158         (gnus-buttonized-mime-types (append (list "multipart/signed"
7159                                                   "multipart/encrypted")
7160                                             gnus-buttonized-mime-types)))
7161     (gnus-summary-select-article nil 'force)))
7162
7163 (defun gnus-summary-set-current-mark (&optional current-mark)
7164   "Obsolete function."
7165   nil)
7166
7167 (defun gnus-summary-next-article (&optional unread subject backward push)
7168   "Select the next article.
7169 If UNREAD, only unread articles are selected.
7170 If SUBJECT, only articles with SUBJECT are selected.
7171 If BACKWARD, the previous article is selected instead of the next."
7172   (interactive "P")
7173   (cond
7174    ;; Is there such an article?
7175    ((and (gnus-summary-search-forward unread subject backward)
7176          (or (gnus-summary-display-article (gnus-summary-article-number))
7177              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7178     (gnus-summary-position-point))
7179    ;; If not, we try the first unread, if that is wanted.
7180    ((and subject
7181          gnus-auto-select-same
7182          (gnus-summary-first-unread-article))
7183     (gnus-summary-position-point)
7184     (gnus-message 6 "Wrapped"))
7185    ;; Try to get next/previous article not displayed in this group.
7186    ((and gnus-auto-extend-newsgroup
7187          (not unread) (not subject))
7188     (gnus-summary-goto-article
7189      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7190      nil (count-lines (point-min) (point))))
7191    ;; Go to next/previous group.
7192    (t
7193     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7194       (gnus-summary-jump-to-group gnus-newsgroup-name))
7195     (let ((cmd last-command-char)
7196           (point
7197            (save-excursion
7198              (set-buffer gnus-group-buffer)
7199              (point)))
7200           (group
7201            (if (eq gnus-keep-same-level 'best)
7202                (gnus-summary-best-group gnus-newsgroup-name)
7203              (gnus-summary-search-group backward gnus-keep-same-level))))
7204       ;; For some reason, the group window gets selected.  We change
7205       ;; it back.
7206       (select-window (get-buffer-window (current-buffer)))
7207       ;; Select next unread newsgroup automagically.
7208       (cond
7209        ((or (not gnus-auto-select-next)
7210             (not cmd))
7211         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7212        ((or (eq gnus-auto-select-next 'quietly)
7213             (and (eq gnus-auto-select-next 'slightly-quietly)
7214                  push)
7215             (and (eq gnus-auto-select-next 'almost-quietly)
7216                  (gnus-summary-last-article-p)))
7217         ;; Select quietly.
7218         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7219             (gnus-summary-exit)
7220           (gnus-message 7 "No more%s articles (%s)..."
7221                         (if unread " unread" "")
7222                         (if group (concat "selecting " group)
7223                           "exiting"))
7224           (gnus-summary-next-group nil group backward)))
7225        (t
7226         (when (gnus-key-press-event-p last-input-event)
7227           (gnus-summary-walk-group-buffer
7228            gnus-newsgroup-name cmd unread backward point))))))))
7229
7230 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7231   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7232                       (?\C-p (gnus-group-prev-unread-group 1))))
7233         (cursor-in-echo-area t)
7234         keve key group ended prompt)
7235     (save-excursion
7236       (set-buffer gnus-group-buffer)
7237       (goto-char start)
7238       (setq group
7239             (if (eq gnus-keep-same-level 'best)
7240                 (gnus-summary-best-group gnus-newsgroup-name)
7241               (gnus-summary-search-group backward gnus-keep-same-level))))
7242     (while (not ended)
7243       (setq prompt
7244             (format
7245              "No more%s articles%s " (if unread " unread" "")
7246              (if (and group
7247                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7248                  (format " (Type %s for %s [%s])"
7249                          (single-key-description cmd) group
7250                          (gnus-group-unread group))
7251                (format " (Type %s to exit %s)"
7252                        (single-key-description cmd)
7253                        gnus-newsgroup-name))))
7254       ;; Confirm auto selection.
7255       (setq key (car (setq keve (gnus-read-event-char prompt)))
7256             ended t)
7257       (cond
7258        ((assq key keystrokes)
7259         (let ((obuf (current-buffer)))
7260           (switch-to-buffer gnus-group-buffer)
7261           (when group
7262             (gnus-group-jump-to-group group))
7263           (eval (cadr (assq key keystrokes)))
7264           (setq group (gnus-group-group-name))
7265           (switch-to-buffer obuf))
7266         (setq ended nil))
7267        ((equal key cmd)
7268         (if (or (not group)
7269                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7270             (gnus-summary-exit)
7271           (gnus-summary-next-group nil group backward)))
7272        (t
7273         (push (cdr keve) unread-command-events))))))
7274
7275 (defun gnus-summary-next-unread-article ()
7276   "Select unread article after current one."
7277   (interactive)
7278   (gnus-summary-next-article
7279    (or (not (eq gnus-summary-goto-unread 'never))
7280        (gnus-summary-last-article-p (gnus-summary-article-number)))
7281    (and gnus-auto-select-same
7282         (gnus-summary-article-subject))))
7283
7284 (defun gnus-summary-prev-article (&optional unread subject)
7285   "Select the article after the current one.
7286 If UNREAD is non-nil, only unread articles are selected."
7287   (interactive "P")
7288   (gnus-summary-next-article unread subject t))
7289
7290 (defun gnus-summary-prev-unread-article ()
7291   "Select unread article before current one."
7292   (interactive)
7293   (gnus-summary-prev-article
7294    (or (not (eq gnus-summary-goto-unread 'never))
7295        (gnus-summary-first-article-p (gnus-summary-article-number)))
7296    (and gnus-auto-select-same
7297         (gnus-summary-article-subject))))
7298
7299 (defun gnus-summary-next-page (&optional lines circular stop)
7300   "Show next page of the selected article.
7301 If at the end of the current article, select the next article.
7302 LINES says how many lines should be scrolled up.
7303
7304 If CIRCULAR is non-nil, go to the start of the article instead of
7305 selecting the next article when reaching the end of the current
7306 article.
7307
7308 If STOP is non-nil, just stop when reaching the end of the message.
7309
7310 Also see the variable `gnus-article-skip-boring'."
7311   (interactive "P")
7312   (setq gnus-summary-buffer (current-buffer))
7313   (gnus-set-global-variables)
7314   (let ((article (gnus-summary-article-number))
7315         (article-window (get-buffer-window gnus-article-buffer t))
7316         endp)
7317     ;; If the buffer is empty, we have no article.
7318     (unless article
7319       (error "No article to select"))
7320     (gnus-configure-windows 'article)
7321     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7322         (if (and (eq gnus-summary-goto-unread 'never)
7323                  (not (gnus-summary-last-article-p article)))
7324             (gnus-summary-next-article)
7325           (gnus-summary-next-unread-article))
7326       (if (or (null gnus-current-article)
7327               (null gnus-article-current)
7328               (/= article (cdr gnus-article-current))
7329               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7330           ;; Selected subject is different from current article's.
7331           (gnus-summary-display-article article)
7332         (when article-window
7333           (gnus-eval-in-buffer-window gnus-article-buffer
7334             (setq endp (or (gnus-article-next-page lines)
7335                            (gnus-article-only-boring-p))))
7336           (when endp
7337             (cond (stop
7338                    (gnus-message 3 "End of message"))
7339                   (circular
7340                    (gnus-summary-beginning-of-article))
7341                   (lines
7342                    (gnus-message 3 "End of message"))
7343                   ((null lines)
7344                    (if (and (eq gnus-summary-goto-unread 'never)
7345                             (not (gnus-summary-last-article-p article)))
7346                        (gnus-summary-next-article)
7347                      (gnus-summary-next-unread-article))))))))
7348     (gnus-summary-recenter)
7349     (gnus-summary-position-point)))
7350
7351 (defun gnus-summary-prev-page (&optional lines move)
7352   "Show previous page of selected article.
7353 Argument LINES specifies lines to be scrolled down.
7354 If MOVE, move to the previous unread article if point is at
7355 the beginning of the buffer."
7356   (interactive "P")
7357   (let ((article (gnus-summary-article-number))
7358         (article-window (get-buffer-window gnus-article-buffer t))
7359         endp)
7360     (gnus-configure-windows 'article)
7361     (if (or (null gnus-current-article)
7362             (null gnus-article-current)
7363             (/= article (cdr gnus-article-current))
7364             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7365         ;; Selected subject is different from current article's.
7366         (gnus-summary-display-article article)
7367       (gnus-summary-recenter)
7368       (when article-window
7369         (gnus-eval-in-buffer-window gnus-article-buffer
7370           (setq endp (gnus-article-prev-page lines)))
7371         (when (and move endp)
7372           (cond (lines
7373                  (gnus-message 3 "Beginning of message"))
7374                 ((null lines)
7375                  (if (and (eq gnus-summary-goto-unread 'never)
7376                           (not (gnus-summary-first-article-p article)))
7377                      (gnus-summary-prev-article)
7378                    (gnus-summary-prev-unread-article))))))))
7379   (gnus-summary-position-point))
7380
7381 (defun gnus-summary-prev-page-or-article (&optional lines)
7382   "Show previous page of selected article.
7383 Argument LINES specifies lines to be scrolled down.
7384 If at the beginning of the article, go to the next article."
7385   (interactive "P")
7386   (gnus-summary-prev-page lines t))
7387
7388 (defun gnus-summary-scroll-up (lines)
7389   "Scroll up (or down) one line current article.
7390 Argument LINES specifies lines to be scrolled up (or down if negative)."
7391   (interactive "p")
7392   (gnus-configure-windows 'article)
7393   (gnus-summary-show-thread)
7394   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7395     (gnus-eval-in-buffer-window gnus-article-buffer
7396       (cond ((> lines 0)
7397              (when (gnus-article-next-page lines)
7398                (gnus-message 3 "End of message")))
7399             ((< lines 0)
7400              (gnus-article-prev-page (- lines))))))
7401   (gnus-summary-recenter)
7402   (gnus-summary-position-point))
7403
7404 (defun gnus-summary-scroll-down (lines)
7405   "Scroll down (or up) one line current article.
7406 Argument LINES specifies lines to be scrolled down (or up if negative)."
7407   (interactive "p")
7408   (gnus-summary-scroll-up (- lines)))
7409
7410 (defun gnus-summary-next-same-subject ()
7411   "Select next article which has the same subject as current one."
7412   (interactive)
7413   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7414
7415 (defun gnus-summary-prev-same-subject ()
7416   "Select previous article which has the same subject as current one."
7417   (interactive)
7418   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7419
7420 (defun gnus-summary-next-unread-same-subject ()
7421   "Select next unread article which has the same subject as current one."
7422   (interactive)
7423   (gnus-summary-next-article t (gnus-summary-article-subject)))
7424
7425 (defun gnus-summary-prev-unread-same-subject ()
7426   "Select previous unread article which has the same subject as current one."
7427   (interactive)
7428   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7429
7430 (defun gnus-summary-first-unread-article ()
7431   "Select the first unread article.
7432 Return nil if there are no unread articles."
7433   (interactive)
7434   (prog1
7435       (when (gnus-summary-first-subject t)
7436         (gnus-summary-show-thread)
7437         (gnus-summary-first-subject t)
7438         (gnus-summary-display-article (gnus-summary-article-number)))
7439     (gnus-summary-position-point)))
7440
7441 (defun gnus-summary-first-unread-subject ()
7442   "Place the point on the subject line of the first unread article.
7443 Return nil if there are no unread articles."
7444   (interactive)
7445   (prog1
7446       (when (gnus-summary-first-subject t)
7447         (gnus-summary-show-thread)
7448         (gnus-summary-first-subject t))
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-first-unseen-subject ()
7452   "Place the point on the subject line of the first unseen article.
7453 Return nil if there are no unseen articles."
7454   (interactive)
7455   (prog1
7456       (when (gnus-summary-first-subject nil nil t)
7457         (gnus-summary-show-thread)
7458         (gnus-summary-first-subject nil nil t))
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-first-unseen-or-unread-subject ()
7462   "Place the point on the subject line of the first unseen article or,
7463 if all article have been seen, on the subject line of the first unread
7464 article."
7465   (interactive)
7466   (prog1
7467       (unless (when (gnus-summary-first-subject nil nil t)
7468                 (gnus-summary-show-thread)
7469                 (gnus-summary-first-subject nil nil t))
7470         (when (gnus-summary-first-subject t)
7471           (gnus-summary-show-thread)
7472           (gnus-summary-first-subject t)))
7473     (gnus-summary-position-point)))
7474
7475 (defun gnus-summary-first-article ()
7476   "Select the first article.
7477 Return nil if there are no articles."
7478   (interactive)
7479   (prog1
7480       (when (gnus-summary-first-subject)
7481         (gnus-summary-show-thread)
7482         (gnus-summary-first-subject)
7483         (gnus-summary-display-article (gnus-summary-article-number)))
7484     (gnus-summary-position-point)))
7485
7486 (defun gnus-summary-best-unread-article (&optional arg)
7487   "Select the unread article with the highest score.
7488 If given a prefix argument, select the next unread article that has a
7489 score higher than the default score."
7490   (interactive "P")
7491   (let ((article (if arg
7492                      (gnus-summary-better-unread-subject)
7493                    (gnus-summary-best-unread-subject))))
7494     (if article
7495         (gnus-summary-goto-article article)
7496       (error "No unread articles"))))
7497
7498 (defun gnus-summary-best-unread-subject ()
7499   "Select the unread subject with the highest score."
7500   (interactive)
7501   (let ((best -1000000)
7502         (data gnus-newsgroup-data)
7503         article score)
7504     (while data
7505       (and (gnus-data-unread-p (car data))
7506            (> (setq score
7507                     (gnus-summary-article-score (gnus-data-number (car data))))
7508               best)
7509            (setq best score
7510                  article (gnus-data-number (car data))))
7511       (setq data (cdr data)))
7512     (when article
7513       (gnus-summary-goto-subject article))
7514     (gnus-summary-position-point)
7515     article))
7516
7517 (defun gnus-summary-better-unread-subject ()
7518   "Select the first unread subject that has a score over the default score."
7519   (interactive)
7520   (let ((data gnus-newsgroup-data)
7521         article score)
7522     (while (and (setq article (gnus-data-number (car data)))
7523                 (or (gnus-data-read-p (car data))
7524                     (not (> (gnus-summary-article-score article)
7525                             gnus-summary-default-score))))
7526       (setq data (cdr data)))
7527     (when article
7528       (gnus-summary-goto-subject article))
7529     (gnus-summary-position-point)
7530     article))
7531
7532 (defun gnus-summary-last-subject ()
7533   "Go to the last displayed subject line in the group."
7534   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7535     (when article
7536       (gnus-summary-goto-subject article))))
7537
7538 (defun gnus-summary-goto-article (article &optional all-headers force)
7539   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7540 If ALL-HEADERS is non-nil, no header lines are hidden.
7541 If FORCE, go to the article even if it isn't displayed.  If FORCE
7542 is a number, it is the line the article is to be displayed on."
7543   (interactive
7544    (list
7545     (completing-read
7546      "Article number or Message-ID: "
7547      (mapcar (lambda (number) (list (int-to-string number)))
7548              gnus-newsgroup-limit))
7549     current-prefix-arg
7550     t))
7551   (prog1
7552       (if (and (stringp article)
7553                (string-match "@\\|%40" article))
7554           (gnus-summary-refer-article article)
7555         (when (stringp article)
7556           (setq article (string-to-number article)))
7557         (if (gnus-summary-goto-subject article force)
7558             (gnus-summary-display-article article all-headers)
7559           (gnus-message 4 "Couldn't go to article %s" article) nil))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-goto-last-article ()
7563   "Go to the previously read article."
7564   (interactive)
7565   (prog1
7566       (when gnus-last-article
7567         (gnus-summary-goto-article gnus-last-article nil t))
7568     (gnus-summary-position-point)))
7569
7570 (defun gnus-summary-pop-article (number)
7571   "Pop one article off the history and go to the previous.
7572 NUMBER articles will be popped off."
7573   (interactive "p")
7574   (let (to)
7575     (setq gnus-newsgroup-history
7576           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7577     (if to
7578         (gnus-summary-goto-article (car to) nil t)
7579       (error "Article history empty")))
7580   (gnus-summary-position-point))
7581
7582 ;; Summary commands and functions for limiting the summary buffer.
7583
7584 (defun gnus-summary-limit-to-articles (n)
7585   "Limit the summary buffer to the next N articles.
7586 If not given a prefix, use the process marked articles instead."
7587   (interactive "P")
7588   (prog1
7589       (let ((articles (gnus-summary-work-articles n)))
7590         (setq gnus-newsgroup-processable nil)
7591         (gnus-summary-limit articles))
7592     (gnus-summary-position-point)))
7593
7594 (defun gnus-summary-pop-limit (&optional total)
7595   "Restore the previous limit.
7596 If given a prefix, remove all limits."
7597   (interactive "P")
7598   (when total
7599     (setq gnus-newsgroup-limits
7600           (list (mapcar (lambda (h) (mail-header-number h))
7601                         gnus-newsgroup-headers))))
7602   (unless gnus-newsgroup-limits
7603     (error "No limit to pop"))
7604   (prog1
7605       (gnus-summary-limit nil 'pop)
7606     (gnus-summary-position-point)))
7607
7608 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7609   "Limit the summary buffer to articles that have subjects that match a regexp.
7610 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7611   (interactive
7612    (list (read-string (if current-prefix-arg
7613                           "Exclude subject (regexp): "
7614                         "Limit to subject (regexp): "))
7615          nil current-prefix-arg))
7616   (unless header
7617     (setq header "subject"))
7618   (when (not (equal "" subject))
7619     (prog1
7620         (let ((articles (gnus-summary-find-matching
7621                          (or header "subject") subject 'all nil nil
7622                          not-matching)))
7623           (unless articles
7624             (error "Found no matches for \"%s\"" subject))
7625           (gnus-summary-limit articles))
7626       (gnus-summary-position-point))))
7627
7628 (defun gnus-summary-limit-to-author (from &optional not-matching)
7629   "Limit the summary buffer to articles that have authors that match a regexp.
7630 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7631   (interactive
7632    (list (read-string (if current-prefix-arg
7633                           "Exclude author (regexp): "
7634                         "Limit to author (regexp): "))
7635          current-prefix-arg))
7636   (gnus-summary-limit-to-subject from "from" not-matching))
7637
7638 (defun gnus-summary-limit-to-age (age &optional younger-p)
7639   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7640 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7641 articles that are younger than AGE days."
7642   (interactive
7643    (let ((younger current-prefix-arg)
7644          (days-got nil)
7645          days)
7646      (while (not days-got)
7647        (setq days (if younger
7648                       (read-string "Limit to articles younger than (in days, older when negative): ")
7649                     (read-string
7650                      "Limit to articles older than (in days, younger when negative): ")))
7651        (when (> (length days) 0)
7652          (setq days (read days)))
7653        (if (numberp days)
7654            (progn
7655              (setq days-got t)
7656              (when (< days 0)
7657                (setq younger (not younger))
7658                (setq days (* days -1))))
7659          (message "Please enter a number.")
7660          (sleep-for 1)))
7661      (list days younger)))
7662   (prog1
7663       (let ((data gnus-newsgroup-data)
7664             (cutoff (days-to-time age))
7665             articles d date is-younger)
7666         (while (setq d (pop data))
7667           (when (and (vectorp (gnus-data-header d))
7668                      (setq date (mail-header-date (gnus-data-header d))))
7669             (setq is-younger (time-less-p
7670                               (time-since (condition-case ()
7671                                               (date-to-time date)
7672                                             (error '(0 0))))
7673                               cutoff))
7674             (when (if younger-p
7675                       is-younger
7676                     (not is-younger))
7677               (push (gnus-data-number d) articles))))
7678         (gnus-summary-limit (nreverse articles)))
7679     (gnus-summary-position-point)))
7680
7681 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7682   "Limit the summary buffer to articles that match an 'extra' header."
7683   (interactive
7684    (let ((header
7685           (intern
7686            (gnus-completing-read-with-default
7687             (symbol-name (car gnus-extra-headers))
7688             (if current-prefix-arg
7689                 "Exclude extra header:"
7690               "Limit extra header:")
7691             (mapcar (lambda (x)
7692                       (cons (symbol-name x) x))
7693                     gnus-extra-headers)
7694             nil
7695             t))))
7696      (list header
7697            (read-string (format "%s header %s (regexp): "
7698                                 (if current-prefix-arg "Exclude" "Limit to")
7699                                 header))
7700            current-prefix-arg)))
7701   (when (not (equal "" regexp))
7702     (prog1
7703         (let ((articles (gnus-summary-find-matching
7704                          (cons 'extra header) regexp 'all nil nil
7705                          not-matching)))
7706           (unless articles
7707             (error "Found no matches for \"%s\"" regexp))
7708           (gnus-summary-limit articles))
7709       (gnus-summary-position-point))))
7710
7711 (defun gnus-summary-limit-to-display-predicate ()
7712   "Limit the summary buffer to the predicated in the `display' group parameter."
7713   (interactive)
7714   (unless gnus-newsgroup-display
7715     (error "There is no `display' group parameter"))
7716   (let (articles)
7717     (dolist (number gnus-newsgroup-articles)
7718       (when (funcall gnus-newsgroup-display)
7719         (push number articles)))
7720     (gnus-summary-limit articles))
7721   (gnus-summary-position-point))
7722
7723 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7724 (make-obsolete
7725  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7726
7727 (defun gnus-summary-limit-to-unread (&optional all)
7728   "Limit the summary buffer to articles that are not marked as read.
7729 If ALL is non-nil, limit strictly to unread articles."
7730   (interactive "P")
7731   (if all
7732       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7733     (gnus-summary-limit-to-marks
7734      ;; Concat all the marks that say that an article is read and have
7735      ;; those removed.
7736      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7737            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7738            gnus-low-score-mark gnus-expirable-mark
7739            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7740            gnus-duplicate-mark gnus-souped-mark)
7741      'reverse)))
7742
7743 (defun gnus-summary-limit-to-replied (&optional unreplied)
7744   "Limit the summary buffer to replied articles.
7745 If UNREPLIED (the prefix), limit to unreplied articles."
7746   (interactive "P")
7747   (if unreplied
7748       (gnus-summary-limit
7749        (gnus-set-difference gnus-newsgroup-articles
7750         gnus-newsgroup-replied))
7751     (gnus-summary-limit gnus-newsgroup-replied))
7752   (gnus-summary-position-point))
7753
7754 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7755 (make-obsolete 'gnus-summary-delete-marked-with
7756                'gnus-summary-limit-exclude-marks)
7757
7758 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7759   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7760 If REVERSE, limit the summary buffer to articles that are marked
7761 with MARKS.  MARKS can either be a string of marks or a list of marks.
7762 Returns how many articles were removed."
7763   (interactive "sMarks: ")
7764   (gnus-summary-limit-to-marks marks t))
7765
7766 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7767   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7768 If REVERSE (the prefix), limit the summary buffer to articles that are
7769 not marked with MARKS.  MARKS can either be a string of marks or a
7770 list of marks.
7771 Returns how many articles were removed."
7772   (interactive "sMarks: \nP")
7773   (prog1
7774       (let ((data gnus-newsgroup-data)
7775             (marks (if (listp marks) marks
7776                      (append marks nil))) ; Transform to list.
7777             articles)
7778         (while data
7779           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7780                   (memq (gnus-data-mark (car data)) marks))
7781             (push (gnus-data-number (car data)) articles))
7782           (setq data (cdr data)))
7783         (gnus-summary-limit articles))
7784     (gnus-summary-position-point)))
7785
7786 (defun gnus-summary-limit-to-score (score)
7787   "Limit to articles with score at or above SCORE."
7788   (interactive "NLimit to articles with score of at least: ")
7789   (let ((data gnus-newsgroup-data)
7790         articles)
7791     (while data
7792       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7793                 score)
7794         (push (gnus-data-number (car data)) articles))
7795       (setq data (cdr data)))
7796     (prog1
7797         (gnus-summary-limit articles)
7798       (gnus-summary-position-point))))
7799
7800 (defun gnus-summary-limit-to-unseen ()
7801   "Limit to unseen articles."
7802   (interactive)
7803   (prog1
7804       (gnus-summary-limit gnus-newsgroup-unseen)
7805     (gnus-summary-position-point)))
7806
7807 (defun gnus-summary-limit-include-thread (id)
7808   "Display all the hidden articles that is in the thread with ID in it.
7809 When called interactively, ID is the Message-ID of the current
7810 article."
7811   (interactive (list (mail-header-id (gnus-summary-article-header))))
7812   (let ((articles (gnus-articles-in-thread
7813                    (gnus-id-to-thread (gnus-root-id id)))))
7814     (prog1
7815         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7816       (gnus-summary-limit-include-matching-articles
7817        "subject"
7818        (regexp-quote (gnus-simplify-subject-re
7819                       (mail-header-subject (gnus-id-to-header id)))))
7820       (gnus-summary-position-point))))
7821
7822 (defun gnus-summary-limit-include-matching-articles (header regexp)
7823   "Display all the hidden articles that have HEADERs that match REGEXP."
7824   (interactive (list (read-string "Match on header: ")
7825                      (read-string "Regexp: ")))
7826   (let ((articles (gnus-find-matching-articles header regexp)))
7827     (prog1
7828         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7829       (gnus-summary-position-point))))
7830
7831 (defun gnus-summary-insert-dormant-articles ()
7832   "Insert all the dormant articles for this group into the current buffer."
7833   (interactive)
7834   (let ((gnus-verbose (max 6 gnus-verbose)))
7835     (if (not gnus-newsgroup-dormant)
7836         (gnus-message 3 "No cached articles for this group")
7837       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7838
7839 (defun gnus-summary-limit-include-dormant ()
7840   "Display all the hidden articles that are marked as dormant.
7841 Note that this command only works on a subset of the articles currently
7842 fetched for this group."
7843   (interactive)
7844   (unless gnus-newsgroup-dormant
7845     (error "There are no dormant articles in this group"))
7846   (prog1
7847       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7848     (gnus-summary-position-point)))
7849
7850 (defun gnus-summary-limit-exclude-dormant ()
7851   "Hide all dormant articles."
7852   (interactive)
7853   (prog1
7854       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7855     (gnus-summary-position-point)))
7856
7857 (defun gnus-summary-limit-exclude-childless-dormant ()
7858   "Hide all dormant articles that have no children."
7859   (interactive)
7860   (let ((data (gnus-data-list t))
7861         articles d children)
7862     ;; Find all articles that are either not dormant or have
7863     ;; children.
7864     (while (setq d (pop data))
7865       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7866                 (and (setq children
7867                            (gnus-article-children (gnus-data-number d)))
7868                      (let (found)
7869                        (while children
7870                          (when (memq (car children) articles)
7871                            (setq children nil
7872                                  found t))
7873                          (pop children))
7874                        found)))
7875         (push (gnus-data-number d) articles)))
7876     ;; Do the limiting.
7877     (prog1
7878         (gnus-summary-limit articles)
7879       (gnus-summary-position-point))))
7880
7881 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7882   "Mark all unread excluded articles as read.
7883 If ALL, mark even excluded ticked and dormants as read."
7884   (interactive "P")
7885   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7886   (let ((articles (gnus-sorted-ndifference
7887                    (sort
7888                     (mapcar (lambda (h) (mail-header-number h))
7889                             gnus-newsgroup-headers)
7890                     '<)
7891                    gnus-newsgroup-limit))
7892         article)
7893     (setq gnus-newsgroup-unreads
7894           (gnus-sorted-intersection gnus-newsgroup-unreads
7895                                     gnus-newsgroup-limit))
7896     (if all
7897         (setq gnus-newsgroup-dormant nil
7898               gnus-newsgroup-marked nil
7899               gnus-newsgroup-reads
7900               (nconc
7901                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7902                gnus-newsgroup-reads))
7903       (while (setq article (pop articles))
7904         (unless (or (memq article gnus-newsgroup-dormant)
7905                     (memq article gnus-newsgroup-marked))
7906           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7907
7908 (defun gnus-summary-limit (articles &optional pop)
7909   (if pop
7910       ;; We pop the previous limit off the stack and use that.
7911       (setq articles (car gnus-newsgroup-limits)
7912             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7913     ;; We use the new limit, so we push the old limit on the stack.
7914     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7915   ;; Set the limit.
7916   (setq gnus-newsgroup-limit articles)
7917   (let ((total (length gnus-newsgroup-data))
7918         (data (gnus-data-find-list (gnus-summary-article-number)))
7919         (gnus-summary-mark-below nil)   ; Inhibit this.
7920         found)
7921     ;; This will do all the work of generating the new summary buffer
7922     ;; according to the new limit.
7923     (gnus-summary-prepare)
7924     ;; Hide any threads, possibly.
7925     (gnus-summary-maybe-hide-threads)
7926     ;; Try to return to the article you were at, or one in the
7927     ;; neighborhood.
7928     (when data
7929       ;; We try to find some article after the current one.
7930       (while data
7931         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7932           (setq data nil
7933                 found t))
7934         (setq data (cdr data))))
7935     (unless found
7936       ;; If there is no data, that means that we were after the last
7937       ;; article.  The same goes when we can't find any articles
7938       ;; after the current one.
7939       (goto-char (point-max))
7940       (gnus-summary-find-prev))
7941     (gnus-set-mode-line 'summary)
7942     ;; We return how many articles were removed from the summary
7943     ;; buffer as a result of the new limit.
7944     (- total (length gnus-newsgroup-data))))
7945
7946 (defsubst gnus-invisible-cut-children (threads)
7947   (let ((num 0))
7948     (while threads
7949       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7950         (incf num))
7951       (pop threads))
7952     (< num 2)))
7953
7954 (defsubst gnus-cut-thread (thread)
7955   "Go forwards in the thread until we find an article that we want to display."
7956   (when (or (eq gnus-fetch-old-headers 'some)
7957             (eq gnus-fetch-old-headers 'invisible)
7958             (numberp gnus-fetch-old-headers)
7959             (eq gnus-build-sparse-threads 'some)
7960             (eq gnus-build-sparse-threads 'more))
7961     ;; Deal with old-fetched headers and sparse threads.
7962     (while (and
7963             thread
7964             (or
7965              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7966              (gnus-summary-article-ancient-p
7967               (mail-header-number (car thread))))
7968             (if (or (<= (length (cdr thread)) 1)
7969                     (eq gnus-fetch-old-headers 'invisible))
7970                 (setq gnus-newsgroup-limit
7971                       (delq (mail-header-number (car thread))
7972                             gnus-newsgroup-limit)
7973                       thread (cadr thread))
7974               (when (gnus-invisible-cut-children (cdr thread))
7975                 (let ((th (cdr thread)))
7976                   (while th
7977                     (if (memq (mail-header-number (caar th))
7978                               gnus-newsgroup-limit)
7979                         (setq thread (car th)
7980                               th nil)
7981                       (setq th (cdr th))))))))))
7982   thread)
7983
7984 (defun gnus-cut-threads (threads)
7985   "Cut off all uninteresting articles from the beginning of THREADS."
7986   (when (or (eq gnus-fetch-old-headers 'some)
7987             (eq gnus-fetch-old-headers 'invisible)
7988             (numberp gnus-fetch-old-headers)
7989             (eq gnus-build-sparse-threads 'some)
7990             (eq gnus-build-sparse-threads 'more))
7991     (let ((th threads))
7992       (while th
7993         (setcar th (gnus-cut-thread (car th)))
7994         (setq th (cdr th)))))
7995   ;; Remove nixed out threads.
7996   (delq nil threads))
7997
7998 (defun gnus-summary-initial-limit (&optional show-if-empty)
7999   "Figure out what the initial limit is supposed to be on group entry.
8000 This entails weeding out unwanted dormants, low-scored articles,
8001 fetch-old-headers verbiage, and so on."
8002   ;; Most groups have nothing to remove.
8003   (if (or gnus-inhibit-limiting
8004           (and (null gnus-newsgroup-dormant)
8005                (eq gnus-newsgroup-display 'gnus-not-ignore)
8006                (not (eq gnus-fetch-old-headers 'some))
8007                (not (numberp gnus-fetch-old-headers))
8008                (not (eq gnus-fetch-old-headers 'invisible))
8009                (null gnus-summary-expunge-below)
8010                (not (eq gnus-build-sparse-threads 'some))
8011                (not (eq gnus-build-sparse-threads 'more))
8012                (null gnus-thread-expunge-below)
8013                (not gnus-use-nocem)))
8014       ()                                ; Do nothing.
8015     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8016     (setq gnus-newsgroup-limit nil)
8017     (mapatoms
8018      (lambda (node)
8019        (unless (car (symbol-value node))
8020          ;; These threads have no parents -- they are roots.
8021          (let ((nodes (cdr (symbol-value node)))
8022                thread)
8023            (while nodes
8024              (if (and gnus-thread-expunge-below
8025                       (< (gnus-thread-total-score (car nodes))
8026                          gnus-thread-expunge-below))
8027                  (gnus-expunge-thread (pop nodes))
8028                (setq thread (pop nodes))
8029                (gnus-summary-limit-children thread))))))
8030      gnus-newsgroup-dependencies)
8031     ;; If this limitation resulted in an empty group, we might
8032     ;; pop the previous limit and use it instead.
8033     (when (and (not gnus-newsgroup-limit)
8034                show-if-empty)
8035       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8036     gnus-newsgroup-limit))
8037
8038 (defun gnus-summary-limit-children (thread)
8039   "Return 1 if this subthread is visible and 0 if it is not."
8040   ;; First we get the number of visible children to this thread.  This
8041   ;; is done by recursing down the thread using this function, so this
8042   ;; will really go down to a leaf article first, before slowly
8043   ;; working its way up towards the root.
8044   (when thread
8045     (let* ((max-lisp-eval-depth 5000)
8046            (children
8047            (if (cdr thread)
8048                (apply '+ (mapcar 'gnus-summary-limit-children
8049                                  (cdr thread)))
8050              0))
8051           (number (mail-header-number (car thread)))
8052           score)
8053       (if (and
8054            (not (memq number gnus-newsgroup-marked))
8055            (or
8056             ;; If this article is dormant and has absolutely no visible
8057             ;; children, then this article isn't visible.
8058             (and (memq number gnus-newsgroup-dormant)
8059                  (zerop children))
8060             ;; If this is "fetch-old-headered" and there is no
8061             ;; visible children, then we don't want this article.
8062             (and (or (eq gnus-fetch-old-headers 'some)
8063                      (numberp gnus-fetch-old-headers))
8064                  (gnus-summary-article-ancient-p number)
8065                  (zerop children))
8066             ;; If this is "fetch-old-headered" and `invisible', then
8067             ;; we don't want this article.
8068             (and (eq gnus-fetch-old-headers 'invisible)
8069                  (gnus-summary-article-ancient-p number))
8070             ;; If this is a sparsely inserted article with no children,
8071             ;; we don't want it.
8072             (and (eq gnus-build-sparse-threads 'some)
8073                  (gnus-summary-article-sparse-p number)
8074                  (zerop children))
8075             ;; If we use expunging, and this article is really
8076             ;; low-scored, then we don't want this article.
8077             (when (and gnus-summary-expunge-below
8078                        (< (setq score
8079                                 (or (cdr (assq number gnus-newsgroup-scored))
8080                                     gnus-summary-default-score))
8081                           gnus-summary-expunge-below))
8082               ;; We increase the expunge-tally here, but that has
8083               ;; nothing to do with the limits, really.
8084               (incf gnus-newsgroup-expunged-tally)
8085               ;; We also mark as read here, if that's wanted.
8086               (when (and gnus-summary-mark-below
8087                          (< score gnus-summary-mark-below))
8088                 (setq gnus-newsgroup-unreads
8089                       (delq number gnus-newsgroup-unreads))
8090                 (if gnus-newsgroup-auto-expire
8091                     (push number gnus-newsgroup-expirable)
8092                   (push (cons number gnus-low-score-mark)
8093                         gnus-newsgroup-reads)))
8094               t)
8095             ;; Do the `display' group parameter.
8096             (and gnus-newsgroup-display
8097                  (not (funcall gnus-newsgroup-display)))
8098             ;; Check NoCeM things.
8099             (when (and gnus-use-nocem
8100                        (gnus-nocem-unwanted-article-p
8101                         (mail-header-id (car thread))))
8102               (setq gnus-newsgroup-unreads
8103                     (delq number gnus-newsgroup-unreads))
8104               t)))
8105           ;; Nope, invisible article.
8106           0
8107         ;; Ok, this article is to be visible, so we add it to the limit
8108         ;; and return 1.
8109         (push number gnus-newsgroup-limit)
8110         1))))
8111
8112 (defun gnus-expunge-thread (thread)
8113   "Mark all articles in THREAD as read."
8114   (let* ((number (mail-header-number (car thread))))
8115     (incf gnus-newsgroup-expunged-tally)
8116     ;; We also mark as read here, if that's wanted.
8117     (setq gnus-newsgroup-unreads
8118           (delq number gnus-newsgroup-unreads))
8119     (if gnus-newsgroup-auto-expire
8120         (push number gnus-newsgroup-expirable)
8121       (push (cons number gnus-low-score-mark)
8122             gnus-newsgroup-reads)))
8123   ;; Go recursively through all subthreads.
8124   (mapcar 'gnus-expunge-thread (cdr thread)))
8125
8126 ;; Summary article oriented commands
8127
8128 (defun gnus-summary-refer-parent-article (n)
8129   "Refer parent article N times.
8130 If N is negative, go to ancestor -N instead.
8131 The difference between N and the number of articles fetched is returned."
8132   (interactive "p")
8133   (let ((skip 1)
8134         error header ref)
8135     (when (not (natnump n))
8136       (setq skip (abs n)
8137             n 1))
8138     (while (and (> n 0)
8139                 (not error))
8140       (setq header (gnus-summary-article-header))
8141       (if (and (eq (mail-header-number header)
8142                    (cdr gnus-article-current))
8143                (equal gnus-newsgroup-name
8144                       (car gnus-article-current)))
8145           ;; If we try to find the parent of the currently
8146           ;; displayed article, then we take a look at the actual
8147           ;; References header, since this is slightly more
8148           ;; reliable than the References field we got from the
8149           ;; server.
8150           (save-excursion
8151             (set-buffer gnus-original-article-buffer)
8152             (nnheader-narrow-to-headers)
8153             (unless (setq ref (message-fetch-field "references"))
8154               (when (setq ref (message-fetch-field "in-reply-to"))
8155                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8156             (widen))
8157         (setq ref
8158               ;; It's not the current article, so we take a bet on
8159               ;; the value we got from the server.
8160               (mail-header-references header)))
8161       (if (and ref
8162                (not (equal ref "")))
8163           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8164             (gnus-message 1 "Couldn't find parent"))
8165         (gnus-message 1 "No references in article %d"
8166                       (gnus-summary-article-number))
8167         (setq error t))
8168       (decf n))
8169     (gnus-summary-position-point)
8170     n))
8171
8172 (defun gnus-summary-refer-references ()
8173   "Fetch all articles mentioned in the References header.
8174 Return the number of articles fetched."
8175   (interactive)
8176   (let ((ref (mail-header-references (gnus-summary-article-header)))
8177         (current (gnus-summary-article-number))
8178         (n 0))
8179     (if (or (not ref)
8180             (equal ref ""))
8181         (error "No References in the current article")
8182       ;; For each Message-ID in the References header...
8183       (while (string-match "<[^>]*>" ref)
8184         (incf n)
8185         ;; ... fetch that article.
8186         (gnus-summary-refer-article
8187          (prog1 (match-string 0 ref)
8188            (setq ref (substring ref (match-end 0))))))
8189       (gnus-summary-goto-subject current)
8190       (gnus-summary-position-point)
8191       n)))
8192
8193 (defun gnus-summary-refer-thread (&optional limit)
8194   "Fetch all articles in the current thread.
8195 If LIMIT (the numerical prefix), fetch that many old headers instead
8196 of what's specified by the `gnus-refer-thread-limit' variable."
8197   (interactive "P")
8198   (let ((id (mail-header-id (gnus-summary-article-header)))
8199         (limit (if limit (prefix-numeric-value limit)
8200                  gnus-refer-thread-limit)))
8201     (unless (eq gnus-fetch-old-headers 'invisible)
8202       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8203       ;; Retrieve the headers and read them in.
8204       (if (eq (if (numberp limit)
8205                   (gnus-retrieve-headers
8206                    (list (min
8207                           (+ (mail-header-number
8208                               (gnus-summary-article-header))
8209                              limit)
8210                           gnus-newsgroup-end))
8211                    gnus-newsgroup-name (* limit 2))
8212                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8213                 ;; headers.
8214                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8215                                        gnus-newsgroup-name limit))
8216               'nov)
8217           (gnus-build-all-threads)
8218         (error "Can't fetch thread from back ends that don't support NOV"))
8219       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8220     (gnus-summary-limit-include-thread id)))
8221
8222 (defun gnus-summary-refer-article (message-id)
8223   "Fetch an article specified by MESSAGE-ID."
8224   (interactive "sMessage-ID: ")
8225   (when (and (stringp message-id)
8226              (not (zerop (length message-id))))
8227     (setq message-id (gnus-replace-in-string message-id " " ""))
8228     ;; Construct the correct Message-ID if necessary.
8229     ;; Suggested by tale@pawl.rpi.edu.
8230     (unless (string-match "^<" message-id)
8231       (setq message-id (concat "<" message-id)))
8232     (unless (string-match ">$" message-id)
8233       (setq message-id (concat message-id ">")))
8234     ;; People often post MIDs from URLs, so unhex it:
8235     (unless (string-match "@" message-id)
8236       (setq message-id (gnus-url-unhex-string message-id)))
8237     (let* ((header (gnus-id-to-header message-id))
8238            (sparse (and header
8239                         (gnus-summary-article-sparse-p
8240                          (mail-header-number header))
8241                         (memq (mail-header-number header)
8242                               gnus-newsgroup-limit)))
8243            number)
8244       (cond
8245        ;; If the article is present in the buffer we just go to it.
8246        ((and header
8247              (or (not (gnus-summary-article-sparse-p
8248                        (mail-header-number header)))
8249                  sparse))
8250         (prog1
8251             (gnus-summary-goto-article
8252              (mail-header-number header) nil t)
8253           (when sparse
8254             (gnus-summary-update-article (mail-header-number header)))))
8255        (t
8256         ;; We fetch the article.
8257         (catch 'found
8258           (dolist (gnus-override-method (gnus-refer-article-methods))
8259             (when (and (gnus-check-server gnus-override-method)
8260                        ;; Fetch the header,
8261                        (setq number (gnus-summary-insert-subject message-id)))
8262               ;; and display the article.
8263               (gnus-summary-select-article nil nil nil number)
8264               (throw 'found t)))
8265           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8266
8267 (defun gnus-refer-article-methods ()
8268   "Return a list of referable methods."
8269   (cond
8270    ;; No method, so we default to current and native.
8271    ((null gnus-refer-article-method)
8272     (list gnus-current-select-method gnus-select-method))
8273    ;; Current.
8274    ((eq 'current gnus-refer-article-method)
8275     (list gnus-current-select-method))
8276    ;; List of select methods.
8277    ((not (and (symbolp (car gnus-refer-article-method))
8278               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8279     (let (out)
8280       (dolist (method gnus-refer-article-method)
8281         (push (if (eq 'current method)
8282                   gnus-current-select-method
8283                 method)
8284               out))
8285       (nreverse out)))
8286    ;; One single select method.
8287    (t
8288     (list gnus-refer-article-method))))
8289
8290 (defun gnus-summary-edit-parameters ()
8291   "Edit the group parameters of the current group."
8292   (interactive)
8293   (gnus-group-edit-group gnus-newsgroup-name 'params))
8294
8295 (defun gnus-summary-customize-parameters ()
8296   "Customize the group parameters of the current group."
8297   (interactive)
8298   (gnus-group-customize gnus-newsgroup-name))
8299
8300 (defun gnus-summary-enter-digest-group (&optional force)
8301   "Enter an nndoc group based on the current article.
8302 If FORCE, force a digest interpretation.  If not, try
8303 to guess what the document format is."
8304   (interactive "P")
8305   (let ((conf gnus-current-window-configuration))
8306     (save-window-excursion
8307       (save-excursion
8308         (let (gnus-article-prepare-hook
8309               gnus-display-mime-function
8310               gnus-break-pages)
8311           (gnus-summary-select-article))))
8312     (setq gnus-current-window-configuration conf)
8313     (let* ((name (format "%s-%d"
8314                          (gnus-group-prefixed-name
8315                           gnus-newsgroup-name (list 'nndoc ""))
8316                          (save-excursion
8317                            (set-buffer gnus-summary-buffer)
8318                            gnus-current-article)))
8319            (ogroup gnus-newsgroup-name)
8320            (params (append (gnus-info-params (gnus-get-info ogroup))
8321                            (list (cons 'to-group ogroup))
8322                            (list (cons 'parent-group ogroup))
8323                            (list (cons 'save-article-group ogroup))))
8324            (case-fold-search t)
8325            (buf (current-buffer))
8326            dig to-address)
8327       (save-excursion
8328         (set-buffer gnus-original-article-buffer)
8329         ;; Have the digest group inherit the main mail address of
8330         ;; the parent article.
8331         (when (setq to-address (or (gnus-fetch-field "reply-to")
8332                                    (gnus-fetch-field "from")))
8333           (setq params (append
8334                         (list (cons 'to-address
8335                                     (funcall gnus-decode-encoded-word-function
8336                                              to-address))))))
8337         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8338         (insert-buffer-substring gnus-original-article-buffer)
8339         ;; Remove lines that may lead nndoc to misinterpret the
8340         ;; document type.
8341         (narrow-to-region
8342          (goto-char (point-min))
8343          (or (search-forward "\n\n" nil t) (point)))
8344         (goto-char (point-min))
8345         (delete-matching-lines "^Path:\\|^From ")
8346         (widen))
8347       (unwind-protect
8348           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8349                     (gnus-newsgroup-ephemeral-ignored-charsets
8350                      gnus-newsgroup-ignored-charsets))
8351                 (gnus-group-read-ephemeral-group
8352                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8353                               (nndoc-article-type
8354                                ,(if force 'mbox 'guess)))
8355                  t nil nil nil
8356                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8357                                                         "ADAPT")))))
8358               ;; Make all postings to this group go to the parent group.
8359               (nconc (gnus-info-params (gnus-get-info name))
8360                      params)
8361             ;; Couldn't select this doc group.
8362             (switch-to-buffer buf)
8363             (gnus-set-global-variables)
8364             (gnus-configure-windows 'summary)
8365             (gnus-message 3 "Article couldn't be entered?"))
8366         (kill-buffer dig)))))
8367
8368 (defun gnus-summary-read-document (n)
8369   "Open a new group based on the current article(s).
8370 This will allow you to read digests and other similar
8371 documents as newsgroups.
8372 Obeys the standard process/prefix convention."
8373   (interactive "P")
8374   (let* ((articles (gnus-summary-work-articles n))
8375          (ogroup gnus-newsgroup-name)
8376          (params (append (gnus-info-params (gnus-get-info ogroup))
8377                          (list (cons 'to-group ogroup))))
8378          article group egroup groups vgroup)
8379     (while (setq article (pop articles))
8380       (setq group (format "%s-%d" gnus-newsgroup-name article))
8381       (gnus-summary-remove-process-mark article)
8382       (when (gnus-summary-display-article article)
8383         (save-excursion
8384           (with-temp-buffer
8385             (insert-buffer-substring gnus-original-article-buffer)
8386             ;; Remove some headers that may lead nndoc to make
8387             ;; the wrong guess.
8388             (message-narrow-to-head)
8389             (goto-char (point-min))
8390             (delete-matching-lines "^Path:\\|^From ")
8391             (widen)
8392             (if (setq egroup
8393                       (gnus-group-read-ephemeral-group
8394                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8395                                      (nndoc-article-type guess))
8396                        t nil t))
8397                 (progn
8398             ;; Make all postings to this group go to the parent group.
8399                   (nconc (gnus-info-params (gnus-get-info egroup))
8400                          params)
8401                   (push egroup groups))
8402               ;; Couldn't select this doc group.
8403               (gnus-error 3 "Article couldn't be entered"))))))
8404     ;; Now we have selected all the documents.
8405     (cond
8406      ((not groups)
8407       (error "None of the articles could be interpreted as documents"))
8408      ((gnus-group-read-ephemeral-group
8409        (setq vgroup (format
8410                      "nnvirtual:%s-%s" gnus-newsgroup-name
8411                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8412        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8413        t
8414        (cons (current-buffer) 'summary)))
8415      (t
8416       (error "Couldn't select virtual nndoc group")))))
8417
8418 (defun gnus-summary-isearch-article (&optional regexp-p)
8419   "Do incremental search forward on the current article.
8420 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8421   (interactive "P")
8422   (gnus-summary-select-article)
8423   (gnus-configure-windows 'article)
8424   (gnus-eval-in-buffer-window gnus-article-buffer
8425     (save-restriction
8426       (widen)
8427       (isearch-forward regexp-p))))
8428
8429 (defun gnus-summary-search-article-forward (regexp &optional backward)
8430   "Search for an article containing REGEXP forward.
8431 If BACKWARD, search backward instead."
8432   (interactive
8433    (list (read-string
8434           (format "Search article %s (regexp%s): "
8435                   (if current-prefix-arg "backward" "forward")
8436                   (if gnus-last-search-regexp
8437                       (concat ", default " gnus-last-search-regexp)
8438                     "")))
8439          current-prefix-arg))
8440   (if (string-equal regexp "")
8441       (setq regexp (or gnus-last-search-regexp ""))
8442     (setq gnus-last-search-regexp regexp)
8443     (setq gnus-article-before-search gnus-current-article))
8444   ;; Intentionally set gnus-last-article.
8445   (setq gnus-last-article gnus-article-before-search)
8446   (let ((gnus-last-article gnus-last-article))
8447     (if (gnus-summary-search-article regexp backward)
8448         (gnus-summary-show-thread)
8449       (error "Search failed: \"%s\"" regexp))))
8450
8451 (defun gnus-summary-search-article-backward (regexp)
8452   "Search for an article containing REGEXP backward."
8453   (interactive
8454    (list (read-string
8455           (format "Search article backward (regexp%s): "
8456                   (if gnus-last-search-regexp
8457                       (concat ", default " gnus-last-search-regexp)
8458                     "")))))
8459   (gnus-summary-search-article-forward regexp 'backward))
8460
8461 (defun gnus-summary-search-article (regexp &optional backward)
8462   "Search for an article containing REGEXP.
8463 Optional argument BACKWARD means do search for backward.
8464 `gnus-select-article-hook' is not called during the search."
8465   ;; We have to require this here to make sure that the following
8466   ;; dynamic binding isn't shadowed by autoloading.
8467   (require 'gnus-async)
8468   (require 'gnus-art)
8469   (let ((gnus-select-article-hook nil)  ;Disable hook.
8470         (gnus-article-prepare-hook nil)
8471         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8472         (gnus-use-article-prefetch nil)
8473         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8474         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8475         (gnus-visual nil)
8476         (gnus-keep-backlog nil)
8477         (gnus-break-pages nil)
8478         (gnus-summary-display-arrow nil)
8479         (gnus-updated-mode-lines nil)
8480         (gnus-auto-center-summary nil)
8481         (sum (current-buffer))
8482         (gnus-display-mime-function nil)
8483         (found nil)
8484         point)
8485     (gnus-save-hidden-threads
8486       (gnus-summary-select-article)
8487       (set-buffer gnus-article-buffer)
8488       (goto-char (window-point (get-buffer-window (current-buffer))))
8489       (when backward
8490         (forward-line -1))
8491       (while (not found)
8492         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8493         (if (if backward
8494                 (re-search-backward regexp nil t)
8495               (re-search-forward regexp nil t))
8496             ;; We found the regexp.
8497             (progn
8498               (setq found 'found)
8499               (beginning-of-line)
8500               (set-window-start
8501                (get-buffer-window (current-buffer))
8502                (point))
8503               (forward-line 1)
8504               (set-window-point
8505                (get-buffer-window (current-buffer))
8506                (point))
8507               (set-buffer sum)
8508               (setq point (point)))
8509           ;; We didn't find it, so we go to the next article.
8510           (set-buffer sum)
8511           (setq found 'not)
8512           (while (eq found 'not)
8513             (if (not (if backward (gnus-summary-find-prev)
8514                        (gnus-summary-find-next)))
8515                 ;; No more articles.
8516                 (setq found t)
8517               ;; Select the next article and adjust point.
8518               (unless (gnus-summary-article-sparse-p
8519                        (gnus-summary-article-number))
8520                 (setq found nil)
8521                 (gnus-summary-select-article)
8522                 (set-buffer gnus-article-buffer)
8523                 (widen)
8524                 (goto-char (if backward (point-max) (point-min))))))))
8525       (gnus-message 7 ""))
8526     ;; Return whether we found the regexp.
8527     (when (eq found 'found)
8528       (goto-char point)
8529       (gnus-summary-show-thread)
8530       (gnus-summary-goto-subject gnus-current-article)
8531       (gnus-summary-position-point)
8532       t)))
8533
8534 (defun gnus-find-matching-articles (header regexp)
8535   "Return a list of all articles that match REGEXP on HEADER.
8536 This search includes all articles in the current group that Gnus has
8537 fetched headers for, whether they are displayed or not."
8538   (let ((articles nil)
8539         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8540         (case-fold-search t))
8541     (dolist (header gnus-newsgroup-headers)
8542       (when (string-match regexp (funcall func header))
8543         (push (mail-header-number header) articles)))
8544     (nreverse articles)))
8545
8546 (defun gnus-summary-find-matching (header regexp &optional backward unread
8547                                           not-case-fold not-matching)
8548   "Return a list of all articles that match REGEXP on HEADER.
8549 The search stars on the current article and goes forwards unless
8550 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8551 If UNREAD is non-nil, only unread articles will
8552 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8553 in the comparisons. If NOT-MATCHING, return a list of all articles that
8554 not match REGEXP on HEADER."
8555   (let ((case-fold-search (not not-case-fold))
8556         articles d func)
8557     (if (consp header)
8558         (if (eq (car header) 'extra)
8559             (setq func
8560                   `(lambda (h)
8561                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8562                          "")))
8563           (error "%s is an invalid header" header))
8564       (unless (fboundp (intern (concat "mail-header-" header)))
8565         (error "%s is not a valid header" header))
8566       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8567     (dolist (d (if (eq backward 'all)
8568                    gnus-newsgroup-data
8569                  (gnus-data-find-list
8570                   (gnus-summary-article-number)
8571                   (gnus-data-list backward))))
8572       (when (and (or (not unread)       ; We want all articles...
8573                      (gnus-data-unread-p d)) ; Or just unreads.
8574                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8575                  (if not-matching
8576                      (not (string-match
8577                            regexp
8578                            (funcall func (gnus-data-header d))))
8579                    (string-match regexp
8580                                  (funcall func (gnus-data-header d)))))
8581         (push (gnus-data-number d) articles))) ; Success!
8582     (nreverse articles)))
8583
8584 (defun gnus-summary-execute-command (header regexp command &optional backward)
8585   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8586 If HEADER is an empty string (or nil), the match is done on the entire
8587 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8588   (interactive
8589    (list (let ((completion-ignore-case t))
8590            (completing-read
8591             "Header name: "
8592             (mapcar (lambda (header) (list (format "%s" header)))
8593                     (append
8594                      '("Number" "Subject" "From" "Lines" "Date"
8595                        "Message-ID" "Xref" "References" "Body")
8596                      gnus-extra-headers))
8597             nil 'require-match))
8598          (read-string "Regexp: ")
8599          (read-key-sequence "Command: ")
8600          current-prefix-arg))
8601   (when (equal header "Body")
8602     (setq header ""))
8603   ;; Hidden thread subtrees must be searched as well.
8604   (gnus-summary-show-all-threads)
8605   ;; We don't want to change current point nor window configuration.
8606   (save-excursion
8607     (save-window-excursion
8608       (let (gnus-visual
8609             gnus-treat-strip-trailing-blank-lines
8610             gnus-treat-strip-leading-blank-lines
8611             gnus-treat-strip-multiple-blank-lines
8612             gnus-treat-hide-boring-headers
8613             gnus-treat-fold-newsgroups
8614             gnus-article-prepare-hook)
8615         (gnus-message 6 "Executing %s..." (key-description command))
8616         ;; We'd like to execute COMMAND interactively so as to give arguments.
8617         (gnus-execute header regexp
8618                       `(call-interactively ',(key-binding command))
8619                       backward)
8620         (gnus-message 6 "Executing %s...done" (key-description command))))))
8621
8622 (defun gnus-summary-beginning-of-article ()
8623   "Scroll the article back to the beginning."
8624   (interactive)
8625   (gnus-summary-select-article)
8626   (gnus-configure-windows 'article)
8627   (gnus-eval-in-buffer-window gnus-article-buffer
8628     (widen)
8629     (goto-char (point-min))
8630     (when gnus-break-pages
8631       (gnus-narrow-to-page))))
8632
8633 (defun gnus-summary-end-of-article ()
8634   "Scroll to the end of the article."
8635   (interactive)
8636   (gnus-summary-select-article)
8637   (gnus-configure-windows 'article)
8638   (gnus-eval-in-buffer-window gnus-article-buffer
8639     (widen)
8640     (goto-char (point-max))
8641     (recenter -3)
8642     (when gnus-break-pages
8643       (when (re-search-backward page-delimiter nil t)
8644         (narrow-to-region (match-end 0) (point-max)))
8645       (gnus-narrow-to-page))))
8646
8647 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8648   "Truncate to LEN and quote all \"(\"'s in STRING."
8649   (gnus-replace-in-string (if (and len (> (length string) len))
8650                               (substring string 0 len)
8651                             string)
8652                           "[()]" "\\\\\\&"))
8653
8654 (defun gnus-summary-print-article (&optional filename n)
8655   "Generate and print a PostScript image of the process-marked (mail) articles.
8656
8657 If used interactively, print the current article if none are
8658 process-marked.  With prefix arg, prompt the user for the name of the
8659 file to save in.
8660
8661 When used from Lisp, accept two optional args FILENAME and N.  N means
8662 to print the next N articles.  If N is negative, print the N previous
8663 articles.  If N is nil and articles have been marked with the process
8664 mark, print these instead.
8665
8666 If the optional first argument FILENAME is nil, send the image to the
8667 printer.  If FILENAME is a string, save the PostScript image in a file with
8668 that name.  If FILENAME is a number, prompt the user for the name of the file
8669 to save in."
8670   (interactive (list (ps-print-preprint current-prefix-arg)))
8671   (dolist (article (gnus-summary-work-articles n))
8672     (gnus-summary-select-article nil nil 'pseudo article)
8673     (gnus-eval-in-buffer-window gnus-article-buffer
8674       (gnus-print-buffer))
8675     (gnus-summary-remove-process-mark article))
8676   (ps-despool filename))
8677
8678 (defun gnus-print-buffer ()
8679   (let ((buffer (generate-new-buffer " *print*")))
8680     (unwind-protect
8681         (progn
8682           (copy-to-buffer buffer (point-min) (point-max))
8683           (set-buffer buffer)
8684           (gnus-remove-text-with-property 'gnus-decoration)
8685           (when (gnus-visual-p 'article-highlight 'highlight)
8686             ;; Copy-to-buffer doesn't copy overlay.  So redo
8687             ;; highlight.
8688             (let ((gnus-article-buffer buffer))
8689               (gnus-article-highlight-citation t)
8690               (gnus-article-highlight-signature)
8691               (gnus-article-emphasize)
8692               (gnus-article-delete-invisible-text)))
8693           (let ((ps-left-header
8694                  (list
8695                   (concat "("
8696                           (gnus-summary-print-truncate-and-quote
8697                            (mail-header-subject gnus-current-headers)
8698                            66) ")")
8699                   (concat "("
8700                           (gnus-summary-print-truncate-and-quote
8701                            (mail-header-from gnus-current-headers)
8702                            45) ")")))
8703                 (ps-right-header
8704                  (list
8705                   "/pagenumberstring load"
8706                   (concat "("
8707                           (mail-header-date gnus-current-headers) ")"))))
8708             (gnus-run-hooks 'gnus-ps-print-hook)
8709             (save-excursion
8710               (if window-system
8711                   (ps-spool-buffer-with-faces)
8712                 (ps-spool-buffer)))))
8713       (kill-buffer buffer))))
8714
8715 (defun gnus-summary-show-article (&optional arg)
8716   "Force redisplaying of the current article.
8717 If ARG (the prefix) is a number, show the article with the charset
8718 defined in `gnus-summary-show-article-charset-alist', or the charset
8719 input.
8720 If ARG (the prefix) is non-nil and not a number, show the raw article
8721 without any article massaging functions being run.  Normally, the key
8722 strokes are `C-u g'."
8723   (interactive "P")
8724   (cond
8725    ((numberp arg)
8726     (gnus-summary-show-article t)
8727     (let ((gnus-newsgroup-charset
8728            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8729                (mm-read-coding-system
8730                 "View as charset: " ;; actually it is coding system.
8731                 (save-excursion
8732                   (set-buffer gnus-article-buffer)
8733                   (mm-detect-coding-region (point) (point-max))))))
8734           (gnus-newsgroup-ignored-charsets 'gnus-all))
8735       (gnus-summary-select-article nil 'force)
8736       (let ((deps gnus-newsgroup-dependencies)
8737             head header lines)
8738         (save-excursion
8739           (set-buffer gnus-original-article-buffer)
8740           (save-restriction
8741             (message-narrow-to-head)
8742             (setq head (buffer-string))
8743             (goto-char (point-min))
8744             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8745               (goto-char (point-max))
8746               (widen)
8747               (setq lines (1- (count-lines (point) (point-max))))))
8748           (with-temp-buffer
8749             (insert (format "211 %d Article retrieved.\n"
8750                             (cdr gnus-article-current)))
8751             (insert head)
8752             (if lines (insert (format "Lines: %d\n" lines)))
8753             (insert ".\n")
8754             (let ((nntp-server-buffer (current-buffer)))
8755               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8756         (gnus-data-set-header
8757          (gnus-data-find (cdr gnus-article-current))
8758          header)
8759         (gnus-summary-update-article-line
8760          (cdr gnus-article-current) header)
8761         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8762           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8763    ((not arg)
8764     ;; Select the article the normal way.
8765     (gnus-summary-select-article nil 'force))
8766    (t
8767     ;; We have to require this here to make sure that the following
8768     ;; dynamic binding isn't shadowed by autoloading.
8769     (require 'gnus-async)
8770     (require 'gnus-art)
8771     ;; Bind the article treatment functions to nil.
8772     (let ((gnus-have-all-headers t)
8773           gnus-article-prepare-hook
8774           gnus-article-decode-hook
8775           gnus-display-mime-function
8776           gnus-break-pages)
8777       ;; Destroy any MIME parts.
8778       (when (gnus-buffer-live-p gnus-article-buffer)
8779         (save-excursion
8780           (set-buffer gnus-article-buffer)
8781           (mm-destroy-parts gnus-article-mime-handles)
8782           ;; Set it to nil for safety reason.
8783           (setq gnus-article-mime-handle-alist nil)
8784           (setq gnus-article-mime-handles nil)))
8785       (gnus-summary-select-article nil 'force))))
8786   (gnus-summary-goto-subject gnus-current-article)
8787   (gnus-summary-position-point))
8788
8789 (defun gnus-summary-show-raw-article ()
8790   "Show the raw article without any article massaging functions being run."
8791   (interactive)
8792   (gnus-summary-show-article t))
8793
8794 (defun gnus-summary-verbose-headers (&optional arg)
8795   "Toggle permanent full header display.
8796 If ARG is a positive number, turn header display on.
8797 If ARG is a negative number, turn header display off."
8798   (interactive "P")
8799   (setq gnus-show-all-headers
8800         (cond ((or (not (numberp arg))
8801                    (zerop arg))
8802                (not gnus-show-all-headers))
8803               ((natnump arg)
8804                t)))
8805   (gnus-summary-show-article))
8806
8807 (defun gnus-summary-toggle-header (&optional arg)
8808   "Show the headers if they are hidden, or hide them if they are shown.
8809 If ARG is a positive number, show the entire header.
8810 If ARG is a negative number, hide the unwanted header lines."
8811   (interactive "P")
8812   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8813                      (get-buffer-window gnus-article-buffer t))))
8814     (with-current-buffer gnus-article-buffer
8815       (widen)
8816       (article-narrow-to-head)
8817       (let* ((buffer-read-only nil)
8818              (inhibit-point-motion-hooks t)
8819              (hidden (if (numberp arg)
8820                          (>= arg 0)
8821                        (gnus-article-hidden-text-p 'headers)))
8822              s e)
8823         (delete-region (point-min) (point-max))
8824         (with-current-buffer gnus-original-article-buffer
8825           (goto-char (setq s (point-min)))
8826           (setq e (if (search-forward "\n\n" nil t)
8827                       (1- (point))
8828                     (point-max))))
8829         (insert-buffer-substring gnus-original-article-buffer s e)
8830         (run-hooks 'gnus-article-decode-hook)
8831         (if hidden
8832             (let ((gnus-treat-hide-headers nil)
8833                   (gnus-treat-hide-boring-headers nil))
8834               (gnus-delete-wash-type 'headers)
8835               (gnus-treat-article 'head))
8836           (gnus-treat-article 'head))
8837         (widen)
8838         (if window
8839             (set-window-start window (goto-char (point-min))))
8840         (if gnus-break-pages
8841             (gnus-narrow-to-page)
8842           (when (gnus-visual-p 'page-marker)
8843             (let ((buffer-read-only nil))
8844               (gnus-remove-text-with-property 'gnus-prev)
8845               (gnus-remove-text-with-property 'gnus-next))))
8846         (gnus-set-mode-line 'article)))))
8847
8848 (defun gnus-summary-show-all-headers ()
8849   "Make all header lines visible."
8850   (interactive)
8851   (gnus-summary-toggle-header 1))
8852
8853 (defun gnus-summary-caesar-message (&optional arg)
8854   "Caesar rotate the current article by 13.
8855 The numerical prefix specifies how many places to rotate each letter
8856 forward."
8857   (interactive "P")
8858   (gnus-summary-select-article)
8859   (let ((mail-header-separator ""))
8860     (gnus-eval-in-buffer-window gnus-article-buffer
8861       (save-restriction
8862         (widen)
8863         (let ((start (window-start))
8864               buffer-read-only)
8865           (message-caesar-buffer-body arg)
8866           (set-window-start (get-buffer-window (current-buffer)) start))))))
8867
8868 (autoload 'unmorse-region "morse"
8869   "Convert morse coded text in region to ordinary ASCII text."
8870   t)
8871
8872 (defun gnus-summary-morse-message (&optional arg)
8873   "Morse decode the current article."
8874   (interactive "P")
8875   (gnus-summary-select-article)
8876   (let ((mail-header-separator ""))
8877     (gnus-eval-in-buffer-window gnus-article-buffer
8878       (save-excursion
8879         (save-restriction
8880           (widen)
8881           (let ((pos (window-start))
8882                 buffer-read-only)
8883             (goto-char (point-min))
8884             (when (message-goto-body)
8885               (gnus-narrow-to-body))
8886             (goto-char (point-min))
8887             (while (re-search-forward "·" (point-max) t)
8888               (replace-match "."))
8889             (unmorse-region (point-min) (point-max))
8890             (widen)
8891             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8892
8893 (defun gnus-summary-stop-page-breaking ()
8894   "Stop page breaking in the current article."
8895   (interactive)
8896   (gnus-summary-select-article)
8897   (gnus-eval-in-buffer-window gnus-article-buffer
8898     (widen)
8899     (when (gnus-visual-p 'page-marker)
8900       (let ((buffer-read-only nil))
8901         (gnus-remove-text-with-property 'gnus-prev)
8902         (gnus-remove-text-with-property 'gnus-next))
8903       (setq gnus-page-broken nil))))
8904
8905 (defun gnus-summary-move-article (&optional n to-newsgroup
8906                                             select-method action)
8907   "Move the current article to a different newsgroup.
8908 If N is a positive number, move the N next articles.
8909 If N is a negative number, move the N previous articles.
8910 If N is nil and any articles have been marked with the process mark,
8911 move those articles instead.
8912 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8913 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8914 re-spool using this method.
8915
8916 When called interactively with TO-NEWSGROUP being nil, the value of
8917 the variable `gnus-move-split-methods' is used for finding a default
8918 for the target newsgroup.
8919
8920 For this function to work, both the current newsgroup and the
8921 newsgroup that you want to move to have to support the `request-move'
8922 and `request-accept' functions.
8923
8924 ACTION can be either `move' (the default), `crosspost' or `copy'."
8925   (interactive "P")
8926   (unless action
8927     (setq action 'move))
8928   ;; Check whether the source group supports the required functions.
8929   (cond ((and (eq action 'move)
8930               (not (gnus-check-backend-function
8931                     'request-move-article gnus-newsgroup-name)))
8932          (error "The current group does not support article moving"))
8933         ((and (eq action 'crosspost)
8934               (not (gnus-check-backend-function
8935                     'request-replace-article gnus-newsgroup-name)))
8936          (error "The current group does not support article editing")))
8937   (let ((articles (gnus-summary-work-articles n))
8938         (prefix (if (gnus-check-backend-function
8939                      'request-move-article gnus-newsgroup-name)
8940                     (funcall gnus-move-group-prefix-function
8941                              gnus-newsgroup-name)
8942                   ""))
8943         (names '((move "Move" "Moving")
8944                  (copy "Copy" "Copying")
8945                  (crosspost "Crosspost" "Crossposting")))
8946         (copy-buf (save-excursion
8947                     (nnheader-set-temp-buffer " *copy article*")))
8948         art-group to-method new-xref article to-groups)
8949     (unless (assq action names)
8950       (error "Unknown action %s" action))
8951     ;; Read the newsgroup name.
8952     (when (and (not to-newsgroup)
8953                (not select-method))
8954       (if (and gnus-move-split-methods
8955                (not
8956                 (and (memq gnus-current-article articles)
8957                      (gnus-buffer-live-p gnus-original-article-buffer))))
8958           ;; When `gnus-move-split-methods' is non-nil, we have to
8959           ;; select an article to give `gnus-read-move-group-name' an
8960           ;; opportunity to suggest an appropriate default.  However,
8961           ;; we needn't render or mark the article.
8962           (let ((gnus-display-mime-function nil)
8963                 (gnus-article-prepare-hook nil)
8964                 (gnus-mark-article-hook nil))
8965             (gnus-summary-select-article nil nil nil (car articles))))
8966       (setq to-newsgroup
8967             (gnus-read-move-group-name
8968              (cadr (assq action names))
8969              (symbol-value (intern (format "gnus-current-%s-group" action)))
8970              articles prefix))
8971       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8972     (setq to-method (or select-method
8973                         (gnus-server-to-method
8974                          (gnus-group-method to-newsgroup))))
8975     ;; Check the method we are to move this article to...
8976     (unless (gnus-check-backend-function
8977              'request-accept-article (car to-method))
8978       (error "%s does not support article copying" (car to-method)))
8979     (unless (gnus-check-server to-method)
8980       (error "Can't open server %s" (car to-method)))
8981     (gnus-message 6 "%s to %s: %s..."
8982                   (caddr (assq action names))
8983                   (or (car select-method) to-newsgroup) articles)
8984     (while articles
8985       (setq article (pop articles))
8986       (setq
8987        art-group
8988        (cond
8989         ;; Move the article.
8990         ((eq action 'move)
8991          ;; Remove this article from future suppression.
8992          (gnus-dup-unsuppress-article article)
8993          (gnus-request-move-article
8994           article                       ; Article to move
8995           gnus-newsgroup-name           ; From newsgroup
8996           (nth 1 (gnus-find-method-for-group
8997                   gnus-newsgroup-name)) ; Server
8998           (list 'gnus-request-accept-article
8999                 to-newsgroup (list 'quote select-method)
9000                 (not articles) t)       ; Accept form
9001           (not articles)))              ; Only save nov last time
9002         ;; Copy the article.
9003         ((eq action 'copy)
9004          (save-excursion
9005            (set-buffer copy-buf)
9006            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9007              (gnus-request-accept-article
9008               to-newsgroup select-method (not articles) t))))
9009         ;; Crosspost the article.
9010         ((eq action 'crosspost)
9011          (let ((xref (message-tokenize-header
9012                       (mail-header-xref (gnus-summary-article-header article))
9013                       " ")))
9014            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9015                                   ":" (number-to-string article)))
9016            (unless xref
9017              (setq xref (list (system-name))))
9018            (setq new-xref
9019                  (concat
9020                   (mapconcat 'identity
9021                              (delete "Xref:" (delete new-xref xref))
9022                              " ")
9023                   " " new-xref))
9024            (save-excursion
9025              (set-buffer copy-buf)
9026              ;; First put the article in the destination group.
9027              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9028              (when (consp (setq art-group
9029                                 (gnus-request-accept-article
9030                                  to-newsgroup select-method (not articles))))
9031                (setq new-xref (concat new-xref " " (car art-group)
9032                                       ":"
9033                                       (number-to-string (cdr art-group))))
9034                ;; Now we have the new Xrefs header, so we insert
9035                ;; it and replace the new article.
9036                (nnheader-replace-header "Xref" new-xref)
9037                (gnus-request-replace-article
9038                 (cdr art-group) to-newsgroup (current-buffer))
9039                art-group))))))
9040       (cond
9041        ((not art-group)
9042         (gnus-message 1 "Couldn't %s article %s: %s"
9043                       (cadr (assq action names)) article
9044                       (nnheader-get-report (car to-method))))
9045        ((eq art-group 'junk)
9046         (when (eq action 'move)
9047           (gnus-summary-mark-article article gnus-canceled-mark)
9048           (gnus-message 4 "Deleted article %s" article)
9049           ;; run the delete hook
9050           (run-hook-with-args 'gnus-summary-article-delete-hook
9051                               action
9052                               (gnus-data-header
9053                                (assoc article (gnus-data-list nil)))
9054                               gnus-newsgroup-name nil
9055                               select-method)))
9056        (t
9057         (let* ((pto-group (gnus-group-prefixed-name
9058                            (car art-group) to-method))
9059                (info (gnus-get-info pto-group))
9060                (to-group (gnus-info-group info))
9061                to-marks)
9062           ;; Update the group that has been moved to.
9063           (when (and info
9064                      (memq action '(move copy)))
9065             (unless (member to-group to-groups)
9066               (push to-group to-groups))
9067
9068             (unless (memq article gnus-newsgroup-unreads)
9069               (push 'read to-marks)
9070               (gnus-info-set-read
9071                info (gnus-add-to-range (gnus-info-read info)
9072                                        (list (cdr art-group)))))
9073
9074             ;; See whether the article is to be put in the cache.
9075             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9076                              gnus-article-mark-lists
9077                            (delete '(expirable . expire)
9078                                    (copy-sequence gnus-article-mark-lists))))
9079                   (to-article (cdr art-group)))
9080
9081               ;; Enter the article into the cache in the new group,
9082               ;; if that is required.
9083               (when gnus-use-cache
9084                 (gnus-cache-possibly-enter-article
9085                  to-group to-article
9086                  (memq article gnus-newsgroup-marked)
9087                  (memq article gnus-newsgroup-dormant)
9088                  (memq article gnus-newsgroup-unreads)))
9089
9090               (when gnus-preserve-marks
9091                 ;; Copy any marks over to the new group.
9092                 (when (and (equal to-group gnus-newsgroup-name)
9093                            (not (memq article gnus-newsgroup-unreads)))
9094                   ;; Mark this article as read in this group.
9095                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9096                   (setcdr (gnus-active to-group) to-article)
9097                   (setcdr gnus-newsgroup-active to-article))
9098
9099                 (while marks
9100                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9101                     (when (memq article (symbol-value
9102                                          (intern (format "gnus-newsgroup-%s"
9103                                                          (caar marks)))))
9104                       (push (cdar marks) to-marks)
9105                       ;; If the other group is the same as this group,
9106                       ;; then we have to add the mark to the list.
9107                       (when (equal to-group gnus-newsgroup-name)
9108                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9109                              (cons to-article
9110                                    (symbol-value
9111                                     (intern (format "gnus-newsgroup-%s"
9112                                                     (caar marks)))))))
9113                       ;; Copy the marks to other group.
9114                       (gnus-add-marked-articles
9115                        to-group (cdar marks) (list to-article) info)))
9116                   (setq marks (cdr marks)))
9117
9118                 (gnus-request-set-mark
9119                  to-group (list (list (list to-article) 'add to-marks))))
9120
9121               (gnus-dribble-enter
9122                (concat "(gnus-group-set-info '"
9123                        (gnus-prin1-to-string (gnus-get-info to-group))
9124                        ")"))))
9125
9126           ;; Update the Xref header in this article to point to
9127           ;; the new crossposted article we have just created.
9128           (when (eq action 'crosspost)
9129             (save-excursion
9130               (set-buffer copy-buf)
9131               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9132               (nnheader-replace-header "Xref" new-xref)
9133               (gnus-request-replace-article
9134                article gnus-newsgroup-name (current-buffer))))
9135
9136           ;; run the move/copy/crosspost/respool hook
9137           (run-hook-with-args 'gnus-summary-article-move-hook
9138                               action
9139                               (gnus-data-header
9140                                (assoc article (gnus-data-list nil)))
9141                               gnus-newsgroup-name
9142                               to-newsgroup
9143                               select-method))
9144
9145         ;;;!!!Why is this necessary?
9146         (set-buffer gnus-summary-buffer)
9147         
9148         (gnus-summary-goto-subject article)
9149         (when (eq action 'move)
9150           (gnus-summary-mark-article article gnus-canceled-mark))))
9151       (gnus-summary-remove-process-mark article))
9152     ;; Re-activate all groups that have been moved to.
9153     (save-excursion
9154       (set-buffer gnus-group-buffer)
9155       (let ((gnus-group-marked to-groups))
9156         (gnus-group-get-new-news-this-group nil t)))
9157
9158     (gnus-kill-buffer copy-buf)
9159     (gnus-summary-position-point)
9160     (gnus-set-mode-line 'summary)))
9161
9162 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9163   "Move the current article to a different newsgroup.
9164 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9165 When called interactively, if TO-NEWSGROUP is nil, use the value of
9166 the variable `gnus-move-split-methods' for finding a default target
9167 newsgroup.
9168 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9169 re-spool using this method."
9170   (interactive "P")
9171   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9172
9173 (defun gnus-summary-crosspost-article (&optional n)
9174   "Crosspost the current article to some other group."
9175   (interactive "P")
9176   (gnus-summary-move-article n nil nil 'crosspost))
9177
9178 (defcustom gnus-summary-respool-default-method nil
9179   "Default method type for respooling an article.
9180 If nil, use to the current newsgroup method."
9181   :type 'symbol
9182   :group 'gnus-summary-mail)
9183
9184 (defcustom gnus-summary-display-while-building nil
9185   "If non-nil, show and update the summary buffer as it's being built.
9186 If the value is t, update the buffer after every line is inserted.  If
9187 the value is an integer (N), update the display every N lines."
9188   :group 'gnus-thread
9189   :type '(choice (const :tag "off" nil)
9190                  number
9191                  (const :tag "frequently" t)))
9192
9193 (defun gnus-summary-respool-article (&optional n method)
9194   "Respool the current article.
9195 The article will be squeezed through the mail spooling process again,
9196 which means that it will be put in some mail newsgroup or other
9197 depending on `nnmail-split-methods'.
9198 If N is a positive number, respool the N next articles.
9199 If N is a negative number, respool the N previous articles.
9200 If N is nil and any articles have been marked with the process mark,
9201 respool those articles instead.
9202
9203 Respooling can be done both from mail groups and \"real\" newsgroups.
9204 In the former case, the articles in question will be moved from the
9205 current group into whatever groups they are destined to.  In the
9206 latter case, they will be copied into the relevant groups."
9207   (interactive
9208    (list current-prefix-arg
9209          (let* ((methods (gnus-methods-using 'respool))
9210                 (methname
9211                  (symbol-name (or gnus-summary-respool-default-method
9212                                   (car (gnus-find-method-for-group
9213                                         gnus-newsgroup-name)))))
9214                 (method
9215                  (gnus-completing-read-with-default
9216                   methname "What backend do you want to use when respooling?"
9217                   methods nil t nil 'gnus-mail-method-history))
9218                 ms)
9219            (cond
9220             ((zerop (length (setq ms (gnus-servers-using-backend
9221                                       (intern method)))))
9222              (list (intern method) ""))
9223             ((= 1 (length ms))
9224              (car ms))
9225             (t
9226              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9227                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9228                            ms-alist))))))))
9229   (unless method
9230     (error "No method given for respooling"))
9231   (if (assoc (symbol-name
9232               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9233              (gnus-methods-using 'respool))
9234       (gnus-summary-move-article n nil method)
9235     (gnus-summary-copy-article n nil method)))
9236
9237 (defun gnus-summary-import-article (file &optional edit)
9238   "Import an arbitrary file into a mail newsgroup."
9239   (interactive "fImport file: \nP")
9240   (let ((group gnus-newsgroup-name)
9241         (now (current-time))
9242         atts lines group-art)
9243     (unless (gnus-check-backend-function 'request-accept-article group)
9244       (error "%s does not support article importing" group))
9245     (or (file-readable-p file)
9246         (not (file-regular-p file))
9247         (error "Can't read %s" file))
9248     (save-excursion
9249       (set-buffer (gnus-get-buffer-create " *import file*"))
9250       (erase-buffer)
9251       (nnheader-insert-file-contents file)
9252       (goto-char (point-min))
9253       (if (nnheader-article-p)
9254           (save-restriction
9255             (goto-char (point-min))
9256             (search-forward "\n\n" nil t)
9257             (narrow-to-region (point-min) (1- (point)))
9258             (goto-char (point-min))
9259             (unless (re-search-forward "^date:" nil t)
9260               (goto-char (point-max))
9261               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9262        ;; This doesn't look like an article, so we fudge some headers.
9263         (setq atts (file-attributes file)
9264               lines (count-lines (point-min) (point-max)))
9265         (insert "From: " (read-string "From: ") "\n"
9266                 "Subject: " (read-string "Subject: ") "\n"
9267                 "Date: " (message-make-date (nth 5 atts)) "\n"
9268                 "Message-ID: " (message-make-message-id) "\n"
9269                 "Lines: " (int-to-string lines) "\n"
9270                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9271       (setq group-art (gnus-request-accept-article group nil t))
9272       (kill-buffer (current-buffer)))
9273     (setq gnus-newsgroup-active (gnus-activate-group group))
9274     (forward-line 1)
9275     (gnus-summary-goto-article (cdr group-art) nil t)
9276     (when edit
9277       (gnus-summary-edit-article))))
9278
9279 (defun gnus-summary-create-article ()
9280   "Create an article in a mail newsgroup."
9281   (interactive)
9282   (let ((group gnus-newsgroup-name)
9283         (now (current-time))
9284         group-art)
9285     (unless (gnus-check-backend-function 'request-accept-article group)
9286       (error "%s does not support article importing" group))
9287     (save-excursion
9288       (set-buffer (gnus-get-buffer-create " *import file*"))
9289       (erase-buffer)
9290       (goto-char (point-min))
9291       ;; This doesn't look like an article, so we fudge some headers.
9292       (insert "From: " (read-string "From: ") "\n"
9293               "Subject: " (read-string "Subject: ") "\n"
9294               "Date: " (message-make-date now) "\n"
9295               "Message-ID: " (message-make-message-id) "\n")
9296       (setq group-art (gnus-request-accept-article group nil t))
9297       (kill-buffer (current-buffer)))
9298     (setq gnus-newsgroup-active (gnus-activate-group group))
9299     (forward-line 1)
9300     (gnus-summary-goto-article (cdr group-art) nil t)
9301     (gnus-summary-edit-article)))
9302
9303 (defun gnus-summary-article-posted-p ()
9304   "Say whether the current (mail) article is available from news as well.
9305 This will be the case if the article has both been mailed and posted."
9306   (interactive)
9307   (let ((id (mail-header-references (gnus-summary-article-header)))
9308         (gnus-override-method (car (gnus-refer-article-methods))))
9309     (if (gnus-request-head id "")
9310         (gnus-message 2 "The current message was found on %s"
9311                       gnus-override-method)
9312       (gnus-message 2 "The current message couldn't be found on %s"
9313                     gnus-override-method)
9314       nil)))
9315
9316 (defun gnus-summary-expire-articles (&optional now)
9317   "Expire all articles that are marked as expirable in the current group."
9318   (interactive)
9319   (when (and (not gnus-group-is-exiting-without-update-p)
9320              (gnus-check-backend-function
9321               'request-expire-articles gnus-newsgroup-name))
9322     ;; This backend supports expiry.
9323     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9324            (expirable (if total
9325                           (progn
9326                             ;; We need to update the info for
9327                             ;; this group for `gnus-list-of-read-articles'
9328                             ;; to give us the right answer.
9329                             (gnus-run-hooks 'gnus-exit-group-hook)
9330                             (gnus-summary-update-info)
9331                             (gnus-list-of-read-articles gnus-newsgroup-name))
9332                         (setq gnus-newsgroup-expirable
9333                               (sort gnus-newsgroup-expirable '<))))
9334            (expiry-wait (if now 'immediate
9335                           (gnus-group-find-parameter
9336                            gnus-newsgroup-name 'expiry-wait)))
9337            (nnmail-expiry-target
9338             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9339                 nnmail-expiry-target))
9340            es)
9341       (when expirable
9342         ;; There are expirable articles in this group, so we run them
9343         ;; through the expiry process.
9344         (gnus-message 6 "Expiring articles...")
9345         (unless (gnus-check-group gnus-newsgroup-name)
9346           (error "Can't open server for %s" gnus-newsgroup-name))
9347         ;; The list of articles that weren't expired is returned.
9348         (save-excursion
9349           (if expiry-wait
9350               (let ((nnmail-expiry-wait-function nil)
9351                     (nnmail-expiry-wait expiry-wait))
9352                 (setq es (gnus-request-expire-articles
9353                           expirable gnus-newsgroup-name)))
9354             (setq es (gnus-request-expire-articles
9355                       expirable gnus-newsgroup-name)))
9356           (unless total
9357             (setq gnus-newsgroup-expirable es))
9358           ;; We go through the old list of expirable, and mark all
9359           ;; really expired articles as nonexistent.
9360           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9361             (let ((gnus-use-cache nil))
9362               (dolist (article expirable)
9363                 (when (and (not (memq article es))
9364                            (gnus-data-find article))
9365                   (gnus-summary-mark-article article gnus-canceled-mark)
9366                   (run-hook-with-args 'gnus-summary-article-expire-hook
9367                                       'delete
9368                                       (gnus-data-header
9369                                        (assoc article (gnus-data-list nil)))
9370                                       gnus-newsgroup-name
9371                                       nil
9372                                       nil))))))
9373         (gnus-message 6 "Expiring articles...done")))))
9374
9375 (defun gnus-summary-expire-articles-now ()
9376   "Expunge all expirable articles in the current group.
9377 This means that *all* articles that are marked as expirable will be
9378 deleted forever, right now."
9379   (interactive)
9380   (or gnus-expert-user
9381       (gnus-yes-or-no-p
9382        "Are you really, really, really sure you want to delete all these messages? ")
9383       (error "Phew!"))
9384   (gnus-summary-expire-articles t))
9385
9386 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9387 (defun gnus-summary-delete-article (&optional n)
9388   "Delete the N next (mail) articles.
9389 This command actually deletes articles.  This is not a marking
9390 command.  The article will disappear forever from your life, never to
9391 return.
9392
9393 If N is negative, delete backwards.
9394 If N is nil and articles have been marked with the process mark,
9395 delete these instead.
9396
9397 If `gnus-novice-user' is non-nil you will be asked for
9398 confirmation before the articles are deleted."
9399   (interactive "P")
9400   (unless (gnus-check-backend-function 'request-expire-articles
9401                                        gnus-newsgroup-name)
9402     (error "The current newsgroup does not support article deletion"))
9403   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9404     (error "Couldn't open server"))
9405   ;; Compute the list of articles to delete.
9406   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9407         (nnmail-expiry-target 'delete)
9408         not-deleted)
9409     (if (and gnus-novice-user
9410              (not (gnus-yes-or-no-p
9411                    (format "Do you really want to delete %s forever? "
9412                            (if (> (length articles) 1)
9413                                (format "these %s articles" (length articles))
9414                              "this article")))))
9415         ()
9416       ;; Delete the articles.
9417       (setq not-deleted (gnus-request-expire-articles
9418                          articles gnus-newsgroup-name 'force))
9419       (while articles
9420         (gnus-summary-remove-process-mark (car articles))
9421         ;; The backend might not have been able to delete the article
9422         ;; after all.
9423         (unless (memq (car articles) not-deleted)
9424           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9425         (let* ((article (car articles))
9426                (id (mail-header-id (gnus-data-header
9427                                     (assoc article (gnus-data-list nil))))))
9428           (run-hook-with-args 'gnus-summary-article-delete-hook
9429                               'delete id gnus-newsgroup-name nil
9430                               nil))
9431         (setq articles (cdr articles)))
9432       (when not-deleted
9433         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9434     (gnus-summary-position-point)
9435     (gnus-set-mode-line 'summary)
9436     not-deleted))
9437
9438 (defun gnus-summary-edit-article (&optional arg)
9439   "Edit the current article.
9440 This will have permanent effect only in mail groups.
9441 If ARG is nil, edit the decoded articles.
9442 If ARG is 1, edit the raw articles.
9443 If ARG is 2, edit the raw articles even in read-only groups.
9444 If ARG is 3, edit the articles with the current handles.
9445 Otherwise, allow editing of articles even in read-only
9446 groups."
9447   (interactive "P")
9448   (let (force raw current-handles)
9449     (cond
9450      ((null arg))
9451      ((eq arg 1)
9452       (setq raw t))
9453      ((eq arg 2)
9454       (setq raw t
9455             force t))
9456      ((eq arg 3)
9457       (setq current-handles
9458             (and (gnus-buffer-live-p gnus-article-buffer)
9459                  (with-current-buffer gnus-article-buffer
9460                    (prog1
9461                        gnus-article-mime-handles
9462                      (setq gnus-article-mime-handles nil))))))
9463      (t
9464       (setq force t)))
9465     (when (and raw (not force)
9466                (member gnus-newsgroup-name '("nndraft:delayed"
9467                                              "nndraft:drafts"
9468                                              "nndraft:queue")))
9469       (error "Can't edit the raw article in group %s"
9470              gnus-newsgroup-name))
9471     (save-excursion
9472       (set-buffer gnus-summary-buffer)
9473       (let ((mail-parse-charset gnus-newsgroup-charset)
9474             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9475         (gnus-set-global-variables)
9476         (when (and (not force)
9477                    (gnus-group-read-only-p))
9478           (error "The current newsgroup does not support article editing"))
9479         (gnus-summary-show-article t)
9480         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9481           (with-current-buffer gnus-article-buffer
9482             (mm-enable-multibyte)))
9483         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9484             (setq raw t))
9485         (gnus-article-edit-article
9486          (if raw 'ignore
9487            `(lambda ()
9488               (let ((mbl mml-buffer-list))
9489                 (setq mml-buffer-list nil)
9490                 (mime-to-mml ,'current-handles)
9491                 (let ((mbl1 mml-buffer-list))
9492                   (setq mml-buffer-list mbl)
9493                   (set (make-local-variable 'mml-buffer-list) mbl1))
9494                 (gnus-make-local-hook 'kill-buffer-hook)
9495                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9496          `(lambda (no-highlight)
9497             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9498                   (message-options message-options)
9499                   (message-options-set-recipient)
9500                   (mail-parse-ignored-charsets
9501                    ',gnus-newsgroup-ignored-charsets))
9502               ,(if (not raw) '(progn
9503                                 (mml-to-mime)
9504                                 (mml-destroy-buffers)
9505                                 (remove-hook 'kill-buffer-hook
9506                                              'mml-destroy-buffers t)
9507                                 (kill-local-variable 'mml-buffer-list)))
9508               (gnus-summary-edit-article-done
9509                ,(or (mail-header-references gnus-current-headers) "")
9510                ,(gnus-group-read-only-p)
9511                ,gnus-summary-buffer no-highlight))))))))
9512
9513 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9514
9515 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9516                                                  no-highlight)
9517   "Make edits to the current article permanent."
9518   (interactive)
9519   (save-excursion
9520    ;; The buffer restriction contains the entire article if it exists.
9521     (when (article-goto-body)
9522       (let ((lines (count-lines (point) (point-max)))
9523             (length (- (point-max) (point)))
9524             (case-fold-search t)
9525             (body (copy-marker (point))))
9526         (goto-char (point-min))
9527         (when (re-search-forward "^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
9532                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9533           (delete-region (match-beginning 1) (match-end 1))
9534           (insert (number-to-string length)))
9535         (goto-char (point-min))
9536         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9537           (delete-region (match-beginning 1) (match-end 1))
9538           (insert (number-to-string lines))))))
9539   ;; Replace the article.
9540   (let ((buf (current-buffer)))
9541     (with-temp-buffer
9542       (insert-buffer-substring buf)
9543
9544       (if (and (not read-only)
9545                (not (gnus-request-replace-article
9546                      (cdr gnus-article-current) (car gnus-article-current)
9547                      (current-buffer) t)))
9548           (error "Couldn't replace article")
9549         ;; Update the summary buffer.
9550         (if (and references
9551                  (equal (message-tokenize-header references " ")
9552                         (message-tokenize-header
9553                          (or (message-fetch-field "references") "") " ")))
9554             ;; We only have to update this line.
9555             (save-excursion
9556               (save-restriction
9557                 (message-narrow-to-head)
9558                 (let ((head (buffer-string))
9559                       header)
9560                   (with-temp-buffer
9561                     (insert (format "211 %d Article retrieved.\n"
9562                                     (cdr gnus-article-current)))
9563                     (insert head)
9564                     (insert ".\n")
9565                     (let ((nntp-server-buffer (current-buffer)))
9566                       (setq header (car (gnus-get-newsgroup-headers
9567                                          nil t))))
9568                     (save-excursion
9569                       (set-buffer gnus-summary-buffer)
9570                       (gnus-data-set-header
9571                        (gnus-data-find (cdr gnus-article-current))
9572                        header)
9573                       (gnus-summary-update-article-line
9574                        (cdr gnus-article-current) header)
9575                       (if (gnus-summary-goto-subject
9576                            (cdr gnus-article-current) nil t)
9577                           (gnus-summary-update-secondary-mark
9578                            (cdr gnus-article-current))))))))
9579           ;; Update threads.
9580           (set-buffer (or buffer gnus-summary-buffer))
9581           (gnus-summary-update-article (cdr gnus-article-current))
9582           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9583               (gnus-summary-update-secondary-mark
9584                (cdr gnus-article-current))))
9585         ;; Prettify the article buffer again.
9586         (unless no-highlight
9587           (save-excursion
9588             (set-buffer gnus-article-buffer)
9589             ;;;!!! Fix this -- article should be rehighlighted.
9590             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9591             (set-buffer gnus-original-article-buffer)
9592             (gnus-request-article
9593              (cdr gnus-article-current)
9594              (car gnus-article-current) (current-buffer))))
9595         ;; Prettify the summary buffer line.
9596         (when (gnus-visual-p 'summary-highlight 'highlight)
9597           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9598
9599 (defun gnus-summary-edit-wash (key)
9600   "Perform editing command KEY in the article buffer."
9601   (interactive
9602    (list
9603     (progn
9604       (message "%s" (concat (this-command-keys) "- "))
9605       (read-char))))
9606   (message "")
9607   (gnus-summary-edit-article)
9608   (execute-kbd-macro (concat (this-command-keys) key))
9609   (gnus-article-edit-done))
9610
9611 ;;; Respooling
9612
9613 (defun gnus-summary-respool-query (&optional silent trace)
9614   "Query where the respool algorithm would put this article."
9615   (interactive)
9616   (let (gnus-mark-article-hook)
9617     (gnus-summary-select-article)
9618     (save-excursion
9619       (set-buffer gnus-original-article-buffer)
9620       (let ((groups (nnmail-article-group 'identity trace)))
9621         (unless silent
9622           (if groups
9623               (message "This message would go to %s"
9624                        (mapconcat 'car groups ", "))
9625             (message "This message would go to no groups"))
9626           groups)))))
9627
9628 (defun gnus-summary-respool-trace ()
9629   "Trace where the respool algorithm would put this article.
9630 Display a buffer showing all fancy splitting patterns which matched."
9631   (interactive)
9632   (gnus-summary-respool-query nil t))
9633
9634 ;; Summary marking commands.
9635
9636 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9637   "Mark articles which has the same subject as read, and then select the next.
9638 If UNMARK is positive, remove any kind of mark.
9639 If UNMARK is negative, tick articles."
9640   (interactive "P")
9641   (when unmark
9642     (setq unmark (prefix-numeric-value unmark)))
9643   (let ((count
9644          (gnus-summary-mark-same-subject
9645           (gnus-summary-article-subject) unmark)))
9646     ;; Select next unread article.  If auto-select-same mode, should
9647     ;; select the first unread article.
9648     (gnus-summary-next-article t (and gnus-auto-select-same
9649                                       (gnus-summary-article-subject)))
9650     (gnus-message 7 "%d article%s marked as %s"
9651                   count (if (= count 1) " is" "s are")
9652                   (if unmark "unread" "read"))))
9653
9654 (defun gnus-summary-kill-same-subject (&optional unmark)
9655   "Mark articles which has the same subject as read.
9656 If UNMARK is positive, remove any kind of mark.
9657 If UNMARK is negative, tick articles."
9658   (interactive "P")
9659   (when unmark
9660     (setq unmark (prefix-numeric-value unmark)))
9661   (let ((count
9662          (gnus-summary-mark-same-subject
9663           (gnus-summary-article-subject) unmark)))
9664     ;; If marked as read, go to next unread subject.
9665     (when (null unmark)
9666       ;; Go to next unread subject.
9667       (gnus-summary-next-subject 1 t))
9668     (gnus-message 7 "%d articles are marked as %s"
9669                   count (if unmark "unread" "read"))))
9670
9671 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9672   "Mark articles with same SUBJECT as read, and return marked number.
9673 If optional argument UNMARK is positive, remove any kinds of marks.
9674 If optional argument UNMARK is negative, mark articles as unread instead."
9675   (let ((count 1))
9676     (save-excursion
9677       (cond
9678        ((null unmark)                   ; Mark as read.
9679         (while (and
9680                 (progn
9681                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9682                   (gnus-summary-show-thread) t)
9683                 (gnus-summary-find-subject subject))
9684           (setq count (1+ count))))
9685        ((> unmark 0)                    ; Tick.
9686         (while (and
9687                 (progn
9688                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9689                   (gnus-summary-show-thread) t)
9690                 (gnus-summary-find-subject subject))
9691           (setq count (1+ count))))
9692        (t                               ; Mark as unread.
9693         (while (and
9694                 (progn
9695                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9696                   (gnus-summary-show-thread) t)
9697                 (gnus-summary-find-subject subject))
9698           (setq count (1+ count)))))
9699       (gnus-set-mode-line 'summary)
9700       ;; Return the number of marked articles.
9701       count)))
9702
9703 (defun gnus-summary-mark-as-processable (n &optional unmark)
9704   "Set the process mark on the next N articles.
9705 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9706 the process mark instead.  The difference between N and the actual
9707 number of articles marked is returned."
9708   (interactive "P")
9709   (if (and (null n) (gnus-region-active-p))
9710       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9711     (setq n (prefix-numeric-value n))
9712     (let ((backward (< n 0))
9713           (n (abs n)))
9714       (while (and
9715               (> n 0)
9716               (if unmark
9717                   (gnus-summary-remove-process-mark
9718                    (gnus-summary-article-number))
9719                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9720               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9721         (setq n (1- n)))
9722       (when (/= 0 n)
9723         (gnus-message 7 "No more articles"))
9724       (gnus-summary-recenter)
9725       (gnus-summary-position-point)
9726       n)))
9727
9728 (defun gnus-summary-unmark-as-processable (n)
9729   "Remove the process mark from the next N articles.
9730 If N is negative, unmark backward instead.  The difference between N and
9731 the actual number of articles unmarked is returned."
9732   (interactive "P")
9733   (gnus-summary-mark-as-processable n t))
9734
9735 (defun gnus-summary-unmark-all-processable ()
9736   "Remove the process mark from all articles."
9737   (interactive)
9738   (save-excursion
9739     (while gnus-newsgroup-processable
9740       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9741   (gnus-summary-position-point))
9742
9743 (defun gnus-summary-add-mark (article type)
9744   "Mark ARTICLE with a mark of TYPE."
9745   (let ((vtype (car (assq type gnus-article-mark-lists)))
9746         var)
9747     (if (not vtype)
9748         (error "No such mark type: %s" type)
9749       (setq var (intern (format "gnus-newsgroup-%s" type)))
9750       (set var (cons article (symbol-value var)))
9751       (if (memq type '(processable cached replied forwarded recent saved))
9752           (gnus-summary-update-secondary-mark article)
9753         ;;; !!! This is bogus.  We should find out what primary
9754         ;;; !!! mark we want to set.
9755         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9756
9757 (defun gnus-summary-mark-as-expirable (n)
9758   "Mark N articles forward as expirable.
9759 If N is negative, mark backward instead.  The difference between N and
9760 the actual number of articles marked is returned."
9761   (interactive "p")
9762   (gnus-summary-mark-forward n gnus-expirable-mark))
9763
9764 (defun gnus-summary-mark-as-spam (n)
9765   "Mark N articles forward as spam.
9766 If N is negative, mark backward instead.  The difference between N and
9767 the actual number of articles marked is returned."
9768   (interactive "p")
9769   (gnus-summary-mark-forward n gnus-spam-mark))
9770
9771 (defun gnus-summary-mark-article-as-replied (article)
9772   "Mark ARTICLE as replied to and update the summary line.
9773 ARTICLE can also be a list of articles."
9774   (interactive (list (gnus-summary-article-number)))
9775   (let ((articles (if (listp article) article (list article))))
9776     (dolist (article articles)
9777       (unless (numberp article)
9778         (error "%s is not a number" article))
9779       (push article gnus-newsgroup-replied)
9780       (let ((buffer-read-only nil))
9781         (when (gnus-summary-goto-subject article nil t)
9782           (gnus-summary-update-secondary-mark article))))))
9783
9784 (defun gnus-summary-mark-article-as-forwarded (article)
9785   "Mark ARTICLE as forwarded and update the summary line.
9786 ARTICLE can also be a list of articles."
9787   (let ((articles (if (listp article) article (list article))))
9788     (dolist (article articles)
9789       (push article gnus-newsgroup-forwarded)
9790       (let ((buffer-read-only nil))
9791         (when (gnus-summary-goto-subject article nil t)
9792           (gnus-summary-update-secondary-mark article))))))
9793
9794 (defun gnus-summary-set-bookmark (article)
9795   "Set a bookmark in current article."
9796   (interactive (list (gnus-summary-article-number)))
9797   (when (or (not (get-buffer gnus-article-buffer))
9798             (not gnus-current-article)
9799             (not gnus-article-current)
9800             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9801     (error "No current article selected"))
9802   ;; Remove old bookmark, if one exists.
9803   (gnus-pull article gnus-newsgroup-bookmarks)
9804   ;; Set the new bookmark, which is on the form
9805   ;; (article-number . line-number-in-body).
9806   (push
9807    (cons article
9808          (save-excursion
9809            (set-buffer gnus-article-buffer)
9810            (count-lines
9811             (min (point)
9812                  (save-excursion
9813                    (article-goto-body)
9814                    (point)))
9815             (point))))
9816    gnus-newsgroup-bookmarks)
9817   (gnus-message 6 "A bookmark has been added to the current article."))
9818
9819 (defun gnus-summary-remove-bookmark (article)
9820   "Remove the bookmark from the current article."
9821   (interactive (list (gnus-summary-article-number)))
9822   ;; Remove old bookmark, if one exists.
9823   (if (not (assq article gnus-newsgroup-bookmarks))
9824       (gnus-message 6 "No bookmark in current article.")
9825     (gnus-pull article gnus-newsgroup-bookmarks)
9826     (gnus-message 6 "Removed bookmark.")))
9827
9828 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9829 (defun gnus-summary-mark-as-dormant (n)
9830   "Mark N articles forward as dormant.
9831 If N is negative, mark backward instead.  The difference between N and
9832 the actual number of articles marked is returned."
9833   (interactive "p")
9834   (gnus-summary-mark-forward n gnus-dormant-mark))
9835
9836 (defun gnus-summary-set-process-mark (article)
9837   "Set the process mark on ARTICLE and update the summary line."
9838   (setq gnus-newsgroup-processable
9839         (cons article
9840               (delq article gnus-newsgroup-processable)))
9841   (when (gnus-summary-goto-subject article)
9842     (gnus-summary-show-thread)
9843     (gnus-summary-goto-subject article)
9844     (gnus-summary-update-secondary-mark article)))
9845
9846 (defun gnus-summary-remove-process-mark (article)
9847   "Remove the process mark from ARTICLE and update the summary line."
9848   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9849   (when (gnus-summary-goto-subject article)
9850     (gnus-summary-show-thread)
9851     (gnus-summary-goto-subject article)
9852     (gnus-summary-update-secondary-mark article)))
9853
9854 (defun gnus-summary-set-saved-mark (article)
9855   "Set the process mark on ARTICLE and update the summary line."
9856   (push article gnus-newsgroup-saved)
9857   (when (gnus-summary-goto-subject article)
9858     (gnus-summary-update-secondary-mark article)))
9859
9860 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9861   "Mark N articles as read forwards.
9862 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9863 The difference between N and the actual number of articles marked is
9864 returned.
9865 If NO-EXPIRE, auto-expiry will be inhibited."
9866   (interactive "p")
9867   (gnus-summary-show-thread)
9868   (let ((backward (< n 0))
9869         (gnus-summary-goto-unread
9870          (and gnus-summary-goto-unread
9871               (not (eq gnus-summary-goto-unread 'never))
9872               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9873                                     gnus-ticked-mark gnus-dormant-mark)))))
9874         (n (abs n))
9875         (mark (or mark gnus-del-mark)))
9876     (while (and (> n 0)
9877                 (gnus-summary-mark-article nil mark no-expire)
9878                 (zerop (gnus-summary-next-subject
9879                         (if backward -1 1)
9880                         (and gnus-summary-goto-unread
9881                              (not (eq gnus-summary-goto-unread 'never)))
9882                         t)))
9883       (setq n (1- n)))
9884     (when (/= 0 n)
9885       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9886     (gnus-summary-recenter)
9887     (gnus-summary-position-point)
9888     (gnus-set-mode-line 'summary)
9889     n))
9890
9891 (defun gnus-summary-mark-article-as-read (mark)
9892   "Mark the current article quickly as read with MARK."
9893   (let ((article (gnus-summary-article-number)))
9894     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9895     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9896     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9897     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9898     (push (cons article mark) gnus-newsgroup-reads)
9899     ;; Possibly remove from cache, if that is used.
9900     (when gnus-use-cache
9901       (gnus-cache-enter-remove-article article))
9902     ;; Allow the backend to change the mark.
9903     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9904     ;; Check for auto-expiry.
9905     (when (and gnus-newsgroup-auto-expire
9906                (memq mark gnus-auto-expirable-marks))
9907       (setq mark gnus-expirable-mark)
9908       ;; Let the backend know about the mark change.
9909       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9910       (push article gnus-newsgroup-expirable))
9911     ;; Set the mark in the buffer.
9912     (gnus-summary-update-mark mark 'unread)
9913     t))
9914
9915 (defun gnus-summary-mark-article-as-unread (mark)
9916   "Mark the current article quickly as unread with MARK."
9917   (let* ((article (gnus-summary-article-number))
9918          (old-mark (gnus-summary-article-mark article)))
9919     ;; Allow the backend to change the mark.
9920     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9921     (if (eq mark old-mark)
9922         t
9923       (if (<= article 0)
9924           (progn
9925             (gnus-error 1 "Can't mark negative article numbers")
9926             nil)
9927         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9928         (setq gnus-newsgroup-spam-marked
9929               (delq article gnus-newsgroup-spam-marked))
9930         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9931         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9932         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9933         (cond ((= mark gnus-ticked-mark)
9934                (setq gnus-newsgroup-marked
9935                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9936                                               article)))
9937               ((= mark gnus-spam-mark)
9938                (setq gnus-newsgroup-spam-marked
9939                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9940                                               article)))
9941               ((= mark gnus-dormant-mark)
9942                (setq gnus-newsgroup-dormant
9943                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9944                                               article)))
9945               (t
9946                (setq gnus-newsgroup-unreads
9947                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9948                                               article))))
9949         (gnus-pull article gnus-newsgroup-reads)
9950
9951         ;; See whether the article is to be put in the cache.
9952         (and gnus-use-cache
9953              (vectorp (gnus-summary-article-header article))
9954              (save-excursion
9955                (gnus-cache-possibly-enter-article
9956                 gnus-newsgroup-name article
9957                 (= mark gnus-ticked-mark)
9958                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9959
9960         ;; Fix the mark.
9961         (gnus-summary-update-mark mark 'unread)
9962         t))))
9963
9964 (defun gnus-summary-mark-article (&optional article mark no-expire)
9965   "Mark ARTICLE with MARK.  MARK can be any character.
9966 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9967 `??' (dormant) and `?E' (expirable).
9968 If MARK is nil, then the default character `?r' is used.
9969 If ARTICLE is nil, then the article on the current line will be
9970 marked.
9971 If NO-EXPIRE, auto-expiry will be inhibited."
9972   ;; The mark might be a string.
9973   (when (stringp mark)
9974     (setq mark (aref mark 0)))
9975   ;; If no mark is given, then we check auto-expiring.
9976   (when (null mark)
9977     (setq mark gnus-del-mark))
9978   (when (and (not no-expire)
9979              gnus-newsgroup-auto-expire
9980              (memq mark gnus-auto-expirable-marks))
9981     (setq mark gnus-expirable-mark))
9982   (let ((article (or article (gnus-summary-article-number)))
9983         (old-mark (gnus-summary-article-mark article)))
9984     ;; Allow the backend to change the mark.
9985     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9986     (if (eq mark old-mark)
9987         t
9988       (unless article
9989         (error "No article on current line"))
9990       (if (not (if (or (= mark gnus-unread-mark)
9991                        (= mark gnus-ticked-mark)
9992                        (= mark gnus-spam-mark)
9993                        (= mark gnus-dormant-mark))
9994                    (gnus-mark-article-as-unread article mark)
9995                  (gnus-mark-article-as-read article mark)))
9996           t
9997         ;; See whether the article is to be put in the cache.
9998         (and gnus-use-cache
9999              (not (= mark gnus-canceled-mark))
10000              (vectorp (gnus-summary-article-header article))
10001              (save-excursion
10002                (gnus-cache-possibly-enter-article
10003                 gnus-newsgroup-name article
10004                 (= mark gnus-ticked-mark)
10005                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10006
10007         (when (gnus-summary-goto-subject article nil t)
10008           (let ((buffer-read-only nil))
10009             (gnus-summary-show-thread)
10010             ;; Fix the mark.
10011             (gnus-summary-update-mark mark 'unread)
10012             t))))))
10013
10014 (defun gnus-summary-update-secondary-mark (article)
10015   "Update the secondary (read, process, cache) mark."
10016   (gnus-summary-update-mark
10017    (cond ((memq article gnus-newsgroup-processable)
10018           gnus-process-mark)
10019          ((memq article gnus-newsgroup-cached)
10020           gnus-cached-mark)
10021          ((memq article gnus-newsgroup-replied)
10022           gnus-replied-mark)
10023          ((memq article gnus-newsgroup-forwarded)
10024           gnus-forwarded-mark)
10025          ((memq article gnus-newsgroup-saved)
10026           gnus-saved-mark)
10027          ((memq article gnus-newsgroup-recent)
10028           gnus-recent-mark)
10029          ((memq article gnus-newsgroup-unseen)
10030           gnus-unseen-mark)
10031          (t gnus-no-mark))
10032    'replied)
10033   (when (gnus-visual-p 'summary-highlight 'highlight)
10034     (gnus-run-hooks 'gnus-summary-update-hook))
10035   t)
10036
10037 (defun gnus-summary-update-download-mark (article)
10038   "Update the download mark."
10039   (gnus-summary-update-mark
10040    (cond ((memq article gnus-newsgroup-undownloaded)
10041           gnus-undownloaded-mark)
10042          (gnus-newsgroup-agentized
10043           gnus-downloaded-mark)
10044          (t
10045           gnus-no-mark))
10046    'download)
10047   (gnus-summary-update-line t)
10048   t)
10049
10050 (defun gnus-summary-update-mark (mark type)
10051   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10052         (buffer-read-only nil))
10053     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10054     (when forward
10055       (when (looking-at "\r")
10056         (incf forward))
10057       (when (<= (+ forward (point)) (point-max))
10058         ;; Go to the right position on the line.
10059         (goto-char (+ forward (point)))
10060         ;; Replace the old mark with the new mark.
10061         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10062         ;; Optionally update the marks by some user rule.
10063         (when (eq type 'unread)
10064           (gnus-data-set-mark
10065            (gnus-data-find (gnus-summary-article-number)) mark)
10066           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10067
10068 (defun gnus-mark-article-as-read (article &optional mark)
10069   "Enter ARTICLE in the pertinent lists and remove it from others."
10070   ;; Make the article expirable.
10071   (let ((mark (or mark gnus-del-mark)))
10072     (setq gnus-newsgroup-expirable
10073           (if (= mark gnus-expirable-mark)
10074               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10075             (delq article gnus-newsgroup-expirable)))
10076     ;; Remove from unread and marked lists.
10077     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10078     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10079     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10080     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10081     (push (cons article mark) gnus-newsgroup-reads)
10082     ;; Possibly remove from cache, if that is used.
10083     (when gnus-use-cache
10084       (gnus-cache-enter-remove-article article))
10085     t))
10086
10087 (defun gnus-mark-article-as-unread (article &optional mark)
10088   "Enter ARTICLE in the pertinent lists and remove it from others."
10089   (let ((mark (or mark gnus-ticked-mark)))
10090     (if (<= article 0)
10091         (progn
10092           (gnus-error 1 "Can't mark negative article numbers")
10093           nil)
10094       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10095             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10096             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10097             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10098             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10099
10100       ;; Unsuppress duplicates?
10101       (when gnus-suppress-duplicates
10102         (gnus-dup-unsuppress-article article))
10103
10104       (cond ((= mark gnus-ticked-mark)
10105              (setq gnus-newsgroup-marked
10106                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10107             ((= mark gnus-spam-mark)
10108              (setq gnus-newsgroup-spam-marked
10109                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10110                                             article)))
10111             ((= mark gnus-dormant-mark)
10112              (setq gnus-newsgroup-dormant
10113                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10114             (t
10115              (setq gnus-newsgroup-unreads
10116                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10117       (gnus-pull article gnus-newsgroup-reads)
10118       t)))
10119
10120 (defalias 'gnus-summary-mark-as-unread-forward
10121   'gnus-summary-tick-article-forward)
10122 (make-obsolete 'gnus-summary-mark-as-unread-forward
10123                'gnus-summary-tick-article-forward)
10124 (defun gnus-summary-tick-article-forward (n)
10125   "Tick N articles forwards.
10126 If N is negative, tick backwards instead.
10127 The difference between N and the number of articles ticked is returned."
10128   (interactive "p")
10129   (gnus-summary-mark-forward n gnus-ticked-mark))
10130
10131 (defalias 'gnus-summary-mark-as-unread-backward
10132   'gnus-summary-tick-article-backward)
10133 (make-obsolete 'gnus-summary-mark-as-unread-backward
10134                'gnus-summary-tick-article-backward)
10135 (defun gnus-summary-tick-article-backward (n)
10136   "Tick N articles backwards.
10137 The difference between N and the number of articles ticked is returned."
10138   (interactive "p")
10139   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10140
10141 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10142 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10143 (defun gnus-summary-tick-article (&optional article clear-mark)
10144   "Mark current article as unread.
10145 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10146 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10147   (interactive)
10148   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10149                                        gnus-ticked-mark)))
10150
10151 (defun gnus-summary-mark-as-read-forward (n)
10152   "Mark N articles as read forwards.
10153 If N is negative, mark backwards instead.
10154 The difference between N and the actual number of articles marked is
10155 returned."
10156   (interactive "p")
10157   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10158
10159 (defun gnus-summary-mark-as-read-backward (n)
10160   "Mark the N articles as read backwards.
10161 The difference between N and the actual number of articles marked is
10162 returned."
10163   (interactive "p")
10164   (gnus-summary-mark-forward
10165    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10166
10167 (defun gnus-summary-mark-as-read (&optional article mark)
10168   "Mark current article as read.
10169 ARTICLE specifies the article to be marked as read.
10170 MARK specifies a string to be inserted at the beginning of the line."
10171   (gnus-summary-mark-article article mark))
10172
10173 (defun gnus-summary-clear-mark-forward (n)
10174   "Clear marks from N articles forward.
10175 If N is negative, clear backward instead.
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-clear-mark-backward (n)
10181   "Clear marks from N articles backward.
10182 The difference between N and the number of marks cleared is returned."
10183   (interactive "p")
10184   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10185
10186 (defun gnus-summary-mark-unread-as-read ()
10187   "Intended to be used by `gnus-summary-mark-article-hook'."
10188   (when (memq gnus-current-article gnus-newsgroup-unreads)
10189     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10190
10191 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10192   "Intended to be used by `gnus-summary-mark-article-hook'."
10193   (let ((mark (gnus-summary-article-mark)))
10194     (when (or (gnus-unread-mark-p mark)
10195               (gnus-read-mark-p mark))
10196       (gnus-summary-mark-article gnus-current-article
10197                                  (or new-mark gnus-read-mark)))))
10198
10199 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10200   "Intended to be used by `gnus-summary-mark-article-hook'."
10201   (let ((mark (gnus-summary-article-mark)))
10202     (when (or (gnus-unread-mark-p mark)
10203               (gnus-read-mark-p mark))
10204       (gnus-summary-mark-article (gnus-summary-article-number)
10205                                  (or new-mark gnus-read-mark)))))
10206
10207 (defun gnus-summary-mark-unread-as-ticked ()
10208   "Intended to be used by `gnus-summary-mark-article-hook'."
10209   (when (memq gnus-current-article gnus-newsgroup-unreads)
10210     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10211
10212 (defun gnus-summary-mark-region-as-read (point mark all)
10213   "Mark all unread articles between point and mark as read.
10214 If given a prefix, mark all articles between point and mark as read,
10215 even ticked and dormant ones."
10216   (interactive "r\nP")
10217   (save-excursion
10218     (let (article)
10219       (goto-char point)
10220       (beginning-of-line)
10221       (while (and
10222               (< (point) mark)
10223               (progn
10224                 (when (or all
10225                           (memq (setq article (gnus-summary-article-number))
10226                                 gnus-newsgroup-unreads))
10227                   (gnus-summary-mark-article article gnus-del-mark))
10228                 t)
10229               (gnus-summary-find-next))))))
10230
10231 (defun gnus-summary-mark-below (score mark)
10232   "Mark articles with score less than SCORE with MARK."
10233   (interactive "P\ncMark: ")
10234   (setq score (if score
10235                   (prefix-numeric-value score)
10236                 (or gnus-summary-default-score 0)))
10237   (save-excursion
10238     (set-buffer gnus-summary-buffer)
10239     (goto-char (point-min))
10240     (while
10241         (progn
10242           (and (< (gnus-summary-article-score) score)
10243                (gnus-summary-mark-article nil mark))
10244           (gnus-summary-find-next)))))
10245
10246 (defun gnus-summary-kill-below (&optional score)
10247   "Mark articles with score below SCORE as read."
10248   (interactive "P")
10249   (gnus-summary-mark-below score gnus-killed-mark))
10250
10251 (defun gnus-summary-clear-above (&optional score)
10252   "Clear all marks from articles with score above SCORE."
10253   (interactive "P")
10254   (gnus-summary-mark-above score gnus-unread-mark))
10255
10256 (defun gnus-summary-tick-above (&optional score)
10257   "Tick all articles with score above SCORE."
10258   (interactive "P")
10259   (gnus-summary-mark-above score gnus-ticked-mark))
10260
10261 (defun gnus-summary-mark-above (score mark)
10262   "Mark articles with score over SCORE with MARK."
10263   (interactive "P\ncMark: ")
10264   (setq score (if score
10265                   (prefix-numeric-value score)
10266                 (or gnus-summary-default-score 0)))
10267   (save-excursion
10268     (set-buffer gnus-summary-buffer)
10269     (goto-char (point-min))
10270     (while (and (progn
10271                   (when (> (gnus-summary-article-score) score)
10272                     (gnus-summary-mark-article nil mark))
10273                   t)
10274                 (gnus-summary-find-next)))))
10275
10276 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10277 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10278 (defun gnus-summary-limit-include-expunged (&optional no-error)
10279   "Display all the hidden articles that were expunged for low scores."
10280   (interactive)
10281   (let ((buffer-read-only nil))
10282     (let ((scored gnus-newsgroup-scored)
10283           headers h)
10284       (while scored
10285         (unless (gnus-summary-article-header (caar scored))
10286           (and (setq h (gnus-number-to-header (caar scored)))
10287                (< (cdar scored) gnus-summary-expunge-below)
10288                (push h headers)))
10289         (setq scored (cdr scored)))
10290       (if (not headers)
10291           (when (not no-error)
10292             (error "No expunged articles hidden"))
10293         (goto-char (point-min))
10294         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10295         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10296         (mapcar (lambda (x) (push (mail-header-number x)
10297                                   gnus-newsgroup-limit))
10298                 headers)
10299         (gnus-summary-prepare-unthreaded (nreverse headers))
10300         (goto-char (point-min))
10301         (gnus-summary-position-point)
10302         t))))
10303
10304 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10305   "Mark all unread articles in this newsgroup as read.
10306 If prefix argument ALL is non-nil, ticked and dormant articles will
10307 also be marked as read.
10308 If QUIETLY is non-nil, no questions will be asked.
10309
10310 If TO-HERE is non-nil, it should be a point in the buffer.  All
10311 articles before (after, if REVERSE is set) this point will be marked
10312 as read.
10313
10314 Note that this function will only catch up the unread article
10315 in the current summary buffer limitation.
10316
10317 The number of articles marked as read is returned."
10318   (interactive "P")
10319   (prog1
10320       (save-excursion
10321         (when (or quietly
10322                   (not gnus-interactive-catchup) ;Without confirmation?
10323                   gnus-expert-user
10324                   (gnus-y-or-n-p
10325                    (if all
10326                        "Mark absolutely all articles as read? "
10327                      "Mark all unread articles as read? ")))
10328           (if (and not-mark
10329                    (not gnus-newsgroup-adaptive)
10330                    (not gnus-newsgroup-auto-expire)
10331                    (not gnus-suppress-duplicates)
10332                    (or (not gnus-use-cache)
10333                        (eq gnus-use-cache 'passive)))
10334               (progn
10335                 (when all
10336                   (setq gnus-newsgroup-marked nil
10337                         gnus-newsgroup-spam-marked nil
10338                         gnus-newsgroup-dormant nil))
10339                 (setq gnus-newsgroup-unreads
10340                       (gnus-sorted-nunion
10341                        (gnus-intersection gnus-newsgroup-unreads
10342                                           gnus-newsgroup-downloadable)
10343                        gnus-newsgroup-unfetched)))
10344             ;; We actually mark all articles as canceled, which we
10345             ;; have to do when using auto-expiry or adaptive scoring.
10346             (gnus-summary-show-all-threads)
10347             (if (and to-here reverse)
10348                 (progn
10349                   (goto-char to-here)
10350                   (gnus-summary-mark-current-read-and-unread-as-read
10351                    gnus-catchup-mark)
10352                   (while (gnus-summary-find-next (not all))
10353                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10354               (when (gnus-summary-first-subject (not all))
10355                 (while (and
10356                         (if to-here (< (point) to-here) t)
10357                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10358                         (gnus-summary-find-next (not all))))))
10359             (gnus-set-mode-line 'summary))
10360           t))
10361     (gnus-summary-position-point)))
10362
10363 (defun gnus-summary-catchup-to-here (&optional all)
10364   "Mark all unticked articles before the current one as read.
10365 If ALL is non-nil, also mark ticked and dormant articles as read."
10366   (interactive "P")
10367   (save-excursion
10368     (gnus-save-hidden-threads
10369       (let ((beg (point)))
10370         ;; We check that there are unread articles.
10371         (when (or all (gnus-summary-find-prev))
10372           (gnus-summary-catchup all t beg)))))
10373   (gnus-summary-position-point))
10374
10375 (defun gnus-summary-catchup-from-here (&optional all)
10376   "Mark all unticked articles after (and including) the current one as read.
10377 If ALL is non-nil, also mark ticked and dormant articles as read."
10378   (interactive "P")
10379   (save-excursion
10380     (gnus-save-hidden-threads
10381       (let ((beg (point)))
10382         ;; We check that there are unread articles.
10383         (when (or all (gnus-summary-find-next))
10384           (gnus-summary-catchup all t beg nil t)))))
10385   (gnus-summary-position-point))
10386
10387 (defun gnus-summary-catchup-all (&optional quietly)
10388   "Mark all articles in this newsgroup as read.
10389 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10390 instead, which marks only unread articles as read."
10391   (interactive "P")
10392   (gnus-summary-catchup t quietly))
10393
10394 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10395   "Mark all unread articles in this group as read, then exit.
10396 If prefix argument ALL is non-nil, all articles are marked as read.
10397 If QUIETLY is non-nil, no questions will be asked."
10398   (interactive "P")
10399   (when (gnus-summary-catchup all quietly nil 'fast)
10400     ;; Select next newsgroup or exit.
10401     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10402              (eq gnus-auto-select-next 'quietly))
10403         (gnus-summary-next-group nil)
10404       (gnus-summary-exit))))
10405
10406 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10407   "Mark all articles in this newsgroup as read, and then exit.
10408 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10409 instead, which marks only unread articles as read."
10410   (interactive "P")
10411   (gnus-summary-catchup-and-exit t quietly))
10412
10413 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10414   "Mark all articles in this group as read and select the next group.
10415 If given a prefix, mark all articles, unread as well as ticked, as
10416 read."
10417   (interactive "P")
10418   (save-excursion
10419     (gnus-summary-catchup all))
10420   (gnus-summary-next-group))
10421
10422 ;;;
10423 ;;; with article
10424 ;;;
10425
10426 (defmacro gnus-with-article (article &rest forms)
10427   "Select ARTICLE and perform FORMS in the original article buffer.
10428 Then replace the article with the result."
10429   `(progn
10430      ;; We don't want the article to be marked as read.
10431      (let (gnus-mark-article-hook)
10432        (gnus-summary-select-article t t nil ,article))
10433      (set-buffer gnus-original-article-buffer)
10434      ,@forms
10435      (if (not (gnus-check-backend-function
10436                'request-replace-article (car gnus-article-current)))
10437          (gnus-message 5 "Read-only group; not replacing")
10438        (unless (gnus-request-replace-article
10439                 ,article (car gnus-article-current)
10440                 (current-buffer) t)
10441          (error "Couldn't replace article")))
10442      ;; The cache and backlog have to be flushed somewhat.
10443      (when gnus-keep-backlog
10444        (gnus-backlog-remove-article
10445         (car gnus-article-current) (cdr gnus-article-current)))
10446      (when gnus-use-cache
10447        (gnus-cache-update-article
10448         (car gnus-article-current) (cdr gnus-article-current)))))
10449
10450 (put 'gnus-with-article 'lisp-indent-function 1)
10451 (put 'gnus-with-article 'edebug-form-spec '(form body))
10452
10453 ;; Thread-based commands.
10454
10455 (defun gnus-summary-articles-in-thread (&optional article)
10456   "Return a list of all articles in the current thread.
10457 If ARTICLE is non-nil, return all articles in the thread that starts
10458 with that article."
10459   (let* ((article (or article (gnus-summary-article-number)))
10460          (data (gnus-data-find-list article))
10461          (top-level (gnus-data-level (car data)))
10462          (top-subject
10463           (cond ((null gnus-thread-operation-ignore-subject)
10464                  (gnus-simplify-subject-re
10465                   (mail-header-subject (gnus-data-header (car data)))))
10466                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10467                  (gnus-simplify-subject-fuzzy
10468                   (mail-header-subject (gnus-data-header (car data)))))
10469                 (t nil)))
10470          (end-point (save-excursion
10471                       (if (gnus-summary-go-to-next-thread)
10472                           (point) (point-max))))
10473          articles)
10474     (while (and data
10475                 (< (gnus-data-pos (car data)) end-point))
10476       (when (or (not top-subject)
10477                 (string= top-subject
10478                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10479                              (gnus-simplify-subject-fuzzy
10480                               (mail-header-subject
10481                                (gnus-data-header (car data))))
10482                            (gnus-simplify-subject-re
10483                             (mail-header-subject
10484                              (gnus-data-header (car data)))))))
10485         (push (gnus-data-number (car data)) articles))
10486       (unless (and (setq data (cdr data))
10487                    (> (gnus-data-level (car data)) top-level))
10488         (setq data nil)))
10489     ;; Return the list of articles.
10490     (nreverse articles)))
10491
10492 (defun gnus-summary-rethread-current ()
10493   "Rethread the thread the current article is part of."
10494   (interactive)
10495   (let* ((gnus-show-threads t)
10496          (article (gnus-summary-article-number))
10497          (id (mail-header-id (gnus-summary-article-header)))
10498          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10499     (unless id
10500       (error "No article on the current line"))
10501     (gnus-rebuild-thread id)
10502     (gnus-summary-goto-subject article)))
10503
10504 (defun gnus-summary-reparent-thread ()
10505   "Make the current article child of the marked (or previous) article.
10506
10507 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10508 is non-nil or the Subject: of both articles are the same."
10509   (interactive)
10510   (unless (not (gnus-group-read-only-p))
10511     (error "The current newsgroup does not support article editing"))
10512   (unless (<= (length gnus-newsgroup-processable) 1)
10513     (error "No more than one article may be marked"))
10514   (save-window-excursion
10515     (let ((gnus-article-buffer " *reparent*")
10516           (current-article (gnus-summary-article-number))
10517           ;; First grab the marked article, otherwise one line up.
10518           (parent-article (if (not (null gnus-newsgroup-processable))
10519                               (car gnus-newsgroup-processable)
10520                             (save-excursion
10521                               (if (eq (forward-line -1) 0)
10522                                   (gnus-summary-article-number)
10523                                 (error "Beginning of summary buffer"))))))
10524       (unless (not (eq current-article parent-article))
10525         (error "An article may not be self-referential"))
10526       (let ((message-id (mail-header-id
10527                          (gnus-summary-article-header parent-article))))
10528         (unless (and message-id (not (equal message-id "")))
10529           (error "No message-id in desired parent"))
10530         (gnus-with-article current-article
10531           (save-restriction
10532             (goto-char (point-min))
10533             (message-narrow-to-head)
10534             (if (re-search-forward "^References: " nil t)
10535                 (progn
10536                   (re-search-forward "^[^ \t]" nil t)
10537                   (forward-line -1)
10538                   (end-of-line)
10539                   (insert " " message-id))
10540               (insert "References: " message-id "\n"))))
10541         (set-buffer gnus-summary-buffer)
10542         (gnus-summary-unmark-all-processable)
10543         (gnus-summary-update-article current-article)
10544         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10545             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10546         (gnus-summary-rethread-current)
10547         (gnus-message 3 "Article %d is now the child of article %d"
10548                       current-article parent-article)))))
10549
10550 (defun gnus-summary-toggle-threads (&optional arg)
10551   "Toggle showing conversation threads.
10552 If ARG is positive number, turn showing conversation threads on."
10553   (interactive "P")
10554   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10555     (setq gnus-show-threads
10556           (if (null arg) (not gnus-show-threads)
10557             (> (prefix-numeric-value arg) 0)))
10558     (gnus-summary-prepare)
10559     (gnus-summary-goto-subject current)
10560     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10561     (gnus-summary-position-point)))
10562
10563 (defun gnus-summary-show-all-threads ()
10564   "Show all threads."
10565   (interactive)
10566   (save-excursion
10567     (let ((buffer-read-only nil))
10568       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10569   (gnus-summary-position-point))
10570
10571 (defun gnus-summary-show-thread ()
10572   "Show thread subtrees.
10573 Returns nil if no thread was there to be shown."
10574   (interactive)
10575   (let ((buffer-read-only nil)
10576         (orig (point))
10577         (end (point-at-eol))
10578         ;; Leave point at bol
10579         (beg (progn (beginning-of-line) (point))))
10580     (prog1
10581         ;; Any hidden lines here?
10582         (search-forward "\r" end t)
10583       (subst-char-in-region beg end ?\^M ?\n t)
10584       (goto-char orig)
10585       (gnus-summary-position-point))))
10586
10587 (defun gnus-summary-maybe-hide-threads ()
10588   "If requested, hide the threads that should be hidden."
10589   (when (and gnus-show-threads
10590              gnus-thread-hide-subtree)
10591     (gnus-summary-hide-all-threads
10592      (if (or (consp gnus-thread-hide-subtree)
10593              (functionp gnus-thread-hide-subtree))
10594          (gnus-make-predicate gnus-thread-hide-subtree)
10595        nil))))
10596
10597 ;;; Hiding predicates.
10598
10599 (defun gnus-article-unread-p (header)
10600   (memq (mail-header-number header) gnus-newsgroup-unreads))
10601
10602 (defun gnus-article-unseen-p (header)
10603   (memq (mail-header-number header) gnus-newsgroup-unseen))
10604
10605 (defun gnus-map-articles (predicate articles)
10606   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10607   (apply 'gnus-or (mapcar predicate
10608                           (mapcar 'gnus-summary-article-header articles))))
10609
10610 (defun gnus-summary-hide-all-threads (&optional predicate)
10611   "Hide all thread subtrees.
10612 If PREDICATE is supplied, threads that satisfy this predicate
10613 will not be hidden."
10614   (interactive)
10615   (save-excursion
10616     (goto-char (point-min))
10617     (let ((end nil))
10618       (while (not end)
10619         (when (or (not predicate)
10620                   (gnus-map-articles
10621                    predicate (gnus-summary-article-children)))
10622             (gnus-summary-hide-thread))
10623         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10624   (gnus-summary-position-point))
10625
10626 (defun gnus-summary-hide-thread ()
10627   "Hide thread subtrees.
10628 If PREDICATE is supplied, threads that satisfy this predicate
10629 will not be hidden.
10630 Returns nil if no threads were there to be hidden."
10631   (interactive)
10632   (let ((buffer-read-only nil)
10633         (start (point))
10634         (article (gnus-summary-article-number)))
10635     (goto-char start)
10636     ;; Go forward until either the buffer ends or the subthread
10637     ;; ends.
10638     (when (and (not (eobp))
10639                (or (zerop (gnus-summary-next-thread 1 t))
10640                    (goto-char (point-max))))
10641       (prog1
10642           (if (and (> (point) start)
10643                    (search-backward "\n" start t))
10644               (progn
10645                 (subst-char-in-region start (point) ?\n ?\^M)
10646                 (gnus-summary-goto-subject article))
10647             (goto-char start)
10648             nil)))))
10649
10650 (defun gnus-summary-go-to-next-thread (&optional previous)
10651   "Go to the same level (or less) next thread.
10652 If PREVIOUS is non-nil, go to previous thread instead.
10653 Return the article number moved to, or nil if moving was impossible."
10654   (let ((level (gnus-summary-thread-level))
10655         (way (if previous -1 1))
10656         (beg (point)))
10657     (forward-line way)
10658     (while (and (not (eobp))
10659                 (< level (gnus-summary-thread-level)))
10660       (forward-line way))
10661     (if (eobp)
10662         (progn
10663           (goto-char beg)
10664           nil)
10665       (setq beg (point))
10666       (prog1
10667           (gnus-summary-article-number)
10668         (goto-char beg)))))
10669
10670 (defun gnus-summary-next-thread (n &optional silent)
10671   "Go to the same level next N'th thread.
10672 If N is negative, search backward instead.
10673 Returns the difference between N and the number of skips actually
10674 done.
10675
10676 If SILENT, don't output messages."
10677   (interactive "p")
10678   (let ((backward (< n 0))
10679         (n (abs n)))
10680     (while (and (> n 0)
10681                 (gnus-summary-go-to-next-thread backward))
10682       (decf n))
10683     (unless silent
10684       (gnus-summary-position-point))
10685     (when (and (not silent) (/= 0 n))
10686       (gnus-message 7 "No more threads"))
10687     n))
10688
10689 (defun gnus-summary-prev-thread (n)
10690   "Go to the same level previous N'th thread.
10691 Returns the difference between N and the number of skips actually
10692 done."
10693   (interactive "p")
10694   (gnus-summary-next-thread (- n)))
10695
10696 (defun gnus-summary-go-down-thread ()
10697   "Go down one level in the current thread."
10698   (let ((children (gnus-summary-article-children)))
10699     (when children
10700       (gnus-summary-goto-subject (car children)))))
10701
10702 (defun gnus-summary-go-up-thread ()
10703   "Go up one level in the current thread."
10704   (let ((parent (gnus-summary-article-parent)))
10705     (when parent
10706       (gnus-summary-goto-subject parent))))
10707
10708 (defun gnus-summary-down-thread (n)
10709   "Go down thread N steps.
10710 If N is negative, go up instead.
10711 Returns the difference between N and how many steps down that were
10712 taken."
10713   (interactive "p")
10714   (let ((up (< n 0))
10715         (n (abs n)))
10716     (while (and (> n 0)
10717                 (if up (gnus-summary-go-up-thread)
10718                   (gnus-summary-go-down-thread)))
10719       (setq n (1- n)))
10720     (gnus-summary-position-point)
10721     (when (/= 0 n)
10722       (gnus-message 7 "Can't go further"))
10723     n))
10724
10725 (defun gnus-summary-up-thread (n)
10726   "Go up thread N steps.
10727 If N is negative, go down instead.
10728 Returns the difference between N and how many steps down that were
10729 taken."
10730   (interactive "p")
10731   (gnus-summary-down-thread (- n)))
10732
10733 (defun gnus-summary-top-thread ()
10734   "Go to the top of the thread."
10735   (interactive)
10736   (while (gnus-summary-go-up-thread))
10737   (gnus-summary-article-number))
10738
10739 (defun gnus-summary-kill-thread (&optional unmark)
10740   "Mark articles under current thread as read.
10741 If the prefix argument is positive, remove any kinds of marks.
10742 If the prefix argument is negative, tick articles instead."
10743   (interactive "P")
10744   (when unmark
10745     (setq unmark (prefix-numeric-value unmark)))
10746   (let ((articles (gnus-summary-articles-in-thread)))
10747     (save-excursion
10748       ;; Expand the thread.
10749       (gnus-summary-show-thread)
10750       ;; Mark all the articles.
10751       (while articles
10752         (gnus-summary-goto-subject (car articles))
10753         (cond ((null unmark)
10754                (gnus-summary-mark-article-as-read gnus-killed-mark))
10755               ((> unmark 0)
10756                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10757               (t
10758                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10759         (setq articles (cdr articles))))
10760     ;; Hide killed subtrees.
10761     (and (null unmark)
10762          gnus-thread-hide-killed
10763          (gnus-summary-hide-thread))
10764     ;; If marked as read, go to next unread subject.
10765     (when (null unmark)
10766       ;; Go to next unread subject.
10767       (gnus-summary-next-subject 1 t)))
10768   (gnus-set-mode-line 'summary))
10769
10770 ;; Summary sorting commands
10771
10772 (defun gnus-summary-sort-by-number (&optional reverse)
10773   "Sort the summary buffer by article number.
10774 Argument REVERSE means reverse order."
10775   (interactive "P")
10776   (gnus-summary-sort 'number reverse))
10777
10778 (defun gnus-summary-sort-by-random (&optional reverse)
10779   "Randomize the order in the summary buffer.
10780 Argument REVERSE means to randomize in reverse order."
10781   (interactive "P")
10782   (gnus-summary-sort 'random reverse))
10783
10784 (defun gnus-summary-sort-by-author (&optional reverse)
10785   "Sort the summary buffer by author name alphabetically.
10786 If `case-fold-search' is non-nil, case of letters is ignored.
10787 Argument REVERSE means reverse order."
10788   (interactive "P")
10789   (gnus-summary-sort 'author reverse))
10790
10791 (defun gnus-summary-sort-by-subject (&optional reverse)
10792   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10793 If `case-fold-search' is non-nil, case of letters is ignored.
10794 Argument REVERSE means reverse order."
10795   (interactive "P")
10796   (gnus-summary-sort 'subject reverse))
10797
10798 (defun gnus-summary-sort-by-date (&optional reverse)
10799   "Sort the summary buffer by date.
10800 Argument REVERSE means reverse order."
10801   (interactive "P")
10802   (gnus-summary-sort 'date reverse))
10803
10804 (defun gnus-summary-sort-by-score (&optional reverse)
10805   "Sort the summary buffer by score.
10806 Argument REVERSE means reverse order."
10807   (interactive "P")
10808   (gnus-summary-sort 'score reverse))
10809
10810 (defun gnus-summary-sort-by-lines (&optional reverse)
10811   "Sort the summary buffer by the number of lines.
10812 Argument REVERSE means reverse order."
10813   (interactive "P")
10814   (gnus-summary-sort 'lines reverse))
10815
10816 (defun gnus-summary-sort-by-chars (&optional reverse)
10817   "Sort the summary buffer by article length.
10818 Argument REVERSE means reverse order."
10819   (interactive "P")
10820   (gnus-summary-sort 'chars reverse))
10821
10822 (defun gnus-summary-sort-by-original (&optional reverse)
10823   "Sort the summary buffer using the default sorting method.
10824 Argument REVERSE means reverse order."
10825   (interactive "P")
10826   (let* ((buffer-read-only)
10827          (gnus-summary-prepare-hook nil))
10828     ;; We do the sorting by regenerating the threads.
10829     (gnus-summary-prepare)
10830     ;; Hide subthreads if needed.
10831     (gnus-summary-maybe-hide-threads)))
10832
10833 (defun gnus-summary-sort (predicate reverse)
10834   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10835   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10836          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10837          (gnus-thread-sort-functions
10838           (if (not reverse)
10839               thread
10840             `(lambda (t1 t2)
10841                (,thread t2 t1))))
10842          (gnus-sort-gathered-threads-function
10843           gnus-thread-sort-functions)
10844          (gnus-article-sort-functions
10845           (if (not reverse)
10846               article
10847             `(lambda (t1 t2)
10848                (,article t2 t1))))
10849          (buffer-read-only)
10850          (gnus-summary-prepare-hook nil))
10851     ;; We do the sorting by regenerating the threads.
10852     (gnus-summary-prepare)
10853     ;; Hide subthreads if needed.
10854     (gnus-summary-maybe-hide-threads)))
10855
10856 ;; Summary saving commands.
10857
10858 (defun gnus-summary-save-article (&optional n not-saved)
10859   "Save the current article using the default saver function.
10860 If N is a positive number, save the N next articles.
10861 If N is a negative number, save the N previous articles.
10862 If N is nil and any articles have been marked with the process mark,
10863 save those articles instead.
10864 The variable `gnus-default-article-saver' specifies the saver function."
10865   (interactive "P")
10866   (let* ((articles (gnus-summary-work-articles n))
10867          (save-buffer (save-excursion
10868                         (nnheader-set-temp-buffer " *Gnus Save*")))
10869          (num (length articles))
10870          header file)
10871     (dolist (article articles)
10872       (setq header (gnus-summary-article-header article))
10873       (if (not (vectorp header))
10874           ;; This is a pseudo-article.
10875           (if (assq 'name header)
10876               (gnus-copy-file (cdr (assq 'name header)))
10877             (gnus-message 1 "Article %d is unsaveable" article))
10878         ;; This is a real article.
10879         (save-window-excursion
10880           (let ((gnus-display-mime-function nil)
10881                 (gnus-article-prepare-hook nil))
10882             (gnus-summary-select-article t nil nil article)))
10883         (save-excursion
10884           (set-buffer save-buffer)
10885           (erase-buffer)
10886           (insert-buffer-substring gnus-original-article-buffer))
10887         (setq file (gnus-article-save save-buffer file num))
10888         (gnus-summary-remove-process-mark article)
10889         (unless not-saved
10890           (gnus-summary-set-saved-mark article))))
10891     (gnus-kill-buffer save-buffer)
10892     (gnus-summary-position-point)
10893     (gnus-set-mode-line 'summary)
10894     n))
10895
10896 (defun gnus-summary-pipe-output (&optional arg headers)
10897   "Pipe the current article to a subprocess.
10898 If N is a positive number, pipe the N next articles.
10899 If N is a negative number, pipe the N previous articles.
10900 If N is nil and any articles have been marked with the process mark,
10901 pipe those articles instead.
10902 If HEADERS (the symbolic prefix), include the headers, too."
10903   (interactive (gnus-interactive "P\ny"))
10904   (require 'gnus-art)
10905   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10906         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10907     (gnus-summary-save-article arg t))
10908   (let ((buffer (get-buffer "*Shell Command Output*")))
10909     (when (and buffer
10910                (not (zerop (buffer-size buffer))))
10911       (gnus-configure-windows 'pipe))))
10912
10913 (defun gnus-summary-save-article-mail (&optional arg)
10914   "Append the current article to an mail file.
10915 If N is a positive number, save the N next articles.
10916 If N is a negative number, save the N previous articles.
10917 If N is nil and any articles have been marked with the process mark,
10918 save those articles instead."
10919   (interactive "P")
10920   (require 'gnus-art)
10921   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10922     (gnus-summary-save-article arg)))
10923
10924 (defun gnus-summary-save-article-rmail (&optional arg)
10925   "Append the current article to an rmail file.
10926 If N is a positive number, save the N next articles.
10927 If N is a negative number, save the N previous articles.
10928 If N is nil and any articles have been marked with the process mark,
10929 save those articles instead."
10930   (interactive "P")
10931   (require 'gnus-art)
10932   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10933     (gnus-summary-save-article arg)))
10934
10935 (defun gnus-summary-save-article-file (&optional arg)
10936   "Append the current article to a file.
10937 If N is a positive number, save the N next articles.
10938 If N is a negative number, save the N previous articles.
10939 If N is nil and any articles have been marked with the process mark,
10940 save those articles instead."
10941   (interactive "P")
10942   (require 'gnus-art)
10943   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10944     (gnus-summary-save-article arg)))
10945
10946 (defun gnus-summary-write-article-file (&optional arg)
10947   "Write the current article to a file, deleting the previous file.
10948 If N is a positive number, save the N next articles.
10949 If N is a negative number, save the N previous articles.
10950 If N is nil and any articles have been marked with the process mark,
10951 save those articles instead."
10952   (interactive "P")
10953   (require 'gnus-art)
10954   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10955     (gnus-summary-save-article arg)))
10956
10957 (defun gnus-summary-save-article-body-file (&optional arg)
10958   "Append the current article body to a file.
10959 If N is a positive number, save the N next articles.
10960 If N is a negative number, save the N previous articles.
10961 If N is nil and any articles have been marked with the process mark,
10962 save those articles instead."
10963   (interactive "P")
10964   (require 'gnus-art)
10965   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10966     (gnus-summary-save-article arg)))
10967
10968 (defun gnus-summary-muttprint (&optional arg)
10969   "Print the current article using Muttprint.
10970 If N is a positive number, save the N next articles.
10971 If N is a negative number, save the N previous articles.
10972 If N is nil and any articles have been marked with the process mark,
10973 save those articles instead."
10974   (interactive "P")
10975   (require 'gnus-art)
10976   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10977     (gnus-summary-save-article arg t)))
10978
10979 (defun gnus-summary-pipe-message (program)
10980   "Pipe the current article through PROGRAM."
10981   (interactive "sProgram: ")
10982   (gnus-summary-select-article)
10983   (let ((mail-header-separator ""))
10984     (gnus-eval-in-buffer-window gnus-article-buffer
10985       (save-restriction
10986         (widen)
10987         (let ((start (window-start))
10988               buffer-read-only)
10989           (message-pipe-buffer-body program)
10990           (set-window-start (get-buffer-window (current-buffer)) start))))))
10991
10992 (defun gnus-get-split-value (methods)
10993   "Return a value based on the split METHODS."
10994   (let (split-name method result match)
10995     (when methods
10996       (save-excursion
10997         (set-buffer gnus-original-article-buffer)
10998         (save-restriction
10999           (nnheader-narrow-to-headers)
11000           (while (and methods (not split-name))
11001             (goto-char (point-min))
11002             (setq method (pop methods))
11003             (setq match (car method))
11004             (when (cond
11005                    ((stringp match)
11006                     ;; Regular expression.
11007                     (ignore-errors
11008                       (re-search-forward match nil t)))
11009                    ((functionp match)
11010                     ;; Function.
11011                     (save-restriction
11012                       (widen)
11013                       (setq result (funcall match gnus-newsgroup-name))))
11014                    ((consp match)
11015                     ;; Form.
11016                     (save-restriction
11017                       (widen)
11018                       (setq result (eval match)))))
11019               (setq split-name (cdr method))
11020               (cond ((stringp result)
11021                      (push (expand-file-name
11022                             result gnus-article-save-directory)
11023                            split-name))
11024                     ((consp result)
11025                      (setq split-name (append result split-name)))))))))
11026     (nreverse split-name)))
11027
11028 (defun gnus-valid-move-group-p (group)
11029   (and (boundp group)
11030        (symbol-name group)
11031        (symbol-value group)
11032        (gnus-get-function (gnus-find-method-for-group
11033                            (symbol-name group)) 'request-accept-article t)))
11034
11035 (defun gnus-read-move-group-name (prompt default articles prefix)
11036   "Read a group name."
11037   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11038          (minibuffer-confirm-incomplete nil) ; XEmacs
11039          (prom
11040           (format "%s %s to:"
11041                   prompt
11042                   (if (> (length articles) 1)
11043                       (format "these %d articles" (length articles))
11044                     "this article")))
11045          (to-newsgroup
11046           (cond
11047            ((null split-name)
11048             (gnus-completing-read-with-default
11049              default prom
11050              gnus-active-hashtb
11051              'gnus-valid-move-group-p
11052              nil prefix
11053              'gnus-group-history))
11054            ((= 1 (length split-name))
11055             (gnus-completing-read-with-default
11056              (car split-name) prom
11057              gnus-active-hashtb
11058              'gnus-valid-move-group-p
11059              nil nil
11060              'gnus-group-history))
11061            (t
11062             (gnus-completing-read-with-default
11063              nil prom
11064              (mapcar (lambda (el) (list el))
11065                      (nreverse split-name))
11066              nil nil nil
11067              'gnus-group-history))))
11068          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11069     (when to-newsgroup
11070       (if (or (string= to-newsgroup "")
11071               (string= to-newsgroup prefix))
11072           (setq to-newsgroup default))
11073       (unless to-newsgroup
11074         (error "No group name entered"))
11075       (or (gnus-active to-newsgroup)
11076           (gnus-activate-group to-newsgroup nil nil to-method)
11077           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11078                                      to-newsgroup))
11079               (or (and (gnus-request-create-group to-newsgroup to-method)
11080                        (gnus-activate-group
11081                         to-newsgroup nil nil to-method)
11082                        (gnus-subscribe-group to-newsgroup))
11083                   (error "Couldn't create group %s" to-newsgroup)))
11084           (error "No such group: %s" to-newsgroup)))
11085     to-newsgroup))
11086
11087 (defun gnus-summary-save-parts (type dir n &optional reverse)
11088   "Save parts matching TYPE to DIR.
11089 If REVERSE, save parts that do not match TYPE."
11090   (interactive
11091    (list (read-string "Save parts of type: "
11092                       (or (car gnus-summary-save-parts-type-history)
11093                           gnus-summary-save-parts-default-mime)
11094                       'gnus-summary-save-parts-type-history)
11095          (setq gnus-summary-save-parts-last-directory
11096                (read-file-name "Save to directory: "
11097                                gnus-summary-save-parts-last-directory
11098                                nil t))
11099          current-prefix-arg))
11100   (gnus-summary-iterate n
11101     (let ((gnus-display-mime-function nil)
11102           (gnus-inhibit-treatment t))
11103       (gnus-summary-select-article))
11104     (save-excursion
11105       (set-buffer gnus-article-buffer)
11106       (let ((handles (or gnus-article-mime-handles
11107                          (mm-dissect-buffer nil gnus-article-loose-mime)
11108                          (and gnus-article-emulate-mime
11109                               (mm-uu-dissect)))))
11110         (when handles
11111           (gnus-summary-save-parts-1 type dir handles reverse)
11112           (unless gnus-article-mime-handles ;; Don't destroy this case.
11113             (mm-destroy-parts handles)))))))
11114
11115 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11116   (if (stringp (car handle))
11117       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11118               (cdr handle))
11119     (when (if reverse
11120               (not (string-match type (mm-handle-media-type handle)))
11121             (string-match type (mm-handle-media-type handle)))
11122       (let ((file (expand-file-name
11123                    (gnus-map-function
11124                     mm-file-name-rewrite-functions
11125                     (file-name-nondirectory
11126                      (or
11127                       (mail-content-type-get
11128                        (mm-handle-disposition handle) 'filename)
11129                       (mail-content-type-get
11130                        (mm-handle-type handle) 'name)
11131                       (concat gnus-newsgroup-name
11132                               "." (number-to-string
11133                                    (cdr gnus-article-current))))))
11134                    dir)))
11135         (unless (file-exists-p file)
11136           (mm-save-part-to-file handle file))))))
11137
11138 ;; Summary extract commands
11139
11140 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11141   (let ((buffer-read-only nil)
11142         (article (gnus-summary-article-number))
11143         after-article b e)
11144     (unless (gnus-summary-goto-subject article)
11145       (error "No such article: %d" article))
11146     (gnus-summary-position-point)
11147     ;; If all commands are to be bunched up on one line, we collect
11148     ;; them here.
11149     (unless gnus-view-pseudos-separately
11150       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11151             files action)
11152         (while ps
11153           (setq action (cdr (assq 'action (car ps))))
11154           (setq files (list (cdr (assq 'name (car ps)))))
11155           (while (and ps (cdr ps)
11156                       (string= (or action "1")
11157                                (or (cdr (assq 'action (cadr ps))) "2")))
11158             (push (cdr (assq 'name (cadr ps))) files)
11159             (setcdr ps (cddr ps)))
11160           (when files
11161             (when (not (string-match "%s" action))
11162               (push " " files))
11163             (push " " files)
11164             (when (assq 'execute (car ps))
11165               (setcdr (assq 'execute (car ps))
11166                       (funcall (if (string-match "%s" action)
11167                                    'format 'concat)
11168                                action
11169                                (mapconcat
11170                                 (lambda (f)
11171                                   (if (equal f " ")
11172                                       f
11173                                     (shell-quote-argument f)))
11174                                 files " ")))))
11175           (setq ps (cdr ps)))))
11176     (if (and gnus-view-pseudos (not not-view))
11177         (while pslist
11178           (when (assq 'execute (car pslist))
11179             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11180                                   (eq gnus-view-pseudos 'not-confirm)))
11181           (setq pslist (cdr pslist)))
11182       (save-excursion
11183         (while pslist
11184           (setq after-article (or (cdr (assq 'article (car pslist)))
11185                                   (gnus-summary-article-number)))
11186           (gnus-summary-goto-subject after-article)
11187           (forward-line 1)
11188           (setq b (point))
11189           (insert "    " (file-name-nondirectory
11190                           (cdr (assq 'name (car pslist))))
11191                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11192           (setq e (point))
11193           (forward-line -1)             ; back to `b'
11194           (gnus-add-text-properties
11195            b (1- e) (list 'gnus-number gnus-reffed-article-number
11196                           gnus-mouse-face-prop gnus-mouse-face))
11197           (gnus-data-enter
11198            after-article gnus-reffed-article-number
11199            gnus-unread-mark b (car pslist) 0 (- e b))
11200           (setq gnus-newsgroup-unreads
11201                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11202                                          gnus-reffed-article-number))
11203           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11204           (setq pslist (cdr pslist)))))))
11205
11206 (defun gnus-pseudos< (p1 p2)
11207   (let ((c1 (cdr (assq 'action p1)))
11208         (c2 (cdr (assq 'action p2))))
11209     (and c1 c2 (string< c1 c2))))
11210
11211 (defun gnus-request-pseudo-article (props)
11212   (cond ((assq 'execute props)
11213          (gnus-execute-command (cdr (assq 'execute props)))))
11214   (let ((gnus-current-article (gnus-summary-article-number)))
11215     (gnus-run-hooks 'gnus-mark-article-hook)))
11216
11217 (defun gnus-execute-command (command &optional automatic)
11218   (save-excursion
11219     (gnus-article-setup-buffer)
11220     (set-buffer gnus-article-buffer)
11221     (setq buffer-read-only nil)
11222     (let ((command (if automatic command
11223                      (read-string "Command: " (cons command 0)))))
11224       (erase-buffer)
11225       (insert "$ " command "\n\n")
11226       (if gnus-view-pseudo-asynchronously
11227           (start-process "gnus-execute" (current-buffer) shell-file-name
11228                          shell-command-switch command)
11229         (call-process shell-file-name nil t nil
11230                       shell-command-switch command)))))
11231
11232 ;; Summary kill commands.
11233
11234 (defun gnus-summary-edit-global-kill (article)
11235   "Edit the \"global\" kill file."
11236   (interactive (list (gnus-summary-article-number)))
11237   (gnus-group-edit-global-kill article))
11238
11239 (defun gnus-summary-edit-local-kill ()
11240   "Edit a local kill file applied to the current newsgroup."
11241   (interactive)
11242   (setq gnus-current-headers (gnus-summary-article-header))
11243   (gnus-group-edit-local-kill
11244    (gnus-summary-article-number) gnus-newsgroup-name))
11245
11246 ;;; Header reading.
11247
11248 (defun gnus-read-header (id &optional header)
11249   "Read the headers of article ID and enter them into the Gnus system."
11250   (let ((group gnus-newsgroup-name)
11251         (gnus-override-method
11252          (or
11253           gnus-override-method
11254           (and (gnus-news-group-p gnus-newsgroup-name)
11255                (car (gnus-refer-article-methods)))))
11256         where)
11257     ;; First we check to see whether the header in question is already
11258     ;; fetched.
11259     (if (stringp id)
11260         ;; This is a Message-ID.
11261         (setq header (or header (gnus-id-to-header id)))
11262       ;; This is an article number.
11263       (setq header (or header (gnus-summary-article-header id))))
11264     (if (and header
11265              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11266         ;; We have found the header.
11267         header
11268       ;; We have to really fetch the header to this article.
11269       (save-excursion
11270         (set-buffer nntp-server-buffer)
11271         (when (setq where (gnus-request-head id group))
11272           (nnheader-fold-continuation-lines)
11273           (goto-char (point-max))
11274           (insert ".\n")
11275           (goto-char (point-min))
11276           (insert "211 ")
11277           (princ (cond
11278                   ((numberp id) id)
11279                   ((cdr where) (cdr where))
11280                   (header (mail-header-number header))
11281                   (t gnus-reffed-article-number))
11282                  (current-buffer))
11283           (insert " Article retrieved.\n"))
11284         (if (or (not where)
11285                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11286             ()                          ; Malformed head.
11287           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11288             (when (and (stringp id)
11289                        (not (string= (gnus-group-real-name group)
11290                                      (car where))))
11291               ;; If we fetched by Message-ID and the article came
11292               ;; from a different group, we fudge some bogus article
11293               ;; numbers for this article.
11294               (mail-header-set-number header gnus-reffed-article-number))
11295             (save-excursion
11296               (set-buffer gnus-summary-buffer)
11297               (decf gnus-reffed-article-number)
11298               (gnus-remove-header (mail-header-number header))
11299               (push header gnus-newsgroup-headers)
11300               (setq gnus-current-headers header)
11301               (push (mail-header-number header) gnus-newsgroup-limit)))
11302           header)))))
11303
11304 (defun gnus-remove-header (number)
11305   "Remove header NUMBER from `gnus-newsgroup-headers'."
11306   (if (and gnus-newsgroup-headers
11307            (= number (mail-header-number (car gnus-newsgroup-headers))))
11308       (pop gnus-newsgroup-headers)
11309     (let ((headers gnus-newsgroup-headers))
11310       (while (and (cdr headers)
11311                   (not (= number (mail-header-number (cadr headers)))))
11312         (pop headers))
11313       (when (cdr headers)
11314         (setcdr headers (cddr headers))))))
11315
11316 ;;;
11317 ;;; summary highlights
11318 ;;;
11319
11320 (defun gnus-highlight-selected-summary ()
11321   "Highlight selected article in summary buffer."
11322   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11323   (when gnus-summary-selected-face
11324     (save-excursion
11325       (let* ((beg (point-at-bol))
11326              (end (point-at-eol))
11327              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11328              (from (if (get-text-property beg gnus-mouse-face-prop)
11329                        beg
11330                      (or (next-single-property-change
11331                           beg gnus-mouse-face-prop nil end)
11332                          beg)))
11333              (to
11334               (if (= from end)
11335                   (- from 2)
11336                 (or (next-single-property-change
11337                      from gnus-mouse-face-prop nil end)
11338                     end))))
11339         ;; If no mouse-face prop on line we will have to = from = end,
11340         ;; so we highlight the entire line instead.
11341         (when (= (+ to 2) from)
11342           (setq from beg)
11343           (setq to end))
11344         (if gnus-newsgroup-selected-overlay
11345             ;; Move old overlay.
11346             (gnus-move-overlay
11347              gnus-newsgroup-selected-overlay from to (current-buffer))
11348           ;; Create new overlay.
11349           (gnus-overlay-put
11350            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11351            'face gnus-summary-selected-face))))))
11352
11353 (defvar gnus-summary-highlight-line-cached nil)
11354 (defvar gnus-summary-highlight-line-trigger nil)
11355
11356 (defun gnus-summary-highlight-line-0 ()
11357   (if (and (eq gnus-summary-highlight-line-trigger
11358                gnus-summary-highlight)
11359            gnus-summary-highlight-line-cached)
11360       gnus-summary-highlight-line-cached
11361     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11362           gnus-summary-highlight-line-cached
11363           (let* ((cond (list 'cond))
11364                  (c cond)
11365                  (list gnus-summary-highlight))
11366             (while list
11367               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11368                               nil))
11369               (setq c (cdr c)
11370                     list (cdr list)))
11371             (gnus-byte-compile (list 'lambda nil cond))))))
11372
11373 (defun gnus-summary-highlight-line ()
11374   "Highlight current line according to `gnus-summary-highlight'."
11375   (let* ((beg (point-at-bol))
11376          (article (or (gnus-summary-article-number) gnus-current-article))
11377          (score (or (cdr (assq article
11378                                gnus-newsgroup-scored))
11379                     gnus-summary-default-score 0))
11380          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11381          (inhibit-read-only t)
11382          (default gnus-summary-default-score)
11383          (default-high gnus-summary-default-high-score)
11384          (default-low gnus-summary-default-low-score)
11385          (uncached (and gnus-summary-use-undownloaded-faces
11386                         (memq article gnus-newsgroup-undownloaded)
11387                         (not (memq article gnus-newsgroup-cached)))))
11388     (let ((face (funcall (gnus-summary-highlight-line-0))))
11389       (unless (eq face (get-text-property beg 'face))
11390         (gnus-put-text-property-excluding-characters-with-faces
11391          beg (point-at-eol) 'face
11392          (setq face (if (boundp face) (symbol-value face) face)))
11393         (when gnus-summary-highlight-line-function
11394           (funcall gnus-summary-highlight-line-function article face))))))
11395
11396 (defun gnus-update-read-articles (group unread &optional compute)
11397   "Update the list of read articles in GROUP.
11398 UNREAD is a sorted list."
11399   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11400         (info (gnus-get-info group))
11401         (prev 1)
11402         read)
11403     (if (or (not info) (not active))
11404         ;; There is no info on this group if it was, in fact,
11405         ;; killed.  Gnus stores no information on killed groups, so
11406         ;; there's nothing to be done.
11407         ;; One could store the information somewhere temporarily,
11408         ;; perhaps...  Hmmm...
11409         ()
11410       ;; Remove any negative articles numbers.
11411       (while (and unread (< (car unread) 0))
11412         (setq unread (cdr unread)))
11413       ;; Remove any expired article numbers
11414       (while (and unread (< (car unread) (car active)))
11415         (setq unread (cdr unread)))
11416       ;; Compute the ranges of read articles by looking at the list of
11417       ;; unread articles.
11418       (while unread
11419         (when (/= (car unread) prev)
11420           (push (if (= prev (1- (car unread))) prev
11421                   (cons prev (1- (car unread))))
11422                 read))
11423         (setq prev (1+ (car unread)))
11424         (setq unread (cdr unread)))
11425       (when (<= prev (cdr active))
11426         (push (cons prev (cdr active)) read))
11427       (setq read (if (> (length read) 1) (nreverse read) read))
11428       (if compute
11429           read
11430         (save-excursion
11431           (let (setmarkundo)
11432             ;; Propagate the read marks to the backend.
11433             (when (gnus-check-backend-function 'request-set-mark group)
11434               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11435                     (add (gnus-remove-from-range read (gnus-info-read info))))
11436                 (when (or add del)
11437                   (unless (gnus-check-group group)
11438                     (error "Can't open server for %s" group))
11439                   (gnus-request-set-mark
11440                    group (delq nil (list (if add (list add 'add '(read)))
11441                                          (if del (list del 'del '(read))))))
11442                   (setq setmarkundo
11443                         `(gnus-request-set-mark
11444                           ,group
11445                           ',(delq nil (list
11446                                        (if del (list del 'add '(read)))
11447                                        (if add (list add 'del '(read))))))))))
11448             (set-buffer gnus-group-buffer)
11449             (gnus-undo-register
11450               `(progn
11451                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11452                  (gnus-info-set-read ',info ',(gnus-info-read info))
11453                  (gnus-get-unread-articles-in-group ',info
11454                                                     (gnus-active ,group))
11455                  (gnus-group-update-group ,group t)
11456                  ,setmarkundo))))
11457         ;; Enter this list into the group info.
11458         (gnus-info-set-read info read)
11459         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11460         (gnus-get-unread-articles-in-group info (gnus-active group))
11461         t))))
11462
11463 (defun gnus-offer-save-summaries ()
11464   "Offer to save all active summary buffers."
11465   (let (buffers)
11466     ;; Go through all buffers and find all summaries.
11467     (dolist (buffer (buffer-list))
11468       (when (and (setq buffer (buffer-name buffer))
11469                  (string-match "Summary" buffer)
11470                  (save-excursion
11471                    (set-buffer buffer)
11472                    ;; We check that this is, indeed, a summary buffer.
11473                    (and (eq major-mode 'gnus-summary-mode)
11474                         ;; Also make sure this isn't bogus.
11475                         gnus-newsgroup-prepared
11476                         ;; Also make sure that this isn't a
11477                         ;; dead summary buffer.
11478                         (not gnus-dead-summary-mode))))
11479         (push buffer buffers)))
11480     ;; Go through all these summary buffers and offer to save them.
11481     (when buffers
11482       (save-excursion
11483         (map-y-or-n-p
11484          "Update summary buffer %s? "
11485          (lambda (buf)
11486            (switch-to-buffer buf)
11487            (gnus-summary-exit))
11488          buffers)))))
11489
11490 (defun gnus-summary-setup-default-charset ()
11491   "Setup newsgroup default charset."
11492   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11493       (setq gnus-newsgroup-charset nil)
11494     (let* ((ignored-charsets
11495             (or gnus-newsgroup-ephemeral-ignored-charsets
11496                 (append
11497                  (and gnus-newsgroup-name
11498                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11499                  gnus-newsgroup-ignored-charsets))))
11500       (setq gnus-newsgroup-charset
11501             (or gnus-newsgroup-ephemeral-charset
11502                 (and gnus-newsgroup-name
11503                      (gnus-parameter-charset gnus-newsgroup-name))
11504                 gnus-default-charset))
11505       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11506            ignored-charsets))))
11507
11508 ;;;
11509 ;;; Mime Commands
11510 ;;;
11511
11512 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11513   "Display the current article buffer fully MIME-buttonized.
11514 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11515 treated as multipart/mixed."
11516   (interactive "P")
11517   (require 'gnus-art)
11518   (let ((gnus-unbuttonized-mime-types nil)
11519         (gnus-mime-display-multipart-as-mixed show-all-parts))
11520     (gnus-summary-show-article)))
11521
11522 (defun gnus-summary-repair-multipart (article)
11523   "Add a Content-Type header to a multipart article without one."
11524   (interactive (list (gnus-summary-article-number)))
11525   (gnus-with-article article
11526     (message-narrow-to-head)
11527     (message-remove-header "Mime-Version")
11528     (goto-char (point-max))
11529     (insert "Mime-Version: 1.0\n")
11530     (widen)
11531     (when (search-forward "\n--" nil t)
11532       (let ((separator (buffer-substring (point) (point-at-eol))))
11533         (message-narrow-to-head)
11534         (message-remove-header "Content-Type")
11535         (goto-char (point-max))
11536         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11537                         separator))
11538         (widen))))
11539   (let (gnus-mark-article-hook)
11540     (gnus-summary-select-article t t nil article)))
11541
11542 (defun gnus-summary-toggle-display-buttonized ()
11543   "Toggle the buttonizing of the article buffer."
11544   (interactive)
11545   (require 'gnus-art)
11546   (if (setq gnus-inhibit-mime-unbuttonizing
11547             (not gnus-inhibit-mime-unbuttonizing))
11548       (let ((gnus-unbuttonized-mime-types nil))
11549         (gnus-summary-show-article))
11550     (gnus-summary-show-article)))
11551
11552 ;;;
11553 ;;; Generic summary marking commands
11554 ;;;
11555
11556 (defvar gnus-summary-marking-alist
11557   '((read gnus-del-mark "d")
11558     (unread gnus-unread-mark "u")
11559     (ticked gnus-ticked-mark "!")
11560     (dormant gnus-dormant-mark "?")
11561     (expirable gnus-expirable-mark "e"))
11562   "An alist of names/marks/keystrokes.")
11563
11564 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11565 (defvar gnus-summary-mark-map)
11566
11567 (defun gnus-summary-make-all-marking-commands ()
11568   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11569   (dolist (elem gnus-summary-marking-alist)
11570     (apply 'gnus-summary-make-marking-command elem)))
11571
11572 (defun gnus-summary-make-marking-command (name mark keystroke)
11573   (let ((map (make-sparse-keymap)))
11574     (define-key gnus-summary-generic-mark-map keystroke map)
11575     (dolist (lway `((next "next" next nil "n")
11576                     (next-unread "next unread" next t "N")
11577                     (prev "previous" prev nil "p")
11578                     (prev-unread "previous unread" prev t "P")
11579                     (nomove "" nil nil ,keystroke)))
11580       (let ((func (gnus-summary-make-marking-command-1
11581                    mark (car lway) lway name)))
11582         (setq func (eval func))
11583         (define-key map (nth 4 lway) func)))))
11584
11585 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11586   `(defun ,(intern
11587             (format "gnus-summary-put-mark-as-%s%s"
11588                     name (if (eq way 'nomove)
11589                              ""
11590                            (concat "-" (symbol-name way)))))
11591      (n)
11592      ,(format
11593        "Mark the current article as %s%s.
11594 If N, the prefix, then repeat N times.
11595 If N is negative, move in reverse order.
11596 The difference between N and the actual number of articles marked is
11597 returned."
11598        name (cadr lway))
11599      (interactive "p")
11600      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11601
11602 (defun gnus-summary-generic-mark (n mark move unread)
11603   "Mark N articles with MARK."
11604   (unless (eq major-mode 'gnus-summary-mode)
11605     (error "This command can only be used in the summary buffer"))
11606   (gnus-summary-show-thread)
11607   (let ((nummove
11608          (cond
11609           ((eq move 'next) 1)
11610           ((eq move 'prev) -1)
11611           (t 0))))
11612     (if (zerop nummove)
11613         (setq n 1)
11614       (when (< n 0)
11615         (setq n (abs n)
11616               nummove (* -1 nummove))))
11617     (while (and (> n 0)
11618                 (gnus-summary-mark-article nil mark)
11619                 (zerop (gnus-summary-next-subject nummove unread t)))
11620       (setq n (1- n)))
11621     (when (/= 0 n)
11622       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11623     (gnus-summary-recenter)
11624     (gnus-summary-position-point)
11625     (gnus-set-mode-line 'summary)
11626     n))
11627
11628 (defun gnus-summary-insert-articles (articles)
11629   (when (setq articles
11630               (gnus-sorted-difference articles
11631                                       (mapcar (lambda (h)
11632                                                 (mail-header-number h))
11633                                               gnus-newsgroup-headers)))
11634     (setq gnus-newsgroup-headers
11635           (gnus-merge 'list
11636                       gnus-newsgroup-headers
11637                       (gnus-fetch-headers articles)
11638                       'gnus-article-sort-by-number))
11639     ;; Suppress duplicates?
11640     (when gnus-suppress-duplicates
11641       (gnus-dup-suppress-articles))
11642
11643     ;; We might want to build some more threads first.
11644     (when (and gnus-fetch-old-headers
11645                (eq gnus-headers-retrieved-by 'nov))
11646       (if (eq gnus-fetch-old-headers 'invisible)
11647           (gnus-build-all-threads)
11648         (gnus-build-old-threads)))
11649     ;; Let the Gnus agent mark articles as read.
11650     (when gnus-agent
11651       (gnus-agent-get-undownloaded-list))
11652     ;; Remove list identifiers from subject
11653     (when gnus-list-identifiers
11654       (gnus-summary-remove-list-identifiers))
11655     ;; First and last article in this newsgroup.
11656     (when gnus-newsgroup-headers
11657       (setq gnus-newsgroup-begin
11658             (mail-header-number (car gnus-newsgroup-headers))
11659             gnus-newsgroup-end
11660             (mail-header-number
11661              (gnus-last-element gnus-newsgroup-headers))))
11662     (when gnus-use-scoring
11663       (gnus-possibly-score-headers))))
11664
11665 (defun gnus-summary-insert-old-articles (&optional all)
11666   "Insert all old articles in this group.
11667 If ALL is non-nil, already read articles become readable.
11668 If ALL is a number, fetch this number of articles."
11669   (interactive "P")
11670   (prog1
11671       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11672             older len)
11673         (setq older
11674               ;; Some nntp servers lie about their active range.  When
11675               ;; this happens, the active range can be in the millions.
11676               ;; Use a compressed range to avoid creating a huge list.
11677               (gnus-range-difference (list gnus-newsgroup-active) old))
11678         (setq len (gnus-range-length older))
11679         (cond
11680          ((null older) nil)
11681          ((numberp all)
11682           (if (< all len)
11683               (let ((older-range (nreverse older)))
11684                 (setq older nil)
11685
11686                 (while (> all 0)
11687                   (let* ((r (pop older-range))
11688                          (min (if (numberp r) r (car r)))
11689                          (max (if (numberp r) r (cdr r))))
11690                     (while (and (<= min max)
11691                                 (> all 0))
11692                       (push max older)
11693                       (setq all (1- all)
11694                             max (1- max))))))
11695             (setq older (gnus-uncompress-range older))))
11696          (all
11697           (setq older (gnus-uncompress-range older)))
11698          (t
11699           (when (and (numberp gnus-large-newsgroup)
11700                    (> len gnus-large-newsgroup))
11701               (let* ((cursor-in-echo-area nil)
11702                      (initial (gnus-parameter-large-newsgroup-initial
11703                                gnus-newsgroup-name))
11704                      (input
11705                       (read-string
11706                        (format
11707                         "How many articles from %s (%s %d): "
11708                         (gnus-limit-string
11709                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11710                         (if initial "max" "default")
11711                         len)
11712                        (if initial
11713                            (cons (number-to-string initial)
11714                                  0)))))
11715                 (unless (string-match "^[ \t]*$" input)
11716                   (setq all (string-to-number input))
11717                   (if (< all len)
11718                       (let ((older-range (nreverse older)))
11719                         (setq older nil)
11720
11721                         (while (> all 0)
11722                           (let* ((r (pop older-range))
11723                                  (min (if (numberp r) r (car r)))
11724                                  (max (if (numberp r) r (cdr r))))
11725                             (while (and (<= min max)
11726                                         (> all 0))
11727                               (push max older)
11728                               (setq all (1- all)
11729                                     max (1- max))))))))))
11730           (setq older (gnus-uncompress-range older))))
11731         (if (not older)
11732             (message "No old news.")
11733           (gnus-summary-insert-articles older)
11734           (gnus-summary-limit (gnus-sorted-nunion old older))))
11735     (gnus-summary-position-point)))
11736
11737 (defun gnus-summary-insert-new-articles ()
11738   "Insert all new articles in this group."
11739   (interactive)
11740   (prog1
11741       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11742             (old-active gnus-newsgroup-active)
11743             (nnmail-fetched-sources (list t))
11744             i new)
11745         (setq gnus-newsgroup-active
11746               (gnus-activate-group gnus-newsgroup-name 'scan))
11747         (setq i (cdr gnus-newsgroup-active))
11748         (while (> i (cdr old-active))
11749           (push i new)
11750           (decf i))
11751         (if (not new)
11752             (message "No gnus is bad news")
11753           (gnus-summary-insert-articles new)
11754           (setq gnus-newsgroup-unreads
11755                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11756           (gnus-summary-limit (gnus-sorted-nunion old new))))
11757     (gnus-summary-position-point)))
11758
11759 (gnus-summary-make-all-marking-commands)
11760
11761 (gnus-ems-redefine)
11762
11763 (provide 'gnus-sum)
11764
11765 (run-hooks 'gnus-sum-load-hook)
11766
11767 ;; Local Variables:
11768 ;; coding: iso-8859-1
11769 ;; End:
11770
11771 ;;; gnus-sum.el ends here