(gnus-read-header): Don't remove a header for the parent article of a sparse
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary t
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715                         
716
717 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
718   "*List of functions used for sorting articles in the summary buffer.
719
720 Each function takes two articles and returns non-nil if the first
721 article should be sorted before the other.  If you use more than one
722 function, the primary sort function should be the last.  You should
723 probably always include `gnus-article-sort-by-number' in the list of
724 sorting functions -- preferably first.  Also note that sorting by date
725 is often much slower than sorting by number, and the sorting order is
726 very similar.  (Sorting by date means sorting by the time the message
727 was sent, sorting by number means sorting by arrival time.)
728
729 Each item can also be a list `(not F)' where F is a function;
730 this reverses the sort order.
731
732 Ready-made functions include `gnus-article-sort-by-number',
733 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
734 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
735 and `gnus-article-sort-by-score'.
736
737 When threading is turned on, the variable `gnus-thread-sort-functions'
738 controls how articles are sorted."
739   :group 'gnus-summary-sort
740   :type '(repeat (gnus-widget-reversible
741                   (choice (function-item gnus-article-sort-by-number)
742                           (function-item gnus-article-sort-by-author)
743                           (function-item gnus-article-sort-by-subject)
744                           (function-item gnus-article-sort-by-date)
745                           (function-item gnus-article-sort-by-score)
746                           (function-item gnus-article-sort-by-random)
747                           (function :tag "other"))
748                   (boolean :tag "Reverse order"))))
749
750
751 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
752   "*List of functions used for sorting threads in the summary buffer.
753 By default, threads are sorted by article number.
754
755 Each function takes two threads and returns non-nil if the first
756 thread should be sorted before the other.  If you use more than one
757 function, the primary sort function should be the last.  You should
758 probably always include `gnus-thread-sort-by-number' in the list of
759 sorting functions -- preferably first.  Also note that sorting by date
760 is often much slower than sorting by number, and the sorting order is
761 very similar.  (Sorting by date means sorting by the time the message
762 was sent, sorting by number means sorting by arrival time.)
763
764 Each list item can also be a list `(not F)' where F is a
765 function; this specifies reversed sort order.
766
767 Ready-made functions include `gnus-thread-sort-by-number',
768 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
769 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
770 `gnus-thread-sort-by-most-recent-number',
771 `gnus-thread-sort-by-most-recent-date',
772 `gnus-thread-sort-by-random', and
773 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
774
775 When threading is turned off, the variable
776 `gnus-article-sort-functions' controls how articles are sorted."
777   :group 'gnus-summary-sort
778   :type '(repeat 
779           (gnus-widget-reversible
780            (choice (function-item gnus-thread-sort-by-number)
781                    (function-item gnus-thread-sort-by-author)
782                    (function-item gnus-thread-sort-by-subject)
783                    (function-item gnus-thread-sort-by-date)
784                    (function-item gnus-thread-sort-by-score)
785                    (function-item gnus-thread-sort-by-most-recent-number)
786                    (function-item gnus-thread-sort-by-most-recent-date)
787                    (function-item gnus-thread-sort-by-random)
788                    (function-item gnus-thread-sort-by-total-score)
789                    (function :tag "other"))
790            (boolean :tag "Reverse order"))))
791
792 (defcustom gnus-thread-score-function '+
793   "*Function used for calculating the total score of a thread.
794
795 The function is called with the scores of the article and each
796 subthread and should then return the score of the thread.
797
798 Some functions you can use are `+', `max', or `min'."
799   :group 'gnus-summary-sort
800   :type 'function)
801
802 (defcustom gnus-summary-expunge-below nil
803   "All articles that have a score less than this variable will be expunged.
804 This variable is local to the summary buffers."
805   :group 'gnus-score-default
806   :type '(choice (const :tag "off" nil)
807                  integer))
808
809 (defcustom gnus-thread-expunge-below nil
810   "All threads that have a total score less than this variable will be expunged.
811 See `gnus-thread-score-function' for en explanation of what a
812 \"thread score\" is.
813
814 This variable is local to the summary buffers."
815   :group 'gnus-threading
816   :group 'gnus-score-default
817   :type '(choice (const :tag "off" nil)
818                  integer))
819
820 (defcustom gnus-summary-mode-hook nil
821   "*A hook for Gnus summary mode.
822 This hook is run before any variables are set in the summary buffer."
823   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
824   :group 'gnus-summary-various
825   :type 'hook)
826
827 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
828 (when (featurep 'xemacs)
829   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
830   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
831   (add-hook 'gnus-summary-mode-hook
832             'gnus-xmas-switch-horizontal-scrollbar-off))
833
834 (defcustom gnus-summary-menu-hook nil
835   "*Hook run after the creation of the summary mode menu."
836   :group 'gnus-summary-visual
837   :type 'hook)
838
839 (defcustom gnus-summary-exit-hook nil
840   "*A hook called on exit from the summary buffer.
841 It will be called with point in the group buffer."
842   :group 'gnus-summary-exit
843   :type 'hook)
844
845 (defcustom gnus-summary-prepare-hook nil
846   "*A hook called after the summary buffer has been generated.
847 If you want to modify the summary buffer, you can use this hook."
848   :group 'gnus-summary-various
849   :type 'hook)
850
851 (defcustom gnus-summary-prepared-hook nil
852   "*A hook called as the last thing after the summary buffer has been generated."
853   :group 'gnus-summary-various
854   :type 'hook)
855
856 (defcustom gnus-summary-generate-hook nil
857   "*A hook run just before generating the summary buffer.
858 This hook is commonly used to customize threading variables and the
859 like."
860   :group 'gnus-summary-various
861   :type 'hook)
862
863 (defcustom gnus-select-group-hook nil
864   "*A hook called when a newsgroup is selected.
865
866 If you'd like to simplify subjects like the
867 `gnus-summary-next-same-subject' command does, you can use the
868 following hook:
869
870  (add-hook gnus-select-group-hook
871            (lambda ()
872              (mapcar (lambda (header)
873                        (mail-header-set-subject
874                         header
875                         (gnus-simplify-subject
876                          (mail-header-subject header) 're-only)))
877                      gnus-newsgroup-headers)))"
878   :group 'gnus-group-select
879   :type 'hook)
880
881 (defcustom gnus-select-article-hook nil
882   "*A hook called when an article is selected."
883   :group 'gnus-summary-choose
884   :options '(gnus-agent-fetch-selected-article)
885   :type 'hook)
886
887 (defcustom gnus-visual-mark-article-hook
888   (list 'gnus-highlight-selected-summary)
889   "*Hook run after selecting an article in the summary buffer.
890 It is meant to be used for highlighting the article in some way.  It
891 is not run if `gnus-visual' is nil."
892   :group 'gnus-summary-visual
893   :type 'hook)
894
895 (defcustom gnus-parse-headers-hook nil
896   "*A hook called before parsing the headers."
897   :group 'gnus-various
898   :type 'hook)
899
900 (defcustom gnus-exit-group-hook nil
901   "*A hook called when exiting summary mode.
902 This hook is not called from the non-updating exit commands like `Q'."
903   :group 'gnus-various
904   :type 'hook)
905
906 (defcustom gnus-summary-update-hook
907   (list 'gnus-summary-highlight-line)
908   "*A hook called when a summary line is changed.
909 The hook will not be called if `gnus-visual' is nil.
910
911 The default function `gnus-summary-highlight-line' will
912 highlight the line according to the `gnus-summary-highlight'
913 variable."
914   :group 'gnus-summary-visual
915   :type 'hook)
916
917 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
918   "*A hook called when an article is selected for the first time.
919 The hook is intended to mark an article as read (or unread)
920 automatically when it is selected."
921   :group 'gnus-summary-choose
922   :type 'hook)
923
924 (defcustom gnus-group-no-more-groups-hook nil
925   "*A hook run when returning to group mode having no more (unread) groups."
926   :group 'gnus-group-select
927   :type 'hook)
928
929 (defcustom gnus-ps-print-hook nil
930   "*A hook run before ps-printing something from Gnus."
931   :group 'gnus-summary
932   :type 'hook)
933
934 (defcustom gnus-summary-article-move-hook nil
935   "*A hook called after an article is moved, copied, respooled, or crossposted."
936   :group 'gnus-summary
937   :type 'hook)
938
939 (defcustom gnus-summary-article-delete-hook nil
940   "*A hook called after an article is deleted."
941   :group 'gnus-summary
942   :type 'hook)
943
944 (defcustom gnus-summary-article-expire-hook nil
945   "*A hook called after an article is expired."
946   :group 'gnus-summary
947   :type 'hook)
948
949 (defcustom gnus-summary-display-arrow
950   (and (fboundp 'display-graphic-p)
951        (display-graphic-p))
952   "*If non-nil, display an arrow highlighting the current article."
953   :version "21.1"
954   :group 'gnus-summary
955   :type 'boolean)
956
957 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
958   "Face used for highlighting the current article in the summary buffer."
959   :group 'gnus-summary-visual
960   :type 'face)
961
962 (defvar gnus-tmp-downloaded nil)
963
964 (defcustom gnus-summary-highlight
965   '(((eq mark gnus-canceled-mark)
966      . gnus-summary-cancelled-face)
967     ((and uncached (> score default-high))
968      . gnus-summary-high-undownloaded-face)
969     ((and uncached (< score default-low))
970      . gnus-summary-low-undownloaded-face)
971     (uncached
972      . gnus-summary-normal-undownloaded-face)
973     ((and (> score default-high)
974           (or (eq mark gnus-dormant-mark)
975               (eq mark gnus-ticked-mark)))
976      . gnus-summary-high-ticked-face)
977     ((and (< score default-low)
978           (or (eq mark gnus-dormant-mark)
979               (eq mark gnus-ticked-mark)))
980      . gnus-summary-low-ticked-face)
981     ((or (eq mark gnus-dormant-mark)
982          (eq mark gnus-ticked-mark))
983      . gnus-summary-normal-ticked-face)
984     ((and (> score default-high) (eq mark gnus-ancient-mark))
985      . gnus-summary-high-ancient-face)
986     ((and (< score default-low) (eq mark gnus-ancient-mark))
987      . gnus-summary-low-ancient-face)
988     ((eq mark gnus-ancient-mark)
989      . gnus-summary-normal-ancient-face)
990     ((and (> score default-high) (eq mark gnus-unread-mark))
991      . gnus-summary-high-unread-face)
992     ((and (< score default-low) (eq mark gnus-unread-mark))
993      . gnus-summary-low-unread-face)
994     ((eq mark gnus-unread-mark)
995      . gnus-summary-normal-unread-face)
996     ((> score default-high)
997      . gnus-summary-high-read-face)
998     ((< score default-low)
999      . gnus-summary-low-read-face)
1000     (t
1001      . gnus-summary-normal-read-face))
1002   "*Controls the highlighting of summary buffer lines.
1003
1004 A list of (FORM . FACE) pairs.  When deciding how a a particular
1005 summary line should be displayed, each form is evaluated.  The content
1006 of the face field after the first true form is used.  You can change
1007 how those summary lines are displayed, by editing the face field.
1008
1009 You can use the following variables in the FORM field.
1010
1011 score:        The article's score
1012 default:      The default article score.
1013 default-high: The default score for high scored articles.
1014 default-low:  The default score for low scored articles.
1015 below:        The score below which articles are automatically marked as read.
1016 mark:         The article's mark.
1017 uncached:     Non-nil if the article is uncached."
1018   :group 'gnus-summary-visual
1019   :type '(repeat (cons (sexp :tag "Form" nil)
1020                        face)))
1021
1022 (defcustom gnus-alter-header-function nil
1023   "Function called to allow alteration of article header structures.
1024 The function is called with one parameter, the article header vector,
1025 which it may alter in any way."
1026   :type '(choice (const :tag "None" nil)
1027                  function)
1028   :group 'gnus-summary)
1029
1030 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1031   "Variable that says which function should be used to decode a string with encoded words.")
1032
1033 (defcustom gnus-extra-headers '(To Newsgroups)
1034   "*Extra headers to parse."
1035   :version "21.1"
1036   :group 'gnus-summary
1037   :type '(repeat symbol))
1038
1039 (defcustom gnus-ignored-from-addresses
1040   (and user-mail-address (regexp-quote user-mail-address))
1041   "*Regexp of From headers that may be suppressed in favor of To headers."
1042   :version "21.1"
1043   :group 'gnus-summary
1044   :type 'regexp)
1045
1046 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1047   "List of charsets that should be ignored.
1048 When these charsets are used in the \"charset\" parameter, the
1049 default charset will be used instead."
1050   :version "21.1"
1051   :type '(repeat symbol)
1052   :group 'gnus-charset)
1053
1054 (gnus-define-group-parameter
1055  ignored-charsets
1056  :type list
1057  :function-document
1058  "Return the ignored charsets of GROUP."
1059  :variable gnus-group-ignored-charsets-alist
1060  :variable-default
1061  '(("alt\\.chinese\\.text" iso-8859-1))
1062  :variable-document
1063  "Alist of regexps (to match group names) and charsets that should be ignored.
1064 When these charsets are used in the \"charset\" parameter, the
1065 default charset will be used instead."
1066  :variable-group gnus-charset
1067  :variable-type '(repeat (cons (regexp :tag "Group")
1068                                (repeat symbol)))
1069  :parameter-type '(choice :tag "Ignored charsets"
1070                           :value nil
1071                           (repeat (symbol)))
1072  :parameter-document       "\
1073 List of charsets that should be ignored.
1074
1075 When these charsets are used in the \"charset\" parameter, the
1076 default charset will be used instead.")
1077
1078 (defcustom gnus-group-highlight-words-alist nil
1079   "Alist of group regexps and highlight regexps.
1080 This variable uses the same syntax as `gnus-emphasis-alist'."
1081   :version "21.1"
1082   :type '(repeat (cons (regexp :tag "Group")
1083                        (repeat (list (regexp :tag "Highlight regexp")
1084                                      (number :tag "Group for entire word" 0)
1085                                      (number :tag "Group for displayed part" 0)
1086                                      (symbol :tag "Face"
1087                                              gnus-emphasis-highlight-words)))))
1088   :group 'gnus-summary-visual)
1089
1090 (defcustom gnus-summary-show-article-charset-alist
1091   nil
1092   "Alist of number and charset.
1093 The article will be shown with the charset corresponding to the
1094 numbered argument.
1095 For example: ((1 . cn-gb-2312) (2 . big5))."
1096   :version "21.1"
1097   :type '(repeat (cons (number :tag "Argument" 1)
1098                        (symbol :tag "Charset")))
1099   :group 'gnus-charset)
1100
1101 (defcustom gnus-preserve-marks t
1102   "Whether marks are preserved when moving, copying and respooling messages."
1103   :version "21.1"
1104   :type 'boolean
1105   :group 'gnus-summary-marks)
1106
1107 (defcustom gnus-alter-articles-to-read-function nil
1108   "Function to be called to alter the list of articles to be selected."
1109   :type '(choice (const nil) function)
1110   :group 'gnus-summary)
1111
1112 (defcustom gnus-orphan-score nil
1113   "*All orphans get this score added.  Set in the score file."
1114   :group 'gnus-score-default
1115   :type '(choice (const nil)
1116                  integer))
1117
1118 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1119   "*A regexp to match MIME parts when saving multiple parts of a
1120 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1121 This regexp will be used by default when prompting the user for which
1122 type of files to save."
1123   :group 'gnus-summary
1124   :type 'regexp)
1125
1126 (defcustom gnus-read-all-available-headers nil
1127   "Whether Gnus should parse all headers made available to it.
1128 This is mostly relevant for slow back ends where the user may
1129 wish to widen the summary buffer to include all headers
1130 that were fetched.  Say, for nnultimate groups."
1131   :group 'gnus-summary
1132   :type '(choice boolean regexp))
1133
1134 (defcustom gnus-summary-muttprint-program "muttprint"
1135   "Command (and optional arguments) used to run Muttprint."
1136   :version "21.3"
1137   :group 'gnus-summary
1138   :type 'string)
1139
1140 (defcustom gnus-article-loose-mime nil
1141   "If non-nil, don't require MIME-Version header.
1142 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1143 supply the MIME-Version header or deliberately strip it From the mail.
1144 Set it to non-nil, Gnus will treat some articles as MIME even if
1145 the MIME-Version header is missed."
1146   :version "21.3"
1147   :type 'boolean
1148   :group 'gnus-article-mime)
1149
1150 (defcustom gnus-article-emulate-mime t
1151   "If non-nil, use MIME emulation for uuencode and the like.
1152 This means that Gnus will search message bodies for text that look
1153 like uuencoded bits, yEncoded bits, and so on, and present that using
1154 the normal Gnus MIME machinery."
1155   :type 'boolean
1156   :group 'gnus-article-mime)
1157
1158 ;;; Internal variables
1159
1160 (defvar gnus-summary-display-cache nil)
1161 (defvar gnus-article-mime-handles nil)
1162 (defvar gnus-article-decoded-p nil)
1163 (defvar gnus-article-charset nil)
1164 (defvar gnus-article-ignored-charsets nil)
1165 (defvar gnus-scores-exclude-files nil)
1166 (defvar gnus-page-broken nil)
1167
1168 (defvar gnus-original-article nil)
1169 (defvar gnus-article-internal-prepare-hook nil)
1170 (defvar gnus-newsgroup-process-stack nil)
1171
1172 (defvar gnus-thread-indent-array nil)
1173 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1174 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1175   "Function called to sort the articles within a thread after it has been gathered together.")
1176
1177 (defvar gnus-summary-save-parts-type-history nil)
1178 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1179
1180 ;; Avoid highlighting in kill files.
1181 (defvar gnus-summary-inhibit-highlight nil)
1182 (defvar gnus-newsgroup-selected-overlay nil)
1183 (defvar gnus-inhibit-limiting nil)
1184 (defvar gnus-newsgroup-adaptive-score-file nil)
1185 (defvar gnus-current-score-file nil)
1186 (defvar gnus-current-move-group nil)
1187 (defvar gnus-current-copy-group nil)
1188 (defvar gnus-current-crosspost-group nil)
1189 (defvar gnus-newsgroup-display nil)
1190
1191 (defvar gnus-newsgroup-dependencies nil)
1192 (defvar gnus-newsgroup-adaptive nil)
1193 (defvar gnus-summary-display-article-function nil)
1194 (defvar gnus-summary-highlight-line-function nil
1195   "Function called after highlighting a summary line.")
1196
1197 (defvar gnus-summary-line-format-alist
1198   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1199     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1200     (?s gnus-tmp-subject-or-nil ?s)
1201     (?n gnus-tmp-name ?s)
1202     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1203         ?s)
1204     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1205             gnus-tmp-from) ?s)
1206     (?F gnus-tmp-from ?s)
1207     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1208     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1209     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1210     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1211     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1212     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1213     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1214     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1215     (?L gnus-tmp-lines ?s)
1216     (?O gnus-tmp-downloaded ?c)
1217     (?I gnus-tmp-indentation ?s)
1218     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1219     (?R gnus-tmp-replied ?c)
1220     (?\[ gnus-tmp-opening-bracket ?c)
1221     (?\] gnus-tmp-closing-bracket ?c)
1222     (?\> (make-string gnus-tmp-level ? ) ?s)
1223     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1224     (?i gnus-tmp-score ?d)
1225     (?z gnus-tmp-score-char ?c)
1226     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1227     (?U gnus-tmp-unread ?c)
1228     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1229         ?s)
1230     (?t (gnus-summary-number-of-articles-in-thread
1231          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1232         ?d)
1233     (?e (gnus-summary-number-of-articles-in-thread
1234          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1235         ?c)
1236     (?u gnus-tmp-user-defined ?s)
1237     (?P (gnus-pick-line-number) ?d)
1238     (?B gnus-tmp-thread-tree-header-string ?s)
1239     (user-date (gnus-user-date
1240                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1241   "An alist of format specifications that can appear in summary lines.
1242 These are paired with what variables they correspond with, along with
1243 the type of the variable (string, integer, character, etc).")
1244
1245 (defvar gnus-summary-dummy-line-format-alist
1246   `((?S gnus-tmp-subject ?s)
1247     (?N gnus-tmp-number ?d)
1248     (?u gnus-tmp-user-defined ?s)))
1249
1250 (defvar gnus-summary-mode-line-format-alist
1251   `((?G gnus-tmp-group-name ?s)
1252     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1253     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1254     (?A gnus-tmp-article-number ?d)
1255     (?Z gnus-tmp-unread-and-unselected ?s)
1256     (?V gnus-version ?s)
1257     (?U gnus-tmp-unread-and-unticked ?d)
1258     (?S gnus-tmp-subject ?s)
1259     (?e gnus-tmp-unselected ?d)
1260     (?u gnus-tmp-user-defined ?s)
1261     (?d (length gnus-newsgroup-dormant) ?d)
1262     (?t (length gnus-newsgroup-marked) ?d)
1263     (?h (length gnus-newsgroup-spam-marked) ?d)
1264     (?r (length gnus-newsgroup-reads) ?d)
1265     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1266     (?E gnus-newsgroup-expunged-tally ?d)
1267     (?s (gnus-current-score-file-nondirectory) ?s)))
1268
1269 (defvar gnus-last-search-regexp nil
1270   "Default regexp for article search command.")
1271
1272 (defvar gnus-last-shell-command nil
1273   "Default shell command on article.")
1274
1275 (defvar gnus-newsgroup-agentized nil
1276   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1277 (defvar gnus-newsgroup-begin nil)
1278 (defvar gnus-newsgroup-end nil)
1279 (defvar gnus-newsgroup-last-rmail nil)
1280 (defvar gnus-newsgroup-last-mail nil)
1281 (defvar gnus-newsgroup-last-folder nil)
1282 (defvar gnus-newsgroup-last-file nil)
1283 (defvar gnus-newsgroup-auto-expire nil)
1284 (defvar gnus-newsgroup-active nil)
1285
1286 (defvar gnus-newsgroup-data nil)
1287 (defvar gnus-newsgroup-data-reverse nil)
1288 (defvar gnus-newsgroup-limit nil)
1289 (defvar gnus-newsgroup-limits nil)
1290 (defvar gnus-summary-use-undownloaded-faces nil)
1291
1292 (defvar gnus-newsgroup-unreads nil
1293   "Sorted list of unread articles in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-unselected nil
1296   "Sorted list of unselected unread articles in the current newsgroup.")
1297
1298 (defvar gnus-newsgroup-reads nil
1299   "Alist of read articles and article marks in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-expunged-tally nil)
1302
1303 (defvar gnus-newsgroup-marked nil
1304   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1305
1306 (defvar gnus-newsgroup-spam-marked nil
1307   "List of ranges of articles that have been marked as spam.")
1308
1309 (defvar gnus-newsgroup-killed nil
1310   "List of ranges of articles that have been through the scoring process.")
1311
1312 (defvar gnus-newsgroup-cached nil
1313   "Sorted list of articles that come from the article cache.")
1314
1315 (defvar gnus-newsgroup-saved nil
1316   "List of articles that have been saved.")
1317
1318 (defvar gnus-newsgroup-kill-headers nil)
1319
1320 (defvar gnus-newsgroup-replied nil
1321   "List of articles that have been replied to in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-forwarded nil
1324   "List of articles that have been forwarded in the current newsgroup.")
1325
1326 (defvar gnus-newsgroup-recent nil
1327   "List of articles that have are recent in the current newsgroup.")
1328
1329 (defvar gnus-newsgroup-expirable nil
1330   "Sorted list of articles in the current newsgroup that can be expired.")
1331
1332 (defvar gnus-newsgroup-processable nil
1333   "List of articles in the current newsgroup that can be processed.")
1334
1335 (defvar gnus-newsgroup-downloadable nil
1336   "Sorted list of articles in the current newsgroup that can be processed.")
1337
1338 (defvar gnus-newsgroup-unfetched nil
1339   "Sorted list of articles in the current newsgroup whose headers have
1340 not been fetched into the agent.
1341
1342 This list will always be a subset of gnus-newsgroup-undownloaded.")
1343
1344 (defvar gnus-newsgroup-undownloaded nil
1345   "List of articles in the current newsgroup that haven't been downloaded.")
1346
1347 (defvar gnus-newsgroup-unsendable nil
1348   "List of articles in the current newsgroup that won't be sent.")
1349
1350 (defvar gnus-newsgroup-bookmarks nil
1351   "List of articles in the current newsgroup that have bookmarks.")
1352
1353 (defvar gnus-newsgroup-dormant nil
1354   "Sorted list of dormant articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-unseen nil
1357   "List of unseen articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-seen nil
1360   "Range of seen articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-articles nil
1363   "List of articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-scored nil
1366   "List of scored articles in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-headers nil
1369   "List of article headers in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-threads nil)
1372
1373 (defvar gnus-newsgroup-prepared nil
1374   "Whether the current group has been prepared properly.")
1375
1376 (defvar gnus-newsgroup-ancient nil
1377   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-sparse nil)
1380
1381 (defvar gnus-current-article nil)
1382 (defvar gnus-article-current nil)
1383 (defvar gnus-current-headers nil)
1384 (defvar gnus-have-all-headers nil)
1385 (defvar gnus-last-article nil)
1386 (defvar gnus-newsgroup-history nil)
1387 (defvar gnus-newsgroup-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-charset nil)
1389 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1390
1391 (defvar gnus-article-before-search nil)
1392
1393 (defvar gnus-summary-local-variables
1394   '(gnus-newsgroup-name
1395     gnus-newsgroup-begin gnus-newsgroup-end
1396     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1397     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1398     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1399     gnus-newsgroup-unselected gnus-newsgroup-marked
1400     gnus-newsgroup-spam-marked
1401     gnus-newsgroup-reads gnus-newsgroup-saved
1402     gnus-newsgroup-replied gnus-newsgroup-forwarded
1403     gnus-newsgroup-recent
1404     gnus-newsgroup-expirable
1405     gnus-newsgroup-processable gnus-newsgroup-killed
1406     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1407     gnus-newsgroup-unfetched
1408     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1409     gnus-newsgroup-seen gnus-newsgroup-articles
1410     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1411     gnus-newsgroup-headers gnus-newsgroup-threads
1412     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1413     gnus-current-article gnus-current-headers gnus-have-all-headers
1414     gnus-last-article gnus-article-internal-prepare-hook
1415     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1416     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1417     gnus-thread-expunge-below
1418     gnus-score-alist gnus-current-score-file
1419     (gnus-summary-expunge-below . global)
1420     (gnus-summary-mark-below . global)
1421     (gnus-orphan-score . global)
1422     gnus-newsgroup-active gnus-scores-exclude-files
1423     gnus-newsgroup-history gnus-newsgroup-ancient
1424     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1425     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1426     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1427     (gnus-newsgroup-expunged-tally . 0)
1428     gnus-cache-removable-articles gnus-newsgroup-cached
1429     gnus-newsgroup-data gnus-newsgroup-data-reverse
1430     gnus-newsgroup-limit gnus-newsgroup-limits
1431     gnus-newsgroup-charset gnus-newsgroup-display
1432     gnus-summary-use-undownloaded-faces)
1433   "Variables that are buffer-local to the summary buffers.")
1434
1435 (defvar gnus-newsgroup-variables nil
1436   "A list of variables that have separate values in different newsgroups.
1437 A list of newsgroup (summary buffer) local variables, or cons of
1438 variables and their default values (when the default values are not
1439 nil), that should be made global while the summary buffer is active.
1440 These variables can be used to set variables in the group parameters
1441 while still allowing them to affect operations done in other
1442 buffers. For example:
1443
1444 \(setq gnus-newsgroup-variables
1445      '(message-use-followup-to
1446        (gnus-visible-headers .
1447          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1448 ")
1449
1450 ;; Byte-compiler warning.
1451 (eval-when-compile
1452   ;; Bind features so that require will believe that gnus-sum has
1453   ;; already been loaded (avoids infinite recursion)
1454   (let ((features (cons 'gnus-sum features)))
1455     ;; Several of the declarations in gnus-sum are needed to load the
1456     ;; following files. Right now, these definitions have been
1457     ;; compiled but not defined (evaluated).  We could either do a
1458     ;; eval-and-compile about all of the declarations or evaluate the
1459     ;; source file.
1460     (if (boundp 'gnus-newsgroup-variables)
1461         nil
1462       (load "gnus-sum.el" t t t))
1463     (require 'gnus)
1464     (require 'gnus-agent)
1465     (require 'gnus-art)))
1466
1467 ;; MIME stuff.
1468
1469 (defvar gnus-decode-encoded-word-methods
1470   '(mail-decode-encoded-word-string)
1471   "List of methods used to decode encoded words.
1472
1473 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1474 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1475 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1476 whose names match REGEXP.
1477
1478 For example:
1479 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1480  mail-decode-encoded-word-string
1481  (\"chinese\" . rfc1843-decode-string))")
1482
1483 (defvar gnus-decode-encoded-word-methods-cache nil)
1484
1485 (defun gnus-multi-decode-encoded-word-string (string)
1486   "Apply the functions from `gnus-encoded-word-methods' that match."
1487   (unless (and gnus-decode-encoded-word-methods-cache
1488                (eq gnus-newsgroup-name
1489                    (car gnus-decode-encoded-word-methods-cache)))
1490     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1491     (mapcar (lambda (x)
1492               (if (symbolp x)
1493                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1494                 (if (and gnus-newsgroup-name
1495                          (string-match (car x) gnus-newsgroup-name))
1496                     (nconc gnus-decode-encoded-word-methods-cache
1497                            (list (cdr x))))))
1498             gnus-decode-encoded-word-methods))
1499   (let ((xlist gnus-decode-encoded-word-methods-cache))
1500     (pop xlist)
1501     (while xlist
1502       (setq string (funcall (pop xlist) string))))
1503   string)
1504
1505 ;; Subject simplification.
1506
1507 (defun gnus-simplify-whitespace (str)
1508   "Remove excessive whitespace from STR."
1509   ;; Multiple spaces.
1510   (while (string-match "[ \t][ \t]+" str)
1511     (setq str (concat (substring str 0 (match-beginning 0))
1512                         " "
1513                         (substring str (match-end 0)))))
1514   ;; Leading spaces.
1515   (when (string-match "^[ \t]+" str)
1516     (setq str (substring str (match-end 0))))
1517   ;; Trailing spaces.
1518   (when (string-match "[ \t]+$" str)
1519     (setq str (substring str 0 (match-beginning 0))))
1520   str)
1521
1522 (defun gnus-simplify-all-whitespace (str)
1523   "Remove all whitespace from STR."
1524   (while (string-match "[ \t\n]+" str)
1525     (setq str (replace-match "" nil nil str)))
1526   str)
1527
1528 (defsubst gnus-simplify-subject-re (subject)
1529   "Remove \"Re:\" from subject lines."
1530   (if (string-match message-subject-re-regexp subject)
1531       (substring subject (match-end 0))
1532     subject))
1533
1534 (defun gnus-simplify-subject (subject &optional re-only)
1535   "Remove `Re:' and words in parentheses.
1536 If RE-ONLY is non-nil, strip leading `Re:'s only."
1537   (let ((case-fold-search t))           ;Ignore case.
1538     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1539     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1540       (setq subject (substring subject (match-end 0))))
1541     ;; Remove uninteresting prefixes.
1542     (when (and (not re-only)
1543                gnus-simplify-ignored-prefixes
1544                (string-match gnus-simplify-ignored-prefixes subject))
1545       (setq subject (substring subject (match-end 0))))
1546     ;; Remove words in parentheses from end.
1547     (unless re-only
1548       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1549         (setq subject (substring subject 0 (match-beginning 0)))))
1550     ;; Return subject string.
1551     subject))
1552
1553 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1554 ;; all whitespace.
1555 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1556   (goto-char (point-min))
1557   (while (re-search-forward regexp nil t)
1558     (replace-match (or newtext ""))))
1559
1560 (defun gnus-simplify-buffer-fuzzy ()
1561   "Simplify string in the buffer fuzzily.
1562 The string in the accessible portion of the current buffer is simplified.
1563 It is assumed to be a single-line subject.
1564 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1565 matter is removed.  Additional things can be deleted by setting
1566 `gnus-simplify-subject-fuzzy-regexp'."
1567   (let ((case-fold-search t)
1568         (modified-tick))
1569     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1570
1571     (while (not (eq modified-tick (buffer-modified-tick)))
1572       (setq modified-tick (buffer-modified-tick))
1573       (cond
1574        ((listp gnus-simplify-subject-fuzzy-regexp)
1575         (mapcar 'gnus-simplify-buffer-fuzzy-step
1576                 gnus-simplify-subject-fuzzy-regexp))
1577        (gnus-simplify-subject-fuzzy-regexp
1578         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1579       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1580       (gnus-simplify-buffer-fuzzy-step
1581        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1582       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1583
1584     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1585     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1586     (gnus-simplify-buffer-fuzzy-step " $")
1587     (gnus-simplify-buffer-fuzzy-step "^ +")))
1588
1589 (defun gnus-simplify-subject-fuzzy (subject)
1590   "Simplify a subject string fuzzily.
1591 See `gnus-simplify-buffer-fuzzy' for details."
1592   (save-excursion
1593     (gnus-set-work-buffer)
1594     (let ((case-fold-search t))
1595       ;; Remove uninteresting prefixes.
1596       (when (and gnus-simplify-ignored-prefixes
1597                  (string-match gnus-simplify-ignored-prefixes subject))
1598         (setq subject (substring subject (match-end 0))))
1599       (insert subject)
1600       (inline (gnus-simplify-buffer-fuzzy))
1601       (buffer-string))))
1602
1603 (defsubst gnus-simplify-subject-fully (subject)
1604   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1605   (cond
1606    (gnus-simplify-subject-functions
1607     (gnus-map-function gnus-simplify-subject-functions subject))
1608    ((null gnus-summary-gather-subject-limit)
1609     (gnus-simplify-subject-re subject))
1610    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1611     (gnus-simplify-subject-fuzzy subject))
1612    ((numberp gnus-summary-gather-subject-limit)
1613     (gnus-limit-string (gnus-simplify-subject-re subject)
1614                        gnus-summary-gather-subject-limit))
1615    (t
1616     subject)))
1617
1618 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1619   "Check whether two subjects are equal.
1620 If optional argument SIMPLE-FIRST is t, first argument is already
1621 simplified."
1622   (cond
1623    ((null simple-first)
1624     (equal (gnus-simplify-subject-fully s1)
1625            (gnus-simplify-subject-fully s2)))
1626    (t
1627     (equal s1
1628            (gnus-simplify-subject-fully s2)))))
1629
1630 (defun gnus-summary-bubble-group ()
1631   "Increase the score of the current group.
1632 This is a handy function to add to `gnus-summary-exit-hook' to
1633 increase the score of each group you read."
1634   (gnus-group-add-score gnus-newsgroup-name))
1635
1636 \f
1637 ;;;
1638 ;;; Gnus summary mode
1639 ;;;
1640
1641 (put 'gnus-summary-mode 'mode-class 'special)
1642
1643 (defvar gnus-article-commands-menu)
1644
1645 ;; Non-orthogonal keys
1646
1647 (gnus-define-keys gnus-summary-mode-map
1648   " " gnus-summary-next-page
1649   "\177" gnus-summary-prev-page
1650   [delete] gnus-summary-prev-page
1651   [backspace] gnus-summary-prev-page
1652   "\r" gnus-summary-scroll-up
1653   "\M-\r" gnus-summary-scroll-down
1654   "n" gnus-summary-next-unread-article
1655   "p" gnus-summary-prev-unread-article
1656   "N" gnus-summary-next-article
1657   "P" gnus-summary-prev-article
1658   "\M-\C-n" gnus-summary-next-same-subject
1659   "\M-\C-p" gnus-summary-prev-same-subject
1660   "\M-n" gnus-summary-next-unread-subject
1661   "\M-p" gnus-summary-prev-unread-subject
1662   "." gnus-summary-first-unread-article
1663   "," gnus-summary-best-unread-article
1664   "\M-s" gnus-summary-search-article-forward
1665   "\M-r" gnus-summary-search-article-backward
1666   "<" gnus-summary-beginning-of-article
1667   ">" gnus-summary-end-of-article
1668   "j" gnus-summary-goto-article
1669   "^" gnus-summary-refer-parent-article
1670   "\M-^" gnus-summary-refer-article
1671   "u" gnus-summary-tick-article-forward
1672   "!" gnus-summary-tick-article-forward
1673   "U" gnus-summary-tick-article-backward
1674   "d" gnus-summary-mark-as-read-forward
1675   "D" gnus-summary-mark-as-read-backward
1676   "E" gnus-summary-mark-as-expirable
1677   "\M-u" gnus-summary-clear-mark-forward
1678   "\M-U" gnus-summary-clear-mark-backward
1679   "k" gnus-summary-kill-same-subject-and-select
1680   "\C-k" gnus-summary-kill-same-subject
1681   "\M-\C-k" gnus-summary-kill-thread
1682   "\M-\C-l" gnus-summary-lower-thread
1683   "e" gnus-summary-edit-article
1684   "#" gnus-summary-mark-as-processable
1685   "\M-#" gnus-summary-unmark-as-processable
1686   "\M-\C-t" gnus-summary-toggle-threads
1687   "\M-\C-s" gnus-summary-show-thread
1688   "\M-\C-h" gnus-summary-hide-thread
1689   "\M-\C-f" gnus-summary-next-thread
1690   "\M-\C-b" gnus-summary-prev-thread
1691   [(meta down)] gnus-summary-next-thread
1692   [(meta up)] gnus-summary-prev-thread
1693   "\M-\C-u" gnus-summary-up-thread
1694   "\M-\C-d" gnus-summary-down-thread
1695   "&" gnus-summary-execute-command
1696   "c" gnus-summary-catchup-and-exit
1697   "\C-w" gnus-summary-mark-region-as-read
1698   "\C-t" gnus-summary-toggle-truncation
1699   "?" gnus-summary-mark-as-dormant
1700   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1701   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1702   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1703   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1704   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1705   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1706   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1707   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1708   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1709   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1710   "=" gnus-summary-expand-window
1711   "\C-x\C-s" gnus-summary-reselect-current-group
1712   "\M-g" gnus-summary-rescan-group
1713   "w" gnus-summary-stop-page-breaking
1714   "\C-c\C-r" gnus-summary-caesar-message
1715   "f" gnus-summary-followup
1716   "F" gnus-summary-followup-with-original
1717   "C" gnus-summary-cancel-article
1718   "r" gnus-summary-reply
1719   "R" gnus-summary-reply-with-original
1720   "\C-c\C-f" gnus-summary-mail-forward
1721   "o" gnus-summary-save-article
1722   "\C-o" gnus-summary-save-article-mail
1723   "|" gnus-summary-pipe-output
1724   "\M-k" gnus-summary-edit-local-kill
1725   "\M-K" gnus-summary-edit-global-kill
1726   ;; "V" gnus-version
1727   "\C-c\C-d" gnus-summary-describe-group
1728   "q" gnus-summary-exit
1729   "Q" gnus-summary-exit-no-update
1730   "\C-c\C-i" gnus-info-find-node
1731   gnus-mouse-2 gnus-mouse-pick-article
1732   "m" gnus-summary-mail-other-window
1733   "a" gnus-summary-post-news
1734   "i" gnus-summary-news-other-window
1735   "x" gnus-summary-limit-to-unread
1736   "s" gnus-summary-isearch-article
1737   "t" gnus-summary-toggle-header
1738   "g" gnus-summary-show-article
1739   "l" gnus-summary-goto-last-article
1740   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1741   "\C-d" gnus-summary-enter-digest-group
1742   "\M-\C-d" gnus-summary-read-document
1743   "\M-\C-e" gnus-summary-edit-parameters
1744   "\M-\C-a" gnus-summary-customize-parameters
1745   "\C-c\C-b" gnus-bug
1746   "*" gnus-cache-enter-article
1747   "\M-*" gnus-cache-remove-article
1748   "\M-&" gnus-summary-universal-argument
1749   "\C-l" gnus-recenter
1750   "I" gnus-summary-increase-score
1751   "L" gnus-summary-lower-score
1752   "\M-i" gnus-symbolic-argument
1753   "h" gnus-summary-select-article-buffer
1754
1755   "b" gnus-article-view-part
1756   "\M-t" gnus-summary-toggle-display-buttonized
1757
1758   "V" gnus-summary-score-map
1759   "X" gnus-uu-extract-map
1760   "S" gnus-summary-send-map)
1761
1762 ;; Sort of orthogonal keymap
1763 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1764   "t" gnus-summary-tick-article-forward
1765   "!" gnus-summary-tick-article-forward
1766   "d" gnus-summary-mark-as-read-forward
1767   "r" gnus-summary-mark-as-read-forward
1768   "c" gnus-summary-clear-mark-forward
1769   " " gnus-summary-clear-mark-forward
1770   "e" gnus-summary-mark-as-expirable
1771   "x" gnus-summary-mark-as-expirable
1772   "?" gnus-summary-mark-as-dormant
1773   "b" gnus-summary-set-bookmark
1774   "B" gnus-summary-remove-bookmark
1775   "#" gnus-summary-mark-as-processable
1776   "\M-#" gnus-summary-unmark-as-processable
1777   "S" gnus-summary-limit-include-expunged
1778   "C" gnus-summary-catchup
1779   "H" gnus-summary-catchup-to-here
1780   "h" gnus-summary-catchup-from-here
1781   "\C-c" gnus-summary-catchup-all
1782   "k" gnus-summary-kill-same-subject-and-select
1783   "K" gnus-summary-kill-same-subject
1784   "P" gnus-uu-mark-map)
1785
1786 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1787   "c" gnus-summary-clear-above
1788   "u" gnus-summary-tick-above
1789   "m" gnus-summary-mark-above
1790   "k" gnus-summary-kill-below)
1791
1792 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1793   "/" gnus-summary-limit-to-subject
1794   "n" gnus-summary-limit-to-articles
1795   "w" gnus-summary-pop-limit
1796   "s" gnus-summary-limit-to-subject
1797   "a" gnus-summary-limit-to-author
1798   "u" gnus-summary-limit-to-unread
1799   "m" gnus-summary-limit-to-marks
1800   "M" gnus-summary-limit-exclude-marks
1801   "v" gnus-summary-limit-to-score
1802   "*" gnus-summary-limit-include-cached
1803   "D" gnus-summary-limit-include-dormant
1804   "T" gnus-summary-limit-include-thread
1805   "d" gnus-summary-limit-exclude-dormant
1806   "t" gnus-summary-limit-to-age
1807   "." gnus-summary-limit-to-unseen
1808   "x" gnus-summary-limit-to-extra
1809   "p" gnus-summary-limit-to-display-predicate
1810   "E" gnus-summary-limit-include-expunged
1811   "c" gnus-summary-limit-exclude-childless-dormant
1812   "C" gnus-summary-limit-mark-excluded-as-read
1813   "o" gnus-summary-insert-old-articles
1814   "N" gnus-summary-insert-new-articles
1815   "r" gnus-summary-limit-to-replied)
1816
1817 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1818   "n" gnus-summary-next-unread-article
1819   "p" gnus-summary-prev-unread-article
1820   "N" gnus-summary-next-article
1821   "P" gnus-summary-prev-article
1822   "\C-n" gnus-summary-next-same-subject
1823   "\C-p" gnus-summary-prev-same-subject
1824   "\M-n" gnus-summary-next-unread-subject
1825   "\M-p" gnus-summary-prev-unread-subject
1826   "f" gnus-summary-first-unread-article
1827   "b" gnus-summary-best-unread-article
1828   "j" gnus-summary-goto-article
1829   "g" gnus-summary-goto-subject
1830   "l" gnus-summary-goto-last-article
1831   "o" gnus-summary-pop-article)
1832
1833 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1834   "k" gnus-summary-kill-thread
1835   "l" gnus-summary-lower-thread
1836   "i" gnus-summary-raise-thread
1837   "T" gnus-summary-toggle-threads
1838   "t" gnus-summary-rethread-current
1839   "^" gnus-summary-reparent-thread
1840   "s" gnus-summary-show-thread
1841   "S" gnus-summary-show-all-threads
1842   "h" gnus-summary-hide-thread
1843   "H" gnus-summary-hide-all-threads
1844   "n" gnus-summary-next-thread
1845   "p" gnus-summary-prev-thread
1846   "u" gnus-summary-up-thread
1847   "o" gnus-summary-top-thread
1848   "d" gnus-summary-down-thread
1849   "#" gnus-uu-mark-thread
1850   "\M-#" gnus-uu-unmark-thread)
1851
1852 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1853   "g" gnus-summary-prepare
1854   "c" gnus-summary-insert-cached-articles
1855   "d" gnus-summary-insert-dormant-articles)
1856
1857 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1858   "c" gnus-summary-catchup-and-exit
1859   "C" gnus-summary-catchup-all-and-exit
1860   "E" gnus-summary-exit-no-update
1861   "Q" gnus-summary-exit
1862   "Z" gnus-summary-exit
1863   "n" gnus-summary-catchup-and-goto-next-group
1864   "R" gnus-summary-reselect-current-group
1865   "G" gnus-summary-rescan-group
1866   "N" gnus-summary-next-group
1867   "s" gnus-summary-save-newsrc
1868   "P" gnus-summary-prev-group)
1869
1870 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1871   " " gnus-summary-next-page
1872   "n" gnus-summary-next-page
1873   "\177" gnus-summary-prev-page
1874   [delete] gnus-summary-prev-page
1875   "p" gnus-summary-prev-page
1876   "\r" gnus-summary-scroll-up
1877   "\M-\r" gnus-summary-scroll-down
1878   "<" gnus-summary-beginning-of-article
1879   ">" gnus-summary-end-of-article
1880   "b" gnus-summary-beginning-of-article
1881   "e" gnus-summary-end-of-article
1882   "^" gnus-summary-refer-parent-article
1883   "r" gnus-summary-refer-parent-article
1884   "D" gnus-summary-enter-digest-group
1885   "R" gnus-summary-refer-references
1886   "T" gnus-summary-refer-thread
1887   "g" gnus-summary-show-article
1888   "s" gnus-summary-isearch-article
1889   "P" gnus-summary-print-article
1890   "M" gnus-mailing-list-insinuate
1891   "t" gnus-article-babel)
1892
1893 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1894   "b" gnus-article-add-buttons
1895   "B" gnus-article-add-buttons-to-head
1896   "o" gnus-article-treat-overstrike
1897   "e" gnus-article-emphasize
1898   "w" gnus-article-fill-cited-article
1899   "Q" gnus-article-fill-long-lines
1900   "C" gnus-article-capitalize-sentences
1901   "c" gnus-article-remove-cr
1902   "q" gnus-article-de-quoted-unreadable
1903   "6" gnus-article-de-base64-unreadable
1904   "Z" gnus-article-decode-HZ
1905   "A" gnus-article-treat-ansi-sequences
1906   "h" gnus-article-wash-html
1907   "u" gnus-article-unsplit-urls
1908   "s" gnus-summary-force-verify-and-decrypt
1909   "f" gnus-article-display-x-face
1910   "l" gnus-summary-stop-page-breaking
1911   "r" gnus-summary-caesar-message
1912   "m" gnus-summary-morse-message
1913   "t" gnus-summary-toggle-header
1914   "g" gnus-treat-smiley
1915   "v" gnus-summary-verbose-headers
1916   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1917   "p" gnus-article-verify-x-pgp-sig
1918   "d" gnus-article-treat-dumbquotes)
1919
1920 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1921   ;; mnemonic: deuglif*Y*
1922   "u" gnus-article-outlook-unwrap-lines
1923   "a" gnus-article-outlook-repair-attribution
1924   "c" gnus-article-outlook-rearrange-citation
1925   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1926
1927 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1928   "a" gnus-article-hide
1929   "h" gnus-article-hide-headers
1930   "b" gnus-article-hide-boring-headers
1931   "s" gnus-article-hide-signature
1932   "c" gnus-article-hide-citation
1933   "C" gnus-article-hide-citation-in-followups
1934   "l" gnus-article-hide-list-identifiers
1935   "B" gnus-article-strip-banner
1936   "P" gnus-article-hide-pem
1937   "\C-c" gnus-article-hide-citation-maybe)
1938
1939 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1940   "a" gnus-article-highlight
1941   "h" gnus-article-highlight-headers
1942   "c" gnus-article-highlight-citation
1943   "s" gnus-article-highlight-signature)
1944
1945 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1946   "f" gnus-article-treat-fold-headers
1947   "u" gnus-article-treat-unfold-headers
1948   "n" gnus-article-treat-fold-newsgroups)
1949
1950 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1951   "x" gnus-article-display-x-face
1952   "d" gnus-article-display-face
1953   "s" gnus-treat-smiley
1954   "D" gnus-article-remove-images
1955   "f" gnus-treat-from-picon
1956   "m" gnus-treat-mail-picon
1957   "n" gnus-treat-newsgroups-picon)
1958
1959 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1960   "w" gnus-article-decode-mime-words
1961   "c" gnus-article-decode-charset
1962   "v" gnus-mime-view-all-parts
1963   "b" gnus-article-view-part)
1964
1965 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1966   "z" gnus-article-date-ut
1967   "u" gnus-article-date-ut
1968   "l" gnus-article-date-local
1969   "p" gnus-article-date-english
1970   "e" gnus-article-date-lapsed
1971   "o" gnus-article-date-original
1972   "i" gnus-article-date-iso8601
1973   "s" gnus-article-date-user)
1974
1975 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1976   "t" gnus-article-remove-trailing-blank-lines
1977   "l" gnus-article-strip-leading-blank-lines
1978   "m" gnus-article-strip-multiple-blank-lines
1979   "a" gnus-article-strip-blank-lines
1980   "A" gnus-article-strip-all-blank-lines
1981   "s" gnus-article-strip-leading-space
1982   "e" gnus-article-strip-trailing-space
1983   "w" gnus-article-remove-leading-whitespace)
1984
1985 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1986   "v" gnus-version
1987   "f" gnus-summary-fetch-faq
1988   "d" gnus-summary-describe-group
1989   "h" gnus-summary-describe-briefly
1990   "i" gnus-info-find-node
1991   "c" gnus-group-fetch-charter
1992   "C" gnus-group-fetch-control)
1993
1994 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1995   "e" gnus-summary-expire-articles
1996   "\M-\C-e" gnus-summary-expire-articles-now
1997   "\177" gnus-summary-delete-article
1998   [delete] gnus-summary-delete-article
1999   [backspace] gnus-summary-delete-article
2000   "m" gnus-summary-move-article
2001   "r" gnus-summary-respool-article
2002   "w" gnus-summary-edit-article
2003   "c" gnus-summary-copy-article
2004   "B" gnus-summary-crosspost-article
2005   "q" gnus-summary-respool-query
2006   "t" gnus-summary-respool-trace
2007   "i" gnus-summary-import-article
2008   "I" gnus-summary-create-article
2009   "p" gnus-summary-article-posted-p)
2010
2011 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2012   "o" gnus-summary-save-article
2013   "m" gnus-summary-save-article-mail
2014   "F" gnus-summary-write-article-file
2015   "r" gnus-summary-save-article-rmail
2016   "f" gnus-summary-save-article-file
2017   "b" gnus-summary-save-article-body-file
2018   "h" gnus-summary-save-article-folder
2019   "v" gnus-summary-save-article-vm
2020   "p" gnus-summary-pipe-output
2021   "P" gnus-summary-muttprint
2022   "s" gnus-soup-add-article)
2023
2024 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2025   "b" gnus-summary-display-buttonized
2026   "m" gnus-summary-repair-multipart
2027   "v" gnus-article-view-part
2028   "o" gnus-article-save-part
2029   "c" gnus-article-copy-part
2030   "C" gnus-article-view-part-as-charset
2031   "e" gnus-article-view-part-externally
2032   "E" gnus-article-encrypt-body
2033   "i" gnus-article-inline-part
2034   "|" gnus-article-pipe-part)
2035
2036 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2037   "p" gnus-summary-mark-as-processable
2038   "u" gnus-summary-unmark-as-processable
2039   "U" gnus-summary-unmark-all-processable
2040   "v" gnus-uu-mark-over
2041   "s" gnus-uu-mark-series
2042   "r" gnus-uu-mark-region
2043   "g" gnus-uu-unmark-region
2044   "R" gnus-uu-mark-by-regexp
2045   "G" gnus-uu-unmark-by-regexp
2046   "t" gnus-uu-mark-thread
2047   "T" gnus-uu-unmark-thread
2048   "a" gnus-uu-mark-all
2049   "b" gnus-uu-mark-buffer
2050   "S" gnus-uu-mark-sparse
2051   "k" gnus-summary-kill-process-mark
2052   "y" gnus-summary-yank-process-mark
2053   "w" gnus-summary-save-process-mark
2054   "i" gnus-uu-invert-processable)
2055
2056 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2057   ;;"x" gnus-uu-extract-any
2058   "m" gnus-summary-save-parts
2059   "u" gnus-uu-decode-uu
2060   "U" gnus-uu-decode-uu-and-save
2061   "s" gnus-uu-decode-unshar
2062   "S" gnus-uu-decode-unshar-and-save
2063   "o" gnus-uu-decode-save
2064   "O" gnus-uu-decode-save
2065   "b" gnus-uu-decode-binhex
2066   "B" gnus-uu-decode-binhex
2067   "p" gnus-uu-decode-postscript
2068   "P" gnus-uu-decode-postscript-and-save)
2069
2070 (gnus-define-keys
2071     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2072   "u" gnus-uu-decode-uu-view
2073   "U" gnus-uu-decode-uu-and-save-view
2074   "s" gnus-uu-decode-unshar-view
2075   "S" gnus-uu-decode-unshar-and-save-view
2076   "o" gnus-uu-decode-save-view
2077   "O" gnus-uu-decode-save-view
2078   "b" gnus-uu-decode-binhex-view
2079   "B" gnus-uu-decode-binhex-view
2080   "p" gnus-uu-decode-postscript-view
2081   "P" gnus-uu-decode-postscript-and-save-view)
2082
2083 (defvar gnus-article-post-menu nil)
2084
2085 (defconst gnus-summary-menu-maxlen 20)
2086
2087 (defun gnus-summary-menu-split (menu)
2088   ;; If we have lots of elements, divide them into groups of 20
2089   ;; and make a pane (or submenu) for each one.
2090   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2091       (let ((menu menu) sublists next
2092             (i 1))
2093         (while menu
2094           ;; Pull off the next gnus-summary-menu-maxlen elements
2095           ;; and make them the next element of sublist.
2096           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2097           (if next
2098               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2099                       nil))
2100           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2101                                              (aref (car (last menu)) 0)) menu)
2102                                sublists))
2103           (setq i (1+ i))
2104           (setq menu next))
2105         (nreverse sublists))
2106     ;; Few elements--put them all in one pane.
2107     menu))
2108
2109 (defun gnus-summary-make-menu-bar ()
2110   (gnus-turn-off-edit-menu 'summary)
2111
2112   (unless (boundp 'gnus-summary-misc-menu)
2113
2114     (easy-menu-define
2115       gnus-summary-kill-menu gnus-summary-mode-map ""
2116       (cons
2117        "Score"
2118        (nconc
2119         (list
2120          ["Customize" gnus-score-customize t])
2121         (gnus-make-score-map 'increase)
2122         (gnus-make-score-map 'lower)
2123         '(("Mark"
2124            ["Kill below" gnus-summary-kill-below t]
2125            ["Mark above" gnus-summary-mark-above t]
2126            ["Tick above" gnus-summary-tick-above t]
2127            ["Clear above" gnus-summary-clear-above t])
2128           ["Current score" gnus-summary-current-score t]
2129           ["Set score" gnus-summary-set-score t]
2130           ["Switch current score file..." gnus-score-change-score-file t]
2131           ["Set mark below..." gnus-score-set-mark-below t]
2132           ["Set expunge below..." gnus-score-set-expunge-below t]
2133           ["Edit current score file" gnus-score-edit-current-scores t]
2134           ["Edit score file" gnus-score-edit-file t]
2135           ["Trace score" gnus-score-find-trace t]
2136           ["Find words" gnus-score-find-favourite-words t]
2137           ["Rescore buffer" gnus-summary-rescore t]
2138           ["Increase score..." gnus-summary-increase-score t]
2139           ["Lower score..." gnus-summary-lower-score t]))))
2140
2141     ;; Define both the Article menu in the summary buffer and the
2142     ;; equivalent Commands menu in the article buffer here for
2143     ;; consistency.
2144     (let ((innards
2145            `(("Hide"
2146               ["All" gnus-article-hide t]
2147               ["Headers" gnus-article-hide-headers t]
2148               ["Signature" gnus-article-hide-signature t]
2149               ["Citation" gnus-article-hide-citation t]
2150               ["List identifiers" gnus-article-hide-list-identifiers t]
2151               ["Banner" gnus-article-strip-banner t]
2152               ["Boring headers" gnus-article-hide-boring-headers t])
2153              ("Highlight"
2154               ["All" gnus-article-highlight t]
2155               ["Headers" gnus-article-highlight-headers t]
2156               ["Signature" gnus-article-highlight-signature t]
2157               ["Citation" gnus-article-highlight-citation t])
2158              ("MIME"
2159               ["Words" gnus-article-decode-mime-words t]
2160               ["Charset" gnus-article-decode-charset t]
2161               ["QP" gnus-article-de-quoted-unreadable t]
2162               ["Base64" gnus-article-de-base64-unreadable t]
2163               ["View MIME buttons" gnus-summary-display-buttonized t]
2164               ["View all" gnus-mime-view-all-parts t]
2165               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2166               ["Encrypt body" gnus-article-encrypt-body
2167                :active (not (gnus-group-read-only-p))
2168                ,@(if (featurep 'xemacs) nil
2169                    '(:help "Encrypt the message body on disk"))]
2170               ["Extract all parts..." gnus-summary-save-parts t]
2171               ("Multipart"
2172                ["Repair multipart" gnus-summary-repair-multipart t]
2173                ["Pipe part..." gnus-article-pipe-part t]
2174                ["Inline part" gnus-article-inline-part t]
2175                ["Encrypt body" gnus-article-encrypt-body
2176                 :active (not (gnus-group-read-only-p))
2177                ,@(if (featurep 'xemacs) nil
2178                    '(:help "Encrypt the message body on disk"))]
2179                ["View part externally" gnus-article-view-part-externally t]
2180                ["View part with charset..." gnus-article-view-part-as-charset t]
2181                ["Copy part" gnus-article-copy-part t]
2182                ["Save part..." gnus-article-save-part t]
2183                ["View part" gnus-article-view-part t]))
2184              ("Date"
2185               ["Local" gnus-article-date-local t]
2186               ["ISO8601" gnus-article-date-iso8601 t]
2187               ["UT" gnus-article-date-ut t]
2188               ["Original" gnus-article-date-original t]
2189               ["Lapsed" gnus-article-date-lapsed t]
2190               ["User-defined" gnus-article-date-user t])
2191              ("Display"
2192               ["Remove images" gnus-article-remove-images t]
2193               ["Toggle smiley" gnus-treat-smiley t]
2194               ["Show X-Face" gnus-article-display-x-face t]
2195               ["Show picons in From" gnus-treat-from-picon t]
2196               ["Show picons in mail headers" gnus-treat-mail-picon t]
2197               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2198               ("View as different encoding"
2199                ,@(gnus-summary-menu-split
2200                   (mapcar
2201                    (lambda (cs)
2202                      ;; Since easymenu under Emacs doesn't allow
2203                      ;; lambda forms for menu commands, we should
2204                      ;; provide intern'ed function symbols.
2205                      (let ((command (intern (format "\
2206 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2207                        (fset command
2208                              `(lambda ()
2209                                 (interactive)
2210                                 (let ((gnus-summary-show-article-charset-alist
2211                                        '((1 . ,cs))))
2212                                   (gnus-summary-show-article 1))))
2213                        `[,(symbol-name cs) ,command t]))
2214                    (sort (if (fboundp 'coding-system-list)
2215                              (coding-system-list)
2216                            (mapcar 'car mm-mime-mule-charset-alist))
2217                          'string<)))))
2218              ("Washing"
2219               ("Remove Blanks"
2220                ["Leading" gnus-article-strip-leading-blank-lines t]
2221                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2222                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2223                ["All of the above" gnus-article-strip-blank-lines t]
2224                ["All" gnus-article-strip-all-blank-lines t]
2225                ["Leading space" gnus-article-strip-leading-space t]
2226                ["Trailing space" gnus-article-strip-trailing-space t]
2227                ["Leading space in headers"
2228                 gnus-article-remove-leading-whitespace t])
2229               ["Overstrike" gnus-article-treat-overstrike t]
2230               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2231               ["Emphasis" gnus-article-emphasize t]
2232               ["Word wrap" gnus-article-fill-cited-article t]
2233               ["Fill long lines" gnus-article-fill-long-lines t]
2234               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2235               ["Remove CR" gnus-article-remove-cr t]
2236               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2237               ["Base64" gnus-article-de-base64-unreadable t]
2238               ["Rot 13" gnus-summary-caesar-message
2239                ,@(if (featurep 'xemacs) '(t)
2240                    '(:help "\"Caesar rotate\" article by 13"))]
2241               ["Morse decode" gnus-summary-morse-message t]
2242               ["Unix pipe..." gnus-summary-pipe-message t]
2243               ["Add buttons" gnus-article-add-buttons t]
2244               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2245               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2246               ["Verbose header" gnus-summary-verbose-headers t]
2247               ["Toggle header" gnus-summary-toggle-header t]
2248               ["Unfold headers" gnus-article-treat-unfold-headers t]
2249               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2250               ["Html" gnus-article-wash-html t]
2251               ["Unsplit URLs" gnus-article-unsplit-urls t]
2252               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2253               ["Decode HZ" gnus-article-decode-HZ t]
2254               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2255               ("(Outlook) Deuglify"
2256                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2257                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2258                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2259                ["Full (Outlook) deuglify"
2260                 gnus-article-outlook-deuglify-article t])
2261               )
2262              ("Output"
2263               ["Save in default format..." gnus-summary-save-article
2264                ,@(if (featurep 'xemacs) '(t)
2265                    '(:help "Save article using default method"))]
2266               ["Save in file..." gnus-summary-save-article-file
2267                ,@(if (featurep 'xemacs) '(t)
2268                    '(:help "Save article in file"))]
2269               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2270               ["Save in MH folder..." gnus-summary-save-article-folder t]
2271               ["Save in VM folder..." gnus-summary-save-article-vm t]
2272               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2273               ["Save body in file..." gnus-summary-save-article-body-file t]
2274               ["Pipe through a filter..." gnus-summary-pipe-output t]
2275               ["Add to SOUP packet" gnus-soup-add-article t]
2276               ["Print with Muttprint..." gnus-summary-muttprint t]
2277               ["Print" gnus-summary-print-article t])
2278              ("Backend"
2279               ["Respool article..." gnus-summary-respool-article t]
2280               ["Move article..." gnus-summary-move-article
2281                (gnus-check-backend-function
2282                 'request-move-article gnus-newsgroup-name)]
2283               ["Copy article..." gnus-summary-copy-article t]
2284               ["Crosspost article..." gnus-summary-crosspost-article
2285                (gnus-check-backend-function
2286                 'request-replace-article gnus-newsgroup-name)]
2287               ["Import file..." gnus-summary-import-article
2288                (gnus-check-backend-function
2289                 'request-accept-article gnus-newsgroup-name)]
2290               ["Create article..." gnus-summary-create-article
2291                (gnus-check-backend-function
2292                 'request-accept-article gnus-newsgroup-name)]
2293               ["Check if posted" gnus-summary-article-posted-p t]
2294               ["Edit article" gnus-summary-edit-article
2295                (not (gnus-group-read-only-p))]
2296               ["Delete article" gnus-summary-delete-article
2297                (gnus-check-backend-function
2298                 'request-expire-articles gnus-newsgroup-name)]
2299               ["Query respool" gnus-summary-respool-query t]
2300               ["Trace respool" gnus-summary-respool-trace t]
2301               ["Delete expirable articles" gnus-summary-expire-articles-now
2302                (gnus-check-backend-function
2303                 'request-expire-articles gnus-newsgroup-name)])
2304              ("Extract"
2305               ["Uudecode" gnus-uu-decode-uu
2306                ,@(if (featurep 'xemacs) '(t)
2307                    '(:help "Decode uuencoded article(s)"))]
2308               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2309               ["Unshar" gnus-uu-decode-unshar t]
2310               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2311               ["Save" gnus-uu-decode-save t]
2312               ["Binhex" gnus-uu-decode-binhex t]
2313               ["Postscript" gnus-uu-decode-postscript t]
2314               ["All MIME parts" gnus-summary-save-parts t])
2315              ("Cache"
2316               ["Enter article" gnus-cache-enter-article t]
2317               ["Remove article" gnus-cache-remove-article t])
2318              ["Translate" gnus-article-babel t]
2319              ["Select article buffer" gnus-summary-select-article-buffer t]
2320              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2321              ["Isearch article..." gnus-summary-isearch-article t]
2322              ["Beginning of the article" gnus-summary-beginning-of-article t]
2323              ["End of the article" gnus-summary-end-of-article t]
2324              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2325              ["Fetch referenced articles" gnus-summary-refer-references t]
2326              ["Fetch current thread" gnus-summary-refer-thread t]
2327              ["Fetch article with id..." gnus-summary-refer-article t]
2328              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2329              ["Redisplay" gnus-summary-show-article t]
2330              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2331       (easy-menu-define
2332         gnus-summary-article-menu gnus-summary-mode-map ""
2333         (cons "Article" innards))
2334
2335       (if (not (keymapp gnus-summary-article-menu))
2336           (easy-menu-define
2337             gnus-article-commands-menu gnus-article-mode-map ""
2338             (cons "Commands" innards))
2339         ;; in Emacs, don't share menu.
2340         (setq gnus-article-commands-menu
2341               (copy-keymap gnus-summary-article-menu))
2342         (define-key gnus-article-mode-map [menu-bar commands]
2343           (cons "Commands" gnus-article-commands-menu))))
2344
2345     (easy-menu-define
2346       gnus-summary-thread-menu gnus-summary-mode-map ""
2347       '("Threads"
2348         ["Find all messages in thread" gnus-summary-refer-thread t]
2349         ["Toggle threading" gnus-summary-toggle-threads t]
2350         ["Hide threads" gnus-summary-hide-all-threads t]
2351         ["Show threads" gnus-summary-show-all-threads t]
2352         ["Hide thread" gnus-summary-hide-thread t]
2353         ["Show thread" gnus-summary-show-thread t]
2354         ["Go to next thread" gnus-summary-next-thread t]
2355         ["Go to previous thread" gnus-summary-prev-thread t]
2356         ["Go down thread" gnus-summary-down-thread t]
2357         ["Go up thread" gnus-summary-up-thread t]
2358         ["Top of thread" gnus-summary-top-thread t]
2359         ["Mark thread as read" gnus-summary-kill-thread t]
2360         ["Lower thread score" gnus-summary-lower-thread t]
2361         ["Raise thread score" gnus-summary-raise-thread t]
2362         ["Rethread current" gnus-summary-rethread-current t]))
2363
2364     (easy-menu-define
2365       gnus-summary-post-menu gnus-summary-mode-map ""
2366       `("Post"
2367         ["Send a message (mail or news)" gnus-summary-post-news
2368          ,@(if (featurep 'xemacs) '(t)
2369              '(:help "Post an article"))]
2370         ["Followup" gnus-summary-followup
2371          ,@(if (featurep 'xemacs) '(t)
2372              '(:help "Post followup to this article"))]
2373         ["Followup and yank" gnus-summary-followup-with-original
2374          ,@(if (featurep 'xemacs) '(t)
2375              '(:help "Post followup to this article, quoting its contents"))]
2376         ["Supersede article" gnus-summary-supersede-article t]
2377         ["Cancel article" gnus-summary-cancel-article
2378          ,@(if (featurep 'xemacs) '(t)
2379              '(:help "Cancel an article you posted"))]
2380         ["Reply" gnus-summary-reply t]
2381         ["Reply and yank" gnus-summary-reply-with-original t]
2382         ["Wide reply" gnus-summary-wide-reply t]
2383         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2384          ,@(if (featurep 'xemacs) '(t)
2385              '(:help "Mail a reply, quoting this article"))]
2386         ["Very wide reply" gnus-summary-very-wide-reply t]
2387         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2388          ,@(if (featurep 'xemacs) '(t)
2389              '(:help "Mail a very wide reply, quoting this article"))]
2390         ["Mail forward" gnus-summary-mail-forward t]
2391         ["Post forward" gnus-summary-post-forward t]
2392         ["Digest and mail" gnus-uu-digest-mail-forward t]
2393         ["Digest and post" gnus-uu-digest-post-forward t]
2394         ["Resend message" gnus-summary-resend-message t]
2395         ["Resend message edit" gnus-summary-resend-message-edit t]
2396         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2397         ["Send a mail" gnus-summary-mail-other-window t]
2398         ["Create a local message" gnus-summary-news-other-window t]
2399         ["Uuencode and post" gnus-uu-post-news
2400          ,@(if (featurep 'xemacs) '(t)
2401              '(:help "Post a uuencoded article"))]
2402         ["Followup via news" gnus-summary-followup-to-mail t]
2403         ["Followup via news and yank"
2404          gnus-summary-followup-to-mail-with-original t]
2405         ;;("Draft"
2406         ;;["Send" gnus-summary-send-draft t]
2407         ;;["Send bounced" gnus-resend-bounced-mail t])
2408         ))
2409
2410     (cond
2411      ((not (keymapp gnus-summary-post-menu))
2412       (setq gnus-article-post-menu gnus-summary-post-menu))
2413      ((not gnus-article-post-menu)
2414       ;; Don't share post menu.
2415       (setq gnus-article-post-menu
2416             (copy-keymap gnus-summary-post-menu))))
2417     (define-key gnus-article-mode-map [menu-bar post]
2418       (cons "Post" gnus-article-post-menu))
2419
2420     (easy-menu-define
2421       gnus-summary-misc-menu gnus-summary-mode-map ""
2422       `("Gnus"
2423         ("Mark Read"
2424          ["Mark as read" gnus-summary-mark-as-read-forward t]
2425          ["Mark same subject and select"
2426           gnus-summary-kill-same-subject-and-select t]
2427          ["Mark same subject" gnus-summary-kill-same-subject t]
2428          ["Catchup" gnus-summary-catchup
2429           ,@(if (featurep 'xemacs) '(t)
2430               '(:help "Mark unread articles in this group as read"))]
2431          ["Catchup all" gnus-summary-catchup-all t]
2432          ["Catchup to here" gnus-summary-catchup-to-here t]
2433          ["Catchup from here" gnus-summary-catchup-from-here t]
2434          ["Catchup region" gnus-summary-mark-region-as-read
2435           (gnus-mark-active-p)]
2436          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2437         ("Mark Various"
2438          ["Tick" gnus-summary-tick-article-forward t]
2439          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2440          ["Remove marks" gnus-summary-clear-mark-forward t]
2441          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2442          ["Set bookmark" gnus-summary-set-bookmark t]
2443          ["Remove bookmark" gnus-summary-remove-bookmark t])
2444         ("Limit to"
2445          ["Marks..." gnus-summary-limit-to-marks t]
2446          ["Subject..." gnus-summary-limit-to-subject t]
2447          ["Author..." gnus-summary-limit-to-author t]
2448          ["Age..." gnus-summary-limit-to-age t]
2449          ["Extra..." gnus-summary-limit-to-extra t]
2450          ["Score..." gnus-summary-limit-to-score t]
2451          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2452          ["Unread" gnus-summary-limit-to-unread t]
2453          ["Unseen" gnus-summary-limit-to-unseen t]
2454          ["Replied" gnus-summary-limit-to-replied t]
2455          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2456          ["Next articles" gnus-summary-limit-to-articles t]
2457          ["Pop limit" gnus-summary-pop-limit t]
2458          ["Show dormant" gnus-summary-limit-include-dormant t]
2459          ["Hide childless dormant"
2460           gnus-summary-limit-exclude-childless-dormant t]
2461          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2462          ["Hide marked" gnus-summary-limit-exclude-marks t]
2463          ["Show expunged" gnus-summary-limit-include-expunged t])
2464         ("Process Mark"
2465          ["Set mark" gnus-summary-mark-as-processable t]
2466          ["Remove mark" gnus-summary-unmark-as-processable t]
2467          ["Remove all marks" gnus-summary-unmark-all-processable t]
2468          ["Mark above" gnus-uu-mark-over t]
2469          ["Mark series" gnus-uu-mark-series t]
2470          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2471          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2472          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2473          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2474          ["Mark all" gnus-uu-mark-all t]
2475          ["Mark buffer" gnus-uu-mark-buffer t]
2476          ["Mark sparse" gnus-uu-mark-sparse t]
2477          ["Mark thread" gnus-uu-mark-thread t]
2478          ["Unmark thread" gnus-uu-unmark-thread t]
2479          ("Process Mark Sets"
2480           ["Kill" gnus-summary-kill-process-mark t]
2481           ["Yank" gnus-summary-yank-process-mark
2482            gnus-newsgroup-process-stack]
2483           ["Save" gnus-summary-save-process-mark t]
2484           ["Run command on marked..." gnus-summary-universal-argument t]))
2485         ("Scroll article"
2486          ["Page forward" gnus-summary-next-page
2487           ,@(if (featurep 'xemacs) '(t)
2488               '(:help "Show next page of article"))]
2489          ["Page backward" gnus-summary-prev-page
2490           ,@(if (featurep 'xemacs) '(t)
2491               '(:help "Show previous page of article"))]
2492          ["Line forward" gnus-summary-scroll-up t])
2493         ("Move"
2494          ["Next unread article" gnus-summary-next-unread-article t]
2495          ["Previous unread article" gnus-summary-prev-unread-article t]
2496          ["Next article" gnus-summary-next-article t]
2497          ["Previous article" gnus-summary-prev-article t]
2498          ["Next unread subject" gnus-summary-next-unread-subject t]
2499          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2500          ["Next article same subject" gnus-summary-next-same-subject t]
2501          ["Previous article same subject" gnus-summary-prev-same-subject t]
2502          ["First unread article" gnus-summary-first-unread-article t]
2503          ["Best unread article" gnus-summary-best-unread-article t]
2504          ["Go to subject number..." gnus-summary-goto-subject t]
2505          ["Go to article number..." gnus-summary-goto-article t]
2506          ["Go to the last article" gnus-summary-goto-last-article t]
2507          ["Pop article off history" gnus-summary-pop-article t])
2508         ("Sort"
2509          ["Sort by number" gnus-summary-sort-by-number t]
2510          ["Sort by author" gnus-summary-sort-by-author t]
2511          ["Sort by subject" gnus-summary-sort-by-subject t]
2512          ["Sort by date" gnus-summary-sort-by-date t]
2513          ["Sort by score" gnus-summary-sort-by-score t]
2514          ["Sort by lines" gnus-summary-sort-by-lines t]
2515          ["Sort by characters" gnus-summary-sort-by-chars t]
2516          ["Randomize" gnus-summary-sort-by-random t]
2517          ["Original sort" gnus-summary-sort-by-original t])
2518         ("Help"
2519          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2520          ["Describe group" gnus-summary-describe-group t]
2521          ["Fetch charter" gnus-group-fetch-charter
2522           ,@(if (featurep 'xemacs) nil
2523               '(:help "Display the charter of the current group"))]
2524          ["Fetch control message" gnus-group-fetch-control
2525           ,@(if (featurep 'xemacs) nil
2526               '(:help "Display the archived control message for the current group"))]
2527          ["Read manual" gnus-info-find-node t])
2528         ("Modes"
2529          ["Pick and read" gnus-pick-mode t]
2530          ["Binary" gnus-binary-mode t])
2531         ("Regeneration"
2532          ["Regenerate" gnus-summary-prepare t]
2533          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2534          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2535          ["Toggle threading" gnus-summary-toggle-threads t])
2536         ["See old articles" gnus-summary-insert-old-articles t]
2537         ["See new articles" gnus-summary-insert-new-articles t]
2538         ["Filter articles..." gnus-summary-execute-command t]
2539         ["Run command on articles..." gnus-summary-universal-argument t]
2540         ["Search articles forward..." gnus-summary-search-article-forward t]
2541         ["Search articles backward..." gnus-summary-search-article-backward t]
2542         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2543         ["Expand window" gnus-summary-expand-window t]
2544         ["Expire expirable articles" gnus-summary-expire-articles
2545          (gnus-check-backend-function
2546           'request-expire-articles gnus-newsgroup-name)]
2547         ["Edit local kill file" gnus-summary-edit-local-kill t]
2548         ["Edit main kill file" gnus-summary-edit-global-kill t]
2549         ["Edit group parameters" gnus-summary-edit-parameters t]
2550         ["Customize group parameters" gnus-summary-customize-parameters t]
2551         ["Send a bug report" gnus-bug t]
2552         ("Exit"
2553          ["Catchup and exit" gnus-summary-catchup-and-exit
2554           ,@(if (featurep 'xemacs) '(t)
2555               '(:help "Mark unread articles in this group as read, then exit"))]
2556          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2557          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2558          ["Exit group" gnus-summary-exit
2559           ,@(if (featurep 'xemacs) '(t)
2560               '(:help "Exit current group, return to group selection mode"))]
2561          ["Exit group without updating" gnus-summary-exit-no-update t]
2562          ["Exit and goto next group" gnus-summary-next-group t]
2563          ["Exit and goto prev group" gnus-summary-prev-group t]
2564          ["Reselect group" gnus-summary-reselect-current-group t]
2565          ["Rescan group" gnus-summary-rescan-group t]
2566          ["Update dribble" gnus-summary-save-newsrc t])))
2567
2568     (gnus-run-hooks 'gnus-summary-menu-hook)))
2569
2570 (defvar gnus-summary-tool-bar-map nil)
2571
2572 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2573 (defun gnus-summary-make-tool-bar ()
2574   (if (and (fboundp 'tool-bar-add-item-from-menu)
2575            (default-value 'tool-bar-mode)
2576            (not gnus-summary-tool-bar-map))
2577       (setq gnus-summary-tool-bar-map
2578             (let ((tool-bar-map (make-sparse-keymap))
2579                   (load-path (mm-image-load-path)))
2580               (tool-bar-add-item-from-menu
2581                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2582               (tool-bar-add-item-from-menu
2583                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2584               (tool-bar-add-item-from-menu
2585                'gnus-summary-post-news "post" gnus-summary-mode-map)
2586               (tool-bar-add-item-from-menu
2587                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-followup "followup" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-reply "reply" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2610               tool-bar-map)))
2611   (if gnus-summary-tool-bar-map
2612       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2613
2614 (defun gnus-score-set-default (var value)
2615   "A version of set that updates the GNU Emacs menu-bar."
2616   (set var value)
2617   ;; It is the message that forces the active status to be updated.
2618   (message ""))
2619
2620 (defun gnus-make-score-map (type)
2621   "Make a summary score map of type TYPE."
2622   (if t
2623       nil
2624     (let ((headers '(("author" "from" string)
2625                      ("subject" "subject" string)
2626                      ("article body" "body" string)
2627                      ("article head" "head" string)
2628                      ("xref" "xref" string)
2629                      ("extra header" "extra" string)
2630                      ("lines" "lines" number)
2631                      ("followups to author" "followup" string)))
2632           (types '((number ("less than" <)
2633                            ("greater than" >)
2634                            ("equal" =))
2635                    (string ("substring" s)
2636                            ("exact string" e)
2637                            ("fuzzy string" f)
2638                            ("regexp" r))))
2639           (perms '(("temporary" (current-time-string))
2640                    ("permanent" nil)
2641                    ("immediate" now)))
2642           header)
2643       (list
2644        (apply
2645         'nconc
2646         (list
2647          (if (eq type 'lower)
2648              "Lower score"
2649            "Increase score"))
2650         (let (outh)
2651           (while headers
2652             (setq header (car headers))
2653             (setq outh
2654                   (cons
2655                    (apply
2656                     'nconc
2657                     (list (car header))
2658                     (let ((ts (cdr (assoc (nth 2 header) types)))
2659                           outt)
2660                       (while ts
2661                         (setq outt
2662                               (cons
2663                                (apply
2664                                 'nconc
2665                                 (list (caar ts))
2666                                 (let ((ps perms)
2667                                       outp)
2668                                   (while ps
2669                                     (setq outp
2670                                           (cons
2671                                            (vector
2672                                             (caar ps)
2673                                             (list
2674                                              'gnus-summary-score-entry
2675                                              (nth 1 header)
2676                                              (if (or (string= (nth 1 header)
2677                                                               "head")
2678                                                      (string= (nth 1 header)
2679                                                               "body"))
2680                                                  ""
2681                                                (list 'gnus-summary-header
2682                                                      (nth 1 header)))
2683                                              (list 'quote (nth 1 (car ts)))
2684                                              (list 'gnus-score-delta-default
2685                                                    nil)
2686                                              (nth 1 (car ps))
2687                                              t)
2688                                             t)
2689                                            outp))
2690                                     (setq ps (cdr ps)))
2691                                   (list (nreverse outp))))
2692                                outt))
2693                         (setq ts (cdr ts)))
2694                       (list (nreverse outt))))
2695                    outh))
2696             (setq headers (cdr headers)))
2697           (list (nreverse outh))))))))
2698
2699 \f
2700
2701 (defun gnus-summary-mode (&optional group)
2702   "Major mode for reading articles.
2703
2704 All normal editing commands are switched off.
2705 \\<gnus-summary-mode-map>
2706 Each line in this buffer represents one article.  To read an
2707 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2708 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2709 respectively.
2710
2711 You can also post articles and send mail from this buffer.  To
2712 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2713 of an article, type `\\[gnus-summary-reply]'.
2714
2715 There are approx. one gazillion commands you can execute in this
2716 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2717
2718 The following commands are available:
2719
2720 \\{gnus-summary-mode-map}"
2721   (interactive)
2722   (kill-all-local-variables)
2723   (when (gnus-visual-p 'summary-menu 'menu)
2724     (gnus-summary-make-menu-bar)
2725     (gnus-summary-make-tool-bar))
2726   (gnus-summary-make-local-variables)
2727   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2728     (gnus-summary-make-local-variables))
2729   (gnus-make-thread-indent-array)
2730   (gnus-simplify-mode-line)
2731   (setq major-mode 'gnus-summary-mode)
2732   (setq mode-name "Summary")
2733   (make-local-variable 'minor-mode-alist)
2734   (use-local-map gnus-summary-mode-map)
2735   (buffer-disable-undo)
2736   (setq buffer-read-only t)             ;Disable modification
2737   (setq truncate-lines t)
2738   (setq selective-display t)
2739   (setq selective-display-ellipses t)   ;Display `...'
2740   (gnus-summary-set-display-table)
2741   (gnus-set-default-directory)
2742   (setq gnus-newsgroup-name group)
2743   (make-local-variable 'gnus-summary-line-format)
2744   (make-local-variable 'gnus-summary-line-format-spec)
2745   (make-local-variable 'gnus-summary-dummy-line-format)
2746   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2747   (make-local-variable 'gnus-summary-mark-positions)
2748   (gnus-make-local-hook 'pre-command-hook)
2749   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2750   (gnus-run-hooks 'gnus-summary-mode-hook)
2751   (turn-on-gnus-mailing-list-mode)
2752   (mm-enable-multibyte)
2753   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2754   (gnus-update-summary-mark-positions))
2755
2756 (defun gnus-summary-make-local-variables ()
2757   "Make all the local summary buffer variables."
2758   (let (global)
2759     (dolist (local gnus-summary-local-variables)
2760       (if (consp local)
2761           (progn
2762             (if (eq (cdr local) 'global)
2763                 ;; Copy the global value of the variable.
2764                 (setq global (symbol-value (car local)))
2765               ;; Use the value from the list.
2766               (setq global (eval (cdr local))))
2767             (set (make-local-variable (car local)) global))
2768         ;; Simple nil-valued local variable.
2769         (set (make-local-variable local) nil)))))
2770
2771 (defun gnus-summary-clear-local-variables ()
2772   (let ((locals gnus-summary-local-variables))
2773     (while locals
2774       (if (consp (car locals))
2775           (and (vectorp (caar locals))
2776                (set (caar locals) nil))
2777         (and (vectorp (car locals))
2778              (set (car locals) nil)))
2779       (setq locals (cdr locals)))))
2780
2781 ;; Summary data functions.
2782
2783 (defmacro gnus-data-number (data)
2784   `(car ,data))
2785
2786 (defmacro gnus-data-set-number (data number)
2787   `(setcar ,data ,number))
2788
2789 (defmacro gnus-data-mark (data)
2790   `(nth 1 ,data))
2791
2792 (defmacro gnus-data-set-mark (data mark)
2793   `(setcar (nthcdr 1 ,data) ,mark))
2794
2795 (defmacro gnus-data-pos (data)
2796   `(nth 2 ,data))
2797
2798 (defmacro gnus-data-set-pos (data pos)
2799   `(setcar (nthcdr 2 ,data) ,pos))
2800
2801 (defmacro gnus-data-header (data)
2802   `(nth 3 ,data))
2803
2804 (defmacro gnus-data-set-header (data header)
2805   `(setf (nth 3 ,data) ,header))
2806
2807 (defmacro gnus-data-level (data)
2808   `(nth 4 ,data))
2809
2810 (defmacro gnus-data-unread-p (data)
2811   `(= (nth 1 ,data) gnus-unread-mark))
2812
2813 (defmacro gnus-data-read-p (data)
2814   `(/= (nth 1 ,data) gnus-unread-mark))
2815
2816 (defmacro gnus-data-pseudo-p (data)
2817   `(consp (nth 3 ,data)))
2818
2819 (defmacro gnus-data-find (number)
2820   `(assq ,number gnus-newsgroup-data))
2821
2822 (defmacro gnus-data-find-list (number &optional data)
2823   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2824      (memq (assq ,number bdata)
2825            bdata)))
2826
2827 (defmacro gnus-data-make (number mark pos header level)
2828   `(list ,number ,mark ,pos ,header ,level))
2829
2830 (defun gnus-data-enter (after-article number mark pos header level offset)
2831   (let ((data (gnus-data-find-list after-article)))
2832     (unless data
2833       (error "No such article: %d" after-article))
2834     (setcdr data (cons (gnus-data-make number mark pos header level)
2835                        (cdr data)))
2836     (setq gnus-newsgroup-data-reverse nil)
2837     (gnus-data-update-list (cddr data) offset)))
2838
2839 (defun gnus-data-enter-list (after-article list &optional offset)
2840   (when list
2841     (let ((data (and after-article (gnus-data-find-list after-article)))
2842           (ilist list))
2843       (if (not (or data
2844                    after-article))
2845           (let ((odata gnus-newsgroup-data))
2846             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2847             (when offset
2848               (gnus-data-update-list odata offset)))
2849       ;; Find the last element in the list to be spliced into the main
2850         ;; list.
2851         (while (cdr list)
2852           (setq list (cdr list)))
2853         (if (not data)
2854             (progn
2855               (setcdr list gnus-newsgroup-data)
2856               (setq gnus-newsgroup-data ilist)
2857               (when offset
2858                 (gnus-data-update-list (cdr list) offset)))
2859           (setcdr list (cdr data))
2860           (setcdr data ilist)
2861           (when offset
2862             (gnus-data-update-list (cdr list) offset))))
2863       (setq gnus-newsgroup-data-reverse nil))))
2864
2865 (defun gnus-data-remove (article &optional offset)
2866   (let ((data gnus-newsgroup-data))
2867     (if (= (gnus-data-number (car data)) article)
2868         (progn
2869           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2870                 gnus-newsgroup-data-reverse nil)
2871           (when offset
2872             (gnus-data-update-list gnus-newsgroup-data offset)))
2873       (while (cdr data)
2874         (when (= (gnus-data-number (cadr data)) article)
2875           (setcdr data (cddr data))
2876           (when offset
2877             (gnus-data-update-list (cdr data) offset))
2878           (setq data nil
2879                 gnus-newsgroup-data-reverse nil))
2880         (setq data (cdr data))))))
2881
2882 (defmacro gnus-data-list (backward)
2883   `(if ,backward
2884        (or gnus-newsgroup-data-reverse
2885            (setq gnus-newsgroup-data-reverse
2886                  (reverse gnus-newsgroup-data)))
2887      gnus-newsgroup-data))
2888
2889 (defun gnus-data-update-list (data offset)
2890   "Add OFFSET to the POS of all data entries in DATA."
2891   (setq gnus-newsgroup-data-reverse nil)
2892   (while data
2893     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2894     (setq data (cdr data))))
2895
2896 (defun gnus-summary-article-pseudo-p (article)
2897   "Say whether this article is a pseudo article or not."
2898   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2899
2900 (defmacro gnus-summary-article-sparse-p (article)
2901   "Say whether this article is a sparse article or not."
2902   `(memq ,article gnus-newsgroup-sparse))
2903
2904 (defmacro gnus-summary-article-ancient-p (article)
2905   "Say whether this article is a sparse article or not."
2906   `(memq ,article gnus-newsgroup-ancient))
2907
2908 (defun gnus-article-parent-p (number)
2909   "Say whether this article is a parent or not."
2910   (let ((data (gnus-data-find-list number)))
2911     (and (cdr data)              ; There has to be an article after...
2912          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2913             (gnus-data-level (nth 1 data))))))
2914
2915 (defun gnus-article-children (number)
2916   "Return a list of all children to NUMBER."
2917   (let* ((data (gnus-data-find-list number))
2918          (level (gnus-data-level (car data)))
2919          children)
2920     (setq data (cdr data))
2921     (while (and data
2922                 (= (gnus-data-level (car data)) (1+ level)))
2923       (push (gnus-data-number (car data)) children)
2924       (setq data (cdr data)))
2925     children))
2926
2927 (defmacro gnus-summary-skip-intangible ()
2928   "If the current article is intangible, then jump to a different article."
2929   '(let ((to (get-text-property (point) 'gnus-intangible)))
2930      (and to (gnus-summary-goto-subject to))))
2931
2932 (defmacro gnus-summary-article-intangible-p ()
2933   "Say whether this article is intangible or not."
2934   '(get-text-property (point) 'gnus-intangible))
2935
2936 (defun gnus-article-read-p (article)
2937   "Say whether ARTICLE is read or not."
2938   (not (or (memq article gnus-newsgroup-marked)
2939            (memq article gnus-newsgroup-spam-marked)
2940            (memq article gnus-newsgroup-unreads)
2941            (memq article gnus-newsgroup-unselected)
2942            (memq article gnus-newsgroup-dormant))))
2943
2944 ;; Some summary mode macros.
2945
2946 (defmacro gnus-summary-article-number ()
2947   "The article number of the article on the current line.
2948 If there isn't an article number here, then we return the current
2949 article number."
2950   '(progn
2951      (gnus-summary-skip-intangible)
2952      (or (get-text-property (point) 'gnus-number)
2953          (gnus-summary-last-subject))))
2954
2955 (defmacro gnus-summary-article-header (&optional number)
2956   "Return the header of article NUMBER."
2957   `(gnus-data-header (gnus-data-find
2958                       ,(or number '(gnus-summary-article-number)))))
2959
2960 (defmacro gnus-summary-thread-level (&optional number)
2961   "Return the level of thread that starts with article NUMBER."
2962   `(if (and (eq gnus-summary-make-false-root 'dummy)
2963             (get-text-property (point) 'gnus-intangible))
2964        0
2965      (gnus-data-level (gnus-data-find
2966                        ,(or number '(gnus-summary-article-number))))))
2967
2968 (defmacro gnus-summary-article-mark (&optional number)
2969   "Return the mark of article NUMBER."
2970   `(gnus-data-mark (gnus-data-find
2971                     ,(or number '(gnus-summary-article-number)))))
2972
2973 (defmacro gnus-summary-article-pos (&optional number)
2974   "Return the position of the line of article NUMBER."
2975   `(gnus-data-pos (gnus-data-find
2976                    ,(or number '(gnus-summary-article-number)))))
2977
2978 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2979 (defmacro gnus-summary-article-subject (&optional number)
2980   "Return current subject string or nil if nothing."
2981   `(let ((headers
2982           ,(if number
2983                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2984              '(gnus-data-header (assq (gnus-summary-article-number)
2985                                       gnus-newsgroup-data)))))
2986      (and headers
2987           (vectorp headers)
2988           (mail-header-subject headers))))
2989
2990 (defmacro gnus-summary-article-score (&optional number)
2991   "Return current article score."
2992   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2993                   gnus-newsgroup-scored))
2994        gnus-summary-default-score 0))
2995
2996 (defun gnus-summary-article-children (&optional number)
2997   "Return a list of article numbers that are children of article NUMBER."
2998   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2999          (level (gnus-data-level (car data)))
3000          l children)
3001     (while (and (setq data (cdr data))
3002                 (> (setq l (gnus-data-level (car data))) level))
3003       (and (= (1+ level) l)
3004            (push (gnus-data-number (car data))
3005                  children)))
3006     (nreverse children)))
3007
3008 (defun gnus-summary-article-parent (&optional number)
3009   "Return the article number of the parent of article NUMBER."
3010   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3011                                     (gnus-data-list t)))
3012          (level (gnus-data-level (car data))))
3013     (if (zerop level)
3014         ()                              ; This is a root.
3015       ;; We search until we find an article with a level less than
3016       ;; this one.  That function has to be the parent.
3017       (while (and (setq data (cdr data))
3018                   (not (< (gnus-data-level (car data)) level))))
3019       (and data (gnus-data-number (car data))))))
3020
3021 (defun gnus-unread-mark-p (mark)
3022   "Say whether MARK is the unread mark."
3023   (= mark gnus-unread-mark))
3024
3025 (defun gnus-read-mark-p (mark)
3026   "Say whether MARK is one of the marks that mark as read.
3027 This is all marks except unread, ticked, dormant, and expirable."
3028   (not (or (= mark gnus-unread-mark)
3029            (= mark gnus-ticked-mark)
3030            (= mark gnus-spam-mark)
3031            (= mark gnus-dormant-mark)
3032            (= mark gnus-expirable-mark))))
3033
3034 (defmacro gnus-article-mark (number)
3035   "Return the MARK of article NUMBER.
3036 This macro should only be used when computing the mark the \"first\"
3037 time; i.e., when generating the summary lines.  After that,
3038 `gnus-summary-article-mark' should be used to examine the
3039 marks of articles."
3040   `(cond
3041     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3042     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3043     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3044     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3045     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3046     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3047     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3048     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3049            gnus-ancient-mark))))
3050
3051 ;; Saving hidden threads.
3052
3053 (defmacro gnus-save-hidden-threads (&rest forms)
3054   "Save hidden threads, eval FORMS, and restore the hidden threads."
3055   (let ((config (make-symbol "config")))
3056     `(let ((,config (gnus-hidden-threads-configuration)))
3057        (unwind-protect
3058            (save-excursion
3059              ,@forms)
3060          (gnus-restore-hidden-threads-configuration ,config)))))
3061 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3062 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3063
3064 (defun gnus-data-compute-positions ()
3065   "Compute the positions of all articles."
3066   (setq gnus-newsgroup-data-reverse nil)
3067   (let ((data gnus-newsgroup-data))
3068     (save-excursion
3069       (gnus-save-hidden-threads
3070         (gnus-summary-show-all-threads)
3071         (goto-char (point-min))
3072         (while data
3073           (while (get-text-property (point) 'gnus-intangible)
3074             (forward-line 1))
3075           (gnus-data-set-pos (car data) (+ (point) 3))
3076           (setq data (cdr data))
3077           (forward-line 1))))))
3078
3079 (defun gnus-hidden-threads-configuration ()
3080   "Return the current hidden threads configuration."
3081   (save-excursion
3082     (let (config)
3083       (goto-char (point-min))
3084       (while (search-forward "\r" nil t)
3085         (push (1- (point)) config))
3086       config)))
3087
3088 (defun gnus-restore-hidden-threads-configuration (config)
3089   "Restore hidden threads configuration from CONFIG."
3090   (save-excursion
3091     (let (point buffer-read-only)
3092       (while (setq point (pop config))
3093         (when (and (< point (point-max))
3094                    (goto-char point)
3095                    (eq (char-after) ?\n))
3096           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3097
3098 ;; Various summary mode internalish functions.
3099
3100 (defun gnus-mouse-pick-article (e)
3101   (interactive "e")
3102   (mouse-set-point e)
3103   (gnus-summary-next-page nil t))
3104
3105 (defun gnus-summary-set-display-table ()
3106   "Change the display table.
3107 Odd characters have a tendency to mess
3108 up nicely formatted displays - we make all possible glyphs
3109 display only a single character."
3110
3111   ;; We start from the standard display table, if any.
3112   (let ((table (or (copy-sequence standard-display-table)
3113                    (make-display-table)))
3114         (i 32))
3115     ;; Nix out all the control chars...
3116     (while (>= (setq i (1- i)) 0)
3117       (aset table i [??]))
3118    ;; ... but not newline and cr, of course.  (cr is necessary for the
3119     ;; selective display).
3120     (aset table ?\n nil)
3121     (aset table ?\r nil)
3122     ;; We keep TAB as well.
3123     (aset table ?\t nil)
3124     ;; We nix out any glyphs over 126 that are not set already.
3125     (let ((i 256))
3126       (while (>= (setq i (1- i)) 127)
3127         ;; Only modify if the entry is nil.
3128         (unless (aref table i)
3129           (aset table i [??]))))
3130     (setq buffer-display-table table)))
3131
3132 (defun gnus-summary-set-article-display-arrow (pos)
3133   "Update the overlay arrow to point to line at position POS."
3134   (when (and gnus-summary-display-arrow
3135              (boundp 'overlay-arrow-position)
3136              (boundp 'overlay-arrow-string))
3137     (save-excursion
3138       (goto-char pos)
3139       (beginning-of-line)
3140       (unless overlay-arrow-position
3141         (setq overlay-arrow-position (make-marker)))
3142       (setq overlay-arrow-string "=>"
3143             overlay-arrow-position (set-marker overlay-arrow-position
3144                                                (point)
3145                                                (current-buffer))))))
3146
3147 (defun gnus-summary-setup-buffer (group)
3148   "Initialize summary buffer."
3149   (let ((buffer (gnus-summary-buffer-name group))
3150         (dead-name (concat "*Dead Summary "
3151                            (gnus-group-decoded-name group) "*")))
3152     ;; If a dead summary buffer exists, we kill it.
3153     (when (gnus-buffer-live-p dead-name)
3154       (gnus-kill-buffer dead-name))
3155     (if (get-buffer buffer)
3156         (progn
3157           (set-buffer buffer)
3158           (setq gnus-summary-buffer (current-buffer))
3159           (not gnus-newsgroup-prepared))
3160       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3161       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3162       (gnus-summary-mode group)
3163       (when gnus-carpal
3164         (gnus-carpal-setup-buffer 'summary))
3165       (unless gnus-single-article-buffer
3166         (make-local-variable 'gnus-article-buffer)
3167         (make-local-variable 'gnus-article-current)
3168         (make-local-variable 'gnus-original-article-buffer))
3169       (setq gnus-newsgroup-name group)
3170       ;; Set any local variables in the group parameters.
3171       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3172       t)))
3173
3174 (defun gnus-set-global-variables ()
3175   "Set the global equivalents of the buffer-local variables.
3176 They are set to the latest values they had.  These reflect the summary
3177 buffer that was in action when the last article was fetched."
3178   (when (eq major-mode 'gnus-summary-mode)
3179     (setq gnus-summary-buffer (current-buffer))
3180     (let ((name gnus-newsgroup-name)
3181           (marked gnus-newsgroup-marked)
3182           (spam gnus-newsgroup-spam-marked)
3183           (unread gnus-newsgroup-unreads)
3184           (headers gnus-current-headers)
3185           (data gnus-newsgroup-data)
3186           (summary gnus-summary-buffer)
3187           (article-buffer gnus-article-buffer)
3188           (original gnus-original-article-buffer)
3189           (gac gnus-article-current)
3190           (reffed gnus-reffed-article-number)
3191           (score-file gnus-current-score-file)
3192           (default-charset gnus-newsgroup-charset)
3193           vlist)
3194       (let ((locals gnus-newsgroup-variables))
3195         (while locals
3196           (if (consp (car locals))
3197               (push (eval (caar locals)) vlist)
3198             (push (eval (car locals)) vlist))
3199           (setq locals (cdr locals)))
3200         (setq vlist (nreverse vlist)))
3201       (save-excursion
3202         (set-buffer gnus-group-buffer)
3203         (setq gnus-newsgroup-name name
3204               gnus-newsgroup-marked marked
3205               gnus-newsgroup-spam-marked spam
3206               gnus-newsgroup-unreads unread
3207               gnus-current-headers headers
3208               gnus-newsgroup-data data
3209               gnus-article-current gac
3210               gnus-summary-buffer summary
3211               gnus-article-buffer article-buffer
3212               gnus-original-article-buffer original
3213               gnus-reffed-article-number reffed
3214               gnus-current-score-file score-file
3215               gnus-newsgroup-charset default-charset)
3216         (let ((locals gnus-newsgroup-variables))
3217           (while locals
3218             (if (consp (car locals))
3219                 (set (caar locals) (pop vlist))
3220               (set (car locals) (pop vlist)))
3221             (setq locals (cdr locals))))
3222         ;; The article buffer also has local variables.
3223         (when (gnus-buffer-live-p gnus-article-buffer)
3224           (set-buffer gnus-article-buffer)
3225           (setq gnus-summary-buffer summary))))))
3226
3227 (defun gnus-summary-article-unread-p (article)
3228   "Say whether ARTICLE is unread or not."
3229   (memq article gnus-newsgroup-unreads))
3230
3231 (defun gnus-summary-first-article-p (&optional article)
3232   "Return whether ARTICLE is the first article in the buffer."
3233   (if (not (setq article (or article (gnus-summary-article-number))))
3234       nil
3235     (eq article (caar gnus-newsgroup-data))))
3236
3237 (defun gnus-summary-last-article-p (&optional article)
3238   "Return whether ARTICLE is the last article in the buffer."
3239   (if (not (setq article (or article (gnus-summary-article-number))))
3240       ;; All non-existent numbers are the last article.  :-)
3241       t
3242     (not (cdr (gnus-data-find-list article)))))
3243
3244 (defun gnus-make-thread-indent-array ()
3245   (let ((n 200))
3246     (unless (and gnus-thread-indent-array
3247                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3248       (setq gnus-thread-indent-array (make-vector 201 "")
3249             gnus-thread-indent-array-level gnus-thread-indent-level)
3250       (while (>= n 0)
3251         (aset gnus-thread-indent-array n
3252               (make-string (* n gnus-thread-indent-level) ? ))
3253         (setq n (1- n))))))
3254
3255 (defun gnus-update-summary-mark-positions ()
3256   "Compute where the summary marks are to go."
3257   (save-excursion
3258     (when (gnus-buffer-exists-p gnus-summary-buffer)
3259       (set-buffer gnus-summary-buffer))
3260     (let ((gnus-replied-mark 129)
3261           (gnus-score-below-mark 130)
3262           (gnus-score-over-mark 130)
3263           (gnus-undownloaded-mark 131)
3264           (spec gnus-summary-line-format-spec)
3265           gnus-visual pos)
3266       (save-excursion
3267         (gnus-set-work-buffer)
3268         (let ((gnus-summary-line-format-spec spec)
3269               (gnus-newsgroup-downloadable '(0)))
3270           (gnus-summary-insert-line
3271            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3272            0 nil t 128 t nil "" nil 1)
3273           (goto-char (point-min))
3274           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3275                                              (- (point) (point-min) 1)))))
3276           (goto-char (point-min))
3277           (push (cons 'replied (and (search-forward "\201" nil t)
3278                                     (- (point) (point-min) 1)))
3279                 pos)
3280           (goto-char (point-min))
3281           (push (cons 'score (and (search-forward "\202" nil t)
3282                                   (- (point) (point-min) 1)))
3283                 pos)
3284           (goto-char (point-min))
3285           (push (cons 'download
3286                       (and (search-forward "\203" nil t)
3287                            (- (point) (point-min) 1)))
3288                 pos)))
3289       (setq gnus-summary-mark-positions pos))))
3290
3291 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3292   "Insert a dummy root in the summary buffer."
3293   (beginning-of-line)
3294   (gnus-add-text-properties
3295    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3296    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3297
3298 (defun gnus-summary-extract-address-component (from)
3299   (or (car (funcall gnus-extract-address-components from))
3300       from))
3301
3302 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3303   (let ((mail-parse-charset gnus-newsgroup-charset)
3304         ; Is it really necessary to do this next part for each summary line?
3305         ; Luckily, doesn't seem to slow things down much.
3306         (mail-parse-ignored-charsets
3307          (with-current-buffer gnus-summary-buffer
3308            gnus-newsgroup-ignored-charsets)))
3309     (or
3310      (and gnus-ignored-from-addresses
3311           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3312           (let ((extra-headers (mail-header-extra header))
3313                 to
3314                 newsgroups)
3315             (cond
3316              ((setq to (cdr (assq 'To extra-headers)))
3317               (concat "-> "
3318                       (inline
3319                         (gnus-summary-extract-address-component
3320                          (funcall gnus-decode-encoded-word-function to)))))
3321              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3322               (concat "=> " newsgroups)))))
3323      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3324
3325 (defun gnus-summary-insert-line (gnus-tmp-header
3326                                  gnus-tmp-level gnus-tmp-current
3327                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3328                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3329                                  &optional gnus-tmp-dummy gnus-tmp-score
3330                                  gnus-tmp-process)
3331   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3332          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3333          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3334          (gnus-tmp-score-char
3335           (if (or (null gnus-summary-default-score)
3336                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3337                       gnus-summary-zcore-fuzz))
3338               ?                         ;Whitespace
3339             (if (< gnus-tmp-score gnus-summary-default-score)
3340                 gnus-score-below-mark gnus-score-over-mark)))
3341          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3342          (gnus-tmp-replied
3343           (cond (gnus-tmp-process gnus-process-mark)
3344                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3345                  gnus-cached-mark)
3346                 (gnus-tmp-replied gnus-replied-mark)
3347                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3348                  gnus-forwarded-mark)
3349                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3350                  gnus-saved-mark)
3351                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3352                  gnus-recent-mark)
3353                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3354                  gnus-unseen-mark)
3355                 (t gnus-no-mark)))
3356          (gnus-tmp-downloaded
3357           (cond (undownloaded
3358                  gnus-undownloaded-mark)
3359                 (gnus-newsgroup-agentized
3360                  gnus-downloaded-mark)
3361                 (t
3362                  gnus-no-mark)))
3363          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3364          (gnus-tmp-name
3365           (cond
3366            ((string-match "<[^>]+> *$" gnus-tmp-from)
3367             (let ((beg (match-beginning 0)))
3368               (or (and (string-match "^\".+\"" gnus-tmp-from)
3369                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3370                   (substring gnus-tmp-from 0 beg))))
3371            ((string-match "(.+)" gnus-tmp-from)
3372             (substring gnus-tmp-from
3373                        (1+ (match-beginning 0)) (1- (match-end 0))))
3374            (t gnus-tmp-from)))
3375          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3376          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3377          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3378          (buffer-read-only nil))
3379     (when (string= gnus-tmp-name "")
3380       (setq gnus-tmp-name gnus-tmp-from))
3381     (unless (numberp gnus-tmp-lines)
3382       (setq gnus-tmp-lines -1))
3383     (if (= gnus-tmp-lines -1)
3384         (setq gnus-tmp-lines "?")
3385       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3386       (gnus-put-text-property
3387      (point)
3388      (progn (eval gnus-summary-line-format-spec) (point))
3389        'gnus-number gnus-tmp-number)
3390     (when (gnus-visual-p 'summary-highlight 'highlight)
3391       (forward-line -1)
3392       (gnus-run-hooks 'gnus-summary-update-hook)
3393       (forward-line 1))))
3394
3395 (defun gnus-summary-update-line (&optional dont-update)
3396   "Update summary line after change."
3397   (when (and gnus-summary-default-score
3398              (not gnus-summary-inhibit-highlight))
3399     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3400            (article (gnus-summary-article-number))
3401            (score (gnus-summary-article-score article)))
3402       (unless dont-update
3403         (if (and gnus-summary-mark-below
3404                  (< (gnus-summary-article-score)
3405                     gnus-summary-mark-below))
3406             ;; This article has a low score, so we mark it as read.
3407             (when (memq article gnus-newsgroup-unreads)
3408               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3409           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3410             ;; This article was previously marked as read on account
3411             ;; of a low score, but now it has risen, so we mark it as
3412             ;; unread.
3413             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3414         (gnus-summary-update-mark
3415          (if (or (null gnus-summary-default-score)
3416                  (<= (abs (- score gnus-summary-default-score))
3417                      gnus-summary-zcore-fuzz))
3418              ?                          ;Whitespace
3419            (if (< score gnus-summary-default-score)
3420                gnus-score-below-mark gnus-score-over-mark))
3421          'score))
3422       ;; Do visual highlighting.
3423       (when (gnus-visual-p 'summary-highlight 'highlight)
3424         (gnus-run-hooks 'gnus-summary-update-hook)))))
3425
3426 (defvar gnus-tmp-new-adopts nil)
3427
3428 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3429   "Return the number of articles in THREAD.
3430 This may be 0 in some cases -- if none of the articles in
3431 the thread are to be displayed."
3432   (let* ((number
3433          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3434           (cond
3435            ((not (listp thread))
3436             1)
3437            ((and (consp thread) (cdr thread))
3438             (apply
3439              '+ 1 (mapcar
3440                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3441            ((null thread)
3442             1)
3443            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3444             1)
3445            (t 0))))
3446     (when (and level (zerop level) gnus-tmp-new-adopts)
3447       (incf number
3448             (apply '+ (mapcar
3449                        'gnus-summary-number-of-articles-in-thread
3450                        gnus-tmp-new-adopts))))
3451     (if char
3452         (if (> number 1) gnus-not-empty-thread-mark
3453           gnus-empty-thread-mark)
3454       number)))
3455
3456 (defsubst gnus-summary-line-message-size (head)
3457   "Return pretty-printed version of message size.
3458 This function is intended to be used in
3459 `gnus-summary-line-format-alist'."
3460   (let ((c (or (mail-header-chars head) -1)))
3461     (cond ((< c 0) "n/a")               ; chars not available
3462           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3463           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3464           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3465           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3466
3467
3468 (defun gnus-summary-set-local-parameters (group)
3469   "Go through the local params of GROUP and set all variable specs in that list."
3470   (let ((params (gnus-group-find-parameter group))
3471         (vars '(quit-config))           ; Ignore quit-config.
3472         elem)
3473     (while params
3474       (setq elem (car params)
3475             params (cdr params))
3476       (and (consp elem)                 ; Has to be a cons.
3477            (consp (cdr elem))           ; The cdr has to be a list.
3478            (symbolp (car elem))         ; Has to be a symbol in there.
3479            (not (memq (car elem) vars))
3480            (ignore-errors               ; So we set it.
3481              (push (car elem) vars)
3482              (make-local-variable (car elem))
3483              (set (car elem) (eval (nth 1 elem))))))))
3484
3485 (defun gnus-summary-read-group (group &optional show-all no-article
3486                                       kill-buffer no-display backward
3487                                       select-articles)
3488   "Start reading news in newsgroup GROUP.
3489 If SHOW-ALL is non-nil, already read articles are also listed.
3490 If NO-ARTICLE is non-nil, no article is selected initially.
3491 If NO-DISPLAY, don't generate a summary buffer."
3492   (let (result)
3493     (while (and group
3494                 (null (setq result
3495                             (let ((gnus-auto-select-next nil))
3496                               (or (gnus-summary-read-group-1
3497                                    group show-all no-article
3498                                    kill-buffer no-display
3499                                    select-articles)
3500                                   (setq show-all nil
3501                                         select-articles nil)))))
3502                 (eq gnus-auto-select-next 'quietly))
3503       (set-buffer gnus-group-buffer)
3504       ;; The entry function called above goes to the next
3505       ;; group automatically, so we go two groups back
3506       ;; if we are searching for the previous group.
3507       (when backward
3508         (gnus-group-prev-unread-group 2))
3509       (if (not (equal group (gnus-group-group-name)))
3510           (setq group (gnus-group-group-name))
3511         (setq group nil)))
3512     result))
3513
3514 (defun gnus-summary-read-group-1 (group show-all no-article
3515                                         kill-buffer no-display
3516                                         &optional select-articles)
3517   ;; Killed foreign groups can't be entered.
3518   ;;  (when (and (not (gnus-group-native-p group))
3519   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3520   ;;    (error "Dead non-native groups can't be entered"))
3521   (gnus-message 5 "Retrieving newsgroup: %s..."
3522                 (gnus-group-decoded-name group))
3523   (let* ((new-group (gnus-summary-setup-buffer group))
3524          (quit-config (gnus-group-quit-config group))
3525          (did-select (and new-group (gnus-select-newsgroup
3526                                      group show-all select-articles))))
3527     (cond
3528      ;; This summary buffer exists already, so we just select it.
3529      ((not new-group)
3530       (gnus-set-global-variables)
3531       (when kill-buffer
3532         (gnus-kill-or-deaden-summary kill-buffer))
3533       (gnus-configure-windows 'summary 'force)
3534       (gnus-set-mode-line 'summary)
3535       (gnus-summary-position-point)
3536       (message "")
3537       t)
3538      ;; We couldn't select this group.
3539      ((null did-select)
3540       (when (and (eq major-mode 'gnus-summary-mode)
3541                  (not (equal (current-buffer) kill-buffer)))
3542         (kill-buffer (current-buffer))
3543         (if (not quit-config)
3544             (progn
3545               ;; Update the info -- marks might need to be removed,
3546               ;; for instance.
3547               (gnus-summary-update-info)
3548               (set-buffer gnus-group-buffer)
3549               (gnus-group-jump-to-group group)
3550               (gnus-group-next-unread-group 1))
3551           (gnus-handle-ephemeral-exit quit-config)))
3552       (let ((grpinfo (gnus-get-info group)))
3553         (if (null (gnus-info-read grpinfo))
3554             (gnus-message 3 "Group %s contains no messages"
3555                           (gnus-group-decoded-name group))
3556           (gnus-message 3 "Can't select group")))
3557       nil)
3558      ;; The user did a `C-g' while prompting for number of articles,
3559      ;; so we exit this group.
3560      ((eq did-select 'quit)
3561       (and (eq major-mode 'gnus-summary-mode)
3562            (not (equal (current-buffer) kill-buffer))
3563            (kill-buffer (current-buffer)))
3564       (when kill-buffer
3565         (gnus-kill-or-deaden-summary kill-buffer))
3566       (if (not quit-config)
3567           (progn
3568             (set-buffer gnus-group-buffer)
3569             (gnus-group-jump-to-group group)
3570             (gnus-group-next-unread-group 1)
3571             (gnus-configure-windows 'group 'force))
3572         (gnus-handle-ephemeral-exit quit-config))
3573       ;; Finally signal the quit.
3574       (signal 'quit nil))
3575      ;; The group was successfully selected.
3576      (t
3577       (gnus-set-global-variables)
3578       ;; Save the active value in effect when the group was entered.
3579       (setq gnus-newsgroup-active
3580             (gnus-copy-sequence
3581              (gnus-active gnus-newsgroup-name)))
3582       ;; You can change the summary buffer in some way with this hook.
3583       (gnus-run-hooks 'gnus-select-group-hook)
3584       (gnus-update-format-specifications
3585        nil 'summary 'summary-mode 'summary-dummy)
3586       (gnus-update-summary-mark-positions)
3587       ;; Do score processing.
3588       (when gnus-use-scoring
3589         (gnus-possibly-score-headers))
3590       ;; Check whether to fill in the gaps in the threads.
3591       (when gnus-build-sparse-threads
3592         (gnus-build-sparse-threads))
3593       ;; Find the initial limit.
3594       (if gnus-show-threads
3595           (if show-all
3596               (let ((gnus-newsgroup-dormant nil))
3597                 (gnus-summary-initial-limit show-all))
3598             (gnus-summary-initial-limit show-all))
3599         ;; When unthreaded, all articles are always shown.
3600         (setq gnus-newsgroup-limit
3601               (mapcar
3602                (lambda (header) (mail-header-number header))
3603                gnus-newsgroup-headers)))
3604       ;; Generate the summary buffer.
3605       (unless no-display
3606         (gnus-summary-prepare))
3607       (when gnus-use-trees
3608         (gnus-tree-open group)
3609         (setq gnus-summary-highlight-line-function
3610               'gnus-tree-highlight-article))
3611       ;; If the summary buffer is empty, but there are some low-scored
3612       ;; articles or some excluded dormants, we include these in the
3613       ;; buffer.
3614       (when (and (zerop (buffer-size))
3615                  (not no-display))
3616         (cond (gnus-newsgroup-dormant
3617                (gnus-summary-limit-include-dormant))
3618               ((and gnus-newsgroup-scored show-all)
3619                (gnus-summary-limit-include-expunged t))))
3620       ;; Function `gnus-apply-kill-file' must be called in this hook.
3621       (gnus-run-hooks 'gnus-apply-kill-hook)
3622       (if (and (zerop (buffer-size))
3623                (not no-display))
3624           (progn
3625             ;; This newsgroup is empty.
3626             (gnus-summary-catchup-and-exit nil t)
3627             (gnus-message 6 "No unread news")
3628             (when kill-buffer
3629               (gnus-kill-or-deaden-summary kill-buffer))
3630             ;; Return nil from this function.
3631             nil)
3632         ;; Hide conversation thread subtrees.  We cannot do this in
3633         ;; gnus-summary-prepare-hook since kill processing may not
3634         ;; work with hidden articles.
3635         (gnus-summary-maybe-hide-threads)
3636         (when kill-buffer
3637           (gnus-kill-or-deaden-summary kill-buffer))
3638         (gnus-summary-auto-select-subject)
3639         ;; Show first unread article if requested.
3640         (if (and (not no-article)
3641                  (not no-display)
3642                  gnus-newsgroup-unreads
3643                  gnus-auto-select-first)
3644             (progn
3645               (gnus-configure-windows 'summary)
3646               (let ((art (gnus-summary-article-number)))
3647                 (unless (and (not gnus-plugged)
3648                              (or (memq art gnus-newsgroup-undownloaded)
3649                                  (memq art gnus-newsgroup-downloadable)))
3650                   (gnus-summary-goto-article art))))
3651           ;; Don't select any articles.
3652           (gnus-summary-position-point)
3653           (gnus-configure-windows 'summary 'force)
3654           (gnus-set-mode-line 'summary))
3655         (when (and gnus-auto-center-group
3656                    (get-buffer-window gnus-group-buffer t))
3657           ;; Gotta use windows, because recenter does weird stuff if
3658           ;; the current buffer ain't the displayed window.
3659           (let ((owin (selected-window)))
3660             (select-window (get-buffer-window gnus-group-buffer t))
3661             (when (gnus-group-goto-group group)
3662               (recenter))
3663             (select-window owin)))
3664         ;; Mark this buffer as "prepared".
3665         (setq gnus-newsgroup-prepared t)
3666         (gnus-run-hooks 'gnus-summary-prepared-hook)
3667         (unless (gnus-ephemeral-group-p group)
3668           (gnus-group-update-group group))
3669         t)))))
3670
3671 (defun gnus-summary-auto-select-subject ()
3672   "Select the subject line on initial group entry."
3673   (goto-char (point-min))
3674   (cond
3675    ((eq gnus-auto-select-subject 'best)
3676     (gnus-summary-best-unread-subject))
3677    ((eq gnus-auto-select-subject 'unread)
3678     (gnus-summary-first-unread-subject))
3679    ((eq gnus-auto-select-subject 'unseen)
3680     (gnus-summary-first-unseen-subject))
3681    ((eq gnus-auto-select-subject 'unseen-or-unread)
3682     (gnus-summary-first-unseen-or-unread-subject))
3683    ((eq gnus-auto-select-subject 'first)
3684     ;; Do nothing.
3685     )
3686    ((functionp gnus-auto-select-subject)
3687     (funcall gnus-auto-select-subject))))
3688
3689 (defun gnus-summary-prepare ()
3690   "Generate the summary buffer."
3691   (interactive)
3692   (let ((buffer-read-only nil))
3693     (erase-buffer)
3694     (setq gnus-newsgroup-data nil
3695           gnus-newsgroup-data-reverse nil)
3696     (gnus-run-hooks 'gnus-summary-generate-hook)
3697     ;; Generate the buffer, either with threads or without.
3698     (when gnus-newsgroup-headers
3699       (gnus-summary-prepare-threads
3700        (if gnus-show-threads
3701            (gnus-sort-gathered-threads
3702             (funcall gnus-summary-thread-gathering-function
3703                      (gnus-sort-threads
3704                       (gnus-cut-threads (gnus-make-threads)))))
3705          ;; Unthreaded display.
3706          (gnus-sort-articles gnus-newsgroup-headers))))
3707     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3708     ;; Call hooks for modifying summary buffer.
3709     (goto-char (point-min))
3710     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3711
3712 (defsubst gnus-general-simplify-subject (subject)
3713   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3714   (setq subject
3715         (cond
3716          ;; Truncate the subject.
3717          (gnus-simplify-subject-functions
3718           (gnus-map-function gnus-simplify-subject-functions subject))
3719          ((numberp gnus-summary-gather-subject-limit)
3720           (setq subject (gnus-simplify-subject-re subject))
3721           (if (> (length subject) gnus-summary-gather-subject-limit)
3722               (substring subject 0 gnus-summary-gather-subject-limit)
3723             subject))
3724          ;; Fuzzily simplify it.
3725          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3726           (gnus-simplify-subject-fuzzy subject))
3727          ;; Just remove the leading "Re:".
3728          (t
3729           (gnus-simplify-subject-re subject))))
3730
3731   (if (and gnus-summary-gather-exclude-subject
3732            (string-match gnus-summary-gather-exclude-subject subject))
3733       nil                         ; This article shouldn't be gathered
3734     subject))
3735
3736 (defun gnus-summary-simplify-subject-query ()
3737   "Query where the respool algorithm would put this article."
3738   (interactive)
3739   (gnus-summary-select-article)
3740   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3741
3742 (defun gnus-gather-threads-by-subject (threads)
3743   "Gather threads by looking at Subject headers."
3744   (if (not gnus-summary-make-false-root)
3745       threads
3746     (let ((hashtb (gnus-make-hashtable 1024))
3747           (prev threads)
3748           (result threads)
3749           subject hthread whole-subject)
3750       (while threads
3751         (setq subject (gnus-general-simplify-subject
3752                        (setq whole-subject (mail-header-subject
3753                                             (caar threads)))))
3754         (when subject
3755           (if (setq hthread (gnus-gethash subject hashtb))
3756               (progn
3757                 ;; We enter a dummy root into the thread, if we
3758                 ;; haven't done that already.
3759                 (unless (stringp (caar hthread))
3760                   (setcar hthread (list whole-subject (car hthread))))
3761                 ;; We add this new gathered thread to this gathered
3762                 ;; thread.
3763                 (setcdr (car hthread)
3764                         (nconc (cdar hthread) (list (car threads))))
3765                 ;; Remove it from the list of threads.
3766                 (setcdr prev (cdr threads))
3767                 (setq threads prev))
3768             ;; Enter this thread into the hash table.
3769             (gnus-sethash subject
3770                           (if gnus-summary-make-false-root-always
3771                               (progn
3772                                 ;; If you want a dummy root above all
3773                                 ;; threads...
3774                                 (setcar threads (list whole-subject
3775                                                       (car threads)))
3776                                 threads)
3777                             threads)
3778                           hashtb)))
3779         (setq prev threads)
3780         (setq threads (cdr threads)))
3781       result)))
3782
3783 (defun gnus-gather-threads-by-references (threads)
3784   "Gather threads by looking at References headers."
3785   (let ((idhashtb (gnus-make-hashtable 1024))
3786         (thhashtb (gnus-make-hashtable 1024))
3787         (prev threads)
3788         (result threads)
3789         ids references id gthread gid entered ref)
3790     (while threads
3791       (when (setq references (mail-header-references (caar threads)))
3792         (setq id (mail-header-id (caar threads))
3793               ids (inline (gnus-split-references references))
3794               entered nil)
3795         (while (setq ref (pop ids))
3796           (setq ids (delete ref ids))
3797           (if (not (setq gid (gnus-gethash ref idhashtb)))
3798               (progn
3799                 (gnus-sethash ref id idhashtb)
3800                 (gnus-sethash id threads thhashtb))
3801             (setq gthread (gnus-gethash gid thhashtb))
3802             (unless entered
3803               ;; We enter a dummy root into the thread, if we
3804               ;; haven't done that already.
3805               (unless (stringp (caar gthread))
3806                 (setcar gthread (list (mail-header-subject (caar gthread))
3807                                       (car gthread))))
3808               ;; We add this new gathered thread to this gathered
3809               ;; thread.
3810               (setcdr (car gthread)
3811                       (nconc (cdar gthread) (list (car threads)))))
3812             ;; Add it into the thread hash table.
3813             (gnus-sethash id gthread thhashtb)
3814             (setq entered t)
3815             ;; Remove it from the list of threads.
3816             (setcdr prev (cdr threads))
3817             (setq threads prev))))
3818       (setq prev threads)
3819       (setq threads (cdr threads)))
3820     result))
3821
3822 (defun gnus-sort-gathered-threads (threads)
3823   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3824   (let ((result threads))
3825     (while threads
3826       (when (stringp (caar threads))
3827         (setcdr (car threads)
3828                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3829       (setq threads (cdr threads)))
3830     result))
3831
3832 (defun gnus-thread-loop-p (root thread)
3833   "Say whether ROOT is in THREAD."
3834   (let ((stack (list thread))
3835         (infloop 0)
3836         th)
3837     (while (setq thread (pop stack))
3838       (setq th (cdr thread))
3839       (while (and th
3840                   (not (eq (caar th) root)))
3841         (pop th))
3842       (if th
3843           ;; We have found a loop.
3844           (let (ref-dep)
3845             (setcdr thread (delq (car th) (cdr thread)))
3846             (if (boundp (setq ref-dep (intern "none"
3847                                               gnus-newsgroup-dependencies)))
3848                 (setcdr (symbol-value ref-dep)
3849                         (nconc (cdr (symbol-value ref-dep))
3850                                (list (car th))))
3851               (set ref-dep (list nil (car th))))
3852             (setq infloop 1
3853                   stack nil))
3854         ;; Push all the subthreads onto the stack.
3855         (push (cdr thread) stack)))
3856     infloop))
3857
3858 (defun gnus-make-threads ()
3859   "Go through the dependency hashtb and find the roots.  Return all threads."
3860   (let (threads)
3861     (while (catch 'infloop
3862              (mapatoms
3863               (lambda (refs)
3864                 ;; Deal with self-referencing References loops.
3865                 (when (and (car (symbol-value refs))
3866                            (not (zerop
3867                                  (apply
3868                                   '+
3869                                   (mapcar
3870                                    (lambda (thread)
3871                                      (gnus-thread-loop-p
3872                                       (car (symbol-value refs)) thread))
3873                                    (cdr (symbol-value refs)))))))
3874                   (setq threads nil)
3875                   (throw 'infloop t))
3876                 (unless (car (symbol-value refs))
3877                   ;; These threads do not refer back to any other
3878                   ;; articles, so they're roots.
3879                   (setq threads (append (cdr (symbol-value refs)) threads))))
3880               gnus-newsgroup-dependencies)))
3881     threads))
3882
3883 ;; Build the thread tree.
3884 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3885   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3886
3887 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3888 if it was already present.
3889
3890 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3891 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3892 Message-IDs will be renamed to a unique Message-ID before being
3893 entered.
3894
3895 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3896   (let* ((id (mail-header-id header))
3897          (id-dep (and id (intern id dependencies)))
3898          parent-id ref ref-dep ref-header replaced)
3899     ;; Enter this `header' in the `dependencies' table.
3900     (cond
3901      ((not id-dep)
3902       (setq header nil))
3903      ;; The first two cases do the normal part: enter a new `header'
3904      ;; in the `dependencies' table.
3905      ((not (boundp id-dep))
3906       (set id-dep (list header)))
3907      ((null (car (symbol-value id-dep)))
3908       (setcar (symbol-value id-dep) header))
3909
3910      ;; From here the `header' was already present in the
3911      ;; `dependencies' table.
3912      (force-new
3913       ;; Overrides an existing entry;
3914       ;; just set the header part of the entry.
3915       (setcar (symbol-value id-dep) header)
3916       (setq replaced t))
3917
3918      ;; Renames the existing `header' to a unique Message-ID.
3919      ((not gnus-summary-ignore-duplicates)
3920       ;; An article with this Message-ID has already been seen.
3921       ;; We rename the Message-ID.
3922       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3923            (list header))
3924       (mail-header-set-id header id))
3925
3926      ;; The last case ignores an existing entry, except it adds any
3927      ;; additional Xrefs (in case the two articles came from different
3928      ;; servers.
3929      ;; Also sets `header' to `nil' meaning that the `dependencies'
3930      ;; table was *not* modified.
3931      (t
3932       (mail-header-set-xref
3933        (car (symbol-value id-dep))
3934        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3935                    "")
3936                (or (mail-header-xref header) "")))
3937       (setq header nil)))
3938
3939     (when (and header (not replaced))
3940       ;; First check that we are not creating a References loop.
3941       (setq parent-id (gnus-parent-id (mail-header-references header)))
3942       (setq ref parent-id)
3943       (while (and ref
3944                   (setq ref-dep (intern-soft ref dependencies))
3945                   (boundp ref-dep)
3946                   (setq ref-header (car (symbol-value ref-dep))))
3947         (if (string= id ref)
3948             ;; Yuk!  This is a reference loop.  Make the article be a
3949             ;; root article.
3950             (progn
3951               (mail-header-set-references (car (symbol-value id-dep)) "none")
3952               (setq ref nil)
3953               (setq parent-id nil))
3954           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3955       (setq ref-dep (intern (or parent-id "none") dependencies))
3956       (if (boundp ref-dep)
3957           (setcdr (symbol-value ref-dep)
3958                   (nconc (cdr (symbol-value ref-dep))
3959                          (list (symbol-value id-dep))))
3960         (set ref-dep (list nil (symbol-value id-dep)))))
3961     header))
3962
3963 (defun gnus-extract-message-id-from-in-reply-to (string)
3964   (if (string-match "<[^>]+>" string)
3965       (substring string (match-beginning 0) (match-end 0))
3966     nil))
3967
3968 (defun gnus-build-sparse-threads ()
3969   (let ((headers gnus-newsgroup-headers)
3970         (mail-parse-charset gnus-newsgroup-charset)
3971         (gnus-summary-ignore-duplicates t)
3972         header references generation relations
3973         subject child end new-child date)
3974     ;; First we create an alist of generations/relations, where
3975     ;; generations is how much we trust the relation, and the relation
3976     ;; is parent/child.
3977     (gnus-message 7 "Making sparse threads...")
3978     (save-excursion
3979       (nnheader-set-temp-buffer " *gnus sparse threads*")
3980       (while (setq header (pop headers))
3981         (when (and (setq references (mail-header-references header))
3982                    (not (string= references "")))
3983           (insert references)
3984           (setq child (mail-header-id header)
3985                 subject (mail-header-subject header)
3986                 date (mail-header-date header)
3987                 generation 0)
3988           (while (search-backward ">" nil t)
3989             (setq end (1+ (point)))
3990             (when (search-backward "<" nil t)
3991               (setq new-child (buffer-substring (point) end))
3992               (push (list (incf generation)
3993                           child (setq child new-child)
3994                           subject date)
3995                     relations)))
3996           (when child
3997             (push (list (1+ generation) child nil subject) relations))
3998           (erase-buffer)))
3999       (kill-buffer (current-buffer)))
4000     ;; Sort over trustworthiness.
4001     (mapcar
4002      (lambda (relation)
4003        (when (gnus-dependencies-add-header
4004               (make-full-mail-header
4005                gnus-reffed-article-number
4006                (nth 3 relation) "" (or (nth 4 relation) "")
4007                (nth 1 relation)
4008                (or (nth 2 relation) "") 0 0 "")
4009               gnus-newsgroup-dependencies nil)
4010          (push gnus-reffed-article-number gnus-newsgroup-limit)
4011          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4012          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4013                gnus-newsgroup-reads)
4014          (decf gnus-reffed-article-number)))
4015      (sort relations 'car-less-than-car))
4016     (gnus-message 7 "Making sparse threads...done")))
4017
4018 (defun gnus-build-old-threads ()
4019   ;; Look at all the articles that refer back to old articles, and
4020   ;; fetch the headers for the articles that aren't there.  This will
4021   ;; build complete threads - if the roots haven't been expired by the
4022   ;; server, that is.
4023   (let ((mail-parse-charset gnus-newsgroup-charset)
4024         id heads)
4025     (mapatoms
4026      (lambda (refs)
4027        (when (not (car (symbol-value refs)))
4028          (setq heads (cdr (symbol-value refs)))
4029          (while heads
4030            (if (memq (mail-header-number (caar heads))
4031                      gnus-newsgroup-dormant)
4032                (setq heads (cdr heads))
4033              (setq id (symbol-name refs))
4034              (while (and (setq id (gnus-build-get-header id))
4035                          (not (car (gnus-id-to-thread id)))))
4036              (setq heads nil)))))
4037      gnus-newsgroup-dependencies)))
4038
4039 (defsubst gnus-remove-odd-characters (string)
4040   "Translate STRING into something that doesn't contain weird characters."
4041   (mm-subst-char-in-string
4042    ?\r ?\-
4043    (mm-subst-char-in-string ?\n ?\- string t) t))
4044
4045 ;; This function has to be called with point after the article number
4046 ;; on the beginning of the line.
4047 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4048   (let ((eol (point-at-eol))
4049         (buffer (current-buffer))
4050         header references in-reply-to)
4051
4052     ;; overview: [num subject from date id refs chars lines misc]
4053     (unwind-protect
4054         (let (x)
4055           (narrow-to-region (point) eol)
4056           (unless (eobp)
4057             (forward-char))
4058
4059           (setq header
4060                 (make-full-mail-header
4061                  number                 ; number
4062                  (condition-case ()     ; subject
4063                      (gnus-remove-odd-characters
4064                       (funcall gnus-decode-encoded-word-function
4065                                (setq x (nnheader-nov-field))))
4066                    (error x))
4067                  (condition-case ()     ; from
4068                      (gnus-remove-odd-characters
4069                       (funcall gnus-decode-encoded-word-function
4070                                (setq x (nnheader-nov-field))))
4071                    (error x))
4072                  (nnheader-nov-field)   ; date
4073                  (nnheader-nov-read-message-id) ; id
4074                  (setq references (nnheader-nov-field)) ; refs
4075                  (nnheader-nov-read-integer) ; chars
4076                  (nnheader-nov-read-integer) ; lines
4077                  (unless (eobp)
4078                    (if (looking-at "Xref: ")
4079                        (goto-char (match-end 0)))
4080                    (nnheader-nov-field)) ; Xref
4081                  (nnheader-nov-parse-extra)))) ; extra
4082
4083       (widen))
4084
4085     (when (and (string= references "")
4086                (setq in-reply-to (mail-header-extra header))
4087                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4088       (mail-header-set-references
4089        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4090
4091     (when gnus-alter-header-function
4092       (funcall gnus-alter-header-function header))
4093     (gnus-dependencies-add-header header dependencies force-new)))
4094
4095 (defun gnus-build-get-header (id)
4096   "Look through the buffer of NOV lines and find the header to ID.
4097 Enter this line into the dependencies hash table, and return
4098 the id of the parent article (if any)."
4099   (let ((deps gnus-newsgroup-dependencies)
4100         found header)
4101     (prog1
4102         (save-excursion
4103           (set-buffer nntp-server-buffer)
4104           (let ((case-fold-search nil))
4105             (goto-char (point-min))
4106             (while (and (not found)
4107                         (search-forward id nil t))
4108               (beginning-of-line)
4109               (setq found (looking-at
4110                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4111                                    (regexp-quote id))))
4112               (or found (beginning-of-line 2)))
4113             (when found
4114               (beginning-of-line)
4115               (and
4116                (setq header (gnus-nov-parse-line
4117                              (read (current-buffer)) deps))
4118                (gnus-parent-id (mail-header-references header))))))
4119       (when header
4120         (let ((number (mail-header-number header)))
4121           (push number gnus-newsgroup-limit)
4122           (push header gnus-newsgroup-headers)
4123           (if (memq number gnus-newsgroup-unselected)
4124               (progn
4125                 (setq gnus-newsgroup-unreads
4126                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4127                                                number))
4128                 (setq gnus-newsgroup-unselected
4129                       (delq number gnus-newsgroup-unselected)))
4130             (push number gnus-newsgroup-ancient)))))))
4131
4132 (defun gnus-build-all-threads ()
4133   "Read all the headers."
4134   (let ((gnus-summary-ignore-duplicates t)
4135         (mail-parse-charset gnus-newsgroup-charset)
4136         (dependencies gnus-newsgroup-dependencies)
4137         header article)
4138     (save-excursion
4139       (set-buffer nntp-server-buffer)
4140       (let ((case-fold-search nil))
4141         (goto-char (point-min))
4142         (while (not (eobp))
4143           (ignore-errors
4144             (setq article (read (current-buffer))
4145                   header (gnus-nov-parse-line article dependencies)))
4146           (when header
4147             (save-excursion
4148               (set-buffer gnus-summary-buffer)
4149               (push header gnus-newsgroup-headers)
4150               (if (memq (setq article (mail-header-number header))
4151                         gnus-newsgroup-unselected)
4152                   (progn
4153                     (setq gnus-newsgroup-unreads
4154                           (gnus-add-to-sorted-list
4155                            gnus-newsgroup-unreads article))
4156                     (setq gnus-newsgroup-unselected
4157                           (delq article gnus-newsgroup-unselected)))
4158                 (push article gnus-newsgroup-ancient)))
4159             (forward-line 1)))))))
4160
4161 (defun gnus-summary-update-article-line (article header)
4162   "Update the line for ARTICLE using HEADER."
4163   (let* ((id (mail-header-id header))
4164          (thread (gnus-id-to-thread id)))
4165     (unless thread
4166       (error "Article in no thread"))
4167     ;; Update the thread.
4168     (setcar thread header)
4169     (gnus-summary-goto-subject article)
4170     (let* ((datal (gnus-data-find-list article))
4171            (data (car datal))
4172            (buffer-read-only nil)
4173            (level (gnus-summary-thread-level)))
4174       (gnus-delete-line)
4175       (let ((inserted (- (point)
4176                          (progn
4177                            (gnus-summary-insert-line
4178                             header level nil
4179                             (memq article gnus-newsgroup-undownloaded)
4180                             (gnus-article-mark article)
4181                             (memq article gnus-newsgroup-replied)
4182                             (memq article gnus-newsgroup-expirable)
4183                             ;; Only insert the Subject string when it's different
4184                             ;; from the previous Subject string.
4185                             (if (and
4186                                  gnus-show-threads
4187                                  (gnus-subject-equal
4188                                   (condition-case ()
4189                                       (mail-header-subject
4190                                        (gnus-data-header
4191                                         (cadr
4192                                          (gnus-data-find-list
4193                                           article
4194                                           (gnus-data-list t)))))
4195                                     ;; Error on the side of excessive subjects.
4196                                     (error ""))
4197                                   (mail-header-subject header)))
4198                                 ""
4199                               (mail-header-subject header))
4200                             nil (cdr (assq article gnus-newsgroup-scored))
4201                             (memq article gnus-newsgroup-processable))
4202                            (point)))))
4203         (when (cdr datal)
4204           (gnus-data-update-list
4205            (cdr datal)
4206            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4207
4208 (defun gnus-summary-update-article (article &optional iheader)
4209   "Update ARTICLE in the summary buffer."
4210   (set-buffer gnus-summary-buffer)
4211   (let* ((header (gnus-summary-article-header article))
4212          (id (mail-header-id header))
4213          (data (gnus-data-find article))
4214          (thread (gnus-id-to-thread id))
4215          (references (mail-header-references header))
4216          (parent
4217           (gnus-id-to-thread
4218            (or (gnus-parent-id
4219                 (when (and references
4220                            (not (equal "" references)))
4221                   references))
4222                "none")))
4223          (buffer-read-only nil)
4224          (old (car thread)))
4225     (when thread
4226       (unless iheader
4227         (setcar thread nil)
4228         (when parent
4229           (delq thread parent)))
4230       (if (gnus-summary-insert-subject id header)
4231           ;; Set the (possibly) new article number in the data structure.
4232           (gnus-data-set-number data (gnus-id-to-article id))
4233         (setcar thread old)
4234         nil))))
4235
4236 (defun gnus-rebuild-thread (id &optional line)
4237   "Rebuild the thread containing ID.
4238 If LINE, insert the rebuilt thread starting on line LINE."
4239   (let ((buffer-read-only nil)
4240         old-pos current thread data)
4241     (if (not gnus-show-threads)
4242         (setq thread (list (car (gnus-id-to-thread id))))
4243       ;; Get the thread this article is part of.
4244       (setq thread (gnus-remove-thread id)))
4245     (setq old-pos (point-at-bol))
4246     (setq current (save-excursion
4247                     (and (re-search-backward "[\r\n]" nil t)
4248                          (gnus-summary-article-number))))
4249     ;; If this is a gathered thread, we have to go some re-gathering.
4250     (when (stringp (car thread))
4251       (let ((subject (car thread))
4252             roots thr)
4253         (setq thread (cdr thread))
4254         (while thread
4255           (unless (memq (setq thr (gnus-id-to-thread
4256                                    (gnus-root-id
4257                                     (mail-header-id (caar thread)))))
4258                         roots)
4259             (push thr roots))
4260           (setq thread (cdr thread)))
4261         ;; We now have all (unique) roots.
4262         (if (= (length roots) 1)
4263             ;; All the loose roots are now one solid root.
4264             (setq thread (car roots))
4265           (setq thread (cons subject (gnus-sort-threads roots))))))
4266     (let (threads)
4267       ;; We then insert this thread into the summary buffer.
4268       (when line
4269         (goto-char (point-min))
4270         (forward-line (1- line)))
4271       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4272         (if gnus-show-threads
4273             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4274           (gnus-summary-prepare-unthreaded thread))
4275         (setq data (nreverse gnus-newsgroup-data))
4276         (setq threads gnus-newsgroup-threads))
4277       ;; We splice the new data into the data structure.
4278       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4279       ;;!!! then we want to insert at the beginning of the buffer.
4280       ;;!!! That happens to be true with Gnus now, but that may
4281       ;;!!! change in the future.  Perhaps.
4282       (gnus-data-enter-list
4283        (if line nil current) data (- (point) old-pos))
4284       (setq gnus-newsgroup-threads
4285             (nconc threads gnus-newsgroup-threads))
4286       (gnus-data-compute-positions))))
4287
4288 (defun gnus-number-to-header (number)
4289   "Return the header for article NUMBER."
4290   (let ((headers gnus-newsgroup-headers))
4291     (while (and headers
4292                 (not (= number (mail-header-number (car headers)))))
4293       (pop headers))
4294     (when headers
4295       (car headers))))
4296
4297 (defun gnus-parent-headers (in-headers &optional generation)
4298   "Return the headers of the GENERATIONeth parent of HEADERS."
4299   (unless generation
4300     (setq generation 1))
4301   (let ((parent t)
4302         (headers in-headers)
4303         references)
4304     (while (and parent
4305                 (not (zerop generation))
4306                 (setq references (mail-header-references headers)))
4307       (setq headers (if (and references
4308                              (setq parent (gnus-parent-id references)))
4309                         (car (gnus-id-to-thread parent))
4310                       nil))
4311       (decf generation))
4312     (and (not (eq headers in-headers))
4313          headers)))
4314
4315 (defun gnus-id-to-thread (id)
4316   "Return the (sub-)thread where ID appears."
4317   (gnus-gethash id gnus-newsgroup-dependencies))
4318
4319 (defun gnus-id-to-article (id)
4320   "Return the article number of ID."
4321   (let ((thread (gnus-id-to-thread id)))
4322     (when (and thread
4323                (car thread))
4324       (mail-header-number (car thread)))))
4325
4326 (defun gnus-id-to-header (id)
4327   "Return the article headers of ID."
4328   (car (gnus-id-to-thread id)))
4329
4330 (defun gnus-article-displayed-root-p (article)
4331   "Say whether ARTICLE is a root(ish) article."
4332   (let ((level (gnus-summary-thread-level article))
4333         (refs (mail-header-references  (gnus-summary-article-header article)))
4334         particle)
4335     (cond
4336      ((null level) nil)
4337      ((zerop level) t)
4338      ((null refs) t)
4339      ((null (gnus-parent-id refs)) t)
4340      ((and (= 1 level)
4341            (null (setq particle (gnus-id-to-article
4342                                  (gnus-parent-id refs))))
4343            (null (gnus-summary-thread-level particle)))))))
4344
4345 (defun gnus-root-id (id)
4346   "Return the id of the root of the thread where ID appears."
4347   (let (last-id prev)
4348     (while (and id (setq prev (car (gnus-id-to-thread id))))
4349       (setq last-id id
4350             id (gnus-parent-id (mail-header-references prev))))
4351     last-id))
4352
4353 (defun gnus-articles-in-thread (thread)
4354   "Return the list of articles in THREAD."
4355   (cons (mail-header-number (car thread))
4356         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4357
4358 (defun gnus-remove-thread (id &optional dont-remove)
4359   "Remove the thread that has ID in it."
4360   (let (headers thread last-id)
4361     ;; First go up in this thread until we find the root.
4362     (setq last-id (gnus-root-id id)
4363           headers (message-flatten-list (gnus-id-to-thread last-id)))
4364     ;; We have now found the real root of this thread.  It might have
4365     ;; been gathered into some loose thread, so we have to search
4366     ;; through the threads to find the thread we wanted.
4367     (let ((threads gnus-newsgroup-threads)
4368           sub)
4369       (while threads
4370         (setq sub (car threads))
4371         (if (stringp (car sub))
4372             ;; This is a gathered thread, so we look at the roots
4373             ;; below it to find whether this article is in this
4374             ;; gathered root.
4375             (progn
4376               (setq sub (cdr sub))
4377               (while sub
4378                 (when (member (caar sub) headers)
4379                   (setq thread (car threads)
4380                         threads nil
4381                         sub nil))
4382                 (setq sub (cdr sub))))
4383           ;; It's an ordinary thread, so we check it.
4384           (when (eq (car sub) (car headers))
4385             (setq thread sub
4386                   threads nil)))
4387         (setq threads (cdr threads)))
4388       ;; If this article is in no thread, then it's a root.
4389       (if thread
4390           (unless dont-remove
4391             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4392         (setq thread (gnus-id-to-thread last-id)))
4393       (when thread
4394         (prog1
4395             thread                      ; We return this thread.
4396           (unless dont-remove
4397             (if (stringp (car thread))
4398                 (progn
4399                   ;; If we use dummy roots, then we have to remove the
4400                   ;; dummy root as well.
4401                   (when (eq gnus-summary-make-false-root 'dummy)
4402                     ;; We go to the dummy root by going to
4403                     ;; the first sub-"thread", and then one line up.
4404                     (gnus-summary-goto-article
4405                      (mail-header-number (caadr thread)))
4406                     (forward-line -1)
4407                     (gnus-delete-line)
4408                     (gnus-data-compute-positions))
4409                   (setq thread (cdr thread))
4410                   (while thread
4411                     (gnus-remove-thread-1 (car thread))
4412                     (setq thread (cdr thread))))
4413               (gnus-remove-thread-1 thread))))))))
4414
4415 (defun gnus-remove-thread-1 (thread)
4416   "Remove the thread THREAD recursively."
4417   (let ((number (mail-header-number (pop thread)))
4418         d)
4419     (setq thread (reverse thread))
4420     (while thread
4421       (gnus-remove-thread-1 (pop thread)))
4422     (when (setq d (gnus-data-find number))
4423       (goto-char (gnus-data-pos d))
4424       (gnus-summary-show-thread)
4425       (gnus-data-remove
4426        number
4427        (- (point-at-bol)
4428           (prog1
4429               (1+ (point-at-eol))
4430             (gnus-delete-line)))))))
4431
4432 (defun gnus-sort-threads-1 (threads func)
4433   (sort (mapcar (lambda (thread)
4434                   (cons (car thread)
4435                         (and (cdr thread)
4436                              (gnus-sort-threads-1 (cdr thread) func))))
4437                 threads) func))
4438
4439 (defun gnus-sort-threads (threads)
4440   "Sort THREADS."
4441   (if (not gnus-thread-sort-functions)
4442       threads
4443     (gnus-message 8 "Sorting threads...")
4444     (let ((max-lisp-eval-depth 5000))
4445       (prog1 (gnus-sort-threads-1
4446          threads
4447          (gnus-make-sort-function gnus-thread-sort-functions))
4448         (gnus-message 8 "Sorting threads...done")))))
4449
4450 (defun gnus-sort-articles (articles)
4451   "Sort ARTICLES."
4452   (when gnus-article-sort-functions
4453     (gnus-message 7 "Sorting articles...")
4454     (prog1
4455         (setq gnus-newsgroup-headers
4456               (sort articles (gnus-make-sort-function
4457                               gnus-article-sort-functions)))
4458       (gnus-message 7 "Sorting articles...done"))))
4459
4460 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4461 (defmacro gnus-thread-header (thread)
4462   "Return header of first article in THREAD.
4463 Note that THREAD must never, ever be anything else than a variable -
4464 using some other form will lead to serious barfage."
4465   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4466   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4467   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4468         (vector thread) 2))
4469
4470 (defsubst gnus-article-sort-by-number (h1 h2)
4471   "Sort articles by article number."
4472   (< (mail-header-number h1)
4473      (mail-header-number h2)))
4474
4475 (defun gnus-thread-sort-by-number (h1 h2)
4476   "Sort threads by root article number."
4477   (gnus-article-sort-by-number
4478    (gnus-thread-header h1) (gnus-thread-header h2)))
4479
4480 (defsubst gnus-article-sort-by-random (h1 h2)
4481   "Sort articles by article number."
4482   (zerop (random 2)))
4483
4484 (defun gnus-thread-sort-by-random (h1 h2)
4485   "Sort threads by root article number."
4486   (gnus-article-sort-by-random
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defsubst gnus-article-sort-by-lines (h1 h2)
4490   "Sort articles by article Lines header."
4491   (< (mail-header-lines h1)
4492      (mail-header-lines h2)))
4493
4494 (defun gnus-thread-sort-by-lines (h1 h2)
4495   "Sort threads by root article Lines header."
4496   (gnus-article-sort-by-lines
4497    (gnus-thread-header h1) (gnus-thread-header h2)))
4498
4499 (defsubst gnus-article-sort-by-chars (h1 h2)
4500   "Sort articles by octet length."
4501   (< (mail-header-chars h1)
4502      (mail-header-chars h2)))
4503
4504 (defun gnus-thread-sort-by-chars (h1 h2)
4505   "Sort threads by root article octet length."
4506   (gnus-article-sort-by-chars
4507    (gnus-thread-header h1) (gnus-thread-header h2)))
4508
4509 (defsubst gnus-article-sort-by-author (h1 h2)
4510   "Sort articles by root author."
4511   (string-lessp
4512    (let ((extract (funcall
4513                    gnus-extract-address-components
4514                    (mail-header-from h1))))
4515      (or (car extract) (cadr extract) ""))
4516    (let ((extract (funcall
4517                    gnus-extract-address-components
4518                    (mail-header-from h2))))
4519      (or (car extract) (cadr extract) ""))))
4520
4521 (defun gnus-thread-sort-by-author (h1 h2)
4522   "Sort threads by root author."
4523   (gnus-article-sort-by-author
4524    (gnus-thread-header h1)  (gnus-thread-header h2)))
4525
4526 (defsubst gnus-article-sort-by-subject (h1 h2)
4527   "Sort articles by root subject."
4528   (string-lessp
4529    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4530    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4531
4532 (defun gnus-thread-sort-by-subject (h1 h2)
4533   "Sort threads by root subject."
4534   (gnus-article-sort-by-subject
4535    (gnus-thread-header h1) (gnus-thread-header h2)))
4536
4537 (defsubst gnus-article-sort-by-date (h1 h2)
4538   "Sort articles by root article date."
4539   (time-less-p
4540    (gnus-date-get-time (mail-header-date h1))
4541    (gnus-date-get-time (mail-header-date h2))))
4542
4543 (defun gnus-thread-sort-by-date (h1 h2)
4544   "Sort threads by root article date."
4545   (gnus-article-sort-by-date
4546    (gnus-thread-header h1) (gnus-thread-header h2)))
4547
4548 (defsubst gnus-article-sort-by-score (h1 h2)
4549   "Sort articles by root article score.
4550 Unscored articles will be counted as having a score of zero."
4551   (> (or (cdr (assq (mail-header-number h1)
4552                     gnus-newsgroup-scored))
4553          gnus-summary-default-score 0)
4554      (or (cdr (assq (mail-header-number h2)
4555                     gnus-newsgroup-scored))
4556          gnus-summary-default-score 0)))
4557
4558 (defun gnus-thread-sort-by-score (h1 h2)
4559   "Sort threads by root article score."
4560   (gnus-article-sort-by-score
4561    (gnus-thread-header h1) (gnus-thread-header h2)))
4562
4563 (defun gnus-thread-sort-by-total-score (h1 h2)
4564   "Sort threads by the sum of all scores in the thread.
4565 Unscored articles will be counted as having a score of zero."
4566   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4567
4568 (defun gnus-thread-total-score (thread)
4569   ;; This function find the total score of THREAD.
4570   (cond
4571    ((null thread)
4572     0)
4573    ((consp thread)
4574     (if (stringp (car thread))
4575         (apply gnus-thread-score-function 0
4576                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4577       (gnus-thread-total-score-1 thread)))
4578    (t
4579     (gnus-thread-total-score-1 (list thread)))))
4580
4581 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4582   "Sort threads such that the thread with the most recently arrived article comes first."
4583   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4584
4585 (defun gnus-thread-highest-number (thread)
4586   "Return the highest article number in THREAD."
4587   (apply 'max (mapcar (lambda (header)
4588                         (mail-header-number header))
4589                       (message-flatten-list thread))))
4590
4591 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4592   "Sort threads such that the thread with the most recently dated article comes first."
4593   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4594
4595 (defun gnus-thread-latest-date (thread)
4596   "Return the highest article date in THREAD."
4597   (let ((previous-time 0))
4598     (apply 'max
4599            (mapcar
4600             (lambda (header)
4601               (setq previous-time
4602                     (condition-case ()
4603                         (time-to-seconds (mail-header-parse-date
4604                                           (mail-header-date header)))
4605                       (error previous-time))))
4606             (sort
4607              (message-flatten-list thread)
4608              (lambda (h1 h2)
4609                (< (mail-header-number h1)
4610                   (mail-header-number h2))))))))
4611
4612 (defun gnus-thread-total-score-1 (root)
4613   ;; This function find the total score of the thread below ROOT.
4614   (setq root (car root))
4615   (apply gnus-thread-score-function
4616          (or (append
4617               (mapcar 'gnus-thread-total-score
4618                       (cdr (gnus-id-to-thread (mail-header-id root))))
4619               (when (> (mail-header-number root) 0)
4620                 (list (or (cdr (assq (mail-header-number root)
4621                                      gnus-newsgroup-scored))
4622                           gnus-summary-default-score 0))))
4623              (list gnus-summary-default-score)
4624              '(0))))
4625
4626 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4627 (defvar gnus-tmp-prev-subject nil)
4628 (defvar gnus-tmp-false-parent nil)
4629 (defvar gnus-tmp-root-expunged nil)
4630 (defvar gnus-tmp-dummy-line nil)
4631
4632 (eval-when-compile (defvar gnus-tmp-header))
4633 (defun gnus-extra-header (type &optional header)
4634   "Return the extra header of TYPE."
4635   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4636       ""))
4637
4638 (defvar gnus-tmp-thread-tree-header-string "")
4639
4640 (defcustom gnus-sum-thread-tree-root "> "
4641   "With %B spec, used for the root of a thread.
4642 If nil, use subject instead."
4643   :type '(radio (const :format "%v  " nil) (string :size 0))
4644   :group 'gnus-thread)
4645 (defcustom gnus-sum-thread-tree-false-root "> "
4646   "With %B spec, used for a false root of a thread.
4647 If nil, use subject instead."
4648   :type '(radio (const :format "%v  " nil) (string :size 0))
4649   :group 'gnus-thread)
4650 (defcustom gnus-sum-thread-tree-single-indent ""
4651   "With %B spec, used for a thread with just one message.
4652 If nil, use subject instead."
4653   :type '(radio (const :format "%v  " nil) (string :size 0))
4654   :group 'gnus-thread)
4655 (defcustom gnus-sum-thread-tree-vertical "| "
4656   "With %B spec, used for drawing a vertical line."
4657   :type 'string
4658   :group 'gnus-thread)
4659 (defcustom gnus-sum-thread-tree-indent "  "
4660   "With %B spec, used for indenting."
4661   :type 'string
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4664   "With %B spec, used for a leaf with brothers."
4665   :type 'string
4666   :group 'gnus-thread)
4667 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4668   "With %B spec, used for a leaf without brothers."
4669   :type 'string
4670   :group 'gnus-thread)
4671
4672 (defun gnus-summary-prepare-threads (threads)
4673   "Prepare summary buffer from THREADS and indentation LEVEL.
4674 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4675 or a straight list of headers."
4676   (gnus-message 7 "Generating summary...")
4677
4678   (setq gnus-newsgroup-threads threads)
4679   (beginning-of-line)
4680
4681   (let ((gnus-tmp-level 0)
4682         (default-score (or gnus-summary-default-score 0))
4683         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4684         (building-line-count gnus-summary-display-while-building)
4685         (building-count (integerp gnus-summary-display-while-building))
4686         thread number subject stack state gnus-tmp-gathered beg-match
4687         new-roots gnus-tmp-new-adopts thread-end simp-subject
4688         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4689         gnus-tmp-replied gnus-tmp-subject-or-nil
4690         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4691         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4692         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4693         tree-stack)
4694
4695     (setq gnus-tmp-prev-subject nil
4696           gnus-tmp-thread-tree-header-string "")
4697
4698     (if (vectorp (car threads))
4699         ;; If this is a straight (sic) list of headers, then a
4700         ;; threaded summary display isn't required, so we just create
4701         ;; an unthreaded one.
4702         (gnus-summary-prepare-unthreaded threads)
4703
4704       ;; Do the threaded display.
4705
4706       (if gnus-summary-display-while-building
4707           (switch-to-buffer (buffer-name)))
4708       (while (or threads stack gnus-tmp-new-adopts new-roots)
4709
4710         (if (and (= gnus-tmp-level 0)
4711                  (or (not stack)
4712                      (= (caar stack) 0))
4713                  (not gnus-tmp-false-parent)
4714                  (or gnus-tmp-new-adopts new-roots))
4715             (if gnus-tmp-new-adopts
4716                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4717                       thread (list (car gnus-tmp-new-adopts))
4718                       gnus-tmp-header (caar thread)
4719                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4720               (when new-roots
4721                 (setq thread (list (car new-roots))
4722                       gnus-tmp-header (caar thread)
4723                       new-roots (cdr new-roots))))
4724
4725           (if threads
4726               ;; If there are some threads, we do them before the
4727               ;; threads on the stack.
4728               (setq thread threads
4729                     gnus-tmp-header (caar thread))
4730             ;; There were no current threads, so we pop something off
4731             ;; the stack.
4732             (setq state (car stack)
4733                   gnus-tmp-level (car state)
4734                   tree-stack (cadr state)
4735                   thread (caddr state)
4736                   stack (cdr stack)
4737                   gnus-tmp-header (caar thread))))
4738
4739         (setq gnus-tmp-false-parent nil)
4740         (setq gnus-tmp-root-expunged nil)
4741         (setq thread-end nil)
4742
4743         (if (stringp gnus-tmp-header)
4744             ;; The header is a dummy root.
4745             (cond
4746              ((eq gnus-summary-make-false-root 'adopt)
4747               ;; We let the first article adopt the rest.
4748               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4749                                                (cddar thread)))
4750               (setq gnus-tmp-gathered
4751                     (nconc (mapcar
4752                             (lambda (h) (mail-header-number (car h)))
4753                             (cddar thread))
4754                            gnus-tmp-gathered))
4755               (setq thread (cons (list (caar thread)
4756                                        (cadar thread))
4757                                  (cdr thread)))
4758               (setq gnus-tmp-level -1
4759                     gnus-tmp-false-parent t))
4760              ((eq gnus-summary-make-false-root 'empty)
4761               ;; We print adopted articles with empty subject fields.
4762               (setq gnus-tmp-gathered
4763                     (nconc (mapcar
4764                             (lambda (h) (mail-header-number (car h)))
4765                             (cddar thread))
4766                            gnus-tmp-gathered))
4767               (setq gnus-tmp-level -1))
4768              ((eq gnus-summary-make-false-root 'dummy)
4769               ;; We remember that we probably want to output a dummy
4770               ;; root.
4771               (setq gnus-tmp-dummy-line gnus-tmp-header)
4772               (setq gnus-tmp-prev-subject gnus-tmp-header))
4773              (t
4774               ;; We do not make a root for the gathered
4775               ;; sub-threads at all.
4776               (setq gnus-tmp-level -1)))
4777
4778           (setq number (mail-header-number gnus-tmp-header)
4779                 subject (mail-header-subject gnus-tmp-header)
4780                 simp-subject (gnus-simplify-subject-fully subject))
4781
4782           (cond
4783            ;; If the thread has changed subject, we might want to make
4784            ;; this subthread into a root.
4785            ((and (null gnus-thread-ignore-subject)
4786                  (not (zerop gnus-tmp-level))
4787                  gnus-tmp-prev-subject
4788                  (not (string= gnus-tmp-prev-subject simp-subject)))
4789             (setq new-roots (nconc new-roots (list (car thread)))
4790                   thread-end t
4791                   gnus-tmp-header nil))
4792            ;; If the article lies outside the current limit,
4793            ;; then we do not display it.
4794            ((not (memq number gnus-newsgroup-limit))
4795             (setq gnus-tmp-gathered
4796                   (nconc (mapcar
4797                           (lambda (h) (mail-header-number (car h)))
4798                           (cdar thread))
4799                          gnus-tmp-gathered))
4800             (setq gnus-tmp-new-adopts (if (cdar thread)
4801                                           (append gnus-tmp-new-adopts
4802                                                   (cdar thread))
4803                                         gnus-tmp-new-adopts)
4804                   thread-end t
4805                   gnus-tmp-header nil)
4806             (when (zerop gnus-tmp-level)
4807               (setq gnus-tmp-root-expunged t)))
4808            ;; Perhaps this article is to be marked as read?
4809            ((and gnus-summary-mark-below
4810                  (< (or (cdr (assq number gnus-newsgroup-scored))
4811                         default-score)
4812                     gnus-summary-mark-below)
4813                  ;; Don't touch sparse articles.
4814                  (not (gnus-summary-article-sparse-p number))
4815                  (not (gnus-summary-article-ancient-p number)))
4816             (setq gnus-newsgroup-unreads
4817                   (delq number gnus-newsgroup-unreads))
4818             (if gnus-newsgroup-auto-expire
4819                 (setq gnus-newsgroup-expirable
4820                       (gnus-add-to-sorted-list
4821                        gnus-newsgroup-expirable number))
4822               (push (cons number gnus-low-score-mark)
4823                     gnus-newsgroup-reads))))
4824
4825           (when gnus-tmp-header
4826             ;; We may have an old dummy line to output before this
4827             ;; article.
4828             (when (and gnus-tmp-dummy-line
4829                        (gnus-subject-equal
4830                         gnus-tmp-dummy-line
4831                         (mail-header-subject gnus-tmp-header)))
4832               (gnus-summary-insert-dummy-line
4833                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4834               (setq gnus-tmp-dummy-line nil))
4835
4836             ;; Compute the mark.
4837             (setq gnus-tmp-unread (gnus-article-mark number))
4838
4839             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4840                                   gnus-tmp-header gnus-tmp-level)
4841                   gnus-newsgroup-data)
4842
4843             ;; Actually insert the line.
4844             (setq
4845              gnus-tmp-subject-or-nil
4846              (cond
4847               ((and gnus-thread-ignore-subject
4848                     gnus-tmp-prev-subject
4849                     (not (string= gnus-tmp-prev-subject simp-subject)))
4850                subject)
4851               ((zerop gnus-tmp-level)
4852                (if (and (eq gnus-summary-make-false-root 'empty)
4853                         (memq number gnus-tmp-gathered)
4854                         gnus-tmp-prev-subject
4855                         (string= gnus-tmp-prev-subject simp-subject))
4856                    gnus-summary-same-subject
4857                  subject))
4858               (t gnus-summary-same-subject)))
4859             (if (and (eq gnus-summary-make-false-root 'adopt)
4860                      (= gnus-tmp-level 1)
4861                      (memq number gnus-tmp-gathered))
4862                 (setq gnus-tmp-opening-bracket ?\<
4863                       gnus-tmp-closing-bracket ?\>)
4864               (setq gnus-tmp-opening-bracket ?\[
4865                     gnus-tmp-closing-bracket ?\]))
4866             (setq
4867              gnus-tmp-indentation
4868              (aref gnus-thread-indent-array gnus-tmp-level)
4869              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4870              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4871                                 gnus-summary-default-score 0)
4872              gnus-tmp-score-char
4873              (if (or (null gnus-summary-default-score)
4874                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4875                          gnus-summary-zcore-fuzz))
4876                  ?                      ;Whitespace
4877                (if (< gnus-tmp-score gnus-summary-default-score)
4878                    gnus-score-below-mark gnus-score-over-mark))
4879              gnus-tmp-replied
4880              (cond ((memq number gnus-newsgroup-processable)
4881                     gnus-process-mark)
4882                    ((memq number gnus-newsgroup-cached)
4883                     gnus-cached-mark)
4884                    ((memq number gnus-newsgroup-replied)
4885                     gnus-replied-mark)
4886                    ((memq number gnus-newsgroup-forwarded)
4887                     gnus-forwarded-mark)
4888                    ((memq number gnus-newsgroup-saved)
4889                     gnus-saved-mark)
4890                    ((memq number gnus-newsgroup-recent)
4891                     gnus-recent-mark)
4892                    ((memq number gnus-newsgroup-unseen)
4893                     gnus-unseen-mark)
4894                    (t gnus-no-mark))
4895              gnus-tmp-downloaded
4896              (cond ((memq number gnus-newsgroup-undownloaded)
4897                     gnus-undownloaded-mark)
4898                    (gnus-newsgroup-agentized
4899                     gnus-downloaded-mark)
4900                    (t
4901                     gnus-no-mark))
4902              gnus-tmp-from (mail-header-from gnus-tmp-header)
4903              gnus-tmp-name
4904              (cond
4905               ((string-match "<[^>]+> *$" gnus-tmp-from)
4906                (setq beg-match (match-beginning 0))
4907                (or (and (string-match "^\".+\"" gnus-tmp-from)
4908                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4909                    (substring gnus-tmp-from 0 beg-match)))
4910               ((string-match "(.+)" gnus-tmp-from)
4911                (substring gnus-tmp-from
4912                           (1+ (match-beginning 0)) (1- (match-end 0))))
4913               (t gnus-tmp-from))
4914
4915              ;; Do the %B string
4916              gnus-tmp-thread-tree-header-string
4917              (cond
4918               ((not gnus-show-threads) "")
4919               ((zerop gnus-tmp-level)
4920                (cond ((cdar thread)
4921                       (or gnus-sum-thread-tree-root subject))
4922                      (gnus-tmp-new-adopts
4923                       (or gnus-sum-thread-tree-false-root subject))
4924                      (t
4925                       (or gnus-sum-thread-tree-single-indent subject))))
4926               (t
4927                (concat (apply 'concat
4928                               (mapcar (lambda (item)
4929                                         (if (= item 1)
4930                                             gnus-sum-thread-tree-vertical
4931                                           gnus-sum-thread-tree-indent))
4932                                       (cdr (reverse tree-stack))))
4933                        (if (nth 1 thread)
4934                            gnus-sum-thread-tree-leaf-with-other
4935                          gnus-sum-thread-tree-single-leaf)))))
4936             (when (string= gnus-tmp-name "")
4937               (setq gnus-tmp-name gnus-tmp-from))
4938             (unless (numberp gnus-tmp-lines)
4939               (setq gnus-tmp-lines -1))
4940             (if (= gnus-tmp-lines -1)
4941                 (setq gnus-tmp-lines "?")
4942               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4943               (gnus-put-text-property
4944              (point)
4945              (progn (eval gnus-summary-line-format-spec) (point))
4946                'gnus-number number)
4947             (when gnus-visual-p
4948               (forward-line -1)
4949               (gnus-run-hooks 'gnus-summary-update-hook)
4950               (forward-line 1))
4951
4952             (setq gnus-tmp-prev-subject simp-subject)))
4953
4954         (when (nth 1 thread)
4955           (push (list (max 0 gnus-tmp-level)
4956                       (copy-sequence tree-stack)
4957                       (nthcdr 1 thread))
4958                 stack))
4959         (push (if (nth 1 thread) 1 0) tree-stack)
4960         (incf gnus-tmp-level)
4961         (setq threads (if thread-end nil (cdar thread)))
4962         (if gnus-summary-display-while-building
4963             (if building-count
4964                 (progn
4965                   ;; use a set frequency
4966                   (setq building-line-count (1- building-line-count))
4967                   (when (= building-line-count 0)
4968                     (sit-for 0)
4969                     (setq building-line-count
4970                           gnus-summary-display-while-building)))
4971               ;; always
4972               (sit-for 0)))
4973         (unless threads
4974           (setq gnus-tmp-level 0)))))
4975   (gnus-message 7 "Generating summary...done"))
4976
4977 (defun gnus-summary-prepare-unthreaded (headers)
4978   "Generate an unthreaded summary buffer based on HEADERS."
4979   (let (header number mark)
4980
4981     (beginning-of-line)
4982
4983     (while headers
4984       ;; We may have to root out some bad articles...
4985       (when (memq (setq number (mail-header-number
4986                                 (setq header (pop headers))))
4987                   gnus-newsgroup-limit)
4988         ;; Mark article as read when it has a low score.
4989         (when (and gnus-summary-mark-below
4990                    (< (or (cdr (assq number gnus-newsgroup-scored))
4991                           gnus-summary-default-score 0)
4992                       gnus-summary-mark-below)
4993                    (not (gnus-summary-article-ancient-p number)))
4994           (setq gnus-newsgroup-unreads
4995                 (delq number gnus-newsgroup-unreads))
4996           (if gnus-newsgroup-auto-expire
4997               (push number gnus-newsgroup-expirable)
4998             (push (cons number gnus-low-score-mark)
4999                   gnus-newsgroup-reads)))
5000
5001         (setq mark (gnus-article-mark number))
5002         (push (gnus-data-make number mark (1+ (point)) header 0)
5003               gnus-newsgroup-data)
5004         (gnus-summary-insert-line
5005          header 0 number
5006          (memq number gnus-newsgroup-undownloaded)
5007          mark (memq number gnus-newsgroup-replied)
5008          (memq number gnus-newsgroup-expirable)
5009          (mail-header-subject header) nil
5010          (cdr (assq number gnus-newsgroup-scored))
5011          (memq number gnus-newsgroup-processable))))))
5012
5013 (defun gnus-summary-remove-list-identifiers ()
5014   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5015   (let ((regexp (if (consp gnus-list-identifiers)
5016                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5017                   gnus-list-identifiers))
5018         changed subject)
5019     (when regexp
5020       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5021       (dolist (header gnus-newsgroup-headers)
5022         (setq subject (mail-header-subject header)
5023               changed nil)
5024         (while (string-match regexp subject)
5025           (setq subject
5026                 (concat (substring subject 0 (match-beginning 1))
5027                         (substring subject (match-end 0)))
5028                 changed t))
5029         (when changed
5030           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5031             (setq subject
5032                   (concat (substring subject 0 (match-beginning 1))
5033                           (substring subject (match-end 1)))))
5034           (mail-header-set-subject header subject))))))
5035
5036 (defun gnus-fetch-headers (articles)
5037   "Fetch headers of ARTICLES."
5038   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5039     (gnus-message 5 "Fetching headers for %s..." name)
5040     (prog1
5041         (if (eq 'nov
5042                 (setq gnus-headers-retrieved-by
5043                       (gnus-retrieve-headers
5044                        articles gnus-newsgroup-name
5045                        ;; We might want to fetch old headers, but
5046                        ;; not if there is only 1 article.
5047                        (and (or (and
5048                                  (not (eq gnus-fetch-old-headers 'some))
5049                                  (not (numberp gnus-fetch-old-headers)))
5050                                 (> (length articles) 1))
5051                             gnus-fetch-old-headers))))
5052             (gnus-get-newsgroup-headers-xover
5053              articles nil nil gnus-newsgroup-name t)
5054           (gnus-get-newsgroup-headers))
5055       (gnus-message 5 "Fetching headers for %s...done" name))))
5056
5057 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5058   "Select newsgroup GROUP.
5059 If READ-ALL is non-nil, all articles in the group are selected.
5060 If SELECT-ARTICLES, only select those articles from GROUP."
5061   (let* ((entry (gnus-group-entry group))
5062          ;;!!! Dirty hack; should be removed.
5063          (gnus-summary-ignore-duplicates
5064           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5065               t
5066             gnus-summary-ignore-duplicates))
5067          (info (nth 2 entry))
5068          articles fetched-articles cached)
5069
5070     (unless (gnus-check-server
5071              (set (make-local-variable 'gnus-current-select-method)
5072                   (gnus-find-method-for-group group)))
5073       (error "Couldn't open server"))
5074
5075     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5076         (gnus-activate-group group)     ; Or we can activate it...
5077         (progn                          ; Or we bug out.
5078           (when (equal major-mode 'gnus-summary-mode)
5079             (gnus-kill-buffer (current-buffer)))
5080           (error "Couldn't activate group %s: %s"
5081                  group (gnus-status-message group))))
5082
5083     (unless (gnus-request-group group t)
5084       (when (equal major-mode 'gnus-summary-mode)
5085         (gnus-kill-buffer (current-buffer)))
5086       (error "Couldn't request group %s: %s"
5087              group (gnus-status-message group)))
5088
5089     (when gnus-agent
5090       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5091       
5092       (setq gnus-summary-use-undownloaded-faces
5093             (gnus-agent-find-parameter
5094              group
5095              'agent-enable-undownloaded-faces)))
5096
5097     (setq gnus-newsgroup-name group
5098           gnus-newsgroup-unselected nil
5099           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5100
5101     (let ((display (gnus-group-find-parameter group 'display)))
5102       (setq gnus-newsgroup-display
5103             (cond
5104              ((not (zerop (or (car-safe read-all) 0)))
5105               ;; The user entered the group with C-u SPC/RET, let's show
5106               ;; all articles.
5107               'gnus-not-ignore)
5108              ((eq display 'all)
5109               'gnus-not-ignore)
5110              ((arrayp display)
5111               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5112              ((numberp display)
5113               ;; The following is probably the "correct" solution, but
5114               ;; it makes Gnus fetch all headers and then limit the
5115               ;; articles (which is slow), so instead we hack the
5116               ;; select-articles parameter instead. -- Simon Josefsson
5117               ;; <jas@kth.se>
5118               ;;
5119               ;; (gnus-byte-compile
5120               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5121               ;;                         display)))))
5122               (setq select-articles
5123                     (gnus-uncompress-range
5124                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5125                              (if (> tmp 0)
5126                                  tmp
5127                                1))
5128                            (cdr (gnus-active group)))))
5129               nil)
5130              (t
5131               nil))))
5132
5133     (gnus-summary-setup-default-charset)
5134
5135     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5136     (when (gnus-virtual-group-p group)
5137       (setq cached gnus-newsgroup-cached))
5138
5139     (setq gnus-newsgroup-unreads
5140           (gnus-sorted-ndifference
5141            (gnus-sorted-ndifference gnus-newsgroup-unreads
5142                                     gnus-newsgroup-marked)
5143            gnus-newsgroup-dormant))
5144
5145     (setq gnus-newsgroup-processable nil)
5146
5147     (gnus-update-read-articles group gnus-newsgroup-unreads)
5148
5149     ;; Adjust and set lists of article marks.
5150     (when info
5151       (gnus-adjust-marked-articles info))
5152     (if (setq articles select-articles)
5153         (setq gnus-newsgroup-unselected
5154               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5155       (setq articles (gnus-articles-to-read group read-all)))
5156
5157     (cond
5158      ((null articles)
5159       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5160       'quit)
5161      ((eq articles 0) nil)
5162      (t
5163       ;; Init the dependencies hash table.
5164       (setq gnus-newsgroup-dependencies
5165             (gnus-make-hashtable (length articles)))
5166       (gnus-set-global-variables)
5167       ;; Retrieve the headers and read them in.
5168
5169       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5170
5171       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5172       (when cached
5173         (setq gnus-newsgroup-cached cached))
5174
5175       ;; Suppress duplicates?
5176       (when gnus-suppress-duplicates
5177         (gnus-dup-suppress-articles))
5178
5179       ;; Set the initial limit.
5180       (setq gnus-newsgroup-limit (copy-sequence articles))
5181       ;; Remove canceled articles from the list of unread articles.
5182       (setq fetched-articles
5183             (mapcar (lambda (headers) (mail-header-number headers))
5184                     gnus-newsgroup-headers))
5185       (setq gnus-newsgroup-articles fetched-articles)
5186       (setq gnus-newsgroup-unreads
5187             (gnus-sorted-nintersection
5188              gnus-newsgroup-unreads fetched-articles))
5189       (gnus-compute-unseen-list)
5190
5191       ;; Removed marked articles that do not exist.
5192       (gnus-update-missing-marks
5193        (gnus-sorted-difference articles fetched-articles))
5194       ;; We might want to build some more threads first.
5195       (when (and gnus-fetch-old-headers
5196                  (eq gnus-headers-retrieved-by 'nov))
5197         (if (eq gnus-fetch-old-headers 'invisible)
5198             (gnus-build-all-threads)
5199           (gnus-build-old-threads)))
5200       ;; Let the Gnus agent mark articles as read.
5201       (when gnus-agent
5202         (gnus-agent-get-undownloaded-list))
5203       ;; Remove list identifiers from subject
5204       (when gnus-list-identifiers
5205         (gnus-summary-remove-list-identifiers))
5206       ;; Check whether auto-expire is to be done in this group.
5207       (setq gnus-newsgroup-auto-expire
5208             (gnus-group-auto-expirable-p group))
5209       ;; Set up the article buffer now, if necessary.
5210       (unless gnus-single-article-buffer
5211         (gnus-article-setup-buffer))
5212       ;; First and last article in this newsgroup.
5213       (when gnus-newsgroup-headers
5214         (setq gnus-newsgroup-begin
5215               (mail-header-number (car gnus-newsgroup-headers))
5216               gnus-newsgroup-end
5217               (mail-header-number
5218                (gnus-last-element gnus-newsgroup-headers))))
5219       ;; GROUP is successfully selected.
5220       (or gnus-newsgroup-headers t)))))
5221
5222 (defun gnus-compute-unseen-list ()
5223   ;; The `seen' marks are treated specially.
5224   (if (not gnus-newsgroup-seen)
5225       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5226     (setq gnus-newsgroup-unseen
5227           (gnus-inverse-list-range-intersection
5228            gnus-newsgroup-articles gnus-newsgroup-seen))))
5229
5230 (defun gnus-summary-display-make-predicate (display)
5231   (require 'gnus-agent)
5232   (when (= (length display) 1)
5233     (setq display (car display)))
5234   (unless gnus-summary-display-cache
5235     (dolist (elem (append '((unread . unread)
5236                             (read . read)
5237                             (unseen . unseen))
5238                           gnus-article-mark-lists))
5239       (push (cons (cdr elem)
5240                   (gnus-byte-compile
5241                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5242             gnus-summary-display-cache)))
5243   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5244         (gnus-category-predicate-cache gnus-summary-display-cache))
5245     (gnus-get-predicate display)))
5246
5247 ;; Uses the dynamically bound `number' variable.
5248 (eval-when-compile
5249   (defvar number))
5250 (defun gnus-article-marked-p (type &optional article)
5251   (let ((article (or article number)))
5252     (cond
5253      ((eq type 'tick)
5254       (memq article gnus-newsgroup-marked))
5255      ((eq type 'spam)
5256       (memq article gnus-newsgroup-spam-marked))
5257      ((eq type 'unsend)
5258       (memq article gnus-newsgroup-unsendable))
5259      ((eq type 'undownload)
5260       (memq article gnus-newsgroup-undownloaded))
5261      ((eq type 'download)
5262       (memq article gnus-newsgroup-downloadable))
5263      ((eq type 'unread)
5264       (memq article gnus-newsgroup-unreads))
5265      ((eq type 'read)
5266       (memq article gnus-newsgroup-reads))
5267      ((eq type 'dormant)
5268       (memq article gnus-newsgroup-dormant) )
5269      ((eq type 'expire)
5270       (memq article gnus-newsgroup-expirable))
5271      ((eq type 'reply)
5272       (memq article gnus-newsgroup-replied))
5273      ((eq type 'killed)
5274       (memq article gnus-newsgroup-killed))
5275      ((eq type 'bookmark)
5276       (assq article gnus-newsgroup-bookmarks))
5277      ((eq type 'score)
5278       (assq article gnus-newsgroup-scored))
5279      ((eq type 'save)
5280       (memq article gnus-newsgroup-saved))
5281      ((eq type 'cache)
5282       (memq article gnus-newsgroup-cached))
5283      ((eq type 'forward)
5284       (memq article gnus-newsgroup-forwarded))
5285      ((eq type 'seen)
5286       (not (memq article gnus-newsgroup-unseen)))
5287      ((eq type 'recent)
5288       (memq article gnus-newsgroup-recent))
5289      (t t))))
5290
5291 (defun gnus-articles-to-read (group &optional read-all)
5292   "Find out what articles the user wants to read."
5293   (let* ((display (gnus-group-find-parameter group 'display))
5294          (articles
5295           ;; Select all articles if `read-all' is non-nil, or if there
5296           ;; are no unread articles.
5297           (if (or read-all
5298                   (and (zerop (length gnus-newsgroup-marked))
5299                        (zerop (length gnus-newsgroup-unreads)))
5300                   ;; Fetch all if the predicate is non-nil.
5301                   gnus-newsgroup-display)
5302               ;; We want to select the headers for all the articles in
5303               ;; the group, so we select either all the active
5304               ;; articles in the group, or (if that's nil), the
5305               ;; articles in the cache.
5306               (or
5307                (gnus-uncompress-range (gnus-active group))
5308                (gnus-cache-articles-in-group group))
5309             ;; Select only the "normal" subset of articles.
5310             (gnus-sorted-nunion
5311              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5312              gnus-newsgroup-unreads)))
5313          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5314          (scored (length scored-list))
5315          (number (length articles))
5316          (marked (+ (length gnus-newsgroup-marked)
5317                     (length gnus-newsgroup-dormant)))
5318          (select
5319           (cond
5320            ((numberp read-all)
5321             read-all)
5322            ((numberp gnus-newsgroup-display)
5323             gnus-newsgroup-display)
5324            (t
5325             (condition-case ()
5326                 (cond
5327                  ((and (or (<= scored marked) (= scored number))
5328                        (numberp gnus-large-newsgroup)
5329                        (> number gnus-large-newsgroup))
5330                   (let* ((cursor-in-echo-area nil)
5331                          (initial (gnus-parameter-large-newsgroup-initial
5332                                    gnus-newsgroup-name))
5333                          (input
5334                           (read-string
5335                            (format
5336                             "How many articles from %s (%s %d): "
5337                             (gnus-limit-string
5338                              (gnus-group-decoded-name gnus-newsgroup-name)
5339                              35)
5340                             (if initial "max" "default")
5341                             number)
5342                            (if initial
5343                                (cons (number-to-string initial)
5344                                      0)))))
5345                     (if (string-match "^[ \t]*$" input) number input)))
5346                  ((and (> scored marked) (< scored number)
5347                        (> (- scored number) 20))
5348                   (let ((input
5349                          (read-string
5350                           (format "%s %s (%d scored, %d total): "
5351                                   "How many articles from"
5352                                   (gnus-group-decoded-name group)
5353                                   scored number))))
5354                     (if (string-match "^[ \t]*$" input)
5355                         number input)))
5356                  (t number))
5357               (quit
5358                (message "Quit getting the articles to read")
5359                nil))))))
5360     (setq select (if (stringp select) (string-to-number select) select))
5361     (if (or (null select) (zerop select))
5362         select
5363       (if (and (not (zerop scored)) (<= (abs select) scored))
5364           (progn
5365             (setq articles (sort scored-list '<))
5366             (setq number (length articles)))
5367         (setq articles (copy-sequence articles)))
5368
5369       (when (< (abs select) number)
5370         (if (< select 0)
5371             ;; Select the N oldest articles.
5372             (setcdr (nthcdr (1- (abs select)) articles) nil)
5373           ;; Select the N most recent articles.
5374           (setq articles (nthcdr (- number select) articles))))
5375       (setq gnus-newsgroup-unselected
5376             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5377       (when gnus-alter-articles-to-read-function
5378         (setq articles
5379               (sort
5380                (funcall gnus-alter-articles-to-read-function
5381                         gnus-newsgroup-name articles)
5382                '<)))
5383       articles)))
5384
5385 (defun gnus-killed-articles (killed articles)
5386   (let (out)
5387     (while articles
5388       (when (inline (gnus-member-of-range (car articles) killed))
5389         (push (car articles) out))
5390       (setq articles (cdr articles)))
5391     out))
5392
5393 (defun gnus-uncompress-marks (marks)
5394   "Uncompress the mark ranges in MARKS."
5395   (let ((uncompressed '(score bookmark))
5396         out)
5397     (while marks
5398       (if (memq (caar marks) uncompressed)
5399           (push (car marks) out)
5400         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5401       (setq marks (cdr marks)))
5402     out))
5403
5404 (defun gnus-article-mark-to-type (mark)
5405   "Return the type of MARK."
5406   (or (cadr (assq mark gnus-article-special-mark-lists))
5407       'list))
5408
5409 (defun gnus-article-unpropagatable-p (mark)
5410   "Return whether MARK should be propagated to back end."
5411   (memq mark gnus-article-unpropagated-mark-lists))
5412
5413 (defun gnus-adjust-marked-articles (info)
5414   "Set all article lists and remove all marks that are no longer valid."
5415   (let* ((marked-lists (gnus-info-marks info))
5416          (active (gnus-active (gnus-info-group info)))
5417          (min (car active))
5418          (max (cdr active))
5419          (types gnus-article-mark-lists)
5420          marks var articles article mark mark-type
5421          bgn end)
5422
5423     (dolist (marks marked-lists)
5424       (setq mark (car marks)
5425             mark-type (gnus-article-mark-to-type mark)
5426             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5427
5428       ;; We set the variable according to the type of the marks list,
5429       ;; and then adjust the marks to a subset of the active articles.
5430       (cond
5431        ;; Adjust "simple" lists - compressed yet unsorted
5432        ((eq mark-type 'list)
5433         ;; Simultaneously uncompress and clip to active range
5434         ;; See gnus-uncompress-range for a description of possible marks
5435         (let (l lh)
5436           (if (not (cadr marks))
5437               (set var nil)
5438             (setq articles (if (numberp (cddr marks))
5439                                (list (cdr marks))
5440                              (cdr marks))
5441                   lh (cons nil nil)
5442                   l lh)
5443
5444             (while (setq article (pop articles))
5445               (cond ((consp article)
5446                      (setq bgn (max (car article) min)
5447                            end (min (cdr article) max))
5448                      (while (<= bgn end)
5449                        (setq l (setcdr l (cons bgn nil))
5450                              bgn (1+ bgn))))
5451                     ((and (<= min article)
5452                           (>= max article))
5453                      (setq l (setcdr l (cons article nil))))))
5454             (set var (cdr lh)))))
5455        ;; Adjust assocs.
5456        ((eq mark-type 'tuple)
5457         (set var (setq articles (cdr marks)))
5458         (when (not (listp (cdr (symbol-value var))))
5459           (set var (list (symbol-value var))))
5460         (when (not (listp (cdr articles)))
5461           (setq articles (list articles)))
5462         (while articles
5463           (when (or (not (consp (setq article (pop articles))))
5464                     (< (car article) min)
5465                     (> (car article) max))
5466             (set var (delq article (symbol-value var))))))
5467        ;; Adjust ranges (sloppily).
5468        ((eq mark-type 'range)
5469         (cond
5470          ((eq mark 'seen)
5471           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5472           ;; It should be (seen (NUM1 . NUM2)).
5473           (when (numberp (cddr marks))
5474             (setcdr marks (list (cdr marks))))
5475           (setq articles (cdr marks))
5476           (while (and articles
5477                       (or (and (consp (car articles))
5478                                (> min (cdar articles)))
5479                           (and (numberp (car articles))
5480                                (> min (car articles)))))
5481             (pop articles))
5482           (set var articles))))))))
5483
5484 (defun gnus-update-missing-marks (missing)
5485   "Go through the list of MISSING articles and remove them from the mark lists."
5486   (when missing
5487     (let (var m)
5488       ;; Go through all types.
5489       (dolist (elem gnus-article-mark-lists)
5490         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5491           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5492           (when (symbol-value var)
5493             ;; This list has articles.  So we delete all missing
5494             ;; articles from it.
5495             (setq m missing)
5496             (while m
5497               (set var (delq (pop m) (symbol-value var))))))))))
5498
5499 (defun gnus-update-marks ()
5500   "Enter the various lists of marked articles into the newsgroup info list."
5501   (let ((types gnus-article-mark-lists)
5502         (info (gnus-get-info gnus-newsgroup-name))
5503         type list newmarked symbol delta-marks)
5504     (when info
5505       ;; Add all marks lists to the list of marks lists.
5506       (while (setq type (pop types))
5507         (setq list (symbol-value
5508                     (setq symbol
5509                           (intern (format "gnus-newsgroup-%s" (car type))))))
5510
5511         (when list
5512           ;; Get rid of the entries of the articles that have the
5513           ;; default score.
5514           (when (and (eq (cdr type) 'score)
5515                      gnus-save-score
5516                      list)
5517             (let* ((arts list)
5518                    (prev (cons nil list))
5519                    (all prev))
5520               (while arts
5521                 (if (or (not (consp (car arts)))
5522                         (= (cdar arts) gnus-summary-default-score))
5523                     (setcdr prev (cdr arts))
5524                   (setq prev arts))
5525                 (setq arts (cdr arts)))
5526               (setq list (cdr all)))))
5527
5528         (when (eq (cdr type) 'seen)
5529           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5530
5531         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5532           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5533
5534         (when (and (gnus-check-backend-function
5535                     'request-set-mark gnus-newsgroup-name)
5536                    (not (gnus-article-unpropagatable-p (cdr type))))
5537           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5538                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5539                  (add (gnus-remove-from-range
5540                        (gnus-copy-sequence list) old)))
5541             (when add
5542               (push (list add 'add (list (cdr type))) delta-marks))
5543             (when del
5544               (push (list del 'del (list (cdr type))) delta-marks))))
5545
5546         (when list
5547           (push (cons (cdr type) list) newmarked)))
5548
5549       (when delta-marks
5550         (unless (gnus-check-group gnus-newsgroup-name)
5551           (error "Can't open server for %s" gnus-newsgroup-name))
5552         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5553
5554       ;; Enter these new marks into the info of the group.
5555       (if (nthcdr 3 info)
5556           (setcar (nthcdr 3 info) newmarked)
5557         ;; Add the marks lists to the end of the info.
5558         (when newmarked
5559           (setcdr (nthcdr 2 info) (list newmarked))))
5560
5561       ;; Cut off the end of the info if there's nothing else there.
5562       (let ((i 5))
5563         (while (and (> i 2)
5564                     (not (nth i info)))
5565           (when (nthcdr (decf i) info)
5566             (setcdr (nthcdr i info) nil)))))))
5567
5568 (defun gnus-set-mode-line (where)
5569   "Set the mode line of the article or summary buffers.
5570 If WHERE is `summary', the summary mode line format will be used."
5571   ;; Is this mode line one we keep updated?
5572   (when (and (memq where gnus-updated-mode-lines)
5573              (symbol-value
5574               (intern (format "gnus-%s-mode-line-format-spec" where))))
5575     (let (mode-string)
5576       (save-excursion
5577         ;; We evaluate this in the summary buffer since these
5578         ;; variables are buffer-local to that buffer.
5579         (set-buffer gnus-summary-buffer)
5580        ;; We bind all these variables that are used in the `eval' form
5581         ;; below.
5582         (let* ((mformat (symbol-value
5583                          (intern
5584                           (format "gnus-%s-mode-line-format-spec" where))))
5585                (gnus-tmp-group-name (gnus-group-decoded-name
5586                                      gnus-newsgroup-name))
5587                (gnus-tmp-article-number (or gnus-current-article 0))
5588                (gnus-tmp-unread gnus-newsgroup-unreads)
5589                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5590                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5591                (gnus-tmp-unread-and-unselected
5592                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5593                             (zerop gnus-tmp-unselected))
5594                        "")
5595                       ((zerop gnus-tmp-unselected)
5596                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5597                       (t (format "{%d(+%d) more}"
5598                                  gnus-tmp-unread-and-unticked
5599                                  gnus-tmp-unselected))))
5600                (gnus-tmp-subject
5601                 (if (and gnus-current-headers
5602                          (vectorp gnus-current-headers))
5603                     (gnus-mode-string-quote
5604                      (mail-header-subject gnus-current-headers))
5605                   ""))
5606                bufname-length max-len
5607                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5608           (setq mode-string (eval mformat))
5609           (setq bufname-length (if (string-match "%b" mode-string)
5610                                    (- (length
5611                                        (buffer-name
5612                                         (if (eq where 'summary)
5613                                             nil
5614                                           (get-buffer gnus-article-buffer))))
5615                                       2)
5616                                  0))
5617           (setq max-len (max 4 (if gnus-mode-non-string-length
5618                                    (- (window-width)
5619                                       gnus-mode-non-string-length
5620                                       bufname-length)
5621                                  (length mode-string))))
5622           ;; We might have to chop a bit of the string off...
5623           (when (> (length mode-string) max-len)
5624             (setq mode-string
5625                   (concat (truncate-string-to-width mode-string (- max-len 3))
5626                           "...")))
5627           ;; Pad the mode string a bit.
5628           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5629       ;; Update the mode line.
5630       (setq mode-line-buffer-identification
5631             (gnus-mode-line-buffer-identification (list mode-string)))
5632       (set-buffer-modified-p t))))
5633
5634 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5635   "Go through the HEADERS list and add all Xrefs to a hash table.
5636 The resulting hash table is returned, or nil if no Xrefs were found."
5637   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5638          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5639          (xref-hashtb (gnus-make-hashtable))
5640          start group entry number xrefs header)
5641     (while headers
5642       (setq header (pop headers))
5643       (when (and (setq xrefs (mail-header-xref header))
5644                  (not (memq (setq number (mail-header-number header))
5645                             unreads)))
5646         (setq start 0)
5647         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5648           (setq start (match-end 0))
5649           (setq group (if prefix
5650                           (concat prefix (substring xrefs (match-beginning 1)
5651                                                     (match-end 1)))
5652                         (substring xrefs (match-beginning 1) (match-end 1))))
5653           (setq number
5654                 (string-to-int (substring xrefs (match-beginning 2)
5655                                           (match-end 2))))
5656           (if (setq entry (gnus-gethash group xref-hashtb))
5657               (setcdr entry (cons number (cdr entry)))
5658             (gnus-sethash group (cons number nil) xref-hashtb)))))
5659     (and start xref-hashtb)))
5660
5661 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5662   "Look through all the headers and mark the Xrefs as read."
5663   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5664         name info xref-hashtb idlist method nth4)
5665     (save-excursion
5666       (set-buffer gnus-group-buffer)
5667       (when (setq xref-hashtb
5668                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5669         (mapatoms
5670          (lambda (group)
5671            (unless (string= from-newsgroup (setq name (symbol-name group)))
5672              (setq idlist (symbol-value group))
5673              ;; Dead groups are not updated.
5674              (and (prog1
5675                       (setq info (gnus-get-info name))
5676                     (when (stringp (setq nth4 (gnus-info-method info)))
5677                       (setq nth4 (gnus-server-to-method nth4))))
5678                   ;; Only do the xrefs if the group has the same
5679                   ;; select method as the group we have just read.
5680                   (or (gnus-methods-equal-p
5681                        nth4 (gnus-find-method-for-group from-newsgroup))
5682                       virtual
5683                       (equal nth4 (setq method (gnus-find-method-for-group
5684                                                 from-newsgroup)))
5685                       (and (equal (car nth4) (car method))
5686                            (equal (nth 1 nth4) (nth 1 method))))
5687                   gnus-use-cross-reference
5688                   (or (not (eq gnus-use-cross-reference t))
5689                       virtual
5690                       ;; Only do cross-references on subscribed
5691                       ;; groups, if that is what is wanted.
5692                       (<= (gnus-info-level info) gnus-level-subscribed))
5693                   (gnus-group-make-articles-read name idlist))))
5694          xref-hashtb)))))
5695
5696 (defun gnus-compute-read-articles (group articles)
5697   (let* ((entry (gnus-group-entry group))
5698          (info (nth 2 entry))
5699          (active (gnus-active group))
5700          ninfo)
5701     (when entry
5702       ;; First peel off all invalid article numbers.
5703       (when active
5704         (let ((ids articles)
5705               id first)
5706           (while (setq id (pop ids))
5707             (when (and first (> id (cdr active)))
5708               ;; We'll end up in this situation in one particular
5709               ;; obscure situation.  If you re-scan a group and get
5710               ;; a new article that is cross-posted to a different
5711               ;; group that has not been re-scanned, you might get
5712               ;; crossposted article that has a higher number than
5713               ;; Gnus believes possible.  So we re-activate this
5714               ;; group as well.  This might mean doing the
5715               ;; crossposting thingy will *increase* the number
5716               ;; of articles in some groups.  Tsk, tsk.
5717               (setq active (or (gnus-activate-group group) active)))
5718             (when (or (> id (cdr active))
5719                       (< id (car active)))
5720               (setq articles (delq id articles))))))
5721       ;; If the read list is nil, we init it.
5722       (if (and active
5723                (null (gnus-info-read info))
5724                (> (car active) 1))
5725           (setq ninfo (cons 1 (1- (car active))))
5726         (setq ninfo (gnus-info-read info)))
5727       ;; Then we add the read articles to the range.
5728       (gnus-add-to-range
5729        ninfo (setq articles (sort articles '<))))))
5730
5731 (defun gnus-group-make-articles-read (group articles)
5732   "Update the info of GROUP to say that ARTICLES are read."
5733   (let* ((num 0)
5734          (entry (gnus-group-entry group))
5735          (info (nth 2 entry))
5736          (active (gnus-active group))
5737          range)
5738     (when entry
5739       (setq range (gnus-compute-read-articles group articles))
5740       (with-current-buffer gnus-group-buffer
5741         (gnus-undo-register
5742           `(progn
5743              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5744              (gnus-info-set-read ',info ',(gnus-info-read info))
5745              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5746              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5747              (gnus-group-update-group ,group t))))
5748       ;; Add the read articles to the range.
5749       (gnus-info-set-read info range)
5750       (gnus-request-set-mark group (list (list range 'add '(read))))
5751       ;; Then we have to re-compute how many unread
5752       ;; articles there are in this group.
5753       (when active
5754         (cond
5755          ((not range)
5756           (setq num (- (1+ (cdr active)) (car active))))
5757          ((not (listp (cdr range)))
5758           (setq num (- (cdr active) (- (1+ (cdr range))
5759                                        (car range)))))
5760          (t
5761           (while range
5762             (if (numberp (car range))
5763                 (setq num (1+ num))
5764               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5765             (setq range (cdr range)))
5766           (setq num (- (cdr active) num))))
5767         ;; Update the number of unread articles.
5768         (setcar entry num)
5769         ;; Update the group buffer.
5770         (unless (gnus-ephemeral-group-p group)
5771           (gnus-group-update-group group t))))))
5772
5773 (defvar gnus-newsgroup-none-id 0)
5774
5775 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5776   (let ((cur nntp-server-buffer)
5777         (dependencies
5778          (or dependencies
5779              (save-excursion (set-buffer gnus-summary-buffer)
5780                              gnus-newsgroup-dependencies)))
5781         headers id end ref
5782         (mail-parse-charset gnus-newsgroup-charset)
5783         (mail-parse-ignored-charsets
5784          (save-excursion (condition-case nil
5785                              (set-buffer gnus-summary-buffer)
5786                            (error))
5787                          gnus-newsgroup-ignored-charsets)))
5788     (save-excursion
5789       (set-buffer nntp-server-buffer)
5790       ;; Translate all TAB characters into SPACE characters.
5791       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5792       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5793       (ietf-drums-unfold-fws)
5794       (gnus-run-hooks 'gnus-parse-headers-hook)
5795       (let ((case-fold-search t)
5796             in-reply-to header p lines chars)
5797         (goto-char (point-min))
5798         ;; Search to the beginning of the next header.  Error messages
5799         ;; do not begin with 2 or 3.
5800         (while (re-search-forward "^[23][0-9]+ " nil t)
5801           (setq id nil
5802                 ref nil)
5803           ;; This implementation of this function, with nine
5804           ;; search-forwards instead of the one re-search-forward and
5805           ;; a case (which basically was the old function) is actually
5806           ;; about twice as fast, even though it looks messier.  You
5807           ;; can't have everything, I guess.  Speed and elegance
5808           ;; doesn't always go hand in hand.
5809           (setq
5810            header
5811            (vector
5812             ;; Number.
5813             (prog1
5814                 (read cur)
5815               (end-of-line)
5816               (setq p (point))
5817               (narrow-to-region (point)
5818                                 (or (and (search-forward "\n.\n" nil t)
5819                                          (- (point) 2))
5820                                     (point))))
5821             ;; Subject.
5822             (progn
5823               (goto-char p)
5824               (if (search-forward "\nsubject:" nil t)
5825                   (funcall gnus-decode-encoded-word-function
5826                            (nnheader-header-value))
5827                 "(none)"))
5828             ;; From.
5829             (progn
5830               (goto-char p)
5831               (if (search-forward "\nfrom:" nil t)
5832                   (funcall gnus-decode-encoded-word-function
5833                            (nnheader-header-value))
5834                 "(nobody)"))
5835             ;; Date.
5836             (progn
5837               (goto-char p)
5838               (if (search-forward "\ndate:" nil t)
5839                   (nnheader-header-value) ""))
5840             ;; Message-ID.
5841             (progn
5842               (goto-char p)
5843               (setq id (if (re-search-forward
5844                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5845                            ;; We do it this way to make sure the Message-ID
5846                            ;; is (somewhat) syntactically valid.
5847                            (buffer-substring (match-beginning 1)
5848                                              (match-end 1))
5849                          ;; If there was no message-id, we just fake one
5850                          ;; to make subsequent routines simpler.
5851                          (nnheader-generate-fake-message-id))))
5852             ;; References.
5853             (progn
5854               (goto-char p)
5855               (if (search-forward "\nreferences:" nil t)
5856                   (progn
5857                     (setq end (point))
5858                     (prog1
5859                         (nnheader-header-value)
5860                       (setq ref
5861                             (buffer-substring
5862                              (progn
5863                                (end-of-line)
5864                                (search-backward ">" end t)
5865                                (1+ (point)))
5866                              (progn
5867                                (search-backward "<" end t)
5868                                (point))))))
5869                 ;; Get the references from the in-reply-to header if there
5870                 ;; were no references and the in-reply-to header looks
5871                 ;; promising.
5872                 (if (and (search-forward "\nin-reply-to:" nil t)
5873                          (setq in-reply-to (nnheader-header-value))
5874                          (string-match "<[^>]+>" in-reply-to))
5875                     (let (ref2)
5876                       (setq ref (substring in-reply-to (match-beginning 0)
5877                                            (match-end 0)))
5878                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5879                         (setq ref2 (substring in-reply-to (match-beginning 0)
5880                                               (match-end 0)))
5881                         (when (> (length ref2) (length ref))
5882                           (setq ref ref2)))
5883                       ref)
5884                   (setq ref nil))))
5885             ;; Chars.
5886             (progn
5887               (goto-char p)
5888               (if (search-forward "\nchars: " nil t)
5889                   (if (numberp (setq chars (ignore-errors (read cur))))
5890                       chars -1)
5891                 -1))
5892             ;; Lines.
5893             (progn
5894               (goto-char p)
5895               (if (search-forward "\nlines: " nil t)
5896                   (if (numberp (setq lines (ignore-errors (read cur))))
5897                       lines -1)
5898                 -1))
5899             ;; Xref.
5900             (progn
5901               (goto-char p)
5902               (and (search-forward "\nxref:" nil t)
5903                    (nnheader-header-value)))
5904             ;; Extra.
5905             (when gnus-extra-headers
5906               (let ((extra gnus-extra-headers)
5907                     out)
5908                 (while extra
5909                   (goto-char p)
5910                   (when (search-forward
5911                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5912                     (push (cons (car extra) (nnheader-header-value))
5913                           out))
5914                   (pop extra))
5915                 out))))
5916           (when (equal id ref)
5917             (setq ref nil))
5918
5919           (when gnus-alter-header-function
5920             (funcall gnus-alter-header-function header)
5921             (setq id (mail-header-id header)
5922                   ref (gnus-parent-id (mail-header-references header))))
5923
5924           (when (setq header
5925                       (gnus-dependencies-add-header
5926                        header dependencies force-new))
5927             (push header headers))
5928           (goto-char (point-max))
5929           (widen))
5930         (nreverse headers)))))
5931
5932 ;; Goes through the xover lines and returns a list of vectors
5933 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5934                                                   force-new dependencies
5935                                                   group also-fetch-heads)
5936   "Parse the news overview data in the server buffer.
5937 Return a list of headers that match SEQUENCE (see
5938 `nntp-retrieve-headers')."
5939   ;; Get the Xref when the users reads the articles since most/some
5940   ;; NNTP servers do not include Xrefs when using XOVER.
5941   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5942   (let ((mail-parse-charset gnus-newsgroup-charset)
5943         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5944         (cur nntp-server-buffer)
5945         (dependencies (or dependencies gnus-newsgroup-dependencies))
5946         (allp (cond
5947                ((eq gnus-read-all-available-headers t)
5948                 t)
5949                ((stringp gnus-read-all-available-headers)
5950                 (string-match gnus-read-all-available-headers group))
5951                (t
5952                 nil)))
5953         number headers header)
5954     (save-excursion
5955       (set-buffer nntp-server-buffer)
5956       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5957       ;; Allow the user to mangle the headers before parsing them.
5958       (gnus-run-hooks 'gnus-parse-headers-hook)
5959       (goto-char (point-min))
5960       (gnus-parse-without-error
5961         (while (and (or sequence allp)
5962                     (not (eobp)))
5963           (setq number (read cur))
5964           (when (not allp)
5965             (while (and sequence
5966                         (< (car sequence) number))
5967               (setq sequence (cdr sequence))))
5968           (when (and (or allp
5969                          (and sequence
5970                               (eq number (car sequence))))
5971                      (progn
5972                        (setq sequence (cdr sequence))
5973                        (setq header (inline
5974                                       (gnus-nov-parse-line
5975                                        number dependencies force-new)))))
5976             (push header headers))
5977           (forward-line 1)))
5978       ;; A common bug in inn is that if you have posted an article and
5979       ;; then retrieves the active file, it will answer correctly --
5980       ;; the new article is included.  However, a NOV entry for the
5981       ;; article may not have been generated yet, so this may fail.
5982       ;; We work around this problem by retrieving the last few
5983       ;; headers using HEAD.
5984       (if (or (not also-fetch-heads)
5985               (not sequence))
5986           ;; We (probably) got all the headers.
5987           (nreverse headers)
5988         (let ((gnus-nov-is-evil t))
5989           (nconc
5990            (nreverse headers)
5991            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5992              (gnus-get-newsgroup-headers))))))))
5993
5994 (defun gnus-article-get-xrefs ()
5995   "Fill in the Xref value in `gnus-current-headers', if necessary.
5996 This is meant to be called in `gnus-article-internal-prepare-hook'."
5997   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5998                                  gnus-current-headers)))
5999     (or (not gnus-use-cross-reference)
6000         (not headers)
6001         (and (mail-header-xref headers)
6002              (not (string= (mail-header-xref headers) "")))
6003         (let ((case-fold-search t)
6004               xref)
6005           (save-restriction
6006             (nnheader-narrow-to-headers)
6007             (goto-char (point-min))
6008             (when (or (and (not (eobp))
6009                            (eq (downcase (char-after)) ?x)
6010                            (looking-at "Xref:"))
6011                       (search-forward "\nXref:" nil t))
6012               (goto-char (1+ (match-end 0)))
6013               (setq xref (buffer-substring (point) (point-at-eol)))
6014               (mail-header-set-xref headers xref)))))))
6015
6016 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6017   "Find article ID and insert the summary line for that article.
6018 OLD-HEADER can either be a header or a line number to insert
6019 the subject line on."
6020   (let* ((line (and (numberp old-header) old-header))
6021          (old-header (and (vectorp old-header) old-header))
6022          (header (cond ((and old-header use-old-header)
6023                         old-header)
6024                        ((and (numberp id)
6025                              (gnus-number-to-header id))
6026                         (gnus-number-to-header id))
6027                        (t
6028                         (gnus-read-header id))))
6029          (number (and (numberp id) id))
6030          d)
6031     (when header
6032       ;; Rebuild the thread that this article is part of and go to the
6033       ;; article we have fetched.
6034       (when (and (not gnus-show-threads)
6035                  old-header)
6036         (when (and number
6037                    (setq d (gnus-data-find (mail-header-number old-header))))
6038           (goto-char (gnus-data-pos d))
6039           (gnus-data-remove
6040            number
6041            (- (point-at-bol)
6042               (prog1
6043                   (1+ (point-at-eol))
6044                 (gnus-delete-line))))))
6045       (when old-header
6046         (mail-header-set-number header (mail-header-number old-header)))
6047       (setq gnus-newsgroup-sparse
6048             (delq (setq number (mail-header-number header))
6049                   gnus-newsgroup-sparse))
6050       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6051       (push number gnus-newsgroup-limit)
6052       (gnus-rebuild-thread (mail-header-id header) line)
6053       (gnus-summary-goto-subject number nil t))
6054     (when (and (numberp number)
6055                (> number 0))
6056       ;; We have to update the boundaries even if we can't fetch the
6057       ;; article if ID is a number -- so that the next `P' or `N'
6058       ;; command will fetch the previous (or next) article even
6059       ;; if the one we tried to fetch this time has been canceled.
6060       (when (> number gnus-newsgroup-end)
6061         (setq gnus-newsgroup-end number))
6062       (when (< number gnus-newsgroup-begin)
6063         (setq gnus-newsgroup-begin number))
6064       (setq gnus-newsgroup-unselected
6065             (delq number gnus-newsgroup-unselected)))
6066     ;; Report back a success?
6067     (and header (mail-header-number header))))
6068
6069 ;;; Process/prefix in the summary buffer
6070
6071 (defun gnus-summary-work-articles (n)
6072   "Return a list of articles to be worked upon.
6073 The prefix argument, the list of process marked articles, and the
6074 current article will be taken into consideration."
6075   (save-excursion
6076     (set-buffer gnus-summary-buffer)
6077     (cond
6078      (n
6079       ;; A numerical prefix has been given.
6080       (setq n (prefix-numeric-value n))
6081       (let ((backward (< n 0))
6082             (n (abs (prefix-numeric-value n)))
6083             articles article)
6084         (save-excursion
6085           (while
6086               (and (> n 0)
6087                    (push (setq article (gnus-summary-article-number))
6088                          articles)
6089                    (if backward
6090                        (gnus-summary-find-prev nil article)
6091                      (gnus-summary-find-next nil article)))
6092             (decf n)))
6093         (nreverse articles)))
6094      ((and (gnus-region-active-p) (mark))
6095       (message "region active")
6096       ;; Work on the region between point and mark.
6097       (let ((max (max (point) (mark)))
6098             articles article)
6099         (save-excursion
6100           (goto-char (min (min (point) (mark))))
6101           (while
6102               (and
6103                (push (setq article (gnus-summary-article-number)) articles)
6104                (gnus-summary-find-next nil article)
6105                (< (point) max)))
6106           (nreverse articles))))
6107      (gnus-newsgroup-processable
6108       ;; There are process-marked articles present.
6109       ;; Save current state.
6110       (gnus-summary-save-process-mark)
6111       ;; Return the list.
6112       (reverse gnus-newsgroup-processable))
6113      (t
6114       ;; Just return the current article.
6115       (list (gnus-summary-article-number))))))
6116
6117 (defmacro gnus-summary-iterate (arg &rest forms)
6118   "Iterate over the process/prefixed articles and do FORMS.
6119 ARG is the interactive prefix given to the command.  FORMS will be
6120 executed with point over the summary line of the articles."
6121   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6122     `(let ((,articles (gnus-summary-work-articles ,arg)))
6123        (while ,articles
6124          (gnus-summary-goto-subject (car ,articles))
6125          ,@forms
6126          (pop ,articles)))))
6127
6128 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6129 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6130
6131 (defun gnus-summary-save-process-mark ()
6132   "Push the current set of process marked articles on the stack."
6133   (interactive)
6134   (push (copy-sequence gnus-newsgroup-processable)
6135         gnus-newsgroup-process-stack))
6136
6137 (defun gnus-summary-kill-process-mark ()
6138   "Push the current set of process marked articles on the stack and unmark."
6139   (interactive)
6140   (gnus-summary-save-process-mark)
6141   (gnus-summary-unmark-all-processable))
6142
6143 (defun gnus-summary-yank-process-mark ()
6144   "Pop the last process mark state off the stack and restore it."
6145   (interactive)
6146   (unless gnus-newsgroup-process-stack
6147     (error "Empty mark stack"))
6148   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6149
6150 (defun gnus-summary-process-mark-set (set)
6151   "Make SET into the current process marked articles."
6152   (gnus-summary-unmark-all-processable)
6153   (while set
6154     (gnus-summary-set-process-mark (pop set))))
6155
6156 ;;; Searching and stuff
6157
6158 (defun gnus-summary-search-group (&optional backward use-level)
6159   "Search for next unread newsgroup.
6160 If optional argument BACKWARD is non-nil, search backward instead."
6161   (save-excursion
6162     (set-buffer gnus-group-buffer)
6163     (when (gnus-group-search-forward
6164            backward nil (if use-level (gnus-group-group-level) nil))
6165       (gnus-group-group-name))))
6166
6167 (defun gnus-summary-best-group (&optional exclude-group)
6168   "Find the name of the best unread group.
6169 If EXCLUDE-GROUP, do not go to this group."
6170   (save-excursion
6171     (set-buffer gnus-group-buffer)
6172     (save-excursion
6173       (gnus-group-best-unread-group exclude-group))))
6174
6175 (defun gnus-summary-find-next (&optional unread article backward)
6176   (if backward
6177       (gnus-summary-find-prev unread article)
6178     (let* ((dummy (gnus-summary-article-intangible-p))
6179            (article (or article (gnus-summary-article-number)))
6180            (data (gnus-data-find-list article))
6181            result)
6182       (when (and (not dummy)
6183                  (or (not gnus-summary-check-current)
6184                      (not unread)
6185                      (not (gnus-data-unread-p (car data)))))
6186         (setq data (cdr data)))
6187       (when (setq result
6188                   (if unread
6189                       (progn
6190                         (while data
6191                           (unless (memq (gnus-data-number (car data))
6192                                         (cond
6193                                          ((eq gnus-auto-goto-ignores
6194                                               'always-undownloaded)
6195                                           gnus-newsgroup-undownloaded)
6196                                          (gnus-plugged
6197                                           nil)
6198                                          ((eq gnus-auto-goto-ignores
6199                                               'unfetched)
6200                                           gnus-newsgroup-unfetched)
6201                                          ((eq gnus-auto-goto-ignores
6202                                               'undownloaded)
6203                                           gnus-newsgroup-undownloaded)))
6204                             (when (gnus-data-unread-p (car data))
6205                               (setq result (car data)
6206                                     data nil)))
6207                           (setq data (cdr data)))
6208                         result)
6209                     (car data)))
6210         (goto-char (gnus-data-pos result))
6211         (gnus-data-number result)))))
6212
6213 (defun gnus-summary-find-prev (&optional unread article)
6214   (let* ((eobp (eobp))
6215          (article (or article (gnus-summary-article-number)))
6216          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6217          result)
6218     (when (and (not eobp)
6219                (or (not gnus-summary-check-current)
6220                    (not unread)
6221                    (not (gnus-data-unread-p (car data)))))
6222       (setq data (cdr data)))
6223     (when (setq result
6224                 (if unread
6225                     (progn
6226                       (while data
6227                         (unless (memq (gnus-data-number (car data))
6228                                       (cond
6229                                        ((eq gnus-auto-goto-ignores
6230                                             'always-undownloaded)
6231                                         gnus-newsgroup-undownloaded)
6232                                        (gnus-plugged
6233                                         nil)
6234                                        ((eq gnus-auto-goto-ignores
6235                                             'unfetched)
6236                                         gnus-newsgroup-unfetched)
6237                                        ((eq gnus-auto-goto-ignores
6238                                             'undownloaded)
6239                                         gnus-newsgroup-undownloaded)))
6240                           (when (gnus-data-unread-p (car data))
6241                             (setq result (car data)
6242                                   data nil)))
6243                         (setq data (cdr data)))
6244                       result)
6245                   (car data)))
6246       (goto-char (gnus-data-pos result))
6247       (gnus-data-number result))))
6248
6249 (defun gnus-summary-find-subject (subject &optional unread backward article)
6250   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6251          (article (or article (gnus-summary-article-number)))
6252          (articles (gnus-data-list backward))
6253          (arts (gnus-data-find-list article articles))
6254          result)
6255     (when (or (not gnus-summary-check-current)
6256               (not unread)
6257               (not (gnus-data-unread-p (car arts))))
6258       (setq arts (cdr arts)))
6259     (while arts
6260       (and (or (not unread)
6261                (gnus-data-unread-p (car arts)))
6262            (vectorp (gnus-data-header (car arts)))
6263            (gnus-subject-equal
6264             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6265            (setq result (car arts)
6266                  arts nil))
6267       (setq arts (cdr arts)))
6268     (and result
6269          (goto-char (gnus-data-pos result))
6270          (gnus-data-number result))))
6271
6272 (defun gnus-summary-search-forward (&optional unread subject backward)
6273   "Search forward for an article.
6274 If UNREAD, look for unread articles.  If SUBJECT, look for
6275 articles with that subject.  If BACKWARD, search backward instead."
6276   (cond (subject (gnus-summary-find-subject subject unread backward))
6277         (backward (gnus-summary-find-prev unread))
6278         (t (gnus-summary-find-next unread))))
6279
6280 (defun gnus-recenter (&optional n)
6281   "Center point in window and redisplay frame.
6282 Also do horizontal recentering."
6283   (interactive "P")
6284   (when (and gnus-auto-center-summary
6285              (not (eq gnus-auto-center-summary 'vertical)))
6286     (gnus-horizontal-recenter))
6287   (recenter n))
6288
6289 (defun gnus-summary-recenter ()
6290   "Center point in the summary window.
6291 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6292 displayed, no centering will be performed."
6293   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6294   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6295   (interactive)
6296   ;; The user has to want it.
6297   (when gnus-auto-center-summary
6298     (let* ((top (cond ((< (window-height) 4) 0)
6299                       ((< (window-height) 7) 1)
6300                       (t (if (numberp gnus-auto-center-summary)
6301                              gnus-auto-center-summary
6302                            2))))
6303            (height (1- (window-height)))
6304            (bottom (save-excursion (goto-char (point-max))
6305                                    (forward-line (- height))
6306                                    (point)))
6307            (window (get-buffer-window (current-buffer))))
6308       (when (get-buffer-window gnus-article-buffer)
6309         ;; Only do recentering when the article buffer is displayed,
6310         ;; Set the window start to either `bottom', which is the biggest
6311         ;; possible valid number, or the second line from the top,
6312         ;; whichever is the least.
6313         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6314           (if (> bottom top-pos)
6315               ;; Keep the second line from the top visible
6316               (set-window-start window top-pos t)
6317             ;; Try to keep the bottom line visible; if it's partially
6318             ;; obscured, either scroll one more line to make it fully
6319             ;; visible, or revert to using TOP-POS.
6320             (save-excursion
6321               (goto-char (point-max))
6322               (forward-line -1)
6323               (let ((last-line-start (point)))
6324                 (goto-char bottom)
6325                 (set-window-start window (point) t)
6326                 (when (not (pos-visible-in-window-p last-line-start window))
6327                   (forward-line 1)
6328                   (set-window-start window (min (point) top-pos) t)))))))
6329       ;; Do horizontal recentering while we're at it.
6330       (when (and (get-buffer-window (current-buffer) t)
6331                  (not (eq gnus-auto-center-summary 'vertical)))
6332         (let ((selected (selected-window)))
6333           (select-window (get-buffer-window (current-buffer) t))
6334           (gnus-summary-position-point)
6335           (gnus-horizontal-recenter)
6336           (select-window selected))))))
6337
6338 (defun gnus-summary-jump-to-group (newsgroup)
6339   "Move point to NEWSGROUP in group mode buffer."
6340   ;; Keep update point of group mode buffer if visible.
6341   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6342       (save-window-excursion
6343         ;; Take care of tree window mode.
6344         (when (get-buffer-window gnus-group-buffer)
6345           (pop-to-buffer gnus-group-buffer))
6346         (gnus-group-jump-to-group newsgroup))
6347     (save-excursion
6348       ;; Take care of tree window mode.
6349       (if (get-buffer-window gnus-group-buffer)
6350           (pop-to-buffer gnus-group-buffer)
6351         (set-buffer gnus-group-buffer))
6352       (gnus-group-jump-to-group newsgroup))))
6353
6354 ;; This function returns a list of article numbers based on the
6355 ;; difference between the ranges of read articles in this group and
6356 ;; the range of active articles.
6357 (defun gnus-list-of-unread-articles (group)
6358   (let* ((read (gnus-info-read (gnus-get-info group)))
6359          (active (or (gnus-active group) (gnus-activate-group group)))
6360          (last (cdr active))
6361          first nlast unread)
6362     ;; If none are read, then all are unread.
6363     (if (not read)
6364         (setq first (car active))
6365       ;; If the range of read articles is a single range, then the
6366       ;; first unread article is the article after the last read
6367       ;; article.  Sounds logical, doesn't it?
6368       (if (and (not (listp (cdr read)))
6369                (or (< (car read) (car active))
6370                    (progn (setq read (list read))
6371                           nil)))
6372           (setq first (max (car active) (1+ (cdr read))))
6373         ;; `read' is a list of ranges.
6374         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6375                                   (caar read)))
6376                   1)
6377           (setq first (car active)))
6378         (while read
6379           (when first
6380             (while (< first nlast)
6381               (setq unread (cons first unread)
6382                     first (1+ first))))
6383           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6384           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6385           (setq read (cdr read)))))
6386     ;; And add the last unread articles.
6387     (while (<= first last)
6388       (setq unread (cons first unread)
6389             first (1+ first)))
6390     ;; Return the list of unread articles.
6391     (delq 0 (nreverse unread))))
6392
6393 (defun gnus-list-of-read-articles (group)
6394   "Return a list of unread, unticked and non-dormant articles."
6395   (let* ((info (gnus-get-info group))
6396          (marked (gnus-info-marks info))
6397          (active (gnus-active group)))
6398     (and info active
6399          (gnus-list-range-difference
6400           (gnus-list-range-difference
6401            (gnus-sorted-complement
6402             (gnus-uncompress-range active)
6403             (gnus-list-of-unread-articles group))
6404            (cdr (assq 'dormant marked)))
6405           (cdr (assq 'tick marked))))))
6406
6407 ;; This function returns a sequence of article numbers based on the
6408 ;; difference between the ranges of read articles in this group and
6409 ;; the range of active articles.
6410 (defun gnus-sequence-of-unread-articles (group)
6411   (let* ((read (gnus-info-read (gnus-get-info group)))
6412          (active (or (gnus-active group) (gnus-activate-group group)))
6413          (last (cdr active))
6414          first nlast unread)
6415     ;; If none are read, then all are unread.
6416     (if (not read)
6417         (setq first (car active))
6418       ;; If the range of read articles is a single range, then the
6419       ;; first unread article is the article after the last read
6420       ;; article.  Sounds logical, doesn't it?
6421       (if (and (not (listp (cdr read)))
6422                (or (< (car read) (car active))
6423                    (progn (setq read (list read))
6424                           nil)))
6425           (setq first (max (car active) (1+ (cdr read))))
6426         ;; `read' is a list of ranges.
6427         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6428                                   (caar read)))
6429                   1)
6430           (setq first (car active)))
6431         (while read
6432           (when first
6433             (push (cons first nlast) unread))
6434           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6435           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6436           (setq read (cdr read)))))
6437     ;; And add the last unread articles.
6438     (cond ((< first last)
6439            (push (cons first last) unread))
6440           ((= first last)
6441            (push first unread)))
6442     ;; Return the sequence of unread articles.
6443     (delq 0 (nreverse unread))))
6444
6445 ;; Various summary commands
6446
6447 (defun gnus-summary-select-article-buffer ()
6448   "Reconfigure windows to show article buffer."
6449   (interactive)
6450   (if (not (gnus-buffer-live-p gnus-article-buffer))
6451       (error "There is no article buffer for this summary buffer")
6452     (gnus-configure-windows 'article)
6453     (select-window (get-buffer-window gnus-article-buffer))))
6454
6455 (defun gnus-summary-universal-argument (arg)
6456   "Perform any operation on all articles that are process/prefixed."
6457   (interactive "P")
6458   (let ((articles (gnus-summary-work-articles arg))
6459         func article)
6460     (if (eq
6461          (setq
6462           func
6463           (key-binding
6464            (read-key-sequence
6465             (substitute-command-keys
6466              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6467          'undefined)
6468         (gnus-error 1 "Undefined key")
6469       (save-excursion
6470         (while articles
6471           (gnus-summary-goto-subject (setq article (pop articles)))
6472           (let (gnus-newsgroup-processable)
6473             (command-execute func))
6474           (gnus-summary-remove-process-mark article)))))
6475   (gnus-summary-position-point))
6476
6477 (defun gnus-summary-toggle-truncation (&optional arg)
6478   "Toggle truncation of summary lines.
6479 With ARG, turn line truncation on if ARG is positive."
6480   (interactive "P")
6481   (setq truncate-lines
6482         (if (null arg) (not truncate-lines)
6483           (> (prefix-numeric-value arg) 0)))
6484   (redraw-display))
6485
6486 (defun gnus-summary-find-for-reselect ()
6487   "Return the number of an article to stay on across a reselect.
6488 The current article is considered, then following articles, then previous
6489 articles.  An article is sought which is not cancelled and isn't a temporary
6490 insertion from another group.  If there's no such then return a dummy 0."
6491   (let (found)
6492     (dolist (rev '(nil t))
6493       (unless found      ; don't demand the reverse list if we don't need it
6494         (let ((data (gnus-data-find-list
6495                      (gnus-summary-article-number) (gnus-data-list rev))))
6496           (while (and data (not found))
6497             (if (and (< 0 (gnus-data-number (car data)))
6498                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6499                 (setq found (gnus-data-number (car data))))
6500             (setq data (cdr data))))))
6501     (or found 0)))
6502
6503 (defun gnus-summary-reselect-current-group (&optional all rescan)
6504   "Exit and then reselect the current newsgroup.
6505 The prefix argument ALL means to select all articles."
6506   (interactive "P")
6507   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6508     (error "Ephemeral groups can't be reselected"))
6509   (let ((current-subject (gnus-summary-find-for-reselect))
6510         (group gnus-newsgroup-name))
6511     (setq gnus-newsgroup-begin nil)
6512     (gnus-summary-exit nil 'leave-hidden)
6513     ;; We have to adjust the point of group mode buffer because
6514     ;; point was moved to the next unread newsgroup by exiting.
6515     (gnus-summary-jump-to-group group)
6516     (when rescan
6517       (save-excursion
6518         (gnus-group-get-new-news-this-group 1)))
6519     (gnus-group-read-group all t)
6520     (gnus-summary-goto-subject current-subject nil t)))
6521
6522 (defun gnus-summary-rescan-group (&optional all)
6523   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6524   (interactive "P")
6525   (gnus-summary-reselect-current-group all t))
6526
6527 (defun gnus-summary-update-info (&optional non-destructive)
6528   (save-excursion
6529     (let ((group gnus-newsgroup-name))
6530       (when group
6531         (when gnus-newsgroup-kill-headers
6532           (setq gnus-newsgroup-killed
6533                 (gnus-compress-sequence
6534                  (gnus-sorted-union
6535                   (gnus-list-range-intersection
6536                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6537                   gnus-newsgroup-unreads)
6538                  t)))
6539         (unless (listp (cdr gnus-newsgroup-killed))
6540           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6541         (let ((headers gnus-newsgroup-headers))
6542           ;; Set the new ranges of read articles.
6543           (save-excursion
6544             (set-buffer gnus-group-buffer)
6545             (gnus-undo-force-boundary))
6546           (gnus-update-read-articles
6547            group (gnus-sorted-union
6548                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6549           ;; Set the current article marks.
6550           (let ((gnus-newsgroup-scored
6551                  (if (and (not gnus-save-score)
6552                           (not non-destructive))
6553                      nil
6554                    gnus-newsgroup-scored)))
6555             (save-excursion
6556               (gnus-update-marks)))
6557           ;; Do the cross-ref thing.
6558           (when gnus-use-cross-reference
6559             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6560           ;; Do not switch windows but change the buffer to work.
6561           (set-buffer gnus-group-buffer)
6562           (unless (gnus-ephemeral-group-p group)
6563             (gnus-group-update-group group)))))))
6564
6565 (defun gnus-summary-save-newsrc (&optional force)
6566   "Save the current number of read/marked articles in the dribble buffer.
6567 The dribble buffer will then be saved.
6568 If FORCE (the prefix), also save the .newsrc file(s)."
6569   (interactive "P")
6570   (gnus-summary-update-info t)
6571   (if force
6572       (gnus-save-newsrc-file)
6573     (gnus-dribble-save)))
6574
6575 (defun gnus-summary-exit (&optional temporary leave-hidden)
6576   "Exit reading current newsgroup, and then return to group selection mode.
6577 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6578   (interactive)
6579   (gnus-set-global-variables)
6580   (when (gnus-buffer-live-p gnus-article-buffer)
6581     (save-excursion
6582       (set-buffer gnus-article-buffer)
6583       (mm-destroy-parts gnus-article-mime-handles)
6584       ;; Set it to nil for safety reason.
6585       (setq gnus-article-mime-handle-alist nil)
6586       (setq gnus-article-mime-handles nil)))
6587   (gnus-kill-save-kill-buffer)
6588   (gnus-async-halt-prefetch)
6589   (let* ((group gnus-newsgroup-name)
6590          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6591          (gnus-group-is-exiting-p t)
6592          (mode major-mode)
6593          (group-point nil)
6594          (buf (current-buffer)))
6595     (unless quit-config
6596       ;; Do adaptive scoring, and possibly save score files.
6597       (when gnus-newsgroup-adaptive
6598         (gnus-score-adaptive))
6599       (when gnus-use-scoring
6600         (gnus-score-save)))
6601     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6602     ;; If we have several article buffers, we kill them at exit.
6603     (unless gnus-single-article-buffer
6604       (gnus-kill-buffer gnus-original-article-buffer)
6605       (setq gnus-article-current nil))
6606     (when gnus-use-cache
6607       (gnus-cache-possibly-remove-articles)
6608       (gnus-cache-save-buffers))
6609     (gnus-async-prefetch-remove-group group)
6610     (when gnus-suppress-duplicates
6611       (gnus-dup-enter-articles))
6612     (when gnus-use-trees
6613       (gnus-tree-close group))
6614     (when gnus-use-cache
6615       (gnus-cache-write-active))
6616     ;; Remove entries for this group.
6617     (nnmail-purge-split-history (gnus-group-real-name group))
6618     ;; Make all changes in this group permanent.
6619     (unless quit-config
6620       (gnus-run-hooks 'gnus-exit-group-hook)
6621       (gnus-summary-update-info))
6622     (gnus-close-group group)
6623     ;; Make sure where we were, and go to next newsgroup.
6624     (set-buffer gnus-group-buffer)
6625     (unless quit-config
6626       (gnus-group-jump-to-group group))
6627     (gnus-run-hooks 'gnus-summary-exit-hook)
6628     (unless (or quit-config
6629                 ;; If this group has disappeared from the summary
6630                 ;; buffer, don't skip forwards.
6631                 (not (string= group (gnus-group-group-name))))
6632       (gnus-group-next-unread-group 1))
6633     (setq group-point (point))
6634     (if temporary
6635         nil                             ;Nothing to do.
6636       ;; If we have several article buffers, we kill them at exit.
6637       (unless gnus-single-article-buffer
6638         (gnus-kill-buffer gnus-article-buffer)
6639         (gnus-kill-buffer gnus-original-article-buffer)
6640         (setq gnus-article-current nil))
6641       (set-buffer buf)
6642       (if (not gnus-kill-summary-on-exit)
6643           (progn
6644             (gnus-deaden-summary)
6645             (setq mode nil))
6646         ;; We set all buffer-local variables to nil.  It is unclear why
6647         ;; this is needed, but if we don't, buffer-local variables are
6648         ;; not garbage-collected, it seems.  This would the lead to en
6649         ;; ever-growing Emacs.
6650         (gnus-summary-clear-local-variables)
6651         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6652           (gnus-summary-clear-local-variables))
6653         (when (get-buffer gnus-article-buffer)
6654           (bury-buffer gnus-article-buffer))
6655         ;; We clear the global counterparts of the buffer-local
6656         ;; variables as well, just to be on the safe side.
6657         (set-buffer gnus-group-buffer)
6658         (gnus-summary-clear-local-variables)
6659         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6660           (gnus-summary-clear-local-variables))
6661         ;; Return to group mode buffer.
6662         (when (eq mode 'gnus-summary-mode)
6663           (gnus-kill-buffer buf)))
6664       (setq gnus-current-select-method gnus-select-method)
6665       (if leave-hidden
6666           (set-buffer gnus-group-buffer)
6667         (pop-to-buffer gnus-group-buffer))
6668       (if (not quit-config)
6669           (progn
6670             (goto-char group-point)
6671             (unless leave-hidden
6672               (gnus-configure-windows 'group 'force)))
6673         (gnus-handle-ephemeral-exit quit-config))
6674       ;; Clear the current group name.
6675       (unless quit-config
6676         (setq gnus-newsgroup-name nil)))))
6677
6678 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6679 (defun gnus-summary-exit-no-update (&optional no-questions)
6680   "Quit reading current newsgroup without updating read article info."
6681   (interactive)
6682   (let* ((group gnus-newsgroup-name)
6683          (gnus-group-is-exiting-p t)
6684          (gnus-group-is-exiting-without-update-p t)
6685          (quit-config (gnus-group-quit-config group)))
6686     (when (or no-questions
6687               gnus-expert-user
6688               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6689       (gnus-async-halt-prefetch)
6690       (run-hooks 'gnus-summary-prepare-exit-hook)
6691       (when (gnus-buffer-live-p gnus-article-buffer)
6692         (save-excursion
6693           (set-buffer gnus-article-buffer)
6694           (mm-destroy-parts gnus-article-mime-handles)
6695           ;; Set it to nil for safety reason.
6696           (setq gnus-article-mime-handle-alist nil)
6697           (setq gnus-article-mime-handles nil)))
6698       ;; If we have several article buffers, we kill them at exit.
6699       (unless gnus-single-article-buffer
6700         (gnus-kill-buffer gnus-article-buffer)
6701         (gnus-kill-buffer gnus-original-article-buffer)
6702         (setq gnus-article-current nil))
6703       (if (not gnus-kill-summary-on-exit)
6704           (gnus-deaden-summary)
6705         (gnus-close-group group)
6706         (gnus-summary-clear-local-variables)
6707         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6708           (gnus-summary-clear-local-variables))
6709         (set-buffer gnus-group-buffer)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (gnus-kill-buffer gnus-summary-buffer))
6714       (unless gnus-single-article-buffer
6715         (setq gnus-article-current nil))
6716       (when gnus-use-trees
6717         (gnus-tree-close group))
6718       (gnus-async-prefetch-remove-group group)
6719       (when (get-buffer gnus-article-buffer)
6720         (bury-buffer gnus-article-buffer))
6721       ;; Return to the group buffer.
6722       (gnus-configure-windows 'group 'force)
6723       ;; Clear the current group name.
6724       (setq gnus-newsgroup-name nil)
6725       (unless (gnus-ephemeral-group-p group)
6726         (gnus-group-update-group group))
6727       (when (equal (gnus-group-group-name) group)
6728         (gnus-group-next-unread-group 1))
6729       (when quit-config
6730         (gnus-handle-ephemeral-exit quit-config)))))
6731
6732 (defun gnus-handle-ephemeral-exit (quit-config)
6733   "Handle movement when leaving an ephemeral group.
6734 The state which existed when entering the ephemeral is reset."
6735   (if (not (buffer-name (car quit-config)))
6736       (gnus-configure-windows 'group 'force)
6737     (set-buffer (car quit-config))
6738     (cond ((eq major-mode 'gnus-summary-mode)
6739            (gnus-set-global-variables))
6740           ((eq major-mode 'gnus-article-mode)
6741            (save-excursion
6742              ;; The `gnus-summary-buffer' variable may point
6743              ;; to the old summary buffer when using a single
6744              ;; article buffer.
6745              (unless (gnus-buffer-live-p gnus-summary-buffer)
6746                (set-buffer gnus-group-buffer))
6747              (set-buffer gnus-summary-buffer)
6748              (gnus-set-global-variables))))
6749     (if (or (eq (cdr quit-config) 'article)
6750             (eq (cdr quit-config) 'pick))
6751         (progn
6752           ;; The current article may be from the ephemeral group
6753           ;; thus it is best that we reload this article
6754           ;;
6755           ;; If we're exiting from a large digest, this can be
6756           ;; extremely slow.  So, it's better not to reload it. -- jh.
6757           ;;(gnus-summary-show-article)
6758           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6759               (gnus-configure-windows 'pick 'force)
6760             (gnus-configure-windows (cdr quit-config) 'force)))
6761       (gnus-configure-windows (cdr quit-config) 'force))
6762     (when (eq major-mode 'gnus-summary-mode)
6763       (gnus-summary-next-subject 1 nil t)
6764       (gnus-summary-recenter)
6765       (gnus-summary-position-point))))
6766
6767 ;;; Dead summaries.
6768
6769 (defvar gnus-dead-summary-mode-map nil)
6770
6771 (unless gnus-dead-summary-mode-map
6772   (setq gnus-dead-summary-mode-map (make-keymap))
6773   (suppress-keymap gnus-dead-summary-mode-map)
6774   (substitute-key-definition
6775    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6776   (dolist (key '("\C-d" "\r" "\177" [delete]))
6777     (define-key gnus-dead-summary-mode-map
6778       key 'gnus-summary-wake-up-the-dead))
6779   (dolist (key '("q" "Q"))
6780     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6781
6782 (defvar gnus-dead-summary-mode nil
6783   "Minor mode for Gnus summary buffers.")
6784
6785 (defun gnus-dead-summary-mode (&optional arg)
6786   "Minor mode for Gnus summary buffers."
6787   (interactive "P")
6788   (when (eq major-mode 'gnus-summary-mode)
6789     (make-local-variable 'gnus-dead-summary-mode)
6790     (setq gnus-dead-summary-mode
6791           (if (null arg) (not gnus-dead-summary-mode)
6792             (> (prefix-numeric-value arg) 0)))
6793     (when gnus-dead-summary-mode
6794       (add-minor-mode
6795        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6796
6797 (defun gnus-deaden-summary ()
6798   "Make the current summary buffer into a dead summary buffer."
6799   ;; Kill any previous dead summary buffer.
6800   (when (and gnus-dead-summary
6801              (buffer-name gnus-dead-summary))
6802     (save-excursion
6803       (set-buffer gnus-dead-summary)
6804       (when gnus-dead-summary-mode
6805         (kill-buffer (current-buffer)))))
6806   ;; Make this the current dead summary.
6807   (setq gnus-dead-summary (current-buffer))
6808   (gnus-dead-summary-mode 1)
6809   (let ((name (buffer-name)))
6810     (when (string-match "Summary" name)
6811       (rename-buffer
6812        (concat (substring name 0 (match-beginning 0)) "Dead "
6813                (substring name (match-beginning 0)))
6814        t)
6815       (bury-buffer))))
6816
6817 (defun gnus-kill-or-deaden-summary (buffer)
6818   "Kill or deaden the summary BUFFER."
6819   (save-excursion
6820     (when (and (buffer-name buffer)
6821                (not gnus-single-article-buffer))
6822       (save-excursion
6823         (set-buffer buffer)
6824         (gnus-kill-buffer gnus-article-buffer)
6825         (gnus-kill-buffer gnus-original-article-buffer)))
6826     (cond
6827      ;; Kill the buffer.
6828      (gnus-kill-summary-on-exit
6829       (when (and gnus-use-trees
6830                  (gnus-buffer-exists-p buffer))
6831         (save-excursion
6832           (set-buffer buffer)
6833           (gnus-tree-close gnus-newsgroup-name)))
6834       (gnus-kill-buffer buffer))
6835      ;; Deaden the buffer.
6836      ((gnus-buffer-exists-p buffer)
6837       (save-excursion
6838         (set-buffer buffer)
6839         (gnus-deaden-summary))))))
6840
6841 (defun gnus-summary-wake-up-the-dead (&rest args)
6842   "Wake up the dead summary buffer."
6843   (interactive)
6844   (gnus-dead-summary-mode -1)
6845   (let ((name (buffer-name)))
6846     (when (string-match "Dead " name)
6847       (rename-buffer
6848        (concat (substring name 0 (match-beginning 0))
6849                (substring name (match-end 0)))
6850        t)))
6851   (gnus-message 3 "This dead summary is now alive again"))
6852
6853 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6854 (defun gnus-summary-fetch-faq (&optional faq-dir)
6855   "Fetch the FAQ for the current group.
6856 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6857 in."
6858   (interactive
6859    (list
6860     (when current-prefix-arg
6861       (completing-read
6862        "FAQ dir: " (and (listp gnus-group-faq-directory)
6863                         (mapcar (lambda (file) (list file))
6864                                 gnus-group-faq-directory))))))
6865   (let (gnus-faq-buffer)
6866     (when (setq gnus-faq-buffer
6867                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6868       (gnus-configure-windows 'summary-faq))))
6869
6870 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6871 (defun gnus-summary-describe-group (&optional force)
6872   "Describe the current newsgroup."
6873   (interactive "P")
6874   (gnus-group-describe-group force gnus-newsgroup-name))
6875
6876 (defun gnus-summary-describe-briefly ()
6877   "Describe summary mode commands briefly."
6878   (interactive)
6879   (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")))
6880
6881 ;; Walking around group mode buffer from summary mode.
6882
6883 (defun gnus-summary-next-group (&optional no-article target-group backward)
6884   "Exit current newsgroup and then select next unread newsgroup.
6885 If prefix argument NO-ARTICLE is non-nil, no article is selected
6886 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6887 previous group instead."
6888   (interactive "P")
6889   ;; Stop pre-fetching.
6890   (gnus-async-halt-prefetch)
6891   (let ((current-group gnus-newsgroup-name)
6892         (current-buffer (current-buffer))
6893         entered)
6894     ;; First we semi-exit this group to update Xrefs and all variables.
6895     ;; We can't do a real exit, because the window conf must remain
6896     ;; the same in case the user is prompted for info, and we don't
6897     ;; want the window conf to change before that...
6898     (gnus-summary-exit t)
6899     (while (not entered)
6900       ;; Then we find what group we are supposed to enter.
6901       (set-buffer gnus-group-buffer)
6902       (gnus-group-jump-to-group current-group)
6903       (setq target-group
6904             (or target-group
6905                 (if (eq gnus-keep-same-level 'best)
6906                     (gnus-summary-best-group gnus-newsgroup-name)
6907                   (gnus-summary-search-group backward gnus-keep-same-level))))
6908       (if (not target-group)
6909           ;; There are no further groups, so we return to the group
6910           ;; buffer.
6911           (progn
6912             (gnus-message 5 "Returning to the group buffer")
6913             (setq entered t)
6914             (when (gnus-buffer-live-p current-buffer)
6915               (set-buffer current-buffer)
6916               (gnus-summary-exit))
6917             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6918         ;; We try to enter the target group.
6919         (gnus-group-jump-to-group target-group)
6920         (let ((unreads (gnus-group-group-unread)))
6921           (if (and (or (eq t unreads)
6922                        (and unreads (not (zerop unreads))))
6923                    (gnus-summary-read-group
6924                     target-group nil no-article
6925                     (and (buffer-name current-buffer) current-buffer)
6926                     nil backward))
6927               (setq entered t)
6928             (setq current-group target-group
6929                   target-group nil)))))))
6930
6931 (defun gnus-summary-prev-group (&optional no-article)
6932   "Exit current newsgroup and then select previous unread newsgroup.
6933 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6934   (interactive "P")
6935   (gnus-summary-next-group no-article nil t))
6936
6937 ;; Walking around summary lines.
6938
6939 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6940   "Go to the first subject satisfying any non-nil constraint.
6941 If UNREAD is non-nil, the article should be unread.
6942 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6943 If UNSEEN is non-nil, the article should be unseen.
6944 Returns the article selected or nil if there are no matching articles."
6945   (interactive "P")
6946   (cond
6947    ;; Empty summary.
6948    ((null gnus-newsgroup-data)
6949     (gnus-message 3 "No articles in the group")
6950     nil)
6951    ;; Pick the first article.
6952    ((not (or unread undownloaded unseen))
6953     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6954     (gnus-data-number (car gnus-newsgroup-data)))
6955    ;; Find the first unread article.
6956    (t
6957     (let ((data gnus-newsgroup-data))
6958       (while (and data
6959                   (let ((num (gnus-data-number (car data))))
6960                     (or (memq num gnus-newsgroup-unfetched)
6961                         (not (or (and unread
6962                                       (memq num gnus-newsgroup-unreads))
6963                                  (and undownloaded
6964                                       (memq num gnus-newsgroup-undownloaded))
6965                                  (and unseen
6966                                       (memq num gnus-newsgroup-unseen)))))))
6967         (setq data (cdr data)))
6968       (prog1
6969           (if data
6970               (progn
6971                 (goto-char (gnus-data-pos (car data)))
6972                 (gnus-data-number (car data)))
6973             (gnus-message 3 "No more%s articles"
6974                           (let* ((r (when unread " unread"))
6975                                  (d (when undownloaded " undownloaded"))
6976                                  (s (when unseen " unseen"))
6977                                  (l (delq nil (list r d s))))
6978                             (cond ((= 3 (length l))
6979                                    (concat r "," d ", or" s))
6980                                   ((= 2 (length l))
6981                                    (concat (car l) ", or" (cadr l)))
6982                                   ((= 1 (length l))
6983                                    (car l))
6984                                   (t
6985                                    ""))))
6986             nil
6987             )
6988         (gnus-summary-position-point))))))
6989
6990 (defun gnus-summary-next-subject (n &optional unread dont-display)
6991   "Go to next N'th summary line.
6992 If N is negative, go to the previous N'th subject line.
6993 If UNREAD is non-nil, only unread articles are selected.
6994 The difference between N and the actual number of steps taken is
6995 returned."
6996   (interactive "p")
6997   (let ((backward (< n 0))
6998         (n (abs n)))
6999     (while (and (> n 0)
7000                 (if backward
7001                     (gnus-summary-find-prev unread)
7002                   (gnus-summary-find-next unread)))
7003       (unless (zerop (setq n (1- n)))
7004         (gnus-summary-show-thread)))
7005     (when (/= 0 n)
7006       (gnus-message 7 "No more%s articles"
7007                     (if unread " unread" "")))
7008     (unless dont-display
7009       (gnus-summary-recenter)
7010       (gnus-summary-position-point))
7011     n))
7012
7013 (defun gnus-summary-next-unread-subject (n)
7014   "Go to next N'th unread summary line."
7015   (interactive "p")
7016   (gnus-summary-next-subject n t))
7017
7018 (defun gnus-summary-prev-subject (n &optional unread)
7019   "Go to previous N'th summary line.
7020 If optional argument UNREAD is non-nil, only unread article is selected."
7021   (interactive "p")
7022   (gnus-summary-next-subject (- n) unread))
7023
7024 (defun gnus-summary-prev-unread-subject (n)
7025   "Go to previous N'th unread summary line."
7026   (interactive "p")
7027   (gnus-summary-next-subject (- n) t))
7028
7029 (defun gnus-summary-goto-subjects (articles)
7030   "Insert the subject header for ARTICLES in the current buffer."
7031   (save-excursion
7032     (dolist (article articles)
7033       (gnus-summary-goto-subject article t)))
7034   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7035   (gnus-summary-position-point))
7036  
7037 (defun gnus-summary-goto-subject (article &optional force silent)
7038   "Go the subject line of ARTICLE.
7039 If FORCE, also allow jumping to articles not currently shown."
7040   (interactive "nArticle number: ")
7041   (unless (numberp article)
7042     (error "Article %s is not a number" article))
7043   (let ((b (point))
7044         (data (gnus-data-find article)))
7045     ;; We read in the article if we have to.
7046     (and (not data)
7047          force
7048          (gnus-summary-insert-subject
7049           article
7050           (if (or (numberp force) (vectorp force)) force)
7051           t)
7052          (setq data (gnus-data-find article)))
7053     (goto-char b)
7054     (if (not data)
7055         (progn
7056           (unless silent
7057             (gnus-message 3 "Can't find article %d" article))
7058           nil)
7059       (let ((pt (gnus-data-pos data)))
7060         (goto-char pt)
7061         (gnus-summary-set-article-display-arrow pt))
7062       (gnus-summary-position-point)
7063       article)))
7064
7065 ;; Walking around summary lines with displaying articles.
7066
7067 (defun gnus-summary-expand-window (&optional arg)
7068   "Make the summary buffer take up the entire Emacs frame.
7069 Given a prefix, will force an `article' buffer configuration."
7070   (interactive "P")
7071   (if arg
7072       (gnus-configure-windows 'article 'force)
7073     (gnus-configure-windows 'summary 'force)))
7074
7075 (defun gnus-summary-display-article (article &optional all-header)
7076   "Display ARTICLE in article buffer."
7077   (when (gnus-buffer-live-p gnus-article-buffer)
7078     (with-current-buffer gnus-article-buffer
7079       (mm-enable-multibyte)))
7080   (gnus-set-global-variables)
7081   (when (gnus-buffer-live-p gnus-article-buffer)
7082     (with-current-buffer gnus-article-buffer
7083       (setq gnus-article-charset gnus-newsgroup-charset)
7084       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7085       (mm-enable-multibyte)))
7086   (if (null article)
7087       nil
7088     (prog1
7089         (if gnus-summary-display-article-function
7090             (funcall gnus-summary-display-article-function article all-header)
7091           (gnus-article-prepare article all-header))
7092       (gnus-run-hooks 'gnus-select-article-hook)
7093       (when (and gnus-current-article
7094                  (not (zerop gnus-current-article)))
7095         (gnus-summary-goto-subject gnus-current-article))
7096       (gnus-summary-recenter)
7097       (when (and gnus-use-trees gnus-show-threads)
7098         (gnus-possibly-generate-tree article)
7099         (gnus-highlight-selected-tree article))
7100       ;; Successfully display article.
7101       (gnus-article-set-window-start
7102        (cdr (assq article gnus-newsgroup-bookmarks))))))
7103
7104 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7105   "Select the current article.
7106 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7107 non-nil, the article will be re-fetched even if it already present in
7108 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7109 be displayed."
7110   ;; Make sure we are in the summary buffer to work around bbdb bug.
7111   (unless (eq major-mode 'gnus-summary-mode)
7112     (set-buffer gnus-summary-buffer))
7113   (let ((article (or article (gnus-summary-article-number)))
7114         (all-headers (not (not all-headers))) ;Must be t or nil.
7115         gnus-summary-display-article-function)
7116     (and (not pseudo)
7117          (gnus-summary-article-pseudo-p article)
7118          (error "This is a pseudo-article"))
7119     (save-excursion
7120       (set-buffer gnus-summary-buffer)
7121       (if (or (and gnus-single-article-buffer
7122                    (or (null gnus-current-article)
7123                        (null gnus-article-current)
7124                        (null (get-buffer gnus-article-buffer))
7125                        (not (eq article (cdr gnus-article-current)))
7126                        (not (equal (car gnus-article-current)
7127                                    gnus-newsgroup-name))))
7128               (and (not gnus-single-article-buffer)
7129                    (or (null gnus-current-article)
7130                        (not (eq gnus-current-article article))))
7131               force)
7132           ;; The requested article is different from the current article.
7133           (progn
7134             (gnus-summary-display-article article all-headers)
7135             (when (gnus-buffer-live-p gnus-article-buffer)
7136               (with-current-buffer gnus-article-buffer
7137                 (if (not gnus-article-decoded-p) ;; a local variable
7138                     (mm-disable-multibyte))))
7139             (gnus-article-set-window-start
7140              (cdr (assq article gnus-newsgroup-bookmarks)))
7141             article)
7142         'old))))
7143
7144 (defun gnus-summary-force-verify-and-decrypt ()
7145   "Display buttons for signed/encrypted parts and verify/decrypt them."
7146   (interactive)
7147   (let ((mm-verify-option 'known)
7148         (mm-decrypt-option 'known)
7149         (gnus-article-emulate-mime t)
7150         (gnus-buttonized-mime-types (append (list "multipart/signed"
7151                                                   "multipart/encrypted")
7152                                             gnus-buttonized-mime-types)))
7153     (gnus-summary-select-article nil 'force)))
7154
7155 (defun gnus-summary-set-current-mark (&optional current-mark)
7156   "Obsolete function."
7157   nil)
7158
7159 (defun gnus-summary-next-article (&optional unread subject backward push)
7160   "Select the next article.
7161 If UNREAD, only unread articles are selected.
7162 If SUBJECT, only articles with SUBJECT are selected.
7163 If BACKWARD, the previous article is selected instead of the next."
7164   (interactive "P")
7165   (cond
7166    ;; Is there such an article?
7167    ((and (gnus-summary-search-forward unread subject backward)
7168          (or (gnus-summary-display-article (gnus-summary-article-number))
7169              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7170     (gnus-summary-position-point))
7171    ;; If not, we try the first unread, if that is wanted.
7172    ((and subject
7173          gnus-auto-select-same
7174          (gnus-summary-first-unread-article))
7175     (gnus-summary-position-point)
7176     (gnus-message 6 "Wrapped"))
7177    ;; Try to get next/previous article not displayed in this group.
7178    ((and gnus-auto-extend-newsgroup
7179          (not unread) (not subject))
7180     (gnus-summary-goto-article
7181      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7182      nil (count-lines (point-min) (point))))
7183    ;; Go to next/previous group.
7184    (t
7185     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7186       (gnus-summary-jump-to-group gnus-newsgroup-name))
7187     (let ((cmd last-command-char)
7188           (point
7189            (save-excursion
7190              (set-buffer gnus-group-buffer)
7191              (point)))
7192           (group
7193            (if (eq gnus-keep-same-level 'best)
7194                (gnus-summary-best-group gnus-newsgroup-name)
7195              (gnus-summary-search-group backward gnus-keep-same-level))))
7196       ;; For some reason, the group window gets selected.  We change
7197       ;; it back.
7198       (select-window (get-buffer-window (current-buffer)))
7199       ;; Select next unread newsgroup automagically.
7200       (cond
7201        ((or (not gnus-auto-select-next)
7202             (not cmd))
7203         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7204        ((or (eq gnus-auto-select-next 'quietly)
7205             (and (eq gnus-auto-select-next 'slightly-quietly)
7206                  push)
7207             (and (eq gnus-auto-select-next 'almost-quietly)
7208                  (gnus-summary-last-article-p)))
7209         ;; Select quietly.
7210         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7211             (gnus-summary-exit)
7212           (gnus-message 7 "No more%s articles (%s)..."
7213                         (if unread " unread" "")
7214                         (if group (concat "selecting " group)
7215                           "exiting"))
7216           (gnus-summary-next-group nil group backward)))
7217        (t
7218         (when (gnus-key-press-event-p last-input-event)
7219           (gnus-summary-walk-group-buffer
7220            gnus-newsgroup-name cmd unread backward point))))))))
7221
7222 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7223   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7224                       (?\C-p (gnus-group-prev-unread-group 1))))
7225         (cursor-in-echo-area t)
7226         keve key group ended prompt)
7227     (save-excursion
7228       (set-buffer gnus-group-buffer)
7229       (goto-char start)
7230       (setq group
7231             (if (eq gnus-keep-same-level 'best)
7232                 (gnus-summary-best-group gnus-newsgroup-name)
7233               (gnus-summary-search-group backward gnus-keep-same-level))))
7234     (while (not ended)
7235       (setq prompt
7236             (format
7237              "No more%s articles%s " (if unread " unread" "")
7238              (if (and group
7239                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7240                  (format " (Type %s for %s [%s])"
7241                          (single-key-description cmd) group
7242                          (gnus-group-unread group))
7243                (format " (Type %s to exit %s)"
7244                        (single-key-description cmd)
7245                        gnus-newsgroup-name))))
7246       ;; Confirm auto selection.
7247       (setq key (car (setq keve (gnus-read-event-char prompt)))
7248             ended t)
7249       (cond
7250        ((assq key keystrokes)
7251         (let ((obuf (current-buffer)))
7252           (switch-to-buffer gnus-group-buffer)
7253           (when group
7254             (gnus-group-jump-to-group group))
7255           (eval (cadr (assq key keystrokes)))
7256           (setq group (gnus-group-group-name))
7257           (switch-to-buffer obuf))
7258         (setq ended nil))
7259        ((equal key cmd)
7260         (if (or (not group)
7261                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7262             (gnus-summary-exit)
7263           (gnus-summary-next-group nil group backward)))
7264        (t
7265         (push (cdr keve) unread-command-events))))))
7266
7267 (defun gnus-summary-next-unread-article ()
7268   "Select unread article after current one."
7269   (interactive)
7270   (gnus-summary-next-article
7271    (or (not (eq gnus-summary-goto-unread 'never))
7272        (gnus-summary-last-article-p (gnus-summary-article-number)))
7273    (and gnus-auto-select-same
7274         (gnus-summary-article-subject))))
7275
7276 (defun gnus-summary-prev-article (&optional unread subject)
7277   "Select the article after the current one.
7278 If UNREAD is non-nil, only unread articles are selected."
7279   (interactive "P")
7280   (gnus-summary-next-article unread subject t))
7281
7282 (defun gnus-summary-prev-unread-article ()
7283   "Select unread article before current one."
7284   (interactive)
7285   (gnus-summary-prev-article
7286    (or (not (eq gnus-summary-goto-unread 'never))
7287        (gnus-summary-first-article-p (gnus-summary-article-number)))
7288    (and gnus-auto-select-same
7289         (gnus-summary-article-subject))))
7290
7291 (defun gnus-summary-next-page (&optional lines circular stop)
7292   "Show next page of the selected article.
7293 If at the end of the current article, select the next article.
7294 LINES says how many lines should be scrolled up.
7295
7296 If CIRCULAR is non-nil, go to the start of the article instead of
7297 selecting the next article when reaching the end of the current
7298 article.
7299
7300 If STOP is non-nil, just stop when reaching the end of the message.
7301
7302 Also see the variable `gnus-article-skip-boring'."
7303   (interactive "P")
7304   (setq gnus-summary-buffer (current-buffer))
7305   (gnus-set-global-variables)
7306   (let ((article (gnus-summary-article-number))
7307         (article-window (get-buffer-window gnus-article-buffer t))
7308         endp)
7309     ;; If the buffer is empty, we have no article.
7310     (unless article
7311       (error "No article to select"))
7312     (gnus-configure-windows 'article)
7313     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7314         (if (and (eq gnus-summary-goto-unread 'never)
7315                  (not (gnus-summary-last-article-p article)))
7316             (gnus-summary-next-article)
7317           (gnus-summary-next-unread-article))
7318       (if (or (null gnus-current-article)
7319               (null gnus-article-current)
7320               (/= article (cdr gnus-article-current))
7321               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7322           ;; Selected subject is different from current article's.
7323           (gnus-summary-display-article article)
7324         (when article-window
7325           (gnus-eval-in-buffer-window gnus-article-buffer
7326             (setq endp (or (gnus-article-next-page lines)
7327                            (gnus-article-only-boring-p))))
7328           (when endp
7329             (cond (stop
7330                    (gnus-message 3 "End of message"))
7331                   (circular
7332                    (gnus-summary-beginning-of-article))
7333                   (lines
7334                    (gnus-message 3 "End of message"))
7335                   ((null lines)
7336                    (if (and (eq gnus-summary-goto-unread 'never)
7337                             (not (gnus-summary-last-article-p article)))
7338                        (gnus-summary-next-article)
7339                      (gnus-summary-next-unread-article))))))))
7340     (gnus-summary-recenter)
7341     (gnus-summary-position-point)))
7342
7343 (defun gnus-summary-prev-page (&optional lines move)
7344   "Show previous page of selected article.
7345 Argument LINES specifies lines to be scrolled down.
7346 If MOVE, move to the previous unread article if point is at
7347 the beginning of the buffer."
7348   (interactive "P")
7349   (let ((article (gnus-summary-article-number))
7350         (article-window (get-buffer-window gnus-article-buffer t))
7351         endp)
7352     (gnus-configure-windows 'article)
7353     (if (or (null gnus-current-article)
7354             (null gnus-article-current)
7355             (/= article (cdr gnus-article-current))
7356             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7357         ;; Selected subject is different from current article's.
7358         (gnus-summary-display-article article)
7359       (gnus-summary-recenter)
7360       (when article-window
7361         (gnus-eval-in-buffer-window gnus-article-buffer
7362           (setq endp (gnus-article-prev-page lines)))
7363         (when (and move endp)
7364           (cond (lines
7365                  (gnus-message 3 "Beginning of message"))
7366                 ((null lines)
7367                  (if (and (eq gnus-summary-goto-unread 'never)
7368                           (not (gnus-summary-first-article-p article)))
7369                      (gnus-summary-prev-article)
7370                    (gnus-summary-prev-unread-article))))))))
7371   (gnus-summary-position-point))
7372
7373 (defun gnus-summary-prev-page-or-article (&optional lines)
7374   "Show previous page of selected article.
7375 Argument LINES specifies lines to be scrolled down.
7376 If at the beginning of the article, go to the next article."
7377   (interactive "P")
7378   (gnus-summary-prev-page lines t))
7379
7380 (defun gnus-summary-scroll-up (lines)
7381   "Scroll up (or down) one line current article.
7382 Argument LINES specifies lines to be scrolled up (or down if negative)."
7383   (interactive "p")
7384   (gnus-configure-windows 'article)
7385   (gnus-summary-show-thread)
7386   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7387     (gnus-eval-in-buffer-window gnus-article-buffer
7388       (cond ((> lines 0)
7389              (when (gnus-article-next-page lines)
7390                (gnus-message 3 "End of message")))
7391             ((< lines 0)
7392              (gnus-article-prev-page (- lines))))))
7393   (gnus-summary-recenter)
7394   (gnus-summary-position-point))
7395
7396 (defun gnus-summary-scroll-down (lines)
7397   "Scroll down (or up) one line current article.
7398 Argument LINES specifies lines to be scrolled down (or up if negative)."
7399   (interactive "p")
7400   (gnus-summary-scroll-up (- lines)))
7401
7402 (defun gnus-summary-next-same-subject ()
7403   "Select next article which has the same subject as current one."
7404   (interactive)
7405   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7406
7407 (defun gnus-summary-prev-same-subject ()
7408   "Select previous article which has the same subject as current one."
7409   (interactive)
7410   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7411
7412 (defun gnus-summary-next-unread-same-subject ()
7413   "Select next unread article which has the same subject as current one."
7414   (interactive)
7415   (gnus-summary-next-article t (gnus-summary-article-subject)))
7416
7417 (defun gnus-summary-prev-unread-same-subject ()
7418   "Select previous unread article which has the same subject as current one."
7419   (interactive)
7420   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7421
7422 (defun gnus-summary-first-unread-article ()
7423   "Select the first unread article.
7424 Return nil if there are no unread articles."
7425   (interactive)
7426   (prog1
7427       (when (gnus-summary-first-subject t)
7428         (gnus-summary-show-thread)
7429         (gnus-summary-first-subject t)
7430         (gnus-summary-display-article (gnus-summary-article-number)))
7431     (gnus-summary-position-point)))
7432
7433 (defun gnus-summary-first-unread-subject ()
7434   "Place the point on the subject line of the first unread article.
7435 Return nil if there are no unread articles."
7436   (interactive)
7437   (prog1
7438       (when (gnus-summary-first-subject t)
7439         (gnus-summary-show-thread)
7440         (gnus-summary-first-subject t))
7441     (gnus-summary-position-point)))
7442
7443 (defun gnus-summary-first-unseen-subject ()
7444   "Place the point on the subject line of the first unseen article.
7445 Return nil if there are no unseen articles."
7446   (interactive)
7447   (prog1
7448       (when (gnus-summary-first-subject nil nil t)
7449         (gnus-summary-show-thread)
7450         (gnus-summary-first-subject nil nil t))
7451     (gnus-summary-position-point)))
7452
7453 (defun gnus-summary-first-unseen-or-unread-subject ()
7454   "Place the point on the subject line of the first unseen article or,
7455 if all article have been seen, on the subject line of the first unread
7456 article."
7457   (interactive)
7458   (prog1
7459       (unless (when (gnus-summary-first-subject nil nil t)
7460                 (gnus-summary-show-thread)
7461                 (gnus-summary-first-subject nil nil t))
7462         (when (gnus-summary-first-subject t)
7463           (gnus-summary-show-thread)
7464           (gnus-summary-first-subject t)))
7465     (gnus-summary-position-point)))
7466
7467 (defun gnus-summary-first-article ()
7468   "Select the first article.
7469 Return nil if there are no articles."
7470   (interactive)
7471   (prog1
7472       (when (gnus-summary-first-subject)
7473         (gnus-summary-show-thread)
7474         (gnus-summary-first-subject)
7475         (gnus-summary-display-article (gnus-summary-article-number)))
7476     (gnus-summary-position-point)))
7477
7478 (defun gnus-summary-best-unread-article (&optional arg)
7479   "Select the unread article with the highest score.
7480 If given a prefix argument, select the next unread article that has a
7481 score higher than the default score."
7482   (interactive "P")
7483   (let ((article (if arg
7484                      (gnus-summary-better-unread-subject)
7485                    (gnus-summary-best-unread-subject))))
7486     (if article
7487         (gnus-summary-goto-article article)
7488       (error "No unread articles"))))
7489
7490 (defun gnus-summary-best-unread-subject ()
7491   "Select the unread subject with the highest score."
7492   (interactive)
7493   (let ((best -1000000)
7494         (data gnus-newsgroup-data)
7495         article score)
7496     (while data
7497       (and (gnus-data-unread-p (car data))
7498            (> (setq score
7499                     (gnus-summary-article-score (gnus-data-number (car data))))
7500               best)
7501            (setq best score
7502                  article (gnus-data-number (car data))))
7503       (setq data (cdr data)))
7504     (when article
7505       (gnus-summary-goto-subject article))
7506     (gnus-summary-position-point)
7507     article))
7508
7509 (defun gnus-summary-better-unread-subject ()
7510   "Select the first unread subject that has a score over the default score."
7511   (interactive)
7512   (let ((data gnus-newsgroup-data)
7513         article score)
7514     (while (and (setq article (gnus-data-number (car data)))
7515                 (or (gnus-data-read-p (car data))
7516                     (not (> (gnus-summary-article-score article)
7517                             gnus-summary-default-score))))
7518       (setq data (cdr data)))
7519     (when article
7520       (gnus-summary-goto-subject article))
7521     (gnus-summary-position-point)
7522     article))
7523
7524 (defun gnus-summary-last-subject ()
7525   "Go to the last displayed subject line in the group."
7526   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7527     (when article
7528       (gnus-summary-goto-subject article))))
7529
7530 (defun gnus-summary-goto-article (article &optional all-headers force)
7531   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7532 If ALL-HEADERS is non-nil, no header lines are hidden.
7533 If FORCE, go to the article even if it isn't displayed.  If FORCE
7534 is a number, it is the line the article is to be displayed on."
7535   (interactive
7536    (list
7537     (completing-read
7538      "Article number or Message-ID: "
7539      (mapcar (lambda (number) (list (int-to-string number)))
7540              gnus-newsgroup-limit))
7541     current-prefix-arg
7542     t))
7543   (prog1
7544       (if (and (stringp article)
7545                (string-match "@\\|%40" article))
7546           (gnus-summary-refer-article article)
7547         (when (stringp article)
7548           (setq article (string-to-number article)))
7549         (if (gnus-summary-goto-subject article force)
7550             (gnus-summary-display-article article all-headers)
7551           (gnus-message 4 "Couldn't go to article %s" article) nil))
7552     (gnus-summary-position-point)))
7553
7554 (defun gnus-summary-goto-last-article ()
7555   "Go to the previously read article."
7556   (interactive)
7557   (prog1
7558       (when gnus-last-article
7559         (gnus-summary-goto-article gnus-last-article nil t))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-pop-article (number)
7563   "Pop one article off the history and go to the previous.
7564 NUMBER articles will be popped off."
7565   (interactive "p")
7566   (let (to)
7567     (setq gnus-newsgroup-history
7568           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7569     (if to
7570         (gnus-summary-goto-article (car to) nil t)
7571       (error "Article history empty")))
7572   (gnus-summary-position-point))
7573
7574 ;; Summary commands and functions for limiting the summary buffer.
7575
7576 (defun gnus-summary-limit-to-articles (n)
7577   "Limit the summary buffer to the next N articles.
7578 If not given a prefix, use the process marked articles instead."
7579   (interactive "P")
7580   (prog1
7581       (let ((articles (gnus-summary-work-articles n)))
7582         (setq gnus-newsgroup-processable nil)
7583         (gnus-summary-limit articles))
7584     (gnus-summary-position-point)))
7585
7586 (defun gnus-summary-pop-limit (&optional total)
7587   "Restore the previous limit.
7588 If given a prefix, remove all limits."
7589   (interactive "P")
7590   (when total
7591     (setq gnus-newsgroup-limits
7592           (list (mapcar (lambda (h) (mail-header-number h))
7593                         gnus-newsgroup-headers))))
7594   (unless gnus-newsgroup-limits
7595     (error "No limit to pop"))
7596   (prog1
7597       (gnus-summary-limit nil 'pop)
7598     (gnus-summary-position-point)))
7599
7600 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7601   "Limit the summary buffer to articles that have subjects that match a regexp.
7602 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7603   (interactive
7604    (list (read-string (if current-prefix-arg
7605                           "Exclude subject (regexp): "
7606                         "Limit to subject (regexp): "))
7607          nil current-prefix-arg))
7608   (unless header
7609     (setq header "subject"))
7610   (when (not (equal "" subject))
7611     (prog1
7612         (let ((articles (gnus-summary-find-matching
7613                          (or header "subject") subject 'all nil nil
7614                          not-matching)))
7615           (unless articles
7616             (error "Found no matches for \"%s\"" subject))
7617           (gnus-summary-limit articles))
7618       (gnus-summary-position-point))))
7619
7620 (defun gnus-summary-limit-to-author (from &optional not-matching)
7621   "Limit the summary buffer to articles that have authors that match a regexp.
7622 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7623   (interactive
7624    (list (read-string (if current-prefix-arg
7625                           "Exclude author (regexp): "
7626                         "Limit to author (regexp): "))
7627          current-prefix-arg))
7628   (gnus-summary-limit-to-subject from "from" not-matching))
7629
7630 (defun gnus-summary-limit-to-age (age &optional younger-p)
7631   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7632 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7633 articles that are younger than AGE days."
7634   (interactive
7635    (let ((younger current-prefix-arg)
7636          (days-got nil)
7637          days)
7638      (while (not days-got)
7639        (setq days (if younger
7640                       (read-string "Limit to articles younger than (in days, older when negative): ")
7641                     (read-string
7642                      "Limit to articles older than (in days, younger when negative): ")))
7643        (when (> (length days) 0)
7644          (setq days (read days)))
7645        (if (numberp days)
7646            (progn
7647              (setq days-got t)
7648              (when (< days 0)
7649                (setq younger (not younger))
7650                (setq days (* days -1))))
7651          (message "Please enter a number.")
7652          (sleep-for 1)))
7653      (list days younger)))
7654   (prog1
7655       (let ((data gnus-newsgroup-data)
7656             (cutoff (days-to-time age))
7657             articles d date is-younger)
7658         (while (setq d (pop data))
7659           (when (and (vectorp (gnus-data-header d))
7660                      (setq date (mail-header-date (gnus-data-header d))))
7661             (setq is-younger (time-less-p
7662                               (time-since (condition-case ()
7663                                               (date-to-time date)
7664                                             (error '(0 0))))
7665                               cutoff))
7666             (when (if younger-p
7667                       is-younger
7668                     (not is-younger))
7669               (push (gnus-data-number d) articles))))
7670         (gnus-summary-limit (nreverse articles)))
7671     (gnus-summary-position-point)))
7672
7673 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7674   "Limit the summary buffer to articles that match an 'extra' header."
7675   (interactive
7676    (let ((header
7677           (intern
7678            (gnus-completing-read-with-default
7679             (symbol-name (car gnus-extra-headers))
7680             (if current-prefix-arg
7681                 "Exclude extra header:"
7682               "Limit extra header:")
7683             (mapcar (lambda (x)
7684                       (cons (symbol-name x) x))
7685                     gnus-extra-headers)
7686             nil
7687             t))))
7688      (list header
7689            (read-string (format "%s header %s (regexp): "
7690                                 (if current-prefix-arg "Exclude" "Limit to")
7691                                 header))
7692            current-prefix-arg)))
7693   (when (not (equal "" regexp))
7694     (prog1
7695         (let ((articles (gnus-summary-find-matching
7696                          (cons 'extra header) regexp 'all nil nil
7697                          not-matching)))
7698           (unless articles
7699             (error "Found no matches for \"%s\"" regexp))
7700           (gnus-summary-limit articles))
7701       (gnus-summary-position-point))))
7702
7703 (defun gnus-summary-limit-to-display-predicate ()
7704   "Limit the summary buffer to the predicated in the `display' group parameter."
7705   (interactive)
7706   (unless gnus-newsgroup-display
7707     (error "There is no `display' group parameter"))
7708   (let (articles)
7709     (dolist (number gnus-newsgroup-articles)
7710       (when (funcall gnus-newsgroup-display)
7711         (push number articles)))
7712     (gnus-summary-limit articles))
7713   (gnus-summary-position-point))
7714
7715 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7716 (make-obsolete
7717  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7718
7719 (defun gnus-summary-limit-to-unread (&optional all)
7720   "Limit the summary buffer to articles that are not marked as read.
7721 If ALL is non-nil, limit strictly to unread articles."
7722   (interactive "P")
7723   (if all
7724       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7725     (gnus-summary-limit-to-marks
7726      ;; Concat all the marks that say that an article is read and have
7727      ;; those removed.
7728      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7729            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7730            gnus-low-score-mark gnus-expirable-mark
7731            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7732            gnus-duplicate-mark gnus-souped-mark)
7733      'reverse)))
7734
7735 (defun gnus-summary-limit-to-replied (&optional unreplied)
7736   "Limit the summary buffer to replied articles.
7737 If UNREPLIED (the prefix), limit to unreplied articles."
7738   (interactive "P")
7739   (if unreplied
7740       (gnus-summary-limit
7741        (gnus-set-difference gnus-newsgroup-articles
7742         gnus-newsgroup-replied))
7743     (gnus-summary-limit gnus-newsgroup-replied))
7744   (gnus-summary-position-point))
7745
7746 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7747 (make-obsolete 'gnus-summary-delete-marked-with
7748                'gnus-summary-limit-exclude-marks)
7749
7750 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7751   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7752 If REVERSE, limit the summary buffer to articles that are marked
7753 with MARKS.  MARKS can either be a string of marks or a list of marks.
7754 Returns how many articles were removed."
7755   (interactive "sMarks: ")
7756   (gnus-summary-limit-to-marks marks t))
7757
7758 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7759   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7760 If REVERSE (the prefix), limit the summary buffer to articles that are
7761 not marked with MARKS.  MARKS can either be a string of marks or a
7762 list of marks.
7763 Returns how many articles were removed."
7764   (interactive "sMarks: \nP")
7765   (prog1
7766       (let ((data gnus-newsgroup-data)
7767             (marks (if (listp marks) marks
7768                      (append marks nil))) ; Transform to list.
7769             articles)
7770         (while data
7771           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7772                   (memq (gnus-data-mark (car data)) marks))
7773             (push (gnus-data-number (car data)) articles))
7774           (setq data (cdr data)))
7775         (gnus-summary-limit articles))
7776     (gnus-summary-position-point)))
7777
7778 (defun gnus-summary-limit-to-score (score)
7779   "Limit to articles with score at or above SCORE."
7780   (interactive "NLimit to articles with score of at least: ")
7781   (let ((data gnus-newsgroup-data)
7782         articles)
7783     (while data
7784       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7785                 score)
7786         (push (gnus-data-number (car data)) articles))
7787       (setq data (cdr data)))
7788     (prog1
7789         (gnus-summary-limit articles)
7790       (gnus-summary-position-point))))
7791
7792 (defun gnus-summary-limit-to-unseen ()
7793   "Limit to unseen articles."
7794   (interactive)
7795   (prog1
7796       (gnus-summary-limit gnus-newsgroup-unseen)
7797     (gnus-summary-position-point)))
7798
7799 (defun gnus-summary-limit-include-thread (id)
7800   "Display all the hidden articles that is in the thread with ID in it.
7801 When called interactively, ID is the Message-ID of the current
7802 article."
7803   (interactive (list (mail-header-id (gnus-summary-article-header))))
7804   (let ((articles (gnus-articles-in-thread
7805                    (gnus-id-to-thread (gnus-root-id id)))))
7806     (prog1
7807         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7808       (gnus-summary-limit-include-matching-articles
7809        "subject"
7810        (regexp-quote (gnus-simplify-subject-re
7811                       (mail-header-subject (gnus-id-to-header id)))))
7812       (gnus-summary-position-point))))
7813
7814 (defun gnus-summary-limit-include-matching-articles (header regexp)
7815   "Display all the hidden articles that have HEADERs that match REGEXP."
7816   (interactive (list (read-string "Match on header: ")
7817                      (read-string "Regexp: ")))
7818   (let ((articles (gnus-find-matching-articles header regexp)))
7819     (prog1
7820         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7821       (gnus-summary-position-point))))
7822
7823 (defun gnus-summary-insert-dormant-articles ()
7824   "Insert all the dormant articles for this group into the current buffer."
7825   (interactive)
7826   (let ((gnus-verbose (max 6 gnus-verbose)))
7827     (if (not gnus-newsgroup-dormant)
7828         (gnus-message 3 "No cached articles for this group")
7829       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7830
7831 (defun gnus-summary-limit-include-dormant ()
7832   "Display all the hidden articles that are marked as dormant.
7833 Note that this command only works on a subset of the articles currently
7834 fetched for this group."
7835   (interactive)
7836   (unless gnus-newsgroup-dormant
7837     (error "There are no dormant articles in this group"))
7838   (prog1
7839       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7840     (gnus-summary-position-point)))
7841
7842 (defun gnus-summary-limit-exclude-dormant ()
7843   "Hide all dormant articles."
7844   (interactive)
7845   (prog1
7846       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7847     (gnus-summary-position-point)))
7848
7849 (defun gnus-summary-limit-exclude-childless-dormant ()
7850   "Hide all dormant articles that have no children."
7851   (interactive)
7852   (let ((data (gnus-data-list t))
7853         articles d children)
7854     ;; Find all articles that are either not dormant or have
7855     ;; children.
7856     (while (setq d (pop data))
7857       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7858                 (and (setq children
7859                            (gnus-article-children (gnus-data-number d)))
7860                      (let (found)
7861                        (while children
7862                          (when (memq (car children) articles)
7863                            (setq children nil
7864                                  found t))
7865                          (pop children))
7866                        found)))
7867         (push (gnus-data-number d) articles)))
7868     ;; Do the limiting.
7869     (prog1
7870         (gnus-summary-limit articles)
7871       (gnus-summary-position-point))))
7872
7873 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7874   "Mark all unread excluded articles as read.
7875 If ALL, mark even excluded ticked and dormants as read."
7876   (interactive "P")
7877   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7878   (let ((articles (gnus-sorted-ndifference
7879                    (sort
7880                     (mapcar (lambda (h) (mail-header-number h))
7881                             gnus-newsgroup-headers)
7882                     '<)
7883                    gnus-newsgroup-limit))
7884         article)
7885     (setq gnus-newsgroup-unreads
7886           (gnus-sorted-intersection gnus-newsgroup-unreads
7887                                     gnus-newsgroup-limit))
7888     (if all
7889         (setq gnus-newsgroup-dormant nil
7890               gnus-newsgroup-marked nil
7891               gnus-newsgroup-reads
7892               (nconc
7893                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7894                gnus-newsgroup-reads))
7895       (while (setq article (pop articles))
7896         (unless (or (memq article gnus-newsgroup-dormant)
7897                     (memq article gnus-newsgroup-marked))
7898           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7899
7900 (defun gnus-summary-limit (articles &optional pop)
7901   (if pop
7902       ;; We pop the previous limit off the stack and use that.
7903       (setq articles (car gnus-newsgroup-limits)
7904             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7905     ;; We use the new limit, so we push the old limit on the stack.
7906     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7907   ;; Set the limit.
7908   (setq gnus-newsgroup-limit articles)
7909   (let ((total (length gnus-newsgroup-data))
7910         (data (gnus-data-find-list (gnus-summary-article-number)))
7911         (gnus-summary-mark-below nil)   ; Inhibit this.
7912         found)
7913     ;; This will do all the work of generating the new summary buffer
7914     ;; according to the new limit.
7915     (gnus-summary-prepare)
7916     ;; Hide any threads, possibly.
7917     (gnus-summary-maybe-hide-threads)
7918     ;; Try to return to the article you were at, or one in the
7919     ;; neighborhood.
7920     (when data
7921       ;; We try to find some article after the current one.
7922       (while data
7923         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7924           (setq data nil
7925                 found t))
7926         (setq data (cdr data))))
7927     (unless found
7928       ;; If there is no data, that means that we were after the last
7929       ;; article.  The same goes when we can't find any articles
7930       ;; after the current one.
7931       (goto-char (point-max))
7932       (gnus-summary-find-prev))
7933     (gnus-set-mode-line 'summary)
7934     ;; We return how many articles were removed from the summary
7935     ;; buffer as a result of the new limit.
7936     (- total (length gnus-newsgroup-data))))
7937
7938 (defsubst gnus-invisible-cut-children (threads)
7939   (let ((num 0))
7940     (while threads
7941       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7942         (incf num))
7943       (pop threads))
7944     (< num 2)))
7945
7946 (defsubst gnus-cut-thread (thread)
7947   "Go forwards in the thread until we find an article that we want to display."
7948   (when (or (eq gnus-fetch-old-headers 'some)
7949             (eq gnus-fetch-old-headers 'invisible)
7950             (numberp gnus-fetch-old-headers)
7951             (eq gnus-build-sparse-threads 'some)
7952             (eq gnus-build-sparse-threads 'more))
7953     ;; Deal with old-fetched headers and sparse threads.
7954     (while (and
7955             thread
7956             (or
7957              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7958              (gnus-summary-article-ancient-p
7959               (mail-header-number (car thread))))
7960             (if (or (<= (length (cdr thread)) 1)
7961                     (eq gnus-fetch-old-headers 'invisible))
7962                 (setq gnus-newsgroup-limit
7963                       (delq (mail-header-number (car thread))
7964                             gnus-newsgroup-limit)
7965                       thread (cadr thread))
7966               (when (gnus-invisible-cut-children (cdr thread))
7967                 (let ((th (cdr thread)))
7968                   (while th
7969                     (if (memq (mail-header-number (caar th))
7970                               gnus-newsgroup-limit)
7971                         (setq thread (car th)
7972                               th nil)
7973                       (setq th (cdr th))))))))))
7974   thread)
7975
7976 (defun gnus-cut-threads (threads)
7977   "Cut off all uninteresting articles from the beginning of THREADS."
7978   (when (or (eq gnus-fetch-old-headers 'some)
7979             (eq gnus-fetch-old-headers 'invisible)
7980             (numberp gnus-fetch-old-headers)
7981             (eq gnus-build-sparse-threads 'some)
7982             (eq gnus-build-sparse-threads 'more))
7983     (let ((th threads))
7984       (while th
7985         (setcar th (gnus-cut-thread (car th)))
7986         (setq th (cdr th)))))
7987   ;; Remove nixed out threads.
7988   (delq nil threads))
7989
7990 (defun gnus-summary-initial-limit (&optional show-if-empty)
7991   "Figure out what the initial limit is supposed to be on group entry.
7992 This entails weeding out unwanted dormants, low-scored articles,
7993 fetch-old-headers verbiage, and so on."
7994   ;; Most groups have nothing to remove.
7995   (if (or gnus-inhibit-limiting
7996           (and (null gnus-newsgroup-dormant)
7997                (eq gnus-newsgroup-display 'gnus-not-ignore)
7998                (not (eq gnus-fetch-old-headers 'some))
7999                (not (numberp gnus-fetch-old-headers))
8000                (not (eq gnus-fetch-old-headers 'invisible))
8001                (null gnus-summary-expunge-below)
8002                (not (eq gnus-build-sparse-threads 'some))
8003                (not (eq gnus-build-sparse-threads 'more))
8004                (null gnus-thread-expunge-below)
8005                (not gnus-use-nocem)))
8006       ()                                ; Do nothing.
8007     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8008     (setq gnus-newsgroup-limit nil)
8009     (mapatoms
8010      (lambda (node)
8011        (unless (car (symbol-value node))
8012          ;; These threads have no parents -- they are roots.
8013          (let ((nodes (cdr (symbol-value node)))
8014                thread)
8015            (while nodes
8016              (if (and gnus-thread-expunge-below
8017                       (< (gnus-thread-total-score (car nodes))
8018                          gnus-thread-expunge-below))
8019                  (gnus-expunge-thread (pop nodes))
8020                (setq thread (pop nodes))
8021                (gnus-summary-limit-children thread))))))
8022      gnus-newsgroup-dependencies)
8023     ;; If this limitation resulted in an empty group, we might
8024     ;; pop the previous limit and use it instead.
8025     (when (and (not gnus-newsgroup-limit)
8026                show-if-empty)
8027       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8028     gnus-newsgroup-limit))
8029
8030 (defun gnus-summary-limit-children (thread)
8031   "Return 1 if this subthread is visible and 0 if it is not."
8032   ;; First we get the number of visible children to this thread.  This
8033   ;; is done by recursing down the thread using this function, so this
8034   ;; will really go down to a leaf article first, before slowly
8035   ;; working its way up towards the root.
8036   (when thread
8037     (let* ((max-lisp-eval-depth 5000)
8038            (children
8039            (if (cdr thread)
8040                (apply '+ (mapcar 'gnus-summary-limit-children
8041                                  (cdr thread)))
8042              0))
8043           (number (mail-header-number (car thread)))
8044           score)
8045       (if (and
8046            (not (memq number gnus-newsgroup-marked))
8047            (or
8048             ;; If this article is dormant and has absolutely no visible
8049             ;; children, then this article isn't visible.
8050             (and (memq number gnus-newsgroup-dormant)
8051                  (zerop children))
8052             ;; If this is "fetch-old-headered" and there is no
8053             ;; visible children, then we don't want this article.
8054             (and (or (eq gnus-fetch-old-headers 'some)
8055                      (numberp gnus-fetch-old-headers))
8056                  (gnus-summary-article-ancient-p number)
8057                  (zerop children))
8058             ;; If this is "fetch-old-headered" and `invisible', then
8059             ;; we don't want this article.
8060             (and (eq gnus-fetch-old-headers 'invisible)
8061                  (gnus-summary-article-ancient-p number))
8062             ;; If this is a sparsely inserted article with no children,
8063             ;; we don't want it.
8064             (and (eq gnus-build-sparse-threads 'some)
8065                  (gnus-summary-article-sparse-p number)
8066                  (zerop children))
8067             ;; If we use expunging, and this article is really
8068             ;; low-scored, then we don't want this article.
8069             (when (and gnus-summary-expunge-below
8070                        (< (setq score
8071                                 (or (cdr (assq number gnus-newsgroup-scored))
8072                                     gnus-summary-default-score))
8073                           gnus-summary-expunge-below))
8074               ;; We increase the expunge-tally here, but that has
8075               ;; nothing to do with the limits, really.
8076               (incf gnus-newsgroup-expunged-tally)
8077               ;; We also mark as read here, if that's wanted.
8078               (when (and gnus-summary-mark-below
8079                          (< score gnus-summary-mark-below))
8080                 (setq gnus-newsgroup-unreads
8081                       (delq number gnus-newsgroup-unreads))
8082                 (if gnus-newsgroup-auto-expire
8083                     (push number gnus-newsgroup-expirable)
8084                   (push (cons number gnus-low-score-mark)
8085                         gnus-newsgroup-reads)))
8086               t)
8087             ;; Do the `display' group parameter.
8088             (and gnus-newsgroup-display
8089                  (not (funcall gnus-newsgroup-display)))
8090             ;; Check NoCeM things.
8091             (when (and gnus-use-nocem
8092                        (gnus-nocem-unwanted-article-p
8093                         (mail-header-id (car thread))))
8094               (setq gnus-newsgroup-unreads
8095                     (delq number gnus-newsgroup-unreads))
8096               t)))
8097           ;; Nope, invisible article.
8098           0
8099         ;; Ok, this article is to be visible, so we add it to the limit
8100         ;; and return 1.
8101         (push number gnus-newsgroup-limit)
8102         1))))
8103
8104 (defun gnus-expunge-thread (thread)
8105   "Mark all articles in THREAD as read."
8106   (let* ((number (mail-header-number (car thread))))
8107     (incf gnus-newsgroup-expunged-tally)
8108     ;; We also mark as read here, if that's wanted.
8109     (setq gnus-newsgroup-unreads
8110           (delq number gnus-newsgroup-unreads))
8111     (if gnus-newsgroup-auto-expire
8112         (push number gnus-newsgroup-expirable)
8113       (push (cons number gnus-low-score-mark)
8114             gnus-newsgroup-reads)))
8115   ;; Go recursively through all subthreads.
8116   (mapcar 'gnus-expunge-thread (cdr thread)))
8117
8118 ;; Summary article oriented commands
8119
8120 (defun gnus-summary-refer-parent-article (n)
8121   "Refer parent article N times.
8122 If N is negative, go to ancestor -N instead.
8123 The difference between N and the number of articles fetched is returned."
8124   (interactive "p")
8125   (let ((skip 1)
8126         error header ref)
8127     (when (not (natnump n))
8128       (setq skip (abs n)
8129             n 1))
8130     (while (and (> n 0)
8131                 (not error))
8132       (setq header (gnus-summary-article-header))
8133       (if (and (eq (mail-header-number header)
8134                    (cdr gnus-article-current))
8135                (equal gnus-newsgroup-name
8136                       (car gnus-article-current)))
8137           ;; If we try to find the parent of the currently
8138           ;; displayed article, then we take a look at the actual
8139           ;; References header, since this is slightly more
8140           ;; reliable than the References field we got from the
8141           ;; server.
8142           (save-excursion
8143             (set-buffer gnus-original-article-buffer)
8144             (nnheader-narrow-to-headers)
8145             (unless (setq ref (message-fetch-field "references"))
8146               (when (setq ref (message-fetch-field "in-reply-to"))
8147                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8148             (widen))
8149         (setq ref
8150               ;; It's not the current article, so we take a bet on
8151               ;; the value we got from the server.
8152               (mail-header-references header)))
8153       (if (and ref
8154                (not (equal ref "")))
8155           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8156             (gnus-message 1 "Couldn't find parent"))
8157         (gnus-message 1 "No references in article %d"
8158                       (gnus-summary-article-number))
8159         (setq error t))
8160       (decf n))
8161     (gnus-summary-position-point)
8162     n))
8163
8164 (defun gnus-summary-refer-references ()
8165   "Fetch all articles mentioned in the References header.
8166 Return the number of articles fetched."
8167   (interactive)
8168   (let ((ref (mail-header-references (gnus-summary-article-header)))
8169         (current (gnus-summary-article-number))
8170         (n 0))
8171     (if (or (not ref)
8172             (equal ref ""))
8173         (error "No References in the current article")
8174       ;; For each Message-ID in the References header...
8175       (while (string-match "<[^>]*>" ref)
8176         (incf n)
8177         ;; ... fetch that article.
8178         (gnus-summary-refer-article
8179          (prog1 (match-string 0 ref)
8180            (setq ref (substring ref (match-end 0))))))
8181       (gnus-summary-goto-subject current)
8182       (gnus-summary-position-point)
8183       n)))
8184
8185 (defun gnus-summary-refer-thread (&optional limit)
8186   "Fetch all articles in the current thread.
8187 If LIMIT (the numerical prefix), fetch that many old headers instead
8188 of what's specified by the `gnus-refer-thread-limit' variable."
8189   (interactive "P")
8190   (let ((id (mail-header-id (gnus-summary-article-header)))
8191         (limit (if limit (prefix-numeric-value limit)
8192                  gnus-refer-thread-limit)))
8193     (unless (eq gnus-fetch-old-headers 'invisible)
8194       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8195       ;; Retrieve the headers and read them in.
8196       (if (eq (if (numberp limit)
8197                   (gnus-retrieve-headers
8198                    (list (min
8199                           (+ (mail-header-number
8200                               (gnus-summary-article-header))
8201                              limit)
8202                           gnus-newsgroup-end))
8203                    gnus-newsgroup-name (* limit 2))
8204                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8205                 ;; headers.
8206                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8207                                        gnus-newsgroup-name limit))
8208               'nov)
8209           (gnus-build-all-threads)
8210         (error "Can't fetch thread from back ends that don't support NOV"))
8211       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8212     (gnus-summary-limit-include-thread id)))
8213
8214 (defun gnus-summary-refer-article (message-id)
8215   "Fetch an article specified by MESSAGE-ID."
8216   (interactive "sMessage-ID: ")
8217   (when (and (stringp message-id)
8218              (not (zerop (length message-id))))
8219     (setq message-id (gnus-replace-in-string message-id " " ""))
8220     ;; Construct the correct Message-ID if necessary.
8221     ;; Suggested by tale@pawl.rpi.edu.
8222     (unless (string-match "^<" message-id)
8223       (setq message-id (concat "<" message-id)))
8224     (unless (string-match ">$" message-id)
8225       (setq message-id (concat message-id ">")))
8226     ;; People often post MIDs from URLs, so unhex it:
8227     (unless (string-match "@" message-id)
8228       (setq message-id (gnus-url-unhex-string message-id)))
8229     (let* ((header (gnus-id-to-header message-id))
8230            (sparse (and header
8231                         (gnus-summary-article-sparse-p
8232                          (mail-header-number header))
8233                         (memq (mail-header-number header)
8234                               gnus-newsgroup-limit)))
8235            number)
8236       (cond
8237        ;; If the article is present in the buffer we just go to it.
8238        ((and header
8239              (or (not (gnus-summary-article-sparse-p
8240                        (mail-header-number header)))
8241                  sparse))
8242         (prog1
8243             (gnus-summary-goto-article
8244              (mail-header-number header) nil t)
8245           (when sparse
8246             (gnus-summary-update-article (mail-header-number header)))))
8247        (t
8248         ;; We fetch the article.
8249         (catch 'found
8250           (dolist (gnus-override-method (gnus-refer-article-methods))
8251             (when (and (gnus-check-server gnus-override-method)
8252                        ;; Fetch the header,
8253                        (setq number (gnus-summary-insert-subject message-id)))
8254               ;; and display the article.
8255               (gnus-summary-select-article nil nil nil number)
8256               (throw 'found t)))
8257           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8258
8259 (defun gnus-refer-article-methods ()
8260   "Return a list of referable methods."
8261   (cond
8262    ;; No method, so we default to current and native.
8263    ((null gnus-refer-article-method)
8264     (list gnus-current-select-method gnus-select-method))
8265    ;; Current.
8266    ((eq 'current gnus-refer-article-method)
8267     (list gnus-current-select-method))
8268    ;; List of select methods.
8269    ((not (and (symbolp (car gnus-refer-article-method))
8270               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8271     (let (out)
8272       (dolist (method gnus-refer-article-method)
8273         (push (if (eq 'current method)
8274                   gnus-current-select-method
8275                 method)
8276               out))
8277       (nreverse out)))
8278    ;; One single select method.
8279    (t
8280     (list gnus-refer-article-method))))
8281
8282 (defun gnus-summary-edit-parameters ()
8283   "Edit the group parameters of the current group."
8284   (interactive)
8285   (gnus-group-edit-group gnus-newsgroup-name 'params))
8286
8287 (defun gnus-summary-customize-parameters ()
8288   "Customize the group parameters of the current group."
8289   (interactive)
8290   (gnus-group-customize gnus-newsgroup-name))
8291
8292 (defun gnus-summary-enter-digest-group (&optional force)
8293   "Enter an nndoc group based on the current article.
8294 If FORCE, force a digest interpretation.  If not, try
8295 to guess what the document format is."
8296   (interactive "P")
8297   (let ((conf gnus-current-window-configuration))
8298     (save-window-excursion
8299       (save-excursion
8300         (let (gnus-article-prepare-hook
8301               gnus-display-mime-function
8302               gnus-break-pages)
8303           (gnus-summary-select-article))))
8304     (setq gnus-current-window-configuration conf)
8305     (let* ((name (format "%s-%d"
8306                          (gnus-group-prefixed-name
8307                           gnus-newsgroup-name (list 'nndoc ""))
8308                          (save-excursion
8309                            (set-buffer gnus-summary-buffer)
8310                            gnus-current-article)))
8311            (ogroup gnus-newsgroup-name)
8312            (params (append (gnus-info-params (gnus-get-info ogroup))
8313                            (list (cons 'to-group ogroup))
8314                            (list (cons 'parent-group ogroup))
8315                            (list (cons 'save-article-group ogroup))))
8316            (case-fold-search t)
8317            (buf (current-buffer))
8318            dig to-address)
8319       (save-excursion
8320         (set-buffer gnus-original-article-buffer)
8321         ;; Have the digest group inherit the main mail address of
8322         ;; the parent article.
8323         (when (setq to-address (or (gnus-fetch-field "reply-to")
8324                                    (gnus-fetch-field "from")))
8325           (setq params (append
8326                         (list (cons 'to-address
8327                                     (funcall gnus-decode-encoded-word-function
8328                                              to-address))))))
8329         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8330         (insert-buffer-substring gnus-original-article-buffer)
8331         ;; Remove lines that may lead nndoc to misinterpret the
8332         ;; document type.
8333         (narrow-to-region
8334          (goto-char (point-min))
8335          (or (search-forward "\n\n" nil t) (point)))
8336         (goto-char (point-min))
8337         (delete-matching-lines "^Path:\\|^From ")
8338         (widen))
8339       (unwind-protect
8340           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8341                     (gnus-newsgroup-ephemeral-ignored-charsets
8342                      gnus-newsgroup-ignored-charsets))
8343                 (gnus-group-read-ephemeral-group
8344                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8345                               (nndoc-article-type
8346                                ,(if force 'mbox 'guess)))
8347                  t nil nil nil
8348                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8349                                                         "ADAPT")))))
8350               ;; Make all postings to this group go to the parent group.
8351               (nconc (gnus-info-params (gnus-get-info name))
8352                      params)
8353             ;; Couldn't select this doc group.
8354             (switch-to-buffer buf)
8355             (gnus-set-global-variables)
8356             (gnus-configure-windows 'summary)
8357             (gnus-message 3 "Article couldn't be entered?"))
8358         (kill-buffer dig)))))
8359
8360 (defun gnus-summary-read-document (n)
8361   "Open a new group based on the current article(s).
8362 This will allow you to read digests and other similar
8363 documents as newsgroups.
8364 Obeys the standard process/prefix convention."
8365   (interactive "P")
8366   (let* ((articles (gnus-summary-work-articles n))
8367          (ogroup gnus-newsgroup-name)
8368          (params (append (gnus-info-params (gnus-get-info ogroup))
8369                          (list (cons 'to-group ogroup))))
8370          article group egroup groups vgroup)
8371     (while (setq article (pop articles))
8372       (setq group (format "%s-%d" gnus-newsgroup-name article))
8373       (gnus-summary-remove-process-mark article)
8374       (when (gnus-summary-display-article article)
8375         (save-excursion
8376           (with-temp-buffer
8377             (insert-buffer-substring gnus-original-article-buffer)
8378             ;; Remove some headers that may lead nndoc to make
8379             ;; the wrong guess.
8380             (message-narrow-to-head)
8381             (goto-char (point-min))
8382             (delete-matching-lines "^Path:\\|^From ")
8383             (widen)
8384             (if (setq egroup
8385                       (gnus-group-read-ephemeral-group
8386                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8387                                      (nndoc-article-type guess))
8388                        t nil t))
8389                 (progn
8390             ;; Make all postings to this group go to the parent group.
8391                   (nconc (gnus-info-params (gnus-get-info egroup))
8392                          params)
8393                   (push egroup groups))
8394               ;; Couldn't select this doc group.
8395               (gnus-error 3 "Article couldn't be entered"))))))
8396     ;; Now we have selected all the documents.
8397     (cond
8398      ((not groups)
8399       (error "None of the articles could be interpreted as documents"))
8400      ((gnus-group-read-ephemeral-group
8401        (setq vgroup (format
8402                      "nnvirtual:%s-%s" gnus-newsgroup-name
8403                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8404        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8405        t
8406        (cons (current-buffer) 'summary)))
8407      (t
8408       (error "Couldn't select virtual nndoc group")))))
8409
8410 (defun gnus-summary-isearch-article (&optional regexp-p)
8411   "Do incremental search forward on the current article.
8412 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8413   (interactive "P")
8414   (gnus-summary-select-article)
8415   (gnus-configure-windows 'article)
8416   (gnus-eval-in-buffer-window gnus-article-buffer
8417     (save-restriction
8418       (widen)
8419       (isearch-forward regexp-p))))
8420
8421 (defun gnus-summary-search-article-forward (regexp &optional backward)
8422   "Search for an article containing REGEXP forward.
8423 If BACKWARD, search backward instead."
8424   (interactive
8425    (list (read-string
8426           (format "Search article %s (regexp%s): "
8427                   (if current-prefix-arg "backward" "forward")
8428                   (if gnus-last-search-regexp
8429                       (concat ", default " gnus-last-search-regexp)
8430                     "")))
8431          current-prefix-arg))
8432   (if (string-equal regexp "")
8433       (setq regexp (or gnus-last-search-regexp ""))
8434     (setq gnus-last-search-regexp regexp)
8435     (setq gnus-article-before-search gnus-current-article))
8436   ;; Intentionally set gnus-last-article.
8437   (setq gnus-last-article gnus-article-before-search)
8438   (let ((gnus-last-article gnus-last-article))
8439     (if (gnus-summary-search-article regexp backward)
8440         (gnus-summary-show-thread)
8441       (error "Search failed: \"%s\"" regexp))))
8442
8443 (defun gnus-summary-search-article-backward (regexp)
8444   "Search for an article containing REGEXP backward."
8445   (interactive
8446    (list (read-string
8447           (format "Search article backward (regexp%s): "
8448                   (if gnus-last-search-regexp
8449                       (concat ", default " gnus-last-search-regexp)
8450                     "")))))
8451   (gnus-summary-search-article-forward regexp 'backward))
8452
8453 (defun gnus-summary-search-article (regexp &optional backward)
8454   "Search for an article containing REGEXP.
8455 Optional argument BACKWARD means do search for backward.
8456 `gnus-select-article-hook' is not called during the search."
8457   ;; We have to require this here to make sure that the following
8458   ;; dynamic binding isn't shadowed by autoloading.
8459   (require 'gnus-async)
8460   (require 'gnus-art)
8461   (let ((gnus-select-article-hook nil)  ;Disable hook.
8462         (gnus-article-prepare-hook nil)
8463         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8464         (gnus-use-article-prefetch nil)
8465         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8466         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8467         (gnus-visual nil)
8468         (gnus-keep-backlog nil)
8469         (gnus-break-pages nil)
8470         (gnus-summary-display-arrow nil)
8471         (gnus-updated-mode-lines nil)
8472         (gnus-auto-center-summary nil)
8473         (sum (current-buffer))
8474         (gnus-display-mime-function nil)
8475         (found nil)
8476         point)
8477     (gnus-save-hidden-threads
8478       (gnus-summary-select-article)
8479       (set-buffer gnus-article-buffer)
8480       (goto-char (window-point (get-buffer-window (current-buffer))))
8481       (when backward
8482         (forward-line -1))
8483       (while (not found)
8484         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8485         (if (if backward
8486                 (re-search-backward regexp nil t)
8487               (re-search-forward regexp nil t))
8488             ;; We found the regexp.
8489             (progn
8490               (setq found 'found)
8491               (beginning-of-line)
8492               (set-window-start
8493                (get-buffer-window (current-buffer))
8494                (point))
8495               (forward-line 1)
8496               (set-window-point
8497                (get-buffer-window (current-buffer))
8498                (point))
8499               (set-buffer sum)
8500               (setq point (point)))
8501           ;; We didn't find it, so we go to the next article.
8502           (set-buffer sum)
8503           (setq found 'not)
8504           (while (eq found 'not)
8505             (if (not (if backward (gnus-summary-find-prev)
8506                        (gnus-summary-find-next)))
8507                 ;; No more articles.
8508                 (setq found t)
8509               ;; Select the next article and adjust point.
8510               (unless (gnus-summary-article-sparse-p
8511                        (gnus-summary-article-number))
8512                 (setq found nil)
8513                 (gnus-summary-select-article)
8514                 (set-buffer gnus-article-buffer)
8515                 (widen)
8516                 (goto-char (if backward (point-max) (point-min))))))))
8517       (gnus-message 7 ""))
8518     ;; Return whether we found the regexp.
8519     (when (eq found 'found)
8520       (goto-char point)
8521       (gnus-summary-show-thread)
8522       (gnus-summary-goto-subject gnus-current-article)
8523       (gnus-summary-position-point)
8524       t)))
8525
8526 (defun gnus-find-matching-articles (header regexp)
8527   "Return a list of all articles that match REGEXP on HEADER.
8528 This search includes all articles in the current group that Gnus has
8529 fetched headers for, whether they are displayed or not."
8530   (let ((articles nil)
8531         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8532         (case-fold-search t))
8533     (dolist (header gnus-newsgroup-headers)
8534       (when (string-match regexp (funcall func header))
8535         (push (mail-header-number header) articles)))
8536     (nreverse articles)))
8537
8538 (defun gnus-summary-find-matching (header regexp &optional backward unread
8539                                           not-case-fold not-matching)
8540   "Return a list of all articles that match REGEXP on HEADER.
8541 The search stars on the current article and goes forwards unless
8542 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8543 If UNREAD is non-nil, only unread articles will
8544 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8545 in the comparisons. If NOT-MATCHING, return a list of all articles that
8546 not match REGEXP on HEADER."
8547   (let ((case-fold-search (not not-case-fold))
8548         articles d func)
8549     (if (consp header)
8550         (if (eq (car header) 'extra)
8551             (setq func
8552                   `(lambda (h)
8553                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8554                          "")))
8555           (error "%s is an invalid header" header))
8556       (unless (fboundp (intern (concat "mail-header-" header)))
8557         (error "%s is not a valid header" header))
8558       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8559     (dolist (d (if (eq backward 'all)
8560                    gnus-newsgroup-data
8561                  (gnus-data-find-list
8562                   (gnus-summary-article-number)
8563                   (gnus-data-list backward))))
8564       (when (and (or (not unread)       ; We want all articles...
8565                      (gnus-data-unread-p d)) ; Or just unreads.
8566                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8567                  (if not-matching
8568                      (not (string-match
8569                            regexp
8570                            (funcall func (gnus-data-header d))))
8571                    (string-match regexp
8572                                  (funcall func (gnus-data-header d)))))
8573         (push (gnus-data-number d) articles))) ; Success!
8574     (nreverse articles)))
8575
8576 (defun gnus-summary-execute-command (header regexp command &optional backward)
8577   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8578 If HEADER is an empty string (or nil), the match is done on the entire
8579 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8580   (interactive
8581    (list (let ((completion-ignore-case t))
8582            (completing-read
8583             "Header name: "
8584             (mapcar (lambda (header) (list (format "%s" header)))
8585                     (append
8586                      '("Number" "Subject" "From" "Lines" "Date"
8587                        "Message-ID" "Xref" "References" "Body")
8588                      gnus-extra-headers))
8589             nil 'require-match))
8590          (read-string "Regexp: ")
8591          (read-key-sequence "Command: ")
8592          current-prefix-arg))
8593   (when (equal header "Body")
8594     (setq header ""))
8595   ;; Hidden thread subtrees must be searched as well.
8596   (gnus-summary-show-all-threads)
8597   ;; We don't want to change current point nor window configuration.
8598   (save-excursion
8599     (save-window-excursion
8600       (let (gnus-visual
8601             gnus-treat-strip-trailing-blank-lines
8602             gnus-treat-strip-leading-blank-lines
8603             gnus-treat-strip-multiple-blank-lines
8604             gnus-treat-hide-boring-headers
8605             gnus-treat-fold-newsgroups
8606             gnus-article-prepare-hook)
8607         (gnus-message 6 "Executing %s..." (key-description command))
8608         ;; We'd like to execute COMMAND interactively so as to give arguments.
8609         (gnus-execute header regexp
8610                       `(call-interactively ',(key-binding command))
8611                       backward)
8612         (gnus-message 6 "Executing %s...done" (key-description command))))))
8613
8614 (defun gnus-summary-beginning-of-article ()
8615   "Scroll the article back to the beginning."
8616   (interactive)
8617   (gnus-summary-select-article)
8618   (gnus-configure-windows 'article)
8619   (gnus-eval-in-buffer-window gnus-article-buffer
8620     (widen)
8621     (goto-char (point-min))
8622     (when gnus-break-pages
8623       (gnus-narrow-to-page))))
8624
8625 (defun gnus-summary-end-of-article ()
8626   "Scroll to the end of the article."
8627   (interactive)
8628   (gnus-summary-select-article)
8629   (gnus-configure-windows 'article)
8630   (gnus-eval-in-buffer-window gnus-article-buffer
8631     (widen)
8632     (goto-char (point-max))
8633     (recenter -3)
8634     (when gnus-break-pages
8635       (when (re-search-backward page-delimiter nil t)
8636         (narrow-to-region (match-end 0) (point-max)))
8637       (gnus-narrow-to-page))))
8638
8639 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8640   "Truncate to LEN and quote all \"(\"'s in STRING."
8641   (gnus-replace-in-string (if (and len (> (length string) len))
8642                               (substring string 0 len)
8643                             string)
8644                           "[()]" "\\\\\\&"))
8645
8646 (defun gnus-summary-print-article (&optional filename n)
8647   "Generate and print a PostScript image of the process-marked (mail) articles.
8648
8649 If used interactively, print the current article if none are
8650 process-marked.  With prefix arg, prompt the user for the name of the
8651 file to save in.
8652
8653 When used from Lisp, accept two optional args FILENAME and N.  N means
8654 to print the next N articles.  If N is negative, print the N previous
8655 articles.  If N is nil and articles have been marked with the process
8656 mark, print these instead.
8657
8658 If the optional first argument FILENAME is nil, send the image to the
8659 printer.  If FILENAME is a string, save the PostScript image in a file with
8660 that name.  If FILENAME is a number, prompt the user for the name of the file
8661 to save in."
8662   (interactive (list (ps-print-preprint current-prefix-arg)))
8663   (dolist (article (gnus-summary-work-articles n))
8664     (gnus-summary-select-article nil nil 'pseudo article)
8665     (gnus-eval-in-buffer-window gnus-article-buffer
8666       (gnus-print-buffer))
8667     (gnus-summary-remove-process-mark article))
8668   (ps-despool filename))
8669
8670 (defun gnus-print-buffer ()
8671   (let ((buffer (generate-new-buffer " *print*")))
8672     (unwind-protect
8673         (progn
8674           (copy-to-buffer buffer (point-min) (point-max))
8675           (set-buffer buffer)
8676           (gnus-remove-text-with-property 'gnus-decoration)
8677           (when (gnus-visual-p 'article-highlight 'highlight)
8678             ;; Copy-to-buffer doesn't copy overlay.  So redo
8679             ;; highlight.
8680             (let ((gnus-article-buffer buffer))
8681               (gnus-article-highlight-citation t)
8682               (gnus-article-highlight-signature)
8683               (gnus-article-emphasize)
8684               (gnus-article-delete-invisible-text)))
8685           (let ((ps-left-header
8686                  (list
8687                   (concat "("
8688                           (gnus-summary-print-truncate-and-quote
8689                            (mail-header-subject gnus-current-headers)
8690                            66) ")")
8691                   (concat "("
8692                           (gnus-summary-print-truncate-and-quote
8693                            (mail-header-from gnus-current-headers)
8694                            45) ")")))
8695                 (ps-right-header
8696                  (list
8697                   "/pagenumberstring load"
8698                   (concat "("
8699                           (mail-header-date gnus-current-headers) ")"))))
8700             (gnus-run-hooks 'gnus-ps-print-hook)
8701             (save-excursion
8702               (if window-system
8703                   (ps-spool-buffer-with-faces)
8704                 (ps-spool-buffer)))))
8705       (kill-buffer buffer))))
8706
8707 (defun gnus-summary-show-article (&optional arg)
8708   "Force redisplaying of the current article.
8709 If ARG (the prefix) is a number, show the article with the charset
8710 defined in `gnus-summary-show-article-charset-alist', or the charset
8711 input.
8712 If ARG (the prefix) is non-nil and not a number, show the raw article
8713 without any article massaging functions being run.  Normally, the key
8714 strokes are `C-u g'."
8715   (interactive "P")
8716   (cond
8717    ((numberp arg)
8718     (gnus-summary-show-article t)
8719     (let ((gnus-newsgroup-charset
8720            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8721                (mm-read-coding-system
8722                 "View as charset: " ;; actually it is coding system.
8723                 (save-excursion
8724                   (set-buffer gnus-article-buffer)
8725                   (mm-detect-coding-region (point) (point-max))))))
8726           (gnus-newsgroup-ignored-charsets 'gnus-all))
8727       (gnus-summary-select-article nil 'force)
8728       (let ((deps gnus-newsgroup-dependencies)
8729             head header lines)
8730         (save-excursion
8731           (set-buffer gnus-original-article-buffer)
8732           (save-restriction
8733             (message-narrow-to-head)
8734             (setq head (buffer-string))
8735             (goto-char (point-min))
8736             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8737               (goto-char (point-max))
8738               (widen)
8739               (setq lines (1- (count-lines (point) (point-max))))))
8740           (with-temp-buffer
8741             (insert (format "211 %d Article retrieved.\n"
8742                             (cdr gnus-article-current)))
8743             (insert head)
8744             (if lines (insert (format "Lines: %d\n" lines)))
8745             (insert ".\n")
8746             (let ((nntp-server-buffer (current-buffer)))
8747               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8748         (gnus-data-set-header
8749          (gnus-data-find (cdr gnus-article-current))
8750          header)
8751         (gnus-summary-update-article-line
8752          (cdr gnus-article-current) header)
8753         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8754           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8755    ((not arg)
8756     ;; Select the article the normal way.
8757     (gnus-summary-select-article nil 'force))
8758    (t
8759     ;; We have to require this here to make sure that the following
8760     ;; dynamic binding isn't shadowed by autoloading.
8761     (require 'gnus-async)
8762     (require 'gnus-art)
8763     ;; Bind the article treatment functions to nil.
8764     (let ((gnus-have-all-headers t)
8765           gnus-article-prepare-hook
8766           gnus-article-decode-hook
8767           gnus-display-mime-function
8768           gnus-break-pages)
8769       ;; Destroy any MIME parts.
8770       (when (gnus-buffer-live-p gnus-article-buffer)
8771         (save-excursion
8772           (set-buffer gnus-article-buffer)
8773           (mm-destroy-parts gnus-article-mime-handles)
8774           ;; Set it to nil for safety reason.
8775           (setq gnus-article-mime-handle-alist nil)
8776           (setq gnus-article-mime-handles nil)))
8777       (gnus-summary-select-article nil 'force))))
8778   (gnus-summary-goto-subject gnus-current-article)
8779   (gnus-summary-position-point))
8780
8781 (defun gnus-summary-show-raw-article ()
8782   "Show the raw article without any article massaging functions being run."
8783   (interactive)
8784   (gnus-summary-show-article t))
8785
8786 (defun gnus-summary-verbose-headers (&optional arg)
8787   "Toggle permanent full header display.
8788 If ARG is a positive number, turn header display on.
8789 If ARG is a negative number, turn header display off."
8790   (interactive "P")
8791   (setq gnus-show-all-headers
8792         (cond ((or (not (numberp arg))
8793                    (zerop arg))
8794                (not gnus-show-all-headers))
8795               ((natnump arg)
8796                t)))
8797   (gnus-summary-show-article))
8798
8799 (defun gnus-summary-toggle-header (&optional arg)
8800   "Show the headers if they are hidden, or hide them if they are shown.
8801 If ARG is a positive number, show the entire header.
8802 If ARG is a negative number, hide the unwanted header lines."
8803   (interactive "P")
8804   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8805                      (get-buffer-window gnus-article-buffer t))))
8806     (with-current-buffer gnus-article-buffer
8807       (widen)
8808       (article-narrow-to-head)
8809       (let* ((buffer-read-only nil)
8810              (inhibit-point-motion-hooks t)
8811              (hidden (if (numberp arg)
8812                          (>= arg 0)
8813                        (gnus-article-hidden-text-p 'headers)))
8814              s e)
8815         (delete-region (point-min) (point-max))
8816         (with-current-buffer gnus-original-article-buffer
8817           (goto-char (setq s (point-min)))
8818           (setq e (if (search-forward "\n\n" nil t)
8819                       (1- (point))
8820                     (point-max))))
8821         (insert-buffer-substring gnus-original-article-buffer s e)
8822         (run-hooks 'gnus-article-decode-hook)
8823         (if hidden
8824             (let ((gnus-treat-hide-headers nil)
8825                   (gnus-treat-hide-boring-headers nil))
8826               (gnus-delete-wash-type 'headers)
8827               (gnus-treat-article 'head))
8828           (gnus-treat-article 'head))
8829         (widen)
8830         (if window
8831             (set-window-start window (goto-char (point-min))))
8832         (if gnus-break-pages
8833             (gnus-narrow-to-page)
8834           (when (gnus-visual-p 'page-marker)
8835             (let ((buffer-read-only nil))
8836               (gnus-remove-text-with-property 'gnus-prev)
8837               (gnus-remove-text-with-property 'gnus-next))))
8838         (gnus-set-mode-line 'article)))))
8839
8840 (defun gnus-summary-show-all-headers ()
8841   "Make all header lines visible."
8842   (interactive)
8843   (gnus-summary-toggle-header 1))
8844
8845 (defun gnus-summary-caesar-message (&optional arg)
8846   "Caesar rotate the current article by 13.
8847 The numerical prefix specifies how many places to rotate each letter
8848 forward."
8849   (interactive "P")
8850   (gnus-summary-select-article)
8851   (let ((mail-header-separator ""))
8852     (gnus-eval-in-buffer-window gnus-article-buffer
8853       (save-restriction
8854         (widen)
8855         (let ((start (window-start))
8856               buffer-read-only)
8857           (message-caesar-buffer-body arg)
8858           (set-window-start (get-buffer-window (current-buffer)) start))))))
8859
8860 (autoload 'unmorse-region "morse"
8861   "Convert morse coded text in region to ordinary ASCII text."
8862   t)
8863
8864 (defun gnus-summary-morse-message (&optional arg)
8865   "Morse decode the current article."
8866   (interactive "P")
8867   (gnus-summary-select-article)
8868   (let ((mail-header-separator ""))
8869     (gnus-eval-in-buffer-window gnus-article-buffer
8870       (save-excursion
8871         (save-restriction
8872           (widen)
8873           (let ((pos (window-start))
8874                 buffer-read-only)
8875             (goto-char (point-min))
8876             (when (message-goto-body)
8877               (gnus-narrow-to-body))
8878             (goto-char (point-min))
8879             (while (re-search-forward "·" (point-max) t)
8880               (replace-match "."))
8881             (unmorse-region (point-min) (point-max))
8882             (widen)
8883             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8884
8885 (defun gnus-summary-stop-page-breaking ()
8886   "Stop page breaking in the current article."
8887   (interactive)
8888   (gnus-summary-select-article)
8889   (gnus-eval-in-buffer-window gnus-article-buffer
8890     (widen)
8891     (when (gnus-visual-p 'page-marker)
8892       (let ((buffer-read-only nil))
8893         (gnus-remove-text-with-property 'gnus-prev)
8894         (gnus-remove-text-with-property 'gnus-next))
8895       (setq gnus-page-broken nil))))
8896
8897 (defun gnus-summary-move-article (&optional n to-newsgroup
8898                                             select-method action)
8899   "Move the current article to a different newsgroup.
8900 If N is a positive number, move the N next articles.
8901 If N is a negative number, move the N previous articles.
8902 If N is nil and any articles have been marked with the process mark,
8903 move those articles instead.
8904 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8905 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8906 re-spool using this method.
8907
8908 When called interactively with TO-NEWSGROUP being nil, the value of
8909 the variable `gnus-move-split-methods' is used for finding a default
8910 for the target newsgroup.
8911
8912 For this function to work, both the current newsgroup and the
8913 newsgroup that you want to move to have to support the `request-move'
8914 and `request-accept' functions.
8915
8916 ACTION can be either `move' (the default), `crosspost' or `copy'."
8917   (interactive "P")
8918   (unless action
8919     (setq action 'move))
8920   ;; Check whether the source group supports the required functions.
8921   (cond ((and (eq action 'move)
8922               (not (gnus-check-backend-function
8923                     'request-move-article gnus-newsgroup-name)))
8924          (error "The current group does not support article moving"))
8925         ((and (eq action 'crosspost)
8926               (not (gnus-check-backend-function
8927                     'request-replace-article gnus-newsgroup-name)))
8928          (error "The current group does not support article editing")))
8929   (let ((articles (gnus-summary-work-articles n))
8930         (prefix (if (gnus-check-backend-function
8931                      'request-move-article gnus-newsgroup-name)
8932                     (funcall gnus-move-group-prefix-function
8933                              gnus-newsgroup-name)
8934                   ""))
8935         (names '((move "Move" "Moving")
8936                  (copy "Copy" "Copying")
8937                  (crosspost "Crosspost" "Crossposting")))
8938         (copy-buf (save-excursion
8939                     (nnheader-set-temp-buffer " *copy article*")))
8940         art-group to-method new-xref article to-groups)
8941     (unless (assq action names)
8942       (error "Unknown action %s" action))
8943     ;; Read the newsgroup name.
8944     (when (and (not to-newsgroup)
8945                (not select-method))
8946       (if (and gnus-move-split-methods
8947                (not
8948                 (and (memq gnus-current-article articles)
8949                      (gnus-buffer-live-p gnus-original-article-buffer))))
8950           ;; When `gnus-move-split-methods' is non-nil, we have to
8951           ;; select an article to give `gnus-read-move-group-name' an
8952           ;; opportunity to suggest an appropriate default.  However,
8953           ;; we needn't render or mark the article.
8954           (let ((gnus-display-mime-function nil)
8955                 (gnus-article-prepare-hook nil)
8956                 (gnus-mark-article-hook nil))
8957             (gnus-summary-select-article nil nil nil (car articles))))
8958       (setq to-newsgroup
8959             (gnus-read-move-group-name
8960              (cadr (assq action names))
8961              (symbol-value (intern (format "gnus-current-%s-group" action)))
8962              articles prefix))
8963       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8964     (setq to-method (or select-method
8965                         (gnus-server-to-method
8966                          (gnus-group-method to-newsgroup))))
8967     ;; Check the method we are to move this article to...
8968     (unless (gnus-check-backend-function
8969              'request-accept-article (car to-method))
8970       (error "%s does not support article copying" (car to-method)))
8971     (unless (gnus-check-server to-method)
8972       (error "Can't open server %s" (car to-method)))
8973     (gnus-message 6 "%s to %s: %s..."
8974                   (caddr (assq action names))
8975                   (or (car select-method) to-newsgroup) articles)
8976     (while articles
8977       (setq article (pop articles))
8978       (setq
8979        art-group
8980        (cond
8981         ;; Move the article.
8982         ((eq action 'move)
8983          ;; Remove this article from future suppression.
8984          (gnus-dup-unsuppress-article article)
8985          (gnus-request-move-article
8986           article                       ; Article to move
8987           gnus-newsgroup-name           ; From newsgroup
8988           (nth 1 (gnus-find-method-for-group
8989                   gnus-newsgroup-name)) ; Server
8990           (list 'gnus-request-accept-article
8991                 to-newsgroup (list 'quote select-method)
8992                 (not articles) t)       ; Accept form
8993           (not articles)))              ; Only save nov last time
8994         ;; Copy the article.
8995         ((eq action 'copy)
8996          (save-excursion
8997            (set-buffer copy-buf)
8998            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8999              (gnus-request-accept-article
9000               to-newsgroup select-method (not articles) t))))
9001         ;; Crosspost the article.
9002         ((eq action 'crosspost)
9003          (let ((xref (message-tokenize-header
9004                       (mail-header-xref (gnus-summary-article-header article))
9005                       " ")))
9006            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9007                                   ":" (number-to-string article)))
9008            (unless xref
9009              (setq xref (list (system-name))))
9010            (setq new-xref
9011                  (concat
9012                   (mapconcat 'identity
9013                              (delete "Xref:" (delete new-xref xref))
9014                              " ")
9015                   " " new-xref))
9016            (save-excursion
9017              (set-buffer copy-buf)
9018              ;; First put the article in the destination group.
9019              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9020              (when (consp (setq art-group
9021                                 (gnus-request-accept-article
9022                                  to-newsgroup select-method (not articles))))
9023                (setq new-xref (concat new-xref " " (car art-group)
9024                                       ":"
9025                                       (number-to-string (cdr art-group))))
9026                ;; Now we have the new Xrefs header, so we insert
9027                ;; it and replace the new article.
9028                (nnheader-replace-header "Xref" new-xref)
9029                (gnus-request-replace-article
9030                 (cdr art-group) to-newsgroup (current-buffer))
9031                art-group))))))
9032       (cond
9033        ((not art-group)
9034         (gnus-message 1 "Couldn't %s article %s: %s"
9035                       (cadr (assq action names)) article
9036                       (nnheader-get-report (car to-method))))
9037        ((eq art-group 'junk)
9038         (when (eq action 'move)
9039           (gnus-summary-mark-article article gnus-canceled-mark)
9040           (gnus-message 4 "Deleted article %s" article)
9041           ;; run the delete hook
9042           (run-hook-with-args 'gnus-summary-article-delete-hook
9043                               action
9044                               (gnus-data-header
9045                                (assoc article (gnus-data-list nil)))
9046                               gnus-newsgroup-name nil
9047                               select-method)))
9048        (t
9049         (let* ((pto-group (gnus-group-prefixed-name
9050                            (car art-group) to-method))
9051                (info (gnus-get-info pto-group))
9052                (to-group (gnus-info-group info))
9053                to-marks)
9054           ;; Update the group that has been moved to.
9055           (when (and info
9056                      (memq action '(move copy)))
9057             (unless (member to-group to-groups)
9058               (push to-group to-groups))
9059
9060             (unless (memq article gnus-newsgroup-unreads)
9061               (push 'read to-marks)
9062               (gnus-info-set-read
9063                info (gnus-add-to-range (gnus-info-read info)
9064                                        (list (cdr art-group)))))
9065
9066             ;; See whether the article is to be put in the cache.
9067             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9068                              gnus-article-mark-lists
9069                            (delete '(expirable . expire)
9070                                    (copy-sequence gnus-article-mark-lists))))
9071                   (to-article (cdr art-group)))
9072
9073               ;; Enter the article into the cache in the new group,
9074               ;; if that is required.
9075               (when gnus-use-cache
9076                 (gnus-cache-possibly-enter-article
9077                  to-group to-article
9078                  (memq article gnus-newsgroup-marked)
9079                  (memq article gnus-newsgroup-dormant)
9080                  (memq article gnus-newsgroup-unreads)))
9081
9082               (when gnus-preserve-marks
9083                 ;; Copy any marks over to the new group.
9084                 (when (and (equal to-group gnus-newsgroup-name)
9085                            (not (memq article gnus-newsgroup-unreads)))
9086                   ;; Mark this article as read in this group.
9087                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9088                   (setcdr (gnus-active to-group) to-article)
9089                   (setcdr gnus-newsgroup-active to-article))
9090
9091                 (while marks
9092                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9093                     (when (memq article (symbol-value
9094                                          (intern (format "gnus-newsgroup-%s"
9095                                                          (caar marks)))))
9096                       (push (cdar marks) to-marks)
9097                       ;; If the other group is the same as this group,
9098                       ;; then we have to add the mark to the list.
9099                       (when (equal to-group gnus-newsgroup-name)
9100                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9101                              (cons to-article
9102                                    (symbol-value
9103                                     (intern (format "gnus-newsgroup-%s"
9104                                                     (caar marks)))))))
9105                       ;; Copy the marks to other group.
9106                       (gnus-add-marked-articles
9107                        to-group (cdar marks) (list to-article) info)))
9108                   (setq marks (cdr marks)))
9109
9110                 (gnus-request-set-mark
9111                  to-group (list (list (list to-article) 'add to-marks))))
9112
9113               (gnus-dribble-enter
9114                (concat "(gnus-group-set-info '"
9115                        (gnus-prin1-to-string (gnus-get-info to-group))
9116                        ")"))))
9117
9118           ;; Update the Xref header in this article to point to
9119           ;; the new crossposted article we have just created.
9120           (when (eq action 'crosspost)
9121             (save-excursion
9122               (set-buffer copy-buf)
9123               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9124               (nnheader-replace-header "Xref" new-xref)
9125               (gnus-request-replace-article
9126                article gnus-newsgroup-name (current-buffer))))
9127
9128           ;; run the move/copy/crosspost/respool hook
9129           (run-hook-with-args 'gnus-summary-article-move-hook
9130                               action
9131                               (gnus-data-header
9132                                (assoc article (gnus-data-list nil)))
9133                               gnus-newsgroup-name
9134                               to-newsgroup
9135                               select-method))
9136
9137         ;;;!!!Why is this necessary?
9138         (set-buffer gnus-summary-buffer)
9139         
9140         (gnus-summary-goto-subject article)
9141         (when (eq action 'move)
9142           (gnus-summary-mark-article article gnus-canceled-mark))))
9143       (gnus-summary-remove-process-mark article))
9144     ;; Re-activate all groups that have been moved to.
9145     (save-excursion
9146       (set-buffer gnus-group-buffer)
9147       (let ((gnus-group-marked to-groups))
9148         (gnus-group-get-new-news-this-group nil t)))
9149
9150     (gnus-kill-buffer copy-buf)
9151     (gnus-summary-position-point)
9152     (gnus-set-mode-line 'summary)))
9153
9154 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9155   "Move the current article to a different newsgroup.
9156 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9157 When called interactively, if TO-NEWSGROUP is nil, use the value of
9158 the variable `gnus-move-split-methods' for finding a default target
9159 newsgroup.
9160 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9161 re-spool using this method."
9162   (interactive "P")
9163   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9164
9165 (defun gnus-summary-crosspost-article (&optional n)
9166   "Crosspost the current article to some other group."
9167   (interactive "P")
9168   (gnus-summary-move-article n nil nil 'crosspost))
9169
9170 (defcustom gnus-summary-respool-default-method nil
9171   "Default method type for respooling an article.
9172 If nil, use to the current newsgroup method."
9173   :type 'symbol
9174   :group 'gnus-summary-mail)
9175
9176 (defcustom gnus-summary-display-while-building nil
9177   "If non-nil, show and update the summary buffer as it's being built.
9178 If the value is t, update the buffer after every line is inserted.  If
9179 the value is an integer (N), update the display every N lines."
9180   :group 'gnus-thread
9181   :type '(choice (const :tag "off" nil)
9182                  number
9183                  (const :tag "frequently" t)))
9184
9185 (defun gnus-summary-respool-article (&optional n method)
9186   "Respool the current article.
9187 The article will be squeezed through the mail spooling process again,
9188 which means that it will be put in some mail newsgroup or other
9189 depending on `nnmail-split-methods'.
9190 If N is a positive number, respool the N next articles.
9191 If N is a negative number, respool the N previous articles.
9192 If N is nil and any articles have been marked with the process mark,
9193 respool those articles instead.
9194
9195 Respooling can be done both from mail groups and \"real\" newsgroups.
9196 In the former case, the articles in question will be moved from the
9197 current group into whatever groups they are destined to.  In the
9198 latter case, they will be copied into the relevant groups."
9199   (interactive
9200    (list current-prefix-arg
9201          (let* ((methods (gnus-methods-using 'respool))
9202                 (methname
9203                  (symbol-name (or gnus-summary-respool-default-method
9204                                   (car (gnus-find-method-for-group
9205                                         gnus-newsgroup-name)))))
9206                 (method
9207                  (gnus-completing-read-with-default
9208                   methname "What backend do you want to use when respooling?"
9209                   methods nil t nil 'gnus-mail-method-history))
9210                 ms)
9211            (cond
9212             ((zerop (length (setq ms (gnus-servers-using-backend
9213                                       (intern method)))))
9214              (list (intern method) ""))
9215             ((= 1 (length ms))
9216              (car ms))
9217             (t
9218              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9219                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9220                            ms-alist))))))))
9221   (unless method
9222     (error "No method given for respooling"))
9223   (if (assoc (symbol-name
9224               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9225              (gnus-methods-using 'respool))
9226       (gnus-summary-move-article n nil method)
9227     (gnus-summary-copy-article n nil method)))
9228
9229 (defun gnus-summary-import-article (file &optional edit)
9230   "Import an arbitrary file into a mail newsgroup."
9231   (interactive "fImport file: \nP")
9232   (let ((group gnus-newsgroup-name)
9233         (now (current-time))
9234         atts lines group-art)
9235     (unless (gnus-check-backend-function 'request-accept-article group)
9236       (error "%s does not support article importing" group))
9237     (or (file-readable-p file)
9238         (not (file-regular-p file))
9239         (error "Can't read %s" file))
9240     (save-excursion
9241       (set-buffer (gnus-get-buffer-create " *import file*"))
9242       (erase-buffer)
9243       (nnheader-insert-file-contents file)
9244       (goto-char (point-min))
9245       (if (nnheader-article-p)
9246           (save-restriction
9247             (goto-char (point-min))
9248             (search-forward "\n\n" nil t)
9249             (narrow-to-region (point-min) (1- (point)))
9250             (goto-char (point-min))
9251             (unless (re-search-forward "^date:" nil t)
9252               (goto-char (point-max))
9253               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9254        ;; This doesn't look like an article, so we fudge some headers.
9255         (setq atts (file-attributes file)
9256               lines (count-lines (point-min) (point-max)))
9257         (insert "From: " (read-string "From: ") "\n"
9258                 "Subject: " (read-string "Subject: ") "\n"
9259                 "Date: " (message-make-date (nth 5 atts)) "\n"
9260                 "Message-ID: " (message-make-message-id) "\n"
9261                 "Lines: " (int-to-string lines) "\n"
9262                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9263       (setq group-art (gnus-request-accept-article group nil t))
9264       (kill-buffer (current-buffer)))
9265     (setq gnus-newsgroup-active (gnus-activate-group group))
9266     (forward-line 1)
9267     (gnus-summary-goto-article (cdr group-art) nil t)
9268     (when edit
9269       (gnus-summary-edit-article))))
9270
9271 (defun gnus-summary-create-article ()
9272   "Create an article in a mail newsgroup."
9273   (interactive)
9274   (let ((group gnus-newsgroup-name)
9275         (now (current-time))
9276         group-art)
9277     (unless (gnus-check-backend-function 'request-accept-article group)
9278       (error "%s does not support article importing" group))
9279     (save-excursion
9280       (set-buffer (gnus-get-buffer-create " *import file*"))
9281       (erase-buffer)
9282       (goto-char (point-min))
9283       ;; This doesn't look like an article, so we fudge some headers.
9284       (insert "From: " (read-string "From: ") "\n"
9285               "Subject: " (read-string "Subject: ") "\n"
9286               "Date: " (message-make-date now) "\n"
9287               "Message-ID: " (message-make-message-id) "\n")
9288       (setq group-art (gnus-request-accept-article group nil t))
9289       (kill-buffer (current-buffer)))
9290     (setq gnus-newsgroup-active (gnus-activate-group group))
9291     (forward-line 1)
9292     (gnus-summary-goto-article (cdr group-art) nil t)
9293     (gnus-summary-edit-article)))
9294
9295 (defun gnus-summary-article-posted-p ()
9296   "Say whether the current (mail) article is available from news as well.
9297 This will be the case if the article has both been mailed and posted."
9298   (interactive)
9299   (let ((id (mail-header-references (gnus-summary-article-header)))
9300         (gnus-override-method (car (gnus-refer-article-methods))))
9301     (if (gnus-request-head id "")
9302         (gnus-message 2 "The current message was found on %s"
9303                       gnus-override-method)
9304       (gnus-message 2 "The current message couldn't be found on %s"
9305                     gnus-override-method)
9306       nil)))
9307
9308 (defun gnus-summary-expire-articles (&optional now)
9309   "Expire all articles that are marked as expirable in the current group."
9310   (interactive)
9311   (when (and (not gnus-group-is-exiting-without-update-p)
9312              (gnus-check-backend-function
9313               'request-expire-articles gnus-newsgroup-name))
9314     ;; This backend supports expiry.
9315     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9316            (expirable (if total
9317                           (progn
9318                             ;; We need to update the info for
9319                             ;; this group for `gnus-list-of-read-articles'
9320                             ;; to give us the right answer.
9321                             (gnus-run-hooks 'gnus-exit-group-hook)
9322                             (gnus-summary-update-info)
9323                             (gnus-list-of-read-articles gnus-newsgroup-name))
9324                         (setq gnus-newsgroup-expirable
9325                               (sort gnus-newsgroup-expirable '<))))
9326            (expiry-wait (if now 'immediate
9327                           (gnus-group-find-parameter
9328                            gnus-newsgroup-name 'expiry-wait)))
9329            (nnmail-expiry-target
9330             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9331                 nnmail-expiry-target))
9332            es)
9333       (when expirable
9334         ;; There are expirable articles in this group, so we run them
9335         ;; through the expiry process.
9336         (gnus-message 6 "Expiring articles...")
9337         (unless (gnus-check-group gnus-newsgroup-name)
9338           (error "Can't open server for %s" gnus-newsgroup-name))
9339         ;; The list of articles that weren't expired is returned.
9340         (save-excursion
9341           (if expiry-wait
9342               (let ((nnmail-expiry-wait-function nil)
9343                     (nnmail-expiry-wait expiry-wait))
9344                 (setq es (gnus-request-expire-articles
9345                           expirable gnus-newsgroup-name)))
9346             (setq es (gnus-request-expire-articles
9347                       expirable gnus-newsgroup-name)))
9348           (unless total
9349             (setq gnus-newsgroup-expirable es))
9350           ;; We go through the old list of expirable, and mark all
9351           ;; really expired articles as nonexistent.
9352           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9353             (let ((gnus-use-cache nil))
9354               (dolist (article expirable)
9355                 (when (and (not (memq article es))
9356                            (gnus-data-find article))
9357                   (gnus-summary-mark-article article gnus-canceled-mark)
9358                   (run-hook-with-args 'gnus-summary-article-expire-hook
9359                                       'delete
9360                                       (gnus-data-header
9361                                        (assoc article (gnus-data-list nil)))
9362                                       gnus-newsgroup-name
9363                                       nil
9364                                       nil))))))
9365         (gnus-message 6 "Expiring articles...done")))))
9366
9367 (defun gnus-summary-expire-articles-now ()
9368   "Expunge all expirable articles in the current group.
9369 This means that *all* articles that are marked as expirable will be
9370 deleted forever, right now."
9371   (interactive)
9372   (or gnus-expert-user
9373       (gnus-yes-or-no-p
9374        "Are you really, really, really sure you want to delete all these messages? ")
9375       (error "Phew!"))
9376   (gnus-summary-expire-articles t))
9377
9378 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9379 (defun gnus-summary-delete-article (&optional n)
9380   "Delete the N next (mail) articles.
9381 This command actually deletes articles.  This is not a marking
9382 command.  The article will disappear forever from your life, never to
9383 return.
9384
9385 If N is negative, delete backwards.
9386 If N is nil and articles have been marked with the process mark,
9387 delete these instead.
9388
9389 If `gnus-novice-user' is non-nil you will be asked for
9390 confirmation before the articles are deleted."
9391   (interactive "P")
9392   (unless (gnus-check-backend-function 'request-expire-articles
9393                                        gnus-newsgroup-name)
9394     (error "The current newsgroup does not support article deletion"))
9395   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9396     (error "Couldn't open server"))
9397   ;; Compute the list of articles to delete.
9398   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9399         (nnmail-expiry-target 'delete)
9400         not-deleted)
9401     (if (and gnus-novice-user
9402              (not (gnus-yes-or-no-p
9403                    (format "Do you really want to delete %s forever? "
9404                            (if (> (length articles) 1)
9405                                (format "these %s articles" (length articles))
9406                              "this article")))))
9407         ()
9408       ;; Delete the articles.
9409       (setq not-deleted (gnus-request-expire-articles
9410                          articles gnus-newsgroup-name 'force))
9411       (while articles
9412         (gnus-summary-remove-process-mark (car articles))
9413         ;; The backend might not have been able to delete the article
9414         ;; after all.
9415         (unless (memq (car articles) not-deleted)
9416           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9417         (let* ((article (car articles))
9418                (id (mail-header-id (gnus-data-header
9419                                     (assoc article (gnus-data-list nil))))))
9420           (run-hook-with-args 'gnus-summary-article-delete-hook
9421                               'delete id gnus-newsgroup-name nil
9422                               nil))
9423         (setq articles (cdr articles)))
9424       (when not-deleted
9425         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9426     (gnus-summary-position-point)
9427     (gnus-set-mode-line 'summary)
9428     not-deleted))
9429
9430 (defun gnus-summary-edit-article (&optional arg)
9431   "Edit the current article.
9432 This will have permanent effect only in mail groups.
9433 If ARG is nil, edit the decoded articles.
9434 If ARG is 1, edit the raw articles.
9435 If ARG is 2, edit the raw articles even in read-only groups.
9436 If ARG is 3, edit the articles with the current handles.
9437 Otherwise, allow editing of articles even in read-only
9438 groups."
9439   (interactive "P")
9440   (let (force raw current-handles)
9441     (cond
9442      ((null arg))
9443      ((eq arg 1)
9444       (setq raw t))
9445      ((eq arg 2)
9446       (setq raw t
9447             force t))
9448      ((eq arg 3)
9449       (setq current-handles
9450             (and (gnus-buffer-live-p gnus-article-buffer)
9451                  (with-current-buffer gnus-article-buffer
9452                    (prog1
9453                        gnus-article-mime-handles
9454                      (setq gnus-article-mime-handles nil))))))
9455      (t
9456       (setq force t)))
9457     (when (and raw (not force)
9458                (member gnus-newsgroup-name '("nndraft:delayed"
9459                                              "nndraft:drafts"
9460                                              "nndraft:queue")))
9461       (error "Can't edit the raw article in group %s"
9462              gnus-newsgroup-name))
9463     (save-excursion
9464       (set-buffer gnus-summary-buffer)
9465       (let ((mail-parse-charset gnus-newsgroup-charset)
9466             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9467         (gnus-set-global-variables)
9468         (when (and (not force)
9469                    (gnus-group-read-only-p))
9470           (error "The current newsgroup does not support article editing"))
9471         (gnus-summary-show-article t)
9472         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9473           (with-current-buffer gnus-article-buffer
9474             (mm-enable-multibyte)))
9475         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9476             (setq raw t))
9477         (gnus-article-edit-article
9478          (if raw 'ignore
9479            `(lambda ()
9480               (let ((mbl mml-buffer-list))
9481                 (setq mml-buffer-list nil)
9482                 (mime-to-mml ,'current-handles)
9483                 (let ((mbl1 mml-buffer-list))
9484                   (setq mml-buffer-list mbl)
9485                   (set (make-local-variable 'mml-buffer-list) mbl1))
9486                 (gnus-make-local-hook 'kill-buffer-hook)
9487                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9488          `(lambda (no-highlight)
9489             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9490                   (message-options message-options)
9491                   (message-options-set-recipient)
9492                   (mail-parse-ignored-charsets
9493                    ',gnus-newsgroup-ignored-charsets))
9494               ,(if (not raw) '(progn
9495                                 (mml-to-mime)
9496                                 (mml-destroy-buffers)
9497                                 (remove-hook 'kill-buffer-hook
9498                                              'mml-destroy-buffers t)
9499                                 (kill-local-variable 'mml-buffer-list)))
9500               (gnus-summary-edit-article-done
9501                ,(or (mail-header-references gnus-current-headers) "")
9502                ,(gnus-group-read-only-p)
9503                ,gnus-summary-buffer no-highlight))))))))
9504
9505 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9506
9507 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9508                                                  no-highlight)
9509   "Make edits to the current article permanent."
9510   (interactive)
9511   (save-excursion
9512    ;; The buffer restriction contains the entire article if it exists.
9513     (when (article-goto-body)
9514       (let ((lines (count-lines (point) (point-max)))
9515             (length (- (point-max) (point)))
9516             (case-fold-search t)
9517             (body (copy-marker (point))))
9518         (goto-char (point-min))
9519         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9520           (delete-region (match-beginning 1) (match-end 1))
9521           (insert (number-to-string length)))
9522         (goto-char (point-min))
9523         (when (re-search-forward
9524                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9525           (delete-region (match-beginning 1) (match-end 1))
9526           (insert (number-to-string length)))
9527         (goto-char (point-min))
9528         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9529           (delete-region (match-beginning 1) (match-end 1))
9530           (insert (number-to-string lines))))))
9531   ;; Replace the article.
9532   (let ((buf (current-buffer)))
9533     (with-temp-buffer
9534       (insert-buffer-substring buf)
9535
9536       (if (and (not read-only)
9537                (not (gnus-request-replace-article
9538                      (cdr gnus-article-current) (car gnus-article-current)
9539                      (current-buffer) t)))
9540           (error "Couldn't replace article")
9541         ;; Update the summary buffer.
9542         (if (and references
9543                  (equal (message-tokenize-header references " ")
9544                         (message-tokenize-header
9545                          (or (message-fetch-field "references") "") " ")))
9546             ;; We only have to update this line.
9547             (save-excursion
9548               (save-restriction
9549                 (message-narrow-to-head)
9550                 (let ((head (buffer-string))
9551                       header)
9552                   (with-temp-buffer
9553                     (insert (format "211 %d Article retrieved.\n"
9554                                     (cdr gnus-article-current)))
9555                     (insert head)
9556                     (insert ".\n")
9557                     (let ((nntp-server-buffer (current-buffer)))
9558                       (setq header (car (gnus-get-newsgroup-headers
9559                                          nil t))))
9560                     (save-excursion
9561                       (set-buffer gnus-summary-buffer)
9562                       (gnus-data-set-header
9563                        (gnus-data-find (cdr gnus-article-current))
9564                        header)
9565                       (gnus-summary-update-article-line
9566                        (cdr gnus-article-current) header)
9567                       (if (gnus-summary-goto-subject
9568                            (cdr gnus-article-current) nil t)
9569                           (gnus-summary-update-secondary-mark
9570                            (cdr gnus-article-current))))))))
9571           ;; Update threads.
9572           (set-buffer (or buffer gnus-summary-buffer))
9573           (gnus-summary-update-article (cdr gnus-article-current))
9574           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9575               (gnus-summary-update-secondary-mark
9576                (cdr gnus-article-current))))
9577         ;; Prettify the article buffer again.
9578         (unless no-highlight
9579           (save-excursion
9580             (set-buffer gnus-article-buffer)
9581             ;;;!!! Fix this -- article should be rehighlighted.
9582             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9583             (set-buffer gnus-original-article-buffer)
9584             (gnus-request-article
9585              (cdr gnus-article-current)
9586              (car gnus-article-current) (current-buffer))))
9587         ;; Prettify the summary buffer line.
9588         (when (gnus-visual-p 'summary-highlight 'highlight)
9589           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9590
9591 (defun gnus-summary-edit-wash (key)
9592   "Perform editing command KEY in the article buffer."
9593   (interactive
9594    (list
9595     (progn
9596       (message "%s" (concat (this-command-keys) "- "))
9597       (read-char))))
9598   (message "")
9599   (gnus-summary-edit-article)
9600   (execute-kbd-macro (concat (this-command-keys) key))
9601   (gnus-article-edit-done))
9602
9603 ;;; Respooling
9604
9605 (defun gnus-summary-respool-query (&optional silent trace)
9606   "Query where the respool algorithm would put this article."
9607   (interactive)
9608   (let (gnus-mark-article-hook)
9609     (gnus-summary-select-article)
9610     (save-excursion
9611       (set-buffer gnus-original-article-buffer)
9612       (let ((groups (nnmail-article-group 'identity trace)))
9613         (unless silent
9614           (if groups
9615               (message "This message would go to %s"
9616                        (mapconcat 'car groups ", "))
9617             (message "This message would go to no groups"))
9618           groups)))))
9619
9620 (defun gnus-summary-respool-trace ()
9621   "Trace where the respool algorithm would put this article.
9622 Display a buffer showing all fancy splitting patterns which matched."
9623   (interactive)
9624   (gnus-summary-respool-query nil t))
9625
9626 ;; Summary marking commands.
9627
9628 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9629   "Mark articles which has the same subject as read, and then select the next.
9630 If UNMARK is positive, remove any kind of mark.
9631 If UNMARK is negative, tick articles."
9632   (interactive "P")
9633   (when unmark
9634     (setq unmark (prefix-numeric-value unmark)))
9635   (let ((count
9636          (gnus-summary-mark-same-subject
9637           (gnus-summary-article-subject) unmark)))
9638     ;; Select next unread article.  If auto-select-same mode, should
9639     ;; select the first unread article.
9640     (gnus-summary-next-article t (and gnus-auto-select-same
9641                                       (gnus-summary-article-subject)))
9642     (gnus-message 7 "%d article%s marked as %s"
9643                   count (if (= count 1) " is" "s are")
9644                   (if unmark "unread" "read"))))
9645
9646 (defun gnus-summary-kill-same-subject (&optional unmark)
9647   "Mark articles which has the same subject as read.
9648 If UNMARK is positive, remove any kind of mark.
9649 If UNMARK is negative, tick articles."
9650   (interactive "P")
9651   (when unmark
9652     (setq unmark (prefix-numeric-value unmark)))
9653   (let ((count
9654          (gnus-summary-mark-same-subject
9655           (gnus-summary-article-subject) unmark)))
9656     ;; If marked as read, go to next unread subject.
9657     (when (null unmark)
9658       ;; Go to next unread subject.
9659       (gnus-summary-next-subject 1 t))
9660     (gnus-message 7 "%d articles are marked as %s"
9661                   count (if unmark "unread" "read"))))
9662
9663 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9664   "Mark articles with same SUBJECT as read, and return marked number.
9665 If optional argument UNMARK is positive, remove any kinds of marks.
9666 If optional argument UNMARK is negative, mark articles as unread instead."
9667   (let ((count 1))
9668     (save-excursion
9669       (cond
9670        ((null unmark)                   ; Mark as read.
9671         (while (and
9672                 (progn
9673                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9674                   (gnus-summary-show-thread) t)
9675                 (gnus-summary-find-subject subject))
9676           (setq count (1+ count))))
9677        ((> unmark 0)                    ; Tick.
9678         (while (and
9679                 (progn
9680                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9681                   (gnus-summary-show-thread) t)
9682                 (gnus-summary-find-subject subject))
9683           (setq count (1+ count))))
9684        (t                               ; Mark as unread.
9685         (while (and
9686                 (progn
9687                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9688                   (gnus-summary-show-thread) t)
9689                 (gnus-summary-find-subject subject))
9690           (setq count (1+ count)))))
9691       (gnus-set-mode-line 'summary)
9692       ;; Return the number of marked articles.
9693       count)))
9694
9695 (defun gnus-summary-mark-as-processable (n &optional unmark)
9696   "Set the process mark on the next N articles.
9697 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9698 the process mark instead.  The difference between N and the actual
9699 number of articles marked is returned."
9700   (interactive "P")
9701   (if (and (null n) (gnus-region-active-p))
9702       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9703     (setq n (prefix-numeric-value n))
9704     (let ((backward (< n 0))
9705           (n (abs n)))
9706       (while (and
9707               (> n 0)
9708               (if unmark
9709                   (gnus-summary-remove-process-mark
9710                    (gnus-summary-article-number))
9711                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9712               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9713         (setq n (1- n)))
9714       (when (/= 0 n)
9715         (gnus-message 7 "No more articles"))
9716       (gnus-summary-recenter)
9717       (gnus-summary-position-point)
9718       n)))
9719
9720 (defun gnus-summary-unmark-as-processable (n)
9721   "Remove the process mark from the next N articles.
9722 If N is negative, unmark backward instead.  The difference between N and
9723 the actual number of articles unmarked is returned."
9724   (interactive "P")
9725   (gnus-summary-mark-as-processable n t))
9726
9727 (defun gnus-summary-unmark-all-processable ()
9728   "Remove the process mark from all articles."
9729   (interactive)
9730   (save-excursion
9731     (while gnus-newsgroup-processable
9732       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9733   (gnus-summary-position-point))
9734
9735 (defun gnus-summary-add-mark (article type)
9736   "Mark ARTICLE with a mark of TYPE."
9737   (let ((vtype (car (assq type gnus-article-mark-lists)))
9738         var)
9739     (if (not vtype)
9740         (error "No such mark type: %s" type)
9741       (setq var (intern (format "gnus-newsgroup-%s" type)))
9742       (set var (cons article (symbol-value var)))
9743       (if (memq type '(processable cached replied forwarded recent saved))
9744           (gnus-summary-update-secondary-mark article)
9745         ;;; !!! This is bogus.  We should find out what primary
9746         ;;; !!! mark we want to set.
9747         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9748
9749 (defun gnus-summary-mark-as-expirable (n)
9750   "Mark N articles forward as expirable.
9751 If N is negative, mark backward instead.  The difference between N and
9752 the actual number of articles marked is returned."
9753   (interactive "p")
9754   (gnus-summary-mark-forward n gnus-expirable-mark))
9755
9756 (defun gnus-summary-mark-as-spam (n)
9757   "Mark N articles forward as spam.
9758 If N is negative, mark backward instead.  The difference between N and
9759 the actual number of articles marked is returned."
9760   (interactive "p")
9761   (gnus-summary-mark-forward n gnus-spam-mark))
9762
9763 (defun gnus-summary-mark-article-as-replied (article)
9764   "Mark ARTICLE as replied to and update the summary line.
9765 ARTICLE can also be a list of articles."
9766   (interactive (list (gnus-summary-article-number)))
9767   (let ((articles (if (listp article) article (list article))))
9768     (dolist (article articles)
9769       (unless (numberp article)
9770         (error "%s is not a number" article))
9771       (push article gnus-newsgroup-replied)
9772       (let ((buffer-read-only nil))
9773         (when (gnus-summary-goto-subject article nil t)
9774           (gnus-summary-update-secondary-mark article))))))
9775
9776 (defun gnus-summary-mark-article-as-forwarded (article)
9777   "Mark ARTICLE as forwarded and update the summary line.
9778 ARTICLE can also be a list of articles."
9779   (let ((articles (if (listp article) article (list article))))
9780     (dolist (article articles)
9781       (push article gnus-newsgroup-forwarded)
9782       (let ((buffer-read-only nil))
9783         (when (gnus-summary-goto-subject article nil t)
9784           (gnus-summary-update-secondary-mark article))))))
9785
9786 (defun gnus-summary-set-bookmark (article)
9787   "Set a bookmark in current article."
9788   (interactive (list (gnus-summary-article-number)))
9789   (when (or (not (get-buffer gnus-article-buffer))
9790             (not gnus-current-article)
9791             (not gnus-article-current)
9792             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9793     (error "No current article selected"))
9794   ;; Remove old bookmark, if one exists.
9795   (gnus-pull article gnus-newsgroup-bookmarks)
9796   ;; Set the new bookmark, which is on the form
9797   ;; (article-number . line-number-in-body).
9798   (push
9799    (cons article
9800          (save-excursion
9801            (set-buffer gnus-article-buffer)
9802            (count-lines
9803             (min (point)
9804                  (save-excursion
9805                    (article-goto-body)
9806                    (point)))
9807             (point))))
9808    gnus-newsgroup-bookmarks)
9809   (gnus-message 6 "A bookmark has been added to the current article."))
9810
9811 (defun gnus-summary-remove-bookmark (article)
9812   "Remove the bookmark from the current article."
9813   (interactive (list (gnus-summary-article-number)))
9814   ;; Remove old bookmark, if one exists.
9815   (if (not (assq article gnus-newsgroup-bookmarks))
9816       (gnus-message 6 "No bookmark in current article.")
9817     (gnus-pull article gnus-newsgroup-bookmarks)
9818     (gnus-message 6 "Removed bookmark.")))
9819
9820 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9821 (defun gnus-summary-mark-as-dormant (n)
9822   "Mark N articles forward as dormant.
9823 If N is negative, mark backward instead.  The difference between N and
9824 the actual number of articles marked is returned."
9825   (interactive "p")
9826   (gnus-summary-mark-forward n gnus-dormant-mark))
9827
9828 (defun gnus-summary-set-process-mark (article)
9829   "Set the process mark on ARTICLE and update the summary line."
9830   (setq gnus-newsgroup-processable
9831         (cons article
9832               (delq article gnus-newsgroup-processable)))
9833   (when (gnus-summary-goto-subject article)
9834     (gnus-summary-show-thread)
9835     (gnus-summary-goto-subject article)
9836     (gnus-summary-update-secondary-mark article)))
9837
9838 (defun gnus-summary-remove-process-mark (article)
9839   "Remove the process mark from ARTICLE and update the summary line."
9840   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9841   (when (gnus-summary-goto-subject article)
9842     (gnus-summary-show-thread)
9843     (gnus-summary-goto-subject article)
9844     (gnus-summary-update-secondary-mark article)))
9845
9846 (defun gnus-summary-set-saved-mark (article)
9847   "Set the process mark on ARTICLE and update the summary line."
9848   (push article gnus-newsgroup-saved)
9849   (when (gnus-summary-goto-subject article)
9850     (gnus-summary-update-secondary-mark article)))
9851
9852 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9853   "Mark N articles as read forwards.
9854 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9855 The difference between N and the actual number of articles marked is
9856 returned.
9857 If NO-EXPIRE, auto-expiry will be inhibited."
9858   (interactive "p")
9859   (gnus-summary-show-thread)
9860   (let ((backward (< n 0))
9861         (gnus-summary-goto-unread
9862          (and gnus-summary-goto-unread
9863               (not (eq gnus-summary-goto-unread 'never))
9864               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9865                                     gnus-ticked-mark gnus-dormant-mark)))))
9866         (n (abs n))
9867         (mark (or mark gnus-del-mark)))
9868     (while (and (> n 0)
9869                 (gnus-summary-mark-article nil mark no-expire)
9870                 (zerop (gnus-summary-next-subject
9871                         (if backward -1 1)
9872                         (and gnus-summary-goto-unread
9873                              (not (eq gnus-summary-goto-unread 'never)))
9874                         t)))
9875       (setq n (1- n)))
9876     (when (/= 0 n)
9877       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9878     (gnus-summary-recenter)
9879     (gnus-summary-position-point)
9880     (gnus-set-mode-line 'summary)
9881     n))
9882
9883 (defun gnus-summary-mark-article-as-read (mark)
9884   "Mark the current article quickly as read with MARK."
9885   (let ((article (gnus-summary-article-number)))
9886     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9887     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9888     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9890     (push (cons article mark) gnus-newsgroup-reads)
9891     ;; Possibly remove from cache, if that is used.
9892     (when gnus-use-cache
9893       (gnus-cache-enter-remove-article article))
9894     ;; Allow the backend to change the mark.
9895     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9896     ;; Check for auto-expiry.
9897     (when (and gnus-newsgroup-auto-expire
9898                (memq mark gnus-auto-expirable-marks))
9899       (setq mark gnus-expirable-mark)
9900       ;; Let the backend know about the mark change.
9901       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9902       (push article gnus-newsgroup-expirable))
9903     ;; Set the mark in the buffer.
9904     (gnus-summary-update-mark mark 'unread)
9905     t))
9906
9907 (defun gnus-summary-mark-article-as-unread (mark)
9908   "Mark the current article quickly as unread with MARK."
9909   (let* ((article (gnus-summary-article-number))
9910          (old-mark (gnus-summary-article-mark article)))
9911     ;; Allow the backend to change the mark.
9912     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9913     (if (eq mark old-mark)
9914         t
9915       (if (<= article 0)
9916           (progn
9917             (gnus-error 1 "Can't mark negative article numbers")
9918             nil)
9919         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9920         (setq gnus-newsgroup-spam-marked
9921               (delq article gnus-newsgroup-spam-marked))
9922         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9923         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9924         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9925         (cond ((= mark gnus-ticked-mark)
9926                (setq gnus-newsgroup-marked
9927                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9928                                               article)))
9929               ((= mark gnus-spam-mark)
9930                (setq gnus-newsgroup-spam-marked
9931                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9932                                               article)))
9933               ((= mark gnus-dormant-mark)
9934                (setq gnus-newsgroup-dormant
9935                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9936                                               article)))
9937               (t
9938                (setq gnus-newsgroup-unreads
9939                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9940                                               article))))
9941         (gnus-pull article gnus-newsgroup-reads)
9942
9943         ;; See whether the article is to be put in the cache.
9944         (and gnus-use-cache
9945              (vectorp (gnus-summary-article-header article))
9946              (save-excursion
9947                (gnus-cache-possibly-enter-article
9948                 gnus-newsgroup-name article
9949                 (= mark gnus-ticked-mark)
9950                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9951
9952         ;; Fix the mark.
9953         (gnus-summary-update-mark mark 'unread)
9954         t))))
9955
9956 (defun gnus-summary-mark-article (&optional article mark no-expire)
9957   "Mark ARTICLE with MARK.  MARK can be any character.
9958 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9959 `??' (dormant) and `?E' (expirable).
9960 If MARK is nil, then the default character `?r' is used.
9961 If ARTICLE is nil, then the article on the current line will be
9962 marked.
9963 If NO-EXPIRE, auto-expiry will be inhibited."
9964   ;; The mark might be a string.
9965   (when (stringp mark)
9966     (setq mark (aref mark 0)))
9967   ;; If no mark is given, then we check auto-expiring.
9968   (when (null mark)
9969     (setq mark gnus-del-mark))
9970   (when (and (not no-expire)
9971              gnus-newsgroup-auto-expire
9972              (memq mark gnus-auto-expirable-marks))
9973     (setq mark gnus-expirable-mark))
9974   (let ((article (or article (gnus-summary-article-number)))
9975         (old-mark (gnus-summary-article-mark article)))
9976     ;; Allow the backend to change the mark.
9977     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9978     (if (eq mark old-mark)
9979         t
9980       (unless article
9981         (error "No article on current line"))
9982       (if (not (if (or (= mark gnus-unread-mark)
9983                        (= mark gnus-ticked-mark)
9984                        (= mark gnus-spam-mark)
9985                        (= mark gnus-dormant-mark))
9986                    (gnus-mark-article-as-unread article mark)
9987                  (gnus-mark-article-as-read article mark)))
9988           t
9989         ;; See whether the article is to be put in the cache.
9990         (and gnus-use-cache
9991              (not (= mark gnus-canceled-mark))
9992              (vectorp (gnus-summary-article-header article))
9993              (save-excursion
9994                (gnus-cache-possibly-enter-article
9995                 gnus-newsgroup-name article
9996                 (= mark gnus-ticked-mark)
9997                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9998
9999         (when (gnus-summary-goto-subject article nil t)
10000           (let ((buffer-read-only nil))
10001             (gnus-summary-show-thread)
10002             ;; Fix the mark.
10003             (gnus-summary-update-mark mark 'unread)
10004             t))))))
10005
10006 (defun gnus-summary-update-secondary-mark (article)
10007   "Update the secondary (read, process, cache) mark."
10008   (gnus-summary-update-mark
10009    (cond ((memq article gnus-newsgroup-processable)
10010           gnus-process-mark)
10011          ((memq article gnus-newsgroup-cached)
10012           gnus-cached-mark)
10013          ((memq article gnus-newsgroup-replied)
10014           gnus-replied-mark)
10015          ((memq article gnus-newsgroup-forwarded)
10016           gnus-forwarded-mark)
10017          ((memq article gnus-newsgroup-saved)
10018           gnus-saved-mark)
10019          ((memq article gnus-newsgroup-recent)
10020           gnus-recent-mark)
10021          ((memq article gnus-newsgroup-unseen)
10022           gnus-unseen-mark)
10023          (t gnus-no-mark))
10024    'replied)
10025   (when (gnus-visual-p 'summary-highlight 'highlight)
10026     (gnus-run-hooks 'gnus-summary-update-hook))
10027   t)
10028
10029 (defun gnus-summary-update-download-mark (article)
10030   "Update the download mark."
10031   (gnus-summary-update-mark
10032    (cond ((memq article gnus-newsgroup-undownloaded)
10033           gnus-undownloaded-mark)
10034          (gnus-newsgroup-agentized
10035           gnus-downloaded-mark)
10036          (t
10037           gnus-no-mark))
10038    'download)
10039   (gnus-summary-update-line t)
10040   t)
10041
10042 (defun gnus-summary-update-mark (mark type)
10043   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10044         (buffer-read-only nil))
10045     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10046     (when forward
10047       (when (looking-at "\r")
10048         (incf forward))
10049       (when (<= (+ forward (point)) (point-max))
10050         ;; Go to the right position on the line.
10051         (goto-char (+ forward (point)))
10052         ;; Replace the old mark with the new mark.
10053         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10054         ;; Optionally update the marks by some user rule.
10055         (when (eq type 'unread)
10056           (gnus-data-set-mark
10057            (gnus-data-find (gnus-summary-article-number)) mark)
10058           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10059
10060 (defun gnus-mark-article-as-read (article &optional mark)
10061   "Enter ARTICLE in the pertinent lists and remove it from others."
10062   ;; Make the article expirable.
10063   (let ((mark (or mark gnus-del-mark)))
10064     (setq gnus-newsgroup-expirable
10065           (if (= mark gnus-expirable-mark)
10066               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10067             (delq article gnus-newsgroup-expirable)))
10068     ;; Remove from unread and marked lists.
10069     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10070     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10071     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10072     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10073     (push (cons article mark) gnus-newsgroup-reads)
10074     ;; Possibly remove from cache, if that is used.
10075     (when gnus-use-cache
10076       (gnus-cache-enter-remove-article article))
10077     t))
10078
10079 (defun gnus-mark-article-as-unread (article &optional mark)
10080   "Enter ARTICLE in the pertinent lists and remove it from others."
10081   (let ((mark (or mark gnus-ticked-mark)))
10082     (if (<= article 0)
10083         (progn
10084           (gnus-error 1 "Can't mark negative article numbers")
10085           nil)
10086       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10087             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10088             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10089             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10090             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10091
10092       ;; Unsuppress duplicates?
10093       (when gnus-suppress-duplicates
10094         (gnus-dup-unsuppress-article article))
10095
10096       (cond ((= mark gnus-ticked-mark)
10097              (setq gnus-newsgroup-marked
10098                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10099             ((= mark gnus-spam-mark)
10100              (setq gnus-newsgroup-spam-marked
10101                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10102                                             article)))
10103             ((= mark gnus-dormant-mark)
10104              (setq gnus-newsgroup-dormant
10105                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10106             (t
10107              (setq gnus-newsgroup-unreads
10108                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10109       (gnus-pull article gnus-newsgroup-reads)
10110       t)))
10111
10112 (defalias 'gnus-summary-mark-as-unread-forward
10113   'gnus-summary-tick-article-forward)
10114 (make-obsolete 'gnus-summary-mark-as-unread-forward
10115                'gnus-summary-tick-article-forward)
10116 (defun gnus-summary-tick-article-forward (n)
10117   "Tick N articles forwards.
10118 If N is negative, tick backwards instead.
10119 The difference between N and the number of articles ticked is returned."
10120   (interactive "p")
10121   (gnus-summary-mark-forward n gnus-ticked-mark))
10122
10123 (defalias 'gnus-summary-mark-as-unread-backward
10124   'gnus-summary-tick-article-backward)
10125 (make-obsolete 'gnus-summary-mark-as-unread-backward
10126                'gnus-summary-tick-article-backward)
10127 (defun gnus-summary-tick-article-backward (n)
10128   "Tick N articles backwards.
10129 The difference between N and the number of articles ticked is returned."
10130   (interactive "p")
10131   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10132
10133 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10134 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10135 (defun gnus-summary-tick-article (&optional article clear-mark)
10136   "Mark current article as unread.
10137 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10138 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10139   (interactive)
10140   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10141                                        gnus-ticked-mark)))
10142
10143 (defun gnus-summary-mark-as-read-forward (n)
10144   "Mark N articles as read forwards.
10145 If N is negative, mark backwards instead.
10146 The difference between N and the actual number of articles marked is
10147 returned."
10148   (interactive "p")
10149   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10150
10151 (defun gnus-summary-mark-as-read-backward (n)
10152   "Mark the N articles as read backwards.
10153 The difference between N and the actual number of articles marked is
10154 returned."
10155   (interactive "p")
10156   (gnus-summary-mark-forward
10157    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10158
10159 (defun gnus-summary-mark-as-read (&optional article mark)
10160   "Mark current article as read.
10161 ARTICLE specifies the article to be marked as read.
10162 MARK specifies a string to be inserted at the beginning of the line."
10163   (gnus-summary-mark-article article mark))
10164
10165 (defun gnus-summary-clear-mark-forward (n)
10166   "Clear marks from N articles forward.
10167 If N is negative, clear backward instead.
10168 The difference between N and the number of marks cleared is returned."
10169   (interactive "p")
10170   (gnus-summary-mark-forward n gnus-unread-mark))
10171
10172 (defun gnus-summary-clear-mark-backward (n)
10173   "Clear marks from N articles backward.
10174 The difference between N and the number of marks cleared is returned."
10175   (interactive "p")
10176   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10177
10178 (defun gnus-summary-mark-unread-as-read ()
10179   "Intended to be used by `gnus-summary-mark-article-hook'."
10180   (when (memq gnus-current-article gnus-newsgroup-unreads)
10181     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10182
10183 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10184   "Intended to be used by `gnus-summary-mark-article-hook'."
10185   (let ((mark (gnus-summary-article-mark)))
10186     (when (or (gnus-unread-mark-p mark)
10187               (gnus-read-mark-p mark))
10188       (gnus-summary-mark-article gnus-current-article
10189                                  (or new-mark gnus-read-mark)))))
10190
10191 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10192   "Intended to be used by `gnus-summary-mark-article-hook'."
10193   (let ((mark (gnus-summary-article-mark)))
10194     (when (or (gnus-unread-mark-p mark)
10195               (gnus-read-mark-p mark))
10196       (gnus-summary-mark-article (gnus-summary-article-number)
10197                                  (or new-mark gnus-read-mark)))))
10198
10199 (defun gnus-summary-mark-unread-as-ticked ()
10200   "Intended to be used by `gnus-summary-mark-article-hook'."
10201   (when (memq gnus-current-article gnus-newsgroup-unreads)
10202     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10203
10204 (defun gnus-summary-mark-region-as-read (point mark all)
10205   "Mark all unread articles between point and mark as read.
10206 If given a prefix, mark all articles between point and mark as read,
10207 even ticked and dormant ones."
10208   (interactive "r\nP")
10209   (save-excursion
10210     (let (article)
10211       (goto-char point)
10212       (beginning-of-line)
10213       (while (and
10214               (< (point) mark)
10215               (progn
10216                 (when (or all
10217                           (memq (setq article (gnus-summary-article-number))
10218                                 gnus-newsgroup-unreads))
10219                   (gnus-summary-mark-article article gnus-del-mark))
10220                 t)
10221               (gnus-summary-find-next))))))
10222
10223 (defun gnus-summary-mark-below (score mark)
10224   "Mark articles with score less than SCORE with MARK."
10225   (interactive "P\ncMark: ")
10226   (setq score (if score
10227                   (prefix-numeric-value score)
10228                 (or gnus-summary-default-score 0)))
10229   (save-excursion
10230     (set-buffer gnus-summary-buffer)
10231     (goto-char (point-min))
10232     (while
10233         (progn
10234           (and (< (gnus-summary-article-score) score)
10235                (gnus-summary-mark-article nil mark))
10236           (gnus-summary-find-next)))))
10237
10238 (defun gnus-summary-kill-below (&optional score)
10239   "Mark articles with score below SCORE as read."
10240   (interactive "P")
10241   (gnus-summary-mark-below score gnus-killed-mark))
10242
10243 (defun gnus-summary-clear-above (&optional score)
10244   "Clear all marks from articles with score above SCORE."
10245   (interactive "P")
10246   (gnus-summary-mark-above score gnus-unread-mark))
10247
10248 (defun gnus-summary-tick-above (&optional score)
10249   "Tick all articles with score above SCORE."
10250   (interactive "P")
10251   (gnus-summary-mark-above score gnus-ticked-mark))
10252
10253 (defun gnus-summary-mark-above (score mark)
10254   "Mark articles with score over SCORE with MARK."
10255   (interactive "P\ncMark: ")
10256   (setq score (if score
10257                   (prefix-numeric-value score)
10258                 (or gnus-summary-default-score 0)))
10259   (save-excursion
10260     (set-buffer gnus-summary-buffer)
10261     (goto-char (point-min))
10262     (while (and (progn
10263                   (when (> (gnus-summary-article-score) score)
10264                     (gnus-summary-mark-article nil mark))
10265                   t)
10266                 (gnus-summary-find-next)))))
10267
10268 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10269 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10270 (defun gnus-summary-limit-include-expunged (&optional no-error)
10271   "Display all the hidden articles that were expunged for low scores."
10272   (interactive)
10273   (let ((buffer-read-only nil))
10274     (let ((scored gnus-newsgroup-scored)
10275           headers h)
10276       (while scored
10277         (unless (gnus-summary-article-header (caar scored))
10278           (and (setq h (gnus-number-to-header (caar scored)))
10279                (< (cdar scored) gnus-summary-expunge-below)
10280                (push h headers)))
10281         (setq scored (cdr scored)))
10282       (if (not headers)
10283           (when (not no-error)
10284             (error "No expunged articles hidden"))
10285         (goto-char (point-min))
10286         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10287         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10288         (mapcar (lambda (x) (push (mail-header-number x)
10289                                   gnus-newsgroup-limit))
10290                 headers)
10291         (gnus-summary-prepare-unthreaded (nreverse headers))
10292         (goto-char (point-min))
10293         (gnus-summary-position-point)
10294         t))))
10295
10296 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10297   "Mark all unread articles in this newsgroup as read.
10298 If prefix argument ALL is non-nil, ticked and dormant articles will
10299 also be marked as read.
10300 If QUIETLY is non-nil, no questions will be asked.
10301
10302 If TO-HERE is non-nil, it should be a point in the buffer.  All
10303 articles before (after, if REVERSE is set) this point will be marked
10304 as read.
10305
10306 Note that this function will only catch up the unread article
10307 in the current summary buffer limitation.
10308
10309 The number of articles marked as read is returned."
10310   (interactive "P")
10311   (prog1
10312       (save-excursion
10313         (when (or quietly
10314                   (not gnus-interactive-catchup) ;Without confirmation?
10315                   gnus-expert-user
10316                   (gnus-y-or-n-p
10317                    (if all
10318                        "Mark absolutely all articles as read? "
10319                      "Mark all unread articles as read? ")))
10320           (if (and not-mark
10321                    (not gnus-newsgroup-adaptive)
10322                    (not gnus-newsgroup-auto-expire)
10323                    (not gnus-suppress-duplicates)
10324                    (or (not gnus-use-cache)
10325                        (eq gnus-use-cache 'passive)))
10326               (progn
10327                 (when all
10328                   (setq gnus-newsgroup-marked nil
10329                         gnus-newsgroup-spam-marked nil
10330                         gnus-newsgroup-dormant nil))
10331                 (setq gnus-newsgroup-unreads
10332                       (gnus-sorted-nunion
10333                        (gnus-intersection gnus-newsgroup-unreads
10334                                           gnus-newsgroup-downloadable)
10335                        gnus-newsgroup-unfetched)))
10336             ;; We actually mark all articles as canceled, which we
10337             ;; have to do when using auto-expiry or adaptive scoring.
10338             (gnus-summary-show-all-threads)
10339             (if (and to-here reverse)
10340                 (progn
10341                   (goto-char to-here)
10342                   (gnus-summary-mark-current-read-and-unread-as-read
10343                    gnus-catchup-mark)
10344                   (while (gnus-summary-find-next (not all))
10345                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10346               (when (gnus-summary-first-subject (not all))
10347                 (while (and
10348                         (if to-here (< (point) to-here) t)
10349                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10350                         (gnus-summary-find-next (not all))))))
10351             (gnus-set-mode-line 'summary))
10352           t))
10353     (gnus-summary-position-point)))
10354
10355 (defun gnus-summary-catchup-to-here (&optional all)
10356   "Mark all unticked articles before the current one as read.
10357 If ALL is non-nil, also mark ticked and dormant articles as read."
10358   (interactive "P")
10359   (save-excursion
10360     (gnus-save-hidden-threads
10361       (let ((beg (point)))
10362         ;; We check that there are unread articles.
10363         (when (or all (gnus-summary-find-prev))
10364           (gnus-summary-catchup all t beg)))))
10365   (gnus-summary-position-point))
10366
10367 (defun gnus-summary-catchup-from-here (&optional all)
10368   "Mark all unticked articles after (and including) the current one as read.
10369 If ALL is non-nil, also mark ticked and dormant articles as read."
10370   (interactive "P")
10371   (save-excursion
10372     (gnus-save-hidden-threads
10373       (let ((beg (point)))
10374         ;; We check that there are unread articles.
10375         (when (or all (gnus-summary-find-next))
10376           (gnus-summary-catchup all t beg nil t)))))
10377   (gnus-summary-position-point))
10378
10379 (defun gnus-summary-catchup-all (&optional quietly)
10380   "Mark all articles in this newsgroup as read.
10381 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10382 instead, which marks only unread articles as read."
10383   (interactive "P")
10384   (gnus-summary-catchup t quietly))
10385
10386 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10387   "Mark all unread articles in this group as read, then exit.
10388 If prefix argument ALL is non-nil, all articles are marked as read.
10389 If QUIETLY is non-nil, no questions will be asked."
10390   (interactive "P")
10391   (when (gnus-summary-catchup all quietly nil 'fast)
10392     ;; Select next newsgroup or exit.
10393     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10394              (eq gnus-auto-select-next 'quietly))
10395         (gnus-summary-next-group nil)
10396       (gnus-summary-exit))))
10397
10398 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10399   "Mark all articles in this newsgroup as read, and then exit.
10400 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10401 instead, which marks only unread articles as read."
10402   (interactive "P")
10403   (gnus-summary-catchup-and-exit t quietly))
10404
10405 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10406   "Mark all articles in this group as read and select the next group.
10407 If given a prefix, mark all articles, unread as well as ticked, as
10408 read."
10409   (interactive "P")
10410   (save-excursion
10411     (gnus-summary-catchup all))
10412   (gnus-summary-next-group))
10413
10414 ;;;
10415 ;;; with article
10416 ;;;
10417
10418 (defmacro gnus-with-article (article &rest forms)
10419   "Select ARTICLE and perform FORMS in the original article buffer.
10420 Then replace the article with the result."
10421   `(progn
10422      ;; We don't want the article to be marked as read.
10423      (let (gnus-mark-article-hook)
10424        (gnus-summary-select-article t t nil ,article))
10425      (set-buffer gnus-original-article-buffer)
10426      ,@forms
10427      (if (not (gnus-check-backend-function
10428                'request-replace-article (car gnus-article-current)))
10429          (gnus-message 5 "Read-only group; not replacing")
10430        (unless (gnus-request-replace-article
10431                 ,article (car gnus-article-current)
10432                 (current-buffer) t)
10433          (error "Couldn't replace article")))
10434      ;; The cache and backlog have to be flushed somewhat.
10435      (when gnus-keep-backlog
10436        (gnus-backlog-remove-article
10437         (car gnus-article-current) (cdr gnus-article-current)))
10438      (when gnus-use-cache
10439        (gnus-cache-update-article
10440         (car gnus-article-current) (cdr gnus-article-current)))))
10441
10442 (put 'gnus-with-article 'lisp-indent-function 1)
10443 (put 'gnus-with-article 'edebug-form-spec '(form body))
10444
10445 ;; Thread-based commands.
10446
10447 (defun gnus-summary-articles-in-thread (&optional article)
10448   "Return a list of all articles in the current thread.
10449 If ARTICLE is non-nil, return all articles in the thread that starts
10450 with that article."
10451   (let* ((article (or article (gnus-summary-article-number)))
10452          (data (gnus-data-find-list article))
10453          (top-level (gnus-data-level (car data)))
10454          (top-subject
10455           (cond ((null gnus-thread-operation-ignore-subject)
10456                  (gnus-simplify-subject-re
10457                   (mail-header-subject (gnus-data-header (car data)))))
10458                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10459                  (gnus-simplify-subject-fuzzy
10460                   (mail-header-subject (gnus-data-header (car data)))))
10461                 (t nil)))
10462          (end-point (save-excursion
10463                       (if (gnus-summary-go-to-next-thread)
10464                           (point) (point-max))))
10465          articles)
10466     (while (and data
10467                 (< (gnus-data-pos (car data)) end-point))
10468       (when (or (not top-subject)
10469                 (string= top-subject
10470                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10471                              (gnus-simplify-subject-fuzzy
10472                               (mail-header-subject
10473                                (gnus-data-header (car data))))
10474                            (gnus-simplify-subject-re
10475                             (mail-header-subject
10476                              (gnus-data-header (car data)))))))
10477         (push (gnus-data-number (car data)) articles))
10478       (unless (and (setq data (cdr data))
10479                    (> (gnus-data-level (car data)) top-level))
10480         (setq data nil)))
10481     ;; Return the list of articles.
10482     (nreverse articles)))
10483
10484 (defun gnus-summary-rethread-current ()
10485   "Rethread the thread the current article is part of."
10486   (interactive)
10487   (let* ((gnus-show-threads t)
10488          (article (gnus-summary-article-number))
10489          (id (mail-header-id (gnus-summary-article-header)))
10490          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10491     (unless id
10492       (error "No article on the current line"))
10493     (gnus-rebuild-thread id)
10494     (gnus-summary-goto-subject article)))
10495
10496 (defun gnus-summary-reparent-thread ()
10497   "Make the current article child of the marked (or previous) article.
10498
10499 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10500 is non-nil or the Subject: of both articles are the same."
10501   (interactive)
10502   (unless (not (gnus-group-read-only-p))
10503     (error "The current newsgroup does not support article editing"))
10504   (unless (<= (length gnus-newsgroup-processable) 1)
10505     (error "No more than one article may be marked"))
10506   (save-window-excursion
10507     (let ((gnus-article-buffer " *reparent*")
10508           (current-article (gnus-summary-article-number))
10509           ;; First grab the marked article, otherwise one line up.
10510           (parent-article (if (not (null gnus-newsgroup-processable))
10511                               (car gnus-newsgroup-processable)
10512                             (save-excursion
10513                               (if (eq (forward-line -1) 0)
10514                                   (gnus-summary-article-number)
10515                                 (error "Beginning of summary buffer"))))))
10516       (unless (not (eq current-article parent-article))
10517         (error "An article may not be self-referential"))
10518       (let ((message-id (mail-header-id
10519                          (gnus-summary-article-header parent-article))))
10520         (unless (and message-id (not (equal message-id "")))
10521           (error "No message-id in desired parent"))
10522         (gnus-with-article current-article
10523           (save-restriction
10524             (goto-char (point-min))
10525             (message-narrow-to-head)
10526             (if (re-search-forward "^References: " nil t)
10527                 (progn
10528                   (re-search-forward "^[^ \t]" nil t)
10529                   (forward-line -1)
10530                   (end-of-line)
10531                   (insert " " message-id))
10532               (insert "References: " message-id "\n"))))
10533         (set-buffer gnus-summary-buffer)
10534         (gnus-summary-unmark-all-processable)
10535         (gnus-summary-update-article current-article)
10536         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10537             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10538         (gnus-summary-rethread-current)
10539         (gnus-message 3 "Article %d is now the child of article %d"
10540                       current-article parent-article)))))
10541
10542 (defun gnus-summary-toggle-threads (&optional arg)
10543   "Toggle showing conversation threads.
10544 If ARG is positive number, turn showing conversation threads on."
10545   (interactive "P")
10546   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10547     (setq gnus-show-threads
10548           (if (null arg) (not gnus-show-threads)
10549             (> (prefix-numeric-value arg) 0)))
10550     (gnus-summary-prepare)
10551     (gnus-summary-goto-subject current)
10552     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10553     (gnus-summary-position-point)))
10554
10555 (defun gnus-summary-show-all-threads ()
10556   "Show all threads."
10557   (interactive)
10558   (save-excursion
10559     (let ((buffer-read-only nil))
10560       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10561   (gnus-summary-position-point))
10562
10563 (defun gnus-summary-show-thread ()
10564   "Show thread subtrees.
10565 Returns nil if no thread was there to be shown."
10566   (interactive)
10567   (let ((buffer-read-only nil)
10568         (orig (point))
10569         (end (point-at-eol))
10570         ;; Leave point at bol
10571         (beg (progn (beginning-of-line) (point))))
10572     (prog1
10573         ;; Any hidden lines here?
10574         (search-forward "\r" end t)
10575       (subst-char-in-region beg end ?\^M ?\n t)
10576       (goto-char orig)
10577       (gnus-summary-position-point))))
10578
10579 (defun gnus-summary-maybe-hide-threads ()
10580   "If requested, hide the threads that should be hidden."
10581   (when (and gnus-show-threads
10582              gnus-thread-hide-subtree)
10583     (gnus-summary-hide-all-threads
10584      (if (or (consp gnus-thread-hide-subtree)
10585              (functionp gnus-thread-hide-subtree))
10586          (gnus-make-predicate gnus-thread-hide-subtree)
10587        nil))))
10588
10589 ;;; Hiding predicates.
10590
10591 (defun gnus-article-unread-p (header)
10592   (memq (mail-header-number header) gnus-newsgroup-unreads))
10593
10594 (defun gnus-article-unseen-p (header)
10595   (memq (mail-header-number header) gnus-newsgroup-unseen))
10596
10597 (defun gnus-map-articles (predicate articles)
10598   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10599   (apply 'gnus-or (mapcar predicate
10600                           (mapcar 'gnus-summary-article-header articles))))
10601
10602 (defun gnus-summary-hide-all-threads (&optional predicate)
10603   "Hide all thread subtrees.
10604 If PREDICATE is supplied, threads that satisfy this predicate
10605 will not be hidden."
10606   (interactive)
10607   (save-excursion
10608     (goto-char (point-min))
10609     (let ((end nil))
10610       (while (not end)
10611         (when (or (not predicate)
10612                   (gnus-map-articles
10613                    predicate (gnus-summary-article-children)))
10614             (gnus-summary-hide-thread))
10615         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10616   (gnus-summary-position-point))
10617
10618 (defun gnus-summary-hide-thread ()
10619   "Hide thread subtrees.
10620 If PREDICATE is supplied, threads that satisfy this predicate
10621 will not be hidden.
10622 Returns nil if no threads were there to be hidden."
10623   (interactive)
10624   (let ((buffer-read-only nil)
10625         (start (point))
10626         (article (gnus-summary-article-number)))
10627     (goto-char start)
10628     ;; Go forward until either the buffer ends or the subthread
10629     ;; ends.
10630     (when (and (not (eobp))
10631                (or (zerop (gnus-summary-next-thread 1 t))
10632                    (goto-char (point-max))))
10633       (prog1
10634           (if (and (> (point) start)
10635                    (search-backward "\n" start t))
10636               (progn
10637                 (subst-char-in-region start (point) ?\n ?\^M)
10638                 (gnus-summary-goto-subject article))
10639             (goto-char start)
10640             nil)))))
10641
10642 (defun gnus-summary-go-to-next-thread (&optional previous)
10643   "Go to the same level (or less) next thread.
10644 If PREVIOUS is non-nil, go to previous thread instead.
10645 Return the article number moved to, or nil if moving was impossible."
10646   (let ((level (gnus-summary-thread-level))
10647         (way (if previous -1 1))
10648         (beg (point)))
10649     (forward-line way)
10650     (while (and (not (eobp))
10651                 (< level (gnus-summary-thread-level)))
10652       (forward-line way))
10653     (if (eobp)
10654         (progn
10655           (goto-char beg)
10656           nil)
10657       (setq beg (point))
10658       (prog1
10659           (gnus-summary-article-number)
10660         (goto-char beg)))))
10661
10662 (defun gnus-summary-next-thread (n &optional silent)
10663   "Go to the same level next N'th thread.
10664 If N is negative, search backward instead.
10665 Returns the difference between N and the number of skips actually
10666 done.
10667
10668 If SILENT, don't output messages."
10669   (interactive "p")
10670   (let ((backward (< n 0))
10671         (n (abs n)))
10672     (while (and (> n 0)
10673                 (gnus-summary-go-to-next-thread backward))
10674       (decf n))
10675     (unless silent
10676       (gnus-summary-position-point))
10677     (when (and (not silent) (/= 0 n))
10678       (gnus-message 7 "No more threads"))
10679     n))
10680
10681 (defun gnus-summary-prev-thread (n)
10682   "Go to the same level previous N'th thread.
10683 Returns the difference between N and the number of skips actually
10684 done."
10685   (interactive "p")
10686   (gnus-summary-next-thread (- n)))
10687
10688 (defun gnus-summary-go-down-thread ()
10689   "Go down one level in the current thread."
10690   (let ((children (gnus-summary-article-children)))
10691     (when children
10692       (gnus-summary-goto-subject (car children)))))
10693
10694 (defun gnus-summary-go-up-thread ()
10695   "Go up one level in the current thread."
10696   (let ((parent (gnus-summary-article-parent)))
10697     (when parent
10698       (gnus-summary-goto-subject parent))))
10699
10700 (defun gnus-summary-down-thread (n)
10701   "Go down thread N steps.
10702 If N is negative, go up instead.
10703 Returns the difference between N and how many steps down that were
10704 taken."
10705   (interactive "p")
10706   (let ((up (< n 0))
10707         (n (abs n)))
10708     (while (and (> n 0)
10709                 (if up (gnus-summary-go-up-thread)
10710                   (gnus-summary-go-down-thread)))
10711       (setq n (1- n)))
10712     (gnus-summary-position-point)
10713     (when (/= 0 n)
10714       (gnus-message 7 "Can't go further"))
10715     n))
10716
10717 (defun gnus-summary-up-thread (n)
10718   "Go up thread N steps.
10719 If N is negative, go down instead.
10720 Returns the difference between N and how many steps down that were
10721 taken."
10722   (interactive "p")
10723   (gnus-summary-down-thread (- n)))
10724
10725 (defun gnus-summary-top-thread ()
10726   "Go to the top of the thread."
10727   (interactive)
10728   (while (gnus-summary-go-up-thread))
10729   (gnus-summary-article-number))
10730
10731 (defun gnus-summary-kill-thread (&optional unmark)
10732   "Mark articles under current thread as read.
10733 If the prefix argument is positive, remove any kinds of marks.
10734 If the prefix argument is negative, tick articles instead."
10735   (interactive "P")
10736   (when unmark
10737     (setq unmark (prefix-numeric-value unmark)))
10738   (let ((articles (gnus-summary-articles-in-thread)))
10739     (save-excursion
10740       ;; Expand the thread.
10741       (gnus-summary-show-thread)
10742       ;; Mark all the articles.
10743       (while articles
10744         (gnus-summary-goto-subject (car articles))
10745         (cond ((null unmark)
10746                (gnus-summary-mark-article-as-read gnus-killed-mark))
10747               ((> unmark 0)
10748                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10749               (t
10750                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10751         (setq articles (cdr articles))))
10752     ;; Hide killed subtrees.
10753     (and (null unmark)
10754          gnus-thread-hide-killed
10755          (gnus-summary-hide-thread))
10756     ;; If marked as read, go to next unread subject.
10757     (when (null unmark)
10758       ;; Go to next unread subject.
10759       (gnus-summary-next-subject 1 t)))
10760   (gnus-set-mode-line 'summary))
10761
10762 ;; Summary sorting commands
10763
10764 (defun gnus-summary-sort-by-number (&optional reverse)
10765   "Sort the summary buffer by article number.
10766 Argument REVERSE means reverse order."
10767   (interactive "P")
10768   (gnus-summary-sort 'number reverse))
10769
10770 (defun gnus-summary-sort-by-random (&optional reverse)
10771   "Randomize the order in the summary buffer.
10772 Argument REVERSE means to randomize in reverse order."
10773   (interactive "P")
10774   (gnus-summary-sort 'random reverse))
10775
10776 (defun gnus-summary-sort-by-author (&optional reverse)
10777   "Sort the summary buffer by author name alphabetically.
10778 If `case-fold-search' is non-nil, case of letters is ignored.
10779 Argument REVERSE means reverse order."
10780   (interactive "P")
10781   (gnus-summary-sort 'author reverse))
10782
10783 (defun gnus-summary-sort-by-subject (&optional reverse)
10784   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10785 If `case-fold-search' is non-nil, case of letters is ignored.
10786 Argument REVERSE means reverse order."
10787   (interactive "P")
10788   (gnus-summary-sort 'subject reverse))
10789
10790 (defun gnus-summary-sort-by-date (&optional reverse)
10791   "Sort the summary buffer by date.
10792 Argument REVERSE means reverse order."
10793   (interactive "P")
10794   (gnus-summary-sort 'date reverse))
10795
10796 (defun gnus-summary-sort-by-score (&optional reverse)
10797   "Sort the summary buffer by score.
10798 Argument REVERSE means reverse order."
10799   (interactive "P")
10800   (gnus-summary-sort 'score reverse))
10801
10802 (defun gnus-summary-sort-by-lines (&optional reverse)
10803   "Sort the summary buffer by the number of lines.
10804 Argument REVERSE means reverse order."
10805   (interactive "P")
10806   (gnus-summary-sort 'lines reverse))
10807
10808 (defun gnus-summary-sort-by-chars (&optional reverse)
10809   "Sort the summary buffer by article length.
10810 Argument REVERSE means reverse order."
10811   (interactive "P")
10812   (gnus-summary-sort 'chars reverse))
10813
10814 (defun gnus-summary-sort-by-original (&optional reverse)
10815   "Sort the summary buffer using the default sorting method.
10816 Argument REVERSE means reverse order."
10817   (interactive "P")
10818   (let* ((buffer-read-only)
10819          (gnus-summary-prepare-hook nil))
10820     ;; We do the sorting by regenerating the threads.
10821     (gnus-summary-prepare)
10822     ;; Hide subthreads if needed.
10823     (gnus-summary-maybe-hide-threads)))
10824
10825 (defun gnus-summary-sort (predicate reverse)
10826   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10827   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10828          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10829          (gnus-thread-sort-functions
10830           (if (not reverse)
10831               thread
10832             `(lambda (t1 t2)
10833                (,thread t2 t1))))
10834          (gnus-sort-gathered-threads-function
10835           gnus-thread-sort-functions)
10836          (gnus-article-sort-functions
10837           (if (not reverse)
10838               article
10839             `(lambda (t1 t2)
10840                (,article t2 t1))))
10841          (buffer-read-only)
10842          (gnus-summary-prepare-hook nil))
10843     ;; We do the sorting by regenerating the threads.
10844     (gnus-summary-prepare)
10845     ;; Hide subthreads if needed.
10846     (gnus-summary-maybe-hide-threads)))
10847
10848 ;; Summary saving commands.
10849
10850 (defun gnus-summary-save-article (&optional n not-saved)
10851   "Save the current article using the default saver function.
10852 If N is a positive number, save the N next articles.
10853 If N is a negative number, save the N previous articles.
10854 If N is nil and any articles have been marked with the process mark,
10855 save those articles instead.
10856 The variable `gnus-default-article-saver' specifies the saver function."
10857   (interactive "P")
10858   (let* ((articles (gnus-summary-work-articles n))
10859          (save-buffer (save-excursion
10860                         (nnheader-set-temp-buffer " *Gnus Save*")))
10861          (num (length articles))
10862          header file)
10863     (dolist (article articles)
10864       (setq header (gnus-summary-article-header article))
10865       (if (not (vectorp header))
10866           ;; This is a pseudo-article.
10867           (if (assq 'name header)
10868               (gnus-copy-file (cdr (assq 'name header)))
10869             (gnus-message 1 "Article %d is unsaveable" article))
10870         ;; This is a real article.
10871         (save-window-excursion
10872           (let ((gnus-display-mime-function nil)
10873                 (gnus-article-prepare-hook nil))
10874             (gnus-summary-select-article t nil nil article)))
10875         (save-excursion
10876           (set-buffer save-buffer)
10877           (erase-buffer)
10878           (insert-buffer-substring gnus-original-article-buffer))
10879         (setq file (gnus-article-save save-buffer file num))
10880         (gnus-summary-remove-process-mark article)
10881         (unless not-saved
10882           (gnus-summary-set-saved-mark article))))
10883     (gnus-kill-buffer save-buffer)
10884     (gnus-summary-position-point)
10885     (gnus-set-mode-line 'summary)
10886     n))
10887
10888 (defun gnus-summary-pipe-output (&optional arg headers)
10889   "Pipe the current article to a subprocess.
10890 If N is a positive number, pipe the N next articles.
10891 If N is a negative number, pipe the N previous articles.
10892 If N is nil and any articles have been marked with the process mark,
10893 pipe those articles instead.
10894 If HEADERS (the symbolic prefix), include the headers, too."
10895   (interactive (gnus-interactive "P\ny"))
10896   (require 'gnus-art)
10897   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10898         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10899     (gnus-summary-save-article arg t))
10900   (let ((buffer (get-buffer "*Shell Command Output*")))
10901     (when (and buffer
10902                (not (zerop (buffer-size buffer))))
10903       (gnus-configure-windows 'pipe))))
10904
10905 (defun gnus-summary-save-article-mail (&optional arg)
10906   "Append the current article to an mail file.
10907 If N is a positive number, save the N next articles.
10908 If N is a negative number, save the N previous articles.
10909 If N is nil and any articles have been marked with the process mark,
10910 save those articles instead."
10911   (interactive "P")
10912   (require 'gnus-art)
10913   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10914     (gnus-summary-save-article arg)))
10915
10916 (defun gnus-summary-save-article-rmail (&optional arg)
10917   "Append the current article to an rmail file.
10918 If N is a positive number, save the N next articles.
10919 If N is a negative number, save the N previous articles.
10920 If N is nil and any articles have been marked with the process mark,
10921 save those articles instead."
10922   (interactive "P")
10923   (require 'gnus-art)
10924   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10925     (gnus-summary-save-article arg)))
10926
10927 (defun gnus-summary-save-article-file (&optional arg)
10928   "Append the current article to a file.
10929 If N is a positive number, save the N next articles.
10930 If N is a negative number, save the N previous articles.
10931 If N is nil and any articles have been marked with the process mark,
10932 save those articles instead."
10933   (interactive "P")
10934   (require 'gnus-art)
10935   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10936     (gnus-summary-save-article arg)))
10937
10938 (defun gnus-summary-write-article-file (&optional arg)
10939   "Write the current article to a file, deleting the previous file.
10940 If N is a positive number, save the N next articles.
10941 If N is a negative number, save the N previous articles.
10942 If N is nil and any articles have been marked with the process mark,
10943 save those articles instead."
10944   (interactive "P")
10945   (require 'gnus-art)
10946   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10947     (gnus-summary-save-article arg)))
10948
10949 (defun gnus-summary-save-article-body-file (&optional arg)
10950   "Append the current article body to a file.
10951 If N is a positive number, save the N next articles.
10952 If N is a negative number, save the N previous articles.
10953 If N is nil and any articles have been marked with the process mark,
10954 save those articles instead."
10955   (interactive "P")
10956   (require 'gnus-art)
10957   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10958     (gnus-summary-save-article arg)))
10959
10960 (defun gnus-summary-muttprint (&optional arg)
10961   "Print the current article using Muttprint.
10962 If N is a positive number, save the N next articles.
10963 If N is a negative number, save the N previous articles.
10964 If N is nil and any articles have been marked with the process mark,
10965 save those articles instead."
10966   (interactive "P")
10967   (require 'gnus-art)
10968   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10969     (gnus-summary-save-article arg t)))
10970
10971 (defun gnus-summary-pipe-message (program)
10972   "Pipe the current article through PROGRAM."
10973   (interactive "sProgram: ")
10974   (gnus-summary-select-article)
10975   (let ((mail-header-separator ""))
10976     (gnus-eval-in-buffer-window gnus-article-buffer
10977       (save-restriction
10978         (widen)
10979         (let ((start (window-start))
10980               buffer-read-only)
10981           (message-pipe-buffer-body program)
10982           (set-window-start (get-buffer-window (current-buffer)) start))))))
10983
10984 (defun gnus-get-split-value (methods)
10985   "Return a value based on the split METHODS."
10986   (let (split-name method result match)
10987     (when methods
10988       (save-excursion
10989         (set-buffer gnus-original-article-buffer)
10990         (save-restriction
10991           (nnheader-narrow-to-headers)
10992           (while (and methods (not split-name))
10993             (goto-char (point-min))
10994             (setq method (pop methods))
10995             (setq match (car method))
10996             (when (cond
10997                    ((stringp match)
10998                     ;; Regular expression.
10999                     (ignore-errors
11000                       (re-search-forward match nil t)))
11001                    ((functionp match)
11002                     ;; Function.
11003                     (save-restriction
11004                       (widen)
11005                       (setq result (funcall match gnus-newsgroup-name))))
11006                    ((consp match)
11007                     ;; Form.
11008                     (save-restriction
11009                       (widen)
11010                       (setq result (eval match)))))
11011               (setq split-name (cdr method))
11012               (cond ((stringp result)
11013                      (push (expand-file-name
11014                             result gnus-article-save-directory)
11015                            split-name))
11016                     ((consp result)
11017                      (setq split-name (append result split-name)))))))))
11018     (nreverse split-name)))
11019
11020 (defun gnus-valid-move-group-p (group)
11021   (and (boundp group)
11022        (symbol-name group)
11023        (symbol-value group)
11024        (gnus-get-function (gnus-find-method-for-group
11025                            (symbol-name group)) 'request-accept-article t)))
11026
11027 (defun gnus-read-move-group-name (prompt default articles prefix)
11028   "Read a group name."
11029   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11030          (minibuffer-confirm-incomplete nil) ; XEmacs
11031          (prom
11032           (format "%s %s to:"
11033                   prompt
11034                   (if (> (length articles) 1)
11035                       (format "these %d articles" (length articles))
11036                     "this article")))
11037          (to-newsgroup
11038           (cond
11039            ((null split-name)
11040             (gnus-completing-read-with-default
11041              default prom
11042              gnus-active-hashtb
11043              'gnus-valid-move-group-p
11044              nil prefix
11045              'gnus-group-history))
11046            ((= 1 (length split-name))
11047             (gnus-completing-read-with-default
11048              (car split-name) prom
11049              gnus-active-hashtb
11050              'gnus-valid-move-group-p
11051              nil nil
11052              'gnus-group-history))
11053            (t
11054             (gnus-completing-read-with-default
11055              nil prom
11056              (mapcar (lambda (el) (list el))
11057                      (nreverse split-name))
11058              nil nil nil
11059              'gnus-group-history))))
11060          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11061     (when to-newsgroup
11062       (if (or (string= to-newsgroup "")
11063               (string= to-newsgroup prefix))
11064           (setq to-newsgroup default))
11065       (unless to-newsgroup
11066         (error "No group name entered"))
11067       (or (gnus-active to-newsgroup)
11068           (gnus-activate-group to-newsgroup nil nil to-method)
11069           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11070                                      to-newsgroup))
11071               (or (and (gnus-request-create-group to-newsgroup to-method)
11072                        (gnus-activate-group
11073                         to-newsgroup nil nil to-method)
11074                        (gnus-subscribe-group to-newsgroup))
11075                   (error "Couldn't create group %s" to-newsgroup)))
11076           (error "No such group: %s" to-newsgroup)))
11077     to-newsgroup))
11078
11079 (defun gnus-summary-save-parts (type dir n &optional reverse)
11080   "Save parts matching TYPE to DIR.
11081 If REVERSE, save parts that do not match TYPE."
11082   (interactive
11083    (list (read-string "Save parts of type: "
11084                       (or (car gnus-summary-save-parts-type-history)
11085                           gnus-summary-save-parts-default-mime)
11086                       'gnus-summary-save-parts-type-history)
11087          (setq gnus-summary-save-parts-last-directory
11088                (read-file-name "Save to directory: "
11089                                gnus-summary-save-parts-last-directory
11090                                nil t))
11091          current-prefix-arg))
11092   (gnus-summary-iterate n
11093     (let ((gnus-display-mime-function nil)
11094           (gnus-inhibit-treatment t))
11095       (gnus-summary-select-article))
11096     (save-excursion
11097       (set-buffer gnus-article-buffer)
11098       (let ((handles (or gnus-article-mime-handles
11099                          (mm-dissect-buffer nil gnus-article-loose-mime)
11100                          (and gnus-article-emulate-mime
11101                               (mm-uu-dissect)))))
11102         (when handles
11103           (gnus-summary-save-parts-1 type dir handles reverse)
11104           (unless gnus-article-mime-handles ;; Don't destroy this case.
11105             (mm-destroy-parts handles)))))))
11106
11107 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11108   (if (stringp (car handle))
11109       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11110               (cdr handle))
11111     (when (if reverse
11112               (not (string-match type (mm-handle-media-type handle)))
11113             (string-match type (mm-handle-media-type handle)))
11114       (let ((file (expand-file-name
11115                    (gnus-map-function
11116                     mm-file-name-rewrite-functions
11117                     (file-name-nondirectory
11118                      (or
11119                       (mail-content-type-get
11120                        (mm-handle-disposition handle) 'filename)
11121                       (mail-content-type-get
11122                        (mm-handle-type handle) 'name)
11123                       (concat gnus-newsgroup-name
11124                               "." (number-to-string
11125                                    (cdr gnus-article-current))))))
11126                    dir)))
11127         (unless (file-exists-p file)
11128           (mm-save-part-to-file handle file))))))
11129
11130 ;; Summary extract commands
11131
11132 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11133   (let ((buffer-read-only nil)
11134         (article (gnus-summary-article-number))
11135         after-article b e)
11136     (unless (gnus-summary-goto-subject article)
11137       (error "No such article: %d" article))
11138     (gnus-summary-position-point)
11139     ;; If all commands are to be bunched up on one line, we collect
11140     ;; them here.
11141     (unless gnus-view-pseudos-separately
11142       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11143             files action)
11144         (while ps
11145           (setq action (cdr (assq 'action (car ps))))
11146           (setq files (list (cdr (assq 'name (car ps)))))
11147           (while (and ps (cdr ps)
11148                       (string= (or action "1")
11149                                (or (cdr (assq 'action (cadr ps))) "2")))
11150             (push (cdr (assq 'name (cadr ps))) files)
11151             (setcdr ps (cddr ps)))
11152           (when files
11153             (when (not (string-match "%s" action))
11154               (push " " files))
11155             (push " " files)
11156             (when (assq 'execute (car ps))
11157               (setcdr (assq 'execute (car ps))
11158                       (funcall (if (string-match "%s" action)
11159                                    'format 'concat)
11160                                action
11161                                (mapconcat
11162                                 (lambda (f)
11163                                   (if (equal f " ")
11164                                       f
11165                                     (shell-quote-argument f)))
11166                                 files " ")))))
11167           (setq ps (cdr ps)))))
11168     (if (and gnus-view-pseudos (not not-view))
11169         (while pslist
11170           (when (assq 'execute (car pslist))
11171             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11172                                   (eq gnus-view-pseudos 'not-confirm)))
11173           (setq pslist (cdr pslist)))
11174       (save-excursion
11175         (while pslist
11176           (setq after-article (or (cdr (assq 'article (car pslist)))
11177                                   (gnus-summary-article-number)))
11178           (gnus-summary-goto-subject after-article)
11179           (forward-line 1)
11180           (setq b (point))
11181           (insert "    " (file-name-nondirectory
11182                           (cdr (assq 'name (car pslist))))
11183                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11184           (setq e (point))
11185           (forward-line -1)             ; back to `b'
11186           (gnus-add-text-properties
11187            b (1- e) (list 'gnus-number gnus-reffed-article-number
11188                           gnus-mouse-face-prop gnus-mouse-face))
11189           (gnus-data-enter
11190            after-article gnus-reffed-article-number
11191            gnus-unread-mark b (car pslist) 0 (- e b))
11192           (setq gnus-newsgroup-unreads
11193                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11194                                          gnus-reffed-article-number))
11195           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11196           (setq pslist (cdr pslist)))))))
11197
11198 (defun gnus-pseudos< (p1 p2)
11199   (let ((c1 (cdr (assq 'action p1)))
11200         (c2 (cdr (assq 'action p2))))
11201     (and c1 c2 (string< c1 c2))))
11202
11203 (defun gnus-request-pseudo-article (props)
11204   (cond ((assq 'execute props)
11205          (gnus-execute-command (cdr (assq 'execute props)))))
11206   (let ((gnus-current-article (gnus-summary-article-number)))
11207     (gnus-run-hooks 'gnus-mark-article-hook)))
11208
11209 (defun gnus-execute-command (command &optional automatic)
11210   (save-excursion
11211     (gnus-article-setup-buffer)
11212     (set-buffer gnus-article-buffer)
11213     (setq buffer-read-only nil)
11214     (let ((command (if automatic command
11215                      (read-string "Command: " (cons command 0)))))
11216       (erase-buffer)
11217       (insert "$ " command "\n\n")
11218       (if gnus-view-pseudo-asynchronously
11219           (start-process "gnus-execute" (current-buffer) shell-file-name
11220                          shell-command-switch command)
11221         (call-process shell-file-name nil t nil
11222                       shell-command-switch command)))))
11223
11224 ;; Summary kill commands.
11225
11226 (defun gnus-summary-edit-global-kill (article)
11227   "Edit the \"global\" kill file."
11228   (interactive (list (gnus-summary-article-number)))
11229   (gnus-group-edit-global-kill article))
11230
11231 (defun gnus-summary-edit-local-kill ()
11232   "Edit a local kill file applied to the current newsgroup."
11233   (interactive)
11234   (setq gnus-current-headers (gnus-summary-article-header))
11235   (gnus-group-edit-local-kill
11236    (gnus-summary-article-number) gnus-newsgroup-name))
11237
11238 ;;; Header reading.
11239
11240 (defun gnus-read-header (id &optional header)
11241   "Read the headers of article ID and enter them into the Gnus system."
11242   (let ((group gnus-newsgroup-name)
11243         (gnus-override-method
11244          (or
11245           gnus-override-method
11246           (and (gnus-news-group-p gnus-newsgroup-name)
11247                (car (gnus-refer-article-methods)))))
11248         where)
11249     ;; First we check to see whether the header in question is already
11250     ;; fetched.
11251     (if (stringp id)
11252         ;; This is a Message-ID.
11253         (setq header (or header (gnus-id-to-header id)))
11254       ;; This is an article number.
11255       (setq header (or header (gnus-summary-article-header id))))
11256     (if (and header
11257              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11258         ;; We have found the header.
11259         header
11260       ;; We have to really fetch the header to this article.
11261       (save-excursion
11262         (set-buffer nntp-server-buffer)
11263         (when (setq where (gnus-request-head id group))
11264           (nnheader-fold-continuation-lines)
11265           (goto-char (point-max))
11266           (insert ".\n")
11267           (goto-char (point-min))
11268           (insert "211 ")
11269           (princ (cond
11270                   ((numberp id) id)
11271                   ((cdr where) (cdr where))
11272                   (header (mail-header-number header))
11273                   (t gnus-reffed-article-number))
11274                  (current-buffer))
11275           (insert " Article retrieved.\n"))
11276         (if (or (not where)
11277                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11278             ()                          ; Malformed head.
11279           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11280             (when (and (stringp id)
11281                        (not (string= (gnus-group-real-name group)
11282                                      (car where))))
11283               ;; If we fetched by Message-ID and the article came
11284               ;; from a different group, we fudge some bogus article
11285               ;; numbers for this article.
11286               (mail-header-set-number header gnus-reffed-article-number))
11287             (save-excursion
11288               (set-buffer gnus-summary-buffer)
11289               (decf gnus-reffed-article-number)
11290               (gnus-remove-header (mail-header-number header))
11291               (push header gnus-newsgroup-headers)
11292               (setq gnus-current-headers header)
11293               (push (mail-header-number header) gnus-newsgroup-limit)))
11294           header)))))
11295
11296 (defun gnus-remove-header (number)
11297   "Remove header NUMBER from `gnus-newsgroup-headers'."
11298   (if (and gnus-newsgroup-headers
11299            (= number (mail-header-number (car gnus-newsgroup-headers))))
11300       (pop gnus-newsgroup-headers)
11301     (let ((headers gnus-newsgroup-headers))
11302       (while (and (cdr headers)
11303                   (not (= number (mail-header-number (cadr headers)))))
11304         (pop headers))
11305       (when (cdr headers)
11306         (setcdr headers (cddr headers))))))
11307
11308 ;;;
11309 ;;; summary highlights
11310 ;;;
11311
11312 (defun gnus-highlight-selected-summary ()
11313   "Highlight selected article in summary buffer."
11314   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11315   (when gnus-summary-selected-face
11316     (save-excursion
11317       (let* ((beg (point-at-bol))
11318              (end (point-at-eol))
11319              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11320              (from (if (get-text-property beg gnus-mouse-face-prop)
11321                        beg
11322                      (or (next-single-property-change
11323                           beg gnus-mouse-face-prop nil end)
11324                          beg)))
11325              (to
11326               (if (= from end)
11327                   (- from 2)
11328                 (or (next-single-property-change
11329                      from gnus-mouse-face-prop nil end)
11330                     end))))
11331         ;; If no mouse-face prop on line we will have to = from = end,
11332         ;; so we highlight the entire line instead.
11333         (when (= (+ to 2) from)
11334           (setq from beg)
11335           (setq to end))
11336         (if gnus-newsgroup-selected-overlay
11337             ;; Move old overlay.
11338             (gnus-move-overlay
11339              gnus-newsgroup-selected-overlay from to (current-buffer))
11340           ;; Create new overlay.
11341           (gnus-overlay-put
11342            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11343            'face gnus-summary-selected-face))))))
11344
11345 (defvar gnus-summary-highlight-line-cached nil)
11346 (defvar gnus-summary-highlight-line-trigger nil)
11347
11348 (defun gnus-summary-highlight-line-0 ()
11349   (if (and (eq gnus-summary-highlight-line-trigger
11350                gnus-summary-highlight)
11351            gnus-summary-highlight-line-cached)
11352       gnus-summary-highlight-line-cached
11353     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11354           gnus-summary-highlight-line-cached
11355           (let* ((cond (list 'cond))
11356                  (c cond)
11357                  (list gnus-summary-highlight))
11358             (while list
11359               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11360                               nil))
11361               (setq c (cdr c)
11362                     list (cdr list)))
11363             (gnus-byte-compile (list 'lambda nil cond))))))
11364
11365 (defun gnus-summary-highlight-line ()
11366   "Highlight current line according to `gnus-summary-highlight'."
11367   (let* ((beg (point-at-bol))
11368          (article (or (gnus-summary-article-number) gnus-current-article))
11369          (score (or (cdr (assq article
11370                                gnus-newsgroup-scored))
11371                     gnus-summary-default-score 0))
11372          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11373          (inhibit-read-only t)
11374          (default gnus-summary-default-score)
11375          (default-high gnus-summary-default-high-score)
11376          (default-low gnus-summary-default-low-score)
11377          (uncached (and gnus-summary-use-undownloaded-faces
11378                         (memq article gnus-newsgroup-undownloaded)
11379                         (not (memq article gnus-newsgroup-cached)))))
11380     (let ((face (funcall (gnus-summary-highlight-line-0))))
11381       (unless (eq face (get-text-property beg 'face))
11382         (gnus-put-text-property-excluding-characters-with-faces
11383          beg (point-at-eol) 'face
11384          (setq face (if (boundp face) (symbol-value face) face)))
11385         (when gnus-summary-highlight-line-function
11386           (funcall gnus-summary-highlight-line-function article face))))))
11387
11388 (defun gnus-update-read-articles (group unread &optional compute)
11389   "Update the list of read articles in GROUP.
11390 UNREAD is a sorted list."
11391   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11392         (info (gnus-get-info group))
11393         (prev 1)
11394         read)
11395     (if (or (not info) (not active))
11396         ;; There is no info on this group if it was, in fact,
11397         ;; killed.  Gnus stores no information on killed groups, so
11398         ;; there's nothing to be done.
11399         ;; One could store the information somewhere temporarily,
11400         ;; perhaps...  Hmmm...
11401         ()
11402       ;; Remove any negative articles numbers.
11403       (while (and unread (< (car unread) 0))
11404         (setq unread (cdr unread)))
11405       ;; Remove any expired article numbers
11406       (while (and unread (< (car unread) (car active)))
11407         (setq unread (cdr unread)))
11408       ;; Compute the ranges of read articles by looking at the list of
11409       ;; unread articles.
11410       (while unread
11411         (when (/= (car unread) prev)
11412           (push (if (= prev (1- (car unread))) prev
11413                   (cons prev (1- (car unread))))
11414                 read))
11415         (setq prev (1+ (car unread)))
11416         (setq unread (cdr unread)))
11417       (when (<= prev (cdr active))
11418         (push (cons prev (cdr active)) read))
11419       (setq read (if (> (length read) 1) (nreverse read) read))
11420       (if compute
11421           read
11422         (save-excursion
11423           (let (setmarkundo)
11424             ;; Propagate the read marks to the backend.
11425             (when (gnus-check-backend-function 'request-set-mark group)
11426               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11427                     (add (gnus-remove-from-range read (gnus-info-read info))))
11428                 (when (or add del)
11429                   (unless (gnus-check-group group)
11430                     (error "Can't open server for %s" group))
11431                   (gnus-request-set-mark
11432                    group (delq nil (list (if add (list add 'add '(read)))
11433                                          (if del (list del 'del '(read))))))
11434                   (setq setmarkundo
11435                         `(gnus-request-set-mark
11436                           ,group
11437                           ',(delq nil (list
11438                                        (if del (list del 'add '(read)))
11439                                        (if add (list add 'del '(read))))))))))
11440             (set-buffer gnus-group-buffer)
11441             (gnus-undo-register
11442               `(progn
11443                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11444                  (gnus-info-set-read ',info ',(gnus-info-read info))
11445                  (gnus-get-unread-articles-in-group ',info
11446                                                     (gnus-active ,group))
11447                  (gnus-group-update-group ,group t)
11448                  ,setmarkundo))))
11449         ;; Enter this list into the group info.
11450         (gnus-info-set-read info read)
11451         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11452         (gnus-get-unread-articles-in-group info (gnus-active group))
11453         t))))
11454
11455 (defun gnus-offer-save-summaries ()
11456   "Offer to save all active summary buffers."
11457   (let (buffers)
11458     ;; Go through all buffers and find all summaries.
11459     (dolist (buffer (buffer-list))
11460       (when (and (setq buffer (buffer-name buffer))
11461                  (string-match "Summary" buffer)
11462                  (save-excursion
11463                    (set-buffer buffer)
11464                    ;; We check that this is, indeed, a summary buffer.
11465                    (and (eq major-mode 'gnus-summary-mode)
11466                         ;; Also make sure this isn't bogus.
11467                         gnus-newsgroup-prepared
11468                         ;; Also make sure that this isn't a
11469                         ;; dead summary buffer.
11470                         (not gnus-dead-summary-mode))))
11471         (push buffer buffers)))
11472     ;; Go through all these summary buffers and offer to save them.
11473     (when buffers
11474       (save-excursion
11475         (map-y-or-n-p
11476          "Update summary buffer %s? "
11477          (lambda (buf)
11478            (switch-to-buffer buf)
11479            (gnus-summary-exit))
11480          buffers)))))
11481
11482 (defun gnus-summary-setup-default-charset ()
11483   "Setup newsgroup default charset."
11484   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11485       (setq gnus-newsgroup-charset nil)
11486     (let* ((ignored-charsets
11487             (or gnus-newsgroup-ephemeral-ignored-charsets
11488                 (append
11489                  (and gnus-newsgroup-name
11490                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11491                  gnus-newsgroup-ignored-charsets))))
11492       (setq gnus-newsgroup-charset
11493             (or gnus-newsgroup-ephemeral-charset
11494                 (and gnus-newsgroup-name
11495                      (gnus-parameter-charset gnus-newsgroup-name))
11496                 gnus-default-charset))
11497       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11498            ignored-charsets))))
11499
11500 ;;;
11501 ;;; Mime Commands
11502 ;;;
11503
11504 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11505   "Display the current article buffer fully MIME-buttonized.
11506 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11507 treated as multipart/mixed."
11508   (interactive "P")
11509   (require 'gnus-art)
11510   (let ((gnus-unbuttonized-mime-types nil)
11511         (gnus-mime-display-multipart-as-mixed show-all-parts))
11512     (gnus-summary-show-article)))
11513
11514 (defun gnus-summary-repair-multipart (article)
11515   "Add a Content-Type header to a multipart article without one."
11516   (interactive (list (gnus-summary-article-number)))
11517   (gnus-with-article article
11518     (message-narrow-to-head)
11519     (message-remove-header "Mime-Version")
11520     (goto-char (point-max))
11521     (insert "Mime-Version: 1.0\n")
11522     (widen)
11523     (when (search-forward "\n--" nil t)
11524       (let ((separator (buffer-substring (point) (point-at-eol))))
11525         (message-narrow-to-head)
11526         (message-remove-header "Content-Type")
11527         (goto-char (point-max))
11528         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11529                         separator))
11530         (widen))))
11531   (let (gnus-mark-article-hook)
11532     (gnus-summary-select-article t t nil article)))
11533
11534 (defun gnus-summary-toggle-display-buttonized ()
11535   "Toggle the buttonizing of the article buffer."
11536   (interactive)
11537   (require 'gnus-art)
11538   (if (setq gnus-inhibit-mime-unbuttonizing
11539             (not gnus-inhibit-mime-unbuttonizing))
11540       (let ((gnus-unbuttonized-mime-types nil))
11541         (gnus-summary-show-article))
11542     (gnus-summary-show-article)))
11543
11544 ;;;
11545 ;;; Generic summary marking commands
11546 ;;;
11547
11548 (defvar gnus-summary-marking-alist
11549   '((read gnus-del-mark "d")
11550     (unread gnus-unread-mark "u")
11551     (ticked gnus-ticked-mark "!")
11552     (dormant gnus-dormant-mark "?")
11553     (expirable gnus-expirable-mark "e"))
11554   "An alist of names/marks/keystrokes.")
11555
11556 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11557 (defvar gnus-summary-mark-map)
11558
11559 (defun gnus-summary-make-all-marking-commands ()
11560   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11561   (dolist (elem gnus-summary-marking-alist)
11562     (apply 'gnus-summary-make-marking-command elem)))
11563
11564 (defun gnus-summary-make-marking-command (name mark keystroke)
11565   (let ((map (make-sparse-keymap)))
11566     (define-key gnus-summary-generic-mark-map keystroke map)
11567     (dolist (lway `((next "next" next nil "n")
11568                     (next-unread "next unread" next t "N")
11569                     (prev "previous" prev nil "p")
11570                     (prev-unread "previous unread" prev t "P")
11571                     (nomove "" nil nil ,keystroke)))
11572       (let ((func (gnus-summary-make-marking-command-1
11573                    mark (car lway) lway name)))
11574         (setq func (eval func))
11575         (define-key map (nth 4 lway) func)))))
11576
11577 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11578   `(defun ,(intern
11579             (format "gnus-summary-put-mark-as-%s%s"
11580                     name (if (eq way 'nomove)
11581                              ""
11582                            (concat "-" (symbol-name way)))))
11583      (n)
11584      ,(format
11585        "Mark the current article as %s%s.
11586 If N, the prefix, then repeat N times.
11587 If N is negative, move in reverse order.
11588 The difference between N and the actual number of articles marked is
11589 returned."
11590        name (cadr lway))
11591      (interactive "p")
11592      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11593
11594 (defun gnus-summary-generic-mark (n mark move unread)
11595   "Mark N articles with MARK."
11596   (unless (eq major-mode 'gnus-summary-mode)
11597     (error "This command can only be used in the summary buffer"))
11598   (gnus-summary-show-thread)
11599   (let ((nummove
11600          (cond
11601           ((eq move 'next) 1)
11602           ((eq move 'prev) -1)
11603           (t 0))))
11604     (if (zerop nummove)
11605         (setq n 1)
11606       (when (< n 0)
11607         (setq n (abs n)
11608               nummove (* -1 nummove))))
11609     (while (and (> n 0)
11610                 (gnus-summary-mark-article nil mark)
11611                 (zerop (gnus-summary-next-subject nummove unread t)))
11612       (setq n (1- n)))
11613     (when (/= 0 n)
11614       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11615     (gnus-summary-recenter)
11616     (gnus-summary-position-point)
11617     (gnus-set-mode-line 'summary)
11618     n))
11619
11620 (defun gnus-summary-insert-articles (articles)
11621   (when (setq articles
11622               (gnus-sorted-difference articles
11623                                       (mapcar (lambda (h)
11624                                                 (mail-header-number h))
11625                                               gnus-newsgroup-headers)))
11626     (setq gnus-newsgroup-headers
11627           (gnus-merge 'list
11628                       gnus-newsgroup-headers
11629                       (gnus-fetch-headers articles)
11630                       'gnus-article-sort-by-number))
11631     ;; Suppress duplicates?
11632     (when gnus-suppress-duplicates
11633       (gnus-dup-suppress-articles))
11634
11635     ;; We might want to build some more threads first.
11636     (when (and gnus-fetch-old-headers
11637                (eq gnus-headers-retrieved-by 'nov))
11638       (if (eq gnus-fetch-old-headers 'invisible)
11639           (gnus-build-all-threads)
11640         (gnus-build-old-threads)))
11641     ;; Let the Gnus agent mark articles as read.
11642     (when gnus-agent
11643       (gnus-agent-get-undownloaded-list))
11644     ;; Remove list identifiers from subject
11645     (when gnus-list-identifiers
11646       (gnus-summary-remove-list-identifiers))
11647     ;; First and last article in this newsgroup.
11648     (when gnus-newsgroup-headers
11649       (setq gnus-newsgroup-begin
11650             (mail-header-number (car gnus-newsgroup-headers))
11651             gnus-newsgroup-end
11652             (mail-header-number
11653              (gnus-last-element gnus-newsgroup-headers))))
11654     (when gnus-use-scoring
11655       (gnus-possibly-score-headers))))
11656
11657 (defun gnus-summary-insert-old-articles (&optional all)
11658   "Insert all old articles in this group.
11659 If ALL is non-nil, already read articles become readable.
11660 If ALL is a number, fetch this number of articles."
11661   (interactive "P")
11662   (prog1
11663       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11664             older len)
11665         (setq older
11666               ;; Some nntp servers lie about their active range.  When
11667               ;; this happens, the active range can be in the millions.
11668               ;; Use a compressed range to avoid creating a huge list.
11669               (gnus-range-difference (list gnus-newsgroup-active) old))
11670         (setq len (gnus-range-length older))
11671         (cond
11672          ((null older) nil)
11673          ((numberp all)
11674           (if (< all len)
11675               (let ((older-range (nreverse older)))
11676                 (setq older nil)
11677
11678                 (while (> all 0)
11679                   (let* ((r (pop older-range))
11680                          (min (if (numberp r) r (car r)))
11681                          (max (if (numberp r) r (cdr r))))
11682                     (while (and (<= min max)
11683                                 (> all 0))
11684                       (push max older)
11685                       (setq all (1- all)
11686                             max (1- max))))))
11687             (setq older (gnus-uncompress-range older))))
11688          (all
11689           (setq older (gnus-uncompress-range older)))
11690          (t
11691           (when (and (numberp gnus-large-newsgroup)
11692                    (> len gnus-large-newsgroup))
11693               (let* ((cursor-in-echo-area nil)
11694                      (initial (gnus-parameter-large-newsgroup-initial
11695                                gnus-newsgroup-name))
11696                      (input
11697                       (read-string
11698                        (format
11699                         "How many articles from %s (%s %d): "
11700                         (gnus-limit-string
11701                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11702                         (if initial "max" "default")
11703                         len)
11704                        (if initial
11705                            (cons (number-to-string initial)
11706                                  0)))))
11707                 (unless (string-match "^[ \t]*$" input)
11708                   (setq all (string-to-number input))
11709                   (if (< all len)
11710                       (let ((older-range (nreverse older)))
11711                         (setq older nil)
11712
11713                         (while (> all 0)
11714                           (let* ((r (pop older-range))
11715                                  (min (if (numberp r) r (car r)))
11716                                  (max (if (numberp r) r (cdr r))))
11717                             (while (and (<= min max)
11718                                         (> all 0))
11719                               (push max older)
11720                               (setq all (1- all)
11721                                     max (1- max))))))))))
11722           (setq older (gnus-uncompress-range older))))
11723         (if (not older)
11724             (message "No old news.")
11725           (gnus-summary-insert-articles older)
11726           (gnus-summary-limit (gnus-sorted-nunion old older))))
11727     (gnus-summary-position-point)))
11728
11729 (defun gnus-summary-insert-new-articles ()
11730   "Insert all new articles in this group."
11731   (interactive)
11732   (prog1
11733       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11734             (old-active gnus-newsgroup-active)
11735             (nnmail-fetched-sources (list t))
11736             i new)
11737         (setq gnus-newsgroup-active
11738               (gnus-activate-group gnus-newsgroup-name 'scan))
11739         (setq i (cdr gnus-newsgroup-active))
11740         (while (> i (cdr old-active))
11741           (push i new)
11742           (decf i))
11743         (if (not new)
11744             (message "No gnus is bad news")
11745           (gnus-summary-insert-articles new)
11746           (setq gnus-newsgroup-unreads
11747                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11748           (gnus-summary-limit (gnus-sorted-nunion old new))))
11749     (gnus-summary-position-point)))
11750
11751 (gnus-summary-make-all-marking-commands)
11752
11753 (gnus-ems-redefine)
11754
11755 (provide 'gnus-sum)
11756
11757 (run-hooks 'gnus-sum-load-hook)
11758
11759 ;; Local Variables:
11760 ;; coding: iso-8859-1
11761 ;; End:
11762
11763 ;;; gnus-sum.el ends here