d5c6570401ca8ca194dbe25c8f637e99d7a719b3
[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
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 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols nil (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-thread-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228 (defvar gnus-summary-use-undownloaded-faces nil)
1229
1230 (defvar gnus-newsgroup-unreads nil
1231   "Sorted list of unread articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-unselected nil
1234   "Sorted list of unselected unread articles in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-reads nil
1237   "Alist of read articles and article marks in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-expunged-tally nil)
1240
1241 (defvar gnus-newsgroup-marked nil
1242   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1243
1244 (defvar gnus-newsgroup-spam-marked nil
1245   "List of ranges of articles that have been marked as spam.")
1246
1247 (defvar gnus-newsgroup-killed nil
1248   "List of ranges of articles that have been through the scoring process.")
1249
1250 (defvar gnus-newsgroup-cached nil
1251   "Sorted list of articles that come from the article cache.")
1252
1253 (defvar gnus-newsgroup-saved nil
1254   "List of articles that have been saved.")
1255
1256 (defvar gnus-newsgroup-kill-headers nil)
1257
1258 (defvar gnus-newsgroup-replied nil
1259   "List of articles that have been replied to in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-forwarded nil
1262   "List of articles that have been forwarded in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-recent nil
1265   "List of articles that have are recent in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-expirable nil
1268   "Sorted list of articles in the current newsgroup that can be expired.")
1269
1270 (defvar gnus-newsgroup-processable nil
1271   "List of articles in the current newsgroup that can be processed.")
1272
1273 (defvar gnus-newsgroup-downloadable nil
1274   "Sorted list of articles in the current newsgroup that can be processed.")
1275
1276 (defvar gnus-newsgroup-unfetched nil
1277   "Sorted list of articles in the current newsgroup whose headers have
1278 not been fetched into the agent.
1279
1280 This list will always be a subset of gnus-newsgroup-undownloaded.")
1281
1282 (defvar gnus-newsgroup-undownloaded nil
1283   "List of articles in the current newsgroup that haven't been downloaded.")
1284
1285 (defvar gnus-newsgroup-unsendable nil
1286   "List of articles in the current newsgroup that won't be sent.")
1287
1288 (defvar gnus-newsgroup-bookmarks nil
1289   "List of articles in the current newsgroup that have bookmarks.")
1290
1291 (defvar gnus-newsgroup-dormant nil
1292   "Sorted list of dormant articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unseen nil
1295   "List of unseen articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-seen nil
1298   "Range of seen articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-articles nil
1301   "List of articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-scored nil
1304   "List of scored articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-headers nil
1307   "List of article headers in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-threads nil)
1310
1311 (defvar gnus-newsgroup-prepared nil
1312   "Whether the current group has been prepared properly.")
1313
1314 (defvar gnus-newsgroup-ancient nil
1315   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-sparse nil)
1318
1319 (defvar gnus-current-article nil)
1320 (defvar gnus-article-current nil)
1321 (defvar gnus-current-headers nil)
1322 (defvar gnus-have-all-headers nil)
1323 (defvar gnus-last-article nil)
1324 (defvar gnus-newsgroup-history nil)
1325 (defvar gnus-newsgroup-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-charset nil)
1327 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1328
1329 (defvar gnus-article-before-search nil)
1330
1331 (defvar gnus-summary-local-variables
1332   '(gnus-newsgroup-name
1333     gnus-newsgroup-begin gnus-newsgroup-end
1334     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1335     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1336     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1337     gnus-newsgroup-unselected gnus-newsgroup-marked
1338     gnus-newsgroup-spam-marked
1339     gnus-newsgroup-reads gnus-newsgroup-saved
1340     gnus-newsgroup-replied gnus-newsgroup-forwarded
1341     gnus-newsgroup-recent
1342     gnus-newsgroup-expirable
1343     gnus-newsgroup-processable gnus-newsgroup-killed
1344     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1345     gnus-newsgroup-unfetched
1346     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1347     gnus-newsgroup-seen gnus-newsgroup-articles
1348     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1349     gnus-newsgroup-headers gnus-newsgroup-threads
1350     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1351     gnus-current-article gnus-current-headers gnus-have-all-headers
1352     gnus-last-article gnus-article-internal-prepare-hook
1353     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1354     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1355     gnus-thread-expunge-below
1356     gnus-score-alist gnus-current-score-file
1357     (gnus-summary-expunge-below . global)
1358     (gnus-summary-mark-below . global)
1359     (gnus-orphan-score . global)
1360     gnus-newsgroup-active gnus-scores-exclude-files
1361     gnus-newsgroup-history gnus-newsgroup-ancient
1362     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1363     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1364     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1365     (gnus-newsgroup-expunged-tally . 0)
1366     gnus-cache-removable-articles gnus-newsgroup-cached
1367     gnus-newsgroup-data gnus-newsgroup-data-reverse
1368     gnus-newsgroup-limit gnus-newsgroup-limits
1369     gnus-newsgroup-charset gnus-newsgroup-display
1370     gnus-summary-use-undownloaded-faces)
1371   "Variables that are buffer-local to the summary buffers.")
1372
1373 (defvar gnus-newsgroup-variables nil
1374   "A list of variables that have separate values in different newsgroups.
1375 A list of newsgroup (summary buffer) local variables, or cons of
1376 variables and their default values (when the default values are not
1377 nil), that should be made global while the summary buffer is active.
1378 These variables can be used to set variables in the group parameters
1379 while still allowing them to affect operations done in other
1380 buffers. For example:
1381
1382 \(setq gnus-newsgroup-variables
1383      '(message-use-followup-to
1384        (gnus-visible-headers .
1385          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1386 ")
1387
1388 ;; Byte-compiler warning.
1389 (eval-when-compile
1390   ;; Bind features so that require will believe that gnus-sum has
1391   ;; already been loaded (avoids infinite recursion)
1392   (let ((features (cons 'gnus-sum features)))
1393     ;; Several of the declarations in gnus-sum are needed to load the
1394     ;; following files. Right now, these definitions have been
1395     ;; compiled but not defined (evaluated).  We could either do a
1396     ;; eval-and-compile about all of the declarations or evaluate the
1397     ;; source file.
1398     (if (boundp 'gnus-newsgroup-variables)
1399         nil
1400       (load "gnus-sum.el" t t t))
1401     (require 'gnus)
1402     (require 'gnus-agent)
1403     (require 'gnus-art)))
1404
1405 ;; MIME stuff.
1406
1407 (defvar gnus-decode-encoded-word-methods
1408   '(mail-decode-encoded-word-string)
1409   "List of methods used to decode encoded words.
1410
1411 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1412 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1413 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1414 whose names match REGEXP.
1415
1416 For example:
1417 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1418  mail-decode-encoded-word-string
1419  (\"chinese\" . rfc1843-decode-string))")
1420
1421 (defvar gnus-decode-encoded-word-methods-cache nil)
1422
1423 (defun gnus-multi-decode-encoded-word-string (string)
1424   "Apply the functions from `gnus-encoded-word-methods' that match."
1425   (unless (and gnus-decode-encoded-word-methods-cache
1426                (eq gnus-newsgroup-name
1427                    (car gnus-decode-encoded-word-methods-cache)))
1428     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1429     (mapcar (lambda (x)
1430               (if (symbolp x)
1431                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1432                 (if (and gnus-newsgroup-name
1433                          (string-match (car x) gnus-newsgroup-name))
1434                     (nconc gnus-decode-encoded-word-methods-cache
1435                            (list (cdr x))))))
1436             gnus-decode-encoded-word-methods))
1437   (let ((xlist gnus-decode-encoded-word-methods-cache))
1438     (pop xlist)
1439     (while xlist
1440       (setq string (funcall (pop xlist) string))))
1441   string)
1442
1443 ;; Subject simplification.
1444
1445 (defun gnus-simplify-whitespace (str)
1446   "Remove excessive whitespace from STR."
1447   ;; Multiple spaces.
1448   (while (string-match "[ \t][ \t]+" str)
1449     (setq str (concat (substring str 0 (match-beginning 0))
1450                         " "
1451                         (substring str (match-end 0)))))
1452   ;; Leading spaces.
1453   (when (string-match "^[ \t]+" str)
1454     (setq str (substring str (match-end 0))))
1455   ;; Trailing spaces.
1456   (when (string-match "[ \t]+$" str)
1457     (setq str (substring str 0 (match-beginning 0))))
1458   str)
1459
1460 (defun gnus-simplify-all-whitespace (str)
1461   "Remove all whitespace from STR."
1462   (while (string-match "[ \t\n]+" str)
1463     (setq str (replace-match "" nil nil str)))
1464   str)
1465
1466 (defsubst gnus-simplify-subject-re (subject)
1467   "Remove \"Re:\" from subject lines."
1468   (if (string-match message-subject-re-regexp subject)
1469       (substring subject (match-end 0))
1470     subject))
1471
1472 (defun gnus-simplify-subject (subject &optional re-only)
1473   "Remove `Re:' and words in parentheses.
1474 If RE-ONLY is non-nil, strip leading `Re:'s only."
1475   (let ((case-fold-search t))           ;Ignore case.
1476     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1477     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1478       (setq subject (substring subject (match-end 0))))
1479     ;; Remove uninteresting prefixes.
1480     (when (and (not re-only)
1481                gnus-simplify-ignored-prefixes
1482                (string-match gnus-simplify-ignored-prefixes subject))
1483       (setq subject (substring subject (match-end 0))))
1484     ;; Remove words in parentheses from end.
1485     (unless re-only
1486       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1487         (setq subject (substring subject 0 (match-beginning 0)))))
1488     ;; Return subject string.
1489     subject))
1490
1491 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1492 ;; all whitespace.
1493 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1494   (goto-char (point-min))
1495   (while (re-search-forward regexp nil t)
1496     (replace-match (or newtext ""))))
1497
1498 (defun gnus-simplify-buffer-fuzzy ()
1499   "Simplify string in the buffer fuzzily.
1500 The string in the accessible portion of the current buffer is simplified.
1501 It is assumed to be a single-line subject.
1502 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1503 matter is removed.  Additional things can be deleted by setting
1504 `gnus-simplify-subject-fuzzy-regexp'."
1505   (let ((case-fold-search t)
1506         (modified-tick))
1507     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1508
1509     (while (not (eq modified-tick (buffer-modified-tick)))
1510       (setq modified-tick (buffer-modified-tick))
1511       (cond
1512        ((listp gnus-simplify-subject-fuzzy-regexp)
1513         (mapcar 'gnus-simplify-buffer-fuzzy-step
1514                 gnus-simplify-subject-fuzzy-regexp))
1515        (gnus-simplify-subject-fuzzy-regexp
1516         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1517       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1518       (gnus-simplify-buffer-fuzzy-step
1519        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1520       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1521
1522     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1523     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1524     (gnus-simplify-buffer-fuzzy-step " $")
1525     (gnus-simplify-buffer-fuzzy-step "^ +")))
1526
1527 (defun gnus-simplify-subject-fuzzy (subject)
1528   "Simplify a subject string fuzzily.
1529 See `gnus-simplify-buffer-fuzzy' for details."
1530   (save-excursion
1531     (gnus-set-work-buffer)
1532     (let ((case-fold-search t))
1533       ;; Remove uninteresting prefixes.
1534       (when (and gnus-simplify-ignored-prefixes
1535                  (string-match gnus-simplify-ignored-prefixes subject))
1536         (setq subject (substring subject (match-end 0))))
1537       (insert subject)
1538       (inline (gnus-simplify-buffer-fuzzy))
1539       (buffer-string))))
1540
1541 (defsubst gnus-simplify-subject-fully (subject)
1542   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1543   (cond
1544    (gnus-simplify-subject-functions
1545     (gnus-map-function gnus-simplify-subject-functions subject))
1546    ((null gnus-summary-gather-subject-limit)
1547     (gnus-simplify-subject-re subject))
1548    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1549     (gnus-simplify-subject-fuzzy subject))
1550    ((numberp gnus-summary-gather-subject-limit)
1551     (gnus-limit-string (gnus-simplify-subject-re subject)
1552                        gnus-summary-gather-subject-limit))
1553    (t
1554     subject)))
1555
1556 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1557   "Check whether two subjects are equal.
1558 If optional argument SIMPLE-FIRST is t, first argument is already
1559 simplified."
1560   (cond
1561    ((null simple-first)
1562     (equal (gnus-simplify-subject-fully s1)
1563            (gnus-simplify-subject-fully s2)))
1564    (t
1565     (equal s1
1566            (gnus-simplify-subject-fully s2)))))
1567
1568 (defun gnus-summary-bubble-group ()
1569   "Increase the score of the current group.
1570 This is a handy function to add to `gnus-summary-exit-hook' to
1571 increase the score of each group you read."
1572   (gnus-group-add-score gnus-newsgroup-name))
1573
1574 \f
1575 ;;;
1576 ;;; Gnus summary mode
1577 ;;;
1578
1579 (put 'gnus-summary-mode 'mode-class 'special)
1580
1581 (defvar gnus-article-commands-menu)
1582
1583 ;; Non-orthogonal keys
1584
1585 (gnus-define-keys gnus-summary-mode-map
1586   " " gnus-summary-next-page
1587   "\177" gnus-summary-prev-page
1588   [delete] gnus-summary-prev-page
1589   [backspace] gnus-summary-prev-page
1590   "\r" gnus-summary-scroll-up
1591   "\M-\r" gnus-summary-scroll-down
1592   "n" gnus-summary-next-unread-article
1593   "p" gnus-summary-prev-unread-article
1594   "N" gnus-summary-next-article
1595   "P" gnus-summary-prev-article
1596   "\M-\C-n" gnus-summary-next-same-subject
1597   "\M-\C-p" gnus-summary-prev-same-subject
1598   "\M-n" gnus-summary-next-unread-subject
1599   "\M-p" gnus-summary-prev-unread-subject
1600   "." gnus-summary-first-unread-article
1601   "," gnus-summary-best-unread-article
1602   "\M-s" gnus-summary-search-article-forward
1603   "\M-r" gnus-summary-search-article-backward
1604   "<" gnus-summary-beginning-of-article
1605   ">" gnus-summary-end-of-article
1606   "j" gnus-summary-goto-article
1607   "^" gnus-summary-refer-parent-article
1608   "\M-^" gnus-summary-refer-article
1609   "u" gnus-summary-tick-article-forward
1610   "!" gnus-summary-tick-article-forward
1611   "U" gnus-summary-tick-article-backward
1612   "d" gnus-summary-mark-as-read-forward
1613   "D" gnus-summary-mark-as-read-backward
1614   "E" gnus-summary-mark-as-expirable
1615   "\M-u" gnus-summary-clear-mark-forward
1616   "\M-U" gnus-summary-clear-mark-backward
1617   "k" gnus-summary-kill-same-subject-and-select
1618   "\C-k" gnus-summary-kill-same-subject
1619   "\M-\C-k" gnus-summary-kill-thread
1620   "\M-\C-l" gnus-summary-lower-thread
1621   "e" gnus-summary-edit-article
1622   "#" gnus-summary-mark-as-processable
1623   "\M-#" gnus-summary-unmark-as-processable
1624   "\M-\C-t" gnus-summary-toggle-threads
1625   "\M-\C-s" gnus-summary-show-thread
1626   "\M-\C-h" gnus-summary-hide-thread
1627   "\M-\C-f" gnus-summary-next-thread
1628   "\M-\C-b" gnus-summary-prev-thread
1629   [(meta down)] gnus-summary-next-thread
1630   [(meta up)] gnus-summary-prev-thread
1631   "\M-\C-u" gnus-summary-up-thread
1632   "\M-\C-d" gnus-summary-down-thread
1633   "&" gnus-summary-execute-command
1634   "c" gnus-summary-catchup-and-exit
1635   "\C-w" gnus-summary-mark-region-as-read
1636   "\C-t" gnus-summary-toggle-truncation
1637   "?" gnus-summary-mark-as-dormant
1638   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1639   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1640   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1641   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1642   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1643   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1644   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1645   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1646   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1647   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1648   "=" gnus-summary-expand-window
1649   "\C-x\C-s" gnus-summary-reselect-current-group
1650   "\M-g" gnus-summary-rescan-group
1651   "w" gnus-summary-stop-page-breaking
1652   "\C-c\C-r" gnus-summary-caesar-message
1653   "f" gnus-summary-followup
1654   "F" gnus-summary-followup-with-original
1655   "C" gnus-summary-cancel-article
1656   "r" gnus-summary-reply
1657   "R" gnus-summary-reply-with-original
1658   "\C-c\C-f" gnus-summary-mail-forward
1659   "o" gnus-summary-save-article
1660   "\C-o" gnus-summary-save-article-mail
1661   "|" gnus-summary-pipe-output
1662   "\M-k" gnus-summary-edit-local-kill
1663   "\M-K" gnus-summary-edit-global-kill
1664   ;; "V" gnus-version
1665   "\C-c\C-d" gnus-summary-describe-group
1666   "q" gnus-summary-exit
1667   "Q" gnus-summary-exit-no-update
1668   "\C-c\C-i" gnus-info-find-node
1669   gnus-mouse-2 gnus-mouse-pick-article
1670   "m" gnus-summary-mail-other-window
1671   "a" gnus-summary-post-news
1672   "i" gnus-summary-news-other-window
1673   "x" gnus-summary-limit-to-unread
1674   "s" gnus-summary-isearch-article
1675   "t" gnus-summary-toggle-header
1676   "g" gnus-summary-show-article
1677   "l" gnus-summary-goto-last-article
1678   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1679   "\C-d" gnus-summary-enter-digest-group
1680   "\M-\C-d" gnus-summary-read-document
1681   "\M-\C-e" gnus-summary-edit-parameters
1682   "\M-\C-a" gnus-summary-customize-parameters
1683   "\C-c\C-b" gnus-bug
1684   "*" gnus-cache-enter-article
1685   "\M-*" gnus-cache-remove-article
1686   "\M-&" gnus-summary-universal-argument
1687   "\C-l" gnus-recenter
1688   "I" gnus-summary-increase-score
1689   "L" gnus-summary-lower-score
1690   "\M-i" gnus-symbolic-argument
1691   "h" gnus-summary-select-article-buffer
1692
1693   "b" gnus-article-view-part
1694   "\M-t" gnus-summary-toggle-display-buttonized
1695
1696   "V" gnus-summary-score-map
1697   "X" gnus-uu-extract-map
1698   "S" gnus-summary-send-map)
1699
1700 ;; Sort of orthogonal keymap
1701 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1702   "t" gnus-summary-tick-article-forward
1703   "!" gnus-summary-tick-article-forward
1704   "d" gnus-summary-mark-as-read-forward
1705   "r" gnus-summary-mark-as-read-forward
1706   "c" gnus-summary-clear-mark-forward
1707   " " gnus-summary-clear-mark-forward
1708   "e" gnus-summary-mark-as-expirable
1709   "x" gnus-summary-mark-as-expirable
1710   "?" gnus-summary-mark-as-dormant
1711   "b" gnus-summary-set-bookmark
1712   "B" gnus-summary-remove-bookmark
1713   "#" gnus-summary-mark-as-processable
1714   "\M-#" gnus-summary-unmark-as-processable
1715   "S" gnus-summary-limit-include-expunged
1716   "C" gnus-summary-catchup
1717   "H" gnus-summary-catchup-to-here
1718   "h" gnus-summary-catchup-from-here
1719   "\C-c" gnus-summary-catchup-all
1720   "k" gnus-summary-kill-same-subject-and-select
1721   "K" gnus-summary-kill-same-subject
1722   "P" gnus-uu-mark-map)
1723
1724 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1725   "c" gnus-summary-clear-above
1726   "u" gnus-summary-tick-above
1727   "m" gnus-summary-mark-above
1728   "k" gnus-summary-kill-below)
1729
1730 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1731   "/" gnus-summary-limit-to-subject
1732   "n" gnus-summary-limit-to-articles
1733   "w" gnus-summary-pop-limit
1734   "s" gnus-summary-limit-to-subject
1735   "a" gnus-summary-limit-to-author
1736   "u" gnus-summary-limit-to-unread
1737   "m" gnus-summary-limit-to-marks
1738   "M" gnus-summary-limit-exclude-marks
1739   "v" gnus-summary-limit-to-score
1740   "*" gnus-summary-limit-include-cached
1741   "D" gnus-summary-limit-include-dormant
1742   "T" gnus-summary-limit-include-thread
1743   "d" gnus-summary-limit-exclude-dormant
1744   "t" gnus-summary-limit-to-age
1745   "." gnus-summary-limit-to-unseen
1746   "x" gnus-summary-limit-to-extra
1747   "p" gnus-summary-limit-to-display-predicate
1748   "E" gnus-summary-limit-include-expunged
1749   "c" gnus-summary-limit-exclude-childless-dormant
1750   "C" gnus-summary-limit-mark-excluded-as-read
1751   "o" gnus-summary-insert-old-articles
1752   "N" gnus-summary-insert-new-articles)
1753
1754 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1755   "n" gnus-summary-next-unread-article
1756   "p" gnus-summary-prev-unread-article
1757   "N" gnus-summary-next-article
1758   "P" gnus-summary-prev-article
1759   "\C-n" gnus-summary-next-same-subject
1760   "\C-p" gnus-summary-prev-same-subject
1761   "\M-n" gnus-summary-next-unread-subject
1762   "\M-p" gnus-summary-prev-unread-subject
1763   "f" gnus-summary-first-unread-article
1764   "b" gnus-summary-best-unread-article
1765   "j" gnus-summary-goto-article
1766   "g" gnus-summary-goto-subject
1767   "l" gnus-summary-goto-last-article
1768   "o" gnus-summary-pop-article)
1769
1770 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1771   "k" gnus-summary-kill-thread
1772   "l" gnus-summary-lower-thread
1773   "i" gnus-summary-raise-thread
1774   "T" gnus-summary-toggle-threads
1775   "t" gnus-summary-rethread-current
1776   "^" gnus-summary-reparent-thread
1777   "s" gnus-summary-show-thread
1778   "S" gnus-summary-show-all-threads
1779   "h" gnus-summary-hide-thread
1780   "H" gnus-summary-hide-all-threads
1781   "n" gnus-summary-next-thread
1782   "p" gnus-summary-prev-thread
1783   "u" gnus-summary-up-thread
1784   "o" gnus-summary-top-thread
1785   "d" gnus-summary-down-thread
1786   "#" gnus-uu-mark-thread
1787   "\M-#" gnus-uu-unmark-thread)
1788
1789 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1790   "g" gnus-summary-prepare
1791   "c" gnus-summary-insert-cached-articles
1792   "d" gnus-summary-insert-dormant-articles)
1793
1794 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1795   "c" gnus-summary-catchup-and-exit
1796   "C" gnus-summary-catchup-all-and-exit
1797   "E" gnus-summary-exit-no-update
1798   "Q" gnus-summary-exit
1799   "Z" gnus-summary-exit
1800   "n" gnus-summary-catchup-and-goto-next-group
1801   "R" gnus-summary-reselect-current-group
1802   "G" gnus-summary-rescan-group
1803   "N" gnus-summary-next-group
1804   "s" gnus-summary-save-newsrc
1805   "P" gnus-summary-prev-group)
1806
1807 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1808   " " gnus-summary-next-page
1809   "n" gnus-summary-next-page
1810   "\177" gnus-summary-prev-page
1811   [delete] gnus-summary-prev-page
1812   "p" gnus-summary-prev-page
1813   "\r" gnus-summary-scroll-up
1814   "\M-\r" gnus-summary-scroll-down
1815   "<" gnus-summary-beginning-of-article
1816   ">" gnus-summary-end-of-article
1817   "b" gnus-summary-beginning-of-article
1818   "e" gnus-summary-end-of-article
1819   "^" gnus-summary-refer-parent-article
1820   "r" gnus-summary-refer-parent-article
1821   "D" gnus-summary-enter-digest-group
1822   "R" gnus-summary-refer-references
1823   "T" gnus-summary-refer-thread
1824   "g" gnus-summary-show-article
1825   "s" gnus-summary-isearch-article
1826   "P" gnus-summary-print-article
1827   "M" gnus-mailing-list-insinuate
1828   "t" gnus-article-babel)
1829
1830 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1831   "b" gnus-article-add-buttons
1832   "B" gnus-article-add-buttons-to-head
1833   "o" gnus-article-treat-overstrike
1834   "e" gnus-article-emphasize
1835   "w" gnus-article-fill-cited-article
1836   "Q" gnus-article-fill-long-lines
1837   "C" gnus-article-capitalize-sentences
1838   "c" gnus-article-remove-cr
1839   "q" gnus-article-de-quoted-unreadable
1840   "6" gnus-article-de-base64-unreadable
1841   "Z" gnus-article-decode-HZ
1842   "h" gnus-article-wash-html
1843   "u" gnus-article-unsplit-urls
1844   "s" gnus-summary-force-verify-and-decrypt
1845   "f" gnus-article-display-x-face
1846   "l" gnus-summary-stop-page-breaking
1847   "r" gnus-summary-caesar-message
1848   "m" gnus-summary-morse-message
1849   "t" gnus-summary-toggle-header
1850   "g" gnus-treat-smiley
1851   "v" gnus-summary-verbose-headers
1852   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1853   "p" gnus-article-verify-x-pgp-sig
1854   "d" gnus-article-treat-dumbquotes)
1855
1856 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1857   ;; mnemonic: deuglif*Y*
1858   "u" gnus-article-outlook-unwrap-lines
1859   "a" gnus-article-outlook-repair-attribution
1860   "c" gnus-article-outlook-rearrange-citation
1861   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1862
1863 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1864   "a" gnus-article-hide
1865   "h" gnus-article-hide-headers
1866   "b" gnus-article-hide-boring-headers
1867   "s" gnus-article-hide-signature
1868   "c" gnus-article-hide-citation
1869   "C" gnus-article-hide-citation-in-followups
1870   "l" gnus-article-hide-list-identifiers
1871   "B" gnus-article-strip-banner
1872   "P" gnus-article-hide-pem
1873   "\C-c" gnus-article-hide-citation-maybe)
1874
1875 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1876   "a" gnus-article-highlight
1877   "h" gnus-article-highlight-headers
1878   "c" gnus-article-highlight-citation
1879   "s" gnus-article-highlight-signature)
1880
1881 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1882   "f" gnus-article-treat-fold-headers
1883   "u" gnus-article-treat-unfold-headers
1884   "n" gnus-article-treat-fold-newsgroups)
1885
1886 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1887   "x" gnus-article-display-x-face
1888   "d" gnus-article-display-face
1889   "s" gnus-treat-smiley
1890   "D" gnus-article-remove-images
1891   "f" gnus-treat-from-picon
1892   "m" gnus-treat-mail-picon
1893   "n" gnus-treat-newsgroups-picon)
1894
1895 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1896   "w" gnus-article-decode-mime-words
1897   "c" gnus-article-decode-charset
1898   "v" gnus-mime-view-all-parts
1899   "b" gnus-article-view-part)
1900
1901 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1902   "z" gnus-article-date-ut
1903   "u" gnus-article-date-ut
1904   "l" gnus-article-date-local
1905   "p" gnus-article-date-english
1906   "e" gnus-article-date-lapsed
1907   "o" gnus-article-date-original
1908   "i" gnus-article-date-iso8601
1909   "s" gnus-article-date-user)
1910
1911 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1912   "t" gnus-article-remove-trailing-blank-lines
1913   "l" gnus-article-strip-leading-blank-lines
1914   "m" gnus-article-strip-multiple-blank-lines
1915   "a" gnus-article-strip-blank-lines
1916   "A" gnus-article-strip-all-blank-lines
1917   "s" gnus-article-strip-leading-space
1918   "e" gnus-article-strip-trailing-space
1919   "w" gnus-article-remove-leading-whitespace)
1920
1921 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1922   "v" gnus-version
1923   "f" gnus-summary-fetch-faq
1924   "d" gnus-summary-describe-group
1925   "h" gnus-summary-describe-briefly
1926   "i" gnus-info-find-node
1927   "c" gnus-group-fetch-charter
1928   "C" gnus-group-fetch-control)
1929
1930 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1931   "e" gnus-summary-expire-articles
1932   "\M-\C-e" gnus-summary-expire-articles-now
1933   "\177" gnus-summary-delete-article
1934   [delete] gnus-summary-delete-article
1935   [backspace] gnus-summary-delete-article
1936   "m" gnus-summary-move-article
1937   "r" gnus-summary-respool-article
1938   "w" gnus-summary-edit-article
1939   "c" gnus-summary-copy-article
1940   "B" gnus-summary-crosspost-article
1941   "q" gnus-summary-respool-query
1942   "t" gnus-summary-respool-trace
1943   "i" gnus-summary-import-article
1944   "I" gnus-summary-create-article
1945   "p" gnus-summary-article-posted-p)
1946
1947 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1948   "o" gnus-summary-save-article
1949   "m" gnus-summary-save-article-mail
1950   "F" gnus-summary-write-article-file
1951   "r" gnus-summary-save-article-rmail
1952   "f" gnus-summary-save-article-file
1953   "b" gnus-summary-save-article-body-file
1954   "h" gnus-summary-save-article-folder
1955   "v" gnus-summary-save-article-vm
1956   "p" gnus-summary-pipe-output
1957   "P" gnus-summary-muttprint
1958   "s" gnus-soup-add-article)
1959
1960 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1961   "b" gnus-summary-display-buttonized
1962   "m" gnus-summary-repair-multipart
1963   "v" gnus-article-view-part
1964   "o" gnus-article-save-part
1965   "c" gnus-article-copy-part
1966   "C" gnus-article-view-part-as-charset
1967   "e" gnus-article-view-part-externally
1968   "E" gnus-article-encrypt-body
1969   "i" gnus-article-inline-part
1970   "|" gnus-article-pipe-part)
1971
1972 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1973   "p" gnus-summary-mark-as-processable
1974   "u" gnus-summary-unmark-as-processable
1975   "U" gnus-summary-unmark-all-processable
1976   "v" gnus-uu-mark-over
1977   "s" gnus-uu-mark-series
1978   "r" gnus-uu-mark-region
1979   "g" gnus-uu-unmark-region
1980   "R" gnus-uu-mark-by-regexp
1981   "G" gnus-uu-unmark-by-regexp
1982   "t" gnus-uu-mark-thread
1983   "T" gnus-uu-unmark-thread
1984   "a" gnus-uu-mark-all
1985   "b" gnus-uu-mark-buffer
1986   "S" gnus-uu-mark-sparse
1987   "k" gnus-summary-kill-process-mark
1988   "y" gnus-summary-yank-process-mark
1989   "w" gnus-summary-save-process-mark
1990   "i" gnus-uu-invert-processable)
1991
1992 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1993   ;;"x" gnus-uu-extract-any
1994   "m" gnus-summary-save-parts
1995   "u" gnus-uu-decode-uu
1996   "U" gnus-uu-decode-uu-and-save
1997   "s" gnus-uu-decode-unshar
1998   "S" gnus-uu-decode-unshar-and-save
1999   "o" gnus-uu-decode-save
2000   "O" gnus-uu-decode-save
2001   "b" gnus-uu-decode-binhex
2002   "B" gnus-uu-decode-binhex
2003   "p" gnus-uu-decode-postscript
2004   "P" gnus-uu-decode-postscript-and-save)
2005
2006 (gnus-define-keys
2007     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2008   "u" gnus-uu-decode-uu-view
2009   "U" gnus-uu-decode-uu-and-save-view
2010   "s" gnus-uu-decode-unshar-view
2011   "S" gnus-uu-decode-unshar-and-save-view
2012   "o" gnus-uu-decode-save-view
2013   "O" gnus-uu-decode-save-view
2014   "b" gnus-uu-decode-binhex-view
2015   "B" gnus-uu-decode-binhex-view
2016   "p" gnus-uu-decode-postscript-view
2017   "P" gnus-uu-decode-postscript-and-save-view)
2018
2019 (defvar gnus-article-post-menu nil)
2020
2021 (defconst gnus-summary-menu-maxlen 20)
2022
2023 (defun gnus-summary-menu-split (menu)
2024   ;; If we have lots of elements, divide them into groups of 20
2025   ;; and make a pane (or submenu) for each one.
2026   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2027       (let ((menu menu) sublists next
2028             (i 1))
2029         (while menu
2030           ;; Pull off the next gnus-summary-menu-maxlen elements
2031           ;; and make them the next element of sublist.
2032           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2033           (if next
2034               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2035                       nil))
2036           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2037                                              (aref (car (last menu)) 0)) menu)
2038                                sublists))
2039           (setq i (1+ i))
2040           (setq menu next))
2041         (nreverse sublists))
2042     ;; Few elements--put them all in one pane.
2043     menu))
2044
2045 (defun gnus-summary-make-menu-bar ()
2046   (gnus-turn-off-edit-menu 'summary)
2047
2048   (unless (boundp 'gnus-summary-misc-menu)
2049
2050     (easy-menu-define
2051       gnus-summary-kill-menu gnus-summary-mode-map ""
2052       (cons
2053        "Score"
2054        (nconc
2055         (list
2056          ["Customize" gnus-score-customize t])
2057         (gnus-make-score-map 'increase)
2058         (gnus-make-score-map 'lower)
2059         '(("Mark"
2060            ["Kill below" gnus-summary-kill-below t]
2061            ["Mark above" gnus-summary-mark-above t]
2062            ["Tick above" gnus-summary-tick-above t]
2063            ["Clear above" gnus-summary-clear-above t])
2064           ["Current score" gnus-summary-current-score t]
2065           ["Set score" gnus-summary-set-score t]
2066           ["Switch current score file..." gnus-score-change-score-file t]
2067           ["Set mark below..." gnus-score-set-mark-below t]
2068           ["Set expunge below..." gnus-score-set-expunge-below t]
2069           ["Edit current score file" gnus-score-edit-current-scores t]
2070           ["Edit score file" gnus-score-edit-file t]
2071           ["Trace score" gnus-score-find-trace t]
2072           ["Find words" gnus-score-find-favourite-words t]
2073           ["Rescore buffer" gnus-summary-rescore t]
2074           ["Increase score..." gnus-summary-increase-score t]
2075           ["Lower score..." gnus-summary-lower-score t]))))
2076
2077     ;; Define both the Article menu in the summary buffer and the
2078     ;; equivalent Commands menu in the article buffer here for
2079     ;; consistency.
2080     (let ((innards
2081            `(("Hide"
2082               ["All" gnus-article-hide t]
2083               ["Headers" gnus-article-hide-headers t]
2084               ["Signature" gnus-article-hide-signature t]
2085               ["Citation" gnus-article-hide-citation t]
2086               ["List identifiers" gnus-article-hide-list-identifiers t]
2087               ["Banner" gnus-article-strip-banner t]
2088               ["Boring headers" gnus-article-hide-boring-headers t])
2089              ("Highlight"
2090               ["All" gnus-article-highlight t]
2091               ["Headers" gnus-article-highlight-headers t]
2092               ["Signature" gnus-article-highlight-signature t]
2093               ["Citation" gnus-article-highlight-citation t])
2094              ("MIME"
2095               ["Words" gnus-article-decode-mime-words t]
2096               ["Charset" gnus-article-decode-charset t]
2097               ["QP" gnus-article-de-quoted-unreadable t]
2098               ["Base64" gnus-article-de-base64-unreadable t]
2099               ["View MIME buttons" gnus-summary-display-buttonized t]
2100               ["View all" gnus-mime-view-all-parts t]
2101               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2102               ["Encrypt body" gnus-article-encrypt-body
2103                :active (not (gnus-group-read-only-p))
2104                ,@(if (featurep 'xemacs) nil
2105                    '(:help "Encrypt the message body on disk"))]
2106               ["Extract all parts" gnus-summary-save-parts t]
2107               ("Multipart"
2108                ["Repair multipart" gnus-summary-repair-multipart t]
2109                ["Add buttons" gnus-summary-display-buttonized t]
2110                ["Pipe part" gnus-article-pipe-part t]
2111                ["Inline part" gnus-article-inline-part t]
2112                ["Encrypt body" gnus-article-encrypt-body
2113                 :active (not (gnus-group-read-only-p))
2114                ,@(if (featurep 'xemacs) nil
2115                    '(:help "Encrypt the message body on disk"))]
2116                ["View part externally" gnus-article-view-part-externally t]
2117                ["View part with charset" gnus-article-view-part-as-charset t]
2118                ["Copy part" gnus-article-copy-part t]
2119                ["Save part" gnus-article-save-part t]
2120                ["View part" gnus-article-view-part t]))
2121              ("Date"
2122               ["Local" gnus-article-date-local t]
2123               ["ISO8601" gnus-article-date-iso8601 t]
2124               ["UT" gnus-article-date-ut t]
2125               ["Original" gnus-article-date-original t]
2126               ["Lapsed" gnus-article-date-lapsed t]
2127               ["User-defined" gnus-article-date-user t])
2128              ("Display"
2129               ["Remove images" gnus-article-remove-images t]
2130               ["Toggle smiley" gnus-treat-smiley t]
2131               ["Show X-Face" gnus-article-display-x-face t]
2132               ["Show picons in From" gnus-treat-from-picon t]
2133               ["Show picons in mail headers" gnus-treat-mail-picon t]
2134               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2135               ("View as different encoding"
2136                ,@(gnus-summary-menu-split
2137                   (mapcar
2138                    (lambda (cs)
2139                      ;; Since easymenu under Emacs doesn't allow
2140                      ;; lambda forms for menu commands, we should
2141                      ;; provide intern'ed function symbols.
2142                      (let ((command (intern (format "\
2143 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2144                        (fset command
2145                              `(lambda ()
2146                                 (interactive)
2147                                 (let ((gnus-summary-show-article-charset-alist
2148                                        '((1 . ,cs))))
2149                                   (gnus-summary-show-article 1))))
2150                        `[,(symbol-name cs) ,command t]))
2151                    (sort (if (fboundp 'coding-system-list)
2152                              (coding-system-list)
2153                            (mapcar 'car mm-mime-mule-charset-alist))
2154                          'string<)))))
2155              ("Washing"
2156               ("Remove Blanks"
2157                ["Leading" gnus-article-strip-leading-blank-lines t]
2158                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2159                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2160                ["All of the above" gnus-article-strip-blank-lines t]
2161                ["All" gnus-article-strip-all-blank-lines t]
2162                ["Leading space" gnus-article-strip-leading-space t]
2163                ["Trailing space" gnus-article-strip-trailing-space t]
2164                ["Leading space in headers"
2165                 gnus-article-remove-leading-whitespace t])
2166               ["Overstrike" gnus-article-treat-overstrike t]
2167               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2168               ["Emphasis" gnus-article-emphasize t]
2169               ["Word wrap" gnus-article-fill-cited-article t]
2170               ["Fill long lines" gnus-article-fill-long-lines t]
2171               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2172               ["CR" gnus-article-remove-cr t]
2173               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2174               ["Base64" gnus-article-de-base64-unreadable t]
2175               ["Rot 13" gnus-summary-caesar-message
2176                ,@(if (featurep 'xemacs) '(t)
2177                    '(:help "\"Caesar rotate\" article by 13"))]
2178               ["Morse decode" gnus-summary-morse-message t]
2179               ["Unix pipe..." gnus-summary-pipe-message t]
2180               ["Add buttons" gnus-article-add-buttons t]
2181               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2182               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2183               ["Verbose header" gnus-summary-verbose-headers t]
2184               ["Toggle header" gnus-summary-toggle-header t]
2185               ["Unfold headers" gnus-article-treat-unfold-headers t]
2186               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2187               ["Html" gnus-article-wash-html t]
2188               ["URLs" gnus-article-unsplit-urls t]
2189               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2190               ["HZ" gnus-article-decode-HZ t]
2191               ("(Outlook) Deuglify"
2192                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2193                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2194                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2195                ["Full (Outlook) deuglify"
2196                 gnus-article-outlook-deuglify-article t])
2197               )
2198              ("Output"
2199               ["Save in default format" gnus-summary-save-article
2200                ,@(if (featurep 'xemacs) '(t)
2201                    '(:help "Save article using default method"))]
2202               ["Save in file" gnus-summary-save-article-file
2203                ,@(if (featurep 'xemacs) '(t)
2204                    '(:help "Save article in file"))]
2205               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2206               ["Save in MH folder" gnus-summary-save-article-folder t]
2207               ["Save in VM folder" gnus-summary-save-article-vm t]
2208               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2209               ["Save body in file" gnus-summary-save-article-body-file t]
2210               ["Pipe through a filter" gnus-summary-pipe-output t]
2211               ["Add to SOUP packet" gnus-soup-add-article t]
2212               ["Print with Muttprint" gnus-summary-muttprint t]
2213               ["Print" gnus-summary-print-article t])
2214              ("Backend"
2215               ["Respool article..." gnus-summary-respool-article t]
2216               ["Move article..." gnus-summary-move-article
2217                (gnus-check-backend-function
2218                 'request-move-article gnus-newsgroup-name)]
2219               ["Copy article..." gnus-summary-copy-article t]
2220               ["Crosspost article..." gnus-summary-crosspost-article
2221                (gnus-check-backend-function
2222                 'request-replace-article gnus-newsgroup-name)]
2223               ["Import file..." gnus-summary-import-article
2224                (gnus-check-backend-function
2225                 'request-accept-article gnus-newsgroup-name)]
2226               ["Create article..." gnus-summary-create-article
2227                (gnus-check-backend-function
2228                 'request-accept-article gnus-newsgroup-name)]
2229               ["Check if posted" gnus-summary-article-posted-p t]
2230               ["Edit article" gnus-summary-edit-article
2231                (not (gnus-group-read-only-p))]
2232               ["Delete article" gnus-summary-delete-article
2233                (gnus-check-backend-function
2234                 'request-expire-articles gnus-newsgroup-name)]
2235               ["Query respool" gnus-summary-respool-query t]
2236               ["Trace respool" gnus-summary-respool-trace t]
2237               ["Delete expirable articles" gnus-summary-expire-articles-now
2238                (gnus-check-backend-function
2239                 'request-expire-articles gnus-newsgroup-name)])
2240              ("Extract"
2241               ["Uudecode" gnus-uu-decode-uu
2242                ,@(if (featurep 'xemacs) '(t)
2243                    '(:help "Decode uuencoded article(s)"))]
2244               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2245               ["Unshar" gnus-uu-decode-unshar t]
2246               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2247               ["Save" gnus-uu-decode-save t]
2248               ["Binhex" gnus-uu-decode-binhex t]
2249               ["Postscript" gnus-uu-decode-postscript t]
2250               ["All MIME parts" gnus-summary-save-parts t])
2251              ("Cache"
2252               ["Enter article" gnus-cache-enter-article t]
2253               ["Remove article" gnus-cache-remove-article t])
2254              ["Translate" gnus-article-babel t]
2255              ["Select article buffer" gnus-summary-select-article-buffer t]
2256              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2257              ["Isearch article..." gnus-summary-isearch-article t]
2258              ["Beginning of the article" gnus-summary-beginning-of-article t]
2259              ["End of the article" gnus-summary-end-of-article t]
2260              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2261              ["Fetch referenced articles" gnus-summary-refer-references t]
2262              ["Fetch current thread" gnus-summary-refer-thread t]
2263              ["Fetch article with id..." gnus-summary-refer-article t]
2264              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2265              ["Redisplay" gnus-summary-show-article t]
2266              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2267       (easy-menu-define
2268         gnus-summary-article-menu gnus-summary-mode-map ""
2269         (cons "Article" innards))
2270
2271       (if (not (keymapp gnus-summary-article-menu))
2272           (easy-menu-define
2273             gnus-article-commands-menu gnus-article-mode-map ""
2274             (cons "Commands" innards))
2275         ;; in Emacs, don't share menu.
2276         (setq gnus-article-commands-menu
2277               (copy-keymap gnus-summary-article-menu))
2278         (define-key gnus-article-mode-map [menu-bar commands]
2279           (cons "Commands" gnus-article-commands-menu))))
2280
2281     (easy-menu-define
2282       gnus-summary-thread-menu gnus-summary-mode-map ""
2283       '("Threads"
2284         ["Find all messages in thread" gnus-summary-refer-thread t]
2285         ["Toggle threading" gnus-summary-toggle-threads t]
2286         ["Hide threads" gnus-summary-hide-all-threads t]
2287         ["Show threads" gnus-summary-show-all-threads t]
2288         ["Hide thread" gnus-summary-hide-thread t]
2289         ["Show thread" gnus-summary-show-thread t]
2290         ["Go to next thread" gnus-summary-next-thread t]
2291         ["Go to previous thread" gnus-summary-prev-thread t]
2292         ["Go down thread" gnus-summary-down-thread t]
2293         ["Go up thread" gnus-summary-up-thread t]
2294         ["Top of thread" gnus-summary-top-thread t]
2295         ["Mark thread as read" gnus-summary-kill-thread t]
2296         ["Lower thread score" gnus-summary-lower-thread t]
2297         ["Raise thread score" gnus-summary-raise-thread t]
2298         ["Rethread current" gnus-summary-rethread-current t]))
2299
2300     (easy-menu-define
2301       gnus-summary-post-menu gnus-summary-mode-map ""
2302       `("Post"
2303         ["Send a message (mail or news)" gnus-summary-post-news
2304          ,@(if (featurep 'xemacs) '(t)
2305              '(:help "Post an article"))]
2306         ["Followup" gnus-summary-followup
2307          ,@(if (featurep 'xemacs) '(t)
2308              '(:help "Post followup to this article"))]
2309         ["Followup and yank" gnus-summary-followup-with-original
2310          ,@(if (featurep 'xemacs) '(t)
2311              '(:help "Post followup to this article, quoting its contents"))]
2312         ["Supersede article" gnus-summary-supersede-article t]
2313         ["Cancel article" gnus-summary-cancel-article
2314          ,@(if (featurep 'xemacs) '(t)
2315              '(:help "Cancel an article you posted"))]
2316         ["Reply" gnus-summary-reply t]
2317         ["Reply and yank" gnus-summary-reply-with-original t]
2318         ["Wide reply" gnus-summary-wide-reply t]
2319         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2320          ,@(if (featurep 'xemacs) '(t)
2321              '(:help "Mail a reply, quoting this article"))]
2322         ["Very wide reply" gnus-summary-very-wide-reply t]
2323         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2324          ,@(if (featurep 'xemacs) '(t)
2325              '(:help "Mail a very wide reply, quoting this article"))]
2326         ["Mail forward" gnus-summary-mail-forward t]
2327         ["Post forward" gnus-summary-post-forward t]
2328         ["Digest and mail" gnus-uu-digest-mail-forward t]
2329         ["Digest and post" gnus-uu-digest-post-forward t]
2330         ["Resend message" gnus-summary-resend-message t]
2331         ["Resend message edit" gnus-summary-resend-message-edit t]
2332         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2333         ["Send a mail" gnus-summary-mail-other-window t]
2334         ["Create a local message" gnus-summary-news-other-window t]
2335         ["Uuencode and post" gnus-uu-post-news
2336          ,@(if (featurep 'xemacs) '(t)
2337              '(:help "Post a uuencoded article"))]
2338         ["Followup via news" gnus-summary-followup-to-mail t]
2339         ["Followup via news and yank"
2340          gnus-summary-followup-to-mail-with-original t]
2341         ;;("Draft"
2342         ;;["Send" gnus-summary-send-draft t]
2343         ;;["Send bounced" gnus-resend-bounced-mail t])
2344         ))
2345
2346     (cond
2347      ((not (keymapp gnus-summary-post-menu))
2348       (setq gnus-article-post-menu gnus-summary-post-menu))
2349      ((not gnus-article-post-menu)
2350       ;; Don't share post menu.
2351       (setq gnus-article-post-menu
2352             (copy-keymap gnus-summary-post-menu))))
2353     (define-key gnus-article-mode-map [menu-bar post]
2354       (cons "Post" gnus-article-post-menu))
2355
2356     (easy-menu-define
2357       gnus-summary-misc-menu gnus-summary-mode-map ""
2358       `("Gnus"
2359         ("Mark Read"
2360          ["Mark as read" gnus-summary-mark-as-read-forward t]
2361          ["Mark same subject and select"
2362           gnus-summary-kill-same-subject-and-select t]
2363          ["Mark same subject" gnus-summary-kill-same-subject t]
2364          ["Catchup" gnus-summary-catchup
2365           ,@(if (featurep 'xemacs) '(t)
2366               '(:help "Mark unread articles in this group as read"))]
2367          ["Catchup all" gnus-summary-catchup-all t]
2368          ["Catchup to here" gnus-summary-catchup-to-here t]
2369          ["Catchup from here" gnus-summary-catchup-from-here t]
2370          ["Catchup region" gnus-summary-mark-region-as-read 
2371           (gnus-mark-active-p)]
2372          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2373         ("Mark Various"
2374          ["Tick" gnus-summary-tick-article-forward t]
2375          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2376          ["Remove marks" gnus-summary-clear-mark-forward t]
2377          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2378          ["Set bookmark" gnus-summary-set-bookmark t]
2379          ["Remove bookmark" gnus-summary-remove-bookmark t])
2380         ("Limit to"
2381          ["Marks..." gnus-summary-limit-to-marks t]
2382          ["Subject..." gnus-summary-limit-to-subject t]
2383          ["Author..." gnus-summary-limit-to-author t]
2384          ["Age..." gnus-summary-limit-to-age t]
2385          ["Extra..." gnus-summary-limit-to-extra t]
2386          ["Score..." gnus-summary-limit-to-score t]
2387          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2388          ["Unread" gnus-summary-limit-to-unread t]
2389          ["Unseen" gnus-summary-limit-to-unseen t]
2390          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2391          ["Next articles" gnus-summary-limit-to-articles t]
2392          ["Pop limit" gnus-summary-pop-limit t]
2393          ["Show dormant" gnus-summary-limit-include-dormant t]
2394          ["Hide childless dormant"
2395           gnus-summary-limit-exclude-childless-dormant t]
2396          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2397          ["Hide marked" gnus-summary-limit-exclude-marks t]
2398          ["Show expunged" gnus-summary-limit-include-expunged t])
2399         ("Process Mark"
2400          ["Set mark" gnus-summary-mark-as-processable t]
2401          ["Remove mark" gnus-summary-unmark-as-processable t]
2402          ["Remove all marks" gnus-summary-unmark-all-processable t]
2403          ["Mark above" gnus-uu-mark-over t]
2404          ["Mark series" gnus-uu-mark-series t]
2405          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2406          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2407          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2408          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2409          ["Mark all" gnus-uu-mark-all t]
2410          ["Mark buffer" gnus-uu-mark-buffer t]
2411          ["Mark sparse" gnus-uu-mark-sparse t]
2412          ["Mark thread" gnus-uu-mark-thread t]
2413          ["Unmark thread" gnus-uu-unmark-thread t]
2414          ("Process Mark Sets"
2415           ["Kill" gnus-summary-kill-process-mark t]
2416           ["Yank" gnus-summary-yank-process-mark
2417            gnus-newsgroup-process-stack]
2418           ["Save" gnus-summary-save-process-mark t]
2419           ["Run command on marked..." gnus-summary-universal-argument t]))
2420         ("Scroll article"
2421          ["Page forward" gnus-summary-next-page
2422           ,@(if (featurep 'xemacs) '(t)
2423               '(:help "Show next page of article"))]
2424          ["Page backward" gnus-summary-prev-page
2425           ,@(if (featurep 'xemacs) '(t)
2426               '(:help "Show previous page of article"))]
2427          ["Line forward" gnus-summary-scroll-up t])
2428         ("Move"
2429          ["Next unread article" gnus-summary-next-unread-article t]
2430          ["Previous unread article" gnus-summary-prev-unread-article t]
2431          ["Next article" gnus-summary-next-article t]
2432          ["Previous article" gnus-summary-prev-article t]
2433          ["Next unread subject" gnus-summary-next-unread-subject t]
2434          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2435          ["Next article same subject" gnus-summary-next-same-subject t]
2436          ["Previous article same subject" gnus-summary-prev-same-subject t]
2437          ["First unread article" gnus-summary-first-unread-article t]
2438          ["Best unread article" gnus-summary-best-unread-article t]
2439          ["Go to subject number..." gnus-summary-goto-subject t]
2440          ["Go to article number..." gnus-summary-goto-article t]
2441          ["Go to the last article" gnus-summary-goto-last-article t]
2442          ["Pop article off history" gnus-summary-pop-article t])
2443         ("Sort"
2444          ["Sort by number" gnus-summary-sort-by-number t]
2445          ["Sort by author" gnus-summary-sort-by-author t]
2446          ["Sort by subject" gnus-summary-sort-by-subject t]
2447          ["Sort by date" gnus-summary-sort-by-date t]
2448          ["Sort by score" gnus-summary-sort-by-score t]
2449          ["Sort by lines" gnus-summary-sort-by-lines t]
2450          ["Sort by characters" gnus-summary-sort-by-chars t]
2451          ["Randomize" gnus-summary-sort-by-random t]
2452          ["Original sort" gnus-summary-sort-by-original t])
2453         ("Help"
2454          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2455          ["Describe group" gnus-summary-describe-group t]
2456          ["Fetch charter" gnus-group-fetch-charter
2457           ,@(if (featurep 'xemacs) nil
2458               '(:help "Display the charter of the current group"))]
2459          ["Fetch control message" gnus-group-fetch-control
2460           ,@(if (featurep 'xemacs) nil
2461               '(:help "Display the archived control message for the current group"))]
2462          ["Read manual" gnus-info-find-node t])
2463         ("Modes"
2464          ["Pick and read" gnus-pick-mode t]
2465          ["Binary" gnus-binary-mode t])
2466         ("Regeneration"
2467          ["Regenerate" gnus-summary-prepare t]
2468          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2469          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2470          ["Toggle threading" gnus-summary-toggle-threads t])
2471         ["See old articles" gnus-summary-insert-old-articles t]
2472         ["See new articles" gnus-summary-insert-new-articles t]
2473         ["Filter articles..." gnus-summary-execute-command t]
2474         ["Run command on articles..." gnus-summary-universal-argument t]
2475         ["Search articles forward..." gnus-summary-search-article-forward t]
2476         ["Search articles backward..." gnus-summary-search-article-backward t]
2477         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2478         ["Expand window" gnus-summary-expand-window t]
2479         ["Expire expirable articles" gnus-summary-expire-articles
2480          (gnus-check-backend-function
2481           'request-expire-articles gnus-newsgroup-name)]
2482         ["Edit local kill file" gnus-summary-edit-local-kill t]
2483         ["Edit main kill file" gnus-summary-edit-global-kill t]
2484         ["Edit group parameters" gnus-summary-edit-parameters t]
2485         ["Customize group parameters" gnus-summary-customize-parameters t]
2486         ["Send a bug report" gnus-bug t]
2487         ("Exit"
2488          ["Catchup and exit" gnus-summary-catchup-and-exit
2489           ,@(if (featurep 'xemacs) '(t)
2490               '(:help "Mark unread articles in this group as read, then exit"))]
2491          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2492          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2493          ["Exit group" gnus-summary-exit
2494           ,@(if (featurep 'xemacs) '(t)
2495               '(:help "Exit current group, return to group selection mode"))]
2496          ["Exit group without updating" gnus-summary-exit-no-update t]
2497          ["Exit and goto next group" gnus-summary-next-group t]
2498          ["Exit and goto prev group" gnus-summary-prev-group t]
2499          ["Reselect group" gnus-summary-reselect-current-group t]
2500          ["Rescan group" gnus-summary-rescan-group t]
2501          ["Update dribble" gnus-summary-save-newsrc t])))
2502
2503     (gnus-run-hooks 'gnus-summary-menu-hook)))
2504
2505 (defvar gnus-summary-tool-bar-map nil)
2506
2507 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2508 (defun gnus-summary-make-tool-bar ()
2509   (if (and (fboundp 'tool-bar-add-item-from-menu)
2510            (default-value 'tool-bar-mode)
2511            (not gnus-summary-tool-bar-map))
2512       (setq gnus-summary-tool-bar-map
2513             (let ((tool-bar-map (make-sparse-keymap))
2514                   (load-path (mm-image-load-path)))
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-post-news "post" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-followup "followup" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-reply "reply" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2545               tool-bar-map)))
2546   (if gnus-summary-tool-bar-map
2547       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2548
2549 (defun gnus-score-set-default (var value)
2550   "A version of set that updates the GNU Emacs menu-bar."
2551   (set var value)
2552   ;; It is the message that forces the active status to be updated.
2553   (message ""))
2554
2555 (defun gnus-make-score-map (type)
2556   "Make a summary score map of type TYPE."
2557   (if t
2558       nil
2559     (let ((headers '(("author" "from" string)
2560                      ("subject" "subject" string)
2561                      ("article body" "body" string)
2562                      ("article head" "head" string)
2563                      ("xref" "xref" string)
2564                      ("extra header" "extra" string)
2565                      ("lines" "lines" number)
2566                      ("followups to author" "followup" string)))
2567           (types '((number ("less than" <)
2568                            ("greater than" >)
2569                            ("equal" =))
2570                    (string ("substring" s)
2571                            ("exact string" e)
2572                            ("fuzzy string" f)
2573                            ("regexp" r))))
2574           (perms '(("temporary" (current-time-string))
2575                    ("permanent" nil)
2576                    ("immediate" now)))
2577           header)
2578       (list
2579        (apply
2580         'nconc
2581         (list
2582          (if (eq type 'lower)
2583              "Lower score"
2584            "Increase score"))
2585         (let (outh)
2586           (while headers
2587             (setq header (car headers))
2588             (setq outh
2589                   (cons
2590                    (apply
2591                     'nconc
2592                     (list (car header))
2593                     (let ((ts (cdr (assoc (nth 2 header) types)))
2594                           outt)
2595                       (while ts
2596                         (setq outt
2597                               (cons
2598                                (apply
2599                                 'nconc
2600                                 (list (caar ts))
2601                                 (let ((ps perms)
2602                                       outp)
2603                                   (while ps
2604                                     (setq outp
2605                                           (cons
2606                                            (vector
2607                                             (caar ps)
2608                                             (list
2609                                              'gnus-summary-score-entry
2610                                              (nth 1 header)
2611                                              (if (or (string= (nth 1 header)
2612                                                               "head")
2613                                                      (string= (nth 1 header)
2614                                                               "body"))
2615                                                  ""
2616                                                (list 'gnus-summary-header
2617                                                      (nth 1 header)))
2618                                              (list 'quote (nth 1 (car ts)))
2619                                              (list 'gnus-score-delta-default
2620                                                    nil)
2621                                              (nth 1 (car ps))
2622                                              t)
2623                                             t)
2624                                            outp))
2625                                     (setq ps (cdr ps)))
2626                                   (list (nreverse outp))))
2627                                outt))
2628                         (setq ts (cdr ts)))
2629                       (list (nreverse outt))))
2630                    outh))
2631             (setq headers (cdr headers)))
2632           (list (nreverse outh))))))))
2633
2634 \f
2635
2636 (defun gnus-summary-mode (&optional group)
2637   "Major mode for reading articles.
2638
2639 All normal editing commands are switched off.
2640 \\<gnus-summary-mode-map>
2641 Each line in this buffer represents one article.  To read an
2642 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2643 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2644 respectively.
2645
2646 You can also post articles and send mail from this buffer.  To
2647 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2648 of an article, type `\\[gnus-summary-reply]'.
2649
2650 There are approx. one gazillion commands you can execute in this
2651 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2652
2653 The following commands are available:
2654
2655 \\{gnus-summary-mode-map}"
2656   (interactive)
2657   (kill-all-local-variables)
2658   (when (gnus-visual-p 'summary-menu 'menu)
2659     (gnus-summary-make-menu-bar)
2660     (gnus-summary-make-tool-bar))
2661   (gnus-summary-make-local-variables)
2662   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2663     (gnus-summary-make-local-variables))
2664   (gnus-make-thread-indent-array)
2665   (gnus-simplify-mode-line)
2666   (setq major-mode 'gnus-summary-mode)
2667   (setq mode-name "Summary")
2668   (make-local-variable 'minor-mode-alist)
2669   (use-local-map gnus-summary-mode-map)
2670   (buffer-disable-undo)
2671   (setq buffer-read-only t)             ;Disable modification
2672   (setq truncate-lines t)
2673   (setq selective-display t)
2674   (setq selective-display-ellipses t)   ;Display `...'
2675   (gnus-summary-set-display-table)
2676   (gnus-set-default-directory)
2677   (setq gnus-newsgroup-name group)
2678   (make-local-variable 'gnus-summary-line-format)
2679   (make-local-variable 'gnus-summary-line-format-spec)
2680   (make-local-variable 'gnus-summary-dummy-line-format)
2681   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2682   (make-local-variable 'gnus-summary-mark-positions)
2683   (gnus-make-local-hook 'pre-command-hook)
2684   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2685   (gnus-run-hooks 'gnus-summary-mode-hook)
2686   (turn-on-gnus-mailing-list-mode)
2687   (mm-enable-multibyte)
2688   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2689   (gnus-update-summary-mark-positions))
2690
2691 (defun gnus-summary-make-local-variables ()
2692   "Make all the local summary buffer variables."
2693   (let (global)
2694     (dolist (local gnus-summary-local-variables)
2695       (if (consp local)
2696           (progn
2697             (if (eq (cdr local) 'global)
2698                 ;; Copy the global value of the variable.
2699                 (setq global (symbol-value (car local)))
2700               ;; Use the value from the list.
2701               (setq global (eval (cdr local))))
2702             (set (make-local-variable (car local)) global))
2703         ;; Simple nil-valued local variable.
2704         (set (make-local-variable local) nil)))))
2705
2706 (defun gnus-summary-clear-local-variables ()
2707   (let ((locals gnus-summary-local-variables))
2708     (while locals
2709       (if (consp (car locals))
2710           (and (vectorp (caar locals))
2711                (set (caar locals) nil))
2712         (and (vectorp (car locals))
2713              (set (car locals) nil)))
2714       (setq locals (cdr locals)))))
2715
2716 ;; Summary data functions.
2717
2718 (defmacro gnus-data-number (data)
2719   `(car ,data))
2720
2721 (defmacro gnus-data-set-number (data number)
2722   `(setcar ,data ,number))
2723
2724 (defmacro gnus-data-mark (data)
2725   `(nth 1 ,data))
2726
2727 (defmacro gnus-data-set-mark (data mark)
2728   `(setcar (nthcdr 1 ,data) ,mark))
2729
2730 (defmacro gnus-data-pos (data)
2731   `(nth 2 ,data))
2732
2733 (defmacro gnus-data-set-pos (data pos)
2734   `(setcar (nthcdr 2 ,data) ,pos))
2735
2736 (defmacro gnus-data-header (data)
2737   `(nth 3 ,data))
2738
2739 (defmacro gnus-data-set-header (data header)
2740   `(setf (nth 3 ,data) ,header))
2741
2742 (defmacro gnus-data-level (data)
2743   `(nth 4 ,data))
2744
2745 (defmacro gnus-data-unread-p (data)
2746   `(= (nth 1 ,data) gnus-unread-mark))
2747
2748 (defmacro gnus-data-read-p (data)
2749   `(/= (nth 1 ,data) gnus-unread-mark))
2750
2751 (defmacro gnus-data-pseudo-p (data)
2752   `(consp (nth 3 ,data)))
2753
2754 (defmacro gnus-data-find (number)
2755   `(assq ,number gnus-newsgroup-data))
2756
2757 (defmacro gnus-data-find-list (number &optional data)
2758   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2759      (memq (assq ,number bdata)
2760            bdata)))
2761
2762 (defmacro gnus-data-make (number mark pos header level)
2763   `(list ,number ,mark ,pos ,header ,level))
2764
2765 (defun gnus-data-enter (after-article number mark pos header level offset)
2766   (let ((data (gnus-data-find-list after-article)))
2767     (unless data
2768       (error "No such article: %d" after-article))
2769     (setcdr data (cons (gnus-data-make number mark pos header level)
2770                        (cdr data)))
2771     (setq gnus-newsgroup-data-reverse nil)
2772     (gnus-data-update-list (cddr data) offset)))
2773
2774 (defun gnus-data-enter-list (after-article list &optional offset)
2775   (when list
2776     (let ((data (and after-article (gnus-data-find-list after-article)))
2777           (ilist list))
2778       (if (not (or data
2779                    after-article))
2780           (let ((odata gnus-newsgroup-data))
2781             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2782             (when offset
2783               (gnus-data-update-list odata offset)))
2784       ;; Find the last element in the list to be spliced into the main
2785         ;; list.
2786         (while (cdr list)
2787           (setq list (cdr list)))
2788         (if (not data)
2789             (progn
2790               (setcdr list gnus-newsgroup-data)
2791               (setq gnus-newsgroup-data ilist)
2792               (when offset
2793                 (gnus-data-update-list (cdr list) offset)))
2794           (setcdr list (cdr data))
2795           (setcdr data ilist)
2796           (when offset
2797             (gnus-data-update-list (cdr list) offset))))
2798       (setq gnus-newsgroup-data-reverse nil))))
2799
2800 (defun gnus-data-remove (article &optional offset)
2801   (let ((data gnus-newsgroup-data))
2802     (if (= (gnus-data-number (car data)) article)
2803         (progn
2804           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2805                 gnus-newsgroup-data-reverse nil)
2806           (when offset
2807             (gnus-data-update-list gnus-newsgroup-data offset)))
2808       (while (cdr data)
2809         (when (= (gnus-data-number (cadr data)) article)
2810           (setcdr data (cddr data))
2811           (when offset
2812             (gnus-data-update-list (cdr data) offset))
2813           (setq data nil
2814                 gnus-newsgroup-data-reverse nil))
2815         (setq data (cdr data))))))
2816
2817 (defmacro gnus-data-list (backward)
2818   `(if ,backward
2819        (or gnus-newsgroup-data-reverse
2820            (setq gnus-newsgroup-data-reverse
2821                  (reverse gnus-newsgroup-data)))
2822      gnus-newsgroup-data))
2823
2824 (defun gnus-data-update-list (data offset)
2825   "Add OFFSET to the POS of all data entries in DATA."
2826   (setq gnus-newsgroup-data-reverse nil)
2827   (while data
2828     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2829     (setq data (cdr data))))
2830
2831 (defun gnus-summary-article-pseudo-p (article)
2832   "Say whether this article is a pseudo article or not."
2833   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2834
2835 (defmacro gnus-summary-article-sparse-p (article)
2836   "Say whether this article is a sparse article or not."
2837   `(memq ,article gnus-newsgroup-sparse))
2838
2839 (defmacro gnus-summary-article-ancient-p (article)
2840   "Say whether this article is a sparse article or not."
2841   `(memq ,article gnus-newsgroup-ancient))
2842
2843 (defun gnus-article-parent-p (number)
2844   "Say whether this article is a parent or not."
2845   (let ((data (gnus-data-find-list number)))
2846     (and (cdr data)              ; There has to be an article after...
2847          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2848             (gnus-data-level (nth 1 data))))))
2849
2850 (defun gnus-article-children (number)
2851   "Return a list of all children to NUMBER."
2852   (let* ((data (gnus-data-find-list number))
2853          (level (gnus-data-level (car data)))
2854          children)
2855     (setq data (cdr data))
2856     (while (and data
2857                 (= (gnus-data-level (car data)) (1+ level)))
2858       (push (gnus-data-number (car data)) children)
2859       (setq data (cdr data)))
2860     children))
2861
2862 (defmacro gnus-summary-skip-intangible ()
2863   "If the current article is intangible, then jump to a different article."
2864   '(let ((to (get-text-property (point) 'gnus-intangible)))
2865      (and to (gnus-summary-goto-subject to))))
2866
2867 (defmacro gnus-summary-article-intangible-p ()
2868   "Say whether this article is intangible or not."
2869   '(get-text-property (point) 'gnus-intangible))
2870
2871 (defun gnus-article-read-p (article)
2872   "Say whether ARTICLE is read or not."
2873   (not (or (memq article gnus-newsgroup-marked)
2874            (memq article gnus-newsgroup-spam-marked)
2875            (memq article gnus-newsgroup-unreads)
2876            (memq article gnus-newsgroup-unselected)
2877            (memq article gnus-newsgroup-dormant))))
2878
2879 ;; Some summary mode macros.
2880
2881 (defmacro gnus-summary-article-number ()
2882   "The article number of the article on the current line.
2883 If there isn't an article number here, then we return the current
2884 article number."
2885   '(progn
2886      (gnus-summary-skip-intangible)
2887      (or (get-text-property (point) 'gnus-number)
2888          (gnus-summary-last-subject))))
2889
2890 (defmacro gnus-summary-article-header (&optional number)
2891   "Return the header of article NUMBER."
2892   `(gnus-data-header (gnus-data-find
2893                       ,(or number '(gnus-summary-article-number)))))
2894
2895 (defmacro gnus-summary-thread-level (&optional number)
2896   "Return the level of thread that starts with article NUMBER."
2897   `(if (and (eq gnus-summary-make-false-root 'dummy)
2898             (get-text-property (point) 'gnus-intangible))
2899        0
2900      (gnus-data-level (gnus-data-find
2901                        ,(or number '(gnus-summary-article-number))))))
2902
2903 (defmacro gnus-summary-article-mark (&optional number)
2904   "Return the mark of article NUMBER."
2905   `(gnus-data-mark (gnus-data-find
2906                     ,(or number '(gnus-summary-article-number)))))
2907
2908 (defmacro gnus-summary-article-pos (&optional number)
2909   "Return the position of the line of article NUMBER."
2910   `(gnus-data-pos (gnus-data-find
2911                    ,(or number '(gnus-summary-article-number)))))
2912
2913 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2914 (defmacro gnus-summary-article-subject (&optional number)
2915   "Return current subject string or nil if nothing."
2916   `(let ((headers
2917           ,(if number
2918                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2919              '(gnus-data-header (assq (gnus-summary-article-number)
2920                                       gnus-newsgroup-data)))))
2921      (and headers
2922           (vectorp headers)
2923           (mail-header-subject headers))))
2924
2925 (defmacro gnus-summary-article-score (&optional number)
2926   "Return current article score."
2927   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2928                   gnus-newsgroup-scored))
2929        gnus-summary-default-score 0))
2930
2931 (defun gnus-summary-article-children (&optional number)
2932   "Return a list of article numbers that are children of article NUMBER."
2933   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2934          (level (gnus-data-level (car data)))
2935          l children)
2936     (while (and (setq data (cdr data))
2937                 (> (setq l (gnus-data-level (car data))) level))
2938       (and (= (1+ level) l)
2939            (push (gnus-data-number (car data))
2940                  children)))
2941     (nreverse children)))
2942
2943 (defun gnus-summary-article-parent (&optional number)
2944   "Return the article number of the parent of article NUMBER."
2945   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2946                                     (gnus-data-list t)))
2947          (level (gnus-data-level (car data))))
2948     (if (zerop level)
2949         ()                              ; This is a root.
2950       ;; We search until we find an article with a level less than
2951       ;; this one.  That function has to be the parent.
2952       (while (and (setq data (cdr data))
2953                   (not (< (gnus-data-level (car data)) level))))
2954       (and data (gnus-data-number (car data))))))
2955
2956 (defun gnus-unread-mark-p (mark)
2957   "Say whether MARK is the unread mark."
2958   (= mark gnus-unread-mark))
2959
2960 (defun gnus-read-mark-p (mark)
2961   "Say whether MARK is one of the marks that mark as read.
2962 This is all marks except unread, ticked, dormant, and expirable."
2963   (not (or (= mark gnus-unread-mark)
2964            (= mark gnus-ticked-mark)
2965            (= mark gnus-spam-mark)
2966            (= mark gnus-dormant-mark)
2967            (= mark gnus-expirable-mark))))
2968
2969 (defmacro gnus-article-mark (number)
2970   "Return the MARK of article NUMBER.
2971 This macro should only be used when computing the mark the \"first\"
2972 time; i.e., when generating the summary lines.  After that,
2973 `gnus-summary-article-mark' should be used to examine the
2974 marks of articles."
2975   `(cond
2976     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2977     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2978     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2979     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2980     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2981     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2982     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2983     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2984            gnus-ancient-mark))))
2985
2986 ;; Saving hidden threads.
2987
2988 (defmacro gnus-save-hidden-threads (&rest forms)
2989   "Save hidden threads, eval FORMS, and restore the hidden threads."
2990   (let ((config (make-symbol "config")))
2991     `(let ((,config (gnus-hidden-threads-configuration)))
2992        (unwind-protect
2993            (save-excursion
2994              ,@forms)
2995          (gnus-restore-hidden-threads-configuration ,config)))))
2996 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2997 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2998
2999 (defun gnus-data-compute-positions ()
3000   "Compute the positions of all articles."
3001   (setq gnus-newsgroup-data-reverse nil)
3002   (let ((data gnus-newsgroup-data))
3003     (save-excursion
3004       (gnus-save-hidden-threads
3005         (gnus-summary-show-all-threads)
3006         (goto-char (point-min))
3007         (while data
3008           (while (get-text-property (point) 'gnus-intangible)
3009             (forward-line 1))
3010           (gnus-data-set-pos (car data) (+ (point) 3))
3011           (setq data (cdr data))
3012           (forward-line 1))))))
3013
3014 (defun gnus-hidden-threads-configuration ()
3015   "Return the current hidden threads configuration."
3016   (save-excursion
3017     (let (config)
3018       (goto-char (point-min))
3019       (while (search-forward "\r" nil t)
3020         (push (1- (point)) config))
3021       config)))
3022
3023 (defun gnus-restore-hidden-threads-configuration (config)
3024   "Restore hidden threads configuration from CONFIG."
3025   (save-excursion
3026     (let (point buffer-read-only)
3027       (while (setq point (pop config))
3028         (when (and (< point (point-max))
3029                    (goto-char point)
3030                    (eq (char-after) ?\n))
3031           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3032
3033 ;; Various summary mode internalish functions.
3034
3035 (defun gnus-mouse-pick-article (e)
3036   (interactive "e")
3037   (mouse-set-point e)
3038   (gnus-summary-next-page nil t))
3039
3040 (defun gnus-summary-set-display-table ()
3041   "Change the display table.
3042 Odd characters have a tendency to mess
3043 up nicely formatted displays - we make all possible glyphs
3044 display only a single character."
3045
3046   ;; We start from the standard display table, if any.
3047   (let ((table (or (copy-sequence standard-display-table)
3048                    (make-display-table)))
3049         (i 32))
3050     ;; Nix out all the control chars...
3051     (while (>= (setq i (1- i)) 0)
3052       (aset table i [??]))
3053    ;; ... but not newline and cr, of course.  (cr is necessary for the
3054     ;; selective display).
3055     (aset table ?\n nil)
3056     (aset table ?\r nil)
3057     ;; We keep TAB as well.
3058     (aset table ?\t nil)
3059     ;; We nix out any glyphs over 126 that are not set already.
3060     (let ((i 256))
3061       (while (>= (setq i (1- i)) 127)
3062         ;; Only modify if the entry is nil.
3063         (unless (aref table i)
3064           (aset table i [??]))))
3065     (setq buffer-display-table table)))
3066
3067 (defun gnus-summary-set-article-display-arrow (pos)
3068   "Update the overlay arrow to point to line at position POS."
3069   (when (and gnus-summary-display-arrow
3070              (boundp 'overlay-arrow-position)
3071              (boundp 'overlay-arrow-string))
3072     (save-excursion
3073       (goto-char pos)
3074       (beginning-of-line)
3075       (unless overlay-arrow-position
3076         (setq overlay-arrow-position (make-marker)))
3077       (setq overlay-arrow-string "=>"
3078             overlay-arrow-position (set-marker overlay-arrow-position
3079                                                (point)
3080                                                (current-buffer))))))
3081
3082 (defun gnus-summary-setup-buffer (group)
3083   "Initialize summary buffer."
3084   (let ((buffer (gnus-summary-buffer-name group))
3085         (dead-name (concat "*Dead Summary "
3086                            (gnus-group-decoded-name group) "*")))
3087     ;; If a dead summary buffer exists, we kill it.
3088     (when (gnus-buffer-live-p dead-name)
3089       (gnus-kill-buffer dead-name))
3090     (if (get-buffer buffer)
3091         (progn
3092           (set-buffer buffer)
3093           (setq gnus-summary-buffer (current-buffer))
3094           (not gnus-newsgroup-prepared))
3095       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3096       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3097       (gnus-summary-mode group)
3098       (when gnus-carpal
3099         (gnus-carpal-setup-buffer 'summary))
3100       (unless gnus-single-article-buffer
3101         (make-local-variable 'gnus-article-buffer)
3102         (make-local-variable 'gnus-article-current)
3103         (make-local-variable 'gnus-original-article-buffer))
3104       (setq gnus-newsgroup-name group)
3105       ;; Set any local variables in the group parameters.
3106       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3107       t)))
3108
3109 (defun gnus-set-global-variables ()
3110   "Set the global equivalents of the buffer-local variables.
3111 They are set to the latest values they had.  These reflect the summary
3112 buffer that was in action when the last article was fetched."
3113   (when (eq major-mode 'gnus-summary-mode)
3114     (setq gnus-summary-buffer (current-buffer))
3115     (let ((name gnus-newsgroup-name)
3116           (marked gnus-newsgroup-marked)
3117           (spam gnus-newsgroup-spam-marked)
3118           (unread gnus-newsgroup-unreads)
3119           (headers gnus-current-headers)
3120           (data gnus-newsgroup-data)
3121           (summary gnus-summary-buffer)
3122           (article-buffer gnus-article-buffer)
3123           (original gnus-original-article-buffer)
3124           (gac gnus-article-current)
3125           (reffed gnus-reffed-article-number)
3126           (score-file gnus-current-score-file)
3127           (default-charset gnus-newsgroup-charset)
3128           vlist)
3129       (let ((locals gnus-newsgroup-variables))
3130         (while locals
3131           (if (consp (car locals))
3132               (push (eval (caar locals)) vlist)
3133             (push (eval (car locals)) vlist))
3134           (setq locals (cdr locals)))
3135         (setq vlist (nreverse vlist)))
3136       (save-excursion
3137         (set-buffer gnus-group-buffer)
3138         (setq gnus-newsgroup-name name
3139               gnus-newsgroup-marked marked
3140               gnus-newsgroup-spam-marked spam
3141               gnus-newsgroup-unreads unread
3142               gnus-current-headers headers
3143               gnus-newsgroup-data data
3144               gnus-article-current gac
3145               gnus-summary-buffer summary
3146               gnus-article-buffer article-buffer
3147               gnus-original-article-buffer original
3148               gnus-reffed-article-number reffed
3149               gnus-current-score-file score-file
3150               gnus-newsgroup-charset default-charset)
3151         (let ((locals gnus-newsgroup-variables))
3152           (while locals
3153             (if (consp (car locals))
3154                 (set (caar locals) (pop vlist))
3155               (set (car locals) (pop vlist)))
3156             (setq locals (cdr locals))))
3157         ;; The article buffer also has local variables.
3158         (when (gnus-buffer-live-p gnus-article-buffer)
3159           (set-buffer gnus-article-buffer)
3160           (setq gnus-summary-buffer summary))))))
3161
3162 (defun gnus-summary-article-unread-p (article)
3163   "Say whether ARTICLE is unread or not."
3164   (memq article gnus-newsgroup-unreads))
3165
3166 (defun gnus-summary-first-article-p (&optional article)
3167   "Return whether ARTICLE is the first article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       nil
3170     (eq article (caar gnus-newsgroup-data))))
3171
3172 (defun gnus-summary-last-article-p (&optional article)
3173   "Return whether ARTICLE is the last article in the buffer."
3174   (if (not (setq article (or article (gnus-summary-article-number))))
3175       ;; All non-existent numbers are the last article.  :-)
3176       t
3177     (not (cdr (gnus-data-find-list article)))))
3178
3179 (defun gnus-make-thread-indent-array ()
3180   (let ((n 200))
3181     (unless (and gnus-thread-indent-array
3182                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3183       (setq gnus-thread-indent-array (make-vector 201 "")
3184             gnus-thread-indent-array-level gnus-thread-indent-level)
3185       (while (>= n 0)
3186         (aset gnus-thread-indent-array n
3187               (make-string (* n gnus-thread-indent-level) ? ))
3188         (setq n (1- n))))))
3189
3190 (defun gnus-update-summary-mark-positions ()
3191   "Compute where the summary marks are to go."
3192   (save-excursion
3193     (when (gnus-buffer-exists-p gnus-summary-buffer)
3194       (set-buffer gnus-summary-buffer))
3195     (let ((gnus-replied-mark 129)
3196           (gnus-score-below-mark 130)
3197           (gnus-score-over-mark 130)
3198           (gnus-undownloaded-mark 131)
3199           (spec gnus-summary-line-format-spec)
3200           gnus-visual pos)
3201       (save-excursion
3202         (gnus-set-work-buffer)
3203         (let ((gnus-summary-line-format-spec spec)
3204               (gnus-newsgroup-downloadable '(0)))
3205           (gnus-summary-insert-line
3206            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3207            0 nil t 128 t nil "" nil 1)
3208           (goto-char (point-min))
3209           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3210                                              (- (point) (point-min) 1)))))
3211           (goto-char (point-min))
3212           (push (cons 'replied (and (search-forward "\201" nil t)
3213                                     (- (point) (point-min) 1)))
3214                 pos)
3215           (goto-char (point-min))
3216           (push (cons 'score (and (search-forward "\202" nil t)
3217                                   (- (point) (point-min) 1)))
3218                 pos)
3219           (goto-char (point-min))
3220           (push (cons 'download
3221                       (and (search-forward "\203" nil t)
3222                            (- (point) (point-min) 1)))
3223                 pos)))
3224       (setq gnus-summary-mark-positions pos))))
3225
3226 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3227   "Insert a dummy root in the summary buffer."
3228   (beginning-of-line)
3229   (gnus-add-text-properties
3230    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3231    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3232
3233 (defun gnus-summary-extract-address-component (from)
3234   (or (car (funcall gnus-extract-address-components from))
3235       from))
3236
3237 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3238   (let ((mail-parse-charset gnus-newsgroup-charset)
3239         ; Is it really necessary to do this next part for each summary line?
3240         ; Luckily, doesn't seem to slow things down much.
3241         (mail-parse-ignored-charsets
3242          (save-excursion (set-buffer gnus-summary-buffer)
3243                          gnus-newsgroup-ignored-charsets)))
3244     (or
3245      (and gnus-ignored-from-addresses
3246           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3247           (let ((extra-headers (mail-header-extra header))
3248                 to
3249                 newsgroups)
3250             (cond
3251              ((setq to (cdr (assq 'To extra-headers)))
3252               (concat "-> "
3253                       (inline
3254                         (gnus-summary-extract-address-component
3255                          (funcall gnus-decode-encoded-word-function to)))))
3256              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3257               (concat "=> " newsgroups)))))
3258      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3259
3260 (defun gnus-summary-insert-line (gnus-tmp-header
3261                                  gnus-tmp-level gnus-tmp-current
3262                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3263                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3264                                  &optional gnus-tmp-dummy gnus-tmp-score
3265                                  gnus-tmp-process)
3266   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3267          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3268          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3269          (gnus-tmp-score-char
3270           (if (or (null gnus-summary-default-score)
3271                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3272                       gnus-summary-zcore-fuzz))
3273               ?                         ;Whitespace
3274             (if (< gnus-tmp-score gnus-summary-default-score)
3275                 gnus-score-below-mark gnus-score-over-mark)))
3276          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3277          (gnus-tmp-replied
3278           (cond (gnus-tmp-process gnus-process-mark)
3279                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3280                  gnus-cached-mark)
3281                 (gnus-tmp-replied gnus-replied-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3283                  gnus-forwarded-mark)
3284                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3285                  gnus-saved-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3287                  gnus-recent-mark)
3288                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3289                  gnus-unseen-mark)
3290                 (t gnus-no-mark)))
3291          (gnus-tmp-downloaded
3292           (cond (undownloaded 
3293                  gnus-undownloaded-mark)
3294                 (gnus-newsgroup-agentized
3295                  gnus-downloaded-mark)
3296                 (t
3297                  gnus-no-mark)))
3298          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3299          (gnus-tmp-name
3300           (cond
3301            ((string-match "<[^>]+> *$" gnus-tmp-from)
3302             (let ((beg (match-beginning 0)))
3303               (or (and (string-match "^\".+\"" gnus-tmp-from)
3304                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3305                   (substring gnus-tmp-from 0 beg))))
3306            ((string-match "(.+)" gnus-tmp-from)
3307             (substring gnus-tmp-from
3308                        (1+ (match-beginning 0)) (1- (match-end 0))))
3309            (t gnus-tmp-from)))
3310          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3311          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3312          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3313          (buffer-read-only nil))
3314     (when (string= gnus-tmp-name "")
3315       (setq gnus-tmp-name gnus-tmp-from))
3316     (unless (numberp gnus-tmp-lines)
3317       (setq gnus-tmp-lines -1))
3318     (if (= gnus-tmp-lines -1)
3319         (setq gnus-tmp-lines "?")
3320       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3321       (gnus-put-text-property
3322      (point)
3323      (progn (eval gnus-summary-line-format-spec) (point))
3324        'gnus-number gnus-tmp-number)
3325     (when (gnus-visual-p 'summary-highlight 'highlight)
3326       (forward-line -1)
3327       (gnus-run-hooks 'gnus-summary-update-hook)
3328       (forward-line 1))))
3329
3330 (defun gnus-summary-update-line (&optional dont-update)
3331   "Update summary line after change."
3332   (when (and gnus-summary-default-score
3333              (not gnus-summary-inhibit-highlight))
3334     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3335            (article (gnus-summary-article-number))
3336            (score (gnus-summary-article-score article)))
3337       (unless dont-update
3338         (if (and gnus-summary-mark-below
3339                  (< (gnus-summary-article-score)
3340                     gnus-summary-mark-below))
3341             ;; This article has a low score, so we mark it as read.
3342             (when (memq article gnus-newsgroup-unreads)
3343               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3344           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3345             ;; This article was previously marked as read on account
3346             ;; of a low score, but now it has risen, so we mark it as
3347             ;; unread.
3348             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3349         (gnus-summary-update-mark
3350          (if (or (null gnus-summary-default-score)
3351                  (<= (abs (- score gnus-summary-default-score))
3352                      gnus-summary-zcore-fuzz))
3353              ?                          ;Whitespace
3354            (if (< score gnus-summary-default-score)
3355                gnus-score-below-mark gnus-score-over-mark))
3356          'score))
3357       ;; Do visual highlighting.
3358       (when (gnus-visual-p 'summary-highlight 'highlight)
3359         (gnus-run-hooks 'gnus-summary-update-hook)))))
3360
3361 (defvar gnus-tmp-new-adopts nil)
3362
3363 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3364   "Return the number of articles in THREAD.
3365 This may be 0 in some cases -- if none of the articles in
3366 the thread are to be displayed."
3367   (let* ((number
3368          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3369           (cond
3370            ((not (listp thread))
3371             1)
3372            ((and (consp thread) (cdr thread))
3373             (apply
3374              '+ 1 (mapcar
3375                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3376            ((null thread)
3377             1)
3378            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3379             1)
3380            (t 0))))
3381     (when (and level (zerop level) gnus-tmp-new-adopts)
3382       (incf number
3383             (apply '+ (mapcar
3384                        'gnus-summary-number-of-articles-in-thread
3385                        gnus-tmp-new-adopts))))
3386     (if char
3387         (if (> number 1) gnus-not-empty-thread-mark
3388           gnus-empty-thread-mark)
3389       number)))
3390
3391 (defsubst gnus-summary-line-message-size (head)
3392   "Return pretty-printed version of message size.
3393 This function is intended to be used in
3394 `gnus-summary-line-format-alist'."
3395   (let ((c (or (mail-header-chars head) -1)))
3396     (cond ((< c 0) "n/a")               ; chars not available
3397           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3398           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3399           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3400           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3401
3402
3403 (defun gnus-summary-set-local-parameters (group)
3404   "Go through the local params of GROUP and set all variable specs in that list."
3405   (let ((params (gnus-group-find-parameter group))
3406         (vars '(quit-config))           ; Ignore quit-config.
3407         elem)
3408     (while params
3409       (setq elem (car params)
3410             params (cdr params))
3411       (and (consp elem)                 ; Has to be a cons.
3412            (consp (cdr elem))           ; The cdr has to be a list.
3413            (symbolp (car elem))         ; Has to be a symbol in there.
3414            (not (memq (car elem) vars))
3415            (ignore-errors               ; So we set it.
3416              (push (car elem) vars)
3417              (make-local-variable (car elem))
3418              (set (car elem) (eval (nth 1 elem))))))))
3419
3420 (defun gnus-summary-read-group (group &optional show-all no-article
3421                                       kill-buffer no-display backward
3422                                       select-articles)
3423   "Start reading news in newsgroup GROUP.
3424 If SHOW-ALL is non-nil, already read articles are also listed.
3425 If NO-ARTICLE is non-nil, no article is selected initially.
3426 If NO-DISPLAY, don't generate a summary buffer."
3427   (let (result)
3428     (while (and group
3429                 (null (setq result
3430                             (let ((gnus-auto-select-next nil))
3431                               (or (gnus-summary-read-group-1
3432                                    group show-all no-article
3433                                    kill-buffer no-display
3434                                    select-articles)
3435                                   (setq show-all nil
3436                                         select-articles nil)))))
3437                 (eq gnus-auto-select-next 'quietly))
3438       (set-buffer gnus-group-buffer)
3439       ;; The entry function called above goes to the next
3440       ;; group automatically, so we go two groups back
3441       ;; if we are searching for the previous group.
3442       (when backward
3443         (gnus-group-prev-unread-group 2))
3444       (if (not (equal group (gnus-group-group-name)))
3445           (setq group (gnus-group-group-name))
3446         (setq group nil)))
3447     result))
3448
3449 (defun gnus-summary-read-group-1 (group show-all no-article
3450                                         kill-buffer no-display
3451                                         &optional select-articles)
3452   ;; Killed foreign groups can't be entered.
3453   ;;  (when (and (not (gnus-group-native-p group))
3454   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3455   ;;    (error "Dead non-native groups can't be entered"))
3456   (gnus-message 5 "Retrieving newsgroup: %s..."
3457                 (gnus-group-decoded-name group))
3458   (let* ((new-group (gnus-summary-setup-buffer group))
3459          (quit-config (gnus-group-quit-config group))
3460          (did-select (and new-group (gnus-select-newsgroup
3461                                      group show-all select-articles))))
3462     (cond
3463      ;; This summary buffer exists already, so we just select it.
3464      ((not new-group)
3465       (gnus-set-global-variables)
3466       (when kill-buffer
3467         (gnus-kill-or-deaden-summary kill-buffer))
3468       (gnus-configure-windows 'summary 'force)
3469       (gnus-set-mode-line 'summary)
3470       (gnus-summary-position-point)
3471       (message "")
3472       t)
3473      ;; We couldn't select this group.
3474      ((null did-select)
3475       (when (and (eq major-mode 'gnus-summary-mode)
3476                  (not (equal (current-buffer) kill-buffer)))
3477         (kill-buffer (current-buffer))
3478         (if (not quit-config)
3479             (progn
3480               ;; Update the info -- marks might need to be removed,
3481               ;; for instance.
3482               (gnus-summary-update-info)
3483               (set-buffer gnus-group-buffer)
3484               (gnus-group-jump-to-group group)
3485               (gnus-group-next-unread-group 1))
3486           (gnus-handle-ephemeral-exit quit-config)))
3487       (let ((grpinfo (gnus-get-info group)))
3488         (if (null (gnus-info-read grpinfo))
3489             (gnus-message 3 "Group %s contains no messages"
3490                           (gnus-group-decoded-name group))
3491           (gnus-message 3 "Can't select group")))
3492       nil)
3493      ;; The user did a `C-g' while prompting for number of articles,
3494      ;; so we exit this group.
3495      ((eq did-select 'quit)
3496       (and (eq major-mode 'gnus-summary-mode)
3497            (not (equal (current-buffer) kill-buffer))
3498            (kill-buffer (current-buffer)))
3499       (when kill-buffer
3500         (gnus-kill-or-deaden-summary kill-buffer))
3501       (if (not quit-config)
3502           (progn
3503             (set-buffer gnus-group-buffer)
3504             (gnus-group-jump-to-group group)
3505             (gnus-group-next-unread-group 1)
3506             (gnus-configure-windows 'group 'force))
3507         (gnus-handle-ephemeral-exit quit-config))
3508       ;; Finally signal the quit.
3509       (signal 'quit nil))
3510      ;; The group was successfully selected.
3511      (t
3512       (gnus-set-global-variables)
3513       ;; Save the active value in effect when the group was entered.
3514       (setq gnus-newsgroup-active
3515             (gnus-copy-sequence
3516              (gnus-active gnus-newsgroup-name)))
3517       ;; You can change the summary buffer in some way with this hook.
3518       (gnus-run-hooks 'gnus-select-group-hook)
3519       (gnus-update-format-specifications
3520        nil 'summary 'summary-mode 'summary-dummy)
3521       (gnus-update-summary-mark-positions)
3522       ;; Do score processing.
3523       (when gnus-use-scoring
3524         (gnus-possibly-score-headers))
3525       ;; Check whether to fill in the gaps in the threads.
3526       (when gnus-build-sparse-threads
3527         (gnus-build-sparse-threads))
3528       ;; Find the initial limit.
3529       (if gnus-show-threads
3530           (if show-all
3531               (let ((gnus-newsgroup-dormant nil))
3532                 (gnus-summary-initial-limit show-all))
3533             (gnus-summary-initial-limit show-all))
3534         ;; When unthreaded, all articles are always shown.
3535         (setq gnus-newsgroup-limit
3536               (mapcar
3537                (lambda (header) (mail-header-number header))
3538                gnus-newsgroup-headers)))
3539       ;; Generate the summary buffer.
3540       (unless no-display
3541         (gnus-summary-prepare))
3542       (when gnus-use-trees
3543         (gnus-tree-open group)
3544         (setq gnus-summary-highlight-line-function
3545               'gnus-tree-highlight-article))
3546       ;; If the summary buffer is empty, but there are some low-scored
3547       ;; articles or some excluded dormants, we include these in the
3548       ;; buffer.
3549       (when (and (zerop (buffer-size))
3550                  (not no-display))
3551         (cond (gnus-newsgroup-dormant
3552                (gnus-summary-limit-include-dormant))
3553               ((and gnus-newsgroup-scored show-all)
3554                (gnus-summary-limit-include-expunged t))))
3555       ;; Function `gnus-apply-kill-file' must be called in this hook.
3556       (gnus-run-hooks 'gnus-apply-kill-hook)
3557       (if (and (zerop (buffer-size))
3558                (not no-display))
3559           (progn
3560             ;; This newsgroup is empty.
3561             (gnus-summary-catchup-and-exit nil t)
3562             (gnus-message 6 "No unread news")
3563             (when kill-buffer
3564               (gnus-kill-or-deaden-summary kill-buffer))
3565             ;; Return nil from this function.
3566             nil)
3567         ;; Hide conversation thread subtrees.  We cannot do this in
3568         ;; gnus-summary-prepare-hook since kill processing may not
3569         ;; work with hidden articles.
3570         (gnus-summary-maybe-hide-threads)
3571         (when kill-buffer
3572           (gnus-kill-or-deaden-summary kill-buffer))
3573         (gnus-summary-auto-select-subject)
3574         ;; Show first unread article if requested.
3575         (if (and (not no-article)
3576                  (not no-display)
3577                  gnus-newsgroup-unreads
3578                  gnus-auto-select-first)
3579             (progn
3580               (gnus-configure-windows 'summary)
3581               (let ((art (gnus-summary-article-number)))
3582                 (unless (and (not gnus-plugged)
3583                              (or (memq art gnus-newsgroup-undownloaded)
3584                                  (memq art gnus-newsgroup-downloadable)))
3585                   (gnus-summary-goto-article art))))
3586           ;; Don't select any articles.
3587           (gnus-summary-position-point)
3588           (gnus-configure-windows 'summary 'force)
3589           (gnus-set-mode-line 'summary))
3590         (when (and gnus-auto-center-group
3591                    (get-buffer-window gnus-group-buffer t))
3592           ;; Gotta use windows, because recenter does weird stuff if
3593           ;; the current buffer ain't the displayed window.
3594           (let ((owin (selected-window)))
3595             (select-window (get-buffer-window gnus-group-buffer t))
3596             (when (gnus-group-goto-group group)
3597               (recenter))
3598             (select-window owin)))
3599         ;; Mark this buffer as "prepared".
3600         (setq gnus-newsgroup-prepared t)
3601         (gnus-run-hooks 'gnus-summary-prepared-hook)
3602         (unless (gnus-ephemeral-group-p group)
3603           (gnus-group-update-group group))
3604         t)))))
3605
3606 (defun gnus-summary-auto-select-subject ()
3607   "Select the subject line on initial group entry."
3608   (goto-char (point-min))
3609   (cond
3610    ((eq gnus-auto-select-subject 'best)
3611     (gnus-summary-best-unread-subject))
3612    ((eq gnus-auto-select-subject 'unread)
3613     (gnus-summary-first-unread-subject))
3614    ((eq gnus-auto-select-subject 'unseen)
3615     (gnus-summary-first-unseen-subject))
3616    ((eq gnus-auto-select-subject 'unseen-or-unread)
3617     (gnus-summary-first-unseen-or-unread-subject))
3618    ((eq gnus-auto-select-subject 'first)
3619     ;; Do nothing.
3620     )
3621    ((functionp gnus-auto-select-subject)
3622     (funcall gnus-auto-select-subject))))
3623
3624 (defun gnus-summary-prepare ()
3625   "Generate the summary buffer."
3626   (interactive)
3627   (let ((buffer-read-only nil))
3628     (erase-buffer)
3629     (setq gnus-newsgroup-data nil
3630           gnus-newsgroup-data-reverse nil)
3631     (gnus-run-hooks 'gnus-summary-generate-hook)
3632     ;; Generate the buffer, either with threads or without.
3633     (when gnus-newsgroup-headers
3634       (gnus-summary-prepare-threads
3635        (if gnus-show-threads
3636            (gnus-sort-gathered-threads
3637             (funcall gnus-summary-thread-gathering-function
3638                      (gnus-sort-threads
3639                       (gnus-cut-threads (gnus-make-threads)))))
3640          ;; Unthreaded display.
3641          (gnus-sort-articles gnus-newsgroup-headers))))
3642     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3643     ;; Call hooks for modifying summary buffer.
3644     (goto-char (point-min))
3645     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3646
3647 (defsubst gnus-general-simplify-subject (subject)
3648   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3649   (setq subject
3650         (cond
3651          ;; Truncate the subject.
3652          (gnus-simplify-subject-functions
3653           (gnus-map-function gnus-simplify-subject-functions subject))
3654          ((numberp gnus-summary-gather-subject-limit)
3655           (setq subject (gnus-simplify-subject-re subject))
3656           (if (> (length subject) gnus-summary-gather-subject-limit)
3657               (substring subject 0 gnus-summary-gather-subject-limit)
3658             subject))
3659          ;; Fuzzily simplify it.
3660          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3661           (gnus-simplify-subject-fuzzy subject))
3662          ;; Just remove the leading "Re:".
3663          (t
3664           (gnus-simplify-subject-re subject))))
3665
3666   (if (and gnus-summary-gather-exclude-subject
3667            (string-match gnus-summary-gather-exclude-subject subject))
3668       nil                         ; This article shouldn't be gathered
3669     subject))
3670
3671 (defun gnus-summary-simplify-subject-query ()
3672   "Query where the respool algorithm would put this article."
3673   (interactive)
3674   (gnus-summary-select-article)
3675   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3676
3677 (defun gnus-gather-threads-by-subject (threads)
3678   "Gather threads by looking at Subject headers."
3679   (if (not gnus-summary-make-false-root)
3680       threads
3681     (let ((hashtb (gnus-make-hashtable 1024))
3682           (prev threads)
3683           (result threads)
3684           subject hthread whole-subject)
3685       (while threads
3686         (setq subject (gnus-general-simplify-subject
3687                        (setq whole-subject (mail-header-subject
3688                                             (caar threads)))))
3689         (when subject
3690           (if (setq hthread (gnus-gethash subject hashtb))
3691               (progn
3692                 ;; We enter a dummy root into the thread, if we
3693                 ;; haven't done that already.
3694                 (unless (stringp (caar hthread))
3695                   (setcar hthread (list whole-subject (car hthread))))
3696                 ;; We add this new gathered thread to this gathered
3697                 ;; thread.
3698                 (setcdr (car hthread)
3699                         (nconc (cdar hthread) (list (car threads))))
3700                 ;; Remove it from the list of threads.
3701                 (setcdr prev (cdr threads))
3702                 (setq threads prev))
3703             ;; Enter this thread into the hash table.
3704             (gnus-sethash subject
3705                           (if gnus-summary-make-false-root-always
3706                               (progn
3707                                 ;; If you want a dummy root above all
3708                                 ;; threads...
3709                                 (setcar threads (list whole-subject
3710                                                       (car threads)))
3711                                 threads)
3712                             threads)
3713                           hashtb)))
3714         (setq prev threads)
3715         (setq threads (cdr threads)))
3716       result)))
3717
3718 (defun gnus-gather-threads-by-references (threads)
3719   "Gather threads by looking at References headers."
3720   (let ((idhashtb (gnus-make-hashtable 1024))
3721         (thhashtb (gnus-make-hashtable 1024))
3722         (prev threads)
3723         (result threads)
3724         ids references id gthread gid entered ref)
3725     (while threads
3726       (when (setq references (mail-header-references (caar threads)))
3727         (setq id (mail-header-id (caar threads))
3728               ids (inline (gnus-split-references references))
3729               entered nil)
3730         (while (setq ref (pop ids))
3731           (setq ids (delete ref ids))
3732           (if (not (setq gid (gnus-gethash ref idhashtb)))
3733               (progn
3734                 (gnus-sethash ref id idhashtb)
3735                 (gnus-sethash id threads thhashtb))
3736             (setq gthread (gnus-gethash gid thhashtb))
3737             (unless entered
3738               ;; We enter a dummy root into the thread, if we
3739               ;; haven't done that already.
3740               (unless (stringp (caar gthread))
3741                 (setcar gthread (list (mail-header-subject (caar gthread))
3742                                       (car gthread))))
3743               ;; We add this new gathered thread to this gathered
3744               ;; thread.
3745               (setcdr (car gthread)
3746                       (nconc (cdar gthread) (list (car threads)))))
3747             ;; Add it into the thread hash table.
3748             (gnus-sethash id gthread thhashtb)
3749             (setq entered t)
3750             ;; Remove it from the list of threads.
3751             (setcdr prev (cdr threads))
3752             (setq threads prev))))
3753       (setq prev threads)
3754       (setq threads (cdr threads)))
3755     result))
3756
3757 (defun gnus-sort-gathered-threads (threads)
3758   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3759   (let ((result threads))
3760     (while threads
3761       (when (stringp (caar threads))
3762         (setcdr (car threads)
3763                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3764       (setq threads (cdr threads)))
3765     result))
3766
3767 (defun gnus-thread-loop-p (root thread)
3768   "Say whether ROOT is in THREAD."
3769   (let ((stack (list thread))
3770         (infloop 0)
3771         th)
3772     (while (setq thread (pop stack))
3773       (setq th (cdr thread))
3774       (while (and th
3775                   (not (eq (caar th) root)))
3776         (pop th))
3777       (if th
3778           ;; We have found a loop.
3779           (let (ref-dep)
3780             (setcdr thread (delq (car th) (cdr thread)))
3781             (if (boundp (setq ref-dep (intern "none"
3782                                               gnus-newsgroup-dependencies)))
3783                 (setcdr (symbol-value ref-dep)
3784                         (nconc (cdr (symbol-value ref-dep))
3785                                (list (car th))))
3786               (set ref-dep (list nil (car th))))
3787             (setq infloop 1
3788                   stack nil))
3789         ;; Push all the subthreads onto the stack.
3790         (push (cdr thread) stack)))
3791     infloop))
3792
3793 (defun gnus-make-threads ()
3794   "Go through the dependency hashtb and find the roots.  Return all threads."
3795   (let (threads)
3796     (while (catch 'infloop
3797              (mapatoms
3798               (lambda (refs)
3799                 ;; Deal with self-referencing References loops.
3800                 (when (and (car (symbol-value refs))
3801                            (not (zerop
3802                                  (apply
3803                                   '+
3804                                   (mapcar
3805                                    (lambda (thread)
3806                                      (gnus-thread-loop-p
3807                                       (car (symbol-value refs)) thread))
3808                                    (cdr (symbol-value refs)))))))
3809                   (setq threads nil)
3810                   (throw 'infloop t))
3811                 (unless (car (symbol-value refs))
3812                   ;; These threads do not refer back to any other
3813                   ;; articles, so they're roots.
3814                   (setq threads (append (cdr (symbol-value refs)) threads))))
3815               gnus-newsgroup-dependencies)))
3816     threads))
3817
3818 ;; Build the thread tree.
3819 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3820   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3821
3822 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3823 if it was already present.
3824
3825 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3826 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3827 Message-IDs will be renamed to a unique Message-ID before being
3828 entered.
3829
3830 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3831   (let* ((id (mail-header-id header))
3832          (id-dep (and id (intern id dependencies)))
3833          parent-id ref ref-dep ref-header replaced)
3834     ;; Enter this `header' in the `dependencies' table.
3835     (cond
3836      ((not id-dep)
3837       (setq header nil))
3838      ;; The first two cases do the normal part: enter a new `header'
3839      ;; in the `dependencies' table.
3840      ((not (boundp id-dep))
3841       (set id-dep (list header)))
3842      ((null (car (symbol-value id-dep)))
3843       (setcar (symbol-value id-dep) header))
3844
3845      ;; From here the `header' was already present in the
3846      ;; `dependencies' table.
3847      (force-new
3848       ;; Overrides an existing entry;
3849       ;; just set the header part of the entry.
3850       (setcar (symbol-value id-dep) header)
3851       (setq replaced t))
3852
3853      ;; Renames the existing `header' to a unique Message-ID.
3854      ((not gnus-summary-ignore-duplicates)
3855       ;; An article with this Message-ID has already been seen.
3856       ;; We rename the Message-ID.
3857       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3858            (list header))
3859       (mail-header-set-id header id))
3860
3861      ;; The last case ignores an existing entry, except it adds any
3862      ;; additional Xrefs (in case the two articles came from different
3863      ;; servers.
3864      ;; Also sets `header' to `nil' meaning that the `dependencies'
3865      ;; table was *not* modified.
3866      (t
3867       (mail-header-set-xref
3868        (car (symbol-value id-dep))
3869        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3870                    "")
3871                (or (mail-header-xref header) "")))
3872       (setq header nil)))
3873
3874     (when (and header (not replaced))
3875       ;; First check that we are not creating a References loop.
3876       (setq parent-id (gnus-parent-id (mail-header-references header)))
3877       (setq ref parent-id)
3878       (while (and ref
3879                   (setq ref-dep (intern-soft ref dependencies))
3880                   (boundp ref-dep)
3881                   (setq ref-header (car (symbol-value ref-dep))))
3882         (if (string= id ref)
3883             ;; Yuk!  This is a reference loop.  Make the article be a
3884             ;; root article.
3885             (progn
3886               (mail-header-set-references (car (symbol-value id-dep)) "none")
3887               (setq ref nil)
3888               (setq parent-id nil))
3889           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3890       (setq ref-dep (intern (or parent-id "none") dependencies))
3891       (if (boundp ref-dep)
3892           (setcdr (symbol-value ref-dep)
3893                   (nconc (cdr (symbol-value ref-dep))
3894                          (list (symbol-value id-dep))))
3895         (set ref-dep (list nil (symbol-value id-dep)))))
3896     header))
3897
3898 (defun gnus-extract-message-id-from-in-reply-to (string)
3899   (if (string-match "<[^>]+>" string)
3900       (substring string (match-beginning 0) (match-end 0))
3901     nil))
3902
3903 (defun gnus-build-sparse-threads ()
3904   (let ((headers gnus-newsgroup-headers)
3905         (mail-parse-charset gnus-newsgroup-charset)
3906         (gnus-summary-ignore-duplicates t)
3907         header references generation relations
3908         subject child end new-child date)
3909     ;; First we create an alist of generations/relations, where
3910     ;; generations is how much we trust the relation, and the relation
3911     ;; is parent/child.
3912     (gnus-message 7 "Making sparse threads...")
3913     (save-excursion
3914       (nnheader-set-temp-buffer " *gnus sparse threads*")
3915       (while (setq header (pop headers))
3916         (when (and (setq references (mail-header-references header))
3917                    (not (string= references "")))
3918           (insert references)
3919           (setq child (mail-header-id header)
3920                 subject (mail-header-subject header)
3921                 date (mail-header-date header)
3922                 generation 0)
3923           (while (search-backward ">" nil t)
3924             (setq end (1+ (point)))
3925             (when (search-backward "<" nil t)
3926               (setq new-child (buffer-substring (point) end))
3927               (push (list (incf generation)
3928                           child (setq child new-child)
3929                           subject date)
3930                     relations)))
3931           (when child
3932             (push (list (1+ generation) child nil subject) relations))
3933           (erase-buffer)))
3934       (kill-buffer (current-buffer)))
3935     ;; Sort over trustworthiness.
3936     (mapcar
3937      (lambda (relation)
3938        (when (gnus-dependencies-add-header
3939               (make-full-mail-header
3940                gnus-reffed-article-number
3941                (nth 3 relation) "" (or (nth 4 relation) "")
3942                (nth 1 relation)
3943                (or (nth 2 relation) "") 0 0 "")
3944               gnus-newsgroup-dependencies nil)
3945          (push gnus-reffed-article-number gnus-newsgroup-limit)
3946          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3947          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3948                gnus-newsgroup-reads)
3949          (decf gnus-reffed-article-number)))
3950      (sort relations 'car-less-than-car))
3951     (gnus-message 7 "Making sparse threads...done")))
3952
3953 (defun gnus-build-old-threads ()
3954   ;; Look at all the articles that refer back to old articles, and
3955   ;; fetch the headers for the articles that aren't there.  This will
3956   ;; build complete threads - if the roots haven't been expired by the
3957   ;; server, that is.
3958   (let ((mail-parse-charset gnus-newsgroup-charset)
3959         id heads)
3960     (mapatoms
3961      (lambda (refs)
3962        (when (not (car (symbol-value refs)))
3963          (setq heads (cdr (symbol-value refs)))
3964          (while heads
3965            (if (memq (mail-header-number (caar heads))
3966                      gnus-newsgroup-dormant)
3967                (setq heads (cdr heads))
3968              (setq id (symbol-name refs))
3969              (while (and (setq id (gnus-build-get-header id))
3970                          (not (car (gnus-id-to-thread id)))))
3971              (setq heads nil)))))
3972      gnus-newsgroup-dependencies)))
3973
3974 ;; This function has to be called with point after the article number
3975 ;; on the beginning of the line.
3976 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3977   (let ((eol (gnus-point-at-eol))
3978         (buffer (current-buffer))
3979         header references in-reply-to)
3980
3981     ;; overview: [num subject from date id refs chars lines misc]
3982     (unwind-protect
3983         (let (x)
3984           (narrow-to-region (point) eol)
3985           (unless (eobp)
3986             (forward-char))
3987
3988           (setq header
3989                 (make-full-mail-header
3990                  number                 ; number
3991                  (condition-case ()     ; subject
3992                      (funcall gnus-decode-encoded-word-function
3993                               (setq x (nnheader-nov-field)))
3994                    (error x))
3995                  (condition-case ()     ; from
3996                      (funcall gnus-decode-encoded-word-function
3997                               (setq x (nnheader-nov-field)))
3998                    (error x))
3999                  (nnheader-nov-field)   ; date
4000                  (nnheader-nov-read-message-id) ; id
4001                  (setq references (nnheader-nov-field)) ; refs
4002                  (nnheader-nov-read-integer) ; chars
4003                  (nnheader-nov-read-integer) ; lines
4004                  (unless (eobp)
4005                    (if (looking-at "Xref: ")
4006                        (goto-char (match-end 0)))
4007                    (nnheader-nov-field)) ; Xref
4008                  (nnheader-nov-parse-extra)))) ; extra
4009
4010       (widen))
4011
4012     (when (and (string= references "")
4013                (setq in-reply-to (mail-header-extra header))
4014                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4015       (mail-header-set-references
4016        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4017
4018     (when gnus-alter-header-function
4019       (funcall gnus-alter-header-function header))
4020     (gnus-dependencies-add-header header dependencies force-new)))
4021
4022 (defun gnus-build-get-header (id)
4023   "Look through the buffer of NOV lines and find the header to ID.
4024 Enter this line into the dependencies hash table, and return
4025 the id of the parent article (if any)."
4026   (let ((deps gnus-newsgroup-dependencies)
4027         found header)
4028     (prog1
4029         (save-excursion
4030           (set-buffer nntp-server-buffer)
4031           (let ((case-fold-search nil))
4032             (goto-char (point-min))
4033             (while (and (not found)
4034                         (search-forward id nil t))
4035               (beginning-of-line)
4036               (setq found (looking-at
4037                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4038                                    (regexp-quote id))))
4039               (or found (beginning-of-line 2)))
4040             (when found
4041               (beginning-of-line)
4042               (and
4043                (setq header (gnus-nov-parse-line
4044                              (read (current-buffer)) deps))
4045                (gnus-parent-id (mail-header-references header))))))
4046       (when header
4047         (let ((number (mail-header-number header)))
4048           (push number gnus-newsgroup-limit)
4049           (push header gnus-newsgroup-headers)
4050           (if (memq number gnus-newsgroup-unselected)
4051               (progn
4052                 (setq gnus-newsgroup-unreads
4053                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4054                                                number))
4055                 (setq gnus-newsgroup-unselected
4056                       (delq number gnus-newsgroup-unselected)))
4057             (push number gnus-newsgroup-ancient)))))))
4058
4059 (defun gnus-build-all-threads ()
4060   "Read all the headers."
4061   (let ((gnus-summary-ignore-duplicates t)
4062         (mail-parse-charset gnus-newsgroup-charset)
4063         (dependencies gnus-newsgroup-dependencies)
4064         header article)
4065     (save-excursion
4066       (set-buffer nntp-server-buffer)
4067       (let ((case-fold-search nil))
4068         (goto-char (point-min))
4069         (while (not (eobp))
4070           (ignore-errors
4071             (setq article (read (current-buffer))
4072                   header (gnus-nov-parse-line article dependencies)))
4073           (when header
4074             (save-excursion
4075               (set-buffer gnus-summary-buffer)
4076               (push header gnus-newsgroup-headers)
4077               (if (memq (setq article (mail-header-number header))
4078                         gnus-newsgroup-unselected)
4079                   (progn
4080                     (setq gnus-newsgroup-unreads
4081                           (gnus-add-to-sorted-list
4082                            gnus-newsgroup-unreads article))
4083                     (setq gnus-newsgroup-unselected
4084                           (delq article gnus-newsgroup-unselected)))
4085                 (push article gnus-newsgroup-ancient)))
4086             (forward-line 1)))))))
4087
4088 (defun gnus-summary-update-article-line (article header)
4089   "Update the line for ARTICLE using HEADERS."
4090   (let* ((id (mail-header-id header))
4091          (thread (gnus-id-to-thread id)))
4092     (unless thread
4093       (error "Article in no thread"))
4094     ;; Update the thread.
4095     (setcar thread header)
4096     (gnus-summary-goto-subject article)
4097     (let* ((datal (gnus-data-find-list article))
4098            (data (car datal))
4099            (buffer-read-only nil)
4100            (level (gnus-summary-thread-level)))
4101       (gnus-delete-line)
4102       (let ((inserted (- (point)
4103                          (progn
4104                            (gnus-summary-insert-line
4105                             header level nil 
4106                             (memq article gnus-newsgroup-undownloaded)
4107                             (gnus-article-mark article)
4108                             (memq article gnus-newsgroup-replied)
4109                             (memq article gnus-newsgroup-expirable)
4110                             ;; Only insert the Subject string when it's different
4111                             ;; from the previous Subject string.
4112                             (if (and
4113                                  gnus-show-threads
4114                                  (gnus-subject-equal
4115                                   (condition-case ()
4116                                       (mail-header-subject
4117                                        (gnus-data-header
4118                                         (cadr
4119                                          (gnus-data-find-list
4120                                           article
4121                                           (gnus-data-list t)))))
4122                                     ;; Error on the side of excessive subjects.
4123                                     (error ""))
4124                                   (mail-header-subject header)))
4125                                 ""
4126                               (mail-header-subject header))
4127                             nil (cdr (assq article gnus-newsgroup-scored))
4128                             (memq article gnus-newsgroup-processable))
4129                            (point)))))
4130         (when (cdr datal)
4131           (gnus-data-update-list
4132            (cdr datal) 
4133            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4134
4135 (defun gnus-summary-update-article (article &optional iheader)
4136   "Update ARTICLE in the summary buffer."
4137   (set-buffer gnus-summary-buffer)
4138   (let* ((header (gnus-summary-article-header article))
4139          (id (mail-header-id header))
4140          (data (gnus-data-find article))
4141          (thread (gnus-id-to-thread id))
4142          (references (mail-header-references header))
4143          (parent
4144           (gnus-id-to-thread
4145            (or (gnus-parent-id
4146                 (when (and references
4147                            (not (equal "" references)))
4148                   references))
4149                "none")))
4150          (buffer-read-only nil)
4151          (old (car thread)))
4152     (when thread
4153       (unless iheader
4154         (setcar thread nil)
4155         (when parent
4156           (delq thread parent)))
4157       (if (gnus-summary-insert-subject id header)
4158           ;; Set the (possibly) new article number in the data structure.
4159           (gnus-data-set-number data (gnus-id-to-article id))
4160         (setcar thread old)
4161         nil))))
4162
4163 (defun gnus-rebuild-thread (id &optional line)
4164   "Rebuild the thread containing ID.
4165 If LINE, insert the rebuilt thread starting on line LINE."
4166   (let ((buffer-read-only nil)
4167         old-pos current thread data)
4168     (if (not gnus-show-threads)
4169         (setq thread (list (car (gnus-id-to-thread id))))
4170       ;; Get the thread this article is part of.
4171       (setq thread (gnus-remove-thread id)))
4172     (setq old-pos (gnus-point-at-bol))
4173     (setq current (save-excursion
4174                     (and (re-search-backward "[\r\n]" nil t)
4175                          (gnus-summary-article-number))))
4176     ;; If this is a gathered thread, we have to go some re-gathering.
4177     (when (stringp (car thread))
4178       (let ((subject (car thread))
4179             roots thr)
4180         (setq thread (cdr thread))
4181         (while thread
4182           (unless (memq (setq thr (gnus-id-to-thread
4183                                    (gnus-root-id
4184                                     (mail-header-id (caar thread)))))
4185                         roots)
4186             (push thr roots))
4187           (setq thread (cdr thread)))
4188         ;; We now have all (unique) roots.
4189         (if (= (length roots) 1)
4190             ;; All the loose roots are now one solid root.
4191             (setq thread (car roots))
4192           (setq thread (cons subject (gnus-sort-threads roots))))))
4193     (let (threads)
4194       ;; We then insert this thread into the summary buffer.
4195       (when line
4196         (goto-char (point-min))
4197         (forward-line (1- line)))
4198       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4199         (if gnus-show-threads
4200             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4201           (gnus-summary-prepare-unthreaded thread))
4202         (setq data (nreverse gnus-newsgroup-data))
4203         (setq threads gnus-newsgroup-threads))
4204       ;; We splice the new data into the data structure.
4205       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4206       ;;!!! then we want to insert at the beginning of the buffer.
4207       ;;!!! That happens to be true with Gnus now, but that may
4208       ;;!!! change in the future.  Perhaps.
4209       (gnus-data-enter-list
4210        (if line nil current) data (- (point) old-pos))
4211       (setq gnus-newsgroup-threads
4212             (nconc threads gnus-newsgroup-threads))
4213       (gnus-data-compute-positions))))
4214
4215 (defun gnus-number-to-header (number)
4216   "Return the header for article NUMBER."
4217   (let ((headers gnus-newsgroup-headers))
4218     (while (and headers
4219                 (not (= number (mail-header-number (car headers)))))
4220       (pop headers))
4221     (when headers
4222       (car headers))))
4223
4224 (defun gnus-parent-headers (in-headers &optional generation)
4225   "Return the headers of the GENERATIONeth parent of HEADERS."
4226   (unless generation
4227     (setq generation 1))
4228   (let ((parent t)
4229         (headers in-headers)
4230         references)
4231     (while (and parent
4232                 (not (zerop generation))
4233                 (setq references (mail-header-references headers)))
4234       (setq headers (if (and references
4235                              (setq parent (gnus-parent-id references)))
4236                         (car (gnus-id-to-thread parent))
4237                       nil))
4238       (decf generation))
4239     (and (not (eq headers in-headers))
4240          headers)))
4241
4242 (defun gnus-id-to-thread (id)
4243   "Return the (sub-)thread where ID appears."
4244   (gnus-gethash id gnus-newsgroup-dependencies))
4245
4246 (defun gnus-id-to-article (id)
4247   "Return the article number of ID."
4248   (let ((thread (gnus-id-to-thread id)))
4249     (when (and thread
4250                (car thread))
4251       (mail-header-number (car thread)))))
4252
4253 (defun gnus-id-to-header (id)
4254   "Return the article headers of ID."
4255   (car (gnus-id-to-thread id)))
4256
4257 (defun gnus-article-displayed-root-p (article)
4258   "Say whether ARTICLE is a root(ish) article."
4259   (let ((level (gnus-summary-thread-level article))
4260         (refs (mail-header-references  (gnus-summary-article-header article)))
4261         particle)
4262     (cond
4263      ((null level) nil)
4264      ((zerop level) t)
4265      ((null refs) t)
4266      ((null (gnus-parent-id refs)) t)
4267      ((and (= 1 level)
4268            (null (setq particle (gnus-id-to-article
4269                                  (gnus-parent-id refs))))
4270            (null (gnus-summary-thread-level particle)))))))
4271
4272 (defun gnus-root-id (id)
4273   "Return the id of the root of the thread where ID appears."
4274   (let (last-id prev)
4275     (while (and id (setq prev (car (gnus-id-to-thread id))))
4276       (setq last-id id
4277             id (gnus-parent-id (mail-header-references prev))))
4278     last-id))
4279
4280 (defun gnus-articles-in-thread (thread)
4281   "Return the list of articles in THREAD."
4282   (cons (mail-header-number (car thread))
4283         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4284
4285 (defun gnus-remove-thread (id &optional dont-remove)
4286   "Remove the thread that has ID in it."
4287   (let (headers thread last-id)
4288     ;; First go up in this thread until we find the root.
4289     (setq last-id (gnus-root-id id)
4290           headers (message-flatten-list (gnus-id-to-thread last-id)))
4291     ;; We have now found the real root of this thread.  It might have
4292     ;; been gathered into some loose thread, so we have to search
4293     ;; through the threads to find the thread we wanted.
4294     (let ((threads gnus-newsgroup-threads)
4295           sub)
4296       (while threads
4297         (setq sub (car threads))
4298         (if (stringp (car sub))
4299             ;; This is a gathered thread, so we look at the roots
4300             ;; below it to find whether this article is in this
4301             ;; gathered root.
4302             (progn
4303               (setq sub (cdr sub))
4304               (while sub
4305                 (when (member (caar sub) headers)
4306                   (setq thread (car threads)
4307                         threads nil
4308                         sub nil))
4309                 (setq sub (cdr sub))))
4310           ;; It's an ordinary thread, so we check it.
4311           (when (eq (car sub) (car headers))
4312             (setq thread sub
4313                   threads nil)))
4314         (setq threads (cdr threads)))
4315       ;; If this article is in no thread, then it's a root.
4316       (if thread
4317           (unless dont-remove
4318             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4319         (setq thread (gnus-id-to-thread last-id)))
4320       (when thread
4321         (prog1
4322             thread                      ; We return this thread.
4323           (unless dont-remove
4324             (if (stringp (car thread))
4325                 (progn
4326                   ;; If we use dummy roots, then we have to remove the
4327                   ;; dummy root as well.
4328                   (when (eq gnus-summary-make-false-root 'dummy)
4329                     ;; We go to the dummy root by going to
4330                     ;; the first sub-"thread", and then one line up.
4331                     (gnus-summary-goto-article
4332                      (mail-header-number (caadr thread)))
4333                     (forward-line -1)
4334                     (gnus-delete-line)
4335                     (gnus-data-compute-positions))
4336                   (setq thread (cdr thread))
4337                   (while thread
4338                     (gnus-remove-thread-1 (car thread))
4339                     (setq thread (cdr thread))))
4340               (gnus-remove-thread-1 thread))))))))
4341
4342 (defun gnus-remove-thread-1 (thread)
4343   "Remove the thread THREAD recursively."
4344   (let ((number (mail-header-number (pop thread)))
4345         d)
4346     (setq thread (reverse thread))
4347     (while thread
4348       (gnus-remove-thread-1 (pop thread)))
4349     (when (setq d (gnus-data-find number))
4350       (goto-char (gnus-data-pos d))
4351       (gnus-summary-show-thread)
4352       (gnus-data-remove
4353        number
4354        (- (gnus-point-at-bol)
4355           (prog1
4356               (1+ (gnus-point-at-eol))
4357             (gnus-delete-line)))))))
4358
4359 (defun gnus-sort-threads-1 (threads func)
4360   (sort (mapcar (lambda (thread)
4361                   (cons (car thread)
4362                         (and (cdr thread)
4363                              (gnus-sort-threads-1 (cdr thread) func))))
4364                 threads) func))
4365
4366 (defun gnus-sort-threads (threads)
4367   "Sort THREADS."
4368   (if (not gnus-thread-sort-functions)
4369       threads
4370     (gnus-message 8 "Sorting threads...")
4371     (let ((max-lisp-eval-depth 5000))
4372       (prog1 (gnus-sort-threads-1
4373          threads
4374          (gnus-make-sort-function gnus-thread-sort-functions))
4375         (gnus-message 8 "Sorting threads...done")))))
4376
4377 (defun gnus-sort-articles (articles)
4378   "Sort ARTICLES."
4379   (when gnus-article-sort-functions
4380     (gnus-message 7 "Sorting articles...")
4381     (prog1
4382         (setq gnus-newsgroup-headers
4383               (sort articles (gnus-make-sort-function
4384                               gnus-article-sort-functions)))
4385       (gnus-message 7 "Sorting articles...done"))))
4386
4387 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4388 (defmacro gnus-thread-header (thread)
4389   "Return header of first article in THREAD.
4390 Note that THREAD must never, ever be anything else than a variable -
4391 using some other form will lead to serious barfage."
4392   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4393   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4394   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4395         (vector thread) 2))
4396
4397 (defsubst gnus-article-sort-by-number (h1 h2)
4398   "Sort articles by article number."
4399   (< (mail-header-number h1)
4400      (mail-header-number h2)))
4401
4402 (defun gnus-thread-sort-by-number (h1 h2)
4403   "Sort threads by root article number."
4404   (gnus-article-sort-by-number
4405    (gnus-thread-header h1) (gnus-thread-header h2)))
4406
4407 (defsubst gnus-article-sort-by-random (h1 h2)
4408   "Sort articles by article number."
4409   (zerop (random 2)))
4410
4411 (defun gnus-thread-sort-by-random (h1 h2)
4412   "Sort threads by root article number."
4413   (gnus-article-sort-by-random
4414    (gnus-thread-header h1) (gnus-thread-header h2)))
4415
4416 (defsubst gnus-article-sort-by-lines (h1 h2)
4417   "Sort articles by article Lines header."
4418   (< (mail-header-lines h1)
4419      (mail-header-lines h2)))
4420
4421 (defun gnus-thread-sort-by-lines (h1 h2)
4422   "Sort threads by root article Lines header."
4423   (gnus-article-sort-by-lines
4424    (gnus-thread-header h1) (gnus-thread-header h2)))
4425
4426 (defsubst gnus-article-sort-by-chars (h1 h2)
4427   "Sort articles by octet length."
4428   (< (mail-header-chars h1)
4429      (mail-header-chars h2)))
4430
4431 (defun gnus-thread-sort-by-chars (h1 h2)
4432   "Sort threads by root article octet length."
4433   (gnus-article-sort-by-chars
4434    (gnus-thread-header h1) (gnus-thread-header h2)))
4435
4436 (defsubst gnus-article-sort-by-author (h1 h2)
4437   "Sort articles by root author."
4438   (string-lessp
4439    (let ((extract (funcall
4440                    gnus-extract-address-components
4441                    (mail-header-from h1))))
4442      (or (car extract) (cadr extract) ""))
4443    (let ((extract (funcall
4444                    gnus-extract-address-components
4445                    (mail-header-from h2))))
4446      (or (car extract) (cadr extract) ""))))
4447
4448 (defun gnus-thread-sort-by-author (h1 h2)
4449   "Sort threads by root author."
4450   (gnus-article-sort-by-author
4451    (gnus-thread-header h1)  (gnus-thread-header h2)))
4452
4453 (defsubst gnus-article-sort-by-subject (h1 h2)
4454   "Sort articles by root subject."
4455   (string-lessp
4456    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4457    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4458
4459 (defun gnus-thread-sort-by-subject (h1 h2)
4460   "Sort threads by root subject."
4461   (gnus-article-sort-by-subject
4462    (gnus-thread-header h1) (gnus-thread-header h2)))
4463
4464 (defsubst gnus-article-sort-by-date (h1 h2)
4465   "Sort articles by root article date."
4466   (time-less-p
4467    (gnus-date-get-time (mail-header-date h1))
4468    (gnus-date-get-time (mail-header-date h2))))
4469
4470 (defun gnus-thread-sort-by-date (h1 h2)
4471   "Sort threads by root article date."
4472   (gnus-article-sort-by-date
4473    (gnus-thread-header h1) (gnus-thread-header h2)))
4474
4475 (defsubst gnus-article-sort-by-score (h1 h2)
4476   "Sort articles by root article score.
4477 Unscored articles will be counted as having a score of zero."
4478   (> (or (cdr (assq (mail-header-number h1)
4479                     gnus-newsgroup-scored))
4480          gnus-summary-default-score 0)
4481      (or (cdr (assq (mail-header-number h2)
4482                     gnus-newsgroup-scored))
4483          gnus-summary-default-score 0)))
4484
4485 (defun gnus-thread-sort-by-score (h1 h2)
4486   "Sort threads by root article score."
4487   (gnus-article-sort-by-score
4488    (gnus-thread-header h1) (gnus-thread-header h2)))
4489
4490 (defun gnus-thread-sort-by-total-score (h1 h2)
4491   "Sort threads by the sum of all scores in the thread.
4492 Unscored articles will be counted as having a score of zero."
4493   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4494
4495 (defun gnus-thread-total-score (thread)
4496   ;; This function find the total score of THREAD.
4497   (cond
4498    ((null thread)
4499     0)
4500    ((consp thread)
4501     (if (stringp (car thread))
4502         (apply gnus-thread-score-function 0
4503                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4504       (gnus-thread-total-score-1 thread)))
4505    (t
4506     (gnus-thread-total-score-1 (list thread)))))
4507
4508 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4509   "Sort threads such that the thread with the most recently arrived article comes first."
4510   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4511
4512 (defun gnus-thread-highest-number (thread)
4513   "Return the highest article number in THREAD."
4514   (apply 'max (mapcar (lambda (header)
4515                         (mail-header-number header))
4516                       (message-flatten-list thread))))
4517
4518 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4519   "Sort threads such that the thread with the most recently dated article comes first."
4520   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4521
4522 (defun gnus-thread-latest-date (thread)
4523   "Return the highest article date in THREAD."
4524   (let ((previous-time 0))
4525     (apply 'max
4526            (mapcar
4527             (lambda (header)
4528               (setq previous-time
4529                     (time-to-seconds
4530                      (condition-case ()
4531                          (mail-header-parse-date (mail-header-date header))
4532                        (error previous-time)))))
4533             (sort
4534              (message-flatten-list thread)
4535              (lambda (h1 h2)
4536                (< (mail-header-number h1)
4537                   (mail-header-number h2))))))))
4538
4539 (defun gnus-thread-total-score-1 (root)
4540   ;; This function find the total score of the thread below ROOT.
4541   (setq root (car root))
4542   (apply gnus-thread-score-function
4543          (or (append
4544               (mapcar 'gnus-thread-total-score
4545                       (cdr (gnus-id-to-thread (mail-header-id root))))
4546               (when (> (mail-header-number root) 0)
4547                 (list (or (cdr (assq (mail-header-number root)
4548                                      gnus-newsgroup-scored))
4549                           gnus-summary-default-score 0))))
4550              (list gnus-summary-default-score)
4551              '(0))))
4552
4553 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4554 (defvar gnus-tmp-prev-subject nil)
4555 (defvar gnus-tmp-false-parent nil)
4556 (defvar gnus-tmp-root-expunged nil)
4557 (defvar gnus-tmp-dummy-line nil)
4558
4559 (eval-when-compile (defvar gnus-tmp-header))
4560 (defun gnus-extra-header (type &optional header)
4561   "Return the extra header of TYPE."
4562   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4563       ""))
4564
4565 (defvar gnus-tmp-thread-tree-header-string "")
4566
4567 (defcustom gnus-sum-thread-tree-root "> "
4568   "With %B spec, used for the root of a thread.
4569 If nil, use subject instead."
4570   :type 'string
4571   :group 'gnus-thread)
4572 (defcustom gnus-sum-thread-tree-false-root "> "
4573   "With %B spec, used for a false root of a thread.
4574 If nil, use subject instead."
4575   :type 'string
4576   :group 'gnus-thread)
4577 (defcustom gnus-sum-thread-tree-single-indent ""
4578   "With %B spec, used for a thread with just one message.
4579 If nil, use subject instead."
4580   :type 'string
4581   :group 'gnus-thread)
4582 (defcustom gnus-sum-thread-tree-vertical "| "
4583   "With %B spec, used for drawing a vertical line."
4584   :type 'string
4585   :group 'gnus-thread)
4586 (defcustom gnus-sum-thread-tree-indent "  "
4587   "With %B spec, used for indenting."
4588   :type 'string
4589   :group 'gnus-thread)
4590 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4591   "With %B spec, used for a leaf with brothers."
4592   :type 'string
4593   :group 'gnus-thread)
4594 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4595   "With %B spec, used for a leaf without brothers."
4596   :type 'string
4597   :group 'gnus-thread)
4598
4599 (defun gnus-summary-prepare-threads (threads)
4600   "Prepare summary buffer from THREADS and indentation LEVEL.
4601 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4602 or a straight list of headers."
4603   (gnus-message 7 "Generating summary...")
4604
4605   (setq gnus-newsgroup-threads threads)
4606   (beginning-of-line)
4607
4608   (let ((gnus-tmp-level 0)
4609         (default-score (or gnus-summary-default-score 0))
4610         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4611         (building-line-count gnus-summary-display-while-building)
4612         (building-count (integerp gnus-summary-display-while-building))
4613         thread number subject stack state gnus-tmp-gathered beg-match
4614         new-roots gnus-tmp-new-adopts thread-end simp-subject
4615         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4616         gnus-tmp-replied gnus-tmp-subject-or-nil
4617         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4618         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4619         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4620         tree-stack)
4621
4622     (setq gnus-tmp-prev-subject nil
4623           gnus-tmp-thread-tree-header-string "")
4624
4625     (if (vectorp (car threads))
4626         ;; If this is a straight (sic) list of headers, then a
4627         ;; threaded summary display isn't required, so we just create
4628         ;; an unthreaded one.
4629         (gnus-summary-prepare-unthreaded threads)
4630
4631       ;; Do the threaded display.
4632
4633       (if gnus-summary-display-while-building
4634           (switch-to-buffer (buffer-name)))
4635       (while (or threads stack gnus-tmp-new-adopts new-roots)
4636
4637         (if (and (= gnus-tmp-level 0)
4638                  (or (not stack)
4639                      (= (caar stack) 0))
4640                  (not gnus-tmp-false-parent)
4641                  (or gnus-tmp-new-adopts new-roots))
4642             (if gnus-tmp-new-adopts
4643                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4644                       thread (list (car gnus-tmp-new-adopts))
4645                       gnus-tmp-header (caar thread)
4646                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4647               (when new-roots
4648                 (setq thread (list (car new-roots))
4649                       gnus-tmp-header (caar thread)
4650                       new-roots (cdr new-roots))))
4651
4652           (if threads
4653               ;; If there are some threads, we do them before the
4654               ;; threads on the stack.
4655               (setq thread threads
4656                     gnus-tmp-header (caar thread))
4657             ;; There were no current threads, so we pop something off
4658             ;; the stack.
4659             (setq state (car stack)
4660                   gnus-tmp-level (car state)
4661                   tree-stack (cadr state)
4662                   thread (caddr state)
4663                   stack (cdr stack)
4664                   gnus-tmp-header (caar thread))))
4665
4666         (setq gnus-tmp-false-parent nil)
4667         (setq gnus-tmp-root-expunged nil)
4668         (setq thread-end nil)
4669
4670         (if (stringp gnus-tmp-header)
4671             ;; The header is a dummy root.
4672             (cond
4673              ((eq gnus-summary-make-false-root 'adopt)
4674               ;; We let the first article adopt the rest.
4675               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4676                                                (cddar thread)))
4677               (setq gnus-tmp-gathered
4678                     (nconc (mapcar
4679                             (lambda (h) (mail-header-number (car h)))
4680                             (cddar thread))
4681                            gnus-tmp-gathered))
4682               (setq thread (cons (list (caar thread)
4683                                        (cadar thread))
4684                                  (cdr thread)))
4685               (setq gnus-tmp-level -1
4686                     gnus-tmp-false-parent t))
4687              ((eq gnus-summary-make-false-root 'empty)
4688               ;; We print adopted articles with empty subject fields.
4689               (setq gnus-tmp-gathered
4690                     (nconc (mapcar
4691                             (lambda (h) (mail-header-number (car h)))
4692                             (cddar thread))
4693                            gnus-tmp-gathered))
4694               (setq gnus-tmp-level -1))
4695              ((eq gnus-summary-make-false-root 'dummy)
4696               ;; We remember that we probably want to output a dummy
4697               ;; root.
4698               (setq gnus-tmp-dummy-line gnus-tmp-header)
4699               (setq gnus-tmp-prev-subject gnus-tmp-header))
4700              (t
4701               ;; We do not make a root for the gathered
4702               ;; sub-threads at all.
4703               (setq gnus-tmp-level -1)))
4704
4705           (setq number (mail-header-number gnus-tmp-header)
4706                 subject (mail-header-subject gnus-tmp-header)
4707                 simp-subject (gnus-simplify-subject-fully subject))
4708
4709           (cond
4710            ;; If the thread has changed subject, we might want to make
4711            ;; this subthread into a root.
4712            ((and (null gnus-thread-ignore-subject)
4713                  (not (zerop gnus-tmp-level))
4714                  gnus-tmp-prev-subject
4715                  (not (string= gnus-tmp-prev-subject simp-subject)))
4716             (setq new-roots (nconc new-roots (list (car thread)))
4717                   thread-end t
4718                   gnus-tmp-header nil))
4719            ;; If the article lies outside the current limit,
4720            ;; then we do not display it.
4721            ((not (memq number gnus-newsgroup-limit))
4722             (setq gnus-tmp-gathered
4723                   (nconc (mapcar
4724                           (lambda (h) (mail-header-number (car h)))
4725                           (cdar thread))
4726                          gnus-tmp-gathered))
4727             (setq gnus-tmp-new-adopts (if (cdar thread)
4728                                           (append gnus-tmp-new-adopts
4729                                                   (cdar thread))
4730                                         gnus-tmp-new-adopts)
4731                   thread-end t
4732                   gnus-tmp-header nil)
4733             (when (zerop gnus-tmp-level)
4734               (setq gnus-tmp-root-expunged t)))
4735            ;; Perhaps this article is to be marked as read?
4736            ((and gnus-summary-mark-below
4737                  (< (or (cdr (assq number gnus-newsgroup-scored))
4738                         default-score)
4739                     gnus-summary-mark-below)
4740                  ;; Don't touch sparse articles.
4741                  (not (gnus-summary-article-sparse-p number))
4742                  (not (gnus-summary-article-ancient-p number)))
4743             (setq gnus-newsgroup-unreads
4744                   (delq number gnus-newsgroup-unreads))
4745             (if gnus-newsgroup-auto-expire
4746                 (setq gnus-newsgroup-expirable
4747                       (gnus-add-to-sorted-list
4748                        gnus-newsgroup-expirable number))
4749               (push (cons number gnus-low-score-mark)
4750                     gnus-newsgroup-reads))))
4751
4752           (when gnus-tmp-header
4753             ;; We may have an old dummy line to output before this
4754             ;; article.
4755             (when (and gnus-tmp-dummy-line
4756                        (gnus-subject-equal
4757                         gnus-tmp-dummy-line
4758                         (mail-header-subject gnus-tmp-header)))
4759               (gnus-summary-insert-dummy-line
4760                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4761               (setq gnus-tmp-dummy-line nil))
4762
4763             ;; Compute the mark.
4764             (setq gnus-tmp-unread (gnus-article-mark number))
4765
4766             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4767                                   gnus-tmp-header gnus-tmp-level)
4768                   gnus-newsgroup-data)
4769
4770             ;; Actually insert the line.
4771             (setq
4772              gnus-tmp-subject-or-nil
4773              (cond
4774               ((and gnus-thread-ignore-subject
4775                     gnus-tmp-prev-subject
4776                     (not (string= gnus-tmp-prev-subject simp-subject)))
4777                subject)
4778               ((zerop gnus-tmp-level)
4779                (if (and (eq gnus-summary-make-false-root 'empty)
4780                         (memq number gnus-tmp-gathered)
4781                         gnus-tmp-prev-subject
4782                         (string= gnus-tmp-prev-subject simp-subject))
4783                    gnus-summary-same-subject
4784                  subject))
4785               (t gnus-summary-same-subject)))
4786             (if (and (eq gnus-summary-make-false-root 'adopt)
4787                      (= gnus-tmp-level 1)
4788                      (memq number gnus-tmp-gathered))
4789                 (setq gnus-tmp-opening-bracket ?\<
4790                       gnus-tmp-closing-bracket ?\>)
4791               (setq gnus-tmp-opening-bracket ?\[
4792                     gnus-tmp-closing-bracket ?\]))
4793             (setq
4794              gnus-tmp-indentation
4795              (aref gnus-thread-indent-array gnus-tmp-level)
4796              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4797              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4798                                 gnus-summary-default-score 0)
4799              gnus-tmp-score-char
4800              (if (or (null gnus-summary-default-score)
4801                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4802                          gnus-summary-zcore-fuzz))
4803                  ?                      ;Whitespace
4804                (if (< gnus-tmp-score gnus-summary-default-score)
4805                    gnus-score-below-mark gnus-score-over-mark))
4806              gnus-tmp-replied
4807              (cond ((memq number gnus-newsgroup-processable)
4808                     gnus-process-mark)
4809                    ((memq number gnus-newsgroup-cached)
4810                     gnus-cached-mark)
4811                    ((memq number gnus-newsgroup-replied)
4812                     gnus-replied-mark)
4813                    ((memq number gnus-newsgroup-forwarded)
4814                     gnus-forwarded-mark)
4815                    ((memq number gnus-newsgroup-saved)
4816                     gnus-saved-mark)
4817                    ((memq number gnus-newsgroup-recent)
4818                     gnus-recent-mark)
4819                    ((memq number gnus-newsgroup-unseen)
4820                     gnus-unseen-mark)
4821                    (t gnus-no-mark))
4822              gnus-tmp-downloaded
4823              (cond ((memq number gnus-newsgroup-undownloaded) 
4824                     gnus-undownloaded-mark)
4825                    (gnus-newsgroup-agentized
4826                     gnus-downloaded-mark)
4827                    (t
4828                     gnus-no-mark))
4829              gnus-tmp-from (mail-header-from gnus-tmp-header)
4830              gnus-tmp-name
4831              (cond
4832               ((string-match "<[^>]+> *$" gnus-tmp-from)
4833                (setq beg-match (match-beginning 0))
4834                (or (and (string-match "^\".+\"" gnus-tmp-from)
4835                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4836                    (substring gnus-tmp-from 0 beg-match)))
4837               ((string-match "(.+)" gnus-tmp-from)
4838                (substring gnus-tmp-from
4839                           (1+ (match-beginning 0)) (1- (match-end 0))))
4840               (t gnus-tmp-from))
4841
4842              ;; Do the %B string
4843              gnus-tmp-thread-tree-header-string
4844              (cond
4845               ((not gnus-show-threads) "")
4846               ((zerop gnus-tmp-level)
4847                (cond ((cdar thread)
4848                       (or gnus-sum-thread-tree-root subject))
4849                      (gnus-tmp-new-adopts
4850                       (or gnus-sum-thread-tree-false-root subject))
4851                      (t
4852                       (or gnus-sum-thread-tree-single-indent subject))))
4853               (t
4854                (concat (apply 'concat
4855                               (mapcar (lambda (item)
4856                                         (if (= item 1)
4857                                             gnus-sum-thread-tree-vertical
4858                                           gnus-sum-thread-tree-indent))
4859                                       (cdr (reverse tree-stack))))
4860                        (if (nth 1 thread)
4861                            gnus-sum-thread-tree-leaf-with-other
4862                          gnus-sum-thread-tree-single-leaf)))))
4863             (when (string= gnus-tmp-name "")
4864               (setq gnus-tmp-name gnus-tmp-from))
4865             (unless (numberp gnus-tmp-lines)
4866               (setq gnus-tmp-lines -1))
4867             (if (= gnus-tmp-lines -1)
4868                 (setq gnus-tmp-lines "?")
4869               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4870               (gnus-put-text-property
4871              (point)
4872              (progn (eval gnus-summary-line-format-spec) (point))
4873                'gnus-number number)
4874             (when gnus-visual-p
4875               (forward-line -1)
4876               (gnus-run-hooks 'gnus-summary-update-hook)
4877               (forward-line 1))
4878
4879             (setq gnus-tmp-prev-subject simp-subject)))
4880
4881         (when (nth 1 thread)
4882           (push (list (max 0 gnus-tmp-level)
4883                       (copy-sequence tree-stack)
4884                       (nthcdr 1 thread))
4885                 stack))
4886         (push (if (nth 1 thread) 1 0) tree-stack)
4887         (incf gnus-tmp-level)
4888         (setq threads (if thread-end nil (cdar thread)))
4889         (if gnus-summary-display-while-building
4890             (if building-count
4891                 (progn
4892                   ;; use a set frequency
4893                   (setq building-line-count (1- building-line-count))
4894                   (when (= building-line-count 0)
4895                     (sit-for 0)
4896                     (setq building-line-count
4897                           gnus-summary-display-while-building)))
4898               ;; always
4899               (sit-for 0)))
4900         (unless threads
4901           (setq gnus-tmp-level 0)))))
4902   (gnus-message 7 "Generating summary...done"))
4903
4904 (defun gnus-summary-prepare-unthreaded (headers)
4905   "Generate an unthreaded summary buffer based on HEADERS."
4906   (let (header number mark)
4907
4908     (beginning-of-line)
4909
4910     (while headers
4911       ;; We may have to root out some bad articles...
4912       (when (memq (setq number (mail-header-number
4913                                 (setq header (pop headers))))
4914                   gnus-newsgroup-limit)
4915         ;; Mark article as read when it has a low score.
4916         (when (and gnus-summary-mark-below
4917                    (< (or (cdr (assq number gnus-newsgroup-scored))
4918                           gnus-summary-default-score 0)
4919                       gnus-summary-mark-below)
4920                    (not (gnus-summary-article-ancient-p number)))
4921           (setq gnus-newsgroup-unreads
4922                 (delq number gnus-newsgroup-unreads))
4923           (if gnus-newsgroup-auto-expire
4924               (push number gnus-newsgroup-expirable)
4925             (push (cons number gnus-low-score-mark)
4926                   gnus-newsgroup-reads)))
4927
4928         (setq mark (gnus-article-mark number))
4929         (push (gnus-data-make number mark (1+ (point)) header 0)
4930               gnus-newsgroup-data)
4931         (gnus-summary-insert-line
4932          header 0 number
4933          (memq number gnus-newsgroup-undownloaded)
4934          mark (memq number gnus-newsgroup-replied)
4935          (memq number gnus-newsgroup-expirable)
4936          (mail-header-subject header) nil
4937          (cdr (assq number gnus-newsgroup-scored))
4938          (memq number gnus-newsgroup-processable))))))
4939
4940 (defun gnus-summary-remove-list-identifiers ()
4941   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4942   (let ((regexp (if (consp gnus-list-identifiers)
4943                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4944                   gnus-list-identifiers))
4945         changed subject)
4946     (when regexp
4947       (dolist (header gnus-newsgroup-headers)
4948         (setq subject (mail-header-subject header)
4949               changed nil)
4950         (while (string-match
4951                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4952                 subject)
4953           (setq subject
4954                 (concat (substring subject 0 (match-beginning 2))
4955                         (substring subject (match-end 0)))
4956                 changed t))
4957         (when (and changed
4958                    (string-match
4959                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4960           (setq subject
4961                 (concat (substring subject 0 (match-beginning 1))
4962                         (substring subject (match-end 1)))))
4963         (when changed
4964           (mail-header-set-subject header subject))))))
4965
4966 (defun gnus-fetch-headers (articles)
4967   "Fetch headers of ARTICLES."
4968   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4969     (gnus-message 5 "Fetching headers for %s..." name)
4970     (prog1
4971         (if (eq 'nov
4972                 (setq gnus-headers-retrieved-by
4973                       (gnus-retrieve-headers
4974                        articles gnus-newsgroup-name
4975                        ;; We might want to fetch old headers, but
4976                        ;; not if there is only 1 article.
4977                        (and (or (and
4978                                  (not (eq gnus-fetch-old-headers 'some))
4979                                  (not (numberp gnus-fetch-old-headers)))
4980                                 (> (length articles) 1))
4981                             gnus-fetch-old-headers))))
4982             (gnus-get-newsgroup-headers-xover
4983              articles nil nil gnus-newsgroup-name t)
4984           (gnus-get-newsgroup-headers))
4985       (gnus-message 5 "Fetching headers for %s...done" name))))
4986
4987 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4988   "Select newsgroup GROUP.
4989 If READ-ALL is non-nil, all articles in the group are selected.
4990 If SELECT-ARTICLES, only select those articles from GROUP."
4991   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4992          ;;!!! Dirty hack; should be removed.
4993          (gnus-summary-ignore-duplicates
4994           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4995               t
4996             gnus-summary-ignore-duplicates))
4997          (info (nth 2 entry))
4998          articles fetched-articles cached)
4999
5000     (unless (gnus-check-server
5001              (set (make-local-variable 'gnus-current-select-method)
5002                   (gnus-find-method-for-group group)))
5003       (error "Couldn't open server"))
5004
5005     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5006         (gnus-activate-group group)     ; Or we can activate it...
5007         (progn                          ; Or we bug out.
5008           (when (equal major-mode 'gnus-summary-mode)
5009             (gnus-kill-buffer (current-buffer)))
5010           (error "Couldn't activate group %s: %s"
5011                  group (gnus-status-message group))))
5012
5013     (unless (gnus-request-group group t)
5014       (when (equal major-mode 'gnus-summary-mode)
5015         (gnus-kill-buffer (current-buffer)))
5016       (error "Couldn't request group %s: %s"
5017              group (gnus-status-message group)))
5018
5019     (when gnus-agent
5020       ;; The agent may be storing articles that are no longer in the
5021       ;; server's active range.  If that is the case, the active range
5022       ;; needs to be expanded such that the agent's articles can be
5023       ;; included in the summary.
5024       (let* ((gnus-command-method (gnus-find-method-for-group group))
5025              (alist (gnus-agent-load-alist group))
5026              (active (gnus-active group)))
5027         (if (and (car alist)
5028                  (< (caar alist) (car active)))
5029             (gnus-set-active group (cons (caar alist) (cdr active)))))
5030
5031       (setq gnus-summary-use-undownloaded-faces
5032             (not (gnus-agent-find-parameter 
5033                   group
5034                   'agent-disable-undownloaded-faces))))
5035
5036     (setq gnus-newsgroup-name group
5037           gnus-newsgroup-unselected nil
5038           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5039
5040     (let ((display (gnus-group-find-parameter group 'display)))
5041       (setq gnus-newsgroup-display
5042             (cond
5043              ((not (zerop (or (car-safe read-all) 0)))
5044               ;; The user entered the group with C-u SPC/RET, let's show
5045               ;; all articles.
5046               'gnus-not-ignore)
5047              ((eq display 'all)
5048               'gnus-not-ignore)
5049              ((arrayp display)
5050               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5051              ((numberp display)
5052               ;; The following is probably the "correct" solution, but
5053               ;; it makes Gnus fetch all headers and then limit the
5054               ;; articles (which is slow), so instead we hack the
5055               ;; select-articles parameter instead. -- Simon Josefsson
5056               ;; <jas@kth.se>
5057               ;;
5058               ;; (gnus-byte-compile
5059               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5060               ;;                         display)))))
5061               (setq select-articles
5062                     (gnus-uncompress-range
5063                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5064                              (if (> tmp 0)
5065                                  tmp
5066                                1))
5067                            (cdr (gnus-active group)))))
5068               nil)
5069              (t
5070               nil))))
5071
5072     (gnus-summary-setup-default-charset)
5073
5074     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5075     (when (gnus-virtual-group-p group)
5076       (setq cached gnus-newsgroup-cached))
5077
5078     (setq gnus-newsgroup-unreads
5079           (gnus-sorted-ndifference
5080            (gnus-sorted-ndifference gnus-newsgroup-unreads
5081                                     gnus-newsgroup-marked)
5082            gnus-newsgroup-dormant))
5083
5084     (setq gnus-newsgroup-processable nil)
5085
5086     (gnus-update-read-articles group gnus-newsgroup-unreads)
5087
5088     ;; Adjust and set lists of article marks.
5089     (when info
5090       (gnus-adjust-marked-articles info))
5091     (if (setq articles select-articles)
5092         (setq gnus-newsgroup-unselected
5093               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5094       (setq articles (gnus-articles-to-read group read-all)))
5095
5096     (cond
5097      ((null articles)
5098       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5099       'quit)
5100      ((eq articles 0) nil)
5101      (t
5102       ;; Init the dependencies hash table.
5103       (setq gnus-newsgroup-dependencies
5104             (gnus-make-hashtable (length articles)))
5105       (gnus-set-global-variables)
5106       ;; Retrieve the headers and read them in.
5107
5108       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5109
5110       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5111       (when cached
5112         (setq gnus-newsgroup-cached cached))
5113
5114       ;; Suppress duplicates?
5115       (when gnus-suppress-duplicates
5116         (gnus-dup-suppress-articles))
5117
5118       ;; Set the initial limit.
5119       (setq gnus-newsgroup-limit (copy-sequence articles))
5120       ;; Remove canceled articles from the list of unread articles.
5121       (setq fetched-articles
5122             (mapcar (lambda (headers) (mail-header-number headers))
5123                     gnus-newsgroup-headers))
5124       (setq gnus-newsgroup-articles fetched-articles)
5125       (setq gnus-newsgroup-unreads
5126             (gnus-sorted-nintersection
5127              gnus-newsgroup-unreads fetched-articles))
5128       (gnus-compute-unseen-list)
5129
5130       ;; Removed marked articles that do not exist.
5131       (gnus-update-missing-marks
5132        (gnus-sorted-difference articles fetched-articles))
5133       ;; We might want to build some more threads first.
5134       (when (and gnus-fetch-old-headers
5135                  (eq gnus-headers-retrieved-by 'nov))
5136         (if (eq gnus-fetch-old-headers 'invisible)
5137             (gnus-build-all-threads)
5138           (gnus-build-old-threads)))
5139       ;; Let the Gnus agent mark articles as read.
5140       (when gnus-agent
5141         (gnus-agent-get-undownloaded-list))
5142       ;; Remove list identifiers from subject
5143       (when gnus-list-identifiers
5144         (gnus-summary-remove-list-identifiers))
5145       ;; Check whether auto-expire is to be done in this group.
5146       (setq gnus-newsgroup-auto-expire
5147             (gnus-group-auto-expirable-p group))
5148       ;; Set up the article buffer now, if necessary.
5149       (unless gnus-single-article-buffer
5150         (gnus-article-setup-buffer))
5151       ;; First and last article in this newsgroup.
5152       (when gnus-newsgroup-headers
5153         (setq gnus-newsgroup-begin
5154               (mail-header-number (car gnus-newsgroup-headers))
5155               gnus-newsgroup-end
5156               (mail-header-number
5157                (gnus-last-element gnus-newsgroup-headers))))
5158       ;; GROUP is successfully selected.
5159       (or gnus-newsgroup-headers t)))))
5160
5161 (defun gnus-compute-unseen-list ()
5162   ;; The `seen' marks are treated specially.
5163   (if (not gnus-newsgroup-seen)
5164       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5165     (setq gnus-newsgroup-unseen
5166           (gnus-inverse-list-range-intersection
5167            gnus-newsgroup-articles gnus-newsgroup-seen))))
5168
5169 (defun gnus-summary-display-make-predicate (display)
5170   (require 'gnus-agent)
5171   (when (= (length display) 1)
5172     (setq display (car display)))
5173   (unless gnus-summary-display-cache
5174     (dolist (elem (append '((unread . unread)
5175                             (read . read)
5176                             (unseen . unseen))
5177                           gnus-article-mark-lists))
5178       (push (cons (cdr elem)
5179                   (gnus-byte-compile
5180                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5181             gnus-summary-display-cache)))
5182   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5183         (gnus-category-predicate-cache gnus-summary-display-cache))
5184     (gnus-get-predicate display)))
5185
5186 ;; Uses the dynamically bound `number' variable.
5187 (eval-when-compile
5188   (defvar number))
5189 (defun gnus-article-marked-p (type &optional article)
5190   (let ((article (or article number)))
5191     (cond
5192      ((eq type 'tick)
5193       (memq article gnus-newsgroup-marked))
5194      ((eq type 'spam)
5195       (memq article gnus-newsgroup-spam-marked))
5196      ((eq type 'unsend)
5197       (memq article gnus-newsgroup-unsendable))
5198      ((eq type 'undownload)
5199       (memq article gnus-newsgroup-undownloaded))
5200      ((eq type 'download)
5201       (memq article gnus-newsgroup-downloadable))
5202      ((eq type 'unread)
5203       (memq article gnus-newsgroup-unreads))
5204      ((eq type 'read)
5205       (memq article gnus-newsgroup-reads))
5206      ((eq type 'dormant)
5207       (memq article gnus-newsgroup-dormant) )
5208      ((eq type 'expire)
5209       (memq article gnus-newsgroup-expirable))
5210      ((eq type 'reply)
5211       (memq article gnus-newsgroup-replied))
5212      ((eq type 'killed)
5213       (memq article gnus-newsgroup-killed))
5214      ((eq type 'bookmark)
5215       (assq article gnus-newsgroup-bookmarks))
5216      ((eq type 'score)
5217       (assq article gnus-newsgroup-scored))
5218      ((eq type 'save)
5219       (memq article gnus-newsgroup-saved))
5220      ((eq type 'cache)
5221       (memq article gnus-newsgroup-cached))
5222      ((eq type 'forward)
5223       (memq article gnus-newsgroup-forwarded))
5224      ((eq type 'seen)
5225       (not (memq article gnus-newsgroup-unseen)))
5226      ((eq type 'recent)
5227       (memq article gnus-newsgroup-recent))
5228      (t t))))
5229
5230 (defun gnus-articles-to-read (group &optional read-all)
5231   "Find out what articles the user wants to read."
5232   (let* ((display (gnus-group-find-parameter group 'display))
5233          (articles
5234           ;; Select all articles if `read-all' is non-nil, or if there
5235           ;; are no unread articles.
5236           (if (or read-all
5237                   (and (zerop (length gnus-newsgroup-marked))
5238                        (zerop (length gnus-newsgroup-unreads)))
5239                   ;; Fetch all if the predicate is non-nil.
5240                   gnus-newsgroup-display)
5241               ;; We want to select the headers for all the articles in
5242               ;; the group, so we select either all the active
5243               ;; articles in the group, or (if that's nil), the
5244               ;; articles in the cache.
5245               (or
5246                (gnus-uncompress-range (gnus-active group))
5247                (gnus-cache-articles-in-group group))
5248             ;; Select only the "normal" subset of articles.
5249             (gnus-sorted-nunion
5250              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5251              gnus-newsgroup-unreads)))
5252          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5253          (scored (length scored-list))
5254          (number (length articles))
5255          (marked (+ (length gnus-newsgroup-marked)
5256                     (length gnus-newsgroup-dormant)))
5257          (select
5258           (cond
5259            ((numberp read-all)
5260             read-all)
5261            ((numberp gnus-newsgroup-display)
5262             gnus-newsgroup-display)
5263            (t
5264             (condition-case ()
5265                 (cond
5266                  ((and (or (<= scored marked) (= scored number))
5267                        (numberp gnus-large-newsgroup)
5268                        (> number gnus-large-newsgroup))
5269                   (let* ((cursor-in-echo-area nil)
5270                          (initial (gnus-parameter-large-newsgroup-initial
5271                                    gnus-newsgroup-name))
5272                          (input
5273                           (read-string
5274                            (format
5275                             "How many articles from %s (%s %d): "
5276                             (gnus-limit-string
5277                              (gnus-group-decoded-name gnus-newsgroup-name)
5278                              35)
5279                             (if initial "max" "default")
5280                             number)
5281                            (if initial
5282                                (cons (number-to-string initial)
5283                                      0)))))
5284                     (if (string-match "^[ \t]*$" input) number input)))
5285                  ((and (> scored marked) (< scored number)
5286                        (> (- scored number) 20))
5287                   (let ((input
5288                          (read-string
5289                           (format "%s %s (%d scored, %d total): "
5290                                   "How many articles from"
5291                                   (gnus-group-decoded-name group)
5292                                   scored number))))
5293                     (if (string-match "^[ \t]*$" input)
5294                         number input)))
5295                  (t number))
5296               (quit
5297                (message "Quit getting the articles to read")
5298                nil))))))
5299     (setq select (if (stringp select) (string-to-number select) select))
5300     (if (or (null select) (zerop select))
5301         select
5302       (if (and (not (zerop scored)) (<= (abs select) scored))
5303           (progn
5304             (setq articles (sort scored-list '<))
5305             (setq number (length articles)))
5306         (setq articles (copy-sequence articles)))
5307
5308       (when (< (abs select) number)
5309         (if (< select 0)
5310             ;; Select the N oldest articles.
5311             (setcdr (nthcdr (1- (abs select)) articles) nil)
5312           ;; Select the N most recent articles.
5313           (setq articles (nthcdr (- number select) articles))))
5314       (setq gnus-newsgroup-unselected
5315             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5316       (when gnus-alter-articles-to-read-function
5317         (setq articles
5318               (sort
5319                (funcall gnus-alter-articles-to-read-function
5320                         gnus-newsgroup-name articles)
5321                '<)))
5322       articles)))
5323
5324 (defun gnus-killed-articles (killed articles)
5325   (let (out)
5326     (while articles
5327       (when (inline (gnus-member-of-range (car articles) killed))
5328         (push (car articles) out))
5329       (setq articles (cdr articles)))
5330     out))
5331
5332 (defun gnus-uncompress-marks (marks)
5333   "Uncompress the mark ranges in MARKS."
5334   (let ((uncompressed '(score bookmark))
5335         out)
5336     (while marks
5337       (if (memq (caar marks) uncompressed)
5338           (push (car marks) out)
5339         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5340       (setq marks (cdr marks)))
5341     out))
5342
5343 (defun gnus-article-mark-to-type (mark)
5344   "Return the type of MARK."
5345   (or (cadr (assq mark gnus-article-special-mark-lists))
5346       'list))
5347
5348 (defun gnus-article-unpropagatable-p (mark)
5349   "Return whether MARK should be propagated to backend."
5350   (memq mark gnus-article-unpropagated-mark-lists))
5351
5352 (defun gnus-adjust-marked-articles (info)
5353   "Set all article lists and remove all marks that are no longer valid."
5354   (let* ((marked-lists (gnus-info-marks info))
5355          (active (gnus-active (gnus-info-group info)))
5356          (min (car active))
5357          (max (cdr active))
5358          (types gnus-article-mark-lists)
5359          marks var articles article mark mark-type)
5360
5361     (dolist (marks marked-lists)
5362       (setq mark (car marks)
5363             mark-type (gnus-article-mark-to-type mark)
5364             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5365
5366       ;; We set the variable according to the type of the marks list,
5367       ;; and then adjust the marks to a subset of the active articles.
5368       (cond
5369        ;; Adjust "simple" lists.
5370        ((eq mark-type 'list)
5371         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5372         (when (memq mark '(tick dormant expire reply save))
5373           (while articles
5374             (when (or (< (setq article (pop articles)) min) (> article max))
5375               (set var (delq article (symbol-value var)))))))
5376        ;; Adjust assocs.
5377        ((eq mark-type 'tuple)
5378         (set var (setq articles (cdr marks)))
5379         (when (not (listp (cdr (symbol-value var))))
5380           (set var (list (symbol-value var))))
5381         (when (not (listp (cdr articles)))
5382           (setq articles (list articles)))
5383         (while articles
5384           (when (or (not (consp (setq article (pop articles))))
5385                     (< (car article) min)
5386                     (> (car article) max))
5387             (set var (delq article (symbol-value var))))))
5388        ;; Adjust ranges (sloppily).
5389        ((eq mark-type 'range)
5390         (cond
5391          ((eq mark 'seen)
5392           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5393           ;; It should be (seen (NUM1 . NUM2)).
5394           (when (numberp (cddr marks))
5395             (setcdr marks (list (cdr marks))))
5396           (setq articles (cdr marks))
5397           (while (and articles
5398                       (or (and (consp (car articles))
5399                                (> min (cdar articles)))
5400                           (and (numberp (car articles))
5401                                (> min (car articles)))))
5402             (pop articles))
5403           (set var articles))))))))
5404
5405 (defun gnus-update-missing-marks (missing)
5406   "Go through the list of MISSING articles and remove them from the mark lists."
5407   (when missing
5408     (let (var m)
5409       ;; Go through all types.
5410       (dolist (elem gnus-article-mark-lists)
5411         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5412           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5413           (when (symbol-value var)
5414             ;; This list has articles.  So we delete all missing
5415             ;; articles from it.
5416             (setq m missing)
5417             (while m
5418               (set var (delq (pop m) (symbol-value var))))))))))
5419
5420 (defun gnus-update-marks ()
5421   "Enter the various lists of marked articles into the newsgroup info list."
5422   (let ((types gnus-article-mark-lists)
5423         (info (gnus-get-info gnus-newsgroup-name))
5424         type list newmarked symbol delta-marks)
5425     (when info
5426       ;; Add all marks lists to the list of marks lists.
5427       (while (setq type (pop types))
5428         (setq list (symbol-value
5429                     (setq symbol
5430                           (intern (format "gnus-newsgroup-%s" (car type))))))
5431
5432         (when list
5433           ;; Get rid of the entries of the articles that have the
5434           ;; default score.
5435           (when (and (eq (cdr type) 'score)
5436                      gnus-save-score
5437                      list)
5438             (let* ((arts list)
5439                    (prev (cons nil list))
5440                    (all prev))
5441               (while arts
5442                 (if (or (not (consp (car arts)))
5443                         (= (cdar arts) gnus-summary-default-score))
5444                     (setcdr prev (cdr arts))
5445                   (setq prev arts))
5446                 (setq arts (cdr arts)))
5447               (setq list (cdr all)))))
5448
5449         (when (eq (cdr type) 'seen)
5450           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5451
5452         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5453           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5454
5455         (when (and (gnus-check-backend-function
5456                     'request-set-mark gnus-newsgroup-name)
5457                    (not (gnus-article-unpropagatable-p (cdr type))))
5458           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5459                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5460                  (add (gnus-remove-from-range
5461                        (gnus-copy-sequence list) old)))
5462             (when add
5463               (push (list add 'add (list (cdr type))) delta-marks))
5464             (when del
5465               (push (list del 'del (list (cdr type))) delta-marks))))
5466
5467         (when list
5468           (push (cons (cdr type) list) newmarked)))
5469
5470       (when delta-marks
5471         (unless (gnus-check-group gnus-newsgroup-name)
5472           (error "Can't open server for %s" gnus-newsgroup-name))
5473         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5474
5475       ;; Enter these new marks into the info of the group.
5476       (if (nthcdr 3 info)
5477           (setcar (nthcdr 3 info) newmarked)
5478         ;; Add the marks lists to the end of the info.
5479         (when newmarked
5480           (setcdr (nthcdr 2 info) (list newmarked))))
5481
5482       ;; Cut off the end of the info if there's nothing else there.
5483       (let ((i 5))
5484         (while (and (> i 2)
5485                     (not (nth i info)))
5486           (when (nthcdr (decf i) info)
5487             (setcdr (nthcdr i info) nil)))))))
5488
5489 (defun gnus-set-mode-line (where)
5490   "Set the mode line of the article or summary buffers.
5491 If WHERE is `summary', the summary mode line format will be used."
5492   ;; Is this mode line one we keep updated?
5493   (when (and (memq where gnus-updated-mode-lines)
5494              (symbol-value
5495               (intern (format "gnus-%s-mode-line-format-spec" where))))
5496     (let (mode-string)
5497       (save-excursion
5498         ;; We evaluate this in the summary buffer since these
5499         ;; variables are buffer-local to that buffer.
5500         (set-buffer gnus-summary-buffer)
5501        ;; We bind all these variables that are used in the `eval' form
5502         ;; below.
5503         (let* ((mformat (symbol-value
5504                          (intern
5505                           (format "gnus-%s-mode-line-format-spec" where))))
5506                (gnus-tmp-group-name (gnus-group-decoded-name
5507                                      gnus-newsgroup-name))
5508                (gnus-tmp-article-number (or gnus-current-article 0))
5509                (gnus-tmp-unread gnus-newsgroup-unreads)
5510                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5511                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5512                (gnus-tmp-unread-and-unselected
5513                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5514                             (zerop gnus-tmp-unselected))
5515                        "")
5516                       ((zerop gnus-tmp-unselected)
5517                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5518                       (t (format "{%d(+%d) more}"
5519                                  gnus-tmp-unread-and-unticked
5520                                  gnus-tmp-unselected))))
5521                (gnus-tmp-subject
5522                 (if (and gnus-current-headers
5523                          (vectorp gnus-current-headers))
5524                     (gnus-mode-string-quote
5525                      (mail-header-subject gnus-current-headers))
5526                   ""))
5527                bufname-length max-len
5528                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5529           (setq mode-string (eval mformat))
5530           (setq bufname-length (if (string-match "%b" mode-string)
5531                                    (- (length
5532                                        (buffer-name
5533                                         (if (eq where 'summary)
5534                                             nil
5535                                           (get-buffer gnus-article-buffer))))
5536                                       2)
5537                                  0))
5538           (setq max-len (max 4 (if gnus-mode-non-string-length
5539                                    (- (window-width)
5540                                       gnus-mode-non-string-length
5541                                       bufname-length)
5542                                  (length mode-string))))
5543           ;; We might have to chop a bit of the string off...
5544           (when (> (length mode-string) max-len)
5545             (setq mode-string
5546                   (concat (truncate-string-to-width mode-string (- max-len 3))
5547                           "...")))
5548           ;; Pad the mode string a bit.
5549           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5550       ;; Update the mode line.
5551       (setq mode-line-buffer-identification
5552             (gnus-mode-line-buffer-identification (list mode-string)))
5553       (set-buffer-modified-p t))))
5554
5555 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5556   "Go through the HEADERS list and add all Xrefs to a hash table.
5557 The resulting hash table is returned, or nil if no Xrefs were found."
5558   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5559          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5560          (xref-hashtb (gnus-make-hashtable))
5561          start group entry number xrefs header)
5562     (while headers
5563       (setq header (pop headers))
5564       (when (and (setq xrefs (mail-header-xref header))
5565                  (not (memq (setq number (mail-header-number header))
5566                             unreads)))
5567         (setq start 0)
5568         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5569           (setq start (match-end 0))
5570           (setq group (if prefix
5571                           (concat prefix (substring xrefs (match-beginning 1)
5572                                                     (match-end 1)))
5573                         (substring xrefs (match-beginning 1) (match-end 1))))
5574           (setq number
5575                 (string-to-int (substring xrefs (match-beginning 2)
5576                                           (match-end 2))))
5577           (if (setq entry (gnus-gethash group xref-hashtb))
5578               (setcdr entry (cons number (cdr entry)))
5579             (gnus-sethash group (cons number nil) xref-hashtb)))))
5580     (and start xref-hashtb)))
5581
5582 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5583   "Look through all the headers and mark the Xrefs as read."
5584   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5585         name entry info xref-hashtb idlist method nth4)
5586     (save-excursion
5587       (set-buffer gnus-group-buffer)
5588       (when (setq xref-hashtb
5589                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5590         (mapatoms
5591          (lambda (group)
5592            (unless (string= from-newsgroup (setq name (symbol-name group)))
5593              (setq idlist (symbol-value group))
5594              ;; Dead groups are not updated.
5595              (and (prog1
5596                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5597                             info (nth 2 entry))
5598                     (when (stringp (setq nth4 (gnus-info-method info)))
5599                       (setq nth4 (gnus-server-to-method nth4))))
5600                   ;; Only do the xrefs if the group has the same
5601                   ;; select method as the group we have just read.
5602                   (or (gnus-methods-equal-p
5603                        nth4 (gnus-find-method-for-group from-newsgroup))
5604                       virtual
5605                       (equal nth4 (setq method (gnus-find-method-for-group
5606                                                 from-newsgroup)))
5607                       (and (equal (car nth4) (car method))
5608                            (equal (nth 1 nth4) (nth 1 method))))
5609                   gnus-use-cross-reference
5610                   (or (not (eq gnus-use-cross-reference t))
5611                       virtual
5612                       ;; Only do cross-references on subscribed
5613                       ;; groups, if that is what is wanted.
5614                       (<= (gnus-info-level info) gnus-level-subscribed))
5615                   (gnus-group-make-articles-read name idlist))))
5616          xref-hashtb)))))
5617
5618 (defun gnus-compute-read-articles (group articles)
5619   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5620          (info (nth 2 entry))
5621          (active (gnus-active group))
5622          ninfo)
5623     (when entry
5624       ;; First peel off all invalid article numbers.
5625       (when active
5626         (let ((ids articles)
5627               id first)
5628           (while (setq id (pop ids))
5629             (when (and first (> id (cdr active)))
5630               ;; We'll end up in this situation in one particular
5631               ;; obscure situation.  If you re-scan a group and get
5632               ;; a new article that is cross-posted to a different
5633               ;; group that has not been re-scanned, you might get
5634               ;; crossposted article that has a higher number than
5635               ;; Gnus believes possible.  So we re-activate this
5636               ;; group as well.  This might mean doing the
5637               ;; crossposting thingy will *increase* the number
5638               ;; of articles in some groups.  Tsk, tsk.
5639               (setq active (or (gnus-activate-group group) active)))
5640             (when (or (> id (cdr active))
5641                       (< id (car active)))
5642               (setq articles (delq id articles))))))
5643       ;; If the read list is nil, we init it.
5644       (if (and active
5645                (null (gnus-info-read info))
5646                (> (car active) 1))
5647           (setq ninfo (cons 1 (1- (car active))))
5648         (setq ninfo (gnus-info-read info)))
5649       ;; Then we add the read articles to the range.
5650       (gnus-add-to-range
5651        ninfo (setq articles (sort articles '<))))))
5652
5653 (defun gnus-group-make-articles-read (group articles)
5654   "Update the info of GROUP to say that ARTICLES are read."
5655   (let* ((num 0)
5656          (entry (gnus-gethash group gnus-newsrc-hashtb))
5657          (info (nth 2 entry))
5658          (active (gnus-active group))
5659          range)
5660     (when entry
5661       (setq range (gnus-compute-read-articles group articles))
5662       (save-excursion
5663         (set-buffer gnus-group-buffer)
5664         (gnus-undo-register
5665           `(progn
5666              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5667              (gnus-info-set-read ',info ',(gnus-info-read info))
5668              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5669              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5670              (gnus-group-update-group ,group t))))
5671       ;; Add the read articles to the range.
5672       (gnus-info-set-read info range)
5673       (gnus-request-set-mark group (list (list range 'add '(read))))
5674       ;; Then we have to re-compute how many unread
5675       ;; articles there are in this group.
5676       (when active
5677         (cond
5678          ((not range)
5679           (setq num (- (1+ (cdr active)) (car active))))
5680          ((not (listp (cdr range)))
5681           (setq num (- (cdr active) (- (1+ (cdr range))
5682                                        (car range)))))
5683          (t
5684           (while range
5685             (if (numberp (car range))
5686                 (setq num (1+ num))
5687               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5688             (setq range (cdr range)))
5689           (setq num (- (cdr active) num))))
5690         ;; Update the number of unread articles.
5691         (setcar entry num)
5692         ;; Update the group buffer.
5693         (unless (gnus-ephemeral-group-p group)
5694           (gnus-group-update-group group t))))))
5695
5696 (defvar gnus-newsgroup-none-id 0)
5697
5698 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5699   (let ((cur nntp-server-buffer)
5700         (dependencies
5701          (or dependencies
5702              (save-excursion (set-buffer gnus-summary-buffer)
5703                              gnus-newsgroup-dependencies)))
5704         headers id end ref
5705         (mail-parse-charset gnus-newsgroup-charset)
5706         (mail-parse-ignored-charsets
5707          (save-excursion (condition-case nil
5708                              (set-buffer gnus-summary-buffer)
5709                            (error))
5710                          gnus-newsgroup-ignored-charsets)))
5711     (save-excursion
5712       (set-buffer nntp-server-buffer)
5713       ;; Translate all TAB characters into SPACE characters.
5714       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5715       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5716       (gnus-run-hooks 'gnus-parse-headers-hook)
5717       (let ((case-fold-search t)
5718             in-reply-to header p lines chars)
5719         (goto-char (point-min))
5720         ;; Search to the beginning of the next header.  Error messages
5721         ;; do not begin with 2 or 3.
5722         (while (re-search-forward "^[23][0-9]+ " nil t)
5723           (setq id nil
5724                 ref nil)
5725           ;; This implementation of this function, with nine
5726           ;; search-forwards instead of the one re-search-forward and
5727           ;; a case (which basically was the old function) is actually
5728           ;; about twice as fast, even though it looks messier.  You
5729           ;; can't have everything, I guess.  Speed and elegance
5730           ;; doesn't always go hand in hand.
5731           (setq
5732            header
5733            (vector
5734             ;; Number.
5735             (prog1
5736                 (read cur)
5737               (end-of-line)
5738               (setq p (point))
5739               (narrow-to-region (point)
5740                                 (or (and (search-forward "\n.\n" nil t)
5741                                          (- (point) 2))
5742                                     (point))))
5743             ;; Subject.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nsubject:" nil t)
5747                   (funcall gnus-decode-encoded-word-function
5748                            (nnheader-header-value))
5749                 "(none)"))
5750             ;; From.
5751             (progn
5752               (goto-char p)
5753               (if (search-forward "\nfrom:" nil t)
5754                   (funcall gnus-decode-encoded-word-function
5755                            (nnheader-header-value))
5756                 "(nobody)"))
5757             ;; Date.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\ndate:" nil t)
5761                   (nnheader-header-value) ""))
5762             ;; Message-ID.
5763             (progn
5764               (goto-char p)
5765               (setq id (if (re-search-forward
5766                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5767                            ;; We do it this way to make sure the Message-ID
5768                            ;; is (somewhat) syntactically valid.
5769                            (buffer-substring (match-beginning 1)
5770                                              (match-end 1))
5771                          ;; If there was no message-id, we just fake one
5772                          ;; to make subsequent routines simpler.
5773                          (nnheader-generate-fake-message-id))))
5774             ;; References.
5775             (progn
5776               (goto-char p)
5777               (if (search-forward "\nreferences:" nil t)
5778                   (progn
5779                     (setq end (point))
5780                     (prog1
5781                         (nnheader-header-value)
5782                       (setq ref
5783                             (buffer-substring
5784                              (progn
5785                                (end-of-line)
5786                                (search-backward ">" end t)
5787                                (1+ (point)))
5788                              (progn
5789                                (search-backward "<" end t)
5790                                (point))))))
5791                 ;; Get the references from the in-reply-to header if there
5792                 ;; were no references and the in-reply-to header looks
5793                 ;; promising.
5794                 (if (and (search-forward "\nin-reply-to:" nil t)
5795                          (setq in-reply-to (nnheader-header-value))
5796                          (string-match "<[^>]+>" in-reply-to))
5797                     (let (ref2)
5798                       (setq ref (substring in-reply-to (match-beginning 0)
5799                                            (match-end 0)))
5800                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5801                         (setq ref2 (substring in-reply-to (match-beginning 0)
5802                                               (match-end 0)))
5803                         (when (> (length ref2) (length ref))
5804                           (setq ref ref2)))
5805                       ref)
5806                   (setq ref nil))))
5807             ;; Chars.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nchars: " nil t)
5811                   (if (numberp (setq chars (ignore-errors (read cur))))
5812                       chars -1)
5813                 -1))
5814             ;; Lines.
5815             (progn
5816               (goto-char p)
5817               (if (search-forward "\nlines: " nil t)
5818                   (if (numberp (setq lines (ignore-errors (read cur))))
5819                       lines -1)
5820                 -1))
5821             ;; Xref.
5822             (progn
5823               (goto-char p)
5824               (and (search-forward "\nxref:" nil t)
5825                    (nnheader-header-value)))
5826             ;; Extra.
5827             (when gnus-extra-headers
5828               (let ((extra gnus-extra-headers)
5829                     out)
5830                 (while extra
5831                   (goto-char p)
5832                   (when (search-forward
5833                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5834                     (push (cons (car extra) (nnheader-header-value))
5835                           out))
5836                   (pop extra))
5837                 out))))
5838           (when (equal id ref)
5839             (setq ref nil))
5840
5841           (when gnus-alter-header-function
5842             (funcall gnus-alter-header-function header)
5843             (setq id (mail-header-id header)
5844                   ref (gnus-parent-id (mail-header-references header))))
5845
5846           (when (setq header
5847                       (gnus-dependencies-add-header
5848                        header dependencies force-new))
5849             (push header headers))
5850           (goto-char (point-max))
5851           (widen))
5852         (nreverse headers)))))
5853
5854 ;; Goes through the xover lines and returns a list of vectors
5855 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5856                                                   force-new dependencies
5857                                                   group also-fetch-heads)
5858   "Parse the news overview data in the server buffer.
5859 Return a list of headers that match SEQUENCE (see
5860 `nntp-retrieve-headers')."
5861   ;; Get the Xref when the users reads the articles since most/some
5862   ;; NNTP servers do not include Xrefs when using XOVER.
5863   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5864   (let ((mail-parse-charset gnus-newsgroup-charset)
5865         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5866         (cur nntp-server-buffer)
5867         (dependencies (or dependencies gnus-newsgroup-dependencies))
5868         (allp (cond
5869                ((eq gnus-read-all-available-headers t)
5870                 t)
5871                ((stringp gnus-read-all-available-headers)
5872                 (string-match gnus-read-all-available-headers group))
5873                (t
5874                 nil)))
5875         number headers header)
5876     (save-excursion
5877       (set-buffer nntp-server-buffer)
5878       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5879       ;; Allow the user to mangle the headers before parsing them.
5880       (gnus-run-hooks 'gnus-parse-headers-hook)
5881       (goto-char (point-min))
5882       (gnus-parse-without-error
5883         (while (and (or sequence allp)
5884                     (not (eobp)))
5885           (setq number (read cur))
5886           (when (not allp)
5887             (while (and sequence
5888                         (< (car sequence) number))
5889               (setq sequence (cdr sequence))))
5890           (when (and (or allp
5891                          (and sequence
5892                               (eq number (car sequence))))
5893                      (progn
5894                        (setq sequence (cdr sequence))
5895                        (setq header (inline
5896                                       (gnus-nov-parse-line
5897                                        number dependencies force-new)))))
5898             (push header headers))
5899           (forward-line 1)))
5900       ;; A common bug in inn is that if you have posted an article and
5901       ;; then retrieves the active file, it will answer correctly --
5902       ;; the new article is included.  However, a NOV entry for the
5903       ;; article may not have been generated yet, so this may fail.
5904       ;; We work around this problem by retrieving the last few
5905       ;; headers using HEAD.
5906       (if (or (not also-fetch-heads)
5907               (not sequence))
5908           ;; We (probably) got all the headers.
5909           (nreverse headers)
5910         (let ((gnus-nov-is-evil t))
5911           (nconc
5912            (nreverse headers)
5913            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5914              (gnus-get-newsgroup-headers))))))))
5915
5916 (defun gnus-article-get-xrefs ()
5917   "Fill in the Xref value in `gnus-current-headers', if necessary.
5918 This is meant to be called in `gnus-article-internal-prepare-hook'."
5919   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5920                                  gnus-current-headers)))
5921     (or (not gnus-use-cross-reference)
5922         (not headers)
5923         (and (mail-header-xref headers)
5924              (not (string= (mail-header-xref headers) "")))
5925         (let ((case-fold-search t)
5926               xref)
5927           (save-restriction
5928             (nnheader-narrow-to-headers)
5929             (goto-char (point-min))
5930             (when (or (and (not (eobp))
5931                            (eq (downcase (char-after)) ?x)
5932                            (looking-at "Xref:"))
5933                       (search-forward "\nXref:" nil t))
5934               (goto-char (1+ (match-end 0)))
5935               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5936               (mail-header-set-xref headers xref)))))))
5937
5938 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5939   "Find article ID and insert the summary line for that article.
5940 OLD-HEADER can either be a header or a line number to insert
5941 the subject line on."
5942   (let* ((line (and (numberp old-header) old-header))
5943          (old-header (and (vectorp old-header) old-header))
5944          (header (cond ((and old-header use-old-header)
5945                         old-header)
5946                        ((and (numberp id)
5947                              (gnus-number-to-header id))
5948                         (gnus-number-to-header id))
5949                        (t
5950                         (gnus-read-header id))))
5951          (number (and (numberp id) id))
5952          d)
5953     (when header
5954       ;; Rebuild the thread that this article is part of and go to the
5955       ;; article we have fetched.
5956       (when (and (not gnus-show-threads)
5957                  old-header)
5958         (when (and number
5959                    (setq d (gnus-data-find (mail-header-number old-header))))
5960           (goto-char (gnus-data-pos d))
5961           (gnus-data-remove
5962            number
5963            (- (gnus-point-at-bol)
5964               (prog1
5965                   (1+ (gnus-point-at-eol))
5966                 (gnus-delete-line))))))
5967       (when old-header
5968         (mail-header-set-number header (mail-header-number old-header)))
5969       (setq gnus-newsgroup-sparse
5970             (delq (setq number (mail-header-number header))
5971                   gnus-newsgroup-sparse))
5972       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5973       (push number gnus-newsgroup-limit)
5974       (gnus-rebuild-thread (mail-header-id header) line)
5975       (gnus-summary-goto-subject number nil t))
5976     (when (and (numberp number)
5977                (> number 0))
5978       ;; We have to update the boundaries even if we can't fetch the
5979       ;; article if ID is a number -- so that the next `P' or `N'
5980       ;; command will fetch the previous (or next) article even
5981       ;; if the one we tried to fetch this time has been canceled.
5982       (when (> number gnus-newsgroup-end)
5983         (setq gnus-newsgroup-end number))
5984       (when (< number gnus-newsgroup-begin)
5985         (setq gnus-newsgroup-begin number))
5986       (setq gnus-newsgroup-unselected
5987             (delq number gnus-newsgroup-unselected)))
5988     ;; Report back a success?
5989     (and header (mail-header-number header))))
5990
5991 ;;; Process/prefix in the summary buffer
5992
5993 (defun gnus-summary-work-articles (n)
5994   "Return a list of articles to be worked upon.
5995 The prefix argument, the list of process marked articles, and the
5996 current article will be taken into consideration."
5997   (save-excursion
5998     (set-buffer gnus-summary-buffer)
5999     (cond
6000      (n
6001       ;; A numerical prefix has been given.
6002       (setq n (prefix-numeric-value n))
6003       (let ((backward (< n 0))
6004             (n (abs (prefix-numeric-value n)))
6005             articles article)
6006         (save-excursion
6007           (while
6008               (and (> n 0)
6009                    (push (setq article (gnus-summary-article-number))
6010                          articles)
6011                    (if backward
6012                        (gnus-summary-find-prev nil article)
6013                      (gnus-summary-find-next nil article)))
6014             (decf n)))
6015         (nreverse articles)))
6016      ((and (gnus-region-active-p) (mark))
6017       (message "region active")
6018       ;; Work on the region between point and mark.
6019       (let ((max (max (point) (mark)))
6020             articles article)
6021         (save-excursion
6022           (goto-char (min (min (point) (mark))))
6023           (while
6024               (and
6025                (push (setq article (gnus-summary-article-number)) articles)
6026                (gnus-summary-find-next nil article)
6027                (< (point) max)))
6028           (nreverse articles))))
6029      (gnus-newsgroup-processable
6030       ;; There are process-marked articles present.
6031       ;; Save current state.
6032       (gnus-summary-save-process-mark)
6033       ;; Return the list.
6034       (reverse gnus-newsgroup-processable))
6035      (t
6036       ;; Just return the current article.
6037       (list (gnus-summary-article-number))))))
6038
6039 (defmacro gnus-summary-iterate (arg &rest forms)
6040   "Iterate over the process/prefixed articles and do FORMS.
6041 ARG is the interactive prefix given to the command.  FORMS will be
6042 executed with point over the summary line of the articles."
6043   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6044     `(let ((,articles (gnus-summary-work-articles ,arg)))
6045        (while ,articles
6046          (gnus-summary-goto-subject (car ,articles))
6047          ,@forms
6048          (pop ,articles)))))
6049
6050 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6051 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6052
6053 (defun gnus-summary-save-process-mark ()
6054   "Push the current set of process marked articles on the stack."
6055   (interactive)
6056   (push (copy-sequence gnus-newsgroup-processable)
6057         gnus-newsgroup-process-stack))
6058
6059 (defun gnus-summary-kill-process-mark ()
6060   "Push the current set of process marked articles on the stack and unmark."
6061   (interactive)
6062   (gnus-summary-save-process-mark)
6063   (gnus-summary-unmark-all-processable))
6064
6065 (defun gnus-summary-yank-process-mark ()
6066   "Pop the last process mark state off the stack and restore it."
6067   (interactive)
6068   (unless gnus-newsgroup-process-stack
6069     (error "Empty mark stack"))
6070   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6071
6072 (defun gnus-summary-process-mark-set (set)
6073   "Make SET into the current process marked articles."
6074   (gnus-summary-unmark-all-processable)
6075   (while set
6076     (gnus-summary-set-process-mark (pop set))))
6077
6078 ;;; Searching and stuff
6079
6080 (defun gnus-summary-search-group (&optional backward use-level)
6081   "Search for next unread newsgroup.
6082 If optional argument BACKWARD is non-nil, search backward instead."
6083   (save-excursion
6084     (set-buffer gnus-group-buffer)
6085     (when (gnus-group-search-forward
6086            backward nil (if use-level (gnus-group-group-level) nil))
6087       (gnus-group-group-name))))
6088
6089 (defun gnus-summary-best-group (&optional exclude-group)
6090   "Find the name of the best unread group.
6091 If EXCLUDE-GROUP, do not go to this group."
6092   (save-excursion
6093     (set-buffer gnus-group-buffer)
6094     (save-excursion
6095       (gnus-group-best-unread-group exclude-group))))
6096
6097 (defun gnus-summary-find-next (&optional unread article backward)
6098   (if backward
6099       (gnus-summary-find-prev unread article)
6100     (let* ((dummy (gnus-summary-article-intangible-p))
6101            (article (or article (gnus-summary-article-number)))
6102            (data (gnus-data-find-list article))
6103            result)
6104       (when (and (not dummy)
6105                  (or (not gnus-summary-check-current)
6106                      (not unread)
6107                      (not (gnus-data-unread-p (car data)))))
6108         (setq data (cdr data)))
6109       (when (setq result
6110                   (if unread
6111                       (progn
6112                         (while data
6113                           (unless (memq (gnus-data-number (car data)) 
6114                                         (cond
6115                                          ((eq gnus-auto-goto-ignores
6116                                               'always-undownloaded)
6117                                           gnus-newsgroup-undownloaded)
6118                                          (gnus-plugged
6119                                           nil)
6120                                          ((eq gnus-auto-goto-ignores
6121                                               'unfetched)
6122                                           gnus-newsgroup-unfetched)
6123                                          ((eq gnus-auto-goto-ignores
6124                                               'undownloaded)
6125                                           gnus-newsgroup-undownloaded)))
6126                             (when (gnus-data-unread-p (car data))
6127                               (setq result (car data)
6128                                     data nil)))
6129                           (setq data (cdr data)))
6130                         result)
6131                     (car data)))
6132         (goto-char (gnus-data-pos result))
6133         (gnus-data-number result)))))
6134
6135 (defun gnus-summary-find-prev (&optional unread article)
6136   (let* ((eobp (eobp))
6137          (article (or article (gnus-summary-article-number)))
6138          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6139          result)
6140     (when (and (not eobp)
6141                (or (not gnus-summary-check-current)
6142                    (not unread)
6143                    (not (gnus-data-unread-p (car data)))))
6144       (setq data (cdr data)))
6145     (when (setq result
6146                 (if unread
6147                     (progn
6148                       (while data
6149                         (unless (memq (gnus-data-number (car data))
6150                                       (cond
6151                                        ((eq gnus-auto-goto-ignores
6152                                             'always-undownloaded)
6153                                         gnus-newsgroup-undownloaded)
6154                                        (gnus-plugged
6155                                         nil)
6156                                        ((eq gnus-auto-goto-ignores
6157                                             'unfetched)
6158                                         gnus-newsgroup-unfetched)
6159                                        ((eq gnus-auto-goto-ignores
6160                                             'undownloaded)
6161                                         gnus-newsgroup-undownloaded)))
6162                           (when (gnus-data-unread-p (car data))
6163                             (setq result (car data)
6164                                   data nil)))
6165                         (setq data (cdr data)))
6166                       result)
6167                   (car data)))
6168       (goto-char (gnus-data-pos result))
6169       (gnus-data-number result))))
6170
6171 (defun gnus-summary-find-subject (subject &optional unread backward article)
6172   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6173          (article (or article (gnus-summary-article-number)))
6174          (articles (gnus-data-list backward))
6175          (arts (gnus-data-find-list article articles))
6176          result)
6177     (when (or (not gnus-summary-check-current)
6178               (not unread)
6179               (not (gnus-data-unread-p (car arts))))
6180       (setq arts (cdr arts)))
6181     (while arts
6182       (and (or (not unread)
6183                (gnus-data-unread-p (car arts)))
6184            (vectorp (gnus-data-header (car arts)))
6185            (gnus-subject-equal
6186             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6187            (setq result (car arts)
6188                  arts nil))
6189       (setq arts (cdr arts)))
6190     (and result
6191          (goto-char (gnus-data-pos result))
6192          (gnus-data-number result))))
6193
6194 (defun gnus-summary-search-forward (&optional unread subject backward)
6195   "Search forward for an article.
6196 If UNREAD, look for unread articles.  If SUBJECT, look for
6197 articles with that subject.  If BACKWARD, search backward instead."
6198   (cond (subject (gnus-summary-find-subject subject unread backward))
6199         (backward (gnus-summary-find-prev unread))
6200         (t (gnus-summary-find-next unread))))
6201
6202 (defun gnus-recenter (&optional n)
6203   "Center point in window and redisplay frame.
6204 Also do horizontal recentering."
6205   (interactive "P")
6206   (when (and gnus-auto-center-summary
6207              (not (eq gnus-auto-center-summary 'vertical)))
6208     (gnus-horizontal-recenter))
6209   (recenter n))
6210
6211 (defun gnus-summary-recenter ()
6212   "Center point in the summary window.
6213 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6214 displayed, no centering will be performed."
6215   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6216   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6217   (interactive)
6218   ;; The user has to want it.
6219   (when gnus-auto-center-summary
6220     (let* ((top (cond ((< (window-height) 4) 0)
6221                       ((< (window-height) 7) 1)
6222                       (t (if (numberp gnus-auto-center-summary)
6223                              gnus-auto-center-summary
6224                            2))))
6225            (height (1- (window-height)))
6226            (bottom (save-excursion (goto-char (point-max))
6227                                    (forward-line (- height))
6228                                    (point)))
6229            (window (get-buffer-window (current-buffer))))
6230       (when (get-buffer-window gnus-article-buffer)
6231         ;; Only do recentering when the article buffer is displayed,
6232         ;; Set the window start to either `bottom', which is the biggest
6233         ;; possible valid number, or the second line from the top,
6234         ;; whichever is the least.
6235         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6236           (if (> bottom top-pos)
6237               ;; Keep the second line from the top visible
6238               (set-window-start window top-pos t)
6239             ;; Try to keep the bottom line visible; if it's partially
6240             ;; obscured, either scroll one more line to make it fully
6241             ;; visible, or revert to using TOP-POS.
6242             (save-excursion
6243               (goto-char (point-max))
6244               (forward-line -1)
6245               (let ((last-line-start (point)))
6246                 (goto-char bottom)
6247                 (set-window-start window (point) t)
6248                 (when (not (pos-visible-in-window-p last-line-start window))
6249                   (forward-line 1)
6250                   (set-window-start window (min (point) top-pos) t)))))))
6251       ;; Do horizontal recentering while we're at it.
6252       (when (and (get-buffer-window (current-buffer) t)
6253                  (not (eq gnus-auto-center-summary 'vertical)))
6254         (let ((selected (selected-window)))
6255           (select-window (get-buffer-window (current-buffer) t))
6256           (gnus-summary-position-point)
6257           (gnus-horizontal-recenter)
6258           (select-window selected))))))
6259
6260 (defun gnus-summary-jump-to-group (newsgroup)
6261   "Move point to NEWSGROUP in group mode buffer."
6262   ;; Keep update point of group mode buffer if visible.
6263   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6264       (save-window-excursion
6265         ;; Take care of tree window mode.
6266         (when (get-buffer-window gnus-group-buffer)
6267           (pop-to-buffer gnus-group-buffer))
6268         (gnus-group-jump-to-group newsgroup))
6269     (save-excursion
6270       ;; Take care of tree window mode.
6271       (if (get-buffer-window gnus-group-buffer)
6272           (pop-to-buffer gnus-group-buffer)
6273         (set-buffer gnus-group-buffer))
6274       (gnus-group-jump-to-group newsgroup))))
6275
6276 ;; This function returns a list of article numbers based on the
6277 ;; difference between the ranges of read articles in this group and
6278 ;; the range of active articles.
6279 (defun gnus-list-of-unread-articles (group)
6280   (let* ((read (gnus-info-read (gnus-get-info group)))
6281          (active (or (gnus-active group) (gnus-activate-group group)))
6282          (last (cdr active))
6283          first nlast unread)
6284     ;; If none are read, then all are unread.
6285     (if (not read)
6286         (setq first (car active))
6287       ;; If the range of read articles is a single range, then the
6288       ;; first unread article is the article after the last read
6289       ;; article.  Sounds logical, doesn't it?
6290       (if (and (not (listp (cdr read)))
6291                (or (< (car read) (car active))
6292                    (progn (setq read (list read))
6293                           nil)))
6294           (setq first (max (car active) (1+ (cdr read))))
6295         ;; `read' is a list of ranges.
6296         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6297                                   (caar read)))
6298                   1)
6299           (setq first (car active)))
6300         (while read
6301           (when first
6302             (while (< first nlast)
6303               (push first unread)
6304               (setq first (1+ first))))
6305           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6306           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6307           (setq read (cdr read)))))
6308     ;; And add the last unread articles.
6309     (while (<= first last)
6310       (push first unread)
6311       (setq first (1+ first)))
6312     ;; Return the list of unread articles.
6313     (delq 0 (nreverse unread))))
6314
6315 (defun gnus-list-of-read-articles (group)
6316   "Return a list of unread, unticked and non-dormant articles."
6317   (let* ((info (gnus-get-info group))
6318          (marked (gnus-info-marks info))
6319          (active (gnus-active group)))
6320     (and info active
6321          (gnus-list-range-difference
6322           (gnus-list-range-difference
6323            (gnus-sorted-complement
6324             (gnus-uncompress-range active)
6325             (gnus-list-of-unread-articles group))
6326            (cdr (assq 'dormant marked)))
6327           (cdr (assq 'tick marked))))))
6328
6329 ;; Various summary commands
6330
6331 (defun gnus-summary-select-article-buffer ()
6332   "Reconfigure windows to show article buffer."
6333   (interactive)
6334   (if (not (gnus-buffer-live-p gnus-article-buffer))
6335       (error "There is no article buffer for this summary buffer")
6336     (gnus-configure-windows 'article)
6337     (select-window (get-buffer-window gnus-article-buffer))))
6338
6339 (defun gnus-summary-universal-argument (arg)
6340   "Perform any operation on all articles that are process/prefixed."
6341   (interactive "P")
6342   (let ((articles (gnus-summary-work-articles arg))
6343         func article)
6344     (if (eq
6345          (setq
6346           func
6347           (key-binding
6348            (read-key-sequence
6349             (substitute-command-keys
6350              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6351          'undefined)
6352         (gnus-error 1 "Undefined key")
6353       (save-excursion
6354         (while articles
6355           (gnus-summary-goto-subject (setq article (pop articles)))
6356           (let (gnus-newsgroup-processable)
6357             (command-execute func))
6358           (gnus-summary-remove-process-mark article)))))
6359   (gnus-summary-position-point))
6360
6361 (defun gnus-summary-toggle-truncation (&optional arg)
6362   "Toggle truncation of summary lines.
6363 With arg, turn line truncation on if arg is positive."
6364   (interactive "P")
6365   (setq truncate-lines
6366         (if (null arg) (not truncate-lines)
6367           (> (prefix-numeric-value arg) 0)))
6368   (redraw-display))
6369
6370 (defun gnus-summary-find-for-reselect ()
6371   "Return the number of an article to stay on across a reselect.
6372 The current article is considered, then following articles, then previous
6373 articles.  An article is sought which is not cancelled and isn't a temporary
6374 insertion from another group.  If there's no such then return a dummy 0."
6375   (let (found)
6376     (dolist (rev '(nil t))
6377       (unless found      ; don't demand the reverse list if we don't need it
6378         (let ((data (gnus-data-find-list
6379                      (gnus-summary-article-number) (gnus-data-list rev))))
6380           (while (and data (not found))
6381             (if (and (< 0 (gnus-data-number (car data)))
6382                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6383                 (setq found (gnus-data-number (car data))))
6384             (setq data (cdr data))))))
6385     (or found 0)))
6386
6387 (defun gnus-summary-reselect-current-group (&optional all rescan)
6388   "Exit and then reselect the current newsgroup.
6389 The prefix argument ALL means to select all articles."
6390   (interactive "P")
6391   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6392     (error "Ephemeral groups can't be reselected"))
6393   (let ((current-subject (gnus-summary-find-for-reselect))
6394         (group gnus-newsgroup-name))
6395     (setq gnus-newsgroup-begin nil)
6396     (gnus-summary-exit)
6397     ;; We have to adjust the point of group mode buffer because
6398     ;; point was moved to the next unread newsgroup by exiting.
6399     (gnus-summary-jump-to-group group)
6400     (when rescan
6401       (save-excursion
6402         (gnus-group-get-new-news-this-group 1)))
6403     (gnus-group-read-group all t)
6404     (gnus-summary-goto-subject current-subject nil t)))
6405
6406 (defun gnus-summary-rescan-group (&optional all)
6407   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6408   (interactive "P")
6409   (gnus-summary-reselect-current-group all t))
6410
6411 (defun gnus-summary-update-info (&optional non-destructive)
6412   (save-excursion
6413     (let ((group gnus-newsgroup-name))
6414       (when group
6415         (when gnus-newsgroup-kill-headers
6416           (setq gnus-newsgroup-killed
6417                 (gnus-compress-sequence
6418                  (gnus-sorted-union
6419                   (gnus-list-range-intersection
6420                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6421                   gnus-newsgroup-unreads)
6422                  t)))
6423         (unless (listp (cdr gnus-newsgroup-killed))
6424           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6425         (let ((headers gnus-newsgroup-headers))
6426           ;; Set the new ranges of read articles.
6427           (save-excursion
6428             (set-buffer gnus-group-buffer)
6429             (gnus-undo-force-boundary))
6430           (gnus-update-read-articles
6431            group (gnus-sorted-union
6432                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6433           ;; Set the current article marks.
6434           (let ((gnus-newsgroup-scored
6435                  (if (and (not gnus-save-score)
6436                           (not non-destructive))
6437                      nil
6438                    gnus-newsgroup-scored)))
6439             (save-excursion
6440               (gnus-update-marks)))
6441           ;; Do the cross-ref thing.
6442           (when gnus-use-cross-reference
6443             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6444           ;; Do not switch windows but change the buffer to work.
6445           (set-buffer gnus-group-buffer)
6446           (unless (gnus-ephemeral-group-p group)
6447             (gnus-group-update-group group)))))))
6448
6449 (defun gnus-summary-save-newsrc (&optional force)
6450   "Save the current number of read/marked articles in the dribble buffer.
6451 The dribble buffer will then be saved.
6452 If FORCE (the prefix), also save the .newsrc file(s)."
6453   (interactive "P")
6454   (gnus-summary-update-info t)
6455   (if force
6456       (gnus-save-newsrc-file)
6457     (gnus-dribble-save)))
6458
6459 (defun gnus-summary-exit (&optional temporary)
6460   "Exit reading current newsgroup, and then return to group selection mode.
6461 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6462   (interactive)
6463   (gnus-set-global-variables)
6464   (when (gnus-buffer-live-p gnus-article-buffer)
6465     (save-excursion
6466       (set-buffer gnus-article-buffer)
6467       (mm-destroy-parts gnus-article-mime-handles)
6468       ;; Set it to nil for safety reason.
6469       (setq gnus-article-mime-handle-alist nil)
6470       (setq gnus-article-mime-handles nil)))
6471   (gnus-kill-save-kill-buffer)
6472   (gnus-async-halt-prefetch)
6473   (let* ((group gnus-newsgroup-name)
6474          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6475          (gnus-group-is-exiting-p t)
6476          (mode major-mode)
6477          (group-point nil)
6478          (buf (current-buffer)))
6479     (unless quit-config
6480       ;; Do adaptive scoring, and possibly save score files.
6481       (when gnus-newsgroup-adaptive
6482         (gnus-score-adaptive))
6483       (when gnus-use-scoring
6484         (gnus-score-save)))
6485     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6486     ;; If we have several article buffers, we kill them at exit.
6487     (unless gnus-single-article-buffer
6488       (gnus-kill-buffer gnus-original-article-buffer)
6489       (setq gnus-article-current nil))
6490     (when gnus-use-cache
6491       (gnus-cache-possibly-remove-articles)
6492       (gnus-cache-save-buffers))
6493     (gnus-async-prefetch-remove-group group)
6494     (when gnus-suppress-duplicates
6495       (gnus-dup-enter-articles))
6496     (when gnus-use-trees
6497       (gnus-tree-close group))
6498     (when gnus-use-cache
6499       (gnus-cache-write-active))
6500     ;; Remove entries for this group.
6501     (nnmail-purge-split-history (gnus-group-real-name group))
6502     ;; Make all changes in this group permanent.
6503     (unless quit-config
6504       (gnus-run-hooks 'gnus-exit-group-hook)
6505       (gnus-summary-update-info))
6506     (gnus-close-group group)
6507     ;; Make sure where we were, and go to next newsgroup.
6508     (set-buffer gnus-group-buffer)
6509     (unless quit-config
6510       (gnus-group-jump-to-group group))
6511     (gnus-run-hooks 'gnus-summary-exit-hook)
6512     (unless (or quit-config
6513                 ;; If this group has disappeared from the summary
6514                 ;; buffer, don't skip forwards.
6515                 (not (string= group (gnus-group-group-name))))
6516       (gnus-group-next-unread-group 1))
6517     (setq group-point (point))
6518     (if temporary
6519         nil                             ;Nothing to do.
6520       ;; If we have several article buffers, we kill them at exit.
6521       (unless gnus-single-article-buffer
6522         (gnus-kill-buffer gnus-article-buffer)
6523         (gnus-kill-buffer gnus-original-article-buffer)
6524         (setq gnus-article-current nil))
6525       (set-buffer buf)
6526       (if (not gnus-kill-summary-on-exit)
6527           (progn
6528             (gnus-deaden-summary)
6529             (setq mode nil))
6530         ;; We set all buffer-local variables to nil.  It is unclear why
6531         ;; this is needed, but if we don't, buffer-local variables are
6532         ;; not garbage-collected, it seems.  This would the lead to en
6533         ;; ever-growing Emacs.
6534         (gnus-summary-clear-local-variables)
6535         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6536           (gnus-summary-clear-local-variables))
6537         (when (get-buffer gnus-article-buffer)
6538           (bury-buffer gnus-article-buffer))
6539         ;; We clear the global counterparts of the buffer-local
6540         ;; variables as well, just to be on the safe side.
6541         (set-buffer gnus-group-buffer)
6542         (gnus-summary-clear-local-variables)
6543         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6544           (gnus-summary-clear-local-variables))
6545         ;; Return to group mode buffer.
6546         (when (eq mode 'gnus-summary-mode)
6547           (gnus-kill-buffer buf)))
6548       (setq gnus-current-select-method gnus-select-method)
6549       (pop-to-buffer gnus-group-buffer)
6550       (if (not quit-config)
6551           (progn
6552             (goto-char group-point)
6553             (gnus-configure-windows 'group 'force))
6554         (gnus-handle-ephemeral-exit quit-config))
6555       ;; Clear the current group name.
6556       (unless quit-config
6557         (setq gnus-newsgroup-name nil)))))
6558
6559 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6560 (defun gnus-summary-exit-no-update (&optional no-questions)
6561   "Quit reading current newsgroup without updating read article info."
6562   (interactive)
6563   (let* ((group gnus-newsgroup-name)
6564          (gnus-group-is-exiting-p t)
6565          (gnus-group-is-exiting-without-update-p t)
6566          (quit-config (gnus-group-quit-config group)))
6567     (when (or no-questions
6568               gnus-expert-user
6569               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6570       (gnus-async-halt-prefetch)
6571       (run-hooks 'gnus-summary-prepare-exit-hook)
6572       (when (gnus-buffer-live-p gnus-article-buffer)
6573         (save-excursion
6574           (set-buffer gnus-article-buffer)
6575           (mm-destroy-parts gnus-article-mime-handles)
6576           ;; Set it to nil for safety reason.
6577           (setq gnus-article-mime-handle-alist nil)
6578           (setq gnus-article-mime-handles nil)))
6579       ;; If we have several article buffers, we kill them at exit.
6580       (unless gnus-single-article-buffer
6581         (gnus-kill-buffer gnus-article-buffer)
6582         (gnus-kill-buffer gnus-original-article-buffer)
6583         (setq gnus-article-current nil))
6584       (if (not gnus-kill-summary-on-exit)
6585           (gnus-deaden-summary)
6586         (gnus-close-group group)
6587         (gnus-summary-clear-local-variables)
6588         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6589           (gnus-summary-clear-local-variables))
6590         (set-buffer gnus-group-buffer)
6591         (gnus-summary-clear-local-variables)
6592         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6593           (gnus-summary-clear-local-variables))
6594         (gnus-kill-buffer gnus-summary-buffer))
6595       (unless gnus-single-article-buffer
6596         (setq gnus-article-current nil))
6597       (when gnus-use-trees
6598         (gnus-tree-close group))
6599       (gnus-async-prefetch-remove-group group)
6600       (when (get-buffer gnus-article-buffer)
6601         (bury-buffer gnus-article-buffer))
6602       ;; Return to the group buffer.
6603       (gnus-configure-windows 'group 'force)
6604       ;; Clear the current group name.
6605       (setq gnus-newsgroup-name nil)
6606       (unless (gnus-ephemeral-group-p group)
6607         (gnus-group-update-group group))
6608       (when (equal (gnus-group-group-name) group)
6609         (gnus-group-next-unread-group 1))
6610       (when quit-config
6611         (gnus-handle-ephemeral-exit quit-config)))))
6612
6613 (defun gnus-handle-ephemeral-exit (quit-config)
6614   "Handle movement when leaving an ephemeral group.
6615 The state which existed when entering the ephemeral is reset."
6616   (if (not (buffer-name (car quit-config)))
6617       (gnus-configure-windows 'group 'force)
6618     (set-buffer (car quit-config))
6619     (cond ((eq major-mode 'gnus-summary-mode)
6620            (gnus-set-global-variables))
6621           ((eq major-mode 'gnus-article-mode)
6622            (save-excursion
6623              ;; The `gnus-summary-buffer' variable may point
6624              ;; to the old summary buffer when using a single
6625              ;; article buffer.
6626              (unless (gnus-buffer-live-p gnus-summary-buffer)
6627                (set-buffer gnus-group-buffer))
6628              (set-buffer gnus-summary-buffer)
6629              (gnus-set-global-variables))))
6630     (if (or (eq (cdr quit-config) 'article)
6631             (eq (cdr quit-config) 'pick))
6632         (progn
6633           ;; The current article may be from the ephemeral group
6634           ;; thus it is best that we reload this article
6635           ;;
6636           ;; If we're exiting from a large digest, this can be
6637           ;; extremely slow.  So, it's better not to reload it. -- jh.
6638           ;;(gnus-summary-show-article)
6639           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6640               (gnus-configure-windows 'pick 'force)
6641             (gnus-configure-windows (cdr quit-config) 'force)))
6642       (gnus-configure-windows (cdr quit-config) 'force))
6643     (when (eq major-mode 'gnus-summary-mode)
6644       (gnus-summary-next-subject 1 nil t)
6645       (gnus-summary-recenter)
6646       (gnus-summary-position-point))))
6647
6648 ;;; Dead summaries.
6649
6650 (defvar gnus-dead-summary-mode-map nil)
6651
6652 (unless gnus-dead-summary-mode-map
6653   (setq gnus-dead-summary-mode-map (make-keymap))
6654   (suppress-keymap gnus-dead-summary-mode-map)
6655   (substitute-key-definition
6656    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6657   (dolist (key '("\C-d" "\r" "\177" [delete]))
6658     (define-key gnus-dead-summary-mode-map
6659       key 'gnus-summary-wake-up-the-dead))
6660   (dolist (key '("q" "Q"))
6661     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6662
6663 (defvar gnus-dead-summary-mode nil
6664   "Minor mode for Gnus summary buffers.")
6665
6666 (defun gnus-dead-summary-mode (&optional arg)
6667   "Minor mode for Gnus summary buffers."
6668   (interactive "P")
6669   (when (eq major-mode 'gnus-summary-mode)
6670     (make-local-variable 'gnus-dead-summary-mode)
6671     (setq gnus-dead-summary-mode
6672           (if (null arg) (not gnus-dead-summary-mode)
6673             (> (prefix-numeric-value arg) 0)))
6674     (when gnus-dead-summary-mode
6675       (gnus-add-minor-mode
6676        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6677
6678 (defun gnus-deaden-summary ()
6679   "Make the current summary buffer into a dead summary buffer."
6680   ;; Kill any previous dead summary buffer.
6681   (when (and gnus-dead-summary
6682              (buffer-name gnus-dead-summary))
6683     (save-excursion
6684       (set-buffer gnus-dead-summary)
6685       (when gnus-dead-summary-mode
6686         (kill-buffer (current-buffer)))))
6687   ;; Make this the current dead summary.
6688   (setq gnus-dead-summary (current-buffer))
6689   (gnus-dead-summary-mode 1)
6690   (let ((name (buffer-name)))
6691     (when (string-match "Summary" name)
6692       (rename-buffer
6693        (concat (substring name 0 (match-beginning 0)) "Dead "
6694                (substring name (match-beginning 0)))
6695        t)
6696       (bury-buffer))))
6697
6698 (defun gnus-kill-or-deaden-summary (buffer)
6699   "Kill or deaden the summary BUFFER."
6700   (save-excursion
6701     (when (and (buffer-name buffer)
6702                (not gnus-single-article-buffer))
6703       (save-excursion
6704         (set-buffer buffer)
6705         (gnus-kill-buffer gnus-article-buffer)
6706         (gnus-kill-buffer gnus-original-article-buffer)))
6707     (cond
6708      ;; Kill the buffer.
6709      (gnus-kill-summary-on-exit
6710       (when (and gnus-use-trees
6711                  (gnus-buffer-exists-p buffer))
6712         (save-excursion
6713           (set-buffer buffer)
6714           (gnus-tree-close gnus-newsgroup-name)))
6715       (gnus-kill-buffer buffer))
6716      ;; Deaden the buffer.
6717      ((gnus-buffer-exists-p buffer)
6718       (save-excursion
6719         (set-buffer buffer)
6720         (gnus-deaden-summary))))))
6721
6722 (defun gnus-summary-wake-up-the-dead (&rest args)
6723   "Wake up the dead summary buffer."
6724   (interactive)
6725   (gnus-dead-summary-mode -1)
6726   (let ((name (buffer-name)))
6727     (when (string-match "Dead " name)
6728       (rename-buffer
6729        (concat (substring name 0 (match-beginning 0))
6730                (substring name (match-end 0)))
6731        t)))
6732   (gnus-message 3 "This dead summary is now alive again"))
6733
6734 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6735 (defun gnus-summary-fetch-faq (&optional faq-dir)
6736   "Fetch the FAQ for the current group.
6737 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6738 in."
6739   (interactive
6740    (list
6741     (when current-prefix-arg
6742       (completing-read
6743        "FAQ dir: " (and (listp gnus-group-faq-directory)
6744                         (mapcar (lambda (file) (list file))
6745                                 gnus-group-faq-directory))))))
6746   (let (gnus-faq-buffer)
6747     (when (setq gnus-faq-buffer
6748                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6749       (gnus-configure-windows 'summary-faq))))
6750
6751 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6752 (defun gnus-summary-describe-group (&optional force)
6753   "Describe the current newsgroup."
6754   (interactive "P")
6755   (gnus-group-describe-group force gnus-newsgroup-name))
6756
6757 (defun gnus-summary-describe-briefly ()
6758   "Describe summary mode commands briefly."
6759   (interactive)
6760   (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")))
6761
6762 ;; Walking around group mode buffer from summary mode.
6763
6764 (defun gnus-summary-next-group (&optional no-article target-group backward)
6765   "Exit current newsgroup and then select next unread newsgroup.
6766 If prefix argument NO-ARTICLE is non-nil, no article is selected
6767 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6768 previous group instead."
6769   (interactive "P")
6770   ;; Stop pre-fetching.
6771   (gnus-async-halt-prefetch)
6772   (let ((current-group gnus-newsgroup-name)
6773         (current-buffer (current-buffer))
6774         entered)
6775     ;; First we semi-exit this group to update Xrefs and all variables.
6776     ;; We can't do a real exit, because the window conf must remain
6777     ;; the same in case the user is prompted for info, and we don't
6778     ;; want the window conf to change before that...
6779     (gnus-summary-exit t)
6780     (while (not entered)
6781       ;; Then we find what group we are supposed to enter.
6782       (set-buffer gnus-group-buffer)
6783       (gnus-group-jump-to-group current-group)
6784       (setq target-group
6785             (or target-group
6786                 (if (eq gnus-keep-same-level 'best)
6787                     (gnus-summary-best-group gnus-newsgroup-name)
6788                   (gnus-summary-search-group backward gnus-keep-same-level))))
6789       (if (not target-group)
6790           ;; There are no further groups, so we return to the group
6791           ;; buffer.
6792           (progn
6793             (gnus-message 5 "Returning to the group buffer")
6794             (setq entered t)
6795             (when (gnus-buffer-live-p current-buffer)
6796               (set-buffer current-buffer)
6797               (gnus-summary-exit))
6798             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6799         ;; We try to enter the target group.
6800         (gnus-group-jump-to-group target-group)
6801         (let ((unreads (gnus-group-group-unread)))
6802           (if (and (or (eq t unreads)
6803                        (and unreads (not (zerop unreads))))
6804                    (gnus-summary-read-group
6805                     target-group nil no-article
6806                     (and (buffer-name current-buffer) current-buffer)
6807                     nil backward))
6808               (setq entered t)
6809             (setq current-group target-group
6810                   target-group nil)))))))
6811
6812 (defun gnus-summary-prev-group (&optional no-article)
6813   "Exit current newsgroup and then select previous unread newsgroup.
6814 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6815   (interactive "P")
6816   (gnus-summary-next-group no-article nil t))
6817
6818 ;; Walking around summary lines.
6819
6820 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6821   "Go to the first subject satisfying any non-nil constraint.
6822 If UNREAD is non-nil, the article should be unread.
6823 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6824 If UNSEED is non-nil, the article should be unseen.
6825 Returns the article selected or nil if there are no matching articles."
6826   (interactive "P")
6827   (cond
6828    ;; Empty summary.
6829    ((null gnus-newsgroup-data)
6830     (gnus-message 3 "No articles in the group")
6831     nil)
6832    ;; Pick the first article.
6833    ((not (or unread undownloaded unseen))
6834     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6835     (gnus-data-number (car gnus-newsgroup-data)))
6836    ;; Find the first unread article.
6837    (t
6838     (let ((data gnus-newsgroup-data))
6839       (while (and data
6840                   (let ((num (gnus-data-number (car data))))
6841                     (or (memq num gnus-newsgroup-unfetched)
6842                         (not (or (and unread
6843                                       (memq num gnus-newsgroup-unreads))
6844                                  (and undownloaded
6845                                       (memq num gnus-newsgroup-undownloaded))
6846                                  (and unseen
6847                                       (memq num gnus-newsgroup-unseen)))))))
6848         (setq data (cdr data)))
6849       (prog1 
6850           (if data
6851               (progn
6852                 (goto-char (gnus-data-pos (car data)))
6853                 (gnus-data-number (car data)))
6854             (gnus-message 3 "No more%s articles"
6855                           (let* ((r (when unread " unread"))
6856                                  (d (when undownloaded " undownloaded"))
6857                                  (s (when unseen " unseen"))
6858                                  (l (delq nil (list r d s))))
6859                             (cond ((= 3 (length l))
6860                                    (concat r "," d ", or" s))
6861                                   ((= 2 (length l))
6862                                    (concat (car l) ", or" (cadr l)))
6863                                   ((= 1 (length l))
6864                                    (car l))
6865                                   (t
6866                                    ""))))
6867             nil
6868             )
6869         (gnus-summary-position-point))))))
6870
6871 (defun gnus-summary-next-subject (n &optional unread dont-display)
6872   "Go to next N'th summary line.
6873 If N is negative, go to the previous N'th subject line.
6874 If UNREAD is non-nil, only unread articles are selected.
6875 The difference between N and the actual number of steps taken is
6876 returned."
6877   (interactive "p")
6878   (let ((backward (< n 0))
6879         (n (abs n)))
6880     (while (and (> n 0)
6881                 (if backward
6882                     (gnus-summary-find-prev unread)
6883                   (gnus-summary-find-next unread)))
6884       (unless (zerop (setq n (1- n)))
6885         (gnus-summary-show-thread)))
6886     (when (/= 0 n)
6887       (gnus-message 7 "No more%s articles"
6888                     (if unread " unread" "")))
6889     (unless dont-display
6890       (gnus-summary-recenter)
6891       (gnus-summary-position-point))
6892     n))
6893
6894 (defun gnus-summary-next-unread-subject (n)
6895   "Go to next N'th unread summary line."
6896   (interactive "p")
6897   (gnus-summary-next-subject n t))
6898
6899 (defun gnus-summary-prev-subject (n &optional unread)
6900   "Go to previous N'th summary line.
6901 If optional argument UNREAD is non-nil, only unread article is selected."
6902   (interactive "p")
6903   (gnus-summary-next-subject (- n) unread))
6904
6905 (defun gnus-summary-prev-unread-subject (n)
6906   "Go to previous N'th unread summary line."
6907   (interactive "p")
6908   (gnus-summary-next-subject (- n) t))
6909
6910 (defun gnus-summary-goto-subjects (articles)
6911   "Insert the subject header for ARTICLES in the current buffer."
6912   (save-excursion
6913     (dolist (article articles)
6914       (gnus-summary-goto-subject article t)))
6915   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6916   (gnus-summary-position-point))
6917  
6918 (defun gnus-summary-goto-subject (article &optional force silent)
6919   "Go the subject line of ARTICLE.
6920 If FORCE, also allow jumping to articles not currently shown."
6921   (interactive "nArticle number: ")
6922   (unless (numberp article)
6923     (error "Article %s is not a number" article))
6924   (let ((b (point))
6925         (data (gnus-data-find article)))
6926     ;; We read in the article if we have to.
6927     (and (not data)
6928          force
6929          (gnus-summary-insert-subject
6930           article
6931           (if (or (numberp force) (vectorp force)) force)
6932           t)
6933          (setq data (gnus-data-find article)))
6934     (goto-char b)
6935     (if (not data)
6936         (progn
6937           (unless silent
6938             (gnus-message 3 "Can't find article %d" article))
6939           nil)
6940       (let ((pt (gnus-data-pos data)))
6941         (goto-char pt)
6942         (gnus-summary-set-article-display-arrow pt))
6943       (gnus-summary-position-point)
6944       article)))
6945
6946 ;; Walking around summary lines with displaying articles.
6947
6948 (defun gnus-summary-expand-window (&optional arg)
6949   "Make the summary buffer take up the entire Emacs frame.
6950 Given a prefix, will force an `article' buffer configuration."
6951   (interactive "P")
6952   (if arg
6953       (gnus-configure-windows 'article 'force)
6954     (gnus-configure-windows 'summary 'force)))
6955
6956 (defun gnus-summary-display-article (article &optional all-header)
6957   "Display ARTICLE in article buffer."
6958   (when (gnus-buffer-live-p gnus-article-buffer)
6959     (with-current-buffer gnus-article-buffer
6960       (mm-enable-multibyte)))
6961   (gnus-set-global-variables)
6962   (when (gnus-buffer-live-p gnus-article-buffer)
6963     (with-current-buffer gnus-article-buffer
6964       (setq gnus-article-charset gnus-newsgroup-charset)
6965       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6966       (mm-enable-multibyte)))
6967   (if (null article)
6968       nil
6969     (prog1
6970         (if gnus-summary-display-article-function
6971             (funcall gnus-summary-display-article-function article all-header)
6972           (gnus-article-prepare article all-header))
6973       (gnus-run-hooks 'gnus-select-article-hook)
6974       (when (and gnus-current-article
6975                  (not (zerop gnus-current-article)))
6976         (gnus-summary-goto-subject gnus-current-article))
6977       (gnus-summary-recenter)
6978       (when (and gnus-use-trees gnus-show-threads)
6979         (gnus-possibly-generate-tree article)
6980         (gnus-highlight-selected-tree article))
6981       ;; Successfully display article.
6982       (gnus-article-set-window-start
6983        (cdr (assq article gnus-newsgroup-bookmarks))))))
6984
6985 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6986   "Select the current article.
6987 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6988 non-nil, the article will be re-fetched even if it already present in
6989 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6990 be displayed."
6991   ;; Make sure we are in the summary buffer to work around bbdb bug.
6992   (unless (eq major-mode 'gnus-summary-mode)
6993     (set-buffer gnus-summary-buffer))
6994   (let ((article (or article (gnus-summary-article-number)))
6995         (all-headers (not (not all-headers))) ;Must be t or nil.
6996         gnus-summary-display-article-function)
6997     (and (not pseudo)
6998          (gnus-summary-article-pseudo-p article)
6999          (error "This is a pseudo-article"))
7000     (save-excursion
7001       (set-buffer gnus-summary-buffer)
7002       (if (or (and gnus-single-article-buffer
7003                    (or (null gnus-current-article)
7004                        (null gnus-article-current)
7005                        (null (get-buffer gnus-article-buffer))
7006                        (not (eq article (cdr gnus-article-current)))
7007                        (not (equal (car gnus-article-current)
7008                                    gnus-newsgroup-name))))
7009               (and (not gnus-single-article-buffer)
7010                    (or (null gnus-current-article)
7011                        (not (eq gnus-current-article article))))
7012               force)
7013           ;; The requested article is different from the current article.
7014           (progn
7015             (gnus-summary-display-article article all-headers)
7016             (when (gnus-buffer-live-p gnus-article-buffer)
7017               (with-current-buffer gnus-article-buffer
7018                 (if (not gnus-article-decoded-p) ;; a local variable
7019                     (mm-disable-multibyte))))
7020             (gnus-article-set-window-start
7021              (cdr (assq article gnus-newsgroup-bookmarks)))
7022             article)
7023         'old))))
7024
7025 (defun gnus-summary-force-verify-and-decrypt ()
7026   "Display buttons for signed/encrypted parts and verify/decrypt them."
7027   (interactive)
7028   (let ((mm-verify-option 'known)
7029         (mm-decrypt-option 'known)
7030         (gnus-buttonized-mime-types (append (list "multipart/signed"
7031                                                   "multipart/encrypted")
7032                                             gnus-buttonized-mime-types)))
7033     (gnus-summary-select-article nil 'force)))
7034
7035 (defun gnus-summary-set-current-mark (&optional current-mark)
7036   "Obsolete function."
7037   nil)
7038
7039 (defun gnus-summary-next-article (&optional unread subject backward push)
7040   "Select the next article.
7041 If UNREAD, only unread articles are selected.
7042 If SUBJECT, only articles with SUBJECT are selected.
7043 If BACKWARD, the previous article is selected instead of the next."
7044   (interactive "P")
7045   (cond
7046    ;; Is there such an article?
7047    ((and (gnus-summary-search-forward unread subject backward)
7048          (or (gnus-summary-display-article (gnus-summary-article-number))
7049              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7050     (gnus-summary-position-point))
7051    ;; If not, we try the first unread, if that is wanted.
7052    ((and subject
7053          gnus-auto-select-same
7054          (gnus-summary-first-unread-article))
7055     (gnus-summary-position-point)
7056     (gnus-message 6 "Wrapped"))
7057    ;; Try to get next/previous article not displayed in this group.
7058    ((and gnus-auto-extend-newsgroup
7059          (not unread) (not subject))
7060     (gnus-summary-goto-article
7061      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7062      nil (count-lines (point-min) (point))))
7063    ;; Go to next/previous group.
7064    (t
7065     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7066       (gnus-summary-jump-to-group gnus-newsgroup-name))
7067     (let ((cmd last-command-char)
7068           (point
7069            (save-excursion
7070              (set-buffer gnus-group-buffer)
7071              (point)))
7072           (group
7073            (if (eq gnus-keep-same-level 'best)
7074                (gnus-summary-best-group gnus-newsgroup-name)
7075              (gnus-summary-search-group backward gnus-keep-same-level))))
7076       ;; For some reason, the group window gets selected.  We change
7077       ;; it back.
7078       (select-window (get-buffer-window (current-buffer)))
7079       ;; Select next unread newsgroup automagically.
7080       (cond
7081        ((or (not gnus-auto-select-next)
7082             (not cmd))
7083         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7084        ((or (eq gnus-auto-select-next 'quietly)
7085             (and (eq gnus-auto-select-next 'slightly-quietly)
7086                  push)
7087             (and (eq gnus-auto-select-next 'almost-quietly)
7088                  (gnus-summary-last-article-p)))
7089         ;; Select quietly.
7090         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7091             (gnus-summary-exit)
7092           (gnus-message 7 "No more%s articles (%s)..."
7093                         (if unread " unread" "")
7094                         (if group (concat "selecting " group)
7095                           "exiting"))
7096           (gnus-summary-next-group nil group backward)))
7097        (t
7098         (when (gnus-key-press-event-p last-input-event)
7099           (gnus-summary-walk-group-buffer
7100            gnus-newsgroup-name cmd unread backward point))))))))
7101
7102 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7103   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7104                       (?\C-p (gnus-group-prev-unread-group 1))))
7105         (cursor-in-echo-area t)
7106         keve key group ended prompt)
7107     (save-excursion
7108       (set-buffer gnus-group-buffer)
7109       (goto-char start)
7110       (setq group
7111             (if (eq gnus-keep-same-level 'best)
7112                 (gnus-summary-best-group gnus-newsgroup-name)
7113               (gnus-summary-search-group backward gnus-keep-same-level))))
7114     (while (not ended)
7115       (setq prompt
7116             (format
7117              "No more%s articles%s " (if unread " unread" "")
7118              (if (and group
7119                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7120                  (format " (Type %s for %s [%s])"
7121                          (single-key-description cmd) group
7122                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7123                (format " (Type %s to exit %s)"
7124                        (single-key-description cmd)
7125                        gnus-newsgroup-name))))
7126       ;; Confirm auto selection.
7127       (setq key (car (setq keve (gnus-read-event-char prompt)))
7128             ended t)
7129       (cond
7130        ((assq key keystrokes)
7131         (let ((obuf (current-buffer)))
7132           (switch-to-buffer gnus-group-buffer)
7133           (when group
7134             (gnus-group-jump-to-group group))
7135           (eval (cadr (assq key keystrokes)))
7136           (setq group (gnus-group-group-name))
7137           (switch-to-buffer obuf))
7138         (setq ended nil))
7139        ((equal key cmd)
7140         (if (or (not group)
7141                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7142             (gnus-summary-exit)
7143           (gnus-summary-next-group nil group backward)))
7144        (t
7145         (push (cdr keve) unread-command-events))))))
7146
7147 (defun gnus-summary-next-unread-article ()
7148   "Select unread article after current one."
7149   (interactive)
7150   (gnus-summary-next-article
7151    (or (not (eq gnus-summary-goto-unread 'never))
7152        (gnus-summary-last-article-p (gnus-summary-article-number)))
7153    (and gnus-auto-select-same
7154         (gnus-summary-article-subject))))
7155
7156 (defun gnus-summary-prev-article (&optional unread subject)
7157   "Select the article after the current one.
7158 If UNREAD is non-nil, only unread articles are selected."
7159   (interactive "P")
7160   (gnus-summary-next-article unread subject t))
7161
7162 (defun gnus-summary-prev-unread-article ()
7163   "Select unread article before current one."
7164   (interactive)
7165   (gnus-summary-prev-article
7166    (or (not (eq gnus-summary-goto-unread 'never))
7167        (gnus-summary-first-article-p (gnus-summary-article-number)))
7168    (and gnus-auto-select-same
7169         (gnus-summary-article-subject))))
7170
7171 (defun gnus-summary-next-page (&optional lines circular stop)
7172   "Show next page of the selected article.
7173 If at the end of the current article, select the next article.
7174 LINES says how many lines should be scrolled up.
7175
7176 If CIRCULAR is non-nil, go to the start of the article instead of
7177 selecting the next article when reaching the end of the current
7178 article.
7179
7180 If STOP is non-nil, just stop when reaching the end of the message.
7181
7182 Also see the variable `gnus-article-skip-boring'."
7183   (interactive "P")
7184   (setq gnus-summary-buffer (current-buffer))
7185   (gnus-set-global-variables)
7186   (let ((article (gnus-summary-article-number))
7187         (article-window (get-buffer-window gnus-article-buffer t))
7188         endp)
7189     ;; If the buffer is empty, we have no article.
7190     (unless article
7191       (error "No article to select"))
7192     (gnus-configure-windows 'article)
7193     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7194         (if (and (eq gnus-summary-goto-unread 'never)
7195                  (not (gnus-summary-last-article-p article)))
7196             (gnus-summary-next-article)
7197           (gnus-summary-next-unread-article))
7198       (if (or (null gnus-current-article)
7199               (null gnus-article-current)
7200               (/= article (cdr gnus-article-current))
7201               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7202           ;; Selected subject is different from current article's.
7203           (gnus-summary-display-article article)
7204         (when article-window
7205           (gnus-eval-in-buffer-window gnus-article-buffer
7206             (setq endp (or (gnus-article-next-page lines)
7207                            (gnus-article-only-boring-p))))
7208           (when endp
7209             (cond (stop
7210                    (gnus-message 3 "End of message"))
7211                   (circular
7212                    (gnus-summary-beginning-of-article))
7213                   (lines
7214                    (gnus-message 3 "End of message"))
7215                   ((null lines)
7216                    (if (and (eq gnus-summary-goto-unread 'never)
7217                             (not (gnus-summary-last-article-p article)))
7218                        (gnus-summary-next-article)
7219                      (gnus-summary-next-unread-article))))))))
7220     (gnus-summary-recenter)
7221     (gnus-summary-position-point)))
7222
7223 (defun gnus-summary-prev-page (&optional lines move)
7224   "Show previous page of selected article.
7225 Argument LINES specifies lines to be scrolled down.
7226 If MOVE, move to the previous unread article if point is at
7227 the beginning of the buffer."
7228   (interactive "P")
7229   (let ((article (gnus-summary-article-number))
7230         (article-window (get-buffer-window gnus-article-buffer t))
7231         endp)
7232     (gnus-configure-windows 'article)
7233     (if (or (null gnus-current-article)
7234             (null gnus-article-current)
7235             (/= article (cdr gnus-article-current))
7236             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7237         ;; Selected subject is different from current article's.
7238         (gnus-summary-display-article article)
7239       (gnus-summary-recenter)
7240       (when article-window
7241         (gnus-eval-in-buffer-window gnus-article-buffer
7242           (setq endp (gnus-article-prev-page lines)))
7243         (when (and move endp)
7244           (cond (lines
7245                  (gnus-message 3 "Beginning of message"))
7246                 ((null lines)
7247                  (if (and (eq gnus-summary-goto-unread 'never)
7248                           (not (gnus-summary-first-article-p article)))
7249                      (gnus-summary-prev-article)
7250                    (gnus-summary-prev-unread-article))))))))
7251   (gnus-summary-position-point))
7252
7253 (defun gnus-summary-prev-page-or-article (&optional lines)
7254   "Show previous page of selected article.
7255 Argument LINES specifies lines to be scrolled down.
7256 If at the beginning of the article, go to the next article."
7257   (interactive "P")
7258   (gnus-summary-prev-page lines t))
7259
7260 (defun gnus-summary-scroll-up (lines)
7261   "Scroll up (or down) one line current article.
7262 Argument LINES specifies lines to be scrolled up (or down if negative)."
7263   (interactive "p")
7264   (gnus-configure-windows 'article)
7265   (gnus-summary-show-thread)
7266   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7267     (gnus-eval-in-buffer-window gnus-article-buffer
7268       (cond ((> lines 0)
7269              (when (gnus-article-next-page lines)
7270                (gnus-message 3 "End of message")))
7271             ((< lines 0)
7272              (gnus-article-prev-page (- lines))))))
7273   (gnus-summary-recenter)
7274   (gnus-summary-position-point))
7275
7276 (defun gnus-summary-scroll-down (lines)
7277   "Scroll down (or up) one line current article.
7278 Argument LINES specifies lines to be scrolled down (or up if negative)."
7279   (interactive "p")
7280   (gnus-summary-scroll-up (- lines)))
7281
7282 (defun gnus-summary-next-same-subject ()
7283   "Select next article which has the same subject as current one."
7284   (interactive)
7285   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7286
7287 (defun gnus-summary-prev-same-subject ()
7288   "Select previous article which has the same subject as current one."
7289   (interactive)
7290   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7291
7292 (defun gnus-summary-next-unread-same-subject ()
7293   "Select next unread article which has the same subject as current one."
7294   (interactive)
7295   (gnus-summary-next-article t (gnus-summary-article-subject)))
7296
7297 (defun gnus-summary-prev-unread-same-subject ()
7298   "Select previous unread article which has the same subject as current one."
7299   (interactive)
7300   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7301
7302 (defun gnus-summary-first-unread-article ()
7303   "Select the first unread article.
7304 Return nil if there are no unread articles."
7305   (interactive)
7306   (prog1
7307       (when (gnus-summary-first-subject t)
7308         (gnus-summary-show-thread)
7309         (gnus-summary-first-subject t)
7310         (gnus-summary-display-article (gnus-summary-article-number)))
7311     (gnus-summary-position-point)))
7312
7313 (defun gnus-summary-first-unread-subject ()
7314   "Place the point on the subject line of the first unread article.
7315 Return nil if there are no unread articles."
7316   (interactive)
7317   (prog1
7318       (when (gnus-summary-first-subject t)
7319         (gnus-summary-show-thread)
7320         (gnus-summary-first-subject t))
7321     (gnus-summary-position-point)))
7322
7323 (defun gnus-summary-first-unseen-subject ()
7324   "Place the point on the subject line of the first unseen article.
7325 Return nil if there are no unseen articles."
7326   (interactive)
7327   (prog1
7328       (when (gnus-summary-first-subject nil nil t)
7329         (gnus-summary-show-thread)
7330         (gnus-summary-first-subject nil nil t))
7331     (gnus-summary-position-point)))
7332
7333 (defun gnus-summary-first-unseen-or-unread-subject ()
7334   "Place the point on the subject line of the first unseen article or,
7335 if all article have been seen, on the subject line of the first unread
7336 article."
7337   (interactive)
7338   (prog1
7339       (unless (when (gnus-summary-first-subject nil nil t)
7340                 (gnus-summary-show-thread)
7341                 (gnus-summary-first-subject nil nil t))
7342         (when (gnus-summary-first-subject t)
7343           (gnus-summary-show-thread)
7344           (gnus-summary-first-subject t)))
7345     (gnus-summary-position-point)))
7346
7347 (defun gnus-summary-first-article ()
7348   "Select the first article.
7349 Return nil if there are no articles."
7350   (interactive)
7351   (prog1
7352       (when (gnus-summary-first-subject)
7353         (gnus-summary-show-thread)
7354         (gnus-summary-first-subject)
7355         (gnus-summary-display-article (gnus-summary-article-number)))
7356     (gnus-summary-position-point)))
7357
7358 (defun gnus-summary-best-unread-article (&optional arg)
7359   "Select the unread article with the highest score.
7360 If given a prefix argument, select the next unread article that has a
7361 score higher than the default score."
7362   (interactive "P")
7363   (let ((article (if arg
7364                      (gnus-summary-better-unread-subject)
7365                    (gnus-summary-best-unread-subject))))
7366     (if article
7367         (gnus-summary-goto-article article)
7368       (error "No unread articles"))))
7369
7370 (defun gnus-summary-best-unread-subject ()
7371   "Select the unread subject with the highest score."
7372   (interactive)
7373   (let ((best -1000000)
7374         (data gnus-newsgroup-data)
7375         article score)
7376     (while data
7377       (and (gnus-data-unread-p (car data))
7378            (> (setq score
7379                     (gnus-summary-article-score (gnus-data-number (car data))))
7380               best)
7381            (setq best score
7382                  article (gnus-data-number (car data))))
7383       (setq data (cdr data)))
7384     (when article
7385       (gnus-summary-goto-subject article))
7386     (gnus-summary-position-point)
7387     article))
7388
7389 (defun gnus-summary-better-unread-subject ()
7390   "Select the first unread subject that has a score over the default score."
7391   (interactive)
7392   (let ((data gnus-newsgroup-data)
7393         article score)
7394     (while (and (setq article (gnus-data-number (car data)))
7395                 (or (gnus-data-read-p (car data))
7396                     (not (> (gnus-summary-article-score article)
7397                             gnus-summary-default-score))))
7398       (setq data (cdr data)))
7399     (when article
7400       (gnus-summary-goto-subject article))
7401     (gnus-summary-position-point)
7402     article))
7403
7404 (defun gnus-summary-last-subject ()
7405   "Go to the last displayed subject line in the group."
7406   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7407     (when article
7408       (gnus-summary-goto-subject article))))
7409
7410 (defun gnus-summary-goto-article (article &optional all-headers force)
7411   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7412 If ALL-HEADERS is non-nil, no header lines are hidden.
7413 If FORCE, go to the article even if it isn't displayed.  If FORCE
7414 is a number, it is the line the article is to be displayed on."
7415   (interactive
7416    (list
7417     (completing-read
7418      "Article number or Message-ID: "
7419      (mapcar (lambda (number) (list (int-to-string number)))
7420              gnus-newsgroup-limit))
7421     current-prefix-arg
7422     t))
7423   (prog1
7424       (if (and (stringp article)
7425                (string-match "@" article))
7426           (gnus-summary-refer-article article)
7427         (when (stringp article)
7428           (setq article (string-to-number article)))
7429         (if (gnus-summary-goto-subject article force)
7430             (gnus-summary-display-article article all-headers)
7431           (gnus-message 4 "Couldn't go to article %s" article) nil))
7432     (gnus-summary-position-point)))
7433
7434 (defun gnus-summary-goto-last-article ()
7435   "Go to the previously read article."
7436   (interactive)
7437   (prog1
7438       (when gnus-last-article
7439         (gnus-summary-goto-article gnus-last-article nil t))
7440     (gnus-summary-position-point)))
7441
7442 (defun gnus-summary-pop-article (number)
7443   "Pop one article off the history and go to the previous.
7444 NUMBER articles will be popped off."
7445   (interactive "p")
7446   (let (to)
7447     (setq gnus-newsgroup-history
7448           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7449     (if to
7450         (gnus-summary-goto-article (car to) nil t)
7451       (error "Article history empty")))
7452   (gnus-summary-position-point))
7453
7454 ;; Summary commands and functions for limiting the summary buffer.
7455
7456 (defun gnus-summary-limit-to-articles (n)
7457   "Limit the summary buffer to the next N articles.
7458 If not given a prefix, use the process marked articles instead."
7459   (interactive "P")
7460   (prog1
7461       (let ((articles (gnus-summary-work-articles n)))
7462         (setq gnus-newsgroup-processable nil)
7463         (gnus-summary-limit articles))
7464     (gnus-summary-position-point)))
7465
7466 (defun gnus-summary-pop-limit (&optional total)
7467   "Restore the previous limit.
7468 If given a prefix, remove all limits."
7469   (interactive "P")
7470   (when total
7471     (setq gnus-newsgroup-limits
7472           (list (mapcar (lambda (h) (mail-header-number h))
7473                         gnus-newsgroup-headers))))
7474   (unless gnus-newsgroup-limits
7475     (error "No limit to pop"))
7476   (prog1
7477       (gnus-summary-limit nil 'pop)
7478     (gnus-summary-position-point)))
7479
7480 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7481   "Limit the summary buffer to articles that have subjects that match a regexp.
7482 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7483   (interactive
7484    (list (read-string (if current-prefix-arg
7485                           "Exclude subject (regexp): "
7486                         "Limit to subject (regexp): "))
7487          nil current-prefix-arg))
7488   (unless header
7489     (setq header "subject"))
7490   (when (not (equal "" subject))
7491     (prog1
7492         (let ((articles (gnus-summary-find-matching
7493                          (or header "subject") subject 'all nil nil
7494                          not-matching)))
7495           (unless articles
7496             (error "Found no matches for \"%s\"" subject))
7497           (gnus-summary-limit articles))
7498       (gnus-summary-position-point))))
7499
7500 (defun gnus-summary-limit-to-author (from &optional not-matching)
7501   "Limit the summary buffer to articles that have authors that match a regexp.
7502 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7503   (interactive
7504    (list (read-string (if current-prefix-arg
7505                           "Exclude author (regexp): "
7506                         "Limit to author (regexp): "))
7507          current-prefix-arg))
7508   (gnus-summary-limit-to-subject from "from" not-matching))
7509
7510 (defun gnus-summary-limit-to-age (age &optional younger-p)
7511   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7512 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7513 articles that are younger than AGE days."
7514   (interactive
7515    (let ((younger current-prefix-arg)
7516          (days-got nil)
7517          days)
7518      (while (not days-got)
7519        (setq days (if younger
7520                       (read-string "Limit to articles younger than (in days, older when negative): ")
7521                     (read-string
7522                      "Limit to articles older than (in days, younger when negative): ")))
7523        (when (> (length days) 0)
7524          (setq days (read days)))
7525        (if (numberp days)
7526            (progn
7527              (setq days-got t)
7528              (if (< days 0)
7529                  (progn
7530                    (setq younger (not younger))
7531                    (setq days (* days -1)))))
7532          (message "Please enter a number.")
7533          (sleep-for 1)))
7534      (list days younger)))
7535   (prog1
7536       (let ((data gnus-newsgroup-data)
7537             (cutoff (days-to-time age))
7538             articles d date is-younger)
7539         (while (setq d (pop data))
7540           (when (and (vectorp (gnus-data-header d))
7541                      (setq date (mail-header-date (gnus-data-header d))))
7542             (setq is-younger (time-less-p
7543                               (time-since (condition-case ()
7544                                               (date-to-time date)
7545                                             (error '(0 0))))
7546                               cutoff))
7547             (when (if younger-p
7548                       is-younger
7549                     (not is-younger))
7550               (push (gnus-data-number d) articles))))
7551         (gnus-summary-limit (nreverse articles)))
7552     (gnus-summary-position-point)))
7553
7554 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7555   "Limit the summary buffer to articles that match an 'extra' header."
7556   (interactive
7557    (let ((header
7558           (intern
7559            (gnus-completing-read-with-default
7560             (symbol-name (car gnus-extra-headers))
7561             (if current-prefix-arg
7562                 "Exclude extra header:"
7563               "Limit extra header:")
7564             (mapcar (lambda (x)
7565                       (cons (symbol-name x) x))
7566                     gnus-extra-headers)
7567             nil
7568             t))))
7569      (list header
7570            (read-string (format "%s header %s (regexp): "
7571                                 (if current-prefix-arg "Exclude" "Limit to")
7572                                 header))
7573            current-prefix-arg)))
7574   (when (not (equal "" regexp))
7575     (prog1
7576         (let ((articles (gnus-summary-find-matching
7577                          (cons 'extra header) regexp 'all nil nil
7578                          not-matching)))
7579           (unless articles
7580             (error "Found no matches for \"%s\"" regexp))
7581           (gnus-summary-limit articles))
7582       (gnus-summary-position-point))))
7583
7584 (defun gnus-summary-limit-to-display-predicate ()
7585   "Limit the summary buffer to the predicated in the `display' group parameter."
7586   (interactive)
7587   (unless gnus-newsgroup-display
7588     (error "There is no `display' group parameter"))
7589   (let (articles)
7590     (dolist (number gnus-newsgroup-articles)
7591       (when (funcall gnus-newsgroup-display)
7592         (push number articles)))
7593     (gnus-summary-limit articles))
7594   (gnus-summary-position-point))
7595
7596 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7597 (make-obsolete
7598  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7599
7600 (defun gnus-summary-limit-to-unread (&optional all)
7601   "Limit the summary buffer to articles that are not marked as read.
7602 If ALL is non-nil, limit strictly to unread articles."
7603   (interactive "P")
7604   (if all
7605       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7606     (gnus-summary-limit-to-marks
7607      ;; Concat all the marks that say that an article is read and have
7608      ;; those removed.
7609      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7610            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7611            gnus-low-score-mark gnus-expirable-mark
7612            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7613            gnus-duplicate-mark gnus-souped-mark)
7614      'reverse)))
7615
7616 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7617 (make-obsolete 'gnus-summary-delete-marked-with
7618                'gnus-summary-limit-exclude-marks)
7619
7620 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7621   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7622 If REVERSE, limit the summary buffer to articles that are marked
7623 with MARKS.  MARKS can either be a string of marks or a list of marks.
7624 Returns how many articles were removed."
7625   (interactive "sMarks: ")
7626   (gnus-summary-limit-to-marks marks t))
7627
7628 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7629   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7630 If REVERSE (the prefix), limit the summary buffer to articles that are
7631 not marked with MARKS.  MARKS can either be a string of marks or a
7632 list of marks.
7633 Returns how many articles were removed."
7634   (interactive "sMarks: \nP")
7635   (prog1
7636       (let ((data gnus-newsgroup-data)
7637             (marks (if (listp marks) marks
7638                      (append marks nil))) ; Transform to list.
7639             articles)
7640         (while data
7641           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7642                   (memq (gnus-data-mark (car data)) marks))
7643             (push (gnus-data-number (car data)) articles))
7644           (setq data (cdr data)))
7645         (gnus-summary-limit articles))
7646     (gnus-summary-position-point)))
7647
7648 (defun gnus-summary-limit-to-score (score)
7649   "Limit to articles with score at or above SCORE."
7650   (interactive "NLimit to articles with score of at least: ")
7651   (let ((data gnus-newsgroup-data)
7652         articles)
7653     (while data
7654       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7655                 score)
7656         (push (gnus-data-number (car data)) articles))
7657       (setq data (cdr data)))
7658     (prog1
7659         (gnus-summary-limit articles)
7660       (gnus-summary-position-point))))
7661
7662 (defun gnus-summary-limit-to-unseen ()
7663   "Limit to unseen articles."
7664   (interactive)
7665   (prog1
7666       (gnus-summary-limit gnus-newsgroup-unseen)
7667     (gnus-summary-position-point)))
7668
7669 (defun gnus-summary-limit-include-thread (id)
7670   "Display all the hidden articles that is in the thread with ID in it.
7671 When called interactively, ID is the Message-ID of the current
7672 article."
7673   (interactive (list (mail-header-id (gnus-summary-article-header))))
7674   (let ((articles (gnus-articles-in-thread
7675                    (gnus-id-to-thread (gnus-root-id id)))))
7676     (prog1
7677         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7678       (gnus-summary-limit-include-matching-articles
7679        "subject"
7680        (regexp-quote (gnus-simplify-subject-re
7681                       (mail-header-subject (gnus-id-to-header id)))))
7682       (gnus-summary-position-point))))
7683
7684 (defun gnus-summary-limit-include-matching-articles (header regexp)
7685   "Display all the hidden articles that have HEADERs that match REGEXP."
7686   (interactive (list (read-string "Match on header: ")
7687                      (read-string "Regexp: ")))
7688   (let ((articles (gnus-find-matching-articles header regexp)))
7689     (prog1
7690         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7691       (gnus-summary-position-point))))
7692
7693 (defun gnus-summary-insert-dormant-articles ()
7694   "Insert all the dormat articles for this group into the current buffer."
7695   (interactive)
7696   (let ((gnus-verbose (max 6 gnus-verbose)))
7697     (if (not gnus-newsgroup-dormant)
7698         (gnus-message 3 "No cached articles for this group")
7699       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7700
7701 (defun gnus-summary-limit-include-dormant ()
7702   "Display all the hidden articles that are marked as dormant.
7703 Note that this command only works on a subset of the articles currently
7704 fetched for this group."
7705   (interactive)
7706   (unless gnus-newsgroup-dormant
7707     (error "There are no dormant articles in this group"))
7708   (prog1
7709       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7710     (gnus-summary-position-point)))
7711
7712 (defun gnus-summary-limit-exclude-dormant ()
7713   "Hide all dormant articles."
7714   (interactive)
7715   (prog1
7716       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7717     (gnus-summary-position-point)))
7718
7719 (defun gnus-summary-limit-exclude-childless-dormant ()
7720   "Hide all dormant articles that have no children."
7721   (interactive)
7722   (let ((data (gnus-data-list t))
7723         articles d children)
7724     ;; Find all articles that are either not dormant or have
7725     ;; children.
7726     (while (setq d (pop data))
7727       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7728                 (and (setq children
7729                            (gnus-article-children (gnus-data-number d)))
7730                      (let (found)
7731                        (while children
7732                          (when (memq (car children) articles)
7733                            (setq children nil
7734                                  found t))
7735                          (pop children))
7736                        found)))
7737         (push (gnus-data-number d) articles)))
7738     ;; Do the limiting.
7739     (prog1
7740         (gnus-summary-limit articles)
7741       (gnus-summary-position-point))))
7742
7743 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7744   "Mark all unread excluded articles as read.
7745 If ALL, mark even excluded ticked and dormants as read."
7746   (interactive "P")
7747   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7748   (let ((articles (gnus-sorted-ndifference
7749                    (sort
7750                     (mapcar (lambda (h) (mail-header-number h))
7751                             gnus-newsgroup-headers)
7752                     '<)
7753                    gnus-newsgroup-limit))
7754         article)
7755     (setq gnus-newsgroup-unreads
7756           (gnus-sorted-intersection gnus-newsgroup-unreads
7757                                     gnus-newsgroup-limit))
7758     (if all
7759         (setq gnus-newsgroup-dormant nil
7760               gnus-newsgroup-marked nil
7761               gnus-newsgroup-reads
7762               (nconc
7763                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7764                gnus-newsgroup-reads))
7765       (while (setq article (pop articles))
7766         (unless (or (memq article gnus-newsgroup-dormant)
7767                     (memq article gnus-newsgroup-marked))
7768           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7769
7770 (defun gnus-summary-limit (articles &optional pop)
7771   (if pop
7772       ;; We pop the previous limit off the stack and use that.
7773       (setq articles (car gnus-newsgroup-limits)
7774             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7775     ;; We use the new limit, so we push the old limit on the stack.
7776     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7777   ;; Set the limit.
7778   (setq gnus-newsgroup-limit articles)
7779   (let ((total (length gnus-newsgroup-data))
7780         (data (gnus-data-find-list (gnus-summary-article-number)))
7781         (gnus-summary-mark-below nil)   ; Inhibit this.
7782         found)
7783     ;; This will do all the work of generating the new summary buffer
7784     ;; according to the new limit.
7785     (gnus-summary-prepare)
7786     ;; Hide any threads, possibly.
7787     (gnus-summary-maybe-hide-threads)
7788     ;; Try to return to the article you were at, or one in the
7789     ;; neighborhood.
7790     (when data
7791       ;; We try to find some article after the current one.
7792       (while data
7793         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7794           (setq data nil
7795                 found t))
7796         (setq data (cdr data))))
7797     (unless found
7798       ;; If there is no data, that means that we were after the last
7799       ;; article.  The same goes when we can't find any articles
7800       ;; after the current one.
7801       (goto-char (point-max))
7802       (gnus-summary-find-prev))
7803     (gnus-set-mode-line 'summary)
7804     ;; We return how many articles were removed from the summary
7805     ;; buffer as a result of the new limit.
7806     (- total (length gnus-newsgroup-data))))
7807
7808 (defsubst gnus-invisible-cut-children (threads)
7809   (let ((num 0))
7810     (while threads
7811       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7812         (incf num))
7813       (pop threads))
7814     (< num 2)))
7815
7816 (defsubst gnus-cut-thread (thread)
7817   "Go forwards in the thread until we find an article that we want to display."
7818   (when (or (eq gnus-fetch-old-headers 'some)
7819             (eq gnus-fetch-old-headers 'invisible)
7820             (numberp gnus-fetch-old-headers)
7821             (eq gnus-build-sparse-threads 'some)
7822             (eq gnus-build-sparse-threads 'more))
7823     ;; Deal with old-fetched headers and sparse threads.
7824     (while (and
7825             thread
7826             (or
7827              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7828              (gnus-summary-article-ancient-p
7829               (mail-header-number (car thread))))
7830             (if (or (<= (length (cdr thread)) 1)
7831                     (eq gnus-fetch-old-headers 'invisible))
7832                 (setq gnus-newsgroup-limit
7833                       (delq (mail-header-number (car thread))
7834                             gnus-newsgroup-limit)
7835                       thread (cadr thread))
7836               (when (gnus-invisible-cut-children (cdr thread))
7837                 (let ((th (cdr thread)))
7838                   (while th
7839                     (if (memq (mail-header-number (caar th))
7840                               gnus-newsgroup-limit)
7841                         (setq thread (car th)
7842                               th nil)
7843                       (setq th (cdr th))))))))))
7844   thread)
7845
7846 (defun gnus-cut-threads (threads)
7847   "Cut off all uninteresting articles from the beginning of threads."
7848   (when (or (eq gnus-fetch-old-headers 'some)
7849             (eq gnus-fetch-old-headers 'invisible)
7850             (numberp gnus-fetch-old-headers)
7851             (eq gnus-build-sparse-threads 'some)
7852             (eq gnus-build-sparse-threads 'more))
7853     (let ((th threads))
7854       (while th
7855         (setcar th (gnus-cut-thread (car th)))
7856         (setq th (cdr th)))))
7857   ;; Remove nixed out threads.
7858   (delq nil threads))
7859
7860 (defun gnus-summary-initial-limit (&optional show-if-empty)
7861   "Figure out what the initial limit is supposed to be on group entry.
7862 This entails weeding out unwanted dormants, low-scored articles,
7863 fetch-old-headers verbiage, and so on."
7864   ;; Most groups have nothing to remove.
7865   (if (or gnus-inhibit-limiting
7866           (and (null gnus-newsgroup-dormant)
7867                (eq gnus-newsgroup-display 'gnus-not-ignore)
7868                (not (eq gnus-fetch-old-headers 'some))
7869                (not (numberp gnus-fetch-old-headers))
7870                (not (eq gnus-fetch-old-headers 'invisible))
7871                (null gnus-summary-expunge-below)
7872                (not (eq gnus-build-sparse-threads 'some))
7873                (not (eq gnus-build-sparse-threads 'more))
7874                (null gnus-thread-expunge-below)
7875                (not gnus-use-nocem)))
7876       ()                                ; Do nothing.
7877     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7878     (setq gnus-newsgroup-limit nil)
7879     (mapatoms
7880      (lambda (node)
7881        (unless (car (symbol-value node))
7882          ;; These threads have no parents -- they are roots.
7883          (let ((nodes (cdr (symbol-value node)))
7884                thread)
7885            (while nodes
7886              (if (and gnus-thread-expunge-below
7887                       (< (gnus-thread-total-score (car nodes))
7888                          gnus-thread-expunge-below))
7889                  (gnus-expunge-thread (pop nodes))
7890                (setq thread (pop nodes))
7891                (gnus-summary-limit-children thread))))))
7892      gnus-newsgroup-dependencies)
7893     ;; If this limitation resulted in an empty group, we might
7894     ;; pop the previous limit and use it instead.
7895     (when (and (not gnus-newsgroup-limit)
7896                show-if-empty)
7897       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7898     gnus-newsgroup-limit))
7899
7900 (defun gnus-summary-limit-children (thread)
7901   "Return 1 if this subthread is visible and 0 if it is not."
7902   ;; First we get the number of visible children to this thread.  This
7903   ;; is done by recursing down the thread using this function, so this
7904   ;; will really go down to a leaf article first, before slowly
7905   ;; working its way up towards the root.
7906   (when thread
7907     (let* ((max-lisp-eval-depth 5000)
7908            (children
7909            (if (cdr thread)
7910                (apply '+ (mapcar 'gnus-summary-limit-children
7911                                  (cdr thread)))
7912              0))
7913           (number (mail-header-number (car thread)))
7914           score)
7915       (if (and
7916            (not (memq number gnus-newsgroup-marked))
7917            (or
7918             ;; If this article is dormant and has absolutely no visible
7919             ;; children, then this article isn't visible.
7920             (and (memq number gnus-newsgroup-dormant)
7921                  (zerop children))
7922             ;; If this is "fetch-old-headered" and there is no
7923             ;; visible children, then we don't want this article.
7924             (and (or (eq gnus-fetch-old-headers 'some)
7925                      (numberp gnus-fetch-old-headers))
7926                  (gnus-summary-article-ancient-p number)
7927                  (zerop children))
7928             ;; If this is "fetch-old-headered" and `invisible', then
7929             ;; we don't want this article.
7930             (and (eq gnus-fetch-old-headers 'invisible)
7931                  (gnus-summary-article-ancient-p number))
7932             ;; If this is a sparsely inserted article with no children,
7933             ;; we don't want it.
7934             (and (eq gnus-build-sparse-threads 'some)
7935                  (gnus-summary-article-sparse-p number)
7936                  (zerop children))
7937             ;; If we use expunging, and this article is really
7938             ;; low-scored, then we don't want this article.
7939             (when (and gnus-summary-expunge-below
7940                        (< (setq score
7941                                 (or (cdr (assq number gnus-newsgroup-scored))
7942                                     gnus-summary-default-score))
7943                           gnus-summary-expunge-below))
7944               ;; We increase the expunge-tally here, but that has
7945               ;; nothing to do with the limits, really.
7946               (incf gnus-newsgroup-expunged-tally)
7947               ;; We also mark as read here, if that's wanted.
7948               (when (and gnus-summary-mark-below
7949                          (< score gnus-summary-mark-below))
7950                 (setq gnus-newsgroup-unreads
7951                       (delq number gnus-newsgroup-unreads))
7952                 (if gnus-newsgroup-auto-expire
7953                     (push number gnus-newsgroup-expirable)
7954                   (push (cons number gnus-low-score-mark)
7955                         gnus-newsgroup-reads)))
7956               t)
7957             ;; Do the `display' group parameter.
7958             (and gnus-newsgroup-display
7959                  (not (funcall gnus-newsgroup-display)))
7960             ;; Check NoCeM things.
7961             (if (and gnus-use-nocem
7962                      (gnus-nocem-unwanted-article-p
7963                       (mail-header-id (car thread))))
7964                 (progn
7965                   (setq gnus-newsgroup-unreads
7966                         (delq number gnus-newsgroup-unreads))
7967                   t))))
7968           ;; Nope, invisible article.
7969           0
7970         ;; Ok, this article is to be visible, so we add it to the limit
7971         ;; and return 1.
7972         (push number gnus-newsgroup-limit)
7973         1))))
7974
7975 (defun gnus-expunge-thread (thread)
7976   "Mark all articles in THREAD as read."
7977   (let* ((number (mail-header-number (car thread))))
7978     (incf gnus-newsgroup-expunged-tally)
7979     ;; We also mark as read here, if that's wanted.
7980     (setq gnus-newsgroup-unreads
7981           (delq number gnus-newsgroup-unreads))
7982     (if gnus-newsgroup-auto-expire
7983         (push number gnus-newsgroup-expirable)
7984       (push (cons number gnus-low-score-mark)
7985             gnus-newsgroup-reads)))
7986   ;; Go recursively through all subthreads.
7987   (mapcar 'gnus-expunge-thread (cdr thread)))
7988
7989 ;; Summary article oriented commands
7990
7991 (defun gnus-summary-refer-parent-article (n)
7992   "Refer parent article N times.
7993 If N is negative, go to ancestor -N instead.
7994 The difference between N and the number of articles fetched is returned."
7995   (interactive "p")
7996   (let ((skip 1)
7997         error header ref)
7998     (when (not (natnump n))
7999       (setq skip (abs n)
8000             n 1))
8001     (while (and (> n 0)
8002                 (not error))
8003       (setq header (gnus-summary-article-header))
8004       (if (and (eq (mail-header-number header)
8005                    (cdr gnus-article-current))
8006                (equal gnus-newsgroup-name
8007                       (car gnus-article-current)))
8008           ;; If we try to find the parent of the currently
8009           ;; displayed article, then we take a look at the actual
8010           ;; References header, since this is slightly more
8011           ;; reliable than the References field we got from the
8012           ;; server.
8013           (save-excursion
8014             (set-buffer gnus-original-article-buffer)
8015             (nnheader-narrow-to-headers)
8016             (unless (setq ref (message-fetch-field "references"))
8017               (setq ref (message-fetch-field "in-reply-to")))
8018             (widen))
8019         (setq ref
8020               ;; It's not the current article, so we take a bet on
8021               ;; the value we got from the server.
8022               (mail-header-references header)))
8023       (if (and ref
8024                (not (equal ref "")))
8025           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8026             (gnus-message 1 "Couldn't find parent"))
8027         (gnus-message 1 "No references in article %d"
8028                       (gnus-summary-article-number))
8029         (setq error t))
8030       (decf n))
8031     (gnus-summary-position-point)
8032     n))
8033
8034 (defun gnus-summary-refer-references ()
8035   "Fetch all articles mentioned in the References header.
8036 Return the number of articles fetched."
8037   (interactive)
8038   (let ((ref (mail-header-references (gnus-summary-article-header)))
8039         (current (gnus-summary-article-number))
8040         (n 0))
8041     (if (or (not ref)
8042             (equal ref ""))
8043         (error "No References in the current article")
8044       ;; For each Message-ID in the References header...
8045       (while (string-match "<[^>]*>" ref)
8046         (incf n)
8047         ;; ... fetch that article.
8048         (gnus-summary-refer-article
8049          (prog1 (match-string 0 ref)
8050            (setq ref (substring ref (match-end 0))))))
8051       (gnus-summary-goto-subject current)
8052       (gnus-summary-position-point)
8053       n)))
8054
8055 (defun gnus-summary-refer-thread (&optional limit)
8056   "Fetch all articles in the current thread.
8057 If LIMIT (the numerical prefix), fetch that many old headers instead
8058 of what's specified by the `gnus-refer-thread-limit' variable."
8059   (interactive "P")
8060   (let ((id (mail-header-id (gnus-summary-article-header)))
8061         (limit (if limit (prefix-numeric-value limit)
8062                  gnus-refer-thread-limit)))
8063     (unless (eq gnus-fetch-old-headers 'invisible)
8064       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8065       ;; Retrieve the headers and read them in.
8066       (if (eq (if (numberp limit)
8067                   (gnus-retrieve-headers
8068                    (list (min
8069                           (+ (mail-header-number
8070                               (gnus-summary-article-header))
8071                              limit)
8072                           gnus-newsgroup-end))
8073                    gnus-newsgroup-name (* limit 2))
8074                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8075                 ;; headers.
8076                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8077                                        gnus-newsgroup-name limit))
8078               'nov)
8079           (gnus-build-all-threads)
8080         (error "Can't fetch thread from backends that don't support NOV"))
8081       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8082     (gnus-summary-limit-include-thread id)))
8083
8084 (defun gnus-summary-refer-article (message-id)
8085   "Fetch an article specified by MESSAGE-ID."
8086   (interactive "sMessage-ID: ")
8087   (when (and (stringp message-id)
8088              (not (zerop (length message-id))))
8089     ;; Construct the correct Message-ID if necessary.
8090     ;; Suggested by tale@pawl.rpi.edu.
8091     (unless (string-match "^<" message-id)
8092       (setq message-id (concat "<" message-id)))
8093     (unless (string-match ">$" message-id)
8094       (setq message-id (concat message-id ">")))
8095     (let* ((header (gnus-id-to-header message-id))
8096            (sparse (and header
8097                         (gnus-summary-article-sparse-p
8098                          (mail-header-number header))
8099                         (memq (mail-header-number header)
8100                               gnus-newsgroup-limit)))
8101            number)
8102       (cond
8103        ;; If the article is present in the buffer we just go to it.
8104        ((and header
8105              (or (not (gnus-summary-article-sparse-p
8106                        (mail-header-number header)))
8107                  sparse))
8108         (prog1
8109             (gnus-summary-goto-article
8110              (mail-header-number header) nil t)
8111           (when sparse
8112             (gnus-summary-update-article (mail-header-number header)))))
8113        (t
8114         ;; We fetch the article.
8115         (catch 'found
8116           (dolist (gnus-override-method (gnus-refer-article-methods))
8117             (when (and (gnus-check-server gnus-override-method)
8118                        ;; Fetch the header,
8119                        (setq number (gnus-summary-insert-subject message-id)))
8120               ;; and display the article.
8121               (gnus-summary-select-article nil nil nil number)
8122               (throw 'found t)))
8123           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8124
8125 (defun gnus-refer-article-methods ()
8126   "Return a list of referable methods."
8127   (cond
8128    ;; No method, so we default to current and native.
8129    ((null gnus-refer-article-method)
8130     (list gnus-current-select-method gnus-select-method))
8131    ;; Current.
8132    ((eq 'current gnus-refer-article-method)
8133     (list gnus-current-select-method))
8134    ;; List of select methods.
8135    ((not (and (symbolp (car gnus-refer-article-method))
8136               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8137     (let (out)
8138       (dolist (method gnus-refer-article-method)
8139         (push (if (eq 'current method)
8140                   gnus-current-select-method
8141                 method)
8142               out))
8143       (nreverse out)))
8144    ;; One single select method.
8145    (t
8146     (list gnus-refer-article-method))))
8147
8148 (defun gnus-summary-edit-parameters ()
8149   "Edit the group parameters of the current group."
8150   (interactive)
8151   (gnus-group-edit-group gnus-newsgroup-name 'params))
8152
8153 (defun gnus-summary-customize-parameters ()
8154   "Customize the group parameters of the current group."
8155   (interactive)
8156   (gnus-group-customize gnus-newsgroup-name))
8157
8158 (defun gnus-summary-enter-digest-group (&optional force)
8159   "Enter an nndoc group based on the current article.
8160 If FORCE, force a digest interpretation.  If not, try
8161 to guess what the document format is."
8162   (interactive "P")
8163   (let ((conf gnus-current-window-configuration))
8164     (save-window-excursion
8165       (save-excursion
8166         (let (gnus-article-prepare-hook
8167               gnus-display-mime-function
8168               gnus-break-pages)
8169           (gnus-summary-select-article))))
8170     (setq gnus-current-window-configuration conf)
8171     (let* ((name (format "%s-%d"
8172                          (gnus-group-prefixed-name
8173                           gnus-newsgroup-name (list 'nndoc ""))
8174                          (save-excursion
8175                            (set-buffer gnus-summary-buffer)
8176                            gnus-current-article)))
8177            (ogroup gnus-newsgroup-name)
8178            (params (append (gnus-info-params (gnus-get-info ogroup))
8179                            (list (cons 'to-group ogroup))
8180                            (list (cons 'save-article-group ogroup))))
8181            (case-fold-search t)
8182            (buf (current-buffer))
8183            dig to-address)
8184       (save-excursion
8185         (set-buffer gnus-original-article-buffer)
8186         ;; Have the digest group inherit the main mail address of
8187         ;; the parent article.
8188         (when (setq to-address (or (gnus-fetch-field "reply-to")
8189                                    (gnus-fetch-field "from")))
8190           (setq params (append
8191                         (list (cons 'to-address
8192                                     (funcall gnus-decode-encoded-word-function
8193                                              to-address))))))
8194         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8195         (insert-buffer-substring gnus-original-article-buffer)
8196         ;; Remove lines that may lead nndoc to misinterpret the
8197         ;; document type.
8198         (narrow-to-region
8199          (goto-char (point-min))
8200          (or (search-forward "\n\n" nil t) (point)))
8201         (goto-char (point-min))
8202         (delete-matching-lines "^Path:\\|^From ")
8203         (widen))
8204       (unwind-protect
8205           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8206                     (gnus-newsgroup-ephemeral-ignored-charsets
8207                      gnus-newsgroup-ignored-charsets))
8208                 (gnus-group-read-ephemeral-group
8209                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8210                               (nndoc-article-type
8211                                ,(if force 'mbox 'guess)))
8212                  t nil nil nil
8213                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8214                                                         "ADAPT")))))
8215               ;; Make all postings to this group go to the parent group.
8216               (nconc (gnus-info-params (gnus-get-info name))
8217                      params)
8218             ;; Couldn't select this doc group.
8219             (switch-to-buffer buf)
8220             (gnus-set-global-variables)
8221             (gnus-configure-windows 'summary)
8222             (gnus-message 3 "Article couldn't be entered?"))
8223         (kill-buffer dig)))))
8224
8225 (defun gnus-summary-read-document (n)
8226   "Open a new group based on the current article(s).
8227 This will allow you to read digests and other similar
8228 documents as newsgroups.
8229 Obeys the standard process/prefix convention."
8230   (interactive "P")
8231   (let* ((articles (gnus-summary-work-articles n))
8232          (ogroup gnus-newsgroup-name)
8233          (params (append (gnus-info-params (gnus-get-info ogroup))
8234                          (list (cons 'to-group ogroup))))
8235          article group egroup groups vgroup)
8236     (while (setq article (pop articles))
8237       (setq group (format "%s-%d" gnus-newsgroup-name article))
8238       (gnus-summary-remove-process-mark article)
8239       (when (gnus-summary-display-article article)
8240         (save-excursion
8241           (with-temp-buffer
8242             (insert-buffer-substring gnus-original-article-buffer)
8243             ;; Remove some headers that may lead nndoc to make
8244             ;; the wrong guess.
8245             (message-narrow-to-head)
8246             (goto-char (point-min))
8247             (delete-matching-lines "^\\(Path\\):\\|^From ")
8248             (widen)
8249             (if (setq egroup
8250                       (gnus-group-read-ephemeral-group
8251                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8252                                      (nndoc-article-type guess))
8253                        t nil t))
8254                 (progn
8255             ;; Make all postings to this group go to the parent group.
8256                   (nconc (gnus-info-params (gnus-get-info egroup))
8257                          params)
8258                   (push egroup groups))
8259               ;; Couldn't select this doc group.
8260               (gnus-error 3 "Article couldn't be entered"))))))
8261     ;; Now we have selected all the documents.
8262     (cond
8263      ((not groups)
8264       (error "None of the articles could be interpreted as documents"))
8265      ((gnus-group-read-ephemeral-group
8266        (setq vgroup (format
8267                      "nnvirtual:%s-%s" gnus-newsgroup-name
8268                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8269        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8270        t
8271        (cons (current-buffer) 'summary)))
8272      (t
8273       (error "Couldn't select virtual nndoc group")))))
8274
8275 (defun gnus-summary-isearch-article (&optional regexp-p)
8276   "Do incremental search forward on the current article.
8277 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8278   (interactive "P")
8279   (gnus-summary-select-article)
8280   (gnus-configure-windows 'article)
8281   (gnus-eval-in-buffer-window gnus-article-buffer
8282     (save-restriction
8283       (widen)
8284       (isearch-forward regexp-p))))
8285
8286 (defun gnus-summary-search-article-forward (regexp &optional backward)
8287   "Search for an article containing REGEXP forward.
8288 If BACKWARD, search backward instead."
8289   (interactive
8290    (list (read-string
8291           (format "Search article %s (regexp%s): "
8292                   (if current-prefix-arg "backward" "forward")
8293                   (if gnus-last-search-regexp
8294                       (concat ", default " gnus-last-search-regexp)
8295                     "")))
8296          current-prefix-arg))
8297   (if (string-equal regexp "")
8298       (setq regexp (or gnus-last-search-regexp ""))
8299     (setq gnus-last-search-regexp regexp)
8300     (setq gnus-article-before-search gnus-current-article))
8301   ;; Intentionally set gnus-last-article.
8302   (setq gnus-last-article gnus-article-before-search)
8303   (let ((gnus-last-article gnus-last-article))
8304     (if (gnus-summary-search-article regexp backward)
8305         (gnus-summary-show-thread)
8306       (error "Search failed: \"%s\"" regexp))))
8307
8308 (defun gnus-summary-search-article-backward (regexp)
8309   "Search for an article containing REGEXP backward."
8310   (interactive
8311    (list (read-string
8312           (format "Search article backward (regexp%s): "
8313                   (if gnus-last-search-regexp
8314                       (concat ", default " gnus-last-search-regexp)
8315                     "")))))
8316   (gnus-summary-search-article-forward regexp 'backward))
8317
8318 (defun gnus-summary-search-article (regexp &optional backward)
8319   "Search for an article containing REGEXP.
8320 Optional argument BACKWARD means do search for backward.
8321 `gnus-select-article-hook' is not called during the search."
8322   ;; We have to require this here to make sure that the following
8323   ;; dynamic binding isn't shadowed by autoloading.
8324   (require 'gnus-async)
8325   (require 'gnus-art)
8326   (let ((gnus-select-article-hook nil)  ;Disable hook.
8327         (gnus-article-prepare-hook nil)
8328         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8329         (gnus-use-article-prefetch nil)
8330         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8331         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8332         (gnus-visual nil)
8333         (gnus-keep-backlog nil)
8334         (gnus-break-pages nil)
8335         (gnus-summary-display-arrow nil)
8336         (gnus-updated-mode-lines nil)
8337         (gnus-auto-center-summary nil)
8338         (sum (current-buffer))
8339         (gnus-display-mime-function nil)
8340         (found nil)
8341         point)
8342     (gnus-save-hidden-threads
8343       (gnus-summary-select-article)
8344       (set-buffer gnus-article-buffer)
8345       (goto-char (window-point (get-buffer-window (current-buffer))))
8346       (when backward
8347         (forward-line -1))
8348       (while (not found)
8349         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8350         (if (if backward
8351                 (re-search-backward regexp nil t)
8352               (re-search-forward regexp nil t))
8353             ;; We found the regexp.
8354             (progn
8355               (setq found 'found)
8356               (beginning-of-line)
8357               (set-window-start
8358                (get-buffer-window (current-buffer))
8359                (point))
8360               (forward-line 1)
8361               (set-window-point
8362                (get-buffer-window (current-buffer))
8363                (point))
8364               (set-buffer sum)
8365               (setq point (point)))
8366           ;; We didn't find it, so we go to the next article.
8367           (set-buffer sum)
8368           (setq found 'not)
8369           (while (eq found 'not)
8370             (if (not (if backward (gnus-summary-find-prev)
8371                        (gnus-summary-find-next)))
8372                 ;; No more articles.
8373                 (setq found t)
8374               ;; Select the next article and adjust point.
8375               (unless (gnus-summary-article-sparse-p
8376                        (gnus-summary-article-number))
8377                 (setq found nil)
8378                 (gnus-summary-select-article)
8379                 (set-buffer gnus-article-buffer)
8380                 (widen)
8381                 (goto-char (if backward (point-max) (point-min))))))))
8382       (gnus-message 7 ""))
8383     ;; Return whether we found the regexp.
8384     (when (eq found 'found)
8385       (goto-char point)
8386       (gnus-summary-show-thread)
8387       (gnus-summary-goto-subject gnus-current-article)
8388       (gnus-summary-position-point)
8389       t)))
8390
8391 (defun gnus-find-matching-articles (header regexp)
8392   "Return a list of all articles that match REGEXP on HEADER.
8393 This search includes all articles in the current group that Gnus has
8394 fetched headers for, whether they are displayed or not."
8395   (let ((articles nil)
8396         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8397         (case-fold-search t))
8398     (dolist (header gnus-newsgroup-headers)
8399       (when (string-match regexp (funcall func header))
8400         (push (mail-header-number header) articles)))
8401     (nreverse articles)))
8402
8403 (defun gnus-summary-find-matching (header regexp &optional backward unread
8404                                           not-case-fold not-matching)
8405   "Return a list of all articles that match REGEXP on HEADER.
8406 The search stars on the current article and goes forwards unless
8407 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8408 If UNREAD is non-nil, only unread articles will
8409 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8410 in the comparisons. If NOT-MATCHING, return a list of all articles that
8411 not match REGEXP on HEADER."
8412   (let ((case-fold-search (not not-case-fold))
8413         articles d func)
8414     (if (consp header)
8415         (if (eq (car header) 'extra)
8416             (setq func
8417                   `(lambda (h)
8418                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8419                          "")))
8420           (error "%s is an invalid header" header))
8421       (unless (fboundp (intern (concat "mail-header-" header)))
8422         (error "%s is not a valid header" header))
8423       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8424     (dolist (d (if (eq backward 'all)
8425                    gnus-newsgroup-data
8426                  (gnus-data-find-list
8427                   (gnus-summary-article-number)
8428                   (gnus-data-list backward))))
8429       (when (and (or (not unread)       ; We want all articles...
8430                      (gnus-data-unread-p d)) ; Or just unreads.
8431                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8432                  (if not-matching
8433                      (not (string-match
8434                            regexp
8435                            (funcall func (gnus-data-header d))))
8436                    (string-match regexp
8437                                  (funcall func (gnus-data-header d)))))
8438         (push (gnus-data-number d) articles))) ; Success!
8439     (nreverse articles)))
8440
8441 (defun gnus-summary-execute-command (header regexp command &optional backward)
8442   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8443 If HEADER is an empty string (or nil), the match is done on the entire
8444 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8445   (interactive
8446    (list (let ((completion-ignore-case t))
8447            (completing-read
8448             "Header name: "
8449             (mapcar (lambda (header) (list (format "%s" header)))
8450                     (append
8451                      '("Number" "Subject" "From" "Lines" "Date"
8452                        "Message-ID" "Xref" "References" "Body")
8453                      gnus-extra-headers))
8454             nil 'require-match))
8455          (read-string "Regexp: ")
8456          (read-key-sequence "Command: ")
8457          current-prefix-arg))
8458   (when (equal header "Body")
8459     (setq header ""))
8460   ;; Hidden thread subtrees must be searched as well.
8461   (gnus-summary-show-all-threads)
8462   ;; We don't want to change current point nor window configuration.
8463   (save-excursion
8464     (save-window-excursion
8465       (let (gnus-visual
8466             gnus-treat-strip-trailing-blank-lines
8467             gnus-treat-strip-leading-blank-lines
8468             gnus-treat-strip-multiple-blank-lines
8469             gnus-treat-hide-boring-headers
8470             gnus-treat-fold-newsgroups
8471             gnus-article-prepare-hook)
8472         (gnus-message 6 "Executing %s..." (key-description command))
8473         ;; We'd like to execute COMMAND interactively so as to give arguments.
8474         (gnus-execute header regexp
8475                       `(call-interactively ',(key-binding command))
8476                       backward)
8477         (gnus-message 6 "Executing %s...done" (key-description command))))))
8478
8479 (defun gnus-summary-beginning-of-article ()
8480   "Scroll the article back to the beginning."
8481   (interactive)
8482   (gnus-summary-select-article)
8483   (gnus-configure-windows 'article)
8484   (gnus-eval-in-buffer-window gnus-article-buffer
8485     (widen)
8486     (goto-char (point-min))
8487     (when gnus-page-broken
8488       (gnus-narrow-to-page))))
8489
8490 (defun gnus-summary-end-of-article ()
8491   "Scroll to the end of the article."
8492   (interactive)
8493   (gnus-summary-select-article)
8494   (gnus-configure-windows 'article)
8495   (gnus-eval-in-buffer-window gnus-article-buffer
8496     (widen)
8497     (goto-char (point-max))
8498     (recenter -3)
8499     (when gnus-page-broken
8500       (gnus-narrow-to-page))))
8501
8502 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8503   "Truncate to LEN and quote all \"(\"'s in STRING."
8504   (gnus-replace-in-string (if (and len (> (length string) len))
8505                               (substring string 0 len)
8506                             string)
8507                           "[()]" "\\\\\\&"))
8508
8509 (defun gnus-summary-print-article (&optional filename n)
8510   "Generate and print a PostScript image of the N next (mail) articles.
8511
8512 If N is negative, print the N previous articles.  If N is nil and articles
8513 have been marked with the process mark, print these instead.
8514
8515 If the optional first argument FILENAME is nil, send the image to the
8516 printer.  If FILENAME is a string, save the PostScript image in a file with
8517 that name.  If FILENAME is a number, prompt the user for the name of the file
8518 to save in."
8519   (interactive (list (ps-print-preprint current-prefix-arg)))
8520   (dolist (article (gnus-summary-work-articles n))
8521     (gnus-summary-select-article nil nil 'pseudo article)
8522     (gnus-eval-in-buffer-window gnus-article-buffer
8523       (gnus-print-buffer))
8524     (gnus-summary-remove-process-mark article))
8525   (ps-despool filename))
8526
8527 (defun gnus-print-buffer ()
8528   (let ((buffer (generate-new-buffer " *print*")))
8529     (unwind-protect
8530         (progn
8531           (copy-to-buffer buffer (point-min) (point-max))
8532           (set-buffer buffer)
8533           (gnus-article-delete-invisible-text)
8534           (gnus-remove-text-with-property 'gnus-decoration)
8535           (when (gnus-visual-p 'article-highlight 'highlight)
8536             ;; Copy-to-buffer doesn't copy overlay.  So redo
8537             ;; highlight.
8538             (let ((gnus-article-buffer buffer))
8539               (gnus-article-highlight-citation t)
8540               (gnus-article-highlight-signature)))
8541           (let ((ps-left-header
8542                  (list
8543                   (concat "("
8544                           (gnus-summary-print-truncate-and-quote
8545                            (mail-header-subject gnus-current-headers)
8546                            66) ")")
8547                   (concat "("
8548                           (gnus-summary-print-truncate-and-quote
8549                            (mail-header-from gnus-current-headers)
8550                            45) ")")))
8551                 (ps-right-header
8552                  (list
8553                   "/pagenumberstring load"
8554                   (concat "("
8555                           (mail-header-date gnus-current-headers) ")"))))
8556             (gnus-run-hooks 'gnus-ps-print-hook)
8557             (save-excursion
8558               (if window-system
8559                   (ps-spool-buffer-with-faces)
8560                 (ps-spool-buffer)))))
8561       (kill-buffer buffer))))
8562
8563 (defun gnus-summary-show-article (&optional arg)
8564   "Force redisplaying of the current article.
8565 If ARG (the prefix) is a number, show the article with the charset
8566 defined in `gnus-summary-show-article-charset-alist', or the charset
8567 input.
8568 If ARG (the prefix) is non-nil and not a number, show the raw article
8569 without any article massaging functions being run.  Normally, the key
8570 strokes are `C-u g'."
8571   (interactive "P")
8572   (cond
8573    ((numberp arg)
8574     (gnus-summary-show-article t)
8575     (let ((gnus-newsgroup-charset
8576            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8577                (mm-read-coding-system
8578                 "View as charset: " ;; actually it is coding system.
8579                 (save-excursion
8580                   (set-buffer gnus-article-buffer)
8581                   (mm-detect-coding-region (point) (point-max))))))
8582           (gnus-newsgroup-ignored-charsets 'gnus-all))
8583       (gnus-summary-select-article nil 'force)
8584       (let ((deps gnus-newsgroup-dependencies)
8585             head header lines)
8586         (save-excursion
8587           (set-buffer gnus-original-article-buffer)
8588           (save-restriction
8589             (message-narrow-to-head)
8590             (setq head (buffer-string))
8591             (goto-char (point-min))
8592             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8593               (goto-char (point-max))
8594               (widen)
8595               (setq lines (1- (count-lines (point) (point-max))))))
8596           (with-temp-buffer
8597             (insert (format "211 %d Article retrieved.\n"
8598                             (cdr gnus-article-current)))
8599             (insert head)
8600             (if lines (insert (format "Lines: %d\n" lines)))
8601             (insert ".\n")
8602             (let ((nntp-server-buffer (current-buffer)))
8603               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8604         (gnus-data-set-header
8605          (gnus-data-find (cdr gnus-article-current))
8606          header)
8607         (gnus-summary-update-article-line
8608          (cdr gnus-article-current) header)
8609         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8610           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8611    ((not arg)
8612     ;; Select the article the normal way.
8613     (gnus-summary-select-article nil 'force))
8614    (t
8615     ;; We have to require this here to make sure that the following
8616     ;; dynamic binding isn't shadowed by autoloading.
8617     (require 'gnus-async)
8618     (require 'gnus-art)
8619     ;; Bind the article treatment functions to nil.
8620     (let ((gnus-have-all-headers t)
8621           gnus-article-prepare-hook
8622           gnus-article-decode-hook
8623           gnus-display-mime-function
8624           gnus-break-pages)
8625       ;; Destroy any MIME parts.
8626       (when (gnus-buffer-live-p gnus-article-buffer)
8627         (save-excursion
8628           (set-buffer gnus-article-buffer)
8629           (mm-destroy-parts gnus-article-mime-handles)
8630           ;; Set it to nil for safety reason.
8631           (setq gnus-article-mime-handle-alist nil)
8632           (setq gnus-article-mime-handles nil)))
8633       (gnus-summary-select-article nil 'force))))
8634   (gnus-summary-goto-subject gnus-current-article)
8635   (gnus-summary-position-point))
8636
8637 (defun gnus-summary-show-raw-article ()
8638   "Show the raw article without any article massaging functions being run."
8639   (interactive)
8640   (gnus-summary-show-article t))
8641
8642 (defun gnus-summary-verbose-headers (&optional arg)
8643   "Toggle permanent full header display.
8644 If ARG is a positive number, turn header display on.
8645 If ARG is a negative number, turn header display off."
8646   (interactive "P")
8647   (setq gnus-show-all-headers
8648         (cond ((or (not (numberp arg))
8649                    (zerop arg))
8650                (not gnus-show-all-headers))
8651               ((natnump arg)
8652                t)))
8653   (gnus-summary-show-article))
8654
8655 (defun gnus-summary-toggle-header (&optional arg)
8656   "Show the headers if they are hidden, or hide them if they are shown.
8657 If ARG is a positive number, show the entire header.
8658 If ARG is a negative number, hide the unwanted header lines."
8659   (interactive "P")
8660   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8661                      (get-buffer-window gnus-article-buffer t))))
8662     (with-current-buffer gnus-article-buffer
8663       (widen)
8664       (article-narrow-to-head)
8665       (let* ((buffer-read-only nil)
8666              (inhibit-point-motion-hooks t)
8667              (hidden (if (numberp arg)
8668                          (>= arg 0)
8669                        (gnus-article-hidden-text-p 'headers)))
8670              s e)
8671         (delete-region (point-min) (point-max))
8672         (with-current-buffer gnus-original-article-buffer
8673           (goto-char (setq s (point-min)))
8674           (setq e (if (search-forward "\n\n" nil t)
8675                       (1- (point))
8676                     (point-max))))
8677         (insert-buffer-substring gnus-original-article-buffer s e)
8678         (run-hooks 'gnus-article-decode-hook)
8679         (if hidden
8680             (let ((gnus-treat-hide-headers nil)
8681                   (gnus-treat-hide-boring-headers nil))
8682               (gnus-delete-wash-type 'headers)
8683               (gnus-treat-article 'head))
8684           (gnus-treat-article 'head))
8685         (widen)
8686         (if window
8687             (set-window-start window (goto-char (point-min))))
8688         (setq gnus-page-broken
8689               (when gnus-break-pages
8690                 (gnus-narrow-to-page)
8691                 t))
8692         (gnus-set-mode-line 'article)))))
8693
8694 (defun gnus-summary-show-all-headers ()
8695   "Make all header lines visible."
8696   (interactive)
8697   (gnus-summary-toggle-header 1))
8698
8699 (defun gnus-summary-caesar-message (&optional arg)
8700   "Caesar rotate the current article by 13.
8701 The numerical prefix specifies how many places to rotate each letter
8702 forward."
8703   (interactive "P")
8704   (gnus-summary-select-article)
8705   (let ((mail-header-separator ""))
8706     (gnus-eval-in-buffer-window gnus-article-buffer
8707       (save-restriction
8708         (widen)
8709         (let ((start (window-start))
8710               buffer-read-only)
8711           (message-caesar-buffer-body arg)
8712           (set-window-start (get-buffer-window (current-buffer)) start))))))
8713
8714 (autoload 'unmorse-region "morse"
8715   "Convert morse coded text in region to ordinary ASCII text."
8716   t)
8717
8718 (defun gnus-summary-morse-message (&optional arg)
8719   "Morse decode the current article."
8720   (interactive "P")
8721   (gnus-summary-select-article)
8722   (let ((mail-header-separator ""))
8723     (gnus-eval-in-buffer-window gnus-article-buffer
8724       (save-excursion
8725         (save-restriction
8726           (widen)
8727           (let ((pos (window-start))
8728                 buffer-read-only)
8729             (goto-char (point-min))
8730             (when (message-goto-body)
8731               (gnus-narrow-to-body))
8732             (goto-char (point-min))
8733             (while (re-search-forward "·" (point-max) t)
8734               (replace-match "."))
8735             (unmorse-region (point-min) (point-max))
8736             (widen)
8737             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8738
8739 (defun gnus-summary-stop-page-breaking ()
8740   "Stop page breaking in the current article."
8741   (interactive)
8742   (gnus-summary-select-article)
8743   (gnus-eval-in-buffer-window gnus-article-buffer
8744     (widen)
8745     (when (gnus-visual-p 'page-marker)
8746       (let ((buffer-read-only nil))
8747         (gnus-remove-text-with-property 'gnus-prev)
8748         (gnus-remove-text-with-property 'gnus-next))
8749       (setq gnus-page-broken nil))))
8750
8751 (defun gnus-summary-move-article (&optional n to-newsgroup
8752                                             select-method action)
8753   "Move the current article to a different newsgroup.
8754 If N is a positive number, move the N next articles.
8755 If N is a negative number, move the N previous articles.
8756 If N is nil and any articles have been marked with the process mark,
8757 move those articles instead.
8758 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8759 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8760 re-spool using this method.
8761
8762 When called interactively with TO-NEWSGROUP being nil, the value of
8763 the variable `gnus-move-split-methods' is used for finding a default
8764 for the target newsgroup.
8765
8766 For this function to work, both the current newsgroup and the
8767 newsgroup that you want to move to have to support the `request-move'
8768 and `request-accept' functions.
8769
8770 ACTION can be either `move' (the default), `crosspost' or `copy'."
8771   (interactive "P")
8772   (unless action
8773     (setq action 'move))
8774   ;; Check whether the source group supports the required functions.
8775   (cond ((and (eq action 'move)
8776               (not (gnus-check-backend-function
8777                     'request-move-article gnus-newsgroup-name)))
8778          (error "The current group does not support article moving"))
8779         ((and (eq action 'crosspost)
8780               (not (gnus-check-backend-function
8781                     'request-replace-article gnus-newsgroup-name)))
8782          (error "The current group does not support article editing")))
8783   (let ((articles (gnus-summary-work-articles n))
8784         (prefix (if (gnus-check-backend-function
8785                      'request-move-article gnus-newsgroup-name)
8786                     (gnus-group-real-prefix gnus-newsgroup-name)
8787                   ""))
8788         (names '((move "Move" "Moving")
8789                  (copy "Copy" "Copying")
8790                  (crosspost "Crosspost" "Crossposting")))
8791         (copy-buf (save-excursion
8792                     (nnheader-set-temp-buffer " *copy article*")))
8793         art-group to-method new-xref article to-groups)
8794     (unless (assq action names)
8795       (error "Unknown action %s" action))
8796     ;; Read the newsgroup name.
8797     (when (and (not to-newsgroup)
8798                (not select-method))
8799       (if (and gnus-move-split-methods
8800                (not
8801                 (and (memq gnus-current-article articles)
8802                      (gnus-buffer-live-p gnus-original-article-buffer))))
8803           ;; When `gnus-move-split-methods' is non-nil, we have to
8804           ;; select an article to give `gnus-read-move-group-name' an
8805           ;; opportunity to suggest an appropriate default.  However,
8806           ;; we needn't render or mark the article.
8807           (let ((gnus-display-mime-function nil)
8808                 (gnus-article-prepare-hook nil)
8809                 (gnus-mark-article-hook nil))
8810             (gnus-summary-select-article nil nil nil (car articles))))
8811       (setq to-newsgroup
8812             (gnus-read-move-group-name
8813              (cadr (assq action names))
8814              (symbol-value (intern (format "gnus-current-%s-group" action)))
8815              articles prefix))
8816       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8817     (setq to-method (or select-method
8818                         (gnus-server-to-method
8819                          (gnus-group-method to-newsgroup))))
8820     ;; Check the method we are to move this article to...
8821     (unless (gnus-check-backend-function
8822              'request-accept-article (car to-method))
8823       (error "%s does not support article copying" (car to-method)))
8824     (unless (gnus-check-server to-method)
8825       (error "Can't open server %s" (car to-method)))
8826     (gnus-message 6 "%s to %s: %s..."
8827                   (caddr (assq action names))
8828                   (or (car select-method) to-newsgroup) articles)
8829     (while articles
8830       (setq article (pop articles))
8831       (setq
8832        art-group
8833        (cond
8834         ;; Move the article.
8835         ((eq action 'move)
8836          ;; Remove this article from future suppression.
8837          (gnus-dup-unsuppress-article article)
8838          (gnus-request-move-article
8839           article                       ; Article to move
8840           gnus-newsgroup-name           ; From newsgroup
8841           (nth 1 (gnus-find-method-for-group
8842                   gnus-newsgroup-name)) ; Server
8843           (list 'gnus-request-accept-article
8844                 to-newsgroup (list 'quote select-method)
8845                 (not articles) t)       ; Accept form
8846           (not articles)))              ; Only save nov last time
8847         ;; Copy the article.
8848         ((eq action 'copy)
8849          (save-excursion
8850            (set-buffer copy-buf)
8851            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8852              (gnus-request-accept-article
8853               to-newsgroup select-method (not articles) t))))
8854         ;; Crosspost the article.
8855         ((eq action 'crosspost)
8856          (let ((xref (message-tokenize-header
8857                       (mail-header-xref (gnus-summary-article-header article))
8858                       " ")))
8859            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8860                                   ":" (number-to-string article)))
8861            (unless xref
8862              (setq xref (list (system-name))))
8863            (setq new-xref
8864                  (concat
8865                   (mapconcat 'identity
8866                              (delete "Xref:" (delete new-xref xref))
8867                              " ")
8868                   " " new-xref))
8869            (save-excursion
8870              (set-buffer copy-buf)
8871              ;; First put the article in the destination group.
8872              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8873              (when (consp (setq art-group
8874                                 (gnus-request-accept-article
8875                                  to-newsgroup select-method (not articles))))
8876                (setq new-xref (concat new-xref " " (car art-group)
8877                                       ":"
8878                                       (number-to-string (cdr art-group))))
8879                ;; Now we have the new Xrefs header, so we insert
8880                ;; it and replace the new article.
8881                (nnheader-replace-header "Xref" new-xref)
8882                (gnus-request-replace-article
8883                 (cdr art-group) to-newsgroup (current-buffer))
8884                art-group))))))
8885       (cond
8886        ((not art-group)
8887         (gnus-message 1 "Couldn't %s article %s: %s"
8888                       (cadr (assq action names)) article
8889                       (nnheader-get-report (car to-method))))
8890        ((eq art-group 'junk)
8891         (when (eq action 'move)
8892           (gnus-summary-mark-article article gnus-canceled-mark)
8893           (gnus-message 4 "Deleted article %s" article)
8894           ;; run the delete hook
8895           (run-hook-with-args 'gnus-summary-article-delete-hook
8896                               action
8897                               (gnus-data-header
8898                                (assoc article (gnus-data-list nil)))
8899                               gnus-newsgroup-name nil
8900                               select-method)))
8901        (t
8902         (let* ((pto-group (gnus-group-prefixed-name
8903                            (car art-group) to-method))
8904                (entry
8905                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8906                (info (nth 2 entry))
8907                (to-group (gnus-info-group info))
8908                to-marks)
8909           ;; Update the group that has been moved to.
8910           (when (and info
8911                      (memq action '(move copy)))
8912             (unless (member to-group to-groups)
8913               (push to-group to-groups))
8914
8915             (unless (memq article gnus-newsgroup-unreads)
8916               (push 'read to-marks)
8917               (gnus-info-set-read
8918                info (gnus-add-to-range (gnus-info-read info)
8919                                        (list (cdr art-group)))))
8920
8921             ;; See whether the article is to be put in the cache.
8922             (let ((marks gnus-article-mark-lists)
8923                   (to-article (cdr art-group)))
8924
8925               ;; Enter the article into the cache in the new group,
8926               ;; if that is required.
8927               (when gnus-use-cache
8928                 (gnus-cache-possibly-enter-article
8929                  to-group to-article
8930                  (memq article gnus-newsgroup-marked)
8931                  (memq article gnus-newsgroup-dormant)
8932                  (memq article gnus-newsgroup-unreads)))
8933
8934               (when gnus-preserve-marks
8935                 ;; Copy any marks over to the new group.
8936                 (when (and (equal to-group gnus-newsgroup-name)
8937                            (not (memq article gnus-newsgroup-unreads)))
8938                   ;; Mark this article as read in this group.
8939                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8940                   (setcdr (gnus-active to-group) to-article)
8941                   (setcdr gnus-newsgroup-active to-article))
8942
8943                 (while marks
8944                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8945                     (when (memq article (symbol-value
8946                                          (intern (format "gnus-newsgroup-%s"
8947                                                          (caar marks)))))
8948                       (push (cdar marks) to-marks)
8949                       ;; If the other group is the same as this group,
8950                       ;; then we have to add the mark to the list.
8951                       (when (equal to-group gnus-newsgroup-name)
8952                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8953                              (cons to-article
8954                                    (symbol-value
8955                                     (intern (format "gnus-newsgroup-%s"
8956                                                     (caar marks)))))))
8957                       ;; Copy the marks to other group.
8958                       (gnus-add-marked-articles
8959                        to-group (cdar marks) (list to-article) info)))
8960                   (setq marks (cdr marks)))
8961
8962                 (gnus-request-set-mark
8963                  to-group (list (list (list to-article) 'add to-marks))))
8964
8965               (gnus-dribble-enter
8966                (concat "(gnus-group-set-info '"
8967                        (gnus-prin1-to-string (gnus-get-info to-group))
8968                        ")"))))
8969
8970           ;; Update the Xref header in this article to point to
8971           ;; the new crossposted article we have just created.
8972           (when (eq action 'crosspost)
8973             (save-excursion
8974               (set-buffer copy-buf)
8975               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8976               (nnheader-replace-header "Xref" new-xref)
8977               (gnus-request-replace-article
8978                article gnus-newsgroup-name (current-buffer))))
8979
8980           ;; run the move/copy/crosspost/respool hook
8981           (run-hook-with-args 'gnus-summary-article-move-hook 
8982                               action
8983                               (gnus-data-header 
8984                                (assoc article (gnus-data-list nil)))
8985                               gnus-newsgroup-name
8986                               to-newsgroup
8987                               select-method))
8988
8989         ;;;!!!Why is this necessary?
8990         (set-buffer gnus-summary-buffer)
8991         
8992         (gnus-summary-goto-subject article)
8993         (when (eq action 'move)
8994           (gnus-summary-mark-article article gnus-canceled-mark))))
8995       (gnus-summary-remove-process-mark article))
8996     ;; Re-activate all groups that have been moved to.
8997     (save-excursion
8998       (set-buffer gnus-group-buffer)
8999       (let ((gnus-group-marked to-groups))
9000         (gnus-group-get-new-news-this-group nil t)))
9001
9002     (gnus-kill-buffer copy-buf)
9003     (gnus-summary-position-point)
9004     (gnus-set-mode-line 'summary)))
9005
9006 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9007   "Move the current article to a different newsgroup.
9008 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9009 When called interactively, if TO-NEWSGROUP is nil, use the value of
9010 the variable `gnus-move-split-methods' for finding a default target
9011 newsgroup.
9012 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9013 re-spool using this method."
9014   (interactive "P")
9015   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9016
9017 (defun gnus-summary-crosspost-article (&optional n)
9018   "Crosspost the current article to some other group."
9019   (interactive "P")
9020   (gnus-summary-move-article n nil nil 'crosspost))
9021
9022 (defcustom gnus-summary-respool-default-method nil
9023   "Default method type for respooling an article.
9024 If nil, use to the current newsgroup method."
9025   :type 'symbol
9026   :group 'gnus-summary-mail)
9027
9028 (defcustom gnus-summary-display-while-building nil
9029   "If non-nil, show and update the summary buffer as it's being built.
9030 If the value is t, update the buffer after every line is inserted.  If
9031 the value is an integer (N), update the display every N lines."
9032   :group 'gnus-thread
9033   :type '(choice (const :tag "off" nil)
9034                  number
9035                  (const :tag "frequently" t)))
9036
9037 (defun gnus-summary-respool-article (&optional n method)
9038   "Respool the current article.
9039 The article will be squeezed through the mail spooling process again,
9040 which means that it will be put in some mail newsgroup or other
9041 depending on `nnmail-split-methods'.
9042 If N is a positive number, respool the N next articles.
9043 If N is a negative number, respool the N previous articles.
9044 If N is nil and any articles have been marked with the process mark,
9045 respool those articles instead.
9046
9047 Respooling can be done both from mail groups and \"real\" newsgroups.
9048 In the former case, the articles in question will be moved from the
9049 current group into whatever groups they are destined to.  In the
9050 latter case, they will be copied into the relevant groups."
9051   (interactive
9052    (list current-prefix-arg
9053          (let* ((methods (gnus-methods-using 'respool))
9054                 (methname
9055                  (symbol-name (or gnus-summary-respool-default-method
9056                                   (car (gnus-find-method-for-group
9057                                         gnus-newsgroup-name)))))
9058                 (method
9059                  (gnus-completing-read-with-default
9060                   methname "What backend do you want to use when respooling?"
9061                   methods nil t nil 'gnus-mail-method-history))
9062                 ms)
9063            (cond
9064             ((zerop (length (setq ms (gnus-servers-using-backend
9065                                       (intern method)))))
9066              (list (intern method) ""))
9067             ((= 1 (length ms))
9068              (car ms))
9069             (t
9070              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9071                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9072                            ms-alist))))))))
9073   (unless method
9074     (error "No method given for respooling"))
9075   (if (assoc (symbol-name
9076               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9077              (gnus-methods-using 'respool))
9078       (gnus-summary-move-article n nil method)
9079     (gnus-summary-copy-article n nil method)))
9080
9081 (defun gnus-summary-import-article (file &optional edit)
9082   "Import an arbitrary file into a mail newsgroup."
9083   (interactive "fImport file: \nP")
9084   (let ((group gnus-newsgroup-name)
9085         (now (current-time))
9086         atts lines group-art)
9087     (unless (gnus-check-backend-function 'request-accept-article group)
9088       (error "%s does not support article importing" group))
9089     (or (file-readable-p file)
9090         (not (file-regular-p file))
9091         (error "Can't read %s" file))
9092     (save-excursion
9093       (set-buffer (gnus-get-buffer-create " *import file*"))
9094       (erase-buffer)
9095       (nnheader-insert-file-contents file)
9096       (goto-char (point-min))
9097       (if (nnheader-article-p)
9098           (save-restriction
9099             (goto-char (point-min))
9100             (search-forward "\n\n" nil t)
9101             (narrow-to-region (point-min) (1- (point)))
9102             (goto-char (point-min))
9103             (unless (re-search-forward "^date:" nil t)
9104               (goto-char (point-max))
9105               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9106        ;; This doesn't look like an article, so we fudge some headers.
9107         (setq atts (file-attributes file)
9108               lines (count-lines (point-min) (point-max)))
9109         (insert "From: " (read-string "From: ") "\n"
9110                 "Subject: " (read-string "Subject: ") "\n"
9111                 "Date: " (message-make-date (nth 5 atts)) "\n"
9112                 "Message-ID: " (message-make-message-id) "\n"
9113                 "Lines: " (int-to-string lines) "\n"
9114                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9115       (setq group-art (gnus-request-accept-article group nil t))
9116       (kill-buffer (current-buffer)))
9117     (setq gnus-newsgroup-active (gnus-activate-group group))
9118     (forward-line 1)
9119     (gnus-summary-goto-article (cdr group-art) nil t)
9120     (when edit
9121       (gnus-summary-edit-article))))
9122
9123 (defun gnus-summary-create-article ()
9124   "Create an article in a mail newsgroup."
9125   (interactive)
9126   (let ((group gnus-newsgroup-name)
9127         (now (current-time))
9128         group-art)
9129     (unless (gnus-check-backend-function 'request-accept-article group)
9130       (error "%s does not support article importing" group))
9131     (save-excursion
9132       (set-buffer (gnus-get-buffer-create " *import file*"))
9133       (erase-buffer)
9134       (goto-char (point-min))
9135       ;; This doesn't look like an article, so we fudge some headers.
9136       (insert "From: " (read-string "From: ") "\n"
9137               "Subject: " (read-string "Subject: ") "\n"
9138               "Date: " (message-make-date now) "\n"
9139               "Message-ID: " (message-make-message-id) "\n")
9140       (setq group-art (gnus-request-accept-article group nil t))
9141       (kill-buffer (current-buffer)))
9142     (setq gnus-newsgroup-active (gnus-activate-group group))
9143     (forward-line 1)
9144     (gnus-summary-goto-article (cdr group-art) nil t)
9145     (gnus-summary-edit-article)))
9146
9147 (defun gnus-summary-article-posted-p ()
9148   "Say whether the current (mail) article is available from news as well.
9149 This will be the case if the article has both been mailed and posted."
9150   (interactive)
9151   (let ((id (mail-header-references (gnus-summary-article-header)))
9152         (gnus-override-method (car (gnus-refer-article-methods))))
9153     (if (gnus-request-head id "")
9154         (gnus-message 2 "The current message was found on %s"
9155                       gnus-override-method)
9156       (gnus-message 2 "The current message couldn't be found on %s"
9157                     gnus-override-method)
9158       nil)))
9159
9160 (defun gnus-summary-expire-articles (&optional now)
9161   "Expire all articles that are marked as expirable in the current group."
9162   (interactive)
9163   (when (and (not gnus-group-is-exiting-without-update-p)
9164              (gnus-check-backend-function
9165               'request-expire-articles gnus-newsgroup-name))
9166     ;; This backend supports expiry.
9167     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9168            (expirable (if total
9169                           (progn
9170                             ;; We need to update the info for
9171                             ;; this group for `gnus-list-of-read-articles'
9172                             ;; to give us the right answer.
9173                             (gnus-run-hooks 'gnus-exit-group-hook)
9174                             (gnus-summary-update-info)
9175                             (gnus-list-of-read-articles gnus-newsgroup-name))
9176                         (setq gnus-newsgroup-expirable
9177                               (sort gnus-newsgroup-expirable '<))))
9178            (expiry-wait (if now 'immediate
9179                           (gnus-group-find-parameter
9180                            gnus-newsgroup-name 'expiry-wait)))
9181            (nnmail-expiry-target
9182             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9183                 nnmail-expiry-target))
9184            es)
9185       (when expirable
9186         ;; There are expirable articles in this group, so we run them
9187         ;; through the expiry process.
9188         (gnus-message 6 "Expiring articles...")
9189         (unless (gnus-check-group gnus-newsgroup-name)
9190           (error "Can't open server for %s" gnus-newsgroup-name))
9191         ;; The list of articles that weren't expired is returned.
9192         (save-excursion
9193           (if expiry-wait
9194               (let ((nnmail-expiry-wait-function nil)
9195                     (nnmail-expiry-wait expiry-wait))
9196                 (setq es (gnus-request-expire-articles
9197                           expirable gnus-newsgroup-name)))
9198             (setq es (gnus-request-expire-articles
9199                       expirable gnus-newsgroup-name)))
9200           (unless total
9201             (setq gnus-newsgroup-expirable es))
9202           ;; We go through the old list of expirable, and mark all
9203           ;; really expired articles as nonexistent.
9204           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9205             (let ((gnus-use-cache nil))
9206               (dolist (article expirable)
9207                 (when (and (not (memq article es))
9208                            (gnus-data-find article))
9209                   (gnus-summary-mark-article article gnus-canceled-mark)
9210                   (run-hook-with-args 'gnus-summary-article-expire-hook
9211                                       'delete
9212                                       (gnus-data-header
9213                                        (assoc article (gnus-data-list nil)))
9214                                       gnus-newsgroup-name
9215                                       nil
9216                                       nil))))))
9217         (gnus-message 6 "Expiring articles...done")))))
9218
9219 (defun gnus-summary-expire-articles-now ()
9220   "Expunge all expirable articles in the current group.
9221 This means that *all* articles that are marked as expirable will be
9222 deleted forever, right now."
9223   (interactive)
9224   (or gnus-expert-user
9225       (gnus-yes-or-no-p
9226        "Are you really, really, really sure you want to delete all these messages? ")
9227       (error "Phew!"))
9228   (gnus-summary-expire-articles t))
9229
9230 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9231 (defun gnus-summary-delete-article (&optional n)
9232   "Delete the N next (mail) articles.
9233 This command actually deletes articles.  This is not a marking
9234 command.  The article will disappear forever from your life, never to
9235 return.
9236
9237 If N is negative, delete backwards.
9238 If N is nil and articles have been marked with the process mark,
9239 delete these instead.
9240
9241 If `gnus-novice-user' is non-nil you will be asked for
9242 confirmation before the articles are deleted."
9243   (interactive "P")
9244   (unless (gnus-check-backend-function 'request-expire-articles
9245                                        gnus-newsgroup-name)
9246     (error "The current newsgroup does not support article deletion"))
9247   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9248     (error "Couldn't open server"))
9249   ;; Compute the list of articles to delete.
9250   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9251         (nnmail-expiry-target 'delete)
9252         not-deleted)
9253     (if (and gnus-novice-user
9254              (not (gnus-yes-or-no-p
9255                    (format "Do you really want to delete %s forever? "
9256                            (if (> (length articles) 1)
9257                                (format "these %s articles" (length articles))
9258                              "this article")))))
9259         ()
9260       ;; Delete the articles.
9261       (setq not-deleted (gnus-request-expire-articles
9262                          articles gnus-newsgroup-name 'force))
9263       (while articles
9264         (gnus-summary-remove-process-mark (car articles))
9265         ;; The backend might not have been able to delete the article
9266         ;; after all.
9267         (unless (memq (car articles) not-deleted)
9268           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9269         (let* ((article (car articles))
9270                (id (mail-header-id (gnus-data-header 
9271                                     (assoc article (gnus-data-list nil))))))
9272           (run-hook-with-args 'gnus-summary-article-delete-hook
9273                               'delete id gnus-newsgroup-name nil
9274                               nil))
9275         (setq articles (cdr articles)))
9276       (when not-deleted
9277         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9278     (gnus-summary-position-point)
9279     (gnus-set-mode-line 'summary)
9280     not-deleted))
9281
9282 (defun gnus-summary-edit-article (&optional arg)
9283   "Edit the current article.
9284 This will have permanent effect only in mail groups.
9285 If ARG is nil, edit the decoded articles.
9286 If ARG is 1, edit the raw articles.
9287 If ARG is 2, edit the raw articles even in read-only groups.
9288 If ARG is 3, edit the articles with the current handles.
9289 Otherwise, allow editing of articles even in read-only
9290 groups."
9291   (interactive "P")
9292   (let (force raw current-handles)
9293     (cond
9294      ((null arg))
9295      ((eq arg 1)
9296       (setq raw t))
9297      ((eq arg 2)
9298       (setq raw t
9299             force t))
9300      ((eq arg 3)
9301       (setq current-handles
9302             (and (gnus-buffer-live-p gnus-article-buffer)
9303                  (with-current-buffer gnus-article-buffer
9304                    (prog1
9305                        gnus-article-mime-handles
9306                      (setq gnus-article-mime-handles nil))))))
9307      (t
9308       (setq force t)))
9309     (when (and raw (not force)
9310                (member gnus-newsgroup-name '("nndraft:delayed"
9311                                              "nndraft:drafts"
9312                                              "nndraft:queue")))
9313       (error "Can't edit the raw article in group %s"
9314              gnus-newsgroup-name))
9315     (save-excursion
9316       (set-buffer gnus-summary-buffer)
9317       (let ((mail-parse-charset gnus-newsgroup-charset)
9318             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9319         (gnus-set-global-variables)
9320         (when (and (not force)
9321                    (gnus-group-read-only-p))
9322           (error "The current newsgroup does not support article editing"))
9323         (gnus-summary-show-article t)
9324         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9325           (with-current-buffer gnus-article-buffer
9326             (mm-enable-multibyte)))
9327         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9328             (setq raw t))
9329         (gnus-article-edit-article
9330          (if raw 'ignore
9331            `(lambda ()
9332               (let ((mbl mml-buffer-list))
9333                 (setq mml-buffer-list nil)
9334                 (mime-to-mml ,'current-handles)
9335                 (let ((mbl1 mml-buffer-list))
9336                   (setq mml-buffer-list mbl)
9337                   (set (make-local-variable 'mml-buffer-list) mbl1))
9338                 (gnus-make-local-hook 'kill-buffer-hook)
9339                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9340          `(lambda (no-highlight)
9341             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9342                   (message-options message-options)
9343                   (message-options-set-recipient)
9344                   (mail-parse-ignored-charsets
9345                    ',gnus-newsgroup-ignored-charsets))
9346               ,(if (not raw) '(progn
9347                                 (mml-to-mime)
9348                                 (mml-destroy-buffers)
9349                                 (remove-hook 'kill-buffer-hook
9350                                              'mml-destroy-buffers t)
9351                                 (kill-local-variable 'mml-buffer-list)))
9352               (gnus-summary-edit-article-done
9353                ,(or (mail-header-references gnus-current-headers) "")
9354                ,(gnus-group-read-only-p)
9355                ,gnus-summary-buffer no-highlight))))))))
9356
9357 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9358
9359 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9360                                                  no-highlight)
9361   "Make edits to the current article permanent."
9362   (interactive)
9363   (save-excursion
9364    ;; The buffer restriction contains the entire article if it exists.
9365     (when (article-goto-body)
9366       (let ((lines (count-lines (point) (point-max)))
9367             (length (- (point-max) (point)))
9368             (case-fold-search t)
9369             (body (copy-marker (point))))
9370         (goto-char (point-min))
9371         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9372           (delete-region (match-beginning 1) (match-end 1))
9373           (insert (number-to-string length)))
9374         (goto-char (point-min))
9375         (when (re-search-forward
9376                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9377           (delete-region (match-beginning 1) (match-end 1))
9378           (insert (number-to-string length)))
9379         (goto-char (point-min))
9380         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9381           (delete-region (match-beginning 1) (match-end 1))
9382           (insert (number-to-string lines))))))
9383   ;; Replace the article.
9384   (let ((buf (current-buffer)))
9385     (with-temp-buffer
9386       (insert-buffer-substring buf)
9387
9388       (if (and (not read-only)
9389                (not (gnus-request-replace-article
9390                      (cdr gnus-article-current) (car gnus-article-current)
9391                      (current-buffer) t)))
9392           (error "Couldn't replace article")
9393         ;; Update the summary buffer.
9394         (if (and references
9395                  (equal (message-tokenize-header references " ")
9396                         (message-tokenize-header
9397                          (or (message-fetch-field "references") "") " ")))
9398             ;; We only have to update this line.
9399             (save-excursion
9400               (save-restriction
9401                 (message-narrow-to-head)
9402                 (let ((head (buffer-string))
9403                       header)
9404                   (with-temp-buffer
9405                     (insert (format "211 %d Article retrieved.\n"
9406                                     (cdr gnus-article-current)))
9407                     (insert head)
9408                     (insert ".\n")
9409                     (let ((nntp-server-buffer (current-buffer)))
9410                       (setq header (car (gnus-get-newsgroup-headers
9411                                          nil t))))
9412                     (save-excursion
9413                       (set-buffer gnus-summary-buffer)
9414                       (gnus-data-set-header
9415                        (gnus-data-find (cdr gnus-article-current))
9416                        header)
9417                       (gnus-summary-update-article-line
9418                        (cdr gnus-article-current) header)
9419                       (if (gnus-summary-goto-subject
9420                            (cdr gnus-article-current) nil t)
9421                           (gnus-summary-update-secondary-mark
9422                            (cdr gnus-article-current))))))))
9423           ;; Update threads.
9424           (set-buffer (or buffer gnus-summary-buffer))
9425           (gnus-summary-update-article (cdr gnus-article-current))
9426           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9427               (gnus-summary-update-secondary-mark
9428                (cdr gnus-article-current))))
9429         ;; Prettify the article buffer again.
9430         (unless no-highlight
9431           (save-excursion
9432             (set-buffer gnus-article-buffer)
9433             ;;;!!! Fix this -- article should be rehighlighted.
9434             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9435             (set-buffer gnus-original-article-buffer)
9436             (gnus-request-article
9437              (cdr gnus-article-current)
9438              (car gnus-article-current) (current-buffer))))
9439         ;; Prettify the summary buffer line.
9440         (when (gnus-visual-p 'summary-highlight 'highlight)
9441           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9442
9443 (defun gnus-summary-edit-wash (key)
9444   "Perform editing command KEY in the article buffer."
9445   (interactive
9446    (list
9447     (progn
9448       (message "%s" (concat (this-command-keys) "- "))
9449       (read-char))))
9450   (message "")
9451   (gnus-summary-edit-article)
9452   (execute-kbd-macro (concat (this-command-keys) key))
9453   (gnus-article-edit-done))
9454
9455 ;;; Respooling
9456
9457 (defun gnus-summary-respool-query (&optional silent trace)
9458   "Query where the respool algorithm would put this article."
9459   (interactive)
9460   (let (gnus-mark-article-hook)
9461     (gnus-summary-select-article)
9462     (save-excursion
9463       (set-buffer gnus-original-article-buffer)
9464       (save-restriction
9465         (message-narrow-to-head)
9466         (let ((groups (nnmail-article-group 'identity trace)))
9467           (unless silent
9468             (if groups
9469                 (message "This message would go to %s"
9470                          (mapconcat 'car groups ", "))
9471               (message "This message would go to no groups"))
9472             groups))))))
9473
9474 (defun gnus-summary-respool-trace ()
9475   "Trace where the respool algorithm would put this article.
9476 Display a buffer showing all fancy splitting patterns which matched."
9477   (interactive)
9478   (gnus-summary-respool-query nil t))
9479
9480 ;; Summary marking commands.
9481
9482 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9483   "Mark articles which has the same subject as read, and then select the next.
9484 If UNMARK is positive, remove any kind of mark.
9485 If UNMARK is negative, tick articles."
9486   (interactive "P")
9487   (when unmark
9488     (setq unmark (prefix-numeric-value unmark)))
9489   (let ((count
9490          (gnus-summary-mark-same-subject
9491           (gnus-summary-article-subject) unmark)))
9492     ;; Select next unread article.  If auto-select-same mode, should
9493     ;; select the first unread article.
9494     (gnus-summary-next-article t (and gnus-auto-select-same
9495                                       (gnus-summary-article-subject)))
9496     (gnus-message 7 "%d article%s marked as %s"
9497                   count (if (= count 1) " is" "s are")
9498                   (if unmark "unread" "read"))))
9499
9500 (defun gnus-summary-kill-same-subject (&optional unmark)
9501   "Mark articles which has the same subject as read.
9502 If UNMARK is positive, remove any kind of mark.
9503 If UNMARK is negative, tick articles."
9504   (interactive "P")
9505   (when unmark
9506     (setq unmark (prefix-numeric-value unmark)))
9507   (let ((count
9508          (gnus-summary-mark-same-subject
9509           (gnus-summary-article-subject) unmark)))
9510     ;; If marked as read, go to next unread subject.
9511     (when (null unmark)
9512       ;; Go to next unread subject.
9513       (gnus-summary-next-subject 1 t))
9514     (gnus-message 7 "%d articles are marked as %s"
9515                   count (if unmark "unread" "read"))))
9516
9517 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9518   "Mark articles with same SUBJECT as read, and return marked number.
9519 If optional argument UNMARK is positive, remove any kinds of marks.
9520 If optional argument UNMARK is negative, mark articles as unread instead."
9521   (let ((count 1))
9522     (save-excursion
9523       (cond
9524        ((null unmark)                   ; Mark as read.
9525         (while (and
9526                 (progn
9527                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9528                   (gnus-summary-show-thread) t)
9529                 (gnus-summary-find-subject subject))
9530           (setq count (1+ count))))
9531        ((> unmark 0)                    ; Tick.
9532         (while (and
9533                 (progn
9534                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9535                   (gnus-summary-show-thread) t)
9536                 (gnus-summary-find-subject subject))
9537           (setq count (1+ count))))
9538        (t                               ; Mark as unread.
9539         (while (and
9540                 (progn
9541                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9542                   (gnus-summary-show-thread) t)
9543                 (gnus-summary-find-subject subject))
9544           (setq count (1+ count)))))
9545       (gnus-set-mode-line 'summary)
9546       ;; Return the number of marked articles.
9547       count)))
9548
9549 (defun gnus-summary-mark-as-processable (n &optional unmark)
9550   "Set the process mark on the next N articles.
9551 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9552 the process mark instead.  The difference between N and the actual
9553 number of articles marked is returned."
9554   (interactive "P")
9555   (if (and (null n) (gnus-region-active-p))
9556       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9557     (setq n (prefix-numeric-value n))
9558     (let ((backward (< n 0))
9559           (n (abs n)))
9560       (while (and
9561               (> n 0)
9562               (if unmark
9563                   (gnus-summary-remove-process-mark
9564                    (gnus-summary-article-number))
9565                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9566               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9567         (setq n (1- n)))
9568       (when (/= 0 n)
9569         (gnus-message 7 "No more articles"))
9570       (gnus-summary-recenter)
9571       (gnus-summary-position-point)
9572       n)))
9573
9574 (defun gnus-summary-unmark-as-processable (n)
9575   "Remove the process mark from the next N articles.
9576 If N is negative, unmark backward instead.  The difference between N and
9577 the actual number of articles unmarked is returned."
9578   (interactive "P")
9579   (gnus-summary-mark-as-processable n t))
9580
9581 (defun gnus-summary-unmark-all-processable ()
9582   "Remove the process mark from all articles."
9583   (interactive)
9584   (save-excursion
9585     (while gnus-newsgroup-processable
9586       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9587   (gnus-summary-position-point))
9588
9589 (defun gnus-summary-add-mark (article type)
9590   "Mark ARTICLE with a mark of TYPE."
9591   (let ((vtype (car (assq type gnus-article-mark-lists)))
9592         var)
9593     (if (not vtype)
9594         (error "No such mark type: %s" type)
9595       (setq var (intern (format "gnus-newsgroup-%s" type)))
9596       (set var (cons article (symbol-value var)))
9597       (if (memq type '(processable cached replied forwarded recent saved))
9598           (gnus-summary-update-secondary-mark article)
9599         ;;; !!! This is bogus.  We should find out what primary
9600         ;;; !!! mark we want to set.
9601         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9602
9603 (defun gnus-summary-mark-as-expirable (n)
9604   "Mark N articles forward as expirable.
9605 If N is negative, mark backward instead.  The difference between N and
9606 the actual number of articles marked is returned."
9607   (interactive "p")
9608   (gnus-summary-mark-forward n gnus-expirable-mark))
9609
9610 (defun gnus-summary-mark-as-spam (n)
9611   "Mark N articles forward as spam.
9612 If N is negative, mark backward instead.  The difference between N and
9613 the actual number of articles marked is returned."
9614   (interactive "p")
9615   (gnus-summary-mark-forward n gnus-spam-mark))
9616
9617 (defun gnus-summary-mark-article-as-replied (article)
9618   "Mark ARTICLE as replied to and update the summary line.
9619 ARTICLE can also be a list of articles."
9620   (interactive (list (gnus-summary-article-number)))
9621   (let ((articles (if (listp article) article (list article))))
9622     (dolist (article articles)
9623       (unless (numberp article)
9624         (error "%s is not a number" article))
9625       (push article gnus-newsgroup-replied)
9626       (let ((buffer-read-only nil))
9627         (when (gnus-summary-goto-subject article nil t)
9628           (gnus-summary-update-secondary-mark article))))))
9629
9630 (defun gnus-summary-mark-article-as-forwarded (article)
9631   "Mark ARTICLE as forwarded and update the summary line.
9632 ARTICLE can also be a list of articles."
9633   (let ((articles (if (listp article) article (list article))))
9634     (dolist (article articles)
9635       (push article gnus-newsgroup-forwarded)
9636       (let ((buffer-read-only nil))
9637         (when (gnus-summary-goto-subject article nil t)
9638           (gnus-summary-update-secondary-mark article))))))
9639
9640 (defun gnus-summary-set-bookmark (article)
9641   "Set a bookmark in current article."
9642   (interactive (list (gnus-summary-article-number)))
9643   (when (or (not (get-buffer gnus-article-buffer))
9644             (not gnus-current-article)
9645             (not gnus-article-current)
9646             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9647     (error "No current article selected"))
9648   ;; Remove old bookmark, if one exists.
9649   (gnus-pull article gnus-newsgroup-bookmarks)
9650   ;; Set the new bookmark, which is on the form
9651   ;; (article-number . line-number-in-body).
9652   (push
9653    (cons article
9654          (save-excursion
9655            (set-buffer gnus-article-buffer)
9656            (count-lines
9657             (min (point)
9658                  (save-excursion
9659                    (article-goto-body)
9660                    (point)))
9661             (point))))
9662    gnus-newsgroup-bookmarks)
9663   (gnus-message 6 "A bookmark has been added to the current article."))
9664
9665 (defun gnus-summary-remove-bookmark (article)
9666   "Remove the bookmark from the current article."
9667   (interactive (list (gnus-summary-article-number)))
9668   ;; Remove old bookmark, if one exists.
9669   (if (not (assq article gnus-newsgroup-bookmarks))
9670       (gnus-message 6 "No bookmark in current article.")
9671     (gnus-pull article gnus-newsgroup-bookmarks)
9672     (gnus-message 6 "Removed bookmark.")))
9673
9674 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9675 (defun gnus-summary-mark-as-dormant (n)
9676   "Mark N articles forward as dormant.
9677 If N is negative, mark backward instead.  The difference between N and
9678 the actual number of articles marked is returned."
9679   (interactive "p")
9680   (gnus-summary-mark-forward n gnus-dormant-mark))
9681
9682 (defun gnus-summary-set-process-mark (article)
9683   "Set the process mark on ARTICLE and update the summary line."
9684   (setq gnus-newsgroup-processable
9685         (cons article
9686               (delq article gnus-newsgroup-processable)))
9687   (when (gnus-summary-goto-subject article)
9688     (gnus-summary-show-thread)
9689     (gnus-summary-goto-subject article)
9690     (gnus-summary-update-secondary-mark article)))
9691
9692 (defun gnus-summary-remove-process-mark (article)
9693   "Remove the process mark from ARTICLE and update the summary line."
9694   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9695   (when (gnus-summary-goto-subject article)
9696     (gnus-summary-show-thread)
9697     (gnus-summary-goto-subject article)
9698     (gnus-summary-update-secondary-mark article)))
9699
9700 (defun gnus-summary-set-saved-mark (article)
9701   "Set the process mark on ARTICLE and update the summary line."
9702   (push article gnus-newsgroup-saved)
9703   (when (gnus-summary-goto-subject article)
9704     (gnus-summary-update-secondary-mark article)))
9705
9706 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9707   "Mark N articles as read forwards.
9708 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9709 The difference between N and the actual number of articles marked is
9710 returned.
9711 If NO-EXPIRE, auto-expiry will be inhibited."
9712   (interactive "p")
9713   (gnus-summary-show-thread)
9714   (let ((backward (< n 0))
9715         (gnus-summary-goto-unread
9716          (and gnus-summary-goto-unread
9717               (not (eq gnus-summary-goto-unread 'never))
9718               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9719                                     gnus-ticked-mark gnus-dormant-mark)))))
9720         (n (abs n))
9721         (mark (or mark gnus-del-mark)))
9722     (while (and (> n 0)
9723                 (gnus-summary-mark-article nil mark no-expire)
9724                 (zerop (gnus-summary-next-subject
9725                         (if backward -1 1)
9726                         (and gnus-summary-goto-unread
9727                              (not (eq gnus-summary-goto-unread 'never)))
9728                         t)))
9729       (setq n (1- n)))
9730     (when (/= 0 n)
9731       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9732     (gnus-summary-recenter)
9733     (gnus-summary-position-point)
9734     (gnus-set-mode-line 'summary)
9735     n))
9736
9737 (defun gnus-summary-mark-article-as-read (mark)
9738   "Mark the current article quickly as read with MARK."
9739   (let ((article (gnus-summary-article-number)))
9740     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9741     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9742     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9743     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9744     (push (cons article mark) gnus-newsgroup-reads)
9745     ;; Possibly remove from cache, if that is used.
9746     (when gnus-use-cache
9747       (gnus-cache-enter-remove-article article))
9748     ;; Allow the backend to change the mark.
9749     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9750     ;; Check for auto-expiry.
9751     (when (and gnus-newsgroup-auto-expire
9752                (memq mark gnus-auto-expirable-marks))
9753       (setq mark gnus-expirable-mark)
9754       ;; Let the backend know about the mark change.
9755       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9756       (push article gnus-newsgroup-expirable))
9757     ;; Set the mark in the buffer.
9758     (gnus-summary-update-mark mark 'unread)
9759     t))
9760
9761 (defun gnus-summary-mark-article-as-unread (mark)
9762   "Mark the current article quickly as unread with MARK."
9763   (let* ((article (gnus-summary-article-number))
9764          (old-mark (gnus-summary-article-mark article)))
9765     ;; Allow the backend to change the mark.
9766     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9767     (if (eq mark old-mark)
9768         t
9769       (if (<= article 0)
9770           (progn
9771             (gnus-error 1 "Can't mark negative article numbers")
9772             nil)
9773         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9774         (setq gnus-newsgroup-spam-marked
9775               (delq article gnus-newsgroup-spam-marked))
9776         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9777         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9778         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9779         (cond ((= mark gnus-ticked-mark)
9780                (setq gnus-newsgroup-marked
9781                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9782                                               article)))
9783               ((= mark gnus-spam-mark)
9784                (setq gnus-newsgroup-spam-marked
9785                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9786                                               article)))
9787               ((= mark gnus-dormant-mark)
9788                (setq gnus-newsgroup-dormant
9789                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9790                                               article)))
9791               (t
9792                (setq gnus-newsgroup-unreads
9793                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9794                                               article))))
9795         (gnus-pull article gnus-newsgroup-reads)
9796
9797         ;; See whether the article is to be put in the cache.
9798         (and gnus-use-cache
9799              (vectorp (gnus-summary-article-header article))
9800              (save-excursion
9801                (gnus-cache-possibly-enter-article
9802                 gnus-newsgroup-name article
9803                 (= mark gnus-ticked-mark)
9804                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9805
9806         ;; Fix the mark.
9807         (gnus-summary-update-mark mark 'unread)
9808         t))))
9809
9810 (defun gnus-summary-mark-article (&optional article mark no-expire)
9811   "Mark ARTICLE with MARK.  MARK can be any character.
9812 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9813 `??' (dormant) and `?E' (expirable).
9814 If MARK is nil, then the default character `?r' is used.
9815 If ARTICLE is nil, then the article on the current line will be
9816 marked.
9817 If NO-EXPIRE, auto-expiry will be inhibited."
9818   ;; The mark might be a string.
9819   (when (stringp mark)
9820     (setq mark (aref mark 0)))
9821   ;; If no mark is given, then we check auto-expiring.
9822   (when (null mark)
9823     (setq mark gnus-del-mark))
9824   (when (and (not no-expire)
9825              gnus-newsgroup-auto-expire
9826              (memq mark gnus-auto-expirable-marks))
9827     (setq mark gnus-expirable-mark))
9828   (let ((article (or article (gnus-summary-article-number)))
9829         (old-mark (gnus-summary-article-mark article)))
9830     ;; Allow the backend to change the mark.
9831     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9832     (if (eq mark old-mark)
9833         t
9834       (unless article
9835         (error "No article on current line"))
9836       (if (not (if (or (= mark gnus-unread-mark)
9837                        (= mark gnus-ticked-mark)
9838                        (= mark gnus-spam-mark)
9839                        (= mark gnus-dormant-mark))
9840                    (gnus-mark-article-as-unread article mark)
9841                  (gnus-mark-article-as-read article mark)))
9842           t
9843         ;; See whether the article is to be put in the cache.
9844         (and gnus-use-cache
9845              (not (= mark gnus-canceled-mark))
9846              (vectorp (gnus-summary-article-header article))
9847              (save-excursion
9848                (gnus-cache-possibly-enter-article
9849                 gnus-newsgroup-name article
9850                 (= mark gnus-ticked-mark)
9851                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9852
9853         (when (gnus-summary-goto-subject article nil t)
9854           (let ((buffer-read-only nil))
9855             (gnus-summary-show-thread)
9856             ;; Fix the mark.
9857             (gnus-summary-update-mark mark 'unread)
9858             t))))))
9859
9860 (defun gnus-summary-update-secondary-mark (article)
9861   "Update the secondary (read, process, cache) mark."
9862   (gnus-summary-update-mark
9863    (cond ((memq article gnus-newsgroup-processable)
9864           gnus-process-mark)
9865          ((memq article gnus-newsgroup-cached)
9866           gnus-cached-mark)
9867          ((memq article gnus-newsgroup-replied)
9868           gnus-replied-mark)
9869          ((memq article gnus-newsgroup-forwarded)
9870           gnus-forwarded-mark)
9871          ((memq article gnus-newsgroup-saved)
9872           gnus-saved-mark)
9873          ((memq article gnus-newsgroup-recent)
9874           gnus-recent-mark)
9875          ((memq article gnus-newsgroup-unseen)
9876           gnus-unseen-mark)
9877          (t gnus-no-mark))
9878    'replied)
9879   (when (gnus-visual-p 'summary-highlight 'highlight)
9880     (gnus-run-hooks 'gnus-summary-update-hook))
9881   t)
9882
9883 (defun gnus-summary-update-download-mark (article)
9884   "Update the download mark."
9885   (gnus-summary-update-mark
9886    (cond ((memq article gnus-newsgroup-undownloaded) 
9887           gnus-undownloaded-mark)
9888          (gnus-newsgroup-agentized
9889           gnus-downloaded-mark)
9890          (t
9891           gnus-no-mark))
9892    'download)
9893   (gnus-summary-update-line t)
9894   t)
9895
9896 (defun gnus-summary-update-mark (mark type)
9897   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9898         (buffer-read-only nil))
9899     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9900     (when forward
9901       (when (looking-at "\r")
9902         (incf forward))
9903       (when (<= (+ forward (point)) (point-max))
9904         ;; Go to the right position on the line.
9905         (goto-char (+ forward (point)))
9906         ;; Replace the old mark with the new mark.
9907         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9908         ;; Optionally update the marks by some user rule.
9909         (when (eq type 'unread)
9910           (gnus-data-set-mark
9911            (gnus-data-find (gnus-summary-article-number)) mark)
9912           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9913
9914 (defun gnus-mark-article-as-read (article &optional mark)
9915   "Enter ARTICLE in the pertinent lists and remove it from others."
9916   ;; Make the article expirable.
9917   (let ((mark (or mark gnus-del-mark)))
9918     (setq gnus-newsgroup-expirable
9919           (if (= mark gnus-expirable-mark)
9920               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9921             (delq article gnus-newsgroup-expirable)))
9922     ;; Remove from unread and marked lists.
9923     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9924     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9925     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9926     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9927     (push (cons article mark) gnus-newsgroup-reads)
9928     ;; Possibly remove from cache, if that is used.
9929     (when gnus-use-cache
9930       (gnus-cache-enter-remove-article article))
9931     t))
9932
9933 (defun gnus-mark-article-as-unread (article &optional mark)
9934   "Enter ARTICLE in the pertinent lists and remove it from others."
9935   (let ((mark (or mark gnus-ticked-mark)))
9936     (if (<= article 0)
9937         (progn
9938           (gnus-error 1 "Can't mark negative article numbers")
9939           nil)
9940       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9941             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9942             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9943             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9944             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9945
9946       ;; Unsuppress duplicates?
9947       (when gnus-suppress-duplicates
9948         (gnus-dup-unsuppress-article article))
9949
9950       (cond ((= mark gnus-ticked-mark)
9951              (setq gnus-newsgroup-marked
9952                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9953             ((= mark gnus-spam-mark)
9954              (setq gnus-newsgroup-spam-marked
9955                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9956                                             article)))
9957             ((= mark gnus-dormant-mark)
9958              (setq gnus-newsgroup-dormant
9959                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9960             (t
9961              (setq gnus-newsgroup-unreads
9962                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9963       (gnus-pull article gnus-newsgroup-reads)
9964       t)))
9965
9966 (defalias 'gnus-summary-mark-as-unread-forward
9967   'gnus-summary-tick-article-forward)
9968 (make-obsolete 'gnus-summary-mark-as-unread-forward
9969                'gnus-summary-tick-article-forward)
9970 (defun gnus-summary-tick-article-forward (n)
9971   "Tick N articles forwards.
9972 If N is negative, tick backwards instead.
9973 The difference between N and the number of articles ticked is returned."
9974   (interactive "p")
9975   (gnus-summary-mark-forward n gnus-ticked-mark))
9976
9977 (defalias 'gnus-summary-mark-as-unread-backward
9978   'gnus-summary-tick-article-backward)
9979 (make-obsolete 'gnus-summary-mark-as-unread-backward
9980                'gnus-summary-tick-article-backward)
9981 (defun gnus-summary-tick-article-backward (n)
9982   "Tick N articles backwards.
9983 The difference between N and the number of articles ticked is returned."
9984   (interactive "p")
9985   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9986
9987 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9988 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9989 (defun gnus-summary-tick-article (&optional article clear-mark)
9990   "Mark current article as unread.
9991 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9992 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9993   (interactive)
9994   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9995                                        gnus-ticked-mark)))
9996
9997 (defun gnus-summary-mark-as-read-forward (n)
9998   "Mark N articles as read forwards.
9999 If N is negative, mark backwards instead.
10000 The difference between N and the actual number of articles marked is
10001 returned."
10002   (interactive "p")
10003   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10004
10005 (defun gnus-summary-mark-as-read-backward (n)
10006   "Mark the N articles as read backwards.
10007 The difference between N and the actual number of articles marked is
10008 returned."
10009   (interactive "p")
10010   (gnus-summary-mark-forward
10011    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10012
10013 (defun gnus-summary-mark-as-read (&optional article mark)
10014   "Mark current article as read.
10015 ARTICLE specifies the article to be marked as read.
10016 MARK specifies a string to be inserted at the beginning of the line."
10017   (gnus-summary-mark-article article mark))
10018
10019 (defun gnus-summary-clear-mark-forward (n)
10020   "Clear marks from N articles forward.
10021 If N is negative, clear backward instead.
10022 The difference between N and the number of marks cleared is returned."
10023   (interactive "p")
10024   (gnus-summary-mark-forward n gnus-unread-mark))
10025
10026 (defun gnus-summary-clear-mark-backward (n)
10027   "Clear marks from N articles backward.
10028 The difference between N and the number of marks cleared is returned."
10029   (interactive "p")
10030   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10031
10032 (defun gnus-summary-mark-unread-as-read ()
10033   "Intended to be used by `gnus-summary-mark-article-hook'."
10034   (when (memq gnus-current-article gnus-newsgroup-unreads)
10035     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10036
10037 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10038   "Intended to be used by `gnus-summary-mark-article-hook'."
10039   (let ((mark (gnus-summary-article-mark)))
10040     (when (or (gnus-unread-mark-p mark)
10041               (gnus-read-mark-p mark))
10042       (gnus-summary-mark-article gnus-current-article
10043                                  (or new-mark gnus-read-mark)))))
10044
10045 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10046   "Intended to be used by `gnus-summary-mark-article-hook'."
10047   (let ((mark (gnus-summary-article-mark)))
10048     (when (or (gnus-unread-mark-p mark)
10049               (gnus-read-mark-p mark))
10050       (gnus-summary-mark-article (gnus-summary-article-number)
10051                                  (or new-mark gnus-read-mark)))))
10052
10053 (defun gnus-summary-mark-unread-as-ticked ()
10054   "Intended to be used by `gnus-summary-mark-article-hook'."
10055   (when (memq gnus-current-article gnus-newsgroup-unreads)
10056     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10057
10058 (defun gnus-summary-mark-region-as-read (point mark all)
10059   "Mark all unread articles between point and mark as read.
10060 If given a prefix, mark all articles between point and mark as read,
10061 even ticked and dormant ones."
10062   (interactive "r\nP")
10063   (save-excursion
10064     (let (article)
10065       (goto-char point)
10066       (beginning-of-line)
10067       (while (and
10068               (< (point) mark)
10069               (progn
10070                 (when (or all
10071                           (memq (setq article (gnus-summary-article-number))
10072                                 gnus-newsgroup-unreads))
10073                   (gnus-summary-mark-article article gnus-del-mark))
10074                 t)
10075               (gnus-summary-find-next))))))
10076
10077 (defun gnus-summary-mark-below (score mark)
10078   "Mark articles with score less than SCORE with MARK."
10079   (interactive "P\ncMark: ")
10080   (setq score (if score
10081                   (prefix-numeric-value score)
10082                 (or gnus-summary-default-score 0)))
10083   (save-excursion
10084     (set-buffer gnus-summary-buffer)
10085     (goto-char (point-min))
10086     (while
10087         (progn
10088           (and (< (gnus-summary-article-score) score)
10089                (gnus-summary-mark-article nil mark))
10090           (gnus-summary-find-next)))))
10091
10092 (defun gnus-summary-kill-below (&optional score)
10093   "Mark articles with score below SCORE as read."
10094   (interactive "P")
10095   (gnus-summary-mark-below score gnus-killed-mark))
10096
10097 (defun gnus-summary-clear-above (&optional score)
10098   "Clear all marks from articles with score above SCORE."
10099   (interactive "P")
10100   (gnus-summary-mark-above score gnus-unread-mark))
10101
10102 (defun gnus-summary-tick-above (&optional score)
10103   "Tick all articles with score above SCORE."
10104   (interactive "P")
10105   (gnus-summary-mark-above score gnus-ticked-mark))
10106
10107 (defun gnus-summary-mark-above (score mark)
10108   "Mark articles with score over SCORE with MARK."
10109   (interactive "P\ncMark: ")
10110   (setq score (if score
10111                   (prefix-numeric-value score)
10112                 (or gnus-summary-default-score 0)))
10113   (save-excursion
10114     (set-buffer gnus-summary-buffer)
10115     (goto-char (point-min))
10116     (while (and (progn
10117                   (when (> (gnus-summary-article-score) score)
10118                     (gnus-summary-mark-article nil mark))
10119                   t)
10120                 (gnus-summary-find-next)))))
10121
10122 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10123 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10124 (defun gnus-summary-limit-include-expunged (&optional no-error)
10125   "Display all the hidden articles that were expunged for low scores."
10126   (interactive)
10127   (let ((buffer-read-only nil))
10128     (let ((scored gnus-newsgroup-scored)
10129           headers h)
10130       (while scored
10131         (unless (gnus-summary-article-header (caar scored))
10132           (and (setq h (gnus-number-to-header (caar scored)))
10133                (< (cdar scored) gnus-summary-expunge-below)
10134                (push h headers)))
10135         (setq scored (cdr scored)))
10136       (if (not headers)
10137           (when (not no-error)
10138             (error "No expunged articles hidden"))
10139         (goto-char (point-min))
10140         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10141         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10142         (mapcar (lambda (x) (push (mail-header-number x)
10143                                   gnus-newsgroup-limit))
10144                 headers)
10145         (gnus-summary-prepare-unthreaded (nreverse headers))
10146         (goto-char (point-min))
10147         (gnus-summary-position-point)
10148         t))))
10149
10150 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10151   "Mark all unread articles in this newsgroup as read.
10152 If prefix argument ALL is non-nil, ticked and dormant articles will
10153 also be marked as read.
10154 If QUIETLY is non-nil, no questions will be asked.
10155
10156 If TO-HERE is non-nil, it should be a point in the buffer.  All
10157 articles before (after, if REVERSE is set) this point will be marked
10158 as read.
10159
10160 Note that this function will only catch up the unread article
10161 in the current summary buffer limitation.
10162
10163 The number of articles marked as read is returned."
10164   (interactive "P")
10165   (prog1
10166       (save-excursion
10167         (when (or quietly
10168                   (not gnus-interactive-catchup) ;Without confirmation?
10169                   gnus-expert-user
10170                   (gnus-y-or-n-p
10171                    (if all
10172                        "Mark absolutely all articles as read? "
10173                      "Mark all unread articles as read? ")))
10174           (if (and not-mark
10175                    (not gnus-newsgroup-adaptive)
10176                    (not gnus-newsgroup-auto-expire)
10177                    (not gnus-suppress-duplicates)
10178                    (or (not gnus-use-cache)
10179                        (eq gnus-use-cache 'passive)))
10180               (progn
10181                 (when all
10182                   (setq gnus-newsgroup-marked nil
10183                         gnus-newsgroup-spam-marked nil
10184                         gnus-newsgroup-dormant nil))
10185                 (setq gnus-newsgroup-unreads
10186                       (gnus-sorted-nunion
10187                        (gnus-intersection gnus-newsgroup-unreads
10188                                           gnus-newsgroup-downloadable)
10189                        gnus-newsgroup-unfetched)))
10190             ;; We actually mark all articles as canceled, which we
10191             ;; have to do when using auto-expiry or adaptive scoring.
10192             (gnus-summary-show-all-threads)
10193             (if (and to-here reverse)
10194                 (progn
10195                   (goto-char to-here)
10196                   (gnus-summary-mark-current-read-and-unread-as-read
10197                    gnus-catchup-mark)
10198                   (while (gnus-summary-find-next (not all))
10199                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10200               (when (gnus-summary-first-subject (not all))
10201                 (while (and
10202                         (if to-here (< (point) to-here) t)
10203                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10204                         (gnus-summary-find-next (not all))))))
10205             (gnus-set-mode-line 'summary))
10206           t))
10207     (gnus-summary-position-point)))
10208
10209 (defun gnus-summary-catchup-to-here (&optional all)
10210   "Mark all unticked articles before the current one as read.
10211 If ALL is non-nil, also mark ticked and dormant articles as read."
10212   (interactive "P")
10213   (save-excursion
10214     (gnus-save-hidden-threads
10215       (let ((beg (point)))
10216         ;; We check that there are unread articles.
10217         (when (or all (gnus-summary-find-prev))
10218           (gnus-summary-catchup all t beg)))))
10219   (gnus-summary-position-point))
10220
10221 (defun gnus-summary-catchup-from-here (&optional all)
10222   "Mark all unticked articles after (and including) the current one as read.
10223 If ALL is non-nil, also mark ticked and dormant articles as read."
10224   (interactive "P")
10225   (save-excursion
10226     (gnus-save-hidden-threads
10227       (let ((beg (point)))
10228         ;; We check that there are unread articles.
10229         (when (or all (gnus-summary-find-next))
10230           (gnus-summary-catchup all t beg nil t)))))
10231   (gnus-summary-position-point))
10232
10233 (defun gnus-summary-catchup-all (&optional quietly)
10234   "Mark all articles in this newsgroup as read.
10235 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10236 instead, which marks only unread articles as read."
10237   (interactive "P")
10238   (gnus-summary-catchup t quietly))
10239
10240 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10241   "Mark all unread articles in this group as read, then exit.
10242 If prefix argument ALL is non-nil, all articles are marked as read.
10243 If QUIETLY is non-nil, no questions will be asked."
10244   (interactive "P")
10245   (when (gnus-summary-catchup all quietly nil 'fast)
10246     ;; Select next newsgroup or exit.
10247     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10248              (eq gnus-auto-select-next 'quietly))
10249         (gnus-summary-next-group nil)
10250       (gnus-summary-exit))))
10251
10252 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10253   "Mark all articles in this newsgroup as read, and then exit.
10254 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10255 instead, which marks only unread articles as read."
10256   (interactive "P")
10257   (gnus-summary-catchup-and-exit t quietly))
10258
10259 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10260   "Mark all articles in this group as read and select the next group.
10261 If given a prefix, mark all articles, unread as well as ticked, as
10262 read."
10263   (interactive "P")
10264   (save-excursion
10265     (gnus-summary-catchup all))
10266   (gnus-summary-next-group))
10267
10268 ;;;
10269 ;;; with article
10270 ;;;
10271
10272 (defmacro gnus-with-article (article &rest forms)
10273   "Select ARTICLE and perform FORMS in the original article buffer.
10274 Then replace the article with the result."
10275   `(progn
10276      ;; We don't want the article to be marked as read.
10277      (let (gnus-mark-article-hook)
10278        (gnus-summary-select-article t t nil ,article))
10279      (set-buffer gnus-original-article-buffer)
10280      ,@forms
10281      (if (not (gnus-check-backend-function
10282                'request-replace-article (car gnus-article-current)))
10283          (gnus-message 5 "Read-only group; not replacing")
10284        (unless (gnus-request-replace-article
10285                 ,article (car gnus-article-current)
10286                 (current-buffer) t)
10287          (error "Couldn't replace article")))
10288      ;; The cache and backlog have to be flushed somewhat.
10289      (when gnus-keep-backlog
10290        (gnus-backlog-remove-article
10291         (car gnus-article-current) (cdr gnus-article-current)))
10292      (when gnus-use-cache
10293        (gnus-cache-update-article
10294         (car gnus-article-current) (cdr gnus-article-current)))))
10295
10296 (put 'gnus-with-article 'lisp-indent-function 1)
10297 (put 'gnus-with-article 'edebug-form-spec '(form body))
10298
10299 ;; Thread-based commands.
10300
10301 (defun gnus-summary-articles-in-thread (&optional article)
10302   "Return a list of all articles in the current thread.
10303 If ARTICLE is non-nil, return all articles in the thread that starts
10304 with that article."
10305   (let* ((article (or article (gnus-summary-article-number)))
10306          (data (gnus-data-find-list article))
10307          (top-level (gnus-data-level (car data)))
10308          (top-subject
10309           (cond ((null gnus-thread-operation-ignore-subject)
10310                  (gnus-simplify-subject-re
10311                   (mail-header-subject (gnus-data-header (car data)))))
10312                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10313                  (gnus-simplify-subject-fuzzy
10314                   (mail-header-subject (gnus-data-header (car data)))))
10315                 (t nil)))
10316          (end-point (save-excursion
10317                       (if (gnus-summary-go-to-next-thread)
10318                           (point) (point-max))))
10319          articles)
10320     (while (and data
10321                 (< (gnus-data-pos (car data)) end-point))
10322       (when (or (not top-subject)
10323                 (string= top-subject
10324                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10325                              (gnus-simplify-subject-fuzzy
10326                               (mail-header-subject
10327                                (gnus-data-header (car data))))
10328                            (gnus-simplify-subject-re
10329                             (mail-header-subject
10330                              (gnus-data-header (car data)))))))
10331         (push (gnus-data-number (car data)) articles))
10332       (unless (and (setq data (cdr data))
10333                    (> (gnus-data-level (car data)) top-level))
10334         (setq data nil)))
10335     ;; Return the list of articles.
10336     (nreverse articles)))
10337
10338 (defun gnus-summary-rethread-current ()
10339   "Rethread the thread the current article is part of."
10340   (interactive)
10341   (let* ((gnus-show-threads t)
10342          (article (gnus-summary-article-number))
10343          (id (mail-header-id (gnus-summary-article-header)))
10344          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10345     (unless id
10346       (error "No article on the current line"))
10347     (gnus-rebuild-thread id)
10348     (gnus-summary-goto-subject article)))
10349
10350 (defun gnus-summary-reparent-thread ()
10351   "Make the current article child of the marked (or previous) article.
10352
10353 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10354 is non-nil or the Subject: of both articles are the same."
10355   (interactive)
10356   (unless (not (gnus-group-read-only-p))
10357     (error "The current newsgroup does not support article editing"))
10358   (unless (<= (length gnus-newsgroup-processable) 1)
10359     (error "No more than one article may be marked"))
10360   (save-window-excursion
10361     (let ((gnus-article-buffer " *reparent*")
10362           (current-article (gnus-summary-article-number))
10363           ;; First grab the marked article, otherwise one line up.
10364           (parent-article (if (not (null gnus-newsgroup-processable))
10365                               (car gnus-newsgroup-processable)
10366                             (save-excursion
10367                               (if (eq (forward-line -1) 0)
10368                                   (gnus-summary-article-number)
10369                                 (error "Beginning of summary buffer"))))))
10370       (unless (not (eq current-article parent-article))
10371         (error "An article may not be self-referential"))
10372       (let ((message-id (mail-header-id
10373                          (gnus-summary-article-header parent-article))))
10374         (unless (and message-id (not (equal message-id "")))
10375           (error "No message-id in desired parent"))
10376         (gnus-with-article current-article
10377           (save-restriction
10378             (goto-char (point-min))
10379             (message-narrow-to-head)
10380             (if (re-search-forward "^References: " nil t)
10381                 (progn
10382                   (re-search-forward "^[^ \t]" nil t)
10383                   (forward-line -1)
10384                   (end-of-line)
10385                   (insert " " message-id))
10386               (insert "References: " message-id "\n"))))
10387         (set-buffer gnus-summary-buffer)
10388         (gnus-summary-unmark-all-processable)
10389         (gnus-summary-update-article current-article)
10390         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10391             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10392         (gnus-summary-rethread-current)
10393         (gnus-message 3 "Article %d is now the child of article %d"
10394                       current-article parent-article)))))
10395
10396 (defun gnus-summary-toggle-threads (&optional arg)
10397   "Toggle showing conversation threads.
10398 If ARG is positive number, turn showing conversation threads on."
10399   (interactive "P")
10400   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10401     (setq gnus-show-threads
10402           (if (null arg) (not gnus-show-threads)
10403             (> (prefix-numeric-value arg) 0)))
10404     (gnus-summary-prepare)
10405     (gnus-summary-goto-subject current)
10406     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10407     (gnus-summary-position-point)))
10408
10409 (defun gnus-summary-show-all-threads ()
10410   "Show all threads."
10411   (interactive)
10412   (save-excursion
10413     (let ((buffer-read-only nil))
10414       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10415   (gnus-summary-position-point))
10416
10417 (defun gnus-summary-show-thread ()
10418   "Show thread subtrees.
10419 Returns nil if no thread was there to be shown."
10420   (interactive)
10421   (let ((buffer-read-only nil)
10422         (orig (point))
10423         (end (gnus-point-at-eol))
10424         ;; Leave point at bol
10425         (beg (progn (beginning-of-line) (point))))
10426     (prog1
10427         ;; Any hidden lines here?
10428         (search-forward "\r" end t)
10429       (subst-char-in-region beg end ?\^M ?\n t)
10430       (goto-char orig)
10431       (gnus-summary-position-point))))
10432
10433 (defun gnus-summary-maybe-hide-threads ()
10434   "If requested, hide the threads that should be hidden."
10435   (when (and gnus-show-threads
10436              gnus-thread-hide-subtree)
10437     (gnus-summary-hide-all-threads
10438      (if (or (consp gnus-thread-hide-subtree)
10439              (functionp gnus-thread-hide-subtree))
10440          (gnus-make-predicate gnus-thread-hide-subtree)
10441        nil))))
10442
10443 ;;; Hiding predicates.
10444
10445 (defun gnus-article-unread-p (header)
10446   (memq (mail-header-number header) gnus-newsgroup-unreads))
10447
10448 (defun gnus-article-unseen-p (header)
10449   (memq (mail-header-number header) gnus-newsgroup-unseen))
10450
10451 (defun gnus-map-articles (predicate articles)
10452   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10453   (apply 'gnus-or (mapcar predicate
10454                           (mapcar 'gnus-summary-article-header articles))))
10455
10456 (defun gnus-summary-hide-all-threads (&optional predicate)
10457   "Hide all thread subtrees.
10458 If PREDICATE is supplied, threads that satisfy this predicate
10459 will not be hidden."
10460   (interactive)
10461   (save-excursion
10462     (goto-char (point-min))
10463     (let ((end nil))
10464       (while (not end)
10465         (when (or (not predicate)
10466                   (gnus-map-articles
10467                    predicate (gnus-summary-article-children)))
10468             (gnus-summary-hide-thread))
10469         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10470   (gnus-summary-position-point))
10471
10472 (defun gnus-summary-hide-thread ()
10473   "Hide thread subtrees.
10474 If PREDICATE is supplied, threads that satisfy this predicate
10475 will not be hidden.
10476 Returns nil if no threads were there to be hidden."
10477   (interactive)
10478   (let ((buffer-read-only nil)
10479         (start (point))
10480         (article (gnus-summary-article-number)))
10481     (goto-char start)
10482     ;; Go forward until either the buffer ends or the subthread
10483     ;; ends.
10484     (when (and (not (eobp))
10485                (or (zerop (gnus-summary-next-thread 1 t))
10486                    (goto-char (point-max))))
10487       (prog1
10488           (if (and (> (point) start)
10489                    (search-backward "\n" start t))
10490               (progn
10491                 (subst-char-in-region start (point) ?\n ?\^M)
10492                 (gnus-summary-goto-subject article))
10493             (goto-char start)
10494             nil)))))
10495
10496 (defun gnus-summary-go-to-next-thread (&optional previous)
10497   "Go to the same level (or less) next thread.
10498 If PREVIOUS is non-nil, go to previous thread instead.
10499 Return the article number moved to, or nil if moving was impossible."
10500   (let ((level (gnus-summary-thread-level))
10501         (way (if previous -1 1))
10502         (beg (point)))
10503     (forward-line way)
10504     (while (and (not (eobp))
10505                 (< level (gnus-summary-thread-level)))
10506       (forward-line way))
10507     (if (eobp)
10508         (progn
10509           (goto-char beg)
10510           nil)
10511       (setq beg (point))
10512       (prog1
10513           (gnus-summary-article-number)
10514         (goto-char beg)))))
10515
10516 (defun gnus-summary-next-thread (n &optional silent)
10517   "Go to the same level next N'th thread.
10518 If N is negative, search backward instead.
10519 Returns the difference between N and the number of skips actually
10520 done.
10521
10522 If SILENT, don't output messages."
10523   (interactive "p")
10524   (let ((backward (< n 0))
10525         (n (abs n)))
10526     (while (and (> n 0)
10527                 (gnus-summary-go-to-next-thread backward))
10528       (decf n))
10529     (unless silent
10530       (gnus-summary-position-point))
10531     (when (and (not silent) (/= 0 n))
10532       (gnus-message 7 "No more threads"))
10533     n))
10534
10535 (defun gnus-summary-prev-thread (n)
10536   "Go to the same level previous N'th thread.
10537 Returns the difference between N and the number of skips actually
10538 done."
10539   (interactive "p")
10540   (gnus-summary-next-thread (- n)))
10541
10542 (defun gnus-summary-go-down-thread ()
10543   "Go down one level in the current thread."
10544   (let ((children (gnus-summary-article-children)))
10545     (when children
10546       (gnus-summary-goto-subject (car children)))))
10547
10548 (defun gnus-summary-go-up-thread ()
10549   "Go up one level in the current thread."
10550   (let ((parent (gnus-summary-article-parent)))
10551     (when parent
10552       (gnus-summary-goto-subject parent))))
10553
10554 (defun gnus-summary-down-thread (n)
10555   "Go down thread N steps.
10556 If N is negative, go up instead.
10557 Returns the difference between N and how many steps down that were
10558 taken."
10559   (interactive "p")
10560   (let ((up (< n 0))
10561         (n (abs n)))
10562     (while (and (> n 0)
10563                 (if up (gnus-summary-go-up-thread)
10564                   (gnus-summary-go-down-thread)))
10565       (setq n (1- n)))
10566     (gnus-summary-position-point)
10567     (when (/= 0 n)
10568       (gnus-message 7 "Can't go further"))
10569     n))
10570
10571 (defun gnus-summary-up-thread (n)
10572   "Go up thread N steps.
10573 If N is negative, go down instead.
10574 Returns the difference between N and how many steps down that were
10575 taken."
10576   (interactive "p")
10577   (gnus-summary-down-thread (- n)))
10578
10579 (defun gnus-summary-top-thread ()
10580   "Go to the top of the thread."
10581   (interactive)
10582   (while (gnus-summary-go-up-thread))
10583   (gnus-summary-article-number))
10584
10585 (defun gnus-summary-kill-thread (&optional unmark)
10586   "Mark articles under current thread as read.
10587 If the prefix argument is positive, remove any kinds of marks.
10588 If the prefix argument is negative, tick articles instead."
10589   (interactive "P")
10590   (when unmark
10591     (setq unmark (prefix-numeric-value unmark)))
10592   (let ((articles (gnus-summary-articles-in-thread)))
10593     (save-excursion
10594       ;; Expand the thread.
10595       (gnus-summary-show-thread)
10596       ;; Mark all the articles.
10597       (while articles
10598         (gnus-summary-goto-subject (car articles))
10599         (cond ((null unmark)
10600                (gnus-summary-mark-article-as-read gnus-killed-mark))
10601               ((> unmark 0)
10602                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10603               (t
10604                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10605         (setq articles (cdr articles))))
10606     ;; Hide killed subtrees.
10607     (and (null unmark)
10608          gnus-thread-hide-killed
10609          (gnus-summary-hide-thread))
10610     ;; If marked as read, go to next unread subject.
10611     (when (null unmark)
10612       ;; Go to next unread subject.
10613       (gnus-summary-next-subject 1 t)))
10614   (gnus-set-mode-line 'summary))
10615
10616 ;; Summary sorting commands
10617
10618 (defun gnus-summary-sort-by-number (&optional reverse)
10619   "Sort the summary buffer by article number.
10620 Argument REVERSE means reverse order."
10621   (interactive "P")
10622   (gnus-summary-sort 'number reverse))
10623
10624 (defun gnus-summary-sort-by-random (&optional reverse)
10625   "Randomize the order in the summary buffer.
10626 Argument REVERSE means to randomize in reverse order."
10627   (interactive "P")
10628   (gnus-summary-sort 'random reverse))
10629
10630 (defun gnus-summary-sort-by-author (&optional reverse)
10631   "Sort the summary buffer by author name alphabetically.
10632 If `case-fold-search' is non-nil, case of letters is ignored.
10633 Argument REVERSE means reverse order."
10634   (interactive "P")
10635   (gnus-summary-sort 'author reverse))
10636
10637 (defun gnus-summary-sort-by-subject (&optional reverse)
10638   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10639 If `case-fold-search' is non-nil, case of letters is ignored.
10640 Argument REVERSE means reverse order."
10641   (interactive "P")
10642   (gnus-summary-sort 'subject reverse))
10643
10644 (defun gnus-summary-sort-by-date (&optional reverse)
10645   "Sort the summary buffer by date.
10646 Argument REVERSE means reverse order."
10647   (interactive "P")
10648   (gnus-summary-sort 'date reverse))
10649
10650 (defun gnus-summary-sort-by-score (&optional reverse)
10651   "Sort the summary buffer by score.
10652 Argument REVERSE means reverse order."
10653   (interactive "P")
10654   (gnus-summary-sort 'score reverse))
10655
10656 (defun gnus-summary-sort-by-lines (&optional reverse)
10657   "Sort the summary buffer by the number of lines.
10658 Argument REVERSE means reverse order."
10659   (interactive "P")
10660   (gnus-summary-sort 'lines reverse))
10661
10662 (defun gnus-summary-sort-by-chars (&optional reverse)
10663   "Sort the summary buffer by article length.
10664 Argument REVERSE means reverse order."
10665   (interactive "P")
10666   (gnus-summary-sort 'chars reverse))
10667
10668 (defun gnus-summary-sort-by-original (&optional reverse)
10669   "Sort the summary buffer using the default sorting method.
10670 Argument REVERSE means reverse order."
10671   (interactive "P")
10672   (let* ((buffer-read-only)
10673          (gnus-summary-prepare-hook nil))
10674     ;; We do the sorting by regenerating the threads.
10675     (gnus-summary-prepare)
10676     ;; Hide subthreads if needed.
10677     (gnus-summary-maybe-hide-threads)))
10678
10679 (defun gnus-summary-sort (predicate reverse)
10680   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10681   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10682          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10683          (gnus-thread-sort-functions
10684           (if (not reverse)
10685               thread
10686             `(lambda (t1 t2)
10687                (,thread t2 t1))))
10688          (gnus-sort-gathered-threads-function
10689           gnus-thread-sort-functions)
10690          (gnus-article-sort-functions
10691           (if (not reverse)
10692               article
10693             `(lambda (t1 t2)
10694                (,article t2 t1))))
10695          (buffer-read-only)
10696          (gnus-summary-prepare-hook nil))
10697     ;; We do the sorting by regenerating the threads.
10698     (gnus-summary-prepare)
10699     ;; Hide subthreads if needed.
10700     (gnus-summary-maybe-hide-threads)))
10701
10702 ;; Summary saving commands.
10703
10704 (defun gnus-summary-save-article (&optional n not-saved)
10705   "Save the current article using the default saver function.
10706 If N is a positive number, save the N next articles.
10707 If N is a negative number, save the N previous articles.
10708 If N is nil and any articles have been marked with the process mark,
10709 save those articles instead.
10710 The variable `gnus-default-article-saver' specifies the saver function."
10711   (interactive "P")
10712   (let* ((articles (gnus-summary-work-articles n))
10713          (save-buffer (save-excursion
10714                         (nnheader-set-temp-buffer " *Gnus Save*")))
10715          (num (length articles))
10716          header file)
10717     (dolist (article articles)
10718       (setq header (gnus-summary-article-header article))
10719       (if (not (vectorp header))
10720           ;; This is a pseudo-article.
10721           (if (assq 'name header)
10722               (gnus-copy-file (cdr (assq 'name header)))
10723             (gnus-message 1 "Article %d is unsaveable" article))
10724         ;; This is a real article.
10725         (save-window-excursion
10726           (let ((gnus-display-mime-function nil)
10727                 (gnus-article-prepare-hook nil))
10728             (gnus-summary-select-article t nil nil article)))
10729         (save-excursion
10730           (set-buffer save-buffer)
10731           (erase-buffer)
10732           (insert-buffer-substring gnus-original-article-buffer))
10733         (setq file (gnus-article-save save-buffer file num))
10734         (gnus-summary-remove-process-mark article)
10735         (unless not-saved
10736           (gnus-summary-set-saved-mark article))))
10737     (gnus-kill-buffer save-buffer)
10738     (gnus-summary-position-point)
10739     (gnus-set-mode-line 'summary)
10740     n))
10741
10742 (defun gnus-summary-pipe-output (&optional arg headers)
10743   "Pipe the current article to a subprocess.
10744 If N is a positive number, pipe the N next articles.
10745 If N is a negative number, pipe the N previous articles.
10746 If N is nil and any articles have been marked with the process mark,
10747 pipe those articles instead.
10748 If HEADERS (the symbolic prefix), include the headers, too."
10749   (interactive (gnus-interactive "P\ny"))
10750   (require 'gnus-art)
10751   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10752         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10753     (gnus-summary-save-article arg t))
10754   (let ((buffer (get-buffer "*Shell Command Output*")))
10755     (when (and buffer
10756                (not (zerop (buffer-size buffer))))
10757       (gnus-configure-windows 'pipe))))
10758
10759 (defun gnus-summary-save-article-mail (&optional arg)
10760   "Append the current article to an mail file.
10761 If N is a positive number, save the N next articles.
10762 If N is a negative number, save the N previous articles.
10763 If N is nil and any articles have been marked with the process mark,
10764 save those articles instead."
10765   (interactive "P")
10766   (require 'gnus-art)
10767   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10768     (gnus-summary-save-article arg)))
10769
10770 (defun gnus-summary-save-article-rmail (&optional arg)
10771   "Append the current article to an rmail file.
10772 If N is a positive number, save the N next articles.
10773 If N is a negative number, save the N previous articles.
10774 If N is nil and any articles have been marked with the process mark,
10775 save those articles instead."
10776   (interactive "P")
10777   (require 'gnus-art)
10778   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10779     (gnus-summary-save-article arg)))
10780
10781 (defun gnus-summary-save-article-file (&optional arg)
10782   "Append the current article to a file.
10783 If N is a positive number, save the N next articles.
10784 If N is a negative number, save the N previous articles.
10785 If N is nil and any articles have been marked with the process mark,
10786 save those articles instead."
10787   (interactive "P")
10788   (require 'gnus-art)
10789   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10790     (gnus-summary-save-article arg)))
10791
10792 (defun gnus-summary-write-article-file (&optional arg)
10793   "Write the current article to a file, deleting the previous file.
10794 If N is a positive number, save the N next articles.
10795 If N is a negative number, save the N previous articles.
10796 If N is nil and any articles have been marked with the process mark,
10797 save those articles instead."
10798   (interactive "P")
10799   (require 'gnus-art)
10800   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10801     (gnus-summary-save-article arg)))
10802
10803 (defun gnus-summary-save-article-body-file (&optional arg)
10804   "Append the current article body to a file.
10805 If N is a positive number, save the N next articles.
10806 If N is a negative number, save the N previous articles.
10807 If N is nil and any articles have been marked with the process mark,
10808 save those articles instead."
10809   (interactive "P")
10810   (require 'gnus-art)
10811   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10812     (gnus-summary-save-article arg)))
10813
10814 (defun gnus-summary-muttprint (&optional arg)
10815   "Print the current article using Muttprint.
10816 If N is a positive number, save the N next articles.
10817 If N is a negative number, save the N previous articles.
10818 If N is nil and any articles have been marked with the process mark,
10819 save those articles instead."
10820   (interactive "P")
10821   (require 'gnus-art)
10822   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10823     (gnus-summary-save-article arg t)))
10824
10825 (defun gnus-summary-pipe-message (program)
10826   "Pipe the current article through PROGRAM."
10827   (interactive "sProgram: ")
10828   (gnus-summary-select-article)
10829   (let ((mail-header-separator ""))
10830     (gnus-eval-in-buffer-window gnus-article-buffer
10831       (save-restriction
10832         (widen)
10833         (let ((start (window-start))
10834               buffer-read-only)
10835           (message-pipe-buffer-body program)
10836           (set-window-start (get-buffer-window (current-buffer)) start))))))
10837
10838 (defun gnus-get-split-value (methods)
10839   "Return a value based on the split METHODS."
10840   (let (split-name method result match)
10841     (when methods
10842       (save-excursion
10843         (set-buffer gnus-original-article-buffer)
10844         (save-restriction
10845           (nnheader-narrow-to-headers)
10846           (while (and methods (not split-name))
10847             (goto-char (point-min))
10848             (setq method (pop methods))
10849             (setq match (car method))
10850             (when (cond
10851                    ((stringp match)
10852                     ;; Regular expression.
10853                     (ignore-errors
10854                       (re-search-forward match nil t)))
10855                    ((functionp match)
10856                     ;; Function.
10857                     (save-restriction
10858                       (widen)
10859                       (setq result (funcall match gnus-newsgroup-name))))
10860                    ((consp match)
10861                     ;; Form.
10862                     (save-restriction
10863                       (widen)
10864                       (setq result (eval match)))))
10865               (setq split-name (cdr method))
10866               (cond ((stringp result)
10867                      (push (expand-file-name
10868                             result gnus-article-save-directory)
10869                            split-name))
10870                     ((consp result)
10871                      (setq split-name (append result split-name)))))))))
10872     (nreverse split-name)))
10873
10874 (defun gnus-valid-move-group-p (group)
10875   (and (boundp group)
10876        (symbol-name group)
10877        (symbol-value group)
10878        (gnus-get-function (gnus-find-method-for-group
10879                            (symbol-name group)) 'request-accept-article t)))
10880
10881 (defun gnus-read-move-group-name (prompt default articles prefix)
10882   "Read a group name."
10883   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10884          (minibuffer-confirm-incomplete nil) ; XEmacs
10885          (prom
10886           (format "%s %s to:"
10887                   prompt
10888                   (if (> (length articles) 1)
10889                       (format "these %d articles" (length articles))
10890                     "this article")))
10891          (to-newsgroup
10892           (cond
10893            ((null split-name)
10894             (gnus-completing-read-with-default
10895              default prom
10896              gnus-active-hashtb
10897              'gnus-valid-move-group-p
10898              nil prefix
10899              'gnus-group-history))
10900            ((= 1 (length split-name))
10901             (gnus-completing-read-with-default
10902              (car split-name) prom
10903              gnus-active-hashtb
10904              'gnus-valid-move-group-p
10905              nil nil
10906              'gnus-group-history))
10907            (t
10908             (gnus-completing-read-with-default
10909              nil prom
10910              (mapcar (lambda (el) (list el))
10911                      (nreverse split-name))
10912              nil nil nil
10913              'gnus-group-history))))
10914          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10915     (when to-newsgroup
10916       (if (or (string= to-newsgroup "")
10917               (string= to-newsgroup prefix))
10918           (setq to-newsgroup default))
10919       (unless to-newsgroup
10920         (error "No group name entered"))
10921       (or (gnus-active to-newsgroup)
10922           (gnus-activate-group to-newsgroup nil nil to-method)
10923           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10924                                      to-newsgroup))
10925               (or (and (gnus-request-create-group to-newsgroup to-method)
10926                        (gnus-activate-group
10927                         to-newsgroup nil nil to-method)
10928                        (gnus-subscribe-group to-newsgroup))
10929                   (error "Couldn't create group %s" to-newsgroup)))
10930           (error "No such group: %s" to-newsgroup)))
10931     to-newsgroup))
10932
10933 (defun gnus-summary-save-parts (type dir n &optional reverse)
10934   "Save parts matching TYPE to DIR.
10935 If REVERSE, save parts that do not match TYPE."
10936   (interactive
10937    (list (read-string "Save parts of type: "
10938                       (or (car gnus-summary-save-parts-type-history)
10939                           gnus-summary-save-parts-default-mime)
10940                       'gnus-summary-save-parts-type-history)
10941          (setq gnus-summary-save-parts-last-directory
10942                (read-file-name "Save to directory: "
10943                                gnus-summary-save-parts-last-directory
10944                                nil t))
10945          current-prefix-arg))
10946   (gnus-summary-iterate n
10947     (let ((gnus-display-mime-function nil)
10948           (gnus-inhibit-treatment t))
10949       (gnus-summary-select-article))
10950     (save-excursion
10951       (set-buffer gnus-article-buffer)
10952       (let ((handles (or gnus-article-mime-handles
10953                          (mm-dissect-buffer nil gnus-article-loose-mime)
10954                          (and gnus-article-emulate-mime
10955                               (mm-uu-dissect)))))
10956         (when handles
10957           (gnus-summary-save-parts-1 type dir handles reverse)
10958           (unless gnus-article-mime-handles ;; Don't destroy this case.
10959             (mm-destroy-parts handles)))))))
10960
10961 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10962   (if (stringp (car handle))
10963       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10964               (cdr handle))
10965     (when (if reverse
10966               (not (string-match type (mm-handle-media-type handle)))
10967             (string-match type (mm-handle-media-type handle)))
10968       (let ((file (expand-file-name
10969                    (file-name-nondirectory
10970                     (or
10971                      (mail-content-type-get
10972                       (mm-handle-disposition handle) 'filename)
10973                      (concat gnus-newsgroup-name
10974                              "." (number-to-string
10975                                   (cdr gnus-article-current)))))
10976                    dir)))
10977         (unless (file-exists-p file)
10978           (mm-save-part-to-file handle file))))))
10979
10980 ;; Summary extract commands
10981
10982 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10983   (let ((buffer-read-only nil)
10984         (article (gnus-summary-article-number))
10985         after-article b e)
10986     (unless (gnus-summary-goto-subject article)
10987       (error "No such article: %d" article))
10988     (gnus-summary-position-point)
10989     ;; If all commands are to be bunched up on one line, we collect
10990     ;; them here.
10991     (unless gnus-view-pseudos-separately
10992       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10993             files action)
10994         (while ps
10995           (setq action (cdr (assq 'action (car ps))))
10996           (setq files (list (cdr (assq 'name (car ps)))))
10997           (while (and ps (cdr ps)
10998                       (string= (or action "1")
10999                                (or (cdr (assq 'action (cadr ps))) "2")))
11000             (push (cdr (assq 'name (cadr ps))) files)
11001             (setcdr ps (cddr ps)))
11002           (when files
11003             (when (not (string-match "%s" action))
11004               (push " " files))
11005             (push " " files)
11006             (when (assq 'execute (car ps))
11007               (setcdr (assq 'execute (car ps))
11008                       (funcall (if (string-match "%s" action)
11009                                    'format 'concat)
11010                                action
11011                                (mapconcat
11012                                 (lambda (f)
11013                                   (if (equal f " ")
11014                                       f
11015                                     (mm-quote-arg f)))
11016                                 files " ")))))
11017           (setq ps (cdr ps)))))
11018     (if (and gnus-view-pseudos (not not-view))
11019         (while pslist
11020           (when (assq 'execute (car pslist))
11021             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11022                                   (eq gnus-view-pseudos 'not-confirm)))
11023           (setq pslist (cdr pslist)))
11024       (save-excursion
11025         (while pslist
11026           (setq after-article (or (cdr (assq 'article (car pslist)))
11027                                   (gnus-summary-article-number)))
11028           (gnus-summary-goto-subject after-article)
11029           (forward-line 1)
11030           (setq b (point))
11031           (insert "    " (file-name-nondirectory
11032                           (cdr (assq 'name (car pslist))))
11033                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11034           (setq e (point))
11035           (forward-line -1)             ; back to `b'
11036           (gnus-add-text-properties
11037            b (1- e) (list 'gnus-number gnus-reffed-article-number
11038                           gnus-mouse-face-prop gnus-mouse-face))
11039           (gnus-data-enter
11040            after-article gnus-reffed-article-number
11041            gnus-unread-mark b (car pslist) 0 (- e b))
11042           (setq gnus-newsgroup-unreads
11043                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11044                                          gnus-reffed-article-number))
11045           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11046           (setq pslist (cdr pslist)))))))
11047
11048 (defun gnus-pseudos< (p1 p2)
11049   (let ((c1 (cdr (assq 'action p1)))
11050         (c2 (cdr (assq 'action p2))))
11051     (and c1 c2 (string< c1 c2))))
11052
11053 (defun gnus-request-pseudo-article (props)
11054   (cond ((assq 'execute props)
11055          (gnus-execute-command (cdr (assq 'execute props)))))
11056   (let ((gnus-current-article (gnus-summary-article-number)))
11057     (gnus-run-hooks 'gnus-mark-article-hook)))
11058
11059 (defun gnus-execute-command (command &optional automatic)
11060   (save-excursion
11061     (gnus-article-setup-buffer)
11062     (set-buffer gnus-article-buffer)
11063     (setq buffer-read-only nil)
11064     (let ((command (if automatic command
11065                      (read-string "Command: " (cons command 0)))))
11066       (erase-buffer)
11067       (insert "$ " command "\n\n")
11068       (if gnus-view-pseudo-asynchronously
11069           (start-process "gnus-execute" (current-buffer) shell-file-name
11070                          shell-command-switch command)
11071         (call-process shell-file-name nil t nil
11072                       shell-command-switch command)))))
11073
11074 ;; Summary kill commands.
11075
11076 (defun gnus-summary-edit-global-kill (article)
11077   "Edit the \"global\" kill file."
11078   (interactive (list (gnus-summary-article-number)))
11079   (gnus-group-edit-global-kill article))
11080
11081 (defun gnus-summary-edit-local-kill ()
11082   "Edit a local kill file applied to the current newsgroup."
11083   (interactive)
11084   (setq gnus-current-headers (gnus-summary-article-header))
11085   (gnus-group-edit-local-kill
11086    (gnus-summary-article-number) gnus-newsgroup-name))
11087
11088 ;;; Header reading.
11089
11090 (defun gnus-read-header (id &optional header)
11091   "Read the headers of article ID and enter them into the Gnus system."
11092   (let ((group gnus-newsgroup-name)
11093         (gnus-override-method
11094          (or
11095           gnus-override-method
11096           (and (gnus-news-group-p gnus-newsgroup-name)
11097                (car (gnus-refer-article-methods)))))
11098         where)
11099     ;; First we check to see whether the header in question is already
11100     ;; fetched.
11101     (if (stringp id)
11102         ;; This is a Message-ID.
11103         (setq header (or header (gnus-id-to-header id)))
11104       ;; This is an article number.
11105       (setq header (or header (gnus-summary-article-header id))))
11106     (if (and header
11107              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11108         ;; We have found the header.
11109         header
11110       ;; If this is a sparse article, we have to nix out its
11111       ;; previous entry in the thread hashtb.
11112       (when (and header
11113                  (gnus-summary-article-sparse-p (mail-header-number header)))
11114         (let* ((parent (gnus-parent-id (mail-header-references header)))
11115                (thread (and parent (gnus-id-to-thread parent))))
11116           (when thread
11117             (delq (assq header thread) thread))))
11118       ;; We have to really fetch the header to this article.
11119       (save-excursion
11120         (set-buffer nntp-server-buffer)
11121         (when (setq where (gnus-request-head id group))
11122           (nnheader-fold-continuation-lines)
11123           (goto-char (point-max))
11124           (insert ".\n")
11125           (goto-char (point-min))
11126           (insert "211 ")
11127           (princ (cond
11128                   ((numberp id) id)
11129                   ((cdr where) (cdr where))
11130                   (header (mail-header-number header))
11131                   (t gnus-reffed-article-number))
11132                  (current-buffer))
11133           (insert " Article retrieved.\n"))
11134         (if (or (not where)
11135                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11136             ()                          ; Malformed head.
11137           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11138             (when (and (stringp id)
11139                        (not (string= (gnus-group-real-name group)
11140                                      (car where))))
11141               ;; If we fetched by Message-ID and the article came
11142               ;; from a different group, we fudge some bogus article
11143               ;; numbers for this article.
11144               (mail-header-set-number header gnus-reffed-article-number))
11145             (save-excursion
11146               (set-buffer gnus-summary-buffer)
11147               (decf gnus-reffed-article-number)
11148               (gnus-remove-header (mail-header-number header))
11149               (push header gnus-newsgroup-headers)
11150               (setq gnus-current-headers header)
11151               (push (mail-header-number header) gnus-newsgroup-limit)))
11152           header)))))
11153
11154 (defun gnus-remove-header (number)
11155   "Remove header NUMBER from `gnus-newsgroup-headers'."
11156   (if (and gnus-newsgroup-headers
11157            (= number (mail-header-number (car gnus-newsgroup-headers))))
11158       (pop gnus-newsgroup-headers)
11159     (let ((headers gnus-newsgroup-headers))
11160       (while (and (cdr headers)
11161                   (not (= number (mail-header-number (cadr headers)))))
11162         (pop headers))
11163       (when (cdr headers)
11164         (setcdr headers (cddr headers))))))
11165
11166 ;;;
11167 ;;; summary highlights
11168 ;;;
11169
11170 (defun gnus-highlight-selected-summary ()
11171   "Highlight selected article in summary buffer."
11172   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11173   (when gnus-summary-selected-face
11174     (save-excursion
11175       (let* ((beg (gnus-point-at-bol))
11176              (end (gnus-point-at-eol))
11177              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11178              (from (if (get-text-property beg gnus-mouse-face-prop)
11179                        beg
11180                      (or (next-single-property-change
11181                           beg gnus-mouse-face-prop nil end)
11182                          beg)))
11183              (to
11184               (if (= from end)
11185                   (- from 2)
11186                 (or (next-single-property-change
11187                      from gnus-mouse-face-prop nil end)
11188                     end))))
11189         ;; If no mouse-face prop on line we will have to = from = end,
11190         ;; so we highlight the entire line instead.
11191         (when (= (+ to 2) from)
11192           (setq from beg)
11193           (setq to end))
11194         (if gnus-newsgroup-selected-overlay
11195             ;; Move old overlay.
11196             (gnus-move-overlay
11197              gnus-newsgroup-selected-overlay from to (current-buffer))
11198           ;; Create new overlay.
11199           (gnus-overlay-put
11200            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11201            'face gnus-summary-selected-face))))))
11202
11203 (defvar gnus-summary-highlight-line-cached nil)
11204 (defvar gnus-summary-highlight-line-trigger nil)
11205
11206 (defun gnus-summary-highlight-line-0 ()
11207   (if (and (eq gnus-summary-highlight-line-trigger 
11208                gnus-summary-highlight)
11209            gnus-summary-highlight-line-cached)
11210       gnus-summary-highlight-line-cached
11211     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11212           gnus-summary-highlight-line-cached
11213           (let* ((cond (list 'cond))
11214                  (c cond)
11215                  (list gnus-summary-highlight))
11216             (while list
11217               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11218                               nil))
11219               (setq c (cdr c)
11220                     list (cdr list)))
11221             (gnus-byte-compile (list 'lambda nil cond))))))
11222
11223 (defun gnus-summary-highlight-line ()
11224   "Highlight current line according to `gnus-summary-highlight'."
11225   (let* ((beg (gnus-point-at-bol))
11226          (article (or (gnus-summary-article-number) gnus-current-article))
11227          (score (or (cdr (assq article
11228                                gnus-newsgroup-scored))
11229                     gnus-summary-default-score 0))
11230          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11231          (inhibit-read-only t)
11232          (default gnus-summary-default-score)
11233          (default-high gnus-summary-default-high-score)
11234          (default-low gnus-summary-default-low-score)
11235          (uncached (and gnus-summary-use-undownloaded-faces
11236                         (memq article gnus-newsgroup-undownloaded))))
11237     (let ((face (funcall (gnus-summary-highlight-line-0))))
11238       (unless (eq face (get-text-property beg 'face))
11239         (gnus-put-text-property-excluding-characters-with-faces
11240          beg (gnus-point-at-eol) 'face
11241          (setq face (if (boundp face) (symbol-value face) face)))
11242         (when gnus-summary-highlight-line-function
11243           (funcall gnus-summary-highlight-line-function article face))))))
11244
11245 (defun gnus-update-read-articles (group unread &optional compute)
11246   "Update the list of read articles in GROUP.
11247 UNREAD is a sorted list."
11248   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11249          (entry (gnus-gethash group gnus-newsrc-hashtb))
11250          (info (nth 2 entry))
11251          (prev 1)
11252          read)
11253     (if (or (not info) (not active))
11254         ;; There is no info on this group if it was, in fact,
11255         ;; killed.  Gnus stores no information on killed groups, so
11256         ;; there's nothing to be done.
11257         ;; One could store the information somewhere temporarily,
11258         ;; perhaps...  Hmmm...
11259         ()
11260       ;; Remove any negative articles numbers.
11261       (while (and unread (< (car unread) 0))
11262         (setq unread (cdr unread)))
11263       ;; Remove any expired article numbers
11264       (while (and unread (< (car unread) (car active)))
11265         (setq unread (cdr unread)))
11266       ;; Compute the ranges of read articles by looking at the list of
11267       ;; unread articles.
11268       (while unread
11269         (when (/= (car unread) prev)
11270           (push (if (= prev (1- (car unread))) prev
11271                   (cons prev (1- (car unread))))
11272                 read))
11273         (setq prev (1+ (car unread)))
11274         (setq unread (cdr unread)))
11275       (when (<= prev (cdr active))
11276         (push (cons prev (cdr active)) read))
11277       (setq read (if (> (length read) 1) (nreverse read) read))
11278       (if compute
11279           read
11280         (save-excursion
11281           (let (setmarkundo)
11282             ;; Propagate the read marks to the backend.
11283             (when (gnus-check-backend-function 'request-set-mark group)
11284               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11285                     (add (gnus-remove-from-range read (gnus-info-read info))))
11286                 (when (or add del)
11287                   (unless (gnus-check-group group)
11288                     (error "Can't open server for %s" group))
11289                   (gnus-request-set-mark
11290                    group (delq nil (list (if add (list add 'add '(read)))
11291                                          (if del (list del 'del '(read))))))
11292                   (setq setmarkundo
11293                         `(gnus-request-set-mark
11294                           ,group
11295                           ',(delq nil (list
11296                                        (if del (list del 'add '(read)))
11297                                        (if add (list add 'del '(read))))))))))
11298             (set-buffer gnus-group-buffer)
11299             (gnus-undo-register
11300               `(progn
11301                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11302                  (gnus-info-set-read ',info ',(gnus-info-read info))
11303                  (gnus-get-unread-articles-in-group ',info
11304                                                     (gnus-active ,group))
11305                  (gnus-group-update-group ,group t)
11306                  ,setmarkundo))))
11307         ;; Enter this list into the group info.
11308         (gnus-info-set-read info read)
11309         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11310         (gnus-get-unread-articles-in-group info (gnus-active group))
11311         t))))
11312
11313 (defun gnus-offer-save-summaries ()
11314   "Offer to save all active summary buffers."
11315   (let (buffers)
11316     ;; Go through all buffers and find all summaries.
11317     (dolist (buffer (buffer-list))
11318       (when (and (setq buffer (buffer-name buffer))
11319                  (string-match "Summary" buffer)
11320                  (save-excursion
11321                    (set-buffer buffer)
11322                    ;; We check that this is, indeed, a summary buffer.
11323                    (and (eq major-mode 'gnus-summary-mode)
11324                         ;; Also make sure this isn't bogus.
11325                         gnus-newsgroup-prepared
11326                         ;; Also make sure that this isn't a
11327                         ;; dead summary buffer.
11328                         (not gnus-dead-summary-mode))))
11329         (push buffer buffers)))
11330     ;; Go through all these summary buffers and offer to save them.
11331     (when buffers
11332       (save-excursion
11333         (map-y-or-n-p
11334          "Update summary buffer %s? "
11335          (lambda (buf)
11336            (switch-to-buffer buf)
11337            (gnus-summary-exit))
11338          buffers)))))
11339
11340 (defun gnus-summary-setup-default-charset ()
11341   "Setup newsgroup default charset."
11342   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11343       (setq gnus-newsgroup-charset nil)
11344     (let* ((ignored-charsets
11345             (or gnus-newsgroup-ephemeral-ignored-charsets
11346                 (append
11347                  (and gnus-newsgroup-name
11348                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11349                  gnus-newsgroup-ignored-charsets))))
11350       (setq gnus-newsgroup-charset
11351             (or gnus-newsgroup-ephemeral-charset
11352                 (and gnus-newsgroup-name
11353                      (gnus-parameter-charset gnus-newsgroup-name))
11354                 gnus-default-charset))
11355       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11356            ignored-charsets))))
11357
11358 ;;;
11359 ;;; Mime Commands
11360 ;;;
11361
11362 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11363   "Display the current article buffer fully MIME-buttonized.
11364 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11365 treated as multipart/mixed."
11366   (interactive "P")
11367   (require 'gnus-art)
11368   (let ((gnus-unbuttonized-mime-types nil)
11369         (gnus-mime-display-multipart-as-mixed show-all-parts))
11370     (gnus-summary-show-article)))
11371
11372 (defun gnus-summary-repair-multipart (article)
11373   "Add a Content-Type header to a multipart article without one."
11374   (interactive (list (gnus-summary-article-number)))
11375   (gnus-with-article article
11376     (message-narrow-to-head)
11377     (message-remove-header "Mime-Version")
11378     (goto-char (point-max))
11379     (insert "Mime-Version: 1.0\n")
11380     (widen)
11381     (when (search-forward "\n--" nil t)
11382       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11383         (message-narrow-to-head)
11384         (message-remove-header "Content-Type")
11385         (goto-char (point-max))
11386         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11387                         separator))
11388         (widen))))
11389   (let (gnus-mark-article-hook)
11390     (gnus-summary-select-article t t nil article)))
11391
11392 (defun gnus-summary-toggle-display-buttonized ()
11393   "Toggle the buttonizing of the article buffer."
11394   (interactive)
11395   (require 'gnus-art)
11396   (if (setq gnus-inhibit-mime-unbuttonizing
11397             (not gnus-inhibit-mime-unbuttonizing))
11398       (let ((gnus-unbuttonized-mime-types nil))
11399         (gnus-summary-show-article))
11400     (gnus-summary-show-article)))
11401
11402 ;;;
11403 ;;; Generic summary marking commands
11404 ;;;
11405
11406 (defvar gnus-summary-marking-alist
11407   '((read gnus-del-mark "d")
11408     (unread gnus-unread-mark "u")
11409     (ticked gnus-ticked-mark "!")
11410     (dormant gnus-dormant-mark "?")
11411     (expirable gnus-expirable-mark "e"))
11412   "An alist of names/marks/keystrokes.")
11413
11414 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11415 (defvar gnus-summary-mark-map)
11416
11417 (defun gnus-summary-make-all-marking-commands ()
11418   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11419   (dolist (elem gnus-summary-marking-alist)
11420     (apply 'gnus-summary-make-marking-command elem)))
11421
11422 (defun gnus-summary-make-marking-command (name mark keystroke)
11423   (let ((map (make-sparse-keymap)))
11424     (define-key gnus-summary-generic-mark-map keystroke map)
11425     (dolist (lway `((next "next" next nil "n")
11426                     (next-unread "next unread" next t "N")
11427                     (prev "previous" prev nil "p")
11428                     (prev-unread "previous unread" prev t "P")
11429                     (nomove "" nil nil ,keystroke)))
11430       (let ((func (gnus-summary-make-marking-command-1
11431                    mark (car lway) lway name)))
11432         (setq func (eval func))
11433         (define-key map (nth 4 lway) func)))))
11434
11435 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11436   `(defun ,(intern
11437             (format "gnus-summary-put-mark-as-%s%s"
11438                     name (if (eq way 'nomove)
11439                              ""
11440                            (concat "-" (symbol-name way)))))
11441      (n)
11442      ,(format
11443        "Mark the current article as %s%s.
11444 If N, the prefix, then repeat N times.
11445 If N is negative, move in reverse order.
11446 The difference between N and the actual number of articles marked is
11447 returned."
11448        name (cadr lway))
11449      (interactive "p")
11450      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11451
11452 (defun gnus-summary-generic-mark (n mark move unread)
11453   "Mark N articles with MARK."
11454   (unless (eq major-mode 'gnus-summary-mode)
11455     (error "This command can only be used in the summary buffer"))
11456   (gnus-summary-show-thread)
11457   (let ((nummove
11458          (cond
11459           ((eq move 'next) 1)
11460           ((eq move 'prev) -1)
11461           (t 0))))
11462     (if (zerop nummove)
11463         (setq n 1)
11464       (when (< n 0)
11465         (setq n (abs n)
11466               nummove (* -1 nummove))))
11467     (while (and (> n 0)
11468                 (gnus-summary-mark-article nil mark)
11469                 (zerop (gnus-summary-next-subject nummove unread t)))
11470       (setq n (1- n)))
11471     (when (/= 0 n)
11472       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11473     (gnus-summary-recenter)
11474     (gnus-summary-position-point)
11475     (gnus-set-mode-line 'summary)
11476     n))
11477
11478 (defun gnus-summary-insert-articles (articles)
11479   (when (setq articles
11480               (gnus-sorted-difference articles
11481                                       (mapcar (lambda (h)
11482                                                 (mail-header-number h))
11483                                               gnus-newsgroup-headers)))
11484     (setq gnus-newsgroup-headers
11485           (gnus-merge 'list
11486                       gnus-newsgroup-headers
11487                       (gnus-fetch-headers articles)
11488                       'gnus-article-sort-by-number))
11489     ;; Suppress duplicates?
11490     (when gnus-suppress-duplicates
11491       (gnus-dup-suppress-articles))
11492
11493     ;; We might want to build some more threads first.
11494     (when (and gnus-fetch-old-headers
11495                (eq gnus-headers-retrieved-by 'nov))
11496       (if (eq gnus-fetch-old-headers 'invisible)
11497           (gnus-build-all-threads)
11498         (gnus-build-old-threads)))
11499     ;; Let the Gnus agent mark articles as read.
11500     (when gnus-agent
11501       (gnus-agent-get-undownloaded-list))
11502     ;; Remove list identifiers from subject
11503     (when gnus-list-identifiers
11504       (gnus-summary-remove-list-identifiers))
11505     ;; First and last article in this newsgroup.
11506     (when gnus-newsgroup-headers
11507       (setq gnus-newsgroup-begin
11508             (mail-header-number (car gnus-newsgroup-headers))
11509             gnus-newsgroup-end
11510             (mail-header-number
11511              (gnus-last-element gnus-newsgroup-headers))))
11512     (when gnus-use-scoring
11513       (gnus-possibly-score-headers))))
11514
11515 (defun gnus-summary-insert-old-articles (&optional all)
11516   "Insert all old articles in this group.
11517 If ALL is non-nil, already read articles become readable.
11518 If ALL is a number, fetch this number of articles."
11519   (interactive "P")
11520   (prog1
11521       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11522             older len)
11523         (setq older
11524               ;; Some nntp servers lie about their active range.  When
11525               ;; this happens, the active range can be in the millions.
11526               ;; Use a compressed range to avoid creating a huge list.
11527               (gnus-range-difference (list gnus-newsgroup-active) old))
11528         (setq len (gnus-range-length older))
11529         (cond
11530          ((null older) nil)
11531          ((numberp all)
11532           (if (< all len)
11533               (let ((older-range (nreverse older)))
11534                 (setq older nil)
11535
11536                 (while (> all 0)
11537                   (let* ((r (pop older-range))
11538                          (min (if (numberp r) r (car r)))
11539                          (max (if (numberp r) r (cdr r))))
11540                     (while (and (<= min max)
11541                                 (> all 0))
11542                       (push max older)
11543                       (setq all (1- all)
11544                             max (1- max))))))
11545             (setq older (gnus-uncompress-range older))))
11546          (all
11547           (setq older (gnus-uncompress-range older)))
11548          (t
11549           (when (and (numberp gnus-large-newsgroup)
11550                    (> len gnus-large-newsgroup))
11551               (let* ((cursor-in-echo-area nil)
11552                      (initial (gnus-parameter-large-newsgroup-initial
11553                                gnus-newsgroup-name))
11554                      (input
11555                       (read-string
11556                        (format
11557                         "How many articles from %s (%s %d): "
11558                         (gnus-limit-string
11559                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11560                         (if initial "max" "default")
11561                         len)
11562                        (if initial
11563                            (cons (number-to-string initial)
11564                                  0)))))
11565                 (unless (string-match "^[ \t]*$" input)
11566                   (setq all (string-to-number input))
11567                   (if (< all len)
11568                       (let ((older-range (nreverse older)))
11569                         (setq older nil)
11570
11571                         (while (> all 0)
11572                           (let* ((r (pop older-range))
11573                                  (min (if (numberp r) r (car r)))
11574                                  (max (if (numberp r) r (cdr r))))
11575                             (while (and (<= min max)
11576                                         (> all 0))
11577                               (push max older)
11578                               (setq all (1- all)
11579                                     max (1- max))))))))))
11580           (setq older (gnus-uncompress-range older))))
11581         (if (not older)
11582             (message "No old news.")
11583           (gnus-summary-insert-articles older)
11584           (gnus-summary-limit (gnus-sorted-nunion old older))))
11585     (gnus-summary-position-point)))
11586
11587 (defun gnus-summary-insert-new-articles ()
11588   "Insert all new articles in this group."
11589   (interactive)
11590   (prog1
11591       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11592             (old-active gnus-newsgroup-active)
11593             (nnmail-fetched-sources (list t))
11594             i new)
11595         (setq gnus-newsgroup-active
11596               (gnus-activate-group gnus-newsgroup-name 'scan))
11597         (setq i (cdr gnus-newsgroup-active))
11598         (while (> i (cdr old-active))
11599           (push i new)
11600           (decf i))
11601         (if (not new)
11602             (message "No gnus is bad news.")
11603           (gnus-summary-insert-articles new)
11604           (setq gnus-newsgroup-unreads
11605                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11606           (gnus-summary-limit (gnus-sorted-nunion old new))))
11607     (gnus-summary-position-point)))
11608
11609 (gnus-summary-make-all-marking-commands)
11610
11611 (gnus-ems-redefine)
11612
11613 (provide 'gnus-sum)
11614
11615 (run-hooks 'gnus-sum-load-hook)
11616
11617 ;; Local Variables:
11618 ;; coding: iso-8859-1
11619 ;; End:
11620
11621 ;;; gnus-sum.el ends here