Synch to Gnus 200308050104.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-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 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (require 'gnus-clfns)
34   (defvar tool-bar-map))
35
36 (require 'gnus)
37 (require 'gnus-group)
38 (require 'gnus-spec)
39 (require 'gnus-range)
40 (require 'gnus-int)
41 (require 'gnus-undo)
42 (require 'gnus-util)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
57 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
58 (autoload 'mm-uu-dissect "mm-uu")
59 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
60   "Deuglify broken Outlook (Express) articles and redisplay."
61   t)
62 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
63 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
64 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
65
66 (defcustom gnus-kill-summary-on-exit t
67   "*If non-nil, kill the summary buffer when you exit from it.
68 If nil, the summary will become a \"*Dead Summary*\" buffer, and
69 it will be killed sometime later."
70   :group 'gnus-summary-exit
71   :type 'boolean)
72
73 (defcustom gnus-fetch-old-headers nil
74   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
75 If an unread article in the group refers to an older, already read (or
76 just marked as read) article, the old article will not normally be
77 displayed in the Summary buffer.  If this variable is non-nil, Gnus
78 will attempt to grab the headers to the old articles, and thereby
79 build complete threads.  If it has the value `some', only enough
80 headers to connect otherwise loose threads will be displayed.  This
81 variable can also be a number.  In that case, no more than that number
82 of old headers will be fetched.  If it has the value `invisible', all
83 old headers will be fetched, but none will be displayed.
84
85 The server has to support NOV for any of this to work."
86   :group 'gnus-thread
87   :type '(choice (const :tag "off" nil)
88                  (const some)
89                  number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-refer-thread-limit 200
93   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
94 If t, fetch all the available old headers."
95   :group 'gnus-thread
96   :type '(choice number
97                  (sexp :menu-tag "other" t)))
98
99 (defcustom gnus-summary-make-false-root 'adopt
100   "*nil means that Gnus won't gather loose threads.
101 If the root of a thread has expired or been read in a previous
102 session, the information necessary to build a complete thread has been
103 lost.  Instead of having many small sub-threads from this original thread
104 scattered all over the summary buffer, Gnus can gather them.
105
106 If non-nil, Gnus will try to gather all loose sub-threads from an
107 original thread into one large thread.
108
109 If this variable is non-nil, it should be one of `none', `adopt',
110 `dummy' or `empty'.
111
112 If this variable is `none', Gnus will not make a false root, but just
113 present the sub-threads after another.
114 If this variable is `dummy', Gnus will create a dummy root that will
115 have all the sub-threads as children.
116 If this variable is `adopt', Gnus will make one of the \"children\"
117 the parent and mark all the step-children as such.
118 If this variable is `empty', the \"children\" are printed with empty
119 subject fields.  (Or rather, they will be printed with a string
120 given by the `gnus-summary-same-subject' variable.)"
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const none)
124                  (const dummy)
125                  (const adopt)
126                  (const empty)))
127
128 (defcustom gnus-summary-make-false-root-always nil
129   "Always make a false dummy root."
130   :group 'gnus-thread
131   :type 'boolean)
132
133 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
134   "*A regexp to match subjects to be excluded from loose thread gathering.
135 As loose thread gathering is done on subjects only, that means that
136 there can be many false gatherings performed.  By rooting out certain
137 common subjects, gathering might become saner."
138   :group 'gnus-thread
139   :type 'regexp)
140
141 (defcustom gnus-summary-gather-subject-limit nil
142   "*Maximum length of subject comparisons when gathering loose threads.
143 Use nil to compare full subjects.  Setting this variable to a low
144 number will help gather threads that have been corrupted by
145 newsreaders chopping off subject lines, but it might also mean that
146 unrelated articles that have subject that happen to begin with the
147 same few characters will be incorrectly gathered.
148
149 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
150 comparing subjects."
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const fuzzy)
154                  (sexp :menu-tag "on" t)))
155
156 (defcustom gnus-simplify-subject-functions nil
157   "List of functions taking a string argument that simplify subjects.
158 The functions are applied recursively.
159
160 Useful functions to put in this list include:
161 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
162 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
163   :group 'gnus-thread
164   :type '(repeat function))
165
166 (defcustom gnus-simplify-ignored-prefixes nil
167   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
168   :group 'gnus-thread
169   :type '(choice (const :tag "off" nil)
170                  regexp))
171
172 (defcustom gnus-build-sparse-threads nil
173   "*If non-nil, fill in the gaps in threads.
174 If `some', only fill in the gaps that are needed to tie loose threads
175 together.  If `more', fill in all leaf nodes that Gnus can find.  If
176 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
177   :group 'gnus-thread
178   :type '(choice (const :tag "off" nil)
179                  (const some)
180                  (const more)
181                  (sexp :menu-tag "all" t)))
182
183 (defcustom gnus-summary-thread-gathering-function
184   'gnus-gather-threads-by-subject
185   "*Function used for gathering loose threads.
186 There are two pre-defined functions: `gnus-gather-threads-by-subject',
187 which only takes Subjects into consideration; and
188 `gnus-gather-threads-by-references', which compared the References
189 headers of the articles to find matches."
190   :group 'gnus-thread
191   :type '(radio (function-item gnus-gather-threads-by-subject)
192                 (function-item gnus-gather-threads-by-references)
193                 (function :tag "other")))
194
195 (defcustom gnus-summary-same-subject ""
196   "*String indicating that the current article has the same subject as the previous.
197 This variable will only be used if the value of
198 `gnus-summary-make-false-root' is `empty'."
199   :group 'gnus-summary-format
200   :type 'string)
201
202 (defcustom gnus-summary-goto-unread t
203   "*If t, many commands will go to the next unread article.
204 This applies to marking commands as well as other commands that
205 \"naturally\" select the next article, like, for instance, `SPC' at
206 the end of an article.
207
208 If nil, the marking commands do NOT go to the next unread article
209 \(they go to the next article instead).  If `never', commands that
210 usually go to the next unread article, will go to the next article,
211 whether it is read or not."
212   :group 'gnus-summary-marks
213   :link '(custom-manual "(gnus)Setting Marks")
214   :type '(choice (const :tag "off" nil)
215                  (const never)
216                  (sexp :menu-tag "on" t)))
217
218 (defcustom gnus-summary-default-score 0
219   "*Default article score level.
220 All scores generated by the score files will be added to this score.
221 If this variable is nil, scoring will be disabled."
222   :group 'gnus-score-default
223   :type '(choice (const :tag "disable")
224                  integer))
225
226 (defcustom gnus-summary-default-high-score 0
227   "*Default threshold for a high scored article.
228 An article will be highlighted as high scored if its score is greater
229 than this score."
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :group 'gnus-score-default
238   :type 'integer)
239
240 (defcustom gnus-summary-zcore-fuzz 0
241   "*Fuzziness factor for the zcore in the summary buffer.
242 Articles with scores closer than this to `gnus-summary-default-score'
243 will not be marked."
244   :group 'gnus-summary-format
245   :type 'integer)
246
247 (defcustom gnus-simplify-subject-fuzzy-regexp nil
248   "*Strings to be removed when doing fuzzy matches.
249 This can either be a regular expression or list of regular expressions
250 that will be removed from subject strings if fuzzy subject
251 simplification is selected."
252   :group 'gnus-thread
253   :type '(repeat regexp))
254
255 (defcustom gnus-show-threads t
256   "*If non-nil, display threads in summary mode."
257   :group 'gnus-thread
258   :type 'boolean)
259
260 (defcustom gnus-thread-hide-subtree nil
261   "*If non-nil, hide all threads initially.
262 This can be a predicate specifier which says which threads to hide.
263 If threads are hidden, you have to run the command
264 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
265 to expose hidden threads."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier" :size 0)))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), 'unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :group 'gnus-group-select
333   :type '(choice (const best)
334                  (const unread)
335                  (const first)
336                  (const unseen)
337                  (const unseen-or-unread)))
338
339 (defcustom gnus-dont-select-after-jump-to-other-group nil
340   "If non-nil, don't select the first unread article after entering the
341 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
342 it is depend on the value of `gnus-auto-select-first' whether to select
343 or not."
344   :group 'gnus-group-select
345   :type 'boolean)
346
347 (defcustom gnus-auto-select-next t
348   "*If non-nil, offer to go to the next group from the end of the previous.
349 If the value is t and the next newsgroup is empty, Gnus will exit
350 summary mode and go back to group mode.  If the value is neither nil
351 nor t, Gnus will select the following unread newsgroup.  In
352 particular, if the value is the symbol `quietly', the next unread
353 newsgroup will be selected without any confirmation, and if it is
354 `almost-quietly', the next group will be selected without any
355 confirmation if you are located on the last article in the group.
356 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
357 will go to the next group without confirmation."
358   :group 'gnus-summary-maneuvering
359   :type '(choice (const :tag "off" nil)
360                  (const quietly)
361                  (const almost-quietly)
362                  (const slightly-quietly)
363                  (sexp :menu-tag "on" t)))
364
365 (defcustom gnus-auto-select-same nil
366   "*If non-nil, select the next article with the same subject.
367 If there are no more articles with the same subject, go to
368 the first unread article."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-goto-ignores 'unfetched
373   "*Says how to handle unfetched articles when maneuvering.
374
375 This variable can either be the symbols nil (maneuver to any
376 article), `undownloaded' (maneuvering while unplugged ignores articles
377 that have not been fetched), `always-undownloaded' (maneuvering always
378 ignores articles that have not been fetched), `unfetched' (maneuvering
379 ignores articles whose headers have not been fetched).
380
381 NOTE: The list of unfetched articles will always be nil when plugged
382 and, when unplugged, a subset of the undownloaded article list."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "None" nil)
385                  (const :tag "Undownloaded when unplugged" undownloaded)
386                  (const :tag "Undownloaded" always-undownloaded)
387                  (const :tag "Unfetched" unfetched)))
388
389 (defcustom gnus-summary-check-current nil
390   "*If non-nil, consider the current article when moving.
391 The \"unread\" movement commands will stay on the same line if the
392 current article is unread."
393   :group 'gnus-summary-maneuvering
394   :type 'boolean)
395
396 (defcustom gnus-auto-center-summary t
397   "*If non-nil, always center the current summary buffer.
398 In particular, if `vertical' do only vertical recentering.  If non-nil
399 and non-`vertical', do both horizontal and vertical recentering."
400   :group 'gnus-summary-maneuvering
401   :type '(choice (const :tag "none" nil)
402                  (const vertical)
403                  (integer :tag "height")
404                  (sexp :menu-tag "both" t)))
405
406 (defvar gnus-auto-center-group t
407   "*If non-nil, always center the group buffer.")
408
409 (defcustom gnus-show-all-headers nil
410   "*If non-nil, don't hide any headers."
411   :group 'gnus-article-hiding
412   :group 'gnus-article-headers
413   :type 'boolean)
414
415 (defcustom gnus-summary-ignore-duplicates nil
416   "*If non-nil, ignore articles with identical Message-ID headers."
417   :group 'gnus-summary
418   :type 'boolean)
419
420 (defcustom gnus-single-article-buffer t
421   "*If non-nil, display all articles in the same buffer.
422 If nil, each group will get its own article buffer."
423   :group 'gnus-article-various
424   :type 'boolean)
425
426 (defcustom gnus-break-pages t
427   "*If non-nil, do page breaking on articles.
428 The page delimiter is specified by the `gnus-page-delimiter'
429 variable."
430   :group 'gnus-article-various
431   :type 'boolean)
432
433 (defcustom gnus-show-mime t
434   "*If non-nil, do mime processing of articles.
435 The articles will simply be fed to the function given by
436 `gnus-article-display-method-for-mime'."
437   :group 'gnus-article-mime
438   :type 'boolean)
439
440 (defcustom gnus-move-split-methods nil
441   "*Variable used to suggest where articles are to be moved to.
442 It uses the same syntax as the `gnus-split-methods' variable.
443 However, whereas `gnus-split-methods' specifies file names as targets,
444 this variable specifies group names."
445   :group 'gnus-summary-mail
446   :type '(repeat (choice (list :value (fun) function)
447                          (cons :value ("" "") regexp (repeat string))
448                          (sexp :value nil))))
449
450 (defcustom gnus-unread-mark ?\ ;;;Whitespace
451   "*Mark used for unread articles."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-ticked-mark ?!
456   "*Mark used for ticked articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-dormant-mark ??
461   "*Mark used for dormant articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-del-mark ?r
466   "*Mark used for del'd articles."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-read-mark ?R
471   "*Mark used for read articles."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-expirable-mark ?E
476   "*Mark used for expirable articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-killed-mark ?K
481   "*Mark used for killed articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-spam-mark ?$
486   "*Mark used for spam articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-souped-mark ?F
491   "*Mark used for souped articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-kill-file-mark ?X
496   "*Mark used for articles killed by kill files."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-low-score-mark ?Y
501   "*Mark used for articles with a low score."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-catchup-mark ?C
506   "*Mark used for articles that are caught up."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-replied-mark ?A
511   "*Mark used for articles that have been replied to."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-forwarded-mark ?F
516   "*Mark used for articles that have been forwarded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-recent-mark ?N
521   "*Mark used for articles that are recent."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-no-mark ?\ ;;;Whitespace
541   "*Mark used for articles that have no other secondary mark."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-ancient-mark ?O
546   "*Mark used for ancient articles."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-sparse-mark ?Q
551   "*Mark used for sparsely reffed articles."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-canceled-mark ?G
556   "*Mark used for canceled articles."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-duplicate-mark ?M
561   "*Mark used for duplicate articles."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-undownloaded-mark ?-
566   "*Mark used for articles that weren't downloaded."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-downloaded-mark ?+
571   "*Mark used for articles that were downloaded."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-downloadable-mark ?%
576   "*Mark used for articles that are to be downloaded."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-unsendable-mark ?=
581   "*Mark used for articles that won't be sent."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-score-over-mark ?+
586   "*Score mark used for articles with high scores."
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-score-below-mark ?-
591   "*Score mark used for articles with low scores."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
596   "*There is no thread under the article."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-not-empty-thread-mark ?=
601   "*There is a thread under the article."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-view-pseudo-asynchronously nil
606   "*If non-nil, Gnus will view pseudo-articles asynchronously."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-auto-expirable-marks
611   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
612         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
613         gnus-souped-mark gnus-duplicate-mark)
614   "*The list of marks converted into expiration if a group is auto-expirable."
615   :version "21.1"
616   :group 'gnus-summary
617   :type '(repeat character))
618
619 (defcustom gnus-inhibit-user-auto-expire t
620   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
621   :version "21.1"
622   :group 'gnus-summary
623   :type 'boolean)
624
625 (defcustom gnus-view-pseudos nil
626   "*If `automatic', pseudo-articles will be viewed automatically.
627 If `not-confirm', pseudos will be viewed automatically, and the user
628 will not be asked to confirm the command."
629   :group 'gnus-extract-view
630   :type '(choice (const :tag "off" nil)
631                  (const automatic)
632                  (const not-confirm)))
633
634 (defcustom gnus-view-pseudos-separately t
635   "*If non-nil, one pseudo-article will be created for each file to be viewed.
636 If nil, all files that use the same viewing command will be given as a
637 list of parameters to that command."
638   :group 'gnus-extract-view
639   :type 'boolean)
640
641 (defcustom gnus-insert-pseudo-articles t
642   "*If non-nil, insert pseudo-articles when decoding articles."
643   :group 'gnus-extract-view
644   :type 'boolean)
645
646 (defcustom gnus-summary-dummy-line-format
647   "   %(:                             :%) %S\n"
648   "*The format specification for the dummy roots in the summary buffer.
649 It works along the same lines as a normal formatting string,
650 with some simple extensions.
651
652 %S  The subject
653
654 General format specifiers can also be used.
655 See `(gnus)Formatting Variables'."
656   :link '(custom-manual "(gnus)Formatting Variables")
657   :group 'gnus-threading
658   :type 'string)
659
660 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
661   "*The format specification for the summary mode line.
662 It works along the same lines as a normal formatting string,
663 with some simple extensions:
664
665 %G  Group name
666 %p  Unprefixed group name
667 %A  Current article number
668 %z  Current article score
669 %V  Gnus version
670 %U  Number of unread articles in the group
671 %e  Number of unselected articles in the group
672 %Z  A string with unread/unselected article counts
673 %g  Shortish group name
674 %S  Subject of the current article
675 %u  User-defined spec
676 %s  Current score file name
677 %d  Number of dormant articles
678 %r  Number of articles that have been marked as read in this session
679 %E  Number of articles expunged by the score files"
680   :group 'gnus-summary-format
681   :type 'string)
682
683 (defcustom gnus-list-identifiers nil
684   "Regexp that matches list identifiers to be removed from subject.
685 This can also be a list of regexps."
686   :version "21.1"
687   :group 'gnus-summary-format
688   :group 'gnus-article-hiding
689   :type '(choice (const :tag "none" nil)
690                  (regexp :value ".*")
691                  (repeat :value (".*") regexp)))
692
693 (defcustom gnus-summary-mark-below 0
694   "*Mark all articles with a score below this variable as read.
695 This variable is local to each summary buffer and usually set by the
696 score file."
697   :group 'gnus-score-default
698   :type 'integer)
699
700 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
701   "*List of functions used for sorting articles in the summary buffer.
702
703 Each function takes two articles and returns non-nil if the first
704 article should be sorted before the other.  If you use more than one
705 function, the primary sort function should be the last.  You should
706 probably always include `gnus-article-sort-by-number' in the list of
707 sorting functions -- preferably first.  Also note that sorting by date
708 is often much slower than sorting by number, and the sorting order is
709 very similar.  (Sorting by date means sorting by the time the message
710 was sent, sorting by number means sorting by arrival time.)
711
712 Ready-made functions include `gnus-article-sort-by-number',
713 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
714 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
715 and `gnus-article-sort-by-score'.
716
717 When threading is turned on, the variable `gnus-thread-sort-functions'
718 controls how articles are sorted."
719   :group 'gnus-summary-sort
720   :type '(repeat (choice (function-item gnus-article-sort-by-number)
721                          (function-item gnus-article-sort-by-author)
722                          (function-item gnus-article-sort-by-subject)
723                          (function-item gnus-article-sort-by-date)
724                          (function-item gnus-article-sort-by-score)
725                          (function-item gnus-article-sort-by-random)
726                          (function :tag "other"))))
727
728 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
729   "*List of functions used for sorting threads in the summary buffer.
730 By default, threads are sorted by article number.
731
732 Each function takes two threads and returns non-nil if the first
733 thread should be sorted before the other.  If you use more than one
734 function, the primary sort function should be the last.  You should
735 probably always include `gnus-thread-sort-by-number' in the list of
736 sorting functions -- preferably first.  Also note that sorting by date
737 is often much slower than sorting by number, and the sorting order is
738 very similar.  (Sorting by date means sorting by the time the message
739 was sent, sorting by number means sorting by arrival time.)
740
741 Ready-made functions include `gnus-thread-sort-by-number',
742 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
743 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
744 `gnus-thread-sort-by-most-recent-number',
745 `gnus-thread-sort-by-most-recent-date',
746 `gnus-thread-sort-by-random', and
747 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
748
749 When threading is turned off, the variable
750 `gnus-article-sort-functions' controls how articles are sorted."
751   :group 'gnus-summary-sort
752   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
753                          (function-item gnus-thread-sort-by-author)
754                          (function-item gnus-thread-sort-by-subject)
755                          (function-item gnus-thread-sort-by-date)
756                          (function-item gnus-thread-sort-by-score)
757                          (function-item gnus-thread-sort-by-total-score)
758                          (function-item gnus-thread-sort-by-random)
759                          (function :tag "other"))))
760
761 (defcustom gnus-thread-score-function '+
762   "*Function used for calculating the total score of a thread.
763
764 The function is called with the scores of the article and each
765 subthread and should then return the score of the thread.
766
767 Some functions you can use are `+', `max', or `min'."
768   :group 'gnus-summary-sort
769   :type 'function)
770
771 (defcustom gnus-summary-expunge-below nil
772   "All articles that have a score less than this variable will be expunged.
773 This variable is local to the summary buffers."
774   :group 'gnus-score-default
775   :type '(choice (const :tag "off" nil)
776                  integer))
777
778 (defcustom gnus-thread-expunge-below nil
779   "All threads that have a total score less than this variable will be expunged.
780 See `gnus-thread-score-function' for en explanation of what a
781 \"thread score\" is.
782
783 This variable is local to the summary buffers."
784   :group 'gnus-threading
785   :group 'gnus-score-default
786   :type '(choice (const :tag "off" nil)
787                  integer))
788
789 (defcustom gnus-summary-mode-hook nil
790   "*A hook for Gnus summary mode.
791 This hook is run before any variables are set in the summary buffer."
792   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
793   :group 'gnus-summary-various
794   :type 'hook)
795
796 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
797 (when (featurep 'xemacs)
798   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
799   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
800   (add-hook 'gnus-summary-mode-hook
801             'gnus-xmas-switch-horizontal-scrollbar-off))
802
803 (defcustom gnus-summary-menu-hook nil
804   "*Hook run after the creation of the summary mode menu."
805   :group 'gnus-summary-visual
806   :type 'hook)
807
808 (defcustom gnus-summary-exit-hook nil
809   "*A hook called on exit from the summary buffer.
810 It will be called with point in the group buffer."
811   :group 'gnus-summary-exit
812   :type 'hook)
813
814 (defcustom gnus-summary-prepare-hook nil
815   "*A hook called after the summary buffer has been generated.
816 If you want to modify the summary buffer, you can use this hook."
817   :group 'gnus-summary-various
818   :type 'hook)
819
820 (defcustom gnus-summary-prepared-hook nil
821   "*A hook called as the last thing after the summary buffer has been generated."
822   :group 'gnus-summary-various
823   :type 'hook)
824
825 (defcustom gnus-summary-generate-hook nil
826   "*A hook run just before generating the summary buffer.
827 This hook is commonly used to customize threading variables and the
828 like."
829   :group 'gnus-summary-various
830   :type 'hook)
831
832 (defcustom gnus-select-group-hook nil
833   "*A hook called when a newsgroup is selected.
834
835 If you'd like to simplify subjects like the
836 `gnus-summary-next-same-subject' command does, you can use the
837 following hook:
838
839  (add-hook gnus-select-group-hook
840            (lambda ()
841              (mapcar (lambda (header)
842                        (mail-header-set-subject
843                         header
844                         (gnus-simplify-subject
845                          (mail-header-subject header) 're-only)))
846                      gnus-newsgroup-headers)))"
847   :group 'gnus-group-select
848   :type 'hook)
849
850 (defcustom gnus-select-article-hook nil
851   "*A hook called when an article is selected."
852   :group 'gnus-summary-choose
853   :options '(gnus-agent-fetch-selected-article)
854   :type 'hook)
855
856 (defcustom gnus-visual-mark-article-hook
857   (list 'gnus-highlight-selected-summary)
858   "*Hook run after selecting an article in the summary buffer.
859 It is meant to be used for highlighting the article in some way.  It
860 is not run if `gnus-visual' is nil."
861   :group 'gnus-summary-visual
862   :type 'hook)
863
864 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
865   "*A hook called before parsing the headers."
866   :group 'gnus-various
867   :type 'hook)
868
869 (defcustom gnus-exit-group-hook nil
870   "*A hook called when exiting summary mode.
871 This hook is not called from the non-updating exit commands like `Q'."
872   :group 'gnus-various
873   :type 'hook)
874
875 (defcustom gnus-summary-update-hook
876   (list 'gnus-summary-highlight-line)
877   "*A hook called when a summary line is changed.
878 The hook will not be called if `gnus-visual' is nil.
879
880 The default function `gnus-summary-highlight-line' will
881 highlight the line according to the `gnus-summary-highlight'
882 variable."
883   :group 'gnus-summary-visual
884   :type 'hook)
885
886 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
887   "*A hook called when an article is selected for the first time.
888 The hook is intended to mark an article as read (or unread)
889 automatically when it is selected."
890   :group 'gnus-summary-choose
891   :type 'hook)
892
893 (defcustom gnus-group-no-more-groups-hook nil
894   "*A hook run when returning to group mode having no more (unread) groups."
895   :group 'gnus-group-select
896   :type 'hook)
897
898 (defcustom gnus-ps-print-hook nil
899   "*A hook run before ps-printing something from Gnus."
900   :group 'gnus-summary
901   :type 'hook)
902
903 (defcustom gnus-summary-article-move-hook nil
904   "*A hook called after an article is moved, copied, respooled, or crossposted."
905   :group 'gnus-summary
906   :type 'hook)
907
908 (defcustom gnus-summary-article-delete-hook nil
909   "*A hook called after an article is deleted."
910   :group 'gnus-summary
911   :type 'hook)
912
913 (defcustom gnus-summary-article-expire-hook nil
914   "*A hook called after an article is expired."
915   :group 'gnus-summary
916   :type 'hook)
917
918 (defcustom gnus-summary-display-arrow
919   (and (fboundp 'display-graphic-p)
920        (display-graphic-p))
921   "*If non-nil, display an arrow highlighting the current article."
922   :version "21.1"
923   :group 'gnus-summary
924   :type 'boolean)
925
926 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
927   "Face used for highlighting the current article in the summary buffer."
928   :group 'gnus-summary-visual
929   :type 'face)
930
931 (defvar gnus-tmp-downloaded nil)
932
933 (defcustom gnus-summary-highlight
934   '(((eq mark gnus-canceled-mark)
935      . gnus-summary-cancelled-face)
936     ((and uncached (> score default-high))
937      . gnus-summary-high-undownloaded-face)
938     ((and uncached (< score default-low))
939      . gnus-summary-low-undownloaded-face)
940     (uncached
941      . gnus-summary-normal-undownloaded-face)
942     ((and (> score default-high)
943           (or (eq mark gnus-dormant-mark)
944               (eq mark gnus-ticked-mark)))
945      . gnus-summary-high-ticked-face)
946     ((and (< score default-low)
947           (or (eq mark gnus-dormant-mark)
948               (eq mark gnus-ticked-mark)))
949      . gnus-summary-low-ticked-face)
950     ((or (eq mark gnus-dormant-mark)
951          (eq mark gnus-ticked-mark))
952      . gnus-summary-normal-ticked-face)
953     ((and (> score default-high) (eq mark gnus-ancient-mark))
954      . gnus-summary-high-ancient-face)
955     ((and (< score default-low) (eq mark gnus-ancient-mark))
956      . gnus-summary-low-ancient-face)
957     ((eq mark gnus-ancient-mark)
958      . gnus-summary-normal-ancient-face)
959     ((and (> score default-high) (eq mark gnus-unread-mark))
960      . gnus-summary-high-unread-face)
961     ((and (< score default-low) (eq mark gnus-unread-mark))
962      . gnus-summary-low-unread-face)
963     ((eq mark gnus-unread-mark)
964      . gnus-summary-normal-unread-face)
965     ((> score default-high)
966      . gnus-summary-high-read-face)
967     ((< score default-low)
968      . gnus-summary-low-read-face)
969     (t
970      . gnus-summary-normal-read-face))
971   "*Controls the highlighting of summary buffer lines.
972
973 A list of (FORM . FACE) pairs.  When deciding how a a particular
974 summary line should be displayed, each form is evaluated.  The content
975 of the face field after the first true form is used.  You can change
976 how those summary lines are displayed, by editing the face field.
977
978 You can use the following variables in the FORM field.
979
980 score:        The article's score
981 default:      The default article score.
982 default-high: The default score for high scored articles.
983 default-low:  The default score for low scored articles.
984 below:        The score below which articles are automatically marked as read.
985 mark:         The article's mark.
986 uncached:     Non-nil if the article is uncached."
987   :group 'gnus-summary-visual
988   :type '(repeat (cons (sexp :tag "Form" nil)
989                        face)))
990
991 (defcustom gnus-alter-header-function nil
992   "Function called to allow alteration of article header structures.
993 The function is called with one parameter, the article header vector,
994 which it may alter in any way."
995   :type '(choice (const :tag "None" nil)
996                  function)
997   :group 'gnus-summary)
998
999 (defvar gnus-decode-encoded-word-function
1000   (mime-find-field-decoder 'From 'nov)
1001   "Variable that says which function should be used to decode a string with encoded words.")
1002
1003 (defcustom gnus-extra-headers '(To Newsgroups)
1004   "*Extra headers to parse."
1005   :version "21.1"
1006   :group 'gnus-summary
1007   :type '(repeat symbol))
1008
1009 (defcustom gnus-ignored-from-addresses
1010   (and user-mail-address (regexp-quote user-mail-address))
1011   "*Regexp of From headers that may be suppressed in favor of To headers."
1012   :version "21.1"
1013   :group 'gnus-summary
1014   :type 'regexp)
1015
1016 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1017   "List of charsets that should be ignored.
1018 When these charsets are used in the \"charset\" parameter, the
1019 default charset will be used instead."
1020   :version "21.1"
1021   :type '(repeat symbol)
1022   :group 'gnus-charset)
1023
1024 (gnus-define-group-parameter
1025  ignored-charsets
1026  :type list
1027  :function-document
1028  "Return the ignored charsets of GROUP."
1029  :variable gnus-group-ignored-charsets-alist
1030  :variable-default
1031  '(("alt\\.chinese\\.text" iso-8859-1))
1032  :variable-document
1033  "Alist of regexps (to match group names) and charsets that should be ignored.
1034 When these charsets are used in the \"charset\" parameter, the
1035 default charset will be used instead."
1036  :variable-group gnus-charset
1037  :variable-type '(repeat (cons (regexp :tag "Group")
1038                                (repeat symbol)))
1039  :parameter-type '(choice :tag "Ignored charsets"
1040                           :value nil
1041                           (repeat (symbol)))
1042  :parameter-document       "\
1043 List of charsets that should be ignored.
1044
1045 When these charsets are used in the \"charset\" parameter, the
1046 default charset will be used instead.")
1047
1048 (defcustom gnus-group-highlight-words-alist nil
1049   "Alist of group regexps and highlight regexps.
1050 This variable uses the same syntax as `gnus-emphasis-alist'."
1051   :version "21.1"
1052   :type '(repeat (cons (regexp :tag "Group")
1053                        (repeat (list (regexp :tag "Highlight regexp")
1054                                      (number :tag "Group for entire word" 0)
1055                                      (number :tag "Group for displayed part" 0)
1056                                      (symbol :tag "Face"
1057                                              gnus-emphasis-highlight-words)))))
1058   :group 'gnus-summary-visual)
1059
1060 (defcustom gnus-use-wheel nil
1061   "Use Intelli-mouse on summary movement"
1062   :type 'boolean
1063   :group 'gnus-summary-maneuvering)
1064
1065 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1066   "Amount to scroll messages by spinning the mouse wheel.
1067 This is actually a cons cell, where the first item is the amount to scroll
1068 on a normal wheel event, and the second is the amount to scroll when the
1069 wheel is moved with the shift key depressed."
1070   :type '(cons (integer :tag "Shift") integer)
1071   :group 'gnus-summary-maneuvering)
1072
1073 (defcustom gnus-wheel-edge-resistance 2
1074   "How hard it should be to change the current article
1075 by moving the mouse over the edge of the article window."
1076   :type 'integer
1077   :group 'gnus-summary-maneuvering)
1078
1079 (defcustom gnus-summary-show-article-charset-alist
1080   nil
1081   "Alist of number and charset.
1082 The article will be shown with the charset corresponding to the
1083 numbered argument.
1084 For example: ((1 . cn-gb-2312) (2 . big5))."
1085   :version "21.1"
1086   :type '(repeat (cons (number :tag "Argument" 1)
1087                        (symbol :tag "Charset")))
1088   :group 'gnus-charset)
1089
1090 (defcustom gnus-preserve-marks t
1091   "Whether marks are preserved when moving, copying and respooling messages."
1092   :version "21.1"
1093   :type 'boolean
1094   :group 'gnus-summary-marks)
1095
1096 (defcustom gnus-alter-articles-to-read-function nil
1097   "Function to be called to alter the list of articles to be selected."
1098   :type '(choice (const nil) function)
1099   :group 'gnus-summary)
1100
1101 (defcustom gnus-orphan-score nil
1102   "*All orphans get this score added.  Set in the score file."
1103   :group 'gnus-score-default
1104   :type '(choice (const nil)
1105                  integer))
1106
1107 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1108   "*A regexp to match MIME parts when saving multiple parts of a
1109 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1110 This regexp will be used by default when prompting the user for which
1111 type of files to save."
1112   :group 'gnus-summary
1113   :type 'regexp)
1114
1115 (defcustom gnus-read-all-available-headers nil
1116   "Whether Gnus should parse all headers made available to it.
1117 This is mostly relevant for slow back ends where the user may
1118 wish to widen the summary buffer to include all headers
1119 that were fetched.  Say, for nnultimate groups."
1120   :group 'gnus-summary
1121   :type '(choice boolean regexp))
1122
1123 (defcustom gnus-summary-muttprint-program "muttprint"
1124   "Command (and optional arguments) used to run Muttprint."
1125   :version "21.3"
1126   :group 'gnus-summary
1127   :type 'string)
1128
1129 (defcustom gnus-article-loose-mime nil
1130   "If non-nil, don't require MIME-Version header.
1131 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1132 supply the MIME-Version header or deliberately strip it From the mail.
1133 Set it to non-nil, Gnus will treat some articles as MIME even if
1134 the MIME-Version header is missed."
1135   :version "21.3"
1136   :type 'boolean
1137   :group 'gnus-article-mime)
1138
1139 (defcustom gnus-article-emulate-mime t
1140   "If non-nil, use MIME emulation for uuencode and the like.
1141 This means that Gnus will search message bodies for text that look
1142 like uuencoded bits, yEncoded bits, and so on, and present that using
1143 the normal Gnus MIME machinery."
1144   :type 'boolean
1145   :group 'gnus-article-mime)
1146
1147 ;;; Internal variables
1148
1149 (defvar gnus-summary-display-cache nil)
1150 (defvar gnus-article-mime-handles nil)
1151 (defvar gnus-article-decoded-p nil)
1152 (defvar gnus-article-charset nil)
1153 (defvar gnus-article-ignored-charsets nil)
1154 (defvar gnus-scores-exclude-files nil)
1155 (defvar gnus-page-broken nil)
1156
1157 (defvar gnus-original-article nil)
1158 (defvar gnus-article-internal-prepare-hook nil)
1159 (defvar gnus-newsgroup-process-stack nil)
1160
1161 (defvar gnus-thread-indent-array nil)
1162 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1163 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1164   "Function called to sort the articles within a thread after it has been gathered together.")
1165
1166 (defvar gnus-summary-save-parts-type-history nil)
1167 (defvar gnus-summary-save-parts-last-directory nil)
1168
1169 ;; Avoid highlighting in kill files.
1170 (defvar gnus-summary-inhibit-highlight nil)
1171 (defvar gnus-newsgroup-selected-overlay nil)
1172 (defvar gnus-inhibit-limiting nil)
1173 (defvar gnus-newsgroup-adaptive-score-file nil)
1174 (defvar gnus-current-score-file nil)
1175 (defvar gnus-current-move-group nil)
1176 (defvar gnus-current-copy-group nil)
1177 (defvar gnus-current-crosspost-group nil)
1178 (defvar gnus-newsgroup-display nil)
1179
1180 (defvar gnus-newsgroup-dependencies nil)
1181 (defvar gnus-newsgroup-adaptive nil)
1182 (defvar gnus-summary-display-article-function nil)
1183 (defvar gnus-summary-highlight-line-function nil
1184   "Function called after highlighting a summary line.")
1185
1186 (defvar gnus-summary-line-format-alist
1187   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1188     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1189     (?s gnus-tmp-subject-or-nil ?s)
1190     (?n gnus-tmp-name ?s)
1191     (?A (std11-address-string
1192          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1193     (?a (or (std11-full-name-string
1194              (car (mime-entity-read-field gnus-tmp-header 'From)))
1195             gnus-tmp-from) ?s)
1196     (?F gnus-tmp-from ?s)
1197     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1198     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1199     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1200     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1201     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1202     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1203     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1204     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1205     (?L gnus-tmp-lines ?s)
1206     (?O gnus-tmp-downloaded ?c)
1207     (?I gnus-tmp-indentation ?s)
1208     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1209     (?R gnus-tmp-replied ?c)
1210     (?\[ gnus-tmp-opening-bracket ?c)
1211     (?\] gnus-tmp-closing-bracket ?c)
1212     (?\> (make-string gnus-tmp-level ? ) ?s)
1213     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1214     (?i gnus-tmp-score ?d)
1215     (?z gnus-tmp-score-char ?c)
1216     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1217     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1218     (?U gnus-tmp-unread ?c)
1219     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1220         ?s)
1221     (?t (gnus-summary-number-of-articles-in-thread
1222          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1223         ?d)
1224     (?e (gnus-summary-number-of-articles-in-thread
1225          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1226         ?c)
1227     (?u gnus-tmp-user-defined ?s)
1228     (?P (gnus-pick-line-number) ?d)
1229     (?B gnus-tmp-thread-tree-header-string ?s)
1230     (user-date (gnus-user-date
1231                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1232   "An alist of format specifications that can appear in summary lines.
1233 These are paired with what variables they correspond with, along with
1234 the type of the variable (string, integer, character, etc).")
1235
1236 (defvar gnus-summary-dummy-line-format-alist
1237   `((?S gnus-tmp-subject ?s)
1238     (?N gnus-tmp-number ?d)
1239     (?u gnus-tmp-user-defined ?s)))
1240
1241 (defvar gnus-summary-mode-line-format-alist
1242   `((?G gnus-tmp-group-name ?s)
1243     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1244     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1245     (?A gnus-tmp-article-number ?d)
1246     (?Z gnus-tmp-unread-and-unselected ?s)
1247     (?V gnus-version ?s)
1248     (?U gnus-tmp-unread-and-unticked ?d)
1249     (?S gnus-tmp-subject ?s)
1250     (?e gnus-tmp-unselected ?d)
1251     (?u gnus-tmp-user-defined ?s)
1252     (?d (length gnus-newsgroup-dormant) ?d)
1253     (?t (length gnus-newsgroup-marked) ?d)
1254     (?h (length gnus-newsgroup-spam-marked) ?d)
1255     (?r (length gnus-newsgroup-reads) ?d)
1256     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1257     (?E gnus-newsgroup-expunged-tally ?d)
1258     (?s (gnus-current-score-file-nondirectory) ?s)))
1259
1260 (defvar gnus-last-search-regexp nil
1261   "Default regexp for article search command.")
1262
1263 (defvar gnus-summary-search-article-matched-data nil
1264   "Last matched data of article search command.  It is the local variable
1265 in `gnus-article-buffer' which consists of the list of start position,
1266 end position and text.")
1267
1268 (defvar gnus-last-shell-command nil
1269   "Default shell command on article.")
1270
1271 (defvar gnus-newsgroup-agentized nil
1272   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1273 (defvar gnus-newsgroup-begin nil)
1274 (defvar gnus-newsgroup-end nil)
1275 (defvar gnus-newsgroup-last-rmail nil)
1276 (defvar gnus-newsgroup-last-mail nil)
1277 (defvar gnus-newsgroup-last-folder nil)
1278 (defvar gnus-newsgroup-last-file nil)
1279 (defvar gnus-newsgroup-auto-expire nil)
1280 (defvar gnus-newsgroup-active nil)
1281
1282 (defvar gnus-newsgroup-data nil)
1283 (defvar gnus-newsgroup-data-reverse nil)
1284 (defvar gnus-newsgroup-limit nil)
1285 (defvar gnus-newsgroup-limits nil)
1286 (defvar gnus-summary-use-undownloaded-faces nil)
1287
1288 (defvar gnus-newsgroup-unreads nil
1289   "Sorted list of unread articles in the current newsgroup.")
1290
1291 (defvar gnus-newsgroup-unselected nil
1292   "Sorted list of unselected unread articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-reads nil
1295   "Alist of read articles and article marks in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-expunged-tally nil)
1298
1299 (defvar gnus-newsgroup-marked nil
1300   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1301
1302 (defvar gnus-newsgroup-spam-marked nil
1303   "List of ranges of articles that have been marked as spam.")
1304
1305 (defvar gnus-newsgroup-killed nil
1306   "List of ranges of articles that have been through the scoring process.")
1307
1308 (defvar gnus-newsgroup-cached nil
1309   "Sorted list of articles that come from the article cache.")
1310
1311 (defvar gnus-newsgroup-saved nil
1312   "List of articles that have been saved.")
1313
1314 (defvar gnus-newsgroup-kill-headers nil)
1315
1316 (defvar gnus-newsgroup-replied nil
1317   "List of articles that have been replied to in the current newsgroup.")
1318
1319 (defvar gnus-newsgroup-forwarded nil
1320   "List of articles that have been forwarded in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-recent nil
1323   "List of articles that have are recent in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-expirable nil
1326   "Sorted list of articles in the current newsgroup that can be expired.")
1327
1328 (defvar gnus-newsgroup-processable nil
1329   "List of articles in the current newsgroup that can be processed.")
1330
1331 (defvar gnus-newsgroup-downloadable nil
1332   "Sorted list of articles in the current newsgroup that can be processed.")
1333
1334 (defvar gnus-newsgroup-unfetched nil
1335   "Sorted list of articles in the current newsgroup whose headers have
1336 not been fetched into the agent.
1337
1338 This list will always be a subset of gnus-newsgroup-undownloaded.")
1339
1340 (defvar gnus-newsgroup-undownloaded nil
1341   "List of articles in the current newsgroup that haven't been downloaded.")
1342
1343 (defvar gnus-newsgroup-unsendable nil
1344   "List of articles in the current newsgroup that won't be sent.")
1345
1346 (defvar gnus-newsgroup-bookmarks nil
1347   "List of articles in the current newsgroup that have bookmarks.")
1348
1349 (defvar gnus-newsgroup-dormant nil
1350   "Sorted list of dormant articles in the current newsgroup.")
1351
1352 (defvar gnus-newsgroup-unseen nil
1353   "List of unseen articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-seen nil
1356   "Range of seen articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-articles nil
1359   "List of articles in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-scored nil
1362   "List of scored articles in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-incorporated nil
1365   "List of incorporated articles in the current newsgroup.")
1366
1367 (defvar gnus-newsgroup-headers nil
1368   "List of article headers in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-threads nil)
1371
1372 (defvar gnus-newsgroup-prepared nil
1373   "Whether the current group has been prepared properly.")
1374
1375 (defvar gnus-newsgroup-ancient nil
1376   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-sparse nil)
1379
1380 (defvar gnus-current-article nil)
1381 (defvar gnus-article-current nil)
1382 (defvar gnus-current-headers nil)
1383 (defvar gnus-have-all-headers nil)
1384 (defvar gnus-last-article nil)
1385 (defvar gnus-newsgroup-history nil)
1386 (defvar gnus-newsgroup-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-charset nil)
1388 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1389
1390 (defvar gnus-article-before-search nil)
1391
1392 (defvar gnus-summary-local-variables
1393   '(gnus-newsgroup-name
1394     gnus-newsgroup-begin gnus-newsgroup-end
1395     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1396     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1397     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1398     gnus-newsgroup-unselected gnus-newsgroup-marked
1399     gnus-newsgroup-spam-marked
1400     gnus-newsgroup-reads gnus-newsgroup-saved
1401     gnus-newsgroup-replied gnus-newsgroup-forwarded
1402     gnus-newsgroup-recent
1403     gnus-newsgroup-expirable
1404     gnus-newsgroup-processable gnus-newsgroup-killed
1405     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1406     gnus-newsgroup-unfetched
1407     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1408     gnus-newsgroup-seen gnus-newsgroup-articles
1409     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1410     gnus-newsgroup-headers gnus-newsgroup-threads
1411     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1412     gnus-current-article gnus-current-headers gnus-have-all-headers
1413     gnus-last-article gnus-article-internal-prepare-hook
1414     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1415     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1416     gnus-thread-expunge-below
1417     gnus-score-alist gnus-current-score-file
1418     (gnus-summary-expunge-below . global)
1419     (gnus-summary-mark-below . global)
1420     (gnus-orphan-score . global)
1421     gnus-newsgroup-active gnus-scores-exclude-files
1422     gnus-newsgroup-history gnus-newsgroup-ancient
1423     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1424     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1425     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1426     (gnus-newsgroup-expunged-tally . 0)
1427     gnus-cache-removable-articles gnus-newsgroup-cached
1428     gnus-newsgroup-data gnus-newsgroup-data-reverse
1429     gnus-newsgroup-limit gnus-newsgroup-limits
1430     gnus-newsgroup-charset gnus-newsgroup-display
1431     gnus-summary-use-undownloaded-faces
1432     gnus-newsgroup-incorporated)
1433   "Variables that are buffer-local to the summary buffers.")
1434
1435 (defvar gnus-newsgroup-variables nil
1436   "A list of variables that have separate values in different newsgroups.
1437 A list of newsgroup (summary buffer) local variables, or cons of
1438 variables and their default values (when the default values are not
1439 nil), that should be made global while the summary buffer is active.
1440 These variables can be used to set variables in the group parameters
1441 while still allowing them to affect operations done in other
1442 buffers. For example:
1443
1444 \(setq gnus-newsgroup-variables
1445      '(message-use-followup-to
1446        (gnus-visible-headers .
1447          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1448 ")
1449
1450 ;; Byte-compiler warning.
1451 (eval-when-compile
1452   ;; Bind features so that require will believe that gnus-sum has
1453   ;; already been loaded (avoids infinite recursion)
1454   (let ((features (cons 'gnus-sum features)))
1455     ;; Several of the declarations in gnus-sum are needed to load the
1456     ;; following files. Right now, these definitions have been
1457     ;; compiled but not defined (evaluated).  We could either do a
1458     ;; eval-and-compile about all of the declarations or evaluate the
1459     ;; source file.
1460     (if (boundp 'gnus-newsgroup-variables)
1461         nil
1462       (load "gnus-sum.el" t t t))
1463     (require 'gnus)
1464     (require 'gnus-agent)
1465     (require 'gnus-art)))
1466
1467 ;; Subject simplification.
1468
1469 (defun gnus-simplify-whitespace (str)
1470   "Remove excessive whitespace from STR."
1471   ;; Multiple spaces.
1472   (while (string-match "[ \t][ \t]+" str)
1473     (setq str (concat (substring str 0 (match-beginning 0))
1474                         " "
1475                         (substring str (match-end 0)))))
1476   ;; Leading spaces.
1477   (when (string-match "^[ \t]+" str)
1478     (setq str (substring str (match-end 0))))
1479   ;; Trailing spaces.
1480   (when (string-match "[ \t]+$" str)
1481     (setq str (substring str 0 (match-beginning 0))))
1482   str)
1483
1484 (defun gnus-simplify-all-whitespace (str)
1485   "Remove all whitespace from STR."
1486   (while (string-match "[ \t\n]+" str)
1487     (setq str (replace-match "" nil nil str)))
1488   str)
1489
1490 (defsubst gnus-simplify-subject-re (subject)
1491   "Remove \"Re:\" from subject lines."
1492   (if (string-match message-subject-re-regexp subject)
1493       (substring subject (match-end 0))
1494     subject))
1495
1496 (defun gnus-simplify-subject (subject &optional re-only)
1497   "Remove `Re:' and words in parentheses.
1498 If RE-ONLY is non-nil, strip leading `Re:'s only."
1499   (let ((case-fold-search t))           ;Ignore case.
1500     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1501     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1502       (setq subject (substring subject (match-end 0))))
1503     ;; Remove uninteresting prefixes.
1504     (when (and (not re-only)
1505                gnus-simplify-ignored-prefixes
1506                (string-match gnus-simplify-ignored-prefixes subject))
1507       (setq subject (substring subject (match-end 0))))
1508     ;; Remove words in parentheses from end.
1509     (unless re-only
1510       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1511         (setq subject (substring subject 0 (match-beginning 0)))))
1512     ;; Return subject string.
1513     subject))
1514
1515 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1516 ;; all whitespace.
1517 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1518   (goto-char (point-min))
1519   (while (re-search-forward regexp nil t)
1520     (replace-match (or newtext ""))))
1521
1522 (defun gnus-simplify-buffer-fuzzy ()
1523   "Simplify string in the buffer fuzzily.
1524 The string in the accessible portion of the current buffer is simplified.
1525 It is assumed to be a single-line subject.
1526 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1527 matter is removed.  Additional things can be deleted by setting
1528 `gnus-simplify-subject-fuzzy-regexp'."
1529   (let ((case-fold-search t)
1530         (modified-tick))
1531     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1532
1533     (while (not (eq modified-tick (buffer-modified-tick)))
1534       (setq modified-tick (buffer-modified-tick))
1535       (cond
1536        ((listp gnus-simplify-subject-fuzzy-regexp)
1537         (mapcar 'gnus-simplify-buffer-fuzzy-step
1538                 gnus-simplify-subject-fuzzy-regexp))
1539        (gnus-simplify-subject-fuzzy-regexp
1540         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1541       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1542       (gnus-simplify-buffer-fuzzy-step
1543        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1544       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1545
1546     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1547     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1548     (gnus-simplify-buffer-fuzzy-step " $")
1549     (gnus-simplify-buffer-fuzzy-step "^ +")))
1550
1551 (defun gnus-simplify-subject-fuzzy (subject)
1552   "Simplify a subject string fuzzily.
1553 See `gnus-simplify-buffer-fuzzy' for details."
1554   (save-excursion
1555     (gnus-set-work-buffer)
1556     (let ((case-fold-search t))
1557       ;; Remove uninteresting prefixes.
1558       (when (and gnus-simplify-ignored-prefixes
1559                  (string-match gnus-simplify-ignored-prefixes subject))
1560         (setq subject (substring subject (match-end 0))))
1561       (insert subject)
1562       (inline (gnus-simplify-buffer-fuzzy))
1563       (buffer-string))))
1564
1565 (defsubst gnus-simplify-subject-fully (subject)
1566   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1567   (cond
1568    (gnus-simplify-subject-functions
1569     (gnus-map-function gnus-simplify-subject-functions subject))
1570    ((null gnus-summary-gather-subject-limit)
1571     (gnus-simplify-subject-re subject))
1572    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1573     (gnus-simplify-subject-fuzzy subject))
1574    ((numberp gnus-summary-gather-subject-limit)
1575     (gnus-limit-string (gnus-simplify-subject-re subject)
1576                        gnus-summary-gather-subject-limit))
1577    (t
1578     subject)))
1579
1580 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1581   "Check whether two subjects are equal.
1582 If optional argument SIMPLE-FIRST is t, first argument is already
1583 simplified."
1584   (cond
1585    ((null simple-first)
1586     (equal (gnus-simplify-subject-fully s1)
1587            (gnus-simplify-subject-fully s2)))
1588    (t
1589     (equal s1
1590            (gnus-simplify-subject-fully s2)))))
1591
1592 (defun gnus-summary-bubble-group ()
1593   "Increase the score of the current group.
1594 This is a handy function to add to `gnus-summary-exit-hook' to
1595 increase the score of each group you read."
1596   (gnus-group-add-score gnus-newsgroup-name))
1597
1598 \f
1599 ;;;
1600 ;;; Gnus summary mode
1601 ;;;
1602
1603 (put 'gnus-summary-mode 'mode-class 'special)
1604
1605 (defvar gnus-article-commands-menu)
1606
1607 ;; Non-orthogonal keys
1608
1609 (gnus-define-keys gnus-summary-mode-map
1610   " " gnus-summary-next-page
1611   "\177" gnus-summary-prev-page
1612   [delete] gnus-summary-prev-page
1613   [backspace] gnus-summary-prev-page
1614   "\r" gnus-summary-scroll-up
1615   "\M-\r" gnus-summary-scroll-down
1616   "n" gnus-summary-next-unread-article
1617   "p" gnus-summary-prev-unread-article
1618   "N" gnus-summary-next-article
1619   "P" gnus-summary-prev-article
1620   "\M-\C-n" gnus-summary-next-same-subject
1621   "\M-\C-p" gnus-summary-prev-same-subject
1622   "\M-n" gnus-summary-next-unread-subject
1623   "\M-p" gnus-summary-prev-unread-subject
1624   "." gnus-summary-first-unread-article
1625   "," gnus-summary-best-unread-article
1626   "\M-s" gnus-summary-search-article-forward
1627   "\M-r" gnus-summary-search-article-backward
1628   "<" gnus-summary-beginning-of-article
1629   ">" gnus-summary-end-of-article
1630   "j" gnus-summary-goto-article
1631   "^" gnus-summary-refer-parent-article
1632   "\M-^" gnus-summary-refer-article
1633   "u" gnus-summary-tick-article-forward
1634   "!" gnus-summary-tick-article-forward
1635   "U" gnus-summary-tick-article-backward
1636   "d" gnus-summary-mark-as-read-forward
1637   "D" gnus-summary-mark-as-read-backward
1638   "E" gnus-summary-mark-as-expirable
1639   "\M-u" gnus-summary-clear-mark-forward
1640   "\M-U" gnus-summary-clear-mark-backward
1641   "k" gnus-summary-kill-same-subject-and-select
1642   "\C-k" gnus-summary-kill-same-subject
1643   "\M-\C-k" gnus-summary-kill-thread
1644   "\M-\C-l" gnus-summary-lower-thread
1645   "e" gnus-summary-edit-article
1646   "#" gnus-summary-mark-as-processable
1647   "\M-#" gnus-summary-unmark-as-processable
1648   "\M-\C-t" gnus-summary-toggle-threads
1649   "\M-\C-s" gnus-summary-show-thread
1650   "\M-\C-h" gnus-summary-hide-thread
1651   "\M-\C-f" gnus-summary-next-thread
1652   "\M-\C-b" gnus-summary-prev-thread
1653   [(meta down)] gnus-summary-next-thread
1654   [(meta up)] gnus-summary-prev-thread
1655   "\M-\C-u" gnus-summary-up-thread
1656   "\M-\C-d" gnus-summary-down-thread
1657   "&" gnus-summary-execute-command
1658   "c" gnus-summary-catchup-and-exit
1659   "\C-w" gnus-summary-mark-region-as-read
1660   "\C-t" gnus-summary-toggle-truncation
1661   "?" gnus-summary-mark-as-dormant
1662   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1663   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1664   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1665   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1666   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1667   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1668   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1669   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1670   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1671   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1672   "=" gnus-summary-expand-window
1673   "\C-x\C-s" gnus-summary-reselect-current-group
1674   "\M-g" gnus-summary-rescan-group
1675   "w" gnus-summary-stop-page-breaking
1676   "\C-c\C-r" gnus-summary-caesar-message
1677   "\M-t" gnus-summary-toggle-mime
1678   "f" gnus-summary-followup
1679   "F" gnus-summary-followup-with-original
1680   "C" gnus-summary-cancel-article
1681   "r" gnus-summary-reply
1682   "R" gnus-summary-reply-with-original
1683   "\C-c\C-f" gnus-summary-mail-forward
1684   "o" gnus-summary-save-article
1685   "\C-o" gnus-summary-save-article-mail
1686   "|" gnus-summary-pipe-output
1687   "\M-k" gnus-summary-edit-local-kill
1688   "\M-K" gnus-summary-edit-global-kill
1689   ;; "V" gnus-version
1690   "\C-c\C-d" gnus-summary-describe-group
1691   "q" gnus-summary-exit
1692   "Q" gnus-summary-exit-no-update
1693   "\C-c\C-i" gnus-info-find-node
1694   gnus-mouse-2 gnus-mouse-pick-article
1695   "m" gnus-summary-mail-other-window
1696   "a" gnus-summary-post-news
1697   "i" gnus-summary-news-other-window
1698   "x" gnus-summary-limit-to-unread
1699   "s" gnus-summary-isearch-article
1700   "t" gnus-summary-toggle-header
1701   "g" gnus-summary-show-article
1702   "l" gnus-summary-goto-last-article
1703   "v" gnus-summary-preview-mime-message
1704   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1705   "\C-d" gnus-summary-enter-digest-group
1706   "\M-\C-d" gnus-summary-read-document
1707   "\M-\C-e" gnus-summary-edit-parameters
1708   "\M-\C-a" gnus-summary-customize-parameters
1709   "\C-c\C-b" gnus-bug
1710   "\C-c\C-n" gnus-namazu-search
1711   "*" gnus-cache-enter-article
1712   "\M-*" gnus-cache-remove-article
1713   "\M-&" gnus-summary-universal-argument
1714   "\C-l" gnus-recenter
1715   "I" gnus-summary-increase-score
1716   "L" gnus-summary-lower-score
1717   "\M-i" gnus-symbolic-argument
1718   "h" gnus-summary-select-article-buffer
1719
1720   "V" gnus-summary-score-map
1721   "X" gnus-uu-extract-map
1722   "S" gnus-summary-send-map)
1723
1724   ;; Sort of orthogonal keymap
1725 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1726   "t" gnus-summary-tick-article-forward
1727   "!" gnus-summary-tick-article-forward
1728   "d" gnus-summary-mark-as-read-forward
1729   "r" gnus-summary-mark-as-read-forward
1730   "c" gnus-summary-clear-mark-forward
1731   " " gnus-summary-clear-mark-forward
1732   "e" gnus-summary-mark-as-expirable
1733   "x" gnus-summary-mark-as-expirable
1734   "?" gnus-summary-mark-as-dormant
1735   "b" gnus-summary-set-bookmark
1736   "B" gnus-summary-remove-bookmark
1737   "#" gnus-summary-mark-as-processable
1738   "\M-#" gnus-summary-unmark-as-processable
1739   "S" gnus-summary-limit-include-expunged
1740   "C" gnus-summary-catchup
1741   "H" gnus-summary-catchup-to-here
1742   "h" gnus-summary-catchup-from-here
1743   "\C-c" gnus-summary-catchup-all
1744   "k" gnus-summary-kill-same-subject-and-select
1745   "K" gnus-summary-kill-same-subject
1746   "P" gnus-uu-mark-map)
1747
1748 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1749   "c" gnus-summary-clear-above
1750   "u" gnus-summary-tick-above
1751   "m" gnus-summary-mark-above
1752   "k" gnus-summary-kill-below)
1753
1754 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1755   "/" gnus-summary-limit-to-subject
1756   "n" gnus-summary-limit-to-articles
1757   "w" gnus-summary-pop-limit
1758   "s" gnus-summary-limit-to-subject
1759   "a" gnus-summary-limit-to-author
1760   "u" gnus-summary-limit-to-unread
1761   "m" gnus-summary-limit-to-marks
1762   "M" gnus-summary-limit-exclude-marks
1763   "v" gnus-summary-limit-to-score
1764   "*" gnus-summary-limit-include-cached
1765   "D" gnus-summary-limit-include-dormant
1766   "T" gnus-summary-limit-include-thread
1767   "d" gnus-summary-limit-exclude-dormant
1768   "t" gnus-summary-limit-to-age
1769   "." gnus-summary-limit-to-unseen
1770   "x" gnus-summary-limit-to-extra
1771   "p" gnus-summary-limit-to-display-predicate
1772   "E" gnus-summary-limit-include-expunged
1773   "c" gnus-summary-limit-exclude-childless-dormant
1774   "C" gnus-summary-limit-mark-excluded-as-read
1775   "o" gnus-summary-insert-old-articles
1776   "N" gnus-summary-insert-new-articles)
1777
1778 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1779   "n" gnus-summary-next-unread-article
1780   "p" gnus-summary-prev-unread-article
1781   "N" gnus-summary-next-article
1782   "P" gnus-summary-prev-article
1783   "\C-n" gnus-summary-next-same-subject
1784   "\C-p" gnus-summary-prev-same-subject
1785   "\M-n" gnus-summary-next-unread-subject
1786   "\M-p" gnus-summary-prev-unread-subject
1787   "f" gnus-summary-first-unread-article
1788   "b" gnus-summary-best-unread-article
1789   "j" gnus-summary-goto-article
1790   "g" gnus-summary-goto-subject
1791   "l" gnus-summary-goto-last-article
1792   "o" gnus-summary-pop-article)
1793
1794 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1795   "k" gnus-summary-kill-thread
1796   "l" gnus-summary-lower-thread
1797   "i" gnus-summary-raise-thread
1798   "T" gnus-summary-toggle-threads
1799   "t" gnus-summary-rethread-current
1800   "^" gnus-summary-reparent-thread
1801   "s" gnus-summary-show-thread
1802   "S" gnus-summary-show-all-threads
1803   "h" gnus-summary-hide-thread
1804   "H" gnus-summary-hide-all-threads
1805   "n" gnus-summary-next-thread
1806   "p" gnus-summary-prev-thread
1807   "u" gnus-summary-up-thread
1808   "o" gnus-summary-top-thread
1809   "d" gnus-summary-down-thread
1810   "#" gnus-uu-mark-thread
1811   "\M-#" gnus-uu-unmark-thread)
1812
1813 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1814   "g" gnus-summary-prepare
1815   "c" gnus-summary-insert-cached-articles
1816   "d" gnus-summary-insert-dormant-articles)
1817
1818 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1819   "c" gnus-summary-catchup-and-exit
1820   "C" gnus-summary-catchup-all-and-exit
1821   "E" gnus-summary-exit-no-update
1822   "J" gnus-summary-jump-to-other-group
1823   "Q" gnus-summary-exit
1824   "Z" gnus-summary-exit
1825   "n" gnus-summary-catchup-and-goto-next-group
1826   "R" gnus-summary-reselect-current-group
1827   "G" gnus-summary-rescan-group
1828   "N" gnus-summary-next-group
1829   "s" gnus-summary-save-newsrc
1830   "P" gnus-summary-prev-group)
1831
1832 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1833   " " gnus-summary-next-page
1834   "n" gnus-summary-next-page
1835   "\177" gnus-summary-prev-page
1836   [delete] gnus-summary-prev-page
1837   "p" gnus-summary-prev-page
1838   "\r" gnus-summary-scroll-up
1839   "\M-\r" gnus-summary-scroll-down
1840   "<" gnus-summary-beginning-of-article
1841   ">" gnus-summary-end-of-article
1842   "b" gnus-summary-beginning-of-article
1843   "e" gnus-summary-end-of-article
1844   "^" gnus-summary-refer-parent-article
1845   "r" gnus-summary-refer-parent-article
1846   "D" gnus-summary-enter-digest-group
1847   "R" gnus-summary-refer-references
1848   "T" gnus-summary-refer-thread
1849   "g" gnus-summary-show-article
1850   "s" gnus-summary-isearch-article
1851   "P" gnus-summary-print-article
1852   "M" gnus-mailing-list-insinuate
1853   "t" gnus-article-babel)
1854
1855 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1856   "b" gnus-article-add-buttons
1857   "B" gnus-article-add-buttons-to-head
1858   "o" gnus-article-treat-overstrike
1859   "e" gnus-article-emphasize
1860   "w" gnus-article-fill-cited-article
1861   "Q" gnus-article-fill-long-lines
1862   "C" gnus-article-capitalize-sentences
1863   "c" gnus-article-remove-cr
1864   "Z" gnus-article-decode-HZ
1865   "h" gnus-article-wash-html
1866   "u" gnus-article-unsplit-urls
1867   "f" gnus-article-display-x-face
1868   "l" gnus-summary-stop-page-breaking
1869   "r" gnus-summary-caesar-message
1870   "m" gnus-summary-morse-message
1871   "t" gnus-summary-toggle-header
1872   "g" gnus-treat-smiley
1873   "v" gnus-summary-verbose-headers
1874   "m" gnus-summary-toggle-mime
1875   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1876   "p" gnus-article-verify-x-pgp-sig
1877   "d" gnus-article-treat-dumbquotes)
1878
1879 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1880   ;; mnemonic: deuglif*Y*
1881   "u" gnus-article-outlook-unwrap-lines
1882   "a" gnus-article-outlook-repair-attribution
1883   "c" gnus-article-outlook-rearrange-citation
1884   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1885
1886 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1887   "a" gnus-article-hide
1888   "h" gnus-article-hide-headers
1889   "b" gnus-article-hide-boring-headers
1890   "s" gnus-article-hide-signature
1891   "c" gnus-article-hide-citation
1892   "C" gnus-article-hide-citation-in-followups
1893   "l" gnus-article-hide-list-identifiers
1894   "B" gnus-article-strip-banner
1895   "P" gnus-article-hide-pem
1896   "\C-c" gnus-article-hide-citation-maybe)
1897
1898 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1899   "a" gnus-article-highlight
1900   "h" gnus-article-highlight-headers
1901   "c" gnus-article-highlight-citation
1902   "s" gnus-article-highlight-signature)
1903
1904 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1905   "f" gnus-article-treat-fold-headers
1906   "u" gnus-article-treat-unfold-headers
1907   "n" gnus-article-treat-fold-newsgroups)
1908
1909 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1910   "x" gnus-article-display-x-face
1911   "d" gnus-article-display-face
1912   "s" gnus-treat-smiley
1913   "D" gnus-article-remove-images
1914   "f" gnus-treat-from-picon
1915   "m" gnus-treat-mail-picon
1916   "n" gnus-treat-newsgroups-picon)
1917
1918 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1919   "z" gnus-article-date-ut
1920   "u" gnus-article-date-ut
1921   "l" gnus-article-date-local
1922   "p" gnus-article-date-english
1923   "e" gnus-article-date-lapsed
1924   "o" gnus-article-date-original
1925   "i" gnus-article-date-iso8601
1926   "s" gnus-article-date-user)
1927
1928 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1929   "t" gnus-article-remove-trailing-blank-lines
1930   "l" gnus-article-strip-leading-blank-lines
1931   "m" gnus-article-strip-multiple-blank-lines
1932   "a" gnus-article-strip-blank-lines
1933   "A" gnus-article-strip-all-blank-lines
1934   "s" gnus-article-strip-leading-space
1935   "e" gnus-article-strip-trailing-space
1936   "w" gnus-article-remove-leading-whitespace)
1937
1938 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1939   "v" gnus-version
1940   "f" gnus-summary-fetch-faq
1941   "d" gnus-summary-describe-group
1942   "h" gnus-summary-describe-briefly
1943   "i" gnus-info-find-node
1944   "c" gnus-group-fetch-charter
1945   "C" gnus-group-fetch-control)
1946
1947 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1948   "e" gnus-summary-expire-articles
1949   "\M-\C-e" gnus-summary-expire-articles-now
1950   "\177" gnus-summary-delete-article
1951   [delete] gnus-summary-delete-article
1952   [backspace] gnus-summary-delete-article
1953   "m" gnus-summary-move-article
1954   "r" gnus-summary-respool-article
1955   "w" gnus-summary-edit-article
1956   "c" gnus-summary-copy-article
1957   "B" gnus-summary-crosspost-article
1958   "q" gnus-summary-respool-query
1959   "t" gnus-summary-respool-trace
1960   "i" gnus-summary-import-article
1961   "I" gnus-summary-create-article
1962   "p" gnus-summary-article-posted-p)
1963
1964 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1965   "o" gnus-summary-save-article
1966   "m" gnus-summary-save-article-mail
1967   "F" gnus-summary-write-article-file
1968   "r" gnus-summary-save-article-rmail
1969   "f" gnus-summary-save-article-file
1970   "b" gnus-summary-save-article-body-file
1971   "h" gnus-summary-save-article-folder
1972   "v" gnus-summary-save-article-vm
1973   "p" gnus-summary-pipe-output
1974   "P" gnus-summary-muttprint
1975   "s" gnus-soup-add-article)
1976
1977 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1978   "b" gnus-summary-display-buttonized
1979   "m" gnus-summary-repair-multipart
1980   "v" gnus-article-view-part
1981   "o" gnus-article-save-part
1982   "c" gnus-article-copy-part
1983   "C" gnus-article-view-part-as-charset
1984   "e" gnus-article-view-part-externally
1985   "E" gnus-article-encrypt-body
1986   "i" gnus-article-inline-part
1987   "|" gnus-article-pipe-part)
1988
1989 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1990   "p" gnus-summary-mark-as-processable
1991   "u" gnus-summary-unmark-as-processable
1992   "U" gnus-summary-unmark-all-processable
1993   "v" gnus-uu-mark-over
1994   "s" gnus-uu-mark-series
1995   "r" gnus-uu-mark-region
1996   "g" gnus-uu-unmark-region
1997   "R" gnus-uu-mark-by-regexp
1998   "G" gnus-uu-unmark-by-regexp
1999   "t" gnus-uu-mark-thread
2000   "T" gnus-uu-unmark-thread
2001   "a" gnus-uu-mark-all
2002   "b" gnus-uu-mark-buffer
2003   "S" gnus-uu-mark-sparse
2004   "k" gnus-summary-kill-process-mark
2005   "y" gnus-summary-yank-process-mark
2006   "w" gnus-summary-save-process-mark
2007   "i" gnus-uu-invert-processable)
2008
2009 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2010   ;;"x" gnus-uu-extract-any
2011   "m" gnus-summary-save-parts
2012   "u" gnus-uu-decode-uu
2013   "U" gnus-uu-decode-uu-and-save
2014   "s" gnus-uu-decode-unshar
2015   "S" gnus-uu-decode-unshar-and-save
2016   "o" gnus-uu-decode-save
2017   "O" gnus-uu-decode-save
2018   "b" gnus-uu-decode-binhex
2019   "B" gnus-uu-decode-binhex
2020   "p" gnus-uu-decode-postscript
2021   "P" gnus-uu-decode-postscript-and-save)
2022
2023 (gnus-define-keys
2024     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2025   "u" gnus-uu-decode-uu-view
2026   "U" gnus-uu-decode-uu-and-save-view
2027   "s" gnus-uu-decode-unshar-view
2028   "S" gnus-uu-decode-unshar-and-save-view
2029   "o" gnus-uu-decode-save-view
2030   "O" gnus-uu-decode-save-view
2031   "b" gnus-uu-decode-binhex-view
2032   "B" gnus-uu-decode-binhex-view
2033   "p" gnus-uu-decode-postscript-view
2034   "P" gnus-uu-decode-postscript-and-save-view)
2035
2036 (defvar gnus-article-post-menu nil)
2037
2038 (defconst gnus-summary-menu-maxlen 20)
2039
2040 (defun gnus-summary-menu-split (menu)
2041   ;; If we have lots of elements, divide them into groups of 20
2042   ;; and make a pane (or submenu) for each one.
2043   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2044       (let ((menu menu) sublists next
2045             (i 1))
2046         (while menu
2047           ;; Pull off the next gnus-summary-menu-maxlen elements
2048           ;; and make them the next element of sublist.
2049           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2050           (if next
2051               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2052                       nil))
2053           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2054                                              (aref (car (last menu)) 0)) menu)
2055                                sublists))
2056           (setq i (1+ i))
2057           (setq menu next))
2058         (nreverse sublists))
2059     ;; Few elements--put them all in one pane.
2060     menu))
2061
2062 (defun gnus-summary-make-menu-bar ()
2063   (gnus-turn-off-edit-menu 'summary)
2064
2065   (unless (boundp 'gnus-summary-misc-menu)
2066
2067     (easy-menu-define
2068      gnus-summary-kill-menu gnus-summary-mode-map ""
2069      (cons
2070       "Score"
2071       (nconc
2072        (list
2073         ["Customize" gnus-score-customize t])
2074        (gnus-make-score-map 'increase)
2075        (gnus-make-score-map 'lower)
2076        '(("Mark"
2077           ["Kill below" gnus-summary-kill-below t]
2078           ["Mark above" gnus-summary-mark-above t]
2079           ["Tick above" gnus-summary-tick-above t]
2080           ["Clear above" gnus-summary-clear-above t])
2081          ["Current score" gnus-summary-current-score t]
2082          ["Set score" gnus-summary-set-score t]
2083          ["Switch current score file..." gnus-score-change-score-file t]
2084          ["Set mark below..." gnus-score-set-mark-below t]
2085          ["Set expunge below..." gnus-score-set-expunge-below t]
2086          ["Edit current score file" gnus-score-edit-current-scores t]
2087          ["Edit score file" gnus-score-edit-file t]
2088          ["Trace score" gnus-score-find-trace t]
2089          ["Find words" gnus-score-find-favourite-words t]
2090          ["Rescore buffer" gnus-summary-rescore t]
2091          ["Increase score..." gnus-summary-increase-score t]
2092          ["Lower score..." gnus-summary-lower-score t]))))
2093
2094     ;; Define both the Article menu in the summary buffer and the
2095     ;; equivalent Commands menu in the article buffer here for
2096     ;; consistency.
2097     (let ((innards
2098            `(("Hide"
2099               ["All" gnus-article-hide t]
2100               ["Headers" gnus-article-hide-headers t]
2101               ["Signature" gnus-article-hide-signature t]
2102               ["Citation" gnus-article-hide-citation t]
2103               ["List identifiers" gnus-article-hide-list-identifiers t]
2104               ["Banner" gnus-article-strip-banner t]
2105               ["Boring headers" gnus-article-hide-boring-headers t])
2106              ("Highlight"
2107               ["All" gnus-article-highlight t]
2108               ["Headers" gnus-article-highlight-headers t]
2109               ["Signature" gnus-article-highlight-signature t]
2110               ["Citation" gnus-article-highlight-citation t])
2111              ("Date"
2112               ["Local" gnus-article-date-local t]
2113               ["ISO8601" gnus-article-date-iso8601 t]
2114               ["UT" gnus-article-date-ut t]
2115               ["Original" gnus-article-date-original t]
2116               ["Lapsed" gnus-article-date-lapsed t]
2117               ["User-defined" gnus-article-date-user t])
2118              ("Display"
2119               ["Remove images" gnus-article-remove-images t]
2120               ["Toggle smiley" gnus-treat-smiley t]
2121               ["Show X-Face" gnus-article-display-x-face t]
2122               ["Show picons in From" gnus-treat-from-picon t]
2123               ["Show picons in mail headers" gnus-treat-mail-picon t]
2124               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2125               ("View as different encoding"
2126                ,@(gnus-summary-menu-split
2127                   (mapcar
2128                    (lambda (cs)
2129                      ;; Since easymenu under Emacs doesn't allow
2130                      ;; lambda forms for menu commands, we should
2131                      ;; provide intern'ed function symbols.
2132                      (let ((command (intern (format "\
2133 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2134                        (fset command
2135                              `(lambda ()
2136                                 (interactive)
2137                                 (let ((gnus-summary-show-article-charset-alist
2138                                        '((1 . ,cs))))
2139                                   (gnus-summary-show-article 1))))
2140                        `[,(symbol-name cs) ,command t]))
2141                    (sort (if (fboundp 'coding-system-list)
2142                              (coding-system-list)
2143                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2144                            )
2145                          'string<)))))
2146              ("Washing"
2147               ("Remove Blanks"
2148                ["Leading" gnus-article-strip-leading-blank-lines t]
2149                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2150                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2151                ["All of the above" gnus-article-strip-blank-lines t]
2152                ["All" gnus-article-strip-all-blank-lines t]
2153                ["Leading space" gnus-article-strip-leading-space t]
2154                ["Trailing space" gnus-article-strip-trailing-space t]
2155                ["Leading space in headers"
2156                 gnus-article-remove-leading-whitespace t])
2157               ["Overstrike" gnus-article-treat-overstrike t]
2158               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2159               ["Emphasis" gnus-article-emphasize t]
2160               ["Word wrap" gnus-article-fill-cited-article t]
2161               ["Fill long lines" gnus-article-fill-long-lines t]
2162               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2163               ["CR" gnus-article-remove-cr t]
2164               ["Rot 13" gnus-summary-caesar-message
2165                ,@(if (featurep 'xemacs) '(t)
2166                    '(:help "\"Caesar rotate\" article by 13"))]
2167               ["Morse decode" gnus-summary-morse-message t]
2168               ["Unix pipe..." gnus-summary-pipe-message t]
2169               ["Add buttons" gnus-article-add-buttons t]
2170               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2171               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2172               ["Toggle MIME" gnus-summary-toggle-mime t]
2173               ["Verbose header" gnus-summary-verbose-headers t]
2174               ["Toggle header" gnus-summary-toggle-header t]
2175               ["Unfold headers" gnus-article-treat-unfold-headers t]
2176               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2177               ["Html" gnus-article-wash-html t]
2178               ["URLs" gnus-article-unsplit-urls t]
2179               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2180               ["HZ" gnus-article-decode-HZ t]
2181               ("(Outlook) Deuglify"
2182                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2183                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2184                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2185                ["Full (Outlook) deuglify"
2186                 gnus-article-outlook-deuglify-article t])
2187               )
2188              ("Output"
2189               ["Save in default format" gnus-summary-save-article
2190                ,@(if (featurep 'xemacs) '(t)
2191                    '(:help "Save article using default method"))]
2192               ["Save in file" gnus-summary-save-article-file
2193                ,@(if (featurep 'xemacs) '(t)
2194                    '(:help "Save article in file"))]
2195               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2196               ["Save in MH folder" gnus-summary-save-article-folder t]
2197               ["Save in VM folder" gnus-summary-save-article-vm t]
2198               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2199               ["Save body in file" gnus-summary-save-article-body-file t]
2200               ["Pipe through a filter" gnus-summary-pipe-output t]
2201               ["Add to SOUP packet" gnus-soup-add-article t]
2202               ["Print with Muttprint" gnus-summary-muttprint t]
2203               ["Print" gnus-summary-print-article t])
2204              ("Backend"
2205               ["Respool article..." gnus-summary-respool-article t]
2206               ["Move article..." gnus-summary-move-article
2207                (gnus-check-backend-function
2208                 'request-move-article gnus-newsgroup-name)]
2209               ["Copy article..." gnus-summary-copy-article t]
2210               ["Crosspost article..." gnus-summary-crosspost-article
2211                (gnus-check-backend-function
2212                 'request-replace-article gnus-newsgroup-name)]
2213               ["Import file..." gnus-summary-import-article
2214                (gnus-check-backend-function
2215                 'request-accept-article gnus-newsgroup-name)]
2216               ["Create article..." gnus-summary-create-article
2217                (gnus-check-backend-function
2218                 'request-accept-article gnus-newsgroup-name)]
2219               ["Check if posted" gnus-summary-article-posted-p t]
2220               ["Edit article" gnus-summary-edit-article
2221                (not (gnus-group-read-only-p))]
2222               ["Delete article" gnus-summary-delete-article
2223                (gnus-check-backend-function
2224                 'request-expire-articles gnus-newsgroup-name)]
2225               ["Query respool" gnus-summary-respool-query t]
2226               ["Trace respool" gnus-summary-respool-trace t]
2227               ["Delete expirable articles" gnus-summary-expire-articles-now
2228                (gnus-check-backend-function
2229                 'request-expire-articles gnus-newsgroup-name)])
2230              ("Extract"
2231               ["Uudecode" gnus-uu-decode-uu
2232                ,@(if (featurep 'xemacs) '(t)
2233                    '(:help "Decode uuencoded article(s)"))]
2234               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2235               ["Unshar" gnus-uu-decode-unshar t]
2236               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2237               ["Save" gnus-uu-decode-save t]
2238               ["Binhex" gnus-uu-decode-binhex t]
2239               ["Postscript" gnus-uu-decode-postscript t])
2240              ("Cache"
2241               ["Enter article" gnus-cache-enter-article t]
2242               ["Remove article" gnus-cache-remove-article t])
2243              ["Translate" gnus-article-babel t]
2244              ["Select article buffer" gnus-summary-select-article-buffer t]
2245              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2246              ["Isearch article..." gnus-summary-isearch-article t]
2247              ["Beginning of the article" gnus-summary-beginning-of-article t]
2248              ["End of the article" gnus-summary-end-of-article t]
2249              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2250              ["Fetch referenced articles" gnus-summary-refer-references t]
2251              ["Fetch current thread" gnus-summary-refer-thread t]
2252              ["Fetch article with id..." gnus-summary-refer-article t]
2253              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2254              ["Redisplay" gnus-summary-show-article t]
2255              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2256       (easy-menu-define
2257        gnus-summary-article-menu gnus-summary-mode-map ""
2258        (cons "Article" innards))
2259
2260       (if (not (keymapp gnus-summary-article-menu))
2261           (easy-menu-define
2262            gnus-article-commands-menu gnus-article-mode-map ""
2263            (cons "Commands" innards))
2264         ;; in Emacs, don't share menu.
2265         (setq gnus-article-commands-menu
2266               (copy-keymap gnus-summary-article-menu))
2267         (define-key gnus-article-mode-map [menu-bar commands]
2268           (cons "Commands" gnus-article-commands-menu))))
2269
2270     (easy-menu-define
2271      gnus-summary-thread-menu gnus-summary-mode-map ""
2272      '("Threads"
2273        ["Find all messages in thread" gnus-summary-refer-thread t]
2274        ["Toggle threading" gnus-summary-toggle-threads t]
2275        ["Hide threads" gnus-summary-hide-all-threads t]
2276        ["Show threads" gnus-summary-show-all-threads t]
2277        ["Hide thread" gnus-summary-hide-thread t]
2278        ["Show thread" gnus-summary-show-thread t]
2279        ["Go to next thread" gnus-summary-next-thread t]
2280        ["Go to previous thread" gnus-summary-prev-thread t]
2281        ["Go down thread" gnus-summary-down-thread t]
2282        ["Go up thread" gnus-summary-up-thread t]
2283        ["Top of thread" gnus-summary-top-thread t]
2284        ["Mark thread as read" gnus-summary-kill-thread t]
2285        ["Lower thread score" gnus-summary-lower-thread t]
2286        ["Raise thread score" gnus-summary-raise-thread t]
2287        ["Rethread current" gnus-summary-rethread-current t]))
2288
2289     (easy-menu-define
2290      gnus-summary-post-menu gnus-summary-mode-map ""
2291      `("Post"
2292        ["Send a message (mail or news)" gnus-summary-post-news
2293         ,@(if (featurep 'xemacs) '(t)
2294             '(:help "Post an article"))]
2295        ["Followup" gnus-summary-followup
2296         ,@(if (featurep 'xemacs) '(t)
2297             '(:help "Post followup to this article"))]
2298        ["Followup and yank" gnus-summary-followup-with-original
2299         ,@(if (featurep 'xemacs) '(t)
2300             '(:help "Post followup to this article, quoting its contents"))]
2301        ["Supersede article" gnus-summary-supersede-article t]
2302        ["Cancel article" gnus-summary-cancel-article
2303         ,@(if (featurep 'xemacs) '(t)
2304             '(:help "Cancel an article you posted"))]
2305        ["Reply" gnus-summary-reply t]
2306        ["Reply and yank" gnus-summary-reply-with-original t]
2307        ["Wide reply" gnus-summary-wide-reply t]
2308        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2309         ,@(if (featurep 'xemacs) '(t)
2310             '(:help "Mail a reply, quoting this article"))]
2311        ["Very wide reply" gnus-summary-very-wide-reply t]
2312        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2313         ,@(if (featurep 'xemacs) '(t)
2314             '(:help "Mail a very wide reply, quoting this article"))]
2315        ["Mail forward" gnus-summary-mail-forward t]
2316        ["Post forward" gnus-summary-post-forward t]
2317        ["Digest and mail" gnus-summary-digest-mail-forward t]
2318        ["Digest and post" gnus-summary-digest-post-forward t]
2319        ["Resend message" gnus-summary-resend-message t]
2320        ["Resend message edit" gnus-summary-resend-message-edit t]
2321        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2322        ["Send a mail" gnus-summary-mail-other-window t]
2323        ["Create a local message" gnus-summary-news-other-window t]
2324        ["Uuencode and post" gnus-uu-post-news
2325         ,@(if (featurep 'xemacs) '(t)
2326             '(:help "Post a uuencoded article"))]
2327        ["Followup via news" gnus-summary-followup-to-mail t]
2328        ["Followup via news and yank"
2329         gnus-summary-followup-to-mail-with-original t]
2330        ;;("Draft"
2331        ;;["Send" gnus-summary-send-draft t]
2332        ;;["Send bounced" gnus-resend-bounced-mail t])
2333        ))
2334
2335     (cond
2336      ((not (keymapp gnus-summary-post-menu))
2337       (setq gnus-article-post-menu gnus-summary-post-menu))
2338      ((not gnus-article-post-menu)
2339       ;; Don't share post menu.
2340       (setq gnus-article-post-menu
2341             (copy-keymap gnus-summary-post-menu))))
2342     (define-key gnus-article-mode-map [menu-bar post]
2343       (cons "Post" gnus-article-post-menu))
2344
2345     (easy-menu-define
2346      gnus-summary-misc-menu gnus-summary-mode-map ""
2347      `("Gnus"
2348        ("Mark Read"
2349         ["Mark as read" gnus-summary-mark-as-read-forward t]
2350         ["Mark same subject and select"
2351          gnus-summary-kill-same-subject-and-select t]
2352         ["Mark same subject" gnus-summary-kill-same-subject t]
2353         ["Catchup" gnus-summary-catchup
2354          ,@(if (featurep 'xemacs) '(t)
2355              '(:help "Mark unread articles in this group as read"))]
2356         ["Catchup all" gnus-summary-catchup-all t]
2357         ["Catchup to here" gnus-summary-catchup-to-here t]
2358         ["Catchup from here" gnus-summary-catchup-from-here t]
2359         ["Catchup region" gnus-summary-mark-region-as-read
2360          (gnus-mark-active-p)]
2361         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2362        ("Mark Various"
2363         ["Tick" gnus-summary-tick-article-forward t]
2364         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2365         ["Remove marks" gnus-summary-clear-mark-forward t]
2366         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2367         ["Set bookmark" gnus-summary-set-bookmark t]
2368         ["Remove bookmark" gnus-summary-remove-bookmark t])
2369        ("Limit to"
2370         ["Marks..." gnus-summary-limit-to-marks t]
2371         ["Subject..." gnus-summary-limit-to-subject t]
2372         ["Author..." gnus-summary-limit-to-author t]
2373         ["Age..." gnus-summary-limit-to-age t]
2374         ["Extra..." gnus-summary-limit-to-extra t]
2375         ["Score..." gnus-summary-limit-to-score t]
2376         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2377         ["Unread" gnus-summary-limit-to-unread t]
2378         ["Unseen" gnus-summary-limit-to-unseen t]
2379         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2380         ["Next articles" gnus-summary-limit-to-articles t]
2381         ["Pop limit" gnus-summary-pop-limit t]
2382         ["Show dormant" gnus-summary-limit-include-dormant t]
2383         ["Hide childless dormant"
2384          gnus-summary-limit-exclude-childless-dormant t]
2385         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2386         ["Hide marked" gnus-summary-limit-exclude-marks t]
2387         ["Show expunged" gnus-summary-limit-include-expunged t])
2388        ("Process Mark"
2389         ["Set mark" gnus-summary-mark-as-processable t]
2390         ["Remove mark" gnus-summary-unmark-as-processable t]
2391         ["Remove all marks" gnus-summary-unmark-all-processable t]
2392         ["Mark above" gnus-uu-mark-over t]
2393         ["Mark series" gnus-uu-mark-series t]
2394         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2395         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2396         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2397         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2398         ["Mark all" gnus-uu-mark-all t]
2399         ["Mark buffer" gnus-uu-mark-buffer t]
2400         ["Mark sparse" gnus-uu-mark-sparse t]
2401         ["Mark thread" gnus-uu-mark-thread t]
2402         ["Unmark thread" gnus-uu-unmark-thread t]
2403         ("Process Mark Sets"
2404          ["Kill" gnus-summary-kill-process-mark t]
2405          ["Yank" gnus-summary-yank-process-mark
2406           gnus-newsgroup-process-stack]
2407          ["Save" gnus-summary-save-process-mark t]
2408          ["Run command on marked..." gnus-summary-universal-argument t]))
2409        ("Scroll article"
2410         ["Page forward" gnus-summary-next-page
2411          ,@(if (featurep 'xemacs) '(t)
2412              '(:help "Show next page of article"))]
2413         ["Page backward" gnus-summary-prev-page
2414          ,@(if (featurep 'xemacs) '(t)
2415              '(:help "Show previous page of article"))]
2416         ["Line forward" gnus-summary-scroll-up t])
2417        ("Move"
2418         ["Next unread article" gnus-summary-next-unread-article t]
2419         ["Previous unread article" gnus-summary-prev-unread-article t]
2420         ["Next article" gnus-summary-next-article t]
2421         ["Previous article" gnus-summary-prev-article t]
2422         ["Next unread subject" gnus-summary-next-unread-subject t]
2423         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2424         ["Next article same subject" gnus-summary-next-same-subject t]
2425         ["Previous article same subject" gnus-summary-prev-same-subject t]
2426         ["First unread article" gnus-summary-first-unread-article t]
2427         ["Best unread article" gnus-summary-best-unread-article t]
2428         ["Go to subject number..." gnus-summary-goto-subject t]
2429         ["Go to article number..." gnus-summary-goto-article t]
2430         ["Go to the last article" gnus-summary-goto-last-article t]
2431         ["Pop article off history" gnus-summary-pop-article t])
2432        ("Sort"
2433         ["Sort by number" gnus-summary-sort-by-number t]
2434         ["Sort by author" gnus-summary-sort-by-author t]
2435         ["Sort by subject" gnus-summary-sort-by-subject t]
2436         ["Sort by date" gnus-summary-sort-by-date t]
2437         ["Sort by score" gnus-summary-sort-by-score t]
2438         ["Sort by lines" gnus-summary-sort-by-lines t]
2439         ["Sort by characters" gnus-summary-sort-by-chars t]
2440         ["Randomize" gnus-summary-sort-by-random t]
2441         ["Original sort" gnus-summary-sort-by-original t])
2442        ("Help"
2443         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2444         ["Describe group" gnus-summary-describe-group t]
2445         ["Fetch charter" gnus-group-fetch-charter
2446          ,@(if (featurep 'xemacs) nil
2447              '(:help "Display the charter of the current group"))]
2448         ["Fetch control message" gnus-group-fetch-control
2449          ,@(if (featurep 'xemacs) nil
2450              '(:help "Display the archived control message for the current group"))]
2451         ["Read manual" gnus-info-find-node t])
2452        ("Modes"
2453         ["Pick and read" gnus-pick-mode t]
2454         ["Binary" gnus-binary-mode t])
2455        ("Regeneration"
2456         ["Regenerate" gnus-summary-prepare t]
2457         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2458         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2459         ["Toggle threading" gnus-summary-toggle-threads t])
2460        ["See old articles" gnus-summary-insert-old-articles t]
2461        ["See new articles" gnus-summary-insert-new-articles t]
2462        ["Filter articles..." gnus-summary-execute-command t]
2463        ["Run command on articles..." gnus-summary-universal-argument t]
2464        ["Search articles forward..." gnus-summary-search-article-forward t]
2465        ["Search articles backward..." gnus-summary-search-article-backward t]
2466        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2467        ["Expand window" gnus-summary-expand-window t]
2468        ["Expire expirable articles" gnus-summary-expire-articles
2469         (gnus-check-backend-function
2470          'request-expire-articles gnus-newsgroup-name)]
2471        ["Edit local kill file" gnus-summary-edit-local-kill t]
2472        ["Edit main kill file" gnus-summary-edit-global-kill t]
2473        ["Edit group parameters" gnus-summary-edit-parameters t]
2474        ["Customize group parameters" gnus-summary-customize-parameters t]
2475        ["Send a bug report" gnus-bug t]
2476        ("Exit"
2477         ["Catchup and exit" gnus-summary-catchup-and-exit
2478          ,@(if (featurep 'xemacs) '(t)
2479              '(:help "Mark unread articles in this group as read, then exit"))]
2480         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2481         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2482         ["Exit group" gnus-summary-exit
2483          ,@(if (featurep 'xemacs) '(t)
2484              '(:help "Exit current group, return to group selection mode"))]
2485         ["Exit group without updating" gnus-summary-exit-no-update t]
2486         ["Exit and goto next group" gnus-summary-next-group t]
2487         ["Exit and goto prev group" gnus-summary-prev-group t]
2488         ["Reselect group" gnus-summary-reselect-current-group t]
2489         ["Rescan group" gnus-summary-rescan-group t]
2490         ["Update dribble" gnus-summary-save-newsrc t])))
2491
2492     (gnus-run-hooks 'gnus-summary-menu-hook)))
2493
2494 (defvar gnus-summary-tool-bar-map nil)
2495
2496 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2497 (defun gnus-summary-make-tool-bar ()
2498   (if (and (fboundp 'tool-bar-add-item-from-menu)
2499            (default-value 'tool-bar-mode)
2500            (not gnus-summary-tool-bar-map))
2501       (setq gnus-summary-tool-bar-map
2502             (let ((tool-bar-map (make-sparse-keymap))
2503                   (load-path (mm-image-load-path)))
2504               (tool-bar-add-item-from-menu
2505                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2506               (tool-bar-add-item-from-menu
2507                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2508               (tool-bar-add-item-from-menu
2509                'gnus-summary-post-news "post" gnus-summary-mode-map)
2510               (tool-bar-add-item-from-menu
2511                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2512               (tool-bar-add-item-from-menu
2513                'gnus-summary-followup "followup" gnus-summary-mode-map)
2514               (tool-bar-add-item-from-menu
2515                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-reply "reply" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2534               tool-bar-map)))
2535   (if gnus-summary-tool-bar-map
2536       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2537
2538 (defun gnus-score-set-default (var value)
2539   "A version of set that updates the GNU Emacs menu-bar."
2540   (set var value)
2541   ;; It is the message that forces the active status to be updated.
2542   (message ""))
2543
2544 (defun gnus-make-score-map (type)
2545   "Make a summary score map of type TYPE."
2546   (if t
2547       nil
2548     (let ((headers '(("author" "from" string)
2549                      ("subject" "subject" string)
2550                      ("article body" "body" string)
2551                      ("article head" "head" string)
2552                      ("xref" "xref" string)
2553                      ("extra header" "extra" string)
2554                      ("lines" "lines" number)
2555                      ("followups to author" "followup" string)))
2556           (types '((number ("less than" <)
2557                            ("greater than" >)
2558                            ("equal" =))
2559                    (string ("substring" s)
2560                            ("exact string" e)
2561                            ("fuzzy string" f)
2562                            ("regexp" r))))
2563           (perms '(("temporary" (current-time-string))
2564                    ("permanent" nil)
2565                    ("immediate" now)))
2566           header)
2567       (list
2568        (apply
2569         'nconc
2570         (list
2571          (if (eq type 'lower)
2572              "Lower score"
2573            "Increase score"))
2574         (let (outh)
2575           (while headers
2576             (setq header (car headers))
2577             (setq outh
2578                   (cons
2579                    (apply
2580                     'nconc
2581                     (list (car header))
2582                     (let ((ts (cdr (assoc (nth 2 header) types)))
2583                           outt)
2584                       (while ts
2585                         (setq outt
2586                               (cons
2587                                (apply
2588                                 'nconc
2589                                 (list (caar ts))
2590                                 (let ((ps perms)
2591                                       outp)
2592                                   (while ps
2593                                     (setq outp
2594                                           (cons
2595                                            (vector
2596                                             (caar ps)
2597                                             (list
2598                                              'gnus-summary-score-entry
2599                                              (nth 1 header)
2600                                              (if (or (string= (nth 1 header)
2601                                                               "head")
2602                                                      (string= (nth 1 header)
2603                                                               "body"))
2604                                                  ""
2605                                                (list 'gnus-summary-header
2606                                                      (nth 1 header)))
2607                                              (list 'quote (nth 1 (car ts)))
2608                                              (list 'gnus-score-delta-default
2609                                                    nil)
2610                                              (nth 1 (car ps))
2611                                              t)
2612                                             t)
2613                                            outp))
2614                                     (setq ps (cdr ps)))
2615                                   (list (nreverse outp))))
2616                                outt))
2617                         (setq ts (cdr ts)))
2618                       (list (nreverse outt))))
2619                    outh))
2620             (setq headers (cdr headers)))
2621           (list (nreverse outh))))))))
2622
2623 \f
2624
2625 (defun gnus-summary-mode (&optional group)
2626   "Major mode for reading articles.
2627
2628 All normal editing commands are switched off.
2629 \\<gnus-summary-mode-map>
2630 Each line in this buffer represents one article.  To read an
2631 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2632 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2633 respectively.
2634
2635 You can also post articles and send mail from this buffer.  To
2636 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2637 of an article, type `\\[gnus-summary-reply]'.
2638
2639 There are approx. one gazillion commands you can execute in this
2640 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2641
2642 The following commands are available:
2643
2644 \\{gnus-summary-mode-map}"
2645   (interactive)
2646   (kill-all-local-variables)
2647   (when (gnus-visual-p 'summary-menu 'menu)
2648     (gnus-summary-make-menu-bar)
2649     (gnus-summary-make-tool-bar))
2650   (gnus-summary-make-local-variables)
2651   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2652     (gnus-summary-make-local-variables))
2653   (gnus-make-thread-indent-array)
2654   (gnus-simplify-mode-line)
2655   (setq major-mode 'gnus-summary-mode)
2656   (setq mode-name "Summary")
2657   (make-local-variable 'minor-mode-alist)
2658   (use-local-map gnus-summary-mode-map)
2659   (buffer-disable-undo)
2660   (setq buffer-read-only t)             ;Disable modification
2661   (setq truncate-lines t)
2662   (setq selective-display t)
2663   (setq selective-display-ellipses t)   ;Display `...'
2664   (gnus-summary-set-display-table)
2665   (gnus-set-default-directory)
2666   (setq gnus-newsgroup-name group)
2667   (unless (gnus-news-group-p group)
2668     (setq gnus-newsgroup-incorporated
2669           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2670   (make-local-variable 'gnus-summary-line-format)
2671   (make-local-variable 'gnus-summary-line-format-spec)
2672   (make-local-variable 'gnus-summary-dummy-line-format)
2673   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2674   (make-local-variable 'gnus-summary-mark-positions)
2675   (gnus-make-local-hook 'pre-command-hook)
2676   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2677   (gnus-run-hooks 'gnus-summary-mode-hook)
2678   (turn-on-gnus-mailing-list-mode)
2679   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2680   (gnus-update-summary-mark-positions))
2681
2682 (defun gnus-summary-make-local-variables ()
2683   "Make all the local summary buffer variables."
2684   (let (global)
2685     (dolist (local gnus-summary-local-variables)
2686       (if (consp local)
2687           (progn
2688             (if (eq (cdr local) 'global)
2689                 ;; Copy the global value of the variable.
2690                 (setq global (symbol-value (car local)))
2691               ;; Use the value from the list.
2692               (setq global (eval (cdr local))))
2693             (set (make-local-variable (car local)) global))
2694         ;; Simple nil-valued local variable.
2695         (set (make-local-variable local) nil)))))
2696
2697 (defun gnus-summary-clear-local-variables ()
2698   (let ((locals gnus-summary-local-variables))
2699     (while locals
2700       (if (consp (car locals))
2701           (and (vectorp (caar locals))
2702                (set (caar locals) nil))
2703         (and (vectorp (car locals))
2704              (set (car locals) nil)))
2705       (setq locals (cdr locals)))))
2706
2707 ;; Summary data functions.
2708
2709 (defmacro gnus-data-number (data)
2710   `(car ,data))
2711
2712 (defmacro gnus-data-set-number (data number)
2713   `(setcar ,data ,number))
2714
2715 (defmacro gnus-data-mark (data)
2716   `(nth 1 ,data))
2717
2718 (defmacro gnus-data-set-mark (data mark)
2719   `(setcar (nthcdr 1 ,data) ,mark))
2720
2721 (defmacro gnus-data-pos (data)
2722   `(nth 2 ,data))
2723
2724 (defmacro gnus-data-set-pos (data pos)
2725   `(setcar (nthcdr 2 ,data) ,pos))
2726
2727 (defmacro gnus-data-header (data)
2728   `(nth 3 ,data))
2729
2730 (defmacro gnus-data-set-header (data header)
2731   `(setcar (nthcdr 3 ,data) ,header))
2732
2733 (defmacro gnus-data-level (data)
2734   `(nth 4 ,data))
2735
2736 (defmacro gnus-data-unread-p (data)
2737   `(= (nth 1 ,data) gnus-unread-mark))
2738
2739 (defmacro gnus-data-read-p (data)
2740   `(/= (nth 1 ,data) gnus-unread-mark))
2741
2742 (defmacro gnus-data-pseudo-p (data)
2743   `(consp (nth 3 ,data)))
2744
2745 (defmacro gnus-data-find (number)
2746   `(assq ,number gnus-newsgroup-data))
2747
2748 (defmacro gnus-data-find-list (number &optional data)
2749   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2750      (memq (assq ,number bdata)
2751            bdata)))
2752
2753 (defmacro gnus-data-make (number mark pos header level)
2754   `(list ,number ,mark ,pos ,header ,level))
2755
2756 (defun gnus-data-enter (after-article number mark pos header level offset)
2757   (let ((data (gnus-data-find-list after-article)))
2758     (unless data
2759       (error "No such article: %d" after-article))
2760     (setcdr data (cons (gnus-data-make number mark pos header level)
2761                        (cdr data)))
2762     (setq gnus-newsgroup-data-reverse nil)
2763     (gnus-data-update-list (cddr data) offset)))
2764
2765 (defun gnus-data-enter-list (after-article list &optional offset)
2766   (when list
2767     (let ((data (and after-article (gnus-data-find-list after-article)))
2768           (ilist list))
2769       (if (not (or data
2770                    after-article))
2771           (let ((odata gnus-newsgroup-data))
2772             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2773             (when offset
2774               (gnus-data-update-list odata offset)))
2775         ;; Find the last element in the list to be spliced into the main
2776         ;; list.
2777         (while (cdr list)
2778           (setq list (cdr list)))
2779         (if (not data)
2780             (progn
2781               (setcdr list gnus-newsgroup-data)
2782               (setq gnus-newsgroup-data ilist)
2783               (when offset
2784                 (gnus-data-update-list (cdr list) offset)))
2785           (setcdr list (cdr data))
2786           (setcdr data ilist)
2787           (when offset
2788             (gnus-data-update-list (cdr list) offset))))
2789       (setq gnus-newsgroup-data-reverse nil))))
2790
2791 (defun gnus-data-remove (article &optional offset)
2792   (let ((data gnus-newsgroup-data))
2793     (if (= (gnus-data-number (car data)) article)
2794         (progn
2795           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2796                 gnus-newsgroup-data-reverse nil)
2797           (when offset
2798             (gnus-data-update-list gnus-newsgroup-data offset)))
2799       (while (cdr data)
2800         (when (= (gnus-data-number (cadr data)) article)
2801           (setcdr data (cddr data))
2802           (when offset
2803             (gnus-data-update-list (cdr data) offset))
2804           (setq data nil
2805                 gnus-newsgroup-data-reverse nil))
2806         (setq data (cdr data))))))
2807
2808 (defmacro gnus-data-list (backward)
2809   `(if ,backward
2810        (or gnus-newsgroup-data-reverse
2811            (setq gnus-newsgroup-data-reverse
2812                  (reverse gnus-newsgroup-data)))
2813      gnus-newsgroup-data))
2814
2815 (defun gnus-data-update-list (data offset)
2816   "Add OFFSET to the POS of all data entries in DATA."
2817   (setq gnus-newsgroup-data-reverse nil)
2818   (while data
2819     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2820     (setq data (cdr data))))
2821
2822 (defun gnus-summary-article-pseudo-p (article)
2823   "Say whether this article is a pseudo article or not."
2824   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2825
2826 (defmacro gnus-summary-article-sparse-p (article)
2827   "Say whether this article is a sparse article or not."
2828   `(memq ,article gnus-newsgroup-sparse))
2829
2830 (defmacro gnus-summary-article-ancient-p (article)
2831   "Say whether this article is a sparse article or not."
2832   `(memq ,article gnus-newsgroup-ancient))
2833
2834 (defun gnus-article-parent-p (number)
2835   "Say whether this article is a parent or not."
2836   (let ((data (gnus-data-find-list number)))
2837     (and (cdr data)                     ; There has to be an article after...
2838          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2839             (gnus-data-level (nth 1 data))))))
2840
2841 (defun gnus-article-children (number)
2842   "Return a list of all children to NUMBER."
2843   (let* ((data (gnus-data-find-list number))
2844          (level (gnus-data-level (car data)))
2845          children)
2846     (setq data (cdr data))
2847     (while (and data
2848                 (= (gnus-data-level (car data)) (1+ level)))
2849       (push (gnus-data-number (car data)) children)
2850       (setq data (cdr data)))
2851     children))
2852
2853 (defmacro gnus-summary-skip-intangible ()
2854   "If the current article is intangible, then jump to a different article."
2855   '(let ((to (get-text-property (point) 'gnus-intangible)))
2856      (and to (gnus-summary-goto-subject to))))
2857
2858 (defmacro gnus-summary-article-intangible-p ()
2859   "Say whether this article is intangible or not."
2860   '(get-text-property (point) 'gnus-intangible))
2861
2862 (defun gnus-article-read-p (article)
2863   "Say whether ARTICLE is read or not."
2864   (not (or (memq article gnus-newsgroup-marked)
2865            (memq article gnus-newsgroup-spam-marked)
2866            (memq article gnus-newsgroup-unreads)
2867            (memq article gnus-newsgroup-unselected)
2868            (memq article gnus-newsgroup-dormant))))
2869
2870 ;; Some summary mode macros.
2871
2872 (defmacro gnus-summary-article-number ()
2873   "The article number of the article on the current line.
2874 If there isn't an article number here, then we return the current
2875 article number."
2876   '(progn
2877      (gnus-summary-skip-intangible)
2878      (or (get-text-property (point) 'gnus-number)
2879          (gnus-summary-last-subject))))
2880
2881 (defmacro gnus-summary-article-header (&optional number)
2882   "Return the header of article NUMBER."
2883   `(gnus-data-header (gnus-data-find
2884                       ,(or number '(gnus-summary-article-number)))))
2885
2886 (defmacro gnus-summary-thread-level (&optional number)
2887   "Return the level of thread that starts with article NUMBER."
2888   `(if (and (eq gnus-summary-make-false-root 'dummy)
2889             (get-text-property (point) 'gnus-intangible))
2890        0
2891      (gnus-data-level (gnus-data-find
2892                        ,(or number '(gnus-summary-article-number))))))
2893
2894 (defmacro gnus-summary-article-mark (&optional number)
2895   "Return the mark of article NUMBER."
2896   `(gnus-data-mark (gnus-data-find
2897                     ,(or number '(gnus-summary-article-number)))))
2898
2899 (defmacro gnus-summary-article-pos (&optional number)
2900   "Return the position of the line of article NUMBER."
2901   `(gnus-data-pos (gnus-data-find
2902                    ,(or number '(gnus-summary-article-number)))))
2903
2904 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2905 (defmacro gnus-summary-article-subject (&optional number)
2906   "Return current subject string or nil if nothing."
2907   `(let ((headers
2908           ,(if number
2909                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2910              '(gnus-data-header (assq (gnus-summary-article-number)
2911                                       gnus-newsgroup-data)))))
2912      (and headers
2913           (vectorp headers)
2914           (mail-header-subject headers))))
2915
2916 (defmacro gnus-summary-article-score (&optional number)
2917   "Return current article score."
2918   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2919                   gnus-newsgroup-scored))
2920        gnus-summary-default-score 0))
2921
2922 (defun gnus-summary-article-children (&optional number)
2923   "Return a list of article numbers that are children of article NUMBER."
2924   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2925          (level (gnus-data-level (car data)))
2926          l children)
2927     (while (and (setq data (cdr data))
2928                 (> (setq l (gnus-data-level (car data))) level))
2929       (and (= (1+ level) l)
2930            (push (gnus-data-number (car data))
2931                  children)))
2932     (nreverse children)))
2933
2934 (defun gnus-summary-article-parent (&optional number)
2935   "Return the article number of the parent of article NUMBER."
2936   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2937                                     (gnus-data-list t)))
2938          (level (gnus-data-level (car data))))
2939     (if (zerop level)
2940         ()                              ; This is a root.
2941       ;; We search until we find an article with a level less than
2942       ;; this one.  That function has to be the parent.
2943       (while (and (setq data (cdr data))
2944                   (not (< (gnus-data-level (car data)) level))))
2945       (and data (gnus-data-number (car data))))))
2946
2947 (defun gnus-unread-mark-p (mark)
2948   "Say whether MARK is the unread mark."
2949   (= mark gnus-unread-mark))
2950
2951 (defun gnus-read-mark-p (mark)
2952   "Say whether MARK is one of the marks that mark as read.
2953 This is all marks except unread, ticked, dormant, and expirable."
2954   (not (or (= mark gnus-unread-mark)
2955            (= mark gnus-ticked-mark)
2956            (= mark gnus-spam-mark)
2957            (= mark gnus-dormant-mark)
2958            (= mark gnus-expirable-mark))))
2959
2960 (defmacro gnus-article-mark (number)
2961   "Return the MARK of article NUMBER.
2962 This macro should only be used when computing the mark the \"first\"
2963 time; i.e., when generating the summary lines.  After that,
2964 `gnus-summary-article-mark' should be used to examine the
2965 marks of articles."
2966   `(cond
2967     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2968     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2969     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2970     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2971     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2972     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2973     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2974     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2975            gnus-ancient-mark))))
2976
2977 ;; Saving hidden threads.
2978
2979 (defmacro gnus-save-hidden-threads (&rest forms)
2980   "Save hidden threads, eval FORMS, and restore the hidden threads."
2981   (let ((config (make-symbol "config")))
2982     `(let ((,config (gnus-hidden-threads-configuration)))
2983        (unwind-protect
2984            (save-excursion
2985              ,@forms)
2986          (gnus-restore-hidden-threads-configuration ,config)))))
2987 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2988 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2989
2990 (defun gnus-data-compute-positions ()
2991   "Compute the positions of all articles."
2992   (setq gnus-newsgroup-data-reverse nil)
2993   (let ((data gnus-newsgroup-data))
2994     (save-excursion
2995       (gnus-save-hidden-threads
2996         (gnus-summary-show-all-threads)
2997         (goto-char (point-min))
2998         (while data
2999           (while (get-text-property (point) 'gnus-intangible)
3000             (forward-line 1))
3001           (gnus-data-set-pos (car data) (+ (point) 3))
3002           (setq data (cdr data))
3003           (forward-line 1))))))
3004
3005 (defun gnus-hidden-threads-configuration ()
3006   "Return the current hidden threads configuration."
3007   (save-excursion
3008     (let (config)
3009       (goto-char (point-min))
3010       (while (search-forward "\r" nil t)
3011         (push (1- (point)) config))
3012       config)))
3013
3014 (defun gnus-restore-hidden-threads-configuration (config)
3015   "Restore hidden threads configuration from CONFIG."
3016   (save-excursion
3017     (let (point buffer-read-only)
3018       (while (setq point (pop config))
3019         (when (and (< point (point-max))
3020                    (goto-char point)
3021                    (eq (char-after) ?\n))
3022           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3023
3024 ;; Various summary mode internalish functions.
3025
3026 (defun gnus-mouse-pick-article (e)
3027   (interactive "e")
3028   (mouse-set-point e)
3029   (gnus-summary-next-page nil t))
3030
3031 (defun gnus-summary-set-display-table ()
3032   "Change the display table.
3033 Odd characters have a tendency to mess
3034 up nicely formatted displays - we make all possible glyphs
3035 display only a single character."
3036
3037   ;; We start from the standard display table, if any.
3038   (let ((table (or (copy-sequence standard-display-table)
3039                    (make-display-table)))
3040         (i 32))
3041     ;; Nix out all the control chars...
3042     (while (>= (setq i (1- i)) 0)
3043       (aset table i [??]))
3044     ;; ... but not newline and cr, of course.  (cr is necessary for the
3045     ;; selective display).
3046     (aset table ?\n nil)
3047     (aset table ?\r nil)
3048     ;; We keep TAB as well.
3049     (aset table ?\t nil)
3050     ;; We nix out any glyphs over 126 that are not set already.
3051     (let ((i 256))
3052       (while (>= (setq i (1- i)) 127)
3053         ;; Only modify if the entry is nil.
3054         (unless (aref table i)
3055           (aset table i [??]))))
3056     (setq buffer-display-table table)))
3057
3058 (defun gnus-summary-set-article-display-arrow (pos)
3059   "Update the overlay arrow to point to line at position POS."
3060   (when (and gnus-summary-display-arrow
3061              (boundp 'overlay-arrow-position)
3062              (boundp 'overlay-arrow-string))
3063     (save-excursion
3064       (goto-char pos)
3065       (beginning-of-line)
3066       (unless overlay-arrow-position
3067         (setq overlay-arrow-position (make-marker)))
3068       (setq overlay-arrow-string "=>"
3069             overlay-arrow-position (set-marker overlay-arrow-position
3070                                                (point)
3071                                                (current-buffer))))))
3072
3073 (defun gnus-summary-setup-buffer (group)
3074   "Initialize summary buffer."
3075   (let ((buffer (gnus-summary-buffer-name group))
3076         (dead-name (concat "*Dead Summary "
3077                            (gnus-group-decoded-name group) "*")))
3078     ;; If a dead summary buffer exists, we kill it.
3079     (when (gnus-buffer-live-p dead-name)
3080       (gnus-kill-buffer dead-name))
3081     (if (get-buffer buffer)
3082         (progn
3083           (set-buffer buffer)
3084           (setq gnus-summary-buffer (current-buffer))
3085           (not gnus-newsgroup-prepared))
3086       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3087       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3088       (gnus-summary-mode group)
3089       (when gnus-carpal
3090         (gnus-carpal-setup-buffer 'summary))
3091       (unless gnus-single-article-buffer
3092         (make-local-variable 'gnus-article-buffer)
3093         (make-local-variable 'gnus-article-current)
3094         (make-local-variable 'gnus-original-article-buffer))
3095       (setq gnus-newsgroup-name group)
3096       ;; Set any local variables in the group parameters.
3097       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3098       t)))
3099
3100 (defun gnus-set-global-variables ()
3101   "Set the global equivalents of the buffer-local variables.
3102 They are set to the latest values they had.  These reflect the summary
3103 buffer that was in action when the last article was fetched."
3104   (when (eq major-mode 'gnus-summary-mode)
3105     (setq gnus-summary-buffer (current-buffer))
3106     (let ((name gnus-newsgroup-name)
3107           (marked gnus-newsgroup-marked)
3108           (spam gnus-newsgroup-spam-marked)
3109           (unread gnus-newsgroup-unreads)
3110           (headers gnus-current-headers)
3111           (data gnus-newsgroup-data)
3112           (summary gnus-summary-buffer)
3113           (article-buffer gnus-article-buffer)
3114           (original gnus-original-article-buffer)
3115           (gac gnus-article-current)
3116           (reffed gnus-reffed-article-number)
3117           (score-file gnus-current-score-file)
3118           (default-charset gnus-newsgroup-charset)
3119           vlist)
3120       (let ((locals gnus-newsgroup-variables))
3121         (while locals
3122           (if (consp (car locals))
3123               (push (eval (caar locals)) vlist)
3124             (push (eval (car locals)) vlist))
3125           (setq locals (cdr locals)))
3126         (setq vlist (nreverse vlist)))
3127       (save-excursion
3128         (set-buffer gnus-group-buffer)
3129         (setq gnus-newsgroup-name name
3130               gnus-newsgroup-marked marked
3131               gnus-newsgroup-spam-marked spam
3132               gnus-newsgroup-unreads unread
3133               gnus-current-headers headers
3134               gnus-newsgroup-data data
3135               gnus-article-current gac
3136               gnus-summary-buffer summary
3137               gnus-article-buffer article-buffer
3138               gnus-original-article-buffer original
3139               gnus-reffed-article-number reffed
3140               gnus-current-score-file score-file
3141               gnus-newsgroup-charset default-charset)
3142         (let ((locals gnus-newsgroup-variables))
3143           (while locals
3144             (if (consp (car locals))
3145                 (set (caar locals) (pop vlist))
3146               (set (car locals) (pop vlist)))
3147             (setq locals (cdr locals))))
3148         ;; The article buffer also has local variables.
3149         (when (gnus-buffer-live-p gnus-article-buffer)
3150           (set-buffer gnus-article-buffer)
3151           (setq gnus-summary-buffer summary))))))
3152
3153 (defun gnus-summary-article-unread-p (article)
3154   "Say whether ARTICLE is unread or not."
3155   (memq article gnus-newsgroup-unreads))
3156
3157 (defun gnus-summary-first-article-p (&optional article)
3158   "Return whether ARTICLE is the first article in the buffer."
3159   (if (not (setq article (or article (gnus-summary-article-number))))
3160       nil
3161     (eq article (caar gnus-newsgroup-data))))
3162
3163 (defun gnus-summary-last-article-p (&optional article)
3164   "Return whether ARTICLE is the last article in the buffer."
3165   (if (not (setq article (or article (gnus-summary-article-number))))
3166       ;; All non-existent numbers are the last article.  :-)
3167       t
3168     (not (cdr (gnus-data-find-list article)))))
3169
3170 (defun gnus-make-thread-indent-array ()
3171   (let ((n 200))
3172     (unless (and gnus-thread-indent-array
3173                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3174       (setq gnus-thread-indent-array (make-vector 201 "")
3175             gnus-thread-indent-array-level gnus-thread-indent-level)
3176       (while (>= n 0)
3177         (aset gnus-thread-indent-array n
3178               (make-string (* n gnus-thread-indent-level) ? ))
3179         (setq n (1- n))))))
3180
3181 (defun gnus-update-summary-mark-positions ()
3182   "Compute where the summary marks are to go."
3183   (save-excursion
3184     (when (gnus-buffer-exists-p gnus-summary-buffer)
3185       (set-buffer gnus-summary-buffer))
3186     (let ((gnus-replied-mark 129)
3187           (gnus-score-below-mark 130)
3188           (gnus-score-over-mark 130)
3189           (gnus-undownloaded-mark 131)
3190           (spec gnus-summary-line-format-spec)
3191           gnus-visual pos)
3192       (save-excursion
3193         (gnus-set-work-buffer)
3194         (let ((gnus-summary-line-format-spec spec)
3195               (gnus-newsgroup-downloadable '(0)))
3196           (gnus-summary-insert-line
3197            (make-full-mail-header 0 "" "nobody"
3198                                   "05 Apr 2001 23:33:09 +0400"
3199                                   "" "" 0 0 "" nil)
3200            0 nil t 128 t nil "" nil 1)
3201           (goto-char (point-min))
3202           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3203                                              (- (point) (point-min) 1)))))
3204           (goto-char (point-min))
3205           (push (cons 'replied (and (search-forward "\201" nil t)
3206                                     (- (point) (point-min) 1)))
3207                 pos)
3208           (goto-char (point-min))
3209           (push (cons 'score (and (search-forward "\202" nil t)
3210                                   (- (point) (point-min) 1)))
3211                 pos)
3212           (goto-char (point-min))
3213           (push (cons 'download
3214                       (and (search-forward "\203" nil t)
3215                            (- (point) (point-min) 1)))
3216                 pos)))
3217       (setq gnus-summary-mark-positions pos))))
3218
3219 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3220   "Insert a dummy root in the summary buffer."
3221   (beginning-of-line)
3222   (gnus-add-text-properties
3223    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3224    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3225
3226 (defun gnus-summary-extract-address-component (from)
3227   (or (car (funcall gnus-extract-address-components from))
3228       from))
3229
3230 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3231   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3232                                 default-mime-charset)))
3233     ;; Is it really necessary to do this next part for each summary line?
3234     ;; Luckily, doesn't seem to slow things down much.
3235     (or
3236      (and gnus-ignored-from-addresses
3237           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3238           (let ((extra-headers (mail-header-extra header))
3239                 to
3240                 newsgroups)
3241             (cond
3242              ((setq to (cdr (assq 'To extra-headers)))
3243               (concat "-> "
3244                       (inline
3245                         (gnus-summary-extract-address-component
3246                          (funcall gnus-decode-encoded-word-function to)))))
3247              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3248               (concat "=> " newsgroups)))))
3249      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3250
3251 (defun gnus-summary-insert-line (gnus-tmp-header
3252                                  gnus-tmp-level gnus-tmp-current
3253                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3254                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3255                                  &optional gnus-tmp-dummy gnus-tmp-score
3256                                  gnus-tmp-process)
3257   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3258          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3259          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3260          (gnus-tmp-score-char
3261           (if (or (null gnus-summary-default-score)
3262                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3263                       gnus-summary-zcore-fuzz))
3264               ?\ ;;;Whitespace
3265             (if (< gnus-tmp-score gnus-summary-default-score)
3266                 gnus-score-below-mark gnus-score-over-mark)))
3267          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3268          (gnus-tmp-replied
3269           (cond (gnus-tmp-process gnus-process-mark)
3270                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3271                  gnus-cached-mark)
3272                 (gnus-tmp-replied gnus-replied-mark)
3273                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3274                  gnus-forwarded-mark)
3275                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3276                  gnus-saved-mark)
3277                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3278                  gnus-recent-mark)
3279                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3280                  gnus-unseen-mark)
3281                 (t gnus-no-mark)))
3282          (gnus-tmp-downloaded
3283           (cond (undownloaded
3284                  gnus-undownloaded-mark)
3285                 (gnus-newsgroup-agentized
3286                  gnus-downloaded-mark)
3287                 (t
3288                  gnus-no-mark)))
3289          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3290          (gnus-tmp-name
3291           (cond
3292            ((string-match "<[^>]+> *$" gnus-tmp-from)
3293             (let ((beg (match-beginning 0)))
3294               (or (and (string-match "^\".+\"" gnus-tmp-from)
3295                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3296                   (substring gnus-tmp-from 0 beg))))
3297            ((string-match "(.+)" gnus-tmp-from)
3298             (substring gnus-tmp-from
3299                        (1+ (match-beginning 0)) (1- (match-end 0))))
3300            (t gnus-tmp-from)))
3301          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3302          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3303          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3304          (buffer-read-only nil))
3305     (when (string= gnus-tmp-name "")
3306       (setq gnus-tmp-name gnus-tmp-from))
3307     (unless (numberp gnus-tmp-lines)
3308       (setq gnus-tmp-lines -1))
3309     (if (= gnus-tmp-lines -1)
3310         (setq gnus-tmp-lines "?")
3311       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3312     (gnus-put-text-property-excluding-characters-with-faces
3313      (point)
3314      (progn (eval gnus-summary-line-format-spec) (point))
3315      'gnus-number gnus-tmp-number)
3316     (when (gnus-visual-p 'summary-highlight 'highlight)
3317       (forward-line -1)
3318       (gnus-run-hooks 'gnus-summary-update-hook)
3319       (forward-line 1))))
3320
3321 (defun gnus-summary-update-line (&optional dont-update)
3322   "Update summary line after change."
3323   (when (and gnus-summary-default-score
3324              (not gnus-summary-inhibit-highlight))
3325     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3326            (article (gnus-summary-article-number))
3327            (score (gnus-summary-article-score article)))
3328       (unless dont-update
3329         (if (and gnus-summary-mark-below
3330                  (< (gnus-summary-article-score)
3331                     gnus-summary-mark-below))
3332             ;; This article has a low score, so we mark it as read.
3333             (when (memq article gnus-newsgroup-unreads)
3334               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3335           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3336             ;; This article was previously marked as read on account
3337             ;; of a low score, but now it has risen, so we mark it as
3338             ;; unread.
3339             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3340         (gnus-summary-update-mark
3341          (if (or (null gnus-summary-default-score)
3342                  (<= (abs (- score gnus-summary-default-score))
3343                      gnus-summary-zcore-fuzz))
3344              ?\ ;;;Whitespace
3345            (if (< score gnus-summary-default-score)
3346                gnus-score-below-mark gnus-score-over-mark))
3347          'score))
3348       ;; Do visual highlighting.
3349       (when (gnus-visual-p 'summary-highlight 'highlight)
3350         (gnus-run-hooks 'gnus-summary-update-hook)))))
3351
3352 (defvar gnus-tmp-new-adopts nil)
3353
3354 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3355   "Return the number of articles in THREAD.
3356 This may be 0 in some cases -- if none of the articles in
3357 the thread are to be displayed."
3358   (let* ((number
3359           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3360           (cond
3361            ((not (listp thread))
3362             1)
3363            ((and (consp thread) (cdr thread))
3364             (apply
3365              '+ 1 (mapcar
3366                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3367            ((null thread)
3368             1)
3369            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3370             1)
3371            (t 0))))
3372     (when (and level (zerop level) gnus-tmp-new-adopts)
3373       (incf number
3374             (apply '+ (mapcar
3375                        'gnus-summary-number-of-articles-in-thread
3376                        gnus-tmp-new-adopts))))
3377     (if char
3378         (if (> number 1) gnus-not-empty-thread-mark
3379           gnus-empty-thread-mark)
3380       number)))
3381
3382 (defsubst gnus-summary-line-message-size (head)
3383   "Return pretty-printed version of message size.
3384 This function is intended to be used in
3385 `gnus-summary-line-format-alist'."
3386   (let ((c (or (mail-header-chars head) -1)))
3387     (cond ((< c 0) "n/a")               ; chars not available
3388           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3389           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3390           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3391           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3392
3393
3394 (defun gnus-summary-set-local-parameters (group)
3395   "Go through the local params of GROUP and set all variable specs in that list."
3396   (let ((params (gnus-group-find-parameter group))
3397         (vars '(quit-config))           ; Ignore quit-config.
3398         elem)
3399     (while params
3400       (setq elem (car params)
3401             params (cdr params))
3402       (and (consp elem)                 ; Has to be a cons.
3403            (consp (cdr elem))           ; The cdr has to be a list.
3404            (symbolp (car elem))         ; Has to be a symbol in there.
3405            (not (memq (car elem) vars))
3406            (ignore-errors               ; So we set it.
3407              (push (car elem) vars)
3408              (make-local-variable (car elem))
3409              (set (car elem) (eval (nth 1 elem))))))))
3410
3411 (defun gnus-summary-read-group (group &optional show-all no-article
3412                                       kill-buffer no-display backward
3413                                       select-articles)
3414   "Start reading news in newsgroup GROUP.
3415 If SHOW-ALL is non-nil, already read articles are also listed.
3416 If NO-ARTICLE is non-nil, no article is selected initially.
3417 If NO-DISPLAY, don't generate a summary buffer."
3418   (let (result)
3419     (while (and group
3420                 (null (setq result
3421                             (let ((gnus-auto-select-next nil))
3422                               (or (gnus-summary-read-group-1
3423                                    group show-all no-article
3424                                    kill-buffer no-display
3425                                    select-articles)
3426                                   (setq show-all nil
3427                                         select-articles nil)))))
3428                 (eq gnus-auto-select-next 'quietly))
3429       (set-buffer gnus-group-buffer)
3430       ;; The entry function called above goes to the next
3431       ;; group automatically, so we go two groups back
3432       ;; if we are searching for the previous group.
3433       (when backward
3434         (gnus-group-prev-unread-group 2))
3435       (if (not (equal group (gnus-group-group-name)))
3436           (setq group (gnus-group-group-name))
3437         (setq group nil)))
3438     result))
3439
3440 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3441   "Directly jump to the other GROUP from summary buffer.
3442 If SHOW-ALL is non-nil, already read articles are also listed."
3443   (interactive
3444    (if (eq gnus-summary-buffer (current-buffer))
3445        (list (completing-read
3446               "Group: " gnus-active-hashtb nil t
3447               (when (and gnus-newsgroup-name
3448                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3449                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3450               'gnus-group-history)
3451              current-prefix-arg)
3452      (error "%s must be invoked from a gnus summary buffer." this-command)))
3453   (unless (or (zerop (length group))
3454               (and gnus-newsgroup-name
3455                    (string-equal gnus-newsgroup-name group)))
3456     (gnus-summary-exit)
3457     (gnus-summary-read-group group show-all
3458                              gnus-dont-select-after-jump-to-other-group)))
3459
3460 (defun gnus-summary-read-group-1 (group show-all no-article
3461                                         kill-buffer no-display
3462                                         &optional select-articles)
3463   ;; Killed foreign groups can't be entered.
3464   ;;  (when (and (not (gnus-group-native-p group))
3465   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3466   ;;    (error "Dead non-native groups can't be entered"))
3467   (gnus-message 5 "Retrieving newsgroup: %s..."
3468                 (gnus-group-decoded-name group))
3469   (let* ((new-group (gnus-summary-setup-buffer group))
3470          (quit-config (gnus-group-quit-config group))
3471          (did-select (and new-group (gnus-select-newsgroup
3472                                      group show-all select-articles))))
3473     (cond
3474      ;; This summary buffer exists already, so we just select it.
3475      ((not new-group)
3476       (gnus-set-global-variables)
3477       (when kill-buffer
3478         (gnus-kill-or-deaden-summary kill-buffer))
3479       (gnus-configure-windows 'summary 'force)
3480       (gnus-set-mode-line 'summary)
3481       (gnus-summary-position-point)
3482       (message "")
3483       t)
3484      ;; We couldn't select this group.
3485      ((null did-select)
3486       (when (and (eq major-mode 'gnus-summary-mode)
3487                  (not (equal (current-buffer) kill-buffer)))
3488         (kill-buffer (current-buffer))
3489         (if (not quit-config)
3490             (progn
3491               ;; Update the info -- marks might need to be removed,
3492               ;; for instance.
3493               (gnus-summary-update-info)
3494               (set-buffer gnus-group-buffer)
3495               (gnus-group-jump-to-group group)
3496               (gnus-group-next-unread-group 1))
3497           (gnus-handle-ephemeral-exit quit-config)))
3498       (let ((grpinfo (gnus-get-info group)))
3499         (if (null (gnus-info-read grpinfo))
3500             (gnus-message 3 "Group %s contains no messages"
3501                           (gnus-group-decoded-name group))
3502           (gnus-message 3 "Can't select group")))
3503       nil)
3504      ;; The user did a `C-g' while prompting for number of articles,
3505      ;; so we exit this group.
3506      ((eq did-select 'quit)
3507       (and (eq major-mode 'gnus-summary-mode)
3508            (not (equal (current-buffer) kill-buffer))
3509            (kill-buffer (current-buffer)))
3510       (when kill-buffer
3511         (gnus-kill-or-deaden-summary kill-buffer))
3512       (if (not quit-config)
3513           (progn
3514             (set-buffer gnus-group-buffer)
3515             (gnus-group-jump-to-group group)
3516             (gnus-group-next-unread-group 1)
3517             (gnus-configure-windows 'group 'force))
3518         (gnus-handle-ephemeral-exit quit-config))
3519       ;; Finally signal the quit.
3520       (signal 'quit nil))
3521      ;; The group was successfully selected.
3522      (t
3523       (gnus-set-global-variables)
3524       ;; Save the active value in effect when the group was entered.
3525       (setq gnus-newsgroup-active
3526             (gnus-copy-sequence
3527              (gnus-active gnus-newsgroup-name)))
3528       ;; You can change the summary buffer in some way with this hook.
3529       (gnus-run-hooks 'gnus-select-group-hook)
3530       (gnus-update-format-specifications
3531        nil 'summary 'summary-mode 'summary-dummy)
3532       (gnus-update-summary-mark-positions)
3533       ;; Do score processing.
3534       (when gnus-use-scoring
3535         (gnus-possibly-score-headers))
3536       ;; Check whether to fill in the gaps in the threads.
3537       (when gnus-build-sparse-threads
3538         (gnus-build-sparse-threads))
3539       ;; Find the initial limit.
3540       (if gnus-show-threads
3541           (if show-all
3542               (let ((gnus-newsgroup-dormant nil))
3543                 (gnus-summary-initial-limit show-all))
3544             (gnus-summary-initial-limit show-all))
3545         ;; When unthreaded, all articles are always shown.
3546         (setq gnus-newsgroup-limit
3547               (mapcar
3548                (lambda (header) (mail-header-number header))
3549                gnus-newsgroup-headers)))
3550       ;; Generate the summary buffer.
3551       (unless no-display
3552         (gnus-summary-prepare))
3553       (when gnus-use-trees
3554         (gnus-tree-open group)
3555         (setq gnus-summary-highlight-line-function
3556               'gnus-tree-highlight-article))
3557       ;; If the summary buffer is empty, but there are some low-scored
3558       ;; articles or some excluded dormants, we include these in the
3559       ;; buffer.
3560       (when (and (zerop (buffer-size))
3561                  (not no-display))
3562         (cond (gnus-newsgroup-dormant
3563                (gnus-summary-limit-include-dormant))
3564               ((and gnus-newsgroup-scored show-all)
3565                (gnus-summary-limit-include-expunged t))))
3566       ;; Function `gnus-apply-kill-file' must be called in this hook.
3567       (gnus-run-hooks 'gnus-apply-kill-hook)
3568       (if (and (zerop (buffer-size))
3569                (not no-display))
3570           (progn
3571             ;; This newsgroup is empty.
3572             (gnus-summary-catchup-and-exit nil t)
3573             (gnus-message 6 "No unread news")
3574             (when kill-buffer
3575               (gnus-kill-or-deaden-summary kill-buffer))
3576             ;; Return nil from this function.
3577             nil)
3578         ;; Hide conversation thread subtrees.  We cannot do this in
3579         ;; gnus-summary-prepare-hook since kill processing may not
3580         ;; work with hidden articles.
3581         (gnus-summary-maybe-hide-threads)
3582         (when kill-buffer
3583           (gnus-kill-or-deaden-summary kill-buffer))
3584         (gnus-summary-auto-select-subject)
3585         ;; Show first unread article if requested.
3586         (if (and (not no-article)
3587                  (not no-display)
3588                  gnus-newsgroup-unreads
3589                  gnus-auto-select-first)
3590             (progn
3591               (gnus-configure-windows 'summary)
3592               (let ((art (gnus-summary-article-number)))
3593                 (unless (and (not gnus-plugged)
3594                              (or (memq art gnus-newsgroup-undownloaded)
3595                                  (memq art gnus-newsgroup-downloadable)))
3596                   (gnus-summary-goto-article art))))
3597           ;; Don't select any articles.
3598           (gnus-summary-position-point)
3599           (gnus-configure-windows 'summary 'force)
3600           (gnus-set-mode-line 'summary))
3601         (when (and gnus-auto-center-group
3602                    (get-buffer-window gnus-group-buffer t))
3603           ;; Gotta use windows, because recenter does weird stuff if
3604           ;; the current buffer ain't the displayed window.
3605           (let ((owin (selected-window)))
3606             (select-window (get-buffer-window gnus-group-buffer t))
3607             (when (gnus-group-goto-group group)
3608               (recenter))
3609             (select-window owin)))
3610         ;; Mark this buffer as "prepared".
3611         (setq gnus-newsgroup-prepared t)
3612         (gnus-run-hooks 'gnus-summary-prepared-hook)
3613         (unless (gnus-ephemeral-group-p group)
3614           (gnus-group-update-group group))
3615         t)))))
3616
3617 (defun gnus-summary-auto-select-subject ()
3618   "Select the subject line on initial group entry."
3619   (goto-char (point-min))
3620   (cond
3621    ((eq gnus-auto-select-subject 'best)
3622     (gnus-summary-best-unread-subject))
3623    ((eq gnus-auto-select-subject 'unread)
3624     (gnus-summary-first-unread-subject))
3625    ((eq gnus-auto-select-subject 'unseen)
3626     (gnus-summary-first-unseen-subject))
3627    ((eq gnus-auto-select-subject 'unseen-or-unread)
3628     (gnus-summary-first-unseen-or-unread-subject))
3629    ((eq gnus-auto-select-subject 'first)
3630     ;; Do nothing.
3631     )
3632    ((functionp gnus-auto-select-subject)
3633     (funcall gnus-auto-select-subject))))
3634
3635 (defun gnus-summary-prepare ()
3636   "Generate the summary buffer."
3637   (interactive)
3638   (let ((buffer-read-only nil))
3639     (erase-buffer)
3640     (setq gnus-newsgroup-data nil
3641           gnus-newsgroup-data-reverse nil)
3642     (gnus-run-hooks 'gnus-summary-generate-hook)
3643     ;; Generate the buffer, either with threads or without.
3644     (when gnus-newsgroup-headers
3645       (gnus-summary-prepare-threads
3646        (if gnus-show-threads
3647            (gnus-sort-gathered-threads
3648             (funcall gnus-summary-thread-gathering-function
3649                      (gnus-sort-threads
3650                       (gnus-cut-threads (gnus-make-threads)))))
3651          ;; Unthreaded display.
3652          (gnus-sort-articles gnus-newsgroup-headers))))
3653     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3654     ;; Call hooks for modifying summary buffer.
3655     (goto-char (point-min))
3656     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3657
3658 (defsubst gnus-general-simplify-subject (subject)
3659   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3660   (setq subject
3661         (cond
3662          ;; Truncate the subject.
3663          (gnus-simplify-subject-functions
3664           (gnus-map-function gnus-simplify-subject-functions subject))
3665          ((numberp gnus-summary-gather-subject-limit)
3666           (setq subject (gnus-simplify-subject-re subject))
3667           (if (> (length subject) gnus-summary-gather-subject-limit)
3668               (substring subject 0 gnus-summary-gather-subject-limit)
3669             subject))
3670          ;; Fuzzily simplify it.
3671          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3672           (gnus-simplify-subject-fuzzy subject))
3673          ;; Just remove the leading "Re:".
3674          (t
3675           (gnus-simplify-subject-re subject))))
3676
3677   (if (and gnus-summary-gather-exclude-subject
3678            (string-match gnus-summary-gather-exclude-subject subject))
3679       nil                               ; This article shouldn't be gathered
3680     subject))
3681
3682 (defun gnus-summary-simplify-subject-query ()
3683   "Query where the respool algorithm would put this article."
3684   (interactive)
3685   (gnus-summary-select-article)
3686   (message "%s"
3687            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3688
3689 (defun gnus-gather-threads-by-subject (threads)
3690   "Gather threads by looking at Subject headers."
3691   (if (not gnus-summary-make-false-root)
3692       threads
3693     (let ((hashtb (gnus-make-hashtable 1024))
3694           (prev threads)
3695           (result threads)
3696           subject hthread whole-subject)
3697       (while threads
3698         (setq subject (gnus-general-simplify-subject
3699                        (setq whole-subject (mail-header-subject
3700                                             (caar threads)))))
3701         (when subject
3702           (if (setq hthread (gnus-gethash subject hashtb))
3703               (progn
3704                 ;; We enter a dummy root into the thread, if we
3705                 ;; haven't done that already.
3706                 (unless (stringp (caar hthread))
3707                   (setcar hthread (list whole-subject (car hthread))))
3708                 ;; We add this new gathered thread to this gathered
3709                 ;; thread.
3710                 (setcdr (car hthread)
3711                         (nconc (cdar hthread) (list (car threads))))
3712                 ;; Remove it from the list of threads.
3713                 (setcdr prev (cdr threads))
3714                 (setq threads prev))
3715             ;; Enter this thread into the hash table.
3716             (gnus-sethash subject
3717                           (if gnus-summary-make-false-root-always
3718                               (progn
3719                                 ;; If you want a dummy root above all
3720                                 ;; threads...
3721                                 (setcar threads (list whole-subject
3722                                                       (car threads)))
3723                                 threads)
3724                             threads)
3725                           hashtb)))
3726         (setq prev threads)
3727         (setq threads (cdr threads)))
3728       result)))
3729
3730 (defun gnus-gather-threads-by-references (threads)
3731   "Gather threads by looking at References headers."
3732   (let ((idhashtb (gnus-make-hashtable 1024))
3733         (thhashtb (gnus-make-hashtable 1024))
3734         (prev threads)
3735         (result threads)
3736         ids references id gthread gid entered ref)
3737     (while threads
3738       (when (setq references (mail-header-references (caar threads)))
3739         (setq id (mail-header-id (caar threads))
3740               ids (inline (gnus-split-references references))
3741               entered nil)
3742         (while (setq ref (pop ids))
3743           (setq ids (delete ref ids))
3744           (if (not (setq gid (gnus-gethash ref idhashtb)))
3745               (progn
3746                 (gnus-sethash ref id idhashtb)
3747                 (gnus-sethash id threads thhashtb))
3748             (setq gthread (gnus-gethash gid thhashtb))
3749             (unless entered
3750               ;; We enter a dummy root into the thread, if we
3751               ;; haven't done that already.
3752               (unless (stringp (caar gthread))
3753                 (setcar gthread (list (mail-header-subject (caar gthread))
3754                                       (car gthread))))
3755               ;; We add this new gathered thread to this gathered
3756               ;; thread.
3757               (setcdr (car gthread)
3758                       (nconc (cdar gthread) (list (car threads)))))
3759             ;; Add it into the thread hash table.
3760             (gnus-sethash id gthread thhashtb)
3761             (setq entered t)
3762             ;; Remove it from the list of threads.
3763             (setcdr prev (cdr threads))
3764             (setq threads prev))))
3765       (setq prev threads)
3766       (setq threads (cdr threads)))
3767     result))
3768
3769 (defun gnus-sort-gathered-threads (threads)
3770   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3771   (let ((result threads))
3772     (while threads
3773       (when (stringp (caar threads))
3774         (setcdr (car threads)
3775                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3776       (setq threads (cdr threads)))
3777     result))
3778
3779 (defun gnus-thread-loop-p (root thread)
3780   "Say whether ROOT is in THREAD."
3781   (let ((stack (list thread))
3782         (infloop 0)
3783         th)
3784     (while (setq thread (pop stack))
3785       (setq th (cdr thread))
3786       (while (and th
3787                   (not (eq (caar th) root)))
3788         (pop th))
3789       (if th
3790           ;; We have found a loop.
3791           (let (ref-dep)
3792             (setcdr thread (delq (car th) (cdr thread)))
3793             (if (boundp (setq ref-dep (intern "none"
3794                                               gnus-newsgroup-dependencies)))
3795                 (setcdr (symbol-value ref-dep)
3796                         (nconc (cdr (symbol-value ref-dep))
3797                                (list (car th))))
3798               (set ref-dep (list nil (car th))))
3799             (setq infloop 1
3800                   stack nil))
3801         ;; Push all the subthreads onto the stack.
3802         (push (cdr thread) stack)))
3803     infloop))
3804
3805 (defun gnus-make-threads ()
3806   "Go through the dependency hashtb and find the roots.  Return all threads."
3807   (let (threads)
3808     (while (catch 'infloop
3809              (mapatoms
3810               (lambda (refs)
3811                 ;; Deal with self-referencing References loops.
3812                 (when (and (car (symbol-value refs))
3813                            (not (zerop
3814                                  (apply
3815                                   '+
3816                                   (mapcar
3817                                    (lambda (thread)
3818                                      (gnus-thread-loop-p
3819                                       (car (symbol-value refs)) thread))
3820                                    (cdr (symbol-value refs)))))))
3821                   (setq threads nil)
3822                   (throw 'infloop t))
3823                 (unless (car (symbol-value refs))
3824                   ;; These threads do not refer back to any other articles,
3825                   ;; so they're roots.
3826                   (setq threads (append (cdr (symbol-value refs)) threads))))
3827               gnus-newsgroup-dependencies)))
3828     threads))
3829
3830 ;; Build the thread tree.
3831 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3832   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3833
3834 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3835 if it was already present.
3836
3837 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3838 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3839 Message-IDs will be renamed to a unique Message-ID before being
3840 entered.
3841
3842 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3843   (let* ((id (mail-header-id header))
3844          (id-dep (and id (intern id dependencies)))
3845          parent-id ref ref-dep ref-header replaced)
3846     ;; Enter this `header' in the `dependencies' table.
3847     (cond
3848      ((not id-dep)
3849       (setq header nil))
3850      ;; The first two cases do the normal part: enter a new `header'
3851      ;; in the `dependencies' table.
3852      ((not (boundp id-dep))
3853       (set id-dep (list header)))
3854      ((null (car (symbol-value id-dep)))
3855       (setcar (symbol-value id-dep) header))
3856
3857      ;; From here the `header' was already present in the
3858      ;; `dependencies' table.
3859      (force-new
3860       ;; Overrides an existing entry;
3861       ;; just set the header part of the entry.
3862       (setcar (symbol-value id-dep) header)
3863       (setq replaced t))
3864
3865      ;; Renames the existing `header' to a unique Message-ID.
3866      ((not gnus-summary-ignore-duplicates)
3867       ;; An article with this Message-ID has already been seen.
3868       ;; We rename the Message-ID.
3869       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3870            (list header))
3871       (mail-header-set-id header id))
3872
3873      ;; The last case ignores an existing entry, except it adds any
3874      ;; additional Xrefs (in case the two articles came from different
3875      ;; servers.
3876      ;; Also sets `header' to `nil' meaning that the `dependencies'
3877      ;; table was *not* modified.
3878      (t
3879       (mail-header-set-xref
3880        (car (symbol-value id-dep))
3881        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3882                    "")
3883                (or (mail-header-xref header) "")))
3884       (setq header nil)))
3885
3886     (when (and header (not replaced))
3887       ;; First check that we are not creating a References loop.
3888       (setq parent-id (gnus-parent-id (mail-header-references header)))
3889       (setq ref parent-id)
3890       (while (and ref
3891                   (setq ref-dep (intern-soft ref dependencies))
3892                   (boundp ref-dep)
3893                   (setq ref-header (car (symbol-value ref-dep))))
3894         (if (string= id ref)
3895             ;; Yuk!  This is a reference loop.  Make the article be a
3896             ;; root article.
3897             (progn
3898               (mail-header-set-references (car (symbol-value id-dep)) "none")
3899               (setq ref nil)
3900               (setq parent-id nil))
3901           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3902       (setq ref-dep (intern (or parent-id "none") dependencies))
3903       (if (boundp ref-dep)
3904           (setcdr (symbol-value ref-dep)
3905                   (nconc (cdr (symbol-value ref-dep))
3906                          (list (symbol-value id-dep))))
3907         (set ref-dep (list nil (symbol-value id-dep)))))
3908     header))
3909
3910 (defun gnus-extract-message-id-from-in-reply-to (string)
3911   (if (string-match "<[^>]+>" string)
3912       (substring string (match-beginning 0) (match-end 0))
3913     nil))
3914
3915 (defun gnus-build-sparse-threads ()
3916   (let ((headers gnus-newsgroup-headers)
3917         (mail-parse-charset gnus-newsgroup-charset)
3918         (gnus-summary-ignore-duplicates t)
3919         header references generation relations
3920         subject child end new-child date)
3921     ;; First we create an alist of generations/relations, where
3922     ;; generations is how much we trust the relation, and the relation
3923     ;; is parent/child.
3924     (gnus-message 7 "Making sparse threads...")
3925     (save-excursion
3926       (nnheader-set-temp-buffer " *gnus sparse threads*")
3927       (while (setq header (pop headers))
3928         (when (and (setq references (mail-header-references header))
3929                    (not (string= references "")))
3930           (insert references)
3931           (setq child (mail-header-id header)
3932                 subject (mail-header-subject header)
3933                 date (mail-header-date header)
3934                 generation 0)
3935           (while (search-backward ">" nil t)
3936             (setq end (1+ (point)))
3937             (when (search-backward "<" nil t)
3938               (setq new-child (buffer-substring (point) end))
3939               (push (list (incf generation)
3940                           child (setq child new-child)
3941                           subject date)
3942                     relations)))
3943           (when child
3944             (push (list (1+ generation) child nil subject) relations))
3945           (erase-buffer)))
3946       (kill-buffer (current-buffer)))
3947     ;; Sort over trustworthiness.
3948     (mapcar
3949      (lambda (relation)
3950        (when (gnus-dependencies-add-header
3951               (make-full-mail-header-from-decoded-header
3952                gnus-reffed-article-number
3953                (nth 3 relation) "" (or (nth 4 relation) "")
3954                (nth 1 relation)
3955                (or (nth 2 relation) "") 0 0 "")
3956               gnus-newsgroup-dependencies nil)
3957          (push gnus-reffed-article-number gnus-newsgroup-limit)
3958          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3959          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3960                gnus-newsgroup-reads)
3961          (decf gnus-reffed-article-number)))
3962      (sort relations 'car-less-than-car))
3963     (gnus-message 7 "Making sparse threads...done")))
3964
3965 (defun gnus-build-old-threads ()
3966   ;; Look at all the articles that refer back to old articles, and
3967   ;; fetch the headers for the articles that aren't there.  This will
3968   ;; build complete threads - if the roots haven't been expired by the
3969   ;; server, that is.
3970   (let ((mail-parse-charset gnus-newsgroup-charset)
3971         id heads)
3972     (mapatoms
3973      (lambda (refs)
3974        (when (not (car (symbol-value refs)))
3975          (setq heads (cdr (symbol-value refs)))
3976          (while heads
3977            (if (memq (mail-header-number (caar heads))
3978                      gnus-newsgroup-dormant)
3979                (setq heads (cdr heads))
3980              (setq id (symbol-name refs))
3981              (while (and (setq id (gnus-build-get-header id))
3982                          (not (car (gnus-id-to-thread id)))))
3983              (setq heads nil)))))
3984      gnus-newsgroup-dependencies)))
3985
3986 ;; This function has to be called with point after the article number
3987 ;; on the beginning of the line.
3988 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3989   (let ((eol (gnus-point-at-eol))
3990         (buffer (current-buffer))
3991         header references in-reply-to)
3992
3993     ;; overview: [num subject from date id refs chars lines misc]
3994     (unwind-protect
3995         (progn
3996           (narrow-to-region (point) eol)
3997           (unless (eobp)
3998             (forward-char))
3999
4000           (setq header
4001                 (make-full-mail-header
4002                  number                         ; number
4003                  (nnheader-nov-field)           ; subject
4004                  (nnheader-nov-field)           ; from
4005                  (nnheader-nov-field)           ; date
4006                  (nnheader-nov-read-message-id) ; id
4007                  (nnheader-nov-field)           ; refs
4008                  (nnheader-nov-read-integer)    ; chars
4009                  (nnheader-nov-read-integer)    ; lines
4010                  (unless (eobp)
4011                    (if (looking-at "Xref: ")
4012                        (goto-char (match-end 0)))
4013                    (nnheader-nov-field))        ; Xref
4014                  (nnheader-nov-parse-extra))))  ; extra
4015
4016       (widen))
4017
4018     (when (and (string= references "")
4019                (setq in-reply-to (mail-header-extra header))
4020                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4021       (mail-header-set-references
4022        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4023
4024     (when gnus-alter-header-function
4025       (funcall gnus-alter-header-function header))
4026     (gnus-dependencies-add-header header dependencies force-new)))
4027
4028 (defun gnus-build-get-header (id)
4029   "Look through the buffer of NOV lines and find the header to ID.
4030 Enter this line into the dependencies hash table, and return
4031 the id of the parent article (if any)."
4032   (let ((deps gnus-newsgroup-dependencies)
4033         found header)
4034     (prog1
4035         (save-excursion
4036           (set-buffer nntp-server-buffer)
4037           (let ((case-fold-search nil))
4038             (goto-char (point-min))
4039             (while (and (not found)
4040                         (search-forward id nil t))
4041               (beginning-of-line)
4042               (setq found (looking-at
4043                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4044                                    (regexp-quote id))))
4045               (or found (beginning-of-line 2)))
4046             (when found
4047               (beginning-of-line)
4048               (and
4049                (setq header (gnus-nov-parse-line
4050                              (read (current-buffer)) deps))
4051                (gnus-parent-id (mail-header-references header))))))
4052       (when header
4053         (let ((number (mail-header-number header)))
4054           (push number gnus-newsgroup-limit)
4055           (push header gnus-newsgroup-headers)
4056           (if (memq number gnus-newsgroup-unselected)
4057               (progn
4058                 (setq gnus-newsgroup-unreads
4059                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4060                                                number))
4061                 (setq gnus-newsgroup-unselected
4062                       (delq number gnus-newsgroup-unselected)))
4063             (push number gnus-newsgroup-ancient)))))))
4064
4065 (defun gnus-build-all-threads ()
4066   "Read all the headers."
4067   (let ((gnus-summary-ignore-duplicates t)
4068         (mail-parse-charset gnus-newsgroup-charset)
4069         (dependencies gnus-newsgroup-dependencies)
4070         header article)
4071     (save-excursion
4072       (set-buffer nntp-server-buffer)
4073       (let ((case-fold-search nil))
4074         (goto-char (point-min))
4075         (while (not (eobp))
4076           (ignore-errors
4077             (setq article (read (current-buffer))
4078                   header (gnus-nov-parse-line article dependencies)))
4079           (when header
4080             (save-excursion
4081               (set-buffer gnus-summary-buffer)
4082               (push header gnus-newsgroup-headers)
4083               (if (memq (setq article (mail-header-number header))
4084                         gnus-newsgroup-unselected)
4085                   (progn
4086                     (setq gnus-newsgroup-unreads
4087                           (gnus-add-to-sorted-list
4088                            gnus-newsgroup-unreads article))
4089                     (setq gnus-newsgroup-unselected
4090                           (delq article gnus-newsgroup-unselected)))
4091                 (push article gnus-newsgroup-ancient)))
4092             (forward-line 1)))))))
4093
4094 (defun gnus-summary-update-article-line (article header)
4095   "Update the line for ARTICLE using HEADER."
4096   (let* ((id (mail-header-id header))
4097          (thread (gnus-id-to-thread id)))
4098     (unless thread
4099       (error "Article in no thread"))
4100     ;; Update the thread.
4101     (setcar thread header)
4102     (gnus-summary-goto-subject article)
4103     (let* ((datal (gnus-data-find-list article))
4104            (data (car datal))
4105            (buffer-read-only nil)
4106            (level (gnus-summary-thread-level)))
4107       (gnus-delete-line)
4108       (let ((inserted (- (point)
4109                          (progn
4110                            (gnus-summary-insert-line
4111                             header level nil
4112                             (memq article gnus-newsgroup-undownloaded)
4113                             (gnus-article-mark article)
4114                             (memq article gnus-newsgroup-replied)
4115                             (memq article gnus-newsgroup-expirable)
4116                             ;; Only insert the Subject string when it's different
4117                             ;; from the previous Subject string.
4118                             (if (and
4119                                  gnus-show-threads
4120                                  (gnus-subject-equal
4121                                   (condition-case ()
4122                                       (mail-header-subject
4123                                        (gnus-data-header
4124                                         (cadr
4125                                          (gnus-data-find-list
4126                                           article
4127                                           (gnus-data-list t)))))
4128                                     ;; Error on the side of excessive subjects.
4129                                     (error ""))
4130                                   (mail-header-subject header)))
4131                                 ""
4132                               (mail-header-subject header))
4133                             nil (cdr (assq article gnus-newsgroup-scored))
4134                             (memq article gnus-newsgroup-processable))
4135                            (point)))))
4136         (when (cdr datal)
4137           (gnus-data-update-list
4138            (cdr datal)
4139            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4140
4141 (defun gnus-summary-update-article (article &optional iheader)
4142   "Update ARTICLE in the summary buffer."
4143   (set-buffer gnus-summary-buffer)
4144   (let* ((header (gnus-summary-article-header article))
4145          (id (mail-header-id header))
4146          (data (gnus-data-find article))
4147          (thread (gnus-id-to-thread id))
4148          (references (mail-header-references header))
4149          (parent
4150           (gnus-id-to-thread
4151            (or (gnus-parent-id
4152                 (when (and references
4153                            (not (equal "" references)))
4154                   references))
4155                "none")))
4156          (buffer-read-only nil)
4157          (old (car thread)))
4158     (when thread
4159       (unless iheader
4160         (setcar thread nil)
4161         (when parent
4162           (delq thread parent)))
4163       (if (gnus-summary-insert-subject id header)
4164           ;; Set the (possibly) new article number in the data structure.
4165           (gnus-data-set-number data (gnus-id-to-article id))
4166         (setcar thread old)
4167         nil))))
4168
4169 (defun gnus-rebuild-thread (id &optional line)
4170   "Rebuild the thread containing ID.
4171 If LINE, insert the rebuilt thread starting on line LINE."
4172   (let ((buffer-read-only nil)
4173         old-pos current thread data)
4174     (if (not gnus-show-threads)
4175         (setq thread (list (car (gnus-id-to-thread id))))
4176       ;; Get the thread this article is part of.
4177       (setq thread (gnus-remove-thread id)))
4178     (setq old-pos (gnus-point-at-bol))
4179     (setq current (save-excursion
4180                     (and (re-search-backward "[\r\n]" nil t)
4181                          (gnus-summary-article-number))))
4182     ;; If this is a gathered thread, we have to go some re-gathering.
4183     (when (stringp (car thread))
4184       (let ((subject (car thread))
4185             roots thr)
4186         (setq thread (cdr thread))
4187         (while thread
4188           (unless (memq (setq thr (gnus-id-to-thread
4189                                    (gnus-root-id
4190                                     (mail-header-id (caar thread)))))
4191                         roots)
4192             (push thr roots))
4193           (setq thread (cdr thread)))
4194         ;; We now have all (unique) roots.
4195         (if (= (length roots) 1)
4196             ;; All the loose roots are now one solid root.
4197             (setq thread (car roots))
4198           (setq thread (cons subject (gnus-sort-threads roots))))))
4199     (let (threads)
4200       ;; We then insert this thread into the summary buffer.
4201       (when line
4202         (goto-char (point-min))
4203         (forward-line (1- line)))
4204       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4205         (if gnus-show-threads
4206             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4207           (gnus-summary-prepare-unthreaded thread))
4208         (setq data (nreverse gnus-newsgroup-data))
4209         (setq threads gnus-newsgroup-threads))
4210       ;; We splice the new data into the data structure.
4211       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4212       ;;!!! then we want to insert at the beginning of the buffer.
4213       ;;!!! That happens to be true with Gnus now, but that may
4214       ;;!!! change in the future.  Perhaps.
4215       (gnus-data-enter-list
4216        (if line nil current) data (- (point) old-pos))
4217       (setq gnus-newsgroup-threads
4218             (nconc threads gnus-newsgroup-threads))
4219       (gnus-data-compute-positions))))
4220
4221 (defun gnus-number-to-header (number)
4222   "Return the header for article NUMBER."
4223   (let ((headers gnus-newsgroup-headers))
4224     (while (and headers
4225                 (not (= number (mail-header-number (car headers)))))
4226       (pop headers))
4227     (when headers
4228       (car headers))))
4229
4230 (defun gnus-parent-headers (in-headers &optional generation)
4231   "Return the headers of the GENERATIONeth parent of HEADERS."
4232   (unless generation
4233     (setq generation 1))
4234   (let ((parent t)
4235         (headers in-headers)
4236         references)
4237     (while (and parent
4238                 (not (zerop generation))
4239                 (setq references (mail-header-references headers)))
4240       (setq headers (if (and references
4241                              (setq parent (gnus-parent-id references)))
4242                         (car (gnus-id-to-thread parent))
4243                       nil))
4244       (decf generation))
4245     (and (not (eq headers in-headers))
4246          headers)))
4247
4248 (defun gnus-id-to-thread (id)
4249   "Return the (sub-)thread where ID appears."
4250   (gnus-gethash id gnus-newsgroup-dependencies))
4251
4252 (defun gnus-id-to-article (id)
4253   "Return the article number of ID."
4254   (let ((thread (gnus-id-to-thread id)))
4255     (when (and thread
4256                (car thread))
4257       (mail-header-number (car thread)))))
4258
4259 (defun gnus-id-to-header (id)
4260   "Return the article headers of ID."
4261   (car (gnus-id-to-thread id)))
4262
4263 (defun gnus-article-displayed-root-p (article)
4264   "Say whether ARTICLE is a root(ish) article."
4265   (let ((level (gnus-summary-thread-level article))
4266         (refs (mail-header-references  (gnus-summary-article-header article)))
4267         particle)
4268     (cond
4269      ((null level) nil)
4270      ((zerop level) t)
4271      ((null refs) t)
4272      ((null (gnus-parent-id refs)) t)
4273      ((and (= 1 level)
4274            (null (setq particle (gnus-id-to-article
4275                                  (gnus-parent-id refs))))
4276            (null (gnus-summary-thread-level particle)))))))
4277
4278 (defun gnus-root-id (id)
4279   "Return the id of the root of the thread where ID appears."
4280   (let (last-id prev)
4281     (while (and id (setq prev (car (gnus-id-to-thread id))))
4282       (setq last-id id
4283             id (gnus-parent-id (mail-header-references prev))))
4284     last-id))
4285
4286 (defun gnus-articles-in-thread (thread)
4287   "Return the list of articles in THREAD."
4288   (cons (mail-header-number (car thread))
4289         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4290
4291 (defun gnus-remove-thread (id &optional dont-remove)
4292   "Remove the thread that has ID in it."
4293   (let (headers thread last-id)
4294     ;; First go up in this thread until we find the root.
4295     (setq last-id (gnus-root-id id)
4296           headers (message-flatten-list (gnus-id-to-thread last-id)))
4297     ;; We have now found the real root of this thread.  It might have
4298     ;; been gathered into some loose thread, so we have to search
4299     ;; through the threads to find the thread we wanted.
4300     (let ((threads gnus-newsgroup-threads)
4301           sub)
4302       (while threads
4303         (setq sub (car threads))
4304         (if (stringp (car sub))
4305             ;; This is a gathered thread, so we look at the roots
4306             ;; below it to find whether this article is in this
4307             ;; gathered root.
4308             (progn
4309               (setq sub (cdr sub))
4310               (while sub
4311                 (when (member (caar sub) headers)
4312                   (setq thread (car threads)
4313                         threads nil
4314                         sub nil))
4315                 (setq sub (cdr sub))))
4316           ;; It's an ordinary thread, so we check it.
4317           (when (eq (car sub) (car headers))
4318             (setq thread sub
4319                   threads nil)))
4320         (setq threads (cdr threads)))
4321       ;; If this article is in no thread, then it's a root.
4322       (if thread
4323           (unless dont-remove
4324             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4325         (setq thread (gnus-id-to-thread last-id)))
4326       (when thread
4327         (prog1
4328             thread                      ; We return this thread.
4329           (unless dont-remove
4330             (if (stringp (car thread))
4331                 (progn
4332                   ;; If we use dummy roots, then we have to remove the
4333                   ;; dummy root as well.
4334                   (when (eq gnus-summary-make-false-root 'dummy)
4335                     ;; We go to the dummy root by going to
4336                     ;; the first sub-"thread", and then one line up.
4337                     (gnus-summary-goto-article
4338                      (mail-header-number (caadr thread)))
4339                     (forward-line -1)
4340                     (gnus-delete-line)
4341                     (gnus-data-compute-positions))
4342                   (setq thread (cdr thread))
4343                   (while thread
4344                     (gnus-remove-thread-1 (car thread))
4345                     (setq thread (cdr thread))))
4346               (gnus-remove-thread-1 thread))))))))
4347
4348 (defun gnus-remove-thread-1 (thread)
4349   "Remove the thread THREAD recursively."
4350   (let ((number (mail-header-number (pop thread)))
4351         d)
4352     (setq thread (reverse thread))
4353     (while thread
4354       (gnus-remove-thread-1 (pop thread)))
4355     (when (setq d (gnus-data-find number))
4356       (goto-char (gnus-data-pos d))
4357       (gnus-summary-show-thread)
4358       (gnus-data-remove
4359        number
4360        (- (gnus-point-at-bol)
4361           (prog1
4362               (1+ (gnus-point-at-eol))
4363             (gnus-delete-line)))))))
4364
4365 (defun gnus-sort-threads-1 (threads func)
4366   (sort (mapcar (lambda (thread)
4367                   (cons (car thread)
4368                         (and (cdr thread)
4369                              (gnus-sort-threads-1 (cdr thread) func))))
4370                 threads) func))
4371
4372 (defun gnus-sort-threads (threads)
4373   "Sort THREADS."
4374   (if (not gnus-thread-sort-functions)
4375       threads
4376     (gnus-message 8 "Sorting threads...")
4377     (let ((max-lisp-eval-depth 5000))
4378       (prog1 (gnus-sort-threads-1
4379          threads
4380          (gnus-make-sort-function gnus-thread-sort-functions))
4381         (gnus-message 8 "Sorting threads...done")))))
4382
4383 (defun gnus-sort-articles (articles)
4384   "Sort ARTICLES."
4385   (when gnus-article-sort-functions
4386     (gnus-message 7 "Sorting articles...")
4387     (prog1
4388         (setq gnus-newsgroup-headers
4389               (sort articles (gnus-make-sort-function
4390                               gnus-article-sort-functions)))
4391       (gnus-message 7 "Sorting articles...done"))))
4392
4393 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4394 (defmacro gnus-thread-header (thread)
4395   "Return header of first article in THREAD.
4396 Note that THREAD must never, ever be anything else than a variable -
4397 using some other form will lead to serious barfage."
4398   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4399   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4400   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4401         (vector thread) 2))
4402
4403 (defsubst gnus-article-sort-by-number (h1 h2)
4404   "Sort articles by article number."
4405   (< (mail-header-number h1)
4406      (mail-header-number h2)))
4407
4408 (defun gnus-thread-sort-by-number (h1 h2)
4409   "Sort threads by root article number."
4410   (gnus-article-sort-by-number
4411    (gnus-thread-header h1) (gnus-thread-header h2)))
4412
4413 (defsubst gnus-article-sort-by-random (h1 h2)
4414   "Sort articles by article number."
4415   (zerop (random 2)))
4416
4417 (defun gnus-thread-sort-by-random (h1 h2)
4418   "Sort threads by root article number."
4419   (gnus-article-sort-by-random
4420    (gnus-thread-header h1) (gnus-thread-header h2)))
4421
4422 (defsubst gnus-article-sort-by-lines (h1 h2)
4423   "Sort articles by article Lines header."
4424   (< (mail-header-lines h1)
4425      (mail-header-lines h2)))
4426
4427 (defun gnus-thread-sort-by-lines (h1 h2)
4428   "Sort threads by root article Lines header."
4429   (gnus-article-sort-by-lines
4430    (gnus-thread-header h1) (gnus-thread-header h2)))
4431
4432 (defsubst gnus-article-sort-by-chars (h1 h2)
4433   "Sort articles by octet length."
4434   (< (mail-header-chars h1)
4435      (mail-header-chars h2)))
4436
4437 (defun gnus-thread-sort-by-chars (h1 h2)
4438   "Sort threads by root article octet length."
4439   (gnus-article-sort-by-chars
4440    (gnus-thread-header h1) (gnus-thread-header h2)))
4441
4442 (defsubst gnus-article-sort-by-author (h1 h2)
4443   "Sort articles by root author."
4444   (string-lessp
4445    (let ((addr (car (mime-entity-read-field h1 'From))))
4446      (or (std11-full-name-string addr)
4447          (std11-address-string addr)
4448          ""))
4449    (let ((addr (car (mime-entity-read-field h2 'From))))
4450      (or (std11-full-name-string addr)
4451          (std11-address-string addr)
4452          ""))
4453    ))
4454
4455 (defun gnus-thread-sort-by-author (h1 h2)
4456   "Sort threads by root author."
4457   (gnus-article-sort-by-author
4458    (gnus-thread-header h1)  (gnus-thread-header h2)))
4459
4460 (defsubst gnus-article-sort-by-subject (h1 h2)
4461   "Sort articles by root subject."
4462   (string-lessp
4463    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4464    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4465
4466 (defun gnus-thread-sort-by-subject (h1 h2)
4467   "Sort threads by root subject."
4468   (gnus-article-sort-by-subject
4469    (gnus-thread-header h1) (gnus-thread-header h2)))
4470
4471 (defsubst gnus-article-sort-by-date (h1 h2)
4472   "Sort articles by root article date."
4473   (time-less-p
4474    (gnus-date-get-time (mail-header-date h1))
4475    (gnus-date-get-time (mail-header-date h2))))
4476
4477 (defun gnus-thread-sort-by-date (h1 h2)
4478   "Sort threads by root article date."
4479   (gnus-article-sort-by-date
4480    (gnus-thread-header h1) (gnus-thread-header h2)))
4481
4482 (defsubst gnus-article-sort-by-score (h1 h2)
4483   "Sort articles by root article score.
4484 Unscored articles will be counted as having a score of zero."
4485   (> (or (cdr (assq (mail-header-number h1)
4486                     gnus-newsgroup-scored))
4487          gnus-summary-default-score 0)
4488      (or (cdr (assq (mail-header-number h2)
4489                     gnus-newsgroup-scored))
4490          gnus-summary-default-score 0)))
4491
4492 (defun gnus-thread-sort-by-score (h1 h2)
4493   "Sort threads by root article score."
4494   (gnus-article-sort-by-score
4495    (gnus-thread-header h1) (gnus-thread-header h2)))
4496
4497 (defun gnus-thread-sort-by-total-score (h1 h2)
4498   "Sort threads by the sum of all scores in the thread.
4499 Unscored articles will be counted as having a score of zero."
4500   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4501
4502 (defun gnus-thread-total-score (thread)
4503   ;; This function find the total score of THREAD.
4504   (cond
4505    ((null thread)
4506     0)
4507    ((consp thread)
4508     (if (stringp (car thread))
4509         (apply gnus-thread-score-function 0
4510                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4511       (gnus-thread-total-score-1 thread)))
4512    (t
4513     (gnus-thread-total-score-1 (list thread)))))
4514
4515 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4516   "Sort threads such that the thread with the most recently arrived article comes first."
4517   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4518
4519 (defun gnus-thread-highest-number (thread)
4520   "Return the highest article number in THREAD."
4521   (apply 'max (mapcar (lambda (header)
4522                         (mail-header-number header))
4523                       (message-flatten-list thread))))
4524
4525 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4526   "Sort threads such that the thread with the most recently dated article comes first."
4527   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4528
4529 (defun gnus-thread-latest-date (thread)
4530   "Return the highest article date in THREAD."
4531   (let ((previous-time 0))
4532     (apply 'max
4533            (mapcar
4534             (lambda (header)
4535               (setq previous-time
4536                     (condition-case ()
4537                         (time-to-seconds (mail-header-parse-date
4538                                           (mail-header-date header)))
4539                       (error previous-time))))
4540             (sort
4541              (message-flatten-list thread)
4542              (lambda (h1 h2)
4543                (< (mail-header-number h1)
4544                   (mail-header-number h2))))))))
4545
4546 (defun gnus-thread-total-score-1 (root)
4547   ;; This function find the total score of the thread below ROOT.
4548   (setq root (car root))
4549   (apply gnus-thread-score-function
4550          (or (append
4551               (mapcar 'gnus-thread-total-score
4552                       (cdr (gnus-id-to-thread (mail-header-id root))))
4553               (when (> (mail-header-number root) 0)
4554                 (list (or (cdr (assq (mail-header-number root)
4555                                      gnus-newsgroup-scored))
4556                           gnus-summary-default-score 0))))
4557              (list gnus-summary-default-score)
4558              '(0))))
4559
4560 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4561 (defvar gnus-tmp-prev-subject nil)
4562 (defvar gnus-tmp-false-parent nil)
4563 (defvar gnus-tmp-root-expunged nil)
4564 (defvar gnus-tmp-dummy-line nil)
4565
4566 (eval-when-compile (defvar gnus-tmp-header))
4567 (defun gnus-extra-header (type &optional header)
4568   "Return the extra header of TYPE."
4569   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4570       ""))
4571
4572 (defvar gnus-tmp-thread-tree-header-string "")
4573
4574 (defcustom gnus-sum-thread-tree-root "> "
4575   "With %B spec, used for the root of a thread.
4576 If nil, use subject instead."
4577   :type 'string
4578   :group 'gnus-thread)
4579 (defcustom gnus-sum-thread-tree-false-root "> "
4580   "With %B spec, used for a false root of a thread.
4581 If nil, use subject instead."
4582   :type 'string
4583   :group 'gnus-thread)
4584 (defcustom gnus-sum-thread-tree-single-indent ""
4585   "With %B spec, used for a thread with just one message.
4586 If nil, use subject instead."
4587   :type 'string
4588   :group 'gnus-thread)
4589 (defcustom gnus-sum-thread-tree-vertical "| "
4590   "With %B spec, used for drawing a vertical line."
4591   :type 'string
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-indent "  "
4594   "With %B spec, used for indenting."
4595   :type 'string
4596   :group 'gnus-thread)
4597 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4598   "With %B spec, used for a leaf with brothers."
4599   :type 'string
4600   :group 'gnus-thread)
4601 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4602   "With %B spec, used for a leaf without brothers."
4603   :type 'string
4604   :group 'gnus-thread)
4605
4606 (defun gnus-summary-prepare-threads (threads)
4607   "Prepare summary buffer from THREADS and indentation LEVEL.
4608 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4609 or a straight list of headers."
4610   (gnus-message 7 "Generating summary...")
4611
4612   (setq gnus-newsgroup-threads threads)
4613   (beginning-of-line)
4614
4615   (let ((gnus-tmp-level 0)
4616         (default-score (or gnus-summary-default-score 0))
4617         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4618         (building-line-count gnus-summary-display-while-building)
4619         (building-count (integerp gnus-summary-display-while-building))
4620         thread number subject stack state gnus-tmp-gathered beg-match
4621         new-roots gnus-tmp-new-adopts thread-end simp-subject
4622         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4623         gnus-tmp-replied gnus-tmp-subject-or-nil
4624         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4625         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4626         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4627         tree-stack)
4628
4629     (setq gnus-tmp-prev-subject nil
4630           gnus-tmp-thread-tree-header-string "")
4631
4632     (if (vectorp (car threads))
4633         ;; If this is a straight (sic) list of headers, then a
4634         ;; threaded summary display isn't required, so we just create
4635         ;; an unthreaded one.
4636         (gnus-summary-prepare-unthreaded threads)
4637
4638       ;; Do the threaded display.
4639
4640       (if gnus-summary-display-while-building
4641           (switch-to-buffer (buffer-name)))
4642       (while (or threads stack gnus-tmp-new-adopts new-roots)
4643
4644         (if (and (= gnus-tmp-level 0)
4645                  (or (not stack)
4646                      (= (caar stack) 0))
4647                  (not gnus-tmp-false-parent)
4648                  (or gnus-tmp-new-adopts new-roots))
4649             (if gnus-tmp-new-adopts
4650                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4651                       thread (list (car gnus-tmp-new-adopts))
4652                       gnus-tmp-header (caar thread)
4653                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4654               (when new-roots
4655                 (setq thread (list (car new-roots))
4656                       gnus-tmp-header (caar thread)
4657                       new-roots (cdr new-roots))))
4658
4659           (if threads
4660               ;; If there are some threads, we do them before the
4661               ;; threads on the stack.
4662               (setq thread threads
4663                     gnus-tmp-header (caar thread))
4664             ;; There were no current threads, so we pop something off
4665             ;; the stack.
4666             (setq state (car stack)
4667                   gnus-tmp-level (car state)
4668                   tree-stack (cadr state)
4669                   thread (caddr state)
4670                   stack (cdr stack)
4671                   gnus-tmp-header (caar thread))))
4672
4673         (setq gnus-tmp-false-parent nil)
4674         (setq gnus-tmp-root-expunged nil)
4675         (setq thread-end nil)
4676
4677         (if (stringp gnus-tmp-header)
4678             ;; The header is a dummy root.
4679             (cond
4680              ((eq gnus-summary-make-false-root 'adopt)
4681               ;; We let the first article adopt the rest.
4682               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4683                                                (cddar thread)))
4684               (setq gnus-tmp-gathered
4685                     (nconc (mapcar
4686                             (lambda (h) (mail-header-number (car h)))
4687                             (cddar thread))
4688                            gnus-tmp-gathered))
4689               (setq thread (cons (list (caar thread)
4690                                        (cadar thread))
4691                                  (cdr thread)))
4692               (setq gnus-tmp-level -1
4693                     gnus-tmp-false-parent t))
4694              ((eq gnus-summary-make-false-root 'empty)
4695               ;; We print adopted articles with empty subject fields.
4696               (setq gnus-tmp-gathered
4697                     (nconc (mapcar
4698                             (lambda (h) (mail-header-number (car h)))
4699                             (cddar thread))
4700                            gnus-tmp-gathered))
4701               (setq gnus-tmp-level -1))
4702              ((eq gnus-summary-make-false-root 'dummy)
4703               ;; We remember that we probably want to output a dummy
4704               ;; root.
4705               (setq gnus-tmp-dummy-line gnus-tmp-header)
4706               (setq gnus-tmp-prev-subject gnus-tmp-header))
4707              (t
4708               ;; We do not make a root for the gathered
4709               ;; sub-threads at all.
4710               (setq gnus-tmp-level -1)))
4711
4712           (setq number (mail-header-number gnus-tmp-header)
4713                 subject (mail-header-subject gnus-tmp-header)
4714                 simp-subject (gnus-simplify-subject-fully subject))
4715
4716           (cond
4717            ;; If the thread has changed subject, we might want to make
4718            ;; this subthread into a root.
4719            ((and (null gnus-thread-ignore-subject)
4720                  (not (zerop gnus-tmp-level))
4721                  gnus-tmp-prev-subject
4722                  (not (string= gnus-tmp-prev-subject simp-subject)))
4723             (setq new-roots (nconc new-roots (list (car thread)))
4724                   thread-end t
4725                   gnus-tmp-header nil))
4726            ;; If the article lies outside the current limit,
4727            ;; then we do not display it.
4728            ((not (memq number gnus-newsgroup-limit))
4729             (setq gnus-tmp-gathered
4730                   (nconc (mapcar
4731                           (lambda (h) (mail-header-number (car h)))
4732                           (cdar thread))
4733                          gnus-tmp-gathered))
4734             (setq gnus-tmp-new-adopts (if (cdar thread)
4735                                           (append gnus-tmp-new-adopts
4736                                                   (cdar thread))
4737                                         gnus-tmp-new-adopts)
4738                   thread-end t
4739                   gnus-tmp-header nil)
4740             (when (zerop gnus-tmp-level)
4741               (setq gnus-tmp-root-expunged t)))
4742            ;; Perhaps this article is to be marked as read?
4743            ((and gnus-summary-mark-below
4744                  (< (or (cdr (assq number gnus-newsgroup-scored))
4745                         default-score)
4746                     gnus-summary-mark-below)
4747                  ;; Don't touch sparse articles.
4748                  (not (gnus-summary-article-sparse-p number))
4749                  (not (gnus-summary-article-ancient-p number)))
4750             (setq gnus-newsgroup-unreads
4751                   (delq number gnus-newsgroup-unreads))
4752             (if gnus-newsgroup-auto-expire
4753                 (setq gnus-newsgroup-expirable
4754                       (gnus-add-to-sorted-list
4755                        gnus-newsgroup-expirable number))
4756               (push (cons number gnus-low-score-mark)
4757                     gnus-newsgroup-reads))))
4758
4759           (when gnus-tmp-header
4760             ;; We may have an old dummy line to output before this
4761             ;; article.
4762             (when (and gnus-tmp-dummy-line
4763                        (gnus-subject-equal
4764                         gnus-tmp-dummy-line
4765                         (mail-header-subject gnus-tmp-header)))
4766               (gnus-summary-insert-dummy-line
4767                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4768               (setq gnus-tmp-dummy-line nil))
4769
4770             ;; Compute the mark.
4771             (setq gnus-tmp-unread (gnus-article-mark number))
4772
4773             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4774                                   gnus-tmp-header gnus-tmp-level)
4775                   gnus-newsgroup-data)
4776
4777             ;; Actually insert the line.
4778             (setq
4779              gnus-tmp-subject-or-nil
4780              (cond
4781               ((and gnus-thread-ignore-subject
4782                     gnus-tmp-prev-subject
4783                     (not (string= gnus-tmp-prev-subject simp-subject)))
4784                subject)
4785               ((zerop gnus-tmp-level)
4786                (if (and (eq gnus-summary-make-false-root 'empty)
4787                         (memq number gnus-tmp-gathered)
4788                         gnus-tmp-prev-subject
4789                         (string= gnus-tmp-prev-subject simp-subject))
4790                    gnus-summary-same-subject
4791                  subject))
4792               (t gnus-summary-same-subject)))
4793             (if (and (eq gnus-summary-make-false-root 'adopt)
4794                      (= gnus-tmp-level 1)
4795                      (memq number gnus-tmp-gathered))
4796                 (setq gnus-tmp-opening-bracket ?\<
4797                       gnus-tmp-closing-bracket ?\>)
4798               (setq gnus-tmp-opening-bracket ?\[
4799                     gnus-tmp-closing-bracket ?\]))
4800             (setq
4801              gnus-tmp-indentation
4802              (aref gnus-thread-indent-array gnus-tmp-level)
4803              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4804              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4805                                 gnus-summary-default-score 0)
4806              gnus-tmp-score-char
4807              (if (or (null gnus-summary-default-score)
4808                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4809                          gnus-summary-zcore-fuzz))
4810                  ?\ ;;;Whitespace
4811                (if (< gnus-tmp-score gnus-summary-default-score)
4812                    gnus-score-below-mark gnus-score-over-mark))
4813              gnus-tmp-replied
4814              (cond ((memq number gnus-newsgroup-processable)
4815                     gnus-process-mark)
4816                    ((memq number gnus-newsgroup-cached)
4817                     gnus-cached-mark)
4818                    ((memq number gnus-newsgroup-replied)
4819                     gnus-replied-mark)
4820                    ((memq number gnus-newsgroup-forwarded)
4821                     gnus-forwarded-mark)
4822                    ((memq number gnus-newsgroup-saved)
4823                     gnus-saved-mark)
4824                    ((memq number gnus-newsgroup-recent)
4825                     gnus-recent-mark)
4826                    ((memq number gnus-newsgroup-unseen)
4827                     gnus-unseen-mark)
4828                    (t gnus-no-mark))
4829              gnus-tmp-downloaded
4830              (cond ((memq number gnus-newsgroup-undownloaded)
4831                     gnus-undownloaded-mark)
4832                    (gnus-newsgroup-agentized
4833                     gnus-downloaded-mark)
4834                    (t
4835                     gnus-no-mark))
4836              gnus-tmp-from (mail-header-from gnus-tmp-header)
4837              gnus-tmp-name
4838              (cond
4839               ((string-match "<[^>]+> *$" gnus-tmp-from)
4840                (setq beg-match (match-beginning 0))
4841                (or (and (string-match "^\".+\"" gnus-tmp-from)
4842                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4843                    (substring gnus-tmp-from 0 beg-match)))
4844               ((string-match "(.+)" gnus-tmp-from)
4845                (substring gnus-tmp-from
4846                           (1+ (match-beginning 0)) (1- (match-end 0))))
4847               (t gnus-tmp-from))
4848
4849              ;; Do the %B string
4850              gnus-tmp-thread-tree-header-string
4851              (cond
4852               ((not gnus-show-threads) "")
4853               ((zerop gnus-tmp-level)
4854                (cond ((cdar thread)
4855                       (or gnus-sum-thread-tree-root subject))
4856                      (gnus-tmp-new-adopts
4857                       (or gnus-sum-thread-tree-false-root subject))
4858                      (t
4859                       (or gnus-sum-thread-tree-single-indent subject))))
4860               (t
4861                (concat (apply 'concat
4862                               (mapcar (lambda (item)
4863                                         (if (= item 1)
4864                                             gnus-sum-thread-tree-vertical
4865                                           gnus-sum-thread-tree-indent))
4866                                       (cdr (reverse tree-stack))))
4867                        (if (nth 1 thread)
4868                            gnus-sum-thread-tree-leaf-with-other
4869                          gnus-sum-thread-tree-single-leaf)))))
4870             (when (string= gnus-tmp-name "")
4871               (setq gnus-tmp-name gnus-tmp-from))
4872             (unless (numberp gnus-tmp-lines)
4873               (setq gnus-tmp-lines -1))
4874             (if (= gnus-tmp-lines -1)
4875                 (setq gnus-tmp-lines "?")
4876               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4877               (gnus-put-text-property
4878              (point)
4879              (progn (eval gnus-summary-line-format-spec) (point))
4880                'gnus-number number)
4881             (when gnus-visual-p
4882               (forward-line -1)
4883               (gnus-run-hooks 'gnus-summary-update-hook)
4884               (forward-line 1))
4885
4886             (setq gnus-tmp-prev-subject simp-subject)))
4887
4888         (when (nth 1 thread)
4889           (push (list (max 0 gnus-tmp-level)
4890                       (copy-sequence tree-stack)
4891                       (nthcdr 1 thread))
4892                 stack))
4893         (push (if (nth 1 thread) 1 0) tree-stack)
4894         (incf gnus-tmp-level)
4895         (setq threads (if thread-end nil (cdar thread)))
4896         (if gnus-summary-display-while-building
4897             (if building-count
4898                 (progn
4899                   ;; use a set frequency
4900                   (setq building-line-count (1- building-line-count))
4901                   (when (= building-line-count 0)
4902                     (sit-for 0)
4903                     (setq building-line-count
4904                           gnus-summary-display-while-building)))
4905               ;; always
4906               (sit-for 0)))
4907         (unless threads
4908           (setq gnus-tmp-level 0)))))
4909   (gnus-message 7 "Generating summary...done"))
4910
4911 (defun gnus-summary-prepare-unthreaded (headers)
4912   "Generate an unthreaded summary buffer based on HEADERS."
4913   (let (header number mark)
4914
4915     (beginning-of-line)
4916
4917     (while headers
4918       ;; We may have to root out some bad articles...
4919       (when (memq (setq number (mail-header-number
4920                                 (setq header (pop headers))))
4921                   gnus-newsgroup-limit)
4922         ;; Mark article as read when it has a low score.
4923         (when (and gnus-summary-mark-below
4924                    (< (or (cdr (assq number gnus-newsgroup-scored))
4925                           gnus-summary-default-score 0)
4926                       gnus-summary-mark-below)
4927                    (not (gnus-summary-article-ancient-p number)))
4928           (setq gnus-newsgroup-unreads
4929                 (delq number gnus-newsgroup-unreads))
4930           (if gnus-newsgroup-auto-expire
4931               (push number gnus-newsgroup-expirable)
4932             (push (cons number gnus-low-score-mark)
4933                   gnus-newsgroup-reads)))
4934
4935         (setq mark (gnus-article-mark number))
4936         (push (gnus-data-make number mark (1+ (point)) header 0)
4937               gnus-newsgroup-data)
4938         (gnus-summary-insert-line
4939          header 0 number
4940          (memq number gnus-newsgroup-undownloaded)
4941          mark (memq number gnus-newsgroup-replied)
4942          (memq number gnus-newsgroup-expirable)
4943          (mail-header-subject header) nil
4944          (cdr (assq number gnus-newsgroup-scored))
4945          (memq number gnus-newsgroup-processable))))))
4946
4947 (defun gnus-summary-remove-list-identifiers ()
4948   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4949   (let ((regexp (if (consp gnus-list-identifiers)
4950                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4951                   gnus-list-identifiers))
4952         changed subject)
4953     (when regexp
4954       (dolist (header gnus-newsgroup-headers)
4955         (setq subject (mail-header-subject header)
4956               changed nil)
4957         (while (string-match
4958                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4959                 subject)
4960           (setq subject
4961                 (concat (substring subject 0 (match-beginning 2))
4962                         (substring subject (match-end 0)))
4963                 changed t))
4964         (when (and changed
4965                    (string-match
4966                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4967           (setq subject
4968                 (concat (substring subject 0 (match-beginning 1))
4969                         (substring subject (match-end 1)))))
4970         (when changed
4971           (mail-header-set-subject header subject))))))
4972
4973 (defun gnus-fetch-headers (articles)
4974   "Fetch headers of ARTICLES."
4975   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4976     (gnus-message 5 "Fetching headers for %s..." name)
4977     (prog1
4978         (if (eq 'nov
4979                 (setq gnus-headers-retrieved-by
4980                       (gnus-retrieve-headers
4981                        articles gnus-newsgroup-name
4982                        ;; We might want to fetch old headers, but
4983                        ;; not if there is only 1 article.
4984                        (and (or (and
4985                                  (not (eq gnus-fetch-old-headers 'some))
4986                                  (not (numberp gnus-fetch-old-headers)))
4987                                 (> (length articles) 1))
4988                             gnus-fetch-old-headers))))
4989             (gnus-get-newsgroup-headers-xover
4990              articles nil nil gnus-newsgroup-name t)
4991           (gnus-get-newsgroup-headers))
4992       (gnus-message 5 "Fetching headers for %s...done" name))))
4993
4994 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4995   "Select newsgroup GROUP.
4996 If READ-ALL is non-nil, all articles in the group are selected.
4997 If SELECT-ARTICLES, only select those articles from GROUP."
4998   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4999          ;;!!! Dirty hack; should be removed.
5000          (gnus-summary-ignore-duplicates
5001           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5002               t
5003             gnus-summary-ignore-duplicates))
5004          (info (nth 2 entry))
5005          articles fetched-articles cached)
5006
5007     (unless (gnus-check-server
5008              (set (make-local-variable 'gnus-current-select-method)
5009                   (gnus-find-method-for-group group)))
5010       (error "Couldn't open server"))
5011
5012     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5013         (gnus-activate-group group)     ; Or we can activate it...
5014         (progn                          ; Or we bug out.
5015           (when (equal major-mode 'gnus-summary-mode)
5016             (gnus-kill-buffer (current-buffer)))
5017           (error "Couldn't activate group %s: %s"
5018                  group (gnus-status-message group))))
5019
5020     (unless (gnus-request-group group t)
5021       (when (equal major-mode 'gnus-summary-mode)
5022         (gnus-kill-buffer (current-buffer)))
5023       (error "Couldn't request group %s: %s"
5024              group (gnus-status-message group)))
5025
5026     (when gnus-agent
5027       ;; The agent may be storing articles that are no longer in the
5028       ;; server's active range.  If that is the case, the active range
5029       ;; needs to be expanded such that the agent's articles can be
5030       ;; included in the summary.
5031       (let* ((gnus-command-method (gnus-find-method-for-group group))
5032              (alist (gnus-agent-load-alist group))
5033              (active (gnus-active group)))
5034         (if (and (car alist)
5035                  (< (caar alist) (car active)))
5036             (gnus-set-active group (cons (caar alist) (cdr active)))))
5037
5038       (setq gnus-summary-use-undownloaded-faces
5039             (not (gnus-agent-find-parameter
5040                   group
5041                   'agent-disable-undownloaded-faces))))
5042
5043     (setq gnus-newsgroup-name group
5044           gnus-newsgroup-unselected nil
5045           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5046
5047     (let ((display (gnus-group-find-parameter group 'display)))
5048       (setq gnus-newsgroup-display
5049             (cond
5050              ((not (zerop (or (car-safe read-all) 0)))
5051               ;; The user entered the group with C-u SPC/RET, let's show
5052               ;; all articles.
5053               'gnus-not-ignore)
5054              ((eq display 'all)
5055               'gnus-not-ignore)
5056              ((arrayp display)
5057               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5058              ((numberp display)
5059               ;; The following is probably the "correct" solution, but
5060               ;; it makes Gnus fetch all headers and then limit the
5061               ;; articles (which is slow), so instead we hack the
5062               ;; select-articles parameter instead. -- Simon Josefsson
5063               ;; <jas@kth.se>
5064               ;;
5065               ;; (gnus-byte-compile
5066               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5067               ;;                         display)))))
5068               (setq select-articles
5069                     (gnus-uncompress-range
5070                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5071                              (if (> tmp 0)
5072                                  tmp
5073                                1))
5074                            (cdr (gnus-active group)))))
5075               nil)
5076              (t
5077               nil))))
5078
5079     (gnus-summary-setup-default-charset)
5080
5081     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5082     (when (gnus-virtual-group-p group)
5083       (setq cached gnus-newsgroup-cached))
5084
5085     (setq gnus-newsgroup-unreads
5086           (gnus-sorted-ndifference
5087            (gnus-sorted-ndifference gnus-newsgroup-unreads
5088                                     gnus-newsgroup-marked)
5089            gnus-newsgroup-dormant))
5090
5091     (setq gnus-newsgroup-processable nil)
5092
5093     (gnus-update-read-articles group gnus-newsgroup-unreads)
5094
5095     ;; Adjust and set lists of article marks.
5096     (when info
5097       (gnus-adjust-marked-articles info))
5098     (if (setq articles select-articles)
5099         (setq gnus-newsgroup-unselected
5100               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5101       (setq articles (gnus-articles-to-read group read-all)))
5102
5103     (cond
5104      ((null articles)
5105       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5106       'quit)
5107      ((eq articles 0) nil)
5108      (t
5109       ;; Init the dependencies hash table.
5110       (setq gnus-newsgroup-dependencies
5111             (gnus-make-hashtable (length articles)))
5112       (gnus-set-global-variables)
5113       ;; Retrieve the headers and read them in.
5114
5115       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5116
5117       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5118       (when cached
5119         (setq gnus-newsgroup-cached cached))
5120
5121       ;; Suppress duplicates?
5122       (when gnus-suppress-duplicates
5123         (gnus-dup-suppress-articles))
5124
5125       ;; Set the initial limit.
5126       (setq gnus-newsgroup-limit (copy-sequence articles))
5127       ;; Remove canceled articles from the list of unread articles.
5128       (setq fetched-articles
5129             (mapcar (lambda (headers) (mail-header-number headers))
5130                     gnus-newsgroup-headers))
5131       (setq gnus-newsgroup-articles fetched-articles)
5132       (setq gnus-newsgroup-unreads
5133             (gnus-sorted-nintersection
5134              gnus-newsgroup-unreads fetched-articles))
5135       (gnus-compute-unseen-list)
5136
5137       ;; Removed marked articles that do not exist.
5138       (gnus-update-missing-marks
5139        (gnus-sorted-difference articles fetched-articles))
5140       ;; We might want to build some more threads first.
5141       (when (and gnus-fetch-old-headers
5142                  (eq gnus-headers-retrieved-by 'nov))
5143         (if (eq gnus-fetch-old-headers 'invisible)
5144             (gnus-build-all-threads)
5145           (gnus-build-old-threads)))
5146       ;; Let the Gnus agent mark articles as read.
5147       (when gnus-agent
5148         (gnus-agent-get-undownloaded-list))
5149       ;; Remove list identifiers from subject
5150       (when gnus-list-identifiers
5151         (gnus-summary-remove-list-identifiers))
5152       ;; Check whether auto-expire is to be done in this group.
5153       (setq gnus-newsgroup-auto-expire
5154             (gnus-group-auto-expirable-p group))
5155       ;; Set up the article buffer now, if necessary.
5156       (unless gnus-single-article-buffer
5157         (gnus-article-setup-buffer))
5158       ;; First and last article in this newsgroup.
5159       (when gnus-newsgroup-headers
5160         (setq gnus-newsgroup-begin
5161               (mail-header-number (car gnus-newsgroup-headers))
5162               gnus-newsgroup-end
5163               (mail-header-number
5164                (gnus-last-element gnus-newsgroup-headers))))
5165       ;; GROUP is successfully selected.
5166       (or gnus-newsgroup-headers t)))))
5167
5168 (defun gnus-compute-unseen-list ()
5169   ;; The `seen' marks are treated specially.
5170   (if (not gnus-newsgroup-seen)
5171       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5172     (setq gnus-newsgroup-unseen
5173           (gnus-inverse-list-range-intersection
5174            gnus-newsgroup-articles gnus-newsgroup-seen))))
5175
5176 (defun gnus-summary-display-make-predicate (display)
5177   (require 'gnus-agent)
5178   (when (= (length display) 1)
5179     (setq display (car display)))
5180   (unless gnus-summary-display-cache
5181     (dolist (elem (append '((unread . unread)
5182                             (read . read)
5183                             (unseen . unseen))
5184                           gnus-article-mark-lists))
5185       (push (cons (cdr elem)
5186                   (gnus-byte-compile
5187                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5188             gnus-summary-display-cache)))
5189   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5190         (gnus-category-predicate-cache gnus-summary-display-cache))
5191     (gnus-get-predicate display)))
5192
5193 ;; Uses the dynamically bound `number' variable.
5194 (eval-when-compile
5195   (defvar number))
5196 (defun gnus-article-marked-p (type &optional article)
5197   (let ((article (or article number)))
5198     (cond
5199      ((eq type 'tick)
5200       (memq article gnus-newsgroup-marked))
5201      ((eq type 'spam)
5202       (memq article gnus-newsgroup-spam-marked))
5203      ((eq type 'unsend)
5204       (memq article gnus-newsgroup-unsendable))
5205      ((eq type 'undownload)
5206       (memq article gnus-newsgroup-undownloaded))
5207      ((eq type 'download)
5208       (memq article gnus-newsgroup-downloadable))
5209      ((eq type 'unread)
5210       (memq article gnus-newsgroup-unreads))
5211      ((eq type 'read)
5212       (memq article gnus-newsgroup-reads))
5213      ((eq type 'dormant)
5214       (memq article gnus-newsgroup-dormant) )
5215      ((eq type 'expire)
5216       (memq article gnus-newsgroup-expirable))
5217      ((eq type 'reply)
5218       (memq article gnus-newsgroup-replied))
5219      ((eq type 'killed)
5220       (memq article gnus-newsgroup-killed))
5221      ((eq type 'bookmark)
5222       (assq article gnus-newsgroup-bookmarks))
5223      ((eq type 'score)
5224       (assq article gnus-newsgroup-scored))
5225      ((eq type 'save)
5226       (memq article gnus-newsgroup-saved))
5227      ((eq type 'cache)
5228       (memq article gnus-newsgroup-cached))
5229      ((eq type 'forward)
5230       (memq article gnus-newsgroup-forwarded))
5231      ((eq type 'seen)
5232       (not (memq article gnus-newsgroup-unseen)))
5233      ((eq type 'recent)
5234       (memq article gnus-newsgroup-recent))
5235      (t t))))
5236
5237 (defun gnus-articles-to-read (group &optional read-all)
5238   "Find out what articles the user wants to read."
5239   (let* ((display (gnus-group-find-parameter group 'display))
5240          (articles
5241           ;; Select all articles if `read-all' is non-nil, or if there
5242           ;; are no unread articles.
5243           (if (or read-all
5244                   (and (zerop (length gnus-newsgroup-marked))
5245                        (zerop (length gnus-newsgroup-unreads)))
5246                   ;; Fetch all if the predicate is non-nil.
5247                   gnus-newsgroup-display)
5248               ;; We want to select the headers for all the articles in
5249               ;; the group, so we select either all the active
5250               ;; articles in the group, or (if that's nil), the
5251               ;; articles in the cache.
5252               (or
5253                (gnus-uncompress-range (gnus-active group))
5254                (gnus-cache-articles-in-group group))
5255             ;; Select only the "normal" subset of articles.
5256             (gnus-sorted-nunion
5257              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5258              gnus-newsgroup-unreads)))
5259          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5260          (scored (length scored-list))
5261          (number (length articles))
5262          (marked (+ (length gnus-newsgroup-marked)
5263                     (length gnus-newsgroup-dormant)))
5264          (select
5265           (cond
5266            ((numberp read-all)
5267             read-all)
5268            ((numberp gnus-newsgroup-display)
5269             gnus-newsgroup-display)
5270            (t
5271             (condition-case ()
5272                 (cond
5273                  ((and (or (<= scored marked) (= scored number))
5274                        (numberp gnus-large-newsgroup)
5275                        (> number gnus-large-newsgroup))
5276                   (let* ((cursor-in-echo-area nil)
5277                          (initial (gnus-parameter-large-newsgroup-initial
5278                                    gnus-newsgroup-name))
5279                          (input
5280                           (read-string
5281                            (format
5282                             "How many articles from %s (%s %d): "
5283                             (gnus-limit-string
5284                              (gnus-group-decoded-name gnus-newsgroup-name)
5285                              35)
5286                             (if initial "max" "default")
5287                             number)
5288                            (if initial
5289                                (cons (number-to-string initial)
5290                                      0)))))
5291                     (if (string-match "^[ \t]*$" input) number input)))
5292                  ((and (> scored marked) (< scored number)
5293                        (> (- scored number) 20))
5294                   (let ((input
5295                          (read-string
5296                           (format "%s %s (%d scored, %d total): "
5297                                   "How many articles from"
5298                                   (gnus-group-decoded-name group)
5299                                   scored number))))
5300                     (if (string-match "^[ \t]*$" input)
5301                         number input)))
5302                  (t number))
5303               (quit
5304                (message "Quit getting the articles to read")
5305                nil))))))
5306     (setq select (if (stringp select) (string-to-number select) select))
5307     (if (or (null select) (zerop select))
5308         select
5309       (if (and (not (zerop scored)) (<= (abs select) scored))
5310           (progn
5311             (setq articles (sort scored-list '<))
5312             (setq number (length articles)))
5313         (setq articles (copy-sequence articles)))
5314
5315       (when (< (abs select) number)
5316         (if (< select 0)
5317             ;; Select the N oldest articles.
5318             (setcdr (nthcdr (1- (abs select)) articles) nil)
5319           ;; Select the N most recent articles.
5320           (setq articles (nthcdr (- number select) articles))))
5321       (setq gnus-newsgroup-unselected
5322             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5323       (when gnus-alter-articles-to-read-function
5324         (setq articles
5325               (sort
5326                (funcall gnus-alter-articles-to-read-function
5327                         gnus-newsgroup-name articles)
5328                '<)))
5329       articles)))
5330
5331 (defun gnus-killed-articles (killed articles)
5332   (let (out)
5333     (while articles
5334       (when (inline (gnus-member-of-range (car articles) killed))
5335         (push (car articles) out))
5336       (setq articles (cdr articles)))
5337     out))
5338
5339 (defun gnus-uncompress-marks (marks)
5340   "Uncompress the mark ranges in MARKS."
5341   (let ((uncompressed '(score bookmark))
5342         out)
5343     (while marks
5344       (if (memq (caar marks) uncompressed)
5345           (push (car marks) out)
5346         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5347       (setq marks (cdr marks)))
5348     out))
5349
5350 (defun gnus-article-mark-to-type (mark)
5351   "Return the type of MARK."
5352   (or (cadr (assq mark gnus-article-special-mark-lists))
5353       'list))
5354
5355 (defun gnus-article-unpropagatable-p (mark)
5356   "Return whether MARK should be propagated to back end."
5357   (memq mark gnus-article-unpropagated-mark-lists))
5358
5359 (defun gnus-adjust-marked-articles (info)
5360   "Set all article lists and remove all marks that are no longer valid."
5361   (let* ((marked-lists (gnus-info-marks info))
5362          (active (gnus-active (gnus-info-group info)))
5363          (min (car active))
5364          (max (cdr active))
5365          (types gnus-article-mark-lists)
5366          marks var articles article mark mark-type)
5367
5368     (dolist (marks marked-lists)
5369       (setq mark (car marks)
5370             mark-type (gnus-article-mark-to-type mark)
5371             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5372
5373       ;; We set the variable according to the type of the marks list,
5374       ;; and then adjust the marks to a subset of the active articles.
5375       (cond
5376        ;; Adjust "simple" lists.
5377        ((eq mark-type 'list)
5378         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5379         (when (memq mark '(tick dormant expire reply save))
5380           (while articles
5381             (when (or (< (setq article (pop articles)) min) (> article max))
5382               (set var (delq article (symbol-value var)))))))
5383        ;; Adjust assocs.
5384        ((eq mark-type 'tuple)
5385         (set var (setq articles (cdr marks)))
5386         (when (not (listp (cdr (symbol-value var))))
5387           (set var (list (symbol-value var))))
5388         (when (not (listp (cdr articles)))
5389           (setq articles (list articles)))
5390         (while articles
5391           (when (or (not (consp (setq article (pop articles))))
5392                     (< (car article) min)
5393                     (> (car article) max))
5394             (set var (delq article (symbol-value var))))))
5395        ;; Adjust ranges (sloppily).
5396        ((eq mark-type 'range)
5397         (cond
5398          ((eq mark 'seen)
5399           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5400           ;; It should be (seen (NUM1 . NUM2)).
5401           (when (numberp (cddr marks))
5402             (setcdr marks (list (cdr marks))))
5403           (setq articles (cdr marks))
5404           (while (and articles
5405                       (or (and (consp (car articles))
5406                                (> min (cdar articles)))
5407                           (and (numberp (car articles))
5408                                (> min (car articles)))))
5409             (pop articles))
5410           (set var articles))))))))
5411
5412 (defun gnus-update-missing-marks (missing)
5413   "Go through the list of MISSING articles and remove them from the mark lists."
5414   (when missing
5415     (let (var m)
5416       ;; Go through all types.
5417       (dolist (elem gnus-article-mark-lists)
5418         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5419           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5420           (when (symbol-value var)
5421             ;; This list has articles.  So we delete all missing
5422             ;; articles from it.
5423             (setq m missing)
5424             (while m
5425               (set var (delq (pop m) (symbol-value var))))))))))
5426
5427 (defun gnus-update-marks ()
5428   "Enter the various lists of marked articles into the newsgroup info list."
5429   (let ((types gnus-article-mark-lists)
5430         (info (gnus-get-info gnus-newsgroup-name))
5431         type list newmarked symbol delta-marks)
5432     (when info
5433       ;; Add all marks lists to the list of marks lists.
5434       (while (setq type (pop types))
5435         (setq list (symbol-value
5436                     (setq symbol
5437                           (intern (format "gnus-newsgroup-%s" (car type))))))
5438
5439         (when list
5440           ;; Get rid of the entries of the articles that have the
5441           ;; default score.
5442           (when (and (eq (cdr type) 'score)
5443                      gnus-save-score
5444                      list)
5445             (let* ((arts list)
5446                    (prev (cons nil list))
5447                    (all prev))
5448               (while arts
5449                 (if (or (not (consp (car arts)))
5450                         (= (cdar arts) gnus-summary-default-score))
5451                     (setcdr prev (cdr arts))
5452                   (setq prev arts))
5453                 (setq arts (cdr arts)))
5454               (setq list (cdr all)))))
5455
5456         (when (eq (cdr type) 'seen)
5457           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5458
5459         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5460           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5461
5462         (when (and (gnus-check-backend-function
5463                     'request-set-mark gnus-newsgroup-name)
5464                    (not (gnus-article-unpropagatable-p (cdr type))))
5465           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5466                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5467                  (add (gnus-remove-from-range
5468                        (gnus-copy-sequence list) old)))
5469             (when add
5470               (push (list add 'add (list (cdr type))) delta-marks))
5471             (when del
5472               (push (list del 'del (list (cdr type))) delta-marks))))
5473
5474         (when list
5475           (push (cons (cdr type) list) newmarked)))
5476
5477       (when delta-marks
5478         (unless (gnus-check-group gnus-newsgroup-name)
5479           (error "Can't open server for %s" gnus-newsgroup-name))
5480         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5481
5482       ;; Enter these new marks into the info of the group.
5483       (if (nthcdr 3 info)
5484           (setcar (nthcdr 3 info) newmarked)
5485         ;; Add the marks lists to the end of the info.
5486         (when newmarked
5487           (setcdr (nthcdr 2 info) (list newmarked))))
5488
5489       ;; Cut off the end of the info if there's nothing else there.
5490       (let ((i 5))
5491         (while (and (> i 2)
5492                     (not (nth i info)))
5493           (when (nthcdr (decf i) info)
5494             (setcdr (nthcdr i info) nil)))))))
5495
5496 (defun gnus-set-mode-line (where)
5497   "Set the mode line of the article or summary buffers.
5498 If WHERE is `summary', the summary mode line format will be used."
5499   ;; Is this mode line one we keep updated?
5500   (when (and (memq where gnus-updated-mode-lines)
5501              (symbol-value
5502               (intern (format "gnus-%s-mode-line-format-spec" where))))
5503     (let (mode-string)
5504       (save-excursion
5505         ;; We evaluate this in the summary buffer since these
5506         ;; variables are buffer-local to that buffer.
5507         (set-buffer gnus-summary-buffer)
5508         ;; We bind all these variables that are used in the `eval' form
5509         ;; below.
5510         (let* ((mformat (symbol-value
5511                          (intern
5512                           (format "gnus-%s-mode-line-format-spec" where))))
5513                (gnus-tmp-group-name (gnus-group-decoded-name
5514                                      gnus-newsgroup-name))
5515                (gnus-tmp-article-number (or gnus-current-article 0))
5516                (gnus-tmp-unread gnus-newsgroup-unreads)
5517                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5518                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5519                (gnus-tmp-unread-and-unselected
5520                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5521                             (zerop gnus-tmp-unselected))
5522                        "")
5523                       ((zerop gnus-tmp-unselected)
5524                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5525                       (t (format "{%d(+%d) more}"
5526                                  gnus-tmp-unread-and-unticked
5527                                  gnus-tmp-unselected))))
5528                (gnus-tmp-subject
5529                 (if (and gnus-current-headers
5530                          (vectorp gnus-current-headers))
5531                     (gnus-mode-string-quote
5532                      (mail-header-subject gnus-current-headers))
5533                   ""))
5534                bufname-length max-len
5535                gnus-tmp-header);; passed as argument to any user-format-funcs
5536           (setq mode-string (eval mformat))
5537           (setq bufname-length (if (string-match "%b" mode-string)
5538                                    (- (length
5539                                        (buffer-name
5540                                         (if (eq where 'summary)
5541                                             nil
5542                                           (get-buffer gnus-article-buffer))))
5543                                       2)
5544                                  0))
5545           (setq max-len (max 4 (if gnus-mode-non-string-length
5546                                    (- (window-width)
5547                                       gnus-mode-non-string-length
5548                                       bufname-length)
5549                                  (length mode-string))))
5550           ;; We might have to chop a bit of the string off...
5551           (when (> (length mode-string) max-len)
5552             (setq mode-string
5553                   (concat (gnus-truncate-string mode-string (- max-len 3))
5554                           "...")))
5555           ;; Pad the mode string a bit.
5556           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5557       ;; Update the mode line.
5558       (setq mode-line-buffer-identification
5559             (gnus-mode-line-buffer-identification (list mode-string)))
5560       (set-buffer-modified-p t))))
5561
5562 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5563   "Go through the HEADERS list and add all Xrefs to a hash table.
5564 The resulting hash table is returned, or nil if no Xrefs were found."
5565   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5566          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5567          (xref-hashtb (gnus-make-hashtable))
5568          start group entry number xrefs header)
5569     (while headers
5570       (setq header (pop headers))
5571       (when (and (setq xrefs (mail-header-xref header))
5572                  (not (memq (setq number (mail-header-number header))
5573                             unreads)))
5574         (setq start 0)
5575         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5576           (setq start (match-end 0))
5577           (setq group (if prefix
5578                           (concat prefix (substring xrefs (match-beginning 1)
5579                                                     (match-end 1)))
5580                         (substring xrefs (match-beginning 1) (match-end 1))))
5581           (setq number
5582                 (string-to-int (substring xrefs (match-beginning 2)
5583                                           (match-end 2))))
5584           (if (setq entry (gnus-gethash group xref-hashtb))
5585               (setcdr entry (cons number (cdr entry)))
5586             (gnus-sethash group (cons number nil) xref-hashtb)))))
5587     (and start xref-hashtb)))
5588
5589 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5590   "Look through all the headers and mark the Xrefs as read."
5591   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5592         name entry info xref-hashtb idlist method nth4)
5593     (save-excursion
5594       (set-buffer gnus-group-buffer)
5595       (when (setq xref-hashtb
5596                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5597         (mapatoms
5598          (lambda (group)
5599            (unless (string= from-newsgroup (setq name (symbol-name group)))
5600              (setq idlist (symbol-value group))
5601              ;; Dead groups are not updated.
5602              (and (prog1
5603                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5604                             info (nth 2 entry))
5605                     (when (stringp (setq nth4 (gnus-info-method info)))
5606                       (setq nth4 (gnus-server-to-method nth4))))
5607                   ;; Only do the xrefs if the group has the same
5608                   ;; select method as the group we have just read.
5609                   (or (gnus-methods-equal-p
5610                        nth4 (gnus-find-method-for-group from-newsgroup))
5611                       virtual
5612                       (equal nth4 (setq method (gnus-find-method-for-group
5613                                                 from-newsgroup)))
5614                       (and (equal (car nth4) (car method))
5615                            (equal (nth 1 nth4) (nth 1 method))))
5616                   gnus-use-cross-reference
5617                   (or (not (eq gnus-use-cross-reference t))
5618                       virtual
5619                       ;; Only do cross-references on subscribed
5620                       ;; groups, if that is what is wanted.
5621                       (<= (gnus-info-level info) gnus-level-subscribed))
5622                   (gnus-group-make-articles-read name idlist))))
5623          xref-hashtb)))))
5624
5625 (defun gnus-compute-read-articles (group articles)
5626   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5627          (info (nth 2 entry))
5628          (active (gnus-active group))
5629          ninfo)
5630     (when entry
5631       ;; First peel off all invalid article numbers.
5632       (when active
5633         (let ((ids articles)
5634               id first)
5635           (while (setq id (pop ids))
5636             (when (and first (> id (cdr active)))
5637               ;; We'll end up in this situation in one particular
5638               ;; obscure situation.  If you re-scan a group and get
5639               ;; a new article that is cross-posted to a different
5640               ;; group that has not been re-scanned, you might get
5641               ;; crossposted article that has a higher number than
5642               ;; Gnus believes possible.  So we re-activate this
5643               ;; group as well.  This might mean doing the
5644               ;; crossposting thingy will *increase* the number
5645               ;; of articles in some groups.  Tsk, tsk.
5646               (setq active (or (gnus-activate-group group) active)))
5647             (when (or (> id (cdr active))
5648                       (< id (car active)))
5649               (setq articles (delq id articles))))))
5650       ;; If the read list is nil, we init it.
5651       (if (and active
5652                (null (gnus-info-read info))
5653                (> (car active) 1))
5654           (setq ninfo (cons 1 (1- (car active))))
5655         (setq ninfo (gnus-info-read info)))
5656       ;; Then we add the read articles to the range.
5657       (gnus-add-to-range
5658        ninfo (setq articles (sort articles '<))))))
5659
5660 (defun gnus-group-make-articles-read (group articles)
5661   "Update the info of GROUP to say that ARTICLES are read."
5662   (let* ((num 0)
5663          (entry (gnus-gethash group gnus-newsrc-hashtb))
5664          (info (nth 2 entry))
5665          (active (gnus-active group))
5666          range)
5667     (when entry
5668       (setq range (gnus-compute-read-articles group articles))
5669       (save-excursion
5670         (set-buffer gnus-group-buffer)
5671         (gnus-undo-register
5672           `(progn
5673              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5674              (gnus-info-set-read ',info ',(gnus-info-read info))
5675              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5676              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5677              (gnus-group-update-group ,group t))))
5678       ;; Add the read articles to the range.
5679       (gnus-info-set-read info range)
5680       (gnus-request-set-mark group (list (list range 'add '(read))))
5681       ;; Then we have to re-compute how many unread
5682       ;; articles there are in this group.
5683       (when active
5684         (cond
5685          ((not range)
5686           (setq num (- (1+ (cdr active)) (car active))))
5687          ((not (listp (cdr range)))
5688           (setq num (- (cdr active) (- (1+ (cdr range))
5689                                        (car range)))))
5690          (t
5691           (while range
5692             (if (numberp (car range))
5693                 (setq num (1+ num))
5694               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5695             (setq range (cdr range)))
5696           (setq num (- (cdr active) num))))
5697         ;; Update the number of unread articles.
5698         (setcar entry num)
5699         ;; Update the group buffer.
5700         (unless (gnus-ephemeral-group-p group)
5701           (gnus-group-update-group group t))))))
5702
5703 (defvar gnus-newsgroup-none-id 0)
5704
5705 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5706   (let ((cur nntp-server-buffer)
5707         (dependencies
5708          (or dependencies
5709              (save-excursion (set-buffer gnus-summary-buffer)
5710                              gnus-newsgroup-dependencies)))
5711         headers id end ref
5712         (mail-parse-charset gnus-newsgroup-charset)
5713         (mail-parse-ignored-charsets
5714          (save-excursion (condition-case nil
5715                              (set-buffer gnus-summary-buffer)
5716                            (error))
5717                          gnus-newsgroup-ignored-charsets)))
5718     (save-excursion
5719       (set-buffer nntp-server-buffer)
5720       ;; Translate all TAB characters into SPACE characters.
5721       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5722       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5723       (ietf-drums-unfold-fws)
5724       (gnus-run-hooks 'gnus-parse-headers-hook)
5725       (let ((case-fold-search t)
5726             in-reply-to header p lines chars ctype)
5727         (goto-char (point-min))
5728         ;; Search to the beginning of the next header.  Error messages
5729         ;; do not begin with 2 or 3.
5730         (while (re-search-forward "^[23][0-9]+ " nil t)
5731           (setq id nil
5732                 ref nil)
5733           ;; This implementation of this function, with nine
5734           ;; search-forwards instead of the one re-search-forward and
5735           ;; a case (which basically was the old function) is actually
5736           ;; about twice as fast, even though it looks messier.  You
5737           ;; can't have everything, I guess.  Speed and elegance
5738           ;; doesn't always go hand in hand.
5739           (setq
5740            header
5741            (make-full-mail-header
5742             ;; Number.
5743             (prog1
5744                 (read cur)
5745               (end-of-line)
5746               (setq p (point))
5747               (narrow-to-region (point)
5748                                 (or (and (search-forward "\n.\n" nil t)
5749                                          (- (point) 2))
5750                                     (point))))
5751             ;; Subject.
5752             (progn
5753               (goto-char p)
5754               (if (search-forward "\nsubject:" nil t)
5755                   (nnheader-header-value)
5756                 "(none)"))
5757             ;; From.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\nfrom:" nil t)
5761                   (nnheader-header-value)
5762                 "(nobody)"))
5763             ;; Date.
5764             (progn
5765               (goto-char p)
5766               (if (search-forward "\ndate:" nil t)
5767                   (nnheader-header-value) ""))
5768             ;; Message-ID.
5769             (progn
5770               (goto-char p)
5771               (setq id (if (re-search-forward
5772                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5773                            ;; We do it this way to make sure the Message-ID
5774                            ;; is (somewhat) syntactically valid.
5775                            (buffer-substring (match-beginning 1)
5776                                              (match-end 1))
5777                          ;; If there was no message-id, we just fake one
5778                          ;; to make subsequent routines simpler.
5779                          (nnheader-generate-fake-message-id))))
5780             ;; References.
5781             (progn
5782               (goto-char p)
5783               (if (search-forward "\nreferences:" nil t)
5784                   (progn
5785                     (setq end (point))
5786                     (prog1
5787                         (nnheader-header-value)
5788                       (setq ref
5789                             (buffer-substring
5790                              (progn
5791                                ;; (end-of-line)
5792                                (search-backward ">" end t)
5793                                (1+ (point)))
5794                              (progn
5795                                (search-backward "<" end t)
5796                                (point))))))
5797                 ;; Get the references from the in-reply-to header if there
5798                 ;; were no references and the in-reply-to header looks
5799                 ;; promising.
5800                 (if (and (search-forward "\nin-reply-to:" nil t)
5801                          (setq in-reply-to (nnheader-header-value))
5802                          (string-match "<[^>]+>" in-reply-to))
5803                     (let (ref2)
5804                       (setq ref (substring in-reply-to (match-beginning 0)
5805                                            (match-end 0)))
5806                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5807                         (setq ref2 (substring in-reply-to (match-beginning 0)
5808                                               (match-end 0)))
5809                         (when (> (length ref2) (length ref))
5810                           (setq ref ref2)))
5811                       ref)
5812                   (setq ref nil))))
5813             ;; Chars.
5814             (progn
5815               (goto-char p)
5816               (if (search-forward "\nchars: " nil t)
5817                   (if (numberp (setq chars (ignore-errors (read cur))))
5818                       chars -1)
5819                 -1))
5820             ;; Lines.
5821             (progn
5822               (goto-char p)
5823               (if (search-forward "\nlines: " nil t)
5824                   (if (numberp (setq lines (ignore-errors (read cur))))
5825                       lines -1)
5826                 -1))
5827             ;; Xref.
5828             (progn
5829               (goto-char p)
5830               (and (search-forward "\nxref:" nil t)
5831                    (nnheader-header-value)))
5832             ;; Extra.
5833             (when gnus-extra-headers
5834               (let ((extra gnus-extra-headers)
5835                     out)
5836                 (while extra
5837                   (goto-char p)
5838                   (when (search-forward
5839                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5840                     (push (cons (car extra) (nnheader-header-value)) out))
5841                   (pop extra))
5842                 out))))
5843           (goto-char p)
5844           (if (and (search-forward "\ncontent-type: " nil t)
5845                    (setq ctype (nnheader-header-value)))
5846               (mime-entity-set-content-type-internal
5847                header (mime-parse-Content-Type ctype)))
5848           (when (equal id ref)
5849             (setq ref nil))
5850
5851           (when gnus-alter-header-function
5852             (funcall gnus-alter-header-function header)
5853             (setq id (mail-header-id header)
5854                   ref (gnus-parent-id (mail-header-references header))))
5855
5856           (when (setq header
5857                       (gnus-dependencies-add-header
5858                        header dependencies force-new))
5859             (push header headers))
5860           (goto-char (point-max))
5861           (widen))
5862         (nreverse headers)))))
5863
5864 ;; Goes through the xover lines and returns a list of vectors
5865 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5866                                                   force-new dependencies
5867                                                   group also-fetch-heads)
5868   "Parse the news overview data in the server buffer.
5869 Return a list of headers that match SEQUENCE (see
5870 `nntp-retrieve-headers')."
5871   ;; Get the Xref when the users reads the articles since most/some
5872   ;; NNTP servers do not include Xrefs when using XOVER.
5873   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5874   (let ((mail-parse-charset gnus-newsgroup-charset)
5875         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5876         (cur nntp-server-buffer)
5877         (dependencies (or dependencies gnus-newsgroup-dependencies))
5878         (allp (cond
5879                ((eq gnus-read-all-available-headers t)
5880                 t)
5881                ((stringp gnus-read-all-available-headers)
5882                 (string-match gnus-read-all-available-headers group))
5883                (t
5884                 nil)))
5885         number headers header)
5886     (save-excursion
5887       (set-buffer nntp-server-buffer)
5888       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5889       ;; Allow the user to mangle the headers before parsing them.
5890       (gnus-run-hooks 'gnus-parse-headers-hook)
5891       (goto-char (point-min))
5892       (gnus-parse-without-error
5893         (while (and (or sequence allp)
5894                     (not (eobp)))
5895           (setq number (read cur))
5896           (when (not allp)
5897             (while (and sequence
5898                         (< (car sequence) number))
5899               (setq sequence (cdr sequence))))
5900           (when (and (or allp
5901                          (and sequence
5902                               (eq number (car sequence))))
5903                      (progn
5904                        (setq sequence (cdr sequence))
5905                        (setq header (inline
5906                                       (gnus-nov-parse-line
5907                                        number dependencies force-new)))))
5908             (push header headers))
5909           (forward-line 1)))
5910       ;; A common bug in inn is that if you have posted an article and
5911       ;; then retrieves the active file, it will answer correctly --
5912       ;; the new article is included.  However, a NOV entry for the
5913       ;; article may not have been generated yet, so this may fail.
5914       ;; We work around this problem by retrieving the last few
5915       ;; headers using HEAD.
5916       (if (or (not also-fetch-heads)
5917               (not sequence))
5918           ;; We (probably) got all the headers.
5919           (nreverse headers)
5920         (let ((gnus-nov-is-evil t))
5921           (nconc
5922            (nreverse headers)
5923            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5924              (gnus-get-newsgroup-headers))))))))
5925
5926 (defun gnus-article-get-xrefs ()
5927   "Fill in the Xref value in `gnus-current-headers', if necessary.
5928 This is meant to be called in `gnus-article-internal-prepare-hook'."
5929   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5930                                  gnus-current-headers)))
5931     (or (not gnus-use-cross-reference)
5932         (not headers)
5933         (and (mail-header-xref headers)
5934              (not (string= (mail-header-xref headers) "")))
5935         (let ((case-fold-search t)
5936               xref)
5937           (save-restriction
5938             (nnheader-narrow-to-headers)
5939             (goto-char (point-min))
5940             (when (or (and (not (eobp))
5941                            (eq (downcase (char-after)) ?x)
5942                            (looking-at "Xref:"))
5943                       (search-forward "\nXref:" nil t))
5944               (goto-char (1+ (match-end 0)))
5945               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5946               (mail-header-set-xref headers xref)))))))
5947
5948 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5949   "Find article ID and insert the summary line for that article.
5950 OLD-HEADER can either be a header or a line number to insert
5951 the subject line on."
5952   (let* ((line (and (numberp old-header) old-header))
5953          (old-header (and (vectorp old-header) old-header))
5954          (header (cond ((and old-header use-old-header)
5955                         old-header)
5956                        ((and (numberp id)
5957                              (gnus-number-to-header id))
5958                         (gnus-number-to-header id))
5959                        (t
5960                         (gnus-read-header id))))
5961          (number (and (numberp id) id))
5962          d)
5963     (when header
5964       ;; Rebuild the thread that this article is part of and go to the
5965       ;; article we have fetched.
5966       (when (and (not gnus-show-threads)
5967                  old-header)
5968         (when (and number
5969                    (setq d (gnus-data-find (mail-header-number old-header))))
5970           (goto-char (gnus-data-pos d))
5971           (gnus-data-remove
5972            number
5973            (- (gnus-point-at-bol)
5974               (prog1
5975                   (1+ (gnus-point-at-eol))
5976                 (gnus-delete-line))))))
5977       (when old-header
5978         (mail-header-set-number header (mail-header-number old-header)))
5979       (setq gnus-newsgroup-sparse
5980             (delq (setq number (mail-header-number header))
5981                   gnus-newsgroup-sparse))
5982       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5983       (push number gnus-newsgroup-limit)
5984       (gnus-rebuild-thread (mail-header-id header) line)
5985       (gnus-summary-goto-subject number nil t))
5986     (when (and (numberp number)
5987                (> number 0))
5988       ;; We have to update the boundaries even if we can't fetch the
5989       ;; article if ID is a number -- so that the next `P' or `N'
5990       ;; command will fetch the previous (or next) article even
5991       ;; if the one we tried to fetch this time has been canceled.
5992       (when (> number gnus-newsgroup-end)
5993         (setq gnus-newsgroup-end number))
5994       (when (< number gnus-newsgroup-begin)
5995         (setq gnus-newsgroup-begin number))
5996       (setq gnus-newsgroup-unselected
5997             (delq number gnus-newsgroup-unselected)))
5998     ;; Report back a success?
5999     (and header (mail-header-number header))))
6000
6001 ;;; Process/prefix in the summary buffer
6002
6003 (defun gnus-summary-work-articles (n)
6004   "Return a list of articles to be worked upon.
6005 The prefix argument, the list of process marked articles, and the
6006 current article will be taken into consideration."
6007   (save-excursion
6008     (set-buffer gnus-summary-buffer)
6009     (cond
6010      (n
6011       ;; A numerical prefix has been given.
6012       (setq n (prefix-numeric-value n))
6013       (let ((backward (< n 0))
6014             (n (abs (prefix-numeric-value n)))
6015             articles article)
6016         (save-excursion
6017           (while
6018               (and (> n 0)
6019                    (push (setq article (gnus-summary-article-number))
6020                          articles)
6021                    (if backward
6022                        (gnus-summary-find-prev nil article)
6023                      (gnus-summary-find-next nil article)))
6024             (decf n)))
6025         (nreverse articles)))
6026      ((and (gnus-region-active-p) (mark))
6027       (message "region active")
6028       ;; Work on the region between point and mark.
6029       (let ((max (max (point) (mark)))
6030             articles article)
6031         (save-excursion
6032           (goto-char (min (point) (mark)))
6033           (while
6034               (and
6035                (push (setq article (gnus-summary-article-number)) articles)
6036                (gnus-summary-find-next nil article)
6037                (< (point) max)))
6038           (nreverse articles))))
6039      (gnus-newsgroup-processable
6040       ;; There are process-marked articles present.
6041       ;; Save current state.
6042       (gnus-summary-save-process-mark)
6043       ;; Return the list.
6044       (reverse gnus-newsgroup-processable))
6045      (t
6046       ;; Just return the current article.
6047       (list (gnus-summary-article-number))))))
6048
6049 (defmacro gnus-summary-iterate (arg &rest forms)
6050   "Iterate over the process/prefixed articles and do FORMS.
6051 ARG is the interactive prefix given to the command.  FORMS will be
6052 executed with point over the summary line of the articles."
6053   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6054     `(let ((,articles (gnus-summary-work-articles ,arg)))
6055        (while ,articles
6056          (gnus-summary-goto-subject (car ,articles))
6057          ,@forms
6058          (pop ,articles)))))
6059
6060 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6061 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6062
6063 (defun gnus-summary-save-process-mark ()
6064   "Push the current set of process marked articles on the stack."
6065   (interactive)
6066   (push (copy-sequence gnus-newsgroup-processable)
6067         gnus-newsgroup-process-stack))
6068
6069 (defun gnus-summary-kill-process-mark ()
6070   "Push the current set of process marked articles on the stack and unmark."
6071   (interactive)
6072   (gnus-summary-save-process-mark)
6073   (gnus-summary-unmark-all-processable))
6074
6075 (defun gnus-summary-yank-process-mark ()
6076   "Pop the last process mark state off the stack and restore it."
6077   (interactive)
6078   (unless gnus-newsgroup-process-stack
6079     (error "Empty mark stack"))
6080   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6081
6082 (defun gnus-summary-process-mark-set (set)
6083   "Make SET into the current process marked articles."
6084   (gnus-summary-unmark-all-processable)
6085   (while set
6086     (gnus-summary-set-process-mark (pop set))))
6087
6088 ;;; Searching and stuff
6089
6090 (defun gnus-summary-search-group (&optional backward use-level)
6091   "Search for next unread newsgroup.
6092 If optional argument BACKWARD is non-nil, search backward instead."
6093   (save-excursion
6094     (set-buffer gnus-group-buffer)
6095     (when (gnus-group-search-forward
6096            backward nil (if use-level (gnus-group-group-level) nil))
6097       (gnus-group-group-name))))
6098
6099 (defun gnus-summary-best-group (&optional exclude-group)
6100   "Find the name of the best unread group.
6101 If EXCLUDE-GROUP, do not go to this group."
6102   (save-excursion
6103     (set-buffer gnus-group-buffer)
6104     (save-excursion
6105       (gnus-group-best-unread-group exclude-group))))
6106
6107 (defun gnus-summary-find-next (&optional unread article backward)
6108   (if backward
6109       (gnus-summary-find-prev unread article)
6110     (let* ((dummy (gnus-summary-article-intangible-p))
6111            (article (or article (gnus-summary-article-number)))
6112            (data (gnus-data-find-list article))
6113            result)
6114       (when (and (not dummy)
6115                  (or (not gnus-summary-check-current)
6116                      (not unread)
6117                      (not (gnus-data-unread-p (car data)))))
6118         (setq data (cdr data)))
6119       (when (setq result
6120                   (if unread
6121                       (progn
6122                         (while data
6123                           (unless (memq (gnus-data-number (car data))
6124                                         (cond
6125                                          ((eq gnus-auto-goto-ignores
6126                                               'always-undownloaded)
6127                                           gnus-newsgroup-undownloaded)
6128                                          (gnus-plugged
6129                                           nil)
6130                                          ((eq gnus-auto-goto-ignores
6131                                               'unfetched)
6132                                           gnus-newsgroup-unfetched)
6133                                          ((eq gnus-auto-goto-ignores
6134                                               'undownloaded)
6135                                           gnus-newsgroup-undownloaded)))
6136                             (when (gnus-data-unread-p (car data))
6137                               (setq result (car data)
6138                                     data nil)))
6139                           (setq data (cdr data)))
6140                         result)
6141                     (car data)))
6142         (goto-char (gnus-data-pos result))
6143         (gnus-data-number result)))))
6144
6145 (defun gnus-summary-find-prev (&optional unread article)
6146   (let* ((eobp (eobp))
6147          (article (or article (gnus-summary-article-number)))
6148          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6149          result)
6150     (when (and (not eobp)
6151                (or (not gnus-summary-check-current)
6152                    (not unread)
6153                    (not (gnus-data-unread-p (car data)))))
6154       (setq data (cdr data)))
6155     (when (setq result
6156                 (if unread
6157                     (progn
6158                       (while data
6159                         (unless (memq (gnus-data-number (car data))
6160                                       (cond
6161                                        ((eq gnus-auto-goto-ignores
6162                                             'always-undownloaded)
6163                                         gnus-newsgroup-undownloaded)
6164                                        (gnus-plugged
6165                                         nil)
6166                                        ((eq gnus-auto-goto-ignores
6167                                             'unfetched)
6168                                         gnus-newsgroup-unfetched)
6169                                        ((eq gnus-auto-goto-ignores
6170                                             'undownloaded)
6171                                         gnus-newsgroup-undownloaded)))
6172                           (when (gnus-data-unread-p (car data))
6173                             (setq result (car data)
6174                                   data nil)))
6175                         (setq data (cdr data)))
6176                       result)
6177                   (car data)))
6178       (goto-char (gnus-data-pos result))
6179       (gnus-data-number result))))
6180
6181 (defun gnus-summary-find-subject (subject &optional unread backward article)
6182   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6183          (article (or article (gnus-summary-article-number)))
6184          (articles (gnus-data-list backward))
6185          (arts (gnus-data-find-list article articles))
6186          result)
6187     (when (or (not gnus-summary-check-current)
6188               (not unread)
6189               (not (gnus-data-unread-p (car arts))))
6190       (setq arts (cdr arts)))
6191     (while arts
6192       (and (or (not unread)
6193                (gnus-data-unread-p (car arts)))
6194            (vectorp (gnus-data-header (car arts)))
6195            (gnus-subject-equal
6196             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6197            (setq result (car arts)
6198                  arts nil))
6199       (setq arts (cdr arts)))
6200     (and result
6201          (goto-char (gnus-data-pos result))
6202          (gnus-data-number result))))
6203
6204 (defun gnus-summary-search-forward (&optional unread subject backward)
6205   "Search forward for an article.
6206 If UNREAD, look for unread articles.  If SUBJECT, look for
6207 articles with that subject.  If BACKWARD, search backward instead."
6208   (cond (subject (gnus-summary-find-subject subject unread backward))
6209         (backward (gnus-summary-find-prev unread))
6210         (t (gnus-summary-find-next unread))))
6211
6212 (defun gnus-recenter (&optional n)
6213   "Center point in window and redisplay frame.
6214 Also do horizontal recentering."
6215   (interactive "P")
6216   (when (and gnus-auto-center-summary
6217              (not (eq gnus-auto-center-summary 'vertical)))
6218     (gnus-horizontal-recenter))
6219   (recenter n))
6220
6221 (defun gnus-summary-recenter ()
6222   "Center point in the summary window.
6223 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6224 displayed, no centering will be performed."
6225   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6226   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6227   (interactive)
6228   ;; The user has to want it.
6229   (when gnus-auto-center-summary
6230     (let* ((top (cond ((< (window-height) 4) 0)
6231                       ((< (window-height) 7) 1)
6232                       (t (if (numberp gnus-auto-center-summary)
6233                              gnus-auto-center-summary
6234                            2))))
6235            (height (1- (window-height)))
6236            (bottom (save-excursion (goto-char (point-max))
6237                                    (forward-line (- height))
6238                                    (point)))
6239            (window (get-buffer-window (current-buffer))))
6240       (when (get-buffer-window gnus-article-buffer)
6241         ;; Only do recentering when the article buffer is displayed,
6242         ;; Set the window start to either `bottom', which is the biggest
6243         ;; possible valid number, or the second line from the top,
6244         ;; whichever is the least.
6245         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6246           (if (> bottom top-pos)
6247               ;; Keep the second line from the top visible
6248               (set-window-start window top-pos t)
6249             ;; Try to keep the bottom line visible; if it's partially
6250             ;; obscured, either scroll one more line to make it fully
6251             ;; visible, or revert to using TOP-POS.
6252             (save-excursion
6253               (goto-char (point-max))
6254               (forward-line -1)
6255               (let ((last-line-start (point)))
6256                 (goto-char bottom)
6257                 (set-window-start window (point) t)
6258                 (when (not (pos-visible-in-window-p last-line-start window))
6259                   (forward-line 1)
6260                   (set-window-start window (min (point) top-pos) t)))))))
6261       ;; Do horizontal recentering while we're at it.
6262       (when (and (get-buffer-window (current-buffer) t)
6263                  (not (eq gnus-auto-center-summary 'vertical)))
6264         (let ((selected (selected-window)))
6265           (select-window (get-buffer-window (current-buffer) t))
6266           (gnus-summary-position-point)
6267           (gnus-horizontal-recenter)
6268           (select-window selected))))))
6269
6270 (defun gnus-summary-jump-to-group (newsgroup)
6271   "Move point to NEWSGROUP in group mode buffer."
6272   ;; Keep update point of group mode buffer if visible.
6273   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6274       (save-window-excursion
6275         ;; Take care of tree window mode.
6276         (when (get-buffer-window gnus-group-buffer)
6277           (pop-to-buffer gnus-group-buffer))
6278         (gnus-group-jump-to-group newsgroup))
6279     (save-excursion
6280       ;; Take care of tree window mode.
6281       (if (get-buffer-window gnus-group-buffer)
6282           (pop-to-buffer gnus-group-buffer)
6283         (set-buffer gnus-group-buffer))
6284       (gnus-group-jump-to-group newsgroup))))
6285
6286 ;; This function returns a list of article numbers based on the
6287 ;; difference between the ranges of read articles in this group and
6288 ;; the range of active articles.
6289 (defun gnus-list-of-unread-articles (group)
6290   (let* ((read (gnus-info-read (gnus-get-info group)))
6291          (active (or (gnus-active group) (gnus-activate-group group)))
6292          (last (cdr active))
6293          first nlast unread)
6294     ;; If none are read, then all are unread.
6295     (if (not read)
6296         (setq first (car active))
6297       ;; If the range of read articles is a single range, then the
6298       ;; first unread article is the article after the last read
6299       ;; article.  Sounds logical, doesn't it?
6300       (if (and (not (listp (cdr read)))
6301                (or (< (car read) (car active))
6302                    (progn (setq read (list read))
6303                           nil)))
6304           (setq first (max (car active) (1+ (cdr read))))
6305         ;; `read' is a list of ranges.
6306         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6307                                   (caar read)))
6308                   1)
6309           (setq first (car active)))
6310         (while read
6311           (when first
6312             (while (< first nlast)
6313               (push first unread)
6314               (setq first (1+ first))))
6315           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6316           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6317           (setq read (cdr read)))))
6318     ;; And add the last unread articles.
6319     (while (<= first last)
6320       (push first unread)
6321       (setq first (1+ first)))
6322     ;; Return the list of unread articles.
6323     (delq 0 (nreverse unread))))
6324
6325 (defun gnus-list-of-read-articles (group)
6326   "Return a list of unread, unticked and non-dormant articles."
6327   (let* ((info (gnus-get-info group))
6328          (marked (gnus-info-marks info))
6329          (active (gnus-active group)))
6330     (and info active
6331          (gnus-list-range-difference
6332           (gnus-list-range-difference
6333            (gnus-sorted-complement
6334             (gnus-uncompress-range active)
6335             (gnus-list-of-unread-articles group))
6336            (cdr (assq 'dormant marked)))
6337           (cdr (assq 'tick marked))))))
6338
6339 ;; Various summary commands
6340
6341 (defun gnus-summary-select-article-buffer ()
6342   "Reconfigure windows to show article buffer."
6343   (interactive)
6344   (if (not (gnus-buffer-live-p gnus-article-buffer))
6345       (error "There is no article buffer for this summary buffer")
6346     (gnus-configure-windows 'article)
6347     (select-window (get-buffer-window gnus-article-buffer))))
6348
6349 (defun gnus-summary-universal-argument (arg)
6350   "Perform any operation on all articles that are process/prefixed."
6351   (interactive "P")
6352   (let ((articles (gnus-summary-work-articles arg))
6353         func article)
6354     (if (eq
6355          (setq
6356           func
6357           (key-binding
6358            (read-key-sequence
6359             (substitute-command-keys
6360              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6361          'undefined)
6362         (gnus-error 1 "Undefined key")
6363       (save-excursion
6364         (while articles
6365           (gnus-summary-goto-subject (setq article (pop articles)))
6366           (let (gnus-newsgroup-processable)
6367             (command-execute func))
6368           (gnus-summary-remove-process-mark article)))))
6369   (gnus-summary-position-point))
6370
6371 (defun gnus-summary-toggle-truncation (&optional arg)
6372   "Toggle truncation of summary lines.
6373 With ARG, turn line truncation on if ARG is positive."
6374   (interactive "P")
6375   (setq truncate-lines
6376         (if (null arg) (not truncate-lines)
6377           (> (prefix-numeric-value arg) 0)))
6378   (redraw-display))
6379
6380 (defun gnus-summary-find-for-reselect ()
6381   "Return the number of an article to stay on across a reselect.
6382 The current article is considered, then following articles, then previous
6383 articles.  An article is sought which is not cancelled and isn't a temporary
6384 insertion from another group.  If there's no such then return a dummy 0."
6385   (let (found)
6386     (dolist (rev '(nil t))
6387       (unless found      ; don't demand the reverse list if we don't need it
6388         (let ((data (gnus-data-find-list
6389                      (gnus-summary-article-number) (gnus-data-list rev))))
6390           (while (and data (not found))
6391             (if (and (< 0 (gnus-data-number (car data)))
6392                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6393                 (setq found (gnus-data-number (car data))))
6394             (setq data (cdr data))))))
6395     (or found 0)))
6396
6397 (defun gnus-summary-reselect-current-group (&optional all rescan)
6398   "Exit and then reselect the current newsgroup.
6399 The prefix argument ALL means to select all articles."
6400   (interactive "P")
6401   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6402     (error "Ephemeral groups can't be reselected"))
6403   (let ((current-subject (gnus-summary-find-for-reselect))
6404         (group gnus-newsgroup-name))
6405     (setq gnus-newsgroup-begin nil)
6406     (gnus-summary-exit nil 'leave-hidden)
6407     ;; We have to adjust the point of group mode buffer because
6408     ;; point was moved to the next unread newsgroup by exiting.
6409     (gnus-summary-jump-to-group group)
6410     (when rescan
6411       (save-excursion
6412         (save-window-excursion
6413           ;; Don't show group contents.
6414           (set-window-start (selected-window) (point-max))
6415           (gnus-group-get-new-news-this-group 1))))
6416     (gnus-group-read-group all t)
6417     (gnus-summary-goto-subject current-subject nil t)))
6418
6419 (defun gnus-summary-rescan-group (&optional all)
6420   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6421   (interactive "P")
6422   (gnus-summary-reselect-current-group all t))
6423
6424 (defun gnus-summary-update-info (&optional non-destructive)
6425   (save-excursion
6426     (let ((group gnus-newsgroup-name))
6427       (when group
6428         (when gnus-newsgroup-kill-headers
6429           (setq gnus-newsgroup-killed
6430                 (gnus-compress-sequence
6431                  (gnus-sorted-union
6432                   (gnus-list-range-intersection
6433                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6434                   gnus-newsgroup-unreads)
6435                  t)))
6436         (unless (listp (cdr gnus-newsgroup-killed))
6437           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6438         (let ((headers gnus-newsgroup-headers))
6439           ;; Set the new ranges of read articles.
6440           (save-excursion
6441             (set-buffer gnus-group-buffer)
6442             (gnus-undo-force-boundary))
6443           (gnus-update-read-articles
6444            group (gnus-sorted-union
6445                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6446           ;; Set the current article marks.
6447           (let ((gnus-newsgroup-scored
6448                  (if (and (not gnus-save-score)
6449                           (not non-destructive))
6450                      nil
6451                    gnus-newsgroup-scored)))
6452             (save-excursion
6453               (gnus-update-marks)))
6454           ;; Do the cross-ref thing.
6455           (when gnus-use-cross-reference
6456             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6457           ;; Do not switch windows but change the buffer to work.
6458           (set-buffer gnus-group-buffer)
6459           (unless (gnus-ephemeral-group-p group)
6460             (gnus-group-update-group group)))))))
6461
6462 (defun gnus-summary-save-newsrc (&optional force)
6463   "Save the current number of read/marked articles in the dribble buffer.
6464 The dribble buffer will then be saved.
6465 If FORCE (the prefix), also save the .newsrc file(s)."
6466   (interactive "P")
6467   (gnus-summary-update-info t)
6468   (if force
6469       (gnus-save-newsrc-file)
6470     (gnus-dribble-save)))
6471
6472 (defun gnus-summary-exit (&optional temporary leave-hidden)
6473   "Exit reading current newsgroup, and then return to group selection mode.
6474 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6475   (interactive)
6476   (gnus-set-global-variables)
6477   (gnus-kill-save-kill-buffer)
6478   (gnus-async-halt-prefetch)
6479   (let* ((group gnus-newsgroup-name)
6480          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6481          (gnus-group-is-exiting-p t)
6482          (mode major-mode)
6483          (group-point nil)
6484          (buf (current-buffer)))
6485     (unless quit-config
6486       ;; Do adaptive scoring, and possibly save score files.
6487       (when gnus-newsgroup-adaptive
6488         (gnus-score-adaptive))
6489       (when gnus-use-scoring
6490         (gnus-score-save)))
6491     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6492     ;; If we have several article buffers, we kill them at exit.
6493     (unless gnus-single-article-buffer
6494       (gnus-kill-buffer gnus-original-article-buffer)
6495       (setq gnus-article-current nil))
6496     (when gnus-use-cache
6497       (gnus-cache-possibly-remove-articles)
6498       (gnus-cache-save-buffers))
6499     (gnus-async-prefetch-remove-group group)
6500     (when gnus-suppress-duplicates
6501       (gnus-dup-enter-articles))
6502     (when gnus-use-trees
6503       (gnus-tree-close group))
6504     (when gnus-use-cache
6505       (gnus-cache-write-active))
6506     ;; Remove entries for this group.
6507     (nnmail-purge-split-history (gnus-group-real-name group))
6508     ;; Make all changes in this group permanent.
6509     (unless quit-config
6510       (gnus-run-hooks 'gnus-exit-group-hook)
6511       (gnus-summary-update-info))
6512     (gnus-close-group group)
6513     ;; Make sure where we were, and go to next newsgroup.
6514     (set-buffer gnus-group-buffer)
6515     (unless quit-config
6516       (gnus-group-jump-to-group group))
6517     (gnus-run-hooks 'gnus-summary-exit-hook)
6518     (unless (or quit-config
6519                 ;; If this group has disappeared from the summary
6520                 ;; buffer, don't skip forwards.
6521                 (not (string= group (gnus-group-group-name))))
6522       (gnus-group-next-unread-group 1))
6523     (setq group-point (point))
6524     (if temporary
6525         nil                             ;Nothing to do.
6526       ;; If we have several article buffers, we kill them at exit.
6527       (unless gnus-single-article-buffer
6528         (gnus-kill-buffer gnus-article-buffer)
6529         (gnus-kill-buffer gnus-original-article-buffer)
6530         (setq gnus-article-current nil))
6531       (set-buffer buf)
6532       (if (not gnus-kill-summary-on-exit)
6533           (progn
6534             (gnus-deaden-summary)
6535             (setq mode nil))
6536         ;; We set all buffer-local variables to nil.  It is unclear why
6537         ;; this is needed, but if we don't, buffer-local variables are
6538         ;; not garbage-collected, it seems.  This would the lead to en
6539         ;; ever-growing Emacs.
6540         (gnus-summary-clear-local-variables)
6541         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6542           (gnus-summary-clear-local-variables))
6543         (when (get-buffer gnus-article-buffer)
6544           (bury-buffer gnus-article-buffer))
6545         ;; We clear the global counterparts of the buffer-local
6546         ;; variables as well, just to be on the safe side.
6547         (set-buffer gnus-group-buffer)
6548         (gnus-summary-clear-local-variables)
6549         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6550           (gnus-summary-clear-local-variables))
6551         ;; Return to group mode buffer.
6552         (when (eq mode 'gnus-summary-mode)
6553           (gnus-kill-buffer buf)))
6554       (setq gnus-current-select-method gnus-select-method)
6555       (if leave-hidden
6556           (set-buffer gnus-group-buffer)
6557         (pop-to-buffer gnus-group-buffer))
6558       (if (not quit-config)
6559           (progn
6560             (goto-char group-point)
6561             (unless leave-hidden
6562               (gnus-configure-windows 'group 'force))
6563             (unless (pos-visible-in-window-p)
6564               (forward-line (/ (static-if (featurep 'xemacs)
6565                                    (window-displayed-height)
6566                                  (1- (window-height)))
6567                                -2))
6568               (set-window-start (selected-window) (point))
6569               (goto-char group-point)))
6570         (gnus-handle-ephemeral-exit quit-config))
6571       ;; Clear the current group name.
6572       (unless quit-config
6573         (setq gnus-newsgroup-name nil)))))
6574
6575 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6576 (defun gnus-summary-exit-no-update (&optional no-questions)
6577   "Quit reading current newsgroup without updating read article info."
6578   (interactive)
6579   (let* ((group gnus-newsgroup-name)
6580          (gnus-group-is-exiting-p t)
6581          (gnus-group-is-exiting-without-update-p t)
6582          (quit-config (gnus-group-quit-config group)))
6583     (when (or no-questions
6584               gnus-expert-user
6585               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6586       (gnus-async-halt-prefetch)
6587       (run-hooks 'gnus-summary-prepare-exit-hook)
6588       ;; If we have several article buffers, we kill them at exit.
6589       (unless gnus-single-article-buffer
6590         (gnus-kill-buffer gnus-article-buffer)
6591         (gnus-kill-buffer gnus-original-article-buffer)
6592         (setq gnus-article-current nil))
6593       (if (not gnus-kill-summary-on-exit)
6594           (gnus-deaden-summary)
6595         (gnus-close-group group)
6596         (gnus-summary-clear-local-variables)
6597         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6598           (gnus-summary-clear-local-variables))
6599         (set-buffer gnus-group-buffer)
6600         (gnus-summary-clear-local-variables)
6601         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6602           (gnus-summary-clear-local-variables))
6603         (gnus-kill-buffer gnus-summary-buffer))
6604       (unless gnus-single-article-buffer
6605         (setq gnus-article-current nil))
6606       (when gnus-use-trees
6607         (gnus-tree-close group))
6608       (gnus-async-prefetch-remove-group group)
6609       (when (get-buffer gnus-article-buffer)
6610         (bury-buffer gnus-article-buffer))
6611       ;; Return to the group buffer.
6612       (gnus-configure-windows 'group 'force)
6613       ;; Clear the current group name.
6614       (setq gnus-newsgroup-name nil)
6615       (unless (gnus-ephemeral-group-p group)
6616         (gnus-group-update-group group))
6617       (when (equal (gnus-group-group-name) group)
6618         (gnus-group-next-unread-group 1))
6619       (when quit-config
6620         (gnus-handle-ephemeral-exit quit-config)))))
6621
6622 (defun gnus-handle-ephemeral-exit (quit-config)
6623   "Handle movement when leaving an ephemeral group.
6624 The state which existed when entering the ephemeral is reset."
6625   (if (not (buffer-name (car quit-config)))
6626       (gnus-configure-windows 'group 'force)
6627     (set-buffer (car quit-config))
6628     (cond ((eq major-mode 'gnus-summary-mode)
6629            (gnus-set-global-variables))
6630           ((eq major-mode 'gnus-article-mode)
6631            (save-excursion
6632              ;; The `gnus-summary-buffer' variable may point
6633              ;; to the old summary buffer when using a single
6634              ;; article buffer.
6635              (unless (gnus-buffer-live-p gnus-summary-buffer)
6636                (set-buffer gnus-group-buffer))
6637              (set-buffer gnus-summary-buffer)
6638              (gnus-set-global-variables))))
6639     (if (or (eq (cdr quit-config) 'article)
6640             (eq (cdr quit-config) 'pick))
6641         (progn
6642           ;; The current article may be from the ephemeral group
6643           ;; thus it is best that we reload this article
6644           ;;
6645           ;; If we're exiting from a large digest, this can be
6646           ;; extremely slow.  So, it's better not to reload it. -- jh.
6647           ;;(gnus-summary-show-article)
6648           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6649               (gnus-configure-windows 'pick 'force)
6650             (gnus-configure-windows (cdr quit-config) 'force)))
6651       (gnus-configure-windows (cdr quit-config) 'force))
6652     (when (eq major-mode 'gnus-summary-mode)
6653       (gnus-summary-next-subject 1 nil t)
6654       (gnus-summary-recenter)
6655       (gnus-summary-position-point))))
6656
6657 (defun gnus-summary-preview-mime-message ()
6658   "MIME decode and play this message."
6659   (interactive)
6660   (let ((gnus-break-pages nil)
6661         (gnus-show-mime t))
6662     (gnus-summary-select-article gnus-show-all-headers t))
6663   (let ((w (get-buffer-window gnus-article-buffer)))
6664     (when w
6665       (select-window (get-buffer-window gnus-article-buffer)))))
6666
6667 ;;; Dead summaries.
6668
6669 (defvar gnus-dead-summary-mode-map nil)
6670
6671 (unless gnus-dead-summary-mode-map
6672   (setq gnus-dead-summary-mode-map (make-keymap))
6673   (suppress-keymap gnus-dead-summary-mode-map)
6674   (substitute-key-definition
6675    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6676   (dolist (key '("\C-d" "\r" "\177" [delete]))
6677     (define-key gnus-dead-summary-mode-map
6678       key 'gnus-summary-wake-up-the-dead))
6679   (dolist (key '("q" "Q"))
6680     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6681
6682 (defvar gnus-dead-summary-mode nil
6683   "Minor mode for Gnus summary buffers.")
6684
6685 (defun gnus-dead-summary-mode (&optional arg)
6686   "Minor mode for Gnus summary buffers."
6687   (interactive "P")
6688   (when (eq major-mode 'gnus-summary-mode)
6689     (make-local-variable 'gnus-dead-summary-mode)
6690     (setq gnus-dead-summary-mode
6691           (if (null arg) (not gnus-dead-summary-mode)
6692             (> (prefix-numeric-value arg) 0)))
6693     (when gnus-dead-summary-mode
6694       (gnus-add-minor-mode
6695        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6696
6697 (defun gnus-deaden-summary ()
6698   "Make the current summary buffer into a dead summary buffer."
6699   ;; Kill any previous dead summary buffer.
6700   (when (and gnus-dead-summary
6701              (buffer-name gnus-dead-summary))
6702     (save-excursion
6703       (set-buffer gnus-dead-summary)
6704       (when gnus-dead-summary-mode
6705         (kill-buffer (current-buffer)))))
6706   ;; Make this the current dead summary.
6707   (setq gnus-dead-summary (current-buffer))
6708   (gnus-dead-summary-mode 1)
6709   (let ((name (buffer-name)))
6710     (when (string-match "Summary" name)
6711       (rename-buffer
6712        (concat (substring name 0 (match-beginning 0)) "Dead "
6713                (substring name (match-beginning 0)))
6714        t)
6715       (bury-buffer))))
6716
6717 (defun gnus-kill-or-deaden-summary (buffer)
6718   "Kill or deaden the summary BUFFER."
6719   (save-excursion
6720     (when (and (buffer-name buffer)
6721                (not gnus-single-article-buffer))
6722       (save-excursion
6723         (set-buffer buffer)
6724         (gnus-kill-buffer gnus-article-buffer)
6725         (gnus-kill-buffer gnus-original-article-buffer)))
6726     (cond
6727      ;; Kill the buffer.
6728      (gnus-kill-summary-on-exit
6729       (when (and gnus-use-trees
6730                  (gnus-buffer-exists-p buffer))
6731         (save-excursion
6732           (set-buffer buffer)
6733           (gnus-tree-close gnus-newsgroup-name)))
6734       (gnus-kill-buffer buffer))
6735      ;; Deaden the buffer.
6736      ((gnus-buffer-exists-p buffer)
6737       (save-excursion
6738         (set-buffer buffer)
6739         (gnus-deaden-summary))))))
6740
6741 (defun gnus-summary-wake-up-the-dead (&rest args)
6742   "Wake up the dead summary buffer."
6743   (interactive)
6744   (gnus-dead-summary-mode -1)
6745   (let ((name (buffer-name)))
6746     (when (string-match "Dead " name)
6747       (rename-buffer
6748        (concat (substring name 0 (match-beginning 0))
6749                (substring name (match-end 0)))
6750        t)))
6751   (gnus-message 3 "This dead summary is now alive again"))
6752
6753 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6754 (defun gnus-summary-fetch-faq (&optional faq-dir)
6755   "Fetch the FAQ for the current group.
6756 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6757 in."
6758   (interactive
6759    (list
6760     (when current-prefix-arg
6761       (completing-read
6762        "FAQ dir: " (and (listp gnus-group-faq-directory)
6763                         (mapcar (lambda (file) (list file))
6764                                 gnus-group-faq-directory))))))
6765   (let (gnus-faq-buffer)
6766     (when (setq gnus-faq-buffer
6767                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6768       (gnus-configure-windows 'summary-faq))))
6769
6770 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6771 (defun gnus-summary-describe-group (&optional force)
6772   "Describe the current newsgroup."
6773   (interactive "P")
6774   (gnus-group-describe-group force gnus-newsgroup-name))
6775
6776 (defun gnus-summary-describe-briefly ()
6777   "Describe summary mode commands briefly."
6778   (interactive)
6779   (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")))
6780
6781 ;; Walking around group mode buffer from summary mode.
6782
6783 (defun gnus-summary-next-group (&optional no-article target-group backward)
6784   "Exit current newsgroup and then select next unread newsgroup.
6785 If prefix argument NO-ARTICLE is non-nil, no article is selected
6786 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6787 previous group instead."
6788   (interactive "P")
6789   ;; Stop pre-fetching.
6790   (gnus-async-halt-prefetch)
6791   (let ((current-group gnus-newsgroup-name)
6792         (current-buffer (current-buffer))
6793         entered)
6794     ;; First we semi-exit this group to update Xrefs and all variables.
6795     ;; We can't do a real exit, because the window conf must remain
6796     ;; the same in case the user is prompted for info, and we don't
6797     ;; want the window conf to change before that...
6798     (gnus-summary-exit t)
6799     (while (not entered)
6800       ;; Then we find what group we are supposed to enter.
6801       (set-buffer gnus-group-buffer)
6802       (gnus-group-jump-to-group current-group)
6803       (setq target-group
6804             (or target-group
6805                 (if (eq gnus-keep-same-level 'best)
6806                     (gnus-summary-best-group gnus-newsgroup-name)
6807                   (gnus-summary-search-group backward gnus-keep-same-level))))
6808       (if (not target-group)
6809           ;; There are no further groups, so we return to the group
6810           ;; buffer.
6811           (progn
6812             (gnus-message 5 "Returning to the group buffer")
6813             (setq entered t)
6814             (when (gnus-buffer-live-p current-buffer)
6815               (set-buffer current-buffer)
6816               (gnus-summary-exit))
6817             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6818         ;; We try to enter the target group.
6819         (gnus-group-jump-to-group target-group)
6820         (let ((unreads (gnus-group-group-unread)))
6821           (if (and (or (eq t unreads)
6822                        (and unreads (not (zerop unreads))))
6823                    (gnus-summary-read-group
6824                     target-group nil no-article
6825                     (and (buffer-name current-buffer) current-buffer)
6826                     nil backward))
6827               (setq entered t)
6828             (setq current-group target-group
6829                   target-group nil)))))))
6830
6831 (defun gnus-summary-prev-group (&optional no-article)
6832   "Exit current newsgroup and then select previous unread newsgroup.
6833 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6834   (interactive "P")
6835   (gnus-summary-next-group no-article nil t))
6836
6837 ;; Walking around summary lines.
6838
6839 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6840   "Go to the first subject satisfying any non-nil constraint.
6841 If UNREAD is non-nil, the article should be unread.
6842 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6843 If UNSEEN is non-nil, the article should be unseen.
6844 Returns the article selected or nil if there are no matching articles."
6845   (interactive "P")
6846   (cond
6847    ;; Empty summary.
6848    ((null gnus-newsgroup-data)
6849     (gnus-message 3 "No articles in the group")
6850     nil)
6851    ;; Pick the first article.
6852    ((not (or unread undownloaded unseen))
6853     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6854     (gnus-data-number (car gnus-newsgroup-data)))
6855    ;; Find the first unread article.
6856    (t
6857     (let ((data gnus-newsgroup-data))
6858       (while (and data
6859                   (let ((num (gnus-data-number (car data))))
6860                     (or (memq num gnus-newsgroup-unfetched)
6861                         (not (or (and unread
6862                                       (memq num gnus-newsgroup-unreads))
6863                                  (and undownloaded
6864                                       (memq num gnus-newsgroup-undownloaded))
6865                                  (and unseen
6866                                       (memq num gnus-newsgroup-unseen)))))))
6867         (setq data (cdr data)))
6868       (prog1
6869           (if data
6870               (progn
6871                 (goto-char (gnus-data-pos (car data)))
6872                 (gnus-data-number (car data)))
6873             (gnus-message 3 "No more%s articles"
6874                           (let* ((r (when unread " unread"))
6875                                  (d (when undownloaded " undownloaded"))
6876                                  (s (when unseen " unseen"))
6877                                  (l (delq nil (list r d s))))
6878                             (cond ((= 3 (length l))
6879                                    (concat r "," d ", or" s))
6880                                   ((= 2 (length l))
6881                                    (concat (car l) ", or" (cadr l)))
6882                                   ((= 1 (length l))
6883                                    (car l))
6884                                   (t
6885                                    ""))))
6886             nil
6887             )
6888         (gnus-summary-position-point))))))
6889
6890 (defun gnus-summary-next-subject (n &optional unread dont-display)
6891   "Go to next N'th summary line.
6892 If N is negative, go to the previous N'th subject line.
6893 If UNREAD is non-nil, only unread articles are selected.
6894 The difference between N and the actual number of steps taken is
6895 returned."
6896   (interactive "p")
6897   (let ((backward (< n 0))
6898         (n (abs n)))
6899     (while (and (> n 0)
6900                 (if backward
6901                     (gnus-summary-find-prev unread)
6902                   (gnus-summary-find-next unread)))
6903       (unless (zerop (setq n (1- n)))
6904         (gnus-summary-show-thread)))
6905     (when (/= 0 n)
6906       (gnus-message 7 "No more%s articles"
6907                     (if unread " unread" "")))
6908     (unless dont-display
6909       (gnus-summary-recenter)
6910       (gnus-summary-position-point))
6911     n))
6912
6913 (defun gnus-summary-next-unread-subject (n)
6914   "Go to next N'th unread summary line."
6915   (interactive "p")
6916   (gnus-summary-next-subject n t))
6917
6918 (defun gnus-summary-prev-subject (n &optional unread)
6919   "Go to previous N'th summary line.
6920 If optional argument UNREAD is non-nil, only unread article is selected."
6921   (interactive "p")
6922   (gnus-summary-next-subject (- n) unread))
6923
6924 (defun gnus-summary-prev-unread-subject (n)
6925   "Go to previous N'th unread summary line."
6926   (interactive "p")
6927   (gnus-summary-next-subject (- n) t))
6928
6929 (defun gnus-summary-goto-subjects (articles)
6930   "Insert the subject header for ARTICLES in the current buffer."
6931   (save-excursion
6932     (dolist (article articles)
6933       (gnus-summary-goto-subject article t)))
6934   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6935   (gnus-summary-position-point))
6936  
6937 (defun gnus-summary-goto-subject (article &optional force silent)
6938   "Go the subject line of ARTICLE.
6939 If FORCE, also allow jumping to articles not currently shown."
6940   (interactive "nArticle number: ")
6941   (unless (numberp article)
6942     (error "Article %s is not a number" article))
6943   (let ((b (point))
6944         (data (gnus-data-find article)))
6945     ;; We read in the article if we have to.
6946     (and (not data)
6947          force
6948          (gnus-summary-insert-subject
6949           article
6950           (if (or (numberp force) (vectorp force)) force)
6951           t)
6952          (setq data (gnus-data-find article)))
6953     (goto-char b)
6954     (if (not data)
6955         (progn
6956           (unless silent
6957             (gnus-message 3 "Can't find article %d" article))
6958           nil)
6959       (let ((pt (gnus-data-pos data)))
6960         (goto-char pt)
6961         (gnus-summary-set-article-display-arrow pt))
6962       (gnus-summary-position-point)
6963       article)))
6964
6965 ;; Walking around summary lines with displaying articles.
6966
6967 (defun gnus-summary-expand-window (&optional arg)
6968   "Make the summary buffer take up the entire Emacs frame.
6969 Given a prefix, will force an `article' buffer configuration."
6970   (interactive "P")
6971   (if arg
6972       (gnus-configure-windows 'article 'force)
6973     (gnus-configure-windows 'summary 'force)))
6974
6975 (defun gnus-summary-display-article (article &optional all-header)
6976   "Display ARTICLE in article buffer."
6977   (when (gnus-buffer-live-p gnus-article-buffer)
6978     (with-current-buffer gnus-article-buffer
6979       (set-buffer-multibyte t)))
6980   (gnus-set-global-variables)
6981   (when (gnus-buffer-live-p gnus-article-buffer)
6982     (with-current-buffer gnus-article-buffer
6983       (setq gnus-article-charset gnus-newsgroup-charset)
6984       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6985       (set-buffer-multibyte t)))
6986   (if (null article)
6987       nil
6988     (prog1
6989         (if gnus-summary-display-article-function
6990             (funcall gnus-summary-display-article-function article all-header)
6991           (gnus-article-prepare article all-header))
6992       (with-current-buffer gnus-article-buffer
6993         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6994              nil))
6995       (gnus-run-hooks 'gnus-select-article-hook)
6996       (when (and gnus-current-article
6997                  (not (zerop gnus-current-article)))
6998         (gnus-summary-goto-subject gnus-current-article))
6999       (gnus-summary-recenter)
7000       (when (and gnus-use-trees gnus-show-threads)
7001         (gnus-possibly-generate-tree article)
7002         (gnus-highlight-selected-tree article))
7003       ;; Successfully display article.
7004       (gnus-article-set-window-start
7005        (cdr (assq article gnus-newsgroup-bookmarks))))))
7006
7007 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7008   "Select the current article.
7009 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7010 non-nil, the article will be re-fetched even if it already present in
7011 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7012 be displayed."
7013   ;; Make sure we are in the summary buffer to work around bbdb bug.
7014   (unless (eq major-mode 'gnus-summary-mode)
7015     (set-buffer gnus-summary-buffer))
7016   (let ((article (or article (gnus-summary-article-number)))
7017         (all-headers (not (not all-headers))) ;Must be T or NIL.
7018         gnus-summary-display-article-function)
7019     (and (not pseudo)
7020          (gnus-summary-article-pseudo-p article)
7021          (error "This is a pseudo-article"))
7022     (save-excursion
7023       (set-buffer gnus-summary-buffer)
7024       (if (or (and gnus-single-article-buffer
7025                    (or (null gnus-current-article)
7026                        (null gnus-article-current)
7027                        (null (get-buffer gnus-article-buffer))
7028                        (not (eq article (cdr gnus-article-current)))
7029                        (not (equal (car gnus-article-current)
7030                                    gnus-newsgroup-name))))
7031               (and (not gnus-single-article-buffer)
7032                    (or (null gnus-current-article)
7033                        (not (eq gnus-current-article article))))
7034               force)
7035           ;; The requested article is different from the current article.
7036           (progn
7037             (gnus-summary-display-article article all-headers)
7038             (gnus-article-set-window-start
7039              (cdr (assq article gnus-newsgroup-bookmarks)))
7040             article)
7041         'old))))
7042
7043 (defun gnus-summary-force-verify-and-decrypt ()
7044   "Display buttons for signed/encrypted parts and verify/decrypt them."
7045   (interactive)
7046   (let ((mm-verify-option 'known)
7047         (mm-decrypt-option 'known)
7048         (gnus-article-emulate-mime t)
7049         (gnus-buttonized-mime-types (append (list "multipart/signed"
7050                                                   "multipart/encrypted")
7051                                             gnus-buttonized-mime-types)))
7052     (gnus-summary-select-article nil 'force)))
7053
7054 (defun gnus-summary-set-current-mark (&optional current-mark)
7055   "Obsolete function."
7056   nil)
7057
7058 (defun gnus-summary-next-article (&optional unread subject backward push)
7059   "Select the next article.
7060 If UNREAD, only unread articles are selected.
7061 If SUBJECT, only articles with SUBJECT are selected.
7062 If BACKWARD, the previous article is selected instead of the next."
7063   (interactive "P")
7064   (cond
7065    ;; Is there such an article?
7066    ((and (gnus-summary-search-forward unread subject backward)
7067          (or (gnus-summary-display-article (gnus-summary-article-number))
7068              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7069     (gnus-summary-position-point))
7070    ;; If not, we try the first unread, if that is wanted.
7071    ((and subject
7072          gnus-auto-select-same
7073          (gnus-summary-first-unread-article))
7074     (gnus-summary-position-point)
7075     (gnus-message 6 "Wrapped"))
7076    ;; Try to get next/previous article not displayed in this group.
7077    ((and gnus-auto-extend-newsgroup
7078          (not unread) (not subject))
7079     (gnus-summary-goto-article
7080      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7081      nil (count-lines (point-min) (point))))
7082    ;; Go to next/previous group.
7083    (t
7084     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7085       (gnus-summary-jump-to-group gnus-newsgroup-name))
7086     (let ((cmd last-command-char)
7087           (point
7088            (save-excursion
7089              (set-buffer gnus-group-buffer)
7090              (point)))
7091           (group
7092            (if (eq gnus-keep-same-level 'best)
7093                (gnus-summary-best-group gnus-newsgroup-name)
7094              (gnus-summary-search-group backward gnus-keep-same-level))))
7095       ;; For some reason, the group window gets selected.  We change
7096       ;; it back.
7097       (select-window (get-buffer-window (current-buffer)))
7098       ;; Select next unread newsgroup automagically.
7099       (cond
7100        ((or (not gnus-auto-select-next)
7101             (not cmd))
7102         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7103        ((or (eq gnus-auto-select-next 'quietly)
7104             (and (eq gnus-auto-select-next 'slightly-quietly)
7105                  push)
7106             (and (eq gnus-auto-select-next 'almost-quietly)
7107                  (gnus-summary-last-article-p)))
7108         ;; Select quietly.
7109         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7110             (gnus-summary-exit)
7111           (gnus-message 7 "No more%s articles (%s)..."
7112                         (if unread " unread" "")
7113                         (if group (concat "selecting " group)
7114                           "exiting"))
7115           (gnus-summary-next-group nil group backward)))
7116        (t
7117         (when (gnus-key-press-event-p last-input-event)
7118           (gnus-summary-walk-group-buffer
7119            gnus-newsgroup-name cmd unread backward point))))))))
7120
7121 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7122   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7123                       (?\C-p (gnus-group-prev-unread-group 1))))
7124         (cursor-in-echo-area t)
7125         keve key group ended prompt)
7126     (save-excursion
7127       (set-buffer gnus-group-buffer)
7128       (goto-char start)
7129       (setq group
7130             (if (eq gnus-keep-same-level 'best)
7131                 (gnus-summary-best-group gnus-newsgroup-name)
7132               (gnus-summary-search-group backward gnus-keep-same-level))))
7133     (while (not ended)
7134       (setq prompt
7135             (format
7136              "No more%s articles%s " (if unread " unread" "")
7137              (if (and group
7138                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7139                  (format " (Type %s for %s [%s])"
7140                          (single-key-description cmd) group
7141                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7142                (format " (Type %s to exit %s)"
7143                        (single-key-description cmd)
7144                        gnus-newsgroup-name))))
7145       ;; Confirm auto selection.
7146       (setq key (car (setq keve (gnus-read-event-char prompt)))
7147             ended t)
7148       (cond
7149        ((assq key keystrokes)
7150         (let ((obuf (current-buffer)))
7151           (switch-to-buffer gnus-group-buffer)
7152           (when group
7153             (gnus-group-jump-to-group group))
7154           (eval (cadr (assq key keystrokes)))
7155           (setq group (gnus-group-group-name))
7156           (switch-to-buffer obuf))
7157         (setq ended nil))
7158        ((equal key cmd)
7159         (if (or (not group)
7160                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7161             (gnus-summary-exit)
7162           (gnus-summary-next-group nil group backward)))
7163        (t
7164         (push (cdr keve) unread-command-events))))))
7165
7166 (defun gnus-summary-next-unread-article ()
7167   "Select unread article after current one."
7168   (interactive)
7169   (gnus-summary-next-article
7170    (or (not (eq gnus-summary-goto-unread 'never))
7171        (gnus-summary-last-article-p (gnus-summary-article-number)))
7172    (and gnus-auto-select-same
7173         (gnus-summary-article-subject))))
7174
7175 (defun gnus-summary-prev-article (&optional unread subject)
7176   "Select the article after the current one.
7177 If UNREAD is non-nil, only unread articles are selected."
7178   (interactive "P")
7179   (gnus-summary-next-article unread subject t))
7180
7181 (defun gnus-summary-prev-unread-article ()
7182   "Select unread article before current one."
7183   (interactive)
7184   (gnus-summary-prev-article
7185    (or (not (eq gnus-summary-goto-unread 'never))
7186        (gnus-summary-first-article-p (gnus-summary-article-number)))
7187    (and gnus-auto-select-same
7188         (gnus-summary-article-subject))))
7189
7190 (defun gnus-summary-next-page (&optional lines circular stop)
7191   "Show next page of the selected article.
7192 If at the end of the current article, select the next article.
7193 LINES says how many lines should be scrolled up.
7194
7195 If CIRCULAR is non-nil, go to the start of the article instead of
7196 selecting the next article when reaching the end of the current
7197 article.
7198
7199 If STOP is non-nil, just stop when reaching the end of the message.
7200
7201 Also see the variable `gnus-article-skip-boring'."
7202   (interactive "P")
7203   (setq gnus-summary-buffer (current-buffer))
7204   (gnus-set-global-variables)
7205   (let ((article (gnus-summary-article-number))
7206         (article-window (get-buffer-window gnus-article-buffer t))
7207         endp)
7208     ;; If the buffer is empty, we have no article.
7209     (unless article
7210       (error "No article to select"))
7211     (gnus-configure-windows 'article)
7212     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7213         (if (and (eq gnus-summary-goto-unread 'never)
7214                  (not (gnus-summary-last-article-p article)))
7215             (gnus-summary-next-article)
7216           (gnus-summary-next-unread-article))
7217       (if (or (null gnus-current-article)
7218               (null gnus-article-current)
7219               (/= article (cdr gnus-article-current))
7220               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7221           ;; Selected subject is different from current article's.
7222           (gnus-summary-display-article article)
7223         (when article-window
7224           (gnus-eval-in-buffer-window gnus-article-buffer
7225             (setq endp (or (gnus-article-next-page lines)
7226                            (gnus-article-only-boring-p))))
7227           (when endp
7228             (cond (stop
7229                    (gnus-message 3 "End of message"))
7230                   (circular
7231                    (gnus-summary-beginning-of-article))
7232                   (lines
7233                    (gnus-message 3 "End of message"))
7234                   ((null lines)
7235                    (if (and (eq gnus-summary-goto-unread 'never)
7236                             (not (gnus-summary-last-article-p article)))
7237                        (gnus-summary-next-article)
7238                      (gnus-summary-next-unread-article))))))))
7239     (gnus-summary-recenter)
7240     (gnus-summary-position-point)))
7241
7242 (defun gnus-summary-prev-page (&optional lines move)
7243   "Show previous page of selected article.
7244 Argument LINES specifies lines to be scrolled down.
7245 If MOVE, move to the previous unread article if point is at
7246 the beginning of the buffer."
7247   (interactive "P")
7248   (let ((article (gnus-summary-article-number))
7249         (article-window (get-buffer-window gnus-article-buffer t))
7250         endp)
7251     (gnus-configure-windows 'article)
7252     (if (or (null gnus-current-article)
7253             (null gnus-article-current)
7254             (/= article (cdr gnus-article-current))
7255             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7256         ;; Selected subject is different from current article's.
7257         (gnus-summary-display-article article)
7258       (gnus-summary-recenter)
7259       (when article-window
7260         (gnus-eval-in-buffer-window gnus-article-buffer
7261           (setq endp (gnus-article-prev-page lines)))
7262         (when (and move endp)
7263           (cond (lines
7264                  (gnus-message 3 "Beginning of message"))
7265                 ((null lines)
7266                  (if (and (eq gnus-summary-goto-unread 'never)
7267                           (not (gnus-summary-first-article-p article)))
7268                      (gnus-summary-prev-article)
7269                    (gnus-summary-prev-unread-article))))))))
7270   (gnus-summary-position-point))
7271
7272 (defun gnus-summary-prev-page-or-article (&optional lines)
7273   "Show previous page of selected article.
7274 Argument LINES specifies lines to be scrolled down.
7275 If at the beginning of the article, go to the next article."
7276   (interactive "P")
7277   (gnus-summary-prev-page lines t))
7278
7279 (defun gnus-summary-scroll-up (lines)
7280   "Scroll up (or down) one line current article.
7281 Argument LINES specifies lines to be scrolled up (or down if negative)."
7282   (interactive "p")
7283   (gnus-configure-windows 'article)
7284   (gnus-summary-show-thread)
7285   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7286     (gnus-eval-in-buffer-window gnus-article-buffer
7287       (cond ((> lines 0)
7288              (when (gnus-article-next-page lines)
7289                (gnus-message 3 "End of message")))
7290             ((< lines 0)
7291              (gnus-article-prev-page (- lines))))))
7292   (gnus-summary-recenter)
7293   (gnus-summary-position-point))
7294
7295 (defun gnus-summary-scroll-down (lines)
7296   "Scroll down (or up) one line current article.
7297 Argument LINES specifies lines to be scrolled down (or up if negative)."
7298   (interactive "p")
7299   (gnus-summary-scroll-up (- lines)))
7300
7301 (defun gnus-summary-next-same-subject ()
7302   "Select next article which has the same subject as current one."
7303   (interactive)
7304   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7305
7306 (defun gnus-summary-prev-same-subject ()
7307   "Select previous article which has the same subject as current one."
7308   (interactive)
7309   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7310
7311 (defun gnus-summary-next-unread-same-subject ()
7312   "Select next unread article which has the same subject as current one."
7313   (interactive)
7314   (gnus-summary-next-article t (gnus-summary-article-subject)))
7315
7316 (defun gnus-summary-prev-unread-same-subject ()
7317   "Select previous unread article which has the same subject as current one."
7318   (interactive)
7319   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7320
7321 (defun gnus-summary-first-unread-article ()
7322   "Select the first unread article.
7323 Return nil if there are no unread articles."
7324   (interactive)
7325   (prog1
7326       (when (gnus-summary-first-subject t)
7327         (gnus-summary-show-thread)
7328         (gnus-summary-first-subject t)
7329         (gnus-summary-display-article (gnus-summary-article-number)))
7330     (gnus-summary-position-point)))
7331
7332 (defun gnus-summary-first-unread-subject ()
7333   "Place the point on the subject line of the first unread article.
7334 Return nil if there are no unread articles."
7335   (interactive)
7336   (prog1
7337       (when (gnus-summary-first-subject t)
7338         (gnus-summary-show-thread)
7339         (gnus-summary-first-subject t))
7340     (gnus-summary-position-point)))
7341
7342 (defun gnus-summary-first-unseen-subject ()
7343   "Place the point on the subject line of the first unseen article.
7344 Return nil if there are no unseen articles."
7345   (interactive)
7346   (prog1
7347       (when (gnus-summary-first-subject nil nil t)
7348         (gnus-summary-show-thread)
7349         (gnus-summary-first-subject nil nil t))
7350     (gnus-summary-position-point)))
7351
7352 (defun gnus-summary-first-unseen-or-unread-subject ()
7353   "Place the point on the subject line of the first unseen article or,
7354 if all article have been seen, on the subject line of the first unread
7355 article."
7356   (interactive)
7357   (prog1
7358       (unless (when (gnus-summary-first-subject nil nil t)
7359                 (gnus-summary-show-thread)
7360                 (gnus-summary-first-subject nil nil t))
7361         (when (gnus-summary-first-subject t)
7362           (gnus-summary-show-thread)
7363           (gnus-summary-first-subject t)))
7364     (gnus-summary-position-point)))
7365
7366 (defun gnus-summary-first-article ()
7367   "Select the first article.
7368 Return nil if there are no articles."
7369   (interactive)
7370   (prog1
7371       (when (gnus-summary-first-subject)
7372         (gnus-summary-show-thread)
7373         (gnus-summary-first-subject)
7374         (gnus-summary-display-article (gnus-summary-article-number)))
7375     (gnus-summary-position-point)))
7376
7377 (defun gnus-summary-best-unread-article (&optional arg)
7378   "Select the unread article with the highest score.
7379 If given a prefix argument, select the next unread article that has a
7380 score higher than the default score."
7381   (interactive "P")
7382   (let ((article (if arg
7383                      (gnus-summary-better-unread-subject)
7384                    (gnus-summary-best-unread-subject))))
7385     (if article
7386         (gnus-summary-goto-article article)
7387       (error "No unread articles"))))
7388
7389 (defun gnus-summary-best-unread-subject ()
7390   "Select the unread subject with the highest score."
7391   (interactive)
7392   (let ((best -1000000)
7393         (data gnus-newsgroup-data)
7394         article score)
7395     (while data
7396       (and (gnus-data-unread-p (car data))
7397            (> (setq score
7398                     (gnus-summary-article-score (gnus-data-number (car data))))
7399               best)
7400            (setq best score
7401                  article (gnus-data-number (car data))))
7402       (setq data (cdr data)))
7403     (when article
7404       (gnus-summary-goto-subject article))
7405     (gnus-summary-position-point)
7406     article))
7407
7408 (defun gnus-summary-better-unread-subject ()
7409   "Select the first unread subject that has a score over the default score."
7410   (interactive)
7411   (let ((data gnus-newsgroup-data)
7412         article score)
7413     (while (and (setq article (gnus-data-number (car data)))
7414                 (or (gnus-data-read-p (car data))
7415                     (not (> (gnus-summary-article-score article)
7416                             gnus-summary-default-score))))
7417       (setq data (cdr data)))
7418     (when article
7419       (gnus-summary-goto-subject article))
7420     (gnus-summary-position-point)
7421     article))
7422
7423 (defun gnus-summary-last-subject ()
7424   "Go to the last displayed subject line in the group."
7425   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7426     (when article
7427       (gnus-summary-goto-subject article))))
7428
7429 (defun gnus-summary-goto-article (article &optional all-headers force)
7430   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7431 If ALL-HEADERS is non-nil, no header lines are hidden.
7432 If FORCE, go to the article even if it isn't displayed.  If FORCE
7433 is a number, it is the line the article is to be displayed on."
7434   (interactive
7435    (list
7436     (completing-read
7437      "Article number or Message-ID: "
7438      (mapcar (lambda (number) (list (int-to-string number)))
7439              gnus-newsgroup-limit))
7440     current-prefix-arg
7441     t))
7442   (prog1
7443       (if (and (stringp article)
7444                (string-match "@" article))
7445           (gnus-summary-refer-article article)
7446         (when (stringp article)
7447           (setq article (string-to-number article)))
7448         (if (gnus-summary-goto-subject article force)
7449             (gnus-summary-display-article article all-headers)
7450           (gnus-message 4 "Couldn't go to article %s" article) nil))
7451     (gnus-summary-position-point)))
7452
7453 (defun gnus-summary-goto-last-article ()
7454   "Go to the previously read article."
7455   (interactive)
7456   (prog1
7457       (when gnus-last-article
7458         (gnus-summary-goto-article gnus-last-article nil t))
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-pop-article (number)
7462   "Pop one article off the history and go to the previous.
7463 NUMBER articles will be popped off."
7464   (interactive "p")
7465   (let (to)
7466     (setq gnus-newsgroup-history
7467           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7468     (if to
7469         (gnus-summary-goto-article (car to) nil t)
7470       (error "Article history empty")))
7471   (gnus-summary-position-point))
7472
7473 ;; Summary commands and functions for limiting the summary buffer.
7474
7475 (defun gnus-summary-limit-to-articles (n)
7476   "Limit the summary buffer to the next N articles.
7477 If not given a prefix, use the process marked articles instead."
7478   (interactive "P")
7479   (prog1
7480       (let ((articles (gnus-summary-work-articles n)))
7481         (setq gnus-newsgroup-processable nil)
7482         (gnus-summary-limit articles))
7483     (gnus-summary-position-point)))
7484
7485 (defun gnus-summary-pop-limit (&optional total)
7486   "Restore the previous limit.
7487 If given a prefix, remove all limits."
7488   (interactive "P")
7489   (when total
7490     (setq gnus-newsgroup-limits
7491           (list (mapcar (lambda (h) (mail-header-number h))
7492                         gnus-newsgroup-headers))))
7493   (unless gnus-newsgroup-limits
7494     (error "No limit to pop"))
7495   (prog1
7496       (gnus-summary-limit nil 'pop)
7497     (gnus-summary-position-point)))
7498
7499 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7500   "Limit the summary buffer to articles that have subjects that match a regexp.
7501 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7502   (interactive
7503    (list (read-string (if current-prefix-arg
7504                           "Exclude subject (regexp): "
7505                         "Limit to subject (regexp): "))
7506          nil current-prefix-arg))
7507   (unless header
7508     (setq header "subject"))
7509   (when (not (equal "" subject))
7510     (prog1
7511         (let ((articles (gnus-summary-find-matching
7512                          (or header "subject") subject 'all nil nil
7513                          not-matching)))
7514           (unless articles
7515             (error "Found no matches for \"%s\"" subject))
7516           (gnus-summary-limit articles))
7517       (gnus-summary-position-point))))
7518
7519 (defun gnus-summary-limit-to-author (from &optional not-matching)
7520   "Limit the summary buffer to articles that have authors that match a regexp.
7521 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7522   (interactive
7523    (list (read-string (if current-prefix-arg
7524                           "Exclude author (regexp): "
7525                         "Limit to author (regexp): "))
7526          current-prefix-arg))
7527   (gnus-summary-limit-to-subject from "from" not-matching))
7528
7529 (defun gnus-summary-limit-to-age (age &optional younger-p)
7530   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7531 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7532 articles that are younger than AGE days."
7533   (interactive
7534    (let ((younger current-prefix-arg)
7535          (days-got nil)
7536          days)
7537      (while (not days-got)
7538        (setq days (if younger
7539                       (read-string "Limit to articles younger than (in days, older when negative): ")
7540                     (read-string
7541                      "Limit to articles older than (in days, younger when negative): ")))
7542        (when (> (length days) 0)
7543          (setq days (read days)))
7544        (if (numberp days)
7545            (progn
7546              (setq days-got t)
7547              (if (< days 0)
7548                  (progn
7549                    (setq younger (not younger))
7550                    (setq days (* days -1)))))
7551          (message "Please enter a number.")
7552          (sleep-for 1)))
7553      (list days younger)))
7554   (prog1
7555       (let ((data gnus-newsgroup-data)
7556             (cutoff (days-to-time age))
7557             articles d date is-younger)
7558         (while (setq d (pop data))
7559           (when (and (vectorp (gnus-data-header d))
7560                      (setq date (mail-header-date (gnus-data-header d))))
7561             (setq is-younger (time-less-p
7562                               (time-since (condition-case ()
7563                                               (date-to-time date)
7564                                             (error '(0 0))))
7565                               cutoff))
7566             (when (if younger-p
7567                       is-younger
7568                     (not is-younger))
7569               (push (gnus-data-number d) articles))))
7570         (gnus-summary-limit (nreverse articles)))
7571     (gnus-summary-position-point)))
7572
7573 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7574   "Limit the summary buffer to articles that match an 'extra' header."
7575   (interactive
7576    (let ((header
7577           (intern
7578            (gnus-completing-read-with-default
7579             (symbol-name (car gnus-extra-headers))
7580             (if current-prefix-arg
7581                 "Exclude extra header:"
7582               "Limit extra header:")
7583             (mapcar (lambda (x)
7584                       (cons (symbol-name x) x))
7585                     gnus-extra-headers)
7586             nil
7587             t))))
7588      (list header
7589            (read-string (format "%s header %s (regexp): "
7590                                 (if current-prefix-arg "Exclude" "Limit to")
7591                                 header))
7592            current-prefix-arg)))
7593   (when (not (equal "" regexp))
7594     (prog1
7595         (let ((articles (gnus-summary-find-matching
7596                          (cons 'extra header) regexp 'all nil nil
7597                          not-matching)))
7598           (unless articles
7599             (error "Found no matches for \"%s\"" regexp))
7600           (gnus-summary-limit articles))
7601       (gnus-summary-position-point))))
7602
7603 (defun gnus-summary-limit-to-display-predicate ()
7604   "Limit the summary buffer to the predicated in the `display' group parameter."
7605   (interactive)
7606   (unless gnus-newsgroup-display
7607     (error "There is no `display' group parameter"))
7608   (let (articles)
7609     (dolist (number gnus-newsgroup-articles)
7610       (when (funcall gnus-newsgroup-display)
7611         (push number articles)))
7612     (gnus-summary-limit articles))
7613   (gnus-summary-position-point))
7614
7615 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7616 (make-obsolete
7617  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7618
7619 (defun gnus-summary-limit-to-unread (&optional all)
7620   "Limit the summary buffer to articles that are not marked as read.
7621 If ALL is non-nil, limit strictly to unread articles."
7622   (interactive "P")
7623   (if all
7624       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7625     (gnus-summary-limit-to-marks
7626      ;; Concat all the marks that say that an article is read and have
7627      ;; those removed.
7628      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7629            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7630            gnus-low-score-mark gnus-expirable-mark
7631            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7632            gnus-duplicate-mark gnus-souped-mark)
7633      'reverse)))
7634
7635 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7636 (make-obsolete 'gnus-summary-delete-marked-with
7637                'gnus-summary-limit-exclude-marks)
7638
7639 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7640   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7641 If REVERSE, limit the summary buffer to articles that are marked
7642 with MARKS.  MARKS can either be a string of marks or a list of marks.
7643 Returns how many articles were removed."
7644   (interactive "sMarks: ")
7645   (gnus-summary-limit-to-marks marks t))
7646
7647 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7648   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7649 If REVERSE (the prefix), limit the summary buffer to articles that are
7650 not marked with MARKS.  MARKS can either be a string of marks or a
7651 list of marks.
7652 Returns how many articles were removed."
7653   (interactive "sMarks: \nP")
7654   (prog1
7655       (let ((data gnus-newsgroup-data)
7656             (marks (if (listp marks) marks
7657                      (append marks nil))) ; Transform to list.
7658             articles)
7659         (while data
7660           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7661                   (memq (gnus-data-mark (car data)) marks))
7662             (push (gnus-data-number (car data)) articles))
7663           (setq data (cdr data)))
7664         (gnus-summary-limit articles))
7665     (gnus-summary-position-point)))
7666
7667 (defun gnus-summary-limit-to-score (score)
7668   "Limit to articles with score at or above SCORE."
7669   (interactive "NLimit to articles with score of at least: ")
7670   (let ((data gnus-newsgroup-data)
7671         articles)
7672     (while data
7673       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7674                 score)
7675         (push (gnus-data-number (car data)) articles))
7676       (setq data (cdr data)))
7677     (prog1
7678         (gnus-summary-limit articles)
7679       (gnus-summary-position-point))))
7680
7681 (defun gnus-summary-limit-to-unseen ()
7682   "Limit to unseen articles."
7683   (interactive)
7684   (prog1
7685       (gnus-summary-limit gnus-newsgroup-unseen)
7686     (gnus-summary-position-point)))
7687
7688 (defun gnus-summary-limit-include-thread (id)
7689   "Display all the hidden articles that is in the thread with ID in it.
7690 When called interactively, ID is the Message-ID of the current
7691 article."
7692   (interactive (list (mail-header-id (gnus-summary-article-header))))
7693   (let ((articles (gnus-articles-in-thread
7694                    (gnus-id-to-thread (gnus-root-id id)))))
7695     (prog1
7696         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7697       (gnus-summary-limit-include-matching-articles
7698        "subject"
7699        (regexp-quote (gnus-simplify-subject-re
7700                       (mail-header-subject (gnus-id-to-header id)))))
7701       (gnus-summary-position-point))))
7702
7703 (defun gnus-summary-limit-include-matching-articles (header regexp)
7704   "Display all the hidden articles that have HEADERs that match REGEXP."
7705   (interactive (list (read-string "Match on header: ")
7706                      (read-string "Regexp: ")))
7707   (let ((articles (gnus-find-matching-articles header regexp)))
7708     (prog1
7709         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7710       (gnus-summary-position-point))))
7711
7712 (defun gnus-summary-insert-dormant-articles ()
7713   "Insert all the dormant articles for this group into the current buffer."
7714   (interactive)
7715   (let ((gnus-verbose (max 6 gnus-verbose)))
7716     (if (not gnus-newsgroup-dormant)
7717         (gnus-message 3 "No cached articles for this group")
7718       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7719
7720 (defun gnus-summary-limit-include-dormant ()
7721   "Display all the hidden articles that are marked as dormant.
7722 Note that this command only works on a subset of the articles currently
7723 fetched for this group."
7724   (interactive)
7725   (unless gnus-newsgroup-dormant
7726     (error "There are no dormant articles in this group"))
7727   (prog1
7728       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7729     (gnus-summary-position-point)))
7730
7731 (defun gnus-summary-limit-exclude-dormant ()
7732   "Hide all dormant articles."
7733   (interactive)
7734   (prog1
7735       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7736     (gnus-summary-position-point)))
7737
7738 (defun gnus-summary-limit-exclude-childless-dormant ()
7739   "Hide all dormant articles that have no children."
7740   (interactive)
7741   (let ((data (gnus-data-list t))
7742         articles d children)
7743     ;; Find all articles that are either not dormant or have
7744     ;; children.
7745     (while (setq d (pop data))
7746       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7747                 (and (setq children
7748                            (gnus-article-children (gnus-data-number d)))
7749                      (let (found)
7750                        (while children
7751                          (when (memq (car children) articles)
7752                            (setq children nil
7753                                  found t))
7754                          (pop children))
7755                        found)))
7756         (push (gnus-data-number d) articles)))
7757     ;; Do the limiting.
7758     (prog1
7759         (gnus-summary-limit articles)
7760       (gnus-summary-position-point))))
7761
7762 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7763   "Mark all unread excluded articles as read.
7764 If ALL, mark even excluded ticked and dormants as read."
7765   (interactive "P")
7766   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7767   (let ((articles (gnus-sorted-ndifference
7768                    (sort
7769                     (mapcar (lambda (h) (mail-header-number h))
7770                             gnus-newsgroup-headers)
7771                     '<)
7772                    gnus-newsgroup-limit))
7773         article)
7774     (setq gnus-newsgroup-unreads
7775           (gnus-sorted-intersection gnus-newsgroup-unreads
7776                                     gnus-newsgroup-limit))
7777     (if all
7778         (setq gnus-newsgroup-dormant nil
7779               gnus-newsgroup-marked nil
7780               gnus-newsgroup-reads
7781               (nconc
7782                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7783                gnus-newsgroup-reads))
7784       (while (setq article (pop articles))
7785         (unless (or (memq article gnus-newsgroup-dormant)
7786                     (memq article gnus-newsgroup-marked))
7787           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7788
7789 (defun gnus-summary-limit (articles &optional pop)
7790   (if pop
7791       ;; We pop the previous limit off the stack and use that.
7792       (setq articles (car gnus-newsgroup-limits)
7793             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7794     ;; We use the new limit, so we push the old limit on the stack.
7795     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7796   ;; Set the limit.
7797   (setq gnus-newsgroup-limit articles)
7798   (let ((total (length gnus-newsgroup-data))
7799         (data (gnus-data-find-list (gnus-summary-article-number)))
7800         (gnus-summary-mark-below nil)   ; Inhibit this.
7801         found)
7802     ;; This will do all the work of generating the new summary buffer
7803     ;; according to the new limit.
7804     (gnus-summary-prepare)
7805     ;; Hide any threads, possibly.
7806     (gnus-summary-maybe-hide-threads)
7807     ;; Try to return to the article you were at, or one in the
7808     ;; neighborhood.
7809     (when data
7810       ;; We try to find some article after the current one.
7811       (while data
7812         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7813           (setq data nil
7814                 found t))
7815         (setq data (cdr data))))
7816     (unless found
7817       ;; If there is no data, that means that we were after the last
7818       ;; article.  The same goes when we can't find any articles
7819       ;; after the current one.
7820       (goto-char (point-max))
7821       (gnus-summary-find-prev))
7822     (gnus-set-mode-line 'summary)
7823     ;; We return how many articles were removed from the summary
7824     ;; buffer as a result of the new limit.
7825     (- total (length gnus-newsgroup-data))))
7826
7827 (defsubst gnus-invisible-cut-children (threads)
7828   (let ((num 0))
7829     (while threads
7830       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7831         (incf num))
7832       (pop threads))
7833     (< num 2)))
7834
7835 (defsubst gnus-cut-thread (thread)
7836   "Go forwards in the thread until we find an article that we want to display."
7837   (when (or (eq gnus-fetch-old-headers 'some)
7838             (eq gnus-fetch-old-headers 'invisible)
7839             (numberp gnus-fetch-old-headers)
7840             (eq gnus-build-sparse-threads 'some)
7841             (eq gnus-build-sparse-threads 'more))
7842     ;; Deal with old-fetched headers and sparse threads.
7843     (while (and
7844             thread
7845             (or
7846              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7847              (gnus-summary-article-ancient-p
7848               (mail-header-number (car thread))))
7849             (if (or (<= (length (cdr thread)) 1)
7850                     (eq gnus-fetch-old-headers 'invisible))
7851                 (setq gnus-newsgroup-limit
7852                       (delq (mail-header-number (car thread))
7853                             gnus-newsgroup-limit)
7854                       thread (cadr thread))
7855               (when (gnus-invisible-cut-children (cdr thread))
7856                 (let ((th (cdr thread)))
7857                   (while th
7858                     (if (memq (mail-header-number (caar th))
7859                               gnus-newsgroup-limit)
7860                         (setq thread (car th)
7861                               th nil)
7862                       (setq th (cdr th))))))))))
7863   thread)
7864
7865 (defun gnus-cut-threads (threads)
7866   "Cut off all uninteresting articles from the beginning of THREADS."
7867   (when (or (eq gnus-fetch-old-headers 'some)
7868             (eq gnus-fetch-old-headers 'invisible)
7869             (numberp gnus-fetch-old-headers)
7870             (eq gnus-build-sparse-threads 'some)
7871             (eq gnus-build-sparse-threads 'more))
7872     (let ((th threads))
7873       (while th
7874         (setcar th (gnus-cut-thread (car th)))
7875         (setq th (cdr th)))))
7876   ;; Remove nixed out threads.
7877   (delq nil threads))
7878
7879 (defun gnus-summary-initial-limit (&optional show-if-empty)
7880   "Figure out what the initial limit is supposed to be on group entry.
7881 This entails weeding out unwanted dormants, low-scored articles,
7882 fetch-old-headers verbiage, and so on."
7883   ;; Most groups have nothing to remove.
7884   (if (or gnus-inhibit-limiting
7885           (and (null gnus-newsgroup-dormant)
7886                (eq gnus-newsgroup-display 'gnus-not-ignore)
7887                (not (eq gnus-fetch-old-headers 'some))
7888                (not (numberp gnus-fetch-old-headers))
7889                (not (eq gnus-fetch-old-headers 'invisible))
7890                (null gnus-summary-expunge-below)
7891                (not (eq gnus-build-sparse-threads 'some))
7892                (not (eq gnus-build-sparse-threads 'more))
7893                (null gnus-thread-expunge-below)
7894                (not gnus-use-nocem)))
7895       ()                                ; Do nothing.
7896     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7897     (setq gnus-newsgroup-limit nil)
7898     (mapatoms
7899      (lambda (node)
7900        (unless (car (symbol-value node))
7901          ;; These threads have no parents -- they are roots.
7902          (let ((nodes (cdr (symbol-value node)))
7903                thread)
7904            (while nodes
7905              (if (and gnus-thread-expunge-below
7906                       (< (gnus-thread-total-score (car nodes))
7907                          gnus-thread-expunge-below))
7908                  (gnus-expunge-thread (pop nodes))
7909                (setq thread (pop nodes))
7910                (gnus-summary-limit-children thread))))))
7911      gnus-newsgroup-dependencies)
7912     ;; If this limitation resulted in an empty group, we might
7913     ;; pop the previous limit and use it instead.
7914     (when (and (not gnus-newsgroup-limit)
7915                show-if-empty)
7916       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7917     gnus-newsgroup-limit))
7918
7919 (defun gnus-summary-limit-children (thread)
7920   "Return 1 if this subthread is visible and 0 if it is not."
7921   ;; First we get the number of visible children to this thread.  This
7922   ;; is done by recursing down the thread using this function, so this
7923   ;; will really go down to a leaf article first, before slowly
7924   ;; working its way up towards the root.
7925   (when thread
7926     (let* ((max-lisp-eval-depth 5000)
7927            (children
7928            (if (cdr thread)
7929                (apply '+ (mapcar 'gnus-summary-limit-children
7930                                  (cdr thread)))
7931              0))
7932           (number (mail-header-number (car thread)))
7933           score)
7934       (if (and
7935            (not (memq number gnus-newsgroup-marked))
7936            (or
7937             ;; If this article is dormant and has absolutely no visible
7938             ;; children, then this article isn't visible.
7939             (and (memq number gnus-newsgroup-dormant)
7940                  (zerop children))
7941             ;; If this is "fetch-old-headered" and there is no
7942             ;; visible children, then we don't want this article.
7943             (and (or (eq gnus-fetch-old-headers 'some)
7944                      (numberp gnus-fetch-old-headers))
7945                  (gnus-summary-article-ancient-p number)
7946                  (zerop children))
7947             ;; If this is "fetch-old-headered" and `invisible', then
7948             ;; we don't want this article.
7949             (and (eq gnus-fetch-old-headers 'invisible)
7950                  (gnus-summary-article-ancient-p number))
7951             ;; If this is a sparsely inserted article with no children,
7952             ;; we don't want it.
7953             (and (eq gnus-build-sparse-threads 'some)
7954                  (gnus-summary-article-sparse-p number)
7955                  (zerop children))
7956             ;; If we use expunging, and this article is really
7957             ;; low-scored, then we don't want this article.
7958             (when (and gnus-summary-expunge-below
7959                        (< (setq score
7960                                 (or (cdr (assq number gnus-newsgroup-scored))
7961                                     gnus-summary-default-score))
7962                           gnus-summary-expunge-below))
7963               ;; We increase the expunge-tally here, but that has
7964               ;; nothing to do with the limits, really.
7965               (incf gnus-newsgroup-expunged-tally)
7966               ;; We also mark as read here, if that's wanted.
7967               (when (and gnus-summary-mark-below
7968                          (< score gnus-summary-mark-below))
7969                 (setq gnus-newsgroup-unreads
7970                       (delq number gnus-newsgroup-unreads))
7971                 (if gnus-newsgroup-auto-expire
7972                     (push number gnus-newsgroup-expirable)
7973                   (push (cons number gnus-low-score-mark)
7974                         gnus-newsgroup-reads)))
7975               t)
7976             ;; Do the `display' group parameter.
7977             (and gnus-newsgroup-display
7978                  (not (funcall gnus-newsgroup-display)))
7979             ;; Check NoCeM things.
7980             (if (and gnus-use-nocem
7981                      (gnus-nocem-unwanted-article-p
7982                       (mail-header-id (car thread))))
7983                 (progn
7984                   (setq gnus-newsgroup-unreads
7985                         (delq number gnus-newsgroup-unreads))
7986                   t))))
7987           ;; Nope, invisible article.
7988           0
7989         ;; Ok, this article is to be visible, so we add it to the limit
7990         ;; and return 1.
7991         (push number gnus-newsgroup-limit)
7992         1))))
7993
7994 (defun gnus-expunge-thread (thread)
7995   "Mark all articles in THREAD as read."
7996   (let* ((number (mail-header-number (car thread))))
7997     (incf gnus-newsgroup-expunged-tally)
7998     ;; We also mark as read here, if that's wanted.
7999     (setq gnus-newsgroup-unreads
8000           (delq number gnus-newsgroup-unreads))
8001     (if gnus-newsgroup-auto-expire
8002         (push number gnus-newsgroup-expirable)
8003       (push (cons number gnus-low-score-mark)
8004             gnus-newsgroup-reads)))
8005   ;; Go recursively through all subthreads.
8006   (mapcar 'gnus-expunge-thread (cdr thread)))
8007
8008 ;; Summary article oriented commands
8009
8010 (defun gnus-summary-refer-parent-article (n)
8011   "Refer parent article N times.
8012 If N is negative, go to ancestor -N instead.
8013 The difference between N and the number of articles fetched is returned."
8014   (interactive "p")
8015   (let ((skip 1)
8016         error header ref)
8017     (when (not (natnump n))
8018       (setq skip (abs n)
8019             n 1))
8020     (while (and (> n 0)
8021                 (not error))
8022       (setq header (gnus-summary-article-header))
8023       (if (and (eq (mail-header-number header)
8024                    (cdr gnus-article-current))
8025                (equal gnus-newsgroup-name
8026                       (car gnus-article-current)))
8027           ;; If we try to find the parent of the currently
8028           ;; displayed article, then we take a look at the actual
8029           ;; References header, since this is slightly more
8030           ;; reliable than the References field we got from the
8031           ;; server.
8032           (save-excursion
8033             (set-buffer gnus-original-article-buffer)
8034             (nnheader-narrow-to-headers)
8035             (unless (setq ref (message-fetch-field "references"))
8036               (when (setq ref (message-fetch-field "in-reply-to"))
8037                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8038             (widen))
8039         (setq ref
8040               ;; It's not the current article, so we take a bet on
8041               ;; the value we got from the server.
8042               (mail-header-references header)))
8043       (if (and ref
8044                (not (equal ref "")))
8045           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8046             (gnus-message 1 "Couldn't find parent"))
8047         (gnus-message 1 "No references in article %d"
8048                       (gnus-summary-article-number))
8049         (setq error t))
8050       (decf n))
8051     (gnus-summary-position-point)
8052     n))
8053
8054 (defun gnus-summary-refer-references ()
8055   "Fetch all articles mentioned in the References header.
8056 Return the number of articles fetched."
8057   (interactive)
8058   (let ((ref (mail-header-references (gnus-summary-article-header)))
8059         (current (gnus-summary-article-number))
8060         (n 0))
8061     (if (or (not ref)
8062             (equal ref ""))
8063         (error "No References in the current article")
8064       ;; For each Message-ID in the References header...
8065       (while (string-match "<[^>]*>" ref)
8066         (incf n)
8067         ;; ... fetch that article.
8068         (gnus-summary-refer-article
8069          (prog1 (match-string 0 ref)
8070            (setq ref (substring ref (match-end 0))))))
8071       (gnus-summary-goto-subject current)
8072       (gnus-summary-position-point)
8073       n)))
8074
8075 (defun gnus-summary-refer-thread (&optional limit)
8076   "Fetch all articles in the current thread.
8077 If LIMIT (the numerical prefix), fetch that many old headers instead
8078 of what's specified by the `gnus-refer-thread-limit' variable."
8079   (interactive "P")
8080   (let ((id (mail-header-id (gnus-summary-article-header)))
8081         (limit (if limit (prefix-numeric-value limit)
8082                  gnus-refer-thread-limit)))
8083     (unless (eq gnus-fetch-old-headers 'invisible)
8084       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8085       ;; Retrieve the headers and read them in.
8086       (if (eq (if (numberp limit)
8087                   (gnus-retrieve-headers
8088                    (list (min
8089                           (+ (mail-header-number
8090                               (gnus-summary-article-header))
8091                              limit)
8092                           gnus-newsgroup-end))
8093                    gnus-newsgroup-name (* limit 2))
8094                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8095                 ;; headers.
8096                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8097                                        gnus-newsgroup-name limit))
8098               'nov)
8099           (gnus-build-all-threads)
8100         (error "Can't fetch thread from back ends that don't support NOV"))
8101       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8102     (gnus-summary-limit-include-thread id)))
8103
8104 (defun gnus-summary-refer-article (message-id)
8105   "Fetch an article specified by MESSAGE-ID."
8106   (interactive "sMessage-ID: ")
8107   (when (and (stringp message-id)
8108              (not (zerop (length message-id))))
8109     ;; Construct the correct Message-ID if necessary.
8110     ;; Suggested by tale@pawl.rpi.edu.
8111     (unless (string-match "^<" message-id)
8112       (setq message-id (concat "<" message-id)))
8113     (unless (string-match ">$" message-id)
8114       (setq message-id (concat message-id ">")))
8115     (let* ((header (gnus-id-to-header message-id))
8116            (sparse (and header
8117                         (gnus-summary-article-sparse-p
8118                          (mail-header-number header))
8119                         (memq (mail-header-number header)
8120                               gnus-newsgroup-limit)))
8121            number)
8122       (cond
8123        ;; If the article is present in the buffer we just go to it.
8124        ((and header
8125              (or (not (gnus-summary-article-sparse-p
8126                        (mail-header-number header)))
8127                  sparse))
8128         (prog1
8129             (gnus-summary-goto-article
8130              (mail-header-number header) nil t)
8131           (when sparse
8132             (gnus-summary-update-article (mail-header-number header)))))
8133        (t
8134         ;; We fetch the article.
8135         (catch 'found
8136           (dolist (gnus-override-method (gnus-refer-article-methods))
8137             (when (and (gnus-check-server gnus-override-method)
8138                        ;; Fetch the header,
8139                        (setq number (gnus-summary-insert-subject message-id)))
8140               ;; and display the article.
8141               (gnus-summary-select-article nil nil nil number)
8142               (throw 'found t)))
8143           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8144
8145 (defun gnus-refer-article-methods ()
8146   "Return a list of referable methods."
8147   (cond
8148    ;; No method, so we default to current and native.
8149    ((null gnus-refer-article-method)
8150     (list gnus-current-select-method gnus-select-method))
8151    ;; Current.
8152    ((eq 'current gnus-refer-article-method)
8153     (list gnus-current-select-method))
8154    ;; List of select methods.
8155    ((not (and (symbolp (car gnus-refer-article-method))
8156               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8157     (let (out)
8158       (dolist (method gnus-refer-article-method)
8159         (push (if (eq 'current method)
8160                   gnus-current-select-method
8161                 method)
8162               out))
8163       (nreverse out)))
8164    ;; One single select method.
8165    (t
8166     (list gnus-refer-article-method))))
8167
8168 (defun gnus-summary-edit-parameters ()
8169   "Edit the group parameters of the current group."
8170   (interactive)
8171   (gnus-group-edit-group gnus-newsgroup-name 'params))
8172
8173 (defun gnus-summary-customize-parameters ()
8174   "Customize the group parameters of the current group."
8175   (interactive)
8176   (gnus-group-customize gnus-newsgroup-name))
8177
8178 (defun gnus-summary-enter-digest-group (&optional force)
8179   "Enter an nndoc group based on the current article.
8180 If FORCE, force a digest interpretation.  If not, try
8181 to guess what the document format is."
8182   (interactive "P")
8183   (let ((conf gnus-current-window-configuration))
8184     (save-window-excursion
8185       (save-excursion
8186         (let (gnus-article-prepare-hook
8187               gnus-display-mime-function
8188               gnus-break-pages)
8189           (gnus-summary-select-article))))
8190     (setq gnus-current-window-configuration conf)
8191     (let* ((name (format "%s-%d"
8192                          (gnus-group-prefixed-name
8193                           gnus-newsgroup-name (list 'nndoc ""))
8194                          (save-excursion
8195                            (set-buffer gnus-summary-buffer)
8196                            gnus-current-article)))
8197            (ogroup gnus-newsgroup-name)
8198            (params (append (gnus-info-params (gnus-get-info ogroup))
8199                            (list (cons 'to-group ogroup))
8200                            (list (cons 'save-article-group ogroup))))
8201            (case-fold-search t)
8202            (buf (current-buffer))
8203            dig to-address)
8204       (save-excursion
8205         (set-buffer gnus-original-article-buffer)
8206         ;; Have the digest group inherit the main mail address of
8207         ;; the parent article.
8208         (when (setq to-address (or (gnus-fetch-field "reply-to")
8209                                    (gnus-fetch-field "from")))
8210           (setq params (append
8211                         (list (cons 'to-address
8212                                     (funcall gnus-decode-encoded-word-function
8213                                              to-address))))))
8214         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8215         (insert-buffer-substring gnus-original-article-buffer)
8216         ;; Remove lines that may lead nndoc to misinterpret the
8217         ;; document type.
8218         (narrow-to-region
8219          (goto-char (point-min))
8220          (or (search-forward "\n\n" nil t) (point)))
8221         (goto-char (point-min))
8222         (delete-matching-lines "^Path:\\|^From ")
8223         (widen))
8224       (unwind-protect
8225           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8226                     (gnus-newsgroup-ephemeral-ignored-charsets
8227                      gnus-newsgroup-ignored-charsets))
8228                 (gnus-group-read-ephemeral-group
8229                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8230                               (nndoc-article-type
8231                                ,(if force 'mbox 'guess)))
8232                  t nil nil nil
8233                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8234                                                         "ADAPT")))))
8235               ;; Make all postings to this group go to the parent group.
8236               (nconc (gnus-info-params (gnus-get-info name))
8237                      params)
8238             ;; Couldn't select this doc group.
8239             (switch-to-buffer buf)
8240             (gnus-set-global-variables)
8241             (gnus-configure-windows 'summary)
8242             (gnus-message 3 "Article couldn't be entered?"))
8243         (kill-buffer dig)))))
8244
8245 (defun gnus-summary-read-document (n)
8246   "Open a new group based on the current article(s).
8247 This will allow you to read digests and other similar
8248 documents as newsgroups.
8249 Obeys the standard process/prefix convention."
8250   (interactive "P")
8251   (let* ((articles (gnus-summary-work-articles n))
8252          (ogroup gnus-newsgroup-name)
8253          (params (append (gnus-info-params (gnus-get-info ogroup))
8254                          (list (cons 'to-group ogroup))))
8255          article group egroup groups vgroup)
8256     (while (setq article (pop articles))
8257       (setq group (format "%s-%d" gnus-newsgroup-name article))
8258       (gnus-summary-remove-process-mark article)
8259       (when (gnus-summary-display-article article)
8260         (save-excursion
8261           (with-temp-buffer
8262             (insert-buffer-substring gnus-original-article-buffer)
8263             ;; Remove some headers that may lead nndoc to make
8264             ;; the wrong guess.
8265             (message-narrow-to-head)
8266             (goto-char (point-min))
8267             (delete-matching-lines "^\\(Path\\):\\|^From ")
8268             (widen)
8269             (if (setq egroup
8270                       (gnus-group-read-ephemeral-group
8271                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8272                                      (nndoc-article-type guess))
8273                        t nil t))
8274                 (progn
8275                   ;; Make all postings to this group go to the parent group.
8276                   (nconc (gnus-info-params (gnus-get-info egroup))
8277                          params)
8278                   (push egroup groups))
8279               ;; Couldn't select this doc group.
8280               (gnus-error 3 "Article couldn't be entered"))))))
8281     ;; Now we have selected all the documents.
8282     (cond
8283      ((not groups)
8284       (error "None of the articles could be interpreted as documents"))
8285      ((gnus-group-read-ephemeral-group
8286        (setq vgroup (format
8287                      "nnvirtual:%s-%s" gnus-newsgroup-name
8288                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8289        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8290        t
8291        (cons (current-buffer) 'summary)))
8292      (t
8293       (error "Couldn't select virtual nndoc group")))))
8294
8295 (defun gnus-summary-isearch-article (&optional regexp-p)
8296   "Do incremental search forward on the current article.
8297 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8298   (interactive "P")
8299   (let* ((gnus-inhibit-treatment t)
8300          (old (gnus-summary-select-article)))
8301     (gnus-configure-windows 'article)
8302     (gnus-eval-in-buffer-window gnus-article-buffer
8303       (save-restriction
8304         (widen)
8305         (when (eq 'old old)
8306           (gnus-article-show-all-headers))
8307         (goto-char (point-min))
8308         (isearch-forward regexp-p)))))
8309
8310 (defun gnus-summary-search-article-forward (regexp &optional backward)
8311   "Search for an article containing REGEXP forward.
8312 If BACKWARD, search backward instead."
8313   (interactive
8314    (list (read-string
8315           (format "Search article %s (regexp%s): "
8316                   (if current-prefix-arg "backward" "forward")
8317                   (if gnus-last-search-regexp
8318                       (concat ", default " gnus-last-search-regexp)
8319                     "")))
8320          current-prefix-arg))
8321   (if (string-equal regexp "")
8322       (setq regexp (or gnus-last-search-regexp ""))
8323     (setq gnus-last-search-regexp regexp)
8324     (setq gnus-article-before-search gnus-current-article))
8325   ;; Intentionally set gnus-last-article.
8326   (setq gnus-last-article gnus-article-before-search)
8327   (let ((gnus-last-article gnus-last-article))
8328     (if (gnus-summary-search-article regexp backward)
8329         (gnus-summary-show-thread)
8330       (error "Search failed: \"%s\"" regexp))))
8331
8332 (defun gnus-summary-search-article-backward (regexp)
8333   "Search for an article containing REGEXP backward."
8334   (interactive
8335    (list (read-string
8336           (format "Search article backward (regexp%s): "
8337                   (if gnus-last-search-regexp
8338                       (concat ", default " gnus-last-search-regexp)
8339                     "")))))
8340   (gnus-summary-search-article-forward regexp 'backward))
8341
8342 (eval-when-compile
8343   (defmacro gnus-summary-search-article-position-point (regexp backward)
8344     "Dehighlight the last matched text and goto the beginning position."
8345     (` (if (and gnus-summary-search-article-matched-data
8346                 (let ((text (caddr gnus-summary-search-article-matched-data))
8347                       (inhibit-read-only t)
8348                       buffer-read-only)
8349                   (delete-region
8350                    (goto-char (car gnus-summary-search-article-matched-data))
8351                    (cadr gnus-summary-search-article-matched-data))
8352                   (insert text)
8353                   (string-match (, regexp) text)))
8354            (if (, backward) (beginning-of-line) (end-of-line))
8355          (goto-char (if (, backward) (point-max) (point-min))))))
8356
8357   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8358     "Place point where X-Face image is displayed."
8359     (if (featurep 'xemacs)
8360         (` (let ((end (if (search-forward "\n\n" nil t)
8361                           (goto-char (1- (point)))
8362                         (point-min)))
8363                  extent)
8364              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8365              (unless (and (re-search-forward "^From:" end t)
8366                           (setq extent (extent-at (point)))
8367                           (extent-begin-glyph extent))
8368                (goto-char (, opoint)))))
8369       (` (let ((end (if (search-forward "\n\n" nil t)
8370                         (goto-char (1- (point)))
8371                       (point-min)))
8372                (start (or (search-backward "\n\n" nil t) (point-min))))
8373            (goto-char
8374             (or (text-property-any start end 'x-face-image t);; x-face-e21
8375                 (text-property-any start end 'x-face-mule-bitmap-image t)
8376                 (, opoint)))))))
8377
8378   (defmacro gnus-summary-search-article-highlight-matched-text
8379     (backward treated x-face)
8380     "Highlight matched text in the function `gnus-summary-search-article'."
8381     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8382              (end (set-marker (make-marker) (match-end 0)))
8383              (inhibit-read-only t)
8384              buffer-read-only)
8385          (unless treated
8386            (let ((,@
8387                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8388                     (mapcar
8389                      (lambda (item) (setq items (delq item items)))
8390                      '(gnus-treat-buttonize
8391                        gnus-treat-fill-article
8392                        gnus-treat-fill-long-lines
8393                        gnus-treat-emphasize
8394                        gnus-treat-highlight-headers
8395                        gnus-treat-highlight-citation
8396                        gnus-treat-highlight-signature
8397                        gnus-treat-overstrike
8398                        gnus-treat-display-xface
8399                        gnus-treat-buttonize-head
8400                        gnus-treat-decode-article-as-default-mime-charset))
8401                     (static-if (featurep 'xemacs)
8402                         items
8403                       (cons '(x-face-mule-delete-x-face-field
8404                               (quote never))
8405                             items))))
8406                  (gnus-treat-display-xface
8407                   (when (, x-face) gnus-treat-display-xface)))
8408              (gnus-article-prepare-mime-display)))
8409          (goto-char (if (, backward) start end))
8410          (when (, x-face)
8411            (gnus-summary-search-article-highlight-goto-x-face (point)))
8412          (setq gnus-summary-search-article-matched-data
8413                (list start end (buffer-substring start end)))
8414          (unless (eq start end);; matched text has been deleted. :-<
8415            (put-text-property start end 'face
8416                               (or (find-face 'isearch)
8417                                   'secondary-selection))))))
8418   )
8419
8420 (defun gnus-summary-search-article (regexp &optional backward)
8421   "Search for an article containing REGEXP.
8422 Optional argument BACKWARD means do search for backward.
8423 `gnus-select-article-hook' is not called during the search."
8424   ;; We have to require this here to make sure that the following
8425   ;; dynamic binding isn't shadowed by autoloading.
8426   (require 'gnus-async)
8427   (require 'gnus-art)
8428   (let ((gnus-select-article-hook nil)  ;Disable hook.
8429         (gnus-article-prepare-hook nil)
8430         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8431         (gnus-use-article-prefetch nil)
8432         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8433         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8434         (gnus-visual nil)
8435         (gnus-keep-backlog nil)
8436         (gnus-break-pages nil)
8437         (gnus-summary-display-arrow nil)
8438         (gnus-updated-mode-lines nil)
8439         (gnus-auto-center-summary nil)
8440         (sum (current-buffer))
8441         (found nil)
8442         point treated)
8443     (gnus-save-hidden-threads
8444       (static-if (featurep 'xemacs)
8445           (let ((gnus-inhibit-treatment t))
8446             (setq treated (eq 'old (gnus-summary-select-article)))
8447             (when (and treated
8448                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8449                                  (window-live-p (get-buffer-window
8450                                                  gnus-article-buffer t)))))
8451               (gnus-summary-select-article nil t)
8452               (setq treated nil)))
8453         (let ((gnus-inhibit-treatment t)
8454               (x-face-mule-delete-x-face-field 'never))
8455           (setq treated (eq 'old (gnus-summary-select-article)))
8456           (when (and treated
8457                      (not
8458                       (and (gnus-buffer-live-p gnus-article-buffer)
8459                            (window-live-p (get-buffer-window
8460                                            gnus-article-buffer t))
8461                            (or (not (string-match "^\\^X-Face:" regexp))
8462                                (with-current-buffer gnus-article-buffer
8463                                  gnus-summary-search-article-matched-data)))))
8464             (gnus-summary-select-article nil t)
8465             (setq treated nil))))
8466       (set-buffer gnus-article-buffer)
8467       (widen)
8468       (if treated
8469           (progn
8470             (gnus-article-show-all-headers)
8471             (gnus-summary-search-article-position-point regexp backward))
8472         (goto-char (if backward (point-max) (point-min))))
8473       (while (not found)
8474         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8475         (if (if backward
8476                 (re-search-backward regexp nil t)
8477               (re-search-forward regexp nil t))
8478             ;; We found the regexp.
8479             (progn
8480               (gnus-summary-search-article-highlight-matched-text
8481                backward treated (string-match "^\\^X-Face:" regexp))
8482               (setq found 'found)
8483               (forward-line
8484                (/ (- 2 (window-height
8485                         (get-buffer-window gnus-article-buffer t)))
8486                   2))
8487               (set-window-start
8488                (get-buffer-window (current-buffer))
8489                (point))
8490               (set-buffer sum)
8491               (setq point (point)))
8492           ;; We didn't find it, so we go to the next article.
8493           (set-buffer sum)
8494           (setq found 'not)
8495           (while (eq found 'not)
8496             (if (not (if backward (gnus-summary-find-prev)
8497                        (gnus-summary-find-next)))
8498                 ;; No more articles.
8499                 (setq found t)
8500               ;; Select the next article and adjust point.
8501               (unless (gnus-summary-article-sparse-p
8502                        (gnus-summary-article-number))
8503                 (setq found nil)
8504                 (let ((gnus-inhibit-treatment t))
8505                   (gnus-summary-select-article))
8506                 (setq treated nil)
8507                 (set-buffer gnus-article-buffer)
8508                 (widen)
8509                 (goto-char (if backward (point-max) (point-min))))))))
8510       (gnus-message 7 ""))
8511     ;; Return whether we found the regexp.
8512     (when (eq found 'found)
8513       (goto-char point)
8514       (gnus-summary-show-thread)
8515       (gnus-summary-goto-subject gnus-current-article)
8516       (gnus-summary-position-point)
8517       t)))
8518
8519 (defun gnus-find-matching-articles (header regexp)
8520   "Return a list of all articles that match REGEXP on HEADER.
8521 This search includes all articles in the current group that Gnus has
8522 fetched headers for, whether they are displayed or not."
8523   (let ((articles nil)
8524         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8525         (case-fold-search t))
8526     (dolist (header gnus-newsgroup-headers)
8527       (when (string-match regexp (funcall func header))
8528         (push (mail-header-number header) articles)))
8529     (nreverse articles)))
8530
8531 (defun gnus-summary-find-matching (header regexp &optional backward unread
8532                                           not-case-fold not-matching)
8533   "Return a list of all articles that match REGEXP on HEADER.
8534 The search stars on the current article and goes forwards unless
8535 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8536 If UNREAD is non-nil, only unread articles will
8537 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8538 in the comparisons. If NOT-MATCHING, return a list of all articles that
8539 not match REGEXP on HEADER."
8540   (let ((case-fold-search (not not-case-fold))
8541         articles d func)
8542     (if (consp header)
8543         (if (eq (car header) 'extra)
8544             (setq func
8545                   `(lambda (h)
8546                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8547                          "")))
8548           (error "%s is an invalid header" header))
8549       (unless (fboundp (intern (concat "mail-header-" header)))
8550         (error "%s is not a valid header" header))
8551       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8552     (dolist (d (if (eq backward 'all)
8553                    gnus-newsgroup-data
8554                  (gnus-data-find-list
8555                   (gnus-summary-article-number)
8556                   (gnus-data-list backward))))
8557       (when (and (or (not unread)       ; We want all articles...
8558                      (gnus-data-unread-p d)) ; Or just unreads.
8559                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8560                  (if not-matching
8561                      (not (string-match
8562                            regexp
8563                            (funcall func (gnus-data-header d))))
8564                    (string-match regexp
8565                                  (funcall func (gnus-data-header d)))))
8566         (push (gnus-data-number d) articles))) ; Success!
8567     (nreverse articles)))
8568
8569 (defun gnus-summary-execute-command (header regexp command &optional backward)
8570   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8571 If HEADER is an empty string (or nil), the match is done on the entire
8572 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8573   (interactive
8574    (list (let ((completion-ignore-case t))
8575            (completing-read
8576             "Header name: "
8577             (mapcar (lambda (header) (list (format "%s" header)))
8578                     (append
8579                      '("Number" "Subject" "From" "Lines" "Date"
8580                        "Message-ID" "Xref" "References" "Body")
8581                      gnus-extra-headers))
8582             nil 'require-match))
8583          (read-string "Regexp: ")
8584          (read-key-sequence "Command: ")
8585          current-prefix-arg))
8586   (when (equal header "Body")
8587     (setq header ""))
8588   ;; Hidden thread subtrees must be searched as well.
8589   (gnus-summary-show-all-threads)
8590   ;; We don't want to change current point nor window configuration.
8591   (save-excursion
8592     (save-window-excursion
8593       (let (gnus-visual
8594             gnus-treat-strip-trailing-blank-lines
8595             gnus-treat-strip-leading-blank-lines
8596             gnus-treat-strip-multiple-blank-lines
8597             gnus-treat-hide-boring-headers
8598             gnus-treat-fold-newsgroups
8599             gnus-article-prepare-hook)
8600         (gnus-message 6 "Executing %s..." (key-description command))
8601         ;; We'd like to execute COMMAND interactively so as to give arguments.
8602         (gnus-execute header regexp
8603                       `(call-interactively ',(key-binding command))
8604                       backward)
8605         (gnus-message 6 "Executing %s...done" (key-description command))))))
8606
8607 (defun gnus-summary-beginning-of-article ()
8608   "Scroll the article back to the beginning."
8609   (interactive)
8610   (gnus-summary-select-article)
8611   (gnus-configure-windows 'article)
8612   (gnus-eval-in-buffer-window gnus-article-buffer
8613     (widen)
8614     (goto-char (point-min))
8615     (when gnus-page-broken
8616       (gnus-narrow-to-page))))
8617
8618 (defun gnus-summary-end-of-article ()
8619   "Scroll to the end of the article."
8620   (interactive)
8621   (gnus-summary-select-article)
8622   (gnus-configure-windows 'article)
8623   (gnus-eval-in-buffer-window gnus-article-buffer
8624     (widen)
8625     (goto-char (point-max))
8626     (recenter -3)
8627     (when gnus-page-broken
8628       (gnus-narrow-to-page))))
8629
8630 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8631   "Truncate to LEN and quote all \"(\"'s in STRING."
8632   (gnus-replace-in-string (if (and len (> (length string) len))
8633                               (substring string 0 len)
8634                             string)
8635                           "[()]" "\\\\\\&"))
8636
8637 (defun gnus-summary-print-article (&optional filename n)
8638   "Generate and print a PostScript image of the N next (mail) articles.
8639
8640 If N is negative, print the N previous articles.  If N is nil and articles
8641 have been marked with the process mark, print these instead.
8642
8643 If the optional first argument FILENAME is nil, send the image to the
8644 printer.  If FILENAME is a string, save the PostScript image in a file with
8645 that name.  If FILENAME is a number, prompt the user for the name of the file
8646 to save in."
8647   (interactive (list (ps-print-preprint current-prefix-arg)))
8648   (dolist (article (gnus-summary-work-articles n))
8649     (gnus-summary-select-article nil nil 'pseudo article)
8650     (gnus-eval-in-buffer-window gnus-article-buffer
8651       (gnus-print-buffer))
8652     (gnus-summary-remove-process-mark article))
8653   (ps-despool filename))
8654
8655 (defun gnus-print-buffer ()
8656   (let ((buffer (generate-new-buffer " *print*")))
8657     (unwind-protect
8658         (progn
8659           (copy-to-buffer buffer (point-min) (point-max))
8660           (set-buffer buffer)
8661           (gnus-remove-text-with-property 'gnus-decoration)
8662           (when (gnus-visual-p 'article-highlight 'highlight)
8663             ;; Copy-to-buffer doesn't copy overlay.  So redo
8664             ;; highlight.
8665             (let ((gnus-article-buffer buffer))
8666               (gnus-article-highlight-citation t)
8667               (gnus-article-highlight-signature)
8668               (gnus-article-emphasize)
8669               (gnus-article-delete-invisible-text)))
8670           (let ((ps-left-header
8671                  (list
8672                   (concat "("
8673                           (gnus-summary-print-truncate-and-quote
8674                            (mail-header-subject gnus-current-headers)
8675                            66) ")")
8676                   (concat "("
8677                           (gnus-summary-print-truncate-and-quote
8678                            (mail-header-from gnus-current-headers)
8679                            45) ")")))
8680                 (ps-right-header
8681                  (list
8682                   "/pagenumberstring load"
8683                   (concat "("
8684                           (mail-header-date gnus-current-headers) ")"))))
8685             (gnus-run-hooks 'gnus-ps-print-hook)
8686             (save-excursion
8687               (if window-system
8688                   (ps-spool-buffer-with-faces)
8689                 (ps-spool-buffer)))))
8690       (kill-buffer buffer))))
8691
8692 (defun gnus-summary-show-article (&optional arg)
8693   "Force redisplaying of the current article.
8694 If ARG (the prefix) is a number, show the article with the charset
8695 defined in `gnus-summary-show-article-charset-alist', or the charset
8696 input.
8697 If ARG (the prefix) is non-nil and not a number, show the raw article
8698 without any article massaging functions being run.  Normally, the key
8699 strokes are `C-u g'."
8700   (interactive "P")
8701   (cond
8702    ((numberp arg)
8703     (gnus-summary-show-article t)
8704     (let* ((gnus-newsgroup-charset
8705             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8706                 (mm-read-coding-system
8707                  "View as charset: " ;; actually it is coding system.
8708                  (save-excursion
8709                    (set-buffer gnus-article-buffer)
8710                    (mm-detect-coding-region (point) (point-max))))))
8711            (default-mime-charset gnus-newsgroup-charset)
8712            (gnus-newsgroup-ignored-charsets 'gnus-all))
8713       (gnus-summary-select-article nil 'force)
8714       (let ((deps gnus-newsgroup-dependencies)
8715             head header lines)
8716         (save-excursion
8717           (set-buffer gnus-original-article-buffer)
8718           (save-restriction
8719             (message-narrow-to-head)
8720             (setq head (buffer-string))
8721             (goto-char (point-min))
8722             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8723               (goto-char (point-max))
8724               (widen)
8725               (setq lines (1- (count-lines (point) (point-max))))))
8726           (with-temp-buffer
8727             (insert (format "211 %d Article retrieved.\n"
8728                             (cdr gnus-article-current)))
8729             (insert head)
8730             (if lines (insert (format "Lines: %d\n" lines)))
8731             (insert ".\n")
8732             (let ((nntp-server-buffer (current-buffer)))
8733               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8734         (gnus-data-set-header
8735          (gnus-data-find (cdr gnus-article-current))
8736          header)
8737         (gnus-summary-update-article-line
8738          (cdr gnus-article-current) header)
8739         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8740           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8741    ((not arg)
8742     ;; Select the article the normal way.
8743     (gnus-summary-select-article nil 'force))
8744    (t
8745     ;; We have to require this here to make sure that the following
8746     ;; dynamic binding isn't shadowed by autoloading.
8747     (require 'gnus-async)
8748     (require 'gnus-art)
8749     ;; Bind the article treatment functions to nil.
8750     (let ((gnus-have-all-headers t)
8751           gnus-article-prepare-hook
8752           gnus-article-decode-hook
8753           gnus-break-pages
8754           gnus-show-mime
8755           (gnus-inhibit-treatment t))
8756       (gnus-summary-select-article nil 'force))))
8757   (gnus-summary-goto-subject gnus-current-article)
8758   (gnus-summary-position-point))
8759
8760 (defun gnus-summary-show-raw-article ()
8761   "Show the raw article without any article massaging functions being run."
8762   (interactive)
8763   (gnus-summary-show-article t))
8764
8765 (defun gnus-summary-verbose-headers (&optional arg)
8766   "Toggle permanent full header display.
8767 If ARG is a positive number, turn header display on.
8768 If ARG is a negative number, turn header display off."
8769   (interactive "P")
8770   (setq gnus-show-all-headers
8771         (cond ((or (not (numberp arg))
8772                    (zerop arg))
8773                (not gnus-show-all-headers))
8774               ((natnump arg)
8775                t)))
8776   (gnus-summary-show-article))
8777
8778 (defun gnus-summary-toggle-header (&optional arg)
8779   "Show the headers if they are hidden, or hide them if they are shown.
8780 If ARG is a positive number, show the entire header.
8781 If ARG is a negative number, hide the unwanted header lines."
8782   (interactive "P")
8783   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8784                      (get-buffer-window gnus-article-buffer t))))
8785     (with-current-buffer gnus-article-buffer
8786       (widen)
8787       (article-narrow-to-head)
8788       (let* ((buffer-read-only nil)
8789              (inhibit-point-motion-hooks t)
8790              (hidden (if (numberp arg)
8791                          (>= arg 0)
8792                        (gnus-article-hidden-text-p 'headers)))
8793              s e)
8794         (delete-region (point-min) (point-max))
8795         (with-current-buffer gnus-original-article-buffer
8796           (goto-char (setq s (point-min)))
8797           (setq e (if (search-forward "\n\n" nil t)
8798                       (1- (point))
8799                     (point-max))))
8800         (insert-buffer-substring gnus-original-article-buffer s e)
8801         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8802         ;; article-decode-encoded-words by default.
8803         (article-decode-encoded-words)
8804         (run-hooks 'gnus-article-decode-hook)
8805         (if hidden
8806             (let ((gnus-treat-hide-headers nil)
8807                   (gnus-treat-hide-boring-headers nil))
8808               (gnus-delete-wash-type 'headers)
8809               (gnus-treat-article 'head))
8810           (gnus-treat-article 'head))
8811         (widen)
8812         (if window
8813             (set-window-start window (goto-char (point-min))))
8814         (setq gnus-page-broken
8815               (when gnus-break-pages
8816                 (gnus-narrow-to-page)
8817                 t))
8818         (gnus-set-mode-line 'article)))))
8819
8820 (defun gnus-summary-show-all-headers ()
8821   "Make all header lines visible."
8822   (interactive)
8823   (gnus-summary-toggle-header 1))
8824
8825 (defun gnus-summary-toggle-mime (&optional arg)
8826   "Toggle MIME processing.
8827 If ARG is a positive number, turn MIME processing on."
8828   (interactive "P")
8829   (setq gnus-show-mime
8830         (if (null arg)
8831             (not gnus-show-mime)
8832           (> (prefix-numeric-value arg) 0)))
8833   (gnus-summary-select-article t 'force))
8834
8835 (defun gnus-summary-caesar-message (&optional arg)
8836   "Caesar rotate the current article by 13.
8837 The numerical prefix specifies how many places to rotate each letter
8838 forward."
8839   (interactive "P")
8840   (gnus-summary-select-article)
8841   (let ((mail-header-separator ""))
8842     (gnus-eval-in-buffer-window gnus-article-buffer
8843       (save-restriction
8844         (widen)
8845         (let ((start (window-start))
8846               buffer-read-only)
8847           (message-caesar-buffer-body arg)
8848           (set-window-start (get-buffer-window (current-buffer)) start))))))
8849
8850 (autoload 'unmorse-region "morse"
8851   "Convert morse coded text in region to ordinary ASCII text."
8852   t)
8853
8854 (defun gnus-summary-morse-message (&optional arg)
8855   "Morse decode the current article."
8856   (interactive "P")
8857   (gnus-summary-select-article)
8858   (let ((mail-header-separator ""))
8859     (gnus-eval-in-buffer-window gnus-article-buffer
8860       (save-excursion
8861         (save-restriction
8862           (widen)
8863           (let ((pos (window-start))
8864                 buffer-read-only)
8865             (goto-char (point-min))
8866             (when (message-goto-body)
8867               (gnus-narrow-to-body))
8868             (goto-char (point-min))
8869             (while (re-search-forward "·" (point-max) t)
8870               (replace-match "."))
8871             (unmorse-region (point-min) (point-max))
8872             (widen)
8873             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8874
8875 (defun gnus-summary-stop-page-breaking ()
8876   "Stop page breaking in the current article."
8877   (interactive)
8878   (gnus-summary-select-article)
8879   (gnus-eval-in-buffer-window gnus-article-buffer
8880     (widen)
8881     (when (gnus-visual-p 'page-marker)
8882       (let ((buffer-read-only nil))
8883         (gnus-remove-text-with-property 'gnus-prev)
8884         (gnus-remove-text-with-property 'gnus-next))
8885       (setq gnus-page-broken nil))))
8886
8887 (defun gnus-summary-move-article (&optional n to-newsgroup
8888                                             select-method action)
8889   "Move the current article to a different newsgroup.
8890 If N is a positive number, move the N next articles.
8891 If N is a negative number, move the N previous articles.
8892 If N is nil and any articles have been marked with the process mark,
8893 move those articles instead.
8894 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8895 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8896 re-spool using this method.
8897
8898 When called interactively with TO-NEWSGROUP being nil, the value of
8899 the variable `gnus-move-split-methods' is used for finding a default
8900 for the target newsgroup.
8901
8902 For this function to work, both the current newsgroup and the
8903 newsgroup that you want to move to have to support the `request-move'
8904 and `request-accept' functions.
8905
8906 ACTION can be either `move' (the default), `crosspost' or `copy'."
8907   (interactive "P")
8908   (unless action
8909     (setq action 'move))
8910   ;; Check whether the source group supports the required functions.
8911   (cond ((and (eq action 'move)
8912               (not (gnus-check-backend-function
8913                     'request-move-article gnus-newsgroup-name)))
8914          (error "The current group does not support article moving"))
8915         ((and (eq action 'crosspost)
8916               (not (gnus-check-backend-function
8917                     'request-replace-article gnus-newsgroup-name)))
8918          (error "The current group does not support article editing")))
8919   (let ((articles (gnus-summary-work-articles n))
8920         (prefix (if (gnus-check-backend-function
8921                      'request-move-article gnus-newsgroup-name)
8922                     (gnus-group-real-prefix gnus-newsgroup-name)
8923                   ""))
8924         (names '((move "Move" "Moving")
8925                  (copy "Copy" "Copying")
8926                  (crosspost "Crosspost" "Crossposting")))
8927         (copy-buf (save-excursion
8928                     (nnheader-set-temp-buffer " *copy article*")))
8929         art-group to-method new-xref article to-groups)
8930     (unless (assq action names)
8931       (error "Unknown action %s" action))
8932     ;; Read the newsgroup name.
8933     (when (and (not to-newsgroup)
8934                (not select-method))
8935       (if (and gnus-move-split-methods
8936                (not
8937                 (and (memq gnus-current-article articles)
8938                      (gnus-buffer-live-p gnus-original-article-buffer))))
8939           ;; When `gnus-move-split-methods' is non-nil, we have to
8940           ;; select an article to give `gnus-read-move-group-name' an
8941           ;; opportunity to suggest an appropriate default.  However,
8942           ;; we needn't render or mark the article.
8943           (let ((gnus-display-mime-function nil)
8944                 (gnus-article-prepare-hook nil)
8945                 (gnus-mark-article-hook nil))
8946             (gnus-summary-select-article nil nil nil (car articles))))
8947       (setq to-newsgroup
8948             (gnus-read-move-group-name
8949              (cadr (assq action names))
8950              (symbol-value (intern (format "gnus-current-%s-group" action)))
8951              articles prefix))
8952       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8953     (setq to-method (or select-method
8954                         (gnus-server-to-method
8955                          (gnus-group-method to-newsgroup))))
8956     ;; Check the method we are to move this article to...
8957     (unless (gnus-check-backend-function
8958              'request-accept-article (car to-method))
8959       (error "%s does not support article copying" (car to-method)))
8960     (unless (gnus-check-server to-method)
8961       (error "Can't open server %s" (car to-method)))
8962     (gnus-message 6 "%s to %s: %s..."
8963                   (caddr (assq action names))
8964                   (or (car select-method) to-newsgroup) articles)
8965     (while articles
8966       (setq article (pop articles))
8967       (setq
8968        art-group
8969        (cond
8970         ;; Move the article.
8971         ((eq action 'move)
8972          ;; Remove this article from future suppression.
8973          (gnus-dup-unsuppress-article article)
8974          (gnus-request-move-article
8975           article                       ; Article to move
8976           gnus-newsgroup-name           ; From newsgroup
8977           (nth 1 (gnus-find-method-for-group
8978                   gnus-newsgroup-name)) ; Server
8979           (list 'gnus-request-accept-article
8980                 to-newsgroup (list 'quote select-method)
8981                 (not articles) t)       ; Accept form
8982           (not articles)))              ; Only save nov last time
8983         ;; Copy the article.
8984         ((eq action 'copy)
8985          (save-excursion
8986            (set-buffer copy-buf)
8987            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8988              (gnus-request-accept-article
8989               to-newsgroup select-method (not articles) t))))
8990         ;; Crosspost the article.
8991         ((eq action 'crosspost)
8992          (let ((xref (message-tokenize-header
8993                       (mail-header-xref (gnus-summary-article-header article))
8994                       " ")))
8995            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8996                                   ":" (number-to-string article)))
8997            (unless xref
8998              (setq xref (list (system-name))))
8999            (setq new-xref
9000                  (concat
9001                   (mapconcat 'identity
9002                              (delete "Xref:" (delete new-xref xref))
9003                              " ")
9004                   " " new-xref))
9005            (save-excursion
9006              (set-buffer copy-buf)
9007              ;; First put the article in the destination group.
9008              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9009              (when (consp (setq art-group
9010                                 (gnus-request-accept-article
9011                                  to-newsgroup select-method (not articles))))
9012                (setq new-xref (concat new-xref " " (car art-group)
9013                                       ":"
9014                                       (number-to-string (cdr art-group))))
9015                ;; Now we have the new Xrefs header, so we insert
9016                ;; it and replace the new article.
9017                (nnheader-replace-header "Xref" new-xref)
9018                (gnus-request-replace-article
9019                 (cdr art-group) to-newsgroup (current-buffer))
9020                art-group))))))
9021       (cond
9022        ((not art-group)
9023         (gnus-message 1 "Couldn't %s article %s: %s"
9024                       (cadr (assq action names)) article
9025                       (nnheader-get-report (car to-method))))
9026        ((eq art-group 'junk)
9027         (when (eq action 'move)
9028           (gnus-summary-mark-article article gnus-canceled-mark)
9029           (gnus-message 4 "Deleted article %s" article)
9030           ;; run the delete hook
9031           (run-hook-with-args 'gnus-summary-article-delete-hook
9032                               action
9033                               (gnus-data-header
9034                                (assoc article (gnus-data-list nil)))
9035                               gnus-newsgroup-name nil
9036                               select-method)))
9037        (t
9038         (let* ((pto-group (gnus-group-prefixed-name
9039                            (car art-group) to-method))
9040                (entry
9041                 (gnus-gethash pto-group gnus-newsrc-hashtb))
9042                (info (nth 2 entry))
9043                (to-group (gnus-info-group info))
9044                to-marks)
9045           ;; Update the group that has been moved to.
9046           (when (and info
9047                      (memq action '(move copy)))
9048             (unless (member to-group to-groups)
9049               (push to-group to-groups))
9050
9051             (unless (memq article gnus-newsgroup-unreads)
9052               (push 'read to-marks)
9053               (gnus-info-set-read
9054                info (gnus-add-to-range (gnus-info-read info)
9055                                        (list (cdr art-group)))))
9056
9057             ;; See whether the article is to be put in the cache.
9058             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9059                              gnus-article-mark-lists
9060                            (delete '(expirable . expire)
9061                                    (copy-sequence gnus-article-mark-lists))))
9062                   (to-article (cdr art-group)))
9063
9064               ;; Enter the article into the cache in the new group,
9065               ;; if that is required.
9066               (when gnus-use-cache
9067                 (gnus-cache-possibly-enter-article
9068                  to-group to-article
9069                  (let ((header (copy-sequence
9070                                 (gnus-summary-article-header article))))
9071                    (mail-header-set-number header to-article)
9072                    header)
9073                  (memq article gnus-newsgroup-marked)
9074                  (memq article gnus-newsgroup-dormant)
9075                  (memq article gnus-newsgroup-unreads)))
9076
9077               (when gnus-preserve-marks
9078                 ;; Copy any marks over to the new group.
9079                 (when (and (equal to-group gnus-newsgroup-name)
9080                            (not (memq article gnus-newsgroup-unreads)))
9081                   ;; Mark this article as read in this group.
9082                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9083                   (setcdr (gnus-active to-group) to-article)
9084                   (setcdr gnus-newsgroup-active to-article))
9085
9086                 (while marks
9087                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9088                     (when (memq article (symbol-value
9089                                          (intern (format "gnus-newsgroup-%s"
9090                                                          (caar marks)))))
9091                       (push (cdar marks) to-marks)
9092                       ;; If the other group is the same as this group,
9093                       ;; then we have to add the mark to the list.
9094                       (when (equal to-group gnus-newsgroup-name)
9095                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9096                              (cons to-article
9097                                    (symbol-value
9098                                     (intern (format "gnus-newsgroup-%s"
9099                                                     (caar marks)))))))
9100                       ;; Copy the marks to other group.
9101                       (gnus-add-marked-articles
9102                        to-group (cdar marks) (list to-article) info)))
9103                   (setq marks (cdr marks)))
9104
9105                 (gnus-request-set-mark
9106                  to-group (list (list (list to-article) 'add to-marks))))
9107
9108               (gnus-dribble-enter
9109                (concat "(gnus-group-set-info '"
9110                        (gnus-prin1-to-string (gnus-get-info to-group))
9111                        ")"))))
9112
9113           ;; Update the Xref header in this article to point to
9114           ;; the new crossposted article we have just created.
9115           (when (eq action 'crosspost)
9116             (save-excursion
9117               (set-buffer copy-buf)
9118               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9119               (nnheader-replace-header "Xref" new-xref)
9120               (gnus-request-replace-article
9121                article gnus-newsgroup-name (current-buffer))))
9122
9123           ;; run the move/copy/crosspost/respool hook
9124           (run-hook-with-args 'gnus-summary-article-move-hook
9125                               action
9126                               (gnus-data-header
9127                                (assoc article (gnus-data-list nil)))
9128                               gnus-newsgroup-name
9129                               to-newsgroup
9130                               select-method))
9131
9132         ;;;!!!Why is this necessary?
9133         (set-buffer gnus-summary-buffer)
9134         
9135         (gnus-summary-goto-subject article)
9136         (when (eq action 'move)
9137           (gnus-summary-mark-article article gnus-canceled-mark))))
9138       (gnus-summary-remove-process-mark article))
9139     ;; Re-activate all groups that have been moved to.
9140     (save-excursion
9141       (set-buffer gnus-group-buffer)
9142       (let ((gnus-group-marked to-groups))
9143         (gnus-group-get-new-news-this-group nil t)))
9144
9145     (gnus-kill-buffer copy-buf)
9146     (gnus-summary-position-point)
9147     (gnus-set-mode-line 'summary)))
9148
9149 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9150   "Move the current article to a different newsgroup.
9151 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9152 When called interactively, if TO-NEWSGROUP is nil, use the value of
9153 the variable `gnus-move-split-methods' for finding a default target
9154 newsgroup.
9155 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9156 re-spool using this method."
9157   (interactive "P")
9158   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9159
9160 (defun gnus-summary-crosspost-article (&optional n)
9161   "Crosspost the current article to some other group."
9162   (interactive "P")
9163   (gnus-summary-move-article n nil nil 'crosspost))
9164
9165 (defcustom gnus-summary-respool-default-method nil
9166   "Default method type for respooling an article.
9167 If nil, use to the current newsgroup method."
9168   :type 'symbol
9169   :group 'gnus-summary-mail)
9170
9171 (defcustom gnus-summary-display-while-building nil
9172   "If non-nil, show and update the summary buffer as it's being built.
9173 If the value is t, update the buffer after every line is inserted.  If
9174 the value is an integer (N), update the display every N lines."
9175   :group 'gnus-thread
9176   :type '(choice (const :tag "off" nil)
9177                  number
9178                  (const :tag "frequently" t)))
9179
9180 (defun gnus-summary-respool-article (&optional n method)
9181   "Respool the current article.
9182 The article will be squeezed through the mail spooling process again,
9183 which means that it will be put in some mail newsgroup or other
9184 depending on `nnmail-split-methods'.
9185 If N is a positive number, respool the N next articles.
9186 If N is a negative number, respool the N previous articles.
9187 If N is nil and any articles have been marked with the process mark,
9188 respool those articles instead.
9189
9190 Respooling can be done both from mail groups and \"real\" newsgroups.
9191 In the former case, the articles in question will be moved from the
9192 current group into whatever groups they are destined to.  In the
9193 latter case, they will be copied into the relevant groups."
9194   (interactive
9195    (list current-prefix-arg
9196          (let* ((methods (gnus-methods-using 'respool))
9197                 (methname
9198                  (symbol-name (or gnus-summary-respool-default-method
9199                                   (car (gnus-find-method-for-group
9200                                         gnus-newsgroup-name)))))
9201                 (method
9202                  (gnus-completing-read-with-default
9203                   methname "What backend do you want to use when respooling?"
9204                   methods nil t nil 'gnus-mail-method-history))
9205                 ms)
9206            (cond
9207             ((zerop (length (setq ms (gnus-servers-using-backend
9208                                       (intern method)))))
9209              (list (intern method) ""))
9210             ((= 1 (length ms))
9211              (car ms))
9212             (t
9213              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9214                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9215                            ms-alist))))))))
9216   (unless method
9217     (error "No method given for respooling"))
9218   (if (assoc (symbol-name
9219               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9220              (gnus-methods-using 'respool))
9221       (gnus-summary-move-article n nil method)
9222     (gnus-summary-copy-article n nil method)))
9223
9224 (defun gnus-summary-import-article (file &optional edit)
9225   "Import an arbitrary file into a mail newsgroup."
9226   (interactive "fImport file: \nP")
9227   (let ((group gnus-newsgroup-name)
9228         (now (current-time))
9229         atts lines group-art)
9230     (unless (gnus-check-backend-function 'request-accept-article group)
9231       (error "%s does not support article importing" group))
9232     (or (file-readable-p file)
9233         (not (file-regular-p file))
9234         (error "Can't read %s" file))
9235     (save-excursion
9236       (set-buffer (gnus-get-buffer-create " *import file*"))
9237       (erase-buffer)
9238       (nnheader-insert-file-contents file)
9239       (goto-char (point-min))
9240       (if (nnheader-article-p)
9241           (save-restriction
9242             (goto-char (point-min))
9243             (search-forward "\n\n" nil t)
9244             (narrow-to-region (point-min) (1- (point)))
9245             (goto-char (point-min))
9246             (unless (re-search-forward "^date:" nil t)
9247               (goto-char (point-max))
9248               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9249         ;; This doesn't look like an article, so we fudge some headers.
9250         (setq atts (file-attributes file)
9251               lines (count-lines (point-min) (point-max)))
9252         (insert "From: " (read-string "From: ") "\n"
9253                 "Subject: " (read-string "Subject: ") "\n"
9254                 "Date: " (message-make-date (nth 5 atts)) "\n"
9255                 "Message-ID: " (message-make-message-id) "\n"
9256                 "Lines: " (int-to-string lines) "\n"
9257                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9258       (setq group-art (gnus-request-accept-article group nil t))
9259       (kill-buffer (current-buffer)))
9260     (setq gnus-newsgroup-active (gnus-activate-group group))
9261     (forward-line 1)
9262     (gnus-summary-goto-article (cdr group-art) nil t)
9263     (when edit
9264       (gnus-summary-edit-article))))
9265
9266 (defun gnus-summary-create-article ()
9267   "Create an article in a mail newsgroup."
9268   (interactive)
9269   (let ((group gnus-newsgroup-name)
9270         (now (current-time))
9271         group-art)
9272     (unless (gnus-check-backend-function 'request-accept-article group)
9273       (error "%s does not support article importing" group))
9274     (save-excursion
9275       (set-buffer (gnus-get-buffer-create " *import file*"))
9276       (erase-buffer)
9277       (goto-char (point-min))
9278       ;; This doesn't look like an article, so we fudge some headers.
9279       (insert "From: " (read-string "From: ") "\n"
9280               "Subject: " (read-string "Subject: ") "\n"
9281               "Date: " (message-make-date now) "\n"
9282               "Message-ID: " (message-make-message-id) "\n")
9283       (setq group-art (gnus-request-accept-article group nil t))
9284       (kill-buffer (current-buffer)))
9285     (setq gnus-newsgroup-active (gnus-activate-group group))
9286     (forward-line 1)
9287     (gnus-summary-goto-article (cdr group-art) nil t)
9288     (gnus-summary-edit-article)))
9289
9290 (defun gnus-summary-article-posted-p ()
9291   "Say whether the current (mail) article is available from news as well.
9292 This will be the case if the article has both been mailed and posted."
9293   (interactive)
9294   (let ((id (mail-header-references (gnus-summary-article-header)))
9295         (gnus-override-method (car (gnus-refer-article-methods))))
9296     (if (gnus-request-head id "")
9297         (gnus-message 2 "The current message was found on %s"
9298                       gnus-override-method)
9299       (gnus-message 2 "The current message couldn't be found on %s"
9300                     gnus-override-method)
9301       nil)))
9302
9303 (defun gnus-summary-expire-articles (&optional now)
9304   "Expire all articles that are marked as expirable in the current group."
9305   (interactive)
9306   (when (and (not gnus-group-is-exiting-without-update-p)
9307              (gnus-check-backend-function
9308               'request-expire-articles gnus-newsgroup-name))
9309     ;; This backend supports expiry.
9310     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9311            (expirable (if total
9312                           (progn
9313                             ;; We need to update the info for
9314                             ;; this group for `gnus-list-of-read-articles'
9315                             ;; to give us the right answer.
9316                             (gnus-run-hooks 'gnus-exit-group-hook)
9317                             (gnus-summary-update-info)
9318                             (gnus-list-of-read-articles gnus-newsgroup-name))
9319                         (setq gnus-newsgroup-expirable
9320                               (sort gnus-newsgroup-expirable '<))))
9321            (expiry-wait (if now 'immediate
9322                           (gnus-group-find-parameter
9323                            gnus-newsgroup-name 'expiry-wait)))
9324            (nnmail-expiry-target
9325             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9326                 nnmail-expiry-target))
9327            es)
9328       (when expirable
9329         ;; There are expirable articles in this group, so we run them
9330         ;; through the expiry process.
9331         (gnus-message 6 "Expiring articles...")
9332         (unless (gnus-check-group gnus-newsgroup-name)
9333           (error "Can't open server for %s" gnus-newsgroup-name))
9334         ;; The list of articles that weren't expired is returned.
9335         (save-excursion
9336           (if expiry-wait
9337               (let ((nnmail-expiry-wait-function nil)
9338                     (nnmail-expiry-wait expiry-wait))
9339                 (setq es (gnus-request-expire-articles
9340                           expirable gnus-newsgroup-name)))
9341             (setq es (gnus-request-expire-articles
9342                       expirable gnus-newsgroup-name)))
9343           (unless total
9344             (setq gnus-newsgroup-expirable es))
9345           ;; We go through the old list of expirable, and mark all
9346           ;; really expired articles as nonexistent.
9347           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9348             (let ((gnus-use-cache nil))
9349               (dolist (article expirable)
9350                 (when (and (not (memq article es))
9351                            (gnus-data-find article))
9352                   (gnus-summary-mark-article article gnus-canceled-mark)
9353                   (run-hook-with-args 'gnus-summary-article-expire-hook
9354                                       'delete
9355                                       (gnus-data-header
9356                                        (assoc article (gnus-data-list nil)))
9357                                       gnus-newsgroup-name
9358                                       nil
9359                                       nil))))))
9360         (gnus-message 6 "Expiring articles...done")))))
9361
9362 (defun gnus-summary-expire-articles-now ()
9363   "Expunge all expirable articles in the current group.
9364 This means that *all* articles that are marked as expirable will be
9365 deleted forever, right now."
9366   (interactive)
9367   (or gnus-expert-user
9368       (gnus-yes-or-no-p
9369        "Are you really, really, really sure you want to delete all these messages? ")
9370       (error "Phew!"))
9371   (gnus-summary-expire-articles t))
9372
9373 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9374 (defun gnus-summary-delete-article (&optional n)
9375   "Delete the N next (mail) articles.
9376 This command actually deletes articles.  This is not a marking
9377 command.  The article will disappear forever from your life, never to
9378 return.
9379
9380 If N is negative, delete backwards.
9381 If N is nil and articles have been marked with the process mark,
9382 delete these instead.
9383
9384 If `gnus-novice-user' is non-nil you will be asked for
9385 confirmation before the articles are deleted."
9386   (interactive "P")
9387   (unless (gnus-check-backend-function 'request-expire-articles
9388                                        gnus-newsgroup-name)
9389     (error "The current newsgroup does not support article deletion"))
9390   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9391     (error "Couldn't open server"))
9392   ;; Compute the list of articles to delete.
9393   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9394         (nnmail-expiry-target 'delete)
9395         not-deleted)
9396     (if (and gnus-novice-user
9397              (not (gnus-yes-or-no-p
9398                    (format "Do you really want to delete %s forever? "
9399                            (if (> (length articles) 1)
9400                                (format "these %s articles" (length articles))
9401                              "this article")))))
9402         ()
9403       ;; Delete the articles.
9404       (setq not-deleted (gnus-request-expire-articles
9405                          articles gnus-newsgroup-name 'force))
9406       (while articles
9407         (gnus-summary-remove-process-mark (car articles))
9408         ;; The backend might not have been able to delete the article
9409         ;; after all.
9410         (unless (memq (car articles) not-deleted)
9411           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9412         (let* ((article (car articles))
9413                (id (mail-header-id (gnus-data-header
9414                                     (assoc article (gnus-data-list nil))))))
9415           (run-hook-with-args 'gnus-summary-article-delete-hook
9416                               'delete id gnus-newsgroup-name nil
9417                               nil))
9418         (setq articles (cdr articles)))
9419       (when not-deleted
9420         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9421     (gnus-summary-position-point)
9422     (gnus-set-mode-line 'summary)
9423     not-deleted))
9424
9425 (defun gnus-summary-edit-article (&optional force)
9426   "Edit the current article.
9427 This will have permanent effect only in mail groups.
9428 If FORCE is non-nil, allow editing of articles even in read-only
9429 groups."
9430   (interactive "P")
9431   (save-excursion
9432     (set-buffer gnus-summary-buffer)
9433     (let ((mail-parse-charset gnus-newsgroup-charset)
9434           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9435       (gnus-set-global-variables)
9436       (when (and (not force)
9437                  (gnus-group-read-only-p))
9438         (error "The current newsgroup does not support article editing"))
9439       (gnus-summary-show-article t)
9440       (gnus-article-edit-article
9441        'ignore
9442        `(lambda (no-highlight)
9443           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9444                 (message-options message-options)
9445                 (message-options-set-recipient)
9446                 (mail-parse-ignored-charsets
9447                  ',gnus-newsgroup-ignored-charsets))
9448             (gnus-summary-edit-article-done
9449              ,(or (mail-header-references gnus-current-headers) "")
9450              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9451
9452 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9453
9454 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9455                                                  no-highlight)
9456   "Make edits to the current article permanent."
9457   (interactive)
9458   (save-excursion
9459     ;; The buffer restriction contains the entire article if it exists.
9460     (when (article-goto-body)
9461       (let ((lines (count-lines (point) (point-max)))
9462             (length (- (point-max) (point)))
9463             (case-fold-search t)
9464             (body (copy-marker (point))))
9465         (goto-char (point-min))
9466         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9467           (delete-region (match-beginning 1) (match-end 1))
9468           (insert (number-to-string length)))
9469         (goto-char (point-min))
9470         (when (re-search-forward
9471                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9472           (delete-region (match-beginning 1) (match-end 1))
9473           (insert (number-to-string length)))
9474         (goto-char (point-min))
9475         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9476           (delete-region (match-beginning 1) (match-end 1))
9477           (insert (number-to-string lines))))))
9478   ;; Replace the article.
9479   (let ((buf (current-buffer)))
9480     (with-temp-buffer
9481       (insert-buffer-substring buf)
9482
9483       (if (and (not read-only)
9484                (not (gnus-request-replace-article
9485                      (cdr gnus-article-current) (car gnus-article-current)
9486                      (current-buffer) t)))
9487           (error "Couldn't replace article")
9488         ;; Update the summary buffer.
9489         (if (and references
9490                  (equal (message-tokenize-header references " ")
9491                         (message-tokenize-header
9492                          (or (message-fetch-field "references") "") " ")))
9493             ;; We only have to update this line.
9494             (save-excursion
9495               (save-restriction
9496                 (message-narrow-to-head)
9497                 (let ((head (buffer-string))
9498                       header)
9499                   (with-temp-buffer
9500                     (insert (format "211 %d Article retrieved.\n"
9501                                     (cdr gnus-article-current)))
9502                     (insert head)
9503                     (insert ".\n")
9504                     (let ((nntp-server-buffer (current-buffer)))
9505                       (setq header (car (gnus-get-newsgroup-headers
9506                                          nil t))))
9507                     (save-excursion
9508                       (set-buffer gnus-summary-buffer)
9509                       (gnus-data-set-header
9510                        (gnus-data-find (cdr gnus-article-current))
9511                        header)
9512                       (gnus-summary-update-article-line
9513                        (cdr gnus-article-current) header)
9514                       (if (gnus-summary-goto-subject
9515                            (cdr gnus-article-current) nil t)
9516                           (gnus-summary-update-secondary-mark
9517                            (cdr gnus-article-current))))))))
9518           ;; Update threads.
9519           (set-buffer (or buffer gnus-summary-buffer))
9520           (gnus-summary-update-article (cdr gnus-article-current))
9521           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9522               (gnus-summary-update-secondary-mark
9523                (cdr gnus-article-current))))
9524         ;; Prettify the article buffer again.
9525         (unless no-highlight
9526           (save-excursion
9527             (set-buffer gnus-article-buffer)
9528             ;;;!!! Fix this -- article should be rehighlighted.
9529             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9530             (set-buffer gnus-original-article-buffer)
9531             (gnus-request-article
9532              (cdr gnus-article-current)
9533              (car gnus-article-current) (current-buffer))))
9534         ;; Prettify the summary buffer line.
9535         (when (gnus-visual-p 'summary-highlight 'highlight)
9536           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9537
9538 (defun gnus-summary-edit-wash (key)
9539   "Perform editing command KEY in the article buffer."
9540   (interactive
9541    (list
9542     (progn
9543       (message "%s" (concat (this-command-keys) "- "))
9544       (read-char))))
9545   (message "")
9546   (gnus-summary-edit-article)
9547   (execute-kbd-macro (concat (this-command-keys) key))
9548   (gnus-article-edit-done))
9549
9550 ;;; Respooling
9551
9552 (defun gnus-summary-respool-query (&optional silent trace)
9553   "Query where the respool algorithm would put this article."
9554   (interactive)
9555   (let (gnus-mark-article-hook)
9556     (gnus-summary-select-article)
9557     (save-excursion
9558       (set-buffer gnus-original-article-buffer)
9559       (save-restriction
9560         (message-narrow-to-head)
9561         (let ((groups (nnmail-article-group 'identity trace)))
9562           (unless silent
9563             (if groups
9564                 (message "This message would go to %s"
9565                          (mapconcat 'car groups ", "))
9566               (message "This message would go to no groups"))
9567             groups))))))
9568
9569 (defun gnus-summary-respool-trace ()
9570   "Trace where the respool algorithm would put this article.
9571 Display a buffer showing all fancy splitting patterns which matched."
9572   (interactive)
9573   (gnus-summary-respool-query nil t))
9574
9575 ;; Summary marking commands.
9576
9577 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9578   "Mark articles which has the same subject as read, and then select the next.
9579 If UNMARK is positive, remove any kind of mark.
9580 If UNMARK is negative, tick articles."
9581   (interactive "P")
9582   (when unmark
9583     (setq unmark (prefix-numeric-value unmark)))
9584   (let ((count
9585          (gnus-summary-mark-same-subject
9586           (gnus-summary-article-subject) unmark)))
9587     ;; Select next unread article.  If auto-select-same mode, should
9588     ;; select the first unread article.
9589     (gnus-summary-next-article t (and gnus-auto-select-same
9590                                       (gnus-summary-article-subject)))
9591     (gnus-message 7 "%d article%s marked as %s"
9592                   count (if (= count 1) " is" "s are")
9593                   (if unmark "unread" "read"))))
9594
9595 (defun gnus-summary-kill-same-subject (&optional unmark)
9596   "Mark articles which has the same subject as read.
9597 If UNMARK is positive, remove any kind of mark.
9598 If UNMARK is negative, tick articles."
9599   (interactive "P")
9600   (when unmark
9601     (setq unmark (prefix-numeric-value unmark)))
9602   (let ((count
9603          (gnus-summary-mark-same-subject
9604           (gnus-summary-article-subject) unmark)))
9605     ;; If marked as read, go to next unread subject.
9606     (when (null unmark)
9607       ;; Go to next unread subject.
9608       (gnus-summary-next-subject 1 t))
9609     (gnus-message 7 "%d articles are marked as %s"
9610                   count (if unmark "unread" "read"))))
9611
9612 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9613   "Mark articles with same SUBJECT as read, and return marked number.
9614 If optional argument UNMARK is positive, remove any kinds of marks.
9615 If optional argument UNMARK is negative, mark articles as unread instead."
9616   (let ((count 1))
9617     (save-excursion
9618       (cond
9619        ((null unmark)                   ; Mark as read.
9620         (while (and
9621                 (progn
9622                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9623                   (gnus-summary-show-thread) t)
9624                 (gnus-summary-find-subject subject))
9625           (setq count (1+ count))))
9626        ((> unmark 0)                    ; Tick.
9627         (while (and
9628                 (progn
9629                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9630                   (gnus-summary-show-thread) t)
9631                 (gnus-summary-find-subject subject))
9632           (setq count (1+ count))))
9633        (t                               ; Mark as unread.
9634         (while (and
9635                 (progn
9636                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9637                   (gnus-summary-show-thread) t)
9638                 (gnus-summary-find-subject subject))
9639           (setq count (1+ count)))))
9640       (gnus-set-mode-line 'summary)
9641       ;; Return the number of marked articles.
9642       count)))
9643
9644 (defun gnus-summary-mark-as-processable (n &optional unmark)
9645   "Set the process mark on the next N articles.
9646 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9647 the process mark instead.  The difference between N and the actual
9648 number of articles marked is returned."
9649   (interactive "P")
9650   (if (and (null n) (gnus-region-active-p))
9651       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9652     (setq n (prefix-numeric-value n))
9653     (let ((backward (< n 0))
9654           (n (abs n)))
9655       (while (and
9656               (> n 0)
9657               (if unmark
9658                   (gnus-summary-remove-process-mark
9659                    (gnus-summary-article-number))
9660                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9661               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9662         (setq n (1- n)))
9663       (when (/= 0 n)
9664         (gnus-message 7 "No more articles"))
9665       (gnus-summary-recenter)
9666       (gnus-summary-position-point)
9667       n)))
9668
9669 (defun gnus-summary-unmark-as-processable (n)
9670   "Remove the process mark from the next N articles.
9671 If N is negative, unmark backward instead.  The difference between N and
9672 the actual number of articles unmarked is returned."
9673   (interactive "P")
9674   (gnus-summary-mark-as-processable n t))
9675
9676 (defun gnus-summary-unmark-all-processable ()
9677   "Remove the process mark from all articles."
9678   (interactive)
9679   (save-excursion
9680     (while gnus-newsgroup-processable
9681       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9682   (gnus-summary-position-point))
9683
9684 (defun gnus-summary-add-mark (article type)
9685   "Mark ARTICLE with a mark of TYPE."
9686   (let ((vtype (car (assq type gnus-article-mark-lists)))
9687         var)
9688     (if (not vtype)
9689         (error "No such mark type: %s" type)
9690       (setq var (intern (format "gnus-newsgroup-%s" type)))
9691       (set var (cons article (symbol-value var)))
9692       (if (memq type '(processable cached replied forwarded recent saved))
9693           (gnus-summary-update-secondary-mark article)
9694         ;;; !!! This is bogus.  We should find out what primary
9695         ;;; !!! mark we want to set.
9696         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9697
9698 (defun gnus-summary-mark-as-expirable (n)
9699   "Mark N articles forward as expirable.
9700 If N is negative, mark backward instead.  The difference between N and
9701 the actual number of articles marked is returned."
9702   (interactive "p")
9703   (gnus-summary-mark-forward n gnus-expirable-mark))
9704
9705 (defun gnus-summary-mark-as-spam (n)
9706   "Mark N articles forward as spam.
9707 If N is negative, mark backward instead.  The difference between N and
9708 the actual number of articles marked is returned."
9709   (interactive "p")
9710   (gnus-summary-mark-forward n gnus-spam-mark))
9711
9712 (defun gnus-summary-mark-article-as-replied (article)
9713   "Mark ARTICLE as replied to and update the summary line.
9714 ARTICLE can also be a list of articles."
9715   (interactive (list (gnus-summary-article-number)))
9716   (let ((articles (if (listp article) article (list article))))
9717     (dolist (article articles)
9718       (unless (numberp article)
9719         (error "%s is not a number" article))
9720       (push article gnus-newsgroup-replied)
9721       (let ((buffer-read-only nil))
9722         (when (gnus-summary-goto-subject article nil t)
9723           (gnus-summary-update-secondary-mark article))))))
9724
9725 (defun gnus-summary-mark-article-as-forwarded (article)
9726   "Mark ARTICLE as forwarded and update the summary line.
9727 ARTICLE can also be a list of articles."
9728   (let ((articles (if (listp article) article (list article))))
9729     (dolist (article articles)
9730       (push article gnus-newsgroup-forwarded)
9731       (let ((buffer-read-only nil))
9732         (when (gnus-summary-goto-subject article nil t)
9733           (gnus-summary-update-secondary-mark article))))))
9734
9735 (defun gnus-summary-set-bookmark (article)
9736   "Set a bookmark in current article."
9737   (interactive (list (gnus-summary-article-number)))
9738   (when (or (not (get-buffer gnus-article-buffer))
9739             (not gnus-current-article)
9740             (not gnus-article-current)
9741             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9742     (error "No current article selected"))
9743   ;; Remove old bookmark, if one exists.
9744   (gnus-pull article gnus-newsgroup-bookmarks)
9745   ;; Set the new bookmark, which is on the form
9746   ;; (article-number . line-number-in-body).
9747   (push
9748    (cons article
9749          (save-excursion
9750            (set-buffer gnus-article-buffer)
9751            (count-lines
9752             (min (point)
9753                  (save-excursion
9754                    (article-goto-body)
9755                    (point)))
9756             (point))))
9757    gnus-newsgroup-bookmarks)
9758   (gnus-message 6 "A bookmark has been added to the current article."))
9759
9760 (defun gnus-summary-remove-bookmark (article)
9761   "Remove the bookmark from the current article."
9762   (interactive (list (gnus-summary-article-number)))
9763   ;; Remove old bookmark, if one exists.
9764   (if (not (assq article gnus-newsgroup-bookmarks))
9765       (gnus-message 6 "No bookmark in current article.")
9766     (gnus-pull article gnus-newsgroup-bookmarks)
9767     (gnus-message 6 "Removed bookmark.")))
9768
9769 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9770 (defun gnus-summary-mark-as-dormant (n)
9771   "Mark N articles forward as dormant.
9772 If N is negative, mark backward instead.  The difference between N and
9773 the actual number of articles marked is returned."
9774   (interactive "p")
9775   (gnus-summary-mark-forward n gnus-dormant-mark))
9776
9777 (defun gnus-summary-set-process-mark (article)
9778   "Set the process mark on ARTICLE and update the summary line."
9779   (setq gnus-newsgroup-processable
9780         (cons article
9781               (delq article gnus-newsgroup-processable)))
9782   (when (gnus-summary-goto-subject article)
9783     (gnus-summary-show-thread)
9784     (gnus-summary-goto-subject article)
9785     (gnus-summary-update-secondary-mark article)))
9786
9787 (defun gnus-summary-remove-process-mark (article)
9788   "Remove the process mark from ARTICLE and update the summary line."
9789   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9790   (when (gnus-summary-goto-subject article)
9791     (gnus-summary-show-thread)
9792     (gnus-summary-goto-subject article)
9793     (gnus-summary-update-secondary-mark article)))
9794
9795 (defun gnus-summary-set-saved-mark (article)
9796   "Set the process mark on ARTICLE and update the summary line."
9797   (push article gnus-newsgroup-saved)
9798   (when (gnus-summary-goto-subject article)
9799     (gnus-summary-update-secondary-mark article)))
9800
9801 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9802   "Mark N articles as read forwards.
9803 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9804 The difference between N and the actual number of articles marked is
9805 returned.
9806 If NO-EXPIRE, auto-expiry will be inhibited."
9807   (interactive "p")
9808   (gnus-summary-show-thread)
9809   (let ((backward (< n 0))
9810         (gnus-summary-goto-unread
9811          (and gnus-summary-goto-unread
9812               (not (eq gnus-summary-goto-unread 'never))
9813               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9814                                     gnus-ticked-mark gnus-dormant-mark)))))
9815         (n (abs n))
9816         (mark (or mark gnus-del-mark)))
9817     (while (and (> n 0)
9818                 (gnus-summary-mark-article nil mark no-expire)
9819                 (zerop (gnus-summary-next-subject
9820                         (if backward -1 1)
9821                         (and gnus-summary-goto-unread
9822                              (not (eq gnus-summary-goto-unread 'never)))
9823                         t)))
9824       (setq n (1- n)))
9825     (when (/= 0 n)
9826       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9827     (gnus-summary-recenter)
9828     (gnus-summary-position-point)
9829     (gnus-set-mode-line 'summary)
9830     n))
9831
9832 (defun gnus-summary-mark-article-as-read (mark)
9833   "Mark the current article quickly as read with MARK."
9834   (let ((article (gnus-summary-article-number)))
9835     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9836     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9837     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9838     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9839     (push (cons article mark) gnus-newsgroup-reads)
9840     ;; Possibly remove from cache, if that is used.
9841     (when gnus-use-cache
9842       (gnus-cache-enter-remove-article article))
9843     ;; Allow the backend to change the mark.
9844     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9845     ;; Check for auto-expiry.
9846     (when (and gnus-newsgroup-auto-expire
9847                (memq mark gnus-auto-expirable-marks))
9848       (setq mark gnus-expirable-mark)
9849       ;; Let the backend know about the mark change.
9850       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9851       (push article gnus-newsgroup-expirable))
9852     ;; Set the mark in the buffer.
9853     (gnus-summary-update-mark mark 'unread)
9854     t))
9855
9856 (defun gnus-summary-mark-article-as-unread (mark)
9857   "Mark the current article quickly as unread with MARK."
9858   (let* ((article (gnus-summary-article-number))
9859          (old-mark (gnus-summary-article-mark article)))
9860     ;; Allow the backend to change the mark.
9861     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9862     (if (eq mark old-mark)
9863         t
9864       (if (<= article 0)
9865           (progn
9866             (gnus-error 1 "Can't mark negative article numbers")
9867             nil)
9868         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9869         (setq gnus-newsgroup-spam-marked
9870               (delq article gnus-newsgroup-spam-marked))
9871         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9872         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9873         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9874         (cond ((= mark gnus-ticked-mark)
9875                (setq gnus-newsgroup-marked
9876                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9877                                               article)))
9878               ((= mark gnus-spam-mark)
9879                (setq gnus-newsgroup-spam-marked
9880                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9881                                               article)))
9882               ((= mark gnus-dormant-mark)
9883                (setq gnus-newsgroup-dormant
9884                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9885                                               article)))
9886               (t
9887                (setq gnus-newsgroup-unreads
9888                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9889                                               article))))
9890         (gnus-pull article gnus-newsgroup-reads)
9891
9892         ;; See whether the article is to be put in the cache.
9893         (and gnus-use-cache
9894              (vectorp (gnus-summary-article-header article))
9895              (save-excursion
9896                (gnus-cache-possibly-enter-article
9897                 gnus-newsgroup-name article
9898                 (gnus-summary-article-header article)
9899                 (= mark gnus-ticked-mark)
9900                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9901
9902         ;; Fix the mark.
9903         (gnus-summary-update-mark mark 'unread)
9904         t))))
9905
9906 (defun gnus-summary-mark-article (&optional article mark no-expire)
9907   "Mark ARTICLE with MARK.  MARK can be any character.
9908 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9909 `??' (dormant) and `?E' (expirable).
9910 If MARK is nil, then the default character `?r' is used.
9911 If ARTICLE is nil, then the article on the current line will be
9912 marked.
9913 If NO-EXPIRE, auto-expiry will be inhibited."
9914   ;; The mark might be a string.
9915   (when (stringp mark)
9916     (setq mark (aref mark 0)))
9917   ;; If no mark is given, then we check auto-expiring.
9918   (when (null mark)
9919     (setq mark gnus-del-mark))
9920   (when (and (not no-expire)
9921              gnus-newsgroup-auto-expire
9922              (memq mark gnus-auto-expirable-marks))
9923     (setq mark gnus-expirable-mark))
9924   (let ((article (or article (gnus-summary-article-number)))
9925         (old-mark (gnus-summary-article-mark article)))
9926     ;; Allow the backend to change the mark.
9927     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9928     (if (eq mark old-mark)
9929         t
9930       (unless article
9931         (error "No article on current line"))
9932       (if (not (if (or (= mark gnus-unread-mark)
9933                        (= mark gnus-ticked-mark)
9934                        (= mark gnus-spam-mark)
9935                        (= mark gnus-dormant-mark))
9936                    (gnus-mark-article-as-unread article mark)
9937                  (gnus-mark-article-as-read article mark)))
9938           t
9939         ;; See whether the article is to be put in the cache.
9940         (and gnus-use-cache
9941              (not (= mark gnus-canceled-mark))
9942              (vectorp (gnus-summary-article-header article))
9943              (save-excursion
9944                (gnus-cache-possibly-enter-article
9945                 gnus-newsgroup-name article
9946                 (gnus-summary-article-header article)
9947                 (= mark gnus-ticked-mark)
9948                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9949
9950         (when (gnus-summary-goto-subject article nil t)
9951           (let ((buffer-read-only nil))
9952             (gnus-summary-show-thread)
9953             ;; Fix the mark.
9954             (gnus-summary-update-mark mark 'unread)
9955             t))))))
9956
9957 (defun gnus-summary-update-secondary-mark (article)
9958   "Update the secondary (read, process, cache) mark."
9959   (gnus-summary-update-mark
9960    (cond ((memq article gnus-newsgroup-processable)
9961           gnus-process-mark)
9962          ((memq article gnus-newsgroup-cached)
9963           gnus-cached-mark)
9964          ((memq article gnus-newsgroup-replied)
9965           gnus-replied-mark)
9966          ((memq article gnus-newsgroup-forwarded)
9967           gnus-forwarded-mark)
9968          ((memq article gnus-newsgroup-saved)
9969           gnus-saved-mark)
9970          ((memq article gnus-newsgroup-recent)
9971           gnus-recent-mark)
9972          ((memq article gnus-newsgroup-unseen)
9973           gnus-unseen-mark)
9974          (t gnus-no-mark))
9975    'replied)
9976   (when (gnus-visual-p 'summary-highlight 'highlight)
9977     (gnus-run-hooks 'gnus-summary-update-hook))
9978   t)
9979
9980 (defun gnus-summary-update-download-mark (article)
9981   "Update the download mark."
9982   (gnus-summary-update-mark
9983    (cond ((memq article gnus-newsgroup-undownloaded)
9984           gnus-undownloaded-mark)
9985          (gnus-newsgroup-agentized
9986           gnus-downloaded-mark)
9987          (t
9988           gnus-no-mark))
9989    'download)
9990   (gnus-summary-update-line t)
9991   t)
9992
9993 (defun gnus-summary-update-mark (mark type)
9994   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9995         (buffer-read-only nil))
9996     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9997     (when forward
9998       (when (looking-at "\r")
9999         (incf forward))
10000       (when (<= (+ forward (point)) (point-max))
10001         ;; Go to the right position on the line.
10002         (goto-char (+ forward (point)))
10003         ;; Replace the old mark with the new mark.
10004         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10005         ;; Optionally update the marks by some user rule.
10006         (when (eq type 'unread)
10007           (gnus-data-set-mark
10008            (gnus-data-find (gnus-summary-article-number)) mark)
10009           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10010
10011 (defun gnus-mark-article-as-read (article &optional mark)
10012   "Enter ARTICLE in the pertinent lists and remove it from others."
10013   ;; Make the article expirable.
10014   (let ((mark (or mark gnus-del-mark)))
10015     (setq gnus-newsgroup-expirable
10016           (if (= mark gnus-expirable-mark)
10017               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10018             (delq article gnus-newsgroup-expirable)))
10019     ;; Remove from unread and marked lists.
10020     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10021     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10022     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10023     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10024     (push (cons article mark) gnus-newsgroup-reads)
10025     ;; Possibly remove from cache, if that is used.
10026     (when gnus-use-cache
10027       (gnus-cache-enter-remove-article article))
10028     t))
10029
10030 (defun gnus-mark-article-as-unread (article &optional mark)
10031   "Enter ARTICLE in the pertinent lists and remove it from others."
10032   (let ((mark (or mark gnus-ticked-mark)))
10033     (if (<= article 0)
10034         (progn
10035           (gnus-error 1 "Can't mark negative article numbers")
10036           nil)
10037       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10038             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10039             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10040             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10041             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10042
10043       ;; Unsuppress duplicates?
10044       (when gnus-suppress-duplicates
10045         (gnus-dup-unsuppress-article article))
10046
10047       (cond ((= mark gnus-ticked-mark)
10048              (setq gnus-newsgroup-marked
10049                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10050             ((= mark gnus-spam-mark)
10051              (setq gnus-newsgroup-spam-marked
10052                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10053                                             article)))
10054             ((= mark gnus-dormant-mark)
10055              (setq gnus-newsgroup-dormant
10056                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10057             (t
10058              (setq gnus-newsgroup-unreads
10059                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10060       (gnus-pull article gnus-newsgroup-reads)
10061       t)))
10062
10063 (defalias 'gnus-summary-mark-as-unread-forward
10064   'gnus-summary-tick-article-forward)
10065 (make-obsolete 'gnus-summary-mark-as-unread-forward
10066                'gnus-summary-tick-article-forward)
10067 (defun gnus-summary-tick-article-forward (n)
10068   "Tick N articles forwards.
10069 If N is negative, tick backwards instead.
10070 The difference between N and the number of articles ticked is returned."
10071   (interactive "p")
10072   (gnus-summary-mark-forward n gnus-ticked-mark))
10073
10074 (defalias 'gnus-summary-mark-as-unread-backward
10075   'gnus-summary-tick-article-backward)
10076 (make-obsolete 'gnus-summary-mark-as-unread-backward
10077                'gnus-summary-tick-article-backward)
10078 (defun gnus-summary-tick-article-backward (n)
10079   "Tick N articles backwards.
10080 The difference between N and the number of articles ticked is returned."
10081   (interactive "p")
10082   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10083
10084 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10085 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10086 (defun gnus-summary-tick-article (&optional article clear-mark)
10087   "Mark current article as unread.
10088 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10089 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10090   (interactive)
10091   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10092                                        gnus-ticked-mark)))
10093
10094 (defun gnus-summary-mark-as-read-forward (n)
10095   "Mark N articles as read forwards.
10096 If N is negative, mark backwards instead.
10097 The difference between N and the actual number of articles marked is
10098 returned."
10099   (interactive "p")
10100   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10101
10102 (defun gnus-summary-mark-as-read-backward (n)
10103   "Mark the N articles as read backwards.
10104 The difference between N and the actual number of articles marked is
10105 returned."
10106   (interactive "p")
10107   (gnus-summary-mark-forward
10108    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10109
10110 (defun gnus-summary-mark-as-read (&optional article mark)
10111   "Mark current article as read.
10112 ARTICLE specifies the article to be marked as read.
10113 MARK specifies a string to be inserted at the beginning of the line."
10114   (gnus-summary-mark-article article mark))
10115
10116 (defun gnus-summary-clear-mark-forward (n)
10117   "Clear marks from N articles forward.
10118 If N is negative, clear backward instead.
10119 The difference between N and the number of marks cleared is returned."
10120   (interactive "p")
10121   (gnus-summary-mark-forward n gnus-unread-mark))
10122
10123 (defun gnus-summary-clear-mark-backward (n)
10124   "Clear marks from N articles backward.
10125 The difference between N and the number of marks cleared is returned."
10126   (interactive "p")
10127   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10128
10129 (defun gnus-summary-mark-unread-as-read ()
10130   "Intended to be used by `gnus-summary-mark-article-hook'."
10131   (when (memq gnus-current-article gnus-newsgroup-unreads)
10132     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10133
10134 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10135   "Intended to be used by `gnus-summary-mark-article-hook'."
10136   (let ((mark (gnus-summary-article-mark)))
10137     (when (or (gnus-unread-mark-p mark)
10138               (gnus-read-mark-p mark))
10139       (gnus-summary-mark-article gnus-current-article
10140                                  (or new-mark gnus-read-mark)))))
10141
10142 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10143   "Intended to be used by `gnus-summary-mark-article-hook'."
10144   (let ((mark (gnus-summary-article-mark)))
10145     (when (or (gnus-unread-mark-p mark)
10146               (gnus-read-mark-p mark))
10147       (gnus-summary-mark-article (gnus-summary-article-number)
10148                                  (or new-mark gnus-read-mark)))))
10149
10150 (defun gnus-summary-mark-unread-as-ticked ()
10151   "Intended to be used by `gnus-summary-mark-article-hook'."
10152   (when (memq gnus-current-article gnus-newsgroup-unreads)
10153     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10154
10155 (defun gnus-summary-mark-region-as-read (point mark all)
10156   "Mark all unread articles between point and mark as read.
10157 If given a prefix, mark all articles between point and mark as read,
10158 even ticked and dormant ones."
10159   (interactive "r\nP")
10160   (save-excursion
10161     (let (article)
10162       (goto-char point)
10163       (beginning-of-line)
10164       (while (and
10165               (< (point) mark)
10166               (progn
10167                 (when (or all
10168                           (memq (setq article (gnus-summary-article-number))
10169                                 gnus-newsgroup-unreads))
10170                   (gnus-summary-mark-article article gnus-del-mark))
10171                 t)
10172               (gnus-summary-find-next))))))
10173
10174 (defun gnus-summary-mark-below (score mark)
10175   "Mark articles with score less than SCORE with MARK."
10176   (interactive "P\ncMark: ")
10177   (setq score (if score
10178                   (prefix-numeric-value score)
10179                 (or gnus-summary-default-score 0)))
10180   (save-excursion
10181     (set-buffer gnus-summary-buffer)
10182     (goto-char (point-min))
10183     (while
10184         (progn
10185           (and (< (gnus-summary-article-score) score)
10186                (gnus-summary-mark-article nil mark))
10187           (gnus-summary-find-next)))))
10188
10189 (defun gnus-summary-kill-below (&optional score)
10190   "Mark articles with score below SCORE as read."
10191   (interactive "P")
10192   (gnus-summary-mark-below score gnus-killed-mark))
10193
10194 (defun gnus-summary-clear-above (&optional score)
10195   "Clear all marks from articles with score above SCORE."
10196   (interactive "P")
10197   (gnus-summary-mark-above score gnus-unread-mark))
10198
10199 (defun gnus-summary-tick-above (&optional score)
10200   "Tick all articles with score above SCORE."
10201   (interactive "P")
10202   (gnus-summary-mark-above score gnus-ticked-mark))
10203
10204 (defun gnus-summary-mark-above (score mark)
10205   "Mark articles with score over SCORE with MARK."
10206   (interactive "P\ncMark: ")
10207   (setq score (if score
10208                   (prefix-numeric-value score)
10209                 (or gnus-summary-default-score 0)))
10210   (save-excursion
10211     (set-buffer gnus-summary-buffer)
10212     (goto-char (point-min))
10213     (while (and (progn
10214                   (when (> (gnus-summary-article-score) score)
10215                     (gnus-summary-mark-article nil mark))
10216                   t)
10217                 (gnus-summary-find-next)))))
10218
10219 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10220 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10221 (defun gnus-summary-limit-include-expunged (&optional no-error)
10222   "Display all the hidden articles that were expunged for low scores."
10223   (interactive)
10224   (let ((buffer-read-only nil))
10225     (let ((scored gnus-newsgroup-scored)
10226           headers h)
10227       (while scored
10228         (unless (gnus-summary-article-header (caar scored))
10229           (and (setq h (gnus-number-to-header (caar scored)))
10230                (< (cdar scored) gnus-summary-expunge-below)
10231                (push h headers)))
10232         (setq scored (cdr scored)))
10233       (if (not headers)
10234           (when (not no-error)
10235             (error "No expunged articles hidden"))
10236         (goto-char (point-min))
10237         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10238         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10239         (mapcar (lambda (x) (push (mail-header-number x)
10240                                   gnus-newsgroup-limit))
10241                 headers)
10242         (gnus-summary-prepare-unthreaded (nreverse headers))
10243         (goto-char (point-min))
10244         (gnus-summary-position-point)
10245         t))))
10246
10247 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10248   "Mark all unread articles in this newsgroup as read.
10249 If prefix argument ALL is non-nil, ticked and dormant articles will
10250 also be marked as read.
10251 If QUIETLY is non-nil, no questions will be asked.
10252
10253 If TO-HERE is non-nil, it should be a point in the buffer.  All
10254 articles before (after, if REVERSE is set) this point will be marked
10255 as read.
10256
10257 Note that this function will only catch up the unread article
10258 in the current summary buffer limitation.
10259
10260 The number of articles marked as read is returned."
10261   (interactive "P")
10262   (prog1
10263       (save-excursion
10264         (when (or quietly
10265                   (not gnus-interactive-catchup) ;Without confirmation?
10266                   gnus-expert-user
10267                   (gnus-y-or-n-p
10268                    (if all
10269                        "Mark absolutely all articles as read? "
10270                      "Mark all unread articles as read? ")))
10271           (if (and not-mark
10272                    (not gnus-newsgroup-adaptive)
10273                    (not gnus-newsgroup-auto-expire)
10274                    (not gnus-suppress-duplicates)
10275                    (or (not gnus-use-cache)
10276                        (eq gnus-use-cache 'passive)))
10277               (progn
10278                 (when all
10279                   (setq gnus-newsgroup-marked nil
10280                         gnus-newsgroup-spam-marked nil
10281                         gnus-newsgroup-dormant nil))
10282                 (setq gnus-newsgroup-unreads
10283                       (gnus-sorted-nunion
10284                        (gnus-intersection gnus-newsgroup-unreads
10285                                           gnus-newsgroup-downloadable)
10286                        gnus-newsgroup-unfetched)))
10287             ;; We actually mark all articles as canceled, which we
10288             ;; have to do when using auto-expiry or adaptive scoring.
10289             (gnus-summary-show-all-threads)
10290             (if (and to-here reverse)
10291                 (progn
10292                   (goto-char to-here)
10293                   (gnus-summary-mark-current-read-and-unread-as-read
10294                    gnus-catchup-mark)
10295                   (while (gnus-summary-find-next (not all))
10296                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10297               (when (gnus-summary-first-subject (not all))
10298                 (while (and
10299                         (if to-here (< (point) to-here) t)
10300                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10301                         (gnus-summary-find-next (not all))))))
10302             (gnus-set-mode-line 'summary))
10303           t))
10304     (gnus-summary-position-point)))
10305
10306 (defun gnus-summary-catchup-to-here (&optional all)
10307   "Mark all unticked articles before the current one as read.
10308 If ALL is non-nil, also mark ticked and dormant articles as read."
10309   (interactive "P")
10310   (save-excursion
10311     (gnus-save-hidden-threads
10312       (let ((beg (point)))
10313         ;; We check that there are unread articles.
10314         (when (or all (gnus-summary-find-prev))
10315           (gnus-summary-catchup all t beg)))))
10316   (gnus-summary-position-point))
10317
10318 (defun gnus-summary-catchup-from-here (&optional all)
10319   "Mark all unticked articles after (and including) the current one as read.
10320 If ALL is non-nil, also mark ticked and dormant articles as read."
10321   (interactive "P")
10322   (save-excursion
10323     (gnus-save-hidden-threads
10324       (let ((beg (point)))
10325         ;; We check that there are unread articles.
10326         (when (or all (gnus-summary-find-next))
10327           (gnus-summary-catchup all t beg nil t)))))
10328   (gnus-summary-position-point))
10329
10330 (defun gnus-summary-catchup-all (&optional quietly)
10331   "Mark all articles in this newsgroup as read.
10332 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10333 instead, which marks only unread articles as read."
10334   (interactive "P")
10335   (gnus-summary-catchup t quietly))
10336
10337 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10338   "Mark all unread articles in this group as read, then exit.
10339 If prefix argument ALL is non-nil, all articles are marked as read.
10340 If QUIETLY is non-nil, no questions will be asked."
10341   (interactive "P")
10342   (when (gnus-summary-catchup all quietly nil 'fast)
10343     ;; Select next newsgroup or exit.
10344     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10345              (eq gnus-auto-select-next 'quietly))
10346         (gnus-summary-next-group nil)
10347       (gnus-summary-exit))))
10348
10349 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10350   "Mark all articles in this newsgroup as read, and then exit.
10351 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10352 instead, which marks only unread articles as read."
10353   (interactive "P")
10354   (gnus-summary-catchup-and-exit t quietly))
10355
10356 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10357   "Mark all articles in this group as read and select the next group.
10358 If given a prefix, mark all articles, unread as well as ticked, as
10359 read."
10360   (interactive "P")
10361   (save-excursion
10362     (gnus-summary-catchup all))
10363   (gnus-summary-next-group))
10364
10365 ;;;
10366 ;;; with article
10367 ;;;
10368
10369 (defmacro gnus-with-article (article &rest forms)
10370   "Select ARTICLE and perform FORMS in the original article buffer.
10371 Then replace the article with the result."
10372   `(progn
10373      ;; We don't want the article to be marked as read.
10374      (let (gnus-mark-article-hook)
10375        (gnus-summary-select-article t t nil ,article))
10376      (set-buffer gnus-original-article-buffer)
10377      ,@forms
10378      (if (not (gnus-check-backend-function
10379                'request-replace-article (car gnus-article-current)))
10380          (gnus-message 5 "Read-only group; not replacing")
10381        (unless (gnus-request-replace-article
10382                 ,article (car gnus-article-current)
10383                 (current-buffer) t)
10384          (error "Couldn't replace article")))
10385      ;; The cache and backlog have to be flushed somewhat.
10386      (when gnus-keep-backlog
10387        (gnus-backlog-remove-article
10388         (car gnus-article-current) (cdr gnus-article-current)))
10389      (when gnus-use-cache
10390        (gnus-cache-update-article
10391         (car gnus-article-current) (cdr gnus-article-current)))))
10392
10393 (put 'gnus-with-article 'lisp-indent-function 1)
10394 (put 'gnus-with-article 'edebug-form-spec '(form body))
10395
10396 ;; Thread-based commands.
10397
10398 (defun gnus-summary-articles-in-thread (&optional article)
10399   "Return a list of all articles in the current thread.
10400 If ARTICLE is non-nil, return all articles in the thread that starts
10401 with that article."
10402   (let* ((article (or article (gnus-summary-article-number)))
10403          (data (gnus-data-find-list article))
10404          (top-level (gnus-data-level (car data)))
10405          (top-subject
10406           (cond ((null gnus-thread-operation-ignore-subject)
10407                  (gnus-simplify-subject-re
10408                   (mail-header-subject (gnus-data-header (car data)))))
10409                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10410                  (gnus-simplify-subject-fuzzy
10411                   (mail-header-subject (gnus-data-header (car data)))))
10412                 (t nil)))
10413          (end-point (save-excursion
10414                       (if (gnus-summary-go-to-next-thread)
10415                           (point) (point-max))))
10416          articles)
10417     (while (and data
10418                 (< (gnus-data-pos (car data)) end-point))
10419       (when (or (not top-subject)
10420                 (string= top-subject
10421                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10422                              (gnus-simplify-subject-fuzzy
10423                               (mail-header-subject
10424                                (gnus-data-header (car data))))
10425                            (gnus-simplify-subject-re
10426                             (mail-header-subject
10427                              (gnus-data-header (car data)))))))
10428         (push (gnus-data-number (car data)) articles))
10429       (unless (and (setq data (cdr data))
10430                    (> (gnus-data-level (car data)) top-level))
10431         (setq data nil)))
10432     ;; Return the list of articles.
10433     (nreverse articles)))
10434
10435 (defun gnus-summary-rethread-current ()
10436   "Rethread the thread the current article is part of."
10437   (interactive)
10438   (let* ((gnus-show-threads t)
10439          (article (gnus-summary-article-number))
10440          (id (mail-header-id (gnus-summary-article-header)))
10441          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10442     (unless id
10443       (error "No article on the current line"))
10444     (gnus-rebuild-thread id)
10445     (gnus-summary-goto-subject article)))
10446
10447 (defun gnus-summary-reparent-thread ()
10448   "Make the current article child of the marked (or previous) article.
10449
10450 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10451 is non-nil or the Subject: of both articles are the same."
10452   (interactive)
10453   (unless (not (gnus-group-read-only-p))
10454     (error "The current newsgroup does not support article editing"))
10455   (unless (<= (length gnus-newsgroup-processable) 1)
10456     (error "No more than one article may be marked"))
10457   (save-window-excursion
10458     (let ((gnus-article-buffer " *reparent*")
10459           (current-article (gnus-summary-article-number))
10460           ;; First grab the marked article, otherwise one line up.
10461           (parent-article (if (not (null gnus-newsgroup-processable))
10462                               (car gnus-newsgroup-processable)
10463                             (save-excursion
10464                               (if (eq (forward-line -1) 0)
10465                                   (gnus-summary-article-number)
10466                                 (error "Beginning of summary buffer"))))))
10467       (unless (not (eq current-article parent-article))
10468         (error "An article may not be self-referential"))
10469       (let ((message-id (mail-header-id
10470                          (gnus-summary-article-header parent-article))))
10471         (unless (and message-id (not (equal message-id "")))
10472           (error "No message-id in desired parent"))
10473         (gnus-with-article current-article
10474           (save-restriction
10475             (goto-char (point-min))
10476             (message-narrow-to-head)
10477             (if (re-search-forward "^References: " nil t)
10478                 (progn
10479                   (re-search-forward "^[^ \t]" nil t)
10480                   (forward-line -1)
10481                   (end-of-line)
10482                   (insert " " message-id))
10483               (insert "References: " message-id "\n"))))
10484         (set-buffer gnus-summary-buffer)
10485         (gnus-summary-unmark-all-processable)
10486         (gnus-summary-update-article current-article)
10487         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10488             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10489         (gnus-summary-rethread-current)
10490         (gnus-message 3 "Article %d is now the child of article %d"
10491                       current-article parent-article)))))
10492
10493 (defun gnus-summary-toggle-threads (&optional arg)
10494   "Toggle showing conversation threads.
10495 If ARG is positive number, turn showing conversation threads on."
10496   (interactive "P")
10497   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10498     (setq gnus-show-threads
10499           (if (null arg) (not gnus-show-threads)
10500             (> (prefix-numeric-value arg) 0)))
10501     (gnus-summary-prepare)
10502     (gnus-summary-goto-subject current)
10503     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10504     (gnus-summary-position-point)))
10505
10506 (defun gnus-summary-show-all-threads ()
10507   "Show all threads."
10508   (interactive)
10509   (save-excursion
10510     (let ((buffer-read-only nil))
10511       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10512   (gnus-summary-position-point))
10513
10514 (defun gnus-summary-show-thread ()
10515   "Show thread subtrees.
10516 Returns nil if no thread was there to be shown."
10517   (interactive)
10518   (let ((buffer-read-only nil)
10519         (orig (point))
10520         (end (gnus-point-at-eol))
10521         ;; Leave point at bol
10522         (beg (progn (beginning-of-line) (point))))
10523     (prog1
10524         ;; Any hidden lines here?
10525         (search-forward "\r" end t)
10526       (subst-char-in-region beg end ?\^M ?\n t)
10527       (goto-char orig)
10528       (gnus-summary-position-point))))
10529
10530 (defun gnus-summary-maybe-hide-threads ()
10531   "If requested, hide the threads that should be hidden."
10532   (when (and gnus-show-threads
10533              gnus-thread-hide-subtree)
10534     (gnus-summary-hide-all-threads
10535      (if (or (consp gnus-thread-hide-subtree)
10536              (functionp gnus-thread-hide-subtree))
10537          (gnus-make-predicate gnus-thread-hide-subtree)
10538        nil))))
10539
10540 ;;; Hiding predicates.
10541
10542 (defun gnus-article-unread-p (header)
10543   (memq (mail-header-number header) gnus-newsgroup-unreads))
10544
10545 (defun gnus-article-unseen-p (header)
10546   (memq (mail-header-number header) gnus-newsgroup-unseen))
10547
10548 (defun gnus-map-articles (predicate articles)
10549   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10550   (apply 'gnus-or (mapcar predicate
10551                           (mapcar 'gnus-summary-article-header articles))))
10552
10553 (defun gnus-summary-hide-all-threads (&optional predicate)
10554   "Hide all thread subtrees.
10555 If PREDICATE is supplied, threads that satisfy this predicate
10556 will not be hidden."
10557   (interactive)
10558   (save-excursion
10559     (goto-char (point-min))
10560     (let ((end nil))
10561       (while (not end)
10562         (when (or (not predicate)
10563                   (gnus-map-articles
10564                    predicate (gnus-summary-article-children)))
10565             (gnus-summary-hide-thread))
10566         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10567   (gnus-summary-position-point))
10568
10569 (defun gnus-summary-hide-thread ()
10570   "Hide thread subtrees.
10571 If PREDICATE is supplied, threads that satisfy this predicate
10572 will not be hidden.
10573 Returns nil if no threads were there to be hidden."
10574   (interactive)
10575   (let ((buffer-read-only nil)
10576         (start (point))
10577         (article (gnus-summary-article-number)))
10578     (goto-char start)
10579     ;; Go forward until either the buffer ends or the subthread
10580     ;; ends.
10581     (when (and (not (eobp))
10582                (or (zerop (gnus-summary-next-thread 1 t))
10583                    (goto-char (point-max))))
10584       (prog1
10585           (if (and (> (point) start)
10586                    (search-backward "\n" start t))
10587               (progn
10588                 (subst-char-in-region start (point) ?\n ?\^M)
10589                 (gnus-summary-goto-subject article))
10590             (goto-char start)
10591             nil)))))
10592
10593 (defun gnus-summary-go-to-next-thread (&optional previous)
10594   "Go to the same level (or less) next thread.
10595 If PREVIOUS is non-nil, go to previous thread instead.
10596 Return the article number moved to, or nil if moving was impossible."
10597   (let ((level (gnus-summary-thread-level))
10598         (way (if previous -1 1))
10599         (beg (point)))
10600     (forward-line way)
10601     (while (and (not (eobp))
10602                 (< level (gnus-summary-thread-level)))
10603       (forward-line way))
10604     (if (eobp)
10605         (progn
10606           (goto-char beg)
10607           nil)
10608       (setq beg (point))
10609       (prog1
10610           (gnus-summary-article-number)
10611         (goto-char beg)))))
10612
10613 (defun gnus-summary-next-thread (n &optional silent)
10614   "Go to the same level next N'th thread.
10615 If N is negative, search backward instead.
10616 Returns the difference between N and the number of skips actually
10617 done.
10618
10619 If SILENT, don't output messages."
10620   (interactive "p")
10621   (let ((backward (< n 0))
10622         (n (abs n)))
10623     (while (and (> n 0)
10624                 (gnus-summary-go-to-next-thread backward))
10625       (decf n))
10626     (unless silent
10627       (gnus-summary-position-point))
10628     (when (and (not silent) (/= 0 n))
10629       (gnus-message 7 "No more threads"))
10630     n))
10631
10632 (defun gnus-summary-prev-thread (n)
10633   "Go to the same level previous N'th thread.
10634 Returns the difference between N and the number of skips actually
10635 done."
10636   (interactive "p")
10637   (gnus-summary-next-thread (- n)))
10638
10639 (defun gnus-summary-go-down-thread ()
10640   "Go down one level in the current thread."
10641   (let ((children (gnus-summary-article-children)))
10642     (when children
10643       (gnus-summary-goto-subject (car children)))))
10644
10645 (defun gnus-summary-go-up-thread ()
10646   "Go up one level in the current thread."
10647   (let ((parent (gnus-summary-article-parent)))
10648     (when parent
10649       (gnus-summary-goto-subject parent))))
10650
10651 (defun gnus-summary-down-thread (n)
10652   "Go down thread N steps.
10653 If N is negative, go up instead.
10654 Returns the difference between N and how many steps down that were
10655 taken."
10656   (interactive "p")
10657   (let ((up (< n 0))
10658         (n (abs n)))
10659     (while (and (> n 0)
10660                 (if up (gnus-summary-go-up-thread)
10661                   (gnus-summary-go-down-thread)))
10662       (setq n (1- n)))
10663     (gnus-summary-position-point)
10664     (when (/= 0 n)
10665       (gnus-message 7 "Can't go further"))
10666     n))
10667
10668 (defun gnus-summary-up-thread (n)
10669   "Go up thread N steps.
10670 If N is negative, go down instead.
10671 Returns the difference between N and how many steps down that were
10672 taken."
10673   (interactive "p")
10674   (gnus-summary-down-thread (- n)))
10675
10676 (defun gnus-summary-top-thread ()
10677   "Go to the top of the thread."
10678   (interactive)
10679   (while (gnus-summary-go-up-thread))
10680   (gnus-summary-article-number))
10681
10682 (defun gnus-summary-kill-thread (&optional unmark)
10683   "Mark articles under current thread as read.
10684 If the prefix argument is positive, remove any kinds of marks.
10685 If the prefix argument is negative, tick articles instead."
10686   (interactive "P")
10687   (when unmark
10688     (setq unmark (prefix-numeric-value unmark)))
10689   (let ((articles (gnus-summary-articles-in-thread)))
10690     (save-excursion
10691       ;; Expand the thread.
10692       (gnus-summary-show-thread)
10693       ;; Mark all the articles.
10694       (while articles
10695         (gnus-summary-goto-subject (car articles))
10696         (cond ((null unmark)
10697                (gnus-summary-mark-article-as-read gnus-killed-mark))
10698               ((> unmark 0)
10699                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10700               (t
10701                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10702         (setq articles (cdr articles))))
10703     ;; Hide killed subtrees.
10704     (and (null unmark)
10705          gnus-thread-hide-killed
10706          (gnus-summary-hide-thread))
10707     ;; If marked as read, go to next unread subject.
10708     (when (null unmark)
10709       ;; Go to next unread subject.
10710       (gnus-summary-next-subject 1 t)))
10711   (gnus-set-mode-line 'summary))
10712
10713 ;; Summary sorting commands
10714
10715 (defun gnus-summary-sort-by-number (&optional reverse)
10716   "Sort the summary buffer by article number.
10717 Argument REVERSE means reverse order."
10718   (interactive "P")
10719   (gnus-summary-sort 'number reverse))
10720
10721 (defun gnus-summary-sort-by-random (&optional reverse)
10722   "Randomize the order in the summary buffer.
10723 Argument REVERSE means to randomize in reverse order."
10724   (interactive "P")
10725   (gnus-summary-sort 'random reverse))
10726
10727 (defun gnus-summary-sort-by-author (&optional reverse)
10728   "Sort the summary buffer by author name alphabetically.
10729 If `case-fold-search' is non-nil, case of letters is ignored.
10730 Argument REVERSE means reverse order."
10731   (interactive "P")
10732   (gnus-summary-sort 'author reverse))
10733
10734 (defun gnus-summary-sort-by-subject (&optional reverse)
10735   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10736 If `case-fold-search' is non-nil, case of letters is ignored.
10737 Argument REVERSE means reverse order."
10738   (interactive "P")
10739   (gnus-summary-sort 'subject reverse))
10740
10741 (defun gnus-summary-sort-by-date (&optional reverse)
10742   "Sort the summary buffer by date.
10743 Argument REVERSE means reverse order."
10744   (interactive "P")
10745   (gnus-summary-sort 'date reverse))
10746
10747 (defun gnus-summary-sort-by-score (&optional reverse)
10748   "Sort the summary buffer by score.
10749 Argument REVERSE means reverse order."
10750   (interactive "P")
10751   (gnus-summary-sort 'score reverse))
10752
10753 (defun gnus-summary-sort-by-lines (&optional reverse)
10754   "Sort the summary buffer by the number of lines.
10755 Argument REVERSE means reverse order."
10756   (interactive "P")
10757   (gnus-summary-sort 'lines reverse))
10758
10759 (defun gnus-summary-sort-by-chars (&optional reverse)
10760   "Sort the summary buffer by article length.
10761 Argument REVERSE means reverse order."
10762   (interactive "P")
10763   (gnus-summary-sort 'chars reverse))
10764
10765 (defun gnus-summary-sort-by-original (&optional reverse)
10766   "Sort the summary buffer using the default sorting method.
10767 Argument REVERSE means reverse order."
10768   (interactive "P")
10769   (let* ((buffer-read-only)
10770          (gnus-summary-prepare-hook nil))
10771     ;; We do the sorting by regenerating the threads.
10772     (gnus-summary-prepare)
10773     ;; Hide subthreads if needed.
10774     (gnus-summary-maybe-hide-threads)))
10775
10776 (defun gnus-summary-sort (predicate reverse)
10777   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10778   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10779          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10780          (gnus-thread-sort-functions
10781           (if (not reverse)
10782               thread
10783             `(lambda (t1 t2)
10784                (,thread t2 t1))))
10785          (gnus-sort-gathered-threads-function
10786           gnus-thread-sort-functions)
10787          (gnus-article-sort-functions
10788           (if (not reverse)
10789               article
10790             `(lambda (t1 t2)
10791                (,article t2 t1))))
10792          (buffer-read-only)
10793          (gnus-summary-prepare-hook nil))
10794     ;; We do the sorting by regenerating the threads.
10795     (gnus-summary-prepare)
10796     ;; Hide subthreads if needed.
10797     (gnus-summary-maybe-hide-threads)))
10798
10799 ;; Summary saving commands.
10800
10801 (defun gnus-summary-save-article (&optional n not-saved)
10802   "Save the current article using the default saver function.
10803 If N is a positive number, save the N next articles.
10804 If N is a negative number, save the N previous articles.
10805 If N is nil and any articles have been marked with the process mark,
10806 save those articles instead.
10807 The variable `gnus-default-article-saver' specifies the saver function."
10808   (interactive "P")
10809   (let* ((articles (gnus-summary-work-articles n))
10810          (save-buffer (save-excursion
10811                         (nnheader-set-temp-buffer " *Gnus Save*")))
10812          (num (length articles))
10813          header file)
10814     (dolist (article articles)
10815       (setq header (gnus-summary-article-header article))
10816       (if (not (vectorp header))
10817           ;; This is a pseudo-article.
10818           (if (assq 'name header)
10819               (gnus-copy-file (cdr (assq 'name header)))
10820             (gnus-message 1 "Article %d is unsaveable" article))
10821         ;; This is a real article.
10822         (save-window-excursion
10823           (let ((gnus-display-mime-function nil)
10824                 (gnus-article-prepare-hook nil))
10825             (gnus-summary-select-article t nil nil article)))
10826         (save-excursion
10827           (set-buffer save-buffer)
10828           (erase-buffer)
10829           (insert-buffer-substring gnus-original-article-buffer))
10830         (setq file (gnus-article-save save-buffer file num))
10831         (gnus-summary-remove-process-mark article)
10832         (unless not-saved
10833           (gnus-summary-set-saved-mark article))))
10834     (gnus-kill-buffer save-buffer)
10835     (gnus-summary-position-point)
10836     (gnus-set-mode-line 'summary)
10837     n))
10838
10839 (defun gnus-summary-pipe-output (&optional arg headers)
10840   "Pipe the current article to a subprocess.
10841 If N is a positive number, pipe the N next articles.
10842 If N is a negative number, pipe the N previous articles.
10843 If N is nil and any articles have been marked with the process mark,
10844 pipe those articles instead.
10845 If HEADERS (the symbolic prefix), include the headers, too."
10846   (interactive (gnus-interactive "P\ny"))
10847   (require 'gnus-art)
10848   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10849         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10850     (gnus-summary-save-article arg t))
10851   (let ((buffer (get-buffer "*Shell Command Output*")))
10852     (when (and buffer
10853                (not (zerop (buffer-size buffer))))
10854       (gnus-configure-windows 'pipe))))
10855
10856 (defun gnus-summary-save-article-mail (&optional arg)
10857   "Append the current article to an mail file.
10858 If N is a positive number, save the N next articles.
10859 If N is a negative number, save the N previous articles.
10860 If N is nil and any articles have been marked with the process mark,
10861 save those articles instead."
10862   (interactive "P")
10863   (require 'gnus-art)
10864   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10865     (gnus-summary-save-article arg)))
10866
10867 (defun gnus-summary-save-article-rmail (&optional arg)
10868   "Append the current article to an rmail file.
10869 If N is a positive number, save the N next articles.
10870 If N is a negative number, save the N previous articles.
10871 If N is nil and any articles have been marked with the process mark,
10872 save those articles instead."
10873   (interactive "P")
10874   (require 'gnus-art)
10875   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10876     (gnus-summary-save-article arg)))
10877
10878 (defun gnus-summary-save-article-file (&optional arg)
10879   "Append the current article to a file.
10880 If N is a positive number, save the N next articles.
10881 If N is a negative number, save the N previous articles.
10882 If N is nil and any articles have been marked with the process mark,
10883 save those articles instead."
10884   (interactive "P")
10885   (require 'gnus-art)
10886   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10887     (gnus-summary-save-article arg)))
10888
10889 (defun gnus-summary-write-article-file (&optional arg)
10890   "Write the current article to a file, deleting the previous file.
10891 If N is a positive number, save the N next articles.
10892 If N is a negative number, save the N previous articles.
10893 If N is nil and any articles have been marked with the process mark,
10894 save those articles instead."
10895   (interactive "P")
10896   (require 'gnus-art)
10897   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10898     (gnus-summary-save-article arg)))
10899
10900 (defun gnus-summary-save-article-body-file (&optional arg)
10901   "Append the current article body to a file.
10902 If N is a positive number, save the N next articles.
10903 If N is a negative number, save the N previous articles.
10904 If N is nil and any articles have been marked with the process mark,
10905 save those articles instead."
10906   (interactive "P")
10907   (require 'gnus-art)
10908   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10909     (gnus-summary-save-article arg)))
10910
10911 (defun gnus-summary-muttprint (&optional arg)
10912   "Print the current article using Muttprint.
10913 If N is a positive number, save the N next articles.
10914 If N is a negative number, save the N previous articles.
10915 If N is nil and any articles have been marked with the process mark,
10916 save those articles instead."
10917   (interactive "P")
10918   (require 'gnus-art)
10919   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10920     (gnus-summary-save-article arg t)))
10921
10922 (defun gnus-summary-pipe-message (program)
10923   "Pipe the current article through PROGRAM."
10924   (interactive "sProgram: ")
10925   (gnus-summary-select-article)
10926   (let ((mail-header-separator ""))
10927     (gnus-eval-in-buffer-window gnus-article-buffer
10928       (save-restriction
10929         (widen)
10930         (let ((start (window-start))
10931               buffer-read-only)
10932           (message-pipe-buffer-body program)
10933           (set-window-start (get-buffer-window (current-buffer)) start))))))
10934
10935 (defun gnus-get-split-value (methods)
10936   "Return a value based on the split METHODS."
10937   (let (split-name method result match)
10938     (when methods
10939       (save-excursion
10940         (set-buffer gnus-original-article-buffer)
10941         (save-restriction
10942           (nnheader-narrow-to-headers)
10943           (while (and methods (not split-name))
10944             (goto-char (point-min))
10945             (setq method (pop methods))
10946             (setq match (car method))
10947             (when (cond
10948                    ((stringp match)
10949                     ;; Regular expression.
10950                     (ignore-errors
10951                       (re-search-forward match nil t)))
10952                    ((functionp match)
10953                     ;; Function.
10954                     (save-restriction
10955                       (widen)
10956                       (setq result (funcall match gnus-newsgroup-name))))
10957                    ((consp match)
10958                     ;; Form.
10959                     (save-restriction
10960                       (widen)
10961                       (setq result (eval match)))))
10962               (setq split-name (cdr method))
10963               (cond ((stringp result)
10964                      (push (expand-file-name
10965                             result gnus-article-save-directory)
10966                            split-name))
10967                     ((consp result)
10968                      (setq split-name (append result split-name)))))))))
10969     (nreverse split-name)))
10970
10971 (defun gnus-valid-move-group-p (group)
10972   (and (boundp group)
10973        (symbol-name group)
10974        (symbol-value group)
10975        (gnus-get-function (gnus-find-method-for-group
10976                            (symbol-name group)) 'request-accept-article t)))
10977
10978 (defun gnus-read-move-group-name (prompt default articles prefix)
10979   "Read a group name."
10980   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10981          (minibuffer-confirm-incomplete nil) ; XEmacs
10982          (prom
10983           (format "%s %s to:"
10984                   prompt
10985                   (if (> (length articles) 1)
10986                       (format "these %d articles" (length articles))
10987                     "this article")))
10988          (to-newsgroup
10989           (cond
10990            ((null split-name)
10991             (gnus-completing-read-with-default
10992              default prom
10993              gnus-active-hashtb
10994              'gnus-valid-move-group-p
10995              nil prefix
10996              'gnus-group-history))
10997            ((= 1 (length split-name))
10998             (gnus-completing-read-with-default
10999              (car split-name) prom
11000              gnus-active-hashtb
11001              'gnus-valid-move-group-p
11002              nil nil
11003              'gnus-group-history))
11004            (t
11005             (gnus-completing-read-with-default
11006              nil prom
11007              (mapcar (lambda (el) (list el))
11008                      (nreverse split-name))
11009              nil nil nil
11010              'gnus-group-history))))
11011          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11012     (when to-newsgroup
11013       (if (or (string= to-newsgroup "")
11014               (string= to-newsgroup prefix))
11015           (setq to-newsgroup default))
11016       (unless to-newsgroup
11017         (error "No group name entered"))
11018       (or (gnus-active to-newsgroup)
11019           (gnus-activate-group to-newsgroup nil nil to-method)
11020           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11021                                      to-newsgroup))
11022               (or (and (gnus-request-create-group to-newsgroup to-method)
11023                        (gnus-activate-group
11024                         to-newsgroup nil nil to-method)
11025                        (gnus-subscribe-group to-newsgroup))
11026                   (error "Couldn't create group %s" to-newsgroup)))
11027           (error "No such group: %s" to-newsgroup)))
11028     to-newsgroup))
11029
11030 (defun gnus-summary-save-parts (type dir n &optional reverse)
11031   "Save parts matching TYPE to DIR.
11032 If REVERSE, save parts that do not match TYPE."
11033   (interactive
11034    (list (read-string "Save parts of type: "
11035                       (or (car gnus-summary-save-parts-type-history)
11036                           gnus-summary-save-parts-default-mime)
11037                       'gnus-summary-save-parts-type-history)
11038          (setq gnus-summary-save-parts-last-directory
11039                (read-file-name "Save to directory: "
11040                                gnus-summary-save-parts-last-directory
11041                                nil t))
11042          current-prefix-arg))
11043   (gnus-summary-iterate n
11044     (let ((gnus-display-mime-function nil)
11045           (gnus-inhibit-treatment t))
11046       (gnus-summary-select-article))
11047     (save-excursion
11048       (set-buffer gnus-article-buffer)
11049       (let ((handles (or gnus-article-mime-handles
11050                          (mm-dissect-buffer nil gnus-article-loose-mime)
11051                          (and gnus-article-emulate-mime
11052                               (mm-uu-dissect)))))
11053         (when handles
11054           (gnus-summary-save-parts-1 type dir handles reverse)
11055           (unless gnus-article-mime-handles ;; Don't destroy this case.
11056             (mm-destroy-parts handles)))))))
11057
11058 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11059   (if (stringp (car handle))
11060       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11061               (cdr handle))
11062     (when (if reverse
11063               (not (string-match type (mm-handle-media-type handle)))
11064             (string-match type (mm-handle-media-type handle)))
11065       (let ((file (expand-file-name
11066                    (file-name-nondirectory
11067                     (or
11068                      (mail-content-type-get
11069                       (mm-handle-disposition handle) 'filename)
11070                      (concat gnus-newsgroup-name
11071                              "." (number-to-string
11072                                   (cdr gnus-article-current)))))
11073                    dir)))
11074         (unless (file-exists-p file)
11075           (mm-save-part-to-file handle file))))))
11076
11077 ;; Summary extract commands
11078
11079 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11080   (let ((buffer-read-only nil)
11081         (article (gnus-summary-article-number))
11082         after-article b e)
11083     (unless (gnus-summary-goto-subject article)
11084       (error "No such article: %d" article))
11085     (gnus-summary-position-point)
11086     ;; If all commands are to be bunched up on one line, we collect
11087     ;; them here.
11088     (unless gnus-view-pseudos-separately
11089       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11090             files action)
11091         (while ps
11092           (setq action (cdr (assq 'action (car ps))))
11093           (setq files (list (cdr (assq 'name (car ps)))))
11094           (while (and ps (cdr ps)
11095                       (string= (or action "1")
11096                                (or (cdr (assq 'action (cadr ps))) "2")))
11097             (push (cdr (assq 'name (cadr ps))) files)
11098             (setcdr ps (cddr ps)))
11099           (when files
11100             (when (not (string-match "%s" action))
11101               (push " " files))
11102             (push " " files)
11103             (when (assq 'execute (car ps))
11104               (setcdr (assq 'execute (car ps))
11105                       (funcall (if (string-match "%s" action)
11106                                    'format 'concat)
11107                                action
11108                                (mapconcat
11109                                 (lambda (f)
11110                                   (if (equal f " ")
11111                                       f
11112                                     (gnus-quote-arg-for-sh-or-csh f)))
11113                                 files " ")))))
11114           (setq ps (cdr ps)))))
11115     (if (and gnus-view-pseudos (not not-view))
11116         (while pslist
11117           (when (assq 'execute (car pslist))
11118             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11119                                   (eq gnus-view-pseudos 'not-confirm)))
11120           (setq pslist (cdr pslist)))
11121       (save-excursion
11122         (while pslist
11123           (setq after-article (or (cdr (assq 'article (car pslist)))
11124                                   (gnus-summary-article-number)))
11125           (gnus-summary-goto-subject after-article)
11126           (forward-line 1)
11127           (setq b (point))
11128           (insert "    " (file-name-nondirectory
11129                           (cdr (assq 'name (car pslist))))
11130                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11131           (setq e (point))
11132           (forward-line -1)             ; back to `b'
11133           (gnus-add-text-properties
11134            b (1- e) (list 'gnus-number gnus-reffed-article-number
11135                           gnus-mouse-face-prop gnus-mouse-face))
11136           (gnus-data-enter
11137            after-article gnus-reffed-article-number
11138            gnus-unread-mark b (car pslist) 0 (- e b))
11139           (setq gnus-newsgroup-unreads
11140                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11141                                          gnus-reffed-article-number))
11142           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11143           (setq pslist (cdr pslist)))))))
11144
11145 (defun gnus-pseudos< (p1 p2)
11146   (let ((c1 (cdr (assq 'action p1)))
11147         (c2 (cdr (assq 'action p2))))
11148     (and c1 c2 (string< c1 c2))))
11149
11150 (defun gnus-request-pseudo-article (props)
11151   (cond ((assq 'execute props)
11152          (gnus-execute-command (cdr (assq 'execute props)))))
11153   (let ((gnus-current-article (gnus-summary-article-number)))
11154     (gnus-run-hooks 'gnus-mark-article-hook)))
11155
11156 (defun gnus-execute-command (command &optional automatic)
11157   (save-excursion
11158     (gnus-article-setup-buffer)
11159     (set-buffer gnus-article-buffer)
11160     (setq buffer-read-only nil)
11161     (let ((command (if automatic command
11162                      (read-string "Command: " (cons command 0)))))
11163       (erase-buffer)
11164       (insert "$ " command "\n\n")
11165       (if gnus-view-pseudo-asynchronously
11166           (start-process "gnus-execute" (current-buffer) shell-file-name
11167                          shell-command-switch command)
11168         (call-process shell-file-name nil t nil
11169                       shell-command-switch command)))))
11170
11171 ;; Summary kill commands.
11172
11173 (defun gnus-summary-edit-global-kill (article)
11174   "Edit the \"global\" kill file."
11175   (interactive (list (gnus-summary-article-number)))
11176   (gnus-group-edit-global-kill article))
11177
11178 (defun gnus-summary-edit-local-kill ()
11179   "Edit a local kill file applied to the current newsgroup."
11180   (interactive)
11181   (setq gnus-current-headers (gnus-summary-article-header))
11182   (gnus-group-edit-local-kill
11183    (gnus-summary-article-number) gnus-newsgroup-name))
11184
11185 ;;; Header reading.
11186
11187 (defun gnus-read-header (id &optional header)
11188   "Read the headers of article ID and enter them into the Gnus system."
11189   (let ((group gnus-newsgroup-name)
11190         (gnus-override-method
11191          (or
11192           gnus-override-method
11193           (and (gnus-news-group-p gnus-newsgroup-name)
11194                (car (gnus-refer-article-methods)))))
11195         where)
11196     ;; First we check to see whether the header in question is already
11197     ;; fetched.
11198     (if (stringp id)
11199         ;; This is a Message-ID.
11200         (setq header (or header (gnus-id-to-header id)))
11201       ;; This is an article number.
11202       (setq header (or header (gnus-summary-article-header id))))
11203     (if (and header
11204              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11205         ;; We have found the header.
11206         header
11207       ;; If this is a sparse article, we have to nix out its
11208       ;; previous entry in the thread hashtb.
11209       (when (and header
11210                  (gnus-summary-article-sparse-p (mail-header-number header)))
11211         (let* ((parent (gnus-parent-id (mail-header-references header)))
11212                (thread (and parent (gnus-id-to-thread parent))))
11213           (when thread
11214             (delq (assq header thread) thread))))
11215       ;; We have to really fetch the header to this article.
11216       (save-excursion
11217         (set-buffer nntp-server-buffer)
11218         (when (setq where (gnus-request-head id group))
11219           (nnheader-fold-continuation-lines)
11220           (goto-char (point-max))
11221           (insert ".\n")
11222           (goto-char (point-min))
11223           (insert "211 ")
11224           (princ (cond
11225                   ((numberp id) id)
11226                   ((cdr where) (cdr where))
11227                   (header (mail-header-number header))
11228                   (t gnus-reffed-article-number))
11229                  (current-buffer))
11230           (insert " Article retrieved.\n"))
11231         (if (or (not where)
11232                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11233             ()                          ; Malformed head.
11234           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11235             (when (and (stringp id)
11236                        (not (string= (gnus-group-real-name group)
11237                                      (car where))))
11238               ;; If we fetched by Message-ID and the article came
11239               ;; from a different group, we fudge some bogus article
11240               ;; numbers for this article.
11241               (mail-header-set-number header gnus-reffed-article-number))
11242             (save-excursion
11243               (set-buffer gnus-summary-buffer)
11244               (decf gnus-reffed-article-number)
11245               (gnus-remove-header (mail-header-number header))
11246               (push header gnus-newsgroup-headers)
11247               (setq gnus-current-headers header)
11248               (push (mail-header-number header) gnus-newsgroup-limit)))
11249           header)))))
11250
11251 (defun gnus-remove-header (number)
11252   "Remove header NUMBER from `gnus-newsgroup-headers'."
11253   (if (and gnus-newsgroup-headers
11254            (= number (mail-header-number (car gnus-newsgroup-headers))))
11255       (pop gnus-newsgroup-headers)
11256     (let ((headers gnus-newsgroup-headers))
11257       (while (and (cdr headers)
11258                   (not (= number (mail-header-number (cadr headers)))))
11259         (pop headers))
11260       (when (cdr headers)
11261         (setcdr headers (cddr headers))))))
11262
11263 ;;;
11264 ;;; summary highlights
11265 ;;;
11266
11267 (defun gnus-highlight-selected-summary ()
11268   "Highlight selected article in summary buffer."
11269   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11270   (when gnus-summary-selected-face
11271     (save-excursion
11272       (let* ((beg (gnus-point-at-bol))
11273              (end (gnus-point-at-eol))
11274              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11275              (from (if (get-text-property beg gnus-mouse-face-prop)
11276                        beg
11277                      (or (next-single-property-change
11278                           beg gnus-mouse-face-prop nil end)
11279                          beg)))
11280              (to
11281               (if (= from end)
11282                   (- from 2)
11283                 (or (next-single-property-change
11284                      from gnus-mouse-face-prop nil end)
11285                     end))))
11286         ;; If no mouse-face prop on line we will have to = from = end,
11287         ;; so we highlight the entire line instead.
11288         (when (= (+ to 2) from)
11289           (setq from beg)
11290           (setq to end))
11291         (if gnus-newsgroup-selected-overlay
11292             ;; Move old overlay.
11293             (gnus-move-overlay
11294              gnus-newsgroup-selected-overlay from to (current-buffer))
11295           ;; Create new overlay.
11296           (gnus-overlay-put
11297            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11298            'face gnus-summary-selected-face))))))
11299
11300 (defvar gnus-summary-highlight-line-cached nil)
11301 (defvar gnus-summary-highlight-line-trigger nil)
11302
11303 (defun gnus-summary-highlight-line-0 ()
11304   (if (and (eq gnus-summary-highlight-line-trigger
11305                gnus-summary-highlight)
11306            gnus-summary-highlight-line-cached)
11307       gnus-summary-highlight-line-cached
11308     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11309           gnus-summary-highlight-line-cached
11310           (let* ((cond (list 'cond))
11311                  (c cond)
11312                  (list gnus-summary-highlight))
11313             (while list
11314               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11315                               nil))
11316               (setq c (cdr c)
11317                     list (cdr list)))
11318             (gnus-byte-compile (list 'lambda nil cond))))))
11319
11320 (defun gnus-summary-highlight-line ()
11321   "Highlight current line according to `gnus-summary-highlight'."
11322   (let* ((beg (gnus-point-at-bol))
11323          (article (or (gnus-summary-article-number) gnus-current-article))
11324          (score (or (cdr (assq article
11325                                gnus-newsgroup-scored))
11326                     gnus-summary-default-score 0))
11327          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11328          (inhibit-read-only t)
11329          (default gnus-summary-default-score)
11330          (default-high gnus-summary-default-high-score)
11331          (default-low gnus-summary-default-low-score)
11332          (uncached (and gnus-summary-use-undownloaded-faces
11333                         (memq article gnus-newsgroup-undownloaded))))
11334     (let ((face (funcall (gnus-summary-highlight-line-0))))
11335       (unless (eq face (get-text-property beg 'face))
11336         (gnus-put-text-property-excluding-characters-with-faces
11337          beg (gnus-point-at-eol) 'face
11338          (setq face (if (boundp face) (symbol-value face) face)))
11339         (when gnus-summary-highlight-line-function
11340           (funcall gnus-summary-highlight-line-function article face))))))
11341
11342 (defun gnus-update-read-articles (group unread &optional compute)
11343   "Update the list of read articles in GROUP.
11344 UNREAD is a sorted list."
11345   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11346          (entry (gnus-gethash group gnus-newsrc-hashtb))
11347          (info (nth 2 entry))
11348          (prev 1)
11349          read)
11350     (if (or (not info) (not active))
11351         ;; There is no info on this group if it was, in fact,
11352         ;; killed.  Gnus stores no information on killed groups, so
11353         ;; there's nothing to be done.
11354         ;; One could store the information somewhere temporarily,
11355         ;; perhaps...  Hmmm...
11356         ()
11357       ;; Remove any negative articles numbers.
11358       (while (and unread (< (car unread) 0))
11359         (setq unread (cdr unread)))
11360       ;; Remove any expired article numbers
11361       (while (and unread (< (car unread) (car active)))
11362         (setq unread (cdr unread)))
11363       ;; Compute the ranges of read articles by looking at the list of
11364       ;; unread articles.
11365       (while unread
11366         (when (/= (car unread) prev)
11367           (push (if (= prev (1- (car unread))) prev
11368                   (cons prev (1- (car unread))))
11369                 read))
11370         (setq prev (1+ (car unread)))
11371         (setq unread (cdr unread)))
11372       (when (<= prev (cdr active))
11373         (push (cons prev (cdr active)) read))
11374       (setq read (if (> (length read) 1) (nreverse read) read))
11375       (if compute
11376           read
11377         (save-excursion
11378           (let (setmarkundo)
11379             ;; Propagate the read marks to the backend.
11380             (when (gnus-check-backend-function 'request-set-mark group)
11381               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11382                     (add (gnus-remove-from-range read (gnus-info-read info))))
11383                 (when (or add del)
11384                   (unless (gnus-check-group group)
11385                     (error "Can't open server for %s" group))
11386                   (gnus-request-set-mark
11387                    group (delq nil (list (if add (list add 'add '(read)))
11388                                          (if del (list del 'del '(read))))))
11389                   (setq setmarkundo
11390                         `(gnus-request-set-mark
11391                           ,group
11392                           ',(delq nil (list
11393                                        (if del (list del 'add '(read)))
11394                                        (if add (list add 'del '(read))))))))))
11395             (set-buffer gnus-group-buffer)
11396             (gnus-undo-register
11397               `(progn
11398                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11399                  (gnus-info-set-read ',info ',(gnus-info-read info))
11400                  (gnus-get-unread-articles-in-group ',info
11401                                                     (gnus-active ,group))
11402                  (gnus-group-update-group ,group t)
11403                  ,setmarkundo))))
11404         ;; Enter this list into the group info.
11405         (gnus-info-set-read info read)
11406         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11407         (gnus-get-unread-articles-in-group info (gnus-active group))
11408         t))))
11409
11410 (defun gnus-offer-save-summaries ()
11411   "Offer to save all active summary buffers."
11412   (let (buffers)
11413     ;; Go through all buffers and find all summaries.
11414     (dolist (buffer (buffer-list))
11415       (when (and (setq buffer (buffer-name buffer))
11416                  (string-match "Summary" buffer)
11417                  (save-excursion
11418                    (set-buffer buffer)
11419                    ;; We check that this is, indeed, a summary buffer.
11420                    (and (eq major-mode 'gnus-summary-mode)
11421                         ;; Also make sure this isn't bogus.
11422                         gnus-newsgroup-prepared
11423                         ;; Also make sure that this isn't a
11424                         ;; dead summary buffer.
11425                         (not gnus-dead-summary-mode))))
11426         (push buffer buffers)))
11427     ;; Go through all these summary buffers and offer to save them.
11428     (when buffers
11429       (save-excursion
11430         (map-y-or-n-p
11431          "Update summary buffer %s? "
11432          (lambda (buf)
11433            (switch-to-buffer buf)
11434            (gnus-summary-exit))
11435          buffers)))))
11436
11437
11438 ;;; @ for mime-partial
11439 ;;;
11440
11441 (defun gnus-request-partial-message ()
11442   (save-excursion
11443     (let ((number (gnus-summary-article-number))
11444           (group gnus-newsgroup-name)
11445           (mother gnus-article-buffer))
11446       (set-buffer (get-buffer-create " *Partial Article*"))
11447       (erase-buffer)
11448       (setq mime-preview-buffer mother)
11449       (gnus-request-article-this-buffer number group)
11450       (mime-parse-buffer)
11451       )))
11452
11453 (autoload 'mime-combine-message/partial-pieces-automatically
11454   "mime-partial"
11455   "Internal method to combine message/partial messages automatically.")
11456
11457 (mime-add-condition
11458  'action '((type . message)(subtype . partial)
11459            (major-mode . gnus-original-article-mode)
11460            (method . mime-combine-message/partial-pieces-automatically)
11461            (summary-buffer-exp . gnus-summary-buffer)
11462            (request-partial-message-method . gnus-request-partial-message)
11463            ))
11464
11465
11466 ;;; @ for message/rfc822
11467 ;;;
11468
11469 (defun gnus-mime-extract-message/rfc822 (entity situation)
11470   "Burst a forwarded article."
11471   (save-excursion
11472     (set-buffer gnus-summary-buffer)
11473     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11474                                    gnus-newsgroup-name 'gnus-group-history))
11475            (gnus-group-marked (list group))
11476            article info)
11477       (with-temp-buffer
11478         (mime-insert-entity-content entity)
11479         (setq article (gnus-request-accept-article group)))
11480       (when (and (consp article)
11481                  (numberp (setq article (cdr article))))
11482         (setq info (gnus-get-info group))
11483         (gnus-info-set-read info
11484                             (gnus-remove-from-range (gnus-info-read info)
11485                                                     (list article)))
11486         (when (string-equal group gnus-newsgroup-name)
11487           (forward-line 1)
11488           (let (gnus-show-threads)
11489             (gnus-summary-goto-subject article t))
11490           (gnus-summary-clear-mark-forward 1))
11491         (set-buffer gnus-group-buffer)
11492         (gnus-group-get-new-news-this-group nil t)))))
11493
11494 (mime-add-condition
11495  'action '((type . message)(subtype . rfc822)
11496            (major-mode . gnus-original-article-mode)
11497            (method . gnus-mime-extract-message/rfc822)
11498            (mode . "extract")
11499            ))
11500
11501 (mime-add-condition
11502  'action '((type . message)(subtype . news)
11503            (major-mode . gnus-original-article-mode)
11504            (method . gnus-mime-extract-message/rfc822)
11505            (mode . "extract")
11506            ))
11507
11508 (defun gnus-mime-extract-multipart (entity situation)
11509   (let ((children (mime-entity-children entity))
11510         mime-acting-situation-to-override
11511         f)
11512     (while children
11513       (mime-play-entity (car children)
11514                         (cons (assq 'mode situation)
11515                               mime-acting-situation-to-override))
11516       (setq children (cdr children)))
11517     (if (setq f (cdr (assq 'after-method
11518                            mime-acting-situation-to-override)))
11519         (eval f)
11520       )))
11521
11522 (mime-add-condition
11523  'action '((type . multipart)
11524            (method . gnus-mime-extract-multipart)
11525            (mode . "extract")
11526            )
11527  'with-default)
11528
11529
11530 ;;; @ end
11531 ;;;
11532
11533 (defun gnus-summary-inherit-default-charset ()
11534   "Import `default-mime-charset' from summary buffer.
11535 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11536 of FLIM is used."
11537   (if (buffer-live-p gnus-summary-buffer)
11538       (let (d-m-c d-m-c-u)
11539         (with-current-buffer gnus-summary-buffer
11540           (setq d-m-c (if (local-variable-p 'default-mime-charset
11541                                             gnus-summary-buffer)
11542                           default-mime-charset
11543                         t)
11544                 ;; LIMIT
11545                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11546                                               gnus-summary-buffer)
11547                             (symbol-value 'default-mime-charset-unlimited)
11548                           t)))
11549         (if (eq t d-m-c)
11550             (kill-local-variable 'default-mime-charset)
11551           (set (make-local-variable 'default-mime-charset) d-m-c))
11552         (if (eq t d-m-c-u)
11553             (kill-local-variable 'default-mime-charset-unlimited)
11554           (set (make-local-variable 'default-mime-charset-unlimited)
11555                d-m-c-u)))))
11556
11557 (defun gnus-summary-setup-default-charset ()
11558   "Setup newsgroup default charset."
11559   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11560       (progn
11561         (setq gnus-newsgroup-charset nil)
11562         (set (make-local-variable 'default-mime-charset) nil)
11563         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11564           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11565     (let ((ignored-charsets
11566            (or gnus-newsgroup-ephemeral-ignored-charsets
11567                (append
11568                 (and gnus-newsgroup-name
11569                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11570                 gnus-newsgroup-ignored-charsets)))
11571           charset)
11572       (setq gnus-newsgroup-charset
11573             (or gnus-newsgroup-ephemeral-charset
11574                 (when (and gnus-newsgroup-name
11575                            (setq charset (gnus-parameter-charset
11576                                           gnus-newsgroup-name)))
11577                   (make-local-variable 'default-mime-charset)
11578                   (setq default-mime-charset charset))
11579                 gnus-default-charset))
11580       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11581            ignored-charsets))))
11582
11583 ;;;
11584 ;;; Mime Commands
11585 ;;;
11586
11587 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11588   "Display the current article buffer fully MIME-buttonized.
11589 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11590 treated as multipart/mixed."
11591   (interactive "P")
11592   (require 'gnus-art)
11593   (let ((gnus-unbuttonized-mime-types nil)
11594         (gnus-mime-display-multipart-as-mixed show-all-parts))
11595     (gnus-summary-show-article)))
11596
11597 (defun gnus-summary-repair-multipart (article)
11598   "Add a Content-Type header to a multipart article without one."
11599   (interactive (list (gnus-summary-article-number)))
11600   (gnus-with-article article
11601     (message-narrow-to-head)
11602     (message-remove-header "Mime-Version")
11603     (goto-char (point-max))
11604     (insert "Mime-Version: 1.0\n")
11605     (widen)
11606     (when (search-forward "\n--" nil t)
11607       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11608         (message-narrow-to-head)
11609         (message-remove-header "Content-Type")
11610         (goto-char (point-max))
11611         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11612                         separator))
11613         (widen))))
11614   (let (gnus-mark-article-hook)
11615     (gnus-summary-select-article t t nil article)))
11616
11617 (defun gnus-summary-toggle-display-buttonized ()
11618   "Toggle the buttonizing of the article buffer."
11619   (interactive)
11620   (require 'gnus-art)
11621   (if (setq gnus-inhibit-mime-unbuttonizing
11622             (not gnus-inhibit-mime-unbuttonizing))
11623       (let ((gnus-unbuttonized-mime-types nil))
11624         (gnus-summary-show-article))
11625     (gnus-summary-show-article)))
11626
11627 ;;;
11628 ;;; Intelli-mouse commmands
11629 ;;;
11630
11631 (defun gnus-wheel-summary-scroll (event)
11632   (interactive "e")
11633   (let ((amount (if (memq 'shift (event-modifiers event))
11634                     (car gnus-wheel-scroll-amount)
11635                   (cdr gnus-wheel-scroll-amount)))
11636         (direction (- (* (static-if (featurep 'xemacs)
11637                              (event-button event)
11638                            (cond ((eq 'mouse-4 (event-basic-type event))
11639                                   4)
11640                                  ((eq 'mouse-5 (event-basic-type event))
11641                                   5)))
11642                          2) 9))
11643         edge)
11644     (gnus-summary-scroll-up (* amount direction))
11645     (when (gnus-eval-in-buffer-window gnus-article-buffer
11646             (save-restriction
11647               (widen)
11648               (and (if (< 0 direction)
11649                        (gnus-article-next-page 0)
11650                      (gnus-article-prev-page 0)
11651                      (bobp))
11652                    (if (setq edge (get-text-property
11653                                    (point-min) 'gnus-wheel-edge))
11654                        (setq edge (* edge direction))
11655                      (setq edge -1))
11656                    (or (plusp edge)
11657                        (let ((buffer-read-only nil)
11658                              (inhibit-read-only t))
11659                          (put-text-property (point-min) (point-max)
11660                                             'gnus-wheel-edge direction)
11661                          nil))
11662                    (or (> edge gnus-wheel-edge-resistance)
11663                        (let ((buffer-read-only nil)
11664                              (inhibit-read-only t))
11665                          (put-text-property (point-min) (point-max)
11666                                             'gnus-wheel-edge
11667                                             (* (1+ edge) direction))
11668                          nil))
11669                    (eq last-command 'gnus-wheel-summary-scroll))))
11670       (gnus-summary-next-article nil nil (minusp direction)))))
11671
11672 (defun gnus-wheel-install ()
11673   "Enable mouse wheel support on summary window."
11674   (when gnus-use-wheel
11675     (let ((keys
11676            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11677       (dolist (key keys)
11678         (define-key gnus-summary-mode-map key
11679           'gnus-wheel-summary-scroll)))))
11680
11681 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11682
11683 ;;;
11684 ;;; Traditional PGP commmands
11685 ;;;
11686
11687 (defun gnus-summary-decrypt-article (&optional force)
11688   "Decrypt the current article in traditional PGP way.
11689 This will have permanent effect only in mail groups.
11690 If FORCE is non-nil, allow editing of articles even in read-only
11691 groups."
11692   (interactive "P")
11693   (gnus-summary-select-article t)
11694   (gnus-eval-in-buffer-window gnus-article-buffer
11695     (save-excursion
11696       (save-restriction
11697         (widen)
11698         (goto-char (point-min))
11699         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11700           (error "Not a traditional PGP message!"))
11701         (let ((armor-start (match-beginning 0)))
11702           (if (and (pgg-decrypt-region armor-start (point-max))
11703                    (or force (not (gnus-group-read-only-p))))
11704               (let ((inhibit-read-only t)
11705                     buffer-read-only)
11706                 (delete-region armor-start
11707                                (progn
11708                                  (re-search-forward "^-+END PGP" nil t)
11709                                  (beginning-of-line 2)
11710                                  (point)))
11711                 (insert-buffer-substring pgg-output-buffer))))))))
11712
11713 (defun gnus-summary-verify-article ()
11714   "Verify the current article in traditional PGP way."
11715   (interactive)
11716   (save-excursion
11717     (set-buffer gnus-original-article-buffer)
11718     (goto-char (point-min))
11719     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11720       (error "Not a traditional PGP message!"))
11721     (re-search-forward "^-+END PGP" nil t)
11722     (beginning-of-line 2)
11723     (call-interactively (function pgg-verify-region))))
11724
11725 ;;;
11726 ;;; Generic summary marking commands
11727 ;;;
11728
11729 (defvar gnus-summary-marking-alist
11730   '((read gnus-del-mark "d")
11731     (unread gnus-unread-mark "u")
11732     (ticked gnus-ticked-mark "!")
11733     (dormant gnus-dormant-mark "?")
11734     (expirable gnus-expirable-mark "e"))
11735   "An alist of names/marks/keystrokes.")
11736
11737 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11738 (defvar gnus-summary-mark-map)
11739
11740 (defun gnus-summary-make-all-marking-commands ()
11741   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11742   (dolist (elem gnus-summary-marking-alist)
11743     (apply 'gnus-summary-make-marking-command elem)))
11744
11745 (defun gnus-summary-make-marking-command (name mark keystroke)
11746   (let ((map (make-sparse-keymap)))
11747     (define-key gnus-summary-generic-mark-map keystroke map)
11748     (dolist (lway `((next "next" next nil "n")
11749                     (next-unread "next unread" next t "N")
11750                     (prev "previous" prev nil "p")
11751                     (prev-unread "previous unread" prev t "P")
11752                     (nomove "" nil nil ,keystroke)))
11753       (let ((func (gnus-summary-make-marking-command-1
11754                    mark (car lway) lway name)))
11755         (setq func (eval func))
11756         (define-key map (nth 4 lway) func)))))
11757
11758 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11759   `(defun ,(intern
11760             (format "gnus-summary-put-mark-as-%s%s"
11761                     name (if (eq way 'nomove)
11762                              ""
11763                            (concat "-" (symbol-name way)))))
11764      (n)
11765      ,(format
11766        "Mark the current article as %s%s.
11767 If N, the prefix, then repeat N times.
11768 If N is negative, move in reverse order.
11769 The difference between N and the actual number of articles marked is
11770 returned."
11771        name (car (cdr lway)))
11772      (interactive "p")
11773      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11774
11775 (defun gnus-summary-generic-mark (n mark move unread)
11776   "Mark N articles with MARK."
11777   (unless (eq major-mode 'gnus-summary-mode)
11778     (error "This command can only be used in the summary buffer"))
11779   (gnus-summary-show-thread)
11780   (let ((nummove
11781          (cond
11782           ((eq move 'next) 1)
11783           ((eq move 'prev) -1)
11784           (t 0))))
11785     (if (zerop nummove)
11786         (setq n 1)
11787       (when (< n 0)
11788         (setq n (abs n)
11789               nummove (* -1 nummove))))
11790     (while (and (> n 0)
11791                 (gnus-summary-mark-article nil mark)
11792                 (zerop (gnus-summary-next-subject nummove unread t)))
11793       (setq n (1- n)))
11794     (when (/= 0 n)
11795       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11796     (gnus-summary-recenter)
11797     (gnus-summary-position-point)
11798     (gnus-set-mode-line 'summary)
11799     n))
11800
11801 (defun gnus-summary-insert-articles (articles)
11802   (when (setq articles
11803               (gnus-sorted-difference articles
11804                                       (mapcar (lambda (h)
11805                                                 (mail-header-number h))
11806                                               gnus-newsgroup-headers)))
11807     (setq gnus-newsgroup-headers
11808           (gnus-merge 'list
11809                       gnus-newsgroup-headers
11810                       (gnus-fetch-headers articles)
11811                       'gnus-article-sort-by-number))
11812     ;; Suppress duplicates?
11813     (when gnus-suppress-duplicates
11814       (gnus-dup-suppress-articles))
11815
11816     ;; We might want to build some more threads first.
11817     (when (and gnus-fetch-old-headers
11818                (eq gnus-headers-retrieved-by 'nov))
11819       (if (eq gnus-fetch-old-headers 'invisible)
11820           (gnus-build-all-threads)
11821         (gnus-build-old-threads)))
11822     ;; Let the Gnus agent mark articles as read.
11823     (when gnus-agent
11824       (gnus-agent-get-undownloaded-list))
11825     ;; Remove list identifiers from subject
11826     (when gnus-list-identifiers
11827       (gnus-summary-remove-list-identifiers))
11828     ;; First and last article in this newsgroup.
11829     (when gnus-newsgroup-headers
11830       (setq gnus-newsgroup-begin
11831             (mail-header-number (car gnus-newsgroup-headers))
11832             gnus-newsgroup-end
11833             (mail-header-number
11834              (gnus-last-element gnus-newsgroup-headers))))
11835     (when gnus-use-scoring
11836       (gnus-possibly-score-headers))))
11837
11838 (defun gnus-summary-insert-old-articles (&optional all)
11839   "Insert all old articles in this group.
11840 If ALL is non-nil, already read articles become readable.
11841 If ALL is a number, fetch this number of articles."
11842   (interactive "P")
11843   (prog1
11844       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11845             older len)
11846         (setq older
11847               ;; Some nntp servers lie about their active range.  When
11848               ;; this happens, the active range can be in the millions.
11849               ;; Use a compressed range to avoid creating a huge list.
11850               (gnus-range-difference (list gnus-newsgroup-active) old))
11851         (setq len (gnus-range-length older))
11852         (cond
11853          ((null older) nil)
11854          ((numberp all)
11855           (if (< all len)
11856               (let ((older-range (nreverse older)))
11857                 (setq older nil)
11858
11859                 (while (> all 0)
11860                   (let* ((r (pop older-range))
11861                          (min (if (numberp r) r (car r)))
11862                          (max (if (numberp r) r (cdr r))))
11863                     (while (and (<= min max)
11864                                 (> all 0))
11865                       (push max older)
11866                       (setq all (1- all)
11867                             max (1- max))))))
11868             (setq older (gnus-uncompress-range older))))
11869          (all
11870           (setq older (gnus-uncompress-range older)))
11871          (t
11872           (when (and (numberp gnus-large-newsgroup)
11873                    (> len gnus-large-newsgroup))
11874               (let* ((cursor-in-echo-area nil)
11875                      (initial (gnus-parameter-large-newsgroup-initial
11876                                gnus-newsgroup-name))
11877                      (input
11878                       (read-string
11879                        (format
11880                         "How many articles from %s (%s %d): "
11881                         (gnus-limit-string
11882                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11883                         (if initial "max" "default")
11884                         len)
11885                        (if initial
11886                            (cons (number-to-string initial)
11887                                  0)))))
11888                 (unless (string-match "^[ \t]*$" input)
11889                   (setq all (string-to-number input))
11890                   (if (< all len)
11891                       (let ((older-range (nreverse older)))
11892                         (setq older nil)
11893
11894                         (while (> all 0)
11895                           (let* ((r (pop older-range))
11896                                  (min (if (numberp r) r (car r)))
11897                                  (max (if (numberp r) r (cdr r))))
11898                             (while (and (<= min max)
11899                                         (> all 0))
11900                               (push max older)
11901                               (setq all (1- all)
11902                                     max (1- max))))))))))
11903           (setq older (gnus-uncompress-range older))))
11904         (if (not older)
11905             (message "No old news.")
11906           (gnus-summary-insert-articles older)
11907           (gnus-summary-limit (gnus-sorted-nunion old older))))
11908     (gnus-summary-position-point)))
11909
11910 (defun gnus-summary-insert-new-articles ()
11911   "Insert all new articles in this group."
11912   (interactive)
11913   (prog1
11914       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11915             (old-active gnus-newsgroup-active)
11916             (nnmail-fetched-sources (list t))
11917             i new)
11918         (setq gnus-newsgroup-active
11919               (gnus-activate-group gnus-newsgroup-name 'scan))
11920         (setq i (cdr gnus-newsgroup-active))
11921         (while (> i (cdr old-active))
11922           (push i new)
11923           (decf i))
11924         (if (not new)
11925             (message "No gnus is bad news.")
11926           (gnus-summary-insert-articles new)
11927           (setq gnus-newsgroup-unreads
11928                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11929           (gnus-summary-limit (gnus-sorted-nunion old new))))
11930     (gnus-summary-position-point)))
11931
11932 (gnus-summary-make-all-marking-commands)
11933
11934 (gnus-ems-redefine)
11935
11936 (provide 'gnus-sum)
11937
11938 (run-hooks 'gnus-sum-load-hook)
11939
11940 ;; Local Variables:
11941 ;; coding: iso-8859-1
11942 ;; End:
11943
11944 ;;; gnus-sum.el ends here