Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-107
[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   :group 'gnus-thread
123   :type 'boolean)
124
125 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
126   "*A regexp to match subjects to be excluded from loose thread gathering.
127 As loose thread gathering is done on subjects only, that means that
128 there can be many false gatherings performed.  By rooting out certain
129 common subjects, gathering might become saner."
130   :group 'gnus-thread
131   :type 'regexp)
132
133 (defcustom gnus-summary-gather-subject-limit nil
134   "*Maximum length of subject comparisons when gathering loose threads.
135 Use nil to compare full subjects.  Setting this variable to a low
136 number will help gather threads that have been corrupted by
137 newsreaders chopping off subject lines, but it might also mean that
138 unrelated articles that have subject that happen to begin with the
139 same few characters will be incorrectly gathered.
140
141 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
142 comparing subjects."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  (const fuzzy)
146                  (sexp :menu-tag "on" t)))
147
148 (defcustom gnus-simplify-subject-functions nil
149   "List of functions taking a string argument that simplify subjects.
150 The functions are applied recursively.
151
152 Useful functions to put in this list include:
153 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
154 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
155   :group 'gnus-thread
156   :type '(repeat function))
157
158 (defcustom gnus-simplify-ignored-prefixes nil
159   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
160   :group 'gnus-thread
161   :type '(choice (const :tag "off" nil)
162                  regexp))
163
164 (defcustom gnus-build-sparse-threads nil
165   "*If non-nil, fill in the gaps in threads.
166 If `some', only fill in the gaps that are needed to tie loose threads
167 together.  If `more', fill in all leaf nodes that Gnus can find.  If
168 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
169   :group 'gnus-thread
170   :type '(choice (const :tag "off" nil)
171                  (const some)
172                  (const more)
173                  (sexp :menu-tag "all" t)))
174
175 (defcustom gnus-summary-thread-gathering-function
176   'gnus-gather-threads-by-subject
177   "*Function used for gathering loose threads.
178 There are two pre-defined functions: `gnus-gather-threads-by-subject',
179 which only takes Subjects into consideration; and
180 `gnus-gather-threads-by-references', which compared the References
181 headers of the articles to find matches."
182   :group 'gnus-thread
183   :type '(radio (function-item gnus-gather-threads-by-subject)
184                 (function-item gnus-gather-threads-by-references)
185                 (function :tag "other")))
186
187 (defcustom gnus-summary-same-subject ""
188   "*String indicating that the current article has the same subject as the previous.
189 This variable will only be used if the value of
190 `gnus-summary-make-false-root' is `empty'."
191   :group 'gnus-summary-format
192   :type 'string)
193
194 (defcustom gnus-summary-goto-unread t
195   "*If t, many commands will go to the next unread article.
196 This applies to marking commands as well as other commands that
197 \"naturally\" select the next article, like, for instance, `SPC' at
198 the end of an article.
199
200 If nil, the marking commands do NOT go to the next unread article
201 \(they go to the next article instead).  If `never', commands that
202 usually go to the next unread article, will go to the next article,
203 whether it is read or not."
204   :group 'gnus-summary-marks
205   :link '(custom-manual "(gnus)Setting Marks")
206   :type '(choice (const :tag "off" nil)
207                  (const never)
208                  (sexp :menu-tag "on" t)))
209
210 (defcustom gnus-summary-default-score 0
211   "*Default article score level.
212 All scores generated by the score files will be added to this score.
213 If this variable is nil, scoring will be disabled."
214   :group 'gnus-score-default
215   :type '(choice (const :tag "disable")
216                  integer))
217
218 (defcustom gnus-summary-default-high-score 0
219   "*Default threshold for a high scored article.
220 An article will be highlighted as high scored if its score is greater
221 than this score."
222   :group 'gnus-score-default
223   :type 'integer)
224
225 (defcustom gnus-summary-default-low-score 0
226   "*Default threshold for a low scored article.
227 An article will be highlighted as low scored if its score is smaller
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-zcore-fuzz 0
233   "*Fuzziness factor for the zcore in the summary buffer.
234 Articles with scores closer than this to `gnus-summary-default-score'
235 will not be marked."
236   :group 'gnus-summary-format
237   :type 'integer)
238
239 (defcustom gnus-simplify-subject-fuzzy-regexp nil
240   "*Strings to be removed when doing fuzzy matches.
241 This can either be a regular expression or list of regular expressions
242 that will be removed from subject strings if fuzzy subject
243 simplification is selected."
244   :group 'gnus-thread
245   :type '(repeat regexp))
246
247 (defcustom gnus-show-threads t
248   "*If non-nil, display threads in summary mode."
249   :group 'gnus-thread
250   :type 'boolean)
251
252 (defcustom gnus-thread-hide-subtree nil
253   "*If non-nil, hide all threads initially.
254 This can be a predicate specifier which says which threads to hide.
255 If threads are hidden, you have to run the command
256 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
257 to expose hidden threads."
258   :group 'gnus-thread
259   :type '(radio (sexp :format "Non-nil\n"
260                       :match (lambda (widget value)
261                                (not (or (consp value) (functionp value))))
262                       :value t)
263                 (const nil)
264                 (sexp :tag "Predicate specifier" :size 0)))
265
266 (defcustom gnus-thread-hide-killed t
267   "*If non-nil, hide killed threads automatically."
268   :group 'gnus-thread
269   :type 'boolean)
270
271 (defcustom gnus-thread-ignore-subject t
272   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
273 If nil, articles that have different subjects from their parents will
274 start separate threads."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-operation-ignore-subject t
279   "*If non-nil, subjects will be ignored when doing thread commands.
280 This affects commands like `gnus-summary-kill-thread' and
281 `gnus-summary-lower-thread'.
282
283 If this variable is nil, articles in the same thread with different
284 subjects will not be included in the operation in question.  If this
285 variable is `fuzzy', only articles that have subjects that are fuzzily
286 equal will be included."
287   :group 'gnus-thread
288   :type '(choice (const :tag "off" nil)
289                  (const fuzzy)
290                  (sexp :tag "on" t)))
291
292 (defcustom gnus-thread-indent-level 4
293   "*Number that says how much each sub-thread should be indented."
294   :group 'gnus-thread
295   :type 'integer)
296
297 (defcustom gnus-auto-extend-newsgroup t
298   "*If non-nil, extend newsgroup forward and backward when requested."
299   :group 'gnus-summary-choose
300   :type 'boolean)
301
302 (defcustom gnus-auto-select-first t
303   "*If non-nil, select the article under point.
304 Which article this is is controlled by the `gnus-auto-select-subject'
305 variable.
306
307 If you want to prevent automatic selection of articles in some
308 newsgroups, set the variable to nil in `gnus-select-group-hook'."
309   :group 'gnus-group-select
310   :type '(choice (const :tag "none" nil)
311                  (sexp :menu-tag "first" t)))
312
313 (defcustom gnus-auto-select-subject 'unread
314   "*Says what subject to place under point when entering a group.
315
316 This variable can either be the symbols `first' (place point on the
317 first subject), `unread' (place point on the subject line of the first
318 unread article), `best' (place point on the subject line of the
319 higest-scored article), `unseen' (place point on the subject line of
320 the first unseen article), 'unseen-or-unread' (place point on the subject
321 line of the first unseen article or, if all article have been seen, on the
322 subject line of the first unread article), or a function to be called to
323 place point on some subject line."
324   :group 'gnus-group-select
325   :type '(choice (const best)
326                  (const unread)
327                  (const first)
328                  (const unseen)
329                  (const unseen-or-unread)))
330
331 (defcustom gnus-auto-select-next t
332   "*If non-nil, offer to go to the next group from the end of the previous.
333 If the value is t and the next newsgroup is empty, Gnus will exit
334 summary mode and go back to group mode.  If the value is neither nil
335 nor t, Gnus will select the following unread newsgroup.  In
336 particular, if the value is the symbol `quietly', the next unread
337 newsgroup will be selected without any confirmation, and if it is
338 `almost-quietly', the next group will be selected without any
339 confirmation if you are located on the last article in the group.
340 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
341 will go to the next group without confirmation."
342   :group 'gnus-summary-maneuvering
343   :type '(choice (const :tag "off" nil)
344                  (const quietly)
345                  (const almost-quietly)
346                  (const slightly-quietly)
347                  (sexp :menu-tag "on" t)))
348
349 (defcustom gnus-auto-select-same nil
350   "*If non-nil, select the next article with the same subject.
351 If there are no more articles with the same subject, go to
352 the first unread article."
353   :group 'gnus-summary-maneuvering
354   :type 'boolean)
355
356 (defcustom gnus-auto-goto-ignores 'unfetched
357   "*Says how to handle unfetched articles when maneuvering.
358
359 This variable can either be the symbols nil (maneuver to any
360 article), `undownloaded' (maneuvering while unplugged ignores articles
361 that have not been fetched), `always-undownloaded' (maneuvering always
362 ignores articles that have not been fetched), `unfetched' (maneuvering
363 ignores articles whose headers have not been fetched).
364
365 NOTE: The list of unfetched articles will always be nil when plugged
366 and, when unplugged, a subset of the undownloaded article list."
367   :group 'gnus-summary-maneuvering
368   :type '(choice (const :tag "None" nil)
369                  (const :tag "Undownloaded when unplugged" undownloaded)
370                  (const :tag "Undownloaded" always-undownloaded)
371                  (const :tag "Unfetched" unfetched)))
372
373 (defcustom gnus-summary-check-current nil
374   "*If non-nil, consider the current article when moving.
375 The \"unread\" movement commands will stay on the same line if the
376 current article is unread."
377   :group 'gnus-summary-maneuvering
378   :type 'boolean)
379
380 (defcustom gnus-auto-center-summary 2
381   "*If non-nil, always center the current summary buffer.
382 In particular, if `vertical' do only vertical recentering.  If non-nil
383 and non-`vertical', do both horizontal and vertical recentering."
384   :group 'gnus-summary-maneuvering
385   :type '(choice (const :tag "none" nil)
386                  (const vertical)
387                  (integer :tag "height")
388                  (sexp :menu-tag "both" t)))
389
390 (defvar gnus-auto-center-group t
391   "*If non-nil, always center the group buffer.")
392
393 (defcustom gnus-show-all-headers nil
394   "*If non-nil, don't hide any headers."
395   :group 'gnus-article-hiding
396   :group 'gnus-article-headers
397   :type 'boolean)
398
399 (defcustom gnus-summary-ignore-duplicates nil
400   "*If non-nil, ignore articles with identical Message-ID headers."
401   :group 'gnus-summary
402   :type 'boolean)
403
404 (defcustom gnus-single-article-buffer t
405   "*If non-nil, display all articles in the same buffer.
406 If nil, each group will get its own article buffer."
407   :group 'gnus-article-various
408   :type 'boolean)
409
410 (defcustom gnus-break-pages t
411   "*If non-nil, do page breaking on articles.
412 The page delimiter is specified by the `gnus-page-delimiter'
413 variable."
414   :group 'gnus-article-various
415   :type 'boolean)
416
417 (defcustom gnus-move-split-methods nil
418   "*Variable used to suggest where articles are to be moved to.
419 It uses the same syntax as the `gnus-split-methods' variable.
420 However, whereas `gnus-split-methods' specifies file names as targets,
421 this variable specifies group names."
422   :group 'gnus-summary-mail
423   :type '(repeat (choice (list :value (fun) function)
424                          (cons :value ("" "") regexp (repeat string))
425                          (sexp :value nil))))
426
427 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
428   "Function used to compute default prefix for article move/copy/etc prompts.
429 The function should take one argument, a group name, and return a
430 string with the suggested prefix."
431   :group 'gnus-summary-mail
432   :type 'function)
433
434 (defcustom gnus-unread-mark ?           ;Whitespace
435   "*Mark used for unread articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-ticked-mark ?!
440   "*Mark used for ticked articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-dormant-mark ??
445   "*Mark used for dormant articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-del-mark ?r
450   "*Mark used for del'd articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-read-mark ?R
455   "*Mark used for read articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-expirable-mark ?E
460   "*Mark used for expirable articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-killed-mark ?K
465   "*Mark used for killed articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-spam-mark ?$
470   "*Mark used for spam articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-souped-mark ?F
475   "*Mark used for souped articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-kill-file-mark ?X
480   "*Mark used for articles killed by kill files."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-low-score-mark ?Y
485   "*Mark used for articles with a low score."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-catchup-mark ?C
490   "*Mark used for articles that are caught up."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-replied-mark ?A
495   "*Mark used for articles that have been replied to."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-forwarded-mark ?F
500   "*Mark used for articles that have been forwarded."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-recent-mark ?N
505   "*Mark used for articles that are recent."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-cached-mark ?*
510   "*Mark used for articles that are in the cache."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-saved-mark ?S
515   "*Mark used for articles that have been saved."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-unseen-mark ?.
520   "*Mark used for articles that haven't been seen."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-no-mark ?               ;Whitespace
525   "*Mark used for articles that have no other secondary mark."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-ancient-mark ?O
530   "*Mark used for ancient articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-sparse-mark ?Q
535   "*Mark used for sparsely reffed articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-canceled-mark ?G
540   "*Mark used for canceled articles."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-duplicate-mark ?M
545   "*Mark used for duplicate articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-undownloaded-mark ?-
550   "*Mark used for articles that weren't downloaded."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-downloaded-mark ?+
555   "*Mark used for articles that were downloaded."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-downloadable-mark ?%
560   "*Mark used for articles that are to be downloaded."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-unsendable-mark ?=
565   "*Mark used for articles that won't be sent."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-score-over-mark ?+
570   "*Score mark used for articles with high scores."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-score-below-mark ?-
575   "*Score mark used for articles with low scores."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-empty-thread-mark ?     ;Whitespace
580   "*There is no thread under the article."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-not-empty-thread-mark ?=
585   "*There is a thread under the article."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-view-pseudo-asynchronously nil
590   "*If non-nil, Gnus will view pseudo-articles asynchronously."
591   :group 'gnus-extract-view
592   :type 'boolean)
593
594 (defcustom gnus-auto-expirable-marks
595   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
596         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
597         gnus-souped-mark gnus-duplicate-mark)
598   "*The list of marks converted into expiration if a group is auto-expirable."
599   :version "21.1"
600   :group 'gnus-summary
601   :type '(repeat character))
602
603 (defcustom gnus-inhibit-user-auto-expire t
604   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
605   :version "21.1"
606   :group 'gnus-summary
607   :type 'boolean)
608
609 (defcustom gnus-view-pseudos nil
610   "*If `automatic', pseudo-articles will be viewed automatically.
611 If `not-confirm', pseudos will be viewed automatically, and the user
612 will not be asked to confirm the command."
613   :group 'gnus-extract-view
614   :type '(choice (const :tag "off" nil)
615                  (const automatic)
616                  (const not-confirm)))
617
618 (defcustom gnus-view-pseudos-separately t
619   "*If non-nil, one pseudo-article will be created for each file to be viewed.
620 If nil, all files that use the same viewing command will be given as a
621 list of parameters to that command."
622   :group 'gnus-extract-view
623   :type 'boolean)
624
625 (defcustom gnus-insert-pseudo-articles t
626   "*If non-nil, insert pseudo-articles when decoding articles."
627   :group 'gnus-extract-view
628   :type 'boolean)
629
630 (defcustom gnus-summary-dummy-line-format
631   "   %(:                             :%) %S\n"
632   "*The format specification for the dummy roots in the summary buffer.
633 It works along the same lines as a normal formatting string,
634 with some simple extensions.
635
636 %S  The subject
637
638 General format specifiers can also be used.
639 See `(gnus)Formatting Variables'."
640   :link '(custom-manual "(gnus)Formatting Variables")
641   :group 'gnus-threading
642   :type 'string)
643
644 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
645   "*The format specification for the summary mode line.
646 It works along the same lines as a normal formatting string,
647 with some simple extensions:
648
649 %G  Group name
650 %p  Unprefixed group name
651 %A  Current article number
652 %z  Current article score
653 %V  Gnus version
654 %U  Number of unread articles in the group
655 %e  Number of unselected articles in the group
656 %Z  A string with unread/unselected article counts
657 %g  Shortish group name
658 %S  Subject of the current article
659 %u  User-defined spec
660 %s  Current score file name
661 %d  Number of dormant articles
662 %r  Number of articles that have been marked as read in this session
663 %E  Number of articles expunged by the score files"
664   :group 'gnus-summary-format
665   :type 'string)
666
667 (defcustom gnus-list-identifiers nil
668   "Regexp that matches list identifiers to be removed from subject.
669 This can also be a list of regexps."
670   :version "21.1"
671   :group 'gnus-summary-format
672   :group 'gnus-article-hiding
673   :type '(choice (const :tag "none" nil)
674                  (regexp :value ".*")
675                  (repeat :value (".*") regexp)))
676
677 (defcustom gnus-summary-mark-below 0
678   "*Mark all articles with a score below this variable as read.
679 This variable is local to each summary buffer and usually set by the
680 score file."
681   :group 'gnus-score-default
682   :type 'integer)
683
684 (defun gnus-widget-reversible-match (widget value)
685   "Ignoring WIDGET, convert VALUE to internal form.
686 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
687   ;; (debug value)
688   (or (symbolp value)
689       (and (listp value)
690            (eq (length value) 2)
691            (eq (nth 0 value) 'not)
692            (symbolp (nth 1 value)))))
693
694 (defun gnus-widget-reversible-to-internal (widget value)
695   "Ignoring WIDGET, convert VALUE to internal form.
696 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
697 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
698   ;; (debug value)
699   (if (atom value)
700       (list value nil)
701     (list (nth 1 value) t)))
702
703 (defun gnus-widget-reversible-to-external (widget value)
704   "Ignoring WIDGET, convert VALUE to external form.
705 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
706 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
707   ;; (debug value)
708   (if (nth 1 value)
709       (list 'not (nth 0 value))
710     (nth 0 value)))
711
712 (define-widget 'gnus-widget-reversible 'group
713   "A `group' that convert values."
714   :match 'gnus-widget-reversible-match
715   :value-to-internal 'gnus-widget-reversible-to-internal
716   :value-to-external 'gnus-widget-reversible-to-external)
717
718 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
719   "*List of functions used for sorting articles in the summary buffer.
720
721 Each function takes two articles and returns non-nil if the first
722 article should be sorted before the other.  If you use more than one
723 function, the primary sort function should be the last.  You should
724 probably always include `gnus-article-sort-by-number' in the list of
725 sorting functions -- preferably first.  Also note that sorting by date
726 is often much slower than sorting by number, and the sorting order is
727 very similar.  (Sorting by date means sorting by the time the message
728 was sent, sorting by number means sorting by arrival time.)
729
730 Each item can also be a list `(not F)' where F is a function;
731 this reverses the sort order.
732
733 Ready-made functions include `gnus-article-sort-by-number',
734 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
735 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
736 and `gnus-article-sort-by-score'.
737
738 When threading is turned on, the variable `gnus-thread-sort-functions'
739 controls how articles are sorted."
740   :group 'gnus-summary-sort
741   :type '(repeat (gnus-widget-reversible
742                   (choice (function-item gnus-article-sort-by-number)
743                           (function-item gnus-article-sort-by-author)
744                           (function-item gnus-article-sort-by-subject)
745                           (function-item gnus-article-sort-by-date)
746                           (function-item gnus-article-sort-by-score)
747                           (function-item gnus-article-sort-by-random)
748                           (function :tag "other"))
749                   (boolean :tag "Reverse order"))))
750
751
752 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
753   "*List of functions used for sorting threads in the summary buffer.
754 By default, threads are sorted by article number.
755
756 Each function takes two threads and returns non-nil if the first
757 thread should be sorted before the other.  If you use more than one
758 function, the primary sort function should be the last.  You should
759 probably always include `gnus-thread-sort-by-number' in the list of
760 sorting functions -- preferably first.  Also note that sorting by date
761 is often much slower than sorting by number, and the sorting order is
762 very similar.  (Sorting by date means sorting by the time the message
763 was sent, sorting by number means sorting by arrival time.)
764
765 Each list item can also be a list `(not F)' where F is a
766 function; this specifies reversed sort order.
767
768 Ready-made functions include `gnus-thread-sort-by-number',
769 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
770 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
771 `gnus-thread-sort-by-most-recent-number',
772 `gnus-thread-sort-by-most-recent-date',
773 `gnus-thread-sort-by-random', and
774 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
775
776 When threading is turned off, the variable
777 `gnus-article-sort-functions' controls how articles are sorted."
778   :group 'gnus-summary-sort
779   :type '(repeat 
780           (gnus-widget-reversible
781            (choice (function-item gnus-thread-sort-by-number)
782                    (function-item gnus-thread-sort-by-author)
783                    (function-item gnus-thread-sort-by-subject)
784                    (function-item gnus-thread-sort-by-date)
785                    (function-item gnus-thread-sort-by-score)
786                    (function-item gnus-thread-sort-by-most-recent-number)
787                    (function-item gnus-thread-sort-by-most-recent-date)
788                    (function-item gnus-thread-sort-by-random)
789                    (function-item gnus-thread-sort-by-total-score)
790                    (function :tag "other"))
791            (boolean :tag "Reverse order"))))
792
793 (defcustom gnus-thread-score-function '+
794   "*Function used for calculating the total score of a thread.
795
796 The function is called with the scores of the article and each
797 subthread and should then return the score of the thread.
798
799 Some functions you can use are `+', `max', or `min'."
800   :group 'gnus-summary-sort
801   :type 'function)
802
803 (defcustom gnus-summary-expunge-below nil
804   "All articles that have a score less than this variable will be expunged.
805 This variable is local to the summary buffers."
806   :group 'gnus-score-default
807   :type '(choice (const :tag "off" nil)
808                  integer))
809
810 (defcustom gnus-thread-expunge-below nil
811   "All threads that have a total score less than this variable will be expunged.
812 See `gnus-thread-score-function' for en explanation of what a
813 \"thread score\" is.
814
815 This variable is local to the summary buffers."
816   :group 'gnus-threading
817   :group 'gnus-score-default
818   :type '(choice (const :tag "off" nil)
819                  integer))
820
821 (defcustom gnus-summary-mode-hook nil
822   "*A hook for Gnus summary mode.
823 This hook is run before any variables are set in the summary buffer."
824   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
825   :group 'gnus-summary-various
826   :type 'hook)
827
828 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
829 (when (featurep 'xemacs)
830   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
831   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
832   (add-hook 'gnus-summary-mode-hook
833             'gnus-xmas-switch-horizontal-scrollbar-off))
834
835 (defcustom gnus-summary-menu-hook nil
836   "*Hook run after the creation of the summary mode menu."
837   :group 'gnus-summary-visual
838   :type 'hook)
839
840 (defcustom gnus-summary-exit-hook nil
841   "*A hook called on exit from the summary buffer.
842 It will be called with point in the group buffer."
843   :group 'gnus-summary-exit
844   :type 'hook)
845
846 (defcustom gnus-summary-prepare-hook nil
847   "*A hook called after the summary buffer has been generated.
848 If you want to modify the summary buffer, you can use this hook."
849   :group 'gnus-summary-various
850   :type 'hook)
851
852 (defcustom gnus-summary-prepared-hook nil
853   "*A hook called as the last thing after the summary buffer has been generated."
854   :group 'gnus-summary-various
855   :type 'hook)
856
857 (defcustom gnus-summary-generate-hook nil
858   "*A hook run just before generating the summary buffer.
859 This hook is commonly used to customize threading variables and the
860 like."
861   :group 'gnus-summary-various
862   :type 'hook)
863
864 (defcustom gnus-select-group-hook nil
865   "*A hook called when a newsgroup is selected.
866
867 If you'd like to simplify subjects like the
868 `gnus-summary-next-same-subject' command does, you can use the
869 following hook:
870
871  (add-hook gnus-select-group-hook
872            (lambda ()
873              (mapcar (lambda (header)
874                        (mail-header-set-subject
875                         header
876                         (gnus-simplify-subject
877                          (mail-header-subject header) 're-only)))
878                      gnus-newsgroup-headers)))"
879   :group 'gnus-group-select
880   :type 'hook)
881
882 (defcustom gnus-select-article-hook nil
883   "*A hook called when an article is selected."
884   :group 'gnus-summary-choose
885   :options '(gnus-agent-fetch-selected-article)
886   :type 'hook)
887
888 (defcustom gnus-visual-mark-article-hook
889   (list 'gnus-highlight-selected-summary)
890   "*Hook run after selecting an article in the summary buffer.
891 It is meant to be used for highlighting the article in some way.  It
892 is not run if `gnus-visual' is nil."
893   :group 'gnus-summary-visual
894   :type 'hook)
895
896 (defcustom gnus-parse-headers-hook nil
897   "*A hook called before parsing the headers."
898   :group 'gnus-various
899   :type 'hook)
900
901 (defcustom gnus-exit-group-hook nil
902   "*A hook called when exiting summary mode.
903 This hook is not called from the non-updating exit commands like `Q'."
904   :group 'gnus-various
905   :type 'hook)
906
907 (defcustom gnus-summary-update-hook
908   (list 'gnus-summary-highlight-line)
909   "*A hook called when a summary line is changed.
910 The hook will not be called if `gnus-visual' is nil.
911
912 The default function `gnus-summary-highlight-line' will
913 highlight the line according to the `gnus-summary-highlight'
914 variable."
915   :group 'gnus-summary-visual
916   :type 'hook)
917
918 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
919   "*A hook called when an article is selected for the first time.
920 The hook is intended to mark an article as read (or unread)
921 automatically when it is selected."
922   :group 'gnus-summary-choose
923   :type 'hook)
924
925 (defcustom gnus-group-no-more-groups-hook nil
926   "*A hook run when returning to group mode having no more (unread) groups."
927   :group 'gnus-group-select
928   :type 'hook)
929
930 (defcustom gnus-ps-print-hook nil
931   "*A hook run before ps-printing something from Gnus."
932   :group 'gnus-summary
933   :type 'hook)
934
935 (defcustom gnus-summary-article-move-hook nil
936   "*A hook called after an article is moved, copied, respooled, or crossposted."
937   :group 'gnus-summary
938   :type 'hook)
939
940 (defcustom gnus-summary-article-delete-hook nil
941   "*A hook called after an article is deleted."
942   :group 'gnus-summary
943   :type 'hook)
944
945 (defcustom gnus-summary-article-expire-hook nil
946   "*A hook called after an article is expired."
947   :group 'gnus-summary
948   :type 'hook)
949
950 (defcustom gnus-summary-display-arrow
951   (and (fboundp 'display-graphic-p)
952        (display-graphic-p))
953   "*If non-nil, display an arrow highlighting the current article."
954   :version "21.1"
955   :group 'gnus-summary
956   :type 'boolean)
957
958 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
959   "Face used for highlighting the current article in the summary buffer."
960   :group 'gnus-summary-visual
961   :type 'face)
962
963 (defvar gnus-tmp-downloaded nil)
964
965 (defcustom gnus-summary-highlight
966   '(((eq mark gnus-canceled-mark)
967      . gnus-summary-cancelled-face)
968     ((and uncached (> score default-high))
969      . gnus-summary-high-undownloaded-face)
970     ((and uncached (< score default-low))
971      . gnus-summary-low-undownloaded-face)
972     (uncached
973      . gnus-summary-normal-undownloaded-face)
974     ((and (> score default-high)
975           (or (eq mark gnus-dormant-mark)
976               (eq mark gnus-ticked-mark)))
977      . gnus-summary-high-ticked-face)
978     ((and (< score default-low)
979           (or (eq mark gnus-dormant-mark)
980               (eq mark gnus-ticked-mark)))
981      . gnus-summary-low-ticked-face)
982     ((or (eq mark gnus-dormant-mark)
983          (eq mark gnus-ticked-mark))
984      . gnus-summary-normal-ticked-face)
985     ((and (> score default-high) (eq mark gnus-ancient-mark))
986      . gnus-summary-high-ancient-face)
987     ((and (< score default-low) (eq mark gnus-ancient-mark))
988      . gnus-summary-low-ancient-face)
989     ((eq mark gnus-ancient-mark)
990      . gnus-summary-normal-ancient-face)
991     ((and (> score default-high) (eq mark gnus-unread-mark))
992      . gnus-summary-high-unread-face)
993     ((and (< score default-low) (eq mark gnus-unread-mark))
994      . gnus-summary-low-unread-face)
995     ((eq mark gnus-unread-mark)
996      . gnus-summary-normal-unread-face)
997     ((> score default-high)
998      . gnus-summary-high-read-face)
999     ((< score default-low)
1000      . gnus-summary-low-read-face)
1001     (t
1002      . gnus-summary-normal-read-face))
1003   "*Controls the highlighting of summary buffer lines.
1004
1005 A list of (FORM . FACE) pairs.  When deciding how a a particular
1006 summary line should be displayed, each form is evaluated.  The content
1007 of the face field after the first true form is used.  You can change
1008 how those summary lines are displayed, by editing the face field.
1009
1010 You can use the following variables in the FORM field.
1011
1012 score:        The article's score
1013 default:      The default article score.
1014 default-high: The default score for high scored articles.
1015 default-low:  The default score for low scored articles.
1016 below:        The score below which articles are automatically marked as read.
1017 mark:         The article's mark.
1018 uncached:     Non-nil if the article is uncached."
1019   :group 'gnus-summary-visual
1020   :type '(repeat (cons (sexp :tag "Form" nil)
1021                        face)))
1022
1023 (defcustom gnus-alter-header-function nil
1024   "Function called to allow alteration of article header structures.
1025 The function is called with one parameter, the article header vector,
1026 which it may alter in any way."
1027   :type '(choice (const :tag "None" nil)
1028                  function)
1029   :group 'gnus-summary)
1030
1031 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1032   "Variable that says which function should be used to decode a string with encoded words.")
1033
1034 (defcustom gnus-extra-headers '(To Newsgroups)
1035   "*Extra headers to parse."
1036   :version "21.1"
1037   :group 'gnus-summary
1038   :type '(repeat symbol))
1039
1040 (defcustom gnus-ignored-from-addresses
1041   (and user-mail-address (regexp-quote user-mail-address))
1042   "*Regexp of From headers that may be suppressed in favor of To headers."
1043   :version "21.1"
1044   :group 'gnus-summary
1045   :type 'regexp)
1046
1047 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1048   "List of charsets that should be ignored.
1049 When these charsets are used in the \"charset\" parameter, the
1050 default charset will be used instead."
1051   :version "21.1"
1052   :type '(repeat symbol)
1053   :group 'gnus-charset)
1054
1055 (gnus-define-group-parameter
1056  ignored-charsets
1057  :type list
1058  :function-document
1059  "Return the ignored charsets of GROUP."
1060  :variable gnus-group-ignored-charsets-alist
1061  :variable-default
1062  '(("alt\\.chinese\\.text" iso-8859-1))
1063  :variable-document
1064  "Alist of regexps (to match group names) and charsets that should be ignored.
1065 When these charsets are used in the \"charset\" parameter, the
1066 default charset will be used instead."
1067  :variable-group gnus-charset
1068  :variable-type '(repeat (cons (regexp :tag "Group")
1069                                (repeat symbol)))
1070  :parameter-type '(choice :tag "Ignored charsets"
1071                           :value nil
1072                           (repeat (symbol)))
1073  :parameter-document       "\
1074 List of charsets that should be ignored.
1075
1076 When these charsets are used in the \"charset\" parameter, the
1077 default charset will be used instead.")
1078
1079 (defcustom gnus-group-highlight-words-alist nil
1080   "Alist of group regexps and highlight regexps.
1081 This variable uses the same syntax as `gnus-emphasis-alist'."
1082   :version "21.1"
1083   :type '(repeat (cons (regexp :tag "Group")
1084                        (repeat (list (regexp :tag "Highlight regexp")
1085                                      (number :tag "Group for entire word" 0)
1086                                      (number :tag "Group for displayed part" 0)
1087                                      (symbol :tag "Face"
1088                                              gnus-emphasis-highlight-words)))))
1089   :group 'gnus-summary-visual)
1090
1091 (defcustom gnus-summary-show-article-charset-alist
1092   nil
1093   "Alist of number and charset.
1094 The article will be shown with the charset corresponding to the
1095 numbered argument.
1096 For example: ((1 . cn-gb-2312) (2 . big5))."
1097   :version "21.1"
1098   :type '(repeat (cons (number :tag "Argument" 1)
1099                        (symbol :tag "Charset")))
1100   :group 'gnus-charset)
1101
1102 (defcustom gnus-preserve-marks t
1103   "Whether marks are preserved when moving, copying and respooling messages."
1104   :version "21.1"
1105   :type 'boolean
1106   :group 'gnus-summary-marks)
1107
1108 (defcustom gnus-alter-articles-to-read-function nil
1109   "Function to be called to alter the list of articles to be selected."
1110   :type '(choice (const nil) function)
1111   :group 'gnus-summary)
1112
1113 (defcustom gnus-orphan-score nil
1114   "*All orphans get this score added.  Set in the score file."
1115   :group 'gnus-score-default
1116   :type '(choice (const nil)
1117                  integer))
1118
1119 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1120   "*A regexp to match MIME parts when saving multiple parts of a
1121 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1122 This regexp will be used by default when prompting the user for which
1123 type of files to save."
1124   :group 'gnus-summary
1125   :type 'regexp)
1126
1127 (defcustom gnus-read-all-available-headers nil
1128   "Whether Gnus should parse all headers made available to it.
1129 This is mostly relevant for slow back ends where the user may
1130 wish to widen the summary buffer to include all headers
1131 that were fetched.  Say, for nnultimate groups."
1132   :group 'gnus-summary
1133   :type '(choice boolean regexp))
1134
1135 (defcustom gnus-summary-muttprint-program "muttprint"
1136   "Command (and optional arguments) used to run Muttprint."
1137   :version "21.3"
1138   :group 'gnus-summary
1139   :type 'string)
1140
1141 (defcustom gnus-article-loose-mime t
1142   "If non-nil, don't require MIME-Version header.
1143 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1144 supply the MIME-Version header or deliberately strip it from the mail.
1145 If non-nil (the default), Gnus will treat some articles as MIME
1146 even if the MIME-Version header is missing."
1147   :version "21.3"
1148   :type 'boolean
1149   :group 'gnus-article-mime)
1150
1151 (defcustom gnus-article-emulate-mime t
1152   "If non-nil, use MIME emulation for uuencode and the like.
1153 This means that Gnus will search message bodies for text that look
1154 like uuencoded bits, yEncoded bits, and so on, and present that using
1155 the normal Gnus MIME machinery."
1156   :type 'boolean
1157   :group 'gnus-article-mime)
1158
1159 ;;; Internal variables
1160
1161 (defvar gnus-summary-display-cache nil)
1162 (defvar gnus-article-mime-handles nil)
1163 (defvar gnus-article-decoded-p nil)
1164 (defvar gnus-article-charset nil)
1165 (defvar gnus-article-ignored-charsets nil)
1166 (defvar gnus-scores-exclude-files nil)
1167 (defvar gnus-page-broken nil)
1168
1169 (defvar gnus-original-article nil)
1170 (defvar gnus-article-internal-prepare-hook nil)
1171 (defvar gnus-newsgroup-process-stack nil)
1172
1173 (defvar gnus-thread-indent-array nil)
1174 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1175 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1176   "Function called to sort the articles within a thread after it has been gathered together.")
1177
1178 (defvar gnus-summary-save-parts-type-history nil)
1179 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1180
1181 ;; Avoid highlighting in kill files.
1182 (defvar gnus-summary-inhibit-highlight nil)
1183 (defvar gnus-newsgroup-selected-overlay nil)
1184 (defvar gnus-inhibit-limiting nil)
1185 (defvar gnus-newsgroup-adaptive-score-file nil)
1186 (defvar gnus-current-score-file nil)
1187 (defvar gnus-current-move-group nil)
1188 (defvar gnus-current-copy-group nil)
1189 (defvar gnus-current-crosspost-group nil)
1190 (defvar gnus-newsgroup-display nil)
1191
1192 (defvar gnus-newsgroup-dependencies nil)
1193 (defvar gnus-newsgroup-adaptive nil)
1194 (defvar gnus-summary-display-article-function nil)
1195 (defvar gnus-summary-highlight-line-function nil
1196   "Function called after highlighting a summary line.")
1197
1198 (defvar gnus-summary-line-format-alist
1199   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1200     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1201     (?s gnus-tmp-subject-or-nil ?s)
1202     (?n gnus-tmp-name ?s)
1203     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1204         ?s)
1205     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1206             gnus-tmp-from) ?s)
1207     (?F gnus-tmp-from ?s)
1208     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1209     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1210     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1211     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1212     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1213     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1214     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1215     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1216     (?L gnus-tmp-lines ?s)
1217     (?O gnus-tmp-downloaded ?c)
1218     (?I gnus-tmp-indentation ?s)
1219     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1220     (?R gnus-tmp-replied ?c)
1221     (?\[ gnus-tmp-opening-bracket ?c)
1222     (?\] gnus-tmp-closing-bracket ?c)
1223     (?\> (make-string gnus-tmp-level ? ) ?s)
1224     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1225     (?i gnus-tmp-score ?d)
1226     (?z gnus-tmp-score-char ?c)
1227     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1228     (?U gnus-tmp-unread ?c)
1229     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1230         ?s)
1231     (?t (gnus-summary-number-of-articles-in-thread
1232          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1233         ?d)
1234     (?e (gnus-summary-number-of-articles-in-thread
1235          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1236         ?c)
1237     (?u gnus-tmp-user-defined ?s)
1238     (?P (gnus-pick-line-number) ?d)
1239     (?B gnus-tmp-thread-tree-header-string ?s)
1240     (user-date (gnus-user-date
1241                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1242   "An alist of format specifications that can appear in summary lines.
1243 These are paired with what variables they correspond with, along with
1244 the type of the variable (string, integer, character, etc).")
1245
1246 (defvar gnus-summary-dummy-line-format-alist
1247   `((?S gnus-tmp-subject ?s)
1248     (?N gnus-tmp-number ?d)
1249     (?u gnus-tmp-user-defined ?s)))
1250
1251 (defvar gnus-summary-mode-line-format-alist
1252   `((?G gnus-tmp-group-name ?s)
1253     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1254     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1255     (?A gnus-tmp-article-number ?d)
1256     (?Z gnus-tmp-unread-and-unselected ?s)
1257     (?V gnus-version ?s)
1258     (?U gnus-tmp-unread-and-unticked ?d)
1259     (?S gnus-tmp-subject ?s)
1260     (?e gnus-tmp-unselected ?d)
1261     (?u gnus-tmp-user-defined ?s)
1262     (?d (length gnus-newsgroup-dormant) ?d)
1263     (?t (length gnus-newsgroup-marked) ?d)
1264     (?h (length gnus-newsgroup-spam-marked) ?d)
1265     (?r (length gnus-newsgroup-reads) ?d)
1266     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1267     (?E gnus-newsgroup-expunged-tally ?d)
1268     (?s (gnus-current-score-file-nondirectory) ?s)))
1269
1270 (defvar gnus-last-search-regexp nil
1271   "Default regexp for article search command.")
1272
1273 (defvar gnus-last-shell-command nil
1274   "Default shell command on article.")
1275
1276 (defvar gnus-newsgroup-agentized nil
1277   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1278 (defvar gnus-newsgroup-begin nil)
1279 (defvar gnus-newsgroup-end nil)
1280 (defvar gnus-newsgroup-last-rmail nil)
1281 (defvar gnus-newsgroup-last-mail nil)
1282 (defvar gnus-newsgroup-last-folder nil)
1283 (defvar gnus-newsgroup-last-file nil)
1284 (defvar gnus-newsgroup-auto-expire nil)
1285 (defvar gnus-newsgroup-active nil)
1286
1287 (defvar gnus-newsgroup-data nil)
1288 (defvar gnus-newsgroup-data-reverse nil)
1289 (defvar gnus-newsgroup-limit nil)
1290 (defvar gnus-newsgroup-limits nil)
1291 (defvar gnus-summary-use-undownloaded-faces nil)
1292
1293 (defvar gnus-newsgroup-unreads nil
1294   "Sorted list of unread articles in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-unselected nil
1297   "Sorted list of unselected unread articles in the current newsgroup.")
1298
1299 (defvar gnus-newsgroup-reads nil
1300   "Alist of read articles and article marks in the current newsgroup.")
1301
1302 (defvar gnus-newsgroup-expunged-tally nil)
1303
1304 (defvar gnus-newsgroup-marked nil
1305   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1306
1307 (defvar gnus-newsgroup-spam-marked nil
1308   "List of ranges of articles that have been marked as spam.")
1309
1310 (defvar gnus-newsgroup-killed nil
1311   "List of ranges of articles that have been through the scoring process.")
1312
1313 (defvar gnus-newsgroup-cached nil
1314   "Sorted list of articles that come from the article cache.")
1315
1316 (defvar gnus-newsgroup-saved nil
1317   "List of articles that have been saved.")
1318
1319 (defvar gnus-newsgroup-kill-headers nil)
1320
1321 (defvar gnus-newsgroup-replied nil
1322   "List of articles that have been replied to in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-forwarded nil
1325   "List of articles that have been forwarded in the current newsgroup.")
1326
1327 (defvar gnus-newsgroup-recent nil
1328   "List of articles that have are recent in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-expirable nil
1331   "Sorted list of articles in the current newsgroup that can be expired.")
1332
1333 (defvar gnus-newsgroup-processable nil
1334   "List of articles in the current newsgroup that can be processed.")
1335
1336 (defvar gnus-newsgroup-downloadable nil
1337   "Sorted list of articles in the current newsgroup that can be processed.")
1338
1339 (defvar gnus-newsgroup-unfetched nil
1340   "Sorted list of articles in the current newsgroup whose headers have
1341 not been fetched into the agent.
1342
1343 This list will always be a subset of gnus-newsgroup-undownloaded.")
1344
1345 (defvar gnus-newsgroup-undownloaded nil
1346   "List of articles in the current newsgroup that haven't been downloaded.")
1347
1348 (defvar gnus-newsgroup-unsendable nil
1349   "List of articles in the current newsgroup that won't be sent.")
1350
1351 (defvar gnus-newsgroup-bookmarks nil
1352   "List of articles in the current newsgroup that have bookmarks.")
1353
1354 (defvar gnus-newsgroup-dormant nil
1355   "Sorted list of dormant articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-unseen nil
1358   "List of unseen articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-seen nil
1361   "Range of seen articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-articles nil
1364   "List of articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-scored nil
1367   "List of scored articles in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-headers nil
1370   "List of article headers in the current newsgroup.")
1371
1372 (defvar gnus-newsgroup-threads nil)
1373
1374 (defvar gnus-newsgroup-prepared nil
1375   "Whether the current group has been prepared properly.")
1376
1377 (defvar gnus-newsgroup-ancient nil
1378   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1379
1380 (defvar gnus-newsgroup-sparse nil)
1381
1382 (defvar gnus-current-article nil)
1383 (defvar gnus-article-current nil)
1384 (defvar gnus-current-headers nil)
1385 (defvar gnus-have-all-headers nil)
1386 (defvar gnus-last-article nil)
1387 (defvar gnus-newsgroup-history nil)
1388 (defvar gnus-newsgroup-charset nil)
1389 (defvar gnus-newsgroup-ephemeral-charset nil)
1390 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1391
1392 (defvar gnus-article-before-search nil)
1393
1394 (defvar gnus-summary-local-variables
1395   '(gnus-newsgroup-name
1396     gnus-newsgroup-begin gnus-newsgroup-end
1397     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1398     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1399     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1400     gnus-newsgroup-unselected gnus-newsgroup-marked
1401     gnus-newsgroup-spam-marked
1402     gnus-newsgroup-reads gnus-newsgroup-saved
1403     gnus-newsgroup-replied gnus-newsgroup-forwarded
1404     gnus-newsgroup-recent
1405     gnus-newsgroup-expirable
1406     gnus-newsgroup-processable gnus-newsgroup-killed
1407     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1408     gnus-newsgroup-unfetched
1409     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1410     gnus-newsgroup-seen gnus-newsgroup-articles
1411     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1412     gnus-newsgroup-headers gnus-newsgroup-threads
1413     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1414     gnus-current-article gnus-current-headers gnus-have-all-headers
1415     gnus-last-article gnus-article-internal-prepare-hook
1416     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1417     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1418     gnus-thread-expunge-below
1419     gnus-score-alist gnus-current-score-file
1420     (gnus-summary-expunge-below . global)
1421     (gnus-summary-mark-below . global)
1422     (gnus-orphan-score . global)
1423     gnus-newsgroup-active gnus-scores-exclude-files
1424     gnus-newsgroup-history gnus-newsgroup-ancient
1425     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1426     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1427     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1428     (gnus-newsgroup-expunged-tally . 0)
1429     gnus-cache-removable-articles gnus-newsgroup-cached
1430     gnus-newsgroup-data gnus-newsgroup-data-reverse
1431     gnus-newsgroup-limit gnus-newsgroup-limits
1432     gnus-newsgroup-charset gnus-newsgroup-display
1433     gnus-summary-use-undownloaded-faces)
1434   "Variables that are buffer-local to the summary buffers.")
1435
1436 (defvar gnus-newsgroup-variables nil
1437   "A list of variables that have separate values in different newsgroups.
1438 A list of newsgroup (summary buffer) local variables, or cons of
1439 variables and their default expressions to be evalled (when the default
1440 values are not nil), that should be made global while the summary buffer
1441 is active.
1442
1443 Note: The default expressions will be evaluated (using function `eval')
1444 before assignment to the local variable rather than just assigned to it.
1445 If the default expression is the symbol `global', that symbol will not
1446 be evaluated but the global value of the local variable will be used
1447 instead.
1448
1449 These variables can be used to set variables in the group parameters
1450 while still allowing them to affect operations done in other buffers.
1451 For example:
1452
1453 \(setq gnus-newsgroup-variables
1454      '(message-use-followup-to
1455        (gnus-visible-headers .
1456          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1457 ")
1458
1459 ;; Byte-compiler warning.
1460 (eval-when-compile
1461   ;; Bind features so that require will believe that gnus-sum has
1462   ;; already been loaded (avoids infinite recursion)
1463   (let ((features (cons 'gnus-sum features)))
1464     ;; Several of the declarations in gnus-sum are needed to load the
1465     ;; following files. Right now, these definitions have been
1466     ;; compiled but not defined (evaluated).  We could either do a
1467     ;; eval-and-compile about all of the declarations or evaluate the
1468     ;; source file.
1469     (if (boundp 'gnus-newsgroup-variables)
1470         nil
1471       (load "gnus-sum.el" t t t))
1472     (require 'gnus)
1473     (require 'gnus-art)))
1474
1475 ;; MIME stuff.
1476
1477 (defvar gnus-decode-encoded-word-methods
1478   '(mail-decode-encoded-word-string)
1479   "List of methods used to decode encoded words.
1480
1481 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1482 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1483 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1484 whose names match REGEXP.
1485
1486 For example:
1487 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1488  mail-decode-encoded-word-string
1489  (\"chinese\" . rfc1843-decode-string))")
1490
1491 (defvar gnus-decode-encoded-word-methods-cache nil)
1492
1493 (defun gnus-multi-decode-encoded-word-string (string)
1494   "Apply the functions from `gnus-encoded-word-methods' that match."
1495   (unless (and gnus-decode-encoded-word-methods-cache
1496                (eq gnus-newsgroup-name
1497                    (car gnus-decode-encoded-word-methods-cache)))
1498     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1499     (mapcar (lambda (x)
1500               (if (symbolp x)
1501                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1502                 (if (and gnus-newsgroup-name
1503                          (string-match (car x) gnus-newsgroup-name))
1504                     (nconc gnus-decode-encoded-word-methods-cache
1505                            (list (cdr x))))))
1506             gnus-decode-encoded-word-methods))
1507   (let ((xlist gnus-decode-encoded-word-methods-cache))
1508     (pop xlist)
1509     (while xlist
1510       (setq string (funcall (pop xlist) string))))
1511   string)
1512
1513 ;; Subject simplification.
1514
1515 (defun gnus-simplify-whitespace (str)
1516   "Remove excessive whitespace from STR."
1517   ;; Multiple spaces.
1518   (while (string-match "[ \t][ \t]+" str)
1519     (setq str (concat (substring str 0 (match-beginning 0))
1520                         " "
1521                         (substring str (match-end 0)))))
1522   ;; Leading spaces.
1523   (when (string-match "^[ \t]+" str)
1524     (setq str (substring str (match-end 0))))
1525   ;; Trailing spaces.
1526   (when (string-match "[ \t]+$" str)
1527     (setq str (substring str 0 (match-beginning 0))))
1528   str)
1529
1530 (defun gnus-simplify-all-whitespace (str)
1531   "Remove all whitespace from STR."
1532   (while (string-match "[ \t\n]+" str)
1533     (setq str (replace-match "" nil nil str)))
1534   str)
1535
1536 (defsubst gnus-simplify-subject-re (subject)
1537   "Remove \"Re:\" from subject lines."
1538   (if (string-match message-subject-re-regexp subject)
1539       (substring subject (match-end 0))
1540     subject))
1541
1542 (defun gnus-simplify-subject (subject &optional re-only)
1543   "Remove `Re:' and words in parentheses.
1544 If RE-ONLY is non-nil, strip leading `Re:'s only."
1545   (let ((case-fold-search t))           ;Ignore case.
1546     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1547     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1548       (setq subject (substring subject (match-end 0))))
1549     ;; Remove uninteresting prefixes.
1550     (when (and (not re-only)
1551                gnus-simplify-ignored-prefixes
1552                (string-match gnus-simplify-ignored-prefixes subject))
1553       (setq subject (substring subject (match-end 0))))
1554     ;; Remove words in parentheses from end.
1555     (unless re-only
1556       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1557         (setq subject (substring subject 0 (match-beginning 0)))))
1558     ;; Return subject string.
1559     subject))
1560
1561 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1562 ;; all whitespace.
1563 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1564   (goto-char (point-min))
1565   (while (re-search-forward regexp nil t)
1566     (replace-match (or newtext ""))))
1567
1568 (defun gnus-simplify-buffer-fuzzy ()
1569   "Simplify string in the buffer fuzzily.
1570 The string in the accessible portion of the current buffer is simplified.
1571 It is assumed to be a single-line subject.
1572 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1573 matter is removed.  Additional things can be deleted by setting
1574 `gnus-simplify-subject-fuzzy-regexp'."
1575   (let ((case-fold-search t)
1576         (modified-tick))
1577     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1578
1579     (while (not (eq modified-tick (buffer-modified-tick)))
1580       (setq modified-tick (buffer-modified-tick))
1581       (cond
1582        ((listp gnus-simplify-subject-fuzzy-regexp)
1583         (mapcar 'gnus-simplify-buffer-fuzzy-step
1584                 gnus-simplify-subject-fuzzy-regexp))
1585        (gnus-simplify-subject-fuzzy-regexp
1586         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1587       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1588       (gnus-simplify-buffer-fuzzy-step
1589        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1590       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1591
1592     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1593     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1594     (gnus-simplify-buffer-fuzzy-step " $")
1595     (gnus-simplify-buffer-fuzzy-step "^ +")))
1596
1597 (defun gnus-simplify-subject-fuzzy (subject)
1598   "Simplify a subject string fuzzily.
1599 See `gnus-simplify-buffer-fuzzy' for details."
1600   (save-excursion
1601     (gnus-set-work-buffer)
1602     (let ((case-fold-search t))
1603       ;; Remove uninteresting prefixes.
1604       (when (and gnus-simplify-ignored-prefixes
1605                  (string-match gnus-simplify-ignored-prefixes subject))
1606         (setq subject (substring subject (match-end 0))))
1607       (insert subject)
1608       (inline (gnus-simplify-buffer-fuzzy))
1609       (buffer-string))))
1610
1611 (defsubst gnus-simplify-subject-fully (subject)
1612   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1613   (cond
1614    (gnus-simplify-subject-functions
1615     (gnus-map-function gnus-simplify-subject-functions subject))
1616    ((null gnus-summary-gather-subject-limit)
1617     (gnus-simplify-subject-re subject))
1618    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1619     (gnus-simplify-subject-fuzzy subject))
1620    ((numberp gnus-summary-gather-subject-limit)
1621     (gnus-limit-string (gnus-simplify-subject-re subject)
1622                        gnus-summary-gather-subject-limit))
1623    (t
1624     subject)))
1625
1626 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1627   "Check whether two subjects are equal.
1628 If optional argument SIMPLE-FIRST is t, first argument is already
1629 simplified."
1630   (cond
1631    ((null simple-first)
1632     (equal (gnus-simplify-subject-fully s1)
1633            (gnus-simplify-subject-fully s2)))
1634    (t
1635     (equal s1
1636            (gnus-simplify-subject-fully s2)))))
1637
1638 (defun gnus-summary-bubble-group ()
1639   "Increase the score of the current group.
1640 This is a handy function to add to `gnus-summary-exit-hook' to
1641 increase the score of each group you read."
1642   (gnus-group-add-score gnus-newsgroup-name))
1643
1644 \f
1645 ;;;
1646 ;;; Gnus summary mode
1647 ;;;
1648
1649 (put 'gnus-summary-mode 'mode-class 'special)
1650
1651 (defvar gnus-article-commands-menu)
1652
1653 ;; Non-orthogonal keys
1654
1655 (gnus-define-keys gnus-summary-mode-map
1656   " " gnus-summary-next-page
1657   "\177" gnus-summary-prev-page
1658   [delete] gnus-summary-prev-page
1659   [backspace] gnus-summary-prev-page
1660   "\r" gnus-summary-scroll-up
1661   "\M-\r" gnus-summary-scroll-down
1662   "n" gnus-summary-next-unread-article
1663   "p" gnus-summary-prev-unread-article
1664   "N" gnus-summary-next-article
1665   "P" gnus-summary-prev-article
1666   "\M-\C-n" gnus-summary-next-same-subject
1667   "\M-\C-p" gnus-summary-prev-same-subject
1668   "\M-n" gnus-summary-next-unread-subject
1669   "\M-p" gnus-summary-prev-unread-subject
1670   "." gnus-summary-first-unread-article
1671   "," gnus-summary-best-unread-article
1672   "\M-s" gnus-summary-search-article-forward
1673   "\M-r" gnus-summary-search-article-backward
1674   "<" gnus-summary-beginning-of-article
1675   ">" gnus-summary-end-of-article
1676   "j" gnus-summary-goto-article
1677   "^" gnus-summary-refer-parent-article
1678   "\M-^" gnus-summary-refer-article
1679   "u" gnus-summary-tick-article-forward
1680   "!" gnus-summary-tick-article-forward
1681   "U" gnus-summary-tick-article-backward
1682   "d" gnus-summary-mark-as-read-forward
1683   "D" gnus-summary-mark-as-read-backward
1684   "E" gnus-summary-mark-as-expirable
1685   "\M-u" gnus-summary-clear-mark-forward
1686   "\M-U" gnus-summary-clear-mark-backward
1687   "k" gnus-summary-kill-same-subject-and-select
1688   "\C-k" gnus-summary-kill-same-subject
1689   "\M-\C-k" gnus-summary-kill-thread
1690   "\M-\C-l" gnus-summary-lower-thread
1691   "e" gnus-summary-edit-article
1692   "#" gnus-summary-mark-as-processable
1693   "\M-#" gnus-summary-unmark-as-processable
1694   "\M-\C-t" gnus-summary-toggle-threads
1695   "\M-\C-s" gnus-summary-show-thread
1696   "\M-\C-h" gnus-summary-hide-thread
1697   "\M-\C-f" gnus-summary-next-thread
1698   "\M-\C-b" gnus-summary-prev-thread
1699   [(meta down)] gnus-summary-next-thread
1700   [(meta up)] gnus-summary-prev-thread
1701   "\M-\C-u" gnus-summary-up-thread
1702   "\M-\C-d" gnus-summary-down-thread
1703   "&" gnus-summary-execute-command
1704   "c" gnus-summary-catchup-and-exit
1705   "\C-w" gnus-summary-mark-region-as-read
1706   "\C-t" gnus-summary-toggle-truncation
1707   "?" gnus-summary-mark-as-dormant
1708   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1709   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1710   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1711   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1712   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1713   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1714   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1715   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1716   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1717   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1718   "=" gnus-summary-expand-window
1719   "\C-x\C-s" gnus-summary-reselect-current-group
1720   "\M-g" gnus-summary-rescan-group
1721   "w" gnus-summary-stop-page-breaking
1722   "\C-c\C-r" gnus-summary-caesar-message
1723   "f" gnus-summary-followup
1724   "F" gnus-summary-followup-with-original
1725   "C" gnus-summary-cancel-article
1726   "r" gnus-summary-reply
1727   "R" gnus-summary-reply-with-original
1728   "\C-c\C-f" gnus-summary-mail-forward
1729   "o" gnus-summary-save-article
1730   "\C-o" gnus-summary-save-article-mail
1731   "|" gnus-summary-pipe-output
1732   "\M-k" gnus-summary-edit-local-kill
1733   "\M-K" gnus-summary-edit-global-kill
1734   ;; "V" gnus-version
1735   "\C-c\C-d" gnus-summary-describe-group
1736   "q" gnus-summary-exit
1737   "Q" gnus-summary-exit-no-update
1738   "\C-c\C-i" gnus-info-find-node
1739   gnus-mouse-2 gnus-mouse-pick-article
1740   "m" gnus-summary-mail-other-window
1741   "a" gnus-summary-post-news
1742   "i" gnus-summary-news-other-window
1743   "x" gnus-summary-limit-to-unread
1744   "s" gnus-summary-isearch-article
1745   "t" gnus-summary-toggle-header
1746   "g" gnus-summary-show-article
1747   "l" gnus-summary-goto-last-article
1748   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1749   "\C-d" gnus-summary-enter-digest-group
1750   "\M-\C-d" gnus-summary-read-document
1751   "\M-\C-e" gnus-summary-edit-parameters
1752   "\M-\C-a" gnus-summary-customize-parameters
1753   "\C-c\C-b" gnus-bug
1754   "*" gnus-cache-enter-article
1755   "\M-*" gnus-cache-remove-article
1756   "\M-&" gnus-summary-universal-argument
1757   "\C-l" gnus-recenter
1758   "I" gnus-summary-increase-score
1759   "L" gnus-summary-lower-score
1760   "\M-i" gnus-symbolic-argument
1761   "h" gnus-summary-select-article-buffer
1762
1763   "b" gnus-article-view-part
1764   "\M-t" gnus-summary-toggle-display-buttonized
1765
1766   "V" gnus-summary-score-map
1767   "X" gnus-uu-extract-map
1768   "S" gnus-summary-send-map)
1769
1770 ;; Sort of orthogonal keymap
1771 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1772   "t" gnus-summary-tick-article-forward
1773   "!" gnus-summary-tick-article-forward
1774   "d" gnus-summary-mark-as-read-forward
1775   "r" gnus-summary-mark-as-read-forward
1776   "c" gnus-summary-clear-mark-forward
1777   " " gnus-summary-clear-mark-forward
1778   "e" gnus-summary-mark-as-expirable
1779   "x" gnus-summary-mark-as-expirable
1780   "?" gnus-summary-mark-as-dormant
1781   "b" gnus-summary-set-bookmark
1782   "B" gnus-summary-remove-bookmark
1783   "#" gnus-summary-mark-as-processable
1784   "\M-#" gnus-summary-unmark-as-processable
1785   "S" gnus-summary-limit-include-expunged
1786   "C" gnus-summary-catchup
1787   "H" gnus-summary-catchup-to-here
1788   "h" gnus-summary-catchup-from-here
1789   "\C-c" gnus-summary-catchup-all
1790   "k" gnus-summary-kill-same-subject-and-select
1791   "K" gnus-summary-kill-same-subject
1792   "P" gnus-uu-mark-map)
1793
1794 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1795   "c" gnus-summary-clear-above
1796   "u" gnus-summary-tick-above
1797   "m" gnus-summary-mark-above
1798   "k" gnus-summary-kill-below)
1799
1800 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1801   "/" gnus-summary-limit-to-subject
1802   "n" gnus-summary-limit-to-articles
1803   "w" gnus-summary-pop-limit
1804   "s" gnus-summary-limit-to-subject
1805   "a" gnus-summary-limit-to-author
1806   "u" gnus-summary-limit-to-unread
1807   "m" gnus-summary-limit-to-marks
1808   "M" gnus-summary-limit-exclude-marks
1809   "v" gnus-summary-limit-to-score
1810   "*" gnus-summary-limit-include-cached
1811   "D" gnus-summary-limit-include-dormant
1812   "T" gnus-summary-limit-include-thread
1813   "d" gnus-summary-limit-exclude-dormant
1814   "t" gnus-summary-limit-to-age
1815   "." gnus-summary-limit-to-unseen
1816   "x" gnus-summary-limit-to-extra
1817   "p" gnus-summary-limit-to-display-predicate
1818   "E" gnus-summary-limit-include-expunged
1819   "c" gnus-summary-limit-exclude-childless-dormant
1820   "C" gnus-summary-limit-mark-excluded-as-read
1821   "o" gnus-summary-insert-old-articles
1822   "N" gnus-summary-insert-new-articles
1823   "r" gnus-summary-limit-to-replied)
1824
1825 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1826   "n" gnus-summary-next-unread-article
1827   "p" gnus-summary-prev-unread-article
1828   "N" gnus-summary-next-article
1829   "P" gnus-summary-prev-article
1830   "\C-n" gnus-summary-next-same-subject
1831   "\C-p" gnus-summary-prev-same-subject
1832   "\M-n" gnus-summary-next-unread-subject
1833   "\M-p" gnus-summary-prev-unread-subject
1834   "f" gnus-summary-first-unread-article
1835   "b" gnus-summary-best-unread-article
1836   "j" gnus-summary-goto-article
1837   "g" gnus-summary-goto-subject
1838   "l" gnus-summary-goto-last-article
1839   "o" gnus-summary-pop-article)
1840
1841 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1842   "k" gnus-summary-kill-thread
1843   "l" gnus-summary-lower-thread
1844   "i" gnus-summary-raise-thread
1845   "T" gnus-summary-toggle-threads
1846   "t" gnus-summary-rethread-current
1847   "^" gnus-summary-reparent-thread
1848   "s" gnus-summary-show-thread
1849   "S" gnus-summary-show-all-threads
1850   "h" gnus-summary-hide-thread
1851   "H" gnus-summary-hide-all-threads
1852   "n" gnus-summary-next-thread
1853   "p" gnus-summary-prev-thread
1854   "u" gnus-summary-up-thread
1855   "o" gnus-summary-top-thread
1856   "d" gnus-summary-down-thread
1857   "#" gnus-uu-mark-thread
1858   "\M-#" gnus-uu-unmark-thread)
1859
1860 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1861   "g" gnus-summary-prepare
1862   "c" gnus-summary-insert-cached-articles
1863   "d" gnus-summary-insert-dormant-articles)
1864
1865 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1866   "c" gnus-summary-catchup-and-exit
1867   "C" gnus-summary-catchup-all-and-exit
1868   "E" gnus-summary-exit-no-update
1869   "Q" gnus-summary-exit
1870   "Z" gnus-summary-exit
1871   "n" gnus-summary-catchup-and-goto-next-group
1872   "R" gnus-summary-reselect-current-group
1873   "G" gnus-summary-rescan-group
1874   "N" gnus-summary-next-group
1875   "s" gnus-summary-save-newsrc
1876   "P" gnus-summary-prev-group)
1877
1878 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1879   " " gnus-summary-next-page
1880   "n" gnus-summary-next-page
1881   "\177" gnus-summary-prev-page
1882   [delete] gnus-summary-prev-page
1883   "p" gnus-summary-prev-page
1884   "\r" gnus-summary-scroll-up
1885   "\M-\r" gnus-summary-scroll-down
1886   "<" gnus-summary-beginning-of-article
1887   ">" gnus-summary-end-of-article
1888   "b" gnus-summary-beginning-of-article
1889   "e" gnus-summary-end-of-article
1890   "^" gnus-summary-refer-parent-article
1891   "r" gnus-summary-refer-parent-article
1892   "D" gnus-summary-enter-digest-group
1893   "R" gnus-summary-refer-references
1894   "T" gnus-summary-refer-thread
1895   "g" gnus-summary-show-article
1896   "s" gnus-summary-isearch-article
1897   "P" gnus-summary-print-article
1898   "M" gnus-mailing-list-insinuate
1899   "t" gnus-article-babel)
1900
1901 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1902   "b" gnus-article-add-buttons
1903   "B" gnus-article-add-buttons-to-head
1904   "o" gnus-article-treat-overstrike
1905   "e" gnus-article-emphasize
1906   "w" gnus-article-fill-cited-article
1907   "Q" gnus-article-fill-long-lines
1908   "C" gnus-article-capitalize-sentences
1909   "c" gnus-article-remove-cr
1910   "q" gnus-article-de-quoted-unreadable
1911   "6" gnus-article-de-base64-unreadable
1912   "Z" gnus-article-decode-HZ
1913   "A" gnus-article-treat-ansi-sequences
1914   "h" gnus-article-wash-html
1915   "u" gnus-article-unsplit-urls
1916   "s" gnus-summary-force-verify-and-decrypt
1917   "f" gnus-article-display-x-face
1918   "l" gnus-summary-stop-page-breaking
1919   "r" gnus-summary-caesar-message
1920   "m" gnus-summary-morse-message
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-treat-smiley
1923   "v" gnus-summary-verbose-headers
1924   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1925   "p" gnus-article-verify-x-pgp-sig
1926   "d" gnus-article-treat-dumbquotes)
1927
1928 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1929   ;; mnemonic: deuglif*Y*
1930   "u" gnus-article-outlook-unwrap-lines
1931   "a" gnus-article-outlook-repair-attribution
1932   "c" gnus-article-outlook-rearrange-citation
1933   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1934
1935 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1936   "a" gnus-article-hide
1937   "h" gnus-article-hide-headers
1938   "b" gnus-article-hide-boring-headers
1939   "s" gnus-article-hide-signature
1940   "c" gnus-article-hide-citation
1941   "C" gnus-article-hide-citation-in-followups
1942   "l" gnus-article-hide-list-identifiers
1943   "B" gnus-article-strip-banner
1944   "P" gnus-article-hide-pem
1945   "\C-c" gnus-article-hide-citation-maybe)
1946
1947 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1948   "a" gnus-article-highlight
1949   "h" gnus-article-highlight-headers
1950   "c" gnus-article-highlight-citation
1951   "s" gnus-article-highlight-signature)
1952
1953 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1954   "f" gnus-article-treat-fold-headers
1955   "u" gnus-article-treat-unfold-headers
1956   "n" gnus-article-treat-fold-newsgroups)
1957
1958 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1959   "x" gnus-article-display-x-face
1960   "d" gnus-article-display-face
1961   "s" gnus-treat-smiley
1962   "D" gnus-article-remove-images
1963   "f" gnus-treat-from-picon
1964   "m" gnus-treat-mail-picon
1965   "n" gnus-treat-newsgroups-picon)
1966
1967 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1968   "w" gnus-article-decode-mime-words
1969   "c" gnus-article-decode-charset
1970   "v" gnus-mime-view-all-parts
1971   "b" gnus-article-view-part)
1972
1973 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1974   "z" gnus-article-date-ut
1975   "u" gnus-article-date-ut
1976   "l" gnus-article-date-local
1977   "p" gnus-article-date-english
1978   "e" gnus-article-date-lapsed
1979   "o" gnus-article-date-original
1980   "i" gnus-article-date-iso8601
1981   "s" gnus-article-date-user)
1982
1983 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1984   "t" gnus-article-remove-trailing-blank-lines
1985   "l" gnus-article-strip-leading-blank-lines
1986   "m" gnus-article-strip-multiple-blank-lines
1987   "a" gnus-article-strip-blank-lines
1988   "A" gnus-article-strip-all-blank-lines
1989   "s" gnus-article-strip-leading-space
1990   "e" gnus-article-strip-trailing-space
1991   "w" gnus-article-remove-leading-whitespace)
1992
1993 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1994   "v" gnus-version
1995   "f" gnus-summary-fetch-faq
1996   "d" gnus-summary-describe-group
1997   "h" gnus-summary-describe-briefly
1998   "i" gnus-info-find-node
1999   "c" gnus-group-fetch-charter
2000   "C" gnus-group-fetch-control)
2001
2002 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2003   "e" gnus-summary-expire-articles
2004   "\M-\C-e" gnus-summary-expire-articles-now
2005   "\177" gnus-summary-delete-article
2006   [delete] gnus-summary-delete-article
2007   [backspace] gnus-summary-delete-article
2008   "m" gnus-summary-move-article
2009   "r" gnus-summary-respool-article
2010   "w" gnus-summary-edit-article
2011   "c" gnus-summary-copy-article
2012   "B" gnus-summary-crosspost-article
2013   "q" gnus-summary-respool-query
2014   "t" gnus-summary-respool-trace
2015   "i" gnus-summary-import-article
2016   "I" gnus-summary-create-article
2017   "p" gnus-summary-article-posted-p)
2018
2019 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2020   "o" gnus-summary-save-article
2021   "m" gnus-summary-save-article-mail
2022   "F" gnus-summary-write-article-file
2023   "r" gnus-summary-save-article-rmail
2024   "f" gnus-summary-save-article-file
2025   "b" gnus-summary-save-article-body-file
2026   "h" gnus-summary-save-article-folder
2027   "v" gnus-summary-save-article-vm
2028   "p" gnus-summary-pipe-output
2029   "P" gnus-summary-muttprint
2030   "s" gnus-soup-add-article)
2031
2032 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2033   "b" gnus-summary-display-buttonized
2034   "m" gnus-summary-repair-multipart
2035   "v" gnus-article-view-part
2036   "o" gnus-article-save-part
2037   "c" gnus-article-copy-part
2038   "C" gnus-article-view-part-as-charset
2039   "e" gnus-article-view-part-externally
2040   "E" gnus-article-encrypt-body
2041   "i" gnus-article-inline-part
2042   "|" gnus-article-pipe-part)
2043
2044 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2045   "p" gnus-summary-mark-as-processable
2046   "u" gnus-summary-unmark-as-processable
2047   "U" gnus-summary-unmark-all-processable
2048   "v" gnus-uu-mark-over
2049   "s" gnus-uu-mark-series
2050   "r" gnus-uu-mark-region
2051   "g" gnus-uu-unmark-region
2052   "R" gnus-uu-mark-by-regexp
2053   "G" gnus-uu-unmark-by-regexp
2054   "t" gnus-uu-mark-thread
2055   "T" gnus-uu-unmark-thread
2056   "a" gnus-uu-mark-all
2057   "b" gnus-uu-mark-buffer
2058   "S" gnus-uu-mark-sparse
2059   "k" gnus-summary-kill-process-mark
2060   "y" gnus-summary-yank-process-mark
2061   "w" gnus-summary-save-process-mark
2062   "i" gnus-uu-invert-processable)
2063
2064 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2065   ;;"x" gnus-uu-extract-any
2066   "m" gnus-summary-save-parts
2067   "u" gnus-uu-decode-uu
2068   "U" gnus-uu-decode-uu-and-save
2069   "s" gnus-uu-decode-unshar
2070   "S" gnus-uu-decode-unshar-and-save
2071   "o" gnus-uu-decode-save
2072   "O" gnus-uu-decode-save
2073   "b" gnus-uu-decode-binhex
2074   "B" gnus-uu-decode-binhex
2075   "p" gnus-uu-decode-postscript
2076   "P" gnus-uu-decode-postscript-and-save)
2077
2078 (gnus-define-keys
2079     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2080   "u" gnus-uu-decode-uu-view
2081   "U" gnus-uu-decode-uu-and-save-view
2082   "s" gnus-uu-decode-unshar-view
2083   "S" gnus-uu-decode-unshar-and-save-view
2084   "o" gnus-uu-decode-save-view
2085   "O" gnus-uu-decode-save-view
2086   "b" gnus-uu-decode-binhex-view
2087   "B" gnus-uu-decode-binhex-view
2088   "p" gnus-uu-decode-postscript-view
2089   "P" gnus-uu-decode-postscript-and-save-view)
2090
2091 (defvar gnus-article-post-menu nil)
2092
2093 (defconst gnus-summary-menu-maxlen 20)
2094
2095 (defun gnus-summary-menu-split (menu)
2096   ;; If we have lots of elements, divide them into groups of 20
2097   ;; and make a pane (or submenu) for each one.
2098   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2099       (let ((menu menu) sublists next
2100             (i 1))
2101         (while menu
2102           ;; Pull off the next gnus-summary-menu-maxlen elements
2103           ;; and make them the next element of sublist.
2104           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2105           (if next
2106               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2107                       nil))
2108           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2109                                              (aref (car (last menu)) 0)) menu)
2110                                sublists))
2111           (setq i (1+ i))
2112           (setq menu next))
2113         (nreverse sublists))
2114     ;; Few elements--put them all in one pane.
2115     menu))
2116
2117 (defun gnus-summary-make-menu-bar ()
2118   (gnus-turn-off-edit-menu 'summary)
2119
2120   (unless (boundp 'gnus-summary-misc-menu)
2121
2122     (easy-menu-define
2123       gnus-summary-kill-menu gnus-summary-mode-map ""
2124       (cons
2125        "Score"
2126        (nconc
2127         (list
2128          ["Customize" gnus-score-customize t])
2129         (gnus-make-score-map 'increase)
2130         (gnus-make-score-map 'lower)
2131         '(("Mark"
2132            ["Kill below" gnus-summary-kill-below t]
2133            ["Mark above" gnus-summary-mark-above t]
2134            ["Tick above" gnus-summary-tick-above t]
2135            ["Clear above" gnus-summary-clear-above t])
2136           ["Current score" gnus-summary-current-score t]
2137           ["Set score" gnus-summary-set-score t]
2138           ["Switch current score file..." gnus-score-change-score-file t]
2139           ["Set mark below..." gnus-score-set-mark-below t]
2140           ["Set expunge below..." gnus-score-set-expunge-below t]
2141           ["Edit current score file" gnus-score-edit-current-scores t]
2142           ["Edit score file" gnus-score-edit-file t]
2143           ["Trace score" gnus-score-find-trace t]
2144           ["Find words" gnus-score-find-favourite-words t]
2145           ["Rescore buffer" gnus-summary-rescore t]
2146           ["Increase score..." gnus-summary-increase-score t]
2147           ["Lower score..." gnus-summary-lower-score t]))))
2148
2149     ;; Define both the Article menu in the summary buffer and the
2150     ;; equivalent Commands menu in the article buffer here for
2151     ;; consistency.
2152     (let ((innards
2153            `(("Hide"
2154               ["All" gnus-article-hide t]
2155               ["Headers" gnus-article-hide-headers t]
2156               ["Signature" gnus-article-hide-signature t]
2157               ["Citation" gnus-article-hide-citation t]
2158               ["List identifiers" gnus-article-hide-list-identifiers t]
2159               ["Banner" gnus-article-strip-banner t]
2160               ["Boring headers" gnus-article-hide-boring-headers t])
2161              ("Highlight"
2162               ["All" gnus-article-highlight t]
2163               ["Headers" gnus-article-highlight-headers t]
2164               ["Signature" gnus-article-highlight-signature t]
2165               ["Citation" gnus-article-highlight-citation t])
2166              ("MIME"
2167               ["Words" gnus-article-decode-mime-words t]
2168               ["Charset" gnus-article-decode-charset t]
2169               ["QP" gnus-article-de-quoted-unreadable t]
2170               ["Base64" gnus-article-de-base64-unreadable t]
2171               ["View MIME buttons" gnus-summary-display-buttonized t]
2172               ["View all" gnus-mime-view-all-parts t]
2173               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2174               ["Encrypt body" gnus-article-encrypt-body
2175                :active (not (gnus-group-read-only-p))
2176                ,@(if (featurep 'xemacs) nil
2177                    '(:help "Encrypt the message body on disk"))]
2178               ["Extract all parts..." gnus-summary-save-parts t]
2179               ("Multipart"
2180                ["Repair multipart" gnus-summary-repair-multipart t]
2181                ["Pipe part..." gnus-article-pipe-part t]
2182                ["Inline part" gnus-article-inline-part t]
2183                ["Encrypt body" gnus-article-encrypt-body
2184                 :active (not (gnus-group-read-only-p))
2185                ,@(if (featurep 'xemacs) nil
2186                    '(:help "Encrypt the message body on disk"))]
2187                ["View part externally" gnus-article-view-part-externally t]
2188                ["View part with charset..." gnus-article-view-part-as-charset t]
2189                ["Copy part" gnus-article-copy-part t]
2190                ["Save part..." gnus-article-save-part t]
2191                ["View part" gnus-article-view-part t]))
2192              ("Date"
2193               ["Local" gnus-article-date-local t]
2194               ["ISO8601" gnus-article-date-iso8601 t]
2195               ["UT" gnus-article-date-ut t]
2196               ["Original" gnus-article-date-original t]
2197               ["Lapsed" gnus-article-date-lapsed t]
2198               ["User-defined" gnus-article-date-user t])
2199              ("Display"
2200               ["Remove images" gnus-article-remove-images t]
2201               ["Toggle smiley" gnus-treat-smiley t]
2202               ["Show X-Face" gnus-article-display-x-face t]
2203               ["Show picons in From" gnus-treat-from-picon t]
2204               ["Show picons in mail headers" gnus-treat-mail-picon t]
2205               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2206               ("View as different encoding"
2207                ,@(gnus-summary-menu-split
2208                   (mapcar
2209                    (lambda (cs)
2210                      ;; Since easymenu under Emacs doesn't allow
2211                      ;; lambda forms for menu commands, we should
2212                      ;; provide intern'ed function symbols.
2213                      (let ((command (intern (format "\
2214 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2215                        (fset command
2216                              `(lambda ()
2217                                 (interactive)
2218                                 (let ((gnus-summary-show-article-charset-alist
2219                                        '((1 . ,cs))))
2220                                   (gnus-summary-show-article 1))))
2221                        `[,(symbol-name cs) ,command t]))
2222                    (sort (if (fboundp 'coding-system-list)
2223                              (coding-system-list)
2224                            (mapcar 'car mm-mime-mule-charset-alist))
2225                          'string<)))))
2226              ("Washing"
2227               ("Remove Blanks"
2228                ["Leading" gnus-article-strip-leading-blank-lines t]
2229                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2230                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2231                ["All of the above" gnus-article-strip-blank-lines t]
2232                ["All" gnus-article-strip-all-blank-lines t]
2233                ["Leading space" gnus-article-strip-leading-space t]
2234                ["Trailing space" gnus-article-strip-trailing-space t]
2235                ["Leading space in headers"
2236                 gnus-article-remove-leading-whitespace t])
2237               ["Overstrike" gnus-article-treat-overstrike t]
2238               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2239               ["Emphasis" gnus-article-emphasize t]
2240               ["Word wrap" gnus-article-fill-cited-article t]
2241               ["Fill long lines" gnus-article-fill-long-lines t]
2242               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2243               ["Remove CR" gnus-article-remove-cr t]
2244               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2245               ["Base64" gnus-article-de-base64-unreadable t]
2246               ["Rot 13" gnus-summary-caesar-message
2247                ,@(if (featurep 'xemacs) '(t)
2248                    '(:help "\"Caesar rotate\" article by 13"))]
2249               ["Morse decode" gnus-summary-morse-message t]
2250               ["Unix pipe..." gnus-summary-pipe-message t]
2251               ["Add buttons" gnus-article-add-buttons t]
2252               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2253               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2254               ["Verbose header" gnus-summary-verbose-headers t]
2255               ["Toggle header" gnus-summary-toggle-header t]
2256               ["Unfold headers" gnus-article-treat-unfold-headers t]
2257               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2258               ["Html" gnus-article-wash-html t]
2259               ["Unsplit URLs" gnus-article-unsplit-urls t]
2260               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2261               ["Decode HZ" gnus-article-decode-HZ t]
2262               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2263               ("(Outlook) Deuglify"
2264                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2265                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2266                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2267                ["Full (Outlook) deuglify"
2268                 gnus-article-outlook-deuglify-article t])
2269               )
2270              ("Output"
2271               ["Save in default format..." gnus-summary-save-article
2272                ,@(if (featurep 'xemacs) '(t)
2273                    '(:help "Save article using default method"))]
2274               ["Save in file..." gnus-summary-save-article-file
2275                ,@(if (featurep 'xemacs) '(t)
2276                    '(:help "Save article in file"))]
2277               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2278               ["Save in MH folder..." gnus-summary-save-article-folder t]
2279               ["Save in VM folder..." gnus-summary-save-article-vm t]
2280               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2281               ["Save body in file..." gnus-summary-save-article-body-file t]
2282               ["Pipe through a filter..." gnus-summary-pipe-output t]
2283               ["Add to SOUP packet" gnus-soup-add-article t]
2284               ["Print with Muttprint..." gnus-summary-muttprint t]
2285               ["Print" gnus-summary-print-article
2286                ,@(if (featurep 'xemacs) '(t)
2287                    '(:help "Generate and print a PostScript image"))])
2288              ("Copy, move,... (Backend)"
2289               ,@(if (featurep 'xemacs) nil
2290                   '(:help "Copying, moving, expiring articles..."))
2291               ["Respool article..." gnus-summary-respool-article t]
2292               ["Move article..." gnus-summary-move-article
2293                (gnus-check-backend-function
2294                 'request-move-article gnus-newsgroup-name)]
2295               ["Copy article..." gnus-summary-copy-article t]
2296               ["Crosspost article..." gnus-summary-crosspost-article
2297                (gnus-check-backend-function
2298                 'request-replace-article gnus-newsgroup-name)]
2299               ["Import file..." gnus-summary-import-article
2300                (gnus-check-backend-function
2301                 'request-accept-article gnus-newsgroup-name)]
2302               ["Create article..." gnus-summary-create-article
2303                (gnus-check-backend-function
2304                 'request-accept-article gnus-newsgroup-name)]
2305               ["Check if posted" gnus-summary-article-posted-p t]
2306               ["Edit article" gnus-summary-edit-article
2307                (not (gnus-group-read-only-p))]
2308               ["Delete article" gnus-summary-delete-article
2309                (gnus-check-backend-function
2310                 'request-expire-articles gnus-newsgroup-name)]
2311               ["Query respool" gnus-summary-respool-query t]
2312               ["Trace respool" gnus-summary-respool-trace t]
2313               ["Delete expirable articles" gnus-summary-expire-articles-now
2314                (gnus-check-backend-function
2315                 'request-expire-articles gnus-newsgroup-name)])
2316              ("Extract"
2317               ["Uudecode" gnus-uu-decode-uu
2318                ,@(if (featurep 'xemacs) '(t)
2319                    '(:help "Decode uuencoded article(s)"))]
2320               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2321               ["Unshar" gnus-uu-decode-unshar t]
2322               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2323               ["Save" gnus-uu-decode-save t]
2324               ["Binhex" gnus-uu-decode-binhex t]
2325               ["Postscript" gnus-uu-decode-postscript t]
2326               ["All MIME parts" gnus-summary-save-parts t])
2327              ("Cache"
2328               ["Enter article" gnus-cache-enter-article t]
2329               ["Remove article" gnus-cache-remove-article t])
2330              ["Translate" gnus-article-babel t]
2331              ["Select article buffer" gnus-summary-select-article-buffer t]
2332              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2333              ["Isearch article..." gnus-summary-isearch-article t]
2334              ["Beginning of the article" gnus-summary-beginning-of-article t]
2335              ["End of the article" gnus-summary-end-of-article t]
2336              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2337              ["Fetch referenced articles" gnus-summary-refer-references t]
2338              ["Fetch current thread" gnus-summary-refer-thread t]
2339              ["Fetch article with id..." gnus-summary-refer-article t]
2340              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2341              ["Redisplay" gnus-summary-show-article t]
2342              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2343       (easy-menu-define
2344         gnus-summary-article-menu gnus-summary-mode-map ""
2345         (cons "Article" innards))
2346
2347       (if (not (keymapp gnus-summary-article-menu))
2348           (easy-menu-define
2349             gnus-article-commands-menu gnus-article-mode-map ""
2350             (cons "Commands" innards))
2351         ;; in Emacs, don't share menu.
2352         (setq gnus-article-commands-menu
2353               (copy-keymap gnus-summary-article-menu))
2354         (define-key gnus-article-mode-map [menu-bar commands]
2355           (cons "Commands" gnus-article-commands-menu))))
2356
2357     (easy-menu-define
2358       gnus-summary-thread-menu gnus-summary-mode-map ""
2359       '("Threads"
2360         ["Find all messages in thread" gnus-summary-refer-thread t]
2361         ["Toggle threading" gnus-summary-toggle-threads t]
2362         ["Hide threads" gnus-summary-hide-all-threads t]
2363         ["Show threads" gnus-summary-show-all-threads t]
2364         ["Hide thread" gnus-summary-hide-thread t]
2365         ["Show thread" gnus-summary-show-thread t]
2366         ["Go to next thread" gnus-summary-next-thread t]
2367         ["Go to previous thread" gnus-summary-prev-thread t]
2368         ["Go down thread" gnus-summary-down-thread t]
2369         ["Go up thread" gnus-summary-up-thread t]
2370         ["Top of thread" gnus-summary-top-thread t]
2371         ["Mark thread as read" gnus-summary-kill-thread t]
2372         ["Lower thread score" gnus-summary-lower-thread t]
2373         ["Raise thread score" gnus-summary-raise-thread t]
2374         ["Rethread current" gnus-summary-rethread-current t]))
2375
2376     (easy-menu-define
2377       gnus-summary-post-menu gnus-summary-mode-map ""
2378       `("Post"
2379         ["Send a message (mail or news)" gnus-summary-post-news
2380          ,@(if (featurep 'xemacs) '(t)
2381              '(:help "Compose a new message (mail or news)"))]
2382         ["Followup" gnus-summary-followup
2383          ,@(if (featurep 'xemacs) '(t)
2384              '(:help "Post followup to this article"))]
2385         ["Followup and yank" gnus-summary-followup-with-original
2386          ,@(if (featurep 'xemacs) '(t)
2387              '(:help "Post followup to this article, quoting its contents"))]
2388         ["Supersede article" gnus-summary-supersede-article t]
2389         ["Cancel article" gnus-summary-cancel-article
2390          ,@(if (featurep 'xemacs) '(t)
2391              '(:help "Cancel an article you posted"))]
2392         ["Reply" gnus-summary-reply t]
2393         ["Reply and yank" gnus-summary-reply-with-original t]
2394         ["Wide reply" gnus-summary-wide-reply t]
2395         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Mail a reply, quoting this article"))]
2398         ["Very wide reply" gnus-summary-very-wide-reply t]
2399         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2400          ,@(if (featurep 'xemacs) '(t)
2401              '(:help "Mail a very wide reply, quoting this article"))]
2402         ["Mail forward" gnus-summary-mail-forward t]
2403         ["Post forward" gnus-summary-post-forward t]
2404         ["Digest and mail" gnus-uu-digest-mail-forward t]
2405         ["Digest and post" gnus-uu-digest-post-forward t]
2406         ["Resend message" gnus-summary-resend-message t]
2407         ["Resend message edit" gnus-summary-resend-message-edit t]
2408         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2409         ["Send a mail" gnus-summary-mail-other-window t]
2410         ["Create a local message" gnus-summary-news-other-window t]
2411         ["Uuencode and post" gnus-uu-post-news
2412          ,@(if (featurep 'xemacs) '(t)
2413              '(:help "Post a uuencoded article"))]
2414         ["Followup via news" gnus-summary-followup-to-mail t]
2415         ["Followup via news and yank"
2416          gnus-summary-followup-to-mail-with-original t]
2417         ;;("Draft"
2418         ;;["Send" gnus-summary-send-draft t]
2419         ;;["Send bounced" gnus-resend-bounced-mail t])
2420         ))
2421
2422     (cond
2423      ((not (keymapp gnus-summary-post-menu))
2424       (setq gnus-article-post-menu gnus-summary-post-menu))
2425      ((not gnus-article-post-menu)
2426       ;; Don't share post menu.
2427       (setq gnus-article-post-menu
2428             (copy-keymap gnus-summary-post-menu))))
2429     (define-key gnus-article-mode-map [menu-bar post]
2430       (cons "Post" gnus-article-post-menu))
2431
2432     (easy-menu-define
2433       gnus-summary-misc-menu gnus-summary-mode-map ""
2434       `("Gnus"
2435         ("Mark Read"
2436          ["Mark as read" gnus-summary-mark-as-read-forward t]
2437          ["Mark same subject and select"
2438           gnus-summary-kill-same-subject-and-select t]
2439          ["Mark same subject" gnus-summary-kill-same-subject t]
2440          ["Catchup" gnus-summary-catchup
2441           ,@(if (featurep 'xemacs) '(t)
2442               '(:help "Mark unread articles in this group as read"))]
2443          ["Catchup all" gnus-summary-catchup-all t]
2444          ["Catchup to here" gnus-summary-catchup-to-here t]
2445          ["Catchup from here" gnus-summary-catchup-from-here t]
2446          ["Catchup region" gnus-summary-mark-region-as-read
2447           (gnus-mark-active-p)]
2448          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2449         ("Mark Various"
2450          ["Tick" gnus-summary-tick-article-forward t]
2451          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2452          ["Remove marks" gnus-summary-clear-mark-forward t]
2453          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2454          ["Set bookmark" gnus-summary-set-bookmark t]
2455          ["Remove bookmark" gnus-summary-remove-bookmark t])
2456         ("Limit to"
2457          ["Marks..." gnus-summary-limit-to-marks t]
2458          ["Subject..." gnus-summary-limit-to-subject t]
2459          ["Author..." gnus-summary-limit-to-author t]
2460          ["Age..." gnus-summary-limit-to-age t]
2461          ["Extra..." gnus-summary-limit-to-extra t]
2462          ["Score..." gnus-summary-limit-to-score t]
2463          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2464          ["Unread" gnus-summary-limit-to-unread t]
2465          ["Unseen" gnus-summary-limit-to-unseen t]
2466          ["Replied" gnus-summary-limit-to-replied t]
2467          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2468          ["Next articles" gnus-summary-limit-to-articles t]
2469          ["Pop limit" gnus-summary-pop-limit t]
2470          ["Show dormant" gnus-summary-limit-include-dormant t]
2471          ["Hide childless dormant"
2472           gnus-summary-limit-exclude-childless-dormant t]
2473          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2474          ["Hide marked" gnus-summary-limit-exclude-marks t]
2475          ["Show expunged" gnus-summary-limit-include-expunged t])
2476         ("Process Mark"
2477          ["Set mark" gnus-summary-mark-as-processable t]
2478          ["Remove mark" gnus-summary-unmark-as-processable t]
2479          ["Remove all marks" gnus-summary-unmark-all-processable t]
2480          ["Invert marks" gnus-uu-invert-processable t]
2481          ["Mark above" gnus-uu-mark-over t]
2482          ["Mark series" gnus-uu-mark-series t]
2483          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2484          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2485          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2486          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2487          ["Mark all" gnus-uu-mark-all t]
2488          ["Mark buffer" gnus-uu-mark-buffer t]
2489          ["Mark sparse" gnus-uu-mark-sparse t]
2490          ["Mark thread" gnus-uu-mark-thread t]
2491          ["Unmark thread" gnus-uu-unmark-thread t]
2492          ("Process Mark Sets"
2493           ["Kill" gnus-summary-kill-process-mark t]
2494           ["Yank" gnus-summary-yank-process-mark
2495            gnus-newsgroup-process-stack]
2496           ["Save" gnus-summary-save-process-mark t]
2497           ["Run command on marked..." gnus-summary-universal-argument t]))
2498         ("Scroll article"
2499          ["Page forward" gnus-summary-next-page
2500           ,@(if (featurep 'xemacs) '(t)
2501               '(:help "Show next page of article"))]
2502          ["Page backward" gnus-summary-prev-page
2503           ,@(if (featurep 'xemacs) '(t)
2504               '(:help "Show previous page of article"))]
2505          ["Line forward" gnus-summary-scroll-up t])
2506         ("Move"
2507          ["Next unread article" gnus-summary-next-unread-article t]
2508          ["Previous unread article" gnus-summary-prev-unread-article t]
2509          ["Next article" gnus-summary-next-article t]
2510          ["Previous article" gnus-summary-prev-article t]
2511          ["Next unread subject" gnus-summary-next-unread-subject t]
2512          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2513          ["Next article same subject" gnus-summary-next-same-subject t]
2514          ["Previous article same subject" gnus-summary-prev-same-subject t]
2515          ["First unread article" gnus-summary-first-unread-article t]
2516          ["Best unread article" gnus-summary-best-unread-article t]
2517          ["Go to subject number..." gnus-summary-goto-subject t]
2518          ["Go to article number..." gnus-summary-goto-article t]
2519          ["Go to the last article" gnus-summary-goto-last-article t]
2520          ["Pop article off history" gnus-summary-pop-article t])
2521         ("Sort"
2522          ["Sort by number" gnus-summary-sort-by-number t]
2523          ["Sort by author" gnus-summary-sort-by-author t]
2524          ["Sort by subject" gnus-summary-sort-by-subject t]
2525          ["Sort by date" gnus-summary-sort-by-date t]
2526          ["Sort by score" gnus-summary-sort-by-score t]
2527          ["Sort by lines" gnus-summary-sort-by-lines t]
2528          ["Sort by characters" gnus-summary-sort-by-chars t]
2529          ["Randomize" gnus-summary-sort-by-random t]
2530          ["Original sort" gnus-summary-sort-by-original t])
2531         ("Help"
2532          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2533          ["Describe group" gnus-summary-describe-group t]
2534          ["Fetch charter" gnus-group-fetch-charter
2535           ,@(if (featurep 'xemacs) nil
2536               '(:help "Display the charter of the current group"))]
2537          ["Fetch control message" gnus-group-fetch-control
2538           ,@(if (featurep 'xemacs) nil
2539               '(:help "Display the archived control message for the current group"))]
2540          ["Read manual" gnus-info-find-node t])
2541         ("Modes"
2542          ["Pick and read" gnus-pick-mode t]
2543          ["Binary" gnus-binary-mode t])
2544         ("Regeneration"
2545          ["Regenerate" gnus-summary-prepare t]
2546          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2547          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2548          ["Toggle threading" gnus-summary-toggle-threads t])
2549         ["See old articles" gnus-summary-insert-old-articles t]
2550         ["See new articles" gnus-summary-insert-new-articles t]
2551         ["Filter articles..." gnus-summary-execute-command t]
2552         ["Run command on articles..." gnus-summary-universal-argument t]
2553         ["Search articles forward..." gnus-summary-search-article-forward t]
2554         ["Search articles backward..." gnus-summary-search-article-backward t]
2555         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2556         ["Expand window" gnus-summary-expand-window t]
2557         ["Expire expirable articles" gnus-summary-expire-articles
2558          (gnus-check-backend-function
2559           'request-expire-articles gnus-newsgroup-name)]
2560         ["Edit local kill file" gnus-summary-edit-local-kill t]
2561         ["Edit main kill file" gnus-summary-edit-global-kill t]
2562         ["Edit group parameters" gnus-summary-edit-parameters t]
2563         ["Customize group parameters" gnus-summary-customize-parameters t]
2564         ["Send a bug report" gnus-bug t]
2565         ("Exit"
2566          ["Catchup and exit" gnus-summary-catchup-and-exit
2567           ,@(if (featurep 'xemacs) '(t)
2568               '(:help "Mark unread articles in this group as read, then exit"))]
2569          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2570          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2571          ["Exit group" gnus-summary-exit
2572           ,@(if (featurep 'xemacs) '(t)
2573               '(:help "Exit current group, return to group selection mode"))]
2574          ["Exit group without updating" gnus-summary-exit-no-update t]
2575          ["Exit and goto next group" gnus-summary-next-group t]
2576          ["Exit and goto prev group" gnus-summary-prev-group t]
2577          ["Reselect group" gnus-summary-reselect-current-group t]
2578          ["Rescan group" gnus-summary-rescan-group t]
2579          ["Update dribble" gnus-summary-save-newsrc t])))
2580
2581     (gnus-run-hooks 'gnus-summary-menu-hook)))
2582
2583 (defvar gnus-summary-tool-bar-map nil)
2584
2585 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2586 (defun gnus-summary-make-tool-bar ()
2587   (if (and (fboundp 'tool-bar-add-item-from-menu)
2588            (default-value 'tool-bar-mode)
2589            (not gnus-summary-tool-bar-map))
2590       (setq gnus-summary-tool-bar-map
2591             (let ((tool-bar-map (make-sparse-keymap))
2592                   (load-path (mm-image-load-path)))
2593               (tool-bar-add-item-from-menu
2594                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2595               (tool-bar-add-item-from-menu
2596                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2597               (tool-bar-add-item-from-menu
2598                'gnus-summary-post-news "post" gnus-summary-mode-map)
2599               (tool-bar-add-item-from-menu
2600                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2601               (tool-bar-add-item-from-menu
2602                'gnus-summary-followup "followup" gnus-summary-mode-map)
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-summary-reply "reply" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2617               (tool-bar-add-item-from-menu
2618                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2619               (tool-bar-add-item-from-menu
2620                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2621               (tool-bar-add-item-from-menu
2622                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2623               (tool-bar-add-item-from-menu
2624                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2625               tool-bar-map)))
2626   (if gnus-summary-tool-bar-map
2627       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2628
2629 (defun gnus-score-set-default (var value)
2630   "A version of set that updates the GNU Emacs menu-bar."
2631   (set var value)
2632   ;; It is the message that forces the active status to be updated.
2633   (message ""))
2634
2635 (defun gnus-make-score-map (type)
2636   "Make a summary score map of type TYPE."
2637   (if t
2638       nil
2639     (let ((headers '(("author" "from" string)
2640                      ("subject" "subject" string)
2641                      ("article body" "body" string)
2642                      ("article head" "head" string)
2643                      ("xref" "xref" string)
2644                      ("extra header" "extra" string)
2645                      ("lines" "lines" number)
2646                      ("followups to author" "followup" string)))
2647           (types '((number ("less than" <)
2648                            ("greater than" >)
2649                            ("equal" =))
2650                    (string ("substring" s)
2651                            ("exact string" e)
2652                            ("fuzzy string" f)
2653                            ("regexp" r))))
2654           (perms '(("temporary" (current-time-string))
2655                    ("permanent" nil)
2656                    ("immediate" now)))
2657           header)
2658       (list
2659        (apply
2660         'nconc
2661         (list
2662          (if (eq type 'lower)
2663              "Lower score"
2664            "Increase score"))
2665         (let (outh)
2666           (while headers
2667             (setq header (car headers))
2668             (setq outh
2669                   (cons
2670                    (apply
2671                     'nconc
2672                     (list (car header))
2673                     (let ((ts (cdr (assoc (nth 2 header) types)))
2674                           outt)
2675                       (while ts
2676                         (setq outt
2677                               (cons
2678                                (apply
2679                                 'nconc
2680                                 (list (caar ts))
2681                                 (let ((ps perms)
2682                                       outp)
2683                                   (while ps
2684                                     (setq outp
2685                                           (cons
2686                                            (vector
2687                                             (caar ps)
2688                                             (list
2689                                              'gnus-summary-score-entry
2690                                              (nth 1 header)
2691                                              (if (or (string= (nth 1 header)
2692                                                               "head")
2693                                                      (string= (nth 1 header)
2694                                                               "body"))
2695                                                  ""
2696                                                (list 'gnus-summary-header
2697                                                      (nth 1 header)))
2698                                              (list 'quote (nth 1 (car ts)))
2699                                              (list 'gnus-score-delta-default
2700                                                    nil)
2701                                              (nth 1 (car ps))
2702                                              t)
2703                                             t)
2704                                            outp))
2705                                     (setq ps (cdr ps)))
2706                                   (list (nreverse outp))))
2707                                outt))
2708                         (setq ts (cdr ts)))
2709                       (list (nreverse outt))))
2710                    outh))
2711             (setq headers (cdr headers)))
2712           (list (nreverse outh))))))))
2713
2714 \f
2715
2716 (defun gnus-summary-mode (&optional group)
2717   "Major mode for reading articles.
2718
2719 All normal editing commands are switched off.
2720 \\<gnus-summary-mode-map>
2721 Each line in this buffer represents one article.  To read an
2722 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2723 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2724 respectively.
2725
2726 You can also post articles and send mail from this buffer.  To
2727 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2728 of an article, type `\\[gnus-summary-reply]'.
2729
2730 There are approx. one gazillion commands you can execute in this
2731 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2732
2733 The following commands are available:
2734
2735 \\{gnus-summary-mode-map}"
2736   (interactive)
2737   (kill-all-local-variables)
2738   (when (gnus-visual-p 'summary-menu 'menu)
2739     (gnus-summary-make-menu-bar)
2740     (gnus-summary-make-tool-bar))
2741   (gnus-summary-make-local-variables)
2742   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2743     (gnus-summary-make-local-variables))
2744   (gnus-make-thread-indent-array)
2745   (gnus-simplify-mode-line)
2746   (setq major-mode 'gnus-summary-mode)
2747   (setq mode-name "Summary")
2748   (make-local-variable 'minor-mode-alist)
2749   (use-local-map gnus-summary-mode-map)
2750   (buffer-disable-undo)
2751   (setq buffer-read-only t              ;Disable modification
2752         show-trailing-whitespace nil)
2753   (setq truncate-lines t)
2754   (setq selective-display t)
2755   (setq selective-display-ellipses t)   ;Display `...'
2756   (gnus-summary-set-display-table)
2757   (gnus-set-default-directory)
2758   (setq gnus-newsgroup-name group)
2759   (make-local-variable 'gnus-summary-line-format)
2760   (make-local-variable 'gnus-summary-line-format-spec)
2761   (make-local-variable 'gnus-summary-dummy-line-format)
2762   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2763   (make-local-variable 'gnus-summary-mark-positions)
2764   (gnus-make-local-hook 'pre-command-hook)
2765   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2766   (gnus-run-hooks 'gnus-summary-mode-hook)
2767   (turn-on-gnus-mailing-list-mode)
2768   (mm-enable-multibyte)
2769   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2770   (gnus-update-summary-mark-positions))
2771
2772 (defun gnus-summary-make-local-variables ()
2773   "Make all the local summary buffer variables."
2774   (let (global)
2775     (dolist (local gnus-summary-local-variables)
2776       (if (consp local)
2777           (progn
2778             (if (eq (cdr local) 'global)
2779                 ;; Copy the global value of the variable.
2780                 (setq global (symbol-value (car local)))
2781               ;; Use the value from the list.
2782               (setq global (eval (cdr local))))
2783             (set (make-local-variable (car local)) global))
2784         ;; Simple nil-valued local variable.
2785         (set (make-local-variable local) nil)))))
2786
2787 (defun gnus-summary-clear-local-variables ()
2788   (let ((locals gnus-summary-local-variables))
2789     (while locals
2790       (if (consp (car locals))
2791           (and (vectorp (caar locals))
2792                (set (caar locals) nil))
2793         (and (vectorp (car locals))
2794              (set (car locals) nil)))
2795       (setq locals (cdr locals)))))
2796
2797 ;; Summary data functions.
2798
2799 (defmacro gnus-data-number (data)
2800   `(car ,data))
2801
2802 (defmacro gnus-data-set-number (data number)
2803   `(setcar ,data ,number))
2804
2805 (defmacro gnus-data-mark (data)
2806   `(nth 1 ,data))
2807
2808 (defmacro gnus-data-set-mark (data mark)
2809   `(setcar (nthcdr 1 ,data) ,mark))
2810
2811 (defmacro gnus-data-pos (data)
2812   `(nth 2 ,data))
2813
2814 (defmacro gnus-data-set-pos (data pos)
2815   `(setcar (nthcdr 2 ,data) ,pos))
2816
2817 (defmacro gnus-data-header (data)
2818   `(nth 3 ,data))
2819
2820 (defmacro gnus-data-set-header (data header)
2821   `(setf (nth 3 ,data) ,header))
2822
2823 (defmacro gnus-data-level (data)
2824   `(nth 4 ,data))
2825
2826 (defmacro gnus-data-unread-p (data)
2827   `(= (nth 1 ,data) gnus-unread-mark))
2828
2829 (defmacro gnus-data-read-p (data)
2830   `(/= (nth 1 ,data) gnus-unread-mark))
2831
2832 (defmacro gnus-data-pseudo-p (data)
2833   `(consp (nth 3 ,data)))
2834
2835 (defmacro gnus-data-find (number)
2836   `(assq ,number gnus-newsgroup-data))
2837
2838 (defmacro gnus-data-find-list (number &optional data)
2839   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2840      (memq (assq ,number bdata)
2841            bdata)))
2842
2843 (defmacro gnus-data-make (number mark pos header level)
2844   `(list ,number ,mark ,pos ,header ,level))
2845
2846 (defun gnus-data-enter (after-article number mark pos header level offset)
2847   (let ((data (gnus-data-find-list after-article)))
2848     (unless data
2849       (error "No such article: %d" after-article))
2850     (setcdr data (cons (gnus-data-make number mark pos header level)
2851                        (cdr data)))
2852     (setq gnus-newsgroup-data-reverse nil)
2853     (gnus-data-update-list (cddr data) offset)))
2854
2855 (defun gnus-data-enter-list (after-article list &optional offset)
2856   (when list
2857     (let ((data (and after-article (gnus-data-find-list after-article)))
2858           (ilist list))
2859       (if (not (or data
2860                    after-article))
2861           (let ((odata gnus-newsgroup-data))
2862             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2863             (when offset
2864               (gnus-data-update-list odata offset)))
2865       ;; Find the last element in the list to be spliced into the main
2866         ;; list.
2867         (while (cdr list)
2868           (setq list (cdr list)))
2869         (if (not data)
2870             (progn
2871               (setcdr list gnus-newsgroup-data)
2872               (setq gnus-newsgroup-data ilist)
2873               (when offset
2874                 (gnus-data-update-list (cdr list) offset)))
2875           (setcdr list (cdr data))
2876           (setcdr data ilist)
2877           (when offset
2878             (gnus-data-update-list (cdr list) offset))))
2879       (setq gnus-newsgroup-data-reverse nil))))
2880
2881 (defun gnus-data-remove (article &optional offset)
2882   (let ((data gnus-newsgroup-data))
2883     (if (= (gnus-data-number (car data)) article)
2884         (progn
2885           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2886                 gnus-newsgroup-data-reverse nil)
2887           (when offset
2888             (gnus-data-update-list gnus-newsgroup-data offset)))
2889       (while (cdr data)
2890         (when (= (gnus-data-number (cadr data)) article)
2891           (setcdr data (cddr data))
2892           (when offset
2893             (gnus-data-update-list (cdr data) offset))
2894           (setq data nil
2895                 gnus-newsgroup-data-reverse nil))
2896         (setq data (cdr data))))))
2897
2898 (defmacro gnus-data-list (backward)
2899   `(if ,backward
2900        (or gnus-newsgroup-data-reverse
2901            (setq gnus-newsgroup-data-reverse
2902                  (reverse gnus-newsgroup-data)))
2903      gnus-newsgroup-data))
2904
2905 (defun gnus-data-update-list (data offset)
2906   "Add OFFSET to the POS of all data entries in DATA."
2907   (setq gnus-newsgroup-data-reverse nil)
2908   (while data
2909     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2910     (setq data (cdr data))))
2911
2912 (defun gnus-summary-article-pseudo-p (article)
2913   "Say whether this article is a pseudo article or not."
2914   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2915
2916 (defmacro gnus-summary-article-sparse-p (article)
2917   "Say whether this article is a sparse article or not."
2918   `(memq ,article gnus-newsgroup-sparse))
2919
2920 (defmacro gnus-summary-article-ancient-p (article)
2921   "Say whether this article is a sparse article or not."
2922   `(memq ,article gnus-newsgroup-ancient))
2923
2924 (defun gnus-article-parent-p (number)
2925   "Say whether this article is a parent or not."
2926   (let ((data (gnus-data-find-list number)))
2927     (and (cdr data)              ; There has to be an article after...
2928          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2929             (gnus-data-level (nth 1 data))))))
2930
2931 (defun gnus-article-children (number)
2932   "Return a list of all children to NUMBER."
2933   (let* ((data (gnus-data-find-list number))
2934          (level (gnus-data-level (car data)))
2935          children)
2936     (setq data (cdr data))
2937     (while (and data
2938                 (= (gnus-data-level (car data)) (1+ level)))
2939       (push (gnus-data-number (car data)) children)
2940       (setq data (cdr data)))
2941     children))
2942
2943 (defmacro gnus-summary-skip-intangible ()
2944   "If the current article is intangible, then jump to a different article."
2945   '(let ((to (get-text-property (point) 'gnus-intangible)))
2946      (and to (gnus-summary-goto-subject to))))
2947
2948 (defmacro gnus-summary-article-intangible-p ()
2949   "Say whether this article is intangible or not."
2950   '(get-text-property (point) 'gnus-intangible))
2951
2952 (defun gnus-article-read-p (article)
2953   "Say whether ARTICLE is read or not."
2954   (not (or (memq article gnus-newsgroup-marked)
2955            (memq article gnus-newsgroup-spam-marked)
2956            (memq article gnus-newsgroup-unreads)
2957            (memq article gnus-newsgroup-unselected)
2958            (memq article gnus-newsgroup-dormant))))
2959
2960 ;; Some summary mode macros.
2961
2962 (defmacro gnus-summary-article-number ()
2963   "The article number of the article on the current line.
2964 If there isn't an article number here, then we return the current
2965 article number."
2966   '(progn
2967      (gnus-summary-skip-intangible)
2968      (or (get-text-property (point) 'gnus-number)
2969          (gnus-summary-last-subject))))
2970
2971 (defmacro gnus-summary-article-header (&optional number)
2972   "Return the header of article NUMBER."
2973   `(gnus-data-header (gnus-data-find
2974                       ,(or number '(gnus-summary-article-number)))))
2975
2976 (defmacro gnus-summary-thread-level (&optional number)
2977   "Return the level of thread that starts with article NUMBER."
2978   `(if (and (eq gnus-summary-make-false-root 'dummy)
2979             (get-text-property (point) 'gnus-intangible))
2980        0
2981      (gnus-data-level (gnus-data-find
2982                        ,(or number '(gnus-summary-article-number))))))
2983
2984 (defmacro gnus-summary-article-mark (&optional number)
2985   "Return the mark of article NUMBER."
2986   `(gnus-data-mark (gnus-data-find
2987                     ,(or number '(gnus-summary-article-number)))))
2988
2989 (defmacro gnus-summary-article-pos (&optional number)
2990   "Return the position of the line of article NUMBER."
2991   `(gnus-data-pos (gnus-data-find
2992                    ,(or number '(gnus-summary-article-number)))))
2993
2994 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2995 (defmacro gnus-summary-article-subject (&optional number)
2996   "Return current subject string or nil if nothing."
2997   `(let ((headers
2998           ,(if number
2999                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3000              '(gnus-data-header (assq (gnus-summary-article-number)
3001                                       gnus-newsgroup-data)))))
3002      (and headers
3003           (vectorp headers)
3004           (mail-header-subject headers))))
3005
3006 (defmacro gnus-summary-article-score (&optional number)
3007   "Return current article score."
3008   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3009                   gnus-newsgroup-scored))
3010        gnus-summary-default-score 0))
3011
3012 (defun gnus-summary-article-children (&optional number)
3013   "Return a list of article numbers that are children of article NUMBER."
3014   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3015          (level (gnus-data-level (car data)))
3016          l children)
3017     (while (and (setq data (cdr data))
3018                 (> (setq l (gnus-data-level (car data))) level))
3019       (and (= (1+ level) l)
3020            (push (gnus-data-number (car data))
3021                  children)))
3022     (nreverse children)))
3023
3024 (defun gnus-summary-article-parent (&optional number)
3025   "Return the article number of the parent of article NUMBER."
3026   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3027                                     (gnus-data-list t)))
3028          (level (gnus-data-level (car data))))
3029     (if (zerop level)
3030         ()                              ; This is a root.
3031       ;; We search until we find an article with a level less than
3032       ;; this one.  That function has to be the parent.
3033       (while (and (setq data (cdr data))
3034                   (not (< (gnus-data-level (car data)) level))))
3035       (and data (gnus-data-number (car data))))))
3036
3037 (defun gnus-unread-mark-p (mark)
3038   "Say whether MARK is the unread mark."
3039   (= mark gnus-unread-mark))
3040
3041 (defun gnus-read-mark-p (mark)
3042   "Say whether MARK is one of the marks that mark as read.
3043 This is all marks except unread, ticked, dormant, and expirable."
3044   (not (or (= mark gnus-unread-mark)
3045            (= mark gnus-ticked-mark)
3046            (= mark gnus-spam-mark)
3047            (= mark gnus-dormant-mark)
3048            (= mark gnus-expirable-mark))))
3049
3050 (defmacro gnus-article-mark (number)
3051   "Return the MARK of article NUMBER.
3052 This macro should only be used when computing the mark the \"first\"
3053 time; i.e., when generating the summary lines.  After that,
3054 `gnus-summary-article-mark' should be used to examine the
3055 marks of articles."
3056   `(cond
3057     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3058     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3059     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3060     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3061     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3062     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3063     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3064     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3065            gnus-ancient-mark))))
3066
3067 ;; Saving hidden threads.
3068
3069 (defmacro gnus-save-hidden-threads (&rest forms)
3070   "Save hidden threads, eval FORMS, and restore the hidden threads."
3071   (let ((config (make-symbol "config")))
3072     `(let ((,config (gnus-hidden-threads-configuration)))
3073        (unwind-protect
3074            (save-excursion
3075              ,@forms)
3076          (gnus-restore-hidden-threads-configuration ,config)))))
3077 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3078 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3079
3080 (defun gnus-data-compute-positions ()
3081   "Compute the positions of all articles."
3082   (setq gnus-newsgroup-data-reverse nil)
3083   (let ((data gnus-newsgroup-data))
3084     (save-excursion
3085       (gnus-save-hidden-threads
3086         (gnus-summary-show-all-threads)
3087         (goto-char (point-min))
3088         (while data
3089           (while (get-text-property (point) 'gnus-intangible)
3090             (forward-line 1))
3091           (gnus-data-set-pos (car data) (+ (point) 3))
3092           (setq data (cdr data))
3093           (forward-line 1))))))
3094
3095 (defun gnus-hidden-threads-configuration ()
3096   "Return the current hidden threads configuration."
3097   (save-excursion
3098     (let (config)
3099       (goto-char (point-min))
3100       (while (search-forward "\r" nil t)
3101         (push (1- (point)) config))
3102       config)))
3103
3104 (defun gnus-restore-hidden-threads-configuration (config)
3105   "Restore hidden threads configuration from CONFIG."
3106   (save-excursion
3107     (let (point buffer-read-only)
3108       (while (setq point (pop config))
3109         (when (and (< point (point-max))
3110                    (goto-char point)
3111                    (eq (char-after) ?\n))
3112           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3113
3114 ;; Various summary mode internalish functions.
3115
3116 (defun gnus-mouse-pick-article (e)
3117   (interactive "e")
3118   (mouse-set-point e)
3119   (gnus-summary-next-page nil t))
3120
3121 (defun gnus-summary-set-display-table ()
3122   "Change the display table.
3123 Odd characters have a tendency to mess
3124 up nicely formatted displays - we make all possible glyphs
3125 display only a single character."
3126
3127   ;; We start from the standard display table, if any.
3128   (let ((table (or (copy-sequence standard-display-table)
3129                    (make-display-table)))
3130         (i 32))
3131     ;; Nix out all the control chars...
3132     (while (>= (setq i (1- i)) 0)
3133       (aset table i [??]))
3134    ;; ... but not newline and cr, of course.  (cr is necessary for the
3135     ;; selective display).
3136     (aset table ?\n nil)
3137     (aset table ?\r nil)
3138     ;; We keep TAB as well.
3139     (aset table ?\t nil)
3140     ;; We nix out any glyphs over 126 that are not set already.
3141     (let ((i 256))
3142       (while (>= (setq i (1- i)) 127)
3143         ;; Only modify if the entry is nil.
3144         (unless (aref table i)
3145           (aset table i [??]))))
3146     (setq buffer-display-table table)))
3147
3148 (defun gnus-summary-set-article-display-arrow (pos)
3149   "Update the overlay arrow to point to line at position POS."
3150   (when (and gnus-summary-display-arrow
3151              (boundp 'overlay-arrow-position)
3152              (boundp 'overlay-arrow-string))
3153     (save-excursion
3154       (goto-char pos)
3155       (beginning-of-line)
3156       (unless overlay-arrow-position
3157         (setq overlay-arrow-position (make-marker)))
3158       (setq overlay-arrow-string "=>"
3159             overlay-arrow-position (set-marker overlay-arrow-position
3160                                                (point)
3161                                                (current-buffer))))))
3162
3163 (defun gnus-summary-setup-buffer (group)
3164   "Initialize summary buffer."
3165   (let ((buffer (gnus-summary-buffer-name group))
3166         (dead-name (concat "*Dead Summary "
3167                            (gnus-group-decoded-name group) "*")))
3168     ;; If a dead summary buffer exists, we kill it.
3169     (when (gnus-buffer-live-p dead-name)
3170       (gnus-kill-buffer dead-name))
3171     (if (get-buffer buffer)
3172         (progn
3173           (set-buffer buffer)
3174           (setq gnus-summary-buffer (current-buffer))
3175           (not gnus-newsgroup-prepared))
3176       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3177       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3178       (gnus-summary-mode group)
3179       (when gnus-carpal
3180         (gnus-carpal-setup-buffer 'summary))
3181       (unless gnus-single-article-buffer
3182         (make-local-variable 'gnus-article-buffer)
3183         (make-local-variable 'gnus-article-current)
3184         (make-local-variable 'gnus-original-article-buffer))
3185       (setq gnus-newsgroup-name group)
3186       ;; Set any local variables in the group parameters.
3187       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3188       t)))
3189
3190 (defun gnus-set-global-variables ()
3191   "Set the global equivalents of the buffer-local variables.
3192 They are set to the latest values they had.  These reflect the summary
3193 buffer that was in action when the last article was fetched."
3194   (when (eq major-mode 'gnus-summary-mode)
3195     (setq gnus-summary-buffer (current-buffer))
3196     (let ((name gnus-newsgroup-name)
3197           (marked gnus-newsgroup-marked)
3198           (spam gnus-newsgroup-spam-marked)
3199           (unread gnus-newsgroup-unreads)
3200           (headers gnus-current-headers)
3201           (data gnus-newsgroup-data)
3202           (summary gnus-summary-buffer)
3203           (article-buffer gnus-article-buffer)
3204           (original gnus-original-article-buffer)
3205           (gac gnus-article-current)
3206           (reffed gnus-reffed-article-number)
3207           (score-file gnus-current-score-file)
3208           (default-charset gnus-newsgroup-charset)
3209           vlist)
3210       (let ((locals gnus-newsgroup-variables))
3211         (while locals
3212           (if (consp (car locals))
3213               (push (eval (caar locals)) vlist)
3214             (push (eval (car locals)) vlist))
3215           (setq locals (cdr locals)))
3216         (setq vlist (nreverse vlist)))
3217       (with-current-buffer gnus-group-buffer
3218         (setq gnus-newsgroup-name name
3219               gnus-newsgroup-marked marked
3220               gnus-newsgroup-spam-marked spam
3221               gnus-newsgroup-unreads unread
3222               gnus-current-headers headers
3223               gnus-newsgroup-data data
3224               gnus-article-current gac
3225               gnus-summary-buffer summary
3226               gnus-article-buffer article-buffer
3227               gnus-original-article-buffer original
3228               gnus-reffed-article-number reffed
3229               gnus-current-score-file score-file
3230               gnus-newsgroup-charset default-charset)
3231         (let ((locals gnus-newsgroup-variables))
3232           (while locals
3233             (if (consp (car locals))
3234                 (set (caar locals) (pop vlist))
3235               (set (car locals) (pop vlist)))
3236             (setq locals (cdr locals))))
3237         ;; The article buffer also has local variables.
3238         (when (gnus-buffer-live-p gnus-article-buffer)
3239           (set-buffer gnus-article-buffer)
3240           (setq gnus-summary-buffer summary))))))
3241
3242 (defun gnus-summary-article-unread-p (article)
3243   "Say whether ARTICLE is unread or not."
3244   (memq article gnus-newsgroup-unreads))
3245
3246 (defun gnus-summary-first-article-p (&optional article)
3247   "Return whether ARTICLE is the first article in the buffer."
3248   (if (not (setq article (or article (gnus-summary-article-number))))
3249       nil
3250     (eq article (caar gnus-newsgroup-data))))
3251
3252 (defun gnus-summary-last-article-p (&optional article)
3253   "Return whether ARTICLE is the last article in the buffer."
3254   (if (not (setq article (or article (gnus-summary-article-number))))
3255       ;; All non-existent numbers are the last article.  :-)
3256       t
3257     (not (cdr (gnus-data-find-list article)))))
3258
3259 (defun gnus-make-thread-indent-array ()
3260   (let ((n 200))
3261     (unless (and gnus-thread-indent-array
3262                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3263       (setq gnus-thread-indent-array (make-vector 201 "")
3264             gnus-thread-indent-array-level gnus-thread-indent-level)
3265       (while (>= n 0)
3266         (aset gnus-thread-indent-array n
3267               (make-string (* n gnus-thread-indent-level) ? ))
3268         (setq n (1- n))))))
3269
3270 (defun gnus-update-summary-mark-positions ()
3271   "Compute where the summary marks are to go."
3272   (save-excursion
3273     (when (gnus-buffer-exists-p gnus-summary-buffer)
3274       (set-buffer gnus-summary-buffer))
3275     (let ((gnus-replied-mark 129)
3276           (gnus-score-below-mark 130)
3277           (gnus-score-over-mark 130)
3278           (gnus-undownloaded-mark 131)
3279           (spec gnus-summary-line-format-spec)
3280           gnus-visual pos)
3281       (save-excursion
3282         (gnus-set-work-buffer)
3283         (let ((gnus-summary-line-format-spec spec)
3284               (gnus-newsgroup-downloadable '(0)))
3285           (gnus-summary-insert-line
3286            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3287            0 nil t 128 t nil "" nil 1)
3288           (goto-char (point-min))
3289           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3290                                              (- (point) (point-min) 1)))))
3291           (goto-char (point-min))
3292           (push (cons 'replied (and (search-forward "\201" nil t)
3293                                     (- (point) (point-min) 1)))
3294                 pos)
3295           (goto-char (point-min))
3296           (push (cons 'score (and (search-forward "\202" nil t)
3297                                   (- (point) (point-min) 1)))
3298                 pos)
3299           (goto-char (point-min))
3300           (push (cons 'download
3301                       (and (search-forward "\203" nil t)
3302                            (- (point) (point-min) 1)))
3303                 pos)))
3304       (setq gnus-summary-mark-positions pos))))
3305
3306 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3307   "Insert a dummy root in the summary buffer."
3308   (beginning-of-line)
3309   (gnus-add-text-properties
3310    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3311    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3312
3313 (defun gnus-summary-extract-address-component (from)
3314   (or (car (funcall gnus-extract-address-components from))
3315       from))
3316
3317 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3318   (let ((mail-parse-charset gnus-newsgroup-charset)
3319         ; Is it really necessary to do this next part for each summary line?
3320         ; Luckily, doesn't seem to slow things down much.
3321         (mail-parse-ignored-charsets
3322          (with-current-buffer gnus-summary-buffer
3323            gnus-newsgroup-ignored-charsets)))
3324     (or
3325      (and gnus-ignored-from-addresses
3326           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3327           (let ((extra-headers (mail-header-extra header))
3328                 to
3329                 newsgroups)
3330             (cond
3331              ((setq to (cdr (assq 'To extra-headers)))
3332               (concat "-> "
3333                       (inline
3334                         (gnus-summary-extract-address-component
3335                          (funcall gnus-decode-encoded-word-function to)))))
3336              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3337               (concat "=> " newsgroups)))))
3338      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3339
3340 (defun gnus-summary-insert-line (gnus-tmp-header
3341                                  gnus-tmp-level gnus-tmp-current
3342                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3343                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3344                                  &optional gnus-tmp-dummy gnus-tmp-score
3345                                  gnus-tmp-process)
3346   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3347          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3348          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3349          (gnus-tmp-score-char
3350           (if (or (null gnus-summary-default-score)
3351                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3352                       gnus-summary-zcore-fuzz))
3353               ?                         ;Whitespace
3354             (if (< gnus-tmp-score gnus-summary-default-score)
3355                 gnus-score-below-mark gnus-score-over-mark)))
3356          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3357          (gnus-tmp-replied
3358           (cond (gnus-tmp-process gnus-process-mark)
3359                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3360                  gnus-cached-mark)
3361                 (gnus-tmp-replied gnus-replied-mark)
3362                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3363                  gnus-forwarded-mark)
3364                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3365                  gnus-saved-mark)
3366                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3367                  gnus-recent-mark)
3368                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3369                  gnus-unseen-mark)
3370                 (t gnus-no-mark)))
3371          (gnus-tmp-downloaded
3372           (cond (undownloaded
3373                  gnus-undownloaded-mark)
3374                 (gnus-newsgroup-agentized
3375                  gnus-downloaded-mark)
3376                 (t
3377                  gnus-no-mark)))
3378          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3379          (gnus-tmp-name
3380           (cond
3381            ((string-match "<[^>]+> *$" gnus-tmp-from)
3382             (let ((beg (match-beginning 0)))
3383               (or (and (string-match "^\".+\"" gnus-tmp-from)
3384                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3385                   (substring gnus-tmp-from 0 beg))))
3386            ((string-match "(.+)" gnus-tmp-from)
3387             (substring gnus-tmp-from
3388                        (1+ (match-beginning 0)) (1- (match-end 0))))
3389            (t gnus-tmp-from)))
3390          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3391          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3392          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3393          (buffer-read-only nil))
3394     (when (string= gnus-tmp-name "")
3395       (setq gnus-tmp-name gnus-tmp-from))
3396     (unless (numberp gnus-tmp-lines)
3397       (setq gnus-tmp-lines -1))
3398     (if (= gnus-tmp-lines -1)
3399         (setq gnus-tmp-lines "?")
3400       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3401       (gnus-put-text-property
3402      (point)
3403      (progn (eval gnus-summary-line-format-spec) (point))
3404        'gnus-number gnus-tmp-number)
3405     (when (gnus-visual-p 'summary-highlight 'highlight)
3406       (forward-line -1)
3407       (gnus-run-hooks 'gnus-summary-update-hook)
3408       (forward-line 1))))
3409
3410 (defun gnus-summary-update-line (&optional dont-update)
3411   "Update summary line after change."
3412   (when (and gnus-summary-default-score
3413              (not gnus-summary-inhibit-highlight))
3414     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3415            (article (gnus-summary-article-number))
3416            (score (gnus-summary-article-score article)))
3417       (unless dont-update
3418         (if (and gnus-summary-mark-below
3419                  (< (gnus-summary-article-score)
3420                     gnus-summary-mark-below))
3421             ;; This article has a low score, so we mark it as read.
3422             (when (memq article gnus-newsgroup-unreads)
3423               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3424           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3425             ;; This article was previously marked as read on account
3426             ;; of a low score, but now it has risen, so we mark it as
3427             ;; unread.
3428             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3429         (gnus-summary-update-mark
3430          (if (or (null gnus-summary-default-score)
3431                  (<= (abs (- score gnus-summary-default-score))
3432                      gnus-summary-zcore-fuzz))
3433              ?                          ;Whitespace
3434            (if (< score gnus-summary-default-score)
3435                gnus-score-below-mark gnus-score-over-mark))
3436          'score))
3437       ;; Do visual highlighting.
3438       (when (gnus-visual-p 'summary-highlight 'highlight)
3439         (gnus-run-hooks 'gnus-summary-update-hook)))))
3440
3441 (defvar gnus-tmp-new-adopts nil)
3442
3443 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3444   "Return the number of articles in THREAD.
3445 This may be 0 in some cases -- if none of the articles in
3446 the thread are to be displayed."
3447   (let* ((number
3448          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3449           (cond
3450            ((not (listp thread))
3451             1)
3452            ((and (consp thread) (cdr thread))
3453             (apply
3454              '+ 1 (mapcar
3455                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3456            ((null thread)
3457             1)
3458            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3459             1)
3460            (t 0))))
3461     (when (and level (zerop level) gnus-tmp-new-adopts)
3462       (incf number
3463             (apply '+ (mapcar
3464                        'gnus-summary-number-of-articles-in-thread
3465                        gnus-tmp-new-adopts))))
3466     (if char
3467         (if (> number 1) gnus-not-empty-thread-mark
3468           gnus-empty-thread-mark)
3469       number)))
3470
3471 (defsubst gnus-summary-line-message-size (head)
3472   "Return pretty-printed version of message size.
3473 This function is intended to be used in
3474 `gnus-summary-line-format-alist'."
3475   (let ((c (or (mail-header-chars head) -1)))
3476     (cond ((< c 0) "n/a")               ; chars not available
3477           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3478           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3479           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3480           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3481
3482
3483 (defun gnus-summary-set-local-parameters (group)
3484   "Go through the local params of GROUP and set all variable specs in that list."
3485   (let ((params (gnus-group-find-parameter group))
3486         (vars '(quit-config))           ; Ignore quit-config.
3487         elem)
3488     (while params
3489       (setq elem (car params)
3490             params (cdr params))
3491       (and (consp elem)                 ; Has to be a cons.
3492            (consp (cdr elem))           ; The cdr has to be a list.
3493            (symbolp (car elem))         ; Has to be a symbol in there.
3494            (not (memq (car elem) vars))
3495            (ignore-errors               ; So we set it.
3496              (push (car elem) vars)
3497              (make-local-variable (car elem))
3498              (set (car elem) (eval (nth 1 elem))))))))
3499
3500 (defun gnus-summary-read-group (group &optional show-all no-article
3501                                       kill-buffer no-display backward
3502                                       select-articles)
3503   "Start reading news in newsgroup GROUP.
3504 If SHOW-ALL is non-nil, already read articles are also listed.
3505 If NO-ARTICLE is non-nil, no article is selected initially.
3506 If NO-DISPLAY, don't generate a summary buffer."
3507   (let (result)
3508     (while (and group
3509                 (null (setq result
3510                             (let ((gnus-auto-select-next nil))
3511                               (or (gnus-summary-read-group-1
3512                                    group show-all no-article
3513                                    kill-buffer no-display
3514                                    select-articles)
3515                                   (setq show-all nil
3516                                         select-articles nil)))))
3517                 (eq gnus-auto-select-next 'quietly))
3518       (set-buffer gnus-group-buffer)
3519       ;; The entry function called above goes to the next
3520       ;; group automatically, so we go two groups back
3521       ;; if we are searching for the previous group.
3522       (when backward
3523         (gnus-group-prev-unread-group 2))
3524       (if (not (equal group (gnus-group-group-name)))
3525           (setq group (gnus-group-group-name))
3526         (setq group nil)))
3527     result))
3528
3529 (defun gnus-summary-read-group-1 (group show-all no-article
3530                                         kill-buffer no-display
3531                                         &optional select-articles)
3532   ;; Killed foreign groups can't be entered.
3533   ;;  (when (and (not (gnus-group-native-p group))
3534   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3535   ;;    (error "Dead non-native groups can't be entered"))
3536   (gnus-message 5 "Retrieving newsgroup: %s..."
3537                 (gnus-group-decoded-name group))
3538   (let* ((new-group (gnus-summary-setup-buffer group))
3539          (quit-config (gnus-group-quit-config group))
3540          (did-select (and new-group (gnus-select-newsgroup
3541                                      group show-all select-articles))))
3542     (cond
3543      ;; This summary buffer exists already, so we just select it.
3544      ((not new-group)
3545       (gnus-set-global-variables)
3546       (when kill-buffer
3547         (gnus-kill-or-deaden-summary kill-buffer))
3548       (gnus-configure-windows 'summary 'force)
3549       (gnus-set-mode-line 'summary)
3550       (gnus-summary-position-point)
3551       (message "")
3552       t)
3553      ;; We couldn't select this group.
3554      ((null did-select)
3555       (when (and (eq major-mode 'gnus-summary-mode)
3556                  (not (equal (current-buffer) kill-buffer)))
3557         (kill-buffer (current-buffer))
3558         (if (not quit-config)
3559             (progn
3560               ;; Update the info -- marks might need to be removed,
3561               ;; for instance.
3562               (gnus-summary-update-info)
3563               (set-buffer gnus-group-buffer)
3564               (gnus-group-jump-to-group group)
3565               (gnus-group-next-unread-group 1))
3566           (gnus-handle-ephemeral-exit quit-config)))
3567       (let ((grpinfo (gnus-get-info group)))
3568         (if (null (gnus-info-read grpinfo))
3569             (gnus-message 3 "Group %s contains no messages"
3570                           (gnus-group-decoded-name group))
3571           (gnus-message 3 "Can't select group")))
3572       nil)
3573      ;; The user did a `C-g' while prompting for number of articles,
3574      ;; so we exit this group.
3575      ((eq did-select 'quit)
3576       (and (eq major-mode 'gnus-summary-mode)
3577            (not (equal (current-buffer) kill-buffer))
3578            (kill-buffer (current-buffer)))
3579       (when kill-buffer
3580         (gnus-kill-or-deaden-summary kill-buffer))
3581       (if (not quit-config)
3582           (progn
3583             (set-buffer gnus-group-buffer)
3584             (gnus-group-jump-to-group group)
3585             (gnus-group-next-unread-group 1)
3586             (gnus-configure-windows 'group 'force))
3587         (gnus-handle-ephemeral-exit quit-config))
3588       ;; Finally signal the quit.
3589       (signal 'quit nil))
3590      ;; The group was successfully selected.
3591      (t
3592       (gnus-set-global-variables)
3593       ;; Save the active value in effect when the group was entered.
3594       (setq gnus-newsgroup-active
3595             (gnus-copy-sequence
3596              (gnus-active gnus-newsgroup-name)))
3597       ;; You can change the summary buffer in some way with this hook.
3598       (gnus-run-hooks 'gnus-select-group-hook)
3599       (gnus-update-format-specifications
3600        nil 'summary 'summary-mode 'summary-dummy)
3601       (gnus-update-summary-mark-positions)
3602       ;; Do score processing.
3603       (when gnus-use-scoring
3604         (gnus-possibly-score-headers))
3605       ;; Check whether to fill in the gaps in the threads.
3606       (when gnus-build-sparse-threads
3607         (gnus-build-sparse-threads))
3608       ;; Find the initial limit.
3609       (if show-all
3610           (let ((gnus-newsgroup-dormant nil))
3611             (gnus-summary-initial-limit show-all))
3612         (gnus-summary-initial-limit show-all))
3613       ;; Generate the summary buffer.
3614       (unless no-display
3615         (gnus-summary-prepare))
3616       (when gnus-use-trees
3617         (gnus-tree-open group)
3618         (setq gnus-summary-highlight-line-function
3619               'gnus-tree-highlight-article))
3620       ;; If the summary buffer is empty, but there are some low-scored
3621       ;; articles or some excluded dormants, we include these in the
3622       ;; buffer.
3623       (when (and (zerop (buffer-size))
3624                  (not no-display))
3625         (cond (gnus-newsgroup-dormant
3626                (gnus-summary-limit-include-dormant))
3627               ((and gnus-newsgroup-scored show-all)
3628                (gnus-summary-limit-include-expunged t))))
3629       ;; Function `gnus-apply-kill-file' must be called in this hook.
3630       (gnus-run-hooks 'gnus-apply-kill-hook)
3631       (if (and (zerop (buffer-size))
3632                (not no-display))
3633           (progn
3634             ;; This newsgroup is empty.
3635             (gnus-summary-catchup-and-exit nil t)
3636             (gnus-message 6 "No unread news")
3637             (when kill-buffer
3638               (gnus-kill-or-deaden-summary kill-buffer))
3639             ;; Return nil from this function.
3640             nil)
3641         ;; Hide conversation thread subtrees.  We cannot do this in
3642         ;; gnus-summary-prepare-hook since kill processing may not
3643         ;; work with hidden articles.
3644         (gnus-summary-maybe-hide-threads)
3645         (when kill-buffer
3646           (gnus-kill-or-deaden-summary kill-buffer))
3647         (gnus-summary-auto-select-subject)
3648         ;; Show first unread article if requested.
3649         (if (and (not no-article)
3650                  (not no-display)
3651                  gnus-newsgroup-unreads
3652                  gnus-auto-select-first)
3653             (progn
3654               (gnus-configure-windows 'summary)
3655               (let ((art (gnus-summary-article-number)))
3656                 (unless (and (not gnus-plugged)
3657                              (or (memq art gnus-newsgroup-undownloaded)
3658                                  (memq art gnus-newsgroup-downloadable)))
3659                   (gnus-summary-goto-article art))))
3660           ;; Don't select any articles.
3661           (gnus-summary-position-point)
3662           (gnus-configure-windows 'summary 'force)
3663           (gnus-set-mode-line 'summary))
3664         (when (and gnus-auto-center-group
3665                    (get-buffer-window gnus-group-buffer t))
3666           ;; Gotta use windows, because recenter does weird stuff if
3667           ;; the current buffer ain't the displayed window.
3668           (let ((owin (selected-window)))
3669             (select-window (get-buffer-window gnus-group-buffer t))
3670             (when (gnus-group-goto-group group)
3671               (recenter))
3672             (select-window owin)))
3673         ;; Mark this buffer as "prepared".
3674         (setq gnus-newsgroup-prepared t)
3675         (gnus-run-hooks 'gnus-summary-prepared-hook)
3676         (unless (gnus-ephemeral-group-p group)
3677           (gnus-group-update-group group))
3678         t)))))
3679
3680 (defun gnus-summary-auto-select-subject ()
3681   "Select the subject line on initial group entry."
3682   (goto-char (point-min))
3683   (cond
3684    ((eq gnus-auto-select-subject 'best)
3685     (gnus-summary-best-unread-subject))
3686    ((eq gnus-auto-select-subject 'unread)
3687     (gnus-summary-first-unread-subject))
3688    ((eq gnus-auto-select-subject 'unseen)
3689     (gnus-summary-first-unseen-subject))
3690    ((eq gnus-auto-select-subject 'unseen-or-unread)
3691     (gnus-summary-first-unseen-or-unread-subject))
3692    ((eq gnus-auto-select-subject 'first)
3693     ;; Do nothing.
3694     )
3695    ((functionp gnus-auto-select-subject)
3696     (funcall gnus-auto-select-subject))))
3697
3698 (defun gnus-summary-prepare ()
3699   "Generate the summary buffer."
3700   (interactive)
3701   (let ((buffer-read-only nil))
3702     (erase-buffer)
3703     (setq gnus-newsgroup-data nil
3704           gnus-newsgroup-data-reverse nil)
3705     (gnus-run-hooks 'gnus-summary-generate-hook)
3706     ;; Generate the buffer, either with threads or without.
3707     (when gnus-newsgroup-headers
3708       (gnus-summary-prepare-threads
3709        (if gnus-show-threads
3710            (gnus-sort-gathered-threads
3711             (funcall gnus-summary-thread-gathering-function
3712                      (gnus-sort-threads
3713                       (gnus-cut-threads (gnus-make-threads)))))
3714          ;; Unthreaded display.
3715          (gnus-sort-articles gnus-newsgroup-headers))))
3716     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3717     ;; Call hooks for modifying summary buffer.
3718     (goto-char (point-min))
3719     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3720
3721 (defsubst gnus-general-simplify-subject (subject)
3722   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3723   (setq subject
3724         (cond
3725          ;; Truncate the subject.
3726          (gnus-simplify-subject-functions
3727           (gnus-map-function gnus-simplify-subject-functions subject))
3728          ((numberp gnus-summary-gather-subject-limit)
3729           (setq subject (gnus-simplify-subject-re subject))
3730           (if (> (length subject) gnus-summary-gather-subject-limit)
3731               (substring subject 0 gnus-summary-gather-subject-limit)
3732             subject))
3733          ;; Fuzzily simplify it.
3734          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3735           (gnus-simplify-subject-fuzzy subject))
3736          ;; Just remove the leading "Re:".
3737          (t
3738           (gnus-simplify-subject-re subject))))
3739
3740   (if (and gnus-summary-gather-exclude-subject
3741            (string-match gnus-summary-gather-exclude-subject subject))
3742       nil                         ; This article shouldn't be gathered
3743     subject))
3744
3745 (defun gnus-summary-simplify-subject-query ()
3746   "Query where the respool algorithm would put this article."
3747   (interactive)
3748   (gnus-summary-select-article)
3749   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3750
3751 (defun gnus-gather-threads-by-subject (threads)
3752   "Gather threads by looking at Subject headers."
3753   (if (not gnus-summary-make-false-root)
3754       threads
3755     (let ((hashtb (gnus-make-hashtable 1024))
3756           (prev threads)
3757           (result threads)
3758           subject hthread whole-subject)
3759       (while threads
3760         (setq subject (gnus-general-simplify-subject
3761                        (setq whole-subject (mail-header-subject
3762                                             (caar threads)))))
3763         (when subject
3764           (if (setq hthread (gnus-gethash subject hashtb))
3765               (progn
3766                 ;; We enter a dummy root into the thread, if we
3767                 ;; haven't done that already.
3768                 (unless (stringp (caar hthread))
3769                   (setcar hthread (list whole-subject (car hthread))))
3770                 ;; We add this new gathered thread to this gathered
3771                 ;; thread.
3772                 (setcdr (car hthread)
3773                         (nconc (cdar hthread) (list (car threads))))
3774                 ;; Remove it from the list of threads.
3775                 (setcdr prev (cdr threads))
3776                 (setq threads prev))
3777             ;; Enter this thread into the hash table.
3778             (gnus-sethash subject
3779                           (if gnus-summary-make-false-root-always
3780                               (progn
3781                                 ;; If you want a dummy root above all
3782                                 ;; threads...
3783                                 (setcar threads (list whole-subject
3784                                                       (car threads)))
3785                                 threads)
3786                             threads)
3787                           hashtb)))
3788         (setq prev threads)
3789         (setq threads (cdr threads)))
3790       result)))
3791
3792 (defun gnus-gather-threads-by-references (threads)
3793   "Gather threads by looking at References headers."
3794   (let ((idhashtb (gnus-make-hashtable 1024))
3795         (thhashtb (gnus-make-hashtable 1024))
3796         (prev threads)
3797         (result threads)
3798         ids references id gthread gid entered ref)
3799     (while threads
3800       (when (setq references (mail-header-references (caar threads)))
3801         (setq id (mail-header-id (caar threads))
3802               ids (inline (gnus-split-references references))
3803               entered nil)
3804         (while (setq ref (pop ids))
3805           (setq ids (delete ref ids))
3806           (if (not (setq gid (gnus-gethash ref idhashtb)))
3807               (progn
3808                 (gnus-sethash ref id idhashtb)
3809                 (gnus-sethash id threads thhashtb))
3810             (setq gthread (gnus-gethash gid thhashtb))
3811             (unless entered
3812               ;; We enter a dummy root into the thread, if we
3813               ;; haven't done that already.
3814               (unless (stringp (caar gthread))
3815                 (setcar gthread (list (mail-header-subject (caar gthread))
3816                                       (car gthread))))
3817               ;; We add this new gathered thread to this gathered
3818               ;; thread.
3819               (setcdr (car gthread)
3820                       (nconc (cdar gthread) (list (car threads)))))
3821             ;; Add it into the thread hash table.
3822             (gnus-sethash id gthread thhashtb)
3823             (setq entered t)
3824             ;; Remove it from the list of threads.
3825             (setcdr prev (cdr threads))
3826             (setq threads prev))))
3827       (setq prev threads)
3828       (setq threads (cdr threads)))
3829     result))
3830
3831 (defun gnus-sort-gathered-threads (threads)
3832   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3833   (let ((result threads))
3834     (while threads
3835       (when (stringp (caar threads))
3836         (setcdr (car threads)
3837                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3838       (setq threads (cdr threads)))
3839     result))
3840
3841 (defun gnus-thread-loop-p (root thread)
3842   "Say whether ROOT is in THREAD."
3843   (let ((stack (list thread))
3844         (infloop 0)
3845         th)
3846     (while (setq thread (pop stack))
3847       (setq th (cdr thread))
3848       (while (and th
3849                   (not (eq (caar th) root)))
3850         (pop th))
3851       (if th
3852           ;; We have found a loop.
3853           (let (ref-dep)
3854             (setcdr thread (delq (car th) (cdr thread)))
3855             (if (boundp (setq ref-dep (intern "none"
3856                                               gnus-newsgroup-dependencies)))
3857                 (setcdr (symbol-value ref-dep)
3858                         (nconc (cdr (symbol-value ref-dep))
3859                                (list (car th))))
3860               (set ref-dep (list nil (car th))))
3861             (setq infloop 1
3862                   stack nil))
3863         ;; Push all the subthreads onto the stack.
3864         (push (cdr thread) stack)))
3865     infloop))
3866
3867 (defun gnus-make-threads ()
3868   "Go through the dependency hashtb and find the roots.  Return all threads."
3869   (let (threads)
3870     (while (catch 'infloop
3871              (mapatoms
3872               (lambda (refs)
3873                 ;; Deal with self-referencing References loops.
3874                 (when (and (car (symbol-value refs))
3875                            (not (zerop
3876                                  (apply
3877                                   '+
3878                                   (mapcar
3879                                    (lambda (thread)
3880                                      (gnus-thread-loop-p
3881                                       (car (symbol-value refs)) thread))
3882                                    (cdr (symbol-value refs)))))))
3883                   (setq threads nil)
3884                   (throw 'infloop t))
3885                 (unless (car (symbol-value refs))
3886                   ;; These threads do not refer back to any other
3887                   ;; articles, so they're roots.
3888                   (setq threads (append (cdr (symbol-value refs)) threads))))
3889               gnus-newsgroup-dependencies)))
3890     threads))
3891
3892 ;; Build the thread tree.
3893 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3894   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3895
3896 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3897 if it was already present.
3898
3899 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3900 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3901 Message-IDs will be renamed to a unique Message-ID before being
3902 entered.
3903
3904 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3905   (let* ((id (mail-header-id header))
3906          (id-dep (and id (intern id dependencies)))
3907          parent-id ref ref-dep ref-header replaced)
3908     ;; Enter this `header' in the `dependencies' table.
3909     (cond
3910      ((not id-dep)
3911       (setq header nil))
3912      ;; The first two cases do the normal part: enter a new `header'
3913      ;; in the `dependencies' table.
3914      ((not (boundp id-dep))
3915       (set id-dep (list header)))
3916      ((null (car (symbol-value id-dep)))
3917       (setcar (symbol-value id-dep) header))
3918
3919      ;; From here the `header' was already present in the
3920      ;; `dependencies' table.
3921      (force-new
3922       ;; Overrides an existing entry;
3923       ;; just set the header part of the entry.
3924       (setcar (symbol-value id-dep) header)
3925       (setq replaced t))
3926
3927      ;; Renames the existing `header' to a unique Message-ID.
3928      ((not gnus-summary-ignore-duplicates)
3929       ;; An article with this Message-ID has already been seen.
3930       ;; We rename the Message-ID.
3931       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3932            (list header))
3933       (mail-header-set-id header id))
3934
3935      ;; The last case ignores an existing entry, except it adds any
3936      ;; additional Xrefs (in case the two articles came from different
3937      ;; servers.
3938      ;; Also sets `header' to `nil' meaning that the `dependencies'
3939      ;; table was *not* modified.
3940      (t
3941       (mail-header-set-xref
3942        (car (symbol-value id-dep))
3943        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3944                    "")
3945                (or (mail-header-xref header) "")))
3946       (setq header nil)))
3947
3948     (when (and header (not replaced))
3949       ;; First check that we are not creating a References loop.
3950       (setq parent-id (gnus-parent-id (mail-header-references header)))
3951       (setq ref parent-id)
3952       (while (and ref
3953                   (setq ref-dep (intern-soft ref dependencies))
3954                   (boundp ref-dep)
3955                   (setq ref-header (car (symbol-value ref-dep))))
3956         (if (string= id ref)
3957             ;; Yuk!  This is a reference loop.  Make the article be a
3958             ;; root article.
3959             (progn
3960               (mail-header-set-references (car (symbol-value id-dep)) "none")
3961               (setq ref nil)
3962               (setq parent-id nil))
3963           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3964       (setq ref-dep (intern (or parent-id "none") dependencies))
3965       (if (boundp ref-dep)
3966           (setcdr (symbol-value ref-dep)
3967                   (nconc (cdr (symbol-value ref-dep))
3968                          (list (symbol-value id-dep))))
3969         (set ref-dep (list nil (symbol-value id-dep)))))
3970     header))
3971
3972 (defun gnus-extract-message-id-from-in-reply-to (string)
3973   (if (string-match "<[^>]+>" string)
3974       (substring string (match-beginning 0) (match-end 0))
3975     nil))
3976
3977 (defun gnus-build-sparse-threads ()
3978   (let ((headers gnus-newsgroup-headers)
3979         (mail-parse-charset gnus-newsgroup-charset)
3980         (gnus-summary-ignore-duplicates t)
3981         header references generation relations
3982         subject child end new-child date)
3983     ;; First we create an alist of generations/relations, where
3984     ;; generations is how much we trust the relation, and the relation
3985     ;; is parent/child.
3986     (gnus-message 7 "Making sparse threads...")
3987     (save-excursion
3988       (nnheader-set-temp-buffer " *gnus sparse threads*")
3989       (while (setq header (pop headers))
3990         (when (and (setq references (mail-header-references header))
3991                    (not (string= references "")))
3992           (insert references)
3993           (setq child (mail-header-id header)
3994                 subject (mail-header-subject header)
3995                 date (mail-header-date header)
3996                 generation 0)
3997           (while (search-backward ">" nil t)
3998             (setq end (1+ (point)))
3999             (when (search-backward "<" nil t)
4000               (setq new-child (buffer-substring (point) end))
4001               (push (list (incf generation)
4002                           child (setq child new-child)
4003                           subject date)
4004                     relations)))
4005           (when child
4006             (push (list (1+ generation) child nil subject) relations))
4007           (erase-buffer)))
4008       (kill-buffer (current-buffer)))
4009     ;; Sort over trustworthiness.
4010     (mapcar
4011      (lambda (relation)
4012        (when (gnus-dependencies-add-header
4013               (make-full-mail-header
4014                gnus-reffed-article-number
4015                (nth 3 relation) "" (or (nth 4 relation) "")
4016                (nth 1 relation)
4017                (or (nth 2 relation) "") 0 0 "")
4018               gnus-newsgroup-dependencies nil)
4019          (push gnus-reffed-article-number gnus-newsgroup-limit)
4020          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4021          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4022                gnus-newsgroup-reads)
4023          (decf gnus-reffed-article-number)))
4024      (sort relations 'car-less-than-car))
4025     (gnus-message 7 "Making sparse threads...done")))
4026
4027 (defun gnus-build-old-threads ()
4028   ;; Look at all the articles that refer back to old articles, and
4029   ;; fetch the headers for the articles that aren't there.  This will
4030   ;; build complete threads - if the roots haven't been expired by the
4031   ;; server, that is.
4032   (let ((mail-parse-charset gnus-newsgroup-charset)
4033         id heads)
4034     (mapatoms
4035      (lambda (refs)
4036        (when (not (car (symbol-value refs)))
4037          (setq heads (cdr (symbol-value refs)))
4038          (while heads
4039            (if (memq (mail-header-number (caar heads))
4040                      gnus-newsgroup-dormant)
4041                (setq heads (cdr heads))
4042              (setq id (symbol-name refs))
4043              (while (and (setq id (gnus-build-get-header id))
4044                          (not (car (gnus-id-to-thread id)))))
4045              (setq heads nil)))))
4046      gnus-newsgroup-dependencies)))
4047
4048 (defsubst gnus-remove-odd-characters (string)
4049   "Translate STRING into something that doesn't contain weird characters."
4050   (mm-subst-char-in-string
4051    ?\r ?\-
4052    (mm-subst-char-in-string ?\n ?\- string t) t))
4053
4054 ;; This function has to be called with point after the article number
4055 ;; on the beginning of the line.
4056 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4057   (let ((eol (point-at-eol))
4058         (buffer (current-buffer))
4059         header references in-reply-to)
4060
4061     ;; overview: [num subject from date id refs chars lines misc]
4062     (unwind-protect
4063         (let (x)
4064           (narrow-to-region (point) eol)
4065           (unless (eobp)
4066             (forward-char))
4067
4068           (setq header
4069                 (make-full-mail-header
4070                  number                 ; number
4071                  (condition-case ()     ; subject
4072                      (gnus-remove-odd-characters
4073                       (funcall gnus-decode-encoded-word-function
4074                                (setq x (nnheader-nov-field))))
4075                    (error x))
4076                  (condition-case ()     ; from
4077                      (gnus-remove-odd-characters
4078                       (funcall gnus-decode-encoded-word-function
4079                                (setq x (nnheader-nov-field))))
4080                    (error x))
4081                  (nnheader-nov-field)   ; date
4082                  (nnheader-nov-read-message-id number)  ; id
4083                  (setq references (nnheader-nov-field)) ; refs
4084                  (nnheader-nov-read-integer) ; chars
4085                  (nnheader-nov-read-integer) ; lines
4086                  (unless (eobp)
4087                    (if (looking-at "Xref: ")
4088                        (goto-char (match-end 0)))
4089                    (nnheader-nov-field)) ; Xref
4090                  (nnheader-nov-parse-extra)))) ; extra
4091
4092       (widen))
4093
4094     (when (and (string= references "")
4095                (setq in-reply-to (mail-header-extra header))
4096                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4097       (mail-header-set-references
4098        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4099
4100     (when gnus-alter-header-function
4101       (funcall gnus-alter-header-function header))
4102     (gnus-dependencies-add-header header dependencies force-new)))
4103
4104 (defun gnus-build-get-header (id)
4105   "Look through the buffer of NOV lines and find the header to ID.
4106 Enter this line into the dependencies hash table, and return
4107 the id of the parent article (if any)."
4108   (let ((deps gnus-newsgroup-dependencies)
4109         found header)
4110     (prog1
4111         (save-excursion
4112           (set-buffer nntp-server-buffer)
4113           (let ((case-fold-search nil))
4114             (goto-char (point-min))
4115             (while (and (not found)
4116                         (search-forward id nil t))
4117               (beginning-of-line)
4118               (setq found (looking-at
4119                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4120                                    (regexp-quote id))))
4121               (or found (beginning-of-line 2)))
4122             (when found
4123               (beginning-of-line)
4124               (and
4125                (setq header (gnus-nov-parse-line
4126                              (read (current-buffer)) deps))
4127                (gnus-parent-id (mail-header-references header))))))
4128       (when header
4129         (let ((number (mail-header-number header)))
4130           (push number gnus-newsgroup-limit)
4131           (push header gnus-newsgroup-headers)
4132           (if (memq number gnus-newsgroup-unselected)
4133               (progn
4134                 (setq gnus-newsgroup-unreads
4135                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4136                                                number))
4137                 (setq gnus-newsgroup-unselected
4138                       (delq number gnus-newsgroup-unselected)))
4139             (push number gnus-newsgroup-ancient)))))))
4140
4141 (defun gnus-build-all-threads ()
4142   "Read all the headers."
4143   (let ((gnus-summary-ignore-duplicates t)
4144         (mail-parse-charset gnus-newsgroup-charset)
4145         (dependencies gnus-newsgroup-dependencies)
4146         header article)
4147     (save-excursion
4148       (set-buffer nntp-server-buffer)
4149       (let ((case-fold-search nil))
4150         (goto-char (point-min))
4151         (while (not (eobp))
4152           (ignore-errors
4153             (setq article (read (current-buffer))
4154                   header (gnus-nov-parse-line article dependencies)))
4155           (when header
4156             (with-current-buffer gnus-summary-buffer
4157               (push header gnus-newsgroup-headers)
4158               (if (memq (setq article (mail-header-number header))
4159                         gnus-newsgroup-unselected)
4160                   (progn
4161                     (setq gnus-newsgroup-unreads
4162                           (gnus-add-to-sorted-list
4163                            gnus-newsgroup-unreads article))
4164                     (setq gnus-newsgroup-unselected
4165                           (delq article gnus-newsgroup-unselected)))
4166                 (push article gnus-newsgroup-ancient)))
4167             (forward-line 1)))))))
4168
4169 (defun gnus-summary-update-article-line (article header)
4170   "Update the line for ARTICLE using HEADER."
4171   (let* ((id (mail-header-id header))
4172          (thread (gnus-id-to-thread id)))
4173     (unless thread
4174       (error "Article in no thread"))
4175     ;; Update the thread.
4176     (setcar thread header)
4177     (gnus-summary-goto-subject article)
4178     (let* ((datal (gnus-data-find-list article))
4179            (data (car datal))
4180            (buffer-read-only nil)
4181            (level (gnus-summary-thread-level)))
4182       (gnus-delete-line)
4183       (let ((inserted (- (point)
4184                          (progn
4185                            (gnus-summary-insert-line
4186                             header level nil
4187                             (memq article gnus-newsgroup-undownloaded)
4188                             (gnus-article-mark article)
4189                             (memq article gnus-newsgroup-replied)
4190                             (memq article gnus-newsgroup-expirable)
4191                             ;; Only insert the Subject string when it's different
4192                             ;; from the previous Subject string.
4193                             (if (and
4194                                  gnus-show-threads
4195                                  (gnus-subject-equal
4196                                   (condition-case ()
4197                                       (mail-header-subject
4198                                        (gnus-data-header
4199                                         (cadr
4200                                          (gnus-data-find-list
4201                                           article
4202                                           (gnus-data-list t)))))
4203                                     ;; Error on the side of excessive subjects.
4204                                     (error ""))
4205                                   (mail-header-subject header)))
4206                                 ""
4207                               (mail-header-subject header))
4208                             nil (cdr (assq article gnus-newsgroup-scored))
4209                             (memq article gnus-newsgroup-processable))
4210                            (point)))))
4211         (when (cdr datal)
4212           (gnus-data-update-list
4213            (cdr datal)
4214            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4215
4216 (defun gnus-summary-update-article (article &optional iheader)
4217   "Update ARTICLE in the summary buffer."
4218   (set-buffer gnus-summary-buffer)
4219   (let* ((header (gnus-summary-article-header article))
4220          (id (mail-header-id header))
4221          (data (gnus-data-find article))
4222          (thread (gnus-id-to-thread id))
4223          (references (mail-header-references header))
4224          (parent
4225           (gnus-id-to-thread
4226            (or (gnus-parent-id
4227                 (when (and references
4228                            (not (equal "" references)))
4229                   references))
4230                "none")))
4231          (buffer-read-only nil)
4232          (old (car thread)))
4233     (when thread
4234       (unless iheader
4235         (setcar thread nil)
4236         (when parent
4237           (delq thread parent)))
4238       (if (gnus-summary-insert-subject id header)
4239           ;; Set the (possibly) new article number in the data structure.
4240           (gnus-data-set-number data (gnus-id-to-article id))
4241         (setcar thread old)
4242         nil))))
4243
4244 (defun gnus-rebuild-thread (id &optional line)
4245   "Rebuild the thread containing ID.
4246 If LINE, insert the rebuilt thread starting on line LINE."
4247   (let ((buffer-read-only nil)
4248         old-pos current thread data)
4249     (if (not gnus-show-threads)
4250         (setq thread (list (car (gnus-id-to-thread id))))
4251       ;; Get the thread this article is part of.
4252       (setq thread (gnus-remove-thread id)))
4253     (setq old-pos (point-at-bol))
4254     (setq current (save-excursion
4255                     (and (re-search-backward "[\r\n]" nil t)
4256                          (gnus-summary-article-number))))
4257     ;; If this is a gathered thread, we have to go some re-gathering.
4258     (when (stringp (car thread))
4259       (let ((subject (car thread))
4260             roots thr)
4261         (setq thread (cdr thread))
4262         (while thread
4263           (unless (memq (setq thr (gnus-id-to-thread
4264                                    (gnus-root-id
4265                                     (mail-header-id (caar thread)))))
4266                         roots)
4267             (push thr roots))
4268           (setq thread (cdr thread)))
4269         ;; We now have all (unique) roots.
4270         (if (= (length roots) 1)
4271             ;; All the loose roots are now one solid root.
4272             (setq thread (car roots))
4273           (setq thread (cons subject (gnus-sort-threads roots))))))
4274     (let (threads)
4275       ;; We then insert this thread into the summary buffer.
4276       (when line
4277         (goto-char (point-min))
4278         (forward-line (1- line)))
4279       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4280         (if gnus-show-threads
4281             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4282           (gnus-summary-prepare-unthreaded thread))
4283         (setq data (nreverse gnus-newsgroup-data))
4284         (setq threads gnus-newsgroup-threads))
4285       ;; We splice the new data into the data structure.
4286       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4287       ;;!!! then we want to insert at the beginning of the buffer.
4288       ;;!!! That happens to be true with Gnus now, but that may
4289       ;;!!! change in the future.  Perhaps.
4290       (gnus-data-enter-list
4291        (if line nil current) data (- (point) old-pos))
4292       (setq gnus-newsgroup-threads
4293             (nconc threads gnus-newsgroup-threads))
4294       (gnus-data-compute-positions))))
4295
4296 (defun gnus-number-to-header (number)
4297   "Return the header for article NUMBER."
4298   (let ((headers gnus-newsgroup-headers))
4299     (while (and headers
4300                 (not (= number (mail-header-number (car headers)))))
4301       (pop headers))
4302     (when headers
4303       (car headers))))
4304
4305 (defun gnus-parent-headers (in-headers &optional generation)
4306   "Return the headers of the GENERATIONeth parent of HEADERS."
4307   (unless generation
4308     (setq generation 1))
4309   (let ((parent t)
4310         (headers in-headers)
4311         references)
4312     (while (and parent
4313                 (not (zerop generation))
4314                 (setq references (mail-header-references headers)))
4315       (setq headers (if (and references
4316                              (setq parent (gnus-parent-id references)))
4317                         (car (gnus-id-to-thread parent))
4318                       nil))
4319       (decf generation))
4320     (and (not (eq headers in-headers))
4321          headers)))
4322
4323 (defun gnus-id-to-thread (id)
4324   "Return the (sub-)thread where ID appears."
4325   (gnus-gethash id gnus-newsgroup-dependencies))
4326
4327 (defun gnus-id-to-article (id)
4328   "Return the article number of ID."
4329   (let ((thread (gnus-id-to-thread id)))
4330     (when (and thread
4331                (car thread))
4332       (mail-header-number (car thread)))))
4333
4334 (defun gnus-id-to-header (id)
4335   "Return the article headers of ID."
4336   (car (gnus-id-to-thread id)))
4337
4338 (defun gnus-article-displayed-root-p (article)
4339   "Say whether ARTICLE is a root(ish) article."
4340   (let ((level (gnus-summary-thread-level article))
4341         (refs (mail-header-references  (gnus-summary-article-header article)))
4342         particle)
4343     (cond
4344      ((null level) nil)
4345      ((zerop level) t)
4346      ((null refs) t)
4347      ((null (gnus-parent-id refs)) t)
4348      ((and (= 1 level)
4349            (null (setq particle (gnus-id-to-article
4350                                  (gnus-parent-id refs))))
4351            (null (gnus-summary-thread-level particle)))))))
4352
4353 (defun gnus-root-id (id)
4354   "Return the id of the root of the thread where ID appears."
4355   (let (last-id prev)
4356     (while (and id (setq prev (car (gnus-id-to-thread id))))
4357       (setq last-id id
4358             id (gnus-parent-id (mail-header-references prev))))
4359     last-id))
4360
4361 (defun gnus-articles-in-thread (thread)
4362   "Return the list of articles in THREAD."
4363   (cons (mail-header-number (car thread))
4364         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4365
4366 (defun gnus-remove-thread (id &optional dont-remove)
4367   "Remove the thread that has ID in it."
4368   (let (headers thread last-id)
4369     ;; First go up in this thread until we find the root.
4370     (setq last-id (gnus-root-id id)
4371           headers (message-flatten-list (gnus-id-to-thread last-id)))
4372     ;; We have now found the real root of this thread.  It might have
4373     ;; been gathered into some loose thread, so we have to search
4374     ;; through the threads to find the thread we wanted.
4375     (let ((threads gnus-newsgroup-threads)
4376           sub)
4377       (while threads
4378         (setq sub (car threads))
4379         (if (stringp (car sub))
4380             ;; This is a gathered thread, so we look at the roots
4381             ;; below it to find whether this article is in this
4382             ;; gathered root.
4383             (progn
4384               (setq sub (cdr sub))
4385               (while sub
4386                 (when (member (caar sub) headers)
4387                   (setq thread (car threads)
4388                         threads nil
4389                         sub nil))
4390                 (setq sub (cdr sub))))
4391           ;; It's an ordinary thread, so we check it.
4392           (when (eq (car sub) (car headers))
4393             (setq thread sub
4394                   threads nil)))
4395         (setq threads (cdr threads)))
4396       ;; If this article is in no thread, then it's a root.
4397       (if thread
4398           (unless dont-remove
4399             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4400         (setq thread (gnus-id-to-thread last-id)))
4401       (when thread
4402         (prog1
4403             thread                      ; We return this thread.
4404           (unless dont-remove
4405             (if (stringp (car thread))
4406                 (progn
4407                   ;; If we use dummy roots, then we have to remove the
4408                   ;; dummy root as well.
4409                   (when (eq gnus-summary-make-false-root 'dummy)
4410                     ;; We go to the dummy root by going to
4411                     ;; the first sub-"thread", and then one line up.
4412                     (gnus-summary-goto-article
4413                      (mail-header-number (caadr thread)))
4414                     (forward-line -1)
4415                     (gnus-delete-line)
4416                     (gnus-data-compute-positions))
4417                   (setq thread (cdr thread))
4418                   (while thread
4419                     (gnus-remove-thread-1 (car thread))
4420                     (setq thread (cdr thread))))
4421               (gnus-remove-thread-1 thread))))))))
4422
4423 (defun gnus-remove-thread-1 (thread)
4424   "Remove the thread THREAD recursively."
4425   (let ((number (mail-header-number (pop thread)))
4426         d)
4427     (setq thread (reverse thread))
4428     (while thread
4429       (gnus-remove-thread-1 (pop thread)))
4430     (when (setq d (gnus-data-find number))
4431       (goto-char (gnus-data-pos d))
4432       (gnus-summary-show-thread)
4433       (gnus-data-remove
4434        number
4435        (- (point-at-bol)
4436           (prog1
4437               (1+ (point-at-eol))
4438             (gnus-delete-line)))))))
4439
4440 (defun gnus-sort-threads-1 (threads func)
4441   (sort (mapcar (lambda (thread)
4442                   (cons (car thread)
4443                         (and (cdr thread)
4444                              (gnus-sort-threads-1 (cdr thread) func))))
4445                 threads) func))
4446
4447 (defun gnus-sort-threads (threads)
4448   "Sort THREADS."
4449   (if (not gnus-thread-sort-functions)
4450       threads
4451     (gnus-message 8 "Sorting threads...")
4452     (let ((max-lisp-eval-depth 5000))
4453       (prog1 (gnus-sort-threads-1
4454          threads
4455          (gnus-make-sort-function gnus-thread-sort-functions))
4456         (gnus-message 8 "Sorting threads...done")))))
4457
4458 (defun gnus-sort-articles (articles)
4459   "Sort ARTICLES."
4460   (when gnus-article-sort-functions
4461     (gnus-message 7 "Sorting articles...")
4462     (prog1
4463         (setq gnus-newsgroup-headers
4464               (sort articles (gnus-make-sort-function
4465                               gnus-article-sort-functions)))
4466       (gnus-message 7 "Sorting articles...done"))))
4467
4468 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4469 (defmacro gnus-thread-header (thread)
4470   "Return header of first article in THREAD.
4471 Note that THREAD must never, ever be anything else than a variable -
4472 using some other form will lead to serious barfage."
4473   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4474   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4475   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4476         (vector thread) 2))
4477
4478 (defsubst gnus-article-sort-by-number (h1 h2)
4479   "Sort articles by article number."
4480   (< (mail-header-number h1)
4481      (mail-header-number h2)))
4482
4483 (defun gnus-thread-sort-by-number (h1 h2)
4484   "Sort threads by root article number."
4485   (gnus-article-sort-by-number
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defsubst gnus-article-sort-by-random (h1 h2)
4489   "Sort articles by article number."
4490   (zerop (random 2)))
4491
4492 (defun gnus-thread-sort-by-random (h1 h2)
4493   "Sort threads by root article number."
4494   (gnus-article-sort-by-random
4495    (gnus-thread-header h1) (gnus-thread-header h2)))
4496
4497 (defsubst gnus-article-sort-by-lines (h1 h2)
4498   "Sort articles by article Lines header."
4499   (< (mail-header-lines h1)
4500      (mail-header-lines h2)))
4501
4502 (defun gnus-thread-sort-by-lines (h1 h2)
4503   "Sort threads by root article Lines header."
4504   (gnus-article-sort-by-lines
4505    (gnus-thread-header h1) (gnus-thread-header h2)))
4506
4507 (defsubst gnus-article-sort-by-chars (h1 h2)
4508   "Sort articles by octet length."
4509   (< (mail-header-chars h1)
4510      (mail-header-chars h2)))
4511
4512 (defun gnus-thread-sort-by-chars (h1 h2)
4513   "Sort threads by root article octet length."
4514   (gnus-article-sort-by-chars
4515    (gnus-thread-header h1) (gnus-thread-header h2)))
4516
4517 (defsubst gnus-article-sort-by-author (h1 h2)
4518   "Sort articles by root author."
4519   (string-lessp
4520    (let ((extract (funcall
4521                    gnus-extract-address-components
4522                    (mail-header-from h1))))
4523      (or (car extract) (cadr extract) ""))
4524    (let ((extract (funcall
4525                    gnus-extract-address-components
4526                    (mail-header-from h2))))
4527      (or (car extract) (cadr extract) ""))))
4528
4529 (defun gnus-thread-sort-by-author (h1 h2)
4530   "Sort threads by root author."
4531   (gnus-article-sort-by-author
4532    (gnus-thread-header h1)  (gnus-thread-header h2)))
4533
4534 (defsubst gnus-article-sort-by-subject (h1 h2)
4535   "Sort articles by root subject."
4536   (string-lessp
4537    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4538    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4539
4540 (defun gnus-thread-sort-by-subject (h1 h2)
4541   "Sort threads by root subject."
4542   (gnus-article-sort-by-subject
4543    (gnus-thread-header h1) (gnus-thread-header h2)))
4544
4545 (defsubst gnus-article-sort-by-date (h1 h2)
4546   "Sort articles by root article date."
4547   (time-less-p
4548    (gnus-date-get-time (mail-header-date h1))
4549    (gnus-date-get-time (mail-header-date h2))))
4550
4551 (defun gnus-thread-sort-by-date (h1 h2)
4552   "Sort threads by root article date."
4553   (gnus-article-sort-by-date
4554    (gnus-thread-header h1) (gnus-thread-header h2)))
4555
4556 (defsubst gnus-article-sort-by-score (h1 h2)
4557   "Sort articles by root article score.
4558 Unscored articles will be counted as having a score of zero."
4559   (> (or (cdr (assq (mail-header-number h1)
4560                     gnus-newsgroup-scored))
4561          gnus-summary-default-score 0)
4562      (or (cdr (assq (mail-header-number h2)
4563                     gnus-newsgroup-scored))
4564          gnus-summary-default-score 0)))
4565
4566 (defun gnus-thread-sort-by-score (h1 h2)
4567   "Sort threads by root article score."
4568   (gnus-article-sort-by-score
4569    (gnus-thread-header h1) (gnus-thread-header h2)))
4570
4571 (defun gnus-thread-sort-by-total-score (h1 h2)
4572   "Sort threads by the sum of all scores in the thread.
4573 Unscored articles will be counted as having a score of zero."
4574   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4575
4576 (defun gnus-thread-total-score (thread)
4577   ;; This function find the total score of THREAD.
4578   (cond
4579    ((null thread)
4580     0)
4581    ((consp thread)
4582     (if (stringp (car thread))
4583         (apply gnus-thread-score-function 0
4584                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4585       (gnus-thread-total-score-1 thread)))
4586    (t
4587     (gnus-thread-total-score-1 (list thread)))))
4588
4589 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4590   "Sort threads such that the thread with the most recently arrived article comes first."
4591   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4592
4593 (defun gnus-thread-highest-number (thread)
4594   "Return the highest article number in THREAD."
4595   (apply 'max (mapcar (lambda (header)
4596                         (mail-header-number header))
4597                       (message-flatten-list thread))))
4598
4599 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4600   "Sort threads such that the thread with the most recently dated article comes first."
4601   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4602
4603 (defun gnus-thread-latest-date (thread)
4604   "Return the highest article date in THREAD."
4605   (let ((previous-time 0))
4606     (apply 'max
4607            (mapcar
4608             (lambda (header)
4609               (setq previous-time
4610                     (condition-case ()
4611                         (time-to-seconds (mail-header-parse-date
4612                                           (mail-header-date header)))
4613                       (error previous-time))))
4614             (sort
4615              (message-flatten-list thread)
4616              (lambda (h1 h2)
4617                (< (mail-header-number h1)
4618                   (mail-header-number h2))))))))
4619
4620 (defun gnus-thread-total-score-1 (root)
4621   ;; This function find the total score of the thread below ROOT.
4622   (setq root (car root))
4623   (apply gnus-thread-score-function
4624          (or (append
4625               (mapcar 'gnus-thread-total-score
4626                       (cdr (gnus-id-to-thread (mail-header-id root))))
4627               (when (> (mail-header-number root) 0)
4628                 (list (or (cdr (assq (mail-header-number root)
4629                                      gnus-newsgroup-scored))
4630                           gnus-summary-default-score 0))))
4631              (list gnus-summary-default-score)
4632              '(0))))
4633
4634 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4635 (defvar gnus-tmp-prev-subject nil)
4636 (defvar gnus-tmp-false-parent nil)
4637 (defvar gnus-tmp-root-expunged nil)
4638 (defvar gnus-tmp-dummy-line nil)
4639
4640 (eval-when-compile (defvar gnus-tmp-header))
4641 (defun gnus-extra-header (type &optional header)
4642   "Return the extra header of TYPE."
4643   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4644       ""))
4645
4646 (defvar gnus-tmp-thread-tree-header-string "")
4647
4648 (defcustom gnus-sum-thread-tree-root "> "
4649   "With %B spec, used for the root of a thread.
4650 If nil, use subject instead."
4651   :type '(radio (const :format "%v  " nil) (string :size 0))
4652   :group 'gnus-thread)
4653 (defcustom gnus-sum-thread-tree-false-root "> "
4654   "With %B spec, used for a false root of a thread.
4655 If nil, use subject instead."
4656   :type '(radio (const :format "%v  " nil) (string :size 0))
4657   :group 'gnus-thread)
4658 (defcustom gnus-sum-thread-tree-single-indent ""
4659   "With %B spec, used for a thread with just one message.
4660 If nil, use subject instead."
4661   :type '(radio (const :format "%v  " nil) (string :size 0))
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-vertical "| "
4664   "With %B spec, used for drawing a vertical line."
4665   :type 'string
4666   :group 'gnus-thread)
4667 (defcustom gnus-sum-thread-tree-indent "  "
4668   "With %B spec, used for indenting."
4669   :type 'string
4670   :group 'gnus-thread)
4671 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4672   "With %B spec, used for a leaf with brothers."
4673   :type 'string
4674   :group 'gnus-thread)
4675 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4676   "With %B spec, used for a leaf without brothers."
4677   :type 'string
4678   :group 'gnus-thread)
4679
4680 (defun gnus-summary-prepare-threads (threads)
4681   "Prepare summary buffer from THREADS and indentation LEVEL.
4682 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4683 or a straight list of headers."
4684   (gnus-message 7 "Generating summary...")
4685
4686   (setq gnus-newsgroup-threads threads)
4687   (beginning-of-line)
4688
4689   (let ((gnus-tmp-level 0)
4690         (default-score (or gnus-summary-default-score 0))
4691         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4692         (building-line-count gnus-summary-display-while-building)
4693         (building-count (integerp gnus-summary-display-while-building))
4694         thread number subject stack state gnus-tmp-gathered beg-match
4695         new-roots gnus-tmp-new-adopts thread-end simp-subject
4696         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4697         gnus-tmp-replied gnus-tmp-subject-or-nil
4698         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4699         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4700         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4701         tree-stack)
4702
4703     (setq gnus-tmp-prev-subject nil
4704           gnus-tmp-thread-tree-header-string "")
4705
4706     (if (vectorp (car threads))
4707         ;; If this is a straight (sic) list of headers, then a
4708         ;; threaded summary display isn't required, so we just create
4709         ;; an unthreaded one.
4710         (gnus-summary-prepare-unthreaded threads)
4711
4712       ;; Do the threaded display.
4713
4714       (if gnus-summary-display-while-building
4715           (switch-to-buffer (buffer-name)))
4716       (while (or threads stack gnus-tmp-new-adopts new-roots)
4717
4718         (if (and (= gnus-tmp-level 0)
4719                  (or (not stack)
4720                      (= (caar stack) 0))
4721                  (not gnus-tmp-false-parent)
4722                  (or gnus-tmp-new-adopts new-roots))
4723             (if gnus-tmp-new-adopts
4724                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4725                       thread (list (car gnus-tmp-new-adopts))
4726                       gnus-tmp-header (caar thread)
4727                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4728               (when new-roots
4729                 (setq thread (list (car new-roots))
4730                       gnus-tmp-header (caar thread)
4731                       new-roots (cdr new-roots))))
4732
4733           (if threads
4734               ;; If there are some threads, we do them before the
4735               ;; threads on the stack.
4736               (setq thread threads
4737                     gnus-tmp-header (caar thread))
4738             ;; There were no current threads, so we pop something off
4739             ;; the stack.
4740             (setq state (car stack)
4741                   gnus-tmp-level (car state)
4742                   tree-stack (cadr state)
4743                   thread (caddr state)
4744                   stack (cdr stack)
4745                   gnus-tmp-header (caar thread))))
4746
4747         (setq gnus-tmp-false-parent nil)
4748         (setq gnus-tmp-root-expunged nil)
4749         (setq thread-end nil)
4750
4751         (if (stringp gnus-tmp-header)
4752             ;; The header is a dummy root.
4753             (cond
4754              ((eq gnus-summary-make-false-root 'adopt)
4755               ;; We let the first article adopt the rest.
4756               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4757                                                (cddar thread)))
4758               (setq gnus-tmp-gathered
4759                     (nconc (mapcar
4760                             (lambda (h) (mail-header-number (car h)))
4761                             (cddar thread))
4762                            gnus-tmp-gathered))
4763               (setq thread (cons (list (caar thread)
4764                                        (cadar thread))
4765                                  (cdr thread)))
4766               (setq gnus-tmp-level -1
4767                     gnus-tmp-false-parent t))
4768              ((eq gnus-summary-make-false-root 'empty)
4769               ;; We print adopted articles with empty subject fields.
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 gnus-tmp-level -1))
4776              ((eq gnus-summary-make-false-root 'dummy)
4777               ;; We remember that we probably want to output a dummy
4778               ;; root.
4779               (setq gnus-tmp-dummy-line gnus-tmp-header)
4780               (setq gnus-tmp-prev-subject gnus-tmp-header))
4781              (t
4782               ;; We do not make a root for the gathered
4783               ;; sub-threads at all.
4784               (setq gnus-tmp-level -1)))
4785
4786           (setq number (mail-header-number gnus-tmp-header)
4787                 subject (mail-header-subject gnus-tmp-header)
4788                 simp-subject (gnus-simplify-subject-fully subject))
4789
4790           (cond
4791            ;; If the thread has changed subject, we might want to make
4792            ;; this subthread into a root.
4793            ((and (null gnus-thread-ignore-subject)
4794                  (not (zerop gnus-tmp-level))
4795                  gnus-tmp-prev-subject
4796                  (not (string= gnus-tmp-prev-subject simp-subject)))
4797             (setq new-roots (nconc new-roots (list (car thread)))
4798                   thread-end t
4799                   gnus-tmp-header nil))
4800            ;; If the article lies outside the current limit,
4801            ;; then we do not display it.
4802            ((not (memq number gnus-newsgroup-limit))
4803             (setq gnus-tmp-gathered
4804                   (nconc (mapcar
4805                           (lambda (h) (mail-header-number (car h)))
4806                           (cdar thread))
4807                          gnus-tmp-gathered))
4808             (setq gnus-tmp-new-adopts (if (cdar thread)
4809                                           (append gnus-tmp-new-adopts
4810                                                   (cdar thread))
4811                                         gnus-tmp-new-adopts)
4812                   thread-end t
4813                   gnus-tmp-header nil)
4814             (when (zerop gnus-tmp-level)
4815               (setq gnus-tmp-root-expunged t)))
4816            ;; Perhaps this article is to be marked as read?
4817            ((and gnus-summary-mark-below
4818                  (< (or (cdr (assq number gnus-newsgroup-scored))
4819                         default-score)
4820                     gnus-summary-mark-below)
4821                  ;; Don't touch sparse articles.
4822                  (not (gnus-summary-article-sparse-p number))
4823                  (not (gnus-summary-article-ancient-p number)))
4824             (setq gnus-newsgroup-unreads
4825                   (delq number gnus-newsgroup-unreads))
4826             (if gnus-newsgroup-auto-expire
4827                 (setq gnus-newsgroup-expirable
4828                       (gnus-add-to-sorted-list
4829                        gnus-newsgroup-expirable number))
4830               (push (cons number gnus-low-score-mark)
4831                     gnus-newsgroup-reads))))
4832
4833           (when gnus-tmp-header
4834             ;; We may have an old dummy line to output before this
4835             ;; article.
4836             (when (and gnus-tmp-dummy-line
4837                        (gnus-subject-equal
4838                         gnus-tmp-dummy-line
4839                         (mail-header-subject gnus-tmp-header)))
4840               (gnus-summary-insert-dummy-line
4841                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4842               (setq gnus-tmp-dummy-line nil))
4843
4844             ;; Compute the mark.
4845             (setq gnus-tmp-unread (gnus-article-mark number))
4846
4847             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4848                                   gnus-tmp-header gnus-tmp-level)
4849                   gnus-newsgroup-data)
4850
4851             ;; Actually insert the line.
4852             (setq
4853              gnus-tmp-subject-or-nil
4854              (cond
4855               ((and gnus-thread-ignore-subject
4856                     gnus-tmp-prev-subject
4857                     (not (string= gnus-tmp-prev-subject simp-subject)))
4858                subject)
4859               ((zerop gnus-tmp-level)
4860                (if (and (eq gnus-summary-make-false-root 'empty)
4861                         (memq number gnus-tmp-gathered)
4862                         gnus-tmp-prev-subject
4863                         (string= gnus-tmp-prev-subject simp-subject))
4864                    gnus-summary-same-subject
4865                  subject))
4866               (t gnus-summary-same-subject)))
4867             (if (and (eq gnus-summary-make-false-root 'adopt)
4868                      (= gnus-tmp-level 1)
4869                      (memq number gnus-tmp-gathered))
4870                 (setq gnus-tmp-opening-bracket ?\<
4871                       gnus-tmp-closing-bracket ?\>)
4872               (setq gnus-tmp-opening-bracket ?\[
4873                     gnus-tmp-closing-bracket ?\]))
4874             (setq
4875              gnus-tmp-indentation
4876              (aref gnus-thread-indent-array gnus-tmp-level)
4877              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4878              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4879                                 gnus-summary-default-score 0)
4880              gnus-tmp-score-char
4881              (if (or (null gnus-summary-default-score)
4882                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4883                          gnus-summary-zcore-fuzz))
4884                  ?                      ;Whitespace
4885                (if (< gnus-tmp-score gnus-summary-default-score)
4886                    gnus-score-below-mark gnus-score-over-mark))
4887              gnus-tmp-replied
4888              (cond ((memq number gnus-newsgroup-processable)
4889                     gnus-process-mark)
4890                    ((memq number gnus-newsgroup-cached)
4891                     gnus-cached-mark)
4892                    ((memq number gnus-newsgroup-replied)
4893                     gnus-replied-mark)
4894                    ((memq number gnus-newsgroup-forwarded)
4895                     gnus-forwarded-mark)
4896                    ((memq number gnus-newsgroup-saved)
4897                     gnus-saved-mark)
4898                    ((memq number gnus-newsgroup-recent)
4899                     gnus-recent-mark)
4900                    ((memq number gnus-newsgroup-unseen)
4901                     gnus-unseen-mark)
4902                    (t gnus-no-mark))
4903              gnus-tmp-downloaded
4904              (cond ((memq number gnus-newsgroup-undownloaded)
4905                     gnus-undownloaded-mark)
4906                    (gnus-newsgroup-agentized
4907                     gnus-downloaded-mark)
4908                    (t
4909                     gnus-no-mark))
4910              gnus-tmp-from (mail-header-from gnus-tmp-header)
4911              gnus-tmp-name
4912              (cond
4913               ((string-match "<[^>]+> *$" gnus-tmp-from)
4914                (setq beg-match (match-beginning 0))
4915                (or (and (string-match "^\".+\"" gnus-tmp-from)
4916                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4917                    (substring gnus-tmp-from 0 beg-match)))
4918               ((string-match "(.+)" gnus-tmp-from)
4919                (substring gnus-tmp-from
4920                           (1+ (match-beginning 0)) (1- (match-end 0))))
4921               (t gnus-tmp-from))
4922
4923              ;; Do the %B string
4924              gnus-tmp-thread-tree-header-string
4925              (cond
4926               ((not gnus-show-threads) "")
4927               ((zerop gnus-tmp-level)
4928                (cond ((cdar thread)
4929                       (or gnus-sum-thread-tree-root subject))
4930                      (gnus-tmp-new-adopts
4931                       (or gnus-sum-thread-tree-false-root subject))
4932                      (t
4933                       (or gnus-sum-thread-tree-single-indent subject))))
4934               (t
4935                (concat (apply 'concat
4936                               (mapcar (lambda (item)
4937                                         (if (= item 1)
4938                                             gnus-sum-thread-tree-vertical
4939                                           gnus-sum-thread-tree-indent))
4940                                       (cdr (reverse tree-stack))))
4941                        (if (nth 1 thread)
4942                            gnus-sum-thread-tree-leaf-with-other
4943                          gnus-sum-thread-tree-single-leaf)))))
4944             (when (string= gnus-tmp-name "")
4945               (setq gnus-tmp-name gnus-tmp-from))
4946             (unless (numberp gnus-tmp-lines)
4947               (setq gnus-tmp-lines -1))
4948             (if (= gnus-tmp-lines -1)
4949                 (setq gnus-tmp-lines "?")
4950               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4951               (gnus-put-text-property
4952              (point)
4953              (progn (eval gnus-summary-line-format-spec) (point))
4954                'gnus-number number)
4955             (when gnus-visual-p
4956               (forward-line -1)
4957               (gnus-run-hooks 'gnus-summary-update-hook)
4958               (forward-line 1))
4959
4960             (setq gnus-tmp-prev-subject simp-subject)))
4961
4962         (when (nth 1 thread)
4963           (push (list (max 0 gnus-tmp-level)
4964                       (copy-sequence tree-stack)
4965                       (nthcdr 1 thread))
4966                 stack))
4967         (push (if (nth 1 thread) 1 0) tree-stack)
4968         (incf gnus-tmp-level)
4969         (setq threads (if thread-end nil (cdar thread)))
4970         (if gnus-summary-display-while-building
4971             (if building-count
4972                 (progn
4973                   ;; use a set frequency
4974                   (setq building-line-count (1- building-line-count))
4975                   (when (= building-line-count 0)
4976                     (sit-for 0)
4977                     (setq building-line-count
4978                           gnus-summary-display-while-building)))
4979               ;; always
4980               (sit-for 0)))
4981         (unless threads
4982           (setq gnus-tmp-level 0)))))
4983   (gnus-message 7 "Generating summary...done"))
4984
4985 (defun gnus-summary-prepare-unthreaded (headers)
4986   "Generate an unthreaded summary buffer based on HEADERS."
4987   (let (header number mark)
4988
4989     (beginning-of-line)
4990
4991     (while headers
4992       ;; We may have to root out some bad articles...
4993       (when (memq (setq number (mail-header-number
4994                                 (setq header (pop headers))))
4995                   gnus-newsgroup-limit)
4996         ;; Mark article as read when it has a low score.
4997         (when (and gnus-summary-mark-below
4998                    (< (or (cdr (assq number gnus-newsgroup-scored))
4999                           gnus-summary-default-score 0)
5000                       gnus-summary-mark-below)
5001                    (not (gnus-summary-article-ancient-p number)))
5002           (setq gnus-newsgroup-unreads
5003                 (delq number gnus-newsgroup-unreads))
5004           (if gnus-newsgroup-auto-expire
5005               (push number gnus-newsgroup-expirable)
5006             (push (cons number gnus-low-score-mark)
5007                   gnus-newsgroup-reads)))
5008
5009         (setq mark (gnus-article-mark number))
5010         (push (gnus-data-make number mark (1+ (point)) header 0)
5011               gnus-newsgroup-data)
5012         (gnus-summary-insert-line
5013          header 0 number
5014          (memq number gnus-newsgroup-undownloaded)
5015          mark (memq number gnus-newsgroup-replied)
5016          (memq number gnus-newsgroup-expirable)
5017          (mail-header-subject header) nil
5018          (cdr (assq number gnus-newsgroup-scored))
5019          (memq number gnus-newsgroup-processable))))))
5020
5021 (defun gnus-summary-remove-list-identifiers ()
5022   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5023   (let ((regexp (if (consp gnus-list-identifiers)
5024                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5025                   gnus-list-identifiers))
5026         changed subject)
5027     (when regexp
5028       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5029       (dolist (header gnus-newsgroup-headers)
5030         (setq subject (mail-header-subject header)
5031               changed nil)
5032         (while (string-match regexp subject)
5033           (setq subject
5034                 (concat (substring subject 0 (match-beginning 1))
5035                         (substring subject (match-end 0)))
5036                 changed t))
5037         (when changed
5038           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5039             (setq subject
5040                   (concat (substring subject 0 (match-beginning 1))
5041                           (substring subject (match-end 1)))))
5042           (mail-header-set-subject header subject))))))
5043
5044 (defun gnus-fetch-headers (articles)
5045   "Fetch headers of ARTICLES."
5046   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5047     (gnus-message 5 "Fetching headers for %s..." name)
5048     (prog1
5049         (if (eq 'nov
5050                 (setq gnus-headers-retrieved-by
5051                       (gnus-retrieve-headers
5052                        articles gnus-newsgroup-name
5053                        ;; We might want to fetch old headers, but
5054                        ;; not if there is only 1 article.
5055                        (and (or (and
5056                                  (not (eq gnus-fetch-old-headers 'some))
5057                                  (not (numberp gnus-fetch-old-headers)))
5058                                 (> (length articles) 1))
5059                             gnus-fetch-old-headers))))
5060             (gnus-get-newsgroup-headers-xover
5061              articles nil nil gnus-newsgroup-name t)
5062           (gnus-get-newsgroup-headers))
5063       (gnus-message 5 "Fetching headers for %s...done" name))))
5064
5065 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5066   "Select newsgroup GROUP.
5067 If READ-ALL is non-nil, all articles in the group are selected.
5068 If SELECT-ARTICLES, only select those articles from GROUP."
5069   (let* ((entry (gnus-group-entry group))
5070          ;;!!! Dirty hack; should be removed.
5071          (gnus-summary-ignore-duplicates
5072           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5073               t
5074             gnus-summary-ignore-duplicates))
5075          (info (nth 2 entry))
5076          articles fetched-articles cached)
5077
5078     (unless (gnus-check-server
5079              (set (make-local-variable 'gnus-current-select-method)
5080                   (gnus-find-method-for-group group)))
5081       (error "Couldn't open server"))
5082
5083     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5084         (gnus-activate-group group)     ; Or we can activate it...
5085         (progn                          ; Or we bug out.
5086           (when (equal major-mode 'gnus-summary-mode)
5087             (gnus-kill-buffer (current-buffer)))
5088           (error "Couldn't activate group %s: %s"
5089                  group (gnus-status-message group))))
5090
5091     (unless (gnus-request-group group t)
5092       (when (equal major-mode 'gnus-summary-mode)
5093         (gnus-kill-buffer (current-buffer)))
5094       (error "Couldn't request group %s: %s"
5095              group (gnus-status-message group)))
5096
5097     (when gnus-agent
5098       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5099       
5100       (setq gnus-summary-use-undownloaded-faces
5101             (gnus-agent-find-parameter
5102              group
5103              'agent-enable-undownloaded-faces)))
5104
5105     (setq gnus-newsgroup-name group
5106           gnus-newsgroup-unselected nil
5107           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5108
5109     (let ((display (gnus-group-find-parameter group 'display)))
5110       (setq gnus-newsgroup-display
5111             (cond
5112              ((not (zerop (or (car-safe read-all) 0)))
5113               ;; The user entered the group with C-u SPC/RET, let's show
5114               ;; all articles.
5115               'gnus-not-ignore)
5116              ((eq display 'all)
5117               'gnus-not-ignore)
5118              ((arrayp display)
5119               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5120              ((numberp display)
5121               ;; The following is probably the "correct" solution, but
5122               ;; it makes Gnus fetch all headers and then limit the
5123               ;; articles (which is slow), so instead we hack the
5124               ;; select-articles parameter instead. -- Simon Josefsson
5125               ;; <jas@kth.se>
5126               ;;
5127               ;; (gnus-byte-compile
5128               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5129               ;;                         display)))))
5130               (setq select-articles
5131                     (gnus-uncompress-range
5132                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5133                              (if (> tmp 0)
5134                                  tmp
5135                                1))
5136                            (cdr (gnus-active group)))))
5137               nil)
5138              (t
5139               nil))))
5140
5141     (gnus-summary-setup-default-charset)
5142
5143     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5144     (when (gnus-virtual-group-p group)
5145       (setq cached gnus-newsgroup-cached))
5146
5147     (setq gnus-newsgroup-unreads
5148           (gnus-sorted-ndifference
5149            (gnus-sorted-ndifference gnus-newsgroup-unreads
5150                                     gnus-newsgroup-marked)
5151            gnus-newsgroup-dormant))
5152
5153     (setq gnus-newsgroup-processable nil)
5154
5155     (gnus-update-read-articles group gnus-newsgroup-unreads)
5156
5157     ;; Adjust and set lists of article marks.
5158     (when info
5159       (gnus-adjust-marked-articles info))
5160     (if (setq articles select-articles)
5161         (setq gnus-newsgroup-unselected
5162               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5163       (setq articles (gnus-articles-to-read group read-all)))
5164
5165     (cond
5166      ((null articles)
5167       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5168       'quit)
5169      ((eq articles 0) nil)
5170      (t
5171       ;; Init the dependencies hash table.
5172       (setq gnus-newsgroup-dependencies
5173             (gnus-make-hashtable (length articles)))
5174       (gnus-set-global-variables)
5175       ;; Retrieve the headers and read them in.
5176
5177       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5178
5179       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5180       (when cached
5181         (setq gnus-newsgroup-cached cached))
5182
5183       ;; Suppress duplicates?
5184       (when gnus-suppress-duplicates
5185         (gnus-dup-suppress-articles))
5186
5187       ;; Set the initial limit.
5188       (setq gnus-newsgroup-limit (copy-sequence articles))
5189       ;; Remove canceled articles from the list of unread articles.
5190       (setq fetched-articles
5191             (mapcar (lambda (headers) (mail-header-number headers))
5192                     gnus-newsgroup-headers))
5193       (setq gnus-newsgroup-articles fetched-articles)
5194       (setq gnus-newsgroup-unreads
5195             (gnus-sorted-nintersection
5196              gnus-newsgroup-unreads fetched-articles))
5197       (gnus-compute-unseen-list)
5198
5199       ;; Removed marked articles that do not exist.
5200       (gnus-update-missing-marks
5201        (gnus-sorted-difference articles fetched-articles))
5202       ;; We might want to build some more threads first.
5203       (when (and gnus-fetch-old-headers
5204                  (eq gnus-headers-retrieved-by 'nov))
5205         (if (eq gnus-fetch-old-headers 'invisible)
5206             (gnus-build-all-threads)
5207           (gnus-build-old-threads)))
5208       ;; Let the Gnus agent mark articles as read.
5209       (when gnus-agent
5210         (gnus-agent-get-undownloaded-list))
5211       ;; Remove list identifiers from subject
5212       (when gnus-list-identifiers
5213         (gnus-summary-remove-list-identifiers))
5214       ;; Check whether auto-expire is to be done in this group.
5215       (setq gnus-newsgroup-auto-expire
5216             (gnus-group-auto-expirable-p group))
5217       ;; Set up the article buffer now, if necessary.
5218       (unless gnus-single-article-buffer
5219         (gnus-article-setup-buffer))
5220       ;; First and last article in this newsgroup.
5221       (when gnus-newsgroup-headers
5222         (setq gnus-newsgroup-begin
5223               (mail-header-number (car gnus-newsgroup-headers))
5224               gnus-newsgroup-end
5225               (mail-header-number
5226                (gnus-last-element gnus-newsgroup-headers))))
5227       ;; GROUP is successfully selected.
5228       (or gnus-newsgroup-headers t)))))
5229
5230 (defun gnus-compute-unseen-list ()
5231   ;; The `seen' marks are treated specially.
5232   (if (not gnus-newsgroup-seen)
5233       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5234     (setq gnus-newsgroup-unseen
5235           (gnus-inverse-list-range-intersection
5236            gnus-newsgroup-articles gnus-newsgroup-seen))))
5237
5238 (defun gnus-summary-display-make-predicate (display)
5239   (require 'gnus-agent)
5240   (when (= (length display) 1)
5241     (setq display (car display)))
5242   (unless gnus-summary-display-cache
5243     (dolist (elem (append '((unread . unread)
5244                             (read . read)
5245                             (unseen . unseen))
5246                           gnus-article-mark-lists))
5247       (push (cons (cdr elem)
5248                   (gnus-byte-compile
5249                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5250             gnus-summary-display-cache)))
5251   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5252         (gnus-category-predicate-cache gnus-summary-display-cache))
5253     (gnus-get-predicate display)))
5254
5255 ;; Uses the dynamically bound `number' variable.
5256 (eval-when-compile
5257   (defvar number))
5258 (defun gnus-article-marked-p (type &optional article)
5259   (let ((article (or article number)))
5260     (cond
5261      ((eq type 'tick)
5262       (memq article gnus-newsgroup-marked))
5263      ((eq type 'spam)
5264       (memq article gnus-newsgroup-spam-marked))
5265      ((eq type 'unsend)
5266       (memq article gnus-newsgroup-unsendable))
5267      ((eq type 'undownload)
5268       (memq article gnus-newsgroup-undownloaded))
5269      ((eq type 'download)
5270       (memq article gnus-newsgroup-downloadable))
5271      ((eq type 'unread)
5272       (memq article gnus-newsgroup-unreads))
5273      ((eq type 'read)
5274       (memq article gnus-newsgroup-reads))
5275      ((eq type 'dormant)
5276       (memq article gnus-newsgroup-dormant) )
5277      ((eq type 'expire)
5278       (memq article gnus-newsgroup-expirable))
5279      ((eq type 'reply)
5280       (memq article gnus-newsgroup-replied))
5281      ((eq type 'killed)
5282       (memq article gnus-newsgroup-killed))
5283      ((eq type 'bookmark)
5284       (assq article gnus-newsgroup-bookmarks))
5285      ((eq type 'score)
5286       (assq article gnus-newsgroup-scored))
5287      ((eq type 'save)
5288       (memq article gnus-newsgroup-saved))
5289      ((eq type 'cache)
5290       (memq article gnus-newsgroup-cached))
5291      ((eq type 'forward)
5292       (memq article gnus-newsgroup-forwarded))
5293      ((eq type 'seen)
5294       (not (memq article gnus-newsgroup-unseen)))
5295      ((eq type 'recent)
5296       (memq article gnus-newsgroup-recent))
5297      (t t))))
5298
5299 (defun gnus-articles-to-read (group &optional read-all)
5300   "Find out what articles the user wants to read."
5301   (let* ((articles
5302           ;; Select all articles if `read-all' is non-nil, or if there
5303           ;; are no unread articles.
5304           (if (or read-all
5305                   (and (zerop (length gnus-newsgroup-marked))
5306                        (zerop (length gnus-newsgroup-unreads)))
5307                   ;; Fetch all if the predicate is non-nil.
5308                   gnus-newsgroup-display)
5309               ;; We want to select the headers for all the articles in
5310               ;; the group, so we select either all the active
5311               ;; articles in the group, or (if that's nil), the
5312               ;; articles in the cache.
5313               (or
5314                (gnus-uncompress-range (gnus-active group))
5315                (gnus-cache-articles-in-group group))
5316             ;; Select only the "normal" subset of articles.
5317             (gnus-sorted-nunion
5318              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5319              gnus-newsgroup-unreads)))
5320          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5321          (scored (length scored-list))
5322          (number (length articles))
5323          (marked (+ (length gnus-newsgroup-marked)
5324                     (length gnus-newsgroup-dormant)))
5325          (select
5326           (cond
5327            ((numberp read-all)
5328             read-all)
5329            ((numberp gnus-newsgroup-display)
5330             gnus-newsgroup-display)
5331            (t
5332             (condition-case ()
5333                 (cond
5334                  ((and (or (<= scored marked) (= scored number))
5335                        (numberp gnus-large-newsgroup)
5336                        (> number gnus-large-newsgroup))
5337                   (let* ((cursor-in-echo-area nil)
5338                          (initial (gnus-parameter-large-newsgroup-initial
5339                                    gnus-newsgroup-name))
5340                          (input
5341                           (read-string
5342                            (format
5343                             "How many articles from %s (%s %d): "
5344                             (gnus-limit-string
5345                              (gnus-group-decoded-name gnus-newsgroup-name)
5346                              35)
5347                             (if initial "max" "default")
5348                             number)
5349                            (if initial
5350                                (cons (number-to-string initial)
5351                                      0)))))
5352                     (if (string-match "^[ \t]*$" input) number input)))
5353                  ((and (> scored marked) (< scored number)
5354                        (> (- scored number) 20))
5355                   (let ((input
5356                          (read-string
5357                           (format "%s %s (%d scored, %d total): "
5358                                   "How many articles from"
5359                                   (gnus-group-decoded-name group)
5360                                   scored number))))
5361                     (if (string-match "^[ \t]*$" input)
5362                         number input)))
5363                  (t number))
5364               (quit
5365                (message "Quit getting the articles to read")
5366                nil))))))
5367     (setq select (if (stringp select) (string-to-number select) select))
5368     (if (or (null select) (zerop select))
5369         select
5370       (if (and (not (zerop scored)) (<= (abs select) scored))
5371           (progn
5372             (setq articles (sort scored-list '<))
5373             (setq number (length articles)))
5374         (setq articles (copy-sequence articles)))
5375
5376       (when (< (abs select) number)
5377         (if (< select 0)
5378             ;; Select the N oldest articles.
5379             (setcdr (nthcdr (1- (abs select)) articles) nil)
5380           ;; Select the N most recent articles.
5381           (setq articles (nthcdr (- number select) articles))))
5382       (setq gnus-newsgroup-unselected
5383             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5384       (when gnus-alter-articles-to-read-function
5385         (setq articles
5386               (sort
5387                (funcall gnus-alter-articles-to-read-function
5388                         gnus-newsgroup-name articles)
5389                '<)))
5390       articles)))
5391
5392 (defun gnus-killed-articles (killed articles)
5393   (let (out)
5394     (while articles
5395       (when (inline (gnus-member-of-range (car articles) killed))
5396         (push (car articles) out))
5397       (setq articles (cdr articles)))
5398     out))
5399
5400 (defun gnus-uncompress-marks (marks)
5401   "Uncompress the mark ranges in MARKS."
5402   (let ((uncompressed '(score bookmark))
5403         out)
5404     (while marks
5405       (if (memq (caar marks) uncompressed)
5406           (push (car marks) out)
5407         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5408       (setq marks (cdr marks)))
5409     out))
5410
5411 (defun gnus-article-mark-to-type (mark)
5412   "Return the type of MARK."
5413   (or (cadr (assq mark gnus-article-special-mark-lists))
5414       'list))
5415
5416 (defun gnus-article-unpropagatable-p (mark)
5417   "Return whether MARK should be propagated to back end."
5418   (memq mark gnus-article-unpropagated-mark-lists))
5419
5420 (defun gnus-adjust-marked-articles (info)
5421   "Set all article lists and remove all marks that are no longer valid."
5422   (let* ((marked-lists (gnus-info-marks info))
5423          (active (gnus-active (gnus-info-group info)))
5424          (min (car active))
5425          (max (cdr active))
5426          (types gnus-article-mark-lists)
5427          marks var articles article mark mark-type
5428          bgn end)
5429
5430     (dolist (marks marked-lists)
5431       (setq mark (car marks)
5432             mark-type (gnus-article-mark-to-type mark)
5433             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5434
5435       ;; We set the variable according to the type of the marks list,
5436       ;; and then adjust the marks to a subset of the active articles.
5437       (cond
5438        ;; Adjust "simple" lists - compressed yet unsorted
5439        ((eq mark-type 'list)
5440         ;; Simultaneously uncompress and clip to active range
5441         ;; See gnus-uncompress-range for a description of possible marks
5442         (let (l lh)
5443           (if (not (cadr marks))
5444               (set var nil)
5445             (setq articles (if (numberp (cddr marks))
5446                                (list (cdr marks))
5447                              (cdr marks))
5448                   lh (cons nil nil)
5449                   l lh)
5450
5451             (while (setq article (pop articles))
5452               (cond ((consp article)
5453                      (setq bgn (max (car article) min)
5454                            end (min (cdr article) max))
5455                      (while (<= bgn end)
5456                        (setq l (setcdr l (cons bgn nil))
5457                              bgn (1+ bgn))))
5458                     ((and (<= min article)
5459                           (>= max article))
5460                      (setq l (setcdr l (cons article nil))))))
5461             (set var (cdr lh)))))
5462        ;; Adjust assocs.
5463        ((eq mark-type 'tuple)
5464         (set var (setq articles (cdr marks)))
5465         (when (not (listp (cdr (symbol-value var))))
5466           (set var (list (symbol-value var))))
5467         (when (not (listp (cdr articles)))
5468           (setq articles (list articles)))
5469         (while articles
5470           (when (or (not (consp (setq article (pop articles))))
5471                     (< (car article) min)
5472                     (> (car article) max))
5473             (set var (delq article (symbol-value var))))))
5474        ;; Adjust ranges (sloppily).
5475        ((eq mark-type 'range)
5476         (cond
5477          ((eq mark 'seen)
5478           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5479           ;; It should be (seen (NUM1 . NUM2)).
5480           (when (numberp (cddr marks))
5481             (setcdr marks (list (cdr marks))))
5482           (setq articles (cdr marks))
5483           (while (and articles
5484                       (or (and (consp (car articles))
5485                                (> min (cdar articles)))
5486                           (and (numberp (car articles))
5487                                (> min (car articles)))))
5488             (pop articles))
5489           (set var articles))))))))
5490
5491 (defun gnus-update-missing-marks (missing)
5492   "Go through the list of MISSING articles and remove them from the mark lists."
5493   (when missing
5494     (let (var m)
5495       ;; Go through all types.
5496       (dolist (elem gnus-article-mark-lists)
5497         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5498           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5499           (when (symbol-value var)
5500             ;; This list has articles.  So we delete all missing
5501             ;; articles from it.
5502             (setq m missing)
5503             (while m
5504               (set var (delq (pop m) (symbol-value var))))))))))
5505
5506 (defun gnus-update-marks ()
5507   "Enter the various lists of marked articles into the newsgroup info list."
5508   (let ((types gnus-article-mark-lists)
5509         (info (gnus-get-info gnus-newsgroup-name))
5510         type list newmarked symbol delta-marks)
5511     (when info
5512       ;; Add all marks lists to the list of marks lists.
5513       (while (setq type (pop types))
5514         (setq list (symbol-value
5515                     (setq symbol
5516                           (intern (format "gnus-newsgroup-%s" (car type))))))
5517
5518         (when list
5519           ;; Get rid of the entries of the articles that have the
5520           ;; default score.
5521           (when (and (eq (cdr type) 'score)
5522                      gnus-save-score
5523                      list)
5524             (let* ((arts list)
5525                    (prev (cons nil list))
5526                    (all prev))
5527               (while arts
5528                 (if (or (not (consp (car arts)))
5529                         (= (cdar arts) gnus-summary-default-score))
5530                     (setcdr prev (cdr arts))
5531                   (setq prev arts))
5532                 (setq arts (cdr arts)))
5533               (setq list (cdr all)))))
5534
5535         (when (eq (cdr type) 'seen)
5536           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5537
5538         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5539           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5540
5541         (when (and (gnus-check-backend-function
5542                     'request-set-mark gnus-newsgroup-name)
5543                    (not (gnus-article-unpropagatable-p (cdr type))))
5544           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5545                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5546                  (add (gnus-remove-from-range
5547                        (gnus-copy-sequence list) old)))
5548             (when add
5549               (push (list add 'add (list (cdr type))) delta-marks))
5550             (when del
5551               (push (list del 'del (list (cdr type))) delta-marks))))
5552
5553         (when list
5554           (push (cons (cdr type) list) newmarked)))
5555
5556       (when delta-marks
5557         (unless (gnus-check-group gnus-newsgroup-name)
5558           (error "Can't open server for %s" gnus-newsgroup-name))
5559         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5560
5561       ;; Enter these new marks into the info of the group.
5562       (if (nthcdr 3 info)
5563           (setcar (nthcdr 3 info) newmarked)
5564         ;; Add the marks lists to the end of the info.
5565         (when newmarked
5566           (setcdr (nthcdr 2 info) (list newmarked))))
5567
5568       ;; Cut off the end of the info if there's nothing else there.
5569       (let ((i 5))
5570         (while (and (> i 2)
5571                     (not (nth i info)))
5572           (when (nthcdr (decf i) info)
5573             (setcdr (nthcdr i info) nil)))))))
5574
5575 (defun gnus-set-mode-line (where)
5576   "Set the mode line of the article or summary buffers.
5577 If WHERE is `summary', the summary mode line format will be used."
5578   ;; Is this mode line one we keep updated?
5579   (when (and (memq where gnus-updated-mode-lines)
5580              (symbol-value
5581               (intern (format "gnus-%s-mode-line-format-spec" where))))
5582     (let (mode-string)
5583       (save-excursion
5584         ;; We evaluate this in the summary buffer since these
5585         ;; variables are buffer-local to that buffer.
5586         (set-buffer gnus-summary-buffer)
5587        ;; We bind all these variables that are used in the `eval' form
5588         ;; below.
5589         (let* ((mformat (symbol-value
5590                          (intern
5591                           (format "gnus-%s-mode-line-format-spec" where))))
5592                (gnus-tmp-group-name (gnus-group-decoded-name
5593                                      gnus-newsgroup-name))
5594                (gnus-tmp-article-number (or gnus-current-article 0))
5595                (gnus-tmp-unread gnus-newsgroup-unreads)
5596                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5597                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5598                (gnus-tmp-unread-and-unselected
5599                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5600                             (zerop gnus-tmp-unselected))
5601                        "")
5602                       ((zerop gnus-tmp-unselected)
5603                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5604                       (t (format "{%d(+%d) more}"
5605                                  gnus-tmp-unread-and-unticked
5606                                  gnus-tmp-unselected))))
5607                (gnus-tmp-subject
5608                 (if (and gnus-current-headers
5609                          (vectorp gnus-current-headers))
5610                     (gnus-mode-string-quote
5611                      (mail-header-subject gnus-current-headers))
5612                   ""))
5613                bufname-length max-len
5614                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5615           (setq mode-string (eval mformat))
5616           (setq bufname-length (if (string-match "%b" mode-string)
5617                                    (- (length
5618                                        (buffer-name
5619                                         (if (eq where 'summary)
5620                                             nil
5621                                           (get-buffer gnus-article-buffer))))
5622                                       2)
5623                                  0))
5624           (setq max-len (max 4 (if gnus-mode-non-string-length
5625                                    (- (window-width)
5626                                       gnus-mode-non-string-length
5627                                       bufname-length)
5628                                  (length mode-string))))
5629           ;; We might have to chop a bit of the string off...
5630           (when (> (length mode-string) max-len)
5631             (setq mode-string
5632                   (concat (truncate-string-to-width mode-string (- max-len 3))
5633                           "...")))
5634           ;; Pad the mode string a bit.
5635           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5636       ;; Update the mode line.
5637       (setq mode-line-buffer-identification
5638             (gnus-mode-line-buffer-identification (list mode-string)))
5639       (set-buffer-modified-p t))))
5640
5641 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5642   "Go through the HEADERS list and add all Xrefs to a hash table.
5643 The resulting hash table is returned, or nil if no Xrefs were found."
5644   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5645          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5646          (xref-hashtb (gnus-make-hashtable))
5647          start group entry number xrefs header)
5648     (while headers
5649       (setq header (pop headers))
5650       (when (and (setq xrefs (mail-header-xref header))
5651                  (not (memq (setq number (mail-header-number header))
5652                             unreads)))
5653         (setq start 0)
5654         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5655           (setq start (match-end 0))
5656           (setq group (if prefix
5657                           (concat prefix (substring xrefs (match-beginning 1)
5658                                                     (match-end 1)))
5659                         (substring xrefs (match-beginning 1) (match-end 1))))
5660           (setq number
5661                 (string-to-int (substring xrefs (match-beginning 2)
5662                                           (match-end 2))))
5663           (if (setq entry (gnus-gethash group xref-hashtb))
5664               (setcdr entry (cons number (cdr entry)))
5665             (gnus-sethash group (cons number nil) xref-hashtb)))))
5666     (and start xref-hashtb)))
5667
5668 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5669   "Look through all the headers and mark the Xrefs as read."
5670   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5671         name info xref-hashtb idlist method nth4)
5672     (save-excursion
5673       (set-buffer gnus-group-buffer)
5674       (when (setq xref-hashtb
5675                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5676         (mapatoms
5677          (lambda (group)
5678            (unless (string= from-newsgroup (setq name (symbol-name group)))
5679              (setq idlist (symbol-value group))
5680              ;; Dead groups are not updated.
5681              (and (prog1
5682                       (setq info (gnus-get-info name))
5683                     (when (stringp (setq nth4 (gnus-info-method info)))
5684                       (setq nth4 (gnus-server-to-method nth4))))
5685                   ;; Only do the xrefs if the group has the same
5686                   ;; select method as the group we have just read.
5687                   (or (gnus-methods-equal-p
5688                        nth4 (gnus-find-method-for-group from-newsgroup))
5689                       virtual
5690                       (equal nth4 (setq method (gnus-find-method-for-group
5691                                                 from-newsgroup)))
5692                       (and (equal (car nth4) (car method))
5693                            (equal (nth 1 nth4) (nth 1 method))))
5694                   gnus-use-cross-reference
5695                   (or (not (eq gnus-use-cross-reference t))
5696                       virtual
5697                       ;; Only do cross-references on subscribed
5698                       ;; groups, if that is what is wanted.
5699                       (<= (gnus-info-level info) gnus-level-subscribed))
5700                   (gnus-group-make-articles-read name idlist))))
5701          xref-hashtb)))))
5702
5703 (defun gnus-compute-read-articles (group articles)
5704   (let* ((entry (gnus-group-entry group))
5705          (info (nth 2 entry))
5706          (active (gnus-active group))
5707          ninfo)
5708     (when entry
5709       ;; First peel off all invalid article numbers.
5710       (when active
5711         (let ((ids articles)
5712               id first)
5713           (while (setq id (pop ids))
5714             (when (and first (> id (cdr active)))
5715               ;; We'll end up in this situation in one particular
5716               ;; obscure situation.  If you re-scan a group and get
5717               ;; a new article that is cross-posted to a different
5718               ;; group that has not been re-scanned, you might get
5719               ;; crossposted article that has a higher number than
5720               ;; Gnus believes possible.  So we re-activate this
5721               ;; group as well.  This might mean doing the
5722               ;; crossposting thingy will *increase* the number
5723               ;; of articles in some groups.  Tsk, tsk.
5724               (setq active (or (gnus-activate-group group) active)))
5725             (when (or (> id (cdr active))
5726                       (< id (car active)))
5727               (setq articles (delq id articles))))))
5728       ;; If the read list is nil, we init it.
5729       (if (and active
5730                (null (gnus-info-read info))
5731                (> (car active) 1))
5732           (setq ninfo (cons 1 (1- (car active))))
5733         (setq ninfo (gnus-info-read info)))
5734       ;; Then we add the read articles to the range.
5735       (gnus-add-to-range
5736        ninfo (setq articles (sort articles '<))))))
5737
5738 (defun gnus-group-make-articles-read (group articles)
5739   "Update the info of GROUP to say that ARTICLES are read."
5740   (let* ((num 0)
5741          (entry (gnus-group-entry group))
5742          (info (nth 2 entry))
5743          (active (gnus-active group))
5744          range)
5745     (when entry
5746       (setq range (gnus-compute-read-articles group articles))
5747       (with-current-buffer gnus-group-buffer
5748         (gnus-undo-register
5749           `(progn
5750              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5751              (gnus-info-set-read ',info ',(gnus-info-read info))
5752              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5753              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5754              (gnus-group-update-group ,group t))))
5755       ;; Add the read articles to the range.
5756       (gnus-info-set-read info range)
5757       (gnus-request-set-mark group (list (list range 'add '(read))))
5758       ;; Then we have to re-compute how many unread
5759       ;; articles there are in this group.
5760       (when active
5761         (cond
5762          ((not range)
5763           (setq num (- (1+ (cdr active)) (car active))))
5764          ((not (listp (cdr range)))
5765           (setq num (- (cdr active) (- (1+ (cdr range))
5766                                        (car range)))))
5767          (t
5768           (while range
5769             (if (numberp (car range))
5770                 (setq num (1+ num))
5771               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5772             (setq range (cdr range)))
5773           (setq num (- (cdr active) num))))
5774         ;; Update the number of unread articles.
5775         (setcar entry num)
5776         ;; Update the group buffer.
5777         (unless (gnus-ephemeral-group-p group)
5778           (gnus-group-update-group group t))))))
5779
5780 (defvar gnus-newsgroup-none-id 0)
5781
5782 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5783   (let ((cur nntp-server-buffer)
5784         (dependencies
5785          (or dependencies
5786              (with-current-buffer gnus-summary-buffer
5787                gnus-newsgroup-dependencies)))
5788         headers id end ref number
5789         (mail-parse-charset gnus-newsgroup-charset)
5790         (mail-parse-ignored-charsets
5791          (save-excursion (condition-case nil
5792                              (set-buffer gnus-summary-buffer)
5793                            (error))
5794                          gnus-newsgroup-ignored-charsets)))
5795     (save-excursion
5796       (set-buffer nntp-server-buffer)
5797       ;; Translate all TAB characters into SPACE characters.
5798       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5799       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5800       (ietf-drums-unfold-fws)
5801       (gnus-run-hooks 'gnus-parse-headers-hook)
5802       (let ((case-fold-search t)
5803             in-reply-to header p lines chars)
5804         (goto-char (point-min))
5805         ;; Search to the beginning of the next header.  Error messages
5806         ;; do not begin with 2 or 3.
5807         (while (re-search-forward "^[23][0-9]+ " nil t)
5808           (setq id nil
5809                 ref nil)
5810           ;; This implementation of this function, with nine
5811           ;; search-forwards instead of the one re-search-forward and
5812           ;; a case (which basically was the old function) is actually
5813           ;; about twice as fast, even though it looks messier.  You
5814           ;; can't have everything, I guess.  Speed and elegance
5815           ;; doesn't always go hand in hand.
5816           (setq
5817            header
5818            (vector
5819             ;; Number.
5820             (prog1
5821                 (setq number (read cur))
5822               (end-of-line)
5823               (setq p (point))
5824               (narrow-to-region (point)
5825                                 (or (and (search-forward "\n.\n" nil t)
5826                                          (- (point) 2))
5827                                     (point))))
5828             ;; Subject.
5829             (progn
5830               (goto-char p)
5831               (if (search-forward "\nsubject:" nil t)
5832                   (funcall gnus-decode-encoded-word-function
5833                            (nnheader-header-value))
5834                 "(none)"))
5835             ;; From.
5836             (progn
5837               (goto-char p)
5838               (if (search-forward "\nfrom:" nil t)
5839                   (funcall gnus-decode-encoded-word-function
5840                            (nnheader-header-value))
5841                 "(nobody)"))
5842             ;; Date.
5843             (progn
5844               (goto-char p)
5845               (if (search-forward "\ndate:" nil t)
5846                   (nnheader-header-value) ""))
5847             ;; Message-ID.
5848             (progn
5849               (goto-char p)
5850               (setq id (if (re-search-forward
5851                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5852                            ;; We do it this way to make sure the Message-ID
5853                            ;; is (somewhat) syntactically valid.
5854                            (buffer-substring (match-beginning 1)
5855                                              (match-end 1))
5856                          ;; If there was no message-id, we just fake one
5857                          ;; to make subsequent routines simpler.
5858                          (nnheader-generate-fake-message-id number))))
5859             ;; References.
5860             (progn
5861               (goto-char p)
5862               (if (search-forward "\nreferences:" nil t)
5863                   (progn
5864                     (setq end (point))
5865                     (prog1
5866                         (nnheader-header-value)
5867                       (setq ref
5868                             (buffer-substring
5869                              (progn
5870                                (end-of-line)
5871                                (search-backward ">" end t)
5872                                (1+ (point)))
5873                              (progn
5874                                (search-backward "<" end t)
5875                                (point))))))
5876                 ;; Get the references from the in-reply-to header if there
5877                 ;; were no references and the in-reply-to header looks
5878                 ;; promising.
5879                 (if (and (search-forward "\nin-reply-to:" nil t)
5880                          (setq in-reply-to (nnheader-header-value))
5881                          (string-match "<[^>]+>" in-reply-to))
5882                     (let (ref2)
5883                       (setq ref (substring in-reply-to (match-beginning 0)
5884                                            (match-end 0)))
5885                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5886                         (setq ref2 (substring in-reply-to (match-beginning 0)
5887                                               (match-end 0)))
5888                         (when (> (length ref2) (length ref))
5889                           (setq ref ref2)))
5890                       ref)
5891                   (setq ref nil))))
5892             ;; Chars.
5893             (progn
5894               (goto-char p)
5895               (if (search-forward "\nchars: " nil t)
5896                   (if (numberp (setq chars (ignore-errors (read cur))))
5897                       chars -1)
5898                 -1))
5899             ;; Lines.
5900             (progn
5901               (goto-char p)
5902               (if (search-forward "\nlines: " nil t)
5903                   (if (numberp (setq lines (ignore-errors (read cur))))
5904                       lines -1)
5905                 -1))
5906             ;; Xref.
5907             (progn
5908               (goto-char p)
5909               (and (search-forward "\nxref:" nil t)
5910                    (nnheader-header-value)))
5911             ;; Extra.
5912             (when gnus-extra-headers
5913               (let ((extra gnus-extra-headers)
5914                     out)
5915                 (while extra
5916                   (goto-char p)
5917                   (when (search-forward
5918                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5919                     (push (cons (car extra) (nnheader-header-value))
5920                           out))
5921                   (pop extra))
5922                 out))))
5923           (when (equal id ref)
5924             (setq ref nil))
5925
5926           (when gnus-alter-header-function
5927             (funcall gnus-alter-header-function header)
5928             (setq id (mail-header-id header)
5929                   ref (gnus-parent-id (mail-header-references header))))
5930
5931           (when (setq header
5932                       (gnus-dependencies-add-header
5933                        header dependencies force-new))
5934             (push header headers))
5935           (goto-char (point-max))
5936           (widen))
5937         (nreverse headers)))))
5938
5939 ;; Goes through the xover lines and returns a list of vectors
5940 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5941                                                   force-new dependencies
5942                                                   group also-fetch-heads)
5943   "Parse the news overview data in the server buffer.
5944 Return a list of headers that match SEQUENCE (see
5945 `nntp-retrieve-headers')."
5946   ;; Get the Xref when the users reads the articles since most/some
5947   ;; NNTP servers do not include Xrefs when using XOVER.
5948   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5949   (let ((mail-parse-charset gnus-newsgroup-charset)
5950         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5951         (cur nntp-server-buffer)
5952         (dependencies (or dependencies gnus-newsgroup-dependencies))
5953         (allp (cond
5954                ((eq gnus-read-all-available-headers t)
5955                 t)
5956                ((stringp gnus-read-all-available-headers)
5957                 (string-match gnus-read-all-available-headers group))
5958                (t
5959                 nil)))
5960         number headers header)
5961     (save-excursion
5962       (set-buffer nntp-server-buffer)
5963       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5964       ;; Allow the user to mangle the headers before parsing them.
5965       (gnus-run-hooks 'gnus-parse-headers-hook)
5966       (goto-char (point-min))
5967       (gnus-parse-without-error
5968         (while (and (or sequence allp)
5969                     (not (eobp)))
5970           (setq number (read cur))
5971           (when (not allp)
5972             (while (and sequence
5973                         (< (car sequence) number))
5974               (setq sequence (cdr sequence))))
5975           (when (and (or allp
5976                          (and sequence
5977                               (eq number (car sequence))))
5978                      (progn
5979                        (setq sequence (cdr sequence))
5980                        (setq header (inline
5981                                       (gnus-nov-parse-line
5982                                        number dependencies force-new)))))
5983             (push header headers))
5984           (forward-line 1)))
5985       ;; A common bug in inn is that if you have posted an article and
5986       ;; then retrieves the active file, it will answer correctly --
5987       ;; the new article is included.  However, a NOV entry for the
5988       ;; article may not have been generated yet, so this may fail.
5989       ;; We work around this problem by retrieving the last few
5990       ;; headers using HEAD.
5991       (if (or (not also-fetch-heads)
5992               (not sequence))
5993           ;; We (probably) got all the headers.
5994           (nreverse headers)
5995         (let ((gnus-nov-is-evil t))
5996           (nconc
5997            (nreverse headers)
5998            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5999              (gnus-get-newsgroup-headers))))))))
6000
6001 (defun gnus-article-get-xrefs ()
6002   "Fill in the Xref value in `gnus-current-headers', if necessary.
6003 This is meant to be called in `gnus-article-internal-prepare-hook'."
6004   (let ((headers (with-current-buffer gnus-summary-buffer
6005                    gnus-current-headers)))
6006     (or (not gnus-use-cross-reference)
6007         (not headers)
6008         (and (mail-header-xref headers)
6009              (not (string= (mail-header-xref headers) "")))
6010         (let ((case-fold-search t)
6011               xref)
6012           (save-restriction
6013             (nnheader-narrow-to-headers)
6014             (goto-char (point-min))
6015             (when (or (and (not (eobp))
6016                            (eq (downcase (char-after)) ?x)
6017                            (looking-at "Xref:"))
6018                       (search-forward "\nXref:" nil t))
6019               (goto-char (1+ (match-end 0)))
6020               (setq xref (buffer-substring (point) (point-at-eol)))
6021               (mail-header-set-xref headers xref)))))))
6022
6023 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6024   "Find article ID and insert the summary line for that article.
6025 OLD-HEADER can either be a header or a line number to insert
6026 the subject line on."
6027   (let* ((line (and (numberp old-header) old-header))
6028          (old-header (and (vectorp old-header) old-header))
6029          (header (cond ((and old-header use-old-header)
6030                         old-header)
6031                        ((and (numberp id)
6032                              (gnus-number-to-header id))
6033                         (gnus-number-to-header id))
6034                        (t
6035                         (gnus-read-header id))))
6036          (number (and (numberp id) id))
6037          d)
6038     (when header
6039       ;; Rebuild the thread that this article is part of and go to the
6040       ;; article we have fetched.
6041       (when (and (not gnus-show-threads)
6042                  old-header)
6043         (when (and number
6044                    (setq d (gnus-data-find (mail-header-number old-header))))
6045           (goto-char (gnus-data-pos d))
6046           (gnus-data-remove
6047            number
6048            (- (point-at-bol)
6049               (prog1
6050                   (1+ (point-at-eol))
6051                 (gnus-delete-line))))))
6052       ;; Remove list identifiers from subject.
6053       (when gnus-list-identifiers
6054         (let ((gnus-newsgroup-headers (list header)))
6055           (gnus-summary-remove-list-identifiers)
6056           (setq header (car gnus-newsgroup-headers))))
6057       (when old-header
6058         (mail-header-set-number header (mail-header-number old-header)))
6059       (setq gnus-newsgroup-sparse
6060             (delq (setq number (mail-header-number header))
6061                   gnus-newsgroup-sparse))
6062       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6063       (push number gnus-newsgroup-limit)
6064       (gnus-rebuild-thread (mail-header-id header) line)
6065       (gnus-summary-goto-subject number nil t))
6066     (when (and (numberp number)
6067                (> number 0))
6068       ;; We have to update the boundaries even if we can't fetch the
6069       ;; article if ID is a number -- so that the next `P' or `N'
6070       ;; command will fetch the previous (or next) article even
6071       ;; if the one we tried to fetch this time has been canceled.
6072       (when (> number gnus-newsgroup-end)
6073         (setq gnus-newsgroup-end number))
6074       (when (< number gnus-newsgroup-begin)
6075         (setq gnus-newsgroup-begin number))
6076       (setq gnus-newsgroup-unselected
6077             (delq number gnus-newsgroup-unselected)))
6078     ;; Report back a success?
6079     (and header (mail-header-number header))))
6080
6081 ;;; Process/prefix in the summary buffer
6082
6083 (defun gnus-summary-work-articles (n)
6084   "Return a list of articles to be worked upon.
6085 The prefix argument, the list of process marked articles, and the
6086 current article will be taken into consideration."
6087   (save-excursion
6088     (set-buffer gnus-summary-buffer)
6089     (cond
6090      (n
6091       ;; A numerical prefix has been given.
6092       (setq n (prefix-numeric-value n))
6093       (let ((backward (< n 0))
6094             (n (abs (prefix-numeric-value n)))
6095             articles article)
6096         (save-excursion
6097           (while
6098               (and (> n 0)
6099                    (push (setq article (gnus-summary-article-number))
6100                          articles)
6101                    (if backward
6102                        (gnus-summary-find-prev nil article)
6103                      (gnus-summary-find-next nil article)))
6104             (decf n)))
6105         (nreverse articles)))
6106      ((and (gnus-region-active-p) (mark))
6107       (message "region active")
6108       ;; Work on the region between point and mark.
6109       (let ((max (max (point) (mark)))
6110             articles article)
6111         (save-excursion
6112           (goto-char (min (min (point) (mark))))
6113           (while
6114               (and
6115                (push (setq article (gnus-summary-article-number)) articles)
6116                (gnus-summary-find-next nil article)
6117                (< (point) max)))
6118           (nreverse articles))))
6119      (gnus-newsgroup-processable
6120       ;; There are process-marked articles present.
6121       ;; Save current state.
6122       (gnus-summary-save-process-mark)
6123       ;; Return the list.
6124       (reverse gnus-newsgroup-processable))
6125      (t
6126       ;; Just return the current article.
6127       (list (gnus-summary-article-number))))))
6128
6129 (defmacro gnus-summary-iterate (arg &rest forms)
6130   "Iterate over the process/prefixed articles and do FORMS.
6131 ARG is the interactive prefix given to the command.  FORMS will be
6132 executed with point over the summary line of the articles."
6133   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6134     `(let ((,articles (gnus-summary-work-articles ,arg)))
6135        (while ,articles
6136          (gnus-summary-goto-subject (car ,articles))
6137          ,@forms
6138          (pop ,articles)))))
6139
6140 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6141 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6142
6143 (defun gnus-summary-save-process-mark ()
6144   "Push the current set of process marked articles on the stack."
6145   (interactive)
6146   (push (copy-sequence gnus-newsgroup-processable)
6147         gnus-newsgroup-process-stack))
6148
6149 (defun gnus-summary-kill-process-mark ()
6150   "Push the current set of process marked articles on the stack and unmark."
6151   (interactive)
6152   (gnus-summary-save-process-mark)
6153   (gnus-summary-unmark-all-processable))
6154
6155 (defun gnus-summary-yank-process-mark ()
6156   "Pop the last process mark state off the stack and restore it."
6157   (interactive)
6158   (unless gnus-newsgroup-process-stack
6159     (error "Empty mark stack"))
6160   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6161
6162 (defun gnus-summary-process-mark-set (set)
6163   "Make SET into the current process marked articles."
6164   (gnus-summary-unmark-all-processable)
6165   (while set
6166     (gnus-summary-set-process-mark (pop set))))
6167
6168 ;;; Searching and stuff
6169
6170 (defun gnus-summary-search-group (&optional backward use-level)
6171   "Search for next unread newsgroup.
6172 If optional argument BACKWARD is non-nil, search backward instead."
6173   (save-excursion
6174     (set-buffer gnus-group-buffer)
6175     (when (gnus-group-search-forward
6176            backward nil (if use-level (gnus-group-group-level) nil))
6177       (gnus-group-group-name))))
6178
6179 (defun gnus-summary-best-group (&optional exclude-group)
6180   "Find the name of the best unread group.
6181 If EXCLUDE-GROUP, do not go to this group."
6182   (with-current-buffer gnus-group-buffer
6183     (save-excursion
6184       (gnus-group-best-unread-group exclude-group))))
6185
6186 (defun gnus-summary-find-next (&optional unread article backward)
6187   (if backward
6188       (gnus-summary-find-prev unread article)
6189     (let* ((dummy (gnus-summary-article-intangible-p))
6190            (article (or article (gnus-summary-article-number)))
6191            (data (gnus-data-find-list article))
6192            result)
6193       (when (and (not dummy)
6194                  (or (not gnus-summary-check-current)
6195                      (not unread)
6196                      (not (gnus-data-unread-p (car data)))))
6197         (setq data (cdr data)))
6198       (when (setq result
6199                   (if unread
6200                       (progn
6201                         (while data
6202                           (unless (memq (gnus-data-number (car data))
6203                                         (cond
6204                                          ((eq gnus-auto-goto-ignores
6205                                               'always-undownloaded)
6206                                           gnus-newsgroup-undownloaded)
6207                                          (gnus-plugged
6208                                           nil)
6209                                          ((eq gnus-auto-goto-ignores
6210                                               'unfetched)
6211                                           gnus-newsgroup-unfetched)
6212                                          ((eq gnus-auto-goto-ignores
6213                                               'undownloaded)
6214                                           gnus-newsgroup-undownloaded)))
6215                             (when (gnus-data-unread-p (car data))
6216                               (setq result (car data)
6217                                     data nil)))
6218                           (setq data (cdr data)))
6219                         result)
6220                     (car data)))
6221         (goto-char (gnus-data-pos result))
6222         (gnus-data-number result)))))
6223
6224 (defun gnus-summary-find-prev (&optional unread article)
6225   (let* ((eobp (eobp))
6226          (article (or article (gnus-summary-article-number)))
6227          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6228          result)
6229     (when (and (not eobp)
6230                (or (not gnus-summary-check-current)
6231                    (not unread)
6232                    (not (gnus-data-unread-p (car data)))))
6233       (setq data (cdr data)))
6234     (when (setq result
6235                 (if unread
6236                     (progn
6237                       (while data
6238                         (unless (memq (gnus-data-number (car data))
6239                                       (cond
6240                                        ((eq gnus-auto-goto-ignores
6241                                             'always-undownloaded)
6242                                         gnus-newsgroup-undownloaded)
6243                                        (gnus-plugged
6244                                         nil)
6245                                        ((eq gnus-auto-goto-ignores
6246                                             'unfetched)
6247                                         gnus-newsgroup-unfetched)
6248                                        ((eq gnus-auto-goto-ignores
6249                                             'undownloaded)
6250                                         gnus-newsgroup-undownloaded)))
6251                           (when (gnus-data-unread-p (car data))
6252                             (setq result (car data)
6253                                   data nil)))
6254                         (setq data (cdr data)))
6255                       result)
6256                   (car data)))
6257       (goto-char (gnus-data-pos result))
6258       (gnus-data-number result))))
6259
6260 (defun gnus-summary-find-subject (subject &optional unread backward article)
6261   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6262          (article (or article (gnus-summary-article-number)))
6263          (articles (gnus-data-list backward))
6264          (arts (gnus-data-find-list article articles))
6265          result)
6266     (when (or (not gnus-summary-check-current)
6267               (not unread)
6268               (not (gnus-data-unread-p (car arts))))
6269       (setq arts (cdr arts)))
6270     (while arts
6271       (and (or (not unread)
6272                (gnus-data-unread-p (car arts)))
6273            (vectorp (gnus-data-header (car arts)))
6274            (gnus-subject-equal
6275             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6276            (setq result (car arts)
6277                  arts nil))
6278       (setq arts (cdr arts)))
6279     (and result
6280          (goto-char (gnus-data-pos result))
6281          (gnus-data-number result))))
6282
6283 (defun gnus-summary-search-forward (&optional unread subject backward)
6284   "Search forward for an article.
6285 If UNREAD, look for unread articles.  If SUBJECT, look for
6286 articles with that subject.  If BACKWARD, search backward instead."
6287   (cond (subject (gnus-summary-find-subject subject unread backward))
6288         (backward (gnus-summary-find-prev unread))
6289         (t (gnus-summary-find-next unread))))
6290
6291 (defun gnus-recenter (&optional n)
6292   "Center point in window and redisplay frame.
6293 Also do horizontal recentering."
6294   (interactive "P")
6295   (when (and gnus-auto-center-summary
6296              (not (eq gnus-auto-center-summary 'vertical)))
6297     (gnus-horizontal-recenter))
6298   (recenter n))
6299
6300 (defun gnus-summary-recenter ()
6301   "Center point in the summary window.
6302 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6303 displayed, no centering will be performed."
6304   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6305   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6306   (interactive)
6307   ;; The user has to want it.
6308   (when gnus-auto-center-summary
6309     (let* ((top (cond ((< (window-height) 4) 0)
6310                       ((< (window-height) 7) 1)
6311                       (t (if (numberp gnus-auto-center-summary)
6312                              gnus-auto-center-summary
6313                            (/ (1- (window-height)) 2)))))
6314            (height (1- (window-height)))
6315            (bottom (save-excursion (goto-char (point-max))
6316                                    (forward-line (- height))
6317                                    (point)))
6318            (window (get-buffer-window (current-buffer))))
6319       (when (get-buffer-window gnus-article-buffer)
6320         ;; Only do recentering when the article buffer is displayed,
6321         ;; Set the window start to either `bottom', which is the biggest
6322         ;; possible valid number, or the second line from the top,
6323         ;; whichever is the least.
6324         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6325           (if (> bottom top-pos)
6326               ;; Keep the second line from the top visible
6327               (set-window-start window top-pos t)
6328             ;; Try to keep the bottom line visible; if it's partially
6329             ;; obscured, either scroll one more line to make it fully
6330             ;; visible, or revert to using TOP-POS.
6331             (save-excursion
6332               (goto-char (point-max))
6333               (forward-line -1)
6334               (let ((last-line-start (point)))
6335                 (goto-char bottom)
6336                 (set-window-start window (point) t)
6337                 (when (not (pos-visible-in-window-p last-line-start window))
6338                   (forward-line 1)
6339                   (set-window-start window (min (point) top-pos) t)))))))
6340       ;; Do horizontal recentering while we're at it.
6341       (when (and (get-buffer-window (current-buffer) t)
6342                  (not (eq gnus-auto-center-summary 'vertical)))
6343         (let ((selected (selected-window)))
6344           (select-window (get-buffer-window (current-buffer) t))
6345           (gnus-summary-position-point)
6346           (gnus-horizontal-recenter)
6347           (select-window selected))))))
6348
6349 (defun gnus-summary-jump-to-group (newsgroup)
6350   "Move point to NEWSGROUP in group mode buffer."
6351   ;; Keep update point of group mode buffer if visible.
6352   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6353       (save-window-excursion
6354         ;; Take care of tree window mode.
6355         (when (get-buffer-window gnus-group-buffer)
6356           (pop-to-buffer gnus-group-buffer))
6357         (gnus-group-jump-to-group newsgroup))
6358     (save-excursion
6359       ;; Take care of tree window mode.
6360       (if (get-buffer-window gnus-group-buffer)
6361           (pop-to-buffer gnus-group-buffer)
6362         (set-buffer gnus-group-buffer))
6363       (gnus-group-jump-to-group newsgroup))))
6364
6365 ;; This function returns a list of article numbers based on the
6366 ;; difference between the ranges of read articles in this group and
6367 ;; the range of active articles.
6368 (defun gnus-list-of-unread-articles (group)
6369   (let* ((read (gnus-info-read (gnus-get-info group)))
6370          (active (or (gnus-active group) (gnus-activate-group group)))
6371          (last (cdr active))
6372          first nlast unread)
6373     ;; If none are read, then all are unread.
6374     (if (not read)
6375         (setq first (car active))
6376       ;; If the range of read articles is a single range, then the
6377       ;; first unread article is the article after the last read
6378       ;; article.  Sounds logical, doesn't it?
6379       (if (and (not (listp (cdr read)))
6380                (or (< (car read) (car active))
6381                    (progn (setq read (list read))
6382                           nil)))
6383           (setq first (max (car active) (1+ (cdr read))))
6384         ;; `read' is a list of ranges.
6385         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6386                                   (caar read)))
6387                   1)
6388           (setq first (car active)))
6389         (while read
6390           (when first
6391             (while (< first nlast)
6392               (setq unread (cons first unread)
6393                     first (1+ first))))
6394           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6395           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6396           (setq read (cdr read)))))
6397     ;; And add the last unread articles.
6398     (while (<= first last)
6399       (setq unread (cons first unread)
6400             first (1+ first)))
6401     ;; Return the list of unread articles.
6402     (delq 0 (nreverse unread))))
6403
6404 (defun gnus-list-of-read-articles (group)
6405   "Return a list of unread, unticked and non-dormant articles."
6406   (let* ((info (gnus-get-info group))
6407          (marked (gnus-info-marks info))
6408          (active (gnus-active group)))
6409     (and info active
6410          (gnus-list-range-difference
6411           (gnus-list-range-difference
6412            (gnus-sorted-complement
6413             (gnus-uncompress-range active)
6414             (gnus-list-of-unread-articles group))
6415            (cdr (assq 'dormant marked)))
6416           (cdr (assq 'tick marked))))))
6417
6418 ;; This function returns a sequence of article numbers based on the
6419 ;; difference between the ranges of read articles in this group and
6420 ;; the range of active articles.
6421 (defun gnus-sequence-of-unread-articles (group)
6422   (let* ((read (gnus-info-read (gnus-get-info group)))
6423          (active (or (gnus-active group) (gnus-activate-group group)))
6424          (last (cdr active))
6425          first nlast unread)
6426     ;; If none are read, then all are unread.
6427     (if (not read)
6428         (setq first (car active))
6429       ;; If the range of read articles is a single range, then the
6430       ;; first unread article is the article after the last read
6431       ;; article.  Sounds logical, doesn't it?
6432       (if (and (not (listp (cdr read)))
6433                (or (< (car read) (car active))
6434                    (progn (setq read (list read))
6435                           nil)))
6436           (setq first (max (car active) (1+ (cdr read))))
6437         ;; `read' is a list of ranges.
6438         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6439                                   (caar read)))
6440                   1)
6441           (setq first (car active)))
6442         (while read
6443           (when first
6444             (push (cons first nlast) unread))
6445           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6446           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6447           (setq read (cdr read)))))
6448     ;; And add the last unread articles.
6449     (cond ((< first last)
6450            (push (cons first last) unread))
6451           ((= first last)
6452            (push first unread)))
6453     ;; Return the sequence of unread articles.
6454     (delq 0 (nreverse unread))))
6455
6456 ;; Various summary commands
6457
6458 (defun gnus-summary-select-article-buffer ()
6459   "Reconfigure windows to show article buffer."
6460   (interactive)
6461   (if (not (gnus-buffer-live-p gnus-article-buffer))
6462       (error "There is no article buffer for this summary buffer")
6463     (gnus-configure-windows 'article)
6464     (select-window (get-buffer-window gnus-article-buffer))))
6465
6466 (defun gnus-summary-universal-argument (arg)
6467   "Perform any operation on all articles that are process/prefixed."
6468   (interactive "P")
6469   (let ((articles (gnus-summary-work-articles arg))
6470         func article)
6471     (if (eq
6472          (setq
6473           func
6474           (key-binding
6475            (read-key-sequence
6476             (substitute-command-keys
6477              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6478          'undefined)
6479         (gnus-error 1 "Undefined key")
6480       (save-excursion
6481         (while articles
6482           (gnus-summary-goto-subject (setq article (pop articles)))
6483           (let (gnus-newsgroup-processable)
6484             (command-execute func))
6485           (gnus-summary-remove-process-mark article)))))
6486   (gnus-summary-position-point))
6487
6488 (defun gnus-summary-toggle-truncation (&optional arg)
6489   "Toggle truncation of summary lines.
6490 With ARG, turn line truncation on if ARG is positive."
6491   (interactive "P")
6492   (setq truncate-lines
6493         (if (null arg) (not truncate-lines)
6494           (> (prefix-numeric-value arg) 0)))
6495   (redraw-display))
6496
6497 (defun gnus-summary-find-for-reselect ()
6498   "Return the number of an article to stay on across a reselect.
6499 The current article is considered, then following articles, then previous
6500 articles.  An article is sought which is not cancelled and isn't a temporary
6501 insertion from another group.  If there's no such then return a dummy 0."
6502   (let (found)
6503     (dolist (rev '(nil t))
6504       (unless found      ; don't demand the reverse list if we don't need it
6505         (let ((data (gnus-data-find-list
6506                      (gnus-summary-article-number) (gnus-data-list rev))))
6507           (while (and data (not found))
6508             (if (and (< 0 (gnus-data-number (car data)))
6509                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6510                 (setq found (gnus-data-number (car data))))
6511             (setq data (cdr data))))))
6512     (or found 0)))
6513
6514 (defun gnus-summary-reselect-current-group (&optional all rescan)
6515   "Exit and then reselect the current newsgroup.
6516 The prefix argument ALL means to select all articles."
6517   (interactive "P")
6518   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6519     (error "Ephemeral groups can't be reselected"))
6520   (let ((current-subject (gnus-summary-find-for-reselect))
6521         (group gnus-newsgroup-name))
6522     (setq gnus-newsgroup-begin nil)
6523     (gnus-summary-exit nil 'leave-hidden)
6524     ;; We have to adjust the point of group mode buffer because
6525     ;; point was moved to the next unread newsgroup by exiting.
6526     (gnus-summary-jump-to-group group)
6527     (when rescan
6528       (save-excursion
6529         (gnus-group-get-new-news-this-group 1)))
6530     (gnus-group-read-group all t)
6531     (gnus-summary-goto-subject current-subject nil t)))
6532
6533 (defun gnus-summary-rescan-group (&optional all)
6534   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6535   (interactive "P")
6536   (gnus-summary-reselect-current-group all t))
6537
6538 (defun gnus-summary-update-info (&optional non-destructive)
6539   (save-excursion
6540     (let ((group gnus-newsgroup-name))
6541       (when group
6542         (when gnus-newsgroup-kill-headers
6543           (setq gnus-newsgroup-killed
6544                 (gnus-compress-sequence
6545                  (gnus-sorted-union
6546                   (gnus-list-range-intersection
6547                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6548                   gnus-newsgroup-unreads)
6549                  t)))
6550         (unless (listp (cdr gnus-newsgroup-killed))
6551           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6552         (let ((headers gnus-newsgroup-headers))
6553           ;; Set the new ranges of read articles.
6554           (with-current-buffer gnus-group-buffer
6555             (gnus-undo-force-boundary))
6556           (gnus-update-read-articles
6557            group (gnus-sorted-union
6558                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6559           ;; Set the current article marks.
6560           (let ((gnus-newsgroup-scored
6561                  (if (and (not gnus-save-score)
6562                           (not non-destructive))
6563                      nil
6564                    gnus-newsgroup-scored)))
6565             (save-excursion
6566               (gnus-update-marks)))
6567           ;; Do the cross-ref thing.
6568           (when gnus-use-cross-reference
6569             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6570           ;; Do not switch windows but change the buffer to work.
6571           (set-buffer gnus-group-buffer)
6572           (unless (gnus-ephemeral-group-p group)
6573             (gnus-group-update-group group)))))))
6574
6575 (defun gnus-summary-save-newsrc (&optional force)
6576   "Save the current number of read/marked articles in the dribble buffer.
6577 The dribble buffer will then be saved.
6578 If FORCE (the prefix), also save the .newsrc file(s)."
6579   (interactive "P")
6580   (gnus-summary-update-info t)
6581   (if force
6582       (gnus-save-newsrc-file)
6583     (gnus-dribble-save)))
6584
6585 (defun gnus-summary-exit (&optional temporary leave-hidden)
6586   "Exit reading current newsgroup, and then return to group selection mode.
6587 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6588   (interactive)
6589   (gnus-set-global-variables)
6590   (when (gnus-buffer-live-p gnus-article-buffer)
6591     (save-excursion
6592       (set-buffer gnus-article-buffer)
6593       (mm-destroy-parts gnus-article-mime-handles)
6594       ;; Set it to nil for safety reason.
6595       (setq gnus-article-mime-handle-alist nil)
6596       (setq gnus-article-mime-handles nil)))
6597   (gnus-kill-save-kill-buffer)
6598   (gnus-async-halt-prefetch)
6599   (let* ((group gnus-newsgroup-name)
6600          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6601          (gnus-group-is-exiting-p t)
6602          (mode major-mode)
6603          (group-point nil)
6604          (buf (current-buffer)))
6605     (unless quit-config
6606       ;; Do adaptive scoring, and possibly save score files.
6607       (when gnus-newsgroup-adaptive
6608         (gnus-score-adaptive))
6609       (when gnus-use-scoring
6610         (gnus-score-save)))
6611     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6612     ;; If we have several article buffers, we kill them at exit.
6613     (unless gnus-single-article-buffer
6614       (gnus-kill-buffer gnus-original-article-buffer)
6615       (setq gnus-article-current nil))
6616     (when gnus-use-cache
6617       (gnus-cache-possibly-remove-articles)
6618       (gnus-cache-save-buffers))
6619     (gnus-async-prefetch-remove-group group)
6620     (when gnus-suppress-duplicates
6621       (gnus-dup-enter-articles))
6622     (when gnus-use-trees
6623       (gnus-tree-close group))
6624     (when gnus-use-cache
6625       (gnus-cache-write-active))
6626     ;; Remove entries for this group.
6627     (nnmail-purge-split-history (gnus-group-real-name group))
6628     ;; Make all changes in this group permanent.
6629     (unless quit-config
6630       (gnus-run-hooks 'gnus-exit-group-hook)
6631       (gnus-summary-update-info))
6632     (gnus-close-group group)
6633     ;; Make sure where we were, and go to next newsgroup.
6634     (set-buffer gnus-group-buffer)
6635     (unless quit-config
6636       (gnus-group-jump-to-group group))
6637     (gnus-run-hooks 'gnus-summary-exit-hook)
6638     (unless (or quit-config
6639                 ;; If this group has disappeared from the summary
6640                 ;; buffer, don't skip forwards.
6641                 (not (string= group (gnus-group-group-name))))
6642       (gnus-group-next-unread-group 1))
6643     (setq group-point (point))
6644     (if temporary
6645         nil                             ;Nothing to do.
6646       ;; If we have several article buffers, we kill them at exit.
6647       (unless gnus-single-article-buffer
6648         (gnus-kill-buffer gnus-article-buffer)
6649         (gnus-kill-buffer gnus-original-article-buffer)
6650         (setq gnus-article-current nil))
6651       (set-buffer buf)
6652       (if (not gnus-kill-summary-on-exit)
6653           (progn
6654             (gnus-deaden-summary)
6655             (setq mode nil))
6656         ;; We set all buffer-local variables to nil.  It is unclear why
6657         ;; this is needed, but if we don't, buffer-local variables are
6658         ;; not garbage-collected, it seems.  This would the lead to en
6659         ;; ever-growing Emacs.
6660         (gnus-summary-clear-local-variables)
6661         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6662           (gnus-summary-clear-local-variables))
6663         (when (get-buffer gnus-article-buffer)
6664           (bury-buffer gnus-article-buffer))
6665         ;; We clear the global counterparts of the buffer-local
6666         ;; variables as well, just to be on the safe side.
6667         (set-buffer gnus-group-buffer)
6668         (gnus-summary-clear-local-variables)
6669         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6670           (gnus-summary-clear-local-variables))
6671         ;; Return to group mode buffer.
6672         (when (eq mode 'gnus-summary-mode)
6673           (gnus-kill-buffer buf)))
6674       (setq gnus-current-select-method gnus-select-method)
6675       (if leave-hidden
6676           (set-buffer gnus-group-buffer)
6677         (pop-to-buffer gnus-group-buffer))
6678       (if (not quit-config)
6679           (progn
6680             (goto-char group-point)
6681             (unless leave-hidden
6682               (gnus-configure-windows 'group 'force)))
6683         (gnus-handle-ephemeral-exit quit-config))
6684       ;; Clear the current group name.
6685       (unless quit-config
6686         (setq gnus-newsgroup-name nil)))))
6687
6688 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6689 (defun gnus-summary-exit-no-update (&optional no-questions)
6690   "Quit reading current newsgroup without updating read article info."
6691   (interactive)
6692   (let* ((group gnus-newsgroup-name)
6693          (gnus-group-is-exiting-p t)
6694          (gnus-group-is-exiting-without-update-p t)
6695          (quit-config (gnus-group-quit-config group)))
6696     (when (or no-questions
6697               gnus-expert-user
6698               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6699       (gnus-async-halt-prefetch)
6700       (run-hooks 'gnus-summary-prepare-exit-hook)
6701       (when (gnus-buffer-live-p gnus-article-buffer)
6702         (save-excursion
6703           (set-buffer gnus-article-buffer)
6704           (mm-destroy-parts gnus-article-mime-handles)
6705           ;; Set it to nil for safety reason.
6706           (setq gnus-article-mime-handle-alist nil)
6707           (setq gnus-article-mime-handles nil)))
6708       ;; If we have several article buffers, we kill them at exit.
6709       (unless gnus-single-article-buffer
6710         (gnus-kill-buffer gnus-article-buffer)
6711         (gnus-kill-buffer gnus-original-article-buffer)
6712         (setq gnus-article-current nil))
6713       (if (not gnus-kill-summary-on-exit)
6714           (gnus-deaden-summary)
6715         (gnus-close-group group)
6716         (gnus-summary-clear-local-variables)
6717         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6718           (gnus-summary-clear-local-variables))
6719         (set-buffer gnus-group-buffer)
6720         (gnus-summary-clear-local-variables)
6721         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6722           (gnus-summary-clear-local-variables))
6723         (gnus-kill-buffer gnus-summary-buffer))
6724       (unless gnus-single-article-buffer
6725         (setq gnus-article-current nil))
6726       (when gnus-use-trees
6727         (gnus-tree-close group))
6728       (gnus-async-prefetch-remove-group group)
6729       (when (get-buffer gnus-article-buffer)
6730         (bury-buffer gnus-article-buffer))
6731       ;; Return to the group buffer.
6732       (gnus-configure-windows 'group 'force)
6733       ;; Clear the current group name.
6734       (setq gnus-newsgroup-name nil)
6735       (unless (gnus-ephemeral-group-p group)
6736         (gnus-group-update-group group))
6737       (when (equal (gnus-group-group-name) group)
6738         (gnus-group-next-unread-group 1))
6739       (when quit-config
6740         (gnus-handle-ephemeral-exit quit-config)))))
6741
6742 (defun gnus-handle-ephemeral-exit (quit-config)
6743   "Handle movement when leaving an ephemeral group.
6744 The state which existed when entering the ephemeral is reset."
6745   (if (not (buffer-name (car quit-config)))
6746       (gnus-configure-windows 'group 'force)
6747     (set-buffer (car quit-config))
6748     (cond ((eq major-mode 'gnus-summary-mode)
6749            (gnus-set-global-variables))
6750           ((eq major-mode 'gnus-article-mode)
6751            (save-excursion
6752              ;; The `gnus-summary-buffer' variable may point
6753              ;; to the old summary buffer when using a single
6754              ;; article buffer.
6755              (unless (gnus-buffer-live-p gnus-summary-buffer)
6756                (set-buffer gnus-group-buffer))
6757              (set-buffer gnus-summary-buffer)
6758              (gnus-set-global-variables))))
6759     (if (or (eq (cdr quit-config) 'article)
6760             (eq (cdr quit-config) 'pick))
6761         (progn
6762           ;; The current article may be from the ephemeral group
6763           ;; thus it is best that we reload this article
6764           ;;
6765           ;; If we're exiting from a large digest, this can be
6766           ;; extremely slow.  So, it's better not to reload it. -- jh.
6767           ;;(gnus-summary-show-article)
6768           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6769               (gnus-configure-windows 'pick 'force)
6770             (gnus-configure-windows (cdr quit-config) 'force)))
6771       (gnus-configure-windows (cdr quit-config) 'force))
6772     (when (eq major-mode 'gnus-summary-mode)
6773       (gnus-summary-next-subject 1 nil t)
6774       (gnus-summary-recenter)
6775       (gnus-summary-position-point))))
6776
6777 ;;; Dead summaries.
6778
6779 (defvar gnus-dead-summary-mode-map nil)
6780
6781 (unless gnus-dead-summary-mode-map
6782   (setq gnus-dead-summary-mode-map (make-keymap))
6783   (suppress-keymap gnus-dead-summary-mode-map)
6784   (substitute-key-definition
6785    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6786   (dolist (key '("\C-d" "\r" "\177" [delete]))
6787     (define-key gnus-dead-summary-mode-map
6788       key 'gnus-summary-wake-up-the-dead))
6789   (dolist (key '("q" "Q"))
6790     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6791
6792 (defvar gnus-dead-summary-mode nil
6793   "Minor mode for Gnus summary buffers.")
6794
6795 (defun gnus-dead-summary-mode (&optional arg)
6796   "Minor mode for Gnus summary buffers."
6797   (interactive "P")
6798   (when (eq major-mode 'gnus-summary-mode)
6799     (make-local-variable 'gnus-dead-summary-mode)
6800     (setq gnus-dead-summary-mode
6801           (if (null arg) (not gnus-dead-summary-mode)
6802             (> (prefix-numeric-value arg) 0)))
6803     (when gnus-dead-summary-mode
6804       (add-minor-mode
6805        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6806
6807 (defun gnus-deaden-summary ()
6808   "Make the current summary buffer into a dead summary buffer."
6809   ;; Kill any previous dead summary buffer.
6810   (when (and gnus-dead-summary
6811              (buffer-name gnus-dead-summary))
6812     (with-current-buffer gnus-dead-summary
6813       (when gnus-dead-summary-mode
6814         (kill-buffer (current-buffer)))))
6815   ;; Make this the current dead summary.
6816   (setq gnus-dead-summary (current-buffer))
6817   (gnus-dead-summary-mode 1)
6818   (let ((name (buffer-name)))
6819     (when (string-match "Summary" name)
6820       (rename-buffer
6821        (concat (substring name 0 (match-beginning 0)) "Dead "
6822                (substring name (match-beginning 0)))
6823        t)
6824       (bury-buffer))))
6825
6826 (defun gnus-kill-or-deaden-summary (buffer)
6827   "Kill or deaden the summary BUFFER."
6828   (save-excursion
6829     (when (and (buffer-name buffer)
6830                (not gnus-single-article-buffer))
6831       (with-current-buffer buffer
6832         (gnus-kill-buffer gnus-article-buffer)
6833         (gnus-kill-buffer gnus-original-article-buffer)))
6834     (cond
6835      ;; Kill the buffer.
6836      (gnus-kill-summary-on-exit
6837       (when (and gnus-use-trees
6838                  (gnus-buffer-exists-p buffer))
6839         (save-excursion
6840           (set-buffer buffer)
6841           (gnus-tree-close gnus-newsgroup-name)))
6842       (gnus-kill-buffer buffer))
6843      ;; Deaden the buffer.
6844      ((gnus-buffer-exists-p buffer)
6845       (save-excursion
6846         (set-buffer buffer)
6847         (gnus-deaden-summary))))))
6848
6849 (defun gnus-summary-wake-up-the-dead (&rest args)
6850   "Wake up the dead summary buffer."
6851   (interactive)
6852   (gnus-dead-summary-mode -1)
6853   (let ((name (buffer-name)))
6854     (when (string-match "Dead " name)
6855       (rename-buffer
6856        (concat (substring name 0 (match-beginning 0))
6857                (substring name (match-end 0)))
6858        t)))
6859   (gnus-message 3 "This dead summary is now alive again"))
6860
6861 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6862 (defun gnus-summary-fetch-faq (&optional faq-dir)
6863   "Fetch the FAQ for the current group.
6864 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6865 in."
6866   (interactive
6867    (list
6868     (when current-prefix-arg
6869       (completing-read
6870        "FAQ dir: " (and (listp gnus-group-faq-directory)
6871                         (mapcar 'list
6872                                 gnus-group-faq-directory))))))
6873   (let (gnus-faq-buffer)
6874     (when (setq gnus-faq-buffer
6875                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6876       (gnus-configure-windows 'summary-faq))))
6877
6878 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6879 (defun gnus-summary-describe-group (&optional force)
6880   "Describe the current newsgroup."
6881   (interactive "P")
6882   (gnus-group-describe-group force gnus-newsgroup-name))
6883
6884 (defun gnus-summary-describe-briefly ()
6885   "Describe summary mode commands briefly."
6886   (interactive)
6887   (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")))
6888
6889 ;; Walking around group mode buffer from summary mode.
6890
6891 (defun gnus-summary-next-group (&optional no-article target-group backward)
6892   "Exit current newsgroup and then select next unread newsgroup.
6893 If prefix argument NO-ARTICLE is non-nil, no article is selected
6894 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6895 previous group instead."
6896   (interactive "P")
6897   ;; Stop pre-fetching.
6898   (gnus-async-halt-prefetch)
6899   (let ((current-group gnus-newsgroup-name)
6900         (current-buffer (current-buffer))
6901         entered)
6902     ;; First we semi-exit this group to update Xrefs and all variables.
6903     ;; We can't do a real exit, because the window conf must remain
6904     ;; the same in case the user is prompted for info, and we don't
6905     ;; want the window conf to change before that...
6906     (gnus-summary-exit t)
6907     (while (not entered)
6908       ;; Then we find what group we are supposed to enter.
6909       (set-buffer gnus-group-buffer)
6910       (gnus-group-jump-to-group current-group)
6911       (setq target-group
6912             (or target-group
6913                 (if (eq gnus-keep-same-level 'best)
6914                     (gnus-summary-best-group gnus-newsgroup-name)
6915                   (gnus-summary-search-group backward gnus-keep-same-level))))
6916       (if (not target-group)
6917           ;; There are no further groups, so we return to the group
6918           ;; buffer.
6919           (progn
6920             (gnus-message 5 "Returning to the group buffer")
6921             (setq entered t)
6922             (when (gnus-buffer-live-p current-buffer)
6923               (set-buffer current-buffer)
6924               (gnus-summary-exit))
6925             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6926         ;; We try to enter the target group.
6927         (gnus-group-jump-to-group target-group)
6928         (let ((unreads (gnus-group-group-unread)))
6929           (if (and (or (eq t unreads)
6930                        (and unreads (not (zerop unreads))))
6931                    (gnus-summary-read-group
6932                     target-group nil no-article
6933                     (and (buffer-name current-buffer) current-buffer)
6934                     nil backward))
6935               (setq entered t)
6936             (setq current-group target-group
6937                   target-group nil)))))))
6938
6939 (defun gnus-summary-prev-group (&optional no-article)
6940   "Exit current newsgroup and then select previous unread newsgroup.
6941 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6942   (interactive "P")
6943   (gnus-summary-next-group no-article nil t))
6944
6945 ;; Walking around summary lines.
6946
6947 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6948   "Go to the first subject satisfying any non-nil constraint.
6949 If UNREAD is non-nil, the article should be unread.
6950 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6951 If UNSEEN is non-nil, the article should be unseen.
6952 Returns the article selected or nil if there are no matching articles."
6953   (interactive "P")
6954   (cond
6955    ;; Empty summary.
6956    ((null gnus-newsgroup-data)
6957     (gnus-message 3 "No articles in the group")
6958     nil)
6959    ;; Pick the first article.
6960    ((not (or unread undownloaded unseen))
6961     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6962     (gnus-data-number (car gnus-newsgroup-data)))
6963    ;; Find the first unread article.
6964    (t
6965     (let ((data gnus-newsgroup-data))
6966       (while (and data
6967                   (let ((num (gnus-data-number (car data))))
6968                     (or (memq num gnus-newsgroup-unfetched)
6969                         (not (or (and unread
6970                                       (memq num gnus-newsgroup-unreads))
6971                                  (and undownloaded
6972                                       (memq num gnus-newsgroup-undownloaded))
6973                                  (and unseen
6974                                       (memq num gnus-newsgroup-unseen)))))))
6975         (setq data (cdr data)))
6976       (prog1
6977           (if data
6978               (progn
6979                 (goto-char (gnus-data-pos (car data)))
6980                 (gnus-data-number (car data)))
6981             (gnus-message 3 "No more%s articles"
6982                           (let* ((r (when unread " unread"))
6983                                  (d (when undownloaded " undownloaded"))
6984                                  (s (when unseen " unseen"))
6985                                  (l (delq nil (list r d s))))
6986                             (cond ((= 3 (length l))
6987                                    (concat r "," d ", or" s))
6988                                   ((= 2 (length l))
6989                                    (concat (car l) ", or" (cadr l)))
6990                                   ((= 1 (length l))
6991                                    (car l))
6992                                   (t
6993                                    ""))))
6994             nil
6995             )
6996         (gnus-summary-position-point))))))
6997
6998 (defun gnus-summary-next-subject (n &optional unread dont-display)
6999   "Go to next N'th summary line.
7000 If N is negative, go to the previous N'th subject line.
7001 If UNREAD is non-nil, only unread articles are selected.
7002 The difference between N and the actual number of steps taken is
7003 returned."
7004   (interactive "p")
7005   (let ((backward (< n 0))
7006         (n (abs n)))
7007     (while (and (> n 0)
7008                 (if backward
7009                     (gnus-summary-find-prev unread)
7010                   (gnus-summary-find-next unread)))
7011       (unless (zerop (setq n (1- n)))
7012         (gnus-summary-show-thread)))
7013     (when (/= 0 n)
7014       (gnus-message 7 "No more%s articles"
7015                     (if unread " unread" "")))
7016     (unless dont-display
7017       (gnus-summary-recenter)
7018       (gnus-summary-position-point))
7019     n))
7020
7021 (defun gnus-summary-next-unread-subject (n)
7022   "Go to next N'th unread summary line."
7023   (interactive "p")
7024   (gnus-summary-next-subject n t))
7025
7026 (defun gnus-summary-prev-subject (n &optional unread)
7027   "Go to previous N'th summary line.
7028 If optional argument UNREAD is non-nil, only unread article is selected."
7029   (interactive "p")
7030   (gnus-summary-next-subject (- n) unread))
7031
7032 (defun gnus-summary-prev-unread-subject (n)
7033   "Go to previous N'th unread summary line."
7034   (interactive "p")
7035   (gnus-summary-next-subject (- n) t))
7036
7037 (defun gnus-summary-goto-subjects (articles)
7038   "Insert the subject header for ARTICLES in the current buffer."
7039   (save-excursion
7040     (dolist (article articles)
7041       (gnus-summary-goto-subject article t)))
7042   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7043   (gnus-summary-position-point))
7044  
7045 (defun gnus-summary-goto-subject (article &optional force silent)
7046   "Go the subject line of ARTICLE.
7047 If FORCE, also allow jumping to articles not currently shown."
7048   (interactive "nArticle number: ")
7049   (unless (numberp article)
7050     (error "Article %s is not a number" article))
7051   (let ((b (point))
7052         (data (gnus-data-find article)))
7053     ;; We read in the article if we have to.
7054     (and (not data)
7055          force
7056          (gnus-summary-insert-subject
7057           article
7058           (if (or (numberp force) (vectorp force)) force)
7059           t)
7060          (setq data (gnus-data-find article)))
7061     (goto-char b)
7062     (if (not data)
7063         (progn
7064           (unless silent
7065             (gnus-message 3 "Can't find article %d" article))
7066           nil)
7067       (let ((pt (gnus-data-pos data)))
7068         (goto-char pt)
7069         (gnus-summary-set-article-display-arrow pt))
7070       (gnus-summary-position-point)
7071       article)))
7072
7073 ;; Walking around summary lines with displaying articles.
7074
7075 (defun gnus-summary-expand-window (&optional arg)
7076   "Make the summary buffer take up the entire Emacs frame.
7077 Given a prefix, will force an `article' buffer configuration."
7078   (interactive "P")
7079   (if arg
7080       (gnus-configure-windows 'article 'force)
7081     (gnus-configure-windows 'summary 'force)))
7082
7083 (defun gnus-summary-display-article (article &optional all-header)
7084   "Display ARTICLE in article buffer."
7085   (when (gnus-buffer-live-p gnus-article-buffer)
7086     (with-current-buffer gnus-article-buffer
7087       (mm-enable-multibyte)))
7088   (gnus-set-global-variables)
7089   (when (gnus-buffer-live-p gnus-article-buffer)
7090     (with-current-buffer gnus-article-buffer
7091       (setq gnus-article-charset gnus-newsgroup-charset)
7092       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7093       (mm-enable-multibyte)))
7094   (if (null article)
7095       nil
7096     (prog1
7097         (if gnus-summary-display-article-function
7098             (funcall gnus-summary-display-article-function article all-header)
7099           (gnus-article-prepare article all-header))
7100       (gnus-run-hooks 'gnus-select-article-hook)
7101       (when (and gnus-current-article
7102                  (not (zerop gnus-current-article)))
7103         (gnus-summary-goto-subject gnus-current-article))
7104       (gnus-summary-recenter)
7105       (when (and gnus-use-trees gnus-show-threads)
7106         (gnus-possibly-generate-tree article)
7107         (gnus-highlight-selected-tree article))
7108       ;; Successfully display article.
7109       (gnus-article-set-window-start
7110        (cdr (assq article gnus-newsgroup-bookmarks))))))
7111
7112 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7113   "Select the current article.
7114 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7115 non-nil, the article will be re-fetched even if it already present in
7116 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7117 be displayed."
7118   ;; Make sure we are in the summary buffer to work around bbdb bug.
7119   (unless (eq major-mode 'gnus-summary-mode)
7120     (set-buffer gnus-summary-buffer))
7121   (let ((article (or article (gnus-summary-article-number)))
7122         (all-headers (not (not all-headers))) ;Must be t or nil.
7123         gnus-summary-display-article-function)
7124     (and (not pseudo)
7125          (gnus-summary-article-pseudo-p article)
7126          (error "This is a pseudo-article"))
7127     (save-excursion
7128       (set-buffer gnus-summary-buffer)
7129       (if (or (and gnus-single-article-buffer
7130                    (or (null gnus-current-article)
7131                        (null gnus-article-current)
7132                        (null (get-buffer gnus-article-buffer))
7133                        (not (eq article (cdr gnus-article-current)))
7134                        (not (equal (car gnus-article-current)
7135                                    gnus-newsgroup-name))))
7136               (and (not gnus-single-article-buffer)
7137                    (or (null gnus-current-article)
7138                        (not (eq gnus-current-article article))))
7139               force)
7140           ;; The requested article is different from the current article.
7141           (progn
7142             (gnus-summary-display-article article all-headers)
7143             (when (gnus-buffer-live-p gnus-article-buffer)
7144               (with-current-buffer gnus-article-buffer
7145                 (if (not gnus-article-decoded-p) ;; a local variable
7146                     (mm-disable-multibyte))))
7147             (gnus-article-set-window-start
7148              (cdr (assq article gnus-newsgroup-bookmarks)))
7149             article)
7150         'old))))
7151
7152 (defun gnus-summary-force-verify-and-decrypt ()
7153   "Display buttons for signed/encrypted parts and verify/decrypt them."
7154   (interactive)
7155   (let ((mm-verify-option 'known)
7156         (mm-decrypt-option 'known)
7157         (gnus-article-emulate-mime t)
7158         (mm-fill-flowed nil)
7159         (gnus-buttonized-mime-types (append (list "multipart/signed"
7160                                                   "multipart/encrypted")
7161                                             gnus-buttonized-mime-types)))
7162     (gnus-summary-select-article nil 'force)))
7163
7164 (defun gnus-summary-set-current-mark (&optional current-mark)
7165   "Obsolete function."
7166   nil)
7167
7168 (defun gnus-summary-next-article (&optional unread subject backward push)
7169   "Select the next article.
7170 If UNREAD, only unread articles are selected.
7171 If SUBJECT, only articles with SUBJECT are selected.
7172 If BACKWARD, the previous article is selected instead of the next."
7173   (interactive "P")
7174   (cond
7175    ;; Is there such an article?
7176    ((and (gnus-summary-search-forward unread subject backward)
7177          (or (gnus-summary-display-article (gnus-summary-article-number))
7178              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7179     (gnus-summary-position-point))
7180    ;; If not, we try the first unread, if that is wanted.
7181    ((and subject
7182          gnus-auto-select-same
7183          (gnus-summary-first-unread-article))
7184     (gnus-summary-position-point)
7185     (gnus-message 6 "Wrapped"))
7186    ;; Try to get next/previous article not displayed in this group.
7187    ((and gnus-auto-extend-newsgroup
7188          (not unread) (not subject))
7189     (gnus-summary-goto-article
7190      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7191      nil (count-lines (point-min) (point))))
7192    ;; Go to next/previous group.
7193    (t
7194     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7195       (gnus-summary-jump-to-group gnus-newsgroup-name))
7196     (let ((cmd last-command-char)
7197           (point
7198            (with-current-buffer gnus-group-buffer
7199              (point)))
7200           (group
7201            (if (eq gnus-keep-same-level 'best)
7202                (gnus-summary-best-group gnus-newsgroup-name)
7203              (gnus-summary-search-group backward gnus-keep-same-level))))
7204       ;; For some reason, the group window gets selected.  We change
7205       ;; it back.
7206       (select-window (get-buffer-window (current-buffer)))
7207       ;; Select next unread newsgroup automagically.
7208       (cond
7209        ((or (not gnus-auto-select-next)
7210             (not cmd))
7211         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7212        ((or (eq gnus-auto-select-next 'quietly)
7213             (and (eq gnus-auto-select-next 'slightly-quietly)
7214                  push)
7215             (and (eq gnus-auto-select-next 'almost-quietly)
7216                  (gnus-summary-last-article-p)))
7217         ;; Select quietly.
7218         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7219             (gnus-summary-exit)
7220           (gnus-message 7 "No more%s articles (%s)..."
7221                         (if unread " unread" "")
7222                         (if group (concat "selecting " group)
7223                           "exiting"))
7224           (gnus-summary-next-group nil group backward)))
7225        (t
7226         (when (gnus-key-press-event-p last-input-event)
7227           (gnus-summary-walk-group-buffer
7228            gnus-newsgroup-name cmd unread backward point))))))))
7229
7230 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7231   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7232                       (?\C-p (gnus-group-prev-unread-group 1))))
7233         (cursor-in-echo-area t)
7234         keve key group ended prompt)
7235     (save-excursion
7236       (set-buffer gnus-group-buffer)
7237       (goto-char start)
7238       (setq group
7239             (if (eq gnus-keep-same-level 'best)
7240                 (gnus-summary-best-group gnus-newsgroup-name)
7241               (gnus-summary-search-group backward gnus-keep-same-level))))
7242     (while (not ended)
7243       (setq prompt
7244             (format
7245              "No more%s articles%s " (if unread " unread" "")
7246              (if (and group
7247                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7248                  (format " (Type %s for %s [%s])"
7249                          (single-key-description cmd) group
7250                          (gnus-group-unread group))
7251                (format " (Type %s to exit %s)"
7252                        (single-key-description cmd)
7253                        gnus-newsgroup-name))))
7254       ;; Confirm auto selection.
7255       (setq key (car (setq keve (gnus-read-event-char prompt)))
7256             ended t)
7257       (cond
7258        ((assq key keystrokes)
7259         (let ((obuf (current-buffer)))
7260           (switch-to-buffer gnus-group-buffer)
7261           (when group
7262             (gnus-group-jump-to-group group))
7263           (eval (cadr (assq key keystrokes)))
7264           (setq group (gnus-group-group-name))
7265           (switch-to-buffer obuf))
7266         (setq ended nil))
7267        ((equal key cmd)
7268         (if (or (not group)
7269                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7270             (gnus-summary-exit)
7271           (gnus-summary-next-group nil group backward)))
7272        (t
7273         (push (cdr keve) unread-command-events))))))
7274
7275 (defun gnus-summary-next-unread-article ()
7276   "Select unread article after current one."
7277   (interactive)
7278   (gnus-summary-next-article
7279    (or (not (eq gnus-summary-goto-unread 'never))
7280        (gnus-summary-last-article-p (gnus-summary-article-number)))
7281    (and gnus-auto-select-same
7282         (gnus-summary-article-subject))))
7283
7284 (defun gnus-summary-prev-article (&optional unread subject)
7285   "Select the article after the current one.
7286 If UNREAD is non-nil, only unread articles are selected."
7287   (interactive "P")
7288   (gnus-summary-next-article unread subject t))
7289
7290 (defun gnus-summary-prev-unread-article ()
7291   "Select unread article before current one."
7292   (interactive)
7293   (gnus-summary-prev-article
7294    (or (not (eq gnus-summary-goto-unread 'never))
7295        (gnus-summary-first-article-p (gnus-summary-article-number)))
7296    (and gnus-auto-select-same
7297         (gnus-summary-article-subject))))
7298
7299 (defun gnus-summary-next-page (&optional lines circular stop)
7300   "Show next page of the selected article.
7301 If at the end of the current article, select the next article.
7302 LINES says how many lines should be scrolled up.
7303
7304 If CIRCULAR is non-nil, go to the start of the article instead of
7305 selecting the next article when reaching the end of the current
7306 article.
7307
7308 If STOP is non-nil, just stop when reaching the end of the message.
7309
7310 Also see the variable `gnus-article-skip-boring'."
7311   (interactive "P")
7312   (setq gnus-summary-buffer (current-buffer))
7313   (gnus-set-global-variables)
7314   (let ((article (gnus-summary-article-number))
7315         (article-window (get-buffer-window gnus-article-buffer t))
7316         endp)
7317     ;; If the buffer is empty, we have no article.
7318     (unless article
7319       (error "No article to select"))
7320     (gnus-configure-windows 'article)
7321     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7322         (if (and (eq gnus-summary-goto-unread 'never)
7323                  (not (gnus-summary-last-article-p article)))
7324             (gnus-summary-next-article)
7325           (gnus-summary-next-unread-article))
7326       (if (or (null gnus-current-article)
7327               (null gnus-article-current)
7328               (/= article (cdr gnus-article-current))
7329               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7330           ;; Selected subject is different from current article's.
7331           (gnus-summary-display-article article)
7332         (when article-window
7333           (gnus-eval-in-buffer-window gnus-article-buffer
7334             (setq endp (or (gnus-article-next-page lines)
7335                            (gnus-article-only-boring-p))))
7336           (when endp
7337             (cond (stop
7338                    (gnus-message 3 "End of message"))
7339                   (circular
7340                    (gnus-summary-beginning-of-article))
7341                   (lines
7342                    (gnus-message 3 "End of message"))
7343                   ((null lines)
7344                    (if (and (eq gnus-summary-goto-unread 'never)
7345                             (not (gnus-summary-last-article-p article)))
7346                        (gnus-summary-next-article)
7347                      (gnus-summary-next-unread-article))))))))
7348     (gnus-summary-recenter)
7349     (gnus-summary-position-point)))
7350
7351 (defun gnus-summary-prev-page (&optional lines move)
7352   "Show previous page of selected article.
7353 Argument LINES specifies lines to be scrolled down.
7354 If MOVE, move to the previous unread article if point is at
7355 the beginning of the buffer."
7356   (interactive "P")
7357   (let ((article (gnus-summary-article-number))
7358         (article-window (get-buffer-window gnus-article-buffer t))
7359         endp)
7360     (gnus-configure-windows 'article)
7361     (if (or (null gnus-current-article)
7362             (null gnus-article-current)
7363             (/= article (cdr gnus-article-current))
7364             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7365         ;; Selected subject is different from current article's.
7366         (gnus-summary-display-article article)
7367       (gnus-summary-recenter)
7368       (when article-window
7369         (gnus-eval-in-buffer-window gnus-article-buffer
7370           (setq endp (gnus-article-prev-page lines)))
7371         (when (and move endp)
7372           (cond (lines
7373                  (gnus-message 3 "Beginning of message"))
7374                 ((null lines)
7375                  (if (and (eq gnus-summary-goto-unread 'never)
7376                           (not (gnus-summary-first-article-p article)))
7377                      (gnus-summary-prev-article)
7378                    (gnus-summary-prev-unread-article))))))))
7379   (gnus-summary-position-point))
7380
7381 (defun gnus-summary-prev-page-or-article (&optional lines)
7382   "Show previous page of selected article.
7383 Argument LINES specifies lines to be scrolled down.
7384 If at the beginning of the article, go to the next article."
7385   (interactive "P")
7386   (gnus-summary-prev-page lines t))
7387
7388 (defun gnus-summary-scroll-up (lines)
7389   "Scroll up (or down) one line current article.
7390 Argument LINES specifies lines to be scrolled up (or down if negative)."
7391   (interactive "p")
7392   (gnus-configure-windows 'article)
7393   (gnus-summary-show-thread)
7394   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7395     (gnus-eval-in-buffer-window gnus-article-buffer
7396       (cond ((> lines 0)
7397              (when (gnus-article-next-page lines)
7398                (gnus-message 3 "End of message")))
7399             ((< lines 0)
7400              (gnus-article-prev-page (- lines))))))
7401   (gnus-summary-recenter)
7402   (gnus-summary-position-point))
7403
7404 (defun gnus-summary-scroll-down (lines)
7405   "Scroll down (or up) one line current article.
7406 Argument LINES specifies lines to be scrolled down (or up if negative)."
7407   (interactive "p")
7408   (gnus-summary-scroll-up (- lines)))
7409
7410 (defun gnus-summary-next-same-subject ()
7411   "Select next article which has the same subject as current one."
7412   (interactive)
7413   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7414
7415 (defun gnus-summary-prev-same-subject ()
7416   "Select previous article which has the same subject as current one."
7417   (interactive)
7418   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7419
7420 (defun gnus-summary-next-unread-same-subject ()
7421   "Select next unread article which has the same subject as current one."
7422   (interactive)
7423   (gnus-summary-next-article t (gnus-summary-article-subject)))
7424
7425 (defun gnus-summary-prev-unread-same-subject ()
7426   "Select previous unread article which has the same subject as current one."
7427   (interactive)
7428   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7429
7430 (defun gnus-summary-first-unread-article ()
7431   "Select the first unread article.
7432 Return nil if there are no unread articles."
7433   (interactive)
7434   (prog1
7435       (when (gnus-summary-first-subject t)
7436         (gnus-summary-show-thread)
7437         (gnus-summary-first-subject t)
7438         (gnus-summary-display-article (gnus-summary-article-number)))
7439     (gnus-summary-position-point)))
7440
7441 (defun gnus-summary-first-unread-subject ()
7442   "Place the point on the subject line of the first unread article.
7443 Return nil if there are no unread articles."
7444   (interactive)
7445   (prog1
7446       (when (gnus-summary-first-subject t)
7447         (gnus-summary-show-thread)
7448         (gnus-summary-first-subject t))
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-first-unseen-subject ()
7452   "Place the point on the subject line of the first unseen article.
7453 Return nil if there are no unseen articles."
7454   (interactive)
7455   (prog1
7456       (when (gnus-summary-first-subject nil nil t)
7457         (gnus-summary-show-thread)
7458         (gnus-summary-first-subject nil nil t))
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-first-unseen-or-unread-subject ()
7462   "Place the point on the subject line of the first unseen article or,
7463 if all article have been seen, on the subject line of the first unread
7464 article."
7465   (interactive)
7466   (prog1
7467       (unless (when (gnus-summary-first-subject nil nil t)
7468                 (gnus-summary-show-thread)
7469                 (gnus-summary-first-subject nil nil t))
7470         (when (gnus-summary-first-subject t)
7471           (gnus-summary-show-thread)
7472           (gnus-summary-first-subject t)))
7473     (gnus-summary-position-point)))
7474
7475 (defun gnus-summary-first-article ()
7476   "Select the first article.
7477 Return nil if there are no articles."
7478   (interactive)
7479   (prog1
7480       (when (gnus-summary-first-subject)
7481         (gnus-summary-show-thread)
7482         (gnus-summary-first-subject)
7483         (gnus-summary-display-article (gnus-summary-article-number)))
7484     (gnus-summary-position-point)))
7485
7486 (defun gnus-summary-best-unread-article (&optional arg)
7487   "Select the unread article with the highest score.
7488 If given a prefix argument, select the next unread article that has a
7489 score higher than the default score."
7490   (interactive "P")
7491   (let ((article (if arg
7492                      (gnus-summary-better-unread-subject)
7493                    (gnus-summary-best-unread-subject))))
7494     (if article
7495         (gnus-summary-goto-article article)
7496       (error "No unread articles"))))
7497
7498 (defun gnus-summary-best-unread-subject ()
7499   "Select the unread subject with the highest score."
7500   (interactive)
7501   (let ((best -1000000)
7502         (data gnus-newsgroup-data)
7503         article score)
7504     (while data
7505       (and (gnus-data-unread-p (car data))
7506            (> (setq score
7507                     (gnus-summary-article-score (gnus-data-number (car data))))
7508               best)
7509            (setq best score
7510                  article (gnus-data-number (car data))))
7511       (setq data (cdr data)))
7512     (when article
7513       (gnus-summary-goto-subject article))
7514     (gnus-summary-position-point)
7515     article))
7516
7517 (defun gnus-summary-better-unread-subject ()
7518   "Select the first unread subject that has a score over the default score."
7519   (interactive)
7520   (let ((data gnus-newsgroup-data)
7521         article score)
7522     (while (and (setq article (gnus-data-number (car data)))
7523                 (or (gnus-data-read-p (car data))
7524                     (not (> (gnus-summary-article-score article)
7525                             gnus-summary-default-score))))
7526       (setq data (cdr data)))
7527     (when article
7528       (gnus-summary-goto-subject article))
7529     (gnus-summary-position-point)
7530     article))
7531
7532 (defun gnus-summary-last-subject ()
7533   "Go to the last displayed subject line in the group."
7534   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7535     (when article
7536       (gnus-summary-goto-subject article))))
7537
7538 (defun gnus-summary-goto-article (article &optional all-headers force)
7539   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7540 If ALL-HEADERS is non-nil, no header lines are hidden.
7541 If FORCE, go to the article even if it isn't displayed.  If FORCE
7542 is a number, it is the line the article is to be displayed on."
7543   (interactive
7544    (list
7545     (completing-read
7546      "Article number or Message-ID: "
7547      (mapcar (lambda (number) (list (int-to-string number)))
7548              gnus-newsgroup-limit))
7549     current-prefix-arg
7550     t))
7551   (prog1
7552       (if (and (stringp article)
7553                (string-match "@\\|%40" article))
7554           (gnus-summary-refer-article article)
7555         (when (stringp article)
7556           (setq article (string-to-number article)))
7557         (if (gnus-summary-goto-subject article force)
7558             (gnus-summary-display-article article all-headers)
7559           (gnus-message 4 "Couldn't go to article %s" article) nil))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-goto-last-article ()
7563   "Go to the previously read article."
7564   (interactive)
7565   (prog1
7566       (when gnus-last-article
7567         (gnus-summary-goto-article gnus-last-article nil t))
7568     (gnus-summary-position-point)))
7569
7570 (defun gnus-summary-pop-article (number)
7571   "Pop one article off the history and go to the previous.
7572 NUMBER articles will be popped off."
7573   (interactive "p")
7574   (let (to)
7575     (setq gnus-newsgroup-history
7576           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7577     (if to
7578         (gnus-summary-goto-article (car to) nil t)
7579       (error "Article history empty")))
7580   (gnus-summary-position-point))
7581
7582 ;; Summary commands and functions for limiting the summary buffer.
7583
7584 (defun gnus-summary-limit-to-articles (n)
7585   "Limit the summary buffer to the next N articles.
7586 If not given a prefix, use the process marked articles instead."
7587   (interactive "P")
7588   (prog1
7589       (let ((articles (gnus-summary-work-articles n)))
7590         (setq gnus-newsgroup-processable nil)
7591         (gnus-summary-limit articles))
7592     (gnus-summary-position-point)))
7593
7594 (defun gnus-summary-pop-limit (&optional total)
7595   "Restore the previous limit.
7596 If given a prefix, remove all limits."
7597   (interactive "P")
7598   (when total
7599     (setq gnus-newsgroup-limits
7600           (list (mapcar (lambda (h) (mail-header-number h))
7601                         gnus-newsgroup-headers))))
7602   (unless gnus-newsgroup-limits
7603     (error "No limit to pop"))
7604   (prog1
7605       (gnus-summary-limit nil 'pop)
7606     (gnus-summary-position-point)))
7607
7608 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7609   "Limit the summary buffer to articles that have subjects that match a regexp.
7610 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7611   (interactive
7612    (list (read-string (if current-prefix-arg
7613                           "Exclude subject (regexp): "
7614                         "Limit to subject (regexp): "))
7615          nil current-prefix-arg))
7616   (unless header
7617     (setq header "subject"))
7618   (when (not (equal "" subject))
7619     (prog1
7620         (let ((articles (gnus-summary-find-matching
7621                          (or header "subject") subject 'all nil nil
7622                          not-matching)))
7623           (unless articles
7624             (error "Found no matches for \"%s\"" subject))
7625           (gnus-summary-limit articles))
7626       (gnus-summary-position-point))))
7627
7628 (defun gnus-summary-limit-to-author (from &optional not-matching)
7629   "Limit the summary buffer to articles that have authors that match a regexp.
7630 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7631   (interactive
7632    (list (read-string (if current-prefix-arg
7633                           "Exclude author (regexp): "
7634                         "Limit to author (regexp): "))
7635          current-prefix-arg))
7636   (gnus-summary-limit-to-subject from "from" not-matching))
7637
7638 (defun gnus-summary-limit-to-age (age &optional younger-p)
7639   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7640 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7641 articles that are younger than AGE days."
7642   (interactive
7643    (let ((younger current-prefix-arg)
7644          (days-got nil)
7645          days)
7646      (while (not days-got)
7647        (setq days (if younger
7648                       (read-string "Limit to articles younger than (in days, older when negative): ")
7649                     (read-string
7650                      "Limit to articles older than (in days, younger when negative): ")))
7651        (when (> (length days) 0)
7652          (setq days (read days)))
7653        (if (numberp days)
7654            (progn
7655              (setq days-got t)
7656              (when (< days 0)
7657                (setq younger (not younger))
7658                (setq days (* days -1))))
7659          (message "Please enter a number.")
7660          (sleep-for 1)))
7661      (list days younger)))
7662   (prog1
7663       (let ((data gnus-newsgroup-data)
7664             (cutoff (days-to-time age))
7665             articles d date is-younger)
7666         (while (setq d (pop data))
7667           (when (and (vectorp (gnus-data-header d))
7668                      (setq date (mail-header-date (gnus-data-header d))))
7669             (setq is-younger (time-less-p
7670                               (time-since (condition-case ()
7671                                               (date-to-time date)
7672                                             (error '(0 0))))
7673                               cutoff))
7674             (when (if younger-p
7675                       is-younger
7676                     (not is-younger))
7677               (push (gnus-data-number d) articles))))
7678         (gnus-summary-limit (nreverse articles)))
7679     (gnus-summary-position-point)))
7680
7681 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7682   "Limit the summary buffer to articles that match an 'extra' header."
7683   (interactive
7684    (let ((header
7685           (intern
7686            (gnus-completing-read-with-default
7687             (symbol-name (car gnus-extra-headers))
7688             (if current-prefix-arg
7689                 "Exclude extra header:"
7690               "Limit extra header:")
7691             (mapcar (lambda (x)
7692                       (cons (symbol-name x) x))
7693                     gnus-extra-headers)
7694             nil
7695             t))))
7696      (list header
7697            (read-string (format "%s header %s (regexp): "
7698                                 (if current-prefix-arg "Exclude" "Limit to")
7699                                 header))
7700            current-prefix-arg)))
7701   (when (not (equal "" regexp))
7702     (prog1
7703         (let ((articles (gnus-summary-find-matching
7704                          (cons 'extra header) regexp 'all nil nil
7705                          not-matching)))
7706           (unless articles
7707             (error "Found no matches for \"%s\"" regexp))
7708           (gnus-summary-limit articles))
7709       (gnus-summary-position-point))))
7710
7711 (defun gnus-summary-limit-to-display-predicate ()
7712   "Limit the summary buffer to the predicated in the `display' group parameter."
7713   (interactive)
7714   (unless gnus-newsgroup-display
7715     (error "There is no `display' group parameter"))
7716   (let (articles)
7717     (dolist (number gnus-newsgroup-articles)
7718       (when (funcall gnus-newsgroup-display)
7719         (push number articles)))
7720     (gnus-summary-limit articles))
7721   (gnus-summary-position-point))
7722
7723 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7724 (make-obsolete
7725  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7726
7727 (defun gnus-summary-limit-to-unread (&optional all)
7728   "Limit the summary buffer to articles that are not marked as read.
7729 If ALL is non-nil, limit strictly to unread articles."
7730   (interactive "P")
7731   (if all
7732       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7733     (gnus-summary-limit-to-marks
7734      ;; Concat all the marks that say that an article is read and have
7735      ;; those removed.
7736      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7737            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7738            gnus-low-score-mark gnus-expirable-mark
7739            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7740            gnus-duplicate-mark gnus-souped-mark)
7741      'reverse)))
7742
7743 (defun gnus-summary-limit-to-replied (&optional unreplied)
7744   "Limit the summary buffer to replied articles.
7745 If UNREPLIED (the prefix), limit to unreplied articles."
7746   (interactive "P")
7747   (if unreplied
7748       (gnus-summary-limit
7749        (gnus-set-difference gnus-newsgroup-articles
7750         gnus-newsgroup-replied))
7751     (gnus-summary-limit gnus-newsgroup-replied))
7752   (gnus-summary-position-point))
7753
7754 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7755 (make-obsolete 'gnus-summary-delete-marked-with
7756                'gnus-summary-limit-exclude-marks)
7757
7758 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7759   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7760 If REVERSE, limit the summary buffer to articles that are marked
7761 with MARKS.  MARKS can either be a string of marks or a list of marks.
7762 Returns how many articles were removed."
7763   (interactive "sMarks: ")
7764   (gnus-summary-limit-to-marks marks t))
7765
7766 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7767   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7768 If REVERSE (the prefix), limit the summary buffer to articles that are
7769 not marked with MARKS.  MARKS can either be a string of marks or a
7770 list of marks.
7771 Returns how many articles were removed."
7772   (interactive "sMarks: \nP")
7773   (prog1
7774       (let ((data gnus-newsgroup-data)
7775             (marks (if (listp marks) marks
7776                      (append marks nil))) ; Transform to list.
7777             articles)
7778         (while data
7779           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7780                   (memq (gnus-data-mark (car data)) marks))
7781             (push (gnus-data-number (car data)) articles))
7782           (setq data (cdr data)))
7783         (gnus-summary-limit articles))
7784     (gnus-summary-position-point)))
7785
7786 (defun gnus-summary-limit-to-score (score)
7787   "Limit to articles with score at or above SCORE."
7788   (interactive "NLimit to articles with score of at least: ")
7789   (let ((data gnus-newsgroup-data)
7790         articles)
7791     (while data
7792       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7793                 score)
7794         (push (gnus-data-number (car data)) articles))
7795       (setq data (cdr data)))
7796     (prog1
7797         (gnus-summary-limit articles)
7798       (gnus-summary-position-point))))
7799
7800 (defun gnus-summary-limit-to-unseen ()
7801   "Limit to unseen articles."
7802   (interactive)
7803   (prog1
7804       (gnus-summary-limit gnus-newsgroup-unseen)
7805     (gnus-summary-position-point)))
7806
7807 (defun gnus-summary-limit-include-thread (id)
7808   "Display all the hidden articles that is in the thread with ID in it.
7809 When called interactively, ID is the Message-ID of the current
7810 article."
7811   (interactive (list (mail-header-id (gnus-summary-article-header))))
7812   (let ((articles (gnus-articles-in-thread
7813                    (gnus-id-to-thread (gnus-root-id id)))))
7814     (prog1
7815         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7816       (gnus-summary-limit-include-matching-articles
7817        "subject"
7818        (regexp-quote (gnus-simplify-subject-re
7819                       (mail-header-subject (gnus-id-to-header id)))))
7820       (gnus-summary-position-point))))
7821
7822 (defun gnus-summary-limit-include-matching-articles (header regexp)
7823   "Display all the hidden articles that have HEADERs that match REGEXP."
7824   (interactive (list (read-string "Match on header: ")
7825                      (read-string "Regexp: ")))
7826   (let ((articles (gnus-find-matching-articles header regexp)))
7827     (prog1
7828         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7829       (gnus-summary-position-point))))
7830
7831 (defun gnus-summary-insert-dormant-articles ()
7832   "Insert all the dormant articles for this group into the current buffer."
7833   (interactive)
7834   (let ((gnus-verbose (max 6 gnus-verbose)))
7835     (if (not gnus-newsgroup-dormant)
7836         (gnus-message 3 "No cached articles for this group")
7837       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7838
7839 (defun gnus-summary-limit-include-dormant ()
7840   "Display all the hidden articles that are marked as dormant.
7841 Note that this command only works on a subset of the articles currently
7842 fetched for this group."
7843   (interactive)
7844   (unless gnus-newsgroup-dormant
7845     (error "There are no dormant articles in this group"))
7846   (prog1
7847       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7848     (gnus-summary-position-point)))
7849
7850 (defun gnus-summary-limit-exclude-dormant ()
7851   "Hide all dormant articles."
7852   (interactive)
7853   (prog1
7854       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7855     (gnus-summary-position-point)))
7856
7857 (defun gnus-summary-limit-exclude-childless-dormant ()
7858   "Hide all dormant articles that have no children."
7859   (interactive)
7860   (let ((data (gnus-data-list t))
7861         articles d children)
7862     ;; Find all articles that are either not dormant or have
7863     ;; children.
7864     (while (setq d (pop data))
7865       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7866                 (and (setq children
7867                            (gnus-article-children (gnus-data-number d)))
7868                      (let (found)
7869                        (while children
7870                          (when (memq (car children) articles)
7871                            (setq children nil
7872                                  found t))
7873                          (pop children))
7874                        found)))
7875         (push (gnus-data-number d) articles)))
7876     ;; Do the limiting.
7877     (prog1
7878         (gnus-summary-limit articles)
7879       (gnus-summary-position-point))))
7880
7881 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7882   "Mark all unread excluded articles as read.
7883 If ALL, mark even excluded ticked and dormants as read."
7884   (interactive "P")
7885   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7886   (let ((articles (gnus-sorted-ndifference
7887                    (sort
7888                     (mapcar (lambda (h) (mail-header-number h))
7889                             gnus-newsgroup-headers)
7890                     '<)
7891                    gnus-newsgroup-limit))
7892         article)
7893     (setq gnus-newsgroup-unreads
7894           (gnus-sorted-intersection gnus-newsgroup-unreads
7895                                     gnus-newsgroup-limit))
7896     (if all
7897         (setq gnus-newsgroup-dormant nil
7898               gnus-newsgroup-marked nil
7899               gnus-newsgroup-reads
7900               (nconc
7901                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7902                gnus-newsgroup-reads))
7903       (while (setq article (pop articles))
7904         (unless (or (memq article gnus-newsgroup-dormant)
7905                     (memq article gnus-newsgroup-marked))
7906           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7907
7908 (defun gnus-summary-limit (articles &optional pop)
7909   (if pop
7910       ;; We pop the previous limit off the stack and use that.
7911       (setq articles (car gnus-newsgroup-limits)
7912             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7913     ;; We use the new limit, so we push the old limit on the stack.
7914     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7915   ;; Set the limit.
7916   (setq gnus-newsgroup-limit articles)
7917   (let ((total (length gnus-newsgroup-data))
7918         (data (gnus-data-find-list (gnus-summary-article-number)))
7919         (gnus-summary-mark-below nil)   ; Inhibit this.
7920         found)
7921     ;; This will do all the work of generating the new summary buffer
7922     ;; according to the new limit.
7923     (gnus-summary-prepare)
7924     ;; Hide any threads, possibly.
7925     (gnus-summary-maybe-hide-threads)
7926     ;; Try to return to the article you were at, or one in the
7927     ;; neighborhood.
7928     (when data
7929       ;; We try to find some article after the current one.
7930       (while data
7931         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7932           (setq data nil
7933                 found t))
7934         (setq data (cdr data))))
7935     (unless found
7936       ;; If there is no data, that means that we were after the last
7937       ;; article.  The same goes when we can't find any articles
7938       ;; after the current one.
7939       (goto-char (point-max))
7940       (gnus-summary-find-prev))
7941     (gnus-set-mode-line 'summary)
7942     ;; We return how many articles were removed from the summary
7943     ;; buffer as a result of the new limit.
7944     (- total (length gnus-newsgroup-data))))
7945
7946 (defsubst gnus-invisible-cut-children (threads)
7947   (let ((num 0))
7948     (while threads
7949       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7950         (incf num))
7951       (pop threads))
7952     (< num 2)))
7953
7954 (defsubst gnus-cut-thread (thread)
7955   "Go forwards in the thread until we find an article that we want to display."
7956   (when (or (eq gnus-fetch-old-headers 'some)
7957             (eq gnus-fetch-old-headers 'invisible)
7958             (numberp gnus-fetch-old-headers)
7959             (eq gnus-build-sparse-threads 'some)
7960             (eq gnus-build-sparse-threads 'more))
7961     ;; Deal with old-fetched headers and sparse threads.
7962     (while (and
7963             thread
7964             (or
7965              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7966              (gnus-summary-article-ancient-p
7967               (mail-header-number (car thread))))
7968             (if (or (<= (length (cdr thread)) 1)
7969                     (eq gnus-fetch-old-headers 'invisible))
7970                 (setq gnus-newsgroup-limit
7971                       (delq (mail-header-number (car thread))
7972                             gnus-newsgroup-limit)
7973                       thread (cadr thread))
7974               (when (gnus-invisible-cut-children (cdr thread))
7975                 (let ((th (cdr thread)))
7976                   (while th
7977                     (if (memq (mail-header-number (caar th))
7978                               gnus-newsgroup-limit)
7979                         (setq thread (car th)
7980                               th nil)
7981                       (setq th (cdr th))))))))))
7982   thread)
7983
7984 (defun gnus-cut-threads (threads)
7985   "Cut off all uninteresting articles from the beginning of THREADS."
7986   (when (or (eq gnus-fetch-old-headers 'some)
7987             (eq gnus-fetch-old-headers 'invisible)
7988             (numberp gnus-fetch-old-headers)
7989             (eq gnus-build-sparse-threads 'some)
7990             (eq gnus-build-sparse-threads 'more))
7991     (let ((th threads))
7992       (while th
7993         (setcar th (gnus-cut-thread (car th)))
7994         (setq th (cdr th)))))
7995   ;; Remove nixed out threads.
7996   (delq nil threads))
7997
7998 (defun gnus-summary-initial-limit (&optional show-if-empty)
7999   "Figure out what the initial limit is supposed to be on group entry.
8000 This entails weeding out unwanted dormants, low-scored articles,
8001 fetch-old-headers verbiage, and so on."
8002   ;; Most groups have nothing to remove.
8003   (if (or gnus-inhibit-limiting
8004           (and (null gnus-newsgroup-dormant)
8005                (eq gnus-newsgroup-display 'gnus-not-ignore)
8006                (not (eq gnus-fetch-old-headers 'some))
8007                (not (numberp gnus-fetch-old-headers))
8008                (not (eq gnus-fetch-old-headers 'invisible))
8009                (null gnus-summary-expunge-below)
8010                (not (eq gnus-build-sparse-threads 'some))
8011                (not (eq gnus-build-sparse-threads 'more))
8012                (null gnus-thread-expunge-below)
8013                (not gnus-use-nocem)))
8014       ()                                ; Do nothing.
8015     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8016     (setq gnus-newsgroup-limit nil)
8017     (mapatoms
8018      (lambda (node)
8019        (unless (car (symbol-value node))
8020          ;; These threads have no parents -- they are roots.
8021          (let ((nodes (cdr (symbol-value node)))
8022                thread)
8023            (while nodes
8024              (if (and gnus-thread-expunge-below
8025                       (< (gnus-thread-total-score (car nodes))
8026                          gnus-thread-expunge-below))
8027                  (gnus-expunge-thread (pop nodes))
8028                (setq thread (pop nodes))
8029                (gnus-summary-limit-children thread))))))
8030      gnus-newsgroup-dependencies)
8031     ;; If this limitation resulted in an empty group, we might
8032     ;; pop the previous limit and use it instead.
8033     (when (and (not gnus-newsgroup-limit)
8034                show-if-empty)
8035       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8036     gnus-newsgroup-limit))
8037
8038 (defun gnus-summary-limit-children (thread)
8039   "Return 1 if this subthread is visible and 0 if it is not."
8040   ;; First we get the number of visible children to this thread.  This
8041   ;; is done by recursing down the thread using this function, so this
8042   ;; will really go down to a leaf article first, before slowly
8043   ;; working its way up towards the root.
8044   (when thread
8045     (let* ((max-lisp-eval-depth 5000)
8046            (children
8047            (if (cdr thread)
8048                (apply '+ (mapcar 'gnus-summary-limit-children
8049                                  (cdr thread)))
8050              0))
8051           (number (mail-header-number (car thread)))
8052           score)
8053       (if (and
8054            (not (memq number gnus-newsgroup-marked))
8055            (or
8056             ;; If this article is dormant and has absolutely no visible
8057             ;; children, then this article isn't visible.
8058             (and (memq number gnus-newsgroup-dormant)
8059                  (zerop children))
8060             ;; If this is "fetch-old-headered" and there is no
8061             ;; visible children, then we don't want this article.
8062             (and (or (eq gnus-fetch-old-headers 'some)
8063                      (numberp gnus-fetch-old-headers))
8064                  (gnus-summary-article-ancient-p number)
8065                  (zerop children))
8066             ;; If this is "fetch-old-headered" and `invisible', then
8067             ;; we don't want this article.
8068             (and (eq gnus-fetch-old-headers 'invisible)
8069                  (gnus-summary-article-ancient-p number))
8070             ;; If this is a sparsely inserted article with no children,
8071             ;; we don't want it.
8072             (and (eq gnus-build-sparse-threads 'some)
8073                  (gnus-summary-article-sparse-p number)
8074                  (zerop children))
8075             ;; If we use expunging, and this article is really
8076             ;; low-scored, then we don't want this article.
8077             (when (and gnus-summary-expunge-below
8078                        (< (setq score
8079                                 (or (cdr (assq number gnus-newsgroup-scored))
8080                                     gnus-summary-default-score))
8081                           gnus-summary-expunge-below))
8082               ;; We increase the expunge-tally here, but that has
8083               ;; nothing to do with the limits, really.
8084               (incf gnus-newsgroup-expunged-tally)
8085               ;; We also mark as read here, if that's wanted.
8086               (when (and gnus-summary-mark-below
8087                          (< score gnus-summary-mark-below))
8088                 (setq gnus-newsgroup-unreads
8089                       (delq number gnus-newsgroup-unreads))
8090                 (if gnus-newsgroup-auto-expire
8091                     (push number gnus-newsgroup-expirable)
8092                   (push (cons number gnus-low-score-mark)
8093                         gnus-newsgroup-reads)))
8094               t)
8095             ;; Do the `display' group parameter.
8096             (and gnus-newsgroup-display
8097                  (not (funcall gnus-newsgroup-display)))
8098             ;; Check NoCeM things.
8099             (when (and gnus-use-nocem
8100                        (gnus-nocem-unwanted-article-p
8101                         (mail-header-id (car thread))))
8102               (setq gnus-newsgroup-unreads
8103                     (delq number gnus-newsgroup-unreads))
8104               t)))
8105           ;; Nope, invisible article.
8106           0
8107         ;; Ok, this article is to be visible, so we add it to the limit
8108         ;; and return 1.
8109         (push number gnus-newsgroup-limit)
8110         1))))
8111
8112 (defun gnus-expunge-thread (thread)
8113   "Mark all articles in THREAD as read."
8114   (let* ((number (mail-header-number (car thread))))
8115     (incf gnus-newsgroup-expunged-tally)
8116     ;; We also mark as read here, if that's wanted.
8117     (setq gnus-newsgroup-unreads
8118           (delq number gnus-newsgroup-unreads))
8119     (if gnus-newsgroup-auto-expire
8120         (push number gnus-newsgroup-expirable)
8121       (push (cons number gnus-low-score-mark)
8122             gnus-newsgroup-reads)))
8123   ;; Go recursively through all subthreads.
8124   (mapcar 'gnus-expunge-thread (cdr thread)))
8125
8126 ;; Summary article oriented commands
8127
8128 (defun gnus-summary-refer-parent-article (n)
8129   "Refer parent article N times.
8130 If N is negative, go to ancestor -N instead.
8131 The difference between N and the number of articles fetched is returned."
8132   (interactive "p")
8133   (let ((skip 1)
8134         error header ref)
8135     (when (not (natnump n))
8136       (setq skip (abs n)
8137             n 1))
8138     (while (and (> n 0)
8139                 (not error))
8140       (setq header (gnus-summary-article-header))
8141       (if (and (eq (mail-header-number header)
8142                    (cdr gnus-article-current))
8143                (equal gnus-newsgroup-name
8144                       (car gnus-article-current)))
8145           ;; If we try to find the parent of the currently
8146           ;; displayed article, then we take a look at the actual
8147           ;; References header, since this is slightly more
8148           ;; reliable than the References field we got from the
8149           ;; server.
8150           (save-excursion
8151             (set-buffer gnus-original-article-buffer)
8152             (nnheader-narrow-to-headers)
8153             (unless (setq ref (message-fetch-field "references"))
8154               (when (setq ref (message-fetch-field "in-reply-to"))
8155                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8156             (widen))
8157         (setq ref
8158               ;; It's not the current article, so we take a bet on
8159               ;; the value we got from the server.
8160               (mail-header-references header)))
8161       (if (and ref
8162                (not (equal ref "")))
8163           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8164             (gnus-message 1 "Couldn't find parent"))
8165         (gnus-message 1 "No references in article %d"
8166                       (gnus-summary-article-number))
8167         (setq error t))
8168       (decf n))
8169     (gnus-summary-position-point)
8170     n))
8171
8172 (defun gnus-summary-refer-references ()
8173   "Fetch all articles mentioned in the References header.
8174 Return the number of articles fetched."
8175   (interactive)
8176   (let ((ref (mail-header-references (gnus-summary-article-header)))
8177         (current (gnus-summary-article-number))
8178         (n 0))
8179     (if (or (not ref)
8180             (equal ref ""))
8181         (error "No References in the current article")
8182       ;; For each Message-ID in the References header...
8183       (while (string-match "<[^>]*>" ref)
8184         (incf n)
8185         ;; ... fetch that article.
8186         (gnus-summary-refer-article
8187          (prog1 (match-string 0 ref)
8188            (setq ref (substring ref (match-end 0))))))
8189       (gnus-summary-goto-subject current)
8190       (gnus-summary-position-point)
8191       n)))
8192
8193 (defun gnus-summary-refer-thread (&optional limit)
8194   "Fetch all articles in the current thread.
8195 If LIMIT (the numerical prefix), fetch that many old headers instead
8196 of what's specified by the `gnus-refer-thread-limit' variable."
8197   (interactive "P")
8198   (let ((id (mail-header-id (gnus-summary-article-header)))
8199         (limit (if limit (prefix-numeric-value limit)
8200                  gnus-refer-thread-limit)))
8201     (unless (eq gnus-fetch-old-headers 'invisible)
8202       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8203       ;; Retrieve the headers and read them in.
8204       (if (eq (if (numberp limit)
8205                   (gnus-retrieve-headers
8206                    (list (min
8207                           (+ (mail-header-number
8208                               (gnus-summary-article-header))
8209                              limit)
8210                           gnus-newsgroup-end))
8211                    gnus-newsgroup-name (* limit 2))
8212                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8213                 ;; headers.
8214                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8215                                        gnus-newsgroup-name limit))
8216               'nov)
8217           (gnus-build-all-threads)
8218         (error "Can't fetch thread from back ends that don't support NOV"))
8219       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8220     (gnus-summary-limit-include-thread id)))
8221
8222 (defun gnus-summary-refer-article (message-id)
8223   "Fetch an article specified by MESSAGE-ID."
8224   (interactive "sMessage-ID: ")
8225   (when (and (stringp message-id)
8226              (not (zerop (length message-id))))
8227     (setq message-id (gnus-replace-in-string message-id " " ""))
8228     ;; Construct the correct Message-ID if necessary.
8229     ;; Suggested by tale@pawl.rpi.edu.
8230     (unless (string-match "^<" message-id)
8231       (setq message-id (concat "<" message-id)))
8232     (unless (string-match ">$" message-id)
8233       (setq message-id (concat message-id ">")))
8234     ;; People often post MIDs from URLs, so unhex it:
8235     (unless (string-match "@" message-id)
8236       (setq message-id (gnus-url-unhex-string message-id)))
8237     (let* ((header (gnus-id-to-header message-id))
8238            (sparse (and header
8239                         (gnus-summary-article-sparse-p
8240                          (mail-header-number header))
8241                         (memq (mail-header-number header)
8242                               gnus-newsgroup-limit)))
8243            number)
8244       (cond
8245        ;; If the article is present in the buffer we just go to it.
8246        ((and header
8247              (or (not (gnus-summary-article-sparse-p
8248                        (mail-header-number header)))
8249                  sparse))
8250         (prog1
8251             (gnus-summary-goto-article
8252              (mail-header-number header) nil t)
8253           (when sparse
8254             (gnus-summary-update-article (mail-header-number header)))))
8255        (t
8256         ;; We fetch the article.
8257         (catch 'found
8258           (dolist (gnus-override-method (gnus-refer-article-methods))
8259             (when (and (gnus-check-server gnus-override-method)
8260                        ;; Fetch the header,
8261                        (setq number (gnus-summary-insert-subject message-id)))
8262               ;; and display the article.
8263               (gnus-summary-select-article nil nil nil number)
8264               (throw 'found t)))
8265           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8266
8267 (defun gnus-refer-article-methods ()
8268   "Return a list of referable methods."
8269   (cond
8270    ;; No method, so we default to current and native.
8271    ((null gnus-refer-article-method)
8272     (list gnus-current-select-method gnus-select-method))
8273    ;; Current.
8274    ((eq 'current gnus-refer-article-method)
8275     (list gnus-current-select-method))
8276    ;; List of select methods.
8277    ((not (and (symbolp (car gnus-refer-article-method))
8278               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8279     (let (out)
8280       (dolist (method gnus-refer-article-method)
8281         (push (if (eq 'current method)
8282                   gnus-current-select-method
8283                 method)
8284               out))
8285       (nreverse out)))
8286    ;; One single select method.
8287    (t
8288     (list gnus-refer-article-method))))
8289
8290 (defun gnus-summary-edit-parameters ()
8291   "Edit the group parameters of the current group."
8292   (interactive)
8293   (gnus-group-edit-group gnus-newsgroup-name 'params))
8294
8295 (defun gnus-summary-customize-parameters ()
8296   "Customize the group parameters of the current group."
8297   (interactive)
8298   (gnus-group-customize gnus-newsgroup-name))
8299
8300 (defun gnus-summary-enter-digest-group (&optional force)
8301   "Enter an nndoc group based on the current article.
8302 If FORCE, force a digest interpretation.  If not, try
8303 to guess what the document format is."
8304   (interactive "P")
8305   (let ((conf gnus-current-window-configuration))
8306     (save-window-excursion
8307       (save-excursion
8308         (let (gnus-article-prepare-hook
8309               gnus-display-mime-function
8310               gnus-break-pages)
8311           (gnus-summary-select-article))))
8312     (setq gnus-current-window-configuration conf)
8313     (let* ((name (format "%s-%d"
8314                          (gnus-group-prefixed-name
8315                           gnus-newsgroup-name (list 'nndoc ""))
8316                          (with-current-buffer gnus-summary-buffer
8317                            gnus-current-article)))
8318            (ogroup gnus-newsgroup-name)
8319            (params (append (gnus-info-params (gnus-get-info ogroup))
8320                            (list (cons 'to-group ogroup))
8321                            (list (cons 'parent-group ogroup))
8322                            (list (cons 'save-article-group ogroup))))
8323            (case-fold-search t)
8324            (buf (current-buffer))
8325            dig to-address)
8326       (save-excursion
8327         (set-buffer gnus-original-article-buffer)
8328         ;; Have the digest group inherit the main mail address of
8329         ;; the parent article.
8330         (when (setq to-address (or (gnus-fetch-field "reply-to")
8331                                    (gnus-fetch-field "from")))
8332           (setq params (append
8333                         (list (cons 'to-address
8334                                     (funcall gnus-decode-encoded-word-function
8335                                              to-address))))))
8336         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8337         (insert-buffer-substring gnus-original-article-buffer)
8338         ;; Remove lines that may lead nndoc to misinterpret the
8339         ;; document type.
8340         (narrow-to-region
8341          (goto-char (point-min))
8342          (or (search-forward "\n\n" nil t) (point)))
8343         (goto-char (point-min))
8344         (delete-matching-lines "^Path:\\|^From ")
8345         (widen))
8346       (unwind-protect
8347           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8348                     (gnus-newsgroup-ephemeral-ignored-charsets
8349                      gnus-newsgroup-ignored-charsets))
8350                 (gnus-group-read-ephemeral-group
8351                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8352                               (nndoc-article-type
8353                                ,(if force 'mbox 'guess)))
8354                  t nil nil nil
8355                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8356                                                         "ADAPT")))))
8357               ;; Make all postings to this group go to the parent group.
8358               (nconc (gnus-info-params (gnus-get-info name))
8359                      params)
8360             ;; Couldn't select this doc group.
8361             (switch-to-buffer buf)
8362             (gnus-set-global-variables)
8363             (gnus-configure-windows 'summary)
8364             (gnus-message 3 "Article couldn't be entered?"))
8365         (kill-buffer dig)))))
8366
8367 (defun gnus-summary-read-document (n)
8368   "Open a new group based on the current article(s).
8369 This will allow you to read digests and other similar
8370 documents as newsgroups.
8371 Obeys the standard process/prefix convention."
8372   (interactive "P")
8373   (let* ((articles (gnus-summary-work-articles n))
8374          (ogroup gnus-newsgroup-name)
8375          (params (append (gnus-info-params (gnus-get-info ogroup))
8376                          (list (cons 'to-group ogroup))))
8377          article group egroup groups vgroup)
8378     (while (setq article (pop articles))
8379       (setq group (format "%s-%d" gnus-newsgroup-name article))
8380       (gnus-summary-remove-process-mark article)
8381       (when (gnus-summary-display-article article)
8382         (save-excursion
8383           (with-temp-buffer
8384             (insert-buffer-substring gnus-original-article-buffer)
8385             ;; Remove some headers that may lead nndoc to make
8386             ;; the wrong guess.
8387             (message-narrow-to-head)
8388             (goto-char (point-min))
8389             (delete-matching-lines "^Path:\\|^From ")
8390             (widen)
8391             (if (setq egroup
8392                       (gnus-group-read-ephemeral-group
8393                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8394                                      (nndoc-article-type guess))
8395                        t nil t))
8396                 (progn
8397             ;; Make all postings to this group go to the parent group.
8398                   (nconc (gnus-info-params (gnus-get-info egroup))
8399                          params)
8400                   (push egroup groups))
8401               ;; Couldn't select this doc group.
8402               (gnus-error 3 "Article couldn't be entered"))))))
8403     ;; Now we have selected all the documents.
8404     (cond
8405      ((not groups)
8406       (error "None of the articles could be interpreted as documents"))
8407      ((gnus-group-read-ephemeral-group
8408        (setq vgroup (format
8409                      "nnvirtual:%s-%s" gnus-newsgroup-name
8410                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8411        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8412        t
8413        (cons (current-buffer) 'summary)))
8414      (t
8415       (error "Couldn't select virtual nndoc group")))))
8416
8417 (defun gnus-summary-isearch-article (&optional regexp-p)
8418   "Do incremental search forward on the current article.
8419 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8420   (interactive "P")
8421   (gnus-summary-select-article)
8422   (gnus-configure-windows 'article)
8423   (gnus-eval-in-buffer-window gnus-article-buffer
8424     (save-restriction
8425       (widen)
8426       (isearch-forward regexp-p))))
8427
8428 (defun gnus-summary-search-article-forward (regexp &optional backward)
8429   "Search for an article containing REGEXP forward.
8430 If BACKWARD, search backward instead."
8431   (interactive
8432    (list (read-string
8433           (format "Search article %s (regexp%s): "
8434                   (if current-prefix-arg "backward" "forward")
8435                   (if gnus-last-search-regexp
8436                       (concat ", default " gnus-last-search-regexp)
8437                     "")))
8438          current-prefix-arg))
8439   (if (string-equal regexp "")
8440       (setq regexp (or gnus-last-search-regexp ""))
8441     (setq gnus-last-search-regexp regexp)
8442     (setq gnus-article-before-search gnus-current-article))
8443   ;; Intentionally set gnus-last-article.
8444   (setq gnus-last-article gnus-article-before-search)
8445   (let ((gnus-last-article gnus-last-article))
8446     (if (gnus-summary-search-article regexp backward)
8447         (gnus-summary-show-thread)
8448       (signal 'search-failed (list regexp)))))
8449
8450 (defun gnus-summary-search-article-backward (regexp)
8451   "Search for an article containing REGEXP backward."
8452   (interactive
8453    (list (read-string
8454           (format "Search article backward (regexp%s): "
8455                   (if gnus-last-search-regexp
8456                       (concat ", default " gnus-last-search-regexp)
8457                     "")))))
8458   (gnus-summary-search-article-forward regexp 'backward))
8459
8460 (defun gnus-summary-search-article (regexp &optional backward)
8461   "Search for an article containing REGEXP.
8462 Optional argument BACKWARD means do search for backward.
8463 `gnus-select-article-hook' is not called during the search."
8464   ;; We have to require this here to make sure that the following
8465   ;; dynamic binding isn't shadowed by autoloading.
8466   (require 'gnus-async)
8467   (require 'gnus-art)
8468   (let ((gnus-select-article-hook nil)  ;Disable hook.
8469         (gnus-article-prepare-hook nil)
8470         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8471         (gnus-use-article-prefetch nil)
8472         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8473         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8474         (gnus-visual nil)
8475         (gnus-keep-backlog nil)
8476         (gnus-break-pages nil)
8477         (gnus-summary-display-arrow nil)
8478         (gnus-updated-mode-lines nil)
8479         (gnus-auto-center-summary nil)
8480         (sum (current-buffer))
8481         (gnus-display-mime-function nil)
8482         (found nil)
8483         point)
8484     (gnus-save-hidden-threads
8485       (gnus-summary-select-article)
8486       (set-buffer gnus-article-buffer)
8487       (goto-char (window-point (get-buffer-window (current-buffer))))
8488       (when backward
8489         (forward-line -1))
8490       (while (not found)
8491         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8492         (if (if backward
8493                 (re-search-backward regexp nil t)
8494               (re-search-forward regexp nil t))
8495             ;; We found the regexp.
8496             (progn
8497               (setq found 'found)
8498               (beginning-of-line)
8499               (set-window-start
8500                (get-buffer-window (current-buffer))
8501                (point))
8502               (forward-line 1)
8503               (set-window-point
8504                (get-buffer-window (current-buffer))
8505                (point))
8506               (set-buffer sum)
8507               (setq point (point)))
8508           ;; We didn't find it, so we go to the next article.
8509           (set-buffer sum)
8510           (setq found 'not)
8511           (while (eq found 'not)
8512             (if (not (if backward (gnus-summary-find-prev)
8513                        (gnus-summary-find-next)))
8514                 ;; No more articles.
8515                 (setq found t)
8516               ;; Select the next article and adjust point.
8517               (unless (gnus-summary-article-sparse-p
8518                        (gnus-summary-article-number))
8519                 (setq found nil)
8520                 (gnus-summary-select-article)
8521                 (set-buffer gnus-article-buffer)
8522                 (widen)
8523                 (goto-char (if backward (point-max) (point-min))))))))
8524       (gnus-message 7 ""))
8525     ;; Return whether we found the regexp.
8526     (when (eq found 'found)
8527       (goto-char point)
8528       (gnus-summary-show-thread)
8529       (gnus-summary-goto-subject gnus-current-article)
8530       (gnus-summary-position-point)
8531       t)))
8532
8533 (defun gnus-find-matching-articles (header regexp)
8534   "Return a list of all articles that match REGEXP on HEADER.
8535 This search includes all articles in the current group that Gnus has
8536 fetched headers for, whether they are displayed or not."
8537   (let ((articles nil)
8538         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8539         (case-fold-search t))
8540     (dolist (header gnus-newsgroup-headers)
8541       (when (string-match regexp (funcall func header))
8542         (push (mail-header-number header) articles)))
8543     (nreverse articles)))
8544
8545 (defun gnus-summary-find-matching (header regexp &optional backward unread
8546                                           not-case-fold not-matching)
8547   "Return a list of all articles that match REGEXP on HEADER.
8548 The search stars on the current article and goes forwards unless
8549 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8550 If UNREAD is non-nil, only unread articles will
8551 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8552 in the comparisons. If NOT-MATCHING, return a list of all articles that
8553 not match REGEXP on HEADER."
8554   (let ((case-fold-search (not not-case-fold))
8555         articles d func)
8556     (if (consp header)
8557         (if (eq (car header) 'extra)
8558             (setq func
8559                   `(lambda (h)
8560                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8561                          "")))
8562           (error "%s is an invalid header" header))
8563       (unless (fboundp (intern (concat "mail-header-" header)))
8564         (error "%s is not a valid header" header))
8565       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8566     (dolist (d (if (eq backward 'all)
8567                    gnus-newsgroup-data
8568                  (gnus-data-find-list
8569                   (gnus-summary-article-number)
8570                   (gnus-data-list backward))))
8571       (when (and (or (not unread)       ; We want all articles...
8572                      (gnus-data-unread-p d)) ; Or just unreads.
8573                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8574                  (if not-matching
8575                      (not (string-match
8576                            regexp
8577                            (funcall func (gnus-data-header d))))
8578                    (string-match regexp
8579                                  (funcall func (gnus-data-header d)))))
8580         (push (gnus-data-number d) articles))) ; Success!
8581     (nreverse articles)))
8582
8583 (defun gnus-summary-execute-command (header regexp command &optional backward)
8584   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8585 If HEADER is an empty string (or nil), the match is done on the entire
8586 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8587   (interactive
8588    (list (let ((completion-ignore-case t))
8589            (completing-read
8590             "Header name: "
8591             (mapcar (lambda (header) (list (format "%s" header)))
8592                     (append
8593                      '("Number" "Subject" "From" "Lines" "Date"
8594                        "Message-ID" "Xref" "References" "Body")
8595                      gnus-extra-headers))
8596             nil 'require-match))
8597          (read-string "Regexp: ")
8598          (read-key-sequence "Command: ")
8599          current-prefix-arg))
8600   (when (equal header "Body")
8601     (setq header ""))
8602   ;; Hidden thread subtrees must be searched as well.
8603   (gnus-summary-show-all-threads)
8604   ;; We don't want to change current point nor window configuration.
8605   (save-excursion
8606     (save-window-excursion
8607       (let (gnus-visual
8608             gnus-treat-strip-trailing-blank-lines
8609             gnus-treat-strip-leading-blank-lines
8610             gnus-treat-strip-multiple-blank-lines
8611             gnus-treat-hide-boring-headers
8612             gnus-treat-fold-newsgroups
8613             gnus-article-prepare-hook)
8614         (gnus-message 6 "Executing %s..." (key-description command))
8615         ;; We'd like to execute COMMAND interactively so as to give arguments.
8616         (gnus-execute header regexp
8617                       `(call-interactively ',(key-binding command))
8618                       backward)
8619         (gnus-message 6 "Executing %s...done" (key-description command))))))
8620
8621 (defun gnus-summary-beginning-of-article ()
8622   "Scroll the article back to the beginning."
8623   (interactive)
8624   (gnus-summary-select-article)
8625   (gnus-configure-windows 'article)
8626   (gnus-eval-in-buffer-window gnus-article-buffer
8627     (widen)
8628     (goto-char (point-min))
8629     (when gnus-break-pages
8630       (gnus-narrow-to-page))))
8631
8632 (defun gnus-summary-end-of-article ()
8633   "Scroll to the end of the article."
8634   (interactive)
8635   (gnus-summary-select-article)
8636   (gnus-configure-windows 'article)
8637   (gnus-eval-in-buffer-window gnus-article-buffer
8638     (widen)
8639     (goto-char (point-max))
8640     (recenter -3)
8641     (when gnus-break-pages
8642       (when (re-search-backward page-delimiter nil t)
8643         (narrow-to-region (match-end 0) (point-max)))
8644       (gnus-narrow-to-page))))
8645
8646 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8647   "Truncate to LEN and quote all \"(\"'s in STRING."
8648   (gnus-replace-in-string (if (and len (> (length string) len))
8649                               (substring string 0 len)
8650                             string)
8651                           "[()]" "\\\\\\&"))
8652
8653 (defun gnus-summary-print-article (&optional filename n)
8654   "Generate and print a PostScript image of the process-marked (mail) articles.
8655
8656 If used interactively, print the current article if none are
8657 process-marked.  With prefix arg, prompt the user for the name of the
8658 file to save in.
8659
8660 When used from Lisp, accept two optional args FILENAME and N.  N means
8661 to print the next N articles.  If N is negative, print the N previous
8662 articles.  If N is nil and articles have been marked with the process
8663 mark, print these instead.
8664
8665 If the optional first argument FILENAME is nil, send the image to the
8666 printer.  If FILENAME is a string, save the PostScript image in a file with
8667 that name.  If FILENAME is a number, prompt the user for the name of the file
8668 to save in."
8669   (interactive (list (ps-print-preprint current-prefix-arg)))
8670   (dolist (article (gnus-summary-work-articles n))
8671     (gnus-summary-select-article nil nil 'pseudo article)
8672     (gnus-eval-in-buffer-window gnus-article-buffer
8673       (gnus-print-buffer))
8674     (gnus-summary-remove-process-mark article))
8675   (ps-despool filename))
8676
8677 (defun gnus-print-buffer ()
8678   (let ((buffer (generate-new-buffer " *print*")))
8679     (unwind-protect
8680         (progn
8681           (copy-to-buffer buffer (point-min) (point-max))
8682           (set-buffer buffer)
8683           (gnus-remove-text-with-property 'gnus-decoration)
8684           (when (gnus-visual-p 'article-highlight 'highlight)
8685             ;; Copy-to-buffer doesn't copy overlay.  So redo
8686             ;; highlight.
8687             (let ((gnus-article-buffer buffer))
8688               (gnus-article-highlight-citation t)
8689               (gnus-article-highlight-signature)
8690               (gnus-article-emphasize)
8691               (gnus-article-delete-invisible-text)))
8692           (let ((ps-left-header
8693                  (list
8694                   (concat "("
8695                           (gnus-summary-print-truncate-and-quote
8696                            (mail-header-subject gnus-current-headers)
8697                            66) ")")
8698                   (concat "("
8699                           (gnus-summary-print-truncate-and-quote
8700                            (mail-header-from gnus-current-headers)
8701                            45) ")")))
8702                 (ps-right-header
8703                  (list
8704                   "/pagenumberstring load"
8705                   (concat "("
8706                           (mail-header-date gnus-current-headers) ")"))))
8707             (gnus-run-hooks 'gnus-ps-print-hook)
8708             (save-excursion
8709               (if window-system
8710                   (ps-spool-buffer-with-faces)
8711                 (ps-spool-buffer)))))
8712       (kill-buffer buffer))))
8713
8714 (defun gnus-summary-show-article (&optional arg)
8715   "Force redisplaying of the current article.
8716 If ARG (the prefix) is a number, show the article with the charset
8717 defined in `gnus-summary-show-article-charset-alist', or the charset
8718 input.
8719 If ARG (the prefix) is non-nil and not a number, show the raw article
8720 without any article massaging functions being run.  Normally, the key
8721 strokes are `C-u g'."
8722   (interactive "P")
8723   (cond
8724    ((numberp arg)
8725     (gnus-summary-show-article t)
8726     (let ((gnus-newsgroup-charset
8727            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8728                (mm-read-coding-system
8729                 "View as charset: " ;; actually it is coding system.
8730                 (save-excursion
8731                   (set-buffer gnus-article-buffer)
8732                   (mm-detect-coding-region (point) (point-max))))))
8733           (gnus-newsgroup-ignored-charsets 'gnus-all))
8734       (gnus-summary-select-article nil 'force)
8735       (let ((deps gnus-newsgroup-dependencies)
8736             head header lines)
8737         (save-excursion
8738           (set-buffer gnus-original-article-buffer)
8739           (save-restriction
8740             (message-narrow-to-head)
8741             (setq head (buffer-string))
8742             (goto-char (point-min))
8743             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8744               (goto-char (point-max))
8745               (widen)
8746               (setq lines (1- (count-lines (point) (point-max))))))
8747           (with-temp-buffer
8748             (insert (format "211 %d Article retrieved.\n"
8749                             (cdr gnus-article-current)))
8750             (insert head)
8751             (if lines (insert (format "Lines: %d\n" lines)))
8752             (insert ".\n")
8753             (let ((nntp-server-buffer (current-buffer)))
8754               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8755         (gnus-data-set-header
8756          (gnus-data-find (cdr gnus-article-current))
8757          header)
8758         (gnus-summary-update-article-line
8759          (cdr gnus-article-current) header)
8760         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8761           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8762    ((not arg)
8763     ;; Select the article the normal way.
8764     (gnus-summary-select-article nil 'force))
8765    (t
8766     ;; We have to require this here to make sure that the following
8767     ;; dynamic binding isn't shadowed by autoloading.
8768     (require 'gnus-async)
8769     (require 'gnus-art)
8770     ;; Bind the article treatment functions to nil.
8771     (let ((gnus-have-all-headers t)
8772           gnus-article-prepare-hook
8773           gnus-article-decode-hook
8774           gnus-display-mime-function
8775           gnus-break-pages)
8776       ;; Destroy any MIME parts.
8777       (when (gnus-buffer-live-p gnus-article-buffer)
8778         (save-excursion
8779           (set-buffer gnus-article-buffer)
8780           (mm-destroy-parts gnus-article-mime-handles)
8781           ;; Set it to nil for safety reason.
8782           (setq gnus-article-mime-handle-alist nil)
8783           (setq gnus-article-mime-handles nil)))
8784       (gnus-summary-select-article nil 'force))))
8785   (gnus-summary-goto-subject gnus-current-article)
8786   (gnus-summary-position-point))
8787
8788 (defun gnus-summary-show-raw-article ()
8789   "Show the raw article without any article massaging functions being run."
8790   (interactive)
8791   (gnus-summary-show-article t))
8792
8793 (defun gnus-summary-verbose-headers (&optional arg)
8794   "Toggle permanent full header display.
8795 If ARG is a positive number, turn header display on.
8796 If ARG is a negative number, turn header display off."
8797   (interactive "P")
8798   (setq gnus-show-all-headers
8799         (cond ((or (not (numberp arg))
8800                    (zerop arg))
8801                (not gnus-show-all-headers))
8802               ((natnump arg)
8803                t)))
8804   (gnus-summary-show-article))
8805
8806 (defun gnus-summary-toggle-header (&optional arg)
8807   "Show the headers if they are hidden, or hide them if they are shown.
8808 If ARG is a positive number, show the entire header.
8809 If ARG is a negative number, hide the unwanted header lines."
8810   (interactive "P")
8811   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8812                      (get-buffer-window gnus-article-buffer t))))
8813     (with-current-buffer gnus-article-buffer
8814       (widen)
8815       (article-narrow-to-head)
8816       (let* ((buffer-read-only nil)
8817              (inhibit-point-motion-hooks t)
8818              (hidden (if (numberp arg)
8819                          (>= arg 0)
8820                        (gnus-article-hidden-text-p 'headers)))
8821              s e)
8822         (delete-region (point-min) (point-max))
8823         (with-current-buffer gnus-original-article-buffer
8824           (goto-char (setq s (point-min)))
8825           (setq e (if (search-forward "\n\n" nil t)
8826                       (1- (point))
8827                     (point-max))))
8828         (insert-buffer-substring gnus-original-article-buffer s e)
8829         (run-hooks 'gnus-article-decode-hook)
8830         (if hidden
8831             (let ((gnus-treat-hide-headers nil)
8832                   (gnus-treat-hide-boring-headers nil))
8833               (gnus-delete-wash-type 'headers)
8834               (gnus-treat-article 'head))
8835           (gnus-treat-article 'head))
8836         (widen)
8837         (if window
8838             (set-window-start window (goto-char (point-min))))
8839         (if gnus-break-pages
8840             (gnus-narrow-to-page)
8841           (when (gnus-visual-p 'page-marker)
8842             (let ((buffer-read-only nil))
8843               (gnus-remove-text-with-property 'gnus-prev)
8844               (gnus-remove-text-with-property 'gnus-next))))
8845         (gnus-set-mode-line 'article)))))
8846
8847 (defun gnus-summary-show-all-headers ()
8848   "Make all header lines visible."
8849   (interactive)
8850   (gnus-summary-toggle-header 1))
8851
8852 (defun gnus-summary-caesar-message (&optional arg)
8853   "Caesar rotate the current article by 13.
8854 The numerical prefix specifies how many places to rotate each letter
8855 forward."
8856   (interactive "P")
8857   (gnus-summary-select-article)
8858   (let ((mail-header-separator ""))
8859     (gnus-eval-in-buffer-window gnus-article-buffer
8860       (save-restriction
8861         (widen)
8862         (let ((start (window-start))
8863               buffer-read-only)
8864           (message-caesar-buffer-body arg)
8865           (set-window-start (get-buffer-window (current-buffer)) start))))))
8866
8867 (autoload 'unmorse-region "morse"
8868   "Convert morse coded text in region to ordinary ASCII text."
8869   t)
8870
8871 (defun gnus-summary-morse-message (&optional arg)
8872   "Morse decode the current article."
8873   (interactive "P")
8874   (gnus-summary-select-article)
8875   (let ((mail-header-separator ""))
8876     (gnus-eval-in-buffer-window gnus-article-buffer
8877       (save-excursion
8878         (save-restriction
8879           (widen)
8880           (let ((pos (window-start))
8881                 buffer-read-only)
8882             (goto-char (point-min))
8883             (when (message-goto-body)
8884               (gnus-narrow-to-body))
8885             (goto-char (point-min))
8886             (while (search-forward "·" (point-max) t)
8887               (replace-match "."))
8888             (unmorse-region (point-min) (point-max))
8889             (widen)
8890             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8891
8892 (defun gnus-summary-stop-page-breaking ()
8893   "Stop page breaking in the current article."
8894   (interactive)
8895   (gnus-summary-select-article)
8896   (gnus-eval-in-buffer-window gnus-article-buffer
8897     (widen)
8898     (when (gnus-visual-p 'page-marker)
8899       (let ((buffer-read-only nil))
8900         (gnus-remove-text-with-property 'gnus-prev)
8901         (gnus-remove-text-with-property 'gnus-next))
8902       (setq gnus-page-broken nil))))
8903
8904 (defun gnus-summary-move-article (&optional n to-newsgroup
8905                                             select-method action)
8906   "Move the current article to a different newsgroup.
8907 If N is a positive number, move the N next articles.
8908 If N is a negative number, move the N previous articles.
8909 If N is nil and any articles have been marked with the process mark,
8910 move those articles instead.
8911 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8912 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8913 re-spool using this method.
8914
8915 When called interactively with TO-NEWSGROUP being nil, the value of
8916 the variable `gnus-move-split-methods' is used for finding a default
8917 for the target newsgroup.
8918
8919 For this function to work, both the current newsgroup and the
8920 newsgroup that you want to move to have to support the `request-move'
8921 and `request-accept' functions.
8922
8923 ACTION can be either `move' (the default), `crosspost' or `copy'."
8924   (interactive "P")
8925   (unless action
8926     (setq action 'move))
8927   ;; Check whether the source group supports the required functions.
8928   (cond ((and (eq action 'move)
8929               (not (gnus-check-backend-function
8930                     'request-move-article gnus-newsgroup-name)))
8931          (error "The current group does not support article moving"))
8932         ((and (eq action 'crosspost)
8933               (not (gnus-check-backend-function
8934                     'request-replace-article gnus-newsgroup-name)))
8935          (error "The current group does not support article editing")))
8936   (let ((articles (gnus-summary-work-articles n))
8937         (prefix (if (gnus-check-backend-function
8938                      'request-move-article gnus-newsgroup-name)
8939                     (funcall gnus-move-group-prefix-function
8940                              gnus-newsgroup-name)
8941                   ""))
8942         (names '((move "Move" "Moving")
8943                  (copy "Copy" "Copying")
8944                  (crosspost "Crosspost" "Crossposting")))
8945         (copy-buf (save-excursion
8946                     (nnheader-set-temp-buffer " *copy article*")))
8947         art-group to-method new-xref article to-groups)
8948     (unless (assq action names)
8949       (error "Unknown action %s" action))
8950     ;; Read the newsgroup name.
8951     (when (and (not to-newsgroup)
8952                (not select-method))
8953       (if (and gnus-move-split-methods
8954                (not
8955                 (and (memq gnus-current-article articles)
8956                      (gnus-buffer-live-p gnus-original-article-buffer))))
8957           ;; When `gnus-move-split-methods' is non-nil, we have to
8958           ;; select an article to give `gnus-read-move-group-name' an
8959           ;; opportunity to suggest an appropriate default.  However,
8960           ;; we needn't render or mark the article.
8961           (let ((gnus-display-mime-function nil)
8962                 (gnus-article-prepare-hook nil)
8963                 (gnus-mark-article-hook nil))
8964             (gnus-summary-select-article nil nil nil (car articles))))
8965       (setq to-newsgroup
8966             (gnus-read-move-group-name
8967              (cadr (assq action names))
8968              (symbol-value (intern (format "gnus-current-%s-group" action)))
8969              articles prefix))
8970       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8971     (setq to-method (or select-method
8972                         (gnus-server-to-method
8973                          (gnus-group-method to-newsgroup))))
8974     ;; Check the method we are to move this article to...
8975     (unless (gnus-check-backend-function
8976              'request-accept-article (car to-method))
8977       (error "%s does not support article copying" (car to-method)))
8978     (unless (gnus-check-server to-method)
8979       (error "Can't open server %s" (car to-method)))
8980     (gnus-message 6 "%s to %s: %s..."
8981                   (caddr (assq action names))
8982                   (or (car select-method) to-newsgroup) articles)
8983     (while articles
8984       (setq article (pop articles))
8985       (setq
8986        art-group
8987        (cond
8988         ;; Move the article.
8989         ((eq action 'move)
8990          ;; Remove this article from future suppression.
8991          (gnus-dup-unsuppress-article article)
8992          (gnus-request-move-article
8993           article                       ; Article to move
8994           gnus-newsgroup-name           ; From newsgroup
8995           (nth 1 (gnus-find-method-for-group
8996                   gnus-newsgroup-name)) ; Server
8997           (list 'gnus-request-accept-article
8998                 to-newsgroup (list 'quote select-method)
8999                 (not articles) t)       ; Accept form
9000           (not articles)))              ; Only save nov last time
9001         ;; Copy the article.
9002         ((eq action 'copy)
9003          (save-excursion
9004            (set-buffer copy-buf)
9005            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9006              (gnus-request-accept-article
9007               to-newsgroup select-method (not articles) t))))
9008         ;; Crosspost the article.
9009         ((eq action 'crosspost)
9010          (let ((xref (message-tokenize-header
9011                       (mail-header-xref (gnus-summary-article-header article))
9012                       " ")))
9013            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9014                                   ":" (number-to-string article)))
9015            (unless xref
9016              (setq xref (list (system-name))))
9017            (setq new-xref
9018                  (concat
9019                   (mapconcat 'identity
9020                              (delete "Xref:" (delete new-xref xref))
9021                              " ")
9022                   " " new-xref))
9023            (save-excursion
9024              (set-buffer copy-buf)
9025              ;; First put the article in the destination group.
9026              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9027              (when (consp (setq art-group
9028                                 (gnus-request-accept-article
9029                                  to-newsgroup select-method (not articles))))
9030                (setq new-xref (concat new-xref " " (car art-group)
9031                                       ":"
9032                                       (number-to-string (cdr art-group))))
9033                ;; Now we have the new Xrefs header, so we insert
9034                ;; it and replace the new article.
9035                (nnheader-replace-header "Xref" new-xref)
9036                (gnus-request-replace-article
9037                 (cdr art-group) to-newsgroup (current-buffer))
9038                art-group))))))
9039       (cond
9040        ((not art-group)
9041         (gnus-message 1 "Couldn't %s article %s: %s"
9042                       (cadr (assq action names)) article
9043                       (nnheader-get-report (car to-method))))
9044        ((eq art-group 'junk)
9045         (when (eq action 'move)
9046           (gnus-summary-mark-article article gnus-canceled-mark)
9047           (gnus-message 4 "Deleted article %s" article)
9048           ;; run the delete hook
9049           (run-hook-with-args 'gnus-summary-article-delete-hook
9050                               action
9051                               (gnus-data-header
9052                                (assoc article (gnus-data-list nil)))
9053                               gnus-newsgroup-name nil
9054                               select-method)))
9055        (t
9056         (let* ((pto-group (gnus-group-prefixed-name
9057                            (car art-group) to-method))
9058                (info (gnus-get-info pto-group))
9059                (to-group (gnus-info-group info))
9060                to-marks)
9061           ;; Update the group that has been moved to.
9062           (when (and info
9063                      (memq action '(move copy)))
9064             (unless (member to-group to-groups)
9065               (push to-group to-groups))
9066
9067             (unless (memq article gnus-newsgroup-unreads)
9068               (push 'read to-marks)
9069               (gnus-info-set-read
9070                info (gnus-add-to-range (gnus-info-read info)
9071                                        (list (cdr art-group)))))
9072
9073             ;; See whether the article is to be put in the cache.
9074             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9075                              gnus-article-mark-lists
9076                            (delete '(expirable . expire)
9077                                    (copy-sequence gnus-article-mark-lists))))
9078                   (to-article (cdr art-group)))
9079
9080               ;; Enter the article into the cache in the new group,
9081               ;; if that is required.
9082               (when gnus-use-cache
9083                 (gnus-cache-possibly-enter-article
9084                  to-group to-article
9085                  (memq article gnus-newsgroup-marked)
9086                  (memq article gnus-newsgroup-dormant)
9087                  (memq article gnus-newsgroup-unreads)))
9088
9089               (when gnus-preserve-marks
9090                 ;; Copy any marks over to the new group.
9091                 (when (and (equal to-group gnus-newsgroup-name)
9092                            (not (memq article gnus-newsgroup-unreads)))
9093                   ;; Mark this article as read in this group.
9094                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9095                   (setcdr (gnus-active to-group) to-article)
9096                   (setcdr gnus-newsgroup-active to-article))
9097
9098                 (while marks
9099                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9100                     (when (memq article (symbol-value
9101                                          (intern (format "gnus-newsgroup-%s"
9102                                                          (caar marks)))))
9103                       (push (cdar marks) to-marks)
9104                       ;; If the other group is the same as this group,
9105                       ;; then we have to add the mark to the list.
9106                       (when (equal to-group gnus-newsgroup-name)
9107                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9108                              (cons to-article
9109                                    (symbol-value
9110                                     (intern (format "gnus-newsgroup-%s"
9111                                                     (caar marks)))))))
9112                       ;; Copy the marks to other group.
9113                       (gnus-add-marked-articles
9114                        to-group (cdar marks) (list to-article) info)))
9115                   (setq marks (cdr marks)))
9116
9117                 (gnus-request-set-mark
9118                  to-group (list (list (list to-article) 'add to-marks))))
9119
9120               (gnus-dribble-enter
9121                (concat "(gnus-group-set-info '"
9122                        (gnus-prin1-to-string (gnus-get-info to-group))
9123                        ")"))))
9124
9125           ;; Update the Xref header in this article to point to
9126           ;; the new crossposted article we have just created.
9127           (when (eq action 'crosspost)
9128             (save-excursion
9129               (set-buffer copy-buf)
9130               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9131               (nnheader-replace-header "Xref" new-xref)
9132               (gnus-request-replace-article
9133                article gnus-newsgroup-name (current-buffer))))
9134
9135           ;; run the move/copy/crosspost/respool hook
9136           (run-hook-with-args 'gnus-summary-article-move-hook
9137                               action
9138                               (gnus-data-header
9139                                (assoc article (gnus-data-list nil)))
9140                               gnus-newsgroup-name
9141                               to-newsgroup
9142                               select-method))
9143
9144         ;;;!!!Why is this necessary?
9145         (set-buffer gnus-summary-buffer)
9146         
9147         (gnus-summary-goto-subject article)
9148         (when (eq action 'move)
9149           (gnus-summary-mark-article article gnus-canceled-mark))))
9150       (gnus-summary-remove-process-mark article))
9151     ;; Re-activate all groups that have been moved to.
9152     (save-excursion
9153       (set-buffer gnus-group-buffer)
9154       (let ((gnus-group-marked to-groups))
9155         (gnus-group-get-new-news-this-group nil t)))
9156
9157     (gnus-kill-buffer copy-buf)
9158     (gnus-summary-position-point)
9159     (gnus-set-mode-line 'summary)))
9160
9161 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9162   "Copy the current article to some other group.
9163 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9164 When called interactively, if TO-NEWSGROUP is nil, use the value of
9165 the variable `gnus-move-split-methods' for finding a default target
9166 newsgroup.
9167 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9168 re-spool using this method."
9169   (interactive "P")
9170   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9171
9172 (defun gnus-summary-crosspost-article (&optional n)
9173   "Crosspost the current article to some other group."
9174   (interactive "P")
9175   (gnus-summary-move-article n nil nil 'crosspost))
9176
9177 (defcustom gnus-summary-respool-default-method nil
9178   "Default method type for respooling an article.
9179 If nil, use to the current newsgroup method."
9180   :type 'symbol
9181   :group 'gnus-summary-mail)
9182
9183 (defcustom gnus-summary-display-while-building nil
9184   "If non-nil, show and update the summary buffer as it's being built.
9185 If the value is t, update the buffer after every line is inserted.  If
9186 the value is an integer (N), update the display every N lines."
9187   :group 'gnus-thread
9188   :type '(choice (const :tag "off" nil)
9189                  number
9190                  (const :tag "frequently" t)))
9191
9192 (defun gnus-summary-respool-article (&optional n method)
9193   "Respool the current article.
9194 The article will be squeezed through the mail spooling process again,
9195 which means that it will be put in some mail newsgroup or other
9196 depending on `nnmail-split-methods'.
9197 If N is a positive number, respool the N next articles.
9198 If N is a negative number, respool the N previous articles.
9199 If N is nil and any articles have been marked with the process mark,
9200 respool those articles instead.
9201
9202 Respooling can be done both from mail groups and \"real\" newsgroups.
9203 In the former case, the articles in question will be moved from the
9204 current group into whatever groups they are destined to.  In the
9205 latter case, they will be copied into the relevant groups."
9206   (interactive
9207    (list current-prefix-arg
9208          (let* ((methods (gnus-methods-using 'respool))
9209                 (methname
9210                  (symbol-name (or gnus-summary-respool-default-method
9211                                   (car (gnus-find-method-for-group
9212                                         gnus-newsgroup-name)))))
9213                 (method
9214                  (gnus-completing-read-with-default
9215                   methname "What backend do you want to use when respooling?"
9216                   methods nil t nil 'gnus-mail-method-history))
9217                 ms)
9218            (cond
9219             ((zerop (length (setq ms (gnus-servers-using-backend
9220                                       (intern method)))))
9221              (list (intern method) ""))
9222             ((= 1 (length ms))
9223              (car ms))
9224             (t
9225              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9226                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9227                            ms-alist))))))))
9228   (unless method
9229     (error "No method given for respooling"))
9230   (if (assoc (symbol-name
9231               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9232              (gnus-methods-using 'respool))
9233       (gnus-summary-move-article n nil method)
9234     (gnus-summary-copy-article n nil method)))
9235
9236 (defun gnus-summary-import-article (file &optional edit)
9237   "Import an arbitrary file into a mail newsgroup."
9238   (interactive "fImport file: \nP")
9239   (let ((group gnus-newsgroup-name)
9240         (now (current-time))
9241         atts lines group-art)
9242     (unless (gnus-check-backend-function 'request-accept-article group)
9243       (error "%s does not support article importing" group))
9244     (or (file-readable-p file)
9245         (not (file-regular-p file))
9246         (error "Can't read %s" file))
9247     (save-excursion
9248       (set-buffer (gnus-get-buffer-create " *import file*"))
9249       (erase-buffer)
9250       (nnheader-insert-file-contents file)
9251       (goto-char (point-min))
9252       (if (nnheader-article-p)
9253           (save-restriction
9254             (goto-char (point-min))
9255             (search-forward "\n\n" nil t)
9256             (narrow-to-region (point-min) (1- (point)))
9257             (goto-char (point-min))
9258             (unless (re-search-forward "^date:" nil t)
9259               (goto-char (point-max))
9260               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9261        ;; This doesn't look like an article, so we fudge some headers.
9262         (setq atts (file-attributes file)
9263               lines (count-lines (point-min) (point-max)))
9264         (insert "From: " (read-string "From: ") "\n"
9265                 "Subject: " (read-string "Subject: ") "\n"
9266                 "Date: " (message-make-date (nth 5 atts)) "\n"
9267                 "Message-ID: " (message-make-message-id) "\n"
9268                 "Lines: " (int-to-string lines) "\n"
9269                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9270       (setq group-art (gnus-request-accept-article group nil t))
9271       (kill-buffer (current-buffer)))
9272     (setq gnus-newsgroup-active (gnus-activate-group group))
9273     (forward-line 1)
9274     (gnus-summary-goto-article (cdr group-art) nil t)
9275     (when edit
9276       (gnus-summary-edit-article))))
9277
9278 (defun gnus-summary-create-article ()
9279   "Create an article in a mail newsgroup."
9280   (interactive)
9281   (let ((group gnus-newsgroup-name)
9282         (now (current-time))
9283         group-art)
9284     (unless (gnus-check-backend-function 'request-accept-article group)
9285       (error "%s does not support article importing" group))
9286     (save-excursion
9287       (set-buffer (gnus-get-buffer-create " *import file*"))
9288       (erase-buffer)
9289       (goto-char (point-min))
9290       ;; This doesn't look like an article, so we fudge some headers.
9291       (insert "From: " (read-string "From: ") "\n"
9292               "Subject: " (read-string "Subject: ") "\n"
9293               "Date: " (message-make-date now) "\n"
9294               "Message-ID: " (message-make-message-id) "\n")
9295       (setq group-art (gnus-request-accept-article group nil t))
9296       (kill-buffer (current-buffer)))
9297     (setq gnus-newsgroup-active (gnus-activate-group group))
9298     (forward-line 1)
9299     (gnus-summary-goto-article (cdr group-art) nil t)
9300     (gnus-summary-edit-article)))
9301
9302 (defun gnus-summary-article-posted-p ()
9303   "Say whether the current (mail) article is available from news as well.
9304 This will be the case if the article has both been mailed and posted."
9305   (interactive)
9306   (let ((id (mail-header-references (gnus-summary-article-header)))
9307         (gnus-override-method (car (gnus-refer-article-methods))))
9308     (if (gnus-request-head id "")
9309         (gnus-message 2 "The current message was found on %s"
9310                       gnus-override-method)
9311       (gnus-message 2 "The current message couldn't be found on %s"
9312                     gnus-override-method)
9313       nil)))
9314
9315 (defun gnus-summary-expire-articles (&optional now)
9316   "Expire all articles that are marked as expirable in the current group."
9317   (interactive)
9318   (when (and (not gnus-group-is-exiting-without-update-p)
9319              (gnus-check-backend-function
9320               'request-expire-articles gnus-newsgroup-name))
9321     ;; This backend supports expiry.
9322     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9323            (expirable (if total
9324                           (progn
9325                             ;; We need to update the info for
9326                             ;; this group for `gnus-list-of-read-articles'
9327                             ;; to give us the right answer.
9328                             (gnus-run-hooks 'gnus-exit-group-hook)
9329                             (gnus-summary-update-info)
9330                             (gnus-list-of-read-articles gnus-newsgroup-name))
9331                         (setq gnus-newsgroup-expirable
9332                               (sort gnus-newsgroup-expirable '<))))
9333            (expiry-wait (if now 'immediate
9334                           (gnus-group-find-parameter
9335                            gnus-newsgroup-name 'expiry-wait)))
9336            (nnmail-expiry-target
9337             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9338                 nnmail-expiry-target))
9339            es)
9340       (when expirable
9341         ;; There are expirable articles in this group, so we run them
9342         ;; through the expiry process.
9343         (gnus-message 6 "Expiring articles...")
9344         (unless (gnus-check-group gnus-newsgroup-name)
9345           (error "Can't open server for %s" gnus-newsgroup-name))
9346         ;; The list of articles that weren't expired is returned.
9347         (save-excursion
9348           (if expiry-wait
9349               (let ((nnmail-expiry-wait-function nil)
9350                     (nnmail-expiry-wait expiry-wait))
9351                 (setq es (gnus-request-expire-articles
9352                           expirable gnus-newsgroup-name)))
9353             (setq es (gnus-request-expire-articles
9354                       expirable gnus-newsgroup-name)))
9355           (unless total
9356             (setq gnus-newsgroup-expirable es))
9357           ;; We go through the old list of expirable, and mark all
9358           ;; really expired articles as nonexistent.
9359           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9360             (let ((gnus-use-cache nil))
9361               (dolist (article expirable)
9362                 (when (and (not (memq article es))
9363                            (gnus-data-find article))
9364                   (gnus-summary-mark-article article gnus-canceled-mark)
9365                   (run-hook-with-args 'gnus-summary-article-expire-hook
9366                                       'delete
9367                                       (gnus-data-header
9368                                        (assoc article (gnus-data-list nil)))
9369                                       gnus-newsgroup-name
9370                                       nil
9371                                       nil))))))
9372         (gnus-message 6 "Expiring articles...done")))))
9373
9374 (defun gnus-summary-expire-articles-now ()
9375   "Expunge all expirable articles in the current group.
9376 This means that *all* articles that are marked as expirable will be
9377 deleted forever, right now."
9378   (interactive)
9379   (or gnus-expert-user
9380       (gnus-yes-or-no-p
9381        "Are you really, really, really sure you want to delete all these messages? ")
9382       (error "Phew!"))
9383   (gnus-summary-expire-articles t))
9384
9385 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9386 (defun gnus-summary-delete-article (&optional n)
9387   "Delete the N next (mail) articles.
9388 This command actually deletes articles.  This is not a marking
9389 command.  The article will disappear forever from your life, never to
9390 return.
9391
9392 If N is negative, delete backwards.
9393 If N is nil and articles have been marked with the process mark,
9394 delete these instead.
9395
9396 If `gnus-novice-user' is non-nil you will be asked for
9397 confirmation before the articles are deleted."
9398   (interactive "P")
9399   (unless (gnus-check-backend-function 'request-expire-articles
9400                                        gnus-newsgroup-name)
9401     (error "The current newsgroup does not support article deletion"))
9402   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9403     (error "Couldn't open server"))
9404   ;; Compute the list of articles to delete.
9405   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9406         (nnmail-expiry-target 'delete)
9407         not-deleted)
9408     (if (and gnus-novice-user
9409              (not (gnus-yes-or-no-p
9410                    (format "Do you really want to delete %s forever? "
9411                            (if (> (length articles) 1)
9412                                (format "these %s articles" (length articles))
9413                              "this article")))))
9414         ()
9415       ;; Delete the articles.
9416       (setq not-deleted (gnus-request-expire-articles
9417                          articles gnus-newsgroup-name 'force))
9418       (while articles
9419         (gnus-summary-remove-process-mark (car articles))
9420         ;; The backend might not have been able to delete the article
9421         ;; after all.
9422         (unless (memq (car articles) not-deleted)
9423           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9424         (let* ((article (car articles))
9425                (ghead  (gnus-data-header
9426                                     (assoc article (gnus-data-list nil)))))
9427           (run-hook-with-args 'gnus-summary-article-delete-hook
9428                               'delete ghead gnus-newsgroup-name nil
9429                               nil))
9430         (setq articles (cdr articles)))
9431       (when not-deleted
9432         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9433     (gnus-summary-position-point)
9434     (gnus-set-mode-line 'summary)
9435     not-deleted))
9436
9437 (defun gnus-summary-edit-article (&optional arg)
9438   "Edit the current article.
9439 This will have permanent effect only in mail groups.
9440 If ARG is nil, edit the decoded articles.
9441 If ARG is 1, edit the raw articles.
9442 If ARG is 2, edit the raw articles even in read-only groups.
9443 If ARG is 3, edit the articles with the current handles.
9444 Otherwise, allow editing of articles even in read-only
9445 groups."
9446   (interactive "P")
9447   (let (force raw current-handles)
9448     (cond
9449      ((null arg))
9450      ((eq arg 1)
9451       (setq raw t))
9452      ((eq arg 2)
9453       (setq raw t
9454             force t))
9455      ((eq arg 3)
9456       (setq current-handles
9457             (and (gnus-buffer-live-p gnus-article-buffer)
9458                  (with-current-buffer gnus-article-buffer
9459                    (prog1
9460                        gnus-article-mime-handles
9461                      (setq gnus-article-mime-handles nil))))))
9462      (t
9463       (setq force t)))
9464     (when (and raw (not force)
9465                (member gnus-newsgroup-name '("nndraft:delayed"
9466                                              "nndraft:drafts"
9467                                              "nndraft:queue")))
9468       (error "Can't edit the raw article in group %s"
9469              gnus-newsgroup-name))
9470     (save-excursion
9471       (set-buffer gnus-summary-buffer)
9472       (let ((mail-parse-charset gnus-newsgroup-charset)
9473             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9474         (gnus-set-global-variables)
9475         (when (and (not force)
9476                    (gnus-group-read-only-p))
9477           (error "The current newsgroup does not support article editing"))
9478         (gnus-summary-show-article t)
9479         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9480           (with-current-buffer gnus-article-buffer
9481             (mm-enable-multibyte)))
9482         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9483             (setq raw t))
9484         (gnus-article-edit-article
9485          (if raw 'ignore
9486            `(lambda ()
9487               (let ((mbl mml-buffer-list))
9488                 (setq mml-buffer-list nil)
9489                 (mime-to-mml ,'current-handles)
9490                 (let ((mbl1 mml-buffer-list))
9491                   (setq mml-buffer-list mbl)
9492                   (set (make-local-variable 'mml-buffer-list) mbl1))
9493                 (gnus-make-local-hook 'kill-buffer-hook)
9494                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9495          `(lambda (no-highlight)
9496             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9497                   (message-options message-options)
9498                   (message-options-set-recipient)
9499                   (mail-parse-ignored-charsets
9500                    ',gnus-newsgroup-ignored-charsets))
9501               ,(if (not raw) '(progn
9502                                 (mml-to-mime)
9503                                 (mml-destroy-buffers)
9504                                 (remove-hook 'kill-buffer-hook
9505                                              'mml-destroy-buffers t)
9506                                 (kill-local-variable 'mml-buffer-list)))
9507               (gnus-summary-edit-article-done
9508                ,(or (mail-header-references gnus-current-headers) "")
9509                ,(gnus-group-read-only-p)
9510                ,gnus-summary-buffer no-highlight))))))))
9511
9512 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9513
9514 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9515                                                  no-highlight)
9516   "Make edits to the current article permanent."
9517   (interactive)
9518   (save-excursion
9519    ;; The buffer restriction contains the entire article if it exists.
9520     (when (article-goto-body)
9521       (let ((lines (count-lines (point) (point-max)))
9522             (length (- (point-max) (point)))
9523             (case-fold-search t)
9524             (body (copy-marker (point))))
9525         (goto-char (point-min))
9526         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9527           (delete-region (match-beginning 1) (match-end 1))
9528           (insert (number-to-string length)))
9529         (goto-char (point-min))
9530         (when (re-search-forward
9531                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9532           (delete-region (match-beginning 1) (match-end 1))
9533           (insert (number-to-string length)))
9534         (goto-char (point-min))
9535         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9536           (delete-region (match-beginning 1) (match-end 1))
9537           (insert (number-to-string lines))))))
9538   ;; Replace the article.
9539   (let ((buf (current-buffer)))
9540     (with-temp-buffer
9541       (insert-buffer-substring buf)
9542
9543       (if (and (not read-only)
9544                (not (gnus-request-replace-article
9545                      (cdr gnus-article-current) (car gnus-article-current)
9546                      (current-buffer) t)))
9547           (error "Couldn't replace article")
9548         ;; Update the summary buffer.
9549         (if (and references
9550                  (equal (message-tokenize-header references " ")
9551                         (message-tokenize-header
9552                          (or (message-fetch-field "references") "") " ")))
9553             ;; We only have to update this line.
9554             (save-excursion
9555               (save-restriction
9556                 (message-narrow-to-head)
9557                 (let ((head (buffer-string))
9558                       header)
9559                   (with-temp-buffer
9560                     (insert (format "211 %d Article retrieved.\n"
9561                                     (cdr gnus-article-current)))
9562                     (insert head)
9563                     (insert ".\n")
9564                     (let ((nntp-server-buffer (current-buffer)))
9565                       (setq header (car (gnus-get-newsgroup-headers
9566                                          nil t))))
9567                     (save-excursion
9568                       (set-buffer gnus-summary-buffer)
9569                       (gnus-data-set-header
9570                        (gnus-data-find (cdr gnus-article-current))
9571                        header)
9572                       (gnus-summary-update-article-line
9573                        (cdr gnus-article-current) header)
9574                       (if (gnus-summary-goto-subject
9575                            (cdr gnus-article-current) nil t)
9576                           (gnus-summary-update-secondary-mark
9577                            (cdr gnus-article-current))))))))
9578           ;; Update threads.
9579           (set-buffer (or buffer gnus-summary-buffer))
9580           (gnus-summary-update-article (cdr gnus-article-current))
9581           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9582               (gnus-summary-update-secondary-mark
9583                (cdr gnus-article-current))))
9584         ;; Prettify the article buffer again.
9585         (unless no-highlight
9586           (save-excursion
9587             (set-buffer gnus-article-buffer)
9588             ;;;!!! Fix this -- article should be rehighlighted.
9589             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9590             (set-buffer gnus-original-article-buffer)
9591             (gnus-request-article
9592              (cdr gnus-article-current)
9593              (car gnus-article-current) (current-buffer))))
9594         ;; Prettify the summary buffer line.
9595         (when (gnus-visual-p 'summary-highlight 'highlight)
9596           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9597
9598 (defun gnus-summary-edit-wash (key)
9599   "Perform editing command KEY in the article buffer."
9600   (interactive
9601    (list
9602     (progn
9603       (message "%s" (concat (this-command-keys) "- "))
9604       (read-char))))
9605   (message "")
9606   (gnus-summary-edit-article)
9607   (execute-kbd-macro (concat (this-command-keys) key))
9608   (gnus-article-edit-done))
9609
9610 ;;; Respooling
9611
9612 (defun gnus-summary-respool-query (&optional silent trace)
9613   "Query where the respool algorithm would put this article."
9614   (interactive)
9615   (let (gnus-mark-article-hook)
9616     (gnus-summary-select-article)
9617     (save-excursion
9618       (set-buffer gnus-original-article-buffer)
9619       (let ((groups (nnmail-article-group 'identity trace)))
9620         (unless silent
9621           (if groups
9622               (message "This message would go to %s"
9623                        (mapconcat 'car groups ", "))
9624             (message "This message would go to no groups"))
9625           groups)))))
9626
9627 (defun gnus-summary-respool-trace ()
9628   "Trace where the respool algorithm would put this article.
9629 Display a buffer showing all fancy splitting patterns which matched."
9630   (interactive)
9631   (gnus-summary-respool-query nil t))
9632
9633 ;; Summary marking commands.
9634
9635 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9636   "Mark articles which has the same subject as read, and then select the next.
9637 If UNMARK is positive, remove any kind of mark.
9638 If UNMARK is negative, tick articles."
9639   (interactive "P")
9640   (when unmark
9641     (setq unmark (prefix-numeric-value unmark)))
9642   (let ((count
9643          (gnus-summary-mark-same-subject
9644           (gnus-summary-article-subject) unmark)))
9645     ;; Select next unread article.  If auto-select-same mode, should
9646     ;; select the first unread article.
9647     (gnus-summary-next-article t (and gnus-auto-select-same
9648                                       (gnus-summary-article-subject)))
9649     (gnus-message 7 "%d article%s marked as %s"
9650                   count (if (= count 1) " is" "s are")
9651                   (if unmark "unread" "read"))))
9652
9653 (defun gnus-summary-kill-same-subject (&optional unmark)
9654   "Mark articles which has the same subject as read.
9655 If UNMARK is positive, remove any kind of mark.
9656 If UNMARK is negative, tick articles."
9657   (interactive "P")
9658   (when unmark
9659     (setq unmark (prefix-numeric-value unmark)))
9660   (let ((count
9661          (gnus-summary-mark-same-subject
9662           (gnus-summary-article-subject) unmark)))
9663     ;; If marked as read, go to next unread subject.
9664     (when (null unmark)
9665       ;; Go to next unread subject.
9666       (gnus-summary-next-subject 1 t))
9667     (gnus-message 7 "%d articles are marked as %s"
9668                   count (if unmark "unread" "read"))))
9669
9670 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9671   "Mark articles with same SUBJECT as read, and return marked number.
9672 If optional argument UNMARK is positive, remove any kinds of marks.
9673 If optional argument UNMARK is negative, mark articles as unread instead."
9674   (let ((count 1))
9675     (save-excursion
9676       (cond
9677        ((null unmark)                   ; Mark as read.
9678         (while (and
9679                 (progn
9680                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9681                   (gnus-summary-show-thread) t)
9682                 (gnus-summary-find-subject subject))
9683           (setq count (1+ count))))
9684        ((> unmark 0)                    ; Tick.
9685         (while (and
9686                 (progn
9687                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9688                   (gnus-summary-show-thread) t)
9689                 (gnus-summary-find-subject subject))
9690           (setq count (1+ count))))
9691        (t                               ; Mark as unread.
9692         (while (and
9693                 (progn
9694                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9695                   (gnus-summary-show-thread) t)
9696                 (gnus-summary-find-subject subject))
9697           (setq count (1+ count)))))
9698       (gnus-set-mode-line 'summary)
9699       ;; Return the number of marked articles.
9700       count)))
9701
9702 (defun gnus-summary-mark-as-processable (n &optional unmark)
9703   "Set the process mark on the next N articles.
9704 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9705 the process mark instead.  The difference between N and the actual
9706 number of articles marked is returned."
9707   (interactive "P")
9708   (if (and (null n) (gnus-region-active-p))
9709       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9710     (setq n (prefix-numeric-value n))
9711     (let ((backward (< n 0))
9712           (n (abs n)))
9713       (while (and
9714               (> n 0)
9715               (if unmark
9716                   (gnus-summary-remove-process-mark
9717                    (gnus-summary-article-number))
9718                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9719               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9720         (setq n (1- n)))
9721       (when (/= 0 n)
9722         (gnus-message 7 "No more articles"))
9723       (gnus-summary-recenter)
9724       (gnus-summary-position-point)
9725       n)))
9726
9727 (defun gnus-summary-unmark-as-processable (n)
9728   "Remove the process mark from the next N articles.
9729 If N is negative, unmark backward instead.  The difference between N and
9730 the actual number of articles unmarked is returned."
9731   (interactive "P")
9732   (gnus-summary-mark-as-processable n t))
9733
9734 (defun gnus-summary-unmark-all-processable ()
9735   "Remove the process mark from all articles."
9736   (interactive)
9737   (save-excursion
9738     (while gnus-newsgroup-processable
9739       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9740   (gnus-summary-position-point))
9741
9742 (defun gnus-summary-add-mark (article type)
9743   "Mark ARTICLE with a mark of TYPE."
9744   (let ((vtype (car (assq type gnus-article-mark-lists)))
9745         var)
9746     (if (not vtype)
9747         (error "No such mark type: %s" type)
9748       (setq var (intern (format "gnus-newsgroup-%s" type)))
9749       (set var (cons article (symbol-value var)))
9750       (if (memq type '(processable cached replied forwarded recent saved))
9751           (gnus-summary-update-secondary-mark article)
9752         ;;; !!! This is bogus.  We should find out what primary
9753         ;;; !!! mark we want to set.
9754         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9755
9756 (defun gnus-summary-mark-as-expirable (n)
9757   "Mark N articles forward as expirable.
9758 If N is negative, mark backward instead.  The difference between N and
9759 the actual number of articles marked is returned."
9760   (interactive "p")
9761   (gnus-summary-mark-forward n gnus-expirable-mark))
9762
9763 (defun gnus-summary-mark-as-spam (n)
9764   "Mark N articles forward as spam.
9765 If N is negative, mark backward instead.  The difference between N and
9766 the actual number of articles marked is returned."
9767   (interactive "p")
9768   (gnus-summary-mark-forward n gnus-spam-mark))
9769
9770 (defun gnus-summary-mark-article-as-replied (article)
9771   "Mark ARTICLE as replied to and update the summary line.
9772 ARTICLE can also be a list of articles."
9773   (interactive (list (gnus-summary-article-number)))
9774   (let ((articles (if (listp article) article (list article))))
9775     (dolist (article articles)
9776       (unless (numberp article)
9777         (error "%s is not a number" article))
9778       (push article gnus-newsgroup-replied)
9779       (let ((buffer-read-only nil))
9780         (when (gnus-summary-goto-subject article nil t)
9781           (gnus-summary-update-secondary-mark article))))))
9782
9783 (defun gnus-summary-mark-article-as-forwarded (article)
9784   "Mark ARTICLE as forwarded and update the summary line.
9785 ARTICLE can also be a list of articles."
9786   (let ((articles (if (listp article) article (list article))))
9787     (dolist (article articles)
9788       (push article gnus-newsgroup-forwarded)
9789       (let ((buffer-read-only nil))
9790         (when (gnus-summary-goto-subject article nil t)
9791           (gnus-summary-update-secondary-mark article))))))
9792
9793 (defun gnus-summary-set-bookmark (article)
9794   "Set a bookmark in current article."
9795   (interactive (list (gnus-summary-article-number)))
9796   (when (or (not (get-buffer gnus-article-buffer))
9797             (not gnus-current-article)
9798             (not gnus-article-current)
9799             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9800     (error "No current article selected"))
9801   ;; Remove old bookmark, if one exists.
9802   (gnus-pull article gnus-newsgroup-bookmarks)
9803   ;; Set the new bookmark, which is on the form
9804   ;; (article-number . line-number-in-body).
9805   (push
9806    (cons article
9807          (with-current-buffer gnus-article-buffer
9808            (count-lines
9809             (min (point)
9810                  (save-excursion
9811                    (article-goto-body)
9812                    (point)))
9813             (point))))
9814    gnus-newsgroup-bookmarks)
9815   (gnus-message 6 "A bookmark has been added to the current article."))
9816
9817 (defun gnus-summary-remove-bookmark (article)
9818   "Remove the bookmark from the current article."
9819   (interactive (list (gnus-summary-article-number)))
9820   ;; Remove old bookmark, if one exists.
9821   (if (not (assq article gnus-newsgroup-bookmarks))
9822       (gnus-message 6 "No bookmark in current article.")
9823     (gnus-pull article gnus-newsgroup-bookmarks)
9824     (gnus-message 6 "Removed bookmark.")))
9825
9826 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9827 (defun gnus-summary-mark-as-dormant (n)
9828   "Mark N articles forward as dormant.
9829 If N is negative, mark backward instead.  The difference between N and
9830 the actual number of articles marked is returned."
9831   (interactive "p")
9832   (gnus-summary-mark-forward n gnus-dormant-mark))
9833
9834 (defun gnus-summary-set-process-mark (article)
9835   "Set the process mark on ARTICLE and update the summary line."
9836   (setq gnus-newsgroup-processable
9837         (cons article
9838               (delq article gnus-newsgroup-processable)))
9839   (when (gnus-summary-goto-subject article)
9840     (gnus-summary-show-thread)
9841     (gnus-summary-goto-subject article)
9842     (gnus-summary-update-secondary-mark article)))
9843
9844 (defun gnus-summary-remove-process-mark (article)
9845   "Remove the process mark from ARTICLE and update the summary line."
9846   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9847   (when (gnus-summary-goto-subject article)
9848     (gnus-summary-show-thread)
9849     (gnus-summary-goto-subject article)
9850     (gnus-summary-update-secondary-mark article)))
9851
9852 (defun gnus-summary-set-saved-mark (article)
9853   "Set the process mark on ARTICLE and update the summary line."
9854   (push article gnus-newsgroup-saved)
9855   (when (gnus-summary-goto-subject article)
9856     (gnus-summary-update-secondary-mark article)))
9857
9858 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9859   "Mark N articles as read forwards.
9860 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9861 The difference between N and the actual number of articles marked is
9862 returned.
9863 If NO-EXPIRE, auto-expiry will be inhibited."
9864   (interactive "p")
9865   (gnus-summary-show-thread)
9866   (let ((backward (< n 0))
9867         (gnus-summary-goto-unread
9868          (and gnus-summary-goto-unread
9869               (not (eq gnus-summary-goto-unread 'never))
9870               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9871                                     gnus-ticked-mark gnus-dormant-mark)))))
9872         (n (abs n))
9873         (mark (or mark gnus-del-mark)))
9874     (while (and (> n 0)
9875                 (gnus-summary-mark-article nil mark no-expire)
9876                 (zerop (gnus-summary-next-subject
9877                         (if backward -1 1)
9878                         (and gnus-summary-goto-unread
9879                              (not (eq gnus-summary-goto-unread 'never)))
9880                         t)))
9881       (setq n (1- n)))
9882     (when (/= 0 n)
9883       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9884     (gnus-summary-recenter)
9885     (gnus-summary-position-point)
9886     (gnus-set-mode-line 'summary)
9887     n))
9888
9889 (defun gnus-summary-mark-article-as-read (mark)
9890   "Mark the current article quickly as read with MARK."
9891   (let ((article (gnus-summary-article-number)))
9892     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9893     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9894     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9895     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9896     (push (cons article mark) gnus-newsgroup-reads)
9897     ;; Possibly remove from cache, if that is used.
9898     (when gnus-use-cache
9899       (gnus-cache-enter-remove-article article))
9900     ;; Allow the backend to change the mark.
9901     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9902     ;; Check for auto-expiry.
9903     (when (and gnus-newsgroup-auto-expire
9904                (memq mark gnus-auto-expirable-marks))
9905       (setq mark gnus-expirable-mark)
9906       ;; Let the backend know about the mark change.
9907       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9908       (push article gnus-newsgroup-expirable))
9909     ;; Set the mark in the buffer.
9910     (gnus-summary-update-mark mark 'unread)
9911     t))
9912
9913 (defun gnus-summary-mark-article-as-unread (mark)
9914   "Mark the current article quickly as unread with MARK."
9915   (let* ((article (gnus-summary-article-number))
9916          (old-mark (gnus-summary-article-mark article)))
9917     ;; Allow the backend to change the mark.
9918     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9919     (if (eq mark old-mark)
9920         t
9921       (if (<= article 0)
9922           (progn
9923             (gnus-error 1 "Can't mark negative article numbers")
9924             nil)
9925         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9926         (setq gnus-newsgroup-spam-marked
9927               (delq article gnus-newsgroup-spam-marked))
9928         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9929         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9930         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9931         (cond ((= mark gnus-ticked-mark)
9932                (setq gnus-newsgroup-marked
9933                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9934                                               article)))
9935               ((= mark gnus-spam-mark)
9936                (setq gnus-newsgroup-spam-marked
9937                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9938                                               article)))
9939               ((= mark gnus-dormant-mark)
9940                (setq gnus-newsgroup-dormant
9941                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9942                                               article)))
9943               (t
9944                (setq gnus-newsgroup-unreads
9945                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9946                                               article))))
9947         (gnus-pull article gnus-newsgroup-reads)
9948
9949         ;; See whether the article is to be put in the cache.
9950         (and gnus-use-cache
9951              (vectorp (gnus-summary-article-header article))
9952              (save-excursion
9953                (gnus-cache-possibly-enter-article
9954                 gnus-newsgroup-name article
9955                 (= mark gnus-ticked-mark)
9956                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9957
9958         ;; Fix the mark.
9959         (gnus-summary-update-mark mark 'unread)
9960         t))))
9961
9962 (defun gnus-summary-mark-article (&optional article mark no-expire)
9963   "Mark ARTICLE with MARK.  MARK can be any character.
9964 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9965 `??' (dormant) and `?E' (expirable).
9966 If MARK is nil, then the default character `?r' is used.
9967 If ARTICLE is nil, then the article on the current line will be
9968 marked.
9969 If NO-EXPIRE, auto-expiry will be inhibited."
9970   ;; The mark might be a string.
9971   (when (stringp mark)
9972     (setq mark (aref mark 0)))
9973   ;; If no mark is given, then we check auto-expiring.
9974   (when (null mark)
9975     (setq mark gnus-del-mark))
9976   (when (and (not no-expire)
9977              gnus-newsgroup-auto-expire
9978              (memq mark gnus-auto-expirable-marks))
9979     (setq mark gnus-expirable-mark))
9980   (let ((article (or article (gnus-summary-article-number)))
9981         (old-mark (gnus-summary-article-mark article)))
9982     ;; Allow the backend to change the mark.
9983     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9984     (if (eq mark old-mark)
9985         t
9986       (unless article
9987         (error "No article on current line"))
9988       (if (not (if (or (= mark gnus-unread-mark)
9989                        (= mark gnus-ticked-mark)
9990                        (= mark gnus-spam-mark)
9991                        (= mark gnus-dormant-mark))
9992                    (gnus-mark-article-as-unread article mark)
9993                  (gnus-mark-article-as-read article mark)))
9994           t
9995         ;; See whether the article is to be put in the cache.
9996         (and gnus-use-cache
9997              (not (= mark gnus-canceled-mark))
9998              (vectorp (gnus-summary-article-header article))
9999              (save-excursion
10000                (gnus-cache-possibly-enter-article
10001                 gnus-newsgroup-name article
10002                 (= mark gnus-ticked-mark)
10003                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10004
10005         (when (gnus-summary-goto-subject article nil t)
10006           (let ((buffer-read-only nil))
10007             (gnus-summary-show-thread)
10008             ;; Fix the mark.
10009             (gnus-summary-update-mark mark 'unread)
10010             t))))))
10011
10012 (defun gnus-summary-update-secondary-mark (article)
10013   "Update the secondary (read, process, cache) mark."
10014   (gnus-summary-update-mark
10015    (cond ((memq article gnus-newsgroup-processable)
10016           gnus-process-mark)
10017          ((memq article gnus-newsgroup-cached)
10018           gnus-cached-mark)
10019          ((memq article gnus-newsgroup-replied)
10020           gnus-replied-mark)
10021          ((memq article gnus-newsgroup-forwarded)
10022           gnus-forwarded-mark)
10023          ((memq article gnus-newsgroup-saved)
10024           gnus-saved-mark)
10025          ((memq article gnus-newsgroup-recent)
10026           gnus-recent-mark)
10027          ((memq article gnus-newsgroup-unseen)
10028           gnus-unseen-mark)
10029          (t gnus-no-mark))
10030    'replied)
10031   (when (gnus-visual-p 'summary-highlight 'highlight)
10032     (gnus-run-hooks 'gnus-summary-update-hook))
10033   t)
10034
10035 (defun gnus-summary-update-download-mark (article)
10036   "Update the download mark."
10037   (gnus-summary-update-mark
10038    (cond ((memq article gnus-newsgroup-undownloaded)
10039           gnus-undownloaded-mark)
10040          (gnus-newsgroup-agentized
10041           gnus-downloaded-mark)
10042          (t
10043           gnus-no-mark))
10044    'download)
10045   (gnus-summary-update-line t)
10046   t)
10047
10048 (defun gnus-summary-update-mark (mark type)
10049   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10050         (buffer-read-only nil))
10051     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10052     (when forward
10053       (when (looking-at "\r")
10054         (incf forward))
10055       (when (<= (+ forward (point)) (point-max))
10056         ;; Go to the right position on the line.
10057         (goto-char (+ forward (point)))
10058         ;; Replace the old mark with the new mark.
10059         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10060         ;; Optionally update the marks by some user rule.
10061         (when (eq type 'unread)
10062           (gnus-data-set-mark
10063            (gnus-data-find (gnus-summary-article-number)) mark)
10064           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10065
10066 (defun gnus-mark-article-as-read (article &optional mark)
10067   "Enter ARTICLE in the pertinent lists and remove it from others."
10068   ;; Make the article expirable.
10069   (let ((mark (or mark gnus-del-mark)))
10070     (setq gnus-newsgroup-expirable
10071           (if (= mark gnus-expirable-mark)
10072               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10073             (delq article gnus-newsgroup-expirable)))
10074     ;; Remove from unread and marked lists.
10075     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10076     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10077     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10078     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10079     (push (cons article mark) gnus-newsgroup-reads)
10080     ;; Possibly remove from cache, if that is used.
10081     (when gnus-use-cache
10082       (gnus-cache-enter-remove-article article))
10083     t))
10084
10085 (defun gnus-mark-article-as-unread (article &optional mark)
10086   "Enter ARTICLE in the pertinent lists and remove it from others."
10087   (let ((mark (or mark gnus-ticked-mark)))
10088     (if (<= article 0)
10089         (progn
10090           (gnus-error 1 "Can't mark negative article numbers")
10091           nil)
10092       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10093             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10094             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10095             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10096             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10097
10098       ;; Unsuppress duplicates?
10099       (when gnus-suppress-duplicates
10100         (gnus-dup-unsuppress-article article))
10101
10102       (cond ((= mark gnus-ticked-mark)
10103              (setq gnus-newsgroup-marked
10104                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10105             ((= mark gnus-spam-mark)
10106              (setq gnus-newsgroup-spam-marked
10107                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10108                                             article)))
10109             ((= mark gnus-dormant-mark)
10110              (setq gnus-newsgroup-dormant
10111                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10112             (t
10113              (setq gnus-newsgroup-unreads
10114                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10115       (gnus-pull article gnus-newsgroup-reads)
10116       t)))
10117
10118 (defalias 'gnus-summary-mark-as-unread-forward
10119   'gnus-summary-tick-article-forward)
10120 (make-obsolete 'gnus-summary-mark-as-unread-forward
10121                'gnus-summary-tick-article-forward)
10122 (defun gnus-summary-tick-article-forward (n)
10123   "Tick N articles forwards.
10124 If N is negative, tick backwards instead.
10125 The difference between N and the number of articles ticked is returned."
10126   (interactive "p")
10127   (gnus-summary-mark-forward n gnus-ticked-mark))
10128
10129 (defalias 'gnus-summary-mark-as-unread-backward
10130   'gnus-summary-tick-article-backward)
10131 (make-obsolete 'gnus-summary-mark-as-unread-backward
10132                'gnus-summary-tick-article-backward)
10133 (defun gnus-summary-tick-article-backward (n)
10134   "Tick N articles backwards.
10135 The difference between N and the number of articles ticked is returned."
10136   (interactive "p")
10137   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10138
10139 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10140 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10141 (defun gnus-summary-tick-article (&optional article clear-mark)
10142   "Mark current article as unread.
10143 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10144 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10145   (interactive)
10146   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10147                                        gnus-ticked-mark)))
10148
10149 (defun gnus-summary-mark-as-read-forward (n)
10150   "Mark N articles as read forwards.
10151 If N is negative, mark backwards instead.
10152 The difference between N and the actual number of articles marked is
10153 returned."
10154   (interactive "p")
10155   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10156
10157 (defun gnus-summary-mark-as-read-backward (n)
10158   "Mark the N articles as read backwards.
10159 The difference between N and the actual number of articles marked is
10160 returned."
10161   (interactive "p")
10162   (gnus-summary-mark-forward
10163    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10164
10165 (defun gnus-summary-mark-as-read (&optional article mark)
10166   "Mark current article as read.
10167 ARTICLE specifies the article to be marked as read.
10168 MARK specifies a string to be inserted at the beginning of the line."
10169   (gnus-summary-mark-article article mark))
10170
10171 (defun gnus-summary-clear-mark-forward (n)
10172   "Clear marks from N articles forward.
10173 If N is negative, clear backward instead.
10174 The difference between N and the number of marks cleared is returned."
10175   (interactive "p")
10176   (gnus-summary-mark-forward n gnus-unread-mark))
10177
10178 (defun gnus-summary-clear-mark-backward (n)
10179   "Clear marks from N articles backward.
10180 The difference between N and the number of marks cleared is returned."
10181   (interactive "p")
10182   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10183
10184 (defun gnus-summary-mark-unread-as-read ()
10185   "Intended to be used by `gnus-summary-mark-article-hook'."
10186   (when (memq gnus-current-article gnus-newsgroup-unreads)
10187     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10188
10189 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10190   "Intended to be used by `gnus-summary-mark-article-hook'."
10191   (let ((mark (gnus-summary-article-mark)))
10192     (when (or (gnus-unread-mark-p mark)
10193               (gnus-read-mark-p mark))
10194       (gnus-summary-mark-article gnus-current-article
10195                                  (or new-mark gnus-read-mark)))))
10196
10197 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10198   "Intended to be used by `gnus-summary-mark-article-hook'."
10199   (let ((mark (gnus-summary-article-mark)))
10200     (when (or (gnus-unread-mark-p mark)
10201               (gnus-read-mark-p mark))
10202       (gnus-summary-mark-article (gnus-summary-article-number)
10203                                  (or new-mark gnus-read-mark)))))
10204
10205 (defun gnus-summary-mark-unread-as-ticked ()
10206   "Intended to be used by `gnus-summary-mark-article-hook'."
10207   (when (memq gnus-current-article gnus-newsgroup-unreads)
10208     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10209
10210 (defun gnus-summary-mark-region-as-read (point mark all)
10211   "Mark all unread articles between point and mark as read.
10212 If given a prefix, mark all articles between point and mark as read,
10213 even ticked and dormant ones."
10214   (interactive "r\nP")
10215   (save-excursion
10216     (let (article)
10217       (goto-char point)
10218       (beginning-of-line)
10219       (while (and
10220               (< (point) mark)
10221               (progn
10222                 (when (or all
10223                           (memq (setq article (gnus-summary-article-number))
10224                                 gnus-newsgroup-unreads))
10225                   (gnus-summary-mark-article article gnus-del-mark))
10226                 t)
10227               (gnus-summary-find-next))))))
10228
10229 (defun gnus-summary-mark-below (score mark)
10230   "Mark articles with score less than SCORE with MARK."
10231   (interactive "P\ncMark: ")
10232   (setq score (if score
10233                   (prefix-numeric-value score)
10234                 (or gnus-summary-default-score 0)))
10235   (save-excursion
10236     (set-buffer gnus-summary-buffer)
10237     (goto-char (point-min))
10238     (while
10239         (progn
10240           (and (< (gnus-summary-article-score) score)
10241                (gnus-summary-mark-article nil mark))
10242           (gnus-summary-find-next)))))
10243
10244 (defun gnus-summary-kill-below (&optional score)
10245   "Mark articles with score below SCORE as read."
10246   (interactive "P")
10247   (gnus-summary-mark-below score gnus-killed-mark))
10248
10249 (defun gnus-summary-clear-above (&optional score)
10250   "Clear all marks from articles with score above SCORE."
10251   (interactive "P")
10252   (gnus-summary-mark-above score gnus-unread-mark))
10253
10254 (defun gnus-summary-tick-above (&optional score)
10255   "Tick all articles with score above SCORE."
10256   (interactive "P")
10257   (gnus-summary-mark-above score gnus-ticked-mark))
10258
10259 (defun gnus-summary-mark-above (score mark)
10260   "Mark articles with score over SCORE with MARK."
10261   (interactive "P\ncMark: ")
10262   (setq score (if score
10263                   (prefix-numeric-value score)
10264                 (or gnus-summary-default-score 0)))
10265   (save-excursion
10266     (set-buffer gnus-summary-buffer)
10267     (goto-char (point-min))
10268     (while (and (progn
10269                   (when (> (gnus-summary-article-score) score)
10270                     (gnus-summary-mark-article nil mark))
10271                   t)
10272                 (gnus-summary-find-next)))))
10273
10274 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10275 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10276 (defun gnus-summary-limit-include-expunged (&optional no-error)
10277   "Display all the hidden articles that were expunged for low scores."
10278   (interactive)
10279   (let ((buffer-read-only nil))
10280     (let ((scored gnus-newsgroup-scored)
10281           headers h)
10282       (while scored
10283         (unless (gnus-summary-article-header (caar scored))
10284           (and (setq h (gnus-number-to-header (caar scored)))
10285                (< (cdar scored) gnus-summary-expunge-below)
10286                (push h headers)))
10287         (setq scored (cdr scored)))
10288       (if (not headers)
10289           (when (not no-error)
10290             (error "No expunged articles hidden"))
10291         (goto-char (point-min))
10292         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10293         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10294         (mapcar (lambda (x) (push (mail-header-number x)
10295                                   gnus-newsgroup-limit))
10296                 headers)
10297         (gnus-summary-prepare-unthreaded (nreverse headers))
10298         (goto-char (point-min))
10299         (gnus-summary-position-point)
10300         t))))
10301
10302 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10303   "Mark all unread articles in this newsgroup as read.
10304 If prefix argument ALL is non-nil, ticked and dormant articles will
10305 also be marked as read.
10306 If QUIETLY is non-nil, no questions will be asked.
10307
10308 If TO-HERE is non-nil, it should be a point in the buffer.  All
10309 articles before (after, if REVERSE is set) this point will be marked
10310 as read.
10311
10312 Note that this function will only catch up the unread article
10313 in the current summary buffer limitation.
10314
10315 The number of articles marked as read is returned."
10316   (interactive "P")
10317   (prog1
10318       (save-excursion
10319         (when (or quietly
10320                   (not gnus-interactive-catchup) ;Without confirmation?
10321                   gnus-expert-user
10322                   (gnus-y-or-n-p
10323                    (if all
10324                        "Mark absolutely all articles as read? "
10325                      "Mark all unread articles as read? ")))
10326           (if (and not-mark
10327                    (not gnus-newsgroup-adaptive)
10328                    (not gnus-newsgroup-auto-expire)
10329                    (not gnus-suppress-duplicates)
10330                    (or (not gnus-use-cache)
10331                        (eq gnus-use-cache 'passive)))
10332               (progn
10333                 (when all
10334                   (setq gnus-newsgroup-marked nil
10335                         gnus-newsgroup-spam-marked nil
10336                         gnus-newsgroup-dormant nil))
10337                 (setq gnus-newsgroup-unreads
10338                       (gnus-sorted-nunion
10339                        (gnus-intersection gnus-newsgroup-unreads
10340                                           gnus-newsgroup-downloadable)
10341                        gnus-newsgroup-unfetched)))
10342             ;; We actually mark all articles as canceled, which we
10343             ;; have to do when using auto-expiry or adaptive scoring.
10344             (gnus-summary-show-all-threads)
10345             (if (and to-here reverse)
10346                 (progn
10347                   (goto-char to-here)
10348                   (gnus-summary-mark-current-read-and-unread-as-read
10349                    gnus-catchup-mark)
10350                   (while (gnus-summary-find-next (not all))
10351                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10352               (when (gnus-summary-first-subject (not all))
10353                 (while (and
10354                         (if to-here (< (point) to-here) t)
10355                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10356                         (gnus-summary-find-next (not all))))))
10357             (gnus-set-mode-line 'summary))
10358           t))
10359     (gnus-summary-position-point)))
10360
10361 (defun gnus-summary-catchup-to-here (&optional all)
10362   "Mark all unticked articles before the current one as read.
10363 If ALL is non-nil, also mark ticked and dormant articles as read."
10364   (interactive "P")
10365   (save-excursion
10366     (gnus-save-hidden-threads
10367       (let ((beg (point)))
10368         ;; We check that there are unread articles.
10369         (when (or all (gnus-summary-find-prev))
10370           (gnus-summary-catchup all t beg)))))
10371   (gnus-summary-position-point))
10372
10373 (defun gnus-summary-catchup-from-here (&optional all)
10374   "Mark all unticked articles after (and including) the current one as read.
10375 If ALL is non-nil, also mark ticked and dormant articles as read."
10376   (interactive "P")
10377   (save-excursion
10378     (gnus-save-hidden-threads
10379       (let ((beg (point)))
10380         ;; We check that there are unread articles.
10381         (when (or all (gnus-summary-find-next))
10382           (gnus-summary-catchup all t beg nil t)))))
10383   (gnus-summary-position-point))
10384
10385 (defun gnus-summary-catchup-all (&optional quietly)
10386   "Mark all articles in this newsgroup as read.
10387 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10388 instead, which marks only unread articles as read."
10389   (interactive "P")
10390   (gnus-summary-catchup t quietly))
10391
10392 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10393   "Mark all unread articles in this group as read, then exit.
10394 If prefix argument ALL is non-nil, all articles are marked as read.
10395 If QUIETLY is non-nil, no questions will be asked."
10396   (interactive "P")
10397   (when (gnus-summary-catchup all quietly nil 'fast)
10398     ;; Select next newsgroup or exit.
10399     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10400              (eq gnus-auto-select-next 'quietly))
10401         (gnus-summary-next-group nil)
10402       (gnus-summary-exit))))
10403
10404 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10405   "Mark all articles in this newsgroup as read, and then exit.
10406 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10407 instead, which marks only unread articles as read."
10408   (interactive "P")
10409   (gnus-summary-catchup-and-exit t quietly))
10410
10411 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10412   "Mark all articles in this group as read and select the next group.
10413 If given a prefix, mark all articles, unread as well as ticked, as
10414 read."
10415   (interactive "P")
10416   (save-excursion
10417     (gnus-summary-catchup all))
10418   (gnus-summary-next-group))
10419
10420 ;;;
10421 ;;; with article
10422 ;;;
10423
10424 (defmacro gnus-with-article (article &rest forms)
10425   "Select ARTICLE and perform FORMS in the original article buffer.
10426 Then replace the article with the result."
10427   `(progn
10428      ;; We don't want the article to be marked as read.
10429      (let (gnus-mark-article-hook)
10430        (gnus-summary-select-article t t nil ,article))
10431      (set-buffer gnus-original-article-buffer)
10432      ,@forms
10433      (if (not (gnus-check-backend-function
10434                'request-replace-article (car gnus-article-current)))
10435          (gnus-message 5 "Read-only group; not replacing")
10436        (unless (gnus-request-replace-article
10437                 ,article (car gnus-article-current)
10438                 (current-buffer) t)
10439          (error "Couldn't replace article")))
10440      ;; The cache and backlog have to be flushed somewhat.
10441      (when gnus-keep-backlog
10442        (gnus-backlog-remove-article
10443         (car gnus-article-current) (cdr gnus-article-current)))
10444      (when gnus-use-cache
10445        (gnus-cache-update-article
10446         (car gnus-article-current) (cdr gnus-article-current)))))
10447
10448 (put 'gnus-with-article 'lisp-indent-function 1)
10449 (put 'gnus-with-article 'edebug-form-spec '(form body))
10450
10451 ;; Thread-based commands.
10452
10453 (defun gnus-summary-articles-in-thread (&optional article)
10454   "Return a list of all articles in the current thread.
10455 If ARTICLE is non-nil, return all articles in the thread that starts
10456 with that article."
10457   (let* ((article (or article (gnus-summary-article-number)))
10458          (data (gnus-data-find-list article))
10459          (top-level (gnus-data-level (car data)))
10460          (top-subject
10461           (cond ((null gnus-thread-operation-ignore-subject)
10462                  (gnus-simplify-subject-re
10463                   (mail-header-subject (gnus-data-header (car data)))))
10464                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10465                  (gnus-simplify-subject-fuzzy
10466                   (mail-header-subject (gnus-data-header (car data)))))
10467                 (t nil)))
10468          (end-point (save-excursion
10469                       (if (gnus-summary-go-to-next-thread)
10470                           (point) (point-max))))
10471          articles)
10472     (while (and data
10473                 (< (gnus-data-pos (car data)) end-point))
10474       (when (or (not top-subject)
10475                 (string= top-subject
10476                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10477                              (gnus-simplify-subject-fuzzy
10478                               (mail-header-subject
10479                                (gnus-data-header (car data))))
10480                            (gnus-simplify-subject-re
10481                             (mail-header-subject
10482                              (gnus-data-header (car data)))))))
10483         (push (gnus-data-number (car data)) articles))
10484       (unless (and (setq data (cdr data))
10485                    (> (gnus-data-level (car data)) top-level))
10486         (setq data nil)))
10487     ;; Return the list of articles.
10488     (nreverse articles)))
10489
10490 (defun gnus-summary-rethread-current ()
10491   "Rethread the thread the current article is part of."
10492   (interactive)
10493   (let* ((gnus-show-threads t)
10494          (article (gnus-summary-article-number))
10495          (id (mail-header-id (gnus-summary-article-header)))
10496          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10497     (unless id
10498       (error "No article on the current line"))
10499     (gnus-rebuild-thread id)
10500     (gnus-summary-goto-subject article)))
10501
10502 (defun gnus-summary-reparent-thread ()
10503   "Make the current article child of the marked (or previous) article.
10504
10505 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10506 is non-nil or the Subject: of both articles are the same."
10507   (interactive)
10508   (unless (not (gnus-group-read-only-p))
10509     (error "The current newsgroup does not support article editing"))
10510   (unless (<= (length gnus-newsgroup-processable) 1)
10511     (error "No more than one article may be marked"))
10512   (save-window-excursion
10513     (let ((gnus-article-buffer " *reparent*")
10514           (current-article (gnus-summary-article-number))
10515           ;; First grab the marked article, otherwise one line up.
10516           (parent-article (if (not (null gnus-newsgroup-processable))
10517                               (car gnus-newsgroup-processable)
10518                             (save-excursion
10519                               (if (eq (forward-line -1) 0)
10520                                   (gnus-summary-article-number)
10521                                 (error "Beginning of summary buffer"))))))
10522       (unless (not (eq current-article parent-article))
10523         (error "An article may not be self-referential"))
10524       (let ((message-id (mail-header-id
10525                          (gnus-summary-article-header parent-article))))
10526         (unless (and message-id (not (equal message-id "")))
10527           (error "No message-id in desired parent"))
10528         (gnus-with-article current-article
10529           (save-restriction
10530             (goto-char (point-min))
10531             (message-narrow-to-head)
10532             (if (re-search-forward "^References: " nil t)
10533                 (progn
10534                   (re-search-forward "^[^ \t]" nil t)
10535                   (forward-line -1)
10536                   (end-of-line)
10537                   (insert " " message-id))
10538               (insert "References: " message-id "\n"))))
10539         (set-buffer gnus-summary-buffer)
10540         (gnus-summary-unmark-all-processable)
10541         (gnus-summary-update-article current-article)
10542         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10543             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10544         (gnus-summary-rethread-current)
10545         (gnus-message 3 "Article %d is now the child of article %d"
10546                       current-article parent-article)))))
10547
10548 (defun gnus-summary-toggle-threads (&optional arg)
10549   "Toggle showing conversation threads.
10550 If ARG is positive number, turn showing conversation threads on."
10551   (interactive "P")
10552   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10553     (setq gnus-show-threads
10554           (if (null arg) (not gnus-show-threads)
10555             (> (prefix-numeric-value arg) 0)))
10556     (gnus-summary-prepare)
10557     (gnus-summary-goto-subject current)
10558     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10559     (gnus-summary-position-point)))
10560
10561 (defun gnus-summary-show-all-threads ()
10562   "Show all threads."
10563   (interactive)
10564   (save-excursion
10565     (let ((buffer-read-only nil))
10566       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10567   (gnus-summary-position-point))
10568
10569 (defun gnus-summary-show-thread ()
10570   "Show thread subtrees.
10571 Returns nil if no thread was there to be shown."
10572   (interactive)
10573   (let ((buffer-read-only nil)
10574         (orig (point))
10575         (end (point-at-eol))
10576         ;; Leave point at bol
10577         (beg (progn (beginning-of-line) (point))))
10578     (prog1
10579         ;; Any hidden lines here?
10580         (search-forward "\r" end t)
10581       (subst-char-in-region beg end ?\^M ?\n t)
10582       (goto-char orig)
10583       (gnus-summary-position-point))))
10584
10585 (defun gnus-summary-maybe-hide-threads ()
10586   "If requested, hide the threads that should be hidden."
10587   (when (and gnus-show-threads
10588              gnus-thread-hide-subtree)
10589     (gnus-summary-hide-all-threads
10590      (if (or (consp gnus-thread-hide-subtree)
10591              (functionp gnus-thread-hide-subtree))
10592          (gnus-make-predicate gnus-thread-hide-subtree)
10593        nil))))
10594
10595 ;;; Hiding predicates.
10596
10597 (defun gnus-article-unread-p (header)
10598   (memq (mail-header-number header) gnus-newsgroup-unreads))
10599
10600 (defun gnus-article-unseen-p (header)
10601   (memq (mail-header-number header) gnus-newsgroup-unseen))
10602
10603 (defun gnus-map-articles (predicate articles)
10604   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10605   (apply 'gnus-or (mapcar predicate
10606                           (mapcar 'gnus-summary-article-header articles))))
10607
10608 (defun gnus-summary-hide-all-threads (&optional predicate)
10609   "Hide all thread subtrees.
10610 If PREDICATE is supplied, threads that satisfy this predicate
10611 will not be hidden."
10612   (interactive)
10613   (save-excursion
10614     (goto-char (point-min))
10615     (let ((end nil))
10616       (while (not end)
10617         (when (or (not predicate)
10618                   (gnus-map-articles
10619                    predicate (gnus-summary-article-children)))
10620             (gnus-summary-hide-thread))
10621         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10622   (gnus-summary-position-point))
10623
10624 (defun gnus-summary-hide-thread ()
10625   "Hide thread subtrees.
10626 If PREDICATE is supplied, threads that satisfy this predicate
10627 will not be hidden.
10628 Returns nil if no threads were there to be hidden."
10629   (interactive)
10630   (let ((buffer-read-only nil)
10631         (start (point))
10632         (article (gnus-summary-article-number)))
10633     (goto-char start)
10634     ;; Go forward until either the buffer ends or the subthread
10635     ;; ends.
10636     (when (and (not (eobp))
10637                (or (zerop (gnus-summary-next-thread 1 t))
10638                    (goto-char (point-max))))
10639       (prog1
10640           (if (and (> (point) start)
10641                    (search-backward "\n" start t))
10642               (progn
10643                 (subst-char-in-region start (point) ?\n ?\^M)
10644                 (gnus-summary-goto-subject article))
10645             (goto-char start)
10646             nil)))))
10647
10648 (defun gnus-summary-go-to-next-thread (&optional previous)
10649   "Go to the same level (or less) next thread.
10650 If PREVIOUS is non-nil, go to previous thread instead.
10651 Return the article number moved to, or nil if moving was impossible."
10652   (let ((level (gnus-summary-thread-level))
10653         (way (if previous -1 1))
10654         (beg (point)))
10655     (forward-line way)
10656     (while (and (not (eobp))
10657                 (< level (gnus-summary-thread-level)))
10658       (forward-line way))
10659     (if (eobp)
10660         (progn
10661           (goto-char beg)
10662           nil)
10663       (setq beg (point))
10664       (prog1
10665           (gnus-summary-article-number)
10666         (goto-char beg)))))
10667
10668 (defun gnus-summary-next-thread (n &optional silent)
10669   "Go to the same level next N'th thread.
10670 If N is negative, search backward instead.
10671 Returns the difference between N and the number of skips actually
10672 done.
10673
10674 If SILENT, don't output messages."
10675   (interactive "p")
10676   (let ((backward (< n 0))
10677         (n (abs n)))
10678     (while (and (> n 0)
10679                 (gnus-summary-go-to-next-thread backward))
10680       (decf n))
10681     (unless silent
10682       (gnus-summary-position-point))
10683     (when (and (not silent) (/= 0 n))
10684       (gnus-message 7 "No more threads"))
10685     n))
10686
10687 (defun gnus-summary-prev-thread (n)
10688   "Go to the same level previous N'th thread.
10689 Returns the difference between N and the number of skips actually
10690 done."
10691   (interactive "p")
10692   (gnus-summary-next-thread (- n)))
10693
10694 (defun gnus-summary-go-down-thread ()
10695   "Go down one level in the current thread."
10696   (let ((children (gnus-summary-article-children)))
10697     (when children
10698       (gnus-summary-goto-subject (car children)))))
10699
10700 (defun gnus-summary-go-up-thread ()
10701   "Go up one level in the current thread."
10702   (let ((parent (gnus-summary-article-parent)))
10703     (when parent
10704       (gnus-summary-goto-subject parent))))
10705
10706 (defun gnus-summary-down-thread (n)
10707   "Go down thread N steps.
10708 If N is negative, go up instead.
10709 Returns the difference between N and how many steps down that were
10710 taken."
10711   (interactive "p")
10712   (let ((up (< n 0))
10713         (n (abs n)))
10714     (while (and (> n 0)
10715                 (if up (gnus-summary-go-up-thread)
10716                   (gnus-summary-go-down-thread)))
10717       (setq n (1- n)))
10718     (gnus-summary-position-point)
10719     (when (/= 0 n)
10720       (gnus-message 7 "Can't go further"))
10721     n))
10722
10723 (defun gnus-summary-up-thread (n)
10724   "Go up thread N steps.
10725 If N is negative, go down instead.
10726 Returns the difference between N and how many steps down that were
10727 taken."
10728   (interactive "p")
10729   (gnus-summary-down-thread (- n)))
10730
10731 (defun gnus-summary-top-thread ()
10732   "Go to the top of the thread."
10733   (interactive)
10734   (while (gnus-summary-go-up-thread))
10735   (gnus-summary-article-number))
10736
10737 (defun gnus-summary-kill-thread (&optional unmark)
10738   "Mark articles under current thread as read.
10739 If the prefix argument is positive, remove any kinds of marks.
10740 If the prefix argument is negative, tick articles instead."
10741   (interactive "P")
10742   (when unmark
10743     (setq unmark (prefix-numeric-value unmark)))
10744   (let ((articles (gnus-summary-articles-in-thread)))
10745     (save-excursion
10746       ;; Expand the thread.
10747       (gnus-summary-show-thread)
10748       ;; Mark all the articles.
10749       (while articles
10750         (gnus-summary-goto-subject (car articles))
10751         (cond ((null unmark)
10752                (gnus-summary-mark-article-as-read gnus-killed-mark))
10753               ((> unmark 0)
10754                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10755               (t
10756                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10757         (setq articles (cdr articles))))
10758     ;; Hide killed subtrees.
10759     (and (null unmark)
10760          gnus-thread-hide-killed
10761          (gnus-summary-hide-thread))
10762     ;; If marked as read, go to next unread subject.
10763     (when (null unmark)
10764       ;; Go to next unread subject.
10765       (gnus-summary-next-subject 1 t)))
10766   (gnus-set-mode-line 'summary))
10767
10768 ;; Summary sorting commands
10769
10770 (defun gnus-summary-sort-by-number (&optional reverse)
10771   "Sort the summary buffer by article number.
10772 Argument REVERSE means reverse order."
10773   (interactive "P")
10774   (gnus-summary-sort 'number reverse))
10775
10776 (defun gnus-summary-sort-by-random (&optional reverse)
10777   "Randomize the order in the summary buffer.
10778 Argument REVERSE means to randomize in reverse order."
10779   (interactive "P")
10780   (gnus-summary-sort 'random reverse))
10781
10782 (defun gnus-summary-sort-by-author (&optional reverse)
10783   "Sort the summary buffer by author name alphabetically.
10784 If `case-fold-search' is non-nil, case of letters is ignored.
10785 Argument REVERSE means reverse order."
10786   (interactive "P")
10787   (gnus-summary-sort 'author reverse))
10788
10789 (defun gnus-summary-sort-by-subject (&optional reverse)
10790   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10791 If `case-fold-search' is non-nil, case of letters is ignored.
10792 Argument REVERSE means reverse order."
10793   (interactive "P")
10794   (gnus-summary-sort 'subject reverse))
10795
10796 (defun gnus-summary-sort-by-date (&optional reverse)
10797   "Sort the summary buffer by date.
10798 Argument REVERSE means reverse order."
10799   (interactive "P")
10800   (gnus-summary-sort 'date reverse))
10801
10802 (defun gnus-summary-sort-by-score (&optional reverse)
10803   "Sort the summary buffer by score.
10804 Argument REVERSE means reverse order."
10805   (interactive "P")
10806   (gnus-summary-sort 'score reverse))
10807
10808 (defun gnus-summary-sort-by-lines (&optional reverse)
10809   "Sort the summary buffer by the number of lines.
10810 Argument REVERSE means reverse order."
10811   (interactive "P")
10812   (gnus-summary-sort 'lines reverse))
10813
10814 (defun gnus-summary-sort-by-chars (&optional reverse)
10815   "Sort the summary buffer by article length.
10816 Argument REVERSE means reverse order."
10817   (interactive "P")
10818   (gnus-summary-sort 'chars reverse))
10819
10820 (defun gnus-summary-sort-by-original (&optional reverse)
10821   "Sort the summary buffer using the default sorting method.
10822 Argument REVERSE means reverse order."
10823   (interactive "P")
10824   (let* ((buffer-read-only)
10825          (gnus-summary-prepare-hook nil))
10826     ;; We do the sorting by regenerating the threads.
10827     (gnus-summary-prepare)
10828     ;; Hide subthreads if needed.
10829     (gnus-summary-maybe-hide-threads)))
10830
10831 (defun gnus-summary-sort (predicate reverse)
10832   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10833   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10834          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10835          (gnus-thread-sort-functions
10836           (if (not reverse)
10837               thread
10838             `(lambda (t1 t2)
10839                (,thread t2 t1))))
10840          (gnus-sort-gathered-threads-function
10841           gnus-thread-sort-functions)
10842          (gnus-article-sort-functions
10843           (if (not reverse)
10844               article
10845             `(lambda (t1 t2)
10846                (,article t2 t1))))
10847          (buffer-read-only)
10848          (gnus-summary-prepare-hook nil))
10849     ;; We do the sorting by regenerating the threads.
10850     (gnus-summary-prepare)
10851     ;; Hide subthreads if needed.
10852     (gnus-summary-maybe-hide-threads)))
10853
10854 ;; Summary saving commands.
10855
10856 (defun gnus-summary-save-article (&optional n not-saved)
10857   "Save the current article using the default saver function.
10858 If N is a positive number, save the N next articles.
10859 If N is a negative number, save the N previous articles.
10860 If N is nil and any articles have been marked with the process mark,
10861 save those articles instead.
10862 The variable `gnus-default-article-saver' specifies the saver function."
10863   (interactive "P")
10864   (let* ((articles (gnus-summary-work-articles n))
10865          (save-buffer (save-excursion
10866                         (nnheader-set-temp-buffer " *Gnus Save*")))
10867          (num (length articles))
10868          header file)
10869     (dolist (article articles)
10870       (setq header (gnus-summary-article-header article))
10871       (if (not (vectorp header))
10872           ;; This is a pseudo-article.
10873           (if (assq 'name header)
10874               (gnus-copy-file (cdr (assq 'name header)))
10875             (gnus-message 1 "Article %d is unsaveable" article))
10876         ;; This is a real article.
10877         (save-window-excursion
10878           (let ((gnus-display-mime-function nil)
10879                 (gnus-article-prepare-hook nil))
10880             (gnus-summary-select-article t nil nil article)))
10881         (save-excursion
10882           (set-buffer save-buffer)
10883           (erase-buffer)
10884           (insert-buffer-substring gnus-original-article-buffer))
10885         (setq file (gnus-article-save save-buffer file num))
10886         (gnus-summary-remove-process-mark article)
10887         (unless not-saved
10888           (gnus-summary-set-saved-mark article))))
10889     (gnus-kill-buffer save-buffer)
10890     (gnus-summary-position-point)
10891     (gnus-set-mode-line 'summary)
10892     n))
10893
10894 (defun gnus-summary-pipe-output (&optional arg headers)
10895   "Pipe the current article to a subprocess.
10896 If N is a positive number, pipe the N next articles.
10897 If N is a negative number, pipe the N previous articles.
10898 If N is nil and any articles have been marked with the process mark,
10899 pipe those articles instead.
10900 If HEADERS (the symbolic prefix), include the headers, too."
10901   (interactive (gnus-interactive "P\ny"))
10902   (require 'gnus-art)
10903   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10904         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10905     (gnus-summary-save-article arg t))
10906   (let ((buffer (get-buffer "*Shell Command Output*")))
10907     (when (and buffer
10908                (not (zerop (buffer-size buffer))))
10909       (gnus-configure-windows 'pipe))))
10910
10911 (defun gnus-summary-save-article-mail (&optional arg)
10912   "Append the current article to an mail file.
10913 If N is a positive number, save the N next articles.
10914 If N is a negative number, save the N previous articles.
10915 If N is nil and any articles have been marked with the process mark,
10916 save those articles instead."
10917   (interactive "P")
10918   (require 'gnus-art)
10919   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10920     (gnus-summary-save-article arg)))
10921
10922 (defun gnus-summary-save-article-rmail (&optional arg)
10923   "Append the current article to an rmail file.
10924 If N is a positive number, save the N next articles.
10925 If N is a negative number, save the N previous articles.
10926 If N is nil and any articles have been marked with the process mark,
10927 save those articles instead."
10928   (interactive "P")
10929   (require 'gnus-art)
10930   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10931     (gnus-summary-save-article arg)))
10932
10933 (defun gnus-summary-save-article-file (&optional arg)
10934   "Append the current article to a file.
10935 If N is a positive number, save the N next articles.
10936 If N is a negative number, save the N previous articles.
10937 If N is nil and any articles have been marked with the process mark,
10938 save those articles instead."
10939   (interactive "P")
10940   (require 'gnus-art)
10941   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10942     (gnus-summary-save-article arg)))
10943
10944 (defun gnus-summary-write-article-file (&optional arg)
10945   "Write the current article to a file, deleting the previous file.
10946 If N is a positive number, save the N next articles.
10947 If N is a negative number, save the N previous articles.
10948 If N is nil and any articles have been marked with the process mark,
10949 save those articles instead."
10950   (interactive "P")
10951   (require 'gnus-art)
10952   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10953     (gnus-summary-save-article arg)))
10954
10955 (defun gnus-summary-save-article-body-file (&optional arg)
10956   "Append the current article body to a file.
10957 If N is a positive number, save the N next articles.
10958 If N is a negative number, save the N previous articles.
10959 If N is nil and any articles have been marked with the process mark,
10960 save those articles instead."
10961   (interactive "P")
10962   (require 'gnus-art)
10963   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10964     (gnus-summary-save-article arg)))
10965
10966 (defun gnus-summary-muttprint (&optional arg)
10967   "Print the current article using Muttprint.
10968 If N is a positive number, save the N next articles.
10969 If N is a negative number, save the N previous articles.
10970 If N is nil and any articles have been marked with the process mark,
10971 save those articles instead."
10972   (interactive "P")
10973   (require 'gnus-art)
10974   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10975     (gnus-summary-save-article arg t)))
10976
10977 (defun gnus-summary-pipe-message (program)
10978   "Pipe the current article through PROGRAM."
10979   (interactive "sProgram: ")
10980   (gnus-summary-select-article)
10981   (let ((mail-header-separator ""))
10982     (gnus-eval-in-buffer-window gnus-article-buffer
10983       (save-restriction
10984         (widen)
10985         (let ((start (window-start))
10986               buffer-read-only)
10987           (message-pipe-buffer-body program)
10988           (set-window-start (get-buffer-window (current-buffer)) start))))))
10989
10990 (defun gnus-get-split-value (methods)
10991   "Return a value based on the split METHODS."
10992   (let (split-name method result match)
10993     (when methods
10994       (save-excursion
10995         (set-buffer gnus-original-article-buffer)
10996         (save-restriction
10997           (nnheader-narrow-to-headers)
10998           (while (and methods (not split-name))
10999             (goto-char (point-min))
11000             (setq method (pop methods))
11001             (setq match (car method))
11002             (when (cond
11003                    ((stringp match)
11004                     ;; Regular expression.
11005                     (ignore-errors
11006                       (re-search-forward match nil t)))
11007                    ((functionp match)
11008                     ;; Function.
11009                     (save-restriction
11010                       (widen)
11011                       (setq result (funcall match gnus-newsgroup-name))))
11012                    ((consp match)
11013                     ;; Form.
11014                     (save-restriction
11015                       (widen)
11016                       (setq result (eval match)))))
11017               (setq split-name (cdr method))
11018               (cond ((stringp result)
11019                      (push (expand-file-name
11020                             result gnus-article-save-directory)
11021                            split-name))
11022                     ((consp result)
11023                      (setq split-name (append result split-name)))))))))
11024     (nreverse split-name)))
11025
11026 (defun gnus-valid-move-group-p (group)
11027   (and (boundp group)
11028        (symbol-name group)
11029        (symbol-value group)
11030        (gnus-get-function (gnus-find-method-for-group
11031                            (symbol-name group)) 'request-accept-article t)))
11032
11033 (defun gnus-read-move-group-name (prompt default articles prefix)
11034   "Read a group name."
11035   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11036          (minibuffer-confirm-incomplete nil) ; XEmacs
11037          (prom
11038           (format "%s %s to:"
11039                   prompt
11040                   (if (> (length articles) 1)
11041                       (format "these %d articles" (length articles))
11042                     "this article")))
11043          (to-newsgroup
11044           (cond
11045            ((null split-name)
11046             (gnus-completing-read-with-default
11047              default prom
11048              gnus-active-hashtb
11049              'gnus-valid-move-group-p
11050              nil prefix
11051              'gnus-group-history))
11052            ((= 1 (length split-name))
11053             (gnus-completing-read-with-default
11054              (car split-name) prom
11055              gnus-active-hashtb
11056              'gnus-valid-move-group-p
11057              nil nil
11058              'gnus-group-history))
11059            (t
11060             (gnus-completing-read-with-default
11061              nil prom
11062              (mapcar 'list (nreverse split-name))
11063              nil nil nil
11064              'gnus-group-history))))
11065          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11066     (when to-newsgroup
11067       (if (or (string= to-newsgroup "")
11068               (string= to-newsgroup prefix))
11069           (setq to-newsgroup default))
11070       (unless to-newsgroup
11071         (error "No group name entered"))
11072       (or (gnus-active to-newsgroup)
11073           (gnus-activate-group to-newsgroup nil nil to-method)
11074           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11075                                      to-newsgroup))
11076               (or (and (gnus-request-create-group to-newsgroup to-method)
11077                        (gnus-activate-group
11078                         to-newsgroup nil nil to-method)
11079                        (gnus-subscribe-group to-newsgroup))
11080                   (error "Couldn't create group %s" to-newsgroup)))
11081           (error "No such group: %s" to-newsgroup)))
11082     to-newsgroup))
11083
11084 (defun gnus-summary-save-parts (type dir n &optional reverse)
11085   "Save parts matching TYPE to DIR.
11086 If REVERSE, save parts that do not match TYPE."
11087   (interactive
11088    (list (read-string "Save parts of type: "
11089                       (or (car gnus-summary-save-parts-type-history)
11090                           gnus-summary-save-parts-default-mime)
11091                       'gnus-summary-save-parts-type-history)
11092          (setq gnus-summary-save-parts-last-directory
11093                (read-file-name "Save to directory: "
11094                                gnus-summary-save-parts-last-directory
11095                                nil t))
11096          current-prefix-arg))
11097   (gnus-summary-iterate n
11098     (let ((gnus-display-mime-function nil)
11099           (gnus-inhibit-treatment t))
11100       (gnus-summary-select-article))
11101     (save-excursion
11102       (set-buffer gnus-article-buffer)
11103       (let ((handles (or gnus-article-mime-handles
11104                          (mm-dissect-buffer nil gnus-article-loose-mime)
11105                          (and gnus-article-emulate-mime
11106                               (mm-uu-dissect)))))
11107         (when handles
11108           (gnus-summary-save-parts-1 type dir handles reverse)
11109           (unless gnus-article-mime-handles ;; Don't destroy this case.
11110             (mm-destroy-parts handles)))))))
11111
11112 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11113   (if (stringp (car handle))
11114       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11115               (cdr handle))
11116     (when (if reverse
11117               (not (string-match type (mm-handle-media-type handle)))
11118             (string-match type (mm-handle-media-type handle)))
11119       (let ((file (expand-file-name
11120                    (gnus-map-function
11121                     mm-file-name-rewrite-functions
11122                     (file-name-nondirectory
11123                      (or
11124                       (mail-content-type-get
11125                        (mm-handle-disposition handle) 'filename)
11126                       (mail-content-type-get
11127                        (mm-handle-type handle) 'name)
11128                       (concat gnus-newsgroup-name
11129                               "." (number-to-string
11130                                    (cdr gnus-article-current))))))
11131                    dir)))
11132         (unless (file-exists-p file)
11133           (mm-save-part-to-file handle file))))))
11134
11135 ;; Summary extract commands
11136
11137 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11138   (let ((buffer-read-only nil)
11139         (article (gnus-summary-article-number))
11140         after-article b e)
11141     (unless (gnus-summary-goto-subject article)
11142       (error "No such article: %d" article))
11143     (gnus-summary-position-point)
11144     ;; If all commands are to be bunched up on one line, we collect
11145     ;; them here.
11146     (unless gnus-view-pseudos-separately
11147       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11148             files action)
11149         (while ps
11150           (setq action (cdr (assq 'action (car ps))))
11151           (setq files (list (cdr (assq 'name (car ps)))))
11152           (while (and ps (cdr ps)
11153                       (string= (or action "1")
11154                                (or (cdr (assq 'action (cadr ps))) "2")))
11155             (push (cdr (assq 'name (cadr ps))) files)
11156             (setcdr ps (cddr ps)))
11157           (when files
11158             (when (not (string-match "%s" action))
11159               (push " " files))
11160             (push " " files)
11161             (when (assq 'execute (car ps))
11162               (setcdr (assq 'execute (car ps))
11163                       (funcall (if (string-match "%s" action)
11164                                    'format 'concat)
11165                                action
11166                                (mapconcat
11167                                 (lambda (f)
11168                                   (if (equal f " ")
11169                                       f
11170                                     (shell-quote-argument f)))
11171                                 files " ")))))
11172           (setq ps (cdr ps)))))
11173     (if (and gnus-view-pseudos (not not-view))
11174         (while pslist
11175           (when (assq 'execute (car pslist))
11176             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11177                                   (eq gnus-view-pseudos 'not-confirm)))
11178           (setq pslist (cdr pslist)))
11179       (save-excursion
11180         (while pslist
11181           (setq after-article (or (cdr (assq 'article (car pslist)))
11182                                   (gnus-summary-article-number)))
11183           (gnus-summary-goto-subject after-article)
11184           (forward-line 1)
11185           (setq b (point))
11186           (insert "    " (file-name-nondirectory
11187                           (cdr (assq 'name (car pslist))))
11188                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11189           (setq e (point))
11190           (forward-line -1)             ; back to `b'
11191           (gnus-add-text-properties
11192            b (1- e) (list 'gnus-number gnus-reffed-article-number
11193                           gnus-mouse-face-prop gnus-mouse-face))
11194           (gnus-data-enter
11195            after-article gnus-reffed-article-number
11196            gnus-unread-mark b (car pslist) 0 (- e b))
11197           (setq gnus-newsgroup-unreads
11198                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11199                                          gnus-reffed-article-number))
11200           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11201           (setq pslist (cdr pslist)))))))
11202
11203 (defun gnus-pseudos< (p1 p2)
11204   (let ((c1 (cdr (assq 'action p1)))
11205         (c2 (cdr (assq 'action p2))))
11206     (and c1 c2 (string< c1 c2))))
11207
11208 (defun gnus-request-pseudo-article (props)
11209   (cond ((assq 'execute props)
11210          (gnus-execute-command (cdr (assq 'execute props)))))
11211   (let ((gnus-current-article (gnus-summary-article-number)))
11212     (gnus-run-hooks 'gnus-mark-article-hook)))
11213
11214 (defun gnus-execute-command (command &optional automatic)
11215   (save-excursion
11216     (gnus-article-setup-buffer)
11217     (set-buffer gnus-article-buffer)
11218     (setq buffer-read-only nil)
11219     (let ((command (if automatic command
11220                      (read-string "Command: " (cons command 0)))))
11221       (erase-buffer)
11222       (insert "$ " command "\n\n")
11223       (if gnus-view-pseudo-asynchronously
11224           (start-process "gnus-execute" (current-buffer) shell-file-name
11225                          shell-command-switch command)
11226         (call-process shell-file-name nil t nil
11227                       shell-command-switch command)))))
11228
11229 ;; Summary kill commands.
11230
11231 (defun gnus-summary-edit-global-kill (article)
11232   "Edit the \"global\" kill file."
11233   (interactive (list (gnus-summary-article-number)))
11234   (gnus-group-edit-global-kill article))
11235
11236 (defun gnus-summary-edit-local-kill ()
11237   "Edit a local kill file applied to the current newsgroup."
11238   (interactive)
11239   (setq gnus-current-headers (gnus-summary-article-header))
11240   (gnus-group-edit-local-kill
11241    (gnus-summary-article-number) gnus-newsgroup-name))
11242
11243 ;;; Header reading.
11244
11245 (defun gnus-read-header (id &optional header)
11246   "Read the headers of article ID and enter them into the Gnus system."
11247   (let ((group gnus-newsgroup-name)
11248         (gnus-override-method
11249          (or
11250           gnus-override-method
11251           (and (gnus-news-group-p gnus-newsgroup-name)
11252                (car (gnus-refer-article-methods)))))
11253         where)
11254     ;; First we check to see whether the header in question is already
11255     ;; fetched.
11256     (if (stringp id)
11257         ;; This is a Message-ID.
11258         (setq header (or header (gnus-id-to-header id)))
11259       ;; This is an article number.
11260       (setq header (or header (gnus-summary-article-header id))))
11261     (if (and header
11262              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11263         ;; We have found the header.
11264         header
11265       ;; We have to really fetch the header to this article.
11266       (save-excursion
11267         (set-buffer nntp-server-buffer)
11268         (when (setq where (gnus-request-head id group))
11269           (nnheader-fold-continuation-lines)
11270           (goto-char (point-max))
11271           (insert ".\n")
11272           (goto-char (point-min))
11273           (insert "211 ")
11274           (princ (cond
11275                   ((numberp id) id)
11276                   ((cdr where) (cdr where))
11277                   (header (mail-header-number header))
11278                   (t gnus-reffed-article-number))
11279                  (current-buffer))
11280           (insert " Article retrieved.\n"))
11281         (if (or (not where)
11282                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11283             ()                          ; Malformed head.
11284           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11285             (when (and (stringp id)
11286                        (not (string= (gnus-group-real-name group)
11287                                      (car where))))
11288               ;; If we fetched by Message-ID and the article came
11289               ;; from a different group, we fudge some bogus article
11290               ;; numbers for this article.
11291               (mail-header-set-number header gnus-reffed-article-number))
11292             (save-excursion
11293               (set-buffer gnus-summary-buffer)
11294               (decf gnus-reffed-article-number)
11295               (gnus-remove-header (mail-header-number header))
11296               (push header gnus-newsgroup-headers)
11297               (setq gnus-current-headers header)
11298               (push (mail-header-number header) gnus-newsgroup-limit)))
11299           header)))))
11300
11301 (defun gnus-remove-header (number)
11302   "Remove header NUMBER from `gnus-newsgroup-headers'."
11303   (if (and gnus-newsgroup-headers
11304            (= number (mail-header-number (car gnus-newsgroup-headers))))
11305       (pop gnus-newsgroup-headers)
11306     (let ((headers gnus-newsgroup-headers))
11307       (while (and (cdr headers)
11308                   (not (= number (mail-header-number (cadr headers)))))
11309         (pop headers))
11310       (when (cdr headers)
11311         (setcdr headers (cddr headers))))))
11312
11313 ;;;
11314 ;;; summary highlights
11315 ;;;
11316
11317 (defun gnus-highlight-selected-summary ()
11318   "Highlight selected article in summary buffer."
11319   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11320   (when gnus-summary-selected-face
11321     (save-excursion
11322       (let* ((beg (point-at-bol))
11323              (end (point-at-eol))
11324              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11325              (from (if (get-text-property beg gnus-mouse-face-prop)
11326                        beg
11327                      (or (next-single-property-change
11328                           beg gnus-mouse-face-prop nil end)
11329                          beg)))
11330              (to
11331               (if (= from end)
11332                   (- from 2)
11333                 (or (next-single-property-change
11334                      from gnus-mouse-face-prop nil end)
11335                     end))))
11336         ;; If no mouse-face prop on line we will have to = from = end,
11337         ;; so we highlight the entire line instead.
11338         (when (= (+ to 2) from)
11339           (setq from beg)
11340           (setq to end))
11341         (if gnus-newsgroup-selected-overlay
11342             ;; Move old overlay.
11343             (gnus-move-overlay
11344              gnus-newsgroup-selected-overlay from to (current-buffer))
11345           ;; Create new overlay.
11346           (gnus-overlay-put
11347            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11348            'face gnus-summary-selected-face))))))
11349
11350 (defvar gnus-summary-highlight-line-cached nil)
11351 (defvar gnus-summary-highlight-line-trigger nil)
11352
11353 (defun gnus-summary-highlight-line-0 ()
11354   (if (and (eq gnus-summary-highlight-line-trigger
11355                gnus-summary-highlight)
11356            gnus-summary-highlight-line-cached)
11357       gnus-summary-highlight-line-cached
11358     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11359           gnus-summary-highlight-line-cached
11360           (let* ((cond (list 'cond))
11361                  (c cond)
11362                  (list gnus-summary-highlight))
11363             (while list
11364               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11365                               nil))
11366               (setq c (cdr c)
11367                     list (cdr list)))
11368             (gnus-byte-compile (list 'lambda nil cond))))))
11369
11370 (defun gnus-summary-highlight-line ()
11371   "Highlight current line according to `gnus-summary-highlight'."
11372   (let* ((beg (point-at-bol))
11373          (article (or (gnus-summary-article-number) gnus-current-article))
11374          (score (or (cdr (assq article
11375                                gnus-newsgroup-scored))
11376                     gnus-summary-default-score 0))
11377          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11378          (inhibit-read-only t)
11379          (default gnus-summary-default-score)
11380          (default-high gnus-summary-default-high-score)
11381          (default-low gnus-summary-default-low-score)
11382          (uncached (and gnus-summary-use-undownloaded-faces
11383                         (memq article gnus-newsgroup-undownloaded)
11384                         (not (memq article gnus-newsgroup-cached)))))
11385     (let ((face (funcall (gnus-summary-highlight-line-0))))
11386       (unless (eq face (get-text-property beg 'face))
11387         (gnus-put-text-property-excluding-characters-with-faces
11388          beg (point-at-eol) 'face
11389          (setq face (if (boundp face) (symbol-value face) face)))
11390         (when gnus-summary-highlight-line-function
11391           (funcall gnus-summary-highlight-line-function article face))))))
11392
11393 (defun gnus-update-read-articles (group unread &optional compute)
11394   "Update the list of read articles in GROUP.
11395 UNREAD is a sorted list."
11396   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11397         (info (gnus-get-info group))
11398         (prev 1)
11399         read)
11400     (if (or (not info) (not active))
11401         ;; There is no info on this group if it was, in fact,
11402         ;; killed.  Gnus stores no information on killed groups, so
11403         ;; there's nothing to be done.
11404         ;; One could store the information somewhere temporarily,
11405         ;; perhaps...  Hmmm...
11406         ()
11407       ;; Remove any negative articles numbers.
11408       (while (and unread (< (car unread) 0))
11409         (setq unread (cdr unread)))
11410       ;; Remove any expired article numbers
11411       (while (and unread (< (car unread) (car active)))
11412         (setq unread (cdr unread)))
11413       ;; Compute the ranges of read articles by looking at the list of
11414       ;; unread articles.
11415       (while unread
11416         (when (/= (car unread) prev)
11417           (push (if (= prev (1- (car unread))) prev
11418                   (cons prev (1- (car unread))))
11419                 read))
11420         (setq prev (1+ (car unread)))
11421         (setq unread (cdr unread)))
11422       (when (<= prev (cdr active))
11423         (push (cons prev (cdr active)) read))
11424       (setq read (if (> (length read) 1) (nreverse read) read))
11425       (if compute
11426           read
11427         (save-excursion
11428           (let (setmarkundo)
11429             ;; Propagate the read marks to the backend.
11430             (when (gnus-check-backend-function 'request-set-mark group)
11431               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11432                     (add (gnus-remove-from-range read (gnus-info-read info))))
11433                 (when (or add del)
11434                   (unless (gnus-check-group group)
11435                     (error "Can't open server for %s" group))
11436                   (gnus-request-set-mark
11437                    group (delq nil (list (if add (list add 'add '(read)))
11438                                          (if del (list del 'del '(read))))))
11439                   (setq setmarkundo
11440                         `(gnus-request-set-mark
11441                           ,group
11442                           ',(delq nil (list
11443                                        (if del (list del 'add '(read)))
11444                                        (if add (list add 'del '(read))))))))))
11445             (set-buffer gnus-group-buffer)
11446             (gnus-undo-register
11447               `(progn
11448                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11449                  (gnus-info-set-read ',info ',(gnus-info-read info))
11450                  (gnus-get-unread-articles-in-group ',info
11451                                                     (gnus-active ,group))
11452                  (gnus-group-update-group ,group t)
11453                  ,setmarkundo))))
11454         ;; Enter this list into the group info.
11455         (gnus-info-set-read info read)
11456         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11457         (gnus-get-unread-articles-in-group info (gnus-active group))
11458         t))))
11459
11460 (defun gnus-offer-save-summaries ()
11461   "Offer to save all active summary buffers."
11462   (let (buffers)
11463     ;; Go through all buffers and find all summaries.
11464     (dolist (buffer (buffer-list))
11465       (when (and (setq buffer (buffer-name buffer))
11466                  (string-match "Summary" buffer)
11467                  (with-current-buffer buffer
11468                    ;; We check that this is, indeed, a summary buffer.
11469                    (and (eq major-mode 'gnus-summary-mode)
11470                         ;; Also make sure this isn't bogus.
11471                         gnus-newsgroup-prepared
11472                         ;; Also make sure that this isn't a
11473                         ;; dead summary buffer.
11474                         (not gnus-dead-summary-mode))))
11475         (push buffer buffers)))
11476     ;; Go through all these summary buffers and offer to save them.
11477     (when buffers
11478       (save-excursion
11479         (map-y-or-n-p
11480          "Update summary buffer %s? "
11481          (lambda (buf)
11482            (switch-to-buffer buf)
11483            (gnus-summary-exit))
11484          buffers)))))
11485
11486 (defun gnus-summary-setup-default-charset ()
11487   "Setup newsgroup default charset."
11488   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11489       (setq gnus-newsgroup-charset nil)
11490     (let* ((ignored-charsets
11491             (or gnus-newsgroup-ephemeral-ignored-charsets
11492                 (append
11493                  (and gnus-newsgroup-name
11494                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11495                  gnus-newsgroup-ignored-charsets))))
11496       (setq gnus-newsgroup-charset
11497             (or gnus-newsgroup-ephemeral-charset
11498                 (and gnus-newsgroup-name
11499                      (gnus-parameter-charset gnus-newsgroup-name))
11500                 gnus-default-charset))
11501       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11502            ignored-charsets))))
11503
11504 ;;;
11505 ;;; Mime Commands
11506 ;;;
11507
11508 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11509   "Display the current article buffer fully MIME-buttonized.
11510 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11511 treated as multipart/mixed."
11512   (interactive "P")
11513   (require 'gnus-art)
11514   (let ((gnus-unbuttonized-mime-types nil)
11515         (gnus-mime-display-multipart-as-mixed show-all-parts))
11516     (gnus-summary-show-article)))
11517
11518 (defun gnus-summary-repair-multipart (article)
11519   "Add a Content-Type header to a multipart article without one."
11520   (interactive (list (gnus-summary-article-number)))
11521   (gnus-with-article article
11522     (message-narrow-to-head)
11523     (message-remove-header "Mime-Version")
11524     (goto-char (point-max))
11525     (insert "Mime-Version: 1.0\n")
11526     (widen)
11527     (when (search-forward "\n--" nil t)
11528       (let ((separator (buffer-substring (point) (point-at-eol))))
11529         (message-narrow-to-head)
11530         (message-remove-header "Content-Type")
11531         (goto-char (point-max))
11532         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11533                         separator))
11534         (widen))))
11535   (let (gnus-mark-article-hook)
11536     (gnus-summary-select-article t t nil article)))
11537
11538 (defun gnus-summary-toggle-display-buttonized ()
11539   "Toggle the buttonizing of the article buffer."
11540   (interactive)
11541   (require 'gnus-art)
11542   (if (setq gnus-inhibit-mime-unbuttonizing
11543             (not gnus-inhibit-mime-unbuttonizing))
11544       (let ((gnus-unbuttonized-mime-types nil))
11545         (gnus-summary-show-article))
11546     (gnus-summary-show-article)))
11547
11548 ;;;
11549 ;;; Generic summary marking commands
11550 ;;;
11551
11552 (defvar gnus-summary-marking-alist
11553   '((read gnus-del-mark "d")
11554     (unread gnus-unread-mark "u")
11555     (ticked gnus-ticked-mark "!")
11556     (dormant gnus-dormant-mark "?")
11557     (expirable gnus-expirable-mark "e"))
11558   "An alist of names/marks/keystrokes.")
11559
11560 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11561 (defvar gnus-summary-mark-map)
11562
11563 (defun gnus-summary-make-all-marking-commands ()
11564   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11565   (dolist (elem gnus-summary-marking-alist)
11566     (apply 'gnus-summary-make-marking-command elem)))
11567
11568 (defun gnus-summary-make-marking-command (name mark keystroke)
11569   (let ((map (make-sparse-keymap)))
11570     (define-key gnus-summary-generic-mark-map keystroke map)
11571     (dolist (lway `((next "next" next nil "n")
11572                     (next-unread "next unread" next t "N")
11573                     (prev "previous" prev nil "p")
11574                     (prev-unread "previous unread" prev t "P")
11575                     (nomove "" nil nil ,keystroke)))
11576       (let ((func (gnus-summary-make-marking-command-1
11577                    mark (car lway) lway name)))
11578         (setq func (eval func))
11579         (define-key map (nth 4 lway) func)))))
11580
11581 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11582   `(defun ,(intern
11583             (format "gnus-summary-put-mark-as-%s%s"
11584                     name (if (eq way 'nomove)
11585                              ""
11586                            (concat "-" (symbol-name way)))))
11587      (n)
11588      ,(format
11589        "Mark the current article as %s%s.
11590 If N, the prefix, then repeat N times.
11591 If N is negative, move in reverse order.
11592 The difference between N and the actual number of articles marked is
11593 returned."
11594        name (cadr lway))
11595      (interactive "p")
11596      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11597
11598 (defun gnus-summary-generic-mark (n mark move unread)
11599   "Mark N articles with MARK."
11600   (unless (eq major-mode 'gnus-summary-mode)
11601     (error "This command can only be used in the summary buffer"))
11602   (gnus-summary-show-thread)
11603   (let ((nummove
11604          (cond
11605           ((eq move 'next) 1)
11606           ((eq move 'prev) -1)
11607           (t 0))))
11608     (if (zerop nummove)
11609         (setq n 1)
11610       (when (< n 0)
11611         (setq n (abs n)
11612               nummove (* -1 nummove))))
11613     (while (and (> n 0)
11614                 (gnus-summary-mark-article nil mark)
11615                 (zerop (gnus-summary-next-subject nummove unread t)))
11616       (setq n (1- n)))
11617     (when (/= 0 n)
11618       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11619     (gnus-summary-recenter)
11620     (gnus-summary-position-point)
11621     (gnus-set-mode-line 'summary)
11622     n))
11623
11624 (defun gnus-summary-insert-articles (articles)
11625   (when (setq articles
11626               (gnus-sorted-difference articles
11627                                       (mapcar (lambda (h)
11628                                                 (mail-header-number h))
11629                                               gnus-newsgroup-headers)))
11630     (setq gnus-newsgroup-headers
11631           (gnus-merge 'list
11632                       gnus-newsgroup-headers
11633                       (gnus-fetch-headers articles)
11634                       'gnus-article-sort-by-number))
11635     ;; Suppress duplicates?
11636     (when gnus-suppress-duplicates
11637       (gnus-dup-suppress-articles))
11638
11639     ;; We might want to build some more threads first.
11640     (when (and gnus-fetch-old-headers
11641                (eq gnus-headers-retrieved-by 'nov))
11642       (if (eq gnus-fetch-old-headers 'invisible)
11643           (gnus-build-all-threads)
11644         (gnus-build-old-threads)))
11645     ;; Let the Gnus agent mark articles as read.
11646     (when gnus-agent
11647       (gnus-agent-get-undownloaded-list))
11648     ;; Remove list identifiers from subject
11649     (when gnus-list-identifiers
11650       (gnus-summary-remove-list-identifiers))
11651     ;; First and last article in this newsgroup.
11652     (when gnus-newsgroup-headers
11653       (setq gnus-newsgroup-begin
11654             (mail-header-number (car gnus-newsgroup-headers))
11655             gnus-newsgroup-end
11656             (mail-header-number
11657              (gnus-last-element gnus-newsgroup-headers))))
11658     (when gnus-use-scoring
11659       (gnus-possibly-score-headers))))
11660
11661 (defun gnus-summary-insert-old-articles (&optional all)
11662   "Insert all old articles in this group.
11663 If ALL is non-nil, already read articles become readable.
11664 If ALL is a number, fetch this number of articles."
11665   (interactive "P")
11666   (prog1
11667       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11668             older len)
11669         (setq older
11670               ;; Some nntp servers lie about their active range.  When
11671               ;; this happens, the active range can be in the millions.
11672               ;; Use a compressed range to avoid creating a huge list.
11673               (gnus-range-difference (list gnus-newsgroup-active) old))
11674         (setq len (gnus-range-length older))
11675         (cond
11676          ((null older) nil)
11677          ((numberp all)
11678           (if (< all len)
11679               (let ((older-range (nreverse older)))
11680                 (setq older nil)
11681
11682                 (while (> all 0)
11683                   (let* ((r (pop older-range))
11684                          (min (if (numberp r) r (car r)))
11685                          (max (if (numberp r) r (cdr r))))
11686                     (while (and (<= min max)
11687                                 (> all 0))
11688                       (push max older)
11689                       (setq all (1- all)
11690                             max (1- max))))))
11691             (setq older (gnus-uncompress-range older))))
11692          (all
11693           (setq older (gnus-uncompress-range older)))
11694          (t
11695           (when (and (numberp gnus-large-newsgroup)
11696                    (> len gnus-large-newsgroup))
11697               (let* ((cursor-in-echo-area nil)
11698                      (initial (gnus-parameter-large-newsgroup-initial
11699                                gnus-newsgroup-name))
11700                      (input
11701                       (read-string
11702                        (format
11703                         "How many articles from %s (%s %d): "
11704                         (gnus-limit-string
11705                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11706                         (if initial "max" "default")
11707                         len)
11708                        (if initial
11709                            (cons (number-to-string initial)
11710                                  0)))))
11711                 (unless (string-match "^[ \t]*$" input)
11712                   (setq all (string-to-number input))
11713                   (if (< all len)
11714                       (let ((older-range (nreverse older)))
11715                         (setq older nil)
11716
11717                         (while (> all 0)
11718                           (let* ((r (pop older-range))
11719                                  (min (if (numberp r) r (car r)))
11720                                  (max (if (numberp r) r (cdr r))))
11721                             (while (and (<= min max)
11722                                         (> all 0))
11723                               (push max older)
11724                               (setq all (1- all)
11725                                     max (1- max))))))))))
11726           (setq older (gnus-uncompress-range older))))
11727         (if (not older)
11728             (message "No old news.")
11729           (gnus-summary-insert-articles older)
11730           (gnus-summary-limit (gnus-sorted-nunion old older))))
11731     (gnus-summary-position-point)))
11732
11733 (defun gnus-summary-insert-new-articles ()
11734   "Insert all new articles in this group."
11735   (interactive)
11736   (prog1
11737       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11738             (old-active gnus-newsgroup-active)
11739             (nnmail-fetched-sources (list t))
11740             i new)
11741         (setq gnus-newsgroup-active
11742               (gnus-activate-group gnus-newsgroup-name 'scan))
11743         (setq i (cdr gnus-newsgroup-active))
11744         (while (> i (cdr old-active))
11745           (push i new)
11746           (decf i))
11747         (if (not new)
11748             (message "No gnus is bad news")
11749           (gnus-summary-insert-articles new)
11750           (setq gnus-newsgroup-unreads
11751                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11752           (gnus-summary-limit (gnus-sorted-nunion old new))))
11753     (gnus-summary-position-point)))
11754
11755 (gnus-summary-make-all-marking-commands)
11756
11757 (gnus-ems-redefine)
11758
11759 (provide 'gnus-sum)
11760
11761 (run-hooks 'gnus-sum-load-hook)
11762
11763 ;; Local Variables:
11764 ;; coding: iso-8859-1
11765 ;; End:
11766
11767 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11768 ;;; gnus-sum.el ends here