(gnus-summary-insert-subject): Remove list identifiers.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary 2
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715
716 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
717   "*List of functions used for sorting articles in the summary buffer.
718
719 Each function takes two articles and returns non-nil if the first
720 article should be sorted before the other.  If you use more than one
721 function, the primary sort function should be the last.  You should
722 probably always include `gnus-article-sort-by-number' in the list of
723 sorting functions -- preferably first.  Also note that sorting by date
724 is often much slower than sorting by number, and the sorting order is
725 very similar.  (Sorting by date means sorting by the time the message
726 was sent, sorting by number means sorting by arrival time.)
727
728 Each item can also be a list `(not F)' where F is a function;
729 this reverses the sort order.
730
731 Ready-made functions include `gnus-article-sort-by-number',
732 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
733 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
734 and `gnus-article-sort-by-score'.
735
736 When threading is turned on, the variable `gnus-thread-sort-functions'
737 controls how articles are sorted."
738   :group 'gnus-summary-sort
739   :type '(repeat (gnus-widget-reversible
740                   (choice (function-item gnus-article-sort-by-number)
741                           (function-item gnus-article-sort-by-author)
742                           (function-item gnus-article-sort-by-subject)
743                           (function-item gnus-article-sort-by-date)
744                           (function-item gnus-article-sort-by-score)
745                           (function-item gnus-article-sort-by-random)
746                           (function :tag "other"))
747                   (boolean :tag "Reverse order"))))
748
749
750 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
751   "*List of functions used for sorting threads in the summary buffer.
752 By default, threads are sorted by article number.
753
754 Each function takes two threads and returns non-nil if the first
755 thread should be sorted before the other.  If you use more than one
756 function, the primary sort function should be the last.  You should
757 probably always include `gnus-thread-sort-by-number' in the list of
758 sorting functions -- preferably first.  Also note that sorting by date
759 is often much slower than sorting by number, and the sorting order is
760 very similar.  (Sorting by date means sorting by the time the message
761 was sent, sorting by number means sorting by arrival time.)
762
763 Each list item can also be a list `(not F)' where F is a
764 function; this specifies reversed sort order.
765
766 Ready-made functions include `gnus-thread-sort-by-number',
767 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
768 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
769 `gnus-thread-sort-by-most-recent-number',
770 `gnus-thread-sort-by-most-recent-date',
771 `gnus-thread-sort-by-random', and
772 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
773
774 When threading is turned off, the variable
775 `gnus-article-sort-functions' controls how articles are sorted."
776   :group 'gnus-summary-sort
777   :type '(repeat 
778           (gnus-widget-reversible
779            (choice (function-item gnus-thread-sort-by-number)
780                    (function-item gnus-thread-sort-by-author)
781                    (function-item gnus-thread-sort-by-subject)
782                    (function-item gnus-thread-sort-by-date)
783                    (function-item gnus-thread-sort-by-score)
784                    (function-item gnus-thread-sort-by-most-recent-number)
785                    (function-item gnus-thread-sort-by-most-recent-date)
786                    (function-item gnus-thread-sort-by-random)
787                    (function-item gnus-thread-sort-by-total-score)
788                    (function :tag "other"))
789            (boolean :tag "Reverse order"))))
790
791 (defcustom gnus-thread-score-function '+
792   "*Function used for calculating the total score of a thread.
793
794 The function is called with the scores of the article and each
795 subthread and should then return the score of the thread.
796
797 Some functions you can use are `+', `max', or `min'."
798   :group 'gnus-summary-sort
799   :type 'function)
800
801 (defcustom gnus-summary-expunge-below nil
802   "All articles that have a score less than this variable will be expunged.
803 This variable is local to the summary buffers."
804   :group 'gnus-score-default
805   :type '(choice (const :tag "off" nil)
806                  integer))
807
808 (defcustom gnus-thread-expunge-below nil
809   "All threads that have a total score less than this variable will be expunged.
810 See `gnus-thread-score-function' for en explanation of what a
811 \"thread score\" is.
812
813 This variable is local to the summary buffers."
814   :group 'gnus-threading
815   :group 'gnus-score-default
816   :type '(choice (const :tag "off" nil)
817                  integer))
818
819 (defcustom gnus-summary-mode-hook nil
820   "*A hook for Gnus summary mode.
821 This hook is run before any variables are set in the summary buffer."
822   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
823   :group 'gnus-summary-various
824   :type 'hook)
825
826 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
827 (when (featurep 'xemacs)
828   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
830   (add-hook 'gnus-summary-mode-hook
831             'gnus-xmas-switch-horizontal-scrollbar-off))
832
833 (defcustom gnus-summary-menu-hook nil
834   "*Hook run after the creation of the summary mode menu."
835   :group 'gnus-summary-visual
836   :type 'hook)
837
838 (defcustom gnus-summary-exit-hook nil
839   "*A hook called on exit from the summary buffer.
840 It will be called with point in the group buffer."
841   :group 'gnus-summary-exit
842   :type 'hook)
843
844 (defcustom gnus-summary-prepare-hook nil
845   "*A hook called after the summary buffer has been generated.
846 If you want to modify the summary buffer, you can use this hook."
847   :group 'gnus-summary-various
848   :type 'hook)
849
850 (defcustom gnus-summary-prepared-hook nil
851   "*A hook called as the last thing after the summary buffer has been generated."
852   :group 'gnus-summary-various
853   :type 'hook)
854
855 (defcustom gnus-summary-generate-hook nil
856   "*A hook run just before generating the summary buffer.
857 This hook is commonly used to customize threading variables and the
858 like."
859   :group 'gnus-summary-various
860   :type 'hook)
861
862 (defcustom gnus-select-group-hook nil
863   "*A hook called when a newsgroup is selected.
864
865 If you'd like to simplify subjects like the
866 `gnus-summary-next-same-subject' command does, you can use the
867 following hook:
868
869  (add-hook gnus-select-group-hook
870            (lambda ()
871              (mapcar (lambda (header)
872                        (mail-header-set-subject
873                         header
874                         (gnus-simplify-subject
875                          (mail-header-subject header) 're-only)))
876                      gnus-newsgroup-headers)))"
877   :group 'gnus-group-select
878   :type 'hook)
879
880 (defcustom gnus-select-article-hook nil
881   "*A hook called when an article is selected."
882   :group 'gnus-summary-choose
883   :options '(gnus-agent-fetch-selected-article)
884   :type 'hook)
885
886 (defcustom gnus-visual-mark-article-hook
887   (list 'gnus-highlight-selected-summary)
888   "*Hook run after selecting an article in the summary buffer.
889 It is meant to be used for highlighting the article in some way.  It
890 is not run if `gnus-visual' is nil."
891   :group 'gnus-summary-visual
892   :type 'hook)
893
894 (defcustom gnus-parse-headers-hook nil
895   "*A hook called before parsing the headers."
896   :group 'gnus-various
897   :type 'hook)
898
899 (defcustom gnus-exit-group-hook nil
900   "*A hook called when exiting summary mode.
901 This hook is not called from the non-updating exit commands like `Q'."
902   :group 'gnus-various
903   :type 'hook)
904
905 (defcustom gnus-summary-update-hook
906   (list 'gnus-summary-highlight-line)
907   "*A hook called when a summary line is changed.
908 The hook will not be called if `gnus-visual' is nil.
909
910 The default function `gnus-summary-highlight-line' will
911 highlight the line according to the `gnus-summary-highlight'
912 variable."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
917   "*A hook called when an article is selected for the first time.
918 The hook is intended to mark an article as read (or unread)
919 automatically when it is selected."
920   :group 'gnus-summary-choose
921   :type 'hook)
922
923 (defcustom gnus-group-no-more-groups-hook nil
924   "*A hook run when returning to group mode having no more (unread) groups."
925   :group 'gnus-group-select
926   :type 'hook)
927
928 (defcustom gnus-ps-print-hook nil
929   "*A hook run before ps-printing something from Gnus."
930   :group 'gnus-summary
931   :type 'hook)
932
933 (defcustom gnus-summary-article-move-hook nil
934   "*A hook called after an article is moved, copied, respooled, or crossposted."
935   :group 'gnus-summary
936   :type 'hook)
937
938 (defcustom gnus-summary-article-delete-hook nil
939   "*A hook called after an article is deleted."
940   :group 'gnus-summary
941   :type 'hook)
942
943 (defcustom gnus-summary-article-expire-hook nil
944   "*A hook called after an article is expired."
945   :group 'gnus-summary
946   :type 'hook)
947
948 (defcustom gnus-summary-display-arrow
949   (and (fboundp 'display-graphic-p)
950        (display-graphic-p))
951   "*If non-nil, display an arrow highlighting the current article."
952   :version "21.1"
953   :group 'gnus-summary
954   :type 'boolean)
955
956 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
957   "Face used for highlighting the current article in the summary buffer."
958   :group 'gnus-summary-visual
959   :type 'face)
960
961 (defvar gnus-tmp-downloaded nil)
962
963 (defcustom gnus-summary-highlight
964   '(((eq mark gnus-canceled-mark)
965      . gnus-summary-cancelled-face)
966     ((and uncached (> score default-high))
967      . gnus-summary-high-undownloaded-face)
968     ((and uncached (< score default-low))
969      . gnus-summary-low-undownloaded-face)
970     (uncached
971      . gnus-summary-normal-undownloaded-face)
972     ((and (> score default-high)
973           (or (eq mark gnus-dormant-mark)
974               (eq mark gnus-ticked-mark)))
975      . gnus-summary-high-ticked-face)
976     ((and (< score default-low)
977           (or (eq mark gnus-dormant-mark)
978               (eq mark gnus-ticked-mark)))
979      . gnus-summary-low-ticked-face)
980     ((or (eq mark gnus-dormant-mark)
981          (eq mark gnus-ticked-mark))
982      . gnus-summary-normal-ticked-face)
983     ((and (> score default-high) (eq mark gnus-ancient-mark))
984      . gnus-summary-high-ancient-face)
985     ((and (< score default-low) (eq mark gnus-ancient-mark))
986      . gnus-summary-low-ancient-face)
987     ((eq mark gnus-ancient-mark)
988      . gnus-summary-normal-ancient-face)
989     ((and (> score default-high) (eq mark gnus-unread-mark))
990      . gnus-summary-high-unread-face)
991     ((and (< score default-low) (eq mark gnus-unread-mark))
992      . gnus-summary-low-unread-face)
993     ((eq mark gnus-unread-mark)
994      . gnus-summary-normal-unread-face)
995     ((> score default-high)
996      . gnus-summary-high-read-face)
997     ((< score default-low)
998      . gnus-summary-low-read-face)
999     (t
1000      . gnus-summary-normal-read-face))
1001   "*Controls the highlighting of summary buffer lines.
1002
1003 A list of (FORM . FACE) pairs.  When deciding how a a particular
1004 summary line should be displayed, each form is evaluated.  The content
1005 of the face field after the first true form is used.  You can change
1006 how those summary lines are displayed, by editing the face field.
1007
1008 You can use the following variables in the FORM field.
1009
1010 score:        The article's score
1011 default:      The default article score.
1012 default-high: The default score for high scored articles.
1013 default-low:  The default score for low scored articles.
1014 below:        The score below which articles are automatically marked as read.
1015 mark:         The article's mark.
1016 uncached:     Non-nil if the article is uncached."
1017   :group 'gnus-summary-visual
1018   :type '(repeat (cons (sexp :tag "Form" nil)
1019                        face)))
1020
1021 (defcustom gnus-alter-header-function nil
1022   "Function called to allow alteration of article header structures.
1023 The function is called with one parameter, the article header vector,
1024 which it may alter in any way."
1025   :type '(choice (const :tag "None" nil)
1026                  function)
1027   :group 'gnus-summary)
1028
1029 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1030   "Variable that says which function should be used to decode a string with encoded words.")
1031
1032 (defcustom gnus-extra-headers '(To Newsgroups)
1033   "*Extra headers to parse."
1034   :version "21.1"
1035   :group 'gnus-summary
1036   :type '(repeat symbol))
1037
1038 (defcustom gnus-ignored-from-addresses
1039   (and user-mail-address (regexp-quote user-mail-address))
1040   "*Regexp of From headers that may be suppressed in favor of To headers."
1041   :version "21.1"
1042   :group 'gnus-summary
1043   :type 'regexp)
1044
1045 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1046   "List of charsets that should be ignored.
1047 When these charsets are used in the \"charset\" parameter, the
1048 default charset will be used instead."
1049   :version "21.1"
1050   :type '(repeat symbol)
1051   :group 'gnus-charset)
1052
1053 (gnus-define-group-parameter
1054  ignored-charsets
1055  :type list
1056  :function-document
1057  "Return the ignored charsets of GROUP."
1058  :variable gnus-group-ignored-charsets-alist
1059  :variable-default
1060  '(("alt\\.chinese\\.text" iso-8859-1))
1061  :variable-document
1062  "Alist of regexps (to match group names) and charsets that should be ignored.
1063 When these charsets are used in the \"charset\" parameter, the
1064 default charset will be used instead."
1065  :variable-group gnus-charset
1066  :variable-type '(repeat (cons (regexp :tag "Group")
1067                                (repeat symbol)))
1068  :parameter-type '(choice :tag "Ignored charsets"
1069                           :value nil
1070                           (repeat (symbol)))
1071  :parameter-document       "\
1072 List of charsets that should be ignored.
1073
1074 When these charsets are used in the \"charset\" parameter, the
1075 default charset will be used instead.")
1076
1077 (defcustom gnus-group-highlight-words-alist nil
1078   "Alist of group regexps and highlight regexps.
1079 This variable uses the same syntax as `gnus-emphasis-alist'."
1080   :version "21.1"
1081   :type '(repeat (cons (regexp :tag "Group")
1082                        (repeat (list (regexp :tag "Highlight regexp")
1083                                      (number :tag "Group for entire word" 0)
1084                                      (number :tag "Group for displayed part" 0)
1085                                      (symbol :tag "Face"
1086                                              gnus-emphasis-highlight-words)))))
1087   :group 'gnus-summary-visual)
1088
1089 (defcustom gnus-summary-show-article-charset-alist
1090   nil
1091   "Alist of number and charset.
1092 The article will be shown with the charset corresponding to the
1093 numbered argument.
1094 For example: ((1 . cn-gb-2312) (2 . big5))."
1095   :version "21.1"
1096   :type '(repeat (cons (number :tag "Argument" 1)
1097                        (symbol :tag "Charset")))
1098   :group 'gnus-charset)
1099
1100 (defcustom gnus-preserve-marks t
1101   "Whether marks are preserved when moving, copying and respooling messages."
1102   :version "21.1"
1103   :type 'boolean
1104   :group 'gnus-summary-marks)
1105
1106 (defcustom gnus-alter-articles-to-read-function nil
1107   "Function to be called to alter the list of articles to be selected."
1108   :type '(choice (const nil) function)
1109   :group 'gnus-summary)
1110
1111 (defcustom gnus-orphan-score nil
1112   "*All orphans get this score added.  Set in the score file."
1113   :group 'gnus-score-default
1114   :type '(choice (const nil)
1115                  integer))
1116
1117 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1118   "*A regexp to match MIME parts when saving multiple parts of a
1119 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1120 This regexp will be used by default when prompting the user for which
1121 type of files to save."
1122   :group 'gnus-summary
1123   :type 'regexp)
1124
1125 (defcustom gnus-read-all-available-headers nil
1126   "Whether Gnus should parse all headers made available to it.
1127 This is mostly relevant for slow back ends where the user may
1128 wish to widen the summary buffer to include all headers
1129 that were fetched.  Say, for nnultimate groups."
1130   :group 'gnus-summary
1131   :type '(choice boolean regexp))
1132
1133 (defcustom gnus-summary-muttprint-program "muttprint"
1134   "Command (and optional arguments) used to run Muttprint."
1135   :version "21.3"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-article-loose-mime t
1140   "If non-nil, don't require MIME-Version header.
1141 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1142 supply the MIME-Version header or deliberately strip it from the mail.
1143 If non-nil (the default), Gnus will treat some articles as MIME
1144 even if the MIME-Version header is missing."
1145   :version "21.3"
1146   :type 'boolean
1147   :group 'gnus-article-mime)
1148
1149 (defcustom gnus-article-emulate-mime t
1150   "If non-nil, use MIME emulation for uuencode and the like.
1151 This means that Gnus will search message bodies for text that look
1152 like uuencoded bits, yEncoded bits, and so on, and present that using
1153 the normal Gnus MIME machinery."
1154   :type 'boolean
1155   :group 'gnus-article-mime)
1156
1157 ;;; Internal variables
1158
1159 (defvar gnus-summary-display-cache nil)
1160 (defvar gnus-article-mime-handles nil)
1161 (defvar gnus-article-decoded-p nil)
1162 (defvar gnus-article-charset nil)
1163 (defvar gnus-article-ignored-charsets nil)
1164 (defvar gnus-scores-exclude-files nil)
1165 (defvar gnus-page-broken nil)
1166
1167 (defvar gnus-original-article nil)
1168 (defvar gnus-article-internal-prepare-hook nil)
1169 (defvar gnus-newsgroup-process-stack nil)
1170
1171 (defvar gnus-thread-indent-array nil)
1172 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1173 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1174   "Function called to sort the articles within a thread after it has been gathered together.")
1175
1176 (defvar gnus-summary-save-parts-type-history nil)
1177 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1178
1179 ;; Avoid highlighting in kill files.
1180 (defvar gnus-summary-inhibit-highlight nil)
1181 (defvar gnus-newsgroup-selected-overlay nil)
1182 (defvar gnus-inhibit-limiting nil)
1183 (defvar gnus-newsgroup-adaptive-score-file nil)
1184 (defvar gnus-current-score-file nil)
1185 (defvar gnus-current-move-group nil)
1186 (defvar gnus-current-copy-group nil)
1187 (defvar gnus-current-crosspost-group nil)
1188 (defvar gnus-newsgroup-display nil)
1189
1190 (defvar gnus-newsgroup-dependencies nil)
1191 (defvar gnus-newsgroup-adaptive nil)
1192 (defvar gnus-summary-display-article-function nil)
1193 (defvar gnus-summary-highlight-line-function nil
1194   "Function called after highlighting a summary line.")
1195
1196 (defvar gnus-summary-line-format-alist
1197   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1198     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1199     (?s gnus-tmp-subject-or-nil ?s)
1200     (?n gnus-tmp-name ?s)
1201     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1202         ?s)
1203     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1204             gnus-tmp-from) ?s)
1205     (?F gnus-tmp-from ?s)
1206     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1207     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1208     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1209     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1210     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1211     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1212     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1213     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1214     (?L gnus-tmp-lines ?s)
1215     (?O gnus-tmp-downloaded ?c)
1216     (?I gnus-tmp-indentation ?s)
1217     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1218     (?R gnus-tmp-replied ?c)
1219     (?\[ gnus-tmp-opening-bracket ?c)
1220     (?\] gnus-tmp-closing-bracket ?c)
1221     (?\> (make-string gnus-tmp-level ? ) ?s)
1222     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1223     (?i gnus-tmp-score ?d)
1224     (?z gnus-tmp-score-char ?c)
1225     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1226     (?U gnus-tmp-unread ?c)
1227     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1228         ?s)
1229     (?t (gnus-summary-number-of-articles-in-thread
1230          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1231         ?d)
1232     (?e (gnus-summary-number-of-articles-in-thread
1233          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1234         ?c)
1235     (?u gnus-tmp-user-defined ?s)
1236     (?P (gnus-pick-line-number) ?d)
1237     (?B gnus-tmp-thread-tree-header-string ?s)
1238     (user-date (gnus-user-date
1239                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1240   "An alist of format specifications that can appear in summary lines.
1241 These are paired with what variables they correspond with, along with
1242 the type of the variable (string, integer, character, etc).")
1243
1244 (defvar gnus-summary-dummy-line-format-alist
1245   `((?S gnus-tmp-subject ?s)
1246     (?N gnus-tmp-number ?d)
1247     (?u gnus-tmp-user-defined ?s)))
1248
1249 (defvar gnus-summary-mode-line-format-alist
1250   `((?G gnus-tmp-group-name ?s)
1251     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1252     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1253     (?A gnus-tmp-article-number ?d)
1254     (?Z gnus-tmp-unread-and-unselected ?s)
1255     (?V gnus-version ?s)
1256     (?U gnus-tmp-unread-and-unticked ?d)
1257     (?S gnus-tmp-subject ?s)
1258     (?e gnus-tmp-unselected ?d)
1259     (?u gnus-tmp-user-defined ?s)
1260     (?d (length gnus-newsgroup-dormant) ?d)
1261     (?t (length gnus-newsgroup-marked) ?d)
1262     (?h (length gnus-newsgroup-spam-marked) ?d)
1263     (?r (length gnus-newsgroup-reads) ?d)
1264     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1265     (?E gnus-newsgroup-expunged-tally ?d)
1266     (?s (gnus-current-score-file-nondirectory) ?s)))
1267
1268 (defvar gnus-last-search-regexp nil
1269   "Default regexp for article search command.")
1270
1271 (defvar gnus-last-shell-command nil
1272   "Default shell command on article.")
1273
1274 (defvar gnus-newsgroup-agentized nil
1275   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1276 (defvar gnus-newsgroup-begin nil)
1277 (defvar gnus-newsgroup-end nil)
1278 (defvar gnus-newsgroup-last-rmail nil)
1279 (defvar gnus-newsgroup-last-mail nil)
1280 (defvar gnus-newsgroup-last-folder nil)
1281 (defvar gnus-newsgroup-last-file nil)
1282 (defvar gnus-newsgroup-auto-expire nil)
1283 (defvar gnus-newsgroup-active nil)
1284
1285 (defvar gnus-newsgroup-data nil)
1286 (defvar gnus-newsgroup-data-reverse nil)
1287 (defvar gnus-newsgroup-limit nil)
1288 (defvar gnus-newsgroup-limits nil)
1289 (defvar gnus-summary-use-undownloaded-faces nil)
1290
1291 (defvar gnus-newsgroup-unreads nil
1292   "Sorted list of unread articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unselected nil
1295   "Sorted list of unselected unread articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-reads nil
1298   "Alist of read articles and article marks in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-expunged-tally nil)
1301
1302 (defvar gnus-newsgroup-marked nil
1303   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1304
1305 (defvar gnus-newsgroup-spam-marked nil
1306   "List of ranges of articles that have been marked as spam.")
1307
1308 (defvar gnus-newsgroup-killed nil
1309   "List of ranges of articles that have been through the scoring process.")
1310
1311 (defvar gnus-newsgroup-cached nil
1312   "Sorted list of articles that come from the article cache.")
1313
1314 (defvar gnus-newsgroup-saved nil
1315   "List of articles that have been saved.")
1316
1317 (defvar gnus-newsgroup-kill-headers nil)
1318
1319 (defvar gnus-newsgroup-replied nil
1320   "List of articles that have been replied to in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-forwarded nil
1323   "List of articles that have been forwarded in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-recent nil
1326   "List of articles that have are recent in the current newsgroup.")
1327
1328 (defvar gnus-newsgroup-expirable nil
1329   "Sorted list of articles in the current newsgroup that can be expired.")
1330
1331 (defvar gnus-newsgroup-processable nil
1332   "List of articles in the current newsgroup that can be processed.")
1333
1334 (defvar gnus-newsgroup-downloadable nil
1335   "Sorted list of articles in the current newsgroup that can be processed.")
1336
1337 (defvar gnus-newsgroup-unfetched nil
1338   "Sorted list of articles in the current newsgroup whose headers have
1339 not been fetched into the agent.
1340
1341 This list will always be a subset of gnus-newsgroup-undownloaded.")
1342
1343 (defvar gnus-newsgroup-undownloaded nil
1344   "List of articles in the current newsgroup that haven't been downloaded.")
1345
1346 (defvar gnus-newsgroup-unsendable nil
1347   "List of articles in the current newsgroup that won't be sent.")
1348
1349 (defvar gnus-newsgroup-bookmarks nil
1350   "List of articles in the current newsgroup that have bookmarks.")
1351
1352 (defvar gnus-newsgroup-dormant nil
1353   "Sorted list of dormant articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-unseen nil
1356   "List of unseen articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-seen nil
1359   "Range of seen articles in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-articles nil
1362   "List of articles in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-scored nil
1365   "List of scored articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-headers nil
1368   "List of article headers in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-threads nil)
1371
1372 (defvar gnus-newsgroup-prepared nil
1373   "Whether the current group has been prepared properly.")
1374
1375 (defvar gnus-newsgroup-ancient nil
1376   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-sparse nil)
1379
1380 (defvar gnus-current-article nil)
1381 (defvar gnus-article-current nil)
1382 (defvar gnus-current-headers nil)
1383 (defvar gnus-have-all-headers nil)
1384 (defvar gnus-last-article nil)
1385 (defvar gnus-newsgroup-history nil)
1386 (defvar gnus-newsgroup-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1389
1390 (defvar gnus-article-before-search nil)
1391
1392 (defvar gnus-summary-local-variables
1393   '(gnus-newsgroup-name
1394     gnus-newsgroup-begin gnus-newsgroup-end
1395     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1396     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1397     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1398     gnus-newsgroup-unselected gnus-newsgroup-marked
1399     gnus-newsgroup-spam-marked
1400     gnus-newsgroup-reads gnus-newsgroup-saved
1401     gnus-newsgroup-replied gnus-newsgroup-forwarded
1402     gnus-newsgroup-recent
1403     gnus-newsgroup-expirable
1404     gnus-newsgroup-processable gnus-newsgroup-killed
1405     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1406     gnus-newsgroup-unfetched
1407     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1408     gnus-newsgroup-seen gnus-newsgroup-articles
1409     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1410     gnus-newsgroup-headers gnus-newsgroup-threads
1411     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1412     gnus-current-article gnus-current-headers gnus-have-all-headers
1413     gnus-last-article gnus-article-internal-prepare-hook
1414     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1415     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1416     gnus-thread-expunge-below
1417     gnus-score-alist gnus-current-score-file
1418     (gnus-summary-expunge-below . global)
1419     (gnus-summary-mark-below . global)
1420     (gnus-orphan-score . global)
1421     gnus-newsgroup-active gnus-scores-exclude-files
1422     gnus-newsgroup-history gnus-newsgroup-ancient
1423     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1424     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1425     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1426     (gnus-newsgroup-expunged-tally . 0)
1427     gnus-cache-removable-articles gnus-newsgroup-cached
1428     gnus-newsgroup-data gnus-newsgroup-data-reverse
1429     gnus-newsgroup-limit gnus-newsgroup-limits
1430     gnus-newsgroup-charset gnus-newsgroup-display
1431     gnus-summary-use-undownloaded-faces)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default expressions to be evalled (when the default
1438 values are not nil), that should be made global while the summary buffer
1439 is active.
1440
1441 Note: The default expressions will be evaluated (using function `eval')
1442 before assignment to the local variable rather than just assigned to it.
1443 If the default expression is the symbol `global', that symbol will not
1444 be evaluated but the global value of the local variable will be used
1445 instead.
1446
1447 These variables can be used to set variables in the group parameters
1448 while still allowing them to affect operations done in other buffers.
1449 For example:
1450
1451 \(setq gnus-newsgroup-variables
1452      '(message-use-followup-to
1453        (gnus-visible-headers .
1454          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1455 ")
1456
1457 ;; Byte-compiler warning.
1458 (eval-when-compile
1459   ;; Bind features so that require will believe that gnus-sum has
1460   ;; already been loaded (avoids infinite recursion)
1461   (let ((features (cons 'gnus-sum features)))
1462     ;; Several of the declarations in gnus-sum are needed to load the
1463     ;; following files. Right now, these definitions have been
1464     ;; compiled but not defined (evaluated).  We could either do a
1465     ;; eval-and-compile about all of the declarations or evaluate the
1466     ;; source file.
1467     (if (boundp 'gnus-newsgroup-variables)
1468         nil
1469       (load "gnus-sum.el" t t t))
1470     (require 'gnus)
1471     (require 'gnus-agent)
1472     (require 'gnus-art)))
1473
1474 ;; MIME stuff.
1475
1476 (defvar gnus-decode-encoded-word-methods
1477   '(mail-decode-encoded-word-string)
1478   "List of methods used to decode encoded words.
1479
1480 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1481 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1482 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1483 whose names match REGEXP.
1484
1485 For example:
1486 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1487  mail-decode-encoded-word-string
1488  (\"chinese\" . rfc1843-decode-string))")
1489
1490 (defvar gnus-decode-encoded-word-methods-cache nil)
1491
1492 (defun gnus-multi-decode-encoded-word-string (string)
1493   "Apply the functions from `gnus-encoded-word-methods' that match."
1494   (unless (and gnus-decode-encoded-word-methods-cache
1495                (eq gnus-newsgroup-name
1496                    (car gnus-decode-encoded-word-methods-cache)))
1497     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1498     (mapcar (lambda (x)
1499               (if (symbolp x)
1500                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1501                 (if (and gnus-newsgroup-name
1502                          (string-match (car x) gnus-newsgroup-name))
1503                     (nconc gnus-decode-encoded-word-methods-cache
1504                            (list (cdr x))))))
1505             gnus-decode-encoded-word-methods))
1506   (let ((xlist gnus-decode-encoded-word-methods-cache))
1507     (pop xlist)
1508     (while xlist
1509       (setq string (funcall (pop xlist) string))))
1510   string)
1511
1512 ;; Subject simplification.
1513
1514 (defun gnus-simplify-whitespace (str)
1515   "Remove excessive whitespace from STR."
1516   ;; Multiple spaces.
1517   (while (string-match "[ \t][ \t]+" str)
1518     (setq str (concat (substring str 0 (match-beginning 0))
1519                         " "
1520                         (substring str (match-end 0)))))
1521   ;; Leading spaces.
1522   (when (string-match "^[ \t]+" str)
1523     (setq str (substring str (match-end 0))))
1524   ;; Trailing spaces.
1525   (when (string-match "[ \t]+$" str)
1526     (setq str (substring str 0 (match-beginning 0))))
1527   str)
1528
1529 (defun gnus-simplify-all-whitespace (str)
1530   "Remove all whitespace from STR."
1531   (while (string-match "[ \t\n]+" str)
1532     (setq str (replace-match "" nil nil str)))
1533   str)
1534
1535 (defsubst gnus-simplify-subject-re (subject)
1536   "Remove \"Re:\" from subject lines."
1537   (if (string-match message-subject-re-regexp subject)
1538       (substring subject (match-end 0))
1539     subject))
1540
1541 (defun gnus-simplify-subject (subject &optional re-only)
1542   "Remove `Re:' and words in parentheses.
1543 If RE-ONLY is non-nil, strip leading `Re:'s only."
1544   (let ((case-fold-search t))           ;Ignore case.
1545     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1546     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1547       (setq subject (substring subject (match-end 0))))
1548     ;; Remove uninteresting prefixes.
1549     (when (and (not re-only)
1550                gnus-simplify-ignored-prefixes
1551                (string-match gnus-simplify-ignored-prefixes subject))
1552       (setq subject (substring subject (match-end 0))))
1553     ;; Remove words in parentheses from end.
1554     (unless re-only
1555       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1556         (setq subject (substring subject 0 (match-beginning 0)))))
1557     ;; Return subject string.
1558     subject))
1559
1560 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1561 ;; all whitespace.
1562 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1563   (goto-char (point-min))
1564   (while (re-search-forward regexp nil t)
1565     (replace-match (or newtext ""))))
1566
1567 (defun gnus-simplify-buffer-fuzzy ()
1568   "Simplify string in the buffer fuzzily.
1569 The string in the accessible portion of the current buffer is simplified.
1570 It is assumed to be a single-line subject.
1571 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1572 matter is removed.  Additional things can be deleted by setting
1573 `gnus-simplify-subject-fuzzy-regexp'."
1574   (let ((case-fold-search t)
1575         (modified-tick))
1576     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1577
1578     (while (not (eq modified-tick (buffer-modified-tick)))
1579       (setq modified-tick (buffer-modified-tick))
1580       (cond
1581        ((listp gnus-simplify-subject-fuzzy-regexp)
1582         (mapcar 'gnus-simplify-buffer-fuzzy-step
1583                 gnus-simplify-subject-fuzzy-regexp))
1584        (gnus-simplify-subject-fuzzy-regexp
1585         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1586       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1587       (gnus-simplify-buffer-fuzzy-step
1588        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1589       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1590
1591     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1592     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1593     (gnus-simplify-buffer-fuzzy-step " $")
1594     (gnus-simplify-buffer-fuzzy-step "^ +")))
1595
1596 (defun gnus-simplify-subject-fuzzy (subject)
1597   "Simplify a subject string fuzzily.
1598 See `gnus-simplify-buffer-fuzzy' for details."
1599   (save-excursion
1600     (gnus-set-work-buffer)
1601     (let ((case-fold-search t))
1602       ;; Remove uninteresting prefixes.
1603       (when (and gnus-simplify-ignored-prefixes
1604                  (string-match gnus-simplify-ignored-prefixes subject))
1605         (setq subject (substring subject (match-end 0))))
1606       (insert subject)
1607       (inline (gnus-simplify-buffer-fuzzy))
1608       (buffer-string))))
1609
1610 (defsubst gnus-simplify-subject-fully (subject)
1611   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1612   (cond
1613    (gnus-simplify-subject-functions
1614     (gnus-map-function gnus-simplify-subject-functions subject))
1615    ((null gnus-summary-gather-subject-limit)
1616     (gnus-simplify-subject-re subject))
1617    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1618     (gnus-simplify-subject-fuzzy subject))
1619    ((numberp gnus-summary-gather-subject-limit)
1620     (gnus-limit-string (gnus-simplify-subject-re subject)
1621                        gnus-summary-gather-subject-limit))
1622    (t
1623     subject)))
1624
1625 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1626   "Check whether two subjects are equal.
1627 If optional argument SIMPLE-FIRST is t, first argument is already
1628 simplified."
1629   (cond
1630    ((null simple-first)
1631     (equal (gnus-simplify-subject-fully s1)
1632            (gnus-simplify-subject-fully s2)))
1633    (t
1634     (equal s1
1635            (gnus-simplify-subject-fully s2)))))
1636
1637 (defun gnus-summary-bubble-group ()
1638   "Increase the score of the current group.
1639 This is a handy function to add to `gnus-summary-exit-hook' to
1640 increase the score of each group you read."
1641   (gnus-group-add-score gnus-newsgroup-name))
1642
1643 \f
1644 ;;;
1645 ;;; Gnus summary mode
1646 ;;;
1647
1648 (put 'gnus-summary-mode 'mode-class 'special)
1649
1650 (defvar gnus-article-commands-menu)
1651
1652 ;; Non-orthogonal keys
1653
1654 (gnus-define-keys gnus-summary-mode-map
1655   " " gnus-summary-next-page
1656   "\177" gnus-summary-prev-page
1657   [delete] gnus-summary-prev-page
1658   [backspace] gnus-summary-prev-page
1659   "\r" gnus-summary-scroll-up
1660   "\M-\r" gnus-summary-scroll-down
1661   "n" gnus-summary-next-unread-article
1662   "p" gnus-summary-prev-unread-article
1663   "N" gnus-summary-next-article
1664   "P" gnus-summary-prev-article
1665   "\M-\C-n" gnus-summary-next-same-subject
1666   "\M-\C-p" gnus-summary-prev-same-subject
1667   "\M-n" gnus-summary-next-unread-subject
1668   "\M-p" gnus-summary-prev-unread-subject
1669   "." gnus-summary-first-unread-article
1670   "," gnus-summary-best-unread-article
1671   "\M-s" gnus-summary-search-article-forward
1672   "\M-r" gnus-summary-search-article-backward
1673   "<" gnus-summary-beginning-of-article
1674   ">" gnus-summary-end-of-article
1675   "j" gnus-summary-goto-article
1676   "^" gnus-summary-refer-parent-article
1677   "\M-^" gnus-summary-refer-article
1678   "u" gnus-summary-tick-article-forward
1679   "!" gnus-summary-tick-article-forward
1680   "U" gnus-summary-tick-article-backward
1681   "d" gnus-summary-mark-as-read-forward
1682   "D" gnus-summary-mark-as-read-backward
1683   "E" gnus-summary-mark-as-expirable
1684   "\M-u" gnus-summary-clear-mark-forward
1685   "\M-U" gnus-summary-clear-mark-backward
1686   "k" gnus-summary-kill-same-subject-and-select
1687   "\C-k" gnus-summary-kill-same-subject
1688   "\M-\C-k" gnus-summary-kill-thread
1689   "\M-\C-l" gnus-summary-lower-thread
1690   "e" gnus-summary-edit-article
1691   "#" gnus-summary-mark-as-processable
1692   "\M-#" gnus-summary-unmark-as-processable
1693   "\M-\C-t" gnus-summary-toggle-threads
1694   "\M-\C-s" gnus-summary-show-thread
1695   "\M-\C-h" gnus-summary-hide-thread
1696   "\M-\C-f" gnus-summary-next-thread
1697   "\M-\C-b" gnus-summary-prev-thread
1698   [(meta down)] gnus-summary-next-thread
1699   [(meta up)] gnus-summary-prev-thread
1700   "\M-\C-u" gnus-summary-up-thread
1701   "\M-\C-d" gnus-summary-down-thread
1702   "&" gnus-summary-execute-command
1703   "c" gnus-summary-catchup-and-exit
1704   "\C-w" gnus-summary-mark-region-as-read
1705   "\C-t" gnus-summary-toggle-truncation
1706   "?" gnus-summary-mark-as-dormant
1707   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1708   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1709   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1710   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1711   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1712   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1713   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1714   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1715   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1716   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1717   "=" gnus-summary-expand-window
1718   "\C-x\C-s" gnus-summary-reselect-current-group
1719   "\M-g" gnus-summary-rescan-group
1720   "w" gnus-summary-stop-page-breaking
1721   "\C-c\C-r" gnus-summary-caesar-message
1722   "f" gnus-summary-followup
1723   "F" gnus-summary-followup-with-original
1724   "C" gnus-summary-cancel-article
1725   "r" gnus-summary-reply
1726   "R" gnus-summary-reply-with-original
1727   "\C-c\C-f" gnus-summary-mail-forward
1728   "o" gnus-summary-save-article
1729   "\C-o" gnus-summary-save-article-mail
1730   "|" gnus-summary-pipe-output
1731   "\M-k" gnus-summary-edit-local-kill
1732   "\M-K" gnus-summary-edit-global-kill
1733   ;; "V" gnus-version
1734   "\C-c\C-d" gnus-summary-describe-group
1735   "q" gnus-summary-exit
1736   "Q" gnus-summary-exit-no-update
1737   "\C-c\C-i" gnus-info-find-node
1738   gnus-mouse-2 gnus-mouse-pick-article
1739   "m" gnus-summary-mail-other-window
1740   "a" gnus-summary-post-news
1741   "i" gnus-summary-news-other-window
1742   "x" gnus-summary-limit-to-unread
1743   "s" gnus-summary-isearch-article
1744   "t" gnus-summary-toggle-header
1745   "g" gnus-summary-show-article
1746   "l" gnus-summary-goto-last-article
1747   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1748   "\C-d" gnus-summary-enter-digest-group
1749   "\M-\C-d" gnus-summary-read-document
1750   "\M-\C-e" gnus-summary-edit-parameters
1751   "\M-\C-a" gnus-summary-customize-parameters
1752   "\C-c\C-b" gnus-bug
1753   "*" gnus-cache-enter-article
1754   "\M-*" gnus-cache-remove-article
1755   "\M-&" gnus-summary-universal-argument
1756   "\C-l" gnus-recenter
1757   "I" gnus-summary-increase-score
1758   "L" gnus-summary-lower-score
1759   "\M-i" gnus-symbolic-argument
1760   "h" gnus-summary-select-article-buffer
1761
1762   "b" gnus-article-view-part
1763   "\M-t" gnus-summary-toggle-display-buttonized
1764
1765   "V" gnus-summary-score-map
1766   "X" gnus-uu-extract-map
1767   "S" gnus-summary-send-map)
1768
1769 ;; Sort of orthogonal keymap
1770 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1771   "t" gnus-summary-tick-article-forward
1772   "!" gnus-summary-tick-article-forward
1773   "d" gnus-summary-mark-as-read-forward
1774   "r" gnus-summary-mark-as-read-forward
1775   "c" gnus-summary-clear-mark-forward
1776   " " gnus-summary-clear-mark-forward
1777   "e" gnus-summary-mark-as-expirable
1778   "x" gnus-summary-mark-as-expirable
1779   "?" gnus-summary-mark-as-dormant
1780   "b" gnus-summary-set-bookmark
1781   "B" gnus-summary-remove-bookmark
1782   "#" gnus-summary-mark-as-processable
1783   "\M-#" gnus-summary-unmark-as-processable
1784   "S" gnus-summary-limit-include-expunged
1785   "C" gnus-summary-catchup
1786   "H" gnus-summary-catchup-to-here
1787   "h" gnus-summary-catchup-from-here
1788   "\C-c" gnus-summary-catchup-all
1789   "k" gnus-summary-kill-same-subject-and-select
1790   "K" gnus-summary-kill-same-subject
1791   "P" gnus-uu-mark-map)
1792
1793 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1794   "c" gnus-summary-clear-above
1795   "u" gnus-summary-tick-above
1796   "m" gnus-summary-mark-above
1797   "k" gnus-summary-kill-below)
1798
1799 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1800   "/" gnus-summary-limit-to-subject
1801   "n" gnus-summary-limit-to-articles
1802   "w" gnus-summary-pop-limit
1803   "s" gnus-summary-limit-to-subject
1804   "a" gnus-summary-limit-to-author
1805   "u" gnus-summary-limit-to-unread
1806   "m" gnus-summary-limit-to-marks
1807   "M" gnus-summary-limit-exclude-marks
1808   "v" gnus-summary-limit-to-score
1809   "*" gnus-summary-limit-include-cached
1810   "D" gnus-summary-limit-include-dormant
1811   "T" gnus-summary-limit-include-thread
1812   "d" gnus-summary-limit-exclude-dormant
1813   "t" gnus-summary-limit-to-age
1814   "." gnus-summary-limit-to-unseen
1815   "x" gnus-summary-limit-to-extra
1816   "p" gnus-summary-limit-to-display-predicate
1817   "E" gnus-summary-limit-include-expunged
1818   "c" gnus-summary-limit-exclude-childless-dormant
1819   "C" gnus-summary-limit-mark-excluded-as-read
1820   "o" gnus-summary-insert-old-articles
1821   "N" gnus-summary-insert-new-articles
1822   "r" gnus-summary-limit-to-replied)
1823
1824 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1825   "n" gnus-summary-next-unread-article
1826   "p" gnus-summary-prev-unread-article
1827   "N" gnus-summary-next-article
1828   "P" gnus-summary-prev-article
1829   "\C-n" gnus-summary-next-same-subject
1830   "\C-p" gnus-summary-prev-same-subject
1831   "\M-n" gnus-summary-next-unread-subject
1832   "\M-p" gnus-summary-prev-unread-subject
1833   "f" gnus-summary-first-unread-article
1834   "b" gnus-summary-best-unread-article
1835   "j" gnus-summary-goto-article
1836   "g" gnus-summary-goto-subject
1837   "l" gnus-summary-goto-last-article
1838   "o" gnus-summary-pop-article)
1839
1840 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1841   "k" gnus-summary-kill-thread
1842   "l" gnus-summary-lower-thread
1843   "i" gnus-summary-raise-thread
1844   "T" gnus-summary-toggle-threads
1845   "t" gnus-summary-rethread-current
1846   "^" gnus-summary-reparent-thread
1847   "s" gnus-summary-show-thread
1848   "S" gnus-summary-show-all-threads
1849   "h" gnus-summary-hide-thread
1850   "H" gnus-summary-hide-all-threads
1851   "n" gnus-summary-next-thread
1852   "p" gnus-summary-prev-thread
1853   "u" gnus-summary-up-thread
1854   "o" gnus-summary-top-thread
1855   "d" gnus-summary-down-thread
1856   "#" gnus-uu-mark-thread
1857   "\M-#" gnus-uu-unmark-thread)
1858
1859 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1860   "g" gnus-summary-prepare
1861   "c" gnus-summary-insert-cached-articles
1862   "d" gnus-summary-insert-dormant-articles)
1863
1864 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1865   "c" gnus-summary-catchup-and-exit
1866   "C" gnus-summary-catchup-all-and-exit
1867   "E" gnus-summary-exit-no-update
1868   "Q" gnus-summary-exit
1869   "Z" gnus-summary-exit
1870   "n" gnus-summary-catchup-and-goto-next-group
1871   "R" gnus-summary-reselect-current-group
1872   "G" gnus-summary-rescan-group
1873   "N" gnus-summary-next-group
1874   "s" gnus-summary-save-newsrc
1875   "P" gnus-summary-prev-group)
1876
1877 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1878   " " gnus-summary-next-page
1879   "n" gnus-summary-next-page
1880   "\177" gnus-summary-prev-page
1881   [delete] gnus-summary-prev-page
1882   "p" gnus-summary-prev-page
1883   "\r" gnus-summary-scroll-up
1884   "\M-\r" gnus-summary-scroll-down
1885   "<" gnus-summary-beginning-of-article
1886   ">" gnus-summary-end-of-article
1887   "b" gnus-summary-beginning-of-article
1888   "e" gnus-summary-end-of-article
1889   "^" gnus-summary-refer-parent-article
1890   "r" gnus-summary-refer-parent-article
1891   "D" gnus-summary-enter-digest-group
1892   "R" gnus-summary-refer-references
1893   "T" gnus-summary-refer-thread
1894   "g" gnus-summary-show-article
1895   "s" gnus-summary-isearch-article
1896   "P" gnus-summary-print-article
1897   "M" gnus-mailing-list-insinuate
1898   "t" gnus-article-babel)
1899
1900 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1901   "b" gnus-article-add-buttons
1902   "B" gnus-article-add-buttons-to-head
1903   "o" gnus-article-treat-overstrike
1904   "e" gnus-article-emphasize
1905   "w" gnus-article-fill-cited-article
1906   "Q" gnus-article-fill-long-lines
1907   "C" gnus-article-capitalize-sentences
1908   "c" gnus-article-remove-cr
1909   "q" gnus-article-de-quoted-unreadable
1910   "6" gnus-article-de-base64-unreadable
1911   "Z" gnus-article-decode-HZ
1912   "A" gnus-article-treat-ansi-sequences
1913   "h" gnus-article-wash-html
1914   "u" gnus-article-unsplit-urls
1915   "s" gnus-summary-force-verify-and-decrypt
1916   "f" gnus-article-display-x-face
1917   "l" gnus-summary-stop-page-breaking
1918   "r" gnus-summary-caesar-message
1919   "m" gnus-summary-morse-message
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-treat-smiley
1922   "v" gnus-summary-verbose-headers
1923   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1924   "p" gnus-article-verify-x-pgp-sig
1925   "d" gnus-article-treat-dumbquotes)
1926
1927 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1928   ;; mnemonic: deuglif*Y*
1929   "u" gnus-article-outlook-unwrap-lines
1930   "a" gnus-article-outlook-repair-attribution
1931   "c" gnus-article-outlook-rearrange-citation
1932   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1933
1934 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1935   "a" gnus-article-hide
1936   "h" gnus-article-hide-headers
1937   "b" gnus-article-hide-boring-headers
1938   "s" gnus-article-hide-signature
1939   "c" gnus-article-hide-citation
1940   "C" gnus-article-hide-citation-in-followups
1941   "l" gnus-article-hide-list-identifiers
1942   "B" gnus-article-strip-banner
1943   "P" gnus-article-hide-pem
1944   "\C-c" gnus-article-hide-citation-maybe)
1945
1946 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1947   "a" gnus-article-highlight
1948   "h" gnus-article-highlight-headers
1949   "c" gnus-article-highlight-citation
1950   "s" gnus-article-highlight-signature)
1951
1952 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1953   "f" gnus-article-treat-fold-headers
1954   "u" gnus-article-treat-unfold-headers
1955   "n" gnus-article-treat-fold-newsgroups)
1956
1957 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1958   "x" gnus-article-display-x-face
1959   "d" gnus-article-display-face
1960   "s" gnus-treat-smiley
1961   "D" gnus-article-remove-images
1962   "f" gnus-treat-from-picon
1963   "m" gnus-treat-mail-picon
1964   "n" gnus-treat-newsgroups-picon)
1965
1966 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1967   "w" gnus-article-decode-mime-words
1968   "c" gnus-article-decode-charset
1969   "v" gnus-mime-view-all-parts
1970   "b" gnus-article-view-part)
1971
1972 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1973   "z" gnus-article-date-ut
1974   "u" gnus-article-date-ut
1975   "l" gnus-article-date-local
1976   "p" gnus-article-date-english
1977   "e" gnus-article-date-lapsed
1978   "o" gnus-article-date-original
1979   "i" gnus-article-date-iso8601
1980   "s" gnus-article-date-user)
1981
1982 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1983   "t" gnus-article-remove-trailing-blank-lines
1984   "l" gnus-article-strip-leading-blank-lines
1985   "m" gnus-article-strip-multiple-blank-lines
1986   "a" gnus-article-strip-blank-lines
1987   "A" gnus-article-strip-all-blank-lines
1988   "s" gnus-article-strip-leading-space
1989   "e" gnus-article-strip-trailing-space
1990   "w" gnus-article-remove-leading-whitespace)
1991
1992 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1993   "v" gnus-version
1994   "f" gnus-summary-fetch-faq
1995   "d" gnus-summary-describe-group
1996   "h" gnus-summary-describe-briefly
1997   "i" gnus-info-find-node
1998   "c" gnus-group-fetch-charter
1999   "C" gnus-group-fetch-control)
2000
2001 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2002   "e" gnus-summary-expire-articles
2003   "\M-\C-e" gnus-summary-expire-articles-now
2004   "\177" gnus-summary-delete-article
2005   [delete] gnus-summary-delete-article
2006   [backspace] gnus-summary-delete-article
2007   "m" gnus-summary-move-article
2008   "r" gnus-summary-respool-article
2009   "w" gnus-summary-edit-article
2010   "c" gnus-summary-copy-article
2011   "B" gnus-summary-crosspost-article
2012   "q" gnus-summary-respool-query
2013   "t" gnus-summary-respool-trace
2014   "i" gnus-summary-import-article
2015   "I" gnus-summary-create-article
2016   "p" gnus-summary-article-posted-p)
2017
2018 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2019   "o" gnus-summary-save-article
2020   "m" gnus-summary-save-article-mail
2021   "F" gnus-summary-write-article-file
2022   "r" gnus-summary-save-article-rmail
2023   "f" gnus-summary-save-article-file
2024   "b" gnus-summary-save-article-body-file
2025   "h" gnus-summary-save-article-folder
2026   "v" gnus-summary-save-article-vm
2027   "p" gnus-summary-pipe-output
2028   "P" gnus-summary-muttprint
2029   "s" gnus-soup-add-article)
2030
2031 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2032   "b" gnus-summary-display-buttonized
2033   "m" gnus-summary-repair-multipart
2034   "v" gnus-article-view-part
2035   "o" gnus-article-save-part
2036   "c" gnus-article-copy-part
2037   "C" gnus-article-view-part-as-charset
2038   "e" gnus-article-view-part-externally
2039   "E" gnus-article-encrypt-body
2040   "i" gnus-article-inline-part
2041   "|" gnus-article-pipe-part)
2042
2043 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2044   "p" gnus-summary-mark-as-processable
2045   "u" gnus-summary-unmark-as-processable
2046   "U" gnus-summary-unmark-all-processable
2047   "v" gnus-uu-mark-over
2048   "s" gnus-uu-mark-series
2049   "r" gnus-uu-mark-region
2050   "g" gnus-uu-unmark-region
2051   "R" gnus-uu-mark-by-regexp
2052   "G" gnus-uu-unmark-by-regexp
2053   "t" gnus-uu-mark-thread
2054   "T" gnus-uu-unmark-thread
2055   "a" gnus-uu-mark-all
2056   "b" gnus-uu-mark-buffer
2057   "S" gnus-uu-mark-sparse
2058   "k" gnus-summary-kill-process-mark
2059   "y" gnus-summary-yank-process-mark
2060   "w" gnus-summary-save-process-mark
2061   "i" gnus-uu-invert-processable)
2062
2063 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2064   ;;"x" gnus-uu-extract-any
2065   "m" gnus-summary-save-parts
2066   "u" gnus-uu-decode-uu
2067   "U" gnus-uu-decode-uu-and-save
2068   "s" gnus-uu-decode-unshar
2069   "S" gnus-uu-decode-unshar-and-save
2070   "o" gnus-uu-decode-save
2071   "O" gnus-uu-decode-save
2072   "b" gnus-uu-decode-binhex
2073   "B" gnus-uu-decode-binhex
2074   "p" gnus-uu-decode-postscript
2075   "P" gnus-uu-decode-postscript-and-save)
2076
2077 (gnus-define-keys
2078     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2079   "u" gnus-uu-decode-uu-view
2080   "U" gnus-uu-decode-uu-and-save-view
2081   "s" gnus-uu-decode-unshar-view
2082   "S" gnus-uu-decode-unshar-and-save-view
2083   "o" gnus-uu-decode-save-view
2084   "O" gnus-uu-decode-save-view
2085   "b" gnus-uu-decode-binhex-view
2086   "B" gnus-uu-decode-binhex-view
2087   "p" gnus-uu-decode-postscript-view
2088   "P" gnus-uu-decode-postscript-and-save-view)
2089
2090 (defvar gnus-article-post-menu nil)
2091
2092 (defconst gnus-summary-menu-maxlen 20)
2093
2094 (defun gnus-summary-menu-split (menu)
2095   ;; If we have lots of elements, divide them into groups of 20
2096   ;; and make a pane (or submenu) for each one.
2097   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2098       (let ((menu menu) sublists next
2099             (i 1))
2100         (while menu
2101           ;; Pull off the next gnus-summary-menu-maxlen elements
2102           ;; and make them the next element of sublist.
2103           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2104           (if next
2105               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2106                       nil))
2107           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2108                                              (aref (car (last menu)) 0)) menu)
2109                                sublists))
2110           (setq i (1+ i))
2111           (setq menu next))
2112         (nreverse sublists))
2113     ;; Few elements--put them all in one pane.
2114     menu))
2115
2116 (defun gnus-summary-make-menu-bar ()
2117   (gnus-turn-off-edit-menu 'summary)
2118
2119   (unless (boundp 'gnus-summary-misc-menu)
2120
2121     (easy-menu-define
2122       gnus-summary-kill-menu gnus-summary-mode-map ""
2123       (cons
2124        "Score"
2125        (nconc
2126         (list
2127          ["Customize" gnus-score-customize t])
2128         (gnus-make-score-map 'increase)
2129         (gnus-make-score-map 'lower)
2130         '(("Mark"
2131            ["Kill below" gnus-summary-kill-below t]
2132            ["Mark above" gnus-summary-mark-above t]
2133            ["Tick above" gnus-summary-tick-above t]
2134            ["Clear above" gnus-summary-clear-above t])
2135           ["Current score" gnus-summary-current-score t]
2136           ["Set score" gnus-summary-set-score t]
2137           ["Switch current score file..." gnus-score-change-score-file t]
2138           ["Set mark below..." gnus-score-set-mark-below t]
2139           ["Set expunge below..." gnus-score-set-expunge-below t]
2140           ["Edit current score file" gnus-score-edit-current-scores t]
2141           ["Edit score file" gnus-score-edit-file t]
2142           ["Trace score" gnus-score-find-trace t]
2143           ["Find words" gnus-score-find-favourite-words t]
2144           ["Rescore buffer" gnus-summary-rescore t]
2145           ["Increase score..." gnus-summary-increase-score t]
2146           ["Lower score..." gnus-summary-lower-score t]))))
2147
2148     ;; Define both the Article menu in the summary buffer and the
2149     ;; equivalent Commands menu in the article buffer here for
2150     ;; consistency.
2151     (let ((innards
2152            `(("Hide"
2153               ["All" gnus-article-hide t]
2154               ["Headers" gnus-article-hide-headers t]
2155               ["Signature" gnus-article-hide-signature t]
2156               ["Citation" gnus-article-hide-citation t]
2157               ["List identifiers" gnus-article-hide-list-identifiers t]
2158               ["Banner" gnus-article-strip-banner t]
2159               ["Boring headers" gnus-article-hide-boring-headers t])
2160              ("Highlight"
2161               ["All" gnus-article-highlight t]
2162               ["Headers" gnus-article-highlight-headers t]
2163               ["Signature" gnus-article-highlight-signature t]
2164               ["Citation" gnus-article-highlight-citation t])
2165              ("MIME"
2166               ["Words" gnus-article-decode-mime-words t]
2167               ["Charset" gnus-article-decode-charset t]
2168               ["QP" gnus-article-de-quoted-unreadable t]
2169               ["Base64" gnus-article-de-base64-unreadable t]
2170               ["View MIME buttons" gnus-summary-display-buttonized t]
2171               ["View all" gnus-mime-view-all-parts t]
2172               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2173               ["Encrypt body" gnus-article-encrypt-body
2174                :active (not (gnus-group-read-only-p))
2175                ,@(if (featurep 'xemacs) nil
2176                    '(:help "Encrypt the message body on disk"))]
2177               ["Extract all parts..." gnus-summary-save-parts t]
2178               ("Multipart"
2179                ["Repair multipart" gnus-summary-repair-multipart t]
2180                ["Pipe part..." gnus-article-pipe-part t]
2181                ["Inline part" gnus-article-inline-part t]
2182                ["Encrypt body" gnus-article-encrypt-body
2183                 :active (not (gnus-group-read-only-p))
2184                ,@(if (featurep 'xemacs) nil
2185                    '(:help "Encrypt the message body on disk"))]
2186                ["View part externally" gnus-article-view-part-externally t]
2187                ["View part with charset..." gnus-article-view-part-as-charset t]
2188                ["Copy part" gnus-article-copy-part t]
2189                ["Save part..." gnus-article-save-part t]
2190                ["View part" gnus-article-view-part t]))
2191              ("Date"
2192               ["Local" gnus-article-date-local t]
2193               ["ISO8601" gnus-article-date-iso8601 t]
2194               ["UT" gnus-article-date-ut t]
2195               ["Original" gnus-article-date-original t]
2196               ["Lapsed" gnus-article-date-lapsed t]
2197               ["User-defined" gnus-article-date-user t])
2198              ("Display"
2199               ["Remove images" gnus-article-remove-images t]
2200               ["Toggle smiley" gnus-treat-smiley t]
2201               ["Show X-Face" gnus-article-display-x-face t]
2202               ["Show picons in From" gnus-treat-from-picon t]
2203               ["Show picons in mail headers" gnus-treat-mail-picon t]
2204               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2205               ("View as different encoding"
2206                ,@(gnus-summary-menu-split
2207                   (mapcar
2208                    (lambda (cs)
2209                      ;; Since easymenu under Emacs doesn't allow
2210                      ;; lambda forms for menu commands, we should
2211                      ;; provide intern'ed function symbols.
2212                      (let ((command (intern (format "\
2213 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2214                        (fset command
2215                              `(lambda ()
2216                                 (interactive)
2217                                 (let ((gnus-summary-show-article-charset-alist
2218                                        '((1 . ,cs))))
2219                                   (gnus-summary-show-article 1))))
2220                        `[,(symbol-name cs) ,command t]))
2221                    (sort (if (fboundp 'coding-system-list)
2222                              (coding-system-list)
2223                            (mapcar 'car mm-mime-mule-charset-alist))
2224                          'string<)))))
2225              ("Washing"
2226               ("Remove Blanks"
2227                ["Leading" gnus-article-strip-leading-blank-lines t]
2228                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2229                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2230                ["All of the above" gnus-article-strip-blank-lines t]
2231                ["All" gnus-article-strip-all-blank-lines t]
2232                ["Leading space" gnus-article-strip-leading-space t]
2233                ["Trailing space" gnus-article-strip-trailing-space t]
2234                ["Leading space in headers"
2235                 gnus-article-remove-leading-whitespace t])
2236               ["Overstrike" gnus-article-treat-overstrike t]
2237               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2238               ["Emphasis" gnus-article-emphasize t]
2239               ["Word wrap" gnus-article-fill-cited-article t]
2240               ["Fill long lines" gnus-article-fill-long-lines t]
2241               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2242               ["Remove CR" gnus-article-remove-cr t]
2243               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2244               ["Base64" gnus-article-de-base64-unreadable t]
2245               ["Rot 13" gnus-summary-caesar-message
2246                ,@(if (featurep 'xemacs) '(t)
2247                    '(:help "\"Caesar rotate\" article by 13"))]
2248               ["Morse decode" gnus-summary-morse-message t]
2249               ["Unix pipe..." gnus-summary-pipe-message t]
2250               ["Add buttons" gnus-article-add-buttons t]
2251               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2252               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2253               ["Verbose header" gnus-summary-verbose-headers t]
2254               ["Toggle header" gnus-summary-toggle-header t]
2255               ["Unfold headers" gnus-article-treat-unfold-headers t]
2256               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2257               ["Html" gnus-article-wash-html t]
2258               ["Unsplit URLs" gnus-article-unsplit-urls t]
2259               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2260               ["Decode HZ" gnus-article-decode-HZ t]
2261               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2262               ("(Outlook) Deuglify"
2263                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2264                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2265                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2266                ["Full (Outlook) deuglify"
2267                 gnus-article-outlook-deuglify-article t])
2268               )
2269              ("Output"
2270               ["Save in default format..." gnus-summary-save-article
2271                ,@(if (featurep 'xemacs) '(t)
2272                    '(:help "Save article using default method"))]
2273               ["Save in file..." gnus-summary-save-article-file
2274                ,@(if (featurep 'xemacs) '(t)
2275                    '(:help "Save article in file"))]
2276               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2277               ["Save in MH folder..." gnus-summary-save-article-folder t]
2278               ["Save in VM folder..." gnus-summary-save-article-vm t]
2279               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2280               ["Save body in file..." gnus-summary-save-article-body-file t]
2281               ["Pipe through a filter..." gnus-summary-pipe-output t]
2282               ["Add to SOUP packet" gnus-soup-add-article t]
2283               ["Print with Muttprint..." gnus-summary-muttprint t]
2284               ["Print" gnus-summary-print-article
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "Generate and print a PostScript image"))])
2287              ("Copy, move,... (Backend)"
2288               ,@(if (featurep 'xemacs) nil
2289                   '(:help "Copying, moving, expiring articles..."))
2290               ["Respool article..." gnus-summary-respool-article t]
2291               ["Move article..." gnus-summary-move-article
2292                (gnus-check-backend-function
2293                 'request-move-article gnus-newsgroup-name)]
2294               ["Copy article..." gnus-summary-copy-article t]
2295               ["Crosspost article..." gnus-summary-crosspost-article
2296                (gnus-check-backend-function
2297                 'request-replace-article gnus-newsgroup-name)]
2298               ["Import file..." gnus-summary-import-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Create article..." gnus-summary-create-article
2302                (gnus-check-backend-function
2303                 'request-accept-article gnus-newsgroup-name)]
2304               ["Check if posted" gnus-summary-article-posted-p t]
2305               ["Edit article" gnus-summary-edit-article
2306                (not (gnus-group-read-only-p))]
2307               ["Delete article" gnus-summary-delete-article
2308                (gnus-check-backend-function
2309                 'request-expire-articles gnus-newsgroup-name)]
2310               ["Query respool" gnus-summary-respool-query t]
2311               ["Trace respool" gnus-summary-respool-trace t]
2312               ["Delete expirable articles" gnus-summary-expire-articles-now
2313                (gnus-check-backend-function
2314                 'request-expire-articles gnus-newsgroup-name)])
2315              ("Extract"
2316               ["Uudecode" gnus-uu-decode-uu
2317                ,@(if (featurep 'xemacs) '(t)
2318                    '(:help "Decode uuencoded article(s)"))]
2319               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2320               ["Unshar" gnus-uu-decode-unshar t]
2321               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2322               ["Save" gnus-uu-decode-save t]
2323               ["Binhex" gnus-uu-decode-binhex t]
2324               ["Postscript" gnus-uu-decode-postscript t]
2325               ["All MIME parts" gnus-summary-save-parts t])
2326              ("Cache"
2327               ["Enter article" gnus-cache-enter-article t]
2328               ["Remove article" gnus-cache-remove-article t])
2329              ["Translate" gnus-article-babel t]
2330              ["Select article buffer" gnus-summary-select-article-buffer t]
2331              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2332              ["Isearch article..." gnus-summary-isearch-article t]
2333              ["Beginning of the article" gnus-summary-beginning-of-article t]
2334              ["End of the article" gnus-summary-end-of-article t]
2335              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2336              ["Fetch referenced articles" gnus-summary-refer-references t]
2337              ["Fetch current thread" gnus-summary-refer-thread t]
2338              ["Fetch article with id..." gnus-summary-refer-article t]
2339              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2340              ["Redisplay" gnus-summary-show-article t]
2341              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2342       (easy-menu-define
2343         gnus-summary-article-menu gnus-summary-mode-map ""
2344         (cons "Article" innards))
2345
2346       (if (not (keymapp gnus-summary-article-menu))
2347           (easy-menu-define
2348             gnus-article-commands-menu gnus-article-mode-map ""
2349             (cons "Commands" innards))
2350         ;; in Emacs, don't share menu.
2351         (setq gnus-article-commands-menu
2352               (copy-keymap gnus-summary-article-menu))
2353         (define-key gnus-article-mode-map [menu-bar commands]
2354           (cons "Commands" gnus-article-commands-menu))))
2355
2356     (easy-menu-define
2357       gnus-summary-thread-menu gnus-summary-mode-map ""
2358       '("Threads"
2359         ["Find all messages in thread" gnus-summary-refer-thread t]
2360         ["Toggle threading" gnus-summary-toggle-threads t]
2361         ["Hide threads" gnus-summary-hide-all-threads t]
2362         ["Show threads" gnus-summary-show-all-threads t]
2363         ["Hide thread" gnus-summary-hide-thread t]
2364         ["Show thread" gnus-summary-show-thread t]
2365         ["Go to next thread" gnus-summary-next-thread t]
2366         ["Go to previous thread" gnus-summary-prev-thread t]
2367         ["Go down thread" gnus-summary-down-thread t]
2368         ["Go up thread" gnus-summary-up-thread t]
2369         ["Top of thread" gnus-summary-top-thread t]
2370         ["Mark thread as read" gnus-summary-kill-thread t]
2371         ["Lower thread score" gnus-summary-lower-thread t]
2372         ["Raise thread score" gnus-summary-raise-thread t]
2373         ["Rethread current" gnus-summary-rethread-current t]))
2374
2375     (easy-menu-define
2376       gnus-summary-post-menu gnus-summary-mode-map ""
2377       `("Post"
2378         ["Send a message (mail or news)" gnus-summary-post-news
2379          ,@(if (featurep 'xemacs) '(t)
2380              '(:help "Compose a new message (mail or news)"))]
2381         ["Followup" gnus-summary-followup
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Post followup to this article"))]
2384         ["Followup and yank" gnus-summary-followup-with-original
2385          ,@(if (featurep 'xemacs) '(t)
2386              '(:help "Post followup to this article, quoting its contents"))]
2387         ["Supersede article" gnus-summary-supersede-article t]
2388         ["Cancel article" gnus-summary-cancel-article
2389          ,@(if (featurep 'xemacs) '(t)
2390              '(:help "Cancel an article you posted"))]
2391         ["Reply" gnus-summary-reply t]
2392         ["Reply and yank" gnus-summary-reply-with-original t]
2393         ["Wide reply" gnus-summary-wide-reply t]
2394         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2395          ,@(if (featurep 'xemacs) '(t)
2396              '(:help "Mail a reply, quoting this article"))]
2397         ["Very wide reply" gnus-summary-very-wide-reply t]
2398         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2399          ,@(if (featurep 'xemacs) '(t)
2400              '(:help "Mail a very wide reply, quoting this article"))]
2401         ["Mail forward" gnus-summary-mail-forward t]
2402         ["Post forward" gnus-summary-post-forward t]
2403         ["Digest and mail" gnus-uu-digest-mail-forward t]
2404         ["Digest and post" gnus-uu-digest-post-forward t]
2405         ["Resend message" gnus-summary-resend-message t]
2406         ["Resend message edit" gnus-summary-resend-message-edit t]
2407         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2408         ["Send a mail" gnus-summary-mail-other-window t]
2409         ["Create a local message" gnus-summary-news-other-window t]
2410         ["Uuencode and post" gnus-uu-post-news
2411          ,@(if (featurep 'xemacs) '(t)
2412              '(:help "Post a uuencoded article"))]
2413         ["Followup via news" gnus-summary-followup-to-mail t]
2414         ["Followup via news and yank"
2415          gnus-summary-followup-to-mail-with-original t]
2416         ;;("Draft"
2417         ;;["Send" gnus-summary-send-draft t]
2418         ;;["Send bounced" gnus-resend-bounced-mail t])
2419         ))
2420
2421     (cond
2422      ((not (keymapp gnus-summary-post-menu))
2423       (setq gnus-article-post-menu gnus-summary-post-menu))
2424      ((not gnus-article-post-menu)
2425       ;; Don't share post menu.
2426       (setq gnus-article-post-menu
2427             (copy-keymap gnus-summary-post-menu))))
2428     (define-key gnus-article-mode-map [menu-bar post]
2429       (cons "Post" gnus-article-post-menu))
2430
2431     (easy-menu-define
2432       gnus-summary-misc-menu gnus-summary-mode-map ""
2433       `("Gnus"
2434         ("Mark Read"
2435          ["Mark as read" gnus-summary-mark-as-read-forward t]
2436          ["Mark same subject and select"
2437           gnus-summary-kill-same-subject-and-select t]
2438          ["Mark same subject" gnus-summary-kill-same-subject t]
2439          ["Catchup" gnus-summary-catchup
2440           ,@(if (featurep 'xemacs) '(t)
2441               '(:help "Mark unread articles in this group as read"))]
2442          ["Catchup all" gnus-summary-catchup-all t]
2443          ["Catchup to here" gnus-summary-catchup-to-here t]
2444          ["Catchup from here" gnus-summary-catchup-from-here t]
2445          ["Catchup region" gnus-summary-mark-region-as-read
2446           (gnus-mark-active-p)]
2447          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2448         ("Mark Various"
2449          ["Tick" gnus-summary-tick-article-forward t]
2450          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2451          ["Remove marks" gnus-summary-clear-mark-forward t]
2452          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2453          ["Set bookmark" gnus-summary-set-bookmark t]
2454          ["Remove bookmark" gnus-summary-remove-bookmark t])
2455         ("Limit to"
2456          ["Marks..." gnus-summary-limit-to-marks t]
2457          ["Subject..." gnus-summary-limit-to-subject t]
2458          ["Author..." gnus-summary-limit-to-author t]
2459          ["Age..." gnus-summary-limit-to-age t]
2460          ["Extra..." gnus-summary-limit-to-extra t]
2461          ["Score..." gnus-summary-limit-to-score t]
2462          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2463          ["Unread" gnus-summary-limit-to-unread t]
2464          ["Unseen" gnus-summary-limit-to-unseen t]
2465          ["Replied" gnus-summary-limit-to-replied t]
2466          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2467          ["Next articles" gnus-summary-limit-to-articles t]
2468          ["Pop limit" gnus-summary-pop-limit t]
2469          ["Show dormant" gnus-summary-limit-include-dormant t]
2470          ["Hide childless dormant"
2471           gnus-summary-limit-exclude-childless-dormant t]
2472          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2473          ["Hide marked" gnus-summary-limit-exclude-marks t]
2474          ["Show expunged" gnus-summary-limit-include-expunged t])
2475         ("Process Mark"
2476          ["Set mark" gnus-summary-mark-as-processable t]
2477          ["Remove mark" gnus-summary-unmark-as-processable t]
2478          ["Remove all marks" gnus-summary-unmark-all-processable t]
2479          ["Invert marks" gnus-uu-invert-processable t]
2480          ["Mark above" gnus-uu-mark-over t]
2481          ["Mark series" gnus-uu-mark-series t]
2482          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2483          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2484          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2485          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2486          ["Mark all" gnus-uu-mark-all t]
2487          ["Mark buffer" gnus-uu-mark-buffer t]
2488          ["Mark sparse" gnus-uu-mark-sparse t]
2489          ["Mark thread" gnus-uu-mark-thread t]
2490          ["Unmark thread" gnus-uu-unmark-thread t]
2491          ("Process Mark Sets"
2492           ["Kill" gnus-summary-kill-process-mark t]
2493           ["Yank" gnus-summary-yank-process-mark
2494            gnus-newsgroup-process-stack]
2495           ["Save" gnus-summary-save-process-mark t]
2496           ["Run command on marked..." gnus-summary-universal-argument t]))
2497         ("Scroll article"
2498          ["Page forward" gnus-summary-next-page
2499           ,@(if (featurep 'xemacs) '(t)
2500               '(:help "Show next page of article"))]
2501          ["Page backward" gnus-summary-prev-page
2502           ,@(if (featurep 'xemacs) '(t)
2503               '(:help "Show previous page of article"))]
2504          ["Line forward" gnus-summary-scroll-up t])
2505         ("Move"
2506          ["Next unread article" gnus-summary-next-unread-article t]
2507          ["Previous unread article" gnus-summary-prev-unread-article t]
2508          ["Next article" gnus-summary-next-article t]
2509          ["Previous article" gnus-summary-prev-article t]
2510          ["Next unread subject" gnus-summary-next-unread-subject t]
2511          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2512          ["Next article same subject" gnus-summary-next-same-subject t]
2513          ["Previous article same subject" gnus-summary-prev-same-subject t]
2514          ["First unread article" gnus-summary-first-unread-article t]
2515          ["Best unread article" gnus-summary-best-unread-article t]
2516          ["Go to subject number..." gnus-summary-goto-subject t]
2517          ["Go to article number..." gnus-summary-goto-article t]
2518          ["Go to the last article" gnus-summary-goto-last-article t]
2519          ["Pop article off history" gnus-summary-pop-article t])
2520         ("Sort"
2521          ["Sort by number" gnus-summary-sort-by-number t]
2522          ["Sort by author" gnus-summary-sort-by-author t]
2523          ["Sort by subject" gnus-summary-sort-by-subject t]
2524          ["Sort by date" gnus-summary-sort-by-date t]
2525          ["Sort by score" gnus-summary-sort-by-score t]
2526          ["Sort by lines" gnus-summary-sort-by-lines t]
2527          ["Sort by characters" gnus-summary-sort-by-chars t]
2528          ["Randomize" gnus-summary-sort-by-random t]
2529          ["Original sort" gnus-summary-sort-by-original t])
2530         ("Help"
2531          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2532          ["Describe group" gnus-summary-describe-group t]
2533          ["Fetch charter" gnus-group-fetch-charter
2534           ,@(if (featurep 'xemacs) nil
2535               '(:help "Display the charter of the current group"))]
2536          ["Fetch control message" gnus-group-fetch-control
2537           ,@(if (featurep 'xemacs) nil
2538               '(:help "Display the archived control message for the current group"))]
2539          ["Read manual" gnus-info-find-node t])
2540         ("Modes"
2541          ["Pick and read" gnus-pick-mode t]
2542          ["Binary" gnus-binary-mode t])
2543         ("Regeneration"
2544          ["Regenerate" gnus-summary-prepare t]
2545          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2546          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2547          ["Toggle threading" gnus-summary-toggle-threads t])
2548         ["See old articles" gnus-summary-insert-old-articles t]
2549         ["See new articles" gnus-summary-insert-new-articles t]
2550         ["Filter articles..." gnus-summary-execute-command t]
2551         ["Run command on articles..." gnus-summary-universal-argument t]
2552         ["Search articles forward..." gnus-summary-search-article-forward t]
2553         ["Search articles backward..." gnus-summary-search-article-backward t]
2554         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2555         ["Expand window" gnus-summary-expand-window t]
2556         ["Expire expirable articles" gnus-summary-expire-articles
2557          (gnus-check-backend-function
2558           'request-expire-articles gnus-newsgroup-name)]
2559         ["Edit local kill file" gnus-summary-edit-local-kill t]
2560         ["Edit main kill file" gnus-summary-edit-global-kill t]
2561         ["Edit group parameters" gnus-summary-edit-parameters t]
2562         ["Customize group parameters" gnus-summary-customize-parameters t]
2563         ["Send a bug report" gnus-bug t]
2564         ("Exit"
2565          ["Catchup and exit" gnus-summary-catchup-and-exit
2566           ,@(if (featurep 'xemacs) '(t)
2567               '(:help "Mark unread articles in this group as read, then exit"))]
2568          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2569          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2570          ["Exit group" gnus-summary-exit
2571           ,@(if (featurep 'xemacs) '(t)
2572               '(:help "Exit current group, return to group selection mode"))]
2573          ["Exit group without updating" gnus-summary-exit-no-update t]
2574          ["Exit and goto next group" gnus-summary-next-group t]
2575          ["Exit and goto prev group" gnus-summary-prev-group t]
2576          ["Reselect group" gnus-summary-reselect-current-group t]
2577          ["Rescan group" gnus-summary-rescan-group t]
2578          ["Update dribble" gnus-summary-save-newsrc t])))
2579
2580     (gnus-run-hooks 'gnus-summary-menu-hook)))
2581
2582 (defvar gnus-summary-tool-bar-map nil)
2583
2584 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2585 (defun gnus-summary-make-tool-bar ()
2586   (if (and (fboundp 'tool-bar-add-item-from-menu)
2587            (default-value 'tool-bar-mode)
2588            (not gnus-summary-tool-bar-map))
2589       (setq gnus-summary-tool-bar-map
2590             (let ((tool-bar-map (make-sparse-keymap))
2591                   (load-path (mm-image-load-path)))
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-post-news "post" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-followup "followup" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-summary-reply "reply" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2620               (tool-bar-add-item-from-menu
2621                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2622               (tool-bar-add-item-from-menu
2623                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2624               tool-bar-map)))
2625   (if gnus-summary-tool-bar-map
2626       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2627
2628 (defun gnus-score-set-default (var value)
2629   "A version of set that updates the GNU Emacs menu-bar."
2630   (set var value)
2631   ;; It is the message that forces the active status to be updated.
2632   (message ""))
2633
2634 (defun gnus-make-score-map (type)
2635   "Make a summary score map of type TYPE."
2636   (if t
2637       nil
2638     (let ((headers '(("author" "from" string)
2639                      ("subject" "subject" string)
2640                      ("article body" "body" string)
2641                      ("article head" "head" string)
2642                      ("xref" "xref" string)
2643                      ("extra header" "extra" string)
2644                      ("lines" "lines" number)
2645                      ("followups to author" "followup" string)))
2646           (types '((number ("less than" <)
2647                            ("greater than" >)
2648                            ("equal" =))
2649                    (string ("substring" s)
2650                            ("exact string" e)
2651                            ("fuzzy string" f)
2652                            ("regexp" r))))
2653           (perms '(("temporary" (current-time-string))
2654                    ("permanent" nil)
2655                    ("immediate" now)))
2656           header)
2657       (list
2658        (apply
2659         'nconc
2660         (list
2661          (if (eq type 'lower)
2662              "Lower score"
2663            "Increase score"))
2664         (let (outh)
2665           (while headers
2666             (setq header (car headers))
2667             (setq outh
2668                   (cons
2669                    (apply
2670                     'nconc
2671                     (list (car header))
2672                     (let ((ts (cdr (assoc (nth 2 header) types)))
2673                           outt)
2674                       (while ts
2675                         (setq outt
2676                               (cons
2677                                (apply
2678                                 'nconc
2679                                 (list (caar ts))
2680                                 (let ((ps perms)
2681                                       outp)
2682                                   (while ps
2683                                     (setq outp
2684                                           (cons
2685                                            (vector
2686                                             (caar ps)
2687                                             (list
2688                                              'gnus-summary-score-entry
2689                                              (nth 1 header)
2690                                              (if (or (string= (nth 1 header)
2691                                                               "head")
2692                                                      (string= (nth 1 header)
2693                                                               "body"))
2694                                                  ""
2695                                                (list 'gnus-summary-header
2696                                                      (nth 1 header)))
2697                                              (list 'quote (nth 1 (car ts)))
2698                                              (list 'gnus-score-delta-default
2699                                                    nil)
2700                                              (nth 1 (car ps))
2701                                              t)
2702                                             t)
2703                                            outp))
2704                                     (setq ps (cdr ps)))
2705                                   (list (nreverse outp))))
2706                                outt))
2707                         (setq ts (cdr ts)))
2708                       (list (nreverse outt))))
2709                    outh))
2710             (setq headers (cdr headers)))
2711           (list (nreverse outh))))))))
2712
2713 \f
2714
2715 (defun gnus-summary-mode (&optional group)
2716   "Major mode for reading articles.
2717
2718 All normal editing commands are switched off.
2719 \\<gnus-summary-mode-map>
2720 Each line in this buffer represents one article.  To read an
2721 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2722 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2723 respectively.
2724
2725 You can also post articles and send mail from this buffer.  To
2726 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2727 of an article, type `\\[gnus-summary-reply]'.
2728
2729 There are approx. one gazillion commands you can execute in this
2730 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2731
2732 The following commands are available:
2733
2734 \\{gnus-summary-mode-map}"
2735   (interactive)
2736   (kill-all-local-variables)
2737   (when (gnus-visual-p 'summary-menu 'menu)
2738     (gnus-summary-make-menu-bar)
2739     (gnus-summary-make-tool-bar))
2740   (gnus-summary-make-local-variables)
2741   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2742     (gnus-summary-make-local-variables))
2743   (gnus-make-thread-indent-array)
2744   (gnus-simplify-mode-line)
2745   (setq major-mode 'gnus-summary-mode)
2746   (setq mode-name "Summary")
2747   (make-local-variable 'minor-mode-alist)
2748   (use-local-map gnus-summary-mode-map)
2749   (buffer-disable-undo)
2750   (setq buffer-read-only t              ;Disable modification
2751         show-trailing-whitespace nil)
2752   (setq truncate-lines t)
2753   (setq selective-display t)
2754   (setq selective-display-ellipses t)   ;Display `...'
2755   (gnus-summary-set-display-table)
2756   (gnus-set-default-directory)
2757   (setq gnus-newsgroup-name group)
2758   (make-local-variable 'gnus-summary-line-format)
2759   (make-local-variable 'gnus-summary-line-format-spec)
2760   (make-local-variable 'gnus-summary-dummy-line-format)
2761   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2762   (make-local-variable 'gnus-summary-mark-positions)
2763   (gnus-make-local-hook 'pre-command-hook)
2764   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2765   (gnus-run-hooks 'gnus-summary-mode-hook)
2766   (turn-on-gnus-mailing-list-mode)
2767   (mm-enable-multibyte)
2768   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2769   (gnus-update-summary-mark-positions))
2770
2771 (defun gnus-summary-make-local-variables ()
2772   "Make all the local summary buffer variables."
2773   (let (global)
2774     (dolist (local gnus-summary-local-variables)
2775       (if (consp local)
2776           (progn
2777             (if (eq (cdr local) 'global)
2778                 ;; Copy the global value of the variable.
2779                 (setq global (symbol-value (car local)))
2780               ;; Use the value from the list.
2781               (setq global (eval (cdr local))))
2782             (set (make-local-variable (car local)) global))
2783         ;; Simple nil-valued local variable.
2784         (set (make-local-variable local) nil)))))
2785
2786 (defun gnus-summary-clear-local-variables ()
2787   (let ((locals gnus-summary-local-variables))
2788     (while locals
2789       (if (consp (car locals))
2790           (and (vectorp (caar locals))
2791                (set (caar locals) nil))
2792         (and (vectorp (car locals))
2793              (set (car locals) nil)))
2794       (setq locals (cdr locals)))))
2795
2796 ;; Summary data functions.
2797
2798 (defmacro gnus-data-number (data)
2799   `(car ,data))
2800
2801 (defmacro gnus-data-set-number (data number)
2802   `(setcar ,data ,number))
2803
2804 (defmacro gnus-data-mark (data)
2805   `(nth 1 ,data))
2806
2807 (defmacro gnus-data-set-mark (data mark)
2808   `(setcar (nthcdr 1 ,data) ,mark))
2809
2810 (defmacro gnus-data-pos (data)
2811   `(nth 2 ,data))
2812
2813 (defmacro gnus-data-set-pos (data pos)
2814   `(setcar (nthcdr 2 ,data) ,pos))
2815
2816 (defmacro gnus-data-header (data)
2817   `(nth 3 ,data))
2818
2819 (defmacro gnus-data-set-header (data header)
2820   `(setf (nth 3 ,data) ,header))
2821
2822 (defmacro gnus-data-level (data)
2823   `(nth 4 ,data))
2824
2825 (defmacro gnus-data-unread-p (data)
2826   `(= (nth 1 ,data) gnus-unread-mark))
2827
2828 (defmacro gnus-data-read-p (data)
2829   `(/= (nth 1 ,data) gnus-unread-mark))
2830
2831 (defmacro gnus-data-pseudo-p (data)
2832   `(consp (nth 3 ,data)))
2833
2834 (defmacro gnus-data-find (number)
2835   `(assq ,number gnus-newsgroup-data))
2836
2837 (defmacro gnus-data-find-list (number &optional data)
2838   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2839      (memq (assq ,number bdata)
2840            bdata)))
2841
2842 (defmacro gnus-data-make (number mark pos header level)
2843   `(list ,number ,mark ,pos ,header ,level))
2844
2845 (defun gnus-data-enter (after-article number mark pos header level offset)
2846   (let ((data (gnus-data-find-list after-article)))
2847     (unless data
2848       (error "No such article: %d" after-article))
2849     (setcdr data (cons (gnus-data-make number mark pos header level)
2850                        (cdr data)))
2851     (setq gnus-newsgroup-data-reverse nil)
2852     (gnus-data-update-list (cddr data) offset)))
2853
2854 (defun gnus-data-enter-list (after-article list &optional offset)
2855   (when list
2856     (let ((data (and after-article (gnus-data-find-list after-article)))
2857           (ilist list))
2858       (if (not (or data
2859                    after-article))
2860           (let ((odata gnus-newsgroup-data))
2861             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2862             (when offset
2863               (gnus-data-update-list odata offset)))
2864       ;; Find the last element in the list to be spliced into the main
2865         ;; list.
2866         (while (cdr list)
2867           (setq list (cdr list)))
2868         (if (not data)
2869             (progn
2870               (setcdr list gnus-newsgroup-data)
2871               (setq gnus-newsgroup-data ilist)
2872               (when offset
2873                 (gnus-data-update-list (cdr list) offset)))
2874           (setcdr list (cdr data))
2875           (setcdr data ilist)
2876           (when offset
2877             (gnus-data-update-list (cdr list) offset))))
2878       (setq gnus-newsgroup-data-reverse nil))))
2879
2880 (defun gnus-data-remove (article &optional offset)
2881   (let ((data gnus-newsgroup-data))
2882     (if (= (gnus-data-number (car data)) article)
2883         (progn
2884           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2885                 gnus-newsgroup-data-reverse nil)
2886           (when offset
2887             (gnus-data-update-list gnus-newsgroup-data offset)))
2888       (while (cdr data)
2889         (when (= (gnus-data-number (cadr data)) article)
2890           (setcdr data (cddr data))
2891           (when offset
2892             (gnus-data-update-list (cdr data) offset))
2893           (setq data nil
2894                 gnus-newsgroup-data-reverse nil))
2895         (setq data (cdr data))))))
2896
2897 (defmacro gnus-data-list (backward)
2898   `(if ,backward
2899        (or gnus-newsgroup-data-reverse
2900            (setq gnus-newsgroup-data-reverse
2901                  (reverse gnus-newsgroup-data)))
2902      gnus-newsgroup-data))
2903
2904 (defun gnus-data-update-list (data offset)
2905   "Add OFFSET to the POS of all data entries in DATA."
2906   (setq gnus-newsgroup-data-reverse nil)
2907   (while data
2908     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2909     (setq data (cdr data))))
2910
2911 (defun gnus-summary-article-pseudo-p (article)
2912   "Say whether this article is a pseudo article or not."
2913   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2914
2915 (defmacro gnus-summary-article-sparse-p (article)
2916   "Say whether this article is a sparse article or not."
2917   `(memq ,article gnus-newsgroup-sparse))
2918
2919 (defmacro gnus-summary-article-ancient-p (article)
2920   "Say whether this article is a sparse article or not."
2921   `(memq ,article gnus-newsgroup-ancient))
2922
2923 (defun gnus-article-parent-p (number)
2924   "Say whether this article is a parent or not."
2925   (let ((data (gnus-data-find-list number)))
2926     (and (cdr data)              ; There has to be an article after...
2927          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2928             (gnus-data-level (nth 1 data))))))
2929
2930 (defun gnus-article-children (number)
2931   "Return a list of all children to NUMBER."
2932   (let* ((data (gnus-data-find-list number))
2933          (level (gnus-data-level (car data)))
2934          children)
2935     (setq data (cdr data))
2936     (while (and data
2937                 (= (gnus-data-level (car data)) (1+ level)))
2938       (push (gnus-data-number (car data)) children)
2939       (setq data (cdr data)))
2940     children))
2941
2942 (defmacro gnus-summary-skip-intangible ()
2943   "If the current article is intangible, then jump to a different article."
2944   '(let ((to (get-text-property (point) 'gnus-intangible)))
2945      (and to (gnus-summary-goto-subject to))))
2946
2947 (defmacro gnus-summary-article-intangible-p ()
2948   "Say whether this article is intangible or not."
2949   '(get-text-property (point) 'gnus-intangible))
2950
2951 (defun gnus-article-read-p (article)
2952   "Say whether ARTICLE is read or not."
2953   (not (or (memq article gnus-newsgroup-marked)
2954            (memq article gnus-newsgroup-spam-marked)
2955            (memq article gnus-newsgroup-unreads)
2956            (memq article gnus-newsgroup-unselected)
2957            (memq article gnus-newsgroup-dormant))))
2958
2959 ;; Some summary mode macros.
2960
2961 (defmacro gnus-summary-article-number ()
2962   "The article number of the article on the current line.
2963 If there isn't an article number here, then we return the current
2964 article number."
2965   '(progn
2966      (gnus-summary-skip-intangible)
2967      (or (get-text-property (point) 'gnus-number)
2968          (gnus-summary-last-subject))))
2969
2970 (defmacro gnus-summary-article-header (&optional number)
2971   "Return the header of article NUMBER."
2972   `(gnus-data-header (gnus-data-find
2973                       ,(or number '(gnus-summary-article-number)))))
2974
2975 (defmacro gnus-summary-thread-level (&optional number)
2976   "Return the level of thread that starts with article NUMBER."
2977   `(if (and (eq gnus-summary-make-false-root 'dummy)
2978             (get-text-property (point) 'gnus-intangible))
2979        0
2980      (gnus-data-level (gnus-data-find
2981                        ,(or number '(gnus-summary-article-number))))))
2982
2983 (defmacro gnus-summary-article-mark (&optional number)
2984   "Return the mark of article NUMBER."
2985   `(gnus-data-mark (gnus-data-find
2986                     ,(or number '(gnus-summary-article-number)))))
2987
2988 (defmacro gnus-summary-article-pos (&optional number)
2989   "Return the position of the line of article NUMBER."
2990   `(gnus-data-pos (gnus-data-find
2991                    ,(or number '(gnus-summary-article-number)))))
2992
2993 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2994 (defmacro gnus-summary-article-subject (&optional number)
2995   "Return current subject string or nil if nothing."
2996   `(let ((headers
2997           ,(if number
2998                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2999              '(gnus-data-header (assq (gnus-summary-article-number)
3000                                       gnus-newsgroup-data)))))
3001      (and headers
3002           (vectorp headers)
3003           (mail-header-subject headers))))
3004
3005 (defmacro gnus-summary-article-score (&optional number)
3006   "Return current article score."
3007   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3008                   gnus-newsgroup-scored))
3009        gnus-summary-default-score 0))
3010
3011 (defun gnus-summary-article-children (&optional number)
3012   "Return a list of article numbers that are children of article NUMBER."
3013   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3014          (level (gnus-data-level (car data)))
3015          l children)
3016     (while (and (setq data (cdr data))
3017                 (> (setq l (gnus-data-level (car data))) level))
3018       (and (= (1+ level) l)
3019            (push (gnus-data-number (car data))
3020                  children)))
3021     (nreverse children)))
3022
3023 (defun gnus-summary-article-parent (&optional number)
3024   "Return the article number of the parent of article NUMBER."
3025   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3026                                     (gnus-data-list t)))
3027          (level (gnus-data-level (car data))))
3028     (if (zerop level)
3029         ()                              ; This is a root.
3030       ;; We search until we find an article with a level less than
3031       ;; this one.  That function has to be the parent.
3032       (while (and (setq data (cdr data))
3033                   (not (< (gnus-data-level (car data)) level))))
3034       (and data (gnus-data-number (car data))))))
3035
3036 (defun gnus-unread-mark-p (mark)
3037   "Say whether MARK is the unread mark."
3038   (= mark gnus-unread-mark))
3039
3040 (defun gnus-read-mark-p (mark)
3041   "Say whether MARK is one of the marks that mark as read.
3042 This is all marks except unread, ticked, dormant, and expirable."
3043   (not (or (= mark gnus-unread-mark)
3044            (= mark gnus-ticked-mark)
3045            (= mark gnus-spam-mark)
3046            (= mark gnus-dormant-mark)
3047            (= mark gnus-expirable-mark))))
3048
3049 (defmacro gnus-article-mark (number)
3050   "Return the MARK of article NUMBER.
3051 This macro should only be used when computing the mark the \"first\"
3052 time; i.e., when generating the summary lines.  After that,
3053 `gnus-summary-article-mark' should be used to examine the
3054 marks of articles."
3055   `(cond
3056     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3057     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3058     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3059     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3060     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3061     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3062     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3063     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3064            gnus-ancient-mark))))
3065
3066 ;; Saving hidden threads.
3067
3068 (defmacro gnus-save-hidden-threads (&rest forms)
3069   "Save hidden threads, eval FORMS, and restore the hidden threads."
3070   (let ((config (make-symbol "config")))
3071     `(let ((,config (gnus-hidden-threads-configuration)))
3072        (unwind-protect
3073            (save-excursion
3074              ,@forms)
3075          (gnus-restore-hidden-threads-configuration ,config)))))
3076 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3077 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3078
3079 (defun gnus-data-compute-positions ()
3080   "Compute the positions of all articles."
3081   (setq gnus-newsgroup-data-reverse nil)
3082   (let ((data gnus-newsgroup-data))
3083     (save-excursion
3084       (gnus-save-hidden-threads
3085         (gnus-summary-show-all-threads)
3086         (goto-char (point-min))
3087         (while data
3088           (while (get-text-property (point) 'gnus-intangible)
3089             (forward-line 1))
3090           (gnus-data-set-pos (car data) (+ (point) 3))
3091           (setq data (cdr data))
3092           (forward-line 1))))))
3093
3094 (defun gnus-hidden-threads-configuration ()
3095   "Return the current hidden threads configuration."
3096   (save-excursion
3097     (let (config)
3098       (goto-char (point-min))
3099       (while (search-forward "\r" nil t)
3100         (push (1- (point)) config))
3101       config)))
3102
3103 (defun gnus-restore-hidden-threads-configuration (config)
3104   "Restore hidden threads configuration from CONFIG."
3105   (save-excursion
3106     (let (point buffer-read-only)
3107       (while (setq point (pop config))
3108         (when (and (< point (point-max))
3109                    (goto-char point)
3110                    (eq (char-after) ?\n))
3111           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3112
3113 ;; Various summary mode internalish functions.
3114
3115 (defun gnus-mouse-pick-article (e)
3116   (interactive "e")
3117   (mouse-set-point e)
3118   (gnus-summary-next-page nil t))
3119
3120 (defun gnus-summary-set-display-table ()
3121   "Change the display table.
3122 Odd characters have a tendency to mess
3123 up nicely formatted displays - we make all possible glyphs
3124 display only a single character."
3125
3126   ;; We start from the standard display table, if any.
3127   (let ((table (or (copy-sequence standard-display-table)
3128                    (make-display-table)))
3129         (i 32))
3130     ;; Nix out all the control chars...
3131     (while (>= (setq i (1- i)) 0)
3132       (aset table i [??]))
3133    ;; ... but not newline and cr, of course.  (cr is necessary for the
3134     ;; selective display).
3135     (aset table ?\n nil)
3136     (aset table ?\r nil)
3137     ;; We keep TAB as well.
3138     (aset table ?\t nil)
3139     ;; We nix out any glyphs over 126 that are not set already.
3140     (let ((i 256))
3141       (while (>= (setq i (1- i)) 127)
3142         ;; Only modify if the entry is nil.
3143         (unless (aref table i)
3144           (aset table i [??]))))
3145     (setq buffer-display-table table)))
3146
3147 (defun gnus-summary-set-article-display-arrow (pos)
3148   "Update the overlay arrow to point to line at position POS."
3149   (when (and gnus-summary-display-arrow
3150              (boundp 'overlay-arrow-position)
3151              (boundp 'overlay-arrow-string))
3152     (save-excursion
3153       (goto-char pos)
3154       (beginning-of-line)
3155       (unless overlay-arrow-position
3156         (setq overlay-arrow-position (make-marker)))
3157       (setq overlay-arrow-string "=>"
3158             overlay-arrow-position (set-marker overlay-arrow-position
3159                                                (point)
3160                                                (current-buffer))))))
3161
3162 (defun gnus-summary-setup-buffer (group)
3163   "Initialize summary buffer."
3164   (let ((buffer (gnus-summary-buffer-name group))
3165         (dead-name (concat "*Dead Summary "
3166                            (gnus-group-decoded-name group) "*")))
3167     ;; If a dead summary buffer exists, we kill it.
3168     (when (gnus-buffer-live-p dead-name)
3169       (gnus-kill-buffer dead-name))
3170     (if (get-buffer buffer)
3171         (progn
3172           (set-buffer buffer)
3173           (setq gnus-summary-buffer (current-buffer))
3174           (not gnus-newsgroup-prepared))
3175       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3176       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3177       (gnus-summary-mode group)
3178       (when gnus-carpal
3179         (gnus-carpal-setup-buffer 'summary))
3180       (unless gnus-single-article-buffer
3181         (make-local-variable 'gnus-article-buffer)
3182         (make-local-variable 'gnus-article-current)
3183         (make-local-variable 'gnus-original-article-buffer))
3184       (setq gnus-newsgroup-name group)
3185       ;; Set any local variables in the group parameters.
3186       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3187       t)))
3188
3189 (defun gnus-set-global-variables ()
3190   "Set the global equivalents of the buffer-local variables.
3191 They are set to the latest values they had.  These reflect the summary
3192 buffer that was in action when the last article was fetched."
3193   (when (eq major-mode 'gnus-summary-mode)
3194     (setq gnus-summary-buffer (current-buffer))
3195     (let ((name gnus-newsgroup-name)
3196           (marked gnus-newsgroup-marked)
3197           (spam gnus-newsgroup-spam-marked)
3198           (unread gnus-newsgroup-unreads)
3199           (headers gnus-current-headers)
3200           (data gnus-newsgroup-data)
3201           (summary gnus-summary-buffer)
3202           (article-buffer gnus-article-buffer)
3203           (original gnus-original-article-buffer)
3204           (gac gnus-article-current)
3205           (reffed gnus-reffed-article-number)
3206           (score-file gnus-current-score-file)
3207           (default-charset gnus-newsgroup-charset)
3208           vlist)
3209       (let ((locals gnus-newsgroup-variables))
3210         (while locals
3211           (if (consp (car locals))
3212               (push (eval (caar locals)) vlist)
3213             (push (eval (car locals)) vlist))
3214           (setq locals (cdr locals)))
3215         (setq vlist (nreverse vlist)))
3216       (with-current-buffer gnus-group-buffer
3217         (setq gnus-newsgroup-name name
3218               gnus-newsgroup-marked marked
3219               gnus-newsgroup-spam-marked spam
3220               gnus-newsgroup-unreads unread
3221               gnus-current-headers headers
3222               gnus-newsgroup-data data
3223               gnus-article-current gac
3224               gnus-summary-buffer summary
3225               gnus-article-buffer article-buffer
3226               gnus-original-article-buffer original
3227               gnus-reffed-article-number reffed
3228               gnus-current-score-file score-file
3229               gnus-newsgroup-charset default-charset)
3230         (let ((locals gnus-newsgroup-variables))
3231           (while locals
3232             (if (consp (car locals))
3233                 (set (caar locals) (pop vlist))
3234               (set (car locals) (pop vlist)))
3235             (setq locals (cdr locals))))
3236         ;; The article buffer also has local variables.
3237         (when (gnus-buffer-live-p gnus-article-buffer)
3238           (set-buffer gnus-article-buffer)
3239           (setq gnus-summary-buffer summary))))))
3240
3241 (defun gnus-summary-article-unread-p (article)
3242   "Say whether ARTICLE is unread or not."
3243   (memq article gnus-newsgroup-unreads))
3244
3245 (defun gnus-summary-first-article-p (&optional article)
3246   "Return whether ARTICLE is the first article in the buffer."
3247   (if (not (setq article (or article (gnus-summary-article-number))))
3248       nil
3249     (eq article (caar gnus-newsgroup-data))))
3250
3251 (defun gnus-summary-last-article-p (&optional article)
3252   "Return whether ARTICLE is the last article in the buffer."
3253   (if (not (setq article (or article (gnus-summary-article-number))))
3254       ;; All non-existent numbers are the last article.  :-)
3255       t
3256     (not (cdr (gnus-data-find-list article)))))
3257
3258 (defun gnus-make-thread-indent-array ()
3259   (let ((n 200))
3260     (unless (and gnus-thread-indent-array
3261                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3262       (setq gnus-thread-indent-array (make-vector 201 "")
3263             gnus-thread-indent-array-level gnus-thread-indent-level)
3264       (while (>= n 0)
3265         (aset gnus-thread-indent-array n
3266               (make-string (* n gnus-thread-indent-level) ? ))
3267         (setq n (1- n))))))
3268
3269 (defun gnus-update-summary-mark-positions ()
3270   "Compute where the summary marks are to go."
3271   (save-excursion
3272     (when (gnus-buffer-exists-p gnus-summary-buffer)
3273       (set-buffer gnus-summary-buffer))
3274     (let ((gnus-replied-mark 129)
3275           (gnus-score-below-mark 130)
3276           (gnus-score-over-mark 130)
3277           (gnus-undownloaded-mark 131)
3278           (spec gnus-summary-line-format-spec)
3279           gnus-visual pos)
3280       (save-excursion
3281         (gnus-set-work-buffer)
3282         (let ((gnus-summary-line-format-spec spec)
3283               (gnus-newsgroup-downloadable '(0)))
3284           (gnus-summary-insert-line
3285            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3286            0 nil t 128 t nil "" nil 1)
3287           (goto-char (point-min))
3288           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3289                                              (- (point) (point-min) 1)))))
3290           (goto-char (point-min))
3291           (push (cons 'replied (and (search-forward "\201" nil t)
3292                                     (- (point) (point-min) 1)))
3293                 pos)
3294           (goto-char (point-min))
3295           (push (cons 'score (and (search-forward "\202" nil t)
3296                                   (- (point) (point-min) 1)))
3297                 pos)
3298           (goto-char (point-min))
3299           (push (cons 'download
3300                       (and (search-forward "\203" nil t)
3301                            (- (point) (point-min) 1)))
3302                 pos)))
3303       (setq gnus-summary-mark-positions pos))))
3304
3305 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3306   "Insert a dummy root in the summary buffer."
3307   (beginning-of-line)
3308   (gnus-add-text-properties
3309    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3310    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3311
3312 (defun gnus-summary-extract-address-component (from)
3313   (or (car (funcall gnus-extract-address-components from))
3314       from))
3315
3316 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3317   (let ((mail-parse-charset gnus-newsgroup-charset)
3318         ; Is it really necessary to do this next part for each summary line?
3319         ; Luckily, doesn't seem to slow things down much.
3320         (mail-parse-ignored-charsets
3321          (with-current-buffer gnus-summary-buffer
3322            gnus-newsgroup-ignored-charsets)))
3323     (or
3324      (and gnus-ignored-from-addresses
3325           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3326           (let ((extra-headers (mail-header-extra header))
3327                 to
3328                 newsgroups)
3329             (cond
3330              ((setq to (cdr (assq 'To extra-headers)))
3331               (concat "-> "
3332                       (inline
3333                         (gnus-summary-extract-address-component
3334                          (funcall gnus-decode-encoded-word-function to)))))
3335              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3336               (concat "=> " newsgroups)))))
3337      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3338
3339 (defun gnus-summary-insert-line (gnus-tmp-header
3340                                  gnus-tmp-level gnus-tmp-current
3341                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3342                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3343                                  &optional gnus-tmp-dummy gnus-tmp-score
3344                                  gnus-tmp-process)
3345   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3346          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3347          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3348          (gnus-tmp-score-char
3349           (if (or (null gnus-summary-default-score)
3350                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3351                       gnus-summary-zcore-fuzz))
3352               ?                         ;Whitespace
3353             (if (< gnus-tmp-score gnus-summary-default-score)
3354                 gnus-score-below-mark gnus-score-over-mark)))
3355          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3356          (gnus-tmp-replied
3357           (cond (gnus-tmp-process gnus-process-mark)
3358                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3359                  gnus-cached-mark)
3360                 (gnus-tmp-replied gnus-replied-mark)
3361                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3362                  gnus-forwarded-mark)
3363                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3364                  gnus-saved-mark)
3365                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3366                  gnus-recent-mark)
3367                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3368                  gnus-unseen-mark)
3369                 (t gnus-no-mark)))
3370          (gnus-tmp-downloaded
3371           (cond (undownloaded
3372                  gnus-undownloaded-mark)
3373                 (gnus-newsgroup-agentized
3374                  gnus-downloaded-mark)
3375                 (t
3376                  gnus-no-mark)))
3377          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3378          (gnus-tmp-name
3379           (cond
3380            ((string-match "<[^>]+> *$" gnus-tmp-from)
3381             (let ((beg (match-beginning 0)))
3382               (or (and (string-match "^\".+\"" gnus-tmp-from)
3383                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3384                   (substring gnus-tmp-from 0 beg))))
3385            ((string-match "(.+)" gnus-tmp-from)
3386             (substring gnus-tmp-from
3387                        (1+ (match-beginning 0)) (1- (match-end 0))))
3388            (t gnus-tmp-from)))
3389          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3390          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3391          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3392          (buffer-read-only nil))
3393     (when (string= gnus-tmp-name "")
3394       (setq gnus-tmp-name gnus-tmp-from))
3395     (unless (numberp gnus-tmp-lines)
3396       (setq gnus-tmp-lines -1))
3397     (if (= gnus-tmp-lines -1)
3398         (setq gnus-tmp-lines "?")
3399       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3400       (gnus-put-text-property
3401      (point)
3402      (progn (eval gnus-summary-line-format-spec) (point))
3403        'gnus-number gnus-tmp-number)
3404     (when (gnus-visual-p 'summary-highlight 'highlight)
3405       (forward-line -1)
3406       (gnus-run-hooks 'gnus-summary-update-hook)
3407       (forward-line 1))))
3408
3409 (defun gnus-summary-update-line (&optional dont-update)
3410   "Update summary line after change."
3411   (when (and gnus-summary-default-score
3412              (not gnus-summary-inhibit-highlight))
3413     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3414            (article (gnus-summary-article-number))
3415            (score (gnus-summary-article-score article)))
3416       (unless dont-update
3417         (if (and gnus-summary-mark-below
3418                  (< (gnus-summary-article-score)
3419                     gnus-summary-mark-below))
3420             ;; This article has a low score, so we mark it as read.
3421             (when (memq article gnus-newsgroup-unreads)
3422               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3423           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3424             ;; This article was previously marked as read on account
3425             ;; of a low score, but now it has risen, so we mark it as
3426             ;; unread.
3427             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3428         (gnus-summary-update-mark
3429          (if (or (null gnus-summary-default-score)
3430                  (<= (abs (- score gnus-summary-default-score))
3431                      gnus-summary-zcore-fuzz))
3432              ?                          ;Whitespace
3433            (if (< score gnus-summary-default-score)
3434                gnus-score-below-mark gnus-score-over-mark))
3435          'score))
3436       ;; Do visual highlighting.
3437       (when (gnus-visual-p 'summary-highlight 'highlight)
3438         (gnus-run-hooks 'gnus-summary-update-hook)))))
3439
3440 (defvar gnus-tmp-new-adopts nil)
3441
3442 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3443   "Return the number of articles in THREAD.
3444 This may be 0 in some cases -- if none of the articles in
3445 the thread are to be displayed."
3446   (let* ((number
3447          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3448           (cond
3449            ((not (listp thread))
3450             1)
3451            ((and (consp thread) (cdr thread))
3452             (apply
3453              '+ 1 (mapcar
3454                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3455            ((null thread)
3456             1)
3457            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3458             1)
3459            (t 0))))
3460     (when (and level (zerop level) gnus-tmp-new-adopts)
3461       (incf number
3462             (apply '+ (mapcar
3463                        'gnus-summary-number-of-articles-in-thread
3464                        gnus-tmp-new-adopts))))
3465     (if char
3466         (if (> number 1) gnus-not-empty-thread-mark
3467           gnus-empty-thread-mark)
3468       number)))
3469
3470 (defsubst gnus-summary-line-message-size (head)
3471   "Return pretty-printed version of message size.
3472 This function is intended to be used in
3473 `gnus-summary-line-format-alist'."
3474   (let ((c (or (mail-header-chars head) -1)))
3475     (cond ((< c 0) "n/a")               ; chars not available
3476           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3477           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3478           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3479           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3480
3481
3482 (defun gnus-summary-set-local-parameters (group)
3483   "Go through the local params of GROUP and set all variable specs in that list."
3484   (let ((params (gnus-group-find-parameter group))
3485         (vars '(quit-config))           ; Ignore quit-config.
3486         elem)
3487     (while params
3488       (setq elem (car params)
3489             params (cdr params))
3490       (and (consp elem)                 ; Has to be a cons.
3491            (consp (cdr elem))           ; The cdr has to be a list.
3492            (symbolp (car elem))         ; Has to be a symbol in there.
3493            (not (memq (car elem) vars))
3494            (ignore-errors               ; So we set it.
3495              (push (car elem) vars)
3496              (make-local-variable (car elem))
3497              (set (car elem) (eval (nth 1 elem))))))))
3498
3499 (defun gnus-summary-read-group (group &optional show-all no-article
3500                                       kill-buffer no-display backward
3501                                       select-articles)
3502   "Start reading news in newsgroup GROUP.
3503 If SHOW-ALL is non-nil, already read articles are also listed.
3504 If NO-ARTICLE is non-nil, no article is selected initially.
3505 If NO-DISPLAY, don't generate a summary buffer."
3506   (let (result)
3507     (while (and group
3508                 (null (setq result
3509                             (let ((gnus-auto-select-next nil))
3510                               (or (gnus-summary-read-group-1
3511                                    group show-all no-article
3512                                    kill-buffer no-display
3513                                    select-articles)
3514                                   (setq show-all nil
3515                                         select-articles nil)))))
3516                 (eq gnus-auto-select-next 'quietly))
3517       (set-buffer gnus-group-buffer)
3518       ;; The entry function called above goes to the next
3519       ;; group automatically, so we go two groups back
3520       ;; if we are searching for the previous group.
3521       (when backward
3522         (gnus-group-prev-unread-group 2))
3523       (if (not (equal group (gnus-group-group-name)))
3524           (setq group (gnus-group-group-name))
3525         (setq group nil)))
3526     result))
3527
3528 (defun gnus-summary-read-group-1 (group show-all no-article
3529                                         kill-buffer no-display
3530                                         &optional select-articles)
3531   ;; Killed foreign groups can't be entered.
3532   ;;  (when (and (not (gnus-group-native-p group))
3533   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3534   ;;    (error "Dead non-native groups can't be entered"))
3535   (gnus-message 5 "Retrieving newsgroup: %s..."
3536                 (gnus-group-decoded-name group))
3537   (let* ((new-group (gnus-summary-setup-buffer group))
3538          (quit-config (gnus-group-quit-config group))
3539          (did-select (and new-group (gnus-select-newsgroup
3540                                      group show-all select-articles))))
3541     (cond
3542      ;; This summary buffer exists already, so we just select it.
3543      ((not new-group)
3544       (gnus-set-global-variables)
3545       (when kill-buffer
3546         (gnus-kill-or-deaden-summary kill-buffer))
3547       (gnus-configure-windows 'summary 'force)
3548       (gnus-set-mode-line 'summary)
3549       (gnus-summary-position-point)
3550       (message "")
3551       t)
3552      ;; We couldn't select this group.
3553      ((null did-select)
3554       (when (and (eq major-mode 'gnus-summary-mode)
3555                  (not (equal (current-buffer) kill-buffer)))
3556         (kill-buffer (current-buffer))
3557         (if (not quit-config)
3558             (progn
3559               ;; Update the info -- marks might need to be removed,
3560               ;; for instance.
3561               (gnus-summary-update-info)
3562               (set-buffer gnus-group-buffer)
3563               (gnus-group-jump-to-group group)
3564               (gnus-group-next-unread-group 1))
3565           (gnus-handle-ephemeral-exit quit-config)))
3566       (let ((grpinfo (gnus-get-info group)))
3567         (if (null (gnus-info-read grpinfo))
3568             (gnus-message 3 "Group %s contains no messages"
3569                           (gnus-group-decoded-name group))
3570           (gnus-message 3 "Can't select group")))
3571       nil)
3572      ;; The user did a `C-g' while prompting for number of articles,
3573      ;; so we exit this group.
3574      ((eq did-select 'quit)
3575       (and (eq major-mode 'gnus-summary-mode)
3576            (not (equal (current-buffer) kill-buffer))
3577            (kill-buffer (current-buffer)))
3578       (when kill-buffer
3579         (gnus-kill-or-deaden-summary kill-buffer))
3580       (if (not quit-config)
3581           (progn
3582             (set-buffer gnus-group-buffer)
3583             (gnus-group-jump-to-group group)
3584             (gnus-group-next-unread-group 1)
3585             (gnus-configure-windows 'group 'force))
3586         (gnus-handle-ephemeral-exit quit-config))
3587       ;; Finally signal the quit.
3588       (signal 'quit nil))
3589      ;; The group was successfully selected.
3590      (t
3591       (gnus-set-global-variables)
3592       ;; Save the active value in effect when the group was entered.
3593       (setq gnus-newsgroup-active
3594             (gnus-copy-sequence
3595              (gnus-active gnus-newsgroup-name)))
3596       ;; You can change the summary buffer in some way with this hook.
3597       (gnus-run-hooks 'gnus-select-group-hook)
3598       (gnus-update-format-specifications
3599        nil 'summary 'summary-mode 'summary-dummy)
3600       (gnus-update-summary-mark-positions)
3601       ;; Do score processing.
3602       (when gnus-use-scoring
3603         (gnus-possibly-score-headers))
3604       ;; Check whether to fill in the gaps in the threads.
3605       (when gnus-build-sparse-threads
3606         (gnus-build-sparse-threads))
3607       ;; Find the initial limit.
3608       (if show-all
3609           (let ((gnus-newsgroup-dormant nil))
3610             (gnus-summary-initial-limit show-all))
3611         (gnus-summary-initial-limit show-all))
3612       ;; Generate the summary buffer.
3613       (unless no-display
3614         (gnus-summary-prepare))
3615       (when gnus-use-trees
3616         (gnus-tree-open group)
3617         (setq gnus-summary-highlight-line-function
3618               'gnus-tree-highlight-article))
3619       ;; If the summary buffer is empty, but there are some low-scored
3620       ;; articles or some excluded dormants, we include these in the
3621       ;; buffer.
3622       (when (and (zerop (buffer-size))
3623                  (not no-display))
3624         (cond (gnus-newsgroup-dormant
3625                (gnus-summary-limit-include-dormant))
3626               ((and gnus-newsgroup-scored show-all)
3627                (gnus-summary-limit-include-expunged t))))
3628       ;; Function `gnus-apply-kill-file' must be called in this hook.
3629       (gnus-run-hooks 'gnus-apply-kill-hook)
3630       (if (and (zerop (buffer-size))
3631                (not no-display))
3632           (progn
3633             ;; This newsgroup is empty.
3634             (gnus-summary-catchup-and-exit nil t)
3635             (gnus-message 6 "No unread news")
3636             (when kill-buffer
3637               (gnus-kill-or-deaden-summary kill-buffer))
3638             ;; Return nil from this function.
3639             nil)
3640         ;; Hide conversation thread subtrees.  We cannot do this in
3641         ;; gnus-summary-prepare-hook since kill processing may not
3642         ;; work with hidden articles.
3643         (gnus-summary-maybe-hide-threads)
3644         (when kill-buffer
3645           (gnus-kill-or-deaden-summary kill-buffer))
3646         (gnus-summary-auto-select-subject)
3647         ;; Show first unread article if requested.
3648         (if (and (not no-article)
3649                  (not no-display)
3650                  gnus-newsgroup-unreads
3651                  gnus-auto-select-first)
3652             (progn
3653               (gnus-configure-windows 'summary)
3654               (let ((art (gnus-summary-article-number)))
3655                 (unless (and (not gnus-plugged)
3656                              (or (memq art gnus-newsgroup-undownloaded)
3657                                  (memq art gnus-newsgroup-downloadable)))
3658                   (gnus-summary-goto-article art))))
3659           ;; Don't select any articles.
3660           (gnus-summary-position-point)
3661           (gnus-configure-windows 'summary 'force)
3662           (gnus-set-mode-line 'summary))
3663         (when (and gnus-auto-center-group
3664                    (get-buffer-window gnus-group-buffer t))
3665           ;; Gotta use windows, because recenter does weird stuff if
3666           ;; the current buffer ain't the displayed window.
3667           (let ((owin (selected-window)))
3668             (select-window (get-buffer-window gnus-group-buffer t))
3669             (when (gnus-group-goto-group group)
3670               (recenter))
3671             (select-window owin)))
3672         ;; Mark this buffer as "prepared".
3673         (setq gnus-newsgroup-prepared t)
3674         (gnus-run-hooks 'gnus-summary-prepared-hook)
3675         (unless (gnus-ephemeral-group-p group)
3676           (gnus-group-update-group group))
3677         t)))))
3678
3679 (defun gnus-summary-auto-select-subject ()
3680   "Select the subject line on initial group entry."
3681   (goto-char (point-min))
3682   (cond
3683    ((eq gnus-auto-select-subject 'best)
3684     (gnus-summary-best-unread-subject))
3685    ((eq gnus-auto-select-subject 'unread)
3686     (gnus-summary-first-unread-subject))
3687    ((eq gnus-auto-select-subject 'unseen)
3688     (gnus-summary-first-unseen-subject))
3689    ((eq gnus-auto-select-subject 'unseen-or-unread)
3690     (gnus-summary-first-unseen-or-unread-subject))
3691    ((eq gnus-auto-select-subject 'first)
3692     ;; Do nothing.
3693     )
3694    ((functionp gnus-auto-select-subject)
3695     (funcall gnus-auto-select-subject))))
3696
3697 (defun gnus-summary-prepare ()
3698   "Generate the summary buffer."
3699   (interactive)
3700   (let ((buffer-read-only nil))
3701     (erase-buffer)
3702     (setq gnus-newsgroup-data nil
3703           gnus-newsgroup-data-reverse nil)
3704     (gnus-run-hooks 'gnus-summary-generate-hook)
3705     ;; Generate the buffer, either with threads or without.
3706     (when gnus-newsgroup-headers
3707       (gnus-summary-prepare-threads
3708        (if gnus-show-threads
3709            (gnus-sort-gathered-threads
3710             (funcall gnus-summary-thread-gathering-function
3711                      (gnus-sort-threads
3712                       (gnus-cut-threads (gnus-make-threads)))))
3713          ;; Unthreaded display.
3714          (gnus-sort-articles gnus-newsgroup-headers))))
3715     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3716     ;; Call hooks for modifying summary buffer.
3717     (goto-char (point-min))
3718     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3719
3720 (defsubst gnus-general-simplify-subject (subject)
3721   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3722   (setq subject
3723         (cond
3724          ;; Truncate the subject.
3725          (gnus-simplify-subject-functions
3726           (gnus-map-function gnus-simplify-subject-functions subject))
3727          ((numberp gnus-summary-gather-subject-limit)
3728           (setq subject (gnus-simplify-subject-re subject))
3729           (if (> (length subject) gnus-summary-gather-subject-limit)
3730               (substring subject 0 gnus-summary-gather-subject-limit)
3731             subject))
3732          ;; Fuzzily simplify it.
3733          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3734           (gnus-simplify-subject-fuzzy subject))
3735          ;; Just remove the leading "Re:".
3736          (t
3737           (gnus-simplify-subject-re subject))))
3738
3739   (if (and gnus-summary-gather-exclude-subject
3740            (string-match gnus-summary-gather-exclude-subject subject))
3741       nil                         ; This article shouldn't be gathered
3742     subject))
3743
3744 (defun gnus-summary-simplify-subject-query ()
3745   "Query where the respool algorithm would put this article."
3746   (interactive)
3747   (gnus-summary-select-article)
3748   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3749
3750 (defun gnus-gather-threads-by-subject (threads)
3751   "Gather threads by looking at Subject headers."
3752   (if (not gnus-summary-make-false-root)
3753       threads
3754     (let ((hashtb (gnus-make-hashtable 1024))
3755           (prev threads)
3756           (result threads)
3757           subject hthread whole-subject)
3758       (while threads
3759         (setq subject (gnus-general-simplify-subject
3760                        (setq whole-subject (mail-header-subject
3761                                             (caar threads)))))
3762         (when subject
3763           (if (setq hthread (gnus-gethash subject hashtb))
3764               (progn
3765                 ;; We enter a dummy root into the thread, if we
3766                 ;; haven't done that already.
3767                 (unless (stringp (caar hthread))
3768                   (setcar hthread (list whole-subject (car hthread))))
3769                 ;; We add this new gathered thread to this gathered
3770                 ;; thread.
3771                 (setcdr (car hthread)
3772                         (nconc (cdar hthread) (list (car threads))))
3773                 ;; Remove it from the list of threads.
3774                 (setcdr prev (cdr threads))
3775                 (setq threads prev))
3776             ;; Enter this thread into the hash table.
3777             (gnus-sethash subject
3778                           (if gnus-summary-make-false-root-always
3779                               (progn
3780                                 ;; If you want a dummy root above all
3781                                 ;; threads...
3782                                 (setcar threads (list whole-subject
3783                                                       (car threads)))
3784                                 threads)
3785                             threads)
3786                           hashtb)))
3787         (setq prev threads)
3788         (setq threads (cdr threads)))
3789       result)))
3790
3791 (defun gnus-gather-threads-by-references (threads)
3792   "Gather threads by looking at References headers."
3793   (let ((idhashtb (gnus-make-hashtable 1024))
3794         (thhashtb (gnus-make-hashtable 1024))
3795         (prev threads)
3796         (result threads)
3797         ids references id gthread gid entered ref)
3798     (while threads
3799       (when (setq references (mail-header-references (caar threads)))
3800         (setq id (mail-header-id (caar threads))
3801               ids (inline (gnus-split-references references))
3802               entered nil)
3803         (while (setq ref (pop ids))
3804           (setq ids (delete ref ids))
3805           (if (not (setq gid (gnus-gethash ref idhashtb)))
3806               (progn
3807                 (gnus-sethash ref id idhashtb)
3808                 (gnus-sethash id threads thhashtb))
3809             (setq gthread (gnus-gethash gid thhashtb))
3810             (unless entered
3811               ;; We enter a dummy root into the thread, if we
3812               ;; haven't done that already.
3813               (unless (stringp (caar gthread))
3814                 (setcar gthread (list (mail-header-subject (caar gthread))
3815                                       (car gthread))))
3816               ;; We add this new gathered thread to this gathered
3817               ;; thread.
3818               (setcdr (car gthread)
3819                       (nconc (cdar gthread) (list (car threads)))))
3820             ;; Add it into the thread hash table.
3821             (gnus-sethash id gthread thhashtb)
3822             (setq entered t)
3823             ;; Remove it from the list of threads.
3824             (setcdr prev (cdr threads))
3825             (setq threads prev))))
3826       (setq prev threads)
3827       (setq threads (cdr threads)))
3828     result))
3829
3830 (defun gnus-sort-gathered-threads (threads)
3831   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3832   (let ((result threads))
3833     (while threads
3834       (when (stringp (caar threads))
3835         (setcdr (car threads)
3836                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3837       (setq threads (cdr threads)))
3838     result))
3839
3840 (defun gnus-thread-loop-p (root thread)
3841   "Say whether ROOT is in THREAD."
3842   (let ((stack (list thread))
3843         (infloop 0)
3844         th)
3845     (while (setq thread (pop stack))
3846       (setq th (cdr thread))
3847       (while (and th
3848                   (not (eq (caar th) root)))
3849         (pop th))
3850       (if th
3851           ;; We have found a loop.
3852           (let (ref-dep)
3853             (setcdr thread (delq (car th) (cdr thread)))
3854             (if (boundp (setq ref-dep (intern "none"
3855                                               gnus-newsgroup-dependencies)))
3856                 (setcdr (symbol-value ref-dep)
3857                         (nconc (cdr (symbol-value ref-dep))
3858                                (list (car th))))
3859               (set ref-dep (list nil (car th))))
3860             (setq infloop 1
3861                   stack nil))
3862         ;; Push all the subthreads onto the stack.
3863         (push (cdr thread) stack)))
3864     infloop))
3865
3866 (defun gnus-make-threads ()
3867   "Go through the dependency hashtb and find the roots.  Return all threads."
3868   (let (threads)
3869     (while (catch 'infloop
3870              (mapatoms
3871               (lambda (refs)
3872                 ;; Deal with self-referencing References loops.
3873                 (when (and (car (symbol-value refs))
3874                            (not (zerop
3875                                  (apply
3876                                   '+
3877                                   (mapcar
3878                                    (lambda (thread)
3879                                      (gnus-thread-loop-p
3880                                       (car (symbol-value refs)) thread))
3881                                    (cdr (symbol-value refs)))))))
3882                   (setq threads nil)
3883                   (throw 'infloop t))
3884                 (unless (car (symbol-value refs))
3885                   ;; These threads do not refer back to any other
3886                   ;; articles, so they're roots.
3887                   (setq threads (append (cdr (symbol-value refs)) threads))))
3888               gnus-newsgroup-dependencies)))
3889     threads))
3890
3891 ;; Build the thread tree.
3892 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3893   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3894
3895 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3896 if it was already present.
3897
3898 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3899 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3900 Message-IDs will be renamed to a unique Message-ID before being
3901 entered.
3902
3903 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3904   (let* ((id (mail-header-id header))
3905          (id-dep (and id (intern id dependencies)))
3906          parent-id ref ref-dep ref-header replaced)
3907     ;; Enter this `header' in the `dependencies' table.
3908     (cond
3909      ((not id-dep)
3910       (setq header nil))
3911      ;; The first two cases do the normal part: enter a new `header'
3912      ;; in the `dependencies' table.
3913      ((not (boundp id-dep))
3914       (set id-dep (list header)))
3915      ((null (car (symbol-value id-dep)))
3916       (setcar (symbol-value id-dep) header))
3917
3918      ;; From here the `header' was already present in the
3919      ;; `dependencies' table.
3920      (force-new
3921       ;; Overrides an existing entry;
3922       ;; just set the header part of the entry.
3923       (setcar (symbol-value id-dep) header)
3924       (setq replaced t))
3925
3926      ;; Renames the existing `header' to a unique Message-ID.
3927      ((not gnus-summary-ignore-duplicates)
3928       ;; An article with this Message-ID has already been seen.
3929       ;; We rename the Message-ID.
3930       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3931            (list header))
3932       (mail-header-set-id header id))
3933
3934      ;; The last case ignores an existing entry, except it adds any
3935      ;; additional Xrefs (in case the two articles came from different
3936      ;; servers.
3937      ;; Also sets `header' to `nil' meaning that the `dependencies'
3938      ;; table was *not* modified.
3939      (t
3940       (mail-header-set-xref
3941        (car (symbol-value id-dep))
3942        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3943                    "")
3944                (or (mail-header-xref header) "")))
3945       (setq header nil)))
3946
3947     (when (and header (not replaced))
3948       ;; First check that we are not creating a References loop.
3949       (setq parent-id (gnus-parent-id (mail-header-references header)))
3950       (setq ref parent-id)
3951       (while (and ref
3952                   (setq ref-dep (intern-soft ref dependencies))
3953                   (boundp ref-dep)
3954                   (setq ref-header (car (symbol-value ref-dep))))
3955         (if (string= id ref)
3956             ;; Yuk!  This is a reference loop.  Make the article be a
3957             ;; root article.
3958             (progn
3959               (mail-header-set-references (car (symbol-value id-dep)) "none")
3960               (setq ref nil)
3961               (setq parent-id nil))
3962           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3963       (setq ref-dep (intern (or parent-id "none") dependencies))
3964       (if (boundp ref-dep)
3965           (setcdr (symbol-value ref-dep)
3966                   (nconc (cdr (symbol-value ref-dep))
3967                          (list (symbol-value id-dep))))
3968         (set ref-dep (list nil (symbol-value id-dep)))))
3969     header))
3970
3971 (defun gnus-extract-message-id-from-in-reply-to (string)
3972   (if (string-match "<[^>]+>" string)
3973       (substring string (match-beginning 0) (match-end 0))
3974     nil))
3975
3976 (defun gnus-build-sparse-threads ()
3977   (let ((headers gnus-newsgroup-headers)
3978         (mail-parse-charset gnus-newsgroup-charset)
3979         (gnus-summary-ignore-duplicates t)
3980         header references generation relations
3981         subject child end new-child date)
3982     ;; First we create an alist of generations/relations, where
3983     ;; generations is how much we trust the relation, and the relation
3984     ;; is parent/child.
3985     (gnus-message 7 "Making sparse threads...")
3986     (save-excursion
3987       (nnheader-set-temp-buffer " *gnus sparse threads*")
3988       (while (setq header (pop headers))
3989         (when (and (setq references (mail-header-references header))
3990                    (not (string= references "")))
3991           (insert references)
3992           (setq child (mail-header-id header)
3993                 subject (mail-header-subject header)
3994                 date (mail-header-date header)
3995                 generation 0)
3996           (while (search-backward ">" nil t)
3997             (setq end (1+ (point)))
3998             (when (search-backward "<" nil t)
3999               (setq new-child (buffer-substring (point) end))
4000               (push (list (incf generation)
4001                           child (setq child new-child)
4002                           subject date)
4003                     relations)))
4004           (when child
4005             (push (list (1+ generation) child nil subject) relations))
4006           (erase-buffer)))
4007       (kill-buffer (current-buffer)))
4008     ;; Sort over trustworthiness.
4009     (mapcar
4010      (lambda (relation)
4011        (when (gnus-dependencies-add-header
4012               (make-full-mail-header
4013                gnus-reffed-article-number
4014                (nth 3 relation) "" (or (nth 4 relation) "")
4015                (nth 1 relation)
4016                (or (nth 2 relation) "") 0 0 "")
4017               gnus-newsgroup-dependencies nil)
4018          (push gnus-reffed-article-number gnus-newsgroup-limit)
4019          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4020          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4021                gnus-newsgroup-reads)
4022          (decf gnus-reffed-article-number)))
4023      (sort relations 'car-less-than-car))
4024     (gnus-message 7 "Making sparse threads...done")))
4025
4026 (defun gnus-build-old-threads ()
4027   ;; Look at all the articles that refer back to old articles, and
4028   ;; fetch the headers for the articles that aren't there.  This will
4029   ;; build complete threads - if the roots haven't been expired by the
4030   ;; server, that is.
4031   (let ((mail-parse-charset gnus-newsgroup-charset)
4032         id heads)
4033     (mapatoms
4034      (lambda (refs)
4035        (when (not (car (symbol-value refs)))
4036          (setq heads (cdr (symbol-value refs)))
4037          (while heads
4038            (if (memq (mail-header-number (caar heads))
4039                      gnus-newsgroup-dormant)
4040                (setq heads (cdr heads))
4041              (setq id (symbol-name refs))
4042              (while (and (setq id (gnus-build-get-header id))
4043                          (not (car (gnus-id-to-thread id)))))
4044              (setq heads nil)))))
4045      gnus-newsgroup-dependencies)))
4046
4047 (defsubst gnus-remove-odd-characters (string)
4048   "Translate STRING into something that doesn't contain weird characters."
4049   (mm-subst-char-in-string
4050    ?\r ?\-
4051    (mm-subst-char-in-string ?\n ?\- string t) t))
4052
4053 ;; This function has to be called with point after the article number
4054 ;; on the beginning of the line.
4055 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4056   (let ((eol (point-at-eol))
4057         (buffer (current-buffer))
4058         header references in-reply-to)
4059
4060     ;; overview: [num subject from date id refs chars lines misc]
4061     (unwind-protect
4062         (let (x)
4063           (narrow-to-region (point) eol)
4064           (unless (eobp)
4065             (forward-char))
4066
4067           (setq header
4068                 (make-full-mail-header
4069                  number                 ; number
4070                  (condition-case ()     ; subject
4071                      (gnus-remove-odd-characters
4072                       (funcall gnus-decode-encoded-word-function
4073                                (setq x (nnheader-nov-field))))
4074                    (error x))
4075                  (condition-case ()     ; from
4076                      (gnus-remove-odd-characters
4077                       (funcall gnus-decode-encoded-word-function
4078                                (setq x (nnheader-nov-field))))
4079                    (error x))
4080                  (nnheader-nov-field)   ; date
4081                  (nnheader-nov-read-message-id number)  ; id
4082                  (setq references (nnheader-nov-field)) ; refs
4083                  (nnheader-nov-read-integer) ; chars
4084                  (nnheader-nov-read-integer) ; lines
4085                  (unless (eobp)
4086                    (if (looking-at "Xref: ")
4087                        (goto-char (match-end 0)))
4088                    (nnheader-nov-field)) ; Xref
4089                  (nnheader-nov-parse-extra)))) ; extra
4090
4091       (widen))
4092
4093     (when (and (string= references "")
4094                (setq in-reply-to (mail-header-extra header))
4095                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4096       (mail-header-set-references
4097        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4098
4099     (when gnus-alter-header-function
4100       (funcall gnus-alter-header-function header))
4101     (gnus-dependencies-add-header header dependencies force-new)))
4102
4103 (defun gnus-build-get-header (id)
4104   "Look through the buffer of NOV lines and find the header to ID.
4105 Enter this line into the dependencies hash table, and return
4106 the id of the parent article (if any)."
4107   (let ((deps gnus-newsgroup-dependencies)
4108         found header)
4109     (prog1
4110         (save-excursion
4111           (set-buffer nntp-server-buffer)
4112           (let ((case-fold-search nil))
4113             (goto-char (point-min))
4114             (while (and (not found)
4115                         (search-forward id nil t))
4116               (beginning-of-line)
4117               (setq found (looking-at
4118                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4119                                    (regexp-quote id))))
4120               (or found (beginning-of-line 2)))
4121             (when found
4122               (beginning-of-line)
4123               (and
4124                (setq header (gnus-nov-parse-line
4125                              (read (current-buffer)) deps))
4126                (gnus-parent-id (mail-header-references header))))))
4127       (when header
4128         (let ((number (mail-header-number header)))
4129           (push number gnus-newsgroup-limit)
4130           (push header gnus-newsgroup-headers)
4131           (if (memq number gnus-newsgroup-unselected)
4132               (progn
4133                 (setq gnus-newsgroup-unreads
4134                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4135                                                number))
4136                 (setq gnus-newsgroup-unselected
4137                       (delq number gnus-newsgroup-unselected)))
4138             (push number gnus-newsgroup-ancient)))))))
4139
4140 (defun gnus-build-all-threads ()
4141   "Read all the headers."
4142   (let ((gnus-summary-ignore-duplicates t)
4143         (mail-parse-charset gnus-newsgroup-charset)
4144         (dependencies gnus-newsgroup-dependencies)
4145         header article)
4146     (save-excursion
4147       (set-buffer nntp-server-buffer)
4148       (let ((case-fold-search nil))
4149         (goto-char (point-min))
4150         (while (not (eobp))
4151           (ignore-errors
4152             (setq article (read (current-buffer))
4153                   header (gnus-nov-parse-line article dependencies)))
4154           (when header
4155             (with-current-buffer gnus-summary-buffer
4156               (push header gnus-newsgroup-headers)
4157               (if (memq (setq article (mail-header-number header))
4158                         gnus-newsgroup-unselected)
4159                   (progn
4160                     (setq gnus-newsgroup-unreads
4161                           (gnus-add-to-sorted-list
4162                            gnus-newsgroup-unreads article))
4163                     (setq gnus-newsgroup-unselected
4164                           (delq article gnus-newsgroup-unselected)))
4165                 (push article gnus-newsgroup-ancient)))
4166             (forward-line 1)))))))
4167
4168 (defun gnus-summary-update-article-line (article header)
4169   "Update the line for ARTICLE using HEADER."
4170   (let* ((id (mail-header-id header))
4171          (thread (gnus-id-to-thread id)))
4172     (unless thread
4173       (error "Article in no thread"))
4174     ;; Update the thread.
4175     (setcar thread header)
4176     (gnus-summary-goto-subject article)
4177     (let* ((datal (gnus-data-find-list article))
4178            (data (car datal))
4179            (buffer-read-only nil)
4180            (level (gnus-summary-thread-level)))
4181       (gnus-delete-line)
4182       (let ((inserted (- (point)
4183                          (progn
4184                            (gnus-summary-insert-line
4185                             header level nil
4186                             (memq article gnus-newsgroup-undownloaded)
4187                             (gnus-article-mark article)
4188                             (memq article gnus-newsgroup-replied)
4189                             (memq article gnus-newsgroup-expirable)
4190                             ;; Only insert the Subject string when it's different
4191                             ;; from the previous Subject string.
4192                             (if (and
4193                                  gnus-show-threads
4194                                  (gnus-subject-equal
4195                                   (condition-case ()
4196                                       (mail-header-subject
4197                                        (gnus-data-header
4198                                         (cadr
4199                                          (gnus-data-find-list
4200                                           article
4201                                           (gnus-data-list t)))))
4202                                     ;; Error on the side of excessive subjects.
4203                                     (error ""))
4204                                   (mail-header-subject header)))
4205                                 ""
4206                               (mail-header-subject header))
4207                             nil (cdr (assq article gnus-newsgroup-scored))
4208                             (memq article gnus-newsgroup-processable))
4209                            (point)))))
4210         (when (cdr datal)
4211           (gnus-data-update-list
4212            (cdr datal)
4213            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4214
4215 (defun gnus-summary-update-article (article &optional iheader)
4216   "Update ARTICLE in the summary buffer."
4217   (set-buffer gnus-summary-buffer)
4218   (let* ((header (gnus-summary-article-header article))
4219          (id (mail-header-id header))
4220          (data (gnus-data-find article))
4221          (thread (gnus-id-to-thread id))
4222          (references (mail-header-references header))
4223          (parent
4224           (gnus-id-to-thread
4225            (or (gnus-parent-id
4226                 (when (and references
4227                            (not (equal "" references)))
4228                   references))
4229                "none")))
4230          (buffer-read-only nil)
4231          (old (car thread)))
4232     (when thread
4233       (unless iheader
4234         (setcar thread nil)
4235         (when parent
4236           (delq thread parent)))
4237       (if (gnus-summary-insert-subject id header)
4238           ;; Set the (possibly) new article number in the data structure.
4239           (gnus-data-set-number data (gnus-id-to-article id))
4240         (setcar thread old)
4241         nil))))
4242
4243 (defun gnus-rebuild-thread (id &optional line)
4244   "Rebuild the thread containing ID.
4245 If LINE, insert the rebuilt thread starting on line LINE."
4246   (let ((buffer-read-only nil)
4247         old-pos current thread data)
4248     (if (not gnus-show-threads)
4249         (setq thread (list (car (gnus-id-to-thread id))))
4250       ;; Get the thread this article is part of.
4251       (setq thread (gnus-remove-thread id)))
4252     (setq old-pos (point-at-bol))
4253     (setq current (save-excursion
4254                     (and (re-search-backward "[\r\n]" nil t)
4255                          (gnus-summary-article-number))))
4256     ;; If this is a gathered thread, we have to go some re-gathering.
4257     (when (stringp (car thread))
4258       (let ((subject (car thread))
4259             roots thr)
4260         (setq thread (cdr thread))
4261         (while thread
4262           (unless (memq (setq thr (gnus-id-to-thread
4263                                    (gnus-root-id
4264                                     (mail-header-id (caar thread)))))
4265                         roots)
4266             (push thr roots))
4267           (setq thread (cdr thread)))
4268         ;; We now have all (unique) roots.
4269         (if (= (length roots) 1)
4270             ;; All the loose roots are now one solid root.
4271             (setq thread (car roots))
4272           (setq thread (cons subject (gnus-sort-threads roots))))))
4273     (let (threads)
4274       ;; We then insert this thread into the summary buffer.
4275       (when line
4276         (goto-char (point-min))
4277         (forward-line (1- line)))
4278       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4279         (if gnus-show-threads
4280             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4281           (gnus-summary-prepare-unthreaded thread))
4282         (setq data (nreverse gnus-newsgroup-data))
4283         (setq threads gnus-newsgroup-threads))
4284       ;; We splice the new data into the data structure.
4285       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4286       ;;!!! then we want to insert at the beginning of the buffer.
4287       ;;!!! That happens to be true with Gnus now, but that may
4288       ;;!!! change in the future.  Perhaps.
4289       (gnus-data-enter-list
4290        (if line nil current) data (- (point) old-pos))
4291       (setq gnus-newsgroup-threads
4292             (nconc threads gnus-newsgroup-threads))
4293       (gnus-data-compute-positions))))
4294
4295 (defun gnus-number-to-header (number)
4296   "Return the header for article NUMBER."
4297   (let ((headers gnus-newsgroup-headers))
4298     (while (and headers
4299                 (not (= number (mail-header-number (car headers)))))
4300       (pop headers))
4301     (when headers
4302       (car headers))))
4303
4304 (defun gnus-parent-headers (in-headers &optional generation)
4305   "Return the headers of the GENERATIONeth parent of HEADERS."
4306   (unless generation
4307     (setq generation 1))
4308   (let ((parent t)
4309         (headers in-headers)
4310         references)
4311     (while (and parent
4312                 (not (zerop generation))
4313                 (setq references (mail-header-references headers)))
4314       (setq headers (if (and references
4315                              (setq parent (gnus-parent-id references)))
4316                         (car (gnus-id-to-thread parent))
4317                       nil))
4318       (decf generation))
4319     (and (not (eq headers in-headers))
4320          headers)))
4321
4322 (defun gnus-id-to-thread (id)
4323   "Return the (sub-)thread where ID appears."
4324   (gnus-gethash id gnus-newsgroup-dependencies))
4325
4326 (defun gnus-id-to-article (id)
4327   "Return the article number of ID."
4328   (let ((thread (gnus-id-to-thread id)))
4329     (when (and thread
4330                (car thread))
4331       (mail-header-number (car thread)))))
4332
4333 (defun gnus-id-to-header (id)
4334   "Return the article headers of ID."
4335   (car (gnus-id-to-thread id)))
4336
4337 (defun gnus-article-displayed-root-p (article)
4338   "Say whether ARTICLE is a root(ish) article."
4339   (let ((level (gnus-summary-thread-level article))
4340         (refs (mail-header-references  (gnus-summary-article-header article)))
4341         particle)
4342     (cond
4343      ((null level) nil)
4344      ((zerop level) t)
4345      ((null refs) t)
4346      ((null (gnus-parent-id refs)) t)
4347      ((and (= 1 level)
4348            (null (setq particle (gnus-id-to-article
4349                                  (gnus-parent-id refs))))
4350            (null (gnus-summary-thread-level particle)))))))
4351
4352 (defun gnus-root-id (id)
4353   "Return the id of the root of the thread where ID appears."
4354   (let (last-id prev)
4355     (while (and id (setq prev (car (gnus-id-to-thread id))))
4356       (setq last-id id
4357             id (gnus-parent-id (mail-header-references prev))))
4358     last-id))
4359
4360 (defun gnus-articles-in-thread (thread)
4361   "Return the list of articles in THREAD."
4362   (cons (mail-header-number (car thread))
4363         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4364
4365 (defun gnus-remove-thread (id &optional dont-remove)
4366   "Remove the thread that has ID in it."
4367   (let (headers thread last-id)
4368     ;; First go up in this thread until we find the root.
4369     (setq last-id (gnus-root-id id)
4370           headers (message-flatten-list (gnus-id-to-thread last-id)))
4371     ;; We have now found the real root of this thread.  It might have
4372     ;; been gathered into some loose thread, so we have to search
4373     ;; through the threads to find the thread we wanted.
4374     (let ((threads gnus-newsgroup-threads)
4375           sub)
4376       (while threads
4377         (setq sub (car threads))
4378         (if (stringp (car sub))
4379             ;; This is a gathered thread, so we look at the roots
4380             ;; below it to find whether this article is in this
4381             ;; gathered root.
4382             (progn
4383               (setq sub (cdr sub))
4384               (while sub
4385                 (when (member (caar sub) headers)
4386                   (setq thread (car threads)
4387                         threads nil
4388                         sub nil))
4389                 (setq sub (cdr sub))))
4390           ;; It's an ordinary thread, so we check it.
4391           (when (eq (car sub) (car headers))
4392             (setq thread sub
4393                   threads nil)))
4394         (setq threads (cdr threads)))
4395       ;; If this article is in no thread, then it's a root.
4396       (if thread
4397           (unless dont-remove
4398             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4399         (setq thread (gnus-id-to-thread last-id)))
4400       (when thread
4401         (prog1
4402             thread                      ; We return this thread.
4403           (unless dont-remove
4404             (if (stringp (car thread))
4405                 (progn
4406                   ;; If we use dummy roots, then we have to remove the
4407                   ;; dummy root as well.
4408                   (when (eq gnus-summary-make-false-root 'dummy)
4409                     ;; We go to the dummy root by going to
4410                     ;; the first sub-"thread", and then one line up.
4411                     (gnus-summary-goto-article
4412                      (mail-header-number (caadr thread)))
4413                     (forward-line -1)
4414                     (gnus-delete-line)
4415                     (gnus-data-compute-positions))
4416                   (setq thread (cdr thread))
4417                   (while thread
4418                     (gnus-remove-thread-1 (car thread))
4419                     (setq thread (cdr thread))))
4420               (gnus-remove-thread-1 thread))))))))
4421
4422 (defun gnus-remove-thread-1 (thread)
4423   "Remove the thread THREAD recursively."
4424   (let ((number (mail-header-number (pop thread)))
4425         d)
4426     (setq thread (reverse thread))
4427     (while thread
4428       (gnus-remove-thread-1 (pop thread)))
4429     (when (setq d (gnus-data-find number))
4430       (goto-char (gnus-data-pos d))
4431       (gnus-summary-show-thread)
4432       (gnus-data-remove
4433        number
4434        (- (point-at-bol)
4435           (prog1
4436               (1+ (point-at-eol))
4437             (gnus-delete-line)))))))
4438
4439 (defun gnus-sort-threads-1 (threads func)
4440   (sort (mapcar (lambda (thread)
4441                   (cons (car thread)
4442                         (and (cdr thread)
4443                              (gnus-sort-threads-1 (cdr thread) func))))
4444                 threads) func))
4445
4446 (defun gnus-sort-threads (threads)
4447   "Sort THREADS."
4448   (if (not gnus-thread-sort-functions)
4449       threads
4450     (gnus-message 8 "Sorting threads...")
4451     (let ((max-lisp-eval-depth 5000))
4452       (prog1 (gnus-sort-threads-1
4453          threads
4454          (gnus-make-sort-function gnus-thread-sort-functions))
4455         (gnus-message 8 "Sorting threads...done")))))
4456
4457 (defun gnus-sort-articles (articles)
4458   "Sort ARTICLES."
4459   (when gnus-article-sort-functions
4460     (gnus-message 7 "Sorting articles...")
4461     (prog1
4462         (setq gnus-newsgroup-headers
4463               (sort articles (gnus-make-sort-function
4464                               gnus-article-sort-functions)))
4465       (gnus-message 7 "Sorting articles...done"))))
4466
4467 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4468 (defmacro gnus-thread-header (thread)
4469   "Return header of first article in THREAD.
4470 Note that THREAD must never, ever be anything else than a variable -
4471 using some other form will lead to serious barfage."
4472   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4473   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4474   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4475         (vector thread) 2))
4476
4477 (defsubst gnus-article-sort-by-number (h1 h2)
4478   "Sort articles by article number."
4479   (< (mail-header-number h1)
4480      (mail-header-number h2)))
4481
4482 (defun gnus-thread-sort-by-number (h1 h2)
4483   "Sort threads by root article number."
4484   (gnus-article-sort-by-number
4485    (gnus-thread-header h1) (gnus-thread-header h2)))
4486
4487 (defsubst gnus-article-sort-by-random (h1 h2)
4488   "Sort articles by article number."
4489   (zerop (random 2)))
4490
4491 (defun gnus-thread-sort-by-random (h1 h2)
4492   "Sort threads by root article number."
4493   (gnus-article-sort-by-random
4494    (gnus-thread-header h1) (gnus-thread-header h2)))
4495
4496 (defsubst gnus-article-sort-by-lines (h1 h2)
4497   "Sort articles by article Lines header."
4498   (< (mail-header-lines h1)
4499      (mail-header-lines h2)))
4500
4501 (defun gnus-thread-sort-by-lines (h1 h2)
4502   "Sort threads by root article Lines header."
4503   (gnus-article-sort-by-lines
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defsubst gnus-article-sort-by-chars (h1 h2)
4507   "Sort articles by octet length."
4508   (< (mail-header-chars h1)
4509      (mail-header-chars h2)))
4510
4511 (defun gnus-thread-sort-by-chars (h1 h2)
4512   "Sort threads by root article octet length."
4513   (gnus-article-sort-by-chars
4514    (gnus-thread-header h1) (gnus-thread-header h2)))
4515
4516 (defsubst gnus-article-sort-by-author (h1 h2)
4517   "Sort articles by root author."
4518   (string-lessp
4519    (let ((extract (funcall
4520                    gnus-extract-address-components
4521                    (mail-header-from h1))))
4522      (or (car extract) (cadr extract) ""))
4523    (let ((extract (funcall
4524                    gnus-extract-address-components
4525                    (mail-header-from h2))))
4526      (or (car extract) (cadr extract) ""))))
4527
4528 (defun gnus-thread-sort-by-author (h1 h2)
4529   "Sort threads by root author."
4530   (gnus-article-sort-by-author
4531    (gnus-thread-header h1)  (gnus-thread-header h2)))
4532
4533 (defsubst gnus-article-sort-by-subject (h1 h2)
4534   "Sort articles by root subject."
4535   (string-lessp
4536    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4537    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4538
4539 (defun gnus-thread-sort-by-subject (h1 h2)
4540   "Sort threads by root subject."
4541   (gnus-article-sort-by-subject
4542    (gnus-thread-header h1) (gnus-thread-header h2)))
4543
4544 (defsubst gnus-article-sort-by-date (h1 h2)
4545   "Sort articles by root article date."
4546   (time-less-p
4547    (gnus-date-get-time (mail-header-date h1))
4548    (gnus-date-get-time (mail-header-date h2))))
4549
4550 (defun gnus-thread-sort-by-date (h1 h2)
4551   "Sort threads by root article date."
4552   (gnus-article-sort-by-date
4553    (gnus-thread-header h1) (gnus-thread-header h2)))
4554
4555 (defsubst gnus-article-sort-by-score (h1 h2)
4556   "Sort articles by root article score.
4557 Unscored articles will be counted as having a score of zero."
4558   (> (or (cdr (assq (mail-header-number h1)
4559                     gnus-newsgroup-scored))
4560          gnus-summary-default-score 0)
4561      (or (cdr (assq (mail-header-number h2)
4562                     gnus-newsgroup-scored))
4563          gnus-summary-default-score 0)))
4564
4565 (defun gnus-thread-sort-by-score (h1 h2)
4566   "Sort threads by root article score."
4567   (gnus-article-sort-by-score
4568    (gnus-thread-header h1) (gnus-thread-header h2)))
4569
4570 (defun gnus-thread-sort-by-total-score (h1 h2)
4571   "Sort threads by the sum of all scores in the thread.
4572 Unscored articles will be counted as having a score of zero."
4573   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4574
4575 (defun gnus-thread-total-score (thread)
4576   ;; This function find the total score of THREAD.
4577   (cond
4578    ((null thread)
4579     0)
4580    ((consp thread)
4581     (if (stringp (car thread))
4582         (apply gnus-thread-score-function 0
4583                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4584       (gnus-thread-total-score-1 thread)))
4585    (t
4586     (gnus-thread-total-score-1 (list thread)))))
4587
4588 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4589   "Sort threads such that the thread with the most recently arrived article comes first."
4590   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4591
4592 (defun gnus-thread-highest-number (thread)
4593   "Return the highest article number in THREAD."
4594   (apply 'max (mapcar (lambda (header)
4595                         (mail-header-number header))
4596                       (message-flatten-list thread))))
4597
4598 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4599   "Sort threads such that the thread with the most recently dated article comes first."
4600   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4601
4602 (defun gnus-thread-latest-date (thread)
4603   "Return the highest article date in THREAD."
4604   (let ((previous-time 0))
4605     (apply 'max
4606            (mapcar
4607             (lambda (header)
4608               (setq previous-time
4609                     (condition-case ()
4610                         (time-to-seconds (mail-header-parse-date
4611                                           (mail-header-date header)))
4612                       (error previous-time))))
4613             (sort
4614              (message-flatten-list thread)
4615              (lambda (h1 h2)
4616                (< (mail-header-number h1)
4617                   (mail-header-number h2))))))))
4618
4619 (defun gnus-thread-total-score-1 (root)
4620   ;; This function find the total score of the thread below ROOT.
4621   (setq root (car root))
4622   (apply gnus-thread-score-function
4623          (or (append
4624               (mapcar 'gnus-thread-total-score
4625                       (cdr (gnus-id-to-thread (mail-header-id root))))
4626               (when (> (mail-header-number root) 0)
4627                 (list (or (cdr (assq (mail-header-number root)
4628                                      gnus-newsgroup-scored))
4629                           gnus-summary-default-score 0))))
4630              (list gnus-summary-default-score)
4631              '(0))))
4632
4633 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4634 (defvar gnus-tmp-prev-subject nil)
4635 (defvar gnus-tmp-false-parent nil)
4636 (defvar gnus-tmp-root-expunged nil)
4637 (defvar gnus-tmp-dummy-line nil)
4638
4639 (eval-when-compile (defvar gnus-tmp-header))
4640 (defun gnus-extra-header (type &optional header)
4641   "Return the extra header of TYPE."
4642   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4643       ""))
4644
4645 (defvar gnus-tmp-thread-tree-header-string "")
4646
4647 (defcustom gnus-sum-thread-tree-root "> "
4648   "With %B spec, used for the root of a thread.
4649 If nil, use subject instead."
4650   :type '(radio (const :format "%v  " nil) (string :size 0))
4651   :group 'gnus-thread)
4652 (defcustom gnus-sum-thread-tree-false-root "> "
4653   "With %B spec, used for a false root of a thread.
4654 If nil, use subject instead."
4655   :type '(radio (const :format "%v  " nil) (string :size 0))
4656   :group 'gnus-thread)
4657 (defcustom gnus-sum-thread-tree-single-indent ""
4658   "With %B spec, used for a thread with just one message.
4659 If nil, use subject instead."
4660   :type '(radio (const :format "%v  " nil) (string :size 0))
4661   :group 'gnus-thread)
4662 (defcustom gnus-sum-thread-tree-vertical "| "
4663   "With %B spec, used for drawing a vertical line."
4664   :type 'string
4665   :group 'gnus-thread)
4666 (defcustom gnus-sum-thread-tree-indent "  "
4667   "With %B spec, used for indenting."
4668   :type 'string
4669   :group 'gnus-thread)
4670 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4671   "With %B spec, used for a leaf with brothers."
4672   :type 'string
4673   :group 'gnus-thread)
4674 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4675   "With %B spec, used for a leaf without brothers."
4676   :type 'string
4677   :group 'gnus-thread)
4678
4679 (defun gnus-summary-prepare-threads (threads)
4680   "Prepare summary buffer from THREADS and indentation LEVEL.
4681 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4682 or a straight list of headers."
4683   (gnus-message 7 "Generating summary...")
4684
4685   (setq gnus-newsgroup-threads threads)
4686   (beginning-of-line)
4687
4688   (let ((gnus-tmp-level 0)
4689         (default-score (or gnus-summary-default-score 0))
4690         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4691         (building-line-count gnus-summary-display-while-building)
4692         (building-count (integerp gnus-summary-display-while-building))
4693         thread number subject stack state gnus-tmp-gathered beg-match
4694         new-roots gnus-tmp-new-adopts thread-end simp-subject
4695         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4696         gnus-tmp-replied gnus-tmp-subject-or-nil
4697         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4698         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4699         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4700         tree-stack)
4701
4702     (setq gnus-tmp-prev-subject nil
4703           gnus-tmp-thread-tree-header-string "")
4704
4705     (if (vectorp (car threads))
4706         ;; If this is a straight (sic) list of headers, then a
4707         ;; threaded summary display isn't required, so we just create
4708         ;; an unthreaded one.
4709         (gnus-summary-prepare-unthreaded threads)
4710
4711       ;; Do the threaded display.
4712
4713       (if gnus-summary-display-while-building
4714           (switch-to-buffer (buffer-name)))
4715       (while (or threads stack gnus-tmp-new-adopts new-roots)
4716
4717         (if (and (= gnus-tmp-level 0)
4718                  (or (not stack)
4719                      (= (caar stack) 0))
4720                  (not gnus-tmp-false-parent)
4721                  (or gnus-tmp-new-adopts new-roots))
4722             (if gnus-tmp-new-adopts
4723                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4724                       thread (list (car gnus-tmp-new-adopts))
4725                       gnus-tmp-header (caar thread)
4726                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4727               (when new-roots
4728                 (setq thread (list (car new-roots))
4729                       gnus-tmp-header (caar thread)
4730                       new-roots (cdr new-roots))))
4731
4732           (if threads
4733               ;; If there are some threads, we do them before the
4734               ;; threads on the stack.
4735               (setq thread threads
4736                     gnus-tmp-header (caar thread))
4737             ;; There were no current threads, so we pop something off
4738             ;; the stack.
4739             (setq state (car stack)
4740                   gnus-tmp-level (car state)
4741                   tree-stack (cadr state)
4742                   thread (caddr state)
4743                   stack (cdr stack)
4744                   gnus-tmp-header (caar thread))))
4745
4746         (setq gnus-tmp-false-parent nil)
4747         (setq gnus-tmp-root-expunged nil)
4748         (setq thread-end nil)
4749
4750         (if (stringp gnus-tmp-header)
4751             ;; The header is a dummy root.
4752             (cond
4753              ((eq gnus-summary-make-false-root 'adopt)
4754               ;; We let the first article adopt the rest.
4755               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4756                                                (cddar thread)))
4757               (setq gnus-tmp-gathered
4758                     (nconc (mapcar
4759                             (lambda (h) (mail-header-number (car h)))
4760                             (cddar thread))
4761                            gnus-tmp-gathered))
4762               (setq thread (cons (list (caar thread)
4763                                        (cadar thread))
4764                                  (cdr thread)))
4765               (setq gnus-tmp-level -1
4766                     gnus-tmp-false-parent t))
4767              ((eq gnus-summary-make-false-root 'empty)
4768               ;; We print adopted articles with empty subject fields.
4769               (setq gnus-tmp-gathered
4770                     (nconc (mapcar
4771                             (lambda (h) (mail-header-number (car h)))
4772                             (cddar thread))
4773                            gnus-tmp-gathered))
4774               (setq gnus-tmp-level -1))
4775              ((eq gnus-summary-make-false-root 'dummy)
4776               ;; We remember that we probably want to output a dummy
4777               ;; root.
4778               (setq gnus-tmp-dummy-line gnus-tmp-header)
4779               (setq gnus-tmp-prev-subject gnus-tmp-header))
4780              (t
4781               ;; We do not make a root for the gathered
4782               ;; sub-threads at all.
4783               (setq gnus-tmp-level -1)))
4784
4785           (setq number (mail-header-number gnus-tmp-header)
4786                 subject (mail-header-subject gnus-tmp-header)
4787                 simp-subject (gnus-simplify-subject-fully subject))
4788
4789           (cond
4790            ;; If the thread has changed subject, we might want to make
4791            ;; this subthread into a root.
4792            ((and (null gnus-thread-ignore-subject)
4793                  (not (zerop gnus-tmp-level))
4794                  gnus-tmp-prev-subject
4795                  (not (string= gnus-tmp-prev-subject simp-subject)))
4796             (setq new-roots (nconc new-roots (list (car thread)))
4797                   thread-end t
4798                   gnus-tmp-header nil))
4799            ;; If the article lies outside the current limit,
4800            ;; then we do not display it.
4801            ((not (memq number gnus-newsgroup-limit))
4802             (setq gnus-tmp-gathered
4803                   (nconc (mapcar
4804                           (lambda (h) (mail-header-number (car h)))
4805                           (cdar thread))
4806                          gnus-tmp-gathered))
4807             (setq gnus-tmp-new-adopts (if (cdar thread)
4808                                           (append gnus-tmp-new-adopts
4809                                                   (cdar thread))
4810                                         gnus-tmp-new-adopts)
4811                   thread-end t
4812                   gnus-tmp-header nil)
4813             (when (zerop gnus-tmp-level)
4814               (setq gnus-tmp-root-expunged t)))
4815            ;; Perhaps this article is to be marked as read?
4816            ((and gnus-summary-mark-below
4817                  (< (or (cdr (assq number gnus-newsgroup-scored))
4818                         default-score)
4819                     gnus-summary-mark-below)
4820                  ;; Don't touch sparse articles.
4821                  (not (gnus-summary-article-sparse-p number))
4822                  (not (gnus-summary-article-ancient-p number)))
4823             (setq gnus-newsgroup-unreads
4824                   (delq number gnus-newsgroup-unreads))
4825             (if gnus-newsgroup-auto-expire
4826                 (setq gnus-newsgroup-expirable
4827                       (gnus-add-to-sorted-list
4828                        gnus-newsgroup-expirable number))
4829               (push (cons number gnus-low-score-mark)
4830                     gnus-newsgroup-reads))))
4831
4832           (when gnus-tmp-header
4833             ;; We may have an old dummy line to output before this
4834             ;; article.
4835             (when (and gnus-tmp-dummy-line
4836                        (gnus-subject-equal
4837                         gnus-tmp-dummy-line
4838                         (mail-header-subject gnus-tmp-header)))
4839               (gnus-summary-insert-dummy-line
4840                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4841               (setq gnus-tmp-dummy-line nil))
4842
4843             ;; Compute the mark.
4844             (setq gnus-tmp-unread (gnus-article-mark number))
4845
4846             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4847                                   gnus-tmp-header gnus-tmp-level)
4848                   gnus-newsgroup-data)
4849
4850             ;; Actually insert the line.
4851             (setq
4852              gnus-tmp-subject-or-nil
4853              (cond
4854               ((and gnus-thread-ignore-subject
4855                     gnus-tmp-prev-subject
4856                     (not (string= gnus-tmp-prev-subject simp-subject)))
4857                subject)
4858               ((zerop gnus-tmp-level)
4859                (if (and (eq gnus-summary-make-false-root 'empty)
4860                         (memq number gnus-tmp-gathered)
4861                         gnus-tmp-prev-subject
4862                         (string= gnus-tmp-prev-subject simp-subject))
4863                    gnus-summary-same-subject
4864                  subject))
4865               (t gnus-summary-same-subject)))
4866             (if (and (eq gnus-summary-make-false-root 'adopt)
4867                      (= gnus-tmp-level 1)
4868                      (memq number gnus-tmp-gathered))
4869                 (setq gnus-tmp-opening-bracket ?\<
4870                       gnus-tmp-closing-bracket ?\>)
4871               (setq gnus-tmp-opening-bracket ?\[
4872                     gnus-tmp-closing-bracket ?\]))
4873             (setq
4874              gnus-tmp-indentation
4875              (aref gnus-thread-indent-array gnus-tmp-level)
4876              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4877              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4878                                 gnus-summary-default-score 0)
4879              gnus-tmp-score-char
4880              (if (or (null gnus-summary-default-score)
4881                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4882                          gnus-summary-zcore-fuzz))
4883                  ?                      ;Whitespace
4884                (if (< gnus-tmp-score gnus-summary-default-score)
4885                    gnus-score-below-mark gnus-score-over-mark))
4886              gnus-tmp-replied
4887              (cond ((memq number gnus-newsgroup-processable)
4888                     gnus-process-mark)
4889                    ((memq number gnus-newsgroup-cached)
4890                     gnus-cached-mark)
4891                    ((memq number gnus-newsgroup-replied)
4892                     gnus-replied-mark)
4893                    ((memq number gnus-newsgroup-forwarded)
4894                     gnus-forwarded-mark)
4895                    ((memq number gnus-newsgroup-saved)
4896                     gnus-saved-mark)
4897                    ((memq number gnus-newsgroup-recent)
4898                     gnus-recent-mark)
4899                    ((memq number gnus-newsgroup-unseen)
4900                     gnus-unseen-mark)
4901                    (t gnus-no-mark))
4902              gnus-tmp-downloaded
4903              (cond ((memq number gnus-newsgroup-undownloaded)
4904                     gnus-undownloaded-mark)
4905                    (gnus-newsgroup-agentized
4906                     gnus-downloaded-mark)
4907                    (t
4908                     gnus-no-mark))
4909              gnus-tmp-from (mail-header-from gnus-tmp-header)
4910              gnus-tmp-name
4911              (cond
4912               ((string-match "<[^>]+> *$" gnus-tmp-from)
4913                (setq beg-match (match-beginning 0))
4914                (or (and (string-match "^\".+\"" gnus-tmp-from)
4915                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4916                    (substring gnus-tmp-from 0 beg-match)))
4917               ((string-match "(.+)" gnus-tmp-from)
4918                (substring gnus-tmp-from
4919                           (1+ (match-beginning 0)) (1- (match-end 0))))
4920               (t gnus-tmp-from))
4921
4922              ;; Do the %B string
4923              gnus-tmp-thread-tree-header-string
4924              (cond
4925               ((not gnus-show-threads) "")
4926               ((zerop gnus-tmp-level)
4927                (cond ((cdar thread)
4928                       (or gnus-sum-thread-tree-root subject))
4929                      (gnus-tmp-new-adopts
4930                       (or gnus-sum-thread-tree-false-root subject))
4931                      (t
4932                       (or gnus-sum-thread-tree-single-indent subject))))
4933               (t
4934                (concat (apply 'concat
4935                               (mapcar (lambda (item)
4936                                         (if (= item 1)
4937                                             gnus-sum-thread-tree-vertical
4938                                           gnus-sum-thread-tree-indent))
4939                                       (cdr (reverse tree-stack))))
4940                        (if (nth 1 thread)
4941                            gnus-sum-thread-tree-leaf-with-other
4942                          gnus-sum-thread-tree-single-leaf)))))
4943             (when (string= gnus-tmp-name "")
4944               (setq gnus-tmp-name gnus-tmp-from))
4945             (unless (numberp gnus-tmp-lines)
4946               (setq gnus-tmp-lines -1))
4947             (if (= gnus-tmp-lines -1)
4948                 (setq gnus-tmp-lines "?")
4949               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4950               (gnus-put-text-property
4951              (point)
4952              (progn (eval gnus-summary-line-format-spec) (point))
4953                'gnus-number number)
4954             (when gnus-visual-p
4955               (forward-line -1)
4956               (gnus-run-hooks 'gnus-summary-update-hook)
4957               (forward-line 1))
4958
4959             (setq gnus-tmp-prev-subject simp-subject)))
4960
4961         (when (nth 1 thread)
4962           (push (list (max 0 gnus-tmp-level)
4963                       (copy-sequence tree-stack)
4964                       (nthcdr 1 thread))
4965                 stack))
4966         (push (if (nth 1 thread) 1 0) tree-stack)
4967         (incf gnus-tmp-level)
4968         (setq threads (if thread-end nil (cdar thread)))
4969         (if gnus-summary-display-while-building
4970             (if building-count
4971                 (progn
4972                   ;; use a set frequency
4973                   (setq building-line-count (1- building-line-count))
4974                   (when (= building-line-count 0)
4975                     (sit-for 0)
4976                     (setq building-line-count
4977                           gnus-summary-display-while-building)))
4978               ;; always
4979               (sit-for 0)))
4980         (unless threads
4981           (setq gnus-tmp-level 0)))))
4982   (gnus-message 7 "Generating summary...done"))
4983
4984 (defun gnus-summary-prepare-unthreaded (headers)
4985   "Generate an unthreaded summary buffer based on HEADERS."
4986   (let (header number mark)
4987
4988     (beginning-of-line)
4989
4990     (while headers
4991       ;; We may have to root out some bad articles...
4992       (when (memq (setq number (mail-header-number
4993                                 (setq header (pop headers))))
4994                   gnus-newsgroup-limit)
4995         ;; Mark article as read when it has a low score.
4996         (when (and gnus-summary-mark-below
4997                    (< (or (cdr (assq number gnus-newsgroup-scored))
4998                           gnus-summary-default-score 0)
4999                       gnus-summary-mark-below)
5000                    (not (gnus-summary-article-ancient-p number)))
5001           (setq gnus-newsgroup-unreads
5002                 (delq number gnus-newsgroup-unreads))
5003           (if gnus-newsgroup-auto-expire
5004               (push number gnus-newsgroup-expirable)
5005             (push (cons number gnus-low-score-mark)
5006                   gnus-newsgroup-reads)))
5007
5008         (setq mark (gnus-article-mark number))
5009         (push (gnus-data-make number mark (1+ (point)) header 0)
5010               gnus-newsgroup-data)
5011         (gnus-summary-insert-line
5012          header 0 number
5013          (memq number gnus-newsgroup-undownloaded)
5014          mark (memq number gnus-newsgroup-replied)
5015          (memq number gnus-newsgroup-expirable)
5016          (mail-header-subject header) nil
5017          (cdr (assq number gnus-newsgroup-scored))
5018          (memq number gnus-newsgroup-processable))))))
5019
5020 (defun gnus-summary-remove-list-identifiers ()
5021   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5022   (let ((regexp (if (consp gnus-list-identifiers)
5023                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5024                   gnus-list-identifiers))
5025         changed subject)
5026     (when regexp
5027       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5028       (dolist (header gnus-newsgroup-headers)
5029         (setq subject (mail-header-subject header)
5030               changed nil)
5031         (while (string-match regexp subject)
5032           (setq subject
5033                 (concat (substring subject 0 (match-beginning 1))
5034                         (substring subject (match-end 0)))
5035                 changed t))
5036         (when changed
5037           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5038             (setq subject
5039                   (concat (substring subject 0 (match-beginning 1))
5040                           (substring subject (match-end 1)))))
5041           (mail-header-set-subject header subject))))))
5042
5043 (defun gnus-fetch-headers (articles)
5044   "Fetch headers of ARTICLES."
5045   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5046     (gnus-message 5 "Fetching headers for %s..." name)
5047     (prog1
5048         (if (eq 'nov
5049                 (setq gnus-headers-retrieved-by
5050                       (gnus-retrieve-headers
5051                        articles gnus-newsgroup-name
5052                        ;; We might want to fetch old headers, but
5053                        ;; not if there is only 1 article.
5054                        (and (or (and
5055                                  (not (eq gnus-fetch-old-headers 'some))
5056                                  (not (numberp gnus-fetch-old-headers)))
5057                                 (> (length articles) 1))
5058                             gnus-fetch-old-headers))))
5059             (gnus-get-newsgroup-headers-xover
5060              articles nil nil gnus-newsgroup-name t)
5061           (gnus-get-newsgroup-headers))
5062       (gnus-message 5 "Fetching headers for %s...done" name))))
5063
5064 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5065   "Select newsgroup GROUP.
5066 If READ-ALL is non-nil, all articles in the group are selected.
5067 If SELECT-ARTICLES, only select those articles from GROUP."
5068   (let* ((entry (gnus-group-entry group))
5069          ;;!!! Dirty hack; should be removed.
5070          (gnus-summary-ignore-duplicates
5071           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5072               t
5073             gnus-summary-ignore-duplicates))
5074          (info (nth 2 entry))
5075          articles fetched-articles cached)
5076
5077     (unless (gnus-check-server
5078              (set (make-local-variable 'gnus-current-select-method)
5079                   (gnus-find-method-for-group group)))
5080       (error "Couldn't open server"))
5081
5082     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5083         (gnus-activate-group group)     ; Or we can activate it...
5084         (progn                          ; Or we bug out.
5085           (when (equal major-mode 'gnus-summary-mode)
5086             (gnus-kill-buffer (current-buffer)))
5087           (error "Couldn't activate group %s: %s"
5088                  group (gnus-status-message group))))
5089
5090     (unless (gnus-request-group group t)
5091       (when (equal major-mode 'gnus-summary-mode)
5092         (gnus-kill-buffer (current-buffer)))
5093       (error "Couldn't request group %s: %s"
5094              group (gnus-status-message group)))
5095
5096     (when gnus-agent
5097       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5098       
5099       (setq gnus-summary-use-undownloaded-faces
5100             (gnus-agent-find-parameter
5101              group
5102              'agent-enable-undownloaded-faces)))
5103
5104     (setq gnus-newsgroup-name group
5105           gnus-newsgroup-unselected nil
5106           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5107
5108     (let ((display (gnus-group-find-parameter group 'display)))
5109       (setq gnus-newsgroup-display
5110             (cond
5111              ((not (zerop (or (car-safe read-all) 0)))
5112               ;; The user entered the group with C-u SPC/RET, let's show
5113               ;; all articles.
5114               'gnus-not-ignore)
5115              ((eq display 'all)
5116               'gnus-not-ignore)
5117              ((arrayp display)
5118               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5119              ((numberp display)
5120               ;; The following is probably the "correct" solution, but
5121               ;; it makes Gnus fetch all headers and then limit the
5122               ;; articles (which is slow), so instead we hack the
5123               ;; select-articles parameter instead. -- Simon Josefsson
5124               ;; <jas@kth.se>
5125               ;;
5126               ;; (gnus-byte-compile
5127               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5128               ;;                         display)))))
5129               (setq select-articles
5130                     (gnus-uncompress-range
5131                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5132                              (if (> tmp 0)
5133                                  tmp
5134                                1))
5135                            (cdr (gnus-active group)))))
5136               nil)
5137              (t
5138               nil))))
5139
5140     (gnus-summary-setup-default-charset)
5141
5142     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5143     (when (gnus-virtual-group-p group)
5144       (setq cached gnus-newsgroup-cached))
5145
5146     (setq gnus-newsgroup-unreads
5147           (gnus-sorted-ndifference
5148            (gnus-sorted-ndifference gnus-newsgroup-unreads
5149                                     gnus-newsgroup-marked)
5150            gnus-newsgroup-dormant))
5151
5152     (setq gnus-newsgroup-processable nil)
5153
5154     (gnus-update-read-articles group gnus-newsgroup-unreads)
5155
5156     ;; Adjust and set lists of article marks.
5157     (when info
5158       (gnus-adjust-marked-articles info))
5159     (if (setq articles select-articles)
5160         (setq gnus-newsgroup-unselected
5161               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5162       (setq articles (gnus-articles-to-read group read-all)))
5163
5164     (cond
5165      ((null articles)
5166       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5167       'quit)
5168      ((eq articles 0) nil)
5169      (t
5170       ;; Init the dependencies hash table.
5171       (setq gnus-newsgroup-dependencies
5172             (gnus-make-hashtable (length articles)))
5173       (gnus-set-global-variables)
5174       ;; Retrieve the headers and read them in.
5175
5176       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5177
5178       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5179       (when cached
5180         (setq gnus-newsgroup-cached cached))
5181
5182       ;; Suppress duplicates?
5183       (when gnus-suppress-duplicates
5184         (gnus-dup-suppress-articles))
5185
5186       ;; Set the initial limit.
5187       (setq gnus-newsgroup-limit (copy-sequence articles))
5188       ;; Remove canceled articles from the list of unread articles.
5189       (setq fetched-articles
5190             (mapcar (lambda (headers) (mail-header-number headers))
5191                     gnus-newsgroup-headers))
5192       (setq gnus-newsgroup-articles fetched-articles)
5193       (setq gnus-newsgroup-unreads
5194             (gnus-sorted-nintersection
5195              gnus-newsgroup-unreads fetched-articles))
5196       (gnus-compute-unseen-list)
5197
5198       ;; Removed marked articles that do not exist.
5199       (gnus-update-missing-marks
5200        (gnus-sorted-difference articles fetched-articles))
5201       ;; We might want to build some more threads first.
5202       (when (and gnus-fetch-old-headers
5203                  (eq gnus-headers-retrieved-by 'nov))
5204         (if (eq gnus-fetch-old-headers 'invisible)
5205             (gnus-build-all-threads)
5206           (gnus-build-old-threads)))
5207       ;; Let the Gnus agent mark articles as read.
5208       (when gnus-agent
5209         (gnus-agent-get-undownloaded-list))
5210       ;; Remove list identifiers from subject
5211       (when gnus-list-identifiers
5212         (gnus-summary-remove-list-identifiers))
5213       ;; Check whether auto-expire is to be done in this group.
5214       (setq gnus-newsgroup-auto-expire
5215             (gnus-group-auto-expirable-p group))
5216       ;; Set up the article buffer now, if necessary.
5217       (unless gnus-single-article-buffer
5218         (gnus-article-setup-buffer))
5219       ;; First and last article in this newsgroup.
5220       (when gnus-newsgroup-headers
5221         (setq gnus-newsgroup-begin
5222               (mail-header-number (car gnus-newsgroup-headers))
5223               gnus-newsgroup-end
5224               (mail-header-number
5225                (gnus-last-element gnus-newsgroup-headers))))
5226       ;; GROUP is successfully selected.
5227       (or gnus-newsgroup-headers t)))))
5228
5229 (defun gnus-compute-unseen-list ()
5230   ;; The `seen' marks are treated specially.
5231   (if (not gnus-newsgroup-seen)
5232       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5233     (setq gnus-newsgroup-unseen
5234           (gnus-inverse-list-range-intersection
5235            gnus-newsgroup-articles gnus-newsgroup-seen))))
5236
5237 (defun gnus-summary-display-make-predicate (display)
5238   (require 'gnus-agent)
5239   (when (= (length display) 1)
5240     (setq display (car display)))
5241   (unless gnus-summary-display-cache
5242     (dolist (elem (append '((unread . unread)
5243                             (read . read)
5244                             (unseen . unseen))
5245                           gnus-article-mark-lists))
5246       (push (cons (cdr elem)
5247                   (gnus-byte-compile
5248                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5249             gnus-summary-display-cache)))
5250   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5251         (gnus-category-predicate-cache gnus-summary-display-cache))
5252     (gnus-get-predicate display)))
5253
5254 ;; Uses the dynamically bound `number' variable.
5255 (eval-when-compile
5256   (defvar number))
5257 (defun gnus-article-marked-p (type &optional article)
5258   (let ((article (or article number)))
5259     (cond
5260      ((eq type 'tick)
5261       (memq article gnus-newsgroup-marked))
5262      ((eq type 'spam)
5263       (memq article gnus-newsgroup-spam-marked))
5264      ((eq type 'unsend)
5265       (memq article gnus-newsgroup-unsendable))
5266      ((eq type 'undownload)
5267       (memq article gnus-newsgroup-undownloaded))
5268      ((eq type 'download)
5269       (memq article gnus-newsgroup-downloadable))
5270      ((eq type 'unread)
5271       (memq article gnus-newsgroup-unreads))
5272      ((eq type 'read)
5273       (memq article gnus-newsgroup-reads))
5274      ((eq type 'dormant)
5275       (memq article gnus-newsgroup-dormant) )
5276      ((eq type 'expire)
5277       (memq article gnus-newsgroup-expirable))
5278      ((eq type 'reply)
5279       (memq article gnus-newsgroup-replied))
5280      ((eq type 'killed)
5281       (memq article gnus-newsgroup-killed))
5282      ((eq type 'bookmark)
5283       (assq article gnus-newsgroup-bookmarks))
5284      ((eq type 'score)
5285       (assq article gnus-newsgroup-scored))
5286      ((eq type 'save)
5287       (memq article gnus-newsgroup-saved))
5288      ((eq type 'cache)
5289       (memq article gnus-newsgroup-cached))
5290      ((eq type 'forward)
5291       (memq article gnus-newsgroup-forwarded))
5292      ((eq type 'seen)
5293       (not (memq article gnus-newsgroup-unseen)))
5294      ((eq type 'recent)
5295       (memq article gnus-newsgroup-recent))
5296      (t t))))
5297
5298 (defun gnus-articles-to-read (group &optional read-all)
5299   "Find out what articles the user wants to read."
5300   (let* ((articles
5301           ;; Select all articles if `read-all' is non-nil, or if there
5302           ;; are no unread articles.
5303           (if (or read-all
5304                   (and (zerop (length gnus-newsgroup-marked))
5305                        (zerop (length gnus-newsgroup-unreads)))
5306                   ;; Fetch all if the predicate is non-nil.
5307                   gnus-newsgroup-display)
5308               ;; We want to select the headers for all the articles in
5309               ;; the group, so we select either all the active
5310               ;; articles in the group, or (if that's nil), the
5311               ;; articles in the cache.
5312               (or
5313                (gnus-uncompress-range (gnus-active group))
5314                (gnus-cache-articles-in-group group))
5315             ;; Select only the "normal" subset of articles.
5316             (gnus-sorted-nunion
5317              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5318              gnus-newsgroup-unreads)))
5319          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5320          (scored (length scored-list))
5321          (number (length articles))
5322          (marked (+ (length gnus-newsgroup-marked)
5323                     (length gnus-newsgroup-dormant)))
5324          (select
5325           (cond
5326            ((numberp read-all)
5327             read-all)
5328            ((numberp gnus-newsgroup-display)
5329             gnus-newsgroup-display)
5330            (t
5331             (condition-case ()
5332                 (cond
5333                  ((and (or (<= scored marked) (= scored number))
5334                        (numberp gnus-large-newsgroup)
5335                        (> number gnus-large-newsgroup))
5336                   (let* ((cursor-in-echo-area nil)
5337                          (initial (gnus-parameter-large-newsgroup-initial
5338                                    gnus-newsgroup-name))
5339                          (input
5340                           (read-string
5341                            (format
5342                             "How many articles from %s (%s %d): "
5343                             (gnus-limit-string
5344                              (gnus-group-decoded-name gnus-newsgroup-name)
5345                              35)
5346                             (if initial "max" "default")
5347                             number)
5348                            (if initial
5349                                (cons (number-to-string initial)
5350                                      0)))))
5351                     (if (string-match "^[ \t]*$" input) number input)))
5352                  ((and (> scored marked) (< scored number)
5353                        (> (- scored number) 20))
5354                   (let ((input
5355                          (read-string
5356                           (format "%s %s (%d scored, %d total): "
5357                                   "How many articles from"
5358                                   (gnus-group-decoded-name group)
5359                                   scored number))))
5360                     (if (string-match "^[ \t]*$" input)
5361                         number input)))
5362                  (t number))
5363               (quit
5364                (message "Quit getting the articles to read")
5365                nil))))))
5366     (setq select (if (stringp select) (string-to-number select) select))
5367     (if (or (null select) (zerop select))
5368         select
5369       (if (and (not (zerop scored)) (<= (abs select) scored))
5370           (progn
5371             (setq articles (sort scored-list '<))
5372             (setq number (length articles)))
5373         (setq articles (copy-sequence articles)))
5374
5375       (when (< (abs select) number)
5376         (if (< select 0)
5377             ;; Select the N oldest articles.
5378             (setcdr (nthcdr (1- (abs select)) articles) nil)
5379           ;; Select the N most recent articles.
5380           (setq articles (nthcdr (- number select) articles))))
5381       (setq gnus-newsgroup-unselected
5382             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5383       (when gnus-alter-articles-to-read-function
5384         (setq articles
5385               (sort
5386                (funcall gnus-alter-articles-to-read-function
5387                         gnus-newsgroup-name articles)
5388                '<)))
5389       articles)))
5390
5391 (defun gnus-killed-articles (killed articles)
5392   (let (out)
5393     (while articles
5394       (when (inline (gnus-member-of-range (car articles) killed))
5395         (push (car articles) out))
5396       (setq articles (cdr articles)))
5397     out))
5398
5399 (defun gnus-uncompress-marks (marks)
5400   "Uncompress the mark ranges in MARKS."
5401   (let ((uncompressed '(score bookmark))
5402         out)
5403     (while marks
5404       (if (memq (caar marks) uncompressed)
5405           (push (car marks) out)
5406         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5407       (setq marks (cdr marks)))
5408     out))
5409
5410 (defun gnus-article-mark-to-type (mark)
5411   "Return the type of MARK."
5412   (or (cadr (assq mark gnus-article-special-mark-lists))
5413       'list))
5414
5415 (defun gnus-article-unpropagatable-p (mark)
5416   "Return whether MARK should be propagated to back end."
5417   (memq mark gnus-article-unpropagated-mark-lists))
5418
5419 (defun gnus-adjust-marked-articles (info)
5420   "Set all article lists and remove all marks that are no longer valid."
5421   (let* ((marked-lists (gnus-info-marks info))
5422          (active (gnus-active (gnus-info-group info)))
5423          (min (car active))
5424          (max (cdr active))
5425          (types gnus-article-mark-lists)
5426          marks var articles article mark mark-type
5427          bgn end)
5428
5429     (dolist (marks marked-lists)
5430       (setq mark (car marks)
5431             mark-type (gnus-article-mark-to-type mark)
5432             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5433
5434       ;; We set the variable according to the type of the marks list,
5435       ;; and then adjust the marks to a subset of the active articles.
5436       (cond
5437        ;; Adjust "simple" lists - compressed yet unsorted
5438        ((eq mark-type 'list)
5439         ;; Simultaneously uncompress and clip to active range
5440         ;; See gnus-uncompress-range for a description of possible marks
5441         (let (l lh)
5442           (if (not (cadr marks))
5443               (set var nil)
5444             (setq articles (if (numberp (cddr marks))
5445                                (list (cdr marks))
5446                              (cdr marks))
5447                   lh (cons nil nil)
5448                   l lh)
5449
5450             (while (setq article (pop articles))
5451               (cond ((consp article)
5452                      (setq bgn (max (car article) min)
5453                            end (min (cdr article) max))
5454                      (while (<= bgn end)
5455                        (setq l (setcdr l (cons bgn nil))
5456                              bgn (1+ bgn))))
5457                     ((and (<= min article)
5458                           (>= max article))
5459                      (setq l (setcdr l (cons article nil))))))
5460             (set var (cdr lh)))))
5461        ;; Adjust assocs.
5462        ((eq mark-type 'tuple)
5463         (set var (setq articles (cdr marks)))
5464         (when (not (listp (cdr (symbol-value var))))
5465           (set var (list (symbol-value var))))
5466         (when (not (listp (cdr articles)))
5467           (setq articles (list articles)))
5468         (while articles
5469           (when (or (not (consp (setq article (pop articles))))
5470                     (< (car article) min)
5471                     (> (car article) max))
5472             (set var (delq article (symbol-value var))))))
5473        ;; Adjust ranges (sloppily).
5474        ((eq mark-type 'range)
5475         (cond
5476          ((eq mark 'seen)
5477           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5478           ;; It should be (seen (NUM1 . NUM2)).
5479           (when (numberp (cddr marks))
5480             (setcdr marks (list (cdr marks))))
5481           (setq articles (cdr marks))
5482           (while (and articles
5483                       (or (and (consp (car articles))
5484                                (> min (cdar articles)))
5485                           (and (numberp (car articles))
5486                                (> min (car articles)))))
5487             (pop articles))
5488           (set var articles))))))))
5489
5490 (defun gnus-update-missing-marks (missing)
5491   "Go through the list of MISSING articles and remove them from the mark lists."
5492   (when missing
5493     (let (var m)
5494       ;; Go through all types.
5495       (dolist (elem gnus-article-mark-lists)
5496         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5497           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5498           (when (symbol-value var)
5499             ;; This list has articles.  So we delete all missing
5500             ;; articles from it.
5501             (setq m missing)
5502             (while m
5503               (set var (delq (pop m) (symbol-value var))))))))))
5504
5505 (defun gnus-update-marks ()
5506   "Enter the various lists of marked articles into the newsgroup info list."
5507   (let ((types gnus-article-mark-lists)
5508         (info (gnus-get-info gnus-newsgroup-name))
5509         type list newmarked symbol delta-marks)
5510     (when info
5511       ;; Add all marks lists to the list of marks lists.
5512       (while (setq type (pop types))
5513         (setq list (symbol-value
5514                     (setq symbol
5515                           (intern (format "gnus-newsgroup-%s" (car type))))))
5516
5517         (when list
5518           ;; Get rid of the entries of the articles that have the
5519           ;; default score.
5520           (when (and (eq (cdr type) 'score)
5521                      gnus-save-score
5522                      list)
5523             (let* ((arts list)
5524                    (prev (cons nil list))
5525                    (all prev))
5526               (while arts
5527                 (if (or (not (consp (car arts)))
5528                         (= (cdar arts) gnus-summary-default-score))
5529                     (setcdr prev (cdr arts))
5530                   (setq prev arts))
5531                 (setq arts (cdr arts)))
5532               (setq list (cdr all)))))
5533
5534         (when (eq (cdr type) 'seen)
5535           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5536
5537         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5538           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5539
5540         (when (and (gnus-check-backend-function
5541                     'request-set-mark gnus-newsgroup-name)
5542                    (not (gnus-article-unpropagatable-p (cdr type))))
5543           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5544                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5545                  (add (gnus-remove-from-range
5546                        (gnus-copy-sequence list) old)))
5547             (when add
5548               (push (list add 'add (list (cdr type))) delta-marks))
5549             (when del
5550               (push (list del 'del (list (cdr type))) delta-marks))))
5551
5552         (when list
5553           (push (cons (cdr type) list) newmarked)))
5554
5555       (when delta-marks
5556         (unless (gnus-check-group gnus-newsgroup-name)
5557           (error "Can't open server for %s" gnus-newsgroup-name))
5558         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5559
5560       ;; Enter these new marks into the info of the group.
5561       (if (nthcdr 3 info)
5562           (setcar (nthcdr 3 info) newmarked)
5563         ;; Add the marks lists to the end of the info.
5564         (when newmarked
5565           (setcdr (nthcdr 2 info) (list newmarked))))
5566
5567       ;; Cut off the end of the info if there's nothing else there.
5568       (let ((i 5))
5569         (while (and (> i 2)
5570                     (not (nth i info)))
5571           (when (nthcdr (decf i) info)
5572             (setcdr (nthcdr i info) nil)))))))
5573
5574 (defun gnus-set-mode-line (where)
5575   "Set the mode line of the article or summary buffers.
5576 If WHERE is `summary', the summary mode line format will be used."
5577   ;; Is this mode line one we keep updated?
5578   (when (and (memq where gnus-updated-mode-lines)
5579              (symbol-value
5580               (intern (format "gnus-%s-mode-line-format-spec" where))))
5581     (let (mode-string)
5582       (save-excursion
5583         ;; We evaluate this in the summary buffer since these
5584         ;; variables are buffer-local to that buffer.
5585         (set-buffer gnus-summary-buffer)
5586        ;; We bind all these variables that are used in the `eval' form
5587         ;; below.
5588         (let* ((mformat (symbol-value
5589                          (intern
5590                           (format "gnus-%s-mode-line-format-spec" where))))
5591                (gnus-tmp-group-name (gnus-group-decoded-name
5592                                      gnus-newsgroup-name))
5593                (gnus-tmp-article-number (or gnus-current-article 0))
5594                (gnus-tmp-unread gnus-newsgroup-unreads)
5595                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5596                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5597                (gnus-tmp-unread-and-unselected
5598                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5599                             (zerop gnus-tmp-unselected))
5600                        "")
5601                       ((zerop gnus-tmp-unselected)
5602                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5603                       (t (format "{%d(+%d) more}"
5604                                  gnus-tmp-unread-and-unticked
5605                                  gnus-tmp-unselected))))
5606                (gnus-tmp-subject
5607                 (if (and gnus-current-headers
5608                          (vectorp gnus-current-headers))
5609                     (gnus-mode-string-quote
5610                      (mail-header-subject gnus-current-headers))
5611                   ""))
5612                bufname-length max-len
5613                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5614           (setq mode-string (eval mformat))
5615           (setq bufname-length (if (string-match "%b" mode-string)
5616                                    (- (length
5617                                        (buffer-name
5618                                         (if (eq where 'summary)
5619                                             nil
5620                                           (get-buffer gnus-article-buffer))))
5621                                       2)
5622                                  0))
5623           (setq max-len (max 4 (if gnus-mode-non-string-length
5624                                    (- (window-width)
5625                                       gnus-mode-non-string-length
5626                                       bufname-length)
5627                                  (length mode-string))))
5628           ;; We might have to chop a bit of the string off...
5629           (when (> (length mode-string) max-len)
5630             (setq mode-string
5631                   (concat (truncate-string-to-width mode-string (- max-len 3))
5632                           "...")))
5633           ;; Pad the mode string a bit.
5634           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5635       ;; Update the mode line.
5636       (setq mode-line-buffer-identification
5637             (gnus-mode-line-buffer-identification (list mode-string)))
5638       (set-buffer-modified-p t))))
5639
5640 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5641   "Go through the HEADERS list and add all Xrefs to a hash table.
5642 The resulting hash table is returned, or nil if no Xrefs were found."
5643   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5644          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5645          (xref-hashtb (gnus-make-hashtable))
5646          start group entry number xrefs header)
5647     (while headers
5648       (setq header (pop headers))
5649       (when (and (setq xrefs (mail-header-xref header))
5650                  (not (memq (setq number (mail-header-number header))
5651                             unreads)))
5652         (setq start 0)
5653         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5654           (setq start (match-end 0))
5655           (setq group (if prefix
5656                           (concat prefix (substring xrefs (match-beginning 1)
5657                                                     (match-end 1)))
5658                         (substring xrefs (match-beginning 1) (match-end 1))))
5659           (setq number
5660                 (string-to-int (substring xrefs (match-beginning 2)
5661                                           (match-end 2))))
5662           (if (setq entry (gnus-gethash group xref-hashtb))
5663               (setcdr entry (cons number (cdr entry)))
5664             (gnus-sethash group (cons number nil) xref-hashtb)))))
5665     (and start xref-hashtb)))
5666
5667 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5668   "Look through all the headers and mark the Xrefs as read."
5669   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5670         name info xref-hashtb idlist method nth4)
5671     (save-excursion
5672       (set-buffer gnus-group-buffer)
5673       (when (setq xref-hashtb
5674                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5675         (mapatoms
5676          (lambda (group)
5677            (unless (string= from-newsgroup (setq name (symbol-name group)))
5678              (setq idlist (symbol-value group))
5679              ;; Dead groups are not updated.
5680              (and (prog1
5681                       (setq info (gnus-get-info name))
5682                     (when (stringp (setq nth4 (gnus-info-method info)))
5683                       (setq nth4 (gnus-server-to-method nth4))))
5684                   ;; Only do the xrefs if the group has the same
5685                   ;; select method as the group we have just read.
5686                   (or (gnus-methods-equal-p
5687                        nth4 (gnus-find-method-for-group from-newsgroup))
5688                       virtual
5689                       (equal nth4 (setq method (gnus-find-method-for-group
5690                                                 from-newsgroup)))
5691                       (and (equal (car nth4) (car method))
5692                            (equal (nth 1 nth4) (nth 1 method))))
5693                   gnus-use-cross-reference
5694                   (or (not (eq gnus-use-cross-reference t))
5695                       virtual
5696                       ;; Only do cross-references on subscribed
5697                       ;; groups, if that is what is wanted.
5698                       (<= (gnus-info-level info) gnus-level-subscribed))
5699                   (gnus-group-make-articles-read name idlist))))
5700          xref-hashtb)))))
5701
5702 (defun gnus-compute-read-articles (group articles)
5703   (let* ((entry (gnus-group-entry group))
5704          (info (nth 2 entry))
5705          (active (gnus-active group))
5706          ninfo)
5707     (when entry
5708       ;; First peel off all invalid article numbers.
5709       (when active
5710         (let ((ids articles)
5711               id first)
5712           (while (setq id (pop ids))
5713             (when (and first (> id (cdr active)))
5714               ;; We'll end up in this situation in one particular
5715               ;; obscure situation.  If you re-scan a group and get
5716               ;; a new article that is cross-posted to a different
5717               ;; group that has not been re-scanned, you might get
5718               ;; crossposted article that has a higher number than
5719               ;; Gnus believes possible.  So we re-activate this
5720               ;; group as well.  This might mean doing the
5721               ;; crossposting thingy will *increase* the number
5722               ;; of articles in some groups.  Tsk, tsk.
5723               (setq active (or (gnus-activate-group group) active)))
5724             (when (or (> id (cdr active))
5725                       (< id (car active)))
5726               (setq articles (delq id articles))))))
5727       ;; If the read list is nil, we init it.
5728       (if (and active
5729                (null (gnus-info-read info))
5730                (> (car active) 1))
5731           (setq ninfo (cons 1 (1- (car active))))
5732         (setq ninfo (gnus-info-read info)))
5733       ;; Then we add the read articles to the range.
5734       (gnus-add-to-range
5735        ninfo (setq articles (sort articles '<))))))
5736
5737 (defun gnus-group-make-articles-read (group articles)
5738   "Update the info of GROUP to say that ARTICLES are read."
5739   (let* ((num 0)
5740          (entry (gnus-group-entry group))
5741          (info (nth 2 entry))
5742          (active (gnus-active group))
5743          range)
5744     (when entry
5745       (setq range (gnus-compute-read-articles group articles))
5746       (with-current-buffer gnus-group-buffer
5747         (gnus-undo-register
5748           `(progn
5749              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5750              (gnus-info-set-read ',info ',(gnus-info-read info))
5751              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5752              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5753              (gnus-group-update-group ,group t))))
5754       ;; Add the read articles to the range.
5755       (gnus-info-set-read info range)
5756       (gnus-request-set-mark group (list (list range 'add '(read))))
5757       ;; Then we have to re-compute how many unread
5758       ;; articles there are in this group.
5759       (when active
5760         (cond
5761          ((not range)
5762           (setq num (- (1+ (cdr active)) (car active))))
5763          ((not (listp (cdr range)))
5764           (setq num (- (cdr active) (- (1+ (cdr range))
5765                                        (car range)))))
5766          (t
5767           (while range
5768             (if (numberp (car range))
5769                 (setq num (1+ num))
5770               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5771             (setq range (cdr range)))
5772           (setq num (- (cdr active) num))))
5773         ;; Update the number of unread articles.
5774         (setcar entry num)
5775         ;; Update the group buffer.
5776         (unless (gnus-ephemeral-group-p group)
5777           (gnus-group-update-group group t))))))
5778
5779 (defvar gnus-newsgroup-none-id 0)
5780
5781 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5782   (let ((cur nntp-server-buffer)
5783         (dependencies
5784          (or dependencies
5785              (with-current-buffer gnus-summary-buffer
5786                gnus-newsgroup-dependencies)))
5787         headers id end ref number
5788         (mail-parse-charset gnus-newsgroup-charset)
5789         (mail-parse-ignored-charsets
5790          (save-excursion (condition-case nil
5791                              (set-buffer gnus-summary-buffer)
5792                            (error))
5793                          gnus-newsgroup-ignored-charsets)))
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       ;; Translate all TAB characters into SPACE characters.
5797       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5799       (ietf-drums-unfold-fws)
5800       (gnus-run-hooks 'gnus-parse-headers-hook)
5801       (let ((case-fold-search t)
5802             in-reply-to header p lines chars)
5803         (goto-char (point-min))
5804         ;; Search to the beginning of the next header.  Error messages
5805         ;; do not begin with 2 or 3.
5806         (while (re-search-forward "^[23][0-9]+ " nil t)
5807           (setq id nil
5808                 ref nil)
5809           ;; This implementation of this function, with nine
5810           ;; search-forwards instead of the one re-search-forward and
5811           ;; a case (which basically was the old function) is actually
5812           ;; about twice as fast, even though it looks messier.  You
5813           ;; can't have everything, I guess.  Speed and elegance
5814           ;; doesn't always go hand in hand.
5815           (setq
5816            header
5817            (vector
5818             ;; Number.
5819             (prog1
5820                 (setq number (read cur))
5821               (end-of-line)
5822               (setq p (point))
5823               (narrow-to-region (point)
5824                                 (or (and (search-forward "\n.\n" nil t)
5825                                          (- (point) 2))
5826                                     (point))))
5827             ;; Subject.
5828             (progn
5829               (goto-char p)
5830               (if (search-forward "\nsubject:" nil t)
5831                   (funcall gnus-decode-encoded-word-function
5832                            (nnheader-header-value))
5833                 "(none)"))
5834             ;; From.
5835             (progn
5836               (goto-char p)
5837               (if (search-forward "\nfrom:" nil t)
5838                   (funcall gnus-decode-encoded-word-function
5839                            (nnheader-header-value))
5840                 "(nobody)"))
5841             ;; Date.
5842             (progn
5843               (goto-char p)
5844               (if (search-forward "\ndate:" nil t)
5845                   (nnheader-header-value) ""))
5846             ;; Message-ID.
5847             (progn
5848               (goto-char p)
5849               (setq id (if (re-search-forward
5850                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5851                            ;; We do it this way to make sure the Message-ID
5852                            ;; is (somewhat) syntactically valid.
5853                            (buffer-substring (match-beginning 1)
5854                                              (match-end 1))
5855                          ;; If there was no message-id, we just fake one
5856                          ;; to make subsequent routines simpler.
5857                          (nnheader-generate-fake-message-id number))))
5858             ;; References.
5859             (progn
5860               (goto-char p)
5861               (if (search-forward "\nreferences:" nil t)
5862                   (progn
5863                     (setq end (point))
5864                     (prog1
5865                         (nnheader-header-value)
5866                       (setq ref
5867                             (buffer-substring
5868                              (progn
5869                                (end-of-line)
5870                                (search-backward ">" end t)
5871                                (1+ (point)))
5872                              (progn
5873                                (search-backward "<" end t)
5874                                (point))))))
5875                 ;; Get the references from the in-reply-to header if there
5876                 ;; were no references and the in-reply-to header looks
5877                 ;; promising.
5878                 (if (and (search-forward "\nin-reply-to:" nil t)
5879                          (setq in-reply-to (nnheader-header-value))
5880                          (string-match "<[^>]+>" in-reply-to))
5881                     (let (ref2)
5882                       (setq ref (substring in-reply-to (match-beginning 0)
5883                                            (match-end 0)))
5884                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5885                         (setq ref2 (substring in-reply-to (match-beginning 0)
5886                                               (match-end 0)))
5887                         (when (> (length ref2) (length ref))
5888                           (setq ref ref2)))
5889                       ref)
5890                   (setq ref nil))))
5891             ;; Chars.
5892             (progn
5893               (goto-char p)
5894               (if (search-forward "\nchars: " nil t)
5895                   (if (numberp (setq chars (ignore-errors (read cur))))
5896                       chars -1)
5897                 -1))
5898             ;; Lines.
5899             (progn
5900               (goto-char p)
5901               (if (search-forward "\nlines: " nil t)
5902                   (if (numberp (setq lines (ignore-errors (read cur))))
5903                       lines -1)
5904                 -1))
5905             ;; Xref.
5906             (progn
5907               (goto-char p)
5908               (and (search-forward "\nxref:" nil t)
5909                    (nnheader-header-value)))
5910             ;; Extra.
5911             (when gnus-extra-headers
5912               (let ((extra gnus-extra-headers)
5913                     out)
5914                 (while extra
5915                   (goto-char p)
5916                   (when (search-forward
5917                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5918                     (push (cons (car extra) (nnheader-header-value))
5919                           out))
5920                   (pop extra))
5921                 out))))
5922           (when (equal id ref)
5923             (setq ref nil))
5924
5925           (when gnus-alter-header-function
5926             (funcall gnus-alter-header-function header)
5927             (setq id (mail-header-id header)
5928                   ref (gnus-parent-id (mail-header-references header))))
5929
5930           (when (setq header
5931                       (gnus-dependencies-add-header
5932                        header dependencies force-new))
5933             (push header headers))
5934           (goto-char (point-max))
5935           (widen))
5936         (nreverse headers)))))
5937
5938 ;; Goes through the xover lines and returns a list of vectors
5939 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5940                                                   force-new dependencies
5941                                                   group also-fetch-heads)
5942   "Parse the news overview data in the server buffer.
5943 Return a list of headers that match SEQUENCE (see
5944 `nntp-retrieve-headers')."
5945   ;; Get the Xref when the users reads the articles since most/some
5946   ;; NNTP servers do not include Xrefs when using XOVER.
5947   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5948   (let ((mail-parse-charset gnus-newsgroup-charset)
5949         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5950         (cur nntp-server-buffer)
5951         (dependencies (or dependencies gnus-newsgroup-dependencies))
5952         (allp (cond
5953                ((eq gnus-read-all-available-headers t)
5954                 t)
5955                ((stringp gnus-read-all-available-headers)
5956                 (string-match gnus-read-all-available-headers group))
5957                (t
5958                 nil)))
5959         number headers header)
5960     (save-excursion
5961       (set-buffer nntp-server-buffer)
5962       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5963       ;; Allow the user to mangle the headers before parsing them.
5964       (gnus-run-hooks 'gnus-parse-headers-hook)
5965       (goto-char (point-min))
5966       (gnus-parse-without-error
5967         (while (and (or sequence allp)
5968                     (not (eobp)))
5969           (setq number (read cur))
5970           (when (not allp)
5971             (while (and sequence
5972                         (< (car sequence) number))
5973               (setq sequence (cdr sequence))))
5974           (when (and (or allp
5975                          (and sequence
5976                               (eq number (car sequence))))
5977                      (progn
5978                        (setq sequence (cdr sequence))
5979                        (setq header (inline
5980                                       (gnus-nov-parse-line
5981                                        number dependencies force-new)))))
5982             (push header headers))
5983           (forward-line 1)))
5984       ;; A common bug in inn is that if you have posted an article and
5985       ;; then retrieves the active file, it will answer correctly --
5986       ;; the new article is included.  However, a NOV entry for the
5987       ;; article may not have been generated yet, so this may fail.
5988       ;; We work around this problem by retrieving the last few
5989       ;; headers using HEAD.
5990       (if (or (not also-fetch-heads)
5991               (not sequence))
5992           ;; We (probably) got all the headers.
5993           (nreverse headers)
5994         (let ((gnus-nov-is-evil t))
5995           (nconc
5996            (nreverse headers)
5997            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5998              (gnus-get-newsgroup-headers))))))))
5999
6000 (defun gnus-article-get-xrefs ()
6001   "Fill in the Xref value in `gnus-current-headers', if necessary.
6002 This is meant to be called in `gnus-article-internal-prepare-hook'."
6003   (let ((headers (with-current-buffer gnus-summary-buffer
6004                    gnus-current-headers)))
6005     (or (not gnus-use-cross-reference)
6006         (not headers)
6007         (and (mail-header-xref headers)
6008              (not (string= (mail-header-xref headers) "")))
6009         (let ((case-fold-search t)
6010               xref)
6011           (save-restriction
6012             (nnheader-narrow-to-headers)
6013             (goto-char (point-min))
6014             (when (or (and (not (eobp))
6015                            (eq (downcase (char-after)) ?x)
6016                            (looking-at "Xref:"))
6017                       (search-forward "\nXref:" nil t))
6018               (goto-char (1+ (match-end 0)))
6019               (setq xref (buffer-substring (point) (point-at-eol)))
6020               (mail-header-set-xref headers xref)))))))
6021
6022 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6023   "Find article ID and insert the summary line for that article.
6024 OLD-HEADER can either be a header or a line number to insert
6025 the subject line on."
6026   (let* ((line (and (numberp old-header) old-header))
6027          (old-header (and (vectorp old-header) old-header))
6028          (header (cond ((and old-header use-old-header)
6029                         old-header)
6030                        ((and (numberp id)
6031                              (gnus-number-to-header id))
6032                         (gnus-number-to-header id))
6033                        (t
6034                         (gnus-read-header id))))
6035          (number (and (numberp id) id))
6036          d)
6037     (when header
6038       ;; Rebuild the thread that this article is part of and go to the
6039       ;; article we have fetched.
6040       (when (and (not gnus-show-threads)
6041                  old-header)
6042         (when (and number
6043                    (setq d (gnus-data-find (mail-header-number old-header))))
6044           (goto-char (gnus-data-pos d))
6045           (gnus-data-remove
6046            number
6047            (- (point-at-bol)
6048               (prog1
6049                   (1+ (point-at-eol))
6050                 (gnus-delete-line))))))
6051       ;; Remove list identifiers from subject.
6052       (when gnus-list-identifiers
6053         (let ((gnus-newsgroup-headers (list header)))
6054           (gnus-summary-remove-list-identifiers)
6055           (setq header (car gnus-newsgroup-headers))))
6056       (when old-header
6057         (mail-header-set-number header (mail-header-number old-header)))
6058       (setq gnus-newsgroup-sparse
6059             (delq (setq number (mail-header-number header))
6060                   gnus-newsgroup-sparse))
6061       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6062       (push number gnus-newsgroup-limit)
6063       (gnus-rebuild-thread (mail-header-id header) line)
6064       (gnus-summary-goto-subject number nil t))
6065     (when (and (numberp number)
6066                (> number 0))
6067       ;; We have to update the boundaries even if we can't fetch the
6068       ;; article if ID is a number -- so that the next `P' or `N'
6069       ;; command will fetch the previous (or next) article even
6070       ;; if the one we tried to fetch this time has been canceled.
6071       (when (> number gnus-newsgroup-end)
6072         (setq gnus-newsgroup-end number))
6073       (when (< number gnus-newsgroup-begin)
6074         (setq gnus-newsgroup-begin number))
6075       (setq gnus-newsgroup-unselected
6076             (delq number gnus-newsgroup-unselected)))
6077     ;; Report back a success?
6078     (and header (mail-header-number header))))
6079
6080 ;;; Process/prefix in the summary buffer
6081
6082 (defun gnus-summary-work-articles (n)
6083   "Return a list of articles to be worked upon.
6084 The prefix argument, the list of process marked articles, and the
6085 current article will be taken into consideration."
6086   (save-excursion
6087     (set-buffer gnus-summary-buffer)
6088     (cond
6089      (n
6090       ;; A numerical prefix has been given.
6091       (setq n (prefix-numeric-value n))
6092       (let ((backward (< n 0))
6093             (n (abs (prefix-numeric-value n)))
6094             articles article)
6095         (save-excursion
6096           (while
6097               (and (> n 0)
6098                    (push (setq article (gnus-summary-article-number))
6099                          articles)
6100                    (if backward
6101                        (gnus-summary-find-prev nil article)
6102                      (gnus-summary-find-next nil article)))
6103             (decf n)))
6104         (nreverse articles)))
6105      ((and (gnus-region-active-p) (mark))
6106       (message "region active")
6107       ;; Work on the region between point and mark.
6108       (let ((max (max (point) (mark)))
6109             articles article)
6110         (save-excursion
6111           (goto-char (min (min (point) (mark))))
6112           (while
6113               (and
6114                (push (setq article (gnus-summary-article-number)) articles)
6115                (gnus-summary-find-next nil article)
6116                (< (point) max)))
6117           (nreverse articles))))
6118      (gnus-newsgroup-processable
6119       ;; There are process-marked articles present.
6120       ;; Save current state.
6121       (gnus-summary-save-process-mark)
6122       ;; Return the list.
6123       (reverse gnus-newsgroup-processable))
6124      (t
6125       ;; Just return the current article.
6126       (list (gnus-summary-article-number))))))
6127
6128 (defmacro gnus-summary-iterate (arg &rest forms)
6129   "Iterate over the process/prefixed articles and do FORMS.
6130 ARG is the interactive prefix given to the command.  FORMS will be
6131 executed with point over the summary line of the articles."
6132   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6133     `(let ((,articles (gnus-summary-work-articles ,arg)))
6134        (while ,articles
6135          (gnus-summary-goto-subject (car ,articles))
6136          ,@forms
6137          (pop ,articles)))))
6138
6139 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6140 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6141
6142 (defun gnus-summary-save-process-mark ()
6143   "Push the current set of process marked articles on the stack."
6144   (interactive)
6145   (push (copy-sequence gnus-newsgroup-processable)
6146         gnus-newsgroup-process-stack))
6147
6148 (defun gnus-summary-kill-process-mark ()
6149   "Push the current set of process marked articles on the stack and unmark."
6150   (interactive)
6151   (gnus-summary-save-process-mark)
6152   (gnus-summary-unmark-all-processable))
6153
6154 (defun gnus-summary-yank-process-mark ()
6155   "Pop the last process mark state off the stack and restore it."
6156   (interactive)
6157   (unless gnus-newsgroup-process-stack
6158     (error "Empty mark stack"))
6159   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6160
6161 (defun gnus-summary-process-mark-set (set)
6162   "Make SET into the current process marked articles."
6163   (gnus-summary-unmark-all-processable)
6164   (while set
6165     (gnus-summary-set-process-mark (pop set))))
6166
6167 ;;; Searching and stuff
6168
6169 (defun gnus-summary-search-group (&optional backward use-level)
6170   "Search for next unread newsgroup.
6171 If optional argument BACKWARD is non-nil, search backward instead."
6172   (save-excursion
6173     (set-buffer gnus-group-buffer)
6174     (when (gnus-group-search-forward
6175            backward nil (if use-level (gnus-group-group-level) nil))
6176       (gnus-group-group-name))))
6177
6178 (defun gnus-summary-best-group (&optional exclude-group)
6179   "Find the name of the best unread group.
6180 If EXCLUDE-GROUP, do not go to this group."
6181   (with-current-buffer gnus-group-buffer
6182     (save-excursion
6183       (gnus-group-best-unread-group exclude-group))))
6184
6185 (defun gnus-summary-find-next (&optional unread article backward)
6186   (if backward
6187       (gnus-summary-find-prev unread article)
6188     (let* ((dummy (gnus-summary-article-intangible-p))
6189            (article (or article (gnus-summary-article-number)))
6190            (data (gnus-data-find-list article))
6191            result)
6192       (when (and (not dummy)
6193                  (or (not gnus-summary-check-current)
6194                      (not unread)
6195                      (not (gnus-data-unread-p (car data)))))
6196         (setq data (cdr data)))
6197       (when (setq result
6198                   (if unread
6199                       (progn
6200                         (while data
6201                           (unless (memq (gnus-data-number (car data))
6202                                         (cond
6203                                          ((eq gnus-auto-goto-ignores
6204                                               'always-undownloaded)
6205                                           gnus-newsgroup-undownloaded)
6206                                          (gnus-plugged
6207                                           nil)
6208                                          ((eq gnus-auto-goto-ignores
6209                                               'unfetched)
6210                                           gnus-newsgroup-unfetched)
6211                                          ((eq gnus-auto-goto-ignores
6212                                               'undownloaded)
6213                                           gnus-newsgroup-undownloaded)))
6214                             (when (gnus-data-unread-p (car data))
6215                               (setq result (car data)
6216                                     data nil)))
6217                           (setq data (cdr data)))
6218                         result)
6219                     (car data)))
6220         (goto-char (gnus-data-pos result))
6221         (gnus-data-number result)))))
6222
6223 (defun gnus-summary-find-prev (&optional unread article)
6224   (let* ((eobp (eobp))
6225          (article (or article (gnus-summary-article-number)))
6226          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6227          result)
6228     (when (and (not eobp)
6229                (or (not gnus-summary-check-current)
6230                    (not unread)
6231                    (not (gnus-data-unread-p (car data)))))
6232       (setq data (cdr data)))
6233     (when (setq result
6234                 (if unread
6235                     (progn
6236                       (while data
6237                         (unless (memq (gnus-data-number (car data))
6238                                       (cond
6239                                        ((eq gnus-auto-goto-ignores
6240                                             'always-undownloaded)
6241                                         gnus-newsgroup-undownloaded)
6242                                        (gnus-plugged
6243                                         nil)
6244                                        ((eq gnus-auto-goto-ignores
6245                                             'unfetched)
6246                                         gnus-newsgroup-unfetched)
6247                                        ((eq gnus-auto-goto-ignores
6248                                             'undownloaded)
6249                                         gnus-newsgroup-undownloaded)))
6250                           (when (gnus-data-unread-p (car data))
6251                             (setq result (car data)
6252                                   data nil)))
6253                         (setq data (cdr data)))
6254                       result)
6255                   (car data)))
6256       (goto-char (gnus-data-pos result))
6257       (gnus-data-number result))))
6258
6259 (defun gnus-summary-find-subject (subject &optional unread backward article)
6260   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6261          (article (or article (gnus-summary-article-number)))
6262          (articles (gnus-data-list backward))
6263          (arts (gnus-data-find-list article articles))
6264          result)
6265     (when (or (not gnus-summary-check-current)
6266               (not unread)
6267               (not (gnus-data-unread-p (car arts))))
6268       (setq arts (cdr arts)))
6269     (while arts
6270       (and (or (not unread)
6271                (gnus-data-unread-p (car arts)))
6272            (vectorp (gnus-data-header (car arts)))
6273            (gnus-subject-equal
6274             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6275            (setq result (car arts)
6276                  arts nil))
6277       (setq arts (cdr arts)))
6278     (and result
6279          (goto-char (gnus-data-pos result))
6280          (gnus-data-number result))))
6281
6282 (defun gnus-summary-search-forward (&optional unread subject backward)
6283   "Search forward for an article.
6284 If UNREAD, look for unread articles.  If SUBJECT, look for
6285 articles with that subject.  If BACKWARD, search backward instead."
6286   (cond (subject (gnus-summary-find-subject subject unread backward))
6287         (backward (gnus-summary-find-prev unread))
6288         (t (gnus-summary-find-next unread))))
6289
6290 (defun gnus-recenter (&optional n)
6291   "Center point in window and redisplay frame.
6292 Also do horizontal recentering."
6293   (interactive "P")
6294   (when (and gnus-auto-center-summary
6295              (not (eq gnus-auto-center-summary 'vertical)))
6296     (gnus-horizontal-recenter))
6297   (recenter n))
6298
6299 (defun gnus-summary-recenter ()
6300   "Center point in the summary window.
6301 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6302 displayed, no centering will be performed."
6303   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6304   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6305   (interactive)
6306   ;; The user has to want it.
6307   (when gnus-auto-center-summary
6308     (let* ((top (cond ((< (window-height) 4) 0)
6309                       ((< (window-height) 7) 1)
6310                       (t (if (numberp gnus-auto-center-summary)
6311                              gnus-auto-center-summary
6312                            (/ (1- (window-height)) 2)))))
6313            (height (1- (window-height)))
6314            (bottom (save-excursion (goto-char (point-max))
6315                                    (forward-line (- height))
6316                                    (point)))
6317            (window (get-buffer-window (current-buffer))))
6318       (when (get-buffer-window gnus-article-buffer)
6319         ;; Only do recentering when the article buffer is displayed,
6320         ;; Set the window start to either `bottom', which is the biggest
6321         ;; possible valid number, or the second line from the top,
6322         ;; whichever is the least.
6323         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6324           (if (> bottom top-pos)
6325               ;; Keep the second line from the top visible
6326               (set-window-start window top-pos t)
6327             ;; Try to keep the bottom line visible; if it's partially
6328             ;; obscured, either scroll one more line to make it fully
6329             ;; visible, or revert to using TOP-POS.
6330             (save-excursion
6331               (goto-char (point-max))
6332               (forward-line -1)
6333               (let ((last-line-start (point)))
6334                 (goto-char bottom)
6335                 (set-window-start window (point) t)
6336                 (when (not (pos-visible-in-window-p last-line-start window))
6337                   (forward-line 1)
6338                   (set-window-start window (min (point) top-pos) t)))))))
6339       ;; Do horizontal recentering while we're at it.
6340       (when (and (get-buffer-window (current-buffer) t)
6341                  (not (eq gnus-auto-center-summary 'vertical)))
6342         (let ((selected (selected-window)))
6343           (select-window (get-buffer-window (current-buffer) t))
6344           (gnus-summary-position-point)
6345           (gnus-horizontal-recenter)
6346           (select-window selected))))))
6347
6348 (defun gnus-summary-jump-to-group (newsgroup)
6349   "Move point to NEWSGROUP in group mode buffer."
6350   ;; Keep update point of group mode buffer if visible.
6351   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6352       (save-window-excursion
6353         ;; Take care of tree window mode.
6354         (when (get-buffer-window gnus-group-buffer)
6355           (pop-to-buffer gnus-group-buffer))
6356         (gnus-group-jump-to-group newsgroup))
6357     (save-excursion
6358       ;; Take care of tree window mode.
6359       (if (get-buffer-window gnus-group-buffer)
6360           (pop-to-buffer gnus-group-buffer)
6361         (set-buffer gnus-group-buffer))
6362       (gnus-group-jump-to-group newsgroup))))
6363
6364 ;; This function returns a list of article numbers based on the
6365 ;; difference between the ranges of read articles in this group and
6366 ;; the range of active articles.
6367 (defun gnus-list-of-unread-articles (group)
6368   (let* ((read (gnus-info-read (gnus-get-info group)))
6369          (active (or (gnus-active group) (gnus-activate-group group)))
6370          (last (cdr active))
6371          first nlast unread)
6372     ;; If none are read, then all are unread.
6373     (if (not read)
6374         (setq first (car active))
6375       ;; If the range of read articles is a single range, then the
6376       ;; first unread article is the article after the last read
6377       ;; article.  Sounds logical, doesn't it?
6378       (if (and (not (listp (cdr read)))
6379                (or (< (car read) (car active))
6380                    (progn (setq read (list read))
6381                           nil)))
6382           (setq first (max (car active) (1+ (cdr read))))
6383         ;; `read' is a list of ranges.
6384         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6385                                   (caar read)))
6386                   1)
6387           (setq first (car active)))
6388         (while read
6389           (when first
6390             (while (< first nlast)
6391               (setq unread (cons first unread)
6392                     first (1+ first))))
6393           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6394           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6395           (setq read (cdr read)))))
6396     ;; And add the last unread articles.
6397     (while (<= first last)
6398       (setq unread (cons first unread)
6399             first (1+ first)))
6400     ;; Return the list of unread articles.
6401     (delq 0 (nreverse unread))))
6402
6403 (defun gnus-list-of-read-articles (group)
6404   "Return a list of unread, unticked and non-dormant articles."
6405   (let* ((info (gnus-get-info group))
6406          (marked (gnus-info-marks info))
6407          (active (gnus-active group)))
6408     (and info active
6409          (gnus-list-range-difference
6410           (gnus-list-range-difference
6411            (gnus-sorted-complement
6412             (gnus-uncompress-range active)
6413             (gnus-list-of-unread-articles group))
6414            (cdr (assq 'dormant marked)))
6415           (cdr (assq 'tick marked))))))
6416
6417 ;; This function returns a sequence of article numbers based on the
6418 ;; difference between the ranges of read articles in this group and
6419 ;; the range of active articles.
6420 (defun gnus-sequence-of-unread-articles (group)
6421   (let* ((read (gnus-info-read (gnus-get-info group)))
6422          (active (or (gnus-active group) (gnus-activate-group group)))
6423          (last (cdr active))
6424          first nlast unread)
6425     ;; If none are read, then all are unread.
6426     (if (not read)
6427         (setq first (car active))
6428       ;; If the range of read articles is a single range, then the
6429       ;; first unread article is the article after the last read
6430       ;; article.  Sounds logical, doesn't it?
6431       (if (and (not (listp (cdr read)))
6432                (or (< (car read) (car active))
6433                    (progn (setq read (list read))
6434                           nil)))
6435           (setq first (max (car active) (1+ (cdr read))))
6436         ;; `read' is a list of ranges.
6437         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6438                                   (caar read)))
6439                   1)
6440           (setq first (car active)))
6441         (while read
6442           (when first
6443             (push (cons first nlast) unread))
6444           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6445           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6446           (setq read (cdr read)))))
6447     ;; And add the last unread articles.
6448     (cond ((< first last)
6449            (push (cons first last) unread))
6450           ((= first last)
6451            (push first unread)))
6452     ;; Return the sequence of unread articles.
6453     (delq 0 (nreverse unread))))
6454
6455 ;; Various summary commands
6456
6457 (defun gnus-summary-select-article-buffer ()
6458   "Reconfigure windows to show article buffer."
6459   (interactive)
6460   (if (not (gnus-buffer-live-p gnus-article-buffer))
6461       (error "There is no article buffer for this summary buffer")
6462     (gnus-configure-windows 'article)
6463     (select-window (get-buffer-window gnus-article-buffer))))
6464
6465 (defun gnus-summary-universal-argument (arg)
6466   "Perform any operation on all articles that are process/prefixed."
6467   (interactive "P")
6468   (let ((articles (gnus-summary-work-articles arg))
6469         func article)
6470     (if (eq
6471          (setq
6472           func
6473           (key-binding
6474            (read-key-sequence
6475             (substitute-command-keys
6476              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6477          'undefined)
6478         (gnus-error 1 "Undefined key")
6479       (save-excursion
6480         (while articles
6481           (gnus-summary-goto-subject (setq article (pop articles)))
6482           (let (gnus-newsgroup-processable)
6483             (command-execute func))
6484           (gnus-summary-remove-process-mark article)))))
6485   (gnus-summary-position-point))
6486
6487 (defun gnus-summary-toggle-truncation (&optional arg)
6488   "Toggle truncation of summary lines.
6489 With ARG, turn line truncation on if ARG is positive."
6490   (interactive "P")
6491   (setq truncate-lines
6492         (if (null arg) (not truncate-lines)
6493           (> (prefix-numeric-value arg) 0)))
6494   (redraw-display))
6495
6496 (defun gnus-summary-find-for-reselect ()
6497   "Return the number of an article to stay on across a reselect.
6498 The current article is considered, then following articles, then previous
6499 articles.  An article is sought which is not cancelled and isn't a temporary
6500 insertion from another group.  If there's no such then return a dummy 0."
6501   (let (found)
6502     (dolist (rev '(nil t))
6503       (unless found      ; don't demand the reverse list if we don't need it
6504         (let ((data (gnus-data-find-list
6505                      (gnus-summary-article-number) (gnus-data-list rev))))
6506           (while (and data (not found))
6507             (if (and (< 0 (gnus-data-number (car data)))
6508                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6509                 (setq found (gnus-data-number (car data))))
6510             (setq data (cdr data))))))
6511     (or found 0)))
6512
6513 (defun gnus-summary-reselect-current-group (&optional all rescan)
6514   "Exit and then reselect the current newsgroup.
6515 The prefix argument ALL means to select all articles."
6516   (interactive "P")
6517   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6518     (error "Ephemeral groups can't be reselected"))
6519   (let ((current-subject (gnus-summary-find-for-reselect))
6520         (group gnus-newsgroup-name))
6521     (setq gnus-newsgroup-begin nil)
6522     (gnus-summary-exit nil 'leave-hidden)
6523     ;; We have to adjust the point of group mode buffer because
6524     ;; point was moved to the next unread newsgroup by exiting.
6525     (gnus-summary-jump-to-group group)
6526     (when rescan
6527       (save-excursion
6528         (gnus-group-get-new-news-this-group 1)))
6529     (gnus-group-read-group all t)
6530     (gnus-summary-goto-subject current-subject nil t)))
6531
6532 (defun gnus-summary-rescan-group (&optional all)
6533   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6534   (interactive "P")
6535   (gnus-summary-reselect-current-group all t))
6536
6537 (defun gnus-summary-update-info (&optional non-destructive)
6538   (save-excursion
6539     (let ((group gnus-newsgroup-name))
6540       (when group
6541         (when gnus-newsgroup-kill-headers
6542           (setq gnus-newsgroup-killed
6543                 (gnus-compress-sequence
6544                  (gnus-sorted-union
6545                   (gnus-list-range-intersection
6546                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6547                   gnus-newsgroup-unreads)
6548                  t)))
6549         (unless (listp (cdr gnus-newsgroup-killed))
6550           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6551         (let ((headers gnus-newsgroup-headers))
6552           ;; Set the new ranges of read articles.
6553           (with-current-buffer gnus-group-buffer
6554             (gnus-undo-force-boundary))
6555           (gnus-update-read-articles
6556            group (gnus-sorted-union
6557                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6558           ;; Set the current article marks.
6559           (let ((gnus-newsgroup-scored
6560                  (if (and (not gnus-save-score)
6561                           (not non-destructive))
6562                      nil
6563                    gnus-newsgroup-scored)))
6564             (save-excursion
6565               (gnus-update-marks)))
6566           ;; Do the cross-ref thing.
6567           (when gnus-use-cross-reference
6568             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6569           ;; Do not switch windows but change the buffer to work.
6570           (set-buffer gnus-group-buffer)
6571           (unless (gnus-ephemeral-group-p group)
6572             (gnus-group-update-group group)))))))
6573
6574 (defun gnus-summary-save-newsrc (&optional force)
6575   "Save the current number of read/marked articles in the dribble buffer.
6576 The dribble buffer will then be saved.
6577 If FORCE (the prefix), also save the .newsrc file(s)."
6578   (interactive "P")
6579   (gnus-summary-update-info t)
6580   (if force
6581       (gnus-save-newsrc-file)
6582     (gnus-dribble-save)))
6583
6584 (defun gnus-summary-exit (&optional temporary leave-hidden)
6585   "Exit reading current newsgroup, and then return to group selection mode.
6586 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6587   (interactive)
6588   (gnus-set-global-variables)
6589   (when (gnus-buffer-live-p gnus-article-buffer)
6590     (save-excursion
6591       (set-buffer gnus-article-buffer)
6592       (mm-destroy-parts gnus-article-mime-handles)
6593       ;; Set it to nil for safety reason.
6594       (setq gnus-article-mime-handle-alist nil)
6595       (setq gnus-article-mime-handles nil)))
6596   (gnus-kill-save-kill-buffer)
6597   (gnus-async-halt-prefetch)
6598   (let* ((group gnus-newsgroup-name)
6599          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6600          (gnus-group-is-exiting-p t)
6601          (mode major-mode)
6602          (group-point nil)
6603          (buf (current-buffer)))
6604     (unless quit-config
6605       ;; Do adaptive scoring, and possibly save score files.
6606       (when gnus-newsgroup-adaptive
6607         (gnus-score-adaptive))
6608       (when gnus-use-scoring
6609         (gnus-score-save)))
6610     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6611     ;; If we have several article buffers, we kill them at exit.
6612     (unless gnus-single-article-buffer
6613       (gnus-kill-buffer gnus-original-article-buffer)
6614       (setq gnus-article-current nil))
6615     (when gnus-use-cache
6616       (gnus-cache-possibly-remove-articles)
6617       (gnus-cache-save-buffers))
6618     (gnus-async-prefetch-remove-group group)
6619     (when gnus-suppress-duplicates
6620       (gnus-dup-enter-articles))
6621     (when gnus-use-trees
6622       (gnus-tree-close group))
6623     (when gnus-use-cache
6624       (gnus-cache-write-active))
6625     ;; Remove entries for this group.
6626     (nnmail-purge-split-history (gnus-group-real-name group))
6627     ;; Make all changes in this group permanent.
6628     (unless quit-config
6629       (gnus-run-hooks 'gnus-exit-group-hook)
6630       (gnus-summary-update-info))
6631     (gnus-close-group group)
6632     ;; Make sure where we were, and go to next newsgroup.
6633     (set-buffer gnus-group-buffer)
6634     (unless quit-config
6635       (gnus-group-jump-to-group group))
6636     (gnus-run-hooks 'gnus-summary-exit-hook)
6637     (unless (or quit-config
6638                 ;; If this group has disappeared from the summary
6639                 ;; buffer, don't skip forwards.
6640                 (not (string= group (gnus-group-group-name))))
6641       (gnus-group-next-unread-group 1))
6642     (setq group-point (point))
6643     (if temporary
6644         nil                             ;Nothing to do.
6645       ;; If we have several article buffers, we kill them at exit.
6646       (unless gnus-single-article-buffer
6647         (gnus-kill-buffer gnus-article-buffer)
6648         (gnus-kill-buffer gnus-original-article-buffer)
6649         (setq gnus-article-current nil))
6650       (set-buffer buf)
6651       (if (not gnus-kill-summary-on-exit)
6652           (progn
6653             (gnus-deaden-summary)
6654             (setq mode nil))
6655         ;; We set all buffer-local variables to nil.  It is unclear why
6656         ;; this is needed, but if we don't, buffer-local variables are
6657         ;; not garbage-collected, it seems.  This would the lead to en
6658         ;; ever-growing Emacs.
6659         (gnus-summary-clear-local-variables)
6660         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6661           (gnus-summary-clear-local-variables))
6662         (when (get-buffer gnus-article-buffer)
6663           (bury-buffer gnus-article-buffer))
6664         ;; We clear the global counterparts of the buffer-local
6665         ;; variables as well, just to be on the safe side.
6666         (set-buffer gnus-group-buffer)
6667         (gnus-summary-clear-local-variables)
6668         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6669           (gnus-summary-clear-local-variables))
6670         ;; Return to group mode buffer.
6671         (when (eq mode 'gnus-summary-mode)
6672           (gnus-kill-buffer buf)))
6673       (setq gnus-current-select-method gnus-select-method)
6674       (if leave-hidden
6675           (set-buffer gnus-group-buffer)
6676         (pop-to-buffer gnus-group-buffer))
6677       (if (not quit-config)
6678           (progn
6679             (goto-char group-point)
6680             (unless leave-hidden
6681               (gnus-configure-windows 'group 'force)))
6682         (gnus-handle-ephemeral-exit quit-config))
6683       ;; Clear the current group name.
6684       (unless quit-config
6685         (setq gnus-newsgroup-name nil)))))
6686
6687 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6688 (defun gnus-summary-exit-no-update (&optional no-questions)
6689   "Quit reading current newsgroup without updating read article info."
6690   (interactive)
6691   (let* ((group gnus-newsgroup-name)
6692          (gnus-group-is-exiting-p t)
6693          (gnus-group-is-exiting-without-update-p t)
6694          (quit-config (gnus-group-quit-config group)))
6695     (when (or no-questions
6696               gnus-expert-user
6697               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6698       (gnus-async-halt-prefetch)
6699       (run-hooks 'gnus-summary-prepare-exit-hook)
6700       (when (gnus-buffer-live-p gnus-article-buffer)
6701         (save-excursion
6702           (set-buffer gnus-article-buffer)
6703           (mm-destroy-parts gnus-article-mime-handles)
6704           ;; Set it to nil for safety reason.
6705           (setq gnus-article-mime-handle-alist nil)
6706           (setq gnus-article-mime-handles nil)))
6707       ;; If we have several article buffers, we kill them at exit.
6708       (unless gnus-single-article-buffer
6709         (gnus-kill-buffer gnus-article-buffer)
6710         (gnus-kill-buffer gnus-original-article-buffer)
6711         (setq gnus-article-current nil))
6712       (if (not gnus-kill-summary-on-exit)
6713           (gnus-deaden-summary)
6714         (gnus-close-group group)
6715         (gnus-summary-clear-local-variables)
6716         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6717           (gnus-summary-clear-local-variables))
6718         (set-buffer gnus-group-buffer)
6719         (gnus-summary-clear-local-variables)
6720         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6721           (gnus-summary-clear-local-variables))
6722         (gnus-kill-buffer gnus-summary-buffer))
6723       (unless gnus-single-article-buffer
6724         (setq gnus-article-current nil))
6725       (when gnus-use-trees
6726         (gnus-tree-close group))
6727       (gnus-async-prefetch-remove-group group)
6728       (when (get-buffer gnus-article-buffer)
6729         (bury-buffer gnus-article-buffer))
6730       ;; Return to the group buffer.
6731       (gnus-configure-windows 'group 'force)
6732       ;; Clear the current group name.
6733       (setq gnus-newsgroup-name nil)
6734       (unless (gnus-ephemeral-group-p group)
6735         (gnus-group-update-group group))
6736       (when (equal (gnus-group-group-name) group)
6737         (gnus-group-next-unread-group 1))
6738       (when quit-config
6739         (gnus-handle-ephemeral-exit quit-config)))))
6740
6741 (defun gnus-handle-ephemeral-exit (quit-config)
6742   "Handle movement when leaving an ephemeral group.
6743 The state which existed when entering the ephemeral is reset."
6744   (if (not (buffer-name (car quit-config)))
6745       (gnus-configure-windows 'group 'force)
6746     (set-buffer (car quit-config))
6747     (cond ((eq major-mode 'gnus-summary-mode)
6748            (gnus-set-global-variables))
6749           ((eq major-mode 'gnus-article-mode)
6750            (save-excursion
6751              ;; The `gnus-summary-buffer' variable may point
6752              ;; to the old summary buffer when using a single
6753              ;; article buffer.
6754              (unless (gnus-buffer-live-p gnus-summary-buffer)
6755                (set-buffer gnus-group-buffer))
6756              (set-buffer gnus-summary-buffer)
6757              (gnus-set-global-variables))))
6758     (if (or (eq (cdr quit-config) 'article)
6759             (eq (cdr quit-config) 'pick))
6760         (progn
6761           ;; The current article may be from the ephemeral group
6762           ;; thus it is best that we reload this article
6763           ;;
6764           ;; If we're exiting from a large digest, this can be
6765           ;; extremely slow.  So, it's better not to reload it. -- jh.
6766           ;;(gnus-summary-show-article)
6767           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6768               (gnus-configure-windows 'pick 'force)
6769             (gnus-configure-windows (cdr quit-config) 'force)))
6770       (gnus-configure-windows (cdr quit-config) 'force))
6771     (when (eq major-mode 'gnus-summary-mode)
6772       (gnus-summary-next-subject 1 nil t)
6773       (gnus-summary-recenter)
6774       (gnus-summary-position-point))))
6775
6776 ;;; Dead summaries.
6777
6778 (defvar gnus-dead-summary-mode-map nil)
6779
6780 (unless gnus-dead-summary-mode-map
6781   (setq gnus-dead-summary-mode-map (make-keymap))
6782   (suppress-keymap gnus-dead-summary-mode-map)
6783   (substitute-key-definition
6784    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6785   (dolist (key '("\C-d" "\r" "\177" [delete]))
6786     (define-key gnus-dead-summary-mode-map
6787       key 'gnus-summary-wake-up-the-dead))
6788   (dolist (key '("q" "Q"))
6789     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6790
6791 (defvar gnus-dead-summary-mode nil
6792   "Minor mode for Gnus summary buffers.")
6793
6794 (defun gnus-dead-summary-mode (&optional arg)
6795   "Minor mode for Gnus summary buffers."
6796   (interactive "P")
6797   (when (eq major-mode 'gnus-summary-mode)
6798     (make-local-variable 'gnus-dead-summary-mode)
6799     (setq gnus-dead-summary-mode
6800           (if (null arg) (not gnus-dead-summary-mode)
6801             (> (prefix-numeric-value arg) 0)))
6802     (when gnus-dead-summary-mode
6803       (add-minor-mode
6804        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6805
6806 (defun gnus-deaden-summary ()
6807   "Make the current summary buffer into a dead summary buffer."
6808   ;; Kill any previous dead summary buffer.
6809   (when (and gnus-dead-summary
6810              (buffer-name gnus-dead-summary))
6811     (with-current-buffer gnus-dead-summary
6812       (when gnus-dead-summary-mode
6813         (kill-buffer (current-buffer)))))
6814   ;; Make this the current dead summary.
6815   (setq gnus-dead-summary (current-buffer))
6816   (gnus-dead-summary-mode 1)
6817   (let ((name (buffer-name)))
6818     (when (string-match "Summary" name)
6819       (rename-buffer
6820        (concat (substring name 0 (match-beginning 0)) "Dead "
6821                (substring name (match-beginning 0)))
6822        t)
6823       (bury-buffer))))
6824
6825 (defun gnus-kill-or-deaden-summary (buffer)
6826   "Kill or deaden the summary BUFFER."
6827   (save-excursion
6828     (when (and (buffer-name buffer)
6829                (not gnus-single-article-buffer))
6830       (with-current-buffer buffer
6831         (gnus-kill-buffer gnus-article-buffer)
6832         (gnus-kill-buffer gnus-original-article-buffer)))
6833     (cond
6834      ;; Kill the buffer.
6835      (gnus-kill-summary-on-exit
6836       (when (and gnus-use-trees
6837                  (gnus-buffer-exists-p buffer))
6838         (save-excursion
6839           (set-buffer buffer)
6840           (gnus-tree-close gnus-newsgroup-name)))
6841       (gnus-kill-buffer buffer))
6842      ;; Deaden the buffer.
6843      ((gnus-buffer-exists-p buffer)
6844       (save-excursion
6845         (set-buffer buffer)
6846         (gnus-deaden-summary))))))
6847
6848 (defun gnus-summary-wake-up-the-dead (&rest args)
6849   "Wake up the dead summary buffer."
6850   (interactive)
6851   (gnus-dead-summary-mode -1)
6852   (let ((name (buffer-name)))
6853     (when (string-match "Dead " name)
6854       (rename-buffer
6855        (concat (substring name 0 (match-beginning 0))
6856                (substring name (match-end 0)))
6857        t)))
6858   (gnus-message 3 "This dead summary is now alive again"))
6859
6860 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6861 (defun gnus-summary-fetch-faq (&optional faq-dir)
6862   "Fetch the FAQ for the current group.
6863 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6864 in."
6865   (interactive
6866    (list
6867     (when current-prefix-arg
6868       (completing-read
6869        "FAQ dir: " (and (listp gnus-group-faq-directory)
6870                         (mapcar 'list
6871                                 gnus-group-faq-directory))))))
6872   (let (gnus-faq-buffer)
6873     (when (setq gnus-faq-buffer
6874                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6875       (gnus-configure-windows 'summary-faq))))
6876
6877 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6878 (defun gnus-summary-describe-group (&optional force)
6879   "Describe the current newsgroup."
6880   (interactive "P")
6881   (gnus-group-describe-group force gnus-newsgroup-name))
6882
6883 (defun gnus-summary-describe-briefly ()
6884   "Describe summary mode commands briefly."
6885   (interactive)
6886   (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")))
6887
6888 ;; Walking around group mode buffer from summary mode.
6889
6890 (defun gnus-summary-next-group (&optional no-article target-group backward)
6891   "Exit current newsgroup and then select next unread newsgroup.
6892 If prefix argument NO-ARTICLE is non-nil, no article is selected
6893 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6894 previous group instead."
6895   (interactive "P")
6896   ;; Stop pre-fetching.
6897   (gnus-async-halt-prefetch)
6898   (let ((current-group gnus-newsgroup-name)
6899         (current-buffer (current-buffer))
6900         entered)
6901     ;; First we semi-exit this group to update Xrefs and all variables.
6902     ;; We can't do a real exit, because the window conf must remain
6903     ;; the same in case the user is prompted for info, and we don't
6904     ;; want the window conf to change before that...
6905     (gnus-summary-exit t)
6906     (while (not entered)
6907       ;; Then we find what group we are supposed to enter.
6908       (set-buffer gnus-group-buffer)
6909       (gnus-group-jump-to-group current-group)
6910       (setq target-group
6911             (or target-group
6912                 (if (eq gnus-keep-same-level 'best)
6913                     (gnus-summary-best-group gnus-newsgroup-name)
6914                   (gnus-summary-search-group backward gnus-keep-same-level))))
6915       (if (not target-group)
6916           ;; There are no further groups, so we return to the group
6917           ;; buffer.
6918           (progn
6919             (gnus-message 5 "Returning to the group buffer")
6920             (setq entered t)
6921             (when (gnus-buffer-live-p current-buffer)
6922               (set-buffer current-buffer)
6923               (gnus-summary-exit))
6924             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6925         ;; We try to enter the target group.
6926         (gnus-group-jump-to-group target-group)
6927         (let ((unreads (gnus-group-group-unread)))
6928           (if (and (or (eq t unreads)
6929                        (and unreads (not (zerop unreads))))
6930                    (gnus-summary-read-group
6931                     target-group nil no-article
6932                     (and (buffer-name current-buffer) current-buffer)
6933                     nil backward))
6934               (setq entered t)
6935             (setq current-group target-group
6936                   target-group nil)))))))
6937
6938 (defun gnus-summary-prev-group (&optional no-article)
6939   "Exit current newsgroup and then select previous unread newsgroup.
6940 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6941   (interactive "P")
6942   (gnus-summary-next-group no-article nil t))
6943
6944 ;; Walking around summary lines.
6945
6946 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6947   "Go to the first subject satisfying any non-nil constraint.
6948 If UNREAD is non-nil, the article should be unread.
6949 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6950 If UNSEEN is non-nil, the article should be unseen.
6951 Returns the article selected or nil if there are no matching articles."
6952   (interactive "P")
6953   (cond
6954    ;; Empty summary.
6955    ((null gnus-newsgroup-data)
6956     (gnus-message 3 "No articles in the group")
6957     nil)
6958    ;; Pick the first article.
6959    ((not (or unread undownloaded unseen))
6960     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6961     (gnus-data-number (car gnus-newsgroup-data)))
6962    ;; Find the first unread article.
6963    (t
6964     (let ((data gnus-newsgroup-data))
6965       (while (and data
6966                   (let ((num (gnus-data-number (car data))))
6967                     (or (memq num gnus-newsgroup-unfetched)
6968                         (not (or (and unread
6969                                       (memq num gnus-newsgroup-unreads))
6970                                  (and undownloaded
6971                                       (memq num gnus-newsgroup-undownloaded))
6972                                  (and unseen
6973                                       (memq num gnus-newsgroup-unseen)))))))
6974         (setq data (cdr data)))
6975       (prog1
6976           (if data
6977               (progn
6978                 (goto-char (gnus-data-pos (car data)))
6979                 (gnus-data-number (car data)))
6980             (gnus-message 3 "No more%s articles"
6981                           (let* ((r (when unread " unread"))
6982                                  (d (when undownloaded " undownloaded"))
6983                                  (s (when unseen " unseen"))
6984                                  (l (delq nil (list r d s))))
6985                             (cond ((= 3 (length l))
6986                                    (concat r "," d ", or" s))
6987                                   ((= 2 (length l))
6988                                    (concat (car l) ", or" (cadr l)))
6989                                   ((= 1 (length l))
6990                                    (car l))
6991                                   (t
6992                                    ""))))
6993             nil
6994             )
6995         (gnus-summary-position-point))))))
6996
6997 (defun gnus-summary-next-subject (n &optional unread dont-display)
6998   "Go to next N'th summary line.
6999 If N is negative, go to the previous N'th subject line.
7000 If UNREAD is non-nil, only unread articles are selected.
7001 The difference between N and the actual number of steps taken is
7002 returned."
7003   (interactive "p")
7004   (let ((backward (< n 0))
7005         (n (abs n)))
7006     (while (and (> n 0)
7007                 (if backward
7008                     (gnus-summary-find-prev unread)
7009                   (gnus-summary-find-next unread)))
7010       (unless (zerop (setq n (1- n)))
7011         (gnus-summary-show-thread)))
7012     (when (/= 0 n)
7013       (gnus-message 7 "No more%s articles"
7014                     (if unread " unread" "")))
7015     (unless dont-display
7016       (gnus-summary-recenter)
7017       (gnus-summary-position-point))
7018     n))
7019
7020 (defun gnus-summary-next-unread-subject (n)
7021   "Go to next N'th unread summary line."
7022   (interactive "p")
7023   (gnus-summary-next-subject n t))
7024
7025 (defun gnus-summary-prev-subject (n &optional unread)
7026   "Go to previous N'th summary line.
7027 If optional argument UNREAD is non-nil, only unread article is selected."
7028   (interactive "p")
7029   (gnus-summary-next-subject (- n) unread))
7030
7031 (defun gnus-summary-prev-unread-subject (n)
7032   "Go to previous N'th unread summary line."
7033   (interactive "p")
7034   (gnus-summary-next-subject (- n) t))
7035
7036 (defun gnus-summary-goto-subjects (articles)
7037   "Insert the subject header for ARTICLES in the current buffer."
7038   (save-excursion
7039     (dolist (article articles)
7040       (gnus-summary-goto-subject article t)))
7041   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7042   (gnus-summary-position-point))
7043  
7044 (defun gnus-summary-goto-subject (article &optional force silent)
7045   "Go the subject line of ARTICLE.
7046 If FORCE, also allow jumping to articles not currently shown."
7047   (interactive "nArticle number: ")
7048   (unless (numberp article)
7049     (error "Article %s is not a number" article))
7050   (let ((b (point))
7051         (data (gnus-data-find article)))
7052     ;; We read in the article if we have to.
7053     (and (not data)
7054          force
7055          (gnus-summary-insert-subject
7056           article
7057           (if (or (numberp force) (vectorp force)) force)
7058           t)
7059          (setq data (gnus-data-find article)))
7060     (goto-char b)
7061     (if (not data)
7062         (progn
7063           (unless silent
7064             (gnus-message 3 "Can't find article %d" article))
7065           nil)
7066       (let ((pt (gnus-data-pos data)))
7067         (goto-char pt)
7068         (gnus-summary-set-article-display-arrow pt))
7069       (gnus-summary-position-point)
7070       article)))
7071
7072 ;; Walking around summary lines with displaying articles.
7073
7074 (defun gnus-summary-expand-window (&optional arg)
7075   "Make the summary buffer take up the entire Emacs frame.
7076 Given a prefix, will force an `article' buffer configuration."
7077   (interactive "P")
7078   (if arg
7079       (gnus-configure-windows 'article 'force)
7080     (gnus-configure-windows 'summary 'force)))
7081
7082 (defun gnus-summary-display-article (article &optional all-header)
7083   "Display ARTICLE in article buffer."
7084   (when (gnus-buffer-live-p gnus-article-buffer)
7085     (with-current-buffer gnus-article-buffer
7086       (mm-enable-multibyte)))
7087   (gnus-set-global-variables)
7088   (when (gnus-buffer-live-p gnus-article-buffer)
7089     (with-current-buffer gnus-article-buffer
7090       (setq gnus-article-charset gnus-newsgroup-charset)
7091       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7092       (mm-enable-multibyte)))
7093   (if (null article)
7094       nil
7095     (prog1
7096         (if gnus-summary-display-article-function
7097             (funcall gnus-summary-display-article-function article all-header)
7098           (gnus-article-prepare article all-header))
7099       (gnus-run-hooks 'gnus-select-article-hook)
7100       (when (and gnus-current-article
7101                  (not (zerop gnus-current-article)))
7102         (gnus-summary-goto-subject gnus-current-article))
7103       (gnus-summary-recenter)
7104       (when (and gnus-use-trees gnus-show-threads)
7105         (gnus-possibly-generate-tree article)
7106         (gnus-highlight-selected-tree article))
7107       ;; Successfully display article.
7108       (gnus-article-set-window-start
7109        (cdr (assq article gnus-newsgroup-bookmarks))))))
7110
7111 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7112   "Select the current article.
7113 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7114 non-nil, the article will be re-fetched even if it already present in
7115 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7116 be displayed."
7117   ;; Make sure we are in the summary buffer to work around bbdb bug.
7118   (unless (eq major-mode 'gnus-summary-mode)
7119     (set-buffer gnus-summary-buffer))
7120   (let ((article (or article (gnus-summary-article-number)))
7121         (all-headers (not (not all-headers))) ;Must be t or nil.
7122         gnus-summary-display-article-function)
7123     (and (not pseudo)
7124          (gnus-summary-article-pseudo-p article)
7125          (error "This is a pseudo-article"))
7126     (save-excursion
7127       (set-buffer gnus-summary-buffer)
7128       (if (or (and gnus-single-article-buffer
7129                    (or (null gnus-current-article)
7130                        (null gnus-article-current)
7131                        (null (get-buffer gnus-article-buffer))
7132                        (not (eq article (cdr gnus-article-current)))
7133                        (not (equal (car gnus-article-current)
7134                                    gnus-newsgroup-name))))
7135               (and (not gnus-single-article-buffer)
7136                    (or (null gnus-current-article)
7137                        (not (eq gnus-current-article article))))
7138               force)
7139           ;; The requested article is different from the current article.
7140           (progn
7141             (gnus-summary-display-article article all-headers)
7142             (when (gnus-buffer-live-p gnus-article-buffer)
7143               (with-current-buffer gnus-article-buffer
7144                 (if (not gnus-article-decoded-p) ;; a local variable
7145                     (mm-disable-multibyte))))
7146             (gnus-article-set-window-start
7147              (cdr (assq article gnus-newsgroup-bookmarks)))
7148             article)
7149         'old))))
7150
7151 (defun gnus-summary-force-verify-and-decrypt ()
7152   "Display buttons for signed/encrypted parts and verify/decrypt them."
7153   (interactive)
7154   (let ((mm-verify-option 'known)
7155         (mm-decrypt-option 'known)
7156         (gnus-article-emulate-mime t)
7157         (mm-fill-flowed nil)
7158         (gnus-buttonized-mime-types (append (list "multipart/signed"
7159                                                   "multipart/encrypted")
7160                                             gnus-buttonized-mime-types)))
7161     (gnus-summary-select-article nil 'force)))
7162
7163 (defun gnus-summary-set-current-mark (&optional current-mark)
7164   "Obsolete function."
7165   nil)
7166
7167 (defun gnus-summary-next-article (&optional unread subject backward push)
7168   "Select the next article.
7169 If UNREAD, only unread articles are selected.
7170 If SUBJECT, only articles with SUBJECT are selected.
7171 If BACKWARD, the previous article is selected instead of the next."
7172   (interactive "P")
7173   (cond
7174    ;; Is there such an article?
7175    ((and (gnus-summary-search-forward unread subject backward)
7176          (or (gnus-summary-display-article (gnus-summary-article-number))
7177              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7178     (gnus-summary-position-point))
7179    ;; If not, we try the first unread, if that is wanted.
7180    ((and subject
7181          gnus-auto-select-same
7182          (gnus-summary-first-unread-article))
7183     (gnus-summary-position-point)
7184     (gnus-message 6 "Wrapped"))
7185    ;; Try to get next/previous article not displayed in this group.
7186    ((and gnus-auto-extend-newsgroup
7187          (not unread) (not subject))
7188     (gnus-summary-goto-article
7189      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7190      nil (count-lines (point-min) (point))))
7191    ;; Go to next/previous group.
7192    (t
7193     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7194       (gnus-summary-jump-to-group gnus-newsgroup-name))
7195     (let ((cmd last-command-char)
7196           (point
7197            (with-current-buffer gnus-group-buffer
7198              (point)))
7199           (group
7200            (if (eq gnus-keep-same-level 'best)
7201                (gnus-summary-best-group gnus-newsgroup-name)
7202              (gnus-summary-search-group backward gnus-keep-same-level))))
7203       ;; For some reason, the group window gets selected.  We change
7204       ;; it back.
7205       (select-window (get-buffer-window (current-buffer)))
7206       ;; Select next unread newsgroup automagically.
7207       (cond
7208        ((or (not gnus-auto-select-next)
7209             (not cmd))
7210         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7211        ((or (eq gnus-auto-select-next 'quietly)
7212             (and (eq gnus-auto-select-next 'slightly-quietly)
7213                  push)
7214             (and (eq gnus-auto-select-next 'almost-quietly)
7215                  (gnus-summary-last-article-p)))
7216         ;; Select quietly.
7217         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7218             (gnus-summary-exit)
7219           (gnus-message 7 "No more%s articles (%s)..."
7220                         (if unread " unread" "")
7221                         (if group (concat "selecting " group)
7222                           "exiting"))
7223           (gnus-summary-next-group nil group backward)))
7224        (t
7225         (when (gnus-key-press-event-p last-input-event)
7226           (gnus-summary-walk-group-buffer
7227            gnus-newsgroup-name cmd unread backward point))))))))
7228
7229 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7230   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7231                       (?\C-p (gnus-group-prev-unread-group 1))))
7232         (cursor-in-echo-area t)
7233         keve key group ended prompt)
7234     (save-excursion
7235       (set-buffer gnus-group-buffer)
7236       (goto-char start)
7237       (setq group
7238             (if (eq gnus-keep-same-level 'best)
7239                 (gnus-summary-best-group gnus-newsgroup-name)
7240               (gnus-summary-search-group backward gnus-keep-same-level))))
7241     (while (not ended)
7242       (setq prompt
7243             (format
7244              "No more%s articles%s " (if unread " unread" "")
7245              (if (and group
7246                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7247                  (format " (Type %s for %s [%s])"
7248                          (single-key-description cmd) group
7249                          (gnus-group-unread group))
7250                (format " (Type %s to exit %s)"
7251                        (single-key-description cmd)
7252                        gnus-newsgroup-name))))
7253       ;; Confirm auto selection.
7254       (setq key (car (setq keve (gnus-read-event-char prompt)))
7255             ended t)
7256       (cond
7257        ((assq key keystrokes)
7258         (let ((obuf (current-buffer)))
7259           (switch-to-buffer gnus-group-buffer)
7260           (when group
7261             (gnus-group-jump-to-group group))
7262           (eval (cadr (assq key keystrokes)))
7263           (setq group (gnus-group-group-name))
7264           (switch-to-buffer obuf))
7265         (setq ended nil))
7266        ((equal key cmd)
7267         (if (or (not group)
7268                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7269             (gnus-summary-exit)
7270           (gnus-summary-next-group nil group backward)))
7271        (t
7272         (push (cdr keve) unread-command-events))))))
7273
7274 (defun gnus-summary-next-unread-article ()
7275   "Select unread article after current one."
7276   (interactive)
7277   (gnus-summary-next-article
7278    (or (not (eq gnus-summary-goto-unread 'never))
7279        (gnus-summary-last-article-p (gnus-summary-article-number)))
7280    (and gnus-auto-select-same
7281         (gnus-summary-article-subject))))
7282
7283 (defun gnus-summary-prev-article (&optional unread subject)
7284   "Select the article after the current one.
7285 If UNREAD is non-nil, only unread articles are selected."
7286   (interactive "P")
7287   (gnus-summary-next-article unread subject t))
7288
7289 (defun gnus-summary-prev-unread-article ()
7290   "Select unread article before current one."
7291   (interactive)
7292   (gnus-summary-prev-article
7293    (or (not (eq gnus-summary-goto-unread 'never))
7294        (gnus-summary-first-article-p (gnus-summary-article-number)))
7295    (and gnus-auto-select-same
7296         (gnus-summary-article-subject))))
7297
7298 (defun gnus-summary-next-page (&optional lines circular stop)
7299   "Show next page of the selected article.
7300 If at the end of the current article, select the next article.
7301 LINES says how many lines should be scrolled up.
7302
7303 If CIRCULAR is non-nil, go to the start of the article instead of
7304 selecting the next article when reaching the end of the current
7305 article.
7306
7307 If STOP is non-nil, just stop when reaching the end of the message.
7308
7309 Also see the variable `gnus-article-skip-boring'."
7310   (interactive "P")
7311   (setq gnus-summary-buffer (current-buffer))
7312   (gnus-set-global-variables)
7313   (let ((article (gnus-summary-article-number))
7314         (article-window (get-buffer-window gnus-article-buffer t))
7315         endp)
7316     ;; If the buffer is empty, we have no article.
7317     (unless article
7318       (error "No article to select"))
7319     (gnus-configure-windows 'article)
7320     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7321         (if (and (eq gnus-summary-goto-unread 'never)
7322                  (not (gnus-summary-last-article-p article)))
7323             (gnus-summary-next-article)
7324           (gnus-summary-next-unread-article))
7325       (if (or (null gnus-current-article)
7326               (null gnus-article-current)
7327               (/= article (cdr gnus-article-current))
7328               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7329           ;; Selected subject is different from current article's.
7330           (gnus-summary-display-article article)
7331         (when article-window
7332           (gnus-eval-in-buffer-window gnus-article-buffer
7333             (setq endp (or (gnus-article-next-page lines)
7334                            (gnus-article-only-boring-p))))
7335           (when endp
7336             (cond (stop
7337                    (gnus-message 3 "End of message"))
7338                   (circular
7339                    (gnus-summary-beginning-of-article))
7340                   (lines
7341                    (gnus-message 3 "End of message"))
7342                   ((null lines)
7343                    (if (and (eq gnus-summary-goto-unread 'never)
7344                             (not (gnus-summary-last-article-p article)))
7345                        (gnus-summary-next-article)
7346                      (gnus-summary-next-unread-article))))))))
7347     (gnus-summary-recenter)
7348     (gnus-summary-position-point)))
7349
7350 (defun gnus-summary-prev-page (&optional lines move)
7351   "Show previous page of selected article.
7352 Argument LINES specifies lines to be scrolled down.
7353 If MOVE, move to the previous unread article if point is at
7354 the beginning of the buffer."
7355   (interactive "P")
7356   (let ((article (gnus-summary-article-number))
7357         (article-window (get-buffer-window gnus-article-buffer t))
7358         endp)
7359     (gnus-configure-windows 'article)
7360     (if (or (null gnus-current-article)
7361             (null gnus-article-current)
7362             (/= article (cdr gnus-article-current))
7363             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7364         ;; Selected subject is different from current article's.
7365         (gnus-summary-display-article article)
7366       (gnus-summary-recenter)
7367       (when article-window
7368         (gnus-eval-in-buffer-window gnus-article-buffer
7369           (setq endp (gnus-article-prev-page lines)))
7370         (when (and move endp)
7371           (cond (lines
7372                  (gnus-message 3 "Beginning of message"))
7373                 ((null lines)
7374                  (if (and (eq gnus-summary-goto-unread 'never)
7375                           (not (gnus-summary-first-article-p article)))
7376                      (gnus-summary-prev-article)
7377                    (gnus-summary-prev-unread-article))))))))
7378   (gnus-summary-position-point))
7379
7380 (defun gnus-summary-prev-page-or-article (&optional lines)
7381   "Show previous page of selected article.
7382 Argument LINES specifies lines to be scrolled down.
7383 If at the beginning of the article, go to the next article."
7384   (interactive "P")
7385   (gnus-summary-prev-page lines t))
7386
7387 (defun gnus-summary-scroll-up (lines)
7388   "Scroll up (or down) one line current article.
7389 Argument LINES specifies lines to be scrolled up (or down if negative)."
7390   (interactive "p")
7391   (gnus-configure-windows 'article)
7392   (gnus-summary-show-thread)
7393   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7394     (gnus-eval-in-buffer-window gnus-article-buffer
7395       (cond ((> lines 0)
7396              (when (gnus-article-next-page lines)
7397                (gnus-message 3 "End of message")))
7398             ((< lines 0)
7399              (gnus-article-prev-page (- lines))))))
7400   (gnus-summary-recenter)
7401   (gnus-summary-position-point))
7402
7403 (defun gnus-summary-scroll-down (lines)
7404   "Scroll down (or up) one line current article.
7405 Argument LINES specifies lines to be scrolled down (or up if negative)."
7406   (interactive "p")
7407   (gnus-summary-scroll-up (- lines)))
7408
7409 (defun gnus-summary-next-same-subject ()
7410   "Select next article which has the same subject as current one."
7411   (interactive)
7412   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7413
7414 (defun gnus-summary-prev-same-subject ()
7415   "Select previous article which has the same subject as current one."
7416   (interactive)
7417   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7418
7419 (defun gnus-summary-next-unread-same-subject ()
7420   "Select next unread article which has the same subject as current one."
7421   (interactive)
7422   (gnus-summary-next-article t (gnus-summary-article-subject)))
7423
7424 (defun gnus-summary-prev-unread-same-subject ()
7425   "Select previous unread article which has the same subject as current one."
7426   (interactive)
7427   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7428
7429 (defun gnus-summary-first-unread-article ()
7430   "Select the first unread article.
7431 Return nil if there are no unread articles."
7432   (interactive)
7433   (prog1
7434       (when (gnus-summary-first-subject t)
7435         (gnus-summary-show-thread)
7436         (gnus-summary-first-subject t)
7437         (gnus-summary-display-article (gnus-summary-article-number)))
7438     (gnus-summary-position-point)))
7439
7440 (defun gnus-summary-first-unread-subject ()
7441   "Place the point on the subject line of the first unread article.
7442 Return nil if there are no unread articles."
7443   (interactive)
7444   (prog1
7445       (when (gnus-summary-first-subject t)
7446         (gnus-summary-show-thread)
7447         (gnus-summary-first-subject t))
7448     (gnus-summary-position-point)))
7449
7450 (defun gnus-summary-first-unseen-subject ()
7451   "Place the point on the subject line of the first unseen article.
7452 Return nil if there are no unseen articles."
7453   (interactive)
7454   (prog1
7455       (when (gnus-summary-first-subject nil nil t)
7456         (gnus-summary-show-thread)
7457         (gnus-summary-first-subject nil nil t))
7458     (gnus-summary-position-point)))
7459
7460 (defun gnus-summary-first-unseen-or-unread-subject ()
7461   "Place the point on the subject line of the first unseen article or,
7462 if all article have been seen, on the subject line of the first unread
7463 article."
7464   (interactive)
7465   (prog1
7466       (unless (when (gnus-summary-first-subject nil nil t)
7467                 (gnus-summary-show-thread)
7468                 (gnus-summary-first-subject nil nil t))
7469         (when (gnus-summary-first-subject t)
7470           (gnus-summary-show-thread)
7471           (gnus-summary-first-subject t)))
7472     (gnus-summary-position-point)))
7473
7474 (defun gnus-summary-first-article ()
7475   "Select the first article.
7476 Return nil if there are no articles."
7477   (interactive)
7478   (prog1
7479       (when (gnus-summary-first-subject)
7480         (gnus-summary-show-thread)
7481         (gnus-summary-first-subject)
7482         (gnus-summary-display-article (gnus-summary-article-number)))
7483     (gnus-summary-position-point)))
7484
7485 (defun gnus-summary-best-unread-article (&optional arg)
7486   "Select the unread article with the highest score.
7487 If given a prefix argument, select the next unread article that has a
7488 score higher than the default score."
7489   (interactive "P")
7490   (let ((article (if arg
7491                      (gnus-summary-better-unread-subject)
7492                    (gnus-summary-best-unread-subject))))
7493     (if article
7494         (gnus-summary-goto-article article)
7495       (error "No unread articles"))))
7496
7497 (defun gnus-summary-best-unread-subject ()
7498   "Select the unread subject with the highest score."
7499   (interactive)
7500   (let ((best -1000000)
7501         (data gnus-newsgroup-data)
7502         article score)
7503     (while data
7504       (and (gnus-data-unread-p (car data))
7505            (> (setq score
7506                     (gnus-summary-article-score (gnus-data-number (car data))))
7507               best)
7508            (setq best score
7509                  article (gnus-data-number (car data))))
7510       (setq data (cdr data)))
7511     (when article
7512       (gnus-summary-goto-subject article))
7513     (gnus-summary-position-point)
7514     article))
7515
7516 (defun gnus-summary-better-unread-subject ()
7517   "Select the first unread subject that has a score over the default score."
7518   (interactive)
7519   (let ((data gnus-newsgroup-data)
7520         article score)
7521     (while (and (setq article (gnus-data-number (car data)))
7522                 (or (gnus-data-read-p (car data))
7523                     (not (> (gnus-summary-article-score article)
7524                             gnus-summary-default-score))))
7525       (setq data (cdr data)))
7526     (when article
7527       (gnus-summary-goto-subject article))
7528     (gnus-summary-position-point)
7529     article))
7530
7531 (defun gnus-summary-last-subject ()
7532   "Go to the last displayed subject line in the group."
7533   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7534     (when article
7535       (gnus-summary-goto-subject article))))
7536
7537 (defun gnus-summary-goto-article (article &optional all-headers force)
7538   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7539 If ALL-HEADERS is non-nil, no header lines are hidden.
7540 If FORCE, go to the article even if it isn't displayed.  If FORCE
7541 is a number, it is the line the article is to be displayed on."
7542   (interactive
7543    (list
7544     (completing-read
7545      "Article number or Message-ID: "
7546      (mapcar (lambda (number) (list (int-to-string number)))
7547              gnus-newsgroup-limit))
7548     current-prefix-arg
7549     t))
7550   (prog1
7551       (if (and (stringp article)
7552                (string-match "@\\|%40" article))
7553           (gnus-summary-refer-article article)
7554         (when (stringp article)
7555           (setq article (string-to-number article)))
7556         (if (gnus-summary-goto-subject article force)
7557             (gnus-summary-display-article article all-headers)
7558           (gnus-message 4 "Couldn't go to article %s" article) nil))
7559     (gnus-summary-position-point)))
7560
7561 (defun gnus-summary-goto-last-article ()
7562   "Go to the previously read article."
7563   (interactive)
7564   (prog1
7565       (when gnus-last-article
7566         (gnus-summary-goto-article gnus-last-article nil t))
7567     (gnus-summary-position-point)))
7568
7569 (defun gnus-summary-pop-article (number)
7570   "Pop one article off the history and go to the previous.
7571 NUMBER articles will be popped off."
7572   (interactive "p")
7573   (let (to)
7574     (setq gnus-newsgroup-history
7575           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7576     (if to
7577         (gnus-summary-goto-article (car to) nil t)
7578       (error "Article history empty")))
7579   (gnus-summary-position-point))
7580
7581 ;; Summary commands and functions for limiting the summary buffer.
7582
7583 (defun gnus-summary-limit-to-articles (n)
7584   "Limit the summary buffer to the next N articles.
7585 If not given a prefix, use the process marked articles instead."
7586   (interactive "P")
7587   (prog1
7588       (let ((articles (gnus-summary-work-articles n)))
7589         (setq gnus-newsgroup-processable nil)
7590         (gnus-summary-limit articles))
7591     (gnus-summary-position-point)))
7592
7593 (defun gnus-summary-pop-limit (&optional total)
7594   "Restore the previous limit.
7595 If given a prefix, remove all limits."
7596   (interactive "P")
7597   (when total
7598     (setq gnus-newsgroup-limits
7599           (list (mapcar (lambda (h) (mail-header-number h))
7600                         gnus-newsgroup-headers))))
7601   (unless gnus-newsgroup-limits
7602     (error "No limit to pop"))
7603   (prog1
7604       (gnus-summary-limit nil 'pop)
7605     (gnus-summary-position-point)))
7606
7607 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7608   "Limit the summary buffer to articles that have subjects that match a regexp.
7609 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7610   (interactive
7611    (list (read-string (if current-prefix-arg
7612                           "Exclude subject (regexp): "
7613                         "Limit to subject (regexp): "))
7614          nil current-prefix-arg))
7615   (unless header
7616     (setq header "subject"))
7617   (when (not (equal "" subject))
7618     (prog1
7619         (let ((articles (gnus-summary-find-matching
7620                          (or header "subject") subject 'all nil nil
7621                          not-matching)))
7622           (unless articles
7623             (error "Found no matches for \"%s\"" subject))
7624           (gnus-summary-limit articles))
7625       (gnus-summary-position-point))))
7626
7627 (defun gnus-summary-limit-to-author (from &optional not-matching)
7628   "Limit the summary buffer to articles that have authors that match a regexp.
7629 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7630   (interactive
7631    (list (read-string (if current-prefix-arg
7632                           "Exclude author (regexp): "
7633                         "Limit to author (regexp): "))
7634          current-prefix-arg))
7635   (gnus-summary-limit-to-subject from "from" not-matching))
7636
7637 (defun gnus-summary-limit-to-age (age &optional younger-p)
7638   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7639 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7640 articles that are younger than AGE days."
7641   (interactive
7642    (let ((younger current-prefix-arg)
7643          (days-got nil)
7644          days)
7645      (while (not days-got)
7646        (setq days (if younger
7647                       (read-string "Limit to articles younger than (in days, older when negative): ")
7648                     (read-string
7649                      "Limit to articles older than (in days, younger when negative): ")))
7650        (when (> (length days) 0)
7651          (setq days (read days)))
7652        (if (numberp days)
7653            (progn
7654              (setq days-got t)
7655              (when (< days 0)
7656                (setq younger (not younger))
7657                (setq days (* days -1))))
7658          (message "Please enter a number.")
7659          (sleep-for 1)))
7660      (list days younger)))
7661   (prog1
7662       (let ((data gnus-newsgroup-data)
7663             (cutoff (days-to-time age))
7664             articles d date is-younger)
7665         (while (setq d (pop data))
7666           (when (and (vectorp (gnus-data-header d))
7667                      (setq date (mail-header-date (gnus-data-header d))))
7668             (setq is-younger (time-less-p
7669                               (time-since (condition-case ()
7670                                               (date-to-time date)
7671                                             (error '(0 0))))
7672                               cutoff))
7673             (when (if younger-p
7674                       is-younger
7675                     (not is-younger))
7676               (push (gnus-data-number d) articles))))
7677         (gnus-summary-limit (nreverse articles)))
7678     (gnus-summary-position-point)))
7679
7680 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7681   "Limit the summary buffer to articles that match an 'extra' header."
7682   (interactive
7683    (let ((header
7684           (intern
7685            (gnus-completing-read-with-default
7686             (symbol-name (car gnus-extra-headers))
7687             (if current-prefix-arg
7688                 "Exclude extra header:"
7689               "Limit extra header:")
7690             (mapcar (lambda (x)
7691                       (cons (symbol-name x) x))
7692                     gnus-extra-headers)
7693             nil
7694             t))))
7695      (list header
7696            (read-string (format "%s header %s (regexp): "
7697                                 (if current-prefix-arg "Exclude" "Limit to")
7698                                 header))
7699            current-prefix-arg)))
7700   (when (not (equal "" regexp))
7701     (prog1
7702         (let ((articles (gnus-summary-find-matching
7703                          (cons 'extra header) regexp 'all nil nil
7704                          not-matching)))
7705           (unless articles
7706             (error "Found no matches for \"%s\"" regexp))
7707           (gnus-summary-limit articles))
7708       (gnus-summary-position-point))))
7709
7710 (defun gnus-summary-limit-to-display-predicate ()
7711   "Limit the summary buffer to the predicated in the `display' group parameter."
7712   (interactive)
7713   (unless gnus-newsgroup-display
7714     (error "There is no `display' group parameter"))
7715   (let (articles)
7716     (dolist (number gnus-newsgroup-articles)
7717       (when (funcall gnus-newsgroup-display)
7718         (push number articles)))
7719     (gnus-summary-limit articles))
7720   (gnus-summary-position-point))
7721
7722 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7723 (make-obsolete
7724  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7725
7726 (defun gnus-summary-limit-to-unread (&optional all)
7727   "Limit the summary buffer to articles that are not marked as read.
7728 If ALL is non-nil, limit strictly to unread articles."
7729   (interactive "P")
7730   (if all
7731       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7732     (gnus-summary-limit-to-marks
7733      ;; Concat all the marks that say that an article is read and have
7734      ;; those removed.
7735      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7736            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7737            gnus-low-score-mark gnus-expirable-mark
7738            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7739            gnus-duplicate-mark gnus-souped-mark)
7740      'reverse)))
7741
7742 (defun gnus-summary-limit-to-replied (&optional unreplied)
7743   "Limit the summary buffer to replied articles.
7744 If UNREPLIED (the prefix), limit to unreplied articles."
7745   (interactive "P")
7746   (if unreplied
7747       (gnus-summary-limit
7748        (gnus-set-difference gnus-newsgroup-articles
7749         gnus-newsgroup-replied))
7750     (gnus-summary-limit gnus-newsgroup-replied))
7751   (gnus-summary-position-point))
7752
7753 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7754 (make-obsolete 'gnus-summary-delete-marked-with
7755                'gnus-summary-limit-exclude-marks)
7756
7757 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7758   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7759 If REVERSE, limit the summary buffer to articles that are marked
7760 with MARKS.  MARKS can either be a string of marks or a list of marks.
7761 Returns how many articles were removed."
7762   (interactive "sMarks: ")
7763   (gnus-summary-limit-to-marks marks t))
7764
7765 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7766   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7767 If REVERSE (the prefix), limit the summary buffer to articles that are
7768 not marked with MARKS.  MARKS can either be a string of marks or a
7769 list of marks.
7770 Returns how many articles were removed."
7771   (interactive "sMarks: \nP")
7772   (prog1
7773       (let ((data gnus-newsgroup-data)
7774             (marks (if (listp marks) marks
7775                      (append marks nil))) ; Transform to list.
7776             articles)
7777         (while data
7778           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7779                   (memq (gnus-data-mark (car data)) marks))
7780             (push (gnus-data-number (car data)) articles))
7781           (setq data (cdr data)))
7782         (gnus-summary-limit articles))
7783     (gnus-summary-position-point)))
7784
7785 (defun gnus-summary-limit-to-score (score)
7786   "Limit to articles with score at or above SCORE."
7787   (interactive "NLimit to articles with score of at least: ")
7788   (let ((data gnus-newsgroup-data)
7789         articles)
7790     (while data
7791       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7792                 score)
7793         (push (gnus-data-number (car data)) articles))
7794       (setq data (cdr data)))
7795     (prog1
7796         (gnus-summary-limit articles)
7797       (gnus-summary-position-point))))
7798
7799 (defun gnus-summary-limit-to-unseen ()
7800   "Limit to unseen articles."
7801   (interactive)
7802   (prog1
7803       (gnus-summary-limit gnus-newsgroup-unseen)
7804     (gnus-summary-position-point)))
7805
7806 (defun gnus-summary-limit-include-thread (id)
7807   "Display all the hidden articles that is in the thread with ID in it.
7808 When called interactively, ID is the Message-ID of the current
7809 article."
7810   (interactive (list (mail-header-id (gnus-summary-article-header))))
7811   (let ((articles (gnus-articles-in-thread
7812                    (gnus-id-to-thread (gnus-root-id id)))))
7813     (prog1
7814         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7815       (gnus-summary-limit-include-matching-articles
7816        "subject"
7817        (regexp-quote (gnus-simplify-subject-re
7818                       (mail-header-subject (gnus-id-to-header id)))))
7819       (gnus-summary-position-point))))
7820
7821 (defun gnus-summary-limit-include-matching-articles (header regexp)
7822   "Display all the hidden articles that have HEADERs that match REGEXP."
7823   (interactive (list (read-string "Match on header: ")
7824                      (read-string "Regexp: ")))
7825   (let ((articles (gnus-find-matching-articles header regexp)))
7826     (prog1
7827         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7828       (gnus-summary-position-point))))
7829
7830 (defun gnus-summary-insert-dormant-articles ()
7831   "Insert all the dormant articles for this group into the current buffer."
7832   (interactive)
7833   (let ((gnus-verbose (max 6 gnus-verbose)))
7834     (if (not gnus-newsgroup-dormant)
7835         (gnus-message 3 "No cached articles for this group")
7836       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7837
7838 (defun gnus-summary-limit-include-dormant ()
7839   "Display all the hidden articles that are marked as dormant.
7840 Note that this command only works on a subset of the articles currently
7841 fetched for this group."
7842   (interactive)
7843   (unless gnus-newsgroup-dormant
7844     (error "There are no dormant articles in this group"))
7845   (prog1
7846       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7847     (gnus-summary-position-point)))
7848
7849 (defun gnus-summary-limit-exclude-dormant ()
7850   "Hide all dormant articles."
7851   (interactive)
7852   (prog1
7853       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7854     (gnus-summary-position-point)))
7855
7856 (defun gnus-summary-limit-exclude-childless-dormant ()
7857   "Hide all dormant articles that have no children."
7858   (interactive)
7859   (let ((data (gnus-data-list t))
7860         articles d children)
7861     ;; Find all articles that are either not dormant or have
7862     ;; children.
7863     (while (setq d (pop data))
7864       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7865                 (and (setq children
7866                            (gnus-article-children (gnus-data-number d)))
7867                      (let (found)
7868                        (while children
7869                          (when (memq (car children) articles)
7870                            (setq children nil
7871                                  found t))
7872                          (pop children))
7873                        found)))
7874         (push (gnus-data-number d) articles)))
7875     ;; Do the limiting.
7876     (prog1
7877         (gnus-summary-limit articles)
7878       (gnus-summary-position-point))))
7879
7880 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7881   "Mark all unread excluded articles as read.
7882 If ALL, mark even excluded ticked and dormants as read."
7883   (interactive "P")
7884   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7885   (let ((articles (gnus-sorted-ndifference
7886                    (sort
7887                     (mapcar (lambda (h) (mail-header-number h))
7888                             gnus-newsgroup-headers)
7889                     '<)
7890                    gnus-newsgroup-limit))
7891         article)
7892     (setq gnus-newsgroup-unreads
7893           (gnus-sorted-intersection gnus-newsgroup-unreads
7894                                     gnus-newsgroup-limit))
7895     (if all
7896         (setq gnus-newsgroup-dormant nil
7897               gnus-newsgroup-marked nil
7898               gnus-newsgroup-reads
7899               (nconc
7900                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7901                gnus-newsgroup-reads))
7902       (while (setq article (pop articles))
7903         (unless (or (memq article gnus-newsgroup-dormant)
7904                     (memq article gnus-newsgroup-marked))
7905           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7906
7907 (defun gnus-summary-limit (articles &optional pop)
7908   (if pop
7909       ;; We pop the previous limit off the stack and use that.
7910       (setq articles (car gnus-newsgroup-limits)
7911             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7912     ;; We use the new limit, so we push the old limit on the stack.
7913     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7914   ;; Set the limit.
7915   (setq gnus-newsgroup-limit articles)
7916   (let ((total (length gnus-newsgroup-data))
7917         (data (gnus-data-find-list (gnus-summary-article-number)))
7918         (gnus-summary-mark-below nil)   ; Inhibit this.
7919         found)
7920     ;; This will do all the work of generating the new summary buffer
7921     ;; according to the new limit.
7922     (gnus-summary-prepare)
7923     ;; Hide any threads, possibly.
7924     (gnus-summary-maybe-hide-threads)
7925     ;; Try to return to the article you were at, or one in the
7926     ;; neighborhood.
7927     (when data
7928       ;; We try to find some article after the current one.
7929       (while data
7930         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7931           (setq data nil
7932                 found t))
7933         (setq data (cdr data))))
7934     (unless found
7935       ;; If there is no data, that means that we were after the last
7936       ;; article.  The same goes when we can't find any articles
7937       ;; after the current one.
7938       (goto-char (point-max))
7939       (gnus-summary-find-prev))
7940     (gnus-set-mode-line 'summary)
7941     ;; We return how many articles were removed from the summary
7942     ;; buffer as a result of the new limit.
7943     (- total (length gnus-newsgroup-data))))
7944
7945 (defsubst gnus-invisible-cut-children (threads)
7946   (let ((num 0))
7947     (while threads
7948       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7949         (incf num))
7950       (pop threads))
7951     (< num 2)))
7952
7953 (defsubst gnus-cut-thread (thread)
7954   "Go forwards in the thread until we find an article that we want to display."
7955   (when (or (eq gnus-fetch-old-headers 'some)
7956             (eq gnus-fetch-old-headers 'invisible)
7957             (numberp gnus-fetch-old-headers)
7958             (eq gnus-build-sparse-threads 'some)
7959             (eq gnus-build-sparse-threads 'more))
7960     ;; Deal with old-fetched headers and sparse threads.
7961     (while (and
7962             thread
7963             (or
7964              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7965              (gnus-summary-article-ancient-p
7966               (mail-header-number (car thread))))
7967             (if (or (<= (length (cdr thread)) 1)
7968                     (eq gnus-fetch-old-headers 'invisible))
7969                 (setq gnus-newsgroup-limit
7970                       (delq (mail-header-number (car thread))
7971                             gnus-newsgroup-limit)
7972                       thread (cadr thread))
7973               (when (gnus-invisible-cut-children (cdr thread))
7974                 (let ((th (cdr thread)))
7975                   (while th
7976                     (if (memq (mail-header-number (caar th))
7977                               gnus-newsgroup-limit)
7978                         (setq thread (car th)
7979                               th nil)
7980                       (setq th (cdr th))))))))))
7981   thread)
7982
7983 (defun gnus-cut-threads (threads)
7984   "Cut off all uninteresting articles from the beginning of THREADS."
7985   (when (or (eq gnus-fetch-old-headers 'some)
7986             (eq gnus-fetch-old-headers 'invisible)
7987             (numberp gnus-fetch-old-headers)
7988             (eq gnus-build-sparse-threads 'some)
7989             (eq gnus-build-sparse-threads 'more))
7990     (let ((th threads))
7991       (while th
7992         (setcar th (gnus-cut-thread (car th)))
7993         (setq th (cdr th)))))
7994   ;; Remove nixed out threads.
7995   (delq nil threads))
7996
7997 (defun gnus-summary-initial-limit (&optional show-if-empty)
7998   "Figure out what the initial limit is supposed to be on group entry.
7999 This entails weeding out unwanted dormants, low-scored articles,
8000 fetch-old-headers verbiage, and so on."
8001   ;; Most groups have nothing to remove.
8002   (if (or gnus-inhibit-limiting
8003           (and (null gnus-newsgroup-dormant)
8004                (eq gnus-newsgroup-display 'gnus-not-ignore)
8005                (not (eq gnus-fetch-old-headers 'some))
8006                (not (numberp gnus-fetch-old-headers))
8007                (not (eq gnus-fetch-old-headers 'invisible))
8008                (null gnus-summary-expunge-below)
8009                (not (eq gnus-build-sparse-threads 'some))
8010                (not (eq gnus-build-sparse-threads 'more))
8011                (null gnus-thread-expunge-below)
8012                (not gnus-use-nocem)))
8013       ()                                ; Do nothing.
8014     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8015     (setq gnus-newsgroup-limit nil)
8016     (mapatoms
8017      (lambda (node)
8018        (unless (car (symbol-value node))
8019          ;; These threads have no parents -- they are roots.
8020          (let ((nodes (cdr (symbol-value node)))
8021                thread)
8022            (while nodes
8023              (if (and gnus-thread-expunge-below
8024                       (< (gnus-thread-total-score (car nodes))
8025                          gnus-thread-expunge-below))
8026                  (gnus-expunge-thread (pop nodes))
8027                (setq thread (pop nodes))
8028                (gnus-summary-limit-children thread))))))
8029      gnus-newsgroup-dependencies)
8030     ;; If this limitation resulted in an empty group, we might
8031     ;; pop the previous limit and use it instead.
8032     (when (and (not gnus-newsgroup-limit)
8033                show-if-empty)
8034       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8035     gnus-newsgroup-limit))
8036
8037 (defun gnus-summary-limit-children (thread)
8038   "Return 1 if this subthread is visible and 0 if it is not."
8039   ;; First we get the number of visible children to this thread.  This
8040   ;; is done by recursing down the thread using this function, so this
8041   ;; will really go down to a leaf article first, before slowly
8042   ;; working its way up towards the root.
8043   (when thread
8044     (let* ((max-lisp-eval-depth 5000)
8045            (children
8046            (if (cdr thread)
8047                (apply '+ (mapcar 'gnus-summary-limit-children
8048                                  (cdr thread)))
8049              0))
8050           (number (mail-header-number (car thread)))
8051           score)
8052       (if (and
8053            (not (memq number gnus-newsgroup-marked))
8054            (or
8055             ;; If this article is dormant and has absolutely no visible
8056             ;; children, then this article isn't visible.
8057             (and (memq number gnus-newsgroup-dormant)
8058                  (zerop children))
8059             ;; If this is "fetch-old-headered" and there is no
8060             ;; visible children, then we don't want this article.
8061             (and (or (eq gnus-fetch-old-headers 'some)
8062                      (numberp gnus-fetch-old-headers))
8063                  (gnus-summary-article-ancient-p number)
8064                  (zerop children))
8065             ;; If this is "fetch-old-headered" and `invisible', then
8066             ;; we don't want this article.
8067             (and (eq gnus-fetch-old-headers 'invisible)
8068                  (gnus-summary-article-ancient-p number))
8069             ;; If this is a sparsely inserted article with no children,
8070             ;; we don't want it.
8071             (and (eq gnus-build-sparse-threads 'some)
8072                  (gnus-summary-article-sparse-p number)
8073                  (zerop children))
8074             ;; If we use expunging, and this article is really
8075             ;; low-scored, then we don't want this article.
8076             (when (and gnus-summary-expunge-below
8077                        (< (setq score
8078                                 (or (cdr (assq number gnus-newsgroup-scored))
8079                                     gnus-summary-default-score))
8080                           gnus-summary-expunge-below))
8081               ;; We increase the expunge-tally here, but that has
8082               ;; nothing to do with the limits, really.
8083               (incf gnus-newsgroup-expunged-tally)
8084               ;; We also mark as read here, if that's wanted.
8085               (when (and gnus-summary-mark-below
8086                          (< score gnus-summary-mark-below))
8087                 (setq gnus-newsgroup-unreads
8088                       (delq number gnus-newsgroup-unreads))
8089                 (if gnus-newsgroup-auto-expire
8090                     (push number gnus-newsgroup-expirable)
8091                   (push (cons number gnus-low-score-mark)
8092                         gnus-newsgroup-reads)))
8093               t)
8094             ;; Do the `display' group parameter.
8095             (and gnus-newsgroup-display
8096                  (not (funcall gnus-newsgroup-display)))
8097             ;; Check NoCeM things.
8098             (when (and gnus-use-nocem
8099                        (gnus-nocem-unwanted-article-p
8100                         (mail-header-id (car thread))))
8101               (setq gnus-newsgroup-unreads
8102                     (delq number gnus-newsgroup-unreads))
8103               t)))
8104           ;; Nope, invisible article.
8105           0
8106         ;; Ok, this article is to be visible, so we add it to the limit
8107         ;; and return 1.
8108         (push number gnus-newsgroup-limit)
8109         1))))
8110
8111 (defun gnus-expunge-thread (thread)
8112   "Mark all articles in THREAD as read."
8113   (let* ((number (mail-header-number (car thread))))
8114     (incf gnus-newsgroup-expunged-tally)
8115     ;; We also mark as read here, if that's wanted.
8116     (setq gnus-newsgroup-unreads
8117           (delq number gnus-newsgroup-unreads))
8118     (if gnus-newsgroup-auto-expire
8119         (push number gnus-newsgroup-expirable)
8120       (push (cons number gnus-low-score-mark)
8121             gnus-newsgroup-reads)))
8122   ;; Go recursively through all subthreads.
8123   (mapcar 'gnus-expunge-thread (cdr thread)))
8124
8125 ;; Summary article oriented commands
8126
8127 (defun gnus-summary-refer-parent-article (n)
8128   "Refer parent article N times.
8129 If N is negative, go to ancestor -N instead.
8130 The difference between N and the number of articles fetched is returned."
8131   (interactive "p")
8132   (let ((skip 1)
8133         error header ref)
8134     (when (not (natnump n))
8135       (setq skip (abs n)
8136             n 1))
8137     (while (and (> n 0)
8138                 (not error))
8139       (setq header (gnus-summary-article-header))
8140       (if (and (eq (mail-header-number header)
8141                    (cdr gnus-article-current))
8142                (equal gnus-newsgroup-name
8143                       (car gnus-article-current)))
8144           ;; If we try to find the parent of the currently
8145           ;; displayed article, then we take a look at the actual
8146           ;; References header, since this is slightly more
8147           ;; reliable than the References field we got from the
8148           ;; server.
8149           (save-excursion
8150             (set-buffer gnus-original-article-buffer)
8151             (nnheader-narrow-to-headers)
8152             (unless (setq ref (message-fetch-field "references"))
8153               (when (setq ref (message-fetch-field "in-reply-to"))
8154                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8155             (widen))
8156         (setq ref
8157               ;; It's not the current article, so we take a bet on
8158               ;; the value we got from the server.
8159               (mail-header-references header)))
8160       (if (and ref
8161                (not (equal ref "")))
8162           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8163             (gnus-message 1 "Couldn't find parent"))
8164         (gnus-message 1 "No references in article %d"
8165                       (gnus-summary-article-number))
8166         (setq error t))
8167       (decf n))
8168     (gnus-summary-position-point)
8169     n))
8170
8171 (defun gnus-summary-refer-references ()
8172   "Fetch all articles mentioned in the References header.
8173 Return the number of articles fetched."
8174   (interactive)
8175   (let ((ref (mail-header-references (gnus-summary-article-header)))
8176         (current (gnus-summary-article-number))
8177         (n 0))
8178     (if (or (not ref)
8179             (equal ref ""))
8180         (error "No References in the current article")
8181       ;; For each Message-ID in the References header...
8182       (while (string-match "<[^>]*>" ref)
8183         (incf n)
8184         ;; ... fetch that article.
8185         (gnus-summary-refer-article
8186          (prog1 (match-string 0 ref)
8187            (setq ref (substring ref (match-end 0))))))
8188       (gnus-summary-goto-subject current)
8189       (gnus-summary-position-point)
8190       n)))
8191
8192 (defun gnus-summary-refer-thread (&optional limit)
8193   "Fetch all articles in the current thread.
8194 If LIMIT (the numerical prefix), fetch that many old headers instead
8195 of what's specified by the `gnus-refer-thread-limit' variable."
8196   (interactive "P")
8197   (let ((id (mail-header-id (gnus-summary-article-header)))
8198         (limit (if limit (prefix-numeric-value limit)
8199                  gnus-refer-thread-limit)))
8200     (unless (eq gnus-fetch-old-headers 'invisible)
8201       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8202       ;; Retrieve the headers and read them in.
8203       (if (eq (if (numberp limit)
8204                   (gnus-retrieve-headers
8205                    (list (min
8206                           (+ (mail-header-number
8207                               (gnus-summary-article-header))
8208                              limit)
8209                           gnus-newsgroup-end))
8210                    gnus-newsgroup-name (* limit 2))
8211                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8212                 ;; headers.
8213                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8214                                        gnus-newsgroup-name limit))
8215               'nov)
8216           (gnus-build-all-threads)
8217         (error "Can't fetch thread from back ends that don't support NOV"))
8218       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8219     (gnus-summary-limit-include-thread id)))
8220
8221 (defun gnus-summary-refer-article (message-id)
8222   "Fetch an article specified by MESSAGE-ID."
8223   (interactive "sMessage-ID: ")
8224   (when (and (stringp message-id)
8225              (not (zerop (length message-id))))
8226     (setq message-id (gnus-replace-in-string message-id " " ""))
8227     ;; Construct the correct Message-ID if necessary.
8228     ;; Suggested by tale@pawl.rpi.edu.
8229     (unless (string-match "^<" message-id)
8230       (setq message-id (concat "<" message-id)))
8231     (unless (string-match ">$" message-id)
8232       (setq message-id (concat message-id ">")))
8233     ;; People often post MIDs from URLs, so unhex it:
8234     (unless (string-match "@" message-id)
8235       (setq message-id (gnus-url-unhex-string message-id)))
8236     (let* ((header (gnus-id-to-header message-id))
8237            (sparse (and header
8238                         (gnus-summary-article-sparse-p
8239                          (mail-header-number header))
8240                         (memq (mail-header-number header)
8241                               gnus-newsgroup-limit)))
8242            number)
8243       (cond
8244        ;; If the article is present in the buffer we just go to it.
8245        ((and header
8246              (or (not (gnus-summary-article-sparse-p
8247                        (mail-header-number header)))
8248                  sparse))
8249         (prog1
8250             (gnus-summary-goto-article
8251              (mail-header-number header) nil t)
8252           (when sparse
8253             (gnus-summary-update-article (mail-header-number header)))))
8254        (t
8255         ;; We fetch the article.
8256         (catch 'found
8257           (dolist (gnus-override-method (gnus-refer-article-methods))
8258             (when (and (gnus-check-server gnus-override-method)
8259                        ;; Fetch the header,
8260                        (setq number (gnus-summary-insert-subject message-id)))
8261               ;; and display the article.
8262               (gnus-summary-select-article nil nil nil number)
8263               (throw 'found t)))
8264           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8265
8266 (defun gnus-refer-article-methods ()
8267   "Return a list of referable methods."
8268   (cond
8269    ;; No method, so we default to current and native.
8270    ((null gnus-refer-article-method)
8271     (list gnus-current-select-method gnus-select-method))
8272    ;; Current.
8273    ((eq 'current gnus-refer-article-method)
8274     (list gnus-current-select-method))
8275    ;; List of select methods.
8276    ((not (and (symbolp (car gnus-refer-article-method))
8277               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8278     (let (out)
8279       (dolist (method gnus-refer-article-method)
8280         (push (if (eq 'current method)
8281                   gnus-current-select-method
8282                 method)
8283               out))
8284       (nreverse out)))
8285    ;; One single select method.
8286    (t
8287     (list gnus-refer-article-method))))
8288
8289 (defun gnus-summary-edit-parameters ()
8290   "Edit the group parameters of the current group."
8291   (interactive)
8292   (gnus-group-edit-group gnus-newsgroup-name 'params))
8293
8294 (defun gnus-summary-customize-parameters ()
8295   "Customize the group parameters of the current group."
8296   (interactive)
8297   (gnus-group-customize gnus-newsgroup-name))
8298
8299 (defun gnus-summary-enter-digest-group (&optional force)
8300   "Enter an nndoc group based on the current article.
8301 If FORCE, force a digest interpretation.  If not, try
8302 to guess what the document format is."
8303   (interactive "P")
8304   (let ((conf gnus-current-window-configuration))
8305     (save-window-excursion
8306       (save-excursion
8307         (let (gnus-article-prepare-hook
8308               gnus-display-mime-function
8309               gnus-break-pages)
8310           (gnus-summary-select-article))))
8311     (setq gnus-current-window-configuration conf)
8312     (let* ((name (format "%s-%d"
8313                          (gnus-group-prefixed-name
8314                           gnus-newsgroup-name (list 'nndoc ""))
8315                          (with-current-buffer gnus-summary-buffer
8316                            gnus-current-article)))
8317            (ogroup gnus-newsgroup-name)
8318            (params (append (gnus-info-params (gnus-get-info ogroup))
8319                            (list (cons 'to-group ogroup))
8320                            (list (cons 'parent-group ogroup))
8321                            (list (cons 'save-article-group ogroup))))
8322            (case-fold-search t)
8323            (buf (current-buffer))
8324            dig to-address)
8325       (save-excursion
8326         (set-buffer gnus-original-article-buffer)
8327         ;; Have the digest group inherit the main mail address of
8328         ;; the parent article.
8329         (when (setq to-address (or (gnus-fetch-field "reply-to")
8330                                    (gnus-fetch-field "from")))
8331           (setq params (append
8332                         (list (cons 'to-address
8333                                     (funcall gnus-decode-encoded-word-function
8334                                              to-address))))))
8335         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8336         (insert-buffer-substring gnus-original-article-buffer)
8337         ;; Remove lines that may lead nndoc to misinterpret the
8338         ;; document type.
8339         (narrow-to-region
8340          (goto-char (point-min))
8341          (or (search-forward "\n\n" nil t) (point)))
8342         (goto-char (point-min))
8343         (delete-matching-lines "^Path:\\|^From ")
8344         (widen))
8345       (unwind-protect
8346           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8347                     (gnus-newsgroup-ephemeral-ignored-charsets
8348                      gnus-newsgroup-ignored-charsets))
8349                 (gnus-group-read-ephemeral-group
8350                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8351                               (nndoc-article-type
8352                                ,(if force 'mbox 'guess)))
8353                  t nil nil nil
8354                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8355                                                         "ADAPT")))))
8356               ;; Make all postings to this group go to the parent group.
8357               (nconc (gnus-info-params (gnus-get-info name))
8358                      params)
8359             ;; Couldn't select this doc group.
8360             (switch-to-buffer buf)
8361             (gnus-set-global-variables)
8362             (gnus-configure-windows 'summary)
8363             (gnus-message 3 "Article couldn't be entered?"))
8364         (kill-buffer dig)))))
8365
8366 (defun gnus-summary-read-document (n)
8367   "Open a new group based on the current article(s).
8368 This will allow you to read digests and other similar
8369 documents as newsgroups.
8370 Obeys the standard process/prefix convention."
8371   (interactive "P")
8372   (let* ((articles (gnus-summary-work-articles n))
8373          (ogroup gnus-newsgroup-name)
8374          (params (append (gnus-info-params (gnus-get-info ogroup))
8375                          (list (cons 'to-group ogroup))))
8376          article group egroup groups vgroup)
8377     (while (setq article (pop articles))
8378       (setq group (format "%s-%d" gnus-newsgroup-name article))
8379       (gnus-summary-remove-process-mark article)
8380       (when (gnus-summary-display-article article)
8381         (save-excursion
8382           (with-temp-buffer
8383             (insert-buffer-substring gnus-original-article-buffer)
8384             ;; Remove some headers that may lead nndoc to make
8385             ;; the wrong guess.
8386             (message-narrow-to-head)
8387             (goto-char (point-min))
8388             (delete-matching-lines "^Path:\\|^From ")
8389             (widen)
8390             (if (setq egroup
8391                       (gnus-group-read-ephemeral-group
8392                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8393                                      (nndoc-article-type guess))
8394                        t nil t))
8395                 (progn
8396             ;; Make all postings to this group go to the parent group.
8397                   (nconc (gnus-info-params (gnus-get-info egroup))
8398                          params)
8399                   (push egroup groups))
8400               ;; Couldn't select this doc group.
8401               (gnus-error 3 "Article couldn't be entered"))))))
8402     ;; Now we have selected all the documents.
8403     (cond
8404      ((not groups)
8405       (error "None of the articles could be interpreted as documents"))
8406      ((gnus-group-read-ephemeral-group
8407        (setq vgroup (format
8408                      "nnvirtual:%s-%s" gnus-newsgroup-name
8409                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8410        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8411        t
8412        (cons (current-buffer) 'summary)))
8413      (t
8414       (error "Couldn't select virtual nndoc group")))))
8415
8416 (defun gnus-summary-isearch-article (&optional regexp-p)
8417   "Do incremental search forward on the current article.
8418 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8419   (interactive "P")
8420   (gnus-summary-select-article)
8421   (gnus-configure-windows 'article)
8422   (gnus-eval-in-buffer-window gnus-article-buffer
8423     (save-restriction
8424       (widen)
8425       (isearch-forward regexp-p))))
8426
8427 (defun gnus-summary-search-article-forward (regexp &optional backward)
8428   "Search for an article containing REGEXP forward.
8429 If BACKWARD, search backward instead."
8430   (interactive
8431    (list (read-string
8432           (format "Search article %s (regexp%s): "
8433                   (if current-prefix-arg "backward" "forward")
8434                   (if gnus-last-search-regexp
8435                       (concat ", default " gnus-last-search-regexp)
8436                     "")))
8437          current-prefix-arg))
8438   (if (string-equal regexp "")
8439       (setq regexp (or gnus-last-search-regexp ""))
8440     (setq gnus-last-search-regexp regexp)
8441     (setq gnus-article-before-search gnus-current-article))
8442   ;; Intentionally set gnus-last-article.
8443   (setq gnus-last-article gnus-article-before-search)
8444   (let ((gnus-last-article gnus-last-article))
8445     (if (gnus-summary-search-article regexp backward)
8446         (gnus-summary-show-thread)
8447       (error "Search failed: \"%s\"" regexp))))
8448
8449 (defun gnus-summary-search-article-backward (regexp)
8450   "Search for an article containing REGEXP backward."
8451   (interactive
8452    (list (read-string
8453           (format "Search article backward (regexp%s): "
8454                   (if gnus-last-search-regexp
8455                       (concat ", default " gnus-last-search-regexp)
8456                     "")))))
8457   (gnus-summary-search-article-forward regexp 'backward))
8458
8459 (defun gnus-summary-search-article (regexp &optional backward)
8460   "Search for an article containing REGEXP.
8461 Optional argument BACKWARD means do search for backward.
8462 `gnus-select-article-hook' is not called during the search."
8463   ;; We have to require this here to make sure that the following
8464   ;; dynamic binding isn't shadowed by autoloading.
8465   (require 'gnus-async)
8466   (require 'gnus-art)
8467   (let ((gnus-select-article-hook nil)  ;Disable hook.
8468         (gnus-article-prepare-hook nil)
8469         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8470         (gnus-use-article-prefetch nil)
8471         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8472         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8473         (gnus-visual nil)
8474         (gnus-keep-backlog nil)
8475         (gnus-break-pages nil)
8476         (gnus-summary-display-arrow nil)
8477         (gnus-updated-mode-lines nil)
8478         (gnus-auto-center-summary nil)
8479         (sum (current-buffer))
8480         (gnus-display-mime-function nil)
8481         (found nil)
8482         point)
8483     (gnus-save-hidden-threads
8484       (gnus-summary-select-article)
8485       (set-buffer gnus-article-buffer)
8486       (goto-char (window-point (get-buffer-window (current-buffer))))
8487       (when backward
8488         (forward-line -1))
8489       (while (not found)
8490         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8491         (if (if backward
8492                 (re-search-backward regexp nil t)
8493               (re-search-forward regexp nil t))
8494             ;; We found the regexp.
8495             (progn
8496               (setq found 'found)
8497               (beginning-of-line)
8498               (set-window-start
8499                (get-buffer-window (current-buffer))
8500                (point))
8501               (forward-line 1)
8502               (set-window-point
8503                (get-buffer-window (current-buffer))
8504                (point))
8505               (set-buffer sum)
8506               (setq point (point)))
8507           ;; We didn't find it, so we go to the next article.
8508           (set-buffer sum)
8509           (setq found 'not)
8510           (while (eq found 'not)
8511             (if (not (if backward (gnus-summary-find-prev)
8512                        (gnus-summary-find-next)))
8513                 ;; No more articles.
8514                 (setq found t)
8515               ;; Select the next article and adjust point.
8516               (unless (gnus-summary-article-sparse-p
8517                        (gnus-summary-article-number))
8518                 (setq found nil)
8519                 (gnus-summary-select-article)
8520                 (set-buffer gnus-article-buffer)
8521                 (widen)
8522                 (goto-char (if backward (point-max) (point-min))))))))
8523       (gnus-message 7 ""))
8524     ;; Return whether we found the regexp.
8525     (when (eq found 'found)
8526       (goto-char point)
8527       (gnus-summary-show-thread)
8528       (gnus-summary-goto-subject gnus-current-article)
8529       (gnus-summary-position-point)
8530       t)))
8531
8532 (defun gnus-find-matching-articles (header regexp)
8533   "Return a list of all articles that match REGEXP on HEADER.
8534 This search includes all articles in the current group that Gnus has
8535 fetched headers for, whether they are displayed or not."
8536   (let ((articles nil)
8537         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8538         (case-fold-search t))
8539     (dolist (header gnus-newsgroup-headers)
8540       (when (string-match regexp (funcall func header))
8541         (push (mail-header-number header) articles)))
8542     (nreverse articles)))
8543
8544 (defun gnus-summary-find-matching (header regexp &optional backward unread
8545                                           not-case-fold not-matching)
8546   "Return a list of all articles that match REGEXP on HEADER.
8547 The search stars on the current article and goes forwards unless
8548 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8549 If UNREAD is non-nil, only unread articles will
8550 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8551 in the comparisons. If NOT-MATCHING, return a list of all articles that
8552 not match REGEXP on HEADER."
8553   (let ((case-fold-search (not not-case-fold))
8554         articles d func)
8555     (if (consp header)
8556         (if (eq (car header) 'extra)
8557             (setq func
8558                   `(lambda (h)
8559                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8560                          "")))
8561           (error "%s is an invalid header" header))
8562       (unless (fboundp (intern (concat "mail-header-" header)))
8563         (error "%s is not a valid header" header))
8564       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8565     (dolist (d (if (eq backward 'all)
8566                    gnus-newsgroup-data
8567                  (gnus-data-find-list
8568                   (gnus-summary-article-number)
8569                   (gnus-data-list backward))))
8570       (when (and (or (not unread)       ; We want all articles...
8571                      (gnus-data-unread-p d)) ; Or just unreads.
8572                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8573                  (if not-matching
8574                      (not (string-match
8575                            regexp
8576                            (funcall func (gnus-data-header d))))
8577                    (string-match regexp
8578                                  (funcall func (gnus-data-header d)))))
8579         (push (gnus-data-number d) articles))) ; Success!
8580     (nreverse articles)))
8581
8582 (defun gnus-summary-execute-command (header regexp command &optional backward)
8583   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8584 If HEADER is an empty string (or nil), the match is done on the entire
8585 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8586   (interactive
8587    (list (let ((completion-ignore-case t))
8588            (completing-read
8589             "Header name: "
8590             (mapcar (lambda (header) (list (format "%s" header)))
8591                     (append
8592                      '("Number" "Subject" "From" "Lines" "Date"
8593                        "Message-ID" "Xref" "References" "Body")
8594                      gnus-extra-headers))
8595             nil 'require-match))
8596          (read-string "Regexp: ")
8597          (read-key-sequence "Command: ")
8598          current-prefix-arg))
8599   (when (equal header "Body")
8600     (setq header ""))
8601   ;; Hidden thread subtrees must be searched as well.
8602   (gnus-summary-show-all-threads)
8603   ;; We don't want to change current point nor window configuration.
8604   (save-excursion
8605     (save-window-excursion
8606       (let (gnus-visual
8607             gnus-treat-strip-trailing-blank-lines
8608             gnus-treat-strip-leading-blank-lines
8609             gnus-treat-strip-multiple-blank-lines
8610             gnus-treat-hide-boring-headers
8611             gnus-treat-fold-newsgroups
8612             gnus-article-prepare-hook)
8613         (gnus-message 6 "Executing %s..." (key-description command))
8614         ;; We'd like to execute COMMAND interactively so as to give arguments.
8615         (gnus-execute header regexp
8616                       `(call-interactively ',(key-binding command))
8617                       backward)
8618         (gnus-message 6 "Executing %s...done" (key-description command))))))
8619
8620 (defun gnus-summary-beginning-of-article ()
8621   "Scroll the article back to the beginning."
8622   (interactive)
8623   (gnus-summary-select-article)
8624   (gnus-configure-windows 'article)
8625   (gnus-eval-in-buffer-window gnus-article-buffer
8626     (widen)
8627     (goto-char (point-min))
8628     (when gnus-break-pages
8629       (gnus-narrow-to-page))))
8630
8631 (defun gnus-summary-end-of-article ()
8632   "Scroll to the end of the article."
8633   (interactive)
8634   (gnus-summary-select-article)
8635   (gnus-configure-windows 'article)
8636   (gnus-eval-in-buffer-window gnus-article-buffer
8637     (widen)
8638     (goto-char (point-max))
8639     (recenter -3)
8640     (when gnus-break-pages
8641       (when (re-search-backward page-delimiter nil t)
8642         (narrow-to-region (match-end 0) (point-max)))
8643       (gnus-narrow-to-page))))
8644
8645 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8646   "Truncate to LEN and quote all \"(\"'s in STRING."
8647   (gnus-replace-in-string (if (and len (> (length string) len))
8648                               (substring string 0 len)
8649                             string)
8650                           "[()]" "\\\\\\&"))
8651
8652 (defun gnus-summary-print-article (&optional filename n)
8653   "Generate and print a PostScript image of the process-marked (mail) articles.
8654
8655 If used interactively, print the current article if none are
8656 process-marked.  With prefix arg, prompt the user for the name of the
8657 file to save in.
8658
8659 When used from Lisp, accept two optional args FILENAME and N.  N means
8660 to print the next N articles.  If N is negative, print the N previous
8661 articles.  If N is nil and articles have been marked with the process
8662 mark, print these instead.
8663
8664 If the optional first argument FILENAME is nil, send the image to the
8665 printer.  If FILENAME is a string, save the PostScript image in a file with
8666 that name.  If FILENAME is a number, prompt the user for the name of the file
8667 to save in."
8668   (interactive (list (ps-print-preprint current-prefix-arg)))
8669   (dolist (article (gnus-summary-work-articles n))
8670     (gnus-summary-select-article nil nil 'pseudo article)
8671     (gnus-eval-in-buffer-window gnus-article-buffer
8672       (gnus-print-buffer))
8673     (gnus-summary-remove-process-mark article))
8674   (ps-despool filename))
8675
8676 (defun gnus-print-buffer ()
8677   (let ((buffer (generate-new-buffer " *print*")))
8678     (unwind-protect
8679         (progn
8680           (copy-to-buffer buffer (point-min) (point-max))
8681           (set-buffer buffer)
8682           (gnus-remove-text-with-property 'gnus-decoration)
8683           (when (gnus-visual-p 'article-highlight 'highlight)
8684             ;; Copy-to-buffer doesn't copy overlay.  So redo
8685             ;; highlight.
8686             (let ((gnus-article-buffer buffer))
8687               (gnus-article-highlight-citation t)
8688               (gnus-article-highlight-signature)
8689               (gnus-article-emphasize)
8690               (gnus-article-delete-invisible-text)))
8691           (let ((ps-left-header
8692                  (list
8693                   (concat "("
8694                           (gnus-summary-print-truncate-and-quote
8695                            (mail-header-subject gnus-current-headers)
8696                            66) ")")
8697                   (concat "("
8698                           (gnus-summary-print-truncate-and-quote
8699                            (mail-header-from gnus-current-headers)
8700                            45) ")")))
8701                 (ps-right-header
8702                  (list
8703                   "/pagenumberstring load"
8704                   (concat "("
8705                           (mail-header-date gnus-current-headers) ")"))))
8706             (gnus-run-hooks 'gnus-ps-print-hook)
8707             (save-excursion
8708               (if window-system
8709                   (ps-spool-buffer-with-faces)
8710                 (ps-spool-buffer)))))
8711       (kill-buffer buffer))))
8712
8713 (defun gnus-summary-show-article (&optional arg)
8714   "Force redisplaying of the current article.
8715 If ARG (the prefix) is a number, show the article with the charset
8716 defined in `gnus-summary-show-article-charset-alist', or the charset
8717 input.
8718 If ARG (the prefix) is non-nil and not a number, show the raw article
8719 without any article massaging functions being run.  Normally, the key
8720 strokes are `C-u g'."
8721   (interactive "P")
8722   (cond
8723    ((numberp arg)
8724     (gnus-summary-show-article t)
8725     (let ((gnus-newsgroup-charset
8726            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8727                (mm-read-coding-system
8728                 "View as charset: " ;; actually it is coding system.
8729                 (save-excursion
8730                   (set-buffer gnus-article-buffer)
8731                   (mm-detect-coding-region (point) (point-max))))))
8732           (gnus-newsgroup-ignored-charsets 'gnus-all))
8733       (gnus-summary-select-article nil 'force)
8734       (let ((deps gnus-newsgroup-dependencies)
8735             head header lines)
8736         (save-excursion
8737           (set-buffer gnus-original-article-buffer)
8738           (save-restriction
8739             (message-narrow-to-head)
8740             (setq head (buffer-string))
8741             (goto-char (point-min))
8742             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8743               (goto-char (point-max))
8744               (widen)
8745               (setq lines (1- (count-lines (point) (point-max))))))
8746           (with-temp-buffer
8747             (insert (format "211 %d Article retrieved.\n"
8748                             (cdr gnus-article-current)))
8749             (insert head)
8750             (if lines (insert (format "Lines: %d\n" lines)))
8751             (insert ".\n")
8752             (let ((nntp-server-buffer (current-buffer)))
8753               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8754         (gnus-data-set-header
8755          (gnus-data-find (cdr gnus-article-current))
8756          header)
8757         (gnus-summary-update-article-line
8758          (cdr gnus-article-current) header)
8759         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8760           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8761    ((not arg)
8762     ;; Select the article the normal way.
8763     (gnus-summary-select-article nil 'force))
8764    (t
8765     ;; We have to require this here to make sure that the following
8766     ;; dynamic binding isn't shadowed by autoloading.
8767     (require 'gnus-async)
8768     (require 'gnus-art)
8769     ;; Bind the article treatment functions to nil.
8770     (let ((gnus-have-all-headers t)
8771           gnus-article-prepare-hook
8772           gnus-article-decode-hook
8773           gnus-display-mime-function
8774           gnus-break-pages)
8775       ;; Destroy any MIME parts.
8776       (when (gnus-buffer-live-p gnus-article-buffer)
8777         (save-excursion
8778           (set-buffer gnus-article-buffer)
8779           (mm-destroy-parts gnus-article-mime-handles)
8780           ;; Set it to nil for safety reason.
8781           (setq gnus-article-mime-handle-alist nil)
8782           (setq gnus-article-mime-handles nil)))
8783       (gnus-summary-select-article nil 'force))))
8784   (gnus-summary-goto-subject gnus-current-article)
8785   (gnus-summary-position-point))
8786
8787 (defun gnus-summary-show-raw-article ()
8788   "Show the raw article without any article massaging functions being run."
8789   (interactive)
8790   (gnus-summary-show-article t))
8791
8792 (defun gnus-summary-verbose-headers (&optional arg)
8793   "Toggle permanent full header display.
8794 If ARG is a positive number, turn header display on.
8795 If ARG is a negative number, turn header display off."
8796   (interactive "P")
8797   (setq gnus-show-all-headers
8798         (cond ((or (not (numberp arg))
8799                    (zerop arg))
8800                (not gnus-show-all-headers))
8801               ((natnump arg)
8802                t)))
8803   (gnus-summary-show-article))
8804
8805 (defun gnus-summary-toggle-header (&optional arg)
8806   "Show the headers if they are hidden, or hide them if they are shown.
8807 If ARG is a positive number, show the entire header.
8808 If ARG is a negative number, hide the unwanted header lines."
8809   (interactive "P")
8810   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8811                      (get-buffer-window gnus-article-buffer t))))
8812     (with-current-buffer gnus-article-buffer
8813       (widen)
8814       (article-narrow-to-head)
8815       (let* ((buffer-read-only nil)
8816              (inhibit-point-motion-hooks t)
8817              (hidden (if (numberp arg)
8818                          (>= arg 0)
8819                        (gnus-article-hidden-text-p 'headers)))
8820              s e)
8821         (delete-region (point-min) (point-max))
8822         (with-current-buffer gnus-original-article-buffer
8823           (goto-char (setq s (point-min)))
8824           (setq e (if (search-forward "\n\n" nil t)
8825                       (1- (point))
8826                     (point-max))))
8827         (insert-buffer-substring gnus-original-article-buffer s e)
8828         (run-hooks 'gnus-article-decode-hook)
8829         (if hidden
8830             (let ((gnus-treat-hide-headers nil)
8831                   (gnus-treat-hide-boring-headers nil))
8832               (gnus-delete-wash-type 'headers)
8833               (gnus-treat-article 'head))
8834           (gnus-treat-article 'head))
8835         (widen)
8836         (if window
8837             (set-window-start window (goto-char (point-min))))
8838         (if gnus-break-pages
8839             (gnus-narrow-to-page)
8840           (when (gnus-visual-p 'page-marker)
8841             (let ((buffer-read-only nil))
8842               (gnus-remove-text-with-property 'gnus-prev)
8843               (gnus-remove-text-with-property 'gnus-next))))
8844         (gnus-set-mode-line 'article)))))
8845
8846 (defun gnus-summary-show-all-headers ()
8847   "Make all header lines visible."
8848   (interactive)
8849   (gnus-summary-toggle-header 1))
8850
8851 (defun gnus-summary-caesar-message (&optional arg)
8852   "Caesar rotate the current article by 13.
8853 The numerical prefix specifies how many places to rotate each letter
8854 forward."
8855   (interactive "P")
8856   (gnus-summary-select-article)
8857   (let ((mail-header-separator ""))
8858     (gnus-eval-in-buffer-window gnus-article-buffer
8859       (save-restriction
8860         (widen)
8861         (let ((start (window-start))
8862               buffer-read-only)
8863           (message-caesar-buffer-body arg)
8864           (set-window-start (get-buffer-window (current-buffer)) start))))))
8865
8866 (autoload 'unmorse-region "morse"
8867   "Convert morse coded text in region to ordinary ASCII text."
8868   t)
8869
8870 (defun gnus-summary-morse-message (&optional arg)
8871   "Morse decode the current article."
8872   (interactive "P")
8873   (gnus-summary-select-article)
8874   (let ((mail-header-separator ""))
8875     (gnus-eval-in-buffer-window gnus-article-buffer
8876       (save-excursion
8877         (save-restriction
8878           (widen)
8879           (let ((pos (window-start))
8880                 buffer-read-only)
8881             (goto-char (point-min))
8882             (when (message-goto-body)
8883               (gnus-narrow-to-body))
8884             (goto-char (point-min))
8885             (while (search-forward "·" (point-max) t)
8886               (replace-match "."))
8887             (unmorse-region (point-min) (point-max))
8888             (widen)
8889             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8890
8891 (defun gnus-summary-stop-page-breaking ()
8892   "Stop page breaking in the current article."
8893   (interactive)
8894   (gnus-summary-select-article)
8895   (gnus-eval-in-buffer-window gnus-article-buffer
8896     (widen)
8897     (when (gnus-visual-p 'page-marker)
8898       (let ((buffer-read-only nil))
8899         (gnus-remove-text-with-property 'gnus-prev)
8900         (gnus-remove-text-with-property 'gnus-next))
8901       (setq gnus-page-broken nil))))
8902
8903 (defun gnus-summary-move-article (&optional n to-newsgroup
8904                                             select-method action)
8905   "Move the current article to a different newsgroup.
8906 If N is a positive number, move the N next articles.
8907 If N is a negative number, move the N previous articles.
8908 If N is nil and any articles have been marked with the process mark,
8909 move those articles instead.
8910 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8911 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8912 re-spool using this method.
8913
8914 When called interactively with TO-NEWSGROUP being nil, the value of
8915 the variable `gnus-move-split-methods' is used for finding a default
8916 for the target newsgroup.
8917
8918 For this function to work, both the current newsgroup and the
8919 newsgroup that you want to move to have to support the `request-move'
8920 and `request-accept' functions.
8921
8922 ACTION can be either `move' (the default), `crosspost' or `copy'."
8923   (interactive "P")
8924   (unless action
8925     (setq action 'move))
8926   ;; Check whether the source group supports the required functions.
8927   (cond ((and (eq action 'move)
8928               (not (gnus-check-backend-function
8929                     'request-move-article gnus-newsgroup-name)))
8930          (error "The current group does not support article moving"))
8931         ((and (eq action 'crosspost)
8932               (not (gnus-check-backend-function
8933                     'request-replace-article gnus-newsgroup-name)))
8934          (error "The current group does not support article editing")))
8935   (let ((articles (gnus-summary-work-articles n))
8936         (prefix (if (gnus-check-backend-function
8937                      'request-move-article gnus-newsgroup-name)
8938                     (funcall gnus-move-group-prefix-function
8939                              gnus-newsgroup-name)
8940                   ""))
8941         (names '((move "Move" "Moving")
8942                  (copy "Copy" "Copying")
8943                  (crosspost "Crosspost" "Crossposting")))
8944         (copy-buf (save-excursion
8945                     (nnheader-set-temp-buffer " *copy article*")))
8946         art-group to-method new-xref article to-groups)
8947     (unless (assq action names)
8948       (error "Unknown action %s" action))
8949     ;; Read the newsgroup name.
8950     (when (and (not to-newsgroup)
8951                (not select-method))
8952       (if (and gnus-move-split-methods
8953                (not
8954                 (and (memq gnus-current-article articles)
8955                      (gnus-buffer-live-p gnus-original-article-buffer))))
8956           ;; When `gnus-move-split-methods' is non-nil, we have to
8957           ;; select an article to give `gnus-read-move-group-name' an
8958           ;; opportunity to suggest an appropriate default.  However,
8959           ;; we needn't render or mark the article.
8960           (let ((gnus-display-mime-function nil)
8961                 (gnus-article-prepare-hook nil)
8962                 (gnus-mark-article-hook nil))
8963             (gnus-summary-select-article nil nil nil (car articles))))
8964       (setq to-newsgroup
8965             (gnus-read-move-group-name
8966              (cadr (assq action names))
8967              (symbol-value (intern (format "gnus-current-%s-group" action)))
8968              articles prefix))
8969       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8970     (setq to-method (or select-method
8971                         (gnus-server-to-method
8972                          (gnus-group-method to-newsgroup))))
8973     ;; Check the method we are to move this article to...
8974     (unless (gnus-check-backend-function
8975              'request-accept-article (car to-method))
8976       (error "%s does not support article copying" (car to-method)))
8977     (unless (gnus-check-server to-method)
8978       (error "Can't open server %s" (car to-method)))
8979     (gnus-message 6 "%s to %s: %s..."
8980                   (caddr (assq action names))
8981                   (or (car select-method) to-newsgroup) articles)
8982     (while articles
8983       (setq article (pop articles))
8984       (setq
8985        art-group
8986        (cond
8987         ;; Move the article.
8988         ((eq action 'move)
8989          ;; Remove this article from future suppression.
8990          (gnus-dup-unsuppress-article article)
8991          (gnus-request-move-article
8992           article                       ; Article to move
8993           gnus-newsgroup-name           ; From newsgroup
8994           (nth 1 (gnus-find-method-for-group
8995                   gnus-newsgroup-name)) ; Server
8996           (list 'gnus-request-accept-article
8997                 to-newsgroup (list 'quote select-method)
8998                 (not articles) t)       ; Accept form
8999           (not articles)))              ; Only save nov last time
9000         ;; Copy the article.
9001         ((eq action 'copy)
9002          (save-excursion
9003            (set-buffer copy-buf)
9004            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9005              (gnus-request-accept-article
9006               to-newsgroup select-method (not articles) t))))
9007         ;; Crosspost the article.
9008         ((eq action 'crosspost)
9009          (let ((xref (message-tokenize-header
9010                       (mail-header-xref (gnus-summary-article-header article))
9011                       " ")))
9012            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9013                                   ":" (number-to-string article)))
9014            (unless xref
9015              (setq xref (list (system-name))))
9016            (setq new-xref
9017                  (concat
9018                   (mapconcat 'identity
9019                              (delete "Xref:" (delete new-xref xref))
9020                              " ")
9021                   " " new-xref))
9022            (save-excursion
9023              (set-buffer copy-buf)
9024              ;; First put the article in the destination group.
9025              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9026              (when (consp (setq art-group
9027                                 (gnus-request-accept-article
9028                                  to-newsgroup select-method (not articles))))
9029                (setq new-xref (concat new-xref " " (car art-group)
9030                                       ":"
9031                                       (number-to-string (cdr art-group))))
9032                ;; Now we have the new Xrefs header, so we insert
9033                ;; it and replace the new article.
9034                (nnheader-replace-header "Xref" new-xref)
9035                (gnus-request-replace-article
9036                 (cdr art-group) to-newsgroup (current-buffer))
9037                art-group))))))
9038       (cond
9039        ((not art-group)
9040         (gnus-message 1 "Couldn't %s article %s: %s"
9041                       (cadr (assq action names)) article
9042                       (nnheader-get-report (car to-method))))
9043        ((eq art-group 'junk)
9044         (when (eq action 'move)
9045           (gnus-summary-mark-article article gnus-canceled-mark)
9046           (gnus-message 4 "Deleted article %s" article)
9047           ;; run the delete hook
9048           (run-hook-with-args 'gnus-summary-article-delete-hook
9049                               action
9050                               (gnus-data-header
9051                                (assoc article (gnus-data-list nil)))
9052                               gnus-newsgroup-name nil
9053                               select-method)))
9054        (t
9055         (let* ((pto-group (gnus-group-prefixed-name
9056                            (car art-group) to-method))
9057                (info (gnus-get-info pto-group))
9058                (to-group (gnus-info-group info))
9059                to-marks)
9060           ;; Update the group that has been moved to.
9061           (when (and info
9062                      (memq action '(move copy)))
9063             (unless (member to-group to-groups)
9064               (push to-group to-groups))
9065
9066             (unless (memq article gnus-newsgroup-unreads)
9067               (push 'read to-marks)
9068               (gnus-info-set-read
9069                info (gnus-add-to-range (gnus-info-read info)
9070                                        (list (cdr art-group)))))
9071
9072             ;; See whether the article is to be put in the cache.
9073             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9074                              gnus-article-mark-lists
9075                            (delete '(expirable . expire)
9076                                    (copy-sequence gnus-article-mark-lists))))
9077                   (to-article (cdr art-group)))
9078
9079               ;; Enter the article into the cache in the new group,
9080               ;; if that is required.
9081               (when gnus-use-cache
9082                 (gnus-cache-possibly-enter-article
9083                  to-group to-article
9084                  (memq article gnus-newsgroup-marked)
9085                  (memq article gnus-newsgroup-dormant)
9086                  (memq article gnus-newsgroup-unreads)))
9087
9088               (when gnus-preserve-marks
9089                 ;; Copy any marks over to the new group.
9090                 (when (and (equal to-group gnus-newsgroup-name)
9091                            (not (memq article gnus-newsgroup-unreads)))
9092                   ;; Mark this article as read in this group.
9093                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9094                   (setcdr (gnus-active to-group) to-article)
9095                   (setcdr gnus-newsgroup-active to-article))
9096
9097                 (while marks
9098                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9099                     (when (memq article (symbol-value
9100                                          (intern (format "gnus-newsgroup-%s"
9101                                                          (caar marks)))))
9102                       (push (cdar marks) to-marks)
9103                       ;; If the other group is the same as this group,
9104                       ;; then we have to add the mark to the list.
9105                       (when (equal to-group gnus-newsgroup-name)
9106                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9107                              (cons to-article
9108                                    (symbol-value
9109                                     (intern (format "gnus-newsgroup-%s"
9110                                                     (caar marks)))))))
9111                       ;; Copy the marks to other group.
9112                       (gnus-add-marked-articles
9113                        to-group (cdar marks) (list to-article) info)))
9114                   (setq marks (cdr marks)))
9115
9116                 (gnus-request-set-mark
9117                  to-group (list (list (list to-article) 'add to-marks))))
9118
9119               (gnus-dribble-enter
9120                (concat "(gnus-group-set-info '"
9121                        (gnus-prin1-to-string (gnus-get-info to-group))
9122                        ")"))))
9123
9124           ;; Update the Xref header in this article to point to
9125           ;; the new crossposted article we have just created.
9126           (when (eq action 'crosspost)
9127             (save-excursion
9128               (set-buffer copy-buf)
9129               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9130               (nnheader-replace-header "Xref" new-xref)
9131               (gnus-request-replace-article
9132                article gnus-newsgroup-name (current-buffer))))
9133
9134           ;; run the move/copy/crosspost/respool hook
9135           (run-hook-with-args 'gnus-summary-article-move-hook
9136                               action
9137                               (gnus-data-header
9138                                (assoc article (gnus-data-list nil)))
9139                               gnus-newsgroup-name
9140                               to-newsgroup
9141                               select-method))
9142
9143         ;;;!!!Why is this necessary?
9144         (set-buffer gnus-summary-buffer)
9145         
9146         (gnus-summary-goto-subject article)
9147         (when (eq action 'move)
9148           (gnus-summary-mark-article article gnus-canceled-mark))))
9149       (gnus-summary-remove-process-mark article))
9150     ;; Re-activate all groups that have been moved to.
9151     (save-excursion
9152       (set-buffer gnus-group-buffer)
9153       (let ((gnus-group-marked to-groups))
9154         (gnus-group-get-new-news-this-group nil t)))
9155
9156     (gnus-kill-buffer copy-buf)
9157     (gnus-summary-position-point)
9158     (gnus-set-mode-line 'summary)))
9159
9160 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9161   "Move the current article to a different newsgroup.
9162 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9163 When called interactively, if TO-NEWSGROUP is nil, use the value of
9164 the variable `gnus-move-split-methods' for finding a default target
9165 newsgroup.
9166 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9167 re-spool using this method."
9168   (interactive "P")
9169   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9170
9171 (defun gnus-summary-crosspost-article (&optional n)
9172   "Crosspost the current article to some other group."
9173   (interactive "P")
9174   (gnus-summary-move-article n nil nil 'crosspost))
9175
9176 (defcustom gnus-summary-respool-default-method nil
9177   "Default method type for respooling an article.
9178 If nil, use to the current newsgroup method."
9179   :type 'symbol
9180   :group 'gnus-summary-mail)
9181
9182 (defcustom gnus-summary-display-while-building nil
9183   "If non-nil, show and update the summary buffer as it's being built.
9184 If the value is t, update the buffer after every line is inserted.  If
9185 the value is an integer (N), update the display every N lines."
9186   :group 'gnus-thread
9187   :type '(choice (const :tag "off" nil)
9188                  number
9189                  (const :tag "frequently" t)))
9190
9191 (defun gnus-summary-respool-article (&optional n method)
9192   "Respool the current article.
9193 The article will be squeezed through the mail spooling process again,
9194 which means that it will be put in some mail newsgroup or other
9195 depending on `nnmail-split-methods'.
9196 If N is a positive number, respool the N next articles.
9197 If N is a negative number, respool the N previous articles.
9198 If N is nil and any articles have been marked with the process mark,
9199 respool those articles instead.
9200
9201 Respooling can be done both from mail groups and \"real\" newsgroups.
9202 In the former case, the articles in question will be moved from the
9203 current group into whatever groups they are destined to.  In the
9204 latter case, they will be copied into the relevant groups."
9205   (interactive
9206    (list current-prefix-arg
9207          (let* ((methods (gnus-methods-using 'respool))
9208                 (methname
9209                  (symbol-name (or gnus-summary-respool-default-method
9210                                   (car (gnus-find-method-for-group
9211                                         gnus-newsgroup-name)))))
9212                 (method
9213                  (gnus-completing-read-with-default
9214                   methname "What backend do you want to use when respooling?"
9215                   methods nil t nil 'gnus-mail-method-history))
9216                 ms)
9217            (cond
9218             ((zerop (length (setq ms (gnus-servers-using-backend
9219                                       (intern method)))))
9220              (list (intern method) ""))
9221             ((= 1 (length ms))
9222              (car ms))
9223             (t
9224              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9225                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9226                            ms-alist))))))))
9227   (unless method
9228     (error "No method given for respooling"))
9229   (if (assoc (symbol-name
9230               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9231              (gnus-methods-using 'respool))
9232       (gnus-summary-move-article n nil method)
9233     (gnus-summary-copy-article n nil method)))
9234
9235 (defun gnus-summary-import-article (file &optional edit)
9236   "Import an arbitrary file into a mail newsgroup."
9237   (interactive "fImport file: \nP")
9238   (let ((group gnus-newsgroup-name)
9239         (now (current-time))
9240         atts lines group-art)
9241     (unless (gnus-check-backend-function 'request-accept-article group)
9242       (error "%s does not support article importing" group))
9243     (or (file-readable-p file)
9244         (not (file-regular-p file))
9245         (error "Can't read %s" file))
9246     (save-excursion
9247       (set-buffer (gnus-get-buffer-create " *import file*"))
9248       (erase-buffer)
9249       (nnheader-insert-file-contents file)
9250       (goto-char (point-min))
9251       (if (nnheader-article-p)
9252           (save-restriction
9253             (goto-char (point-min))
9254             (search-forward "\n\n" nil t)
9255             (narrow-to-region (point-min) (1- (point)))
9256             (goto-char (point-min))
9257             (unless (re-search-forward "^date:" nil t)
9258               (goto-char (point-max))
9259               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9260        ;; This doesn't look like an article, so we fudge some headers.
9261         (setq atts (file-attributes file)
9262               lines (count-lines (point-min) (point-max)))
9263         (insert "From: " (read-string "From: ") "\n"
9264                 "Subject: " (read-string "Subject: ") "\n"
9265                 "Date: " (message-make-date (nth 5 atts)) "\n"
9266                 "Message-ID: " (message-make-message-id) "\n"
9267                 "Lines: " (int-to-string lines) "\n"
9268                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9269       (setq group-art (gnus-request-accept-article group nil t))
9270       (kill-buffer (current-buffer)))
9271     (setq gnus-newsgroup-active (gnus-activate-group group))
9272     (forward-line 1)
9273     (gnus-summary-goto-article (cdr group-art) nil t)
9274     (when edit
9275       (gnus-summary-edit-article))))
9276
9277 (defun gnus-summary-create-article ()
9278   "Create an article in a mail newsgroup."
9279   (interactive)
9280   (let ((group gnus-newsgroup-name)
9281         (now (current-time))
9282         group-art)
9283     (unless (gnus-check-backend-function 'request-accept-article group)
9284       (error "%s does not support article importing" group))
9285     (save-excursion
9286       (set-buffer (gnus-get-buffer-create " *import file*"))
9287       (erase-buffer)
9288       (goto-char (point-min))
9289       ;; This doesn't look like an article, so we fudge some headers.
9290       (insert "From: " (read-string "From: ") "\n"
9291               "Subject: " (read-string "Subject: ") "\n"
9292               "Date: " (message-make-date now) "\n"
9293               "Message-ID: " (message-make-message-id) "\n")
9294       (setq group-art (gnus-request-accept-article group nil t))
9295       (kill-buffer (current-buffer)))
9296     (setq gnus-newsgroup-active (gnus-activate-group group))
9297     (forward-line 1)
9298     (gnus-summary-goto-article (cdr group-art) nil t)
9299     (gnus-summary-edit-article)))
9300
9301 (defun gnus-summary-article-posted-p ()
9302   "Say whether the current (mail) article is available from news as well.
9303 This will be the case if the article has both been mailed and posted."
9304   (interactive)
9305   (let ((id (mail-header-references (gnus-summary-article-header)))
9306         (gnus-override-method (car (gnus-refer-article-methods))))
9307     (if (gnus-request-head id "")
9308         (gnus-message 2 "The current message was found on %s"
9309                       gnus-override-method)
9310       (gnus-message 2 "The current message couldn't be found on %s"
9311                     gnus-override-method)
9312       nil)))
9313
9314 (defun gnus-summary-expire-articles (&optional now)
9315   "Expire all articles that are marked as expirable in the current group."
9316   (interactive)
9317   (when (and (not gnus-group-is-exiting-without-update-p)
9318              (gnus-check-backend-function
9319               'request-expire-articles gnus-newsgroup-name))
9320     ;; This backend supports expiry.
9321     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9322            (expirable (if total
9323                           (progn
9324                             ;; We need to update the info for
9325                             ;; this group for `gnus-list-of-read-articles'
9326                             ;; to give us the right answer.
9327                             (gnus-run-hooks 'gnus-exit-group-hook)
9328                             (gnus-summary-update-info)
9329                             (gnus-list-of-read-articles gnus-newsgroup-name))
9330                         (setq gnus-newsgroup-expirable
9331                               (sort gnus-newsgroup-expirable '<))))
9332            (expiry-wait (if now 'immediate
9333                           (gnus-group-find-parameter
9334                            gnus-newsgroup-name 'expiry-wait)))
9335            (nnmail-expiry-target
9336             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9337                 nnmail-expiry-target))
9338            es)
9339       (when expirable
9340         ;; There are expirable articles in this group, so we run them
9341         ;; through the expiry process.
9342         (gnus-message 6 "Expiring articles...")
9343         (unless (gnus-check-group gnus-newsgroup-name)
9344           (error "Can't open server for %s" gnus-newsgroup-name))
9345         ;; The list of articles that weren't expired is returned.
9346         (save-excursion
9347           (if expiry-wait
9348               (let ((nnmail-expiry-wait-function nil)
9349                     (nnmail-expiry-wait expiry-wait))
9350                 (setq es (gnus-request-expire-articles
9351                           expirable gnus-newsgroup-name)))
9352             (setq es (gnus-request-expire-articles
9353                       expirable gnus-newsgroup-name)))
9354           (unless total
9355             (setq gnus-newsgroup-expirable es))
9356           ;; We go through the old list of expirable, and mark all
9357           ;; really expired articles as nonexistent.
9358           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9359             (let ((gnus-use-cache nil))
9360               (dolist (article expirable)
9361                 (when (and (not (memq article es))
9362                            (gnus-data-find article))
9363                   (gnus-summary-mark-article article gnus-canceled-mark)
9364                   (run-hook-with-args 'gnus-summary-article-expire-hook
9365                                       'delete
9366                                       (gnus-data-header
9367                                        (assoc article (gnus-data-list nil)))
9368                                       gnus-newsgroup-name
9369                                       nil
9370                                       nil))))))
9371         (gnus-message 6 "Expiring articles...done")))))
9372
9373 (defun gnus-summary-expire-articles-now ()
9374   "Expunge all expirable articles in the current group.
9375 This means that *all* articles that are marked as expirable will be
9376 deleted forever, right now."
9377   (interactive)
9378   (or gnus-expert-user
9379       (gnus-yes-or-no-p
9380        "Are you really, really, really sure you want to delete all these messages? ")
9381       (error "Phew!"))
9382   (gnus-summary-expire-articles t))
9383
9384 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9385 (defun gnus-summary-delete-article (&optional n)
9386   "Delete the N next (mail) articles.
9387 This command actually deletes articles.  This is not a marking
9388 command.  The article will disappear forever from your life, never to
9389 return.
9390
9391 If N is negative, delete backwards.
9392 If N is nil and articles have been marked with the process mark,
9393 delete these instead.
9394
9395 If `gnus-novice-user' is non-nil you will be asked for
9396 confirmation before the articles are deleted."
9397   (interactive "P")
9398   (unless (gnus-check-backend-function 'request-expire-articles
9399                                        gnus-newsgroup-name)
9400     (error "The current newsgroup does not support article deletion"))
9401   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9402     (error "Couldn't open server"))
9403   ;; Compute the list of articles to delete.
9404   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9405         (nnmail-expiry-target 'delete)
9406         not-deleted)
9407     (if (and gnus-novice-user
9408              (not (gnus-yes-or-no-p
9409                    (format "Do you really want to delete %s forever? "
9410                            (if (> (length articles) 1)
9411                                (format "these %s articles" (length articles))
9412                              "this article")))))
9413         ()
9414       ;; Delete the articles.
9415       (setq not-deleted (gnus-request-expire-articles
9416                          articles gnus-newsgroup-name 'force))
9417       (while articles
9418         (gnus-summary-remove-process-mark (car articles))
9419         ;; The backend might not have been able to delete the article
9420         ;; after all.
9421         (unless (memq (car articles) not-deleted)
9422           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9423         (let* ((article (car articles))
9424                (ghead  (gnus-data-header
9425                                     (assoc article (gnus-data-list nil)))))
9426           (run-hook-with-args 'gnus-summary-article-delete-hook
9427                               'delete ghead gnus-newsgroup-name nil
9428                               nil))
9429         (setq articles (cdr articles)))
9430       (when not-deleted
9431         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9432     (gnus-summary-position-point)
9433     (gnus-set-mode-line 'summary)
9434     not-deleted))
9435
9436 (defun gnus-summary-edit-article (&optional arg)
9437   "Edit the current article.
9438 This will have permanent effect only in mail groups.
9439 If ARG is nil, edit the decoded articles.
9440 If ARG is 1, edit the raw articles.
9441 If ARG is 2, edit the raw articles even in read-only groups.
9442 If ARG is 3, edit the articles with the current handles.
9443 Otherwise, allow editing of articles even in read-only
9444 groups."
9445   (interactive "P")
9446   (let (force raw current-handles)
9447     (cond
9448      ((null arg))
9449      ((eq arg 1)
9450       (setq raw t))
9451      ((eq arg 2)
9452       (setq raw t
9453             force t))
9454      ((eq arg 3)
9455       (setq current-handles
9456             (and (gnus-buffer-live-p gnus-article-buffer)
9457                  (with-current-buffer gnus-article-buffer
9458                    (prog1
9459                        gnus-article-mime-handles
9460                      (setq gnus-article-mime-handles nil))))))
9461      (t
9462       (setq force t)))
9463     (when (and raw (not force)
9464                (member gnus-newsgroup-name '("nndraft:delayed"
9465                                              "nndraft:drafts"
9466                                              "nndraft:queue")))
9467       (error "Can't edit the raw article in group %s"
9468              gnus-newsgroup-name))
9469     (save-excursion
9470       (set-buffer gnus-summary-buffer)
9471       (let ((mail-parse-charset gnus-newsgroup-charset)
9472             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9473         (gnus-set-global-variables)
9474         (when (and (not force)
9475                    (gnus-group-read-only-p))
9476           (error "The current newsgroup does not support article editing"))
9477         (gnus-summary-show-article t)
9478         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9479           (with-current-buffer gnus-article-buffer
9480             (mm-enable-multibyte)))
9481         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9482             (setq raw t))
9483         (gnus-article-edit-article
9484          (if raw 'ignore
9485            `(lambda ()
9486               (let ((mbl mml-buffer-list))
9487                 (setq mml-buffer-list nil)
9488                 (mime-to-mml ,'current-handles)
9489                 (let ((mbl1 mml-buffer-list))
9490                   (setq mml-buffer-list mbl)
9491                   (set (make-local-variable 'mml-buffer-list) mbl1))
9492                 (gnus-make-local-hook 'kill-buffer-hook)
9493                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9494          `(lambda (no-highlight)
9495             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9496                   (message-options message-options)
9497                   (message-options-set-recipient)
9498                   (mail-parse-ignored-charsets
9499                    ',gnus-newsgroup-ignored-charsets))
9500               ,(if (not raw) '(progn
9501                                 (mml-to-mime)
9502                                 (mml-destroy-buffers)
9503                                 (remove-hook 'kill-buffer-hook
9504                                              'mml-destroy-buffers t)
9505                                 (kill-local-variable 'mml-buffer-list)))
9506               (gnus-summary-edit-article-done
9507                ,(or (mail-header-references gnus-current-headers) "")
9508                ,(gnus-group-read-only-p)
9509                ,gnus-summary-buffer no-highlight))))))))
9510
9511 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9512
9513 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9514                                                  no-highlight)
9515   "Make edits to the current article permanent."
9516   (interactive)
9517   (save-excursion
9518    ;; The buffer restriction contains the entire article if it exists.
9519     (when (article-goto-body)
9520       (let ((lines (count-lines (point) (point-max)))
9521             (length (- (point-max) (point)))
9522             (case-fold-search t)
9523             (body (copy-marker (point))))
9524         (goto-char (point-min))
9525         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9526           (delete-region (match-beginning 1) (match-end 1))
9527           (insert (number-to-string length)))
9528         (goto-char (point-min))
9529         (when (re-search-forward
9530                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9531           (delete-region (match-beginning 1) (match-end 1))
9532           (insert (number-to-string length)))
9533         (goto-char (point-min))
9534         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9535           (delete-region (match-beginning 1) (match-end 1))
9536           (insert (number-to-string lines))))))
9537   ;; Replace the article.
9538   (let ((buf (current-buffer)))
9539     (with-temp-buffer
9540       (insert-buffer-substring buf)
9541
9542       (if (and (not read-only)
9543                (not (gnus-request-replace-article
9544                      (cdr gnus-article-current) (car gnus-article-current)
9545                      (current-buffer) t)))
9546           (error "Couldn't replace article")
9547         ;; Update the summary buffer.
9548         (if (and references
9549                  (equal (message-tokenize-header references " ")
9550                         (message-tokenize-header
9551                          (or (message-fetch-field "references") "") " ")))
9552             ;; We only have to update this line.
9553             (save-excursion
9554               (save-restriction
9555                 (message-narrow-to-head)
9556                 (let ((head (buffer-string))
9557                       header)
9558                   (with-temp-buffer
9559                     (insert (format "211 %d Article retrieved.\n"
9560                                     (cdr gnus-article-current)))
9561                     (insert head)
9562                     (insert ".\n")
9563                     (let ((nntp-server-buffer (current-buffer)))
9564                       (setq header (car (gnus-get-newsgroup-headers
9565                                          nil t))))
9566                     (save-excursion
9567                       (set-buffer gnus-summary-buffer)
9568                       (gnus-data-set-header
9569                        (gnus-data-find (cdr gnus-article-current))
9570                        header)
9571                       (gnus-summary-update-article-line
9572                        (cdr gnus-article-current) header)
9573                       (if (gnus-summary-goto-subject
9574                            (cdr gnus-article-current) nil t)
9575                           (gnus-summary-update-secondary-mark
9576                            (cdr gnus-article-current))))))))
9577           ;; Update threads.
9578           (set-buffer (or buffer gnus-summary-buffer))
9579           (gnus-summary-update-article (cdr gnus-article-current))
9580           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9581               (gnus-summary-update-secondary-mark
9582                (cdr gnus-article-current))))
9583         ;; Prettify the article buffer again.
9584         (unless no-highlight
9585           (save-excursion
9586             (set-buffer gnus-article-buffer)
9587             ;;;!!! Fix this -- article should be rehighlighted.
9588             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9589             (set-buffer gnus-original-article-buffer)
9590             (gnus-request-article
9591              (cdr gnus-article-current)
9592              (car gnus-article-current) (current-buffer))))
9593         ;; Prettify the summary buffer line.
9594         (when (gnus-visual-p 'summary-highlight 'highlight)
9595           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9596
9597 (defun gnus-summary-edit-wash (key)
9598   "Perform editing command KEY in the article buffer."
9599   (interactive
9600    (list
9601     (progn
9602       (message "%s" (concat (this-command-keys) "- "))
9603       (read-char))))
9604   (message "")
9605   (gnus-summary-edit-article)
9606   (execute-kbd-macro (concat (this-command-keys) key))
9607   (gnus-article-edit-done))
9608
9609 ;;; Respooling
9610
9611 (defun gnus-summary-respool-query (&optional silent trace)
9612   "Query where the respool algorithm would put this article."
9613   (interactive)
9614   (let (gnus-mark-article-hook)
9615     (gnus-summary-select-article)
9616     (save-excursion
9617       (set-buffer gnus-original-article-buffer)
9618       (let ((groups (nnmail-article-group 'identity trace)))
9619         (unless silent
9620           (if groups
9621               (message "This message would go to %s"
9622                        (mapconcat 'car groups ", "))
9623             (message "This message would go to no groups"))
9624           groups)))))
9625
9626 (defun gnus-summary-respool-trace ()
9627   "Trace where the respool algorithm would put this article.
9628 Display a buffer showing all fancy splitting patterns which matched."
9629   (interactive)
9630   (gnus-summary-respool-query nil t))
9631
9632 ;; Summary marking commands.
9633
9634 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9635   "Mark articles which has the same subject as read, and then select the next.
9636 If UNMARK is positive, remove any kind of mark.
9637 If UNMARK is negative, tick articles."
9638   (interactive "P")
9639   (when unmark
9640     (setq unmark (prefix-numeric-value unmark)))
9641   (let ((count
9642          (gnus-summary-mark-same-subject
9643           (gnus-summary-article-subject) unmark)))
9644     ;; Select next unread article.  If auto-select-same mode, should
9645     ;; select the first unread article.
9646     (gnus-summary-next-article t (and gnus-auto-select-same
9647                                       (gnus-summary-article-subject)))
9648     (gnus-message 7 "%d article%s marked as %s"
9649                   count (if (= count 1) " is" "s are")
9650                   (if unmark "unread" "read"))))
9651
9652 (defun gnus-summary-kill-same-subject (&optional unmark)
9653   "Mark articles which has the same subject as read.
9654 If UNMARK is positive, remove any kind of mark.
9655 If UNMARK is negative, tick articles."
9656   (interactive "P")
9657   (when unmark
9658     (setq unmark (prefix-numeric-value unmark)))
9659   (let ((count
9660          (gnus-summary-mark-same-subject
9661           (gnus-summary-article-subject) unmark)))
9662     ;; If marked as read, go to next unread subject.
9663     (when (null unmark)
9664       ;; Go to next unread subject.
9665       (gnus-summary-next-subject 1 t))
9666     (gnus-message 7 "%d articles are marked as %s"
9667                   count (if unmark "unread" "read"))))
9668
9669 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9670   "Mark articles with same SUBJECT as read, and return marked number.
9671 If optional argument UNMARK is positive, remove any kinds of marks.
9672 If optional argument UNMARK is negative, mark articles as unread instead."
9673   (let ((count 1))
9674     (save-excursion
9675       (cond
9676        ((null unmark)                   ; Mark as read.
9677         (while (and
9678                 (progn
9679                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9680                   (gnus-summary-show-thread) t)
9681                 (gnus-summary-find-subject subject))
9682           (setq count (1+ count))))
9683        ((> unmark 0)                    ; Tick.
9684         (while (and
9685                 (progn
9686                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9687                   (gnus-summary-show-thread) t)
9688                 (gnus-summary-find-subject subject))
9689           (setq count (1+ count))))
9690        (t                               ; Mark as unread.
9691         (while (and
9692                 (progn
9693                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9694                   (gnus-summary-show-thread) t)
9695                 (gnus-summary-find-subject subject))
9696           (setq count (1+ count)))))
9697       (gnus-set-mode-line 'summary)
9698       ;; Return the number of marked articles.
9699       count)))
9700
9701 (defun gnus-summary-mark-as-processable (n &optional unmark)
9702   "Set the process mark on the next N articles.
9703 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9704 the process mark instead.  The difference between N and the actual
9705 number of articles marked is returned."
9706   (interactive "P")
9707   (if (and (null n) (gnus-region-active-p))
9708       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9709     (setq n (prefix-numeric-value n))
9710     (let ((backward (< n 0))
9711           (n (abs n)))
9712       (while (and
9713               (> n 0)
9714               (if unmark
9715                   (gnus-summary-remove-process-mark
9716                    (gnus-summary-article-number))
9717                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9718               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9719         (setq n (1- n)))
9720       (when (/= 0 n)
9721         (gnus-message 7 "No more articles"))
9722       (gnus-summary-recenter)
9723       (gnus-summary-position-point)
9724       n)))
9725
9726 (defun gnus-summary-unmark-as-processable (n)
9727   "Remove the process mark from the next N articles.
9728 If N is negative, unmark backward instead.  The difference between N and
9729 the actual number of articles unmarked is returned."
9730   (interactive "P")
9731   (gnus-summary-mark-as-processable n t))
9732
9733 (defun gnus-summary-unmark-all-processable ()
9734   "Remove the process mark from all articles."
9735   (interactive)
9736   (save-excursion
9737     (while gnus-newsgroup-processable
9738       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9739   (gnus-summary-position-point))
9740
9741 (defun gnus-summary-add-mark (article type)
9742   "Mark ARTICLE with a mark of TYPE."
9743   (let ((vtype (car (assq type gnus-article-mark-lists)))
9744         var)
9745     (if (not vtype)
9746         (error "No such mark type: %s" type)
9747       (setq var (intern (format "gnus-newsgroup-%s" type)))
9748       (set var (cons article (symbol-value var)))
9749       (if (memq type '(processable cached replied forwarded recent saved))
9750           (gnus-summary-update-secondary-mark article)
9751         ;;; !!! This is bogus.  We should find out what primary
9752         ;;; !!! mark we want to set.
9753         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9754
9755 (defun gnus-summary-mark-as-expirable (n)
9756   "Mark N articles forward as expirable.
9757 If N is negative, mark backward instead.  The difference between N and
9758 the actual number of articles marked is returned."
9759   (interactive "p")
9760   (gnus-summary-mark-forward n gnus-expirable-mark))
9761
9762 (defun gnus-summary-mark-as-spam (n)
9763   "Mark N articles forward as spam.
9764 If N is negative, mark backward instead.  The difference between N and
9765 the actual number of articles marked is returned."
9766   (interactive "p")
9767   (gnus-summary-mark-forward n gnus-spam-mark))
9768
9769 (defun gnus-summary-mark-article-as-replied (article)
9770   "Mark ARTICLE as replied to and update the summary line.
9771 ARTICLE can also be a list of articles."
9772   (interactive (list (gnus-summary-article-number)))
9773   (let ((articles (if (listp article) article (list article))))
9774     (dolist (article articles)
9775       (unless (numberp article)
9776         (error "%s is not a number" article))
9777       (push article gnus-newsgroup-replied)
9778       (let ((buffer-read-only nil))
9779         (when (gnus-summary-goto-subject article nil t)
9780           (gnus-summary-update-secondary-mark article))))))
9781
9782 (defun gnus-summary-mark-article-as-forwarded (article)
9783   "Mark ARTICLE as forwarded and update the summary line.
9784 ARTICLE can also be a list of articles."
9785   (let ((articles (if (listp article) article (list article))))
9786     (dolist (article articles)
9787       (push article gnus-newsgroup-forwarded)
9788       (let ((buffer-read-only nil))
9789         (when (gnus-summary-goto-subject article nil t)
9790           (gnus-summary-update-secondary-mark article))))))
9791
9792 (defun gnus-summary-set-bookmark (article)
9793   "Set a bookmark in current article."
9794   (interactive (list (gnus-summary-article-number)))
9795   (when (or (not (get-buffer gnus-article-buffer))
9796             (not gnus-current-article)
9797             (not gnus-article-current)
9798             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9799     (error "No current article selected"))
9800   ;; Remove old bookmark, if one exists.
9801   (gnus-pull article gnus-newsgroup-bookmarks)
9802   ;; Set the new bookmark, which is on the form
9803   ;; (article-number . line-number-in-body).
9804   (push
9805    (cons article
9806          (with-current-buffer gnus-article-buffer
9807            (count-lines
9808             (min (point)
9809                  (save-excursion
9810                    (article-goto-body)
9811                    (point)))
9812             (point))))
9813    gnus-newsgroup-bookmarks)
9814   (gnus-message 6 "A bookmark has been added to the current article."))
9815
9816 (defun gnus-summary-remove-bookmark (article)
9817   "Remove the bookmark from the current article."
9818   (interactive (list (gnus-summary-article-number)))
9819   ;; Remove old bookmark, if one exists.
9820   (if (not (assq article gnus-newsgroup-bookmarks))
9821       (gnus-message 6 "No bookmark in current article.")
9822     (gnus-pull article gnus-newsgroup-bookmarks)
9823     (gnus-message 6 "Removed bookmark.")))
9824
9825 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9826 (defun gnus-summary-mark-as-dormant (n)
9827   "Mark N articles forward as dormant.
9828 If N is negative, mark backward instead.  The difference between N and
9829 the actual number of articles marked is returned."
9830   (interactive "p")
9831   (gnus-summary-mark-forward n gnus-dormant-mark))
9832
9833 (defun gnus-summary-set-process-mark (article)
9834   "Set the process mark on ARTICLE and update the summary line."
9835   (setq gnus-newsgroup-processable
9836         (cons article
9837               (delq article gnus-newsgroup-processable)))
9838   (when (gnus-summary-goto-subject article)
9839     (gnus-summary-show-thread)
9840     (gnus-summary-goto-subject article)
9841     (gnus-summary-update-secondary-mark article)))
9842
9843 (defun gnus-summary-remove-process-mark (article)
9844   "Remove the process mark from ARTICLE and update the summary line."
9845   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9846   (when (gnus-summary-goto-subject article)
9847     (gnus-summary-show-thread)
9848     (gnus-summary-goto-subject article)
9849     (gnus-summary-update-secondary-mark article)))
9850
9851 (defun gnus-summary-set-saved-mark (article)
9852   "Set the process mark on ARTICLE and update the summary line."
9853   (push article gnus-newsgroup-saved)
9854   (when (gnus-summary-goto-subject article)
9855     (gnus-summary-update-secondary-mark article)))
9856
9857 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9858   "Mark N articles as read forwards.
9859 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9860 The difference between N and the actual number of articles marked is
9861 returned.
9862 If NO-EXPIRE, auto-expiry will be inhibited."
9863   (interactive "p")
9864   (gnus-summary-show-thread)
9865   (let ((backward (< n 0))
9866         (gnus-summary-goto-unread
9867          (and gnus-summary-goto-unread
9868               (not (eq gnus-summary-goto-unread 'never))
9869               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9870                                     gnus-ticked-mark gnus-dormant-mark)))))
9871         (n (abs n))
9872         (mark (or mark gnus-del-mark)))
9873     (while (and (> n 0)
9874                 (gnus-summary-mark-article nil mark no-expire)
9875                 (zerop (gnus-summary-next-subject
9876                         (if backward -1 1)
9877                         (and gnus-summary-goto-unread
9878                              (not (eq gnus-summary-goto-unread 'never)))
9879                         t)))
9880       (setq n (1- n)))
9881     (when (/= 0 n)
9882       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9883     (gnus-summary-recenter)
9884     (gnus-summary-position-point)
9885     (gnus-set-mode-line 'summary)
9886     n))
9887
9888 (defun gnus-summary-mark-article-as-read (mark)
9889   "Mark the current article quickly as read with MARK."
9890   (let ((article (gnus-summary-article-number)))
9891     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9892     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9893     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9894     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9895     (push (cons article mark) gnus-newsgroup-reads)
9896     ;; Possibly remove from cache, if that is used.
9897     (when gnus-use-cache
9898       (gnus-cache-enter-remove-article article))
9899     ;; Allow the backend to change the mark.
9900     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9901     ;; Check for auto-expiry.
9902     (when (and gnus-newsgroup-auto-expire
9903                (memq mark gnus-auto-expirable-marks))
9904       (setq mark gnus-expirable-mark)
9905       ;; Let the backend know about the mark change.
9906       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9907       (push article gnus-newsgroup-expirable))
9908     ;; Set the mark in the buffer.
9909     (gnus-summary-update-mark mark 'unread)
9910     t))
9911
9912 (defun gnus-summary-mark-article-as-unread (mark)
9913   "Mark the current article quickly as unread with MARK."
9914   (let* ((article (gnus-summary-article-number))
9915          (old-mark (gnus-summary-article-mark article)))
9916     ;; Allow the backend to change the mark.
9917     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9918     (if (eq mark old-mark)
9919         t
9920       (if (<= article 0)
9921           (progn
9922             (gnus-error 1 "Can't mark negative article numbers")
9923             nil)
9924         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9925         (setq gnus-newsgroup-spam-marked
9926               (delq article gnus-newsgroup-spam-marked))
9927         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9928         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9929         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9930         (cond ((= mark gnus-ticked-mark)
9931                (setq gnus-newsgroup-marked
9932                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9933                                               article)))
9934               ((= mark gnus-spam-mark)
9935                (setq gnus-newsgroup-spam-marked
9936                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9937                                               article)))
9938               ((= mark gnus-dormant-mark)
9939                (setq gnus-newsgroup-dormant
9940                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9941                                               article)))
9942               (t
9943                (setq gnus-newsgroup-unreads
9944                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9945                                               article))))
9946         (gnus-pull article gnus-newsgroup-reads)
9947
9948         ;; See whether the article is to be put in the cache.
9949         (and gnus-use-cache
9950              (vectorp (gnus-summary-article-header article))
9951              (save-excursion
9952                (gnus-cache-possibly-enter-article
9953                 gnus-newsgroup-name article
9954                 (= mark gnus-ticked-mark)
9955                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9956
9957         ;; Fix the mark.
9958         (gnus-summary-update-mark mark 'unread)
9959         t))))
9960
9961 (defun gnus-summary-mark-article (&optional article mark no-expire)
9962   "Mark ARTICLE with MARK.  MARK can be any character.
9963 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9964 `??' (dormant) and `?E' (expirable).
9965 If MARK is nil, then the default character `?r' is used.
9966 If ARTICLE is nil, then the article on the current line will be
9967 marked.
9968 If NO-EXPIRE, auto-expiry will be inhibited."
9969   ;; The mark might be a string.
9970   (when (stringp mark)
9971     (setq mark (aref mark 0)))
9972   ;; If no mark is given, then we check auto-expiring.
9973   (when (null mark)
9974     (setq mark gnus-del-mark))
9975   (when (and (not no-expire)
9976              gnus-newsgroup-auto-expire
9977              (memq mark gnus-auto-expirable-marks))
9978     (setq mark gnus-expirable-mark))
9979   (let ((article (or article (gnus-summary-article-number)))
9980         (old-mark (gnus-summary-article-mark article)))
9981     ;; Allow the backend to change the mark.
9982     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9983     (if (eq mark old-mark)
9984         t
9985       (unless article
9986         (error "No article on current line"))
9987       (if (not (if (or (= mark gnus-unread-mark)
9988                        (= mark gnus-ticked-mark)
9989                        (= mark gnus-spam-mark)
9990                        (= mark gnus-dormant-mark))
9991                    (gnus-mark-article-as-unread article mark)
9992                  (gnus-mark-article-as-read article mark)))
9993           t
9994         ;; See whether the article is to be put in the cache.
9995         (and gnus-use-cache
9996              (not (= mark gnus-canceled-mark))
9997              (vectorp (gnus-summary-article-header article))
9998              (save-excursion
9999                (gnus-cache-possibly-enter-article
10000                 gnus-newsgroup-name article
10001                 (= mark gnus-ticked-mark)
10002                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10003
10004         (when (gnus-summary-goto-subject article nil t)
10005           (let ((buffer-read-only nil))
10006             (gnus-summary-show-thread)
10007             ;; Fix the mark.
10008             (gnus-summary-update-mark mark 'unread)
10009             t))))))
10010
10011 (defun gnus-summary-update-secondary-mark (article)
10012   "Update the secondary (read, process, cache) mark."
10013   (gnus-summary-update-mark
10014    (cond ((memq article gnus-newsgroup-processable)
10015           gnus-process-mark)
10016          ((memq article gnus-newsgroup-cached)
10017           gnus-cached-mark)
10018          ((memq article gnus-newsgroup-replied)
10019           gnus-replied-mark)
10020          ((memq article gnus-newsgroup-forwarded)
10021           gnus-forwarded-mark)
10022          ((memq article gnus-newsgroup-saved)
10023           gnus-saved-mark)
10024          ((memq article gnus-newsgroup-recent)
10025           gnus-recent-mark)
10026          ((memq article gnus-newsgroup-unseen)
10027           gnus-unseen-mark)
10028          (t gnus-no-mark))
10029    'replied)
10030   (when (gnus-visual-p 'summary-highlight 'highlight)
10031     (gnus-run-hooks 'gnus-summary-update-hook))
10032   t)
10033
10034 (defun gnus-summary-update-download-mark (article)
10035   "Update the download mark."
10036   (gnus-summary-update-mark
10037    (cond ((memq article gnus-newsgroup-undownloaded)
10038           gnus-undownloaded-mark)
10039          (gnus-newsgroup-agentized
10040           gnus-downloaded-mark)
10041          (t
10042           gnus-no-mark))
10043    'download)
10044   (gnus-summary-update-line t)
10045   t)
10046
10047 (defun gnus-summary-update-mark (mark type)
10048   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10049         (buffer-read-only nil))
10050     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10051     (when forward
10052       (when (looking-at "\r")
10053         (incf forward))
10054       (when (<= (+ forward (point)) (point-max))
10055         ;; Go to the right position on the line.
10056         (goto-char (+ forward (point)))
10057         ;; Replace the old mark with the new mark.
10058         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10059         ;; Optionally update the marks by some user rule.
10060         (when (eq type 'unread)
10061           (gnus-data-set-mark
10062            (gnus-data-find (gnus-summary-article-number)) mark)
10063           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10064
10065 (defun gnus-mark-article-as-read (article &optional mark)
10066   "Enter ARTICLE in the pertinent lists and remove it from others."
10067   ;; Make the article expirable.
10068   (let ((mark (or mark gnus-del-mark)))
10069     (setq gnus-newsgroup-expirable
10070           (if (= mark gnus-expirable-mark)
10071               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10072             (delq article gnus-newsgroup-expirable)))
10073     ;; Remove from unread and marked lists.
10074     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10075     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10076     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10077     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10078     (push (cons article mark) gnus-newsgroup-reads)
10079     ;; Possibly remove from cache, if that is used.
10080     (when gnus-use-cache
10081       (gnus-cache-enter-remove-article article))
10082     t))
10083
10084 (defun gnus-mark-article-as-unread (article &optional mark)
10085   "Enter ARTICLE in the pertinent lists and remove it from others."
10086   (let ((mark (or mark gnus-ticked-mark)))
10087     (if (<= article 0)
10088         (progn
10089           (gnus-error 1 "Can't mark negative article numbers")
10090           nil)
10091       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10092             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10093             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10094             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10095             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10096
10097       ;; Unsuppress duplicates?
10098       (when gnus-suppress-duplicates
10099         (gnus-dup-unsuppress-article article))
10100
10101       (cond ((= mark gnus-ticked-mark)
10102              (setq gnus-newsgroup-marked
10103                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10104             ((= mark gnus-spam-mark)
10105              (setq gnus-newsgroup-spam-marked
10106                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10107                                             article)))
10108             ((= mark gnus-dormant-mark)
10109              (setq gnus-newsgroup-dormant
10110                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10111             (t
10112              (setq gnus-newsgroup-unreads
10113                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10114       (gnus-pull article gnus-newsgroup-reads)
10115       t)))
10116
10117 (defalias 'gnus-summary-mark-as-unread-forward
10118   'gnus-summary-tick-article-forward)
10119 (make-obsolete 'gnus-summary-mark-as-unread-forward
10120                'gnus-summary-tick-article-forward)
10121 (defun gnus-summary-tick-article-forward (n)
10122   "Tick N articles forwards.
10123 If N is negative, tick backwards instead.
10124 The difference between N and the number of articles ticked is returned."
10125   (interactive "p")
10126   (gnus-summary-mark-forward n gnus-ticked-mark))
10127
10128 (defalias 'gnus-summary-mark-as-unread-backward
10129   'gnus-summary-tick-article-backward)
10130 (make-obsolete 'gnus-summary-mark-as-unread-backward
10131                'gnus-summary-tick-article-backward)
10132 (defun gnus-summary-tick-article-backward (n)
10133   "Tick N articles backwards.
10134 The difference between N and the number of articles ticked is returned."
10135   (interactive "p")
10136   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10137
10138 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10139 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10140 (defun gnus-summary-tick-article (&optional article clear-mark)
10141   "Mark current article as unread.
10142 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10143 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10144   (interactive)
10145   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10146                                        gnus-ticked-mark)))
10147
10148 (defun gnus-summary-mark-as-read-forward (n)
10149   "Mark N articles as read forwards.
10150 If N is negative, mark backwards instead.
10151 The difference between N and the actual number of articles marked is
10152 returned."
10153   (interactive "p")
10154   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10155
10156 (defun gnus-summary-mark-as-read-backward (n)
10157   "Mark the N articles as read backwards.
10158 The difference between N and the actual number of articles marked is
10159 returned."
10160   (interactive "p")
10161   (gnus-summary-mark-forward
10162    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10163
10164 (defun gnus-summary-mark-as-read (&optional article mark)
10165   "Mark current article as read.
10166 ARTICLE specifies the article to be marked as read.
10167 MARK specifies a string to be inserted at the beginning of the line."
10168   (gnus-summary-mark-article article mark))
10169
10170 (defun gnus-summary-clear-mark-forward (n)
10171   "Clear marks from N articles forward.
10172 If N is negative, clear backward instead.
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-clear-mark-backward (n)
10178   "Clear marks from N articles backward.
10179 The difference between N and the number of marks cleared is returned."
10180   (interactive "p")
10181   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10182
10183 (defun gnus-summary-mark-unread-as-read ()
10184   "Intended to be used by `gnus-summary-mark-article-hook'."
10185   (when (memq gnus-current-article gnus-newsgroup-unreads)
10186     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10187
10188 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10189   "Intended to be used by `gnus-summary-mark-article-hook'."
10190   (let ((mark (gnus-summary-article-mark)))
10191     (when (or (gnus-unread-mark-p mark)
10192               (gnus-read-mark-p mark))
10193       (gnus-summary-mark-article gnus-current-article
10194                                  (or new-mark gnus-read-mark)))))
10195
10196 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10197   "Intended to be used by `gnus-summary-mark-article-hook'."
10198   (let ((mark (gnus-summary-article-mark)))
10199     (when (or (gnus-unread-mark-p mark)
10200               (gnus-read-mark-p mark))
10201       (gnus-summary-mark-article (gnus-summary-article-number)
10202                                  (or new-mark gnus-read-mark)))))
10203
10204 (defun gnus-summary-mark-unread-as-ticked ()
10205   "Intended to be used by `gnus-summary-mark-article-hook'."
10206   (when (memq gnus-current-article gnus-newsgroup-unreads)
10207     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10208
10209 (defun gnus-summary-mark-region-as-read (point mark all)
10210   "Mark all unread articles between point and mark as read.
10211 If given a prefix, mark all articles between point and mark as read,
10212 even ticked and dormant ones."
10213   (interactive "r\nP")
10214   (save-excursion
10215     (let (article)
10216       (goto-char point)
10217       (beginning-of-line)
10218       (while (and
10219               (< (point) mark)
10220               (progn
10221                 (when (or all
10222                           (memq (setq article (gnus-summary-article-number))
10223                                 gnus-newsgroup-unreads))
10224                   (gnus-summary-mark-article article gnus-del-mark))
10225                 t)
10226               (gnus-summary-find-next))))))
10227
10228 (defun gnus-summary-mark-below (score mark)
10229   "Mark articles with score less than SCORE with MARK."
10230   (interactive "P\ncMark: ")
10231   (setq score (if score
10232                   (prefix-numeric-value score)
10233                 (or gnus-summary-default-score 0)))
10234   (save-excursion
10235     (set-buffer gnus-summary-buffer)
10236     (goto-char (point-min))
10237     (while
10238         (progn
10239           (and (< (gnus-summary-article-score) score)
10240                (gnus-summary-mark-article nil mark))
10241           (gnus-summary-find-next)))))
10242
10243 (defun gnus-summary-kill-below (&optional score)
10244   "Mark articles with score below SCORE as read."
10245   (interactive "P")
10246   (gnus-summary-mark-below score gnus-killed-mark))
10247
10248 (defun gnus-summary-clear-above (&optional score)
10249   "Clear all marks from articles with score above SCORE."
10250   (interactive "P")
10251   (gnus-summary-mark-above score gnus-unread-mark))
10252
10253 (defun gnus-summary-tick-above (&optional score)
10254   "Tick all articles with score above SCORE."
10255   (interactive "P")
10256   (gnus-summary-mark-above score gnus-ticked-mark))
10257
10258 (defun gnus-summary-mark-above (score mark)
10259   "Mark articles with score over SCORE with MARK."
10260   (interactive "P\ncMark: ")
10261   (setq score (if score
10262                   (prefix-numeric-value score)
10263                 (or gnus-summary-default-score 0)))
10264   (save-excursion
10265     (set-buffer gnus-summary-buffer)
10266     (goto-char (point-min))
10267     (while (and (progn
10268                   (when (> (gnus-summary-article-score) score)
10269                     (gnus-summary-mark-article nil mark))
10270                   t)
10271                 (gnus-summary-find-next)))))
10272
10273 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10274 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10275 (defun gnus-summary-limit-include-expunged (&optional no-error)
10276   "Display all the hidden articles that were expunged for low scores."
10277   (interactive)
10278   (let ((buffer-read-only nil))
10279     (let ((scored gnus-newsgroup-scored)
10280           headers h)
10281       (while scored
10282         (unless (gnus-summary-article-header (caar scored))
10283           (and (setq h (gnus-number-to-header (caar scored)))
10284                (< (cdar scored) gnus-summary-expunge-below)
10285                (push h headers)))
10286         (setq scored (cdr scored)))
10287       (if (not headers)
10288           (when (not no-error)
10289             (error "No expunged articles hidden"))
10290         (goto-char (point-min))
10291         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10292         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10293         (mapcar (lambda (x) (push (mail-header-number x)
10294                                   gnus-newsgroup-limit))
10295                 headers)
10296         (gnus-summary-prepare-unthreaded (nreverse headers))
10297         (goto-char (point-min))
10298         (gnus-summary-position-point)
10299         t))))
10300
10301 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10302   "Mark all unread articles in this newsgroup as read.
10303 If prefix argument ALL is non-nil, ticked and dormant articles will
10304 also be marked as read.
10305 If QUIETLY is non-nil, no questions will be asked.
10306
10307 If TO-HERE is non-nil, it should be a point in the buffer.  All
10308 articles before (after, if REVERSE is set) this point will be marked
10309 as read.
10310
10311 Note that this function will only catch up the unread article
10312 in the current summary buffer limitation.
10313
10314 The number of articles marked as read is returned."
10315   (interactive "P")
10316   (prog1
10317       (save-excursion
10318         (when (or quietly
10319                   (not gnus-interactive-catchup) ;Without confirmation?
10320                   gnus-expert-user
10321                   (gnus-y-or-n-p
10322                    (if all
10323                        "Mark absolutely all articles as read? "
10324                      "Mark all unread articles as read? ")))
10325           (if (and not-mark
10326                    (not gnus-newsgroup-adaptive)
10327                    (not gnus-newsgroup-auto-expire)
10328                    (not gnus-suppress-duplicates)
10329                    (or (not gnus-use-cache)
10330                        (eq gnus-use-cache 'passive)))
10331               (progn
10332                 (when all
10333                   (setq gnus-newsgroup-marked nil
10334                         gnus-newsgroup-spam-marked nil
10335                         gnus-newsgroup-dormant nil))
10336                 (setq gnus-newsgroup-unreads
10337                       (gnus-sorted-nunion
10338                        (gnus-intersection gnus-newsgroup-unreads
10339                                           gnus-newsgroup-downloadable)
10340                        gnus-newsgroup-unfetched)))
10341             ;; We actually mark all articles as canceled, which we
10342             ;; have to do when using auto-expiry or adaptive scoring.
10343             (gnus-summary-show-all-threads)
10344             (if (and to-here reverse)
10345                 (progn
10346                   (goto-char to-here)
10347                   (gnus-summary-mark-current-read-and-unread-as-read
10348                    gnus-catchup-mark)
10349                   (while (gnus-summary-find-next (not all))
10350                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10351               (when (gnus-summary-first-subject (not all))
10352                 (while (and
10353                         (if to-here (< (point) to-here) t)
10354                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10355                         (gnus-summary-find-next (not all))))))
10356             (gnus-set-mode-line 'summary))
10357           t))
10358     (gnus-summary-position-point)))
10359
10360 (defun gnus-summary-catchup-to-here (&optional all)
10361   "Mark all unticked articles before the current one as read.
10362 If ALL is non-nil, also mark ticked and dormant articles as read."
10363   (interactive "P")
10364   (save-excursion
10365     (gnus-save-hidden-threads
10366       (let ((beg (point)))
10367         ;; We check that there are unread articles.
10368         (when (or all (gnus-summary-find-prev))
10369           (gnus-summary-catchup all t beg)))))
10370   (gnus-summary-position-point))
10371
10372 (defun gnus-summary-catchup-from-here (&optional all)
10373   "Mark all unticked articles after (and including) the current one as read.
10374 If ALL is non-nil, also mark ticked and dormant articles as read."
10375   (interactive "P")
10376   (save-excursion
10377     (gnus-save-hidden-threads
10378       (let ((beg (point)))
10379         ;; We check that there are unread articles.
10380         (when (or all (gnus-summary-find-next))
10381           (gnus-summary-catchup all t beg nil t)))))
10382   (gnus-summary-position-point))
10383
10384 (defun gnus-summary-catchup-all (&optional quietly)
10385   "Mark all articles in this newsgroup as read.
10386 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10387 instead, which marks only unread articles as read."
10388   (interactive "P")
10389   (gnus-summary-catchup t quietly))
10390
10391 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10392   "Mark all unread articles in this group as read, then exit.
10393 If prefix argument ALL is non-nil, all articles are marked as read.
10394 If QUIETLY is non-nil, no questions will be asked."
10395   (interactive "P")
10396   (when (gnus-summary-catchup all quietly nil 'fast)
10397     ;; Select next newsgroup or exit.
10398     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10399              (eq gnus-auto-select-next 'quietly))
10400         (gnus-summary-next-group nil)
10401       (gnus-summary-exit))))
10402
10403 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10404   "Mark all articles in this newsgroup as read, and then exit.
10405 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10406 instead, which marks only unread articles as read."
10407   (interactive "P")
10408   (gnus-summary-catchup-and-exit t quietly))
10409
10410 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10411   "Mark all articles in this group as read and select the next group.
10412 If given a prefix, mark all articles, unread as well as ticked, as
10413 read."
10414   (interactive "P")
10415   (save-excursion
10416     (gnus-summary-catchup all))
10417   (gnus-summary-next-group))
10418
10419 ;;;
10420 ;;; with article
10421 ;;;
10422
10423 (defmacro gnus-with-article (article &rest forms)
10424   "Select ARTICLE and perform FORMS in the original article buffer.
10425 Then replace the article with the result."
10426   `(progn
10427      ;; We don't want the article to be marked as read.
10428      (let (gnus-mark-article-hook)
10429        (gnus-summary-select-article t t nil ,article))
10430      (set-buffer gnus-original-article-buffer)
10431      ,@forms
10432      (if (not (gnus-check-backend-function
10433                'request-replace-article (car gnus-article-current)))
10434          (gnus-message 5 "Read-only group; not replacing")
10435        (unless (gnus-request-replace-article
10436                 ,article (car gnus-article-current)
10437                 (current-buffer) t)
10438          (error "Couldn't replace article")))
10439      ;; The cache and backlog have to be flushed somewhat.
10440      (when gnus-keep-backlog
10441        (gnus-backlog-remove-article
10442         (car gnus-article-current) (cdr gnus-article-current)))
10443      (when gnus-use-cache
10444        (gnus-cache-update-article
10445         (car gnus-article-current) (cdr gnus-article-current)))))
10446
10447 (put 'gnus-with-article 'lisp-indent-function 1)
10448 (put 'gnus-with-article 'edebug-form-spec '(form body))
10449
10450 ;; Thread-based commands.
10451
10452 (defun gnus-summary-articles-in-thread (&optional article)
10453   "Return a list of all articles in the current thread.
10454 If ARTICLE is non-nil, return all articles in the thread that starts
10455 with that article."
10456   (let* ((article (or article (gnus-summary-article-number)))
10457          (data (gnus-data-find-list article))
10458          (top-level (gnus-data-level (car data)))
10459          (top-subject
10460           (cond ((null gnus-thread-operation-ignore-subject)
10461                  (gnus-simplify-subject-re
10462                   (mail-header-subject (gnus-data-header (car data)))))
10463                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10464                  (gnus-simplify-subject-fuzzy
10465                   (mail-header-subject (gnus-data-header (car data)))))
10466                 (t nil)))
10467          (end-point (save-excursion
10468                       (if (gnus-summary-go-to-next-thread)
10469                           (point) (point-max))))
10470          articles)
10471     (while (and data
10472                 (< (gnus-data-pos (car data)) end-point))
10473       (when (or (not top-subject)
10474                 (string= top-subject
10475                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10476                              (gnus-simplify-subject-fuzzy
10477                               (mail-header-subject
10478                                (gnus-data-header (car data))))
10479                            (gnus-simplify-subject-re
10480                             (mail-header-subject
10481                              (gnus-data-header (car data)))))))
10482         (push (gnus-data-number (car data)) articles))
10483       (unless (and (setq data (cdr data))
10484                    (> (gnus-data-level (car data)) top-level))
10485         (setq data nil)))
10486     ;; Return the list of articles.
10487     (nreverse articles)))
10488
10489 (defun gnus-summary-rethread-current ()
10490   "Rethread the thread the current article is part of."
10491   (interactive)
10492   (let* ((gnus-show-threads t)
10493          (article (gnus-summary-article-number))
10494          (id (mail-header-id (gnus-summary-article-header)))
10495          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10496     (unless id
10497       (error "No article on the current line"))
10498     (gnus-rebuild-thread id)
10499     (gnus-summary-goto-subject article)))
10500
10501 (defun gnus-summary-reparent-thread ()
10502   "Make the current article child of the marked (or previous) article.
10503
10504 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10505 is non-nil or the Subject: of both articles are the same."
10506   (interactive)
10507   (unless (not (gnus-group-read-only-p))
10508     (error "The current newsgroup does not support article editing"))
10509   (unless (<= (length gnus-newsgroup-processable) 1)
10510     (error "No more than one article may be marked"))
10511   (save-window-excursion
10512     (let ((gnus-article-buffer " *reparent*")
10513           (current-article (gnus-summary-article-number))
10514           ;; First grab the marked article, otherwise one line up.
10515           (parent-article (if (not (null gnus-newsgroup-processable))
10516                               (car gnus-newsgroup-processable)
10517                             (save-excursion
10518                               (if (eq (forward-line -1) 0)
10519                                   (gnus-summary-article-number)
10520                                 (error "Beginning of summary buffer"))))))
10521       (unless (not (eq current-article parent-article))
10522         (error "An article may not be self-referential"))
10523       (let ((message-id (mail-header-id
10524                          (gnus-summary-article-header parent-article))))
10525         (unless (and message-id (not (equal message-id "")))
10526           (error "No message-id in desired parent"))
10527         (gnus-with-article current-article
10528           (save-restriction
10529             (goto-char (point-min))
10530             (message-narrow-to-head)
10531             (if (re-search-forward "^References: " nil t)
10532                 (progn
10533                   (re-search-forward "^[^ \t]" nil t)
10534                   (forward-line -1)
10535                   (end-of-line)
10536                   (insert " " message-id))
10537               (insert "References: " message-id "\n"))))
10538         (set-buffer gnus-summary-buffer)
10539         (gnus-summary-unmark-all-processable)
10540         (gnus-summary-update-article current-article)
10541         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10542             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10543         (gnus-summary-rethread-current)
10544         (gnus-message 3 "Article %d is now the child of article %d"
10545                       current-article parent-article)))))
10546
10547 (defun gnus-summary-toggle-threads (&optional arg)
10548   "Toggle showing conversation threads.
10549 If ARG is positive number, turn showing conversation threads on."
10550   (interactive "P")
10551   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10552     (setq gnus-show-threads
10553           (if (null arg) (not gnus-show-threads)
10554             (> (prefix-numeric-value arg) 0)))
10555     (gnus-summary-prepare)
10556     (gnus-summary-goto-subject current)
10557     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10558     (gnus-summary-position-point)))
10559
10560 (defun gnus-summary-show-all-threads ()
10561   "Show all threads."
10562   (interactive)
10563   (save-excursion
10564     (let ((buffer-read-only nil))
10565       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10566   (gnus-summary-position-point))
10567
10568 (defun gnus-summary-show-thread ()
10569   "Show thread subtrees.
10570 Returns nil if no thread was there to be shown."
10571   (interactive)
10572   (let ((buffer-read-only nil)
10573         (orig (point))
10574         (end (point-at-eol))
10575         ;; Leave point at bol
10576         (beg (progn (beginning-of-line) (point))))
10577     (prog1
10578         ;; Any hidden lines here?
10579         (search-forward "\r" end t)
10580       (subst-char-in-region beg end ?\^M ?\n t)
10581       (goto-char orig)
10582       (gnus-summary-position-point))))
10583
10584 (defun gnus-summary-maybe-hide-threads ()
10585   "If requested, hide the threads that should be hidden."
10586   (when (and gnus-show-threads
10587              gnus-thread-hide-subtree)
10588     (gnus-summary-hide-all-threads
10589      (if (or (consp gnus-thread-hide-subtree)
10590              (functionp gnus-thread-hide-subtree))
10591          (gnus-make-predicate gnus-thread-hide-subtree)
10592        nil))))
10593
10594 ;;; Hiding predicates.
10595
10596 (defun gnus-article-unread-p (header)
10597   (memq (mail-header-number header) gnus-newsgroup-unreads))
10598
10599 (defun gnus-article-unseen-p (header)
10600   (memq (mail-header-number header) gnus-newsgroup-unseen))
10601
10602 (defun gnus-map-articles (predicate articles)
10603   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10604   (apply 'gnus-or (mapcar predicate
10605                           (mapcar 'gnus-summary-article-header articles))))
10606
10607 (defun gnus-summary-hide-all-threads (&optional predicate)
10608   "Hide all thread subtrees.
10609 If PREDICATE is supplied, threads that satisfy this predicate
10610 will not be hidden."
10611   (interactive)
10612   (save-excursion
10613     (goto-char (point-min))
10614     (let ((end nil))
10615       (while (not end)
10616         (when (or (not predicate)
10617                   (gnus-map-articles
10618                    predicate (gnus-summary-article-children)))
10619             (gnus-summary-hide-thread))
10620         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10621   (gnus-summary-position-point))
10622
10623 (defun gnus-summary-hide-thread ()
10624   "Hide thread subtrees.
10625 If PREDICATE is supplied, threads that satisfy this predicate
10626 will not be hidden.
10627 Returns nil if no threads were there to be hidden."
10628   (interactive)
10629   (let ((buffer-read-only nil)
10630         (start (point))
10631         (article (gnus-summary-article-number)))
10632     (goto-char start)
10633     ;; Go forward until either the buffer ends or the subthread
10634     ;; ends.
10635     (when (and (not (eobp))
10636                (or (zerop (gnus-summary-next-thread 1 t))
10637                    (goto-char (point-max))))
10638       (prog1
10639           (if (and (> (point) start)
10640                    (search-backward "\n" start t))
10641               (progn
10642                 (subst-char-in-region start (point) ?\n ?\^M)
10643                 (gnus-summary-goto-subject article))
10644             (goto-char start)
10645             nil)))))
10646
10647 (defun gnus-summary-go-to-next-thread (&optional previous)
10648   "Go to the same level (or less) next thread.
10649 If PREVIOUS is non-nil, go to previous thread instead.
10650 Return the article number moved to, or nil if moving was impossible."
10651   (let ((level (gnus-summary-thread-level))
10652         (way (if previous -1 1))
10653         (beg (point)))
10654     (forward-line way)
10655     (while (and (not (eobp))
10656                 (< level (gnus-summary-thread-level)))
10657       (forward-line way))
10658     (if (eobp)
10659         (progn
10660           (goto-char beg)
10661           nil)
10662       (setq beg (point))
10663       (prog1
10664           (gnus-summary-article-number)
10665         (goto-char beg)))))
10666
10667 (defun gnus-summary-next-thread (n &optional silent)
10668   "Go to the same level next N'th thread.
10669 If N is negative, search backward instead.
10670 Returns the difference between N and the number of skips actually
10671 done.
10672
10673 If SILENT, don't output messages."
10674   (interactive "p")
10675   (let ((backward (< n 0))
10676         (n (abs n)))
10677     (while (and (> n 0)
10678                 (gnus-summary-go-to-next-thread backward))
10679       (decf n))
10680     (unless silent
10681       (gnus-summary-position-point))
10682     (when (and (not silent) (/= 0 n))
10683       (gnus-message 7 "No more threads"))
10684     n))
10685
10686 (defun gnus-summary-prev-thread (n)
10687   "Go to the same level previous N'th thread.
10688 Returns the difference between N and the number of skips actually
10689 done."
10690   (interactive "p")
10691   (gnus-summary-next-thread (- n)))
10692
10693 (defun gnus-summary-go-down-thread ()
10694   "Go down one level in the current thread."
10695   (let ((children (gnus-summary-article-children)))
10696     (when children
10697       (gnus-summary-goto-subject (car children)))))
10698
10699 (defun gnus-summary-go-up-thread ()
10700   "Go up one level in the current thread."
10701   (let ((parent (gnus-summary-article-parent)))
10702     (when parent
10703       (gnus-summary-goto-subject parent))))
10704
10705 (defun gnus-summary-down-thread (n)
10706   "Go down thread N steps.
10707 If N is negative, go up instead.
10708 Returns the difference between N and how many steps down that were
10709 taken."
10710   (interactive "p")
10711   (let ((up (< n 0))
10712         (n (abs n)))
10713     (while (and (> n 0)
10714                 (if up (gnus-summary-go-up-thread)
10715                   (gnus-summary-go-down-thread)))
10716       (setq n (1- n)))
10717     (gnus-summary-position-point)
10718     (when (/= 0 n)
10719       (gnus-message 7 "Can't go further"))
10720     n))
10721
10722 (defun gnus-summary-up-thread (n)
10723   "Go up thread N steps.
10724 If N is negative, go down instead.
10725 Returns the difference between N and how many steps down that were
10726 taken."
10727   (interactive "p")
10728   (gnus-summary-down-thread (- n)))
10729
10730 (defun gnus-summary-top-thread ()
10731   "Go to the top of the thread."
10732   (interactive)
10733   (while (gnus-summary-go-up-thread))
10734   (gnus-summary-article-number))
10735
10736 (defun gnus-summary-kill-thread (&optional unmark)
10737   "Mark articles under current thread as read.
10738 If the prefix argument is positive, remove any kinds of marks.
10739 If the prefix argument is negative, tick articles instead."
10740   (interactive "P")
10741   (when unmark
10742     (setq unmark (prefix-numeric-value unmark)))
10743   (let ((articles (gnus-summary-articles-in-thread)))
10744     (save-excursion
10745       ;; Expand the thread.
10746       (gnus-summary-show-thread)
10747       ;; Mark all the articles.
10748       (while articles
10749         (gnus-summary-goto-subject (car articles))
10750         (cond ((null unmark)
10751                (gnus-summary-mark-article-as-read gnus-killed-mark))
10752               ((> unmark 0)
10753                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10754               (t
10755                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10756         (setq articles (cdr articles))))
10757     ;; Hide killed subtrees.
10758     (and (null unmark)
10759          gnus-thread-hide-killed
10760          (gnus-summary-hide-thread))
10761     ;; If marked as read, go to next unread subject.
10762     (when (null unmark)
10763       ;; Go to next unread subject.
10764       (gnus-summary-next-subject 1 t)))
10765   (gnus-set-mode-line 'summary))
10766
10767 ;; Summary sorting commands
10768
10769 (defun gnus-summary-sort-by-number (&optional reverse)
10770   "Sort the summary buffer by article number.
10771 Argument REVERSE means reverse order."
10772   (interactive "P")
10773   (gnus-summary-sort 'number reverse))
10774
10775 (defun gnus-summary-sort-by-random (&optional reverse)
10776   "Randomize the order in the summary buffer.
10777 Argument REVERSE means to randomize in reverse order."
10778   (interactive "P")
10779   (gnus-summary-sort 'random reverse))
10780
10781 (defun gnus-summary-sort-by-author (&optional reverse)
10782   "Sort the summary buffer by author name alphabetically.
10783 If `case-fold-search' is non-nil, case of letters is ignored.
10784 Argument REVERSE means reverse order."
10785   (interactive "P")
10786   (gnus-summary-sort 'author reverse))
10787
10788 (defun gnus-summary-sort-by-subject (&optional reverse)
10789   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10790 If `case-fold-search' is non-nil, case of letters is ignored.
10791 Argument REVERSE means reverse order."
10792   (interactive "P")
10793   (gnus-summary-sort 'subject reverse))
10794
10795 (defun gnus-summary-sort-by-date (&optional reverse)
10796   "Sort the summary buffer by date.
10797 Argument REVERSE means reverse order."
10798   (interactive "P")
10799   (gnus-summary-sort 'date reverse))
10800
10801 (defun gnus-summary-sort-by-score (&optional reverse)
10802   "Sort the summary buffer by score.
10803 Argument REVERSE means reverse order."
10804   (interactive "P")
10805   (gnus-summary-sort 'score reverse))
10806
10807 (defun gnus-summary-sort-by-lines (&optional reverse)
10808   "Sort the summary buffer by the number of lines.
10809 Argument REVERSE means reverse order."
10810   (interactive "P")
10811   (gnus-summary-sort 'lines reverse))
10812
10813 (defun gnus-summary-sort-by-chars (&optional reverse)
10814   "Sort the summary buffer by article length.
10815 Argument REVERSE means reverse order."
10816   (interactive "P")
10817   (gnus-summary-sort 'chars reverse))
10818
10819 (defun gnus-summary-sort-by-original (&optional reverse)
10820   "Sort the summary buffer using the default sorting method.
10821 Argument REVERSE means reverse order."
10822   (interactive "P")
10823   (let* ((buffer-read-only)
10824          (gnus-summary-prepare-hook nil))
10825     ;; We do the sorting by regenerating the threads.
10826     (gnus-summary-prepare)
10827     ;; Hide subthreads if needed.
10828     (gnus-summary-maybe-hide-threads)))
10829
10830 (defun gnus-summary-sort (predicate reverse)
10831   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10832   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10833          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10834          (gnus-thread-sort-functions
10835           (if (not reverse)
10836               thread
10837             `(lambda (t1 t2)
10838                (,thread t2 t1))))
10839          (gnus-sort-gathered-threads-function
10840           gnus-thread-sort-functions)
10841          (gnus-article-sort-functions
10842           (if (not reverse)
10843               article
10844             `(lambda (t1 t2)
10845                (,article t2 t1))))
10846          (buffer-read-only)
10847          (gnus-summary-prepare-hook nil))
10848     ;; We do the sorting by regenerating the threads.
10849     (gnus-summary-prepare)
10850     ;; Hide subthreads if needed.
10851     (gnus-summary-maybe-hide-threads)))
10852
10853 ;; Summary saving commands.
10854
10855 (defun gnus-summary-save-article (&optional n not-saved)
10856   "Save the current article using the default saver function.
10857 If N is a positive number, save the N next articles.
10858 If N is a negative number, save the N previous articles.
10859 If N is nil and any articles have been marked with the process mark,
10860 save those articles instead.
10861 The variable `gnus-default-article-saver' specifies the saver function."
10862   (interactive "P")
10863   (let* ((articles (gnus-summary-work-articles n))
10864          (save-buffer (save-excursion
10865                         (nnheader-set-temp-buffer " *Gnus Save*")))
10866          (num (length articles))
10867          header file)
10868     (dolist (article articles)
10869       (setq header (gnus-summary-article-header article))
10870       (if (not (vectorp header))
10871           ;; This is a pseudo-article.
10872           (if (assq 'name header)
10873               (gnus-copy-file (cdr (assq 'name header)))
10874             (gnus-message 1 "Article %d is unsaveable" article))
10875         ;; This is a real article.
10876         (save-window-excursion
10877           (let ((gnus-display-mime-function nil)
10878                 (gnus-article-prepare-hook nil))
10879             (gnus-summary-select-article t nil nil article)))
10880         (save-excursion
10881           (set-buffer save-buffer)
10882           (erase-buffer)
10883           (insert-buffer-substring gnus-original-article-buffer))
10884         (setq file (gnus-article-save save-buffer file num))
10885         (gnus-summary-remove-process-mark article)
10886         (unless not-saved
10887           (gnus-summary-set-saved-mark article))))
10888     (gnus-kill-buffer save-buffer)
10889     (gnus-summary-position-point)
10890     (gnus-set-mode-line 'summary)
10891     n))
10892
10893 (defun gnus-summary-pipe-output (&optional arg headers)
10894   "Pipe the current article to a subprocess.
10895 If N is a positive number, pipe the N next articles.
10896 If N is a negative number, pipe the N previous articles.
10897 If N is nil and any articles have been marked with the process mark,
10898 pipe those articles instead.
10899 If HEADERS (the symbolic prefix), include the headers, too."
10900   (interactive (gnus-interactive "P\ny"))
10901   (require 'gnus-art)
10902   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10903         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10904     (gnus-summary-save-article arg t))
10905   (let ((buffer (get-buffer "*Shell Command Output*")))
10906     (when (and buffer
10907                (not (zerop (buffer-size buffer))))
10908       (gnus-configure-windows 'pipe))))
10909
10910 (defun gnus-summary-save-article-mail (&optional arg)
10911   "Append the current article to an mail file.
10912 If N is a positive number, save the N next articles.
10913 If N is a negative number, save the N previous articles.
10914 If N is nil and any articles have been marked with the process mark,
10915 save those articles instead."
10916   (interactive "P")
10917   (require 'gnus-art)
10918   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10919     (gnus-summary-save-article arg)))
10920
10921 (defun gnus-summary-save-article-rmail (&optional arg)
10922   "Append the current article to an rmail file.
10923 If N is a positive number, save the N next articles.
10924 If N is a negative number, save the N previous articles.
10925 If N is nil and any articles have been marked with the process mark,
10926 save those articles instead."
10927   (interactive "P")
10928   (require 'gnus-art)
10929   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10930     (gnus-summary-save-article arg)))
10931
10932 (defun gnus-summary-save-article-file (&optional arg)
10933   "Append the current article to a file.
10934 If N is a positive number, save the N next articles.
10935 If N is a negative number, save the N previous articles.
10936 If N is nil and any articles have been marked with the process mark,
10937 save those articles instead."
10938   (interactive "P")
10939   (require 'gnus-art)
10940   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10941     (gnus-summary-save-article arg)))
10942
10943 (defun gnus-summary-write-article-file (&optional arg)
10944   "Write the current article to a file, deleting the previous file.
10945 If N is a positive number, save the N next articles.
10946 If N is a negative number, save the N previous articles.
10947 If N is nil and any articles have been marked with the process mark,
10948 save those articles instead."
10949   (interactive "P")
10950   (require 'gnus-art)
10951   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10952     (gnus-summary-save-article arg)))
10953
10954 (defun gnus-summary-save-article-body-file (&optional arg)
10955   "Append the current article body to a file.
10956 If N is a positive number, save the N next articles.
10957 If N is a negative number, save the N previous articles.
10958 If N is nil and any articles have been marked with the process mark,
10959 save those articles instead."
10960   (interactive "P")
10961   (require 'gnus-art)
10962   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10963     (gnus-summary-save-article arg)))
10964
10965 (defun gnus-summary-muttprint (&optional arg)
10966   "Print the current article using Muttprint.
10967 If N is a positive number, save the N next articles.
10968 If N is a negative number, save the N previous articles.
10969 If N is nil and any articles have been marked with the process mark,
10970 save those articles instead."
10971   (interactive "P")
10972   (require 'gnus-art)
10973   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10974     (gnus-summary-save-article arg t)))
10975
10976 (defun gnus-summary-pipe-message (program)
10977   "Pipe the current article through PROGRAM."
10978   (interactive "sProgram: ")
10979   (gnus-summary-select-article)
10980   (let ((mail-header-separator ""))
10981     (gnus-eval-in-buffer-window gnus-article-buffer
10982       (save-restriction
10983         (widen)
10984         (let ((start (window-start))
10985               buffer-read-only)
10986           (message-pipe-buffer-body program)
10987           (set-window-start (get-buffer-window (current-buffer)) start))))))
10988
10989 (defun gnus-get-split-value (methods)
10990   "Return a value based on the split METHODS."
10991   (let (split-name method result match)
10992     (when methods
10993       (save-excursion
10994         (set-buffer gnus-original-article-buffer)
10995         (save-restriction
10996           (nnheader-narrow-to-headers)
10997           (while (and methods (not split-name))
10998             (goto-char (point-min))
10999             (setq method (pop methods))
11000             (setq match (car method))
11001             (when (cond
11002                    ((stringp match)
11003                     ;; Regular expression.
11004                     (ignore-errors
11005                       (re-search-forward match nil t)))
11006                    ((functionp match)
11007                     ;; Function.
11008                     (save-restriction
11009                       (widen)
11010                       (setq result (funcall match gnus-newsgroup-name))))
11011                    ((consp match)
11012                     ;; Form.
11013                     (save-restriction
11014                       (widen)
11015                       (setq result (eval match)))))
11016               (setq split-name (cdr method))
11017               (cond ((stringp result)
11018                      (push (expand-file-name
11019                             result gnus-article-save-directory)
11020                            split-name))
11021                     ((consp result)
11022                      (setq split-name (append result split-name)))))))))
11023     (nreverse split-name)))
11024
11025 (defun gnus-valid-move-group-p (group)
11026   (and (boundp group)
11027        (symbol-name group)
11028        (symbol-value group)
11029        (gnus-get-function (gnus-find-method-for-group
11030                            (symbol-name group)) 'request-accept-article t)))
11031
11032 (defun gnus-read-move-group-name (prompt default articles prefix)
11033   "Read a group name."
11034   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11035          (minibuffer-confirm-incomplete nil) ; XEmacs
11036          (prom
11037           (format "%s %s to:"
11038                   prompt
11039                   (if (> (length articles) 1)
11040                       (format "these %d articles" (length articles))
11041                     "this article")))
11042          (to-newsgroup
11043           (cond
11044            ((null split-name)
11045             (gnus-completing-read-with-default
11046              default prom
11047              gnus-active-hashtb
11048              'gnus-valid-move-group-p
11049              nil prefix
11050              'gnus-group-history))
11051            ((= 1 (length split-name))
11052             (gnus-completing-read-with-default
11053              (car split-name) prom
11054              gnus-active-hashtb
11055              'gnus-valid-move-group-p
11056              nil nil
11057              'gnus-group-history))
11058            (t
11059             (gnus-completing-read-with-default
11060              nil prom
11061              (mapcar 'list (nreverse split-name))
11062              nil nil nil
11063              'gnus-group-history))))
11064          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11065     (when to-newsgroup
11066       (if (or (string= to-newsgroup "")
11067               (string= to-newsgroup prefix))
11068           (setq to-newsgroup default))
11069       (unless to-newsgroup
11070         (error "No group name entered"))
11071       (or (gnus-active to-newsgroup)
11072           (gnus-activate-group to-newsgroup nil nil to-method)
11073           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11074                                      to-newsgroup))
11075               (or (and (gnus-request-create-group to-newsgroup to-method)
11076                        (gnus-activate-group
11077                         to-newsgroup nil nil to-method)
11078                        (gnus-subscribe-group to-newsgroup))
11079                   (error "Couldn't create group %s" to-newsgroup)))
11080           (error "No such group: %s" to-newsgroup)))
11081     to-newsgroup))
11082
11083 (defun gnus-summary-save-parts (type dir n &optional reverse)
11084   "Save parts matching TYPE to DIR.
11085 If REVERSE, save parts that do not match TYPE."
11086   (interactive
11087    (list (read-string "Save parts of type: "
11088                       (or (car gnus-summary-save-parts-type-history)
11089                           gnus-summary-save-parts-default-mime)
11090                       'gnus-summary-save-parts-type-history)
11091          (setq gnus-summary-save-parts-last-directory
11092                (read-file-name "Save to directory: "
11093                                gnus-summary-save-parts-last-directory
11094                                nil t))
11095          current-prefix-arg))
11096   (gnus-summary-iterate n
11097     (let ((gnus-display-mime-function nil)
11098           (gnus-inhibit-treatment t))
11099       (gnus-summary-select-article))
11100     (save-excursion
11101       (set-buffer gnus-article-buffer)
11102       (let ((handles (or gnus-article-mime-handles
11103                          (mm-dissect-buffer nil gnus-article-loose-mime)
11104                          (and gnus-article-emulate-mime
11105                               (mm-uu-dissect)))))
11106         (when handles
11107           (gnus-summary-save-parts-1 type dir handles reverse)
11108           (unless gnus-article-mime-handles ;; Don't destroy this case.
11109             (mm-destroy-parts handles)))))))
11110
11111 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11112   (if (stringp (car handle))
11113       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11114               (cdr handle))
11115     (when (if reverse
11116               (not (string-match type (mm-handle-media-type handle)))
11117             (string-match type (mm-handle-media-type handle)))
11118       (let ((file (expand-file-name
11119                    (gnus-map-function
11120                     mm-file-name-rewrite-functions
11121                     (file-name-nondirectory
11122                      (or
11123                       (mail-content-type-get
11124                        (mm-handle-disposition handle) 'filename)
11125                       (mail-content-type-get
11126                        (mm-handle-type handle) 'name)
11127                       (concat gnus-newsgroup-name
11128                               "." (number-to-string
11129                                    (cdr gnus-article-current))))))
11130                    dir)))
11131         (unless (file-exists-p file)
11132           (mm-save-part-to-file handle file))))))
11133
11134 ;; Summary extract commands
11135
11136 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11137   (let ((buffer-read-only nil)
11138         (article (gnus-summary-article-number))
11139         after-article b e)
11140     (unless (gnus-summary-goto-subject article)
11141       (error "No such article: %d" article))
11142     (gnus-summary-position-point)
11143     ;; If all commands are to be bunched up on one line, we collect
11144     ;; them here.
11145     (unless gnus-view-pseudos-separately
11146       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11147             files action)
11148         (while ps
11149           (setq action (cdr (assq 'action (car ps))))
11150           (setq files (list (cdr (assq 'name (car ps)))))
11151           (while (and ps (cdr ps)
11152                       (string= (or action "1")
11153                                (or (cdr (assq 'action (cadr ps))) "2")))
11154             (push (cdr (assq 'name (cadr ps))) files)
11155             (setcdr ps (cddr ps)))
11156           (when files
11157             (when (not (string-match "%s" action))
11158               (push " " files))
11159             (push " " files)
11160             (when (assq 'execute (car ps))
11161               (setcdr (assq 'execute (car ps))
11162                       (funcall (if (string-match "%s" action)
11163                                    'format 'concat)
11164                                action
11165                                (mapconcat
11166                                 (lambda (f)
11167                                   (if (equal f " ")
11168                                       f
11169                                     (shell-quote-argument f)))
11170                                 files " ")))))
11171           (setq ps (cdr ps)))))
11172     (if (and gnus-view-pseudos (not not-view))
11173         (while pslist
11174           (when (assq 'execute (car pslist))
11175             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11176                                   (eq gnus-view-pseudos 'not-confirm)))
11177           (setq pslist (cdr pslist)))
11178       (save-excursion
11179         (while pslist
11180           (setq after-article (or (cdr (assq 'article (car pslist)))
11181                                   (gnus-summary-article-number)))
11182           (gnus-summary-goto-subject after-article)
11183           (forward-line 1)
11184           (setq b (point))
11185           (insert "    " (file-name-nondirectory
11186                           (cdr (assq 'name (car pslist))))
11187                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11188           (setq e (point))
11189           (forward-line -1)             ; back to `b'
11190           (gnus-add-text-properties
11191            b (1- e) (list 'gnus-number gnus-reffed-article-number
11192                           gnus-mouse-face-prop gnus-mouse-face))
11193           (gnus-data-enter
11194            after-article gnus-reffed-article-number
11195            gnus-unread-mark b (car pslist) 0 (- e b))
11196           (setq gnus-newsgroup-unreads
11197                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11198                                          gnus-reffed-article-number))
11199           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11200           (setq pslist (cdr pslist)))))))
11201
11202 (defun gnus-pseudos< (p1 p2)
11203   (let ((c1 (cdr (assq 'action p1)))
11204         (c2 (cdr (assq 'action p2))))
11205     (and c1 c2 (string< c1 c2))))
11206
11207 (defun gnus-request-pseudo-article (props)
11208   (cond ((assq 'execute props)
11209          (gnus-execute-command (cdr (assq 'execute props)))))
11210   (let ((gnus-current-article (gnus-summary-article-number)))
11211     (gnus-run-hooks 'gnus-mark-article-hook)))
11212
11213 (defun gnus-execute-command (command &optional automatic)
11214   (save-excursion
11215     (gnus-article-setup-buffer)
11216     (set-buffer gnus-article-buffer)
11217     (setq buffer-read-only nil)
11218     (let ((command (if automatic command
11219                      (read-string "Command: " (cons command 0)))))
11220       (erase-buffer)
11221       (insert "$ " command "\n\n")
11222       (if gnus-view-pseudo-asynchronously
11223           (start-process "gnus-execute" (current-buffer) shell-file-name
11224                          shell-command-switch command)
11225         (call-process shell-file-name nil t nil
11226                       shell-command-switch command)))))
11227
11228 ;; Summary kill commands.
11229
11230 (defun gnus-summary-edit-global-kill (article)
11231   "Edit the \"global\" kill file."
11232   (interactive (list (gnus-summary-article-number)))
11233   (gnus-group-edit-global-kill article))
11234
11235 (defun gnus-summary-edit-local-kill ()
11236   "Edit a local kill file applied to the current newsgroup."
11237   (interactive)
11238   (setq gnus-current-headers (gnus-summary-article-header))
11239   (gnus-group-edit-local-kill
11240    (gnus-summary-article-number) gnus-newsgroup-name))
11241
11242 ;;; Header reading.
11243
11244 (defun gnus-read-header (id &optional header)
11245   "Read the headers of article ID and enter them into the Gnus system."
11246   (let ((group gnus-newsgroup-name)
11247         (gnus-override-method
11248          (or
11249           gnus-override-method
11250           (and (gnus-news-group-p gnus-newsgroup-name)
11251                (car (gnus-refer-article-methods)))))
11252         where)
11253     ;; First we check to see whether the header in question is already
11254     ;; fetched.
11255     (if (stringp id)
11256         ;; This is a Message-ID.
11257         (setq header (or header (gnus-id-to-header id)))
11258       ;; This is an article number.
11259       (setq header (or header (gnus-summary-article-header id))))
11260     (if (and header
11261              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11262         ;; We have found the header.
11263         header
11264       ;; We have to really fetch the header to this article.
11265       (save-excursion
11266         (set-buffer nntp-server-buffer)
11267         (when (setq where (gnus-request-head id group))
11268           (nnheader-fold-continuation-lines)
11269           (goto-char (point-max))
11270           (insert ".\n")
11271           (goto-char (point-min))
11272           (insert "211 ")
11273           (princ (cond
11274                   ((numberp id) id)
11275                   ((cdr where) (cdr where))
11276                   (header (mail-header-number header))
11277                   (t gnus-reffed-article-number))
11278                  (current-buffer))
11279           (insert " Article retrieved.\n"))
11280         (if (or (not where)
11281                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11282             ()                          ; Malformed head.
11283           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11284             (when (and (stringp id)
11285                        (not (string= (gnus-group-real-name group)
11286                                      (car where))))
11287               ;; If we fetched by Message-ID and the article came
11288               ;; from a different group, we fudge some bogus article
11289               ;; numbers for this article.
11290               (mail-header-set-number header gnus-reffed-article-number))
11291             (save-excursion
11292               (set-buffer gnus-summary-buffer)
11293               (decf gnus-reffed-article-number)
11294               (gnus-remove-header (mail-header-number header))
11295               (push header gnus-newsgroup-headers)
11296               (setq gnus-current-headers header)
11297               (push (mail-header-number header) gnus-newsgroup-limit)))
11298           header)))))
11299
11300 (defun gnus-remove-header (number)
11301   "Remove header NUMBER from `gnus-newsgroup-headers'."
11302   (if (and gnus-newsgroup-headers
11303            (= number (mail-header-number (car gnus-newsgroup-headers))))
11304       (pop gnus-newsgroup-headers)
11305     (let ((headers gnus-newsgroup-headers))
11306       (while (and (cdr headers)
11307                   (not (= number (mail-header-number (cadr headers)))))
11308         (pop headers))
11309       (when (cdr headers)
11310         (setcdr headers (cddr headers))))))
11311
11312 ;;;
11313 ;;; summary highlights
11314 ;;;
11315
11316 (defun gnus-highlight-selected-summary ()
11317   "Highlight selected article in summary buffer."
11318   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11319   (when gnus-summary-selected-face
11320     (save-excursion
11321       (let* ((beg (point-at-bol))
11322              (end (point-at-eol))
11323              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11324              (from (if (get-text-property beg gnus-mouse-face-prop)
11325                        beg
11326                      (or (next-single-property-change
11327                           beg gnus-mouse-face-prop nil end)
11328                          beg)))
11329              (to
11330               (if (= from end)
11331                   (- from 2)
11332                 (or (next-single-property-change
11333                      from gnus-mouse-face-prop nil end)
11334                     end))))
11335         ;; If no mouse-face prop on line we will have to = from = end,
11336         ;; so we highlight the entire line instead.
11337         (when (= (+ to 2) from)
11338           (setq from beg)
11339           (setq to end))
11340         (if gnus-newsgroup-selected-overlay
11341             ;; Move old overlay.
11342             (gnus-move-overlay
11343              gnus-newsgroup-selected-overlay from to (current-buffer))
11344           ;; Create new overlay.
11345           (gnus-overlay-put
11346            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11347            'face gnus-summary-selected-face))))))
11348
11349 (defvar gnus-summary-highlight-line-cached nil)
11350 (defvar gnus-summary-highlight-line-trigger nil)
11351
11352 (defun gnus-summary-highlight-line-0 ()
11353   (if (and (eq gnus-summary-highlight-line-trigger
11354                gnus-summary-highlight)
11355            gnus-summary-highlight-line-cached)
11356       gnus-summary-highlight-line-cached
11357     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11358           gnus-summary-highlight-line-cached
11359           (let* ((cond (list 'cond))
11360                  (c cond)
11361                  (list gnus-summary-highlight))
11362             (while list
11363               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11364                               nil))
11365               (setq c (cdr c)
11366                     list (cdr list)))
11367             (gnus-byte-compile (list 'lambda nil cond))))))
11368
11369 (defun gnus-summary-highlight-line ()
11370   "Highlight current line according to `gnus-summary-highlight'."
11371   (let* ((beg (point-at-bol))
11372          (article (or (gnus-summary-article-number) gnus-current-article))
11373          (score (or (cdr (assq article
11374                                gnus-newsgroup-scored))
11375                     gnus-summary-default-score 0))
11376          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11377          (inhibit-read-only t)
11378          (default gnus-summary-default-score)
11379          (default-high gnus-summary-default-high-score)
11380          (default-low gnus-summary-default-low-score)
11381          (uncached (and gnus-summary-use-undownloaded-faces
11382                         (memq article gnus-newsgroup-undownloaded)
11383                         (not (memq article gnus-newsgroup-cached)))))
11384     (let ((face (funcall (gnus-summary-highlight-line-0))))
11385       (unless (eq face (get-text-property beg 'face))
11386         (gnus-put-text-property-excluding-characters-with-faces
11387          beg (point-at-eol) 'face
11388          (setq face (if (boundp face) (symbol-value face) face)))
11389         (when gnus-summary-highlight-line-function
11390           (funcall gnus-summary-highlight-line-function article face))))))
11391
11392 (defun gnus-update-read-articles (group unread &optional compute)
11393   "Update the list of read articles in GROUP.
11394 UNREAD is a sorted list."
11395   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11396         (info (gnus-get-info group))
11397         (prev 1)
11398         read)
11399     (if (or (not info) (not active))
11400         ;; There is no info on this group if it was, in fact,
11401         ;; killed.  Gnus stores no information on killed groups, so
11402         ;; there's nothing to be done.
11403         ;; One could store the information somewhere temporarily,
11404         ;; perhaps...  Hmmm...
11405         ()
11406       ;; Remove any negative articles numbers.
11407       (while (and unread (< (car unread) 0))
11408         (setq unread (cdr unread)))
11409       ;; Remove any expired article numbers
11410       (while (and unread (< (car unread) (car active)))
11411         (setq unread (cdr unread)))
11412       ;; Compute the ranges of read articles by looking at the list of
11413       ;; unread articles.
11414       (while unread
11415         (when (/= (car unread) prev)
11416           (push (if (= prev (1- (car unread))) prev
11417                   (cons prev (1- (car unread))))
11418                 read))
11419         (setq prev (1+ (car unread)))
11420         (setq unread (cdr unread)))
11421       (when (<= prev (cdr active))
11422         (push (cons prev (cdr active)) read))
11423       (setq read (if (> (length read) 1) (nreverse read) read))
11424       (if compute
11425           read
11426         (save-excursion
11427           (let (setmarkundo)
11428             ;; Propagate the read marks to the backend.
11429             (when (gnus-check-backend-function 'request-set-mark group)
11430               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11431                     (add (gnus-remove-from-range read (gnus-info-read info))))
11432                 (when (or add del)
11433                   (unless (gnus-check-group group)
11434                     (error "Can't open server for %s" group))
11435                   (gnus-request-set-mark
11436                    group (delq nil (list (if add (list add 'add '(read)))
11437                                          (if del (list del 'del '(read))))))
11438                   (setq setmarkundo
11439                         `(gnus-request-set-mark
11440                           ,group
11441                           ',(delq nil (list
11442                                        (if del (list del 'add '(read)))
11443                                        (if add (list add 'del '(read))))))))))
11444             (set-buffer gnus-group-buffer)
11445             (gnus-undo-register
11446               `(progn
11447                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11448                  (gnus-info-set-read ',info ',(gnus-info-read info))
11449                  (gnus-get-unread-articles-in-group ',info
11450                                                     (gnus-active ,group))
11451                  (gnus-group-update-group ,group t)
11452                  ,setmarkundo))))
11453         ;; Enter this list into the group info.
11454         (gnus-info-set-read info read)
11455         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11456         (gnus-get-unread-articles-in-group info (gnus-active group))
11457         t))))
11458
11459 (defun gnus-offer-save-summaries ()
11460   "Offer to save all active summary buffers."
11461   (let (buffers)
11462     ;; Go through all buffers and find all summaries.
11463     (dolist (buffer (buffer-list))
11464       (when (and (setq buffer (buffer-name buffer))
11465                  (string-match "Summary" buffer)
11466                  (with-current-buffer buffer
11467                    ;; We check that this is, indeed, a summary buffer.
11468                    (and (eq major-mode 'gnus-summary-mode)
11469                         ;; Also make sure this isn't bogus.
11470                         gnus-newsgroup-prepared
11471                         ;; Also make sure that this isn't a
11472                         ;; dead summary buffer.
11473                         (not gnus-dead-summary-mode))))
11474         (push buffer buffers)))
11475     ;; Go through all these summary buffers and offer to save them.
11476     (when buffers
11477       (save-excursion
11478         (map-y-or-n-p
11479          "Update summary buffer %s? "
11480          (lambda (buf)
11481            (switch-to-buffer buf)
11482            (gnus-summary-exit))
11483          buffers)))))
11484
11485 (defun gnus-summary-setup-default-charset ()
11486   "Setup newsgroup default charset."
11487   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11488       (setq gnus-newsgroup-charset nil)
11489     (let* ((ignored-charsets
11490             (or gnus-newsgroup-ephemeral-ignored-charsets
11491                 (append
11492                  (and gnus-newsgroup-name
11493                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11494                  gnus-newsgroup-ignored-charsets))))
11495       (setq gnus-newsgroup-charset
11496             (or gnus-newsgroup-ephemeral-charset
11497                 (and gnus-newsgroup-name
11498                      (gnus-parameter-charset gnus-newsgroup-name))
11499                 gnus-default-charset))
11500       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11501            ignored-charsets))))
11502
11503 ;;;
11504 ;;; Mime Commands
11505 ;;;
11506
11507 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11508   "Display the current article buffer fully MIME-buttonized.
11509 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11510 treated as multipart/mixed."
11511   (interactive "P")
11512   (require 'gnus-art)
11513   (let ((gnus-unbuttonized-mime-types nil)
11514         (gnus-mime-display-multipart-as-mixed show-all-parts))
11515     (gnus-summary-show-article)))
11516
11517 (defun gnus-summary-repair-multipart (article)
11518   "Add a Content-Type header to a multipart article without one."
11519   (interactive (list (gnus-summary-article-number)))
11520   (gnus-with-article article
11521     (message-narrow-to-head)
11522     (message-remove-header "Mime-Version")
11523     (goto-char (point-max))
11524     (insert "Mime-Version: 1.0\n")
11525     (widen)
11526     (when (search-forward "\n--" nil t)
11527       (let ((separator (buffer-substring (point) (point-at-eol))))
11528         (message-narrow-to-head)
11529         (message-remove-header "Content-Type")
11530         (goto-char (point-max))
11531         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11532                         separator))
11533         (widen))))
11534   (let (gnus-mark-article-hook)
11535     (gnus-summary-select-article t t nil article)))
11536
11537 (defun gnus-summary-toggle-display-buttonized ()
11538   "Toggle the buttonizing of the article buffer."
11539   (interactive)
11540   (require 'gnus-art)
11541   (if (setq gnus-inhibit-mime-unbuttonizing
11542             (not gnus-inhibit-mime-unbuttonizing))
11543       (let ((gnus-unbuttonized-mime-types nil))
11544         (gnus-summary-show-article))
11545     (gnus-summary-show-article)))
11546
11547 ;;;
11548 ;;; Generic summary marking commands
11549 ;;;
11550
11551 (defvar gnus-summary-marking-alist
11552   '((read gnus-del-mark "d")
11553     (unread gnus-unread-mark "u")
11554     (ticked gnus-ticked-mark "!")
11555     (dormant gnus-dormant-mark "?")
11556     (expirable gnus-expirable-mark "e"))
11557   "An alist of names/marks/keystrokes.")
11558
11559 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11560 (defvar gnus-summary-mark-map)
11561
11562 (defun gnus-summary-make-all-marking-commands ()
11563   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11564   (dolist (elem gnus-summary-marking-alist)
11565     (apply 'gnus-summary-make-marking-command elem)))
11566
11567 (defun gnus-summary-make-marking-command (name mark keystroke)
11568   (let ((map (make-sparse-keymap)))
11569     (define-key gnus-summary-generic-mark-map keystroke map)
11570     (dolist (lway `((next "next" next nil "n")
11571                     (next-unread "next unread" next t "N")
11572                     (prev "previous" prev nil "p")
11573                     (prev-unread "previous unread" prev t "P")
11574                     (nomove "" nil nil ,keystroke)))
11575       (let ((func (gnus-summary-make-marking-command-1
11576                    mark (car lway) lway name)))
11577         (setq func (eval func))
11578         (define-key map (nth 4 lway) func)))))
11579
11580 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11581   `(defun ,(intern
11582             (format "gnus-summary-put-mark-as-%s%s"
11583                     name (if (eq way 'nomove)
11584                              ""
11585                            (concat "-" (symbol-name way)))))
11586      (n)
11587      ,(format
11588        "Mark the current article as %s%s.
11589 If N, the prefix, then repeat N times.
11590 If N is negative, move in reverse order.
11591 The difference between N and the actual number of articles marked is
11592 returned."
11593        name (cadr lway))
11594      (interactive "p")
11595      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11596
11597 (defun gnus-summary-generic-mark (n mark move unread)
11598   "Mark N articles with MARK."
11599   (unless (eq major-mode 'gnus-summary-mode)
11600     (error "This command can only be used in the summary buffer"))
11601   (gnus-summary-show-thread)
11602   (let ((nummove
11603          (cond
11604           ((eq move 'next) 1)
11605           ((eq move 'prev) -1)
11606           (t 0))))
11607     (if (zerop nummove)
11608         (setq n 1)
11609       (when (< n 0)
11610         (setq n (abs n)
11611               nummove (* -1 nummove))))
11612     (while (and (> n 0)
11613                 (gnus-summary-mark-article nil mark)
11614                 (zerop (gnus-summary-next-subject nummove unread t)))
11615       (setq n (1- n)))
11616     (when (/= 0 n)
11617       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11618     (gnus-summary-recenter)
11619     (gnus-summary-position-point)
11620     (gnus-set-mode-line 'summary)
11621     n))
11622
11623 (defun gnus-summary-insert-articles (articles)
11624   (when (setq articles
11625               (gnus-sorted-difference articles
11626                                       (mapcar (lambda (h)
11627                                                 (mail-header-number h))
11628                                               gnus-newsgroup-headers)))
11629     (setq gnus-newsgroup-headers
11630           (gnus-merge 'list
11631                       gnus-newsgroup-headers
11632                       (gnus-fetch-headers articles)
11633                       'gnus-article-sort-by-number))
11634     ;; Suppress duplicates?
11635     (when gnus-suppress-duplicates
11636       (gnus-dup-suppress-articles))
11637
11638     ;; We might want to build some more threads first.
11639     (when (and gnus-fetch-old-headers
11640                (eq gnus-headers-retrieved-by 'nov))
11641       (if (eq gnus-fetch-old-headers 'invisible)
11642           (gnus-build-all-threads)
11643         (gnus-build-old-threads)))
11644     ;; Let the Gnus agent mark articles as read.
11645     (when gnus-agent
11646       (gnus-agent-get-undownloaded-list))
11647     ;; Remove list identifiers from subject
11648     (when gnus-list-identifiers
11649       (gnus-summary-remove-list-identifiers))
11650     ;; First and last article in this newsgroup.
11651     (when gnus-newsgroup-headers
11652       (setq gnus-newsgroup-begin
11653             (mail-header-number (car gnus-newsgroup-headers))
11654             gnus-newsgroup-end
11655             (mail-header-number
11656              (gnus-last-element gnus-newsgroup-headers))))
11657     (when gnus-use-scoring
11658       (gnus-possibly-score-headers))))
11659
11660 (defun gnus-summary-insert-old-articles (&optional all)
11661   "Insert all old articles in this group.
11662 If ALL is non-nil, already read articles become readable.
11663 If ALL is a number, fetch this number of articles."
11664   (interactive "P")
11665   (prog1
11666       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11667             older len)
11668         (setq older
11669               ;; Some nntp servers lie about their active range.  When
11670               ;; this happens, the active range can be in the millions.
11671               ;; Use a compressed range to avoid creating a huge list.
11672               (gnus-range-difference (list gnus-newsgroup-active) old))
11673         (setq len (gnus-range-length older))
11674         (cond
11675          ((null older) nil)
11676          ((numberp all)
11677           (if (< all len)
11678               (let ((older-range (nreverse older)))
11679                 (setq older nil)
11680
11681                 (while (> all 0)
11682                   (let* ((r (pop older-range))
11683                          (min (if (numberp r) r (car r)))
11684                          (max (if (numberp r) r (cdr r))))
11685                     (while (and (<= min max)
11686                                 (> all 0))
11687                       (push max older)
11688                       (setq all (1- all)
11689                             max (1- max))))))
11690             (setq older (gnus-uncompress-range older))))
11691          (all
11692           (setq older (gnus-uncompress-range older)))
11693          (t
11694           (when (and (numberp gnus-large-newsgroup)
11695                    (> len gnus-large-newsgroup))
11696               (let* ((cursor-in-echo-area nil)
11697                      (initial (gnus-parameter-large-newsgroup-initial
11698                                gnus-newsgroup-name))
11699                      (input
11700                       (read-string
11701                        (format
11702                         "How many articles from %s (%s %d): "
11703                         (gnus-limit-string
11704                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11705                         (if initial "max" "default")
11706                         len)
11707                        (if initial
11708                            (cons (number-to-string initial)
11709                                  0)))))
11710                 (unless (string-match "^[ \t]*$" input)
11711                   (setq all (string-to-number input))
11712                   (if (< all len)
11713                       (let ((older-range (nreverse older)))
11714                         (setq older nil)
11715
11716                         (while (> all 0)
11717                           (let* ((r (pop older-range))
11718                                  (min (if (numberp r) r (car r)))
11719                                  (max (if (numberp r) r (cdr r))))
11720                             (while (and (<= min max)
11721                                         (> all 0))
11722                               (push max older)
11723                               (setq all (1- all)
11724                                     max (1- max))))))))))
11725           (setq older (gnus-uncompress-range older))))
11726         (if (not older)
11727             (message "No old news.")
11728           (gnus-summary-insert-articles older)
11729           (gnus-summary-limit (gnus-sorted-nunion old older))))
11730     (gnus-summary-position-point)))
11731
11732 (defun gnus-summary-insert-new-articles ()
11733   "Insert all new articles in this group."
11734   (interactive)
11735   (prog1
11736       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11737             (old-active gnus-newsgroup-active)
11738             (nnmail-fetched-sources (list t))
11739             i new)
11740         (setq gnus-newsgroup-active
11741               (gnus-activate-group gnus-newsgroup-name 'scan))
11742         (setq i (cdr gnus-newsgroup-active))
11743         (while (> i (cdr old-active))
11744           (push i new)
11745           (decf i))
11746         (if (not new)
11747             (message "No gnus is bad news")
11748           (gnus-summary-insert-articles new)
11749           (setq gnus-newsgroup-unreads
11750                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11751           (gnus-summary-limit (gnus-sorted-nunion old new))))
11752     (gnus-summary-position-point)))
11753
11754 (gnus-summary-make-all-marking-commands)
11755
11756 (gnus-ems-redefine)
11757
11758 (provide 'gnus-sum)
11759
11760 (run-hooks 'gnus-sum-load-hook)
11761
11762 ;; Local Variables:
11763 ;; coding: iso-8859-1
11764 ;; End:
11765
11766 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11767 ;;; gnus-sum.el ends here