(nneething-file-name): Don't create spurions
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary t
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715                         
716
717 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
718   "*List of functions used for sorting articles in the summary buffer.
719
720 Each function takes two articles and returns non-nil if the first
721 article should be sorted before the other.  If you use more than one
722 function, the primary sort function should be the last.  You should
723 probably always include `gnus-article-sort-by-number' in the list of
724 sorting functions -- preferably first.  Also note that sorting by date
725 is often much slower than sorting by number, and the sorting order is
726 very similar.  (Sorting by date means sorting by the time the message
727 was sent, sorting by number means sorting by arrival time.)
728
729 Each item can also be a list `(not F)' where F is a function;
730 this reverses the sort order.
731
732 Ready-made functions include `gnus-article-sort-by-number',
733 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
734 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
735 and `gnus-article-sort-by-score'.
736
737 When threading is turned on, the variable `gnus-thread-sort-functions'
738 controls how articles are sorted."
739   :group 'gnus-summary-sort
740   :type '(repeat (gnus-widget-reversible
741                   (choice (function-item gnus-article-sort-by-number)
742                           (function-item gnus-article-sort-by-author)
743                           (function-item gnus-article-sort-by-subject)
744                           (function-item gnus-article-sort-by-date)
745                           (function-item gnus-article-sort-by-score)
746                           (function-item gnus-article-sort-by-random)
747                           (function :tag "other"))
748                   (boolean :tag "Reverse order"))))
749
750
751 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
752   "*List of functions used for sorting threads in the summary buffer.
753 By default, threads are sorted by article number.
754
755 Each function takes two threads and returns non-nil if the first
756 thread should be sorted before the other.  If you use more than one
757 function, the primary sort function should be the last.  You should
758 probably always include `gnus-thread-sort-by-number' in the list of
759 sorting functions -- preferably first.  Also note that sorting by date
760 is often much slower than sorting by number, and the sorting order is
761 very similar.  (Sorting by date means sorting by the time the message
762 was sent, sorting by number means sorting by arrival time.)
763
764 Each list item can also be a list `(not F)' where F is a
765 function; this specifies reversed sort order.
766
767 Ready-made functions include `gnus-thread-sort-by-number',
768 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
769 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
770 `gnus-thread-sort-by-most-recent-number',
771 `gnus-thread-sort-by-most-recent-date',
772 `gnus-thread-sort-by-random', and
773 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
774
775 When threading is turned off, the variable
776 `gnus-article-sort-functions' controls how articles are sorted."
777   :group 'gnus-summary-sort
778   :type '(repeat 
779           (gnus-widget-reversible
780            (choice (function-item gnus-thread-sort-by-number)
781                    (function-item gnus-thread-sort-by-author)
782                    (function-item gnus-thread-sort-by-subject)
783                    (function-item gnus-thread-sort-by-date)
784                    (function-item gnus-thread-sort-by-score)
785                    (function-item gnus-thread-sort-by-most-recent-number)
786                    (function-item gnus-thread-sort-by-most-recent-date)
787                    (function-item gnus-thread-sort-by-random)
788                    (function-item gnus-thread-sort-by-total-score)
789                    (function :tag "other"))
790            (boolean :tag "Reverse order"))))
791
792 (defcustom gnus-thread-score-function '+
793   "*Function used for calculating the total score of a thread.
794
795 The function is called with the scores of the article and each
796 subthread and should then return the score of the thread.
797
798 Some functions you can use are `+', `max', or `min'."
799   :group 'gnus-summary-sort
800   :type 'function)
801
802 (defcustom gnus-summary-expunge-below nil
803   "All articles that have a score less than this variable will be expunged.
804 This variable is local to the summary buffers."
805   :group 'gnus-score-default
806   :type '(choice (const :tag "off" nil)
807                  integer))
808
809 (defcustom gnus-thread-expunge-below nil
810   "All threads that have a total score less than this variable will be expunged.
811 See `gnus-thread-score-function' for en explanation of what a
812 \"thread score\" is.
813
814 This variable is local to the summary buffers."
815   :group 'gnus-threading
816   :group 'gnus-score-default
817   :type '(choice (const :tag "off" nil)
818                  integer))
819
820 (defcustom gnus-summary-mode-hook nil
821   "*A hook for Gnus summary mode.
822 This hook is run before any variables are set in the summary buffer."
823   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
824   :group 'gnus-summary-various
825   :type 'hook)
826
827 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
828 (when (featurep 'xemacs)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
830   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
831   (add-hook 'gnus-summary-mode-hook
832             'gnus-xmas-switch-horizontal-scrollbar-off))
833
834 (defcustom gnus-summary-menu-hook nil
835   "*Hook run after the creation of the summary mode menu."
836   :group 'gnus-summary-visual
837   :type 'hook)
838
839 (defcustom gnus-summary-exit-hook nil
840   "*A hook called on exit from the summary buffer.
841 It will be called with point in the group buffer."
842   :group 'gnus-summary-exit
843   :type 'hook)
844
845 (defcustom gnus-summary-prepare-hook nil
846   "*A hook called after the summary buffer has been generated.
847 If you want to modify the summary buffer, you can use this hook."
848   :group 'gnus-summary-various
849   :type 'hook)
850
851 (defcustom gnus-summary-prepared-hook nil
852   "*A hook called as the last thing after the summary buffer has been generated."
853   :group 'gnus-summary-various
854   :type 'hook)
855
856 (defcustom gnus-summary-generate-hook nil
857   "*A hook run just before generating the summary buffer.
858 This hook is commonly used to customize threading variables and the
859 like."
860   :group 'gnus-summary-various
861   :type 'hook)
862
863 (defcustom gnus-select-group-hook nil
864   "*A hook called when a newsgroup is selected.
865
866 If you'd like to simplify subjects like the
867 `gnus-summary-next-same-subject' command does, you can use the
868 following hook:
869
870  (add-hook gnus-select-group-hook
871            (lambda ()
872              (mapcar (lambda (header)
873                        (mail-header-set-subject
874                         header
875                         (gnus-simplify-subject
876                          (mail-header-subject header) 're-only)))
877                      gnus-newsgroup-headers)))"
878   :group 'gnus-group-select
879   :type 'hook)
880
881 (defcustom gnus-select-article-hook nil
882   "*A hook called when an article is selected."
883   :group 'gnus-summary-choose
884   :options '(gnus-agent-fetch-selected-article)
885   :type 'hook)
886
887 (defcustom gnus-visual-mark-article-hook
888   (list 'gnus-highlight-selected-summary)
889   "*Hook run after selecting an article in the summary buffer.
890 It is meant to be used for highlighting the article in some way.  It
891 is not run if `gnus-visual' is nil."
892   :group 'gnus-summary-visual
893   :type 'hook)
894
895 (defcustom gnus-parse-headers-hook nil
896   "*A hook called before parsing the headers."
897   :group 'gnus-various
898   :type 'hook)
899
900 (defcustom gnus-exit-group-hook nil
901   "*A hook called when exiting summary mode.
902 This hook is not called from the non-updating exit commands like `Q'."
903   :group 'gnus-various
904   :type 'hook)
905
906 (defcustom gnus-summary-update-hook
907   (list 'gnus-summary-highlight-line)
908   "*A hook called when a summary line is changed.
909 The hook will not be called if `gnus-visual' is nil.
910
911 The default function `gnus-summary-highlight-line' will
912 highlight the line according to the `gnus-summary-highlight'
913 variable."
914   :group 'gnus-summary-visual
915   :type 'hook)
916
917 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
918   "*A hook called when an article is selected for the first time.
919 The hook is intended to mark an article as read (or unread)
920 automatically when it is selected."
921   :group 'gnus-summary-choose
922   :type 'hook)
923
924 (defcustom gnus-group-no-more-groups-hook nil
925   "*A hook run when returning to group mode having no more (unread) groups."
926   :group 'gnus-group-select
927   :type 'hook)
928
929 (defcustom gnus-ps-print-hook nil
930   "*A hook run before ps-printing something from Gnus."
931   :group 'gnus-summary
932   :type 'hook)
933
934 (defcustom gnus-summary-article-move-hook nil
935   "*A hook called after an article is moved, copied, respooled, or crossposted."
936   :group 'gnus-summary
937   :type 'hook)
938
939 (defcustom gnus-summary-article-delete-hook nil
940   "*A hook called after an article is deleted."
941   :group 'gnus-summary
942   :type 'hook)
943
944 (defcustom gnus-summary-article-expire-hook nil
945   "*A hook called after an article is expired."
946   :group 'gnus-summary
947   :type 'hook)
948
949 (defcustom gnus-summary-display-arrow
950   (and (fboundp 'display-graphic-p)
951        (display-graphic-p))
952   "*If non-nil, display an arrow highlighting the current article."
953   :version "21.1"
954   :group 'gnus-summary
955   :type 'boolean)
956
957 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
958   "Face used for highlighting the current article in the summary buffer."
959   :group 'gnus-summary-visual
960   :type 'face)
961
962 (defvar gnus-tmp-downloaded nil)
963
964 (defcustom gnus-summary-highlight
965   '(((eq mark gnus-canceled-mark)
966      . gnus-summary-cancelled-face)
967     ((and uncached (> score default-high))
968      . gnus-summary-high-undownloaded-face)
969     ((and uncached (< score default-low))
970      . gnus-summary-low-undownloaded-face)
971     (uncached
972      . gnus-summary-normal-undownloaded-face)
973     ((and (> score default-high)
974           (or (eq mark gnus-dormant-mark)
975               (eq mark gnus-ticked-mark)))
976      . gnus-summary-high-ticked-face)
977     ((and (< score default-low)
978           (or (eq mark gnus-dormant-mark)
979               (eq mark gnus-ticked-mark)))
980      . gnus-summary-low-ticked-face)
981     ((or (eq mark gnus-dormant-mark)
982          (eq mark gnus-ticked-mark))
983      . gnus-summary-normal-ticked-face)
984     ((and (> score default-high) (eq mark gnus-ancient-mark))
985      . gnus-summary-high-ancient-face)
986     ((and (< score default-low) (eq mark gnus-ancient-mark))
987      . gnus-summary-low-ancient-face)
988     ((eq mark gnus-ancient-mark)
989      . gnus-summary-normal-ancient-face)
990     ((and (> score default-high) (eq mark gnus-unread-mark))
991      . gnus-summary-high-unread-face)
992     ((and (< score default-low) (eq mark gnus-unread-mark))
993      . gnus-summary-low-unread-face)
994     ((eq mark gnus-unread-mark)
995      . gnus-summary-normal-unread-face)
996     ((> score default-high)
997      . gnus-summary-high-read-face)
998     ((< score default-low)
999      . gnus-summary-low-read-face)
1000     (t
1001      . gnus-summary-normal-read-face))
1002   "*Controls the highlighting of summary buffer lines.
1003
1004 A list of (FORM . FACE) pairs.  When deciding how a a particular
1005 summary line should be displayed, each form is evaluated.  The content
1006 of the face field after the first true form is used.  You can change
1007 how those summary lines are displayed, by editing the face field.
1008
1009 You can use the following variables in the FORM field.
1010
1011 score:        The article's score
1012 default:      The default article score.
1013 default-high: The default score for high scored articles.
1014 default-low:  The default score for low scored articles.
1015 below:        The score below which articles are automatically marked as read.
1016 mark:         The article's mark.
1017 uncached:     Non-nil if the article is uncached."
1018   :group 'gnus-summary-visual
1019   :type '(repeat (cons (sexp :tag "Form" nil)
1020                        face)))
1021
1022 (defcustom gnus-alter-header-function nil
1023   "Function called to allow alteration of article header structures.
1024 The function is called with one parameter, the article header vector,
1025 which it may alter in any way."
1026   :type '(choice (const :tag "None" nil)
1027                  function)
1028   :group 'gnus-summary)
1029
1030 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1031   "Variable that says which function should be used to decode a string with encoded words.")
1032
1033 (defcustom gnus-extra-headers '(To Newsgroups)
1034   "*Extra headers to parse."
1035   :version "21.1"
1036   :group 'gnus-summary
1037   :type '(repeat symbol))
1038
1039 (defcustom gnus-ignored-from-addresses
1040   (and user-mail-address (regexp-quote user-mail-address))
1041   "*Regexp of From headers that may be suppressed in favor of To headers."
1042   :version "21.1"
1043   :group 'gnus-summary
1044   :type 'regexp)
1045
1046 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1047   "List of charsets that should be ignored.
1048 When these charsets are used in the \"charset\" parameter, the
1049 default charset will be used instead."
1050   :version "21.1"
1051   :type '(repeat symbol)
1052   :group 'gnus-charset)
1053
1054 (gnus-define-group-parameter
1055  ignored-charsets
1056  :type list
1057  :function-document
1058  "Return the ignored charsets of GROUP."
1059  :variable gnus-group-ignored-charsets-alist
1060  :variable-default
1061  '(("alt\\.chinese\\.text" iso-8859-1))
1062  :variable-document
1063  "Alist of regexps (to match group names) and charsets that should be ignored.
1064 When these charsets are used in the \"charset\" parameter, the
1065 default charset will be used instead."
1066  :variable-group gnus-charset
1067  :variable-type '(repeat (cons (regexp :tag "Group")
1068                                (repeat symbol)))
1069  :parameter-type '(choice :tag "Ignored charsets"
1070                           :value nil
1071                           (repeat (symbol)))
1072  :parameter-document       "\
1073 List of charsets that should be ignored.
1074
1075 When these charsets are used in the \"charset\" parameter, the
1076 default charset will be used instead.")
1077
1078 (defcustom gnus-group-highlight-words-alist nil
1079   "Alist of group regexps and highlight regexps.
1080 This variable uses the same syntax as `gnus-emphasis-alist'."
1081   :version "21.1"
1082   :type '(repeat (cons (regexp :tag "Group")
1083                        (repeat (list (regexp :tag "Highlight regexp")
1084                                      (number :tag "Group for entire word" 0)
1085                                      (number :tag "Group for displayed part" 0)
1086                                      (symbol :tag "Face"
1087                                              gnus-emphasis-highlight-words)))))
1088   :group 'gnus-summary-visual)
1089
1090 (defcustom gnus-summary-show-article-charset-alist
1091   nil
1092   "Alist of number and charset.
1093 The article will be shown with the charset corresponding to the
1094 numbered argument.
1095 For example: ((1 . cn-gb-2312) (2 . big5))."
1096   :version "21.1"
1097   :type '(repeat (cons (number :tag "Argument" 1)
1098                        (symbol :tag "Charset")))
1099   :group 'gnus-charset)
1100
1101 (defcustom gnus-preserve-marks t
1102   "Whether marks are preserved when moving, copying and respooling messages."
1103   :version "21.1"
1104   :type 'boolean
1105   :group 'gnus-summary-marks)
1106
1107 (defcustom gnus-alter-articles-to-read-function nil
1108   "Function to be called to alter the list of articles to be selected."
1109   :type '(choice (const nil) function)
1110   :group 'gnus-summary)
1111
1112 (defcustom gnus-orphan-score nil
1113   "*All orphans get this score added.  Set in the score file."
1114   :group 'gnus-score-default
1115   :type '(choice (const nil)
1116                  integer))
1117
1118 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1119   "*A regexp to match MIME parts when saving multiple parts of a
1120 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1121 This regexp will be used by default when prompting the user for which
1122 type of files to save."
1123   :group 'gnus-summary
1124   :type 'regexp)
1125
1126 (defcustom gnus-read-all-available-headers nil
1127   "Whether Gnus should parse all headers made available to it.
1128 This is mostly relevant for slow back ends where the user may
1129 wish to widen the summary buffer to include all headers
1130 that were fetched.  Say, for nnultimate groups."
1131   :group 'gnus-summary
1132   :type '(choice boolean regexp))
1133
1134 (defcustom gnus-summary-muttprint-program "muttprint"
1135   "Command (and optional arguments) used to run Muttprint."
1136   :version "21.3"
1137   :group 'gnus-summary
1138   :type 'string)
1139
1140 (defcustom gnus-article-loose-mime nil
1141   "If non-nil, don't require MIME-Version header.
1142 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1143 supply the MIME-Version header or deliberately strip it From the mail.
1144 Set it to non-nil, Gnus will treat some articles as MIME even if
1145 the MIME-Version header is missed."
1146   :version "21.3"
1147   :type 'boolean
1148   :group 'gnus-article-mime)
1149
1150 (defcustom gnus-article-emulate-mime t
1151   "If non-nil, use MIME emulation for uuencode and the like.
1152 This means that Gnus will search message bodies for text that look
1153 like uuencoded bits, yEncoded bits, and so on, and present that using
1154 the normal Gnus MIME machinery."
1155   :type 'boolean
1156   :group 'gnus-article-mime)
1157
1158 ;;; Internal variables
1159
1160 (defvar gnus-summary-display-cache nil)
1161 (defvar gnus-article-mime-handles nil)
1162 (defvar gnus-article-decoded-p nil)
1163 (defvar gnus-article-charset nil)
1164 (defvar gnus-article-ignored-charsets nil)
1165 (defvar gnus-scores-exclude-files nil)
1166 (defvar gnus-page-broken nil)
1167
1168 (defvar gnus-original-article nil)
1169 (defvar gnus-article-internal-prepare-hook nil)
1170 (defvar gnus-newsgroup-process-stack nil)
1171
1172 (defvar gnus-thread-indent-array nil)
1173 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1174 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1175   "Function called to sort the articles within a thread after it has been gathered together.")
1176
1177 (defvar gnus-summary-save-parts-type-history nil)
1178 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1179
1180 ;; Avoid highlighting in kill files.
1181 (defvar gnus-summary-inhibit-highlight nil)
1182 (defvar gnus-newsgroup-selected-overlay nil)
1183 (defvar gnus-inhibit-limiting nil)
1184 (defvar gnus-newsgroup-adaptive-score-file nil)
1185 (defvar gnus-current-score-file nil)
1186 (defvar gnus-current-move-group nil)
1187 (defvar gnus-current-copy-group nil)
1188 (defvar gnus-current-crosspost-group nil)
1189 (defvar gnus-newsgroup-display nil)
1190
1191 (defvar gnus-newsgroup-dependencies nil)
1192 (defvar gnus-newsgroup-adaptive nil)
1193 (defvar gnus-summary-display-article-function nil)
1194 (defvar gnus-summary-highlight-line-function nil
1195   "Function called after highlighting a summary line.")
1196
1197 (defvar gnus-summary-line-format-alist
1198   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1199     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1200     (?s gnus-tmp-subject-or-nil ?s)
1201     (?n gnus-tmp-name ?s)
1202     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1203         ?s)
1204     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1205             gnus-tmp-from) ?s)
1206     (?F gnus-tmp-from ?s)
1207     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1208     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1209     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1210     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1211     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1212     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1213     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1214     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1215     (?L gnus-tmp-lines ?s)
1216     (?O gnus-tmp-downloaded ?c)
1217     (?I gnus-tmp-indentation ?s)
1218     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1219     (?R gnus-tmp-replied ?c)
1220     (?\[ gnus-tmp-opening-bracket ?c)
1221     (?\] gnus-tmp-closing-bracket ?c)
1222     (?\> (make-string gnus-tmp-level ? ) ?s)
1223     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1224     (?i gnus-tmp-score ?d)
1225     (?z gnus-tmp-score-char ?c)
1226     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1227     (?U gnus-tmp-unread ?c)
1228     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1229         ?s)
1230     (?t (gnus-summary-number-of-articles-in-thread
1231          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1232         ?d)
1233     (?e (gnus-summary-number-of-articles-in-thread
1234          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1235         ?c)
1236     (?u gnus-tmp-user-defined ?s)
1237     (?P (gnus-pick-line-number) ?d)
1238     (?B gnus-tmp-thread-tree-header-string ?s)
1239     (user-date (gnus-user-date
1240                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1241   "An alist of format specifications that can appear in summary lines.
1242 These are paired with what variables they correspond with, along with
1243 the type of the variable (string, integer, character, etc).")
1244
1245 (defvar gnus-summary-dummy-line-format-alist
1246   `((?S gnus-tmp-subject ?s)
1247     (?N gnus-tmp-number ?d)
1248     (?u gnus-tmp-user-defined ?s)))
1249
1250 (defvar gnus-summary-mode-line-format-alist
1251   `((?G gnus-tmp-group-name ?s)
1252     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1253     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1254     (?A gnus-tmp-article-number ?d)
1255     (?Z gnus-tmp-unread-and-unselected ?s)
1256     (?V gnus-version ?s)
1257     (?U gnus-tmp-unread-and-unticked ?d)
1258     (?S gnus-tmp-subject ?s)
1259     (?e gnus-tmp-unselected ?d)
1260     (?u gnus-tmp-user-defined ?s)
1261     (?d (length gnus-newsgroup-dormant) ?d)
1262     (?t (length gnus-newsgroup-marked) ?d)
1263     (?h (length gnus-newsgroup-spam-marked) ?d)
1264     (?r (length gnus-newsgroup-reads) ?d)
1265     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1266     (?E gnus-newsgroup-expunged-tally ?d)
1267     (?s (gnus-current-score-file-nondirectory) ?s)))
1268
1269 (defvar gnus-last-search-regexp nil
1270   "Default regexp for article search command.")
1271
1272 (defvar gnus-last-shell-command nil
1273   "Default shell command on article.")
1274
1275 (defvar gnus-newsgroup-agentized nil
1276   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1277 (defvar gnus-newsgroup-begin nil)
1278 (defvar gnus-newsgroup-end nil)
1279 (defvar gnus-newsgroup-last-rmail nil)
1280 (defvar gnus-newsgroup-last-mail nil)
1281 (defvar gnus-newsgroup-last-folder nil)
1282 (defvar gnus-newsgroup-last-file nil)
1283 (defvar gnus-newsgroup-auto-expire nil)
1284 (defvar gnus-newsgroup-active nil)
1285
1286 (defvar gnus-newsgroup-data nil)
1287 (defvar gnus-newsgroup-data-reverse nil)
1288 (defvar gnus-newsgroup-limit nil)
1289 (defvar gnus-newsgroup-limits nil)
1290 (defvar gnus-summary-use-undownloaded-faces nil)
1291
1292 (defvar gnus-newsgroup-unreads nil
1293   "Sorted list of unread articles in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-unselected nil
1296   "Sorted list of unselected unread articles in the current newsgroup.")
1297
1298 (defvar gnus-newsgroup-reads nil
1299   "Alist of read articles and article marks in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-expunged-tally nil)
1302
1303 (defvar gnus-newsgroup-marked nil
1304   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1305
1306 (defvar gnus-newsgroup-spam-marked nil
1307   "List of ranges of articles that have been marked as spam.")
1308
1309 (defvar gnus-newsgroup-killed nil
1310   "List of ranges of articles that have been through the scoring process.")
1311
1312 (defvar gnus-newsgroup-cached nil
1313   "Sorted list of articles that come from the article cache.")
1314
1315 (defvar gnus-newsgroup-saved nil
1316   "List of articles that have been saved.")
1317
1318 (defvar gnus-newsgroup-kill-headers nil)
1319
1320 (defvar gnus-newsgroup-replied nil
1321   "List of articles that have been replied to in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-forwarded nil
1324   "List of articles that have been forwarded in the current newsgroup.")
1325
1326 (defvar gnus-newsgroup-recent nil
1327   "List of articles that have are recent in the current newsgroup.")
1328
1329 (defvar gnus-newsgroup-expirable nil
1330   "Sorted list of articles in the current newsgroup that can be expired.")
1331
1332 (defvar gnus-newsgroup-processable nil
1333   "List of articles in the current newsgroup that can be processed.")
1334
1335 (defvar gnus-newsgroup-downloadable nil
1336   "Sorted list of articles in the current newsgroup that can be processed.")
1337
1338 (defvar gnus-newsgroup-unfetched nil
1339   "Sorted list of articles in the current newsgroup whose headers have
1340 not been fetched into the agent.
1341
1342 This list will always be a subset of gnus-newsgroup-undownloaded.")
1343
1344 (defvar gnus-newsgroup-undownloaded nil
1345   "List of articles in the current newsgroup that haven't been downloaded.")
1346
1347 (defvar gnus-newsgroup-unsendable nil
1348   "List of articles in the current newsgroup that won't be sent.")
1349
1350 (defvar gnus-newsgroup-bookmarks nil
1351   "List of articles in the current newsgroup that have bookmarks.")
1352
1353 (defvar gnus-newsgroup-dormant nil
1354   "Sorted list of dormant articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-unseen nil
1357   "List of unseen articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-seen nil
1360   "Range of seen articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-articles nil
1363   "List of articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-scored nil
1366   "List of scored articles in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-headers nil
1369   "List of article headers in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-threads nil)
1372
1373 (defvar gnus-newsgroup-prepared nil
1374   "Whether the current group has been prepared properly.")
1375
1376 (defvar gnus-newsgroup-ancient nil
1377   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-sparse nil)
1380
1381 (defvar gnus-current-article nil)
1382 (defvar gnus-article-current nil)
1383 (defvar gnus-current-headers nil)
1384 (defvar gnus-have-all-headers nil)
1385 (defvar gnus-last-article nil)
1386 (defvar gnus-newsgroup-history nil)
1387 (defvar gnus-newsgroup-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-charset nil)
1389 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1390
1391 (defvar gnus-article-before-search nil)
1392
1393 (defvar gnus-summary-local-variables
1394   '(gnus-newsgroup-name
1395     gnus-newsgroup-begin gnus-newsgroup-end
1396     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1397     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1398     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1399     gnus-newsgroup-unselected gnus-newsgroup-marked
1400     gnus-newsgroup-spam-marked
1401     gnus-newsgroup-reads gnus-newsgroup-saved
1402     gnus-newsgroup-replied gnus-newsgroup-forwarded
1403     gnus-newsgroup-recent
1404     gnus-newsgroup-expirable
1405     gnus-newsgroup-processable gnus-newsgroup-killed
1406     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1407     gnus-newsgroup-unfetched
1408     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1409     gnus-newsgroup-seen gnus-newsgroup-articles
1410     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1411     gnus-newsgroup-headers gnus-newsgroup-threads
1412     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1413     gnus-current-article gnus-current-headers gnus-have-all-headers
1414     gnus-last-article gnus-article-internal-prepare-hook
1415     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1416     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1417     gnus-thread-expunge-below
1418     gnus-score-alist gnus-current-score-file
1419     (gnus-summary-expunge-below . global)
1420     (gnus-summary-mark-below . global)
1421     (gnus-orphan-score . global)
1422     gnus-newsgroup-active gnus-scores-exclude-files
1423     gnus-newsgroup-history gnus-newsgroup-ancient
1424     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1425     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1426     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1427     (gnus-newsgroup-expunged-tally . 0)
1428     gnus-cache-removable-articles gnus-newsgroup-cached
1429     gnus-newsgroup-data gnus-newsgroup-data-reverse
1430     gnus-newsgroup-limit gnus-newsgroup-limits
1431     gnus-newsgroup-charset gnus-newsgroup-display
1432     gnus-summary-use-undownloaded-faces)
1433   "Variables that are buffer-local to the summary buffers.")
1434
1435 (defvar gnus-newsgroup-variables nil
1436   "A list of variables that have separate values in different newsgroups.
1437 A list of newsgroup (summary buffer) local variables, or cons of
1438 variables and their default expressions to be evalled (when the default
1439 values are not nil), that should be made global while the summary buffer
1440 is active.
1441
1442 Note: The default expressions will be evaluated (using function `eval')
1443 before assignment to the local variable rather than just assigned to it.
1444 If the default expression is the symbol `global', that symbol will not
1445 be evaluated but the global value of the local variable will be used
1446 instead.
1447
1448 These variables can be used to set variables in the group parameters
1449 while still allowing them to affect operations done in other buffers.
1450 For example:
1451
1452 \(setq gnus-newsgroup-variables
1453      '(message-use-followup-to
1454        (gnus-visible-headers .
1455          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1456 ")
1457
1458 ;; Byte-compiler warning.
1459 (eval-when-compile
1460   ;; Bind features so that require will believe that gnus-sum has
1461   ;; already been loaded (avoids infinite recursion)
1462   (let ((features (cons 'gnus-sum features)))
1463     ;; Several of the declarations in gnus-sum are needed to load the
1464     ;; following files. Right now, these definitions have been
1465     ;; compiled but not defined (evaluated).  We could either do a
1466     ;; eval-and-compile about all of the declarations or evaluate the
1467     ;; source file.
1468     (if (boundp 'gnus-newsgroup-variables)
1469         nil
1470       (load "gnus-sum.el" t t t))
1471     (require 'gnus)
1472     (require 'gnus-agent)
1473     (require 'gnus-art)))
1474
1475 ;; MIME stuff.
1476
1477 (defvar gnus-decode-encoded-word-methods
1478   '(mail-decode-encoded-word-string)
1479   "List of methods used to decode encoded words.
1480
1481 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1482 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1483 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1484 whose names match REGEXP.
1485
1486 For example:
1487 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1488  mail-decode-encoded-word-string
1489  (\"chinese\" . rfc1843-decode-string))")
1490
1491 (defvar gnus-decode-encoded-word-methods-cache nil)
1492
1493 (defun gnus-multi-decode-encoded-word-string (string)
1494   "Apply the functions from `gnus-encoded-word-methods' that match."
1495   (unless (and gnus-decode-encoded-word-methods-cache
1496                (eq gnus-newsgroup-name
1497                    (car gnus-decode-encoded-word-methods-cache)))
1498     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1499     (mapcar (lambda (x)
1500               (if (symbolp x)
1501                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1502                 (if (and gnus-newsgroup-name
1503                          (string-match (car x) gnus-newsgroup-name))
1504                     (nconc gnus-decode-encoded-word-methods-cache
1505                            (list (cdr x))))))
1506             gnus-decode-encoded-word-methods))
1507   (let ((xlist gnus-decode-encoded-word-methods-cache))
1508     (pop xlist)
1509     (while xlist
1510       (setq string (funcall (pop xlist) string))))
1511   string)
1512
1513 ;; Subject simplification.
1514
1515 (defun gnus-simplify-whitespace (str)
1516   "Remove excessive whitespace from STR."
1517   ;; Multiple spaces.
1518   (while (string-match "[ \t][ \t]+" str)
1519     (setq str (concat (substring str 0 (match-beginning 0))
1520                         " "
1521                         (substring str (match-end 0)))))
1522   ;; Leading spaces.
1523   (when (string-match "^[ \t]+" str)
1524     (setq str (substring str (match-end 0))))
1525   ;; Trailing spaces.
1526   (when (string-match "[ \t]+$" str)
1527     (setq str (substring str 0 (match-beginning 0))))
1528   str)
1529
1530 (defun gnus-simplify-all-whitespace (str)
1531   "Remove all whitespace from STR."
1532   (while (string-match "[ \t\n]+" str)
1533     (setq str (replace-match "" nil nil str)))
1534   str)
1535
1536 (defsubst gnus-simplify-subject-re (subject)
1537   "Remove \"Re:\" from subject lines."
1538   (if (string-match message-subject-re-regexp subject)
1539       (substring subject (match-end 0))
1540     subject))
1541
1542 (defun gnus-simplify-subject (subject &optional re-only)
1543   "Remove `Re:' and words in parentheses.
1544 If RE-ONLY is non-nil, strip leading `Re:'s only."
1545   (let ((case-fold-search t))           ;Ignore case.
1546     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1547     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1548       (setq subject (substring subject (match-end 0))))
1549     ;; Remove uninteresting prefixes.
1550     (when (and (not re-only)
1551                gnus-simplify-ignored-prefixes
1552                (string-match gnus-simplify-ignored-prefixes subject))
1553       (setq subject (substring subject (match-end 0))))
1554     ;; Remove words in parentheses from end.
1555     (unless re-only
1556       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1557         (setq subject (substring subject 0 (match-beginning 0)))))
1558     ;; Return subject string.
1559     subject))
1560
1561 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1562 ;; all whitespace.
1563 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1564   (goto-char (point-min))
1565   (while (re-search-forward regexp nil t)
1566     (replace-match (or newtext ""))))
1567
1568 (defun gnus-simplify-buffer-fuzzy ()
1569   "Simplify string in the buffer fuzzily.
1570 The string in the accessible portion of the current buffer is simplified.
1571 It is assumed to be a single-line subject.
1572 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1573 matter is removed.  Additional things can be deleted by setting
1574 `gnus-simplify-subject-fuzzy-regexp'."
1575   (let ((case-fold-search t)
1576         (modified-tick))
1577     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1578
1579     (while (not (eq modified-tick (buffer-modified-tick)))
1580       (setq modified-tick (buffer-modified-tick))
1581       (cond
1582        ((listp gnus-simplify-subject-fuzzy-regexp)
1583         (mapcar 'gnus-simplify-buffer-fuzzy-step
1584                 gnus-simplify-subject-fuzzy-regexp))
1585        (gnus-simplify-subject-fuzzy-regexp
1586         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1587       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1588       (gnus-simplify-buffer-fuzzy-step
1589        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1590       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1591
1592     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1593     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1594     (gnus-simplify-buffer-fuzzy-step " $")
1595     (gnus-simplify-buffer-fuzzy-step "^ +")))
1596
1597 (defun gnus-simplify-subject-fuzzy (subject)
1598   "Simplify a subject string fuzzily.
1599 See `gnus-simplify-buffer-fuzzy' for details."
1600   (save-excursion
1601     (gnus-set-work-buffer)
1602     (let ((case-fold-search t))
1603       ;; Remove uninteresting prefixes.
1604       (when (and gnus-simplify-ignored-prefixes
1605                  (string-match gnus-simplify-ignored-prefixes subject))
1606         (setq subject (substring subject (match-end 0))))
1607       (insert subject)
1608       (inline (gnus-simplify-buffer-fuzzy))
1609       (buffer-string))))
1610
1611 (defsubst gnus-simplify-subject-fully (subject)
1612   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1613   (cond
1614    (gnus-simplify-subject-functions
1615     (gnus-map-function gnus-simplify-subject-functions subject))
1616    ((null gnus-summary-gather-subject-limit)
1617     (gnus-simplify-subject-re subject))
1618    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1619     (gnus-simplify-subject-fuzzy subject))
1620    ((numberp gnus-summary-gather-subject-limit)
1621     (gnus-limit-string (gnus-simplify-subject-re subject)
1622                        gnus-summary-gather-subject-limit))
1623    (t
1624     subject)))
1625
1626 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1627   "Check whether two subjects are equal.
1628 If optional argument SIMPLE-FIRST is t, first argument is already
1629 simplified."
1630   (cond
1631    ((null simple-first)
1632     (equal (gnus-simplify-subject-fully s1)
1633            (gnus-simplify-subject-fully s2)))
1634    (t
1635     (equal s1
1636            (gnus-simplify-subject-fully s2)))))
1637
1638 (defun gnus-summary-bubble-group ()
1639   "Increase the score of the current group.
1640 This is a handy function to add to `gnus-summary-exit-hook' to
1641 increase the score of each group you read."
1642   (gnus-group-add-score gnus-newsgroup-name))
1643
1644 \f
1645 ;;;
1646 ;;; Gnus summary mode
1647 ;;;
1648
1649 (put 'gnus-summary-mode 'mode-class 'special)
1650
1651 (defvar gnus-article-commands-menu)
1652
1653 ;; Non-orthogonal keys
1654
1655 (gnus-define-keys gnus-summary-mode-map
1656   " " gnus-summary-next-page
1657   "\177" gnus-summary-prev-page
1658   [delete] gnus-summary-prev-page
1659   [backspace] gnus-summary-prev-page
1660   "\r" gnus-summary-scroll-up
1661   "\M-\r" gnus-summary-scroll-down
1662   "n" gnus-summary-next-unread-article
1663   "p" gnus-summary-prev-unread-article
1664   "N" gnus-summary-next-article
1665   "P" gnus-summary-prev-article
1666   "\M-\C-n" gnus-summary-next-same-subject
1667   "\M-\C-p" gnus-summary-prev-same-subject
1668   "\M-n" gnus-summary-next-unread-subject
1669   "\M-p" gnus-summary-prev-unread-subject
1670   "." gnus-summary-first-unread-article
1671   "," gnus-summary-best-unread-article
1672   "\M-s" gnus-summary-search-article-forward
1673   "\M-r" gnus-summary-search-article-backward
1674   "<" gnus-summary-beginning-of-article
1675   ">" gnus-summary-end-of-article
1676   "j" gnus-summary-goto-article
1677   "^" gnus-summary-refer-parent-article
1678   "\M-^" gnus-summary-refer-article
1679   "u" gnus-summary-tick-article-forward
1680   "!" gnus-summary-tick-article-forward
1681   "U" gnus-summary-tick-article-backward
1682   "d" gnus-summary-mark-as-read-forward
1683   "D" gnus-summary-mark-as-read-backward
1684   "E" gnus-summary-mark-as-expirable
1685   "\M-u" gnus-summary-clear-mark-forward
1686   "\M-U" gnus-summary-clear-mark-backward
1687   "k" gnus-summary-kill-same-subject-and-select
1688   "\C-k" gnus-summary-kill-same-subject
1689   "\M-\C-k" gnus-summary-kill-thread
1690   "\M-\C-l" gnus-summary-lower-thread
1691   "e" gnus-summary-edit-article
1692   "#" gnus-summary-mark-as-processable
1693   "\M-#" gnus-summary-unmark-as-processable
1694   "\M-\C-t" gnus-summary-toggle-threads
1695   "\M-\C-s" gnus-summary-show-thread
1696   "\M-\C-h" gnus-summary-hide-thread
1697   "\M-\C-f" gnus-summary-next-thread
1698   "\M-\C-b" gnus-summary-prev-thread
1699   [(meta down)] gnus-summary-next-thread
1700   [(meta up)] gnus-summary-prev-thread
1701   "\M-\C-u" gnus-summary-up-thread
1702   "\M-\C-d" gnus-summary-down-thread
1703   "&" gnus-summary-execute-command
1704   "c" gnus-summary-catchup-and-exit
1705   "\C-w" gnus-summary-mark-region-as-read
1706   "\C-t" gnus-summary-toggle-truncation
1707   "?" gnus-summary-mark-as-dormant
1708   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1709   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1710   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1711   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1712   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1713   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1714   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1715   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1716   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1717   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1718   "=" gnus-summary-expand-window
1719   "\C-x\C-s" gnus-summary-reselect-current-group
1720   "\M-g" gnus-summary-rescan-group
1721   "w" gnus-summary-stop-page-breaking
1722   "\C-c\C-r" gnus-summary-caesar-message
1723   "f" gnus-summary-followup
1724   "F" gnus-summary-followup-with-original
1725   "C" gnus-summary-cancel-article
1726   "r" gnus-summary-reply
1727   "R" gnus-summary-reply-with-original
1728   "\C-c\C-f" gnus-summary-mail-forward
1729   "o" gnus-summary-save-article
1730   "\C-o" gnus-summary-save-article-mail
1731   "|" gnus-summary-pipe-output
1732   "\M-k" gnus-summary-edit-local-kill
1733   "\M-K" gnus-summary-edit-global-kill
1734   ;; "V" gnus-version
1735   "\C-c\C-d" gnus-summary-describe-group
1736   "q" gnus-summary-exit
1737   "Q" gnus-summary-exit-no-update
1738   "\C-c\C-i" gnus-info-find-node
1739   gnus-mouse-2 gnus-mouse-pick-article
1740   "m" gnus-summary-mail-other-window
1741   "a" gnus-summary-post-news
1742   "i" gnus-summary-news-other-window
1743   "x" gnus-summary-limit-to-unread
1744   "s" gnus-summary-isearch-article
1745   "t" gnus-summary-toggle-header
1746   "g" gnus-summary-show-article
1747   "l" gnus-summary-goto-last-article
1748   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1749   "\C-d" gnus-summary-enter-digest-group
1750   "\M-\C-d" gnus-summary-read-document
1751   "\M-\C-e" gnus-summary-edit-parameters
1752   "\M-\C-a" gnus-summary-customize-parameters
1753   "\C-c\C-b" gnus-bug
1754   "*" gnus-cache-enter-article
1755   "\M-*" gnus-cache-remove-article
1756   "\M-&" gnus-summary-universal-argument
1757   "\C-l" gnus-recenter
1758   "I" gnus-summary-increase-score
1759   "L" gnus-summary-lower-score
1760   "\M-i" gnus-symbolic-argument
1761   "h" gnus-summary-select-article-buffer
1762
1763   "b" gnus-article-view-part
1764   "\M-t" gnus-summary-toggle-display-buttonized
1765
1766   "V" gnus-summary-score-map
1767   "X" gnus-uu-extract-map
1768   "S" gnus-summary-send-map)
1769
1770 ;; Sort of orthogonal keymap
1771 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1772   "t" gnus-summary-tick-article-forward
1773   "!" gnus-summary-tick-article-forward
1774   "d" gnus-summary-mark-as-read-forward
1775   "r" gnus-summary-mark-as-read-forward
1776   "c" gnus-summary-clear-mark-forward
1777   " " gnus-summary-clear-mark-forward
1778   "e" gnus-summary-mark-as-expirable
1779   "x" gnus-summary-mark-as-expirable
1780   "?" gnus-summary-mark-as-dormant
1781   "b" gnus-summary-set-bookmark
1782   "B" gnus-summary-remove-bookmark
1783   "#" gnus-summary-mark-as-processable
1784   "\M-#" gnus-summary-unmark-as-processable
1785   "S" gnus-summary-limit-include-expunged
1786   "C" gnus-summary-catchup
1787   "H" gnus-summary-catchup-to-here
1788   "h" gnus-summary-catchup-from-here
1789   "\C-c" gnus-summary-catchup-all
1790   "k" gnus-summary-kill-same-subject-and-select
1791   "K" gnus-summary-kill-same-subject
1792   "P" gnus-uu-mark-map)
1793
1794 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1795   "c" gnus-summary-clear-above
1796   "u" gnus-summary-tick-above
1797   "m" gnus-summary-mark-above
1798   "k" gnus-summary-kill-below)
1799
1800 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1801   "/" gnus-summary-limit-to-subject
1802   "n" gnus-summary-limit-to-articles
1803   "w" gnus-summary-pop-limit
1804   "s" gnus-summary-limit-to-subject
1805   "a" gnus-summary-limit-to-author
1806   "u" gnus-summary-limit-to-unread
1807   "m" gnus-summary-limit-to-marks
1808   "M" gnus-summary-limit-exclude-marks
1809   "v" gnus-summary-limit-to-score
1810   "*" gnus-summary-limit-include-cached
1811   "D" gnus-summary-limit-include-dormant
1812   "T" gnus-summary-limit-include-thread
1813   "d" gnus-summary-limit-exclude-dormant
1814   "t" gnus-summary-limit-to-age
1815   "." gnus-summary-limit-to-unseen
1816   "x" gnus-summary-limit-to-extra
1817   "p" gnus-summary-limit-to-display-predicate
1818   "E" gnus-summary-limit-include-expunged
1819   "c" gnus-summary-limit-exclude-childless-dormant
1820   "C" gnus-summary-limit-mark-excluded-as-read
1821   "o" gnus-summary-insert-old-articles
1822   "N" gnus-summary-insert-new-articles
1823   "r" gnus-summary-limit-to-replied)
1824
1825 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1826   "n" gnus-summary-next-unread-article
1827   "p" gnus-summary-prev-unread-article
1828   "N" gnus-summary-next-article
1829   "P" gnus-summary-prev-article
1830   "\C-n" gnus-summary-next-same-subject
1831   "\C-p" gnus-summary-prev-same-subject
1832   "\M-n" gnus-summary-next-unread-subject
1833   "\M-p" gnus-summary-prev-unread-subject
1834   "f" gnus-summary-first-unread-article
1835   "b" gnus-summary-best-unread-article
1836   "j" gnus-summary-goto-article
1837   "g" gnus-summary-goto-subject
1838   "l" gnus-summary-goto-last-article
1839   "o" gnus-summary-pop-article)
1840
1841 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1842   "k" gnus-summary-kill-thread
1843   "l" gnus-summary-lower-thread
1844   "i" gnus-summary-raise-thread
1845   "T" gnus-summary-toggle-threads
1846   "t" gnus-summary-rethread-current
1847   "^" gnus-summary-reparent-thread
1848   "s" gnus-summary-show-thread
1849   "S" gnus-summary-show-all-threads
1850   "h" gnus-summary-hide-thread
1851   "H" gnus-summary-hide-all-threads
1852   "n" gnus-summary-next-thread
1853   "p" gnus-summary-prev-thread
1854   "u" gnus-summary-up-thread
1855   "o" gnus-summary-top-thread
1856   "d" gnus-summary-down-thread
1857   "#" gnus-uu-mark-thread
1858   "\M-#" gnus-uu-unmark-thread)
1859
1860 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1861   "g" gnus-summary-prepare
1862   "c" gnus-summary-insert-cached-articles
1863   "d" gnus-summary-insert-dormant-articles)
1864
1865 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1866   "c" gnus-summary-catchup-and-exit
1867   "C" gnus-summary-catchup-all-and-exit
1868   "E" gnus-summary-exit-no-update
1869   "Q" gnus-summary-exit
1870   "Z" gnus-summary-exit
1871   "n" gnus-summary-catchup-and-goto-next-group
1872   "R" gnus-summary-reselect-current-group
1873   "G" gnus-summary-rescan-group
1874   "N" gnus-summary-next-group
1875   "s" gnus-summary-save-newsrc
1876   "P" gnus-summary-prev-group)
1877
1878 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1879   " " gnus-summary-next-page
1880   "n" gnus-summary-next-page
1881   "\177" gnus-summary-prev-page
1882   [delete] gnus-summary-prev-page
1883   "p" gnus-summary-prev-page
1884   "\r" gnus-summary-scroll-up
1885   "\M-\r" gnus-summary-scroll-down
1886   "<" gnus-summary-beginning-of-article
1887   ">" gnus-summary-end-of-article
1888   "b" gnus-summary-beginning-of-article
1889   "e" gnus-summary-end-of-article
1890   "^" gnus-summary-refer-parent-article
1891   "r" gnus-summary-refer-parent-article
1892   "D" gnus-summary-enter-digest-group
1893   "R" gnus-summary-refer-references
1894   "T" gnus-summary-refer-thread
1895   "g" gnus-summary-show-article
1896   "s" gnus-summary-isearch-article
1897   "P" gnus-summary-print-article
1898   "M" gnus-mailing-list-insinuate
1899   "t" gnus-article-babel)
1900
1901 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1902   "b" gnus-article-add-buttons
1903   "B" gnus-article-add-buttons-to-head
1904   "o" gnus-article-treat-overstrike
1905   "e" gnus-article-emphasize
1906   "w" gnus-article-fill-cited-article
1907   "Q" gnus-article-fill-long-lines
1908   "C" gnus-article-capitalize-sentences
1909   "c" gnus-article-remove-cr
1910   "q" gnus-article-de-quoted-unreadable
1911   "6" gnus-article-de-base64-unreadable
1912   "Z" gnus-article-decode-HZ
1913   "A" gnus-article-treat-ansi-sequences
1914   "h" gnus-article-wash-html
1915   "u" gnus-article-unsplit-urls
1916   "s" gnus-summary-force-verify-and-decrypt
1917   "f" gnus-article-display-x-face
1918   "l" gnus-summary-stop-page-breaking
1919   "r" gnus-summary-caesar-message
1920   "m" gnus-summary-morse-message
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-treat-smiley
1923   "v" gnus-summary-verbose-headers
1924   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1925   "p" gnus-article-verify-x-pgp-sig
1926   "d" gnus-article-treat-dumbquotes)
1927
1928 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1929   ;; mnemonic: deuglif*Y*
1930   "u" gnus-article-outlook-unwrap-lines
1931   "a" gnus-article-outlook-repair-attribution
1932   "c" gnus-article-outlook-rearrange-citation
1933   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1934
1935 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1936   "a" gnus-article-hide
1937   "h" gnus-article-hide-headers
1938   "b" gnus-article-hide-boring-headers
1939   "s" gnus-article-hide-signature
1940   "c" gnus-article-hide-citation
1941   "C" gnus-article-hide-citation-in-followups
1942   "l" gnus-article-hide-list-identifiers
1943   "B" gnus-article-strip-banner
1944   "P" gnus-article-hide-pem
1945   "\C-c" gnus-article-hide-citation-maybe)
1946
1947 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1948   "a" gnus-article-highlight
1949   "h" gnus-article-highlight-headers
1950   "c" gnus-article-highlight-citation
1951   "s" gnus-article-highlight-signature)
1952
1953 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1954   "f" gnus-article-treat-fold-headers
1955   "u" gnus-article-treat-unfold-headers
1956   "n" gnus-article-treat-fold-newsgroups)
1957
1958 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1959   "x" gnus-article-display-x-face
1960   "d" gnus-article-display-face
1961   "s" gnus-treat-smiley
1962   "D" gnus-article-remove-images
1963   "f" gnus-treat-from-picon
1964   "m" gnus-treat-mail-picon
1965   "n" gnus-treat-newsgroups-picon)
1966
1967 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1968   "w" gnus-article-decode-mime-words
1969   "c" gnus-article-decode-charset
1970   "v" gnus-mime-view-all-parts
1971   "b" gnus-article-view-part)
1972
1973 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1974   "z" gnus-article-date-ut
1975   "u" gnus-article-date-ut
1976   "l" gnus-article-date-local
1977   "p" gnus-article-date-english
1978   "e" gnus-article-date-lapsed
1979   "o" gnus-article-date-original
1980   "i" gnus-article-date-iso8601
1981   "s" gnus-article-date-user)
1982
1983 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1984   "t" gnus-article-remove-trailing-blank-lines
1985   "l" gnus-article-strip-leading-blank-lines
1986   "m" gnus-article-strip-multiple-blank-lines
1987   "a" gnus-article-strip-blank-lines
1988   "A" gnus-article-strip-all-blank-lines
1989   "s" gnus-article-strip-leading-space
1990   "e" gnus-article-strip-trailing-space
1991   "w" gnus-article-remove-leading-whitespace)
1992
1993 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1994   "v" gnus-version
1995   "f" gnus-summary-fetch-faq
1996   "d" gnus-summary-describe-group
1997   "h" gnus-summary-describe-briefly
1998   "i" gnus-info-find-node
1999   "c" gnus-group-fetch-charter
2000   "C" gnus-group-fetch-control)
2001
2002 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2003   "e" gnus-summary-expire-articles
2004   "\M-\C-e" gnus-summary-expire-articles-now
2005   "\177" gnus-summary-delete-article
2006   [delete] gnus-summary-delete-article
2007   [backspace] gnus-summary-delete-article
2008   "m" gnus-summary-move-article
2009   "r" gnus-summary-respool-article
2010   "w" gnus-summary-edit-article
2011   "c" gnus-summary-copy-article
2012   "B" gnus-summary-crosspost-article
2013   "q" gnus-summary-respool-query
2014   "t" gnus-summary-respool-trace
2015   "i" gnus-summary-import-article
2016   "I" gnus-summary-create-article
2017   "p" gnus-summary-article-posted-p)
2018
2019 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2020   "o" gnus-summary-save-article
2021   "m" gnus-summary-save-article-mail
2022   "F" gnus-summary-write-article-file
2023   "r" gnus-summary-save-article-rmail
2024   "f" gnus-summary-save-article-file
2025   "b" gnus-summary-save-article-body-file
2026   "h" gnus-summary-save-article-folder
2027   "v" gnus-summary-save-article-vm
2028   "p" gnus-summary-pipe-output
2029   "P" gnus-summary-muttprint
2030   "s" gnus-soup-add-article)
2031
2032 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2033   "b" gnus-summary-display-buttonized
2034   "m" gnus-summary-repair-multipart
2035   "v" gnus-article-view-part
2036   "o" gnus-article-save-part
2037   "c" gnus-article-copy-part
2038   "C" gnus-article-view-part-as-charset
2039   "e" gnus-article-view-part-externally
2040   "E" gnus-article-encrypt-body
2041   "i" gnus-article-inline-part
2042   "|" gnus-article-pipe-part)
2043
2044 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2045   "p" gnus-summary-mark-as-processable
2046   "u" gnus-summary-unmark-as-processable
2047   "U" gnus-summary-unmark-all-processable
2048   "v" gnus-uu-mark-over
2049   "s" gnus-uu-mark-series
2050   "r" gnus-uu-mark-region
2051   "g" gnus-uu-unmark-region
2052   "R" gnus-uu-mark-by-regexp
2053   "G" gnus-uu-unmark-by-regexp
2054   "t" gnus-uu-mark-thread
2055   "T" gnus-uu-unmark-thread
2056   "a" gnus-uu-mark-all
2057   "b" gnus-uu-mark-buffer
2058   "S" gnus-uu-mark-sparse
2059   "k" gnus-summary-kill-process-mark
2060   "y" gnus-summary-yank-process-mark
2061   "w" gnus-summary-save-process-mark
2062   "i" gnus-uu-invert-processable)
2063
2064 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2065   ;;"x" gnus-uu-extract-any
2066   "m" gnus-summary-save-parts
2067   "u" gnus-uu-decode-uu
2068   "U" gnus-uu-decode-uu-and-save
2069   "s" gnus-uu-decode-unshar
2070   "S" gnus-uu-decode-unshar-and-save
2071   "o" gnus-uu-decode-save
2072   "O" gnus-uu-decode-save
2073   "b" gnus-uu-decode-binhex
2074   "B" gnus-uu-decode-binhex
2075   "p" gnus-uu-decode-postscript
2076   "P" gnus-uu-decode-postscript-and-save)
2077
2078 (gnus-define-keys
2079     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2080   "u" gnus-uu-decode-uu-view
2081   "U" gnus-uu-decode-uu-and-save-view
2082   "s" gnus-uu-decode-unshar-view
2083   "S" gnus-uu-decode-unshar-and-save-view
2084   "o" gnus-uu-decode-save-view
2085   "O" gnus-uu-decode-save-view
2086   "b" gnus-uu-decode-binhex-view
2087   "B" gnus-uu-decode-binhex-view
2088   "p" gnus-uu-decode-postscript-view
2089   "P" gnus-uu-decode-postscript-and-save-view)
2090
2091 (defvar gnus-article-post-menu nil)
2092
2093 (defconst gnus-summary-menu-maxlen 20)
2094
2095 (defun gnus-summary-menu-split (menu)
2096   ;; If we have lots of elements, divide them into groups of 20
2097   ;; and make a pane (or submenu) for each one.
2098   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2099       (let ((menu menu) sublists next
2100             (i 1))
2101         (while menu
2102           ;; Pull off the next gnus-summary-menu-maxlen elements
2103           ;; and make them the next element of sublist.
2104           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2105           (if next
2106               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2107                       nil))
2108           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2109                                              (aref (car (last menu)) 0)) menu)
2110                                sublists))
2111           (setq i (1+ i))
2112           (setq menu next))
2113         (nreverse sublists))
2114     ;; Few elements--put them all in one pane.
2115     menu))
2116
2117 (defun gnus-summary-make-menu-bar ()
2118   (gnus-turn-off-edit-menu 'summary)
2119
2120   (unless (boundp 'gnus-summary-misc-menu)
2121
2122     (easy-menu-define
2123       gnus-summary-kill-menu gnus-summary-mode-map ""
2124       (cons
2125        "Score"
2126        (nconc
2127         (list
2128          ["Customize" gnus-score-customize t])
2129         (gnus-make-score-map 'increase)
2130         (gnus-make-score-map 'lower)
2131         '(("Mark"
2132            ["Kill below" gnus-summary-kill-below t]
2133            ["Mark above" gnus-summary-mark-above t]
2134            ["Tick above" gnus-summary-tick-above t]
2135            ["Clear above" gnus-summary-clear-above t])
2136           ["Current score" gnus-summary-current-score t]
2137           ["Set score" gnus-summary-set-score t]
2138           ["Switch current score file..." gnus-score-change-score-file t]
2139           ["Set mark below..." gnus-score-set-mark-below t]
2140           ["Set expunge below..." gnus-score-set-expunge-below t]
2141           ["Edit current score file" gnus-score-edit-current-scores t]
2142           ["Edit score file" gnus-score-edit-file t]
2143           ["Trace score" gnus-score-find-trace t]
2144           ["Find words" gnus-score-find-favourite-words t]
2145           ["Rescore buffer" gnus-summary-rescore t]
2146           ["Increase score..." gnus-summary-increase-score t]
2147           ["Lower score..." gnus-summary-lower-score t]))))
2148
2149     ;; Define both the Article menu in the summary buffer and the
2150     ;; equivalent Commands menu in the article buffer here for
2151     ;; consistency.
2152     (let ((innards
2153            `(("Hide"
2154               ["All" gnus-article-hide t]
2155               ["Headers" gnus-article-hide-headers t]
2156               ["Signature" gnus-article-hide-signature t]
2157               ["Citation" gnus-article-hide-citation t]
2158               ["List identifiers" gnus-article-hide-list-identifiers t]
2159               ["Banner" gnus-article-strip-banner t]
2160               ["Boring headers" gnus-article-hide-boring-headers t])
2161              ("Highlight"
2162               ["All" gnus-article-highlight t]
2163               ["Headers" gnus-article-highlight-headers t]
2164               ["Signature" gnus-article-highlight-signature t]
2165               ["Citation" gnus-article-highlight-citation t])
2166              ("MIME"
2167               ["Words" gnus-article-decode-mime-words t]
2168               ["Charset" gnus-article-decode-charset t]
2169               ["QP" gnus-article-de-quoted-unreadable t]
2170               ["Base64" gnus-article-de-base64-unreadable t]
2171               ["View MIME buttons" gnus-summary-display-buttonized t]
2172               ["View all" gnus-mime-view-all-parts t]
2173               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2174               ["Encrypt body" gnus-article-encrypt-body
2175                :active (not (gnus-group-read-only-p))
2176                ,@(if (featurep 'xemacs) nil
2177                    '(:help "Encrypt the message body on disk"))]
2178               ["Extract all parts..." gnus-summary-save-parts t]
2179               ("Multipart"
2180                ["Repair multipart" gnus-summary-repair-multipart t]
2181                ["Pipe part..." gnus-article-pipe-part t]
2182                ["Inline part" gnus-article-inline-part t]
2183                ["Encrypt body" gnus-article-encrypt-body
2184                 :active (not (gnus-group-read-only-p))
2185                ,@(if (featurep 'xemacs) nil
2186                    '(:help "Encrypt the message body on disk"))]
2187                ["View part externally" gnus-article-view-part-externally t]
2188                ["View part with charset..." gnus-article-view-part-as-charset t]
2189                ["Copy part" gnus-article-copy-part t]
2190                ["Save part..." gnus-article-save-part t]
2191                ["View part" gnus-article-view-part t]))
2192              ("Date"
2193               ["Local" gnus-article-date-local t]
2194               ["ISO8601" gnus-article-date-iso8601 t]
2195               ["UT" gnus-article-date-ut t]
2196               ["Original" gnus-article-date-original t]
2197               ["Lapsed" gnus-article-date-lapsed t]
2198               ["User-defined" gnus-article-date-user t])
2199              ("Display"
2200               ["Remove images" gnus-article-remove-images t]
2201               ["Toggle smiley" gnus-treat-smiley t]
2202               ["Show X-Face" gnus-article-display-x-face t]
2203               ["Show picons in From" gnus-treat-from-picon t]
2204               ["Show picons in mail headers" gnus-treat-mail-picon t]
2205               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2206               ("View as different encoding"
2207                ,@(gnus-summary-menu-split
2208                   (mapcar
2209                    (lambda (cs)
2210                      ;; Since easymenu under Emacs doesn't allow
2211                      ;; lambda forms for menu commands, we should
2212                      ;; provide intern'ed function symbols.
2213                      (let ((command (intern (format "\
2214 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2215                        (fset command
2216                              `(lambda ()
2217                                 (interactive)
2218                                 (let ((gnus-summary-show-article-charset-alist
2219                                        '((1 . ,cs))))
2220                                   (gnus-summary-show-article 1))))
2221                        `[,(symbol-name cs) ,command t]))
2222                    (sort (if (fboundp 'coding-system-list)
2223                              (coding-system-list)
2224                            (mapcar 'car mm-mime-mule-charset-alist))
2225                          'string<)))))
2226              ("Washing"
2227               ("Remove Blanks"
2228                ["Leading" gnus-article-strip-leading-blank-lines t]
2229                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2230                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2231                ["All of the above" gnus-article-strip-blank-lines t]
2232                ["All" gnus-article-strip-all-blank-lines t]
2233                ["Leading space" gnus-article-strip-leading-space t]
2234                ["Trailing space" gnus-article-strip-trailing-space t]
2235                ["Leading space in headers"
2236                 gnus-article-remove-leading-whitespace t])
2237               ["Overstrike" gnus-article-treat-overstrike t]
2238               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2239               ["Emphasis" gnus-article-emphasize t]
2240               ["Word wrap" gnus-article-fill-cited-article t]
2241               ["Fill long lines" gnus-article-fill-long-lines t]
2242               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2243               ["Remove CR" gnus-article-remove-cr t]
2244               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2245               ["Base64" gnus-article-de-base64-unreadable t]
2246               ["Rot 13" gnus-summary-caesar-message
2247                ,@(if (featurep 'xemacs) '(t)
2248                    '(:help "\"Caesar rotate\" article by 13"))]
2249               ["Morse decode" gnus-summary-morse-message t]
2250               ["Unix pipe..." gnus-summary-pipe-message t]
2251               ["Add buttons" gnus-article-add-buttons t]
2252               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2253               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2254               ["Verbose header" gnus-summary-verbose-headers t]
2255               ["Toggle header" gnus-summary-toggle-header t]
2256               ["Unfold headers" gnus-article-treat-unfold-headers t]
2257               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2258               ["Html" gnus-article-wash-html t]
2259               ["Unsplit URLs" gnus-article-unsplit-urls t]
2260               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2261               ["Decode HZ" gnus-article-decode-HZ t]
2262               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2263               ("(Outlook) Deuglify"
2264                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2265                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2266                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2267                ["Full (Outlook) deuglify"
2268                 gnus-article-outlook-deuglify-article t])
2269               )
2270              ("Output"
2271               ["Save in default format..." gnus-summary-save-article
2272                ,@(if (featurep 'xemacs) '(t)
2273                    '(:help "Save article using default method"))]
2274               ["Save in file..." gnus-summary-save-article-file
2275                ,@(if (featurep 'xemacs) '(t)
2276                    '(:help "Save article in file"))]
2277               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2278               ["Save in MH folder..." gnus-summary-save-article-folder t]
2279               ["Save in VM folder..." gnus-summary-save-article-vm t]
2280               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2281               ["Save body in file..." gnus-summary-save-article-body-file t]
2282               ["Pipe through a filter..." gnus-summary-pipe-output t]
2283               ["Add to SOUP packet" gnus-soup-add-article t]
2284               ["Print with Muttprint..." gnus-summary-muttprint t]
2285               ["Print" gnus-summary-print-article t])
2286              ("Backend"
2287               ["Respool article..." gnus-summary-respool-article t]
2288               ["Move article..." gnus-summary-move-article
2289                (gnus-check-backend-function
2290                 'request-move-article gnus-newsgroup-name)]
2291               ["Copy article..." gnus-summary-copy-article t]
2292               ["Crosspost article..." gnus-summary-crosspost-article
2293                (gnus-check-backend-function
2294                 'request-replace-article gnus-newsgroup-name)]
2295               ["Import file..." gnus-summary-import-article
2296                (gnus-check-backend-function
2297                 'request-accept-article gnus-newsgroup-name)]
2298               ["Create article..." gnus-summary-create-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Check if posted" gnus-summary-article-posted-p t]
2302               ["Edit article" gnus-summary-edit-article
2303                (not (gnus-group-read-only-p))]
2304               ["Delete article" gnus-summary-delete-article
2305                (gnus-check-backend-function
2306                 'request-expire-articles gnus-newsgroup-name)]
2307               ["Query respool" gnus-summary-respool-query t]
2308               ["Trace respool" gnus-summary-respool-trace t]
2309               ["Delete expirable articles" gnus-summary-expire-articles-now
2310                (gnus-check-backend-function
2311                 'request-expire-articles gnus-newsgroup-name)])
2312              ("Extract"
2313               ["Uudecode" gnus-uu-decode-uu
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Decode uuencoded article(s)"))]
2316               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2317               ["Unshar" gnus-uu-decode-unshar t]
2318               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2319               ["Save" gnus-uu-decode-save t]
2320               ["Binhex" gnus-uu-decode-binhex t]
2321               ["Postscript" gnus-uu-decode-postscript t]
2322               ["All MIME parts" gnus-summary-save-parts t])
2323              ("Cache"
2324               ["Enter article" gnus-cache-enter-article t]
2325               ["Remove article" gnus-cache-remove-article t])
2326              ["Translate" gnus-article-babel t]
2327              ["Select article buffer" gnus-summary-select-article-buffer t]
2328              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2329              ["Isearch article..." gnus-summary-isearch-article t]
2330              ["Beginning of the article" gnus-summary-beginning-of-article t]
2331              ["End of the article" gnus-summary-end-of-article t]
2332              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2333              ["Fetch referenced articles" gnus-summary-refer-references t]
2334              ["Fetch current thread" gnus-summary-refer-thread t]
2335              ["Fetch article with id..." gnus-summary-refer-article t]
2336              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2337              ["Redisplay" gnus-summary-show-article t]
2338              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2339       (easy-menu-define
2340         gnus-summary-article-menu gnus-summary-mode-map ""
2341         (cons "Article" innards))
2342
2343       (if (not (keymapp gnus-summary-article-menu))
2344           (easy-menu-define
2345             gnus-article-commands-menu gnus-article-mode-map ""
2346             (cons "Commands" innards))
2347         ;; in Emacs, don't share menu.
2348         (setq gnus-article-commands-menu
2349               (copy-keymap gnus-summary-article-menu))
2350         (define-key gnus-article-mode-map [menu-bar commands]
2351           (cons "Commands" gnus-article-commands-menu))))
2352
2353     (easy-menu-define
2354       gnus-summary-thread-menu gnus-summary-mode-map ""
2355       '("Threads"
2356         ["Find all messages in thread" gnus-summary-refer-thread t]
2357         ["Toggle threading" gnus-summary-toggle-threads t]
2358         ["Hide threads" gnus-summary-hide-all-threads t]
2359         ["Show threads" gnus-summary-show-all-threads t]
2360         ["Hide thread" gnus-summary-hide-thread t]
2361         ["Show thread" gnus-summary-show-thread t]
2362         ["Go to next thread" gnus-summary-next-thread t]
2363         ["Go to previous thread" gnus-summary-prev-thread t]
2364         ["Go down thread" gnus-summary-down-thread t]
2365         ["Go up thread" gnus-summary-up-thread t]
2366         ["Top of thread" gnus-summary-top-thread t]
2367         ["Mark thread as read" gnus-summary-kill-thread t]
2368         ["Lower thread score" gnus-summary-lower-thread t]
2369         ["Raise thread score" gnus-summary-raise-thread t]
2370         ["Rethread current" gnus-summary-rethread-current t]))
2371
2372     (easy-menu-define
2373       gnus-summary-post-menu gnus-summary-mode-map ""
2374       `("Post"
2375         ["Send a message (mail or news)" gnus-summary-post-news
2376          ,@(if (featurep 'xemacs) '(t)
2377              '(:help "Post an article"))]
2378         ["Followup" gnus-summary-followup
2379          ,@(if (featurep 'xemacs) '(t)
2380              '(:help "Post followup to this article"))]
2381         ["Followup and yank" gnus-summary-followup-with-original
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Post followup to this article, quoting its contents"))]
2384         ["Supersede article" gnus-summary-supersede-article t]
2385         ["Cancel article" gnus-summary-cancel-article
2386          ,@(if (featurep 'xemacs) '(t)
2387              '(:help "Cancel an article you posted"))]
2388         ["Reply" gnus-summary-reply t]
2389         ["Reply and yank" gnus-summary-reply-with-original t]
2390         ["Wide reply" gnus-summary-wide-reply t]
2391         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Mail a reply, quoting this article"))]
2394         ["Very wide reply" gnus-summary-very-wide-reply t]
2395         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Mail a very wide reply, quoting this article"))]
2398         ["Mail forward" gnus-summary-mail-forward t]
2399         ["Post forward" gnus-summary-post-forward t]
2400         ["Digest and mail" gnus-uu-digest-mail-forward t]
2401         ["Digest and post" gnus-uu-digest-post-forward t]
2402         ["Resend message" gnus-summary-resend-message t]
2403         ["Resend message edit" gnus-summary-resend-message-edit t]
2404         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2405         ["Send a mail" gnus-summary-mail-other-window t]
2406         ["Create a local message" gnus-summary-news-other-window t]
2407         ["Uuencode and post" gnus-uu-post-news
2408          ,@(if (featurep 'xemacs) '(t)
2409              '(:help "Post a uuencoded article"))]
2410         ["Followup via news" gnus-summary-followup-to-mail t]
2411         ["Followup via news and yank"
2412          gnus-summary-followup-to-mail-with-original t]
2413         ;;("Draft"
2414         ;;["Send" gnus-summary-send-draft t]
2415         ;;["Send bounced" gnus-resend-bounced-mail t])
2416         ))
2417
2418     (cond
2419      ((not (keymapp gnus-summary-post-menu))
2420       (setq gnus-article-post-menu gnus-summary-post-menu))
2421      ((not gnus-article-post-menu)
2422       ;; Don't share post menu.
2423       (setq gnus-article-post-menu
2424             (copy-keymap gnus-summary-post-menu))))
2425     (define-key gnus-article-mode-map [menu-bar post]
2426       (cons "Post" gnus-article-post-menu))
2427
2428     (easy-menu-define
2429       gnus-summary-misc-menu gnus-summary-mode-map ""
2430       `("Gnus"
2431         ("Mark Read"
2432          ["Mark as read" gnus-summary-mark-as-read-forward t]
2433          ["Mark same subject and select"
2434           gnus-summary-kill-same-subject-and-select t]
2435          ["Mark same subject" gnus-summary-kill-same-subject t]
2436          ["Catchup" gnus-summary-catchup
2437           ,@(if (featurep 'xemacs) '(t)
2438               '(:help "Mark unread articles in this group as read"))]
2439          ["Catchup all" gnus-summary-catchup-all t]
2440          ["Catchup to here" gnus-summary-catchup-to-here t]
2441          ["Catchup from here" gnus-summary-catchup-from-here t]
2442          ["Catchup region" gnus-summary-mark-region-as-read
2443           (gnus-mark-active-p)]
2444          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2445         ("Mark Various"
2446          ["Tick" gnus-summary-tick-article-forward t]
2447          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2448          ["Remove marks" gnus-summary-clear-mark-forward t]
2449          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2450          ["Set bookmark" gnus-summary-set-bookmark t]
2451          ["Remove bookmark" gnus-summary-remove-bookmark t])
2452         ("Limit to"
2453          ["Marks..." gnus-summary-limit-to-marks t]
2454          ["Subject..." gnus-summary-limit-to-subject t]
2455          ["Author..." gnus-summary-limit-to-author t]
2456          ["Age..." gnus-summary-limit-to-age t]
2457          ["Extra..." gnus-summary-limit-to-extra t]
2458          ["Score..." gnus-summary-limit-to-score t]
2459          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2460          ["Unread" gnus-summary-limit-to-unread t]
2461          ["Unseen" gnus-summary-limit-to-unseen t]
2462          ["Replied" gnus-summary-limit-to-replied t]
2463          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2464          ["Next articles" gnus-summary-limit-to-articles t]
2465          ["Pop limit" gnus-summary-pop-limit t]
2466          ["Show dormant" gnus-summary-limit-include-dormant t]
2467          ["Hide childless dormant"
2468           gnus-summary-limit-exclude-childless-dormant t]
2469          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2470          ["Hide marked" gnus-summary-limit-exclude-marks t]
2471          ["Show expunged" gnus-summary-limit-include-expunged t])
2472         ("Process Mark"
2473          ["Set mark" gnus-summary-mark-as-processable t]
2474          ["Remove mark" gnus-summary-unmark-as-processable t]
2475          ["Remove all marks" gnus-summary-unmark-all-processable t]
2476          ["Mark above" gnus-uu-mark-over t]
2477          ["Mark series" gnus-uu-mark-series t]
2478          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2479          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2480          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2481          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2482          ["Mark all" gnus-uu-mark-all t]
2483          ["Mark buffer" gnus-uu-mark-buffer t]
2484          ["Mark sparse" gnus-uu-mark-sparse t]
2485          ["Mark thread" gnus-uu-mark-thread t]
2486          ["Unmark thread" gnus-uu-unmark-thread t]
2487          ("Process Mark Sets"
2488           ["Kill" gnus-summary-kill-process-mark t]
2489           ["Yank" gnus-summary-yank-process-mark
2490            gnus-newsgroup-process-stack]
2491           ["Save" gnus-summary-save-process-mark t]
2492           ["Run command on marked..." gnus-summary-universal-argument t]))
2493         ("Scroll article"
2494          ["Page forward" gnus-summary-next-page
2495           ,@(if (featurep 'xemacs) '(t)
2496               '(:help "Show next page of article"))]
2497          ["Page backward" gnus-summary-prev-page
2498           ,@(if (featurep 'xemacs) '(t)
2499               '(:help "Show previous page of article"))]
2500          ["Line forward" gnus-summary-scroll-up t])
2501         ("Move"
2502          ["Next unread article" gnus-summary-next-unread-article t]
2503          ["Previous unread article" gnus-summary-prev-unread-article t]
2504          ["Next article" gnus-summary-next-article t]
2505          ["Previous article" gnus-summary-prev-article t]
2506          ["Next unread subject" gnus-summary-next-unread-subject t]
2507          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2508          ["Next article same subject" gnus-summary-next-same-subject t]
2509          ["Previous article same subject" gnus-summary-prev-same-subject t]
2510          ["First unread article" gnus-summary-first-unread-article t]
2511          ["Best unread article" gnus-summary-best-unread-article t]
2512          ["Go to subject number..." gnus-summary-goto-subject t]
2513          ["Go to article number..." gnus-summary-goto-article t]
2514          ["Go to the last article" gnus-summary-goto-last-article t]
2515          ["Pop article off history" gnus-summary-pop-article t])
2516         ("Sort"
2517          ["Sort by number" gnus-summary-sort-by-number t]
2518          ["Sort by author" gnus-summary-sort-by-author t]
2519          ["Sort by subject" gnus-summary-sort-by-subject t]
2520          ["Sort by date" gnus-summary-sort-by-date t]
2521          ["Sort by score" gnus-summary-sort-by-score t]
2522          ["Sort by lines" gnus-summary-sort-by-lines t]
2523          ["Sort by characters" gnus-summary-sort-by-chars t]
2524          ["Randomize" gnus-summary-sort-by-random t]
2525          ["Original sort" gnus-summary-sort-by-original t])
2526         ("Help"
2527          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2528          ["Describe group" gnus-summary-describe-group t]
2529          ["Fetch charter" gnus-group-fetch-charter
2530           ,@(if (featurep 'xemacs) nil
2531               '(:help "Display the charter of the current group"))]
2532          ["Fetch control message" gnus-group-fetch-control
2533           ,@(if (featurep 'xemacs) nil
2534               '(:help "Display the archived control message for the current group"))]
2535          ["Read manual" gnus-info-find-node t])
2536         ("Modes"
2537          ["Pick and read" gnus-pick-mode t]
2538          ["Binary" gnus-binary-mode t])
2539         ("Regeneration"
2540          ["Regenerate" gnus-summary-prepare t]
2541          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2542          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2543          ["Toggle threading" gnus-summary-toggle-threads t])
2544         ["See old articles" gnus-summary-insert-old-articles t]
2545         ["See new articles" gnus-summary-insert-new-articles t]
2546         ["Filter articles..." gnus-summary-execute-command t]
2547         ["Run command on articles..." gnus-summary-universal-argument t]
2548         ["Search articles forward..." gnus-summary-search-article-forward t]
2549         ["Search articles backward..." gnus-summary-search-article-backward t]
2550         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2551         ["Expand window" gnus-summary-expand-window t]
2552         ["Expire expirable articles" gnus-summary-expire-articles
2553          (gnus-check-backend-function
2554           'request-expire-articles gnus-newsgroup-name)]
2555         ["Edit local kill file" gnus-summary-edit-local-kill t]
2556         ["Edit main kill file" gnus-summary-edit-global-kill t]
2557         ["Edit group parameters" gnus-summary-edit-parameters t]
2558         ["Customize group parameters" gnus-summary-customize-parameters t]
2559         ["Send a bug report" gnus-bug t]
2560         ("Exit"
2561          ["Catchup and exit" gnus-summary-catchup-and-exit
2562           ,@(if (featurep 'xemacs) '(t)
2563               '(:help "Mark unread articles in this group as read, then exit"))]
2564          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2565          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2566          ["Exit group" gnus-summary-exit
2567           ,@(if (featurep 'xemacs) '(t)
2568               '(:help "Exit current group, return to group selection mode"))]
2569          ["Exit group without updating" gnus-summary-exit-no-update t]
2570          ["Exit and goto next group" gnus-summary-next-group t]
2571          ["Exit and goto prev group" gnus-summary-prev-group t]
2572          ["Reselect group" gnus-summary-reselect-current-group t]
2573          ["Rescan group" gnus-summary-rescan-group t]
2574          ["Update dribble" gnus-summary-save-newsrc t])))
2575
2576     (gnus-run-hooks 'gnus-summary-menu-hook)))
2577
2578 (defvar gnus-summary-tool-bar-map nil)
2579
2580 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2581 (defun gnus-summary-make-tool-bar ()
2582   (if (and (fboundp 'tool-bar-add-item-from-menu)
2583            (default-value 'tool-bar-mode)
2584            (not gnus-summary-tool-bar-map))
2585       (setq gnus-summary-tool-bar-map
2586             (let ((tool-bar-map (make-sparse-keymap))
2587                   (load-path (mm-image-load-path)))
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-post-news "post" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-followup "followup" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-reply "reply" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2618               tool-bar-map)))
2619   (if gnus-summary-tool-bar-map
2620       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2621
2622 (defun gnus-score-set-default (var value)
2623   "A version of set that updates the GNU Emacs menu-bar."
2624   (set var value)
2625   ;; It is the message that forces the active status to be updated.
2626   (message ""))
2627
2628 (defun gnus-make-score-map (type)
2629   "Make a summary score map of type TYPE."
2630   (if t
2631       nil
2632     (let ((headers '(("author" "from" string)
2633                      ("subject" "subject" string)
2634                      ("article body" "body" string)
2635                      ("article head" "head" string)
2636                      ("xref" "xref" string)
2637                      ("extra header" "extra" string)
2638                      ("lines" "lines" number)
2639                      ("followups to author" "followup" string)))
2640           (types '((number ("less than" <)
2641                            ("greater than" >)
2642                            ("equal" =))
2643                    (string ("substring" s)
2644                            ("exact string" e)
2645                            ("fuzzy string" f)
2646                            ("regexp" r))))
2647           (perms '(("temporary" (current-time-string))
2648                    ("permanent" nil)
2649                    ("immediate" now)))
2650           header)
2651       (list
2652        (apply
2653         'nconc
2654         (list
2655          (if (eq type 'lower)
2656              "Lower score"
2657            "Increase score"))
2658         (let (outh)
2659           (while headers
2660             (setq header (car headers))
2661             (setq outh
2662                   (cons
2663                    (apply
2664                     'nconc
2665                     (list (car header))
2666                     (let ((ts (cdr (assoc (nth 2 header) types)))
2667                           outt)
2668                       (while ts
2669                         (setq outt
2670                               (cons
2671                                (apply
2672                                 'nconc
2673                                 (list (caar ts))
2674                                 (let ((ps perms)
2675                                       outp)
2676                                   (while ps
2677                                     (setq outp
2678                                           (cons
2679                                            (vector
2680                                             (caar ps)
2681                                             (list
2682                                              'gnus-summary-score-entry
2683                                              (nth 1 header)
2684                                              (if (or (string= (nth 1 header)
2685                                                               "head")
2686                                                      (string= (nth 1 header)
2687                                                               "body"))
2688                                                  ""
2689                                                (list 'gnus-summary-header
2690                                                      (nth 1 header)))
2691                                              (list 'quote (nth 1 (car ts)))
2692                                              (list 'gnus-score-delta-default
2693                                                    nil)
2694                                              (nth 1 (car ps))
2695                                              t)
2696                                             t)
2697                                            outp))
2698                                     (setq ps (cdr ps)))
2699                                   (list (nreverse outp))))
2700                                outt))
2701                         (setq ts (cdr ts)))
2702                       (list (nreverse outt))))
2703                    outh))
2704             (setq headers (cdr headers)))
2705           (list (nreverse outh))))))))
2706
2707 \f
2708
2709 (defun gnus-summary-mode (&optional group)
2710   "Major mode for reading articles.
2711
2712 All normal editing commands are switched off.
2713 \\<gnus-summary-mode-map>
2714 Each line in this buffer represents one article.  To read an
2715 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2716 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2717 respectively.
2718
2719 You can also post articles and send mail from this buffer.  To
2720 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2721 of an article, type `\\[gnus-summary-reply]'.
2722
2723 There are approx. one gazillion commands you can execute in this
2724 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2725
2726 The following commands are available:
2727
2728 \\{gnus-summary-mode-map}"
2729   (interactive)
2730   (kill-all-local-variables)
2731   (when (gnus-visual-p 'summary-menu 'menu)
2732     (gnus-summary-make-menu-bar)
2733     (gnus-summary-make-tool-bar))
2734   (gnus-summary-make-local-variables)
2735   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2736     (gnus-summary-make-local-variables))
2737   (gnus-make-thread-indent-array)
2738   (gnus-simplify-mode-line)
2739   (setq major-mode 'gnus-summary-mode)
2740   (setq mode-name "Summary")
2741   (make-local-variable 'minor-mode-alist)
2742   (use-local-map gnus-summary-mode-map)
2743   (buffer-disable-undo)
2744   (setq buffer-read-only t)             ;Disable modification
2745   (setq truncate-lines t)
2746   (setq selective-display t)
2747   (setq selective-display-ellipses t)   ;Display `...'
2748   (gnus-summary-set-display-table)
2749   (gnus-set-default-directory)
2750   (setq gnus-newsgroup-name group)
2751   (make-local-variable 'gnus-summary-line-format)
2752   (make-local-variable 'gnus-summary-line-format-spec)
2753   (make-local-variable 'gnus-summary-dummy-line-format)
2754   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2755   (make-local-variable 'gnus-summary-mark-positions)
2756   (gnus-make-local-hook 'pre-command-hook)
2757   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2758   (gnus-run-hooks 'gnus-summary-mode-hook)
2759   (turn-on-gnus-mailing-list-mode)
2760   (mm-enable-multibyte)
2761   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2762   (gnus-update-summary-mark-positions))
2763
2764 (defun gnus-summary-make-local-variables ()
2765   "Make all the local summary buffer variables."
2766   (let (global)
2767     (dolist (local gnus-summary-local-variables)
2768       (if (consp local)
2769           (progn
2770             (if (eq (cdr local) 'global)
2771                 ;; Copy the global value of the variable.
2772                 (setq global (symbol-value (car local)))
2773               ;; Use the value from the list.
2774               (setq global (eval (cdr local))))
2775             (set (make-local-variable (car local)) global))
2776         ;; Simple nil-valued local variable.
2777         (set (make-local-variable local) nil)))))
2778
2779 (defun gnus-summary-clear-local-variables ()
2780   (let ((locals gnus-summary-local-variables))
2781     (while locals
2782       (if (consp (car locals))
2783           (and (vectorp (caar locals))
2784                (set (caar locals) nil))
2785         (and (vectorp (car locals))
2786              (set (car locals) nil)))
2787       (setq locals (cdr locals)))))
2788
2789 ;; Summary data functions.
2790
2791 (defmacro gnus-data-number (data)
2792   `(car ,data))
2793
2794 (defmacro gnus-data-set-number (data number)
2795   `(setcar ,data ,number))
2796
2797 (defmacro gnus-data-mark (data)
2798   `(nth 1 ,data))
2799
2800 (defmacro gnus-data-set-mark (data mark)
2801   `(setcar (nthcdr 1 ,data) ,mark))
2802
2803 (defmacro gnus-data-pos (data)
2804   `(nth 2 ,data))
2805
2806 (defmacro gnus-data-set-pos (data pos)
2807   `(setcar (nthcdr 2 ,data) ,pos))
2808
2809 (defmacro gnus-data-header (data)
2810   `(nth 3 ,data))
2811
2812 (defmacro gnus-data-set-header (data header)
2813   `(setf (nth 3 ,data) ,header))
2814
2815 (defmacro gnus-data-level (data)
2816   `(nth 4 ,data))
2817
2818 (defmacro gnus-data-unread-p (data)
2819   `(= (nth 1 ,data) gnus-unread-mark))
2820
2821 (defmacro gnus-data-read-p (data)
2822   `(/= (nth 1 ,data) gnus-unread-mark))
2823
2824 (defmacro gnus-data-pseudo-p (data)
2825   `(consp (nth 3 ,data)))
2826
2827 (defmacro gnus-data-find (number)
2828   `(assq ,number gnus-newsgroup-data))
2829
2830 (defmacro gnus-data-find-list (number &optional data)
2831   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2832      (memq (assq ,number bdata)
2833            bdata)))
2834
2835 (defmacro gnus-data-make (number mark pos header level)
2836   `(list ,number ,mark ,pos ,header ,level))
2837
2838 (defun gnus-data-enter (after-article number mark pos header level offset)
2839   (let ((data (gnus-data-find-list after-article)))
2840     (unless data
2841       (error "No such article: %d" after-article))
2842     (setcdr data (cons (gnus-data-make number mark pos header level)
2843                        (cdr data)))
2844     (setq gnus-newsgroup-data-reverse nil)
2845     (gnus-data-update-list (cddr data) offset)))
2846
2847 (defun gnus-data-enter-list (after-article list &optional offset)
2848   (when list
2849     (let ((data (and after-article (gnus-data-find-list after-article)))
2850           (ilist list))
2851       (if (not (or data
2852                    after-article))
2853           (let ((odata gnus-newsgroup-data))
2854             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2855             (when offset
2856               (gnus-data-update-list odata offset)))
2857       ;; Find the last element in the list to be spliced into the main
2858         ;; list.
2859         (while (cdr list)
2860           (setq list (cdr list)))
2861         (if (not data)
2862             (progn
2863               (setcdr list gnus-newsgroup-data)
2864               (setq gnus-newsgroup-data ilist)
2865               (when offset
2866                 (gnus-data-update-list (cdr list) offset)))
2867           (setcdr list (cdr data))
2868           (setcdr data ilist)
2869           (when offset
2870             (gnus-data-update-list (cdr list) offset))))
2871       (setq gnus-newsgroup-data-reverse nil))))
2872
2873 (defun gnus-data-remove (article &optional offset)
2874   (let ((data gnus-newsgroup-data))
2875     (if (= (gnus-data-number (car data)) article)
2876         (progn
2877           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2878                 gnus-newsgroup-data-reverse nil)
2879           (when offset
2880             (gnus-data-update-list gnus-newsgroup-data offset)))
2881       (while (cdr data)
2882         (when (= (gnus-data-number (cadr data)) article)
2883           (setcdr data (cddr data))
2884           (when offset
2885             (gnus-data-update-list (cdr data) offset))
2886           (setq data nil
2887                 gnus-newsgroup-data-reverse nil))
2888         (setq data (cdr data))))))
2889
2890 (defmacro gnus-data-list (backward)
2891   `(if ,backward
2892        (or gnus-newsgroup-data-reverse
2893            (setq gnus-newsgroup-data-reverse
2894                  (reverse gnus-newsgroup-data)))
2895      gnus-newsgroup-data))
2896
2897 (defun gnus-data-update-list (data offset)
2898   "Add OFFSET to the POS of all data entries in DATA."
2899   (setq gnus-newsgroup-data-reverse nil)
2900   (while data
2901     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2902     (setq data (cdr data))))
2903
2904 (defun gnus-summary-article-pseudo-p (article)
2905   "Say whether this article is a pseudo article or not."
2906   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2907
2908 (defmacro gnus-summary-article-sparse-p (article)
2909   "Say whether this article is a sparse article or not."
2910   `(memq ,article gnus-newsgroup-sparse))
2911
2912 (defmacro gnus-summary-article-ancient-p (article)
2913   "Say whether this article is a sparse article or not."
2914   `(memq ,article gnus-newsgroup-ancient))
2915
2916 (defun gnus-article-parent-p (number)
2917   "Say whether this article is a parent or not."
2918   (let ((data (gnus-data-find-list number)))
2919     (and (cdr data)              ; There has to be an article after...
2920          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2921             (gnus-data-level (nth 1 data))))))
2922
2923 (defun gnus-article-children (number)
2924   "Return a list of all children to NUMBER."
2925   (let* ((data (gnus-data-find-list number))
2926          (level (gnus-data-level (car data)))
2927          children)
2928     (setq data (cdr data))
2929     (while (and data
2930                 (= (gnus-data-level (car data)) (1+ level)))
2931       (push (gnus-data-number (car data)) children)
2932       (setq data (cdr data)))
2933     children))
2934
2935 (defmacro gnus-summary-skip-intangible ()
2936   "If the current article is intangible, then jump to a different article."
2937   '(let ((to (get-text-property (point) 'gnus-intangible)))
2938      (and to (gnus-summary-goto-subject to))))
2939
2940 (defmacro gnus-summary-article-intangible-p ()
2941   "Say whether this article is intangible or not."
2942   '(get-text-property (point) 'gnus-intangible))
2943
2944 (defun gnus-article-read-p (article)
2945   "Say whether ARTICLE is read or not."
2946   (not (or (memq article gnus-newsgroup-marked)
2947            (memq article gnus-newsgroup-spam-marked)
2948            (memq article gnus-newsgroup-unreads)
2949            (memq article gnus-newsgroup-unselected)
2950            (memq article gnus-newsgroup-dormant))))
2951
2952 ;; Some summary mode macros.
2953
2954 (defmacro gnus-summary-article-number ()
2955   "The article number of the article on the current line.
2956 If there isn't an article number here, then we return the current
2957 article number."
2958   '(progn
2959      (gnus-summary-skip-intangible)
2960      (or (get-text-property (point) 'gnus-number)
2961          (gnus-summary-last-subject))))
2962
2963 (defmacro gnus-summary-article-header (&optional number)
2964   "Return the header of article NUMBER."
2965   `(gnus-data-header (gnus-data-find
2966                       ,(or number '(gnus-summary-article-number)))))
2967
2968 (defmacro gnus-summary-thread-level (&optional number)
2969   "Return the level of thread that starts with article NUMBER."
2970   `(if (and (eq gnus-summary-make-false-root 'dummy)
2971             (get-text-property (point) 'gnus-intangible))
2972        0
2973      (gnus-data-level (gnus-data-find
2974                        ,(or number '(gnus-summary-article-number))))))
2975
2976 (defmacro gnus-summary-article-mark (&optional number)
2977   "Return the mark of article NUMBER."
2978   `(gnus-data-mark (gnus-data-find
2979                     ,(or number '(gnus-summary-article-number)))))
2980
2981 (defmacro gnus-summary-article-pos (&optional number)
2982   "Return the position of the line of article NUMBER."
2983   `(gnus-data-pos (gnus-data-find
2984                    ,(or number '(gnus-summary-article-number)))))
2985
2986 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2987 (defmacro gnus-summary-article-subject (&optional number)
2988   "Return current subject string or nil if nothing."
2989   `(let ((headers
2990           ,(if number
2991                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2992              '(gnus-data-header (assq (gnus-summary-article-number)
2993                                       gnus-newsgroup-data)))))
2994      (and headers
2995           (vectorp headers)
2996           (mail-header-subject headers))))
2997
2998 (defmacro gnus-summary-article-score (&optional number)
2999   "Return current article score."
3000   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3001                   gnus-newsgroup-scored))
3002        gnus-summary-default-score 0))
3003
3004 (defun gnus-summary-article-children (&optional number)
3005   "Return a list of article numbers that are children of article NUMBER."
3006   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3007          (level (gnus-data-level (car data)))
3008          l children)
3009     (while (and (setq data (cdr data))
3010                 (> (setq l (gnus-data-level (car data))) level))
3011       (and (= (1+ level) l)
3012            (push (gnus-data-number (car data))
3013                  children)))
3014     (nreverse children)))
3015
3016 (defun gnus-summary-article-parent (&optional number)
3017   "Return the article number of the parent of article NUMBER."
3018   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3019                                     (gnus-data-list t)))
3020          (level (gnus-data-level (car data))))
3021     (if (zerop level)
3022         ()                              ; This is a root.
3023       ;; We search until we find an article with a level less than
3024       ;; this one.  That function has to be the parent.
3025       (while (and (setq data (cdr data))
3026                   (not (< (gnus-data-level (car data)) level))))
3027       (and data (gnus-data-number (car data))))))
3028
3029 (defun gnus-unread-mark-p (mark)
3030   "Say whether MARK is the unread mark."
3031   (= mark gnus-unread-mark))
3032
3033 (defun gnus-read-mark-p (mark)
3034   "Say whether MARK is one of the marks that mark as read.
3035 This is all marks except unread, ticked, dormant, and expirable."
3036   (not (or (= mark gnus-unread-mark)
3037            (= mark gnus-ticked-mark)
3038            (= mark gnus-spam-mark)
3039            (= mark gnus-dormant-mark)
3040            (= mark gnus-expirable-mark))))
3041
3042 (defmacro gnus-article-mark (number)
3043   "Return the MARK of article NUMBER.
3044 This macro should only be used when computing the mark the \"first\"
3045 time; i.e., when generating the summary lines.  After that,
3046 `gnus-summary-article-mark' should be used to examine the
3047 marks of articles."
3048   `(cond
3049     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3050     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3051     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3052     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3053     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3054     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3055     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3056     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3057            gnus-ancient-mark))))
3058
3059 ;; Saving hidden threads.
3060
3061 (defmacro gnus-save-hidden-threads (&rest forms)
3062   "Save hidden threads, eval FORMS, and restore the hidden threads."
3063   (let ((config (make-symbol "config")))
3064     `(let ((,config (gnus-hidden-threads-configuration)))
3065        (unwind-protect
3066            (save-excursion
3067              ,@forms)
3068          (gnus-restore-hidden-threads-configuration ,config)))))
3069 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3070 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3071
3072 (defun gnus-data-compute-positions ()
3073   "Compute the positions of all articles."
3074   (setq gnus-newsgroup-data-reverse nil)
3075   (let ((data gnus-newsgroup-data))
3076     (save-excursion
3077       (gnus-save-hidden-threads
3078         (gnus-summary-show-all-threads)
3079         (goto-char (point-min))
3080         (while data
3081           (while (get-text-property (point) 'gnus-intangible)
3082             (forward-line 1))
3083           (gnus-data-set-pos (car data) (+ (point) 3))
3084           (setq data (cdr data))
3085           (forward-line 1))))))
3086
3087 (defun gnus-hidden-threads-configuration ()
3088   "Return the current hidden threads configuration."
3089   (save-excursion
3090     (let (config)
3091       (goto-char (point-min))
3092       (while (search-forward "\r" nil t)
3093         (push (1- (point)) config))
3094       config)))
3095
3096 (defun gnus-restore-hidden-threads-configuration (config)
3097   "Restore hidden threads configuration from CONFIG."
3098   (save-excursion
3099     (let (point buffer-read-only)
3100       (while (setq point (pop config))
3101         (when (and (< point (point-max))
3102                    (goto-char point)
3103                    (eq (char-after) ?\n))
3104           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3105
3106 ;; Various summary mode internalish functions.
3107
3108 (defun gnus-mouse-pick-article (e)
3109   (interactive "e")
3110   (mouse-set-point e)
3111   (gnus-summary-next-page nil t))
3112
3113 (defun gnus-summary-set-display-table ()
3114   "Change the display table.
3115 Odd characters have a tendency to mess
3116 up nicely formatted displays - we make all possible glyphs
3117 display only a single character."
3118
3119   ;; We start from the standard display table, if any.
3120   (let ((table (or (copy-sequence standard-display-table)
3121                    (make-display-table)))
3122         (i 32))
3123     ;; Nix out all the control chars...
3124     (while (>= (setq i (1- i)) 0)
3125       (aset table i [??]))
3126    ;; ... but not newline and cr, of course.  (cr is necessary for the
3127     ;; selective display).
3128     (aset table ?\n nil)
3129     (aset table ?\r nil)
3130     ;; We keep TAB as well.
3131     (aset table ?\t nil)
3132     ;; We nix out any glyphs over 126 that are not set already.
3133     (let ((i 256))
3134       (while (>= (setq i (1- i)) 127)
3135         ;; Only modify if the entry is nil.
3136         (unless (aref table i)
3137           (aset table i [??]))))
3138     (setq buffer-display-table table)))
3139
3140 (defun gnus-summary-set-article-display-arrow (pos)
3141   "Update the overlay arrow to point to line at position POS."
3142   (when (and gnus-summary-display-arrow
3143              (boundp 'overlay-arrow-position)
3144              (boundp 'overlay-arrow-string))
3145     (save-excursion
3146       (goto-char pos)
3147       (beginning-of-line)
3148       (unless overlay-arrow-position
3149         (setq overlay-arrow-position (make-marker)))
3150       (setq overlay-arrow-string "=>"
3151             overlay-arrow-position (set-marker overlay-arrow-position
3152                                                (point)
3153                                                (current-buffer))))))
3154
3155 (defun gnus-summary-setup-buffer (group)
3156   "Initialize summary buffer."
3157   (let ((buffer (gnus-summary-buffer-name group))
3158         (dead-name (concat "*Dead Summary "
3159                            (gnus-group-decoded-name group) "*")))
3160     ;; If a dead summary buffer exists, we kill it.
3161     (when (gnus-buffer-live-p dead-name)
3162       (gnus-kill-buffer dead-name))
3163     (if (get-buffer buffer)
3164         (progn
3165           (set-buffer buffer)
3166           (setq gnus-summary-buffer (current-buffer))
3167           (not gnus-newsgroup-prepared))
3168       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3169       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3170       (gnus-summary-mode group)
3171       (when gnus-carpal
3172         (gnus-carpal-setup-buffer 'summary))
3173       (unless gnus-single-article-buffer
3174         (make-local-variable 'gnus-article-buffer)
3175         (make-local-variable 'gnus-article-current)
3176         (make-local-variable 'gnus-original-article-buffer))
3177       (setq gnus-newsgroup-name group)
3178       ;; Set any local variables in the group parameters.
3179       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3180       t)))
3181
3182 (defun gnus-set-global-variables ()
3183   "Set the global equivalents of the buffer-local variables.
3184 They are set to the latest values they had.  These reflect the summary
3185 buffer that was in action when the last article was fetched."
3186   (when (eq major-mode 'gnus-summary-mode)
3187     (setq gnus-summary-buffer (current-buffer))
3188     (let ((name gnus-newsgroup-name)
3189           (marked gnus-newsgroup-marked)
3190           (spam gnus-newsgroup-spam-marked)
3191           (unread gnus-newsgroup-unreads)
3192           (headers gnus-current-headers)
3193           (data gnus-newsgroup-data)
3194           (summary gnus-summary-buffer)
3195           (article-buffer gnus-article-buffer)
3196           (original gnus-original-article-buffer)
3197           (gac gnus-article-current)
3198           (reffed gnus-reffed-article-number)
3199           (score-file gnus-current-score-file)
3200           (default-charset gnus-newsgroup-charset)
3201           vlist)
3202       (let ((locals gnus-newsgroup-variables))
3203         (while locals
3204           (if (consp (car locals))
3205               (push (eval (caar locals)) vlist)
3206             (push (eval (car locals)) vlist))
3207           (setq locals (cdr locals)))
3208         (setq vlist (nreverse vlist)))
3209       (with-current-buffer gnus-group-buffer
3210         (setq gnus-newsgroup-name name
3211               gnus-newsgroup-marked marked
3212               gnus-newsgroup-spam-marked spam
3213               gnus-newsgroup-unreads unread
3214               gnus-current-headers headers
3215               gnus-newsgroup-data data
3216               gnus-article-current gac
3217               gnus-summary-buffer summary
3218               gnus-article-buffer article-buffer
3219               gnus-original-article-buffer original
3220               gnus-reffed-article-number reffed
3221               gnus-current-score-file score-file
3222               gnus-newsgroup-charset default-charset)
3223         (let ((locals gnus-newsgroup-variables))
3224           (while locals
3225             (if (consp (car locals))
3226                 (set (caar locals) (pop vlist))
3227               (set (car locals) (pop vlist)))
3228             (setq locals (cdr locals))))
3229         ;; The article buffer also has local variables.
3230         (when (gnus-buffer-live-p gnus-article-buffer)
3231           (set-buffer gnus-article-buffer)
3232           (setq gnus-summary-buffer summary))))))
3233
3234 (defun gnus-summary-article-unread-p (article)
3235   "Say whether ARTICLE is unread or not."
3236   (memq article gnus-newsgroup-unreads))
3237
3238 (defun gnus-summary-first-article-p (&optional article)
3239   "Return whether ARTICLE is the first article in the buffer."
3240   (if (not (setq article (or article (gnus-summary-article-number))))
3241       nil
3242     (eq article (caar gnus-newsgroup-data))))
3243
3244 (defun gnus-summary-last-article-p (&optional article)
3245   "Return whether ARTICLE is the last article in the buffer."
3246   (if (not (setq article (or article (gnus-summary-article-number))))
3247       ;; All non-existent numbers are the last article.  :-)
3248       t
3249     (not (cdr (gnus-data-find-list article)))))
3250
3251 (defun gnus-make-thread-indent-array ()
3252   (let ((n 200))
3253     (unless (and gnus-thread-indent-array
3254                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3255       (setq gnus-thread-indent-array (make-vector 201 "")
3256             gnus-thread-indent-array-level gnus-thread-indent-level)
3257       (while (>= n 0)
3258         (aset gnus-thread-indent-array n
3259               (make-string (* n gnus-thread-indent-level) ? ))
3260         (setq n (1- n))))))
3261
3262 (defun gnus-update-summary-mark-positions ()
3263   "Compute where the summary marks are to go."
3264   (save-excursion
3265     (when (gnus-buffer-exists-p gnus-summary-buffer)
3266       (set-buffer gnus-summary-buffer))
3267     (let ((gnus-replied-mark 129)
3268           (gnus-score-below-mark 130)
3269           (gnus-score-over-mark 130)
3270           (gnus-undownloaded-mark 131)
3271           (spec gnus-summary-line-format-spec)
3272           gnus-visual pos)
3273       (save-excursion
3274         (gnus-set-work-buffer)
3275         (let ((gnus-summary-line-format-spec spec)
3276               (gnus-newsgroup-downloadable '(0)))
3277           (gnus-summary-insert-line
3278            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3279            0 nil t 128 t nil "" nil 1)
3280           (goto-char (point-min))
3281           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3282                                              (- (point) (point-min) 1)))))
3283           (goto-char (point-min))
3284           (push (cons 'replied (and (search-forward "\201" nil t)
3285                                     (- (point) (point-min) 1)))
3286                 pos)
3287           (goto-char (point-min))
3288           (push (cons 'score (and (search-forward "\202" nil t)
3289                                   (- (point) (point-min) 1)))
3290                 pos)
3291           (goto-char (point-min))
3292           (push (cons 'download
3293                       (and (search-forward "\203" nil t)
3294                            (- (point) (point-min) 1)))
3295                 pos)))
3296       (setq gnus-summary-mark-positions pos))))
3297
3298 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3299   "Insert a dummy root in the summary buffer."
3300   (beginning-of-line)
3301   (gnus-add-text-properties
3302    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3303    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3304
3305 (defun gnus-summary-extract-address-component (from)
3306   (or (car (funcall gnus-extract-address-components from))
3307       from))
3308
3309 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3310   (let ((mail-parse-charset gnus-newsgroup-charset)
3311         ; Is it really necessary to do this next part for each summary line?
3312         ; Luckily, doesn't seem to slow things down much.
3313         (mail-parse-ignored-charsets
3314          (with-current-buffer gnus-summary-buffer
3315            gnus-newsgroup-ignored-charsets)))
3316     (or
3317      (and gnus-ignored-from-addresses
3318           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3319           (let ((extra-headers (mail-header-extra header))
3320                 to
3321                 newsgroups)
3322             (cond
3323              ((setq to (cdr (assq 'To extra-headers)))
3324               (concat "-> "
3325                       (inline
3326                         (gnus-summary-extract-address-component
3327                          (funcall gnus-decode-encoded-word-function to)))))
3328              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3329               (concat "=> " newsgroups)))))
3330      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3331
3332 (defun gnus-summary-insert-line (gnus-tmp-header
3333                                  gnus-tmp-level gnus-tmp-current
3334                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3335                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3336                                  &optional gnus-tmp-dummy gnus-tmp-score
3337                                  gnus-tmp-process)
3338   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3339          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3340          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3341          (gnus-tmp-score-char
3342           (if (or (null gnus-summary-default-score)
3343                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3344                       gnus-summary-zcore-fuzz))
3345               ?                         ;Whitespace
3346             (if (< gnus-tmp-score gnus-summary-default-score)
3347                 gnus-score-below-mark gnus-score-over-mark)))
3348          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3349          (gnus-tmp-replied
3350           (cond (gnus-tmp-process gnus-process-mark)
3351                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3352                  gnus-cached-mark)
3353                 (gnus-tmp-replied gnus-replied-mark)
3354                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3355                  gnus-forwarded-mark)
3356                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3357                  gnus-saved-mark)
3358                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3359                  gnus-recent-mark)
3360                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3361                  gnus-unseen-mark)
3362                 (t gnus-no-mark)))
3363          (gnus-tmp-downloaded
3364           (cond (undownloaded
3365                  gnus-undownloaded-mark)
3366                 (gnus-newsgroup-agentized
3367                  gnus-downloaded-mark)
3368                 (t
3369                  gnus-no-mark)))
3370          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3371          (gnus-tmp-name
3372           (cond
3373            ((string-match "<[^>]+> *$" gnus-tmp-from)
3374             (let ((beg (match-beginning 0)))
3375               (or (and (string-match "^\".+\"" gnus-tmp-from)
3376                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3377                   (substring gnus-tmp-from 0 beg))))
3378            ((string-match "(.+)" gnus-tmp-from)
3379             (substring gnus-tmp-from
3380                        (1+ (match-beginning 0)) (1- (match-end 0))))
3381            (t gnus-tmp-from)))
3382          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3383          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3384          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3385          (buffer-read-only nil))
3386     (when (string= gnus-tmp-name "")
3387       (setq gnus-tmp-name gnus-tmp-from))
3388     (unless (numberp gnus-tmp-lines)
3389       (setq gnus-tmp-lines -1))
3390     (if (= gnus-tmp-lines -1)
3391         (setq gnus-tmp-lines "?")
3392       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3393       (gnus-put-text-property
3394      (point)
3395      (progn (eval gnus-summary-line-format-spec) (point))
3396        'gnus-number gnus-tmp-number)
3397     (when (gnus-visual-p 'summary-highlight 'highlight)
3398       (forward-line -1)
3399       (gnus-run-hooks 'gnus-summary-update-hook)
3400       (forward-line 1))))
3401
3402 (defun gnus-summary-update-line (&optional dont-update)
3403   "Update summary line after change."
3404   (when (and gnus-summary-default-score
3405              (not gnus-summary-inhibit-highlight))
3406     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3407            (article (gnus-summary-article-number))
3408            (score (gnus-summary-article-score article)))
3409       (unless dont-update
3410         (if (and gnus-summary-mark-below
3411                  (< (gnus-summary-article-score)
3412                     gnus-summary-mark-below))
3413             ;; This article has a low score, so we mark it as read.
3414             (when (memq article gnus-newsgroup-unreads)
3415               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3416           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3417             ;; This article was previously marked as read on account
3418             ;; of a low score, but now it has risen, so we mark it as
3419             ;; unread.
3420             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3421         (gnus-summary-update-mark
3422          (if (or (null gnus-summary-default-score)
3423                  (<= (abs (- score gnus-summary-default-score))
3424                      gnus-summary-zcore-fuzz))
3425              ?                          ;Whitespace
3426            (if (< score gnus-summary-default-score)
3427                gnus-score-below-mark gnus-score-over-mark))
3428          'score))
3429       ;; Do visual highlighting.
3430       (when (gnus-visual-p 'summary-highlight 'highlight)
3431         (gnus-run-hooks 'gnus-summary-update-hook)))))
3432
3433 (defvar gnus-tmp-new-adopts nil)
3434
3435 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3436   "Return the number of articles in THREAD.
3437 This may be 0 in some cases -- if none of the articles in
3438 the thread are to be displayed."
3439   (let* ((number
3440          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3441           (cond
3442            ((not (listp thread))
3443             1)
3444            ((and (consp thread) (cdr thread))
3445             (apply
3446              '+ 1 (mapcar
3447                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3448            ((null thread)
3449             1)
3450            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3451             1)
3452            (t 0))))
3453     (when (and level (zerop level) gnus-tmp-new-adopts)
3454       (incf number
3455             (apply '+ (mapcar
3456                        'gnus-summary-number-of-articles-in-thread
3457                        gnus-tmp-new-adopts))))
3458     (if char
3459         (if (> number 1) gnus-not-empty-thread-mark
3460           gnus-empty-thread-mark)
3461       number)))
3462
3463 (defsubst gnus-summary-line-message-size (head)
3464   "Return pretty-printed version of message size.
3465 This function is intended to be used in
3466 `gnus-summary-line-format-alist'."
3467   (let ((c (or (mail-header-chars head) -1)))
3468     (cond ((< c 0) "n/a")               ; chars not available
3469           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3470           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3471           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3472           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3473
3474
3475 (defun gnus-summary-set-local-parameters (group)
3476   "Go through the local params of GROUP and set all variable specs in that list."
3477   (let ((params (gnus-group-find-parameter group))
3478         (vars '(quit-config))           ; Ignore quit-config.
3479         elem)
3480     (while params
3481       (setq elem (car params)
3482             params (cdr params))
3483       (and (consp elem)                 ; Has to be a cons.
3484            (consp (cdr elem))           ; The cdr has to be a list.
3485            (symbolp (car elem))         ; Has to be a symbol in there.
3486            (not (memq (car elem) vars))
3487            (ignore-errors               ; So we set it.
3488              (push (car elem) vars)
3489              (make-local-variable (car elem))
3490              (set (car elem) (eval (nth 1 elem))))))))
3491
3492 (defun gnus-summary-read-group (group &optional show-all no-article
3493                                       kill-buffer no-display backward
3494                                       select-articles)
3495   "Start reading news in newsgroup GROUP.
3496 If SHOW-ALL is non-nil, already read articles are also listed.
3497 If NO-ARTICLE is non-nil, no article is selected initially.
3498 If NO-DISPLAY, don't generate a summary buffer."
3499   (let (result)
3500     (while (and group
3501                 (null (setq result
3502                             (let ((gnus-auto-select-next nil))
3503                               (or (gnus-summary-read-group-1
3504                                    group show-all no-article
3505                                    kill-buffer no-display
3506                                    select-articles)
3507                                   (setq show-all nil
3508                                         select-articles nil)))))
3509                 (eq gnus-auto-select-next 'quietly))
3510       (set-buffer gnus-group-buffer)
3511       ;; The entry function called above goes to the next
3512       ;; group automatically, so we go two groups back
3513       ;; if we are searching for the previous group.
3514       (when backward
3515         (gnus-group-prev-unread-group 2))
3516       (if (not (equal group (gnus-group-group-name)))
3517           (setq group (gnus-group-group-name))
3518         (setq group nil)))
3519     result))
3520
3521 (defun gnus-summary-read-group-1 (group show-all no-article
3522                                         kill-buffer no-display
3523                                         &optional select-articles)
3524   ;; Killed foreign groups can't be entered.
3525   ;;  (when (and (not (gnus-group-native-p group))
3526   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3527   ;;    (error "Dead non-native groups can't be entered"))
3528   (gnus-message 5 "Retrieving newsgroup: %s..."
3529                 (gnus-group-decoded-name group))
3530   (let* ((new-group (gnus-summary-setup-buffer group))
3531          (quit-config (gnus-group-quit-config group))
3532          (did-select (and new-group (gnus-select-newsgroup
3533                                      group show-all select-articles))))
3534     (cond
3535      ;; This summary buffer exists already, so we just select it.
3536      ((not new-group)
3537       (gnus-set-global-variables)
3538       (when kill-buffer
3539         (gnus-kill-or-deaden-summary kill-buffer))
3540       (gnus-configure-windows 'summary 'force)
3541       (gnus-set-mode-line 'summary)
3542       (gnus-summary-position-point)
3543       (message "")
3544       t)
3545      ;; We couldn't select this group.
3546      ((null did-select)
3547       (when (and (eq major-mode 'gnus-summary-mode)
3548                  (not (equal (current-buffer) kill-buffer)))
3549         (kill-buffer (current-buffer))
3550         (if (not quit-config)
3551             (progn
3552               ;; Update the info -- marks might need to be removed,
3553               ;; for instance.
3554               (gnus-summary-update-info)
3555               (set-buffer gnus-group-buffer)
3556               (gnus-group-jump-to-group group)
3557               (gnus-group-next-unread-group 1))
3558           (gnus-handle-ephemeral-exit quit-config)))
3559       (let ((grpinfo (gnus-get-info group)))
3560         (if (null (gnus-info-read grpinfo))
3561             (gnus-message 3 "Group %s contains no messages"
3562                           (gnus-group-decoded-name group))
3563           (gnus-message 3 "Can't select group")))
3564       nil)
3565      ;; The user did a `C-g' while prompting for number of articles,
3566      ;; so we exit this group.
3567      ((eq did-select 'quit)
3568       (and (eq major-mode 'gnus-summary-mode)
3569            (not (equal (current-buffer) kill-buffer))
3570            (kill-buffer (current-buffer)))
3571       (when kill-buffer
3572         (gnus-kill-or-deaden-summary kill-buffer))
3573       (if (not quit-config)
3574           (progn
3575             (set-buffer gnus-group-buffer)
3576             (gnus-group-jump-to-group group)
3577             (gnus-group-next-unread-group 1)
3578             (gnus-configure-windows 'group 'force))
3579         (gnus-handle-ephemeral-exit quit-config))
3580       ;; Finally signal the quit.
3581       (signal 'quit nil))
3582      ;; The group was successfully selected.
3583      (t
3584       (gnus-set-global-variables)
3585       ;; Save the active value in effect when the group was entered.
3586       (setq gnus-newsgroup-active
3587             (gnus-copy-sequence
3588              (gnus-active gnus-newsgroup-name)))
3589       ;; You can change the summary buffer in some way with this hook.
3590       (gnus-run-hooks 'gnus-select-group-hook)
3591       (gnus-update-format-specifications
3592        nil 'summary 'summary-mode 'summary-dummy)
3593       (gnus-update-summary-mark-positions)
3594       ;; Do score processing.
3595       (when gnus-use-scoring
3596         (gnus-possibly-score-headers))
3597       ;; Check whether to fill in the gaps in the threads.
3598       (when gnus-build-sparse-threads
3599         (gnus-build-sparse-threads))
3600       ;; Find the initial limit.
3601       (if gnus-show-threads
3602           (if show-all
3603               (let ((gnus-newsgroup-dormant nil))
3604                 (gnus-summary-initial-limit show-all))
3605             (gnus-summary-initial-limit show-all))
3606         ;; When unthreaded, all articles are always shown.
3607         (setq gnus-newsgroup-limit
3608               (mapcar
3609                (lambda (header) (mail-header-number header))
3610                gnus-newsgroup-headers)))
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) ; 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* ((display (gnus-group-find-parameter group 'display))
5300          (articles
5301           ;; Select all articles if `read-all' is non-nil, or if there
5302           ;; are no unread articles.
5303           (if (or read-all
5304                   (and (zerop (length gnus-newsgroup-marked))
5305                        (zerop (length gnus-newsgroup-unreads)))
5306                   ;; Fetch all if the predicate is non-nil.
5307                   gnus-newsgroup-display)
5308               ;; We want to select the headers for all the articles in
5309               ;; the group, so we select either all the active
5310               ;; articles in the group, or (if that's nil), the
5311               ;; articles in the cache.
5312               (or
5313                (gnus-uncompress-range (gnus-active group))
5314                (gnus-cache-articles-in-group group))
5315             ;; Select only the "normal" subset of articles.
5316             (gnus-sorted-nunion
5317              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5318              gnus-newsgroup-unreads)))
5319          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5320          (scored (length scored-list))
5321          (number (length articles))
5322          (marked (+ (length gnus-newsgroup-marked)
5323                     (length gnus-newsgroup-dormant)))
5324          (select
5325           (cond
5326            ((numberp read-all)
5327             read-all)
5328            ((numberp gnus-newsgroup-display)
5329             gnus-newsgroup-display)
5330            (t
5331             (condition-case ()
5332                 (cond
5333                  ((and (or (<= scored marked) (= scored number))
5334                        (numberp gnus-large-newsgroup)
5335                        (> number gnus-large-newsgroup))
5336                   (let* ((cursor-in-echo-area nil)
5337                          (initial (gnus-parameter-large-newsgroup-initial
5338                                    gnus-newsgroup-name))
5339                          (input
5340                           (read-string
5341                            (format
5342                             "How many articles from %s (%s %d): "
5343                             (gnus-limit-string
5344                              (gnus-group-decoded-name gnus-newsgroup-name)
5345                              35)
5346                             (if initial "max" "default")
5347                             number)
5348                            (if initial
5349                                (cons (number-to-string initial)
5350                                      0)))))
5351                     (if (string-match "^[ \t]*$" input) number input)))
5352                  ((and (> scored marked) (< scored number)
5353                        (> (- scored number) 20))
5354                   (let ((input
5355                          (read-string
5356                           (format "%s %s (%d scored, %d total): "
5357                                   "How many articles from"
5358                                   (gnus-group-decoded-name group)
5359                                   scored number))))
5360                     (if (string-match "^[ \t]*$" input)
5361                         number input)))
5362                  (t number))
5363               (quit
5364                (message "Quit getting the articles to read")
5365                nil))))))
5366     (setq select (if (stringp select) (string-to-number select) select))
5367     (if (or (null select) (zerop select))
5368         select
5369       (if (and (not (zerop scored)) (<= (abs select) scored))
5370           (progn
5371             (setq articles (sort scored-list '<))
5372             (setq number (length articles)))
5373         (setq articles (copy-sequence articles)))
5374
5375       (when (< (abs select) number)
5376         (if (< select 0)
5377             ;; Select the N oldest articles.
5378             (setcdr (nthcdr (1- (abs select)) articles) nil)
5379           ;; Select the N most recent articles.
5380           (setq articles (nthcdr (- number select) articles))))
5381       (setq gnus-newsgroup-unselected
5382             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5383       (when gnus-alter-articles-to-read-function
5384         (setq articles
5385               (sort
5386                (funcall gnus-alter-articles-to-read-function
5387                         gnus-newsgroup-name articles)
5388                '<)))
5389       articles)))
5390
5391 (defun gnus-killed-articles (killed articles)
5392   (let (out)
5393     (while articles
5394       (when (inline (gnus-member-of-range (car articles) killed))
5395         (push (car articles) out))
5396       (setq articles (cdr articles)))
5397     out))
5398
5399 (defun gnus-uncompress-marks (marks)
5400   "Uncompress the mark ranges in MARKS."
5401   (let ((uncompressed '(score bookmark))
5402         out)
5403     (while marks
5404       (if (memq (caar marks) uncompressed)
5405           (push (car marks) out)
5406         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5407       (setq marks (cdr marks)))
5408     out))
5409
5410 (defun gnus-article-mark-to-type (mark)
5411   "Return the type of MARK."
5412   (or (cadr (assq mark gnus-article-special-mark-lists))
5413       'list))
5414
5415 (defun gnus-article-unpropagatable-p (mark)
5416   "Return whether MARK should be propagated to back end."
5417   (memq mark gnus-article-unpropagated-mark-lists))
5418
5419 (defun gnus-adjust-marked-articles (info)
5420   "Set all article lists and remove all marks that are no longer valid."
5421   (let* ((marked-lists (gnus-info-marks info))
5422          (active (gnus-active (gnus-info-group info)))
5423          (min (car active))
5424          (max (cdr active))
5425          (types gnus-article-mark-lists)
5426          marks var articles article mark mark-type
5427          bgn end)
5428
5429     (dolist (marks marked-lists)
5430       (setq mark (car marks)
5431             mark-type (gnus-article-mark-to-type mark)
5432             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5433
5434       ;; We set the variable according to the type of the marks list,
5435       ;; and then adjust the marks to a subset of the active articles.
5436       (cond
5437        ;; Adjust "simple" lists - compressed yet unsorted
5438        ((eq mark-type 'list)
5439         ;; Simultaneously uncompress and clip to active range
5440         ;; See gnus-uncompress-range for a description of possible marks
5441         (let (l lh)
5442           (if (not (cadr marks))
5443               (set var nil)
5444             (setq articles (if (numberp (cddr marks))
5445                                (list (cdr marks))
5446                              (cdr marks))
5447                   lh (cons nil nil)
5448                   l lh)
5449
5450             (while (setq article (pop articles))
5451               (cond ((consp article)
5452                      (setq bgn (max (car article) min)
5453                            end (min (cdr article) max))
5454                      (while (<= bgn end)
5455                        (setq l (setcdr l (cons bgn nil))
5456                              bgn (1+ bgn))))
5457                     ((and (<= min article)
5458                           (>= max article))
5459                      (setq l (setcdr l (cons article nil))))))
5460             (set var (cdr lh)))))
5461        ;; Adjust assocs.
5462        ((eq mark-type 'tuple)
5463         (set var (setq articles (cdr marks)))
5464         (when (not (listp (cdr (symbol-value var))))
5465           (set var (list (symbol-value var))))
5466         (when (not (listp (cdr articles)))
5467           (setq articles (list articles)))
5468         (while articles
5469           (when (or (not (consp (setq article (pop articles))))
5470                     (< (car article) min)
5471                     (> (car article) max))
5472             (set var (delq article (symbol-value var))))))
5473        ;; Adjust ranges (sloppily).
5474        ((eq mark-type 'range)
5475         (cond
5476          ((eq mark 'seen)
5477           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5478           ;; It should be (seen (NUM1 . NUM2)).
5479           (when (numberp (cddr marks))
5480             (setcdr marks (list (cdr marks))))
5481           (setq articles (cdr marks))
5482           (while (and articles
5483                       (or (and (consp (car articles))
5484                                (> min (cdar articles)))
5485                           (and (numberp (car articles))
5486                                (> min (car articles)))))
5487             (pop articles))
5488           (set var articles))))))))
5489
5490 (defun gnus-update-missing-marks (missing)
5491   "Go through the list of MISSING articles and remove them from the mark lists."
5492   (when missing
5493     (let (var m)
5494       ;; Go through all types.
5495       (dolist (elem gnus-article-mark-lists)
5496         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5497           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5498           (when (symbol-value var)
5499             ;; This list has articles.  So we delete all missing
5500             ;; articles from it.
5501             (setq m missing)
5502             (while m
5503               (set var (delq (pop m) (symbol-value var))))))))))
5504
5505 (defun gnus-update-marks ()
5506   "Enter the various lists of marked articles into the newsgroup info list."
5507   (let ((types gnus-article-mark-lists)
5508         (info (gnus-get-info gnus-newsgroup-name))
5509         type list newmarked symbol delta-marks)
5510     (when info
5511       ;; Add all marks lists to the list of marks lists.
5512       (while (setq type (pop types))
5513         (setq list (symbol-value
5514                     (setq symbol
5515                           (intern (format "gnus-newsgroup-%s" (car type))))))
5516
5517         (when list
5518           ;; Get rid of the entries of the articles that have the
5519           ;; default score.
5520           (when (and (eq (cdr type) 'score)
5521                      gnus-save-score
5522                      list)
5523             (let* ((arts list)
5524                    (prev (cons nil list))
5525                    (all prev))
5526               (while arts
5527                 (if (or (not (consp (car arts)))
5528                         (= (cdar arts) gnus-summary-default-score))
5529                     (setcdr prev (cdr arts))
5530                   (setq prev arts))
5531                 (setq arts (cdr arts)))
5532               (setq list (cdr all)))))
5533
5534         (when (eq (cdr type) 'seen)
5535           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5536
5537         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5538           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5539
5540         (when (and (gnus-check-backend-function
5541                     'request-set-mark gnus-newsgroup-name)
5542                    (not (gnus-article-unpropagatable-p (cdr type))))
5543           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5544                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5545                  (add (gnus-remove-from-range
5546                        (gnus-copy-sequence list) old)))
5547             (when add
5548               (push (list add 'add (list (cdr type))) delta-marks))
5549             (when del
5550               (push (list del 'del (list (cdr type))) delta-marks))))
5551
5552         (when list
5553           (push (cons (cdr type) list) newmarked)))
5554
5555       (when delta-marks
5556         (unless (gnus-check-group gnus-newsgroup-name)
5557           (error "Can't open server for %s" gnus-newsgroup-name))
5558         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5559
5560       ;; Enter these new marks into the info of the group.
5561       (if (nthcdr 3 info)
5562           (setcar (nthcdr 3 info) newmarked)
5563         ;; Add the marks lists to the end of the info.
5564         (when newmarked
5565           (setcdr (nthcdr 2 info) (list newmarked))))
5566
5567       ;; Cut off the end of the info if there's nothing else there.
5568       (let ((i 5))
5569         (while (and (> i 2)
5570                     (not (nth i info)))
5571           (when (nthcdr (decf i) info)
5572             (setcdr (nthcdr i info) nil)))))))
5573
5574 (defun gnus-set-mode-line (where)
5575   "Set the mode line of the article or summary buffers.
5576 If WHERE is `summary', the summary mode line format will be used."
5577   ;; Is this mode line one we keep updated?
5578   (when (and (memq where gnus-updated-mode-lines)
5579              (symbol-value
5580               (intern (format "gnus-%s-mode-line-format-spec" where))))
5581     (let (mode-string)
5582       (save-excursion
5583         ;; We evaluate this in the summary buffer since these
5584         ;; variables are buffer-local to that buffer.
5585         (set-buffer gnus-summary-buffer)
5586        ;; We bind all these variables that are used in the `eval' form
5587         ;; below.
5588         (let* ((mformat (symbol-value
5589                          (intern
5590                           (format "gnus-%s-mode-line-format-spec" where))))
5591                (gnus-tmp-group-name (gnus-group-decoded-name
5592                                      gnus-newsgroup-name))
5593                (gnus-tmp-article-number (or gnus-current-article 0))
5594                (gnus-tmp-unread gnus-newsgroup-unreads)
5595                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5596                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5597                (gnus-tmp-unread-and-unselected
5598                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5599                             (zerop gnus-tmp-unselected))
5600                        "")
5601                       ((zerop gnus-tmp-unselected)
5602                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5603                       (t (format "{%d(+%d) more}"
5604                                  gnus-tmp-unread-and-unticked
5605                                  gnus-tmp-unselected))))
5606                (gnus-tmp-subject
5607                 (if (and gnus-current-headers
5608                          (vectorp gnus-current-headers))
5609                     (gnus-mode-string-quote
5610                      (mail-header-subject gnus-current-headers))
5611                   ""))
5612                bufname-length max-len
5613                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5614           (setq mode-string (eval mformat))
5615           (setq bufname-length (if (string-match "%b" mode-string)
5616                                    (- (length
5617                                        (buffer-name
5618                                         (if (eq where 'summary)
5619                                             nil
5620                                           (get-buffer gnus-article-buffer))))
5621                                       2)
5622                                  0))
5623           (setq max-len (max 4 (if gnus-mode-non-string-length
5624                                    (- (window-width)
5625                                       gnus-mode-non-string-length
5626                                       bufname-length)
5627                                  (length mode-string))))
5628           ;; We might have to chop a bit of the string off...
5629           (when (> (length mode-string) max-len)
5630             (setq mode-string
5631                   (concat (truncate-string-to-width mode-string (- max-len 3))
5632                           "...")))
5633           ;; Pad the mode string a bit.
5634           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5635       ;; Update the mode line.
5636       (setq mode-line-buffer-identification
5637             (gnus-mode-line-buffer-identification (list mode-string)))
5638       (set-buffer-modified-p t))))
5639
5640 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5641   "Go through the HEADERS list and add all Xrefs to a hash table.
5642 The resulting hash table is returned, or nil if no Xrefs were found."
5643   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5644          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5645          (xref-hashtb (gnus-make-hashtable))
5646          start group entry number xrefs header)
5647     (while headers
5648       (setq header (pop headers))
5649       (when (and (setq xrefs (mail-header-xref header))
5650                  (not (memq (setq number (mail-header-number header))
5651                             unreads)))
5652         (setq start 0)
5653         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5654           (setq start (match-end 0))
5655           (setq group (if prefix
5656                           (concat prefix (substring xrefs (match-beginning 1)
5657                                                     (match-end 1)))
5658                         (substring xrefs (match-beginning 1) (match-end 1))))
5659           (setq number
5660                 (string-to-int (substring xrefs (match-beginning 2)
5661                                           (match-end 2))))
5662           (if (setq entry (gnus-gethash group xref-hashtb))
5663               (setcdr entry (cons number (cdr entry)))
5664             (gnus-sethash group (cons number nil) xref-hashtb)))))
5665     (and start xref-hashtb)))
5666
5667 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5668   "Look through all the headers and mark the Xrefs as read."
5669   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5670         name info xref-hashtb idlist method nth4)
5671     (save-excursion
5672       (set-buffer gnus-group-buffer)
5673       (when (setq xref-hashtb
5674                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5675         (mapatoms
5676          (lambda (group)
5677            (unless (string= from-newsgroup (setq name (symbol-name group)))
5678              (setq idlist (symbol-value group))
5679              ;; Dead groups are not updated.
5680              (and (prog1
5681                       (setq info (gnus-get-info name))
5682                     (when (stringp (setq nth4 (gnus-info-method info)))
5683                       (setq nth4 (gnus-server-to-method nth4))))
5684                   ;; Only do the xrefs if the group has the same
5685                   ;; select method as the group we have just read.
5686                   (or (gnus-methods-equal-p
5687                        nth4 (gnus-find-method-for-group from-newsgroup))
5688                       virtual
5689                       (equal nth4 (setq method (gnus-find-method-for-group
5690                                                 from-newsgroup)))
5691                       (and (equal (car nth4) (car method))
5692                            (equal (nth 1 nth4) (nth 1 method))))
5693                   gnus-use-cross-reference
5694                   (or (not (eq gnus-use-cross-reference t))
5695                       virtual
5696                       ;; Only do cross-references on subscribed
5697                       ;; groups, if that is what is wanted.
5698                       (<= (gnus-info-level info) gnus-level-subscribed))
5699                   (gnus-group-make-articles-read name idlist))))
5700          xref-hashtb)))))
5701
5702 (defun gnus-compute-read-articles (group articles)
5703   (let* ((entry (gnus-group-entry group))
5704          (info (nth 2 entry))
5705          (active (gnus-active group))
5706          ninfo)
5707     (when entry
5708       ;; First peel off all invalid article numbers.
5709       (when active
5710         (let ((ids articles)
5711               id first)
5712           (while (setq id (pop ids))
5713             (when (and first (> id (cdr active)))
5714               ;; We'll end up in this situation in one particular
5715               ;; obscure situation.  If you re-scan a group and get
5716               ;; a new article that is cross-posted to a different
5717               ;; group that has not been re-scanned, you might get
5718               ;; crossposted article that has a higher number than
5719               ;; Gnus believes possible.  So we re-activate this
5720               ;; group as well.  This might mean doing the
5721               ;; crossposting thingy will *increase* the number
5722               ;; of articles in some groups.  Tsk, tsk.
5723               (setq active (or (gnus-activate-group group) active)))
5724             (when (or (> id (cdr active))
5725                       (< id (car active)))
5726               (setq articles (delq id articles))))))
5727       ;; If the read list is nil, we init it.
5728       (if (and active
5729                (null (gnus-info-read info))
5730                (> (car active) 1))
5731           (setq ninfo (cons 1 (1- (car active))))
5732         (setq ninfo (gnus-info-read info)))
5733       ;; Then we add the read articles to the range.
5734       (gnus-add-to-range
5735        ninfo (setq articles (sort articles '<))))))
5736
5737 (defun gnus-group-make-articles-read (group articles)
5738   "Update the info of GROUP to say that ARTICLES are read."
5739   (let* ((num 0)
5740          (entry (gnus-group-entry group))
5741          (info (nth 2 entry))
5742          (active (gnus-active group))
5743          range)
5744     (when entry
5745       (setq range (gnus-compute-read-articles group articles))
5746       (with-current-buffer gnus-group-buffer
5747         (gnus-undo-register
5748           `(progn
5749              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5750              (gnus-info-set-read ',info ',(gnus-info-read info))
5751              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5752              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5753              (gnus-group-update-group ,group t))))
5754       ;; Add the read articles to the range.
5755       (gnus-info-set-read info range)
5756       (gnus-request-set-mark group (list (list range 'add '(read))))
5757       ;; Then we have to re-compute how many unread
5758       ;; articles there are in this group.
5759       (when active
5760         (cond
5761          ((not range)
5762           (setq num (- (1+ (cdr active)) (car active))))
5763          ((not (listp (cdr range)))
5764           (setq num (- (cdr active) (- (1+ (cdr range))
5765                                        (car range)))))
5766          (t
5767           (while range
5768             (if (numberp (car range))
5769                 (setq num (1+ num))
5770               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5771             (setq range (cdr range)))
5772           (setq num (- (cdr active) num))))
5773         ;; Update the number of unread articles.
5774         (setcar entry num)
5775         ;; Update the group buffer.
5776         (unless (gnus-ephemeral-group-p group)
5777           (gnus-group-update-group group t))))))
5778
5779 (defvar gnus-newsgroup-none-id 0)
5780
5781 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5782   (let ((cur nntp-server-buffer)
5783         (dependencies
5784          (or dependencies
5785              (with-current-buffer gnus-summary-buffer
5786                gnus-newsgroup-dependencies)))
5787         headers id end ref
5788         (mail-parse-charset gnus-newsgroup-charset)
5789         (mail-parse-ignored-charsets
5790          (save-excursion (condition-case nil
5791                              (set-buffer gnus-summary-buffer)
5792                            (error))
5793                          gnus-newsgroup-ignored-charsets)))
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       ;; Translate all TAB characters into SPACE characters.
5797       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5799       (ietf-drums-unfold-fws)
5800       (gnus-run-hooks 'gnus-parse-headers-hook)
5801       (let ((case-fold-search t)
5802             in-reply-to header p lines chars)
5803         (goto-char (point-min))
5804         ;; Search to the beginning of the next header.  Error messages
5805         ;; do not begin with 2 or 3.
5806         (while (re-search-forward "^[23][0-9]+ " nil t)
5807           (setq id nil
5808                 ref nil)
5809           ;; This implementation of this function, with nine
5810           ;; search-forwards instead of the one re-search-forward and
5811           ;; a case (which basically was the old function) is actually
5812           ;; about twice as fast, even though it looks messier.  You
5813           ;; can't have everything, I guess.  Speed and elegance
5814           ;; doesn't always go hand in hand.
5815           (setq
5816            header
5817            (vector
5818             ;; Number.
5819             (prog1
5820                 (read cur)
5821               (end-of-line)
5822               (setq p (point))
5823               (narrow-to-region (point)
5824                                 (or (and (search-forward "\n.\n" nil t)
5825                                          (- (point) 2))
5826                                     (point))))
5827             ;; Subject.
5828             (progn
5829               (goto-char p)
5830               (if (search-forward "\nsubject:" nil t)
5831                   (funcall gnus-decode-encoded-word-function
5832                            (nnheader-header-value))
5833                 "(none)"))
5834             ;; From.
5835             (progn
5836               (goto-char p)
5837               (if (search-forward "\nfrom:" nil t)
5838                   (funcall gnus-decode-encoded-word-function
5839                            (nnheader-header-value))
5840                 "(nobody)"))
5841             ;; Date.
5842             (progn
5843               (goto-char p)
5844               (if (search-forward "\ndate:" nil t)
5845                   (nnheader-header-value) ""))
5846             ;; Message-ID.
5847             (progn
5848               (goto-char p)
5849               (setq id (if (re-search-forward
5850                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5851                            ;; We do it this way to make sure the Message-ID
5852                            ;; is (somewhat) syntactically valid.
5853                            (buffer-substring (match-beginning 1)
5854                                              (match-end 1))
5855                          ;; If there was no message-id, we just fake one
5856                          ;; to make subsequent routines simpler.
5857                          (nnheader-generate-fake-message-id))))
5858             ;; References.
5859             (progn
5860               (goto-char p)
5861               (if (search-forward "\nreferences:" nil t)
5862                   (progn
5863                     (setq end (point))
5864                     (prog1
5865                         (nnheader-header-value)
5866                       (setq ref
5867                             (buffer-substring
5868                              (progn
5869                                (end-of-line)
5870                                (search-backward ">" end t)
5871                                (1+ (point)))
5872                              (progn
5873                                (search-backward "<" end t)
5874                                (point))))))
5875                 ;; Get the references from the in-reply-to header if there
5876                 ;; were no references and the in-reply-to header looks
5877                 ;; promising.
5878                 (if (and (search-forward "\nin-reply-to:" nil t)
5879                          (setq in-reply-to (nnheader-header-value))
5880                          (string-match "<[^>]+>" in-reply-to))
5881                     (let (ref2)
5882                       (setq ref (substring in-reply-to (match-beginning 0)
5883                                            (match-end 0)))
5884                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5885                         (setq ref2 (substring in-reply-to (match-beginning 0)
5886                                               (match-end 0)))
5887                         (when (> (length ref2) (length ref))
5888                           (setq ref ref2)))
5889                       ref)
5890                   (setq ref nil))))
5891             ;; Chars.
5892             (progn
5893               (goto-char p)
5894               (if (search-forward "\nchars: " nil t)
5895                   (if (numberp (setq chars (ignore-errors (read cur))))
5896                       chars -1)
5897                 -1))
5898             ;; Lines.
5899             (progn
5900               (goto-char p)
5901               (if (search-forward "\nlines: " nil t)
5902                   (if (numberp (setq lines (ignore-errors (read cur))))
5903                       lines -1)
5904                 -1))
5905             ;; Xref.
5906             (progn
5907               (goto-char p)
5908               (and (search-forward "\nxref:" nil t)
5909                    (nnheader-header-value)))
5910             ;; Extra.
5911             (when gnus-extra-headers
5912               (let ((extra gnus-extra-headers)
5913                     out)
5914                 (while extra
5915                   (goto-char p)
5916                   (when (search-forward
5917                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5918                     (push (cons (car extra) (nnheader-header-value))
5919                           out))
5920                   (pop extra))
5921                 out))))
5922           (when (equal id ref)
5923             (setq ref nil))
5924
5925           (when gnus-alter-header-function
5926             (funcall gnus-alter-header-function header)
5927             (setq id (mail-header-id header)
5928                   ref (gnus-parent-id (mail-header-references header))))
5929
5930           (when (setq header
5931                       (gnus-dependencies-add-header
5932                        header dependencies force-new))
5933             (push header headers))
5934           (goto-char (point-max))
5935           (widen))
5936         (nreverse headers)))))
5937
5938 ;; Goes through the xover lines and returns a list of vectors
5939 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5940                                                   force-new dependencies
5941                                                   group also-fetch-heads)
5942   "Parse the news overview data in the server buffer.
5943 Return a list of headers that match SEQUENCE (see
5944 `nntp-retrieve-headers')."
5945   ;; Get the Xref when the users reads the articles since most/some
5946   ;; NNTP servers do not include Xrefs when using XOVER.
5947   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5948   (let ((mail-parse-charset gnus-newsgroup-charset)
5949         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5950         (cur nntp-server-buffer)
5951         (dependencies (or dependencies gnus-newsgroup-dependencies))
5952         (allp (cond
5953                ((eq gnus-read-all-available-headers t)
5954                 t)
5955                ((stringp gnus-read-all-available-headers)
5956                 (string-match gnus-read-all-available-headers group))
5957                (t
5958                 nil)))
5959         number headers header)
5960     (save-excursion
5961       (set-buffer nntp-server-buffer)
5962       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5963       ;; Allow the user to mangle the headers before parsing them.
5964       (gnus-run-hooks 'gnus-parse-headers-hook)
5965       (goto-char (point-min))
5966       (gnus-parse-without-error
5967         (while (and (or sequence allp)
5968                     (not (eobp)))
5969           (setq number (read cur))
5970           (when (not allp)
5971             (while (and sequence
5972                         (< (car sequence) number))
5973               (setq sequence (cdr sequence))))
5974           (when (and (or allp
5975                          (and sequence
5976                               (eq number (car sequence))))
5977                      (progn
5978                        (setq sequence (cdr sequence))
5979                        (setq header (inline
5980                                       (gnus-nov-parse-line
5981                                        number dependencies force-new)))))
5982             (push header headers))
5983           (forward-line 1)))
5984       ;; A common bug in inn is that if you have posted an article and
5985       ;; then retrieves the active file, it will answer correctly --
5986       ;; the new article is included.  However, a NOV entry for the
5987       ;; article may not have been generated yet, so this may fail.
5988       ;; We work around this problem by retrieving the last few
5989       ;; headers using HEAD.
5990       (if (or (not also-fetch-heads)
5991               (not sequence))
5992           ;; We (probably) got all the headers.
5993           (nreverse headers)
5994         (let ((gnus-nov-is-evil t))
5995           (nconc
5996            (nreverse headers)
5997            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5998              (gnus-get-newsgroup-headers))))))))
5999
6000 (defun gnus-article-get-xrefs ()
6001   "Fill in the Xref value in `gnus-current-headers', if necessary.
6002 This is meant to be called in `gnus-article-internal-prepare-hook'."
6003   (let ((headers (with-current-buffer gnus-summary-buffer
6004                    gnus-current-headers)))
6005     (or (not gnus-use-cross-reference)
6006         (not headers)
6007         (and (mail-header-xref headers)
6008              (not (string= (mail-header-xref headers) "")))
6009         (let ((case-fold-search t)
6010               xref)
6011           (save-restriction
6012             (nnheader-narrow-to-headers)
6013             (goto-char (point-min))
6014             (when (or (and (not (eobp))
6015                            (eq (downcase (char-after)) ?x)
6016                            (looking-at "Xref:"))
6017                       (search-forward "\nXref:" nil t))
6018               (goto-char (1+ (match-end 0)))
6019               (setq xref (buffer-substring (point) (point-at-eol)))
6020               (mail-header-set-xref headers xref)))))))
6021
6022 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6023   "Find article ID and insert the summary line for that article.
6024 OLD-HEADER can either be a header or a line number to insert
6025 the subject line on."
6026   (let* ((line (and (numberp old-header) old-header))
6027          (old-header (and (vectorp old-header) old-header))
6028          (header (cond ((and old-header use-old-header)
6029                         old-header)
6030                        ((and (numberp id)
6031                              (gnus-number-to-header id))
6032                         (gnus-number-to-header id))
6033                        (t
6034                         (gnus-read-header id))))
6035          (number (and (numberp id) id))
6036          d)
6037     (when header
6038       ;; Rebuild the thread that this article is part of and go to the
6039       ;; article we have fetched.
6040       (when (and (not gnus-show-threads)
6041                  old-header)
6042         (when (and number
6043                    (setq d (gnus-data-find (mail-header-number old-header))))
6044           (goto-char (gnus-data-pos d))
6045           (gnus-data-remove
6046            number
6047            (- (point-at-bol)
6048               (prog1
6049                   (1+ (point-at-eol))
6050                 (gnus-delete-line))))))
6051       (when old-header
6052         (mail-header-set-number header (mail-header-number old-header)))
6053       (setq gnus-newsgroup-sparse
6054             (delq (setq number (mail-header-number header))
6055                   gnus-newsgroup-sparse))
6056       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6057       (push number gnus-newsgroup-limit)
6058       (gnus-rebuild-thread (mail-header-id header) line)
6059       (gnus-summary-goto-subject number nil t))
6060     (when (and (numberp number)
6061                (> number 0))
6062       ;; We have to update the boundaries even if we can't fetch the
6063       ;; article if ID is a number -- so that the next `P' or `N'
6064       ;; command will fetch the previous (or next) article even
6065       ;; if the one we tried to fetch this time has been canceled.
6066       (when (> number gnus-newsgroup-end)
6067         (setq gnus-newsgroup-end number))
6068       (when (< number gnus-newsgroup-begin)
6069         (setq gnus-newsgroup-begin number))
6070       (setq gnus-newsgroup-unselected
6071             (delq number gnus-newsgroup-unselected)))
6072     ;; Report back a success?
6073     (and header (mail-header-number header))))
6074
6075 ;;; Process/prefix in the summary buffer
6076
6077 (defun gnus-summary-work-articles (n)
6078   "Return a list of articles to be worked upon.
6079 The prefix argument, the list of process marked articles, and the
6080 current article will be taken into consideration."
6081   (save-excursion
6082     (set-buffer gnus-summary-buffer)
6083     (cond
6084      (n
6085       ;; A numerical prefix has been given.
6086       (setq n (prefix-numeric-value n))
6087       (let ((backward (< n 0))
6088             (n (abs (prefix-numeric-value n)))
6089             articles article)
6090         (save-excursion
6091           (while
6092               (and (> n 0)
6093                    (push (setq article (gnus-summary-article-number))
6094                          articles)
6095                    (if backward
6096                        (gnus-summary-find-prev nil article)
6097                      (gnus-summary-find-next nil article)))
6098             (decf n)))
6099         (nreverse articles)))
6100      ((and (gnus-region-active-p) (mark))
6101       (message "region active")
6102       ;; Work on the region between point and mark.
6103       (let ((max (max (point) (mark)))
6104             articles article)
6105         (save-excursion
6106           (goto-char (min (min (point) (mark))))
6107           (while
6108               (and
6109                (push (setq article (gnus-summary-article-number)) articles)
6110                (gnus-summary-find-next nil article)
6111                (< (point) max)))
6112           (nreverse articles))))
6113      (gnus-newsgroup-processable
6114       ;; There are process-marked articles present.
6115       ;; Save current state.
6116       (gnus-summary-save-process-mark)
6117       ;; Return the list.
6118       (reverse gnus-newsgroup-processable))
6119      (t
6120       ;; Just return the current article.
6121       (list (gnus-summary-article-number))))))
6122
6123 (defmacro gnus-summary-iterate (arg &rest forms)
6124   "Iterate over the process/prefixed articles and do FORMS.
6125 ARG is the interactive prefix given to the command.  FORMS will be
6126 executed with point over the summary line of the articles."
6127   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6128     `(let ((,articles (gnus-summary-work-articles ,arg)))
6129        (while ,articles
6130          (gnus-summary-goto-subject (car ,articles))
6131          ,@forms
6132          (pop ,articles)))))
6133
6134 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6135 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6136
6137 (defun gnus-summary-save-process-mark ()
6138   "Push the current set of process marked articles on the stack."
6139   (interactive)
6140   (push (copy-sequence gnus-newsgroup-processable)
6141         gnus-newsgroup-process-stack))
6142
6143 (defun gnus-summary-kill-process-mark ()
6144   "Push the current set of process marked articles on the stack and unmark."
6145   (interactive)
6146   (gnus-summary-save-process-mark)
6147   (gnus-summary-unmark-all-processable))
6148
6149 (defun gnus-summary-yank-process-mark ()
6150   "Pop the last process mark state off the stack and restore it."
6151   (interactive)
6152   (unless gnus-newsgroup-process-stack
6153     (error "Empty mark stack"))
6154   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6155
6156 (defun gnus-summary-process-mark-set (set)
6157   "Make SET into the current process marked articles."
6158   (gnus-summary-unmark-all-processable)
6159   (while set
6160     (gnus-summary-set-process-mark (pop set))))
6161
6162 ;;; Searching and stuff
6163
6164 (defun gnus-summary-search-group (&optional backward use-level)
6165   "Search for next unread newsgroup.
6166 If optional argument BACKWARD is non-nil, search backward instead."
6167   (save-excursion
6168     (set-buffer gnus-group-buffer)
6169     (when (gnus-group-search-forward
6170            backward nil (if use-level (gnus-group-group-level) nil))
6171       (gnus-group-group-name))))
6172
6173 (defun gnus-summary-best-group (&optional exclude-group)
6174   "Find the name of the best unread group.
6175 If EXCLUDE-GROUP, do not go to this group."
6176   (with-current-buffer gnus-group-buffer
6177     (save-excursion
6178       (gnus-group-best-unread-group exclude-group))))
6179
6180 (defun gnus-summary-find-next (&optional unread article backward)
6181   (if backward
6182       (gnus-summary-find-prev unread article)
6183     (let* ((dummy (gnus-summary-article-intangible-p))
6184            (article (or article (gnus-summary-article-number)))
6185            (data (gnus-data-find-list article))
6186            result)
6187       (when (and (not dummy)
6188                  (or (not gnus-summary-check-current)
6189                      (not unread)
6190                      (not (gnus-data-unread-p (car data)))))
6191         (setq data (cdr data)))
6192       (when (setq result
6193                   (if unread
6194                       (progn
6195                         (while data
6196                           (unless (memq (gnus-data-number (car data))
6197                                         (cond
6198                                          ((eq gnus-auto-goto-ignores
6199                                               'always-undownloaded)
6200                                           gnus-newsgroup-undownloaded)
6201                                          (gnus-plugged
6202                                           nil)
6203                                          ((eq gnus-auto-goto-ignores
6204                                               'unfetched)
6205                                           gnus-newsgroup-unfetched)
6206                                          ((eq gnus-auto-goto-ignores
6207                                               'undownloaded)
6208                                           gnus-newsgroup-undownloaded)))
6209                             (when (gnus-data-unread-p (car data))
6210                               (setq result (car data)
6211                                     data nil)))
6212                           (setq data (cdr data)))
6213                         result)
6214                     (car data)))
6215         (goto-char (gnus-data-pos result))
6216         (gnus-data-number result)))))
6217
6218 (defun gnus-summary-find-prev (&optional unread article)
6219   (let* ((eobp (eobp))
6220          (article (or article (gnus-summary-article-number)))
6221          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6222          result)
6223     (when (and (not eobp)
6224                (or (not gnus-summary-check-current)
6225                    (not unread)
6226                    (not (gnus-data-unread-p (car data)))))
6227       (setq data (cdr data)))
6228     (when (setq result
6229                 (if unread
6230                     (progn
6231                       (while data
6232                         (unless (memq (gnus-data-number (car data))
6233                                       (cond
6234                                        ((eq gnus-auto-goto-ignores
6235                                             'always-undownloaded)
6236                                         gnus-newsgroup-undownloaded)
6237                                        (gnus-plugged
6238                                         nil)
6239                                        ((eq gnus-auto-goto-ignores
6240                                             'unfetched)
6241                                         gnus-newsgroup-unfetched)
6242                                        ((eq gnus-auto-goto-ignores
6243                                             'undownloaded)
6244                                         gnus-newsgroup-undownloaded)))
6245                           (when (gnus-data-unread-p (car data))
6246                             (setq result (car data)
6247                                   data nil)))
6248                         (setq data (cdr data)))
6249                       result)
6250                   (car data)))
6251       (goto-char (gnus-data-pos result))
6252       (gnus-data-number result))))
6253
6254 (defun gnus-summary-find-subject (subject &optional unread backward article)
6255   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6256          (article (or article (gnus-summary-article-number)))
6257          (articles (gnus-data-list backward))
6258          (arts (gnus-data-find-list article articles))
6259          result)
6260     (when (or (not gnus-summary-check-current)
6261               (not unread)
6262               (not (gnus-data-unread-p (car arts))))
6263       (setq arts (cdr arts)))
6264     (while arts
6265       (and (or (not unread)
6266                (gnus-data-unread-p (car arts)))
6267            (vectorp (gnus-data-header (car arts)))
6268            (gnus-subject-equal
6269             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6270            (setq result (car arts)
6271                  arts nil))
6272       (setq arts (cdr arts)))
6273     (and result
6274          (goto-char (gnus-data-pos result))
6275          (gnus-data-number result))))
6276
6277 (defun gnus-summary-search-forward (&optional unread subject backward)
6278   "Search forward for an article.
6279 If UNREAD, look for unread articles.  If SUBJECT, look for
6280 articles with that subject.  If BACKWARD, search backward instead."
6281   (cond (subject (gnus-summary-find-subject subject unread backward))
6282         (backward (gnus-summary-find-prev unread))
6283         (t (gnus-summary-find-next unread))))
6284
6285 (defun gnus-recenter (&optional n)
6286   "Center point in window and redisplay frame.
6287 Also do horizontal recentering."
6288   (interactive "P")
6289   (when (and gnus-auto-center-summary
6290              (not (eq gnus-auto-center-summary 'vertical)))
6291     (gnus-horizontal-recenter))
6292   (recenter n))
6293
6294 (defun gnus-summary-recenter ()
6295   "Center point in the summary window.
6296 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6297 displayed, no centering will be performed."
6298   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6299   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6300   (interactive)
6301   ;; The user has to want it.
6302   (when gnus-auto-center-summary
6303     (let* ((top (cond ((< (window-height) 4) 0)
6304                       ((< (window-height) 7) 1)
6305                       (t (if (numberp gnus-auto-center-summary)
6306                              gnus-auto-center-summary
6307                            2))))
6308            (height (1- (window-height)))
6309            (bottom (save-excursion (goto-char (point-max))
6310                                    (forward-line (- height))
6311                                    (point)))
6312            (window (get-buffer-window (current-buffer))))
6313       (when (get-buffer-window gnus-article-buffer)
6314         ;; Only do recentering when the article buffer is displayed,
6315         ;; Set the window start to either `bottom', which is the biggest
6316         ;; possible valid number, or the second line from the top,
6317         ;; whichever is the least.
6318         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6319           (if (> bottom top-pos)
6320               ;; Keep the second line from the top visible
6321               (set-window-start window top-pos t)
6322             ;; Try to keep the bottom line visible; if it's partially
6323             ;; obscured, either scroll one more line to make it fully
6324             ;; visible, or revert to using TOP-POS.
6325             (save-excursion
6326               (goto-char (point-max))
6327               (forward-line -1)
6328               (let ((last-line-start (point)))
6329                 (goto-char bottom)
6330                 (set-window-start window (point) t)
6331                 (when (not (pos-visible-in-window-p last-line-start window))
6332                   (forward-line 1)
6333                   (set-window-start window (min (point) top-pos) t)))))))
6334       ;; Do horizontal recentering while we're at it.
6335       (when (and (get-buffer-window (current-buffer) t)
6336                  (not (eq gnus-auto-center-summary 'vertical)))
6337         (let ((selected (selected-window)))
6338           (select-window (get-buffer-window (current-buffer) t))
6339           (gnus-summary-position-point)
6340           (gnus-horizontal-recenter)
6341           (select-window selected))))))
6342
6343 (defun gnus-summary-jump-to-group (newsgroup)
6344   "Move point to NEWSGROUP in group mode buffer."
6345   ;; Keep update point of group mode buffer if visible.
6346   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6347       (save-window-excursion
6348         ;; Take care of tree window mode.
6349         (when (get-buffer-window gnus-group-buffer)
6350           (pop-to-buffer gnus-group-buffer))
6351         (gnus-group-jump-to-group newsgroup))
6352     (save-excursion
6353       ;; Take care of tree window mode.
6354       (if (get-buffer-window gnus-group-buffer)
6355           (pop-to-buffer gnus-group-buffer)
6356         (set-buffer gnus-group-buffer))
6357       (gnus-group-jump-to-group newsgroup))))
6358
6359 ;; This function returns a list of article numbers based on the
6360 ;; difference between the ranges of read articles in this group and
6361 ;; the range of active articles.
6362 (defun gnus-list-of-unread-articles (group)
6363   (let* ((read (gnus-info-read (gnus-get-info group)))
6364          (active (or (gnus-active group) (gnus-activate-group group)))
6365          (last (cdr active))
6366          first nlast unread)
6367     ;; If none are read, then all are unread.
6368     (if (not read)
6369         (setq first (car active))
6370       ;; If the range of read articles is a single range, then the
6371       ;; first unread article is the article after the last read
6372       ;; article.  Sounds logical, doesn't it?
6373       (if (and (not (listp (cdr read)))
6374                (or (< (car read) (car active))
6375                    (progn (setq read (list read))
6376                           nil)))
6377           (setq first (max (car active) (1+ (cdr read))))
6378         ;; `read' is a list of ranges.
6379         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6380                                   (caar read)))
6381                   1)
6382           (setq first (car active)))
6383         (while read
6384           (when first
6385             (while (< first nlast)
6386               (setq unread (cons first unread)
6387                     first (1+ first))))
6388           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6389           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6390           (setq read (cdr read)))))
6391     ;; And add the last unread articles.
6392     (while (<= first last)
6393       (setq unread (cons first unread)
6394             first (1+ first)))
6395     ;; Return the list of unread articles.
6396     (delq 0 (nreverse unread))))
6397
6398 (defun gnus-list-of-read-articles (group)
6399   "Return a list of unread, unticked and non-dormant articles."
6400   (let* ((info (gnus-get-info group))
6401          (marked (gnus-info-marks info))
6402          (active (gnus-active group)))
6403     (and info active
6404          (gnus-list-range-difference
6405           (gnus-list-range-difference
6406            (gnus-sorted-complement
6407             (gnus-uncompress-range active)
6408             (gnus-list-of-unread-articles group))
6409            (cdr (assq 'dormant marked)))
6410           (cdr (assq 'tick marked))))))
6411
6412 ;; This function returns a sequence of article numbers based on the
6413 ;; difference between the ranges of read articles in this group and
6414 ;; the range of active articles.
6415 (defun gnus-sequence-of-unread-articles (group)
6416   (let* ((read (gnus-info-read (gnus-get-info group)))
6417          (active (or (gnus-active group) (gnus-activate-group group)))
6418          (last (cdr active))
6419          first nlast unread)
6420     ;; If none are read, then all are unread.
6421     (if (not read)
6422         (setq first (car active))
6423       ;; If the range of read articles is a single range, then the
6424       ;; first unread article is the article after the last read
6425       ;; article.  Sounds logical, doesn't it?
6426       (if (and (not (listp (cdr read)))
6427                (or (< (car read) (car active))
6428                    (progn (setq read (list read))
6429                           nil)))
6430           (setq first (max (car active) (1+ (cdr read))))
6431         ;; `read' is a list of ranges.
6432         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6433                                   (caar read)))
6434                   1)
6435           (setq first (car active)))
6436         (while read
6437           (when first
6438             (push (cons first nlast) unread))
6439           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6440           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6441           (setq read (cdr read)))))
6442     ;; And add the last unread articles.
6443     (cond ((< first last)
6444            (push (cons first last) unread))
6445           ((= first last)
6446            (push first unread)))
6447     ;; Return the sequence of unread articles.
6448     (delq 0 (nreverse unread))))
6449
6450 ;; Various summary commands
6451
6452 (defun gnus-summary-select-article-buffer ()
6453   "Reconfigure windows to show article buffer."
6454   (interactive)
6455   (if (not (gnus-buffer-live-p gnus-article-buffer))
6456       (error "There is no article buffer for this summary buffer")
6457     (gnus-configure-windows 'article)
6458     (select-window (get-buffer-window gnus-article-buffer))))
6459
6460 (defun gnus-summary-universal-argument (arg)
6461   "Perform any operation on all articles that are process/prefixed."
6462   (interactive "P")
6463   (let ((articles (gnus-summary-work-articles arg))
6464         func article)
6465     (if (eq
6466          (setq
6467           func
6468           (key-binding
6469            (read-key-sequence
6470             (substitute-command-keys
6471              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6472          'undefined)
6473         (gnus-error 1 "Undefined key")
6474       (save-excursion
6475         (while articles
6476           (gnus-summary-goto-subject (setq article (pop articles)))
6477           (let (gnus-newsgroup-processable)
6478             (command-execute func))
6479           (gnus-summary-remove-process-mark article)))))
6480   (gnus-summary-position-point))
6481
6482 (defun gnus-summary-toggle-truncation (&optional arg)
6483   "Toggle truncation of summary lines.
6484 With ARG, turn line truncation on if ARG is positive."
6485   (interactive "P")
6486   (setq truncate-lines
6487         (if (null arg) (not truncate-lines)
6488           (> (prefix-numeric-value arg) 0)))
6489   (redraw-display))
6490
6491 (defun gnus-summary-find-for-reselect ()
6492   "Return the number of an article to stay on across a reselect.
6493 The current article is considered, then following articles, then previous
6494 articles.  An article is sought which is not cancelled and isn't a temporary
6495 insertion from another group.  If there's no such then return a dummy 0."
6496   (let (found)
6497     (dolist (rev '(nil t))
6498       (unless found      ; don't demand the reverse list if we don't need it
6499         (let ((data (gnus-data-find-list
6500                      (gnus-summary-article-number) (gnus-data-list rev))))
6501           (while (and data (not found))
6502             (if (and (< 0 (gnus-data-number (car data)))
6503                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6504                 (setq found (gnus-data-number (car data))))
6505             (setq data (cdr data))))))
6506     (or found 0)))
6507
6508 (defun gnus-summary-reselect-current-group (&optional all rescan)
6509   "Exit and then reselect the current newsgroup.
6510 The prefix argument ALL means to select all articles."
6511   (interactive "P")
6512   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6513     (error "Ephemeral groups can't be reselected"))
6514   (let ((current-subject (gnus-summary-find-for-reselect))
6515         (group gnus-newsgroup-name))
6516     (setq gnus-newsgroup-begin nil)
6517     (gnus-summary-exit nil 'leave-hidden)
6518     ;; We have to adjust the point of group mode buffer because
6519     ;; point was moved to the next unread newsgroup by exiting.
6520     (gnus-summary-jump-to-group group)
6521     (when rescan
6522       (save-excursion
6523         (gnus-group-get-new-news-this-group 1)))
6524     (gnus-group-read-group all t)
6525     (gnus-summary-goto-subject current-subject nil t)))
6526
6527 (defun gnus-summary-rescan-group (&optional all)
6528   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6529   (interactive "P")
6530   (gnus-summary-reselect-current-group all t))
6531
6532 (defun gnus-summary-update-info (&optional non-destructive)
6533   (save-excursion
6534     (let ((group gnus-newsgroup-name))
6535       (when group
6536         (when gnus-newsgroup-kill-headers
6537           (setq gnus-newsgroup-killed
6538                 (gnus-compress-sequence
6539                  (gnus-sorted-union
6540                   (gnus-list-range-intersection
6541                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6542                   gnus-newsgroup-unreads)
6543                  t)))
6544         (unless (listp (cdr gnus-newsgroup-killed))
6545           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6546         (let ((headers gnus-newsgroup-headers))
6547           ;; Set the new ranges of read articles.
6548           (with-current-buffer gnus-group-buffer
6549             (gnus-undo-force-boundary))
6550           (gnus-update-read-articles
6551            group (gnus-sorted-union
6552                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6553           ;; Set the current article marks.
6554           (let ((gnus-newsgroup-scored
6555                  (if (and (not gnus-save-score)
6556                           (not non-destructive))
6557                      nil
6558                    gnus-newsgroup-scored)))
6559             (save-excursion
6560               (gnus-update-marks)))
6561           ;; Do the cross-ref thing.
6562           (when gnus-use-cross-reference
6563             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6564           ;; Do not switch windows but change the buffer to work.
6565           (set-buffer gnus-group-buffer)
6566           (unless (gnus-ephemeral-group-p group)
6567             (gnus-group-update-group group)))))))
6568
6569 (defun gnus-summary-save-newsrc (&optional force)
6570   "Save the current number of read/marked articles in the dribble buffer.
6571 The dribble buffer will then be saved.
6572 If FORCE (the prefix), also save the .newsrc file(s)."
6573   (interactive "P")
6574   (gnus-summary-update-info t)
6575   (if force
6576       (gnus-save-newsrc-file)
6577     (gnus-dribble-save)))
6578
6579 (defun gnus-summary-exit (&optional temporary leave-hidden)
6580   "Exit reading current newsgroup, and then return to group selection mode.
6581 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6582   (interactive)
6583   (gnus-set-global-variables)
6584   (when (gnus-buffer-live-p gnus-article-buffer)
6585     (save-excursion
6586       (set-buffer gnus-article-buffer)
6587       (mm-destroy-parts gnus-article-mime-handles)
6588       ;; Set it to nil for safety reason.
6589       (setq gnus-article-mime-handle-alist nil)
6590       (setq gnus-article-mime-handles nil)))
6591   (gnus-kill-save-kill-buffer)
6592   (gnus-async-halt-prefetch)
6593   (let* ((group gnus-newsgroup-name)
6594          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6595          (gnus-group-is-exiting-p t)
6596          (mode major-mode)
6597          (group-point nil)
6598          (buf (current-buffer)))
6599     (unless quit-config
6600       ;; Do adaptive scoring, and possibly save score files.
6601       (when gnus-newsgroup-adaptive
6602         (gnus-score-adaptive))
6603       (when gnus-use-scoring
6604         (gnus-score-save)))
6605     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6606     ;; If we have several article buffers, we kill them at exit.
6607     (unless gnus-single-article-buffer
6608       (gnus-kill-buffer gnus-original-article-buffer)
6609       (setq gnus-article-current nil))
6610     (when gnus-use-cache
6611       (gnus-cache-possibly-remove-articles)
6612       (gnus-cache-save-buffers))
6613     (gnus-async-prefetch-remove-group group)
6614     (when gnus-suppress-duplicates
6615       (gnus-dup-enter-articles))
6616     (when gnus-use-trees
6617       (gnus-tree-close group))
6618     (when gnus-use-cache
6619       (gnus-cache-write-active))
6620     ;; Remove entries for this group.
6621     (nnmail-purge-split-history (gnus-group-real-name group))
6622     ;; Make all changes in this group permanent.
6623     (unless quit-config
6624       (gnus-run-hooks 'gnus-exit-group-hook)
6625       (gnus-summary-update-info))
6626     (gnus-close-group group)
6627     ;; Make sure where we were, and go to next newsgroup.
6628     (set-buffer gnus-group-buffer)
6629     (unless quit-config
6630       (gnus-group-jump-to-group group))
6631     (gnus-run-hooks 'gnus-summary-exit-hook)
6632     (unless (or quit-config
6633                 ;; If this group has disappeared from the summary
6634                 ;; buffer, don't skip forwards.
6635                 (not (string= group (gnus-group-group-name))))
6636       (gnus-group-next-unread-group 1))
6637     (setq group-point (point))
6638     (if temporary
6639         nil                             ;Nothing to do.
6640       ;; If we have several article buffers, we kill them at exit.
6641       (unless gnus-single-article-buffer
6642         (gnus-kill-buffer gnus-article-buffer)
6643         (gnus-kill-buffer gnus-original-article-buffer)
6644         (setq gnus-article-current nil))
6645       (set-buffer buf)
6646       (if (not gnus-kill-summary-on-exit)
6647           (progn
6648             (gnus-deaden-summary)
6649             (setq mode nil))
6650         ;; We set all buffer-local variables to nil.  It is unclear why
6651         ;; this is needed, but if we don't, buffer-local variables are
6652         ;; not garbage-collected, it seems.  This would the lead to en
6653         ;; ever-growing Emacs.
6654         (gnus-summary-clear-local-variables)
6655         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6656           (gnus-summary-clear-local-variables))
6657         (when (get-buffer gnus-article-buffer)
6658           (bury-buffer gnus-article-buffer))
6659         ;; We clear the global counterparts of the buffer-local
6660         ;; variables as well, just to be on the safe side.
6661         (set-buffer gnus-group-buffer)
6662         (gnus-summary-clear-local-variables)
6663         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6664           (gnus-summary-clear-local-variables))
6665         ;; Return to group mode buffer.
6666         (when (eq mode 'gnus-summary-mode)
6667           (gnus-kill-buffer buf)))
6668       (setq gnus-current-select-method gnus-select-method)
6669       (if leave-hidden
6670           (set-buffer gnus-group-buffer)
6671         (pop-to-buffer gnus-group-buffer))
6672       (if (not quit-config)
6673           (progn
6674             (goto-char group-point)
6675             (unless leave-hidden
6676               (gnus-configure-windows 'group 'force)))
6677         (gnus-handle-ephemeral-exit quit-config))
6678       ;; Clear the current group name.
6679       (unless quit-config
6680         (setq gnus-newsgroup-name nil)))))
6681
6682 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6683 (defun gnus-summary-exit-no-update (&optional no-questions)
6684   "Quit reading current newsgroup without updating read article info."
6685   (interactive)
6686   (let* ((group gnus-newsgroup-name)
6687          (gnus-group-is-exiting-p t)
6688          (gnus-group-is-exiting-without-update-p t)
6689          (quit-config (gnus-group-quit-config group)))
6690     (when (or no-questions
6691               gnus-expert-user
6692               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6693       (gnus-async-halt-prefetch)
6694       (run-hooks 'gnus-summary-prepare-exit-hook)
6695       (when (gnus-buffer-live-p gnus-article-buffer)
6696         (save-excursion
6697           (set-buffer gnus-article-buffer)
6698           (mm-destroy-parts gnus-article-mime-handles)
6699           ;; Set it to nil for safety reason.
6700           (setq gnus-article-mime-handle-alist nil)
6701           (setq gnus-article-mime-handles nil)))
6702       ;; If we have several article buffers, we kill them at exit.
6703       (unless gnus-single-article-buffer
6704         (gnus-kill-buffer gnus-article-buffer)
6705         (gnus-kill-buffer gnus-original-article-buffer)
6706         (setq gnus-article-current nil))
6707       (if (not gnus-kill-summary-on-exit)
6708           (gnus-deaden-summary)
6709         (gnus-close-group group)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (set-buffer gnus-group-buffer)
6714         (gnus-summary-clear-local-variables)
6715         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6716           (gnus-summary-clear-local-variables))
6717         (gnus-kill-buffer gnus-summary-buffer))
6718       (unless gnus-single-article-buffer
6719         (setq gnus-article-current nil))
6720       (when gnus-use-trees
6721         (gnus-tree-close group))
6722       (gnus-async-prefetch-remove-group group)
6723       (when (get-buffer gnus-article-buffer)
6724         (bury-buffer gnus-article-buffer))
6725       ;; Return to the group buffer.
6726       (gnus-configure-windows 'group 'force)
6727       ;; Clear the current group name.
6728       (setq gnus-newsgroup-name nil)
6729       (unless (gnus-ephemeral-group-p group)
6730         (gnus-group-update-group group))
6731       (when (equal (gnus-group-group-name) group)
6732         (gnus-group-next-unread-group 1))
6733       (when quit-config
6734         (gnus-handle-ephemeral-exit quit-config)))))
6735
6736 (defun gnus-handle-ephemeral-exit (quit-config)
6737   "Handle movement when leaving an ephemeral group.
6738 The state which existed when entering the ephemeral is reset."
6739   (if (not (buffer-name (car quit-config)))
6740       (gnus-configure-windows 'group 'force)
6741     (set-buffer (car quit-config))
6742     (cond ((eq major-mode 'gnus-summary-mode)
6743            (gnus-set-global-variables))
6744           ((eq major-mode 'gnus-article-mode)
6745            (save-excursion
6746              ;; The `gnus-summary-buffer' variable may point
6747              ;; to the old summary buffer when using a single
6748              ;; article buffer.
6749              (unless (gnus-buffer-live-p gnus-summary-buffer)
6750                (set-buffer gnus-group-buffer))
6751              (set-buffer gnus-summary-buffer)
6752              (gnus-set-global-variables))))
6753     (if (or (eq (cdr quit-config) 'article)
6754             (eq (cdr quit-config) 'pick))
6755         (progn
6756           ;; The current article may be from the ephemeral group
6757           ;; thus it is best that we reload this article
6758           ;;
6759           ;; If we're exiting from a large digest, this can be
6760           ;; extremely slow.  So, it's better not to reload it. -- jh.
6761           ;;(gnus-summary-show-article)
6762           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6763               (gnus-configure-windows 'pick 'force)
6764             (gnus-configure-windows (cdr quit-config) 'force)))
6765       (gnus-configure-windows (cdr quit-config) 'force))
6766     (when (eq major-mode 'gnus-summary-mode)
6767       (gnus-summary-next-subject 1 nil t)
6768       (gnus-summary-recenter)
6769       (gnus-summary-position-point))))
6770
6771 ;;; Dead summaries.
6772
6773 (defvar gnus-dead-summary-mode-map nil)
6774
6775 (unless gnus-dead-summary-mode-map
6776   (setq gnus-dead-summary-mode-map (make-keymap))
6777   (suppress-keymap gnus-dead-summary-mode-map)
6778   (substitute-key-definition
6779    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6780   (dolist (key '("\C-d" "\r" "\177" [delete]))
6781     (define-key gnus-dead-summary-mode-map
6782       key 'gnus-summary-wake-up-the-dead))
6783   (dolist (key '("q" "Q"))
6784     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6785
6786 (defvar gnus-dead-summary-mode nil
6787   "Minor mode for Gnus summary buffers.")
6788
6789 (defun gnus-dead-summary-mode (&optional arg)
6790   "Minor mode for Gnus summary buffers."
6791   (interactive "P")
6792   (when (eq major-mode 'gnus-summary-mode)
6793     (make-local-variable 'gnus-dead-summary-mode)
6794     (setq gnus-dead-summary-mode
6795           (if (null arg) (not gnus-dead-summary-mode)
6796             (> (prefix-numeric-value arg) 0)))
6797     (when gnus-dead-summary-mode
6798       (add-minor-mode
6799        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6800
6801 (defun gnus-deaden-summary ()
6802   "Make the current summary buffer into a dead summary buffer."
6803   ;; Kill any previous dead summary buffer.
6804   (when (and gnus-dead-summary
6805              (buffer-name gnus-dead-summary))
6806     (with-current-buffer gnus-dead-summary
6807       (when gnus-dead-summary-mode
6808         (kill-buffer (current-buffer)))))
6809   ;; Make this the current dead summary.
6810   (setq gnus-dead-summary (current-buffer))
6811   (gnus-dead-summary-mode 1)
6812   (let ((name (buffer-name)))
6813     (when (string-match "Summary" name)
6814       (rename-buffer
6815        (concat (substring name 0 (match-beginning 0)) "Dead "
6816                (substring name (match-beginning 0)))
6817        t)
6818       (bury-buffer))))
6819
6820 (defun gnus-kill-or-deaden-summary (buffer)
6821   "Kill or deaden the summary BUFFER."
6822   (save-excursion
6823     (when (and (buffer-name buffer)
6824                (not gnus-single-article-buffer))
6825       (with-current-buffer buffer
6826         (gnus-kill-buffer gnus-article-buffer)
6827         (gnus-kill-buffer gnus-original-article-buffer)))
6828     (cond
6829      ;; Kill the buffer.
6830      (gnus-kill-summary-on-exit
6831       (when (and gnus-use-trees
6832                  (gnus-buffer-exists-p buffer))
6833         (save-excursion
6834           (set-buffer buffer)
6835           (gnus-tree-close gnus-newsgroup-name)))
6836       (gnus-kill-buffer buffer))
6837      ;; Deaden the buffer.
6838      ((gnus-buffer-exists-p buffer)
6839       (save-excursion
6840         (set-buffer buffer)
6841         (gnus-deaden-summary))))))
6842
6843 (defun gnus-summary-wake-up-the-dead (&rest args)
6844   "Wake up the dead summary buffer."
6845   (interactive)
6846   (gnus-dead-summary-mode -1)
6847   (let ((name (buffer-name)))
6848     (when (string-match "Dead " name)
6849       (rename-buffer
6850        (concat (substring name 0 (match-beginning 0))
6851                (substring name (match-end 0)))
6852        t)))
6853   (gnus-message 3 "This dead summary is now alive again"))
6854
6855 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6856 (defun gnus-summary-fetch-faq (&optional faq-dir)
6857   "Fetch the FAQ for the current group.
6858 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6859 in."
6860   (interactive
6861    (list
6862     (when current-prefix-arg
6863       (completing-read
6864        "FAQ dir: " (and (listp gnus-group-faq-directory)
6865                         (mapcar 'list
6866                                 gnus-group-faq-directory))))))
6867   (let (gnus-faq-buffer)
6868     (when (setq gnus-faq-buffer
6869                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6870       (gnus-configure-windows 'summary-faq))))
6871
6872 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6873 (defun gnus-summary-describe-group (&optional force)
6874   "Describe the current newsgroup."
6875   (interactive "P")
6876   (gnus-group-describe-group force gnus-newsgroup-name))
6877
6878 (defun gnus-summary-describe-briefly ()
6879   "Describe summary mode commands briefly."
6880   (interactive)
6881   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
6882
6883 ;; Walking around group mode buffer from summary mode.
6884
6885 (defun gnus-summary-next-group (&optional no-article target-group backward)
6886   "Exit current newsgroup and then select next unread newsgroup.
6887 If prefix argument NO-ARTICLE is non-nil, no article is selected
6888 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6889 previous group instead."
6890   (interactive "P")
6891   ;; Stop pre-fetching.
6892   (gnus-async-halt-prefetch)
6893   (let ((current-group gnus-newsgroup-name)
6894         (current-buffer (current-buffer))
6895         entered)
6896     ;; First we semi-exit this group to update Xrefs and all variables.
6897     ;; We can't do a real exit, because the window conf must remain
6898     ;; the same in case the user is prompted for info, and we don't
6899     ;; want the window conf to change before that...
6900     (gnus-summary-exit t)
6901     (while (not entered)
6902       ;; Then we find what group we are supposed to enter.
6903       (set-buffer gnus-group-buffer)
6904       (gnus-group-jump-to-group current-group)
6905       (setq target-group
6906             (or target-group
6907                 (if (eq gnus-keep-same-level 'best)
6908                     (gnus-summary-best-group gnus-newsgroup-name)
6909                   (gnus-summary-search-group backward gnus-keep-same-level))))
6910       (if (not target-group)
6911           ;; There are no further groups, so we return to the group
6912           ;; buffer.
6913           (progn
6914             (gnus-message 5 "Returning to the group buffer")
6915             (setq entered t)
6916             (when (gnus-buffer-live-p current-buffer)
6917               (set-buffer current-buffer)
6918               (gnus-summary-exit))
6919             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6920         ;; We try to enter the target group.
6921         (gnus-group-jump-to-group target-group)
6922         (let ((unreads (gnus-group-group-unread)))
6923           (if (and (or (eq t unreads)
6924                        (and unreads (not (zerop unreads))))
6925                    (gnus-summary-read-group
6926                     target-group nil no-article
6927                     (and (buffer-name current-buffer) current-buffer)
6928                     nil backward))
6929               (setq entered t)
6930             (setq current-group target-group
6931                   target-group nil)))))))
6932
6933 (defun gnus-summary-prev-group (&optional no-article)
6934   "Exit current newsgroup and then select previous unread newsgroup.
6935 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6936   (interactive "P")
6937   (gnus-summary-next-group no-article nil t))
6938
6939 ;; Walking around summary lines.
6940
6941 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6942   "Go to the first subject satisfying any non-nil constraint.
6943 If UNREAD is non-nil, the article should be unread.
6944 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6945 If UNSEEN is non-nil, the article should be unseen.
6946 Returns the article selected or nil if there are no matching articles."
6947   (interactive "P")
6948   (cond
6949    ;; Empty summary.
6950    ((null gnus-newsgroup-data)
6951     (gnus-message 3 "No articles in the group")
6952     nil)
6953    ;; Pick the first article.
6954    ((not (or unread undownloaded unseen))
6955     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6956     (gnus-data-number (car gnus-newsgroup-data)))
6957    ;; Find the first unread article.
6958    (t
6959     (let ((data gnus-newsgroup-data))
6960       (while (and data
6961                   (let ((num (gnus-data-number (car data))))
6962                     (or (memq num gnus-newsgroup-unfetched)
6963                         (not (or (and unread
6964                                       (memq num gnus-newsgroup-unreads))
6965                                  (and undownloaded
6966                                       (memq num gnus-newsgroup-undownloaded))
6967                                  (and unseen
6968                                       (memq num gnus-newsgroup-unseen)))))))
6969         (setq data (cdr data)))
6970       (prog1
6971           (if data
6972               (progn
6973                 (goto-char (gnus-data-pos (car data)))
6974                 (gnus-data-number (car data)))
6975             (gnus-message 3 "No more%s articles"
6976                           (let* ((r (when unread " unread"))
6977                                  (d (when undownloaded " undownloaded"))
6978                                  (s (when unseen " unseen"))
6979                                  (l (delq nil (list r d s))))
6980                             (cond ((= 3 (length l))
6981                                    (concat r "," d ", or" s))
6982                                   ((= 2 (length l))
6983                                    (concat (car l) ", or" (cadr l)))
6984                                   ((= 1 (length l))
6985                                    (car l))
6986                                   (t
6987                                    ""))))
6988             nil
6989             )
6990         (gnus-summary-position-point))))))
6991
6992 (defun gnus-summary-next-subject (n &optional unread dont-display)
6993   "Go to next N'th summary line.
6994 If N is negative, go to the previous N'th subject line.
6995 If UNREAD is non-nil, only unread articles are selected.
6996 The difference between N and the actual number of steps taken is
6997 returned."
6998   (interactive "p")
6999   (let ((backward (< n 0))
7000         (n (abs n)))
7001     (while (and (> n 0)
7002                 (if backward
7003                     (gnus-summary-find-prev unread)
7004                   (gnus-summary-find-next unread)))
7005       (unless (zerop (setq n (1- n)))
7006         (gnus-summary-show-thread)))
7007     (when (/= 0 n)
7008       (gnus-message 7 "No more%s articles"
7009                     (if unread " unread" "")))
7010     (unless dont-display
7011       (gnus-summary-recenter)
7012       (gnus-summary-position-point))
7013     n))
7014
7015 (defun gnus-summary-next-unread-subject (n)
7016   "Go to next N'th unread summary line."
7017   (interactive "p")
7018   (gnus-summary-next-subject n t))
7019
7020 (defun gnus-summary-prev-subject (n &optional unread)
7021   "Go to previous N'th summary line.
7022 If optional argument UNREAD is non-nil, only unread article is selected."
7023   (interactive "p")
7024   (gnus-summary-next-subject (- n) unread))
7025
7026 (defun gnus-summary-prev-unread-subject (n)
7027   "Go to previous N'th unread summary line."
7028   (interactive "p")
7029   (gnus-summary-next-subject (- n) t))
7030
7031 (defun gnus-summary-goto-subjects (articles)
7032   "Insert the subject header for ARTICLES in the current buffer."
7033   (save-excursion
7034     (dolist (article articles)
7035       (gnus-summary-goto-subject article t)))
7036   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7037   (gnus-summary-position-point))
7038  
7039 (defun gnus-summary-goto-subject (article &optional force silent)
7040   "Go the subject line of ARTICLE.
7041 If FORCE, also allow jumping to articles not currently shown."
7042   (interactive "nArticle number: ")
7043   (unless (numberp article)
7044     (error "Article %s is not a number" article))
7045   (let ((b (point))
7046         (data (gnus-data-find article)))
7047     ;; We read in the article if we have to.
7048     (and (not data)
7049          force
7050          (gnus-summary-insert-subject
7051           article
7052           (if (or (numberp force) (vectorp force)) force)
7053           t)
7054          (setq data (gnus-data-find article)))
7055     (goto-char b)
7056     (if (not data)
7057         (progn
7058           (unless silent
7059             (gnus-message 3 "Can't find article %d" article))
7060           nil)
7061       (let ((pt (gnus-data-pos data)))
7062         (goto-char pt)
7063         (gnus-summary-set-article-display-arrow pt))
7064       (gnus-summary-position-point)
7065       article)))
7066
7067 ;; Walking around summary lines with displaying articles.
7068
7069 (defun gnus-summary-expand-window (&optional arg)
7070   "Make the summary buffer take up the entire Emacs frame.
7071 Given a prefix, will force an `article' buffer configuration."
7072   (interactive "P")
7073   (if arg
7074       (gnus-configure-windows 'article 'force)
7075     (gnus-configure-windows 'summary 'force)))
7076
7077 (defun gnus-summary-display-article (article &optional all-header)
7078   "Display ARTICLE in article buffer."
7079   (when (gnus-buffer-live-p gnus-article-buffer)
7080     (with-current-buffer gnus-article-buffer
7081       (mm-enable-multibyte)))
7082   (gnus-set-global-variables)
7083   (when (gnus-buffer-live-p gnus-article-buffer)
7084     (with-current-buffer gnus-article-buffer
7085       (setq gnus-article-charset gnus-newsgroup-charset)
7086       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7087       (mm-enable-multibyte)))
7088   (if (null article)
7089       nil
7090     (prog1
7091         (if gnus-summary-display-article-function
7092             (funcall gnus-summary-display-article-function article all-header)
7093           (gnus-article-prepare article all-header))
7094       (gnus-run-hooks 'gnus-select-article-hook)
7095       (when (and gnus-current-article
7096                  (not (zerop gnus-current-article)))
7097         (gnus-summary-goto-subject gnus-current-article))
7098       (gnus-summary-recenter)
7099       (when (and gnus-use-trees gnus-show-threads)
7100         (gnus-possibly-generate-tree article)
7101         (gnus-highlight-selected-tree article))
7102       ;; Successfully display article.
7103       (gnus-article-set-window-start
7104        (cdr (assq article gnus-newsgroup-bookmarks))))))
7105
7106 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7107   "Select the current article.
7108 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7109 non-nil, the article will be re-fetched even if it already present in
7110 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7111 be displayed."
7112   ;; Make sure we are in the summary buffer to work around bbdb bug.
7113   (unless (eq major-mode 'gnus-summary-mode)
7114     (set-buffer gnus-summary-buffer))
7115   (let ((article (or article (gnus-summary-article-number)))
7116         (all-headers (not (not all-headers))) ;Must be t or nil.
7117         gnus-summary-display-article-function)
7118     (and (not pseudo)
7119          (gnus-summary-article-pseudo-p article)
7120          (error "This is a pseudo-article"))
7121     (save-excursion
7122       (set-buffer gnus-summary-buffer)
7123       (if (or (and gnus-single-article-buffer
7124                    (or (null gnus-current-article)
7125                        (null gnus-article-current)
7126                        (null (get-buffer gnus-article-buffer))
7127                        (not (eq article (cdr gnus-article-current)))
7128                        (not (equal (car gnus-article-current)
7129                                    gnus-newsgroup-name))))
7130               (and (not gnus-single-article-buffer)
7131                    (or (null gnus-current-article)
7132                        (not (eq gnus-current-article article))))
7133               force)
7134           ;; The requested article is different from the current article.
7135           (progn
7136             (gnus-summary-display-article article all-headers)
7137             (when (gnus-buffer-live-p gnus-article-buffer)
7138               (with-current-buffer gnus-article-buffer
7139                 (if (not gnus-article-decoded-p) ;; a local variable
7140                     (mm-disable-multibyte))))
7141             (gnus-article-set-window-start
7142              (cdr (assq article gnus-newsgroup-bookmarks)))
7143             article)
7144         'old))))
7145
7146 (defun gnus-summary-force-verify-and-decrypt ()
7147   "Display buttons for signed/encrypted parts and verify/decrypt them."
7148   (interactive)
7149   (let ((mm-verify-option 'known)
7150         (mm-decrypt-option 'known)
7151         (gnus-article-emulate-mime t)
7152         (gnus-buttonized-mime-types (append (list "multipart/signed"
7153                                                   "multipart/encrypted")
7154                                             gnus-buttonized-mime-types)))
7155     (gnus-summary-select-article nil 'force)))
7156
7157 (defun gnus-summary-set-current-mark (&optional current-mark)
7158   "Obsolete function."
7159   nil)
7160
7161 (defun gnus-summary-next-article (&optional unread subject backward push)
7162   "Select the next article.
7163 If UNREAD, only unread articles are selected.
7164 If SUBJECT, only articles with SUBJECT are selected.
7165 If BACKWARD, the previous article is selected instead of the next."
7166   (interactive "P")
7167   (cond
7168    ;; Is there such an article?
7169    ((and (gnus-summary-search-forward unread subject backward)
7170          (or (gnus-summary-display-article (gnus-summary-article-number))
7171              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7172     (gnus-summary-position-point))
7173    ;; If not, we try the first unread, if that is wanted.
7174    ((and subject
7175          gnus-auto-select-same
7176          (gnus-summary-first-unread-article))
7177     (gnus-summary-position-point)
7178     (gnus-message 6 "Wrapped"))
7179    ;; Try to get next/previous article not displayed in this group.
7180    ((and gnus-auto-extend-newsgroup
7181          (not unread) (not subject))
7182     (gnus-summary-goto-article
7183      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7184      nil (count-lines (point-min) (point))))
7185    ;; Go to next/previous group.
7186    (t
7187     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7188       (gnus-summary-jump-to-group gnus-newsgroup-name))
7189     (let ((cmd last-command-char)
7190           (point
7191            (with-current-buffer gnus-group-buffer
7192              (point)))
7193           (group
7194            (if (eq gnus-keep-same-level 'best)
7195                (gnus-summary-best-group gnus-newsgroup-name)
7196              (gnus-summary-search-group backward gnus-keep-same-level))))
7197       ;; For some reason, the group window gets selected.  We change
7198       ;; it back.
7199       (select-window (get-buffer-window (current-buffer)))
7200       ;; Select next unread newsgroup automagically.
7201       (cond
7202        ((or (not gnus-auto-select-next)
7203             (not cmd))
7204         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7205        ((or (eq gnus-auto-select-next 'quietly)
7206             (and (eq gnus-auto-select-next 'slightly-quietly)
7207                  push)
7208             (and (eq gnus-auto-select-next 'almost-quietly)
7209                  (gnus-summary-last-article-p)))
7210         ;; Select quietly.
7211         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7212             (gnus-summary-exit)
7213           (gnus-message 7 "No more%s articles (%s)..."
7214                         (if unread " unread" "")
7215                         (if group (concat "selecting " group)
7216                           "exiting"))
7217           (gnus-summary-next-group nil group backward)))
7218        (t
7219         (when (gnus-key-press-event-p last-input-event)
7220           (gnus-summary-walk-group-buffer
7221            gnus-newsgroup-name cmd unread backward point))))))))
7222
7223 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7224   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7225                       (?\C-p (gnus-group-prev-unread-group 1))))
7226         (cursor-in-echo-area t)
7227         keve key group ended prompt)
7228     (save-excursion
7229       (set-buffer gnus-group-buffer)
7230       (goto-char start)
7231       (setq group
7232             (if (eq gnus-keep-same-level 'best)
7233                 (gnus-summary-best-group gnus-newsgroup-name)
7234               (gnus-summary-search-group backward gnus-keep-same-level))))
7235     (while (not ended)
7236       (setq prompt
7237             (format
7238              "No more%s articles%s " (if unread " unread" "")
7239              (if (and group
7240                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7241                  (format " (Type %s for %s [%s])"
7242                          (single-key-description cmd) group
7243                          (gnus-group-unread group))
7244                (format " (Type %s to exit %s)"
7245                        (single-key-description cmd)
7246                        gnus-newsgroup-name))))
7247       ;; Confirm auto selection.
7248       (setq key (car (setq keve (gnus-read-event-char prompt)))
7249             ended t)
7250       (cond
7251        ((assq key keystrokes)
7252         (let ((obuf (current-buffer)))
7253           (switch-to-buffer gnus-group-buffer)
7254           (when group
7255             (gnus-group-jump-to-group group))
7256           (eval (cadr (assq key keystrokes)))
7257           (setq group (gnus-group-group-name))
7258           (switch-to-buffer obuf))
7259         (setq ended nil))
7260        ((equal key cmd)
7261         (if (or (not group)
7262                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7263             (gnus-summary-exit)
7264           (gnus-summary-next-group nil group backward)))
7265        (t
7266         (push (cdr keve) unread-command-events))))))
7267
7268 (defun gnus-summary-next-unread-article ()
7269   "Select unread article after current one."
7270   (interactive)
7271   (gnus-summary-next-article
7272    (or (not (eq gnus-summary-goto-unread 'never))
7273        (gnus-summary-last-article-p (gnus-summary-article-number)))
7274    (and gnus-auto-select-same
7275         (gnus-summary-article-subject))))
7276
7277 (defun gnus-summary-prev-article (&optional unread subject)
7278   "Select the article after the current one.
7279 If UNREAD is non-nil, only unread articles are selected."
7280   (interactive "P")
7281   (gnus-summary-next-article unread subject t))
7282
7283 (defun gnus-summary-prev-unread-article ()
7284   "Select unread article before current one."
7285   (interactive)
7286   (gnus-summary-prev-article
7287    (or (not (eq gnus-summary-goto-unread 'never))
7288        (gnus-summary-first-article-p (gnus-summary-article-number)))
7289    (and gnus-auto-select-same
7290         (gnus-summary-article-subject))))
7291
7292 (defun gnus-summary-next-page (&optional lines circular stop)
7293   "Show next page of the selected article.
7294 If at the end of the current article, select the next article.
7295 LINES says how many lines should be scrolled up.
7296
7297 If CIRCULAR is non-nil, go to the start of the article instead of
7298 selecting the next article when reaching the end of the current
7299 article.
7300
7301 If STOP is non-nil, just stop when reaching the end of the message.
7302
7303 Also see the variable `gnus-article-skip-boring'."
7304   (interactive "P")
7305   (setq gnus-summary-buffer (current-buffer))
7306   (gnus-set-global-variables)
7307   (let ((article (gnus-summary-article-number))
7308         (article-window (get-buffer-window gnus-article-buffer t))
7309         endp)
7310     ;; If the buffer is empty, we have no article.
7311     (unless article
7312       (error "No article to select"))
7313     (gnus-configure-windows 'article)
7314     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7315         (if (and (eq gnus-summary-goto-unread 'never)
7316                  (not (gnus-summary-last-article-p article)))
7317             (gnus-summary-next-article)
7318           (gnus-summary-next-unread-article))
7319       (if (or (null gnus-current-article)
7320               (null gnus-article-current)
7321               (/= article (cdr gnus-article-current))
7322               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7323           ;; Selected subject is different from current article's.
7324           (gnus-summary-display-article article)
7325         (when article-window
7326           (gnus-eval-in-buffer-window gnus-article-buffer
7327             (setq endp (or (gnus-article-next-page lines)
7328                            (gnus-article-only-boring-p))))
7329           (when endp
7330             (cond (stop
7331                    (gnus-message 3 "End of message"))
7332                   (circular
7333                    (gnus-summary-beginning-of-article))
7334                   (lines
7335                    (gnus-message 3 "End of message"))
7336                   ((null lines)
7337                    (if (and (eq gnus-summary-goto-unread 'never)
7338                             (not (gnus-summary-last-article-p article)))
7339                        (gnus-summary-next-article)
7340                      (gnus-summary-next-unread-article))))))))
7341     (gnus-summary-recenter)
7342     (gnus-summary-position-point)))
7343
7344 (defun gnus-summary-prev-page (&optional lines move)
7345   "Show previous page of selected article.
7346 Argument LINES specifies lines to be scrolled down.
7347 If MOVE, move to the previous unread article if point is at
7348 the beginning of the buffer."
7349   (interactive "P")
7350   (let ((article (gnus-summary-article-number))
7351         (article-window (get-buffer-window gnus-article-buffer t))
7352         endp)
7353     (gnus-configure-windows 'article)
7354     (if (or (null gnus-current-article)
7355             (null gnus-article-current)
7356             (/= article (cdr gnus-article-current))
7357             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7358         ;; Selected subject is different from current article's.
7359         (gnus-summary-display-article article)
7360       (gnus-summary-recenter)
7361       (when article-window
7362         (gnus-eval-in-buffer-window gnus-article-buffer
7363           (setq endp (gnus-article-prev-page lines)))
7364         (when (and move endp)
7365           (cond (lines
7366                  (gnus-message 3 "Beginning of message"))
7367                 ((null lines)
7368                  (if (and (eq gnus-summary-goto-unread 'never)
7369                           (not (gnus-summary-first-article-p article)))
7370                      (gnus-summary-prev-article)
7371                    (gnus-summary-prev-unread-article))))))))
7372   (gnus-summary-position-point))
7373
7374 (defun gnus-summary-prev-page-or-article (&optional lines)
7375   "Show previous page of selected article.
7376 Argument LINES specifies lines to be scrolled down.
7377 If at the beginning of the article, go to the next article."
7378   (interactive "P")
7379   (gnus-summary-prev-page lines t))
7380
7381 (defun gnus-summary-scroll-up (lines)
7382   "Scroll up (or down) one line current article.
7383 Argument LINES specifies lines to be scrolled up (or down if negative)."
7384   (interactive "p")
7385   (gnus-configure-windows 'article)
7386   (gnus-summary-show-thread)
7387   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7388     (gnus-eval-in-buffer-window gnus-article-buffer
7389       (cond ((> lines 0)
7390              (when (gnus-article-next-page lines)
7391                (gnus-message 3 "End of message")))
7392             ((< lines 0)
7393              (gnus-article-prev-page (- lines))))))
7394   (gnus-summary-recenter)
7395   (gnus-summary-position-point))
7396
7397 (defun gnus-summary-scroll-down (lines)
7398   "Scroll down (or up) one line current article.
7399 Argument LINES specifies lines to be scrolled down (or up if negative)."
7400   (interactive "p")
7401   (gnus-summary-scroll-up (- lines)))
7402
7403 (defun gnus-summary-next-same-subject ()
7404   "Select next article which has the same subject as current one."
7405   (interactive)
7406   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7407
7408 (defun gnus-summary-prev-same-subject ()
7409   "Select previous article which has the same subject as current one."
7410   (interactive)
7411   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7412
7413 (defun gnus-summary-next-unread-same-subject ()
7414   "Select next unread article which has the same subject as current one."
7415   (interactive)
7416   (gnus-summary-next-article t (gnus-summary-article-subject)))
7417
7418 (defun gnus-summary-prev-unread-same-subject ()
7419   "Select previous unread article which has the same subject as current one."
7420   (interactive)
7421   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7422
7423 (defun gnus-summary-first-unread-article ()
7424   "Select the first unread article.
7425 Return nil if there are no unread articles."
7426   (interactive)
7427   (prog1
7428       (when (gnus-summary-first-subject t)
7429         (gnus-summary-show-thread)
7430         (gnus-summary-first-subject t)
7431         (gnus-summary-display-article (gnus-summary-article-number)))
7432     (gnus-summary-position-point)))
7433
7434 (defun gnus-summary-first-unread-subject ()
7435   "Place the point on the subject line of the first unread article.
7436 Return nil if there are no unread articles."
7437   (interactive)
7438   (prog1
7439       (when (gnus-summary-first-subject t)
7440         (gnus-summary-show-thread)
7441         (gnus-summary-first-subject t))
7442     (gnus-summary-position-point)))
7443
7444 (defun gnus-summary-first-unseen-subject ()
7445   "Place the point on the subject line of the first unseen article.
7446 Return nil if there are no unseen articles."
7447   (interactive)
7448   (prog1
7449       (when (gnus-summary-first-subject nil nil t)
7450         (gnus-summary-show-thread)
7451         (gnus-summary-first-subject nil nil t))
7452     (gnus-summary-position-point)))
7453
7454 (defun gnus-summary-first-unseen-or-unread-subject ()
7455   "Place the point on the subject line of the first unseen article or,
7456 if all article have been seen, on the subject line of the first unread
7457 article."
7458   (interactive)
7459   (prog1
7460       (unless (when (gnus-summary-first-subject nil nil t)
7461                 (gnus-summary-show-thread)
7462                 (gnus-summary-first-subject nil nil t))
7463         (when (gnus-summary-first-subject t)
7464           (gnus-summary-show-thread)
7465           (gnus-summary-first-subject t)))
7466     (gnus-summary-position-point)))
7467
7468 (defun gnus-summary-first-article ()
7469   "Select the first article.
7470 Return nil if there are no articles."
7471   (interactive)
7472   (prog1
7473       (when (gnus-summary-first-subject)
7474         (gnus-summary-show-thread)
7475         (gnus-summary-first-subject)
7476         (gnus-summary-display-article (gnus-summary-article-number)))
7477     (gnus-summary-position-point)))
7478
7479 (defun gnus-summary-best-unread-article (&optional arg)
7480   "Select the unread article with the highest score.
7481 If given a prefix argument, select the next unread article that has a
7482 score higher than the default score."
7483   (interactive "P")
7484   (let ((article (if arg
7485                      (gnus-summary-better-unread-subject)
7486                    (gnus-summary-best-unread-subject))))
7487     (if article
7488         (gnus-summary-goto-article article)
7489       (error "No unread articles"))))
7490
7491 (defun gnus-summary-best-unread-subject ()
7492   "Select the unread subject with the highest score."
7493   (interactive)
7494   (let ((best -1000000)
7495         (data gnus-newsgroup-data)
7496         article score)
7497     (while data
7498       (and (gnus-data-unread-p (car data))
7499            (> (setq score
7500                     (gnus-summary-article-score (gnus-data-number (car data))))
7501               best)
7502            (setq best score
7503                  article (gnus-data-number (car data))))
7504       (setq data (cdr data)))
7505     (when article
7506       (gnus-summary-goto-subject article))
7507     (gnus-summary-position-point)
7508     article))
7509
7510 (defun gnus-summary-better-unread-subject ()
7511   "Select the first unread subject that has a score over the default score."
7512   (interactive)
7513   (let ((data gnus-newsgroup-data)
7514         article score)
7515     (while (and (setq article (gnus-data-number (car data)))
7516                 (or (gnus-data-read-p (car data))
7517                     (not (> (gnus-summary-article-score article)
7518                             gnus-summary-default-score))))
7519       (setq data (cdr data)))
7520     (when article
7521       (gnus-summary-goto-subject article))
7522     (gnus-summary-position-point)
7523     article))
7524
7525 (defun gnus-summary-last-subject ()
7526   "Go to the last displayed subject line in the group."
7527   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7528     (when article
7529       (gnus-summary-goto-subject article))))
7530
7531 (defun gnus-summary-goto-article (article &optional all-headers force)
7532   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7533 If ALL-HEADERS is non-nil, no header lines are hidden.
7534 If FORCE, go to the article even if it isn't displayed.  If FORCE
7535 is a number, it is the line the article is to be displayed on."
7536   (interactive
7537    (list
7538     (completing-read
7539      "Article number or Message-ID: "
7540      (mapcar (lambda (number) (list (int-to-string number)))
7541              gnus-newsgroup-limit))
7542     current-prefix-arg
7543     t))
7544   (prog1
7545       (if (and (stringp article)
7546                (string-match "@\\|%40" article))
7547           (gnus-summary-refer-article article)
7548         (when (stringp article)
7549           (setq article (string-to-number article)))
7550         (if (gnus-summary-goto-subject article force)
7551             (gnus-summary-display-article article all-headers)
7552           (gnus-message 4 "Couldn't go to article %s" article) nil))
7553     (gnus-summary-position-point)))
7554
7555 (defun gnus-summary-goto-last-article ()
7556   "Go to the previously read article."
7557   (interactive)
7558   (prog1
7559       (when gnus-last-article
7560         (gnus-summary-goto-article gnus-last-article nil t))
7561     (gnus-summary-position-point)))
7562
7563 (defun gnus-summary-pop-article (number)
7564   "Pop one article off the history and go to the previous.
7565 NUMBER articles will be popped off."
7566   (interactive "p")
7567   (let (to)
7568     (setq gnus-newsgroup-history
7569           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7570     (if to
7571         (gnus-summary-goto-article (car to) nil t)
7572       (error "Article history empty")))
7573   (gnus-summary-position-point))
7574
7575 ;; Summary commands and functions for limiting the summary buffer.
7576
7577 (defun gnus-summary-limit-to-articles (n)
7578   "Limit the summary buffer to the next N articles.
7579 If not given a prefix, use the process marked articles instead."
7580   (interactive "P")
7581   (prog1
7582       (let ((articles (gnus-summary-work-articles n)))
7583         (setq gnus-newsgroup-processable nil)
7584         (gnus-summary-limit articles))
7585     (gnus-summary-position-point)))
7586
7587 (defun gnus-summary-pop-limit (&optional total)
7588   "Restore the previous limit.
7589 If given a prefix, remove all limits."
7590   (interactive "P")
7591   (when total
7592     (setq gnus-newsgroup-limits
7593           (list (mapcar (lambda (h) (mail-header-number h))
7594                         gnus-newsgroup-headers))))
7595   (unless gnus-newsgroup-limits
7596     (error "No limit to pop"))
7597   (prog1
7598       (gnus-summary-limit nil 'pop)
7599     (gnus-summary-position-point)))
7600
7601 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7602   "Limit the summary buffer to articles that have subjects that match a regexp.
7603 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7604   (interactive
7605    (list (read-string (if current-prefix-arg
7606                           "Exclude subject (regexp): "
7607                         "Limit to subject (regexp): "))
7608          nil current-prefix-arg))
7609   (unless header
7610     (setq header "subject"))
7611   (when (not (equal "" subject))
7612     (prog1
7613         (let ((articles (gnus-summary-find-matching
7614                          (or header "subject") subject 'all nil nil
7615                          not-matching)))
7616           (unless articles
7617             (error "Found no matches for \"%s\"" subject))
7618           (gnus-summary-limit articles))
7619       (gnus-summary-position-point))))
7620
7621 (defun gnus-summary-limit-to-author (from &optional not-matching)
7622   "Limit the summary buffer to articles that have authors that match a regexp.
7623 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7624   (interactive
7625    (list (read-string (if current-prefix-arg
7626                           "Exclude author (regexp): "
7627                         "Limit to author (regexp): "))
7628          current-prefix-arg))
7629   (gnus-summary-limit-to-subject from "from" not-matching))
7630
7631 (defun gnus-summary-limit-to-age (age &optional younger-p)
7632   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7633 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7634 articles that are younger than AGE days."
7635   (interactive
7636    (let ((younger current-prefix-arg)
7637          (days-got nil)
7638          days)
7639      (while (not days-got)
7640        (setq days (if younger
7641                       (read-string "Limit to articles younger than (in days, older when negative): ")
7642                     (read-string
7643                      "Limit to articles older than (in days, younger when negative): ")))
7644        (when (> (length days) 0)
7645          (setq days (read days)))
7646        (if (numberp days)
7647            (progn
7648              (setq days-got t)
7649              (when (< days 0)
7650                (setq younger (not younger))
7651                (setq days (* days -1))))
7652          (message "Please enter a number.")
7653          (sleep-for 1)))
7654      (list days younger)))
7655   (prog1
7656       (let ((data gnus-newsgroup-data)
7657             (cutoff (days-to-time age))
7658             articles d date is-younger)
7659         (while (setq d (pop data))
7660           (when (and (vectorp (gnus-data-header d))
7661                      (setq date (mail-header-date (gnus-data-header d))))
7662             (setq is-younger (time-less-p
7663                               (time-since (condition-case ()
7664                                               (date-to-time date)
7665                                             (error '(0 0))))
7666                               cutoff))
7667             (when (if younger-p
7668                       is-younger
7669                     (not is-younger))
7670               (push (gnus-data-number d) articles))))
7671         (gnus-summary-limit (nreverse articles)))
7672     (gnus-summary-position-point)))
7673
7674 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7675   "Limit the summary buffer to articles that match an 'extra' header."
7676   (interactive
7677    (let ((header
7678           (intern
7679            (gnus-completing-read-with-default
7680             (symbol-name (car gnus-extra-headers))
7681             (if current-prefix-arg
7682                 "Exclude extra header:"
7683               "Limit extra header:")
7684             (mapcar (lambda (x)
7685                       (cons (symbol-name x) x))
7686                     gnus-extra-headers)
7687             nil
7688             t))))
7689      (list header
7690            (read-string (format "%s header %s (regexp): "
7691                                 (if current-prefix-arg "Exclude" "Limit to")
7692                                 header))
7693            current-prefix-arg)))
7694   (when (not (equal "" regexp))
7695     (prog1
7696         (let ((articles (gnus-summary-find-matching
7697                          (cons 'extra header) regexp 'all nil nil
7698                          not-matching)))
7699           (unless articles
7700             (error "Found no matches for \"%s\"" regexp))
7701           (gnus-summary-limit articles))
7702       (gnus-summary-position-point))))
7703
7704 (defun gnus-summary-limit-to-display-predicate ()
7705   "Limit the summary buffer to the predicated in the `display' group parameter."
7706   (interactive)
7707   (unless gnus-newsgroup-display
7708     (error "There is no `display' group parameter"))
7709   (let (articles)
7710     (dolist (number gnus-newsgroup-articles)
7711       (when (funcall gnus-newsgroup-display)
7712         (push number articles)))
7713     (gnus-summary-limit articles))
7714   (gnus-summary-position-point))
7715
7716 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7717 (make-obsolete
7718  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7719
7720 (defun gnus-summary-limit-to-unread (&optional all)
7721   "Limit the summary buffer to articles that are not marked as read.
7722 If ALL is non-nil, limit strictly to unread articles."
7723   (interactive "P")
7724   (if all
7725       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7726     (gnus-summary-limit-to-marks
7727      ;; Concat all the marks that say that an article is read and have
7728      ;; those removed.
7729      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7730            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7731            gnus-low-score-mark gnus-expirable-mark
7732            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7733            gnus-duplicate-mark gnus-souped-mark)
7734      'reverse)))
7735
7736 (defun gnus-summary-limit-to-replied (&optional unreplied)
7737   "Limit the summary buffer to replied articles.
7738 If UNREPLIED (the prefix), limit to unreplied articles."
7739   (interactive "P")
7740   (if unreplied
7741       (gnus-summary-limit
7742        (gnus-set-difference gnus-newsgroup-articles
7743         gnus-newsgroup-replied))
7744     (gnus-summary-limit gnus-newsgroup-replied))
7745   (gnus-summary-position-point))
7746
7747 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7748 (make-obsolete 'gnus-summary-delete-marked-with
7749                'gnus-summary-limit-exclude-marks)
7750
7751 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7752   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7753 If REVERSE, limit the summary buffer to articles that are marked
7754 with MARKS.  MARKS can either be a string of marks or a list of marks.
7755 Returns how many articles were removed."
7756   (interactive "sMarks: ")
7757   (gnus-summary-limit-to-marks marks t))
7758
7759 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7760   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7761 If REVERSE (the prefix), limit the summary buffer to articles that are
7762 not marked with MARKS.  MARKS can either be a string of marks or a
7763 list of marks.
7764 Returns how many articles were removed."
7765   (interactive "sMarks: \nP")
7766   (prog1
7767       (let ((data gnus-newsgroup-data)
7768             (marks (if (listp marks) marks
7769                      (append marks nil))) ; Transform to list.
7770             articles)
7771         (while data
7772           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7773                   (memq (gnus-data-mark (car data)) marks))
7774             (push (gnus-data-number (car data)) articles))
7775           (setq data (cdr data)))
7776         (gnus-summary-limit articles))
7777     (gnus-summary-position-point)))
7778
7779 (defun gnus-summary-limit-to-score (score)
7780   "Limit to articles with score at or above SCORE."
7781   (interactive "NLimit to articles with score of at least: ")
7782   (let ((data gnus-newsgroup-data)
7783         articles)
7784     (while data
7785       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7786                 score)
7787         (push (gnus-data-number (car data)) articles))
7788       (setq data (cdr data)))
7789     (prog1
7790         (gnus-summary-limit articles)
7791       (gnus-summary-position-point))))
7792
7793 (defun gnus-summary-limit-to-unseen ()
7794   "Limit to unseen articles."
7795   (interactive)
7796   (prog1
7797       (gnus-summary-limit gnus-newsgroup-unseen)
7798     (gnus-summary-position-point)))
7799
7800 (defun gnus-summary-limit-include-thread (id)
7801   "Display all the hidden articles that is in the thread with ID in it.
7802 When called interactively, ID is the Message-ID of the current
7803 article."
7804   (interactive (list (mail-header-id (gnus-summary-article-header))))
7805   (let ((articles (gnus-articles-in-thread
7806                    (gnus-id-to-thread (gnus-root-id id)))))
7807     (prog1
7808         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7809       (gnus-summary-limit-include-matching-articles
7810        "subject"
7811        (regexp-quote (gnus-simplify-subject-re
7812                       (mail-header-subject (gnus-id-to-header id)))))
7813       (gnus-summary-position-point))))
7814
7815 (defun gnus-summary-limit-include-matching-articles (header regexp)
7816   "Display all the hidden articles that have HEADERs that match REGEXP."
7817   (interactive (list (read-string "Match on header: ")
7818                      (read-string "Regexp: ")))
7819   (let ((articles (gnus-find-matching-articles header regexp)))
7820     (prog1
7821         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7822       (gnus-summary-position-point))))
7823
7824 (defun gnus-summary-insert-dormant-articles ()
7825   "Insert all the dormant articles for this group into the current buffer."
7826   (interactive)
7827   (let ((gnus-verbose (max 6 gnus-verbose)))
7828     (if (not gnus-newsgroup-dormant)
7829         (gnus-message 3 "No cached articles for this group")
7830       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7831
7832 (defun gnus-summary-limit-include-dormant ()
7833   "Display all the hidden articles that are marked as dormant.
7834 Note that this command only works on a subset of the articles currently
7835 fetched for this group."
7836   (interactive)
7837   (unless gnus-newsgroup-dormant
7838     (error "There are no dormant articles in this group"))
7839   (prog1
7840       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7841     (gnus-summary-position-point)))
7842
7843 (defun gnus-summary-limit-exclude-dormant ()
7844   "Hide all dormant articles."
7845   (interactive)
7846   (prog1
7847       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7848     (gnus-summary-position-point)))
7849
7850 (defun gnus-summary-limit-exclude-childless-dormant ()
7851   "Hide all dormant articles that have no children."
7852   (interactive)
7853   (let ((data (gnus-data-list t))
7854         articles d children)
7855     ;; Find all articles that are either not dormant or have
7856     ;; children.
7857     (while (setq d (pop data))
7858       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7859                 (and (setq children
7860                            (gnus-article-children (gnus-data-number d)))
7861                      (let (found)
7862                        (while children
7863                          (when (memq (car children) articles)
7864                            (setq children nil
7865                                  found t))
7866                          (pop children))
7867                        found)))
7868         (push (gnus-data-number d) articles)))
7869     ;; Do the limiting.
7870     (prog1
7871         (gnus-summary-limit articles)
7872       (gnus-summary-position-point))))
7873
7874 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7875   "Mark all unread excluded articles as read.
7876 If ALL, mark even excluded ticked and dormants as read."
7877   (interactive "P")
7878   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7879   (let ((articles (gnus-sorted-ndifference
7880                    (sort
7881                     (mapcar (lambda (h) (mail-header-number h))
7882                             gnus-newsgroup-headers)
7883                     '<)
7884                    gnus-newsgroup-limit))
7885         article)
7886     (setq gnus-newsgroup-unreads
7887           (gnus-sorted-intersection gnus-newsgroup-unreads
7888                                     gnus-newsgroup-limit))
7889     (if all
7890         (setq gnus-newsgroup-dormant nil
7891               gnus-newsgroup-marked nil
7892               gnus-newsgroup-reads
7893               (nconc
7894                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7895                gnus-newsgroup-reads))
7896       (while (setq article (pop articles))
7897         (unless (or (memq article gnus-newsgroup-dormant)
7898                     (memq article gnus-newsgroup-marked))
7899           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7900
7901 (defun gnus-summary-limit (articles &optional pop)
7902   (if pop
7903       ;; We pop the previous limit off the stack and use that.
7904       (setq articles (car gnus-newsgroup-limits)
7905             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7906     ;; We use the new limit, so we push the old limit on the stack.
7907     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7908   ;; Set the limit.
7909   (setq gnus-newsgroup-limit articles)
7910   (let ((total (length gnus-newsgroup-data))
7911         (data (gnus-data-find-list (gnus-summary-article-number)))
7912         (gnus-summary-mark-below nil)   ; Inhibit this.
7913         found)
7914     ;; This will do all the work of generating the new summary buffer
7915     ;; according to the new limit.
7916     (gnus-summary-prepare)
7917     ;; Hide any threads, possibly.
7918     (gnus-summary-maybe-hide-threads)
7919     ;; Try to return to the article you were at, or one in the
7920     ;; neighborhood.
7921     (when data
7922       ;; We try to find some article after the current one.
7923       (while data
7924         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7925           (setq data nil
7926                 found t))
7927         (setq data (cdr data))))
7928     (unless found
7929       ;; If there is no data, that means that we were after the last
7930       ;; article.  The same goes when we can't find any articles
7931       ;; after the current one.
7932       (goto-char (point-max))
7933       (gnus-summary-find-prev))
7934     (gnus-set-mode-line 'summary)
7935     ;; We return how many articles were removed from the summary
7936     ;; buffer as a result of the new limit.
7937     (- total (length gnus-newsgroup-data))))
7938
7939 (defsubst gnus-invisible-cut-children (threads)
7940   (let ((num 0))
7941     (while threads
7942       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7943         (incf num))
7944       (pop threads))
7945     (< num 2)))
7946
7947 (defsubst gnus-cut-thread (thread)
7948   "Go forwards in the thread until we find an article that we want to display."
7949   (when (or (eq gnus-fetch-old-headers 'some)
7950             (eq gnus-fetch-old-headers 'invisible)
7951             (numberp gnus-fetch-old-headers)
7952             (eq gnus-build-sparse-threads 'some)
7953             (eq gnus-build-sparse-threads 'more))
7954     ;; Deal with old-fetched headers and sparse threads.
7955     (while (and
7956             thread
7957             (or
7958              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7959              (gnus-summary-article-ancient-p
7960               (mail-header-number (car thread))))
7961             (if (or (<= (length (cdr thread)) 1)
7962                     (eq gnus-fetch-old-headers 'invisible))
7963                 (setq gnus-newsgroup-limit
7964                       (delq (mail-header-number (car thread))
7965                             gnus-newsgroup-limit)
7966                       thread (cadr thread))
7967               (when (gnus-invisible-cut-children (cdr thread))
7968                 (let ((th (cdr thread)))
7969                   (while th
7970                     (if (memq (mail-header-number (caar th))
7971                               gnus-newsgroup-limit)
7972                         (setq thread (car th)
7973                               th nil)
7974                       (setq th (cdr th))))))))))
7975   thread)
7976
7977 (defun gnus-cut-threads (threads)
7978   "Cut off all uninteresting articles from the beginning of THREADS."
7979   (when (or (eq gnus-fetch-old-headers 'some)
7980             (eq gnus-fetch-old-headers 'invisible)
7981             (numberp gnus-fetch-old-headers)
7982             (eq gnus-build-sparse-threads 'some)
7983             (eq gnus-build-sparse-threads 'more))
7984     (let ((th threads))
7985       (while th
7986         (setcar th (gnus-cut-thread (car th)))
7987         (setq th (cdr th)))))
7988   ;; Remove nixed out threads.
7989   (delq nil threads))
7990
7991 (defun gnus-summary-initial-limit (&optional show-if-empty)
7992   "Figure out what the initial limit is supposed to be on group entry.
7993 This entails weeding out unwanted dormants, low-scored articles,
7994 fetch-old-headers verbiage, and so on."
7995   ;; Most groups have nothing to remove.
7996   (if (or gnus-inhibit-limiting
7997           (and (null gnus-newsgroup-dormant)
7998                (eq gnus-newsgroup-display 'gnus-not-ignore)
7999                (not (eq gnus-fetch-old-headers 'some))
8000                (not (numberp gnus-fetch-old-headers))
8001                (not (eq gnus-fetch-old-headers 'invisible))
8002                (null gnus-summary-expunge-below)
8003                (not (eq gnus-build-sparse-threads 'some))
8004                (not (eq gnus-build-sparse-threads 'more))
8005                (null gnus-thread-expunge-below)
8006                (not gnus-use-nocem)))
8007       ()                                ; Do nothing.
8008     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8009     (setq gnus-newsgroup-limit nil)
8010     (mapatoms
8011      (lambda (node)
8012        (unless (car (symbol-value node))
8013          ;; These threads have no parents -- they are roots.
8014          (let ((nodes (cdr (symbol-value node)))
8015                thread)
8016            (while nodes
8017              (if (and gnus-thread-expunge-below
8018                       (< (gnus-thread-total-score (car nodes))
8019                          gnus-thread-expunge-below))
8020                  (gnus-expunge-thread (pop nodes))
8021                (setq thread (pop nodes))
8022                (gnus-summary-limit-children thread))))))
8023      gnus-newsgroup-dependencies)
8024     ;; If this limitation resulted in an empty group, we might
8025     ;; pop the previous limit and use it instead.
8026     (when (and (not gnus-newsgroup-limit)
8027                show-if-empty)
8028       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8029     gnus-newsgroup-limit))
8030
8031 (defun gnus-summary-limit-children (thread)
8032   "Return 1 if this subthread is visible and 0 if it is not."
8033   ;; First we get the number of visible children to this thread.  This
8034   ;; is done by recursing down the thread using this function, so this
8035   ;; will really go down to a leaf article first, before slowly
8036   ;; working its way up towards the root.
8037   (when thread
8038     (let* ((max-lisp-eval-depth 5000)
8039            (children
8040            (if (cdr thread)
8041                (apply '+ (mapcar 'gnus-summary-limit-children
8042                                  (cdr thread)))
8043              0))
8044           (number (mail-header-number (car thread)))
8045           score)
8046       (if (and
8047            (not (memq number gnus-newsgroup-marked))
8048            (or
8049             ;; If this article is dormant and has absolutely no visible
8050             ;; children, then this article isn't visible.
8051             (and (memq number gnus-newsgroup-dormant)
8052                  (zerop children))
8053             ;; If this is "fetch-old-headered" and there is no
8054             ;; visible children, then we don't want this article.
8055             (and (or (eq gnus-fetch-old-headers 'some)
8056                      (numberp gnus-fetch-old-headers))
8057                  (gnus-summary-article-ancient-p number)
8058                  (zerop children))
8059             ;; If this is "fetch-old-headered" and `invisible', then
8060             ;; we don't want this article.
8061             (and (eq gnus-fetch-old-headers 'invisible)
8062                  (gnus-summary-article-ancient-p number))
8063             ;; If this is a sparsely inserted article with no children,
8064             ;; we don't want it.
8065             (and (eq gnus-build-sparse-threads 'some)
8066                  (gnus-summary-article-sparse-p number)
8067                  (zerop children))
8068             ;; If we use expunging, and this article is really
8069             ;; low-scored, then we don't want this article.
8070             (when (and gnus-summary-expunge-below
8071                        (< (setq score
8072                                 (or (cdr (assq number gnus-newsgroup-scored))
8073                                     gnus-summary-default-score))
8074                           gnus-summary-expunge-below))
8075               ;; We increase the expunge-tally here, but that has
8076               ;; nothing to do with the limits, really.
8077               (incf gnus-newsgroup-expunged-tally)
8078               ;; We also mark as read here, if that's wanted.
8079               (when (and gnus-summary-mark-below
8080                          (< score gnus-summary-mark-below))
8081                 (setq gnus-newsgroup-unreads
8082                       (delq number gnus-newsgroup-unreads))
8083                 (if gnus-newsgroup-auto-expire
8084                     (push number gnus-newsgroup-expirable)
8085                   (push (cons number gnus-low-score-mark)
8086                         gnus-newsgroup-reads)))
8087               t)
8088             ;; Do the `display' group parameter.
8089             (and gnus-newsgroup-display
8090                  (not (funcall gnus-newsgroup-display)))
8091             ;; Check NoCeM things.
8092             (when (and gnus-use-nocem
8093                        (gnus-nocem-unwanted-article-p
8094                         (mail-header-id (car thread))))
8095               (setq gnus-newsgroup-unreads
8096                     (delq number gnus-newsgroup-unreads))
8097               t)))
8098           ;; Nope, invisible article.
8099           0
8100         ;; Ok, this article is to be visible, so we add it to the limit
8101         ;; and return 1.
8102         (push number gnus-newsgroup-limit)
8103         1))))
8104
8105 (defun gnus-expunge-thread (thread)
8106   "Mark all articles in THREAD as read."
8107   (let* ((number (mail-header-number (car thread))))
8108     (incf gnus-newsgroup-expunged-tally)
8109     ;; We also mark as read here, if that's wanted.
8110     (setq gnus-newsgroup-unreads
8111           (delq number gnus-newsgroup-unreads))
8112     (if gnus-newsgroup-auto-expire
8113         (push number gnus-newsgroup-expirable)
8114       (push (cons number gnus-low-score-mark)
8115             gnus-newsgroup-reads)))
8116   ;; Go recursively through all subthreads.
8117   (mapcar 'gnus-expunge-thread (cdr thread)))
8118
8119 ;; Summary article oriented commands
8120
8121 (defun gnus-summary-refer-parent-article (n)
8122   "Refer parent article N times.
8123 If N is negative, go to ancestor -N instead.
8124 The difference between N and the number of articles fetched is returned."
8125   (interactive "p")
8126   (let ((skip 1)
8127         error header ref)
8128     (when (not (natnump n))
8129       (setq skip (abs n)
8130             n 1))
8131     (while (and (> n 0)
8132                 (not error))
8133       (setq header (gnus-summary-article-header))
8134       (if (and (eq (mail-header-number header)
8135                    (cdr gnus-article-current))
8136                (equal gnus-newsgroup-name
8137                       (car gnus-article-current)))
8138           ;; If we try to find the parent of the currently
8139           ;; displayed article, then we take a look at the actual
8140           ;; References header, since this is slightly more
8141           ;; reliable than the References field we got from the
8142           ;; server.
8143           (save-excursion
8144             (set-buffer gnus-original-article-buffer)
8145             (nnheader-narrow-to-headers)
8146             (unless (setq ref (message-fetch-field "references"))
8147               (when (setq ref (message-fetch-field "in-reply-to"))
8148                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8149             (widen))
8150         (setq ref
8151               ;; It's not the current article, so we take a bet on
8152               ;; the value we got from the server.
8153               (mail-header-references header)))
8154       (if (and ref
8155                (not (equal ref "")))
8156           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8157             (gnus-message 1 "Couldn't find parent"))
8158         (gnus-message 1 "No references in article %d"
8159                       (gnus-summary-article-number))
8160         (setq error t))
8161       (decf n))
8162     (gnus-summary-position-point)
8163     n))
8164
8165 (defun gnus-summary-refer-references ()
8166   "Fetch all articles mentioned in the References header.
8167 Return the number of articles fetched."
8168   (interactive)
8169   (let ((ref (mail-header-references (gnus-summary-article-header)))
8170         (current (gnus-summary-article-number))
8171         (n 0))
8172     (if (or (not ref)
8173             (equal ref ""))
8174         (error "No References in the current article")
8175       ;; For each Message-ID in the References header...
8176       (while (string-match "<[^>]*>" ref)
8177         (incf n)
8178         ;; ... fetch that article.
8179         (gnus-summary-refer-article
8180          (prog1 (match-string 0 ref)
8181            (setq ref (substring ref (match-end 0))))))
8182       (gnus-summary-goto-subject current)
8183       (gnus-summary-position-point)
8184       n)))
8185
8186 (defun gnus-summary-refer-thread (&optional limit)
8187   "Fetch all articles in the current thread.
8188 If LIMIT (the numerical prefix), fetch that many old headers instead
8189 of what's specified by the `gnus-refer-thread-limit' variable."
8190   (interactive "P")
8191   (let ((id (mail-header-id (gnus-summary-article-header)))
8192         (limit (if limit (prefix-numeric-value limit)
8193                  gnus-refer-thread-limit)))
8194     (unless (eq gnus-fetch-old-headers 'invisible)
8195       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8196       ;; Retrieve the headers and read them in.
8197       (if (eq (if (numberp limit)
8198                   (gnus-retrieve-headers
8199                    (list (min
8200                           (+ (mail-header-number
8201                               (gnus-summary-article-header))
8202                              limit)
8203                           gnus-newsgroup-end))
8204                    gnus-newsgroup-name (* limit 2))
8205                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8206                 ;; headers.
8207                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8208                                        gnus-newsgroup-name limit))
8209               'nov)
8210           (gnus-build-all-threads)
8211         (error "Can't fetch thread from back ends that don't support NOV"))
8212       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8213     (gnus-summary-limit-include-thread id)))
8214
8215 (defun gnus-summary-refer-article (message-id)
8216   "Fetch an article specified by MESSAGE-ID."
8217   (interactive "sMessage-ID: ")
8218   (when (and (stringp message-id)
8219              (not (zerop (length message-id))))
8220     (setq message-id (gnus-replace-in-string message-id " " ""))
8221     ;; Construct the correct Message-ID if necessary.
8222     ;; Suggested by tale@pawl.rpi.edu.
8223     (unless (string-match "^<" message-id)
8224       (setq message-id (concat "<" message-id)))
8225     (unless (string-match ">$" message-id)
8226       (setq message-id (concat message-id ">")))
8227     ;; People often post MIDs from URLs, so unhex it:
8228     (unless (string-match "@" message-id)
8229       (setq message-id (gnus-url-unhex-string message-id)))
8230     (let* ((header (gnus-id-to-header message-id))
8231            (sparse (and header
8232                         (gnus-summary-article-sparse-p
8233                          (mail-header-number header))
8234                         (memq (mail-header-number header)
8235                               gnus-newsgroup-limit)))
8236            number)
8237       (cond
8238        ;; If the article is present in the buffer we just go to it.
8239        ((and header
8240              (or (not (gnus-summary-article-sparse-p
8241                        (mail-header-number header)))
8242                  sparse))
8243         (prog1
8244             (gnus-summary-goto-article
8245              (mail-header-number header) nil t)
8246           (when sparse
8247             (gnus-summary-update-article (mail-header-number header)))))
8248        (t
8249         ;; We fetch the article.
8250         (catch 'found
8251           (dolist (gnus-override-method (gnus-refer-article-methods))
8252             (when (and (gnus-check-server gnus-override-method)
8253                        ;; Fetch the header,
8254                        (setq number (gnus-summary-insert-subject message-id)))
8255               ;; and display the article.
8256               (gnus-summary-select-article nil nil nil number)
8257               (throw 'found t)))
8258           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8259
8260 (defun gnus-refer-article-methods ()
8261   "Return a list of referable methods."
8262   (cond
8263    ;; No method, so we default to current and native.
8264    ((null gnus-refer-article-method)
8265     (list gnus-current-select-method gnus-select-method))
8266    ;; Current.
8267    ((eq 'current gnus-refer-article-method)
8268     (list gnus-current-select-method))
8269    ;; List of select methods.
8270    ((not (and (symbolp (car gnus-refer-article-method))
8271               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8272     (let (out)
8273       (dolist (method gnus-refer-article-method)
8274         (push (if (eq 'current method)
8275                   gnus-current-select-method
8276                 method)
8277               out))
8278       (nreverse out)))
8279    ;; One single select method.
8280    (t
8281     (list gnus-refer-article-method))))
8282
8283 (defun gnus-summary-edit-parameters ()
8284   "Edit the group parameters of the current group."
8285   (interactive)
8286   (gnus-group-edit-group gnus-newsgroup-name 'params))
8287
8288 (defun gnus-summary-customize-parameters ()
8289   "Customize the group parameters of the current group."
8290   (interactive)
8291   (gnus-group-customize gnus-newsgroup-name))
8292
8293 (defun gnus-summary-enter-digest-group (&optional force)
8294   "Enter an nndoc group based on the current article.
8295 If FORCE, force a digest interpretation.  If not, try
8296 to guess what the document format is."
8297   (interactive "P")
8298   (let ((conf gnus-current-window-configuration))
8299     (save-window-excursion
8300       (save-excursion
8301         (let (gnus-article-prepare-hook
8302               gnus-display-mime-function
8303               gnus-break-pages)
8304           (gnus-summary-select-article))))
8305     (setq gnus-current-window-configuration conf)
8306     (let* ((name (format "%s-%d"
8307                          (gnus-group-prefixed-name
8308                           gnus-newsgroup-name (list 'nndoc ""))
8309                          (with-current-buffer gnus-summary-buffer
8310                            gnus-current-article)))
8311            (ogroup gnus-newsgroup-name)
8312            (params (append (gnus-info-params (gnus-get-info ogroup))
8313                            (list (cons 'to-group ogroup))
8314                            (list (cons 'parent-group ogroup))
8315                            (list (cons 'save-article-group ogroup))))
8316            (case-fold-search t)
8317            (buf (current-buffer))
8318            dig to-address)
8319       (save-excursion
8320         (set-buffer gnus-original-article-buffer)
8321         ;; Have the digest group inherit the main mail address of
8322         ;; the parent article.
8323         (when (setq to-address (or (gnus-fetch-field "reply-to")
8324                                    (gnus-fetch-field "from")))
8325           (setq params (append
8326                         (list (cons 'to-address
8327                                     (funcall gnus-decode-encoded-word-function
8328                                              to-address))))))
8329         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8330         (insert-buffer-substring gnus-original-article-buffer)
8331         ;; Remove lines that may lead nndoc to misinterpret the
8332         ;; document type.
8333         (narrow-to-region
8334          (goto-char (point-min))
8335          (or (search-forward "\n\n" nil t) (point)))
8336         (goto-char (point-min))
8337         (delete-matching-lines "^Path:\\|^From ")
8338         (widen))
8339       (unwind-protect
8340           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8341                     (gnus-newsgroup-ephemeral-ignored-charsets
8342                      gnus-newsgroup-ignored-charsets))
8343                 (gnus-group-read-ephemeral-group
8344                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8345                               (nndoc-article-type
8346                                ,(if force 'mbox 'guess)))
8347                  t nil nil nil
8348                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8349                                                         "ADAPT")))))
8350               ;; Make all postings to this group go to the parent group.
8351               (nconc (gnus-info-params (gnus-get-info name))
8352                      params)
8353             ;; Couldn't select this doc group.
8354             (switch-to-buffer buf)
8355             (gnus-set-global-variables)
8356             (gnus-configure-windows 'summary)
8357             (gnus-message 3 "Article couldn't be entered?"))
8358         (kill-buffer dig)))))
8359
8360 (defun gnus-summary-read-document (n)
8361   "Open a new group based on the current article(s).
8362 This will allow you to read digests and other similar
8363 documents as newsgroups.
8364 Obeys the standard process/prefix convention."
8365   (interactive "P")
8366   (let* ((articles (gnus-summary-work-articles n))
8367          (ogroup gnus-newsgroup-name)
8368          (params (append (gnus-info-params (gnus-get-info ogroup))
8369                          (list (cons 'to-group ogroup))))
8370          article group egroup groups vgroup)
8371     (while (setq article (pop articles))
8372       (setq group (format "%s-%d" gnus-newsgroup-name article))
8373       (gnus-summary-remove-process-mark article)
8374       (when (gnus-summary-display-article article)
8375         (save-excursion
8376           (with-temp-buffer
8377             (insert-buffer-substring gnus-original-article-buffer)
8378             ;; Remove some headers that may lead nndoc to make
8379             ;; the wrong guess.
8380             (message-narrow-to-head)
8381             (goto-char (point-min))
8382             (delete-matching-lines "^Path:\\|^From ")
8383             (widen)
8384             (if (setq egroup
8385                       (gnus-group-read-ephemeral-group
8386                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8387                                      (nndoc-article-type guess))
8388                        t nil t))
8389                 (progn
8390             ;; Make all postings to this group go to the parent group.
8391                   (nconc (gnus-info-params (gnus-get-info egroup))
8392                          params)
8393                   (push egroup groups))
8394               ;; Couldn't select this doc group.
8395               (gnus-error 3 "Article couldn't be entered"))))))
8396     ;; Now we have selected all the documents.
8397     (cond
8398      ((not groups)
8399       (error "None of the articles could be interpreted as documents"))
8400      ((gnus-group-read-ephemeral-group
8401        (setq vgroup (format
8402                      "nnvirtual:%s-%s" gnus-newsgroup-name
8403                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8404        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8405        t
8406        (cons (current-buffer) 'summary)))
8407      (t
8408       (error "Couldn't select virtual nndoc group")))))
8409
8410 (defun gnus-summary-isearch-article (&optional regexp-p)
8411   "Do incremental search forward on the current article.
8412 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8413   (interactive "P")
8414   (gnus-summary-select-article)
8415   (gnus-configure-windows 'article)
8416   (gnus-eval-in-buffer-window gnus-article-buffer
8417     (save-restriction
8418       (widen)
8419       (isearch-forward regexp-p))))
8420
8421 (defun gnus-summary-search-article-forward (regexp &optional backward)
8422   "Search for an article containing REGEXP forward.
8423 If BACKWARD, search backward instead."
8424   (interactive
8425    (list (read-string
8426           (format "Search article %s (regexp%s): "
8427                   (if current-prefix-arg "backward" "forward")
8428                   (if gnus-last-search-regexp
8429                       (concat ", default " gnus-last-search-regexp)
8430                     "")))
8431          current-prefix-arg))
8432   (if (string-equal regexp "")
8433       (setq regexp (or gnus-last-search-regexp ""))
8434     (setq gnus-last-search-regexp regexp)
8435     (setq gnus-article-before-search gnus-current-article))
8436   ;; Intentionally set gnus-last-article.
8437   (setq gnus-last-article gnus-article-before-search)
8438   (let ((gnus-last-article gnus-last-article))
8439     (if (gnus-summary-search-article regexp backward)
8440         (gnus-summary-show-thread)
8441       (error "Search failed: \"%s\"" regexp))))
8442
8443 (defun gnus-summary-search-article-backward (regexp)
8444   "Search for an article containing REGEXP backward."
8445   (interactive
8446    (list (read-string
8447           (format "Search article backward (regexp%s): "
8448                   (if gnus-last-search-regexp
8449                       (concat ", default " gnus-last-search-regexp)
8450                     "")))))
8451   (gnus-summary-search-article-forward regexp 'backward))
8452
8453 (defun gnus-summary-search-article (regexp &optional backward)
8454   "Search for an article containing REGEXP.
8455 Optional argument BACKWARD means do search for backward.
8456 `gnus-select-article-hook' is not called during the search."
8457   ;; We have to require this here to make sure that the following
8458   ;; dynamic binding isn't shadowed by autoloading.
8459   (require 'gnus-async)
8460   (require 'gnus-art)
8461   (let ((gnus-select-article-hook nil)  ;Disable hook.
8462         (gnus-article-prepare-hook nil)
8463         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8464         (gnus-use-article-prefetch nil)
8465         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8466         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8467         (gnus-visual nil)
8468         (gnus-keep-backlog nil)
8469         (gnus-break-pages nil)
8470         (gnus-summary-display-arrow nil)
8471         (gnus-updated-mode-lines nil)
8472         (gnus-auto-center-summary nil)
8473         (sum (current-buffer))
8474         (gnus-display-mime-function nil)
8475         (found nil)
8476         point)
8477     (gnus-save-hidden-threads
8478       (gnus-summary-select-article)
8479       (set-buffer gnus-article-buffer)
8480       (goto-char (window-point (get-buffer-window (current-buffer))))
8481       (when backward
8482         (forward-line -1))
8483       (while (not found)
8484         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8485         (if (if backward
8486                 (re-search-backward regexp nil t)
8487               (re-search-forward regexp nil t))
8488             ;; We found the regexp.
8489             (progn
8490               (setq found 'found)
8491               (beginning-of-line)
8492               (set-window-start
8493                (get-buffer-window (current-buffer))
8494                (point))
8495               (forward-line 1)
8496               (set-window-point
8497                (get-buffer-window (current-buffer))
8498                (point))
8499               (set-buffer sum)
8500               (setq point (point)))
8501           ;; We didn't find it, so we go to the next article.
8502           (set-buffer sum)
8503           (setq found 'not)
8504           (while (eq found 'not)
8505             (if (not (if backward (gnus-summary-find-prev)
8506                        (gnus-summary-find-next)))
8507                 ;; No more articles.
8508                 (setq found t)
8509               ;; Select the next article and adjust point.
8510               (unless (gnus-summary-article-sparse-p
8511                        (gnus-summary-article-number))
8512                 (setq found nil)
8513                 (gnus-summary-select-article)
8514                 (set-buffer gnus-article-buffer)
8515                 (widen)
8516                 (goto-char (if backward (point-max) (point-min))))))))
8517       (gnus-message 7 ""))
8518     ;; Return whether we found the regexp.
8519     (when (eq found 'found)
8520       (goto-char point)
8521       (gnus-summary-show-thread)
8522       (gnus-summary-goto-subject gnus-current-article)
8523       (gnus-summary-position-point)
8524       t)))
8525
8526 (defun gnus-find-matching-articles (header regexp)
8527   "Return a list of all articles that match REGEXP on HEADER.
8528 This search includes all articles in the current group that Gnus has
8529 fetched headers for, whether they are displayed or not."
8530   (let ((articles nil)
8531         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8532         (case-fold-search t))
8533     (dolist (header gnus-newsgroup-headers)
8534       (when (string-match regexp (funcall func header))
8535         (push (mail-header-number header) articles)))
8536     (nreverse articles)))
8537
8538 (defun gnus-summary-find-matching (header regexp &optional backward unread
8539                                           not-case-fold not-matching)
8540   "Return a list of all articles that match REGEXP on HEADER.
8541 The search stars on the current article and goes forwards unless
8542 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8543 If UNREAD is non-nil, only unread articles will
8544 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8545 in the comparisons. If NOT-MATCHING, return a list of all articles that
8546 not match REGEXP on HEADER."
8547   (let ((case-fold-search (not not-case-fold))
8548         articles d func)
8549     (if (consp header)
8550         (if (eq (car header) 'extra)
8551             (setq func
8552                   `(lambda (h)
8553                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8554                          "")))
8555           (error "%s is an invalid header" header))
8556       (unless (fboundp (intern (concat "mail-header-" header)))
8557         (error "%s is not a valid header" header))
8558       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8559     (dolist (d (if (eq backward 'all)
8560                    gnus-newsgroup-data
8561                  (gnus-data-find-list
8562                   (gnus-summary-article-number)
8563                   (gnus-data-list backward))))
8564       (when (and (or (not unread)       ; We want all articles...
8565                      (gnus-data-unread-p d)) ; Or just unreads.
8566                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8567                  (if not-matching
8568                      (not (string-match
8569                            regexp
8570                            (funcall func (gnus-data-header d))))
8571                    (string-match regexp
8572                                  (funcall func (gnus-data-header d)))))
8573         (push (gnus-data-number d) articles))) ; Success!
8574     (nreverse articles)))
8575
8576 (defun gnus-summary-execute-command (header regexp command &optional backward)
8577   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8578 If HEADER is an empty string (or nil), the match is done on the entire
8579 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8580   (interactive
8581    (list (let ((completion-ignore-case t))
8582            (completing-read
8583             "Header name: "
8584             (mapcar (lambda (header) (list (format "%s" header)))
8585                     (append
8586                      '("Number" "Subject" "From" "Lines" "Date"
8587                        "Message-ID" "Xref" "References" "Body")
8588                      gnus-extra-headers))
8589             nil 'require-match))
8590          (read-string "Regexp: ")
8591          (read-key-sequence "Command: ")
8592          current-prefix-arg))
8593   (when (equal header "Body")
8594     (setq header ""))
8595   ;; Hidden thread subtrees must be searched as well.
8596   (gnus-summary-show-all-threads)
8597   ;; We don't want to change current point nor window configuration.
8598   (save-excursion
8599     (save-window-excursion
8600       (let (gnus-visual
8601             gnus-treat-strip-trailing-blank-lines
8602             gnus-treat-strip-leading-blank-lines
8603             gnus-treat-strip-multiple-blank-lines
8604             gnus-treat-hide-boring-headers
8605             gnus-treat-fold-newsgroups
8606             gnus-article-prepare-hook)
8607         (gnus-message 6 "Executing %s..." (key-description command))
8608         ;; We'd like to execute COMMAND interactively so as to give arguments.
8609         (gnus-execute header regexp
8610                       `(call-interactively ',(key-binding command))
8611                       backward)
8612         (gnus-message 6 "Executing %s...done" (key-description command))))))
8613
8614 (defun gnus-summary-beginning-of-article ()
8615   "Scroll the article back to the beginning."
8616   (interactive)
8617   (gnus-summary-select-article)
8618   (gnus-configure-windows 'article)
8619   (gnus-eval-in-buffer-window gnus-article-buffer
8620     (widen)
8621     (goto-char (point-min))
8622     (when gnus-break-pages
8623       (gnus-narrow-to-page))))
8624
8625 (defun gnus-summary-end-of-article ()
8626   "Scroll to the end of the article."
8627   (interactive)
8628   (gnus-summary-select-article)
8629   (gnus-configure-windows 'article)
8630   (gnus-eval-in-buffer-window gnus-article-buffer
8631     (widen)
8632     (goto-char (point-max))
8633     (recenter -3)
8634     (when gnus-break-pages
8635       (when (re-search-backward page-delimiter nil t)
8636         (narrow-to-region (match-end 0) (point-max)))
8637       (gnus-narrow-to-page))))
8638
8639 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8640   "Truncate to LEN and quote all \"(\"'s in STRING."
8641   (gnus-replace-in-string (if (and len (> (length string) len))
8642                               (substring string 0 len)
8643                             string)
8644                           "[()]" "\\\\\\&"))
8645
8646 (defun gnus-summary-print-article (&optional filename n)
8647   "Generate and print a PostScript image of the process-marked (mail) articles.
8648
8649 If used interactively, print the current article if none are
8650 process-marked.  With prefix arg, prompt the user for the name of the
8651 file to save in.
8652
8653 When used from Lisp, accept two optional args FILENAME and N.  N means
8654 to print the next N articles.  If N is negative, print the N previous
8655 articles.  If N is nil and articles have been marked with the process
8656 mark, print these instead.
8657
8658 If the optional first argument FILENAME is nil, send the image to the
8659 printer.  If FILENAME is a string, save the PostScript image in a file with
8660 that name.  If FILENAME is a number, prompt the user for the name of the file
8661 to save in."
8662   (interactive (list (ps-print-preprint current-prefix-arg)))
8663   (dolist (article (gnus-summary-work-articles n))
8664     (gnus-summary-select-article nil nil 'pseudo article)
8665     (gnus-eval-in-buffer-window gnus-article-buffer
8666       (gnus-print-buffer))
8667     (gnus-summary-remove-process-mark article))
8668   (ps-despool filename))
8669
8670 (defun gnus-print-buffer ()
8671   (let ((buffer (generate-new-buffer " *print*")))
8672     (unwind-protect
8673         (progn
8674           (copy-to-buffer buffer (point-min) (point-max))
8675           (set-buffer buffer)
8676           (gnus-remove-text-with-property 'gnus-decoration)
8677           (when (gnus-visual-p 'article-highlight 'highlight)
8678             ;; Copy-to-buffer doesn't copy overlay.  So redo
8679             ;; highlight.
8680             (let ((gnus-article-buffer buffer))
8681               (gnus-article-highlight-citation t)
8682               (gnus-article-highlight-signature)
8683               (gnus-article-emphasize)
8684               (gnus-article-delete-invisible-text)))
8685           (let ((ps-left-header
8686                  (list
8687                   (concat "("
8688                           (gnus-summary-print-truncate-and-quote
8689                            (mail-header-subject gnus-current-headers)
8690                            66) ")")
8691                   (concat "("
8692                           (gnus-summary-print-truncate-and-quote
8693                            (mail-header-from gnus-current-headers)
8694                            45) ")")))
8695                 (ps-right-header
8696                  (list
8697                   "/pagenumberstring load"
8698                   (concat "("
8699                           (mail-header-date gnus-current-headers) ")"))))
8700             (gnus-run-hooks 'gnus-ps-print-hook)
8701             (save-excursion
8702               (if window-system
8703                   (ps-spool-buffer-with-faces)
8704                 (ps-spool-buffer)))))
8705       (kill-buffer buffer))))
8706
8707 (defun gnus-summary-show-article (&optional arg)
8708   "Force redisplaying of the current article.
8709 If ARG (the prefix) is a number, show the article with the charset
8710 defined in `gnus-summary-show-article-charset-alist', or the charset
8711 input.
8712 If ARG (the prefix) is non-nil and not a number, show the raw article
8713 without any article massaging functions being run.  Normally, the key
8714 strokes are `C-u g'."
8715   (interactive "P")
8716   (cond
8717    ((numberp arg)
8718     (gnus-summary-show-article t)
8719     (let ((gnus-newsgroup-charset
8720            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8721                (mm-read-coding-system
8722                 "View as charset: " ;; actually it is coding system.
8723                 (save-excursion
8724                   (set-buffer gnus-article-buffer)
8725                   (mm-detect-coding-region (point) (point-max))))))
8726           (gnus-newsgroup-ignored-charsets 'gnus-all))
8727       (gnus-summary-select-article nil 'force)
8728       (let ((deps gnus-newsgroup-dependencies)
8729             head header lines)
8730         (save-excursion
8731           (set-buffer gnus-original-article-buffer)
8732           (save-restriction
8733             (message-narrow-to-head)
8734             (setq head (buffer-string))
8735             (goto-char (point-min))
8736             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8737               (goto-char (point-max))
8738               (widen)
8739               (setq lines (1- (count-lines (point) (point-max))))))
8740           (with-temp-buffer
8741             (insert (format "211 %d Article retrieved.\n"
8742                             (cdr gnus-article-current)))
8743             (insert head)
8744             (if lines (insert (format "Lines: %d\n" lines)))
8745             (insert ".\n")
8746             (let ((nntp-server-buffer (current-buffer)))
8747               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8748         (gnus-data-set-header
8749          (gnus-data-find (cdr gnus-article-current))
8750          header)
8751         (gnus-summary-update-article-line
8752          (cdr gnus-article-current) header)
8753         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8754           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8755    ((not arg)
8756     ;; Select the article the normal way.
8757     (gnus-summary-select-article nil 'force))
8758    (t
8759     ;; We have to require this here to make sure that the following
8760     ;; dynamic binding isn't shadowed by autoloading.
8761     (require 'gnus-async)
8762     (require 'gnus-art)
8763     ;; Bind the article treatment functions to nil.
8764     (let ((gnus-have-all-headers t)
8765           gnus-article-prepare-hook
8766           gnus-article-decode-hook
8767           gnus-display-mime-function
8768           gnus-break-pages)
8769       ;; Destroy any MIME parts.
8770       (when (gnus-buffer-live-p gnus-article-buffer)
8771         (save-excursion
8772           (set-buffer gnus-article-buffer)
8773           (mm-destroy-parts gnus-article-mime-handles)
8774           ;; Set it to nil for safety reason.
8775           (setq gnus-article-mime-handle-alist nil)
8776           (setq gnus-article-mime-handles nil)))
8777       (gnus-summary-select-article nil 'force))))
8778   (gnus-summary-goto-subject gnus-current-article)
8779   (gnus-summary-position-point))
8780
8781 (defun gnus-summary-show-raw-article ()
8782   "Show the raw article without any article massaging functions being run."
8783   (interactive)
8784   (gnus-summary-show-article t))
8785
8786 (defun gnus-summary-verbose-headers (&optional arg)
8787   "Toggle permanent full header display.
8788 If ARG is a positive number, turn header display on.
8789 If ARG is a negative number, turn header display off."
8790   (interactive "P")
8791   (setq gnus-show-all-headers
8792         (cond ((or (not (numberp arg))
8793                    (zerop arg))
8794                (not gnus-show-all-headers))
8795               ((natnump arg)
8796                t)))
8797   (gnus-summary-show-article))
8798
8799 (defun gnus-summary-toggle-header (&optional arg)
8800   "Show the headers if they are hidden, or hide them if they are shown.
8801 If ARG is a positive number, show the entire header.
8802 If ARG is a negative number, hide the unwanted header lines."
8803   (interactive "P")
8804   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8805                      (get-buffer-window gnus-article-buffer t))))
8806     (with-current-buffer gnus-article-buffer
8807       (widen)
8808       (article-narrow-to-head)
8809       (let* ((buffer-read-only nil)
8810              (inhibit-point-motion-hooks t)
8811              (hidden (if (numberp arg)
8812                          (>= arg 0)
8813                        (gnus-article-hidden-text-p 'headers)))
8814              s e)
8815         (delete-region (point-min) (point-max))
8816         (with-current-buffer gnus-original-article-buffer
8817           (goto-char (setq s (point-min)))
8818           (setq e (if (search-forward "\n\n" nil t)
8819                       (1- (point))
8820                     (point-max))))
8821         (insert-buffer-substring gnus-original-article-buffer s e)
8822         (run-hooks 'gnus-article-decode-hook)
8823         (if hidden
8824             (let ((gnus-treat-hide-headers nil)
8825                   (gnus-treat-hide-boring-headers nil))
8826               (gnus-delete-wash-type 'headers)
8827               (gnus-treat-article 'head))
8828           (gnus-treat-article 'head))
8829         (widen)
8830         (if window
8831             (set-window-start window (goto-char (point-min))))
8832         (if gnus-break-pages
8833             (gnus-narrow-to-page)
8834           (when (gnus-visual-p 'page-marker)
8835             (let ((buffer-read-only nil))
8836               (gnus-remove-text-with-property 'gnus-prev)
8837               (gnus-remove-text-with-property 'gnus-next))))
8838         (gnus-set-mode-line 'article)))))
8839
8840 (defun gnus-summary-show-all-headers ()
8841   "Make all header lines visible."
8842   (interactive)
8843   (gnus-summary-toggle-header 1))
8844
8845 (defun gnus-summary-caesar-message (&optional arg)
8846   "Caesar rotate the current article by 13.
8847 The numerical prefix specifies how many places to rotate each letter
8848 forward."
8849   (interactive "P")
8850   (gnus-summary-select-article)
8851   (let ((mail-header-separator ""))
8852     (gnus-eval-in-buffer-window gnus-article-buffer
8853       (save-restriction
8854         (widen)
8855         (let ((start (window-start))
8856               buffer-read-only)
8857           (message-caesar-buffer-body arg)
8858           (set-window-start (get-buffer-window (current-buffer)) start))))))
8859
8860 (autoload 'unmorse-region "morse"
8861   "Convert morse coded text in region to ordinary ASCII text."
8862   t)
8863
8864 (defun gnus-summary-morse-message (&optional arg)
8865   "Morse decode the current article."
8866   (interactive "P")
8867   (gnus-summary-select-article)
8868   (let ((mail-header-separator ""))
8869     (gnus-eval-in-buffer-window gnus-article-buffer
8870       (save-excursion
8871         (save-restriction
8872           (widen)
8873           (let ((pos (window-start))
8874                 buffer-read-only)
8875             (goto-char (point-min))
8876             (when (message-goto-body)
8877               (gnus-narrow-to-body))
8878             (goto-char (point-min))
8879             (while (re-search-forward "·" (point-max) t)
8880               (replace-match "."))
8881             (unmorse-region (point-min) (point-max))
8882             (widen)
8883             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8884
8885 (defun gnus-summary-stop-page-breaking ()
8886   "Stop page breaking in the current article."
8887   (interactive)
8888   (gnus-summary-select-article)
8889   (gnus-eval-in-buffer-window gnus-article-buffer
8890     (widen)
8891     (when (gnus-visual-p 'page-marker)
8892       (let ((buffer-read-only nil))
8893         (gnus-remove-text-with-property 'gnus-prev)
8894         (gnus-remove-text-with-property 'gnus-next))
8895       (setq gnus-page-broken nil))))
8896
8897 (defun gnus-summary-move-article (&optional n to-newsgroup
8898                                             select-method action)
8899   "Move the current article to a different newsgroup.
8900 If N is a positive number, move the N next articles.
8901 If N is a negative number, move the N previous articles.
8902 If N is nil and any articles have been marked with the process mark,
8903 move those articles instead.
8904 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8905 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8906 re-spool using this method.
8907
8908 When called interactively with TO-NEWSGROUP being nil, the value of
8909 the variable `gnus-move-split-methods' is used for finding a default
8910 for the target newsgroup.
8911
8912 For this function to work, both the current newsgroup and the
8913 newsgroup that you want to move to have to support the `request-move'
8914 and `request-accept' functions.
8915
8916 ACTION can be either `move' (the default), `crosspost' or `copy'."
8917   (interactive "P")
8918   (unless action
8919     (setq action 'move))
8920   ;; Check whether the source group supports the required functions.
8921   (cond ((and (eq action 'move)
8922               (not (gnus-check-backend-function
8923                     'request-move-article gnus-newsgroup-name)))
8924          (error "The current group does not support article moving"))
8925         ((and (eq action 'crosspost)
8926               (not (gnus-check-backend-function
8927                     'request-replace-article gnus-newsgroup-name)))
8928          (error "The current group does not support article editing")))
8929   (let ((articles (gnus-summary-work-articles n))
8930         (prefix (if (gnus-check-backend-function
8931                      'request-move-article gnus-newsgroup-name)
8932                     (funcall gnus-move-group-prefix-function
8933                              gnus-newsgroup-name)
8934                   ""))
8935         (names '((move "Move" "Moving")
8936                  (copy "Copy" "Copying")
8937                  (crosspost "Crosspost" "Crossposting")))
8938         (copy-buf (save-excursion
8939                     (nnheader-set-temp-buffer " *copy article*")))
8940         art-group to-method new-xref article to-groups)
8941     (unless (assq action names)
8942       (error "Unknown action %s" action))
8943     ;; Read the newsgroup name.
8944     (when (and (not to-newsgroup)
8945                (not select-method))
8946       (if (and gnus-move-split-methods
8947                (not
8948                 (and (memq gnus-current-article articles)
8949                      (gnus-buffer-live-p gnus-original-article-buffer))))
8950           ;; When `gnus-move-split-methods' is non-nil, we have to
8951           ;; select an article to give `gnus-read-move-group-name' an
8952           ;; opportunity to suggest an appropriate default.  However,
8953           ;; we needn't render or mark the article.
8954           (let ((gnus-display-mime-function nil)
8955                 (gnus-article-prepare-hook nil)
8956                 (gnus-mark-article-hook nil))
8957             (gnus-summary-select-article nil nil nil (car articles))))
8958       (setq to-newsgroup
8959             (gnus-read-move-group-name
8960              (cadr (assq action names))
8961              (symbol-value (intern (format "gnus-current-%s-group" action)))
8962              articles prefix))
8963       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8964     (setq to-method (or select-method
8965                         (gnus-server-to-method
8966                          (gnus-group-method to-newsgroup))))
8967     ;; Check the method we are to move this article to...
8968     (unless (gnus-check-backend-function
8969              'request-accept-article (car to-method))
8970       (error "%s does not support article copying" (car to-method)))
8971     (unless (gnus-check-server to-method)
8972       (error "Can't open server %s" (car to-method)))
8973     (gnus-message 6 "%s to %s: %s..."
8974                   (caddr (assq action names))
8975                   (or (car select-method) to-newsgroup) articles)
8976     (while articles
8977       (setq article (pop articles))
8978       (setq
8979        art-group
8980        (cond
8981         ;; Move the article.
8982         ((eq action 'move)
8983          ;; Remove this article from future suppression.
8984          (gnus-dup-unsuppress-article article)
8985          (gnus-request-move-article
8986           article                       ; Article to move
8987           gnus-newsgroup-name           ; From newsgroup
8988           (nth 1 (gnus-find-method-for-group
8989                   gnus-newsgroup-name)) ; Server
8990           (list 'gnus-request-accept-article
8991                 to-newsgroup (list 'quote select-method)
8992                 (not articles) t)       ; Accept form
8993           (not articles)))              ; Only save nov last time
8994         ;; Copy the article.
8995         ((eq action 'copy)
8996          (save-excursion
8997            (set-buffer copy-buf)
8998            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8999              (gnus-request-accept-article
9000               to-newsgroup select-method (not articles) t))))
9001         ;; Crosspost the article.
9002         ((eq action 'crosspost)
9003          (let ((xref (message-tokenize-header
9004                       (mail-header-xref (gnus-summary-article-header article))
9005                       " ")))
9006            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9007                                   ":" (number-to-string article)))
9008            (unless xref
9009              (setq xref (list (system-name))))
9010            (setq new-xref
9011                  (concat
9012                   (mapconcat 'identity
9013                              (delete "Xref:" (delete new-xref xref))
9014                              " ")
9015                   " " new-xref))
9016            (save-excursion
9017              (set-buffer copy-buf)
9018              ;; First put the article in the destination group.
9019              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9020              (when (consp (setq art-group
9021                                 (gnus-request-accept-article
9022                                  to-newsgroup select-method (not articles))))
9023                (setq new-xref (concat new-xref " " (car art-group)
9024                                       ":"
9025                                       (number-to-string (cdr art-group))))
9026                ;; Now we have the new Xrefs header, so we insert
9027                ;; it and replace the new article.
9028                (nnheader-replace-header "Xref" new-xref)
9029                (gnus-request-replace-article
9030                 (cdr art-group) to-newsgroup (current-buffer))
9031                art-group))))))
9032       (cond
9033        ((not art-group)
9034         (gnus-message 1 "Couldn't %s article %s: %s"
9035                       (cadr (assq action names)) article
9036                       (nnheader-get-report (car to-method))))
9037        ((eq art-group 'junk)
9038         (when (eq action 'move)
9039           (gnus-summary-mark-article article gnus-canceled-mark)
9040           (gnus-message 4 "Deleted article %s" article)
9041           ;; run the delete hook
9042           (run-hook-with-args 'gnus-summary-article-delete-hook
9043                               action
9044                               (gnus-data-header
9045                                (assoc article (gnus-data-list nil)))
9046                               gnus-newsgroup-name nil
9047                               select-method)))
9048        (t
9049         (let* ((pto-group (gnus-group-prefixed-name
9050                            (car art-group) to-method))
9051                (info (gnus-get-info pto-group))
9052                (to-group (gnus-info-group info))
9053                to-marks)
9054           ;; Update the group that has been moved to.
9055           (when (and info
9056                      (memq action '(move copy)))
9057             (unless (member to-group to-groups)
9058               (push to-group to-groups))
9059
9060             (unless (memq article gnus-newsgroup-unreads)
9061               (push 'read to-marks)
9062               (gnus-info-set-read
9063                info (gnus-add-to-range (gnus-info-read info)
9064                                        (list (cdr art-group)))))
9065
9066             ;; See whether the article is to be put in the cache.
9067             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9068                              gnus-article-mark-lists
9069                            (delete '(expirable . expire)
9070                                    (copy-sequence gnus-article-mark-lists))))
9071                   (to-article (cdr art-group)))
9072
9073               ;; Enter the article into the cache in the new group,
9074               ;; if that is required.
9075               (when gnus-use-cache
9076                 (gnus-cache-possibly-enter-article
9077                  to-group to-article
9078                  (memq article gnus-newsgroup-marked)
9079                  (memq article gnus-newsgroup-dormant)
9080                  (memq article gnus-newsgroup-unreads)))
9081
9082               (when gnus-preserve-marks
9083                 ;; Copy any marks over to the new group.
9084                 (when (and (equal to-group gnus-newsgroup-name)
9085                            (not (memq article gnus-newsgroup-unreads)))
9086                   ;; Mark this article as read in this group.
9087                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9088                   (setcdr (gnus-active to-group) to-article)
9089                   (setcdr gnus-newsgroup-active to-article))
9090
9091                 (while marks
9092                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9093                     (when (memq article (symbol-value
9094                                          (intern (format "gnus-newsgroup-%s"
9095                                                          (caar marks)))))
9096                       (push (cdar marks) to-marks)
9097                       ;; If the other group is the same as this group,
9098                       ;; then we have to add the mark to the list.
9099                       (when (equal to-group gnus-newsgroup-name)
9100                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9101                              (cons to-article
9102                                    (symbol-value
9103                                     (intern (format "gnus-newsgroup-%s"
9104                                                     (caar marks)))))))
9105                       ;; Copy the marks to other group.
9106                       (gnus-add-marked-articles
9107                        to-group (cdar marks) (list to-article) info)))
9108                   (setq marks (cdr marks)))
9109
9110                 (gnus-request-set-mark
9111                  to-group (list (list (list to-article) 'add to-marks))))
9112
9113               (gnus-dribble-enter
9114                (concat "(gnus-group-set-info '"
9115                        (gnus-prin1-to-string (gnus-get-info to-group))
9116                        ")"))))
9117
9118           ;; Update the Xref header in this article to point to
9119           ;; the new crossposted article we have just created.
9120           (when (eq action 'crosspost)
9121             (save-excursion
9122               (set-buffer copy-buf)
9123               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9124               (nnheader-replace-header "Xref" new-xref)
9125               (gnus-request-replace-article
9126                article gnus-newsgroup-name (current-buffer))))
9127
9128           ;; run the move/copy/crosspost/respool hook
9129           (run-hook-with-args 'gnus-summary-article-move-hook
9130                               action
9131                               (gnus-data-header
9132                                (assoc article (gnus-data-list nil)))
9133                               gnus-newsgroup-name
9134                               to-newsgroup
9135                               select-method))
9136
9137         ;;;!!!Why is this necessary?
9138         (set-buffer gnus-summary-buffer)
9139         
9140         (gnus-summary-goto-subject article)
9141         (when (eq action 'move)
9142           (gnus-summary-mark-article article gnus-canceled-mark))))
9143       (gnus-summary-remove-process-mark article))
9144     ;; Re-activate all groups that have been moved to.
9145     (save-excursion
9146       (set-buffer gnus-group-buffer)
9147       (let ((gnus-group-marked to-groups))
9148         (gnus-group-get-new-news-this-group nil t)))
9149
9150     (gnus-kill-buffer copy-buf)
9151     (gnus-summary-position-point)
9152     (gnus-set-mode-line 'summary)))
9153
9154 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9155   "Move the current article to a different newsgroup.
9156 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9157 When called interactively, if TO-NEWSGROUP is nil, use the value of
9158 the variable `gnus-move-split-methods' for finding a default target
9159 newsgroup.
9160 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9161 re-spool using this method."
9162   (interactive "P")
9163   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9164
9165 (defun gnus-summary-crosspost-article (&optional n)
9166   "Crosspost the current article to some other group."
9167   (interactive "P")
9168   (gnus-summary-move-article n nil nil 'crosspost))
9169
9170 (defcustom gnus-summary-respool-default-method nil
9171   "Default method type for respooling an article.
9172 If nil, use to the current newsgroup method."
9173   :type 'symbol
9174   :group 'gnus-summary-mail)
9175
9176 (defcustom gnus-summary-display-while-building nil
9177   "If non-nil, show and update the summary buffer as it's being built.
9178 If the value is t, update the buffer after every line is inserted.  If
9179 the value is an integer (N), update the display every N lines."
9180   :group 'gnus-thread
9181   :type '(choice (const :tag "off" nil)
9182                  number
9183                  (const :tag "frequently" t)))
9184
9185 (defun gnus-summary-respool-article (&optional n method)
9186   "Respool the current article.
9187 The article will be squeezed through the mail spooling process again,
9188 which means that it will be put in some mail newsgroup or other
9189 depending on `nnmail-split-methods'.
9190 If N is a positive number, respool the N next articles.
9191 If N is a negative number, respool the N previous articles.
9192 If N is nil and any articles have been marked with the process mark,
9193 respool those articles instead.
9194
9195 Respooling can be done both from mail groups and \"real\" newsgroups.
9196 In the former case, the articles in question will be moved from the
9197 current group into whatever groups they are destined to.  In the
9198 latter case, they will be copied into the relevant groups."
9199   (interactive
9200    (list current-prefix-arg
9201          (let* ((methods (gnus-methods-using 'respool))
9202                 (methname
9203                  (symbol-name (or gnus-summary-respool-default-method
9204                                   (car (gnus-find-method-for-group
9205                                         gnus-newsgroup-name)))))
9206                 (method
9207                  (gnus-completing-read-with-default
9208                   methname "What backend do you want to use when respooling?"
9209                   methods nil t nil 'gnus-mail-method-history))
9210                 ms)
9211            (cond
9212             ((zerop (length (setq ms (gnus-servers-using-backend
9213                                       (intern method)))))
9214              (list (intern method) ""))
9215             ((= 1 (length ms))
9216              (car ms))
9217             (t
9218              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9219                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9220                            ms-alist))))))))
9221   (unless method
9222     (error "No method given for respooling"))
9223   (if (assoc (symbol-name
9224               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9225              (gnus-methods-using 'respool))
9226       (gnus-summary-move-article n nil method)
9227     (gnus-summary-copy-article n nil method)))
9228
9229 (defun gnus-summary-import-article (file &optional edit)
9230   "Import an arbitrary file into a mail newsgroup."
9231   (interactive "fImport file: \nP")
9232   (let ((group gnus-newsgroup-name)
9233         (now (current-time))
9234         atts lines group-art)
9235     (unless (gnus-check-backend-function 'request-accept-article group)
9236       (error "%s does not support article importing" group))
9237     (or (file-readable-p file)
9238         (not (file-regular-p file))
9239         (error "Can't read %s" file))
9240     (save-excursion
9241       (set-buffer (gnus-get-buffer-create " *import file*"))
9242       (erase-buffer)
9243       (nnheader-insert-file-contents file)
9244       (goto-char (point-min))
9245       (if (nnheader-article-p)
9246           (save-restriction
9247             (goto-char (point-min))
9248             (search-forward "\n\n" nil t)
9249             (narrow-to-region (point-min) (1- (point)))
9250             (goto-char (point-min))
9251             (unless (re-search-forward "^date:" nil t)
9252               (goto-char (point-max))
9253               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9254        ;; This doesn't look like an article, so we fudge some headers.
9255         (setq atts (file-attributes file)
9256               lines (count-lines (point-min) (point-max)))
9257         (insert "From: " (read-string "From: ") "\n"
9258                 "Subject: " (read-string "Subject: ") "\n"
9259                 "Date: " (message-make-date (nth 5 atts)) "\n"
9260                 "Message-ID: " (message-make-message-id) "\n"
9261                 "Lines: " (int-to-string lines) "\n"
9262                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9263       (setq group-art (gnus-request-accept-article group nil t))
9264       (kill-buffer (current-buffer)))
9265     (setq gnus-newsgroup-active (gnus-activate-group group))
9266     (forward-line 1)
9267     (gnus-summary-goto-article (cdr group-art) nil t)
9268     (when edit
9269       (gnus-summary-edit-article))))
9270
9271 (defun gnus-summary-create-article ()
9272   "Create an article in a mail newsgroup."
9273   (interactive)
9274   (let ((group gnus-newsgroup-name)
9275         (now (current-time))
9276         group-art)
9277     (unless (gnus-check-backend-function 'request-accept-article group)
9278       (error "%s does not support article importing" group))
9279     (save-excursion
9280       (set-buffer (gnus-get-buffer-create " *import file*"))
9281       (erase-buffer)
9282       (goto-char (point-min))
9283       ;; This doesn't look like an article, so we fudge some headers.
9284       (insert "From: " (read-string "From: ") "\n"
9285               "Subject: " (read-string "Subject: ") "\n"
9286               "Date: " (message-make-date now) "\n"
9287               "Message-ID: " (message-make-message-id) "\n")
9288       (setq group-art (gnus-request-accept-article group nil t))
9289       (kill-buffer (current-buffer)))
9290     (setq gnus-newsgroup-active (gnus-activate-group group))
9291     (forward-line 1)
9292     (gnus-summary-goto-article (cdr group-art) nil t)
9293     (gnus-summary-edit-article)))
9294
9295 (defun gnus-summary-article-posted-p ()
9296   "Say whether the current (mail) article is available from news as well.
9297 This will be the case if the article has both been mailed and posted."
9298   (interactive)
9299   (let ((id (mail-header-references (gnus-summary-article-header)))
9300         (gnus-override-method (car (gnus-refer-article-methods))))
9301     (if (gnus-request-head id "")
9302         (gnus-message 2 "The current message was found on %s"
9303                       gnus-override-method)
9304       (gnus-message 2 "The current message couldn't be found on %s"
9305                     gnus-override-method)
9306       nil)))
9307
9308 (defun gnus-summary-expire-articles (&optional now)
9309   "Expire all articles that are marked as expirable in the current group."
9310   (interactive)
9311   (when (and (not gnus-group-is-exiting-without-update-p)
9312              (gnus-check-backend-function
9313               'request-expire-articles gnus-newsgroup-name))
9314     ;; This backend supports expiry.
9315     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9316            (expirable (if total
9317                           (progn
9318                             ;; We need to update the info for
9319                             ;; this group for `gnus-list-of-read-articles'
9320                             ;; to give us the right answer.
9321                             (gnus-run-hooks 'gnus-exit-group-hook)
9322                             (gnus-summary-update-info)
9323                             (gnus-list-of-read-articles gnus-newsgroup-name))
9324                         (setq gnus-newsgroup-expirable
9325                               (sort gnus-newsgroup-expirable '<))))
9326            (expiry-wait (if now 'immediate
9327                           (gnus-group-find-parameter
9328                            gnus-newsgroup-name 'expiry-wait)))
9329            (nnmail-expiry-target
9330             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9331                 nnmail-expiry-target))
9332            es)
9333       (when expirable
9334         ;; There are expirable articles in this group, so we run them
9335         ;; through the expiry process.
9336         (gnus-message 6 "Expiring articles...")
9337         (unless (gnus-check-group gnus-newsgroup-name)
9338           (error "Can't open server for %s" gnus-newsgroup-name))
9339         ;; The list of articles that weren't expired is returned.
9340         (save-excursion
9341           (if expiry-wait
9342               (let ((nnmail-expiry-wait-function nil)
9343                     (nnmail-expiry-wait expiry-wait))
9344                 (setq es (gnus-request-expire-articles
9345                           expirable gnus-newsgroup-name)))
9346             (setq es (gnus-request-expire-articles
9347                       expirable gnus-newsgroup-name)))
9348           (unless total
9349             (setq gnus-newsgroup-expirable es))
9350           ;; We go through the old list of expirable, and mark all
9351           ;; really expired articles as nonexistent.
9352           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9353             (let ((gnus-use-cache nil))
9354               (dolist (article expirable)
9355                 (when (and (not (memq article es))
9356                            (gnus-data-find article))
9357                   (gnus-summary-mark-article article gnus-canceled-mark)
9358                   (run-hook-with-args 'gnus-summary-article-expire-hook
9359                                       'delete
9360                                       (gnus-data-header
9361                                        (assoc article (gnus-data-list nil)))
9362                                       gnus-newsgroup-name
9363                                       nil
9364                                       nil))))))
9365         (gnus-message 6 "Expiring articles...done")))))
9366
9367 (defun gnus-summary-expire-articles-now ()
9368   "Expunge all expirable articles in the current group.
9369 This means that *all* articles that are marked as expirable will be
9370 deleted forever, right now."
9371   (interactive)
9372   (or gnus-expert-user
9373       (gnus-yes-or-no-p
9374        "Are you really, really, really sure you want to delete all these messages? ")
9375       (error "Phew!"))
9376   (gnus-summary-expire-articles t))
9377
9378 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9379 (defun gnus-summary-delete-article (&optional n)
9380   "Delete the N next (mail) articles.
9381 This command actually deletes articles.  This is not a marking
9382 command.  The article will disappear forever from your life, never to
9383 return.
9384
9385 If N is negative, delete backwards.
9386 If N is nil and articles have been marked with the process mark,
9387 delete these instead.
9388
9389 If `gnus-novice-user' is non-nil you will be asked for
9390 confirmation before the articles are deleted."
9391   (interactive "P")
9392   (unless (gnus-check-backend-function 'request-expire-articles
9393                                        gnus-newsgroup-name)
9394     (error "The current newsgroup does not support article deletion"))
9395   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9396     (error "Couldn't open server"))
9397   ;; Compute the list of articles to delete.
9398   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9399         (nnmail-expiry-target 'delete)
9400         not-deleted)
9401     (if (and gnus-novice-user
9402              (not (gnus-yes-or-no-p
9403                    (format "Do you really want to delete %s forever? "
9404                            (if (> (length articles) 1)
9405                                (format "these %s articles" (length articles))
9406                              "this article")))))
9407         ()
9408       ;; Delete the articles.
9409       (setq not-deleted (gnus-request-expire-articles
9410                          articles gnus-newsgroup-name 'force))
9411       (while articles
9412         (gnus-summary-remove-process-mark (car articles))
9413         ;; The backend might not have been able to delete the article
9414         ;; after all.
9415         (unless (memq (car articles) not-deleted)
9416           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9417         (let* ((article (car articles))
9418                (id (mail-header-id (gnus-data-header
9419                                     (assoc article (gnus-data-list nil))))))
9420           (run-hook-with-args 'gnus-summary-article-delete-hook
9421                               'delete id gnus-newsgroup-name nil
9422                               nil))
9423         (setq articles (cdr articles)))
9424       (when not-deleted
9425         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9426     (gnus-summary-position-point)
9427     (gnus-set-mode-line 'summary)
9428     not-deleted))
9429
9430 (defun gnus-summary-edit-article (&optional arg)
9431   "Edit the current article.
9432 This will have permanent effect only in mail groups.
9433 If ARG is nil, edit the decoded articles.
9434 If ARG is 1, edit the raw articles.
9435 If ARG is 2, edit the raw articles even in read-only groups.
9436 If ARG is 3, edit the articles with the current handles.
9437 Otherwise, allow editing of articles even in read-only
9438 groups."
9439   (interactive "P")
9440   (let (force raw current-handles)
9441     (cond
9442      ((null arg))
9443      ((eq arg 1)
9444       (setq raw t))
9445      ((eq arg 2)
9446       (setq raw t
9447             force t))
9448      ((eq arg 3)
9449       (setq current-handles
9450             (and (gnus-buffer-live-p gnus-article-buffer)
9451                  (with-current-buffer gnus-article-buffer
9452                    (prog1
9453                        gnus-article-mime-handles
9454                      (setq gnus-article-mime-handles nil))))))
9455      (t
9456       (setq force t)))
9457     (when (and raw (not force)
9458                (member gnus-newsgroup-name '("nndraft:delayed"
9459                                              "nndraft:drafts"
9460                                              "nndraft:queue")))
9461       (error "Can't edit the raw article in group %s"
9462              gnus-newsgroup-name))
9463     (save-excursion
9464       (set-buffer gnus-summary-buffer)
9465       (let ((mail-parse-charset gnus-newsgroup-charset)
9466             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9467         (gnus-set-global-variables)
9468         (when (and (not force)
9469                    (gnus-group-read-only-p))
9470           (error "The current newsgroup does not support article editing"))
9471         (gnus-summary-show-article t)
9472         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9473           (with-current-buffer gnus-article-buffer
9474             (mm-enable-multibyte)))
9475         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9476             (setq raw t))
9477         (gnus-article-edit-article
9478          (if raw 'ignore
9479            `(lambda ()
9480               (let ((mbl mml-buffer-list))
9481                 (setq mml-buffer-list nil)
9482                 (mime-to-mml ,'current-handles)
9483                 (let ((mbl1 mml-buffer-list))
9484                   (setq mml-buffer-list mbl)
9485                   (set (make-local-variable 'mml-buffer-list) mbl1))
9486                 (gnus-make-local-hook 'kill-buffer-hook)
9487                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9488          `(lambda (no-highlight)
9489             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9490                   (message-options message-options)
9491                   (message-options-set-recipient)
9492                   (mail-parse-ignored-charsets
9493                    ',gnus-newsgroup-ignored-charsets))
9494               ,(if (not raw) '(progn
9495                                 (mml-to-mime)
9496                                 (mml-destroy-buffers)
9497                                 (remove-hook 'kill-buffer-hook
9498                                              'mml-destroy-buffers t)
9499                                 (kill-local-variable 'mml-buffer-list)))
9500               (gnus-summary-edit-article-done
9501                ,(or (mail-header-references gnus-current-headers) "")
9502                ,(gnus-group-read-only-p)
9503                ,gnus-summary-buffer no-highlight))))))))
9504
9505 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9506
9507 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9508                                                  no-highlight)
9509   "Make edits to the current article permanent."
9510   (interactive)
9511   (save-excursion
9512    ;; The buffer restriction contains the entire article if it exists.
9513     (when (article-goto-body)
9514       (let ((lines (count-lines (point) (point-max)))
9515             (length (- (point-max) (point)))
9516             (case-fold-search t)
9517             (body (copy-marker (point))))
9518         (goto-char (point-min))
9519         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9520           (delete-region (match-beginning 1) (match-end 1))
9521           (insert (number-to-string length)))
9522         (goto-char (point-min))
9523         (when (re-search-forward
9524                "^x-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 "^lines:[ \t]\\([0-9]+\\)" body t)
9529           (delete-region (match-beginning 1) (match-end 1))
9530           (insert (number-to-string lines))))))
9531   ;; Replace the article.
9532   (let ((buf (current-buffer)))
9533     (with-temp-buffer
9534       (insert-buffer-substring buf)
9535
9536       (if (and (not read-only)
9537                (not (gnus-request-replace-article
9538                      (cdr gnus-article-current) (car gnus-article-current)
9539                      (current-buffer) t)))
9540           (error "Couldn't replace article")
9541         ;; Update the summary buffer.
9542         (if (and references
9543                  (equal (message-tokenize-header references " ")
9544                         (message-tokenize-header
9545                          (or (message-fetch-field "references") "") " ")))
9546             ;; We only have to update this line.
9547             (save-excursion
9548               (save-restriction
9549                 (message-narrow-to-head)
9550                 (let ((head (buffer-string))
9551                       header)
9552                   (with-temp-buffer
9553                     (insert (format "211 %d Article retrieved.\n"
9554                                     (cdr gnus-article-current)))
9555                     (insert head)
9556                     (insert ".\n")
9557                     (let ((nntp-server-buffer (current-buffer)))
9558                       (setq header (car (gnus-get-newsgroup-headers
9559                                          nil t))))
9560                     (save-excursion
9561                       (set-buffer gnus-summary-buffer)
9562                       (gnus-data-set-header
9563                        (gnus-data-find (cdr gnus-article-current))
9564                        header)
9565                       (gnus-summary-update-article-line
9566                        (cdr gnus-article-current) header)
9567                       (if (gnus-summary-goto-subject
9568                            (cdr gnus-article-current) nil t)
9569                           (gnus-summary-update-secondary-mark
9570                            (cdr gnus-article-current))))))))
9571           ;; Update threads.
9572           (set-buffer (or buffer gnus-summary-buffer))
9573           (gnus-summary-update-article (cdr gnus-article-current))
9574           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9575               (gnus-summary-update-secondary-mark
9576                (cdr gnus-article-current))))
9577         ;; Prettify the article buffer again.
9578         (unless no-highlight
9579           (save-excursion
9580             (set-buffer gnus-article-buffer)
9581             ;;;!!! Fix this -- article should be rehighlighted.
9582             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9583             (set-buffer gnus-original-article-buffer)
9584             (gnus-request-article
9585              (cdr gnus-article-current)
9586              (car gnus-article-current) (current-buffer))))
9587         ;; Prettify the summary buffer line.
9588         (when (gnus-visual-p 'summary-highlight 'highlight)
9589           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9590
9591 (defun gnus-summary-edit-wash (key)
9592   "Perform editing command KEY in the article buffer."
9593   (interactive
9594    (list
9595     (progn
9596       (message "%s" (concat (this-command-keys) "- "))
9597       (read-char))))
9598   (message "")
9599   (gnus-summary-edit-article)
9600   (execute-kbd-macro (concat (this-command-keys) key))
9601   (gnus-article-edit-done))
9602
9603 ;;; Respooling
9604
9605 (defun gnus-summary-respool-query (&optional silent trace)
9606   "Query where the respool algorithm would put this article."
9607   (interactive)
9608   (let (gnus-mark-article-hook)
9609     (gnus-summary-select-article)
9610     (save-excursion
9611       (set-buffer gnus-original-article-buffer)
9612       (let ((groups (nnmail-article-group 'identity trace)))
9613         (unless silent
9614           (if groups
9615               (message "This message would go to %s"
9616                        (mapconcat 'car groups ", "))
9617             (message "This message would go to no groups"))
9618           groups)))))
9619
9620 (defun gnus-summary-respool-trace ()
9621   "Trace where the respool algorithm would put this article.
9622 Display a buffer showing all fancy splitting patterns which matched."
9623   (interactive)
9624   (gnus-summary-respool-query nil t))
9625
9626 ;; Summary marking commands.
9627
9628 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9629   "Mark articles which has the same subject as read, and then select the next.
9630 If UNMARK is positive, remove any kind of mark.
9631 If UNMARK is negative, tick articles."
9632   (interactive "P")
9633   (when unmark
9634     (setq unmark (prefix-numeric-value unmark)))
9635   (let ((count
9636          (gnus-summary-mark-same-subject
9637           (gnus-summary-article-subject) unmark)))
9638     ;; Select next unread article.  If auto-select-same mode, should
9639     ;; select the first unread article.
9640     (gnus-summary-next-article t (and gnus-auto-select-same
9641                                       (gnus-summary-article-subject)))
9642     (gnus-message 7 "%d article%s marked as %s"
9643                   count (if (= count 1) " is" "s are")
9644                   (if unmark "unread" "read"))))
9645
9646 (defun gnus-summary-kill-same-subject (&optional unmark)
9647   "Mark articles which has the same subject as read.
9648 If UNMARK is positive, remove any kind of mark.
9649 If UNMARK is negative, tick articles."
9650   (interactive "P")
9651   (when unmark
9652     (setq unmark (prefix-numeric-value unmark)))
9653   (let ((count
9654          (gnus-summary-mark-same-subject
9655           (gnus-summary-article-subject) unmark)))
9656     ;; If marked as read, go to next unread subject.
9657     (when (null unmark)
9658       ;; Go to next unread subject.
9659       (gnus-summary-next-subject 1 t))
9660     (gnus-message 7 "%d articles are marked as %s"
9661                   count (if unmark "unread" "read"))))
9662
9663 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9664   "Mark articles with same SUBJECT as read, and return marked number.
9665 If optional argument UNMARK is positive, remove any kinds of marks.
9666 If optional argument UNMARK is negative, mark articles as unread instead."
9667   (let ((count 1))
9668     (save-excursion
9669       (cond
9670        ((null unmark)                   ; Mark as read.
9671         (while (and
9672                 (progn
9673                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9674                   (gnus-summary-show-thread) t)
9675                 (gnus-summary-find-subject subject))
9676           (setq count (1+ count))))
9677        ((> unmark 0)                    ; Tick.
9678         (while (and
9679                 (progn
9680                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9681                   (gnus-summary-show-thread) t)
9682                 (gnus-summary-find-subject subject))
9683           (setq count (1+ count))))
9684        (t                               ; Mark as unread.
9685         (while (and
9686                 (progn
9687                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9688                   (gnus-summary-show-thread) t)
9689                 (gnus-summary-find-subject subject))
9690           (setq count (1+ count)))))
9691       (gnus-set-mode-line 'summary)
9692       ;; Return the number of marked articles.
9693       count)))
9694
9695 (defun gnus-summary-mark-as-processable (n &optional unmark)
9696   "Set the process mark on the next N articles.
9697 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9698 the process mark instead.  The difference between N and the actual
9699 number of articles marked is returned."
9700   (interactive "P")
9701   (if (and (null n) (gnus-region-active-p))
9702       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9703     (setq n (prefix-numeric-value n))
9704     (let ((backward (< n 0))
9705           (n (abs n)))
9706       (while (and
9707               (> n 0)
9708               (if unmark
9709                   (gnus-summary-remove-process-mark
9710                    (gnus-summary-article-number))
9711                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9712               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9713         (setq n (1- n)))
9714       (when (/= 0 n)
9715         (gnus-message 7 "No more articles"))
9716       (gnus-summary-recenter)
9717       (gnus-summary-position-point)
9718       n)))
9719
9720 (defun gnus-summary-unmark-as-processable (n)
9721   "Remove the process mark from the next N articles.
9722 If N is negative, unmark backward instead.  The difference between N and
9723 the actual number of articles unmarked is returned."
9724   (interactive "P")
9725   (gnus-summary-mark-as-processable n t))
9726
9727 (defun gnus-summary-unmark-all-processable ()
9728   "Remove the process mark from all articles."
9729   (interactive)
9730   (save-excursion
9731     (while gnus-newsgroup-processable
9732       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9733   (gnus-summary-position-point))
9734
9735 (defun gnus-summary-add-mark (article type)
9736   "Mark ARTICLE with a mark of TYPE."
9737   (let ((vtype (car (assq type gnus-article-mark-lists)))
9738         var)
9739     (if (not vtype)
9740         (error "No such mark type: %s" type)
9741       (setq var (intern (format "gnus-newsgroup-%s" type)))
9742       (set var (cons article (symbol-value var)))
9743       (if (memq type '(processable cached replied forwarded recent saved))
9744           (gnus-summary-update-secondary-mark article)
9745         ;;; !!! This is bogus.  We should find out what primary
9746         ;;; !!! mark we want to set.
9747         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9748
9749 (defun gnus-summary-mark-as-expirable (n)
9750   "Mark N articles forward as expirable.
9751 If N is negative, mark backward instead.  The difference between N and
9752 the actual number of articles marked is returned."
9753   (interactive "p")
9754   (gnus-summary-mark-forward n gnus-expirable-mark))
9755
9756 (defun gnus-summary-mark-as-spam (n)
9757   "Mark N articles forward as spam.
9758 If N is negative, mark backward instead.  The difference between N and
9759 the actual number of articles marked is returned."
9760   (interactive "p")
9761   (gnus-summary-mark-forward n gnus-spam-mark))
9762
9763 (defun gnus-summary-mark-article-as-replied (article)
9764   "Mark ARTICLE as replied to and update the summary line.
9765 ARTICLE can also be a list of articles."
9766   (interactive (list (gnus-summary-article-number)))
9767   (let ((articles (if (listp article) article (list article))))
9768     (dolist (article articles)
9769       (unless (numberp article)
9770         (error "%s is not a number" article))
9771       (push article gnus-newsgroup-replied)
9772       (let ((buffer-read-only nil))
9773         (when (gnus-summary-goto-subject article nil t)
9774           (gnus-summary-update-secondary-mark article))))))
9775
9776 (defun gnus-summary-mark-article-as-forwarded (article)
9777   "Mark ARTICLE as forwarded and update the summary line.
9778 ARTICLE can also be a list of articles."
9779   (let ((articles (if (listp article) article (list article))))
9780     (dolist (article articles)
9781       (push article gnus-newsgroup-forwarded)
9782       (let ((buffer-read-only nil))
9783         (when (gnus-summary-goto-subject article nil t)
9784           (gnus-summary-update-secondary-mark article))))))
9785
9786 (defun gnus-summary-set-bookmark (article)
9787   "Set a bookmark in current article."
9788   (interactive (list (gnus-summary-article-number)))
9789   (when (or (not (get-buffer gnus-article-buffer))
9790             (not gnus-current-article)
9791             (not gnus-article-current)
9792             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9793     (error "No current article selected"))
9794   ;; Remove old bookmark, if one exists.
9795   (gnus-pull article gnus-newsgroup-bookmarks)
9796   ;; Set the new bookmark, which is on the form
9797   ;; (article-number . line-number-in-body).
9798   (push
9799    (cons article
9800          (with-current-buffer gnus-article-buffer
9801            (count-lines
9802             (min (point)
9803                  (save-excursion
9804                    (article-goto-body)
9805                    (point)))
9806             (point))))
9807    gnus-newsgroup-bookmarks)
9808   (gnus-message 6 "A bookmark has been added to the current article."))
9809
9810 (defun gnus-summary-remove-bookmark (article)
9811   "Remove the bookmark from the current article."
9812   (interactive (list (gnus-summary-article-number)))
9813   ;; Remove old bookmark, if one exists.
9814   (if (not (assq article gnus-newsgroup-bookmarks))
9815       (gnus-message 6 "No bookmark in current article.")
9816     (gnus-pull article gnus-newsgroup-bookmarks)
9817     (gnus-message 6 "Removed bookmark.")))
9818
9819 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9820 (defun gnus-summary-mark-as-dormant (n)
9821   "Mark N articles forward as dormant.
9822 If N is negative, mark backward instead.  The difference between N and
9823 the actual number of articles marked is returned."
9824   (interactive "p")
9825   (gnus-summary-mark-forward n gnus-dormant-mark))
9826
9827 (defun gnus-summary-set-process-mark (article)
9828   "Set the process mark on ARTICLE and update the summary line."
9829   (setq gnus-newsgroup-processable
9830         (cons article
9831               (delq article gnus-newsgroup-processable)))
9832   (when (gnus-summary-goto-subject article)
9833     (gnus-summary-show-thread)
9834     (gnus-summary-goto-subject article)
9835     (gnus-summary-update-secondary-mark article)))
9836
9837 (defun gnus-summary-remove-process-mark (article)
9838   "Remove the process mark from ARTICLE and update the summary line."
9839   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9840   (when (gnus-summary-goto-subject article)
9841     (gnus-summary-show-thread)
9842     (gnus-summary-goto-subject article)
9843     (gnus-summary-update-secondary-mark article)))
9844
9845 (defun gnus-summary-set-saved-mark (article)
9846   "Set the process mark on ARTICLE and update the summary line."
9847   (push article gnus-newsgroup-saved)
9848   (when (gnus-summary-goto-subject article)
9849     (gnus-summary-update-secondary-mark article)))
9850
9851 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9852   "Mark N articles as read forwards.
9853 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9854 The difference between N and the actual number of articles marked is
9855 returned.
9856 If NO-EXPIRE, auto-expiry will be inhibited."
9857   (interactive "p")
9858   (gnus-summary-show-thread)
9859   (let ((backward (< n 0))
9860         (gnus-summary-goto-unread
9861          (and gnus-summary-goto-unread
9862               (not (eq gnus-summary-goto-unread 'never))
9863               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9864                                     gnus-ticked-mark gnus-dormant-mark)))))
9865         (n (abs n))
9866         (mark (or mark gnus-del-mark)))
9867     (while (and (> n 0)
9868                 (gnus-summary-mark-article nil mark no-expire)
9869                 (zerop (gnus-summary-next-subject
9870                         (if backward -1 1)
9871                         (and gnus-summary-goto-unread
9872                              (not (eq gnus-summary-goto-unread 'never)))
9873                         t)))
9874       (setq n (1- n)))
9875     (when (/= 0 n)
9876       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9877     (gnus-summary-recenter)
9878     (gnus-summary-position-point)
9879     (gnus-set-mode-line 'summary)
9880     n))
9881
9882 (defun gnus-summary-mark-article-as-read (mark)
9883   "Mark the current article quickly as read with MARK."
9884   (let ((article (gnus-summary-article-number)))
9885     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9886     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9887     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9888     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9889     (push (cons article mark) gnus-newsgroup-reads)
9890     ;; Possibly remove from cache, if that is used.
9891     (when gnus-use-cache
9892       (gnus-cache-enter-remove-article article))
9893     ;; Allow the backend to change the mark.
9894     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9895     ;; Check for auto-expiry.
9896     (when (and gnus-newsgroup-auto-expire
9897                (memq mark gnus-auto-expirable-marks))
9898       (setq mark gnus-expirable-mark)
9899       ;; Let the backend know about the mark change.
9900       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9901       (push article gnus-newsgroup-expirable))
9902     ;; Set the mark in the buffer.
9903     (gnus-summary-update-mark mark 'unread)
9904     t))
9905
9906 (defun gnus-summary-mark-article-as-unread (mark)
9907   "Mark the current article quickly as unread with MARK."
9908   (let* ((article (gnus-summary-article-number))
9909          (old-mark (gnus-summary-article-mark article)))
9910     ;; Allow the backend to change the mark.
9911     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9912     (if (eq mark old-mark)
9913         t
9914       (if (<= article 0)
9915           (progn
9916             (gnus-error 1 "Can't mark negative article numbers")
9917             nil)
9918         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9919         (setq gnus-newsgroup-spam-marked
9920               (delq article gnus-newsgroup-spam-marked))
9921         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9922         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9923         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9924         (cond ((= mark gnus-ticked-mark)
9925                (setq gnus-newsgroup-marked
9926                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9927                                               article)))
9928               ((= mark gnus-spam-mark)
9929                (setq gnus-newsgroup-spam-marked
9930                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9931                                               article)))
9932               ((= mark gnus-dormant-mark)
9933                (setq gnus-newsgroup-dormant
9934                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9935                                               article)))
9936               (t
9937                (setq gnus-newsgroup-unreads
9938                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9939                                               article))))
9940         (gnus-pull article gnus-newsgroup-reads)
9941
9942         ;; See whether the article is to be put in the cache.
9943         (and gnus-use-cache
9944              (vectorp (gnus-summary-article-header article))
9945              (save-excursion
9946                (gnus-cache-possibly-enter-article
9947                 gnus-newsgroup-name article
9948                 (= mark gnus-ticked-mark)
9949                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9950
9951         ;; Fix the mark.
9952         (gnus-summary-update-mark mark 'unread)
9953         t))))
9954
9955 (defun gnus-summary-mark-article (&optional article mark no-expire)
9956   "Mark ARTICLE with MARK.  MARK can be any character.
9957 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9958 `??' (dormant) and `?E' (expirable).
9959 If MARK is nil, then the default character `?r' is used.
9960 If ARTICLE is nil, then the article on the current line will be
9961 marked.
9962 If NO-EXPIRE, auto-expiry will be inhibited."
9963   ;; The mark might be a string.
9964   (when (stringp mark)
9965     (setq mark (aref mark 0)))
9966   ;; If no mark is given, then we check auto-expiring.
9967   (when (null mark)
9968     (setq mark gnus-del-mark))
9969   (when (and (not no-expire)
9970              gnus-newsgroup-auto-expire
9971              (memq mark gnus-auto-expirable-marks))
9972     (setq mark gnus-expirable-mark))
9973   (let ((article (or article (gnus-summary-article-number)))
9974         (old-mark (gnus-summary-article-mark article)))
9975     ;; Allow the backend to change the mark.
9976     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9977     (if (eq mark old-mark)
9978         t
9979       (unless article
9980         (error "No article on current line"))
9981       (if (not (if (or (= mark gnus-unread-mark)
9982                        (= mark gnus-ticked-mark)
9983                        (= mark gnus-spam-mark)
9984                        (= mark gnus-dormant-mark))
9985                    (gnus-mark-article-as-unread article mark)
9986                  (gnus-mark-article-as-read article mark)))
9987           t
9988         ;; See whether the article is to be put in the cache.
9989         (and gnus-use-cache
9990              (not (= mark gnus-canceled-mark))
9991              (vectorp (gnus-summary-article-header article))
9992              (save-excursion
9993                (gnus-cache-possibly-enter-article
9994                 gnus-newsgroup-name article
9995                 (= mark gnus-ticked-mark)
9996                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9997
9998         (when (gnus-summary-goto-subject article nil t)
9999           (let ((buffer-read-only nil))
10000             (gnus-summary-show-thread)
10001             ;; Fix the mark.
10002             (gnus-summary-update-mark mark 'unread)
10003             t))))))
10004
10005 (defun gnus-summary-update-secondary-mark (article)
10006   "Update the secondary (read, process, cache) mark."
10007   (gnus-summary-update-mark
10008    (cond ((memq article gnus-newsgroup-processable)
10009           gnus-process-mark)
10010          ((memq article gnus-newsgroup-cached)
10011           gnus-cached-mark)
10012          ((memq article gnus-newsgroup-replied)
10013           gnus-replied-mark)
10014          ((memq article gnus-newsgroup-forwarded)
10015           gnus-forwarded-mark)
10016          ((memq article gnus-newsgroup-saved)
10017           gnus-saved-mark)
10018          ((memq article gnus-newsgroup-recent)
10019           gnus-recent-mark)
10020          ((memq article gnus-newsgroup-unseen)
10021           gnus-unseen-mark)
10022          (t gnus-no-mark))
10023    'replied)
10024   (when (gnus-visual-p 'summary-highlight 'highlight)
10025     (gnus-run-hooks 'gnus-summary-update-hook))
10026   t)
10027
10028 (defun gnus-summary-update-download-mark (article)
10029   "Update the download mark."
10030   (gnus-summary-update-mark
10031    (cond ((memq article gnus-newsgroup-undownloaded)
10032           gnus-undownloaded-mark)
10033          (gnus-newsgroup-agentized
10034           gnus-downloaded-mark)
10035          (t
10036           gnus-no-mark))
10037    'download)
10038   (gnus-summary-update-line t)
10039   t)
10040
10041 (defun gnus-summary-update-mark (mark type)
10042   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10043         (buffer-read-only nil))
10044     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10045     (when forward
10046       (when (looking-at "\r")
10047         (incf forward))
10048       (when (<= (+ forward (point)) (point-max))
10049         ;; Go to the right position on the line.
10050         (goto-char (+ forward (point)))
10051         ;; Replace the old mark with the new mark.
10052         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10053         ;; Optionally update the marks by some user rule.
10054         (when (eq type 'unread)
10055           (gnus-data-set-mark
10056            (gnus-data-find (gnus-summary-article-number)) mark)
10057           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10058
10059 (defun gnus-mark-article-as-read (article &optional mark)
10060   "Enter ARTICLE in the pertinent lists and remove it from others."
10061   ;; Make the article expirable.
10062   (let ((mark (or mark gnus-del-mark)))
10063     (setq gnus-newsgroup-expirable
10064           (if (= mark gnus-expirable-mark)
10065               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10066             (delq article gnus-newsgroup-expirable)))
10067     ;; Remove from unread and marked lists.
10068     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10069     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10070     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10071     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10072     (push (cons article mark) gnus-newsgroup-reads)
10073     ;; Possibly remove from cache, if that is used.
10074     (when gnus-use-cache
10075       (gnus-cache-enter-remove-article article))
10076     t))
10077
10078 (defun gnus-mark-article-as-unread (article &optional mark)
10079   "Enter ARTICLE in the pertinent lists and remove it from others."
10080   (let ((mark (or mark gnus-ticked-mark)))
10081     (if (<= article 0)
10082         (progn
10083           (gnus-error 1 "Can't mark negative article numbers")
10084           nil)
10085       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10086             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10087             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10088             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10089             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10090
10091       ;; Unsuppress duplicates?
10092       (when gnus-suppress-duplicates
10093         (gnus-dup-unsuppress-article article))
10094
10095       (cond ((= mark gnus-ticked-mark)
10096              (setq gnus-newsgroup-marked
10097                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10098             ((= mark gnus-spam-mark)
10099              (setq gnus-newsgroup-spam-marked
10100                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10101                                             article)))
10102             ((= mark gnus-dormant-mark)
10103              (setq gnus-newsgroup-dormant
10104                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10105             (t
10106              (setq gnus-newsgroup-unreads
10107                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10108       (gnus-pull article gnus-newsgroup-reads)
10109       t)))
10110
10111 (defalias 'gnus-summary-mark-as-unread-forward
10112   'gnus-summary-tick-article-forward)
10113 (make-obsolete 'gnus-summary-mark-as-unread-forward
10114                'gnus-summary-tick-article-forward)
10115 (defun gnus-summary-tick-article-forward (n)
10116   "Tick N articles forwards.
10117 If N is negative, tick backwards instead.
10118 The difference between N and the number of articles ticked is returned."
10119   (interactive "p")
10120   (gnus-summary-mark-forward n gnus-ticked-mark))
10121
10122 (defalias 'gnus-summary-mark-as-unread-backward
10123   'gnus-summary-tick-article-backward)
10124 (make-obsolete 'gnus-summary-mark-as-unread-backward
10125                'gnus-summary-tick-article-backward)
10126 (defun gnus-summary-tick-article-backward (n)
10127   "Tick N articles backwards.
10128 The difference between N and the number of articles ticked is returned."
10129   (interactive "p")
10130   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10131
10132 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10133 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10134 (defun gnus-summary-tick-article (&optional article clear-mark)
10135   "Mark current article as unread.
10136 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10137 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10138   (interactive)
10139   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10140                                        gnus-ticked-mark)))
10141
10142 (defun gnus-summary-mark-as-read-forward (n)
10143   "Mark N articles as read forwards.
10144 If N is negative, mark backwards instead.
10145 The difference between N and the actual number of articles marked is
10146 returned."
10147   (interactive "p")
10148   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10149
10150 (defun gnus-summary-mark-as-read-backward (n)
10151   "Mark the N articles as read backwards.
10152 The difference between N and the actual number of articles marked is
10153 returned."
10154   (interactive "p")
10155   (gnus-summary-mark-forward
10156    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10157
10158 (defun gnus-summary-mark-as-read (&optional article mark)
10159   "Mark current article as read.
10160 ARTICLE specifies the article to be marked as read.
10161 MARK specifies a string to be inserted at the beginning of the line."
10162   (gnus-summary-mark-article article mark))
10163
10164 (defun gnus-summary-clear-mark-forward (n)
10165   "Clear marks from N articles forward.
10166 If N is negative, clear backward instead.
10167 The difference between N and the number of marks cleared is returned."
10168   (interactive "p")
10169   (gnus-summary-mark-forward n gnus-unread-mark))
10170
10171 (defun gnus-summary-clear-mark-backward (n)
10172   "Clear marks from N articles backward.
10173 The difference between N and the number of marks cleared is returned."
10174   (interactive "p")
10175   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10176
10177 (defun gnus-summary-mark-unread-as-read ()
10178   "Intended to be used by `gnus-summary-mark-article-hook'."
10179   (when (memq gnus-current-article gnus-newsgroup-unreads)
10180     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10181
10182 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10183   "Intended to be used by `gnus-summary-mark-article-hook'."
10184   (let ((mark (gnus-summary-article-mark)))
10185     (when (or (gnus-unread-mark-p mark)
10186               (gnus-read-mark-p mark))
10187       (gnus-summary-mark-article gnus-current-article
10188                                  (or new-mark gnus-read-mark)))))
10189
10190 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10191   "Intended to be used by `gnus-summary-mark-article-hook'."
10192   (let ((mark (gnus-summary-article-mark)))
10193     (when (or (gnus-unread-mark-p mark)
10194               (gnus-read-mark-p mark))
10195       (gnus-summary-mark-article (gnus-summary-article-number)
10196                                  (or new-mark gnus-read-mark)))))
10197
10198 (defun gnus-summary-mark-unread-as-ticked ()
10199   "Intended to be used by `gnus-summary-mark-article-hook'."
10200   (when (memq gnus-current-article gnus-newsgroup-unreads)
10201     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10202
10203 (defun gnus-summary-mark-region-as-read (point mark all)
10204   "Mark all unread articles between point and mark as read.
10205 If given a prefix, mark all articles between point and mark as read,
10206 even ticked and dormant ones."
10207   (interactive "r\nP")
10208   (save-excursion
10209     (let (article)
10210       (goto-char point)
10211       (beginning-of-line)
10212       (while (and
10213               (< (point) mark)
10214               (progn
10215                 (when (or all
10216                           (memq (setq article (gnus-summary-article-number))
10217                                 gnus-newsgroup-unreads))
10218                   (gnus-summary-mark-article article gnus-del-mark))
10219                 t)
10220               (gnus-summary-find-next))))))
10221
10222 (defun gnus-summary-mark-below (score mark)
10223   "Mark articles with score less than SCORE with MARK."
10224   (interactive "P\ncMark: ")
10225   (setq score (if score
10226                   (prefix-numeric-value score)
10227                 (or gnus-summary-default-score 0)))
10228   (save-excursion
10229     (set-buffer gnus-summary-buffer)
10230     (goto-char (point-min))
10231     (while
10232         (progn
10233           (and (< (gnus-summary-article-score) score)
10234                (gnus-summary-mark-article nil mark))
10235           (gnus-summary-find-next)))))
10236
10237 (defun gnus-summary-kill-below (&optional score)
10238   "Mark articles with score below SCORE as read."
10239   (interactive "P")
10240   (gnus-summary-mark-below score gnus-killed-mark))
10241
10242 (defun gnus-summary-clear-above (&optional score)
10243   "Clear all marks from articles with score above SCORE."
10244   (interactive "P")
10245   (gnus-summary-mark-above score gnus-unread-mark))
10246
10247 (defun gnus-summary-tick-above (&optional score)
10248   "Tick all articles with score above SCORE."
10249   (interactive "P")
10250   (gnus-summary-mark-above score gnus-ticked-mark))
10251
10252 (defun gnus-summary-mark-above (score mark)
10253   "Mark articles with score over SCORE with MARK."
10254   (interactive "P\ncMark: ")
10255   (setq score (if score
10256                   (prefix-numeric-value score)
10257                 (or gnus-summary-default-score 0)))
10258   (save-excursion
10259     (set-buffer gnus-summary-buffer)
10260     (goto-char (point-min))
10261     (while (and (progn
10262                   (when (> (gnus-summary-article-score) score)
10263                     (gnus-summary-mark-article nil mark))
10264                   t)
10265                 (gnus-summary-find-next)))))
10266
10267 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10268 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10269 (defun gnus-summary-limit-include-expunged (&optional no-error)
10270   "Display all the hidden articles that were expunged for low scores."
10271   (interactive)
10272   (let ((buffer-read-only nil))
10273     (let ((scored gnus-newsgroup-scored)
10274           headers h)
10275       (while scored
10276         (unless (gnus-summary-article-header (caar scored))
10277           (and (setq h (gnus-number-to-header (caar scored)))
10278                (< (cdar scored) gnus-summary-expunge-below)
10279                (push h headers)))
10280         (setq scored (cdr scored)))
10281       (if (not headers)
10282           (when (not no-error)
10283             (error "No expunged articles hidden"))
10284         (goto-char (point-min))
10285         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10286         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10287         (mapcar (lambda (x) (push (mail-header-number x)
10288                                   gnus-newsgroup-limit))
10289                 headers)
10290         (gnus-summary-prepare-unthreaded (nreverse headers))
10291         (goto-char (point-min))
10292         (gnus-summary-position-point)
10293         t))))
10294
10295 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10296   "Mark all unread articles in this newsgroup as read.
10297 If prefix argument ALL is non-nil, ticked and dormant articles will
10298 also be marked as read.
10299 If QUIETLY is non-nil, no questions will be asked.
10300
10301 If TO-HERE is non-nil, it should be a point in the buffer.  All
10302 articles before (after, if REVERSE is set) this point will be marked
10303 as read.
10304
10305 Note that this function will only catch up the unread article
10306 in the current summary buffer limitation.
10307
10308 The number of articles marked as read is returned."
10309   (interactive "P")
10310   (prog1
10311       (save-excursion
10312         (when (or quietly
10313                   (not gnus-interactive-catchup) ;Without confirmation?
10314                   gnus-expert-user
10315                   (gnus-y-or-n-p
10316                    (if all
10317                        "Mark absolutely all articles as read? "
10318                      "Mark all unread articles as read? ")))
10319           (if (and not-mark
10320                    (not gnus-newsgroup-adaptive)
10321                    (not gnus-newsgroup-auto-expire)
10322                    (not gnus-suppress-duplicates)
10323                    (or (not gnus-use-cache)
10324                        (eq gnus-use-cache 'passive)))
10325               (progn
10326                 (when all
10327                   (setq gnus-newsgroup-marked nil
10328                         gnus-newsgroup-spam-marked nil
10329                         gnus-newsgroup-dormant nil))
10330                 (setq gnus-newsgroup-unreads
10331                       (gnus-sorted-nunion
10332                        (gnus-intersection gnus-newsgroup-unreads
10333                                           gnus-newsgroup-downloadable)
10334                        gnus-newsgroup-unfetched)))
10335             ;; We actually mark all articles as canceled, which we
10336             ;; have to do when using auto-expiry or adaptive scoring.
10337             (gnus-summary-show-all-threads)
10338             (if (and to-here reverse)
10339                 (progn
10340                   (goto-char to-here)
10341                   (gnus-summary-mark-current-read-and-unread-as-read
10342                    gnus-catchup-mark)
10343                   (while (gnus-summary-find-next (not all))
10344                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10345               (when (gnus-summary-first-subject (not all))
10346                 (while (and
10347                         (if to-here (< (point) to-here) t)
10348                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10349                         (gnus-summary-find-next (not all))))))
10350             (gnus-set-mode-line 'summary))
10351           t))
10352     (gnus-summary-position-point)))
10353
10354 (defun gnus-summary-catchup-to-here (&optional all)
10355   "Mark all unticked articles before the current one as read.
10356 If ALL is non-nil, also mark ticked and dormant articles as read."
10357   (interactive "P")
10358   (save-excursion
10359     (gnus-save-hidden-threads
10360       (let ((beg (point)))
10361         ;; We check that there are unread articles.
10362         (when (or all (gnus-summary-find-prev))
10363           (gnus-summary-catchup all t beg)))))
10364   (gnus-summary-position-point))
10365
10366 (defun gnus-summary-catchup-from-here (&optional all)
10367   "Mark all unticked articles after (and including) the current one as read.
10368 If ALL is non-nil, also mark ticked and dormant articles as read."
10369   (interactive "P")
10370   (save-excursion
10371     (gnus-save-hidden-threads
10372       (let ((beg (point)))
10373         ;; We check that there are unread articles.
10374         (when (or all (gnus-summary-find-next))
10375           (gnus-summary-catchup all t beg nil t)))))
10376   (gnus-summary-position-point))
10377
10378 (defun gnus-summary-catchup-all (&optional quietly)
10379   "Mark all articles in this newsgroup as read.
10380 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10381 instead, which marks only unread articles as read."
10382   (interactive "P")
10383   (gnus-summary-catchup t quietly))
10384
10385 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10386   "Mark all unread articles in this group as read, then exit.
10387 If prefix argument ALL is non-nil, all articles are marked as read.
10388 If QUIETLY is non-nil, no questions will be asked."
10389   (interactive "P")
10390   (when (gnus-summary-catchup all quietly nil 'fast)
10391     ;; Select next newsgroup or exit.
10392     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10393              (eq gnus-auto-select-next 'quietly))
10394         (gnus-summary-next-group nil)
10395       (gnus-summary-exit))))
10396
10397 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10398   "Mark all articles in this newsgroup as read, and then exit.
10399 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10400 instead, which marks only unread articles as read."
10401   (interactive "P")
10402   (gnus-summary-catchup-and-exit t quietly))
10403
10404 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10405   "Mark all articles in this group as read and select the next group.
10406 If given a prefix, mark all articles, unread as well as ticked, as
10407 read."
10408   (interactive "P")
10409   (save-excursion
10410     (gnus-summary-catchup all))
10411   (gnus-summary-next-group))
10412
10413 ;;;
10414 ;;; with article
10415 ;;;
10416
10417 (defmacro gnus-with-article (article &rest forms)
10418   "Select ARTICLE and perform FORMS in the original article buffer.
10419 Then replace the article with the result."
10420   `(progn
10421      ;; We don't want the article to be marked as read.
10422      (let (gnus-mark-article-hook)
10423        (gnus-summary-select-article t t nil ,article))
10424      (set-buffer gnus-original-article-buffer)
10425      ,@forms
10426      (if (not (gnus-check-backend-function
10427                'request-replace-article (car gnus-article-current)))
10428          (gnus-message 5 "Read-only group; not replacing")
10429        (unless (gnus-request-replace-article
10430                 ,article (car gnus-article-current)
10431                 (current-buffer) t)
10432          (error "Couldn't replace article")))
10433      ;; The cache and backlog have to be flushed somewhat.
10434      (when gnus-keep-backlog
10435        (gnus-backlog-remove-article
10436         (car gnus-article-current) (cdr gnus-article-current)))
10437      (when gnus-use-cache
10438        (gnus-cache-update-article
10439         (car gnus-article-current) (cdr gnus-article-current)))))
10440
10441 (put 'gnus-with-article 'lisp-indent-function 1)
10442 (put 'gnus-with-article 'edebug-form-spec '(form body))
10443
10444 ;; Thread-based commands.
10445
10446 (defun gnus-summary-articles-in-thread (&optional article)
10447   "Return a list of all articles in the current thread.
10448 If ARTICLE is non-nil, return all articles in the thread that starts
10449 with that article."
10450   (let* ((article (or article (gnus-summary-article-number)))
10451          (data (gnus-data-find-list article))
10452          (top-level (gnus-data-level (car data)))
10453          (top-subject
10454           (cond ((null gnus-thread-operation-ignore-subject)
10455                  (gnus-simplify-subject-re
10456                   (mail-header-subject (gnus-data-header (car data)))))
10457                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10458                  (gnus-simplify-subject-fuzzy
10459                   (mail-header-subject (gnus-data-header (car data)))))
10460                 (t nil)))
10461          (end-point (save-excursion
10462                       (if (gnus-summary-go-to-next-thread)
10463                           (point) (point-max))))
10464          articles)
10465     (while (and data
10466                 (< (gnus-data-pos (car data)) end-point))
10467       (when (or (not top-subject)
10468                 (string= top-subject
10469                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10470                              (gnus-simplify-subject-fuzzy
10471                               (mail-header-subject
10472                                (gnus-data-header (car data))))
10473                            (gnus-simplify-subject-re
10474                             (mail-header-subject
10475                              (gnus-data-header (car data)))))))
10476         (push (gnus-data-number (car data)) articles))
10477       (unless (and (setq data (cdr data))
10478                    (> (gnus-data-level (car data)) top-level))
10479         (setq data nil)))
10480     ;; Return the list of articles.
10481     (nreverse articles)))
10482
10483 (defun gnus-summary-rethread-current ()
10484   "Rethread the thread the current article is part of."
10485   (interactive)
10486   (let* ((gnus-show-threads t)
10487          (article (gnus-summary-article-number))
10488          (id (mail-header-id (gnus-summary-article-header)))
10489          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10490     (unless id
10491       (error "No article on the current line"))
10492     (gnus-rebuild-thread id)
10493     (gnus-summary-goto-subject article)))
10494
10495 (defun gnus-summary-reparent-thread ()
10496   "Make the current article child of the marked (or previous) article.
10497
10498 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10499 is non-nil or the Subject: of both articles are the same."
10500   (interactive)
10501   (unless (not (gnus-group-read-only-p))
10502     (error "The current newsgroup does not support article editing"))
10503   (unless (<= (length gnus-newsgroup-processable) 1)
10504     (error "No more than one article may be marked"))
10505   (save-window-excursion
10506     (let ((gnus-article-buffer " *reparent*")
10507           (current-article (gnus-summary-article-number))
10508           ;; First grab the marked article, otherwise one line up.
10509           (parent-article (if (not (null gnus-newsgroup-processable))
10510                               (car gnus-newsgroup-processable)
10511                             (save-excursion
10512                               (if (eq (forward-line -1) 0)
10513                                   (gnus-summary-article-number)
10514                                 (error "Beginning of summary buffer"))))))
10515       (unless (not (eq current-article parent-article))
10516         (error "An article may not be self-referential"))
10517       (let ((message-id (mail-header-id
10518                          (gnus-summary-article-header parent-article))))
10519         (unless (and message-id (not (equal message-id "")))
10520           (error "No message-id in desired parent"))
10521         (gnus-with-article current-article
10522           (save-restriction
10523             (goto-char (point-min))
10524             (message-narrow-to-head)
10525             (if (re-search-forward "^References: " nil t)
10526                 (progn
10527                   (re-search-forward "^[^ \t]" nil t)
10528                   (forward-line -1)
10529                   (end-of-line)
10530                   (insert " " message-id))
10531               (insert "References: " message-id "\n"))))
10532         (set-buffer gnus-summary-buffer)
10533         (gnus-summary-unmark-all-processable)
10534         (gnus-summary-update-article current-article)
10535         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10536             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10537         (gnus-summary-rethread-current)
10538         (gnus-message 3 "Article %d is now the child of article %d"
10539                       current-article parent-article)))))
10540
10541 (defun gnus-summary-toggle-threads (&optional arg)
10542   "Toggle showing conversation threads.
10543 If ARG is positive number, turn showing conversation threads on."
10544   (interactive "P")
10545   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10546     (setq gnus-show-threads
10547           (if (null arg) (not gnus-show-threads)
10548             (> (prefix-numeric-value arg) 0)))
10549     (gnus-summary-prepare)
10550     (gnus-summary-goto-subject current)
10551     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10552     (gnus-summary-position-point)))
10553
10554 (defun gnus-summary-show-all-threads ()
10555   "Show all threads."
10556   (interactive)
10557   (save-excursion
10558     (let ((buffer-read-only nil))
10559       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10560   (gnus-summary-position-point))
10561
10562 (defun gnus-summary-show-thread ()
10563   "Show thread subtrees.
10564 Returns nil if no thread was there to be shown."
10565   (interactive)
10566   (let ((buffer-read-only nil)
10567         (orig (point))
10568         (end (point-at-eol))
10569         ;; Leave point at bol
10570         (beg (progn (beginning-of-line) (point))))
10571     (prog1
10572         ;; Any hidden lines here?
10573         (search-forward "\r" end t)
10574       (subst-char-in-region beg end ?\^M ?\n t)
10575       (goto-char orig)
10576       (gnus-summary-position-point))))
10577
10578 (defun gnus-summary-maybe-hide-threads ()
10579   "If requested, hide the threads that should be hidden."
10580   (when (and gnus-show-threads
10581              gnus-thread-hide-subtree)
10582     (gnus-summary-hide-all-threads
10583      (if (or (consp gnus-thread-hide-subtree)
10584              (functionp gnus-thread-hide-subtree))
10585          (gnus-make-predicate gnus-thread-hide-subtree)
10586        nil))))
10587
10588 ;;; Hiding predicates.
10589
10590 (defun gnus-article-unread-p (header)
10591   (memq (mail-header-number header) gnus-newsgroup-unreads))
10592
10593 (defun gnus-article-unseen-p (header)
10594   (memq (mail-header-number header) gnus-newsgroup-unseen))
10595
10596 (defun gnus-map-articles (predicate articles)
10597   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10598   (apply 'gnus-or (mapcar predicate
10599                           (mapcar 'gnus-summary-article-header articles))))
10600
10601 (defun gnus-summary-hide-all-threads (&optional predicate)
10602   "Hide all thread subtrees.
10603 If PREDICATE is supplied, threads that satisfy this predicate
10604 will not be hidden."
10605   (interactive)
10606   (save-excursion
10607     (goto-char (point-min))
10608     (let ((end nil))
10609       (while (not end)
10610         (when (or (not predicate)
10611                   (gnus-map-articles
10612                    predicate (gnus-summary-article-children)))
10613             (gnus-summary-hide-thread))
10614         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10615   (gnus-summary-position-point))
10616
10617 (defun gnus-summary-hide-thread ()
10618   "Hide thread subtrees.
10619 If PREDICATE is supplied, threads that satisfy this predicate
10620 will not be hidden.
10621 Returns nil if no threads were there to be hidden."
10622   (interactive)
10623   (let ((buffer-read-only nil)
10624         (start (point))
10625         (article (gnus-summary-article-number)))
10626     (goto-char start)
10627     ;; Go forward until either the buffer ends or the subthread
10628     ;; ends.
10629     (when (and (not (eobp))
10630                (or (zerop (gnus-summary-next-thread 1 t))
10631                    (goto-char (point-max))))
10632       (prog1
10633           (if (and (> (point) start)
10634                    (search-backward "\n" start t))
10635               (progn
10636                 (subst-char-in-region start (point) ?\n ?\^M)
10637                 (gnus-summary-goto-subject article))
10638             (goto-char start)
10639             nil)))))
10640
10641 (defun gnus-summary-go-to-next-thread (&optional previous)
10642   "Go to the same level (or less) next thread.
10643 If PREVIOUS is non-nil, go to previous thread instead.
10644 Return the article number moved to, or nil if moving was impossible."
10645   (let ((level (gnus-summary-thread-level))
10646         (way (if previous -1 1))
10647         (beg (point)))
10648     (forward-line way)
10649     (while (and (not (eobp))
10650                 (< level (gnus-summary-thread-level)))
10651       (forward-line way))
10652     (if (eobp)
10653         (progn
10654           (goto-char beg)
10655           nil)
10656       (setq beg (point))
10657       (prog1
10658           (gnus-summary-article-number)
10659         (goto-char beg)))))
10660
10661 (defun gnus-summary-next-thread (n &optional silent)
10662   "Go to the same level next N'th thread.
10663 If N is negative, search backward instead.
10664 Returns the difference between N and the number of skips actually
10665 done.
10666
10667 If SILENT, don't output messages."
10668   (interactive "p")
10669   (let ((backward (< n 0))
10670         (n (abs n)))
10671     (while (and (> n 0)
10672                 (gnus-summary-go-to-next-thread backward))
10673       (decf n))
10674     (unless silent
10675       (gnus-summary-position-point))
10676     (when (and (not silent) (/= 0 n))
10677       (gnus-message 7 "No more threads"))
10678     n))
10679
10680 (defun gnus-summary-prev-thread (n)
10681   "Go to the same level previous N'th thread.
10682 Returns the difference between N and the number of skips actually
10683 done."
10684   (interactive "p")
10685   (gnus-summary-next-thread (- n)))
10686
10687 (defun gnus-summary-go-down-thread ()
10688   "Go down one level in the current thread."
10689   (let ((children (gnus-summary-article-children)))
10690     (when children
10691       (gnus-summary-goto-subject (car children)))))
10692
10693 (defun gnus-summary-go-up-thread ()
10694   "Go up one level in the current thread."
10695   (let ((parent (gnus-summary-article-parent)))
10696     (when parent
10697       (gnus-summary-goto-subject parent))))
10698
10699 (defun gnus-summary-down-thread (n)
10700   "Go down thread N steps.
10701 If N is negative, go up instead.
10702 Returns the difference between N and how many steps down that were
10703 taken."
10704   (interactive "p")
10705   (let ((up (< n 0))
10706         (n (abs n)))
10707     (while (and (> n 0)
10708                 (if up (gnus-summary-go-up-thread)
10709                   (gnus-summary-go-down-thread)))
10710       (setq n (1- n)))
10711     (gnus-summary-position-point)
10712     (when (/= 0 n)
10713       (gnus-message 7 "Can't go further"))
10714     n))
10715
10716 (defun gnus-summary-up-thread (n)
10717   "Go up thread N steps.
10718 If N is negative, go down instead.
10719 Returns the difference between N and how many steps down that were
10720 taken."
10721   (interactive "p")
10722   (gnus-summary-down-thread (- n)))
10723
10724 (defun gnus-summary-top-thread ()
10725   "Go to the top of the thread."
10726   (interactive)
10727   (while (gnus-summary-go-up-thread))
10728   (gnus-summary-article-number))
10729
10730 (defun gnus-summary-kill-thread (&optional unmark)
10731   "Mark articles under current thread as read.
10732 If the prefix argument is positive, remove any kinds of marks.
10733 If the prefix argument is negative, tick articles instead."
10734   (interactive "P")
10735   (when unmark
10736     (setq unmark (prefix-numeric-value unmark)))
10737   (let ((articles (gnus-summary-articles-in-thread)))
10738     (save-excursion
10739       ;; Expand the thread.
10740       (gnus-summary-show-thread)
10741       ;; Mark all the articles.
10742       (while articles
10743         (gnus-summary-goto-subject (car articles))
10744         (cond ((null unmark)
10745                (gnus-summary-mark-article-as-read gnus-killed-mark))
10746               ((> unmark 0)
10747                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10748               (t
10749                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10750         (setq articles (cdr articles))))
10751     ;; Hide killed subtrees.
10752     (and (null unmark)
10753          gnus-thread-hide-killed
10754          (gnus-summary-hide-thread))
10755     ;; If marked as read, go to next unread subject.
10756     (when (null unmark)
10757       ;; Go to next unread subject.
10758       (gnus-summary-next-subject 1 t)))
10759   (gnus-set-mode-line 'summary))
10760
10761 ;; Summary sorting commands
10762
10763 (defun gnus-summary-sort-by-number (&optional reverse)
10764   "Sort the summary buffer by article number.
10765 Argument REVERSE means reverse order."
10766   (interactive "P")
10767   (gnus-summary-sort 'number reverse))
10768
10769 (defun gnus-summary-sort-by-random (&optional reverse)
10770   "Randomize the order in the summary buffer.
10771 Argument REVERSE means to randomize in reverse order."
10772   (interactive "P")
10773   (gnus-summary-sort 'random reverse))
10774
10775 (defun gnus-summary-sort-by-author (&optional reverse)
10776   "Sort the summary buffer by author name alphabetically.
10777 If `case-fold-search' is non-nil, case of letters is ignored.
10778 Argument REVERSE means reverse order."
10779   (interactive "P")
10780   (gnus-summary-sort 'author reverse))
10781
10782 (defun gnus-summary-sort-by-subject (&optional reverse)
10783   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10784 If `case-fold-search' is non-nil, case of letters is ignored.
10785 Argument REVERSE means reverse order."
10786   (interactive "P")
10787   (gnus-summary-sort 'subject reverse))
10788
10789 (defun gnus-summary-sort-by-date (&optional reverse)
10790   "Sort the summary buffer by date.
10791 Argument REVERSE means reverse order."
10792   (interactive "P")
10793   (gnus-summary-sort 'date reverse))
10794
10795 (defun gnus-summary-sort-by-score (&optional reverse)
10796   "Sort the summary buffer by score.
10797 Argument REVERSE means reverse order."
10798   (interactive "P")
10799   (gnus-summary-sort 'score reverse))
10800
10801 (defun gnus-summary-sort-by-lines (&optional reverse)
10802   "Sort the summary buffer by the number of lines.
10803 Argument REVERSE means reverse order."
10804   (interactive "P")
10805   (gnus-summary-sort 'lines reverse))
10806
10807 (defun gnus-summary-sort-by-chars (&optional reverse)
10808   "Sort the summary buffer by article length.
10809 Argument REVERSE means reverse order."
10810   (interactive "P")
10811   (gnus-summary-sort 'chars reverse))
10812
10813 (defun gnus-summary-sort-by-original (&optional reverse)
10814   "Sort the summary buffer using the default sorting method.
10815 Argument REVERSE means reverse order."
10816   (interactive "P")
10817   (let* ((buffer-read-only)
10818          (gnus-summary-prepare-hook nil))
10819     ;; We do the sorting by regenerating the threads.
10820     (gnus-summary-prepare)
10821     ;; Hide subthreads if needed.
10822     (gnus-summary-maybe-hide-threads)))
10823
10824 (defun gnus-summary-sort (predicate reverse)
10825   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10826   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10827          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10828          (gnus-thread-sort-functions
10829           (if (not reverse)
10830               thread
10831             `(lambda (t1 t2)
10832                (,thread t2 t1))))
10833          (gnus-sort-gathered-threads-function
10834           gnus-thread-sort-functions)
10835          (gnus-article-sort-functions
10836           (if (not reverse)
10837               article
10838             `(lambda (t1 t2)
10839                (,article t2 t1))))
10840          (buffer-read-only)
10841          (gnus-summary-prepare-hook nil))
10842     ;; We do the sorting by regenerating the threads.
10843     (gnus-summary-prepare)
10844     ;; Hide subthreads if needed.
10845     (gnus-summary-maybe-hide-threads)))
10846
10847 ;; Summary saving commands.
10848
10849 (defun gnus-summary-save-article (&optional n not-saved)
10850   "Save the current article using the default saver function.
10851 If N is a positive number, save the N next articles.
10852 If N is a negative number, save the N previous articles.
10853 If N is nil and any articles have been marked with the process mark,
10854 save those articles instead.
10855 The variable `gnus-default-article-saver' specifies the saver function."
10856   (interactive "P")
10857   (let* ((articles (gnus-summary-work-articles n))
10858          (save-buffer (save-excursion
10859                         (nnheader-set-temp-buffer " *Gnus Save*")))
10860          (num (length articles))
10861          header file)
10862     (dolist (article articles)
10863       (setq header (gnus-summary-article-header article))
10864       (if (not (vectorp header))
10865           ;; This is a pseudo-article.
10866           (if (assq 'name header)
10867               (gnus-copy-file (cdr (assq 'name header)))
10868             (gnus-message 1 "Article %d is unsaveable" article))
10869         ;; This is a real article.
10870         (save-window-excursion
10871           (let ((gnus-display-mime-function nil)
10872                 (gnus-article-prepare-hook nil))
10873             (gnus-summary-select-article t nil nil article)))
10874         (save-excursion
10875           (set-buffer save-buffer)
10876           (erase-buffer)
10877           (insert-buffer-substring gnus-original-article-buffer))
10878         (setq file (gnus-article-save save-buffer file num))
10879         (gnus-summary-remove-process-mark article)
10880         (unless not-saved
10881           (gnus-summary-set-saved-mark article))))
10882     (gnus-kill-buffer save-buffer)
10883     (gnus-summary-position-point)
10884     (gnus-set-mode-line 'summary)
10885     n))
10886
10887 (defun gnus-summary-pipe-output (&optional arg headers)
10888   "Pipe the current article to a subprocess.
10889 If N is a positive number, pipe the N next articles.
10890 If N is a negative number, pipe the N previous articles.
10891 If N is nil and any articles have been marked with the process mark,
10892 pipe those articles instead.
10893 If HEADERS (the symbolic prefix), include the headers, too."
10894   (interactive (gnus-interactive "P\ny"))
10895   (require 'gnus-art)
10896   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10897         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10898     (gnus-summary-save-article arg t))
10899   (let ((buffer (get-buffer "*Shell Command Output*")))
10900     (when (and buffer
10901                (not (zerop (buffer-size buffer))))
10902       (gnus-configure-windows 'pipe))))
10903
10904 (defun gnus-summary-save-article-mail (&optional arg)
10905   "Append the current article to an mail file.
10906 If N is a positive number, save the N next articles.
10907 If N is a negative number, save the N previous articles.
10908 If N is nil and any articles have been marked with the process mark,
10909 save those articles instead."
10910   (interactive "P")
10911   (require 'gnus-art)
10912   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10913     (gnus-summary-save-article arg)))
10914
10915 (defun gnus-summary-save-article-rmail (&optional arg)
10916   "Append the current article to an rmail file.
10917 If N is a positive number, save the N next articles.
10918 If N is a negative number, save the N previous articles.
10919 If N is nil and any articles have been marked with the process mark,
10920 save those articles instead."
10921   (interactive "P")
10922   (require 'gnus-art)
10923   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10924     (gnus-summary-save-article arg)))
10925
10926 (defun gnus-summary-save-article-file (&optional arg)
10927   "Append the current article to a file.
10928 If N is a positive number, save the N next articles.
10929 If N is a negative number, save the N previous articles.
10930 If N is nil and any articles have been marked with the process mark,
10931 save those articles instead."
10932   (interactive "P")
10933   (require 'gnus-art)
10934   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10935     (gnus-summary-save-article arg)))
10936
10937 (defun gnus-summary-write-article-file (&optional arg)
10938   "Write the current article to a file, deleting the previous file.
10939 If N is a positive number, save the N next articles.
10940 If N is a negative number, save the N previous articles.
10941 If N is nil and any articles have been marked with the process mark,
10942 save those articles instead."
10943   (interactive "P")
10944   (require 'gnus-art)
10945   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10946     (gnus-summary-save-article arg)))
10947
10948 (defun gnus-summary-save-article-body-file (&optional arg)
10949   "Append the current article body to a file.
10950 If N is a positive number, save the N next articles.
10951 If N is a negative number, save the N previous articles.
10952 If N is nil and any articles have been marked with the process mark,
10953 save those articles instead."
10954   (interactive "P")
10955   (require 'gnus-art)
10956   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10957     (gnus-summary-save-article arg)))
10958
10959 (defun gnus-summary-muttprint (&optional arg)
10960   "Print the current article using Muttprint.
10961 If N is a positive number, save the N next articles.
10962 If N is a negative number, save the N previous articles.
10963 If N is nil and any articles have been marked with the process mark,
10964 save those articles instead."
10965   (interactive "P")
10966   (require 'gnus-art)
10967   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10968     (gnus-summary-save-article arg t)))
10969
10970 (defun gnus-summary-pipe-message (program)
10971   "Pipe the current article through PROGRAM."
10972   (interactive "sProgram: ")
10973   (gnus-summary-select-article)
10974   (let ((mail-header-separator ""))
10975     (gnus-eval-in-buffer-window gnus-article-buffer
10976       (save-restriction
10977         (widen)
10978         (let ((start (window-start))
10979               buffer-read-only)
10980           (message-pipe-buffer-body program)
10981           (set-window-start (get-buffer-window (current-buffer)) start))))))
10982
10983 (defun gnus-get-split-value (methods)
10984   "Return a value based on the split METHODS."
10985   (let (split-name method result match)
10986     (when methods
10987       (save-excursion
10988         (set-buffer gnus-original-article-buffer)
10989         (save-restriction
10990           (nnheader-narrow-to-headers)
10991           (while (and methods (not split-name))
10992             (goto-char (point-min))
10993             (setq method (pop methods))
10994             (setq match (car method))
10995             (when (cond
10996                    ((stringp match)
10997                     ;; Regular expression.
10998                     (ignore-errors
10999                       (re-search-forward match nil t)))
11000                    ((functionp match)
11001                     ;; Function.
11002                     (save-restriction
11003                       (widen)
11004                       (setq result (funcall match gnus-newsgroup-name))))
11005                    ((consp match)
11006                     ;; Form.
11007                     (save-restriction
11008                       (widen)
11009                       (setq result (eval match)))))
11010               (setq split-name (cdr method))
11011               (cond ((stringp result)
11012                      (push (expand-file-name
11013                             result gnus-article-save-directory)
11014                            split-name))
11015                     ((consp result)
11016                      (setq split-name (append result split-name)))))))))
11017     (nreverse split-name)))
11018
11019 (defun gnus-valid-move-group-p (group)
11020   (and (boundp group)
11021        (symbol-name group)
11022        (symbol-value group)
11023        (gnus-get-function (gnus-find-method-for-group
11024                            (symbol-name group)) 'request-accept-article t)))
11025
11026 (defun gnus-read-move-group-name (prompt default articles prefix)
11027   "Read a group name."
11028   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11029          (minibuffer-confirm-incomplete nil) ; XEmacs
11030          (prom
11031           (format "%s %s to:"
11032                   prompt
11033                   (if (> (length articles) 1)
11034                       (format "these %d articles" (length articles))
11035                     "this article")))
11036          (to-newsgroup
11037           (cond
11038            ((null split-name)
11039             (gnus-completing-read-with-default
11040              default prom
11041              gnus-active-hashtb
11042              'gnus-valid-move-group-p
11043              nil prefix
11044              'gnus-group-history))
11045            ((= 1 (length split-name))
11046             (gnus-completing-read-with-default
11047              (car split-name) prom
11048              gnus-active-hashtb
11049              'gnus-valid-move-group-p
11050              nil nil
11051              'gnus-group-history))
11052            (t
11053             (gnus-completing-read-with-default
11054              nil prom
11055              (mapcar 'list (nreverse split-name))
11056              nil nil nil
11057              'gnus-group-history))))
11058          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11059     (when to-newsgroup
11060       (if (or (string= to-newsgroup "")
11061               (string= to-newsgroup prefix))
11062           (setq to-newsgroup default))
11063       (unless to-newsgroup
11064         (error "No group name entered"))
11065       (or (gnus-active to-newsgroup)
11066           (gnus-activate-group to-newsgroup nil nil to-method)
11067           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11068                                      to-newsgroup))
11069               (or (and (gnus-request-create-group to-newsgroup to-method)
11070                        (gnus-activate-group
11071                         to-newsgroup nil nil to-method)
11072                        (gnus-subscribe-group to-newsgroup))
11073                   (error "Couldn't create group %s" to-newsgroup)))
11074           (error "No such group: %s" to-newsgroup)))
11075     to-newsgroup))
11076
11077 (defun gnus-summary-save-parts (type dir n &optional reverse)
11078   "Save parts matching TYPE to DIR.
11079 If REVERSE, save parts that do not match TYPE."
11080   (interactive
11081    (list (read-string "Save parts of type: "
11082                       (or (car gnus-summary-save-parts-type-history)
11083                           gnus-summary-save-parts-default-mime)
11084                       'gnus-summary-save-parts-type-history)
11085          (setq gnus-summary-save-parts-last-directory
11086                (read-file-name "Save to directory: "
11087                                gnus-summary-save-parts-last-directory
11088                                nil t))
11089          current-prefix-arg))
11090   (gnus-summary-iterate n
11091     (let ((gnus-display-mime-function nil)
11092           (gnus-inhibit-treatment t))
11093       (gnus-summary-select-article))
11094     (save-excursion
11095       (set-buffer gnus-article-buffer)
11096       (let ((handles (or gnus-article-mime-handles
11097                          (mm-dissect-buffer nil gnus-article-loose-mime)
11098                          (and gnus-article-emulate-mime
11099                               (mm-uu-dissect)))))
11100         (when handles
11101           (gnus-summary-save-parts-1 type dir handles reverse)
11102           (unless gnus-article-mime-handles ;; Don't destroy this case.
11103             (mm-destroy-parts handles)))))))
11104
11105 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11106   (if (stringp (car handle))
11107       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11108               (cdr handle))
11109     (when (if reverse
11110               (not (string-match type (mm-handle-media-type handle)))
11111             (string-match type (mm-handle-media-type handle)))
11112       (let ((file (expand-file-name
11113                    (gnus-map-function
11114                     mm-file-name-rewrite-functions
11115                     (file-name-nondirectory
11116                      (or
11117                       (mail-content-type-get
11118                        (mm-handle-disposition handle) 'filename)
11119                       (mail-content-type-get
11120                        (mm-handle-type handle) 'name)
11121                       (concat gnus-newsgroup-name
11122                               "." (number-to-string
11123                                    (cdr gnus-article-current))))))
11124                    dir)))
11125         (unless (file-exists-p file)
11126           (mm-save-part-to-file handle file))))))
11127
11128 ;; Summary extract commands
11129
11130 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11131   (let ((buffer-read-only nil)
11132         (article (gnus-summary-article-number))
11133         after-article b e)
11134     (unless (gnus-summary-goto-subject article)
11135       (error "No such article: %d" article))
11136     (gnus-summary-position-point)
11137     ;; If all commands are to be bunched up on one line, we collect
11138     ;; them here.
11139     (unless gnus-view-pseudos-separately
11140       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11141             files action)
11142         (while ps
11143           (setq action (cdr (assq 'action (car ps))))
11144           (setq files (list (cdr (assq 'name (car ps)))))
11145           (while (and ps (cdr ps)
11146                       (string= (or action "1")
11147                                (or (cdr (assq 'action (cadr ps))) "2")))
11148             (push (cdr (assq 'name (cadr ps))) files)
11149             (setcdr ps (cddr ps)))
11150           (when files
11151             (when (not (string-match "%s" action))
11152               (push " " files))
11153             (push " " files)
11154             (when (assq 'execute (car ps))
11155               (setcdr (assq 'execute (car ps))
11156                       (funcall (if (string-match "%s" action)
11157                                    'format 'concat)
11158                                action
11159                                (mapconcat
11160                                 (lambda (f)
11161                                   (if (equal f " ")
11162                                       f
11163                                     (shell-quote-argument f)))
11164                                 files " ")))))
11165           (setq ps (cdr ps)))))
11166     (if (and gnus-view-pseudos (not not-view))
11167         (while pslist
11168           (when (assq 'execute (car pslist))
11169             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11170                                   (eq gnus-view-pseudos 'not-confirm)))
11171           (setq pslist (cdr pslist)))
11172       (save-excursion
11173         (while pslist
11174           (setq after-article (or (cdr (assq 'article (car pslist)))
11175                                   (gnus-summary-article-number)))
11176           (gnus-summary-goto-subject after-article)
11177           (forward-line 1)
11178           (setq b (point))
11179           (insert "    " (file-name-nondirectory
11180                           (cdr (assq 'name (car pslist))))
11181                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11182           (setq e (point))
11183           (forward-line -1)             ; back to `b'
11184           (gnus-add-text-properties
11185            b (1- e) (list 'gnus-number gnus-reffed-article-number
11186                           gnus-mouse-face-prop gnus-mouse-face))
11187           (gnus-data-enter
11188            after-article gnus-reffed-article-number
11189            gnus-unread-mark b (car pslist) 0 (- e b))
11190           (setq gnus-newsgroup-unreads
11191                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11192                                          gnus-reffed-article-number))
11193           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11194           (setq pslist (cdr pslist)))))))
11195
11196 (defun gnus-pseudos< (p1 p2)
11197   (let ((c1 (cdr (assq 'action p1)))
11198         (c2 (cdr (assq 'action p2))))
11199     (and c1 c2 (string< c1 c2))))
11200
11201 (defun gnus-request-pseudo-article (props)
11202   (cond ((assq 'execute props)
11203          (gnus-execute-command (cdr (assq 'execute props)))))
11204   (let ((gnus-current-article (gnus-summary-article-number)))
11205     (gnus-run-hooks 'gnus-mark-article-hook)))
11206
11207 (defun gnus-execute-command (command &optional automatic)
11208   (save-excursion
11209     (gnus-article-setup-buffer)
11210     (set-buffer gnus-article-buffer)
11211     (setq buffer-read-only nil)
11212     (let ((command (if automatic command
11213                      (read-string "Command: " (cons command 0)))))
11214       (erase-buffer)
11215       (insert "$ " command "\n\n")
11216       (if gnus-view-pseudo-asynchronously
11217           (start-process "gnus-execute" (current-buffer) shell-file-name
11218                          shell-command-switch command)
11219         (call-process shell-file-name nil t nil
11220                       shell-command-switch command)))))
11221
11222 ;; Summary kill commands.
11223
11224 (defun gnus-summary-edit-global-kill (article)
11225   "Edit the \"global\" kill file."
11226   (interactive (list (gnus-summary-article-number)))
11227   (gnus-group-edit-global-kill article))
11228
11229 (defun gnus-summary-edit-local-kill ()
11230   "Edit a local kill file applied to the current newsgroup."
11231   (interactive)
11232   (setq gnus-current-headers (gnus-summary-article-header))
11233   (gnus-group-edit-local-kill
11234    (gnus-summary-article-number) gnus-newsgroup-name))
11235
11236 ;;; Header reading.
11237
11238 (defun gnus-read-header (id &optional header)
11239   "Read the headers of article ID and enter them into the Gnus system."
11240   (let ((group gnus-newsgroup-name)
11241         (gnus-override-method
11242          (or
11243           gnus-override-method
11244           (and (gnus-news-group-p gnus-newsgroup-name)
11245                (car (gnus-refer-article-methods)))))
11246         where)
11247     ;; First we check to see whether the header in question is already
11248     ;; fetched.
11249     (if (stringp id)
11250         ;; This is a Message-ID.
11251         (setq header (or header (gnus-id-to-header id)))
11252       ;; This is an article number.
11253       (setq header (or header (gnus-summary-article-header id))))
11254     (if (and header
11255              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11256         ;; We have found the header.
11257         header
11258       ;; We have to really fetch the header to this article.
11259       (save-excursion
11260         (set-buffer nntp-server-buffer)
11261         (when (setq where (gnus-request-head id group))
11262           (nnheader-fold-continuation-lines)
11263           (goto-char (point-max))
11264           (insert ".\n")
11265           (goto-char (point-min))
11266           (insert "211 ")
11267           (princ (cond
11268                   ((numberp id) id)
11269                   ((cdr where) (cdr where))
11270                   (header (mail-header-number header))
11271                   (t gnus-reffed-article-number))
11272                  (current-buffer))
11273           (insert " Article retrieved.\n"))
11274         (if (or (not where)
11275                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11276             ()                          ; Malformed head.
11277           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11278             (when (and (stringp id)
11279                        (not (string= (gnus-group-real-name group)
11280                                      (car where))))
11281               ;; If we fetched by Message-ID and the article came
11282               ;; from a different group, we fudge some bogus article
11283               ;; numbers for this article.
11284               (mail-header-set-number header gnus-reffed-article-number))
11285             (save-excursion
11286               (set-buffer gnus-summary-buffer)
11287               (decf gnus-reffed-article-number)
11288               (gnus-remove-header (mail-header-number header))
11289               (push header gnus-newsgroup-headers)
11290               (setq gnus-current-headers header)
11291               (push (mail-header-number header) gnus-newsgroup-limit)))
11292           header)))))
11293
11294 (defun gnus-remove-header (number)
11295   "Remove header NUMBER from `gnus-newsgroup-headers'."
11296   (if (and gnus-newsgroup-headers
11297            (= number (mail-header-number (car gnus-newsgroup-headers))))
11298       (pop gnus-newsgroup-headers)
11299     (let ((headers gnus-newsgroup-headers))
11300       (while (and (cdr headers)
11301                   (not (= number (mail-header-number (cadr headers)))))
11302         (pop headers))
11303       (when (cdr headers)
11304         (setcdr headers (cddr headers))))))
11305
11306 ;;;
11307 ;;; summary highlights
11308 ;;;
11309
11310 (defun gnus-highlight-selected-summary ()
11311   "Highlight selected article in summary buffer."
11312   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11313   (when gnus-summary-selected-face
11314     (save-excursion
11315       (let* ((beg (point-at-bol))
11316              (end (point-at-eol))
11317              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11318              (from (if (get-text-property beg gnus-mouse-face-prop)
11319                        beg
11320                      (or (next-single-property-change
11321                           beg gnus-mouse-face-prop nil end)
11322                          beg)))
11323              (to
11324               (if (= from end)
11325                   (- from 2)
11326                 (or (next-single-property-change
11327                      from gnus-mouse-face-prop nil end)
11328                     end))))
11329         ;; If no mouse-face prop on line we will have to = from = end,
11330         ;; so we highlight the entire line instead.
11331         (when (= (+ to 2) from)
11332           (setq from beg)
11333           (setq to end))
11334         (if gnus-newsgroup-selected-overlay
11335             ;; Move old overlay.
11336             (gnus-move-overlay
11337              gnus-newsgroup-selected-overlay from to (current-buffer))
11338           ;; Create new overlay.
11339           (gnus-overlay-put
11340            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11341            'face gnus-summary-selected-face))))))
11342
11343 (defvar gnus-summary-highlight-line-cached nil)
11344 (defvar gnus-summary-highlight-line-trigger nil)
11345
11346 (defun gnus-summary-highlight-line-0 ()
11347   (if (and (eq gnus-summary-highlight-line-trigger
11348                gnus-summary-highlight)
11349            gnus-summary-highlight-line-cached)
11350       gnus-summary-highlight-line-cached
11351     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11352           gnus-summary-highlight-line-cached
11353           (let* ((cond (list 'cond))
11354                  (c cond)
11355                  (list gnus-summary-highlight))
11356             (while list
11357               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11358                               nil))
11359               (setq c (cdr c)
11360                     list (cdr list)))
11361             (gnus-byte-compile (list 'lambda nil cond))))))
11362
11363 (defun gnus-summary-highlight-line ()
11364   "Highlight current line according to `gnus-summary-highlight'."
11365   (let* ((beg (point-at-bol))
11366          (article (or (gnus-summary-article-number) gnus-current-article))
11367          (score (or (cdr (assq article
11368                                gnus-newsgroup-scored))
11369                     gnus-summary-default-score 0))
11370          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11371          (inhibit-read-only t)
11372          (default gnus-summary-default-score)
11373          (default-high gnus-summary-default-high-score)
11374          (default-low gnus-summary-default-low-score)
11375          (uncached (and gnus-summary-use-undownloaded-faces
11376                         (memq article gnus-newsgroup-undownloaded)
11377                         (not (memq article gnus-newsgroup-cached)))))
11378     (let ((face (funcall (gnus-summary-highlight-line-0))))
11379       (unless (eq face (get-text-property beg 'face))
11380         (gnus-put-text-property-excluding-characters-with-faces
11381          beg (point-at-eol) 'face
11382          (setq face (if (boundp face) (symbol-value face) face)))
11383         (when gnus-summary-highlight-line-function
11384           (funcall gnus-summary-highlight-line-function article face))))))
11385
11386 (defun gnus-update-read-articles (group unread &optional compute)
11387   "Update the list of read articles in GROUP.
11388 UNREAD is a sorted list."
11389   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11390         (info (gnus-get-info group))
11391         (prev 1)
11392         read)
11393     (if (or (not info) (not active))
11394         ;; There is no info on this group if it was, in fact,
11395         ;; killed.  Gnus stores no information on killed groups, so
11396         ;; there's nothing to be done.
11397         ;; One could store the information somewhere temporarily,
11398         ;; perhaps...  Hmmm...
11399         ()
11400       ;; Remove any negative articles numbers.
11401       (while (and unread (< (car unread) 0))
11402         (setq unread (cdr unread)))
11403       ;; Remove any expired article numbers
11404       (while (and unread (< (car unread) (car active)))
11405         (setq unread (cdr unread)))
11406       ;; Compute the ranges of read articles by looking at the list of
11407       ;; unread articles.
11408       (while unread
11409         (when (/= (car unread) prev)
11410           (push (if (= prev (1- (car unread))) prev
11411                   (cons prev (1- (car unread))))
11412                 read))
11413         (setq prev (1+ (car unread)))
11414         (setq unread (cdr unread)))
11415       (when (<= prev (cdr active))
11416         (push (cons prev (cdr active)) read))
11417       (setq read (if (> (length read) 1) (nreverse read) read))
11418       (if compute
11419           read
11420         (save-excursion
11421           (let (setmarkundo)
11422             ;; Propagate the read marks to the backend.
11423             (when (gnus-check-backend-function 'request-set-mark group)
11424               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11425                     (add (gnus-remove-from-range read (gnus-info-read info))))
11426                 (when (or add del)
11427                   (unless (gnus-check-group group)
11428                     (error "Can't open server for %s" group))
11429                   (gnus-request-set-mark
11430                    group (delq nil (list (if add (list add 'add '(read)))
11431                                          (if del (list del 'del '(read))))))
11432                   (setq setmarkundo
11433                         `(gnus-request-set-mark
11434                           ,group
11435                           ',(delq nil (list
11436                                        (if del (list del 'add '(read)))
11437                                        (if add (list add 'del '(read))))))))))
11438             (set-buffer gnus-group-buffer)
11439             (gnus-undo-register
11440               `(progn
11441                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11442                  (gnus-info-set-read ',info ',(gnus-info-read info))
11443                  (gnus-get-unread-articles-in-group ',info
11444                                                     (gnus-active ,group))
11445                  (gnus-group-update-group ,group t)
11446                  ,setmarkundo))))
11447         ;; Enter this list into the group info.
11448         (gnus-info-set-read info read)
11449         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11450         (gnus-get-unread-articles-in-group info (gnus-active group))
11451         t))))
11452
11453 (defun gnus-offer-save-summaries ()
11454   "Offer to save all active summary buffers."
11455   (let (buffers)
11456     ;; Go through all buffers and find all summaries.
11457     (dolist (buffer (buffer-list))
11458       (when (and (setq buffer (buffer-name buffer))
11459                  (string-match "Summary" buffer)
11460                  (with-current-buffer buffer
11461                    ;; We check that this is, indeed, a summary buffer.
11462                    (and (eq major-mode 'gnus-summary-mode)
11463                         ;; Also make sure this isn't bogus.
11464                         gnus-newsgroup-prepared
11465                         ;; Also make sure that this isn't a
11466                         ;; dead summary buffer.
11467                         (not gnus-dead-summary-mode))))
11468         (push buffer buffers)))
11469     ;; Go through all these summary buffers and offer to save them.
11470     (when buffers
11471       (save-excursion
11472         (map-y-or-n-p
11473          "Update summary buffer %s? "
11474          (lambda (buf)
11475            (switch-to-buffer buf)
11476            (gnus-summary-exit))
11477          buffers)))))
11478
11479 (defun gnus-summary-setup-default-charset ()
11480   "Setup newsgroup default charset."
11481   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11482       (setq gnus-newsgroup-charset nil)
11483     (let* ((ignored-charsets
11484             (or gnus-newsgroup-ephemeral-ignored-charsets
11485                 (append
11486                  (and gnus-newsgroup-name
11487                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11488                  gnus-newsgroup-ignored-charsets))))
11489       (setq gnus-newsgroup-charset
11490             (or gnus-newsgroup-ephemeral-charset
11491                 (and gnus-newsgroup-name
11492                      (gnus-parameter-charset gnus-newsgroup-name))
11493                 gnus-default-charset))
11494       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11495            ignored-charsets))))
11496
11497 ;;;
11498 ;;; Mime Commands
11499 ;;;
11500
11501 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11502   "Display the current article buffer fully MIME-buttonized.
11503 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11504 treated as multipart/mixed."
11505   (interactive "P")
11506   (require 'gnus-art)
11507   (let ((gnus-unbuttonized-mime-types nil)
11508         (gnus-mime-display-multipart-as-mixed show-all-parts))
11509     (gnus-summary-show-article)))
11510
11511 (defun gnus-summary-repair-multipart (article)
11512   "Add a Content-Type header to a multipart article without one."
11513   (interactive (list (gnus-summary-article-number)))
11514   (gnus-with-article article
11515     (message-narrow-to-head)
11516     (message-remove-header "Mime-Version")
11517     (goto-char (point-max))
11518     (insert "Mime-Version: 1.0\n")
11519     (widen)
11520     (when (search-forward "\n--" nil t)
11521       (let ((separator (buffer-substring (point) (point-at-eol))))
11522         (message-narrow-to-head)
11523         (message-remove-header "Content-Type")
11524         (goto-char (point-max))
11525         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11526                         separator))
11527         (widen))))
11528   (let (gnus-mark-article-hook)
11529     (gnus-summary-select-article t t nil article)))
11530
11531 (defun gnus-summary-toggle-display-buttonized ()
11532   "Toggle the buttonizing of the article buffer."
11533   (interactive)
11534   (require 'gnus-art)
11535   (if (setq gnus-inhibit-mime-unbuttonizing
11536             (not gnus-inhibit-mime-unbuttonizing))
11537       (let ((gnus-unbuttonized-mime-types nil))
11538         (gnus-summary-show-article))
11539     (gnus-summary-show-article)))
11540
11541 ;;;
11542 ;;; Generic summary marking commands
11543 ;;;
11544
11545 (defvar gnus-summary-marking-alist
11546   '((read gnus-del-mark "d")
11547     (unread gnus-unread-mark "u")
11548     (ticked gnus-ticked-mark "!")
11549     (dormant gnus-dormant-mark "?")
11550     (expirable gnus-expirable-mark "e"))
11551   "An alist of names/marks/keystrokes.")
11552
11553 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11554 (defvar gnus-summary-mark-map)
11555
11556 (defun gnus-summary-make-all-marking-commands ()
11557   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11558   (dolist (elem gnus-summary-marking-alist)
11559     (apply 'gnus-summary-make-marking-command elem)))
11560
11561 (defun gnus-summary-make-marking-command (name mark keystroke)
11562   (let ((map (make-sparse-keymap)))
11563     (define-key gnus-summary-generic-mark-map keystroke map)
11564     (dolist (lway `((next "next" next nil "n")
11565                     (next-unread "next unread" next t "N")
11566                     (prev "previous" prev nil "p")
11567                     (prev-unread "previous unread" prev t "P")
11568                     (nomove "" nil nil ,keystroke)))
11569       (let ((func (gnus-summary-make-marking-command-1
11570                    mark (car lway) lway name)))
11571         (setq func (eval func))
11572         (define-key map (nth 4 lway) func)))))
11573
11574 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11575   `(defun ,(intern
11576             (format "gnus-summary-put-mark-as-%s%s"
11577                     name (if (eq way 'nomove)
11578                              ""
11579                            (concat "-" (symbol-name way)))))
11580      (n)
11581      ,(format
11582        "Mark the current article as %s%s.
11583 If N, the prefix, then repeat N times.
11584 If N is negative, move in reverse order.
11585 The difference between N and the actual number of articles marked is
11586 returned."
11587        name (cadr lway))
11588      (interactive "p")
11589      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11590
11591 (defun gnus-summary-generic-mark (n mark move unread)
11592   "Mark N articles with MARK."
11593   (unless (eq major-mode 'gnus-summary-mode)
11594     (error "This command can only be used in the summary buffer"))
11595   (gnus-summary-show-thread)
11596   (let ((nummove
11597          (cond
11598           ((eq move 'next) 1)
11599           ((eq move 'prev) -1)
11600           (t 0))))
11601     (if (zerop nummove)
11602         (setq n 1)
11603       (when (< n 0)
11604         (setq n (abs n)
11605               nummove (* -1 nummove))))
11606     (while (and (> n 0)
11607                 (gnus-summary-mark-article nil mark)
11608                 (zerop (gnus-summary-next-subject nummove unread t)))
11609       (setq n (1- n)))
11610     (when (/= 0 n)
11611       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11612     (gnus-summary-recenter)
11613     (gnus-summary-position-point)
11614     (gnus-set-mode-line 'summary)
11615     n))
11616
11617 (defun gnus-summary-insert-articles (articles)
11618   (when (setq articles
11619               (gnus-sorted-difference articles
11620                                       (mapcar (lambda (h)
11621                                                 (mail-header-number h))
11622                                               gnus-newsgroup-headers)))
11623     (setq gnus-newsgroup-headers
11624           (gnus-merge 'list
11625                       gnus-newsgroup-headers
11626                       (gnus-fetch-headers articles)
11627                       'gnus-article-sort-by-number))
11628     ;; Suppress duplicates?
11629     (when gnus-suppress-duplicates
11630       (gnus-dup-suppress-articles))
11631
11632     ;; We might want to build some more threads first.
11633     (when (and gnus-fetch-old-headers
11634                (eq gnus-headers-retrieved-by 'nov))
11635       (if (eq gnus-fetch-old-headers 'invisible)
11636           (gnus-build-all-threads)
11637         (gnus-build-old-threads)))
11638     ;; Let the Gnus agent mark articles as read.
11639     (when gnus-agent
11640       (gnus-agent-get-undownloaded-list))
11641     ;; Remove list identifiers from subject
11642     (when gnus-list-identifiers
11643       (gnus-summary-remove-list-identifiers))
11644     ;; First and last article in this newsgroup.
11645     (when gnus-newsgroup-headers
11646       (setq gnus-newsgroup-begin
11647             (mail-header-number (car gnus-newsgroup-headers))
11648             gnus-newsgroup-end
11649             (mail-header-number
11650              (gnus-last-element gnus-newsgroup-headers))))
11651     (when gnus-use-scoring
11652       (gnus-possibly-score-headers))))
11653
11654 (defun gnus-summary-insert-old-articles (&optional all)
11655   "Insert all old articles in this group.
11656 If ALL is non-nil, already read articles become readable.
11657 If ALL is a number, fetch this number of articles."
11658   (interactive "P")
11659   (prog1
11660       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11661             older len)
11662         (setq older
11663               ;; Some nntp servers lie about their active range.  When
11664               ;; this happens, the active range can be in the millions.
11665               ;; Use a compressed range to avoid creating a huge list.
11666               (gnus-range-difference (list gnus-newsgroup-active) old))
11667         (setq len (gnus-range-length older))
11668         (cond
11669          ((null older) nil)
11670          ((numberp all)
11671           (if (< all len)
11672               (let ((older-range (nreverse older)))
11673                 (setq older nil)
11674
11675                 (while (> all 0)
11676                   (let* ((r (pop older-range))
11677                          (min (if (numberp r) r (car r)))
11678                          (max (if (numberp r) r (cdr r))))
11679                     (while (and (<= min max)
11680                                 (> all 0))
11681                       (push max older)
11682                       (setq all (1- all)
11683                             max (1- max))))))
11684             (setq older (gnus-uncompress-range older))))
11685          (all
11686           (setq older (gnus-uncompress-range older)))
11687          (t
11688           (when (and (numberp gnus-large-newsgroup)
11689                    (> len gnus-large-newsgroup))
11690               (let* ((cursor-in-echo-area nil)
11691                      (initial (gnus-parameter-large-newsgroup-initial
11692                                gnus-newsgroup-name))
11693                      (input
11694                       (read-string
11695                        (format
11696                         "How many articles from %s (%s %d): "
11697                         (gnus-limit-string
11698                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11699                         (if initial "max" "default")
11700                         len)
11701                        (if initial
11702                            (cons (number-to-string initial)
11703                                  0)))))
11704                 (unless (string-match "^[ \t]*$" input)
11705                   (setq all (string-to-number input))
11706                   (if (< all len)
11707                       (let ((older-range (nreverse older)))
11708                         (setq older nil)
11709
11710                         (while (> all 0)
11711                           (let* ((r (pop older-range))
11712                                  (min (if (numberp r) r (car r)))
11713                                  (max (if (numberp r) r (cdr r))))
11714                             (while (and (<= min max)
11715                                         (> all 0))
11716                               (push max older)
11717                               (setq all (1- all)
11718                                     max (1- max))))))))))
11719           (setq older (gnus-uncompress-range older))))
11720         (if (not older)
11721             (message "No old news.")
11722           (gnus-summary-insert-articles older)
11723           (gnus-summary-limit (gnus-sorted-nunion old older))))
11724     (gnus-summary-position-point)))
11725
11726 (defun gnus-summary-insert-new-articles ()
11727   "Insert all new articles in this group."
11728   (interactive)
11729   (prog1
11730       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11731             (old-active gnus-newsgroup-active)
11732             (nnmail-fetched-sources (list t))
11733             i new)
11734         (setq gnus-newsgroup-active
11735               (gnus-activate-group gnus-newsgroup-name 'scan))
11736         (setq i (cdr gnus-newsgroup-active))
11737         (while (> i (cdr old-active))
11738           (push i new)
11739           (decf i))
11740         (if (not new)
11741             (message "No gnus is bad news")
11742           (gnus-summary-insert-articles new)
11743           (setq gnus-newsgroup-unreads
11744                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11745           (gnus-summary-limit (gnus-sorted-nunion old new))))
11746     (gnus-summary-position-point)))
11747
11748 (gnus-summary-make-all-marking-commands)
11749
11750 (gnus-ems-redefine)
11751
11752 (provide 'gnus-sum)
11753
11754 (run-hooks 'gnus-sum-load-hook)
11755
11756 ;; Local Variables:
11757 ;; coding: iso-8859-1
11758 ;; End:
11759
11760 ;;; gnus-sum.el ends here