Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-99
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary 2
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715
716 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
717   "*List of functions used for sorting articles in the summary buffer.
718
719 Each function takes two articles and returns non-nil if the first
720 article should be sorted before the other.  If you use more than one
721 function, the primary sort function should be the last.  You should
722 probably always include `gnus-article-sort-by-number' in the list of
723 sorting functions -- preferably first.  Also note that sorting by date
724 is often much slower than sorting by number, and the sorting order is
725 very similar.  (Sorting by date means sorting by the time the message
726 was sent, sorting by number means sorting by arrival time.)
727
728 Each item can also be a list `(not F)' where F is a function;
729 this reverses the sort order.
730
731 Ready-made functions include `gnus-article-sort-by-number',
732 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
733 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
734 and `gnus-article-sort-by-score'.
735
736 When threading is turned on, the variable `gnus-thread-sort-functions'
737 controls how articles are sorted."
738   :group 'gnus-summary-sort
739   :type '(repeat (gnus-widget-reversible
740                   (choice (function-item gnus-article-sort-by-number)
741                           (function-item gnus-article-sort-by-author)
742                           (function-item gnus-article-sort-by-subject)
743                           (function-item gnus-article-sort-by-date)
744                           (function-item gnus-article-sort-by-score)
745                           (function-item gnus-article-sort-by-random)
746                           (function :tag "other"))
747                   (boolean :tag "Reverse order"))))
748
749
750 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
751   "*List of functions used for sorting threads in the summary buffer.
752 By default, threads are sorted by article number.
753
754 Each function takes two threads and returns non-nil if the first
755 thread should be sorted before the other.  If you use more than one
756 function, the primary sort function should be the last.  You should
757 probably always include `gnus-thread-sort-by-number' in the list of
758 sorting functions -- preferably first.  Also note that sorting by date
759 is often much slower than sorting by number, and the sorting order is
760 very similar.  (Sorting by date means sorting by the time the message
761 was sent, sorting by number means sorting by arrival time.)
762
763 Each list item can also be a list `(not F)' where F is a
764 function; this specifies reversed sort order.
765
766 Ready-made functions include `gnus-thread-sort-by-number',
767 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
768 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
769 `gnus-thread-sort-by-most-recent-number',
770 `gnus-thread-sort-by-most-recent-date',
771 `gnus-thread-sort-by-random', and
772 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
773
774 When threading is turned off, the variable
775 `gnus-article-sort-functions' controls how articles are sorted."
776   :group 'gnus-summary-sort
777   :type '(repeat 
778           (gnus-widget-reversible
779            (choice (function-item gnus-thread-sort-by-number)
780                    (function-item gnus-thread-sort-by-author)
781                    (function-item gnus-thread-sort-by-subject)
782                    (function-item gnus-thread-sort-by-date)
783                    (function-item gnus-thread-sort-by-score)
784                    (function-item gnus-thread-sort-by-most-recent-number)
785                    (function-item gnus-thread-sort-by-most-recent-date)
786                    (function-item gnus-thread-sort-by-random)
787                    (function-item gnus-thread-sort-by-total-score)
788                    (function :tag "other"))
789            (boolean :tag "Reverse order"))))
790
791 (defcustom gnus-thread-score-function '+
792   "*Function used for calculating the total score of a thread.
793
794 The function is called with the scores of the article and each
795 subthread and should then return the score of the thread.
796
797 Some functions you can use are `+', `max', or `min'."
798   :group 'gnus-summary-sort
799   :type 'function)
800
801 (defcustom gnus-summary-expunge-below nil
802   "All articles that have a score less than this variable will be expunged.
803 This variable is local to the summary buffers."
804   :group 'gnus-score-default
805   :type '(choice (const :tag "off" nil)
806                  integer))
807
808 (defcustom gnus-thread-expunge-below nil
809   "All threads that have a total score less than this variable will be expunged.
810 See `gnus-thread-score-function' for en explanation of what a
811 \"thread score\" is.
812
813 This variable is local to the summary buffers."
814   :group 'gnus-threading
815   :group 'gnus-score-default
816   :type '(choice (const :tag "off" nil)
817                  integer))
818
819 (defcustom gnus-summary-mode-hook nil
820   "*A hook for Gnus summary mode.
821 This hook is run before any variables are set in the summary buffer."
822   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
823   :group 'gnus-summary-various
824   :type 'hook)
825
826 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
827 (when (featurep 'xemacs)
828   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
830   (add-hook 'gnus-summary-mode-hook
831             'gnus-xmas-switch-horizontal-scrollbar-off))
832
833 (defcustom gnus-summary-menu-hook nil
834   "*Hook run after the creation of the summary mode menu."
835   :group 'gnus-summary-visual
836   :type 'hook)
837
838 (defcustom gnus-summary-exit-hook nil
839   "*A hook called on exit from the summary buffer.
840 It will be called with point in the group buffer."
841   :group 'gnus-summary-exit
842   :type 'hook)
843
844 (defcustom gnus-summary-prepare-hook nil
845   "*A hook called after the summary buffer has been generated.
846 If you want to modify the summary buffer, you can use this hook."
847   :group 'gnus-summary-various
848   :type 'hook)
849
850 (defcustom gnus-summary-prepared-hook nil
851   "*A hook called as the last thing after the summary buffer has been generated."
852   :group 'gnus-summary-various
853   :type 'hook)
854
855 (defcustom gnus-summary-generate-hook nil
856   "*A hook run just before generating the summary buffer.
857 This hook is commonly used to customize threading variables and the
858 like."
859   :group 'gnus-summary-various
860   :type 'hook)
861
862 (defcustom gnus-select-group-hook nil
863   "*A hook called when a newsgroup is selected.
864
865 If you'd like to simplify subjects like the
866 `gnus-summary-next-same-subject' command does, you can use the
867 following hook:
868
869  (add-hook gnus-select-group-hook
870            (lambda ()
871              (mapcar (lambda (header)
872                        (mail-header-set-subject
873                         header
874                         (gnus-simplify-subject
875                          (mail-header-subject header) 're-only)))
876                      gnus-newsgroup-headers)))"
877   :group 'gnus-group-select
878   :type 'hook)
879
880 (defcustom gnus-select-article-hook nil
881   "*A hook called when an article is selected."
882   :group 'gnus-summary-choose
883   :options '(gnus-agent-fetch-selected-article)
884   :type 'hook)
885
886 (defcustom gnus-visual-mark-article-hook
887   (list 'gnus-highlight-selected-summary)
888   "*Hook run after selecting an article in the summary buffer.
889 It is meant to be used for highlighting the article in some way.  It
890 is not run if `gnus-visual' is nil."
891   :group 'gnus-summary-visual
892   :type 'hook)
893
894 (defcustom gnus-parse-headers-hook nil
895   "*A hook called before parsing the headers."
896   :group 'gnus-various
897   :type 'hook)
898
899 (defcustom gnus-exit-group-hook nil
900   "*A hook called when exiting summary mode.
901 This hook is not called from the non-updating exit commands like `Q'."
902   :group 'gnus-various
903   :type 'hook)
904
905 (defcustom gnus-summary-update-hook
906   (list 'gnus-summary-highlight-line)
907   "*A hook called when a summary line is changed.
908 The hook will not be called if `gnus-visual' is nil.
909
910 The default function `gnus-summary-highlight-line' will
911 highlight the line according to the `gnus-summary-highlight'
912 variable."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
917   "*A hook called when an article is selected for the first time.
918 The hook is intended to mark an article as read (or unread)
919 automatically when it is selected."
920   :group 'gnus-summary-choose
921   :type 'hook)
922
923 (defcustom gnus-group-no-more-groups-hook nil
924   "*A hook run when returning to group mode having no more (unread) groups."
925   :group 'gnus-group-select
926   :type 'hook)
927
928 (defcustom gnus-ps-print-hook nil
929   "*A hook run before ps-printing something from Gnus."
930   :group 'gnus-summary
931   :type 'hook)
932
933 (defcustom gnus-summary-article-move-hook nil
934   "*A hook called after an article is moved, copied, respooled, or crossposted."
935   :group 'gnus-summary
936   :type 'hook)
937
938 (defcustom gnus-summary-article-delete-hook nil
939   "*A hook called after an article is deleted."
940   :group 'gnus-summary
941   :type 'hook)
942
943 (defcustom gnus-summary-article-expire-hook nil
944   "*A hook called after an article is expired."
945   :group 'gnus-summary
946   :type 'hook)
947
948 (defcustom gnus-summary-display-arrow
949   (and (fboundp 'display-graphic-p)
950        (display-graphic-p))
951   "*If non-nil, display an arrow highlighting the current article."
952   :version "21.1"
953   :group 'gnus-summary
954   :type 'boolean)
955
956 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
957   "Face used for highlighting the current article in the summary buffer."
958   :group 'gnus-summary-visual
959   :type 'face)
960
961 (defvar gnus-tmp-downloaded nil)
962
963 (defcustom gnus-summary-highlight
964   '(((eq mark gnus-canceled-mark)
965      . gnus-summary-cancelled-face)
966     ((and uncached (> score default-high))
967      . gnus-summary-high-undownloaded-face)
968     ((and uncached (< score default-low))
969      . gnus-summary-low-undownloaded-face)
970     (uncached
971      . gnus-summary-normal-undownloaded-face)
972     ((and (> score default-high)
973           (or (eq mark gnus-dormant-mark)
974               (eq mark gnus-ticked-mark)))
975      . gnus-summary-high-ticked-face)
976     ((and (< score default-low)
977           (or (eq mark gnus-dormant-mark)
978               (eq mark gnus-ticked-mark)))
979      . gnus-summary-low-ticked-face)
980     ((or (eq mark gnus-dormant-mark)
981          (eq mark gnus-ticked-mark))
982      . gnus-summary-normal-ticked-face)
983     ((and (> score default-high) (eq mark gnus-ancient-mark))
984      . gnus-summary-high-ancient-face)
985     ((and (< score default-low) (eq mark gnus-ancient-mark))
986      . gnus-summary-low-ancient-face)
987     ((eq mark gnus-ancient-mark)
988      . gnus-summary-normal-ancient-face)
989     ((and (> score default-high) (eq mark gnus-unread-mark))
990      . gnus-summary-high-unread-face)
991     ((and (< score default-low) (eq mark gnus-unread-mark))
992      . gnus-summary-low-unread-face)
993     ((eq mark gnus-unread-mark)
994      . gnus-summary-normal-unread-face)
995     ((> score default-high)
996      . gnus-summary-high-read-face)
997     ((< score default-low)
998      . gnus-summary-low-read-face)
999     (t
1000      . gnus-summary-normal-read-face))
1001   "*Controls the highlighting of summary buffer lines.
1002
1003 A list of (FORM . FACE) pairs.  When deciding how a a particular
1004 summary line should be displayed, each form is evaluated.  The content
1005 of the face field after the first true form is used.  You can change
1006 how those summary lines are displayed, by editing the face field.
1007
1008 You can use the following variables in the FORM field.
1009
1010 score:        The article's score
1011 default:      The default article score.
1012 default-high: The default score for high scored articles.
1013 default-low:  The default score for low scored articles.
1014 below:        The score below which articles are automatically marked as read.
1015 mark:         The article's mark.
1016 uncached:     Non-nil if the article is uncached."
1017   :group 'gnus-summary-visual
1018   :type '(repeat (cons (sexp :tag "Form" nil)
1019                        face)))
1020
1021 (defcustom gnus-alter-header-function nil
1022   "Function called to allow alteration of article header structures.
1023 The function is called with one parameter, the article header vector,
1024 which it may alter in any way."
1025   :type '(choice (const :tag "None" nil)
1026                  function)
1027   :group 'gnus-summary)
1028
1029 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1030   "Variable that says which function should be used to decode a string with encoded words.")
1031
1032 (defcustom gnus-extra-headers '(To Newsgroups)
1033   "*Extra headers to parse."
1034   :version "21.1"
1035   :group 'gnus-summary
1036   :type '(repeat symbol))
1037
1038 (defcustom gnus-ignored-from-addresses
1039   (and user-mail-address (regexp-quote user-mail-address))
1040   "*Regexp of From headers that may be suppressed in favor of To headers."
1041   :version "21.1"
1042   :group 'gnus-summary
1043   :type 'regexp)
1044
1045 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1046   "List of charsets that should be ignored.
1047 When these charsets are used in the \"charset\" parameter, the
1048 default charset will be used instead."
1049   :version "21.1"
1050   :type '(repeat symbol)
1051   :group 'gnus-charset)
1052
1053 (gnus-define-group-parameter
1054  ignored-charsets
1055  :type list
1056  :function-document
1057  "Return the ignored charsets of GROUP."
1058  :variable gnus-group-ignored-charsets-alist
1059  :variable-default
1060  '(("alt\\.chinese\\.text" iso-8859-1))
1061  :variable-document
1062  "Alist of regexps (to match group names) and charsets that should be ignored.
1063 When these charsets are used in the \"charset\" parameter, the
1064 default charset will be used instead."
1065  :variable-group gnus-charset
1066  :variable-type '(repeat (cons (regexp :tag "Group")
1067                                (repeat symbol)))
1068  :parameter-type '(choice :tag "Ignored charsets"
1069                           :value nil
1070                           (repeat (symbol)))
1071  :parameter-document       "\
1072 List of charsets that should be ignored.
1073
1074 When these charsets are used in the \"charset\" parameter, the
1075 default charset will be used instead.")
1076
1077 (defcustom gnus-group-highlight-words-alist nil
1078   "Alist of group regexps and highlight regexps.
1079 This variable uses the same syntax as `gnus-emphasis-alist'."
1080   :version "21.1"
1081   :type '(repeat (cons (regexp :tag "Group")
1082                        (repeat (list (regexp :tag "Highlight regexp")
1083                                      (number :tag "Group for entire word" 0)
1084                                      (number :tag "Group for displayed part" 0)
1085                                      (symbol :tag "Face"
1086                                              gnus-emphasis-highlight-words)))))
1087   :group 'gnus-summary-visual)
1088
1089 (defcustom gnus-summary-show-article-charset-alist
1090   nil
1091   "Alist of number and charset.
1092 The article will be shown with the charset corresponding to the
1093 numbered argument.
1094 For example: ((1 . cn-gb-2312) (2 . big5))."
1095   :version "21.1"
1096   :type '(repeat (cons (number :tag "Argument" 1)
1097                        (symbol :tag "Charset")))
1098   :group 'gnus-charset)
1099
1100 (defcustom gnus-preserve-marks t
1101   "Whether marks are preserved when moving, copying and respooling messages."
1102   :version "21.1"
1103   :type 'boolean
1104   :group 'gnus-summary-marks)
1105
1106 (defcustom gnus-alter-articles-to-read-function nil
1107   "Function to be called to alter the list of articles to be selected."
1108   :type '(choice (const nil) function)
1109   :group 'gnus-summary)
1110
1111 (defcustom gnus-orphan-score nil
1112   "*All orphans get this score added.  Set in the score file."
1113   :group 'gnus-score-default
1114   :type '(choice (const nil)
1115                  integer))
1116
1117 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1118   "*A regexp to match MIME parts when saving multiple parts of a
1119 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1120 This regexp will be used by default when prompting the user for which
1121 type of files to save."
1122   :group 'gnus-summary
1123   :type 'regexp)
1124
1125 (defcustom gnus-read-all-available-headers nil
1126   "Whether Gnus should parse all headers made available to it.
1127 This is mostly relevant for slow back ends where the user may
1128 wish to widen the summary buffer to include all headers
1129 that were fetched.  Say, for nnultimate groups."
1130   :group 'gnus-summary
1131   :type '(choice boolean regexp))
1132
1133 (defcustom gnus-summary-muttprint-program "muttprint"
1134   "Command (and optional arguments) used to run Muttprint."
1135   :version "21.3"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-article-loose-mime t
1140   "If non-nil, don't require MIME-Version header.
1141 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1142 supply the MIME-Version header or deliberately strip it from the mail.
1143 If non-nil (the default), Gnus will treat some articles as MIME
1144 even if the MIME-Version header is missing."
1145   :version "21.3"
1146   :type 'boolean
1147   :group 'gnus-article-mime)
1148
1149 (defcustom gnus-article-emulate-mime t
1150   "If non-nil, use MIME emulation for uuencode and the like.
1151 This means that Gnus will search message bodies for text that look
1152 like uuencoded bits, yEncoded bits, and so on, and present that using
1153 the normal Gnus MIME machinery."
1154   :type 'boolean
1155   :group 'gnus-article-mime)
1156
1157 ;;; Internal variables
1158
1159 (defvar gnus-summary-display-cache nil)
1160 (defvar gnus-article-mime-handles nil)
1161 (defvar gnus-article-decoded-p nil)
1162 (defvar gnus-article-charset nil)
1163 (defvar gnus-article-ignored-charsets nil)
1164 (defvar gnus-scores-exclude-files nil)
1165 (defvar gnus-page-broken nil)
1166
1167 (defvar gnus-original-article nil)
1168 (defvar gnus-article-internal-prepare-hook nil)
1169 (defvar gnus-newsgroup-process-stack nil)
1170
1171 (defvar gnus-thread-indent-array nil)
1172 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1173 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1174   "Function called to sort the articles within a thread after it has been gathered together.")
1175
1176 (defvar gnus-summary-save-parts-type-history nil)
1177 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1178
1179 ;; Avoid highlighting in kill files.
1180 (defvar gnus-summary-inhibit-highlight nil)
1181 (defvar gnus-newsgroup-selected-overlay nil)
1182 (defvar gnus-inhibit-limiting nil)
1183 (defvar gnus-newsgroup-adaptive-score-file nil)
1184 (defvar gnus-current-score-file nil)
1185 (defvar gnus-current-move-group nil)
1186 (defvar gnus-current-copy-group nil)
1187 (defvar gnus-current-crosspost-group nil)
1188 (defvar gnus-newsgroup-display nil)
1189
1190 (defvar gnus-newsgroup-dependencies nil)
1191 (defvar gnus-newsgroup-adaptive nil)
1192 (defvar gnus-summary-display-article-function nil)
1193 (defvar gnus-summary-highlight-line-function nil
1194   "Function called after highlighting a summary line.")
1195
1196 (defvar gnus-summary-line-format-alist
1197   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1198     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1199     (?s gnus-tmp-subject-or-nil ?s)
1200     (?n gnus-tmp-name ?s)
1201     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1202         ?s)
1203     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1204             gnus-tmp-from) ?s)
1205     (?F gnus-tmp-from ?s)
1206     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1207     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1208     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1209     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1210     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1211     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1212     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1213     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1214     (?L gnus-tmp-lines ?s)
1215     (?O gnus-tmp-downloaded ?c)
1216     (?I gnus-tmp-indentation ?s)
1217     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1218     (?R gnus-tmp-replied ?c)
1219     (?\[ gnus-tmp-opening-bracket ?c)
1220     (?\] gnus-tmp-closing-bracket ?c)
1221     (?\> (make-string gnus-tmp-level ? ) ?s)
1222     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1223     (?i gnus-tmp-score ?d)
1224     (?z gnus-tmp-score-char ?c)
1225     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1226     (?U gnus-tmp-unread ?c)
1227     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1228         ?s)
1229     (?t (gnus-summary-number-of-articles-in-thread
1230          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1231         ?d)
1232     (?e (gnus-summary-number-of-articles-in-thread
1233          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1234         ?c)
1235     (?u gnus-tmp-user-defined ?s)
1236     (?P (gnus-pick-line-number) ?d)
1237     (?B gnus-tmp-thread-tree-header-string ?s)
1238     (user-date (gnus-user-date
1239                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1240   "An alist of format specifications that can appear in summary lines.
1241 These are paired with what variables they correspond with, along with
1242 the type of the variable (string, integer, character, etc).")
1243
1244 (defvar gnus-summary-dummy-line-format-alist
1245   `((?S gnus-tmp-subject ?s)
1246     (?N gnus-tmp-number ?d)
1247     (?u gnus-tmp-user-defined ?s)))
1248
1249 (defvar gnus-summary-mode-line-format-alist
1250   `((?G gnus-tmp-group-name ?s)
1251     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1252     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1253     (?A gnus-tmp-article-number ?d)
1254     (?Z gnus-tmp-unread-and-unselected ?s)
1255     (?V gnus-version ?s)
1256     (?U gnus-tmp-unread-and-unticked ?d)
1257     (?S gnus-tmp-subject ?s)
1258     (?e gnus-tmp-unselected ?d)
1259     (?u gnus-tmp-user-defined ?s)
1260     (?d (length gnus-newsgroup-dormant) ?d)
1261     (?t (length gnus-newsgroup-marked) ?d)
1262     (?h (length gnus-newsgroup-spam-marked) ?d)
1263     (?r (length gnus-newsgroup-reads) ?d)
1264     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1265     (?E gnus-newsgroup-expunged-tally ?d)
1266     (?s (gnus-current-score-file-nondirectory) ?s)))
1267
1268 (defvar gnus-last-search-regexp nil
1269   "Default regexp for article search command.")
1270
1271 (defvar gnus-last-shell-command nil
1272   "Default shell command on article.")
1273
1274 (defvar gnus-newsgroup-agentized nil
1275   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1276 (defvar gnus-newsgroup-begin nil)
1277 (defvar gnus-newsgroup-end nil)
1278 (defvar gnus-newsgroup-last-rmail nil)
1279 (defvar gnus-newsgroup-last-mail nil)
1280 (defvar gnus-newsgroup-last-folder nil)
1281 (defvar gnus-newsgroup-last-file nil)
1282 (defvar gnus-newsgroup-auto-expire nil)
1283 (defvar gnus-newsgroup-active nil)
1284
1285 (defvar gnus-newsgroup-data nil)
1286 (defvar gnus-newsgroup-data-reverse nil)
1287 (defvar gnus-newsgroup-limit nil)
1288 (defvar gnus-newsgroup-limits nil)
1289 (defvar gnus-summary-use-undownloaded-faces nil)
1290
1291 (defvar gnus-newsgroup-unreads nil
1292   "Sorted list of unread articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unselected nil
1295   "Sorted list of unselected unread articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-reads nil
1298   "Alist of read articles and article marks in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-expunged-tally nil)
1301
1302 (defvar gnus-newsgroup-marked nil
1303   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1304
1305 (defvar gnus-newsgroup-spam-marked nil
1306   "List of ranges of articles that have been marked as spam.")
1307
1308 (defvar gnus-newsgroup-killed nil
1309   "List of ranges of articles that have been through the scoring process.")
1310
1311 (defvar gnus-newsgroup-cached nil
1312   "Sorted list of articles that come from the article cache.")
1313
1314 (defvar gnus-newsgroup-saved nil
1315   "List of articles that have been saved.")
1316
1317 (defvar gnus-newsgroup-kill-headers nil)
1318
1319 (defvar gnus-newsgroup-replied nil
1320   "List of articles that have been replied to in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-forwarded nil
1323   "List of articles that have been forwarded in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-recent nil
1326   "List of articles that have are recent in the current newsgroup.")
1327
1328 (defvar gnus-newsgroup-expirable nil
1329   "Sorted list of articles in the current newsgroup that can be expired.")
1330
1331 (defvar gnus-newsgroup-processable nil
1332   "List of articles in the current newsgroup that can be processed.")
1333
1334 (defvar gnus-newsgroup-downloadable nil
1335   "Sorted list of articles in the current newsgroup that can be processed.")
1336
1337 (defvar gnus-newsgroup-unfetched nil
1338   "Sorted list of articles in the current newsgroup whose headers have
1339 not been fetched into the agent.
1340
1341 This list will always be a subset of gnus-newsgroup-undownloaded.")
1342
1343 (defvar gnus-newsgroup-undownloaded nil
1344   "List of articles in the current newsgroup that haven't been downloaded.")
1345
1346 (defvar gnus-newsgroup-unsendable nil
1347   "List of articles in the current newsgroup that won't be sent.")
1348
1349 (defvar gnus-newsgroup-bookmarks nil
1350   "List of articles in the current newsgroup that have bookmarks.")
1351
1352 (defvar gnus-newsgroup-dormant nil
1353   "Sorted list of dormant articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-unseen nil
1356   "List of unseen articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-seen nil
1359   "Range of seen articles in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-articles nil
1362   "List of articles in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-scored nil
1365   "List of scored articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-headers nil
1368   "List of article headers in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-threads nil)
1371
1372 (defvar gnus-newsgroup-prepared nil
1373   "Whether the current group has been prepared properly.")
1374
1375 (defvar gnus-newsgroup-ancient nil
1376   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-sparse nil)
1379
1380 (defvar gnus-current-article nil)
1381 (defvar gnus-article-current nil)
1382 (defvar gnus-current-headers nil)
1383 (defvar gnus-have-all-headers nil)
1384 (defvar gnus-last-article nil)
1385 (defvar gnus-newsgroup-history nil)
1386 (defvar gnus-newsgroup-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1389
1390 (defvar gnus-article-before-search nil)
1391
1392 (defvar gnus-summary-local-variables
1393   '(gnus-newsgroup-name
1394     gnus-newsgroup-begin gnus-newsgroup-end
1395     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1396     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1397     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1398     gnus-newsgroup-unselected gnus-newsgroup-marked
1399     gnus-newsgroup-spam-marked
1400     gnus-newsgroup-reads gnus-newsgroup-saved
1401     gnus-newsgroup-replied gnus-newsgroup-forwarded
1402     gnus-newsgroup-recent
1403     gnus-newsgroup-expirable
1404     gnus-newsgroup-processable gnus-newsgroup-killed
1405     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1406     gnus-newsgroup-unfetched
1407     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1408     gnus-newsgroup-seen gnus-newsgroup-articles
1409     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1410     gnus-newsgroup-headers gnus-newsgroup-threads
1411     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1412     gnus-current-article gnus-current-headers gnus-have-all-headers
1413     gnus-last-article gnus-article-internal-prepare-hook
1414     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1415     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1416     gnus-thread-expunge-below
1417     gnus-score-alist gnus-current-score-file
1418     (gnus-summary-expunge-below . global)
1419     (gnus-summary-mark-below . global)
1420     (gnus-orphan-score . global)
1421     gnus-newsgroup-active gnus-scores-exclude-files
1422     gnus-newsgroup-history gnus-newsgroup-ancient
1423     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1424     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1425     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1426     (gnus-newsgroup-expunged-tally . 0)
1427     gnus-cache-removable-articles gnus-newsgroup-cached
1428     gnus-newsgroup-data gnus-newsgroup-data-reverse
1429     gnus-newsgroup-limit gnus-newsgroup-limits
1430     gnus-newsgroup-charset gnus-newsgroup-display
1431     gnus-summary-use-undownloaded-faces)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default expressions to be evalled (when the default
1438 values are not nil), that should be made global while the summary buffer
1439 is active.
1440
1441 Note: The default expressions will be evaluated (using function `eval')
1442 before assignment to the local variable rather than just assigned to it.
1443 If the default expression is the symbol `global', that symbol will not
1444 be evaluated but the global value of the local variable will be used
1445 instead.
1446
1447 These variables can be used to set variables in the group parameters
1448 while still allowing them to affect operations done in other buffers.
1449 For example:
1450
1451 \(setq gnus-newsgroup-variables
1452      '(message-use-followup-to
1453        (gnus-visible-headers .
1454          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1455 ")
1456
1457 ;; Byte-compiler warning.
1458 (eval-when-compile
1459   ;; Bind features so that require will believe that gnus-sum has
1460   ;; already been loaded (avoids infinite recursion)
1461   (let ((features (cons 'gnus-sum features)))
1462     ;; Several of the declarations in gnus-sum are needed to load the
1463     ;; following files. Right now, these definitions have been
1464     ;; compiled but not defined (evaluated).  We could either do a
1465     ;; eval-and-compile about all of the declarations or evaluate the
1466     ;; source file.
1467     (if (boundp 'gnus-newsgroup-variables)
1468         nil
1469       (load "gnus-sum.el" t t t))
1470     (require 'gnus)
1471     (require 'gnus-art)))
1472
1473 ;; MIME stuff.
1474
1475 (defvar gnus-decode-encoded-word-methods
1476   '(mail-decode-encoded-word-string)
1477   "List of methods used to decode encoded words.
1478
1479 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1480 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1481 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1482 whose names match REGEXP.
1483
1484 For example:
1485 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1486  mail-decode-encoded-word-string
1487  (\"chinese\" . rfc1843-decode-string))")
1488
1489 (defvar gnus-decode-encoded-word-methods-cache nil)
1490
1491 (defun gnus-multi-decode-encoded-word-string (string)
1492   "Apply the functions from `gnus-encoded-word-methods' that match."
1493   (unless (and gnus-decode-encoded-word-methods-cache
1494                (eq gnus-newsgroup-name
1495                    (car gnus-decode-encoded-word-methods-cache)))
1496     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1497     (mapcar (lambda (x)
1498               (if (symbolp x)
1499                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1500                 (if (and gnus-newsgroup-name
1501                          (string-match (car x) gnus-newsgroup-name))
1502                     (nconc gnus-decode-encoded-word-methods-cache
1503                            (list (cdr x))))))
1504             gnus-decode-encoded-word-methods))
1505   (let ((xlist gnus-decode-encoded-word-methods-cache))
1506     (pop xlist)
1507     (while xlist
1508       (setq string (funcall (pop xlist) string))))
1509   string)
1510
1511 ;; Subject simplification.
1512
1513 (defun gnus-simplify-whitespace (str)
1514   "Remove excessive whitespace from STR."
1515   ;; Multiple spaces.
1516   (while (string-match "[ \t][ \t]+" str)
1517     (setq str (concat (substring str 0 (match-beginning 0))
1518                         " "
1519                         (substring str (match-end 0)))))
1520   ;; Leading spaces.
1521   (when (string-match "^[ \t]+" str)
1522     (setq str (substring str (match-end 0))))
1523   ;; Trailing spaces.
1524   (when (string-match "[ \t]+$" str)
1525     (setq str (substring str 0 (match-beginning 0))))
1526   str)
1527
1528 (defun gnus-simplify-all-whitespace (str)
1529   "Remove all whitespace from STR."
1530   (while (string-match "[ \t\n]+" str)
1531     (setq str (replace-match "" nil nil str)))
1532   str)
1533
1534 (defsubst gnus-simplify-subject-re (subject)
1535   "Remove \"Re:\" from subject lines."
1536   (if (string-match message-subject-re-regexp subject)
1537       (substring subject (match-end 0))
1538     subject))
1539
1540 (defun gnus-simplify-subject (subject &optional re-only)
1541   "Remove `Re:' and words in parentheses.
1542 If RE-ONLY is non-nil, strip leading `Re:'s only."
1543   (let ((case-fold-search t))           ;Ignore case.
1544     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1545     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1546       (setq subject (substring subject (match-end 0))))
1547     ;; Remove uninteresting prefixes.
1548     (when (and (not re-only)
1549                gnus-simplify-ignored-prefixes
1550                (string-match gnus-simplify-ignored-prefixes subject))
1551       (setq subject (substring subject (match-end 0))))
1552     ;; Remove words in parentheses from end.
1553     (unless re-only
1554       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1555         (setq subject (substring subject 0 (match-beginning 0)))))
1556     ;; Return subject string.
1557     subject))
1558
1559 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1560 ;; all whitespace.
1561 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1562   (goto-char (point-min))
1563   (while (re-search-forward regexp nil t)
1564     (replace-match (or newtext ""))))
1565
1566 (defun gnus-simplify-buffer-fuzzy ()
1567   "Simplify string in the buffer fuzzily.
1568 The string in the accessible portion of the current buffer is simplified.
1569 It is assumed to be a single-line subject.
1570 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1571 matter is removed.  Additional things can be deleted by setting
1572 `gnus-simplify-subject-fuzzy-regexp'."
1573   (let ((case-fold-search t)
1574         (modified-tick))
1575     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1576
1577     (while (not (eq modified-tick (buffer-modified-tick)))
1578       (setq modified-tick (buffer-modified-tick))
1579       (cond
1580        ((listp gnus-simplify-subject-fuzzy-regexp)
1581         (mapcar 'gnus-simplify-buffer-fuzzy-step
1582                 gnus-simplify-subject-fuzzy-regexp))
1583        (gnus-simplify-subject-fuzzy-regexp
1584         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1585       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1586       (gnus-simplify-buffer-fuzzy-step
1587        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1588       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1589
1590     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1591     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1592     (gnus-simplify-buffer-fuzzy-step " $")
1593     (gnus-simplify-buffer-fuzzy-step "^ +")))
1594
1595 (defun gnus-simplify-subject-fuzzy (subject)
1596   "Simplify a subject string fuzzily.
1597 See `gnus-simplify-buffer-fuzzy' for details."
1598   (save-excursion
1599     (gnus-set-work-buffer)
1600     (let ((case-fold-search t))
1601       ;; Remove uninteresting prefixes.
1602       (when (and gnus-simplify-ignored-prefixes
1603                  (string-match gnus-simplify-ignored-prefixes subject))
1604         (setq subject (substring subject (match-end 0))))
1605       (insert subject)
1606       (inline (gnus-simplify-buffer-fuzzy))
1607       (buffer-string))))
1608
1609 (defsubst gnus-simplify-subject-fully (subject)
1610   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1611   (cond
1612    (gnus-simplify-subject-functions
1613     (gnus-map-function gnus-simplify-subject-functions subject))
1614    ((null gnus-summary-gather-subject-limit)
1615     (gnus-simplify-subject-re subject))
1616    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1617     (gnus-simplify-subject-fuzzy subject))
1618    ((numberp gnus-summary-gather-subject-limit)
1619     (gnus-limit-string (gnus-simplify-subject-re subject)
1620                        gnus-summary-gather-subject-limit))
1621    (t
1622     subject)))
1623
1624 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1625   "Check whether two subjects are equal.
1626 If optional argument SIMPLE-FIRST is t, first argument is already
1627 simplified."
1628   (cond
1629    ((null simple-first)
1630     (equal (gnus-simplify-subject-fully s1)
1631            (gnus-simplify-subject-fully s2)))
1632    (t
1633     (equal s1
1634            (gnus-simplify-subject-fully s2)))))
1635
1636 (defun gnus-summary-bubble-group ()
1637   "Increase the score of the current group.
1638 This is a handy function to add to `gnus-summary-exit-hook' to
1639 increase the score of each group you read."
1640   (gnus-group-add-score gnus-newsgroup-name))
1641
1642 \f
1643 ;;;
1644 ;;; Gnus summary mode
1645 ;;;
1646
1647 (put 'gnus-summary-mode 'mode-class 'special)
1648
1649 (defvar gnus-article-commands-menu)
1650
1651 ;; Non-orthogonal keys
1652
1653 (gnus-define-keys gnus-summary-mode-map
1654   " " gnus-summary-next-page
1655   "\177" gnus-summary-prev-page
1656   [delete] gnus-summary-prev-page
1657   [backspace] gnus-summary-prev-page
1658   "\r" gnus-summary-scroll-up
1659   "\M-\r" gnus-summary-scroll-down
1660   "n" gnus-summary-next-unread-article
1661   "p" gnus-summary-prev-unread-article
1662   "N" gnus-summary-next-article
1663   "P" gnus-summary-prev-article
1664   "\M-\C-n" gnus-summary-next-same-subject
1665   "\M-\C-p" gnus-summary-prev-same-subject
1666   "\M-n" gnus-summary-next-unread-subject
1667   "\M-p" gnus-summary-prev-unread-subject
1668   "." gnus-summary-first-unread-article
1669   "," gnus-summary-best-unread-article
1670   "\M-s" gnus-summary-search-article-forward
1671   "\M-r" gnus-summary-search-article-backward
1672   "<" gnus-summary-beginning-of-article
1673   ">" gnus-summary-end-of-article
1674   "j" gnus-summary-goto-article
1675   "^" gnus-summary-refer-parent-article
1676   "\M-^" gnus-summary-refer-article
1677   "u" gnus-summary-tick-article-forward
1678   "!" gnus-summary-tick-article-forward
1679   "U" gnus-summary-tick-article-backward
1680   "d" gnus-summary-mark-as-read-forward
1681   "D" gnus-summary-mark-as-read-backward
1682   "E" gnus-summary-mark-as-expirable
1683   "\M-u" gnus-summary-clear-mark-forward
1684   "\M-U" gnus-summary-clear-mark-backward
1685   "k" gnus-summary-kill-same-subject-and-select
1686   "\C-k" gnus-summary-kill-same-subject
1687   "\M-\C-k" gnus-summary-kill-thread
1688   "\M-\C-l" gnus-summary-lower-thread
1689   "e" gnus-summary-edit-article
1690   "#" gnus-summary-mark-as-processable
1691   "\M-#" gnus-summary-unmark-as-processable
1692   "\M-\C-t" gnus-summary-toggle-threads
1693   "\M-\C-s" gnus-summary-show-thread
1694   "\M-\C-h" gnus-summary-hide-thread
1695   "\M-\C-f" gnus-summary-next-thread
1696   "\M-\C-b" gnus-summary-prev-thread
1697   [(meta down)] gnus-summary-next-thread
1698   [(meta up)] gnus-summary-prev-thread
1699   "\M-\C-u" gnus-summary-up-thread
1700   "\M-\C-d" gnus-summary-down-thread
1701   "&" gnus-summary-execute-command
1702   "c" gnus-summary-catchup-and-exit
1703   "\C-w" gnus-summary-mark-region-as-read
1704   "\C-t" gnus-summary-toggle-truncation
1705   "?" gnus-summary-mark-as-dormant
1706   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1707   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1708   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1709   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1710   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1711   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1712   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1713   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1714   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1715   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1716   "=" gnus-summary-expand-window
1717   "\C-x\C-s" gnus-summary-reselect-current-group
1718   "\M-g" gnus-summary-rescan-group
1719   "w" gnus-summary-stop-page-breaking
1720   "\C-c\C-r" gnus-summary-caesar-message
1721   "f" gnus-summary-followup
1722   "F" gnus-summary-followup-with-original
1723   "C" gnus-summary-cancel-article
1724   "r" gnus-summary-reply
1725   "R" gnus-summary-reply-with-original
1726   "\C-c\C-f" gnus-summary-mail-forward
1727   "o" gnus-summary-save-article
1728   "\C-o" gnus-summary-save-article-mail
1729   "|" gnus-summary-pipe-output
1730   "\M-k" gnus-summary-edit-local-kill
1731   "\M-K" gnus-summary-edit-global-kill
1732   ;; "V" gnus-version
1733   "\C-c\C-d" gnus-summary-describe-group
1734   "q" gnus-summary-exit
1735   "Q" gnus-summary-exit-no-update
1736   "\C-c\C-i" gnus-info-find-node
1737   gnus-mouse-2 gnus-mouse-pick-article
1738   "m" gnus-summary-mail-other-window
1739   "a" gnus-summary-post-news
1740   "i" gnus-summary-news-other-window
1741   "x" gnus-summary-limit-to-unread
1742   "s" gnus-summary-isearch-article
1743   "t" gnus-summary-toggle-header
1744   "g" gnus-summary-show-article
1745   "l" gnus-summary-goto-last-article
1746   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1747   "\C-d" gnus-summary-enter-digest-group
1748   "\M-\C-d" gnus-summary-read-document
1749   "\M-\C-e" gnus-summary-edit-parameters
1750   "\M-\C-a" gnus-summary-customize-parameters
1751   "\C-c\C-b" gnus-bug
1752   "*" gnus-cache-enter-article
1753   "\M-*" gnus-cache-remove-article
1754   "\M-&" gnus-summary-universal-argument
1755   "\C-l" gnus-recenter
1756   "I" gnus-summary-increase-score
1757   "L" gnus-summary-lower-score
1758   "\M-i" gnus-symbolic-argument
1759   "h" gnus-summary-select-article-buffer
1760
1761   "b" gnus-article-view-part
1762   "\M-t" gnus-summary-toggle-display-buttonized
1763
1764   "V" gnus-summary-score-map
1765   "X" gnus-uu-extract-map
1766   "S" gnus-summary-send-map)
1767
1768 ;; Sort of orthogonal keymap
1769 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1770   "t" gnus-summary-tick-article-forward
1771   "!" gnus-summary-tick-article-forward
1772   "d" gnus-summary-mark-as-read-forward
1773   "r" gnus-summary-mark-as-read-forward
1774   "c" gnus-summary-clear-mark-forward
1775   " " gnus-summary-clear-mark-forward
1776   "e" gnus-summary-mark-as-expirable
1777   "x" gnus-summary-mark-as-expirable
1778   "?" gnus-summary-mark-as-dormant
1779   "b" gnus-summary-set-bookmark
1780   "B" gnus-summary-remove-bookmark
1781   "#" gnus-summary-mark-as-processable
1782   "\M-#" gnus-summary-unmark-as-processable
1783   "S" gnus-summary-limit-include-expunged
1784   "C" gnus-summary-catchup
1785   "H" gnus-summary-catchup-to-here
1786   "h" gnus-summary-catchup-from-here
1787   "\C-c" gnus-summary-catchup-all
1788   "k" gnus-summary-kill-same-subject-and-select
1789   "K" gnus-summary-kill-same-subject
1790   "P" gnus-uu-mark-map)
1791
1792 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1793   "c" gnus-summary-clear-above
1794   "u" gnus-summary-tick-above
1795   "m" gnus-summary-mark-above
1796   "k" gnus-summary-kill-below)
1797
1798 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1799   "/" gnus-summary-limit-to-subject
1800   "n" gnus-summary-limit-to-articles
1801   "w" gnus-summary-pop-limit
1802   "s" gnus-summary-limit-to-subject
1803   "a" gnus-summary-limit-to-author
1804   "u" gnus-summary-limit-to-unread
1805   "m" gnus-summary-limit-to-marks
1806   "M" gnus-summary-limit-exclude-marks
1807   "v" gnus-summary-limit-to-score
1808   "*" gnus-summary-limit-include-cached
1809   "D" gnus-summary-limit-include-dormant
1810   "T" gnus-summary-limit-include-thread
1811   "d" gnus-summary-limit-exclude-dormant
1812   "t" gnus-summary-limit-to-age
1813   "." gnus-summary-limit-to-unseen
1814   "x" gnus-summary-limit-to-extra
1815   "p" gnus-summary-limit-to-display-predicate
1816   "E" gnus-summary-limit-include-expunged
1817   "c" gnus-summary-limit-exclude-childless-dormant
1818   "C" gnus-summary-limit-mark-excluded-as-read
1819   "o" gnus-summary-insert-old-articles
1820   "N" gnus-summary-insert-new-articles
1821   "r" gnus-summary-limit-to-replied)
1822
1823 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1824   "n" gnus-summary-next-unread-article
1825   "p" gnus-summary-prev-unread-article
1826   "N" gnus-summary-next-article
1827   "P" gnus-summary-prev-article
1828   "\C-n" gnus-summary-next-same-subject
1829   "\C-p" gnus-summary-prev-same-subject
1830   "\M-n" gnus-summary-next-unread-subject
1831   "\M-p" gnus-summary-prev-unread-subject
1832   "f" gnus-summary-first-unread-article
1833   "b" gnus-summary-best-unread-article
1834   "j" gnus-summary-goto-article
1835   "g" gnus-summary-goto-subject
1836   "l" gnus-summary-goto-last-article
1837   "o" gnus-summary-pop-article)
1838
1839 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1840   "k" gnus-summary-kill-thread
1841   "l" gnus-summary-lower-thread
1842   "i" gnus-summary-raise-thread
1843   "T" gnus-summary-toggle-threads
1844   "t" gnus-summary-rethread-current
1845   "^" gnus-summary-reparent-thread
1846   "s" gnus-summary-show-thread
1847   "S" gnus-summary-show-all-threads
1848   "h" gnus-summary-hide-thread
1849   "H" gnus-summary-hide-all-threads
1850   "n" gnus-summary-next-thread
1851   "p" gnus-summary-prev-thread
1852   "u" gnus-summary-up-thread
1853   "o" gnus-summary-top-thread
1854   "d" gnus-summary-down-thread
1855   "#" gnus-uu-mark-thread
1856   "\M-#" gnus-uu-unmark-thread)
1857
1858 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1859   "g" gnus-summary-prepare
1860   "c" gnus-summary-insert-cached-articles
1861   "d" gnus-summary-insert-dormant-articles)
1862
1863 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1864   "c" gnus-summary-catchup-and-exit
1865   "C" gnus-summary-catchup-all-and-exit
1866   "E" gnus-summary-exit-no-update
1867   "Q" gnus-summary-exit
1868   "Z" gnus-summary-exit
1869   "n" gnus-summary-catchup-and-goto-next-group
1870   "R" gnus-summary-reselect-current-group
1871   "G" gnus-summary-rescan-group
1872   "N" gnus-summary-next-group
1873   "s" gnus-summary-save-newsrc
1874   "P" gnus-summary-prev-group)
1875
1876 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1877   " " gnus-summary-next-page
1878   "n" gnus-summary-next-page
1879   "\177" gnus-summary-prev-page
1880   [delete] gnus-summary-prev-page
1881   "p" gnus-summary-prev-page
1882   "\r" gnus-summary-scroll-up
1883   "\M-\r" gnus-summary-scroll-down
1884   "<" gnus-summary-beginning-of-article
1885   ">" gnus-summary-end-of-article
1886   "b" gnus-summary-beginning-of-article
1887   "e" gnus-summary-end-of-article
1888   "^" gnus-summary-refer-parent-article
1889   "r" gnus-summary-refer-parent-article
1890   "D" gnus-summary-enter-digest-group
1891   "R" gnus-summary-refer-references
1892   "T" gnus-summary-refer-thread
1893   "g" gnus-summary-show-article
1894   "s" gnus-summary-isearch-article
1895   "P" gnus-summary-print-article
1896   "M" gnus-mailing-list-insinuate
1897   "t" gnus-article-babel)
1898
1899 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1900   "b" gnus-article-add-buttons
1901   "B" gnus-article-add-buttons-to-head
1902   "o" gnus-article-treat-overstrike
1903   "e" gnus-article-emphasize
1904   "w" gnus-article-fill-cited-article
1905   "Q" gnus-article-fill-long-lines
1906   "C" gnus-article-capitalize-sentences
1907   "c" gnus-article-remove-cr
1908   "q" gnus-article-de-quoted-unreadable
1909   "6" gnus-article-de-base64-unreadable
1910   "Z" gnus-article-decode-HZ
1911   "A" gnus-article-treat-ansi-sequences
1912   "h" gnus-article-wash-html
1913   "u" gnus-article-unsplit-urls
1914   "s" gnus-summary-force-verify-and-decrypt
1915   "f" gnus-article-display-x-face
1916   "l" gnus-summary-stop-page-breaking
1917   "r" gnus-summary-caesar-message
1918   "m" gnus-summary-morse-message
1919   "t" gnus-summary-toggle-header
1920   "g" gnus-treat-smiley
1921   "v" gnus-summary-verbose-headers
1922   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1923   "p" gnus-article-verify-x-pgp-sig
1924   "d" gnus-article-treat-dumbquotes)
1925
1926 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1927   ;; mnemonic: deuglif*Y*
1928   "u" gnus-article-outlook-unwrap-lines
1929   "a" gnus-article-outlook-repair-attribution
1930   "c" gnus-article-outlook-rearrange-citation
1931   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1932
1933 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1934   "a" gnus-article-hide
1935   "h" gnus-article-hide-headers
1936   "b" gnus-article-hide-boring-headers
1937   "s" gnus-article-hide-signature
1938   "c" gnus-article-hide-citation
1939   "C" gnus-article-hide-citation-in-followups
1940   "l" gnus-article-hide-list-identifiers
1941   "B" gnus-article-strip-banner
1942   "P" gnus-article-hide-pem
1943   "\C-c" gnus-article-hide-citation-maybe)
1944
1945 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1946   "a" gnus-article-highlight
1947   "h" gnus-article-highlight-headers
1948   "c" gnus-article-highlight-citation
1949   "s" gnus-article-highlight-signature)
1950
1951 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1952   "f" gnus-article-treat-fold-headers
1953   "u" gnus-article-treat-unfold-headers
1954   "n" gnus-article-treat-fold-newsgroups)
1955
1956 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1957   "x" gnus-article-display-x-face
1958   "d" gnus-article-display-face
1959   "s" gnus-treat-smiley
1960   "D" gnus-article-remove-images
1961   "f" gnus-treat-from-picon
1962   "m" gnus-treat-mail-picon
1963   "n" gnus-treat-newsgroups-picon)
1964
1965 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1966   "w" gnus-article-decode-mime-words
1967   "c" gnus-article-decode-charset
1968   "v" gnus-mime-view-all-parts
1969   "b" gnus-article-view-part)
1970
1971 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1972   "z" gnus-article-date-ut
1973   "u" gnus-article-date-ut
1974   "l" gnus-article-date-local
1975   "p" gnus-article-date-english
1976   "e" gnus-article-date-lapsed
1977   "o" gnus-article-date-original
1978   "i" gnus-article-date-iso8601
1979   "s" gnus-article-date-user)
1980
1981 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1982   "t" gnus-article-remove-trailing-blank-lines
1983   "l" gnus-article-strip-leading-blank-lines
1984   "m" gnus-article-strip-multiple-blank-lines
1985   "a" gnus-article-strip-blank-lines
1986   "A" gnus-article-strip-all-blank-lines
1987   "s" gnus-article-strip-leading-space
1988   "e" gnus-article-strip-trailing-space
1989   "w" gnus-article-remove-leading-whitespace)
1990
1991 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1992   "v" gnus-version
1993   "f" gnus-summary-fetch-faq
1994   "d" gnus-summary-describe-group
1995   "h" gnus-summary-describe-briefly
1996   "i" gnus-info-find-node
1997   "c" gnus-group-fetch-charter
1998   "C" gnus-group-fetch-control)
1999
2000 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2001   "e" gnus-summary-expire-articles
2002   "\M-\C-e" gnus-summary-expire-articles-now
2003   "\177" gnus-summary-delete-article
2004   [delete] gnus-summary-delete-article
2005   [backspace] gnus-summary-delete-article
2006   "m" gnus-summary-move-article
2007   "r" gnus-summary-respool-article
2008   "w" gnus-summary-edit-article
2009   "c" gnus-summary-copy-article
2010   "B" gnus-summary-crosspost-article
2011   "q" gnus-summary-respool-query
2012   "t" gnus-summary-respool-trace
2013   "i" gnus-summary-import-article
2014   "I" gnus-summary-create-article
2015   "p" gnus-summary-article-posted-p)
2016
2017 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2018   "o" gnus-summary-save-article
2019   "m" gnus-summary-save-article-mail
2020   "F" gnus-summary-write-article-file
2021   "r" gnus-summary-save-article-rmail
2022   "f" gnus-summary-save-article-file
2023   "b" gnus-summary-save-article-body-file
2024   "h" gnus-summary-save-article-folder
2025   "v" gnus-summary-save-article-vm
2026   "p" gnus-summary-pipe-output
2027   "P" gnus-summary-muttprint
2028   "s" gnus-soup-add-article)
2029
2030 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2031   "b" gnus-summary-display-buttonized
2032   "m" gnus-summary-repair-multipart
2033   "v" gnus-article-view-part
2034   "o" gnus-article-save-part
2035   "c" gnus-article-copy-part
2036   "C" gnus-article-view-part-as-charset
2037   "e" gnus-article-view-part-externally
2038   "E" gnus-article-encrypt-body
2039   "i" gnus-article-inline-part
2040   "|" gnus-article-pipe-part)
2041
2042 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2043   "p" gnus-summary-mark-as-processable
2044   "u" gnus-summary-unmark-as-processable
2045   "U" gnus-summary-unmark-all-processable
2046   "v" gnus-uu-mark-over
2047   "s" gnus-uu-mark-series
2048   "r" gnus-uu-mark-region
2049   "g" gnus-uu-unmark-region
2050   "R" gnus-uu-mark-by-regexp
2051   "G" gnus-uu-unmark-by-regexp
2052   "t" gnus-uu-mark-thread
2053   "T" gnus-uu-unmark-thread
2054   "a" gnus-uu-mark-all
2055   "b" gnus-uu-mark-buffer
2056   "S" gnus-uu-mark-sparse
2057   "k" gnus-summary-kill-process-mark
2058   "y" gnus-summary-yank-process-mark
2059   "w" gnus-summary-save-process-mark
2060   "i" gnus-uu-invert-processable)
2061
2062 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2063   ;;"x" gnus-uu-extract-any
2064   "m" gnus-summary-save-parts
2065   "u" gnus-uu-decode-uu
2066   "U" gnus-uu-decode-uu-and-save
2067   "s" gnus-uu-decode-unshar
2068   "S" gnus-uu-decode-unshar-and-save
2069   "o" gnus-uu-decode-save
2070   "O" gnus-uu-decode-save
2071   "b" gnus-uu-decode-binhex
2072   "B" gnus-uu-decode-binhex
2073   "p" gnus-uu-decode-postscript
2074   "P" gnus-uu-decode-postscript-and-save)
2075
2076 (gnus-define-keys
2077     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2078   "u" gnus-uu-decode-uu-view
2079   "U" gnus-uu-decode-uu-and-save-view
2080   "s" gnus-uu-decode-unshar-view
2081   "S" gnus-uu-decode-unshar-and-save-view
2082   "o" gnus-uu-decode-save-view
2083   "O" gnus-uu-decode-save-view
2084   "b" gnus-uu-decode-binhex-view
2085   "B" gnus-uu-decode-binhex-view
2086   "p" gnus-uu-decode-postscript-view
2087   "P" gnus-uu-decode-postscript-and-save-view)
2088
2089 (defvar gnus-article-post-menu nil)
2090
2091 (defconst gnus-summary-menu-maxlen 20)
2092
2093 (defun gnus-summary-menu-split (menu)
2094   ;; If we have lots of elements, divide them into groups of 20
2095   ;; and make a pane (or submenu) for each one.
2096   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2097       (let ((menu menu) sublists next
2098             (i 1))
2099         (while menu
2100           ;; Pull off the next gnus-summary-menu-maxlen elements
2101           ;; and make them the next element of sublist.
2102           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2103           (if next
2104               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2105                       nil))
2106           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2107                                              (aref (car (last menu)) 0)) menu)
2108                                sublists))
2109           (setq i (1+ i))
2110           (setq menu next))
2111         (nreverse sublists))
2112     ;; Few elements--put them all in one pane.
2113     menu))
2114
2115 (defun gnus-summary-make-menu-bar ()
2116   (gnus-turn-off-edit-menu 'summary)
2117
2118   (unless (boundp 'gnus-summary-misc-menu)
2119
2120     (easy-menu-define
2121       gnus-summary-kill-menu gnus-summary-mode-map ""
2122       (cons
2123        "Score"
2124        (nconc
2125         (list
2126          ["Customize" gnus-score-customize t])
2127         (gnus-make-score-map 'increase)
2128         (gnus-make-score-map 'lower)
2129         '(("Mark"
2130            ["Kill below" gnus-summary-kill-below t]
2131            ["Mark above" gnus-summary-mark-above t]
2132            ["Tick above" gnus-summary-tick-above t]
2133            ["Clear above" gnus-summary-clear-above t])
2134           ["Current score" gnus-summary-current-score t]
2135           ["Set score" gnus-summary-set-score t]
2136           ["Switch current score file..." gnus-score-change-score-file t]
2137           ["Set mark below..." gnus-score-set-mark-below t]
2138           ["Set expunge below..." gnus-score-set-expunge-below t]
2139           ["Edit current score file" gnus-score-edit-current-scores t]
2140           ["Edit score file" gnus-score-edit-file t]
2141           ["Trace score" gnus-score-find-trace t]
2142           ["Find words" gnus-score-find-favourite-words t]
2143           ["Rescore buffer" gnus-summary-rescore t]
2144           ["Increase score..." gnus-summary-increase-score t]
2145           ["Lower score..." gnus-summary-lower-score t]))))
2146
2147     ;; Define both the Article menu in the summary buffer and the
2148     ;; equivalent Commands menu in the article buffer here for
2149     ;; consistency.
2150     (let ((innards
2151            `(("Hide"
2152               ["All" gnus-article-hide t]
2153               ["Headers" gnus-article-hide-headers t]
2154               ["Signature" gnus-article-hide-signature t]
2155               ["Citation" gnus-article-hide-citation t]
2156               ["List identifiers" gnus-article-hide-list-identifiers t]
2157               ["Banner" gnus-article-strip-banner t]
2158               ["Boring headers" gnus-article-hide-boring-headers t])
2159              ("Highlight"
2160               ["All" gnus-article-highlight t]
2161               ["Headers" gnus-article-highlight-headers t]
2162               ["Signature" gnus-article-highlight-signature t]
2163               ["Citation" gnus-article-highlight-citation t])
2164              ("MIME"
2165               ["Words" gnus-article-decode-mime-words t]
2166               ["Charset" gnus-article-decode-charset t]
2167               ["QP" gnus-article-de-quoted-unreadable t]
2168               ["Base64" gnus-article-de-base64-unreadable t]
2169               ["View MIME buttons" gnus-summary-display-buttonized t]
2170               ["View all" gnus-mime-view-all-parts t]
2171               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2172               ["Encrypt body" gnus-article-encrypt-body
2173                :active (not (gnus-group-read-only-p))
2174                ,@(if (featurep 'xemacs) nil
2175                    '(:help "Encrypt the message body on disk"))]
2176               ["Extract all parts..." gnus-summary-save-parts t]
2177               ("Multipart"
2178                ["Repair multipart" gnus-summary-repair-multipart t]
2179                ["Pipe part..." gnus-article-pipe-part t]
2180                ["Inline part" gnus-article-inline-part t]
2181                ["Encrypt body" gnus-article-encrypt-body
2182                 :active (not (gnus-group-read-only-p))
2183                ,@(if (featurep 'xemacs) nil
2184                    '(:help "Encrypt the message body on disk"))]
2185                ["View part externally" gnus-article-view-part-externally t]
2186                ["View part with charset..." gnus-article-view-part-as-charset t]
2187                ["Copy part" gnus-article-copy-part t]
2188                ["Save part..." gnus-article-save-part t]
2189                ["View part" gnus-article-view-part t]))
2190              ("Date"
2191               ["Local" gnus-article-date-local t]
2192               ["ISO8601" gnus-article-date-iso8601 t]
2193               ["UT" gnus-article-date-ut t]
2194               ["Original" gnus-article-date-original t]
2195               ["Lapsed" gnus-article-date-lapsed t]
2196               ["User-defined" gnus-article-date-user t])
2197              ("Display"
2198               ["Remove images" gnus-article-remove-images t]
2199               ["Toggle smiley" gnus-treat-smiley t]
2200               ["Show X-Face" gnus-article-display-x-face t]
2201               ["Show picons in From" gnus-treat-from-picon t]
2202               ["Show picons in mail headers" gnus-treat-mail-picon t]
2203               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2204               ("View as different encoding"
2205                ,@(gnus-summary-menu-split
2206                   (mapcar
2207                    (lambda (cs)
2208                      ;; Since easymenu under Emacs doesn't allow
2209                      ;; lambda forms for menu commands, we should
2210                      ;; provide intern'ed function symbols.
2211                      (let ((command (intern (format "\
2212 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2213                        (fset command
2214                              `(lambda ()
2215                                 (interactive)
2216                                 (let ((gnus-summary-show-article-charset-alist
2217                                        '((1 . ,cs))))
2218                                   (gnus-summary-show-article 1))))
2219                        `[,(symbol-name cs) ,command t]))
2220                    (sort (if (fboundp 'coding-system-list)
2221                              (coding-system-list)
2222                            (mapcar 'car mm-mime-mule-charset-alist))
2223                          'string<)))))
2224              ("Washing"
2225               ("Remove Blanks"
2226                ["Leading" gnus-article-strip-leading-blank-lines t]
2227                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2228                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2229                ["All of the above" gnus-article-strip-blank-lines t]
2230                ["All" gnus-article-strip-all-blank-lines t]
2231                ["Leading space" gnus-article-strip-leading-space t]
2232                ["Trailing space" gnus-article-strip-trailing-space t]
2233                ["Leading space in headers"
2234                 gnus-article-remove-leading-whitespace t])
2235               ["Overstrike" gnus-article-treat-overstrike t]
2236               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2237               ["Emphasis" gnus-article-emphasize t]
2238               ["Word wrap" gnus-article-fill-cited-article t]
2239               ["Fill long lines" gnus-article-fill-long-lines t]
2240               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2241               ["Remove CR" gnus-article-remove-cr t]
2242               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2243               ["Base64" gnus-article-de-base64-unreadable t]
2244               ["Rot 13" gnus-summary-caesar-message
2245                ,@(if (featurep 'xemacs) '(t)
2246                    '(:help "\"Caesar rotate\" article by 13"))]
2247               ["Morse decode" gnus-summary-morse-message t]
2248               ["Unix pipe..." gnus-summary-pipe-message t]
2249               ["Add buttons" gnus-article-add-buttons t]
2250               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2251               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2252               ["Verbose header" gnus-summary-verbose-headers t]
2253               ["Toggle header" gnus-summary-toggle-header t]
2254               ["Unfold headers" gnus-article-treat-unfold-headers t]
2255               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2256               ["Html" gnus-article-wash-html t]
2257               ["Unsplit URLs" gnus-article-unsplit-urls t]
2258               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2259               ["Decode HZ" gnus-article-decode-HZ t]
2260               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2261               ("(Outlook) Deuglify"
2262                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2263                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2264                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2265                ["Full (Outlook) deuglify"
2266                 gnus-article-outlook-deuglify-article t])
2267               )
2268              ("Output"
2269               ["Save in default format..." gnus-summary-save-article
2270                ,@(if (featurep 'xemacs) '(t)
2271                    '(:help "Save article using default method"))]
2272               ["Save in file..." gnus-summary-save-article-file
2273                ,@(if (featurep 'xemacs) '(t)
2274                    '(:help "Save article in file"))]
2275               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2276               ["Save in MH folder..." gnus-summary-save-article-folder t]
2277               ["Save in VM folder..." gnus-summary-save-article-vm t]
2278               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2279               ["Save body in file..." gnus-summary-save-article-body-file t]
2280               ["Pipe through a filter..." gnus-summary-pipe-output t]
2281               ["Add to SOUP packet" gnus-soup-add-article t]
2282               ["Print with Muttprint..." gnus-summary-muttprint t]
2283               ["Print" gnus-summary-print-article
2284                ,@(if (featurep 'xemacs) '(t)
2285                    '(:help "Generate and print a PostScript image"))])
2286              ("Copy, move,... (Backend)"
2287               ,@(if (featurep 'xemacs) nil
2288                   '(:help "Copying, moving, expiring articles..."))
2289               ["Respool article..." gnus-summary-respool-article t]
2290               ["Move article..." gnus-summary-move-article
2291                (gnus-check-backend-function
2292                 'request-move-article gnus-newsgroup-name)]
2293               ["Copy article..." gnus-summary-copy-article t]
2294               ["Crosspost article..." gnus-summary-crosspost-article
2295                (gnus-check-backend-function
2296                 'request-replace-article gnus-newsgroup-name)]
2297               ["Import file..." gnus-summary-import-article
2298                (gnus-check-backend-function
2299                 'request-accept-article gnus-newsgroup-name)]
2300               ["Create article..." gnus-summary-create-article
2301                (gnus-check-backend-function
2302                 'request-accept-article gnus-newsgroup-name)]
2303               ["Check if posted" gnus-summary-article-posted-p t]
2304               ["Edit article" gnus-summary-edit-article
2305                (not (gnus-group-read-only-p))]
2306               ["Delete article" gnus-summary-delete-article
2307                (gnus-check-backend-function
2308                 'request-expire-articles gnus-newsgroup-name)]
2309               ["Query respool" gnus-summary-respool-query t]
2310               ["Trace respool" gnus-summary-respool-trace t]
2311               ["Delete expirable articles" gnus-summary-expire-articles-now
2312                (gnus-check-backend-function
2313                 'request-expire-articles gnus-newsgroup-name)])
2314              ("Extract"
2315               ["Uudecode" gnus-uu-decode-uu
2316                ,@(if (featurep 'xemacs) '(t)
2317                    '(:help "Decode uuencoded article(s)"))]
2318               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2319               ["Unshar" gnus-uu-decode-unshar t]
2320               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2321               ["Save" gnus-uu-decode-save t]
2322               ["Binhex" gnus-uu-decode-binhex t]
2323               ["Postscript" gnus-uu-decode-postscript t]
2324               ["All MIME parts" gnus-summary-save-parts t])
2325              ("Cache"
2326               ["Enter article" gnus-cache-enter-article t]
2327               ["Remove article" gnus-cache-remove-article t])
2328              ["Translate" gnus-article-babel t]
2329              ["Select article buffer" gnus-summary-select-article-buffer t]
2330              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2331              ["Isearch article..." gnus-summary-isearch-article t]
2332              ["Beginning of the article" gnus-summary-beginning-of-article t]
2333              ["End of the article" gnus-summary-end-of-article t]
2334              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2335              ["Fetch referenced articles" gnus-summary-refer-references t]
2336              ["Fetch current thread" gnus-summary-refer-thread t]
2337              ["Fetch article with id..." gnus-summary-refer-article t]
2338              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2339              ["Redisplay" gnus-summary-show-article t]
2340              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2341       (easy-menu-define
2342         gnus-summary-article-menu gnus-summary-mode-map ""
2343         (cons "Article" innards))
2344
2345       (if (not (keymapp gnus-summary-article-menu))
2346           (easy-menu-define
2347             gnus-article-commands-menu gnus-article-mode-map ""
2348             (cons "Commands" innards))
2349         ;; in Emacs, don't share menu.
2350         (setq gnus-article-commands-menu
2351               (copy-keymap gnus-summary-article-menu))
2352         (define-key gnus-article-mode-map [menu-bar commands]
2353           (cons "Commands" gnus-article-commands-menu))))
2354
2355     (easy-menu-define
2356       gnus-summary-thread-menu gnus-summary-mode-map ""
2357       '("Threads"
2358         ["Find all messages in thread" gnus-summary-refer-thread t]
2359         ["Toggle threading" gnus-summary-toggle-threads t]
2360         ["Hide threads" gnus-summary-hide-all-threads t]
2361         ["Show threads" gnus-summary-show-all-threads t]
2362         ["Hide thread" gnus-summary-hide-thread t]
2363         ["Show thread" gnus-summary-show-thread t]
2364         ["Go to next thread" gnus-summary-next-thread t]
2365         ["Go to previous thread" gnus-summary-prev-thread t]
2366         ["Go down thread" gnus-summary-down-thread t]
2367         ["Go up thread" gnus-summary-up-thread t]
2368         ["Top of thread" gnus-summary-top-thread t]
2369         ["Mark thread as read" gnus-summary-kill-thread t]
2370         ["Lower thread score" gnus-summary-lower-thread t]
2371         ["Raise thread score" gnus-summary-raise-thread t]
2372         ["Rethread current" gnus-summary-rethread-current t]))
2373
2374     (easy-menu-define
2375       gnus-summary-post-menu gnus-summary-mode-map ""
2376       `("Post"
2377         ["Send a message (mail or news)" gnus-summary-post-news
2378          ,@(if (featurep 'xemacs) '(t)
2379              '(:help "Compose a new message (mail or news)"))]
2380         ["Followup" gnus-summary-followup
2381          ,@(if (featurep 'xemacs) '(t)
2382              '(:help "Post followup to this article"))]
2383         ["Followup and yank" gnus-summary-followup-with-original
2384          ,@(if (featurep 'xemacs) '(t)
2385              '(:help "Post followup to this article, quoting its contents"))]
2386         ["Supersede article" gnus-summary-supersede-article t]
2387         ["Cancel article" gnus-summary-cancel-article
2388          ,@(if (featurep 'xemacs) '(t)
2389              '(:help "Cancel an article you posted"))]
2390         ["Reply" gnus-summary-reply t]
2391         ["Reply and yank" gnus-summary-reply-with-original t]
2392         ["Wide reply" gnus-summary-wide-reply t]
2393         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2394          ,@(if (featurep 'xemacs) '(t)
2395              '(:help "Mail a reply, quoting this article"))]
2396         ["Very wide reply" gnus-summary-very-wide-reply t]
2397         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2398          ,@(if (featurep 'xemacs) '(t)
2399              '(:help "Mail a very wide reply, quoting this article"))]
2400         ["Mail forward" gnus-summary-mail-forward t]
2401         ["Post forward" gnus-summary-post-forward t]
2402         ["Digest and mail" gnus-uu-digest-mail-forward t]
2403         ["Digest and post" gnus-uu-digest-post-forward t]
2404         ["Resend message" gnus-summary-resend-message t]
2405         ["Resend message edit" gnus-summary-resend-message-edit t]
2406         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2407         ["Send a mail" gnus-summary-mail-other-window t]
2408         ["Create a local message" gnus-summary-news-other-window t]
2409         ["Uuencode and post" gnus-uu-post-news
2410          ,@(if (featurep 'xemacs) '(t)
2411              '(:help "Post a uuencoded article"))]
2412         ["Followup via news" gnus-summary-followup-to-mail t]
2413         ["Followup via news and yank"
2414          gnus-summary-followup-to-mail-with-original t]
2415         ;;("Draft"
2416         ;;["Send" gnus-summary-send-draft t]
2417         ;;["Send bounced" gnus-resend-bounced-mail t])
2418         ))
2419
2420     (cond
2421      ((not (keymapp gnus-summary-post-menu))
2422       (setq gnus-article-post-menu gnus-summary-post-menu))
2423      ((not gnus-article-post-menu)
2424       ;; Don't share post menu.
2425       (setq gnus-article-post-menu
2426             (copy-keymap gnus-summary-post-menu))))
2427     (define-key gnus-article-mode-map [menu-bar post]
2428       (cons "Post" gnus-article-post-menu))
2429
2430     (easy-menu-define
2431       gnus-summary-misc-menu gnus-summary-mode-map ""
2432       `("Gnus"
2433         ("Mark Read"
2434          ["Mark as read" gnus-summary-mark-as-read-forward t]
2435          ["Mark same subject and select"
2436           gnus-summary-kill-same-subject-and-select t]
2437          ["Mark same subject" gnus-summary-kill-same-subject t]
2438          ["Catchup" gnus-summary-catchup
2439           ,@(if (featurep 'xemacs) '(t)
2440               '(:help "Mark unread articles in this group as read"))]
2441          ["Catchup all" gnus-summary-catchup-all t]
2442          ["Catchup to here" gnus-summary-catchup-to-here t]
2443          ["Catchup from here" gnus-summary-catchup-from-here t]
2444          ["Catchup region" gnus-summary-mark-region-as-read
2445           (gnus-mark-active-p)]
2446          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2447         ("Mark Various"
2448          ["Tick" gnus-summary-tick-article-forward t]
2449          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2450          ["Remove marks" gnus-summary-clear-mark-forward t]
2451          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2452          ["Set bookmark" gnus-summary-set-bookmark t]
2453          ["Remove bookmark" gnus-summary-remove-bookmark t])
2454         ("Limit to"
2455          ["Marks..." gnus-summary-limit-to-marks t]
2456          ["Subject..." gnus-summary-limit-to-subject t]
2457          ["Author..." gnus-summary-limit-to-author t]
2458          ["Age..." gnus-summary-limit-to-age t]
2459          ["Extra..." gnus-summary-limit-to-extra t]
2460          ["Score..." gnus-summary-limit-to-score t]
2461          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2462          ["Unread" gnus-summary-limit-to-unread t]
2463          ["Unseen" gnus-summary-limit-to-unseen t]
2464          ["Replied" gnus-summary-limit-to-replied t]
2465          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2466          ["Next articles" gnus-summary-limit-to-articles t]
2467          ["Pop limit" gnus-summary-pop-limit t]
2468          ["Show dormant" gnus-summary-limit-include-dormant t]
2469          ["Hide childless dormant"
2470           gnus-summary-limit-exclude-childless-dormant t]
2471          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2472          ["Hide marked" gnus-summary-limit-exclude-marks t]
2473          ["Show expunged" gnus-summary-limit-include-expunged t])
2474         ("Process Mark"
2475          ["Set mark" gnus-summary-mark-as-processable t]
2476          ["Remove mark" gnus-summary-unmark-as-processable t]
2477          ["Remove all marks" gnus-summary-unmark-all-processable t]
2478          ["Invert marks" gnus-uu-invert-processable t]
2479          ["Mark above" gnus-uu-mark-over t]
2480          ["Mark series" gnus-uu-mark-series t]
2481          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2482          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2483          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2484          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2485          ["Mark all" gnus-uu-mark-all t]
2486          ["Mark buffer" gnus-uu-mark-buffer t]
2487          ["Mark sparse" gnus-uu-mark-sparse t]
2488          ["Mark thread" gnus-uu-mark-thread t]
2489          ["Unmark thread" gnus-uu-unmark-thread t]
2490          ("Process Mark Sets"
2491           ["Kill" gnus-summary-kill-process-mark t]
2492           ["Yank" gnus-summary-yank-process-mark
2493            gnus-newsgroup-process-stack]
2494           ["Save" gnus-summary-save-process-mark t]
2495           ["Run command on marked..." gnus-summary-universal-argument t]))
2496         ("Scroll article"
2497          ["Page forward" gnus-summary-next-page
2498           ,@(if (featurep 'xemacs) '(t)
2499               '(:help "Show next page of article"))]
2500          ["Page backward" gnus-summary-prev-page
2501           ,@(if (featurep 'xemacs) '(t)
2502               '(:help "Show previous page of article"))]
2503          ["Line forward" gnus-summary-scroll-up t])
2504         ("Move"
2505          ["Next unread article" gnus-summary-next-unread-article t]
2506          ["Previous unread article" gnus-summary-prev-unread-article t]
2507          ["Next article" gnus-summary-next-article t]
2508          ["Previous article" gnus-summary-prev-article t]
2509          ["Next unread subject" gnus-summary-next-unread-subject t]
2510          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2511          ["Next article same subject" gnus-summary-next-same-subject t]
2512          ["Previous article same subject" gnus-summary-prev-same-subject t]
2513          ["First unread article" gnus-summary-first-unread-article t]
2514          ["Best unread article" gnus-summary-best-unread-article t]
2515          ["Go to subject number..." gnus-summary-goto-subject t]
2516          ["Go to article number..." gnus-summary-goto-article t]
2517          ["Go to the last article" gnus-summary-goto-last-article t]
2518          ["Pop article off history" gnus-summary-pop-article t])
2519         ("Sort"
2520          ["Sort by number" gnus-summary-sort-by-number t]
2521          ["Sort by author" gnus-summary-sort-by-author t]
2522          ["Sort by subject" gnus-summary-sort-by-subject t]
2523          ["Sort by date" gnus-summary-sort-by-date t]
2524          ["Sort by score" gnus-summary-sort-by-score t]
2525          ["Sort by lines" gnus-summary-sort-by-lines t]
2526          ["Sort by characters" gnus-summary-sort-by-chars t]
2527          ["Randomize" gnus-summary-sort-by-random t]
2528          ["Original sort" gnus-summary-sort-by-original t])
2529         ("Help"
2530          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2531          ["Describe group" gnus-summary-describe-group t]
2532          ["Fetch charter" gnus-group-fetch-charter
2533           ,@(if (featurep 'xemacs) nil
2534               '(:help "Display the charter of the current group"))]
2535          ["Fetch control message" gnus-group-fetch-control
2536           ,@(if (featurep 'xemacs) nil
2537               '(:help "Display the archived control message for the current group"))]
2538          ["Read manual" gnus-info-find-node t])
2539         ("Modes"
2540          ["Pick and read" gnus-pick-mode t]
2541          ["Binary" gnus-binary-mode t])
2542         ("Regeneration"
2543          ["Regenerate" gnus-summary-prepare t]
2544          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2545          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2546          ["Toggle threading" gnus-summary-toggle-threads t])
2547         ["See old articles" gnus-summary-insert-old-articles t]
2548         ["See new articles" gnus-summary-insert-new-articles t]
2549         ["Filter articles..." gnus-summary-execute-command t]
2550         ["Run command on articles..." gnus-summary-universal-argument t]
2551         ["Search articles forward..." gnus-summary-search-article-forward t]
2552         ["Search articles backward..." gnus-summary-search-article-backward t]
2553         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2554         ["Expand window" gnus-summary-expand-window t]
2555         ["Expire expirable articles" gnus-summary-expire-articles
2556          (gnus-check-backend-function
2557           'request-expire-articles gnus-newsgroup-name)]
2558         ["Edit local kill file" gnus-summary-edit-local-kill t]
2559         ["Edit main kill file" gnus-summary-edit-global-kill t]
2560         ["Edit group parameters" gnus-summary-edit-parameters t]
2561         ["Customize group parameters" gnus-summary-customize-parameters t]
2562         ["Send a bug report" gnus-bug t]
2563         ("Exit"
2564          ["Catchup and exit" gnus-summary-catchup-and-exit
2565           ,@(if (featurep 'xemacs) '(t)
2566               '(:help "Mark unread articles in this group as read, then exit"))]
2567          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2568          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2569          ["Exit group" gnus-summary-exit
2570           ,@(if (featurep 'xemacs) '(t)
2571               '(:help "Exit current group, return to group selection mode"))]
2572          ["Exit group without updating" gnus-summary-exit-no-update t]
2573          ["Exit and goto next group" gnus-summary-next-group t]
2574          ["Exit and goto prev group" gnus-summary-prev-group t]
2575          ["Reselect group" gnus-summary-reselect-current-group t]
2576          ["Rescan group" gnus-summary-rescan-group t]
2577          ["Update dribble" gnus-summary-save-newsrc t])))
2578
2579     (gnus-run-hooks 'gnus-summary-menu-hook)))
2580
2581 (defvar gnus-summary-tool-bar-map nil)
2582
2583 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2584 (defun gnus-summary-make-tool-bar ()
2585   (if (and (fboundp 'tool-bar-add-item-from-menu)
2586            (default-value 'tool-bar-mode)
2587            (not gnus-summary-tool-bar-map))
2588       (setq gnus-summary-tool-bar-map
2589             (let ((tool-bar-map (make-sparse-keymap))
2590                   (load-path (mm-image-load-path)))
2591               (tool-bar-add-item-from-menu
2592                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2593               (tool-bar-add-item-from-menu
2594                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2595               (tool-bar-add-item-from-menu
2596                'gnus-summary-post-news "post" gnus-summary-mode-map)
2597               (tool-bar-add-item-from-menu
2598                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2599               (tool-bar-add-item-from-menu
2600                'gnus-summary-followup "followup" gnus-summary-mode-map)
2601               (tool-bar-add-item-from-menu
2602                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-reply "reply" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2617               (tool-bar-add-item-from-menu
2618                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2619               (tool-bar-add-item-from-menu
2620                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2621               (tool-bar-add-item-from-menu
2622                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2623               tool-bar-map)))
2624   (if gnus-summary-tool-bar-map
2625       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2626
2627 (defun gnus-score-set-default (var value)
2628   "A version of set that updates the GNU Emacs menu-bar."
2629   (set var value)
2630   ;; It is the message that forces the active status to be updated.
2631   (message ""))
2632
2633 (defun gnus-make-score-map (type)
2634   "Make a summary score map of type TYPE."
2635   (if t
2636       nil
2637     (let ((headers '(("author" "from" string)
2638                      ("subject" "subject" string)
2639                      ("article body" "body" string)
2640                      ("article head" "head" string)
2641                      ("xref" "xref" string)
2642                      ("extra header" "extra" string)
2643                      ("lines" "lines" number)
2644                      ("followups to author" "followup" string)))
2645           (types '((number ("less than" <)
2646                            ("greater than" >)
2647                            ("equal" =))
2648                    (string ("substring" s)
2649                            ("exact string" e)
2650                            ("fuzzy string" f)
2651                            ("regexp" r))))
2652           (perms '(("temporary" (current-time-string))
2653                    ("permanent" nil)
2654                    ("immediate" now)))
2655           header)
2656       (list
2657        (apply
2658         'nconc
2659         (list
2660          (if (eq type 'lower)
2661              "Lower score"
2662            "Increase score"))
2663         (let (outh)
2664           (while headers
2665             (setq header (car headers))
2666             (setq outh
2667                   (cons
2668                    (apply
2669                     'nconc
2670                     (list (car header))
2671                     (let ((ts (cdr (assoc (nth 2 header) types)))
2672                           outt)
2673                       (while ts
2674                         (setq outt
2675                               (cons
2676                                (apply
2677                                 'nconc
2678                                 (list (caar ts))
2679                                 (let ((ps perms)
2680                                       outp)
2681                                   (while ps
2682                                     (setq outp
2683                                           (cons
2684                                            (vector
2685                                             (caar ps)
2686                                             (list
2687                                              'gnus-summary-score-entry
2688                                              (nth 1 header)
2689                                              (if (or (string= (nth 1 header)
2690                                                               "head")
2691                                                      (string= (nth 1 header)
2692                                                               "body"))
2693                                                  ""
2694                                                (list 'gnus-summary-header
2695                                                      (nth 1 header)))
2696                                              (list 'quote (nth 1 (car ts)))
2697                                              (list 'gnus-score-delta-default
2698                                                    nil)
2699                                              (nth 1 (car ps))
2700                                              t)
2701                                             t)
2702                                            outp))
2703                                     (setq ps (cdr ps)))
2704                                   (list (nreverse outp))))
2705                                outt))
2706                         (setq ts (cdr ts)))
2707                       (list (nreverse outt))))
2708                    outh))
2709             (setq headers (cdr headers)))
2710           (list (nreverse outh))))))))
2711
2712 \f
2713
2714 (defun gnus-summary-mode (&optional group)
2715   "Major mode for reading articles.
2716
2717 All normal editing commands are switched off.
2718 \\<gnus-summary-mode-map>
2719 Each line in this buffer represents one article.  To read an
2720 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2721 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2722 respectively.
2723
2724 You can also post articles and send mail from this buffer.  To
2725 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2726 of an article, type `\\[gnus-summary-reply]'.
2727
2728 There are approx. one gazillion commands you can execute in this
2729 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2730
2731 The following commands are available:
2732
2733 \\{gnus-summary-mode-map}"
2734   (interactive)
2735   (kill-all-local-variables)
2736   (when (gnus-visual-p 'summary-menu 'menu)
2737     (gnus-summary-make-menu-bar)
2738     (gnus-summary-make-tool-bar))
2739   (gnus-summary-make-local-variables)
2740   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2741     (gnus-summary-make-local-variables))
2742   (gnus-make-thread-indent-array)
2743   (gnus-simplify-mode-line)
2744   (setq major-mode 'gnus-summary-mode)
2745   (setq mode-name "Summary")
2746   (make-local-variable 'minor-mode-alist)
2747   (use-local-map gnus-summary-mode-map)
2748   (buffer-disable-undo)
2749   (setq buffer-read-only t              ;Disable modification
2750         show-trailing-whitespace nil)
2751   (setq truncate-lines t)
2752   (setq selective-display t)
2753   (setq selective-display-ellipses t)   ;Display `...'
2754   (gnus-summary-set-display-table)
2755   (gnus-set-default-directory)
2756   (setq gnus-newsgroup-name group)
2757   (make-local-variable 'gnus-summary-line-format)
2758   (make-local-variable 'gnus-summary-line-format-spec)
2759   (make-local-variable 'gnus-summary-dummy-line-format)
2760   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2761   (make-local-variable 'gnus-summary-mark-positions)
2762   (gnus-make-local-hook 'pre-command-hook)
2763   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2764   (gnus-run-hooks 'gnus-summary-mode-hook)
2765   (turn-on-gnus-mailing-list-mode)
2766   (mm-enable-multibyte)
2767   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2768   (gnus-update-summary-mark-positions))
2769
2770 (defun gnus-summary-make-local-variables ()
2771   "Make all the local summary buffer variables."
2772   (let (global)
2773     (dolist (local gnus-summary-local-variables)
2774       (if (consp local)
2775           (progn
2776             (if (eq (cdr local) 'global)
2777                 ;; Copy the global value of the variable.
2778                 (setq global (symbol-value (car local)))
2779               ;; Use the value from the list.
2780               (setq global (eval (cdr local))))
2781             (set (make-local-variable (car local)) global))
2782         ;; Simple nil-valued local variable.
2783         (set (make-local-variable local) nil)))))
2784
2785 (defun gnus-summary-clear-local-variables ()
2786   (let ((locals gnus-summary-local-variables))
2787     (while locals
2788       (if (consp (car locals))
2789           (and (vectorp (caar locals))
2790                (set (caar locals) nil))
2791         (and (vectorp (car locals))
2792              (set (car locals) nil)))
2793       (setq locals (cdr locals)))))
2794
2795 ;; Summary data functions.
2796
2797 (defmacro gnus-data-number (data)
2798   `(car ,data))
2799
2800 (defmacro gnus-data-set-number (data number)
2801   `(setcar ,data ,number))
2802
2803 (defmacro gnus-data-mark (data)
2804   `(nth 1 ,data))
2805
2806 (defmacro gnus-data-set-mark (data mark)
2807   `(setcar (nthcdr 1 ,data) ,mark))
2808
2809 (defmacro gnus-data-pos (data)
2810   `(nth 2 ,data))
2811
2812 (defmacro gnus-data-set-pos (data pos)
2813   `(setcar (nthcdr 2 ,data) ,pos))
2814
2815 (defmacro gnus-data-header (data)
2816   `(nth 3 ,data))
2817
2818 (defmacro gnus-data-set-header (data header)
2819   `(setf (nth 3 ,data) ,header))
2820
2821 (defmacro gnus-data-level (data)
2822   `(nth 4 ,data))
2823
2824 (defmacro gnus-data-unread-p (data)
2825   `(= (nth 1 ,data) gnus-unread-mark))
2826
2827 (defmacro gnus-data-read-p (data)
2828   `(/= (nth 1 ,data) gnus-unread-mark))
2829
2830 (defmacro gnus-data-pseudo-p (data)
2831   `(consp (nth 3 ,data)))
2832
2833 (defmacro gnus-data-find (number)
2834   `(assq ,number gnus-newsgroup-data))
2835
2836 (defmacro gnus-data-find-list (number &optional data)
2837   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2838      (memq (assq ,number bdata)
2839            bdata)))
2840
2841 (defmacro gnus-data-make (number mark pos header level)
2842   `(list ,number ,mark ,pos ,header ,level))
2843
2844 (defun gnus-data-enter (after-article number mark pos header level offset)
2845   (let ((data (gnus-data-find-list after-article)))
2846     (unless data
2847       (error "No such article: %d" after-article))
2848     (setcdr data (cons (gnus-data-make number mark pos header level)
2849                        (cdr data)))
2850     (setq gnus-newsgroup-data-reverse nil)
2851     (gnus-data-update-list (cddr data) offset)))
2852
2853 (defun gnus-data-enter-list (after-article list &optional offset)
2854   (when list
2855     (let ((data (and after-article (gnus-data-find-list after-article)))
2856           (ilist list))
2857       (if (not (or data
2858                    after-article))
2859           (let ((odata gnus-newsgroup-data))
2860             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2861             (when offset
2862               (gnus-data-update-list odata offset)))
2863       ;; Find the last element in the list to be spliced into the main
2864         ;; list.
2865         (while (cdr list)
2866           (setq list (cdr list)))
2867         (if (not data)
2868             (progn
2869               (setcdr list gnus-newsgroup-data)
2870               (setq gnus-newsgroup-data ilist)
2871               (when offset
2872                 (gnus-data-update-list (cdr list) offset)))
2873           (setcdr list (cdr data))
2874           (setcdr data ilist)
2875           (when offset
2876             (gnus-data-update-list (cdr list) offset))))
2877       (setq gnus-newsgroup-data-reverse nil))))
2878
2879 (defun gnus-data-remove (article &optional offset)
2880   (let ((data gnus-newsgroup-data))
2881     (if (= (gnus-data-number (car data)) article)
2882         (progn
2883           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2884                 gnus-newsgroup-data-reverse nil)
2885           (when offset
2886             (gnus-data-update-list gnus-newsgroup-data offset)))
2887       (while (cdr data)
2888         (when (= (gnus-data-number (cadr data)) article)
2889           (setcdr data (cddr data))
2890           (when offset
2891             (gnus-data-update-list (cdr data) offset))
2892           (setq data nil
2893                 gnus-newsgroup-data-reverse nil))
2894         (setq data (cdr data))))))
2895
2896 (defmacro gnus-data-list (backward)
2897   `(if ,backward
2898        (or gnus-newsgroup-data-reverse
2899            (setq gnus-newsgroup-data-reverse
2900                  (reverse gnus-newsgroup-data)))
2901      gnus-newsgroup-data))
2902
2903 (defun gnus-data-update-list (data offset)
2904   "Add OFFSET to the POS of all data entries in DATA."
2905   (setq gnus-newsgroup-data-reverse nil)
2906   (while data
2907     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2908     (setq data (cdr data))))
2909
2910 (defun gnus-summary-article-pseudo-p (article)
2911   "Say whether this article is a pseudo article or not."
2912   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2913
2914 (defmacro gnus-summary-article-sparse-p (article)
2915   "Say whether this article is a sparse article or not."
2916   `(memq ,article gnus-newsgroup-sparse))
2917
2918 (defmacro gnus-summary-article-ancient-p (article)
2919   "Say whether this article is a sparse article or not."
2920   `(memq ,article gnus-newsgroup-ancient))
2921
2922 (defun gnus-article-parent-p (number)
2923   "Say whether this article is a parent or not."
2924   (let ((data (gnus-data-find-list number)))
2925     (and (cdr data)              ; There has to be an article after...
2926          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2927             (gnus-data-level (nth 1 data))))))
2928
2929 (defun gnus-article-children (number)
2930   "Return a list of all children to NUMBER."
2931   (let* ((data (gnus-data-find-list number))
2932          (level (gnus-data-level (car data)))
2933          children)
2934     (setq data (cdr data))
2935     (while (and data
2936                 (= (gnus-data-level (car data)) (1+ level)))
2937       (push (gnus-data-number (car data)) children)
2938       (setq data (cdr data)))
2939     children))
2940
2941 (defmacro gnus-summary-skip-intangible ()
2942   "If the current article is intangible, then jump to a different article."
2943   '(let ((to (get-text-property (point) 'gnus-intangible)))
2944      (and to (gnus-summary-goto-subject to))))
2945
2946 (defmacro gnus-summary-article-intangible-p ()
2947   "Say whether this article is intangible or not."
2948   '(get-text-property (point) 'gnus-intangible))
2949
2950 (defun gnus-article-read-p (article)
2951   "Say whether ARTICLE is read or not."
2952   (not (or (memq article gnus-newsgroup-marked)
2953            (memq article gnus-newsgroup-spam-marked)
2954            (memq article gnus-newsgroup-unreads)
2955            (memq article gnus-newsgroup-unselected)
2956            (memq article gnus-newsgroup-dormant))))
2957
2958 ;; Some summary mode macros.
2959
2960 (defmacro gnus-summary-article-number ()
2961   "The article number of the article on the current line.
2962 If there isn't an article number here, then we return the current
2963 article number."
2964   '(progn
2965      (gnus-summary-skip-intangible)
2966      (or (get-text-property (point) 'gnus-number)
2967          (gnus-summary-last-subject))))
2968
2969 (defmacro gnus-summary-article-header (&optional number)
2970   "Return the header of article NUMBER."
2971   `(gnus-data-header (gnus-data-find
2972                       ,(or number '(gnus-summary-article-number)))))
2973
2974 (defmacro gnus-summary-thread-level (&optional number)
2975   "Return the level of thread that starts with article NUMBER."
2976   `(if (and (eq gnus-summary-make-false-root 'dummy)
2977             (get-text-property (point) 'gnus-intangible))
2978        0
2979      (gnus-data-level (gnus-data-find
2980                        ,(or number '(gnus-summary-article-number))))))
2981
2982 (defmacro gnus-summary-article-mark (&optional number)
2983   "Return the mark of article NUMBER."
2984   `(gnus-data-mark (gnus-data-find
2985                     ,(or number '(gnus-summary-article-number)))))
2986
2987 (defmacro gnus-summary-article-pos (&optional number)
2988   "Return the position of the line of article NUMBER."
2989   `(gnus-data-pos (gnus-data-find
2990                    ,(or number '(gnus-summary-article-number)))))
2991
2992 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2993 (defmacro gnus-summary-article-subject (&optional number)
2994   "Return current subject string or nil if nothing."
2995   `(let ((headers
2996           ,(if number
2997                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2998              '(gnus-data-header (assq (gnus-summary-article-number)
2999                                       gnus-newsgroup-data)))))
3000      (and headers
3001           (vectorp headers)
3002           (mail-header-subject headers))))
3003
3004 (defmacro gnus-summary-article-score (&optional number)
3005   "Return current article score."
3006   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3007                   gnus-newsgroup-scored))
3008        gnus-summary-default-score 0))
3009
3010 (defun gnus-summary-article-children (&optional number)
3011   "Return a list of article numbers that are children of article NUMBER."
3012   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3013          (level (gnus-data-level (car data)))
3014          l children)
3015     (while (and (setq data (cdr data))
3016                 (> (setq l (gnus-data-level (car data))) level))
3017       (and (= (1+ level) l)
3018            (push (gnus-data-number (car data))
3019                  children)))
3020     (nreverse children)))
3021
3022 (defun gnus-summary-article-parent (&optional number)
3023   "Return the article number of the parent of article NUMBER."
3024   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3025                                     (gnus-data-list t)))
3026          (level (gnus-data-level (car data))))
3027     (if (zerop level)
3028         ()                              ; This is a root.
3029       ;; We search until we find an article with a level less than
3030       ;; this one.  That function has to be the parent.
3031       (while (and (setq data (cdr data))
3032                   (not (< (gnus-data-level (car data)) level))))
3033       (and data (gnus-data-number (car data))))))
3034
3035 (defun gnus-unread-mark-p (mark)
3036   "Say whether MARK is the unread mark."
3037   (= mark gnus-unread-mark))
3038
3039 (defun gnus-read-mark-p (mark)
3040   "Say whether MARK is one of the marks that mark as read.
3041 This is all marks except unread, ticked, dormant, and expirable."
3042   (not (or (= mark gnus-unread-mark)
3043            (= mark gnus-ticked-mark)
3044            (= mark gnus-spam-mark)
3045            (= mark gnus-dormant-mark)
3046            (= mark gnus-expirable-mark))))
3047
3048 (defmacro gnus-article-mark (number)
3049   "Return the MARK of article NUMBER.
3050 This macro should only be used when computing the mark the \"first\"
3051 time; i.e., when generating the summary lines.  After that,
3052 `gnus-summary-article-mark' should be used to examine the
3053 marks of articles."
3054   `(cond
3055     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3056     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3057     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3058     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3059     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3060     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3061     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3062     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3063            gnus-ancient-mark))))
3064
3065 ;; Saving hidden threads.
3066
3067 (defmacro gnus-save-hidden-threads (&rest forms)
3068   "Save hidden threads, eval FORMS, and restore the hidden threads."
3069   (let ((config (make-symbol "config")))
3070     `(let ((,config (gnus-hidden-threads-configuration)))
3071        (unwind-protect
3072            (save-excursion
3073              ,@forms)
3074          (gnus-restore-hidden-threads-configuration ,config)))))
3075 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3076 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3077
3078 (defun gnus-data-compute-positions ()
3079   "Compute the positions of all articles."
3080   (setq gnus-newsgroup-data-reverse nil)
3081   (let ((data gnus-newsgroup-data))
3082     (save-excursion
3083       (gnus-save-hidden-threads
3084         (gnus-summary-show-all-threads)
3085         (goto-char (point-min))
3086         (while data
3087           (while (get-text-property (point) 'gnus-intangible)
3088             (forward-line 1))
3089           (gnus-data-set-pos (car data) (+ (point) 3))
3090           (setq data (cdr data))
3091           (forward-line 1))))))
3092
3093 (defun gnus-hidden-threads-configuration ()
3094   "Return the current hidden threads configuration."
3095   (save-excursion
3096     (let (config)
3097       (goto-char (point-min))
3098       (while (search-forward "\r" nil t)
3099         (push (1- (point)) config))
3100       config)))
3101
3102 (defun gnus-restore-hidden-threads-configuration (config)
3103   "Restore hidden threads configuration from CONFIG."
3104   (save-excursion
3105     (let (point buffer-read-only)
3106       (while (setq point (pop config))
3107         (when (and (< point (point-max))
3108                    (goto-char point)
3109                    (eq (char-after) ?\n))
3110           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3111
3112 ;; Various summary mode internalish functions.
3113
3114 (defun gnus-mouse-pick-article (e)
3115   (interactive "e")
3116   (mouse-set-point e)
3117   (gnus-summary-next-page nil t))
3118
3119 (defun gnus-summary-set-display-table ()
3120   "Change the display table.
3121 Odd characters have a tendency to mess
3122 up nicely formatted displays - we make all possible glyphs
3123 display only a single character."
3124
3125   ;; We start from the standard display table, if any.
3126   (let ((table (or (copy-sequence standard-display-table)
3127                    (make-display-table)))
3128         (i 32))
3129     ;; Nix out all the control chars...
3130     (while (>= (setq i (1- i)) 0)
3131       (aset table i [??]))
3132    ;; ... but not newline and cr, of course.  (cr is necessary for the
3133     ;; selective display).
3134     (aset table ?\n nil)
3135     (aset table ?\r nil)
3136     ;; We keep TAB as well.
3137     (aset table ?\t nil)
3138     ;; We nix out any glyphs over 126 that are not set already.
3139     (let ((i 256))
3140       (while (>= (setq i (1- i)) 127)
3141         ;; Only modify if the entry is nil.
3142         (unless (aref table i)
3143           (aset table i [??]))))
3144     (setq buffer-display-table table)))
3145
3146 (defun gnus-summary-set-article-display-arrow (pos)
3147   "Update the overlay arrow to point to line at position POS."
3148   (when (and gnus-summary-display-arrow
3149              (boundp 'overlay-arrow-position)
3150              (boundp 'overlay-arrow-string))
3151     (save-excursion
3152       (goto-char pos)
3153       (beginning-of-line)
3154       (unless overlay-arrow-position
3155         (setq overlay-arrow-position (make-marker)))
3156       (setq overlay-arrow-string "=>"
3157             overlay-arrow-position (set-marker overlay-arrow-position
3158                                                (point)
3159                                                (current-buffer))))))
3160
3161 (defun gnus-summary-setup-buffer (group)
3162   "Initialize summary buffer."
3163   (let ((buffer (gnus-summary-buffer-name group))
3164         (dead-name (concat "*Dead Summary "
3165                            (gnus-group-decoded-name group) "*")))
3166     ;; If a dead summary buffer exists, we kill it.
3167     (when (gnus-buffer-live-p dead-name)
3168       (gnus-kill-buffer dead-name))
3169     (if (get-buffer buffer)
3170         (progn
3171           (set-buffer buffer)
3172           (setq gnus-summary-buffer (current-buffer))
3173           (not gnus-newsgroup-prepared))
3174       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3175       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3176       (gnus-summary-mode group)
3177       (when gnus-carpal
3178         (gnus-carpal-setup-buffer 'summary))
3179       (unless gnus-single-article-buffer
3180         (make-local-variable 'gnus-article-buffer)
3181         (make-local-variable 'gnus-article-current)
3182         (make-local-variable 'gnus-original-article-buffer))
3183       (setq gnus-newsgroup-name group)
3184       ;; Set any local variables in the group parameters.
3185       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3186       t)))
3187
3188 (defun gnus-set-global-variables ()
3189   "Set the global equivalents of the buffer-local variables.
3190 They are set to the latest values they had.  These reflect the summary
3191 buffer that was in action when the last article was fetched."
3192   (when (eq major-mode 'gnus-summary-mode)
3193     (setq gnus-summary-buffer (current-buffer))
3194     (let ((name gnus-newsgroup-name)
3195           (marked gnus-newsgroup-marked)
3196           (spam gnus-newsgroup-spam-marked)
3197           (unread gnus-newsgroup-unreads)
3198           (headers gnus-current-headers)
3199           (data gnus-newsgroup-data)
3200           (summary gnus-summary-buffer)
3201           (article-buffer gnus-article-buffer)
3202           (original gnus-original-article-buffer)
3203           (gac gnus-article-current)
3204           (reffed gnus-reffed-article-number)
3205           (score-file gnus-current-score-file)
3206           (default-charset gnus-newsgroup-charset)
3207           vlist)
3208       (let ((locals gnus-newsgroup-variables))
3209         (while locals
3210           (if (consp (car locals))
3211               (push (eval (caar locals)) vlist)
3212             (push (eval (car locals)) vlist))
3213           (setq locals (cdr locals)))
3214         (setq vlist (nreverse vlist)))
3215       (with-current-buffer gnus-group-buffer
3216         (setq gnus-newsgroup-name name
3217               gnus-newsgroup-marked marked
3218               gnus-newsgroup-spam-marked spam
3219               gnus-newsgroup-unreads unread
3220               gnus-current-headers headers
3221               gnus-newsgroup-data data
3222               gnus-article-current gac
3223               gnus-summary-buffer summary
3224               gnus-article-buffer article-buffer
3225               gnus-original-article-buffer original
3226               gnus-reffed-article-number reffed
3227               gnus-current-score-file score-file
3228               gnus-newsgroup-charset default-charset)
3229         (let ((locals gnus-newsgroup-variables))
3230           (while locals
3231             (if (consp (car locals))
3232                 (set (caar locals) (pop vlist))
3233               (set (car locals) (pop vlist)))
3234             (setq locals (cdr locals))))
3235         ;; The article buffer also has local variables.
3236         (when (gnus-buffer-live-p gnus-article-buffer)
3237           (set-buffer gnus-article-buffer)
3238           (setq gnus-summary-buffer summary))))))
3239
3240 (defun gnus-summary-article-unread-p (article)
3241   "Say whether ARTICLE is unread or not."
3242   (memq article gnus-newsgroup-unreads))
3243
3244 (defun gnus-summary-first-article-p (&optional article)
3245   "Return whether ARTICLE is the first article in the buffer."
3246   (if (not (setq article (or article (gnus-summary-article-number))))
3247       nil
3248     (eq article (caar gnus-newsgroup-data))))
3249
3250 (defun gnus-summary-last-article-p (&optional article)
3251   "Return whether ARTICLE is the last article in the buffer."
3252   (if (not (setq article (or article (gnus-summary-article-number))))
3253       ;; All non-existent numbers are the last article.  :-)
3254       t
3255     (not (cdr (gnus-data-find-list article)))))
3256
3257 (defun gnus-make-thread-indent-array ()
3258   (let ((n 200))
3259     (unless (and gnus-thread-indent-array
3260                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3261       (setq gnus-thread-indent-array (make-vector 201 "")
3262             gnus-thread-indent-array-level gnus-thread-indent-level)
3263       (while (>= n 0)
3264         (aset gnus-thread-indent-array n
3265               (make-string (* n gnus-thread-indent-level) ? ))
3266         (setq n (1- n))))))
3267
3268 (defun gnus-update-summary-mark-positions ()
3269   "Compute where the summary marks are to go."
3270   (save-excursion
3271     (when (gnus-buffer-exists-p gnus-summary-buffer)
3272       (set-buffer gnus-summary-buffer))
3273     (let ((gnus-replied-mark 129)
3274           (gnus-score-below-mark 130)
3275           (gnus-score-over-mark 130)
3276           (gnus-undownloaded-mark 131)
3277           (spec gnus-summary-line-format-spec)
3278           gnus-visual pos)
3279       (save-excursion
3280         (gnus-set-work-buffer)
3281         (let ((gnus-summary-line-format-spec spec)
3282               (gnus-newsgroup-downloadable '(0)))
3283           (gnus-summary-insert-line
3284            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3285            0 nil t 128 t nil "" nil 1)
3286           (goto-char (point-min))
3287           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3288                                              (- (point) (point-min) 1)))))
3289           (goto-char (point-min))
3290           (push (cons 'replied (and (search-forward "\201" nil t)
3291                                     (- (point) (point-min) 1)))
3292                 pos)
3293           (goto-char (point-min))
3294           (push (cons 'score (and (search-forward "\202" nil t)
3295                                   (- (point) (point-min) 1)))
3296                 pos)
3297           (goto-char (point-min))
3298           (push (cons 'download
3299                       (and (search-forward "\203" nil t)
3300                            (- (point) (point-min) 1)))
3301                 pos)))
3302       (setq gnus-summary-mark-positions pos))))
3303
3304 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3305   "Insert a dummy root in the summary buffer."
3306   (beginning-of-line)
3307   (gnus-add-text-properties
3308    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3309    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3310
3311 (defun gnus-summary-extract-address-component (from)
3312   (or (car (funcall gnus-extract-address-components from))
3313       from))
3314
3315 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3316   (let ((mail-parse-charset gnus-newsgroup-charset)
3317         ; Is it really necessary to do this next part for each summary line?
3318         ; Luckily, doesn't seem to slow things down much.
3319         (mail-parse-ignored-charsets
3320          (with-current-buffer gnus-summary-buffer
3321            gnus-newsgroup-ignored-charsets)))
3322     (or
3323      (and gnus-ignored-from-addresses
3324           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3325           (let ((extra-headers (mail-header-extra header))
3326                 to
3327                 newsgroups)
3328             (cond
3329              ((setq to (cdr (assq 'To extra-headers)))
3330               (concat "-> "
3331                       (inline
3332                         (gnus-summary-extract-address-component
3333                          (funcall gnus-decode-encoded-word-function to)))))
3334              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3335               (concat "=> " newsgroups)))))
3336      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3337
3338 (defun gnus-summary-insert-line (gnus-tmp-header
3339                                  gnus-tmp-level gnus-tmp-current
3340                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3341                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3342                                  &optional gnus-tmp-dummy gnus-tmp-score
3343                                  gnus-tmp-process)
3344   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3345          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3346          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3347          (gnus-tmp-score-char
3348           (if (or (null gnus-summary-default-score)
3349                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3350                       gnus-summary-zcore-fuzz))
3351               ?                         ;Whitespace
3352             (if (< gnus-tmp-score gnus-summary-default-score)
3353                 gnus-score-below-mark gnus-score-over-mark)))
3354          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3355          (gnus-tmp-replied
3356           (cond (gnus-tmp-process gnus-process-mark)
3357                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3358                  gnus-cached-mark)
3359                 (gnus-tmp-replied gnus-replied-mark)
3360                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3361                  gnus-forwarded-mark)
3362                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3363                  gnus-saved-mark)
3364                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3365                  gnus-recent-mark)
3366                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3367                  gnus-unseen-mark)
3368                 (t gnus-no-mark)))
3369          (gnus-tmp-downloaded
3370           (cond (undownloaded
3371                  gnus-undownloaded-mark)
3372                 (gnus-newsgroup-agentized
3373                  gnus-downloaded-mark)
3374                 (t
3375                  gnus-no-mark)))
3376          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3377          (gnus-tmp-name
3378           (cond
3379            ((string-match "<[^>]+> *$" gnus-tmp-from)
3380             (let ((beg (match-beginning 0)))
3381               (or (and (string-match "^\".+\"" gnus-tmp-from)
3382                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3383                   (substring gnus-tmp-from 0 beg))))
3384            ((string-match "(.+)" gnus-tmp-from)
3385             (substring gnus-tmp-from
3386                        (1+ (match-beginning 0)) (1- (match-end 0))))
3387            (t gnus-tmp-from)))
3388          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3389          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3390          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3391          (buffer-read-only nil))
3392     (when (string= gnus-tmp-name "")
3393       (setq gnus-tmp-name gnus-tmp-from))
3394     (unless (numberp gnus-tmp-lines)
3395       (setq gnus-tmp-lines -1))
3396     (if (= gnus-tmp-lines -1)
3397         (setq gnus-tmp-lines "?")
3398       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3399       (gnus-put-text-property
3400      (point)
3401      (progn (eval gnus-summary-line-format-spec) (point))
3402        'gnus-number gnus-tmp-number)
3403     (when (gnus-visual-p 'summary-highlight 'highlight)
3404       (forward-line -1)
3405       (gnus-run-hooks 'gnus-summary-update-hook)
3406       (forward-line 1))))
3407
3408 (defun gnus-summary-update-line (&optional dont-update)
3409   "Update summary line after change."
3410   (when (and gnus-summary-default-score
3411              (not gnus-summary-inhibit-highlight))
3412     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3413            (article (gnus-summary-article-number))
3414            (score (gnus-summary-article-score article)))
3415       (unless dont-update
3416         (if (and gnus-summary-mark-below
3417                  (< (gnus-summary-article-score)
3418                     gnus-summary-mark-below))
3419             ;; This article has a low score, so we mark it as read.
3420             (when (memq article gnus-newsgroup-unreads)
3421               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3422           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3423             ;; This article was previously marked as read on account
3424             ;; of a low score, but now it has risen, so we mark it as
3425             ;; unread.
3426             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3427         (gnus-summary-update-mark
3428          (if (or (null gnus-summary-default-score)
3429                  (<= (abs (- score gnus-summary-default-score))
3430                      gnus-summary-zcore-fuzz))
3431              ?                          ;Whitespace
3432            (if (< score gnus-summary-default-score)
3433                gnus-score-below-mark gnus-score-over-mark))
3434          'score))
3435       ;; Do visual highlighting.
3436       (when (gnus-visual-p 'summary-highlight 'highlight)
3437         (gnus-run-hooks 'gnus-summary-update-hook)))))
3438
3439 (defvar gnus-tmp-new-adopts nil)
3440
3441 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3442   "Return the number of articles in THREAD.
3443 This may be 0 in some cases -- if none of the articles in
3444 the thread are to be displayed."
3445   (let* ((number
3446          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3447           (cond
3448            ((not (listp thread))
3449             1)
3450            ((and (consp thread) (cdr thread))
3451             (apply
3452              '+ 1 (mapcar
3453                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3454            ((null thread)
3455             1)
3456            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3457             1)
3458            (t 0))))
3459     (when (and level (zerop level) gnus-tmp-new-adopts)
3460       (incf number
3461             (apply '+ (mapcar
3462                        'gnus-summary-number-of-articles-in-thread
3463                        gnus-tmp-new-adopts))))
3464     (if char
3465         (if (> number 1) gnus-not-empty-thread-mark
3466           gnus-empty-thread-mark)
3467       number)))
3468
3469 (defsubst gnus-summary-line-message-size (head)
3470   "Return pretty-printed version of message size.
3471 This function is intended to be used in
3472 `gnus-summary-line-format-alist'."
3473   (let ((c (or (mail-header-chars head) -1)))
3474     (cond ((< c 0) "n/a")               ; chars not available
3475           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3476           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3477           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3478           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3479
3480
3481 (defun gnus-summary-set-local-parameters (group)
3482   "Go through the local params of GROUP and set all variable specs in that list."
3483   (let ((params (gnus-group-find-parameter group))
3484         (vars '(quit-config))           ; Ignore quit-config.
3485         elem)
3486     (while params
3487       (setq elem (car params)
3488             params (cdr params))
3489       (and (consp elem)                 ; Has to be a cons.
3490            (consp (cdr elem))           ; The cdr has to be a list.
3491            (symbolp (car elem))         ; Has to be a symbol in there.
3492            (not (memq (car elem) vars))
3493            (ignore-errors               ; So we set it.
3494              (push (car elem) vars)
3495              (make-local-variable (car elem))
3496              (set (car elem) (eval (nth 1 elem))))))))
3497
3498 (defun gnus-summary-read-group (group &optional show-all no-article
3499                                       kill-buffer no-display backward
3500                                       select-articles)
3501   "Start reading news in newsgroup GROUP.
3502 If SHOW-ALL is non-nil, already read articles are also listed.
3503 If NO-ARTICLE is non-nil, no article is selected initially.
3504 If NO-DISPLAY, don't generate a summary buffer."
3505   (let (result)
3506     (while (and group
3507                 (null (setq result
3508                             (let ((gnus-auto-select-next nil))
3509                               (or (gnus-summary-read-group-1
3510                                    group show-all no-article
3511                                    kill-buffer no-display
3512                                    select-articles)
3513                                   (setq show-all nil
3514                                         select-articles nil)))))
3515                 (eq gnus-auto-select-next 'quietly))
3516       (set-buffer gnus-group-buffer)
3517       ;; The entry function called above goes to the next
3518       ;; group automatically, so we go two groups back
3519       ;; if we are searching for the previous group.
3520       (when backward
3521         (gnus-group-prev-unread-group 2))
3522       (if (not (equal group (gnus-group-group-name)))
3523           (setq group (gnus-group-group-name))
3524         (setq group nil)))
3525     result))
3526
3527 (defun gnus-summary-read-group-1 (group show-all no-article
3528                                         kill-buffer no-display
3529                                         &optional select-articles)
3530   ;; Killed foreign groups can't be entered.
3531   ;;  (when (and (not (gnus-group-native-p group))
3532   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3533   ;;    (error "Dead non-native groups can't be entered"))
3534   (gnus-message 5 "Retrieving newsgroup: %s..."
3535                 (gnus-group-decoded-name group))
3536   (let* ((new-group (gnus-summary-setup-buffer group))
3537          (quit-config (gnus-group-quit-config group))
3538          (did-select (and new-group (gnus-select-newsgroup
3539                                      group show-all select-articles))))
3540     (cond
3541      ;; This summary buffer exists already, so we just select it.
3542      ((not new-group)
3543       (gnus-set-global-variables)
3544       (when kill-buffer
3545         (gnus-kill-or-deaden-summary kill-buffer))
3546       (gnus-configure-windows 'summary 'force)
3547       (gnus-set-mode-line 'summary)
3548       (gnus-summary-position-point)
3549       (message "")
3550       t)
3551      ;; We couldn't select this group.
3552      ((null did-select)
3553       (when (and (eq major-mode 'gnus-summary-mode)
3554                  (not (equal (current-buffer) kill-buffer)))
3555         (kill-buffer (current-buffer))
3556         (if (not quit-config)
3557             (progn
3558               ;; Update the info -- marks might need to be removed,
3559               ;; for instance.
3560               (gnus-summary-update-info)
3561               (set-buffer gnus-group-buffer)
3562               (gnus-group-jump-to-group group)
3563               (gnus-group-next-unread-group 1))
3564           (gnus-handle-ephemeral-exit quit-config)))
3565       (let ((grpinfo (gnus-get-info group)))
3566         (if (null (gnus-info-read grpinfo))
3567             (gnus-message 3 "Group %s contains no messages"
3568                           (gnus-group-decoded-name group))
3569           (gnus-message 3 "Can't select group")))
3570       nil)
3571      ;; The user did a `C-g' while prompting for number of articles,
3572      ;; so we exit this group.
3573      ((eq did-select 'quit)
3574       (and (eq major-mode 'gnus-summary-mode)
3575            (not (equal (current-buffer) kill-buffer))
3576            (kill-buffer (current-buffer)))
3577       (when kill-buffer
3578         (gnus-kill-or-deaden-summary kill-buffer))
3579       (if (not quit-config)
3580           (progn
3581             (set-buffer gnus-group-buffer)
3582             (gnus-group-jump-to-group group)
3583             (gnus-group-next-unread-group 1)
3584             (gnus-configure-windows 'group 'force))
3585         (gnus-handle-ephemeral-exit quit-config))
3586       ;; Finally signal the quit.
3587       (signal 'quit nil))
3588      ;; The group was successfully selected.
3589      (t
3590       (gnus-set-global-variables)
3591       ;; Save the active value in effect when the group was entered.
3592       (setq gnus-newsgroup-active
3593             (gnus-copy-sequence
3594              (gnus-active gnus-newsgroup-name)))
3595       ;; You can change the summary buffer in some way with this hook.
3596       (gnus-run-hooks 'gnus-select-group-hook)
3597       (gnus-update-format-specifications
3598        nil 'summary 'summary-mode 'summary-dummy)
3599       (gnus-update-summary-mark-positions)
3600       ;; Do score processing.
3601       (when gnus-use-scoring
3602         (gnus-possibly-score-headers))
3603       ;; Check whether to fill in the gaps in the threads.
3604       (when gnus-build-sparse-threads
3605         (gnus-build-sparse-threads))
3606       ;; Find the initial limit.
3607       (if show-all
3608           (let ((gnus-newsgroup-dormant nil))
3609             (gnus-summary-initial-limit show-all))
3610         (gnus-summary-initial-limit show-all))
3611       ;; Generate the summary buffer.
3612       (unless no-display
3613         (gnus-summary-prepare))
3614       (when gnus-use-trees
3615         (gnus-tree-open group)
3616         (setq gnus-summary-highlight-line-function
3617               'gnus-tree-highlight-article))
3618       ;; If the summary buffer is empty, but there are some low-scored
3619       ;; articles or some excluded dormants, we include these in the
3620       ;; buffer.
3621       (when (and (zerop (buffer-size))
3622                  (not no-display))
3623         (cond (gnus-newsgroup-dormant
3624                (gnus-summary-limit-include-dormant))
3625               ((and gnus-newsgroup-scored show-all)
3626                (gnus-summary-limit-include-expunged t))))
3627       ;; Function `gnus-apply-kill-file' must be called in this hook.
3628       (gnus-run-hooks 'gnus-apply-kill-hook)
3629       (if (and (zerop (buffer-size))
3630                (not no-display))
3631           (progn
3632             ;; This newsgroup is empty.
3633             (gnus-summary-catchup-and-exit nil t)
3634             (gnus-message 6 "No unread news")
3635             (when kill-buffer
3636               (gnus-kill-or-deaden-summary kill-buffer))
3637             ;; Return nil from this function.
3638             nil)
3639         ;; Hide conversation thread subtrees.  We cannot do this in
3640         ;; gnus-summary-prepare-hook since kill processing may not
3641         ;; work with hidden articles.
3642         (gnus-summary-maybe-hide-threads)
3643         (when kill-buffer
3644           (gnus-kill-or-deaden-summary kill-buffer))
3645         (gnus-summary-auto-select-subject)
3646         ;; Show first unread article if requested.
3647         (if (and (not no-article)
3648                  (not no-display)
3649                  gnus-newsgroup-unreads
3650                  gnus-auto-select-first)
3651             (progn
3652               (gnus-configure-windows 'summary)
3653               (let ((art (gnus-summary-article-number)))
3654                 (unless (and (not gnus-plugged)
3655                              (or (memq art gnus-newsgroup-undownloaded)
3656                                  (memq art gnus-newsgroup-downloadable)))
3657                   (gnus-summary-goto-article art))))
3658           ;; Don't select any articles.
3659           (gnus-summary-position-point)
3660           (gnus-configure-windows 'summary 'force)
3661           (gnus-set-mode-line 'summary))
3662         (when (and gnus-auto-center-group
3663                    (get-buffer-window gnus-group-buffer t))
3664           ;; Gotta use windows, because recenter does weird stuff if
3665           ;; the current buffer ain't the displayed window.
3666           (let ((owin (selected-window)))
3667             (select-window (get-buffer-window gnus-group-buffer t))
3668             (when (gnus-group-goto-group group)
3669               (recenter))
3670             (select-window owin)))
3671         ;; Mark this buffer as "prepared".
3672         (setq gnus-newsgroup-prepared t)
3673         (gnus-run-hooks 'gnus-summary-prepared-hook)
3674         (unless (gnus-ephemeral-group-p group)
3675           (gnus-group-update-group group))
3676         t)))))
3677
3678 (defun gnus-summary-auto-select-subject ()
3679   "Select the subject line on initial group entry."
3680   (goto-char (point-min))
3681   (cond
3682    ((eq gnus-auto-select-subject 'best)
3683     (gnus-summary-best-unread-subject))
3684    ((eq gnus-auto-select-subject 'unread)
3685     (gnus-summary-first-unread-subject))
3686    ((eq gnus-auto-select-subject 'unseen)
3687     (gnus-summary-first-unseen-subject))
3688    ((eq gnus-auto-select-subject 'unseen-or-unread)
3689     (gnus-summary-first-unseen-or-unread-subject))
3690    ((eq gnus-auto-select-subject 'first)
3691     ;; Do nothing.
3692     )
3693    ((functionp gnus-auto-select-subject)
3694     (funcall gnus-auto-select-subject))))
3695
3696 (defun gnus-summary-prepare ()
3697   "Generate the summary buffer."
3698   (interactive)
3699   (let ((buffer-read-only nil))
3700     (erase-buffer)
3701     (setq gnus-newsgroup-data nil
3702           gnus-newsgroup-data-reverse nil)
3703     (gnus-run-hooks 'gnus-summary-generate-hook)
3704     ;; Generate the buffer, either with threads or without.
3705     (when gnus-newsgroup-headers
3706       (gnus-summary-prepare-threads
3707        (if gnus-show-threads
3708            (gnus-sort-gathered-threads
3709             (funcall gnus-summary-thread-gathering-function
3710                      (gnus-sort-threads
3711                       (gnus-cut-threads (gnus-make-threads)))))
3712          ;; Unthreaded display.
3713          (gnus-sort-articles gnus-newsgroup-headers))))
3714     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3715     ;; Call hooks for modifying summary buffer.
3716     (goto-char (point-min))
3717     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3718
3719 (defsubst gnus-general-simplify-subject (subject)
3720   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3721   (setq subject
3722         (cond
3723          ;; Truncate the subject.
3724          (gnus-simplify-subject-functions
3725           (gnus-map-function gnus-simplify-subject-functions subject))
3726          ((numberp gnus-summary-gather-subject-limit)
3727           (setq subject (gnus-simplify-subject-re subject))
3728           (if (> (length subject) gnus-summary-gather-subject-limit)
3729               (substring subject 0 gnus-summary-gather-subject-limit)
3730             subject))
3731          ;; Fuzzily simplify it.
3732          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3733           (gnus-simplify-subject-fuzzy subject))
3734          ;; Just remove the leading "Re:".
3735          (t
3736           (gnus-simplify-subject-re subject))))
3737
3738   (if (and gnus-summary-gather-exclude-subject
3739            (string-match gnus-summary-gather-exclude-subject subject))
3740       nil                         ; This article shouldn't be gathered
3741     subject))
3742
3743 (defun gnus-summary-simplify-subject-query ()
3744   "Query where the respool algorithm would put this article."
3745   (interactive)
3746   (gnus-summary-select-article)
3747   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3748
3749 (defun gnus-gather-threads-by-subject (threads)
3750   "Gather threads by looking at Subject headers."
3751   (if (not gnus-summary-make-false-root)
3752       threads
3753     (let ((hashtb (gnus-make-hashtable 1024))
3754           (prev threads)
3755           (result threads)
3756           subject hthread whole-subject)
3757       (while threads
3758         (setq subject (gnus-general-simplify-subject
3759                        (setq whole-subject (mail-header-subject
3760                                             (caar threads)))))
3761         (when subject
3762           (if (setq hthread (gnus-gethash subject hashtb))
3763               (progn
3764                 ;; We enter a dummy root into the thread, if we
3765                 ;; haven't done that already.
3766                 (unless (stringp (caar hthread))
3767                   (setcar hthread (list whole-subject (car hthread))))
3768                 ;; We add this new gathered thread to this gathered
3769                 ;; thread.
3770                 (setcdr (car hthread)
3771                         (nconc (cdar hthread) (list (car threads))))
3772                 ;; Remove it from the list of threads.
3773                 (setcdr prev (cdr threads))
3774                 (setq threads prev))
3775             ;; Enter this thread into the hash table.
3776             (gnus-sethash subject
3777                           (if gnus-summary-make-false-root-always
3778                               (progn
3779                                 ;; If you want a dummy root above all
3780                                 ;; threads...
3781                                 (setcar threads (list whole-subject
3782                                                       (car threads)))
3783                                 threads)
3784                             threads)
3785                           hashtb)))
3786         (setq prev threads)
3787         (setq threads (cdr threads)))
3788       result)))
3789
3790 (defun gnus-gather-threads-by-references (threads)
3791   "Gather threads by looking at References headers."
3792   (let ((idhashtb (gnus-make-hashtable 1024))
3793         (thhashtb (gnus-make-hashtable 1024))
3794         (prev threads)
3795         (result threads)
3796         ids references id gthread gid entered ref)
3797     (while threads
3798       (when (setq references (mail-header-references (caar threads)))
3799         (setq id (mail-header-id (caar threads))
3800               ids (inline (gnus-split-references references))
3801               entered nil)
3802         (while (setq ref (pop ids))
3803           (setq ids (delete ref ids))
3804           (if (not (setq gid (gnus-gethash ref idhashtb)))
3805               (progn
3806                 (gnus-sethash ref id idhashtb)
3807                 (gnus-sethash id threads thhashtb))
3808             (setq gthread (gnus-gethash gid thhashtb))
3809             (unless entered
3810               ;; We enter a dummy root into the thread, if we
3811               ;; haven't done that already.
3812               (unless (stringp (caar gthread))
3813                 (setcar gthread (list (mail-header-subject (caar gthread))
3814                                       (car gthread))))
3815               ;; We add this new gathered thread to this gathered
3816               ;; thread.
3817               (setcdr (car gthread)
3818                       (nconc (cdar gthread) (list (car threads)))))
3819             ;; Add it into the thread hash table.
3820             (gnus-sethash id gthread thhashtb)
3821             (setq entered t)
3822             ;; Remove it from the list of threads.
3823             (setcdr prev (cdr threads))
3824             (setq threads prev))))
3825       (setq prev threads)
3826       (setq threads (cdr threads)))
3827     result))
3828
3829 (defun gnus-sort-gathered-threads (threads)
3830   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3831   (let ((result threads))
3832     (while threads
3833       (when (stringp (caar threads))
3834         (setcdr (car threads)
3835                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3836       (setq threads (cdr threads)))
3837     result))
3838
3839 (defun gnus-thread-loop-p (root thread)
3840   "Say whether ROOT is in THREAD."
3841   (let ((stack (list thread))
3842         (infloop 0)
3843         th)
3844     (while (setq thread (pop stack))
3845       (setq th (cdr thread))
3846       (while (and th
3847                   (not (eq (caar th) root)))
3848         (pop th))
3849       (if th
3850           ;; We have found a loop.
3851           (let (ref-dep)
3852             (setcdr thread (delq (car th) (cdr thread)))
3853             (if (boundp (setq ref-dep (intern "none"
3854                                               gnus-newsgroup-dependencies)))
3855                 (setcdr (symbol-value ref-dep)
3856                         (nconc (cdr (symbol-value ref-dep))
3857                                (list (car th))))
3858               (set ref-dep (list nil (car th))))
3859             (setq infloop 1
3860                   stack nil))
3861         ;; Push all the subthreads onto the stack.
3862         (push (cdr thread) stack)))
3863     infloop))
3864
3865 (defun gnus-make-threads ()
3866   "Go through the dependency hashtb and find the roots.  Return all threads."
3867   (let (threads)
3868     (while (catch 'infloop
3869              (mapatoms
3870               (lambda (refs)
3871                 ;; Deal with self-referencing References loops.
3872                 (when (and (car (symbol-value refs))
3873                            (not (zerop
3874                                  (apply
3875                                   '+
3876                                   (mapcar
3877                                    (lambda (thread)
3878                                      (gnus-thread-loop-p
3879                                       (car (symbol-value refs)) thread))
3880                                    (cdr (symbol-value refs)))))))
3881                   (setq threads nil)
3882                   (throw 'infloop t))
3883                 (unless (car (symbol-value refs))
3884                   ;; These threads do not refer back to any other
3885                   ;; articles, so they're roots.
3886                   (setq threads (append (cdr (symbol-value refs)) threads))))
3887               gnus-newsgroup-dependencies)))
3888     threads))
3889
3890 ;; Build the thread tree.
3891 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3892   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3893
3894 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3895 if it was already present.
3896
3897 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3898 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3899 Message-IDs will be renamed to a unique Message-ID before being
3900 entered.
3901
3902 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3903   (let* ((id (mail-header-id header))
3904          (id-dep (and id (intern id dependencies)))
3905          parent-id ref ref-dep ref-header replaced)
3906     ;; Enter this `header' in the `dependencies' table.
3907     (cond
3908      ((not id-dep)
3909       (setq header nil))
3910      ;; The first two cases do the normal part: enter a new `header'
3911      ;; in the `dependencies' table.
3912      ((not (boundp id-dep))
3913       (set id-dep (list header)))
3914      ((null (car (symbol-value id-dep)))
3915       (setcar (symbol-value id-dep) header))
3916
3917      ;; From here the `header' was already present in the
3918      ;; `dependencies' table.
3919      (force-new
3920       ;; Overrides an existing entry;
3921       ;; just set the header part of the entry.
3922       (setcar (symbol-value id-dep) header)
3923       (setq replaced t))
3924
3925      ;; Renames the existing `header' to a unique Message-ID.
3926      ((not gnus-summary-ignore-duplicates)
3927       ;; An article with this Message-ID has already been seen.
3928       ;; We rename the Message-ID.
3929       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3930            (list header))
3931       (mail-header-set-id header id))
3932
3933      ;; The last case ignores an existing entry, except it adds any
3934      ;; additional Xrefs (in case the two articles came from different
3935      ;; servers.
3936      ;; Also sets `header' to `nil' meaning that the `dependencies'
3937      ;; table was *not* modified.
3938      (t
3939       (mail-header-set-xref
3940        (car (symbol-value id-dep))
3941        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3942                    "")
3943                (or (mail-header-xref header) "")))
3944       (setq header nil)))
3945
3946     (when (and header (not replaced))
3947       ;; First check that we are not creating a References loop.
3948       (setq parent-id (gnus-parent-id (mail-header-references header)))
3949       (setq ref parent-id)
3950       (while (and ref
3951                   (setq ref-dep (intern-soft ref dependencies))
3952                   (boundp ref-dep)
3953                   (setq ref-header (car (symbol-value ref-dep))))
3954         (if (string= id ref)
3955             ;; Yuk!  This is a reference loop.  Make the article be a
3956             ;; root article.
3957             (progn
3958               (mail-header-set-references (car (symbol-value id-dep)) "none")
3959               (setq ref nil)
3960               (setq parent-id nil))
3961           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3962       (setq ref-dep (intern (or parent-id "none") dependencies))
3963       (if (boundp ref-dep)
3964           (setcdr (symbol-value ref-dep)
3965                   (nconc (cdr (symbol-value ref-dep))
3966                          (list (symbol-value id-dep))))
3967         (set ref-dep (list nil (symbol-value id-dep)))))
3968     header))
3969
3970 (defun gnus-extract-message-id-from-in-reply-to (string)
3971   (if (string-match "<[^>]+>" string)
3972       (substring string (match-beginning 0) (match-end 0))
3973     nil))
3974
3975 (defun gnus-build-sparse-threads ()
3976   (let ((headers gnus-newsgroup-headers)
3977         (mail-parse-charset gnus-newsgroup-charset)
3978         (gnus-summary-ignore-duplicates t)
3979         header references generation relations
3980         subject child end new-child date)
3981     ;; First we create an alist of generations/relations, where
3982     ;; generations is how much we trust the relation, and the relation
3983     ;; is parent/child.
3984     (gnus-message 7 "Making sparse threads...")
3985     (save-excursion
3986       (nnheader-set-temp-buffer " *gnus sparse threads*")
3987       (while (setq header (pop headers))
3988         (when (and (setq references (mail-header-references header))
3989                    (not (string= references "")))
3990           (insert references)
3991           (setq child (mail-header-id header)
3992                 subject (mail-header-subject header)
3993                 date (mail-header-date header)
3994                 generation 0)
3995           (while (search-backward ">" nil t)
3996             (setq end (1+ (point)))
3997             (when (search-backward "<" nil t)
3998               (setq new-child (buffer-substring (point) end))
3999               (push (list (incf generation)
4000                           child (setq child new-child)
4001                           subject date)
4002                     relations)))
4003           (when child
4004             (push (list (1+ generation) child nil subject) relations))
4005           (erase-buffer)))
4006       (kill-buffer (current-buffer)))
4007     ;; Sort over trustworthiness.
4008     (mapcar
4009      (lambda (relation)
4010        (when (gnus-dependencies-add-header
4011               (make-full-mail-header
4012                gnus-reffed-article-number
4013                (nth 3 relation) "" (or (nth 4 relation) "")
4014                (nth 1 relation)
4015                (or (nth 2 relation) "") 0 0 "")
4016               gnus-newsgroup-dependencies nil)
4017          (push gnus-reffed-article-number gnus-newsgroup-limit)
4018          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4019          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4020                gnus-newsgroup-reads)
4021          (decf gnus-reffed-article-number)))
4022      (sort relations 'car-less-than-car))
4023     (gnus-message 7 "Making sparse threads...done")))
4024
4025 (defun gnus-build-old-threads ()
4026   ;; Look at all the articles that refer back to old articles, and
4027   ;; fetch the headers for the articles that aren't there.  This will
4028   ;; build complete threads - if the roots haven't been expired by the
4029   ;; server, that is.
4030   (let ((mail-parse-charset gnus-newsgroup-charset)
4031         id heads)
4032     (mapatoms
4033      (lambda (refs)
4034        (when (not (car (symbol-value refs)))
4035          (setq heads (cdr (symbol-value refs)))
4036          (while heads
4037            (if (memq (mail-header-number (caar heads))
4038                      gnus-newsgroup-dormant)
4039                (setq heads (cdr heads))
4040              (setq id (symbol-name refs))
4041              (while (and (setq id (gnus-build-get-header id))
4042                          (not (car (gnus-id-to-thread id)))))
4043              (setq heads nil)))))
4044      gnus-newsgroup-dependencies)))
4045
4046 (defsubst gnus-remove-odd-characters (string)
4047   "Translate STRING into something that doesn't contain weird characters."
4048   (mm-subst-char-in-string
4049    ?\r ?\-
4050    (mm-subst-char-in-string ?\n ?\- string t) t))
4051
4052 ;; This function has to be called with point after the article number
4053 ;; on the beginning of the line.
4054 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4055   (let ((eol (point-at-eol))
4056         (buffer (current-buffer))
4057         header references in-reply-to)
4058
4059     ;; overview: [num subject from date id refs chars lines misc]
4060     (unwind-protect
4061         (let (x)
4062           (narrow-to-region (point) eol)
4063           (unless (eobp)
4064             (forward-char))
4065
4066           (setq header
4067                 (make-full-mail-header
4068                  number                 ; number
4069                  (condition-case ()     ; subject
4070                      (gnus-remove-odd-characters
4071                       (funcall gnus-decode-encoded-word-function
4072                                (setq x (nnheader-nov-field))))
4073                    (error x))
4074                  (condition-case ()     ; from
4075                      (gnus-remove-odd-characters
4076                       (funcall gnus-decode-encoded-word-function
4077                                (setq x (nnheader-nov-field))))
4078                    (error x))
4079                  (nnheader-nov-field)   ; date
4080                  (nnheader-nov-read-message-id number)  ; id
4081                  (setq references (nnheader-nov-field)) ; refs
4082                  (nnheader-nov-read-integer) ; chars
4083                  (nnheader-nov-read-integer) ; lines
4084                  (unless (eobp)
4085                    (if (looking-at "Xref: ")
4086                        (goto-char (match-end 0)))
4087                    (nnheader-nov-field)) ; Xref
4088                  (nnheader-nov-parse-extra)))) ; extra
4089
4090       (widen))
4091
4092     (when (and (string= references "")
4093                (setq in-reply-to (mail-header-extra header))
4094                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4095       (mail-header-set-references
4096        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4097
4098     (when gnus-alter-header-function
4099       (funcall gnus-alter-header-function header))
4100     (gnus-dependencies-add-header header dependencies force-new)))
4101
4102 (defun gnus-build-get-header (id)
4103   "Look through the buffer of NOV lines and find the header to ID.
4104 Enter this line into the dependencies hash table, and return
4105 the id of the parent article (if any)."
4106   (let ((deps gnus-newsgroup-dependencies)
4107         found header)
4108     (prog1
4109         (save-excursion
4110           (set-buffer nntp-server-buffer)
4111           (let ((case-fold-search nil))
4112             (goto-char (point-min))
4113             (while (and (not found)
4114                         (search-forward id nil t))
4115               (beginning-of-line)
4116               (setq found (looking-at
4117                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4118                                    (regexp-quote id))))
4119               (or found (beginning-of-line 2)))
4120             (when found
4121               (beginning-of-line)
4122               (and
4123                (setq header (gnus-nov-parse-line
4124                              (read (current-buffer)) deps))
4125                (gnus-parent-id (mail-header-references header))))))
4126       (when header
4127         (let ((number (mail-header-number header)))
4128           (push number gnus-newsgroup-limit)
4129           (push header gnus-newsgroup-headers)
4130           (if (memq number gnus-newsgroup-unselected)
4131               (progn
4132                 (setq gnus-newsgroup-unreads
4133                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4134                                                number))
4135                 (setq gnus-newsgroup-unselected
4136                       (delq number gnus-newsgroup-unselected)))
4137             (push number gnus-newsgroup-ancient)))))))
4138
4139 (defun gnus-build-all-threads ()
4140   "Read all the headers."
4141   (let ((gnus-summary-ignore-duplicates t)
4142         (mail-parse-charset gnus-newsgroup-charset)
4143         (dependencies gnus-newsgroup-dependencies)
4144         header article)
4145     (save-excursion
4146       (set-buffer nntp-server-buffer)
4147       (let ((case-fold-search nil))
4148         (goto-char (point-min))
4149         (while (not (eobp))
4150           (ignore-errors
4151             (setq article (read (current-buffer))
4152                   header (gnus-nov-parse-line article dependencies)))
4153           (when header
4154             (with-current-buffer gnus-summary-buffer
4155               (push header gnus-newsgroup-headers)
4156               (if (memq (setq article (mail-header-number header))
4157                         gnus-newsgroup-unselected)
4158                   (progn
4159                     (setq gnus-newsgroup-unreads
4160                           (gnus-add-to-sorted-list
4161                            gnus-newsgroup-unreads article))
4162                     (setq gnus-newsgroup-unselected
4163                           (delq article gnus-newsgroup-unselected)))
4164                 (push article gnus-newsgroup-ancient)))
4165             (forward-line 1)))))))
4166
4167 (defun gnus-summary-update-article-line (article header)
4168   "Update the line for ARTICLE using HEADER."
4169   (let* ((id (mail-header-id header))
4170          (thread (gnus-id-to-thread id)))
4171     (unless thread
4172       (error "Article in no thread"))
4173     ;; Update the thread.
4174     (setcar thread header)
4175     (gnus-summary-goto-subject article)
4176     (let* ((datal (gnus-data-find-list article))
4177            (data (car datal))
4178            (buffer-read-only nil)
4179            (level (gnus-summary-thread-level)))
4180       (gnus-delete-line)
4181       (let ((inserted (- (point)
4182                          (progn
4183                            (gnus-summary-insert-line
4184                             header level nil
4185                             (memq article gnus-newsgroup-undownloaded)
4186                             (gnus-article-mark article)
4187                             (memq article gnus-newsgroup-replied)
4188                             (memq article gnus-newsgroup-expirable)
4189                             ;; Only insert the Subject string when it's different
4190                             ;; from the previous Subject string.
4191                             (if (and
4192                                  gnus-show-threads
4193                                  (gnus-subject-equal
4194                                   (condition-case ()
4195                                       (mail-header-subject
4196                                        (gnus-data-header
4197                                         (cadr
4198                                          (gnus-data-find-list
4199                                           article
4200                                           (gnus-data-list t)))))
4201                                     ;; Error on the side of excessive subjects.
4202                                     (error ""))
4203                                   (mail-header-subject header)))
4204                                 ""
4205                               (mail-header-subject header))
4206                             nil (cdr (assq article gnus-newsgroup-scored))
4207                             (memq article gnus-newsgroup-processable))
4208                            (point)))))
4209         (when (cdr datal)
4210           (gnus-data-update-list
4211            (cdr datal)
4212            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4213
4214 (defun gnus-summary-update-article (article &optional iheader)
4215   "Update ARTICLE in the summary buffer."
4216   (set-buffer gnus-summary-buffer)
4217   (let* ((header (gnus-summary-article-header article))
4218          (id (mail-header-id header))
4219          (data (gnus-data-find article))
4220          (thread (gnus-id-to-thread id))
4221          (references (mail-header-references header))
4222          (parent
4223           (gnus-id-to-thread
4224            (or (gnus-parent-id
4225                 (when (and references
4226                            (not (equal "" references)))
4227                   references))
4228                "none")))
4229          (buffer-read-only nil)
4230          (old (car thread)))
4231     (when thread
4232       (unless iheader
4233         (setcar thread nil)
4234         (when parent
4235           (delq thread parent)))
4236       (if (gnus-summary-insert-subject id header)
4237           ;; Set the (possibly) new article number in the data structure.
4238           (gnus-data-set-number data (gnus-id-to-article id))
4239         (setcar thread old)
4240         nil))))
4241
4242 (defun gnus-rebuild-thread (id &optional line)
4243   "Rebuild the thread containing ID.
4244 If LINE, insert the rebuilt thread starting on line LINE."
4245   (let ((buffer-read-only nil)
4246         old-pos current thread data)
4247     (if (not gnus-show-threads)
4248         (setq thread (list (car (gnus-id-to-thread id))))
4249       ;; Get the thread this article is part of.
4250       (setq thread (gnus-remove-thread id)))
4251     (setq old-pos (point-at-bol))
4252     (setq current (save-excursion
4253                     (and (re-search-backward "[\r\n]" nil t)
4254                          (gnus-summary-article-number))))
4255     ;; If this is a gathered thread, we have to go some re-gathering.
4256     (when (stringp (car thread))
4257       (let ((subject (car thread))
4258             roots thr)
4259         (setq thread (cdr thread))
4260         (while thread
4261           (unless (memq (setq thr (gnus-id-to-thread
4262                                    (gnus-root-id
4263                                     (mail-header-id (caar thread)))))
4264                         roots)
4265             (push thr roots))
4266           (setq thread (cdr thread)))
4267         ;; We now have all (unique) roots.
4268         (if (= (length roots) 1)
4269             ;; All the loose roots are now one solid root.
4270             (setq thread (car roots))
4271           (setq thread (cons subject (gnus-sort-threads roots))))))
4272     (let (threads)
4273       ;; We then insert this thread into the summary buffer.
4274       (when line
4275         (goto-char (point-min))
4276         (forward-line (1- line)))
4277       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4278         (if gnus-show-threads
4279             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4280           (gnus-summary-prepare-unthreaded thread))
4281         (setq data (nreverse gnus-newsgroup-data))
4282         (setq threads gnus-newsgroup-threads))
4283       ;; We splice the new data into the data structure.
4284       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4285       ;;!!! then we want to insert at the beginning of the buffer.
4286       ;;!!! That happens to be true with Gnus now, but that may
4287       ;;!!! change in the future.  Perhaps.
4288       (gnus-data-enter-list
4289        (if line nil current) data (- (point) old-pos))
4290       (setq gnus-newsgroup-threads
4291             (nconc threads gnus-newsgroup-threads))
4292       (gnus-data-compute-positions))))
4293
4294 (defun gnus-number-to-header (number)
4295   "Return the header for article NUMBER."
4296   (let ((headers gnus-newsgroup-headers))
4297     (while (and headers
4298                 (not (= number (mail-header-number (car headers)))))
4299       (pop headers))
4300     (when headers
4301       (car headers))))
4302
4303 (defun gnus-parent-headers (in-headers &optional generation)
4304   "Return the headers of the GENERATIONeth parent of HEADERS."
4305   (unless generation
4306     (setq generation 1))
4307   (let ((parent t)
4308         (headers in-headers)
4309         references)
4310     (while (and parent
4311                 (not (zerop generation))
4312                 (setq references (mail-header-references headers)))
4313       (setq headers (if (and references
4314                              (setq parent (gnus-parent-id references)))
4315                         (car (gnus-id-to-thread parent))
4316                       nil))
4317       (decf generation))
4318     (and (not (eq headers in-headers))
4319          headers)))
4320
4321 (defun gnus-id-to-thread (id)
4322   "Return the (sub-)thread where ID appears."
4323   (gnus-gethash id gnus-newsgroup-dependencies))
4324
4325 (defun gnus-id-to-article (id)
4326   "Return the article number of ID."
4327   (let ((thread (gnus-id-to-thread id)))
4328     (when (and thread
4329                (car thread))
4330       (mail-header-number (car thread)))))
4331
4332 (defun gnus-id-to-header (id)
4333   "Return the article headers of ID."
4334   (car (gnus-id-to-thread id)))
4335
4336 (defun gnus-article-displayed-root-p (article)
4337   "Say whether ARTICLE is a root(ish) article."
4338   (let ((level (gnus-summary-thread-level article))
4339         (refs (mail-header-references  (gnus-summary-article-header article)))
4340         particle)
4341     (cond
4342      ((null level) nil)
4343      ((zerop level) t)
4344      ((null refs) t)
4345      ((null (gnus-parent-id refs)) t)
4346      ((and (= 1 level)
4347            (null (setq particle (gnus-id-to-article
4348                                  (gnus-parent-id refs))))
4349            (null (gnus-summary-thread-level particle)))))))
4350
4351 (defun gnus-root-id (id)
4352   "Return the id of the root of the thread where ID appears."
4353   (let (last-id prev)
4354     (while (and id (setq prev (car (gnus-id-to-thread id))))
4355       (setq last-id id
4356             id (gnus-parent-id (mail-header-references prev))))
4357     last-id))
4358
4359 (defun gnus-articles-in-thread (thread)
4360   "Return the list of articles in THREAD."
4361   (cons (mail-header-number (car thread))
4362         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4363
4364 (defun gnus-remove-thread (id &optional dont-remove)
4365   "Remove the thread that has ID in it."
4366   (let (headers thread last-id)
4367     ;; First go up in this thread until we find the root.
4368     (setq last-id (gnus-root-id id)
4369           headers (message-flatten-list (gnus-id-to-thread last-id)))
4370     ;; We have now found the real root of this thread.  It might have
4371     ;; been gathered into some loose thread, so we have to search
4372     ;; through the threads to find the thread we wanted.
4373     (let ((threads gnus-newsgroup-threads)
4374           sub)
4375       (while threads
4376         (setq sub (car threads))
4377         (if (stringp (car sub))
4378             ;; This is a gathered thread, so we look at the roots
4379             ;; below it to find whether this article is in this
4380             ;; gathered root.
4381             (progn
4382               (setq sub (cdr sub))
4383               (while sub
4384                 (when (member (caar sub) headers)
4385                   (setq thread (car threads)
4386                         threads nil
4387                         sub nil))
4388                 (setq sub (cdr sub))))
4389           ;; It's an ordinary thread, so we check it.
4390           (when (eq (car sub) (car headers))
4391             (setq thread sub
4392                   threads nil)))
4393         (setq threads (cdr threads)))
4394       ;; If this article is in no thread, then it's a root.
4395       (if thread
4396           (unless dont-remove
4397             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4398         (setq thread (gnus-id-to-thread last-id)))
4399       (when thread
4400         (prog1
4401             thread                      ; We return this thread.
4402           (unless dont-remove
4403             (if (stringp (car thread))
4404                 (progn
4405                   ;; If we use dummy roots, then we have to remove the
4406                   ;; dummy root as well.
4407                   (when (eq gnus-summary-make-false-root 'dummy)
4408                     ;; We go to the dummy root by going to
4409                     ;; the first sub-"thread", and then one line up.
4410                     (gnus-summary-goto-article
4411                      (mail-header-number (caadr thread)))
4412                     (forward-line -1)
4413                     (gnus-delete-line)
4414                     (gnus-data-compute-positions))
4415                   (setq thread (cdr thread))
4416                   (while thread
4417                     (gnus-remove-thread-1 (car thread))
4418                     (setq thread (cdr thread))))
4419               (gnus-remove-thread-1 thread))))))))
4420
4421 (defun gnus-remove-thread-1 (thread)
4422   "Remove the thread THREAD recursively."
4423   (let ((number (mail-header-number (pop thread)))
4424         d)
4425     (setq thread (reverse thread))
4426     (while thread
4427       (gnus-remove-thread-1 (pop thread)))
4428     (when (setq d (gnus-data-find number))
4429       (goto-char (gnus-data-pos d))
4430       (gnus-summary-show-thread)
4431       (gnus-data-remove
4432        number
4433        (- (point-at-bol)
4434           (prog1
4435               (1+ (point-at-eol))
4436             (gnus-delete-line)))))))
4437
4438 (defun gnus-sort-threads-1 (threads func)
4439   (sort (mapcar (lambda (thread)
4440                   (cons (car thread)
4441                         (and (cdr thread)
4442                              (gnus-sort-threads-1 (cdr thread) func))))
4443                 threads) func))
4444
4445 (defun gnus-sort-threads (threads)
4446   "Sort THREADS."
4447   (if (not gnus-thread-sort-functions)
4448       threads
4449     (gnus-message 8 "Sorting threads...")
4450     (let ((max-lisp-eval-depth 5000))
4451       (prog1 (gnus-sort-threads-1
4452          threads
4453          (gnus-make-sort-function gnus-thread-sort-functions))
4454         (gnus-message 8 "Sorting threads...done")))))
4455
4456 (defun gnus-sort-articles (articles)
4457   "Sort ARTICLES."
4458   (when gnus-article-sort-functions
4459     (gnus-message 7 "Sorting articles...")
4460     (prog1
4461         (setq gnus-newsgroup-headers
4462               (sort articles (gnus-make-sort-function
4463                               gnus-article-sort-functions)))
4464       (gnus-message 7 "Sorting articles...done"))))
4465
4466 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4467 (defmacro gnus-thread-header (thread)
4468   "Return header of first article in THREAD.
4469 Note that THREAD must never, ever be anything else than a variable -
4470 using some other form will lead to serious barfage."
4471   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4472   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4473   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4474         (vector thread) 2))
4475
4476 (defsubst gnus-article-sort-by-number (h1 h2)
4477   "Sort articles by article number."
4478   (< (mail-header-number h1)
4479      (mail-header-number h2)))
4480
4481 (defun gnus-thread-sort-by-number (h1 h2)
4482   "Sort threads by root article number."
4483   (gnus-article-sort-by-number
4484    (gnus-thread-header h1) (gnus-thread-header h2)))
4485
4486 (defsubst gnus-article-sort-by-random (h1 h2)
4487   "Sort articles by article number."
4488   (zerop (random 2)))
4489
4490 (defun gnus-thread-sort-by-random (h1 h2)
4491   "Sort threads by root article number."
4492   (gnus-article-sort-by-random
4493    (gnus-thread-header h1) (gnus-thread-header h2)))
4494
4495 (defsubst gnus-article-sort-by-lines (h1 h2)
4496   "Sort articles by article Lines header."
4497   (< (mail-header-lines h1)
4498      (mail-header-lines h2)))
4499
4500 (defun gnus-thread-sort-by-lines (h1 h2)
4501   "Sort threads by root article Lines header."
4502   (gnus-article-sort-by-lines
4503    (gnus-thread-header h1) (gnus-thread-header h2)))
4504
4505 (defsubst gnus-article-sort-by-chars (h1 h2)
4506   "Sort articles by octet length."
4507   (< (mail-header-chars h1)
4508      (mail-header-chars h2)))
4509
4510 (defun gnus-thread-sort-by-chars (h1 h2)
4511   "Sort threads by root article octet length."
4512   (gnus-article-sort-by-chars
4513    (gnus-thread-header h1) (gnus-thread-header h2)))
4514
4515 (defsubst gnus-article-sort-by-author (h1 h2)
4516   "Sort articles by root author."
4517   (string-lessp
4518    (let ((extract (funcall
4519                    gnus-extract-address-components
4520                    (mail-header-from h1))))
4521      (or (car extract) (cadr extract) ""))
4522    (let ((extract (funcall
4523                    gnus-extract-address-components
4524                    (mail-header-from h2))))
4525      (or (car extract) (cadr extract) ""))))
4526
4527 (defun gnus-thread-sort-by-author (h1 h2)
4528   "Sort threads by root author."
4529   (gnus-article-sort-by-author
4530    (gnus-thread-header h1)  (gnus-thread-header h2)))
4531
4532 (defsubst gnus-article-sort-by-subject (h1 h2)
4533   "Sort articles by root subject."
4534   (string-lessp
4535    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4536    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4537
4538 (defun gnus-thread-sort-by-subject (h1 h2)
4539   "Sort threads by root subject."
4540   (gnus-article-sort-by-subject
4541    (gnus-thread-header h1) (gnus-thread-header h2)))
4542
4543 (defsubst gnus-article-sort-by-date (h1 h2)
4544   "Sort articles by root article date."
4545   (time-less-p
4546    (gnus-date-get-time (mail-header-date h1))
4547    (gnus-date-get-time (mail-header-date h2))))
4548
4549 (defun gnus-thread-sort-by-date (h1 h2)
4550   "Sort threads by root article date."
4551   (gnus-article-sort-by-date
4552    (gnus-thread-header h1) (gnus-thread-header h2)))
4553
4554 (defsubst gnus-article-sort-by-score (h1 h2)
4555   "Sort articles by root article score.
4556 Unscored articles will be counted as having a score of zero."
4557   (> (or (cdr (assq (mail-header-number h1)
4558                     gnus-newsgroup-scored))
4559          gnus-summary-default-score 0)
4560      (or (cdr (assq (mail-header-number h2)
4561                     gnus-newsgroup-scored))
4562          gnus-summary-default-score 0)))
4563
4564 (defun gnus-thread-sort-by-score (h1 h2)
4565   "Sort threads by root article score."
4566   (gnus-article-sort-by-score
4567    (gnus-thread-header h1) (gnus-thread-header h2)))
4568
4569 (defun gnus-thread-sort-by-total-score (h1 h2)
4570   "Sort threads by the sum of all scores in the thread.
4571 Unscored articles will be counted as having a score of zero."
4572   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4573
4574 (defun gnus-thread-total-score (thread)
4575   ;; This function find the total score of THREAD.
4576   (cond
4577    ((null thread)
4578     0)
4579    ((consp thread)
4580     (if (stringp (car thread))
4581         (apply gnus-thread-score-function 0
4582                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4583       (gnus-thread-total-score-1 thread)))
4584    (t
4585     (gnus-thread-total-score-1 (list thread)))))
4586
4587 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4588   "Sort threads such that the thread with the most recently arrived article comes first."
4589   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4590
4591 (defun gnus-thread-highest-number (thread)
4592   "Return the highest article number in THREAD."
4593   (apply 'max (mapcar (lambda (header)
4594                         (mail-header-number header))
4595                       (message-flatten-list thread))))
4596
4597 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4598   "Sort threads such that the thread with the most recently dated article comes first."
4599   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4600
4601 (defun gnus-thread-latest-date (thread)
4602   "Return the highest article date in THREAD."
4603   (let ((previous-time 0))
4604     (apply 'max
4605            (mapcar
4606             (lambda (header)
4607               (setq previous-time
4608                     (condition-case ()
4609                         (time-to-seconds (mail-header-parse-date
4610                                           (mail-header-date header)))
4611                       (error previous-time))))
4612             (sort
4613              (message-flatten-list thread)
4614              (lambda (h1 h2)
4615                (< (mail-header-number h1)
4616                   (mail-header-number h2))))))))
4617
4618 (defun gnus-thread-total-score-1 (root)
4619   ;; This function find the total score of the thread below ROOT.
4620   (setq root (car root))
4621   (apply gnus-thread-score-function
4622          (or (append
4623               (mapcar 'gnus-thread-total-score
4624                       (cdr (gnus-id-to-thread (mail-header-id root))))
4625               (when (> (mail-header-number root) 0)
4626                 (list (or (cdr (assq (mail-header-number root)
4627                                      gnus-newsgroup-scored))
4628                           gnus-summary-default-score 0))))
4629              (list gnus-summary-default-score)
4630              '(0))))
4631
4632 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4633 (defvar gnus-tmp-prev-subject nil)
4634 (defvar gnus-tmp-false-parent nil)
4635 (defvar gnus-tmp-root-expunged nil)
4636 (defvar gnus-tmp-dummy-line nil)
4637
4638 (eval-when-compile (defvar gnus-tmp-header))
4639 (defun gnus-extra-header (type &optional header)
4640   "Return the extra header of TYPE."
4641   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4642       ""))
4643
4644 (defvar gnus-tmp-thread-tree-header-string "")
4645
4646 (defcustom gnus-sum-thread-tree-root "> "
4647   "With %B spec, used for the root of a thread.
4648 If nil, use subject instead."
4649   :type '(radio (const :format "%v  " nil) (string :size 0))
4650   :group 'gnus-thread)
4651 (defcustom gnus-sum-thread-tree-false-root "> "
4652   "With %B spec, used for a false root of a thread.
4653 If nil, use subject instead."
4654   :type '(radio (const :format "%v  " nil) (string :size 0))
4655   :group 'gnus-thread)
4656 (defcustom gnus-sum-thread-tree-single-indent ""
4657   "With %B spec, used for a thread with just one message.
4658 If nil, use subject instead."
4659   :type '(radio (const :format "%v  " nil) (string :size 0))
4660   :group 'gnus-thread)
4661 (defcustom gnus-sum-thread-tree-vertical "| "
4662   "With %B spec, used for drawing a vertical line."
4663   :type 'string
4664   :group 'gnus-thread)
4665 (defcustom gnus-sum-thread-tree-indent "  "
4666   "With %B spec, used for indenting."
4667   :type 'string
4668   :group 'gnus-thread)
4669 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4670   "With %B spec, used for a leaf with brothers."
4671   :type 'string
4672   :group 'gnus-thread)
4673 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4674   "With %B spec, used for a leaf without brothers."
4675   :type 'string
4676   :group 'gnus-thread)
4677
4678 (defun gnus-summary-prepare-threads (threads)
4679   "Prepare summary buffer from THREADS and indentation LEVEL.
4680 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4681 or a straight list of headers."
4682   (gnus-message 7 "Generating summary...")
4683
4684   (setq gnus-newsgroup-threads threads)
4685   (beginning-of-line)
4686
4687   (let ((gnus-tmp-level 0)
4688         (default-score (or gnus-summary-default-score 0))
4689         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4690         (building-line-count gnus-summary-display-while-building)
4691         (building-count (integerp gnus-summary-display-while-building))
4692         thread number subject stack state gnus-tmp-gathered beg-match
4693         new-roots gnus-tmp-new-adopts thread-end simp-subject
4694         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4695         gnus-tmp-replied gnus-tmp-subject-or-nil
4696         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4697         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4698         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4699         tree-stack)
4700
4701     (setq gnus-tmp-prev-subject nil
4702           gnus-tmp-thread-tree-header-string "")
4703
4704     (if (vectorp (car threads))
4705         ;; If this is a straight (sic) list of headers, then a
4706         ;; threaded summary display isn't required, so we just create
4707         ;; an unthreaded one.
4708         (gnus-summary-prepare-unthreaded threads)
4709
4710       ;; Do the threaded display.
4711
4712       (if gnus-summary-display-while-building
4713           (switch-to-buffer (buffer-name)))
4714       (while (or threads stack gnus-tmp-new-adopts new-roots)
4715
4716         (if (and (= gnus-tmp-level 0)
4717                  (or (not stack)
4718                      (= (caar stack) 0))
4719                  (not gnus-tmp-false-parent)
4720                  (or gnus-tmp-new-adopts new-roots))
4721             (if gnus-tmp-new-adopts
4722                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4723                       thread (list (car gnus-tmp-new-adopts))
4724                       gnus-tmp-header (caar thread)
4725                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4726               (when new-roots
4727                 (setq thread (list (car new-roots))
4728                       gnus-tmp-header (caar thread)
4729                       new-roots (cdr new-roots))))
4730
4731           (if threads
4732               ;; If there are some threads, we do them before the
4733               ;; threads on the stack.
4734               (setq thread threads
4735                     gnus-tmp-header (caar thread))
4736             ;; There were no current threads, so we pop something off
4737             ;; the stack.
4738             (setq state (car stack)
4739                   gnus-tmp-level (car state)
4740                   tree-stack (cadr state)
4741                   thread (caddr state)
4742                   stack (cdr stack)
4743                   gnus-tmp-header (caar thread))))
4744
4745         (setq gnus-tmp-false-parent nil)
4746         (setq gnus-tmp-root-expunged nil)
4747         (setq thread-end nil)
4748
4749         (if (stringp gnus-tmp-header)
4750             ;; The header is a dummy root.
4751             (cond
4752              ((eq gnus-summary-make-false-root 'adopt)
4753               ;; We let the first article adopt the rest.
4754               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4755                                                (cddar thread)))
4756               (setq gnus-tmp-gathered
4757                     (nconc (mapcar
4758                             (lambda (h) (mail-header-number (car h)))
4759                             (cddar thread))
4760                            gnus-tmp-gathered))
4761               (setq thread (cons (list (caar thread)
4762                                        (cadar thread))
4763                                  (cdr thread)))
4764               (setq gnus-tmp-level -1
4765                     gnus-tmp-false-parent t))
4766              ((eq gnus-summary-make-false-root 'empty)
4767               ;; We print adopted articles with empty subject fields.
4768               (setq gnus-tmp-gathered
4769                     (nconc (mapcar
4770                             (lambda (h) (mail-header-number (car h)))
4771                             (cddar thread))
4772                            gnus-tmp-gathered))
4773               (setq gnus-tmp-level -1))
4774              ((eq gnus-summary-make-false-root 'dummy)
4775               ;; We remember that we probably want to output a dummy
4776               ;; root.
4777               (setq gnus-tmp-dummy-line gnus-tmp-header)
4778               (setq gnus-tmp-prev-subject gnus-tmp-header))
4779              (t
4780               ;; We do not make a root for the gathered
4781               ;; sub-threads at all.
4782               (setq gnus-tmp-level -1)))
4783
4784           (setq number (mail-header-number gnus-tmp-header)
4785                 subject (mail-header-subject gnus-tmp-header)
4786                 simp-subject (gnus-simplify-subject-fully subject))
4787
4788           (cond
4789            ;; If the thread has changed subject, we might want to make
4790            ;; this subthread into a root.
4791            ((and (null gnus-thread-ignore-subject)
4792                  (not (zerop gnus-tmp-level))
4793                  gnus-tmp-prev-subject
4794                  (not (string= gnus-tmp-prev-subject simp-subject)))
4795             (setq new-roots (nconc new-roots (list (car thread)))
4796                   thread-end t
4797                   gnus-tmp-header nil))
4798            ;; If the article lies outside the current limit,
4799            ;; then we do not display it.
4800            ((not (memq number gnus-newsgroup-limit))
4801             (setq gnus-tmp-gathered
4802                   (nconc (mapcar
4803                           (lambda (h) (mail-header-number (car h)))
4804                           (cdar thread))
4805                          gnus-tmp-gathered))
4806             (setq gnus-tmp-new-adopts (if (cdar thread)
4807                                           (append gnus-tmp-new-adopts
4808                                                   (cdar thread))
4809                                         gnus-tmp-new-adopts)
4810                   thread-end t
4811                   gnus-tmp-header nil)
4812             (when (zerop gnus-tmp-level)
4813               (setq gnus-tmp-root-expunged t)))
4814            ;; Perhaps this article is to be marked as read?
4815            ((and gnus-summary-mark-below
4816                  (< (or (cdr (assq number gnus-newsgroup-scored))
4817                         default-score)
4818                     gnus-summary-mark-below)
4819                  ;; Don't touch sparse articles.
4820                  (not (gnus-summary-article-sparse-p number))
4821                  (not (gnus-summary-article-ancient-p number)))
4822             (setq gnus-newsgroup-unreads
4823                   (delq number gnus-newsgroup-unreads))
4824             (if gnus-newsgroup-auto-expire
4825                 (setq gnus-newsgroup-expirable
4826                       (gnus-add-to-sorted-list
4827                        gnus-newsgroup-expirable number))
4828               (push (cons number gnus-low-score-mark)
4829                     gnus-newsgroup-reads))))
4830
4831           (when gnus-tmp-header
4832             ;; We may have an old dummy line to output before this
4833             ;; article.
4834             (when (and gnus-tmp-dummy-line
4835                        (gnus-subject-equal
4836                         gnus-tmp-dummy-line
4837                         (mail-header-subject gnus-tmp-header)))
4838               (gnus-summary-insert-dummy-line
4839                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4840               (setq gnus-tmp-dummy-line nil))
4841
4842             ;; Compute the mark.
4843             (setq gnus-tmp-unread (gnus-article-mark number))
4844
4845             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4846                                   gnus-tmp-header gnus-tmp-level)
4847                   gnus-newsgroup-data)
4848
4849             ;; Actually insert the line.
4850             (setq
4851              gnus-tmp-subject-or-nil
4852              (cond
4853               ((and gnus-thread-ignore-subject
4854                     gnus-tmp-prev-subject
4855                     (not (string= gnus-tmp-prev-subject simp-subject)))
4856                subject)
4857               ((zerop gnus-tmp-level)
4858                (if (and (eq gnus-summary-make-false-root 'empty)
4859                         (memq number gnus-tmp-gathered)
4860                         gnus-tmp-prev-subject
4861                         (string= gnus-tmp-prev-subject simp-subject))
4862                    gnus-summary-same-subject
4863                  subject))
4864               (t gnus-summary-same-subject)))
4865             (if (and (eq gnus-summary-make-false-root 'adopt)
4866                      (= gnus-tmp-level 1)
4867                      (memq number gnus-tmp-gathered))
4868                 (setq gnus-tmp-opening-bracket ?\<
4869                       gnus-tmp-closing-bracket ?\>)
4870               (setq gnus-tmp-opening-bracket ?\[
4871                     gnus-tmp-closing-bracket ?\]))
4872             (setq
4873              gnus-tmp-indentation
4874              (aref gnus-thread-indent-array gnus-tmp-level)
4875              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4876              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4877                                 gnus-summary-default-score 0)
4878              gnus-tmp-score-char
4879              (if (or (null gnus-summary-default-score)
4880                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4881                          gnus-summary-zcore-fuzz))
4882                  ?                      ;Whitespace
4883                (if (< gnus-tmp-score gnus-summary-default-score)
4884                    gnus-score-below-mark gnus-score-over-mark))
4885              gnus-tmp-replied
4886              (cond ((memq number gnus-newsgroup-processable)
4887                     gnus-process-mark)
4888                    ((memq number gnus-newsgroup-cached)
4889                     gnus-cached-mark)
4890                    ((memq number gnus-newsgroup-replied)
4891                     gnus-replied-mark)
4892                    ((memq number gnus-newsgroup-forwarded)
4893                     gnus-forwarded-mark)
4894                    ((memq number gnus-newsgroup-saved)
4895                     gnus-saved-mark)
4896                    ((memq number gnus-newsgroup-recent)
4897                     gnus-recent-mark)
4898                    ((memq number gnus-newsgroup-unseen)
4899                     gnus-unseen-mark)
4900                    (t gnus-no-mark))
4901              gnus-tmp-downloaded
4902              (cond ((memq number gnus-newsgroup-undownloaded)
4903                     gnus-undownloaded-mark)
4904                    (gnus-newsgroup-agentized
4905                     gnus-downloaded-mark)
4906                    (t
4907                     gnus-no-mark))
4908              gnus-tmp-from (mail-header-from gnus-tmp-header)
4909              gnus-tmp-name
4910              (cond
4911               ((string-match "<[^>]+> *$" gnus-tmp-from)
4912                (setq beg-match (match-beginning 0))
4913                (or (and (string-match "^\".+\"" gnus-tmp-from)
4914                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4915                    (substring gnus-tmp-from 0 beg-match)))
4916               ((string-match "(.+)" gnus-tmp-from)
4917                (substring gnus-tmp-from
4918                           (1+ (match-beginning 0)) (1- (match-end 0))))
4919               (t gnus-tmp-from))
4920
4921              ;; Do the %B string
4922              gnus-tmp-thread-tree-header-string
4923              (cond
4924               ((not gnus-show-threads) "")
4925               ((zerop gnus-tmp-level)
4926                (cond ((cdar thread)
4927                       (or gnus-sum-thread-tree-root subject))
4928                      (gnus-tmp-new-adopts
4929                       (or gnus-sum-thread-tree-false-root subject))
4930                      (t
4931                       (or gnus-sum-thread-tree-single-indent subject))))
4932               (t
4933                (concat (apply 'concat
4934                               (mapcar (lambda (item)
4935                                         (if (= item 1)
4936                                             gnus-sum-thread-tree-vertical
4937                                           gnus-sum-thread-tree-indent))
4938                                       (cdr (reverse tree-stack))))
4939                        (if (nth 1 thread)
4940                            gnus-sum-thread-tree-leaf-with-other
4941                          gnus-sum-thread-tree-single-leaf)))))
4942             (when (string= gnus-tmp-name "")
4943               (setq gnus-tmp-name gnus-tmp-from))
4944             (unless (numberp gnus-tmp-lines)
4945               (setq gnus-tmp-lines -1))
4946             (if (= gnus-tmp-lines -1)
4947                 (setq gnus-tmp-lines "?")
4948               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4949               (gnus-put-text-property
4950              (point)
4951              (progn (eval gnus-summary-line-format-spec) (point))
4952                'gnus-number number)
4953             (when gnus-visual-p
4954               (forward-line -1)
4955               (gnus-run-hooks 'gnus-summary-update-hook)
4956               (forward-line 1))
4957
4958             (setq gnus-tmp-prev-subject simp-subject)))
4959
4960         (when (nth 1 thread)
4961           (push (list (max 0 gnus-tmp-level)
4962                       (copy-sequence tree-stack)
4963                       (nthcdr 1 thread))
4964                 stack))
4965         (push (if (nth 1 thread) 1 0) tree-stack)
4966         (incf gnus-tmp-level)
4967         (setq threads (if thread-end nil (cdar thread)))
4968         (if gnus-summary-display-while-building
4969             (if building-count
4970                 (progn
4971                   ;; use a set frequency
4972                   (setq building-line-count (1- building-line-count))
4973                   (when (= building-line-count 0)
4974                     (sit-for 0)
4975                     (setq building-line-count
4976                           gnus-summary-display-while-building)))
4977               ;; always
4978               (sit-for 0)))
4979         (unless threads
4980           (setq gnus-tmp-level 0)))))
4981   (gnus-message 7 "Generating summary...done"))
4982
4983 (defun gnus-summary-prepare-unthreaded (headers)
4984   "Generate an unthreaded summary buffer based on HEADERS."
4985   (let (header number mark)
4986
4987     (beginning-of-line)
4988
4989     (while headers
4990       ;; We may have to root out some bad articles...
4991       (when (memq (setq number (mail-header-number
4992                                 (setq header (pop headers))))
4993                   gnus-newsgroup-limit)
4994         ;; Mark article as read when it has a low score.
4995         (when (and gnus-summary-mark-below
4996                    (< (or (cdr (assq number gnus-newsgroup-scored))
4997                           gnus-summary-default-score 0)
4998                       gnus-summary-mark-below)
4999                    (not (gnus-summary-article-ancient-p number)))
5000           (setq gnus-newsgroup-unreads
5001                 (delq number gnus-newsgroup-unreads))
5002           (if gnus-newsgroup-auto-expire
5003               (push number gnus-newsgroup-expirable)
5004             (push (cons number gnus-low-score-mark)
5005                   gnus-newsgroup-reads)))
5006
5007         (setq mark (gnus-article-mark number))
5008         (push (gnus-data-make number mark (1+ (point)) header 0)
5009               gnus-newsgroup-data)
5010         (gnus-summary-insert-line
5011          header 0 number
5012          (memq number gnus-newsgroup-undownloaded)
5013          mark (memq number gnus-newsgroup-replied)
5014          (memq number gnus-newsgroup-expirable)
5015          (mail-header-subject header) nil
5016          (cdr (assq number gnus-newsgroup-scored))
5017          (memq number gnus-newsgroup-processable))))))
5018
5019 (defun gnus-summary-remove-list-identifiers ()
5020   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5021   (let ((regexp (if (consp gnus-list-identifiers)
5022                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5023                   gnus-list-identifiers))
5024         changed subject)
5025     (when regexp
5026       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5027       (dolist (header gnus-newsgroup-headers)
5028         (setq subject (mail-header-subject header)
5029               changed nil)
5030         (while (string-match regexp subject)
5031           (setq subject
5032                 (concat (substring subject 0 (match-beginning 1))
5033                         (substring subject (match-end 0)))
5034                 changed t))
5035         (when changed
5036           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5037             (setq subject
5038                   (concat (substring subject 0 (match-beginning 1))
5039                           (substring subject (match-end 1)))))
5040           (mail-header-set-subject header subject))))))
5041
5042 (defun gnus-fetch-headers (articles)
5043   "Fetch headers of ARTICLES."
5044   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5045     (gnus-message 5 "Fetching headers for %s..." name)
5046     (prog1
5047         (if (eq 'nov
5048                 (setq gnus-headers-retrieved-by
5049                       (gnus-retrieve-headers
5050                        articles gnus-newsgroup-name
5051                        ;; We might want to fetch old headers, but
5052                        ;; not if there is only 1 article.
5053                        (and (or (and
5054                                  (not (eq gnus-fetch-old-headers 'some))
5055                                  (not (numberp gnus-fetch-old-headers)))
5056                                 (> (length articles) 1))
5057                             gnus-fetch-old-headers))))
5058             (gnus-get-newsgroup-headers-xover
5059              articles nil nil gnus-newsgroup-name t)
5060           (gnus-get-newsgroup-headers))
5061       (gnus-message 5 "Fetching headers for %s...done" name))))
5062
5063 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5064   "Select newsgroup GROUP.
5065 If READ-ALL is non-nil, all articles in the group are selected.
5066 If SELECT-ARTICLES, only select those articles from GROUP."
5067   (let* ((entry (gnus-group-entry group))
5068          ;;!!! Dirty hack; should be removed.
5069          (gnus-summary-ignore-duplicates
5070           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5071               t
5072             gnus-summary-ignore-duplicates))
5073          (info (nth 2 entry))
5074          articles fetched-articles cached)
5075
5076     (unless (gnus-check-server
5077              (set (make-local-variable 'gnus-current-select-method)
5078                   (gnus-find-method-for-group group)))
5079       (error "Couldn't open server"))
5080
5081     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5082         (gnus-activate-group group)     ; Or we can activate it...
5083         (progn                          ; Or we bug out.
5084           (when (equal major-mode 'gnus-summary-mode)
5085             (gnus-kill-buffer (current-buffer)))
5086           (error "Couldn't activate group %s: %s"
5087                  group (gnus-status-message group))))
5088
5089     (unless (gnus-request-group group t)
5090       (when (equal major-mode 'gnus-summary-mode)
5091         (gnus-kill-buffer (current-buffer)))
5092       (error "Couldn't request group %s: %s"
5093              group (gnus-status-message group)))
5094
5095     (when gnus-agent
5096       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5097       
5098       (setq gnus-summary-use-undownloaded-faces
5099             (gnus-agent-find-parameter
5100              group
5101              'agent-enable-undownloaded-faces)))
5102
5103     (setq gnus-newsgroup-name group
5104           gnus-newsgroup-unselected nil
5105           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5106
5107     (let ((display (gnus-group-find-parameter group 'display)))
5108       (setq gnus-newsgroup-display
5109             (cond
5110              ((not (zerop (or (car-safe read-all) 0)))
5111               ;; The user entered the group with C-u SPC/RET, let's show
5112               ;; all articles.
5113               'gnus-not-ignore)
5114              ((eq display 'all)
5115               'gnus-not-ignore)
5116              ((arrayp display)
5117               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5118              ((numberp display)
5119               ;; The following is probably the "correct" solution, but
5120               ;; it makes Gnus fetch all headers and then limit the
5121               ;; articles (which is slow), so instead we hack the
5122               ;; select-articles parameter instead. -- Simon Josefsson
5123               ;; <jas@kth.se>
5124               ;;
5125               ;; (gnus-byte-compile
5126               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5127               ;;                         display)))))
5128               (setq select-articles
5129                     (gnus-uncompress-range
5130                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5131                              (if (> tmp 0)
5132                                  tmp
5133                                1))
5134                            (cdr (gnus-active group)))))
5135               nil)
5136              (t
5137               nil))))
5138
5139     (gnus-summary-setup-default-charset)
5140
5141     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5142     (when (gnus-virtual-group-p group)
5143       (setq cached gnus-newsgroup-cached))
5144
5145     (setq gnus-newsgroup-unreads
5146           (gnus-sorted-ndifference
5147            (gnus-sorted-ndifference gnus-newsgroup-unreads
5148                                     gnus-newsgroup-marked)
5149            gnus-newsgroup-dormant))
5150
5151     (setq gnus-newsgroup-processable nil)
5152
5153     (gnus-update-read-articles group gnus-newsgroup-unreads)
5154
5155     ;; Adjust and set lists of article marks.
5156     (when info
5157       (gnus-adjust-marked-articles info))
5158     (if (setq articles select-articles)
5159         (setq gnus-newsgroup-unselected
5160               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5161       (setq articles (gnus-articles-to-read group read-all)))
5162
5163     (cond
5164      ((null articles)
5165       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5166       'quit)
5167      ((eq articles 0) nil)
5168      (t
5169       ;; Init the dependencies hash table.
5170       (setq gnus-newsgroup-dependencies
5171             (gnus-make-hashtable (length articles)))
5172       (gnus-set-global-variables)
5173       ;; Retrieve the headers and read them in.
5174
5175       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5176
5177       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5178       (when cached
5179         (setq gnus-newsgroup-cached cached))
5180
5181       ;; Suppress duplicates?
5182       (when gnus-suppress-duplicates
5183         (gnus-dup-suppress-articles))
5184
5185       ;; Set the initial limit.
5186       (setq gnus-newsgroup-limit (copy-sequence articles))
5187       ;; Remove canceled articles from the list of unread articles.
5188       (setq fetched-articles
5189             (mapcar (lambda (headers) (mail-header-number headers))
5190                     gnus-newsgroup-headers))
5191       (setq gnus-newsgroup-articles fetched-articles)
5192       (setq gnus-newsgroup-unreads
5193             (gnus-sorted-nintersection
5194              gnus-newsgroup-unreads fetched-articles))
5195       (gnus-compute-unseen-list)
5196
5197       ;; Removed marked articles that do not exist.
5198       (gnus-update-missing-marks
5199        (gnus-sorted-difference articles fetched-articles))
5200       ;; We might want to build some more threads first.
5201       (when (and gnus-fetch-old-headers
5202                  (eq gnus-headers-retrieved-by 'nov))
5203         (if (eq gnus-fetch-old-headers 'invisible)
5204             (gnus-build-all-threads)
5205           (gnus-build-old-threads)))
5206       ;; Let the Gnus agent mark articles as read.
5207       (when gnus-agent
5208         (gnus-agent-get-undownloaded-list))
5209       ;; Remove list identifiers from subject
5210       (when gnus-list-identifiers
5211         (gnus-summary-remove-list-identifiers))
5212       ;; Check whether auto-expire is to be done in this group.
5213       (setq gnus-newsgroup-auto-expire
5214             (gnus-group-auto-expirable-p group))
5215       ;; Set up the article buffer now, if necessary.
5216       (unless gnus-single-article-buffer
5217         (gnus-article-setup-buffer))
5218       ;; First and last article in this newsgroup.
5219       (when gnus-newsgroup-headers
5220         (setq gnus-newsgroup-begin
5221               (mail-header-number (car gnus-newsgroup-headers))
5222               gnus-newsgroup-end
5223               (mail-header-number
5224                (gnus-last-element gnus-newsgroup-headers))))
5225       ;; GROUP is successfully selected.
5226       (or gnus-newsgroup-headers t)))))
5227
5228 (defun gnus-compute-unseen-list ()
5229   ;; The `seen' marks are treated specially.
5230   (if (not gnus-newsgroup-seen)
5231       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5232     (setq gnus-newsgroup-unseen
5233           (gnus-inverse-list-range-intersection
5234            gnus-newsgroup-articles gnus-newsgroup-seen))))
5235
5236 (defun gnus-summary-display-make-predicate (display)
5237   (require 'gnus-agent)
5238   (when (= (length display) 1)
5239     (setq display (car display)))
5240   (unless gnus-summary-display-cache
5241     (dolist (elem (append '((unread . unread)
5242                             (read . read)
5243                             (unseen . unseen))
5244                           gnus-article-mark-lists))
5245       (push (cons (cdr elem)
5246                   (gnus-byte-compile
5247                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5248             gnus-summary-display-cache)))
5249   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5250         (gnus-category-predicate-cache gnus-summary-display-cache))
5251     (gnus-get-predicate display)))
5252
5253 ;; Uses the dynamically bound `number' variable.
5254 (eval-when-compile
5255   (defvar number))
5256 (defun gnus-article-marked-p (type &optional article)
5257   (let ((article (or article number)))
5258     (cond
5259      ((eq type 'tick)
5260       (memq article gnus-newsgroup-marked))
5261      ((eq type 'spam)
5262       (memq article gnus-newsgroup-spam-marked))
5263      ((eq type 'unsend)
5264       (memq article gnus-newsgroup-unsendable))
5265      ((eq type 'undownload)
5266       (memq article gnus-newsgroup-undownloaded))
5267      ((eq type 'download)
5268       (memq article gnus-newsgroup-downloadable))
5269      ((eq type 'unread)
5270       (memq article gnus-newsgroup-unreads))
5271      ((eq type 'read)
5272       (memq article gnus-newsgroup-reads))
5273      ((eq type 'dormant)
5274       (memq article gnus-newsgroup-dormant) )
5275      ((eq type 'expire)
5276       (memq article gnus-newsgroup-expirable))
5277      ((eq type 'reply)
5278       (memq article gnus-newsgroup-replied))
5279      ((eq type 'killed)
5280       (memq article gnus-newsgroup-killed))
5281      ((eq type 'bookmark)
5282       (assq article gnus-newsgroup-bookmarks))
5283      ((eq type 'score)
5284       (assq article gnus-newsgroup-scored))
5285      ((eq type 'save)
5286       (memq article gnus-newsgroup-saved))
5287      ((eq type 'cache)
5288       (memq article gnus-newsgroup-cached))
5289      ((eq type 'forward)
5290       (memq article gnus-newsgroup-forwarded))
5291      ((eq type 'seen)
5292       (not (memq article gnus-newsgroup-unseen)))
5293      ((eq type 'recent)
5294       (memq article gnus-newsgroup-recent))
5295      (t t))))
5296
5297 (defun gnus-articles-to-read (group &optional read-all)
5298   "Find out what articles the user wants to read."
5299   (let* ((articles
5300           ;; Select all articles if `read-all' is non-nil, or if there
5301           ;; are no unread articles.
5302           (if (or read-all
5303                   (and (zerop (length gnus-newsgroup-marked))
5304                        (zerop (length gnus-newsgroup-unreads)))
5305                   ;; Fetch all if the predicate is non-nil.
5306                   gnus-newsgroup-display)
5307               ;; We want to select the headers for all the articles in
5308               ;; the group, so we select either all the active
5309               ;; articles in the group, or (if that's nil), the
5310               ;; articles in the cache.
5311               (or
5312                (gnus-uncompress-range (gnus-active group))
5313                (gnus-cache-articles-in-group group))
5314             ;; Select only the "normal" subset of articles.
5315             (gnus-sorted-nunion
5316              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5317              gnus-newsgroup-unreads)))
5318          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5319          (scored (length scored-list))
5320          (number (length articles))
5321          (marked (+ (length gnus-newsgroup-marked)
5322                     (length gnus-newsgroup-dormant)))
5323          (select
5324           (cond
5325            ((numberp read-all)
5326             read-all)
5327            ((numberp gnus-newsgroup-display)
5328             gnus-newsgroup-display)
5329            (t
5330             (condition-case ()
5331                 (cond
5332                  ((and (or (<= scored marked) (= scored number))
5333                        (numberp gnus-large-newsgroup)
5334                        (> number gnus-large-newsgroup))
5335                   (let* ((cursor-in-echo-area nil)
5336                          (initial (gnus-parameter-large-newsgroup-initial
5337                                    gnus-newsgroup-name))
5338                          (input
5339                           (read-string
5340                            (format
5341                             "How many articles from %s (%s %d): "
5342                             (gnus-limit-string
5343                              (gnus-group-decoded-name gnus-newsgroup-name)
5344                              35)
5345                             (if initial "max" "default")
5346                             number)
5347                            (if initial
5348                                (cons (number-to-string initial)
5349                                      0)))))
5350                     (if (string-match "^[ \t]*$" input) number input)))
5351                  ((and (> scored marked) (< scored number)
5352                        (> (- scored number) 20))
5353                   (let ((input
5354                          (read-string
5355                           (format "%s %s (%d scored, %d total): "
5356                                   "How many articles from"
5357                                   (gnus-group-decoded-name group)
5358                                   scored number))))
5359                     (if (string-match "^[ \t]*$" input)
5360                         number input)))
5361                  (t number))
5362               (quit
5363                (message "Quit getting the articles to read")
5364                nil))))))
5365     (setq select (if (stringp select) (string-to-number select) select))
5366     (if (or (null select) (zerop select))
5367         select
5368       (if (and (not (zerop scored)) (<= (abs select) scored))
5369           (progn
5370             (setq articles (sort scored-list '<))
5371             (setq number (length articles)))
5372         (setq articles (copy-sequence articles)))
5373
5374       (when (< (abs select) number)
5375         (if (< select 0)
5376             ;; Select the N oldest articles.
5377             (setcdr (nthcdr (1- (abs select)) articles) nil)
5378           ;; Select the N most recent articles.
5379           (setq articles (nthcdr (- number select) articles))))
5380       (setq gnus-newsgroup-unselected
5381             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5382       (when gnus-alter-articles-to-read-function
5383         (setq articles
5384               (sort
5385                (funcall gnus-alter-articles-to-read-function
5386                         gnus-newsgroup-name articles)
5387                '<)))
5388       articles)))
5389
5390 (defun gnus-killed-articles (killed articles)
5391   (let (out)
5392     (while articles
5393       (when (inline (gnus-member-of-range (car articles) killed))
5394         (push (car articles) out))
5395       (setq articles (cdr articles)))
5396     out))
5397
5398 (defun gnus-uncompress-marks (marks)
5399   "Uncompress the mark ranges in MARKS."
5400   (let ((uncompressed '(score bookmark))
5401         out)
5402     (while marks
5403       (if (memq (caar marks) uncompressed)
5404           (push (car marks) out)
5405         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5406       (setq marks (cdr marks)))
5407     out))
5408
5409 (defun gnus-article-mark-to-type (mark)
5410   "Return the type of MARK."
5411   (or (cadr (assq mark gnus-article-special-mark-lists))
5412       'list))
5413
5414 (defun gnus-article-unpropagatable-p (mark)
5415   "Return whether MARK should be propagated to back end."
5416   (memq mark gnus-article-unpropagated-mark-lists))
5417
5418 (defun gnus-adjust-marked-articles (info)
5419   "Set all article lists and remove all marks that are no longer valid."
5420   (let* ((marked-lists (gnus-info-marks info))
5421          (active (gnus-active (gnus-info-group info)))
5422          (min (car active))
5423          (max (cdr active))
5424          (types gnus-article-mark-lists)
5425          marks var articles article mark mark-type
5426          bgn end)
5427
5428     (dolist (marks marked-lists)
5429       (setq mark (car marks)
5430             mark-type (gnus-article-mark-to-type mark)
5431             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5432
5433       ;; We set the variable according to the type of the marks list,
5434       ;; and then adjust the marks to a subset of the active articles.
5435       (cond
5436        ;; Adjust "simple" lists - compressed yet unsorted
5437        ((eq mark-type 'list)
5438         ;; Simultaneously uncompress and clip to active range
5439         ;; See gnus-uncompress-range for a description of possible marks
5440         (let (l lh)
5441           (if (not (cadr marks))
5442               (set var nil)
5443             (setq articles (if (numberp (cddr marks))
5444                                (list (cdr marks))
5445                              (cdr marks))
5446                   lh (cons nil nil)
5447                   l lh)
5448
5449             (while (setq article (pop articles))
5450               (cond ((consp article)
5451                      (setq bgn (max (car article) min)
5452                            end (min (cdr article) max))
5453                      (while (<= bgn end)
5454                        (setq l (setcdr l (cons bgn nil))
5455                              bgn (1+ bgn))))
5456                     ((and (<= min article)
5457                           (>= max article))
5458                      (setq l (setcdr l (cons article nil))))))
5459             (set var (cdr lh)))))
5460        ;; Adjust assocs.
5461        ((eq mark-type 'tuple)
5462         (set var (setq articles (cdr marks)))
5463         (when (not (listp (cdr (symbol-value var))))
5464           (set var (list (symbol-value var))))
5465         (when (not (listp (cdr articles)))
5466           (setq articles (list articles)))
5467         (while articles
5468           (when (or (not (consp (setq article (pop articles))))
5469                     (< (car article) min)
5470                     (> (car article) max))
5471             (set var (delq article (symbol-value var))))))
5472        ;; Adjust ranges (sloppily).
5473        ((eq mark-type 'range)
5474         (cond
5475          ((eq mark 'seen)
5476           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5477           ;; It should be (seen (NUM1 . NUM2)).
5478           (when (numberp (cddr marks))
5479             (setcdr marks (list (cdr marks))))
5480           (setq articles (cdr marks))
5481           (while (and articles
5482                       (or (and (consp (car articles))
5483                                (> min (cdar articles)))
5484                           (and (numberp (car articles))
5485                                (> min (car articles)))))
5486             (pop articles))
5487           (set var articles))))))))
5488
5489 (defun gnus-update-missing-marks (missing)
5490   "Go through the list of MISSING articles and remove them from the mark lists."
5491   (when missing
5492     (let (var m)
5493       ;; Go through all types.
5494       (dolist (elem gnus-article-mark-lists)
5495         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5496           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5497           (when (symbol-value var)
5498             ;; This list has articles.  So we delete all missing
5499             ;; articles from it.
5500             (setq m missing)
5501             (while m
5502               (set var (delq (pop m) (symbol-value var))))))))))
5503
5504 (defun gnus-update-marks ()
5505   "Enter the various lists of marked articles into the newsgroup info list."
5506   (let ((types gnus-article-mark-lists)
5507         (info (gnus-get-info gnus-newsgroup-name))
5508         type list newmarked symbol delta-marks)
5509     (when info
5510       ;; Add all marks lists to the list of marks lists.
5511       (while (setq type (pop types))
5512         (setq list (symbol-value
5513                     (setq symbol
5514                           (intern (format "gnus-newsgroup-%s" (car type))))))
5515
5516         (when list
5517           ;; Get rid of the entries of the articles that have the
5518           ;; default score.
5519           (when (and (eq (cdr type) 'score)
5520                      gnus-save-score
5521                      list)
5522             (let* ((arts list)
5523                    (prev (cons nil list))
5524                    (all prev))
5525               (while arts
5526                 (if (or (not (consp (car arts)))
5527                         (= (cdar arts) gnus-summary-default-score))
5528                     (setcdr prev (cdr arts))
5529                   (setq prev arts))
5530                 (setq arts (cdr arts)))
5531               (setq list (cdr all)))))
5532
5533         (when (eq (cdr type) 'seen)
5534           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5535
5536         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5537           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5538
5539         (when (and (gnus-check-backend-function
5540                     'request-set-mark gnus-newsgroup-name)
5541                    (not (gnus-article-unpropagatable-p (cdr type))))
5542           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5543                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5544                  (add (gnus-remove-from-range
5545                        (gnus-copy-sequence list) old)))
5546             (when add
5547               (push (list add 'add (list (cdr type))) delta-marks))
5548             (when del
5549               (push (list del 'del (list (cdr type))) delta-marks))))
5550
5551         (when list
5552           (push (cons (cdr type) list) newmarked)))
5553
5554       (when delta-marks
5555         (unless (gnus-check-group gnus-newsgroup-name)
5556           (error "Can't open server for %s" gnus-newsgroup-name))
5557         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5558
5559       ;; Enter these new marks into the info of the group.
5560       (if (nthcdr 3 info)
5561           (setcar (nthcdr 3 info) newmarked)
5562         ;; Add the marks lists to the end of the info.
5563         (when newmarked
5564           (setcdr (nthcdr 2 info) (list newmarked))))
5565
5566       ;; Cut off the end of the info if there's nothing else there.
5567       (let ((i 5))
5568         (while (and (> i 2)
5569                     (not (nth i info)))
5570           (when (nthcdr (decf i) info)
5571             (setcdr (nthcdr i info) nil)))))))
5572
5573 (defun gnus-set-mode-line (where)
5574   "Set the mode line of the article or summary buffers.
5575 If WHERE is `summary', the summary mode line format will be used."
5576   ;; Is this mode line one we keep updated?
5577   (when (and (memq where gnus-updated-mode-lines)
5578              (symbol-value
5579               (intern (format "gnus-%s-mode-line-format-spec" where))))
5580     (let (mode-string)
5581       (save-excursion
5582         ;; We evaluate this in the summary buffer since these
5583         ;; variables are buffer-local to that buffer.
5584         (set-buffer gnus-summary-buffer)
5585        ;; We bind all these variables that are used in the `eval' form
5586         ;; below.
5587         (let* ((mformat (symbol-value
5588                          (intern
5589                           (format "gnus-%s-mode-line-format-spec" where))))
5590                (gnus-tmp-group-name (gnus-group-decoded-name
5591                                      gnus-newsgroup-name))
5592                (gnus-tmp-article-number (or gnus-current-article 0))
5593                (gnus-tmp-unread gnus-newsgroup-unreads)
5594                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5595                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5596                (gnus-tmp-unread-and-unselected
5597                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5598                             (zerop gnus-tmp-unselected))
5599                        "")
5600                       ((zerop gnus-tmp-unselected)
5601                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5602                       (t (format "{%d(+%d) more}"
5603                                  gnus-tmp-unread-and-unticked
5604                                  gnus-tmp-unselected))))
5605                (gnus-tmp-subject
5606                 (if (and gnus-current-headers
5607                          (vectorp gnus-current-headers))
5608                     (gnus-mode-string-quote
5609                      (mail-header-subject gnus-current-headers))
5610                   ""))
5611                bufname-length max-len
5612                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5613           (setq mode-string (eval mformat))
5614           (setq bufname-length (if (string-match "%b" mode-string)
5615                                    (- (length
5616                                        (buffer-name
5617                                         (if (eq where 'summary)
5618                                             nil
5619                                           (get-buffer gnus-article-buffer))))
5620                                       2)
5621                                  0))
5622           (setq max-len (max 4 (if gnus-mode-non-string-length
5623                                    (- (window-width)
5624                                       gnus-mode-non-string-length
5625                                       bufname-length)
5626                                  (length mode-string))))
5627           ;; We might have to chop a bit of the string off...
5628           (when (> (length mode-string) max-len)
5629             (setq mode-string
5630                   (concat (truncate-string-to-width mode-string (- max-len 3))
5631                           "...")))
5632           ;; Pad the mode string a bit.
5633           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5634       ;; Update the mode line.
5635       (setq mode-line-buffer-identification
5636             (gnus-mode-line-buffer-identification (list mode-string)))
5637       (set-buffer-modified-p t))))
5638
5639 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5640   "Go through the HEADERS list and add all Xrefs to a hash table.
5641 The resulting hash table is returned, or nil if no Xrefs were found."
5642   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5643          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5644          (xref-hashtb (gnus-make-hashtable))
5645          start group entry number xrefs header)
5646     (while headers
5647       (setq header (pop headers))
5648       (when (and (setq xrefs (mail-header-xref header))
5649                  (not (memq (setq number (mail-header-number header))
5650                             unreads)))
5651         (setq start 0)
5652         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5653           (setq start (match-end 0))
5654           (setq group (if prefix
5655                           (concat prefix (substring xrefs (match-beginning 1)
5656                                                     (match-end 1)))
5657                         (substring xrefs (match-beginning 1) (match-end 1))))
5658           (setq number
5659                 (string-to-int (substring xrefs (match-beginning 2)
5660                                           (match-end 2))))
5661           (if (setq entry (gnus-gethash group xref-hashtb))
5662               (setcdr entry (cons number (cdr entry)))
5663             (gnus-sethash group (cons number nil) xref-hashtb)))))
5664     (and start xref-hashtb)))
5665
5666 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5667   "Look through all the headers and mark the Xrefs as read."
5668   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5669         name info xref-hashtb idlist method nth4)
5670     (save-excursion
5671       (set-buffer gnus-group-buffer)
5672       (when (setq xref-hashtb
5673                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5674         (mapatoms
5675          (lambda (group)
5676            (unless (string= from-newsgroup (setq name (symbol-name group)))
5677              (setq idlist (symbol-value group))
5678              ;; Dead groups are not updated.
5679              (and (prog1
5680                       (setq info (gnus-get-info name))
5681                     (when (stringp (setq nth4 (gnus-info-method info)))
5682                       (setq nth4 (gnus-server-to-method nth4))))
5683                   ;; Only do the xrefs if the group has the same
5684                   ;; select method as the group we have just read.
5685                   (or (gnus-methods-equal-p
5686                        nth4 (gnus-find-method-for-group from-newsgroup))
5687                       virtual
5688                       (equal nth4 (setq method (gnus-find-method-for-group
5689                                                 from-newsgroup)))
5690                       (and (equal (car nth4) (car method))
5691                            (equal (nth 1 nth4) (nth 1 method))))
5692                   gnus-use-cross-reference
5693                   (or (not (eq gnus-use-cross-reference t))
5694                       virtual
5695                       ;; Only do cross-references on subscribed
5696                       ;; groups, if that is what is wanted.
5697                       (<= (gnus-info-level info) gnus-level-subscribed))
5698                   (gnus-group-make-articles-read name idlist))))
5699          xref-hashtb)))))
5700
5701 (defun gnus-compute-read-articles (group articles)
5702   (let* ((entry (gnus-group-entry group))
5703          (info (nth 2 entry))
5704          (active (gnus-active group))
5705          ninfo)
5706     (when entry
5707       ;; First peel off all invalid article numbers.
5708       (when active
5709         (let ((ids articles)
5710               id first)
5711           (while (setq id (pop ids))
5712             (when (and first (> id (cdr active)))
5713               ;; We'll end up in this situation in one particular
5714               ;; obscure situation.  If you re-scan a group and get
5715               ;; a new article that is cross-posted to a different
5716               ;; group that has not been re-scanned, you might get
5717               ;; crossposted article that has a higher number than
5718               ;; Gnus believes possible.  So we re-activate this
5719               ;; group as well.  This might mean doing the
5720               ;; crossposting thingy will *increase* the number
5721               ;; of articles in some groups.  Tsk, tsk.
5722               (setq active (or (gnus-activate-group group) active)))
5723             (when (or (> id (cdr active))
5724                       (< id (car active)))
5725               (setq articles (delq id articles))))))
5726       ;; If the read list is nil, we init it.
5727       (if (and active
5728                (null (gnus-info-read info))
5729                (> (car active) 1))
5730           (setq ninfo (cons 1 (1- (car active))))
5731         (setq ninfo (gnus-info-read info)))
5732       ;; Then we add the read articles to the range.
5733       (gnus-add-to-range
5734        ninfo (setq articles (sort articles '<))))))
5735
5736 (defun gnus-group-make-articles-read (group articles)
5737   "Update the info of GROUP to say that ARTICLES are read."
5738   (let* ((num 0)
5739          (entry (gnus-group-entry group))
5740          (info (nth 2 entry))
5741          (active (gnus-active group))
5742          range)
5743     (when entry
5744       (setq range (gnus-compute-read-articles group articles))
5745       (with-current-buffer gnus-group-buffer
5746         (gnus-undo-register
5747           `(progn
5748              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5749              (gnus-info-set-read ',info ',(gnus-info-read info))
5750              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5751              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5752              (gnus-group-update-group ,group t))))
5753       ;; Add the read articles to the range.
5754       (gnus-info-set-read info range)
5755       (gnus-request-set-mark group (list (list range 'add '(read))))
5756       ;; Then we have to re-compute how many unread
5757       ;; articles there are in this group.
5758       (when active
5759         (cond
5760          ((not range)
5761           (setq num (- (1+ (cdr active)) (car active))))
5762          ((not (listp (cdr range)))
5763           (setq num (- (cdr active) (- (1+ (cdr range))
5764                                        (car range)))))
5765          (t
5766           (while range
5767             (if (numberp (car range))
5768                 (setq num (1+ num))
5769               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5770             (setq range (cdr range)))
5771           (setq num (- (cdr active) num))))
5772         ;; Update the number of unread articles.
5773         (setcar entry num)
5774         ;; Update the group buffer.
5775         (unless (gnus-ephemeral-group-p group)
5776           (gnus-group-update-group group t))))))
5777
5778 (defvar gnus-newsgroup-none-id 0)
5779
5780 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5781   (let ((cur nntp-server-buffer)
5782         (dependencies
5783          (or dependencies
5784              (with-current-buffer gnus-summary-buffer
5785                gnus-newsgroup-dependencies)))
5786         headers id end ref number
5787         (mail-parse-charset gnus-newsgroup-charset)
5788         (mail-parse-ignored-charsets
5789          (save-excursion (condition-case nil
5790                              (set-buffer gnus-summary-buffer)
5791                            (error))
5792                          gnus-newsgroup-ignored-charsets)))
5793     (save-excursion
5794       (set-buffer nntp-server-buffer)
5795       ;; Translate all TAB characters into SPACE characters.
5796       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5797       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5798       (ietf-drums-unfold-fws)
5799       (gnus-run-hooks 'gnus-parse-headers-hook)
5800       (let ((case-fold-search t)
5801             in-reply-to header p lines chars)
5802         (goto-char (point-min))
5803         ;; Search to the beginning of the next header.  Error messages
5804         ;; do not begin with 2 or 3.
5805         (while (re-search-forward "^[23][0-9]+ " nil t)
5806           (setq id nil
5807                 ref nil)
5808           ;; This implementation of this function, with nine
5809           ;; search-forwards instead of the one re-search-forward and
5810           ;; a case (which basically was the old function) is actually
5811           ;; about twice as fast, even though it looks messier.  You
5812           ;; can't have everything, I guess.  Speed and elegance
5813           ;; doesn't always go hand in hand.
5814           (setq
5815            header
5816            (vector
5817             ;; Number.
5818             (prog1
5819                 (setq number (read cur))
5820               (end-of-line)
5821               (setq p (point))
5822               (narrow-to-region (point)
5823                                 (or (and (search-forward "\n.\n" nil t)
5824                                          (- (point) 2))
5825                                     (point))))
5826             ;; Subject.
5827             (progn
5828               (goto-char p)
5829               (if (search-forward "\nsubject:" nil t)
5830                   (funcall gnus-decode-encoded-word-function
5831                            (nnheader-header-value))
5832                 "(none)"))
5833             ;; From.
5834             (progn
5835               (goto-char p)
5836               (if (search-forward "\nfrom:" nil t)
5837                   (funcall gnus-decode-encoded-word-function
5838                            (nnheader-header-value))
5839                 "(nobody)"))
5840             ;; Date.
5841             (progn
5842               (goto-char p)
5843               (if (search-forward "\ndate:" nil t)
5844                   (nnheader-header-value) ""))
5845             ;; Message-ID.
5846             (progn
5847               (goto-char p)
5848               (setq id (if (re-search-forward
5849                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5850                            ;; We do it this way to make sure the Message-ID
5851                            ;; is (somewhat) syntactically valid.
5852                            (buffer-substring (match-beginning 1)
5853                                              (match-end 1))
5854                          ;; If there was no message-id, we just fake one
5855                          ;; to make subsequent routines simpler.
5856                          (nnheader-generate-fake-message-id number))))
5857             ;; References.
5858             (progn
5859               (goto-char p)
5860               (if (search-forward "\nreferences:" nil t)
5861                   (progn
5862                     (setq end (point))
5863                     (prog1
5864                         (nnheader-header-value)
5865                       (setq ref
5866                             (buffer-substring
5867                              (progn
5868                                (end-of-line)
5869                                (search-backward ">" end t)
5870                                (1+ (point)))
5871                              (progn
5872                                (search-backward "<" end t)
5873                                (point))))))
5874                 ;; Get the references from the in-reply-to header if there
5875                 ;; were no references and the in-reply-to header looks
5876                 ;; promising.
5877                 (if (and (search-forward "\nin-reply-to:" nil t)
5878                          (setq in-reply-to (nnheader-header-value))
5879                          (string-match "<[^>]+>" in-reply-to))
5880                     (let (ref2)
5881                       (setq ref (substring in-reply-to (match-beginning 0)
5882                                            (match-end 0)))
5883                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5884                         (setq ref2 (substring in-reply-to (match-beginning 0)
5885                                               (match-end 0)))
5886                         (when (> (length ref2) (length ref))
5887                           (setq ref ref2)))
5888                       ref)
5889                   (setq ref nil))))
5890             ;; Chars.
5891             (progn
5892               (goto-char p)
5893               (if (search-forward "\nchars: " nil t)
5894                   (if (numberp (setq chars (ignore-errors (read cur))))
5895                       chars -1)
5896                 -1))
5897             ;; Lines.
5898             (progn
5899               (goto-char p)
5900               (if (search-forward "\nlines: " nil t)
5901                   (if (numberp (setq lines (ignore-errors (read cur))))
5902                       lines -1)
5903                 -1))
5904             ;; Xref.
5905             (progn
5906               (goto-char p)
5907               (and (search-forward "\nxref:" nil t)
5908                    (nnheader-header-value)))
5909             ;; Extra.
5910             (when gnus-extra-headers
5911               (let ((extra gnus-extra-headers)
5912                     out)
5913                 (while extra
5914                   (goto-char p)
5915                   (when (search-forward
5916                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5917                     (push (cons (car extra) (nnheader-header-value))
5918                           out))
5919                   (pop extra))
5920                 out))))
5921           (when (equal id ref)
5922             (setq ref nil))
5923
5924           (when gnus-alter-header-function
5925             (funcall gnus-alter-header-function header)
5926             (setq id (mail-header-id header)
5927                   ref (gnus-parent-id (mail-header-references header))))
5928
5929           (when (setq header
5930                       (gnus-dependencies-add-header
5931                        header dependencies force-new))
5932             (push header headers))
5933           (goto-char (point-max))
5934           (widen))
5935         (nreverse headers)))))
5936
5937 ;; Goes through the xover lines and returns a list of vectors
5938 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5939                                                   force-new dependencies
5940                                                   group also-fetch-heads)
5941   "Parse the news overview data in the server buffer.
5942 Return a list of headers that match SEQUENCE (see
5943 `nntp-retrieve-headers')."
5944   ;; Get the Xref when the users reads the articles since most/some
5945   ;; NNTP servers do not include Xrefs when using XOVER.
5946   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5947   (let ((mail-parse-charset gnus-newsgroup-charset)
5948         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5949         (cur nntp-server-buffer)
5950         (dependencies (or dependencies gnus-newsgroup-dependencies))
5951         (allp (cond
5952                ((eq gnus-read-all-available-headers t)
5953                 t)
5954                ((stringp gnus-read-all-available-headers)
5955                 (string-match gnus-read-all-available-headers group))
5956                (t
5957                 nil)))
5958         number headers header)
5959     (save-excursion
5960       (set-buffer nntp-server-buffer)
5961       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5962       ;; Allow the user to mangle the headers before parsing them.
5963       (gnus-run-hooks 'gnus-parse-headers-hook)
5964       (goto-char (point-min))
5965       (gnus-parse-without-error
5966         (while (and (or sequence allp)
5967                     (not (eobp)))
5968           (setq number (read cur))
5969           (when (not allp)
5970             (while (and sequence
5971                         (< (car sequence) number))
5972               (setq sequence (cdr sequence))))
5973           (when (and (or allp
5974                          (and sequence
5975                               (eq number (car sequence))))
5976                      (progn
5977                        (setq sequence (cdr sequence))
5978                        (setq header (inline
5979                                       (gnus-nov-parse-line
5980                                        number dependencies force-new)))))
5981             (push header headers))
5982           (forward-line 1)))
5983       ;; A common bug in inn is that if you have posted an article and
5984       ;; then retrieves the active file, it will answer correctly --
5985       ;; the new article is included.  However, a NOV entry for the
5986       ;; article may not have been generated yet, so this may fail.
5987       ;; We work around this problem by retrieving the last few
5988       ;; headers using HEAD.
5989       (if (or (not also-fetch-heads)
5990               (not sequence))
5991           ;; We (probably) got all the headers.
5992           (nreverse headers)
5993         (let ((gnus-nov-is-evil t))
5994           (nconc
5995            (nreverse headers)
5996            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5997              (gnus-get-newsgroup-headers))))))))
5998
5999 (defun gnus-article-get-xrefs ()
6000   "Fill in the Xref value in `gnus-current-headers', if necessary.
6001 This is meant to be called in `gnus-article-internal-prepare-hook'."
6002   (let ((headers (with-current-buffer gnus-summary-buffer
6003                    gnus-current-headers)))
6004     (or (not gnus-use-cross-reference)
6005         (not headers)
6006         (and (mail-header-xref headers)
6007              (not (string= (mail-header-xref headers) "")))
6008         (let ((case-fold-search t)
6009               xref)
6010           (save-restriction
6011             (nnheader-narrow-to-headers)
6012             (goto-char (point-min))
6013             (when (or (and (not (eobp))
6014                            (eq (downcase (char-after)) ?x)
6015                            (looking-at "Xref:"))
6016                       (search-forward "\nXref:" nil t))
6017               (goto-char (1+ (match-end 0)))
6018               (setq xref (buffer-substring (point) (point-at-eol)))
6019               (mail-header-set-xref headers xref)))))))
6020
6021 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6022   "Find article ID and insert the summary line for that article.
6023 OLD-HEADER can either be a header or a line number to insert
6024 the subject line on."
6025   (let* ((line (and (numberp old-header) old-header))
6026          (old-header (and (vectorp old-header) old-header))
6027          (header (cond ((and old-header use-old-header)
6028                         old-header)
6029                        ((and (numberp id)
6030                              (gnus-number-to-header id))
6031                         (gnus-number-to-header id))
6032                        (t
6033                         (gnus-read-header id))))
6034          (number (and (numberp id) id))
6035          d)
6036     (when header
6037       ;; Rebuild the thread that this article is part of and go to the
6038       ;; article we have fetched.
6039       (when (and (not gnus-show-threads)
6040                  old-header)
6041         (when (and number
6042                    (setq d (gnus-data-find (mail-header-number old-header))))
6043           (goto-char (gnus-data-pos d))
6044           (gnus-data-remove
6045            number
6046            (- (point-at-bol)
6047               (prog1
6048                   (1+ (point-at-eol))
6049                 (gnus-delete-line))))))
6050       ;; Remove list identifiers from subject.
6051       (when gnus-list-identifiers
6052         (let ((gnus-newsgroup-headers (list header)))
6053           (gnus-summary-remove-list-identifiers)
6054           (setq header (car gnus-newsgroup-headers))))
6055       (when old-header
6056         (mail-header-set-number header (mail-header-number old-header)))
6057       (setq gnus-newsgroup-sparse
6058             (delq (setq number (mail-header-number header))
6059                   gnus-newsgroup-sparse))
6060       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6061       (push number gnus-newsgroup-limit)
6062       (gnus-rebuild-thread (mail-header-id header) line)
6063       (gnus-summary-goto-subject number nil t))
6064     (when (and (numberp number)
6065                (> number 0))
6066       ;; We have to update the boundaries even if we can't fetch the
6067       ;; article if ID is a number -- so that the next `P' or `N'
6068       ;; command will fetch the previous (or next) article even
6069       ;; if the one we tried to fetch this time has been canceled.
6070       (when (> number gnus-newsgroup-end)
6071         (setq gnus-newsgroup-end number))
6072       (when (< number gnus-newsgroup-begin)
6073         (setq gnus-newsgroup-begin number))
6074       (setq gnus-newsgroup-unselected
6075             (delq number gnus-newsgroup-unselected)))
6076     ;; Report back a success?
6077     (and header (mail-header-number header))))
6078
6079 ;;; Process/prefix in the summary buffer
6080
6081 (defun gnus-summary-work-articles (n)
6082   "Return a list of articles to be worked upon.
6083 The prefix argument, the list of process marked articles, and the
6084 current article will be taken into consideration."
6085   (save-excursion
6086     (set-buffer gnus-summary-buffer)
6087     (cond
6088      (n
6089       ;; A numerical prefix has been given.
6090       (setq n (prefix-numeric-value n))
6091       (let ((backward (< n 0))
6092             (n (abs (prefix-numeric-value n)))
6093             articles article)
6094         (save-excursion
6095           (while
6096               (and (> n 0)
6097                    (push (setq article (gnus-summary-article-number))
6098                          articles)
6099                    (if backward
6100                        (gnus-summary-find-prev nil article)
6101                      (gnus-summary-find-next nil article)))
6102             (decf n)))
6103         (nreverse articles)))
6104      ((and (gnus-region-active-p) (mark))
6105       (message "region active")
6106       ;; Work on the region between point and mark.
6107       (let ((max (max (point) (mark)))
6108             articles article)
6109         (save-excursion
6110           (goto-char (min (min (point) (mark))))
6111           (while
6112               (and
6113                (push (setq article (gnus-summary-article-number)) articles)
6114                (gnus-summary-find-next nil article)
6115                (< (point) max)))
6116           (nreverse articles))))
6117      (gnus-newsgroup-processable
6118       ;; There are process-marked articles present.
6119       ;; Save current state.
6120       (gnus-summary-save-process-mark)
6121       ;; Return the list.
6122       (reverse gnus-newsgroup-processable))
6123      (t
6124       ;; Just return the current article.
6125       (list (gnus-summary-article-number))))))
6126
6127 (defmacro gnus-summary-iterate (arg &rest forms)
6128   "Iterate over the process/prefixed articles and do FORMS.
6129 ARG is the interactive prefix given to the command.  FORMS will be
6130 executed with point over the summary line of the articles."
6131   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6132     `(let ((,articles (gnus-summary-work-articles ,arg)))
6133        (while ,articles
6134          (gnus-summary-goto-subject (car ,articles))
6135          ,@forms
6136          (pop ,articles)))))
6137
6138 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6139 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6140
6141 (defun gnus-summary-save-process-mark ()
6142   "Push the current set of process marked articles on the stack."
6143   (interactive)
6144   (push (copy-sequence gnus-newsgroup-processable)
6145         gnus-newsgroup-process-stack))
6146
6147 (defun gnus-summary-kill-process-mark ()
6148   "Push the current set of process marked articles on the stack and unmark."
6149   (interactive)
6150   (gnus-summary-save-process-mark)
6151   (gnus-summary-unmark-all-processable))
6152
6153 (defun gnus-summary-yank-process-mark ()
6154   "Pop the last process mark state off the stack and restore it."
6155   (interactive)
6156   (unless gnus-newsgroup-process-stack
6157     (error "Empty mark stack"))
6158   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6159
6160 (defun gnus-summary-process-mark-set (set)
6161   "Make SET into the current process marked articles."
6162   (gnus-summary-unmark-all-processable)
6163   (while set
6164     (gnus-summary-set-process-mark (pop set))))
6165
6166 ;;; Searching and stuff
6167
6168 (defun gnus-summary-search-group (&optional backward use-level)
6169   "Search for next unread newsgroup.
6170 If optional argument BACKWARD is non-nil, search backward instead."
6171   (save-excursion
6172     (set-buffer gnus-group-buffer)
6173     (when (gnus-group-search-forward
6174            backward nil (if use-level (gnus-group-group-level) nil))
6175       (gnus-group-group-name))))
6176
6177 (defun gnus-summary-best-group (&optional exclude-group)
6178   "Find the name of the best unread group.
6179 If EXCLUDE-GROUP, do not go to this group."
6180   (with-current-buffer gnus-group-buffer
6181     (save-excursion
6182       (gnus-group-best-unread-group exclude-group))))
6183
6184 (defun gnus-summary-find-next (&optional unread article backward)
6185   (if backward
6186       (gnus-summary-find-prev unread article)
6187     (let* ((dummy (gnus-summary-article-intangible-p))
6188            (article (or article (gnus-summary-article-number)))
6189            (data (gnus-data-find-list article))
6190            result)
6191       (when (and (not dummy)
6192                  (or (not gnus-summary-check-current)
6193                      (not unread)
6194                      (not (gnus-data-unread-p (car data)))))
6195         (setq data (cdr data)))
6196       (when (setq result
6197                   (if unread
6198                       (progn
6199                         (while data
6200                           (unless (memq (gnus-data-number (car data))
6201                                         (cond
6202                                          ((eq gnus-auto-goto-ignores
6203                                               'always-undownloaded)
6204                                           gnus-newsgroup-undownloaded)
6205                                          (gnus-plugged
6206                                           nil)
6207                                          ((eq gnus-auto-goto-ignores
6208                                               'unfetched)
6209                                           gnus-newsgroup-unfetched)
6210                                          ((eq gnus-auto-goto-ignores
6211                                               'undownloaded)
6212                                           gnus-newsgroup-undownloaded)))
6213                             (when (gnus-data-unread-p (car data))
6214                               (setq result (car data)
6215                                     data nil)))
6216                           (setq data (cdr data)))
6217                         result)
6218                     (car data)))
6219         (goto-char (gnus-data-pos result))
6220         (gnus-data-number result)))))
6221
6222 (defun gnus-summary-find-prev (&optional unread article)
6223   (let* ((eobp (eobp))
6224          (article (or article (gnus-summary-article-number)))
6225          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6226          result)
6227     (when (and (not eobp)
6228                (or (not gnus-summary-check-current)
6229                    (not unread)
6230                    (not (gnus-data-unread-p (car data)))))
6231       (setq data (cdr data)))
6232     (when (setq result
6233                 (if unread
6234                     (progn
6235                       (while data
6236                         (unless (memq (gnus-data-number (car data))
6237                                       (cond
6238                                        ((eq gnus-auto-goto-ignores
6239                                             'always-undownloaded)
6240                                         gnus-newsgroup-undownloaded)
6241                                        (gnus-plugged
6242                                         nil)
6243                                        ((eq gnus-auto-goto-ignores
6244                                             'unfetched)
6245                                         gnus-newsgroup-unfetched)
6246                                        ((eq gnus-auto-goto-ignores
6247                                             'undownloaded)
6248                                         gnus-newsgroup-undownloaded)))
6249                           (when (gnus-data-unread-p (car data))
6250                             (setq result (car data)
6251                                   data nil)))
6252                         (setq data (cdr data)))
6253                       result)
6254                   (car data)))
6255       (goto-char (gnus-data-pos result))
6256       (gnus-data-number result))))
6257
6258 (defun gnus-summary-find-subject (subject &optional unread backward article)
6259   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6260          (article (or article (gnus-summary-article-number)))
6261          (articles (gnus-data-list backward))
6262          (arts (gnus-data-find-list article articles))
6263          result)
6264     (when (or (not gnus-summary-check-current)
6265               (not unread)
6266               (not (gnus-data-unread-p (car arts))))
6267       (setq arts (cdr arts)))
6268     (while arts
6269       (and (or (not unread)
6270                (gnus-data-unread-p (car arts)))
6271            (vectorp (gnus-data-header (car arts)))
6272            (gnus-subject-equal
6273             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6274            (setq result (car arts)
6275                  arts nil))
6276       (setq arts (cdr arts)))
6277     (and result
6278          (goto-char (gnus-data-pos result))
6279          (gnus-data-number result))))
6280
6281 (defun gnus-summary-search-forward (&optional unread subject backward)
6282   "Search forward for an article.
6283 If UNREAD, look for unread articles.  If SUBJECT, look for
6284 articles with that subject.  If BACKWARD, search backward instead."
6285   (cond (subject (gnus-summary-find-subject subject unread backward))
6286         (backward (gnus-summary-find-prev unread))
6287         (t (gnus-summary-find-next unread))))
6288
6289 (defun gnus-recenter (&optional n)
6290   "Center point in window and redisplay frame.
6291 Also do horizontal recentering."
6292   (interactive "P")
6293   (when (and gnus-auto-center-summary
6294              (not (eq gnus-auto-center-summary 'vertical)))
6295     (gnus-horizontal-recenter))
6296   (recenter n))
6297
6298 (defun gnus-summary-recenter ()
6299   "Center point in the summary window.
6300 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6301 displayed, no centering will be performed."
6302   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6303   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6304   (interactive)
6305   ;; The user has to want it.
6306   (when gnus-auto-center-summary
6307     (let* ((top (cond ((< (window-height) 4) 0)
6308                       ((< (window-height) 7) 1)
6309                       (t (if (numberp gnus-auto-center-summary)
6310                              gnus-auto-center-summary
6311                            (/ (1- (window-height)) 2)))))
6312            (height (1- (window-height)))
6313            (bottom (save-excursion (goto-char (point-max))
6314                                    (forward-line (- height))
6315                                    (point)))
6316            (window (get-buffer-window (current-buffer))))
6317       (when (get-buffer-window gnus-article-buffer)
6318         ;; Only do recentering when the article buffer is displayed,
6319         ;; Set the window start to either `bottom', which is the biggest
6320         ;; possible valid number, or the second line from the top,
6321         ;; whichever is the least.
6322         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6323           (if (> bottom top-pos)
6324               ;; Keep the second line from the top visible
6325               (set-window-start window top-pos t)
6326             ;; Try to keep the bottom line visible; if it's partially
6327             ;; obscured, either scroll one more line to make it fully
6328             ;; visible, or revert to using TOP-POS.
6329             (save-excursion
6330               (goto-char (point-max))
6331               (forward-line -1)
6332               (let ((last-line-start (point)))
6333                 (goto-char bottom)
6334                 (set-window-start window (point) t)
6335                 (when (not (pos-visible-in-window-p last-line-start window))
6336                   (forward-line 1)
6337                   (set-window-start window (min (point) top-pos) t)))))))
6338       ;; Do horizontal recentering while we're at it.
6339       (when (and (get-buffer-window (current-buffer) t)
6340                  (not (eq gnus-auto-center-summary 'vertical)))
6341         (let ((selected (selected-window)))
6342           (select-window (get-buffer-window (current-buffer) t))
6343           (gnus-summary-position-point)
6344           (gnus-horizontal-recenter)
6345           (select-window selected))))))
6346
6347 (defun gnus-summary-jump-to-group (newsgroup)
6348   "Move point to NEWSGROUP in group mode buffer."
6349   ;; Keep update point of group mode buffer if visible.
6350   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6351       (save-window-excursion
6352         ;; Take care of tree window mode.
6353         (when (get-buffer-window gnus-group-buffer)
6354           (pop-to-buffer gnus-group-buffer))
6355         (gnus-group-jump-to-group newsgroup))
6356     (save-excursion
6357       ;; Take care of tree window mode.
6358       (if (get-buffer-window gnus-group-buffer)
6359           (pop-to-buffer gnus-group-buffer)
6360         (set-buffer gnus-group-buffer))
6361       (gnus-group-jump-to-group newsgroup))))
6362
6363 ;; This function returns a list of article numbers based on the
6364 ;; difference between the ranges of read articles in this group and
6365 ;; the range of active articles.
6366 (defun gnus-list-of-unread-articles (group)
6367   (let* ((read (gnus-info-read (gnus-get-info group)))
6368          (active (or (gnus-active group) (gnus-activate-group group)))
6369          (last (cdr active))
6370          first nlast unread)
6371     ;; If none are read, then all are unread.
6372     (if (not read)
6373         (setq first (car active))
6374       ;; If the range of read articles is a single range, then the
6375       ;; first unread article is the article after the last read
6376       ;; article.  Sounds logical, doesn't it?
6377       (if (and (not (listp (cdr read)))
6378                (or (< (car read) (car active))
6379                    (progn (setq read (list read))
6380                           nil)))
6381           (setq first (max (car active) (1+ (cdr read))))
6382         ;; `read' is a list of ranges.
6383         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6384                                   (caar read)))
6385                   1)
6386           (setq first (car active)))
6387         (while read
6388           (when first
6389             (while (< first nlast)
6390               (setq unread (cons first unread)
6391                     first (1+ first))))
6392           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6393           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6394           (setq read (cdr read)))))
6395     ;; And add the last unread articles.
6396     (while (<= first last)
6397       (setq unread (cons first unread)
6398             first (1+ first)))
6399     ;; Return the list of unread articles.
6400     (delq 0 (nreverse unread))))
6401
6402 (defun gnus-list-of-read-articles (group)
6403   "Return a list of unread, unticked and non-dormant articles."
6404   (let* ((info (gnus-get-info group))
6405          (marked (gnus-info-marks info))
6406          (active (gnus-active group)))
6407     (and info active
6408          (gnus-list-range-difference
6409           (gnus-list-range-difference
6410            (gnus-sorted-complement
6411             (gnus-uncompress-range active)
6412             (gnus-list-of-unread-articles group))
6413            (cdr (assq 'dormant marked)))
6414           (cdr (assq 'tick marked))))))
6415
6416 ;; This function returns a sequence of article numbers based on the
6417 ;; difference between the ranges of read articles in this group and
6418 ;; the range of active articles.
6419 (defun gnus-sequence-of-unread-articles (group)
6420   (let* ((read (gnus-info-read (gnus-get-info group)))
6421          (active (or (gnus-active group) (gnus-activate-group group)))
6422          (last (cdr active))
6423          first nlast unread)
6424     ;; If none are read, then all are unread.
6425     (if (not read)
6426         (setq first (car active))
6427       ;; If the range of read articles is a single range, then the
6428       ;; first unread article is the article after the last read
6429       ;; article.  Sounds logical, doesn't it?
6430       (if (and (not (listp (cdr read)))
6431                (or (< (car read) (car active))
6432                    (progn (setq read (list read))
6433                           nil)))
6434           (setq first (max (car active) (1+ (cdr read))))
6435         ;; `read' is a list of ranges.
6436         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6437                                   (caar read)))
6438                   1)
6439           (setq first (car active)))
6440         (while read
6441           (when first
6442             (push (cons first nlast) unread))
6443           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6444           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6445           (setq read (cdr read)))))
6446     ;; And add the last unread articles.
6447     (cond ((< first last)
6448            (push (cons first last) unread))
6449           ((= first last)
6450            (push first unread)))
6451     ;; Return the sequence of unread articles.
6452     (delq 0 (nreverse unread))))
6453
6454 ;; Various summary commands
6455
6456 (defun gnus-summary-select-article-buffer ()
6457   "Reconfigure windows to show article buffer."
6458   (interactive)
6459   (if (not (gnus-buffer-live-p gnus-article-buffer))
6460       (error "There is no article buffer for this summary buffer")
6461     (gnus-configure-windows 'article)
6462     (select-window (get-buffer-window gnus-article-buffer))))
6463
6464 (defun gnus-summary-universal-argument (arg)
6465   "Perform any operation on all articles that are process/prefixed."
6466   (interactive "P")
6467   (let ((articles (gnus-summary-work-articles arg))
6468         func article)
6469     (if (eq
6470          (setq
6471           func
6472           (key-binding
6473            (read-key-sequence
6474             (substitute-command-keys
6475              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6476          'undefined)
6477         (gnus-error 1 "Undefined key")
6478       (save-excursion
6479         (while articles
6480           (gnus-summary-goto-subject (setq article (pop articles)))
6481           (let (gnus-newsgroup-processable)
6482             (command-execute func))
6483           (gnus-summary-remove-process-mark article)))))
6484   (gnus-summary-position-point))
6485
6486 (defun gnus-summary-toggle-truncation (&optional arg)
6487   "Toggle truncation of summary lines.
6488 With ARG, turn line truncation on if ARG is positive."
6489   (interactive "P")
6490   (setq truncate-lines
6491         (if (null arg) (not truncate-lines)
6492           (> (prefix-numeric-value arg) 0)))
6493   (redraw-display))
6494
6495 (defun gnus-summary-find-for-reselect ()
6496   "Return the number of an article to stay on across a reselect.
6497 The current article is considered, then following articles, then previous
6498 articles.  An article is sought which is not cancelled and isn't a temporary
6499 insertion from another group.  If there's no such then return a dummy 0."
6500   (let (found)
6501     (dolist (rev '(nil t))
6502       (unless found      ; don't demand the reverse list if we don't need it
6503         (let ((data (gnus-data-find-list
6504                      (gnus-summary-article-number) (gnus-data-list rev))))
6505           (while (and data (not found))
6506             (if (and (< 0 (gnus-data-number (car data)))
6507                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6508                 (setq found (gnus-data-number (car data))))
6509             (setq data (cdr data))))))
6510     (or found 0)))
6511
6512 (defun gnus-summary-reselect-current-group (&optional all rescan)
6513   "Exit and then reselect the current newsgroup.
6514 The prefix argument ALL means to select all articles."
6515   (interactive "P")
6516   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6517     (error "Ephemeral groups can't be reselected"))
6518   (let ((current-subject (gnus-summary-find-for-reselect))
6519         (group gnus-newsgroup-name))
6520     (setq gnus-newsgroup-begin nil)
6521     (gnus-summary-exit nil 'leave-hidden)
6522     ;; We have to adjust the point of group mode buffer because
6523     ;; point was moved to the next unread newsgroup by exiting.
6524     (gnus-summary-jump-to-group group)
6525     (when rescan
6526       (save-excursion
6527         (gnus-group-get-new-news-this-group 1)))
6528     (gnus-group-read-group all t)
6529     (gnus-summary-goto-subject current-subject nil t)))
6530
6531 (defun gnus-summary-rescan-group (&optional all)
6532   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6533   (interactive "P")
6534   (gnus-summary-reselect-current-group all t))
6535
6536 (defun gnus-summary-update-info (&optional non-destructive)
6537   (save-excursion
6538     (let ((group gnus-newsgroup-name))
6539       (when group
6540         (when gnus-newsgroup-kill-headers
6541           (setq gnus-newsgroup-killed
6542                 (gnus-compress-sequence
6543                  (gnus-sorted-union
6544                   (gnus-list-range-intersection
6545                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6546                   gnus-newsgroup-unreads)
6547                  t)))
6548         (unless (listp (cdr gnus-newsgroup-killed))
6549           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6550         (let ((headers gnus-newsgroup-headers))
6551           ;; Set the new ranges of read articles.
6552           (with-current-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     (with-current-buffer gnus-dead-summary
6811       (when gnus-dead-summary-mode
6812         (kill-buffer (current-buffer)))))
6813   ;; Make this the current dead summary.
6814   (setq gnus-dead-summary (current-buffer))
6815   (gnus-dead-summary-mode 1)
6816   (let ((name (buffer-name)))
6817     (when (string-match "Summary" name)
6818       (rename-buffer
6819        (concat (substring name 0 (match-beginning 0)) "Dead "
6820                (substring name (match-beginning 0)))
6821        t)
6822       (bury-buffer))))
6823
6824 (defun gnus-kill-or-deaden-summary (buffer)
6825   "Kill or deaden the summary BUFFER."
6826   (save-excursion
6827     (when (and (buffer-name buffer)
6828                (not gnus-single-article-buffer))
6829       (with-current-buffer buffer
6830         (gnus-kill-buffer gnus-article-buffer)
6831         (gnus-kill-buffer gnus-original-article-buffer)))
6832     (cond
6833      ;; Kill the buffer.
6834      (gnus-kill-summary-on-exit
6835       (when (and gnus-use-trees
6836                  (gnus-buffer-exists-p buffer))
6837         (save-excursion
6838           (set-buffer buffer)
6839           (gnus-tree-close gnus-newsgroup-name)))
6840       (gnus-kill-buffer buffer))
6841      ;; Deaden the buffer.
6842      ((gnus-buffer-exists-p buffer)
6843       (save-excursion
6844         (set-buffer buffer)
6845         (gnus-deaden-summary))))))
6846
6847 (defun gnus-summary-wake-up-the-dead (&rest args)
6848   "Wake up the dead summary buffer."
6849   (interactive)
6850   (gnus-dead-summary-mode -1)
6851   (let ((name (buffer-name)))
6852     (when (string-match "Dead " name)
6853       (rename-buffer
6854        (concat (substring name 0 (match-beginning 0))
6855                (substring name (match-end 0)))
6856        t)))
6857   (gnus-message 3 "This dead summary is now alive again"))
6858
6859 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6860 (defun gnus-summary-fetch-faq (&optional faq-dir)
6861   "Fetch the FAQ for the current group.
6862 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6863 in."
6864   (interactive
6865    (list
6866     (when current-prefix-arg
6867       (completing-read
6868        "FAQ dir: " (and (listp gnus-group-faq-directory)
6869                         (mapcar 'list
6870                                 gnus-group-faq-directory))))))
6871   (let (gnus-faq-buffer)
6872     (when (setq gnus-faq-buffer
6873                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6874       (gnus-configure-windows 'summary-faq))))
6875
6876 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6877 (defun gnus-summary-describe-group (&optional force)
6878   "Describe the current newsgroup."
6879   (interactive "P")
6880   (gnus-group-describe-group force gnus-newsgroup-name))
6881
6882 (defun gnus-summary-describe-briefly ()
6883   "Describe summary mode commands briefly."
6884   (interactive)
6885   (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")))
6886
6887 ;; Walking around group mode buffer from summary mode.
6888
6889 (defun gnus-summary-next-group (&optional no-article target-group backward)
6890   "Exit current newsgroup and then select next unread newsgroup.
6891 If prefix argument NO-ARTICLE is non-nil, no article is selected
6892 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6893 previous group instead."
6894   (interactive "P")
6895   ;; Stop pre-fetching.
6896   (gnus-async-halt-prefetch)
6897   (let ((current-group gnus-newsgroup-name)
6898         (current-buffer (current-buffer))
6899         entered)
6900     ;; First we semi-exit this group to update Xrefs and all variables.
6901     ;; We can't do a real exit, because the window conf must remain
6902     ;; the same in case the user is prompted for info, and we don't
6903     ;; want the window conf to change before that...
6904     (gnus-summary-exit t)
6905     (while (not entered)
6906       ;; Then we find what group we are supposed to enter.
6907       (set-buffer gnus-group-buffer)
6908       (gnus-group-jump-to-group current-group)
6909       (setq target-group
6910             (or target-group
6911                 (if (eq gnus-keep-same-level 'best)
6912                     (gnus-summary-best-group gnus-newsgroup-name)
6913                   (gnus-summary-search-group backward gnus-keep-same-level))))
6914       (if (not target-group)
6915           ;; There are no further groups, so we return to the group
6916           ;; buffer.
6917           (progn
6918             (gnus-message 5 "Returning to the group buffer")
6919             (setq entered t)
6920             (when (gnus-buffer-live-p current-buffer)
6921               (set-buffer current-buffer)
6922               (gnus-summary-exit))
6923             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6924         ;; We try to enter the target group.
6925         (gnus-group-jump-to-group target-group)
6926         (let ((unreads (gnus-group-group-unread)))
6927           (if (and (or (eq t unreads)
6928                        (and unreads (not (zerop unreads))))
6929                    (gnus-summary-read-group
6930                     target-group nil no-article
6931                     (and (buffer-name current-buffer) current-buffer)
6932                     nil backward))
6933               (setq entered t)
6934             (setq current-group target-group
6935                   target-group nil)))))))
6936
6937 (defun gnus-summary-prev-group (&optional no-article)
6938   "Exit current newsgroup and then select previous unread newsgroup.
6939 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6940   (interactive "P")
6941   (gnus-summary-next-group no-article nil t))
6942
6943 ;; Walking around summary lines.
6944
6945 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6946   "Go to the first subject satisfying any non-nil constraint.
6947 If UNREAD is non-nil, the article should be unread.
6948 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6949 If UNSEEN is non-nil, the article should be unseen.
6950 Returns the article selected or nil if there are no matching articles."
6951   (interactive "P")
6952   (cond
6953    ;; Empty summary.
6954    ((null gnus-newsgroup-data)
6955     (gnus-message 3 "No articles in the group")
6956     nil)
6957    ;; Pick the first article.
6958    ((not (or unread undownloaded unseen))
6959     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6960     (gnus-data-number (car gnus-newsgroup-data)))
6961    ;; Find the first unread article.
6962    (t
6963     (let ((data gnus-newsgroup-data))
6964       (while (and data
6965                   (let ((num (gnus-data-number (car data))))
6966                     (or (memq num gnus-newsgroup-unfetched)
6967                         (not (or (and unread
6968                                       (memq num gnus-newsgroup-unreads))
6969                                  (and undownloaded
6970                                       (memq num gnus-newsgroup-undownloaded))
6971                                  (and unseen
6972                                       (memq num gnus-newsgroup-unseen)))))))
6973         (setq data (cdr data)))
6974       (prog1
6975           (if data
6976               (progn
6977                 (goto-char (gnus-data-pos (car data)))
6978                 (gnus-data-number (car data)))
6979             (gnus-message 3 "No more%s articles"
6980                           (let* ((r (when unread " unread"))
6981                                  (d (when undownloaded " undownloaded"))
6982                                  (s (when unseen " unseen"))
6983                                  (l (delq nil (list r d s))))
6984                             (cond ((= 3 (length l))
6985                                    (concat r "," d ", or" s))
6986                                   ((= 2 (length l))
6987                                    (concat (car l) ", or" (cadr l)))
6988                                   ((= 1 (length l))
6989                                    (car l))
6990                                   (t
6991                                    ""))))
6992             nil
6993             )
6994         (gnus-summary-position-point))))))
6995
6996 (defun gnus-summary-next-subject (n &optional unread dont-display)
6997   "Go to next N'th summary line.
6998 If N is negative, go to the previous N'th subject line.
6999 If UNREAD is non-nil, only unread articles are selected.
7000 The difference between N and the actual number of steps taken is
7001 returned."
7002   (interactive "p")
7003   (let ((backward (< n 0))
7004         (n (abs n)))
7005     (while (and (> n 0)
7006                 (if backward
7007                     (gnus-summary-find-prev unread)
7008                   (gnus-summary-find-next unread)))
7009       (unless (zerop (setq n (1- n)))
7010         (gnus-summary-show-thread)))
7011     (when (/= 0 n)
7012       (gnus-message 7 "No more%s articles"
7013                     (if unread " unread" "")))
7014     (unless dont-display
7015       (gnus-summary-recenter)
7016       (gnus-summary-position-point))
7017     n))
7018
7019 (defun gnus-summary-next-unread-subject (n)
7020   "Go to next N'th unread summary line."
7021   (interactive "p")
7022   (gnus-summary-next-subject n t))
7023
7024 (defun gnus-summary-prev-subject (n &optional unread)
7025   "Go to previous N'th summary line.
7026 If optional argument UNREAD is non-nil, only unread article is selected."
7027   (interactive "p")
7028   (gnus-summary-next-subject (- n) unread))
7029
7030 (defun gnus-summary-prev-unread-subject (n)
7031   "Go to previous N'th unread summary line."
7032   (interactive "p")
7033   (gnus-summary-next-subject (- n) t))
7034
7035 (defun gnus-summary-goto-subjects (articles)
7036   "Insert the subject header for ARTICLES in the current buffer."
7037   (save-excursion
7038     (dolist (article articles)
7039       (gnus-summary-goto-subject article t)))
7040   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7041   (gnus-summary-position-point))
7042  
7043 (defun gnus-summary-goto-subject (article &optional force silent)
7044   "Go the subject line of ARTICLE.
7045 If FORCE, also allow jumping to articles not currently shown."
7046   (interactive "nArticle number: ")
7047   (unless (numberp article)
7048     (error "Article %s is not a number" article))
7049   (let ((b (point))
7050         (data (gnus-data-find article)))
7051     ;; We read in the article if we have to.
7052     (and (not data)
7053          force
7054          (gnus-summary-insert-subject
7055           article
7056           (if (or (numberp force) (vectorp force)) force)
7057           t)
7058          (setq data (gnus-data-find article)))
7059     (goto-char b)
7060     (if (not data)
7061         (progn
7062           (unless silent
7063             (gnus-message 3 "Can't find article %d" article))
7064           nil)
7065       (let ((pt (gnus-data-pos data)))
7066         (goto-char pt)
7067         (gnus-summary-set-article-display-arrow pt))
7068       (gnus-summary-position-point)
7069       article)))
7070
7071 ;; Walking around summary lines with displaying articles.
7072
7073 (defun gnus-summary-expand-window (&optional arg)
7074   "Make the summary buffer take up the entire Emacs frame.
7075 Given a prefix, will force an `article' buffer configuration."
7076   (interactive "P")
7077   (if arg
7078       (gnus-configure-windows 'article 'force)
7079     (gnus-configure-windows 'summary 'force)))
7080
7081 (defun gnus-summary-display-article (article &optional all-header)
7082   "Display ARTICLE in article buffer."
7083   (when (gnus-buffer-live-p gnus-article-buffer)
7084     (with-current-buffer gnus-article-buffer
7085       (mm-enable-multibyte)))
7086   (gnus-set-global-variables)
7087   (when (gnus-buffer-live-p gnus-article-buffer)
7088     (with-current-buffer gnus-article-buffer
7089       (setq gnus-article-charset gnus-newsgroup-charset)
7090       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7091       (mm-enable-multibyte)))
7092   (if (null article)
7093       nil
7094     (prog1
7095         (if gnus-summary-display-article-function
7096             (funcall gnus-summary-display-article-function article all-header)
7097           (gnus-article-prepare article all-header))
7098       (gnus-run-hooks 'gnus-select-article-hook)
7099       (when (and gnus-current-article
7100                  (not (zerop gnus-current-article)))
7101         (gnus-summary-goto-subject gnus-current-article))
7102       (gnus-summary-recenter)
7103       (when (and gnus-use-trees gnus-show-threads)
7104         (gnus-possibly-generate-tree article)
7105         (gnus-highlight-selected-tree article))
7106       ;; Successfully display article.
7107       (gnus-article-set-window-start
7108        (cdr (assq article gnus-newsgroup-bookmarks))))))
7109
7110 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7111   "Select the current article.
7112 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7113 non-nil, the article will be re-fetched even if it already present in
7114 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7115 be displayed."
7116   ;; Make sure we are in the summary buffer to work around bbdb bug.
7117   (unless (eq major-mode 'gnus-summary-mode)
7118     (set-buffer gnus-summary-buffer))
7119   (let ((article (or article (gnus-summary-article-number)))
7120         (all-headers (not (not all-headers))) ;Must be t or nil.
7121         gnus-summary-display-article-function)
7122     (and (not pseudo)
7123          (gnus-summary-article-pseudo-p article)
7124          (error "This is a pseudo-article"))
7125     (save-excursion
7126       (set-buffer gnus-summary-buffer)
7127       (if (or (and gnus-single-article-buffer
7128                    (or (null gnus-current-article)
7129                        (null gnus-article-current)
7130                        (null (get-buffer gnus-article-buffer))
7131                        (not (eq article (cdr gnus-article-current)))
7132                        (not (equal (car gnus-article-current)
7133                                    gnus-newsgroup-name))))
7134               (and (not gnus-single-article-buffer)
7135                    (or (null gnus-current-article)
7136                        (not (eq gnus-current-article article))))
7137               force)
7138           ;; The requested article is different from the current article.
7139           (progn
7140             (gnus-summary-display-article article all-headers)
7141             (when (gnus-buffer-live-p gnus-article-buffer)
7142               (with-current-buffer gnus-article-buffer
7143                 (if (not gnus-article-decoded-p) ;; a local variable
7144                     (mm-disable-multibyte))))
7145             (gnus-article-set-window-start
7146              (cdr (assq article gnus-newsgroup-bookmarks)))
7147             article)
7148         'old))))
7149
7150 (defun gnus-summary-force-verify-and-decrypt ()
7151   "Display buttons for signed/encrypted parts and verify/decrypt them."
7152   (interactive)
7153   (let ((mm-verify-option 'known)
7154         (mm-decrypt-option 'known)
7155         (gnus-article-emulate-mime t)
7156         (mm-fill-flowed nil)
7157         (gnus-buttonized-mime-types (append (list "multipart/signed"
7158                                                   "multipart/encrypted")
7159                                             gnus-buttonized-mime-types)))
7160     (gnus-summary-select-article nil 'force)))
7161
7162 (defun gnus-summary-set-current-mark (&optional current-mark)
7163   "Obsolete function."
7164   nil)
7165
7166 (defun gnus-summary-next-article (&optional unread subject backward push)
7167   "Select the next article.
7168 If UNREAD, only unread articles are selected.
7169 If SUBJECT, only articles with SUBJECT are selected.
7170 If BACKWARD, the previous article is selected instead of the next."
7171   (interactive "P")
7172   (cond
7173    ;; Is there such an article?
7174    ((and (gnus-summary-search-forward unread subject backward)
7175          (or (gnus-summary-display-article (gnus-summary-article-number))
7176              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7177     (gnus-summary-position-point))
7178    ;; If not, we try the first unread, if that is wanted.
7179    ((and subject
7180          gnus-auto-select-same
7181          (gnus-summary-first-unread-article))
7182     (gnus-summary-position-point)
7183     (gnus-message 6 "Wrapped"))
7184    ;; Try to get next/previous article not displayed in this group.
7185    ((and gnus-auto-extend-newsgroup
7186          (not unread) (not subject))
7187     (gnus-summary-goto-article
7188      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7189      nil (count-lines (point-min) (point))))
7190    ;; Go to next/previous group.
7191    (t
7192     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7193       (gnus-summary-jump-to-group gnus-newsgroup-name))
7194     (let ((cmd last-command-char)
7195           (point
7196            (with-current-buffer gnus-group-buffer
7197              (point)))
7198           (group
7199            (if (eq gnus-keep-same-level 'best)
7200                (gnus-summary-best-group gnus-newsgroup-name)
7201              (gnus-summary-search-group backward gnus-keep-same-level))))
7202       ;; For some reason, the group window gets selected.  We change
7203       ;; it back.
7204       (select-window (get-buffer-window (current-buffer)))
7205       ;; Select next unread newsgroup automagically.
7206       (cond
7207        ((or (not gnus-auto-select-next)
7208             (not cmd))
7209         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7210        ((or (eq gnus-auto-select-next 'quietly)
7211             (and (eq gnus-auto-select-next 'slightly-quietly)
7212                  push)
7213             (and (eq gnus-auto-select-next 'almost-quietly)
7214                  (gnus-summary-last-article-p)))
7215         ;; Select quietly.
7216         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7217             (gnus-summary-exit)
7218           (gnus-message 7 "No more%s articles (%s)..."
7219                         (if unread " unread" "")
7220                         (if group (concat "selecting " group)
7221                           "exiting"))
7222           (gnus-summary-next-group nil group backward)))
7223        (t
7224         (when (gnus-key-press-event-p last-input-event)
7225           (gnus-summary-walk-group-buffer
7226            gnus-newsgroup-name cmd unread backward point))))))))
7227
7228 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7229   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7230                       (?\C-p (gnus-group-prev-unread-group 1))))
7231         (cursor-in-echo-area t)
7232         keve key group ended prompt)
7233     (save-excursion
7234       (set-buffer gnus-group-buffer)
7235       (goto-char start)
7236       (setq group
7237             (if (eq gnus-keep-same-level 'best)
7238                 (gnus-summary-best-group gnus-newsgroup-name)
7239               (gnus-summary-search-group backward gnus-keep-same-level))))
7240     (while (not ended)
7241       (setq prompt
7242             (format
7243              "No more%s articles%s " (if unread " unread" "")
7244              (if (and group
7245                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7246                  (format " (Type %s for %s [%s])"
7247                          (single-key-description cmd) group
7248                          (gnus-group-unread group))
7249                (format " (Type %s to exit %s)"
7250                        (single-key-description cmd)
7251                        gnus-newsgroup-name))))
7252       ;; Confirm auto selection.
7253       (setq key (car (setq keve (gnus-read-event-char prompt)))
7254             ended t)
7255       (cond
7256        ((assq key keystrokes)
7257         (let ((obuf (current-buffer)))
7258           (switch-to-buffer gnus-group-buffer)
7259           (when group
7260             (gnus-group-jump-to-group group))
7261           (eval (cadr (assq key keystrokes)))
7262           (setq group (gnus-group-group-name))
7263           (switch-to-buffer obuf))
7264         (setq ended nil))
7265        ((equal key cmd)
7266         (if (or (not group)
7267                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7268             (gnus-summary-exit)
7269           (gnus-summary-next-group nil group backward)))
7270        (t
7271         (push (cdr keve) unread-command-events))))))
7272
7273 (defun gnus-summary-next-unread-article ()
7274   "Select unread article after current one."
7275   (interactive)
7276   (gnus-summary-next-article
7277    (or (not (eq gnus-summary-goto-unread 'never))
7278        (gnus-summary-last-article-p (gnus-summary-article-number)))
7279    (and gnus-auto-select-same
7280         (gnus-summary-article-subject))))
7281
7282 (defun gnus-summary-prev-article (&optional unread subject)
7283   "Select the article after the current one.
7284 If UNREAD is non-nil, only unread articles are selected."
7285   (interactive "P")
7286   (gnus-summary-next-article unread subject t))
7287
7288 (defun gnus-summary-prev-unread-article ()
7289   "Select unread article before current one."
7290   (interactive)
7291   (gnus-summary-prev-article
7292    (or (not (eq gnus-summary-goto-unread 'never))
7293        (gnus-summary-first-article-p (gnus-summary-article-number)))
7294    (and gnus-auto-select-same
7295         (gnus-summary-article-subject))))
7296
7297 (defun gnus-summary-next-page (&optional lines circular stop)
7298   "Show next page of the selected article.
7299 If at the end of the current article, select the next article.
7300 LINES says how many lines should be scrolled up.
7301
7302 If CIRCULAR is non-nil, go to the start of the article instead of
7303 selecting the next article when reaching the end of the current
7304 article.
7305
7306 If STOP is non-nil, just stop when reaching the end of the message.
7307
7308 Also see the variable `gnus-article-skip-boring'."
7309   (interactive "P")
7310   (setq gnus-summary-buffer (current-buffer))
7311   (gnus-set-global-variables)
7312   (let ((article (gnus-summary-article-number))
7313         (article-window (get-buffer-window gnus-article-buffer t))
7314         endp)
7315     ;; If the buffer is empty, we have no article.
7316     (unless article
7317       (error "No article to select"))
7318     (gnus-configure-windows 'article)
7319     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7320         (if (and (eq gnus-summary-goto-unread 'never)
7321                  (not (gnus-summary-last-article-p article)))
7322             (gnus-summary-next-article)
7323           (gnus-summary-next-unread-article))
7324       (if (or (null gnus-current-article)
7325               (null gnus-article-current)
7326               (/= article (cdr gnus-article-current))
7327               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7328           ;; Selected subject is different from current article's.
7329           (gnus-summary-display-article article)
7330         (when article-window
7331           (gnus-eval-in-buffer-window gnus-article-buffer
7332             (setq endp (or (gnus-article-next-page lines)
7333                            (gnus-article-only-boring-p))))
7334           (when endp
7335             (cond (stop
7336                    (gnus-message 3 "End of message"))
7337                   (circular
7338                    (gnus-summary-beginning-of-article))
7339                   (lines
7340                    (gnus-message 3 "End of message"))
7341                   ((null lines)
7342                    (if (and (eq gnus-summary-goto-unread 'never)
7343                             (not (gnus-summary-last-article-p article)))
7344                        (gnus-summary-next-article)
7345                      (gnus-summary-next-unread-article))))))))
7346     (gnus-summary-recenter)
7347     (gnus-summary-position-point)))
7348
7349 (defun gnus-summary-prev-page (&optional lines move)
7350   "Show previous page of selected article.
7351 Argument LINES specifies lines to be scrolled down.
7352 If MOVE, move to the previous unread article if point is at
7353 the beginning of the buffer."
7354   (interactive "P")
7355   (let ((article (gnus-summary-article-number))
7356         (article-window (get-buffer-window gnus-article-buffer t))
7357         endp)
7358     (gnus-configure-windows 'article)
7359     (if (or (null gnus-current-article)
7360             (null gnus-article-current)
7361             (/= article (cdr gnus-article-current))
7362             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7363         ;; Selected subject is different from current article's.
7364         (gnus-summary-display-article article)
7365       (gnus-summary-recenter)
7366       (when article-window
7367         (gnus-eval-in-buffer-window gnus-article-buffer
7368           (setq endp (gnus-article-prev-page lines)))
7369         (when (and move endp)
7370           (cond (lines
7371                  (gnus-message 3 "Beginning of message"))
7372                 ((null lines)
7373                  (if (and (eq gnus-summary-goto-unread 'never)
7374                           (not (gnus-summary-first-article-p article)))
7375                      (gnus-summary-prev-article)
7376                    (gnus-summary-prev-unread-article))))))))
7377   (gnus-summary-position-point))
7378
7379 (defun gnus-summary-prev-page-or-article (&optional lines)
7380   "Show previous page of selected article.
7381 Argument LINES specifies lines to be scrolled down.
7382 If at the beginning of the article, go to the next article."
7383   (interactive "P")
7384   (gnus-summary-prev-page lines t))
7385
7386 (defun gnus-summary-scroll-up (lines)
7387   "Scroll up (or down) one line current article.
7388 Argument LINES specifies lines to be scrolled up (or down if negative)."
7389   (interactive "p")
7390   (gnus-configure-windows 'article)
7391   (gnus-summary-show-thread)
7392   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7393     (gnus-eval-in-buffer-window gnus-article-buffer
7394       (cond ((> lines 0)
7395              (when (gnus-article-next-page lines)
7396                (gnus-message 3 "End of message")))
7397             ((< lines 0)
7398              (gnus-article-prev-page (- lines))))))
7399   (gnus-summary-recenter)
7400   (gnus-summary-position-point))
7401
7402 (defun gnus-summary-scroll-down (lines)
7403   "Scroll down (or up) one line current article.
7404 Argument LINES specifies lines to be scrolled down (or up if negative)."
7405   (interactive "p")
7406   (gnus-summary-scroll-up (- lines)))
7407
7408 (defun gnus-summary-next-same-subject ()
7409   "Select next article which has the same subject as current one."
7410   (interactive)
7411   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7412
7413 (defun gnus-summary-prev-same-subject ()
7414   "Select previous article which has the same subject as current one."
7415   (interactive)
7416   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7417
7418 (defun gnus-summary-next-unread-same-subject ()
7419   "Select next unread article which has the same subject as current one."
7420   (interactive)
7421   (gnus-summary-next-article t (gnus-summary-article-subject)))
7422
7423 (defun gnus-summary-prev-unread-same-subject ()
7424   "Select previous unread article which has the same subject as current one."
7425   (interactive)
7426   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7427
7428 (defun gnus-summary-first-unread-article ()
7429   "Select the first unread article.
7430 Return nil if there are no unread articles."
7431   (interactive)
7432   (prog1
7433       (when (gnus-summary-first-subject t)
7434         (gnus-summary-show-thread)
7435         (gnus-summary-first-subject t)
7436         (gnus-summary-display-article (gnus-summary-article-number)))
7437     (gnus-summary-position-point)))
7438
7439 (defun gnus-summary-first-unread-subject ()
7440   "Place the point on the subject line of the first unread article.
7441 Return nil if there are no unread articles."
7442   (interactive)
7443   (prog1
7444       (when (gnus-summary-first-subject t)
7445         (gnus-summary-show-thread)
7446         (gnus-summary-first-subject t))
7447     (gnus-summary-position-point)))
7448
7449 (defun gnus-summary-first-unseen-subject ()
7450   "Place the point on the subject line of the first unseen article.
7451 Return nil if there are no unseen articles."
7452   (interactive)
7453   (prog1
7454       (when (gnus-summary-first-subject nil nil t)
7455         (gnus-summary-show-thread)
7456         (gnus-summary-first-subject nil nil t))
7457     (gnus-summary-position-point)))
7458
7459 (defun gnus-summary-first-unseen-or-unread-subject ()
7460   "Place the point on the subject line of the first unseen article or,
7461 if all article have been seen, on the subject line of the first unread
7462 article."
7463   (interactive)
7464   (prog1
7465       (unless (when (gnus-summary-first-subject nil nil t)
7466                 (gnus-summary-show-thread)
7467                 (gnus-summary-first-subject nil nil t))
7468         (when (gnus-summary-first-subject t)
7469           (gnus-summary-show-thread)
7470           (gnus-summary-first-subject t)))
7471     (gnus-summary-position-point)))
7472
7473 (defun gnus-summary-first-article ()
7474   "Select the first article.
7475 Return nil if there are no articles."
7476   (interactive)
7477   (prog1
7478       (when (gnus-summary-first-subject)
7479         (gnus-summary-show-thread)
7480         (gnus-summary-first-subject)
7481         (gnus-summary-display-article (gnus-summary-article-number)))
7482     (gnus-summary-position-point)))
7483
7484 (defun gnus-summary-best-unread-article (&optional arg)
7485   "Select the unread article with the highest score.
7486 If given a prefix argument, select the next unread article that has a
7487 score higher than the default score."
7488   (interactive "P")
7489   (let ((article (if arg
7490                      (gnus-summary-better-unread-subject)
7491                    (gnus-summary-best-unread-subject))))
7492     (if article
7493         (gnus-summary-goto-article article)
7494       (error "No unread articles"))))
7495
7496 (defun gnus-summary-best-unread-subject ()
7497   "Select the unread subject with the highest score."
7498   (interactive)
7499   (let ((best -1000000)
7500         (data gnus-newsgroup-data)
7501         article score)
7502     (while data
7503       (and (gnus-data-unread-p (car data))
7504            (> (setq score
7505                     (gnus-summary-article-score (gnus-data-number (car data))))
7506               best)
7507            (setq best score
7508                  article (gnus-data-number (car data))))
7509       (setq data (cdr data)))
7510     (when article
7511       (gnus-summary-goto-subject article))
7512     (gnus-summary-position-point)
7513     article))
7514
7515 (defun gnus-summary-better-unread-subject ()
7516   "Select the first unread subject that has a score over the default score."
7517   (interactive)
7518   (let ((data gnus-newsgroup-data)
7519         article score)
7520     (while (and (setq article (gnus-data-number (car data)))
7521                 (or (gnus-data-read-p (car data))
7522                     (not (> (gnus-summary-article-score article)
7523                             gnus-summary-default-score))))
7524       (setq data (cdr data)))
7525     (when article
7526       (gnus-summary-goto-subject article))
7527     (gnus-summary-position-point)
7528     article))
7529
7530 (defun gnus-summary-last-subject ()
7531   "Go to the last displayed subject line in the group."
7532   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7533     (when article
7534       (gnus-summary-goto-subject article))))
7535
7536 (defun gnus-summary-goto-article (article &optional all-headers force)
7537   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7538 If ALL-HEADERS is non-nil, no header lines are hidden.
7539 If FORCE, go to the article even if it isn't displayed.  If FORCE
7540 is a number, it is the line the article is to be displayed on."
7541   (interactive
7542    (list
7543     (completing-read
7544      "Article number or Message-ID: "
7545      (mapcar (lambda (number) (list (int-to-string number)))
7546              gnus-newsgroup-limit))
7547     current-prefix-arg
7548     t))
7549   (prog1
7550       (if (and (stringp article)
7551                (string-match "@\\|%40" article))
7552           (gnus-summary-refer-article article)
7553         (when (stringp article)
7554           (setq article (string-to-number article)))
7555         (if (gnus-summary-goto-subject article force)
7556             (gnus-summary-display-article article all-headers)
7557           (gnus-message 4 "Couldn't go to article %s" article) nil))
7558     (gnus-summary-position-point)))
7559
7560 (defun gnus-summary-goto-last-article ()
7561   "Go to the previously read article."
7562   (interactive)
7563   (prog1
7564       (when gnus-last-article
7565         (gnus-summary-goto-article gnus-last-article nil t))
7566     (gnus-summary-position-point)))
7567
7568 (defun gnus-summary-pop-article (number)
7569   "Pop one article off the history and go to the previous.
7570 NUMBER articles will be popped off."
7571   (interactive "p")
7572   (let (to)
7573     (setq gnus-newsgroup-history
7574           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7575     (if to
7576         (gnus-summary-goto-article (car to) nil t)
7577       (error "Article history empty")))
7578   (gnus-summary-position-point))
7579
7580 ;; Summary commands and functions for limiting the summary buffer.
7581
7582 (defun gnus-summary-limit-to-articles (n)
7583   "Limit the summary buffer to the next N articles.
7584 If not given a prefix, use the process marked articles instead."
7585   (interactive "P")
7586   (prog1
7587       (let ((articles (gnus-summary-work-articles n)))
7588         (setq gnus-newsgroup-processable nil)
7589         (gnus-summary-limit articles))
7590     (gnus-summary-position-point)))
7591
7592 (defun gnus-summary-pop-limit (&optional total)
7593   "Restore the previous limit.
7594 If given a prefix, remove all limits."
7595   (interactive "P")
7596   (when total
7597     (setq gnus-newsgroup-limits
7598           (list (mapcar (lambda (h) (mail-header-number h))
7599                         gnus-newsgroup-headers))))
7600   (unless gnus-newsgroup-limits
7601     (error "No limit to pop"))
7602   (prog1
7603       (gnus-summary-limit nil 'pop)
7604     (gnus-summary-position-point)))
7605
7606 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7607   "Limit the summary buffer to articles that have subjects that match a regexp.
7608 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7609   (interactive
7610    (list (read-string (if current-prefix-arg
7611                           "Exclude subject (regexp): "
7612                         "Limit to subject (regexp): "))
7613          nil current-prefix-arg))
7614   (unless header
7615     (setq header "subject"))
7616   (when (not (equal "" subject))
7617     (prog1
7618         (let ((articles (gnus-summary-find-matching
7619                          (or header "subject") subject 'all nil nil
7620                          not-matching)))
7621           (unless articles
7622             (error "Found no matches for \"%s\"" subject))
7623           (gnus-summary-limit articles))
7624       (gnus-summary-position-point))))
7625
7626 (defun gnus-summary-limit-to-author (from &optional not-matching)
7627   "Limit the summary buffer to articles that have authors that match a regexp.
7628 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7629   (interactive
7630    (list (read-string (if current-prefix-arg
7631                           "Exclude author (regexp): "
7632                         "Limit to author (regexp): "))
7633          current-prefix-arg))
7634   (gnus-summary-limit-to-subject from "from" not-matching))
7635
7636 (defun gnus-summary-limit-to-age (age &optional younger-p)
7637   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7638 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7639 articles that are younger than AGE days."
7640   (interactive
7641    (let ((younger current-prefix-arg)
7642          (days-got nil)
7643          days)
7644      (while (not days-got)
7645        (setq days (if younger
7646                       (read-string "Limit to articles younger than (in days, older when negative): ")
7647                     (read-string
7648                      "Limit to articles older than (in days, younger when negative): ")))
7649        (when (> (length days) 0)
7650          (setq days (read days)))
7651        (if (numberp days)
7652            (progn
7653              (setq days-got t)
7654              (when (< days 0)
7655                (setq younger (not younger))
7656                (setq days (* days -1))))
7657          (message "Please enter a number.")
7658          (sleep-for 1)))
7659      (list days younger)))
7660   (prog1
7661       (let ((data gnus-newsgroup-data)
7662             (cutoff (days-to-time age))
7663             articles d date is-younger)
7664         (while (setq d (pop data))
7665           (when (and (vectorp (gnus-data-header d))
7666                      (setq date (mail-header-date (gnus-data-header d))))
7667             (setq is-younger (time-less-p
7668                               (time-since (condition-case ()
7669                                               (date-to-time date)
7670                                             (error '(0 0))))
7671                               cutoff))
7672             (when (if younger-p
7673                       is-younger
7674                     (not is-younger))
7675               (push (gnus-data-number d) articles))))
7676         (gnus-summary-limit (nreverse articles)))
7677     (gnus-summary-position-point)))
7678
7679 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7680   "Limit the summary buffer to articles that match an 'extra' header."
7681   (interactive
7682    (let ((header
7683           (intern
7684            (gnus-completing-read-with-default
7685             (symbol-name (car gnus-extra-headers))
7686             (if current-prefix-arg
7687                 "Exclude extra header:"
7688               "Limit extra header:")
7689             (mapcar (lambda (x)
7690                       (cons (symbol-name x) x))
7691                     gnus-extra-headers)
7692             nil
7693             t))))
7694      (list header
7695            (read-string (format "%s header %s (regexp): "
7696                                 (if current-prefix-arg "Exclude" "Limit to")
7697                                 header))
7698            current-prefix-arg)))
7699   (when (not (equal "" regexp))
7700     (prog1
7701         (let ((articles (gnus-summary-find-matching
7702                          (cons 'extra header) regexp 'all nil nil
7703                          not-matching)))
7704           (unless articles
7705             (error "Found no matches for \"%s\"" regexp))
7706           (gnus-summary-limit articles))
7707       (gnus-summary-position-point))))
7708
7709 (defun gnus-summary-limit-to-display-predicate ()
7710   "Limit the summary buffer to the predicated in the `display' group parameter."
7711   (interactive)
7712   (unless gnus-newsgroup-display
7713     (error "There is no `display' group parameter"))
7714   (let (articles)
7715     (dolist (number gnus-newsgroup-articles)
7716       (when (funcall gnus-newsgroup-display)
7717         (push number articles)))
7718     (gnus-summary-limit articles))
7719   (gnus-summary-position-point))
7720
7721 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7722 (make-obsolete
7723  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7724
7725 (defun gnus-summary-limit-to-unread (&optional all)
7726   "Limit the summary buffer to articles that are not marked as read.
7727 If ALL is non-nil, limit strictly to unread articles."
7728   (interactive "P")
7729   (if all
7730       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7731     (gnus-summary-limit-to-marks
7732      ;; Concat all the marks that say that an article is read and have
7733      ;; those removed.
7734      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7735            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7736            gnus-low-score-mark gnus-expirable-mark
7737            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7738            gnus-duplicate-mark gnus-souped-mark)
7739      'reverse)))
7740
7741 (defun gnus-summary-limit-to-replied (&optional unreplied)
7742   "Limit the summary buffer to replied articles.
7743 If UNREPLIED (the prefix), limit to unreplied articles."
7744   (interactive "P")
7745   (if unreplied
7746       (gnus-summary-limit
7747        (gnus-set-difference gnus-newsgroup-articles
7748         gnus-newsgroup-replied))
7749     (gnus-summary-limit gnus-newsgroup-replied))
7750   (gnus-summary-position-point))
7751
7752 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7753 (make-obsolete 'gnus-summary-delete-marked-with
7754                'gnus-summary-limit-exclude-marks)
7755
7756 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7757   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7758 If REVERSE, limit the summary buffer to articles that are marked
7759 with MARKS.  MARKS can either be a string of marks or a list of marks.
7760 Returns how many articles were removed."
7761   (interactive "sMarks: ")
7762   (gnus-summary-limit-to-marks marks t))
7763
7764 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7765   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7766 If REVERSE (the prefix), limit the summary buffer to articles that are
7767 not marked with MARKS.  MARKS can either be a string of marks or a
7768 list of marks.
7769 Returns how many articles were removed."
7770   (interactive "sMarks: \nP")
7771   (prog1
7772       (let ((data gnus-newsgroup-data)
7773             (marks (if (listp marks) marks
7774                      (append marks nil))) ; Transform to list.
7775             articles)
7776         (while data
7777           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7778                   (memq (gnus-data-mark (car data)) marks))
7779             (push (gnus-data-number (car data)) articles))
7780           (setq data (cdr data)))
7781         (gnus-summary-limit articles))
7782     (gnus-summary-position-point)))
7783
7784 (defun gnus-summary-limit-to-score (score)
7785   "Limit to articles with score at or above SCORE."
7786   (interactive "NLimit to articles with score of at least: ")
7787   (let ((data gnus-newsgroup-data)
7788         articles)
7789     (while data
7790       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7791                 score)
7792         (push (gnus-data-number (car data)) articles))
7793       (setq data (cdr data)))
7794     (prog1
7795         (gnus-summary-limit articles)
7796       (gnus-summary-position-point))))
7797
7798 (defun gnus-summary-limit-to-unseen ()
7799   "Limit to unseen articles."
7800   (interactive)
7801   (prog1
7802       (gnus-summary-limit gnus-newsgroup-unseen)
7803     (gnus-summary-position-point)))
7804
7805 (defun gnus-summary-limit-include-thread (id)
7806   "Display all the hidden articles that is in the thread with ID in it.
7807 When called interactively, ID is the Message-ID of the current
7808 article."
7809   (interactive (list (mail-header-id (gnus-summary-article-header))))
7810   (let ((articles (gnus-articles-in-thread
7811                    (gnus-id-to-thread (gnus-root-id id)))))
7812     (prog1
7813         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7814       (gnus-summary-limit-include-matching-articles
7815        "subject"
7816        (regexp-quote (gnus-simplify-subject-re
7817                       (mail-header-subject (gnus-id-to-header id)))))
7818       (gnus-summary-position-point))))
7819
7820 (defun gnus-summary-limit-include-matching-articles (header regexp)
7821   "Display all the hidden articles that have HEADERs that match REGEXP."
7822   (interactive (list (read-string "Match on header: ")
7823                      (read-string "Regexp: ")))
7824   (let ((articles (gnus-find-matching-articles header regexp)))
7825     (prog1
7826         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7827       (gnus-summary-position-point))))
7828
7829 (defun gnus-summary-insert-dormant-articles ()
7830   "Insert all the dormant articles for this group into the current buffer."
7831   (interactive)
7832   (let ((gnus-verbose (max 6 gnus-verbose)))
7833     (if (not gnus-newsgroup-dormant)
7834         (gnus-message 3 "No cached articles for this group")
7835       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7836
7837 (defun gnus-summary-limit-include-dormant ()
7838   "Display all the hidden articles that are marked as dormant.
7839 Note that this command only works on a subset of the articles currently
7840 fetched for this group."
7841   (interactive)
7842   (unless gnus-newsgroup-dormant
7843     (error "There are no dormant articles in this group"))
7844   (prog1
7845       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7846     (gnus-summary-position-point)))
7847
7848 (defun gnus-summary-limit-exclude-dormant ()
7849   "Hide all dormant articles."
7850   (interactive)
7851   (prog1
7852       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7853     (gnus-summary-position-point)))
7854
7855 (defun gnus-summary-limit-exclude-childless-dormant ()
7856   "Hide all dormant articles that have no children."
7857   (interactive)
7858   (let ((data (gnus-data-list t))
7859         articles d children)
7860     ;; Find all articles that are either not dormant or have
7861     ;; children.
7862     (while (setq d (pop data))
7863       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7864                 (and (setq children
7865                            (gnus-article-children (gnus-data-number d)))
7866                      (let (found)
7867                        (while children
7868                          (when (memq (car children) articles)
7869                            (setq children nil
7870                                  found t))
7871                          (pop children))
7872                        found)))
7873         (push (gnus-data-number d) articles)))
7874     ;; Do the limiting.
7875     (prog1
7876         (gnus-summary-limit articles)
7877       (gnus-summary-position-point))))
7878
7879 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7880   "Mark all unread excluded articles as read.
7881 If ALL, mark even excluded ticked and dormants as read."
7882   (interactive "P")
7883   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7884   (let ((articles (gnus-sorted-ndifference
7885                    (sort
7886                     (mapcar (lambda (h) (mail-header-number h))
7887                             gnus-newsgroup-headers)
7888                     '<)
7889                    gnus-newsgroup-limit))
7890         article)
7891     (setq gnus-newsgroup-unreads
7892           (gnus-sorted-intersection gnus-newsgroup-unreads
7893                                     gnus-newsgroup-limit))
7894     (if all
7895         (setq gnus-newsgroup-dormant nil
7896               gnus-newsgroup-marked nil
7897               gnus-newsgroup-reads
7898               (nconc
7899                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7900                gnus-newsgroup-reads))
7901       (while (setq article (pop articles))
7902         (unless (or (memq article gnus-newsgroup-dormant)
7903                     (memq article gnus-newsgroup-marked))
7904           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7905
7906 (defun gnus-summary-limit (articles &optional pop)
7907   (if pop
7908       ;; We pop the previous limit off the stack and use that.
7909       (setq articles (car gnus-newsgroup-limits)
7910             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7911     ;; We use the new limit, so we push the old limit on the stack.
7912     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7913   ;; Set the limit.
7914   (setq gnus-newsgroup-limit articles)
7915   (let ((total (length gnus-newsgroup-data))
7916         (data (gnus-data-find-list (gnus-summary-article-number)))
7917         (gnus-summary-mark-below nil)   ; Inhibit this.
7918         found)
7919     ;; This will do all the work of generating the new summary buffer
7920     ;; according to the new limit.
7921     (gnus-summary-prepare)
7922     ;; Hide any threads, possibly.
7923     (gnus-summary-maybe-hide-threads)
7924     ;; Try to return to the article you were at, or one in the
7925     ;; neighborhood.
7926     (when data
7927       ;; We try to find some article after the current one.
7928       (while data
7929         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7930           (setq data nil
7931                 found t))
7932         (setq data (cdr data))))
7933     (unless found
7934       ;; If there is no data, that means that we were after the last
7935       ;; article.  The same goes when we can't find any articles
7936       ;; after the current one.
7937       (goto-char (point-max))
7938       (gnus-summary-find-prev))
7939     (gnus-set-mode-line 'summary)
7940     ;; We return how many articles were removed from the summary
7941     ;; buffer as a result of the new limit.
7942     (- total (length gnus-newsgroup-data))))
7943
7944 (defsubst gnus-invisible-cut-children (threads)
7945   (let ((num 0))
7946     (while threads
7947       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7948         (incf num))
7949       (pop threads))
7950     (< num 2)))
7951
7952 (defsubst gnus-cut-thread (thread)
7953   "Go forwards in the thread until we find an article that we want to display."
7954   (when (or (eq gnus-fetch-old-headers 'some)
7955             (eq gnus-fetch-old-headers 'invisible)
7956             (numberp gnus-fetch-old-headers)
7957             (eq gnus-build-sparse-threads 'some)
7958             (eq gnus-build-sparse-threads 'more))
7959     ;; Deal with old-fetched headers and sparse threads.
7960     (while (and
7961             thread
7962             (or
7963              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7964              (gnus-summary-article-ancient-p
7965               (mail-header-number (car thread))))
7966             (if (or (<= (length (cdr thread)) 1)
7967                     (eq gnus-fetch-old-headers 'invisible))
7968                 (setq gnus-newsgroup-limit
7969                       (delq (mail-header-number (car thread))
7970                             gnus-newsgroup-limit)
7971                       thread (cadr thread))
7972               (when (gnus-invisible-cut-children (cdr thread))
7973                 (let ((th (cdr thread)))
7974                   (while th
7975                     (if (memq (mail-header-number (caar th))
7976                               gnus-newsgroup-limit)
7977                         (setq thread (car th)
7978                               th nil)
7979                       (setq th (cdr th))))))))))
7980   thread)
7981
7982 (defun gnus-cut-threads (threads)
7983   "Cut off all uninteresting articles from the beginning of THREADS."
7984   (when (or (eq gnus-fetch-old-headers 'some)
7985             (eq gnus-fetch-old-headers 'invisible)
7986             (numberp gnus-fetch-old-headers)
7987             (eq gnus-build-sparse-threads 'some)
7988             (eq gnus-build-sparse-threads 'more))
7989     (let ((th threads))
7990       (while th
7991         (setcar th (gnus-cut-thread (car th)))
7992         (setq th (cdr th)))))
7993   ;; Remove nixed out threads.
7994   (delq nil threads))
7995
7996 (defun gnus-summary-initial-limit (&optional show-if-empty)
7997   "Figure out what the initial limit is supposed to be on group entry.
7998 This entails weeding out unwanted dormants, low-scored articles,
7999 fetch-old-headers verbiage, and so on."
8000   ;; Most groups have nothing to remove.
8001   (if (or gnus-inhibit-limiting
8002           (and (null gnus-newsgroup-dormant)
8003                (eq gnus-newsgroup-display 'gnus-not-ignore)
8004                (not (eq gnus-fetch-old-headers 'some))
8005                (not (numberp gnus-fetch-old-headers))
8006                (not (eq gnus-fetch-old-headers 'invisible))
8007                (null gnus-summary-expunge-below)
8008                (not (eq gnus-build-sparse-threads 'some))
8009                (not (eq gnus-build-sparse-threads 'more))
8010                (null gnus-thread-expunge-below)
8011                (not gnus-use-nocem)))
8012       ()                                ; Do nothing.
8013     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8014     (setq gnus-newsgroup-limit nil)
8015     (mapatoms
8016      (lambda (node)
8017        (unless (car (symbol-value node))
8018          ;; These threads have no parents -- they are roots.
8019          (let ((nodes (cdr (symbol-value node)))
8020                thread)
8021            (while nodes
8022              (if (and gnus-thread-expunge-below
8023                       (< (gnus-thread-total-score (car nodes))
8024                          gnus-thread-expunge-below))
8025                  (gnus-expunge-thread (pop nodes))
8026                (setq thread (pop nodes))
8027                (gnus-summary-limit-children thread))))))
8028      gnus-newsgroup-dependencies)
8029     ;; If this limitation resulted in an empty group, we might
8030     ;; pop the previous limit and use it instead.
8031     (when (and (not gnus-newsgroup-limit)
8032                show-if-empty)
8033       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8034     gnus-newsgroup-limit))
8035
8036 (defun gnus-summary-limit-children (thread)
8037   "Return 1 if this subthread is visible and 0 if it is not."
8038   ;; First we get the number of visible children to this thread.  This
8039   ;; is done by recursing down the thread using this function, so this
8040   ;; will really go down to a leaf article first, before slowly
8041   ;; working its way up towards the root.
8042   (when thread
8043     (let* ((max-lisp-eval-depth 5000)
8044            (children
8045            (if (cdr thread)
8046                (apply '+ (mapcar 'gnus-summary-limit-children
8047                                  (cdr thread)))
8048              0))
8049           (number (mail-header-number (car thread)))
8050           score)
8051       (if (and
8052            (not (memq number gnus-newsgroup-marked))
8053            (or
8054             ;; If this article is dormant and has absolutely no visible
8055             ;; children, then this article isn't visible.
8056             (and (memq number gnus-newsgroup-dormant)
8057                  (zerop children))
8058             ;; If this is "fetch-old-headered" and there is no
8059             ;; visible children, then we don't want this article.
8060             (and (or (eq gnus-fetch-old-headers 'some)
8061                      (numberp gnus-fetch-old-headers))
8062                  (gnus-summary-article-ancient-p number)
8063                  (zerop children))
8064             ;; If this is "fetch-old-headered" and `invisible', then
8065             ;; we don't want this article.
8066             (and (eq gnus-fetch-old-headers 'invisible)
8067                  (gnus-summary-article-ancient-p number))
8068             ;; If this is a sparsely inserted article with no children,
8069             ;; we don't want it.
8070             (and (eq gnus-build-sparse-threads 'some)
8071                  (gnus-summary-article-sparse-p number)
8072                  (zerop children))
8073             ;; If we use expunging, and this article is really
8074             ;; low-scored, then we don't want this article.
8075             (when (and gnus-summary-expunge-below
8076                        (< (setq score
8077                                 (or (cdr (assq number gnus-newsgroup-scored))
8078                                     gnus-summary-default-score))
8079                           gnus-summary-expunge-below))
8080               ;; We increase the expunge-tally here, but that has
8081               ;; nothing to do with the limits, really.
8082               (incf gnus-newsgroup-expunged-tally)
8083               ;; We also mark as read here, if that's wanted.
8084               (when (and gnus-summary-mark-below
8085                          (< score gnus-summary-mark-below))
8086                 (setq gnus-newsgroup-unreads
8087                       (delq number gnus-newsgroup-unreads))
8088                 (if gnus-newsgroup-auto-expire
8089                     (push number gnus-newsgroup-expirable)
8090                   (push (cons number gnus-low-score-mark)
8091                         gnus-newsgroup-reads)))
8092               t)
8093             ;; Do the `display' group parameter.
8094             (and gnus-newsgroup-display
8095                  (not (funcall gnus-newsgroup-display)))
8096             ;; Check NoCeM things.
8097             (when (and gnus-use-nocem
8098                        (gnus-nocem-unwanted-article-p
8099                         (mail-header-id (car thread))))
8100               (setq gnus-newsgroup-unreads
8101                     (delq number gnus-newsgroup-unreads))
8102               t)))
8103           ;; Nope, invisible article.
8104           0
8105         ;; Ok, this article is to be visible, so we add it to the limit
8106         ;; and return 1.
8107         (push number gnus-newsgroup-limit)
8108         1))))
8109
8110 (defun gnus-expunge-thread (thread)
8111   "Mark all articles in THREAD as read."
8112   (let* ((number (mail-header-number (car thread))))
8113     (incf gnus-newsgroup-expunged-tally)
8114     ;; We also mark as read here, if that's wanted.
8115     (setq gnus-newsgroup-unreads
8116           (delq number gnus-newsgroup-unreads))
8117     (if gnus-newsgroup-auto-expire
8118         (push number gnus-newsgroup-expirable)
8119       (push (cons number gnus-low-score-mark)
8120             gnus-newsgroup-reads)))
8121   ;; Go recursively through all subthreads.
8122   (mapcar 'gnus-expunge-thread (cdr thread)))
8123
8124 ;; Summary article oriented commands
8125
8126 (defun gnus-summary-refer-parent-article (n)
8127   "Refer parent article N times.
8128 If N is negative, go to ancestor -N instead.
8129 The difference between N and the number of articles fetched is returned."
8130   (interactive "p")
8131   (let ((skip 1)
8132         error header ref)
8133     (when (not (natnump n))
8134       (setq skip (abs n)
8135             n 1))
8136     (while (and (> n 0)
8137                 (not error))
8138       (setq header (gnus-summary-article-header))
8139       (if (and (eq (mail-header-number header)
8140                    (cdr gnus-article-current))
8141                (equal gnus-newsgroup-name
8142                       (car gnus-article-current)))
8143           ;; If we try to find the parent of the currently
8144           ;; displayed article, then we take a look at the actual
8145           ;; References header, since this is slightly more
8146           ;; reliable than the References field we got from the
8147           ;; server.
8148           (save-excursion
8149             (set-buffer gnus-original-article-buffer)
8150             (nnheader-narrow-to-headers)
8151             (unless (setq ref (message-fetch-field "references"))
8152               (when (setq ref (message-fetch-field "in-reply-to"))
8153                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8154             (widen))
8155         (setq ref
8156               ;; It's not the current article, so we take a bet on
8157               ;; the value we got from the server.
8158               (mail-header-references header)))
8159       (if (and ref
8160                (not (equal ref "")))
8161           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8162             (gnus-message 1 "Couldn't find parent"))
8163         (gnus-message 1 "No references in article %d"
8164                       (gnus-summary-article-number))
8165         (setq error t))
8166       (decf n))
8167     (gnus-summary-position-point)
8168     n))
8169
8170 (defun gnus-summary-refer-references ()
8171   "Fetch all articles mentioned in the References header.
8172 Return the number of articles fetched."
8173   (interactive)
8174   (let ((ref (mail-header-references (gnus-summary-article-header)))
8175         (current (gnus-summary-article-number))
8176         (n 0))
8177     (if (or (not ref)
8178             (equal ref ""))
8179         (error "No References in the current article")
8180       ;; For each Message-ID in the References header...
8181       (while (string-match "<[^>]*>" ref)
8182         (incf n)
8183         ;; ... fetch that article.
8184         (gnus-summary-refer-article
8185          (prog1 (match-string 0 ref)
8186            (setq ref (substring ref (match-end 0))))))
8187       (gnus-summary-goto-subject current)
8188       (gnus-summary-position-point)
8189       n)))
8190
8191 (defun gnus-summary-refer-thread (&optional limit)
8192   "Fetch all articles in the current thread.
8193 If LIMIT (the numerical prefix), fetch that many old headers instead
8194 of what's specified by the `gnus-refer-thread-limit' variable."
8195   (interactive "P")
8196   (let ((id (mail-header-id (gnus-summary-article-header)))
8197         (limit (if limit (prefix-numeric-value limit)
8198                  gnus-refer-thread-limit)))
8199     (unless (eq gnus-fetch-old-headers 'invisible)
8200       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8201       ;; Retrieve the headers and read them in.
8202       (if (eq (if (numberp limit)
8203                   (gnus-retrieve-headers
8204                    (list (min
8205                           (+ (mail-header-number
8206                               (gnus-summary-article-header))
8207                              limit)
8208                           gnus-newsgroup-end))
8209                    gnus-newsgroup-name (* limit 2))
8210                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8211                 ;; headers.
8212                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8213                                        gnus-newsgroup-name limit))
8214               'nov)
8215           (gnus-build-all-threads)
8216         (error "Can't fetch thread from back ends that don't support NOV"))
8217       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8218     (gnus-summary-limit-include-thread id)))
8219
8220 (defun gnus-summary-refer-article (message-id)
8221   "Fetch an article specified by MESSAGE-ID."
8222   (interactive "sMessage-ID: ")
8223   (when (and (stringp message-id)
8224              (not (zerop (length message-id))))
8225     (setq message-id (gnus-replace-in-string message-id " " ""))
8226     ;; Construct the correct Message-ID if necessary.
8227     ;; Suggested by tale@pawl.rpi.edu.
8228     (unless (string-match "^<" message-id)
8229       (setq message-id (concat "<" message-id)))
8230     (unless (string-match ">$" message-id)
8231       (setq message-id (concat message-id ">")))
8232     ;; People often post MIDs from URLs, so unhex it:
8233     (unless (string-match "@" message-id)
8234       (setq message-id (gnus-url-unhex-string message-id)))
8235     (let* ((header (gnus-id-to-header message-id))
8236            (sparse (and header
8237                         (gnus-summary-article-sparse-p
8238                          (mail-header-number header))
8239                         (memq (mail-header-number header)
8240                               gnus-newsgroup-limit)))
8241            number)
8242       (cond
8243        ;; If the article is present in the buffer we just go to it.
8244        ((and header
8245              (or (not (gnus-summary-article-sparse-p
8246                        (mail-header-number header)))
8247                  sparse))
8248         (prog1
8249             (gnus-summary-goto-article
8250              (mail-header-number header) nil t)
8251           (when sparse
8252             (gnus-summary-update-article (mail-header-number header)))))
8253        (t
8254         ;; We fetch the article.
8255         (catch 'found
8256           (dolist (gnus-override-method (gnus-refer-article-methods))
8257             (when (and (gnus-check-server gnus-override-method)
8258                        ;; Fetch the header,
8259                        (setq number (gnus-summary-insert-subject message-id)))
8260               ;; and display the article.
8261               (gnus-summary-select-article nil nil nil number)
8262               (throw 'found t)))
8263           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8264
8265 (defun gnus-refer-article-methods ()
8266   "Return a list of referable methods."
8267   (cond
8268    ;; No method, so we default to current and native.
8269    ((null gnus-refer-article-method)
8270     (list gnus-current-select-method gnus-select-method))
8271    ;; Current.
8272    ((eq 'current gnus-refer-article-method)
8273     (list gnus-current-select-method))
8274    ;; List of select methods.
8275    ((not (and (symbolp (car gnus-refer-article-method))
8276               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8277     (let (out)
8278       (dolist (method gnus-refer-article-method)
8279         (push (if (eq 'current method)
8280                   gnus-current-select-method
8281                 method)
8282               out))
8283       (nreverse out)))
8284    ;; One single select method.
8285    (t
8286     (list gnus-refer-article-method))))
8287
8288 (defun gnus-summary-edit-parameters ()
8289   "Edit the group parameters of the current group."
8290   (interactive)
8291   (gnus-group-edit-group gnus-newsgroup-name 'params))
8292
8293 (defun gnus-summary-customize-parameters ()
8294   "Customize the group parameters of the current group."
8295   (interactive)
8296   (gnus-group-customize gnus-newsgroup-name))
8297
8298 (defun gnus-summary-enter-digest-group (&optional force)
8299   "Enter an nndoc group based on the current article.
8300 If FORCE, force a digest interpretation.  If not, try
8301 to guess what the document format is."
8302   (interactive "P")
8303   (let ((conf gnus-current-window-configuration))
8304     (save-window-excursion
8305       (save-excursion
8306         (let (gnus-article-prepare-hook
8307               gnus-display-mime-function
8308               gnus-break-pages)
8309           (gnus-summary-select-article))))
8310     (setq gnus-current-window-configuration conf)
8311     (let* ((name (format "%s-%d"
8312                          (gnus-group-prefixed-name
8313                           gnus-newsgroup-name (list 'nndoc ""))
8314                          (with-current-buffer gnus-summary-buffer
8315                            gnus-current-article)))
8316            (ogroup gnus-newsgroup-name)
8317            (params (append (gnus-info-params (gnus-get-info ogroup))
8318                            (list (cons 'to-group ogroup))
8319                            (list (cons 'parent-group ogroup))
8320                            (list (cons 'save-article-group ogroup))))
8321            (case-fold-search t)
8322            (buf (current-buffer))
8323            dig to-address)
8324       (save-excursion
8325         (set-buffer gnus-original-article-buffer)
8326         ;; Have the digest group inherit the main mail address of
8327         ;; the parent article.
8328         (when (setq to-address (or (gnus-fetch-field "reply-to")
8329                                    (gnus-fetch-field "from")))
8330           (setq params (append
8331                         (list (cons 'to-address
8332                                     (funcall gnus-decode-encoded-word-function
8333                                              to-address))))))
8334         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8335         (insert-buffer-substring gnus-original-article-buffer)
8336         ;; Remove lines that may lead nndoc to misinterpret the
8337         ;; document type.
8338         (narrow-to-region
8339          (goto-char (point-min))
8340          (or (search-forward "\n\n" nil t) (point)))
8341         (goto-char (point-min))
8342         (delete-matching-lines "^Path:\\|^From ")
8343         (widen))
8344       (unwind-protect
8345           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8346                     (gnus-newsgroup-ephemeral-ignored-charsets
8347                      gnus-newsgroup-ignored-charsets))
8348                 (gnus-group-read-ephemeral-group
8349                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8350                               (nndoc-article-type
8351                                ,(if force 'mbox 'guess)))
8352                  t nil nil nil
8353                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8354                                                         "ADAPT")))))
8355               ;; Make all postings to this group go to the parent group.
8356               (nconc (gnus-info-params (gnus-get-info name))
8357                      params)
8358             ;; Couldn't select this doc group.
8359             (switch-to-buffer buf)
8360             (gnus-set-global-variables)
8361             (gnus-configure-windows 'summary)
8362             (gnus-message 3 "Article couldn't be entered?"))
8363         (kill-buffer dig)))))
8364
8365 (defun gnus-summary-read-document (n)
8366   "Open a new group based on the current article(s).
8367 This will allow you to read digests and other similar
8368 documents as newsgroups.
8369 Obeys the standard process/prefix convention."
8370   (interactive "P")
8371   (let* ((articles (gnus-summary-work-articles n))
8372          (ogroup gnus-newsgroup-name)
8373          (params (append (gnus-info-params (gnus-get-info ogroup))
8374                          (list (cons 'to-group ogroup))))
8375          article group egroup groups vgroup)
8376     (while (setq article (pop articles))
8377       (setq group (format "%s-%d" gnus-newsgroup-name article))
8378       (gnus-summary-remove-process-mark article)
8379       (when (gnus-summary-display-article article)
8380         (save-excursion
8381           (with-temp-buffer
8382             (insert-buffer-substring gnus-original-article-buffer)
8383             ;; Remove some headers that may lead nndoc to make
8384             ;; the wrong guess.
8385             (message-narrow-to-head)
8386             (goto-char (point-min))
8387             (delete-matching-lines "^Path:\\|^From ")
8388             (widen)
8389             (if (setq egroup
8390                       (gnus-group-read-ephemeral-group
8391                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8392                                      (nndoc-article-type guess))
8393                        t nil t))
8394                 (progn
8395             ;; Make all postings to this group go to the parent group.
8396                   (nconc (gnus-info-params (gnus-get-info egroup))
8397                          params)
8398                   (push egroup groups))
8399               ;; Couldn't select this doc group.
8400               (gnus-error 3 "Article couldn't be entered"))))))
8401     ;; Now we have selected all the documents.
8402     (cond
8403      ((not groups)
8404       (error "None of the articles could be interpreted as documents"))
8405      ((gnus-group-read-ephemeral-group
8406        (setq vgroup (format
8407                      "nnvirtual:%s-%s" gnus-newsgroup-name
8408                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8409        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8410        t
8411        (cons (current-buffer) 'summary)))
8412      (t
8413       (error "Couldn't select virtual nndoc group")))))
8414
8415 (defun gnus-summary-isearch-article (&optional regexp-p)
8416   "Do incremental search forward on the current article.
8417 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8418   (interactive "P")
8419   (gnus-summary-select-article)
8420   (gnus-configure-windows 'article)
8421   (gnus-eval-in-buffer-window gnus-article-buffer
8422     (save-restriction
8423       (widen)
8424       (isearch-forward regexp-p))))
8425
8426 (defun gnus-summary-search-article-forward (regexp &optional backward)
8427   "Search for an article containing REGEXP forward.
8428 If BACKWARD, search backward instead."
8429   (interactive
8430    (list (read-string
8431           (format "Search article %s (regexp%s): "
8432                   (if current-prefix-arg "backward" "forward")
8433                   (if gnus-last-search-regexp
8434                       (concat ", default " gnus-last-search-regexp)
8435                     "")))
8436          current-prefix-arg))
8437   (if (string-equal regexp "")
8438       (setq regexp (or gnus-last-search-regexp ""))
8439     (setq gnus-last-search-regexp regexp)
8440     (setq gnus-article-before-search gnus-current-article))
8441   ;; Intentionally set gnus-last-article.
8442   (setq gnus-last-article gnus-article-before-search)
8443   (let ((gnus-last-article gnus-last-article))
8444     (if (gnus-summary-search-article regexp backward)
8445         (gnus-summary-show-thread)
8446       (signal 'search-failed (list regexp)))))
8447
8448 (defun gnus-summary-search-article-backward (regexp)
8449   "Search for an article containing REGEXP backward."
8450   (interactive
8451    (list (read-string
8452           (format "Search article backward (regexp%s): "
8453                   (if gnus-last-search-regexp
8454                       (concat ", default " gnus-last-search-regexp)
8455                     "")))))
8456   (gnus-summary-search-article-forward regexp 'backward))
8457
8458 (defun gnus-summary-search-article (regexp &optional backward)
8459   "Search for an article containing REGEXP.
8460 Optional argument BACKWARD means do search for backward.
8461 `gnus-select-article-hook' is not called during the search."
8462   ;; We have to require this here to make sure that the following
8463   ;; dynamic binding isn't shadowed by autoloading.
8464   (require 'gnus-async)
8465   (require 'gnus-art)
8466   (let ((gnus-select-article-hook nil)  ;Disable hook.
8467         (gnus-article-prepare-hook nil)
8468         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8469         (gnus-use-article-prefetch nil)
8470         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8471         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8472         (gnus-visual nil)
8473         (gnus-keep-backlog nil)
8474         (gnus-break-pages nil)
8475         (gnus-summary-display-arrow nil)
8476         (gnus-updated-mode-lines nil)
8477         (gnus-auto-center-summary nil)
8478         (sum (current-buffer))
8479         (gnus-display-mime-function nil)
8480         (found nil)
8481         point)
8482     (gnus-save-hidden-threads
8483       (gnus-summary-select-article)
8484       (set-buffer gnus-article-buffer)
8485       (goto-char (window-point (get-buffer-window (current-buffer))))
8486       (when backward
8487         (forward-line -1))
8488       (while (not found)
8489         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8490         (if (if backward
8491                 (re-search-backward regexp nil t)
8492               (re-search-forward regexp nil t))
8493             ;; We found the regexp.
8494             (progn
8495               (setq found 'found)
8496               (beginning-of-line)
8497               (set-window-start
8498                (get-buffer-window (current-buffer))
8499                (point))
8500               (forward-line 1)
8501               (set-window-point
8502                (get-buffer-window (current-buffer))
8503                (point))
8504               (set-buffer sum)
8505               (setq point (point)))
8506           ;; We didn't find it, so we go to the next article.
8507           (set-buffer sum)
8508           (setq found 'not)
8509           (while (eq found 'not)
8510             (if (not (if backward (gnus-summary-find-prev)
8511                        (gnus-summary-find-next)))
8512                 ;; No more articles.
8513                 (setq found t)
8514               ;; Select the next article and adjust point.
8515               (unless (gnus-summary-article-sparse-p
8516                        (gnus-summary-article-number))
8517                 (setq found nil)
8518                 (gnus-summary-select-article)
8519                 (set-buffer gnus-article-buffer)
8520                 (widen)
8521                 (goto-char (if backward (point-max) (point-min))))))))
8522       (gnus-message 7 ""))
8523     ;; Return whether we found the regexp.
8524     (when (eq found 'found)
8525       (goto-char point)
8526       (gnus-summary-show-thread)
8527       (gnus-summary-goto-subject gnus-current-article)
8528       (gnus-summary-position-point)
8529       t)))
8530
8531 (defun gnus-find-matching-articles (header regexp)
8532   "Return a list of all articles that match REGEXP on HEADER.
8533 This search includes all articles in the current group that Gnus has
8534 fetched headers for, whether they are displayed or not."
8535   (let ((articles nil)
8536         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8537         (case-fold-search t))
8538     (dolist (header gnus-newsgroup-headers)
8539       (when (string-match regexp (funcall func header))
8540         (push (mail-header-number header) articles)))
8541     (nreverse articles)))
8542
8543 (defun gnus-summary-find-matching (header regexp &optional backward unread
8544                                           not-case-fold not-matching)
8545   "Return a list of all articles that match REGEXP on HEADER.
8546 The search stars on the current article and goes forwards unless
8547 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8548 If UNREAD is non-nil, only unread articles will
8549 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8550 in the comparisons. If NOT-MATCHING, return a list of all articles that
8551 not match REGEXP on HEADER."
8552   (let ((case-fold-search (not not-case-fold))
8553         articles d func)
8554     (if (consp header)
8555         (if (eq (car header) 'extra)
8556             (setq func
8557                   `(lambda (h)
8558                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8559                          "")))
8560           (error "%s is an invalid header" header))
8561       (unless (fboundp (intern (concat "mail-header-" header)))
8562         (error "%s is not a valid header" header))
8563       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8564     (dolist (d (if (eq backward 'all)
8565                    gnus-newsgroup-data
8566                  (gnus-data-find-list
8567                   (gnus-summary-article-number)
8568                   (gnus-data-list backward))))
8569       (when (and (or (not unread)       ; We want all articles...
8570                      (gnus-data-unread-p d)) ; Or just unreads.
8571                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8572                  (if not-matching
8573                      (not (string-match
8574                            regexp
8575                            (funcall func (gnus-data-header d))))
8576                    (string-match regexp
8577                                  (funcall func (gnus-data-header d)))))
8578         (push (gnus-data-number d) articles))) ; Success!
8579     (nreverse articles)))
8580
8581 (defun gnus-summary-execute-command (header regexp command &optional backward)
8582   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8583 If HEADER is an empty string (or nil), the match is done on the entire
8584 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8585   (interactive
8586    (list (let ((completion-ignore-case t))
8587            (completing-read
8588             "Header name: "
8589             (mapcar (lambda (header) (list (format "%s" header)))
8590                     (append
8591                      '("Number" "Subject" "From" "Lines" "Date"
8592                        "Message-ID" "Xref" "References" "Body")
8593                      gnus-extra-headers))
8594             nil 'require-match))
8595          (read-string "Regexp: ")
8596          (read-key-sequence "Command: ")
8597          current-prefix-arg))
8598   (when (equal header "Body")
8599     (setq header ""))
8600   ;; Hidden thread subtrees must be searched as well.
8601   (gnus-summary-show-all-threads)
8602   ;; We don't want to change current point nor window configuration.
8603   (save-excursion
8604     (save-window-excursion
8605       (let (gnus-visual
8606             gnus-treat-strip-trailing-blank-lines
8607             gnus-treat-strip-leading-blank-lines
8608             gnus-treat-strip-multiple-blank-lines
8609             gnus-treat-hide-boring-headers
8610             gnus-treat-fold-newsgroups
8611             gnus-article-prepare-hook)
8612         (gnus-message 6 "Executing %s..." (key-description command))
8613         ;; We'd like to execute COMMAND interactively so as to give arguments.
8614         (gnus-execute header regexp
8615                       `(call-interactively ',(key-binding command))
8616                       backward)
8617         (gnus-message 6 "Executing %s...done" (key-description command))))))
8618
8619 (defun gnus-summary-beginning-of-article ()
8620   "Scroll the article back to the beginning."
8621   (interactive)
8622   (gnus-summary-select-article)
8623   (gnus-configure-windows 'article)
8624   (gnus-eval-in-buffer-window gnus-article-buffer
8625     (widen)
8626     (goto-char (point-min))
8627     (when gnus-break-pages
8628       (gnus-narrow-to-page))))
8629
8630 (defun gnus-summary-end-of-article ()
8631   "Scroll to the end of the article."
8632   (interactive)
8633   (gnus-summary-select-article)
8634   (gnus-configure-windows 'article)
8635   (gnus-eval-in-buffer-window gnus-article-buffer
8636     (widen)
8637     (goto-char (point-max))
8638     (recenter -3)
8639     (when gnus-break-pages
8640       (when (re-search-backward page-delimiter nil t)
8641         (narrow-to-region (match-end 0) (point-max)))
8642       (gnus-narrow-to-page))))
8643
8644 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8645   "Truncate to LEN and quote all \"(\"'s in STRING."
8646   (gnus-replace-in-string (if (and len (> (length string) len))
8647                               (substring string 0 len)
8648                             string)
8649                           "[()]" "\\\\\\&"))
8650
8651 (defun gnus-summary-print-article (&optional filename n)
8652   "Generate and print a PostScript image of the process-marked (mail) articles.
8653
8654 If used interactively, print the current article if none are
8655 process-marked.  With prefix arg, prompt the user for the name of the
8656 file to save in.
8657
8658 When used from Lisp, accept two optional args FILENAME and N.  N means
8659 to print the next N articles.  If N is negative, print the N previous
8660 articles.  If N is nil and articles have been marked with the process
8661 mark, print these instead.
8662
8663 If the optional first argument FILENAME is nil, send the image to the
8664 printer.  If FILENAME is a string, save the PostScript image in a file with
8665 that name.  If FILENAME is a number, prompt the user for the name of the file
8666 to save in."
8667   (interactive (list (ps-print-preprint current-prefix-arg)))
8668   (dolist (article (gnus-summary-work-articles n))
8669     (gnus-summary-select-article nil nil 'pseudo article)
8670     (gnus-eval-in-buffer-window gnus-article-buffer
8671       (gnus-print-buffer))
8672     (gnus-summary-remove-process-mark article))
8673   (ps-despool filename))
8674
8675 (defun gnus-print-buffer ()
8676   (let ((buffer (generate-new-buffer " *print*")))
8677     (unwind-protect
8678         (progn
8679           (copy-to-buffer buffer (point-min) (point-max))
8680           (set-buffer buffer)
8681           (gnus-remove-text-with-property 'gnus-decoration)
8682           (when (gnus-visual-p 'article-highlight 'highlight)
8683             ;; Copy-to-buffer doesn't copy overlay.  So redo
8684             ;; highlight.
8685             (let ((gnus-article-buffer buffer))
8686               (gnus-article-highlight-citation t)
8687               (gnus-article-highlight-signature)
8688               (gnus-article-emphasize)
8689               (gnus-article-delete-invisible-text)))
8690           (let ((ps-left-header
8691                  (list
8692                   (concat "("
8693                           (gnus-summary-print-truncate-and-quote
8694                            (mail-header-subject gnus-current-headers)
8695                            66) ")")
8696                   (concat "("
8697                           (gnus-summary-print-truncate-and-quote
8698                            (mail-header-from gnus-current-headers)
8699                            45) ")")))
8700                 (ps-right-header
8701                  (list
8702                   "/pagenumberstring load"
8703                   (concat "("
8704                           (mail-header-date gnus-current-headers) ")"))))
8705             (gnus-run-hooks 'gnus-ps-print-hook)
8706             (save-excursion
8707               (if window-system
8708                   (ps-spool-buffer-with-faces)
8709                 (ps-spool-buffer)))))
8710       (kill-buffer buffer))))
8711
8712 (defun gnus-summary-show-article (&optional arg)
8713   "Force redisplaying of the current article.
8714 If ARG (the prefix) is a number, show the article with the charset
8715 defined in `gnus-summary-show-article-charset-alist', or the charset
8716 input.
8717 If ARG (the prefix) is non-nil and not a number, show the raw article
8718 without any article massaging functions being run.  Normally, the key
8719 strokes are `C-u g'."
8720   (interactive "P")
8721   (cond
8722    ((numberp arg)
8723     (gnus-summary-show-article t)
8724     (let ((gnus-newsgroup-charset
8725            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8726                (mm-read-coding-system
8727                 "View as charset: " ;; actually it is coding system.
8728                 (save-excursion
8729                   (set-buffer gnus-article-buffer)
8730                   (mm-detect-coding-region (point) (point-max))))))
8731           (gnus-newsgroup-ignored-charsets 'gnus-all))
8732       (gnus-summary-select-article nil 'force)
8733       (let ((deps gnus-newsgroup-dependencies)
8734             head header lines)
8735         (save-excursion
8736           (set-buffer gnus-original-article-buffer)
8737           (save-restriction
8738             (message-narrow-to-head)
8739             (setq head (buffer-string))
8740             (goto-char (point-min))
8741             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8742               (goto-char (point-max))
8743               (widen)
8744               (setq lines (1- (count-lines (point) (point-max))))))
8745           (with-temp-buffer
8746             (insert (format "211 %d Article retrieved.\n"
8747                             (cdr gnus-article-current)))
8748             (insert head)
8749             (if lines (insert (format "Lines: %d\n" lines)))
8750             (insert ".\n")
8751             (let ((nntp-server-buffer (current-buffer)))
8752               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8753         (gnus-data-set-header
8754          (gnus-data-find (cdr gnus-article-current))
8755          header)
8756         (gnus-summary-update-article-line
8757          (cdr gnus-article-current) header)
8758         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8759           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8760    ((not arg)
8761     ;; Select the article the normal way.
8762     (gnus-summary-select-article nil 'force))
8763    (t
8764     ;; We have to require this here to make sure that the following
8765     ;; dynamic binding isn't shadowed by autoloading.
8766     (require 'gnus-async)
8767     (require 'gnus-art)
8768     ;; Bind the article treatment functions to nil.
8769     (let ((gnus-have-all-headers t)
8770           gnus-article-prepare-hook
8771           gnus-article-decode-hook
8772           gnus-display-mime-function
8773           gnus-break-pages)
8774       ;; Destroy any MIME parts.
8775       (when (gnus-buffer-live-p gnus-article-buffer)
8776         (save-excursion
8777           (set-buffer gnus-article-buffer)
8778           (mm-destroy-parts gnus-article-mime-handles)
8779           ;; Set it to nil for safety reason.
8780           (setq gnus-article-mime-handle-alist nil)
8781           (setq gnus-article-mime-handles nil)))
8782       (gnus-summary-select-article nil 'force))))
8783   (gnus-summary-goto-subject gnus-current-article)
8784   (gnus-summary-position-point))
8785
8786 (defun gnus-summary-show-raw-article ()
8787   "Show the raw article without any article massaging functions being run."
8788   (interactive)
8789   (gnus-summary-show-article t))
8790
8791 (defun gnus-summary-verbose-headers (&optional arg)
8792   "Toggle permanent full header display.
8793 If ARG is a positive number, turn header display on.
8794 If ARG is a negative number, turn header display off."
8795   (interactive "P")
8796   (setq gnus-show-all-headers
8797         (cond ((or (not (numberp arg))
8798                    (zerop arg))
8799                (not gnus-show-all-headers))
8800               ((natnump arg)
8801                t)))
8802   (gnus-summary-show-article))
8803
8804 (defun gnus-summary-toggle-header (&optional arg)
8805   "Show the headers if they are hidden, or hide them if they are shown.
8806 If ARG is a positive number, show the entire header.
8807 If ARG is a negative number, hide the unwanted header lines."
8808   (interactive "P")
8809   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8810                      (get-buffer-window gnus-article-buffer t))))
8811     (with-current-buffer gnus-article-buffer
8812       (widen)
8813       (article-narrow-to-head)
8814       (let* ((buffer-read-only nil)
8815              (inhibit-point-motion-hooks t)
8816              (hidden (if (numberp arg)
8817                          (>= arg 0)
8818                        (gnus-article-hidden-text-p 'headers)))
8819              s e)
8820         (delete-region (point-min) (point-max))
8821         (with-current-buffer gnus-original-article-buffer
8822           (goto-char (setq s (point-min)))
8823           (setq e (if (search-forward "\n\n" nil t)
8824                       (1- (point))
8825                     (point-max))))
8826         (insert-buffer-substring gnus-original-article-buffer s e)
8827         (run-hooks 'gnus-article-decode-hook)
8828         (if hidden
8829             (let ((gnus-treat-hide-headers nil)
8830                   (gnus-treat-hide-boring-headers nil))
8831               (gnus-delete-wash-type 'headers)
8832               (gnus-treat-article 'head))
8833           (gnus-treat-article 'head))
8834         (widen)
8835         (if window
8836             (set-window-start window (goto-char (point-min))))
8837         (if gnus-break-pages
8838             (gnus-narrow-to-page)
8839           (when (gnus-visual-p 'page-marker)
8840             (let ((buffer-read-only nil))
8841               (gnus-remove-text-with-property 'gnus-prev)
8842               (gnus-remove-text-with-property 'gnus-next))))
8843         (gnus-set-mode-line 'article)))))
8844
8845 (defun gnus-summary-show-all-headers ()
8846   "Make all header lines visible."
8847   (interactive)
8848   (gnus-summary-toggle-header 1))
8849
8850 (defun gnus-summary-caesar-message (&optional arg)
8851   "Caesar rotate the current article by 13.
8852 The numerical prefix specifies how many places to rotate each letter
8853 forward."
8854   (interactive "P")
8855   (gnus-summary-select-article)
8856   (let ((mail-header-separator ""))
8857     (gnus-eval-in-buffer-window gnus-article-buffer
8858       (save-restriction
8859         (widen)
8860         (let ((start (window-start))
8861               buffer-read-only)
8862           (message-caesar-buffer-body arg)
8863           (set-window-start (get-buffer-window (current-buffer)) start))))))
8864
8865 (autoload 'unmorse-region "morse"
8866   "Convert morse coded text in region to ordinary ASCII text."
8867   t)
8868
8869 (defun gnus-summary-morse-message (&optional arg)
8870   "Morse decode the current article."
8871   (interactive "P")
8872   (gnus-summary-select-article)
8873   (let ((mail-header-separator ""))
8874     (gnus-eval-in-buffer-window gnus-article-buffer
8875       (save-excursion
8876         (save-restriction
8877           (widen)
8878           (let ((pos (window-start))
8879                 buffer-read-only)
8880             (goto-char (point-min))
8881             (when (message-goto-body)
8882               (gnus-narrow-to-body))
8883             (goto-char (point-min))
8884             (while (search-forward "·" (point-max) t)
8885               (replace-match "."))
8886             (unmorse-region (point-min) (point-max))
8887             (widen)
8888             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8889
8890 (defun gnus-summary-stop-page-breaking ()
8891   "Stop page breaking in the current article."
8892   (interactive)
8893   (gnus-summary-select-article)
8894   (gnus-eval-in-buffer-window gnus-article-buffer
8895     (widen)
8896     (when (gnus-visual-p 'page-marker)
8897       (let ((buffer-read-only nil))
8898         (gnus-remove-text-with-property 'gnus-prev)
8899         (gnus-remove-text-with-property 'gnus-next))
8900       (setq gnus-page-broken nil))))
8901
8902 (defun gnus-summary-move-article (&optional n to-newsgroup
8903                                             select-method action)
8904   "Move the current article to a different newsgroup.
8905 If N is a positive number, move the N next articles.
8906 If N is a negative number, move the N previous articles.
8907 If N is nil and any articles have been marked with the process mark,
8908 move those articles instead.
8909 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8910 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8911 re-spool using this method.
8912
8913 When called interactively with TO-NEWSGROUP being nil, the value of
8914 the variable `gnus-move-split-methods' is used for finding a default
8915 for the target newsgroup.
8916
8917 For this function to work, both the current newsgroup and the
8918 newsgroup that you want to move to have to support the `request-move'
8919 and `request-accept' functions.
8920
8921 ACTION can be either `move' (the default), `crosspost' or `copy'."
8922   (interactive "P")
8923   (unless action
8924     (setq action 'move))
8925   ;; Check whether the source group supports the required functions.
8926   (cond ((and (eq action 'move)
8927               (not (gnus-check-backend-function
8928                     'request-move-article gnus-newsgroup-name)))
8929          (error "The current group does not support article moving"))
8930         ((and (eq action 'crosspost)
8931               (not (gnus-check-backend-function
8932                     'request-replace-article gnus-newsgroup-name)))
8933          (error "The current group does not support article editing")))
8934   (let ((articles (gnus-summary-work-articles n))
8935         (prefix (if (gnus-check-backend-function
8936                      'request-move-article gnus-newsgroup-name)
8937                     (funcall gnus-move-group-prefix-function
8938                              gnus-newsgroup-name)
8939                   ""))
8940         (names '((move "Move" "Moving")
8941                  (copy "Copy" "Copying")
8942                  (crosspost "Crosspost" "Crossposting")))
8943         (copy-buf (save-excursion
8944                     (nnheader-set-temp-buffer " *copy article*")))
8945         art-group to-method new-xref article to-groups)
8946     (unless (assq action names)
8947       (error "Unknown action %s" action))
8948     ;; Read the newsgroup name.
8949     (when (and (not to-newsgroup)
8950                (not select-method))
8951       (if (and gnus-move-split-methods
8952                (not
8953                 (and (memq gnus-current-article articles)
8954                      (gnus-buffer-live-p gnus-original-article-buffer))))
8955           ;; When `gnus-move-split-methods' is non-nil, we have to
8956           ;; select an article to give `gnus-read-move-group-name' an
8957           ;; opportunity to suggest an appropriate default.  However,
8958           ;; we needn't render or mark the article.
8959           (let ((gnus-display-mime-function nil)
8960                 (gnus-article-prepare-hook nil)
8961                 (gnus-mark-article-hook nil))
8962             (gnus-summary-select-article nil nil nil (car articles))))
8963       (setq to-newsgroup
8964             (gnus-read-move-group-name
8965              (cadr (assq action names))
8966              (symbol-value (intern (format "gnus-current-%s-group" action)))
8967              articles prefix))
8968       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8969     (setq to-method (or select-method
8970                         (gnus-server-to-method
8971                          (gnus-group-method to-newsgroup))))
8972     ;; Check the method we are to move this article to...
8973     (unless (gnus-check-backend-function
8974              'request-accept-article (car to-method))
8975       (error "%s does not support article copying" (car to-method)))
8976     (unless (gnus-check-server to-method)
8977       (error "Can't open server %s" (car to-method)))
8978     (gnus-message 6 "%s to %s: %s..."
8979                   (caddr (assq action names))
8980                   (or (car select-method) to-newsgroup) articles)
8981     (while articles
8982       (setq article (pop articles))
8983       (setq
8984        art-group
8985        (cond
8986         ;; Move the article.
8987         ((eq action 'move)
8988          ;; Remove this article from future suppression.
8989          (gnus-dup-unsuppress-article article)
8990          (gnus-request-move-article
8991           article                       ; Article to move
8992           gnus-newsgroup-name           ; From newsgroup
8993           (nth 1 (gnus-find-method-for-group
8994                   gnus-newsgroup-name)) ; Server
8995           (list 'gnus-request-accept-article
8996                 to-newsgroup (list 'quote select-method)
8997                 (not articles) t)       ; Accept form
8998           (not articles)))              ; Only save nov last time
8999         ;; Copy the article.
9000         ((eq action 'copy)
9001          (save-excursion
9002            (set-buffer copy-buf)
9003            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9004              (gnus-request-accept-article
9005               to-newsgroup select-method (not articles) t))))
9006         ;; Crosspost the article.
9007         ((eq action 'crosspost)
9008          (let ((xref (message-tokenize-header
9009                       (mail-header-xref (gnus-summary-article-header article))
9010                       " ")))
9011            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9012                                   ":" (number-to-string article)))
9013            (unless xref
9014              (setq xref (list (system-name))))
9015            (setq new-xref
9016                  (concat
9017                   (mapconcat 'identity
9018                              (delete "Xref:" (delete new-xref xref))
9019                              " ")
9020                   " " new-xref))
9021            (save-excursion
9022              (set-buffer copy-buf)
9023              ;; First put the article in the destination group.
9024              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9025              (when (consp (setq art-group
9026                                 (gnus-request-accept-article
9027                                  to-newsgroup select-method (not articles))))
9028                (setq new-xref (concat new-xref " " (car art-group)
9029                                       ":"
9030                                       (number-to-string (cdr art-group))))
9031                ;; Now we have the new Xrefs header, so we insert
9032                ;; it and replace the new article.
9033                (nnheader-replace-header "Xref" new-xref)
9034                (gnus-request-replace-article
9035                 (cdr art-group) to-newsgroup (current-buffer))
9036                art-group))))))
9037       (cond
9038        ((not art-group)
9039         (gnus-message 1 "Couldn't %s article %s: %s"
9040                       (cadr (assq action names)) article
9041                       (nnheader-get-report (car to-method))))
9042        ((eq art-group 'junk)
9043         (when (eq action 'move)
9044           (gnus-summary-mark-article article gnus-canceled-mark)
9045           (gnus-message 4 "Deleted article %s" article)
9046           ;; run the delete hook
9047           (run-hook-with-args 'gnus-summary-article-delete-hook
9048                               action
9049                               (gnus-data-header
9050                                (assoc article (gnus-data-list nil)))
9051                               gnus-newsgroup-name nil
9052                               select-method)))
9053        (t
9054         (let* ((pto-group (gnus-group-prefixed-name
9055                            (car art-group) to-method))
9056                (info (gnus-get-info pto-group))
9057                (to-group (gnus-info-group info))
9058                to-marks)
9059           ;; Update the group that has been moved to.
9060           (when (and info
9061                      (memq action '(move copy)))
9062             (unless (member to-group to-groups)
9063               (push to-group to-groups))
9064
9065             (unless (memq article gnus-newsgroup-unreads)
9066               (push 'read to-marks)
9067               (gnus-info-set-read
9068                info (gnus-add-to-range (gnus-info-read info)
9069                                        (list (cdr art-group)))))
9070
9071             ;; See whether the article is to be put in the cache.
9072             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9073                              gnus-article-mark-lists
9074                            (delete '(expirable . expire)
9075                                    (copy-sequence gnus-article-mark-lists))))
9076                   (to-article (cdr art-group)))
9077
9078               ;; Enter the article into the cache in the new group,
9079               ;; if that is required.
9080               (when gnus-use-cache
9081                 (gnus-cache-possibly-enter-article
9082                  to-group to-article
9083                  (memq article gnus-newsgroup-marked)
9084                  (memq article gnus-newsgroup-dormant)
9085                  (memq article gnus-newsgroup-unreads)))
9086
9087               (when gnus-preserve-marks
9088                 ;; Copy any marks over to the new group.
9089                 (when (and (equal to-group gnus-newsgroup-name)
9090                            (not (memq article gnus-newsgroup-unreads)))
9091                   ;; Mark this article as read in this group.
9092                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9093                   (setcdr (gnus-active to-group) to-article)
9094                   (setcdr gnus-newsgroup-active to-article))
9095
9096                 (while marks
9097                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9098                     (when (memq article (symbol-value
9099                                          (intern (format "gnus-newsgroup-%s"
9100                                                          (caar marks)))))
9101                       (push (cdar marks) to-marks)
9102                       ;; If the other group is the same as this group,
9103                       ;; then we have to add the mark to the list.
9104                       (when (equal to-group gnus-newsgroup-name)
9105                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9106                              (cons to-article
9107                                    (symbol-value
9108                                     (intern (format "gnus-newsgroup-%s"
9109                                                     (caar marks)))))))
9110                       ;; Copy the marks to other group.
9111                       (gnus-add-marked-articles
9112                        to-group (cdar marks) (list to-article) info)))
9113                   (setq marks (cdr marks)))
9114
9115                 (gnus-request-set-mark
9116                  to-group (list (list (list to-article) 'add to-marks))))
9117
9118               (gnus-dribble-enter
9119                (concat "(gnus-group-set-info '"
9120                        (gnus-prin1-to-string (gnus-get-info to-group))
9121                        ")"))))
9122
9123           ;; Update the Xref header in this article to point to
9124           ;; the new crossposted article we have just created.
9125           (when (eq action 'crosspost)
9126             (save-excursion
9127               (set-buffer copy-buf)
9128               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9129               (nnheader-replace-header "Xref" new-xref)
9130               (gnus-request-replace-article
9131                article gnus-newsgroup-name (current-buffer))))
9132
9133           ;; run the move/copy/crosspost/respool hook
9134           (run-hook-with-args 'gnus-summary-article-move-hook
9135                               action
9136                               (gnus-data-header
9137                                (assoc article (gnus-data-list nil)))
9138                               gnus-newsgroup-name
9139                               to-newsgroup
9140                               select-method))
9141
9142         ;;;!!!Why is this necessary?
9143         (set-buffer gnus-summary-buffer)
9144         
9145         (gnus-summary-goto-subject article)
9146         (when (eq action 'move)
9147           (gnus-summary-mark-article article gnus-canceled-mark))))
9148       (gnus-summary-remove-process-mark article))
9149     ;; Re-activate all groups that have been moved to.
9150     (save-excursion
9151       (set-buffer gnus-group-buffer)
9152       (let ((gnus-group-marked to-groups))
9153         (gnus-group-get-new-news-this-group nil t)))
9154
9155     (gnus-kill-buffer copy-buf)
9156     (gnus-summary-position-point)
9157     (gnus-set-mode-line 'summary)))
9158
9159 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9160   "Move the current article to a different newsgroup.
9161 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9162 When called interactively, if TO-NEWSGROUP is nil, use the value of
9163 the variable `gnus-move-split-methods' for finding a default target
9164 newsgroup.
9165 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9166 re-spool using this method."
9167   (interactive "P")
9168   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9169
9170 (defun gnus-summary-crosspost-article (&optional n)
9171   "Crosspost the current article to some other group."
9172   (interactive "P")
9173   (gnus-summary-move-article n nil nil 'crosspost))
9174
9175 (defcustom gnus-summary-respool-default-method nil
9176   "Default method type for respooling an article.
9177 If nil, use to the current newsgroup method."
9178   :type 'symbol
9179   :group 'gnus-summary-mail)
9180
9181 (defcustom gnus-summary-display-while-building nil
9182   "If non-nil, show and update the summary buffer as it's being built.
9183 If the value is t, update the buffer after every line is inserted.  If
9184 the value is an integer (N), update the display every N lines."
9185   :group 'gnus-thread
9186   :type '(choice (const :tag "off" nil)
9187                  number
9188                  (const :tag "frequently" t)))
9189
9190 (defun gnus-summary-respool-article (&optional n method)
9191   "Respool the current article.
9192 The article will be squeezed through the mail spooling process again,
9193 which means that it will be put in some mail newsgroup or other
9194 depending on `nnmail-split-methods'.
9195 If N is a positive number, respool the N next articles.
9196 If N is a negative number, respool the N previous articles.
9197 If N is nil and any articles have been marked with the process mark,
9198 respool those articles instead.
9199
9200 Respooling can be done both from mail groups and \"real\" newsgroups.
9201 In the former case, the articles in question will be moved from the
9202 current group into whatever groups they are destined to.  In the
9203 latter case, they will be copied into the relevant groups."
9204   (interactive
9205    (list current-prefix-arg
9206          (let* ((methods (gnus-methods-using 'respool))
9207                 (methname
9208                  (symbol-name (or gnus-summary-respool-default-method
9209                                   (car (gnus-find-method-for-group
9210                                         gnus-newsgroup-name)))))
9211                 (method
9212                  (gnus-completing-read-with-default
9213                   methname "What backend do you want to use when respooling?"
9214                   methods nil t nil 'gnus-mail-method-history))
9215                 ms)
9216            (cond
9217             ((zerop (length (setq ms (gnus-servers-using-backend
9218                                       (intern method)))))
9219              (list (intern method) ""))
9220             ((= 1 (length ms))
9221              (car ms))
9222             (t
9223              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9224                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9225                            ms-alist))))))))
9226   (unless method
9227     (error "No method given for respooling"))
9228   (if (assoc (symbol-name
9229               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9230              (gnus-methods-using 'respool))
9231       (gnus-summary-move-article n nil method)
9232     (gnus-summary-copy-article n nil method)))
9233
9234 (defun gnus-summary-import-article (file &optional edit)
9235   "Import an arbitrary file into a mail newsgroup."
9236   (interactive "fImport file: \nP")
9237   (let ((group gnus-newsgroup-name)
9238         (now (current-time))
9239         atts lines group-art)
9240     (unless (gnus-check-backend-function 'request-accept-article group)
9241       (error "%s does not support article importing" group))
9242     (or (file-readable-p file)
9243         (not (file-regular-p file))
9244         (error "Can't read %s" file))
9245     (save-excursion
9246       (set-buffer (gnus-get-buffer-create " *import file*"))
9247       (erase-buffer)
9248       (nnheader-insert-file-contents file)
9249       (goto-char (point-min))
9250       (if (nnheader-article-p)
9251           (save-restriction
9252             (goto-char (point-min))
9253             (search-forward "\n\n" nil t)
9254             (narrow-to-region (point-min) (1- (point)))
9255             (goto-char (point-min))
9256             (unless (re-search-forward "^date:" nil t)
9257               (goto-char (point-max))
9258               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9259        ;; This doesn't look like an article, so we fudge some headers.
9260         (setq atts (file-attributes file)
9261               lines (count-lines (point-min) (point-max)))
9262         (insert "From: " (read-string "From: ") "\n"
9263                 "Subject: " (read-string "Subject: ") "\n"
9264                 "Date: " (message-make-date (nth 5 atts)) "\n"
9265                 "Message-ID: " (message-make-message-id) "\n"
9266                 "Lines: " (int-to-string lines) "\n"
9267                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9268       (setq group-art (gnus-request-accept-article group nil t))
9269       (kill-buffer (current-buffer)))
9270     (setq gnus-newsgroup-active (gnus-activate-group group))
9271     (forward-line 1)
9272     (gnus-summary-goto-article (cdr group-art) nil t)
9273     (when edit
9274       (gnus-summary-edit-article))))
9275
9276 (defun gnus-summary-create-article ()
9277   "Create an article in a mail newsgroup."
9278   (interactive)
9279   (let ((group gnus-newsgroup-name)
9280         (now (current-time))
9281         group-art)
9282     (unless (gnus-check-backend-function 'request-accept-article group)
9283       (error "%s does not support article importing" group))
9284     (save-excursion
9285       (set-buffer (gnus-get-buffer-create " *import file*"))
9286       (erase-buffer)
9287       (goto-char (point-min))
9288       ;; This doesn't look like an article, so we fudge some headers.
9289       (insert "From: " (read-string "From: ") "\n"
9290               "Subject: " (read-string "Subject: ") "\n"
9291               "Date: " (message-make-date now) "\n"
9292               "Message-ID: " (message-make-message-id) "\n")
9293       (setq group-art (gnus-request-accept-article group nil t))
9294       (kill-buffer (current-buffer)))
9295     (setq gnus-newsgroup-active (gnus-activate-group group))
9296     (forward-line 1)
9297     (gnus-summary-goto-article (cdr group-art) nil t)
9298     (gnus-summary-edit-article)))
9299
9300 (defun gnus-summary-article-posted-p ()
9301   "Say whether the current (mail) article is available from news as well.
9302 This will be the case if the article has both been mailed and posted."
9303   (interactive)
9304   (let ((id (mail-header-references (gnus-summary-article-header)))
9305         (gnus-override-method (car (gnus-refer-article-methods))))
9306     (if (gnus-request-head id "")
9307         (gnus-message 2 "The current message was found on %s"
9308                       gnus-override-method)
9309       (gnus-message 2 "The current message couldn't be found on %s"
9310                     gnus-override-method)
9311       nil)))
9312
9313 (defun gnus-summary-expire-articles (&optional now)
9314   "Expire all articles that are marked as expirable in the current group."
9315   (interactive)
9316   (when (and (not gnus-group-is-exiting-without-update-p)
9317              (gnus-check-backend-function
9318               'request-expire-articles gnus-newsgroup-name))
9319     ;; This backend supports expiry.
9320     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9321            (expirable (if total
9322                           (progn
9323                             ;; We need to update the info for
9324                             ;; this group for `gnus-list-of-read-articles'
9325                             ;; to give us the right answer.
9326                             (gnus-run-hooks 'gnus-exit-group-hook)
9327                             (gnus-summary-update-info)
9328                             (gnus-list-of-read-articles gnus-newsgroup-name))
9329                         (setq gnus-newsgroup-expirable
9330                               (sort gnus-newsgroup-expirable '<))))
9331            (expiry-wait (if now 'immediate
9332                           (gnus-group-find-parameter
9333                            gnus-newsgroup-name 'expiry-wait)))
9334            (nnmail-expiry-target
9335             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9336                 nnmail-expiry-target))
9337            es)
9338       (when expirable
9339         ;; There are expirable articles in this group, so we run them
9340         ;; through the expiry process.
9341         (gnus-message 6 "Expiring articles...")
9342         (unless (gnus-check-group gnus-newsgroup-name)
9343           (error "Can't open server for %s" gnus-newsgroup-name))
9344         ;; The list of articles that weren't expired is returned.
9345         (save-excursion
9346           (if expiry-wait
9347               (let ((nnmail-expiry-wait-function nil)
9348                     (nnmail-expiry-wait expiry-wait))
9349                 (setq es (gnus-request-expire-articles
9350                           expirable gnus-newsgroup-name)))
9351             (setq es (gnus-request-expire-articles
9352                       expirable gnus-newsgroup-name)))
9353           (unless total
9354             (setq gnus-newsgroup-expirable es))
9355           ;; We go through the old list of expirable, and mark all
9356           ;; really expired articles as nonexistent.
9357           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9358             (let ((gnus-use-cache nil))
9359               (dolist (article expirable)
9360                 (when (and (not (memq article es))
9361                            (gnus-data-find article))
9362                   (gnus-summary-mark-article article gnus-canceled-mark)
9363                   (run-hook-with-args 'gnus-summary-article-expire-hook
9364                                       'delete
9365                                       (gnus-data-header
9366                                        (assoc article (gnus-data-list nil)))
9367                                       gnus-newsgroup-name
9368                                       nil
9369                                       nil))))))
9370         (gnus-message 6 "Expiring articles...done")))))
9371
9372 (defun gnus-summary-expire-articles-now ()
9373   "Expunge all expirable articles in the current group.
9374 This means that *all* articles that are marked as expirable will be
9375 deleted forever, right now."
9376   (interactive)
9377   (or gnus-expert-user
9378       (gnus-yes-or-no-p
9379        "Are you really, really, really sure you want to delete all these messages? ")
9380       (error "Phew!"))
9381   (gnus-summary-expire-articles t))
9382
9383 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9384 (defun gnus-summary-delete-article (&optional n)
9385   "Delete the N next (mail) articles.
9386 This command actually deletes articles.  This is not a marking
9387 command.  The article will disappear forever from your life, never to
9388 return.
9389
9390 If N is negative, delete backwards.
9391 If N is nil and articles have been marked with the process mark,
9392 delete these instead.
9393
9394 If `gnus-novice-user' is non-nil you will be asked for
9395 confirmation before the articles are deleted."
9396   (interactive "P")
9397   (unless (gnus-check-backend-function 'request-expire-articles
9398                                        gnus-newsgroup-name)
9399     (error "The current newsgroup does not support article deletion"))
9400   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9401     (error "Couldn't open server"))
9402   ;; Compute the list of articles to delete.
9403   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9404         (nnmail-expiry-target 'delete)
9405         not-deleted)
9406     (if (and gnus-novice-user
9407              (not (gnus-yes-or-no-p
9408                    (format "Do you really want to delete %s forever? "
9409                            (if (> (length articles) 1)
9410                                (format "these %s articles" (length articles))
9411                              "this article")))))
9412         ()
9413       ;; Delete the articles.
9414       (setq not-deleted (gnus-request-expire-articles
9415                          articles gnus-newsgroup-name 'force))
9416       (while articles
9417         (gnus-summary-remove-process-mark (car articles))
9418         ;; The backend might not have been able to delete the article
9419         ;; after all.
9420         (unless (memq (car articles) not-deleted)
9421           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9422         (let* ((article (car articles))
9423                (ghead  (gnus-data-header
9424                                     (assoc article (gnus-data-list nil)))))
9425           (run-hook-with-args 'gnus-summary-article-delete-hook
9426                               'delete ghead gnus-newsgroup-name nil
9427                               nil))
9428         (setq articles (cdr articles)))
9429       (when not-deleted
9430         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9431     (gnus-summary-position-point)
9432     (gnus-set-mode-line 'summary)
9433     not-deleted))
9434
9435 (defun gnus-summary-edit-article (&optional arg)
9436   "Edit the current article.
9437 This will have permanent effect only in mail groups.
9438 If ARG is nil, edit the decoded articles.
9439 If ARG is 1, edit the raw articles.
9440 If ARG is 2, edit the raw articles even in read-only groups.
9441 If ARG is 3, edit the articles with the current handles.
9442 Otherwise, allow editing of articles even in read-only
9443 groups."
9444   (interactive "P")
9445   (let (force raw current-handles)
9446     (cond
9447      ((null arg))
9448      ((eq arg 1)
9449       (setq raw t))
9450      ((eq arg 2)
9451       (setq raw t
9452             force t))
9453      ((eq arg 3)
9454       (setq current-handles
9455             (and (gnus-buffer-live-p gnus-article-buffer)
9456                  (with-current-buffer gnus-article-buffer
9457                    (prog1
9458                        gnus-article-mime-handles
9459                      (setq gnus-article-mime-handles nil))))))
9460      (t
9461       (setq force t)))
9462     (when (and raw (not force)
9463                (member gnus-newsgroup-name '("nndraft:delayed"
9464                                              "nndraft:drafts"
9465                                              "nndraft:queue")))
9466       (error "Can't edit the raw article in group %s"
9467              gnus-newsgroup-name))
9468     (save-excursion
9469       (set-buffer gnus-summary-buffer)
9470       (let ((mail-parse-charset gnus-newsgroup-charset)
9471             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9472         (gnus-set-global-variables)
9473         (when (and (not force)
9474                    (gnus-group-read-only-p))
9475           (error "The current newsgroup does not support article editing"))
9476         (gnus-summary-show-article t)
9477         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9478           (with-current-buffer gnus-article-buffer
9479             (mm-enable-multibyte)))
9480         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9481             (setq raw t))
9482         (gnus-article-edit-article
9483          (if raw 'ignore
9484            `(lambda ()
9485               (let ((mbl mml-buffer-list))
9486                 (setq mml-buffer-list nil)
9487                 (mime-to-mml ,'current-handles)
9488                 (let ((mbl1 mml-buffer-list))
9489                   (setq mml-buffer-list mbl)
9490                   (set (make-local-variable 'mml-buffer-list) mbl1))
9491                 (gnus-make-local-hook 'kill-buffer-hook)
9492                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9493          `(lambda (no-highlight)
9494             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9495                   (message-options message-options)
9496                   (message-options-set-recipient)
9497                   (mail-parse-ignored-charsets
9498                    ',gnus-newsgroup-ignored-charsets))
9499               ,(if (not raw) '(progn
9500                                 (mml-to-mime)
9501                                 (mml-destroy-buffers)
9502                                 (remove-hook 'kill-buffer-hook
9503                                              'mml-destroy-buffers t)
9504                                 (kill-local-variable 'mml-buffer-list)))
9505               (gnus-summary-edit-article-done
9506                ,(or (mail-header-references gnus-current-headers) "")
9507                ,(gnus-group-read-only-p)
9508                ,gnus-summary-buffer no-highlight))))))))
9509
9510 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9511
9512 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9513                                                  no-highlight)
9514   "Make edits to the current article permanent."
9515   (interactive)
9516   (save-excursion
9517    ;; The buffer restriction contains the entire article if it exists.
9518     (when (article-goto-body)
9519       (let ((lines (count-lines (point) (point-max)))
9520             (length (- (point-max) (point)))
9521             (case-fold-search t)
9522             (body (copy-marker (point))))
9523         (goto-char (point-min))
9524         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9525           (delete-region (match-beginning 1) (match-end 1))
9526           (insert (number-to-string length)))
9527         (goto-char (point-min))
9528         (when (re-search-forward
9529                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9530           (delete-region (match-beginning 1) (match-end 1))
9531           (insert (number-to-string length)))
9532         (goto-char (point-min))
9533         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9534           (delete-region (match-beginning 1) (match-end 1))
9535           (insert (number-to-string lines))))))
9536   ;; Replace the article.
9537   (let ((buf (current-buffer)))
9538     (with-temp-buffer
9539       (insert-buffer-substring buf)
9540
9541       (if (and (not read-only)
9542                (not (gnus-request-replace-article
9543                      (cdr gnus-article-current) (car gnus-article-current)
9544                      (current-buffer) t)))
9545           (error "Couldn't replace article")
9546         ;; Update the summary buffer.
9547         (if (and references
9548                  (equal (message-tokenize-header references " ")
9549                         (message-tokenize-header
9550                          (or (message-fetch-field "references") "") " ")))
9551             ;; We only have to update this line.
9552             (save-excursion
9553               (save-restriction
9554                 (message-narrow-to-head)
9555                 (let ((head (buffer-string))
9556                       header)
9557                   (with-temp-buffer
9558                     (insert (format "211 %d Article retrieved.\n"
9559                                     (cdr gnus-article-current)))
9560                     (insert head)
9561                     (insert ".\n")
9562                     (let ((nntp-server-buffer (current-buffer)))
9563                       (setq header (car (gnus-get-newsgroup-headers
9564                                          nil t))))
9565                     (save-excursion
9566                       (set-buffer gnus-summary-buffer)
9567                       (gnus-data-set-header
9568                        (gnus-data-find (cdr gnus-article-current))
9569                        header)
9570                       (gnus-summary-update-article-line
9571                        (cdr gnus-article-current) header)
9572                       (if (gnus-summary-goto-subject
9573                            (cdr gnus-article-current) nil t)
9574                           (gnus-summary-update-secondary-mark
9575                            (cdr gnus-article-current))))))))
9576           ;; Update threads.
9577           (set-buffer (or buffer gnus-summary-buffer))
9578           (gnus-summary-update-article (cdr gnus-article-current))
9579           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9580               (gnus-summary-update-secondary-mark
9581                (cdr gnus-article-current))))
9582         ;; Prettify the article buffer again.
9583         (unless no-highlight
9584           (save-excursion
9585             (set-buffer gnus-article-buffer)
9586             ;;;!!! Fix this -- article should be rehighlighted.
9587             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9588             (set-buffer gnus-original-article-buffer)
9589             (gnus-request-article
9590              (cdr gnus-article-current)
9591              (car gnus-article-current) (current-buffer))))
9592         ;; Prettify the summary buffer line.
9593         (when (gnus-visual-p 'summary-highlight 'highlight)
9594           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9595
9596 (defun gnus-summary-edit-wash (key)
9597   "Perform editing command KEY in the article buffer."
9598   (interactive
9599    (list
9600     (progn
9601       (message "%s" (concat (this-command-keys) "- "))
9602       (read-char))))
9603   (message "")
9604   (gnus-summary-edit-article)
9605   (execute-kbd-macro (concat (this-command-keys) key))
9606   (gnus-article-edit-done))
9607
9608 ;;; Respooling
9609
9610 (defun gnus-summary-respool-query (&optional silent trace)
9611   "Query where the respool algorithm would put this article."
9612   (interactive)
9613   (let (gnus-mark-article-hook)
9614     (gnus-summary-select-article)
9615     (save-excursion
9616       (set-buffer gnus-original-article-buffer)
9617       (let ((groups (nnmail-article-group 'identity trace)))
9618         (unless silent
9619           (if groups
9620               (message "This message would go to %s"
9621                        (mapconcat 'car groups ", "))
9622             (message "This message would go to no groups"))
9623           groups)))))
9624
9625 (defun gnus-summary-respool-trace ()
9626   "Trace where the respool algorithm would put this article.
9627 Display a buffer showing all fancy splitting patterns which matched."
9628   (interactive)
9629   (gnus-summary-respool-query nil t))
9630
9631 ;; Summary marking commands.
9632
9633 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9634   "Mark articles which has the same subject as read, and then select the next.
9635 If UNMARK is positive, remove any kind of mark.
9636 If UNMARK is negative, tick articles."
9637   (interactive "P")
9638   (when unmark
9639     (setq unmark (prefix-numeric-value unmark)))
9640   (let ((count
9641          (gnus-summary-mark-same-subject
9642           (gnus-summary-article-subject) unmark)))
9643     ;; Select next unread article.  If auto-select-same mode, should
9644     ;; select the first unread article.
9645     (gnus-summary-next-article t (and gnus-auto-select-same
9646                                       (gnus-summary-article-subject)))
9647     (gnus-message 7 "%d article%s marked as %s"
9648                   count (if (= count 1) " is" "s are")
9649                   (if unmark "unread" "read"))))
9650
9651 (defun gnus-summary-kill-same-subject (&optional unmark)
9652   "Mark articles which has the same subject as read.
9653 If UNMARK is positive, remove any kind of mark.
9654 If UNMARK is negative, tick articles."
9655   (interactive "P")
9656   (when unmark
9657     (setq unmark (prefix-numeric-value unmark)))
9658   (let ((count
9659          (gnus-summary-mark-same-subject
9660           (gnus-summary-article-subject) unmark)))
9661     ;; If marked as read, go to next unread subject.
9662     (when (null unmark)
9663       ;; Go to next unread subject.
9664       (gnus-summary-next-subject 1 t))
9665     (gnus-message 7 "%d articles are marked as %s"
9666                   count (if unmark "unread" "read"))))
9667
9668 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9669   "Mark articles with same SUBJECT as read, and return marked number.
9670 If optional argument UNMARK is positive, remove any kinds of marks.
9671 If optional argument UNMARK is negative, mark articles as unread instead."
9672   (let ((count 1))
9673     (save-excursion
9674       (cond
9675        ((null unmark)                   ; Mark as read.
9676         (while (and
9677                 (progn
9678                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9679                   (gnus-summary-show-thread) t)
9680                 (gnus-summary-find-subject subject))
9681           (setq count (1+ count))))
9682        ((> unmark 0)                    ; Tick.
9683         (while (and
9684                 (progn
9685                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9686                   (gnus-summary-show-thread) t)
9687                 (gnus-summary-find-subject subject))
9688           (setq count (1+ count))))
9689        (t                               ; Mark as unread.
9690         (while (and
9691                 (progn
9692                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9693                   (gnus-summary-show-thread) t)
9694                 (gnus-summary-find-subject subject))
9695           (setq count (1+ count)))))
9696       (gnus-set-mode-line 'summary)
9697       ;; Return the number of marked articles.
9698       count)))
9699
9700 (defun gnus-summary-mark-as-processable (n &optional unmark)
9701   "Set the process mark on the next N articles.
9702 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9703 the process mark instead.  The difference between N and the actual
9704 number of articles marked is returned."
9705   (interactive "P")
9706   (if (and (null n) (gnus-region-active-p))
9707       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9708     (setq n (prefix-numeric-value n))
9709     (let ((backward (< n 0))
9710           (n (abs n)))
9711       (while (and
9712               (> n 0)
9713               (if unmark
9714                   (gnus-summary-remove-process-mark
9715                    (gnus-summary-article-number))
9716                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9717               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9718         (setq n (1- n)))
9719       (when (/= 0 n)
9720         (gnus-message 7 "No more articles"))
9721       (gnus-summary-recenter)
9722       (gnus-summary-position-point)
9723       n)))
9724
9725 (defun gnus-summary-unmark-as-processable (n)
9726   "Remove the process mark from the next N articles.
9727 If N is negative, unmark backward instead.  The difference between N and
9728 the actual number of articles unmarked is returned."
9729   (interactive "P")
9730   (gnus-summary-mark-as-processable n t))
9731
9732 (defun gnus-summary-unmark-all-processable ()
9733   "Remove the process mark from all articles."
9734   (interactive)
9735   (save-excursion
9736     (while gnus-newsgroup-processable
9737       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9738   (gnus-summary-position-point))
9739
9740 (defun gnus-summary-add-mark (article type)
9741   "Mark ARTICLE with a mark of TYPE."
9742   (let ((vtype (car (assq type gnus-article-mark-lists)))
9743         var)
9744     (if (not vtype)
9745         (error "No such mark type: %s" type)
9746       (setq var (intern (format "gnus-newsgroup-%s" type)))
9747       (set var (cons article (symbol-value var)))
9748       (if (memq type '(processable cached replied forwarded recent saved))
9749           (gnus-summary-update-secondary-mark article)
9750         ;;; !!! This is bogus.  We should find out what primary
9751         ;;; !!! mark we want to set.
9752         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9753
9754 (defun gnus-summary-mark-as-expirable (n)
9755   "Mark N articles forward as expirable.
9756 If N is negative, mark backward instead.  The difference between N and
9757 the actual number of articles marked is returned."
9758   (interactive "p")
9759   (gnus-summary-mark-forward n gnus-expirable-mark))
9760
9761 (defun gnus-summary-mark-as-spam (n)
9762   "Mark N articles forward as spam.
9763 If N is negative, mark backward instead.  The difference between N and
9764 the actual number of articles marked is returned."
9765   (interactive "p")
9766   (gnus-summary-mark-forward n gnus-spam-mark))
9767
9768 (defun gnus-summary-mark-article-as-replied (article)
9769   "Mark ARTICLE as replied to and update the summary line.
9770 ARTICLE can also be a list of articles."
9771   (interactive (list (gnus-summary-article-number)))
9772   (let ((articles (if (listp article) article (list article))))
9773     (dolist (article articles)
9774       (unless (numberp article)
9775         (error "%s is not a number" article))
9776       (push article gnus-newsgroup-replied)
9777       (let ((buffer-read-only nil))
9778         (when (gnus-summary-goto-subject article nil t)
9779           (gnus-summary-update-secondary-mark article))))))
9780
9781 (defun gnus-summary-mark-article-as-forwarded (article)
9782   "Mark ARTICLE as forwarded and update the summary line.
9783 ARTICLE can also be a list of articles."
9784   (let ((articles (if (listp article) article (list article))))
9785     (dolist (article articles)
9786       (push article gnus-newsgroup-forwarded)
9787       (let ((buffer-read-only nil))
9788         (when (gnus-summary-goto-subject article nil t)
9789           (gnus-summary-update-secondary-mark article))))))
9790
9791 (defun gnus-summary-set-bookmark (article)
9792   "Set a bookmark in current article."
9793   (interactive (list (gnus-summary-article-number)))
9794   (when (or (not (get-buffer gnus-article-buffer))
9795             (not gnus-current-article)
9796             (not gnus-article-current)
9797             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9798     (error "No current article selected"))
9799   ;; Remove old bookmark, if one exists.
9800   (gnus-pull article gnus-newsgroup-bookmarks)
9801   ;; Set the new bookmark, which is on the form
9802   ;; (article-number . line-number-in-body).
9803   (push
9804    (cons article
9805          (with-current-buffer gnus-article-buffer
9806            (count-lines
9807             (min (point)
9808                  (save-excursion
9809                    (article-goto-body)
9810                    (point)))
9811             (point))))
9812    gnus-newsgroup-bookmarks)
9813   (gnus-message 6 "A bookmark has been added to the current article."))
9814
9815 (defun gnus-summary-remove-bookmark (article)
9816   "Remove the bookmark from the current article."
9817   (interactive (list (gnus-summary-article-number)))
9818   ;; Remove old bookmark, if one exists.
9819   (if (not (assq article gnus-newsgroup-bookmarks))
9820       (gnus-message 6 "No bookmark in current article.")
9821     (gnus-pull article gnus-newsgroup-bookmarks)
9822     (gnus-message 6 "Removed bookmark.")))
9823
9824 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9825 (defun gnus-summary-mark-as-dormant (n)
9826   "Mark N articles forward as dormant.
9827 If N is negative, mark backward instead.  The difference between N and
9828 the actual number of articles marked is returned."
9829   (interactive "p")
9830   (gnus-summary-mark-forward n gnus-dormant-mark))
9831
9832 (defun gnus-summary-set-process-mark (article)
9833   "Set the process mark on ARTICLE and update the summary line."
9834   (setq gnus-newsgroup-processable
9835         (cons article
9836               (delq article gnus-newsgroup-processable)))
9837   (when (gnus-summary-goto-subject article)
9838     (gnus-summary-show-thread)
9839     (gnus-summary-goto-subject article)
9840     (gnus-summary-update-secondary-mark article)))
9841
9842 (defun gnus-summary-remove-process-mark (article)
9843   "Remove the process mark from ARTICLE and update the summary line."
9844   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9845   (when (gnus-summary-goto-subject article)
9846     (gnus-summary-show-thread)
9847     (gnus-summary-goto-subject article)
9848     (gnus-summary-update-secondary-mark article)))
9849
9850 (defun gnus-summary-set-saved-mark (article)
9851   "Set the process mark on ARTICLE and update the summary line."
9852   (push article gnus-newsgroup-saved)
9853   (when (gnus-summary-goto-subject article)
9854     (gnus-summary-update-secondary-mark article)))
9855
9856 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9857   "Mark N articles as read forwards.
9858 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9859 The difference between N and the actual number of articles marked is
9860 returned.
9861 If NO-EXPIRE, auto-expiry will be inhibited."
9862   (interactive "p")
9863   (gnus-summary-show-thread)
9864   (let ((backward (< n 0))
9865         (gnus-summary-goto-unread
9866          (and gnus-summary-goto-unread
9867               (not (eq gnus-summary-goto-unread 'never))
9868               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9869                                     gnus-ticked-mark gnus-dormant-mark)))))
9870         (n (abs n))
9871         (mark (or mark gnus-del-mark)))
9872     (while (and (> n 0)
9873                 (gnus-summary-mark-article nil mark no-expire)
9874                 (zerop (gnus-summary-next-subject
9875                         (if backward -1 1)
9876                         (and gnus-summary-goto-unread
9877                              (not (eq gnus-summary-goto-unread 'never)))
9878                         t)))
9879       (setq n (1- n)))
9880     (when (/= 0 n)
9881       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9882     (gnus-summary-recenter)
9883     (gnus-summary-position-point)
9884     (gnus-set-mode-line 'summary)
9885     n))
9886
9887 (defun gnus-summary-mark-article-as-read (mark)
9888   "Mark the current article quickly as read with MARK."
9889   (let ((article (gnus-summary-article-number)))
9890     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9891     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9892     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9893     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9894     (push (cons article mark) gnus-newsgroup-reads)
9895     ;; Possibly remove from cache, if that is used.
9896     (when gnus-use-cache
9897       (gnus-cache-enter-remove-article article))
9898     ;; Allow the backend to change the mark.
9899     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9900     ;; Check for auto-expiry.
9901     (when (and gnus-newsgroup-auto-expire
9902                (memq mark gnus-auto-expirable-marks))
9903       (setq mark gnus-expirable-mark)
9904       ;; Let the backend know about the mark change.
9905       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9906       (push article gnus-newsgroup-expirable))
9907     ;; Set the mark in the buffer.
9908     (gnus-summary-update-mark mark 'unread)
9909     t))
9910
9911 (defun gnus-summary-mark-article-as-unread (mark)
9912   "Mark the current article quickly as unread with MARK."
9913   (let* ((article (gnus-summary-article-number))
9914          (old-mark (gnus-summary-article-mark article)))
9915     ;; Allow the backend to change the mark.
9916     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9917     (if (eq mark old-mark)
9918         t
9919       (if (<= article 0)
9920           (progn
9921             (gnus-error 1 "Can't mark negative article numbers")
9922             nil)
9923         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9924         (setq gnus-newsgroup-spam-marked
9925               (delq article gnus-newsgroup-spam-marked))
9926         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9927         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9928         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9929         (cond ((= mark gnus-ticked-mark)
9930                (setq gnus-newsgroup-marked
9931                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9932                                               article)))
9933               ((= mark gnus-spam-mark)
9934                (setq gnus-newsgroup-spam-marked
9935                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9936                                               article)))
9937               ((= mark gnus-dormant-mark)
9938                (setq gnus-newsgroup-dormant
9939                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9940                                               article)))
9941               (t
9942                (setq gnus-newsgroup-unreads
9943                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9944                                               article))))
9945         (gnus-pull article gnus-newsgroup-reads)
9946
9947         ;; See whether the article is to be put in the cache.
9948         (and gnus-use-cache
9949              (vectorp (gnus-summary-article-header article))
9950              (save-excursion
9951                (gnus-cache-possibly-enter-article
9952                 gnus-newsgroup-name article
9953                 (= mark gnus-ticked-mark)
9954                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9955
9956         ;; Fix the mark.
9957         (gnus-summary-update-mark mark 'unread)
9958         t))))
9959
9960 (defun gnus-summary-mark-article (&optional article mark no-expire)
9961   "Mark ARTICLE with MARK.  MARK can be any character.
9962 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9963 `??' (dormant) and `?E' (expirable).
9964 If MARK is nil, then the default character `?r' is used.
9965 If ARTICLE is nil, then the article on the current line will be
9966 marked.
9967 If NO-EXPIRE, auto-expiry will be inhibited."
9968   ;; The mark might be a string.
9969   (when (stringp mark)
9970     (setq mark (aref mark 0)))
9971   ;; If no mark is given, then we check auto-expiring.
9972   (when (null mark)
9973     (setq mark gnus-del-mark))
9974   (when (and (not no-expire)
9975              gnus-newsgroup-auto-expire
9976              (memq mark gnus-auto-expirable-marks))
9977     (setq mark gnus-expirable-mark))
9978   (let ((article (or article (gnus-summary-article-number)))
9979         (old-mark (gnus-summary-article-mark article)))
9980     ;; Allow the backend to change the mark.
9981     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9982     (if (eq mark old-mark)
9983         t
9984       (unless article
9985         (error "No article on current line"))
9986       (if (not (if (or (= mark gnus-unread-mark)
9987                        (= mark gnus-ticked-mark)
9988                        (= mark gnus-spam-mark)
9989                        (= mark gnus-dormant-mark))
9990                    (gnus-mark-article-as-unread article mark)
9991                  (gnus-mark-article-as-read article mark)))
9992           t
9993         ;; See whether the article is to be put in the cache.
9994         (and gnus-use-cache
9995              (not (= mark gnus-canceled-mark))
9996              (vectorp (gnus-summary-article-header article))
9997              (save-excursion
9998                (gnus-cache-possibly-enter-article
9999                 gnus-newsgroup-name article
10000                 (= mark gnus-ticked-mark)
10001                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10002
10003         (when (gnus-summary-goto-subject article nil t)
10004           (let ((buffer-read-only nil))
10005             (gnus-summary-show-thread)
10006             ;; Fix the mark.
10007             (gnus-summary-update-mark mark 'unread)
10008             t))))))
10009
10010 (defun gnus-summary-update-secondary-mark (article)
10011   "Update the secondary (read, process, cache) mark."
10012   (gnus-summary-update-mark
10013    (cond ((memq article gnus-newsgroup-processable)
10014           gnus-process-mark)
10015          ((memq article gnus-newsgroup-cached)
10016           gnus-cached-mark)
10017          ((memq article gnus-newsgroup-replied)
10018           gnus-replied-mark)
10019          ((memq article gnus-newsgroup-forwarded)
10020           gnus-forwarded-mark)
10021          ((memq article gnus-newsgroup-saved)
10022           gnus-saved-mark)
10023          ((memq article gnus-newsgroup-recent)
10024           gnus-recent-mark)
10025          ((memq article gnus-newsgroup-unseen)
10026           gnus-unseen-mark)
10027          (t gnus-no-mark))
10028    'replied)
10029   (when (gnus-visual-p 'summary-highlight 'highlight)
10030     (gnus-run-hooks 'gnus-summary-update-hook))
10031   t)
10032
10033 (defun gnus-summary-update-download-mark (article)
10034   "Update the download mark."
10035   (gnus-summary-update-mark
10036    (cond ((memq article gnus-newsgroup-undownloaded)
10037           gnus-undownloaded-mark)
10038          (gnus-newsgroup-agentized
10039           gnus-downloaded-mark)
10040          (t
10041           gnus-no-mark))
10042    'download)
10043   (gnus-summary-update-line t)
10044   t)
10045
10046 (defun gnus-summary-update-mark (mark type)
10047   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10048         (buffer-read-only nil))
10049     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10050     (when forward
10051       (when (looking-at "\r")
10052         (incf forward))
10053       (when (<= (+ forward (point)) (point-max))
10054         ;; Go to the right position on the line.
10055         (goto-char (+ forward (point)))
10056         ;; Replace the old mark with the new mark.
10057         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10058         ;; Optionally update the marks by some user rule.
10059         (when (eq type 'unread)
10060           (gnus-data-set-mark
10061            (gnus-data-find (gnus-summary-article-number)) mark)
10062           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10063
10064 (defun gnus-mark-article-as-read (article &optional mark)
10065   "Enter ARTICLE in the pertinent lists and remove it from others."
10066   ;; Make the article expirable.
10067   (let ((mark (or mark gnus-del-mark)))
10068     (setq gnus-newsgroup-expirable
10069           (if (= mark gnus-expirable-mark)
10070               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10071             (delq article gnus-newsgroup-expirable)))
10072     ;; Remove from unread and marked lists.
10073     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10074     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10075     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10076     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10077     (push (cons article mark) gnus-newsgroup-reads)
10078     ;; Possibly remove from cache, if that is used.
10079     (when gnus-use-cache
10080       (gnus-cache-enter-remove-article article))
10081     t))
10082
10083 (defun gnus-mark-article-as-unread (article &optional mark)
10084   "Enter ARTICLE in the pertinent lists and remove it from others."
10085   (let ((mark (or mark gnus-ticked-mark)))
10086     (if (<= article 0)
10087         (progn
10088           (gnus-error 1 "Can't mark negative article numbers")
10089           nil)
10090       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10091             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10092             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10093             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10094             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10095
10096       ;; Unsuppress duplicates?
10097       (when gnus-suppress-duplicates
10098         (gnus-dup-unsuppress-article article))
10099
10100       (cond ((= mark gnus-ticked-mark)
10101              (setq gnus-newsgroup-marked
10102                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10103             ((= mark gnus-spam-mark)
10104              (setq gnus-newsgroup-spam-marked
10105                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10106                                             article)))
10107             ((= mark gnus-dormant-mark)
10108              (setq gnus-newsgroup-dormant
10109                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10110             (t
10111              (setq gnus-newsgroup-unreads
10112                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10113       (gnus-pull article gnus-newsgroup-reads)
10114       t)))
10115
10116 (defalias 'gnus-summary-mark-as-unread-forward
10117   'gnus-summary-tick-article-forward)
10118 (make-obsolete 'gnus-summary-mark-as-unread-forward
10119                'gnus-summary-tick-article-forward)
10120 (defun gnus-summary-tick-article-forward (n)
10121   "Tick N articles forwards.
10122 If N is negative, tick backwards instead.
10123 The difference between N and the number of articles ticked is returned."
10124   (interactive "p")
10125   (gnus-summary-mark-forward n gnus-ticked-mark))
10126
10127 (defalias 'gnus-summary-mark-as-unread-backward
10128   'gnus-summary-tick-article-backward)
10129 (make-obsolete 'gnus-summary-mark-as-unread-backward
10130                'gnus-summary-tick-article-backward)
10131 (defun gnus-summary-tick-article-backward (n)
10132   "Tick N articles backwards.
10133 The difference between N and the number of articles ticked is returned."
10134   (interactive "p")
10135   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10136
10137 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10138 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10139 (defun gnus-summary-tick-article (&optional article clear-mark)
10140   "Mark current article as unread.
10141 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10142 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10143   (interactive)
10144   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10145                                        gnus-ticked-mark)))
10146
10147 (defun gnus-summary-mark-as-read-forward (n)
10148   "Mark N articles as read forwards.
10149 If N is negative, mark backwards instead.
10150 The difference between N and the actual number of articles marked is
10151 returned."
10152   (interactive "p")
10153   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10154
10155 (defun gnus-summary-mark-as-read-backward (n)
10156   "Mark the N articles as read backwards.
10157 The difference between N and the actual number of articles marked is
10158 returned."
10159   (interactive "p")
10160   (gnus-summary-mark-forward
10161    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10162
10163 (defun gnus-summary-mark-as-read (&optional article mark)
10164   "Mark current article as read.
10165 ARTICLE specifies the article to be marked as read.
10166 MARK specifies a string to be inserted at the beginning of the line."
10167   (gnus-summary-mark-article article mark))
10168
10169 (defun gnus-summary-clear-mark-forward (n)
10170   "Clear marks from N articles forward.
10171 If N is negative, clear backward instead.
10172 The difference between N and the number of marks cleared is returned."
10173   (interactive "p")
10174   (gnus-summary-mark-forward n gnus-unread-mark))
10175
10176 (defun gnus-summary-clear-mark-backward (n)
10177   "Clear marks from N articles backward.
10178 The difference between N and the number of marks cleared is returned."
10179   (interactive "p")
10180   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10181
10182 (defun gnus-summary-mark-unread-as-read ()
10183   "Intended to be used by `gnus-summary-mark-article-hook'."
10184   (when (memq gnus-current-article gnus-newsgroup-unreads)
10185     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10186
10187 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10188   "Intended to be used by `gnus-summary-mark-article-hook'."
10189   (let ((mark (gnus-summary-article-mark)))
10190     (when (or (gnus-unread-mark-p mark)
10191               (gnus-read-mark-p mark))
10192       (gnus-summary-mark-article gnus-current-article
10193                                  (or new-mark gnus-read-mark)))))
10194
10195 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10196   "Intended to be used by `gnus-summary-mark-article-hook'."
10197   (let ((mark (gnus-summary-article-mark)))
10198     (when (or (gnus-unread-mark-p mark)
10199               (gnus-read-mark-p mark))
10200       (gnus-summary-mark-article (gnus-summary-article-number)
10201                                  (or new-mark gnus-read-mark)))))
10202
10203 (defun gnus-summary-mark-unread-as-ticked ()
10204   "Intended to be used by `gnus-summary-mark-article-hook'."
10205   (when (memq gnus-current-article gnus-newsgroup-unreads)
10206     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10207
10208 (defun gnus-summary-mark-region-as-read (point mark all)
10209   "Mark all unread articles between point and mark as read.
10210 If given a prefix, mark all articles between point and mark as read,
10211 even ticked and dormant ones."
10212   (interactive "r\nP")
10213   (save-excursion
10214     (let (article)
10215       (goto-char point)
10216       (beginning-of-line)
10217       (while (and
10218               (< (point) mark)
10219               (progn
10220                 (when (or all
10221                           (memq (setq article (gnus-summary-article-number))
10222                                 gnus-newsgroup-unreads))
10223                   (gnus-summary-mark-article article gnus-del-mark))
10224                 t)
10225               (gnus-summary-find-next))))))
10226
10227 (defun gnus-summary-mark-below (score mark)
10228   "Mark articles with score less than SCORE with MARK."
10229   (interactive "P\ncMark: ")
10230   (setq score (if score
10231                   (prefix-numeric-value score)
10232                 (or gnus-summary-default-score 0)))
10233   (save-excursion
10234     (set-buffer gnus-summary-buffer)
10235     (goto-char (point-min))
10236     (while
10237         (progn
10238           (and (< (gnus-summary-article-score) score)
10239                (gnus-summary-mark-article nil mark))
10240           (gnus-summary-find-next)))))
10241
10242 (defun gnus-summary-kill-below (&optional score)
10243   "Mark articles with score below SCORE as read."
10244   (interactive "P")
10245   (gnus-summary-mark-below score gnus-killed-mark))
10246
10247 (defun gnus-summary-clear-above (&optional score)
10248   "Clear all marks from articles with score above SCORE."
10249   (interactive "P")
10250   (gnus-summary-mark-above score gnus-unread-mark))
10251
10252 (defun gnus-summary-tick-above (&optional score)
10253   "Tick all articles with score above SCORE."
10254   (interactive "P")
10255   (gnus-summary-mark-above score gnus-ticked-mark))
10256
10257 (defun gnus-summary-mark-above (score mark)
10258   "Mark articles with score over SCORE with MARK."
10259   (interactive "P\ncMark: ")
10260   (setq score (if score
10261                   (prefix-numeric-value score)
10262                 (or gnus-summary-default-score 0)))
10263   (save-excursion
10264     (set-buffer gnus-summary-buffer)
10265     (goto-char (point-min))
10266     (while (and (progn
10267                   (when (> (gnus-summary-article-score) score)
10268                     (gnus-summary-mark-article nil mark))
10269                   t)
10270                 (gnus-summary-find-next)))))
10271
10272 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10273 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10274 (defun gnus-summary-limit-include-expunged (&optional no-error)
10275   "Display all the hidden articles that were expunged for low scores."
10276   (interactive)
10277   (let ((buffer-read-only nil))
10278     (let ((scored gnus-newsgroup-scored)
10279           headers h)
10280       (while scored
10281         (unless (gnus-summary-article-header (caar scored))
10282           (and (setq h (gnus-number-to-header (caar scored)))
10283                (< (cdar scored) gnus-summary-expunge-below)
10284                (push h headers)))
10285         (setq scored (cdr scored)))
10286       (if (not headers)
10287           (when (not no-error)
10288             (error "No expunged articles hidden"))
10289         (goto-char (point-min))
10290         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10291         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10292         (mapcar (lambda (x) (push (mail-header-number x)
10293                                   gnus-newsgroup-limit))
10294                 headers)
10295         (gnus-summary-prepare-unthreaded (nreverse headers))
10296         (goto-char (point-min))
10297         (gnus-summary-position-point)
10298         t))))
10299
10300 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10301   "Mark all unread articles in this newsgroup as read.
10302 If prefix argument ALL is non-nil, ticked and dormant articles will
10303 also be marked as read.
10304 If QUIETLY is non-nil, no questions will be asked.
10305
10306 If TO-HERE is non-nil, it should be a point in the buffer.  All
10307 articles before (after, if REVERSE is set) this point will be marked
10308 as read.
10309
10310 Note that this function will only catch up the unread article
10311 in the current summary buffer limitation.
10312
10313 The number of articles marked as read is returned."
10314   (interactive "P")
10315   (prog1
10316       (save-excursion
10317         (when (or quietly
10318                   (not gnus-interactive-catchup) ;Without confirmation?
10319                   gnus-expert-user
10320                   (gnus-y-or-n-p
10321                    (if all
10322                        "Mark absolutely all articles as read? "
10323                      "Mark all unread articles as read? ")))
10324           (if (and not-mark
10325                    (not gnus-newsgroup-adaptive)
10326                    (not gnus-newsgroup-auto-expire)
10327                    (not gnus-suppress-duplicates)
10328                    (or (not gnus-use-cache)
10329                        (eq gnus-use-cache 'passive)))
10330               (progn
10331                 (when all
10332                   (setq gnus-newsgroup-marked nil
10333                         gnus-newsgroup-spam-marked nil
10334                         gnus-newsgroup-dormant nil))
10335                 (setq gnus-newsgroup-unreads
10336                       (gnus-sorted-nunion
10337                        (gnus-intersection gnus-newsgroup-unreads
10338                                           gnus-newsgroup-downloadable)
10339                        gnus-newsgroup-unfetched)))
10340             ;; We actually mark all articles as canceled, which we
10341             ;; have to do when using auto-expiry or adaptive scoring.
10342             (gnus-summary-show-all-threads)
10343             (if (and to-here reverse)
10344                 (progn
10345                   (goto-char to-here)
10346                   (gnus-summary-mark-current-read-and-unread-as-read
10347                    gnus-catchup-mark)
10348                   (while (gnus-summary-find-next (not all))
10349                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10350               (when (gnus-summary-first-subject (not all))
10351                 (while (and
10352                         (if to-here (< (point) to-here) t)
10353                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10354                         (gnus-summary-find-next (not all))))))
10355             (gnus-set-mode-line 'summary))
10356           t))
10357     (gnus-summary-position-point)))
10358
10359 (defun gnus-summary-catchup-to-here (&optional all)
10360   "Mark all unticked articles before the current one as read.
10361 If ALL is non-nil, also mark ticked and dormant articles as read."
10362   (interactive "P")
10363   (save-excursion
10364     (gnus-save-hidden-threads
10365       (let ((beg (point)))
10366         ;; We check that there are unread articles.
10367         (when (or all (gnus-summary-find-prev))
10368           (gnus-summary-catchup all t beg)))))
10369   (gnus-summary-position-point))
10370
10371 (defun gnus-summary-catchup-from-here (&optional all)
10372   "Mark all unticked articles after (and including) the current one as read.
10373 If ALL is non-nil, also mark ticked and dormant articles as read."
10374   (interactive "P")
10375   (save-excursion
10376     (gnus-save-hidden-threads
10377       (let ((beg (point)))
10378         ;; We check that there are unread articles.
10379         (when (or all (gnus-summary-find-next))
10380           (gnus-summary-catchup all t beg nil t)))))
10381   (gnus-summary-position-point))
10382
10383 (defun gnus-summary-catchup-all (&optional quietly)
10384   "Mark all articles in this newsgroup as read.
10385 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10386 instead, which marks only unread articles as read."
10387   (interactive "P")
10388   (gnus-summary-catchup t quietly))
10389
10390 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10391   "Mark all unread articles in this group as read, then exit.
10392 If prefix argument ALL is non-nil, all articles are marked as read.
10393 If QUIETLY is non-nil, no questions will be asked."
10394   (interactive "P")
10395   (when (gnus-summary-catchup all quietly nil 'fast)
10396     ;; Select next newsgroup or exit.
10397     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10398              (eq gnus-auto-select-next 'quietly))
10399         (gnus-summary-next-group nil)
10400       (gnus-summary-exit))))
10401
10402 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10403   "Mark all articles in this newsgroup as read, and then exit.
10404 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10405 instead, which marks only unread articles as read."
10406   (interactive "P")
10407   (gnus-summary-catchup-and-exit t quietly))
10408
10409 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10410   "Mark all articles in this group as read and select the next group.
10411 If given a prefix, mark all articles, unread as well as ticked, as
10412 read."
10413   (interactive "P")
10414   (save-excursion
10415     (gnus-summary-catchup all))
10416   (gnus-summary-next-group))
10417
10418 ;;;
10419 ;;; with article
10420 ;;;
10421
10422 (defmacro gnus-with-article (article &rest forms)
10423   "Select ARTICLE and perform FORMS in the original article buffer.
10424 Then replace the article with the result."
10425   `(progn
10426      ;; We don't want the article to be marked as read.
10427      (let (gnus-mark-article-hook)
10428        (gnus-summary-select-article t t nil ,article))
10429      (set-buffer gnus-original-article-buffer)
10430      ,@forms
10431      (if (not (gnus-check-backend-function
10432                'request-replace-article (car gnus-article-current)))
10433          (gnus-message 5 "Read-only group; not replacing")
10434        (unless (gnus-request-replace-article
10435                 ,article (car gnus-article-current)
10436                 (current-buffer) t)
10437          (error "Couldn't replace article")))
10438      ;; The cache and backlog have to be flushed somewhat.
10439      (when gnus-keep-backlog
10440        (gnus-backlog-remove-article
10441         (car gnus-article-current) (cdr gnus-article-current)))
10442      (when gnus-use-cache
10443        (gnus-cache-update-article
10444         (car gnus-article-current) (cdr gnus-article-current)))))
10445
10446 (put 'gnus-with-article 'lisp-indent-function 1)
10447 (put 'gnus-with-article 'edebug-form-spec '(form body))
10448
10449 ;; Thread-based commands.
10450
10451 (defun gnus-summary-articles-in-thread (&optional article)
10452   "Return a list of all articles in the current thread.
10453 If ARTICLE is non-nil, return all articles in the thread that starts
10454 with that article."
10455   (let* ((article (or article (gnus-summary-article-number)))
10456          (data (gnus-data-find-list article))
10457          (top-level (gnus-data-level (car data)))
10458          (top-subject
10459           (cond ((null gnus-thread-operation-ignore-subject)
10460                  (gnus-simplify-subject-re
10461                   (mail-header-subject (gnus-data-header (car data)))))
10462                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10463                  (gnus-simplify-subject-fuzzy
10464                   (mail-header-subject (gnus-data-header (car data)))))
10465                 (t nil)))
10466          (end-point (save-excursion
10467                       (if (gnus-summary-go-to-next-thread)
10468                           (point) (point-max))))
10469          articles)
10470     (while (and data
10471                 (< (gnus-data-pos (car data)) end-point))
10472       (when (or (not top-subject)
10473                 (string= top-subject
10474                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10475                              (gnus-simplify-subject-fuzzy
10476                               (mail-header-subject
10477                                (gnus-data-header (car data))))
10478                            (gnus-simplify-subject-re
10479                             (mail-header-subject
10480                              (gnus-data-header (car data)))))))
10481         (push (gnus-data-number (car data)) articles))
10482       (unless (and (setq data (cdr data))
10483                    (> (gnus-data-level (car data)) top-level))
10484         (setq data nil)))
10485     ;; Return the list of articles.
10486     (nreverse articles)))
10487
10488 (defun gnus-summary-rethread-current ()
10489   "Rethread the thread the current article is part of."
10490   (interactive)
10491   (let* ((gnus-show-threads t)
10492          (article (gnus-summary-article-number))
10493          (id (mail-header-id (gnus-summary-article-header)))
10494          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10495     (unless id
10496       (error "No article on the current line"))
10497     (gnus-rebuild-thread id)
10498     (gnus-summary-goto-subject article)))
10499
10500 (defun gnus-summary-reparent-thread ()
10501   "Make the current article child of the marked (or previous) article.
10502
10503 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10504 is non-nil or the Subject: of both articles are the same."
10505   (interactive)
10506   (unless (not (gnus-group-read-only-p))
10507     (error "The current newsgroup does not support article editing"))
10508   (unless (<= (length gnus-newsgroup-processable) 1)
10509     (error "No more than one article may be marked"))
10510   (save-window-excursion
10511     (let ((gnus-article-buffer " *reparent*")
10512           (current-article (gnus-summary-article-number))
10513           ;; First grab the marked article, otherwise one line up.
10514           (parent-article (if (not (null gnus-newsgroup-processable))
10515                               (car gnus-newsgroup-processable)
10516                             (save-excursion
10517                               (if (eq (forward-line -1) 0)
10518                                   (gnus-summary-article-number)
10519                                 (error "Beginning of summary buffer"))))))
10520       (unless (not (eq current-article parent-article))
10521         (error "An article may not be self-referential"))
10522       (let ((message-id (mail-header-id
10523                          (gnus-summary-article-header parent-article))))
10524         (unless (and message-id (not (equal message-id "")))
10525           (error "No message-id in desired parent"))
10526         (gnus-with-article current-article
10527           (save-restriction
10528             (goto-char (point-min))
10529             (message-narrow-to-head)
10530             (if (re-search-forward "^References: " nil t)
10531                 (progn
10532                   (re-search-forward "^[^ \t]" nil t)
10533                   (forward-line -1)
10534                   (end-of-line)
10535                   (insert " " message-id))
10536               (insert "References: " message-id "\n"))))
10537         (set-buffer gnus-summary-buffer)
10538         (gnus-summary-unmark-all-processable)
10539         (gnus-summary-update-article current-article)
10540         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10541             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10542         (gnus-summary-rethread-current)
10543         (gnus-message 3 "Article %d is now the child of article %d"
10544                       current-article parent-article)))))
10545
10546 (defun gnus-summary-toggle-threads (&optional arg)
10547   "Toggle showing conversation threads.
10548 If ARG is positive number, turn showing conversation threads on."
10549   (interactive "P")
10550   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10551     (setq gnus-show-threads
10552           (if (null arg) (not gnus-show-threads)
10553             (> (prefix-numeric-value arg) 0)))
10554     (gnus-summary-prepare)
10555     (gnus-summary-goto-subject current)
10556     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10557     (gnus-summary-position-point)))
10558
10559 (defun gnus-summary-show-all-threads ()
10560   "Show all threads."
10561   (interactive)
10562   (save-excursion
10563     (let ((buffer-read-only nil))
10564       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10565   (gnus-summary-position-point))
10566
10567 (defun gnus-summary-show-thread ()
10568   "Show thread subtrees.
10569 Returns nil if no thread was there to be shown."
10570   (interactive)
10571   (let ((buffer-read-only nil)
10572         (orig (point))
10573         (end (point-at-eol))
10574         ;; Leave point at bol
10575         (beg (progn (beginning-of-line) (point))))
10576     (prog1
10577         ;; Any hidden lines here?
10578         (search-forward "\r" end t)
10579       (subst-char-in-region beg end ?\^M ?\n t)
10580       (goto-char orig)
10581       (gnus-summary-position-point))))
10582
10583 (defun gnus-summary-maybe-hide-threads ()
10584   "If requested, hide the threads that should be hidden."
10585   (when (and gnus-show-threads
10586              gnus-thread-hide-subtree)
10587     (gnus-summary-hide-all-threads
10588      (if (or (consp gnus-thread-hide-subtree)
10589              (functionp gnus-thread-hide-subtree))
10590          (gnus-make-predicate gnus-thread-hide-subtree)
10591        nil))))
10592
10593 ;;; Hiding predicates.
10594
10595 (defun gnus-article-unread-p (header)
10596   (memq (mail-header-number header) gnus-newsgroup-unreads))
10597
10598 (defun gnus-article-unseen-p (header)
10599   (memq (mail-header-number header) gnus-newsgroup-unseen))
10600
10601 (defun gnus-map-articles (predicate articles)
10602   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10603   (apply 'gnus-or (mapcar predicate
10604                           (mapcar 'gnus-summary-article-header articles))))
10605
10606 (defun gnus-summary-hide-all-threads (&optional predicate)
10607   "Hide all thread subtrees.
10608 If PREDICATE is supplied, threads that satisfy this predicate
10609 will not be hidden."
10610   (interactive)
10611   (save-excursion
10612     (goto-char (point-min))
10613     (let ((end nil))
10614       (while (not end)
10615         (when (or (not predicate)
10616                   (gnus-map-articles
10617                    predicate (gnus-summary-article-children)))
10618             (gnus-summary-hide-thread))
10619         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10620   (gnus-summary-position-point))
10621
10622 (defun gnus-summary-hide-thread ()
10623   "Hide thread subtrees.
10624 If PREDICATE is supplied, threads that satisfy this predicate
10625 will not be hidden.
10626 Returns nil if no threads were there to be hidden."
10627   (interactive)
10628   (let ((buffer-read-only nil)
10629         (start (point))
10630         (article (gnus-summary-article-number)))
10631     (goto-char start)
10632     ;; Go forward until either the buffer ends or the subthread
10633     ;; ends.
10634     (when (and (not (eobp))
10635                (or (zerop (gnus-summary-next-thread 1 t))
10636                    (goto-char (point-max))))
10637       (prog1
10638           (if (and (> (point) start)
10639                    (search-backward "\n" start t))
10640               (progn
10641                 (subst-char-in-region start (point) ?\n ?\^M)
10642                 (gnus-summary-goto-subject article))
10643             (goto-char start)
10644             nil)))))
10645
10646 (defun gnus-summary-go-to-next-thread (&optional previous)
10647   "Go to the same level (or less) next thread.
10648 If PREVIOUS is non-nil, go to previous thread instead.
10649 Return the article number moved to, or nil if moving was impossible."
10650   (let ((level (gnus-summary-thread-level))
10651         (way (if previous -1 1))
10652         (beg (point)))
10653     (forward-line way)
10654     (while (and (not (eobp))
10655                 (< level (gnus-summary-thread-level)))
10656       (forward-line way))
10657     (if (eobp)
10658         (progn
10659           (goto-char beg)
10660           nil)
10661       (setq beg (point))
10662       (prog1
10663           (gnus-summary-article-number)
10664         (goto-char beg)))))
10665
10666 (defun gnus-summary-next-thread (n &optional silent)
10667   "Go to the same level next N'th thread.
10668 If N is negative, search backward instead.
10669 Returns the difference between N and the number of skips actually
10670 done.
10671
10672 If SILENT, don't output messages."
10673   (interactive "p")
10674   (let ((backward (< n 0))
10675         (n (abs n)))
10676     (while (and (> n 0)
10677                 (gnus-summary-go-to-next-thread backward))
10678       (decf n))
10679     (unless silent
10680       (gnus-summary-position-point))
10681     (when (and (not silent) (/= 0 n))
10682       (gnus-message 7 "No more threads"))
10683     n))
10684
10685 (defun gnus-summary-prev-thread (n)
10686   "Go to the same level previous N'th thread.
10687 Returns the difference between N and the number of skips actually
10688 done."
10689   (interactive "p")
10690   (gnus-summary-next-thread (- n)))
10691
10692 (defun gnus-summary-go-down-thread ()
10693   "Go down one level in the current thread."
10694   (let ((children (gnus-summary-article-children)))
10695     (when children
10696       (gnus-summary-goto-subject (car children)))))
10697
10698 (defun gnus-summary-go-up-thread ()
10699   "Go up one level in the current thread."
10700   (let ((parent (gnus-summary-article-parent)))
10701     (when parent
10702       (gnus-summary-goto-subject parent))))
10703
10704 (defun gnus-summary-down-thread (n)
10705   "Go down thread N steps.
10706 If N is negative, go up instead.
10707 Returns the difference between N and how many steps down that were
10708 taken."
10709   (interactive "p")
10710   (let ((up (< n 0))
10711         (n (abs n)))
10712     (while (and (> n 0)
10713                 (if up (gnus-summary-go-up-thread)
10714                   (gnus-summary-go-down-thread)))
10715       (setq n (1- n)))
10716     (gnus-summary-position-point)
10717     (when (/= 0 n)
10718       (gnus-message 7 "Can't go further"))
10719     n))
10720
10721 (defun gnus-summary-up-thread (n)
10722   "Go up thread N steps.
10723 If N is negative, go down instead.
10724 Returns the difference between N and how many steps down that were
10725 taken."
10726   (interactive "p")
10727   (gnus-summary-down-thread (- n)))
10728
10729 (defun gnus-summary-top-thread ()
10730   "Go to the top of the thread."
10731   (interactive)
10732   (while (gnus-summary-go-up-thread))
10733   (gnus-summary-article-number))
10734
10735 (defun gnus-summary-kill-thread (&optional unmark)
10736   "Mark articles under current thread as read.
10737 If the prefix argument is positive, remove any kinds of marks.
10738 If the prefix argument is negative, tick articles instead."
10739   (interactive "P")
10740   (when unmark
10741     (setq unmark (prefix-numeric-value unmark)))
10742   (let ((articles (gnus-summary-articles-in-thread)))
10743     (save-excursion
10744       ;; Expand the thread.
10745       (gnus-summary-show-thread)
10746       ;; Mark all the articles.
10747       (while articles
10748         (gnus-summary-goto-subject (car articles))
10749         (cond ((null unmark)
10750                (gnus-summary-mark-article-as-read gnus-killed-mark))
10751               ((> unmark 0)
10752                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10753               (t
10754                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10755         (setq articles (cdr articles))))
10756     ;; Hide killed subtrees.
10757     (and (null unmark)
10758          gnus-thread-hide-killed
10759          (gnus-summary-hide-thread))
10760     ;; If marked as read, go to next unread subject.
10761     (when (null unmark)
10762       ;; Go to next unread subject.
10763       (gnus-summary-next-subject 1 t)))
10764   (gnus-set-mode-line 'summary))
10765
10766 ;; Summary sorting commands
10767
10768 (defun gnus-summary-sort-by-number (&optional reverse)
10769   "Sort the summary buffer by article number.
10770 Argument REVERSE means reverse order."
10771   (interactive "P")
10772   (gnus-summary-sort 'number reverse))
10773
10774 (defun gnus-summary-sort-by-random (&optional reverse)
10775   "Randomize the order in the summary buffer.
10776 Argument REVERSE means to randomize in reverse order."
10777   (interactive "P")
10778   (gnus-summary-sort 'random reverse))
10779
10780 (defun gnus-summary-sort-by-author (&optional reverse)
10781   "Sort the summary buffer by author name alphabetically.
10782 If `case-fold-search' is non-nil, case of letters is ignored.
10783 Argument REVERSE means reverse order."
10784   (interactive "P")
10785   (gnus-summary-sort 'author reverse))
10786
10787 (defun gnus-summary-sort-by-subject (&optional reverse)
10788   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10789 If `case-fold-search' is non-nil, case of letters is ignored.
10790 Argument REVERSE means reverse order."
10791   (interactive "P")
10792   (gnus-summary-sort 'subject reverse))
10793
10794 (defun gnus-summary-sort-by-date (&optional reverse)
10795   "Sort the summary buffer by date.
10796 Argument REVERSE means reverse order."
10797   (interactive "P")
10798   (gnus-summary-sort 'date reverse))
10799
10800 (defun gnus-summary-sort-by-score (&optional reverse)
10801   "Sort the summary buffer by score.
10802 Argument REVERSE means reverse order."
10803   (interactive "P")
10804   (gnus-summary-sort 'score reverse))
10805
10806 (defun gnus-summary-sort-by-lines (&optional reverse)
10807   "Sort the summary buffer by the number of lines.
10808 Argument REVERSE means reverse order."
10809   (interactive "P")
10810   (gnus-summary-sort 'lines reverse))
10811
10812 (defun gnus-summary-sort-by-chars (&optional reverse)
10813   "Sort the summary buffer by article length.
10814 Argument REVERSE means reverse order."
10815   (interactive "P")
10816   (gnus-summary-sort 'chars reverse))
10817
10818 (defun gnus-summary-sort-by-original (&optional reverse)
10819   "Sort the summary buffer using the default sorting method.
10820 Argument REVERSE means reverse order."
10821   (interactive "P")
10822   (let* ((buffer-read-only)
10823          (gnus-summary-prepare-hook nil))
10824     ;; We do the sorting by regenerating the threads.
10825     (gnus-summary-prepare)
10826     ;; Hide subthreads if needed.
10827     (gnus-summary-maybe-hide-threads)))
10828
10829 (defun gnus-summary-sort (predicate reverse)
10830   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10831   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10832          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10833          (gnus-thread-sort-functions
10834           (if (not reverse)
10835               thread
10836             `(lambda (t1 t2)
10837                (,thread t2 t1))))
10838          (gnus-sort-gathered-threads-function
10839           gnus-thread-sort-functions)
10840          (gnus-article-sort-functions
10841           (if (not reverse)
10842               article
10843             `(lambda (t1 t2)
10844                (,article t2 t1))))
10845          (buffer-read-only)
10846          (gnus-summary-prepare-hook nil))
10847     ;; We do the sorting by regenerating the threads.
10848     (gnus-summary-prepare)
10849     ;; Hide subthreads if needed.
10850     (gnus-summary-maybe-hide-threads)))
10851
10852 ;; Summary saving commands.
10853
10854 (defun gnus-summary-save-article (&optional n not-saved)
10855   "Save the current article using the default saver function.
10856 If N is a positive number, save the N next articles.
10857 If N is a negative number, save the N previous articles.
10858 If N is nil and any articles have been marked with the process mark,
10859 save those articles instead.
10860 The variable `gnus-default-article-saver' specifies the saver function."
10861   (interactive "P")
10862   (let* ((articles (gnus-summary-work-articles n))
10863          (save-buffer (save-excursion
10864                         (nnheader-set-temp-buffer " *Gnus Save*")))
10865          (num (length articles))
10866          header file)
10867     (dolist (article articles)
10868       (setq header (gnus-summary-article-header article))
10869       (if (not (vectorp header))
10870           ;; This is a pseudo-article.
10871           (if (assq 'name header)
10872               (gnus-copy-file (cdr (assq 'name header)))
10873             (gnus-message 1 "Article %d is unsaveable" article))
10874         ;; This is a real article.
10875         (save-window-excursion
10876           (let ((gnus-display-mime-function nil)
10877                 (gnus-article-prepare-hook nil))
10878             (gnus-summary-select-article t nil nil article)))
10879         (save-excursion
10880           (set-buffer save-buffer)
10881           (erase-buffer)
10882           (insert-buffer-substring gnus-original-article-buffer))
10883         (setq file (gnus-article-save save-buffer file num))
10884         (gnus-summary-remove-process-mark article)
10885         (unless not-saved
10886           (gnus-summary-set-saved-mark article))))
10887     (gnus-kill-buffer save-buffer)
10888     (gnus-summary-position-point)
10889     (gnus-set-mode-line 'summary)
10890     n))
10891
10892 (defun gnus-summary-pipe-output (&optional arg headers)
10893   "Pipe the current article to a subprocess.
10894 If N is a positive number, pipe the N next articles.
10895 If N is a negative number, pipe the N previous articles.
10896 If N is nil and any articles have been marked with the process mark,
10897 pipe those articles instead.
10898 If HEADERS (the symbolic prefix), include the headers, too."
10899   (interactive (gnus-interactive "P\ny"))
10900   (require 'gnus-art)
10901   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10902         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10903     (gnus-summary-save-article arg t))
10904   (let ((buffer (get-buffer "*Shell Command Output*")))
10905     (when (and buffer
10906                (not (zerop (buffer-size buffer))))
10907       (gnus-configure-windows 'pipe))))
10908
10909 (defun gnus-summary-save-article-mail (&optional arg)
10910   "Append the current article to an mail file.
10911 If N is a positive number, save the N next articles.
10912 If N is a negative number, save the N previous articles.
10913 If N is nil and any articles have been marked with the process mark,
10914 save those articles instead."
10915   (interactive "P")
10916   (require 'gnus-art)
10917   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10918     (gnus-summary-save-article arg)))
10919
10920 (defun gnus-summary-save-article-rmail (&optional arg)
10921   "Append the current article to an rmail file.
10922 If N is a positive number, save the N next articles.
10923 If N is a negative number, save the N previous articles.
10924 If N is nil and any articles have been marked with the process mark,
10925 save those articles instead."
10926   (interactive "P")
10927   (require 'gnus-art)
10928   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10929     (gnus-summary-save-article arg)))
10930
10931 (defun gnus-summary-save-article-file (&optional arg)
10932   "Append the current article to a file.
10933 If N is a positive number, save the N next articles.
10934 If N is a negative number, save the N previous articles.
10935 If N is nil and any articles have been marked with the process mark,
10936 save those articles instead."
10937   (interactive "P")
10938   (require 'gnus-art)
10939   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10940     (gnus-summary-save-article arg)))
10941
10942 (defun gnus-summary-write-article-file (&optional arg)
10943   "Write the current article to a file, deleting the previous file.
10944 If N is a positive number, save the N next articles.
10945 If N is a negative number, save the N previous articles.
10946 If N is nil and any articles have been marked with the process mark,
10947 save those articles instead."
10948   (interactive "P")
10949   (require 'gnus-art)
10950   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10951     (gnus-summary-save-article arg)))
10952
10953 (defun gnus-summary-save-article-body-file (&optional arg)
10954   "Append the current article body to a file.
10955 If N is a positive number, save the N next articles.
10956 If N is a negative number, save the N previous articles.
10957 If N is nil and any articles have been marked with the process mark,
10958 save those articles instead."
10959   (interactive "P")
10960   (require 'gnus-art)
10961   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10962     (gnus-summary-save-article arg)))
10963
10964 (defun gnus-summary-muttprint (&optional arg)
10965   "Print the current article using Muttprint.
10966 If N is a positive number, save the N next articles.
10967 If N is a negative number, save the N previous articles.
10968 If N is nil and any articles have been marked with the process mark,
10969 save those articles instead."
10970   (interactive "P")
10971   (require 'gnus-art)
10972   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10973     (gnus-summary-save-article arg t)))
10974
10975 (defun gnus-summary-pipe-message (program)
10976   "Pipe the current article through PROGRAM."
10977   (interactive "sProgram: ")
10978   (gnus-summary-select-article)
10979   (let ((mail-header-separator ""))
10980     (gnus-eval-in-buffer-window gnus-article-buffer
10981       (save-restriction
10982         (widen)
10983         (let ((start (window-start))
10984               buffer-read-only)
10985           (message-pipe-buffer-body program)
10986           (set-window-start (get-buffer-window (current-buffer)) start))))))
10987
10988 (defun gnus-get-split-value (methods)
10989   "Return a value based on the split METHODS."
10990   (let (split-name method result match)
10991     (when methods
10992       (save-excursion
10993         (set-buffer gnus-original-article-buffer)
10994         (save-restriction
10995           (nnheader-narrow-to-headers)
10996           (while (and methods (not split-name))
10997             (goto-char (point-min))
10998             (setq method (pop methods))
10999             (setq match (car method))
11000             (when (cond
11001                    ((stringp match)
11002                     ;; Regular expression.
11003                     (ignore-errors
11004                       (re-search-forward match nil t)))
11005                    ((functionp match)
11006                     ;; Function.
11007                     (save-restriction
11008                       (widen)
11009                       (setq result (funcall match gnus-newsgroup-name))))
11010                    ((consp match)
11011                     ;; Form.
11012                     (save-restriction
11013                       (widen)
11014                       (setq result (eval match)))))
11015               (setq split-name (cdr method))
11016               (cond ((stringp result)
11017                      (push (expand-file-name
11018                             result gnus-article-save-directory)
11019                            split-name))
11020                     ((consp result)
11021                      (setq split-name (append result split-name)))))))))
11022     (nreverse split-name)))
11023
11024 (defun gnus-valid-move-group-p (group)
11025   (and (boundp group)
11026        (symbol-name group)
11027        (symbol-value group)
11028        (gnus-get-function (gnus-find-method-for-group
11029                            (symbol-name group)) 'request-accept-article t)))
11030
11031 (defun gnus-read-move-group-name (prompt default articles prefix)
11032   "Read a group name."
11033   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11034          (minibuffer-confirm-incomplete nil) ; XEmacs
11035          (prom
11036           (format "%s %s to:"
11037                   prompt
11038                   (if (> (length articles) 1)
11039                       (format "these %d articles" (length articles))
11040                     "this article")))
11041          (to-newsgroup
11042           (cond
11043            ((null split-name)
11044             (gnus-completing-read-with-default
11045              default prom
11046              gnus-active-hashtb
11047              'gnus-valid-move-group-p
11048              nil prefix
11049              'gnus-group-history))
11050            ((= 1 (length split-name))
11051             (gnus-completing-read-with-default
11052              (car split-name) prom
11053              gnus-active-hashtb
11054              'gnus-valid-move-group-p
11055              nil nil
11056              'gnus-group-history))
11057            (t
11058             (gnus-completing-read-with-default
11059              nil prom
11060              (mapcar 'list (nreverse split-name))
11061              nil nil nil
11062              'gnus-group-history))))
11063          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11064     (when to-newsgroup
11065       (if (or (string= to-newsgroup "")
11066               (string= to-newsgroup prefix))
11067           (setq to-newsgroup default))
11068       (unless to-newsgroup
11069         (error "No group name entered"))
11070       (or (gnus-active to-newsgroup)
11071           (gnus-activate-group to-newsgroup nil nil to-method)
11072           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11073                                      to-newsgroup))
11074               (or (and (gnus-request-create-group to-newsgroup to-method)
11075                        (gnus-activate-group
11076                         to-newsgroup nil nil to-method)
11077                        (gnus-subscribe-group to-newsgroup))
11078                   (error "Couldn't create group %s" to-newsgroup)))
11079           (error "No such group: %s" to-newsgroup)))
11080     to-newsgroup))
11081
11082 (defun gnus-summary-save-parts (type dir n &optional reverse)
11083   "Save parts matching TYPE to DIR.
11084 If REVERSE, save parts that do not match TYPE."
11085   (interactive
11086    (list (read-string "Save parts of type: "
11087                       (or (car gnus-summary-save-parts-type-history)
11088                           gnus-summary-save-parts-default-mime)
11089                       'gnus-summary-save-parts-type-history)
11090          (setq gnus-summary-save-parts-last-directory
11091                (read-file-name "Save to directory: "
11092                                gnus-summary-save-parts-last-directory
11093                                nil t))
11094          current-prefix-arg))
11095   (gnus-summary-iterate n
11096     (let ((gnus-display-mime-function nil)
11097           (gnus-inhibit-treatment t))
11098       (gnus-summary-select-article))
11099     (save-excursion
11100       (set-buffer gnus-article-buffer)
11101       (let ((handles (or gnus-article-mime-handles
11102                          (mm-dissect-buffer nil gnus-article-loose-mime)
11103                          (and gnus-article-emulate-mime
11104                               (mm-uu-dissect)))))
11105         (when handles
11106           (gnus-summary-save-parts-1 type dir handles reverse)
11107           (unless gnus-article-mime-handles ;; Don't destroy this case.
11108             (mm-destroy-parts handles)))))))
11109
11110 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11111   (if (stringp (car handle))
11112       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11113               (cdr handle))
11114     (when (if reverse
11115               (not (string-match type (mm-handle-media-type handle)))
11116             (string-match type (mm-handle-media-type handle)))
11117       (let ((file (expand-file-name
11118                    (gnus-map-function
11119                     mm-file-name-rewrite-functions
11120                     (file-name-nondirectory
11121                      (or
11122                       (mail-content-type-get
11123                        (mm-handle-disposition handle) 'filename)
11124                       (mail-content-type-get
11125                        (mm-handle-type handle) 'name)
11126                       (concat gnus-newsgroup-name
11127                               "." (number-to-string
11128                                    (cdr gnus-article-current))))))
11129                    dir)))
11130         (unless (file-exists-p file)
11131           (mm-save-part-to-file handle file))))))
11132
11133 ;; Summary extract commands
11134
11135 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11136   (let ((buffer-read-only nil)
11137         (article (gnus-summary-article-number))
11138         after-article b e)
11139     (unless (gnus-summary-goto-subject article)
11140       (error "No such article: %d" article))
11141     (gnus-summary-position-point)
11142     ;; If all commands are to be bunched up on one line, we collect
11143     ;; them here.
11144     (unless gnus-view-pseudos-separately
11145       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11146             files action)
11147         (while ps
11148           (setq action (cdr (assq 'action (car ps))))
11149           (setq files (list (cdr (assq 'name (car ps)))))
11150           (while (and ps (cdr ps)
11151                       (string= (or action "1")
11152                                (or (cdr (assq 'action (cadr ps))) "2")))
11153             (push (cdr (assq 'name (cadr ps))) files)
11154             (setcdr ps (cddr ps)))
11155           (when files
11156             (when (not (string-match "%s" action))
11157               (push " " files))
11158             (push " " files)
11159             (when (assq 'execute (car ps))
11160               (setcdr (assq 'execute (car ps))
11161                       (funcall (if (string-match "%s" action)
11162                                    'format 'concat)
11163                                action
11164                                (mapconcat
11165                                 (lambda (f)
11166                                   (if (equal f " ")
11167                                       f
11168                                     (shell-quote-argument f)))
11169                                 files " ")))))
11170           (setq ps (cdr ps)))))
11171     (if (and gnus-view-pseudos (not not-view))
11172         (while pslist
11173           (when (assq 'execute (car pslist))
11174             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11175                                   (eq gnus-view-pseudos 'not-confirm)))
11176           (setq pslist (cdr pslist)))
11177       (save-excursion
11178         (while pslist
11179           (setq after-article (or (cdr (assq 'article (car pslist)))
11180                                   (gnus-summary-article-number)))
11181           (gnus-summary-goto-subject after-article)
11182           (forward-line 1)
11183           (setq b (point))
11184           (insert "    " (file-name-nondirectory
11185                           (cdr (assq 'name (car pslist))))
11186                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11187           (setq e (point))
11188           (forward-line -1)             ; back to `b'
11189           (gnus-add-text-properties
11190            b (1- e) (list 'gnus-number gnus-reffed-article-number
11191                           gnus-mouse-face-prop gnus-mouse-face))
11192           (gnus-data-enter
11193            after-article gnus-reffed-article-number
11194            gnus-unread-mark b (car pslist) 0 (- e b))
11195           (setq gnus-newsgroup-unreads
11196                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11197                                          gnus-reffed-article-number))
11198           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11199           (setq pslist (cdr pslist)))))))
11200
11201 (defun gnus-pseudos< (p1 p2)
11202   (let ((c1 (cdr (assq 'action p1)))
11203         (c2 (cdr (assq 'action p2))))
11204     (and c1 c2 (string< c1 c2))))
11205
11206 (defun gnus-request-pseudo-article (props)
11207   (cond ((assq 'execute props)
11208          (gnus-execute-command (cdr (assq 'execute props)))))
11209   (let ((gnus-current-article (gnus-summary-article-number)))
11210     (gnus-run-hooks 'gnus-mark-article-hook)))
11211
11212 (defun gnus-execute-command (command &optional automatic)
11213   (save-excursion
11214     (gnus-article-setup-buffer)
11215     (set-buffer gnus-article-buffer)
11216     (setq buffer-read-only nil)
11217     (let ((command (if automatic command
11218                      (read-string "Command: " (cons command 0)))))
11219       (erase-buffer)
11220       (insert "$ " command "\n\n")
11221       (if gnus-view-pseudo-asynchronously
11222           (start-process "gnus-execute" (current-buffer) shell-file-name
11223                          shell-command-switch command)
11224         (call-process shell-file-name nil t nil
11225                       shell-command-switch command)))))
11226
11227 ;; Summary kill commands.
11228
11229 (defun gnus-summary-edit-global-kill (article)
11230   "Edit the \"global\" kill file."
11231   (interactive (list (gnus-summary-article-number)))
11232   (gnus-group-edit-global-kill article))
11233
11234 (defun gnus-summary-edit-local-kill ()
11235   "Edit a local kill file applied to the current newsgroup."
11236   (interactive)
11237   (setq gnus-current-headers (gnus-summary-article-header))
11238   (gnus-group-edit-local-kill
11239    (gnus-summary-article-number) gnus-newsgroup-name))
11240
11241 ;;; Header reading.
11242
11243 (defun gnus-read-header (id &optional header)
11244   "Read the headers of article ID and enter them into the Gnus system."
11245   (let ((group gnus-newsgroup-name)
11246         (gnus-override-method
11247          (or
11248           gnus-override-method
11249           (and (gnus-news-group-p gnus-newsgroup-name)
11250                (car (gnus-refer-article-methods)))))
11251         where)
11252     ;; First we check to see whether the header in question is already
11253     ;; fetched.
11254     (if (stringp id)
11255         ;; This is a Message-ID.
11256         (setq header (or header (gnus-id-to-header id)))
11257       ;; This is an article number.
11258       (setq header (or header (gnus-summary-article-header id))))
11259     (if (and header
11260              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11261         ;; We have found the header.
11262         header
11263       ;; We have to really fetch the header to this article.
11264       (save-excursion
11265         (set-buffer nntp-server-buffer)
11266         (when (setq where (gnus-request-head id group))
11267           (nnheader-fold-continuation-lines)
11268           (goto-char (point-max))
11269           (insert ".\n")
11270           (goto-char (point-min))
11271           (insert "211 ")
11272           (princ (cond
11273                   ((numberp id) id)
11274                   ((cdr where) (cdr where))
11275                   (header (mail-header-number header))
11276                   (t gnus-reffed-article-number))
11277                  (current-buffer))
11278           (insert " Article retrieved.\n"))
11279         (if (or (not where)
11280                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11281             ()                          ; Malformed head.
11282           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11283             (when (and (stringp id)
11284                        (not (string= (gnus-group-real-name group)
11285                                      (car where))))
11286               ;; If we fetched by Message-ID and the article came
11287               ;; from a different group, we fudge some bogus article
11288               ;; numbers for this article.
11289               (mail-header-set-number header gnus-reffed-article-number))
11290             (save-excursion
11291               (set-buffer gnus-summary-buffer)
11292               (decf gnus-reffed-article-number)
11293               (gnus-remove-header (mail-header-number header))
11294               (push header gnus-newsgroup-headers)
11295               (setq gnus-current-headers header)
11296               (push (mail-header-number header) gnus-newsgroup-limit)))
11297           header)))))
11298
11299 (defun gnus-remove-header (number)
11300   "Remove header NUMBER from `gnus-newsgroup-headers'."
11301   (if (and gnus-newsgroup-headers
11302            (= number (mail-header-number (car gnus-newsgroup-headers))))
11303       (pop gnus-newsgroup-headers)
11304     (let ((headers gnus-newsgroup-headers))
11305       (while (and (cdr headers)
11306                   (not (= number (mail-header-number (cadr headers)))))
11307         (pop headers))
11308       (when (cdr headers)
11309         (setcdr headers (cddr headers))))))
11310
11311 ;;;
11312 ;;; summary highlights
11313 ;;;
11314
11315 (defun gnus-highlight-selected-summary ()
11316   "Highlight selected article in summary buffer."
11317   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11318   (when gnus-summary-selected-face
11319     (save-excursion
11320       (let* ((beg (point-at-bol))
11321              (end (point-at-eol))
11322              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11323              (from (if (get-text-property beg gnus-mouse-face-prop)
11324                        beg
11325                      (or (next-single-property-change
11326                           beg gnus-mouse-face-prop nil end)
11327                          beg)))
11328              (to
11329               (if (= from end)
11330                   (- from 2)
11331                 (or (next-single-property-change
11332                      from gnus-mouse-face-prop nil end)
11333                     end))))
11334         ;; If no mouse-face prop on line we will have to = from = end,
11335         ;; so we highlight the entire line instead.
11336         (when (= (+ to 2) from)
11337           (setq from beg)
11338           (setq to end))
11339         (if gnus-newsgroup-selected-overlay
11340             ;; Move old overlay.
11341             (gnus-move-overlay
11342              gnus-newsgroup-selected-overlay from to (current-buffer))
11343           ;; Create new overlay.
11344           (gnus-overlay-put
11345            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11346            'face gnus-summary-selected-face))))))
11347
11348 (defvar gnus-summary-highlight-line-cached nil)
11349 (defvar gnus-summary-highlight-line-trigger nil)
11350
11351 (defun gnus-summary-highlight-line-0 ()
11352   (if (and (eq gnus-summary-highlight-line-trigger
11353                gnus-summary-highlight)
11354            gnus-summary-highlight-line-cached)
11355       gnus-summary-highlight-line-cached
11356     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11357           gnus-summary-highlight-line-cached
11358           (let* ((cond (list 'cond))
11359                  (c cond)
11360                  (list gnus-summary-highlight))
11361             (while list
11362               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11363                               nil))
11364               (setq c (cdr c)
11365                     list (cdr list)))
11366             (gnus-byte-compile (list 'lambda nil cond))))))
11367
11368 (defun gnus-summary-highlight-line ()
11369   "Highlight current line according to `gnus-summary-highlight'."
11370   (let* ((beg (point-at-bol))
11371          (article (or (gnus-summary-article-number) gnus-current-article))
11372          (score (or (cdr (assq article
11373                                gnus-newsgroup-scored))
11374                     gnus-summary-default-score 0))
11375          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11376          (inhibit-read-only t)
11377          (default gnus-summary-default-score)
11378          (default-high gnus-summary-default-high-score)
11379          (default-low gnus-summary-default-low-score)
11380          (uncached (and gnus-summary-use-undownloaded-faces
11381                         (memq article gnus-newsgroup-undownloaded)
11382                         (not (memq article gnus-newsgroup-cached)))))
11383     (let ((face (funcall (gnus-summary-highlight-line-0))))
11384       (unless (eq face (get-text-property beg 'face))
11385         (gnus-put-text-property-excluding-characters-with-faces
11386          beg (point-at-eol) 'face
11387          (setq face (if (boundp face) (symbol-value face) face)))
11388         (when gnus-summary-highlight-line-function
11389           (funcall gnus-summary-highlight-line-function article face))))))
11390
11391 (defun gnus-update-read-articles (group unread &optional compute)
11392   "Update the list of read articles in GROUP.
11393 UNREAD is a sorted list."
11394   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11395         (info (gnus-get-info group))
11396         (prev 1)
11397         read)
11398     (if (or (not info) (not active))
11399         ;; There is no info on this group if it was, in fact,
11400         ;; killed.  Gnus stores no information on killed groups, so
11401         ;; there's nothing to be done.
11402         ;; One could store the information somewhere temporarily,
11403         ;; perhaps...  Hmmm...
11404         ()
11405       ;; Remove any negative articles numbers.
11406       (while (and unread (< (car unread) 0))
11407         (setq unread (cdr unread)))
11408       ;; Remove any expired article numbers
11409       (while (and unread (< (car unread) (car active)))
11410         (setq unread (cdr unread)))
11411       ;; Compute the ranges of read articles by looking at the list of
11412       ;; unread articles.
11413       (while unread
11414         (when (/= (car unread) prev)
11415           (push (if (= prev (1- (car unread))) prev
11416                   (cons prev (1- (car unread))))
11417                 read))
11418         (setq prev (1+ (car unread)))
11419         (setq unread (cdr unread)))
11420       (when (<= prev (cdr active))
11421         (push (cons prev (cdr active)) read))
11422       (setq read (if (> (length read) 1) (nreverse read) read))
11423       (if compute
11424           read
11425         (save-excursion
11426           (let (setmarkundo)
11427             ;; Propagate the read marks to the backend.
11428             (when (gnus-check-backend-function 'request-set-mark group)
11429               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11430                     (add (gnus-remove-from-range read (gnus-info-read info))))
11431                 (when (or add del)
11432                   (unless (gnus-check-group group)
11433                     (error "Can't open server for %s" group))
11434                   (gnus-request-set-mark
11435                    group (delq nil (list (if add (list add 'add '(read)))
11436                                          (if del (list del 'del '(read))))))
11437                   (setq setmarkundo
11438                         `(gnus-request-set-mark
11439                           ,group
11440                           ',(delq nil (list
11441                                        (if del (list del 'add '(read)))
11442                                        (if add (list add 'del '(read))))))))))
11443             (set-buffer gnus-group-buffer)
11444             (gnus-undo-register
11445               `(progn
11446                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11447                  (gnus-info-set-read ',info ',(gnus-info-read info))
11448                  (gnus-get-unread-articles-in-group ',info
11449                                                     (gnus-active ,group))
11450                  (gnus-group-update-group ,group t)
11451                  ,setmarkundo))))
11452         ;; Enter this list into the group info.
11453         (gnus-info-set-read info read)
11454         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11455         (gnus-get-unread-articles-in-group info (gnus-active group))
11456         t))))
11457
11458 (defun gnus-offer-save-summaries ()
11459   "Offer to save all active summary buffers."
11460   (let (buffers)
11461     ;; Go through all buffers and find all summaries.
11462     (dolist (buffer (buffer-list))
11463       (when (and (setq buffer (buffer-name buffer))
11464                  (string-match "Summary" buffer)
11465                  (with-current-buffer buffer
11466                    ;; We check that this is, indeed, a summary buffer.
11467                    (and (eq major-mode 'gnus-summary-mode)
11468                         ;; Also make sure this isn't bogus.
11469                         gnus-newsgroup-prepared
11470                         ;; Also make sure that this isn't a
11471                         ;; dead summary buffer.
11472                         (not gnus-dead-summary-mode))))
11473         (push buffer buffers)))
11474     ;; Go through all these summary buffers and offer to save them.
11475     (when buffers
11476       (save-excursion
11477         (map-y-or-n-p
11478          "Update summary buffer %s? "
11479          (lambda (buf)
11480            (switch-to-buffer buf)
11481            (gnus-summary-exit))
11482          buffers)))))
11483
11484 (defun gnus-summary-setup-default-charset ()
11485   "Setup newsgroup default charset."
11486   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11487       (setq gnus-newsgroup-charset nil)
11488     (let* ((ignored-charsets
11489             (or gnus-newsgroup-ephemeral-ignored-charsets
11490                 (append
11491                  (and gnus-newsgroup-name
11492                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11493                  gnus-newsgroup-ignored-charsets))))
11494       (setq gnus-newsgroup-charset
11495             (or gnus-newsgroup-ephemeral-charset
11496                 (and gnus-newsgroup-name
11497                      (gnus-parameter-charset gnus-newsgroup-name))
11498                 gnus-default-charset))
11499       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11500            ignored-charsets))))
11501
11502 ;;;
11503 ;;; Mime Commands
11504 ;;;
11505
11506 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11507   "Display the current article buffer fully MIME-buttonized.
11508 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11509 treated as multipart/mixed."
11510   (interactive "P")
11511   (require 'gnus-art)
11512   (let ((gnus-unbuttonized-mime-types nil)
11513         (gnus-mime-display-multipart-as-mixed show-all-parts))
11514     (gnus-summary-show-article)))
11515
11516 (defun gnus-summary-repair-multipart (article)
11517   "Add a Content-Type header to a multipart article without one."
11518   (interactive (list (gnus-summary-article-number)))
11519   (gnus-with-article article
11520     (message-narrow-to-head)
11521     (message-remove-header "Mime-Version")
11522     (goto-char (point-max))
11523     (insert "Mime-Version: 1.0\n")
11524     (widen)
11525     (when (search-forward "\n--" nil t)
11526       (let ((separator (buffer-substring (point) (point-at-eol))))
11527         (message-narrow-to-head)
11528         (message-remove-header "Content-Type")
11529         (goto-char (point-max))
11530         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11531                         separator))
11532         (widen))))
11533   (let (gnus-mark-article-hook)
11534     (gnus-summary-select-article t t nil article)))
11535
11536 (defun gnus-summary-toggle-display-buttonized ()
11537   "Toggle the buttonizing of the article buffer."
11538   (interactive)
11539   (require 'gnus-art)
11540   (if (setq gnus-inhibit-mime-unbuttonizing
11541             (not gnus-inhibit-mime-unbuttonizing))
11542       (let ((gnus-unbuttonized-mime-types nil))
11543         (gnus-summary-show-article))
11544     (gnus-summary-show-article)))
11545
11546 ;;;
11547 ;;; Generic summary marking commands
11548 ;;;
11549
11550 (defvar gnus-summary-marking-alist
11551   '((read gnus-del-mark "d")
11552     (unread gnus-unread-mark "u")
11553     (ticked gnus-ticked-mark "!")
11554     (dormant gnus-dormant-mark "?")
11555     (expirable gnus-expirable-mark "e"))
11556   "An alist of names/marks/keystrokes.")
11557
11558 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11559 (defvar gnus-summary-mark-map)
11560
11561 (defun gnus-summary-make-all-marking-commands ()
11562   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11563   (dolist (elem gnus-summary-marking-alist)
11564     (apply 'gnus-summary-make-marking-command elem)))
11565
11566 (defun gnus-summary-make-marking-command (name mark keystroke)
11567   (let ((map (make-sparse-keymap)))
11568     (define-key gnus-summary-generic-mark-map keystroke map)
11569     (dolist (lway `((next "next" next nil "n")
11570                     (next-unread "next unread" next t "N")
11571                     (prev "previous" prev nil "p")
11572                     (prev-unread "previous unread" prev t "P")
11573                     (nomove "" nil nil ,keystroke)))
11574       (let ((func (gnus-summary-make-marking-command-1
11575                    mark (car lway) lway name)))
11576         (setq func (eval func))
11577         (define-key map (nth 4 lway) func)))))
11578
11579 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11580   `(defun ,(intern
11581             (format "gnus-summary-put-mark-as-%s%s"
11582                     name (if (eq way 'nomove)
11583                              ""
11584                            (concat "-" (symbol-name way)))))
11585      (n)
11586      ,(format
11587        "Mark the current article as %s%s.
11588 If N, the prefix, then repeat N times.
11589 If N is negative, move in reverse order.
11590 The difference between N and the actual number of articles marked is
11591 returned."
11592        name (cadr lway))
11593      (interactive "p")
11594      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11595
11596 (defun gnus-summary-generic-mark (n mark move unread)
11597   "Mark N articles with MARK."
11598   (unless (eq major-mode 'gnus-summary-mode)
11599     (error "This command can only be used in the summary buffer"))
11600   (gnus-summary-show-thread)
11601   (let ((nummove
11602          (cond
11603           ((eq move 'next) 1)
11604           ((eq move 'prev) -1)
11605           (t 0))))
11606     (if (zerop nummove)
11607         (setq n 1)
11608       (when (< n 0)
11609         (setq n (abs n)
11610               nummove (* -1 nummove))))
11611     (while (and (> n 0)
11612                 (gnus-summary-mark-article nil mark)
11613                 (zerop (gnus-summary-next-subject nummove unread t)))
11614       (setq n (1- n)))
11615     (when (/= 0 n)
11616       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11617     (gnus-summary-recenter)
11618     (gnus-summary-position-point)
11619     (gnus-set-mode-line 'summary)
11620     n))
11621
11622 (defun gnus-summary-insert-articles (articles)
11623   (when (setq articles
11624               (gnus-sorted-difference articles
11625                                       (mapcar (lambda (h)
11626                                                 (mail-header-number h))
11627                                               gnus-newsgroup-headers)))
11628     (setq gnus-newsgroup-headers
11629           (gnus-merge 'list
11630                       gnus-newsgroup-headers
11631                       (gnus-fetch-headers articles)
11632                       'gnus-article-sort-by-number))
11633     ;; Suppress duplicates?
11634     (when gnus-suppress-duplicates
11635       (gnus-dup-suppress-articles))
11636
11637     ;; We might want to build some more threads first.
11638     (when (and gnus-fetch-old-headers
11639                (eq gnus-headers-retrieved-by 'nov))
11640       (if (eq gnus-fetch-old-headers 'invisible)
11641           (gnus-build-all-threads)
11642         (gnus-build-old-threads)))
11643     ;; Let the Gnus agent mark articles as read.
11644     (when gnus-agent
11645       (gnus-agent-get-undownloaded-list))
11646     ;; Remove list identifiers from subject
11647     (when gnus-list-identifiers
11648       (gnus-summary-remove-list-identifiers))
11649     ;; First and last article in this newsgroup.
11650     (when gnus-newsgroup-headers
11651       (setq gnus-newsgroup-begin
11652             (mail-header-number (car gnus-newsgroup-headers))
11653             gnus-newsgroup-end
11654             (mail-header-number
11655              (gnus-last-element gnus-newsgroup-headers))))
11656     (when gnus-use-scoring
11657       (gnus-possibly-score-headers))))
11658
11659 (defun gnus-summary-insert-old-articles (&optional all)
11660   "Insert all old articles in this group.
11661 If ALL is non-nil, already read articles become readable.
11662 If ALL is a number, fetch this number of articles."
11663   (interactive "P")
11664   (prog1
11665       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11666             older len)
11667         (setq older
11668               ;; Some nntp servers lie about their active range.  When
11669               ;; this happens, the active range can be in the millions.
11670               ;; Use a compressed range to avoid creating a huge list.
11671               (gnus-range-difference (list gnus-newsgroup-active) old))
11672         (setq len (gnus-range-length older))
11673         (cond
11674          ((null older) nil)
11675          ((numberp all)
11676           (if (< all len)
11677               (let ((older-range (nreverse older)))
11678                 (setq older nil)
11679
11680                 (while (> all 0)
11681                   (let* ((r (pop older-range))
11682                          (min (if (numberp r) r (car r)))
11683                          (max (if (numberp r) r (cdr r))))
11684                     (while (and (<= min max)
11685                                 (> all 0))
11686                       (push max older)
11687                       (setq all (1- all)
11688                             max (1- max))))))
11689             (setq older (gnus-uncompress-range older))))
11690          (all
11691           (setq older (gnus-uncompress-range older)))
11692          (t
11693           (when (and (numberp gnus-large-newsgroup)
11694                    (> len gnus-large-newsgroup))
11695               (let* ((cursor-in-echo-area nil)
11696                      (initial (gnus-parameter-large-newsgroup-initial
11697                                gnus-newsgroup-name))
11698                      (input
11699                       (read-string
11700                        (format
11701                         "How many articles from %s (%s %d): "
11702                         (gnus-limit-string
11703                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11704                         (if initial "max" "default")
11705                         len)
11706                        (if initial
11707                            (cons (number-to-string initial)
11708                                  0)))))
11709                 (unless (string-match "^[ \t]*$" input)
11710                   (setq all (string-to-number input))
11711                   (if (< all len)
11712                       (let ((older-range (nreverse older)))
11713                         (setq older nil)
11714
11715                         (while (> all 0)
11716                           (let* ((r (pop older-range))
11717                                  (min (if (numberp r) r (car r)))
11718                                  (max (if (numberp r) r (cdr r))))
11719                             (while (and (<= min max)
11720                                         (> all 0))
11721                               (push max older)
11722                               (setq all (1- all)
11723                                     max (1- max))))))))))
11724           (setq older (gnus-uncompress-range older))))
11725         (if (not older)
11726             (message "No old news.")
11727           (gnus-summary-insert-articles older)
11728           (gnus-summary-limit (gnus-sorted-nunion old older))))
11729     (gnus-summary-position-point)))
11730
11731 (defun gnus-summary-insert-new-articles ()
11732   "Insert all new articles in this group."
11733   (interactive)
11734   (prog1
11735       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11736             (old-active gnus-newsgroup-active)
11737             (nnmail-fetched-sources (list t))
11738             i new)
11739         (setq gnus-newsgroup-active
11740               (gnus-activate-group gnus-newsgroup-name 'scan))
11741         (setq i (cdr gnus-newsgroup-active))
11742         (while (> i (cdr old-active))
11743           (push i new)
11744           (decf i))
11745         (if (not new)
11746             (message "No gnus is bad news")
11747           (gnus-summary-insert-articles new)
11748           (setq gnus-newsgroup-unreads
11749                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11750           (gnus-summary-limit (gnus-sorted-nunion old new))))
11751     (gnus-summary-position-point)))
11752
11753 (gnus-summary-make-all-marking-commands)
11754
11755 (gnus-ems-redefine)
11756
11757 (provide 'gnus-sum)
11758
11759 (run-hooks 'gnus-sum-load-hook)
11760
11761 ;; Local Variables:
11762 ;; coding: iso-8859-1
11763 ;; End:
11764
11765 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11766 ;;; gnus-sum.el ends here