* mm-uu.el (mm-uu-dissect): Allow optional arg.
[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 t, 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 :tag "on" t)
79                  (const some)
80                  (const invisible)
81                  number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-refer-thread-limit 200
85   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
86 If t, fetch all the available old headers."
87   :group 'gnus-thread
88   :type '(choice number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-summary-make-false-root 'adopt
92   "*nil means that Gnus won't gather loose threads.
93 If the root of a thread has expired or been read in a previous
94 session, the information necessary to build a complete thread has been
95 lost.  Instead of having many small sub-threads from this original thread
96 scattered all over the summary buffer, Gnus can gather them.
97
98 If non-nil, Gnus will try to gather all loose sub-threads from an
99 original thread into one large thread.
100
101 If this variable is non-nil, it should be one of `none', `adopt',
102 `dummy' or `empty'.
103
104 If this variable is `none', Gnus will not make a false root, but just
105 present the sub-threads after another.
106 If this variable is `dummy', Gnus will create a dummy root that will
107 have all the sub-threads as children.
108 If this variable is `adopt', Gnus will make one of the \"children\"
109 the parent and mark all the step-children as such.
110 If this variable is `empty', the \"children\" are printed with empty
111 subject fields.  (Or rather, they will be printed with a string
112 given by the `gnus-summary-same-subject' variable.)"
113   :group 'gnus-thread
114   :type '(choice (const :tag "off" nil)
115                  (const none)
116                  (const dummy)
117                  (const adopt)
118                  (const empty)))
119
120 (defcustom gnus-summary-make-false-root-always nil
121   "Always make a false dummy root."
122   :version "21.4"
123   :group 'gnus-thread
124   :type 'boolean)
125
126 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
127   "*A regexp to match subjects to be excluded from loose thread gathering.
128 As loose thread gathering is done on subjects only, that means that
129 there can be many false gatherings performed.  By rooting out certain
130 common subjects, gathering might become saner."
131   :group 'gnus-thread
132   :type 'regexp)
133
134 (defcustom gnus-summary-gather-subject-limit nil
135   "*Maximum length of subject comparisons when gathering loose threads.
136 Use nil to compare full subjects.  Setting this variable to a low
137 number will help gather threads that have been corrupted by
138 newsreaders chopping off subject lines, but it might also mean that
139 unrelated articles that have subject that happen to begin with the
140 same few characters will be incorrectly gathered.
141
142 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
143 comparing subjects."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  (const fuzzy)
147                  (sexp :menu-tag "on" t)))
148
149 (defcustom gnus-simplify-subject-functions nil
150   "List of functions taking a string argument that simplify subjects.
151 The functions are applied recursively.
152
153 Useful functions to put in this list include:
154 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
155 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
156   :group 'gnus-thread
157   :type '(repeat function))
158
159 (defcustom gnus-simplify-ignored-prefixes nil
160   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  regexp))
164
165 (defcustom gnus-build-sparse-threads nil
166   "*If non-nil, fill in the gaps in threads.
167 If `some', only fill in the gaps that are needed to tie loose threads
168 together.  If `more', fill in all leaf nodes that Gnus can find.  If
169 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
170   :group 'gnus-thread
171   :type '(choice (const :tag "off" nil)
172                  (const some)
173                  (const more)
174                  (sexp :menu-tag "all" t)))
175
176 (defcustom gnus-summary-thread-gathering-function
177   'gnus-gather-threads-by-subject
178   "*Function used for gathering loose threads.
179 There are two pre-defined functions: `gnus-gather-threads-by-subject',
180 which only takes Subjects into consideration; and
181 `gnus-gather-threads-by-references', which compared the References
182 headers of the articles to find matches."
183   :group 'gnus-thread
184   :type '(radio (function-item gnus-gather-threads-by-subject)
185                 (function-item gnus-gather-threads-by-references)
186                 (function :tag "other")))
187
188 (defcustom gnus-summary-same-subject ""
189   "*String indicating that the current article has the same subject as the previous.
190 This variable will only be used if the value of
191 `gnus-summary-make-false-root' is `empty'."
192   :group 'gnus-summary-format
193   :type 'string)
194
195 (defcustom gnus-summary-goto-unread t
196   "*If t, many commands will go to the next unread article.
197 This applies to marking commands as well as other commands that
198 \"naturally\" select the next article, like, for instance, `SPC' at
199 the end of an article.
200
201 If nil, the marking commands do NOT go to the next unread article
202 \(they go to the next article instead).  If `never', commands that
203 usually go to the next unread article, will go to the next article,
204 whether it is read or not."
205   :group 'gnus-summary-marks
206   :link '(custom-manual "(gnus)Setting Marks")
207   :type '(choice (const :tag "off" nil)
208                  (const never)
209                  (sexp :menu-tag "on" t)))
210
211 (defcustom gnus-summary-default-score 0
212   "*Default article score level.
213 All scores generated by the score files will be added to this score.
214 If this variable is nil, scoring will be disabled."
215   :group 'gnus-score-default
216   :type '(choice (const :tag "disable")
217                  integer))
218
219 (defcustom gnus-summary-default-high-score 0
220   "*Default threshold for a high scored article.
221 An article will be highlighted as high scored if its score is greater
222 than this score."
223   :version "21.4"
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-default-low-score 0
228   "*Default threshold for a low scored article.
229 An article will be highlighted as low scored if its score is smaller
230 than this score."
231   :version "21.4"
232   :group 'gnus-score-default
233   :type 'integer)
234
235 (defcustom gnus-summary-zcore-fuzz 0
236   "*Fuzziness factor for the zcore in the summary buffer.
237 Articles with scores closer than this to `gnus-summary-default-score'
238 will not be marked."
239   :group 'gnus-summary-format
240   :type 'integer)
241
242 (defcustom gnus-simplify-subject-fuzzy-regexp nil
243   "*Strings to be removed when doing fuzzy matches.
244 This can either be a regular expression or list of regular expressions
245 that will be removed from subject strings if fuzzy subject
246 simplification is selected."
247   :group 'gnus-thread
248   :type '(repeat regexp))
249
250 (defcustom gnus-show-threads t
251   "*If non-nil, display threads in summary mode."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-hide-subtree nil
256   "*If non-nil, hide all threads initially.
257 This can be a predicate specifier which says which threads to hide.
258 If threads are hidden, you have to run the command
259 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
260 to expose hidden threads."
261   :group 'gnus-thread
262   :type '(radio (sexp :format "Non-nil\n"
263                       :match (lambda (widget value)
264                                (not (or (consp value) (functionp value))))
265                       :value t)
266                 (const nil)
267                 (sexp :tag "Predicate specifier" :size 0)))
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), 'unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :version "21.4"
328   :group 'gnus-group-select
329   :type '(choice (const best)
330                  (const unread)
331                  (const first)
332                  (const unseen)
333                  (const unseen-or-unread)))
334
335 (defcustom gnus-auto-select-next t
336   "*If non-nil, offer to go to the next group from the end of the previous.
337 If the value is t and the next newsgroup is empty, Gnus will exit
338 summary mode and go back to group mode.  If the value is neither nil
339 nor t, Gnus will select the following unread newsgroup.  In
340 particular, if the value is the symbol `quietly', the next unread
341 newsgroup will be selected without any confirmation, and if it is
342 `almost-quietly', the next group will be selected without any
343 confirmation if you are located on the last article in the group.
344 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
345 will go to the next group without confirmation."
346   :group 'gnus-summary-maneuvering
347   :type '(choice (const :tag "off" nil)
348                  (const quietly)
349                  (const almost-quietly)
350                  (const slightly-quietly)
351                  (sexp :menu-tag "on" t)))
352
353 (defcustom gnus-auto-select-same nil
354   "*If non-nil, select the next article with the same subject.
355 If there are no more articles with the same subject, go to
356 the first unread article."
357   :group 'gnus-summary-maneuvering
358   :type 'boolean)
359
360 (defcustom gnus-auto-goto-ignores 'unfetched
361   "*Says how to handle unfetched articles when maneuvering.
362
363 This variable can either be the symbols nil (maneuver to any
364 article), `undownloaded' (maneuvering while unplugged ignores articles
365 that have not been fetched), `always-undownloaded' (maneuvering always
366 ignores articles that have not been fetched), `unfetched' (maneuvering
367 ignores articles whose headers have not been fetched).
368
369 NOTE: The list of unfetched articles will always be nil when plugged
370 and, when unplugged, a subset of the undownloaded article list."
371   :version "21.4"
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "None" nil)
374                  (const :tag "Undownloaded when unplugged" undownloaded)
375                  (const :tag "Undownloaded" always-undownloaded)
376                  (const :tag "Unfetched" unfetched)))
377
378 (defcustom gnus-summary-check-current nil
379   "*If non-nil, consider the current article when moving.
380 The \"unread\" movement commands will stay on the same line if the
381 current article is unread."
382   :group 'gnus-summary-maneuvering
383   :type 'boolean)
384
385 (defcustom gnus-auto-center-summary 2
386   "*If non-nil, always center the current summary buffer.
387 In particular, if `vertical' do only vertical recentering.  If non-nil
388 and non-`vertical', do both horizontal and vertical recentering."
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "none" nil)
391                  (const vertical)
392                  (integer :tag "height")
393                  (sexp :menu-tag "both" t)))
394
395 (defvar gnus-auto-center-group t
396   "*If non-nil, always center the group buffer.")
397
398 (defcustom gnus-show-all-headers nil
399   "*If non-nil, don't hide any headers."
400   :group 'gnus-article-hiding
401   :group 'gnus-article-headers
402   :type 'boolean)
403
404 (defcustom gnus-summary-ignore-duplicates nil
405   "*If non-nil, ignore articles with identical Message-ID headers."
406   :group 'gnus-summary
407   :type 'boolean)
408
409 (defcustom gnus-single-article-buffer t
410   "*If non-nil, display all articles in the same buffer.
411 If nil, each group will get its own article buffer."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-break-pages t
416   "*If non-nil, do page breaking on articles.
417 The page delimiter is specified by the `gnus-page-delimiter'
418 variable."
419   :group 'gnus-article-various
420   :type 'boolean)
421
422 (defcustom gnus-move-split-methods nil
423   "*Variable used to suggest where articles are to be moved to.
424 It uses the same syntax as the `gnus-split-methods' variable.
425 However, whereas `gnus-split-methods' specifies file names as targets,
426 this variable specifies group names."
427   :group 'gnus-summary-mail
428   :type '(repeat (choice (list :value (fun) function)
429                          (cons :value ("" "") regexp (repeat string))
430                          (sexp :value nil))))
431
432 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
433   "Function used to compute default prefix for article move/copy/etc prompts.
434 The function should take one argument, a group name, and return a
435 string with the suggested prefix."
436   :group 'gnus-summary-mail
437   :type 'function)
438
439 (defcustom gnus-unread-mark ?           ;Whitespace
440   "*Mark used for unread articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-ticked-mark ?!
445   "*Mark used for ticked articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-dormant-mark ??
450   "*Mark used for dormant articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-del-mark ?r
455   "*Mark used for del'd articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-read-mark ?R
460   "*Mark used for read articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-expirable-mark ?E
465   "*Mark used for expirable articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-killed-mark ?K
470   "*Mark used for killed articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-spam-mark ?$
475   "*Mark used for spam articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-souped-mark ?F
480   "*Mark used for souped articles."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-kill-file-mark ?X
485   "*Mark used for articles killed by kill files."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-low-score-mark ?Y
490   "*Mark used for articles with a low score."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-catchup-mark ?C
495   "*Mark used for articles that are caught up."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-replied-mark ?A
500   "*Mark used for articles that have been replied to."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-forwarded-mark ?F
505   "*Mark used for articles that have been forwarded."
506   :version "21.4"
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-recent-mark ?N
511   "*Mark used for articles that are recent."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-cached-mark ?*
516   "*Mark used for articles that are in the cache."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-saved-mark ?S
521   "*Mark used for articles that have been saved."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unseen-mark ?.
526   "*Mark used for articles that haven't been seen."
527   :version "21.4"
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-no-mark ?               ;Whitespace
532   "*Mark used for articles that have no other secondary mark."
533   :version "21.4"
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-ancient-mark ?O
538   "*Mark used for ancient articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-sparse-mark ?Q
543   "*Mark used for sparsely reffed articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-canceled-mark ?G
548   "*Mark used for canceled articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-duplicate-mark ?M
553   "*Mark used for duplicate articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-undownloaded-mark ?-
558   "*Mark used for articles that weren't downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-downloaded-mark ?+
563   "*Mark used for articles that were downloaded."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-downloadable-mark ?%
568   "*Mark used for articles that are to be downloaded."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-unsendable-mark ?=
573   "*Mark used for articles that won't be sent."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-score-over-mark ?+
578   "*Score mark used for articles with high scores."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-score-below-mark ?-
583   "*Score mark used for articles with low scores."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-empty-thread-mark ?     ;Whitespace
588   "*There is no thread under the article."
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-not-empty-thread-mark ?=
593   "*There is a thread under the article."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-view-pseudo-asynchronously nil
598   "*If non-nil, Gnus will view pseudo-articles asynchronously."
599   :group 'gnus-extract-view
600   :type 'boolean)
601
602 (defcustom gnus-auto-expirable-marks
603   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
604         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
605         gnus-souped-mark gnus-duplicate-mark)
606   "*The list of marks converted into expiration if a group is auto-expirable."
607   :version "21.1"
608   :group 'gnus-summary
609   :type '(repeat character))
610
611 (defcustom gnus-inhibit-user-auto-expire t
612   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
613   :version "21.1"
614   :group 'gnus-summary
615   :type 'boolean)
616
617 (defcustom gnus-view-pseudos nil
618   "*If `automatic', pseudo-articles will be viewed automatically.
619 If `not-confirm', pseudos will be viewed automatically, and the user
620 will not be asked to confirm the command."
621   :group 'gnus-extract-view
622   :type '(choice (const :tag "off" nil)
623                  (const automatic)
624                  (const not-confirm)))
625
626 (defcustom gnus-view-pseudos-separately t
627   "*If non-nil, one pseudo-article will be created for each file to be viewed.
628 If nil, all files that use the same viewing command will be given as a
629 list of parameters to that command."
630   :group 'gnus-extract-view
631   :type 'boolean)
632
633 (defcustom gnus-insert-pseudo-articles t
634   "*If non-nil, insert pseudo-articles when decoding articles."
635   :group 'gnus-extract-view
636   :type 'boolean)
637
638 (defcustom gnus-summary-dummy-line-format
639   "   %(:                             :%) %S\n"
640   "*The format specification for the dummy roots in the summary buffer.
641 It works along the same lines as a normal formatting string,
642 with some simple extensions.
643
644 %S  The subject
645
646 General format specifiers can also be used.
647 See `(gnus)Formatting Variables'."
648   :link '(custom-manual "(gnus)Formatting Variables")
649   :group 'gnus-threading
650   :type 'string)
651
652 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
653   "*The format specification for the summary mode line.
654 It works along the same lines as a normal formatting string,
655 with some simple extensions:
656
657 %G  Group name
658 %p  Unprefixed group name
659 %A  Current article number
660 %z  Current article score
661 %V  Gnus version
662 %U  Number of unread articles in the group
663 %e  Number of unselected articles in the group
664 %Z  A string with unread/unselected article counts
665 %g  Shortish group name
666 %S  Subject of the current article
667 %u  User-defined spec
668 %s  Current score file name
669 %d  Number of dormant articles
670 %r  Number of articles that have been marked as read in this session
671 %E  Number of articles expunged by the score files"
672   :group 'gnus-summary-format
673   :type 'string)
674
675 (defcustom gnus-list-identifiers nil
676   "Regexp that matches list identifiers to be removed from subject.
677 This can also be a list of regexps."
678   :version "21.1"
679   :group 'gnus-summary-format
680   :group 'gnus-article-hiding
681   :type '(choice (const :tag "none" nil)
682                  (regexp :value ".*")
683                  (repeat :value (".*") regexp)))
684
685 (defcustom gnus-summary-mark-below 0
686   "*Mark all articles with a score below this variable as read.
687 This variable is local to each summary buffer and usually set by the
688 score file."
689   :group 'gnus-score-default
690   :type 'integer)
691
692 (defun gnus-widget-reversible-match (widget value)
693   "Ignoring WIDGET, convert VALUE to internal form.
694 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
695   ;; (debug value)
696   (or (symbolp value)
697       (and (listp value)
698            (eq (length value) 2)
699            (eq (nth 0 value) 'not)
700            (symbolp (nth 1 value)))))
701
702 (defun gnus-widget-reversible-to-internal (widget value)
703   "Ignoring WIDGET, convert VALUE to internal form.
704 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
705 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
706   ;; (debug value)
707   (if (atom value)
708       (list value nil)
709     (list (nth 1 value) t)))
710
711 (defun gnus-widget-reversible-to-external (widget value)
712   "Ignoring WIDGET, convert VALUE to external form.
713 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
714 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
715   ;; (debug value)
716   (if (nth 1 value)
717       (list 'not (nth 0 value))
718     (nth 0 value)))
719
720 (define-widget 'gnus-widget-reversible 'group
721   "A `group' that convert values."
722   :match 'gnus-widget-reversible-match
723   :value-to-internal 'gnus-widget-reversible-to-internal
724   :value-to-external 'gnus-widget-reversible-to-external)
725
726 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
727   "*List of functions used for sorting articles in the summary buffer.
728
729 Each function takes two articles and returns non-nil if the first
730 article should be sorted before the other.  If you use more than one
731 function, the primary sort function should be the last.  You should
732 probably always include `gnus-article-sort-by-number' in the list of
733 sorting functions -- preferably first.  Also note that sorting by date
734 is often much slower than sorting by number, and the sorting order is
735 very similar.  (Sorting by date means sorting by the time the message
736 was sent, sorting by number means sorting by arrival time.)
737
738 Each item can also be a list `(not F)' where F is a function;
739 this reverses the sort order.
740
741 Ready-made functions include `gnus-article-sort-by-number',
742 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
743 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
744 and `gnus-article-sort-by-score'.
745
746 When threading is turned on, the variable `gnus-thread-sort-functions'
747 controls how articles are sorted."
748   :group 'gnus-summary-sort
749   :type '(repeat (gnus-widget-reversible
750                   (choice (function-item gnus-article-sort-by-number)
751                           (function-item gnus-article-sort-by-author)
752                           (function-item gnus-article-sort-by-subject)
753                           (function-item gnus-article-sort-by-date)
754                           (function-item gnus-article-sort-by-score)
755                           (function-item gnus-article-sort-by-random)
756                           (function :tag "other"))
757                   (boolean :tag "Reverse order"))))
758
759
760 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
761   "*List of functions used for sorting threads in the summary buffer.
762 By default, threads are sorted by article number.
763
764 Each function takes two threads and returns non-nil if the first
765 thread should be sorted before the other.  If you use more than one
766 function, the primary sort function should be the last.  You should
767 probably always include `gnus-thread-sort-by-number' in the list of
768 sorting functions -- preferably first.  Also note that sorting by date
769 is often much slower than sorting by number, and the sorting order is
770 very similar.  (Sorting by date means sorting by the time the message
771 was sent, sorting by number means sorting by arrival time.)
772
773 Each list item can also be a list `(not F)' where F is a
774 function; this specifies reversed sort order.
775
776 Ready-made functions include `gnus-thread-sort-by-number',
777 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
778 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
779 `gnus-thread-sort-by-most-recent-number',
780 `gnus-thread-sort-by-most-recent-date',
781 `gnus-thread-sort-by-random', and
782 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
783
784 When threading is turned off, the variable
785 `gnus-article-sort-functions' controls how articles are sorted."
786   :group 'gnus-summary-sort
787   :type '(repeat 
788           (gnus-widget-reversible
789            (choice (function-item gnus-thread-sort-by-number)
790                    (function-item gnus-thread-sort-by-author)
791                    (function-item gnus-thread-sort-by-subject)
792                    (function-item gnus-thread-sort-by-date)
793                    (function-item gnus-thread-sort-by-score)
794                    (function-item gnus-thread-sort-by-most-recent-number)
795                    (function-item gnus-thread-sort-by-most-recent-date)
796                    (function-item gnus-thread-sort-by-random)
797                    (function-item gnus-thread-sort-by-total-score)
798                    (function :tag "other"))
799            (boolean :tag "Reverse order"))))
800
801 (defcustom gnus-thread-score-function '+
802   "*Function used for calculating the total score of a thread.
803
804 The function is called with the scores of the article and each
805 subthread and should then return the score of the thread.
806
807 Some functions you can use are `+', `max', or `min'."
808   :group 'gnus-summary-sort
809   :type 'function)
810
811 (defcustom gnus-summary-expunge-below nil
812   "All articles that have a score less than this variable will be expunged.
813 This variable is local to the summary buffers."
814   :group 'gnus-score-default
815   :type '(choice (const :tag "off" nil)
816                  integer))
817
818 (defcustom gnus-thread-expunge-below nil
819   "All threads that have a total score less than this variable will be expunged.
820 See `gnus-thread-score-function' for en explanation of what a
821 \"thread score\" is.
822
823 This variable is local to the summary buffers."
824   :group 'gnus-threading
825   :group 'gnus-score-default
826   :type '(choice (const :tag "off" nil)
827                  integer))
828
829 (defcustom gnus-summary-mode-hook nil
830   "*A hook for Gnus summary mode.
831 This hook is run before any variables are set in the summary buffer."
832   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
833   :group 'gnus-summary-various
834   :type 'hook)
835
836 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
837 (when (featurep 'xemacs)
838   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
839   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
840   (add-hook 'gnus-summary-mode-hook
841             'gnus-xmas-switch-horizontal-scrollbar-off))
842
843 (defcustom gnus-summary-menu-hook nil
844   "*Hook run after the creation of the summary mode menu."
845   :group 'gnus-summary-visual
846   :type 'hook)
847
848 (defcustom gnus-summary-exit-hook nil
849   "*A hook called on exit from the summary buffer.
850 It will be called with point in the group buffer."
851   :group 'gnus-summary-exit
852   :type 'hook)
853
854 (defcustom gnus-summary-prepare-hook nil
855   "*A hook called after the summary buffer has been generated.
856 If you want to modify the summary buffer, you can use this hook."
857   :group 'gnus-summary-various
858   :type 'hook)
859
860 (defcustom gnus-summary-prepared-hook nil
861   "*A hook called as the last thing after the summary buffer has been generated."
862   :group 'gnus-summary-various
863   :type 'hook)
864
865 (defcustom gnus-summary-generate-hook nil
866   "*A hook run just before generating the summary buffer.
867 This hook is commonly used to customize threading variables and the
868 like."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-select-group-hook nil
873   "*A hook called when a newsgroup is selected.
874
875 If you'd like to simplify subjects like the
876 `gnus-summary-next-same-subject' command does, you can use the
877 following hook:
878
879  (add-hook gnus-select-group-hook
880            (lambda ()
881              (mapcar (lambda (header)
882                        (mail-header-set-subject
883                         header
884                         (gnus-simplify-subject
885                          (mail-header-subject header) 're-only)))
886                      gnus-newsgroup-headers)))"
887   :group 'gnus-group-select
888   :type 'hook)
889
890 (defcustom gnus-select-article-hook nil
891   "*A hook called when an article is selected."
892   :group 'gnus-summary-choose
893   :options '(gnus-agent-fetch-selected-article)
894   :type 'hook)
895
896 (defcustom gnus-visual-mark-article-hook
897   (list 'gnus-highlight-selected-summary)
898   "*Hook run after selecting an article in the summary buffer.
899 It is meant to be used for highlighting the article in some way.  It
900 is not run if `gnus-visual' is nil."
901   :group 'gnus-summary-visual
902   :type 'hook)
903
904 (defcustom gnus-parse-headers-hook nil
905   "*A hook called before parsing the headers."
906   :group 'gnus-various
907   :type 'hook)
908
909 (defcustom gnus-exit-group-hook nil
910   "*A hook called when exiting summary mode.
911 This hook is not called from the non-updating exit commands like `Q'."
912   :group 'gnus-various
913   :type 'hook)
914
915 (defcustom gnus-summary-update-hook
916   (list 'gnus-summary-highlight-line)
917   "*A hook called when a summary line is changed.
918 The hook will not be called if `gnus-visual' is nil.
919
920 The default function `gnus-summary-highlight-line' will
921 highlight the line according to the `gnus-summary-highlight'
922 variable."
923   :group 'gnus-summary-visual
924   :type 'hook)
925
926 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
927   "*A hook called when an article is selected for the first time.
928 The hook is intended to mark an article as read (or unread)
929 automatically when it is selected."
930   :group 'gnus-summary-choose
931   :type 'hook)
932
933 (defcustom gnus-group-no-more-groups-hook nil
934   "*A hook run when returning to group mode having no more (unread) groups."
935   :group 'gnus-group-select
936   :type 'hook)
937
938 (defcustom gnus-ps-print-hook nil
939   "*A hook run before ps-printing something from Gnus."
940   :group 'gnus-summary
941   :type 'hook)
942
943 (defcustom gnus-summary-article-move-hook nil
944   "*A hook called after an article is moved, copied, respooled, or crossposted."
945   :group 'gnus-summary
946   :type 'hook)
947
948 (defcustom gnus-summary-article-delete-hook nil
949   "*A hook called after an article is deleted."
950   :group 'gnus-summary
951   :type 'hook)
952
953 (defcustom gnus-summary-article-expire-hook nil
954   "*A hook called after an article is expired."
955   :group 'gnus-summary
956   :type 'hook)
957
958 (defcustom gnus-summary-display-arrow
959   (and (fboundp 'display-graphic-p)
960        (display-graphic-p))
961   "*If non-nil, display an arrow highlighting the current article."
962   :version "21.4"
963   :group 'gnus-summary
964   :type 'boolean)
965
966 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
967   "Face used for highlighting the current article in the summary buffer."
968   :group 'gnus-summary-visual
969   :type 'face)
970
971 (defvar gnus-tmp-downloaded nil)
972
973 (defcustom gnus-summary-highlight
974   '(((eq mark gnus-canceled-mark)
975      . gnus-summary-cancelled-face)
976     ((and uncached (> score default-high))
977      . gnus-summary-high-undownloaded-face)
978     ((and uncached (< score default-low))
979      . gnus-summary-low-undownloaded-face)
980     (uncached
981      . gnus-summary-normal-undownloaded-face)
982     ((and (> score default-high)
983           (or (eq mark gnus-dormant-mark)
984               (eq mark gnus-ticked-mark)))
985      . gnus-summary-high-ticked-face)
986     ((and (< score default-low)
987           (or (eq mark gnus-dormant-mark)
988               (eq mark gnus-ticked-mark)))
989      . gnus-summary-low-ticked-face)
990     ((or (eq mark gnus-dormant-mark)
991          (eq mark gnus-ticked-mark))
992      . gnus-summary-normal-ticked-face)
993     ((and (> score default-high) (eq mark gnus-ancient-mark))
994      . gnus-summary-high-ancient-face)
995     ((and (< score default-low) (eq mark gnus-ancient-mark))
996      . gnus-summary-low-ancient-face)
997     ((eq mark gnus-ancient-mark)
998      . gnus-summary-normal-ancient-face)
999     ((and (> score default-high) (eq mark gnus-unread-mark))
1000      . gnus-summary-high-unread-face)
1001     ((and (< score default-low) (eq mark gnus-unread-mark))
1002      . gnus-summary-low-unread-face)
1003     ((eq mark gnus-unread-mark)
1004      . gnus-summary-normal-unread-face)
1005     ((> score default-high)
1006      . gnus-summary-high-read-face)
1007     ((< score default-low)
1008      . gnus-summary-low-read-face)
1009     (t
1010      . gnus-summary-normal-read-face))
1011   "*Controls the highlighting of summary buffer lines.
1012
1013 A list of (FORM . FACE) pairs.  When deciding how a a particular
1014 summary line should be displayed, each form is evaluated.  The content
1015 of the face field after the first true form is used.  You can change
1016 how those summary lines are displayed, by editing the face field.
1017
1018 You can use the following variables in the FORM field.
1019
1020 score:        The article's score
1021 default:      The default article score.
1022 default-high: The default score for high scored articles.
1023 default-low:  The default score for low scored articles.
1024 below:        The score below which articles are automatically marked as read.
1025 mark:         The article's mark.
1026 uncached:     Non-nil if the article is uncached."
1027   :group 'gnus-summary-visual
1028   :type '(repeat (cons (sexp :tag "Form" nil)
1029                        face)))
1030
1031 (defcustom gnus-alter-header-function nil
1032   "Function called to allow alteration of article header structures.
1033 The function is called with one parameter, the article header vector,
1034 which it may alter in any way."
1035   :type '(choice (const :tag "None" nil)
1036                  function)
1037   :group 'gnus-summary)
1038
1039 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1040   "Variable that says which function should be used to decode a string with encoded words.")
1041
1042 (defcustom gnus-extra-headers '(To Newsgroups)
1043   "*Extra headers to parse."
1044   :version "21.1"
1045   :group 'gnus-summary
1046   :type '(repeat symbol))
1047
1048 (defcustom gnus-ignored-from-addresses
1049   (and user-mail-address (regexp-quote user-mail-address))
1050   "*Regexp of From headers that may be suppressed in favor of To headers."
1051   :version "21.1"
1052   :group 'gnus-summary
1053   :type 'regexp)
1054
1055 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1056   "List of charsets that should be ignored.
1057 When these charsets are used in the \"charset\" parameter, the
1058 default charset will be used instead."
1059   :version "21.1"
1060   :type '(repeat symbol)
1061   :group 'gnus-charset)
1062
1063 (gnus-define-group-parameter
1064  ignored-charsets
1065  :type list
1066  :function-document
1067  "Return the ignored charsets of GROUP."
1068  :variable gnus-group-ignored-charsets-alist
1069  :variable-default
1070  '(("alt\\.chinese\\.text" iso-8859-1))
1071  :variable-document
1072  "Alist of regexps (to match group names) and charsets that should be ignored.
1073 When these charsets are used in the \"charset\" parameter, the
1074 default charset will be used instead."
1075  :variable-group gnus-charset
1076  :variable-type '(repeat (cons (regexp :tag "Group")
1077                                (repeat symbol)))
1078  :parameter-type '(choice :tag "Ignored charsets"
1079                           :value nil
1080                           (repeat (symbol)))
1081  :parameter-document       "\
1082 List of charsets that should be ignored.
1083
1084 When these charsets are used in the \"charset\" parameter, the
1085 default charset will be used instead.")
1086
1087 (defcustom gnus-group-highlight-words-alist nil
1088   "Alist of group regexps and highlight regexps.
1089 This variable uses the same syntax as `gnus-emphasis-alist'."
1090   :version "21.1"
1091   :type '(repeat (cons (regexp :tag "Group")
1092                        (repeat (list (regexp :tag "Highlight regexp")
1093                                      (number :tag "Group for entire word" 0)
1094                                      (number :tag "Group for displayed part" 0)
1095                                      (symbol :tag "Face"
1096                                              gnus-emphasis-highlight-words)))))
1097   :group 'gnus-summary-visual)
1098
1099 (defcustom gnus-summary-show-article-charset-alist
1100   nil
1101   "Alist of number and charset.
1102 The article will be shown with the charset corresponding to the
1103 numbered argument.
1104 For example: ((1 . cn-gb-2312) (2 . big5))."
1105   :version "21.1"
1106   :type '(repeat (cons (number :tag "Argument" 1)
1107                        (symbol :tag "Charset")))
1108   :group 'gnus-charset)
1109
1110 (defcustom gnus-preserve-marks t
1111   "Whether marks are preserved when moving, copying and respooling messages."
1112   :version "21.1"
1113   :type 'boolean
1114   :group 'gnus-summary-marks)
1115
1116 (defcustom gnus-alter-articles-to-read-function nil
1117   "Function to be called to alter the list of articles to be selected."
1118   :type '(choice (const nil) function)
1119   :group 'gnus-summary)
1120
1121 (defcustom gnus-orphan-score nil
1122   "*All orphans get this score added.  Set in the score file."
1123   :group 'gnus-score-default
1124   :type '(choice (const nil)
1125                  integer))
1126
1127 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1128   "*A regexp to match MIME parts when saving multiple parts of a
1129 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1130 This regexp will be used by default when prompting the user for which
1131 type of files to save."
1132   :group 'gnus-summary
1133   :type 'regexp)
1134
1135 (defcustom gnus-read-all-available-headers nil
1136   "Whether Gnus should parse all headers made available to it.
1137 This is mostly relevant for slow back ends where the user may
1138 wish to widen the summary buffer to include all headers
1139 that were fetched.  Say, for nnultimate groups."
1140   :version "21.4"
1141   :group 'gnus-summary
1142   :type '(choice boolean regexp))
1143
1144 (defcustom gnus-summary-muttprint-program "muttprint"
1145   "Command (and optional arguments) used to run Muttprint."
1146   :version "21.4"
1147   :group 'gnus-summary
1148   :type 'string)
1149
1150 (defcustom gnus-article-loose-mime t
1151   "If non-nil, don't require MIME-Version header.
1152 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1153 supply the MIME-Version header or deliberately strip it from the mail.
1154 If non-nil (the default), Gnus will treat some articles as MIME
1155 even if the MIME-Version header is missing."
1156   :version "21.4"
1157   :type 'boolean
1158   :group 'gnus-article-mime)
1159
1160 (defcustom gnus-article-emulate-mime t
1161   "If non-nil, use MIME emulation for uuencode and the like.
1162 This means that Gnus will search message bodies for text that look
1163 like uuencoded bits, yEncoded bits, and so on, and present that using
1164 the normal Gnus MIME machinery."
1165   :version "21.4"
1166   :type 'boolean
1167   :group 'gnus-article-mime)
1168
1169 ;;; Internal variables
1170
1171 (defvar gnus-summary-display-cache nil)
1172 (defvar gnus-article-mime-handles nil)
1173 (defvar gnus-article-decoded-p nil)
1174 (defvar gnus-article-charset nil)
1175 (defvar gnus-article-ignored-charsets nil)
1176 (defvar gnus-scores-exclude-files nil)
1177 (defvar gnus-page-broken nil)
1178
1179 (defvar gnus-original-article nil)
1180 (defvar gnus-article-internal-prepare-hook nil)
1181 (defvar gnus-newsgroup-process-stack nil)
1182
1183 (defvar gnus-thread-indent-array nil)
1184 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1185 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1186   "Function called to sort the articles within a thread after it has been gathered together.")
1187
1188 (defvar gnus-summary-save-parts-type-history nil)
1189 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1190
1191 ;; Avoid highlighting in kill files.
1192 (defvar gnus-summary-inhibit-highlight nil)
1193 (defvar gnus-newsgroup-selected-overlay nil)
1194 (defvar gnus-inhibit-limiting nil)
1195 (defvar gnus-newsgroup-adaptive-score-file nil)
1196 (defvar gnus-current-score-file nil)
1197 (defvar gnus-current-move-group nil)
1198 (defvar gnus-current-copy-group nil)
1199 (defvar gnus-current-crosspost-group nil)
1200 (defvar gnus-newsgroup-display nil)
1201
1202 (defvar gnus-newsgroup-dependencies nil)
1203 (defvar gnus-newsgroup-adaptive nil)
1204 (defvar gnus-summary-display-article-function nil)
1205 (defvar gnus-summary-highlight-line-function nil
1206   "Function called after highlighting a summary line.")
1207
1208 (defvar gnus-summary-line-format-alist
1209   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1210     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1211     (?s gnus-tmp-subject-or-nil ?s)
1212     (?n gnus-tmp-name ?s)
1213     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1214         ?s)
1215     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1216             gnus-tmp-from) ?s)
1217     (?F gnus-tmp-from ?s)
1218     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1219     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1220     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1221     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1222     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1223     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1224     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1225     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1226     (?L gnus-tmp-lines ?s)
1227     (?O gnus-tmp-downloaded ?c)
1228     (?I gnus-tmp-indentation ?s)
1229     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1230     (?R gnus-tmp-replied ?c)
1231     (?\[ gnus-tmp-opening-bracket ?c)
1232     (?\] gnus-tmp-closing-bracket ?c)
1233     (?\> (make-string gnus-tmp-level ? ) ?s)
1234     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1235     (?i gnus-tmp-score ?d)
1236     (?z gnus-tmp-score-char ?c)
1237     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1238     (?U gnus-tmp-unread ?c)
1239     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1240         ?s)
1241     (?t (gnus-summary-number-of-articles-in-thread
1242          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1243         ?d)
1244     (?e (gnus-summary-number-of-articles-in-thread
1245          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1246         ?c)
1247     (?u gnus-tmp-user-defined ?s)
1248     (?P (gnus-pick-line-number) ?d)
1249     (?B gnus-tmp-thread-tree-header-string ?s)
1250     (user-date (gnus-user-date
1251                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1252   "An alist of format specifications that can appear in summary lines.
1253 These are paired with what variables they correspond with, along with
1254 the type of the variable (string, integer, character, etc).")
1255
1256 (defvar gnus-summary-dummy-line-format-alist
1257   `((?S gnus-tmp-subject ?s)
1258     (?N gnus-tmp-number ?d)
1259     (?u gnus-tmp-user-defined ?s)))
1260
1261 (defvar gnus-summary-mode-line-format-alist
1262   `((?G gnus-tmp-group-name ?s)
1263     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1264     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1265     (?A gnus-tmp-article-number ?d)
1266     (?Z gnus-tmp-unread-and-unselected ?s)
1267     (?V gnus-version ?s)
1268     (?U gnus-tmp-unread-and-unticked ?d)
1269     (?S gnus-tmp-subject ?s)
1270     (?e gnus-tmp-unselected ?d)
1271     (?u gnus-tmp-user-defined ?s)
1272     (?d (length gnus-newsgroup-dormant) ?d)
1273     (?t (length gnus-newsgroup-marked) ?d)
1274     (?h (length gnus-newsgroup-spam-marked) ?d)
1275     (?r (length gnus-newsgroup-reads) ?d)
1276     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1277     (?E gnus-newsgroup-expunged-tally ?d)
1278     (?s (gnus-current-score-file-nondirectory) ?s)))
1279
1280 (defvar gnus-last-search-regexp nil
1281   "Default regexp for article search command.")
1282
1283 (defvar gnus-last-shell-command nil
1284   "Default shell command on article.")
1285
1286 (defvar gnus-newsgroup-agentized nil
1287   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1288 (defvar gnus-newsgroup-begin nil)
1289 (defvar gnus-newsgroup-end nil)
1290 (defvar gnus-newsgroup-last-rmail nil)
1291 (defvar gnus-newsgroup-last-mail nil)
1292 (defvar gnus-newsgroup-last-folder nil)
1293 (defvar gnus-newsgroup-last-file nil)
1294 (defvar gnus-newsgroup-auto-expire nil)
1295 (defvar gnus-newsgroup-active nil)
1296
1297 (defvar gnus-newsgroup-data nil)
1298 (defvar gnus-newsgroup-data-reverse nil)
1299 (defvar gnus-newsgroup-limit nil)
1300 (defvar gnus-newsgroup-limits nil)
1301 (defvar gnus-summary-use-undownloaded-faces nil)
1302
1303 (defvar gnus-newsgroup-unreads nil
1304   "Sorted list of unread articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-unselected nil
1307   "Sorted list of unselected unread articles in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-reads nil
1310   "Alist of read articles and article marks in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-expunged-tally nil)
1313
1314 (defvar gnus-newsgroup-marked nil
1315   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1316
1317 (defvar gnus-newsgroup-spam-marked nil
1318   "List of ranges of articles that have been marked as spam.")
1319
1320 (defvar gnus-newsgroup-killed nil
1321   "List of ranges of articles that have been through the scoring process.")
1322
1323 (defvar gnus-newsgroup-cached nil
1324   "Sorted list of articles that come from the article cache.")
1325
1326 (defvar gnus-newsgroup-saved nil
1327   "List of articles that have been saved.")
1328
1329 (defvar gnus-newsgroup-kill-headers nil)
1330
1331 (defvar gnus-newsgroup-replied nil
1332   "List of articles that have been replied to in the current newsgroup.")
1333
1334 (defvar gnus-newsgroup-forwarded nil
1335   "List of articles that have been forwarded in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-recent nil
1338   "List of articles that have are recent in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-expirable nil
1341   "Sorted list of articles in the current newsgroup that can be expired.")
1342
1343 (defvar gnus-newsgroup-processable nil
1344   "List of articles in the current newsgroup that can be processed.")
1345
1346 (defvar gnus-newsgroup-downloadable nil
1347   "Sorted list of articles in the current newsgroup that can be processed.")
1348
1349 (defvar gnus-newsgroup-unfetched nil
1350   "Sorted list of articles in the current newsgroup whose headers have
1351 not been fetched into the agent.
1352
1353 This list will always be a subset of gnus-newsgroup-undownloaded.")
1354
1355 (defvar gnus-newsgroup-undownloaded nil
1356   "List of articles in the current newsgroup that haven't been downloaded.")
1357
1358 (defvar gnus-newsgroup-unsendable nil
1359   "List of articles in the current newsgroup that won't be sent.")
1360
1361 (defvar gnus-newsgroup-bookmarks nil
1362   "List of articles in the current newsgroup that have bookmarks.")
1363
1364 (defvar gnus-newsgroup-dormant nil
1365   "Sorted list of dormant articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-unseen nil
1368   "List of unseen articles in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-seen nil
1371   "Range of seen articles in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-articles nil
1374   "List of articles in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-scored nil
1377   "List of scored articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-headers nil
1380   "List of article headers in the current newsgroup.")
1381
1382 (defvar gnus-newsgroup-threads nil)
1383
1384 (defvar gnus-newsgroup-prepared nil
1385   "Whether the current group has been prepared properly.")
1386
1387 (defvar gnus-newsgroup-ancient nil
1388   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1389
1390 (defvar gnus-newsgroup-sparse nil)
1391
1392 (defvar gnus-current-article nil)
1393 (defvar gnus-article-current nil)
1394 (defvar gnus-current-headers nil)
1395 (defvar gnus-have-all-headers nil)
1396 (defvar gnus-last-article nil)
1397 (defvar gnus-newsgroup-history nil)
1398 (defvar gnus-newsgroup-charset nil)
1399 (defvar gnus-newsgroup-ephemeral-charset nil)
1400 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1401
1402 (defvar gnus-article-before-search nil)
1403
1404 (defvar gnus-summary-local-variables
1405   '(gnus-newsgroup-name
1406     gnus-newsgroup-begin gnus-newsgroup-end
1407     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1408     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1409     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1410     gnus-newsgroup-unselected gnus-newsgroup-marked
1411     gnus-newsgroup-spam-marked
1412     gnus-newsgroup-reads gnus-newsgroup-saved
1413     gnus-newsgroup-replied gnus-newsgroup-forwarded
1414     gnus-newsgroup-recent
1415     gnus-newsgroup-expirable
1416     gnus-newsgroup-processable gnus-newsgroup-killed
1417     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1418     gnus-newsgroup-unfetched
1419     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1420     gnus-newsgroup-seen gnus-newsgroup-articles
1421     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1422     gnus-newsgroup-headers gnus-newsgroup-threads
1423     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1424     gnus-current-article gnus-current-headers gnus-have-all-headers
1425     gnus-last-article gnus-article-internal-prepare-hook
1426     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1427     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1428     gnus-thread-expunge-below
1429     gnus-score-alist gnus-current-score-file
1430     (gnus-summary-expunge-below . global)
1431     (gnus-summary-mark-below . global)
1432     (gnus-orphan-score . global)
1433     gnus-newsgroup-active gnus-scores-exclude-files
1434     gnus-newsgroup-history gnus-newsgroup-ancient
1435     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1436     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1437     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1438     (gnus-newsgroup-expunged-tally . 0)
1439     gnus-cache-removable-articles gnus-newsgroup-cached
1440     gnus-newsgroup-data gnus-newsgroup-data-reverse
1441     gnus-newsgroup-limit gnus-newsgroup-limits
1442     gnus-newsgroup-charset gnus-newsgroup-display
1443     gnus-summary-use-undownloaded-faces)
1444   "Variables that are buffer-local to the summary buffers.")
1445
1446 (defvar gnus-newsgroup-variables nil
1447   "A list of variables that have separate values in different newsgroups.
1448 A list of newsgroup (summary buffer) local variables, or cons of
1449 variables and their default expressions to be evalled (when the default
1450 values are not nil), that should be made global while the summary buffer
1451 is active.
1452
1453 Note: The default expressions will be evaluated (using function `eval')
1454 before assignment to the local variable rather than just assigned to it.
1455 If the default expression is the symbol `global', that symbol will not
1456 be evaluated but the global value of the local variable will be used
1457 instead.
1458
1459 These variables can be used to set variables in the group parameters
1460 while still allowing them to affect operations done in other buffers.
1461 For example:
1462
1463 \(setq gnus-newsgroup-variables
1464      '(message-use-followup-to
1465        (gnus-visible-headers .
1466          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1467 ")
1468
1469 ;; Byte-compiler warning.
1470 (eval-when-compile
1471   ;; Bind features so that require will believe that gnus-sum has
1472   ;; already been loaded (avoids infinite recursion)
1473   (let ((features (cons 'gnus-sum features)))
1474     ;; Several of the declarations in gnus-sum are needed to load the
1475     ;; following files. Right now, these definitions have been
1476     ;; compiled but not defined (evaluated).  We could either do a
1477     ;; eval-and-compile about all of the declarations or evaluate the
1478     ;; source file.
1479     (if (boundp 'gnus-newsgroup-variables)
1480         nil
1481       (load "gnus-sum.el" t t t))
1482     (require 'gnus)
1483     (require 'gnus-art)))
1484
1485 ;; MIME stuff.
1486
1487 (defvar gnus-decode-encoded-word-methods
1488   '(mail-decode-encoded-word-string)
1489   "List of methods used to decode encoded words.
1490
1491 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1492 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1493 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1494 whose names match REGEXP.
1495
1496 For example:
1497 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1498  mail-decode-encoded-word-string
1499  (\"chinese\" . rfc1843-decode-string))")
1500
1501 (defvar gnus-decode-encoded-word-methods-cache nil)
1502
1503 (defun gnus-multi-decode-encoded-word-string (string)
1504   "Apply the functions from `gnus-encoded-word-methods' that match."
1505   (unless (and gnus-decode-encoded-word-methods-cache
1506                (eq gnus-newsgroup-name
1507                    (car gnus-decode-encoded-word-methods-cache)))
1508     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1509     (mapcar (lambda (x)
1510               (if (symbolp x)
1511                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1512                 (if (and gnus-newsgroup-name
1513                          (string-match (car x) gnus-newsgroup-name))
1514                     (nconc gnus-decode-encoded-word-methods-cache
1515                            (list (cdr x))))))
1516             gnus-decode-encoded-word-methods))
1517   (let ((xlist gnus-decode-encoded-word-methods-cache))
1518     (pop xlist)
1519     (while xlist
1520       (setq string (funcall (pop xlist) string))))
1521   string)
1522
1523 ;; Subject simplification.
1524
1525 (defun gnus-simplify-whitespace (str)
1526   "Remove excessive whitespace from STR."
1527   ;; Multiple spaces.
1528   (while (string-match "[ \t][ \t]+" str)
1529     (setq str (concat (substring str 0 (match-beginning 0))
1530                         " "
1531                         (substring str (match-end 0)))))
1532   ;; Leading spaces.
1533   (when (string-match "^[ \t]+" str)
1534     (setq str (substring str (match-end 0))))
1535   ;; Trailing spaces.
1536   (when (string-match "[ \t]+$" str)
1537     (setq str (substring str 0 (match-beginning 0))))
1538   str)
1539
1540 (defun gnus-simplify-all-whitespace (str)
1541   "Remove all whitespace from STR."
1542   (while (string-match "[ \t\n]+" str)
1543     (setq str (replace-match "" nil nil str)))
1544   str)
1545
1546 (defsubst gnus-simplify-subject-re (subject)
1547   "Remove \"Re:\" from subject lines."
1548   (if (string-match message-subject-re-regexp subject)
1549       (substring subject (match-end 0))
1550     subject))
1551
1552 (defun gnus-simplify-subject (subject &optional re-only)
1553   "Remove `Re:' and words in parentheses.
1554 If RE-ONLY is non-nil, strip leading `Re:'s only."
1555   (let ((case-fold-search t))           ;Ignore case.
1556     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1557     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1558       (setq subject (substring subject (match-end 0))))
1559     ;; Remove uninteresting prefixes.
1560     (when (and (not re-only)
1561                gnus-simplify-ignored-prefixes
1562                (string-match gnus-simplify-ignored-prefixes subject))
1563       (setq subject (substring subject (match-end 0))))
1564     ;; Remove words in parentheses from end.
1565     (unless re-only
1566       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1567         (setq subject (substring subject 0 (match-beginning 0)))))
1568     ;; Return subject string.
1569     subject))
1570
1571 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1572 ;; all whitespace.
1573 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1574   (goto-char (point-min))
1575   (while (re-search-forward regexp nil t)
1576     (replace-match (or newtext ""))))
1577
1578 (defun gnus-simplify-buffer-fuzzy ()
1579   "Simplify string in the buffer fuzzily.
1580 The string in the accessible portion of the current buffer is simplified.
1581 It is assumed to be a single-line subject.
1582 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1583 matter is removed.  Additional things can be deleted by setting
1584 `gnus-simplify-subject-fuzzy-regexp'."
1585   (let ((case-fold-search t)
1586         (modified-tick))
1587     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1588
1589     (while (not (eq modified-tick (buffer-modified-tick)))
1590       (setq modified-tick (buffer-modified-tick))
1591       (cond
1592        ((listp gnus-simplify-subject-fuzzy-regexp)
1593         (mapcar 'gnus-simplify-buffer-fuzzy-step
1594                 gnus-simplify-subject-fuzzy-regexp))
1595        (gnus-simplify-subject-fuzzy-regexp
1596         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1597       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1598       (gnus-simplify-buffer-fuzzy-step
1599        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1600       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1601
1602     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1603     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1604     (gnus-simplify-buffer-fuzzy-step " $")
1605     (gnus-simplify-buffer-fuzzy-step "^ +")))
1606
1607 (defun gnus-simplify-subject-fuzzy (subject)
1608   "Simplify a subject string fuzzily.
1609 See `gnus-simplify-buffer-fuzzy' for details."
1610   (save-excursion
1611     (gnus-set-work-buffer)
1612     (let ((case-fold-search t))
1613       ;; Remove uninteresting prefixes.
1614       (when (and gnus-simplify-ignored-prefixes
1615                  (string-match gnus-simplify-ignored-prefixes subject))
1616         (setq subject (substring subject (match-end 0))))
1617       (insert subject)
1618       (inline (gnus-simplify-buffer-fuzzy))
1619       (buffer-string))))
1620
1621 (defsubst gnus-simplify-subject-fully (subject)
1622   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1623   (cond
1624    (gnus-simplify-subject-functions
1625     (gnus-map-function gnus-simplify-subject-functions subject))
1626    ((null gnus-summary-gather-subject-limit)
1627     (gnus-simplify-subject-re subject))
1628    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1629     (gnus-simplify-subject-fuzzy subject))
1630    ((numberp gnus-summary-gather-subject-limit)
1631     (gnus-limit-string (gnus-simplify-subject-re subject)
1632                        gnus-summary-gather-subject-limit))
1633    (t
1634     subject)))
1635
1636 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1637   "Check whether two subjects are equal.
1638 If optional argument SIMPLE-FIRST is t, first argument is already
1639 simplified."
1640   (cond
1641    ((null simple-first)
1642     (equal (gnus-simplify-subject-fully s1)
1643            (gnus-simplify-subject-fully s2)))
1644    (t
1645     (equal s1
1646            (gnus-simplify-subject-fully s2)))))
1647
1648 (defun gnus-summary-bubble-group ()
1649   "Increase the score of the current group.
1650 This is a handy function to add to `gnus-summary-exit-hook' to
1651 increase the score of each group you read."
1652   (gnus-group-add-score gnus-newsgroup-name))
1653
1654 \f
1655 ;;;
1656 ;;; Gnus summary mode
1657 ;;;
1658
1659 (put 'gnus-summary-mode 'mode-class 'special)
1660
1661 (defvar gnus-article-commands-menu)
1662
1663 ;; Non-orthogonal keys
1664
1665 (gnus-define-keys gnus-summary-mode-map
1666   " " gnus-summary-next-page
1667   "\177" gnus-summary-prev-page
1668   [delete] gnus-summary-prev-page
1669   [backspace] gnus-summary-prev-page
1670   "\r" gnus-summary-scroll-up
1671   "\M-\r" gnus-summary-scroll-down
1672   "n" gnus-summary-next-unread-article
1673   "p" gnus-summary-prev-unread-article
1674   "N" gnus-summary-next-article
1675   "P" gnus-summary-prev-article
1676   "\M-\C-n" gnus-summary-next-same-subject
1677   "\M-\C-p" gnus-summary-prev-same-subject
1678   "\M-n" gnus-summary-next-unread-subject
1679   "\M-p" gnus-summary-prev-unread-subject
1680   "." gnus-summary-first-unread-article
1681   "," gnus-summary-best-unread-article
1682   "\M-s" gnus-summary-search-article-forward
1683   "\M-r" gnus-summary-search-article-backward
1684   "<" gnus-summary-beginning-of-article
1685   ">" gnus-summary-end-of-article
1686   "j" gnus-summary-goto-article
1687   "^" gnus-summary-refer-parent-article
1688   "\M-^" gnus-summary-refer-article
1689   "u" gnus-summary-tick-article-forward
1690   "!" gnus-summary-tick-article-forward
1691   "U" gnus-summary-tick-article-backward
1692   "d" gnus-summary-mark-as-read-forward
1693   "D" gnus-summary-mark-as-read-backward
1694   "E" gnus-summary-mark-as-expirable
1695   "\M-u" gnus-summary-clear-mark-forward
1696   "\M-U" gnus-summary-clear-mark-backward
1697   "k" gnus-summary-kill-same-subject-and-select
1698   "\C-k" gnus-summary-kill-same-subject
1699   "\M-\C-k" gnus-summary-kill-thread
1700   "\M-\C-l" gnus-summary-lower-thread
1701   "e" gnus-summary-edit-article
1702   "#" gnus-summary-mark-as-processable
1703   "\M-#" gnus-summary-unmark-as-processable
1704   "\M-\C-t" gnus-summary-toggle-threads
1705   "\M-\C-s" gnus-summary-show-thread
1706   "\M-\C-h" gnus-summary-hide-thread
1707   "\M-\C-f" gnus-summary-next-thread
1708   "\M-\C-b" gnus-summary-prev-thread
1709   [(meta down)] gnus-summary-next-thread
1710   [(meta up)] gnus-summary-prev-thread
1711   "\M-\C-u" gnus-summary-up-thread
1712   "\M-\C-d" gnus-summary-down-thread
1713   "&" gnus-summary-execute-command
1714   "c" gnus-summary-catchup-and-exit
1715   "\C-w" gnus-summary-mark-region-as-read
1716   "\C-t" gnus-summary-toggle-truncation
1717   "?" gnus-summary-mark-as-dormant
1718   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1719   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1720   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1721   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1722   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1723   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1724   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1725   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1726   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1727   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1728   "=" gnus-summary-expand-window
1729   "\C-x\C-s" gnus-summary-reselect-current-group
1730   "\M-g" gnus-summary-rescan-group
1731   "w" gnus-summary-stop-page-breaking
1732   "\C-c\C-r" gnus-summary-caesar-message
1733   "f" gnus-summary-followup
1734   "F" gnus-summary-followup-with-original
1735   "C" gnus-summary-cancel-article
1736   "r" gnus-summary-reply
1737   "R" gnus-summary-reply-with-original
1738   "\C-c\C-f" gnus-summary-mail-forward
1739   "o" gnus-summary-save-article
1740   "\C-o" gnus-summary-save-article-mail
1741   "|" gnus-summary-pipe-output
1742   "\M-k" gnus-summary-edit-local-kill
1743   "\M-K" gnus-summary-edit-global-kill
1744   ;; "V" gnus-version
1745   "\C-c\C-d" gnus-summary-describe-group
1746   "q" gnus-summary-exit
1747   "Q" gnus-summary-exit-no-update
1748   "\C-c\C-i" gnus-info-find-node
1749   gnus-mouse-2 gnus-mouse-pick-article
1750   "m" gnus-summary-mail-other-window
1751   "a" gnus-summary-post-news
1752   "i" gnus-summary-news-other-window
1753   "x" gnus-summary-limit-to-unread
1754   "s" gnus-summary-isearch-article
1755   "t" gnus-summary-toggle-header
1756   "g" gnus-summary-show-article
1757   "l" gnus-summary-goto-last-article
1758   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1759   "\C-d" gnus-summary-enter-digest-group
1760   "\M-\C-d" gnus-summary-read-document
1761   "\M-\C-e" gnus-summary-edit-parameters
1762   "\M-\C-a" gnus-summary-customize-parameters
1763   "\C-c\C-b" gnus-bug
1764   "*" gnus-cache-enter-article
1765   "\M-*" gnus-cache-remove-article
1766   "\M-&" gnus-summary-universal-argument
1767   "\C-l" gnus-recenter
1768   "I" gnus-summary-increase-score
1769   "L" gnus-summary-lower-score
1770   "\M-i" gnus-symbolic-argument
1771   "h" gnus-summary-select-article-buffer
1772
1773   "b" gnus-article-view-part
1774   "\M-t" gnus-summary-toggle-display-buttonized
1775
1776   "V" gnus-summary-score-map
1777   "X" gnus-uu-extract-map
1778   "S" gnus-summary-send-map)
1779
1780 ;; Sort of orthogonal keymap
1781 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1782   "t" gnus-summary-tick-article-forward
1783   "!" gnus-summary-tick-article-forward
1784   "d" gnus-summary-mark-as-read-forward
1785   "r" gnus-summary-mark-as-read-forward
1786   "c" gnus-summary-clear-mark-forward
1787   " " gnus-summary-clear-mark-forward
1788   "e" gnus-summary-mark-as-expirable
1789   "x" gnus-summary-mark-as-expirable
1790   "?" gnus-summary-mark-as-dormant
1791   "b" gnus-summary-set-bookmark
1792   "B" gnus-summary-remove-bookmark
1793   "#" gnus-summary-mark-as-processable
1794   "\M-#" gnus-summary-unmark-as-processable
1795   "S" gnus-summary-limit-include-expunged
1796   "C" gnus-summary-catchup
1797   "H" gnus-summary-catchup-to-here
1798   "h" gnus-summary-catchup-from-here
1799   "\C-c" gnus-summary-catchup-all
1800   "k" gnus-summary-kill-same-subject-and-select
1801   "K" gnus-summary-kill-same-subject
1802   "P" gnus-uu-mark-map)
1803
1804 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1805   "c" gnus-summary-clear-above
1806   "u" gnus-summary-tick-above
1807   "m" gnus-summary-mark-above
1808   "k" gnus-summary-kill-below)
1809
1810 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1811   "/" gnus-summary-limit-to-subject
1812   "n" gnus-summary-limit-to-articles
1813   "w" gnus-summary-pop-limit
1814   "s" gnus-summary-limit-to-subject
1815   "a" gnus-summary-limit-to-author
1816   "u" gnus-summary-limit-to-unread
1817   "m" gnus-summary-limit-to-marks
1818   "M" gnus-summary-limit-exclude-marks
1819   "v" gnus-summary-limit-to-score
1820   "*" gnus-summary-limit-include-cached
1821   "D" gnus-summary-limit-include-dormant
1822   "T" gnus-summary-limit-include-thread
1823   "d" gnus-summary-limit-exclude-dormant
1824   "t" gnus-summary-limit-to-age
1825   "." gnus-summary-limit-to-unseen
1826   "x" gnus-summary-limit-to-extra
1827   "p" gnus-summary-limit-to-display-predicate
1828   "E" gnus-summary-limit-include-expunged
1829   "c" gnus-summary-limit-exclude-childless-dormant
1830   "C" gnus-summary-limit-mark-excluded-as-read
1831   "o" gnus-summary-insert-old-articles
1832   "N" gnus-summary-insert-new-articles
1833   "r" gnus-summary-limit-to-replied)
1834
1835 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1836   "n" gnus-summary-next-unread-article
1837   "p" gnus-summary-prev-unread-article
1838   "N" gnus-summary-next-article
1839   "P" gnus-summary-prev-article
1840   "\C-n" gnus-summary-next-same-subject
1841   "\C-p" gnus-summary-prev-same-subject
1842   "\M-n" gnus-summary-next-unread-subject
1843   "\M-p" gnus-summary-prev-unread-subject
1844   "f" gnus-summary-first-unread-article
1845   "b" gnus-summary-best-unread-article
1846   "j" gnus-summary-goto-article
1847   "g" gnus-summary-goto-subject
1848   "l" gnus-summary-goto-last-article
1849   "o" gnus-summary-pop-article)
1850
1851 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1852   "k" gnus-summary-kill-thread
1853   "l" gnus-summary-lower-thread
1854   "i" gnus-summary-raise-thread
1855   "T" gnus-summary-toggle-threads
1856   "t" gnus-summary-rethread-current
1857   "^" gnus-summary-reparent-thread
1858   "s" gnus-summary-show-thread
1859   "S" gnus-summary-show-all-threads
1860   "h" gnus-summary-hide-thread
1861   "H" gnus-summary-hide-all-threads
1862   "n" gnus-summary-next-thread
1863   "p" gnus-summary-prev-thread
1864   "u" gnus-summary-up-thread
1865   "o" gnus-summary-top-thread
1866   "d" gnus-summary-down-thread
1867   "#" gnus-uu-mark-thread
1868   "\M-#" gnus-uu-unmark-thread)
1869
1870 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1871   "g" gnus-summary-prepare
1872   "c" gnus-summary-insert-cached-articles
1873   "d" gnus-summary-insert-dormant-articles)
1874
1875 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1876   "c" gnus-summary-catchup-and-exit
1877   "C" gnus-summary-catchup-all-and-exit
1878   "E" gnus-summary-exit-no-update
1879   "Q" gnus-summary-exit
1880   "Z" gnus-summary-exit
1881   "n" gnus-summary-catchup-and-goto-next-group
1882   "R" gnus-summary-reselect-current-group
1883   "G" gnus-summary-rescan-group
1884   "N" gnus-summary-next-group
1885   "s" gnus-summary-save-newsrc
1886   "P" gnus-summary-prev-group)
1887
1888 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1889   " " gnus-summary-next-page
1890   "n" gnus-summary-next-page
1891   "\177" gnus-summary-prev-page
1892   [delete] gnus-summary-prev-page
1893   "p" gnus-summary-prev-page
1894   "\r" gnus-summary-scroll-up
1895   "\M-\r" gnus-summary-scroll-down
1896   "<" gnus-summary-beginning-of-article
1897   ">" gnus-summary-end-of-article
1898   "b" gnus-summary-beginning-of-article
1899   "e" gnus-summary-end-of-article
1900   "^" gnus-summary-refer-parent-article
1901   "r" gnus-summary-refer-parent-article
1902   "D" gnus-summary-enter-digest-group
1903   "R" gnus-summary-refer-references
1904   "T" gnus-summary-refer-thread
1905   "g" gnus-summary-show-article
1906   "s" gnus-summary-isearch-article
1907   "P" gnus-summary-print-article
1908   "M" gnus-mailing-list-insinuate
1909   "t" gnus-article-babel)
1910
1911 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1912   "b" gnus-article-add-buttons
1913   "B" gnus-article-add-buttons-to-head
1914   "o" gnus-article-treat-overstrike
1915   "e" gnus-article-emphasize
1916   "w" gnus-article-fill-cited-article
1917   "Q" gnus-article-fill-long-lines
1918   "C" gnus-article-capitalize-sentences
1919   "c" gnus-article-remove-cr
1920   "q" gnus-article-de-quoted-unreadable
1921   "6" gnus-article-de-base64-unreadable
1922   "Z" gnus-article-decode-HZ
1923   "A" gnus-article-treat-ansi-sequences
1924   "h" gnus-article-wash-html
1925   "u" gnus-article-unsplit-urls
1926   "s" gnus-summary-force-verify-and-decrypt
1927   "f" gnus-article-display-x-face
1928   "l" gnus-summary-stop-page-breaking
1929   "r" gnus-summary-caesar-message
1930   "m" gnus-summary-morse-message
1931   "t" gnus-summary-toggle-header
1932   "g" gnus-treat-smiley
1933   "v" gnus-summary-verbose-headers
1934   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1935   "p" gnus-article-verify-x-pgp-sig
1936   "d" gnus-article-treat-dumbquotes)
1937
1938 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1939   ;; mnemonic: deuglif*Y*
1940   "u" gnus-article-outlook-unwrap-lines
1941   "a" gnus-article-outlook-repair-attribution
1942   "c" gnus-article-outlook-rearrange-citation
1943   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1944
1945 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1946   "a" gnus-article-hide
1947   "h" gnus-article-hide-headers
1948   "b" gnus-article-hide-boring-headers
1949   "s" gnus-article-hide-signature
1950   "c" gnus-article-hide-citation
1951   "C" gnus-article-hide-citation-in-followups
1952   "l" gnus-article-hide-list-identifiers
1953   "B" gnus-article-strip-banner
1954   "P" gnus-article-hide-pem
1955   "\C-c" gnus-article-hide-citation-maybe)
1956
1957 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1958   "a" gnus-article-highlight
1959   "h" gnus-article-highlight-headers
1960   "c" gnus-article-highlight-citation
1961   "s" gnus-article-highlight-signature)
1962
1963 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1964   "f" gnus-article-treat-fold-headers
1965   "u" gnus-article-treat-unfold-headers
1966   "n" gnus-article-treat-fold-newsgroups)
1967
1968 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1969   "x" gnus-article-display-x-face
1970   "d" gnus-article-display-face
1971   "s" gnus-treat-smiley
1972   "D" gnus-article-remove-images
1973   "f" gnus-treat-from-picon
1974   "m" gnus-treat-mail-picon
1975   "n" gnus-treat-newsgroups-picon)
1976
1977 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1978   "w" gnus-article-decode-mime-words
1979   "c" gnus-article-decode-charset
1980   "v" gnus-mime-view-all-parts
1981   "b" gnus-article-view-part)
1982
1983 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1984   "z" gnus-article-date-ut
1985   "u" gnus-article-date-ut
1986   "l" gnus-article-date-local
1987   "p" gnus-article-date-english
1988   "e" gnus-article-date-lapsed
1989   "o" gnus-article-date-original
1990   "i" gnus-article-date-iso8601
1991   "s" gnus-article-date-user)
1992
1993 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1994   "t" gnus-article-remove-trailing-blank-lines
1995   "l" gnus-article-strip-leading-blank-lines
1996   "m" gnus-article-strip-multiple-blank-lines
1997   "a" gnus-article-strip-blank-lines
1998   "A" gnus-article-strip-all-blank-lines
1999   "s" gnus-article-strip-leading-space
2000   "e" gnus-article-strip-trailing-space
2001   "w" gnus-article-remove-leading-whitespace)
2002
2003 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2004   "v" gnus-version
2005   "f" gnus-summary-fetch-faq
2006   "d" gnus-summary-describe-group
2007   "h" gnus-summary-describe-briefly
2008   "i" gnus-info-find-node
2009   "c" gnus-group-fetch-charter
2010   "C" gnus-group-fetch-control)
2011
2012 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2013   "e" gnus-summary-expire-articles
2014   "\M-\C-e" gnus-summary-expire-articles-now
2015   "\177" gnus-summary-delete-article
2016   [delete] gnus-summary-delete-article
2017   [backspace] gnus-summary-delete-article
2018   "m" gnus-summary-move-article
2019   "r" gnus-summary-respool-article
2020   "w" gnus-summary-edit-article
2021   "c" gnus-summary-copy-article
2022   "B" gnus-summary-crosspost-article
2023   "q" gnus-summary-respool-query
2024   "t" gnus-summary-respool-trace
2025   "i" gnus-summary-import-article
2026   "I" gnus-summary-create-article
2027   "p" gnus-summary-article-posted-p)
2028
2029 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2030   "o" gnus-summary-save-article
2031   "m" gnus-summary-save-article-mail
2032   "F" gnus-summary-write-article-file
2033   "r" gnus-summary-save-article-rmail
2034   "f" gnus-summary-save-article-file
2035   "b" gnus-summary-save-article-body-file
2036   "h" gnus-summary-save-article-folder
2037   "v" gnus-summary-save-article-vm
2038   "p" gnus-summary-pipe-output
2039   "P" gnus-summary-muttprint
2040   "s" gnus-soup-add-article)
2041
2042 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2043   "b" gnus-summary-display-buttonized
2044   "m" gnus-summary-repair-multipart
2045   "v" gnus-article-view-part
2046   "o" gnus-article-save-part
2047   "c" gnus-article-copy-part
2048   "C" gnus-article-view-part-as-charset
2049   "e" gnus-article-view-part-externally
2050   "E" gnus-article-encrypt-body
2051   "i" gnus-article-inline-part
2052   "|" gnus-article-pipe-part)
2053
2054 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2055   "p" gnus-summary-mark-as-processable
2056   "u" gnus-summary-unmark-as-processable
2057   "U" gnus-summary-unmark-all-processable
2058   "v" gnus-uu-mark-over
2059   "s" gnus-uu-mark-series
2060   "r" gnus-uu-mark-region
2061   "g" gnus-uu-unmark-region
2062   "R" gnus-uu-mark-by-regexp
2063   "G" gnus-uu-unmark-by-regexp
2064   "t" gnus-uu-mark-thread
2065   "T" gnus-uu-unmark-thread
2066   "a" gnus-uu-mark-all
2067   "b" gnus-uu-mark-buffer
2068   "S" gnus-uu-mark-sparse
2069   "k" gnus-summary-kill-process-mark
2070   "y" gnus-summary-yank-process-mark
2071   "w" gnus-summary-save-process-mark
2072   "i" gnus-uu-invert-processable)
2073
2074 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2075   ;;"x" gnus-uu-extract-any
2076   "m" gnus-summary-save-parts
2077   "u" gnus-uu-decode-uu
2078   "U" gnus-uu-decode-uu-and-save
2079   "s" gnus-uu-decode-unshar
2080   "S" gnus-uu-decode-unshar-and-save
2081   "o" gnus-uu-decode-save
2082   "O" gnus-uu-decode-save
2083   "b" gnus-uu-decode-binhex
2084   "B" gnus-uu-decode-binhex
2085   "p" gnus-uu-decode-postscript
2086   "P" gnus-uu-decode-postscript-and-save)
2087
2088 (gnus-define-keys
2089     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2090   "u" gnus-uu-decode-uu-view
2091   "U" gnus-uu-decode-uu-and-save-view
2092   "s" gnus-uu-decode-unshar-view
2093   "S" gnus-uu-decode-unshar-and-save-view
2094   "o" gnus-uu-decode-save-view
2095   "O" gnus-uu-decode-save-view
2096   "b" gnus-uu-decode-binhex-view
2097   "B" gnus-uu-decode-binhex-view
2098   "p" gnus-uu-decode-postscript-view
2099   "P" gnus-uu-decode-postscript-and-save-view)
2100
2101 (defvar gnus-article-post-menu nil)
2102
2103 (defconst gnus-summary-menu-maxlen 20)
2104
2105 (defun gnus-summary-menu-split (menu)
2106   ;; If we have lots of elements, divide them into groups of 20
2107   ;; and make a pane (or submenu) for each one.
2108   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2109       (let ((menu menu) sublists next
2110             (i 1))
2111         (while menu
2112           ;; Pull off the next gnus-summary-menu-maxlen elements
2113           ;; and make them the next element of sublist.
2114           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2115           (if next
2116               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2117                       nil))
2118           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2119                                              (aref (car (last menu)) 0)) menu)
2120                                sublists))
2121           (setq i (1+ i))
2122           (setq menu next))
2123         (nreverse sublists))
2124     ;; Few elements--put them all in one pane.
2125     menu))
2126
2127 (defun gnus-summary-make-menu-bar ()
2128   (gnus-turn-off-edit-menu 'summary)
2129
2130   (unless (boundp 'gnus-summary-misc-menu)
2131
2132     (easy-menu-define
2133       gnus-summary-kill-menu gnus-summary-mode-map ""
2134       (cons
2135        "Score"
2136        (nconc
2137         (list
2138          ["Customize" gnus-score-customize t])
2139         (gnus-make-score-map 'increase)
2140         (gnus-make-score-map 'lower)
2141         '(("Mark"
2142            ["Kill below" gnus-summary-kill-below t]
2143            ["Mark above" gnus-summary-mark-above t]
2144            ["Tick above" gnus-summary-tick-above t]
2145            ["Clear above" gnus-summary-clear-above t])
2146           ["Current score" gnus-summary-current-score t]
2147           ["Set score" gnus-summary-set-score t]
2148           ["Switch current score file..." gnus-score-change-score-file t]
2149           ["Set mark below..." gnus-score-set-mark-below t]
2150           ["Set expunge below..." gnus-score-set-expunge-below t]
2151           ["Edit current score file" gnus-score-edit-current-scores t]
2152           ["Edit score file" gnus-score-edit-file t]
2153           ["Trace score" gnus-score-find-trace t]
2154           ["Find words" gnus-score-find-favourite-words t]
2155           ["Rescore buffer" gnus-summary-rescore t]
2156           ["Increase score..." gnus-summary-increase-score t]
2157           ["Lower score..." gnus-summary-lower-score t]))))
2158
2159     ;; Define both the Article menu in the summary buffer and the
2160     ;; equivalent Commands menu in the article buffer here for
2161     ;; consistency.
2162     (let ((innards
2163            `(("Hide"
2164               ["All" gnus-article-hide t]
2165               ["Headers" gnus-article-hide-headers t]
2166               ["Signature" gnus-article-hide-signature t]
2167               ["Citation" gnus-article-hide-citation t]
2168               ["List identifiers" gnus-article-hide-list-identifiers t]
2169               ["Banner" gnus-article-strip-banner t]
2170               ["Boring headers" gnus-article-hide-boring-headers t])
2171              ("Highlight"
2172               ["All" gnus-article-highlight t]
2173               ["Headers" gnus-article-highlight-headers t]
2174               ["Signature" gnus-article-highlight-signature t]
2175               ["Citation" gnus-article-highlight-citation t])
2176              ("MIME"
2177               ["Words" gnus-article-decode-mime-words t]
2178               ["Charset" gnus-article-decode-charset t]
2179               ["QP" gnus-article-de-quoted-unreadable t]
2180               ["Base64" gnus-article-de-base64-unreadable t]
2181               ["View MIME buttons" gnus-summary-display-buttonized t]
2182               ["View all" gnus-mime-view-all-parts t]
2183               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2184               ["Encrypt body" gnus-article-encrypt-body
2185                :active (not (gnus-group-read-only-p))
2186                ,@(if (featurep 'xemacs) nil
2187                    '(:help "Encrypt the message body on disk"))]
2188               ["Extract all parts..." gnus-summary-save-parts t]
2189               ("Multipart"
2190                ["Repair multipart" gnus-summary-repair-multipart t]
2191                ["Pipe part..." gnus-article-pipe-part t]
2192                ["Inline part" gnus-article-inline-part t]
2193                ["Encrypt body" gnus-article-encrypt-body
2194                 :active (not (gnus-group-read-only-p))
2195                ,@(if (featurep 'xemacs) nil
2196                    '(:help "Encrypt the message body on disk"))]
2197                ["View part externally" gnus-article-view-part-externally t]
2198                ["View part with charset..." gnus-article-view-part-as-charset t]
2199                ["Copy part" gnus-article-copy-part t]
2200                ["Save part..." gnus-article-save-part t]
2201                ["View part" gnus-article-view-part t]))
2202              ("Date"
2203               ["Local" gnus-article-date-local t]
2204               ["ISO8601" gnus-article-date-iso8601 t]
2205               ["UT" gnus-article-date-ut t]
2206               ["Original" gnus-article-date-original t]
2207               ["Lapsed" gnus-article-date-lapsed t]
2208               ["User-defined" gnus-article-date-user t])
2209              ("Display"
2210               ["Remove images" gnus-article-remove-images t]
2211               ["Toggle smiley" gnus-treat-smiley t]
2212               ["Show X-Face" gnus-article-display-x-face t]
2213               ["Show picons in From" gnus-treat-from-picon t]
2214               ["Show picons in mail headers" gnus-treat-mail-picon t]
2215               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2216               ("View as different encoding"
2217                ,@(gnus-summary-menu-split
2218                   (mapcar
2219                    (lambda (cs)
2220                      ;; Since easymenu under Emacs doesn't allow
2221                      ;; lambda forms for menu commands, we should
2222                      ;; provide intern'ed function symbols.
2223                      (let ((command (intern (format "\
2224 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2225                        (fset command
2226                              `(lambda ()
2227                                 (interactive)
2228                                 (let ((gnus-summary-show-article-charset-alist
2229                                        '((1 . ,cs))))
2230                                   (gnus-summary-show-article 1))))
2231                        `[,(symbol-name cs) ,command t]))
2232                    (sort (if (fboundp 'coding-system-list)
2233                              (coding-system-list)
2234                            (mapcar 'car mm-mime-mule-charset-alist))
2235                          'string<)))))
2236              ("Washing"
2237               ("Remove Blanks"
2238                ["Leading" gnus-article-strip-leading-blank-lines t]
2239                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2240                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2241                ["All of the above" gnus-article-strip-blank-lines t]
2242                ["All" gnus-article-strip-all-blank-lines t]
2243                ["Leading space" gnus-article-strip-leading-space t]
2244                ["Trailing space" gnus-article-strip-trailing-space t]
2245                ["Leading space in headers"
2246                 gnus-article-remove-leading-whitespace t])
2247               ["Overstrike" gnus-article-treat-overstrike t]
2248               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2249               ["Emphasis" gnus-article-emphasize t]
2250               ["Word wrap" gnus-article-fill-cited-article t]
2251               ["Fill long lines" gnus-article-fill-long-lines t]
2252               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2253               ["Remove CR" gnus-article-remove-cr t]
2254               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2255               ["Base64" gnus-article-de-base64-unreadable t]
2256               ["Rot 13" gnus-summary-caesar-message
2257                ,@(if (featurep 'xemacs) '(t)
2258                    '(:help "\"Caesar rotate\" article by 13"))]
2259               ["Morse decode" gnus-summary-morse-message t]
2260               ["Unix pipe..." gnus-summary-pipe-message t]
2261               ["Add buttons" gnus-article-add-buttons t]
2262               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2263               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2264               ["Verbose header" gnus-summary-verbose-headers t]
2265               ["Toggle header" gnus-summary-toggle-header t]
2266               ["Unfold headers" gnus-article-treat-unfold-headers t]
2267               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2268               ["Html" gnus-article-wash-html t]
2269               ["Unsplit URLs" gnus-article-unsplit-urls t]
2270               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2271               ["Decode HZ" gnus-article-decode-HZ t]
2272               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2273               ("(Outlook) Deuglify"
2274                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2275                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2276                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2277                ["Full (Outlook) deuglify"
2278                 gnus-article-outlook-deuglify-article t])
2279               )
2280              ("Output"
2281               ["Save in default format..." gnus-summary-save-article
2282                ,@(if (featurep 'xemacs) '(t)
2283                    '(:help "Save article using default method"))]
2284               ["Save in file..." gnus-summary-save-article-file
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "Save article in file"))]
2287               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2288               ["Save in MH folder..." gnus-summary-save-article-folder t]
2289               ["Save in VM folder..." gnus-summary-save-article-vm t]
2290               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2291               ["Save body in file..." gnus-summary-save-article-body-file t]
2292               ["Pipe through a filter..." gnus-summary-pipe-output t]
2293               ["Add to SOUP packet" gnus-soup-add-article t]
2294               ["Print with Muttprint..." gnus-summary-muttprint t]
2295               ["Print" gnus-summary-print-article
2296                ,@(if (featurep 'xemacs) '(t)
2297                    '(:help "Generate and print a PostScript image"))])
2298              ("Copy, move,... (Backend)"
2299               ,@(if (featurep 'xemacs) nil
2300                   '(:help "Copying, moving, expiring articles..."))
2301               ["Respool article..." gnus-summary-respool-article t]
2302               ["Move article..." gnus-summary-move-article
2303                (gnus-check-backend-function
2304                 'request-move-article gnus-newsgroup-name)]
2305               ["Copy article..." gnus-summary-copy-article t]
2306               ["Crosspost article..." gnus-summary-crosspost-article
2307                (gnus-check-backend-function
2308                 'request-replace-article gnus-newsgroup-name)]
2309               ["Import file..." gnus-summary-import-article
2310                (gnus-check-backend-function
2311                 'request-accept-article gnus-newsgroup-name)]
2312               ["Create article..." gnus-summary-create-article
2313                (gnus-check-backend-function
2314                 'request-accept-article gnus-newsgroup-name)]
2315               ["Check if posted" gnus-summary-article-posted-p t]
2316               ["Edit article" gnus-summary-edit-article
2317                (not (gnus-group-read-only-p))]
2318               ["Delete article" gnus-summary-delete-article
2319                (gnus-check-backend-function
2320                 'request-expire-articles gnus-newsgroup-name)]
2321               ["Query respool" gnus-summary-respool-query t]
2322               ["Trace respool" gnus-summary-respool-trace t]
2323               ["Delete expirable articles" gnus-summary-expire-articles-now
2324                (gnus-check-backend-function
2325                 'request-expire-articles gnus-newsgroup-name)])
2326              ("Extract"
2327               ["Uudecode" gnus-uu-decode-uu
2328                ,@(if (featurep 'xemacs) '(t)
2329                    '(:help "Decode uuencoded article(s)"))]
2330               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2331               ["Unshar" gnus-uu-decode-unshar t]
2332               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2333               ["Save" gnus-uu-decode-save t]
2334               ["Binhex" gnus-uu-decode-binhex t]
2335               ["Postscript" gnus-uu-decode-postscript t]
2336               ["All MIME parts" gnus-summary-save-parts t])
2337              ("Cache"
2338               ["Enter article" gnus-cache-enter-article t]
2339               ["Remove article" gnus-cache-remove-article t])
2340              ["Translate" gnus-article-babel t]
2341              ["Select article buffer" gnus-summary-select-article-buffer t]
2342              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2343              ["Isearch article..." gnus-summary-isearch-article t]
2344              ["Beginning of the article" gnus-summary-beginning-of-article t]
2345              ["End of the article" gnus-summary-end-of-article t]
2346              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2347              ["Fetch referenced articles" gnus-summary-refer-references t]
2348              ["Fetch current thread" gnus-summary-refer-thread t]
2349              ["Fetch article with id..." gnus-summary-refer-article t]
2350              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2351              ["Redisplay" gnus-summary-show-article t]
2352              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2353       (easy-menu-define
2354         gnus-summary-article-menu gnus-summary-mode-map ""
2355         (cons "Article" innards))
2356
2357       (if (not (keymapp gnus-summary-article-menu))
2358           (easy-menu-define
2359             gnus-article-commands-menu gnus-article-mode-map ""
2360             (cons "Commands" innards))
2361         ;; in Emacs, don't share menu.
2362         (setq gnus-article-commands-menu
2363               (copy-keymap gnus-summary-article-menu))
2364         (define-key gnus-article-mode-map [menu-bar commands]
2365           (cons "Commands" gnus-article-commands-menu))))
2366
2367     (easy-menu-define
2368       gnus-summary-thread-menu gnus-summary-mode-map ""
2369       '("Threads"
2370         ["Find all messages in thread" gnus-summary-refer-thread t]
2371         ["Toggle threading" gnus-summary-toggle-threads t]
2372         ["Hide threads" gnus-summary-hide-all-threads t]
2373         ["Show threads" gnus-summary-show-all-threads t]
2374         ["Hide thread" gnus-summary-hide-thread t]
2375         ["Show thread" gnus-summary-show-thread t]
2376         ["Go to next thread" gnus-summary-next-thread t]
2377         ["Go to previous thread" gnus-summary-prev-thread t]
2378         ["Go down thread" gnus-summary-down-thread t]
2379         ["Go up thread" gnus-summary-up-thread t]
2380         ["Top of thread" gnus-summary-top-thread t]
2381         ["Mark thread as read" gnus-summary-kill-thread t]
2382         ["Lower thread score" gnus-summary-lower-thread t]
2383         ["Raise thread score" gnus-summary-raise-thread t]
2384         ["Rethread current" gnus-summary-rethread-current t]))
2385
2386     (easy-menu-define
2387       gnus-summary-post-menu gnus-summary-mode-map ""
2388       `("Post"
2389         ["Send a message (mail or news)" gnus-summary-post-news
2390          ,@(if (featurep 'xemacs) '(t)
2391              '(:help "Compose a new message (mail or news)"))]
2392         ["Followup" gnus-summary-followup
2393          ,@(if (featurep 'xemacs) '(t)
2394              '(:help "Post followup to this article"))]
2395         ["Followup and yank" gnus-summary-followup-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Post followup to this article, quoting its contents"))]
2398         ["Supersede article" gnus-summary-supersede-article t]
2399         ["Cancel article" gnus-summary-cancel-article
2400          ,@(if (featurep 'xemacs) '(t)
2401              '(:help "Cancel an article you posted"))]
2402         ["Reply" gnus-summary-reply t]
2403         ["Reply and yank" gnus-summary-reply-with-original t]
2404         ["Wide reply" gnus-summary-wide-reply t]
2405         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2406          ,@(if (featurep 'xemacs) '(t)
2407              '(:help "Mail a reply, quoting this article"))]
2408         ["Very wide reply" gnus-summary-very-wide-reply t]
2409         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2410          ,@(if (featurep 'xemacs) '(t)
2411              '(:help "Mail a very wide reply, quoting this article"))]
2412         ["Mail forward" gnus-summary-mail-forward t]
2413         ["Post forward" gnus-summary-post-forward t]
2414         ["Digest and mail" gnus-uu-digest-mail-forward t]
2415         ["Digest and post" gnus-uu-digest-post-forward t]
2416         ["Resend message" gnus-summary-resend-message t]
2417         ["Resend message edit" gnus-summary-resend-message-edit t]
2418         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2419         ["Send a mail" gnus-summary-mail-other-window t]
2420         ["Create a local message" gnus-summary-news-other-window t]
2421         ["Uuencode and post" gnus-uu-post-news
2422          ,@(if (featurep 'xemacs) '(t)
2423              '(:help "Post a uuencoded article"))]
2424         ["Followup via news" gnus-summary-followup-to-mail t]
2425         ["Followup via news and yank"
2426          gnus-summary-followup-to-mail-with-original t]
2427         ;;("Draft"
2428         ;;["Send" gnus-summary-send-draft t]
2429         ;;["Send bounced" gnus-resend-bounced-mail t])
2430         ))
2431
2432     (cond
2433      ((not (keymapp gnus-summary-post-menu))
2434       (setq gnus-article-post-menu gnus-summary-post-menu))
2435      ((not gnus-article-post-menu)
2436       ;; Don't share post menu.
2437       (setq gnus-article-post-menu
2438             (copy-keymap gnus-summary-post-menu))))
2439     (define-key gnus-article-mode-map [menu-bar post]
2440       (cons "Post" gnus-article-post-menu))
2441
2442     (easy-menu-define
2443       gnus-summary-misc-menu gnus-summary-mode-map ""
2444       `("Gnus"
2445         ("Mark Read"
2446          ["Mark as read" gnus-summary-mark-as-read-forward t]
2447          ["Mark same subject and select"
2448           gnus-summary-kill-same-subject-and-select t]
2449          ["Mark same subject" gnus-summary-kill-same-subject t]
2450          ["Catchup" gnus-summary-catchup
2451           ,@(if (featurep 'xemacs) '(t)
2452               '(:help "Mark unread articles in this group as read"))]
2453          ["Catchup all" gnus-summary-catchup-all t]
2454          ["Catchup to here" gnus-summary-catchup-to-here t]
2455          ["Catchup from here" gnus-summary-catchup-from-here t]
2456          ["Catchup region" gnus-summary-mark-region-as-read
2457           (gnus-mark-active-p)]
2458          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2459         ("Mark Various"
2460          ["Tick" gnus-summary-tick-article-forward t]
2461          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2462          ["Remove marks" gnus-summary-clear-mark-forward t]
2463          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2464          ["Set bookmark" gnus-summary-set-bookmark t]
2465          ["Remove bookmark" gnus-summary-remove-bookmark t])
2466         ("Limit to"
2467          ["Marks..." gnus-summary-limit-to-marks t]
2468          ["Subject..." gnus-summary-limit-to-subject t]
2469          ["Author..." gnus-summary-limit-to-author t]
2470          ["Age..." gnus-summary-limit-to-age t]
2471          ["Extra..." gnus-summary-limit-to-extra t]
2472          ["Score..." gnus-summary-limit-to-score t]
2473          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2474          ["Unread" gnus-summary-limit-to-unread t]
2475          ["Unseen" gnus-summary-limit-to-unseen t]
2476          ["Replied" gnus-summary-limit-to-replied t]
2477          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2478          ["Next articles" gnus-summary-limit-to-articles t]
2479          ["Pop limit" gnus-summary-pop-limit t]
2480          ["Show dormant" gnus-summary-limit-include-dormant t]
2481          ["Hide childless dormant"
2482           gnus-summary-limit-exclude-childless-dormant t]
2483          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2484          ["Hide marked" gnus-summary-limit-exclude-marks t]
2485          ["Show expunged" gnus-summary-limit-include-expunged t])
2486         ("Process Mark"
2487          ["Set mark" gnus-summary-mark-as-processable t]
2488          ["Remove mark" gnus-summary-unmark-as-processable t]
2489          ["Remove all marks" gnus-summary-unmark-all-processable t]
2490          ["Invert marks" gnus-uu-invert-processable t]
2491          ["Mark above" gnus-uu-mark-over t]
2492          ["Mark series" gnus-uu-mark-series t]
2493          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2494          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2495          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2496          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2497          ["Mark all" gnus-uu-mark-all t]
2498          ["Mark buffer" gnus-uu-mark-buffer t]
2499          ["Mark sparse" gnus-uu-mark-sparse t]
2500          ["Mark thread" gnus-uu-mark-thread t]
2501          ["Unmark thread" gnus-uu-unmark-thread t]
2502          ("Process Mark Sets"
2503           ["Kill" gnus-summary-kill-process-mark t]
2504           ["Yank" gnus-summary-yank-process-mark
2505            gnus-newsgroup-process-stack]
2506           ["Save" gnus-summary-save-process-mark t]
2507           ["Run command on marked..." gnus-summary-universal-argument t]))
2508         ("Scroll article"
2509          ["Page forward" gnus-summary-next-page
2510           ,@(if (featurep 'xemacs) '(t)
2511               '(:help "Show next page of article"))]
2512          ["Page backward" gnus-summary-prev-page
2513           ,@(if (featurep 'xemacs) '(t)
2514               '(:help "Show previous page of article"))]
2515          ["Line forward" gnus-summary-scroll-up t])
2516         ("Move"
2517          ["Next unread article" gnus-summary-next-unread-article t]
2518          ["Previous unread article" gnus-summary-prev-unread-article t]
2519          ["Next article" gnus-summary-next-article t]
2520          ["Previous article" gnus-summary-prev-article t]
2521          ["Next unread subject" gnus-summary-next-unread-subject t]
2522          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2523          ["Next article same subject" gnus-summary-next-same-subject t]
2524          ["Previous article same subject" gnus-summary-prev-same-subject t]
2525          ["First unread article" gnus-summary-first-unread-article t]
2526          ["Best unread article" gnus-summary-best-unread-article t]
2527          ["Go to subject number..." gnus-summary-goto-subject t]
2528          ["Go to article number..." gnus-summary-goto-article t]
2529          ["Go to the last article" gnus-summary-goto-last-article t]
2530          ["Pop article off history" gnus-summary-pop-article t])
2531         ("Sort"
2532          ["Sort by number" gnus-summary-sort-by-number t]
2533          ["Sort by author" gnus-summary-sort-by-author t]
2534          ["Sort by subject" gnus-summary-sort-by-subject t]
2535          ["Sort by date" gnus-summary-sort-by-date t]
2536          ["Sort by score" gnus-summary-sort-by-score t]
2537          ["Sort by lines" gnus-summary-sort-by-lines t]
2538          ["Sort by characters" gnus-summary-sort-by-chars t]
2539          ["Randomize" gnus-summary-sort-by-random t]
2540          ["Original sort" gnus-summary-sort-by-original t])
2541         ("Help"
2542          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2543          ["Describe group" gnus-summary-describe-group t]
2544          ["Fetch charter" gnus-group-fetch-charter
2545           ,@(if (featurep 'xemacs) nil
2546               '(:help "Display the charter of the current group"))]
2547          ["Fetch control message" gnus-group-fetch-control
2548           ,@(if (featurep 'xemacs) nil
2549               '(:help "Display the archived control message for the current group"))]
2550          ["Read manual" gnus-info-find-node t])
2551         ("Modes"
2552          ["Pick and read" gnus-pick-mode t]
2553          ["Binary" gnus-binary-mode t])
2554         ("Regeneration"
2555          ["Regenerate" gnus-summary-prepare t]
2556          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2557          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2558          ["Toggle threading" gnus-summary-toggle-threads t])
2559         ["See old articles" gnus-summary-insert-old-articles t]
2560         ["See new articles" gnus-summary-insert-new-articles t]
2561         ["Filter articles..." gnus-summary-execute-command t]
2562         ["Run command on articles..." gnus-summary-universal-argument t]
2563         ["Search articles forward..." gnus-summary-search-article-forward t]
2564         ["Search articles backward..." gnus-summary-search-article-backward t]
2565         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2566         ["Expand window" gnus-summary-expand-window t]
2567         ["Expire expirable articles" gnus-summary-expire-articles
2568          (gnus-check-backend-function
2569           'request-expire-articles gnus-newsgroup-name)]
2570         ["Edit local kill file" gnus-summary-edit-local-kill t]
2571         ["Edit main kill file" gnus-summary-edit-global-kill t]
2572         ["Edit group parameters" gnus-summary-edit-parameters t]
2573         ["Customize group parameters" gnus-summary-customize-parameters t]
2574         ["Send a bug report" gnus-bug t]
2575         ("Exit"
2576          ["Catchup and exit" gnus-summary-catchup-and-exit
2577           ,@(if (featurep 'xemacs) '(t)
2578               '(:help "Mark unread articles in this group as read, then exit"))]
2579          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2580          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2581          ["Exit group" gnus-summary-exit
2582           ,@(if (featurep 'xemacs) '(t)
2583               '(:help "Exit current group, return to group selection mode"))]
2584          ["Exit group without updating" gnus-summary-exit-no-update t]
2585          ["Exit and goto next group" gnus-summary-next-group t]
2586          ["Exit and goto prev group" gnus-summary-prev-group t]
2587          ["Reselect group" gnus-summary-reselect-current-group t]
2588          ["Rescan group" gnus-summary-rescan-group t]
2589          ["Update dribble" gnus-summary-save-newsrc t])))
2590
2591     (gnus-run-hooks 'gnus-summary-menu-hook)))
2592
2593 (defvar gnus-summary-tool-bar-map nil)
2594
2595 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2596 (defun gnus-summary-make-tool-bar ()
2597   (if (and (fboundp 'tool-bar-add-item-from-menu)
2598            (default-value 'tool-bar-mode)
2599            (not gnus-summary-tool-bar-map))
2600       (setq gnus-summary-tool-bar-map
2601             (let ((tool-bar-map (make-sparse-keymap))
2602                   (load-path (mm-image-load-path)))
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-summary-post-news "post" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-summary-followup "followup" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-summary-reply "reply" gnus-summary-mode-map)
2617               (tool-bar-add-item-from-menu
2618                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2619               (tool-bar-add-item-from-menu
2620                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2621               (tool-bar-add-item-from-menu
2622                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2623               (tool-bar-add-item-from-menu
2624                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2625               (tool-bar-add-item-from-menu
2626                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2627               (tool-bar-add-item-from-menu
2628                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2629               (tool-bar-add-item-from-menu
2630                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2631               (tool-bar-add-item-from-menu
2632                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2633               (tool-bar-add-item-from-menu
2634                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2635               tool-bar-map)))
2636   (if gnus-summary-tool-bar-map
2637       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2638
2639 (defun gnus-score-set-default (var value)
2640   "A version of set that updates the GNU Emacs menu-bar."
2641   (set var value)
2642   ;; It is the message that forces the active status to be updated.
2643   (message ""))
2644
2645 (defun gnus-make-score-map (type)
2646   "Make a summary score map of type TYPE."
2647   (if t
2648       nil
2649     (let ((headers '(("author" "from" string)
2650                      ("subject" "subject" string)
2651                      ("article body" "body" string)
2652                      ("article head" "head" string)
2653                      ("xref" "xref" string)
2654                      ("extra header" "extra" string)
2655                      ("lines" "lines" number)
2656                      ("followups to author" "followup" string)))
2657           (types '((number ("less than" <)
2658                            ("greater than" >)
2659                            ("equal" =))
2660                    (string ("substring" s)
2661                            ("exact string" e)
2662                            ("fuzzy string" f)
2663                            ("regexp" r))))
2664           (perms '(("temporary" (current-time-string))
2665                    ("permanent" nil)
2666                    ("immediate" now)))
2667           header)
2668       (list
2669        (apply
2670         'nconc
2671         (list
2672          (if (eq type 'lower)
2673              "Lower score"
2674            "Increase score"))
2675         (let (outh)
2676           (while headers
2677             (setq header (car headers))
2678             (setq outh
2679                   (cons
2680                    (apply
2681                     'nconc
2682                     (list (car header))
2683                     (let ((ts (cdr (assoc (nth 2 header) types)))
2684                           outt)
2685                       (while ts
2686                         (setq outt
2687                               (cons
2688                                (apply
2689                                 'nconc
2690                                 (list (caar ts))
2691                                 (let ((ps perms)
2692                                       outp)
2693                                   (while ps
2694                                     (setq outp
2695                                           (cons
2696                                            (vector
2697                                             (caar ps)
2698                                             (list
2699                                              'gnus-summary-score-entry
2700                                              (nth 1 header)
2701                                              (if (or (string= (nth 1 header)
2702                                                               "head")
2703                                                      (string= (nth 1 header)
2704                                                               "body"))
2705                                                  ""
2706                                                (list 'gnus-summary-header
2707                                                      (nth 1 header)))
2708                                              (list 'quote (nth 1 (car ts)))
2709                                              (list 'gnus-score-delta-default
2710                                                    nil)
2711                                              (nth 1 (car ps))
2712                                              t)
2713                                             t)
2714                                            outp))
2715                                     (setq ps (cdr ps)))
2716                                   (list (nreverse outp))))
2717                                outt))
2718                         (setq ts (cdr ts)))
2719                       (list (nreverse outt))))
2720                    outh))
2721             (setq headers (cdr headers)))
2722           (list (nreverse outh))))))))
2723
2724 \f
2725
2726 (defun gnus-summary-mode (&optional group)
2727   "Major mode for reading articles.
2728
2729 All normal editing commands are switched off.
2730 \\<gnus-summary-mode-map>
2731 Each line in this buffer represents one article.  To read an
2732 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2733 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2734 respectively.
2735
2736 You can also post articles and send mail from this buffer.  To
2737 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2738 of an article, type `\\[gnus-summary-reply]'.
2739
2740 There are approx. one gazillion commands you can execute in this
2741 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2742
2743 The following commands are available:
2744
2745 \\{gnus-summary-mode-map}"
2746   (interactive)
2747   (kill-all-local-variables)
2748   (when (gnus-visual-p 'summary-menu 'menu)
2749     (gnus-summary-make-menu-bar)
2750     (gnus-summary-make-tool-bar))
2751   (gnus-summary-make-local-variables)
2752   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2753     (gnus-summary-make-local-variables))
2754   (gnus-make-thread-indent-array)
2755   (gnus-simplify-mode-line)
2756   (setq major-mode 'gnus-summary-mode)
2757   (setq mode-name "Summary")
2758   (make-local-variable 'minor-mode-alist)
2759   (use-local-map gnus-summary-mode-map)
2760   (buffer-disable-undo)
2761   (setq buffer-read-only t              ;Disable modification
2762         show-trailing-whitespace nil)
2763   (setq truncate-lines t)
2764   (setq selective-display t)
2765   (setq selective-display-ellipses t)   ;Display `...'
2766   (gnus-summary-set-display-table)
2767   (gnus-set-default-directory)
2768   (setq gnus-newsgroup-name group)
2769   (make-local-variable 'gnus-summary-line-format)
2770   (make-local-variable 'gnus-summary-line-format-spec)
2771   (make-local-variable 'gnus-summary-dummy-line-format)
2772   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2773   (make-local-variable 'gnus-summary-mark-positions)
2774   (gnus-make-local-hook 'pre-command-hook)
2775   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2776   (gnus-run-hooks 'gnus-summary-mode-hook)
2777   (turn-on-gnus-mailing-list-mode)
2778   (mm-enable-multibyte)
2779   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2780   (gnus-update-summary-mark-positions))
2781
2782 (defun gnus-summary-make-local-variables ()
2783   "Make all the local summary buffer variables."
2784   (let (global)
2785     (dolist (local gnus-summary-local-variables)
2786       (if (consp local)
2787           (progn
2788             (if (eq (cdr local) 'global)
2789                 ;; Copy the global value of the variable.
2790                 (setq global (symbol-value (car local)))
2791               ;; Use the value from the list.
2792               (setq global (eval (cdr local))))
2793             (set (make-local-variable (car local)) global))
2794         ;; Simple nil-valued local variable.
2795         (set (make-local-variable local) nil)))))
2796
2797 (defun gnus-summary-clear-local-variables ()
2798   (let ((locals gnus-summary-local-variables))
2799     (while locals
2800       (if (consp (car locals))
2801           (and (vectorp (caar locals))
2802                (set (caar locals) nil))
2803         (and (vectorp (car locals))
2804              (set (car locals) nil)))
2805       (setq locals (cdr locals)))))
2806
2807 ;; Summary data functions.
2808
2809 (defmacro gnus-data-number (data)
2810   `(car ,data))
2811
2812 (defmacro gnus-data-set-number (data number)
2813   `(setcar ,data ,number))
2814
2815 (defmacro gnus-data-mark (data)
2816   `(nth 1 ,data))
2817
2818 (defmacro gnus-data-set-mark (data mark)
2819   `(setcar (nthcdr 1 ,data) ,mark))
2820
2821 (defmacro gnus-data-pos (data)
2822   `(nth 2 ,data))
2823
2824 (defmacro gnus-data-set-pos (data pos)
2825   `(setcar (nthcdr 2 ,data) ,pos))
2826
2827 (defmacro gnus-data-header (data)
2828   `(nth 3 ,data))
2829
2830 (defmacro gnus-data-set-header (data header)
2831   `(setf (nth 3 ,data) ,header))
2832
2833 (defmacro gnus-data-level (data)
2834   `(nth 4 ,data))
2835
2836 (defmacro gnus-data-unread-p (data)
2837   `(= (nth 1 ,data) gnus-unread-mark))
2838
2839 (defmacro gnus-data-read-p (data)
2840   `(/= (nth 1 ,data) gnus-unread-mark))
2841
2842 (defmacro gnus-data-pseudo-p (data)
2843   `(consp (nth 3 ,data)))
2844
2845 (defmacro gnus-data-find (number)
2846   `(assq ,number gnus-newsgroup-data))
2847
2848 (defmacro gnus-data-find-list (number &optional data)
2849   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2850      (memq (assq ,number bdata)
2851            bdata)))
2852
2853 (defmacro gnus-data-make (number mark pos header level)
2854   `(list ,number ,mark ,pos ,header ,level))
2855
2856 (defun gnus-data-enter (after-article number mark pos header level offset)
2857   (let ((data (gnus-data-find-list after-article)))
2858     (unless data
2859       (error "No such article: %d" after-article))
2860     (setcdr data (cons (gnus-data-make number mark pos header level)
2861                        (cdr data)))
2862     (setq gnus-newsgroup-data-reverse nil)
2863     (gnus-data-update-list (cddr data) offset)))
2864
2865 (defun gnus-data-enter-list (after-article list &optional offset)
2866   (when list
2867     (let ((data (and after-article (gnus-data-find-list after-article)))
2868           (ilist list))
2869       (if (not (or data
2870                    after-article))
2871           (let ((odata gnus-newsgroup-data))
2872             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2873             (when offset
2874               (gnus-data-update-list odata offset)))
2875         ;; Find the last element in the list to be spliced into the main
2876         ;; list.
2877         (setq list (last list))
2878         (if (not data)
2879             (progn
2880               (setcdr list gnus-newsgroup-data)
2881               (setq gnus-newsgroup-data ilist)
2882               (when offset
2883                 (gnus-data-update-list (cdr list) offset)))
2884           (setcdr list (cdr data))
2885           (setcdr data ilist)
2886           (when offset
2887             (gnus-data-update-list (cdr list) offset))))
2888       (setq gnus-newsgroup-data-reverse nil))))
2889
2890 (defun gnus-data-remove (article &optional offset)
2891   (let ((data gnus-newsgroup-data))
2892     (if (= (gnus-data-number (car data)) article)
2893         (progn
2894           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2895                 gnus-newsgroup-data-reverse nil)
2896           (when offset
2897             (gnus-data-update-list gnus-newsgroup-data offset)))
2898       (while (cdr data)
2899         (when (= (gnus-data-number (cadr data)) article)
2900           (setcdr data (cddr data))
2901           (when offset
2902             (gnus-data-update-list (cdr data) offset))
2903           (setq data nil
2904                 gnus-newsgroup-data-reverse nil))
2905         (setq data (cdr data))))))
2906
2907 (defmacro gnus-data-list (backward)
2908   `(if ,backward
2909        (or gnus-newsgroup-data-reverse
2910            (setq gnus-newsgroup-data-reverse
2911                  (reverse gnus-newsgroup-data)))
2912      gnus-newsgroup-data))
2913
2914 (defun gnus-data-update-list (data offset)
2915   "Add OFFSET to the POS of all data entries in DATA."
2916   (setq gnus-newsgroup-data-reverse nil)
2917   (while data
2918     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2919     (setq data (cdr data))))
2920
2921 (defun gnus-summary-article-pseudo-p (article)
2922   "Say whether this article is a pseudo article or not."
2923   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2924
2925 (defmacro gnus-summary-article-sparse-p (article)
2926   "Say whether this article is a sparse article or not."
2927   `(memq ,article gnus-newsgroup-sparse))
2928
2929 (defmacro gnus-summary-article-ancient-p (article)
2930   "Say whether this article is a sparse article or not."
2931   `(memq ,article gnus-newsgroup-ancient))
2932
2933 (defun gnus-article-parent-p (number)
2934   "Say whether this article is a parent or not."
2935   (let ((data (gnus-data-find-list number)))
2936     (and (cdr data)              ; There has to be an article after...
2937          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2938             (gnus-data-level (nth 1 data))))))
2939
2940 (defun gnus-article-children (number)
2941   "Return a list of all children to NUMBER."
2942   (let* ((data (gnus-data-find-list number))
2943          (level (gnus-data-level (car data)))
2944          children)
2945     (setq data (cdr data))
2946     (while (and data
2947                 (= (gnus-data-level (car data)) (1+ level)))
2948       (push (gnus-data-number (car data)) children)
2949       (setq data (cdr data)))
2950     children))
2951
2952 (defmacro gnus-summary-skip-intangible ()
2953   "If the current article is intangible, then jump to a different article."
2954   '(let ((to (get-text-property (point) 'gnus-intangible)))
2955      (and to (gnus-summary-goto-subject to))))
2956
2957 (defmacro gnus-summary-article-intangible-p ()
2958   "Say whether this article is intangible or not."
2959   '(get-text-property (point) 'gnus-intangible))
2960
2961 (defun gnus-article-read-p (article)
2962   "Say whether ARTICLE is read or not."
2963   (not (or (memq article gnus-newsgroup-marked)
2964            (memq article gnus-newsgroup-spam-marked)
2965            (memq article gnus-newsgroup-unreads)
2966            (memq article gnus-newsgroup-unselected)
2967            (memq article gnus-newsgroup-dormant))))
2968
2969 ;; Some summary mode macros.
2970
2971 (defmacro gnus-summary-article-number ()
2972   "The article number of the article on the current line.
2973 If there isn't an article number here, then we return the current
2974 article number."
2975   '(progn
2976      (gnus-summary-skip-intangible)
2977      (or (get-text-property (point) 'gnus-number)
2978          (gnus-summary-last-subject))))
2979
2980 (defmacro gnus-summary-article-header (&optional number)
2981   "Return the header of article NUMBER."
2982   `(gnus-data-header (gnus-data-find
2983                       ,(or number '(gnus-summary-article-number)))))
2984
2985 (defmacro gnus-summary-thread-level (&optional number)
2986   "Return the level of thread that starts with article NUMBER."
2987   `(if (and (eq gnus-summary-make-false-root 'dummy)
2988             (get-text-property (point) 'gnus-intangible))
2989        0
2990      (gnus-data-level (gnus-data-find
2991                        ,(or number '(gnus-summary-article-number))))))
2992
2993 (defmacro gnus-summary-article-mark (&optional number)
2994   "Return the mark of article NUMBER."
2995   `(gnus-data-mark (gnus-data-find
2996                     ,(or number '(gnus-summary-article-number)))))
2997
2998 (defmacro gnus-summary-article-pos (&optional number)
2999   "Return the position of the line of article NUMBER."
3000   `(gnus-data-pos (gnus-data-find
3001                    ,(or number '(gnus-summary-article-number)))))
3002
3003 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3004 (defmacro gnus-summary-article-subject (&optional number)
3005   "Return current subject string or nil if nothing."
3006   `(let ((headers
3007           ,(if number
3008                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3009              '(gnus-data-header (assq (gnus-summary-article-number)
3010                                       gnus-newsgroup-data)))))
3011      (and headers
3012           (vectorp headers)
3013           (mail-header-subject headers))))
3014
3015 (defmacro gnus-summary-article-score (&optional number)
3016   "Return current article score."
3017   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3018                   gnus-newsgroup-scored))
3019        gnus-summary-default-score 0))
3020
3021 (defun gnus-summary-article-children (&optional number)
3022   "Return a list of article numbers that are children of article NUMBER."
3023   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3024          (level (gnus-data-level (car data)))
3025          l children)
3026     (while (and (setq data (cdr data))
3027                 (> (setq l (gnus-data-level (car data))) level))
3028       (and (= (1+ level) l)
3029            (push (gnus-data-number (car data))
3030                  children)))
3031     (nreverse children)))
3032
3033 (defun gnus-summary-article-parent (&optional number)
3034   "Return the article number of the parent of article NUMBER."
3035   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3036                                     (gnus-data-list t)))
3037          (level (gnus-data-level (car data))))
3038     (if (zerop level)
3039         ()                              ; This is a root.
3040       ;; We search until we find an article with a level less than
3041       ;; this one.  That function has to be the parent.
3042       (while (and (setq data (cdr data))
3043                   (not (< (gnus-data-level (car data)) level))))
3044       (and data (gnus-data-number (car data))))))
3045
3046 (defun gnus-unread-mark-p (mark)
3047   "Say whether MARK is the unread mark."
3048   (= mark gnus-unread-mark))
3049
3050 (defun gnus-read-mark-p (mark)
3051   "Say whether MARK is one of the marks that mark as read.
3052 This is all marks except unread, ticked, dormant, and expirable."
3053   (not (or (= mark gnus-unread-mark)
3054            (= mark gnus-ticked-mark)
3055            (= mark gnus-spam-mark)
3056            (= mark gnus-dormant-mark)
3057            (= mark gnus-expirable-mark))))
3058
3059 (defmacro gnus-article-mark (number)
3060   "Return the MARK of article NUMBER.
3061 This macro should only be used when computing the mark the \"first\"
3062 time; i.e., when generating the summary lines.  After that,
3063 `gnus-summary-article-mark' should be used to examine the
3064 marks of articles."
3065   `(cond
3066     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3067     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3068     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3069     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3070     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3071     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3072     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3073     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3074            gnus-ancient-mark))))
3075
3076 ;; Saving hidden threads.
3077
3078 (defmacro gnus-save-hidden-threads (&rest forms)
3079   "Save hidden threads, eval FORMS, and restore the hidden threads."
3080   (let ((config (make-symbol "config")))
3081     `(let ((,config (gnus-hidden-threads-configuration)))
3082        (unwind-protect
3083            (save-excursion
3084              ,@forms)
3085          (gnus-restore-hidden-threads-configuration ,config)))))
3086 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3087 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3088
3089 (defun gnus-data-compute-positions ()
3090   "Compute the positions of all articles."
3091   (setq gnus-newsgroup-data-reverse nil)
3092   (let ((data gnus-newsgroup-data))
3093     (save-excursion
3094       (gnus-save-hidden-threads
3095         (gnus-summary-show-all-threads)
3096         (goto-char (point-min))
3097         (while data
3098           (while (get-text-property (point) 'gnus-intangible)
3099             (forward-line 1))
3100           (gnus-data-set-pos (car data) (+ (point) 3))
3101           (setq data (cdr data))
3102           (forward-line 1))))))
3103
3104 (defun gnus-hidden-threads-configuration ()
3105   "Return the current hidden threads configuration."
3106   (save-excursion
3107     (let (config)
3108       (goto-char (point-min))
3109       (while (search-forward "\r" nil t)
3110         (push (1- (point)) config))
3111       config)))
3112
3113 (defun gnus-restore-hidden-threads-configuration (config)
3114   "Restore hidden threads configuration from CONFIG."
3115   (save-excursion
3116     (let (point buffer-read-only)
3117       (while (setq point (pop config))
3118         (when (and (< point (point-max))
3119                    (goto-char point)
3120                    (eq (char-after) ?\n))
3121           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3122
3123 ;; Various summary mode internalish functions.
3124
3125 (defun gnus-mouse-pick-article (e)
3126   (interactive "e")
3127   (mouse-set-point e)
3128   (gnus-summary-next-page nil t))
3129
3130 (defun gnus-summary-set-display-table ()
3131   "Change the display table.
3132 Odd characters have a tendency to mess
3133 up nicely formatted displays - we make all possible glyphs
3134 display only a single character."
3135
3136   ;; We start from the standard display table, if any.
3137   (let ((table (or (copy-sequence standard-display-table)
3138                    (make-display-table)))
3139         (i 32))
3140     ;; Nix out all the control chars...
3141     (while (>= (setq i (1- i)) 0)
3142       (aset table i [??]))
3143    ;; ... but not newline and cr, of course.  (cr is necessary for the
3144     ;; selective display).
3145     (aset table ?\n nil)
3146     (aset table ?\r nil)
3147     ;; We keep TAB as well.
3148     (aset table ?\t nil)
3149     ;; We nix out any glyphs over 126 that are not set already.
3150     (let ((i 256))
3151       (while (>= (setq i (1- i)) 127)
3152         ;; Only modify if the entry is nil.
3153         (unless (aref table i)
3154           (aset table i [??]))))
3155     (setq buffer-display-table table)))
3156
3157 (defun gnus-summary-set-article-display-arrow (pos)
3158   "Update the overlay arrow to point to line at position POS."
3159   (when (and gnus-summary-display-arrow
3160              (boundp 'overlay-arrow-position)
3161              (boundp 'overlay-arrow-string))
3162     (save-excursion
3163       (goto-char pos)
3164       (beginning-of-line)
3165       (unless overlay-arrow-position
3166         (setq overlay-arrow-position (make-marker)))
3167       (setq overlay-arrow-string "=>"
3168             overlay-arrow-position (set-marker overlay-arrow-position
3169                                                (point)
3170                                                (current-buffer))))))
3171
3172 (defun gnus-summary-setup-buffer (group)
3173   "Initialize summary buffer."
3174   (let ((buffer (gnus-summary-buffer-name group))
3175         (dead-name (concat "*Dead Summary "
3176                            (gnus-group-decoded-name group) "*")))
3177     ;; If a dead summary buffer exists, we kill it.
3178     (when (gnus-buffer-live-p dead-name)
3179       (gnus-kill-buffer dead-name))
3180     (if (get-buffer buffer)
3181         (progn
3182           (set-buffer buffer)
3183           (setq gnus-summary-buffer (current-buffer))
3184           (not gnus-newsgroup-prepared))
3185       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3186       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3187       (gnus-summary-mode group)
3188       (when gnus-carpal
3189         (gnus-carpal-setup-buffer 'summary))
3190       (unless gnus-single-article-buffer
3191         (make-local-variable 'gnus-article-buffer)
3192         (make-local-variable 'gnus-article-current)
3193         (make-local-variable 'gnus-original-article-buffer))
3194       (setq gnus-newsgroup-name group)
3195       ;; Set any local variables in the group parameters.
3196       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3197       t)))
3198
3199 (defun gnus-set-global-variables ()
3200   "Set the global equivalents of the buffer-local variables.
3201 They are set to the latest values they had.  These reflect the summary
3202 buffer that was in action when the last article was fetched."
3203   (when (eq major-mode 'gnus-summary-mode)
3204     (setq gnus-summary-buffer (current-buffer))
3205     (let ((name gnus-newsgroup-name)
3206           (marked gnus-newsgroup-marked)
3207           (spam gnus-newsgroup-spam-marked)
3208           (unread gnus-newsgroup-unreads)
3209           (headers gnus-current-headers)
3210           (data gnus-newsgroup-data)
3211           (summary gnus-summary-buffer)
3212           (article-buffer gnus-article-buffer)
3213           (original gnus-original-article-buffer)
3214           (gac gnus-article-current)
3215           (reffed gnus-reffed-article-number)
3216           (score-file gnus-current-score-file)
3217           (default-charset gnus-newsgroup-charset)
3218           vlist)
3219       (let ((locals gnus-newsgroup-variables))
3220         (while locals
3221           (if (consp (car locals))
3222               (push (eval (caar locals)) vlist)
3223             (push (eval (car locals)) vlist))
3224           (setq locals (cdr locals)))
3225         (setq vlist (nreverse vlist)))
3226       (with-current-buffer gnus-group-buffer
3227         (setq gnus-newsgroup-name name
3228               gnus-newsgroup-marked marked
3229               gnus-newsgroup-spam-marked spam
3230               gnus-newsgroup-unreads unread
3231               gnus-current-headers headers
3232               gnus-newsgroup-data data
3233               gnus-article-current gac
3234               gnus-summary-buffer summary
3235               gnus-article-buffer article-buffer
3236               gnus-original-article-buffer original
3237               gnus-reffed-article-number reffed
3238               gnus-current-score-file score-file
3239               gnus-newsgroup-charset default-charset)
3240         (let ((locals gnus-newsgroup-variables))
3241           (while locals
3242             (if (consp (car locals))
3243                 (set (caar locals) (pop vlist))
3244               (set (car locals) (pop vlist)))
3245             (setq locals (cdr locals))))
3246         ;; The article buffer also has local variables.
3247         (when (gnus-buffer-live-p gnus-article-buffer)
3248           (set-buffer gnus-article-buffer)
3249           (setq gnus-summary-buffer summary))))))
3250
3251 (defun gnus-summary-article-unread-p (article)
3252   "Say whether ARTICLE is unread or not."
3253   (memq article gnus-newsgroup-unreads))
3254
3255 (defun gnus-summary-first-article-p (&optional article)
3256   "Return whether ARTICLE is the first article in the buffer."
3257   (if (not (setq article (or article (gnus-summary-article-number))))
3258       nil
3259     (eq article (caar gnus-newsgroup-data))))
3260
3261 (defun gnus-summary-last-article-p (&optional article)
3262   "Return whether ARTICLE is the last article in the buffer."
3263   (if (not (setq article (or article (gnus-summary-article-number))))
3264       ;; All non-existent numbers are the last article.  :-)
3265       t
3266     (not (cdr (gnus-data-find-list article)))))
3267
3268 (defun gnus-make-thread-indent-array ()
3269   (let ((n 200))
3270     (unless (and gnus-thread-indent-array
3271                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3272       (setq gnus-thread-indent-array (make-vector 201 "")
3273             gnus-thread-indent-array-level gnus-thread-indent-level)
3274       (while (>= n 0)
3275         (aset gnus-thread-indent-array n
3276               (make-string (* n gnus-thread-indent-level) ? ))
3277         (setq n (1- n))))))
3278
3279 (defun gnus-update-summary-mark-positions ()
3280   "Compute where the summary marks are to go."
3281   (save-excursion
3282     (when (gnus-buffer-exists-p gnus-summary-buffer)
3283       (set-buffer gnus-summary-buffer))
3284     (let ((gnus-replied-mark 129)
3285           (gnus-score-below-mark 130)
3286           (gnus-score-over-mark 130)
3287           (gnus-undownloaded-mark 131)
3288           (spec gnus-summary-line-format-spec)
3289           gnus-visual pos)
3290       (save-excursion
3291         (gnus-set-work-buffer)
3292         (let ((gnus-summary-line-format-spec spec)
3293               (gnus-newsgroup-downloadable '(0)))
3294           (gnus-summary-insert-line
3295            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3296            0 nil t 128 t nil "" nil 1)
3297           (goto-char (point-min))
3298           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3299                                              (- (point) (point-min) 1)))))
3300           (goto-char (point-min))
3301           (push (cons 'replied (and (search-forward "\201" nil t)
3302                                     (- (point) (point-min) 1)))
3303                 pos)
3304           (goto-char (point-min))
3305           (push (cons 'score (and (search-forward "\202" nil t)
3306                                   (- (point) (point-min) 1)))
3307                 pos)
3308           (goto-char (point-min))
3309           (push (cons 'download
3310                       (and (search-forward "\203" nil t)
3311                            (- (point) (point-min) 1)))
3312                 pos)))
3313       (setq gnus-summary-mark-positions pos))))
3314
3315 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3316   "Insert a dummy root in the summary buffer."
3317   (beginning-of-line)
3318   (gnus-add-text-properties
3319    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3320    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3321
3322 (defun gnus-summary-extract-address-component (from)
3323   (or (car (funcall gnus-extract-address-components from))
3324       from))
3325
3326 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3327   (let ((mail-parse-charset gnus-newsgroup-charset)
3328         ; Is it really necessary to do this next part for each summary line?
3329         ; Luckily, doesn't seem to slow things down much.
3330         (mail-parse-ignored-charsets
3331          (with-current-buffer gnus-summary-buffer
3332            gnus-newsgroup-ignored-charsets)))
3333     (or
3334      (and gnus-ignored-from-addresses
3335           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3336           (let ((extra-headers (mail-header-extra header))
3337                 to
3338                 newsgroups)
3339             (cond
3340              ((setq to (cdr (assq 'To extra-headers)))
3341               (concat "-> "
3342                       (inline
3343                         (gnus-summary-extract-address-component
3344                          (funcall gnus-decode-encoded-word-function to)))))
3345              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3346               (concat "=> " newsgroups)))))
3347      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3348
3349 (defun gnus-summary-insert-line (gnus-tmp-header
3350                                  gnus-tmp-level gnus-tmp-current
3351                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3352                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3353                                  &optional gnus-tmp-dummy gnus-tmp-score
3354                                  gnus-tmp-process)
3355   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3356          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3357          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3358          (gnus-tmp-score-char
3359           (if (or (null gnus-summary-default-score)
3360                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3361                       gnus-summary-zcore-fuzz))
3362               ?                         ;Whitespace
3363             (if (< gnus-tmp-score gnus-summary-default-score)
3364                 gnus-score-below-mark gnus-score-over-mark)))
3365          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3366          (gnus-tmp-replied
3367           (cond (gnus-tmp-process gnus-process-mark)
3368                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3369                  gnus-cached-mark)
3370                 (gnus-tmp-replied gnus-replied-mark)
3371                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3372                  gnus-forwarded-mark)
3373                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3374                  gnus-saved-mark)
3375                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3376                  gnus-recent-mark)
3377                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3378                  gnus-unseen-mark)
3379                 (t gnus-no-mark)))
3380          (gnus-tmp-downloaded
3381           (cond (undownloaded
3382                  gnus-undownloaded-mark)
3383                 (gnus-newsgroup-agentized
3384                  gnus-downloaded-mark)
3385                 (t
3386                  gnus-no-mark)))
3387          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3388          (gnus-tmp-name
3389           (cond
3390            ((string-match "<[^>]+> *$" gnus-tmp-from)
3391             (let ((beg (match-beginning 0)))
3392               (or (and (string-match "^\".+\"" gnus-tmp-from)
3393                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3394                   (substring gnus-tmp-from 0 beg))))
3395            ((string-match "(.+)" gnus-tmp-from)
3396             (substring gnus-tmp-from
3397                        (1+ (match-beginning 0)) (1- (match-end 0))))
3398            (t gnus-tmp-from)))
3399          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3400          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3401          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3402          (buffer-read-only nil))
3403     (when (string= gnus-tmp-name "")
3404       (setq gnus-tmp-name gnus-tmp-from))
3405     (unless (numberp gnus-tmp-lines)
3406       (setq gnus-tmp-lines -1))
3407     (if (= gnus-tmp-lines -1)
3408         (setq gnus-tmp-lines "?")
3409       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3410       (gnus-put-text-property
3411      (point)
3412      (progn (eval gnus-summary-line-format-spec) (point))
3413        'gnus-number gnus-tmp-number)
3414     (when (gnus-visual-p 'summary-highlight 'highlight)
3415       (forward-line -1)
3416       (gnus-run-hooks 'gnus-summary-update-hook)
3417       (forward-line 1))))
3418
3419 (defun gnus-summary-update-line (&optional dont-update)
3420   "Update summary line after change."
3421   (when (and gnus-summary-default-score
3422              (not gnus-summary-inhibit-highlight))
3423     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3424            (article (gnus-summary-article-number))
3425            (score (gnus-summary-article-score article)))
3426       (unless dont-update
3427         (if (and gnus-summary-mark-below
3428                  (< (gnus-summary-article-score)
3429                     gnus-summary-mark-below))
3430             ;; This article has a low score, so we mark it as read.
3431             (when (memq article gnus-newsgroup-unreads)
3432               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3433           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3434             ;; This article was previously marked as read on account
3435             ;; of a low score, but now it has risen, so we mark it as
3436             ;; unread.
3437             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3438         (gnus-summary-update-mark
3439          (if (or (null gnus-summary-default-score)
3440                  (<= (abs (- score gnus-summary-default-score))
3441                      gnus-summary-zcore-fuzz))
3442              ?                          ;Whitespace
3443            (if (< score gnus-summary-default-score)
3444                gnus-score-below-mark gnus-score-over-mark))
3445          'score))
3446       ;; Do visual highlighting.
3447       (when (gnus-visual-p 'summary-highlight 'highlight)
3448         (gnus-run-hooks 'gnus-summary-update-hook)))))
3449
3450 (defvar gnus-tmp-new-adopts nil)
3451
3452 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3453   "Return the number of articles in THREAD.
3454 This may be 0 in some cases -- if none of the articles in
3455 the thread are to be displayed."
3456   (let* ((number
3457          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3458           (cond
3459            ((not (listp thread))
3460             1)
3461            ((and (consp thread) (cdr thread))
3462             (apply
3463              '+ 1 (mapcar
3464                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3465            ((null thread)
3466             1)
3467            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3468             1)
3469            (t 0))))
3470     (when (and level (zerop level) gnus-tmp-new-adopts)
3471       (incf number
3472             (apply '+ (mapcar
3473                        'gnus-summary-number-of-articles-in-thread
3474                        gnus-tmp-new-adopts))))
3475     (if char
3476         (if (> number 1) gnus-not-empty-thread-mark
3477           gnus-empty-thread-mark)
3478       number)))
3479
3480 (defsubst gnus-summary-line-message-size (head)
3481   "Return pretty-printed version of message size.
3482 This function is intended to be used in
3483 `gnus-summary-line-format-alist'."
3484   (let ((c (or (mail-header-chars head) -1)))
3485     (cond ((< c 0) "n/a")               ; chars not available
3486           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3487           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3488           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3489           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3490
3491
3492 (defun gnus-summary-set-local-parameters (group)
3493   "Go through the local params of GROUP and set all variable specs in that list."
3494   (let ((vars '(quit-config)))          ; Ignore quit-config.
3495     (dolist (elem (gnus-group-find-parameter group))
3496       (and (consp elem)                 ; Has to be a cons.
3497            (consp (cdr elem))           ; The cdr has to be a list.
3498            (symbolp (car elem))         ; Has to be a symbol in there.
3499            (not (memq (car elem) vars))
3500            (ignore-errors               ; So we set it.
3501              (push (car elem) vars)
3502              (make-local-variable (car elem))
3503              (set (car elem) (eval (nth 1 elem))))))))
3504
3505 (defun gnus-summary-read-group (group &optional show-all no-article
3506                                       kill-buffer no-display backward
3507                                       select-articles)
3508   "Start reading news in newsgroup GROUP.
3509 If SHOW-ALL is non-nil, already read articles are also listed.
3510 If NO-ARTICLE is non-nil, no article is selected initially.
3511 If NO-DISPLAY, don't generate a summary buffer."
3512   (let (result)
3513     (while (and group
3514                 (null (setq result
3515                             (let ((gnus-auto-select-next nil))
3516                               (or (gnus-summary-read-group-1
3517                                    group show-all no-article
3518                                    kill-buffer no-display
3519                                    select-articles)
3520                                   (setq show-all nil
3521                                         select-articles nil)))))
3522                 (eq gnus-auto-select-next 'quietly))
3523       (set-buffer gnus-group-buffer)
3524       ;; The entry function called above goes to the next
3525       ;; group automatically, so we go two groups back
3526       ;; if we are searching for the previous group.
3527       (when backward
3528         (gnus-group-prev-unread-group 2))
3529       (if (not (equal group (gnus-group-group-name)))
3530           (setq group (gnus-group-group-name))
3531         (setq group nil)))
3532     result))
3533
3534 (defun gnus-summary-read-group-1 (group show-all no-article
3535                                         kill-buffer no-display
3536                                         &optional select-articles)
3537   ;; Killed foreign groups can't be entered.
3538   ;;  (when (and (not (gnus-group-native-p group))
3539   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3540   ;;    (error "Dead non-native groups can't be entered"))
3541   (gnus-message 5 "Retrieving newsgroup: %s..."
3542                 (gnus-group-decoded-name group))
3543   (let* ((new-group (gnus-summary-setup-buffer group))
3544          (quit-config (gnus-group-quit-config group))
3545          (did-select (and new-group (gnus-select-newsgroup
3546                                      group show-all select-articles))))
3547     (cond
3548      ;; This summary buffer exists already, so we just select it.
3549      ((not new-group)
3550       (gnus-set-global-variables)
3551       (when kill-buffer
3552         (gnus-kill-or-deaden-summary kill-buffer))
3553       (gnus-configure-windows 'summary 'force)
3554       (gnus-set-mode-line 'summary)
3555       (gnus-summary-position-point)
3556       (message "")
3557       t)
3558      ;; We couldn't select this group.
3559      ((null did-select)
3560       (when (and (eq major-mode 'gnus-summary-mode)
3561                  (not (equal (current-buffer) kill-buffer)))
3562         (kill-buffer (current-buffer))
3563         (if (not quit-config)
3564             (progn
3565               ;; Update the info -- marks might need to be removed,
3566               ;; for instance.
3567               (gnus-summary-update-info)
3568               (set-buffer gnus-group-buffer)
3569               (gnus-group-jump-to-group group)
3570               (gnus-group-next-unread-group 1))
3571           (gnus-handle-ephemeral-exit quit-config)))
3572       (let ((grpinfo (gnus-get-info group)))
3573         (if (null (gnus-info-read grpinfo))
3574             (gnus-message 3 "Group %s contains no messages"
3575                           (gnus-group-decoded-name group))
3576           (gnus-message 3 "Can't select group")))
3577       nil)
3578      ;; The user did a `C-g' while prompting for number of articles,
3579      ;; so we exit this group.
3580      ((eq did-select 'quit)
3581       (and (eq major-mode 'gnus-summary-mode)
3582            (not (equal (current-buffer) kill-buffer))
3583            (kill-buffer (current-buffer)))
3584       (when kill-buffer
3585         (gnus-kill-or-deaden-summary kill-buffer))
3586       (if (not quit-config)
3587           (progn
3588             (set-buffer gnus-group-buffer)
3589             (gnus-group-jump-to-group group)
3590             (gnus-group-next-unread-group 1)
3591             (gnus-configure-windows 'group 'force))
3592         (gnus-handle-ephemeral-exit quit-config))
3593       ;; Finally signal the quit.
3594       (signal 'quit nil))
3595      ;; The group was successfully selected.
3596      (t
3597       (gnus-set-global-variables)
3598       ;; Save the active value in effect when the group was entered.
3599       (setq gnus-newsgroup-active
3600             (gnus-copy-sequence
3601              (gnus-active gnus-newsgroup-name)))
3602       ;; You can change the summary buffer in some way with this hook.
3603       (gnus-run-hooks 'gnus-select-group-hook)
3604       (gnus-update-format-specifications
3605        nil 'summary 'summary-mode 'summary-dummy)
3606       (gnus-update-summary-mark-positions)
3607       ;; Do score processing.
3608       (when gnus-use-scoring
3609         (gnus-possibly-score-headers))
3610       ;; Check whether to fill in the gaps in the threads.
3611       (when gnus-build-sparse-threads
3612         (gnus-build-sparse-threads))
3613       ;; Find the initial limit.
3614       (if show-all
3615           (let ((gnus-newsgroup-dormant nil))
3616             (gnus-summary-initial-limit show-all))
3617         (gnus-summary-initial-limit show-all))
3618       ;; Generate the summary buffer.
3619       (unless no-display
3620         (gnus-summary-prepare))
3621       (when gnus-use-trees
3622         (gnus-tree-open group)
3623         (setq gnus-summary-highlight-line-function
3624               'gnus-tree-highlight-article))
3625       ;; If the summary buffer is empty, but there are some low-scored
3626       ;; articles or some excluded dormants, we include these in the
3627       ;; buffer.
3628       (when (and (zerop (buffer-size))
3629                  (not no-display))
3630         (cond (gnus-newsgroup-dormant
3631                (gnus-summary-limit-include-dormant))
3632               ((and gnus-newsgroup-scored show-all)
3633                (gnus-summary-limit-include-expunged t))))
3634       ;; Function `gnus-apply-kill-file' must be called in this hook.
3635       (gnus-run-hooks 'gnus-apply-kill-hook)
3636       (if (and (zerop (buffer-size))
3637                (not no-display))
3638           (progn
3639             ;; This newsgroup is empty.
3640             (gnus-summary-catchup-and-exit nil t)
3641             (gnus-message 6 "No unread news")
3642             (when kill-buffer
3643               (gnus-kill-or-deaden-summary kill-buffer))
3644             ;; Return nil from this function.
3645             nil)
3646         ;; Hide conversation thread subtrees.  We cannot do this in
3647         ;; gnus-summary-prepare-hook since kill processing may not
3648         ;; work with hidden articles.
3649         (gnus-summary-maybe-hide-threads)
3650         (when kill-buffer
3651           (gnus-kill-or-deaden-summary kill-buffer))
3652         (gnus-summary-auto-select-subject)
3653         ;; Show first unread article if requested.
3654         (if (and (not no-article)
3655                  (not no-display)
3656                  gnus-newsgroup-unreads
3657                  gnus-auto-select-first)
3658             (progn
3659               (gnus-configure-windows 'summary)
3660               (let ((art (gnus-summary-article-number)))
3661                 (unless (and (not gnus-plugged)
3662                              (or (memq art gnus-newsgroup-undownloaded)
3663                                  (memq art gnus-newsgroup-downloadable)))
3664                   (gnus-summary-goto-article art))))
3665           ;; Don't select any articles.
3666           (gnus-summary-position-point)
3667           (gnus-configure-windows 'summary 'force)
3668           (gnus-set-mode-line 'summary))
3669         (when (and gnus-auto-center-group
3670                    (get-buffer-window gnus-group-buffer t))
3671           ;; Gotta use windows, because recenter does weird stuff if
3672           ;; the current buffer ain't the displayed window.
3673           (let ((owin (selected-window)))
3674             (select-window (get-buffer-window gnus-group-buffer t))
3675             (when (gnus-group-goto-group group)
3676               (recenter))
3677             (select-window owin)))
3678         ;; Mark this buffer as "prepared".
3679         (setq gnus-newsgroup-prepared t)
3680         (gnus-run-hooks 'gnus-summary-prepared-hook)
3681         (unless (gnus-ephemeral-group-p group)
3682           (gnus-group-update-group group))
3683         t)))))
3684
3685 (defun gnus-summary-auto-select-subject ()
3686   "Select the subject line on initial group entry."
3687   (goto-char (point-min))
3688   (cond
3689    ((eq gnus-auto-select-subject 'best)
3690     (gnus-summary-best-unread-subject))
3691    ((eq gnus-auto-select-subject 'unread)
3692     (gnus-summary-first-unread-subject))
3693    ((eq gnus-auto-select-subject 'unseen)
3694     (gnus-summary-first-unseen-subject))
3695    ((eq gnus-auto-select-subject 'unseen-or-unread)
3696     (gnus-summary-first-unseen-or-unread-subject))
3697    ((eq gnus-auto-select-subject 'first)
3698     ;; Do nothing.
3699     )
3700    ((functionp gnus-auto-select-subject)
3701     (funcall gnus-auto-select-subject))))
3702
3703 (defun gnus-summary-prepare ()
3704   "Generate the summary buffer."
3705   (interactive)
3706   (let ((buffer-read-only nil))
3707     (erase-buffer)
3708     (setq gnus-newsgroup-data nil
3709           gnus-newsgroup-data-reverse nil)
3710     (gnus-run-hooks 'gnus-summary-generate-hook)
3711     ;; Generate the buffer, either with threads or without.
3712     (when gnus-newsgroup-headers
3713       (gnus-summary-prepare-threads
3714        (if gnus-show-threads
3715            (gnus-sort-gathered-threads
3716             (funcall gnus-summary-thread-gathering-function
3717                      (gnus-sort-threads
3718                       (gnus-cut-threads (gnus-make-threads)))))
3719          ;; Unthreaded display.
3720          (gnus-sort-articles gnus-newsgroup-headers))))
3721     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3722     ;; Call hooks for modifying summary buffer.
3723     (goto-char (point-min))
3724     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3725
3726 (defsubst gnus-general-simplify-subject (subject)
3727   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3728   (setq subject
3729         (cond
3730          ;; Truncate the subject.
3731          (gnus-simplify-subject-functions
3732           (gnus-map-function gnus-simplify-subject-functions subject))
3733          ((numberp gnus-summary-gather-subject-limit)
3734           (setq subject (gnus-simplify-subject-re subject))
3735           (if (> (length subject) gnus-summary-gather-subject-limit)
3736               (substring subject 0 gnus-summary-gather-subject-limit)
3737             subject))
3738          ;; Fuzzily simplify it.
3739          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3740           (gnus-simplify-subject-fuzzy subject))
3741          ;; Just remove the leading "Re:".
3742          (t
3743           (gnus-simplify-subject-re subject))))
3744
3745   (if (and gnus-summary-gather-exclude-subject
3746            (string-match gnus-summary-gather-exclude-subject subject))
3747       nil                         ; This article shouldn't be gathered
3748     subject))
3749
3750 (defun gnus-summary-simplify-subject-query ()
3751   "Query where the respool algorithm would put this article."
3752   (interactive)
3753   (gnus-summary-select-article)
3754   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3755
3756 (defun gnus-gather-threads-by-subject (threads)
3757   "Gather threads by looking at Subject headers."
3758   (if (not gnus-summary-make-false-root)
3759       threads
3760     (let ((hashtb (gnus-make-hashtable 1024))
3761           (prev threads)
3762           (result threads)
3763           subject hthread whole-subject)
3764       (while threads
3765         (setq subject (gnus-general-simplify-subject
3766                        (setq whole-subject (mail-header-subject
3767                                             (caar threads)))))
3768         (when subject
3769           (if (setq hthread (gnus-gethash subject hashtb))
3770               (progn
3771                 ;; We enter a dummy root into the thread, if we
3772                 ;; haven't done that already.
3773                 (unless (stringp (caar hthread))
3774                   (setcar hthread (list whole-subject (car hthread))))
3775                 ;; We add this new gathered thread to this gathered
3776                 ;; thread.
3777                 (setcdr (car hthread)
3778                         (nconc (cdar hthread) (list (car threads))))
3779                 ;; Remove it from the list of threads.
3780                 (setcdr prev (cdr threads))
3781                 (setq threads prev))
3782             ;; Enter this thread into the hash table.
3783             (gnus-sethash subject
3784                           (if gnus-summary-make-false-root-always
3785                               (progn
3786                                 ;; If you want a dummy root above all
3787                                 ;; threads...
3788                                 (setcar threads (list whole-subject
3789                                                       (car threads)))
3790                                 threads)
3791                             threads)
3792                           hashtb)))
3793         (setq prev threads)
3794         (setq threads (cdr threads)))
3795       result)))
3796
3797 (defun gnus-gather-threads-by-references (threads)
3798   "Gather threads by looking at References headers."
3799   (let ((idhashtb (gnus-make-hashtable 1024))
3800         (thhashtb (gnus-make-hashtable 1024))
3801         (prev threads)
3802         (result threads)
3803         ids references id gthread gid entered ref)
3804     (while threads
3805       (when (setq references (mail-header-references (caar threads)))
3806         (setq id (mail-header-id (caar threads))
3807               ids (inline (gnus-split-references references))
3808               entered nil)
3809         (while (setq ref (pop ids))
3810           (setq ids (delete ref ids))
3811           (if (not (setq gid (gnus-gethash ref idhashtb)))
3812               (progn
3813                 (gnus-sethash ref id idhashtb)
3814                 (gnus-sethash id threads thhashtb))
3815             (setq gthread (gnus-gethash gid thhashtb))
3816             (unless entered
3817               ;; We enter a dummy root into the thread, if we
3818               ;; haven't done that already.
3819               (unless (stringp (caar gthread))
3820                 (setcar gthread (list (mail-header-subject (caar gthread))
3821                                       (car gthread))))
3822               ;; We add this new gathered thread to this gathered
3823               ;; thread.
3824               (setcdr (car gthread)
3825                       (nconc (cdar gthread) (list (car threads)))))
3826             ;; Add it into the thread hash table.
3827             (gnus-sethash id gthread thhashtb)
3828             (setq entered t)
3829             ;; Remove it from the list of threads.
3830             (setcdr prev (cdr threads))
3831             (setq threads prev))))
3832       (setq prev threads)
3833       (setq threads (cdr threads)))
3834     result))
3835
3836 (defun gnus-sort-gathered-threads (threads)
3837   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3838   (let ((result threads))
3839     (while threads
3840       (when (stringp (caar threads))
3841         (setcdr (car threads)
3842                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3843       (setq threads (cdr threads)))
3844     result))
3845
3846 (defun gnus-thread-loop-p (root thread)
3847   "Say whether ROOT is in THREAD."
3848   (let ((stack (list thread))
3849         (infloop 0)
3850         th)
3851     (while (setq thread (pop stack))
3852       (setq th (cdr thread))
3853       (while (and th
3854                   (not (eq (caar th) root)))
3855         (pop th))
3856       (if th
3857           ;; We have found a loop.
3858           (let (ref-dep)
3859             (setcdr thread (delq (car th) (cdr thread)))
3860             (if (boundp (setq ref-dep (intern "none"
3861                                               gnus-newsgroup-dependencies)))
3862                 (setcdr (symbol-value ref-dep)
3863                         (nconc (cdr (symbol-value ref-dep))
3864                                (list (car th))))
3865               (set ref-dep (list nil (car th))))
3866             (setq infloop 1
3867                   stack nil))
3868         ;; Push all the subthreads onto the stack.
3869         (push (cdr thread) stack)))
3870     infloop))
3871
3872 (defun gnus-make-threads ()
3873   "Go through the dependency hashtb and find the roots.  Return all threads."
3874   (let (threads)
3875     (while (catch 'infloop
3876              (mapatoms
3877               (lambda (refs)
3878                 ;; Deal with self-referencing References loops.
3879                 (when (and (car (symbol-value refs))
3880                            (not (zerop
3881                                  (apply
3882                                   '+
3883                                   (mapcar
3884                                    (lambda (thread)
3885                                      (gnus-thread-loop-p
3886                                       (car (symbol-value refs)) thread))
3887                                    (cdr (symbol-value refs)))))))
3888                   (setq threads nil)
3889                   (throw 'infloop t))
3890                 (unless (car (symbol-value refs))
3891                   ;; These threads do not refer back to any other
3892                   ;; articles, so they're roots.
3893                   (setq threads (append (cdr (symbol-value refs)) threads))))
3894               gnus-newsgroup-dependencies)))
3895     threads))
3896
3897 ;; Build the thread tree.
3898 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3899   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3900
3901 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3902 if it was already present.
3903
3904 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3905 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3906 Message-IDs will be renamed to a unique Message-ID before being
3907 entered.
3908
3909 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3910   (let* ((id (mail-header-id header))
3911          (id-dep (and id (intern id dependencies)))
3912          parent-id ref ref-dep ref-header replaced)
3913     ;; Enter this `header' in the `dependencies' table.
3914     (cond
3915      ((not id-dep)
3916       (setq header nil))
3917      ;; The first two cases do the normal part: enter a new `header'
3918      ;; in the `dependencies' table.
3919      ((not (boundp id-dep))
3920       (set id-dep (list header)))
3921      ((null (car (symbol-value id-dep)))
3922       (setcar (symbol-value id-dep) header))
3923
3924      ;; From here the `header' was already present in the
3925      ;; `dependencies' table.
3926      (force-new
3927       ;; Overrides an existing entry;
3928       ;; just set the header part of the entry.
3929       (setcar (symbol-value id-dep) header)
3930       (setq replaced t))
3931
3932      ;; Renames the existing `header' to a unique Message-ID.
3933      ((not gnus-summary-ignore-duplicates)
3934       ;; An article with this Message-ID has already been seen.
3935       ;; We rename the Message-ID.
3936       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3937            (list header))
3938       (mail-header-set-id header id))
3939
3940      ;; The last case ignores an existing entry, except it adds any
3941      ;; additional Xrefs (in case the two articles came from different
3942      ;; servers.
3943      ;; Also sets `header' to `nil' meaning that the `dependencies'
3944      ;; table was *not* modified.
3945      (t
3946       (mail-header-set-xref
3947        (car (symbol-value id-dep))
3948        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3949                    "")
3950                (or (mail-header-xref header) "")))
3951       (setq header nil)))
3952
3953     (when (and header (not replaced))
3954       ;; First check that we are not creating a References loop.
3955       (setq parent-id (gnus-parent-id (mail-header-references header)))
3956       (setq ref parent-id)
3957       (while (and ref
3958                   (setq ref-dep (intern-soft ref dependencies))
3959                   (boundp ref-dep)
3960                   (setq ref-header (car (symbol-value ref-dep))))
3961         (if (string= id ref)
3962             ;; Yuk!  This is a reference loop.  Make the article be a
3963             ;; root article.
3964             (progn
3965               (mail-header-set-references (car (symbol-value id-dep)) "none")
3966               (setq ref nil)
3967               (setq parent-id nil))
3968           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3969       (setq ref-dep (intern (or parent-id "none") dependencies))
3970       (if (boundp ref-dep)
3971           (setcdr (symbol-value ref-dep)
3972                   (nconc (cdr (symbol-value ref-dep))
3973                          (list (symbol-value id-dep))))
3974         (set ref-dep (list nil (symbol-value id-dep)))))
3975     header))
3976
3977 (defun gnus-extract-message-id-from-in-reply-to (string)
3978   (if (string-match "<[^>]+>" string)
3979       (substring string (match-beginning 0) (match-end 0))
3980     nil))
3981
3982 (defun gnus-build-sparse-threads ()
3983   (let ((headers gnus-newsgroup-headers)
3984         (mail-parse-charset gnus-newsgroup-charset)
3985         (gnus-summary-ignore-duplicates t)
3986         header references generation relations
3987         subject child end new-child date)
3988     ;; First we create an alist of generations/relations, where
3989     ;; generations is how much we trust the relation, and the relation
3990     ;; is parent/child.
3991     (gnus-message 7 "Making sparse threads...")
3992     (save-excursion
3993       (nnheader-set-temp-buffer " *gnus sparse threads*")
3994       (while (setq header (pop headers))
3995         (when (and (setq references (mail-header-references header))
3996                    (not (string= references "")))
3997           (insert references)
3998           (setq child (mail-header-id header)
3999                 subject (mail-header-subject header)
4000                 date (mail-header-date header)
4001                 generation 0)
4002           (while (search-backward ">" nil t)
4003             (setq end (1+ (point)))
4004             (when (search-backward "<" nil t)
4005               (setq new-child (buffer-substring (point) end))
4006               (push (list (incf generation)
4007                           child (setq child new-child)
4008                           subject date)
4009                     relations)))
4010           (when child
4011             (push (list (1+ generation) child nil subject) relations))
4012           (erase-buffer)))
4013       (kill-buffer (current-buffer)))
4014     ;; Sort over trustworthiness.
4015     (mapcar
4016      (lambda (relation)
4017        (when (gnus-dependencies-add-header
4018               (make-full-mail-header
4019                gnus-reffed-article-number
4020                (nth 3 relation) "" (or (nth 4 relation) "")
4021                (nth 1 relation)
4022                (or (nth 2 relation) "") 0 0 "")
4023               gnus-newsgroup-dependencies nil)
4024          (push gnus-reffed-article-number gnus-newsgroup-limit)
4025          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4026          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4027                gnus-newsgroup-reads)
4028          (decf gnus-reffed-article-number)))
4029      (sort relations 'car-less-than-car))
4030     (gnus-message 7 "Making sparse threads...done")))
4031
4032 (defun gnus-build-old-threads ()
4033   ;; Look at all the articles that refer back to old articles, and
4034   ;; fetch the headers for the articles that aren't there.  This will
4035   ;; build complete threads - if the roots haven't been expired by the
4036   ;; server, that is.
4037   (let ((mail-parse-charset gnus-newsgroup-charset)
4038         id heads)
4039     (mapatoms
4040      (lambda (refs)
4041        (when (not (car (symbol-value refs)))
4042          (setq heads (cdr (symbol-value refs)))
4043          (while heads
4044            (if (memq (mail-header-number (caar heads))
4045                      gnus-newsgroup-dormant)
4046                (setq heads (cdr heads))
4047              (setq id (symbol-name refs))
4048              (while (and (setq id (gnus-build-get-header id))
4049                          (not (car (gnus-id-to-thread id)))))
4050              (setq heads nil)))))
4051      gnus-newsgroup-dependencies)))
4052
4053 (defsubst gnus-remove-odd-characters (string)
4054   "Translate STRING into something that doesn't contain weird characters."
4055   (mm-subst-char-in-string
4056    ?\r ?\-
4057    (mm-subst-char-in-string ?\n ?\- string t) t))
4058
4059 ;; This function has to be called with point after the article number
4060 ;; on the beginning of the line.
4061 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4062   (let ((eol (point-at-eol))
4063         (buffer (current-buffer))
4064         header references in-reply-to)
4065
4066     ;; overview: [num subject from date id refs chars lines misc]
4067     (unwind-protect
4068         (let (x)
4069           (narrow-to-region (point) eol)
4070           (unless (eobp)
4071             (forward-char))
4072
4073           (setq header
4074                 (make-full-mail-header
4075                  number                 ; number
4076                  (condition-case ()     ; subject
4077                      (gnus-remove-odd-characters
4078                       (funcall gnus-decode-encoded-word-function
4079                                (setq x (nnheader-nov-field))))
4080                    (error x))
4081                  (condition-case ()     ; from
4082                      (gnus-remove-odd-characters
4083                       (funcall gnus-decode-encoded-word-function
4084                                (setq x (nnheader-nov-field))))
4085                    (error x))
4086                  (nnheader-nov-field)   ; date
4087                  (nnheader-nov-read-message-id number)  ; id
4088                  (setq references (nnheader-nov-field)) ; refs
4089                  (nnheader-nov-read-integer) ; chars
4090                  (nnheader-nov-read-integer) ; lines
4091                  (unless (eobp)
4092                    (if (looking-at "Xref: ")
4093                        (goto-char (match-end 0)))
4094                    (nnheader-nov-field)) ; Xref
4095                  (nnheader-nov-parse-extra)))) ; extra
4096
4097       (widen))
4098
4099     (when (and (string= references "")
4100                (setq in-reply-to (mail-header-extra header))
4101                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4102       (mail-header-set-references
4103        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4104
4105     (when gnus-alter-header-function
4106       (funcall gnus-alter-header-function header))
4107     (gnus-dependencies-add-header header dependencies force-new)))
4108
4109 (defun gnus-build-get-header (id)
4110   "Look through the buffer of NOV lines and find the header to ID.
4111 Enter this line into the dependencies hash table, and return
4112 the id of the parent article (if any)."
4113   (let ((deps gnus-newsgroup-dependencies)
4114         found header)
4115     (prog1
4116         (save-excursion
4117           (set-buffer nntp-server-buffer)
4118           (let ((case-fold-search nil))
4119             (goto-char (point-min))
4120             (while (and (not found)
4121                         (search-forward id nil t))
4122               (beginning-of-line)
4123               (setq found (looking-at
4124                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4125                                    (regexp-quote id))))
4126               (or found (beginning-of-line 2)))
4127             (when found
4128               (beginning-of-line)
4129               (and
4130                (setq header (gnus-nov-parse-line
4131                              (read (current-buffer)) deps))
4132                (gnus-parent-id (mail-header-references header))))))
4133       (when header
4134         (let ((number (mail-header-number header)))
4135           (push number gnus-newsgroup-limit)
4136           (push header gnus-newsgroup-headers)
4137           (if (memq number gnus-newsgroup-unselected)
4138               (progn
4139                 (setq gnus-newsgroup-unreads
4140                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4141                                                number))
4142                 (setq gnus-newsgroup-unselected
4143                       (delq number gnus-newsgroup-unselected)))
4144             (push number gnus-newsgroup-ancient)))))))
4145
4146 (defun gnus-build-all-threads ()
4147   "Read all the headers."
4148   (let ((gnus-summary-ignore-duplicates t)
4149         (mail-parse-charset gnus-newsgroup-charset)
4150         (dependencies gnus-newsgroup-dependencies)
4151         header article)
4152     (save-excursion
4153       (set-buffer nntp-server-buffer)
4154       (let ((case-fold-search nil))
4155         (goto-char (point-min))
4156         (while (not (eobp))
4157           (ignore-errors
4158             (setq article (read (current-buffer))
4159                   header (gnus-nov-parse-line article dependencies)))
4160           (when header
4161             (with-current-buffer gnus-summary-buffer
4162               (push header gnus-newsgroup-headers)
4163               (if (memq (setq article (mail-header-number header))
4164                         gnus-newsgroup-unselected)
4165                   (progn
4166                     (setq gnus-newsgroup-unreads
4167                           (gnus-add-to-sorted-list
4168                            gnus-newsgroup-unreads article))
4169                     (setq gnus-newsgroup-unselected
4170                           (delq article gnus-newsgroup-unselected)))
4171                 (push article gnus-newsgroup-ancient)))
4172             (forward-line 1)))))))
4173
4174 (defun gnus-summary-update-article-line (article header)
4175   "Update the line for ARTICLE using HEADER."
4176   (let* ((id (mail-header-id header))
4177          (thread (gnus-id-to-thread id)))
4178     (unless thread
4179       (error "Article in no thread"))
4180     ;; Update the thread.
4181     (setcar thread header)
4182     (gnus-summary-goto-subject article)
4183     (let* ((datal (gnus-data-find-list article))
4184            (data (car datal))
4185            (buffer-read-only nil)
4186            (level (gnus-summary-thread-level)))
4187       (gnus-delete-line)
4188       (let ((inserted (- (point)
4189                          (progn
4190                            (gnus-summary-insert-line
4191                             header level nil
4192                             (memq article gnus-newsgroup-undownloaded)
4193                             (gnus-article-mark article)
4194                             (memq article gnus-newsgroup-replied)
4195                             (memq article gnus-newsgroup-expirable)
4196                             ;; Only insert the Subject string when it's different
4197                             ;; from the previous Subject string.
4198                             (if (and
4199                                  gnus-show-threads
4200                                  (gnus-subject-equal
4201                                   (condition-case ()
4202                                       (mail-header-subject
4203                                        (gnus-data-header
4204                                         (cadr
4205                                          (gnus-data-find-list
4206                                           article
4207                                           (gnus-data-list t)))))
4208                                     ;; Error on the side of excessive subjects.
4209                                     (error ""))
4210                                   (mail-header-subject header)))
4211                                 ""
4212                               (mail-header-subject header))
4213                             nil (cdr (assq article gnus-newsgroup-scored))
4214                             (memq article gnus-newsgroup-processable))
4215                            (point)))))
4216         (when (cdr datal)
4217           (gnus-data-update-list
4218            (cdr datal)
4219            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4220
4221 (defun gnus-summary-update-article (article &optional iheader)
4222   "Update ARTICLE in the summary buffer."
4223   (set-buffer gnus-summary-buffer)
4224   (let* ((header (gnus-summary-article-header article))
4225          (id (mail-header-id header))
4226          (data (gnus-data-find article))
4227          (thread (gnus-id-to-thread id))
4228          (references (mail-header-references header))
4229          (parent
4230           (gnus-id-to-thread
4231            (or (gnus-parent-id
4232                 (when (and references
4233                            (not (equal "" references)))
4234                   references))
4235                "none")))
4236          (buffer-read-only nil)
4237          (old (car thread)))
4238     (when thread
4239       (unless iheader
4240         (setcar thread nil)
4241         (when parent
4242           (delq thread parent)))
4243       (if (gnus-summary-insert-subject id header)
4244           ;; Set the (possibly) new article number in the data structure.
4245           (gnus-data-set-number data (gnus-id-to-article id))
4246         (setcar thread old)
4247         nil))))
4248
4249 (defun gnus-rebuild-thread (id &optional line)
4250   "Rebuild the thread containing ID.
4251 If LINE, insert the rebuilt thread starting on line LINE."
4252   (let ((buffer-read-only nil)
4253         old-pos current thread data)
4254     (if (not gnus-show-threads)
4255         (setq thread (list (car (gnus-id-to-thread id))))
4256       ;; Get the thread this article is part of.
4257       (setq thread (gnus-remove-thread id)))
4258     (setq old-pos (point-at-bol))
4259     (setq current (save-excursion
4260                     (and (re-search-backward "[\r\n]" nil t)
4261                          (gnus-summary-article-number))))
4262     ;; If this is a gathered thread, we have to go some re-gathering.
4263     (when (stringp (car thread))
4264       (let ((subject (car thread))
4265             roots thr)
4266         (setq thread (cdr thread))
4267         (while thread
4268           (unless (memq (setq thr (gnus-id-to-thread
4269                                    (gnus-root-id
4270                                     (mail-header-id (caar thread)))))
4271                         roots)
4272             (push thr roots))
4273           (setq thread (cdr thread)))
4274         ;; We now have all (unique) roots.
4275         (if (= (length roots) 1)
4276             ;; All the loose roots are now one solid root.
4277             (setq thread (car roots))
4278           (setq thread (cons subject (gnus-sort-threads roots))))))
4279     (let (threads)
4280       ;; We then insert this thread into the summary buffer.
4281       (when line
4282         (goto-char (point-min))
4283         (forward-line (1- line)))
4284       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4285         (if gnus-show-threads
4286             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4287           (gnus-summary-prepare-unthreaded thread))
4288         (setq data (nreverse gnus-newsgroup-data))
4289         (setq threads gnus-newsgroup-threads))
4290       ;; We splice the new data into the data structure.
4291       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4292       ;;!!! then we want to insert at the beginning of the buffer.
4293       ;;!!! That happens to be true with Gnus now, but that may
4294       ;;!!! change in the future.  Perhaps.
4295       (gnus-data-enter-list
4296        (if line nil current) data (- (point) old-pos))
4297       (setq gnus-newsgroup-threads
4298             (nconc threads gnus-newsgroup-threads))
4299       (gnus-data-compute-positions))))
4300
4301 (defun gnus-number-to-header (number)
4302   "Return the header for article NUMBER."
4303   (let ((headers gnus-newsgroup-headers))
4304     (while (and headers
4305                 (not (= number (mail-header-number (car headers)))))
4306       (pop headers))
4307     (when headers
4308       (car headers))))
4309
4310 (defun gnus-parent-headers (in-headers &optional generation)
4311   "Return the headers of the GENERATIONeth parent of HEADERS."
4312   (unless generation
4313     (setq generation 1))
4314   (let ((parent t)
4315         (headers in-headers)
4316         references)
4317     (while (and parent
4318                 (not (zerop generation))
4319                 (setq references (mail-header-references headers)))
4320       (setq headers (if (and references
4321                              (setq parent (gnus-parent-id references)))
4322                         (car (gnus-id-to-thread parent))
4323                       nil))
4324       (decf generation))
4325     (and (not (eq headers in-headers))
4326          headers)))
4327
4328 (defun gnus-id-to-thread (id)
4329   "Return the (sub-)thread where ID appears."
4330   (gnus-gethash id gnus-newsgroup-dependencies))
4331
4332 (defun gnus-id-to-article (id)
4333   "Return the article number of ID."
4334   (let ((thread (gnus-id-to-thread id)))
4335     (when (and thread
4336                (car thread))
4337       (mail-header-number (car thread)))))
4338
4339 (defun gnus-id-to-header (id)
4340   "Return the article headers of ID."
4341   (car (gnus-id-to-thread id)))
4342
4343 (defun gnus-article-displayed-root-p (article)
4344   "Say whether ARTICLE is a root(ish) article."
4345   (let ((level (gnus-summary-thread-level article))
4346         (refs (mail-header-references  (gnus-summary-article-header article)))
4347         particle)
4348     (cond
4349      ((null level) nil)
4350      ((zerop level) t)
4351      ((null refs) t)
4352      ((null (gnus-parent-id refs)) t)
4353      ((and (= 1 level)
4354            (null (setq particle (gnus-id-to-article
4355                                  (gnus-parent-id refs))))
4356            (null (gnus-summary-thread-level particle)))))))
4357
4358 (defun gnus-root-id (id)
4359   "Return the id of the root of the thread where ID appears."
4360   (let (last-id prev)
4361     (while (and id (setq prev (car (gnus-id-to-thread id))))
4362       (setq last-id id
4363             id (gnus-parent-id (mail-header-references prev))))
4364     last-id))
4365
4366 (defun gnus-articles-in-thread (thread)
4367   "Return the list of articles in THREAD."
4368   (cons (mail-header-number (car thread))
4369         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4370
4371 (defun gnus-remove-thread (id &optional dont-remove)
4372   "Remove the thread that has ID in it."
4373   (let (headers thread last-id)
4374     ;; First go up in this thread until we find the root.
4375     (setq last-id (gnus-root-id id)
4376           headers (message-flatten-list (gnus-id-to-thread last-id)))
4377     ;; We have now found the real root of this thread.  It might have
4378     ;; been gathered into some loose thread, so we have to search
4379     ;; through the threads to find the thread we wanted.
4380     (let ((threads gnus-newsgroup-threads)
4381           sub)
4382       (while threads
4383         (setq sub (car threads))
4384         (if (stringp (car sub))
4385             ;; This is a gathered thread, so we look at the roots
4386             ;; below it to find whether this article is in this
4387             ;; gathered root.
4388             (progn
4389               (setq sub (cdr sub))
4390               (while sub
4391                 (when (member (caar sub) headers)
4392                   (setq thread (car threads)
4393                         threads nil
4394                         sub nil))
4395                 (setq sub (cdr sub))))
4396           ;; It's an ordinary thread, so we check it.
4397           (when (eq (car sub) (car headers))
4398             (setq thread sub
4399                   threads nil)))
4400         (setq threads (cdr threads)))
4401       ;; If this article is in no thread, then it's a root.
4402       (if thread
4403           (unless dont-remove
4404             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4405         (setq thread (gnus-id-to-thread last-id)))
4406       (when thread
4407         (prog1
4408             thread                      ; We return this thread.
4409           (unless dont-remove
4410             (if (stringp (car thread))
4411                 (progn
4412                   ;; If we use dummy roots, then we have to remove the
4413                   ;; dummy root as well.
4414                   (when (eq gnus-summary-make-false-root 'dummy)
4415                     ;; We go to the dummy root by going to
4416                     ;; the first sub-"thread", and then one line up.
4417                     (gnus-summary-goto-article
4418                      (mail-header-number (caadr thread)))
4419                     (forward-line -1)
4420                     (gnus-delete-line)
4421                     (gnus-data-compute-positions))
4422                   (setq thread (cdr thread))
4423                   (while thread
4424                     (gnus-remove-thread-1 (car thread))
4425                     (setq thread (cdr thread))))
4426               (gnus-remove-thread-1 thread))))))))
4427
4428 (defun gnus-remove-thread-1 (thread)
4429   "Remove the thread THREAD recursively."
4430   (let ((number (mail-header-number (pop thread)))
4431         d)
4432     (setq thread (reverse thread))
4433     (while thread
4434       (gnus-remove-thread-1 (pop thread)))
4435     (when (setq d (gnus-data-find number))
4436       (goto-char (gnus-data-pos d))
4437       (gnus-summary-show-thread)
4438       (gnus-data-remove
4439        number
4440        (- (point-at-bol)
4441           (prog1
4442               (1+ (point-at-eol))
4443             (gnus-delete-line)))))))
4444
4445 (defun gnus-sort-threads-1 (threads func)
4446   (sort (mapcar (lambda (thread)
4447                   (cons (car thread)
4448                         (and (cdr thread)
4449                              (gnus-sort-threads-1 (cdr thread) func))))
4450                 threads) func))
4451
4452 (defun gnus-sort-threads (threads)
4453   "Sort THREADS."
4454   (if (not gnus-thread-sort-functions)
4455       threads
4456     (gnus-message 8 "Sorting threads...")
4457     (let ((max-lisp-eval-depth 5000))
4458       (prog1 (gnus-sort-threads-1
4459          threads
4460          (gnus-make-sort-function gnus-thread-sort-functions))
4461         (gnus-message 8 "Sorting threads...done")))))
4462
4463 (defun gnus-sort-articles (articles)
4464   "Sort ARTICLES."
4465   (when gnus-article-sort-functions
4466     (gnus-message 7 "Sorting articles...")
4467     (prog1
4468         (setq gnus-newsgroup-headers
4469               (sort articles (gnus-make-sort-function
4470                               gnus-article-sort-functions)))
4471       (gnus-message 7 "Sorting articles...done"))))
4472
4473 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4474 (defmacro gnus-thread-header (thread)
4475   "Return header of first article in THREAD.
4476 Note that THREAD must never, ever be anything else than a variable -
4477 using some other form will lead to serious barfage."
4478   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4479   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4480   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4481         (vector thread) 2))
4482
4483 (defsubst gnus-article-sort-by-number (h1 h2)
4484   "Sort articles by article number."
4485   (< (mail-header-number h1)
4486      (mail-header-number h2)))
4487
4488 (defun gnus-thread-sort-by-number (h1 h2)
4489   "Sort threads by root article number."
4490   (gnus-article-sort-by-number
4491    (gnus-thread-header h1) (gnus-thread-header h2)))
4492
4493 (defsubst gnus-article-sort-by-random (h1 h2)
4494   "Sort articles by article number."
4495   (zerop (random 2)))
4496
4497 (defun gnus-thread-sort-by-random (h1 h2)
4498   "Sort threads by root article number."
4499   (gnus-article-sort-by-random
4500    (gnus-thread-header h1) (gnus-thread-header h2)))
4501
4502 (defsubst gnus-article-sort-by-lines (h1 h2)
4503   "Sort articles by article Lines header."
4504   (< (mail-header-lines h1)
4505      (mail-header-lines h2)))
4506
4507 (defun gnus-thread-sort-by-lines (h1 h2)
4508   "Sort threads by root article Lines header."
4509   (gnus-article-sort-by-lines
4510    (gnus-thread-header h1) (gnus-thread-header h2)))
4511
4512 (defsubst gnus-article-sort-by-chars (h1 h2)
4513   "Sort articles by octet length."
4514   (< (mail-header-chars h1)
4515      (mail-header-chars h2)))
4516
4517 (defun gnus-thread-sort-by-chars (h1 h2)
4518   "Sort threads by root article octet length."
4519   (gnus-article-sort-by-chars
4520    (gnus-thread-header h1) (gnus-thread-header h2)))
4521
4522 (defsubst gnus-article-sort-by-author (h1 h2)
4523   "Sort articles by root author."
4524   (string-lessp
4525    (let ((extract (funcall
4526                    gnus-extract-address-components
4527                    (mail-header-from h1))))
4528      (or (car extract) (cadr extract) ""))
4529    (let ((extract (funcall
4530                    gnus-extract-address-components
4531                    (mail-header-from h2))))
4532      (or (car extract) (cadr extract) ""))))
4533
4534 (defun gnus-thread-sort-by-author (h1 h2)
4535   "Sort threads by root author."
4536   (gnus-article-sort-by-author
4537    (gnus-thread-header h1)  (gnus-thread-header h2)))
4538
4539 (defsubst gnus-article-sort-by-subject (h1 h2)
4540   "Sort articles by root subject."
4541   (string-lessp
4542    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4543    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4544
4545 (defun gnus-thread-sort-by-subject (h1 h2)
4546   "Sort threads by root subject."
4547   (gnus-article-sort-by-subject
4548    (gnus-thread-header h1) (gnus-thread-header h2)))
4549
4550 (defsubst gnus-article-sort-by-date (h1 h2)
4551   "Sort articles by root article date."
4552   (time-less-p
4553    (gnus-date-get-time (mail-header-date h1))
4554    (gnus-date-get-time (mail-header-date h2))))
4555
4556 (defun gnus-thread-sort-by-date (h1 h2)
4557   "Sort threads by root article date."
4558   (gnus-article-sort-by-date
4559    (gnus-thread-header h1) (gnus-thread-header h2)))
4560
4561 (defsubst gnus-article-sort-by-score (h1 h2)
4562   "Sort articles by root article score.
4563 Unscored articles will be counted as having a score of zero."
4564   (> (or (cdr (assq (mail-header-number h1)
4565                     gnus-newsgroup-scored))
4566          gnus-summary-default-score 0)
4567      (or (cdr (assq (mail-header-number h2)
4568                     gnus-newsgroup-scored))
4569          gnus-summary-default-score 0)))
4570
4571 (defun gnus-thread-sort-by-score (h1 h2)
4572   "Sort threads by root article score."
4573   (gnus-article-sort-by-score
4574    (gnus-thread-header h1) (gnus-thread-header h2)))
4575
4576 (defun gnus-thread-sort-by-total-score (h1 h2)
4577   "Sort threads by the sum of all scores in the thread.
4578 Unscored articles will be counted as having a score of zero."
4579   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4580
4581 (defun gnus-thread-total-score (thread)
4582   ;; This function find the total score of THREAD.
4583   (cond
4584    ((null thread)
4585     0)
4586    ((consp thread)
4587     (if (stringp (car thread))
4588         (apply gnus-thread-score-function 0
4589                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4590       (gnus-thread-total-score-1 thread)))
4591    (t
4592     (gnus-thread-total-score-1 (list thread)))))
4593
4594 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4595   "Sort threads such that the thread with the most recently arrived article comes first."
4596   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4597
4598 (defun gnus-thread-highest-number (thread)
4599   "Return the highest article number in THREAD."
4600   (apply 'max (mapcar (lambda (header)
4601                         (mail-header-number header))
4602                       (message-flatten-list thread))))
4603
4604 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4605   "Sort threads such that the thread with the most recently dated article comes first."
4606   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4607
4608 (defun gnus-thread-latest-date (thread)
4609   "Return the highest article date in THREAD."
4610   (let ((previous-time 0))
4611     (apply 'max
4612            (mapcar
4613             (lambda (header)
4614               (setq previous-time
4615                     (condition-case ()
4616                         (time-to-seconds (mail-header-parse-date
4617                                           (mail-header-date header)))
4618                       (error previous-time))))
4619             (sort
4620              (message-flatten-list thread)
4621              (lambda (h1 h2)
4622                (< (mail-header-number h1)
4623                   (mail-header-number h2))))))))
4624
4625 (defun gnus-thread-total-score-1 (root)
4626   ;; This function find the total score of the thread below ROOT.
4627   (setq root (car root))
4628   (apply gnus-thread-score-function
4629          (or (append
4630               (mapcar 'gnus-thread-total-score
4631                       (cdr (gnus-id-to-thread (mail-header-id root))))
4632               (when (> (mail-header-number root) 0)
4633                 (list (or (cdr (assq (mail-header-number root)
4634                                      gnus-newsgroup-scored))
4635                           gnus-summary-default-score 0))))
4636              (list gnus-summary-default-score)
4637              '(0))))
4638
4639 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4640 (defvar gnus-tmp-prev-subject nil)
4641 (defvar gnus-tmp-false-parent nil)
4642 (defvar gnus-tmp-root-expunged nil)
4643 (defvar gnus-tmp-dummy-line nil)
4644
4645 (eval-when-compile (defvar gnus-tmp-header))
4646 (defun gnus-extra-header (type &optional header)
4647   "Return the extra header of TYPE."
4648   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4649       ""))
4650
4651 (defvar gnus-tmp-thread-tree-header-string "")
4652
4653 (defcustom gnus-sum-thread-tree-root "> "
4654   "With %B spec, used for the root of a thread.
4655 If nil, use subject instead."
4656   :version "21.4"
4657   :type '(radio (const :format "%v  " nil) (string :size 0))
4658   :group 'gnus-thread)
4659 (defcustom gnus-sum-thread-tree-false-root "> "
4660   "With %B spec, used for a false root of a thread.
4661 If nil, use subject instead."
4662   :version "21.4"
4663   :type '(radio (const :format "%v  " nil) (string :size 0))
4664   :group 'gnus-thread)
4665 (defcustom gnus-sum-thread-tree-single-indent ""
4666   "With %B spec, used for a thread with just one message.
4667 If nil, use subject instead."
4668   :version "21.4"
4669   :type '(radio (const :format "%v  " nil) (string :size 0))
4670   :group 'gnus-thread)
4671 (defcustom gnus-sum-thread-tree-vertical "| "
4672   "With %B spec, used for drawing a vertical line."
4673   :version "21.4"
4674   :type 'string
4675   :group 'gnus-thread)
4676 (defcustom gnus-sum-thread-tree-indent "  "
4677   "With %B spec, used for indenting."
4678   :version "21.4"
4679   :type 'string
4680   :group 'gnus-thread)
4681 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4682   "With %B spec, used for a leaf with brothers."
4683   :version "21.4"
4684   :type 'string
4685   :group 'gnus-thread)
4686 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4687   "With %B spec, used for a leaf without brothers."
4688   :version "21.4"
4689   :type 'string
4690   :group 'gnus-thread)
4691
4692 (defun gnus-summary-prepare-threads (threads)
4693   "Prepare summary buffer from THREADS and indentation LEVEL.
4694 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4695 or a straight list of headers."
4696   (gnus-message 7 "Generating summary...")
4697
4698   (setq gnus-newsgroup-threads threads)
4699   (beginning-of-line)
4700
4701   (let ((gnus-tmp-level 0)
4702         (default-score (or gnus-summary-default-score 0))
4703         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4704         (building-line-count gnus-summary-display-while-building)
4705         (building-count (integerp gnus-summary-display-while-building))
4706         thread number subject stack state gnus-tmp-gathered beg-match
4707         new-roots gnus-tmp-new-adopts thread-end simp-subject
4708         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4709         gnus-tmp-replied gnus-tmp-subject-or-nil
4710         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4711         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4712         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4713         tree-stack)
4714
4715     (setq gnus-tmp-prev-subject nil
4716           gnus-tmp-thread-tree-header-string "")
4717
4718     (if (vectorp (car threads))
4719         ;; If this is a straight (sic) list of headers, then a
4720         ;; threaded summary display isn't required, so we just create
4721         ;; an unthreaded one.
4722         (gnus-summary-prepare-unthreaded threads)
4723
4724       ;; Do the threaded display.
4725
4726       (if gnus-summary-display-while-building
4727           (switch-to-buffer (buffer-name)))
4728       (while (or threads stack gnus-tmp-new-adopts new-roots)
4729
4730         (if (and (= gnus-tmp-level 0)
4731                  (or (not stack)
4732                      (= (caar stack) 0))
4733                  (not gnus-tmp-false-parent)
4734                  (or gnus-tmp-new-adopts new-roots))
4735             (if gnus-tmp-new-adopts
4736                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4737                       thread (list (car gnus-tmp-new-adopts))
4738                       gnus-tmp-header (caar thread)
4739                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4740               (when new-roots
4741                 (setq thread (list (car new-roots))
4742                       gnus-tmp-header (caar thread)
4743                       new-roots (cdr new-roots))))
4744
4745           (if threads
4746               ;; If there are some threads, we do them before the
4747               ;; threads on the stack.
4748               (setq thread threads
4749                     gnus-tmp-header (caar thread))
4750             ;; There were no current threads, so we pop something off
4751             ;; the stack.
4752             (setq state (car stack)
4753                   gnus-tmp-level (car state)
4754                   tree-stack (cadr state)
4755                   thread (caddr state)
4756                   stack (cdr stack)
4757                   gnus-tmp-header (caar thread))))
4758
4759         (setq gnus-tmp-false-parent nil)
4760         (setq gnus-tmp-root-expunged nil)
4761         (setq thread-end nil)
4762
4763         (if (stringp gnus-tmp-header)
4764             ;; The header is a dummy root.
4765             (cond
4766              ((eq gnus-summary-make-false-root 'adopt)
4767               ;; We let the first article adopt the rest.
4768               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4769                                                (cddar thread)))
4770               (setq gnus-tmp-gathered
4771                     (nconc (mapcar
4772                             (lambda (h) (mail-header-number (car h)))
4773                             (cddar thread))
4774                            gnus-tmp-gathered))
4775               (setq thread (cons (list (caar thread)
4776                                        (cadar thread))
4777                                  (cdr thread)))
4778               (setq gnus-tmp-level -1
4779                     gnus-tmp-false-parent t))
4780              ((eq gnus-summary-make-false-root 'empty)
4781               ;; We print adopted articles with empty subject fields.
4782               (setq gnus-tmp-gathered
4783                     (nconc (mapcar
4784                             (lambda (h) (mail-header-number (car h)))
4785                             (cddar thread))
4786                            gnus-tmp-gathered))
4787               (setq gnus-tmp-level -1))
4788              ((eq gnus-summary-make-false-root 'dummy)
4789               ;; We remember that we probably want to output a dummy
4790               ;; root.
4791               (setq gnus-tmp-dummy-line gnus-tmp-header)
4792               (setq gnus-tmp-prev-subject gnus-tmp-header))
4793              (t
4794               ;; We do not make a root for the gathered
4795               ;; sub-threads at all.
4796               (setq gnus-tmp-level -1)))
4797
4798           (setq number (mail-header-number gnus-tmp-header)
4799                 subject (mail-header-subject gnus-tmp-header)
4800                 simp-subject (gnus-simplify-subject-fully subject))
4801
4802           (cond
4803            ;; If the thread has changed subject, we might want to make
4804            ;; this subthread into a root.
4805            ((and (null gnus-thread-ignore-subject)
4806                  (not (zerop gnus-tmp-level))
4807                  gnus-tmp-prev-subject
4808                  (not (string= gnus-tmp-prev-subject simp-subject)))
4809             (setq new-roots (nconc new-roots (list (car thread)))
4810                   thread-end t
4811                   gnus-tmp-header nil))
4812            ;; If the article lies outside the current limit,
4813            ;; then we do not display it.
4814            ((not (memq number gnus-newsgroup-limit))
4815             (setq gnus-tmp-gathered
4816                   (nconc (mapcar
4817                           (lambda (h) (mail-header-number (car h)))
4818                           (cdar thread))
4819                          gnus-tmp-gathered))
4820             (setq gnus-tmp-new-adopts (if (cdar thread)
4821                                           (append gnus-tmp-new-adopts
4822                                                   (cdar thread))
4823                                         gnus-tmp-new-adopts)
4824                   thread-end t
4825                   gnus-tmp-header nil)
4826             (when (zerop gnus-tmp-level)
4827               (setq gnus-tmp-root-expunged t)))
4828            ;; Perhaps this article is to be marked as read?
4829            ((and gnus-summary-mark-below
4830                  (< (or (cdr (assq number gnus-newsgroup-scored))
4831                         default-score)
4832                     gnus-summary-mark-below)
4833                  ;; Don't touch sparse articles.
4834                  (not (gnus-summary-article-sparse-p number))
4835                  (not (gnus-summary-article-ancient-p number)))
4836             (setq gnus-newsgroup-unreads
4837                   (delq number gnus-newsgroup-unreads))
4838             (if gnus-newsgroup-auto-expire
4839                 (setq gnus-newsgroup-expirable
4840                       (gnus-add-to-sorted-list
4841                        gnus-newsgroup-expirable number))
4842               (push (cons number gnus-low-score-mark)
4843                     gnus-newsgroup-reads))))
4844
4845           (when gnus-tmp-header
4846             ;; We may have an old dummy line to output before this
4847             ;; article.
4848             (when (and gnus-tmp-dummy-line
4849                        (gnus-subject-equal
4850                         gnus-tmp-dummy-line
4851                         (mail-header-subject gnus-tmp-header)))
4852               (gnus-summary-insert-dummy-line
4853                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4854               (setq gnus-tmp-dummy-line nil))
4855
4856             ;; Compute the mark.
4857             (setq gnus-tmp-unread (gnus-article-mark number))
4858
4859             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4860                                   gnus-tmp-header gnus-tmp-level)
4861                   gnus-newsgroup-data)
4862
4863             ;; Actually insert the line.
4864             (setq
4865              gnus-tmp-subject-or-nil
4866              (cond
4867               ((and gnus-thread-ignore-subject
4868                     gnus-tmp-prev-subject
4869                     (not (string= gnus-tmp-prev-subject simp-subject)))
4870                subject)
4871               ((zerop gnus-tmp-level)
4872                (if (and (eq gnus-summary-make-false-root 'empty)
4873                         (memq number gnus-tmp-gathered)
4874                         gnus-tmp-prev-subject
4875                         (string= gnus-tmp-prev-subject simp-subject))
4876                    gnus-summary-same-subject
4877                  subject))
4878               (t gnus-summary-same-subject)))
4879             (if (and (eq gnus-summary-make-false-root 'adopt)
4880                      (= gnus-tmp-level 1)
4881                      (memq number gnus-tmp-gathered))
4882                 (setq gnus-tmp-opening-bracket ?\<
4883                       gnus-tmp-closing-bracket ?\>)
4884               (setq gnus-tmp-opening-bracket ?\[
4885                     gnus-tmp-closing-bracket ?\]))
4886             (setq
4887              gnus-tmp-indentation
4888              (aref gnus-thread-indent-array gnus-tmp-level)
4889              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4890              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4891                                 gnus-summary-default-score 0)
4892              gnus-tmp-score-char
4893              (if (or (null gnus-summary-default-score)
4894                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4895                          gnus-summary-zcore-fuzz))
4896                  ?                      ;Whitespace
4897                (if (< gnus-tmp-score gnus-summary-default-score)
4898                    gnus-score-below-mark gnus-score-over-mark))
4899              gnus-tmp-replied
4900              (cond ((memq number gnus-newsgroup-processable)
4901                     gnus-process-mark)
4902                    ((memq number gnus-newsgroup-cached)
4903                     gnus-cached-mark)
4904                    ((memq number gnus-newsgroup-replied)
4905                     gnus-replied-mark)
4906                    ((memq number gnus-newsgroup-forwarded)
4907                     gnus-forwarded-mark)
4908                    ((memq number gnus-newsgroup-saved)
4909                     gnus-saved-mark)
4910                    ((memq number gnus-newsgroup-recent)
4911                     gnus-recent-mark)
4912                    ((memq number gnus-newsgroup-unseen)
4913                     gnus-unseen-mark)
4914                    (t gnus-no-mark))
4915              gnus-tmp-downloaded
4916              (cond ((memq number gnus-newsgroup-undownloaded)
4917                     gnus-undownloaded-mark)
4918                    (gnus-newsgroup-agentized
4919                     gnus-downloaded-mark)
4920                    (t
4921                     gnus-no-mark))
4922              gnus-tmp-from (mail-header-from gnus-tmp-header)
4923              gnus-tmp-name
4924              (cond
4925               ((string-match "<[^>]+> *$" gnus-tmp-from)
4926                (setq beg-match (match-beginning 0))
4927                (or (and (string-match "^\".+\"" gnus-tmp-from)
4928                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4929                    (substring gnus-tmp-from 0 beg-match)))
4930               ((string-match "(.+)" gnus-tmp-from)
4931                (substring gnus-tmp-from
4932                           (1+ (match-beginning 0)) (1- (match-end 0))))
4933               (t gnus-tmp-from))
4934
4935              ;; Do the %B string
4936              gnus-tmp-thread-tree-header-string
4937              (cond
4938               ((not gnus-show-threads) "")
4939               ((zerop gnus-tmp-level)
4940                (cond ((cdar thread)
4941                       (or gnus-sum-thread-tree-root subject))
4942                      (gnus-tmp-new-adopts
4943                       (or gnus-sum-thread-tree-false-root subject))
4944                      (t
4945                       (or gnus-sum-thread-tree-single-indent subject))))
4946               (t
4947                (concat (apply 'concat
4948                               (mapcar (lambda (item)
4949                                         (if (= item 1)
4950                                             gnus-sum-thread-tree-vertical
4951                                           gnus-sum-thread-tree-indent))
4952                                       (cdr (reverse tree-stack))))
4953                        (if (nth 1 thread)
4954                            gnus-sum-thread-tree-leaf-with-other
4955                          gnus-sum-thread-tree-single-leaf)))))
4956             (when (string= gnus-tmp-name "")
4957               (setq gnus-tmp-name gnus-tmp-from))
4958             (unless (numberp gnus-tmp-lines)
4959               (setq gnus-tmp-lines -1))
4960             (if (= gnus-tmp-lines -1)
4961                 (setq gnus-tmp-lines "?")
4962               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4963               (gnus-put-text-property
4964              (point)
4965              (progn (eval gnus-summary-line-format-spec) (point))
4966                'gnus-number number)
4967             (when gnus-visual-p
4968               (forward-line -1)
4969               (gnus-run-hooks 'gnus-summary-update-hook)
4970               (forward-line 1))
4971
4972             (setq gnus-tmp-prev-subject simp-subject)))
4973
4974         (when (nth 1 thread)
4975           (push (list (max 0 gnus-tmp-level)
4976                       (copy-sequence tree-stack)
4977                       (nthcdr 1 thread))
4978                 stack))
4979         (push (if (nth 1 thread) 1 0) tree-stack)
4980         (incf gnus-tmp-level)
4981         (setq threads (if thread-end nil (cdar thread)))
4982         (if gnus-summary-display-while-building
4983             (if building-count
4984                 (progn
4985                   ;; use a set frequency
4986                   (setq building-line-count (1- building-line-count))
4987                   (when (= building-line-count 0)
4988                     (sit-for 0)
4989                     (setq building-line-count
4990                           gnus-summary-display-while-building)))
4991               ;; always
4992               (sit-for 0)))
4993         (unless threads
4994           (setq gnus-tmp-level 0)))))
4995   (gnus-message 7 "Generating summary...done"))
4996
4997 (defun gnus-summary-prepare-unthreaded (headers)
4998   "Generate an unthreaded summary buffer based on HEADERS."
4999   (let (header number mark)
5000
5001     (beginning-of-line)
5002
5003     (while headers
5004       ;; We may have to root out some bad articles...
5005       (when (memq (setq number (mail-header-number
5006                                 (setq header (pop headers))))
5007                   gnus-newsgroup-limit)
5008         ;; Mark article as read when it has a low score.
5009         (when (and gnus-summary-mark-below
5010                    (< (or (cdr (assq number gnus-newsgroup-scored))
5011                           gnus-summary-default-score 0)
5012                       gnus-summary-mark-below)
5013                    (not (gnus-summary-article-ancient-p number)))
5014           (setq gnus-newsgroup-unreads
5015                 (delq number gnus-newsgroup-unreads))
5016           (if gnus-newsgroup-auto-expire
5017               (push number gnus-newsgroup-expirable)
5018             (push (cons number gnus-low-score-mark)
5019                   gnus-newsgroup-reads)))
5020
5021         (setq mark (gnus-article-mark number))
5022         (push (gnus-data-make number mark (1+ (point)) header 0)
5023               gnus-newsgroup-data)
5024         (gnus-summary-insert-line
5025          header 0 number
5026          (memq number gnus-newsgroup-undownloaded)
5027          mark (memq number gnus-newsgroup-replied)
5028          (memq number gnus-newsgroup-expirable)
5029          (mail-header-subject header) nil
5030          (cdr (assq number gnus-newsgroup-scored))
5031          (memq number gnus-newsgroup-processable))))))
5032
5033 (defun gnus-summary-remove-list-identifiers ()
5034   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5035   (let ((regexp (if (consp gnus-list-identifiers)
5036                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5037                   gnus-list-identifiers))
5038         changed subject)
5039     (when regexp
5040       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5041       (dolist (header gnus-newsgroup-headers)
5042         (setq subject (mail-header-subject header)
5043               changed nil)
5044         (while (string-match regexp subject)
5045           (setq subject
5046                 (concat (substring subject 0 (match-beginning 1))
5047                         (substring subject (match-end 0)))
5048                 changed t))
5049         (when changed
5050           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5051             (setq subject
5052                   (concat (substring subject 0 (match-beginning 1))
5053                           (substring subject (match-end 1)))))
5054           (mail-header-set-subject header subject))))))
5055
5056 (defun gnus-fetch-headers (articles)
5057   "Fetch headers of ARTICLES."
5058   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5059     (gnus-message 5 "Fetching headers for %s..." name)
5060     (prog1
5061         (if (eq 'nov
5062                 (setq gnus-headers-retrieved-by
5063                       (gnus-retrieve-headers
5064                        articles gnus-newsgroup-name
5065                        ;; We might want to fetch old headers, but
5066                        ;; not if there is only 1 article.
5067                        (and (or (and
5068                                  (not (eq gnus-fetch-old-headers 'some))
5069                                  (not (numberp gnus-fetch-old-headers)))
5070                                 (> (length articles) 1))
5071                             gnus-fetch-old-headers))))
5072             (gnus-get-newsgroup-headers-xover
5073              articles nil nil gnus-newsgroup-name t)
5074           (gnus-get-newsgroup-headers))
5075       (gnus-message 5 "Fetching headers for %s...done" name))))
5076
5077 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5078   "Select newsgroup GROUP.
5079 If READ-ALL is non-nil, all articles in the group are selected.
5080 If SELECT-ARTICLES, only select those articles from GROUP."
5081   (let* ((entry (gnus-group-entry group))
5082          ;;!!! Dirty hack; should be removed.
5083          (gnus-summary-ignore-duplicates
5084           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5085               t
5086             gnus-summary-ignore-duplicates))
5087          (info (nth 2 entry))
5088          articles fetched-articles cached)
5089
5090     (unless (gnus-check-server
5091              (set (make-local-variable 'gnus-current-select-method)
5092                   (gnus-find-method-for-group group)))
5093       (error "Couldn't open server"))
5094
5095     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5096         (gnus-activate-group group)     ; Or we can activate it...
5097         (progn                          ; Or we bug out.
5098           (when (equal major-mode 'gnus-summary-mode)
5099             (gnus-kill-buffer (current-buffer)))
5100           (error "Couldn't activate group %s: %s"
5101                  group (gnus-status-message group))))
5102
5103     (unless (gnus-request-group group t)
5104       (when (equal major-mode 'gnus-summary-mode)
5105         (gnus-kill-buffer (current-buffer)))
5106       (error "Couldn't request group %s: %s"
5107              group (gnus-status-message group)))
5108
5109     (when gnus-agent
5110       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5111       
5112       (setq gnus-summary-use-undownloaded-faces
5113             (gnus-agent-find-parameter
5114              group
5115              'agent-enable-undownloaded-faces)))
5116
5117     (setq gnus-newsgroup-name group
5118           gnus-newsgroup-unselected nil
5119           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5120
5121     (let ((display (gnus-group-find-parameter group 'display)))
5122       (setq gnus-newsgroup-display
5123             (cond
5124              ((not (zerop (or (car-safe read-all) 0)))
5125               ;; The user entered the group with C-u SPC/RET, let's show
5126               ;; all articles.
5127               'gnus-not-ignore)
5128              ((eq display 'all)
5129               'gnus-not-ignore)
5130              ((arrayp display)
5131               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5132              ((numberp display)
5133               ;; The following is probably the "correct" solution, but
5134               ;; it makes Gnus fetch all headers and then limit the
5135               ;; articles (which is slow), so instead we hack the
5136               ;; select-articles parameter instead. -- Simon Josefsson
5137               ;; <jas@kth.se>
5138               ;;
5139               ;; (gnus-byte-compile
5140               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5141               ;;                         display)))))
5142               (setq select-articles
5143                     (gnus-uncompress-range
5144                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5145                              (if (> tmp 0)
5146                                  tmp
5147                                1))
5148                            (cdr (gnus-active group)))))
5149               nil)
5150              (t
5151               nil))))
5152
5153     (gnus-summary-setup-default-charset)
5154
5155     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5156     (when (gnus-virtual-group-p group)
5157       (setq cached gnus-newsgroup-cached))
5158
5159     (setq gnus-newsgroup-unreads
5160           (gnus-sorted-ndifference
5161            (gnus-sorted-ndifference gnus-newsgroup-unreads
5162                                     gnus-newsgroup-marked)
5163            gnus-newsgroup-dormant))
5164
5165     (setq gnus-newsgroup-processable nil)
5166
5167     (gnus-update-read-articles group gnus-newsgroup-unreads)
5168
5169     ;; Adjust and set lists of article marks.
5170     (when info
5171       (gnus-adjust-marked-articles info))
5172     (if (setq articles select-articles)
5173         (setq gnus-newsgroup-unselected
5174               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5175       (setq articles (gnus-articles-to-read group read-all)))
5176
5177     (cond
5178      ((null articles)
5179       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5180       'quit)
5181      ((eq articles 0) nil)
5182      (t
5183       ;; Init the dependencies hash table.
5184       (setq gnus-newsgroup-dependencies
5185             (gnus-make-hashtable (length articles)))
5186       (gnus-set-global-variables)
5187       ;; Retrieve the headers and read them in.
5188
5189       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5190
5191       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5192       (when cached
5193         (setq gnus-newsgroup-cached cached))
5194
5195       ;; Suppress duplicates?
5196       (when gnus-suppress-duplicates
5197         (gnus-dup-suppress-articles))
5198
5199       ;; Set the initial limit.
5200       (setq gnus-newsgroup-limit (copy-sequence articles))
5201       ;; Remove canceled articles from the list of unread articles.
5202       (setq fetched-articles
5203             (mapcar (lambda (headers) (mail-header-number headers))
5204                     gnus-newsgroup-headers))
5205       (setq gnus-newsgroup-articles fetched-articles)
5206       (setq gnus-newsgroup-unreads
5207             (gnus-sorted-nintersection
5208              gnus-newsgroup-unreads fetched-articles))
5209       (gnus-compute-unseen-list)
5210
5211       ;; Removed marked articles that do not exist.
5212       (gnus-update-missing-marks
5213        (gnus-sorted-difference articles fetched-articles))
5214       ;; We might want to build some more threads first.
5215       (when (and gnus-fetch-old-headers
5216                  (eq gnus-headers-retrieved-by 'nov))
5217         (if (eq gnus-fetch-old-headers 'invisible)
5218             (gnus-build-all-threads)
5219           (gnus-build-old-threads)))
5220       ;; Let the Gnus agent mark articles as read.
5221       (when gnus-agent
5222         (gnus-agent-get-undownloaded-list))
5223       ;; Remove list identifiers from subject
5224       (when gnus-list-identifiers
5225         (gnus-summary-remove-list-identifiers))
5226       ;; Check whether auto-expire is to be done in this group.
5227       (setq gnus-newsgroup-auto-expire
5228             (gnus-group-auto-expirable-p group))
5229       ;; Set up the article buffer now, if necessary.
5230       (unless gnus-single-article-buffer
5231         (gnus-article-setup-buffer))
5232       ;; First and last article in this newsgroup.
5233       (when gnus-newsgroup-headers
5234         (setq gnus-newsgroup-begin
5235               (mail-header-number (car gnus-newsgroup-headers))
5236               gnus-newsgroup-end
5237               (mail-header-number
5238                (gnus-last-element gnus-newsgroup-headers))))
5239       ;; GROUP is successfully selected.
5240       (or gnus-newsgroup-headers t)))))
5241
5242 (defun gnus-compute-unseen-list ()
5243   ;; The `seen' marks are treated specially.
5244   (if (not gnus-newsgroup-seen)
5245       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5246     (setq gnus-newsgroup-unseen
5247           (gnus-inverse-list-range-intersection
5248            gnus-newsgroup-articles gnus-newsgroup-seen))))
5249
5250 (defun gnus-summary-display-make-predicate (display)
5251   (require 'gnus-agent)
5252   (when (= (length display) 1)
5253     (setq display (car display)))
5254   (unless gnus-summary-display-cache
5255     (dolist (elem (append '((unread . unread)
5256                             (read . read)
5257                             (unseen . unseen))
5258                           gnus-article-mark-lists))
5259       (push (cons (cdr elem)
5260                   (gnus-byte-compile
5261                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5262             gnus-summary-display-cache)))
5263   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5264         (gnus-category-predicate-cache gnus-summary-display-cache))
5265     (gnus-get-predicate display)))
5266
5267 ;; Uses the dynamically bound `number' variable.
5268 (eval-when-compile
5269   (defvar number))
5270 (defun gnus-article-marked-p (type &optional article)
5271   (let ((article (or article number)))
5272     (cond
5273      ((eq type 'tick)
5274       (memq article gnus-newsgroup-marked))
5275      ((eq type 'spam)
5276       (memq article gnus-newsgroup-spam-marked))
5277      ((eq type 'unsend)
5278       (memq article gnus-newsgroup-unsendable))
5279      ((eq type 'undownload)
5280       (memq article gnus-newsgroup-undownloaded))
5281      ((eq type 'download)
5282       (memq article gnus-newsgroup-downloadable))
5283      ((eq type 'unread)
5284       (memq article gnus-newsgroup-unreads))
5285      ((eq type 'read)
5286       (memq article gnus-newsgroup-reads))
5287      ((eq type 'dormant)
5288       (memq article gnus-newsgroup-dormant) )
5289      ((eq type 'expire)
5290       (memq article gnus-newsgroup-expirable))
5291      ((eq type 'reply)
5292       (memq article gnus-newsgroup-replied))
5293      ((eq type 'killed)
5294       (memq article gnus-newsgroup-killed))
5295      ((eq type 'bookmark)
5296       (assq article gnus-newsgroup-bookmarks))
5297      ((eq type 'score)
5298       (assq article gnus-newsgroup-scored))
5299      ((eq type 'save)
5300       (memq article gnus-newsgroup-saved))
5301      ((eq type 'cache)
5302       (memq article gnus-newsgroup-cached))
5303      ((eq type 'forward)
5304       (memq article gnus-newsgroup-forwarded))
5305      ((eq type 'seen)
5306       (not (memq article gnus-newsgroup-unseen)))
5307      ((eq type 'recent)
5308       (memq article gnus-newsgroup-recent))
5309      (t t))))
5310
5311 (defun gnus-articles-to-read (group &optional read-all)
5312   "Find out what articles the user wants to read."
5313   (let* ((articles
5314           ;; Select all articles if `read-all' is non-nil, or if there
5315           ;; are no unread articles.
5316           (if (or read-all
5317                   (and (zerop (length gnus-newsgroup-marked))
5318                        (zerop (length gnus-newsgroup-unreads)))
5319                   ;; Fetch all if the predicate is non-nil.
5320                   gnus-newsgroup-display)
5321               ;; We want to select the headers for all the articles in
5322               ;; the group, so we select either all the active
5323               ;; articles in the group, or (if that's nil), the
5324               ;; articles in the cache.
5325               (or
5326                (gnus-uncompress-range (gnus-active group))
5327                (gnus-cache-articles-in-group group))
5328             ;; Select only the "normal" subset of articles.
5329             (gnus-sorted-nunion
5330              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5331              gnus-newsgroup-unreads)))
5332          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5333          (scored (length scored-list))
5334          (number (length articles))
5335          (marked (+ (length gnus-newsgroup-marked)
5336                     (length gnus-newsgroup-dormant)))
5337          (select
5338           (cond
5339            ((numberp read-all)
5340             read-all)
5341            ((numberp gnus-newsgroup-display)
5342             gnus-newsgroup-display)
5343            (t
5344             (condition-case ()
5345                 (cond
5346                  ((and (or (<= scored marked) (= scored number))
5347                        (numberp gnus-large-newsgroup)
5348                        (> number gnus-large-newsgroup))
5349                   (let* ((cursor-in-echo-area nil)
5350                          (initial (gnus-parameter-large-newsgroup-initial
5351                                    gnus-newsgroup-name))
5352                          (input
5353                           (read-string
5354                            (format
5355                             "How many articles from %s (%s %d): "
5356                             (gnus-limit-string
5357                              (gnus-group-decoded-name gnus-newsgroup-name)
5358                              35)
5359                             (if initial "max" "default")
5360                             number)
5361                            (if initial
5362                                (cons (number-to-string initial)
5363                                      0)))))
5364                     (if (string-match "^[ \t]*$" input) number input)))
5365                  ((and (> scored marked) (< scored number)
5366                        (> (- scored number) 20))
5367                   (let ((input
5368                          (read-string
5369                           (format "%s %s (%d scored, %d total): "
5370                                   "How many articles from"
5371                                   (gnus-group-decoded-name group)
5372                                   scored number))))
5373                     (if (string-match "^[ \t]*$" input)
5374                         number input)))
5375                  (t number))
5376               (quit
5377                (message "Quit getting the articles to read")
5378                nil))))))
5379     (setq select (if (stringp select) (string-to-number select) select))
5380     (if (or (null select) (zerop select))
5381         select
5382       (if (and (not (zerop scored)) (<= (abs select) scored))
5383           (progn
5384             (setq articles (sort scored-list '<))
5385             (setq number (length articles)))
5386         (setq articles (copy-sequence articles)))
5387
5388       (when (< (abs select) number)
5389         (if (< select 0)
5390             ;; Select the N oldest articles.
5391             (setcdr (nthcdr (1- (abs select)) articles) nil)
5392           ;; Select the N most recent articles.
5393           (setq articles (nthcdr (- number select) articles))))
5394       (setq gnus-newsgroup-unselected
5395             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5396       (when gnus-alter-articles-to-read-function
5397         (setq articles
5398               (sort
5399                (funcall gnus-alter-articles-to-read-function
5400                         gnus-newsgroup-name articles)
5401                '<)))
5402       articles)))
5403
5404 (defun gnus-killed-articles (killed articles)
5405   (let (out)
5406     (while articles
5407       (when (inline (gnus-member-of-range (car articles) killed))
5408         (push (car articles) out))
5409       (setq articles (cdr articles)))
5410     out))
5411
5412 (defun gnus-uncompress-marks (marks)
5413   "Uncompress the mark ranges in MARKS."
5414   (let ((uncompressed '(score bookmark))
5415         out)
5416     (while marks
5417       (if (memq (caar marks) uncompressed)
5418           (push (car marks) out)
5419         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5420       (setq marks (cdr marks)))
5421     out))
5422
5423 (defun gnus-article-mark-to-type (mark)
5424   "Return the type of MARK."
5425   (or (cadr (assq mark gnus-article-special-mark-lists))
5426       'list))
5427
5428 (defun gnus-article-unpropagatable-p (mark)
5429   "Return whether MARK should be propagated to back end."
5430   (memq mark gnus-article-unpropagated-mark-lists))
5431
5432 (defun gnus-adjust-marked-articles (info)
5433   "Set all article lists and remove all marks that are no longer valid."
5434   (let* ((marked-lists (gnus-info-marks info))
5435          (active (gnus-active (gnus-info-group info)))
5436          (min (car active))
5437          (max (cdr active))
5438          (types gnus-article-mark-lists)
5439          marks var articles article mark mark-type
5440          bgn end)
5441
5442     (dolist (marks marked-lists)
5443       (setq mark (car marks)
5444             mark-type (gnus-article-mark-to-type mark)
5445             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5446
5447       ;; We set the variable according to the type of the marks list,
5448       ;; and then adjust the marks to a subset of the active articles.
5449       (cond
5450        ;; Adjust "simple" lists - compressed yet unsorted
5451        ((eq mark-type 'list)
5452         ;; Simultaneously uncompress and clip to active range
5453         ;; See gnus-uncompress-range for a description of possible marks
5454         (let (l lh)
5455           (if (not (cadr marks))
5456               (set var nil)
5457             (setq articles (if (numberp (cddr marks))
5458                                (list (cdr marks))
5459                              (cdr marks))
5460                   lh (cons nil nil)
5461                   l lh)
5462
5463             (while (setq article (pop articles))
5464               (cond ((consp article)
5465                      (setq bgn (max (car article) min)
5466                            end (min (cdr article) max))
5467                      (while (<= bgn end)
5468                        (setq l (setcdr l (cons bgn nil))
5469                              bgn (1+ bgn))))
5470                     ((and (<= min article)
5471                           (>= max article))
5472                      (setq l (setcdr l (cons article nil))))))
5473             (set var (cdr lh)))))
5474        ;; Adjust assocs.
5475        ((eq mark-type 'tuple)
5476         (set var (setq articles (cdr marks)))
5477         (when (not (listp (cdr (symbol-value var))))
5478           (set var (list (symbol-value var))))
5479         (when (not (listp (cdr articles)))
5480           (setq articles (list articles)))
5481         (while articles
5482           (when (or (not (consp (setq article (pop articles))))
5483                     (< (car article) min)
5484                     (> (car article) max))
5485             (set var (delq article (symbol-value var))))))
5486        ;; Adjust ranges (sloppily).
5487        ((eq mark-type 'range)
5488         (cond
5489          ((eq mark 'seen)
5490           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5491           ;; It should be (seen (NUM1 . NUM2)).
5492           (when (numberp (cddr marks))
5493             (setcdr marks (list (cdr marks))))
5494           (setq articles (cdr marks))
5495           (while (and articles
5496                       (or (and (consp (car articles))
5497                                (> min (cdar articles)))
5498                           (and (numberp (car articles))
5499                                (> min (car articles)))))
5500             (pop articles))
5501           (set var articles))))))))
5502
5503 (defun gnus-update-missing-marks (missing)
5504   "Go through the list of MISSING articles and remove them from the mark lists."
5505   (when missing
5506     (let (var m)
5507       ;; Go through all types.
5508       (dolist (elem gnus-article-mark-lists)
5509         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5510           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5511           (when (symbol-value var)
5512             ;; This list has articles.  So we delete all missing
5513             ;; articles from it.
5514             (setq m missing)
5515             (while m
5516               (set var (delq (pop m) (symbol-value var))))))))))
5517
5518 (defun gnus-update-marks ()
5519   "Enter the various lists of marked articles into the newsgroup info list."
5520   (let ((types gnus-article-mark-lists)
5521         (info (gnus-get-info gnus-newsgroup-name))
5522         type list newmarked symbol delta-marks)
5523     (when info
5524       ;; Add all marks lists to the list of marks lists.
5525       (while (setq type (pop types))
5526         (setq list (symbol-value
5527                     (setq symbol
5528                           (intern (format "gnus-newsgroup-%s" (car type))))))
5529
5530         (when list
5531           ;; Get rid of the entries of the articles that have the
5532           ;; default score.
5533           (when (and (eq (cdr type) 'score)
5534                      gnus-save-score
5535                      list)
5536             (let* ((arts list)
5537                    (prev (cons nil list))
5538                    (all prev))
5539               (while arts
5540                 (if (or (not (consp (car arts)))
5541                         (= (cdar arts) gnus-summary-default-score))
5542                     (setcdr prev (cdr arts))
5543                   (setq prev arts))
5544                 (setq arts (cdr arts)))
5545               (setq list (cdr all)))))
5546
5547         (when (eq (cdr type) 'seen)
5548           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5549
5550         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5551           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5552
5553         (when (and (gnus-check-backend-function
5554                     'request-set-mark gnus-newsgroup-name)
5555                    (not (gnus-article-unpropagatable-p (cdr type))))
5556           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5557                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5558                  (add (gnus-remove-from-range
5559                        (gnus-copy-sequence list) old)))
5560             (when add
5561               (push (list add 'add (list (cdr type))) delta-marks))
5562             (when del
5563               (push (list del 'del (list (cdr type))) delta-marks))))
5564
5565         (when list
5566           (push (cons (cdr type) list) newmarked)))
5567
5568       (when delta-marks
5569         (unless (gnus-check-group gnus-newsgroup-name)
5570           (error "Can't open server for %s" gnus-newsgroup-name))
5571         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5572
5573       ;; Enter these new marks into the info of the group.
5574       (if (nthcdr 3 info)
5575           (setcar (nthcdr 3 info) newmarked)
5576         ;; Add the marks lists to the end of the info.
5577         (when newmarked
5578           (setcdr (nthcdr 2 info) (list newmarked))))
5579
5580       ;; Cut off the end of the info if there's nothing else there.
5581       (let ((i 5))
5582         (while (and (> i 2)
5583                     (not (nth i info)))
5584           (when (nthcdr (decf i) info)
5585             (setcdr (nthcdr i info) nil)))))))
5586
5587 (defun gnus-set-mode-line (where)
5588   "Set the mode line of the article or summary buffers.
5589 If WHERE is `summary', the summary mode line format will be used."
5590   ;; Is this mode line one we keep updated?
5591   (when (and (memq where gnus-updated-mode-lines)
5592              (symbol-value
5593               (intern (format "gnus-%s-mode-line-format-spec" where))))
5594     (let (mode-string)
5595       (save-excursion
5596         ;; We evaluate this in the summary buffer since these
5597         ;; variables are buffer-local to that buffer.
5598         (set-buffer gnus-summary-buffer)
5599        ;; We bind all these variables that are used in the `eval' form
5600         ;; below.
5601         (let* ((mformat (symbol-value
5602                          (intern
5603                           (format "gnus-%s-mode-line-format-spec" where))))
5604                (gnus-tmp-group-name (gnus-group-decoded-name
5605                                      gnus-newsgroup-name))
5606                (gnus-tmp-article-number (or gnus-current-article 0))
5607                (gnus-tmp-unread gnus-newsgroup-unreads)
5608                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5609                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5610                (gnus-tmp-unread-and-unselected
5611                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5612                             (zerop gnus-tmp-unselected))
5613                        "")
5614                       ((zerop gnus-tmp-unselected)
5615                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5616                       (t (format "{%d(+%d) more}"
5617                                  gnus-tmp-unread-and-unticked
5618                                  gnus-tmp-unselected))))
5619                (gnus-tmp-subject
5620                 (if (and gnus-current-headers
5621                          (vectorp gnus-current-headers))
5622                     (gnus-mode-string-quote
5623                      (mail-header-subject gnus-current-headers))
5624                   ""))
5625                bufname-length max-len
5626                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5627           (setq mode-string (eval mformat))
5628           (setq bufname-length (if (string-match "%b" mode-string)
5629                                    (- (length
5630                                        (buffer-name
5631                                         (if (eq where 'summary)
5632                                             nil
5633                                           (get-buffer gnus-article-buffer))))
5634                                       2)
5635                                  0))
5636           (setq max-len (max 4 (if gnus-mode-non-string-length
5637                                    (- (window-width)
5638                                       gnus-mode-non-string-length
5639                                       bufname-length)
5640                                  (length mode-string))))
5641           ;; We might have to chop a bit of the string off...
5642           (when (> (length mode-string) max-len)
5643             (setq mode-string
5644                   (concat (truncate-string-to-width mode-string (- max-len 3))
5645                           "...")))
5646           ;; Pad the mode string a bit.
5647           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5648       ;; Update the mode line.
5649       (setq mode-line-buffer-identification
5650             (gnus-mode-line-buffer-identification (list mode-string)))
5651       (set-buffer-modified-p t))))
5652
5653 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5654   "Go through the HEADERS list and add all Xrefs to a hash table.
5655 The resulting hash table is returned, or nil if no Xrefs were found."
5656   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5657          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5658          (xref-hashtb (gnus-make-hashtable))
5659          start group entry number xrefs header)
5660     (while headers
5661       (setq header (pop headers))
5662       (when (and (setq xrefs (mail-header-xref header))
5663                  (not (memq (setq number (mail-header-number header))
5664                             unreads)))
5665         (setq start 0)
5666         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5667           (setq start (match-end 0))
5668           (setq group (if prefix
5669                           (concat prefix (substring xrefs (match-beginning 1)
5670                                                     (match-end 1)))
5671                         (substring xrefs (match-beginning 1) (match-end 1))))
5672           (setq number
5673                 (string-to-int (substring xrefs (match-beginning 2)
5674                                           (match-end 2))))
5675           (if (setq entry (gnus-gethash group xref-hashtb))
5676               (setcdr entry (cons number (cdr entry)))
5677             (gnus-sethash group (cons number nil) xref-hashtb)))))
5678     (and start xref-hashtb)))
5679
5680 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5681   "Look through all the headers and mark the Xrefs as read."
5682   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5683         name info xref-hashtb idlist method nth4)
5684     (save-excursion
5685       (set-buffer gnus-group-buffer)
5686       (when (setq xref-hashtb
5687                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5688         (mapatoms
5689          (lambda (group)
5690            (unless (string= from-newsgroup (setq name (symbol-name group)))
5691              (setq idlist (symbol-value group))
5692              ;; Dead groups are not updated.
5693              (and (prog1
5694                       (setq info (gnus-get-info name))
5695                     (when (stringp (setq nth4 (gnus-info-method info)))
5696                       (setq nth4 (gnus-server-to-method nth4))))
5697                   ;; Only do the xrefs if the group has the same
5698                   ;; select method as the group we have just read.
5699                   (or (gnus-methods-equal-p
5700                        nth4 (gnus-find-method-for-group from-newsgroup))
5701                       virtual
5702                       (equal nth4 (setq method (gnus-find-method-for-group
5703                                                 from-newsgroup)))
5704                       (and (equal (car nth4) (car method))
5705                            (equal (nth 1 nth4) (nth 1 method))))
5706                   gnus-use-cross-reference
5707                   (or (not (eq gnus-use-cross-reference t))
5708                       virtual
5709                       ;; Only do cross-references on subscribed
5710                       ;; groups, if that is what is wanted.
5711                       (<= (gnus-info-level info) gnus-level-subscribed))
5712                   (gnus-group-make-articles-read name idlist))))
5713          xref-hashtb)))))
5714
5715 (defun gnus-compute-read-articles (group articles)
5716   (let* ((entry (gnus-group-entry group))
5717          (info (nth 2 entry))
5718          (active (gnus-active group))
5719          ninfo)
5720     (when entry
5721       ;; First peel off all invalid article numbers.
5722       (when active
5723         (let ((ids articles)
5724               id first)
5725           (while (setq id (pop ids))
5726             (when (and first (> id (cdr active)))
5727               ;; We'll end up in this situation in one particular
5728               ;; obscure situation.  If you re-scan a group and get
5729               ;; a new article that is cross-posted to a different
5730               ;; group that has not been re-scanned, you might get
5731               ;; crossposted article that has a higher number than
5732               ;; Gnus believes possible.  So we re-activate this
5733               ;; group as well.  This might mean doing the
5734               ;; crossposting thingy will *increase* the number
5735               ;; of articles in some groups.  Tsk, tsk.
5736               (setq active (or (gnus-activate-group group) active)))
5737             (when (or (> id (cdr active))
5738                       (< id (car active)))
5739               (setq articles (delq id articles))))))
5740       ;; If the read list is nil, we init it.
5741       (if (and active
5742                (null (gnus-info-read info))
5743                (> (car active) 1))
5744           (setq ninfo (cons 1 (1- (car active))))
5745         (setq ninfo (gnus-info-read info)))
5746       ;; Then we add the read articles to the range.
5747       (gnus-add-to-range
5748        ninfo (setq articles (sort articles '<))))))
5749
5750 (defun gnus-group-make-articles-read (group articles)
5751   "Update the info of GROUP to say that ARTICLES are read."
5752   (let* ((num 0)
5753          (entry (gnus-group-entry group))
5754          (info (nth 2 entry))
5755          (active (gnus-active group))
5756          range)
5757     (when entry
5758       (setq range (gnus-compute-read-articles group articles))
5759       (with-current-buffer gnus-group-buffer
5760         (gnus-undo-register
5761           `(progn
5762              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5763              (gnus-info-set-read ',info ',(gnus-info-read info))
5764              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5765              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5766              (gnus-group-update-group ,group t))))
5767       ;; Add the read articles to the range.
5768       (gnus-info-set-read info range)
5769       (gnus-request-set-mark group (list (list range 'add '(read))))
5770       ;; Then we have to re-compute how many unread
5771       ;; articles there are in this group.
5772       (when active
5773         (cond
5774          ((not range)
5775           (setq num (- (1+ (cdr active)) (car active))))
5776          ((not (listp (cdr range)))
5777           (setq num (- (cdr active) (- (1+ (cdr range))
5778                                        (car range)))))
5779          (t
5780           (while range
5781             (if (numberp (car range))
5782                 (setq num (1+ num))
5783               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5784             (setq range (cdr range)))
5785           (setq num (- (cdr active) num))))
5786         ;; Update the number of unread articles.
5787         (setcar entry num)
5788         ;; Update the group buffer.
5789         (unless (gnus-ephemeral-group-p group)
5790           (gnus-group-update-group group t))))))
5791
5792 (defvar gnus-newsgroup-none-id 0)
5793
5794 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5795   (let ((cur nntp-server-buffer)
5796         (dependencies
5797          (or dependencies
5798              (with-current-buffer gnus-summary-buffer
5799                gnus-newsgroup-dependencies)))
5800         headers id end ref number
5801         (mail-parse-charset gnus-newsgroup-charset)
5802         (mail-parse-ignored-charsets
5803          (save-excursion (condition-case nil
5804                              (set-buffer gnus-summary-buffer)
5805                            (error))
5806                          gnus-newsgroup-ignored-charsets)))
5807     (save-excursion
5808       (set-buffer nntp-server-buffer)
5809       ;; Translate all TAB characters into SPACE characters.
5810       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5811       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5812       (ietf-drums-unfold-fws)
5813       (gnus-run-hooks 'gnus-parse-headers-hook)
5814       (let ((case-fold-search t)
5815             in-reply-to header p lines chars)
5816         (goto-char (point-min))
5817         ;; Search to the beginning of the next header.  Error messages
5818         ;; do not begin with 2 or 3.
5819         (while (re-search-forward "^[23][0-9]+ " nil t)
5820           (setq id nil
5821                 ref nil)
5822           ;; This implementation of this function, with nine
5823           ;; search-forwards instead of the one re-search-forward and
5824           ;; a case (which basically was the old function) is actually
5825           ;; about twice as fast, even though it looks messier.  You
5826           ;; can't have everything, I guess.  Speed and elegance
5827           ;; doesn't always go hand in hand.
5828           (setq
5829            header
5830            (vector
5831             ;; Number.
5832             (prog1
5833                 (setq number (read cur))
5834               (end-of-line)
5835               (setq p (point))
5836               (narrow-to-region (point)
5837                                 (or (and (search-forward "\n.\n" nil t)
5838                                          (- (point) 2))
5839                                     (point))))
5840             ;; Subject.
5841             (progn
5842               (goto-char p)
5843               (if (search-forward "\nsubject:" nil t)
5844                   (funcall gnus-decode-encoded-word-function
5845                            (nnheader-header-value))
5846                 "(none)"))
5847             ;; From.
5848             (progn
5849               (goto-char p)
5850               (if (search-forward "\nfrom:" nil t)
5851                   (funcall gnus-decode-encoded-word-function
5852                            (nnheader-header-value))
5853                 "(nobody)"))
5854             ;; Date.
5855             (progn
5856               (goto-char p)
5857               (if (search-forward "\ndate:" nil t)
5858                   (nnheader-header-value) ""))
5859             ;; Message-ID.
5860             (progn
5861               (goto-char p)
5862               (setq id (if (re-search-forward
5863                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5864                            ;; We do it this way to make sure the Message-ID
5865                            ;; is (somewhat) syntactically valid.
5866                            (buffer-substring (match-beginning 1)
5867                                              (match-end 1))
5868                          ;; If there was no message-id, we just fake one
5869                          ;; to make subsequent routines simpler.
5870                          (nnheader-generate-fake-message-id number))))
5871             ;; References.
5872             (progn
5873               (goto-char p)
5874               (if (search-forward "\nreferences:" nil t)
5875                   (progn
5876                     (setq end (point))
5877                     (prog1
5878                         (nnheader-header-value)
5879                       (setq ref
5880                             (buffer-substring
5881                              (progn
5882                                (end-of-line)
5883                                (search-backward ">" end t)
5884                                (1+ (point)))
5885                              (progn
5886                                (search-backward "<" end t)
5887                                (point))))))
5888                 ;; Get the references from the in-reply-to header if there
5889                 ;; were no references and the in-reply-to header looks
5890                 ;; promising.
5891                 (if (and (search-forward "\nin-reply-to:" nil t)
5892                          (setq in-reply-to (nnheader-header-value))
5893                          (string-match "<[^>]+>" in-reply-to))
5894                     (let (ref2)
5895                       (setq ref (substring in-reply-to (match-beginning 0)
5896                                            (match-end 0)))
5897                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5898                         (setq ref2 (substring in-reply-to (match-beginning 0)
5899                                               (match-end 0)))
5900                         (when (> (length ref2) (length ref))
5901                           (setq ref ref2)))
5902                       ref)
5903                   (setq ref nil))))
5904             ;; Chars.
5905             (progn
5906               (goto-char p)
5907               (if (search-forward "\nchars: " nil t)
5908                   (if (numberp (setq chars (ignore-errors (read cur))))
5909                       chars -1)
5910                 -1))
5911             ;; Lines.
5912             (progn
5913               (goto-char p)
5914               (if (search-forward "\nlines: " nil t)
5915                   (if (numberp (setq lines (ignore-errors (read cur))))
5916                       lines -1)
5917                 -1))
5918             ;; Xref.
5919             (progn
5920               (goto-char p)
5921               (and (search-forward "\nxref:" nil t)
5922                    (nnheader-header-value)))
5923             ;; Extra.
5924             (when gnus-extra-headers
5925               (let ((extra gnus-extra-headers)
5926                     out)
5927                 (while extra
5928                   (goto-char p)
5929                   (when (search-forward
5930                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5931                     (push (cons (car extra) (nnheader-header-value))
5932                           out))
5933                   (pop extra))
5934                 out))))
5935           (when (equal id ref)
5936             (setq ref nil))
5937
5938           (when gnus-alter-header-function
5939             (funcall gnus-alter-header-function header)
5940             (setq id (mail-header-id header)
5941                   ref (gnus-parent-id (mail-header-references header))))
5942
5943           (when (setq header
5944                       (gnus-dependencies-add-header
5945                        header dependencies force-new))
5946             (push header headers))
5947           (goto-char (point-max))
5948           (widen))
5949         (nreverse headers)))))
5950
5951 ;; Goes through the xover lines and returns a list of vectors
5952 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5953                                                   force-new dependencies
5954                                                   group also-fetch-heads)
5955   "Parse the news overview data in the server buffer.
5956 Return a list of headers that match SEQUENCE (see
5957 `nntp-retrieve-headers')."
5958   ;; Get the Xref when the users reads the articles since most/some
5959   ;; NNTP servers do not include Xrefs when using XOVER.
5960   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5961   (let ((mail-parse-charset gnus-newsgroup-charset)
5962         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5963         (cur nntp-server-buffer)
5964         (dependencies (or dependencies gnus-newsgroup-dependencies))
5965         (allp (cond
5966                ((eq gnus-read-all-available-headers t)
5967                 t)
5968                ((stringp gnus-read-all-available-headers)
5969                 (string-match gnus-read-all-available-headers group))
5970                (t
5971                 nil)))
5972         number headers header)
5973     (save-excursion
5974       (set-buffer nntp-server-buffer)
5975       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5976       ;; Allow the user to mangle the headers before parsing them.
5977       (gnus-run-hooks 'gnus-parse-headers-hook)
5978       (goto-char (point-min))
5979       (gnus-parse-without-error
5980         (while (and (or sequence allp)
5981                     (not (eobp)))
5982           (setq number (read cur))
5983           (when (not allp)
5984             (while (and sequence
5985                         (< (car sequence) number))
5986               (setq sequence (cdr sequence))))
5987           (when (and (or allp
5988                          (and sequence
5989                               (eq number (car sequence))))
5990                      (progn
5991                        (setq sequence (cdr sequence))
5992                        (setq header (inline
5993                                       (gnus-nov-parse-line
5994                                        number dependencies force-new)))))
5995             (push header headers))
5996           (forward-line 1)))
5997       ;; A common bug in inn is that if you have posted an article and
5998       ;; then retrieves the active file, it will answer correctly --
5999       ;; the new article is included.  However, a NOV entry for the
6000       ;; article may not have been generated yet, so this may fail.
6001       ;; We work around this problem by retrieving the last few
6002       ;; headers using HEAD.
6003       (if (or (not also-fetch-heads)
6004               (not sequence))
6005           ;; We (probably) got all the headers.
6006           (nreverse headers)
6007         (let ((gnus-nov-is-evil t))
6008           (nconc
6009            (nreverse headers)
6010            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6011              (gnus-get-newsgroup-headers))))))))
6012
6013 (defun gnus-article-get-xrefs ()
6014   "Fill in the Xref value in `gnus-current-headers', if necessary.
6015 This is meant to be called in `gnus-article-internal-prepare-hook'."
6016   (let ((headers (with-current-buffer gnus-summary-buffer
6017                    gnus-current-headers)))
6018     (or (not gnus-use-cross-reference)
6019         (not headers)
6020         (and (mail-header-xref headers)
6021              (not (string= (mail-header-xref headers) "")))
6022         (let ((case-fold-search t)
6023               xref)
6024           (save-restriction
6025             (nnheader-narrow-to-headers)
6026             (goto-char (point-min))
6027             (when (or (and (not (eobp))
6028                            (eq (downcase (char-after)) ?x)
6029                            (looking-at "Xref:"))
6030                       (search-forward "\nXref:" nil t))
6031               (goto-char (1+ (match-end 0)))
6032               (setq xref (buffer-substring (point) (point-at-eol)))
6033               (mail-header-set-xref headers xref)))))))
6034
6035 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6036   "Find article ID and insert the summary line for that article.
6037 OLD-HEADER can either be a header or a line number to insert
6038 the subject line on."
6039   (let* ((line (and (numberp old-header) old-header))
6040          (old-header (and (vectorp old-header) old-header))
6041          (header (cond ((and old-header use-old-header)
6042                         old-header)
6043                        ((and (numberp id)
6044                              (gnus-number-to-header id))
6045                         (gnus-number-to-header id))
6046                        (t
6047                         (gnus-read-header id))))
6048          (number (and (numberp id) id))
6049          d)
6050     (when header
6051       ;; Rebuild the thread that this article is part of and go to the
6052       ;; article we have fetched.
6053       (when (and (not gnus-show-threads)
6054                  old-header)
6055         (when (and number
6056                    (setq d (gnus-data-find (mail-header-number old-header))))
6057           (goto-char (gnus-data-pos d))
6058           (gnus-data-remove
6059            number
6060            (- (point-at-bol)
6061               (prog1
6062                   (1+ (point-at-eol))
6063                 (gnus-delete-line))))))
6064       ;; Remove list identifiers from subject.
6065       (when gnus-list-identifiers
6066         (let ((gnus-newsgroup-headers (list header)))
6067           (gnus-summary-remove-list-identifiers)
6068           (setq header (car gnus-newsgroup-headers))))
6069       (when old-header
6070         (mail-header-set-number header (mail-header-number old-header)))
6071       (setq gnus-newsgroup-sparse
6072             (delq (setq number (mail-header-number header))
6073                   gnus-newsgroup-sparse))
6074       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6075       (push number gnus-newsgroup-limit)
6076       (gnus-rebuild-thread (mail-header-id header) line)
6077       (gnus-summary-goto-subject number nil t))
6078     (when (and (numberp number)
6079                (> number 0))
6080       ;; We have to update the boundaries even if we can't fetch the
6081       ;; article if ID is a number -- so that the next `P' or `N'
6082       ;; command will fetch the previous (or next) article even
6083       ;; if the one we tried to fetch this time has been canceled.
6084       (when (> number gnus-newsgroup-end)
6085         (setq gnus-newsgroup-end number))
6086       (when (< number gnus-newsgroup-begin)
6087         (setq gnus-newsgroup-begin number))
6088       (setq gnus-newsgroup-unselected
6089             (delq number gnus-newsgroup-unselected)))
6090     ;; Report back a success?
6091     (and header (mail-header-number header))))
6092
6093 ;;; Process/prefix in the summary buffer
6094
6095 (defun gnus-summary-work-articles (n)
6096   "Return a list of articles to be worked upon.
6097 The prefix argument, the list of process marked articles, and the
6098 current article will be taken into consideration."
6099   (save-excursion
6100     (set-buffer gnus-summary-buffer)
6101     (cond
6102      (n
6103       ;; A numerical prefix has been given.
6104       (setq n (prefix-numeric-value n))
6105       (let ((backward (< n 0))
6106             (n (abs (prefix-numeric-value n)))
6107             articles article)
6108         (save-excursion
6109           (while
6110               (and (> n 0)
6111                    (push (setq article (gnus-summary-article-number))
6112                          articles)
6113                    (if backward
6114                        (gnus-summary-find-prev nil article)
6115                      (gnus-summary-find-next nil article)))
6116             (decf n)))
6117         (nreverse articles)))
6118      ((and (gnus-region-active-p) (mark))
6119       (message "region active")
6120       ;; Work on the region between point and mark.
6121       (let ((max (max (point) (mark)))
6122             articles article)
6123         (save-excursion
6124           (goto-char (min (min (point) (mark))))
6125           (while
6126               (and
6127                (push (setq article (gnus-summary-article-number)) articles)
6128                (gnus-summary-find-next nil article)
6129                (< (point) max)))
6130           (nreverse articles))))
6131      (gnus-newsgroup-processable
6132       ;; There are process-marked articles present.
6133       ;; Save current state.
6134       (gnus-summary-save-process-mark)
6135       ;; Return the list.
6136       (reverse gnus-newsgroup-processable))
6137      (t
6138       ;; Just return the current article.
6139       (list (gnus-summary-article-number))))))
6140
6141 (defmacro gnus-summary-iterate (arg &rest forms)
6142   "Iterate over the process/prefixed articles and do FORMS.
6143 ARG is the interactive prefix given to the command.  FORMS will be
6144 executed with point over the summary line of the articles."
6145   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6146     `(let ((,articles (gnus-summary-work-articles ,arg)))
6147        (while ,articles
6148          (gnus-summary-goto-subject (car ,articles))
6149          ,@forms
6150          (pop ,articles)))))
6151
6152 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6153 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6154
6155 (defun gnus-summary-save-process-mark ()
6156   "Push the current set of process marked articles on the stack."
6157   (interactive)
6158   (push (copy-sequence gnus-newsgroup-processable)
6159         gnus-newsgroup-process-stack))
6160
6161 (defun gnus-summary-kill-process-mark ()
6162   "Push the current set of process marked articles on the stack and unmark."
6163   (interactive)
6164   (gnus-summary-save-process-mark)
6165   (gnus-summary-unmark-all-processable))
6166
6167 (defun gnus-summary-yank-process-mark ()
6168   "Pop the last process mark state off the stack and restore it."
6169   (interactive)
6170   (unless gnus-newsgroup-process-stack
6171     (error "Empty mark stack"))
6172   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6173
6174 (defun gnus-summary-process-mark-set (set)
6175   "Make SET into the current process marked articles."
6176   (gnus-summary-unmark-all-processable)
6177   (mapc 'gnus-summary-set-process-mark set))
6178
6179 ;;; Searching and stuff
6180
6181 (defun gnus-summary-search-group (&optional backward use-level)
6182   "Search for next unread newsgroup.
6183 If optional argument BACKWARD is non-nil, search backward instead."
6184   (save-excursion
6185     (set-buffer gnus-group-buffer)
6186     (when (gnus-group-search-forward
6187            backward nil (if use-level (gnus-group-group-level) nil))
6188       (gnus-group-group-name))))
6189
6190 (defun gnus-summary-best-group (&optional exclude-group)
6191   "Find the name of the best unread group.
6192 If EXCLUDE-GROUP, do not go to this group."
6193   (with-current-buffer gnus-group-buffer
6194     (save-excursion
6195       (gnus-group-best-unread-group exclude-group))))
6196
6197 (defun gnus-summary-find-next (&optional unread article backward)
6198   (if backward
6199       (gnus-summary-find-prev unread article)
6200     (let* ((dummy (gnus-summary-article-intangible-p))
6201            (article (or article (gnus-summary-article-number)))
6202            (data (gnus-data-find-list article))
6203            result)
6204       (when (and (not dummy)
6205                  (or (not gnus-summary-check-current)
6206                      (not unread)
6207                      (not (gnus-data-unread-p (car data)))))
6208         (setq data (cdr data)))
6209       (when (setq result
6210                   (if unread
6211                       (progn
6212                         (while data
6213                           (unless (memq (gnus-data-number (car data))
6214                                         (cond
6215                                          ((eq gnus-auto-goto-ignores
6216                                               'always-undownloaded)
6217                                           gnus-newsgroup-undownloaded)
6218                                          (gnus-plugged
6219                                           nil)
6220                                          ((eq gnus-auto-goto-ignores
6221                                               'unfetched)
6222                                           gnus-newsgroup-unfetched)
6223                                          ((eq gnus-auto-goto-ignores
6224                                               'undownloaded)
6225                                           gnus-newsgroup-undownloaded)))
6226                             (when (gnus-data-unread-p (car data))
6227                               (setq result (car data)
6228                                     data nil)))
6229                           (setq data (cdr data)))
6230                         result)
6231                     (car data)))
6232         (goto-char (gnus-data-pos result))
6233         (gnus-data-number result)))))
6234
6235 (defun gnus-summary-find-prev (&optional unread article)
6236   (let* ((eobp (eobp))
6237          (article (or article (gnus-summary-article-number)))
6238          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6239          result)
6240     (when (and (not eobp)
6241                (or (not gnus-summary-check-current)
6242                    (not unread)
6243                    (not (gnus-data-unread-p (car data)))))
6244       (setq data (cdr data)))
6245     (when (setq result
6246                 (if unread
6247                     (progn
6248                       (while data
6249                         (unless (memq (gnus-data-number (car data))
6250                                       (cond
6251                                        ((eq gnus-auto-goto-ignores
6252                                             'always-undownloaded)
6253                                         gnus-newsgroup-undownloaded)
6254                                        (gnus-plugged
6255                                         nil)
6256                                        ((eq gnus-auto-goto-ignores
6257                                             'unfetched)
6258                                         gnus-newsgroup-unfetched)
6259                                        ((eq gnus-auto-goto-ignores
6260                                             'undownloaded)
6261                                         gnus-newsgroup-undownloaded)))
6262                           (when (gnus-data-unread-p (car data))
6263                             (setq result (car data)
6264                                   data nil)))
6265                         (setq data (cdr data)))
6266                       result)
6267                   (car data)))
6268       (goto-char (gnus-data-pos result))
6269       (gnus-data-number result))))
6270
6271 (defun gnus-summary-find-subject (subject &optional unread backward article)
6272   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6273          (article (or article (gnus-summary-article-number)))
6274          (articles (gnus-data-list backward))
6275          (arts (gnus-data-find-list article articles))
6276          result)
6277     (when (or (not gnus-summary-check-current)
6278               (not unread)
6279               (not (gnus-data-unread-p (car arts))))
6280       (setq arts (cdr arts)))
6281     (while arts
6282       (and (or (not unread)
6283                (gnus-data-unread-p (car arts)))
6284            (vectorp (gnus-data-header (car arts)))
6285            (gnus-subject-equal
6286             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6287            (setq result (car arts)
6288                  arts nil))
6289       (setq arts (cdr arts)))
6290     (and result
6291          (goto-char (gnus-data-pos result))
6292          (gnus-data-number result))))
6293
6294 (defun gnus-summary-search-forward (&optional unread subject backward)
6295   "Search forward for an article.
6296 If UNREAD, look for unread articles.  If SUBJECT, look for
6297 articles with that subject.  If BACKWARD, search backward instead."
6298   (cond (subject (gnus-summary-find-subject subject unread backward))
6299         (backward (gnus-summary-find-prev unread))
6300         (t (gnus-summary-find-next unread))))
6301
6302 (defun gnus-recenter (&optional n)
6303   "Center point in window and redisplay frame.
6304 Also do horizontal recentering."
6305   (interactive "P")
6306   (when (and gnus-auto-center-summary
6307              (not (eq gnus-auto-center-summary 'vertical)))
6308     (gnus-horizontal-recenter))
6309   (recenter n))
6310
6311 (defun gnus-summary-recenter ()
6312   "Center point in the summary window.
6313 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6314 displayed, no centering will be performed."
6315   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6316   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6317   (interactive)
6318   ;; The user has to want it.
6319   (when gnus-auto-center-summary
6320     (let* ((top (cond ((< (window-height) 4) 0)
6321                       ((< (window-height) 7) 1)
6322                       (t (if (numberp gnus-auto-center-summary)
6323                              gnus-auto-center-summary
6324                            (/ (1- (window-height)) 2)))))
6325            (height (1- (window-height)))
6326            (bottom (save-excursion (goto-char (point-max))
6327                                    (forward-line (- height))
6328                                    (point)))
6329            (window (get-buffer-window (current-buffer))))
6330       (when (get-buffer-window gnus-article-buffer)
6331         ;; Only do recentering when the article buffer is displayed,
6332         ;; Set the window start to either `bottom', which is the biggest
6333         ;; possible valid number, or the second line from the top,
6334         ;; whichever is the least.
6335         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6336           (if (> bottom top-pos)
6337               ;; Keep the second line from the top visible
6338               (set-window-start window top-pos t)
6339             ;; Try to keep the bottom line visible; if it's partially
6340             ;; obscured, either scroll one more line to make it fully
6341             ;; visible, or revert to using TOP-POS.
6342             (save-excursion
6343               (goto-char (point-max))
6344               (forward-line -1)
6345               (let ((last-line-start (point)))
6346                 (goto-char bottom)
6347                 (set-window-start window (point) t)
6348                 (when (not (pos-visible-in-window-p last-line-start window))
6349                   (forward-line 1)
6350                   (set-window-start window (min (point) top-pos) t)))))))
6351       ;; Do horizontal recentering while we're at it.
6352       (when (and (get-buffer-window (current-buffer) t)
6353                  (not (eq gnus-auto-center-summary 'vertical)))
6354         (let ((selected (selected-window)))
6355           (select-window (get-buffer-window (current-buffer) t))
6356           (gnus-summary-position-point)
6357           (gnus-horizontal-recenter)
6358           (select-window selected))))))
6359
6360 (defun gnus-summary-jump-to-group (newsgroup)
6361   "Move point to NEWSGROUP in group mode buffer."
6362   ;; Keep update point of group mode buffer if visible.
6363   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6364       (save-window-excursion
6365         ;; Take care of tree window mode.
6366         (when (get-buffer-window gnus-group-buffer)
6367           (pop-to-buffer gnus-group-buffer))
6368         (gnus-group-jump-to-group newsgroup))
6369     (save-excursion
6370       ;; Take care of tree window mode.
6371       (if (get-buffer-window gnus-group-buffer)
6372           (pop-to-buffer gnus-group-buffer)
6373         (set-buffer gnus-group-buffer))
6374       (gnus-group-jump-to-group newsgroup))))
6375
6376 ;; This function returns a list of article numbers based on the
6377 ;; difference between the ranges of read articles in this group and
6378 ;; the range of active articles.
6379 (defun gnus-list-of-unread-articles (group)
6380   (let* ((read (gnus-info-read (gnus-get-info group)))
6381          (active (or (gnus-active group) (gnus-activate-group group)))
6382          (last (cdr active))
6383          first nlast unread)
6384     ;; If none are read, then all are unread.
6385     (if (not read)
6386         (setq first (car active))
6387       ;; If the range of read articles is a single range, then the
6388       ;; first unread article is the article after the last read
6389       ;; article.  Sounds logical, doesn't it?
6390       (if (and (not (listp (cdr read)))
6391                (or (< (car read) (car active))
6392                    (progn (setq read (list read))
6393                           nil)))
6394           (setq first (max (car active) (1+ (cdr read))))
6395         ;; `read' is a list of ranges.
6396         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6397                                   (caar read)))
6398                   1)
6399           (setq first (car active)))
6400         (while read
6401           (when first
6402             (while (< first nlast)
6403               (setq unread (cons first unread)
6404                     first (1+ first))))
6405           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6406           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6407           (setq read (cdr read)))))
6408     ;; And add the last unread articles.
6409     (while (<= first last)
6410       (setq unread (cons first unread)
6411             first (1+ first)))
6412     ;; Return the list of unread articles.
6413     (delq 0 (nreverse unread))))
6414
6415 (defun gnus-list-of-read-articles (group)
6416   "Return a list of unread, unticked and non-dormant articles."
6417   (let* ((info (gnus-get-info group))
6418          (marked (gnus-info-marks info))
6419          (active (gnus-active group)))
6420     (and info active
6421          (gnus-list-range-difference
6422           (gnus-list-range-difference
6423            (gnus-sorted-complement
6424             (gnus-uncompress-range active)
6425             (gnus-list-of-unread-articles group))
6426            (cdr (assq 'dormant marked)))
6427           (cdr (assq 'tick marked))))))
6428
6429 ;; This function returns a sequence of article numbers based on the
6430 ;; difference between the ranges of read articles in this group and
6431 ;; the range of active articles.
6432 (defun gnus-sequence-of-unread-articles (group)
6433   (let* ((read (gnus-info-read (gnus-get-info group)))
6434          (active (or (gnus-active group) (gnus-activate-group group)))
6435          (last (cdr active))
6436          first nlast unread)
6437     ;; If none are read, then all are unread.
6438     (if (not read)
6439         (setq first (car active))
6440       ;; If the range of read articles is a single range, then the
6441       ;; first unread article is the article after the last read
6442       ;; article.  Sounds logical, doesn't it?
6443       (if (and (not (listp (cdr read)))
6444                (or (< (car read) (car active))
6445                    (progn (setq read (list read))
6446                           nil)))
6447           (setq first (max (car active) (1+ (cdr read))))
6448         ;; `read' is a list of ranges.
6449         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6450                                   (caar read)))
6451                   1)
6452           (setq first (car active)))
6453         (while read
6454           (when first
6455             (push (cons first nlast) unread))
6456           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6457           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6458           (setq read (cdr read)))))
6459     ;; And add the last unread articles.
6460     (cond ((< first last)
6461            (push (cons first last) unread))
6462           ((= first last)
6463            (push first unread)))
6464     ;; Return the sequence of unread articles.
6465     (delq 0 (nreverse unread))))
6466
6467 ;; Various summary commands
6468
6469 (defun gnus-summary-select-article-buffer ()
6470   "Reconfigure windows to show article buffer."
6471   (interactive)
6472   (if (not (gnus-buffer-live-p gnus-article-buffer))
6473       (error "There is no article buffer for this summary buffer")
6474     (gnus-configure-windows 'article)
6475     (select-window (get-buffer-window gnus-article-buffer))))
6476
6477 (defun gnus-summary-universal-argument (arg)
6478   "Perform any operation on all articles that are process/prefixed."
6479   (interactive "P")
6480   (let ((articles (gnus-summary-work-articles arg))
6481         func article)
6482     (if (eq
6483          (setq
6484           func
6485           (key-binding
6486            (read-key-sequence
6487             (substitute-command-keys
6488              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6489          'undefined)
6490         (gnus-error 1 "Undefined key")
6491       (save-excursion
6492         (while articles
6493           (gnus-summary-goto-subject (setq article (pop articles)))
6494           (let (gnus-newsgroup-processable)
6495             (command-execute func))
6496           (gnus-summary-remove-process-mark article)))))
6497   (gnus-summary-position-point))
6498
6499 (defun gnus-summary-toggle-truncation (&optional arg)
6500   "Toggle truncation of summary lines.
6501 With ARG, turn line truncation on if ARG is positive."
6502   (interactive "P")
6503   (setq truncate-lines
6504         (if (null arg) (not truncate-lines)
6505           (> (prefix-numeric-value arg) 0)))
6506   (redraw-display))
6507
6508 (defun gnus-summary-find-for-reselect ()
6509   "Return the number of an article to stay on across a reselect.
6510 The current article is considered, then following articles, then previous
6511 articles.  An article is sought which is not cancelled and isn't a temporary
6512 insertion from another group.  If there's no such then return a dummy 0."
6513   (let (found)
6514     (dolist (rev '(nil t))
6515       (unless found      ; don't demand the reverse list if we don't need it
6516         (let ((data (gnus-data-find-list
6517                      (gnus-summary-article-number) (gnus-data-list rev))))
6518           (while (and data (not found))
6519             (if (and (< 0 (gnus-data-number (car data)))
6520                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6521                 (setq found (gnus-data-number (car data))))
6522             (setq data (cdr data))))))
6523     (or found 0)))
6524
6525 (defun gnus-summary-reselect-current-group (&optional all rescan)
6526   "Exit and then reselect the current newsgroup.
6527 The prefix argument ALL means to select all articles."
6528   (interactive "P")
6529   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6530     (error "Ephemeral groups can't be reselected"))
6531   (let ((current-subject (gnus-summary-find-for-reselect))
6532         (group gnus-newsgroup-name))
6533     (setq gnus-newsgroup-begin nil)
6534     (gnus-summary-exit nil 'leave-hidden)
6535     ;; We have to adjust the point of group mode buffer because
6536     ;; point was moved to the next unread newsgroup by exiting.
6537     (gnus-summary-jump-to-group group)
6538     (when rescan
6539       (save-excursion
6540         (gnus-group-get-new-news-this-group 1)))
6541     (gnus-group-read-group all t)
6542     (gnus-summary-goto-subject current-subject nil t)))
6543
6544 (defun gnus-summary-rescan-group (&optional all)
6545   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6546   (interactive "P")
6547   (gnus-summary-reselect-current-group all t))
6548
6549 (defun gnus-summary-update-info (&optional non-destructive)
6550   (save-excursion
6551     (let ((group gnus-newsgroup-name))
6552       (when group
6553         (when gnus-newsgroup-kill-headers
6554           (setq gnus-newsgroup-killed
6555                 (gnus-compress-sequence
6556                  (gnus-sorted-union
6557                   (gnus-list-range-intersection
6558                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6559                   gnus-newsgroup-unreads)
6560                  t)))
6561         (unless (listp (cdr gnus-newsgroup-killed))
6562           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6563         (let ((headers gnus-newsgroup-headers))
6564           ;; Set the new ranges of read articles.
6565           (with-current-buffer gnus-group-buffer
6566             (gnus-undo-force-boundary))
6567           (gnus-update-read-articles
6568            group (gnus-sorted-union
6569                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6570           ;; Set the current article marks.
6571           (let ((gnus-newsgroup-scored
6572                  (if (and (not gnus-save-score)
6573                           (not non-destructive))
6574                      nil
6575                    gnus-newsgroup-scored)))
6576             (save-excursion
6577               (gnus-update-marks)))
6578           ;; Do the cross-ref thing.
6579           (when gnus-use-cross-reference
6580             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6581           ;; Do not switch windows but change the buffer to work.
6582           (set-buffer gnus-group-buffer)
6583           (unless (gnus-ephemeral-group-p group)
6584             (gnus-group-update-group group)))))))
6585
6586 (defun gnus-summary-save-newsrc (&optional force)
6587   "Save the current number of read/marked articles in the dribble buffer.
6588 The dribble buffer will then be saved.
6589 If FORCE (the prefix), also save the .newsrc file(s)."
6590   (interactive "P")
6591   (gnus-summary-update-info t)
6592   (if force
6593       (gnus-save-newsrc-file)
6594     (gnus-dribble-save)))
6595
6596 (defun gnus-summary-exit (&optional temporary leave-hidden)
6597   "Exit reading current newsgroup, and then return to group selection mode.
6598 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6599   (interactive)
6600   (gnus-set-global-variables)
6601   (when (gnus-buffer-live-p gnus-article-buffer)
6602     (save-excursion
6603       (set-buffer gnus-article-buffer)
6604       (mm-destroy-parts gnus-article-mime-handles)
6605       ;; Set it to nil for safety reason.
6606       (setq gnus-article-mime-handle-alist nil)
6607       (setq gnus-article-mime-handles nil)))
6608   (gnus-kill-save-kill-buffer)
6609   (gnus-async-halt-prefetch)
6610   (let* ((group gnus-newsgroup-name)
6611          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6612          (gnus-group-is-exiting-p t)
6613          (mode major-mode)
6614          (group-point nil)
6615          (buf (current-buffer)))
6616     (unless quit-config
6617       ;; Do adaptive scoring, and possibly save score files.
6618       (when gnus-newsgroup-adaptive
6619         (gnus-score-adaptive))
6620       (when gnus-use-scoring
6621         (gnus-score-save)))
6622     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6623     ;; If we have several article buffers, we kill them at exit.
6624     (unless gnus-single-article-buffer
6625       (gnus-kill-buffer gnus-original-article-buffer)
6626       (setq gnus-article-current nil))
6627     (when gnus-use-cache
6628       (gnus-cache-possibly-remove-articles)
6629       (gnus-cache-save-buffers))
6630     (gnus-async-prefetch-remove-group group)
6631     (when gnus-suppress-duplicates
6632       (gnus-dup-enter-articles))
6633     (when gnus-use-trees
6634       (gnus-tree-close group))
6635     (when gnus-use-cache
6636       (gnus-cache-write-active))
6637     ;; Remove entries for this group.
6638     (nnmail-purge-split-history (gnus-group-real-name group))
6639     ;; Make all changes in this group permanent.
6640     (unless quit-config
6641       (gnus-run-hooks 'gnus-exit-group-hook)
6642       (gnus-summary-update-info))
6643     (gnus-close-group group)
6644     ;; Make sure where we were, and go to next newsgroup.
6645     (set-buffer gnus-group-buffer)
6646     (unless quit-config
6647       (gnus-group-jump-to-group group))
6648     (gnus-run-hooks 'gnus-summary-exit-hook)
6649     (unless (or quit-config
6650                 ;; If this group has disappeared from the summary
6651                 ;; buffer, don't skip forwards.
6652                 (not (string= group (gnus-group-group-name))))
6653       (gnus-group-next-unread-group 1))
6654     (setq group-point (point))
6655     (if temporary
6656         nil                             ;Nothing to do.
6657       ;; If we have several article buffers, we kill them at exit.
6658       (unless gnus-single-article-buffer
6659         (gnus-kill-buffer gnus-article-buffer)
6660         (gnus-kill-buffer gnus-original-article-buffer)
6661         (setq gnus-article-current nil))
6662       (set-buffer buf)
6663       (if (not gnus-kill-summary-on-exit)
6664           (progn
6665             (gnus-deaden-summary)
6666             (setq mode nil))
6667         ;; We set all buffer-local variables to nil.  It is unclear why
6668         ;; this is needed, but if we don't, buffer-local variables are
6669         ;; not garbage-collected, it seems.  This would the lead to en
6670         ;; ever-growing Emacs.
6671         (gnus-summary-clear-local-variables)
6672         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6673           (gnus-summary-clear-local-variables))
6674         (when (get-buffer gnus-article-buffer)
6675           (bury-buffer gnus-article-buffer))
6676         ;; We clear the global counterparts of the buffer-local
6677         ;; variables as well, just to be on the safe side.
6678         (set-buffer gnus-group-buffer)
6679         (gnus-summary-clear-local-variables)
6680         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6681           (gnus-summary-clear-local-variables))
6682         ;; Return to group mode buffer.
6683         (when (eq mode 'gnus-summary-mode)
6684           (gnus-kill-buffer buf)))
6685       (setq gnus-current-select-method gnus-select-method)
6686       (if leave-hidden
6687           (set-buffer gnus-group-buffer)
6688         (pop-to-buffer gnus-group-buffer))
6689       (if (not quit-config)
6690           (progn
6691             (goto-char group-point)
6692             (unless leave-hidden
6693               (gnus-configure-windows 'group 'force)))
6694         (gnus-handle-ephemeral-exit quit-config))
6695       ;; Clear the current group name.
6696       (unless quit-config
6697         (setq gnus-newsgroup-name nil)))))
6698
6699 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6700 (defun gnus-summary-exit-no-update (&optional no-questions)
6701   "Quit reading current newsgroup without updating read article info."
6702   (interactive)
6703   (let* ((group gnus-newsgroup-name)
6704          (gnus-group-is-exiting-p t)
6705          (gnus-group-is-exiting-without-update-p t)
6706          (quit-config (gnus-group-quit-config group)))
6707     (when (or no-questions
6708               gnus-expert-user
6709               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6710       (gnus-async-halt-prefetch)
6711       (run-hooks 'gnus-summary-prepare-exit-hook)
6712       (when (gnus-buffer-live-p gnus-article-buffer)
6713         (save-excursion
6714           (set-buffer gnus-article-buffer)
6715           (mm-destroy-parts gnus-article-mime-handles)
6716           ;; Set it to nil for safety reason.
6717           (setq gnus-article-mime-handle-alist nil)
6718           (setq gnus-article-mime-handles nil)))
6719       ;; If we have several article buffers, we kill them at exit.
6720       (unless gnus-single-article-buffer
6721         (gnus-kill-buffer gnus-article-buffer)
6722         (gnus-kill-buffer gnus-original-article-buffer)
6723         (setq gnus-article-current nil))
6724       (if (not gnus-kill-summary-on-exit)
6725           (gnus-deaden-summary)
6726         (gnus-close-group group)
6727         (gnus-summary-clear-local-variables)
6728         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6729           (gnus-summary-clear-local-variables))
6730         (set-buffer gnus-group-buffer)
6731         (gnus-summary-clear-local-variables)
6732         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6733           (gnus-summary-clear-local-variables))
6734         (gnus-kill-buffer gnus-summary-buffer))
6735       (unless gnus-single-article-buffer
6736         (setq gnus-article-current nil))
6737       (when gnus-use-trees
6738         (gnus-tree-close group))
6739       (gnus-async-prefetch-remove-group group)
6740       (when (get-buffer gnus-article-buffer)
6741         (bury-buffer gnus-article-buffer))
6742       ;; Return to the group buffer.
6743       (gnus-configure-windows 'group 'force)
6744       ;; Clear the current group name.
6745       (setq gnus-newsgroup-name nil)
6746       (unless (gnus-ephemeral-group-p group)
6747         (gnus-group-update-group group))
6748       (when (equal (gnus-group-group-name) group)
6749         (gnus-group-next-unread-group 1))
6750       (when quit-config
6751         (gnus-handle-ephemeral-exit quit-config)))))
6752
6753 (defun gnus-handle-ephemeral-exit (quit-config)
6754   "Handle movement when leaving an ephemeral group.
6755 The state which existed when entering the ephemeral is reset."
6756   (if (not (buffer-name (car quit-config)))
6757       (gnus-configure-windows 'group 'force)
6758     (set-buffer (car quit-config))
6759     (cond ((eq major-mode 'gnus-summary-mode)
6760            (gnus-set-global-variables))
6761           ((eq major-mode 'gnus-article-mode)
6762            (save-excursion
6763              ;; The `gnus-summary-buffer' variable may point
6764              ;; to the old summary buffer when using a single
6765              ;; article buffer.
6766              (unless (gnus-buffer-live-p gnus-summary-buffer)
6767                (set-buffer gnus-group-buffer))
6768              (set-buffer gnus-summary-buffer)
6769              (gnus-set-global-variables))))
6770     (if (or (eq (cdr quit-config) 'article)
6771             (eq (cdr quit-config) 'pick))
6772         (progn
6773           ;; The current article may be from the ephemeral group
6774           ;; thus it is best that we reload this article
6775           ;;
6776           ;; If we're exiting from a large digest, this can be
6777           ;; extremely slow.  So, it's better not to reload it. -- jh.
6778           ;;(gnus-summary-show-article)
6779           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6780               (gnus-configure-windows 'pick 'force)
6781             (gnus-configure-windows (cdr quit-config) 'force)))
6782       (gnus-configure-windows (cdr quit-config) 'force))
6783     (when (eq major-mode 'gnus-summary-mode)
6784       (gnus-summary-next-subject 1 nil t)
6785       (gnus-summary-recenter)
6786       (gnus-summary-position-point))))
6787
6788 ;;; Dead summaries.
6789
6790 (defvar gnus-dead-summary-mode-map nil)
6791
6792 (unless gnus-dead-summary-mode-map
6793   (setq gnus-dead-summary-mode-map (make-keymap))
6794   (suppress-keymap gnus-dead-summary-mode-map)
6795   (substitute-key-definition
6796    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6797   (dolist (key '("\C-d" "\r" "\177" [delete]))
6798     (define-key gnus-dead-summary-mode-map
6799       key 'gnus-summary-wake-up-the-dead))
6800   (dolist (key '("q" "Q"))
6801     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6802
6803 (defvar gnus-dead-summary-mode nil
6804   "Minor mode for Gnus summary buffers.")
6805
6806 (defun gnus-dead-summary-mode (&optional arg)
6807   "Minor mode for Gnus summary buffers."
6808   (interactive "P")
6809   (when (eq major-mode 'gnus-summary-mode)
6810     (make-local-variable 'gnus-dead-summary-mode)
6811     (setq gnus-dead-summary-mode
6812           (if (null arg) (not gnus-dead-summary-mode)
6813             (> (prefix-numeric-value arg) 0)))
6814     (when gnus-dead-summary-mode
6815       (add-minor-mode
6816        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6817
6818 (defun gnus-deaden-summary ()
6819   "Make the current summary buffer into a dead summary buffer."
6820   ;; Kill any previous dead summary buffer.
6821   (when (and gnus-dead-summary
6822              (buffer-name gnus-dead-summary))
6823     (with-current-buffer gnus-dead-summary
6824       (when gnus-dead-summary-mode
6825         (kill-buffer (current-buffer)))))
6826   ;; Make this the current dead summary.
6827   (setq gnus-dead-summary (current-buffer))
6828   (gnus-dead-summary-mode 1)
6829   (let ((name (buffer-name)))
6830     (when (string-match "Summary" name)
6831       (rename-buffer
6832        (concat (substring name 0 (match-beginning 0)) "Dead "
6833                (substring name (match-beginning 0)))
6834        t)
6835       (bury-buffer))))
6836
6837 (defun gnus-kill-or-deaden-summary (buffer)
6838   "Kill or deaden the summary BUFFER."
6839   (save-excursion
6840     (when (and (buffer-name buffer)
6841                (not gnus-single-article-buffer))
6842       (with-current-buffer buffer
6843         (gnus-kill-buffer gnus-article-buffer)
6844         (gnus-kill-buffer gnus-original-article-buffer)))
6845     (cond
6846      ;; Kill the buffer.
6847      (gnus-kill-summary-on-exit
6848       (when (and gnus-use-trees
6849                  (gnus-buffer-exists-p buffer))
6850         (save-excursion
6851           (set-buffer buffer)
6852           (gnus-tree-close gnus-newsgroup-name)))
6853       (gnus-kill-buffer buffer))
6854      ;; Deaden the buffer.
6855      ((gnus-buffer-exists-p buffer)
6856       (save-excursion
6857         (set-buffer buffer)
6858         (gnus-deaden-summary))))))
6859
6860 (defun gnus-summary-wake-up-the-dead (&rest args)
6861   "Wake up the dead summary buffer."
6862   (interactive)
6863   (gnus-dead-summary-mode -1)
6864   (let ((name (buffer-name)))
6865     (when (string-match "Dead " name)
6866       (rename-buffer
6867        (concat (substring name 0 (match-beginning 0))
6868                (substring name (match-end 0)))
6869        t)))
6870   (gnus-message 3 "This dead summary is now alive again"))
6871
6872 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6873 (defun gnus-summary-fetch-faq (&optional faq-dir)
6874   "Fetch the FAQ for the current group.
6875 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6876 in."
6877   (interactive
6878    (list
6879     (when current-prefix-arg
6880       (completing-read
6881        "FAQ dir: " (and (listp gnus-group-faq-directory)
6882                         (mapcar 'list
6883                                 gnus-group-faq-directory))))))
6884   (let (gnus-faq-buffer)
6885     (when (setq gnus-faq-buffer
6886                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6887       (gnus-configure-windows 'summary-faq))))
6888
6889 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6890 (defun gnus-summary-describe-group (&optional force)
6891   "Describe the current newsgroup."
6892   (interactive "P")
6893   (gnus-group-describe-group force gnus-newsgroup-name))
6894
6895 (defun gnus-summary-describe-briefly ()
6896   "Describe summary mode commands briefly."
6897   (interactive)
6898   (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")))
6899
6900 ;; Walking around group mode buffer from summary mode.
6901
6902 (defun gnus-summary-next-group (&optional no-article target-group backward)
6903   "Exit current newsgroup and then select next unread newsgroup.
6904 If prefix argument NO-ARTICLE is non-nil, no article is selected
6905 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6906 previous group instead."
6907   (interactive "P")
6908   ;; Stop pre-fetching.
6909   (gnus-async-halt-prefetch)
6910   (let ((current-group gnus-newsgroup-name)
6911         (current-buffer (current-buffer))
6912         entered)
6913     ;; First we semi-exit this group to update Xrefs and all variables.
6914     ;; We can't do a real exit, because the window conf must remain
6915     ;; the same in case the user is prompted for info, and we don't
6916     ;; want the window conf to change before that...
6917     (gnus-summary-exit t)
6918     (while (not entered)
6919       ;; Then we find what group we are supposed to enter.
6920       (set-buffer gnus-group-buffer)
6921       (gnus-group-jump-to-group current-group)
6922       (setq target-group
6923             (or target-group
6924                 (if (eq gnus-keep-same-level 'best)
6925                     (gnus-summary-best-group gnus-newsgroup-name)
6926                   (gnus-summary-search-group backward gnus-keep-same-level))))
6927       (if (not target-group)
6928           ;; There are no further groups, so we return to the group
6929           ;; buffer.
6930           (progn
6931             (gnus-message 5 "Returning to the group buffer")
6932             (setq entered t)
6933             (when (gnus-buffer-live-p current-buffer)
6934               (set-buffer current-buffer)
6935               (gnus-summary-exit))
6936             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6937         ;; We try to enter the target group.
6938         (gnus-group-jump-to-group target-group)
6939         (let ((unreads (gnus-group-group-unread)))
6940           (if (and (or (eq t unreads)
6941                        (and unreads (not (zerop unreads))))
6942                    (gnus-summary-read-group
6943                     target-group nil no-article
6944                     (and (buffer-name current-buffer) current-buffer)
6945                     nil backward))
6946               (setq entered t)
6947             (setq current-group target-group
6948                   target-group nil)))))))
6949
6950 (defun gnus-summary-prev-group (&optional no-article)
6951   "Exit current newsgroup and then select previous unread newsgroup.
6952 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6953   (interactive "P")
6954   (gnus-summary-next-group no-article nil t))
6955
6956 ;; Walking around summary lines.
6957
6958 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6959   "Go to the first subject satisfying any non-nil constraint.
6960 If UNREAD is non-nil, the article should be unread.
6961 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6962 If UNSEEN is non-nil, the article should be unseen.
6963 Returns the article selected or nil if there are no matching articles."
6964   (interactive "P")
6965   (cond
6966    ;; Empty summary.
6967    ((null gnus-newsgroup-data)
6968     (gnus-message 3 "No articles in the group")
6969     nil)
6970    ;; Pick the first article.
6971    ((not (or unread undownloaded unseen))
6972     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6973     (gnus-data-number (car gnus-newsgroup-data)))
6974    ;; Find the first unread article.
6975    (t
6976     (let ((data gnus-newsgroup-data))
6977       (while (and data
6978                   (let ((num (gnus-data-number (car data))))
6979                     (or (memq num gnus-newsgroup-unfetched)
6980                         (not (or (and unread
6981                                       (memq num gnus-newsgroup-unreads))
6982                                  (and undownloaded
6983                                       (memq num gnus-newsgroup-undownloaded))
6984                                  (and unseen
6985                                       (memq num gnus-newsgroup-unseen)))))))
6986         (setq data (cdr data)))
6987       (prog1
6988           (if data
6989               (progn
6990                 (goto-char (gnus-data-pos (car data)))
6991                 (gnus-data-number (car data)))
6992             (gnus-message 3 "No more%s articles"
6993                           (let* ((r (when unread " unread"))
6994                                  (d (when undownloaded " undownloaded"))
6995                                  (s (when unseen " unseen"))
6996                                  (l (delq nil (list r d s))))
6997                             (cond ((= 3 (length l))
6998                                    (concat r "," d ", or" s))
6999                                   ((= 2 (length l))
7000                                    (concat (car l) ", or" (cadr l)))
7001                                   ((= 1 (length l))
7002                                    (car l))
7003                                   (t
7004                                    ""))))
7005             nil
7006             )
7007         (gnus-summary-position-point))))))
7008
7009 (defun gnus-summary-next-subject (n &optional unread dont-display)
7010   "Go to next N'th summary line.
7011 If N is negative, go to the previous N'th subject line.
7012 If UNREAD is non-nil, only unread articles are selected.
7013 The difference between N and the actual number of steps taken is
7014 returned."
7015   (interactive "p")
7016   (let ((backward (< n 0))
7017         (n (abs n)))
7018     (while (and (> n 0)
7019                 (if backward
7020                     (gnus-summary-find-prev unread)
7021                   (gnus-summary-find-next unread)))
7022       (unless (zerop (setq n (1- n)))
7023         (gnus-summary-show-thread)))
7024     (when (/= 0 n)
7025       (gnus-message 7 "No more%s articles"
7026                     (if unread " unread" "")))
7027     (unless dont-display
7028       (gnus-summary-recenter)
7029       (gnus-summary-position-point))
7030     n))
7031
7032 (defun gnus-summary-next-unread-subject (n)
7033   "Go to next N'th unread summary line."
7034   (interactive "p")
7035   (gnus-summary-next-subject n t))
7036
7037 (defun gnus-summary-prev-subject (n &optional unread)
7038   "Go to previous N'th summary line.
7039 If optional argument UNREAD is non-nil, only unread article is selected."
7040   (interactive "p")
7041   (gnus-summary-next-subject (- n) unread))
7042
7043 (defun gnus-summary-prev-unread-subject (n)
7044   "Go to previous N'th unread summary line."
7045   (interactive "p")
7046   (gnus-summary-next-subject (- n) t))
7047
7048 (defun gnus-summary-goto-subjects (articles)
7049   "Insert the subject header for ARTICLES in the current buffer."
7050   (save-excursion
7051     (dolist (article articles)
7052       (gnus-summary-goto-subject article t)))
7053   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7054   (gnus-summary-position-point))
7055  
7056 (defun gnus-summary-goto-subject (article &optional force silent)
7057   "Go the subject line of ARTICLE.
7058 If FORCE, also allow jumping to articles not currently shown."
7059   (interactive "nArticle number: ")
7060   (unless (numberp article)
7061     (error "Article %s is not a number" article))
7062   (let ((b (point))
7063         (data (gnus-data-find article)))
7064     ;; We read in the article if we have to.
7065     (and (not data)
7066          force
7067          (gnus-summary-insert-subject
7068           article
7069           (if (or (numberp force) (vectorp force)) force)
7070           t)
7071          (setq data (gnus-data-find article)))
7072     (goto-char b)
7073     (if (not data)
7074         (progn
7075           (unless silent
7076             (gnus-message 3 "Can't find article %d" article))
7077           nil)
7078       (let ((pt (gnus-data-pos data)))
7079         (goto-char pt)
7080         (gnus-summary-set-article-display-arrow pt))
7081       (gnus-summary-position-point)
7082       article)))
7083
7084 ;; Walking around summary lines with displaying articles.
7085
7086 (defun gnus-summary-expand-window (&optional arg)
7087   "Make the summary buffer take up the entire Emacs frame.
7088 Given a prefix, will force an `article' buffer configuration."
7089   (interactive "P")
7090   (if arg
7091       (gnus-configure-windows 'article 'force)
7092     (gnus-configure-windows 'summary 'force)))
7093
7094 (defun gnus-summary-display-article (article &optional all-header)
7095   "Display ARTICLE in article buffer."
7096   (when (gnus-buffer-live-p gnus-article-buffer)
7097     (with-current-buffer gnus-article-buffer
7098       (mm-enable-multibyte)))
7099   (gnus-set-global-variables)
7100   (when (gnus-buffer-live-p gnus-article-buffer)
7101     (with-current-buffer gnus-article-buffer
7102       (setq gnus-article-charset gnus-newsgroup-charset)
7103       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7104       (mm-enable-multibyte)))
7105   (if (null article)
7106       nil
7107     (prog1
7108         (if gnus-summary-display-article-function
7109             (funcall gnus-summary-display-article-function article all-header)
7110           (gnus-article-prepare article all-header))
7111       (gnus-run-hooks 'gnus-select-article-hook)
7112       (when (and gnus-current-article
7113                  (not (zerop gnus-current-article)))
7114         (gnus-summary-goto-subject gnus-current-article))
7115       (gnus-summary-recenter)
7116       (when (and gnus-use-trees gnus-show-threads)
7117         (gnus-possibly-generate-tree article)
7118         (gnus-highlight-selected-tree article))
7119       ;; Successfully display article.
7120       (gnus-article-set-window-start
7121        (cdr (assq article gnus-newsgroup-bookmarks))))))
7122
7123 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7124   "Select the current article.
7125 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7126 non-nil, the article will be re-fetched even if it already present in
7127 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7128 be displayed."
7129   ;; Make sure we are in the summary buffer to work around bbdb bug.
7130   (unless (eq major-mode 'gnus-summary-mode)
7131     (set-buffer gnus-summary-buffer))
7132   (let ((article (or article (gnus-summary-article-number)))
7133         (all-headers (not (not all-headers))) ;Must be t or nil.
7134         gnus-summary-display-article-function)
7135     (and (not pseudo)
7136          (gnus-summary-article-pseudo-p article)
7137          (error "This is a pseudo-article"))
7138     (save-excursion
7139       (set-buffer gnus-summary-buffer)
7140       (if (or (and gnus-single-article-buffer
7141                    (or (null gnus-current-article)
7142                        (null gnus-article-current)
7143                        (null (get-buffer gnus-article-buffer))
7144                        (not (eq article (cdr gnus-article-current)))
7145                        (not (equal (car gnus-article-current)
7146                                    gnus-newsgroup-name))))
7147               (and (not gnus-single-article-buffer)
7148                    (or (null gnus-current-article)
7149                        (not (eq gnus-current-article article))))
7150               force)
7151           ;; The requested article is different from the current article.
7152           (progn
7153             (gnus-summary-display-article article all-headers)
7154             (when (gnus-buffer-live-p gnus-article-buffer)
7155               (with-current-buffer gnus-article-buffer
7156                 (if (not gnus-article-decoded-p) ;; a local variable
7157                     (mm-disable-multibyte))))
7158             (gnus-article-set-window-start
7159              (cdr (assq article gnus-newsgroup-bookmarks)))
7160             article)
7161         'old))))
7162
7163 (defun gnus-summary-force-verify-and-decrypt ()
7164   "Display buttons for signed/encrypted parts and verify/decrypt them."
7165   (interactive)
7166   (let ((mm-verify-option 'known)
7167         (mm-decrypt-option 'known)
7168         (gnus-article-emulate-mime t)
7169         (gnus-buttonized-mime-types (append (list "multipart/signed"
7170                                                   "multipart/encrypted")
7171                                             gnus-buttonized-mime-types)))
7172     (gnus-summary-select-article nil 'force)))
7173
7174 (defun gnus-summary-set-current-mark (&optional current-mark)
7175   "Obsolete function."
7176   nil)
7177
7178 (defun gnus-summary-next-article (&optional unread subject backward push)
7179   "Select the next article.
7180 If UNREAD, only unread articles are selected.
7181 If SUBJECT, only articles with SUBJECT are selected.
7182 If BACKWARD, the previous article is selected instead of the next."
7183   (interactive "P")
7184   (cond
7185    ;; Is there such an article?
7186    ((and (gnus-summary-search-forward unread subject backward)
7187          (or (gnus-summary-display-article (gnus-summary-article-number))
7188              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7189     (gnus-summary-position-point))
7190    ;; If not, we try the first unread, if that is wanted.
7191    ((and subject
7192          gnus-auto-select-same
7193          (gnus-summary-first-unread-article))
7194     (gnus-summary-position-point)
7195     (gnus-message 6 "Wrapped"))
7196    ;; Try to get next/previous article not displayed in this group.
7197    ((and gnus-auto-extend-newsgroup
7198          (not unread) (not subject))
7199     (gnus-summary-goto-article
7200      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7201      nil (count-lines (point-min) (point))))
7202    ;; Go to next/previous group.
7203    (t
7204     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7205       (gnus-summary-jump-to-group gnus-newsgroup-name))
7206     (let ((cmd last-command-char)
7207           (point
7208            (with-current-buffer gnus-group-buffer
7209              (point)))
7210           (group
7211            (if (eq gnus-keep-same-level 'best)
7212                (gnus-summary-best-group gnus-newsgroup-name)
7213              (gnus-summary-search-group backward gnus-keep-same-level))))
7214       ;; For some reason, the group window gets selected.  We change
7215       ;; it back.
7216       (select-window (get-buffer-window (current-buffer)))
7217       ;; Select next unread newsgroup automagically.
7218       (cond
7219        ((or (not gnus-auto-select-next)
7220             (not cmd))
7221         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7222        ((or (eq gnus-auto-select-next 'quietly)
7223             (and (eq gnus-auto-select-next 'slightly-quietly)
7224                  push)
7225             (and (eq gnus-auto-select-next 'almost-quietly)
7226                  (gnus-summary-last-article-p)))
7227         ;; Select quietly.
7228         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7229             (gnus-summary-exit)
7230           (gnus-message 7 "No more%s articles (%s)..."
7231                         (if unread " unread" "")
7232                         (if group (concat "selecting " group)
7233                           "exiting"))
7234           (gnus-summary-next-group nil group backward)))
7235        (t
7236         (when (gnus-key-press-event-p last-input-event)
7237           (gnus-summary-walk-group-buffer
7238            gnus-newsgroup-name cmd unread backward point))))))))
7239
7240 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7241   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7242                       (?\C-p (gnus-group-prev-unread-group 1))))
7243         (cursor-in-echo-area t)
7244         keve key group ended prompt)
7245     (save-excursion
7246       (set-buffer gnus-group-buffer)
7247       (goto-char start)
7248       (setq group
7249             (if (eq gnus-keep-same-level 'best)
7250                 (gnus-summary-best-group gnus-newsgroup-name)
7251               (gnus-summary-search-group backward gnus-keep-same-level))))
7252     (while (not ended)
7253       (setq prompt
7254             (format
7255              "No more%s articles%s " (if unread " unread" "")
7256              (if (and group
7257                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7258                  (format " (Type %s for %s [%s])"
7259                          (single-key-description cmd) group
7260                          (gnus-group-unread group))
7261                (format " (Type %s to exit %s)"
7262                        (single-key-description cmd)
7263                        gnus-newsgroup-name))))
7264       ;; Confirm auto selection.
7265       (setq key (car (setq keve (gnus-read-event-char prompt)))
7266             ended t)
7267       (cond
7268        ((assq key keystrokes)
7269         (let ((obuf (current-buffer)))
7270           (switch-to-buffer gnus-group-buffer)
7271           (when group
7272             (gnus-group-jump-to-group group))
7273           (eval (cadr (assq key keystrokes)))
7274           (setq group (gnus-group-group-name))
7275           (switch-to-buffer obuf))
7276         (setq ended nil))
7277        ((equal key cmd)
7278         (if (or (not group)
7279                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7280             (gnus-summary-exit)
7281           (gnus-summary-next-group nil group backward)))
7282        (t
7283         (push (cdr keve) unread-command-events))))))
7284
7285 (defun gnus-summary-next-unread-article ()
7286   "Select unread article after current one."
7287   (interactive)
7288   (gnus-summary-next-article
7289    (or (not (eq gnus-summary-goto-unread 'never))
7290        (gnus-summary-last-article-p (gnus-summary-article-number)))
7291    (and gnus-auto-select-same
7292         (gnus-summary-article-subject))))
7293
7294 (defun gnus-summary-prev-article (&optional unread subject)
7295   "Select the article after the current one.
7296 If UNREAD is non-nil, only unread articles are selected."
7297   (interactive "P")
7298   (gnus-summary-next-article unread subject t))
7299
7300 (defun gnus-summary-prev-unread-article ()
7301   "Select unread article before current one."
7302   (interactive)
7303   (gnus-summary-prev-article
7304    (or (not (eq gnus-summary-goto-unread 'never))
7305        (gnus-summary-first-article-p (gnus-summary-article-number)))
7306    (and gnus-auto-select-same
7307         (gnus-summary-article-subject))))
7308
7309 (defun gnus-summary-next-page (&optional lines circular stop)
7310   "Show next page of the selected article.
7311 If at the end of the current article, select the next article.
7312 LINES says how many lines should be scrolled up.
7313
7314 If CIRCULAR is non-nil, go to the start of the article instead of
7315 selecting the next article when reaching the end of the current
7316 article.
7317
7318 If STOP is non-nil, just stop when reaching the end of the message.
7319
7320 Also see the variable `gnus-article-skip-boring'."
7321   (interactive "P")
7322   (setq gnus-summary-buffer (current-buffer))
7323   (gnus-set-global-variables)
7324   (let ((article (gnus-summary-article-number))
7325         (article-window (get-buffer-window gnus-article-buffer t))
7326         endp)
7327     ;; If the buffer is empty, we have no article.
7328     (unless article
7329       (error "No article to select"))
7330     (gnus-configure-windows 'article)
7331     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7332         (if (and (eq gnus-summary-goto-unread 'never)
7333                  (not (gnus-summary-last-article-p article)))
7334             (gnus-summary-next-article)
7335           (gnus-summary-next-unread-article))
7336       (if (or (null gnus-current-article)
7337               (null gnus-article-current)
7338               (/= article (cdr gnus-article-current))
7339               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7340           ;; Selected subject is different from current article's.
7341           (gnus-summary-display-article article)
7342         (when article-window
7343           (gnus-eval-in-buffer-window gnus-article-buffer
7344             (setq endp (or (gnus-article-next-page lines)
7345                            (gnus-article-only-boring-p))))
7346           (when endp
7347             (cond (stop
7348                    (gnus-message 3 "End of message"))
7349                   (circular
7350                    (gnus-summary-beginning-of-article))
7351                   (lines
7352                    (gnus-message 3 "End of message"))
7353                   ((null lines)
7354                    (if (and (eq gnus-summary-goto-unread 'never)
7355                             (not (gnus-summary-last-article-p article)))
7356                        (gnus-summary-next-article)
7357                      (gnus-summary-next-unread-article))))))))
7358     (gnus-summary-recenter)
7359     (gnus-summary-position-point)))
7360
7361 (defun gnus-summary-prev-page (&optional lines move)
7362   "Show previous page of selected article.
7363 Argument LINES specifies lines to be scrolled down.
7364 If MOVE, move to the previous unread article if point is at
7365 the beginning of the buffer."
7366   (interactive "P")
7367   (let ((article (gnus-summary-article-number))
7368         (article-window (get-buffer-window gnus-article-buffer t))
7369         endp)
7370     (gnus-configure-windows 'article)
7371     (if (or (null gnus-current-article)
7372             (null gnus-article-current)
7373             (/= article (cdr gnus-article-current))
7374             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7375         ;; Selected subject is different from current article's.
7376         (gnus-summary-display-article article)
7377       (gnus-summary-recenter)
7378       (when article-window
7379         (gnus-eval-in-buffer-window gnus-article-buffer
7380           (setq endp (gnus-article-prev-page lines)))
7381         (when (and move endp)
7382           (cond (lines
7383                  (gnus-message 3 "Beginning of message"))
7384                 ((null lines)
7385                  (if (and (eq gnus-summary-goto-unread 'never)
7386                           (not (gnus-summary-first-article-p article)))
7387                      (gnus-summary-prev-article)
7388                    (gnus-summary-prev-unread-article))))))))
7389   (gnus-summary-position-point))
7390
7391 (defun gnus-summary-prev-page-or-article (&optional lines)
7392   "Show previous page of selected article.
7393 Argument LINES specifies lines to be scrolled down.
7394 If at the beginning of the article, go to the next article."
7395   (interactive "P")
7396   (gnus-summary-prev-page lines t))
7397
7398 (defun gnus-summary-scroll-up (lines)
7399   "Scroll up (or down) one line current article.
7400 Argument LINES specifies lines to be scrolled up (or down if negative)."
7401   (interactive "p")
7402   (gnus-configure-windows 'article)
7403   (gnus-summary-show-thread)
7404   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7405     (gnus-eval-in-buffer-window gnus-article-buffer
7406       (cond ((> lines 0)
7407              (when (gnus-article-next-page lines)
7408                (gnus-message 3 "End of message")))
7409             ((< lines 0)
7410              (gnus-article-prev-page (- lines))))))
7411   (gnus-summary-recenter)
7412   (gnus-summary-position-point))
7413
7414 (defun gnus-summary-scroll-down (lines)
7415   "Scroll down (or up) one line current article.
7416 Argument LINES specifies lines to be scrolled down (or up if negative)."
7417   (interactive "p")
7418   (gnus-summary-scroll-up (- lines)))
7419
7420 (defun gnus-summary-next-same-subject ()
7421   "Select next article which has the same subject as current one."
7422   (interactive)
7423   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7424
7425 (defun gnus-summary-prev-same-subject ()
7426   "Select previous article which has the same subject as current one."
7427   (interactive)
7428   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7429
7430 (defun gnus-summary-next-unread-same-subject ()
7431   "Select next unread article which has the same subject as current one."
7432   (interactive)
7433   (gnus-summary-next-article t (gnus-summary-article-subject)))
7434
7435 (defun gnus-summary-prev-unread-same-subject ()
7436   "Select previous unread article which has the same subject as current one."
7437   (interactive)
7438   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7439
7440 (defun gnus-summary-first-unread-article ()
7441   "Select the first unread article.
7442 Return nil if there are no unread articles."
7443   (interactive)
7444   (prog1
7445       (when (gnus-summary-first-subject t)
7446         (gnus-summary-show-thread)
7447         (gnus-summary-first-subject t)
7448         (gnus-summary-display-article (gnus-summary-article-number)))
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-first-unread-subject ()
7452   "Place the point on the subject line of the first unread article.
7453 Return nil if there are no unread articles."
7454   (interactive)
7455   (prog1
7456       (when (gnus-summary-first-subject t)
7457         (gnus-summary-show-thread)
7458         (gnus-summary-first-subject t))
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-first-unseen-subject ()
7462   "Place the point on the subject line of the first unseen article.
7463 Return nil if there are no unseen articles."
7464   (interactive)
7465   (prog1
7466       (when (gnus-summary-first-subject nil nil t)
7467         (gnus-summary-show-thread)
7468         (gnus-summary-first-subject nil nil t))
7469     (gnus-summary-position-point)))
7470
7471 (defun gnus-summary-first-unseen-or-unread-subject ()
7472   "Place the point on the subject line of the first unseen article or,
7473 if all article have been seen, on the subject line of the first unread
7474 article."
7475   (interactive)
7476   (prog1
7477       (unless (when (gnus-summary-first-subject nil nil t)
7478                 (gnus-summary-show-thread)
7479                 (gnus-summary-first-subject nil nil t))
7480         (when (gnus-summary-first-subject t)
7481           (gnus-summary-show-thread)
7482           (gnus-summary-first-subject t)))
7483     (gnus-summary-position-point)))
7484
7485 (defun gnus-summary-first-article ()
7486   "Select the first article.
7487 Return nil if there are no articles."
7488   (interactive)
7489   (prog1
7490       (when (gnus-summary-first-subject)
7491         (gnus-summary-show-thread)
7492         (gnus-summary-first-subject)
7493         (gnus-summary-display-article (gnus-summary-article-number)))
7494     (gnus-summary-position-point)))
7495
7496 (defun gnus-summary-best-unread-article (&optional arg)
7497   "Select the unread article with the highest score.
7498 If given a prefix argument, select the next unread article that has a
7499 score higher than the default score."
7500   (interactive "P")
7501   (let ((article (if arg
7502                      (gnus-summary-better-unread-subject)
7503                    (gnus-summary-best-unread-subject))))
7504     (if article
7505         (gnus-summary-goto-article article)
7506       (error "No unread articles"))))
7507
7508 (defun gnus-summary-best-unread-subject ()
7509   "Select the unread subject with the highest score."
7510   (interactive)
7511   (let ((best -1000000)
7512         (data gnus-newsgroup-data)
7513         article score)
7514     (while data
7515       (and (gnus-data-unread-p (car data))
7516            (> (setq score
7517                     (gnus-summary-article-score (gnus-data-number (car data))))
7518               best)
7519            (setq best score
7520                  article (gnus-data-number (car data))))
7521       (setq data (cdr data)))
7522     (when article
7523       (gnus-summary-goto-subject article))
7524     (gnus-summary-position-point)
7525     article))
7526
7527 (defun gnus-summary-better-unread-subject ()
7528   "Select the first unread subject that has a score over the default score."
7529   (interactive)
7530   (let ((data gnus-newsgroup-data)
7531         article score)
7532     (while (and (setq article (gnus-data-number (car data)))
7533                 (or (gnus-data-read-p (car data))
7534                     (not (> (gnus-summary-article-score article)
7535                             gnus-summary-default-score))))
7536       (setq data (cdr data)))
7537     (when article
7538       (gnus-summary-goto-subject article))
7539     (gnus-summary-position-point)
7540     article))
7541
7542 (defun gnus-summary-last-subject ()
7543   "Go to the last displayed subject line in the group."
7544   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7545     (when article
7546       (gnus-summary-goto-subject article))))
7547
7548 (defun gnus-summary-goto-article (article &optional all-headers force)
7549   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7550 If ALL-HEADERS is non-nil, no header lines are hidden.
7551 If FORCE, go to the article even if it isn't displayed.  If FORCE
7552 is a number, it is the line the article is to be displayed on."
7553   (interactive
7554    (list
7555     (completing-read
7556      "Article number or Message-ID: "
7557      (mapcar (lambda (number) (list (int-to-string number)))
7558              gnus-newsgroup-limit))
7559     current-prefix-arg
7560     t))
7561   (prog1
7562       (if (and (stringp article)
7563                (string-match "@\\|%40" article))
7564           (gnus-summary-refer-article article)
7565         (when (stringp article)
7566           (setq article (string-to-number article)))
7567         (if (gnus-summary-goto-subject article force)
7568             (gnus-summary-display-article article all-headers)
7569           (gnus-message 4 "Couldn't go to article %s" article) nil))
7570     (gnus-summary-position-point)))
7571
7572 (defun gnus-summary-goto-last-article ()
7573   "Go to the previously read article."
7574   (interactive)
7575   (prog1
7576       (when gnus-last-article
7577         (gnus-summary-goto-article gnus-last-article nil t))
7578     (gnus-summary-position-point)))
7579
7580 (defun gnus-summary-pop-article (number)
7581   "Pop one article off the history and go to the previous.
7582 NUMBER articles will be popped off."
7583   (interactive "p")
7584   (let (to)
7585     (setq gnus-newsgroup-history
7586           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7587     (if to
7588         (gnus-summary-goto-article (car to) nil t)
7589       (error "Article history empty")))
7590   (gnus-summary-position-point))
7591
7592 ;; Summary commands and functions for limiting the summary buffer.
7593
7594 (defun gnus-summary-limit-to-articles (n)
7595   "Limit the summary buffer to the next N articles.
7596 If not given a prefix, use the process marked articles instead."
7597   (interactive "P")
7598   (prog1
7599       (let ((articles (gnus-summary-work-articles n)))
7600         (setq gnus-newsgroup-processable nil)
7601         (gnus-summary-limit articles))
7602     (gnus-summary-position-point)))
7603
7604 (defun gnus-summary-pop-limit (&optional total)
7605   "Restore the previous limit.
7606 If given a prefix, remove all limits."
7607   (interactive "P")
7608   (when total
7609     (setq gnus-newsgroup-limits
7610           (list (mapcar (lambda (h) (mail-header-number h))
7611                         gnus-newsgroup-headers))))
7612   (unless gnus-newsgroup-limits
7613     (error "No limit to pop"))
7614   (prog1
7615       (gnus-summary-limit nil 'pop)
7616     (gnus-summary-position-point)))
7617
7618 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7619   "Limit the summary buffer to articles that have subjects that match a regexp.
7620 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7621   (interactive
7622    (list (read-string (if current-prefix-arg
7623                           "Exclude subject (regexp): "
7624                         "Limit to subject (regexp): "))
7625          nil current-prefix-arg))
7626   (unless header
7627     (setq header "subject"))
7628   (when (not (equal "" subject))
7629     (prog1
7630         (let ((articles (gnus-summary-find-matching
7631                          (or header "subject") subject 'all nil nil
7632                          not-matching)))
7633           (unless articles
7634             (error "Found no matches for \"%s\"" subject))
7635           (gnus-summary-limit articles))
7636       (gnus-summary-position-point))))
7637
7638 (defun gnus-summary-limit-to-author (from &optional not-matching)
7639   "Limit the summary buffer to articles that have authors that match a regexp.
7640 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7641   (interactive
7642    (list (read-string (if current-prefix-arg
7643                           "Exclude author (regexp): "
7644                         "Limit to author (regexp): "))
7645          current-prefix-arg))
7646   (gnus-summary-limit-to-subject from "from" not-matching))
7647
7648 (defun gnus-summary-limit-to-age (age &optional younger-p)
7649   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7650 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7651 articles that are younger than AGE days."
7652   (interactive
7653    (let ((younger current-prefix-arg)
7654          (days-got nil)
7655          days)
7656      (while (not days-got)
7657        (setq days (if younger
7658                       (read-string "Limit to articles younger than (in days, older when negative): ")
7659                     (read-string
7660                      "Limit to articles older than (in days, younger when negative): ")))
7661        (when (> (length days) 0)
7662          (setq days (read days)))
7663        (if (numberp days)
7664            (progn
7665              (setq days-got t)
7666              (when (< days 0)
7667                (setq younger (not younger))
7668                (setq days (* days -1))))
7669          (message "Please enter a number.")
7670          (sleep-for 1)))
7671      (list days younger)))
7672   (prog1
7673       (let ((data gnus-newsgroup-data)
7674             (cutoff (days-to-time age))
7675             articles d date is-younger)
7676         (while (setq d (pop data))
7677           (when (and (vectorp (gnus-data-header d))
7678                      (setq date (mail-header-date (gnus-data-header d))))
7679             (setq is-younger (time-less-p
7680                               (time-since (condition-case ()
7681                                               (date-to-time date)
7682                                             (error '(0 0))))
7683                               cutoff))
7684             (when (if younger-p
7685                       is-younger
7686                     (not is-younger))
7687               (push (gnus-data-number d) articles))))
7688         (gnus-summary-limit (nreverse articles)))
7689     (gnus-summary-position-point)))
7690
7691 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7692   "Limit the summary buffer to articles that match an 'extra' header."
7693   (interactive
7694    (let ((header
7695           (intern
7696            (gnus-completing-read-with-default
7697             (symbol-name (car gnus-extra-headers))
7698             (if current-prefix-arg
7699                 "Exclude extra header:"
7700               "Limit extra header:")
7701             (mapcar (lambda (x)
7702                       (cons (symbol-name x) x))
7703                     gnus-extra-headers)
7704             nil
7705             t))))
7706      (list header
7707            (read-string (format "%s header %s (regexp): "
7708                                 (if current-prefix-arg "Exclude" "Limit to")
7709                                 header))
7710            current-prefix-arg)))
7711   (when (not (equal "" regexp))
7712     (prog1
7713         (let ((articles (gnus-summary-find-matching
7714                          (cons 'extra header) regexp 'all nil nil
7715                          not-matching)))
7716           (unless articles
7717             (error "Found no matches for \"%s\"" regexp))
7718           (gnus-summary-limit articles))
7719       (gnus-summary-position-point))))
7720
7721 (defun gnus-summary-limit-to-display-predicate ()
7722   "Limit the summary buffer to the predicated in the `display' group parameter."
7723   (interactive)
7724   (unless gnus-newsgroup-display
7725     (error "There is no `display' group parameter"))
7726   (let (articles)
7727     (dolist (number gnus-newsgroup-articles)
7728       (when (funcall gnus-newsgroup-display)
7729         (push number articles)))
7730     (gnus-summary-limit articles))
7731   (gnus-summary-position-point))
7732
7733 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7734 (make-obsolete
7735  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7736
7737 (defun gnus-summary-limit-to-unread (&optional all)
7738   "Limit the summary buffer to articles that are not marked as read.
7739 If ALL is non-nil, limit strictly to unread articles."
7740   (interactive "P")
7741   (if all
7742       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7743     (gnus-summary-limit-to-marks
7744      ;; Concat all the marks that say that an article is read and have
7745      ;; those removed.
7746      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7747            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7748            gnus-low-score-mark gnus-expirable-mark
7749            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7750            gnus-duplicate-mark gnus-souped-mark)
7751      'reverse)))
7752
7753 (defun gnus-summary-limit-to-replied (&optional unreplied)
7754   "Limit the summary buffer to replied articles.
7755 If UNREPLIED (the prefix), limit to unreplied articles."
7756   (interactive "P")
7757   (if unreplied
7758       (gnus-summary-limit
7759        (gnus-set-difference gnus-newsgroup-articles
7760         gnus-newsgroup-replied))
7761     (gnus-summary-limit gnus-newsgroup-replied))
7762   (gnus-summary-position-point))
7763
7764 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7765 (make-obsolete 'gnus-summary-delete-marked-with
7766                'gnus-summary-limit-exclude-marks)
7767
7768 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7769   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7770 If REVERSE, limit the summary buffer to articles that are marked
7771 with MARKS.  MARKS can either be a string of marks or a list of marks.
7772 Returns how many articles were removed."
7773   (interactive "sMarks: ")
7774   (gnus-summary-limit-to-marks marks t))
7775
7776 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7777   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7778 If REVERSE (the prefix), limit the summary buffer to articles that are
7779 not marked with MARKS.  MARKS can either be a string of marks or a
7780 list of marks.
7781 Returns how many articles were removed."
7782   (interactive "sMarks: \nP")
7783   (prog1
7784       (let ((data gnus-newsgroup-data)
7785             (marks (if (listp marks) marks
7786                      (append marks nil))) ; Transform to list.
7787             articles)
7788         (while data
7789           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7790                   (memq (gnus-data-mark (car data)) marks))
7791             (push (gnus-data-number (car data)) articles))
7792           (setq data (cdr data)))
7793         (gnus-summary-limit articles))
7794     (gnus-summary-position-point)))
7795
7796 (defun gnus-summary-limit-to-score (score)
7797   "Limit to articles with score at or above SCORE."
7798   (interactive "NLimit to articles with score of at least: ")
7799   (let ((data gnus-newsgroup-data)
7800         articles)
7801     (while data
7802       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7803                 score)
7804         (push (gnus-data-number (car data)) articles))
7805       (setq data (cdr data)))
7806     (prog1
7807         (gnus-summary-limit articles)
7808       (gnus-summary-position-point))))
7809
7810 (defun gnus-summary-limit-to-unseen ()
7811   "Limit to unseen articles."
7812   (interactive)
7813   (prog1
7814       (gnus-summary-limit gnus-newsgroup-unseen)
7815     (gnus-summary-position-point)))
7816
7817 (defun gnus-summary-limit-include-thread (id)
7818   "Display all the hidden articles that is in the thread with ID in it.
7819 When called interactively, ID is the Message-ID of the current
7820 article."
7821   (interactive (list (mail-header-id (gnus-summary-article-header))))
7822   (let ((articles (gnus-articles-in-thread
7823                    (gnus-id-to-thread (gnus-root-id id)))))
7824     (prog1
7825         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7826       (gnus-summary-limit-include-matching-articles
7827        "subject"
7828        (regexp-quote (gnus-simplify-subject-re
7829                       (mail-header-subject (gnus-id-to-header id)))))
7830       (gnus-summary-position-point))))
7831
7832 (defun gnus-summary-limit-include-matching-articles (header regexp)
7833   "Display all the hidden articles that have HEADERs that match REGEXP."
7834   (interactive (list (read-string "Match on header: ")
7835                      (read-string "Regexp: ")))
7836   (let ((articles (gnus-find-matching-articles header regexp)))
7837     (prog1
7838         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7839       (gnus-summary-position-point))))
7840
7841 (defun gnus-summary-insert-dormant-articles ()
7842   "Insert all the dormant articles for this group into the current buffer."
7843   (interactive)
7844   (let ((gnus-verbose (max 6 gnus-verbose)))
7845     (if (not gnus-newsgroup-dormant)
7846         (gnus-message 3 "No cached articles for this group")
7847       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7848
7849 (defun gnus-summary-limit-include-dormant ()
7850   "Display all the hidden articles that are marked as dormant.
7851 Note that this command only works on a subset of the articles currently
7852 fetched for this group."
7853   (interactive)
7854   (unless gnus-newsgroup-dormant
7855     (error "There are no dormant articles in this group"))
7856   (prog1
7857       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7858     (gnus-summary-position-point)))
7859
7860 (defun gnus-summary-limit-exclude-dormant ()
7861   "Hide all dormant articles."
7862   (interactive)
7863   (prog1
7864       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7865     (gnus-summary-position-point)))
7866
7867 (defun gnus-summary-limit-exclude-childless-dormant ()
7868   "Hide all dormant articles that have no children."
7869   (interactive)
7870   (let ((data (gnus-data-list t))
7871         articles d children)
7872     ;; Find all articles that are either not dormant or have
7873     ;; children.
7874     (while (setq d (pop data))
7875       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7876                 (and (setq children
7877                            (gnus-article-children (gnus-data-number d)))
7878                      (let (found)
7879                        (while children
7880                          (when (memq (car children) articles)
7881                            (setq children nil
7882                                  found t))
7883                          (pop children))
7884                        found)))
7885         (push (gnus-data-number d) articles)))
7886     ;; Do the limiting.
7887     (prog1
7888         (gnus-summary-limit articles)
7889       (gnus-summary-position-point))))
7890
7891 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7892   "Mark all unread excluded articles as read.
7893 If ALL, mark even excluded ticked and dormants as read."
7894   (interactive "P")
7895   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7896   (let ((articles (gnus-sorted-ndifference
7897                    (sort
7898                     (mapcar (lambda (h) (mail-header-number h))
7899                             gnus-newsgroup-headers)
7900                     '<)
7901                    gnus-newsgroup-limit))
7902         article)
7903     (setq gnus-newsgroup-unreads
7904           (gnus-sorted-intersection gnus-newsgroup-unreads
7905                                     gnus-newsgroup-limit))
7906     (if all
7907         (setq gnus-newsgroup-dormant nil
7908               gnus-newsgroup-marked nil
7909               gnus-newsgroup-reads
7910               (nconc
7911                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7912                gnus-newsgroup-reads))
7913       (while (setq article (pop articles))
7914         (unless (or (memq article gnus-newsgroup-dormant)
7915                     (memq article gnus-newsgroup-marked))
7916           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7917
7918 (defun gnus-summary-limit (articles &optional pop)
7919   (if pop
7920       ;; We pop the previous limit off the stack and use that.
7921       (setq articles (car gnus-newsgroup-limits)
7922             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7923     ;; We use the new limit, so we push the old limit on the stack.
7924     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7925   ;; Set the limit.
7926   (setq gnus-newsgroup-limit articles)
7927   (let ((total (length gnus-newsgroup-data))
7928         (data (gnus-data-find-list (gnus-summary-article-number)))
7929         (gnus-summary-mark-below nil)   ; Inhibit this.
7930         found)
7931     ;; This will do all the work of generating the new summary buffer
7932     ;; according to the new limit.
7933     (gnus-summary-prepare)
7934     ;; Hide any threads, possibly.
7935     (gnus-summary-maybe-hide-threads)
7936     ;; Try to return to the article you were at, or one in the
7937     ;; neighborhood.
7938     (when data
7939       ;; We try to find some article after the current one.
7940       (while data
7941         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7942           (setq data nil
7943                 found t))
7944         (setq data (cdr data))))
7945     (unless found
7946       ;; If there is no data, that means that we were after the last
7947       ;; article.  The same goes when we can't find any articles
7948       ;; after the current one.
7949       (goto-char (point-max))
7950       (gnus-summary-find-prev))
7951     (gnus-set-mode-line 'summary)
7952     ;; We return how many articles were removed from the summary
7953     ;; buffer as a result of the new limit.
7954     (- total (length gnus-newsgroup-data))))
7955
7956 (defsubst gnus-invisible-cut-children (threads)
7957   (let ((num 0))
7958     (while threads
7959       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7960         (incf num))
7961       (pop threads))
7962     (< num 2)))
7963
7964 (defsubst gnus-cut-thread (thread)
7965   "Go forwards in the thread until we find an article that we want to display."
7966   (when (or (eq gnus-fetch-old-headers 'some)
7967             (eq gnus-fetch-old-headers 'invisible)
7968             (numberp gnus-fetch-old-headers)
7969             (eq gnus-build-sparse-threads 'some)
7970             (eq gnus-build-sparse-threads 'more))
7971     ;; Deal with old-fetched headers and sparse threads.
7972     (while (and
7973             thread
7974             (or
7975              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7976              (gnus-summary-article-ancient-p
7977               (mail-header-number (car thread))))
7978             (if (or (<= (length (cdr thread)) 1)
7979                     (eq gnus-fetch-old-headers 'invisible))
7980                 (setq gnus-newsgroup-limit
7981                       (delq (mail-header-number (car thread))
7982                             gnus-newsgroup-limit)
7983                       thread (cadr thread))
7984               (when (gnus-invisible-cut-children (cdr thread))
7985                 (let ((th (cdr thread)))
7986                   (while th
7987                     (if (memq (mail-header-number (caar th))
7988                               gnus-newsgroup-limit)
7989                         (setq thread (car th)
7990                               th nil)
7991                       (setq th (cdr th))))))))))
7992   thread)
7993
7994 (defun gnus-cut-threads (threads)
7995   "Cut off all uninteresting articles from the beginning of THREADS."
7996   (when (or (eq gnus-fetch-old-headers 'some)
7997             (eq gnus-fetch-old-headers 'invisible)
7998             (numberp gnus-fetch-old-headers)
7999             (eq gnus-build-sparse-threads 'some)
8000             (eq gnus-build-sparse-threads 'more))
8001     (let ((th threads))
8002       (while th
8003         (setcar th (gnus-cut-thread (car th)))
8004         (setq th (cdr th)))))
8005   ;; Remove nixed out threads.
8006   (delq nil threads))
8007
8008 (defun gnus-summary-initial-limit (&optional show-if-empty)
8009   "Figure out what the initial limit is supposed to be on group entry.
8010 This entails weeding out unwanted dormants, low-scored articles,
8011 fetch-old-headers verbiage, and so on."
8012   ;; Most groups have nothing to remove.
8013   (if (or gnus-inhibit-limiting
8014           (and (null gnus-newsgroup-dormant)
8015                (eq gnus-newsgroup-display 'gnus-not-ignore)
8016                (not (eq gnus-fetch-old-headers 'some))
8017                (not (numberp gnus-fetch-old-headers))
8018                (not (eq gnus-fetch-old-headers 'invisible))
8019                (null gnus-summary-expunge-below)
8020                (not (eq gnus-build-sparse-threads 'some))
8021                (not (eq gnus-build-sparse-threads 'more))
8022                (null gnus-thread-expunge-below)
8023                (not gnus-use-nocem)))
8024       ()                                ; Do nothing.
8025     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8026     (setq gnus-newsgroup-limit nil)
8027     (mapatoms
8028      (lambda (node)
8029        (unless (car (symbol-value node))
8030          ;; These threads have no parents -- they are roots.
8031          (let ((nodes (cdr (symbol-value node)))
8032                thread)
8033            (while nodes
8034              (if (and gnus-thread-expunge-below
8035                       (< (gnus-thread-total-score (car nodes))
8036                          gnus-thread-expunge-below))
8037                  (gnus-expunge-thread (pop nodes))
8038                (setq thread (pop nodes))
8039                (gnus-summary-limit-children thread))))))
8040      gnus-newsgroup-dependencies)
8041     ;; If this limitation resulted in an empty group, we might
8042     ;; pop the previous limit and use it instead.
8043     (when (and (not gnus-newsgroup-limit)
8044                show-if-empty)
8045       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8046     gnus-newsgroup-limit))
8047
8048 (defun gnus-summary-limit-children (thread)
8049   "Return 1 if this subthread is visible and 0 if it is not."
8050   ;; First we get the number of visible children to this thread.  This
8051   ;; is done by recursing down the thread using this function, so this
8052   ;; will really go down to a leaf article first, before slowly
8053   ;; working its way up towards the root.
8054   (when thread
8055     (let* ((max-lisp-eval-depth 5000)
8056            (children
8057            (if (cdr thread)
8058                (apply '+ (mapcar 'gnus-summary-limit-children
8059                                  (cdr thread)))
8060              0))
8061           (number (mail-header-number (car thread)))
8062           score)
8063       (if (and
8064            (not (memq number gnus-newsgroup-marked))
8065            (or
8066             ;; If this article is dormant and has absolutely no visible
8067             ;; children, then this article isn't visible.
8068             (and (memq number gnus-newsgroup-dormant)
8069                  (zerop children))
8070             ;; If this is "fetch-old-headered" and there is no
8071             ;; visible children, then we don't want this article.
8072             (and (or (eq gnus-fetch-old-headers 'some)
8073                      (numberp gnus-fetch-old-headers))
8074                  (gnus-summary-article-ancient-p number)
8075                  (zerop children))
8076             ;; If this is "fetch-old-headered" and `invisible', then
8077             ;; we don't want this article.
8078             (and (eq gnus-fetch-old-headers 'invisible)
8079                  (gnus-summary-article-ancient-p number))
8080             ;; If this is a sparsely inserted article with no children,
8081             ;; we don't want it.
8082             (and (eq gnus-build-sparse-threads 'some)
8083                  (gnus-summary-article-sparse-p number)
8084                  (zerop children))
8085             ;; If we use expunging, and this article is really
8086             ;; low-scored, then we don't want this article.
8087             (when (and gnus-summary-expunge-below
8088                        (< (setq score
8089                                 (or (cdr (assq number gnus-newsgroup-scored))
8090                                     gnus-summary-default-score))
8091                           gnus-summary-expunge-below))
8092               ;; We increase the expunge-tally here, but that has
8093               ;; nothing to do with the limits, really.
8094               (incf gnus-newsgroup-expunged-tally)
8095               ;; We also mark as read here, if that's wanted.
8096               (when (and gnus-summary-mark-below
8097                          (< score gnus-summary-mark-below))
8098                 (setq gnus-newsgroup-unreads
8099                       (delq number gnus-newsgroup-unreads))
8100                 (if gnus-newsgroup-auto-expire
8101                     (push number gnus-newsgroup-expirable)
8102                   (push (cons number gnus-low-score-mark)
8103                         gnus-newsgroup-reads)))
8104               t)
8105             ;; Do the `display' group parameter.
8106             (and gnus-newsgroup-display
8107                  (not (funcall gnus-newsgroup-display)))
8108             ;; Check NoCeM things.
8109             (when (and gnus-use-nocem
8110                        (gnus-nocem-unwanted-article-p
8111                         (mail-header-id (car thread))))
8112               (setq gnus-newsgroup-unreads
8113                     (delq number gnus-newsgroup-unreads))
8114               t)))
8115           ;; Nope, invisible article.
8116           0
8117         ;; Ok, this article is to be visible, so we add it to the limit
8118         ;; and return 1.
8119         (push number gnus-newsgroup-limit)
8120         1))))
8121
8122 (defun gnus-expunge-thread (thread)
8123   "Mark all articles in THREAD as read."
8124   (let* ((number (mail-header-number (car thread))))
8125     (incf gnus-newsgroup-expunged-tally)
8126     ;; We also mark as read here, if that's wanted.
8127     (setq gnus-newsgroup-unreads
8128           (delq number gnus-newsgroup-unreads))
8129     (if gnus-newsgroup-auto-expire
8130         (push number gnus-newsgroup-expirable)
8131       (push (cons number gnus-low-score-mark)
8132             gnus-newsgroup-reads)))
8133   ;; Go recursively through all subthreads.
8134   (mapcar 'gnus-expunge-thread (cdr thread)))
8135
8136 ;; Summary article oriented commands
8137
8138 (defun gnus-summary-refer-parent-article (n)
8139   "Refer parent article N times.
8140 If N is negative, go to ancestor -N instead.
8141 The difference between N and the number of articles fetched is returned."
8142   (interactive "p")
8143   (let ((skip 1)
8144         error header ref)
8145     (when (not (natnump n))
8146       (setq skip (abs n)
8147             n 1))
8148     (while (and (> n 0)
8149                 (not error))
8150       (setq header (gnus-summary-article-header))
8151       (if (and (eq (mail-header-number header)
8152                    (cdr gnus-article-current))
8153                (equal gnus-newsgroup-name
8154                       (car gnus-article-current)))
8155           ;; If we try to find the parent of the currently
8156           ;; displayed article, then we take a look at the actual
8157           ;; References header, since this is slightly more
8158           ;; reliable than the References field we got from the
8159           ;; server.
8160           (save-excursion
8161             (set-buffer gnus-original-article-buffer)
8162             (nnheader-narrow-to-headers)
8163             (unless (setq ref (message-fetch-field "references"))
8164               (when (setq ref (message-fetch-field "in-reply-to"))
8165                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8166             (widen))
8167         (setq ref
8168               ;; It's not the current article, so we take a bet on
8169               ;; the value we got from the server.
8170               (mail-header-references header)))
8171       (if (and ref
8172                (not (equal ref "")))
8173           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8174             (gnus-message 1 "Couldn't find parent"))
8175         (gnus-message 1 "No references in article %d"
8176                       (gnus-summary-article-number))
8177         (setq error t))
8178       (decf n))
8179     (gnus-summary-position-point)
8180     n))
8181
8182 (defun gnus-summary-refer-references ()
8183   "Fetch all articles mentioned in the References header.
8184 Return the number of articles fetched."
8185   (interactive)
8186   (let ((ref (mail-header-references (gnus-summary-article-header)))
8187         (current (gnus-summary-article-number))
8188         (n 0))
8189     (if (or (not ref)
8190             (equal ref ""))
8191         (error "No References in the current article")
8192       ;; For each Message-ID in the References header...
8193       (while (string-match "<[^>]*>" ref)
8194         (incf n)
8195         ;; ... fetch that article.
8196         (gnus-summary-refer-article
8197          (prog1 (match-string 0 ref)
8198            (setq ref (substring ref (match-end 0))))))
8199       (gnus-summary-goto-subject current)
8200       (gnus-summary-position-point)
8201       n)))
8202
8203 (defun gnus-summary-refer-thread (&optional limit)
8204   "Fetch all articles in the current thread.
8205 If LIMIT (the numerical prefix), fetch that many old headers instead
8206 of what's specified by the `gnus-refer-thread-limit' variable."
8207   (interactive "P")
8208   (let ((id (mail-header-id (gnus-summary-article-header)))
8209         (limit (if limit (prefix-numeric-value limit)
8210                  gnus-refer-thread-limit)))
8211     (unless (eq gnus-fetch-old-headers 'invisible)
8212       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8213       ;; Retrieve the headers and read them in.
8214       (if (eq (if (numberp limit)
8215                   (gnus-retrieve-headers
8216                    (list (min
8217                           (+ (mail-header-number
8218                               (gnus-summary-article-header))
8219                              limit)
8220                           gnus-newsgroup-end))
8221                    gnus-newsgroup-name (* limit 2))
8222                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8223                 ;; headers.
8224                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8225                                        gnus-newsgroup-name limit))
8226               'nov)
8227           (gnus-build-all-threads)
8228         (error "Can't fetch thread from back ends that don't support NOV"))
8229       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8230     (gnus-summary-limit-include-thread id)))
8231
8232 (defun gnus-summary-refer-article (message-id)
8233   "Fetch an article specified by MESSAGE-ID."
8234   (interactive "sMessage-ID: ")
8235   (when (and (stringp message-id)
8236              (not (zerop (length message-id))))
8237     (setq message-id (gnus-replace-in-string message-id " " ""))
8238     ;; Construct the correct Message-ID if necessary.
8239     ;; Suggested by tale@pawl.rpi.edu.
8240     (unless (string-match "^<" message-id)
8241       (setq message-id (concat "<" message-id)))
8242     (unless (string-match ">$" message-id)
8243       (setq message-id (concat message-id ">")))
8244     ;; People often post MIDs from URLs, so unhex it:
8245     (unless (string-match "@" message-id)
8246       (setq message-id (gnus-url-unhex-string message-id)))
8247     (let* ((header (gnus-id-to-header message-id))
8248            (sparse (and header
8249                         (gnus-summary-article-sparse-p
8250                          (mail-header-number header))
8251                         (memq (mail-header-number header)
8252                               gnus-newsgroup-limit)))
8253            number)
8254       (cond
8255        ;; If the article is present in the buffer we just go to it.
8256        ((and header
8257              (or (not (gnus-summary-article-sparse-p
8258                        (mail-header-number header)))
8259                  sparse))
8260         (prog1
8261             (gnus-summary-goto-article
8262              (mail-header-number header) nil t)
8263           (when sparse
8264             (gnus-summary-update-article (mail-header-number header)))))
8265        (t
8266         ;; We fetch the article.
8267         (catch 'found
8268           (dolist (gnus-override-method (gnus-refer-article-methods))
8269             (when (and (gnus-check-server gnus-override-method)
8270                        ;; Fetch the header,
8271                        (setq number (gnus-summary-insert-subject message-id)))
8272               ;; and display the article.
8273               (gnus-summary-select-article nil nil nil number)
8274               (throw 'found t)))
8275           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8276
8277 (defun gnus-refer-article-methods ()
8278   "Return a list of referable methods."
8279   (cond
8280    ;; No method, so we default to current and native.
8281    ((null gnus-refer-article-method)
8282     (list gnus-current-select-method gnus-select-method))
8283    ;; Current.
8284    ((eq 'current gnus-refer-article-method)
8285     (list gnus-current-select-method))
8286    ;; List of select methods.
8287    ((not (and (symbolp (car gnus-refer-article-method))
8288               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8289     (let (out)
8290       (dolist (method gnus-refer-article-method)
8291         (push (if (eq 'current method)
8292                   gnus-current-select-method
8293                 method)
8294               out))
8295       (nreverse out)))
8296    ;; One single select method.
8297    (t
8298     (list gnus-refer-article-method))))
8299
8300 (defun gnus-summary-edit-parameters ()
8301   "Edit the group parameters of the current group."
8302   (interactive)
8303   (gnus-group-edit-group gnus-newsgroup-name 'params))
8304
8305 (defun gnus-summary-customize-parameters ()
8306   "Customize the group parameters of the current group."
8307   (interactive)
8308   (gnus-group-customize gnus-newsgroup-name))
8309
8310 (defun gnus-summary-enter-digest-group (&optional force)
8311   "Enter an nndoc group based on the current article.
8312 If FORCE, force a digest interpretation.  If not, try
8313 to guess what the document format is."
8314   (interactive "P")
8315   (let ((conf gnus-current-window-configuration))
8316     (save-window-excursion
8317       (save-excursion
8318         (let (gnus-article-prepare-hook
8319               gnus-display-mime-function
8320               gnus-break-pages)
8321           (gnus-summary-select-article))))
8322     (setq gnus-current-window-configuration conf)
8323     (let* ((name (format "%s-%d"
8324                          (gnus-group-prefixed-name
8325                           gnus-newsgroup-name (list 'nndoc ""))
8326                          (with-current-buffer gnus-summary-buffer
8327                            gnus-current-article)))
8328            (ogroup gnus-newsgroup-name)
8329            (params (append (gnus-info-params (gnus-get-info ogroup))
8330                            (list (cons 'to-group ogroup))
8331                            (list (cons 'parent-group ogroup))
8332                            (list (cons 'save-article-group ogroup))))
8333            (case-fold-search t)
8334            (buf (current-buffer))
8335            dig to-address)
8336       (save-excursion
8337         (set-buffer gnus-original-article-buffer)
8338         ;; Have the digest group inherit the main mail address of
8339         ;; the parent article.
8340         (when (setq to-address (or (gnus-fetch-field "reply-to")
8341                                    (gnus-fetch-field "from")))
8342           (setq params (append
8343                         (list (cons 'to-address
8344                                     (funcall gnus-decode-encoded-word-function
8345                                              to-address))))))
8346         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8347         (insert-buffer-substring gnus-original-article-buffer)
8348         ;; Remove lines that may lead nndoc to misinterpret the
8349         ;; document type.
8350         (narrow-to-region
8351          (goto-char (point-min))
8352          (or (search-forward "\n\n" nil t) (point)))
8353         (goto-char (point-min))
8354         (delete-matching-lines "^Path:\\|^From ")
8355         (widen))
8356       (unwind-protect
8357           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8358                     (gnus-newsgroup-ephemeral-ignored-charsets
8359                      gnus-newsgroup-ignored-charsets))
8360                 (gnus-group-read-ephemeral-group
8361                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8362                               (nndoc-article-type
8363                                ,(if force 'mbox 'guess)))
8364                  t nil nil nil
8365                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8366                                                         "ADAPT")))))
8367               ;; Make all postings to this group go to the parent group.
8368               (nconc (gnus-info-params (gnus-get-info name))
8369                      params)
8370             ;; Couldn't select this doc group.
8371             (switch-to-buffer buf)
8372             (gnus-set-global-variables)
8373             (gnus-configure-windows 'summary)
8374             (gnus-message 3 "Article couldn't be entered?"))
8375         (kill-buffer dig)))))
8376
8377 (defun gnus-summary-read-document (n)
8378   "Open a new group based on the current article(s).
8379 This will allow you to read digests and other similar
8380 documents as newsgroups.
8381 Obeys the standard process/prefix convention."
8382   (interactive "P")
8383   (let* ((ogroup gnus-newsgroup-name)
8384          (params (append (gnus-info-params (gnus-get-info ogroup))
8385                          (list (cons 'to-group ogroup))))
8386          group egroup groups vgroup)
8387     (dolist (article (gnus-summary-work-articles n))
8388       (setq group (format "%s-%d" gnus-newsgroup-name article))
8389       (gnus-summary-remove-process-mark article)
8390       (when (gnus-summary-display-article article)
8391         (save-excursion
8392           (with-temp-buffer
8393             (insert-buffer-substring gnus-original-article-buffer)
8394             ;; Remove some headers that may lead nndoc to make
8395             ;; the wrong guess.
8396             (message-narrow-to-head)
8397             (goto-char (point-min))
8398             (delete-matching-lines "^Path:\\|^From ")
8399             (widen)
8400             (if (setq egroup
8401                       (gnus-group-read-ephemeral-group
8402                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8403                                      (nndoc-article-type guess))
8404                        t nil t))
8405                 (progn
8406             ;; Make all postings to this group go to the parent group.
8407                   (nconc (gnus-info-params (gnus-get-info egroup))
8408                          params)
8409                   (push egroup groups))
8410               ;; Couldn't select this doc group.
8411               (gnus-error 3 "Article couldn't be entered"))))))
8412     ;; Now we have selected all the documents.
8413     (cond
8414      ((not groups)
8415       (error "None of the articles could be interpreted as documents"))
8416      ((gnus-group-read-ephemeral-group
8417        (setq vgroup (format
8418                      "nnvirtual:%s-%s" gnus-newsgroup-name
8419                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8420        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8421        t
8422        (cons (current-buffer) 'summary)))
8423      (t
8424       (error "Couldn't select virtual nndoc group")))))
8425
8426 (defun gnus-summary-isearch-article (&optional regexp-p)
8427   "Do incremental search forward on the current article.
8428 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8429   (interactive "P")
8430   (gnus-summary-select-article)
8431   (gnus-configure-windows 'article)
8432   (gnus-eval-in-buffer-window gnus-article-buffer
8433     (save-restriction
8434       (widen)
8435       (isearch-forward regexp-p))))
8436
8437 (defun gnus-summary-search-article-forward (regexp &optional backward)
8438   "Search for an article containing REGEXP forward.
8439 If BACKWARD, search backward instead."
8440   (interactive
8441    (list (read-string
8442           (format "Search article %s (regexp%s): "
8443                   (if current-prefix-arg "backward" "forward")
8444                   (if gnus-last-search-regexp
8445                       (concat ", default " gnus-last-search-regexp)
8446                     "")))
8447          current-prefix-arg))
8448   (if (string-equal regexp "")
8449       (setq regexp (or gnus-last-search-regexp ""))
8450     (setq gnus-last-search-regexp regexp)
8451     (setq gnus-article-before-search gnus-current-article))
8452   ;; Intentionally set gnus-last-article.
8453   (setq gnus-last-article gnus-article-before-search)
8454   (let ((gnus-last-article gnus-last-article))
8455     (if (gnus-summary-search-article regexp backward)
8456         (gnus-summary-show-thread)
8457       (signal 'search-failed (list regexp)))))
8458
8459 (defun gnus-summary-search-article-backward (regexp)
8460   "Search for an article containing REGEXP backward."
8461   (interactive
8462    (list (read-string
8463           (format "Search article backward (regexp%s): "
8464                   (if gnus-last-search-regexp
8465                       (concat ", default " gnus-last-search-regexp)
8466                     "")))))
8467   (gnus-summary-search-article-forward regexp 'backward))
8468
8469 (defun gnus-summary-search-article (regexp &optional backward)
8470   "Search for an article containing REGEXP.
8471 Optional argument BACKWARD means do search for backward.
8472 `gnus-select-article-hook' is not called during the search."
8473   ;; We have to require this here to make sure that the following
8474   ;; dynamic binding isn't shadowed by autoloading.
8475   (require 'gnus-async)
8476   (require 'gnus-art)
8477   (let ((gnus-select-article-hook nil)  ;Disable hook.
8478         (gnus-article-prepare-hook nil)
8479         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8480         (gnus-use-article-prefetch nil)
8481         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8482         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8483         (gnus-visual nil)
8484         (gnus-keep-backlog nil)
8485         (gnus-break-pages nil)
8486         (gnus-summary-display-arrow nil)
8487         (gnus-updated-mode-lines nil)
8488         (gnus-auto-center-summary nil)
8489         (sum (current-buffer))
8490         (gnus-display-mime-function nil)
8491         (found nil)
8492         point)
8493     (gnus-save-hidden-threads
8494       (gnus-summary-select-article)
8495       (set-buffer gnus-article-buffer)
8496       (goto-char (window-point (get-buffer-window (current-buffer))))
8497       (when backward
8498         (forward-line -1))
8499       (while (not found)
8500         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8501         (if (if backward
8502                 (re-search-backward regexp nil t)
8503               (re-search-forward regexp nil t))
8504             ;; We found the regexp.
8505             (progn
8506               (setq found 'found)
8507               (beginning-of-line)
8508               (set-window-start
8509                (get-buffer-window (current-buffer))
8510                (point))
8511               (forward-line 1)
8512               (set-window-point
8513                (get-buffer-window (current-buffer))
8514                (point))
8515               (set-buffer sum)
8516               (setq point (point)))
8517           ;; We didn't find it, so we go to the next article.
8518           (set-buffer sum)
8519           (setq found 'not)
8520           (while (eq found 'not)
8521             (if (not (if backward (gnus-summary-find-prev)
8522                        (gnus-summary-find-next)))
8523                 ;; No more articles.
8524                 (setq found t)
8525               ;; Select the next article and adjust point.
8526               (unless (gnus-summary-article-sparse-p
8527                        (gnus-summary-article-number))
8528                 (setq found nil)
8529                 (gnus-summary-select-article)
8530                 (set-buffer gnus-article-buffer)
8531                 (widen)
8532                 (goto-char (if backward (point-max) (point-min))))))))
8533       (gnus-message 7 ""))
8534     ;; Return whether we found the regexp.
8535     (when (eq found 'found)
8536       (goto-char point)
8537       (gnus-summary-show-thread)
8538       (gnus-summary-goto-subject gnus-current-article)
8539       (gnus-summary-position-point)
8540       t)))
8541
8542 (defun gnus-find-matching-articles (header regexp)
8543   "Return a list of all articles that match REGEXP on HEADER.
8544 This search includes all articles in the current group that Gnus has
8545 fetched headers for, whether they are displayed or not."
8546   (let ((articles nil)
8547         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8548         (case-fold-search t))
8549     (dolist (header gnus-newsgroup-headers)
8550       (when (string-match regexp (funcall func header))
8551         (push (mail-header-number header) articles)))
8552     (nreverse articles)))
8553
8554 (defun gnus-summary-find-matching (header regexp &optional backward unread
8555                                           not-case-fold not-matching)
8556   "Return a list of all articles that match REGEXP on HEADER.
8557 The search stars on the current article and goes forwards unless
8558 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8559 If UNREAD is non-nil, only unread articles will
8560 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8561 in the comparisons. If NOT-MATCHING, return a list of all articles that
8562 not match REGEXP on HEADER."
8563   (let ((case-fold-search (not not-case-fold))
8564         articles d func)
8565     (if (consp header)
8566         (if (eq (car header) 'extra)
8567             (setq func
8568                   `(lambda (h)
8569                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8570                          "")))
8571           (error "%s is an invalid header" header))
8572       (unless (fboundp (intern (concat "mail-header-" header)))
8573         (error "%s is not a valid header" header))
8574       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8575     (dolist (d (if (eq backward 'all)
8576                    gnus-newsgroup-data
8577                  (gnus-data-find-list
8578                   (gnus-summary-article-number)
8579                   (gnus-data-list backward))))
8580       (when (and (or (not unread)       ; We want all articles...
8581                      (gnus-data-unread-p d)) ; Or just unreads.
8582                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8583                  (if not-matching
8584                      (not (string-match
8585                            regexp
8586                            (funcall func (gnus-data-header d))))
8587                    (string-match regexp
8588                                  (funcall func (gnus-data-header d)))))
8589         (push (gnus-data-number d) articles))) ; Success!
8590     (nreverse articles)))
8591
8592 (defun gnus-summary-execute-command (header regexp command &optional backward)
8593   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8594 If HEADER is an empty string (or nil), the match is done on the entire
8595 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8596   (interactive
8597    (list (let ((completion-ignore-case t))
8598            (completing-read
8599             "Header name: "
8600             (mapcar (lambda (header) (list (format "%s" header)))
8601                     (append
8602                      '("Number" "Subject" "From" "Lines" "Date"
8603                        "Message-ID" "Xref" "References" "Body")
8604                      gnus-extra-headers))
8605             nil 'require-match))
8606          (read-string "Regexp: ")
8607          (read-key-sequence "Command: ")
8608          current-prefix-arg))
8609   (when (equal header "Body")
8610     (setq header ""))
8611   ;; Hidden thread subtrees must be searched as well.
8612   (gnus-summary-show-all-threads)
8613   ;; We don't want to change current point nor window configuration.
8614   (save-excursion
8615     (save-window-excursion
8616       (let (gnus-visual
8617             gnus-treat-strip-trailing-blank-lines
8618             gnus-treat-strip-leading-blank-lines
8619             gnus-treat-strip-multiple-blank-lines
8620             gnus-treat-hide-boring-headers
8621             gnus-treat-fold-newsgroups
8622             gnus-article-prepare-hook)
8623         (gnus-message 6 "Executing %s..." (key-description command))
8624         ;; We'd like to execute COMMAND interactively so as to give arguments.
8625         (gnus-execute header regexp
8626                       `(call-interactively ',(key-binding command))
8627                       backward)
8628         (gnus-message 6 "Executing %s...done" (key-description command))))))
8629
8630 (defun gnus-summary-beginning-of-article ()
8631   "Scroll the article back to the beginning."
8632   (interactive)
8633   (gnus-summary-select-article)
8634   (gnus-configure-windows 'article)
8635   (gnus-eval-in-buffer-window gnus-article-buffer
8636     (widen)
8637     (goto-char (point-min))
8638     (when gnus-break-pages
8639       (gnus-narrow-to-page))))
8640
8641 (defun gnus-summary-end-of-article ()
8642   "Scroll to the end of the article."
8643   (interactive)
8644   (gnus-summary-select-article)
8645   (gnus-configure-windows 'article)
8646   (gnus-eval-in-buffer-window gnus-article-buffer
8647     (widen)
8648     (goto-char (point-max))
8649     (recenter -3)
8650     (when gnus-break-pages
8651       (when (re-search-backward page-delimiter nil t)
8652         (narrow-to-region (match-end 0) (point-max)))
8653       (gnus-narrow-to-page))))
8654
8655 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8656   "Truncate to LEN and quote all \"(\"'s in STRING."
8657   (gnus-replace-in-string (if (and len (> (length string) len))
8658                               (substring string 0 len)
8659                             string)
8660                           "[()]" "\\\\\\&"))
8661
8662 (defun gnus-summary-print-article (&optional filename n)
8663   "Generate and print a PostScript image of the process-marked (mail) articles.
8664
8665 If used interactively, print the current article if none are
8666 process-marked.  With prefix arg, prompt the user for the name of the
8667 file to save in.
8668
8669 When used from Lisp, accept two optional args FILENAME and N.  N means
8670 to print the next N articles.  If N is negative, print the N previous
8671 articles.  If N is nil and articles have been marked with the process
8672 mark, print these instead.
8673
8674 If the optional first argument FILENAME is nil, send the image to the
8675 printer.  If FILENAME is a string, save the PostScript image in a file with
8676 that name.  If FILENAME is a number, prompt the user for the name of the file
8677 to save in."
8678   (interactive (list (ps-print-preprint current-prefix-arg)))
8679   (dolist (article (gnus-summary-work-articles n))
8680     (gnus-summary-select-article nil nil 'pseudo article)
8681     (gnus-eval-in-buffer-window gnus-article-buffer
8682       (gnus-print-buffer))
8683     (gnus-summary-remove-process-mark article))
8684   (ps-despool filename))
8685
8686 (defun gnus-print-buffer ()
8687   (let ((buffer (generate-new-buffer " *print*")))
8688     (unwind-protect
8689         (progn
8690           (copy-to-buffer buffer (point-min) (point-max))
8691           (set-buffer buffer)
8692           (gnus-remove-text-with-property 'gnus-decoration)
8693           (when (gnus-visual-p 'article-highlight 'highlight)
8694             ;; Copy-to-buffer doesn't copy overlay.  So redo
8695             ;; highlight.
8696             (let ((gnus-article-buffer buffer))
8697               (gnus-article-highlight-citation t)
8698               (gnus-article-highlight-signature)
8699               (gnus-article-emphasize)
8700               (gnus-article-delete-invisible-text)))
8701           (let ((ps-left-header
8702                  (list
8703                   (concat "("
8704                           (gnus-summary-print-truncate-and-quote
8705                            (mail-header-subject gnus-current-headers)
8706                            66) ")")
8707                   (concat "("
8708                           (gnus-summary-print-truncate-and-quote
8709                            (mail-header-from gnus-current-headers)
8710                            45) ")")))
8711                 (ps-right-header
8712                  (list
8713                   "/pagenumberstring load"
8714                   (concat "("
8715                           (mail-header-date gnus-current-headers) ")"))))
8716             (gnus-run-hooks 'gnus-ps-print-hook)
8717             (save-excursion
8718               (if window-system
8719                   (ps-spool-buffer-with-faces)
8720                 (ps-spool-buffer)))))
8721       (kill-buffer buffer))))
8722
8723 (defun gnus-summary-show-article (&optional arg)
8724   "Force redisplaying of the current article.
8725 If ARG (the prefix) is a number, show the article with the charset
8726 defined in `gnus-summary-show-article-charset-alist', or the charset
8727 input.
8728 If ARG (the prefix) is non-nil and not a number, show the raw article
8729 without any article massaging functions being run.  Normally, the key
8730 strokes are `C-u g'."
8731   (interactive "P")
8732   (cond
8733    ((numberp arg)
8734     (gnus-summary-show-article t)
8735     (let ((gnus-newsgroup-charset
8736            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8737                (mm-read-coding-system
8738                 "View as charset: " ;; actually it is coding system.
8739                 (with-current-buffer gnus-article-buffer
8740                   (mm-detect-coding-region (point) (point-max))))))
8741           (gnus-newsgroup-ignored-charsets 'gnus-all))
8742       (gnus-summary-select-article nil 'force)
8743       (let ((deps gnus-newsgroup-dependencies)
8744             head header lines)
8745         (save-excursion
8746           (set-buffer gnus-original-article-buffer)
8747           (save-restriction
8748             (message-narrow-to-head)
8749             (setq head (buffer-string))
8750             (goto-char (point-min))
8751             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8752               (goto-char (point-max))
8753               (widen)
8754               (setq lines (1- (count-lines (point) (point-max))))))
8755           (with-temp-buffer
8756             (insert (format "211 %d Article retrieved.\n"
8757                             (cdr gnus-article-current)))
8758             (insert head)
8759             (if lines (insert (format "Lines: %d\n" lines)))
8760             (insert ".\n")
8761             (let ((nntp-server-buffer (current-buffer)))
8762               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8763         (gnus-data-set-header
8764          (gnus-data-find (cdr gnus-article-current))
8765          header)
8766         (gnus-summary-update-article-line
8767          (cdr gnus-article-current) header)
8768         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8769           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8770    ((not arg)
8771     ;; Select the article the normal way.
8772     (gnus-summary-select-article nil 'force))
8773    (t
8774     ;; We have to require this here to make sure that the following
8775     ;; dynamic binding isn't shadowed by autoloading.
8776     (require 'gnus-async)
8777     (require 'gnus-art)
8778     ;; Bind the article treatment functions to nil.
8779     (let ((gnus-have-all-headers t)
8780           gnus-article-prepare-hook
8781           gnus-article-decode-hook
8782           gnus-display-mime-function
8783           gnus-break-pages)
8784       ;; Destroy any MIME parts.
8785       (when (gnus-buffer-live-p gnus-article-buffer)
8786         (save-excursion
8787           (set-buffer gnus-article-buffer)
8788           (mm-destroy-parts gnus-article-mime-handles)
8789           ;; Set it to nil for safety reason.
8790           (setq gnus-article-mime-handle-alist nil)
8791           (setq gnus-article-mime-handles nil)))
8792       (gnus-summary-select-article nil 'force))))
8793   (gnus-summary-goto-subject gnus-current-article)
8794   (gnus-summary-position-point))
8795
8796 (defun gnus-summary-show-raw-article ()
8797   "Show the raw article without any article massaging functions being run."
8798   (interactive)
8799   (gnus-summary-show-article t))
8800
8801 (defun gnus-summary-verbose-headers (&optional arg)
8802   "Toggle permanent full header display.
8803 If ARG is a positive number, turn header display on.
8804 If ARG is a negative number, turn header display off."
8805   (interactive "P")
8806   (setq gnus-show-all-headers
8807         (cond ((or (not (numberp arg))
8808                    (zerop arg))
8809                (not gnus-show-all-headers))
8810               ((natnump arg)
8811                t)))
8812   (gnus-summary-show-article))
8813
8814 (defun gnus-summary-toggle-header (&optional arg)
8815   "Show the headers if they are hidden, or hide them if they are shown.
8816 If ARG is a positive number, show the entire header.
8817 If ARG is a negative number, hide the unwanted header lines."
8818   (interactive "P")
8819   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8820                      (get-buffer-window gnus-article-buffer t))))
8821     (with-current-buffer gnus-article-buffer
8822       (widen)
8823       (article-narrow-to-head)
8824       (let* ((buffer-read-only nil)
8825              (inhibit-point-motion-hooks t)
8826              (hidden (if (numberp arg)
8827                          (>= arg 0)
8828                        (or (not (looking-at "[^ \t\n]+:"))
8829                            (gnus-article-hidden-text-p 'headers))))
8830              s e)
8831         (delete-region (point-min) (point-max))
8832         (with-current-buffer gnus-original-article-buffer
8833           (goto-char (setq s (point-min)))
8834           (setq e (if (search-forward "\n\n" nil t)
8835                       (1- (point))
8836                     (point-max))))
8837         (insert-buffer-substring gnus-original-article-buffer s e)
8838         (run-hooks 'gnus-article-decode-hook)
8839         (if hidden
8840             (let ((gnus-treat-hide-headers nil)
8841                   (gnus-treat-hide-boring-headers nil))
8842               (gnus-delete-wash-type 'headers)
8843               (gnus-treat-article 'head))
8844           (gnus-treat-article 'head))
8845         (widen)
8846         (if window
8847             (set-window-start window (goto-char (point-min))))
8848         (if gnus-break-pages
8849             (gnus-narrow-to-page)
8850           (when (gnus-visual-p 'page-marker)
8851             (let ((buffer-read-only nil))
8852               (gnus-remove-text-with-property 'gnus-prev)
8853               (gnus-remove-text-with-property 'gnus-next))))
8854         (gnus-set-mode-line 'article)))))
8855
8856 (defun gnus-summary-show-all-headers ()
8857   "Make all header lines visible."
8858   (interactive)
8859   (gnus-summary-toggle-header 1))
8860
8861 (defun gnus-summary-caesar-message (&optional arg)
8862   "Caesar rotate the current article by 13.
8863 The numerical prefix specifies how many places to rotate each letter
8864 forward."
8865   (interactive "P")
8866   (gnus-summary-select-article)
8867   (let ((mail-header-separator ""))
8868     (gnus-eval-in-buffer-window gnus-article-buffer
8869       (save-restriction
8870         (widen)
8871         (let ((start (window-start))
8872               buffer-read-only)
8873           (message-caesar-buffer-body arg)
8874           (set-window-start (get-buffer-window (current-buffer)) start))))))
8875
8876 (autoload 'unmorse-region "morse"
8877   "Convert morse coded text in region to ordinary ASCII text."
8878   t)
8879
8880 (defun gnus-summary-morse-message (&optional arg)
8881   "Morse decode the current article."
8882   (interactive "P")
8883   (gnus-summary-select-article)
8884   (let ((mail-header-separator ""))
8885     (gnus-eval-in-buffer-window gnus-article-buffer
8886       (save-excursion
8887         (save-restriction
8888           (widen)
8889           (let ((pos (window-start))
8890                 buffer-read-only)
8891             (goto-char (point-min))
8892             (when (message-goto-body)
8893               (gnus-narrow-to-body))
8894             (goto-char (point-min))
8895             (while (search-forward "·" (point-max) t)
8896               (replace-match "."))
8897             (unmorse-region (point-min) (point-max))
8898             (widen)
8899             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8900
8901 (defun gnus-summary-stop-page-breaking ()
8902   "Stop page breaking in the current article."
8903   (interactive)
8904   (gnus-summary-select-article)
8905   (gnus-eval-in-buffer-window gnus-article-buffer
8906     (widen)
8907     (when (gnus-visual-p 'page-marker)
8908       (let ((buffer-read-only nil))
8909         (gnus-remove-text-with-property 'gnus-prev)
8910         (gnus-remove-text-with-property 'gnus-next))
8911       (setq gnus-page-broken nil))))
8912
8913 (defun gnus-summary-move-article (&optional n to-newsgroup
8914                                             select-method action)
8915   "Move the current article to a different newsgroup.
8916 If N is a positive number, move the N next articles.
8917 If N is a negative number, move the N previous articles.
8918 If N is nil and any articles have been marked with the process mark,
8919 move those articles instead.
8920 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8921 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8922 re-spool using this method.
8923
8924 When called interactively with TO-NEWSGROUP being nil, the value of
8925 the variable `gnus-move-split-methods' is used for finding a default
8926 for the target newsgroup.
8927
8928 For this function to work, both the current newsgroup and the
8929 newsgroup that you want to move to have to support the `request-move'
8930 and `request-accept' functions.
8931
8932 ACTION can be either `move' (the default), `crosspost' or `copy'."
8933   (interactive "P")
8934   (unless action
8935     (setq action 'move))
8936   ;; Check whether the source group supports the required functions.
8937   (cond ((and (eq action 'move)
8938               (not (gnus-check-backend-function
8939                     'request-move-article gnus-newsgroup-name)))
8940          (error "The current group does not support article moving"))
8941         ((and (eq action 'crosspost)
8942               (not (gnus-check-backend-function
8943                     'request-replace-article gnus-newsgroup-name)))
8944          (error "The current group does not support article editing")))
8945   (let ((articles (gnus-summary-work-articles n))
8946         (prefix (if (gnus-check-backend-function
8947                      'request-move-article gnus-newsgroup-name)
8948                     (funcall gnus-move-group-prefix-function
8949                              gnus-newsgroup-name)
8950                   ""))
8951         (names '((move "Move" "Moving")
8952                  (copy "Copy" "Copying")
8953                  (crosspost "Crosspost" "Crossposting")))
8954         (copy-buf (save-excursion
8955                     (nnheader-set-temp-buffer " *copy article*")))
8956         art-group to-method new-xref article to-groups)
8957     (unless (assq action names)
8958       (error "Unknown action %s" action))
8959     ;; Read the newsgroup name.
8960     (when (and (not to-newsgroup)
8961                (not select-method))
8962       (if (and gnus-move-split-methods
8963                (not
8964                 (and (memq gnus-current-article articles)
8965                      (gnus-buffer-live-p gnus-original-article-buffer))))
8966           ;; When `gnus-move-split-methods' is non-nil, we have to
8967           ;; select an article to give `gnus-read-move-group-name' an
8968           ;; opportunity to suggest an appropriate default.  However,
8969           ;; we needn't render or mark the article.
8970           (let ((gnus-display-mime-function nil)
8971                 (gnus-article-prepare-hook nil)
8972                 (gnus-mark-article-hook nil))
8973             (gnus-summary-select-article nil nil nil (car articles))))
8974       (setq to-newsgroup
8975             (gnus-read-move-group-name
8976              (cadr (assq action names))
8977              (symbol-value (intern (format "gnus-current-%s-group" action)))
8978              articles prefix))
8979       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8980     (setq to-method (or select-method
8981                         (gnus-server-to-method
8982                          (gnus-group-method to-newsgroup))))
8983     ;; Check the method we are to move this article to...
8984     (unless (gnus-check-backend-function
8985              'request-accept-article (car to-method))
8986       (error "%s does not support article copying" (car to-method)))
8987     (unless (gnus-check-server to-method)
8988       (error "Can't open server %s" (car to-method)))
8989     (gnus-message 6 "%s to %s: %s..."
8990                   (caddr (assq action names))
8991                   (or (car select-method) to-newsgroup) articles)
8992     (while articles
8993       (setq article (pop articles))
8994       (setq
8995        art-group
8996        (cond
8997         ;; Move the article.
8998         ((eq action 'move)
8999          ;; Remove this article from future suppression.
9000          (gnus-dup-unsuppress-article article)
9001          (gnus-request-move-article
9002           article                       ; Article to move
9003           gnus-newsgroup-name           ; From newsgroup
9004           (nth 1 (gnus-find-method-for-group
9005                   gnus-newsgroup-name)) ; Server
9006           (list 'gnus-request-accept-article
9007                 to-newsgroup (list 'quote select-method)
9008                 (not articles) t)       ; Accept form
9009           (not articles)))              ; Only save nov last time
9010         ;; Copy the article.
9011         ((eq action 'copy)
9012          (save-excursion
9013            (set-buffer copy-buf)
9014            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9015              (gnus-request-accept-article
9016               to-newsgroup select-method (not articles) t))))
9017         ;; Crosspost the article.
9018         ((eq action 'crosspost)
9019          (let ((xref (message-tokenize-header
9020                       (mail-header-xref (gnus-summary-article-header article))
9021                       " ")))
9022            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9023                                   ":" (number-to-string article)))
9024            (unless xref
9025              (setq xref (list (system-name))))
9026            (setq new-xref
9027                  (concat
9028                   (mapconcat 'identity
9029                              (delete "Xref:" (delete new-xref xref))
9030                              " ")
9031                   " " new-xref))
9032            (save-excursion
9033              (set-buffer copy-buf)
9034              ;; First put the article in the destination group.
9035              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9036              (when (consp (setq art-group
9037                                 (gnus-request-accept-article
9038                                  to-newsgroup select-method (not articles))))
9039                (setq new-xref (concat new-xref " " (car art-group)
9040                                       ":"
9041                                       (number-to-string (cdr art-group))))
9042                ;; Now we have the new Xrefs header, so we insert
9043                ;; it and replace the new article.
9044                (nnheader-replace-header "Xref" new-xref)
9045                (gnus-request-replace-article
9046                 (cdr art-group) to-newsgroup (current-buffer))
9047                art-group))))))
9048       (cond
9049        ((not art-group)
9050         (gnus-message 1 "Couldn't %s article %s: %s"
9051                       (cadr (assq action names)) article
9052                       (nnheader-get-report (car to-method))))
9053        ((eq art-group 'junk)
9054         (when (eq action 'move)
9055           (gnus-summary-mark-article article gnus-canceled-mark)
9056           (gnus-message 4 "Deleted article %s" article)
9057           ;; run the delete hook
9058           (run-hook-with-args 'gnus-summary-article-delete-hook
9059                               action
9060                               (gnus-data-header
9061                                (assoc article (gnus-data-list nil)))
9062                               gnus-newsgroup-name nil
9063                               select-method)))
9064        (t
9065         (let* ((pto-group (gnus-group-prefixed-name
9066                            (car art-group) to-method))
9067                (info (gnus-get-info pto-group))
9068                (to-group (gnus-info-group info))
9069                to-marks)
9070           ;; Update the group that has been moved to.
9071           (when (and info
9072                      (memq action '(move copy)))
9073             (unless (member to-group to-groups)
9074               (push to-group to-groups))
9075
9076             (unless (memq article gnus-newsgroup-unreads)
9077               (push 'read to-marks)
9078               (gnus-info-set-read
9079                info (gnus-add-to-range (gnus-info-read info)
9080                                        (list (cdr art-group)))))
9081
9082             ;; See whether the article is to be put in the cache.
9083             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9084                              gnus-article-mark-lists
9085                            (delete '(expirable . expire)
9086                                    (copy-sequence gnus-article-mark-lists))))
9087                   (to-article (cdr art-group)))
9088
9089               ;; Enter the article into the cache in the new group,
9090               ;; if that is required.
9091               (when gnus-use-cache
9092                 (gnus-cache-possibly-enter-article
9093                  to-group to-article
9094                  (memq article gnus-newsgroup-marked)
9095                  (memq article gnus-newsgroup-dormant)
9096                  (memq article gnus-newsgroup-unreads)))
9097
9098               (when gnus-preserve-marks
9099                 ;; Copy any marks over to the new group.
9100                 (when (and (equal to-group gnus-newsgroup-name)
9101                            (not (memq article gnus-newsgroup-unreads)))
9102                   ;; Mark this article as read in this group.
9103                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9104                   (setcdr (gnus-active to-group) to-article)
9105                   (setcdr gnus-newsgroup-active to-article))
9106
9107                 (while marks
9108                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9109                     (when (memq article (symbol-value
9110                                          (intern (format "gnus-newsgroup-%s"
9111                                                          (caar marks)))))
9112                       (push (cdar marks) to-marks)
9113                       ;; If the other group is the same as this group,
9114                       ;; then we have to add the mark to the list.
9115                       (when (equal to-group gnus-newsgroup-name)
9116                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9117                              (cons to-article
9118                                    (symbol-value
9119                                     (intern (format "gnus-newsgroup-%s"
9120                                                     (caar marks)))))))
9121                       ;; Copy the marks to other group.
9122                       (gnus-add-marked-articles
9123                        to-group (cdar marks) (list to-article) info)))
9124                   (setq marks (cdr marks)))
9125
9126                 (gnus-request-set-mark
9127                  to-group (list (list (list to-article) 'add to-marks))))
9128
9129               (gnus-dribble-enter
9130                (concat "(gnus-group-set-info '"
9131                        (gnus-prin1-to-string (gnus-get-info to-group))
9132                        ")"))))
9133
9134           ;; Update the Xref header in this article to point to
9135           ;; the new crossposted article we have just created.
9136           (when (eq action 'crosspost)
9137             (save-excursion
9138               (set-buffer copy-buf)
9139               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9140               (nnheader-replace-header "Xref" new-xref)
9141               (gnus-request-replace-article
9142                article gnus-newsgroup-name (current-buffer))))
9143
9144           ;; run the move/copy/crosspost/respool hook
9145           (run-hook-with-args 'gnus-summary-article-move-hook
9146                               action
9147                               (gnus-data-header
9148                                (assoc article (gnus-data-list nil)))
9149                               gnus-newsgroup-name
9150                               to-newsgroup
9151                               select-method))
9152
9153         ;;;!!!Why is this necessary?
9154         (set-buffer gnus-summary-buffer)
9155         
9156         (gnus-summary-goto-subject article)
9157         (when (eq action 'move)
9158           (gnus-summary-mark-article article gnus-canceled-mark))))
9159       (gnus-summary-remove-process-mark article))
9160     ;; Re-activate all groups that have been moved to.
9161     (save-excursion
9162       (set-buffer gnus-group-buffer)
9163       (let ((gnus-group-marked to-groups))
9164         (gnus-group-get-new-news-this-group nil t)))
9165
9166     (gnus-kill-buffer copy-buf)
9167     (gnus-summary-position-point)
9168     (gnus-set-mode-line 'summary)))
9169
9170 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9171   "Copy the current article to some other group.
9172 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9173 When called interactively, if TO-NEWSGROUP is nil, use the value of
9174 the variable `gnus-move-split-methods' for finding a default target
9175 newsgroup.
9176 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9177 re-spool using this method."
9178   (interactive "P")
9179   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9180
9181 (defun gnus-summary-crosspost-article (&optional n)
9182   "Crosspost the current article to some other group."
9183   (interactive "P")
9184   (gnus-summary-move-article n nil nil 'crosspost))
9185
9186 (defcustom gnus-summary-respool-default-method nil
9187   "Default method type for respooling an article.
9188 If nil, use to the current newsgroup method."
9189   :type 'symbol
9190   :group 'gnus-summary-mail)
9191
9192 (defcustom gnus-summary-display-while-building nil
9193   "If non-nil, show and update the summary buffer as it's being built.
9194 If the value is t, update the buffer after every line is inserted.  If
9195 the value is an integer (N), update the display every N lines."
9196   :group 'gnus-thread
9197   :type '(choice (const :tag "off" nil)
9198                  number
9199                  (const :tag "frequently" t)))
9200
9201 (defun gnus-summary-respool-article (&optional n method)
9202   "Respool the current article.
9203 The article will be squeezed through the mail spooling process again,
9204 which means that it will be put in some mail newsgroup or other
9205 depending on `nnmail-split-methods'.
9206 If N is a positive number, respool the N next articles.
9207 If N is a negative number, respool the N previous articles.
9208 If N is nil and any articles have been marked with the process mark,
9209 respool those articles instead.
9210
9211 Respooling can be done both from mail groups and \"real\" newsgroups.
9212 In the former case, the articles in question will be moved from the
9213 current group into whatever groups they are destined to.  In the
9214 latter case, they will be copied into the relevant groups."
9215   (interactive
9216    (list current-prefix-arg
9217          (let* ((methods (gnus-methods-using 'respool))
9218                 (methname
9219                  (symbol-name (or gnus-summary-respool-default-method
9220                                   (car (gnus-find-method-for-group
9221                                         gnus-newsgroup-name)))))
9222                 (method
9223                  (gnus-completing-read-with-default
9224                   methname "What backend do you want to use when respooling?"
9225                   methods nil t nil 'gnus-mail-method-history))
9226                 ms)
9227            (cond
9228             ((zerop (length (setq ms (gnus-servers-using-backend
9229                                       (intern method)))))
9230              (list (intern method) ""))
9231             ((= 1 (length ms))
9232              (car ms))
9233             (t
9234              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9235                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9236                            ms-alist))))))))
9237   (unless method
9238     (error "No method given for respooling"))
9239   (if (assoc (symbol-name
9240               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9241              (gnus-methods-using 'respool))
9242       (gnus-summary-move-article n nil method)
9243     (gnus-summary-copy-article n nil method)))
9244
9245 (defun gnus-summary-import-article (file &optional edit)
9246   "Import an arbitrary file into a mail newsgroup."
9247   (interactive "fImport file: \nP")
9248   (let ((group gnus-newsgroup-name)
9249         (now (current-time))
9250         atts lines group-art)
9251     (unless (gnus-check-backend-function 'request-accept-article group)
9252       (error "%s does not support article importing" group))
9253     (or (file-readable-p file)
9254         (not (file-regular-p file))
9255         (error "Can't read %s" file))
9256     (save-excursion
9257       (set-buffer (gnus-get-buffer-create " *import file*"))
9258       (erase-buffer)
9259       (nnheader-insert-file-contents file)
9260       (goto-char (point-min))
9261       (if (nnheader-article-p)
9262           (save-restriction
9263             (goto-char (point-min))
9264             (search-forward "\n\n" nil t)
9265             (narrow-to-region (point-min) (1- (point)))
9266             (goto-char (point-min))
9267             (unless (re-search-forward "^date:" nil t)
9268               (goto-char (point-max))
9269               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9270        ;; This doesn't look like an article, so we fudge some headers.
9271         (setq atts (file-attributes file)
9272               lines (count-lines (point-min) (point-max)))
9273         (insert "From: " (read-string "From: ") "\n"
9274                 "Subject: " (read-string "Subject: ") "\n"
9275                 "Date: " (message-make-date (nth 5 atts)) "\n"
9276                 "Message-ID: " (message-make-message-id) "\n"
9277                 "Lines: " (int-to-string lines) "\n"
9278                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9279       (setq group-art (gnus-request-accept-article group nil t))
9280       (kill-buffer (current-buffer)))
9281     (setq gnus-newsgroup-active (gnus-activate-group group))
9282     (forward-line 1)
9283     (gnus-summary-goto-article (cdr group-art) nil t)
9284     (when edit
9285       (gnus-summary-edit-article))))
9286
9287 (defun gnus-summary-create-article ()
9288   "Create an article in a mail newsgroup."
9289   (interactive)
9290   (let ((group gnus-newsgroup-name)
9291         (now (current-time))
9292         group-art)
9293     (unless (gnus-check-backend-function 'request-accept-article group)
9294       (error "%s does not support article importing" group))
9295     (save-excursion
9296       (set-buffer (gnus-get-buffer-create " *import file*"))
9297       (erase-buffer)
9298       (goto-char (point-min))
9299       ;; This doesn't look like an article, so we fudge some headers.
9300       (insert "From: " (read-string "From: ") "\n"
9301               "Subject: " (read-string "Subject: ") "\n"
9302               "Date: " (message-make-date now) "\n"
9303               "Message-ID: " (message-make-message-id) "\n")
9304       (setq group-art (gnus-request-accept-article group nil t))
9305       (kill-buffer (current-buffer)))
9306     (setq gnus-newsgroup-active (gnus-activate-group group))
9307     (forward-line 1)
9308     (gnus-summary-goto-article (cdr group-art) nil t)
9309     (gnus-summary-edit-article)))
9310
9311 (defun gnus-summary-article-posted-p ()
9312   "Say whether the current (mail) article is available from news as well.
9313 This will be the case if the article has both been mailed and posted."
9314   (interactive)
9315   (let ((id (mail-header-references (gnus-summary-article-header)))
9316         (gnus-override-method (car (gnus-refer-article-methods))))
9317     (if (gnus-request-head id "")
9318         (gnus-message 2 "The current message was found on %s"
9319                       gnus-override-method)
9320       (gnus-message 2 "The current message couldn't be found on %s"
9321                     gnus-override-method)
9322       nil)))
9323
9324 (defun gnus-summary-expire-articles (&optional now)
9325   "Expire all articles that are marked as expirable in the current group."
9326   (interactive)
9327   (when (and (not gnus-group-is-exiting-without-update-p)
9328              (gnus-check-backend-function
9329               'request-expire-articles gnus-newsgroup-name))
9330     ;; This backend supports expiry.
9331     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9332            (expirable (if total
9333                           (progn
9334                             ;; We need to update the info for
9335                             ;; this group for `gnus-list-of-read-articles'
9336                             ;; to give us the right answer.
9337                             (gnus-run-hooks 'gnus-exit-group-hook)
9338                             (gnus-summary-update-info)
9339                             (gnus-list-of-read-articles gnus-newsgroup-name))
9340                         (setq gnus-newsgroup-expirable
9341                               (sort gnus-newsgroup-expirable '<))))
9342            (expiry-wait (if now 'immediate
9343                           (gnus-group-find-parameter
9344                            gnus-newsgroup-name 'expiry-wait)))
9345            (nnmail-expiry-target
9346             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9347                 nnmail-expiry-target))
9348            es)
9349       (when expirable
9350         ;; There are expirable articles in this group, so we run them
9351         ;; through the expiry process.
9352         (gnus-message 6 "Expiring articles...")
9353         (unless (gnus-check-group gnus-newsgroup-name)
9354           (error "Can't open server for %s" gnus-newsgroup-name))
9355         ;; The list of articles that weren't expired is returned.
9356         (save-excursion
9357           (if expiry-wait
9358               (let ((nnmail-expiry-wait-function nil)
9359                     (nnmail-expiry-wait expiry-wait))
9360                 (setq es (gnus-request-expire-articles
9361                           expirable gnus-newsgroup-name)))
9362             (setq es (gnus-request-expire-articles
9363                       expirable gnus-newsgroup-name)))
9364           (unless total
9365             (setq gnus-newsgroup-expirable es))
9366           ;; We go through the old list of expirable, and mark all
9367           ;; really expired articles as nonexistent.
9368           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9369             (let ((gnus-use-cache nil))
9370               (dolist (article expirable)
9371                 (when (and (not (memq article es))
9372                            (gnus-data-find article))
9373                   (gnus-summary-mark-article article gnus-canceled-mark)
9374                   (run-hook-with-args 'gnus-summary-article-expire-hook
9375                                       'delete
9376                                       (gnus-data-header
9377                                        (assoc article (gnus-data-list nil)))
9378                                       gnus-newsgroup-name
9379                                       nil
9380                                       nil))))))
9381         (gnus-message 6 "Expiring articles...done")))))
9382
9383 (defun gnus-summary-expire-articles-now ()
9384   "Expunge all expirable articles in the current group.
9385 This means that *all* articles that are marked as expirable will be
9386 deleted forever, right now."
9387   (interactive)
9388   (or gnus-expert-user
9389       (gnus-yes-or-no-p
9390        "Are you really, really, really sure you want to delete all these messages? ")
9391       (error "Phew!"))
9392   (gnus-summary-expire-articles t))
9393
9394 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9395 (defun gnus-summary-delete-article (&optional n)
9396   "Delete the N next (mail) articles.
9397 This command actually deletes articles.  This is not a marking
9398 command.  The article will disappear forever from your life, never to
9399 return.
9400
9401 If N is negative, delete backwards.
9402 If N is nil and articles have been marked with the process mark,
9403 delete these instead.
9404
9405 If `gnus-novice-user' is non-nil you will be asked for
9406 confirmation before the articles are deleted."
9407   (interactive "P")
9408   (unless (gnus-check-backend-function 'request-expire-articles
9409                                        gnus-newsgroup-name)
9410     (error "The current newsgroup does not support article deletion"))
9411   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9412     (error "Couldn't open server"))
9413   ;; Compute the list of articles to delete.
9414   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9415         (nnmail-expiry-target 'delete)
9416         not-deleted)
9417     (if (and gnus-novice-user
9418              (not (gnus-yes-or-no-p
9419                    (format "Do you really want to delete %s forever? "
9420                            (if (> (length articles) 1)
9421                                (format "these %s articles" (length articles))
9422                              "this article")))))
9423         ()
9424       ;; Delete the articles.
9425       (setq not-deleted (gnus-request-expire-articles
9426                          articles gnus-newsgroup-name 'force))
9427       (while articles
9428         (gnus-summary-remove-process-mark (car articles))
9429         ;; The backend might not have been able to delete the article
9430         ;; after all.
9431         (unless (memq (car articles) not-deleted)
9432           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9433         (let* ((article (car articles))
9434                (ghead  (gnus-data-header
9435                                     (assoc article (gnus-data-list nil)))))
9436           (run-hook-with-args 'gnus-summary-article-delete-hook
9437                               'delete ghead gnus-newsgroup-name nil
9438                               nil))
9439         (setq articles (cdr articles)))
9440       (when not-deleted
9441         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9442     (gnus-summary-position-point)
9443     (gnus-set-mode-line 'summary)
9444     not-deleted))
9445
9446 (defun gnus-summary-edit-article (&optional arg)
9447   "Edit the current article.
9448 This will have permanent effect only in mail groups.
9449 If ARG is nil, edit the decoded articles.
9450 If ARG is 1, edit the raw articles.
9451 If ARG is 2, edit the raw articles even in read-only groups.
9452 If ARG is 3, edit the articles with the current handles.
9453 Otherwise, allow editing of articles even in read-only
9454 groups."
9455   (interactive "P")
9456   (let (force raw current-handles)
9457     (cond
9458      ((null arg))
9459      ((eq arg 1)
9460       (setq raw t))
9461      ((eq arg 2)
9462       (setq raw t
9463             force t))
9464      ((eq arg 3)
9465       (setq current-handles
9466             (and (gnus-buffer-live-p gnus-article-buffer)
9467                  (with-current-buffer gnus-article-buffer
9468                    (prog1
9469                        gnus-article-mime-handles
9470                      (setq gnus-article-mime-handles nil))))))
9471      (t
9472       (setq force t)))
9473     (when (and raw (not force)
9474                (member gnus-newsgroup-name '("nndraft:delayed"
9475                                              "nndraft:drafts"
9476                                              "nndraft:queue")))
9477       (error "Can't edit the raw article in group %s"
9478              gnus-newsgroup-name))
9479     (save-excursion
9480       (set-buffer gnus-summary-buffer)
9481       (let ((mail-parse-charset gnus-newsgroup-charset)
9482             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9483         (gnus-set-global-variables)
9484         (when (and (not force)
9485                    (gnus-group-read-only-p))
9486           (error "The current newsgroup does not support article editing"))
9487         (gnus-summary-show-article t)
9488         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9489           (with-current-buffer gnus-article-buffer
9490             (mm-enable-multibyte)))
9491         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9492             (setq raw t))
9493         (gnus-article-edit-article
9494          (if raw 'ignore
9495            `(lambda ()
9496               (let ((mbl mml-buffer-list))
9497                 (setq mml-buffer-list nil)
9498                 (mime-to-mml ,'current-handles)
9499                 (let ((mbl1 mml-buffer-list))
9500                   (setq mml-buffer-list mbl)
9501                   (set (make-local-variable 'mml-buffer-list) mbl1))
9502                 (gnus-make-local-hook 'kill-buffer-hook)
9503                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9504          `(lambda (no-highlight)
9505             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9506                   (message-options message-options)
9507                   (message-options-set-recipient)
9508                   (mail-parse-ignored-charsets
9509                    ',gnus-newsgroup-ignored-charsets))
9510               ,(if (not raw) '(progn
9511                                 (mml-to-mime)
9512                                 (mml-destroy-buffers)
9513                                 (remove-hook 'kill-buffer-hook
9514                                              'mml-destroy-buffers t)
9515                                 (kill-local-variable 'mml-buffer-list)))
9516               (gnus-summary-edit-article-done
9517                ,(or (mail-header-references gnus-current-headers) "")
9518                ,(gnus-group-read-only-p)
9519                ,gnus-summary-buffer no-highlight))))))))
9520
9521 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9522
9523 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9524                                                  no-highlight)
9525   "Make edits to the current article permanent."
9526   (interactive)
9527   (save-excursion
9528    ;; The buffer restriction contains the entire article if it exists.
9529     (when (article-goto-body)
9530       (let ((lines (count-lines (point) (point-max)))
9531             (length (- (point-max) (point)))
9532             (case-fold-search t)
9533             (body (copy-marker (point))))
9534         (goto-char (point-min))
9535         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9536           (delete-region (match-beginning 1) (match-end 1))
9537           (insert (number-to-string length)))
9538         (goto-char (point-min))
9539         (when (re-search-forward
9540                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9541           (delete-region (match-beginning 1) (match-end 1))
9542           (insert (number-to-string length)))
9543         (goto-char (point-min))
9544         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9545           (delete-region (match-beginning 1) (match-end 1))
9546           (insert (number-to-string lines))))))
9547   ;; Replace the article.
9548   (let ((buf (current-buffer)))
9549     (with-temp-buffer
9550       (insert-buffer-substring buf)
9551
9552       (if (and (not read-only)
9553                (not (gnus-request-replace-article
9554                      (cdr gnus-article-current) (car gnus-article-current)
9555                      (current-buffer) t)))
9556           (error "Couldn't replace article")
9557         ;; Update the summary buffer.
9558         (if (and references
9559                  (equal (message-tokenize-header references " ")
9560                         (message-tokenize-header
9561                          (or (message-fetch-field "references") "") " ")))
9562             ;; We only have to update this line.
9563             (save-excursion
9564               (save-restriction
9565                 (message-narrow-to-head)
9566                 (let ((head (buffer-string))
9567                       header)
9568                   (with-temp-buffer
9569                     (insert (format "211 %d Article retrieved.\n"
9570                                     (cdr gnus-article-current)))
9571                     (insert head)
9572                     (insert ".\n")
9573                     (let ((nntp-server-buffer (current-buffer)))
9574                       (setq header (car (gnus-get-newsgroup-headers
9575                                          nil t))))
9576                     (save-excursion
9577                       (set-buffer gnus-summary-buffer)
9578                       (gnus-data-set-header
9579                        (gnus-data-find (cdr gnus-article-current))
9580                        header)
9581                       (gnus-summary-update-article-line
9582                        (cdr gnus-article-current) header)
9583                       (if (gnus-summary-goto-subject
9584                            (cdr gnus-article-current) nil t)
9585                           (gnus-summary-update-secondary-mark
9586                            (cdr gnus-article-current))))))))
9587           ;; Update threads.
9588           (set-buffer (or buffer gnus-summary-buffer))
9589           (gnus-summary-update-article (cdr gnus-article-current))
9590           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9591               (gnus-summary-update-secondary-mark
9592                (cdr gnus-article-current))))
9593         ;; Prettify the article buffer again.
9594         (unless no-highlight
9595           (save-excursion
9596             (set-buffer gnus-article-buffer)
9597             ;;;!!! Fix this -- article should be rehighlighted.
9598             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9599             (set-buffer gnus-original-article-buffer)
9600             (gnus-request-article
9601              (cdr gnus-article-current)
9602              (car gnus-article-current) (current-buffer))))
9603         ;; Prettify the summary buffer line.
9604         (when (gnus-visual-p 'summary-highlight 'highlight)
9605           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9606
9607 (defun gnus-summary-edit-wash (key)
9608   "Perform editing command KEY in the article buffer."
9609   (interactive
9610    (list
9611     (progn
9612       (message "%s" (concat (this-command-keys) "- "))
9613       (read-char))))
9614   (message "")
9615   (gnus-summary-edit-article)
9616   (execute-kbd-macro (concat (this-command-keys) key))
9617   (gnus-article-edit-done))
9618
9619 ;;; Respooling
9620
9621 (defun gnus-summary-respool-query (&optional silent trace)
9622   "Query where the respool algorithm would put this article."
9623   (interactive)
9624   (let (gnus-mark-article-hook)
9625     (gnus-summary-select-article)
9626     (save-excursion
9627       (set-buffer gnus-original-article-buffer)
9628       (let ((groups (nnmail-article-group 'identity trace)))
9629         (unless silent
9630           (if groups
9631               (message "This message would go to %s"
9632                        (mapconcat 'car groups ", "))
9633             (message "This message would go to no groups"))
9634           groups)))))
9635
9636 (defun gnus-summary-respool-trace ()
9637   "Trace where the respool algorithm would put this article.
9638 Display a buffer showing all fancy splitting patterns which matched."
9639   (interactive)
9640   (gnus-summary-respool-query nil t))
9641
9642 ;; Summary marking commands.
9643
9644 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9645   "Mark articles which has the same subject as read, and then select the next.
9646 If UNMARK is positive, remove any kind of mark.
9647 If UNMARK is negative, tick articles."
9648   (interactive "P")
9649   (when unmark
9650     (setq unmark (prefix-numeric-value unmark)))
9651   (let ((count
9652          (gnus-summary-mark-same-subject
9653           (gnus-summary-article-subject) unmark)))
9654     ;; Select next unread article.  If auto-select-same mode, should
9655     ;; select the first unread article.
9656     (gnus-summary-next-article t (and gnus-auto-select-same
9657                                       (gnus-summary-article-subject)))
9658     (gnus-message 7 "%d article%s marked as %s"
9659                   count (if (= count 1) " is" "s are")
9660                   (if unmark "unread" "read"))))
9661
9662 (defun gnus-summary-kill-same-subject (&optional unmark)
9663   "Mark articles which has the same subject as read.
9664 If UNMARK is positive, remove any kind of mark.
9665 If UNMARK is negative, tick articles."
9666   (interactive "P")
9667   (when unmark
9668     (setq unmark (prefix-numeric-value unmark)))
9669   (let ((count
9670          (gnus-summary-mark-same-subject
9671           (gnus-summary-article-subject) unmark)))
9672     ;; If marked as read, go to next unread subject.
9673     (when (null unmark)
9674       ;; Go to next unread subject.
9675       (gnus-summary-next-subject 1 t))
9676     (gnus-message 7 "%d articles are marked as %s"
9677                   count (if unmark "unread" "read"))))
9678
9679 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9680   "Mark articles with same SUBJECT as read, and return marked number.
9681 If optional argument UNMARK is positive, remove any kinds of marks.
9682 If optional argument UNMARK is negative, mark articles as unread instead."
9683   (let ((count 1))
9684     (save-excursion
9685       (cond
9686        ((null unmark)                   ; Mark as read.
9687         (while (and
9688                 (progn
9689                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9690                   (gnus-summary-show-thread) t)
9691                 (gnus-summary-find-subject subject))
9692           (setq count (1+ count))))
9693        ((> unmark 0)                    ; Tick.
9694         (while (and
9695                 (progn
9696                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9697                   (gnus-summary-show-thread) t)
9698                 (gnus-summary-find-subject subject))
9699           (setq count (1+ count))))
9700        (t                               ; Mark as unread.
9701         (while (and
9702                 (progn
9703                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9704                   (gnus-summary-show-thread) t)
9705                 (gnus-summary-find-subject subject))
9706           (setq count (1+ count)))))
9707       (gnus-set-mode-line 'summary)
9708       ;; Return the number of marked articles.
9709       count)))
9710
9711 (defun gnus-summary-mark-as-processable (n &optional unmark)
9712   "Set the process mark on the next N articles.
9713 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9714 the process mark instead.  The difference between N and the actual
9715 number of articles marked is returned."
9716   (interactive "P")
9717   (if (and (null n) (gnus-region-active-p))
9718       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9719     (setq n (prefix-numeric-value n))
9720     (let ((backward (< n 0))
9721           (n (abs n)))
9722       (while (and
9723               (> n 0)
9724               (if unmark
9725                   (gnus-summary-remove-process-mark
9726                    (gnus-summary-article-number))
9727                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9728               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9729         (setq n (1- n)))
9730       (when (/= 0 n)
9731         (gnus-message 7 "No more articles"))
9732       (gnus-summary-recenter)
9733       (gnus-summary-position-point)
9734       n)))
9735
9736 (defun gnus-summary-unmark-as-processable (n)
9737   "Remove the process mark from the next N articles.
9738 If N is negative, unmark backward instead.  The difference between N and
9739 the actual number of articles unmarked is returned."
9740   (interactive "P")
9741   (gnus-summary-mark-as-processable n t))
9742
9743 (defun gnus-summary-unmark-all-processable ()
9744   "Remove the process mark from all articles."
9745   (interactive)
9746   (save-excursion
9747     (while gnus-newsgroup-processable
9748       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9749   (gnus-summary-position-point))
9750
9751 (defun gnus-summary-add-mark (article type)
9752   "Mark ARTICLE with a mark of TYPE."
9753   (let ((vtype (car (assq type gnus-article-mark-lists)))
9754         var)
9755     (if (not vtype)
9756         (error "No such mark type: %s" type)
9757       (setq var (intern (format "gnus-newsgroup-%s" type)))
9758       (set var (cons article (symbol-value var)))
9759       (if (memq type '(processable cached replied forwarded recent saved))
9760           (gnus-summary-update-secondary-mark article)
9761         ;;; !!! This is bogus.  We should find out what primary
9762         ;;; !!! mark we want to set.
9763         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9764
9765 (defun gnus-summary-mark-as-expirable (n)
9766   "Mark N articles forward as expirable.
9767 If N is negative, mark backward instead.  The difference between N and
9768 the actual number of articles marked is returned."
9769   (interactive "p")
9770   (gnus-summary-mark-forward n gnus-expirable-mark))
9771
9772 (defun gnus-summary-mark-as-spam (n)
9773   "Mark N articles forward as spam.
9774 If N is negative, mark backward instead.  The difference between N and
9775 the actual number of articles marked is returned."
9776   (interactive "p")
9777   (gnus-summary-mark-forward n gnus-spam-mark))
9778
9779 (defun gnus-summary-mark-article-as-replied (article)
9780   "Mark ARTICLE as replied to and update the summary line.
9781 ARTICLE can also be a list of articles."
9782   (interactive (list (gnus-summary-article-number)))
9783   (let ((articles (if (listp article) article (list article))))
9784     (dolist (article articles)
9785       (unless (numberp article)
9786         (error "%s is not a number" article))
9787       (push article gnus-newsgroup-replied)
9788       (let ((buffer-read-only nil))
9789         (when (gnus-summary-goto-subject article nil t)
9790           (gnus-summary-update-secondary-mark article))))))
9791
9792 (defun gnus-summary-mark-article-as-forwarded (article)
9793   "Mark ARTICLE as forwarded and update the summary line.
9794 ARTICLE can also be a list of articles."
9795   (let ((articles (if (listp article) article (list article))))
9796     (dolist (article articles)
9797       (push article gnus-newsgroup-forwarded)
9798       (let ((buffer-read-only nil))
9799         (when (gnus-summary-goto-subject article nil t)
9800           (gnus-summary-update-secondary-mark article))))))
9801
9802 (defun gnus-summary-set-bookmark (article)
9803   "Set a bookmark in current article."
9804   (interactive (list (gnus-summary-article-number)))
9805   (when (or (not (get-buffer gnus-article-buffer))
9806             (not gnus-current-article)
9807             (not gnus-article-current)
9808             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9809     (error "No current article selected"))
9810   ;; Remove old bookmark, if one exists.
9811   (gnus-pull article gnus-newsgroup-bookmarks)
9812   ;; Set the new bookmark, which is on the form
9813   ;; (article-number . line-number-in-body).
9814   (push
9815    (cons article
9816          (with-current-buffer gnus-article-buffer
9817            (count-lines
9818             (min (point)
9819                  (save-excursion
9820                    (article-goto-body)
9821                    (point)))
9822             (point))))
9823    gnus-newsgroup-bookmarks)
9824   (gnus-message 6 "A bookmark has been added to the current article."))
9825
9826 (defun gnus-summary-remove-bookmark (article)
9827   "Remove the bookmark from the current article."
9828   (interactive (list (gnus-summary-article-number)))
9829   ;; Remove old bookmark, if one exists.
9830   (if (not (assq article gnus-newsgroup-bookmarks))
9831       (gnus-message 6 "No bookmark in current article.")
9832     (gnus-pull article gnus-newsgroup-bookmarks)
9833     (gnus-message 6 "Removed bookmark.")))
9834
9835 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9836 (defun gnus-summary-mark-as-dormant (n)
9837   "Mark N articles forward as dormant.
9838 If N is negative, mark backward instead.  The difference between N and
9839 the actual number of articles marked is returned."
9840   (interactive "p")
9841   (gnus-summary-mark-forward n gnus-dormant-mark))
9842
9843 (defun gnus-summary-set-process-mark (article)
9844   "Set the process mark on ARTICLE and update the summary line."
9845   (setq gnus-newsgroup-processable
9846         (cons article
9847               (delq article gnus-newsgroup-processable)))
9848   (when (gnus-summary-goto-subject article)
9849     (gnus-summary-show-thread)
9850     (gnus-summary-goto-subject article)
9851     (gnus-summary-update-secondary-mark article)))
9852
9853 (defun gnus-summary-remove-process-mark (article)
9854   "Remove the process mark from ARTICLE and update the summary line."
9855   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9856   (when (gnus-summary-goto-subject article)
9857     (gnus-summary-show-thread)
9858     (gnus-summary-goto-subject article)
9859     (gnus-summary-update-secondary-mark article)))
9860
9861 (defun gnus-summary-set-saved-mark (article)
9862   "Set the process mark on ARTICLE and update the summary line."
9863   (push article gnus-newsgroup-saved)
9864   (when (gnus-summary-goto-subject article)
9865     (gnus-summary-update-secondary-mark article)))
9866
9867 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9868   "Mark N articles as read forwards.
9869 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9870 The difference between N and the actual number of articles marked is
9871 returned.
9872 If NO-EXPIRE, auto-expiry will be inhibited."
9873   (interactive "p")
9874   (gnus-summary-show-thread)
9875   (let ((backward (< n 0))
9876         (gnus-summary-goto-unread
9877          (and gnus-summary-goto-unread
9878               (not (eq gnus-summary-goto-unread 'never))
9879               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9880                                     gnus-ticked-mark gnus-dormant-mark)))))
9881         (n (abs n))
9882         (mark (or mark gnus-del-mark)))
9883     (while (and (> n 0)
9884                 (gnus-summary-mark-article nil mark no-expire)
9885                 (zerop (gnus-summary-next-subject
9886                         (if backward -1 1)
9887                         (and gnus-summary-goto-unread
9888                              (not (eq gnus-summary-goto-unread 'never)))
9889                         t)))
9890       (setq n (1- n)))
9891     (when (/= 0 n)
9892       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9893     (gnus-summary-recenter)
9894     (gnus-summary-position-point)
9895     (gnus-set-mode-line 'summary)
9896     n))
9897
9898 (defun gnus-summary-mark-article-as-read (mark)
9899   "Mark the current article quickly as read with MARK."
9900   (let ((article (gnus-summary-article-number)))
9901     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9902     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9903     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9904     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9905     (push (cons article mark) gnus-newsgroup-reads)
9906     ;; Possibly remove from cache, if that is used.
9907     (when gnus-use-cache
9908       (gnus-cache-enter-remove-article article))
9909     ;; Allow the backend to change the mark.
9910     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9911     ;; Check for auto-expiry.
9912     (when (and gnus-newsgroup-auto-expire
9913                (memq mark gnus-auto-expirable-marks))
9914       (setq mark gnus-expirable-mark)
9915       ;; Let the backend know about the mark change.
9916       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9917       (push article gnus-newsgroup-expirable))
9918     ;; Set the mark in the buffer.
9919     (gnus-summary-update-mark mark 'unread)
9920     t))
9921
9922 (defun gnus-summary-mark-article-as-unread (mark)
9923   "Mark the current article quickly as unread with MARK."
9924   (let* ((article (gnus-summary-article-number))
9925          (old-mark (gnus-summary-article-mark article)))
9926     ;; Allow the backend to change the mark.
9927     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9928     (if (eq mark old-mark)
9929         t
9930       (if (<= article 0)
9931           (progn
9932             (gnus-error 1 "Can't mark negative article numbers")
9933             nil)
9934         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9935         (setq gnus-newsgroup-spam-marked
9936               (delq article gnus-newsgroup-spam-marked))
9937         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9938         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9939         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9940         (cond ((= mark gnus-ticked-mark)
9941                (setq gnus-newsgroup-marked
9942                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9943                                               article)))
9944               ((= mark gnus-spam-mark)
9945                (setq gnus-newsgroup-spam-marked
9946                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9947                                               article)))
9948               ((= mark gnus-dormant-mark)
9949                (setq gnus-newsgroup-dormant
9950                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9951                                               article)))
9952               (t
9953                (setq gnus-newsgroup-unreads
9954                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9955                                               article))))
9956         (gnus-pull article gnus-newsgroup-reads)
9957
9958         ;; See whether the article is to be put in the cache.
9959         (and gnus-use-cache
9960              (vectorp (gnus-summary-article-header article))
9961              (save-excursion
9962                (gnus-cache-possibly-enter-article
9963                 gnus-newsgroup-name article
9964                 (= mark gnus-ticked-mark)
9965                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9966
9967         ;; Fix the mark.
9968         (gnus-summary-update-mark mark 'unread)
9969         t))))
9970
9971 (defun gnus-summary-mark-article (&optional article mark no-expire)
9972   "Mark ARTICLE with MARK.  MARK can be any character.
9973 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9974 `??' (dormant) and `?E' (expirable).
9975 If MARK is nil, then the default character `?r' is used.
9976 If ARTICLE is nil, then the article on the current line will be
9977 marked.
9978 If NO-EXPIRE, auto-expiry will be inhibited."
9979   ;; The mark might be a string.
9980   (when (stringp mark)
9981     (setq mark (aref mark 0)))
9982   ;; If no mark is given, then we check auto-expiring.
9983   (when (null mark)
9984     (setq mark gnus-del-mark))
9985   (when (and (not no-expire)
9986              gnus-newsgroup-auto-expire
9987              (memq mark gnus-auto-expirable-marks))
9988     (setq mark gnus-expirable-mark))
9989   (let ((article (or article (gnus-summary-article-number)))
9990         (old-mark (gnus-summary-article-mark article)))
9991     ;; Allow the backend to change the mark.
9992     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9993     (if (eq mark old-mark)
9994         t
9995       (unless article
9996         (error "No article on current line"))
9997       (if (not (if (or (= mark gnus-unread-mark)
9998                        (= mark gnus-ticked-mark)
9999                        (= mark gnus-spam-mark)
10000                        (= mark gnus-dormant-mark))
10001                    (gnus-mark-article-as-unread article mark)
10002                  (gnus-mark-article-as-read article mark)))
10003           t
10004         ;; See whether the article is to be put in the cache.
10005         (and gnus-use-cache
10006              (not (= mark gnus-canceled-mark))
10007              (vectorp (gnus-summary-article-header article))
10008              (save-excursion
10009                (gnus-cache-possibly-enter-article
10010                 gnus-newsgroup-name article
10011                 (= mark gnus-ticked-mark)
10012                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10013
10014         (when (gnus-summary-goto-subject article nil t)
10015           (let ((buffer-read-only nil))
10016             (gnus-summary-show-thread)
10017             ;; Fix the mark.
10018             (gnus-summary-update-mark mark 'unread)
10019             t))))))
10020
10021 (defun gnus-summary-update-secondary-mark (article)
10022   "Update the secondary (read, process, cache) mark."
10023   (gnus-summary-update-mark
10024    (cond ((memq article gnus-newsgroup-processable)
10025           gnus-process-mark)
10026          ((memq article gnus-newsgroup-cached)
10027           gnus-cached-mark)
10028          ((memq article gnus-newsgroup-replied)
10029           gnus-replied-mark)
10030          ((memq article gnus-newsgroup-forwarded)
10031           gnus-forwarded-mark)
10032          ((memq article gnus-newsgroup-saved)
10033           gnus-saved-mark)
10034          ((memq article gnus-newsgroup-recent)
10035           gnus-recent-mark)
10036          ((memq article gnus-newsgroup-unseen)
10037           gnus-unseen-mark)
10038          (t gnus-no-mark))
10039    'replied)
10040   (when (gnus-visual-p 'summary-highlight 'highlight)
10041     (gnus-run-hooks 'gnus-summary-update-hook))
10042   t)
10043
10044 (defun gnus-summary-update-download-mark (article)
10045   "Update the download mark."
10046   (gnus-summary-update-mark
10047    (cond ((memq article gnus-newsgroup-undownloaded)
10048           gnus-undownloaded-mark)
10049          (gnus-newsgroup-agentized
10050           gnus-downloaded-mark)
10051          (t
10052           gnus-no-mark))
10053    'download)
10054   (gnus-summary-update-line t)
10055   t)
10056
10057 (defun gnus-summary-update-mark (mark type)
10058   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10059         (buffer-read-only nil))
10060     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10061     (when forward
10062       (when (looking-at "\r")
10063         (incf forward))
10064       (when (<= (+ forward (point)) (point-max))
10065         ;; Go to the right position on the line.
10066         (goto-char (+ forward (point)))
10067         ;; Replace the old mark with the new mark.
10068         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10069         ;; Optionally update the marks by some user rule.
10070         (when (eq type 'unread)
10071           (gnus-data-set-mark
10072            (gnus-data-find (gnus-summary-article-number)) mark)
10073           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10074
10075 (defun gnus-mark-article-as-read (article &optional mark)
10076   "Enter ARTICLE in the pertinent lists and remove it from others."
10077   ;; Make the article expirable.
10078   (let ((mark (or mark gnus-del-mark)))
10079     (setq gnus-newsgroup-expirable
10080           (if (= mark gnus-expirable-mark)
10081               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10082             (delq article gnus-newsgroup-expirable)))
10083     ;; Remove from unread and marked lists.
10084     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10085     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10086     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10087     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10088     (push (cons article mark) gnus-newsgroup-reads)
10089     ;; Possibly remove from cache, if that is used.
10090     (when gnus-use-cache
10091       (gnus-cache-enter-remove-article article))
10092     t))
10093
10094 (defun gnus-mark-article-as-unread (article &optional mark)
10095   "Enter ARTICLE in the pertinent lists and remove it from others."
10096   (let ((mark (or mark gnus-ticked-mark)))
10097     (if (<= article 0)
10098         (progn
10099           (gnus-error 1 "Can't mark negative article numbers")
10100           nil)
10101       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10102             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10103             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10104             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10105             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10106
10107       ;; Unsuppress duplicates?
10108       (when gnus-suppress-duplicates
10109         (gnus-dup-unsuppress-article article))
10110
10111       (cond ((= mark gnus-ticked-mark)
10112              (setq gnus-newsgroup-marked
10113                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10114             ((= mark gnus-spam-mark)
10115              (setq gnus-newsgroup-spam-marked
10116                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10117                                             article)))
10118             ((= mark gnus-dormant-mark)
10119              (setq gnus-newsgroup-dormant
10120                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10121             (t
10122              (setq gnus-newsgroup-unreads
10123                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10124       (gnus-pull article gnus-newsgroup-reads)
10125       t)))
10126
10127 (defalias 'gnus-summary-mark-as-unread-forward
10128   'gnus-summary-tick-article-forward)
10129 (make-obsolete 'gnus-summary-mark-as-unread-forward
10130                'gnus-summary-tick-article-forward)
10131 (defun gnus-summary-tick-article-forward (n)
10132   "Tick N articles forwards.
10133 If N is negative, tick backwards instead.
10134 The difference between N and the number of articles ticked is returned."
10135   (interactive "p")
10136   (gnus-summary-mark-forward n gnus-ticked-mark))
10137
10138 (defalias 'gnus-summary-mark-as-unread-backward
10139   'gnus-summary-tick-article-backward)
10140 (make-obsolete 'gnus-summary-mark-as-unread-backward
10141                'gnus-summary-tick-article-backward)
10142 (defun gnus-summary-tick-article-backward (n)
10143   "Tick N articles backwards.
10144 The difference between N and the number of articles ticked is returned."
10145   (interactive "p")
10146   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10147
10148 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10149 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10150 (defun gnus-summary-tick-article (&optional article clear-mark)
10151   "Mark current article as unread.
10152 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10153 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10154   (interactive)
10155   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10156                                        gnus-ticked-mark)))
10157
10158 (defun gnus-summary-mark-as-read-forward (n)
10159   "Mark N articles as read forwards.
10160 If N is negative, mark backwards instead.
10161 The difference between N and the actual number of articles marked is
10162 returned."
10163   (interactive "p")
10164   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10165
10166 (defun gnus-summary-mark-as-read-backward (n)
10167   "Mark the N articles as read backwards.
10168 The difference between N and the actual number of articles marked is
10169 returned."
10170   (interactive "p")
10171   (gnus-summary-mark-forward
10172    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10173
10174 (defun gnus-summary-mark-as-read (&optional article mark)
10175   "Mark current article as read.
10176 ARTICLE specifies the article to be marked as read.
10177 MARK specifies a string to be inserted at the beginning of the line."
10178   (gnus-summary-mark-article article mark))
10179
10180 (defun gnus-summary-clear-mark-forward (n)
10181   "Clear marks from N articles forward.
10182 If N is negative, clear backward instead.
10183 The difference between N and the number of marks cleared is returned."
10184   (interactive "p")
10185   (gnus-summary-mark-forward n gnus-unread-mark))
10186
10187 (defun gnus-summary-clear-mark-backward (n)
10188   "Clear marks from N articles backward.
10189 The difference between N and the number of marks cleared is returned."
10190   (interactive "p")
10191   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10192
10193 (defun gnus-summary-mark-unread-as-read ()
10194   "Intended to be used by `gnus-summary-mark-article-hook'."
10195   (when (memq gnus-current-article gnus-newsgroup-unreads)
10196     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10197
10198 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10199   "Intended to be used by `gnus-summary-mark-article-hook'."
10200   (let ((mark (gnus-summary-article-mark)))
10201     (when (or (gnus-unread-mark-p mark)
10202               (gnus-read-mark-p mark))
10203       (gnus-summary-mark-article gnus-current-article
10204                                  (or new-mark gnus-read-mark)))))
10205
10206 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10207   "Intended to be used by `gnus-summary-mark-article-hook'."
10208   (let ((mark (gnus-summary-article-mark)))
10209     (when (or (gnus-unread-mark-p mark)
10210               (gnus-read-mark-p mark))
10211       (gnus-summary-mark-article (gnus-summary-article-number)
10212                                  (or new-mark gnus-read-mark)))))
10213
10214 (defun gnus-summary-mark-unread-as-ticked ()
10215   "Intended to be used by `gnus-summary-mark-article-hook'."
10216   (when (memq gnus-current-article gnus-newsgroup-unreads)
10217     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10218
10219 (defun gnus-summary-mark-region-as-read (point mark all)
10220   "Mark all unread articles between point and mark as read.
10221 If given a prefix, mark all articles between point and mark as read,
10222 even ticked and dormant ones."
10223   (interactive "r\nP")
10224   (save-excursion
10225     (let (article)
10226       (goto-char point)
10227       (beginning-of-line)
10228       (while (and
10229               (< (point) mark)
10230               (progn
10231                 (when (or all
10232                           (memq (setq article (gnus-summary-article-number))
10233                                 gnus-newsgroup-unreads))
10234                   (gnus-summary-mark-article article gnus-del-mark))
10235                 t)
10236               (gnus-summary-find-next))))))
10237
10238 (defun gnus-summary-mark-below (score mark)
10239   "Mark articles with score less than SCORE with MARK."
10240   (interactive "P\ncMark: ")
10241   (setq score (if score
10242                   (prefix-numeric-value score)
10243                 (or gnus-summary-default-score 0)))
10244   (save-excursion
10245     (set-buffer gnus-summary-buffer)
10246     (goto-char (point-min))
10247     (while
10248         (progn
10249           (and (< (gnus-summary-article-score) score)
10250                (gnus-summary-mark-article nil mark))
10251           (gnus-summary-find-next)))))
10252
10253 (defun gnus-summary-kill-below (&optional score)
10254   "Mark articles with score below SCORE as read."
10255   (interactive "P")
10256   (gnus-summary-mark-below score gnus-killed-mark))
10257
10258 (defun gnus-summary-clear-above (&optional score)
10259   "Clear all marks from articles with score above SCORE."
10260   (interactive "P")
10261   (gnus-summary-mark-above score gnus-unread-mark))
10262
10263 (defun gnus-summary-tick-above (&optional score)
10264   "Tick all articles with score above SCORE."
10265   (interactive "P")
10266   (gnus-summary-mark-above score gnus-ticked-mark))
10267
10268 (defun gnus-summary-mark-above (score mark)
10269   "Mark articles with score over SCORE with MARK."
10270   (interactive "P\ncMark: ")
10271   (setq score (if score
10272                   (prefix-numeric-value score)
10273                 (or gnus-summary-default-score 0)))
10274   (save-excursion
10275     (set-buffer gnus-summary-buffer)
10276     (goto-char (point-min))
10277     (while (and (progn
10278                   (when (> (gnus-summary-article-score) score)
10279                     (gnus-summary-mark-article nil mark))
10280                   t)
10281                 (gnus-summary-find-next)))))
10282
10283 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10284 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10285 (defun gnus-summary-limit-include-expunged (&optional no-error)
10286   "Display all the hidden articles that were expunged for low scores."
10287   (interactive)
10288   (let ((buffer-read-only nil))
10289     (let ((scored gnus-newsgroup-scored)
10290           headers h)
10291       (while scored
10292         (unless (gnus-summary-article-header (caar scored))
10293           (and (setq h (gnus-number-to-header (caar scored)))
10294                (< (cdar scored) gnus-summary-expunge-below)
10295                (push h headers)))
10296         (setq scored (cdr scored)))
10297       (if (not headers)
10298           (when (not no-error)
10299             (error "No expunged articles hidden"))
10300         (goto-char (point-min))
10301         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10302         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10303         (mapcar (lambda (x) (push (mail-header-number x)
10304                                   gnus-newsgroup-limit))
10305                 headers)
10306         (gnus-summary-prepare-unthreaded (nreverse headers))
10307         (goto-char (point-min))
10308         (gnus-summary-position-point)
10309         t))))
10310
10311 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10312   "Mark all unread articles in this newsgroup as read.
10313 If prefix argument ALL is non-nil, ticked and dormant articles will
10314 also be marked as read.
10315 If QUIETLY is non-nil, no questions will be asked.
10316
10317 If TO-HERE is non-nil, it should be a point in the buffer.  All
10318 articles before (after, if REVERSE is set) this point will be marked
10319 as read.
10320
10321 Note that this function will only catch up the unread article
10322 in the current summary buffer limitation.
10323
10324 The number of articles marked as read is returned."
10325   (interactive "P")
10326   (prog1
10327       (save-excursion
10328         (when (or quietly
10329                   (not gnus-interactive-catchup) ;Without confirmation?
10330                   gnus-expert-user
10331                   (gnus-y-or-n-p
10332                    (if all
10333                        "Mark absolutely all articles as read? "
10334                      "Mark all unread articles as read? ")))
10335           (if (and not-mark
10336                    (not gnus-newsgroup-adaptive)
10337                    (not gnus-newsgroup-auto-expire)
10338                    (not gnus-suppress-duplicates)
10339                    (or (not gnus-use-cache)
10340                        (eq gnus-use-cache 'passive)))
10341               (progn
10342                 (when all
10343                   (setq gnus-newsgroup-marked nil
10344                         gnus-newsgroup-spam-marked nil
10345                         gnus-newsgroup-dormant nil))
10346                 (setq gnus-newsgroup-unreads
10347                       (gnus-sorted-nunion
10348                        (gnus-intersection gnus-newsgroup-unreads
10349                                           gnus-newsgroup-downloadable)
10350                        gnus-newsgroup-unfetched)))
10351             ;; We actually mark all articles as canceled, which we
10352             ;; have to do when using auto-expiry or adaptive scoring.
10353             (gnus-summary-show-all-threads)
10354             (if (and to-here reverse)
10355                 (progn
10356                   (goto-char to-here)
10357                   (gnus-summary-mark-current-read-and-unread-as-read
10358                    gnus-catchup-mark)
10359                   (while (gnus-summary-find-next (not all))
10360                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10361               (when (gnus-summary-first-subject (not all))
10362                 (while (and
10363                         (if to-here (< (point) to-here) t)
10364                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10365                         (gnus-summary-find-next (not all))))))
10366             (gnus-set-mode-line 'summary))
10367           t))
10368     (gnus-summary-position-point)))
10369
10370 (defun gnus-summary-catchup-to-here (&optional all)
10371   "Mark all unticked articles before the current one as read.
10372 If ALL is non-nil, also mark ticked and dormant articles as read."
10373   (interactive "P")
10374   (save-excursion
10375     (gnus-save-hidden-threads
10376       (let ((beg (point)))
10377         ;; We check that there are unread articles.
10378         (when (or all (gnus-summary-find-prev))
10379           (gnus-summary-catchup all t beg)))))
10380   (gnus-summary-position-point))
10381
10382 (defun gnus-summary-catchup-from-here (&optional all)
10383   "Mark all unticked articles after (and including) the current one as read.
10384 If ALL is non-nil, also mark ticked and dormant articles as read."
10385   (interactive "P")
10386   (save-excursion
10387     (gnus-save-hidden-threads
10388       (let ((beg (point)))
10389         ;; We check that there are unread articles.
10390         (when (or all (gnus-summary-find-next))
10391           (gnus-summary-catchup all t beg nil t)))))
10392   (gnus-summary-position-point))
10393
10394 (defun gnus-summary-catchup-all (&optional quietly)
10395   "Mark all articles in this newsgroup as read.
10396 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10397 instead, which marks only unread articles as read."
10398   (interactive "P")
10399   (gnus-summary-catchup t quietly))
10400
10401 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10402   "Mark all unread articles in this group as read, then exit.
10403 If prefix argument ALL is non-nil, all articles are marked as read.
10404 If QUIETLY is non-nil, no questions will be asked."
10405   (interactive "P")
10406   (when (gnus-summary-catchup all quietly nil 'fast)
10407     ;; Select next newsgroup or exit.
10408     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10409              (eq gnus-auto-select-next 'quietly))
10410         (gnus-summary-next-group nil)
10411       (gnus-summary-exit))))
10412
10413 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10414   "Mark all articles in this newsgroup as read, and then exit.
10415 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10416 instead, which marks only unread articles as read."
10417   (interactive "P")
10418   (gnus-summary-catchup-and-exit t quietly))
10419
10420 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10421   "Mark all articles in this group as read and select the next group.
10422 If given a prefix, mark all articles, unread as well as ticked, as
10423 read."
10424   (interactive "P")
10425   (save-excursion
10426     (gnus-summary-catchup all))
10427   (gnus-summary-next-group))
10428
10429 ;;;
10430 ;;; with article
10431 ;;;
10432
10433 (defmacro gnus-with-article (article &rest forms)
10434   "Select ARTICLE and perform FORMS in the original article buffer.
10435 Then replace the article with the result."
10436   `(progn
10437      ;; We don't want the article to be marked as read.
10438      (let (gnus-mark-article-hook)
10439        (gnus-summary-select-article t t nil ,article))
10440      (set-buffer gnus-original-article-buffer)
10441      ,@forms
10442      (if (not (gnus-check-backend-function
10443                'request-replace-article (car gnus-article-current)))
10444          (gnus-message 5 "Read-only group; not replacing")
10445        (unless (gnus-request-replace-article
10446                 ,article (car gnus-article-current)
10447                 (current-buffer) t)
10448          (error "Couldn't replace article")))
10449      ;; The cache and backlog have to be flushed somewhat.
10450      (when gnus-keep-backlog
10451        (gnus-backlog-remove-article
10452         (car gnus-article-current) (cdr gnus-article-current)))
10453      (when gnus-use-cache
10454        (gnus-cache-update-article
10455         (car gnus-article-current) (cdr gnus-article-current)))))
10456
10457 (put 'gnus-with-article 'lisp-indent-function 1)
10458 (put 'gnus-with-article 'edebug-form-spec '(form body))
10459
10460 ;; Thread-based commands.
10461
10462 (defun gnus-summary-articles-in-thread (&optional article)
10463   "Return a list of all articles in the current thread.
10464 If ARTICLE is non-nil, return all articles in the thread that starts
10465 with that article."
10466   (let* ((article (or article (gnus-summary-article-number)))
10467          (data (gnus-data-find-list article))
10468          (top-level (gnus-data-level (car data)))
10469          (top-subject
10470           (cond ((null gnus-thread-operation-ignore-subject)
10471                  (gnus-simplify-subject-re
10472                   (mail-header-subject (gnus-data-header (car data)))))
10473                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10474                  (gnus-simplify-subject-fuzzy
10475                   (mail-header-subject (gnus-data-header (car data)))))
10476                 (t nil)))
10477          (end-point (save-excursion
10478                       (if (gnus-summary-go-to-next-thread)
10479                           (point) (point-max))))
10480          articles)
10481     (while (and data
10482                 (< (gnus-data-pos (car data)) end-point))
10483       (when (or (not top-subject)
10484                 (string= top-subject
10485                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10486                              (gnus-simplify-subject-fuzzy
10487                               (mail-header-subject
10488                                (gnus-data-header (car data))))
10489                            (gnus-simplify-subject-re
10490                             (mail-header-subject
10491                              (gnus-data-header (car data)))))))
10492         (push (gnus-data-number (car data)) articles))
10493       (unless (and (setq data (cdr data))
10494                    (> (gnus-data-level (car data)) top-level))
10495         (setq data nil)))
10496     ;; Return the list of articles.
10497     (nreverse articles)))
10498
10499 (defun gnus-summary-rethread-current ()
10500   "Rethread the thread the current article is part of."
10501   (interactive)
10502   (let* ((gnus-show-threads t)
10503          (article (gnus-summary-article-number))
10504          (id (mail-header-id (gnus-summary-article-header)))
10505          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10506     (unless id
10507       (error "No article on the current line"))
10508     (gnus-rebuild-thread id)
10509     (gnus-summary-goto-subject article)))
10510
10511 (defun gnus-summary-reparent-thread ()
10512   "Make the current article child of the marked (or previous) article.
10513
10514 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10515 is non-nil or the Subject: of both articles are the same."
10516   (interactive)
10517   (unless (not (gnus-group-read-only-p))
10518     (error "The current newsgroup does not support article editing"))
10519   (unless (<= (length gnus-newsgroup-processable) 1)
10520     (error "No more than one article may be marked"))
10521   (save-window-excursion
10522     (let ((gnus-article-buffer " *reparent*")
10523           (current-article (gnus-summary-article-number))
10524           ;; First grab the marked article, otherwise one line up.
10525           (parent-article (if (not (null gnus-newsgroup-processable))
10526                               (car gnus-newsgroup-processable)
10527                             (save-excursion
10528                               (if (eq (forward-line -1) 0)
10529                                   (gnus-summary-article-number)
10530                                 (error "Beginning of summary buffer"))))))
10531       (unless (not (eq current-article parent-article))
10532         (error "An article may not be self-referential"))
10533       (let ((message-id (mail-header-id
10534                          (gnus-summary-article-header parent-article))))
10535         (unless (and message-id (not (equal message-id "")))
10536           (error "No message-id in desired parent"))
10537         (gnus-with-article current-article
10538           (save-restriction
10539             (goto-char (point-min))
10540             (message-narrow-to-head)
10541             (if (re-search-forward "^References: " nil t)
10542                 (progn
10543                   (re-search-forward "^[^ \t]" nil t)
10544                   (forward-line -1)
10545                   (end-of-line)
10546                   (insert " " message-id))
10547               (insert "References: " message-id "\n"))))
10548         (set-buffer gnus-summary-buffer)
10549         (gnus-summary-unmark-all-processable)
10550         (gnus-summary-update-article current-article)
10551         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10552             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10553         (gnus-summary-rethread-current)
10554         (gnus-message 3 "Article %d is now the child of article %d"
10555                       current-article parent-article)))))
10556
10557 (defun gnus-summary-toggle-threads (&optional arg)
10558   "Toggle showing conversation threads.
10559 If ARG is positive number, turn showing conversation threads on."
10560   (interactive "P")
10561   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10562     (setq gnus-show-threads
10563           (if (null arg) (not gnus-show-threads)
10564             (> (prefix-numeric-value arg) 0)))
10565     (gnus-summary-prepare)
10566     (gnus-summary-goto-subject current)
10567     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10568     (gnus-summary-position-point)))
10569
10570 (defun gnus-summary-show-all-threads ()
10571   "Show all threads."
10572   (interactive)
10573   (save-excursion
10574     (let ((buffer-read-only nil))
10575       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10576   (gnus-summary-position-point))
10577
10578 (defun gnus-summary-show-thread ()
10579   "Show thread subtrees.
10580 Returns nil if no thread was there to be shown."
10581   (interactive)
10582   (let ((buffer-read-only nil)
10583         (orig (point))
10584         (end (point-at-eol))
10585         ;; Leave point at bol
10586         (beg (progn (beginning-of-line) (point))))
10587     (prog1
10588         ;; Any hidden lines here?
10589         (search-forward "\r" end t)
10590       (subst-char-in-region beg end ?\^M ?\n t)
10591       (goto-char orig)
10592       (gnus-summary-position-point))))
10593
10594 (defun gnus-summary-maybe-hide-threads ()
10595   "If requested, hide the threads that should be hidden."
10596   (when (and gnus-show-threads
10597              gnus-thread-hide-subtree)
10598     (gnus-summary-hide-all-threads
10599      (if (or (consp gnus-thread-hide-subtree)
10600              (functionp gnus-thread-hide-subtree))
10601          (gnus-make-predicate gnus-thread-hide-subtree)
10602        nil))))
10603
10604 ;;; Hiding predicates.
10605
10606 (defun gnus-article-unread-p (header)
10607   (memq (mail-header-number header) gnus-newsgroup-unreads))
10608
10609 (defun gnus-article-unseen-p (header)
10610   (memq (mail-header-number header) gnus-newsgroup-unseen))
10611
10612 (defun gnus-map-articles (predicate articles)
10613   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10614   (apply 'gnus-or (mapcar predicate
10615                           (mapcar 'gnus-summary-article-header articles))))
10616
10617 (defun gnus-summary-hide-all-threads (&optional predicate)
10618   "Hide all thread subtrees.
10619 If PREDICATE is supplied, threads that satisfy this predicate
10620 will not be hidden."
10621   (interactive)
10622   (save-excursion
10623     (goto-char (point-min))
10624     (let ((end nil))
10625       (while (not end)
10626         (when (or (not predicate)
10627                   (gnus-map-articles
10628                    predicate (gnus-summary-article-children)))
10629             (gnus-summary-hide-thread))
10630         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10631   (gnus-summary-position-point))
10632
10633 (defun gnus-summary-hide-thread ()
10634   "Hide thread subtrees.
10635 If PREDICATE is supplied, threads that satisfy this predicate
10636 will not be hidden.
10637 Returns nil if no threads were there to be hidden."
10638   (interactive)
10639   (let ((buffer-read-only nil)
10640         (start (point))
10641         (article (gnus-summary-article-number)))
10642     (goto-char start)
10643     ;; Go forward until either the buffer ends or the subthread
10644     ;; ends.
10645     (when (and (not (eobp))
10646                (or (zerop (gnus-summary-next-thread 1 t))
10647                    (goto-char (point-max))))
10648       (prog1
10649           (if (and (> (point) start)
10650                    (search-backward "\n" start t))
10651               (progn
10652                 (subst-char-in-region start (point) ?\n ?\^M)
10653                 (gnus-summary-goto-subject article))
10654             (goto-char start)
10655             nil)))))
10656
10657 (defun gnus-summary-go-to-next-thread (&optional previous)
10658   "Go to the same level (or less) next thread.
10659 If PREVIOUS is non-nil, go to previous thread instead.
10660 Return the article number moved to, or nil if moving was impossible."
10661   (let ((level (gnus-summary-thread-level))
10662         (way (if previous -1 1))
10663         (beg (point)))
10664     (forward-line way)
10665     (while (and (not (eobp))
10666                 (< level (gnus-summary-thread-level)))
10667       (forward-line way))
10668     (if (eobp)
10669         (progn
10670           (goto-char beg)
10671           nil)
10672       (setq beg (point))
10673       (prog1
10674           (gnus-summary-article-number)
10675         (goto-char beg)))))
10676
10677 (defun gnus-summary-next-thread (n &optional silent)
10678   "Go to the same level next N'th thread.
10679 If N is negative, search backward instead.
10680 Returns the difference between N and the number of skips actually
10681 done.
10682
10683 If SILENT, don't output messages."
10684   (interactive "p")
10685   (let ((backward (< n 0))
10686         (n (abs n)))
10687     (while (and (> n 0)
10688                 (gnus-summary-go-to-next-thread backward))
10689       (decf n))
10690     (unless silent
10691       (gnus-summary-position-point))
10692     (when (and (not silent) (/= 0 n))
10693       (gnus-message 7 "No more threads"))
10694     n))
10695
10696 (defun gnus-summary-prev-thread (n)
10697   "Go to the same level previous N'th thread.
10698 Returns the difference between N and the number of skips actually
10699 done."
10700   (interactive "p")
10701   (gnus-summary-next-thread (- n)))
10702
10703 (defun gnus-summary-go-down-thread ()
10704   "Go down one level in the current thread."
10705   (let ((children (gnus-summary-article-children)))
10706     (when children
10707       (gnus-summary-goto-subject (car children)))))
10708
10709 (defun gnus-summary-go-up-thread ()
10710   "Go up one level in the current thread."
10711   (let ((parent (gnus-summary-article-parent)))
10712     (when parent
10713       (gnus-summary-goto-subject parent))))
10714
10715 (defun gnus-summary-down-thread (n)
10716   "Go down thread N steps.
10717 If N is negative, go up instead.
10718 Returns the difference between N and how many steps down that were
10719 taken."
10720   (interactive "p")
10721   (let ((up (< n 0))
10722         (n (abs n)))
10723     (while (and (> n 0)
10724                 (if up (gnus-summary-go-up-thread)
10725                   (gnus-summary-go-down-thread)))
10726       (setq n (1- n)))
10727     (gnus-summary-position-point)
10728     (when (/= 0 n)
10729       (gnus-message 7 "Can't go further"))
10730     n))
10731
10732 (defun gnus-summary-up-thread (n)
10733   "Go up thread N steps.
10734 If N is negative, go down instead.
10735 Returns the difference between N and how many steps down that were
10736 taken."
10737   (interactive "p")
10738   (gnus-summary-down-thread (- n)))
10739
10740 (defun gnus-summary-top-thread ()
10741   "Go to the top of the thread."
10742   (interactive)
10743   (while (gnus-summary-go-up-thread))
10744   (gnus-summary-article-number))
10745
10746 (defun gnus-summary-kill-thread (&optional unmark)
10747   "Mark articles under current thread as read.
10748 If the prefix argument is positive, remove any kinds of marks.
10749 If the prefix argument is negative, tick articles instead."
10750   (interactive "P")
10751   (when unmark
10752     (setq unmark (prefix-numeric-value unmark)))
10753   (let ((articles (gnus-summary-articles-in-thread)))
10754     (save-excursion
10755       ;; Expand the thread.
10756       (gnus-summary-show-thread)
10757       ;; Mark all the articles.
10758       (while articles
10759         (gnus-summary-goto-subject (car articles))
10760         (cond ((null unmark)
10761                (gnus-summary-mark-article-as-read gnus-killed-mark))
10762               ((> unmark 0)
10763                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10764               (t
10765                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10766         (setq articles (cdr articles))))
10767     ;; Hide killed subtrees.
10768     (and (null unmark)
10769          gnus-thread-hide-killed
10770          (gnus-summary-hide-thread))
10771     ;; If marked as read, go to next unread subject.
10772     (when (null unmark)
10773       ;; Go to next unread subject.
10774       (gnus-summary-next-subject 1 t)))
10775   (gnus-set-mode-line 'summary))
10776
10777 ;; Summary sorting commands
10778
10779 (defun gnus-summary-sort-by-number (&optional reverse)
10780   "Sort the summary buffer by article number.
10781 Argument REVERSE means reverse order."
10782   (interactive "P")
10783   (gnus-summary-sort 'number reverse))
10784
10785 (defun gnus-summary-sort-by-random (&optional reverse)
10786   "Randomize the order in the summary buffer.
10787 Argument REVERSE means to randomize in reverse order."
10788   (interactive "P")
10789   (gnus-summary-sort 'random reverse))
10790
10791 (defun gnus-summary-sort-by-author (&optional reverse)
10792   "Sort the summary buffer by author name alphabetically.
10793 If `case-fold-search' is non-nil, case of letters is ignored.
10794 Argument REVERSE means reverse order."
10795   (interactive "P")
10796   (gnus-summary-sort 'author reverse))
10797
10798 (defun gnus-summary-sort-by-subject (&optional reverse)
10799   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10800 If `case-fold-search' is non-nil, case of letters is ignored.
10801 Argument REVERSE means reverse order."
10802   (interactive "P")
10803   (gnus-summary-sort 'subject reverse))
10804
10805 (defun gnus-summary-sort-by-date (&optional reverse)
10806   "Sort the summary buffer by date.
10807 Argument REVERSE means reverse order."
10808   (interactive "P")
10809   (gnus-summary-sort 'date reverse))
10810
10811 (defun gnus-summary-sort-by-score (&optional reverse)
10812   "Sort the summary buffer by score.
10813 Argument REVERSE means reverse order."
10814   (interactive "P")
10815   (gnus-summary-sort 'score reverse))
10816
10817 (defun gnus-summary-sort-by-lines (&optional reverse)
10818   "Sort the summary buffer by the number of lines.
10819 Argument REVERSE means reverse order."
10820   (interactive "P")
10821   (gnus-summary-sort 'lines reverse))
10822
10823 (defun gnus-summary-sort-by-chars (&optional reverse)
10824   "Sort the summary buffer by article length.
10825 Argument REVERSE means reverse order."
10826   (interactive "P")
10827   (gnus-summary-sort 'chars reverse))
10828
10829 (defun gnus-summary-sort-by-original (&optional reverse)
10830   "Sort the summary buffer using the default sorting method.
10831 Argument REVERSE means reverse order."
10832   (interactive "P")
10833   (let* ((buffer-read-only)
10834          (gnus-summary-prepare-hook nil))
10835     ;; We do the sorting by regenerating the threads.
10836     (gnus-summary-prepare)
10837     ;; Hide subthreads if needed.
10838     (gnus-summary-maybe-hide-threads)))
10839
10840 (defun gnus-summary-sort (predicate reverse)
10841   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10842   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10843          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10844          (gnus-thread-sort-functions
10845           (if (not reverse)
10846               thread
10847             `(lambda (t1 t2)
10848                (,thread t2 t1))))
10849          (gnus-sort-gathered-threads-function
10850           gnus-thread-sort-functions)
10851          (gnus-article-sort-functions
10852           (if (not reverse)
10853               article
10854             `(lambda (t1 t2)
10855                (,article t2 t1))))
10856          (buffer-read-only)
10857          (gnus-summary-prepare-hook nil))
10858     ;; We do the sorting by regenerating the threads.
10859     (gnus-summary-prepare)
10860     ;; Hide subthreads if needed.
10861     (gnus-summary-maybe-hide-threads)))
10862
10863 ;; Summary saving commands.
10864
10865 (defun gnus-summary-save-article (&optional n not-saved)
10866   "Save the current article using the default saver function.
10867 If N is a positive number, save the N next articles.
10868 If N is a negative number, save the N previous articles.
10869 If N is nil and any articles have been marked with the process mark,
10870 save those articles instead.
10871 The variable `gnus-default-article-saver' specifies the saver function."
10872   (interactive "P")
10873   (let* ((articles (gnus-summary-work-articles n))
10874          (save-buffer (save-excursion
10875                         (nnheader-set-temp-buffer " *Gnus Save*")))
10876          (num (length articles))
10877          header file)
10878     (dolist (article articles)
10879       (setq header (gnus-summary-article-header article))
10880       (if (not (vectorp header))
10881           ;; This is a pseudo-article.
10882           (if (assq 'name header)
10883               (gnus-copy-file (cdr (assq 'name header)))
10884             (gnus-message 1 "Article %d is unsaveable" article))
10885         ;; This is a real article.
10886         (save-window-excursion
10887           (let ((gnus-display-mime-function nil)
10888                 (gnus-article-prepare-hook nil))
10889             (gnus-summary-select-article t nil nil article)))
10890         (save-excursion
10891           (set-buffer save-buffer)
10892           (erase-buffer)
10893           (insert-buffer-substring gnus-original-article-buffer))
10894         (setq file (gnus-article-save save-buffer file num))
10895         (gnus-summary-remove-process-mark article)
10896         (unless not-saved
10897           (gnus-summary-set-saved-mark article))))
10898     (gnus-kill-buffer save-buffer)
10899     (gnus-summary-position-point)
10900     (gnus-set-mode-line 'summary)
10901     n))
10902
10903 (defun gnus-summary-pipe-output (&optional arg headers)
10904   "Pipe the current article to a subprocess.
10905 If N is a positive number, pipe the N next articles.
10906 If N is a negative number, pipe the N previous articles.
10907 If N is nil and any articles have been marked with the process mark,
10908 pipe those articles instead.
10909 If HEADERS (the symbolic prefix), include the headers, too."
10910   (interactive (gnus-interactive "P\ny"))
10911   (require 'gnus-art)
10912   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10913         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10914     (gnus-summary-save-article arg t))
10915   (let ((buffer (get-buffer "*Shell Command Output*")))
10916     (when (and buffer
10917                (not (zerop (buffer-size buffer))))
10918       (gnus-configure-windows 'pipe))))
10919
10920 (defun gnus-summary-save-article-mail (&optional arg)
10921   "Append the current article to an mail file.
10922 If N is a positive number, save the N next articles.
10923 If N is a negative number, save the N previous articles.
10924 If N is nil and any articles have been marked with the process mark,
10925 save those articles instead."
10926   (interactive "P")
10927   (require 'gnus-art)
10928   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10929     (gnus-summary-save-article arg)))
10930
10931 (defun gnus-summary-save-article-rmail (&optional arg)
10932   "Append the current article to an rmail file.
10933 If N is a positive number, save the N next articles.
10934 If N is a negative number, save the N previous articles.
10935 If N is nil and any articles have been marked with the process mark,
10936 save those articles instead."
10937   (interactive "P")
10938   (require 'gnus-art)
10939   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10940     (gnus-summary-save-article arg)))
10941
10942 (defun gnus-summary-save-article-file (&optional arg)
10943   "Append the current article to a file.
10944 If N is a positive number, save the N next articles.
10945 If N is a negative number, save the N previous articles.
10946 If N is nil and any articles have been marked with the process mark,
10947 save those articles instead."
10948   (interactive "P")
10949   (require 'gnus-art)
10950   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10951     (gnus-summary-save-article arg)))
10952
10953 (defun gnus-summary-write-article-file (&optional arg)
10954   "Write the current article to a file, deleting the previous file.
10955 If N is a positive number, save the N next articles.
10956 If N is a negative number, save the N previous articles.
10957 If N is nil and any articles have been marked with the process mark,
10958 save those articles instead."
10959   (interactive "P")
10960   (require 'gnus-art)
10961   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10962     (gnus-summary-save-article arg)))
10963
10964 (defun gnus-summary-save-article-body-file (&optional arg)
10965   "Append the current article body to a file.
10966 If N is a positive number, save the N next articles.
10967 If N is a negative number, save the N previous articles.
10968 If N is nil and any articles have been marked with the process mark,
10969 save those articles instead."
10970   (interactive "P")
10971   (require 'gnus-art)
10972   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10973     (gnus-summary-save-article arg)))
10974
10975 (defun gnus-summary-muttprint (&optional arg)
10976   "Print the current article using Muttprint.
10977 If N is a positive number, save the N next articles.
10978 If N is a negative number, save the N previous articles.
10979 If N is nil and any articles have been marked with the process mark,
10980 save those articles instead."
10981   (interactive "P")
10982   (require 'gnus-art)
10983   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10984     (gnus-summary-save-article arg t)))
10985
10986 (defun gnus-summary-pipe-message (program)
10987   "Pipe the current article through PROGRAM."
10988   (interactive "sProgram: ")
10989   (gnus-summary-select-article)
10990   (let ((mail-header-separator ""))
10991     (gnus-eval-in-buffer-window gnus-article-buffer
10992       (save-restriction
10993         (widen)
10994         (let ((start (window-start))
10995               buffer-read-only)
10996           (message-pipe-buffer-body program)
10997           (set-window-start (get-buffer-window (current-buffer)) start))))))
10998
10999 (defun gnus-get-split-value (methods)
11000   "Return a value based on the split METHODS."
11001   (let (split-name method result match)
11002     (when methods
11003       (save-excursion
11004         (set-buffer gnus-original-article-buffer)
11005         (save-restriction
11006           (nnheader-narrow-to-headers)
11007           (while (and methods (not split-name))
11008             (goto-char (point-min))
11009             (setq method (pop methods))
11010             (setq match (car method))
11011             (when (cond
11012                    ((stringp match)
11013                     ;; Regular expression.
11014                     (ignore-errors
11015                       (re-search-forward match nil t)))
11016                    ((functionp match)
11017                     ;; Function.
11018                     (save-restriction
11019                       (widen)
11020                       (setq result (funcall match gnus-newsgroup-name))))
11021                    ((consp match)
11022                     ;; Form.
11023                     (save-restriction
11024                       (widen)
11025                       (setq result (eval match)))))
11026               (setq split-name (cdr method))
11027               (cond ((stringp result)
11028                      (push (expand-file-name
11029                             result gnus-article-save-directory)
11030                            split-name))
11031                     ((consp result)
11032                      (setq split-name (append result split-name)))))))))
11033     (nreverse split-name)))
11034
11035 (defun gnus-valid-move-group-p (group)
11036   (and (boundp group)
11037        (symbol-name group)
11038        (symbol-value group)
11039        (gnus-get-function (gnus-find-method-for-group
11040                            (symbol-name group)) 'request-accept-article t)))
11041
11042 (defun gnus-read-move-group-name (prompt default articles prefix)
11043   "Read a group name."
11044   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11045          (minibuffer-confirm-incomplete nil) ; XEmacs
11046          (prom
11047           (format "%s %s to:"
11048                   prompt
11049                   (if (> (length articles) 1)
11050                       (format "these %d articles" (length articles))
11051                     "this article")))
11052          (to-newsgroup
11053           (cond
11054            ((null split-name)
11055             (gnus-completing-read-with-default
11056              default prom
11057              gnus-active-hashtb
11058              'gnus-valid-move-group-p
11059              nil prefix
11060              'gnus-group-history))
11061            ((= 1 (length split-name))
11062             (gnus-completing-read-with-default
11063              (car split-name) prom
11064              gnus-active-hashtb
11065              'gnus-valid-move-group-p
11066              nil nil
11067              'gnus-group-history))
11068            (t
11069             (gnus-completing-read-with-default
11070              nil prom
11071              (mapcar 'list (nreverse split-name))
11072              nil nil nil
11073              'gnus-group-history))))
11074          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11075     (when to-newsgroup
11076       (if (or (string= to-newsgroup "")
11077               (string= to-newsgroup prefix))
11078           (setq to-newsgroup default))
11079       (unless to-newsgroup
11080         (error "No group name entered"))
11081       (or (gnus-active to-newsgroup)
11082           (gnus-activate-group to-newsgroup nil nil to-method)
11083           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11084                                      to-newsgroup))
11085               (or (and (gnus-request-create-group to-newsgroup to-method)
11086                        (gnus-activate-group
11087                         to-newsgroup nil nil to-method)
11088                        (gnus-subscribe-group to-newsgroup))
11089                   (error "Couldn't create group %s" to-newsgroup)))
11090           (error "No such group: %s" to-newsgroup)))
11091     to-newsgroup))
11092
11093 (defun gnus-summary-save-parts (type dir n &optional reverse)
11094   "Save parts matching TYPE to DIR.
11095 If REVERSE, save parts that do not match TYPE."
11096   (interactive
11097    (list (read-string "Save parts of type: "
11098                       (or (car gnus-summary-save-parts-type-history)
11099                           gnus-summary-save-parts-default-mime)
11100                       'gnus-summary-save-parts-type-history)
11101          (setq gnus-summary-save-parts-last-directory
11102                (read-file-name "Save to directory: "
11103                                gnus-summary-save-parts-last-directory
11104                                nil t))
11105          current-prefix-arg))
11106   (gnus-summary-iterate n
11107     (let ((gnus-display-mime-function nil)
11108           (gnus-inhibit-treatment t))
11109       (gnus-summary-select-article))
11110     (save-excursion
11111       (set-buffer gnus-article-buffer)
11112       (let ((handles (or gnus-article-mime-handles
11113                          (mm-dissect-buffer nil gnus-article-loose-mime)
11114                          (and gnus-article-emulate-mime
11115                               (mm-uu-dissect)))))
11116         (when handles
11117           (gnus-summary-save-parts-1 type dir handles reverse)
11118           (unless gnus-article-mime-handles ;; Don't destroy this case.
11119             (mm-destroy-parts handles)))))))
11120
11121 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11122   (if (stringp (car handle))
11123       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11124               (cdr handle))
11125     (when (if reverse
11126               (not (string-match type (mm-handle-media-type handle)))
11127             (string-match type (mm-handle-media-type handle)))
11128       (let ((file (expand-file-name
11129                    (gnus-map-function
11130                     mm-file-name-rewrite-functions
11131                     (file-name-nondirectory
11132                      (or
11133                       (mail-content-type-get
11134                        (mm-handle-disposition handle) 'filename)
11135                       (mail-content-type-get
11136                        (mm-handle-type handle) 'name)
11137                       (concat gnus-newsgroup-name
11138                               "." (number-to-string
11139                                    (cdr gnus-article-current))))))
11140                    dir)))
11141         (unless (file-exists-p file)
11142           (mm-save-part-to-file handle file))))))
11143
11144 ;; Summary extract commands
11145
11146 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11147   (let ((buffer-read-only nil)
11148         (article (gnus-summary-article-number))
11149         after-article b e)
11150     (unless (gnus-summary-goto-subject article)
11151       (error "No such article: %d" article))
11152     (gnus-summary-position-point)
11153     ;; If all commands are to be bunched up on one line, we collect
11154     ;; them here.
11155     (unless gnus-view-pseudos-separately
11156       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11157             files action)
11158         (while ps
11159           (setq action (cdr (assq 'action (car ps))))
11160           (setq files (list (cdr (assq 'name (car ps)))))
11161           (while (and ps (cdr ps)
11162                       (string= (or action "1")
11163                                (or (cdr (assq 'action (cadr ps))) "2")))
11164             (push (cdr (assq 'name (cadr ps))) files)
11165             (setcdr ps (cddr ps)))
11166           (when files
11167             (when (not (string-match "%s" action))
11168               (push " " files))
11169             (push " " files)
11170             (when (assq 'execute (car ps))
11171               (setcdr (assq 'execute (car ps))
11172                       (funcall (if (string-match "%s" action)
11173                                    'format 'concat)
11174                                action
11175                                (mapconcat
11176                                 (lambda (f)
11177                                   (if (equal f " ")
11178                                       f
11179                                     (shell-quote-argument f)))
11180                                 files " ")))))
11181           (setq ps (cdr ps)))))
11182     (if (and gnus-view-pseudos (not not-view))
11183         (while pslist
11184           (when (assq 'execute (car pslist))
11185             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11186                                   (eq gnus-view-pseudos 'not-confirm)))
11187           (setq pslist (cdr pslist)))
11188       (save-excursion
11189         (while pslist
11190           (setq after-article (or (cdr (assq 'article (car pslist)))
11191                                   (gnus-summary-article-number)))
11192           (gnus-summary-goto-subject after-article)
11193           (forward-line 1)
11194           (setq b (point))
11195           (insert "    " (file-name-nondirectory
11196                           (cdr (assq 'name (car pslist))))
11197                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11198           (setq e (point))
11199           (forward-line -1)             ; back to `b'
11200           (gnus-add-text-properties
11201            b (1- e) (list 'gnus-number gnus-reffed-article-number
11202                           gnus-mouse-face-prop gnus-mouse-face))
11203           (gnus-data-enter
11204            after-article gnus-reffed-article-number
11205            gnus-unread-mark b (car pslist) 0 (- e b))
11206           (setq gnus-newsgroup-unreads
11207                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11208                                          gnus-reffed-article-number))
11209           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11210           (setq pslist (cdr pslist)))))))
11211
11212 (defun gnus-pseudos< (p1 p2)
11213   (let ((c1 (cdr (assq 'action p1)))
11214         (c2 (cdr (assq 'action p2))))
11215     (and c1 c2 (string< c1 c2))))
11216
11217 (defun gnus-request-pseudo-article (props)
11218   (cond ((assq 'execute props)
11219          (gnus-execute-command (cdr (assq 'execute props)))))
11220   (let ((gnus-current-article (gnus-summary-article-number)))
11221     (gnus-run-hooks 'gnus-mark-article-hook)))
11222
11223 (defun gnus-execute-command (command &optional automatic)
11224   (save-excursion
11225     (gnus-article-setup-buffer)
11226     (set-buffer gnus-article-buffer)
11227     (setq buffer-read-only nil)
11228     (let ((command (if automatic command
11229                      (read-string "Command: " (cons command 0)))))
11230       (erase-buffer)
11231       (insert "$ " command "\n\n")
11232       (if gnus-view-pseudo-asynchronously
11233           (start-process "gnus-execute" (current-buffer) shell-file-name
11234                          shell-command-switch command)
11235         (call-process shell-file-name nil t nil
11236                       shell-command-switch command)))))
11237
11238 ;; Summary kill commands.
11239
11240 (defun gnus-summary-edit-global-kill (article)
11241   "Edit the \"global\" kill file."
11242   (interactive (list (gnus-summary-article-number)))
11243   (gnus-group-edit-global-kill article))
11244
11245 (defun gnus-summary-edit-local-kill ()
11246   "Edit a local kill file applied to the current newsgroup."
11247   (interactive)
11248   (setq gnus-current-headers (gnus-summary-article-header))
11249   (gnus-group-edit-local-kill
11250    (gnus-summary-article-number) gnus-newsgroup-name))
11251
11252 ;;; Header reading.
11253
11254 (defun gnus-read-header (id &optional header)
11255   "Read the headers of article ID and enter them into the Gnus system."
11256   (let ((group gnus-newsgroup-name)
11257         (gnus-override-method
11258          (or
11259           gnus-override-method
11260           (and (gnus-news-group-p gnus-newsgroup-name)
11261                (car (gnus-refer-article-methods)))))
11262         where)
11263     ;; First we check to see whether the header in question is already
11264     ;; fetched.
11265     (if (stringp id)
11266         ;; This is a Message-ID.
11267         (setq header (or header (gnus-id-to-header id)))
11268       ;; This is an article number.
11269       (setq header (or header (gnus-summary-article-header id))))
11270     (if (and header
11271              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11272         ;; We have found the header.
11273         header
11274       ;; We have to really fetch the header to this article.
11275       (save-excursion
11276         (set-buffer nntp-server-buffer)
11277         (when (setq where (gnus-request-head id group))
11278           (nnheader-fold-continuation-lines)
11279           (goto-char (point-max))
11280           (insert ".\n")
11281           (goto-char (point-min))
11282           (insert "211 ")
11283           (princ (cond
11284                   ((numberp id) id)
11285                   ((cdr where) (cdr where))
11286                   (header (mail-header-number header))
11287                   (t gnus-reffed-article-number))
11288                  (current-buffer))
11289           (insert " Article retrieved.\n"))
11290         (if (or (not where)
11291                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11292             ()                          ; Malformed head.
11293           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11294             (when (and (stringp id)
11295                        (not (string= (gnus-group-real-name group)
11296                                      (car where))))
11297               ;; If we fetched by Message-ID and the article came
11298               ;; from a different group, we fudge some bogus article
11299               ;; numbers for this article.
11300               (mail-header-set-number header gnus-reffed-article-number))
11301             (save-excursion
11302               (set-buffer gnus-summary-buffer)
11303               (decf gnus-reffed-article-number)
11304               (gnus-remove-header (mail-header-number header))
11305               (push header gnus-newsgroup-headers)
11306               (setq gnus-current-headers header)
11307               (push (mail-header-number header) gnus-newsgroup-limit)))
11308           header)))))
11309
11310 (defun gnus-remove-header (number)
11311   "Remove header NUMBER from `gnus-newsgroup-headers'."
11312   (if (and gnus-newsgroup-headers
11313            (= number (mail-header-number (car gnus-newsgroup-headers))))
11314       (pop gnus-newsgroup-headers)
11315     (let ((headers gnus-newsgroup-headers))
11316       (while (and (cdr headers)
11317                   (not (= number (mail-header-number (cadr headers)))))
11318         (pop headers))
11319       (when (cdr headers)
11320         (setcdr headers (cddr headers))))))
11321
11322 ;;;
11323 ;;; summary highlights
11324 ;;;
11325
11326 (defun gnus-highlight-selected-summary ()
11327   "Highlight selected article in summary buffer."
11328   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11329   (when gnus-summary-selected-face
11330     (save-excursion
11331       (let* ((beg (point-at-bol))
11332              (end (point-at-eol))
11333              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11334              (from (if (get-text-property beg gnus-mouse-face-prop)
11335                        beg
11336                      (or (next-single-property-change
11337                           beg gnus-mouse-face-prop nil end)
11338                          beg)))
11339              (to
11340               (if (= from end)
11341                   (- from 2)
11342                 (or (next-single-property-change
11343                      from gnus-mouse-face-prop nil end)
11344                     end))))
11345         ;; If no mouse-face prop on line we will have to = from = end,
11346         ;; so we highlight the entire line instead.
11347         (when (= (+ to 2) from)
11348           (setq from beg)
11349           (setq to end))
11350         (if gnus-newsgroup-selected-overlay
11351             ;; Move old overlay.
11352             (gnus-move-overlay
11353              gnus-newsgroup-selected-overlay from to (current-buffer))
11354           ;; Create new overlay.
11355           (gnus-overlay-put
11356            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11357            'face gnus-summary-selected-face))))))
11358
11359 (defvar gnus-summary-highlight-line-cached nil)
11360 (defvar gnus-summary-highlight-line-trigger nil)
11361
11362 (defun gnus-summary-highlight-line-0 ()
11363   (if (and (eq gnus-summary-highlight-line-trigger
11364                gnus-summary-highlight)
11365            gnus-summary-highlight-line-cached)
11366       gnus-summary-highlight-line-cached
11367     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11368           gnus-summary-highlight-line-cached
11369           (let* ((cond (list 'cond))
11370                  (c cond)
11371                  (list gnus-summary-highlight))
11372             (while list
11373               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11374                               nil))
11375               (setq c (cdr c)
11376                     list (cdr list)))
11377             (gnus-byte-compile (list 'lambda nil cond))))))
11378
11379 (defun gnus-summary-highlight-line ()
11380   "Highlight current line according to `gnus-summary-highlight'."
11381   (let* ((beg (point-at-bol))
11382          (article (or (gnus-summary-article-number) gnus-current-article))
11383          (score (or (cdr (assq article
11384                                gnus-newsgroup-scored))
11385                     gnus-summary-default-score 0))
11386          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11387          (inhibit-read-only t)
11388          (default gnus-summary-default-score)
11389          (default-high gnus-summary-default-high-score)
11390          (default-low gnus-summary-default-low-score)
11391          (uncached (and gnus-summary-use-undownloaded-faces
11392                         (memq article gnus-newsgroup-undownloaded)
11393                         (not (memq article gnus-newsgroup-cached)))))
11394     (let ((face (funcall (gnus-summary-highlight-line-0))))
11395       (unless (eq face (get-text-property beg 'face))
11396         (gnus-put-text-property-excluding-characters-with-faces
11397          beg (point-at-eol) 'face
11398          (setq face (if (boundp face) (symbol-value face) face)))
11399         (when gnus-summary-highlight-line-function
11400           (funcall gnus-summary-highlight-line-function article face))))))
11401
11402 (defun gnus-update-read-articles (group unread &optional compute)
11403   "Update the list of read articles in GROUP.
11404 UNREAD is a sorted list."
11405   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11406         (info (gnus-get-info group))
11407         (prev 1)
11408         read)
11409     (if (or (not info) (not active))
11410         ;; There is no info on this group if it was, in fact,
11411         ;; killed.  Gnus stores no information on killed groups, so
11412         ;; there's nothing to be done.
11413         ;; One could store the information somewhere temporarily,
11414         ;; perhaps...  Hmmm...
11415         ()
11416       ;; Remove any negative articles numbers.
11417       (while (and unread (< (car unread) 0))
11418         (setq unread (cdr unread)))
11419       ;; Remove any expired article numbers
11420       (while (and unread (< (car unread) (car active)))
11421         (setq unread (cdr unread)))
11422       ;; Compute the ranges of read articles by looking at the list of
11423       ;; unread articles.
11424       (while unread
11425         (when (/= (car unread) prev)
11426           (push (if (= prev (1- (car unread))) prev
11427                   (cons prev (1- (car unread))))
11428                 read))
11429         (setq prev (1+ (car unread)))
11430         (setq unread (cdr unread)))
11431       (when (<= prev (cdr active))
11432         (push (cons prev (cdr active)) read))
11433       (setq read (if (> (length read) 1) (nreverse read) read))
11434       (if compute
11435           read
11436         (save-excursion
11437           (let (setmarkundo)
11438             ;; Propagate the read marks to the backend.
11439             (when (gnus-check-backend-function 'request-set-mark group)
11440               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11441                     (add (gnus-remove-from-range read (gnus-info-read info))))
11442                 (when (or add del)
11443                   (unless (gnus-check-group group)
11444                     (error "Can't open server for %s" group))
11445                   (gnus-request-set-mark
11446                    group (delq nil (list (if add (list add 'add '(read)))
11447                                          (if del (list del 'del '(read))))))
11448                   (setq setmarkundo
11449                         `(gnus-request-set-mark
11450                           ,group
11451                           ',(delq nil (list
11452                                        (if del (list del 'add '(read)))
11453                                        (if add (list add 'del '(read))))))))))
11454             (set-buffer gnus-group-buffer)
11455             (gnus-undo-register
11456               `(progn
11457                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11458                  (gnus-info-set-read ',info ',(gnus-info-read info))
11459                  (gnus-get-unread-articles-in-group ',info
11460                                                     (gnus-active ,group))
11461                  (gnus-group-update-group ,group t)
11462                  ,setmarkundo))))
11463         ;; Enter this list into the group info.
11464         (gnus-info-set-read info read)
11465         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11466         (gnus-get-unread-articles-in-group info (gnus-active group))
11467         t))))
11468
11469 (defun gnus-offer-save-summaries ()
11470   "Offer to save all active summary buffers."
11471   (let (buffers)
11472     ;; Go through all buffers and find all summaries.
11473     (dolist (buffer (buffer-list))
11474       (when (and (setq buffer (buffer-name buffer))
11475                  (string-match "Summary" buffer)
11476                  (with-current-buffer buffer
11477                    ;; We check that this is, indeed, a summary buffer.
11478                    (and (eq major-mode 'gnus-summary-mode)
11479                         ;; Also make sure this isn't bogus.
11480                         gnus-newsgroup-prepared
11481                         ;; Also make sure that this isn't a
11482                         ;; dead summary buffer.
11483                         (not gnus-dead-summary-mode))))
11484         (push buffer buffers)))
11485     ;; Go through all these summary buffers and offer to save them.
11486     (when buffers
11487       (save-excursion
11488         (map-y-or-n-p
11489          "Update summary buffer %s? "
11490          (lambda (buf)
11491            (switch-to-buffer buf)
11492            (gnus-summary-exit))
11493          buffers)))))
11494
11495 (defun gnus-summary-setup-default-charset ()
11496   "Setup newsgroup default charset."
11497   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11498       (setq gnus-newsgroup-charset nil)
11499     (let* ((ignored-charsets
11500             (or gnus-newsgroup-ephemeral-ignored-charsets
11501                 (append
11502                  (and gnus-newsgroup-name
11503                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11504                  gnus-newsgroup-ignored-charsets))))
11505       (setq gnus-newsgroup-charset
11506             (or gnus-newsgroup-ephemeral-charset
11507                 (and gnus-newsgroup-name
11508                      (gnus-parameter-charset gnus-newsgroup-name))
11509                 gnus-default-charset))
11510       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11511            ignored-charsets))))
11512
11513 ;;;
11514 ;;; Mime Commands
11515 ;;;
11516
11517 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11518   "Display the current article buffer fully MIME-buttonized.
11519 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11520 treated as multipart/mixed."
11521   (interactive "P")
11522   (require 'gnus-art)
11523   (let ((gnus-unbuttonized-mime-types nil)
11524         (gnus-mime-display-multipart-as-mixed show-all-parts))
11525     (gnus-summary-show-article)))
11526
11527 (defun gnus-summary-repair-multipart (article)
11528   "Add a Content-Type header to a multipart article without one."
11529   (interactive (list (gnus-summary-article-number)))
11530   (gnus-with-article article
11531     (message-narrow-to-head)
11532     (message-remove-header "Mime-Version")
11533     (goto-char (point-max))
11534     (insert "Mime-Version: 1.0\n")
11535     (widen)
11536     (when (search-forward "\n--" nil t)
11537       (let ((separator (buffer-substring (point) (point-at-eol))))
11538         (message-narrow-to-head)
11539         (message-remove-header "Content-Type")
11540         (goto-char (point-max))
11541         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11542                         separator))
11543         (widen))))
11544   (let (gnus-mark-article-hook)
11545     (gnus-summary-select-article t t nil article)))
11546
11547 (defun gnus-summary-toggle-display-buttonized ()
11548   "Toggle the buttonizing of the article buffer."
11549   (interactive)
11550   (require 'gnus-art)
11551   (if (setq gnus-inhibit-mime-unbuttonizing
11552             (not gnus-inhibit-mime-unbuttonizing))
11553       (let ((gnus-unbuttonized-mime-types nil))
11554         (gnus-summary-show-article))
11555     (gnus-summary-show-article)))
11556
11557 ;;;
11558 ;;; Generic summary marking commands
11559 ;;;
11560
11561 (defvar gnus-summary-marking-alist
11562   '((read gnus-del-mark "d")
11563     (unread gnus-unread-mark "u")
11564     (ticked gnus-ticked-mark "!")
11565     (dormant gnus-dormant-mark "?")
11566     (expirable gnus-expirable-mark "e"))
11567   "An alist of names/marks/keystrokes.")
11568
11569 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11570 (defvar gnus-summary-mark-map)
11571
11572 (defun gnus-summary-make-all-marking-commands ()
11573   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11574   (dolist (elem gnus-summary-marking-alist)
11575     (apply 'gnus-summary-make-marking-command elem)))
11576
11577 (defun gnus-summary-make-marking-command (name mark keystroke)
11578   (let ((map (make-sparse-keymap)))
11579     (define-key gnus-summary-generic-mark-map keystroke map)
11580     (dolist (lway `((next "next" next nil "n")
11581                     (next-unread "next unread" next t "N")
11582                     (prev "previous" prev nil "p")
11583                     (prev-unread "previous unread" prev t "P")
11584                     (nomove "" nil nil ,keystroke)))
11585       (let ((func (gnus-summary-make-marking-command-1
11586                    mark (car lway) lway name)))
11587         (setq func (eval func))
11588         (define-key map (nth 4 lway) func)))))
11589
11590 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11591   `(defun ,(intern
11592             (format "gnus-summary-put-mark-as-%s%s"
11593                     name (if (eq way 'nomove)
11594                              ""
11595                            (concat "-" (symbol-name way)))))
11596      (n)
11597      ,(format
11598        "Mark the current article as %s%s.
11599 If N, the prefix, then repeat N times.
11600 If N is negative, move in reverse order.
11601 The difference between N and the actual number of articles marked is
11602 returned."
11603        name (cadr lway))
11604      (interactive "p")
11605      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11606
11607 (defun gnus-summary-generic-mark (n mark move unread)
11608   "Mark N articles with MARK."
11609   (unless (eq major-mode 'gnus-summary-mode)
11610     (error "This command can only be used in the summary buffer"))
11611   (gnus-summary-show-thread)
11612   (let ((nummove
11613          (cond
11614           ((eq move 'next) 1)
11615           ((eq move 'prev) -1)
11616           (t 0))))
11617     (if (zerop nummove)
11618         (setq n 1)
11619       (when (< n 0)
11620         (setq n (abs n)
11621               nummove (* -1 nummove))))
11622     (while (and (> n 0)
11623                 (gnus-summary-mark-article nil mark)
11624                 (zerop (gnus-summary-next-subject nummove unread t)))
11625       (setq n (1- n)))
11626     (when (/= 0 n)
11627       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11628     (gnus-summary-recenter)
11629     (gnus-summary-position-point)
11630     (gnus-set-mode-line 'summary)
11631     n))
11632
11633 (defun gnus-summary-insert-articles (articles)
11634   (when (setq articles
11635               (gnus-sorted-difference articles
11636                                       (mapcar (lambda (h)
11637                                                 (mail-header-number h))
11638                                               gnus-newsgroup-headers)))
11639     (setq gnus-newsgroup-headers
11640           (gnus-merge 'list
11641                       gnus-newsgroup-headers
11642                       (gnus-fetch-headers articles)
11643                       'gnus-article-sort-by-number))
11644     ;; Suppress duplicates?
11645     (when gnus-suppress-duplicates
11646       (gnus-dup-suppress-articles))
11647
11648     ;; We might want to build some more threads first.
11649     (when (and gnus-fetch-old-headers
11650                (eq gnus-headers-retrieved-by 'nov))
11651       (if (eq gnus-fetch-old-headers 'invisible)
11652           (gnus-build-all-threads)
11653         (gnus-build-old-threads)))
11654     ;; Let the Gnus agent mark articles as read.
11655     (when gnus-agent
11656       (gnus-agent-get-undownloaded-list))
11657     ;; Remove list identifiers from subject
11658     (when gnus-list-identifiers
11659       (gnus-summary-remove-list-identifiers))
11660     ;; First and last article in this newsgroup.
11661     (when gnus-newsgroup-headers
11662       (setq gnus-newsgroup-begin
11663             (mail-header-number (car gnus-newsgroup-headers))
11664             gnus-newsgroup-end
11665             (mail-header-number
11666              (gnus-last-element gnus-newsgroup-headers))))
11667     (when gnus-use-scoring
11668       (gnus-possibly-score-headers))))
11669
11670 (defun gnus-summary-insert-old-articles (&optional all)
11671   "Insert all old articles in this group.
11672 If ALL is non-nil, already read articles become readable.
11673 If ALL is a number, fetch this number of articles."
11674   (interactive "P")
11675   (prog1
11676       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11677             older len)
11678         (setq older
11679               ;; Some nntp servers lie about their active range.  When
11680               ;; this happens, the active range can be in the millions.
11681               ;; Use a compressed range to avoid creating a huge list.
11682               (gnus-range-difference (list gnus-newsgroup-active) old))
11683         (setq len (gnus-range-length older))
11684         (cond
11685          ((null older) nil)
11686          ((numberp all)
11687           (if (< all len)
11688               (let ((older-range (nreverse older)))
11689                 (setq older nil)
11690
11691                 (while (> all 0)
11692                   (let* ((r (pop older-range))
11693                          (min (if (numberp r) r (car r)))
11694                          (max (if (numberp r) r (cdr r))))
11695                     (while (and (<= min max)
11696                                 (> all 0))
11697                       (push max older)
11698                       (setq all (1- all)
11699                             max (1- max))))))
11700             (setq older (gnus-uncompress-range older))))
11701          (all
11702           (setq older (gnus-uncompress-range older)))
11703          (t
11704           (when (and (numberp gnus-large-newsgroup)
11705                    (> len gnus-large-newsgroup))
11706               (let* ((cursor-in-echo-area nil)
11707                      (initial (gnus-parameter-large-newsgroup-initial
11708                                gnus-newsgroup-name))
11709                      (input
11710                       (read-string
11711                        (format
11712                         "How many articles from %s (%s %d): "
11713                         (gnus-limit-string
11714                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11715                         (if initial "max" "default")
11716                         len)
11717                        (if initial
11718                            (cons (number-to-string initial)
11719                                  0)))))
11720                 (unless (string-match "^[ \t]*$" input)
11721                   (setq all (string-to-number input))
11722                   (if (< all len)
11723                       (let ((older-range (nreverse older)))
11724                         (setq older nil)
11725
11726                         (while (> all 0)
11727                           (let* ((r (pop older-range))
11728                                  (min (if (numberp r) r (car r)))
11729                                  (max (if (numberp r) r (cdr r))))
11730                             (while (and (<= min max)
11731                                         (> all 0))
11732                               (push max older)
11733                               (setq all (1- all)
11734                                     max (1- max))))))))))
11735           (setq older (gnus-uncompress-range older))))
11736         (if (not older)
11737             (message "No old news.")
11738           (gnus-summary-insert-articles older)
11739           (gnus-summary-limit (gnus-sorted-nunion old older))))
11740     (gnus-summary-position-point)))
11741
11742 (defun gnus-summary-insert-new-articles ()
11743   "Insert all new articles in this group."
11744   (interactive)
11745   (prog1
11746       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11747             (old-active gnus-newsgroup-active)
11748             (nnmail-fetched-sources (list t))
11749             i new)
11750         (setq gnus-newsgroup-active
11751               (gnus-activate-group gnus-newsgroup-name 'scan))
11752         (setq i (cdr gnus-newsgroup-active))
11753         (while (> i (cdr old-active))
11754           (push i new)
11755           (decf i))
11756         (if (not new)
11757             (message "No gnus is bad news")
11758           (gnus-summary-insert-articles new)
11759           (setq gnus-newsgroup-unreads
11760                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11761           (gnus-summary-limit (gnus-sorted-nunion old new))))
11762     (gnus-summary-position-point)))
11763
11764 (gnus-summary-make-all-marking-commands)
11765
11766 (gnus-ems-redefine)
11767
11768 (provide 'gnus-sum)
11769
11770 (run-hooks 'gnus-sum-load-hook)
11771
11772 ;; Local Variables:
11773 ;; coding: iso-8859-1
11774 ;; End:
11775
11776 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11777 ;;; gnus-sum.el ends here