Typo.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary 2
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defun gnus-widget-reversible-match (widget value)
683   "Ignoring WIDGET, convert VALUE to internal form.
684 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
685   ;; (debug value)
686   (or (symbolp value)
687       (and (listp value)
688            (eq (length value) 2)
689            (eq (nth 0 value) 'not)
690            (symbolp (nth 1 value)))))
691
692 (defun gnus-widget-reversible-to-internal (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
695 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
696   ;; (debug value)
697   (if (atom value)
698       (list value nil)
699     (list (nth 1 value) t)))
700
701 (defun gnus-widget-reversible-to-external (widget value)
702   "Ignoring WIDGET, convert VALUE to external form.
703 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
704 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
705   ;; (debug value)
706   (if (nth 1 value)
707       (list 'not (nth 0 value))
708     (nth 0 value)))
709
710 (define-widget 'gnus-widget-reversible 'group
711   "A `group' that convert values."
712   :match 'gnus-widget-reversible-match
713   :value-to-internal 'gnus-widget-reversible-to-internal
714   :value-to-external 'gnus-widget-reversible-to-external)
715                         
716
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 t
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 If non-nil (the default), Gnus will treat some articles as MIME
1145 even if the MIME-Version header is missing."
1146   :version "21.3"
1147   :type 'boolean
1148   :group 'gnus-article-mime)
1149
1150 (defcustom gnus-article-emulate-mime t
1151   "If non-nil, use MIME emulation for uuencode and the like.
1152 This means that Gnus will search message bodies for text that look
1153 like uuencoded bits, yEncoded bits, and so on, and present that using
1154 the normal Gnus MIME machinery."
1155   :type 'boolean
1156   :group 'gnus-article-mime)
1157
1158 ;;; Internal variables
1159
1160 (defvar gnus-summary-display-cache nil)
1161 (defvar gnus-article-mime-handles nil)
1162 (defvar gnus-article-decoded-p nil)
1163 (defvar gnus-article-charset nil)
1164 (defvar gnus-article-ignored-charsets nil)
1165 (defvar gnus-scores-exclude-files nil)
1166 (defvar gnus-page-broken nil)
1167
1168 (defvar gnus-original-article nil)
1169 (defvar gnus-article-internal-prepare-hook nil)
1170 (defvar gnus-newsgroup-process-stack nil)
1171
1172 (defvar gnus-thread-indent-array nil)
1173 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1174 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1175   "Function called to sort the articles within a thread after it has been gathered together.")
1176
1177 (defvar gnus-summary-save-parts-type-history nil)
1178 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1179
1180 ;; Avoid highlighting in kill files.
1181 (defvar gnus-summary-inhibit-highlight nil)
1182 (defvar gnus-newsgroup-selected-overlay nil)
1183 (defvar gnus-inhibit-limiting nil)
1184 (defvar gnus-newsgroup-adaptive-score-file nil)
1185 (defvar gnus-current-score-file nil)
1186 (defvar gnus-current-move-group nil)
1187 (defvar gnus-current-copy-group nil)
1188 (defvar gnus-current-crosspost-group nil)
1189 (defvar gnus-newsgroup-display nil)
1190
1191 (defvar gnus-newsgroup-dependencies nil)
1192 (defvar gnus-newsgroup-adaptive nil)
1193 (defvar gnus-summary-display-article-function nil)
1194 (defvar gnus-summary-highlight-line-function nil
1195   "Function called after highlighting a summary line.")
1196
1197 (defvar gnus-summary-line-format-alist
1198   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1199     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1200     (?s gnus-tmp-subject-or-nil ?s)
1201     (?n gnus-tmp-name ?s)
1202     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1203         ?s)
1204     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1205             gnus-tmp-from) ?s)
1206     (?F gnus-tmp-from ?s)
1207     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1208     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1209     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1210     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1211     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1212     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1213     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1214     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1215     (?L gnus-tmp-lines ?s)
1216     (?O gnus-tmp-downloaded ?c)
1217     (?I gnus-tmp-indentation ?s)
1218     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1219     (?R gnus-tmp-replied ?c)
1220     (?\[ gnus-tmp-opening-bracket ?c)
1221     (?\] gnus-tmp-closing-bracket ?c)
1222     (?\> (make-string gnus-tmp-level ? ) ?s)
1223     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1224     (?i gnus-tmp-score ?d)
1225     (?z gnus-tmp-score-char ?c)
1226     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1227     (?U gnus-tmp-unread ?c)
1228     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1229         ?s)
1230     (?t (gnus-summary-number-of-articles-in-thread
1231          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1232         ?d)
1233     (?e (gnus-summary-number-of-articles-in-thread
1234          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1235         ?c)
1236     (?u gnus-tmp-user-defined ?s)
1237     (?P (gnus-pick-line-number) ?d)
1238     (?B gnus-tmp-thread-tree-header-string ?s)
1239     (user-date (gnus-user-date
1240                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1241   "An alist of format specifications that can appear in summary lines.
1242 These are paired with what variables they correspond with, along with
1243 the type of the variable (string, integer, character, etc).")
1244
1245 (defvar gnus-summary-dummy-line-format-alist
1246   `((?S gnus-tmp-subject ?s)
1247     (?N gnus-tmp-number ?d)
1248     (?u gnus-tmp-user-defined ?s)))
1249
1250 (defvar gnus-summary-mode-line-format-alist
1251   `((?G gnus-tmp-group-name ?s)
1252     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1253     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1254     (?A gnus-tmp-article-number ?d)
1255     (?Z gnus-tmp-unread-and-unselected ?s)
1256     (?V gnus-version ?s)
1257     (?U gnus-tmp-unread-and-unticked ?d)
1258     (?S gnus-tmp-subject ?s)
1259     (?e gnus-tmp-unselected ?d)
1260     (?u gnus-tmp-user-defined ?s)
1261     (?d (length gnus-newsgroup-dormant) ?d)
1262     (?t (length gnus-newsgroup-marked) ?d)
1263     (?h (length gnus-newsgroup-spam-marked) ?d)
1264     (?r (length gnus-newsgroup-reads) ?d)
1265     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1266     (?E gnus-newsgroup-expunged-tally ?d)
1267     (?s (gnus-current-score-file-nondirectory) ?s)))
1268
1269 (defvar gnus-last-search-regexp nil
1270   "Default regexp for article search command.")
1271
1272 (defvar gnus-last-shell-command nil
1273   "Default shell command on article.")
1274
1275 (defvar gnus-newsgroup-agentized nil
1276   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1277 (defvar gnus-newsgroup-begin nil)
1278 (defvar gnus-newsgroup-end nil)
1279 (defvar gnus-newsgroup-last-rmail nil)
1280 (defvar gnus-newsgroup-last-mail nil)
1281 (defvar gnus-newsgroup-last-folder nil)
1282 (defvar gnus-newsgroup-last-file nil)
1283 (defvar gnus-newsgroup-auto-expire nil)
1284 (defvar gnus-newsgroup-active nil)
1285
1286 (defvar gnus-newsgroup-data nil)
1287 (defvar gnus-newsgroup-data-reverse nil)
1288 (defvar gnus-newsgroup-limit nil)
1289 (defvar gnus-newsgroup-limits nil)
1290 (defvar gnus-summary-use-undownloaded-faces nil)
1291
1292 (defvar gnus-newsgroup-unreads nil
1293   "Sorted list of unread articles in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-unselected nil
1296   "Sorted list of unselected unread articles in the current newsgroup.")
1297
1298 (defvar gnus-newsgroup-reads nil
1299   "Alist of read articles and article marks in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-expunged-tally nil)
1302
1303 (defvar gnus-newsgroup-marked nil
1304   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1305
1306 (defvar gnus-newsgroup-spam-marked nil
1307   "List of ranges of articles that have been marked as spam.")
1308
1309 (defvar gnus-newsgroup-killed nil
1310   "List of ranges of articles that have been through the scoring process.")
1311
1312 (defvar gnus-newsgroup-cached nil
1313   "Sorted list of articles that come from the article cache.")
1314
1315 (defvar gnus-newsgroup-saved nil
1316   "List of articles that have been saved.")
1317
1318 (defvar gnus-newsgroup-kill-headers nil)
1319
1320 (defvar gnus-newsgroup-replied nil
1321   "List of articles that have been replied to in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-forwarded nil
1324   "List of articles that have been forwarded in the current newsgroup.")
1325
1326 (defvar gnus-newsgroup-recent nil
1327   "List of articles that have are recent in the current newsgroup.")
1328
1329 (defvar gnus-newsgroup-expirable nil
1330   "Sorted list of articles in the current newsgroup that can be expired.")
1331
1332 (defvar gnus-newsgroup-processable nil
1333   "List of articles in the current newsgroup that can be processed.")
1334
1335 (defvar gnus-newsgroup-downloadable nil
1336   "Sorted list of articles in the current newsgroup that can be processed.")
1337
1338 (defvar gnus-newsgroup-unfetched nil
1339   "Sorted list of articles in the current newsgroup whose headers have
1340 not been fetched into the agent.
1341
1342 This list will always be a subset of gnus-newsgroup-undownloaded.")
1343
1344 (defvar gnus-newsgroup-undownloaded nil
1345   "List of articles in the current newsgroup that haven't been downloaded.")
1346
1347 (defvar gnus-newsgroup-unsendable nil
1348   "List of articles in the current newsgroup that won't be sent.")
1349
1350 (defvar gnus-newsgroup-bookmarks nil
1351   "List of articles in the current newsgroup that have bookmarks.")
1352
1353 (defvar gnus-newsgroup-dormant nil
1354   "Sorted list of dormant articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-unseen nil
1357   "List of unseen articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-seen nil
1360   "Range of seen articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-articles nil
1363   "List of articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-scored nil
1366   "List of scored articles in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-headers nil
1369   "List of article headers in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-threads nil)
1372
1373 (defvar gnus-newsgroup-prepared nil
1374   "Whether the current group has been prepared properly.")
1375
1376 (defvar gnus-newsgroup-ancient nil
1377   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-sparse nil)
1380
1381 (defvar gnus-current-article nil)
1382 (defvar gnus-article-current nil)
1383 (defvar gnus-current-headers nil)
1384 (defvar gnus-have-all-headers nil)
1385 (defvar gnus-last-article nil)
1386 (defvar gnus-newsgroup-history nil)
1387 (defvar gnus-newsgroup-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-charset nil)
1389 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1390
1391 (defvar gnus-article-before-search nil)
1392
1393 (defvar gnus-summary-local-variables
1394   '(gnus-newsgroup-name
1395     gnus-newsgroup-begin gnus-newsgroup-end
1396     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1397     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1398     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1399     gnus-newsgroup-unselected gnus-newsgroup-marked
1400     gnus-newsgroup-spam-marked
1401     gnus-newsgroup-reads gnus-newsgroup-saved
1402     gnus-newsgroup-replied gnus-newsgroup-forwarded
1403     gnus-newsgroup-recent
1404     gnus-newsgroup-expirable
1405     gnus-newsgroup-processable gnus-newsgroup-killed
1406     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1407     gnus-newsgroup-unfetched
1408     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1409     gnus-newsgroup-seen gnus-newsgroup-articles
1410     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1411     gnus-newsgroup-headers gnus-newsgroup-threads
1412     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1413     gnus-current-article gnus-current-headers gnus-have-all-headers
1414     gnus-last-article gnus-article-internal-prepare-hook
1415     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1416     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1417     gnus-thread-expunge-below
1418     gnus-score-alist gnus-current-score-file
1419     (gnus-summary-expunge-below . global)
1420     (gnus-summary-mark-below . global)
1421     (gnus-orphan-score . global)
1422     gnus-newsgroup-active gnus-scores-exclude-files
1423     gnus-newsgroup-history gnus-newsgroup-ancient
1424     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1425     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1426     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1427     (gnus-newsgroup-expunged-tally . 0)
1428     gnus-cache-removable-articles gnus-newsgroup-cached
1429     gnus-newsgroup-data gnus-newsgroup-data-reverse
1430     gnus-newsgroup-limit gnus-newsgroup-limits
1431     gnus-newsgroup-charset gnus-newsgroup-display
1432     gnus-summary-use-undownloaded-faces)
1433   "Variables that are buffer-local to the summary buffers.")
1434
1435 (defvar gnus-newsgroup-variables nil
1436   "A list of variables that have separate values in different newsgroups.
1437 A list of newsgroup (summary buffer) local variables, or cons of
1438 variables and their default expressions to be evalled (when the default
1439 values are not nil), that should be made global while the summary buffer
1440 is active.
1441
1442 Note: The default expressions will be evaluated (using function `eval')
1443 before assignment to the local variable rather than just assigned to it.
1444 If the default expression is the symbol `global', that symbol will not
1445 be evaluated but the global value of the local variable will be used
1446 instead.
1447
1448 These variables can be used to set variables in the group parameters
1449 while still allowing them to affect operations done in other buffers.
1450 For example:
1451
1452 \(setq gnus-newsgroup-variables
1453      '(message-use-followup-to
1454        (gnus-visible-headers .
1455          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1456 ")
1457
1458 ;; Byte-compiler warning.
1459 (eval-when-compile
1460   ;; Bind features so that require will believe that gnus-sum has
1461   ;; already been loaded (avoids infinite recursion)
1462   (let ((features (cons 'gnus-sum features)))
1463     ;; Several of the declarations in gnus-sum are needed to load the
1464     ;; following files. Right now, these definitions have been
1465     ;; compiled but not defined (evaluated).  We could either do a
1466     ;; eval-and-compile about all of the declarations or evaluate the
1467     ;; source file.
1468     (if (boundp 'gnus-newsgroup-variables)
1469         nil
1470       (load "gnus-sum.el" t t t))
1471     (require 'gnus)
1472     (require 'gnus-agent)
1473     (require 'gnus-art)))
1474
1475 ;; MIME stuff.
1476
1477 (defvar gnus-decode-encoded-word-methods
1478   '(mail-decode-encoded-word-string)
1479   "List of methods used to decode encoded words.
1480
1481 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1482 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1483 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1484 whose names match REGEXP.
1485
1486 For example:
1487 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1488  mail-decode-encoded-word-string
1489  (\"chinese\" . rfc1843-decode-string))")
1490
1491 (defvar gnus-decode-encoded-word-methods-cache nil)
1492
1493 (defun gnus-multi-decode-encoded-word-string (string)
1494   "Apply the functions from `gnus-encoded-word-methods' that match."
1495   (unless (and gnus-decode-encoded-word-methods-cache
1496                (eq gnus-newsgroup-name
1497                    (car gnus-decode-encoded-word-methods-cache)))
1498     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1499     (mapcar (lambda (x)
1500               (if (symbolp x)
1501                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1502                 (if (and gnus-newsgroup-name
1503                          (string-match (car x) gnus-newsgroup-name))
1504                     (nconc gnus-decode-encoded-word-methods-cache
1505                            (list (cdr x))))))
1506             gnus-decode-encoded-word-methods))
1507   (let ((xlist gnus-decode-encoded-word-methods-cache))
1508     (pop xlist)
1509     (while xlist
1510       (setq string (funcall (pop xlist) string))))
1511   string)
1512
1513 ;; Subject simplification.
1514
1515 (defun gnus-simplify-whitespace (str)
1516   "Remove excessive whitespace from STR."
1517   ;; Multiple spaces.
1518   (while (string-match "[ \t][ \t]+" str)
1519     (setq str (concat (substring str 0 (match-beginning 0))
1520                         " "
1521                         (substring str (match-end 0)))))
1522   ;; Leading spaces.
1523   (when (string-match "^[ \t]+" str)
1524     (setq str (substring str (match-end 0))))
1525   ;; Trailing spaces.
1526   (when (string-match "[ \t]+$" str)
1527     (setq str (substring str 0 (match-beginning 0))))
1528   str)
1529
1530 (defun gnus-simplify-all-whitespace (str)
1531   "Remove all whitespace from STR."
1532   (while (string-match "[ \t\n]+" str)
1533     (setq str (replace-match "" nil nil str)))
1534   str)
1535
1536 (defsubst gnus-simplify-subject-re (subject)
1537   "Remove \"Re:\" from subject lines."
1538   (if (string-match message-subject-re-regexp subject)
1539       (substring subject (match-end 0))
1540     subject))
1541
1542 (defun gnus-simplify-subject (subject &optional re-only)
1543   "Remove `Re:' and words in parentheses.
1544 If RE-ONLY is non-nil, strip leading `Re:'s only."
1545   (let ((case-fold-search t))           ;Ignore case.
1546     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1547     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1548       (setq subject (substring subject (match-end 0))))
1549     ;; Remove uninteresting prefixes.
1550     (when (and (not re-only)
1551                gnus-simplify-ignored-prefixes
1552                (string-match gnus-simplify-ignored-prefixes subject))
1553       (setq subject (substring subject (match-end 0))))
1554     ;; Remove words in parentheses from end.
1555     (unless re-only
1556       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1557         (setq subject (substring subject 0 (match-beginning 0)))))
1558     ;; Return subject string.
1559     subject))
1560
1561 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1562 ;; all whitespace.
1563 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1564   (goto-char (point-min))
1565   (while (re-search-forward regexp nil t)
1566     (replace-match (or newtext ""))))
1567
1568 (defun gnus-simplify-buffer-fuzzy ()
1569   "Simplify string in the buffer fuzzily.
1570 The string in the accessible portion of the current buffer is simplified.
1571 It is assumed to be a single-line subject.
1572 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1573 matter is removed.  Additional things can be deleted by setting
1574 `gnus-simplify-subject-fuzzy-regexp'."
1575   (let ((case-fold-search t)
1576         (modified-tick))
1577     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1578
1579     (while (not (eq modified-tick (buffer-modified-tick)))
1580       (setq modified-tick (buffer-modified-tick))
1581       (cond
1582        ((listp gnus-simplify-subject-fuzzy-regexp)
1583         (mapcar 'gnus-simplify-buffer-fuzzy-step
1584                 gnus-simplify-subject-fuzzy-regexp))
1585        (gnus-simplify-subject-fuzzy-regexp
1586         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1587       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1588       (gnus-simplify-buffer-fuzzy-step
1589        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1590       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1591
1592     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1593     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1594     (gnus-simplify-buffer-fuzzy-step " $")
1595     (gnus-simplify-buffer-fuzzy-step "^ +")))
1596
1597 (defun gnus-simplify-subject-fuzzy (subject)
1598   "Simplify a subject string fuzzily.
1599 See `gnus-simplify-buffer-fuzzy' for details."
1600   (save-excursion
1601     (gnus-set-work-buffer)
1602     (let ((case-fold-search t))
1603       ;; Remove uninteresting prefixes.
1604       (when (and gnus-simplify-ignored-prefixes
1605                  (string-match gnus-simplify-ignored-prefixes subject))
1606         (setq subject (substring subject (match-end 0))))
1607       (insert subject)
1608       (inline (gnus-simplify-buffer-fuzzy))
1609       (buffer-string))))
1610
1611 (defsubst gnus-simplify-subject-fully (subject)
1612   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1613   (cond
1614    (gnus-simplify-subject-functions
1615     (gnus-map-function gnus-simplify-subject-functions subject))
1616    ((null gnus-summary-gather-subject-limit)
1617     (gnus-simplify-subject-re subject))
1618    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1619     (gnus-simplify-subject-fuzzy subject))
1620    ((numberp gnus-summary-gather-subject-limit)
1621     (gnus-limit-string (gnus-simplify-subject-re subject)
1622                        gnus-summary-gather-subject-limit))
1623    (t
1624     subject)))
1625
1626 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1627   "Check whether two subjects are equal.
1628 If optional argument SIMPLE-FIRST is t, first argument is already
1629 simplified."
1630   (cond
1631    ((null simple-first)
1632     (equal (gnus-simplify-subject-fully s1)
1633            (gnus-simplify-subject-fully s2)))
1634    (t
1635     (equal s1
1636            (gnus-simplify-subject-fully s2)))))
1637
1638 (defun gnus-summary-bubble-group ()
1639   "Increase the score of the current group.
1640 This is a handy function to add to `gnus-summary-exit-hook' to
1641 increase the score of each group you read."
1642   (gnus-group-add-score gnus-newsgroup-name))
1643
1644 \f
1645 ;;;
1646 ;;; Gnus summary mode
1647 ;;;
1648
1649 (put 'gnus-summary-mode 'mode-class 'special)
1650
1651 (defvar gnus-article-commands-menu)
1652
1653 ;; Non-orthogonal keys
1654
1655 (gnus-define-keys gnus-summary-mode-map
1656   " " gnus-summary-next-page
1657   "\177" gnus-summary-prev-page
1658   [delete] gnus-summary-prev-page
1659   [backspace] gnus-summary-prev-page
1660   "\r" gnus-summary-scroll-up
1661   "\M-\r" gnus-summary-scroll-down
1662   "n" gnus-summary-next-unread-article
1663   "p" gnus-summary-prev-unread-article
1664   "N" gnus-summary-next-article
1665   "P" gnus-summary-prev-article
1666   "\M-\C-n" gnus-summary-next-same-subject
1667   "\M-\C-p" gnus-summary-prev-same-subject
1668   "\M-n" gnus-summary-next-unread-subject
1669   "\M-p" gnus-summary-prev-unread-subject
1670   "." gnus-summary-first-unread-article
1671   "," gnus-summary-best-unread-article
1672   "\M-s" gnus-summary-search-article-forward
1673   "\M-r" gnus-summary-search-article-backward
1674   "<" gnus-summary-beginning-of-article
1675   ">" gnus-summary-end-of-article
1676   "j" gnus-summary-goto-article
1677   "^" gnus-summary-refer-parent-article
1678   "\M-^" gnus-summary-refer-article
1679   "u" gnus-summary-tick-article-forward
1680   "!" gnus-summary-tick-article-forward
1681   "U" gnus-summary-tick-article-backward
1682   "d" gnus-summary-mark-as-read-forward
1683   "D" gnus-summary-mark-as-read-backward
1684   "E" gnus-summary-mark-as-expirable
1685   "\M-u" gnus-summary-clear-mark-forward
1686   "\M-U" gnus-summary-clear-mark-backward
1687   "k" gnus-summary-kill-same-subject-and-select
1688   "\C-k" gnus-summary-kill-same-subject
1689   "\M-\C-k" gnus-summary-kill-thread
1690   "\M-\C-l" gnus-summary-lower-thread
1691   "e" gnus-summary-edit-article
1692   "#" gnus-summary-mark-as-processable
1693   "\M-#" gnus-summary-unmark-as-processable
1694   "\M-\C-t" gnus-summary-toggle-threads
1695   "\M-\C-s" gnus-summary-show-thread
1696   "\M-\C-h" gnus-summary-hide-thread
1697   "\M-\C-f" gnus-summary-next-thread
1698   "\M-\C-b" gnus-summary-prev-thread
1699   [(meta down)] gnus-summary-next-thread
1700   [(meta up)] gnus-summary-prev-thread
1701   "\M-\C-u" gnus-summary-up-thread
1702   "\M-\C-d" gnus-summary-down-thread
1703   "&" gnus-summary-execute-command
1704   "c" gnus-summary-catchup-and-exit
1705   "\C-w" gnus-summary-mark-region-as-read
1706   "\C-t" gnus-summary-toggle-truncation
1707   "?" gnus-summary-mark-as-dormant
1708   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1709   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1710   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1711   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1712   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1713   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1714   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1715   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1716   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1717   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1718   "=" gnus-summary-expand-window
1719   "\C-x\C-s" gnus-summary-reselect-current-group
1720   "\M-g" gnus-summary-rescan-group
1721   "w" gnus-summary-stop-page-breaking
1722   "\C-c\C-r" gnus-summary-caesar-message
1723   "f" gnus-summary-followup
1724   "F" gnus-summary-followup-with-original
1725   "C" gnus-summary-cancel-article
1726   "r" gnus-summary-reply
1727   "R" gnus-summary-reply-with-original
1728   "\C-c\C-f" gnus-summary-mail-forward
1729   "o" gnus-summary-save-article
1730   "\C-o" gnus-summary-save-article-mail
1731   "|" gnus-summary-pipe-output
1732   "\M-k" gnus-summary-edit-local-kill
1733   "\M-K" gnus-summary-edit-global-kill
1734   ;; "V" gnus-version
1735   "\C-c\C-d" gnus-summary-describe-group
1736   "q" gnus-summary-exit
1737   "Q" gnus-summary-exit-no-update
1738   "\C-c\C-i" gnus-info-find-node
1739   gnus-mouse-2 gnus-mouse-pick-article
1740   "m" gnus-summary-mail-other-window
1741   "a" gnus-summary-post-news
1742   "i" gnus-summary-news-other-window
1743   "x" gnus-summary-limit-to-unread
1744   "s" gnus-summary-isearch-article
1745   "t" gnus-summary-toggle-header
1746   "g" gnus-summary-show-article
1747   "l" gnus-summary-goto-last-article
1748   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1749   "\C-d" gnus-summary-enter-digest-group
1750   "\M-\C-d" gnus-summary-read-document
1751   "\M-\C-e" gnus-summary-edit-parameters
1752   "\M-\C-a" gnus-summary-customize-parameters
1753   "\C-c\C-b" gnus-bug
1754   "*" gnus-cache-enter-article
1755   "\M-*" gnus-cache-remove-article
1756   "\M-&" gnus-summary-universal-argument
1757   "\C-l" gnus-recenter
1758   "I" gnus-summary-increase-score
1759   "L" gnus-summary-lower-score
1760   "\M-i" gnus-symbolic-argument
1761   "h" gnus-summary-select-article-buffer
1762
1763   "b" gnus-article-view-part
1764   "\M-t" gnus-summary-toggle-display-buttonized
1765
1766   "V" gnus-summary-score-map
1767   "X" gnus-uu-extract-map
1768   "S" gnus-summary-send-map)
1769
1770 ;; Sort of orthogonal keymap
1771 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1772   "t" gnus-summary-tick-article-forward
1773   "!" gnus-summary-tick-article-forward
1774   "d" gnus-summary-mark-as-read-forward
1775   "r" gnus-summary-mark-as-read-forward
1776   "c" gnus-summary-clear-mark-forward
1777   " " gnus-summary-clear-mark-forward
1778   "e" gnus-summary-mark-as-expirable
1779   "x" gnus-summary-mark-as-expirable
1780   "?" gnus-summary-mark-as-dormant
1781   "b" gnus-summary-set-bookmark
1782   "B" gnus-summary-remove-bookmark
1783   "#" gnus-summary-mark-as-processable
1784   "\M-#" gnus-summary-unmark-as-processable
1785   "S" gnus-summary-limit-include-expunged
1786   "C" gnus-summary-catchup
1787   "H" gnus-summary-catchup-to-here
1788   "h" gnus-summary-catchup-from-here
1789   "\C-c" gnus-summary-catchup-all
1790   "k" gnus-summary-kill-same-subject-and-select
1791   "K" gnus-summary-kill-same-subject
1792   "P" gnus-uu-mark-map)
1793
1794 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1795   "c" gnus-summary-clear-above
1796   "u" gnus-summary-tick-above
1797   "m" gnus-summary-mark-above
1798   "k" gnus-summary-kill-below)
1799
1800 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1801   "/" gnus-summary-limit-to-subject
1802   "n" gnus-summary-limit-to-articles
1803   "w" gnus-summary-pop-limit
1804   "s" gnus-summary-limit-to-subject
1805   "a" gnus-summary-limit-to-author
1806   "u" gnus-summary-limit-to-unread
1807   "m" gnus-summary-limit-to-marks
1808   "M" gnus-summary-limit-exclude-marks
1809   "v" gnus-summary-limit-to-score
1810   "*" gnus-summary-limit-include-cached
1811   "D" gnus-summary-limit-include-dormant
1812   "T" gnus-summary-limit-include-thread
1813   "d" gnus-summary-limit-exclude-dormant
1814   "t" gnus-summary-limit-to-age
1815   "." gnus-summary-limit-to-unseen
1816   "x" gnus-summary-limit-to-extra
1817   "p" gnus-summary-limit-to-display-predicate
1818   "E" gnus-summary-limit-include-expunged
1819   "c" gnus-summary-limit-exclude-childless-dormant
1820   "C" gnus-summary-limit-mark-excluded-as-read
1821   "o" gnus-summary-insert-old-articles
1822   "N" gnus-summary-insert-new-articles
1823   "r" gnus-summary-limit-to-replied)
1824
1825 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1826   "n" gnus-summary-next-unread-article
1827   "p" gnus-summary-prev-unread-article
1828   "N" gnus-summary-next-article
1829   "P" gnus-summary-prev-article
1830   "\C-n" gnus-summary-next-same-subject
1831   "\C-p" gnus-summary-prev-same-subject
1832   "\M-n" gnus-summary-next-unread-subject
1833   "\M-p" gnus-summary-prev-unread-subject
1834   "f" gnus-summary-first-unread-article
1835   "b" gnus-summary-best-unread-article
1836   "j" gnus-summary-goto-article
1837   "g" gnus-summary-goto-subject
1838   "l" gnus-summary-goto-last-article
1839   "o" gnus-summary-pop-article)
1840
1841 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1842   "k" gnus-summary-kill-thread
1843   "l" gnus-summary-lower-thread
1844   "i" gnus-summary-raise-thread
1845   "T" gnus-summary-toggle-threads
1846   "t" gnus-summary-rethread-current
1847   "^" gnus-summary-reparent-thread
1848   "s" gnus-summary-show-thread
1849   "S" gnus-summary-show-all-threads
1850   "h" gnus-summary-hide-thread
1851   "H" gnus-summary-hide-all-threads
1852   "n" gnus-summary-next-thread
1853   "p" gnus-summary-prev-thread
1854   "u" gnus-summary-up-thread
1855   "o" gnus-summary-top-thread
1856   "d" gnus-summary-down-thread
1857   "#" gnus-uu-mark-thread
1858   "\M-#" gnus-uu-unmark-thread)
1859
1860 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1861   "g" gnus-summary-prepare
1862   "c" gnus-summary-insert-cached-articles
1863   "d" gnus-summary-insert-dormant-articles)
1864
1865 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1866   "c" gnus-summary-catchup-and-exit
1867   "C" gnus-summary-catchup-all-and-exit
1868   "E" gnus-summary-exit-no-update
1869   "Q" gnus-summary-exit
1870   "Z" gnus-summary-exit
1871   "n" gnus-summary-catchup-and-goto-next-group
1872   "R" gnus-summary-reselect-current-group
1873   "G" gnus-summary-rescan-group
1874   "N" gnus-summary-next-group
1875   "s" gnus-summary-save-newsrc
1876   "P" gnus-summary-prev-group)
1877
1878 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1879   " " gnus-summary-next-page
1880   "n" gnus-summary-next-page
1881   "\177" gnus-summary-prev-page
1882   [delete] gnus-summary-prev-page
1883   "p" gnus-summary-prev-page
1884   "\r" gnus-summary-scroll-up
1885   "\M-\r" gnus-summary-scroll-down
1886   "<" gnus-summary-beginning-of-article
1887   ">" gnus-summary-end-of-article
1888   "b" gnus-summary-beginning-of-article
1889   "e" gnus-summary-end-of-article
1890   "^" gnus-summary-refer-parent-article
1891   "r" gnus-summary-refer-parent-article
1892   "D" gnus-summary-enter-digest-group
1893   "R" gnus-summary-refer-references
1894   "T" gnus-summary-refer-thread
1895   "g" gnus-summary-show-article
1896   "s" gnus-summary-isearch-article
1897   "P" gnus-summary-print-article
1898   "M" gnus-mailing-list-insinuate
1899   "t" gnus-article-babel)
1900
1901 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1902   "b" gnus-article-add-buttons
1903   "B" gnus-article-add-buttons-to-head
1904   "o" gnus-article-treat-overstrike
1905   "e" gnus-article-emphasize
1906   "w" gnus-article-fill-cited-article
1907   "Q" gnus-article-fill-long-lines
1908   "C" gnus-article-capitalize-sentences
1909   "c" gnus-article-remove-cr
1910   "q" gnus-article-de-quoted-unreadable
1911   "6" gnus-article-de-base64-unreadable
1912   "Z" gnus-article-decode-HZ
1913   "A" gnus-article-treat-ansi-sequences
1914   "h" gnus-article-wash-html
1915   "u" gnus-article-unsplit-urls
1916   "s" gnus-summary-force-verify-and-decrypt
1917   "f" gnus-article-display-x-face
1918   "l" gnus-summary-stop-page-breaking
1919   "r" gnus-summary-caesar-message
1920   "m" gnus-summary-morse-message
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-treat-smiley
1923   "v" gnus-summary-verbose-headers
1924   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1925   "p" gnus-article-verify-x-pgp-sig
1926   "d" gnus-article-treat-dumbquotes)
1927
1928 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1929   ;; mnemonic: deuglif*Y*
1930   "u" gnus-article-outlook-unwrap-lines
1931   "a" gnus-article-outlook-repair-attribution
1932   "c" gnus-article-outlook-rearrange-citation
1933   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1934
1935 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1936   "a" gnus-article-hide
1937   "h" gnus-article-hide-headers
1938   "b" gnus-article-hide-boring-headers
1939   "s" gnus-article-hide-signature
1940   "c" gnus-article-hide-citation
1941   "C" gnus-article-hide-citation-in-followups
1942   "l" gnus-article-hide-list-identifiers
1943   "B" gnus-article-strip-banner
1944   "P" gnus-article-hide-pem
1945   "\C-c" gnus-article-hide-citation-maybe)
1946
1947 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1948   "a" gnus-article-highlight
1949   "h" gnus-article-highlight-headers
1950   "c" gnus-article-highlight-citation
1951   "s" gnus-article-highlight-signature)
1952
1953 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1954   "f" gnus-article-treat-fold-headers
1955   "u" gnus-article-treat-unfold-headers
1956   "n" gnus-article-treat-fold-newsgroups)
1957
1958 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1959   "x" gnus-article-display-x-face
1960   "d" gnus-article-display-face
1961   "s" gnus-treat-smiley
1962   "D" gnus-article-remove-images
1963   "f" gnus-treat-from-picon
1964   "m" gnus-treat-mail-picon
1965   "n" gnus-treat-newsgroups-picon)
1966
1967 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1968   "w" gnus-article-decode-mime-words
1969   "c" gnus-article-decode-charset
1970   "v" gnus-mime-view-all-parts
1971   "b" gnus-article-view-part)
1972
1973 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1974   "z" gnus-article-date-ut
1975   "u" gnus-article-date-ut
1976   "l" gnus-article-date-local
1977   "p" gnus-article-date-english
1978   "e" gnus-article-date-lapsed
1979   "o" gnus-article-date-original
1980   "i" gnus-article-date-iso8601
1981   "s" gnus-article-date-user)
1982
1983 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1984   "t" gnus-article-remove-trailing-blank-lines
1985   "l" gnus-article-strip-leading-blank-lines
1986   "m" gnus-article-strip-multiple-blank-lines
1987   "a" gnus-article-strip-blank-lines
1988   "A" gnus-article-strip-all-blank-lines
1989   "s" gnus-article-strip-leading-space
1990   "e" gnus-article-strip-trailing-space
1991   "w" gnus-article-remove-leading-whitespace)
1992
1993 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1994   "v" gnus-version
1995   "f" gnus-summary-fetch-faq
1996   "d" gnus-summary-describe-group
1997   "h" gnus-summary-describe-briefly
1998   "i" gnus-info-find-node
1999   "c" gnus-group-fetch-charter
2000   "C" gnus-group-fetch-control)
2001
2002 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2003   "e" gnus-summary-expire-articles
2004   "\M-\C-e" gnus-summary-expire-articles-now
2005   "\177" gnus-summary-delete-article
2006   [delete] gnus-summary-delete-article
2007   [backspace] gnus-summary-delete-article
2008   "m" gnus-summary-move-article
2009   "r" gnus-summary-respool-article
2010   "w" gnus-summary-edit-article
2011   "c" gnus-summary-copy-article
2012   "B" gnus-summary-crosspost-article
2013   "q" gnus-summary-respool-query
2014   "t" gnus-summary-respool-trace
2015   "i" gnus-summary-import-article
2016   "I" gnus-summary-create-article
2017   "p" gnus-summary-article-posted-p)
2018
2019 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2020   "o" gnus-summary-save-article
2021   "m" gnus-summary-save-article-mail
2022   "F" gnus-summary-write-article-file
2023   "r" gnus-summary-save-article-rmail
2024   "f" gnus-summary-save-article-file
2025   "b" gnus-summary-save-article-body-file
2026   "h" gnus-summary-save-article-folder
2027   "v" gnus-summary-save-article-vm
2028   "p" gnus-summary-pipe-output
2029   "P" gnus-summary-muttprint
2030   "s" gnus-soup-add-article)
2031
2032 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2033   "b" gnus-summary-display-buttonized
2034   "m" gnus-summary-repair-multipart
2035   "v" gnus-article-view-part
2036   "o" gnus-article-save-part
2037   "c" gnus-article-copy-part
2038   "C" gnus-article-view-part-as-charset
2039   "e" gnus-article-view-part-externally
2040   "E" gnus-article-encrypt-body
2041   "i" gnus-article-inline-part
2042   "|" gnus-article-pipe-part)
2043
2044 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2045   "p" gnus-summary-mark-as-processable
2046   "u" gnus-summary-unmark-as-processable
2047   "U" gnus-summary-unmark-all-processable
2048   "v" gnus-uu-mark-over
2049   "s" gnus-uu-mark-series
2050   "r" gnus-uu-mark-region
2051   "g" gnus-uu-unmark-region
2052   "R" gnus-uu-mark-by-regexp
2053   "G" gnus-uu-unmark-by-regexp
2054   "t" gnus-uu-mark-thread
2055   "T" gnus-uu-unmark-thread
2056   "a" gnus-uu-mark-all
2057   "b" gnus-uu-mark-buffer
2058   "S" gnus-uu-mark-sparse
2059   "k" gnus-summary-kill-process-mark
2060   "y" gnus-summary-yank-process-mark
2061   "w" gnus-summary-save-process-mark
2062   "i" gnus-uu-invert-processable)
2063
2064 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2065   ;;"x" gnus-uu-extract-any
2066   "m" gnus-summary-save-parts
2067   "u" gnus-uu-decode-uu
2068   "U" gnus-uu-decode-uu-and-save
2069   "s" gnus-uu-decode-unshar
2070   "S" gnus-uu-decode-unshar-and-save
2071   "o" gnus-uu-decode-save
2072   "O" gnus-uu-decode-save
2073   "b" gnus-uu-decode-binhex
2074   "B" gnus-uu-decode-binhex
2075   "p" gnus-uu-decode-postscript
2076   "P" gnus-uu-decode-postscript-and-save)
2077
2078 (gnus-define-keys
2079     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2080   "u" gnus-uu-decode-uu-view
2081   "U" gnus-uu-decode-uu-and-save-view
2082   "s" gnus-uu-decode-unshar-view
2083   "S" gnus-uu-decode-unshar-and-save-view
2084   "o" gnus-uu-decode-save-view
2085   "O" gnus-uu-decode-save-view
2086   "b" gnus-uu-decode-binhex-view
2087   "B" gnus-uu-decode-binhex-view
2088   "p" gnus-uu-decode-postscript-view
2089   "P" gnus-uu-decode-postscript-and-save-view)
2090
2091 (defvar gnus-article-post-menu nil)
2092
2093 (defconst gnus-summary-menu-maxlen 20)
2094
2095 (defun gnus-summary-menu-split (menu)
2096   ;; If we have lots of elements, divide them into groups of 20
2097   ;; and make a pane (or submenu) for each one.
2098   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2099       (let ((menu menu) sublists next
2100             (i 1))
2101         (while menu
2102           ;; Pull off the next gnus-summary-menu-maxlen elements
2103           ;; and make them the next element of sublist.
2104           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2105           (if next
2106               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2107                       nil))
2108           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2109                                              (aref (car (last menu)) 0)) menu)
2110                                sublists))
2111           (setq i (1+ i))
2112           (setq menu next))
2113         (nreverse sublists))
2114     ;; Few elements--put them all in one pane.
2115     menu))
2116
2117 (defun gnus-summary-make-menu-bar ()
2118   (gnus-turn-off-edit-menu 'summary)
2119
2120   (unless (boundp 'gnus-summary-misc-menu)
2121
2122     (easy-menu-define
2123       gnus-summary-kill-menu gnus-summary-mode-map ""
2124       (cons
2125        "Score"
2126        (nconc
2127         (list
2128          ["Customize" gnus-score-customize t])
2129         (gnus-make-score-map 'increase)
2130         (gnus-make-score-map 'lower)
2131         '(("Mark"
2132            ["Kill below" gnus-summary-kill-below t]
2133            ["Mark above" gnus-summary-mark-above t]
2134            ["Tick above" gnus-summary-tick-above t]
2135            ["Clear above" gnus-summary-clear-above t])
2136           ["Current score" gnus-summary-current-score t]
2137           ["Set score" gnus-summary-set-score t]
2138           ["Switch current score file..." gnus-score-change-score-file t]
2139           ["Set mark below..." gnus-score-set-mark-below t]
2140           ["Set expunge below..." gnus-score-set-expunge-below t]
2141           ["Edit current score file" gnus-score-edit-current-scores t]
2142           ["Edit score file" gnus-score-edit-file t]
2143           ["Trace score" gnus-score-find-trace t]
2144           ["Find words" gnus-score-find-favourite-words t]
2145           ["Rescore buffer" gnus-summary-rescore t]
2146           ["Increase score..." gnus-summary-increase-score t]
2147           ["Lower score..." gnus-summary-lower-score t]))))
2148
2149     ;; Define both the Article menu in the summary buffer and the
2150     ;; equivalent Commands menu in the article buffer here for
2151     ;; consistency.
2152     (let ((innards
2153            `(("Hide"
2154               ["All" gnus-article-hide t]
2155               ["Headers" gnus-article-hide-headers t]
2156               ["Signature" gnus-article-hide-signature t]
2157               ["Citation" gnus-article-hide-citation t]
2158               ["List identifiers" gnus-article-hide-list-identifiers t]
2159               ["Banner" gnus-article-strip-banner t]
2160               ["Boring headers" gnus-article-hide-boring-headers t])
2161              ("Highlight"
2162               ["All" gnus-article-highlight t]
2163               ["Headers" gnus-article-highlight-headers t]
2164               ["Signature" gnus-article-highlight-signature t]
2165               ["Citation" gnus-article-highlight-citation t])
2166              ("MIME"
2167               ["Words" gnus-article-decode-mime-words t]
2168               ["Charset" gnus-article-decode-charset t]
2169               ["QP" gnus-article-de-quoted-unreadable t]
2170               ["Base64" gnus-article-de-base64-unreadable t]
2171               ["View MIME buttons" gnus-summary-display-buttonized t]
2172               ["View all" gnus-mime-view-all-parts t]
2173               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2174               ["Encrypt body" gnus-article-encrypt-body
2175                :active (not (gnus-group-read-only-p))
2176                ,@(if (featurep 'xemacs) nil
2177                    '(:help "Encrypt the message body on disk"))]
2178               ["Extract all parts..." gnus-summary-save-parts t]
2179               ("Multipart"
2180                ["Repair multipart" gnus-summary-repair-multipart t]
2181                ["Pipe part..." gnus-article-pipe-part t]
2182                ["Inline part" gnus-article-inline-part t]
2183                ["Encrypt body" gnus-article-encrypt-body
2184                 :active (not (gnus-group-read-only-p))
2185                ,@(if (featurep 'xemacs) nil
2186                    '(:help "Encrypt the message body on disk"))]
2187                ["View part externally" gnus-article-view-part-externally t]
2188                ["View part with charset..." gnus-article-view-part-as-charset t]
2189                ["Copy part" gnus-article-copy-part t]
2190                ["Save part..." gnus-article-save-part t]
2191                ["View part" gnus-article-view-part t]))
2192              ("Date"
2193               ["Local" gnus-article-date-local t]
2194               ["ISO8601" gnus-article-date-iso8601 t]
2195               ["UT" gnus-article-date-ut t]
2196               ["Original" gnus-article-date-original t]
2197               ["Lapsed" gnus-article-date-lapsed t]
2198               ["User-defined" gnus-article-date-user t])
2199              ("Display"
2200               ["Remove images" gnus-article-remove-images t]
2201               ["Toggle smiley" gnus-treat-smiley t]
2202               ["Show X-Face" gnus-article-display-x-face t]
2203               ["Show picons in From" gnus-treat-from-picon t]
2204               ["Show picons in mail headers" gnus-treat-mail-picon t]
2205               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2206               ("View as different encoding"
2207                ,@(gnus-summary-menu-split
2208                   (mapcar
2209                    (lambda (cs)
2210                      ;; Since easymenu under Emacs doesn't allow
2211                      ;; lambda forms for menu commands, we should
2212                      ;; provide intern'ed function symbols.
2213                      (let ((command (intern (format "\
2214 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2215                        (fset command
2216                              `(lambda ()
2217                                 (interactive)
2218                                 (let ((gnus-summary-show-article-charset-alist
2219                                        '((1 . ,cs))))
2220                                   (gnus-summary-show-article 1))))
2221                        `[,(symbol-name cs) ,command t]))
2222                    (sort (if (fboundp 'coding-system-list)
2223                              (coding-system-list)
2224                            (mapcar 'car mm-mime-mule-charset-alist))
2225                          'string<)))))
2226              ("Washing"
2227               ("Remove Blanks"
2228                ["Leading" gnus-article-strip-leading-blank-lines t]
2229                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2230                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2231                ["All of the above" gnus-article-strip-blank-lines t]
2232                ["All" gnus-article-strip-all-blank-lines t]
2233                ["Leading space" gnus-article-strip-leading-space t]
2234                ["Trailing space" gnus-article-strip-trailing-space t]
2235                ["Leading space in headers"
2236                 gnus-article-remove-leading-whitespace t])
2237               ["Overstrike" gnus-article-treat-overstrike t]
2238               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2239               ["Emphasis" gnus-article-emphasize t]
2240               ["Word wrap" gnus-article-fill-cited-article t]
2241               ["Fill long lines" gnus-article-fill-long-lines t]
2242               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2243               ["Remove CR" gnus-article-remove-cr t]
2244               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2245               ["Base64" gnus-article-de-base64-unreadable t]
2246               ["Rot 13" gnus-summary-caesar-message
2247                ,@(if (featurep 'xemacs) '(t)
2248                    '(:help "\"Caesar rotate\" article by 13"))]
2249               ["Morse decode" gnus-summary-morse-message t]
2250               ["Unix pipe..." gnus-summary-pipe-message t]
2251               ["Add buttons" gnus-article-add-buttons t]
2252               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2253               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2254               ["Verbose header" gnus-summary-verbose-headers t]
2255               ["Toggle header" gnus-summary-toggle-header t]
2256               ["Unfold headers" gnus-article-treat-unfold-headers t]
2257               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2258               ["Html" gnus-article-wash-html t]
2259               ["Unsplit URLs" gnus-article-unsplit-urls t]
2260               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2261               ["Decode HZ" gnus-article-decode-HZ t]
2262               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2263               ("(Outlook) Deuglify"
2264                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2265                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2266                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2267                ["Full (Outlook) deuglify"
2268                 gnus-article-outlook-deuglify-article t])
2269               )
2270              ("Output"
2271               ["Save in default format..." gnus-summary-save-article
2272                ,@(if (featurep 'xemacs) '(t)
2273                    '(:help "Save article using default method"))]
2274               ["Save in file..." gnus-summary-save-article-file
2275                ,@(if (featurep 'xemacs) '(t)
2276                    '(:help "Save article in file"))]
2277               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2278               ["Save in MH folder..." gnus-summary-save-article-folder t]
2279               ["Save in VM folder..." gnus-summary-save-article-vm t]
2280               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2281               ["Save body in file..." gnus-summary-save-article-body-file t]
2282               ["Pipe through a filter..." gnus-summary-pipe-output t]
2283               ["Add to SOUP packet" gnus-soup-add-article t]
2284               ["Print with Muttprint..." gnus-summary-muttprint t]
2285               ["Print" gnus-summary-print-article t])
2286              ("Backend"
2287               ["Respool article..." gnus-summary-respool-article t]
2288               ["Move article..." gnus-summary-move-article
2289                (gnus-check-backend-function
2290                 'request-move-article gnus-newsgroup-name)]
2291               ["Copy article..." gnus-summary-copy-article t]
2292               ["Crosspost article..." gnus-summary-crosspost-article
2293                (gnus-check-backend-function
2294                 'request-replace-article gnus-newsgroup-name)]
2295               ["Import file..." gnus-summary-import-article
2296                (gnus-check-backend-function
2297                 'request-accept-article gnus-newsgroup-name)]
2298               ["Create article..." gnus-summary-create-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Check if posted" gnus-summary-article-posted-p t]
2302               ["Edit article" gnus-summary-edit-article
2303                (not (gnus-group-read-only-p))]
2304               ["Delete article" gnus-summary-delete-article
2305                (gnus-check-backend-function
2306                 'request-expire-articles gnus-newsgroup-name)]
2307               ["Query respool" gnus-summary-respool-query t]
2308               ["Trace respool" gnus-summary-respool-trace t]
2309               ["Delete expirable articles" gnus-summary-expire-articles-now
2310                (gnus-check-backend-function
2311                 'request-expire-articles gnus-newsgroup-name)])
2312              ("Extract"
2313               ["Uudecode" gnus-uu-decode-uu
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Decode uuencoded article(s)"))]
2316               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2317               ["Unshar" gnus-uu-decode-unshar t]
2318               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2319               ["Save" gnus-uu-decode-save t]
2320               ["Binhex" gnus-uu-decode-binhex t]
2321               ["Postscript" gnus-uu-decode-postscript t]
2322               ["All MIME parts" gnus-summary-save-parts t])
2323              ("Cache"
2324               ["Enter article" gnus-cache-enter-article t]
2325               ["Remove article" gnus-cache-remove-article t])
2326              ["Translate" gnus-article-babel t]
2327              ["Select article buffer" gnus-summary-select-article-buffer t]
2328              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2329              ["Isearch article..." gnus-summary-isearch-article t]
2330              ["Beginning of the article" gnus-summary-beginning-of-article t]
2331              ["End of the article" gnus-summary-end-of-article t]
2332              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2333              ["Fetch referenced articles" gnus-summary-refer-references t]
2334              ["Fetch current thread" gnus-summary-refer-thread t]
2335              ["Fetch article with id..." gnus-summary-refer-article t]
2336              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2337              ["Redisplay" gnus-summary-show-article t]
2338              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2339       (easy-menu-define
2340         gnus-summary-article-menu gnus-summary-mode-map ""
2341         (cons "Article" innards))
2342
2343       (if (not (keymapp gnus-summary-article-menu))
2344           (easy-menu-define
2345             gnus-article-commands-menu gnus-article-mode-map ""
2346             (cons "Commands" innards))
2347         ;; in Emacs, don't share menu.
2348         (setq gnus-article-commands-menu
2349               (copy-keymap gnus-summary-article-menu))
2350         (define-key gnus-article-mode-map [menu-bar commands]
2351           (cons "Commands" gnus-article-commands-menu))))
2352
2353     (easy-menu-define
2354       gnus-summary-thread-menu gnus-summary-mode-map ""
2355       '("Threads"
2356         ["Find all messages in thread" gnus-summary-refer-thread t]
2357         ["Toggle threading" gnus-summary-toggle-threads t]
2358         ["Hide threads" gnus-summary-hide-all-threads t]
2359         ["Show threads" gnus-summary-show-all-threads t]
2360         ["Hide thread" gnus-summary-hide-thread t]
2361         ["Show thread" gnus-summary-show-thread t]
2362         ["Go to next thread" gnus-summary-next-thread t]
2363         ["Go to previous thread" gnus-summary-prev-thread t]
2364         ["Go down thread" gnus-summary-down-thread t]
2365         ["Go up thread" gnus-summary-up-thread t]
2366         ["Top of thread" gnus-summary-top-thread t]
2367         ["Mark thread as read" gnus-summary-kill-thread t]
2368         ["Lower thread score" gnus-summary-lower-thread t]
2369         ["Raise thread score" gnus-summary-raise-thread t]
2370         ["Rethread current" gnus-summary-rethread-current t]))
2371
2372     (easy-menu-define
2373       gnus-summary-post-menu gnus-summary-mode-map ""
2374       `("Post"
2375         ["Send a message (mail or news)" gnus-summary-post-news
2376          ,@(if (featurep 'xemacs) '(t)
2377              '(:help "Post an article"))]
2378         ["Followup" gnus-summary-followup
2379          ,@(if (featurep 'xemacs) '(t)
2380              '(:help "Post followup to this article"))]
2381         ["Followup and yank" gnus-summary-followup-with-original
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Post followup to this article, quoting its contents"))]
2384         ["Supersede article" gnus-summary-supersede-article t]
2385         ["Cancel article" gnus-summary-cancel-article
2386          ,@(if (featurep 'xemacs) '(t)
2387              '(:help "Cancel an article you posted"))]
2388         ["Reply" gnus-summary-reply t]
2389         ["Reply and yank" gnus-summary-reply-with-original t]
2390         ["Wide reply" gnus-summary-wide-reply t]
2391         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Mail a reply, quoting this article"))]
2394         ["Very wide reply" gnus-summary-very-wide-reply t]
2395         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Mail a very wide reply, quoting this article"))]
2398         ["Mail forward" gnus-summary-mail-forward t]
2399         ["Post forward" gnus-summary-post-forward t]
2400         ["Digest and mail" gnus-uu-digest-mail-forward t]
2401         ["Digest and post" gnus-uu-digest-post-forward t]
2402         ["Resend message" gnus-summary-resend-message t]
2403         ["Resend message edit" gnus-summary-resend-message-edit t]
2404         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2405         ["Send a mail" gnus-summary-mail-other-window t]
2406         ["Create a local message" gnus-summary-news-other-window t]
2407         ["Uuencode and post" gnus-uu-post-news
2408          ,@(if (featurep 'xemacs) '(t)
2409              '(:help "Post a uuencoded article"))]
2410         ["Followup via news" gnus-summary-followup-to-mail t]
2411         ["Followup via news and yank"
2412          gnus-summary-followup-to-mail-with-original t]
2413         ;;("Draft"
2414         ;;["Send" gnus-summary-send-draft t]
2415         ;;["Send bounced" gnus-resend-bounced-mail t])
2416         ))
2417
2418     (cond
2419      ((not (keymapp gnus-summary-post-menu))
2420       (setq gnus-article-post-menu gnus-summary-post-menu))
2421      ((not gnus-article-post-menu)
2422       ;; Don't share post menu.
2423       (setq gnus-article-post-menu
2424             (copy-keymap gnus-summary-post-menu))))
2425     (define-key gnus-article-mode-map [menu-bar post]
2426       (cons "Post" gnus-article-post-menu))
2427
2428     (easy-menu-define
2429       gnus-summary-misc-menu gnus-summary-mode-map ""
2430       `("Gnus"
2431         ("Mark Read"
2432          ["Mark as read" gnus-summary-mark-as-read-forward t]
2433          ["Mark same subject and select"
2434           gnus-summary-kill-same-subject-and-select t]
2435          ["Mark same subject" gnus-summary-kill-same-subject t]
2436          ["Catchup" gnus-summary-catchup
2437           ,@(if (featurep 'xemacs) '(t)
2438               '(:help "Mark unread articles in this group as read"))]
2439          ["Catchup all" gnus-summary-catchup-all t]
2440          ["Catchup to here" gnus-summary-catchup-to-here t]
2441          ["Catchup from here" gnus-summary-catchup-from-here t]
2442          ["Catchup region" gnus-summary-mark-region-as-read
2443           (gnus-mark-active-p)]
2444          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2445         ("Mark Various"
2446          ["Tick" gnus-summary-tick-article-forward t]
2447          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2448          ["Remove marks" gnus-summary-clear-mark-forward t]
2449          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2450          ["Set bookmark" gnus-summary-set-bookmark t]
2451          ["Remove bookmark" gnus-summary-remove-bookmark t])
2452         ("Limit to"
2453          ["Marks..." gnus-summary-limit-to-marks t]
2454          ["Subject..." gnus-summary-limit-to-subject t]
2455          ["Author..." gnus-summary-limit-to-author t]
2456          ["Age..." gnus-summary-limit-to-age t]
2457          ["Extra..." gnus-summary-limit-to-extra t]
2458          ["Score..." gnus-summary-limit-to-score t]
2459          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2460          ["Unread" gnus-summary-limit-to-unread t]
2461          ["Unseen" gnus-summary-limit-to-unseen t]
2462          ["Replied" gnus-summary-limit-to-replied t]
2463          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2464          ["Next articles" gnus-summary-limit-to-articles t]
2465          ["Pop limit" gnus-summary-pop-limit t]
2466          ["Show dormant" gnus-summary-limit-include-dormant t]
2467          ["Hide childless dormant"
2468           gnus-summary-limit-exclude-childless-dormant t]
2469          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2470          ["Hide marked" gnus-summary-limit-exclude-marks t]
2471          ["Show expunged" gnus-summary-limit-include-expunged t])
2472         ("Process Mark"
2473          ["Set mark" gnus-summary-mark-as-processable t]
2474          ["Remove mark" gnus-summary-unmark-as-processable t]
2475          ["Remove all marks" gnus-summary-unmark-all-processable t]
2476          ["Invert marks" gnus-uu-invert-processable t]
2477          ["Mark above" gnus-uu-mark-over t]
2478          ["Mark series" gnus-uu-mark-series t]
2479          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2480          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2481          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2482          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2483          ["Mark all" gnus-uu-mark-all t]
2484          ["Mark buffer" gnus-uu-mark-buffer t]
2485          ["Mark sparse" gnus-uu-mark-sparse t]
2486          ["Mark thread" gnus-uu-mark-thread t]
2487          ["Unmark thread" gnus-uu-unmark-thread t]
2488          ("Process Mark Sets"
2489           ["Kill" gnus-summary-kill-process-mark t]
2490           ["Yank" gnus-summary-yank-process-mark
2491            gnus-newsgroup-process-stack]
2492           ["Save" gnus-summary-save-process-mark t]
2493           ["Run command on marked..." gnus-summary-universal-argument t]))
2494         ("Scroll article"
2495          ["Page forward" gnus-summary-next-page
2496           ,@(if (featurep 'xemacs) '(t)
2497               '(:help "Show next page of article"))]
2498          ["Page backward" gnus-summary-prev-page
2499           ,@(if (featurep 'xemacs) '(t)
2500               '(:help "Show previous page of article"))]
2501          ["Line forward" gnus-summary-scroll-up t])
2502         ("Move"
2503          ["Next unread article" gnus-summary-next-unread-article t]
2504          ["Previous unread article" gnus-summary-prev-unread-article t]
2505          ["Next article" gnus-summary-next-article t]
2506          ["Previous article" gnus-summary-prev-article t]
2507          ["Next unread subject" gnus-summary-next-unread-subject t]
2508          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2509          ["Next article same subject" gnus-summary-next-same-subject t]
2510          ["Previous article same subject" gnus-summary-prev-same-subject t]
2511          ["First unread article" gnus-summary-first-unread-article t]
2512          ["Best unread article" gnus-summary-best-unread-article t]
2513          ["Go to subject number..." gnus-summary-goto-subject t]
2514          ["Go to article number..." gnus-summary-goto-article t]
2515          ["Go to the last article" gnus-summary-goto-last-article t]
2516          ["Pop article off history" gnus-summary-pop-article t])
2517         ("Sort"
2518          ["Sort by number" gnus-summary-sort-by-number t]
2519          ["Sort by author" gnus-summary-sort-by-author t]
2520          ["Sort by subject" gnus-summary-sort-by-subject t]
2521          ["Sort by date" gnus-summary-sort-by-date t]
2522          ["Sort by score" gnus-summary-sort-by-score t]
2523          ["Sort by lines" gnus-summary-sort-by-lines t]
2524          ["Sort by characters" gnus-summary-sort-by-chars t]
2525          ["Randomize" gnus-summary-sort-by-random t]
2526          ["Original sort" gnus-summary-sort-by-original t])
2527         ("Help"
2528          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2529          ["Describe group" gnus-summary-describe-group t]
2530          ["Fetch charter" gnus-group-fetch-charter
2531           ,@(if (featurep 'xemacs) nil
2532               '(:help "Display the charter of the current group"))]
2533          ["Fetch control message" gnus-group-fetch-control
2534           ,@(if (featurep 'xemacs) nil
2535               '(:help "Display the archived control message for the current group"))]
2536          ["Read manual" gnus-info-find-node t])
2537         ("Modes"
2538          ["Pick and read" gnus-pick-mode t]
2539          ["Binary" gnus-binary-mode t])
2540         ("Regeneration"
2541          ["Regenerate" gnus-summary-prepare t]
2542          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2543          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2544          ["Toggle threading" gnus-summary-toggle-threads t])
2545         ["See old articles" gnus-summary-insert-old-articles t]
2546         ["See new articles" gnus-summary-insert-new-articles t]
2547         ["Filter articles..." gnus-summary-execute-command t]
2548         ["Run command on articles..." gnus-summary-universal-argument t]
2549         ["Search articles forward..." gnus-summary-search-article-forward t]
2550         ["Search articles backward..." gnus-summary-search-article-backward t]
2551         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2552         ["Expand window" gnus-summary-expand-window t]
2553         ["Expire expirable articles" gnus-summary-expire-articles
2554          (gnus-check-backend-function
2555           'request-expire-articles gnus-newsgroup-name)]
2556         ["Edit local kill file" gnus-summary-edit-local-kill t]
2557         ["Edit main kill file" gnus-summary-edit-global-kill t]
2558         ["Edit group parameters" gnus-summary-edit-parameters t]
2559         ["Customize group parameters" gnus-summary-customize-parameters t]
2560         ["Send a bug report" gnus-bug t]
2561         ("Exit"
2562          ["Catchup and exit" gnus-summary-catchup-and-exit
2563           ,@(if (featurep 'xemacs) '(t)
2564               '(:help "Mark unread articles in this group as read, then exit"))]
2565          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2566          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2567          ["Exit group" gnus-summary-exit
2568           ,@(if (featurep 'xemacs) '(t)
2569               '(:help "Exit current group, return to group selection mode"))]
2570          ["Exit group without updating" gnus-summary-exit-no-update t]
2571          ["Exit and goto next group" gnus-summary-next-group t]
2572          ["Exit and goto prev group" gnus-summary-prev-group t]
2573          ["Reselect group" gnus-summary-reselect-current-group t]
2574          ["Rescan group" gnus-summary-rescan-group t]
2575          ["Update dribble" gnus-summary-save-newsrc t])))
2576
2577     (gnus-run-hooks 'gnus-summary-menu-hook)))
2578
2579 (defvar gnus-summary-tool-bar-map nil)
2580
2581 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2582 (defun gnus-summary-make-tool-bar ()
2583   (if (and (fboundp 'tool-bar-add-item-from-menu)
2584            (default-value 'tool-bar-mode)
2585            (not gnus-summary-tool-bar-map))
2586       (setq gnus-summary-tool-bar-map
2587             (let ((tool-bar-map (make-sparse-keymap))
2588                   (load-path (mm-image-load-path)))
2589               (tool-bar-add-item-from-menu
2590                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2591               (tool-bar-add-item-from-menu
2592                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2593               (tool-bar-add-item-from-menu
2594                'gnus-summary-post-news "post" gnus-summary-mode-map)
2595               (tool-bar-add-item-from-menu
2596                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2597               (tool-bar-add-item-from-menu
2598                'gnus-summary-followup "followup" gnus-summary-mode-map)
2599               (tool-bar-add-item-from-menu
2600                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2601               (tool-bar-add-item-from-menu
2602                'gnus-summary-reply "reply" gnus-summary-mode-map)
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2617               (tool-bar-add-item-from-menu
2618                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2619               (tool-bar-add-item-from-menu
2620                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2621               tool-bar-map)))
2622   (if gnus-summary-tool-bar-map
2623       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2624
2625 (defun gnus-score-set-default (var value)
2626   "A version of set that updates the GNU Emacs menu-bar."
2627   (set var value)
2628   ;; It is the message that forces the active status to be updated.
2629   (message ""))
2630
2631 (defun gnus-make-score-map (type)
2632   "Make a summary score map of type TYPE."
2633   (if t
2634       nil
2635     (let ((headers '(("author" "from" string)
2636                      ("subject" "subject" string)
2637                      ("article body" "body" string)
2638                      ("article head" "head" string)
2639                      ("xref" "xref" string)
2640                      ("extra header" "extra" string)
2641                      ("lines" "lines" number)
2642                      ("followups to author" "followup" string)))
2643           (types '((number ("less than" <)
2644                            ("greater than" >)
2645                            ("equal" =))
2646                    (string ("substring" s)
2647                            ("exact string" e)
2648                            ("fuzzy string" f)
2649                            ("regexp" r))))
2650           (perms '(("temporary" (current-time-string))
2651                    ("permanent" nil)
2652                    ("immediate" now)))
2653           header)
2654       (list
2655        (apply
2656         'nconc
2657         (list
2658          (if (eq type 'lower)
2659              "Lower score"
2660            "Increase score"))
2661         (let (outh)
2662           (while headers
2663             (setq header (car headers))
2664             (setq outh
2665                   (cons
2666                    (apply
2667                     'nconc
2668                     (list (car header))
2669                     (let ((ts (cdr (assoc (nth 2 header) types)))
2670                           outt)
2671                       (while ts
2672                         (setq outt
2673                               (cons
2674                                (apply
2675                                 'nconc
2676                                 (list (caar ts))
2677                                 (let ((ps perms)
2678                                       outp)
2679                                   (while ps
2680                                     (setq outp
2681                                           (cons
2682                                            (vector
2683                                             (caar ps)
2684                                             (list
2685                                              'gnus-summary-score-entry
2686                                              (nth 1 header)
2687                                              (if (or (string= (nth 1 header)
2688                                                               "head")
2689                                                      (string= (nth 1 header)
2690                                                               "body"))
2691                                                  ""
2692                                                (list 'gnus-summary-header
2693                                                      (nth 1 header)))
2694                                              (list 'quote (nth 1 (car ts)))
2695                                              (list 'gnus-score-delta-default
2696                                                    nil)
2697                                              (nth 1 (car ps))
2698                                              t)
2699                                             t)
2700                                            outp))
2701                                     (setq ps (cdr ps)))
2702                                   (list (nreverse outp))))
2703                                outt))
2704                         (setq ts (cdr ts)))
2705                       (list (nreverse outt))))
2706                    outh))
2707             (setq headers (cdr headers)))
2708           (list (nreverse outh))))))))
2709
2710 \f
2711
2712 (defun gnus-summary-mode (&optional group)
2713   "Major mode for reading articles.
2714
2715 All normal editing commands are switched off.
2716 \\<gnus-summary-mode-map>
2717 Each line in this buffer represents one article.  To read an
2718 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2719 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2720 respectively.
2721
2722 You can also post articles and send mail from this buffer.  To
2723 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2724 of an article, type `\\[gnus-summary-reply]'.
2725
2726 There are approx. one gazillion commands you can execute in this
2727 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2728
2729 The following commands are available:
2730
2731 \\{gnus-summary-mode-map}"
2732   (interactive)
2733   (kill-all-local-variables)
2734   (when (gnus-visual-p 'summary-menu 'menu)
2735     (gnus-summary-make-menu-bar)
2736     (gnus-summary-make-tool-bar))
2737   (gnus-summary-make-local-variables)
2738   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2739     (gnus-summary-make-local-variables))
2740   (gnus-make-thread-indent-array)
2741   (gnus-simplify-mode-line)
2742   (setq major-mode 'gnus-summary-mode)
2743   (setq mode-name "Summary")
2744   (make-local-variable 'minor-mode-alist)
2745   (use-local-map gnus-summary-mode-map)
2746   (buffer-disable-undo)
2747   (setq buffer-read-only t              ;Disable modification
2748         show-trailing-whitespace nil)
2749   (setq truncate-lines t)
2750   (setq selective-display t)
2751   (setq selective-display-ellipses t)   ;Display `...'
2752   (gnus-summary-set-display-table)
2753   (gnus-set-default-directory)
2754   (setq gnus-newsgroup-name group)
2755   (make-local-variable 'gnus-summary-line-format)
2756   (make-local-variable 'gnus-summary-line-format-spec)
2757   (make-local-variable 'gnus-summary-dummy-line-format)
2758   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2759   (make-local-variable 'gnus-summary-mark-positions)
2760   (gnus-make-local-hook 'pre-command-hook)
2761   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2762   (gnus-run-hooks 'gnus-summary-mode-hook)
2763   (turn-on-gnus-mailing-list-mode)
2764   (mm-enable-multibyte)
2765   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2766   (gnus-update-summary-mark-positions))
2767
2768 (defun gnus-summary-make-local-variables ()
2769   "Make all the local summary buffer variables."
2770   (let (global)
2771     (dolist (local gnus-summary-local-variables)
2772       (if (consp local)
2773           (progn
2774             (if (eq (cdr local) 'global)
2775                 ;; Copy the global value of the variable.
2776                 (setq global (symbol-value (car local)))
2777               ;; Use the value from the list.
2778               (setq global (eval (cdr local))))
2779             (set (make-local-variable (car local)) global))
2780         ;; Simple nil-valued local variable.
2781         (set (make-local-variable local) nil)))))
2782
2783 (defun gnus-summary-clear-local-variables ()
2784   (let ((locals gnus-summary-local-variables))
2785     (while locals
2786       (if (consp (car locals))
2787           (and (vectorp (caar locals))
2788                (set (caar locals) nil))
2789         (and (vectorp (car locals))
2790              (set (car locals) nil)))
2791       (setq locals (cdr locals)))))
2792
2793 ;; Summary data functions.
2794
2795 (defmacro gnus-data-number (data)
2796   `(car ,data))
2797
2798 (defmacro gnus-data-set-number (data number)
2799   `(setcar ,data ,number))
2800
2801 (defmacro gnus-data-mark (data)
2802   `(nth 1 ,data))
2803
2804 (defmacro gnus-data-set-mark (data mark)
2805   `(setcar (nthcdr 1 ,data) ,mark))
2806
2807 (defmacro gnus-data-pos (data)
2808   `(nth 2 ,data))
2809
2810 (defmacro gnus-data-set-pos (data pos)
2811   `(setcar (nthcdr 2 ,data) ,pos))
2812
2813 (defmacro gnus-data-header (data)
2814   `(nth 3 ,data))
2815
2816 (defmacro gnus-data-set-header (data header)
2817   `(setf (nth 3 ,data) ,header))
2818
2819 (defmacro gnus-data-level (data)
2820   `(nth 4 ,data))
2821
2822 (defmacro gnus-data-unread-p (data)
2823   `(= (nth 1 ,data) gnus-unread-mark))
2824
2825 (defmacro gnus-data-read-p (data)
2826   `(/= (nth 1 ,data) gnus-unread-mark))
2827
2828 (defmacro gnus-data-pseudo-p (data)
2829   `(consp (nth 3 ,data)))
2830
2831 (defmacro gnus-data-find (number)
2832   `(assq ,number gnus-newsgroup-data))
2833
2834 (defmacro gnus-data-find-list (number &optional data)
2835   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2836      (memq (assq ,number bdata)
2837            bdata)))
2838
2839 (defmacro gnus-data-make (number mark pos header level)
2840   `(list ,number ,mark ,pos ,header ,level))
2841
2842 (defun gnus-data-enter (after-article number mark pos header level offset)
2843   (let ((data (gnus-data-find-list after-article)))
2844     (unless data
2845       (error "No such article: %d" after-article))
2846     (setcdr data (cons (gnus-data-make number mark pos header level)
2847                        (cdr data)))
2848     (setq gnus-newsgroup-data-reverse nil)
2849     (gnus-data-update-list (cddr data) offset)))
2850
2851 (defun gnus-data-enter-list (after-article list &optional offset)
2852   (when list
2853     (let ((data (and after-article (gnus-data-find-list after-article)))
2854           (ilist list))
2855       (if (not (or data
2856                    after-article))
2857           (let ((odata gnus-newsgroup-data))
2858             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2859             (when offset
2860               (gnus-data-update-list odata offset)))
2861       ;; Find the last element in the list to be spliced into the main
2862         ;; list.
2863         (while (cdr list)
2864           (setq list (cdr list)))
2865         (if (not data)
2866             (progn
2867               (setcdr list gnus-newsgroup-data)
2868               (setq gnus-newsgroup-data ilist)
2869               (when offset
2870                 (gnus-data-update-list (cdr list) offset)))
2871           (setcdr list (cdr data))
2872           (setcdr data ilist)
2873           (when offset
2874             (gnus-data-update-list (cdr list) offset))))
2875       (setq gnus-newsgroup-data-reverse nil))))
2876
2877 (defun gnus-data-remove (article &optional offset)
2878   (let ((data gnus-newsgroup-data))
2879     (if (= (gnus-data-number (car data)) article)
2880         (progn
2881           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2882                 gnus-newsgroup-data-reverse nil)
2883           (when offset
2884             (gnus-data-update-list gnus-newsgroup-data offset)))
2885       (while (cdr data)
2886         (when (= (gnus-data-number (cadr data)) article)
2887           (setcdr data (cddr data))
2888           (when offset
2889             (gnus-data-update-list (cdr data) offset))
2890           (setq data nil
2891                 gnus-newsgroup-data-reverse nil))
2892         (setq data (cdr data))))))
2893
2894 (defmacro gnus-data-list (backward)
2895   `(if ,backward
2896        (or gnus-newsgroup-data-reverse
2897            (setq gnus-newsgroup-data-reverse
2898                  (reverse gnus-newsgroup-data)))
2899      gnus-newsgroup-data))
2900
2901 (defun gnus-data-update-list (data offset)
2902   "Add OFFSET to the POS of all data entries in DATA."
2903   (setq gnus-newsgroup-data-reverse nil)
2904   (while data
2905     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2906     (setq data (cdr data))))
2907
2908 (defun gnus-summary-article-pseudo-p (article)
2909   "Say whether this article is a pseudo article or not."
2910   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2911
2912 (defmacro gnus-summary-article-sparse-p (article)
2913   "Say whether this article is a sparse article or not."
2914   `(memq ,article gnus-newsgroup-sparse))
2915
2916 (defmacro gnus-summary-article-ancient-p (article)
2917   "Say whether this article is a sparse article or not."
2918   `(memq ,article gnus-newsgroup-ancient))
2919
2920 (defun gnus-article-parent-p (number)
2921   "Say whether this article is a parent or not."
2922   (let ((data (gnus-data-find-list number)))
2923     (and (cdr data)              ; There has to be an article after...
2924          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2925             (gnus-data-level (nth 1 data))))))
2926
2927 (defun gnus-article-children (number)
2928   "Return a list of all children to NUMBER."
2929   (let* ((data (gnus-data-find-list number))
2930          (level (gnus-data-level (car data)))
2931          children)
2932     (setq data (cdr data))
2933     (while (and data
2934                 (= (gnus-data-level (car data)) (1+ level)))
2935       (push (gnus-data-number (car data)) children)
2936       (setq data (cdr data)))
2937     children))
2938
2939 (defmacro gnus-summary-skip-intangible ()
2940   "If the current article is intangible, then jump to a different article."
2941   '(let ((to (get-text-property (point) 'gnus-intangible)))
2942      (and to (gnus-summary-goto-subject to))))
2943
2944 (defmacro gnus-summary-article-intangible-p ()
2945   "Say whether this article is intangible or not."
2946   '(get-text-property (point) 'gnus-intangible))
2947
2948 (defun gnus-article-read-p (article)
2949   "Say whether ARTICLE is read or not."
2950   (not (or (memq article gnus-newsgroup-marked)
2951            (memq article gnus-newsgroup-spam-marked)
2952            (memq article gnus-newsgroup-unreads)
2953            (memq article gnus-newsgroup-unselected)
2954            (memq article gnus-newsgroup-dormant))))
2955
2956 ;; Some summary mode macros.
2957
2958 (defmacro gnus-summary-article-number ()
2959   "The article number of the article on the current line.
2960 If there isn't an article number here, then we return the current
2961 article number."
2962   '(progn
2963      (gnus-summary-skip-intangible)
2964      (or (get-text-property (point) 'gnus-number)
2965          (gnus-summary-last-subject))))
2966
2967 (defmacro gnus-summary-article-header (&optional number)
2968   "Return the header of article NUMBER."
2969   `(gnus-data-header (gnus-data-find
2970                       ,(or number '(gnus-summary-article-number)))))
2971
2972 (defmacro gnus-summary-thread-level (&optional number)
2973   "Return the level of thread that starts with article NUMBER."
2974   `(if (and (eq gnus-summary-make-false-root 'dummy)
2975             (get-text-property (point) 'gnus-intangible))
2976        0
2977      (gnus-data-level (gnus-data-find
2978                        ,(or number '(gnus-summary-article-number))))))
2979
2980 (defmacro gnus-summary-article-mark (&optional number)
2981   "Return the mark of article NUMBER."
2982   `(gnus-data-mark (gnus-data-find
2983                     ,(or number '(gnus-summary-article-number)))))
2984
2985 (defmacro gnus-summary-article-pos (&optional number)
2986   "Return the position of the line of article NUMBER."
2987   `(gnus-data-pos (gnus-data-find
2988                    ,(or number '(gnus-summary-article-number)))))
2989
2990 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2991 (defmacro gnus-summary-article-subject (&optional number)
2992   "Return current subject string or nil if nothing."
2993   `(let ((headers
2994           ,(if number
2995                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2996              '(gnus-data-header (assq (gnus-summary-article-number)
2997                                       gnus-newsgroup-data)))))
2998      (and headers
2999           (vectorp headers)
3000           (mail-header-subject headers))))
3001
3002 (defmacro gnus-summary-article-score (&optional number)
3003   "Return current article score."
3004   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3005                   gnus-newsgroup-scored))
3006        gnus-summary-default-score 0))
3007
3008 (defun gnus-summary-article-children (&optional number)
3009   "Return a list of article numbers that are children of article NUMBER."
3010   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3011          (level (gnus-data-level (car data)))
3012          l children)
3013     (while (and (setq data (cdr data))
3014                 (> (setq l (gnus-data-level (car data))) level))
3015       (and (= (1+ level) l)
3016            (push (gnus-data-number (car data))
3017                  children)))
3018     (nreverse children)))
3019
3020 (defun gnus-summary-article-parent (&optional number)
3021   "Return the article number of the parent of article NUMBER."
3022   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3023                                     (gnus-data-list t)))
3024          (level (gnus-data-level (car data))))
3025     (if (zerop level)
3026         ()                              ; This is a root.
3027       ;; We search until we find an article with a level less than
3028       ;; this one.  That function has to be the parent.
3029       (while (and (setq data (cdr data))
3030                   (not (< (gnus-data-level (car data)) level))))
3031       (and data (gnus-data-number (car data))))))
3032
3033 (defun gnus-unread-mark-p (mark)
3034   "Say whether MARK is the unread mark."
3035   (= mark gnus-unread-mark))
3036
3037 (defun gnus-read-mark-p (mark)
3038   "Say whether MARK is one of the marks that mark as read.
3039 This is all marks except unread, ticked, dormant, and expirable."
3040   (not (or (= mark gnus-unread-mark)
3041            (= mark gnus-ticked-mark)
3042            (= mark gnus-spam-mark)
3043            (= mark gnus-dormant-mark)
3044            (= mark gnus-expirable-mark))))
3045
3046 (defmacro gnus-article-mark (number)
3047   "Return the MARK of article NUMBER.
3048 This macro should only be used when computing the mark the \"first\"
3049 time; i.e., when generating the summary lines.  After that,
3050 `gnus-summary-article-mark' should be used to examine the
3051 marks of articles."
3052   `(cond
3053     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3054     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3055     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3056     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3057     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3058     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3059     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3060     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3061            gnus-ancient-mark))))
3062
3063 ;; Saving hidden threads.
3064
3065 (defmacro gnus-save-hidden-threads (&rest forms)
3066   "Save hidden threads, eval FORMS, and restore the hidden threads."
3067   (let ((config (make-symbol "config")))
3068     `(let ((,config (gnus-hidden-threads-configuration)))
3069        (unwind-protect
3070            (save-excursion
3071              ,@forms)
3072          (gnus-restore-hidden-threads-configuration ,config)))))
3073 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3074 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3075
3076 (defun gnus-data-compute-positions ()
3077   "Compute the positions of all articles."
3078   (setq gnus-newsgroup-data-reverse nil)
3079   (let ((data gnus-newsgroup-data))
3080     (save-excursion
3081       (gnus-save-hidden-threads
3082         (gnus-summary-show-all-threads)
3083         (goto-char (point-min))
3084         (while data
3085           (while (get-text-property (point) 'gnus-intangible)
3086             (forward-line 1))
3087           (gnus-data-set-pos (car data) (+ (point) 3))
3088           (setq data (cdr data))
3089           (forward-line 1))))))
3090
3091 (defun gnus-hidden-threads-configuration ()
3092   "Return the current hidden threads configuration."
3093   (save-excursion
3094     (let (config)
3095       (goto-char (point-min))
3096       (while (search-forward "\r" nil t)
3097         (push (1- (point)) config))
3098       config)))
3099
3100 (defun gnus-restore-hidden-threads-configuration (config)
3101   "Restore hidden threads configuration from CONFIG."
3102   (save-excursion
3103     (let (point buffer-read-only)
3104       (while (setq point (pop config))
3105         (when (and (< point (point-max))
3106                    (goto-char point)
3107                    (eq (char-after) ?\n))
3108           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3109
3110 ;; Various summary mode internalish functions.
3111
3112 (defun gnus-mouse-pick-article (e)
3113   (interactive "e")
3114   (mouse-set-point e)
3115   (gnus-summary-next-page nil t))
3116
3117 (defun gnus-summary-set-display-table ()
3118   "Change the display table.
3119 Odd characters have a tendency to mess
3120 up nicely formatted displays - we make all possible glyphs
3121 display only a single character."
3122
3123   ;; We start from the standard display table, if any.
3124   (let ((table (or (copy-sequence standard-display-table)
3125                    (make-display-table)))
3126         (i 32))
3127     ;; Nix out all the control chars...
3128     (while (>= (setq i (1- i)) 0)
3129       (aset table i [??]))
3130    ;; ... but not newline and cr, of course.  (cr is necessary for the
3131     ;; selective display).
3132     (aset table ?\n nil)
3133     (aset table ?\r nil)
3134     ;; We keep TAB as well.
3135     (aset table ?\t nil)
3136     ;; We nix out any glyphs over 126 that are not set already.
3137     (let ((i 256))
3138       (while (>= (setq i (1- i)) 127)
3139         ;; Only modify if the entry is nil.
3140         (unless (aref table i)
3141           (aset table i [??]))))
3142     (setq buffer-display-table table)))
3143
3144 (defun gnus-summary-set-article-display-arrow (pos)
3145   "Update the overlay arrow to point to line at position POS."
3146   (when (and gnus-summary-display-arrow
3147              (boundp 'overlay-arrow-position)
3148              (boundp 'overlay-arrow-string))
3149     (save-excursion
3150       (goto-char pos)
3151       (beginning-of-line)
3152       (unless overlay-arrow-position
3153         (setq overlay-arrow-position (make-marker)))
3154       (setq overlay-arrow-string "=>"
3155             overlay-arrow-position (set-marker overlay-arrow-position
3156                                                (point)
3157                                                (current-buffer))))))
3158
3159 (defun gnus-summary-setup-buffer (group)
3160   "Initialize summary buffer."
3161   (let ((buffer (gnus-summary-buffer-name group))
3162         (dead-name (concat "*Dead Summary "
3163                            (gnus-group-decoded-name group) "*")))
3164     ;; If a dead summary buffer exists, we kill it.
3165     (when (gnus-buffer-live-p dead-name)
3166       (gnus-kill-buffer dead-name))
3167     (if (get-buffer buffer)
3168         (progn
3169           (set-buffer buffer)
3170           (setq gnus-summary-buffer (current-buffer))
3171           (not gnus-newsgroup-prepared))
3172       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3173       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3174       (gnus-summary-mode group)
3175       (when gnus-carpal
3176         (gnus-carpal-setup-buffer 'summary))
3177       (unless gnus-single-article-buffer
3178         (make-local-variable 'gnus-article-buffer)
3179         (make-local-variable 'gnus-article-current)
3180         (make-local-variable 'gnus-original-article-buffer))
3181       (setq gnus-newsgroup-name group)
3182       ;; Set any local variables in the group parameters.
3183       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3184       t)))
3185
3186 (defun gnus-set-global-variables ()
3187   "Set the global equivalents of the buffer-local variables.
3188 They are set to the latest values they had.  These reflect the summary
3189 buffer that was in action when the last article was fetched."
3190   (when (eq major-mode 'gnus-summary-mode)
3191     (setq gnus-summary-buffer (current-buffer))
3192     (let ((name gnus-newsgroup-name)
3193           (marked gnus-newsgroup-marked)
3194           (spam gnus-newsgroup-spam-marked)
3195           (unread gnus-newsgroup-unreads)
3196           (headers gnus-current-headers)
3197           (data gnus-newsgroup-data)
3198           (summary gnus-summary-buffer)
3199           (article-buffer gnus-article-buffer)
3200           (original gnus-original-article-buffer)
3201           (gac gnus-article-current)
3202           (reffed gnus-reffed-article-number)
3203           (score-file gnus-current-score-file)
3204           (default-charset gnus-newsgroup-charset)
3205           vlist)
3206       (let ((locals gnus-newsgroup-variables))
3207         (while locals
3208           (if (consp (car locals))
3209               (push (eval (caar locals)) vlist)
3210             (push (eval (car locals)) vlist))
3211           (setq locals (cdr locals)))
3212         (setq vlist (nreverse vlist)))
3213       (with-current-buffer gnus-group-buffer
3214         (setq gnus-newsgroup-name name
3215               gnus-newsgroup-marked marked
3216               gnus-newsgroup-spam-marked spam
3217               gnus-newsgroup-unreads unread
3218               gnus-current-headers headers
3219               gnus-newsgroup-data data
3220               gnus-article-current gac
3221               gnus-summary-buffer summary
3222               gnus-article-buffer article-buffer
3223               gnus-original-article-buffer original
3224               gnus-reffed-article-number reffed
3225               gnus-current-score-file score-file
3226               gnus-newsgroup-charset default-charset)
3227         (let ((locals gnus-newsgroup-variables))
3228           (while locals
3229             (if (consp (car locals))
3230                 (set (caar locals) (pop vlist))
3231               (set (car locals) (pop vlist)))
3232             (setq locals (cdr locals))))
3233         ;; The article buffer also has local variables.
3234         (when (gnus-buffer-live-p gnus-article-buffer)
3235           (set-buffer gnus-article-buffer)
3236           (setq gnus-summary-buffer summary))))))
3237
3238 (defun gnus-summary-article-unread-p (article)
3239   "Say whether ARTICLE is unread or not."
3240   (memq article gnus-newsgroup-unreads))
3241
3242 (defun gnus-summary-first-article-p (&optional article)
3243   "Return whether ARTICLE is the first article in the buffer."
3244   (if (not (setq article (or article (gnus-summary-article-number))))
3245       nil
3246     (eq article (caar gnus-newsgroup-data))))
3247
3248 (defun gnus-summary-last-article-p (&optional article)
3249   "Return whether ARTICLE is the last article in the buffer."
3250   (if (not (setq article (or article (gnus-summary-article-number))))
3251       ;; All non-existent numbers are the last article.  :-)
3252       t
3253     (not (cdr (gnus-data-find-list article)))))
3254
3255 (defun gnus-make-thread-indent-array ()
3256   (let ((n 200))
3257     (unless (and gnus-thread-indent-array
3258                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3259       (setq gnus-thread-indent-array (make-vector 201 "")
3260             gnus-thread-indent-array-level gnus-thread-indent-level)
3261       (while (>= n 0)
3262         (aset gnus-thread-indent-array n
3263               (make-string (* n gnus-thread-indent-level) ? ))
3264         (setq n (1- n))))))
3265
3266 (defun gnus-update-summary-mark-positions ()
3267   "Compute where the summary marks are to go."
3268   (save-excursion
3269     (when (gnus-buffer-exists-p gnus-summary-buffer)
3270       (set-buffer gnus-summary-buffer))
3271     (let ((gnus-replied-mark 129)
3272           (gnus-score-below-mark 130)
3273           (gnus-score-over-mark 130)
3274           (gnus-undownloaded-mark 131)
3275           (spec gnus-summary-line-format-spec)
3276           gnus-visual pos)
3277       (save-excursion
3278         (gnus-set-work-buffer)
3279         (let ((gnus-summary-line-format-spec spec)
3280               (gnus-newsgroup-downloadable '(0)))
3281           (gnus-summary-insert-line
3282            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3283            0 nil t 128 t nil "" nil 1)
3284           (goto-char (point-min))
3285           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3286                                              (- (point) (point-min) 1)))))
3287           (goto-char (point-min))
3288           (push (cons 'replied (and (search-forward "\201" nil t)
3289                                     (- (point) (point-min) 1)))
3290                 pos)
3291           (goto-char (point-min))
3292           (push (cons 'score (and (search-forward "\202" nil t)
3293                                   (- (point) (point-min) 1)))
3294                 pos)
3295           (goto-char (point-min))
3296           (push (cons 'download
3297                       (and (search-forward "\203" nil t)
3298                            (- (point) (point-min) 1)))
3299                 pos)))
3300       (setq gnus-summary-mark-positions pos))))
3301
3302 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3303   "Insert a dummy root in the summary buffer."
3304   (beginning-of-line)
3305   (gnus-add-text-properties
3306    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3307    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3308
3309 (defun gnus-summary-extract-address-component (from)
3310   (or (car (funcall gnus-extract-address-components from))
3311       from))
3312
3313 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3314   (let ((mail-parse-charset gnus-newsgroup-charset)
3315         ; Is it really necessary to do this next part for each summary line?
3316         ; Luckily, doesn't seem to slow things down much.
3317         (mail-parse-ignored-charsets
3318          (with-current-buffer gnus-summary-buffer
3319            gnus-newsgroup-ignored-charsets)))
3320     (or
3321      (and gnus-ignored-from-addresses
3322           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3323           (let ((extra-headers (mail-header-extra header))
3324                 to
3325                 newsgroups)
3326             (cond
3327              ((setq to (cdr (assq 'To extra-headers)))
3328               (concat "-> "
3329                       (inline
3330                         (gnus-summary-extract-address-component
3331                          (funcall gnus-decode-encoded-word-function to)))))
3332              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3333               (concat "=> " newsgroups)))))
3334      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3335
3336 (defun gnus-summary-insert-line (gnus-tmp-header
3337                                  gnus-tmp-level gnus-tmp-current
3338                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3339                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3340                                  &optional gnus-tmp-dummy gnus-tmp-score
3341                                  gnus-tmp-process)
3342   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3343          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3344          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3345          (gnus-tmp-score-char
3346           (if (or (null gnus-summary-default-score)
3347                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3348                       gnus-summary-zcore-fuzz))
3349               ?                         ;Whitespace
3350             (if (< gnus-tmp-score gnus-summary-default-score)
3351                 gnus-score-below-mark gnus-score-over-mark)))
3352          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3353          (gnus-tmp-replied
3354           (cond (gnus-tmp-process gnus-process-mark)
3355                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3356                  gnus-cached-mark)
3357                 (gnus-tmp-replied gnus-replied-mark)
3358                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3359                  gnus-forwarded-mark)
3360                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3361                  gnus-saved-mark)
3362                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3363                  gnus-recent-mark)
3364                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3365                  gnus-unseen-mark)
3366                 (t gnus-no-mark)))
3367          (gnus-tmp-downloaded
3368           (cond (undownloaded
3369                  gnus-undownloaded-mark)
3370                 (gnus-newsgroup-agentized
3371                  gnus-downloaded-mark)
3372                 (t
3373                  gnus-no-mark)))
3374          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3375          (gnus-tmp-name
3376           (cond
3377            ((string-match "<[^>]+> *$" gnus-tmp-from)
3378             (let ((beg (match-beginning 0)))
3379               (or (and (string-match "^\".+\"" gnus-tmp-from)
3380                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3381                   (substring gnus-tmp-from 0 beg))))
3382            ((string-match "(.+)" gnus-tmp-from)
3383             (substring gnus-tmp-from
3384                        (1+ (match-beginning 0)) (1- (match-end 0))))
3385            (t gnus-tmp-from)))
3386          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3387          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3388          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3389          (buffer-read-only nil))
3390     (when (string= gnus-tmp-name "")
3391       (setq gnus-tmp-name gnus-tmp-from))
3392     (unless (numberp gnus-tmp-lines)
3393       (setq gnus-tmp-lines -1))
3394     (if (= gnus-tmp-lines -1)
3395         (setq gnus-tmp-lines "?")
3396       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3397       (gnus-put-text-property
3398      (point)
3399      (progn (eval gnus-summary-line-format-spec) (point))
3400        'gnus-number gnus-tmp-number)
3401     (when (gnus-visual-p 'summary-highlight 'highlight)
3402       (forward-line -1)
3403       (gnus-run-hooks 'gnus-summary-update-hook)
3404       (forward-line 1))))
3405
3406 (defun gnus-summary-update-line (&optional dont-update)
3407   "Update summary line after change."
3408   (when (and gnus-summary-default-score
3409              (not gnus-summary-inhibit-highlight))
3410     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3411            (article (gnus-summary-article-number))
3412            (score (gnus-summary-article-score article)))
3413       (unless dont-update
3414         (if (and gnus-summary-mark-below
3415                  (< (gnus-summary-article-score)
3416                     gnus-summary-mark-below))
3417             ;; This article has a low score, so we mark it as read.
3418             (when (memq article gnus-newsgroup-unreads)
3419               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3420           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3421             ;; This article was previously marked as read on account
3422             ;; of a low score, but now it has risen, so we mark it as
3423             ;; unread.
3424             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3425         (gnus-summary-update-mark
3426          (if (or (null gnus-summary-default-score)
3427                  (<= (abs (- score gnus-summary-default-score))
3428                      gnus-summary-zcore-fuzz))
3429              ?                          ;Whitespace
3430            (if (< score gnus-summary-default-score)
3431                gnus-score-below-mark gnus-score-over-mark))
3432          'score))
3433       ;; Do visual highlighting.
3434       (when (gnus-visual-p 'summary-highlight 'highlight)
3435         (gnus-run-hooks 'gnus-summary-update-hook)))))
3436
3437 (defvar gnus-tmp-new-adopts nil)
3438
3439 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3440   "Return the number of articles in THREAD.
3441 This may be 0 in some cases -- if none of the articles in
3442 the thread are to be displayed."
3443   (let* ((number
3444          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3445           (cond
3446            ((not (listp thread))
3447             1)
3448            ((and (consp thread) (cdr thread))
3449             (apply
3450              '+ 1 (mapcar
3451                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3452            ((null thread)
3453             1)
3454            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3455             1)
3456            (t 0))))
3457     (when (and level (zerop level) gnus-tmp-new-adopts)
3458       (incf number
3459             (apply '+ (mapcar
3460                        'gnus-summary-number-of-articles-in-thread
3461                        gnus-tmp-new-adopts))))
3462     (if char
3463         (if (> number 1) gnus-not-empty-thread-mark
3464           gnus-empty-thread-mark)
3465       number)))
3466
3467 (defsubst gnus-summary-line-message-size (head)
3468   "Return pretty-printed version of message size.
3469 This function is intended to be used in
3470 `gnus-summary-line-format-alist'."
3471   (let ((c (or (mail-header-chars head) -1)))
3472     (cond ((< c 0) "n/a")               ; chars not available
3473           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3474           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3475           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3476           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3477
3478
3479 (defun gnus-summary-set-local-parameters (group)
3480   "Go through the local params of GROUP and set all variable specs in that list."
3481   (let ((params (gnus-group-find-parameter group))
3482         (vars '(quit-config))           ; Ignore quit-config.
3483         elem)
3484     (while params
3485       (setq elem (car params)
3486             params (cdr params))
3487       (and (consp elem)                 ; Has to be a cons.
3488            (consp (cdr elem))           ; The cdr has to be a list.
3489            (symbolp (car elem))         ; Has to be a symbol in there.
3490            (not (memq (car elem) vars))
3491            (ignore-errors               ; So we set it.
3492              (push (car elem) vars)
3493              (make-local-variable (car elem))
3494              (set (car elem) (eval (nth 1 elem))))))))
3495
3496 (defun gnus-summary-read-group (group &optional show-all no-article
3497                                       kill-buffer no-display backward
3498                                       select-articles)
3499   "Start reading news in newsgroup GROUP.
3500 If SHOW-ALL is non-nil, already read articles are also listed.
3501 If NO-ARTICLE is non-nil, no article is selected initially.
3502 If NO-DISPLAY, don't generate a summary buffer."
3503   (let (result)
3504     (while (and group
3505                 (null (setq result
3506                             (let ((gnus-auto-select-next nil))
3507                               (or (gnus-summary-read-group-1
3508                                    group show-all no-article
3509                                    kill-buffer no-display
3510                                    select-articles)
3511                                   (setq show-all nil
3512                                         select-articles nil)))))
3513                 (eq gnus-auto-select-next 'quietly))
3514       (set-buffer gnus-group-buffer)
3515       ;; The entry function called above goes to the next
3516       ;; group automatically, so we go two groups back
3517       ;; if we are searching for the previous group.
3518       (when backward
3519         (gnus-group-prev-unread-group 2))
3520       (if (not (equal group (gnus-group-group-name)))
3521           (setq group (gnus-group-group-name))
3522         (setq group nil)))
3523     result))
3524
3525 (defun gnus-summary-read-group-1 (group show-all no-article
3526                                         kill-buffer no-display
3527                                         &optional select-articles)
3528   ;; Killed foreign groups can't be entered.
3529   ;;  (when (and (not (gnus-group-native-p group))
3530   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3531   ;;    (error "Dead non-native groups can't be entered"))
3532   (gnus-message 5 "Retrieving newsgroup: %s..."
3533                 (gnus-group-decoded-name group))
3534   (let* ((new-group (gnus-summary-setup-buffer group))
3535          (quit-config (gnus-group-quit-config group))
3536          (did-select (and new-group (gnus-select-newsgroup
3537                                      group show-all select-articles))))
3538     (cond
3539      ;; This summary buffer exists already, so we just select it.
3540      ((not new-group)
3541       (gnus-set-global-variables)
3542       (when kill-buffer
3543         (gnus-kill-or-deaden-summary kill-buffer))
3544       (gnus-configure-windows 'summary 'force)
3545       (gnus-set-mode-line 'summary)
3546       (gnus-summary-position-point)
3547       (message "")
3548       t)
3549      ;; We couldn't select this group.
3550      ((null did-select)
3551       (when (and (eq major-mode 'gnus-summary-mode)
3552                  (not (equal (current-buffer) kill-buffer)))
3553         (kill-buffer (current-buffer))
3554         (if (not quit-config)
3555             (progn
3556               ;; Update the info -- marks might need to be removed,
3557               ;; for instance.
3558               (gnus-summary-update-info)
3559               (set-buffer gnus-group-buffer)
3560               (gnus-group-jump-to-group group)
3561               (gnus-group-next-unread-group 1))
3562           (gnus-handle-ephemeral-exit quit-config)))
3563       (let ((grpinfo (gnus-get-info group)))
3564         (if (null (gnus-info-read grpinfo))
3565             (gnus-message 3 "Group %s contains no messages"
3566                           (gnus-group-decoded-name group))
3567           (gnus-message 3 "Can't select group")))
3568       nil)
3569      ;; The user did a `C-g' while prompting for number of articles,
3570      ;; so we exit this group.
3571      ((eq did-select 'quit)
3572       (and (eq major-mode 'gnus-summary-mode)
3573            (not (equal (current-buffer) kill-buffer))
3574            (kill-buffer (current-buffer)))
3575       (when kill-buffer
3576         (gnus-kill-or-deaden-summary kill-buffer))
3577       (if (not quit-config)
3578           (progn
3579             (set-buffer gnus-group-buffer)
3580             (gnus-group-jump-to-group group)
3581             (gnus-group-next-unread-group 1)
3582             (gnus-configure-windows 'group 'force))
3583         (gnus-handle-ephemeral-exit quit-config))
3584       ;; Finally signal the quit.
3585       (signal 'quit nil))
3586      ;; The group was successfully selected.
3587      (t
3588       (gnus-set-global-variables)
3589       ;; Save the active value in effect when the group was entered.
3590       (setq gnus-newsgroup-active
3591             (gnus-copy-sequence
3592              (gnus-active gnus-newsgroup-name)))
3593       ;; You can change the summary buffer in some way with this hook.
3594       (gnus-run-hooks 'gnus-select-group-hook)
3595       (gnus-update-format-specifications
3596        nil 'summary 'summary-mode 'summary-dummy)
3597       (gnus-update-summary-mark-positions)
3598       ;; Do score processing.
3599       (when gnus-use-scoring
3600         (gnus-possibly-score-headers))
3601       ;; Check whether to fill in the gaps in the threads.
3602       (when gnus-build-sparse-threads
3603         (gnus-build-sparse-threads))
3604       ;; Find the initial limit.
3605       (if show-all
3606           (let ((gnus-newsgroup-dormant nil))
3607             (gnus-summary-initial-limit show-all))
3608         (gnus-summary-initial-limit show-all))
3609       ;; Generate the summary buffer.
3610       (unless no-display
3611         (gnus-summary-prepare))
3612       (when gnus-use-trees
3613         (gnus-tree-open group)
3614         (setq gnus-summary-highlight-line-function
3615               'gnus-tree-highlight-article))
3616       ;; If the summary buffer is empty, but there are some low-scored
3617       ;; articles or some excluded dormants, we include these in the
3618       ;; buffer.
3619       (when (and (zerop (buffer-size))
3620                  (not no-display))
3621         (cond (gnus-newsgroup-dormant
3622                (gnus-summary-limit-include-dormant))
3623               ((and gnus-newsgroup-scored show-all)
3624                (gnus-summary-limit-include-expunged t))))
3625       ;; Function `gnus-apply-kill-file' must be called in this hook.
3626       (gnus-run-hooks 'gnus-apply-kill-hook)
3627       (if (and (zerop (buffer-size))
3628                (not no-display))
3629           (progn
3630             ;; This newsgroup is empty.
3631             (gnus-summary-catchup-and-exit nil t)
3632             (gnus-message 6 "No unread news")
3633             (when kill-buffer
3634               (gnus-kill-or-deaden-summary kill-buffer))
3635             ;; Return nil from this function.
3636             nil)
3637         ;; Hide conversation thread subtrees.  We cannot do this in
3638         ;; gnus-summary-prepare-hook since kill processing may not
3639         ;; work with hidden articles.
3640         (gnus-summary-maybe-hide-threads)
3641         (when kill-buffer
3642           (gnus-kill-or-deaden-summary kill-buffer))
3643         (gnus-summary-auto-select-subject)
3644         ;; Show first unread article if requested.
3645         (if (and (not no-article)
3646                  (not no-display)
3647                  gnus-newsgroup-unreads
3648                  gnus-auto-select-first)
3649             (progn
3650               (gnus-configure-windows 'summary)
3651               (let ((art (gnus-summary-article-number)))
3652                 (unless (and (not gnus-plugged)
3653                              (or (memq art gnus-newsgroup-undownloaded)
3654                                  (memq art gnus-newsgroup-downloadable)))
3655                   (gnus-summary-goto-article art))))
3656           ;; Don't select any articles.
3657           (gnus-summary-position-point)
3658           (gnus-configure-windows 'summary 'force)
3659           (gnus-set-mode-line 'summary))
3660         (when (and gnus-auto-center-group
3661                    (get-buffer-window gnus-group-buffer t))
3662           ;; Gotta use windows, because recenter does weird stuff if
3663           ;; the current buffer ain't the displayed window.
3664           (let ((owin (selected-window)))
3665             (select-window (get-buffer-window gnus-group-buffer t))
3666             (when (gnus-group-goto-group group)
3667               (recenter))
3668             (select-window owin)))
3669         ;; Mark this buffer as "prepared".
3670         (setq gnus-newsgroup-prepared t)
3671         (gnus-run-hooks 'gnus-summary-prepared-hook)
3672         (unless (gnus-ephemeral-group-p group)
3673           (gnus-group-update-group group))
3674         t)))))
3675
3676 (defun gnus-summary-auto-select-subject ()
3677   "Select the subject line on initial group entry."
3678   (goto-char (point-min))
3679   (cond
3680    ((eq gnus-auto-select-subject 'best)
3681     (gnus-summary-best-unread-subject))
3682    ((eq gnus-auto-select-subject 'unread)
3683     (gnus-summary-first-unread-subject))
3684    ((eq gnus-auto-select-subject 'unseen)
3685     (gnus-summary-first-unseen-subject))
3686    ((eq gnus-auto-select-subject 'unseen-or-unread)
3687     (gnus-summary-first-unseen-or-unread-subject))
3688    ((eq gnus-auto-select-subject 'first)
3689     ;; Do nothing.
3690     )
3691    ((functionp gnus-auto-select-subject)
3692     (funcall gnus-auto-select-subject))))
3693
3694 (defun gnus-summary-prepare ()
3695   "Generate the summary buffer."
3696   (interactive)
3697   (let ((buffer-read-only nil))
3698     (erase-buffer)
3699     (setq gnus-newsgroup-data nil
3700           gnus-newsgroup-data-reverse nil)
3701     (gnus-run-hooks 'gnus-summary-generate-hook)
3702     ;; Generate the buffer, either with threads or without.
3703     (when gnus-newsgroup-headers
3704       (gnus-summary-prepare-threads
3705        (if gnus-show-threads
3706            (gnus-sort-gathered-threads
3707             (funcall gnus-summary-thread-gathering-function
3708                      (gnus-sort-threads
3709                       (gnus-cut-threads (gnus-make-threads)))))
3710          ;; Unthreaded display.
3711          (gnus-sort-articles gnus-newsgroup-headers))))
3712     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3713     ;; Call hooks for modifying summary buffer.
3714     (goto-char (point-min))
3715     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3716
3717 (defsubst gnus-general-simplify-subject (subject)
3718   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3719   (setq subject
3720         (cond
3721          ;; Truncate the subject.
3722          (gnus-simplify-subject-functions
3723           (gnus-map-function gnus-simplify-subject-functions subject))
3724          ((numberp gnus-summary-gather-subject-limit)
3725           (setq subject (gnus-simplify-subject-re subject))
3726           (if (> (length subject) gnus-summary-gather-subject-limit)
3727               (substring subject 0 gnus-summary-gather-subject-limit)
3728             subject))
3729          ;; Fuzzily simplify it.
3730          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3731           (gnus-simplify-subject-fuzzy subject))
3732          ;; Just remove the leading "Re:".
3733          (t
3734           (gnus-simplify-subject-re subject))))
3735
3736   (if (and gnus-summary-gather-exclude-subject
3737            (string-match gnus-summary-gather-exclude-subject subject))
3738       nil                         ; This article shouldn't be gathered
3739     subject))
3740
3741 (defun gnus-summary-simplify-subject-query ()
3742   "Query where the respool algorithm would put this article."
3743   (interactive)
3744   (gnus-summary-select-article)
3745   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3746
3747 (defun gnus-gather-threads-by-subject (threads)
3748   "Gather threads by looking at Subject headers."
3749   (if (not gnus-summary-make-false-root)
3750       threads
3751     (let ((hashtb (gnus-make-hashtable 1024))
3752           (prev threads)
3753           (result threads)
3754           subject hthread whole-subject)
3755       (while threads
3756         (setq subject (gnus-general-simplify-subject
3757                        (setq whole-subject (mail-header-subject
3758                                             (caar threads)))))
3759         (when subject
3760           (if (setq hthread (gnus-gethash subject hashtb))
3761               (progn
3762                 ;; We enter a dummy root into the thread, if we
3763                 ;; haven't done that already.
3764                 (unless (stringp (caar hthread))
3765                   (setcar hthread (list whole-subject (car hthread))))
3766                 ;; We add this new gathered thread to this gathered
3767                 ;; thread.
3768                 (setcdr (car hthread)
3769                         (nconc (cdar hthread) (list (car threads))))
3770                 ;; Remove it from the list of threads.
3771                 (setcdr prev (cdr threads))
3772                 (setq threads prev))
3773             ;; Enter this thread into the hash table.
3774             (gnus-sethash subject
3775                           (if gnus-summary-make-false-root-always
3776                               (progn
3777                                 ;; If you want a dummy root above all
3778                                 ;; threads...
3779                                 (setcar threads (list whole-subject
3780                                                       (car threads)))
3781                                 threads)
3782                             threads)
3783                           hashtb)))
3784         (setq prev threads)
3785         (setq threads (cdr threads)))
3786       result)))
3787
3788 (defun gnus-gather-threads-by-references (threads)
3789   "Gather threads by looking at References headers."
3790   (let ((idhashtb (gnus-make-hashtable 1024))
3791         (thhashtb (gnus-make-hashtable 1024))
3792         (prev threads)
3793         (result threads)
3794         ids references id gthread gid entered ref)
3795     (while threads
3796       (when (setq references (mail-header-references (caar threads)))
3797         (setq id (mail-header-id (caar threads))
3798               ids (inline (gnus-split-references references))
3799               entered nil)
3800         (while (setq ref (pop ids))
3801           (setq ids (delete ref ids))
3802           (if (not (setq gid (gnus-gethash ref idhashtb)))
3803               (progn
3804                 (gnus-sethash ref id idhashtb)
3805                 (gnus-sethash id threads thhashtb))
3806             (setq gthread (gnus-gethash gid thhashtb))
3807             (unless entered
3808               ;; We enter a dummy root into the thread, if we
3809               ;; haven't done that already.
3810               (unless (stringp (caar gthread))
3811                 (setcar gthread (list (mail-header-subject (caar gthread))
3812                                       (car gthread))))
3813               ;; We add this new gathered thread to this gathered
3814               ;; thread.
3815               (setcdr (car gthread)
3816                       (nconc (cdar gthread) (list (car threads)))))
3817             ;; Add it into the thread hash table.
3818             (gnus-sethash id gthread thhashtb)
3819             (setq entered t)
3820             ;; Remove it from the list of threads.
3821             (setcdr prev (cdr threads))
3822             (setq threads prev))))
3823       (setq prev threads)
3824       (setq threads (cdr threads)))
3825     result))
3826
3827 (defun gnus-sort-gathered-threads (threads)
3828   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3829   (let ((result threads))
3830     (while threads
3831       (when (stringp (caar threads))
3832         (setcdr (car threads)
3833                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3834       (setq threads (cdr threads)))
3835     result))
3836
3837 (defun gnus-thread-loop-p (root thread)
3838   "Say whether ROOT is in THREAD."
3839   (let ((stack (list thread))
3840         (infloop 0)
3841         th)
3842     (while (setq thread (pop stack))
3843       (setq th (cdr thread))
3844       (while (and th
3845                   (not (eq (caar th) root)))
3846         (pop th))
3847       (if th
3848           ;; We have found a loop.
3849           (let (ref-dep)
3850             (setcdr thread (delq (car th) (cdr thread)))
3851             (if (boundp (setq ref-dep (intern "none"
3852                                               gnus-newsgroup-dependencies)))
3853                 (setcdr (symbol-value ref-dep)
3854                         (nconc (cdr (symbol-value ref-dep))
3855                                (list (car th))))
3856               (set ref-dep (list nil (car th))))
3857             (setq infloop 1
3858                   stack nil))
3859         ;; Push all the subthreads onto the stack.
3860         (push (cdr thread) stack)))
3861     infloop))
3862
3863 (defun gnus-make-threads ()
3864   "Go through the dependency hashtb and find the roots.  Return all threads."
3865   (let (threads)
3866     (while (catch 'infloop
3867              (mapatoms
3868               (lambda (refs)
3869                 ;; Deal with self-referencing References loops.
3870                 (when (and (car (symbol-value refs))
3871                            (not (zerop
3872                                  (apply
3873                                   '+
3874                                   (mapcar
3875                                    (lambda (thread)
3876                                      (gnus-thread-loop-p
3877                                       (car (symbol-value refs)) thread))
3878                                    (cdr (symbol-value refs)))))))
3879                   (setq threads nil)
3880                   (throw 'infloop t))
3881                 (unless (car (symbol-value refs))
3882                   ;; These threads do not refer back to any other
3883                   ;; articles, so they're roots.
3884                   (setq threads (append (cdr (symbol-value refs)) threads))))
3885               gnus-newsgroup-dependencies)))
3886     threads))
3887
3888 ;; Build the thread tree.
3889 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3890   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3891
3892 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3893 if it was already present.
3894
3895 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3896 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3897 Message-IDs will be renamed to a unique Message-ID before being
3898 entered.
3899
3900 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3901   (let* ((id (mail-header-id header))
3902          (id-dep (and id (intern id dependencies)))
3903          parent-id ref ref-dep ref-header replaced)
3904     ;; Enter this `header' in the `dependencies' table.
3905     (cond
3906      ((not id-dep)
3907       (setq header nil))
3908      ;; The first two cases do the normal part: enter a new `header'
3909      ;; in the `dependencies' table.
3910      ((not (boundp id-dep))
3911       (set id-dep (list header)))
3912      ((null (car (symbol-value id-dep)))
3913       (setcar (symbol-value id-dep) header))
3914
3915      ;; From here the `header' was already present in the
3916      ;; `dependencies' table.
3917      (force-new
3918       ;; Overrides an existing entry;
3919       ;; just set the header part of the entry.
3920       (setcar (symbol-value id-dep) header)
3921       (setq replaced t))
3922
3923      ;; Renames the existing `header' to a unique Message-ID.
3924      ((not gnus-summary-ignore-duplicates)
3925       ;; An article with this Message-ID has already been seen.
3926       ;; We rename the Message-ID.
3927       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3928            (list header))
3929       (mail-header-set-id header id))
3930
3931      ;; The last case ignores an existing entry, except it adds any
3932      ;; additional Xrefs (in case the two articles came from different
3933      ;; servers.
3934      ;; Also sets `header' to `nil' meaning that the `dependencies'
3935      ;; table was *not* modified.
3936      (t
3937       (mail-header-set-xref
3938        (car (symbol-value id-dep))
3939        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3940                    "")
3941                (or (mail-header-xref header) "")))
3942       (setq header nil)))
3943
3944     (when (and header (not replaced))
3945       ;; First check that we are not creating a References loop.
3946       (setq parent-id (gnus-parent-id (mail-header-references header)))
3947       (setq ref parent-id)
3948       (while (and ref
3949                   (setq ref-dep (intern-soft ref dependencies))
3950                   (boundp ref-dep)
3951                   (setq ref-header (car (symbol-value ref-dep))))
3952         (if (string= id ref)
3953             ;; Yuk!  This is a reference loop.  Make the article be a
3954             ;; root article.
3955             (progn
3956               (mail-header-set-references (car (symbol-value id-dep)) "none")
3957               (setq ref nil)
3958               (setq parent-id nil))
3959           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3960       (setq ref-dep (intern (or parent-id "none") dependencies))
3961       (if (boundp ref-dep)
3962           (setcdr (symbol-value ref-dep)
3963                   (nconc (cdr (symbol-value ref-dep))
3964                          (list (symbol-value id-dep))))
3965         (set ref-dep (list nil (symbol-value id-dep)))))
3966     header))
3967
3968 (defun gnus-extract-message-id-from-in-reply-to (string)
3969   (if (string-match "<[^>]+>" string)
3970       (substring string (match-beginning 0) (match-end 0))
3971     nil))
3972
3973 (defun gnus-build-sparse-threads ()
3974   (let ((headers gnus-newsgroup-headers)
3975         (mail-parse-charset gnus-newsgroup-charset)
3976         (gnus-summary-ignore-duplicates t)
3977         header references generation relations
3978         subject child end new-child date)
3979     ;; First we create an alist of generations/relations, where
3980     ;; generations is how much we trust the relation, and the relation
3981     ;; is parent/child.
3982     (gnus-message 7 "Making sparse threads...")
3983     (save-excursion
3984       (nnheader-set-temp-buffer " *gnus sparse threads*")
3985       (while (setq header (pop headers))
3986         (when (and (setq references (mail-header-references header))
3987                    (not (string= references "")))
3988           (insert references)
3989           (setq child (mail-header-id header)
3990                 subject (mail-header-subject header)
3991                 date (mail-header-date header)
3992                 generation 0)
3993           (while (search-backward ">" nil t)
3994             (setq end (1+ (point)))
3995             (when (search-backward "<" nil t)
3996               (setq new-child (buffer-substring (point) end))
3997               (push (list (incf generation)
3998                           child (setq child new-child)
3999                           subject date)
4000                     relations)))
4001           (when child
4002             (push (list (1+ generation) child nil subject) relations))
4003           (erase-buffer)))
4004       (kill-buffer (current-buffer)))
4005     ;; Sort over trustworthiness.
4006     (mapcar
4007      (lambda (relation)
4008        (when (gnus-dependencies-add-header
4009               (make-full-mail-header
4010                gnus-reffed-article-number
4011                (nth 3 relation) "" (or (nth 4 relation) "")
4012                (nth 1 relation)
4013                (or (nth 2 relation) "") 0 0 "")
4014               gnus-newsgroup-dependencies nil)
4015          (push gnus-reffed-article-number gnus-newsgroup-limit)
4016          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4017          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4018                gnus-newsgroup-reads)
4019          (decf gnus-reffed-article-number)))
4020      (sort relations 'car-less-than-car))
4021     (gnus-message 7 "Making sparse threads...done")))
4022
4023 (defun gnus-build-old-threads ()
4024   ;; Look at all the articles that refer back to old articles, and
4025   ;; fetch the headers for the articles that aren't there.  This will
4026   ;; build complete threads - if the roots haven't been expired by the
4027   ;; server, that is.
4028   (let ((mail-parse-charset gnus-newsgroup-charset)
4029         id heads)
4030     (mapatoms
4031      (lambda (refs)
4032        (when (not (car (symbol-value refs)))
4033          (setq heads (cdr (symbol-value refs)))
4034          (while heads
4035            (if (memq (mail-header-number (caar heads))
4036                      gnus-newsgroup-dormant)
4037                (setq heads (cdr heads))
4038              (setq id (symbol-name refs))
4039              (while (and (setq id (gnus-build-get-header id))
4040                          (not (car (gnus-id-to-thread id)))))
4041              (setq heads nil)))))
4042      gnus-newsgroup-dependencies)))
4043
4044 (defsubst gnus-remove-odd-characters (string)
4045   "Translate STRING into something that doesn't contain weird characters."
4046   (mm-subst-char-in-string
4047    ?\r ?\-
4048    (mm-subst-char-in-string ?\n ?\- string t) t))
4049
4050 ;; This function has to be called with point after the article number
4051 ;; on the beginning of the line.
4052 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4053   (let ((eol (point-at-eol))
4054         (buffer (current-buffer))
4055         header references in-reply-to)
4056
4057     ;; overview: [num subject from date id refs chars lines misc]
4058     (unwind-protect
4059         (let (x)
4060           (narrow-to-region (point) eol)
4061           (unless (eobp)
4062             (forward-char))
4063
4064           (setq header
4065                 (make-full-mail-header
4066                  number                 ; number
4067                  (condition-case ()     ; subject
4068                      (gnus-remove-odd-characters
4069                       (funcall gnus-decode-encoded-word-function
4070                                (setq x (nnheader-nov-field))))
4071                    (error x))
4072                  (condition-case ()     ; from
4073                      (gnus-remove-odd-characters
4074                       (funcall gnus-decode-encoded-word-function
4075                                (setq x (nnheader-nov-field))))
4076                    (error x))
4077                  (nnheader-nov-field)   ; date
4078                  (nnheader-nov-read-message-id number)  ; id
4079                  (setq references (nnheader-nov-field)) ; refs
4080                  (nnheader-nov-read-integer) ; chars
4081                  (nnheader-nov-read-integer) ; lines
4082                  (unless (eobp)
4083                    (if (looking-at "Xref: ")
4084                        (goto-char (match-end 0)))
4085                    (nnheader-nov-field)) ; Xref
4086                  (nnheader-nov-parse-extra)))) ; extra
4087
4088       (widen))
4089
4090     (when (and (string= references "")
4091                (setq in-reply-to (mail-header-extra header))
4092                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4093       (mail-header-set-references
4094        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4095
4096     (when gnus-alter-header-function
4097       (funcall gnus-alter-header-function header))
4098     (gnus-dependencies-add-header header dependencies force-new)))
4099
4100 (defun gnus-build-get-header (id)
4101   "Look through the buffer of NOV lines and find the header to ID.
4102 Enter this line into the dependencies hash table, and return
4103 the id of the parent article (if any)."
4104   (let ((deps gnus-newsgroup-dependencies)
4105         found header)
4106     (prog1
4107         (save-excursion
4108           (set-buffer nntp-server-buffer)
4109           (let ((case-fold-search nil))
4110             (goto-char (point-min))
4111             (while (and (not found)
4112                         (search-forward id nil t))
4113               (beginning-of-line)
4114               (setq found (looking-at
4115                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4116                                    (regexp-quote id))))
4117               (or found (beginning-of-line 2)))
4118             (when found
4119               (beginning-of-line)
4120               (and
4121                (setq header (gnus-nov-parse-line
4122                              (read (current-buffer)) deps))
4123                (gnus-parent-id (mail-header-references header))))))
4124       (when header
4125         (let ((number (mail-header-number header)))
4126           (push number gnus-newsgroup-limit)
4127           (push header gnus-newsgroup-headers)
4128           (if (memq number gnus-newsgroup-unselected)
4129               (progn
4130                 (setq gnus-newsgroup-unreads
4131                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4132                                                number))
4133                 (setq gnus-newsgroup-unselected
4134                       (delq number gnus-newsgroup-unselected)))
4135             (push number gnus-newsgroup-ancient)))))))
4136
4137 (defun gnus-build-all-threads ()
4138   "Read all the headers."
4139   (let ((gnus-summary-ignore-duplicates t)
4140         (mail-parse-charset gnus-newsgroup-charset)
4141         (dependencies gnus-newsgroup-dependencies)
4142         header article)
4143     (save-excursion
4144       (set-buffer nntp-server-buffer)
4145       (let ((case-fold-search nil))
4146         (goto-char (point-min))
4147         (while (not (eobp))
4148           (ignore-errors
4149             (setq article (read (current-buffer))
4150                   header (gnus-nov-parse-line article dependencies)))
4151           (when header
4152             (with-current-buffer gnus-summary-buffer
4153               (push header gnus-newsgroup-headers)
4154               (if (memq (setq article (mail-header-number header))
4155                         gnus-newsgroup-unselected)
4156                   (progn
4157                     (setq gnus-newsgroup-unreads
4158                           (gnus-add-to-sorted-list
4159                            gnus-newsgroup-unreads article))
4160                     (setq gnus-newsgroup-unselected
4161                           (delq article gnus-newsgroup-unselected)))
4162                 (push article gnus-newsgroup-ancient)))
4163             (forward-line 1)))))))
4164
4165 (defun gnus-summary-update-article-line (article header)
4166   "Update the line for ARTICLE using HEADER."
4167   (let* ((id (mail-header-id header))
4168          (thread (gnus-id-to-thread id)))
4169     (unless thread
4170       (error "Article in no thread"))
4171     ;; Update the thread.
4172     (setcar thread header)
4173     (gnus-summary-goto-subject article)
4174     (let* ((datal (gnus-data-find-list article))
4175            (data (car datal))
4176            (buffer-read-only nil)
4177            (level (gnus-summary-thread-level)))
4178       (gnus-delete-line)
4179       (let ((inserted (- (point)
4180                          (progn
4181                            (gnus-summary-insert-line
4182                             header level nil
4183                             (memq article gnus-newsgroup-undownloaded)
4184                             (gnus-article-mark article)
4185                             (memq article gnus-newsgroup-replied)
4186                             (memq article gnus-newsgroup-expirable)
4187                             ;; Only insert the Subject string when it's different
4188                             ;; from the previous Subject string.
4189                             (if (and
4190                                  gnus-show-threads
4191                                  (gnus-subject-equal
4192                                   (condition-case ()
4193                                       (mail-header-subject
4194                                        (gnus-data-header
4195                                         (cadr
4196                                          (gnus-data-find-list
4197                                           article
4198                                           (gnus-data-list t)))))
4199                                     ;; Error on the side of excessive subjects.
4200                                     (error ""))
4201                                   (mail-header-subject header)))
4202                                 ""
4203                               (mail-header-subject header))
4204                             nil (cdr (assq article gnus-newsgroup-scored))
4205                             (memq article gnus-newsgroup-processable))
4206                            (point)))))
4207         (when (cdr datal)
4208           (gnus-data-update-list
4209            (cdr datal)
4210            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4211
4212 (defun gnus-summary-update-article (article &optional iheader)
4213   "Update ARTICLE in the summary buffer."
4214   (set-buffer gnus-summary-buffer)
4215   (let* ((header (gnus-summary-article-header article))
4216          (id (mail-header-id header))
4217          (data (gnus-data-find article))
4218          (thread (gnus-id-to-thread id))
4219          (references (mail-header-references header))
4220          (parent
4221           (gnus-id-to-thread
4222            (or (gnus-parent-id
4223                 (when (and references
4224                            (not (equal "" references)))
4225                   references))
4226                "none")))
4227          (buffer-read-only nil)
4228          (old (car thread)))
4229     (when thread
4230       (unless iheader
4231         (setcar thread nil)
4232         (when parent
4233           (delq thread parent)))
4234       (if (gnus-summary-insert-subject id header)
4235           ;; Set the (possibly) new article number in the data structure.
4236           (gnus-data-set-number data (gnus-id-to-article id))
4237         (setcar thread old)
4238         nil))))
4239
4240 (defun gnus-rebuild-thread (id &optional line)
4241   "Rebuild the thread containing ID.
4242 If LINE, insert the rebuilt thread starting on line LINE."
4243   (let ((buffer-read-only nil)
4244         old-pos current thread data)
4245     (if (not gnus-show-threads)
4246         (setq thread (list (car (gnus-id-to-thread id))))
4247       ;; Get the thread this article is part of.
4248       (setq thread (gnus-remove-thread id)))
4249     (setq old-pos (point-at-bol))
4250     (setq current (save-excursion
4251                     (and (re-search-backward "[\r\n]" nil t)
4252                          (gnus-summary-article-number))))
4253     ;; If this is a gathered thread, we have to go some re-gathering.
4254     (when (stringp (car thread))
4255       (let ((subject (car thread))
4256             roots thr)
4257         (setq thread (cdr thread))
4258         (while thread
4259           (unless (memq (setq thr (gnus-id-to-thread
4260                                    (gnus-root-id
4261                                     (mail-header-id (caar thread)))))
4262                         roots)
4263             (push thr roots))
4264           (setq thread (cdr thread)))
4265         ;; We now have all (unique) roots.
4266         (if (= (length roots) 1)
4267             ;; All the loose roots are now one solid root.
4268             (setq thread (car roots))
4269           (setq thread (cons subject (gnus-sort-threads roots))))))
4270     (let (threads)
4271       ;; We then insert this thread into the summary buffer.
4272       (when line
4273         (goto-char (point-min))
4274         (forward-line (1- line)))
4275       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4276         (if gnus-show-threads
4277             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4278           (gnus-summary-prepare-unthreaded thread))
4279         (setq data (nreverse gnus-newsgroup-data))
4280         (setq threads gnus-newsgroup-threads))
4281       ;; We splice the new data into the data structure.
4282       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4283       ;;!!! then we want to insert at the beginning of the buffer.
4284       ;;!!! That happens to be true with Gnus now, but that may
4285       ;;!!! change in the future.  Perhaps.
4286       (gnus-data-enter-list
4287        (if line nil current) data (- (point) old-pos))
4288       (setq gnus-newsgroup-threads
4289             (nconc threads gnus-newsgroup-threads))
4290       (gnus-data-compute-positions))))
4291
4292 (defun gnus-number-to-header (number)
4293   "Return the header for article NUMBER."
4294   (let ((headers gnus-newsgroup-headers))
4295     (while (and headers
4296                 (not (= number (mail-header-number (car headers)))))
4297       (pop headers))
4298     (when headers
4299       (car headers))))
4300
4301 (defun gnus-parent-headers (in-headers &optional generation)
4302   "Return the headers of the GENERATIONeth parent of HEADERS."
4303   (unless generation
4304     (setq generation 1))
4305   (let ((parent t)
4306         (headers in-headers)
4307         references)
4308     (while (and parent
4309                 (not (zerop generation))
4310                 (setq references (mail-header-references headers)))
4311       (setq headers (if (and references
4312                              (setq parent (gnus-parent-id references)))
4313                         (car (gnus-id-to-thread parent))
4314                       nil))
4315       (decf generation))
4316     (and (not (eq headers in-headers))
4317          headers)))
4318
4319 (defun gnus-id-to-thread (id)
4320   "Return the (sub-)thread where ID appears."
4321   (gnus-gethash id gnus-newsgroup-dependencies))
4322
4323 (defun gnus-id-to-article (id)
4324   "Return the article number of ID."
4325   (let ((thread (gnus-id-to-thread id)))
4326     (when (and thread
4327                (car thread))
4328       (mail-header-number (car thread)))))
4329
4330 (defun gnus-id-to-header (id)
4331   "Return the article headers of ID."
4332   (car (gnus-id-to-thread id)))
4333
4334 (defun gnus-article-displayed-root-p (article)
4335   "Say whether ARTICLE is a root(ish) article."
4336   (let ((level (gnus-summary-thread-level article))
4337         (refs (mail-header-references  (gnus-summary-article-header article)))
4338         particle)
4339     (cond
4340      ((null level) nil)
4341      ((zerop level) t)
4342      ((null refs) t)
4343      ((null (gnus-parent-id refs)) t)
4344      ((and (= 1 level)
4345            (null (setq particle (gnus-id-to-article
4346                                  (gnus-parent-id refs))))
4347            (null (gnus-summary-thread-level particle)))))))
4348
4349 (defun gnus-root-id (id)
4350   "Return the id of the root of the thread where ID appears."
4351   (let (last-id prev)
4352     (while (and id (setq prev (car (gnus-id-to-thread id))))
4353       (setq last-id id
4354             id (gnus-parent-id (mail-header-references prev))))
4355     last-id))
4356
4357 (defun gnus-articles-in-thread (thread)
4358   "Return the list of articles in THREAD."
4359   (cons (mail-header-number (car thread))
4360         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4361
4362 (defun gnus-remove-thread (id &optional dont-remove)
4363   "Remove the thread that has ID in it."
4364   (let (headers thread last-id)
4365     ;; First go up in this thread until we find the root.
4366     (setq last-id (gnus-root-id id)
4367           headers (message-flatten-list (gnus-id-to-thread last-id)))
4368     ;; We have now found the real root of this thread.  It might have
4369     ;; been gathered into some loose thread, so we have to search
4370     ;; through the threads to find the thread we wanted.
4371     (let ((threads gnus-newsgroup-threads)
4372           sub)
4373       (while threads
4374         (setq sub (car threads))
4375         (if (stringp (car sub))
4376             ;; This is a gathered thread, so we look at the roots
4377             ;; below it to find whether this article is in this
4378             ;; gathered root.
4379             (progn
4380               (setq sub (cdr sub))
4381               (while sub
4382                 (when (member (caar sub) headers)
4383                   (setq thread (car threads)
4384                         threads nil
4385                         sub nil))
4386                 (setq sub (cdr sub))))
4387           ;; It's an ordinary thread, so we check it.
4388           (when (eq (car sub) (car headers))
4389             (setq thread sub
4390                   threads nil)))
4391         (setq threads (cdr threads)))
4392       ;; If this article is in no thread, then it's a root.
4393       (if thread
4394           (unless dont-remove
4395             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4396         (setq thread (gnus-id-to-thread last-id)))
4397       (when thread
4398         (prog1
4399             thread                      ; We return this thread.
4400           (unless dont-remove
4401             (if (stringp (car thread))
4402                 (progn
4403                   ;; If we use dummy roots, then we have to remove the
4404                   ;; dummy root as well.
4405                   (when (eq gnus-summary-make-false-root 'dummy)
4406                     ;; We go to the dummy root by going to
4407                     ;; the first sub-"thread", and then one line up.
4408                     (gnus-summary-goto-article
4409                      (mail-header-number (caadr thread)))
4410                     (forward-line -1)
4411                     (gnus-delete-line)
4412                     (gnus-data-compute-positions))
4413                   (setq thread (cdr thread))
4414                   (while thread
4415                     (gnus-remove-thread-1 (car thread))
4416                     (setq thread (cdr thread))))
4417               (gnus-remove-thread-1 thread))))))))
4418
4419 (defun gnus-remove-thread-1 (thread)
4420   "Remove the thread THREAD recursively."
4421   (let ((number (mail-header-number (pop thread)))
4422         d)
4423     (setq thread (reverse thread))
4424     (while thread
4425       (gnus-remove-thread-1 (pop thread)))
4426     (when (setq d (gnus-data-find number))
4427       (goto-char (gnus-data-pos d))
4428       (gnus-summary-show-thread)
4429       (gnus-data-remove
4430        number
4431        (- (point-at-bol)
4432           (prog1
4433               (1+ (point-at-eol))
4434             (gnus-delete-line)))))))
4435
4436 (defun gnus-sort-threads-1 (threads func)
4437   (sort (mapcar (lambda (thread)
4438                   (cons (car thread)
4439                         (and (cdr thread)
4440                              (gnus-sort-threads-1 (cdr thread) func))))
4441                 threads) func))
4442
4443 (defun gnus-sort-threads (threads)
4444   "Sort THREADS."
4445   (if (not gnus-thread-sort-functions)
4446       threads
4447     (gnus-message 8 "Sorting threads...")
4448     (let ((max-lisp-eval-depth 5000))
4449       (prog1 (gnus-sort-threads-1
4450          threads
4451          (gnus-make-sort-function gnus-thread-sort-functions))
4452         (gnus-message 8 "Sorting threads...done")))))
4453
4454 (defun gnus-sort-articles (articles)
4455   "Sort ARTICLES."
4456   (when gnus-article-sort-functions
4457     (gnus-message 7 "Sorting articles...")
4458     (prog1
4459         (setq gnus-newsgroup-headers
4460               (sort articles (gnus-make-sort-function
4461                               gnus-article-sort-functions)))
4462       (gnus-message 7 "Sorting articles...done"))))
4463
4464 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4465 (defmacro gnus-thread-header (thread)
4466   "Return header of first article in THREAD.
4467 Note that THREAD must never, ever be anything else than a variable -
4468 using some other form will lead to serious barfage."
4469   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4470   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4471   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4472         (vector thread) 2))
4473
4474 (defsubst gnus-article-sort-by-number (h1 h2)
4475   "Sort articles by article number."
4476   (< (mail-header-number h1)
4477      (mail-header-number h2)))
4478
4479 (defun gnus-thread-sort-by-number (h1 h2)
4480   "Sort threads by root article number."
4481   (gnus-article-sort-by-number
4482    (gnus-thread-header h1) (gnus-thread-header h2)))
4483
4484 (defsubst gnus-article-sort-by-random (h1 h2)
4485   "Sort articles by article number."
4486   (zerop (random 2)))
4487
4488 (defun gnus-thread-sort-by-random (h1 h2)
4489   "Sort threads by root article number."
4490   (gnus-article-sort-by-random
4491    (gnus-thread-header h1) (gnus-thread-header h2)))
4492
4493 (defsubst gnus-article-sort-by-lines (h1 h2)
4494   "Sort articles by article Lines header."
4495   (< (mail-header-lines h1)
4496      (mail-header-lines h2)))
4497
4498 (defun gnus-thread-sort-by-lines (h1 h2)
4499   "Sort threads by root article Lines header."
4500   (gnus-article-sort-by-lines
4501    (gnus-thread-header h1) (gnus-thread-header h2)))
4502
4503 (defsubst gnus-article-sort-by-chars (h1 h2)
4504   "Sort articles by octet length."
4505   (< (mail-header-chars h1)
4506      (mail-header-chars h2)))
4507
4508 (defun gnus-thread-sort-by-chars (h1 h2)
4509   "Sort threads by root article octet length."
4510   (gnus-article-sort-by-chars
4511    (gnus-thread-header h1) (gnus-thread-header h2)))
4512
4513 (defsubst gnus-article-sort-by-author (h1 h2)
4514   "Sort articles by root author."
4515   (string-lessp
4516    (let ((extract (funcall
4517                    gnus-extract-address-components
4518                    (mail-header-from h1))))
4519      (or (car extract) (cadr extract) ""))
4520    (let ((extract (funcall
4521                    gnus-extract-address-components
4522                    (mail-header-from h2))))
4523      (or (car extract) (cadr extract) ""))))
4524
4525 (defun gnus-thread-sort-by-author (h1 h2)
4526   "Sort threads by root author."
4527   (gnus-article-sort-by-author
4528    (gnus-thread-header h1)  (gnus-thread-header h2)))
4529
4530 (defsubst gnus-article-sort-by-subject (h1 h2)
4531   "Sort articles by root subject."
4532   (string-lessp
4533    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4534    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4535
4536 (defun gnus-thread-sort-by-subject (h1 h2)
4537   "Sort threads by root subject."
4538   (gnus-article-sort-by-subject
4539    (gnus-thread-header h1) (gnus-thread-header h2)))
4540
4541 (defsubst gnus-article-sort-by-date (h1 h2)
4542   "Sort articles by root article date."
4543   (time-less-p
4544    (gnus-date-get-time (mail-header-date h1))
4545    (gnus-date-get-time (mail-header-date h2))))
4546
4547 (defun gnus-thread-sort-by-date (h1 h2)
4548   "Sort threads by root article date."
4549   (gnus-article-sort-by-date
4550    (gnus-thread-header h1) (gnus-thread-header h2)))
4551
4552 (defsubst gnus-article-sort-by-score (h1 h2)
4553   "Sort articles by root article score.
4554 Unscored articles will be counted as having a score of zero."
4555   (> (or (cdr (assq (mail-header-number h1)
4556                     gnus-newsgroup-scored))
4557          gnus-summary-default-score 0)
4558      (or (cdr (assq (mail-header-number h2)
4559                     gnus-newsgroup-scored))
4560          gnus-summary-default-score 0)))
4561
4562 (defun gnus-thread-sort-by-score (h1 h2)
4563   "Sort threads by root article score."
4564   (gnus-article-sort-by-score
4565    (gnus-thread-header h1) (gnus-thread-header h2)))
4566
4567 (defun gnus-thread-sort-by-total-score (h1 h2)
4568   "Sort threads by the sum of all scores in the thread.
4569 Unscored articles will be counted as having a score of zero."
4570   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4571
4572 (defun gnus-thread-total-score (thread)
4573   ;; This function find the total score of THREAD.
4574   (cond
4575    ((null thread)
4576     0)
4577    ((consp thread)
4578     (if (stringp (car thread))
4579         (apply gnus-thread-score-function 0
4580                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4581       (gnus-thread-total-score-1 thread)))
4582    (t
4583     (gnus-thread-total-score-1 (list thread)))))
4584
4585 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4586   "Sort threads such that the thread with the most recently arrived article comes first."
4587   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4588
4589 (defun gnus-thread-highest-number (thread)
4590   "Return the highest article number in THREAD."
4591   (apply 'max (mapcar (lambda (header)
4592                         (mail-header-number header))
4593                       (message-flatten-list thread))))
4594
4595 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4596   "Sort threads such that the thread with the most recently dated article comes first."
4597   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4598
4599 (defun gnus-thread-latest-date (thread)
4600   "Return the highest article date in THREAD."
4601   (let ((previous-time 0))
4602     (apply 'max
4603            (mapcar
4604             (lambda (header)
4605               (setq previous-time
4606                     (condition-case ()
4607                         (time-to-seconds (mail-header-parse-date
4608                                           (mail-header-date header)))
4609                       (error previous-time))))
4610             (sort
4611              (message-flatten-list thread)
4612              (lambda (h1 h2)
4613                (< (mail-header-number h1)
4614                   (mail-header-number h2))))))))
4615
4616 (defun gnus-thread-total-score-1 (root)
4617   ;; This function find the total score of the thread below ROOT.
4618   (setq root (car root))
4619   (apply gnus-thread-score-function
4620          (or (append
4621               (mapcar 'gnus-thread-total-score
4622                       (cdr (gnus-id-to-thread (mail-header-id root))))
4623               (when (> (mail-header-number root) 0)
4624                 (list (or (cdr (assq (mail-header-number root)
4625                                      gnus-newsgroup-scored))
4626                           gnus-summary-default-score 0))))
4627              (list gnus-summary-default-score)
4628              '(0))))
4629
4630 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4631 (defvar gnus-tmp-prev-subject nil)
4632 (defvar gnus-tmp-false-parent nil)
4633 (defvar gnus-tmp-root-expunged nil)
4634 (defvar gnus-tmp-dummy-line nil)
4635
4636 (eval-when-compile (defvar gnus-tmp-header))
4637 (defun gnus-extra-header (type &optional header)
4638   "Return the extra header of TYPE."
4639   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4640       ""))
4641
4642 (defvar gnus-tmp-thread-tree-header-string "")
4643
4644 (defcustom gnus-sum-thread-tree-root "> "
4645   "With %B spec, used for the root of a thread.
4646 If nil, use subject instead."
4647   :type '(radio (const :format "%v  " nil) (string :size 0))
4648   :group 'gnus-thread)
4649 (defcustom gnus-sum-thread-tree-false-root "> "
4650   "With %B spec, used for a false root of a thread.
4651 If nil, use subject instead."
4652   :type '(radio (const :format "%v  " nil) (string :size 0))
4653   :group 'gnus-thread)
4654 (defcustom gnus-sum-thread-tree-single-indent ""
4655   "With %B spec, used for a thread with just one message.
4656 If nil, use subject instead."
4657   :type '(radio (const :format "%v  " nil) (string :size 0))
4658   :group 'gnus-thread)
4659 (defcustom gnus-sum-thread-tree-vertical "| "
4660   "With %B spec, used for drawing a vertical line."
4661   :type 'string
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-indent "  "
4664   "With %B spec, used for indenting."
4665   :type 'string
4666   :group 'gnus-thread)
4667 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4668   "With %B spec, used for a leaf with brothers."
4669   :type 'string
4670   :group 'gnus-thread)
4671 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4672   "With %B spec, used for a leaf without brothers."
4673   :type 'string
4674   :group 'gnus-thread)
4675
4676 (defun gnus-summary-prepare-threads (threads)
4677   "Prepare summary buffer from THREADS and indentation LEVEL.
4678 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4679 or a straight list of headers."
4680   (gnus-message 7 "Generating summary...")
4681
4682   (setq gnus-newsgroup-threads threads)
4683   (beginning-of-line)
4684
4685   (let ((gnus-tmp-level 0)
4686         (default-score (or gnus-summary-default-score 0))
4687         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4688         (building-line-count gnus-summary-display-while-building)
4689         (building-count (integerp gnus-summary-display-while-building))
4690         thread number subject stack state gnus-tmp-gathered beg-match
4691         new-roots gnus-tmp-new-adopts thread-end simp-subject
4692         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4693         gnus-tmp-replied gnus-tmp-subject-or-nil
4694         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4695         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4696         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4697         tree-stack)
4698
4699     (setq gnus-tmp-prev-subject nil
4700           gnus-tmp-thread-tree-header-string "")
4701
4702     (if (vectorp (car threads))
4703         ;; If this is a straight (sic) list of headers, then a
4704         ;; threaded summary display isn't required, so we just create
4705         ;; an unthreaded one.
4706         (gnus-summary-prepare-unthreaded threads)
4707
4708       ;; Do the threaded display.
4709
4710       (if gnus-summary-display-while-building
4711           (switch-to-buffer (buffer-name)))
4712       (while (or threads stack gnus-tmp-new-adopts new-roots)
4713
4714         (if (and (= gnus-tmp-level 0)
4715                  (or (not stack)
4716                      (= (caar stack) 0))
4717                  (not gnus-tmp-false-parent)
4718                  (or gnus-tmp-new-adopts new-roots))
4719             (if gnus-tmp-new-adopts
4720                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4721                       thread (list (car gnus-tmp-new-adopts))
4722                       gnus-tmp-header (caar thread)
4723                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4724               (when new-roots
4725                 (setq thread (list (car new-roots))
4726                       gnus-tmp-header (caar thread)
4727                       new-roots (cdr new-roots))))
4728
4729           (if threads
4730               ;; If there are some threads, we do them before the
4731               ;; threads on the stack.
4732               (setq thread threads
4733                     gnus-tmp-header (caar thread))
4734             ;; There were no current threads, so we pop something off
4735             ;; the stack.
4736             (setq state (car stack)
4737                   gnus-tmp-level (car state)
4738                   tree-stack (cadr state)
4739                   thread (caddr state)
4740                   stack (cdr stack)
4741                   gnus-tmp-header (caar thread))))
4742
4743         (setq gnus-tmp-false-parent nil)
4744         (setq gnus-tmp-root-expunged nil)
4745         (setq thread-end nil)
4746
4747         (if (stringp gnus-tmp-header)
4748             ;; The header is a dummy root.
4749             (cond
4750              ((eq gnus-summary-make-false-root 'adopt)
4751               ;; We let the first article adopt the rest.
4752               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4753                                                (cddar thread)))
4754               (setq gnus-tmp-gathered
4755                     (nconc (mapcar
4756                             (lambda (h) (mail-header-number (car h)))
4757                             (cddar thread))
4758                            gnus-tmp-gathered))
4759               (setq thread (cons (list (caar thread)
4760                                        (cadar thread))
4761                                  (cdr thread)))
4762               (setq gnus-tmp-level -1
4763                     gnus-tmp-false-parent t))
4764              ((eq gnus-summary-make-false-root 'empty)
4765               ;; We print adopted articles with empty subject fields.
4766               (setq gnus-tmp-gathered
4767                     (nconc (mapcar
4768                             (lambda (h) (mail-header-number (car h)))
4769                             (cddar thread))
4770                            gnus-tmp-gathered))
4771               (setq gnus-tmp-level -1))
4772              ((eq gnus-summary-make-false-root 'dummy)
4773               ;; We remember that we probably want to output a dummy
4774               ;; root.
4775               (setq gnus-tmp-dummy-line gnus-tmp-header)
4776               (setq gnus-tmp-prev-subject gnus-tmp-header))
4777              (t
4778               ;; We do not make a root for the gathered
4779               ;; sub-threads at all.
4780               (setq gnus-tmp-level -1)))
4781
4782           (setq number (mail-header-number gnus-tmp-header)
4783                 subject (mail-header-subject gnus-tmp-header)
4784                 simp-subject (gnus-simplify-subject-fully subject))
4785
4786           (cond
4787            ;; If the thread has changed subject, we might want to make
4788            ;; this subthread into a root.
4789            ((and (null gnus-thread-ignore-subject)
4790                  (not (zerop gnus-tmp-level))
4791                  gnus-tmp-prev-subject
4792                  (not (string= gnus-tmp-prev-subject simp-subject)))
4793             (setq new-roots (nconc new-roots (list (car thread)))
4794                   thread-end t
4795                   gnus-tmp-header nil))
4796            ;; If the article lies outside the current limit,
4797            ;; then we do not display it.
4798            ((not (memq number gnus-newsgroup-limit))
4799             (setq gnus-tmp-gathered
4800                   (nconc (mapcar
4801                           (lambda (h) (mail-header-number (car h)))
4802                           (cdar thread))
4803                          gnus-tmp-gathered))
4804             (setq gnus-tmp-new-adopts (if (cdar thread)
4805                                           (append gnus-tmp-new-adopts
4806                                                   (cdar thread))
4807                                         gnus-tmp-new-adopts)
4808                   thread-end t
4809                   gnus-tmp-header nil)
4810             (when (zerop gnus-tmp-level)
4811               (setq gnus-tmp-root-expunged t)))
4812            ;; Perhaps this article is to be marked as read?
4813            ((and gnus-summary-mark-below
4814                  (< (or (cdr (assq number gnus-newsgroup-scored))
4815                         default-score)
4816                     gnus-summary-mark-below)
4817                  ;; Don't touch sparse articles.
4818                  (not (gnus-summary-article-sparse-p number))
4819                  (not (gnus-summary-article-ancient-p number)))
4820             (setq gnus-newsgroup-unreads
4821                   (delq number gnus-newsgroup-unreads))
4822             (if gnus-newsgroup-auto-expire
4823                 (setq gnus-newsgroup-expirable
4824                       (gnus-add-to-sorted-list
4825                        gnus-newsgroup-expirable number))
4826               (push (cons number gnus-low-score-mark)
4827                     gnus-newsgroup-reads))))
4828
4829           (when gnus-tmp-header
4830             ;; We may have an old dummy line to output before this
4831             ;; article.
4832             (when (and gnus-tmp-dummy-line
4833                        (gnus-subject-equal
4834                         gnus-tmp-dummy-line
4835                         (mail-header-subject gnus-tmp-header)))
4836               (gnus-summary-insert-dummy-line
4837                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4838               (setq gnus-tmp-dummy-line nil))
4839
4840             ;; Compute the mark.
4841             (setq gnus-tmp-unread (gnus-article-mark number))
4842
4843             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4844                                   gnus-tmp-header gnus-tmp-level)
4845                   gnus-newsgroup-data)
4846
4847             ;; Actually insert the line.
4848             (setq
4849              gnus-tmp-subject-or-nil
4850              (cond
4851               ((and gnus-thread-ignore-subject
4852                     gnus-tmp-prev-subject
4853                     (not (string= gnus-tmp-prev-subject simp-subject)))
4854                subject)
4855               ((zerop gnus-tmp-level)
4856                (if (and (eq gnus-summary-make-false-root 'empty)
4857                         (memq number gnus-tmp-gathered)
4858                         gnus-tmp-prev-subject
4859                         (string= gnus-tmp-prev-subject simp-subject))
4860                    gnus-summary-same-subject
4861                  subject))
4862               (t gnus-summary-same-subject)))
4863             (if (and (eq gnus-summary-make-false-root 'adopt)
4864                      (= gnus-tmp-level 1)
4865                      (memq number gnus-tmp-gathered))
4866                 (setq gnus-tmp-opening-bracket ?\<
4867                       gnus-tmp-closing-bracket ?\>)
4868               (setq gnus-tmp-opening-bracket ?\[
4869                     gnus-tmp-closing-bracket ?\]))
4870             (setq
4871              gnus-tmp-indentation
4872              (aref gnus-thread-indent-array gnus-tmp-level)
4873              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4874              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4875                                 gnus-summary-default-score 0)
4876              gnus-tmp-score-char
4877              (if (or (null gnus-summary-default-score)
4878                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4879                          gnus-summary-zcore-fuzz))
4880                  ?                      ;Whitespace
4881                (if (< gnus-tmp-score gnus-summary-default-score)
4882                    gnus-score-below-mark gnus-score-over-mark))
4883              gnus-tmp-replied
4884              (cond ((memq number gnus-newsgroup-processable)
4885                     gnus-process-mark)
4886                    ((memq number gnus-newsgroup-cached)
4887                     gnus-cached-mark)
4888                    ((memq number gnus-newsgroup-replied)
4889                     gnus-replied-mark)
4890                    ((memq number gnus-newsgroup-forwarded)
4891                     gnus-forwarded-mark)
4892                    ((memq number gnus-newsgroup-saved)
4893                     gnus-saved-mark)
4894                    ((memq number gnus-newsgroup-recent)
4895                     gnus-recent-mark)
4896                    ((memq number gnus-newsgroup-unseen)
4897                     gnus-unseen-mark)
4898                    (t gnus-no-mark))
4899              gnus-tmp-downloaded
4900              (cond ((memq number gnus-newsgroup-undownloaded)
4901                     gnus-undownloaded-mark)
4902                    (gnus-newsgroup-agentized
4903                     gnus-downloaded-mark)
4904                    (t
4905                     gnus-no-mark))
4906              gnus-tmp-from (mail-header-from gnus-tmp-header)
4907              gnus-tmp-name
4908              (cond
4909               ((string-match "<[^>]+> *$" gnus-tmp-from)
4910                (setq beg-match (match-beginning 0))
4911                (or (and (string-match "^\".+\"" gnus-tmp-from)
4912                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4913                    (substring gnus-tmp-from 0 beg-match)))
4914               ((string-match "(.+)" gnus-tmp-from)
4915                (substring gnus-tmp-from
4916                           (1+ (match-beginning 0)) (1- (match-end 0))))
4917               (t gnus-tmp-from))
4918
4919              ;; Do the %B string
4920              gnus-tmp-thread-tree-header-string
4921              (cond
4922               ((not gnus-show-threads) "")
4923               ((zerop gnus-tmp-level)
4924                (cond ((cdar thread)
4925                       (or gnus-sum-thread-tree-root subject))
4926                      (gnus-tmp-new-adopts
4927                       (or gnus-sum-thread-tree-false-root subject))
4928                      (t
4929                       (or gnus-sum-thread-tree-single-indent subject))))
4930               (t
4931                (concat (apply 'concat
4932                               (mapcar (lambda (item)
4933                                         (if (= item 1)
4934                                             gnus-sum-thread-tree-vertical
4935                                           gnus-sum-thread-tree-indent))
4936                                       (cdr (reverse tree-stack))))
4937                        (if (nth 1 thread)
4938                            gnus-sum-thread-tree-leaf-with-other
4939                          gnus-sum-thread-tree-single-leaf)))))
4940             (when (string= gnus-tmp-name "")
4941               (setq gnus-tmp-name gnus-tmp-from))
4942             (unless (numberp gnus-tmp-lines)
4943               (setq gnus-tmp-lines -1))
4944             (if (= gnus-tmp-lines -1)
4945                 (setq gnus-tmp-lines "?")
4946               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4947               (gnus-put-text-property
4948              (point)
4949              (progn (eval gnus-summary-line-format-spec) (point))
4950                'gnus-number number)
4951             (when gnus-visual-p
4952               (forward-line -1)
4953               (gnus-run-hooks 'gnus-summary-update-hook)
4954               (forward-line 1))
4955
4956             (setq gnus-tmp-prev-subject simp-subject)))
4957
4958         (when (nth 1 thread)
4959           (push (list (max 0 gnus-tmp-level)
4960                       (copy-sequence tree-stack)
4961                       (nthcdr 1 thread))
4962                 stack))
4963         (push (if (nth 1 thread) 1 0) tree-stack)
4964         (incf gnus-tmp-level)
4965         (setq threads (if thread-end nil (cdar thread)))
4966         (if gnus-summary-display-while-building
4967             (if building-count
4968                 (progn
4969                   ;; use a set frequency
4970                   (setq building-line-count (1- building-line-count))
4971                   (when (= building-line-count 0)
4972                     (sit-for 0)
4973                     (setq building-line-count
4974                           gnus-summary-display-while-building)))
4975               ;; always
4976               (sit-for 0)))
4977         (unless threads
4978           (setq gnus-tmp-level 0)))))
4979   (gnus-message 7 "Generating summary...done"))
4980
4981 (defun gnus-summary-prepare-unthreaded (headers)
4982   "Generate an unthreaded summary buffer based on HEADERS."
4983   (let (header number mark)
4984
4985     (beginning-of-line)
4986
4987     (while headers
4988       ;; We may have to root out some bad articles...
4989       (when (memq (setq number (mail-header-number
4990                                 (setq header (pop headers))))
4991                   gnus-newsgroup-limit)
4992         ;; Mark article as read when it has a low score.
4993         (when (and gnus-summary-mark-below
4994                    (< (or (cdr (assq number gnus-newsgroup-scored))
4995                           gnus-summary-default-score 0)
4996                       gnus-summary-mark-below)
4997                    (not (gnus-summary-article-ancient-p number)))
4998           (setq gnus-newsgroup-unreads
4999                 (delq number gnus-newsgroup-unreads))
5000           (if gnus-newsgroup-auto-expire
5001               (push number gnus-newsgroup-expirable)
5002             (push (cons number gnus-low-score-mark)
5003                   gnus-newsgroup-reads)))
5004
5005         (setq mark (gnus-article-mark number))
5006         (push (gnus-data-make number mark (1+ (point)) header 0)
5007               gnus-newsgroup-data)
5008         (gnus-summary-insert-line
5009          header 0 number
5010          (memq number gnus-newsgroup-undownloaded)
5011          mark (memq number gnus-newsgroup-replied)
5012          (memq number gnus-newsgroup-expirable)
5013          (mail-header-subject header) nil
5014          (cdr (assq number gnus-newsgroup-scored))
5015          (memq number gnus-newsgroup-processable))))))
5016
5017 (defun gnus-summary-remove-list-identifiers ()
5018   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5019   (let ((regexp (if (consp gnus-list-identifiers)
5020                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5021                   gnus-list-identifiers))
5022         changed subject)
5023     (when regexp
5024       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5025       (dolist (header gnus-newsgroup-headers)
5026         (setq subject (mail-header-subject header)
5027               changed nil)
5028         (while (string-match regexp subject)
5029           (setq subject
5030                 (concat (substring subject 0 (match-beginning 1))
5031                         (substring subject (match-end 0)))
5032                 changed t))
5033         (when changed
5034           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5035             (setq subject
5036                   (concat (substring subject 0 (match-beginning 1))
5037                           (substring subject (match-end 1)))))
5038           (mail-header-set-subject header subject))))))
5039
5040 (defun gnus-fetch-headers (articles)
5041   "Fetch headers of ARTICLES."
5042   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5043     (gnus-message 5 "Fetching headers for %s..." name)
5044     (prog1
5045         (if (eq 'nov
5046                 (setq gnus-headers-retrieved-by
5047                       (gnus-retrieve-headers
5048                        articles gnus-newsgroup-name
5049                        ;; We might want to fetch old headers, but
5050                        ;; not if there is only 1 article.
5051                        (and (or (and
5052                                  (not (eq gnus-fetch-old-headers 'some))
5053                                  (not (numberp gnus-fetch-old-headers)))
5054                                 (> (length articles) 1))
5055                             gnus-fetch-old-headers))))
5056             (gnus-get-newsgroup-headers-xover
5057              articles nil nil gnus-newsgroup-name t)
5058           (gnus-get-newsgroup-headers))
5059       (gnus-message 5 "Fetching headers for %s...done" name))))
5060
5061 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5062   "Select newsgroup GROUP.
5063 If READ-ALL is non-nil, all articles in the group are selected.
5064 If SELECT-ARTICLES, only select those articles from GROUP."
5065   (let* ((entry (gnus-group-entry group))
5066          ;;!!! Dirty hack; should be removed.
5067          (gnus-summary-ignore-duplicates
5068           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5069               t
5070             gnus-summary-ignore-duplicates))
5071          (info (nth 2 entry))
5072          articles fetched-articles cached)
5073
5074     (unless (gnus-check-server
5075              (set (make-local-variable 'gnus-current-select-method)
5076                   (gnus-find-method-for-group group)))
5077       (error "Couldn't open server"))
5078
5079     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5080         (gnus-activate-group group)     ; Or we can activate it...
5081         (progn                          ; Or we bug out.
5082           (when (equal major-mode 'gnus-summary-mode)
5083             (gnus-kill-buffer (current-buffer)))
5084           (error "Couldn't activate group %s: %s"
5085                  group (gnus-status-message group))))
5086
5087     (unless (gnus-request-group group t)
5088       (when (equal major-mode 'gnus-summary-mode)
5089         (gnus-kill-buffer (current-buffer)))
5090       (error "Couldn't request group %s: %s"
5091              group (gnus-status-message group)))
5092
5093     (when gnus-agent
5094       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5095       
5096       (setq gnus-summary-use-undownloaded-faces
5097             (gnus-agent-find-parameter
5098              group
5099              'agent-enable-undownloaded-faces)))
5100
5101     (setq gnus-newsgroup-name group
5102           gnus-newsgroup-unselected nil
5103           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5104
5105     (let ((display (gnus-group-find-parameter group 'display)))
5106       (setq gnus-newsgroup-display
5107             (cond
5108              ((not (zerop (or (car-safe read-all) 0)))
5109               ;; The user entered the group with C-u SPC/RET, let's show
5110               ;; all articles.
5111               'gnus-not-ignore)
5112              ((eq display 'all)
5113               'gnus-not-ignore)
5114              ((arrayp display)
5115               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5116              ((numberp display)
5117               ;; The following is probably the "correct" solution, but
5118               ;; it makes Gnus fetch all headers and then limit the
5119               ;; articles (which is slow), so instead we hack the
5120               ;; select-articles parameter instead. -- Simon Josefsson
5121               ;; <jas@kth.se>
5122               ;;
5123               ;; (gnus-byte-compile
5124               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5125               ;;                         display)))))
5126               (setq select-articles
5127                     (gnus-uncompress-range
5128                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5129                              (if (> tmp 0)
5130                                  tmp
5131                                1))
5132                            (cdr (gnus-active group)))))
5133               nil)
5134              (t
5135               nil))))
5136
5137     (gnus-summary-setup-default-charset)
5138
5139     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5140     (when (gnus-virtual-group-p group)
5141       (setq cached gnus-newsgroup-cached))
5142
5143     (setq gnus-newsgroup-unreads
5144           (gnus-sorted-ndifference
5145            (gnus-sorted-ndifference gnus-newsgroup-unreads
5146                                     gnus-newsgroup-marked)
5147            gnus-newsgroup-dormant))
5148
5149     (setq gnus-newsgroup-processable nil)
5150
5151     (gnus-update-read-articles group gnus-newsgroup-unreads)
5152
5153     ;; Adjust and set lists of article marks.
5154     (when info
5155       (gnus-adjust-marked-articles info))
5156     (if (setq articles select-articles)
5157         (setq gnus-newsgroup-unselected
5158               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5159       (setq articles (gnus-articles-to-read group read-all)))
5160
5161     (cond
5162      ((null articles)
5163       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5164       'quit)
5165      ((eq articles 0) nil)
5166      (t
5167       ;; Init the dependencies hash table.
5168       (setq gnus-newsgroup-dependencies
5169             (gnus-make-hashtable (length articles)))
5170       (gnus-set-global-variables)
5171       ;; Retrieve the headers and read them in.
5172
5173       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5174
5175       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5176       (when cached
5177         (setq gnus-newsgroup-cached cached))
5178
5179       ;; Suppress duplicates?
5180       (when gnus-suppress-duplicates
5181         (gnus-dup-suppress-articles))
5182
5183       ;; Set the initial limit.
5184       (setq gnus-newsgroup-limit (copy-sequence articles))
5185       ;; Remove canceled articles from the list of unread articles.
5186       (setq fetched-articles
5187             (mapcar (lambda (headers) (mail-header-number headers))
5188                     gnus-newsgroup-headers))
5189       (setq gnus-newsgroup-articles fetched-articles)
5190       (setq gnus-newsgroup-unreads
5191             (gnus-sorted-nintersection
5192              gnus-newsgroup-unreads fetched-articles))
5193       (gnus-compute-unseen-list)
5194
5195       ;; Removed marked articles that do not exist.
5196       (gnus-update-missing-marks
5197        (gnus-sorted-difference articles fetched-articles))
5198       ;; We might want to build some more threads first.
5199       (when (and gnus-fetch-old-headers
5200                  (eq gnus-headers-retrieved-by 'nov))
5201         (if (eq gnus-fetch-old-headers 'invisible)
5202             (gnus-build-all-threads)
5203           (gnus-build-old-threads)))
5204       ;; Let the Gnus agent mark articles as read.
5205       (when gnus-agent
5206         (gnus-agent-get-undownloaded-list))
5207       ;; Remove list identifiers from subject
5208       (when gnus-list-identifiers
5209         (gnus-summary-remove-list-identifiers))
5210       ;; Check whether auto-expire is to be done in this group.
5211       (setq gnus-newsgroup-auto-expire
5212             (gnus-group-auto-expirable-p group))
5213       ;; Set up the article buffer now, if necessary.
5214       (unless gnus-single-article-buffer
5215         (gnus-article-setup-buffer))
5216       ;; First and last article in this newsgroup.
5217       (when gnus-newsgroup-headers
5218         (setq gnus-newsgroup-begin
5219               (mail-header-number (car gnus-newsgroup-headers))
5220               gnus-newsgroup-end
5221               (mail-header-number
5222                (gnus-last-element gnus-newsgroup-headers))))
5223       ;; GROUP is successfully selected.
5224       (or gnus-newsgroup-headers t)))))
5225
5226 (defun gnus-compute-unseen-list ()
5227   ;; The `seen' marks are treated specially.
5228   (if (not gnus-newsgroup-seen)
5229       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5230     (setq gnus-newsgroup-unseen
5231           (gnus-inverse-list-range-intersection
5232            gnus-newsgroup-articles gnus-newsgroup-seen))))
5233
5234 (defun gnus-summary-display-make-predicate (display)
5235   (require 'gnus-agent)
5236   (when (= (length display) 1)
5237     (setq display (car display)))
5238   (unless gnus-summary-display-cache
5239     (dolist (elem (append '((unread . unread)
5240                             (read . read)
5241                             (unseen . unseen))
5242                           gnus-article-mark-lists))
5243       (push (cons (cdr elem)
5244                   (gnus-byte-compile
5245                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5246             gnus-summary-display-cache)))
5247   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5248         (gnus-category-predicate-cache gnus-summary-display-cache))
5249     (gnus-get-predicate display)))
5250
5251 ;; Uses the dynamically bound `number' variable.
5252 (eval-when-compile
5253   (defvar number))
5254 (defun gnus-article-marked-p (type &optional article)
5255   (let ((article (or article number)))
5256     (cond
5257      ((eq type 'tick)
5258       (memq article gnus-newsgroup-marked))
5259      ((eq type 'spam)
5260       (memq article gnus-newsgroup-spam-marked))
5261      ((eq type 'unsend)
5262       (memq article gnus-newsgroup-unsendable))
5263      ((eq type 'undownload)
5264       (memq article gnus-newsgroup-undownloaded))
5265      ((eq type 'download)
5266       (memq article gnus-newsgroup-downloadable))
5267      ((eq type 'unread)
5268       (memq article gnus-newsgroup-unreads))
5269      ((eq type 'read)
5270       (memq article gnus-newsgroup-reads))
5271      ((eq type 'dormant)
5272       (memq article gnus-newsgroup-dormant) )
5273      ((eq type 'expire)
5274       (memq article gnus-newsgroup-expirable))
5275      ((eq type 'reply)
5276       (memq article gnus-newsgroup-replied))
5277      ((eq type 'killed)
5278       (memq article gnus-newsgroup-killed))
5279      ((eq type 'bookmark)
5280       (assq article gnus-newsgroup-bookmarks))
5281      ((eq type 'score)
5282       (assq article gnus-newsgroup-scored))
5283      ((eq type 'save)
5284       (memq article gnus-newsgroup-saved))
5285      ((eq type 'cache)
5286       (memq article gnus-newsgroup-cached))
5287      ((eq type 'forward)
5288       (memq article gnus-newsgroup-forwarded))
5289      ((eq type 'seen)
5290       (not (memq article gnus-newsgroup-unseen)))
5291      ((eq type 'recent)
5292       (memq article gnus-newsgroup-recent))
5293      (t t))))
5294
5295 (defun gnus-articles-to-read (group &optional read-all)
5296   "Find out what articles the user wants to read."
5297   (let* ((articles
5298           ;; Select all articles if `read-all' is non-nil, or if there
5299           ;; are no unread articles.
5300           (if (or read-all
5301                   (and (zerop (length gnus-newsgroup-marked))
5302                        (zerop (length gnus-newsgroup-unreads)))
5303                   ;; Fetch all if the predicate is non-nil.
5304                   gnus-newsgroup-display)
5305               ;; We want to select the headers for all the articles in
5306               ;; the group, so we select either all the active
5307               ;; articles in the group, or (if that's nil), the
5308               ;; articles in the cache.
5309               (or
5310                (gnus-uncompress-range (gnus-active group))
5311                (gnus-cache-articles-in-group group))
5312             ;; Select only the "normal" subset of articles.
5313             (gnus-sorted-nunion
5314              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5315              gnus-newsgroup-unreads)))
5316          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5317          (scored (length scored-list))
5318          (number (length articles))
5319          (marked (+ (length gnus-newsgroup-marked)
5320                     (length gnus-newsgroup-dormant)))
5321          (select
5322           (cond
5323            ((numberp read-all)
5324             read-all)
5325            ((numberp gnus-newsgroup-display)
5326             gnus-newsgroup-display)
5327            (t
5328             (condition-case ()
5329                 (cond
5330                  ((and (or (<= scored marked) (= scored number))
5331                        (numberp gnus-large-newsgroup)
5332                        (> number gnus-large-newsgroup))
5333                   (let* ((cursor-in-echo-area nil)
5334                          (initial (gnus-parameter-large-newsgroup-initial
5335                                    gnus-newsgroup-name))
5336                          (input
5337                           (read-string
5338                            (format
5339                             "How many articles from %s (%s %d): "
5340                             (gnus-limit-string
5341                              (gnus-group-decoded-name gnus-newsgroup-name)
5342                              35)
5343                             (if initial "max" "default")
5344                             number)
5345                            (if initial
5346                                (cons (number-to-string initial)
5347                                      0)))))
5348                     (if (string-match "^[ \t]*$" input) number input)))
5349                  ((and (> scored marked) (< scored number)
5350                        (> (- scored number) 20))
5351                   (let ((input
5352                          (read-string
5353                           (format "%s %s (%d scored, %d total): "
5354                                   "How many articles from"
5355                                   (gnus-group-decoded-name group)
5356                                   scored number))))
5357                     (if (string-match "^[ \t]*$" input)
5358                         number input)))
5359                  (t number))
5360               (quit
5361                (message "Quit getting the articles to read")
5362                nil))))))
5363     (setq select (if (stringp select) (string-to-number select) select))
5364     (if (or (null select) (zerop select))
5365         select
5366       (if (and (not (zerop scored)) (<= (abs select) scored))
5367           (progn
5368             (setq articles (sort scored-list '<))
5369             (setq number (length articles)))
5370         (setq articles (copy-sequence articles)))
5371
5372       (when (< (abs select) number)
5373         (if (< select 0)
5374             ;; Select the N oldest articles.
5375             (setcdr (nthcdr (1- (abs select)) articles) nil)
5376           ;; Select the N most recent articles.
5377           (setq articles (nthcdr (- number select) articles))))
5378       (setq gnus-newsgroup-unselected
5379             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5380       (when gnus-alter-articles-to-read-function
5381         (setq articles
5382               (sort
5383                (funcall gnus-alter-articles-to-read-function
5384                         gnus-newsgroup-name articles)
5385                '<)))
5386       articles)))
5387
5388 (defun gnus-killed-articles (killed articles)
5389   (let (out)
5390     (while articles
5391       (when (inline (gnus-member-of-range (car articles) killed))
5392         (push (car articles) out))
5393       (setq articles (cdr articles)))
5394     out))
5395
5396 (defun gnus-uncompress-marks (marks)
5397   "Uncompress the mark ranges in MARKS."
5398   (let ((uncompressed '(score bookmark))
5399         out)
5400     (while marks
5401       (if (memq (caar marks) uncompressed)
5402           (push (car marks) out)
5403         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5404       (setq marks (cdr marks)))
5405     out))
5406
5407 (defun gnus-article-mark-to-type (mark)
5408   "Return the type of MARK."
5409   (or (cadr (assq mark gnus-article-special-mark-lists))
5410       'list))
5411
5412 (defun gnus-article-unpropagatable-p (mark)
5413   "Return whether MARK should be propagated to back end."
5414   (memq mark gnus-article-unpropagated-mark-lists))
5415
5416 (defun gnus-adjust-marked-articles (info)
5417   "Set all article lists and remove all marks that are no longer valid."
5418   (let* ((marked-lists (gnus-info-marks info))
5419          (active (gnus-active (gnus-info-group info)))
5420          (min (car active))
5421          (max (cdr active))
5422          (types gnus-article-mark-lists)
5423          marks var articles article mark mark-type
5424          bgn end)
5425
5426     (dolist (marks marked-lists)
5427       (setq mark (car marks)
5428             mark-type (gnus-article-mark-to-type mark)
5429             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5430
5431       ;; We set the variable according to the type of the marks list,
5432       ;; and then adjust the marks to a subset of the active articles.
5433       (cond
5434        ;; Adjust "simple" lists - compressed yet unsorted
5435        ((eq mark-type 'list)
5436         ;; Simultaneously uncompress and clip to active range
5437         ;; See gnus-uncompress-range for a description of possible marks
5438         (let (l lh)
5439           (if (not (cadr marks))
5440               (set var nil)
5441             (setq articles (if (numberp (cddr marks))
5442                                (list (cdr marks))
5443                              (cdr marks))
5444                   lh (cons nil nil)
5445                   l lh)
5446
5447             (while (setq article (pop articles))
5448               (cond ((consp article)
5449                      (setq bgn (max (car article) min)
5450                            end (min (cdr article) max))
5451                      (while (<= bgn end)
5452                        (setq l (setcdr l (cons bgn nil))
5453                              bgn (1+ bgn))))
5454                     ((and (<= min article)
5455                           (>= max article))
5456                      (setq l (setcdr l (cons article nil))))))
5457             (set var (cdr lh)))))
5458        ;; Adjust assocs.
5459        ((eq mark-type 'tuple)
5460         (set var (setq articles (cdr marks)))
5461         (when (not (listp (cdr (symbol-value var))))
5462           (set var (list (symbol-value var))))
5463         (when (not (listp (cdr articles)))
5464           (setq articles (list articles)))
5465         (while articles
5466           (when (or (not (consp (setq article (pop articles))))
5467                     (< (car article) min)
5468                     (> (car article) max))
5469             (set var (delq article (symbol-value var))))))
5470        ;; Adjust ranges (sloppily).
5471        ((eq mark-type 'range)
5472         (cond
5473          ((eq mark 'seen)
5474           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5475           ;; It should be (seen (NUM1 . NUM2)).
5476           (when (numberp (cddr marks))
5477             (setcdr marks (list (cdr marks))))
5478           (setq articles (cdr marks))
5479           (while (and articles
5480                       (or (and (consp (car articles))
5481                                (> min (cdar articles)))
5482                           (and (numberp (car articles))
5483                                (> min (car articles)))))
5484             (pop articles))
5485           (set var articles))))))))
5486
5487 (defun gnus-update-missing-marks (missing)
5488   "Go through the list of MISSING articles and remove them from the mark lists."
5489   (when missing
5490     (let (var m)
5491       ;; Go through all types.
5492       (dolist (elem gnus-article-mark-lists)
5493         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5494           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5495           (when (symbol-value var)
5496             ;; This list has articles.  So we delete all missing
5497             ;; articles from it.
5498             (setq m missing)
5499             (while m
5500               (set var (delq (pop m) (symbol-value var))))))))))
5501
5502 (defun gnus-update-marks ()
5503   "Enter the various lists of marked articles into the newsgroup info list."
5504   (let ((types gnus-article-mark-lists)
5505         (info (gnus-get-info gnus-newsgroup-name))
5506         type list newmarked symbol delta-marks)
5507     (when info
5508       ;; Add all marks lists to the list of marks lists.
5509       (while (setq type (pop types))
5510         (setq list (symbol-value
5511                     (setq symbol
5512                           (intern (format "gnus-newsgroup-%s" (car type))))))
5513
5514         (when list
5515           ;; Get rid of the entries of the articles that have the
5516           ;; default score.
5517           (when (and (eq (cdr type) 'score)
5518                      gnus-save-score
5519                      list)
5520             (let* ((arts list)
5521                    (prev (cons nil list))
5522                    (all prev))
5523               (while arts
5524                 (if (or (not (consp (car arts)))
5525                         (= (cdar arts) gnus-summary-default-score))
5526                     (setcdr prev (cdr arts))
5527                   (setq prev arts))
5528                 (setq arts (cdr arts)))
5529               (setq list (cdr all)))))
5530
5531         (when (eq (cdr type) 'seen)
5532           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5533
5534         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5535           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5536
5537         (when (and (gnus-check-backend-function
5538                     'request-set-mark gnus-newsgroup-name)
5539                    (not (gnus-article-unpropagatable-p (cdr type))))
5540           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5541                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5542                  (add (gnus-remove-from-range
5543                        (gnus-copy-sequence list) old)))
5544             (when add
5545               (push (list add 'add (list (cdr type))) delta-marks))
5546             (when del
5547               (push (list del 'del (list (cdr type))) delta-marks))))
5548
5549         (when list
5550           (push (cons (cdr type) list) newmarked)))
5551
5552       (when delta-marks
5553         (unless (gnus-check-group gnus-newsgroup-name)
5554           (error "Can't open server for %s" gnus-newsgroup-name))
5555         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5556
5557       ;; Enter these new marks into the info of the group.
5558       (if (nthcdr 3 info)
5559           (setcar (nthcdr 3 info) newmarked)
5560         ;; Add the marks lists to the end of the info.
5561         (when newmarked
5562           (setcdr (nthcdr 2 info) (list newmarked))))
5563
5564       ;; Cut off the end of the info if there's nothing else there.
5565       (let ((i 5))
5566         (while (and (> i 2)
5567                     (not (nth i info)))
5568           (when (nthcdr (decf i) info)
5569             (setcdr (nthcdr i info) nil)))))))
5570
5571 (defun gnus-set-mode-line (where)
5572   "Set the mode line of the article or summary buffers.
5573 If WHERE is `summary', the summary mode line format will be used."
5574   ;; Is this mode line one we keep updated?
5575   (when (and (memq where gnus-updated-mode-lines)
5576              (symbol-value
5577               (intern (format "gnus-%s-mode-line-format-spec" where))))
5578     (let (mode-string)
5579       (save-excursion
5580         ;; We evaluate this in the summary buffer since these
5581         ;; variables are buffer-local to that buffer.
5582         (set-buffer gnus-summary-buffer)
5583        ;; We bind all these variables that are used in the `eval' form
5584         ;; below.
5585         (let* ((mformat (symbol-value
5586                          (intern
5587                           (format "gnus-%s-mode-line-format-spec" where))))
5588                (gnus-tmp-group-name (gnus-group-decoded-name
5589                                      gnus-newsgroup-name))
5590                (gnus-tmp-article-number (or gnus-current-article 0))
5591                (gnus-tmp-unread gnus-newsgroup-unreads)
5592                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5593                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5594                (gnus-tmp-unread-and-unselected
5595                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5596                             (zerop gnus-tmp-unselected))
5597                        "")
5598                       ((zerop gnus-tmp-unselected)
5599                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5600                       (t (format "{%d(+%d) more}"
5601                                  gnus-tmp-unread-and-unticked
5602                                  gnus-tmp-unselected))))
5603                (gnus-tmp-subject
5604                 (if (and gnus-current-headers
5605                          (vectorp gnus-current-headers))
5606                     (gnus-mode-string-quote
5607                      (mail-header-subject gnus-current-headers))
5608                   ""))
5609                bufname-length max-len
5610                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5611           (setq mode-string (eval mformat))
5612           (setq bufname-length (if (string-match "%b" mode-string)
5613                                    (- (length
5614                                        (buffer-name
5615                                         (if (eq where 'summary)
5616                                             nil
5617                                           (get-buffer gnus-article-buffer))))
5618                                       2)
5619                                  0))
5620           (setq max-len (max 4 (if gnus-mode-non-string-length
5621                                    (- (window-width)
5622                                       gnus-mode-non-string-length
5623                                       bufname-length)
5624                                  (length mode-string))))
5625           ;; We might have to chop a bit of the string off...
5626           (when (> (length mode-string) max-len)
5627             (setq mode-string
5628                   (concat (truncate-string-to-width mode-string (- max-len 3))
5629                           "...")))
5630           ;; Pad the mode string a bit.
5631           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5632       ;; Update the mode line.
5633       (setq mode-line-buffer-identification
5634             (gnus-mode-line-buffer-identification (list mode-string)))
5635       (set-buffer-modified-p t))))
5636
5637 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5638   "Go through the HEADERS list and add all Xrefs to a hash table.
5639 The resulting hash table is returned, or nil if no Xrefs were found."
5640   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5641          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5642          (xref-hashtb (gnus-make-hashtable))
5643          start group entry number xrefs header)
5644     (while headers
5645       (setq header (pop headers))
5646       (when (and (setq xrefs (mail-header-xref header))
5647                  (not (memq (setq number (mail-header-number header))
5648                             unreads)))
5649         (setq start 0)
5650         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5651           (setq start (match-end 0))
5652           (setq group (if prefix
5653                           (concat prefix (substring xrefs (match-beginning 1)
5654                                                     (match-end 1)))
5655                         (substring xrefs (match-beginning 1) (match-end 1))))
5656           (setq number
5657                 (string-to-int (substring xrefs (match-beginning 2)
5658                                           (match-end 2))))
5659           (if (setq entry (gnus-gethash group xref-hashtb))
5660               (setcdr entry (cons number (cdr entry)))
5661             (gnus-sethash group (cons number nil) xref-hashtb)))))
5662     (and start xref-hashtb)))
5663
5664 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5665   "Look through all the headers and mark the Xrefs as read."
5666   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5667         name info xref-hashtb idlist method nth4)
5668     (save-excursion
5669       (set-buffer gnus-group-buffer)
5670       (when (setq xref-hashtb
5671                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5672         (mapatoms
5673          (lambda (group)
5674            (unless (string= from-newsgroup (setq name (symbol-name group)))
5675              (setq idlist (symbol-value group))
5676              ;; Dead groups are not updated.
5677              (and (prog1
5678                       (setq info (gnus-get-info name))
5679                     (when (stringp (setq nth4 (gnus-info-method info)))
5680                       (setq nth4 (gnus-server-to-method nth4))))
5681                   ;; Only do the xrefs if the group has the same
5682                   ;; select method as the group we have just read.
5683                   (or (gnus-methods-equal-p
5684                        nth4 (gnus-find-method-for-group from-newsgroup))
5685                       virtual
5686                       (equal nth4 (setq method (gnus-find-method-for-group
5687                                                 from-newsgroup)))
5688                       (and (equal (car nth4) (car method))
5689                            (equal (nth 1 nth4) (nth 1 method))))
5690                   gnus-use-cross-reference
5691                   (or (not (eq gnus-use-cross-reference t))
5692                       virtual
5693                       ;; Only do cross-references on subscribed
5694                       ;; groups, if that is what is wanted.
5695                       (<= (gnus-info-level info) gnus-level-subscribed))
5696                   (gnus-group-make-articles-read name idlist))))
5697          xref-hashtb)))))
5698
5699 (defun gnus-compute-read-articles (group articles)
5700   (let* ((entry (gnus-group-entry group))
5701          (info (nth 2 entry))
5702          (active (gnus-active group))
5703          ninfo)
5704     (when entry
5705       ;; First peel off all invalid article numbers.
5706       (when active
5707         (let ((ids articles)
5708               id first)
5709           (while (setq id (pop ids))
5710             (when (and first (> id (cdr active)))
5711               ;; We'll end up in this situation in one particular
5712               ;; obscure situation.  If you re-scan a group and get
5713               ;; a new article that is cross-posted to a different
5714               ;; group that has not been re-scanned, you might get
5715               ;; crossposted article that has a higher number than
5716               ;; Gnus believes possible.  So we re-activate this
5717               ;; group as well.  This might mean doing the
5718               ;; crossposting thingy will *increase* the number
5719               ;; of articles in some groups.  Tsk, tsk.
5720               (setq active (or (gnus-activate-group group) active)))
5721             (when (or (> id (cdr active))
5722                       (< id (car active)))
5723               (setq articles (delq id articles))))))
5724       ;; If the read list is nil, we init it.
5725       (if (and active
5726                (null (gnus-info-read info))
5727                (> (car active) 1))
5728           (setq ninfo (cons 1 (1- (car active))))
5729         (setq ninfo (gnus-info-read info)))
5730       ;; Then we add the read articles to the range.
5731       (gnus-add-to-range
5732        ninfo (setq articles (sort articles '<))))))
5733
5734 (defun gnus-group-make-articles-read (group articles)
5735   "Update the info of GROUP to say that ARTICLES are read."
5736   (let* ((num 0)
5737          (entry (gnus-group-entry group))
5738          (info (nth 2 entry))
5739          (active (gnus-active group))
5740          range)
5741     (when entry
5742       (setq range (gnus-compute-read-articles group articles))
5743       (with-current-buffer gnus-group-buffer
5744         (gnus-undo-register
5745           `(progn
5746              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5747              (gnus-info-set-read ',info ',(gnus-info-read info))
5748              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5749              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5750              (gnus-group-update-group ,group t))))
5751       ;; Add the read articles to the range.
5752       (gnus-info-set-read info range)
5753       (gnus-request-set-mark group (list (list range 'add '(read))))
5754       ;; Then we have to re-compute how many unread
5755       ;; articles there are in this group.
5756       (when active
5757         (cond
5758          ((not range)
5759           (setq num (- (1+ (cdr active)) (car active))))
5760          ((not (listp (cdr range)))
5761           (setq num (- (cdr active) (- (1+ (cdr range))
5762                                        (car range)))))
5763          (t
5764           (while range
5765             (if (numberp (car range))
5766                 (setq num (1+ num))
5767               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5768             (setq range (cdr range)))
5769           (setq num (- (cdr active) num))))
5770         ;; Update the number of unread articles.
5771         (setcar entry num)
5772         ;; Update the group buffer.
5773         (unless (gnus-ephemeral-group-p group)
5774           (gnus-group-update-group group t))))))
5775
5776 (defvar gnus-newsgroup-none-id 0)
5777
5778 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5779   (let ((cur nntp-server-buffer)
5780         (dependencies
5781          (or dependencies
5782              (with-current-buffer gnus-summary-buffer
5783                gnus-newsgroup-dependencies)))
5784         headers id end ref number
5785         (mail-parse-charset gnus-newsgroup-charset)
5786         (mail-parse-ignored-charsets
5787          (save-excursion (condition-case nil
5788                              (set-buffer gnus-summary-buffer)
5789                            (error))
5790                          gnus-newsgroup-ignored-charsets)))
5791     (save-excursion
5792       (set-buffer nntp-server-buffer)
5793       ;; Translate all TAB characters into SPACE characters.
5794       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5795       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5796       (ietf-drums-unfold-fws)
5797       (gnus-run-hooks 'gnus-parse-headers-hook)
5798       (let ((case-fold-search t)
5799             in-reply-to header p lines chars)
5800         (goto-char (point-min))
5801         ;; Search to the beginning of the next header.  Error messages
5802         ;; do not begin with 2 or 3.
5803         (while (re-search-forward "^[23][0-9]+ " nil t)
5804           (setq id nil
5805                 ref nil)
5806           ;; This implementation of this function, with nine
5807           ;; search-forwards instead of the one re-search-forward and
5808           ;; a case (which basically was the old function) is actually
5809           ;; about twice as fast, even though it looks messier.  You
5810           ;; can't have everything, I guess.  Speed and elegance
5811           ;; doesn't always go hand in hand.
5812           (setq
5813            header
5814            (vector
5815             ;; Number.
5816             (prog1
5817                 (setq number (read cur))
5818               (end-of-line)
5819               (setq p (point))
5820               (narrow-to-region (point)
5821                                 (or (and (search-forward "\n.\n" nil t)
5822                                          (- (point) 2))
5823                                     (point))))
5824             ;; Subject.
5825             (progn
5826               (goto-char p)
5827               (if (search-forward "\nsubject:" nil t)
5828                   (funcall gnus-decode-encoded-word-function
5829                            (nnheader-header-value))
5830                 "(none)"))
5831             ;; From.
5832             (progn
5833               (goto-char p)
5834               (if (search-forward "\nfrom:" nil t)
5835                   (funcall gnus-decode-encoded-word-function
5836                            (nnheader-header-value))
5837                 "(nobody)"))
5838             ;; Date.
5839             (progn
5840               (goto-char p)
5841               (if (search-forward "\ndate:" nil t)
5842                   (nnheader-header-value) ""))
5843             ;; Message-ID.
5844             (progn
5845               (goto-char p)
5846               (setq id (if (re-search-forward
5847                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5848                            ;; We do it this way to make sure the Message-ID
5849                            ;; is (somewhat) syntactically valid.
5850                            (buffer-substring (match-beginning 1)
5851                                              (match-end 1))
5852                          ;; If there was no message-id, we just fake one
5853                          ;; to make subsequent routines simpler.
5854                          (nnheader-generate-fake-message-id number))))
5855             ;; References.
5856             (progn
5857               (goto-char p)
5858               (if (search-forward "\nreferences:" nil t)
5859                   (progn
5860                     (setq end (point))
5861                     (prog1
5862                         (nnheader-header-value)
5863                       (setq ref
5864                             (buffer-substring
5865                              (progn
5866                                (end-of-line)
5867                                (search-backward ">" end t)
5868                                (1+ (point)))
5869                              (progn
5870                                (search-backward "<" end t)
5871                                (point))))))
5872                 ;; Get the references from the in-reply-to header if there
5873                 ;; were no references and the in-reply-to header looks
5874                 ;; promising.
5875                 (if (and (search-forward "\nin-reply-to:" nil t)
5876                          (setq in-reply-to (nnheader-header-value))
5877                          (string-match "<[^>]+>" in-reply-to))
5878                     (let (ref2)
5879                       (setq ref (substring in-reply-to (match-beginning 0)
5880                                            (match-end 0)))
5881                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5882                         (setq ref2 (substring in-reply-to (match-beginning 0)
5883                                               (match-end 0)))
5884                         (when (> (length ref2) (length ref))
5885                           (setq ref ref2)))
5886                       ref)
5887                   (setq ref nil))))
5888             ;; Chars.
5889             (progn
5890               (goto-char p)
5891               (if (search-forward "\nchars: " nil t)
5892                   (if (numberp (setq chars (ignore-errors (read cur))))
5893                       chars -1)
5894                 -1))
5895             ;; Lines.
5896             (progn
5897               (goto-char p)
5898               (if (search-forward "\nlines: " nil t)
5899                   (if (numberp (setq lines (ignore-errors (read cur))))
5900                       lines -1)
5901                 -1))
5902             ;; Xref.
5903             (progn
5904               (goto-char p)
5905               (and (search-forward "\nxref:" nil t)
5906                    (nnheader-header-value)))
5907             ;; Extra.
5908             (when gnus-extra-headers
5909               (let ((extra gnus-extra-headers)
5910                     out)
5911                 (while extra
5912                   (goto-char p)
5913                   (when (search-forward
5914                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5915                     (push (cons (car extra) (nnheader-header-value))
5916                           out))
5917                   (pop extra))
5918                 out))))
5919           (when (equal id ref)
5920             (setq ref nil))
5921
5922           (when gnus-alter-header-function
5923             (funcall gnus-alter-header-function header)
5924             (setq id (mail-header-id header)
5925                   ref (gnus-parent-id (mail-header-references header))))
5926
5927           (when (setq header
5928                       (gnus-dependencies-add-header
5929                        header dependencies force-new))
5930             (push header headers))
5931           (goto-char (point-max))
5932           (widen))
5933         (nreverse headers)))))
5934
5935 ;; Goes through the xover lines and returns a list of vectors
5936 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5937                                                   force-new dependencies
5938                                                   group also-fetch-heads)
5939   "Parse the news overview data in the server buffer.
5940 Return a list of headers that match SEQUENCE (see
5941 `nntp-retrieve-headers')."
5942   ;; Get the Xref when the users reads the articles since most/some
5943   ;; NNTP servers do not include Xrefs when using XOVER.
5944   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5945   (let ((mail-parse-charset gnus-newsgroup-charset)
5946         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5947         (cur nntp-server-buffer)
5948         (dependencies (or dependencies gnus-newsgroup-dependencies))
5949         (allp (cond
5950                ((eq gnus-read-all-available-headers t)
5951                 t)
5952                ((stringp gnus-read-all-available-headers)
5953                 (string-match gnus-read-all-available-headers group))
5954                (t
5955                 nil)))
5956         number headers header)
5957     (save-excursion
5958       (set-buffer nntp-server-buffer)
5959       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5960       ;; Allow the user to mangle the headers before parsing them.
5961       (gnus-run-hooks 'gnus-parse-headers-hook)
5962       (goto-char (point-min))
5963       (gnus-parse-without-error
5964         (while (and (or sequence allp)
5965                     (not (eobp)))
5966           (setq number (read cur))
5967           (when (not allp)
5968             (while (and sequence
5969                         (< (car sequence) number))
5970               (setq sequence (cdr sequence))))
5971           (when (and (or allp
5972                          (and sequence
5973                               (eq number (car sequence))))
5974                      (progn
5975                        (setq sequence (cdr sequence))
5976                        (setq header (inline
5977                                       (gnus-nov-parse-line
5978                                        number dependencies force-new)))))
5979             (push header headers))
5980           (forward-line 1)))
5981       ;; A common bug in inn is that if you have posted an article and
5982       ;; then retrieves the active file, it will answer correctly --
5983       ;; the new article is included.  However, a NOV entry for the
5984       ;; article may not have been generated yet, so this may fail.
5985       ;; We work around this problem by retrieving the last few
5986       ;; headers using HEAD.
5987       (if (or (not also-fetch-heads)
5988               (not sequence))
5989           ;; We (probably) got all the headers.
5990           (nreverse headers)
5991         (let ((gnus-nov-is-evil t))
5992           (nconc
5993            (nreverse headers)
5994            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5995              (gnus-get-newsgroup-headers))))))))
5996
5997 (defun gnus-article-get-xrefs ()
5998   "Fill in the Xref value in `gnus-current-headers', if necessary.
5999 This is meant to be called in `gnus-article-internal-prepare-hook'."
6000   (let ((headers (with-current-buffer gnus-summary-buffer
6001                    gnus-current-headers)))
6002     (or (not gnus-use-cross-reference)
6003         (not headers)
6004         (and (mail-header-xref headers)
6005              (not (string= (mail-header-xref headers) "")))
6006         (let ((case-fold-search t)
6007               xref)
6008           (save-restriction
6009             (nnheader-narrow-to-headers)
6010             (goto-char (point-min))
6011             (when (or (and (not (eobp))
6012                            (eq (downcase (char-after)) ?x)
6013                            (looking-at "Xref:"))
6014                       (search-forward "\nXref:" nil t))
6015               (goto-char (1+ (match-end 0)))
6016               (setq xref (buffer-substring (point) (point-at-eol)))
6017               (mail-header-set-xref headers xref)))))))
6018
6019 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6020   "Find article ID and insert the summary line for that article.
6021 OLD-HEADER can either be a header or a line number to insert
6022 the subject line on."
6023   (let* ((line (and (numberp old-header) old-header))
6024          (old-header (and (vectorp old-header) old-header))
6025          (header (cond ((and old-header use-old-header)
6026                         old-header)
6027                        ((and (numberp id)
6028                              (gnus-number-to-header id))
6029                         (gnus-number-to-header id))
6030                        (t
6031                         (gnus-read-header id))))
6032          (number (and (numberp id) id))
6033          d)
6034     (when header
6035       ;; Rebuild the thread that this article is part of and go to the
6036       ;; article we have fetched.
6037       (when (and (not gnus-show-threads)
6038                  old-header)
6039         (when (and number
6040                    (setq d (gnus-data-find (mail-header-number old-header))))
6041           (goto-char (gnus-data-pos d))
6042           (gnus-data-remove
6043            number
6044            (- (point-at-bol)
6045               (prog1
6046                   (1+ (point-at-eol))
6047                 (gnus-delete-line))))))
6048       (when old-header
6049         (mail-header-set-number header (mail-header-number old-header)))
6050       (setq gnus-newsgroup-sparse
6051             (delq (setq number (mail-header-number header))
6052                   gnus-newsgroup-sparse))
6053       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6054       (push number gnus-newsgroup-limit)
6055       (gnus-rebuild-thread (mail-header-id header) line)
6056       (gnus-summary-goto-subject number nil t))
6057     (when (and (numberp number)
6058                (> number 0))
6059       ;; We have to update the boundaries even if we can't fetch the
6060       ;; article if ID is a number -- so that the next `P' or `N'
6061       ;; command will fetch the previous (or next) article even
6062       ;; if the one we tried to fetch this time has been canceled.
6063       (when (> number gnus-newsgroup-end)
6064         (setq gnus-newsgroup-end number))
6065       (when (< number gnus-newsgroup-begin)
6066         (setq gnus-newsgroup-begin number))
6067       (setq gnus-newsgroup-unselected
6068             (delq number gnus-newsgroup-unselected)))
6069     ;; Report back a success?
6070     (and header (mail-header-number header))))
6071
6072 ;;; Process/prefix in the summary buffer
6073
6074 (defun gnus-summary-work-articles (n)
6075   "Return a list of articles to be worked upon.
6076 The prefix argument, the list of process marked articles, and the
6077 current article will be taken into consideration."
6078   (save-excursion
6079     (set-buffer gnus-summary-buffer)
6080     (cond
6081      (n
6082       ;; A numerical prefix has been given.
6083       (setq n (prefix-numeric-value n))
6084       (let ((backward (< n 0))
6085             (n (abs (prefix-numeric-value n)))
6086             articles article)
6087         (save-excursion
6088           (while
6089               (and (> n 0)
6090                    (push (setq article (gnus-summary-article-number))
6091                          articles)
6092                    (if backward
6093                        (gnus-summary-find-prev nil article)
6094                      (gnus-summary-find-next nil article)))
6095             (decf n)))
6096         (nreverse articles)))
6097      ((and (gnus-region-active-p) (mark))
6098       (message "region active")
6099       ;; Work on the region between point and mark.
6100       (let ((max (max (point) (mark)))
6101             articles article)
6102         (save-excursion
6103           (goto-char (min (min (point) (mark))))
6104           (while
6105               (and
6106                (push (setq article (gnus-summary-article-number)) articles)
6107                (gnus-summary-find-next nil article)
6108                (< (point) max)))
6109           (nreverse articles))))
6110      (gnus-newsgroup-processable
6111       ;; There are process-marked articles present.
6112       ;; Save current state.
6113       (gnus-summary-save-process-mark)
6114       ;; Return the list.
6115       (reverse gnus-newsgroup-processable))
6116      (t
6117       ;; Just return the current article.
6118       (list (gnus-summary-article-number))))))
6119
6120 (defmacro gnus-summary-iterate (arg &rest forms)
6121   "Iterate over the process/prefixed articles and do FORMS.
6122 ARG is the interactive prefix given to the command.  FORMS will be
6123 executed with point over the summary line of the articles."
6124   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6125     `(let ((,articles (gnus-summary-work-articles ,arg)))
6126        (while ,articles
6127          (gnus-summary-goto-subject (car ,articles))
6128          ,@forms
6129          (pop ,articles)))))
6130
6131 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6132 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6133
6134 (defun gnus-summary-save-process-mark ()
6135   "Push the current set of process marked articles on the stack."
6136   (interactive)
6137   (push (copy-sequence gnus-newsgroup-processable)
6138         gnus-newsgroup-process-stack))
6139
6140 (defun gnus-summary-kill-process-mark ()
6141   "Push the current set of process marked articles on the stack and unmark."
6142   (interactive)
6143   (gnus-summary-save-process-mark)
6144   (gnus-summary-unmark-all-processable))
6145
6146 (defun gnus-summary-yank-process-mark ()
6147   "Pop the last process mark state off the stack and restore it."
6148   (interactive)
6149   (unless gnus-newsgroup-process-stack
6150     (error "Empty mark stack"))
6151   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6152
6153 (defun gnus-summary-process-mark-set (set)
6154   "Make SET into the current process marked articles."
6155   (gnus-summary-unmark-all-processable)
6156   (while set
6157     (gnus-summary-set-process-mark (pop set))))
6158
6159 ;;; Searching and stuff
6160
6161 (defun gnus-summary-search-group (&optional backward use-level)
6162   "Search for next unread newsgroup.
6163 If optional argument BACKWARD is non-nil, search backward instead."
6164   (save-excursion
6165     (set-buffer gnus-group-buffer)
6166     (when (gnus-group-search-forward
6167            backward nil (if use-level (gnus-group-group-level) nil))
6168       (gnus-group-group-name))))
6169
6170 (defun gnus-summary-best-group (&optional exclude-group)
6171   "Find the name of the best unread group.
6172 If EXCLUDE-GROUP, do not go to this group."
6173   (with-current-buffer gnus-group-buffer
6174     (save-excursion
6175       (gnus-group-best-unread-group exclude-group))))
6176
6177 (defun gnus-summary-find-next (&optional unread article backward)
6178   (if backward
6179       (gnus-summary-find-prev unread article)
6180     (let* ((dummy (gnus-summary-article-intangible-p))
6181            (article (or article (gnus-summary-article-number)))
6182            (data (gnus-data-find-list article))
6183            result)
6184       (when (and (not dummy)
6185                  (or (not gnus-summary-check-current)
6186                      (not unread)
6187                      (not (gnus-data-unread-p (car data)))))
6188         (setq data (cdr data)))
6189       (when (setq result
6190                   (if unread
6191                       (progn
6192                         (while data
6193                           (unless (memq (gnus-data-number (car data))
6194                                         (cond
6195                                          ((eq gnus-auto-goto-ignores
6196                                               'always-undownloaded)
6197                                           gnus-newsgroup-undownloaded)
6198                                          (gnus-plugged
6199                                           nil)
6200                                          ((eq gnus-auto-goto-ignores
6201                                               'unfetched)
6202                                           gnus-newsgroup-unfetched)
6203                                          ((eq gnus-auto-goto-ignores
6204                                               'undownloaded)
6205                                           gnus-newsgroup-undownloaded)))
6206                             (when (gnus-data-unread-p (car data))
6207                               (setq result (car data)
6208                                     data nil)))
6209                           (setq data (cdr data)))
6210                         result)
6211                     (car data)))
6212         (goto-char (gnus-data-pos result))
6213         (gnus-data-number result)))))
6214
6215 (defun gnus-summary-find-prev (&optional unread article)
6216   (let* ((eobp (eobp))
6217          (article (or article (gnus-summary-article-number)))
6218          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6219          result)
6220     (when (and (not eobp)
6221                (or (not gnus-summary-check-current)
6222                    (not unread)
6223                    (not (gnus-data-unread-p (car data)))))
6224       (setq data (cdr data)))
6225     (when (setq result
6226                 (if unread
6227                     (progn
6228                       (while data
6229                         (unless (memq (gnus-data-number (car data))
6230                                       (cond
6231                                        ((eq gnus-auto-goto-ignores
6232                                             'always-undownloaded)
6233                                         gnus-newsgroup-undownloaded)
6234                                        (gnus-plugged
6235                                         nil)
6236                                        ((eq gnus-auto-goto-ignores
6237                                             'unfetched)
6238                                         gnus-newsgroup-unfetched)
6239                                        ((eq gnus-auto-goto-ignores
6240                                             'undownloaded)
6241                                         gnus-newsgroup-undownloaded)))
6242                           (when (gnus-data-unread-p (car data))
6243                             (setq result (car data)
6244                                   data nil)))
6245                         (setq data (cdr data)))
6246                       result)
6247                   (car data)))
6248       (goto-char (gnus-data-pos result))
6249       (gnus-data-number result))))
6250
6251 (defun gnus-summary-find-subject (subject &optional unread backward article)
6252   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6253          (article (or article (gnus-summary-article-number)))
6254          (articles (gnus-data-list backward))
6255          (arts (gnus-data-find-list article articles))
6256          result)
6257     (when (or (not gnus-summary-check-current)
6258               (not unread)
6259               (not (gnus-data-unread-p (car arts))))
6260       (setq arts (cdr arts)))
6261     (while arts
6262       (and (or (not unread)
6263                (gnus-data-unread-p (car arts)))
6264            (vectorp (gnus-data-header (car arts)))
6265            (gnus-subject-equal
6266             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6267            (setq result (car arts)
6268                  arts nil))
6269       (setq arts (cdr arts)))
6270     (and result
6271          (goto-char (gnus-data-pos result))
6272          (gnus-data-number result))))
6273
6274 (defun gnus-summary-search-forward (&optional unread subject backward)
6275   "Search forward for an article.
6276 If UNREAD, look for unread articles.  If SUBJECT, look for
6277 articles with that subject.  If BACKWARD, search backward instead."
6278   (cond (subject (gnus-summary-find-subject subject unread backward))
6279         (backward (gnus-summary-find-prev unread))
6280         (t (gnus-summary-find-next unread))))
6281
6282 (defun gnus-recenter (&optional n)
6283   "Center point in window and redisplay frame.
6284 Also do horizontal recentering."
6285   (interactive "P")
6286   (when (and gnus-auto-center-summary
6287              (not (eq gnus-auto-center-summary 'vertical)))
6288     (gnus-horizontal-recenter))
6289   (recenter n))
6290
6291 (defun gnus-summary-recenter ()
6292   "Center point in the summary window.
6293 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6294 displayed, no centering will be performed."
6295   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6296   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6297   (interactive)
6298   ;; The user has to want it.
6299   (when gnus-auto-center-summary
6300     (let* ((top (cond ((< (window-height) 4) 0)
6301                       ((< (window-height) 7) 1)
6302                       (t (if (numberp gnus-auto-center-summary)
6303                              gnus-auto-center-summary
6304                            (/ (1- (window-height)) 2)))))
6305            (height (1- (window-height)))
6306            (bottom (save-excursion (goto-char (point-max))
6307                                    (forward-line (- height))
6308                                    (point)))
6309            (window (get-buffer-window (current-buffer))))
6310       (when (get-buffer-window gnus-article-buffer)
6311         ;; Only do recentering when the article buffer is displayed,
6312         ;; Set the window start to either `bottom', which is the biggest
6313         ;; possible valid number, or the second line from the top,
6314         ;; whichever is the least.
6315         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6316           (if (> bottom top-pos)
6317               ;; Keep the second line from the top visible
6318               (set-window-start window top-pos t)
6319             ;; Try to keep the bottom line visible; if it's partially
6320             ;; obscured, either scroll one more line to make it fully
6321             ;; visible, or revert to using TOP-POS.
6322             (save-excursion
6323               (goto-char (point-max))
6324               (forward-line -1)
6325               (let ((last-line-start (point)))
6326                 (goto-char bottom)
6327                 (set-window-start window (point) t)
6328                 (when (not (pos-visible-in-window-p last-line-start window))
6329                   (forward-line 1)
6330                   (set-window-start window (min (point) top-pos) t)))))))
6331       ;; Do horizontal recentering while we're at it.
6332       (when (and (get-buffer-window (current-buffer) t)
6333                  (not (eq gnus-auto-center-summary 'vertical)))
6334         (let ((selected (selected-window)))
6335           (select-window (get-buffer-window (current-buffer) t))
6336           (gnus-summary-position-point)
6337           (gnus-horizontal-recenter)
6338           (select-window selected))))))
6339
6340 (defun gnus-summary-jump-to-group (newsgroup)
6341   "Move point to NEWSGROUP in group mode buffer."
6342   ;; Keep update point of group mode buffer if visible.
6343   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6344       (save-window-excursion
6345         ;; Take care of tree window mode.
6346         (when (get-buffer-window gnus-group-buffer)
6347           (pop-to-buffer gnus-group-buffer))
6348         (gnus-group-jump-to-group newsgroup))
6349     (save-excursion
6350       ;; Take care of tree window mode.
6351       (if (get-buffer-window gnus-group-buffer)
6352           (pop-to-buffer gnus-group-buffer)
6353         (set-buffer gnus-group-buffer))
6354       (gnus-group-jump-to-group newsgroup))))
6355
6356 ;; This function returns a list of article numbers based on the
6357 ;; difference between the ranges of read articles in this group and
6358 ;; the range of active articles.
6359 (defun gnus-list-of-unread-articles (group)
6360   (let* ((read (gnus-info-read (gnus-get-info group)))
6361          (active (or (gnus-active group) (gnus-activate-group group)))
6362          (last (cdr active))
6363          first nlast unread)
6364     ;; If none are read, then all are unread.
6365     (if (not read)
6366         (setq first (car active))
6367       ;; If the range of read articles is a single range, then the
6368       ;; first unread article is the article after the last read
6369       ;; article.  Sounds logical, doesn't it?
6370       (if (and (not (listp (cdr read)))
6371                (or (< (car read) (car active))
6372                    (progn (setq read (list read))
6373                           nil)))
6374           (setq first (max (car active) (1+ (cdr read))))
6375         ;; `read' is a list of ranges.
6376         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6377                                   (caar read)))
6378                   1)
6379           (setq first (car active)))
6380         (while read
6381           (when first
6382             (while (< first nlast)
6383               (setq unread (cons first unread)
6384                     first (1+ first))))
6385           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6386           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6387           (setq read (cdr read)))))
6388     ;; And add the last unread articles.
6389     (while (<= first last)
6390       (setq unread (cons first unread)
6391             first (1+ first)))
6392     ;; Return the list of unread articles.
6393     (delq 0 (nreverse unread))))
6394
6395 (defun gnus-list-of-read-articles (group)
6396   "Return a list of unread, unticked and non-dormant articles."
6397   (let* ((info (gnus-get-info group))
6398          (marked (gnus-info-marks info))
6399          (active (gnus-active group)))
6400     (and info active
6401          (gnus-list-range-difference
6402           (gnus-list-range-difference
6403            (gnus-sorted-complement
6404             (gnus-uncompress-range active)
6405             (gnus-list-of-unread-articles group))
6406            (cdr (assq 'dormant marked)))
6407           (cdr (assq 'tick marked))))))
6408
6409 ;; This function returns a sequence of article numbers based on the
6410 ;; difference between the ranges of read articles in this group and
6411 ;; the range of active articles.
6412 (defun gnus-sequence-of-unread-articles (group)
6413   (let* ((read (gnus-info-read (gnus-get-info group)))
6414          (active (or (gnus-active group) (gnus-activate-group group)))
6415          (last (cdr active))
6416          first nlast unread)
6417     ;; If none are read, then all are unread.
6418     (if (not read)
6419         (setq first (car active))
6420       ;; If the range of read articles is a single range, then the
6421       ;; first unread article is the article after the last read
6422       ;; article.  Sounds logical, doesn't it?
6423       (if (and (not (listp (cdr read)))
6424                (or (< (car read) (car active))
6425                    (progn (setq read (list read))
6426                           nil)))
6427           (setq first (max (car active) (1+ (cdr read))))
6428         ;; `read' is a list of ranges.
6429         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6430                                   (caar read)))
6431                   1)
6432           (setq first (car active)))
6433         (while read
6434           (when first
6435             (push (cons first nlast) unread))
6436           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6437           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6438           (setq read (cdr read)))))
6439     ;; And add the last unread articles.
6440     (cond ((< first last)
6441            (push (cons first last) unread))
6442           ((= first last)
6443            (push first unread)))
6444     ;; Return the sequence of unread articles.
6445     (delq 0 (nreverse unread))))
6446
6447 ;; Various summary commands
6448
6449 (defun gnus-summary-select-article-buffer ()
6450   "Reconfigure windows to show article buffer."
6451   (interactive)
6452   (if (not (gnus-buffer-live-p gnus-article-buffer))
6453       (error "There is no article buffer for this summary buffer")
6454     (gnus-configure-windows 'article)
6455     (select-window (get-buffer-window gnus-article-buffer))))
6456
6457 (defun gnus-summary-universal-argument (arg)
6458   "Perform any operation on all articles that are process/prefixed."
6459   (interactive "P")
6460   (let ((articles (gnus-summary-work-articles arg))
6461         func article)
6462     (if (eq
6463          (setq
6464           func
6465           (key-binding
6466            (read-key-sequence
6467             (substitute-command-keys
6468              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6469          'undefined)
6470         (gnus-error 1 "Undefined key")
6471       (save-excursion
6472         (while articles
6473           (gnus-summary-goto-subject (setq article (pop articles)))
6474           (let (gnus-newsgroup-processable)
6475             (command-execute func))
6476           (gnus-summary-remove-process-mark article)))))
6477   (gnus-summary-position-point))
6478
6479 (defun gnus-summary-toggle-truncation (&optional arg)
6480   "Toggle truncation of summary lines.
6481 With ARG, turn line truncation on if ARG is positive."
6482   (interactive "P")
6483   (setq truncate-lines
6484         (if (null arg) (not truncate-lines)
6485           (> (prefix-numeric-value arg) 0)))
6486   (redraw-display))
6487
6488 (defun gnus-summary-find-for-reselect ()
6489   "Return the number of an article to stay on across a reselect.
6490 The current article is considered, then following articles, then previous
6491 articles.  An article is sought which is not cancelled and isn't a temporary
6492 insertion from another group.  If there's no such then return a dummy 0."
6493   (let (found)
6494     (dolist (rev '(nil t))
6495       (unless found      ; don't demand the reverse list if we don't need it
6496         (let ((data (gnus-data-find-list
6497                      (gnus-summary-article-number) (gnus-data-list rev))))
6498           (while (and data (not found))
6499             (if (and (< 0 (gnus-data-number (car data)))
6500                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6501                 (setq found (gnus-data-number (car data))))
6502             (setq data (cdr data))))))
6503     (or found 0)))
6504
6505 (defun gnus-summary-reselect-current-group (&optional all rescan)
6506   "Exit and then reselect the current newsgroup.
6507 The prefix argument ALL means to select all articles."
6508   (interactive "P")
6509   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6510     (error "Ephemeral groups can't be reselected"))
6511   (let ((current-subject (gnus-summary-find-for-reselect))
6512         (group gnus-newsgroup-name))
6513     (setq gnus-newsgroup-begin nil)
6514     (gnus-summary-exit nil 'leave-hidden)
6515     ;; We have to adjust the point of group mode buffer because
6516     ;; point was moved to the next unread newsgroup by exiting.
6517     (gnus-summary-jump-to-group group)
6518     (when rescan
6519       (save-excursion
6520         (gnus-group-get-new-news-this-group 1)))
6521     (gnus-group-read-group all t)
6522     (gnus-summary-goto-subject current-subject nil t)))
6523
6524 (defun gnus-summary-rescan-group (&optional all)
6525   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6526   (interactive "P")
6527   (gnus-summary-reselect-current-group all t))
6528
6529 (defun gnus-summary-update-info (&optional non-destructive)
6530   (save-excursion
6531     (let ((group gnus-newsgroup-name))
6532       (when group
6533         (when gnus-newsgroup-kill-headers
6534           (setq gnus-newsgroup-killed
6535                 (gnus-compress-sequence
6536                  (gnus-sorted-union
6537                   (gnus-list-range-intersection
6538                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6539                   gnus-newsgroup-unreads)
6540                  t)))
6541         (unless (listp (cdr gnus-newsgroup-killed))
6542           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6543         (let ((headers gnus-newsgroup-headers))
6544           ;; Set the new ranges of read articles.
6545           (with-current-buffer gnus-group-buffer
6546             (gnus-undo-force-boundary))
6547           (gnus-update-read-articles
6548            group (gnus-sorted-union
6549                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6550           ;; Set the current article marks.
6551           (let ((gnus-newsgroup-scored
6552                  (if (and (not gnus-save-score)
6553                           (not non-destructive))
6554                      nil
6555                    gnus-newsgroup-scored)))
6556             (save-excursion
6557               (gnus-update-marks)))
6558           ;; Do the cross-ref thing.
6559           (when gnus-use-cross-reference
6560             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6561           ;; Do not switch windows but change the buffer to work.
6562           (set-buffer gnus-group-buffer)
6563           (unless (gnus-ephemeral-group-p group)
6564             (gnus-group-update-group group)))))))
6565
6566 (defun gnus-summary-save-newsrc (&optional force)
6567   "Save the current number of read/marked articles in the dribble buffer.
6568 The dribble buffer will then be saved.
6569 If FORCE (the prefix), also save the .newsrc file(s)."
6570   (interactive "P")
6571   (gnus-summary-update-info t)
6572   (if force
6573       (gnus-save-newsrc-file)
6574     (gnus-dribble-save)))
6575
6576 (defun gnus-summary-exit (&optional temporary leave-hidden)
6577   "Exit reading current newsgroup, and then return to group selection mode.
6578 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6579   (interactive)
6580   (gnus-set-global-variables)
6581   (when (gnus-buffer-live-p gnus-article-buffer)
6582     (save-excursion
6583       (set-buffer gnus-article-buffer)
6584       (mm-destroy-parts gnus-article-mime-handles)
6585       ;; Set it to nil for safety reason.
6586       (setq gnus-article-mime-handle-alist nil)
6587       (setq gnus-article-mime-handles nil)))
6588   (gnus-kill-save-kill-buffer)
6589   (gnus-async-halt-prefetch)
6590   (let* ((group gnus-newsgroup-name)
6591          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6592          (gnus-group-is-exiting-p t)
6593          (mode major-mode)
6594          (group-point nil)
6595          (buf (current-buffer)))
6596     (unless quit-config
6597       ;; Do adaptive scoring, and possibly save score files.
6598       (when gnus-newsgroup-adaptive
6599         (gnus-score-adaptive))
6600       (when gnus-use-scoring
6601         (gnus-score-save)))
6602     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6603     ;; If we have several article buffers, we kill them at exit.
6604     (unless gnus-single-article-buffer
6605       (gnus-kill-buffer gnus-original-article-buffer)
6606       (setq gnus-article-current nil))
6607     (when gnus-use-cache
6608       (gnus-cache-possibly-remove-articles)
6609       (gnus-cache-save-buffers))
6610     (gnus-async-prefetch-remove-group group)
6611     (when gnus-suppress-duplicates
6612       (gnus-dup-enter-articles))
6613     (when gnus-use-trees
6614       (gnus-tree-close group))
6615     (when gnus-use-cache
6616       (gnus-cache-write-active))
6617     ;; Remove entries for this group.
6618     (nnmail-purge-split-history (gnus-group-real-name group))
6619     ;; Make all changes in this group permanent.
6620     (unless quit-config
6621       (gnus-run-hooks 'gnus-exit-group-hook)
6622       (gnus-summary-update-info))
6623     (gnus-close-group group)
6624     ;; Make sure where we were, and go to next newsgroup.
6625     (set-buffer gnus-group-buffer)
6626     (unless quit-config
6627       (gnus-group-jump-to-group group))
6628     (gnus-run-hooks 'gnus-summary-exit-hook)
6629     (unless (or quit-config
6630                 ;; If this group has disappeared from the summary
6631                 ;; buffer, don't skip forwards.
6632                 (not (string= group (gnus-group-group-name))))
6633       (gnus-group-next-unread-group 1))
6634     (setq group-point (point))
6635     (if temporary
6636         nil                             ;Nothing to do.
6637       ;; If we have several article buffers, we kill them at exit.
6638       (unless gnus-single-article-buffer
6639         (gnus-kill-buffer gnus-article-buffer)
6640         (gnus-kill-buffer gnus-original-article-buffer)
6641         (setq gnus-article-current nil))
6642       (set-buffer buf)
6643       (if (not gnus-kill-summary-on-exit)
6644           (progn
6645             (gnus-deaden-summary)
6646             (setq mode nil))
6647         ;; We set all buffer-local variables to nil.  It is unclear why
6648         ;; this is needed, but if we don't, buffer-local variables are
6649         ;; not garbage-collected, it seems.  This would the lead to en
6650         ;; ever-growing Emacs.
6651         (gnus-summary-clear-local-variables)
6652         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6653           (gnus-summary-clear-local-variables))
6654         (when (get-buffer gnus-article-buffer)
6655           (bury-buffer gnus-article-buffer))
6656         ;; We clear the global counterparts of the buffer-local
6657         ;; variables as well, just to be on the safe side.
6658         (set-buffer gnus-group-buffer)
6659         (gnus-summary-clear-local-variables)
6660         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6661           (gnus-summary-clear-local-variables))
6662         ;; Return to group mode buffer.
6663         (when (eq mode 'gnus-summary-mode)
6664           (gnus-kill-buffer buf)))
6665       (setq gnus-current-select-method gnus-select-method)
6666       (if leave-hidden
6667           (set-buffer gnus-group-buffer)
6668         (pop-to-buffer gnus-group-buffer))
6669       (if (not quit-config)
6670           (progn
6671             (goto-char group-point)
6672             (unless leave-hidden
6673               (gnus-configure-windows 'group 'force)))
6674         (gnus-handle-ephemeral-exit quit-config))
6675       ;; Clear the current group name.
6676       (unless quit-config
6677         (setq gnus-newsgroup-name nil)))))
6678
6679 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6680 (defun gnus-summary-exit-no-update (&optional no-questions)
6681   "Quit reading current newsgroup without updating read article info."
6682   (interactive)
6683   (let* ((group gnus-newsgroup-name)
6684          (gnus-group-is-exiting-p t)
6685          (gnus-group-is-exiting-without-update-p t)
6686          (quit-config (gnus-group-quit-config group)))
6687     (when (or no-questions
6688               gnus-expert-user
6689               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6690       (gnus-async-halt-prefetch)
6691       (run-hooks 'gnus-summary-prepare-exit-hook)
6692       (when (gnus-buffer-live-p gnus-article-buffer)
6693         (save-excursion
6694           (set-buffer gnus-article-buffer)
6695           (mm-destroy-parts gnus-article-mime-handles)
6696           ;; Set it to nil for safety reason.
6697           (setq gnus-article-mime-handle-alist nil)
6698           (setq gnus-article-mime-handles nil)))
6699       ;; If we have several article buffers, we kill them at exit.
6700       (unless gnus-single-article-buffer
6701         (gnus-kill-buffer gnus-article-buffer)
6702         (gnus-kill-buffer gnus-original-article-buffer)
6703         (setq gnus-article-current nil))
6704       (if (not gnus-kill-summary-on-exit)
6705           (gnus-deaden-summary)
6706         (gnus-close-group group)
6707         (gnus-summary-clear-local-variables)
6708         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6709           (gnus-summary-clear-local-variables))
6710         (set-buffer gnus-group-buffer)
6711         (gnus-summary-clear-local-variables)
6712         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6713           (gnus-summary-clear-local-variables))
6714         (gnus-kill-buffer gnus-summary-buffer))
6715       (unless gnus-single-article-buffer
6716         (setq gnus-article-current nil))
6717       (when gnus-use-trees
6718         (gnus-tree-close group))
6719       (gnus-async-prefetch-remove-group group)
6720       (when (get-buffer gnus-article-buffer)
6721         (bury-buffer gnus-article-buffer))
6722       ;; Return to the group buffer.
6723       (gnus-configure-windows 'group 'force)
6724       ;; Clear the current group name.
6725       (setq gnus-newsgroup-name nil)
6726       (unless (gnus-ephemeral-group-p group)
6727         (gnus-group-update-group group))
6728       (when (equal (gnus-group-group-name) group)
6729         (gnus-group-next-unread-group 1))
6730       (when quit-config
6731         (gnus-handle-ephemeral-exit quit-config)))))
6732
6733 (defun gnus-handle-ephemeral-exit (quit-config)
6734   "Handle movement when leaving an ephemeral group.
6735 The state which existed when entering the ephemeral is reset."
6736   (if (not (buffer-name (car quit-config)))
6737       (gnus-configure-windows 'group 'force)
6738     (set-buffer (car quit-config))
6739     (cond ((eq major-mode 'gnus-summary-mode)
6740            (gnus-set-global-variables))
6741           ((eq major-mode 'gnus-article-mode)
6742            (save-excursion
6743              ;; The `gnus-summary-buffer' variable may point
6744              ;; to the old summary buffer when using a single
6745              ;; article buffer.
6746              (unless (gnus-buffer-live-p gnus-summary-buffer)
6747                (set-buffer gnus-group-buffer))
6748              (set-buffer gnus-summary-buffer)
6749              (gnus-set-global-variables))))
6750     (if (or (eq (cdr quit-config) 'article)
6751             (eq (cdr quit-config) 'pick))
6752         (progn
6753           ;; The current article may be from the ephemeral group
6754           ;; thus it is best that we reload this article
6755           ;;
6756           ;; If we're exiting from a large digest, this can be
6757           ;; extremely slow.  So, it's better not to reload it. -- jh.
6758           ;;(gnus-summary-show-article)
6759           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6760               (gnus-configure-windows 'pick 'force)
6761             (gnus-configure-windows (cdr quit-config) 'force)))
6762       (gnus-configure-windows (cdr quit-config) 'force))
6763     (when (eq major-mode 'gnus-summary-mode)
6764       (gnus-summary-next-subject 1 nil t)
6765       (gnus-summary-recenter)
6766       (gnus-summary-position-point))))
6767
6768 ;;; Dead summaries.
6769
6770 (defvar gnus-dead-summary-mode-map nil)
6771
6772 (unless gnus-dead-summary-mode-map
6773   (setq gnus-dead-summary-mode-map (make-keymap))
6774   (suppress-keymap gnus-dead-summary-mode-map)
6775   (substitute-key-definition
6776    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6777   (dolist (key '("\C-d" "\r" "\177" [delete]))
6778     (define-key gnus-dead-summary-mode-map
6779       key 'gnus-summary-wake-up-the-dead))
6780   (dolist (key '("q" "Q"))
6781     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6782
6783 (defvar gnus-dead-summary-mode nil
6784   "Minor mode for Gnus summary buffers.")
6785
6786 (defun gnus-dead-summary-mode (&optional arg)
6787   "Minor mode for Gnus summary buffers."
6788   (interactive "P")
6789   (when (eq major-mode 'gnus-summary-mode)
6790     (make-local-variable 'gnus-dead-summary-mode)
6791     (setq gnus-dead-summary-mode
6792           (if (null arg) (not gnus-dead-summary-mode)
6793             (> (prefix-numeric-value arg) 0)))
6794     (when gnus-dead-summary-mode
6795       (add-minor-mode
6796        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6797
6798 (defun gnus-deaden-summary ()
6799   "Make the current summary buffer into a dead summary buffer."
6800   ;; Kill any previous dead summary buffer.
6801   (when (and gnus-dead-summary
6802              (buffer-name gnus-dead-summary))
6803     (with-current-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       (with-current-buffer buffer
6823         (gnus-kill-buffer gnus-article-buffer)
6824         (gnus-kill-buffer gnus-original-article-buffer)))
6825     (cond
6826      ;; Kill the buffer.
6827      (gnus-kill-summary-on-exit
6828       (when (and gnus-use-trees
6829                  (gnus-buffer-exists-p buffer))
6830         (save-excursion
6831           (set-buffer buffer)
6832           (gnus-tree-close gnus-newsgroup-name)))
6833       (gnus-kill-buffer buffer))
6834      ;; Deaden the buffer.
6835      ((gnus-buffer-exists-p buffer)
6836       (save-excursion
6837         (set-buffer buffer)
6838         (gnus-deaden-summary))))))
6839
6840 (defun gnus-summary-wake-up-the-dead (&rest args)
6841   "Wake up the dead summary buffer."
6842   (interactive)
6843   (gnus-dead-summary-mode -1)
6844   (let ((name (buffer-name)))
6845     (when (string-match "Dead " name)
6846       (rename-buffer
6847        (concat (substring name 0 (match-beginning 0))
6848                (substring name (match-end 0)))
6849        t)))
6850   (gnus-message 3 "This dead summary is now alive again"))
6851
6852 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6853 (defun gnus-summary-fetch-faq (&optional faq-dir)
6854   "Fetch the FAQ for the current group.
6855 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6856 in."
6857   (interactive
6858    (list
6859     (when current-prefix-arg
6860       (completing-read
6861        "FAQ dir: " (and (listp gnus-group-faq-directory)
6862                         (mapcar 'list
6863                                 gnus-group-faq-directory))))))
6864   (let (gnus-faq-buffer)
6865     (when (setq gnus-faq-buffer
6866                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6867       (gnus-configure-windows 'summary-faq))))
6868
6869 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6870 (defun gnus-summary-describe-group (&optional force)
6871   "Describe the current newsgroup."
6872   (interactive "P")
6873   (gnus-group-describe-group force gnus-newsgroup-name))
6874
6875 (defun gnus-summary-describe-briefly ()
6876   "Describe summary mode commands briefly."
6877   (interactive)
6878   (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")))
6879
6880 ;; Walking around group mode buffer from summary mode.
6881
6882 (defun gnus-summary-next-group (&optional no-article target-group backward)
6883   "Exit current newsgroup and then select next unread newsgroup.
6884 If prefix argument NO-ARTICLE is non-nil, no article is selected
6885 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6886 previous group instead."
6887   (interactive "P")
6888   ;; Stop pre-fetching.
6889   (gnus-async-halt-prefetch)
6890   (let ((current-group gnus-newsgroup-name)
6891         (current-buffer (current-buffer))
6892         entered)
6893     ;; First we semi-exit this group to update Xrefs and all variables.
6894     ;; We can't do a real exit, because the window conf must remain
6895     ;; the same in case the user is prompted for info, and we don't
6896     ;; want the window conf to change before that...
6897     (gnus-summary-exit t)
6898     (while (not entered)
6899       ;; Then we find what group we are supposed to enter.
6900       (set-buffer gnus-group-buffer)
6901       (gnus-group-jump-to-group current-group)
6902       (setq target-group
6903             (or target-group
6904                 (if (eq gnus-keep-same-level 'best)
6905                     (gnus-summary-best-group gnus-newsgroup-name)
6906                   (gnus-summary-search-group backward gnus-keep-same-level))))
6907       (if (not target-group)
6908           ;; There are no further groups, so we return to the group
6909           ;; buffer.
6910           (progn
6911             (gnus-message 5 "Returning to the group buffer")
6912             (setq entered t)
6913             (when (gnus-buffer-live-p current-buffer)
6914               (set-buffer current-buffer)
6915               (gnus-summary-exit))
6916             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6917         ;; We try to enter the target group.
6918         (gnus-group-jump-to-group target-group)
6919         (let ((unreads (gnus-group-group-unread)))
6920           (if (and (or (eq t unreads)
6921                        (and unreads (not (zerop unreads))))
6922                    (gnus-summary-read-group
6923                     target-group nil no-article
6924                     (and (buffer-name current-buffer) current-buffer)
6925                     nil backward))
6926               (setq entered t)
6927             (setq current-group target-group
6928                   target-group nil)))))))
6929
6930 (defun gnus-summary-prev-group (&optional no-article)
6931   "Exit current newsgroup and then select previous unread newsgroup.
6932 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6933   (interactive "P")
6934   (gnus-summary-next-group no-article nil t))
6935
6936 ;; Walking around summary lines.
6937
6938 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6939   "Go to the first subject satisfying any non-nil constraint.
6940 If UNREAD is non-nil, the article should be unread.
6941 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6942 If UNSEEN is non-nil, the article should be unseen.
6943 Returns the article selected or nil if there are no matching articles."
6944   (interactive "P")
6945   (cond
6946    ;; Empty summary.
6947    ((null gnus-newsgroup-data)
6948     (gnus-message 3 "No articles in the group")
6949     nil)
6950    ;; Pick the first article.
6951    ((not (or unread undownloaded unseen))
6952     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6953     (gnus-data-number (car gnus-newsgroup-data)))
6954    ;; Find the first unread article.
6955    (t
6956     (let ((data gnus-newsgroup-data))
6957       (while (and data
6958                   (let ((num (gnus-data-number (car data))))
6959                     (or (memq num gnus-newsgroup-unfetched)
6960                         (not (or (and unread
6961                                       (memq num gnus-newsgroup-unreads))
6962                                  (and undownloaded
6963                                       (memq num gnus-newsgroup-undownloaded))
6964                                  (and unseen
6965                                       (memq num gnus-newsgroup-unseen)))))))
6966         (setq data (cdr data)))
6967       (prog1
6968           (if data
6969               (progn
6970                 (goto-char (gnus-data-pos (car data)))
6971                 (gnus-data-number (car data)))
6972             (gnus-message 3 "No more%s articles"
6973                           (let* ((r (when unread " unread"))
6974                                  (d (when undownloaded " undownloaded"))
6975                                  (s (when unseen " unseen"))
6976                                  (l (delq nil (list r d s))))
6977                             (cond ((= 3 (length l))
6978                                    (concat r "," d ", or" s))
6979                                   ((= 2 (length l))
6980                                    (concat (car l) ", or" (cadr l)))
6981                                   ((= 1 (length l))
6982                                    (car l))
6983                                   (t
6984                                    ""))))
6985             nil
6986             )
6987         (gnus-summary-position-point))))))
6988
6989 (defun gnus-summary-next-subject (n &optional unread dont-display)
6990   "Go to next N'th summary line.
6991 If N is negative, go to the previous N'th subject line.
6992 If UNREAD is non-nil, only unread articles are selected.
6993 The difference between N and the actual number of steps taken is
6994 returned."
6995   (interactive "p")
6996   (let ((backward (< n 0))
6997         (n (abs n)))
6998     (while (and (> n 0)
6999                 (if backward
7000                     (gnus-summary-find-prev unread)
7001                   (gnus-summary-find-next unread)))
7002       (unless (zerop (setq n (1- n)))
7003         (gnus-summary-show-thread)))
7004     (when (/= 0 n)
7005       (gnus-message 7 "No more%s articles"
7006                     (if unread " unread" "")))
7007     (unless dont-display
7008       (gnus-summary-recenter)
7009       (gnus-summary-position-point))
7010     n))
7011
7012 (defun gnus-summary-next-unread-subject (n)
7013   "Go to next N'th unread summary line."
7014   (interactive "p")
7015   (gnus-summary-next-subject n t))
7016
7017 (defun gnus-summary-prev-subject (n &optional unread)
7018   "Go to previous N'th summary line.
7019 If optional argument UNREAD is non-nil, only unread article is selected."
7020   (interactive "p")
7021   (gnus-summary-next-subject (- n) unread))
7022
7023 (defun gnus-summary-prev-unread-subject (n)
7024   "Go to previous N'th unread summary line."
7025   (interactive "p")
7026   (gnus-summary-next-subject (- n) t))
7027
7028 (defun gnus-summary-goto-subjects (articles)
7029   "Insert the subject header for ARTICLES in the current buffer."
7030   (save-excursion
7031     (dolist (article articles)
7032       (gnus-summary-goto-subject article t)))
7033   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7034   (gnus-summary-position-point))
7035  
7036 (defun gnus-summary-goto-subject (article &optional force silent)
7037   "Go the subject line of ARTICLE.
7038 If FORCE, also allow jumping to articles not currently shown."
7039   (interactive "nArticle number: ")
7040   (unless (numberp article)
7041     (error "Article %s is not a number" article))
7042   (let ((b (point))
7043         (data (gnus-data-find article)))
7044     ;; We read in the article if we have to.
7045     (and (not data)
7046          force
7047          (gnus-summary-insert-subject
7048           article
7049           (if (or (numberp force) (vectorp force)) force)
7050           t)
7051          (setq data (gnus-data-find article)))
7052     (goto-char b)
7053     (if (not data)
7054         (progn
7055           (unless silent
7056             (gnus-message 3 "Can't find article %d" article))
7057           nil)
7058       (let ((pt (gnus-data-pos data)))
7059         (goto-char pt)
7060         (gnus-summary-set-article-display-arrow pt))
7061       (gnus-summary-position-point)
7062       article)))
7063
7064 ;; Walking around summary lines with displaying articles.
7065
7066 (defun gnus-summary-expand-window (&optional arg)
7067   "Make the summary buffer take up the entire Emacs frame.
7068 Given a prefix, will force an `article' buffer configuration."
7069   (interactive "P")
7070   (if arg
7071       (gnus-configure-windows 'article 'force)
7072     (gnus-configure-windows 'summary 'force)))
7073
7074 (defun gnus-summary-display-article (article &optional all-header)
7075   "Display ARTICLE in article buffer."
7076   (when (gnus-buffer-live-p gnus-article-buffer)
7077     (with-current-buffer gnus-article-buffer
7078       (mm-enable-multibyte)))
7079   (gnus-set-global-variables)
7080   (when (gnus-buffer-live-p gnus-article-buffer)
7081     (with-current-buffer gnus-article-buffer
7082       (setq gnus-article-charset gnus-newsgroup-charset)
7083       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7084       (mm-enable-multibyte)))
7085   (if (null article)
7086       nil
7087     (prog1
7088         (if gnus-summary-display-article-function
7089             (funcall gnus-summary-display-article-function article all-header)
7090           (gnus-article-prepare article all-header))
7091       (gnus-run-hooks 'gnus-select-article-hook)
7092       (when (and gnus-current-article
7093                  (not (zerop gnus-current-article)))
7094         (gnus-summary-goto-subject gnus-current-article))
7095       (gnus-summary-recenter)
7096       (when (and gnus-use-trees gnus-show-threads)
7097         (gnus-possibly-generate-tree article)
7098         (gnus-highlight-selected-tree article))
7099       ;; Successfully display article.
7100       (gnus-article-set-window-start
7101        (cdr (assq article gnus-newsgroup-bookmarks))))))
7102
7103 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7104   "Select the current article.
7105 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7106 non-nil, the article will be re-fetched even if it already present in
7107 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7108 be displayed."
7109   ;; Make sure we are in the summary buffer to work around bbdb bug.
7110   (unless (eq major-mode 'gnus-summary-mode)
7111     (set-buffer gnus-summary-buffer))
7112   (let ((article (or article (gnus-summary-article-number)))
7113         (all-headers (not (not all-headers))) ;Must be t or nil.
7114         gnus-summary-display-article-function)
7115     (and (not pseudo)
7116          (gnus-summary-article-pseudo-p article)
7117          (error "This is a pseudo-article"))
7118     (save-excursion
7119       (set-buffer gnus-summary-buffer)
7120       (if (or (and gnus-single-article-buffer
7121                    (or (null gnus-current-article)
7122                        (null gnus-article-current)
7123                        (null (get-buffer gnus-article-buffer))
7124                        (not (eq article (cdr gnus-article-current)))
7125                        (not (equal (car gnus-article-current)
7126                                    gnus-newsgroup-name))))
7127               (and (not gnus-single-article-buffer)
7128                    (or (null gnus-current-article)
7129                        (not (eq gnus-current-article article))))
7130               force)
7131           ;; The requested article is different from the current article.
7132           (progn
7133             (gnus-summary-display-article article all-headers)
7134             (when (gnus-buffer-live-p gnus-article-buffer)
7135               (with-current-buffer gnus-article-buffer
7136                 (if (not gnus-article-decoded-p) ;; a local variable
7137                     (mm-disable-multibyte))))
7138             (gnus-article-set-window-start
7139              (cdr (assq article gnus-newsgroup-bookmarks)))
7140             article)
7141         'old))))
7142
7143 (defun gnus-summary-force-verify-and-decrypt ()
7144   "Display buttons for signed/encrypted parts and verify/decrypt them."
7145   (interactive)
7146   (let ((mm-verify-option 'known)
7147         (mm-decrypt-option 'known)
7148         (gnus-article-emulate-mime t)
7149         (gnus-buttonized-mime-types (append (list "multipart/signed"
7150                                                   "multipart/encrypted")
7151                                             gnus-buttonized-mime-types)))
7152     (gnus-summary-select-article nil 'force)))
7153
7154 (defun gnus-summary-set-current-mark (&optional current-mark)
7155   "Obsolete function."
7156   nil)
7157
7158 (defun gnus-summary-next-article (&optional unread subject backward push)
7159   "Select the next article.
7160 If UNREAD, only unread articles are selected.
7161 If SUBJECT, only articles with SUBJECT are selected.
7162 If BACKWARD, the previous article is selected instead of the next."
7163   (interactive "P")
7164   (cond
7165    ;; Is there such an article?
7166    ((and (gnus-summary-search-forward unread subject backward)
7167          (or (gnus-summary-display-article (gnus-summary-article-number))
7168              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7169     (gnus-summary-position-point))
7170    ;; If not, we try the first unread, if that is wanted.
7171    ((and subject
7172          gnus-auto-select-same
7173          (gnus-summary-first-unread-article))
7174     (gnus-summary-position-point)
7175     (gnus-message 6 "Wrapped"))
7176    ;; Try to get next/previous article not displayed in this group.
7177    ((and gnus-auto-extend-newsgroup
7178          (not unread) (not subject))
7179     (gnus-summary-goto-article
7180      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7181      nil (count-lines (point-min) (point))))
7182    ;; Go to next/previous group.
7183    (t
7184     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7185       (gnus-summary-jump-to-group gnus-newsgroup-name))
7186     (let ((cmd last-command-char)
7187           (point
7188            (with-current-buffer gnus-group-buffer
7189              (point)))
7190           (group
7191            (if (eq gnus-keep-same-level 'best)
7192                (gnus-summary-best-group gnus-newsgroup-name)
7193              (gnus-summary-search-group backward gnus-keep-same-level))))
7194       ;; For some reason, the group window gets selected.  We change
7195       ;; it back.
7196       (select-window (get-buffer-window (current-buffer)))
7197       ;; Select next unread newsgroup automagically.
7198       (cond
7199        ((or (not gnus-auto-select-next)
7200             (not cmd))
7201         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7202        ((or (eq gnus-auto-select-next 'quietly)
7203             (and (eq gnus-auto-select-next 'slightly-quietly)
7204                  push)
7205             (and (eq gnus-auto-select-next 'almost-quietly)
7206                  (gnus-summary-last-article-p)))
7207         ;; Select quietly.
7208         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7209             (gnus-summary-exit)
7210           (gnus-message 7 "No more%s articles (%s)..."
7211                         (if unread " unread" "")
7212                         (if group (concat "selecting " group)
7213                           "exiting"))
7214           (gnus-summary-next-group nil group backward)))
7215        (t
7216         (when (gnus-key-press-event-p last-input-event)
7217           (gnus-summary-walk-group-buffer
7218            gnus-newsgroup-name cmd unread backward point))))))))
7219
7220 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7221   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7222                       (?\C-p (gnus-group-prev-unread-group 1))))
7223         (cursor-in-echo-area t)
7224         keve key group ended prompt)
7225     (save-excursion
7226       (set-buffer gnus-group-buffer)
7227       (goto-char start)
7228       (setq group
7229             (if (eq gnus-keep-same-level 'best)
7230                 (gnus-summary-best-group gnus-newsgroup-name)
7231               (gnus-summary-search-group backward gnus-keep-same-level))))
7232     (while (not ended)
7233       (setq prompt
7234             (format
7235              "No more%s articles%s " (if unread " unread" "")
7236              (if (and group
7237                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7238                  (format " (Type %s for %s [%s])"
7239                          (single-key-description cmd) group
7240                          (gnus-group-unread group))
7241                (format " (Type %s to exit %s)"
7242                        (single-key-description cmd)
7243                        gnus-newsgroup-name))))
7244       ;; Confirm auto selection.
7245       (setq key (car (setq keve (gnus-read-event-char prompt)))
7246             ended t)
7247       (cond
7248        ((assq key keystrokes)
7249         (let ((obuf (current-buffer)))
7250           (switch-to-buffer gnus-group-buffer)
7251           (when group
7252             (gnus-group-jump-to-group group))
7253           (eval (cadr (assq key keystrokes)))
7254           (setq group (gnus-group-group-name))
7255           (switch-to-buffer obuf))
7256         (setq ended nil))
7257        ((equal key cmd)
7258         (if (or (not group)
7259                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7260             (gnus-summary-exit)
7261           (gnus-summary-next-group nil group backward)))
7262        (t
7263         (push (cdr keve) unread-command-events))))))
7264
7265 (defun gnus-summary-next-unread-article ()
7266   "Select unread article after current one."
7267   (interactive)
7268   (gnus-summary-next-article
7269    (or (not (eq gnus-summary-goto-unread 'never))
7270        (gnus-summary-last-article-p (gnus-summary-article-number)))
7271    (and gnus-auto-select-same
7272         (gnus-summary-article-subject))))
7273
7274 (defun gnus-summary-prev-article (&optional unread subject)
7275   "Select the article after the current one.
7276 If UNREAD is non-nil, only unread articles are selected."
7277   (interactive "P")
7278   (gnus-summary-next-article unread subject t))
7279
7280 (defun gnus-summary-prev-unread-article ()
7281   "Select unread article before current one."
7282   (interactive)
7283   (gnus-summary-prev-article
7284    (or (not (eq gnus-summary-goto-unread 'never))
7285        (gnus-summary-first-article-p (gnus-summary-article-number)))
7286    (and gnus-auto-select-same
7287         (gnus-summary-article-subject))))
7288
7289 (defun gnus-summary-next-page (&optional lines circular stop)
7290   "Show next page of the selected article.
7291 If at the end of the current article, select the next article.
7292 LINES says how many lines should be scrolled up.
7293
7294 If CIRCULAR is non-nil, go to the start of the article instead of
7295 selecting the next article when reaching the end of the current
7296 article.
7297
7298 If STOP is non-nil, just stop when reaching the end of the message.
7299
7300 Also see the variable `gnus-article-skip-boring'."
7301   (interactive "P")
7302   (setq gnus-summary-buffer (current-buffer))
7303   (gnus-set-global-variables)
7304   (let ((article (gnus-summary-article-number))
7305         (article-window (get-buffer-window gnus-article-buffer t))
7306         endp)
7307     ;; If the buffer is empty, we have no article.
7308     (unless article
7309       (error "No article to select"))
7310     (gnus-configure-windows 'article)
7311     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7312         (if (and (eq gnus-summary-goto-unread 'never)
7313                  (not (gnus-summary-last-article-p article)))
7314             (gnus-summary-next-article)
7315           (gnus-summary-next-unread-article))
7316       (if (or (null gnus-current-article)
7317               (null gnus-article-current)
7318               (/= article (cdr gnus-article-current))
7319               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7320           ;; Selected subject is different from current article's.
7321           (gnus-summary-display-article article)
7322         (when article-window
7323           (gnus-eval-in-buffer-window gnus-article-buffer
7324             (setq endp (or (gnus-article-next-page lines)
7325                            (gnus-article-only-boring-p))))
7326           (when endp
7327             (cond (stop
7328                    (gnus-message 3 "End of message"))
7329                   (circular
7330                    (gnus-summary-beginning-of-article))
7331                   (lines
7332                    (gnus-message 3 "End of message"))
7333                   ((null lines)
7334                    (if (and (eq gnus-summary-goto-unread 'never)
7335                             (not (gnus-summary-last-article-p article)))
7336                        (gnus-summary-next-article)
7337                      (gnus-summary-next-unread-article))))))))
7338     (gnus-summary-recenter)
7339     (gnus-summary-position-point)))
7340
7341 (defun gnus-summary-prev-page (&optional lines move)
7342   "Show previous page of selected article.
7343 Argument LINES specifies lines to be scrolled down.
7344 If MOVE, move to the previous unread article if point is at
7345 the beginning of the buffer."
7346   (interactive "P")
7347   (let ((article (gnus-summary-article-number))
7348         (article-window (get-buffer-window gnus-article-buffer t))
7349         endp)
7350     (gnus-configure-windows 'article)
7351     (if (or (null gnus-current-article)
7352             (null gnus-article-current)
7353             (/= article (cdr gnus-article-current))
7354             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7355         ;; Selected subject is different from current article's.
7356         (gnus-summary-display-article article)
7357       (gnus-summary-recenter)
7358       (when article-window
7359         (gnus-eval-in-buffer-window gnus-article-buffer
7360           (setq endp (gnus-article-prev-page lines)))
7361         (when (and move endp)
7362           (cond (lines
7363                  (gnus-message 3 "Beginning of message"))
7364                 ((null lines)
7365                  (if (and (eq gnus-summary-goto-unread 'never)
7366                           (not (gnus-summary-first-article-p article)))
7367                      (gnus-summary-prev-article)
7368                    (gnus-summary-prev-unread-article))))))))
7369   (gnus-summary-position-point))
7370
7371 (defun gnus-summary-prev-page-or-article (&optional lines)
7372   "Show previous page of selected article.
7373 Argument LINES specifies lines to be scrolled down.
7374 If at the beginning of the article, go to the next article."
7375   (interactive "P")
7376   (gnus-summary-prev-page lines t))
7377
7378 (defun gnus-summary-scroll-up (lines)
7379   "Scroll up (or down) one line current article.
7380 Argument LINES specifies lines to be scrolled up (or down if negative)."
7381   (interactive "p")
7382   (gnus-configure-windows 'article)
7383   (gnus-summary-show-thread)
7384   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7385     (gnus-eval-in-buffer-window gnus-article-buffer
7386       (cond ((> lines 0)
7387              (when (gnus-article-next-page lines)
7388                (gnus-message 3 "End of message")))
7389             ((< lines 0)
7390              (gnus-article-prev-page (- lines))))))
7391   (gnus-summary-recenter)
7392   (gnus-summary-position-point))
7393
7394 (defun gnus-summary-scroll-down (lines)
7395   "Scroll down (or up) one line current article.
7396 Argument LINES specifies lines to be scrolled down (or up if negative)."
7397   (interactive "p")
7398   (gnus-summary-scroll-up (- lines)))
7399
7400 (defun gnus-summary-next-same-subject ()
7401   "Select next article which has the same subject as current one."
7402   (interactive)
7403   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7404
7405 (defun gnus-summary-prev-same-subject ()
7406   "Select previous article which has the same subject as current one."
7407   (interactive)
7408   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7409
7410 (defun gnus-summary-next-unread-same-subject ()
7411   "Select next unread article which has the same subject as current one."
7412   (interactive)
7413   (gnus-summary-next-article t (gnus-summary-article-subject)))
7414
7415 (defun gnus-summary-prev-unread-same-subject ()
7416   "Select previous unread article which has the same subject as current one."
7417   (interactive)
7418   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7419
7420 (defun gnus-summary-first-unread-article ()
7421   "Select the first unread article.
7422 Return nil if there are no unread articles."
7423   (interactive)
7424   (prog1
7425       (when (gnus-summary-first-subject t)
7426         (gnus-summary-show-thread)
7427         (gnus-summary-first-subject t)
7428         (gnus-summary-display-article (gnus-summary-article-number)))
7429     (gnus-summary-position-point)))
7430
7431 (defun gnus-summary-first-unread-subject ()
7432   "Place the point on the subject line of the first unread article.
7433 Return nil if there are no unread articles."
7434   (interactive)
7435   (prog1
7436       (when (gnus-summary-first-subject t)
7437         (gnus-summary-show-thread)
7438         (gnus-summary-first-subject t))
7439     (gnus-summary-position-point)))
7440
7441 (defun gnus-summary-first-unseen-subject ()
7442   "Place the point on the subject line of the first unseen article.
7443 Return nil if there are no unseen articles."
7444   (interactive)
7445   (prog1
7446       (when (gnus-summary-first-subject nil nil t)
7447         (gnus-summary-show-thread)
7448         (gnus-summary-first-subject nil nil t))
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-first-unseen-or-unread-subject ()
7452   "Place the point on the subject line of the first unseen article or,
7453 if all article have been seen, on the subject line of the first unread
7454 article."
7455   (interactive)
7456   (prog1
7457       (unless (when (gnus-summary-first-subject nil nil t)
7458                 (gnus-summary-show-thread)
7459                 (gnus-summary-first-subject nil nil t))
7460         (when (gnus-summary-first-subject t)
7461           (gnus-summary-show-thread)
7462           (gnus-summary-first-subject t)))
7463     (gnus-summary-position-point)))
7464
7465 (defun gnus-summary-first-article ()
7466   "Select the first article.
7467 Return nil if there are no articles."
7468   (interactive)
7469   (prog1
7470       (when (gnus-summary-first-subject)
7471         (gnus-summary-show-thread)
7472         (gnus-summary-first-subject)
7473         (gnus-summary-display-article (gnus-summary-article-number)))
7474     (gnus-summary-position-point)))
7475
7476 (defun gnus-summary-best-unread-article (&optional arg)
7477   "Select the unread article with the highest score.
7478 If given a prefix argument, select the next unread article that has a
7479 score higher than the default score."
7480   (interactive "P")
7481   (let ((article (if arg
7482                      (gnus-summary-better-unread-subject)
7483                    (gnus-summary-best-unread-subject))))
7484     (if article
7485         (gnus-summary-goto-article article)
7486       (error "No unread articles"))))
7487
7488 (defun gnus-summary-best-unread-subject ()
7489   "Select the unread subject with the highest score."
7490   (interactive)
7491   (let ((best -1000000)
7492         (data gnus-newsgroup-data)
7493         article score)
7494     (while data
7495       (and (gnus-data-unread-p (car data))
7496            (> (setq score
7497                     (gnus-summary-article-score (gnus-data-number (car data))))
7498               best)
7499            (setq best score
7500                  article (gnus-data-number (car data))))
7501       (setq data (cdr data)))
7502     (when article
7503       (gnus-summary-goto-subject article))
7504     (gnus-summary-position-point)
7505     article))
7506
7507 (defun gnus-summary-better-unread-subject ()
7508   "Select the first unread subject that has a score over the default score."
7509   (interactive)
7510   (let ((data gnus-newsgroup-data)
7511         article score)
7512     (while (and (setq article (gnus-data-number (car data)))
7513                 (or (gnus-data-read-p (car data))
7514                     (not (> (gnus-summary-article-score article)
7515                             gnus-summary-default-score))))
7516       (setq data (cdr data)))
7517     (when article
7518       (gnus-summary-goto-subject article))
7519     (gnus-summary-position-point)
7520     article))
7521
7522 (defun gnus-summary-last-subject ()
7523   "Go to the last displayed subject line in the group."
7524   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7525     (when article
7526       (gnus-summary-goto-subject article))))
7527
7528 (defun gnus-summary-goto-article (article &optional all-headers force)
7529   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7530 If ALL-HEADERS is non-nil, no header lines are hidden.
7531 If FORCE, go to the article even if it isn't displayed.  If FORCE
7532 is a number, it is the line the article is to be displayed on."
7533   (interactive
7534    (list
7535     (completing-read
7536      "Article number or Message-ID: "
7537      (mapcar (lambda (number) (list (int-to-string number)))
7538              gnus-newsgroup-limit))
7539     current-prefix-arg
7540     t))
7541   (prog1
7542       (if (and (stringp article)
7543                (string-match "@\\|%40" article))
7544           (gnus-summary-refer-article article)
7545         (when (stringp article)
7546           (setq article (string-to-number article)))
7547         (if (gnus-summary-goto-subject article force)
7548             (gnus-summary-display-article article all-headers)
7549           (gnus-message 4 "Couldn't go to article %s" article) nil))
7550     (gnus-summary-position-point)))
7551
7552 (defun gnus-summary-goto-last-article ()
7553   "Go to the previously read article."
7554   (interactive)
7555   (prog1
7556       (when gnus-last-article
7557         (gnus-summary-goto-article gnus-last-article nil t))
7558     (gnus-summary-position-point)))
7559
7560 (defun gnus-summary-pop-article (number)
7561   "Pop one article off the history and go to the previous.
7562 NUMBER articles will be popped off."
7563   (interactive "p")
7564   (let (to)
7565     (setq gnus-newsgroup-history
7566           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7567     (if to
7568         (gnus-summary-goto-article (car to) nil t)
7569       (error "Article history empty")))
7570   (gnus-summary-position-point))
7571
7572 ;; Summary commands and functions for limiting the summary buffer.
7573
7574 (defun gnus-summary-limit-to-articles (n)
7575   "Limit the summary buffer to the next N articles.
7576 If not given a prefix, use the process marked articles instead."
7577   (interactive "P")
7578   (prog1
7579       (let ((articles (gnus-summary-work-articles n)))
7580         (setq gnus-newsgroup-processable nil)
7581         (gnus-summary-limit articles))
7582     (gnus-summary-position-point)))
7583
7584 (defun gnus-summary-pop-limit (&optional total)
7585   "Restore the previous limit.
7586 If given a prefix, remove all limits."
7587   (interactive "P")
7588   (when total
7589     (setq gnus-newsgroup-limits
7590           (list (mapcar (lambda (h) (mail-header-number h))
7591                         gnus-newsgroup-headers))))
7592   (unless gnus-newsgroup-limits
7593     (error "No limit to pop"))
7594   (prog1
7595       (gnus-summary-limit nil 'pop)
7596     (gnus-summary-position-point)))
7597
7598 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7599   "Limit the summary buffer to articles that have subjects that match a regexp.
7600 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7601   (interactive
7602    (list (read-string (if current-prefix-arg
7603                           "Exclude subject (regexp): "
7604                         "Limit to subject (regexp): "))
7605          nil current-prefix-arg))
7606   (unless header
7607     (setq header "subject"))
7608   (when (not (equal "" subject))
7609     (prog1
7610         (let ((articles (gnus-summary-find-matching
7611                          (or header "subject") subject 'all nil nil
7612                          not-matching)))
7613           (unless articles
7614             (error "Found no matches for \"%s\"" subject))
7615           (gnus-summary-limit articles))
7616       (gnus-summary-position-point))))
7617
7618 (defun gnus-summary-limit-to-author (from &optional not-matching)
7619   "Limit the summary buffer to articles that have authors that match a regexp.
7620 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7621   (interactive
7622    (list (read-string (if current-prefix-arg
7623                           "Exclude author (regexp): "
7624                         "Limit to author (regexp): "))
7625          current-prefix-arg))
7626   (gnus-summary-limit-to-subject from "from" not-matching))
7627
7628 (defun gnus-summary-limit-to-age (age &optional younger-p)
7629   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7630 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7631 articles that are younger than AGE days."
7632   (interactive
7633    (let ((younger current-prefix-arg)
7634          (days-got nil)
7635          days)
7636      (while (not days-got)
7637        (setq days (if younger
7638                       (read-string "Limit to articles younger than (in days, older when negative): ")
7639                     (read-string
7640                      "Limit to articles older than (in days, younger when negative): ")))
7641        (when (> (length days) 0)
7642          (setq days (read days)))
7643        (if (numberp days)
7644            (progn
7645              (setq days-got t)
7646              (when (< days 0)
7647                (setq younger (not younger))
7648                (setq days (* days -1))))
7649          (message "Please enter a number.")
7650          (sleep-for 1)))
7651      (list days younger)))
7652   (prog1
7653       (let ((data gnus-newsgroup-data)
7654             (cutoff (days-to-time age))
7655             articles d date is-younger)
7656         (while (setq d (pop data))
7657           (when (and (vectorp (gnus-data-header d))
7658                      (setq date (mail-header-date (gnus-data-header d))))
7659             (setq is-younger (time-less-p
7660                               (time-since (condition-case ()
7661                                               (date-to-time date)
7662                                             (error '(0 0))))
7663                               cutoff))
7664             (when (if younger-p
7665                       is-younger
7666                     (not is-younger))
7667               (push (gnus-data-number d) articles))))
7668         (gnus-summary-limit (nreverse articles)))
7669     (gnus-summary-position-point)))
7670
7671 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7672   "Limit the summary buffer to articles that match an 'extra' header."
7673   (interactive
7674    (let ((header
7675           (intern
7676            (gnus-completing-read-with-default
7677             (symbol-name (car gnus-extra-headers))
7678             (if current-prefix-arg
7679                 "Exclude extra header:"
7680               "Limit extra header:")
7681             (mapcar (lambda (x)
7682                       (cons (symbol-name x) x))
7683                     gnus-extra-headers)
7684             nil
7685             t))))
7686      (list header
7687            (read-string (format "%s header %s (regexp): "
7688                                 (if current-prefix-arg "Exclude" "Limit to")
7689                                 header))
7690            current-prefix-arg)))
7691   (when (not (equal "" regexp))
7692     (prog1
7693         (let ((articles (gnus-summary-find-matching
7694                          (cons 'extra header) regexp 'all nil nil
7695                          not-matching)))
7696           (unless articles
7697             (error "Found no matches for \"%s\"" regexp))
7698           (gnus-summary-limit articles))
7699       (gnus-summary-position-point))))
7700
7701 (defun gnus-summary-limit-to-display-predicate ()
7702   "Limit the summary buffer to the predicated in the `display' group parameter."
7703   (interactive)
7704   (unless gnus-newsgroup-display
7705     (error "There is no `display' group parameter"))
7706   (let (articles)
7707     (dolist (number gnus-newsgroup-articles)
7708       (when (funcall gnus-newsgroup-display)
7709         (push number articles)))
7710     (gnus-summary-limit articles))
7711   (gnus-summary-position-point))
7712
7713 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7714 (make-obsolete
7715  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7716
7717 (defun gnus-summary-limit-to-unread (&optional all)
7718   "Limit the summary buffer to articles that are not marked as read.
7719 If ALL is non-nil, limit strictly to unread articles."
7720   (interactive "P")
7721   (if all
7722       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7723     (gnus-summary-limit-to-marks
7724      ;; Concat all the marks that say that an article is read and have
7725      ;; those removed.
7726      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7727            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7728            gnus-low-score-mark gnus-expirable-mark
7729            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7730            gnus-duplicate-mark gnus-souped-mark)
7731      'reverse)))
7732
7733 (defun gnus-summary-limit-to-replied (&optional unreplied)
7734   "Limit the summary buffer to replied articles.
7735 If UNREPLIED (the prefix), limit to unreplied articles."
7736   (interactive "P")
7737   (if unreplied
7738       (gnus-summary-limit
7739        (gnus-set-difference gnus-newsgroup-articles
7740         gnus-newsgroup-replied))
7741     (gnus-summary-limit gnus-newsgroup-replied))
7742   (gnus-summary-position-point))
7743
7744 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7745 (make-obsolete 'gnus-summary-delete-marked-with
7746                'gnus-summary-limit-exclude-marks)
7747
7748 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7749   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7750 If REVERSE, limit the summary buffer to articles that are marked
7751 with MARKS.  MARKS can either be a string of marks or a list of marks.
7752 Returns how many articles were removed."
7753   (interactive "sMarks: ")
7754   (gnus-summary-limit-to-marks marks t))
7755
7756 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7757   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7758 If REVERSE (the prefix), limit the summary buffer to articles that are
7759 not marked with MARKS.  MARKS can either be a string of marks or a
7760 list of marks.
7761 Returns how many articles were removed."
7762   (interactive "sMarks: \nP")
7763   (prog1
7764       (let ((data gnus-newsgroup-data)
7765             (marks (if (listp marks) marks
7766                      (append marks nil))) ; Transform to list.
7767             articles)
7768         (while data
7769           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7770                   (memq (gnus-data-mark (car data)) marks))
7771             (push (gnus-data-number (car data)) articles))
7772           (setq data (cdr data)))
7773         (gnus-summary-limit articles))
7774     (gnus-summary-position-point)))
7775
7776 (defun gnus-summary-limit-to-score (score)
7777   "Limit to articles with score at or above SCORE."
7778   (interactive "NLimit to articles with score of at least: ")
7779   (let ((data gnus-newsgroup-data)
7780         articles)
7781     (while data
7782       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7783                 score)
7784         (push (gnus-data-number (car data)) articles))
7785       (setq data (cdr data)))
7786     (prog1
7787         (gnus-summary-limit articles)
7788       (gnus-summary-position-point))))
7789
7790 (defun gnus-summary-limit-to-unseen ()
7791   "Limit to unseen articles."
7792   (interactive)
7793   (prog1
7794       (gnus-summary-limit gnus-newsgroup-unseen)
7795     (gnus-summary-position-point)))
7796
7797 (defun gnus-summary-limit-include-thread (id)
7798   "Display all the hidden articles that is in the thread with ID in it.
7799 When called interactively, ID is the Message-ID of the current
7800 article."
7801   (interactive (list (mail-header-id (gnus-summary-article-header))))
7802   (let ((articles (gnus-articles-in-thread
7803                    (gnus-id-to-thread (gnus-root-id id)))))
7804     (prog1
7805         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7806       (gnus-summary-limit-include-matching-articles
7807        "subject"
7808        (regexp-quote (gnus-simplify-subject-re
7809                       (mail-header-subject (gnus-id-to-header id)))))
7810       (gnus-summary-position-point))))
7811
7812 (defun gnus-summary-limit-include-matching-articles (header regexp)
7813   "Display all the hidden articles that have HEADERs that match REGEXP."
7814   (interactive (list (read-string "Match on header: ")
7815                      (read-string "Regexp: ")))
7816   (let ((articles (gnus-find-matching-articles header regexp)))
7817     (prog1
7818         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7819       (gnus-summary-position-point))))
7820
7821 (defun gnus-summary-insert-dormant-articles ()
7822   "Insert all the dormant articles for this group into the current buffer."
7823   (interactive)
7824   (let ((gnus-verbose (max 6 gnus-verbose)))
7825     (if (not gnus-newsgroup-dormant)
7826         (gnus-message 3 "No cached articles for this group")
7827       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7828
7829 (defun gnus-summary-limit-include-dormant ()
7830   "Display all the hidden articles that are marked as dormant.
7831 Note that this command only works on a subset of the articles currently
7832 fetched for this group."
7833   (interactive)
7834   (unless gnus-newsgroup-dormant
7835     (error "There are no dormant articles in this group"))
7836   (prog1
7837       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7838     (gnus-summary-position-point)))
7839
7840 (defun gnus-summary-limit-exclude-dormant ()
7841   "Hide all dormant articles."
7842   (interactive)
7843   (prog1
7844       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7845     (gnus-summary-position-point)))
7846
7847 (defun gnus-summary-limit-exclude-childless-dormant ()
7848   "Hide all dormant articles that have no children."
7849   (interactive)
7850   (let ((data (gnus-data-list t))
7851         articles d children)
7852     ;; Find all articles that are either not dormant or have
7853     ;; children.
7854     (while (setq d (pop data))
7855       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7856                 (and (setq children
7857                            (gnus-article-children (gnus-data-number d)))
7858                      (let (found)
7859                        (while children
7860                          (when (memq (car children) articles)
7861                            (setq children nil
7862                                  found t))
7863                          (pop children))
7864                        found)))
7865         (push (gnus-data-number d) articles)))
7866     ;; Do the limiting.
7867     (prog1
7868         (gnus-summary-limit articles)
7869       (gnus-summary-position-point))))
7870
7871 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7872   "Mark all unread excluded articles as read.
7873 If ALL, mark even excluded ticked and dormants as read."
7874   (interactive "P")
7875   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7876   (let ((articles (gnus-sorted-ndifference
7877                    (sort
7878                     (mapcar (lambda (h) (mail-header-number h))
7879                             gnus-newsgroup-headers)
7880                     '<)
7881                    gnus-newsgroup-limit))
7882         article)
7883     (setq gnus-newsgroup-unreads
7884           (gnus-sorted-intersection gnus-newsgroup-unreads
7885                                     gnus-newsgroup-limit))
7886     (if all
7887         (setq gnus-newsgroup-dormant nil
7888               gnus-newsgroup-marked nil
7889               gnus-newsgroup-reads
7890               (nconc
7891                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7892                gnus-newsgroup-reads))
7893       (while (setq article (pop articles))
7894         (unless (or (memq article gnus-newsgroup-dormant)
7895                     (memq article gnus-newsgroup-marked))
7896           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7897
7898 (defun gnus-summary-limit (articles &optional pop)
7899   (if pop
7900       ;; We pop the previous limit off the stack and use that.
7901       (setq articles (car gnus-newsgroup-limits)
7902             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7903     ;; We use the new limit, so we push the old limit on the stack.
7904     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7905   ;; Set the limit.
7906   (setq gnus-newsgroup-limit articles)
7907   (let ((total (length gnus-newsgroup-data))
7908         (data (gnus-data-find-list (gnus-summary-article-number)))
7909         (gnus-summary-mark-below nil)   ; Inhibit this.
7910         found)
7911     ;; This will do all the work of generating the new summary buffer
7912     ;; according to the new limit.
7913     (gnus-summary-prepare)
7914     ;; Hide any threads, possibly.
7915     (gnus-summary-maybe-hide-threads)
7916     ;; Try to return to the article you were at, or one in the
7917     ;; neighborhood.
7918     (when data
7919       ;; We try to find some article after the current one.
7920       (while data
7921         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7922           (setq data nil
7923                 found t))
7924         (setq data (cdr data))))
7925     (unless found
7926       ;; If there is no data, that means that we were after the last
7927       ;; article.  The same goes when we can't find any articles
7928       ;; after the current one.
7929       (goto-char (point-max))
7930       (gnus-summary-find-prev))
7931     (gnus-set-mode-line 'summary)
7932     ;; We return how many articles were removed from the summary
7933     ;; buffer as a result of the new limit.
7934     (- total (length gnus-newsgroup-data))))
7935
7936 (defsubst gnus-invisible-cut-children (threads)
7937   (let ((num 0))
7938     (while threads
7939       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7940         (incf num))
7941       (pop threads))
7942     (< num 2)))
7943
7944 (defsubst gnus-cut-thread (thread)
7945   "Go forwards in the thread until we find an article that we want to display."
7946   (when (or (eq gnus-fetch-old-headers 'some)
7947             (eq gnus-fetch-old-headers 'invisible)
7948             (numberp gnus-fetch-old-headers)
7949             (eq gnus-build-sparse-threads 'some)
7950             (eq gnus-build-sparse-threads 'more))
7951     ;; Deal with old-fetched headers and sparse threads.
7952     (while (and
7953             thread
7954             (or
7955              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7956              (gnus-summary-article-ancient-p
7957               (mail-header-number (car thread))))
7958             (if (or (<= (length (cdr thread)) 1)
7959                     (eq gnus-fetch-old-headers 'invisible))
7960                 (setq gnus-newsgroup-limit
7961                       (delq (mail-header-number (car thread))
7962                             gnus-newsgroup-limit)
7963                       thread (cadr thread))
7964               (when (gnus-invisible-cut-children (cdr thread))
7965                 (let ((th (cdr thread)))
7966                   (while th
7967                     (if (memq (mail-header-number (caar th))
7968                               gnus-newsgroup-limit)
7969                         (setq thread (car th)
7970                               th nil)
7971                       (setq th (cdr th))))))))))
7972   thread)
7973
7974 (defun gnus-cut-threads (threads)
7975   "Cut off all uninteresting articles from the beginning of THREADS."
7976   (when (or (eq gnus-fetch-old-headers 'some)
7977             (eq gnus-fetch-old-headers 'invisible)
7978             (numberp gnus-fetch-old-headers)
7979             (eq gnus-build-sparse-threads 'some)
7980             (eq gnus-build-sparse-threads 'more))
7981     (let ((th threads))
7982       (while th
7983         (setcar th (gnus-cut-thread (car th)))
7984         (setq th (cdr th)))))
7985   ;; Remove nixed out threads.
7986   (delq nil threads))
7987
7988 (defun gnus-summary-initial-limit (&optional show-if-empty)
7989   "Figure out what the initial limit is supposed to be on group entry.
7990 This entails weeding out unwanted dormants, low-scored articles,
7991 fetch-old-headers verbiage, and so on."
7992   ;; Most groups have nothing to remove.
7993   (if (or gnus-inhibit-limiting
7994           (and (null gnus-newsgroup-dormant)
7995                (eq gnus-newsgroup-display 'gnus-not-ignore)
7996                (not (eq gnus-fetch-old-headers 'some))
7997                (not (numberp gnus-fetch-old-headers))
7998                (not (eq gnus-fetch-old-headers 'invisible))
7999                (null gnus-summary-expunge-below)
8000                (not (eq gnus-build-sparse-threads 'some))
8001                (not (eq gnus-build-sparse-threads 'more))
8002                (null gnus-thread-expunge-below)
8003                (not gnus-use-nocem)))
8004       ()                                ; Do nothing.
8005     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8006     (setq gnus-newsgroup-limit nil)
8007     (mapatoms
8008      (lambda (node)
8009        (unless (car (symbol-value node))
8010          ;; These threads have no parents -- they are roots.
8011          (let ((nodes (cdr (symbol-value node)))
8012                thread)
8013            (while nodes
8014              (if (and gnus-thread-expunge-below
8015                       (< (gnus-thread-total-score (car nodes))
8016                          gnus-thread-expunge-below))
8017                  (gnus-expunge-thread (pop nodes))
8018                (setq thread (pop nodes))
8019                (gnus-summary-limit-children thread))))))
8020      gnus-newsgroup-dependencies)
8021     ;; If this limitation resulted in an empty group, we might
8022     ;; pop the previous limit and use it instead.
8023     (when (and (not gnus-newsgroup-limit)
8024                show-if-empty)
8025       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8026     gnus-newsgroup-limit))
8027
8028 (defun gnus-summary-limit-children (thread)
8029   "Return 1 if this subthread is visible and 0 if it is not."
8030   ;; First we get the number of visible children to this thread.  This
8031   ;; is done by recursing down the thread using this function, so this
8032   ;; will really go down to a leaf article first, before slowly
8033   ;; working its way up towards the root.
8034   (when thread
8035     (let* ((max-lisp-eval-depth 5000)
8036            (children
8037            (if (cdr thread)
8038                (apply '+ (mapcar 'gnus-summary-limit-children
8039                                  (cdr thread)))
8040              0))
8041           (number (mail-header-number (car thread)))
8042           score)
8043       (if (and
8044            (not (memq number gnus-newsgroup-marked))
8045            (or
8046             ;; If this article is dormant and has absolutely no visible
8047             ;; children, then this article isn't visible.
8048             (and (memq number gnus-newsgroup-dormant)
8049                  (zerop children))
8050             ;; If this is "fetch-old-headered" and there is no
8051             ;; visible children, then we don't want this article.
8052             (and (or (eq gnus-fetch-old-headers 'some)
8053                      (numberp gnus-fetch-old-headers))
8054                  (gnus-summary-article-ancient-p number)
8055                  (zerop children))
8056             ;; If this is "fetch-old-headered" and `invisible', then
8057             ;; we don't want this article.
8058             (and (eq gnus-fetch-old-headers 'invisible)
8059                  (gnus-summary-article-ancient-p number))
8060             ;; If this is a sparsely inserted article with no children,
8061             ;; we don't want it.
8062             (and (eq gnus-build-sparse-threads 'some)
8063                  (gnus-summary-article-sparse-p number)
8064                  (zerop children))
8065             ;; If we use expunging, and this article is really
8066             ;; low-scored, then we don't want this article.
8067             (when (and gnus-summary-expunge-below
8068                        (< (setq score
8069                                 (or (cdr (assq number gnus-newsgroup-scored))
8070                                     gnus-summary-default-score))
8071                           gnus-summary-expunge-below))
8072               ;; We increase the expunge-tally here, but that has
8073               ;; nothing to do with the limits, really.
8074               (incf gnus-newsgroup-expunged-tally)
8075               ;; We also mark as read here, if that's wanted.
8076               (when (and gnus-summary-mark-below
8077                          (< score gnus-summary-mark-below))
8078                 (setq gnus-newsgroup-unreads
8079                       (delq number gnus-newsgroup-unreads))
8080                 (if gnus-newsgroup-auto-expire
8081                     (push number gnus-newsgroup-expirable)
8082                   (push (cons number gnus-low-score-mark)
8083                         gnus-newsgroup-reads)))
8084               t)
8085             ;; Do the `display' group parameter.
8086             (and gnus-newsgroup-display
8087                  (not (funcall gnus-newsgroup-display)))
8088             ;; Check NoCeM things.
8089             (when (and gnus-use-nocem
8090                        (gnus-nocem-unwanted-article-p
8091                         (mail-header-id (car thread))))
8092               (setq gnus-newsgroup-unreads
8093                     (delq number gnus-newsgroup-unreads))
8094               t)))
8095           ;; Nope, invisible article.
8096           0
8097         ;; Ok, this article is to be visible, so we add it to the limit
8098         ;; and return 1.
8099         (push number gnus-newsgroup-limit)
8100         1))))
8101
8102 (defun gnus-expunge-thread (thread)
8103   "Mark all articles in THREAD as read."
8104   (let* ((number (mail-header-number (car thread))))
8105     (incf gnus-newsgroup-expunged-tally)
8106     ;; We also mark as read here, if that's wanted.
8107     (setq gnus-newsgroup-unreads
8108           (delq number gnus-newsgroup-unreads))
8109     (if gnus-newsgroup-auto-expire
8110         (push number gnus-newsgroup-expirable)
8111       (push (cons number gnus-low-score-mark)
8112             gnus-newsgroup-reads)))
8113   ;; Go recursively through all subthreads.
8114   (mapcar 'gnus-expunge-thread (cdr thread)))
8115
8116 ;; Summary article oriented commands
8117
8118 (defun gnus-summary-refer-parent-article (n)
8119   "Refer parent article N times.
8120 If N is negative, go to ancestor -N instead.
8121 The difference between N and the number of articles fetched is returned."
8122   (interactive "p")
8123   (let ((skip 1)
8124         error header ref)
8125     (when (not (natnump n))
8126       (setq skip (abs n)
8127             n 1))
8128     (while (and (> n 0)
8129                 (not error))
8130       (setq header (gnus-summary-article-header))
8131       (if (and (eq (mail-header-number header)
8132                    (cdr gnus-article-current))
8133                (equal gnus-newsgroup-name
8134                       (car gnus-article-current)))
8135           ;; If we try to find the parent of the currently
8136           ;; displayed article, then we take a look at the actual
8137           ;; References header, since this is slightly more
8138           ;; reliable than the References field we got from the
8139           ;; server.
8140           (save-excursion
8141             (set-buffer gnus-original-article-buffer)
8142             (nnheader-narrow-to-headers)
8143             (unless (setq ref (message-fetch-field "references"))
8144               (when (setq ref (message-fetch-field "in-reply-to"))
8145                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8146             (widen))
8147         (setq ref
8148               ;; It's not the current article, so we take a bet on
8149               ;; the value we got from the server.
8150               (mail-header-references header)))
8151       (if (and ref
8152                (not (equal ref "")))
8153           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8154             (gnus-message 1 "Couldn't find parent"))
8155         (gnus-message 1 "No references in article %d"
8156                       (gnus-summary-article-number))
8157         (setq error t))
8158       (decf n))
8159     (gnus-summary-position-point)
8160     n))
8161
8162 (defun gnus-summary-refer-references ()
8163   "Fetch all articles mentioned in the References header.
8164 Return the number of articles fetched."
8165   (interactive)
8166   (let ((ref (mail-header-references (gnus-summary-article-header)))
8167         (current (gnus-summary-article-number))
8168         (n 0))
8169     (if (or (not ref)
8170             (equal ref ""))
8171         (error "No References in the current article")
8172       ;; For each Message-ID in the References header...
8173       (while (string-match "<[^>]*>" ref)
8174         (incf n)
8175         ;; ... fetch that article.
8176         (gnus-summary-refer-article
8177          (prog1 (match-string 0 ref)
8178            (setq ref (substring ref (match-end 0))))))
8179       (gnus-summary-goto-subject current)
8180       (gnus-summary-position-point)
8181       n)))
8182
8183 (defun gnus-summary-refer-thread (&optional limit)
8184   "Fetch all articles in the current thread.
8185 If LIMIT (the numerical prefix), fetch that many old headers instead
8186 of what's specified by the `gnus-refer-thread-limit' variable."
8187   (interactive "P")
8188   (let ((id (mail-header-id (gnus-summary-article-header)))
8189         (limit (if limit (prefix-numeric-value limit)
8190                  gnus-refer-thread-limit)))
8191     (unless (eq gnus-fetch-old-headers 'invisible)
8192       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8193       ;; Retrieve the headers and read them in.
8194       (if (eq (if (numberp limit)
8195                   (gnus-retrieve-headers
8196                    (list (min
8197                           (+ (mail-header-number
8198                               (gnus-summary-article-header))
8199                              limit)
8200                           gnus-newsgroup-end))
8201                    gnus-newsgroup-name (* limit 2))
8202                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8203                 ;; headers.
8204                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8205                                        gnus-newsgroup-name limit))
8206               'nov)
8207           (gnus-build-all-threads)
8208         (error "Can't fetch thread from back ends that don't support NOV"))
8209       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8210     (gnus-summary-limit-include-thread id)))
8211
8212 (defun gnus-summary-refer-article (message-id)
8213   "Fetch an article specified by MESSAGE-ID."
8214   (interactive "sMessage-ID: ")
8215   (when (and (stringp message-id)
8216              (not (zerop (length message-id))))
8217     (setq message-id (gnus-replace-in-string message-id " " ""))
8218     ;; Construct the correct Message-ID if necessary.
8219     ;; Suggested by tale@pawl.rpi.edu.
8220     (unless (string-match "^<" message-id)
8221       (setq message-id (concat "<" message-id)))
8222     (unless (string-match ">$" message-id)
8223       (setq message-id (concat message-id ">")))
8224     ;; People often post MIDs from URLs, so unhex it:
8225     (unless (string-match "@" message-id)
8226       (setq message-id (gnus-url-unhex-string message-id)))
8227     (let* ((header (gnus-id-to-header message-id))
8228            (sparse (and header
8229                         (gnus-summary-article-sparse-p
8230                          (mail-header-number header))
8231                         (memq (mail-header-number header)
8232                               gnus-newsgroup-limit)))
8233            number)
8234       (cond
8235        ;; If the article is present in the buffer we just go to it.
8236        ((and header
8237              (or (not (gnus-summary-article-sparse-p
8238                        (mail-header-number header)))
8239                  sparse))
8240         (prog1
8241             (gnus-summary-goto-article
8242              (mail-header-number header) nil t)
8243           (when sparse
8244             (gnus-summary-update-article (mail-header-number header)))))
8245        (t
8246         ;; We fetch the article.
8247         (catch 'found
8248           (dolist (gnus-override-method (gnus-refer-article-methods))
8249             (when (and (gnus-check-server gnus-override-method)
8250                        ;; Fetch the header,
8251                        (setq number (gnus-summary-insert-subject message-id)))
8252               ;; and display the article.
8253               (gnus-summary-select-article nil nil nil number)
8254               (throw 'found t)))
8255           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8256
8257 (defun gnus-refer-article-methods ()
8258   "Return a list of referable methods."
8259   (cond
8260    ;; No method, so we default to current and native.
8261    ((null gnus-refer-article-method)
8262     (list gnus-current-select-method gnus-select-method))
8263    ;; Current.
8264    ((eq 'current gnus-refer-article-method)
8265     (list gnus-current-select-method))
8266    ;; List of select methods.
8267    ((not (and (symbolp (car gnus-refer-article-method))
8268               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8269     (let (out)
8270       (dolist (method gnus-refer-article-method)
8271         (push (if (eq 'current method)
8272                   gnus-current-select-method
8273                 method)
8274               out))
8275       (nreverse out)))
8276    ;; One single select method.
8277    (t
8278     (list gnus-refer-article-method))))
8279
8280 (defun gnus-summary-edit-parameters ()
8281   "Edit the group parameters of the current group."
8282   (interactive)
8283   (gnus-group-edit-group gnus-newsgroup-name 'params))
8284
8285 (defun gnus-summary-customize-parameters ()
8286   "Customize the group parameters of the current group."
8287   (interactive)
8288   (gnus-group-customize gnus-newsgroup-name))
8289
8290 (defun gnus-summary-enter-digest-group (&optional force)
8291   "Enter an nndoc group based on the current article.
8292 If FORCE, force a digest interpretation.  If not, try
8293 to guess what the document format is."
8294   (interactive "P")
8295   (let ((conf gnus-current-window-configuration))
8296     (save-window-excursion
8297       (save-excursion
8298         (let (gnus-article-prepare-hook
8299               gnus-display-mime-function
8300               gnus-break-pages)
8301           (gnus-summary-select-article))))
8302     (setq gnus-current-window-configuration conf)
8303     (let* ((name (format "%s-%d"
8304                          (gnus-group-prefixed-name
8305                           gnus-newsgroup-name (list 'nndoc ""))
8306                          (with-current-buffer gnus-summary-buffer
8307                            gnus-current-article)))
8308            (ogroup gnus-newsgroup-name)
8309            (params (append (gnus-info-params (gnus-get-info ogroup))
8310                            (list (cons 'to-group ogroup))
8311                            (list (cons 'parent-group ogroup))
8312                            (list (cons 'save-article-group ogroup))))
8313            (case-fold-search t)
8314            (buf (current-buffer))
8315            dig to-address)
8316       (save-excursion
8317         (set-buffer gnus-original-article-buffer)
8318         ;; Have the digest group inherit the main mail address of
8319         ;; the parent article.
8320         (when (setq to-address (or (gnus-fetch-field "reply-to")
8321                                    (gnus-fetch-field "from")))
8322           (setq params (append
8323                         (list (cons 'to-address
8324                                     (funcall gnus-decode-encoded-word-function
8325                                              to-address))))))
8326         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8327         (insert-buffer-substring gnus-original-article-buffer)
8328         ;; Remove lines that may lead nndoc to misinterpret the
8329         ;; document type.
8330         (narrow-to-region
8331          (goto-char (point-min))
8332          (or (search-forward "\n\n" nil t) (point)))
8333         (goto-char (point-min))
8334         (delete-matching-lines "^Path:\\|^From ")
8335         (widen))
8336       (unwind-protect
8337           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8338                     (gnus-newsgroup-ephemeral-ignored-charsets
8339                      gnus-newsgroup-ignored-charsets))
8340                 (gnus-group-read-ephemeral-group
8341                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8342                               (nndoc-article-type
8343                                ,(if force 'mbox 'guess)))
8344                  t nil nil nil
8345                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8346                                                         "ADAPT")))))
8347               ;; Make all postings to this group go to the parent group.
8348               (nconc (gnus-info-params (gnus-get-info name))
8349                      params)
8350             ;; Couldn't select this doc group.
8351             (switch-to-buffer buf)
8352             (gnus-set-global-variables)
8353             (gnus-configure-windows 'summary)
8354             (gnus-message 3 "Article couldn't be entered?"))
8355         (kill-buffer dig)))))
8356
8357 (defun gnus-summary-read-document (n)
8358   "Open a new group based on the current article(s).
8359 This will allow you to read digests and other similar
8360 documents as newsgroups.
8361 Obeys the standard process/prefix convention."
8362   (interactive "P")
8363   (let* ((articles (gnus-summary-work-articles n))
8364          (ogroup gnus-newsgroup-name)
8365          (params (append (gnus-info-params (gnus-get-info ogroup))
8366                          (list (cons 'to-group ogroup))))
8367          article group egroup groups vgroup)
8368     (while (setq article (pop articles))
8369       (setq group (format "%s-%d" gnus-newsgroup-name article))
8370       (gnus-summary-remove-process-mark article)
8371       (when (gnus-summary-display-article article)
8372         (save-excursion
8373           (with-temp-buffer
8374             (insert-buffer-substring gnus-original-article-buffer)
8375             ;; Remove some headers that may lead nndoc to make
8376             ;; the wrong guess.
8377             (message-narrow-to-head)
8378             (goto-char (point-min))
8379             (delete-matching-lines "^Path:\\|^From ")
8380             (widen)
8381             (if (setq egroup
8382                       (gnus-group-read-ephemeral-group
8383                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8384                                      (nndoc-article-type guess))
8385                        t nil t))
8386                 (progn
8387             ;; Make all postings to this group go to the parent group.
8388                   (nconc (gnus-info-params (gnus-get-info egroup))
8389                          params)
8390                   (push egroup groups))
8391               ;; Couldn't select this doc group.
8392               (gnus-error 3 "Article couldn't be entered"))))))
8393     ;; Now we have selected all the documents.
8394     (cond
8395      ((not groups)
8396       (error "None of the articles could be interpreted as documents"))
8397      ((gnus-group-read-ephemeral-group
8398        (setq vgroup (format
8399                      "nnvirtual:%s-%s" gnus-newsgroup-name
8400                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8401        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8402        t
8403        (cons (current-buffer) 'summary)))
8404      (t
8405       (error "Couldn't select virtual nndoc group")))))
8406
8407 (defun gnus-summary-isearch-article (&optional regexp-p)
8408   "Do incremental search forward on the current article.
8409 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8410   (interactive "P")
8411   (gnus-summary-select-article)
8412   (gnus-configure-windows 'article)
8413   (gnus-eval-in-buffer-window gnus-article-buffer
8414     (save-restriction
8415       (widen)
8416       (isearch-forward regexp-p))))
8417
8418 (defun gnus-summary-search-article-forward (regexp &optional backward)
8419   "Search for an article containing REGEXP forward.
8420 If BACKWARD, search backward instead."
8421   (interactive
8422    (list (read-string
8423           (format "Search article %s (regexp%s): "
8424                   (if current-prefix-arg "backward" "forward")
8425                   (if gnus-last-search-regexp
8426                       (concat ", default " gnus-last-search-regexp)
8427                     "")))
8428          current-prefix-arg))
8429   (if (string-equal regexp "")
8430       (setq regexp (or gnus-last-search-regexp ""))
8431     (setq gnus-last-search-regexp regexp)
8432     (setq gnus-article-before-search gnus-current-article))
8433   ;; Intentionally set gnus-last-article.
8434   (setq gnus-last-article gnus-article-before-search)
8435   (let ((gnus-last-article gnus-last-article))
8436     (if (gnus-summary-search-article regexp backward)
8437         (gnus-summary-show-thread)
8438       (error "Search failed: \"%s\"" regexp))))
8439
8440 (defun gnus-summary-search-article-backward (regexp)
8441   "Search for an article containing REGEXP backward."
8442   (interactive
8443    (list (read-string
8444           (format "Search article backward (regexp%s): "
8445                   (if gnus-last-search-regexp
8446                       (concat ", default " gnus-last-search-regexp)
8447                     "")))))
8448   (gnus-summary-search-article-forward regexp 'backward))
8449
8450 (defun gnus-summary-search-article (regexp &optional backward)
8451   "Search for an article containing REGEXP.
8452 Optional argument BACKWARD means do search for backward.
8453 `gnus-select-article-hook' is not called during the search."
8454   ;; We have to require this here to make sure that the following
8455   ;; dynamic binding isn't shadowed by autoloading.
8456   (require 'gnus-async)
8457   (require 'gnus-art)
8458   (let ((gnus-select-article-hook nil)  ;Disable hook.
8459         (gnus-article-prepare-hook nil)
8460         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8461         (gnus-use-article-prefetch nil)
8462         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8463         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8464         (gnus-visual nil)
8465         (gnus-keep-backlog nil)
8466         (gnus-break-pages nil)
8467         (gnus-summary-display-arrow nil)
8468         (gnus-updated-mode-lines nil)
8469         (gnus-auto-center-summary nil)
8470         (sum (current-buffer))
8471         (gnus-display-mime-function nil)
8472         (found nil)
8473         point)
8474     (gnus-save-hidden-threads
8475       (gnus-summary-select-article)
8476       (set-buffer gnus-article-buffer)
8477       (goto-char (window-point (get-buffer-window (current-buffer))))
8478       (when backward
8479         (forward-line -1))
8480       (while (not found)
8481         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8482         (if (if backward
8483                 (re-search-backward regexp nil t)
8484               (re-search-forward regexp nil t))
8485             ;; We found the regexp.
8486             (progn
8487               (setq found 'found)
8488               (beginning-of-line)
8489               (set-window-start
8490                (get-buffer-window (current-buffer))
8491                (point))
8492               (forward-line 1)
8493               (set-window-point
8494                (get-buffer-window (current-buffer))
8495                (point))
8496               (set-buffer sum)
8497               (setq point (point)))
8498           ;; We didn't find it, so we go to the next article.
8499           (set-buffer sum)
8500           (setq found 'not)
8501           (while (eq found 'not)
8502             (if (not (if backward (gnus-summary-find-prev)
8503                        (gnus-summary-find-next)))
8504                 ;; No more articles.
8505                 (setq found t)
8506               ;; Select the next article and adjust point.
8507               (unless (gnus-summary-article-sparse-p
8508                        (gnus-summary-article-number))
8509                 (setq found nil)
8510                 (gnus-summary-select-article)
8511                 (set-buffer gnus-article-buffer)
8512                 (widen)
8513                 (goto-char (if backward (point-max) (point-min))))))))
8514       (gnus-message 7 ""))
8515     ;; Return whether we found the regexp.
8516     (when (eq found 'found)
8517       (goto-char point)
8518       (gnus-summary-show-thread)
8519       (gnus-summary-goto-subject gnus-current-article)
8520       (gnus-summary-position-point)
8521       t)))
8522
8523 (defun gnus-find-matching-articles (header regexp)
8524   "Return a list of all articles that match REGEXP on HEADER.
8525 This search includes all articles in the current group that Gnus has
8526 fetched headers for, whether they are displayed or not."
8527   (let ((articles nil)
8528         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8529         (case-fold-search t))
8530     (dolist (header gnus-newsgroup-headers)
8531       (when (string-match regexp (funcall func header))
8532         (push (mail-header-number header) articles)))
8533     (nreverse articles)))
8534
8535 (defun gnus-summary-find-matching (header regexp &optional backward unread
8536                                           not-case-fold not-matching)
8537   "Return a list of all articles that match REGEXP on HEADER.
8538 The search stars on the current article and goes forwards unless
8539 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8540 If UNREAD is non-nil, only unread articles will
8541 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8542 in the comparisons. If NOT-MATCHING, return a list of all articles that
8543 not match REGEXP on HEADER."
8544   (let ((case-fold-search (not not-case-fold))
8545         articles d func)
8546     (if (consp header)
8547         (if (eq (car header) 'extra)
8548             (setq func
8549                   `(lambda (h)
8550                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8551                          "")))
8552           (error "%s is an invalid header" header))
8553       (unless (fboundp (intern (concat "mail-header-" header)))
8554         (error "%s is not a valid header" header))
8555       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8556     (dolist (d (if (eq backward 'all)
8557                    gnus-newsgroup-data
8558                  (gnus-data-find-list
8559                   (gnus-summary-article-number)
8560                   (gnus-data-list backward))))
8561       (when (and (or (not unread)       ; We want all articles...
8562                      (gnus-data-unread-p d)) ; Or just unreads.
8563                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8564                  (if not-matching
8565                      (not (string-match
8566                            regexp
8567                            (funcall func (gnus-data-header d))))
8568                    (string-match regexp
8569                                  (funcall func (gnus-data-header d)))))
8570         (push (gnus-data-number d) articles))) ; Success!
8571     (nreverse articles)))
8572
8573 (defun gnus-summary-execute-command (header regexp command &optional backward)
8574   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8575 If HEADER is an empty string (or nil), the match is done on the entire
8576 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8577   (interactive
8578    (list (let ((completion-ignore-case t))
8579            (completing-read
8580             "Header name: "
8581             (mapcar (lambda (header) (list (format "%s" header)))
8582                     (append
8583                      '("Number" "Subject" "From" "Lines" "Date"
8584                        "Message-ID" "Xref" "References" "Body")
8585                      gnus-extra-headers))
8586             nil 'require-match))
8587          (read-string "Regexp: ")
8588          (read-key-sequence "Command: ")
8589          current-prefix-arg))
8590   (when (equal header "Body")
8591     (setq header ""))
8592   ;; Hidden thread subtrees must be searched as well.
8593   (gnus-summary-show-all-threads)
8594   ;; We don't want to change current point nor window configuration.
8595   (save-excursion
8596     (save-window-excursion
8597       (let (gnus-visual
8598             gnus-treat-strip-trailing-blank-lines
8599             gnus-treat-strip-leading-blank-lines
8600             gnus-treat-strip-multiple-blank-lines
8601             gnus-treat-hide-boring-headers
8602             gnus-treat-fold-newsgroups
8603             gnus-article-prepare-hook)
8604         (gnus-message 6 "Executing %s..." (key-description command))
8605         ;; We'd like to execute COMMAND interactively so as to give arguments.
8606         (gnus-execute header regexp
8607                       `(call-interactively ',(key-binding command))
8608                       backward)
8609         (gnus-message 6 "Executing %s...done" (key-description command))))))
8610
8611 (defun gnus-summary-beginning-of-article ()
8612   "Scroll the article back to the beginning."
8613   (interactive)
8614   (gnus-summary-select-article)
8615   (gnus-configure-windows 'article)
8616   (gnus-eval-in-buffer-window gnus-article-buffer
8617     (widen)
8618     (goto-char (point-min))
8619     (when gnus-break-pages
8620       (gnus-narrow-to-page))))
8621
8622 (defun gnus-summary-end-of-article ()
8623   "Scroll to the end of the article."
8624   (interactive)
8625   (gnus-summary-select-article)
8626   (gnus-configure-windows 'article)
8627   (gnus-eval-in-buffer-window gnus-article-buffer
8628     (widen)
8629     (goto-char (point-max))
8630     (recenter -3)
8631     (when gnus-break-pages
8632       (when (re-search-backward page-delimiter nil t)
8633         (narrow-to-region (match-end 0) (point-max)))
8634       (gnus-narrow-to-page))))
8635
8636 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8637   "Truncate to LEN and quote all \"(\"'s in STRING."
8638   (gnus-replace-in-string (if (and len (> (length string) len))
8639                               (substring string 0 len)
8640                             string)
8641                           "[()]" "\\\\\\&"))
8642
8643 (defun gnus-summary-print-article (&optional filename n)
8644   "Generate and print a PostScript image of the process-marked (mail) articles.
8645
8646 If used interactively, print the current article if none are
8647 process-marked.  With prefix arg, prompt the user for the name of the
8648 file to save in.
8649
8650 When used from Lisp, accept two optional args FILENAME and N.  N means
8651 to print the next N articles.  If N is negative, print the N previous
8652 articles.  If N is nil and articles have been marked with the process
8653 mark, print these instead.
8654
8655 If the optional first argument FILENAME is nil, send the image to the
8656 printer.  If FILENAME is a string, save the PostScript image in a file with
8657 that name.  If FILENAME is a number, prompt the user for the name of the file
8658 to save in."
8659   (interactive (list (ps-print-preprint current-prefix-arg)))
8660   (dolist (article (gnus-summary-work-articles n))
8661     (gnus-summary-select-article nil nil 'pseudo article)
8662     (gnus-eval-in-buffer-window gnus-article-buffer
8663       (gnus-print-buffer))
8664     (gnus-summary-remove-process-mark article))
8665   (ps-despool filename))
8666
8667 (defun gnus-print-buffer ()
8668   (let ((buffer (generate-new-buffer " *print*")))
8669     (unwind-protect
8670         (progn
8671           (copy-to-buffer buffer (point-min) (point-max))
8672           (set-buffer buffer)
8673           (gnus-remove-text-with-property 'gnus-decoration)
8674           (when (gnus-visual-p 'article-highlight 'highlight)
8675             ;; Copy-to-buffer doesn't copy overlay.  So redo
8676             ;; highlight.
8677             (let ((gnus-article-buffer buffer))
8678               (gnus-article-highlight-citation t)
8679               (gnus-article-highlight-signature)
8680               (gnus-article-emphasize)
8681               (gnus-article-delete-invisible-text)))
8682           (let ((ps-left-header
8683                  (list
8684                   (concat "("
8685                           (gnus-summary-print-truncate-and-quote
8686                            (mail-header-subject gnus-current-headers)
8687                            66) ")")
8688                   (concat "("
8689                           (gnus-summary-print-truncate-and-quote
8690                            (mail-header-from gnus-current-headers)
8691                            45) ")")))
8692                 (ps-right-header
8693                  (list
8694                   "/pagenumberstring load"
8695                   (concat "("
8696                           (mail-header-date gnus-current-headers) ")"))))
8697             (gnus-run-hooks 'gnus-ps-print-hook)
8698             (save-excursion
8699               (if window-system
8700                   (ps-spool-buffer-with-faces)
8701                 (ps-spool-buffer)))))
8702       (kill-buffer buffer))))
8703
8704 (defun gnus-summary-show-article (&optional arg)
8705   "Force redisplaying of the current article.
8706 If ARG (the prefix) is a number, show the article with the charset
8707 defined in `gnus-summary-show-article-charset-alist', or the charset
8708 input.
8709 If ARG (the prefix) is non-nil and not a number, show the raw article
8710 without any article massaging functions being run.  Normally, the key
8711 strokes are `C-u g'."
8712   (interactive "P")
8713   (cond
8714    ((numberp arg)
8715     (gnus-summary-show-article t)
8716     (let ((gnus-newsgroup-charset
8717            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8718                (mm-read-coding-system
8719                 "View as charset: " ;; actually it is coding system.
8720                 (save-excursion
8721                   (set-buffer gnus-article-buffer)
8722                   (mm-detect-coding-region (point) (point-max))))))
8723           (gnus-newsgroup-ignored-charsets 'gnus-all))
8724       (gnus-summary-select-article nil 'force)
8725       (let ((deps gnus-newsgroup-dependencies)
8726             head header lines)
8727         (save-excursion
8728           (set-buffer gnus-original-article-buffer)
8729           (save-restriction
8730             (message-narrow-to-head)
8731             (setq head (buffer-string))
8732             (goto-char (point-min))
8733             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8734               (goto-char (point-max))
8735               (widen)
8736               (setq lines (1- (count-lines (point) (point-max))))))
8737           (with-temp-buffer
8738             (insert (format "211 %d Article retrieved.\n"
8739                             (cdr gnus-article-current)))
8740             (insert head)
8741             (if lines (insert (format "Lines: %d\n" lines)))
8742             (insert ".\n")
8743             (let ((nntp-server-buffer (current-buffer)))
8744               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8745         (gnus-data-set-header
8746          (gnus-data-find (cdr gnus-article-current))
8747          header)
8748         (gnus-summary-update-article-line
8749          (cdr gnus-article-current) header)
8750         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8751           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8752    ((not arg)
8753     ;; Select the article the normal way.
8754     (gnus-summary-select-article nil 'force))
8755    (t
8756     ;; We have to require this here to make sure that the following
8757     ;; dynamic binding isn't shadowed by autoloading.
8758     (require 'gnus-async)
8759     (require 'gnus-art)
8760     ;; Bind the article treatment functions to nil.
8761     (let ((gnus-have-all-headers t)
8762           gnus-article-prepare-hook
8763           gnus-article-decode-hook
8764           gnus-display-mime-function
8765           gnus-break-pages)
8766       ;; Destroy any MIME parts.
8767       (when (gnus-buffer-live-p gnus-article-buffer)
8768         (save-excursion
8769           (set-buffer gnus-article-buffer)
8770           (mm-destroy-parts gnus-article-mime-handles)
8771           ;; Set it to nil for safety reason.
8772           (setq gnus-article-mime-handle-alist nil)
8773           (setq gnus-article-mime-handles nil)))
8774       (gnus-summary-select-article nil 'force))))
8775   (gnus-summary-goto-subject gnus-current-article)
8776   (gnus-summary-position-point))
8777
8778 (defun gnus-summary-show-raw-article ()
8779   "Show the raw article without any article massaging functions being run."
8780   (interactive)
8781   (gnus-summary-show-article t))
8782
8783 (defun gnus-summary-verbose-headers (&optional arg)
8784   "Toggle permanent full header display.
8785 If ARG is a positive number, turn header display on.
8786 If ARG is a negative number, turn header display off."
8787   (interactive "P")
8788   (setq gnus-show-all-headers
8789         (cond ((or (not (numberp arg))
8790                    (zerop arg))
8791                (not gnus-show-all-headers))
8792               ((natnump arg)
8793                t)))
8794   (gnus-summary-show-article))
8795
8796 (defun gnus-summary-toggle-header (&optional arg)
8797   "Show the headers if they are hidden, or hide them if they are shown.
8798 If ARG is a positive number, show the entire header.
8799 If ARG is a negative number, hide the unwanted header lines."
8800   (interactive "P")
8801   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8802                      (get-buffer-window gnus-article-buffer t))))
8803     (with-current-buffer gnus-article-buffer
8804       (widen)
8805       (article-narrow-to-head)
8806       (let* ((buffer-read-only nil)
8807              (inhibit-point-motion-hooks t)
8808              (hidden (if (numberp arg)
8809                          (>= arg 0)
8810                        (gnus-article-hidden-text-p 'headers)))
8811              s e)
8812         (delete-region (point-min) (point-max))
8813         (with-current-buffer gnus-original-article-buffer
8814           (goto-char (setq s (point-min)))
8815           (setq e (if (search-forward "\n\n" nil t)
8816                       (1- (point))
8817                     (point-max))))
8818         (insert-buffer-substring gnus-original-article-buffer s e)
8819         (run-hooks 'gnus-article-decode-hook)
8820         (if hidden
8821             (let ((gnus-treat-hide-headers nil)
8822                   (gnus-treat-hide-boring-headers nil))
8823               (gnus-delete-wash-type 'headers)
8824               (gnus-treat-article 'head))
8825           (gnus-treat-article 'head))
8826         (widen)
8827         (if window
8828             (set-window-start window (goto-char (point-min))))
8829         (if gnus-break-pages
8830             (gnus-narrow-to-page)
8831           (when (gnus-visual-p 'page-marker)
8832             (let ((buffer-read-only nil))
8833               (gnus-remove-text-with-property 'gnus-prev)
8834               (gnus-remove-text-with-property 'gnus-next))))
8835         (gnus-set-mode-line 'article)))))
8836
8837 (defun gnus-summary-show-all-headers ()
8838   "Make all header lines visible."
8839   (interactive)
8840   (gnus-summary-toggle-header 1))
8841
8842 (defun gnus-summary-caesar-message (&optional arg)
8843   "Caesar rotate the current article by 13.
8844 The numerical prefix specifies how many places to rotate each letter
8845 forward."
8846   (interactive "P")
8847   (gnus-summary-select-article)
8848   (let ((mail-header-separator ""))
8849     (gnus-eval-in-buffer-window gnus-article-buffer
8850       (save-restriction
8851         (widen)
8852         (let ((start (window-start))
8853               buffer-read-only)
8854           (message-caesar-buffer-body arg)
8855           (set-window-start (get-buffer-window (current-buffer)) start))))))
8856
8857 (autoload 'unmorse-region "morse"
8858   "Convert morse coded text in region to ordinary ASCII text."
8859   t)
8860
8861 (defun gnus-summary-morse-message (&optional arg)
8862   "Morse decode the current article."
8863   (interactive "P")
8864   (gnus-summary-select-article)
8865   (let ((mail-header-separator ""))
8866     (gnus-eval-in-buffer-window gnus-article-buffer
8867       (save-excursion
8868         (save-restriction
8869           (widen)
8870           (let ((pos (window-start))
8871                 buffer-read-only)
8872             (goto-char (point-min))
8873             (when (message-goto-body)
8874               (gnus-narrow-to-body))
8875             (goto-char (point-min))
8876             (while (re-search-forward "·" (point-max) t)
8877               (replace-match "."))
8878             (unmorse-region (point-min) (point-max))
8879             (widen)
8880             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8881
8882 (defun gnus-summary-stop-page-breaking ()
8883   "Stop page breaking in the current article."
8884   (interactive)
8885   (gnus-summary-select-article)
8886   (gnus-eval-in-buffer-window gnus-article-buffer
8887     (widen)
8888     (when (gnus-visual-p 'page-marker)
8889       (let ((buffer-read-only nil))
8890         (gnus-remove-text-with-property 'gnus-prev)
8891         (gnus-remove-text-with-property 'gnus-next))
8892       (setq gnus-page-broken nil))))
8893
8894 (defun gnus-summary-move-article (&optional n to-newsgroup
8895                                             select-method action)
8896   "Move the current article to a different newsgroup.
8897 If N is a positive number, move the N next articles.
8898 If N is a negative number, move the N previous articles.
8899 If N is nil and any articles have been marked with the process mark,
8900 move those articles instead.
8901 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8902 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8903 re-spool using this method.
8904
8905 When called interactively with TO-NEWSGROUP being nil, the value of
8906 the variable `gnus-move-split-methods' is used for finding a default
8907 for the target newsgroup.
8908
8909 For this function to work, both the current newsgroup and the
8910 newsgroup that you want to move to have to support the `request-move'
8911 and `request-accept' functions.
8912
8913 ACTION can be either `move' (the default), `crosspost' or `copy'."
8914   (interactive "P")
8915   (unless action
8916     (setq action 'move))
8917   ;; Check whether the source group supports the required functions.
8918   (cond ((and (eq action 'move)
8919               (not (gnus-check-backend-function
8920                     'request-move-article gnus-newsgroup-name)))
8921          (error "The current group does not support article moving"))
8922         ((and (eq action 'crosspost)
8923               (not (gnus-check-backend-function
8924                     'request-replace-article gnus-newsgroup-name)))
8925          (error "The current group does not support article editing")))
8926   (let ((articles (gnus-summary-work-articles n))
8927         (prefix (if (gnus-check-backend-function
8928                      'request-move-article gnus-newsgroup-name)
8929                     (funcall gnus-move-group-prefix-function
8930                              gnus-newsgroup-name)
8931                   ""))
8932         (names '((move "Move" "Moving")
8933                  (copy "Copy" "Copying")
8934                  (crosspost "Crosspost" "Crossposting")))
8935         (copy-buf (save-excursion
8936                     (nnheader-set-temp-buffer " *copy article*")))
8937         art-group to-method new-xref article to-groups)
8938     (unless (assq action names)
8939       (error "Unknown action %s" action))
8940     ;; Read the newsgroup name.
8941     (when (and (not to-newsgroup)
8942                (not select-method))
8943       (if (and gnus-move-split-methods
8944                (not
8945                 (and (memq gnus-current-article articles)
8946                      (gnus-buffer-live-p gnus-original-article-buffer))))
8947           ;; When `gnus-move-split-methods' is non-nil, we have to
8948           ;; select an article to give `gnus-read-move-group-name' an
8949           ;; opportunity to suggest an appropriate default.  However,
8950           ;; we needn't render or mark the article.
8951           (let ((gnus-display-mime-function nil)
8952                 (gnus-article-prepare-hook nil)
8953                 (gnus-mark-article-hook nil))
8954             (gnus-summary-select-article nil nil nil (car articles))))
8955       (setq to-newsgroup
8956             (gnus-read-move-group-name
8957              (cadr (assq action names))
8958              (symbol-value (intern (format "gnus-current-%s-group" action)))
8959              articles prefix))
8960       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8961     (setq to-method (or select-method
8962                         (gnus-server-to-method
8963                          (gnus-group-method to-newsgroup))))
8964     ;; Check the method we are to move this article to...
8965     (unless (gnus-check-backend-function
8966              'request-accept-article (car to-method))
8967       (error "%s does not support article copying" (car to-method)))
8968     (unless (gnus-check-server to-method)
8969       (error "Can't open server %s" (car to-method)))
8970     (gnus-message 6 "%s to %s: %s..."
8971                   (caddr (assq action names))
8972                   (or (car select-method) to-newsgroup) articles)
8973     (while articles
8974       (setq article (pop articles))
8975       (setq
8976        art-group
8977        (cond
8978         ;; Move the article.
8979         ((eq action 'move)
8980          ;; Remove this article from future suppression.
8981          (gnus-dup-unsuppress-article article)
8982          (gnus-request-move-article
8983           article                       ; Article to move
8984           gnus-newsgroup-name           ; From newsgroup
8985           (nth 1 (gnus-find-method-for-group
8986                   gnus-newsgroup-name)) ; Server
8987           (list 'gnus-request-accept-article
8988                 to-newsgroup (list 'quote select-method)
8989                 (not articles) t)       ; Accept form
8990           (not articles)))              ; Only save nov last time
8991         ;; Copy the article.
8992         ((eq action 'copy)
8993          (save-excursion
8994            (set-buffer copy-buf)
8995            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8996              (gnus-request-accept-article
8997               to-newsgroup select-method (not articles) t))))
8998         ;; Crosspost the article.
8999         ((eq action 'crosspost)
9000          (let ((xref (message-tokenize-header
9001                       (mail-header-xref (gnus-summary-article-header article))
9002                       " ")))
9003            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9004                                   ":" (number-to-string article)))
9005            (unless xref
9006              (setq xref (list (system-name))))
9007            (setq new-xref
9008                  (concat
9009                   (mapconcat 'identity
9010                              (delete "Xref:" (delete new-xref xref))
9011                              " ")
9012                   " " new-xref))
9013            (save-excursion
9014              (set-buffer copy-buf)
9015              ;; First put the article in the destination group.
9016              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9017              (when (consp (setq art-group
9018                                 (gnus-request-accept-article
9019                                  to-newsgroup select-method (not articles))))
9020                (setq new-xref (concat new-xref " " (car art-group)
9021                                       ":"
9022                                       (number-to-string (cdr art-group))))
9023                ;; Now we have the new Xrefs header, so we insert
9024                ;; it and replace the new article.
9025                (nnheader-replace-header "Xref" new-xref)
9026                (gnus-request-replace-article
9027                 (cdr art-group) to-newsgroup (current-buffer))
9028                art-group))))))
9029       (cond
9030        ((not art-group)
9031         (gnus-message 1 "Couldn't %s article %s: %s"
9032                       (cadr (assq action names)) article
9033                       (nnheader-get-report (car to-method))))
9034        ((eq art-group 'junk)
9035         (when (eq action 'move)
9036           (gnus-summary-mark-article article gnus-canceled-mark)
9037           (gnus-message 4 "Deleted article %s" article)
9038           ;; run the delete hook
9039           (run-hook-with-args 'gnus-summary-article-delete-hook
9040                               action
9041                               (gnus-data-header
9042                                (assoc article (gnus-data-list nil)))
9043                               gnus-newsgroup-name nil
9044                               select-method)))
9045        (t
9046         (let* ((pto-group (gnus-group-prefixed-name
9047                            (car art-group) to-method))
9048                (info (gnus-get-info pto-group))
9049                (to-group (gnus-info-group info))
9050                to-marks)
9051           ;; Update the group that has been moved to.
9052           (when (and info
9053                      (memq action '(move copy)))
9054             (unless (member to-group to-groups)
9055               (push to-group to-groups))
9056
9057             (unless (memq article gnus-newsgroup-unreads)
9058               (push 'read to-marks)
9059               (gnus-info-set-read
9060                info (gnus-add-to-range (gnus-info-read info)
9061                                        (list (cdr art-group)))))
9062
9063             ;; See whether the article is to be put in the cache.
9064             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9065                              gnus-article-mark-lists
9066                            (delete '(expirable . expire)
9067                                    (copy-sequence gnus-article-mark-lists))))
9068                   (to-article (cdr art-group)))
9069
9070               ;; Enter the article into the cache in the new group,
9071               ;; if that is required.
9072               (when gnus-use-cache
9073                 (gnus-cache-possibly-enter-article
9074                  to-group to-article
9075                  (memq article gnus-newsgroup-marked)
9076                  (memq article gnus-newsgroup-dormant)
9077                  (memq article gnus-newsgroup-unreads)))
9078
9079               (when gnus-preserve-marks
9080                 ;; Copy any marks over to the new group.
9081                 (when (and (equal to-group gnus-newsgroup-name)
9082                            (not (memq article gnus-newsgroup-unreads)))
9083                   ;; Mark this article as read in this group.
9084                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9085                   (setcdr (gnus-active to-group) to-article)
9086                   (setcdr gnus-newsgroup-active to-article))
9087
9088                 (while marks
9089                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9090                     (when (memq article (symbol-value
9091                                          (intern (format "gnus-newsgroup-%s"
9092                                                          (caar marks)))))
9093                       (push (cdar marks) to-marks)
9094                       ;; If the other group is the same as this group,
9095                       ;; then we have to add the mark to the list.
9096                       (when (equal to-group gnus-newsgroup-name)
9097                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9098                              (cons to-article
9099                                    (symbol-value
9100                                     (intern (format "gnus-newsgroup-%s"
9101                                                     (caar marks)))))))
9102                       ;; Copy the marks to other group.
9103                       (gnus-add-marked-articles
9104                        to-group (cdar marks) (list to-article) info)))
9105                   (setq marks (cdr marks)))
9106
9107                 (gnus-request-set-mark
9108                  to-group (list (list (list to-article) 'add to-marks))))
9109
9110               (gnus-dribble-enter
9111                (concat "(gnus-group-set-info '"
9112                        (gnus-prin1-to-string (gnus-get-info to-group))
9113                        ")"))))
9114
9115           ;; Update the Xref header in this article to point to
9116           ;; the new crossposted article we have just created.
9117           (when (eq action 'crosspost)
9118             (save-excursion
9119               (set-buffer copy-buf)
9120               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9121               (nnheader-replace-header "Xref" new-xref)
9122               (gnus-request-replace-article
9123                article gnus-newsgroup-name (current-buffer))))
9124
9125           ;; run the move/copy/crosspost/respool hook
9126           (run-hook-with-args 'gnus-summary-article-move-hook
9127                               action
9128                               (gnus-data-header
9129                                (assoc article (gnus-data-list nil)))
9130                               gnus-newsgroup-name
9131                               to-newsgroup
9132                               select-method))
9133
9134         ;;;!!!Why is this necessary?
9135         (set-buffer gnus-summary-buffer)
9136         
9137         (gnus-summary-goto-subject article)
9138         (when (eq action 'move)
9139           (gnus-summary-mark-article article gnus-canceled-mark))))
9140       (gnus-summary-remove-process-mark article))
9141     ;; Re-activate all groups that have been moved to.
9142     (save-excursion
9143       (set-buffer gnus-group-buffer)
9144       (let ((gnus-group-marked to-groups))
9145         (gnus-group-get-new-news-this-group nil t)))
9146
9147     (gnus-kill-buffer copy-buf)
9148     (gnus-summary-position-point)
9149     (gnus-set-mode-line 'summary)))
9150
9151 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9152   "Move the current article to a different newsgroup.
9153 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9154 When called interactively, if TO-NEWSGROUP is nil, use the value of
9155 the variable `gnus-move-split-methods' for finding a default target
9156 newsgroup.
9157 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9158 re-spool using this method."
9159   (interactive "P")
9160   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9161
9162 (defun gnus-summary-crosspost-article (&optional n)
9163   "Crosspost the current article to some other group."
9164   (interactive "P")
9165   (gnus-summary-move-article n nil nil 'crosspost))
9166
9167 (defcustom gnus-summary-respool-default-method nil
9168   "Default method type for respooling an article.
9169 If nil, use to the current newsgroup method."
9170   :type 'symbol
9171   :group 'gnus-summary-mail)
9172
9173 (defcustom gnus-summary-display-while-building nil
9174   "If non-nil, show and update the summary buffer as it's being built.
9175 If the value is t, update the buffer after every line is inserted.  If
9176 the value is an integer (N), update the display every N lines."
9177   :group 'gnus-thread
9178   :type '(choice (const :tag "off" nil)
9179                  number
9180                  (const :tag "frequently" t)))
9181
9182 (defun gnus-summary-respool-article (&optional n method)
9183   "Respool the current article.
9184 The article will be squeezed through the mail spooling process again,
9185 which means that it will be put in some mail newsgroup or other
9186 depending on `nnmail-split-methods'.
9187 If N is a positive number, respool the N next articles.
9188 If N is a negative number, respool the N previous articles.
9189 If N is nil and any articles have been marked with the process mark,
9190 respool those articles instead.
9191
9192 Respooling can be done both from mail groups and \"real\" newsgroups.
9193 In the former case, the articles in question will be moved from the
9194 current group into whatever groups they are destined to.  In the
9195 latter case, they will be copied into the relevant groups."
9196   (interactive
9197    (list current-prefix-arg
9198          (let* ((methods (gnus-methods-using 'respool))
9199                 (methname
9200                  (symbol-name (or gnus-summary-respool-default-method
9201                                   (car (gnus-find-method-for-group
9202                                         gnus-newsgroup-name)))))
9203                 (method
9204                  (gnus-completing-read-with-default
9205                   methname "What backend do you want to use when respooling?"
9206                   methods nil t nil 'gnus-mail-method-history))
9207                 ms)
9208            (cond
9209             ((zerop (length (setq ms (gnus-servers-using-backend
9210                                       (intern method)))))
9211              (list (intern method) ""))
9212             ((= 1 (length ms))
9213              (car ms))
9214             (t
9215              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9216                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9217                            ms-alist))))))))
9218   (unless method
9219     (error "No method given for respooling"))
9220   (if (assoc (symbol-name
9221               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9222              (gnus-methods-using 'respool))
9223       (gnus-summary-move-article n nil method)
9224     (gnus-summary-copy-article n nil method)))
9225
9226 (defun gnus-summary-import-article (file &optional edit)
9227   "Import an arbitrary file into a mail newsgroup."
9228   (interactive "fImport file: \nP")
9229   (let ((group gnus-newsgroup-name)
9230         (now (current-time))
9231         atts lines group-art)
9232     (unless (gnus-check-backend-function 'request-accept-article group)
9233       (error "%s does not support article importing" group))
9234     (or (file-readable-p file)
9235         (not (file-regular-p file))
9236         (error "Can't read %s" file))
9237     (save-excursion
9238       (set-buffer (gnus-get-buffer-create " *import file*"))
9239       (erase-buffer)
9240       (nnheader-insert-file-contents file)
9241       (goto-char (point-min))
9242       (if (nnheader-article-p)
9243           (save-restriction
9244             (goto-char (point-min))
9245             (search-forward "\n\n" nil t)
9246             (narrow-to-region (point-min) (1- (point)))
9247             (goto-char (point-min))
9248             (unless (re-search-forward "^date:" nil t)
9249               (goto-char (point-max))
9250               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9251        ;; This doesn't look like an article, so we fudge some headers.
9252         (setq atts (file-attributes file)
9253               lines (count-lines (point-min) (point-max)))
9254         (insert "From: " (read-string "From: ") "\n"
9255                 "Subject: " (read-string "Subject: ") "\n"
9256                 "Date: " (message-make-date (nth 5 atts)) "\n"
9257                 "Message-ID: " (message-make-message-id) "\n"
9258                 "Lines: " (int-to-string lines) "\n"
9259                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9260       (setq group-art (gnus-request-accept-article group nil t))
9261       (kill-buffer (current-buffer)))
9262     (setq gnus-newsgroup-active (gnus-activate-group group))
9263     (forward-line 1)
9264     (gnus-summary-goto-article (cdr group-art) nil t)
9265     (when edit
9266       (gnus-summary-edit-article))))
9267
9268 (defun gnus-summary-create-article ()
9269   "Create an article in a mail newsgroup."
9270   (interactive)
9271   (let ((group gnus-newsgroup-name)
9272         (now (current-time))
9273         group-art)
9274     (unless (gnus-check-backend-function 'request-accept-article group)
9275       (error "%s does not support article importing" group))
9276     (save-excursion
9277       (set-buffer (gnus-get-buffer-create " *import file*"))
9278       (erase-buffer)
9279       (goto-char (point-min))
9280       ;; This doesn't look like an article, so we fudge some headers.
9281       (insert "From: " (read-string "From: ") "\n"
9282               "Subject: " (read-string "Subject: ") "\n"
9283               "Date: " (message-make-date now) "\n"
9284               "Message-ID: " (message-make-message-id) "\n")
9285       (setq group-art (gnus-request-accept-article group nil t))
9286       (kill-buffer (current-buffer)))
9287     (setq gnus-newsgroup-active (gnus-activate-group group))
9288     (forward-line 1)
9289     (gnus-summary-goto-article (cdr group-art) nil t)
9290     (gnus-summary-edit-article)))
9291
9292 (defun gnus-summary-article-posted-p ()
9293   "Say whether the current (mail) article is available from news as well.
9294 This will be the case if the article has both been mailed and posted."
9295   (interactive)
9296   (let ((id (mail-header-references (gnus-summary-article-header)))
9297         (gnus-override-method (car (gnus-refer-article-methods))))
9298     (if (gnus-request-head id "")
9299         (gnus-message 2 "The current message was found on %s"
9300                       gnus-override-method)
9301       (gnus-message 2 "The current message couldn't be found on %s"
9302                     gnus-override-method)
9303       nil)))
9304
9305 (defun gnus-summary-expire-articles (&optional now)
9306   "Expire all articles that are marked as expirable in the current group."
9307   (interactive)
9308   (when (and (not gnus-group-is-exiting-without-update-p)
9309              (gnus-check-backend-function
9310               'request-expire-articles gnus-newsgroup-name))
9311     ;; This backend supports expiry.
9312     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9313            (expirable (if total
9314                           (progn
9315                             ;; We need to update the info for
9316                             ;; this group for `gnus-list-of-read-articles'
9317                             ;; to give us the right answer.
9318                             (gnus-run-hooks 'gnus-exit-group-hook)
9319                             (gnus-summary-update-info)
9320                             (gnus-list-of-read-articles gnus-newsgroup-name))
9321                         (setq gnus-newsgroup-expirable
9322                               (sort gnus-newsgroup-expirable '<))))
9323            (expiry-wait (if now 'immediate
9324                           (gnus-group-find-parameter
9325                            gnus-newsgroup-name 'expiry-wait)))
9326            (nnmail-expiry-target
9327             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9328                 nnmail-expiry-target))
9329            es)
9330       (when expirable
9331         ;; There are expirable articles in this group, so we run them
9332         ;; through the expiry process.
9333         (gnus-message 6 "Expiring articles...")
9334         (unless (gnus-check-group gnus-newsgroup-name)
9335           (error "Can't open server for %s" gnus-newsgroup-name))
9336         ;; The list of articles that weren't expired is returned.
9337         (save-excursion
9338           (if expiry-wait
9339               (let ((nnmail-expiry-wait-function nil)
9340                     (nnmail-expiry-wait expiry-wait))
9341                 (setq es (gnus-request-expire-articles
9342                           expirable gnus-newsgroup-name)))
9343             (setq es (gnus-request-expire-articles
9344                       expirable gnus-newsgroup-name)))
9345           (unless total
9346             (setq gnus-newsgroup-expirable es))
9347           ;; We go through the old list of expirable, and mark all
9348           ;; really expired articles as nonexistent.
9349           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9350             (let ((gnus-use-cache nil))
9351               (dolist (article expirable)
9352                 (when (and (not (memq article es))
9353                            (gnus-data-find article))
9354                   (gnus-summary-mark-article article gnus-canceled-mark)
9355                   (run-hook-with-args 'gnus-summary-article-expire-hook
9356                                       'delete
9357                                       (gnus-data-header
9358                                        (assoc article (gnus-data-list nil)))
9359                                       gnus-newsgroup-name
9360                                       nil
9361                                       nil))))))
9362         (gnus-message 6 "Expiring articles...done")))))
9363
9364 (defun gnus-summary-expire-articles-now ()
9365   "Expunge all expirable articles in the current group.
9366 This means that *all* articles that are marked as expirable will be
9367 deleted forever, right now."
9368   (interactive)
9369   (or gnus-expert-user
9370       (gnus-yes-or-no-p
9371        "Are you really, really, really sure you want to delete all these messages? ")
9372       (error "Phew!"))
9373   (gnus-summary-expire-articles t))
9374
9375 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9376 (defun gnus-summary-delete-article (&optional n)
9377   "Delete the N next (mail) articles.
9378 This command actually deletes articles.  This is not a marking
9379 command.  The article will disappear forever from your life, never to
9380 return.
9381
9382 If N is negative, delete backwards.
9383 If N is nil and articles have been marked with the process mark,
9384 delete these instead.
9385
9386 If `gnus-novice-user' is non-nil you will be asked for
9387 confirmation before the articles are deleted."
9388   (interactive "P")
9389   (unless (gnus-check-backend-function 'request-expire-articles
9390                                        gnus-newsgroup-name)
9391     (error "The current newsgroup does not support article deletion"))
9392   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9393     (error "Couldn't open server"))
9394   ;; Compute the list of articles to delete.
9395   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9396         (nnmail-expiry-target 'delete)
9397         not-deleted)
9398     (if (and gnus-novice-user
9399              (not (gnus-yes-or-no-p
9400                    (format "Do you really want to delete %s forever? "
9401                            (if (> (length articles) 1)
9402                                (format "these %s articles" (length articles))
9403                              "this article")))))
9404         ()
9405       ;; Delete the articles.
9406       (setq not-deleted (gnus-request-expire-articles
9407                          articles gnus-newsgroup-name 'force))
9408       (while articles
9409         (gnus-summary-remove-process-mark (car articles))
9410         ;; The backend might not have been able to delete the article
9411         ;; after all.
9412         (unless (memq (car articles) not-deleted)
9413           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9414         (let* ((article (car articles))
9415                (ghead  (gnus-data-header
9416                                     (assoc article (gnus-data-list nil)))))
9417           (run-hook-with-args 'gnus-summary-article-delete-hook
9418                               'delete ghead gnus-newsgroup-name nil
9419                               nil))
9420         (setq articles (cdr articles)))
9421       (when not-deleted
9422         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9423     (gnus-summary-position-point)
9424     (gnus-set-mode-line 'summary)
9425     not-deleted))
9426
9427 (defun gnus-summary-edit-article (&optional arg)
9428   "Edit the current article.
9429 This will have permanent effect only in mail groups.
9430 If ARG is nil, edit the decoded articles.
9431 If ARG is 1, edit the raw articles.
9432 If ARG is 2, edit the raw articles even in read-only groups.
9433 If ARG is 3, edit the articles with the current handles.
9434 Otherwise, allow editing of articles even in read-only
9435 groups."
9436   (interactive "P")
9437   (let (force raw current-handles)
9438     (cond
9439      ((null arg))
9440      ((eq arg 1)
9441       (setq raw t))
9442      ((eq arg 2)
9443       (setq raw t
9444             force t))
9445      ((eq arg 3)
9446       (setq current-handles
9447             (and (gnus-buffer-live-p gnus-article-buffer)
9448                  (with-current-buffer gnus-article-buffer
9449                    (prog1
9450                        gnus-article-mime-handles
9451                      (setq gnus-article-mime-handles nil))))))
9452      (t
9453       (setq force t)))
9454     (when (and raw (not force)
9455                (member gnus-newsgroup-name '("nndraft:delayed"
9456                                              "nndraft:drafts"
9457                                              "nndraft:queue")))
9458       (error "Can't edit the raw article in group %s"
9459              gnus-newsgroup-name))
9460     (save-excursion
9461       (set-buffer gnus-summary-buffer)
9462       (let ((mail-parse-charset gnus-newsgroup-charset)
9463             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9464         (gnus-set-global-variables)
9465         (when (and (not force)
9466                    (gnus-group-read-only-p))
9467           (error "The current newsgroup does not support article editing"))
9468         (gnus-summary-show-article t)
9469         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9470           (with-current-buffer gnus-article-buffer
9471             (mm-enable-multibyte)))
9472         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9473             (setq raw t))
9474         (gnus-article-edit-article
9475          (if raw 'ignore
9476            `(lambda ()
9477               (let ((mbl mml-buffer-list))
9478                 (setq mml-buffer-list nil)
9479                 (mime-to-mml ,'current-handles)
9480                 (let ((mbl1 mml-buffer-list))
9481                   (setq mml-buffer-list mbl)
9482                   (set (make-local-variable 'mml-buffer-list) mbl1))
9483                 (gnus-make-local-hook 'kill-buffer-hook)
9484                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9485          `(lambda (no-highlight)
9486             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9487                   (message-options message-options)
9488                   (message-options-set-recipient)
9489                   (mail-parse-ignored-charsets
9490                    ',gnus-newsgroup-ignored-charsets))
9491               ,(if (not raw) '(progn
9492                                 (mml-to-mime)
9493                                 (mml-destroy-buffers)
9494                                 (remove-hook 'kill-buffer-hook
9495                                              'mml-destroy-buffers t)
9496                                 (kill-local-variable 'mml-buffer-list)))
9497               (gnus-summary-edit-article-done
9498                ,(or (mail-header-references gnus-current-headers) "")
9499                ,(gnus-group-read-only-p)
9500                ,gnus-summary-buffer no-highlight))))))))
9501
9502 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9503
9504 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9505                                                  no-highlight)
9506   "Make edits to the current article permanent."
9507   (interactive)
9508   (save-excursion
9509    ;; The buffer restriction contains the entire article if it exists.
9510     (when (article-goto-body)
9511       (let ((lines (count-lines (point) (point-max)))
9512             (length (- (point-max) (point)))
9513             (case-fold-search t)
9514             (body (copy-marker (point))))
9515         (goto-char (point-min))
9516         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9517           (delete-region (match-beginning 1) (match-end 1))
9518           (insert (number-to-string length)))
9519         (goto-char (point-min))
9520         (when (re-search-forward
9521                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9522           (delete-region (match-beginning 1) (match-end 1))
9523           (insert (number-to-string length)))
9524         (goto-char (point-min))
9525         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9526           (delete-region (match-beginning 1) (match-end 1))
9527           (insert (number-to-string lines))))))
9528   ;; Replace the article.
9529   (let ((buf (current-buffer)))
9530     (with-temp-buffer
9531       (insert-buffer-substring buf)
9532
9533       (if (and (not read-only)
9534                (not (gnus-request-replace-article
9535                      (cdr gnus-article-current) (car gnus-article-current)
9536                      (current-buffer) t)))
9537           (error "Couldn't replace article")
9538         ;; Update the summary buffer.
9539         (if (and references
9540                  (equal (message-tokenize-header references " ")
9541                         (message-tokenize-header
9542                          (or (message-fetch-field "references") "") " ")))
9543             ;; We only have to update this line.
9544             (save-excursion
9545               (save-restriction
9546                 (message-narrow-to-head)
9547                 (let ((head (buffer-string))
9548                       header)
9549                   (with-temp-buffer
9550                     (insert (format "211 %d Article retrieved.\n"
9551                                     (cdr gnus-article-current)))
9552                     (insert head)
9553                     (insert ".\n")
9554                     (let ((nntp-server-buffer (current-buffer)))
9555                       (setq header (car (gnus-get-newsgroup-headers
9556                                          nil t))))
9557                     (save-excursion
9558                       (set-buffer gnus-summary-buffer)
9559                       (gnus-data-set-header
9560                        (gnus-data-find (cdr gnus-article-current))
9561                        header)
9562                       (gnus-summary-update-article-line
9563                        (cdr gnus-article-current) header)
9564                       (if (gnus-summary-goto-subject
9565                            (cdr gnus-article-current) nil t)
9566                           (gnus-summary-update-secondary-mark
9567                            (cdr gnus-article-current))))))))
9568           ;; Update threads.
9569           (set-buffer (or buffer gnus-summary-buffer))
9570           (gnus-summary-update-article (cdr gnus-article-current))
9571           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9572               (gnus-summary-update-secondary-mark
9573                (cdr gnus-article-current))))
9574         ;; Prettify the article buffer again.
9575         (unless no-highlight
9576           (save-excursion
9577             (set-buffer gnus-article-buffer)
9578             ;;;!!! Fix this -- article should be rehighlighted.
9579             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9580             (set-buffer gnus-original-article-buffer)
9581             (gnus-request-article
9582              (cdr gnus-article-current)
9583              (car gnus-article-current) (current-buffer))))
9584         ;; Prettify the summary buffer line.
9585         (when (gnus-visual-p 'summary-highlight 'highlight)
9586           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9587
9588 (defun gnus-summary-edit-wash (key)
9589   "Perform editing command KEY in the article buffer."
9590   (interactive
9591    (list
9592     (progn
9593       (message "%s" (concat (this-command-keys) "- "))
9594       (read-char))))
9595   (message "")
9596   (gnus-summary-edit-article)
9597   (execute-kbd-macro (concat (this-command-keys) key))
9598   (gnus-article-edit-done))
9599
9600 ;;; Respooling
9601
9602 (defun gnus-summary-respool-query (&optional silent trace)
9603   "Query where the respool algorithm would put this article."
9604   (interactive)
9605   (let (gnus-mark-article-hook)
9606     (gnus-summary-select-article)
9607     (save-excursion
9608       (set-buffer gnus-original-article-buffer)
9609       (let ((groups (nnmail-article-group 'identity trace)))
9610         (unless silent
9611           (if groups
9612               (message "This message would go to %s"
9613                        (mapconcat 'car groups ", "))
9614             (message "This message would go to no groups"))
9615           groups)))))
9616
9617 (defun gnus-summary-respool-trace ()
9618   "Trace where the respool algorithm would put this article.
9619 Display a buffer showing all fancy splitting patterns which matched."
9620   (interactive)
9621   (gnus-summary-respool-query nil t))
9622
9623 ;; Summary marking commands.
9624
9625 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9626   "Mark articles which has the same subject as read, and then select the next.
9627 If UNMARK is positive, remove any kind of mark.
9628 If UNMARK is negative, tick articles."
9629   (interactive "P")
9630   (when unmark
9631     (setq unmark (prefix-numeric-value unmark)))
9632   (let ((count
9633          (gnus-summary-mark-same-subject
9634           (gnus-summary-article-subject) unmark)))
9635     ;; Select next unread article.  If auto-select-same mode, should
9636     ;; select the first unread article.
9637     (gnus-summary-next-article t (and gnus-auto-select-same
9638                                       (gnus-summary-article-subject)))
9639     (gnus-message 7 "%d article%s marked as %s"
9640                   count (if (= count 1) " is" "s are")
9641                   (if unmark "unread" "read"))))
9642
9643 (defun gnus-summary-kill-same-subject (&optional unmark)
9644   "Mark articles which has the same subject as read.
9645 If UNMARK is positive, remove any kind of mark.
9646 If UNMARK is negative, tick articles."
9647   (interactive "P")
9648   (when unmark
9649     (setq unmark (prefix-numeric-value unmark)))
9650   (let ((count
9651          (gnus-summary-mark-same-subject
9652           (gnus-summary-article-subject) unmark)))
9653     ;; If marked as read, go to next unread subject.
9654     (when (null unmark)
9655       ;; Go to next unread subject.
9656       (gnus-summary-next-subject 1 t))
9657     (gnus-message 7 "%d articles are marked as %s"
9658                   count (if unmark "unread" "read"))))
9659
9660 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9661   "Mark articles with same SUBJECT as read, and return marked number.
9662 If optional argument UNMARK is positive, remove any kinds of marks.
9663 If optional argument UNMARK is negative, mark articles as unread instead."
9664   (let ((count 1))
9665     (save-excursion
9666       (cond
9667        ((null unmark)                   ; Mark as read.
9668         (while (and
9669                 (progn
9670                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9671                   (gnus-summary-show-thread) t)
9672                 (gnus-summary-find-subject subject))
9673           (setq count (1+ count))))
9674        ((> unmark 0)                    ; Tick.
9675         (while (and
9676                 (progn
9677                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9678                   (gnus-summary-show-thread) t)
9679                 (gnus-summary-find-subject subject))
9680           (setq count (1+ count))))
9681        (t                               ; Mark as unread.
9682         (while (and
9683                 (progn
9684                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9685                   (gnus-summary-show-thread) t)
9686                 (gnus-summary-find-subject subject))
9687           (setq count (1+ count)))))
9688       (gnus-set-mode-line 'summary)
9689       ;; Return the number of marked articles.
9690       count)))
9691
9692 (defun gnus-summary-mark-as-processable (n &optional unmark)
9693   "Set the process mark on the next N articles.
9694 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9695 the process mark instead.  The difference between N and the actual
9696 number of articles marked is returned."
9697   (interactive "P")
9698   (if (and (null n) (gnus-region-active-p))
9699       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9700     (setq n (prefix-numeric-value n))
9701     (let ((backward (< n 0))
9702           (n (abs n)))
9703       (while (and
9704               (> n 0)
9705               (if unmark
9706                   (gnus-summary-remove-process-mark
9707                    (gnus-summary-article-number))
9708                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9709               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9710         (setq n (1- n)))
9711       (when (/= 0 n)
9712         (gnus-message 7 "No more articles"))
9713       (gnus-summary-recenter)
9714       (gnus-summary-position-point)
9715       n)))
9716
9717 (defun gnus-summary-unmark-as-processable (n)
9718   "Remove the process mark from the next N articles.
9719 If N is negative, unmark backward instead.  The difference between N and
9720 the actual number of articles unmarked is returned."
9721   (interactive "P")
9722   (gnus-summary-mark-as-processable n t))
9723
9724 (defun gnus-summary-unmark-all-processable ()
9725   "Remove the process mark from all articles."
9726   (interactive)
9727   (save-excursion
9728     (while gnus-newsgroup-processable
9729       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9730   (gnus-summary-position-point))
9731
9732 (defun gnus-summary-add-mark (article type)
9733   "Mark ARTICLE with a mark of TYPE."
9734   (let ((vtype (car (assq type gnus-article-mark-lists)))
9735         var)
9736     (if (not vtype)
9737         (error "No such mark type: %s" type)
9738       (setq var (intern (format "gnus-newsgroup-%s" type)))
9739       (set var (cons article (symbol-value var)))
9740       (if (memq type '(processable cached replied forwarded recent saved))
9741           (gnus-summary-update-secondary-mark article)
9742         ;;; !!! This is bogus.  We should find out what primary
9743         ;;; !!! mark we want to set.
9744         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9745
9746 (defun gnus-summary-mark-as-expirable (n)
9747   "Mark N articles forward as expirable.
9748 If N is negative, mark backward instead.  The difference between N and
9749 the actual number of articles marked is returned."
9750   (interactive "p")
9751   (gnus-summary-mark-forward n gnus-expirable-mark))
9752
9753 (defun gnus-summary-mark-as-spam (n)
9754   "Mark N articles forward as spam.
9755 If N is negative, mark backward instead.  The difference between N and
9756 the actual number of articles marked is returned."
9757   (interactive "p")
9758   (gnus-summary-mark-forward n gnus-spam-mark))
9759
9760 (defun gnus-summary-mark-article-as-replied (article)
9761   "Mark ARTICLE as replied to and update the summary line.
9762 ARTICLE can also be a list of articles."
9763   (interactive (list (gnus-summary-article-number)))
9764   (let ((articles (if (listp article) article (list article))))
9765     (dolist (article articles)
9766       (unless (numberp article)
9767         (error "%s is not a number" article))
9768       (push article gnus-newsgroup-replied)
9769       (let ((buffer-read-only nil))
9770         (when (gnus-summary-goto-subject article nil t)
9771           (gnus-summary-update-secondary-mark article))))))
9772
9773 (defun gnus-summary-mark-article-as-forwarded (article)
9774   "Mark ARTICLE as forwarded and update the summary line.
9775 ARTICLE can also be a list of articles."
9776   (let ((articles (if (listp article) article (list article))))
9777     (dolist (article articles)
9778       (push article gnus-newsgroup-forwarded)
9779       (let ((buffer-read-only nil))
9780         (when (gnus-summary-goto-subject article nil t)
9781           (gnus-summary-update-secondary-mark article))))))
9782
9783 (defun gnus-summary-set-bookmark (article)
9784   "Set a bookmark in current article."
9785   (interactive (list (gnus-summary-article-number)))
9786   (when (or (not (get-buffer gnus-article-buffer))
9787             (not gnus-current-article)
9788             (not gnus-article-current)
9789             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9790     (error "No current article selected"))
9791   ;; Remove old bookmark, if one exists.
9792   (gnus-pull article gnus-newsgroup-bookmarks)
9793   ;; Set the new bookmark, which is on the form
9794   ;; (article-number . line-number-in-body).
9795   (push
9796    (cons article
9797          (with-current-buffer gnus-article-buffer
9798            (count-lines
9799             (min (point)
9800                  (save-excursion
9801                    (article-goto-body)
9802                    (point)))
9803             (point))))
9804    gnus-newsgroup-bookmarks)
9805   (gnus-message 6 "A bookmark has been added to the current article."))
9806
9807 (defun gnus-summary-remove-bookmark (article)
9808   "Remove the bookmark from the current article."
9809   (interactive (list (gnus-summary-article-number)))
9810   ;; Remove old bookmark, if one exists.
9811   (if (not (assq article gnus-newsgroup-bookmarks))
9812       (gnus-message 6 "No bookmark in current article.")
9813     (gnus-pull article gnus-newsgroup-bookmarks)
9814     (gnus-message 6 "Removed bookmark.")))
9815
9816 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9817 (defun gnus-summary-mark-as-dormant (n)
9818   "Mark N articles forward as dormant.
9819 If N is negative, mark backward instead.  The difference between N and
9820 the actual number of articles marked is returned."
9821   (interactive "p")
9822   (gnus-summary-mark-forward n gnus-dormant-mark))
9823
9824 (defun gnus-summary-set-process-mark (article)
9825   "Set the process mark on ARTICLE and update the summary line."
9826   (setq gnus-newsgroup-processable
9827         (cons article
9828               (delq article gnus-newsgroup-processable)))
9829   (when (gnus-summary-goto-subject article)
9830     (gnus-summary-show-thread)
9831     (gnus-summary-goto-subject article)
9832     (gnus-summary-update-secondary-mark article)))
9833
9834 (defun gnus-summary-remove-process-mark (article)
9835   "Remove the process mark from ARTICLE and update the summary line."
9836   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9837   (when (gnus-summary-goto-subject article)
9838     (gnus-summary-show-thread)
9839     (gnus-summary-goto-subject article)
9840     (gnus-summary-update-secondary-mark article)))
9841
9842 (defun gnus-summary-set-saved-mark (article)
9843   "Set the process mark on ARTICLE and update the summary line."
9844   (push article gnus-newsgroup-saved)
9845   (when (gnus-summary-goto-subject article)
9846     (gnus-summary-update-secondary-mark article)))
9847
9848 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9849   "Mark N articles as read forwards.
9850 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9851 The difference between N and the actual number of articles marked is
9852 returned.
9853 If NO-EXPIRE, auto-expiry will be inhibited."
9854   (interactive "p")
9855   (gnus-summary-show-thread)
9856   (let ((backward (< n 0))
9857         (gnus-summary-goto-unread
9858          (and gnus-summary-goto-unread
9859               (not (eq gnus-summary-goto-unread 'never))
9860               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9861                                     gnus-ticked-mark gnus-dormant-mark)))))
9862         (n (abs n))
9863         (mark (or mark gnus-del-mark)))
9864     (while (and (> n 0)
9865                 (gnus-summary-mark-article nil mark no-expire)
9866                 (zerop (gnus-summary-next-subject
9867                         (if backward -1 1)
9868                         (and gnus-summary-goto-unread
9869                              (not (eq gnus-summary-goto-unread 'never)))
9870                         t)))
9871       (setq n (1- n)))
9872     (when (/= 0 n)
9873       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9874     (gnus-summary-recenter)
9875     (gnus-summary-position-point)
9876     (gnus-set-mode-line 'summary)
9877     n))
9878
9879 (defun gnus-summary-mark-article-as-read (mark)
9880   "Mark the current article quickly as read with MARK."
9881   (let ((article (gnus-summary-article-number)))
9882     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9883     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9884     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9885     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9886     (push (cons article mark) gnus-newsgroup-reads)
9887     ;; Possibly remove from cache, if that is used.
9888     (when gnus-use-cache
9889       (gnus-cache-enter-remove-article article))
9890     ;; Allow the backend to change the mark.
9891     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9892     ;; Check for auto-expiry.
9893     (when (and gnus-newsgroup-auto-expire
9894                (memq mark gnus-auto-expirable-marks))
9895       (setq mark gnus-expirable-mark)
9896       ;; Let the backend know about the mark change.
9897       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9898       (push article gnus-newsgroup-expirable))
9899     ;; Set the mark in the buffer.
9900     (gnus-summary-update-mark mark 'unread)
9901     t))
9902
9903 (defun gnus-summary-mark-article-as-unread (mark)
9904   "Mark the current article quickly as unread with MARK."
9905   (let* ((article (gnus-summary-article-number))
9906          (old-mark (gnus-summary-article-mark article)))
9907     ;; Allow the backend to change the mark.
9908     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9909     (if (eq mark old-mark)
9910         t
9911       (if (<= article 0)
9912           (progn
9913             (gnus-error 1 "Can't mark negative article numbers")
9914             nil)
9915         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9916         (setq gnus-newsgroup-spam-marked
9917               (delq article gnus-newsgroup-spam-marked))
9918         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9919         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9920         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9921         (cond ((= mark gnus-ticked-mark)
9922                (setq gnus-newsgroup-marked
9923                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9924                                               article)))
9925               ((= mark gnus-spam-mark)
9926                (setq gnus-newsgroup-spam-marked
9927                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9928                                               article)))
9929               ((= mark gnus-dormant-mark)
9930                (setq gnus-newsgroup-dormant
9931                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9932                                               article)))
9933               (t
9934                (setq gnus-newsgroup-unreads
9935                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9936                                               article))))
9937         (gnus-pull article gnus-newsgroup-reads)
9938
9939         ;; See whether the article is to be put in the cache.
9940         (and gnus-use-cache
9941              (vectorp (gnus-summary-article-header article))
9942              (save-excursion
9943                (gnus-cache-possibly-enter-article
9944                 gnus-newsgroup-name article
9945                 (= mark gnus-ticked-mark)
9946                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9947
9948         ;; Fix the mark.
9949         (gnus-summary-update-mark mark 'unread)
9950         t))))
9951
9952 (defun gnus-summary-mark-article (&optional article mark no-expire)
9953   "Mark ARTICLE with MARK.  MARK can be any character.
9954 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9955 `??' (dormant) and `?E' (expirable).
9956 If MARK is nil, then the default character `?r' is used.
9957 If ARTICLE is nil, then the article on the current line will be
9958 marked.
9959 If NO-EXPIRE, auto-expiry will be inhibited."
9960   ;; The mark might be a string.
9961   (when (stringp mark)
9962     (setq mark (aref mark 0)))
9963   ;; If no mark is given, then we check auto-expiring.
9964   (when (null mark)
9965     (setq mark gnus-del-mark))
9966   (when (and (not no-expire)
9967              gnus-newsgroup-auto-expire
9968              (memq mark gnus-auto-expirable-marks))
9969     (setq mark gnus-expirable-mark))
9970   (let ((article (or article (gnus-summary-article-number)))
9971         (old-mark (gnus-summary-article-mark article)))
9972     ;; Allow the backend to change the mark.
9973     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9974     (if (eq mark old-mark)
9975         t
9976       (unless article
9977         (error "No article on current line"))
9978       (if (not (if (or (= mark gnus-unread-mark)
9979                        (= mark gnus-ticked-mark)
9980                        (= mark gnus-spam-mark)
9981                        (= mark gnus-dormant-mark))
9982                    (gnus-mark-article-as-unread article mark)
9983                  (gnus-mark-article-as-read article mark)))
9984           t
9985         ;; See whether the article is to be put in the cache.
9986         (and gnus-use-cache
9987              (not (= mark gnus-canceled-mark))
9988              (vectorp (gnus-summary-article-header article))
9989              (save-excursion
9990                (gnus-cache-possibly-enter-article
9991                 gnus-newsgroup-name article
9992                 (= mark gnus-ticked-mark)
9993                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9994
9995         (when (gnus-summary-goto-subject article nil t)
9996           (let ((buffer-read-only nil))
9997             (gnus-summary-show-thread)
9998             ;; Fix the mark.
9999             (gnus-summary-update-mark mark 'unread)
10000             t))))))
10001
10002 (defun gnus-summary-update-secondary-mark (article)
10003   "Update the secondary (read, process, cache) mark."
10004   (gnus-summary-update-mark
10005    (cond ((memq article gnus-newsgroup-processable)
10006           gnus-process-mark)
10007          ((memq article gnus-newsgroup-cached)
10008           gnus-cached-mark)
10009          ((memq article gnus-newsgroup-replied)
10010           gnus-replied-mark)
10011          ((memq article gnus-newsgroup-forwarded)
10012           gnus-forwarded-mark)
10013          ((memq article gnus-newsgroup-saved)
10014           gnus-saved-mark)
10015          ((memq article gnus-newsgroup-recent)
10016           gnus-recent-mark)
10017          ((memq article gnus-newsgroup-unseen)
10018           gnus-unseen-mark)
10019          (t gnus-no-mark))
10020    'replied)
10021   (when (gnus-visual-p 'summary-highlight 'highlight)
10022     (gnus-run-hooks 'gnus-summary-update-hook))
10023   t)
10024
10025 (defun gnus-summary-update-download-mark (article)
10026   "Update the download mark."
10027   (gnus-summary-update-mark
10028    (cond ((memq article gnus-newsgroup-undownloaded)
10029           gnus-undownloaded-mark)
10030          (gnus-newsgroup-agentized
10031           gnus-downloaded-mark)
10032          (t
10033           gnus-no-mark))
10034    'download)
10035   (gnus-summary-update-line t)
10036   t)
10037
10038 (defun gnus-summary-update-mark (mark type)
10039   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10040         (buffer-read-only nil))
10041     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10042     (when forward
10043       (when (looking-at "\r")
10044         (incf forward))
10045       (when (<= (+ forward (point)) (point-max))
10046         ;; Go to the right position on the line.
10047         (goto-char (+ forward (point)))
10048         ;; Replace the old mark with the new mark.
10049         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10050         ;; Optionally update the marks by some user rule.
10051         (when (eq type 'unread)
10052           (gnus-data-set-mark
10053            (gnus-data-find (gnus-summary-article-number)) mark)
10054           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10055
10056 (defun gnus-mark-article-as-read (article &optional mark)
10057   "Enter ARTICLE in the pertinent lists and remove it from others."
10058   ;; Make the article expirable.
10059   (let ((mark (or mark gnus-del-mark)))
10060     (setq gnus-newsgroup-expirable
10061           (if (= mark gnus-expirable-mark)
10062               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10063             (delq article gnus-newsgroup-expirable)))
10064     ;; Remove from unread and marked lists.
10065     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10066     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10067     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10068     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10069     (push (cons article mark) gnus-newsgroup-reads)
10070     ;; Possibly remove from cache, if that is used.
10071     (when gnus-use-cache
10072       (gnus-cache-enter-remove-article article))
10073     t))
10074
10075 (defun gnus-mark-article-as-unread (article &optional mark)
10076   "Enter ARTICLE in the pertinent lists and remove it from others."
10077   (let ((mark (or mark gnus-ticked-mark)))
10078     (if (<= article 0)
10079         (progn
10080           (gnus-error 1 "Can't mark negative article numbers")
10081           nil)
10082       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10083             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10084             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10085             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10086             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10087
10088       ;; Unsuppress duplicates?
10089       (when gnus-suppress-duplicates
10090         (gnus-dup-unsuppress-article article))
10091
10092       (cond ((= mark gnus-ticked-mark)
10093              (setq gnus-newsgroup-marked
10094                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10095             ((= mark gnus-spam-mark)
10096              (setq gnus-newsgroup-spam-marked
10097                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10098                                             article)))
10099             ((= mark gnus-dormant-mark)
10100              (setq gnus-newsgroup-dormant
10101                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10102             (t
10103              (setq gnus-newsgroup-unreads
10104                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10105       (gnus-pull article gnus-newsgroup-reads)
10106       t)))
10107
10108 (defalias 'gnus-summary-mark-as-unread-forward
10109   'gnus-summary-tick-article-forward)
10110 (make-obsolete 'gnus-summary-mark-as-unread-forward
10111                'gnus-summary-tick-article-forward)
10112 (defun gnus-summary-tick-article-forward (n)
10113   "Tick N articles forwards.
10114 If N is negative, tick backwards instead.
10115 The difference between N and the number of articles ticked is returned."
10116   (interactive "p")
10117   (gnus-summary-mark-forward n gnus-ticked-mark))
10118
10119 (defalias 'gnus-summary-mark-as-unread-backward
10120   'gnus-summary-tick-article-backward)
10121 (make-obsolete 'gnus-summary-mark-as-unread-backward
10122                'gnus-summary-tick-article-backward)
10123 (defun gnus-summary-tick-article-backward (n)
10124   "Tick N articles backwards.
10125 The difference between N and the number of articles ticked is returned."
10126   (interactive "p")
10127   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10128
10129 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10130 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10131 (defun gnus-summary-tick-article (&optional article clear-mark)
10132   "Mark current article as unread.
10133 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10134 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10135   (interactive)
10136   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10137                                        gnus-ticked-mark)))
10138
10139 (defun gnus-summary-mark-as-read-forward (n)
10140   "Mark N articles as read forwards.
10141 If N is negative, mark backwards instead.
10142 The difference between N and the actual number of articles marked is
10143 returned."
10144   (interactive "p")
10145   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10146
10147 (defun gnus-summary-mark-as-read-backward (n)
10148   "Mark the N articles as read backwards.
10149 The difference between N and the actual number of articles marked is
10150 returned."
10151   (interactive "p")
10152   (gnus-summary-mark-forward
10153    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10154
10155 (defun gnus-summary-mark-as-read (&optional article mark)
10156   "Mark current article as read.
10157 ARTICLE specifies the article to be marked as read.
10158 MARK specifies a string to be inserted at the beginning of the line."
10159   (gnus-summary-mark-article article mark))
10160
10161 (defun gnus-summary-clear-mark-forward (n)
10162   "Clear marks from N articles forward.
10163 If N is negative, clear backward instead.
10164 The difference between N and the number of marks cleared is returned."
10165   (interactive "p")
10166   (gnus-summary-mark-forward n gnus-unread-mark))
10167
10168 (defun gnus-summary-clear-mark-backward (n)
10169   "Clear marks from N articles backward.
10170 The difference between N and the number of marks cleared is returned."
10171   (interactive "p")
10172   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10173
10174 (defun gnus-summary-mark-unread-as-read ()
10175   "Intended to be used by `gnus-summary-mark-article-hook'."
10176   (when (memq gnus-current-article gnus-newsgroup-unreads)
10177     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10178
10179 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10180   "Intended to be used by `gnus-summary-mark-article-hook'."
10181   (let ((mark (gnus-summary-article-mark)))
10182     (when (or (gnus-unread-mark-p mark)
10183               (gnus-read-mark-p mark))
10184       (gnus-summary-mark-article gnus-current-article
10185                                  (or new-mark gnus-read-mark)))))
10186
10187 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10188   "Intended to be used by `gnus-summary-mark-article-hook'."
10189   (let ((mark (gnus-summary-article-mark)))
10190     (when (or (gnus-unread-mark-p mark)
10191               (gnus-read-mark-p mark))
10192       (gnus-summary-mark-article (gnus-summary-article-number)
10193                                  (or new-mark gnus-read-mark)))))
10194
10195 (defun gnus-summary-mark-unread-as-ticked ()
10196   "Intended to be used by `gnus-summary-mark-article-hook'."
10197   (when (memq gnus-current-article gnus-newsgroup-unreads)
10198     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10199
10200 (defun gnus-summary-mark-region-as-read (point mark all)
10201   "Mark all unread articles between point and mark as read.
10202 If given a prefix, mark all articles between point and mark as read,
10203 even ticked and dormant ones."
10204   (interactive "r\nP")
10205   (save-excursion
10206     (let (article)
10207       (goto-char point)
10208       (beginning-of-line)
10209       (while (and
10210               (< (point) mark)
10211               (progn
10212                 (when (or all
10213                           (memq (setq article (gnus-summary-article-number))
10214                                 gnus-newsgroup-unreads))
10215                   (gnus-summary-mark-article article gnus-del-mark))
10216                 t)
10217               (gnus-summary-find-next))))))
10218
10219 (defun gnus-summary-mark-below (score mark)
10220   "Mark articles with score less than SCORE with MARK."
10221   (interactive "P\ncMark: ")
10222   (setq score (if score
10223                   (prefix-numeric-value score)
10224                 (or gnus-summary-default-score 0)))
10225   (save-excursion
10226     (set-buffer gnus-summary-buffer)
10227     (goto-char (point-min))
10228     (while
10229         (progn
10230           (and (< (gnus-summary-article-score) score)
10231                (gnus-summary-mark-article nil mark))
10232           (gnus-summary-find-next)))))
10233
10234 (defun gnus-summary-kill-below (&optional score)
10235   "Mark articles with score below SCORE as read."
10236   (interactive "P")
10237   (gnus-summary-mark-below score gnus-killed-mark))
10238
10239 (defun gnus-summary-clear-above (&optional score)
10240   "Clear all marks from articles with score above SCORE."
10241   (interactive "P")
10242   (gnus-summary-mark-above score gnus-unread-mark))
10243
10244 (defun gnus-summary-tick-above (&optional score)
10245   "Tick all articles with score above SCORE."
10246   (interactive "P")
10247   (gnus-summary-mark-above score gnus-ticked-mark))
10248
10249 (defun gnus-summary-mark-above (score mark)
10250   "Mark articles with score over SCORE with MARK."
10251   (interactive "P\ncMark: ")
10252   (setq score (if score
10253                   (prefix-numeric-value score)
10254                 (or gnus-summary-default-score 0)))
10255   (save-excursion
10256     (set-buffer gnus-summary-buffer)
10257     (goto-char (point-min))
10258     (while (and (progn
10259                   (when (> (gnus-summary-article-score) score)
10260                     (gnus-summary-mark-article nil mark))
10261                   t)
10262                 (gnus-summary-find-next)))))
10263
10264 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10265 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10266 (defun gnus-summary-limit-include-expunged (&optional no-error)
10267   "Display all the hidden articles that were expunged for low scores."
10268   (interactive)
10269   (let ((buffer-read-only nil))
10270     (let ((scored gnus-newsgroup-scored)
10271           headers h)
10272       (while scored
10273         (unless (gnus-summary-article-header (caar scored))
10274           (and (setq h (gnus-number-to-header (caar scored)))
10275                (< (cdar scored) gnus-summary-expunge-below)
10276                (push h headers)))
10277         (setq scored (cdr scored)))
10278       (if (not headers)
10279           (when (not no-error)
10280             (error "No expunged articles hidden"))
10281         (goto-char (point-min))
10282         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10283         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10284         (mapcar (lambda (x) (push (mail-header-number x)
10285                                   gnus-newsgroup-limit))
10286                 headers)
10287         (gnus-summary-prepare-unthreaded (nreverse headers))
10288         (goto-char (point-min))
10289         (gnus-summary-position-point)
10290         t))))
10291
10292 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10293   "Mark all unread articles in this newsgroup as read.
10294 If prefix argument ALL is non-nil, ticked and dormant articles will
10295 also be marked as read.
10296 If QUIETLY is non-nil, no questions will be asked.
10297
10298 If TO-HERE is non-nil, it should be a point in the buffer.  All
10299 articles before (after, if REVERSE is set) this point will be marked
10300 as read.
10301
10302 Note that this function will only catch up the unread article
10303 in the current summary buffer limitation.
10304
10305 The number of articles marked as read is returned."
10306   (interactive "P")
10307   (prog1
10308       (save-excursion
10309         (when (or quietly
10310                   (not gnus-interactive-catchup) ;Without confirmation?
10311                   gnus-expert-user
10312                   (gnus-y-or-n-p
10313                    (if all
10314                        "Mark absolutely all articles as read? "
10315                      "Mark all unread articles as read? ")))
10316           (if (and not-mark
10317                    (not gnus-newsgroup-adaptive)
10318                    (not gnus-newsgroup-auto-expire)
10319                    (not gnus-suppress-duplicates)
10320                    (or (not gnus-use-cache)
10321                        (eq gnus-use-cache 'passive)))
10322               (progn
10323                 (when all
10324                   (setq gnus-newsgroup-marked nil
10325                         gnus-newsgroup-spam-marked nil
10326                         gnus-newsgroup-dormant nil))
10327                 (setq gnus-newsgroup-unreads
10328                       (gnus-sorted-nunion
10329                        (gnus-intersection gnus-newsgroup-unreads
10330                                           gnus-newsgroup-downloadable)
10331                        gnus-newsgroup-unfetched)))
10332             ;; We actually mark all articles as canceled, which we
10333             ;; have to do when using auto-expiry or adaptive scoring.
10334             (gnus-summary-show-all-threads)
10335             (if (and to-here reverse)
10336                 (progn
10337                   (goto-char to-here)
10338                   (gnus-summary-mark-current-read-and-unread-as-read
10339                    gnus-catchup-mark)
10340                   (while (gnus-summary-find-next (not all))
10341                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10342               (when (gnus-summary-first-subject (not all))
10343                 (while (and
10344                         (if to-here (< (point) to-here) t)
10345                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10346                         (gnus-summary-find-next (not all))))))
10347             (gnus-set-mode-line 'summary))
10348           t))
10349     (gnus-summary-position-point)))
10350
10351 (defun gnus-summary-catchup-to-here (&optional all)
10352   "Mark all unticked articles before the current one as read.
10353 If ALL is non-nil, also mark ticked and dormant articles as read."
10354   (interactive "P")
10355   (save-excursion
10356     (gnus-save-hidden-threads
10357       (let ((beg (point)))
10358         ;; We check that there are unread articles.
10359         (when (or all (gnus-summary-find-prev))
10360           (gnus-summary-catchup all t beg)))))
10361   (gnus-summary-position-point))
10362
10363 (defun gnus-summary-catchup-from-here (&optional all)
10364   "Mark all unticked articles after (and including) the current one as read.
10365 If ALL is non-nil, also mark ticked and dormant articles as read."
10366   (interactive "P")
10367   (save-excursion
10368     (gnus-save-hidden-threads
10369       (let ((beg (point)))
10370         ;; We check that there are unread articles.
10371         (when (or all (gnus-summary-find-next))
10372           (gnus-summary-catchup all t beg nil t)))))
10373   (gnus-summary-position-point))
10374
10375 (defun gnus-summary-catchup-all (&optional quietly)
10376   "Mark all articles in this newsgroup as read.
10377 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10378 instead, which marks only unread articles as read."
10379   (interactive "P")
10380   (gnus-summary-catchup t quietly))
10381
10382 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10383   "Mark all unread articles in this group as read, then exit.
10384 If prefix argument ALL is non-nil, all articles are marked as read.
10385 If QUIETLY is non-nil, no questions will be asked."
10386   (interactive "P")
10387   (when (gnus-summary-catchup all quietly nil 'fast)
10388     ;; Select next newsgroup or exit.
10389     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10390              (eq gnus-auto-select-next 'quietly))
10391         (gnus-summary-next-group nil)
10392       (gnus-summary-exit))))
10393
10394 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10395   "Mark all articles in this newsgroup as read, and then exit.
10396 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10397 instead, which marks only unread articles as read."
10398   (interactive "P")
10399   (gnus-summary-catchup-and-exit t quietly))
10400
10401 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10402   "Mark all articles in this group as read and select the next group.
10403 If given a prefix, mark all articles, unread as well as ticked, as
10404 read."
10405   (interactive "P")
10406   (save-excursion
10407     (gnus-summary-catchup all))
10408   (gnus-summary-next-group))
10409
10410 ;;;
10411 ;;; with article
10412 ;;;
10413
10414 (defmacro gnus-with-article (article &rest forms)
10415   "Select ARTICLE and perform FORMS in the original article buffer.
10416 Then replace the article with the result."
10417   `(progn
10418      ;; We don't want the article to be marked as read.
10419      (let (gnus-mark-article-hook)
10420        (gnus-summary-select-article t t nil ,article))
10421      (set-buffer gnus-original-article-buffer)
10422      ,@forms
10423      (if (not (gnus-check-backend-function
10424                'request-replace-article (car gnus-article-current)))
10425          (gnus-message 5 "Read-only group; not replacing")
10426        (unless (gnus-request-replace-article
10427                 ,article (car gnus-article-current)
10428                 (current-buffer) t)
10429          (error "Couldn't replace article")))
10430      ;; The cache and backlog have to be flushed somewhat.
10431      (when gnus-keep-backlog
10432        (gnus-backlog-remove-article
10433         (car gnus-article-current) (cdr gnus-article-current)))
10434      (when gnus-use-cache
10435        (gnus-cache-update-article
10436         (car gnus-article-current) (cdr gnus-article-current)))))
10437
10438 (put 'gnus-with-article 'lisp-indent-function 1)
10439 (put 'gnus-with-article 'edebug-form-spec '(form body))
10440
10441 ;; Thread-based commands.
10442
10443 (defun gnus-summary-articles-in-thread (&optional article)
10444   "Return a list of all articles in the current thread.
10445 If ARTICLE is non-nil, return all articles in the thread that starts
10446 with that article."
10447   (let* ((article (or article (gnus-summary-article-number)))
10448          (data (gnus-data-find-list article))
10449          (top-level (gnus-data-level (car data)))
10450          (top-subject
10451           (cond ((null gnus-thread-operation-ignore-subject)
10452                  (gnus-simplify-subject-re
10453                   (mail-header-subject (gnus-data-header (car data)))))
10454                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10455                  (gnus-simplify-subject-fuzzy
10456                   (mail-header-subject (gnus-data-header (car data)))))
10457                 (t nil)))
10458          (end-point (save-excursion
10459                       (if (gnus-summary-go-to-next-thread)
10460                           (point) (point-max))))
10461          articles)
10462     (while (and data
10463                 (< (gnus-data-pos (car data)) end-point))
10464       (when (or (not top-subject)
10465                 (string= top-subject
10466                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10467                              (gnus-simplify-subject-fuzzy
10468                               (mail-header-subject
10469                                (gnus-data-header (car data))))
10470                            (gnus-simplify-subject-re
10471                             (mail-header-subject
10472                              (gnus-data-header (car data)))))))
10473         (push (gnus-data-number (car data)) articles))
10474       (unless (and (setq data (cdr data))
10475                    (> (gnus-data-level (car data)) top-level))
10476         (setq data nil)))
10477     ;; Return the list of articles.
10478     (nreverse articles)))
10479
10480 (defun gnus-summary-rethread-current ()
10481   "Rethread the thread the current article is part of."
10482   (interactive)
10483   (let* ((gnus-show-threads t)
10484          (article (gnus-summary-article-number))
10485          (id (mail-header-id (gnus-summary-article-header)))
10486          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10487     (unless id
10488       (error "No article on the current line"))
10489     (gnus-rebuild-thread id)
10490     (gnus-summary-goto-subject article)))
10491
10492 (defun gnus-summary-reparent-thread ()
10493   "Make the current article child of the marked (or previous) article.
10494
10495 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10496 is non-nil or the Subject: of both articles are the same."
10497   (interactive)
10498   (unless (not (gnus-group-read-only-p))
10499     (error "The current newsgroup does not support article editing"))
10500   (unless (<= (length gnus-newsgroup-processable) 1)
10501     (error "No more than one article may be marked"))
10502   (save-window-excursion
10503     (let ((gnus-article-buffer " *reparent*")
10504           (current-article (gnus-summary-article-number))
10505           ;; First grab the marked article, otherwise one line up.
10506           (parent-article (if (not (null gnus-newsgroup-processable))
10507                               (car gnus-newsgroup-processable)
10508                             (save-excursion
10509                               (if (eq (forward-line -1) 0)
10510                                   (gnus-summary-article-number)
10511                                 (error "Beginning of summary buffer"))))))
10512       (unless (not (eq current-article parent-article))
10513         (error "An article may not be self-referential"))
10514       (let ((message-id (mail-header-id
10515                          (gnus-summary-article-header parent-article))))
10516         (unless (and message-id (not (equal message-id "")))
10517           (error "No message-id in desired parent"))
10518         (gnus-with-article current-article
10519           (save-restriction
10520             (goto-char (point-min))
10521             (message-narrow-to-head)
10522             (if (re-search-forward "^References: " nil t)
10523                 (progn
10524                   (re-search-forward "^[^ \t]" nil t)
10525                   (forward-line -1)
10526                   (end-of-line)
10527                   (insert " " message-id))
10528               (insert "References: " message-id "\n"))))
10529         (set-buffer gnus-summary-buffer)
10530         (gnus-summary-unmark-all-processable)
10531         (gnus-summary-update-article current-article)
10532         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10533             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10534         (gnus-summary-rethread-current)
10535         (gnus-message 3 "Article %d is now the child of article %d"
10536                       current-article parent-article)))))
10537
10538 (defun gnus-summary-toggle-threads (&optional arg)
10539   "Toggle showing conversation threads.
10540 If ARG is positive number, turn showing conversation threads on."
10541   (interactive "P")
10542   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10543     (setq gnus-show-threads
10544           (if (null arg) (not gnus-show-threads)
10545             (> (prefix-numeric-value arg) 0)))
10546     (gnus-summary-prepare)
10547     (gnus-summary-goto-subject current)
10548     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10549     (gnus-summary-position-point)))
10550
10551 (defun gnus-summary-show-all-threads ()
10552   "Show all threads."
10553   (interactive)
10554   (save-excursion
10555     (let ((buffer-read-only nil))
10556       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10557   (gnus-summary-position-point))
10558
10559 (defun gnus-summary-show-thread ()
10560   "Show thread subtrees.
10561 Returns nil if no thread was there to be shown."
10562   (interactive)
10563   (let ((buffer-read-only nil)
10564         (orig (point))
10565         (end (point-at-eol))
10566         ;; Leave point at bol
10567         (beg (progn (beginning-of-line) (point))))
10568     (prog1
10569         ;; Any hidden lines here?
10570         (search-forward "\r" end t)
10571       (subst-char-in-region beg end ?\^M ?\n t)
10572       (goto-char orig)
10573       (gnus-summary-position-point))))
10574
10575 (defun gnus-summary-maybe-hide-threads ()
10576   "If requested, hide the threads that should be hidden."
10577   (when (and gnus-show-threads
10578              gnus-thread-hide-subtree)
10579     (gnus-summary-hide-all-threads
10580      (if (or (consp gnus-thread-hide-subtree)
10581              (functionp gnus-thread-hide-subtree))
10582          (gnus-make-predicate gnus-thread-hide-subtree)
10583        nil))))
10584
10585 ;;; Hiding predicates.
10586
10587 (defun gnus-article-unread-p (header)
10588   (memq (mail-header-number header) gnus-newsgroup-unreads))
10589
10590 (defun gnus-article-unseen-p (header)
10591   (memq (mail-header-number header) gnus-newsgroup-unseen))
10592
10593 (defun gnus-map-articles (predicate articles)
10594   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10595   (apply 'gnus-or (mapcar predicate
10596                           (mapcar 'gnus-summary-article-header articles))))
10597
10598 (defun gnus-summary-hide-all-threads (&optional predicate)
10599   "Hide all thread subtrees.
10600 If PREDICATE is supplied, threads that satisfy this predicate
10601 will not be hidden."
10602   (interactive)
10603   (save-excursion
10604     (goto-char (point-min))
10605     (let ((end nil))
10606       (while (not end)
10607         (when (or (not predicate)
10608                   (gnus-map-articles
10609                    predicate (gnus-summary-article-children)))
10610             (gnus-summary-hide-thread))
10611         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10612   (gnus-summary-position-point))
10613
10614 (defun gnus-summary-hide-thread ()
10615   "Hide thread subtrees.
10616 If PREDICATE is supplied, threads that satisfy this predicate
10617 will not be hidden.
10618 Returns nil if no threads were there to be hidden."
10619   (interactive)
10620   (let ((buffer-read-only nil)
10621         (start (point))
10622         (article (gnus-summary-article-number)))
10623     (goto-char start)
10624     ;; Go forward until either the buffer ends or the subthread
10625     ;; ends.
10626     (when (and (not (eobp))
10627                (or (zerop (gnus-summary-next-thread 1 t))
10628                    (goto-char (point-max))))
10629       (prog1
10630           (if (and (> (point) start)
10631                    (search-backward "\n" start t))
10632               (progn
10633                 (subst-char-in-region start (point) ?\n ?\^M)
10634                 (gnus-summary-goto-subject article))
10635             (goto-char start)
10636             nil)))))
10637
10638 (defun gnus-summary-go-to-next-thread (&optional previous)
10639   "Go to the same level (or less) next thread.
10640 If PREVIOUS is non-nil, go to previous thread instead.
10641 Return the article number moved to, or nil if moving was impossible."
10642   (let ((level (gnus-summary-thread-level))
10643         (way (if previous -1 1))
10644         (beg (point)))
10645     (forward-line way)
10646     (while (and (not (eobp))
10647                 (< level (gnus-summary-thread-level)))
10648       (forward-line way))
10649     (if (eobp)
10650         (progn
10651           (goto-char beg)
10652           nil)
10653       (setq beg (point))
10654       (prog1
10655           (gnus-summary-article-number)
10656         (goto-char beg)))))
10657
10658 (defun gnus-summary-next-thread (n &optional silent)
10659   "Go to the same level next N'th thread.
10660 If N is negative, search backward instead.
10661 Returns the difference between N and the number of skips actually
10662 done.
10663
10664 If SILENT, don't output messages."
10665   (interactive "p")
10666   (let ((backward (< n 0))
10667         (n (abs n)))
10668     (while (and (> n 0)
10669                 (gnus-summary-go-to-next-thread backward))
10670       (decf n))
10671     (unless silent
10672       (gnus-summary-position-point))
10673     (when (and (not silent) (/= 0 n))
10674       (gnus-message 7 "No more threads"))
10675     n))
10676
10677 (defun gnus-summary-prev-thread (n)
10678   "Go to the same level previous N'th thread.
10679 Returns the difference between N and the number of skips actually
10680 done."
10681   (interactive "p")
10682   (gnus-summary-next-thread (- n)))
10683
10684 (defun gnus-summary-go-down-thread ()
10685   "Go down one level in the current thread."
10686   (let ((children (gnus-summary-article-children)))
10687     (when children
10688       (gnus-summary-goto-subject (car children)))))
10689
10690 (defun gnus-summary-go-up-thread ()
10691   "Go up one level in the current thread."
10692   (let ((parent (gnus-summary-article-parent)))
10693     (when parent
10694       (gnus-summary-goto-subject parent))))
10695
10696 (defun gnus-summary-down-thread (n)
10697   "Go down thread N steps.
10698 If N is negative, go up instead.
10699 Returns the difference between N and how many steps down that were
10700 taken."
10701   (interactive "p")
10702   (let ((up (< n 0))
10703         (n (abs n)))
10704     (while (and (> n 0)
10705                 (if up (gnus-summary-go-up-thread)
10706                   (gnus-summary-go-down-thread)))
10707       (setq n (1- n)))
10708     (gnus-summary-position-point)
10709     (when (/= 0 n)
10710       (gnus-message 7 "Can't go further"))
10711     n))
10712
10713 (defun gnus-summary-up-thread (n)
10714   "Go up thread N steps.
10715 If N is negative, go down instead.
10716 Returns the difference between N and how many steps down that were
10717 taken."
10718   (interactive "p")
10719   (gnus-summary-down-thread (- n)))
10720
10721 (defun gnus-summary-top-thread ()
10722   "Go to the top of the thread."
10723   (interactive)
10724   (while (gnus-summary-go-up-thread))
10725   (gnus-summary-article-number))
10726
10727 (defun gnus-summary-kill-thread (&optional unmark)
10728   "Mark articles under current thread as read.
10729 If the prefix argument is positive, remove any kinds of marks.
10730 If the prefix argument is negative, tick articles instead."
10731   (interactive "P")
10732   (when unmark
10733     (setq unmark (prefix-numeric-value unmark)))
10734   (let ((articles (gnus-summary-articles-in-thread)))
10735     (save-excursion
10736       ;; Expand the thread.
10737       (gnus-summary-show-thread)
10738       ;; Mark all the articles.
10739       (while articles
10740         (gnus-summary-goto-subject (car articles))
10741         (cond ((null unmark)
10742                (gnus-summary-mark-article-as-read gnus-killed-mark))
10743               ((> unmark 0)
10744                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10745               (t
10746                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10747         (setq articles (cdr articles))))
10748     ;; Hide killed subtrees.
10749     (and (null unmark)
10750          gnus-thread-hide-killed
10751          (gnus-summary-hide-thread))
10752     ;; If marked as read, go to next unread subject.
10753     (when (null unmark)
10754       ;; Go to next unread subject.
10755       (gnus-summary-next-subject 1 t)))
10756   (gnus-set-mode-line 'summary))
10757
10758 ;; Summary sorting commands
10759
10760 (defun gnus-summary-sort-by-number (&optional reverse)
10761   "Sort the summary buffer by article number.
10762 Argument REVERSE means reverse order."
10763   (interactive "P")
10764   (gnus-summary-sort 'number reverse))
10765
10766 (defun gnus-summary-sort-by-random (&optional reverse)
10767   "Randomize the order in the summary buffer.
10768 Argument REVERSE means to randomize in reverse order."
10769   (interactive "P")
10770   (gnus-summary-sort 'random reverse))
10771
10772 (defun gnus-summary-sort-by-author (&optional reverse)
10773   "Sort the summary buffer by author name alphabetically.
10774 If `case-fold-search' is non-nil, case of letters is ignored.
10775 Argument REVERSE means reverse order."
10776   (interactive "P")
10777   (gnus-summary-sort 'author reverse))
10778
10779 (defun gnus-summary-sort-by-subject (&optional reverse)
10780   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10781 If `case-fold-search' is non-nil, case of letters is ignored.
10782 Argument REVERSE means reverse order."
10783   (interactive "P")
10784   (gnus-summary-sort 'subject reverse))
10785
10786 (defun gnus-summary-sort-by-date (&optional reverse)
10787   "Sort the summary buffer by date.
10788 Argument REVERSE means reverse order."
10789   (interactive "P")
10790   (gnus-summary-sort 'date reverse))
10791
10792 (defun gnus-summary-sort-by-score (&optional reverse)
10793   "Sort the summary buffer by score.
10794 Argument REVERSE means reverse order."
10795   (interactive "P")
10796   (gnus-summary-sort 'score reverse))
10797
10798 (defun gnus-summary-sort-by-lines (&optional reverse)
10799   "Sort the summary buffer by the number of lines.
10800 Argument REVERSE means reverse order."
10801   (interactive "P")
10802   (gnus-summary-sort 'lines reverse))
10803
10804 (defun gnus-summary-sort-by-chars (&optional reverse)
10805   "Sort the summary buffer by article length.
10806 Argument REVERSE means reverse order."
10807   (interactive "P")
10808   (gnus-summary-sort 'chars reverse))
10809
10810 (defun gnus-summary-sort-by-original (&optional reverse)
10811   "Sort the summary buffer using the default sorting method.
10812 Argument REVERSE means reverse order."
10813   (interactive "P")
10814   (let* ((buffer-read-only)
10815          (gnus-summary-prepare-hook nil))
10816     ;; We do the sorting by regenerating the threads.
10817     (gnus-summary-prepare)
10818     ;; Hide subthreads if needed.
10819     (gnus-summary-maybe-hide-threads)))
10820
10821 (defun gnus-summary-sort (predicate reverse)
10822   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10823   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10824          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10825          (gnus-thread-sort-functions
10826           (if (not reverse)
10827               thread
10828             `(lambda (t1 t2)
10829                (,thread t2 t1))))
10830          (gnus-sort-gathered-threads-function
10831           gnus-thread-sort-functions)
10832          (gnus-article-sort-functions
10833           (if (not reverse)
10834               article
10835             `(lambda (t1 t2)
10836                (,article t2 t1))))
10837          (buffer-read-only)
10838          (gnus-summary-prepare-hook nil))
10839     ;; We do the sorting by regenerating the threads.
10840     (gnus-summary-prepare)
10841     ;; Hide subthreads if needed.
10842     (gnus-summary-maybe-hide-threads)))
10843
10844 ;; Summary saving commands.
10845
10846 (defun gnus-summary-save-article (&optional n not-saved)
10847   "Save the current article using the default saver function.
10848 If N is a positive number, save the N next articles.
10849 If N is a negative number, save the N previous articles.
10850 If N is nil and any articles have been marked with the process mark,
10851 save those articles instead.
10852 The variable `gnus-default-article-saver' specifies the saver function."
10853   (interactive "P")
10854   (let* ((articles (gnus-summary-work-articles n))
10855          (save-buffer (save-excursion
10856                         (nnheader-set-temp-buffer " *Gnus Save*")))
10857          (num (length articles))
10858          header file)
10859     (dolist (article articles)
10860       (setq header (gnus-summary-article-header article))
10861       (if (not (vectorp header))
10862           ;; This is a pseudo-article.
10863           (if (assq 'name header)
10864               (gnus-copy-file (cdr (assq 'name header)))
10865             (gnus-message 1 "Article %d is unsaveable" article))
10866         ;; This is a real article.
10867         (save-window-excursion
10868           (let ((gnus-display-mime-function nil)
10869                 (gnus-article-prepare-hook nil))
10870             (gnus-summary-select-article t nil nil article)))
10871         (save-excursion
10872           (set-buffer save-buffer)
10873           (erase-buffer)
10874           (insert-buffer-substring gnus-original-article-buffer))
10875         (setq file (gnus-article-save save-buffer file num))
10876         (gnus-summary-remove-process-mark article)
10877         (unless not-saved
10878           (gnus-summary-set-saved-mark article))))
10879     (gnus-kill-buffer save-buffer)
10880     (gnus-summary-position-point)
10881     (gnus-set-mode-line 'summary)
10882     n))
10883
10884 (defun gnus-summary-pipe-output (&optional arg headers)
10885   "Pipe the current article to a subprocess.
10886 If N is a positive number, pipe the N next articles.
10887 If N is a negative number, pipe the N previous articles.
10888 If N is nil and any articles have been marked with the process mark,
10889 pipe those articles instead.
10890 If HEADERS (the symbolic prefix), include the headers, too."
10891   (interactive (gnus-interactive "P\ny"))
10892   (require 'gnus-art)
10893   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10894         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10895     (gnus-summary-save-article arg t))
10896   (let ((buffer (get-buffer "*Shell Command Output*")))
10897     (when (and buffer
10898                (not (zerop (buffer-size buffer))))
10899       (gnus-configure-windows 'pipe))))
10900
10901 (defun gnus-summary-save-article-mail (&optional arg)
10902   "Append the current article to an mail file.
10903 If N is a positive number, save the N next articles.
10904 If N is a negative number, save the N previous articles.
10905 If N is nil and any articles have been marked with the process mark,
10906 save those articles instead."
10907   (interactive "P")
10908   (require 'gnus-art)
10909   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10910     (gnus-summary-save-article arg)))
10911
10912 (defun gnus-summary-save-article-rmail (&optional arg)
10913   "Append the current article to an rmail file.
10914 If N is a positive number, save the N next articles.
10915 If N is a negative number, save the N previous articles.
10916 If N is nil and any articles have been marked with the process mark,
10917 save those articles instead."
10918   (interactive "P")
10919   (require 'gnus-art)
10920   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10921     (gnus-summary-save-article arg)))
10922
10923 (defun gnus-summary-save-article-file (&optional arg)
10924   "Append the current article to a file.
10925 If N is a positive number, save the N next articles.
10926 If N is a negative number, save the N previous articles.
10927 If N is nil and any articles have been marked with the process mark,
10928 save those articles instead."
10929   (interactive "P")
10930   (require 'gnus-art)
10931   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10932     (gnus-summary-save-article arg)))
10933
10934 (defun gnus-summary-write-article-file (&optional arg)
10935   "Write the current article to a file, deleting the previous file.
10936 If N is a positive number, save the N next articles.
10937 If N is a negative number, save the N previous articles.
10938 If N is nil and any articles have been marked with the process mark,
10939 save those articles instead."
10940   (interactive "P")
10941   (require 'gnus-art)
10942   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10943     (gnus-summary-save-article arg)))
10944
10945 (defun gnus-summary-save-article-body-file (&optional arg)
10946   "Append the current article body to a file.
10947 If N is a positive number, save the N next articles.
10948 If N is a negative number, save the N previous articles.
10949 If N is nil and any articles have been marked with the process mark,
10950 save those articles instead."
10951   (interactive "P")
10952   (require 'gnus-art)
10953   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10954     (gnus-summary-save-article arg)))
10955
10956 (defun gnus-summary-muttprint (&optional arg)
10957   "Print the current article using Muttprint.
10958 If N is a positive number, save the N next articles.
10959 If N is a negative number, save the N previous articles.
10960 If N is nil and any articles have been marked with the process mark,
10961 save those articles instead."
10962   (interactive "P")
10963   (require 'gnus-art)
10964   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10965     (gnus-summary-save-article arg t)))
10966
10967 (defun gnus-summary-pipe-message (program)
10968   "Pipe the current article through PROGRAM."
10969   (interactive "sProgram: ")
10970   (gnus-summary-select-article)
10971   (let ((mail-header-separator ""))
10972     (gnus-eval-in-buffer-window gnus-article-buffer
10973       (save-restriction
10974         (widen)
10975         (let ((start (window-start))
10976               buffer-read-only)
10977           (message-pipe-buffer-body program)
10978           (set-window-start (get-buffer-window (current-buffer)) start))))))
10979
10980 (defun gnus-get-split-value (methods)
10981   "Return a value based on the split METHODS."
10982   (let (split-name method result match)
10983     (when methods
10984       (save-excursion
10985         (set-buffer gnus-original-article-buffer)
10986         (save-restriction
10987           (nnheader-narrow-to-headers)
10988           (while (and methods (not split-name))
10989             (goto-char (point-min))
10990             (setq method (pop methods))
10991             (setq match (car method))
10992             (when (cond
10993                    ((stringp match)
10994                     ;; Regular expression.
10995                     (ignore-errors
10996                       (re-search-forward match nil t)))
10997                    ((functionp match)
10998                     ;; Function.
10999                     (save-restriction
11000                       (widen)
11001                       (setq result (funcall match gnus-newsgroup-name))))
11002                    ((consp match)
11003                     ;; Form.
11004                     (save-restriction
11005                       (widen)
11006                       (setq result (eval match)))))
11007               (setq split-name (cdr method))
11008               (cond ((stringp result)
11009                      (push (expand-file-name
11010                             result gnus-article-save-directory)
11011                            split-name))
11012                     ((consp result)
11013                      (setq split-name (append result split-name)))))))))
11014     (nreverse split-name)))
11015
11016 (defun gnus-valid-move-group-p (group)
11017   (and (boundp group)
11018        (symbol-name group)
11019        (symbol-value group)
11020        (gnus-get-function (gnus-find-method-for-group
11021                            (symbol-name group)) 'request-accept-article t)))
11022
11023 (defun gnus-read-move-group-name (prompt default articles prefix)
11024   "Read a group name."
11025   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11026          (minibuffer-confirm-incomplete nil) ; XEmacs
11027          (prom
11028           (format "%s %s to:"
11029                   prompt
11030                   (if (> (length articles) 1)
11031                       (format "these %d articles" (length articles))
11032                     "this article")))
11033          (to-newsgroup
11034           (cond
11035            ((null split-name)
11036             (gnus-completing-read-with-default
11037              default prom
11038              gnus-active-hashtb
11039              'gnus-valid-move-group-p
11040              nil prefix
11041              'gnus-group-history))
11042            ((= 1 (length split-name))
11043             (gnus-completing-read-with-default
11044              (car split-name) prom
11045              gnus-active-hashtb
11046              'gnus-valid-move-group-p
11047              nil nil
11048              'gnus-group-history))
11049            (t
11050             (gnus-completing-read-with-default
11051              nil prom
11052              (mapcar 'list (nreverse split-name))
11053              nil nil nil
11054              'gnus-group-history))))
11055          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11056     (when to-newsgroup
11057       (if (or (string= to-newsgroup "")
11058               (string= to-newsgroup prefix))
11059           (setq to-newsgroup default))
11060       (unless to-newsgroup
11061         (error "No group name entered"))
11062       (or (gnus-active to-newsgroup)
11063           (gnus-activate-group to-newsgroup nil nil to-method)
11064           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11065                                      to-newsgroup))
11066               (or (and (gnus-request-create-group to-newsgroup to-method)
11067                        (gnus-activate-group
11068                         to-newsgroup nil nil to-method)
11069                        (gnus-subscribe-group to-newsgroup))
11070                   (error "Couldn't create group %s" to-newsgroup)))
11071           (error "No such group: %s" to-newsgroup)))
11072     to-newsgroup))
11073
11074 (defun gnus-summary-save-parts (type dir n &optional reverse)
11075   "Save parts matching TYPE to DIR.
11076 If REVERSE, save parts that do not match TYPE."
11077   (interactive
11078    (list (read-string "Save parts of type: "
11079                       (or (car gnus-summary-save-parts-type-history)
11080                           gnus-summary-save-parts-default-mime)
11081                       'gnus-summary-save-parts-type-history)
11082          (setq gnus-summary-save-parts-last-directory
11083                (read-file-name "Save to directory: "
11084                                gnus-summary-save-parts-last-directory
11085                                nil t))
11086          current-prefix-arg))
11087   (gnus-summary-iterate n
11088     (let ((gnus-display-mime-function nil)
11089           (gnus-inhibit-treatment t))
11090       (gnus-summary-select-article))
11091     (save-excursion
11092       (set-buffer gnus-article-buffer)
11093       (let ((handles (or gnus-article-mime-handles
11094                          (mm-dissect-buffer nil gnus-article-loose-mime)
11095                          (and gnus-article-emulate-mime
11096                               (mm-uu-dissect)))))
11097         (when handles
11098           (gnus-summary-save-parts-1 type dir handles reverse)
11099           (unless gnus-article-mime-handles ;; Don't destroy this case.
11100             (mm-destroy-parts handles)))))))
11101
11102 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11103   (if (stringp (car handle))
11104       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11105               (cdr handle))
11106     (when (if reverse
11107               (not (string-match type (mm-handle-media-type handle)))
11108             (string-match type (mm-handle-media-type handle)))
11109       (let ((file (expand-file-name
11110                    (gnus-map-function
11111                     mm-file-name-rewrite-functions
11112                     (file-name-nondirectory
11113                      (or
11114                       (mail-content-type-get
11115                        (mm-handle-disposition handle) 'filename)
11116                       (mail-content-type-get
11117                        (mm-handle-type handle) 'name)
11118                       (concat gnus-newsgroup-name
11119                               "." (number-to-string
11120                                    (cdr gnus-article-current))))))
11121                    dir)))
11122         (unless (file-exists-p file)
11123           (mm-save-part-to-file handle file))))))
11124
11125 ;; Summary extract commands
11126
11127 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11128   (let ((buffer-read-only nil)
11129         (article (gnus-summary-article-number))
11130         after-article b e)
11131     (unless (gnus-summary-goto-subject article)
11132       (error "No such article: %d" article))
11133     (gnus-summary-position-point)
11134     ;; If all commands are to be bunched up on one line, we collect
11135     ;; them here.
11136     (unless gnus-view-pseudos-separately
11137       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11138             files action)
11139         (while ps
11140           (setq action (cdr (assq 'action (car ps))))
11141           (setq files (list (cdr (assq 'name (car ps)))))
11142           (while (and ps (cdr ps)
11143                       (string= (or action "1")
11144                                (or (cdr (assq 'action (cadr ps))) "2")))
11145             (push (cdr (assq 'name (cadr ps))) files)
11146             (setcdr ps (cddr ps)))
11147           (when files
11148             (when (not (string-match "%s" action))
11149               (push " " files))
11150             (push " " files)
11151             (when (assq 'execute (car ps))
11152               (setcdr (assq 'execute (car ps))
11153                       (funcall (if (string-match "%s" action)
11154                                    'format 'concat)
11155                                action
11156                                (mapconcat
11157                                 (lambda (f)
11158                                   (if (equal f " ")
11159                                       f
11160                                     (shell-quote-argument f)))
11161                                 files " ")))))
11162           (setq ps (cdr ps)))))
11163     (if (and gnus-view-pseudos (not not-view))
11164         (while pslist
11165           (when (assq 'execute (car pslist))
11166             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11167                                   (eq gnus-view-pseudos 'not-confirm)))
11168           (setq pslist (cdr pslist)))
11169       (save-excursion
11170         (while pslist
11171           (setq after-article (or (cdr (assq 'article (car pslist)))
11172                                   (gnus-summary-article-number)))
11173           (gnus-summary-goto-subject after-article)
11174           (forward-line 1)
11175           (setq b (point))
11176           (insert "    " (file-name-nondirectory
11177                           (cdr (assq 'name (car pslist))))
11178                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11179           (setq e (point))
11180           (forward-line -1)             ; back to `b'
11181           (gnus-add-text-properties
11182            b (1- e) (list 'gnus-number gnus-reffed-article-number
11183                           gnus-mouse-face-prop gnus-mouse-face))
11184           (gnus-data-enter
11185            after-article gnus-reffed-article-number
11186            gnus-unread-mark b (car pslist) 0 (- e b))
11187           (setq gnus-newsgroup-unreads
11188                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11189                                          gnus-reffed-article-number))
11190           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11191           (setq pslist (cdr pslist)))))))
11192
11193 (defun gnus-pseudos< (p1 p2)
11194   (let ((c1 (cdr (assq 'action p1)))
11195         (c2 (cdr (assq 'action p2))))
11196     (and c1 c2 (string< c1 c2))))
11197
11198 (defun gnus-request-pseudo-article (props)
11199   (cond ((assq 'execute props)
11200          (gnus-execute-command (cdr (assq 'execute props)))))
11201   (let ((gnus-current-article (gnus-summary-article-number)))
11202     (gnus-run-hooks 'gnus-mark-article-hook)))
11203
11204 (defun gnus-execute-command (command &optional automatic)
11205   (save-excursion
11206     (gnus-article-setup-buffer)
11207     (set-buffer gnus-article-buffer)
11208     (setq buffer-read-only nil)
11209     (let ((command (if automatic command
11210                      (read-string "Command: " (cons command 0)))))
11211       (erase-buffer)
11212       (insert "$ " command "\n\n")
11213       (if gnus-view-pseudo-asynchronously
11214           (start-process "gnus-execute" (current-buffer) shell-file-name
11215                          shell-command-switch command)
11216         (call-process shell-file-name nil t nil
11217                       shell-command-switch command)))))
11218
11219 ;; Summary kill commands.
11220
11221 (defun gnus-summary-edit-global-kill (article)
11222   "Edit the \"global\" kill file."
11223   (interactive (list (gnus-summary-article-number)))
11224   (gnus-group-edit-global-kill article))
11225
11226 (defun gnus-summary-edit-local-kill ()
11227   "Edit a local kill file applied to the current newsgroup."
11228   (interactive)
11229   (setq gnus-current-headers (gnus-summary-article-header))
11230   (gnus-group-edit-local-kill
11231    (gnus-summary-article-number) gnus-newsgroup-name))
11232
11233 ;;; Header reading.
11234
11235 (defun gnus-read-header (id &optional header)
11236   "Read the headers of article ID and enter them into the Gnus system."
11237   (let ((group gnus-newsgroup-name)
11238         (gnus-override-method
11239          (or
11240           gnus-override-method
11241           (and (gnus-news-group-p gnus-newsgroup-name)
11242                (car (gnus-refer-article-methods)))))
11243         where)
11244     ;; First we check to see whether the header in question is already
11245     ;; fetched.
11246     (if (stringp id)
11247         ;; This is a Message-ID.
11248         (setq header (or header (gnus-id-to-header id)))
11249       ;; This is an article number.
11250       (setq header (or header (gnus-summary-article-header id))))
11251     (if (and header
11252              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11253         ;; We have found the header.
11254         header
11255       ;; We have to really fetch the header to this article.
11256       (save-excursion
11257         (set-buffer nntp-server-buffer)
11258         (when (setq where (gnus-request-head id group))
11259           (nnheader-fold-continuation-lines)
11260           (goto-char (point-max))
11261           (insert ".\n")
11262           (goto-char (point-min))
11263           (insert "211 ")
11264           (princ (cond
11265                   ((numberp id) id)
11266                   ((cdr where) (cdr where))
11267                   (header (mail-header-number header))
11268                   (t gnus-reffed-article-number))
11269                  (current-buffer))
11270           (insert " Article retrieved.\n"))
11271         (if (or (not where)
11272                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11273             ()                          ; Malformed head.
11274           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11275             (when (and (stringp id)
11276                        (not (string= (gnus-group-real-name group)
11277                                      (car where))))
11278               ;; If we fetched by Message-ID and the article came
11279               ;; from a different group, we fudge some bogus article
11280               ;; numbers for this article.
11281               (mail-header-set-number header gnus-reffed-article-number))
11282             (save-excursion
11283               (set-buffer gnus-summary-buffer)
11284               (decf gnus-reffed-article-number)
11285               (gnus-remove-header (mail-header-number header))
11286               (push header gnus-newsgroup-headers)
11287               (setq gnus-current-headers header)
11288               (push (mail-header-number header) gnus-newsgroup-limit)))
11289           header)))))
11290
11291 (defun gnus-remove-header (number)
11292   "Remove header NUMBER from `gnus-newsgroup-headers'."
11293   (if (and gnus-newsgroup-headers
11294            (= number (mail-header-number (car gnus-newsgroup-headers))))
11295       (pop gnus-newsgroup-headers)
11296     (let ((headers gnus-newsgroup-headers))
11297       (while (and (cdr headers)
11298                   (not (= number (mail-header-number (cadr headers)))))
11299         (pop headers))
11300       (when (cdr headers)
11301         (setcdr headers (cddr headers))))))
11302
11303 ;;;
11304 ;;; summary highlights
11305 ;;;
11306
11307 (defun gnus-highlight-selected-summary ()
11308   "Highlight selected article in summary buffer."
11309   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11310   (when gnus-summary-selected-face
11311     (save-excursion
11312       (let* ((beg (point-at-bol))
11313              (end (point-at-eol))
11314              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11315              (from (if (get-text-property beg gnus-mouse-face-prop)
11316                        beg
11317                      (or (next-single-property-change
11318                           beg gnus-mouse-face-prop nil end)
11319                          beg)))
11320              (to
11321               (if (= from end)
11322                   (- from 2)
11323                 (or (next-single-property-change
11324                      from gnus-mouse-face-prop nil end)
11325                     end))))
11326         ;; If no mouse-face prop on line we will have to = from = end,
11327         ;; so we highlight the entire line instead.
11328         (when (= (+ to 2) from)
11329           (setq from beg)
11330           (setq to end))
11331         (if gnus-newsgroup-selected-overlay
11332             ;; Move old overlay.
11333             (gnus-move-overlay
11334              gnus-newsgroup-selected-overlay from to (current-buffer))
11335           ;; Create new overlay.
11336           (gnus-overlay-put
11337            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11338            'face gnus-summary-selected-face))))))
11339
11340 (defvar gnus-summary-highlight-line-cached nil)
11341 (defvar gnus-summary-highlight-line-trigger nil)
11342
11343 (defun gnus-summary-highlight-line-0 ()
11344   (if (and (eq gnus-summary-highlight-line-trigger
11345                gnus-summary-highlight)
11346            gnus-summary-highlight-line-cached)
11347       gnus-summary-highlight-line-cached
11348     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11349           gnus-summary-highlight-line-cached
11350           (let* ((cond (list 'cond))
11351                  (c cond)
11352                  (list gnus-summary-highlight))
11353             (while list
11354               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11355                               nil))
11356               (setq c (cdr c)
11357                     list (cdr list)))
11358             (gnus-byte-compile (list 'lambda nil cond))))))
11359
11360 (defun gnus-summary-highlight-line ()
11361   "Highlight current line according to `gnus-summary-highlight'."
11362   (let* ((beg (point-at-bol))
11363          (article (or (gnus-summary-article-number) gnus-current-article))
11364          (score (or (cdr (assq article
11365                                gnus-newsgroup-scored))
11366                     gnus-summary-default-score 0))
11367          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11368          (inhibit-read-only t)
11369          (default gnus-summary-default-score)
11370          (default-high gnus-summary-default-high-score)
11371          (default-low gnus-summary-default-low-score)
11372          (uncached (and gnus-summary-use-undownloaded-faces
11373                         (memq article gnus-newsgroup-undownloaded)
11374                         (not (memq article gnus-newsgroup-cached)))))
11375     (let ((face (funcall (gnus-summary-highlight-line-0))))
11376       (unless (eq face (get-text-property beg 'face))
11377         (gnus-put-text-property-excluding-characters-with-faces
11378          beg (point-at-eol) 'face
11379          (setq face (if (boundp face) (symbol-value face) face)))
11380         (when gnus-summary-highlight-line-function
11381           (funcall gnus-summary-highlight-line-function article face))))))
11382
11383 (defun gnus-update-read-articles (group unread &optional compute)
11384   "Update the list of read articles in GROUP.
11385 UNREAD is a sorted list."
11386   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11387         (info (gnus-get-info group))
11388         (prev 1)
11389         read)
11390     (if (or (not info) (not active))
11391         ;; There is no info on this group if it was, in fact,
11392         ;; killed.  Gnus stores no information on killed groups, so
11393         ;; there's nothing to be done.
11394         ;; One could store the information somewhere temporarily,
11395         ;; perhaps...  Hmmm...
11396         ()
11397       ;; Remove any negative articles numbers.
11398       (while (and unread (< (car unread) 0))
11399         (setq unread (cdr unread)))
11400       ;; Remove any expired article numbers
11401       (while (and unread (< (car unread) (car active)))
11402         (setq unread (cdr unread)))
11403       ;; Compute the ranges of read articles by looking at the list of
11404       ;; unread articles.
11405       (while unread
11406         (when (/= (car unread) prev)
11407           (push (if (= prev (1- (car unread))) prev
11408                   (cons prev (1- (car unread))))
11409                 read))
11410         (setq prev (1+ (car unread)))
11411         (setq unread (cdr unread)))
11412       (when (<= prev (cdr active))
11413         (push (cons prev (cdr active)) read))
11414       (setq read (if (> (length read) 1) (nreverse read) read))
11415       (if compute
11416           read
11417         (save-excursion
11418           (let (setmarkundo)
11419             ;; Propagate the read marks to the backend.
11420             (when (gnus-check-backend-function 'request-set-mark group)
11421               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11422                     (add (gnus-remove-from-range read (gnus-info-read info))))
11423                 (when (or add del)
11424                   (unless (gnus-check-group group)
11425                     (error "Can't open server for %s" group))
11426                   (gnus-request-set-mark
11427                    group (delq nil (list (if add (list add 'add '(read)))
11428                                          (if del (list del 'del '(read))))))
11429                   (setq setmarkundo
11430                         `(gnus-request-set-mark
11431                           ,group
11432                           ',(delq nil (list
11433                                        (if del (list del 'add '(read)))
11434                                        (if add (list add 'del '(read))))))))))
11435             (set-buffer gnus-group-buffer)
11436             (gnus-undo-register
11437               `(progn
11438                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11439                  (gnus-info-set-read ',info ',(gnus-info-read info))
11440                  (gnus-get-unread-articles-in-group ',info
11441                                                     (gnus-active ,group))
11442                  (gnus-group-update-group ,group t)
11443                  ,setmarkundo))))
11444         ;; Enter this list into the group info.
11445         (gnus-info-set-read info read)
11446         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11447         (gnus-get-unread-articles-in-group info (gnus-active group))
11448         t))))
11449
11450 (defun gnus-offer-save-summaries ()
11451   "Offer to save all active summary buffers."
11452   (let (buffers)
11453     ;; Go through all buffers and find all summaries.
11454     (dolist (buffer (buffer-list))
11455       (when (and (setq buffer (buffer-name buffer))
11456                  (string-match "Summary" buffer)
11457                  (with-current-buffer buffer
11458                    ;; We check that this is, indeed, a summary buffer.
11459                    (and (eq major-mode 'gnus-summary-mode)
11460                         ;; Also make sure this isn't bogus.
11461                         gnus-newsgroup-prepared
11462                         ;; Also make sure that this isn't a
11463                         ;; dead summary buffer.
11464                         (not gnus-dead-summary-mode))))
11465         (push buffer buffers)))
11466     ;; Go through all these summary buffers and offer to save them.
11467     (when buffers
11468       (save-excursion
11469         (map-y-or-n-p
11470          "Update summary buffer %s? "
11471          (lambda (buf)
11472            (switch-to-buffer buf)
11473            (gnus-summary-exit))
11474          buffers)))))
11475
11476 (defun gnus-summary-setup-default-charset ()
11477   "Setup newsgroup default charset."
11478   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11479       (setq gnus-newsgroup-charset nil)
11480     (let* ((ignored-charsets
11481             (or gnus-newsgroup-ephemeral-ignored-charsets
11482                 (append
11483                  (and gnus-newsgroup-name
11484                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11485                  gnus-newsgroup-ignored-charsets))))
11486       (setq gnus-newsgroup-charset
11487             (or gnus-newsgroup-ephemeral-charset
11488                 (and gnus-newsgroup-name
11489                      (gnus-parameter-charset gnus-newsgroup-name))
11490                 gnus-default-charset))
11491       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11492            ignored-charsets))))
11493
11494 ;;;
11495 ;;; Mime Commands
11496 ;;;
11497
11498 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11499   "Display the current article buffer fully MIME-buttonized.
11500 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11501 treated as multipart/mixed."
11502   (interactive "P")
11503   (require 'gnus-art)
11504   (let ((gnus-unbuttonized-mime-types nil)
11505         (gnus-mime-display-multipart-as-mixed show-all-parts))
11506     (gnus-summary-show-article)))
11507
11508 (defun gnus-summary-repair-multipart (article)
11509   "Add a Content-Type header to a multipart article without one."
11510   (interactive (list (gnus-summary-article-number)))
11511   (gnus-with-article article
11512     (message-narrow-to-head)
11513     (message-remove-header "Mime-Version")
11514     (goto-char (point-max))
11515     (insert "Mime-Version: 1.0\n")
11516     (widen)
11517     (when (search-forward "\n--" nil t)
11518       (let ((separator (buffer-substring (point) (point-at-eol))))
11519         (message-narrow-to-head)
11520         (message-remove-header "Content-Type")
11521         (goto-char (point-max))
11522         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11523                         separator))
11524         (widen))))
11525   (let (gnus-mark-article-hook)
11526     (gnus-summary-select-article t t nil article)))
11527
11528 (defun gnus-summary-toggle-display-buttonized ()
11529   "Toggle the buttonizing of the article buffer."
11530   (interactive)
11531   (require 'gnus-art)
11532   (if (setq gnus-inhibit-mime-unbuttonizing
11533             (not gnus-inhibit-mime-unbuttonizing))
11534       (let ((gnus-unbuttonized-mime-types nil))
11535         (gnus-summary-show-article))
11536     (gnus-summary-show-article)))
11537
11538 ;;;
11539 ;;; Generic summary marking commands
11540 ;;;
11541
11542 (defvar gnus-summary-marking-alist
11543   '((read gnus-del-mark "d")
11544     (unread gnus-unread-mark "u")
11545     (ticked gnus-ticked-mark "!")
11546     (dormant gnus-dormant-mark "?")
11547     (expirable gnus-expirable-mark "e"))
11548   "An alist of names/marks/keystrokes.")
11549
11550 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11551 (defvar gnus-summary-mark-map)
11552
11553 (defun gnus-summary-make-all-marking-commands ()
11554   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11555   (dolist (elem gnus-summary-marking-alist)
11556     (apply 'gnus-summary-make-marking-command elem)))
11557
11558 (defun gnus-summary-make-marking-command (name mark keystroke)
11559   (let ((map (make-sparse-keymap)))
11560     (define-key gnus-summary-generic-mark-map keystroke map)
11561     (dolist (lway `((next "next" next nil "n")
11562                     (next-unread "next unread" next t "N")
11563                     (prev "previous" prev nil "p")
11564                     (prev-unread "previous unread" prev t "P")
11565                     (nomove "" nil nil ,keystroke)))
11566       (let ((func (gnus-summary-make-marking-command-1
11567                    mark (car lway) lway name)))
11568         (setq func (eval func))
11569         (define-key map (nth 4 lway) func)))))
11570
11571 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11572   `(defun ,(intern
11573             (format "gnus-summary-put-mark-as-%s%s"
11574                     name (if (eq way 'nomove)
11575                              ""
11576                            (concat "-" (symbol-name way)))))
11577      (n)
11578      ,(format
11579        "Mark the current article as %s%s.
11580 If N, the prefix, then repeat N times.
11581 If N is negative, move in reverse order.
11582 The difference between N and the actual number of articles marked is
11583 returned."
11584        name (cadr lway))
11585      (interactive "p")
11586      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11587
11588 (defun gnus-summary-generic-mark (n mark move unread)
11589   "Mark N articles with MARK."
11590   (unless (eq major-mode 'gnus-summary-mode)
11591     (error "This command can only be used in the summary buffer"))
11592   (gnus-summary-show-thread)
11593   (let ((nummove
11594          (cond
11595           ((eq move 'next) 1)
11596           ((eq move 'prev) -1)
11597           (t 0))))
11598     (if (zerop nummove)
11599         (setq n 1)
11600       (when (< n 0)
11601         (setq n (abs n)
11602               nummove (* -1 nummove))))
11603     (while (and (> n 0)
11604                 (gnus-summary-mark-article nil mark)
11605                 (zerop (gnus-summary-next-subject nummove unread t)))
11606       (setq n (1- n)))
11607     (when (/= 0 n)
11608       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11609     (gnus-summary-recenter)
11610     (gnus-summary-position-point)
11611     (gnus-set-mode-line 'summary)
11612     n))
11613
11614 (defun gnus-summary-insert-articles (articles)
11615   (when (setq articles
11616               (gnus-sorted-difference articles
11617                                       (mapcar (lambda (h)
11618                                                 (mail-header-number h))
11619                                               gnus-newsgroup-headers)))
11620     (setq gnus-newsgroup-headers
11621           (gnus-merge 'list
11622                       gnus-newsgroup-headers
11623                       (gnus-fetch-headers articles)
11624                       'gnus-article-sort-by-number))
11625     ;; Suppress duplicates?
11626     (when gnus-suppress-duplicates
11627       (gnus-dup-suppress-articles))
11628
11629     ;; We might want to build some more threads first.
11630     (when (and gnus-fetch-old-headers
11631                (eq gnus-headers-retrieved-by 'nov))
11632       (if (eq gnus-fetch-old-headers 'invisible)
11633           (gnus-build-all-threads)
11634         (gnus-build-old-threads)))
11635     ;; Let the Gnus agent mark articles as read.
11636     (when gnus-agent
11637       (gnus-agent-get-undownloaded-list))
11638     ;; Remove list identifiers from subject
11639     (when gnus-list-identifiers
11640       (gnus-summary-remove-list-identifiers))
11641     ;; First and last article in this newsgroup.
11642     (when gnus-newsgroup-headers
11643       (setq gnus-newsgroup-begin
11644             (mail-header-number (car gnus-newsgroup-headers))
11645             gnus-newsgroup-end
11646             (mail-header-number
11647              (gnus-last-element gnus-newsgroup-headers))))
11648     (when gnus-use-scoring
11649       (gnus-possibly-score-headers))))
11650
11651 (defun gnus-summary-insert-old-articles (&optional all)
11652   "Insert all old articles in this group.
11653 If ALL is non-nil, already read articles become readable.
11654 If ALL is a number, fetch this number of articles."
11655   (interactive "P")
11656   (prog1
11657       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11658             older len)
11659         (setq older
11660               ;; Some nntp servers lie about their active range.  When
11661               ;; this happens, the active range can be in the millions.
11662               ;; Use a compressed range to avoid creating a huge list.
11663               (gnus-range-difference (list gnus-newsgroup-active) old))
11664         (setq len (gnus-range-length older))
11665         (cond
11666          ((null older) nil)
11667          ((numberp all)
11668           (if (< all len)
11669               (let ((older-range (nreverse older)))
11670                 (setq older nil)
11671
11672                 (while (> all 0)
11673                   (let* ((r (pop older-range))
11674                          (min (if (numberp r) r (car r)))
11675                          (max (if (numberp r) r (cdr r))))
11676                     (while (and (<= min max)
11677                                 (> all 0))
11678                       (push max older)
11679                       (setq all (1- all)
11680                             max (1- max))))))
11681             (setq older (gnus-uncompress-range older))))
11682          (all
11683           (setq older (gnus-uncompress-range older)))
11684          (t
11685           (when (and (numberp gnus-large-newsgroup)
11686                    (> len gnus-large-newsgroup))
11687               (let* ((cursor-in-echo-area nil)
11688                      (initial (gnus-parameter-large-newsgroup-initial
11689                                gnus-newsgroup-name))
11690                      (input
11691                       (read-string
11692                        (format
11693                         "How many articles from %s (%s %d): "
11694                         (gnus-limit-string
11695                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11696                         (if initial "max" "default")
11697                         len)
11698                        (if initial
11699                            (cons (number-to-string initial)
11700                                  0)))))
11701                 (unless (string-match "^[ \t]*$" input)
11702                   (setq all (string-to-number input))
11703                   (if (< all len)
11704                       (let ((older-range (nreverse older)))
11705                         (setq older nil)
11706
11707                         (while (> all 0)
11708                           (let* ((r (pop older-range))
11709                                  (min (if (numberp r) r (car r)))
11710                                  (max (if (numberp r) r (cdr r))))
11711                             (while (and (<= min max)
11712                                         (> all 0))
11713                               (push max older)
11714                               (setq all (1- all)
11715                                     max (1- max))))))))))
11716           (setq older (gnus-uncompress-range older))))
11717         (if (not older)
11718             (message "No old news.")
11719           (gnus-summary-insert-articles older)
11720           (gnus-summary-limit (gnus-sorted-nunion old older))))
11721     (gnus-summary-position-point)))
11722
11723 (defun gnus-summary-insert-new-articles ()
11724   "Insert all new articles in this group."
11725   (interactive)
11726   (prog1
11727       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11728             (old-active gnus-newsgroup-active)
11729             (nnmail-fetched-sources (list t))
11730             i new)
11731         (setq gnus-newsgroup-active
11732               (gnus-activate-group gnus-newsgroup-name 'scan))
11733         (setq i (cdr gnus-newsgroup-active))
11734         (while (> i (cdr old-active))
11735           (push i new)
11736           (decf i))
11737         (if (not new)
11738             (message "No gnus is bad news")
11739           (gnus-summary-insert-articles new)
11740           (setq gnus-newsgroup-unreads
11741                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11742           (gnus-summary-limit (gnus-sorted-nunion old new))))
11743     (gnus-summary-position-point)))
11744
11745 (gnus-summary-make-all-marking-commands)
11746
11747 (gnus-ems-redefine)
11748
11749 (provide 'gnus-sum)
11750
11751 (run-hooks 'gnus-sum-load-hook)
11752
11753 ;; Local Variables:
11754 ;; coding: iso-8859-1
11755 ;; End:
11756
11757 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11758 ;;; gnus-sum.el ends here