(gnus-summary-make-menu-bar): Added
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-make-false-root-always t
113   "Always make a false dummy root."
114   :group 'gnus-thread
115   :type 'boolean)
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include:
145 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
146 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 \(they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-default-high-score 0
211   "*Default threshold for a high scored article.
212 An article will be highlighted as high scored if its score is greater
213 than this score."
214   :group 'gnus-score-default
215   :type 'integer)
216
217 (defcustom gnus-summary-default-low-score 0
218   "*Default threshold for a low scored article.
219 An article will be highlighted as low scored if its score is smaller
220 than this score."
221   :group 'gnus-score-default
222   :type 'integer)
223
224 (defcustom gnus-summary-zcore-fuzz 0
225   "*Fuzziness factor for the zcore in the summary buffer.
226 Articles with scores closer than this to `gnus-summary-default-score'
227 will not be marked."
228   :group 'gnus-summary-format
229   :type 'integer)
230
231 (defcustom gnus-simplify-subject-fuzzy-regexp nil
232   "*Strings to be removed when doing fuzzy matches.
233 This can either be a regular expression or list of regular expressions
234 that will be removed from subject strings if fuzzy subject
235 simplification is selected."
236   :group 'gnus-thread
237   :type '(repeat regexp))
238
239 (defcustom gnus-show-threads t
240   "*If non-nil, display threads in summary mode."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-subtree nil
245   "*If non-nil, hide all threads initially.
246 This can be a predicate specifier which says which threads to hide.
247 If threads are hidden, you have to run the command
248 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
249 to expose hidden threads."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-hide-killed t
254   "*If non-nil, hide killed threads automatically."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-ignore-subject t
259   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
260 If nil, articles that have different subjects from their parents will
261 start separate threads."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-operation-ignore-subject t
266   "*If non-nil, subjects will be ignored when doing thread commands.
267 This affects commands like `gnus-summary-kill-thread' and
268 `gnus-summary-lower-thread'.
269
270 If this variable is nil, articles in the same thread with different
271 subjects will not be included in the operation in question.  If this
272 variable is `fuzzy', only articles that have subjects that are fuzzily
273 equal will be included."
274   :group 'gnus-thread
275   :type '(choice (const :tag "off" nil)
276                  (const fuzzy)
277                  (sexp :tag "on" t)))
278
279 (defcustom gnus-thread-indent-level 4
280   "*Number that says how much each sub-thread should be indented."
281   :group 'gnus-thread
282   :type 'integer)
283
284 (defcustom gnus-auto-extend-newsgroup t
285   "*If non-nil, extend newsgroup forward and backward when requested."
286   :group 'gnus-summary-choose
287   :type 'boolean)
288
289 (defcustom gnus-auto-select-first t
290   "*If non-nil, select the article under point.
291 Which article this is is controlled by the `gnus-auto-select-subject'
292 variable.
293
294 If you want to prevent automatic selection of articles in some
295 newsgroups, set the variable to nil in `gnus-select-group-hook'."
296   :group 'gnus-group-select
297   :type '(choice (const :tag "none" nil)
298                  (sexp :menu-tag "first" t)))
299
300 (defcustom gnus-auto-select-subject 'unread
301   "*Says what subject to place under point when entering a group.
302
303 This variable can either be the symbols `first' (place point on the
304 first subject), `unread' (place point on the subject line of the first
305 unread article), `best' (place point on the subject line of the
306 higest-scored article), `unseen' (place point on the subject line of
307 the first unseen article), 'unseen-or-unread' (place point on the subject
308 line of the first unseen article or, if all article have been seen, on the
309 subject line of the first unread article), or a function to be called to
310 place point on some subject line."
311   :group 'gnus-group-select
312   :type '(choice (const best)
313                  (const unread)
314                  (const first)
315                  (const unseen)
316                  (const unseen-or-unread)))
317
318 (defcustom gnus-auto-select-next t
319   "*If non-nil, offer to go to the next group from the end of the previous.
320 If the value is t and the next newsgroup is empty, Gnus will exit
321 summary mode and go back to group mode.  If the value is neither nil
322 nor t, Gnus will select the following unread newsgroup.  In
323 particular, if the value is the symbol `quietly', the next unread
324 newsgroup will be selected without any confirmation, and if it is
325 `almost-quietly', the next group will be selected without any
326 confirmation if you are located on the last article in the group.
327 Finally, if this variable is `slightly-quietly', the `Z n' command
328 will go to the next group without confirmation."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "off" nil)
331                  (const quietly)
332                  (const almost-quietly)
333                  (const slightly-quietly)
334                  (sexp :menu-tag "on" t)))
335
336 (defcustom gnus-auto-select-same nil
337   "*If non-nil, select the next article with the same subject.
338 If there are no more articles with the same subject, go to
339 the first unread article."
340   :group 'gnus-summary-maneuvering
341   :type 'boolean)
342
343 (defcustom gnus-summary-check-current nil
344   "*If non-nil, consider the current article when moving.
345 The \"unread\" movement commands will stay on the same line if the
346 current article is unread."
347   :group 'gnus-summary-maneuvering
348   :type 'boolean)
349
350 (defcustom gnus-auto-center-summary t
351   "*If non-nil, always center the current summary buffer.
352 In particular, if `vertical' do only vertical recentering.  If non-nil
353 and non-`vertical', do both horizontal and vertical recentering."
354   :group 'gnus-summary-maneuvering
355   :type '(choice (const :tag "none" nil)
356                  (const vertical)
357                  (integer :tag "height")
358                  (sexp :menu-tag "both" t)))
359
360 (defcustom gnus-show-all-headers nil
361   "*If non-nil, don't hide any headers."
362   :group 'gnus-article-hiding
363   :group 'gnus-article-headers
364   :type 'boolean)
365
366 (defcustom gnus-summary-ignore-duplicates nil
367   "*If non-nil, ignore articles with identical Message-ID headers."
368   :group 'gnus-summary
369   :type 'boolean)
370
371 (defcustom gnus-single-article-buffer t
372   "*If non-nil, display all articles in the same buffer.
373 If nil, each group will get its own article buffer."
374   :group 'gnus-article-various
375   :type 'boolean)
376
377 (defcustom gnus-break-pages t
378   "*If non-nil, do page breaking on articles.
379 The page delimiter is specified by the `gnus-page-delimiter'
380 variable."
381   :group 'gnus-article-various
382   :type 'boolean)
383
384 (defcustom gnus-move-split-methods nil
385   "*Variable used to suggest where articles are to be moved to.
386 It uses the same syntax as the `gnus-split-methods' variable.
387 However, whereas `gnus-split-methods' specifies file names as targets,
388 this variable specifies group names."
389   :group 'gnus-summary-mail
390   :type '(repeat (choice (list :value (fun) function)
391                          (cons :value ("" "") regexp (repeat string))
392                          (sexp :value nil))))
393
394 (defcustom gnus-unread-mark ?           ;Whitespace
395   "*Mark used for unread articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-ticked-mark ?!
400   "*Mark used for ticked articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-dormant-mark ??
405   "*Mark used for dormant articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-del-mark ?r
410   "*Mark used for del'd articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-read-mark ?R
415   "*Mark used for read articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-expirable-mark ?E
420   "*Mark used for expirable articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-killed-mark ?K
425   "*Mark used for killed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-spam-mark ?H
430   "*Mark used for spam articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-souped-mark ?F
435   "*Mark used for souped articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-kill-file-mark ?X
440   "*Mark used for articles killed by kill files."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-low-score-mark ?Y
445   "*Mark used for articles with a low score."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-catchup-mark ?C
450   "*Mark used for articles that are caught up."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-replied-mark ?A
455   "*Mark used for articles that have been replied to."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-forwarded-mark ?F
460   "*Mark used for articles that have been forwarded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-recent-mark ?N
465   "*Mark used for articles that are recent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-cached-mark ?*
470   "*Mark used for articles that are in the cache."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-saved-mark ?S
475   "*Mark used for articles that have been saved."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unseen-mark ?.
480   "*Mark used for articles that haven't been seen."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-no-mark ?               ;Whitespace
485   "*Mark used for articles that have no other secondary mark."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-ancient-mark ?O
490   "*Mark used for ancient articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-sparse-mark ?Q
495   "*Mark used for sparsely reffed articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-canceled-mark ?G
500   "*Mark used for canceled articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-duplicate-mark ?M
505   "*Mark used for duplicate articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-undownloaded-mark ?-
510   "*Mark used for articles that weren't downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-downloaded-mark ?+
515   "*Mark used for articles that were downloaded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-downloadable-mark ?%
520   "*Mark used for articles that are to be downloaded."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-unsendable-mark ?=
525   "*Mark used for articles that won't be sent."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-score-over-mark ?+
530   "*Score mark used for articles with high scores."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-score-below-mark ?-
535   "*Score mark used for articles with low scores."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-empty-thread-mark ?     ;Whitespace
540   "*There is no thread under the article."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-not-empty-thread-mark ?=
545   "*There is a thread under the article."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-view-pseudo-asynchronously nil
550   "*If non-nil, Gnus will view pseudo-articles asynchronously."
551   :group 'gnus-extract-view
552   :type 'boolean)
553
554 (defcustom gnus-auto-expirable-marks
555   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
556         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
557         gnus-souped-mark gnus-duplicate-mark)
558   "*The list of marks converted into expiration if a group is auto-expirable."
559   :version "21.1"
560   :group 'gnus-summary
561   :type '(repeat character))
562
563 (defcustom gnus-inhibit-user-auto-expire t
564   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
565   :version "21.1"
566   :group 'gnus-summary
567   :type 'boolean)
568
569 (defcustom gnus-view-pseudos nil
570   "*If `automatic', pseudo-articles will be viewed automatically.
571 If `not-confirm', pseudos will be viewed automatically, and the user
572 will not be asked to confirm the command."
573   :group 'gnus-extract-view
574   :type '(choice (const :tag "off" nil)
575                  (const automatic)
576                  (const not-confirm)))
577
578 (defcustom gnus-view-pseudos-separately t
579   "*If non-nil, one pseudo-article will be created for each file to be viewed.
580 If nil, all files that use the same viewing command will be given as a
581 list of parameters to that command."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-insert-pseudo-articles t
586   "*If non-nil, insert pseudo-articles when decoding articles."
587   :group 'gnus-extract-view
588   :type 'boolean)
589
590 (defcustom gnus-summary-dummy-line-format
591   "   %(:                             :%) %S\n"
592   "*The format specification for the dummy roots in the summary buffer.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions.
595
596 %S  The subject
597
598 General format specifiers can also be used.
599 See `(gnus)Formatting Variables'."
600   :link '(custom-manual "(gnus)Formatting Variables")
601   :group 'gnus-threading
602   :type 'string)
603
604 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
605   "*The format specification for the summary mode line.
606 It works along the same lines as a normal formatting string,
607 with some simple extensions:
608
609 %G  Group name
610 %p  Unprefixed group name
611 %A  Current article number
612 %z  Current article score
613 %V  Gnus version
614 %U  Number of unread articles in the group
615 %e  Number of unselected articles in the group
616 %Z  A string with unread/unselected article counts
617 %g  Shortish group name
618 %S  Subject of the current article
619 %u  User-defined spec
620 %s  Current score file name
621 %d  Number of dormant articles
622 %r  Number of articles that have been marked as read in this session
623 %E  Number of articles expunged by the score files"
624   :group 'gnus-summary-format
625   :type 'string)
626
627 (defcustom gnus-list-identifiers nil
628   "Regexp that matches list identifiers to be removed from subject.
629 This can also be a list of regexps."
630   :version "21.1"
631   :group 'gnus-summary-format
632   :group 'gnus-article-hiding
633   :type '(choice (const :tag "none" nil)
634                  (regexp :value ".*")
635                  (repeat :value (".*") regexp)))
636
637 (defcustom gnus-summary-mark-below 0
638   "*Mark all articles with a score below this variable as read.
639 This variable is local to each summary buffer and usually set by the
640 score file."
641   :group 'gnus-score-default
642   :type 'integer)
643
644 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
645   "*List of functions used for sorting articles in the summary buffer.
646
647 Each function takes two articles and returns non-nil if the first
648 article should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-article-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-article-sort-by-number',
657 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
658 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
659 and `gnus-article-sort-by-score'.
660
661 When threading is turned on, the variable `gnus-thread-sort-functions'
662 controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-article-sort-by-number)
665                          (function-item gnus-article-sort-by-author)
666                          (function-item gnus-article-sort-by-subject)
667                          (function-item gnus-article-sort-by-date)
668                          (function-item gnus-article-sort-by-score)
669                          (function-item gnus-article-sort-by-random)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
673   "*List of functions used for sorting threads in the summary buffer.
674 By default, threads are sorted by article number.
675
676 Each function takes two threads and returns non-nil if the first
677 thread should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-thread-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-thread-sort-by-number',
686 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
687 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
688 `gnus-thread-sort-by-most-recent-number',
689 `gnus-thread-sort-by-most-recent-date',
690 `gnus-thread-sort-by-random', and
691 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
692
693 When threading is turned off, the variable
694 `gnus-article-sort-functions' controls how articles are sorted."
695   :group 'gnus-summary-sort
696   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
697                          (function-item gnus-thread-sort-by-author)
698                          (function-item gnus-thread-sort-by-subject)
699                          (function-item gnus-thread-sort-by-date)
700                          (function-item gnus-thread-sort-by-score)
701                          (function-item gnus-thread-sort-by-total-score)
702                          (function-item gnus-thread-sort-by-random)
703                          (function :tag "other"))))
704
705 (defcustom gnus-thread-score-function '+
706   "*Function used for calculating the total score of a thread.
707
708 The function is called with the scores of the article and each
709 subthread and should then return the score of the thread.
710
711 Some functions you can use are `+', `max', or `min'."
712   :group 'gnus-summary-sort
713   :type 'function)
714
715 (defcustom gnus-summary-expunge-below nil
716   "All articles that have a score less than this variable will be expunged.
717 This variable is local to the summary buffers."
718   :group 'gnus-score-default
719   :type '(choice (const :tag "off" nil)
720                  integer))
721
722 (defcustom gnus-thread-expunge-below nil
723   "All threads that have a total score less than this variable will be expunged.
724 See `gnus-thread-score-function' for en explanation of what a
725 \"thread score\" is.
726
727 This variable is local to the summary buffers."
728   :group 'gnus-threading
729   :group 'gnus-score-default
730   :type '(choice (const :tag "off" nil)
731                  integer))
732
733 (defcustom gnus-summary-mode-hook nil
734   "*A hook for Gnus summary mode.
735 This hook is run before any variables are set in the summary buffer."
736   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
737   :group 'gnus-summary-various
738   :type 'hook)
739
740 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
741 (when (featurep 'xemacs)
742   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
743   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
744   (add-hook 'gnus-summary-mode-hook
745             'gnus-xmas-switch-horizontal-scrollbar-off))
746
747 (defcustom gnus-summary-menu-hook nil
748   "*Hook run after the creation of the summary mode menu."
749   :group 'gnus-summary-visual
750   :type 'hook)
751
752 (defcustom gnus-summary-exit-hook nil
753   "*A hook called on exit from the summary buffer.
754 It will be called with point in the group buffer."
755   :group 'gnus-summary-exit
756   :type 'hook)
757
758 (defcustom gnus-summary-prepare-hook nil
759   "*A hook called after the summary buffer has been generated.
760 If you want to modify the summary buffer, you can use this hook."
761   :group 'gnus-summary-various
762   :type 'hook)
763
764 (defcustom gnus-summary-prepared-hook nil
765   "*A hook called as the last thing after the summary buffer has been generated."
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 (defcustom gnus-summary-generate-hook nil
770   "*A hook run just before generating the summary buffer.
771 This hook is commonly used to customize threading variables and the
772 like."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-select-group-hook nil
777   "*A hook called when a newsgroup is selected.
778
779 If you'd like to simplify subjects like the
780 `gnus-summary-next-same-subject' command does, you can use the
781 following hook:
782
783  (add-hook gnus-select-group-hook
784            (lambda ()
785              (mapcar (lambda (header)
786                        (mail-header-set-subject
787                         header
788                         (gnus-simplify-subject
789                          (mail-header-subject header) 're-only)))
790                      gnus-newsgroup-headers)))"
791   :group 'gnus-group-select
792   :type 'hook)
793
794 (defcustom gnus-select-article-hook nil
795   "*A hook called when an article is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-visual-mark-article-hook
800   (list 'gnus-highlight-selected-summary)
801   "*Hook run after selecting an article in the summary buffer.
802 It is meant to be used for highlighting the article in some way.  It
803 is not run if `gnus-visual' is nil."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-parse-headers-hook nil
808   "*A hook called before parsing the headers."
809   :group 'gnus-various
810   :type 'hook)
811
812 (defcustom gnus-exit-group-hook nil
813   "*A hook called when exiting summary mode.
814 This hook is not called from the non-updating exit commands like `Q'."
815   :group 'gnus-various
816   :type 'hook)
817
818 (defcustom gnus-summary-update-hook
819   (list 'gnus-summary-highlight-line)
820   "*A hook called when a summary line is changed.
821 The hook will not be called if `gnus-visual' is nil.
822
823 The default function `gnus-summary-highlight-line' will
824 highlight the line according to the `gnus-summary-highlight'
825 variable."
826   :group 'gnus-summary-visual
827   :type 'hook)
828
829 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
830   "*A hook called when an article is selected for the first time.
831 The hook is intended to mark an article as read (or unread)
832 automatically when it is selected."
833   :group 'gnus-summary-choose
834   :type 'hook)
835
836 (defcustom gnus-group-no-more-groups-hook nil
837   "*A hook run when returning to group mode having no more (unread) groups."
838   :group 'gnus-group-select
839   :type 'hook)
840
841 (defcustom gnus-ps-print-hook nil
842   "*A hook run before ps-printing something from Gnus."
843   :group 'gnus-summary
844   :type 'hook)
845
846 (defcustom gnus-summary-display-arrow
847   (and (fboundp 'display-graphic-p)
848        (display-graphic-p))
849   "*If non-nil, display an arrow highlighting the current article."
850   :version "21.1"
851   :group 'gnus-summary
852   :type 'boolean)
853
854 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
855   "Face used for highlighting the current article in the summary buffer."
856   :group 'gnus-summary-visual
857   :type 'face)
858
859 (defvar gnus-tmp-downloaded nil)
860
861 (defcustom gnus-summary-highlight
862   '(((eq mark gnus-canceled-mark)
863      . gnus-summary-cancelled-face)
864     ((and uncached (> score default-high))
865      . gnus-summary-high-undownloaded-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-undownloaded-face)
868     (uncached
869      . gnus-summary-normal-undownloaded-face)
870     ((and (> score default-high)
871           (or (eq mark gnus-dormant-mark)
872               (eq mark gnus-ticked-mark)))
873      . gnus-summary-high-ticked-face)
874     ((and (< score default-low)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-low-ticked-face)
878     ((or (eq mark gnus-dormant-mark)
879          (eq mark gnus-ticked-mark))
880      . gnus-summary-normal-ticked-face)
881     ((and (> score default-high) (eq mark gnus-ancient-mark))
882      . gnus-summary-high-ancient-face)
883     ((and (< score default-low) (eq mark gnus-ancient-mark))
884      . gnus-summary-low-ancient-face)
885     ((eq mark gnus-ancient-mark)
886      . gnus-summary-normal-ancient-face)
887     ((and (> score default-high) (eq mark gnus-unread-mark))
888      . gnus-summary-high-unread-face)
889     ((and (< score default-low) (eq mark gnus-unread-mark))
890      . gnus-summary-low-unread-face)
891     ((eq mark gnus-unread-mark)
892      . gnus-summary-normal-unread-face)
893     ((> score default-high)
894      . gnus-summary-high-read-face)
895     ((< score default-low)
896      . gnus-summary-low-read-face)
897     (t
898      . gnus-summary-normal-read-face))
899   "*Controls the highlighting of summary buffer lines.
900
901 A list of (FORM . FACE) pairs.  When deciding how a a particular
902 summary line should be displayed, each form is evaluated.  The content
903 of the face field after the first true form is used.  You can change
904 how those summary lines are displayed, by editing the face field.
905
906 You can use the following variables in the FORM field.
907
908 score:        The article's score
909 default:      The default article score.
910 default-high: The default score for high scored articles.
911 default-low:  The default score for low scored articles.
912 below:        The score below which articles are automatically marked as read.
913 mark:         The articles mark."
914   :group 'gnus-summary-visual
915   :type '(repeat (cons (sexp :tag "Form" nil)
916                        face)))
917
918 (defcustom gnus-alter-header-function nil
919   "Function called to allow alteration of article header structures.
920 The function is called with one parameter, the article header vector,
921 which it may alter in any way.")
922
923 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
924   "Variable that says which function should be used to decode a string with encoded words.")
925
926 (defcustom gnus-extra-headers '(To Newsgroups)
927   "*Extra headers to parse."
928   :version "21.1"
929   :group 'gnus-summary
930   :type '(repeat symbol))
931
932 (defcustom gnus-ignored-from-addresses
933   (and user-mail-address (regexp-quote user-mail-address))
934   "*Regexp of From headers that may be suppressed in favor of To headers."
935   :version "21.1"
936   :group 'gnus-summary
937   :type 'regexp)
938
939 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
940   "List of charsets that should be ignored.
941 When these charsets are used in the \"charset\" parameter, the
942 default charset will be used instead."
943   :version "21.1"
944   :type '(repeat symbol)
945   :group 'gnus-charset)
946
947 (gnus-define-group-parameter
948  ignored-charsets
949  :type list
950  :function-document
951  "Return the ignored charsets of GROUP."
952  :variable gnus-group-ignored-charsets-alist
953  :variable-default
954  '(("alt\\.chinese\\.text" iso-8859-1))
955  :variable-document
956  "Alist of regexps (to match group names) and charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959  :variable-group gnus-charset
960  :variable-type '(repeat (cons (regexp :tag "Group")
961                                (repeat symbol)))
962  :parameter-type '(choice :tag "Ignored charsets"
963                           :value nil
964                           (repeat (symbol)))
965  :parameter-document       "\
966 List of charsets that should be ignored.
967
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead.")
970
971 (defcustom gnus-group-highlight-words-alist nil
972   "Alist of group regexps and highlight regexps.
973 This variable uses the same syntax as `gnus-emphasis-alist'."
974   :version "21.1"
975   :type '(repeat (cons (regexp :tag "Group")
976                        (repeat (list (regexp :tag "Highlight regexp")
977                                      (number :tag "Group for entire word" 0)
978                                      (number :tag "Group for displayed part" 0)
979                                      (symbol :tag "Face"
980                                              gnus-emphasis-highlight-words)))))
981   :group 'gnus-summary-visual)
982
983 (defcustom gnus-summary-show-article-charset-alist
984   nil
985   "Alist of number and charset.
986 The article will be shown with the charset corresponding to the
987 numbered argument.
988 For example: ((1 . cn-gb-2312) (2 . big5))."
989   :version "21.1"
990   :type '(repeat (cons (number :tag "Argument" 1)
991                        (symbol :tag "Charset")))
992   :group 'gnus-charset)
993
994 (defcustom gnus-preserve-marks t
995   "Whether marks are preserved when moving, copying and respooling messages."
996   :version "21.1"
997   :type 'boolean
998   :group 'gnus-summary-marks)
999
1000 (defcustom gnus-alter-articles-to-read-function nil
1001   "Function to be called to alter the list of articles to be selected."
1002   :type '(choice (const nil) function)
1003   :group 'gnus-summary)
1004
1005 (defcustom gnus-orphan-score nil
1006   "*All orphans get this score added.  Set in the score file."
1007   :group 'gnus-score-default
1008   :type '(choice (const nil)
1009                  integer))
1010
1011 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1012   "*A regexp to match MIME parts when saving multiple parts of a message
1013 with gnus-summary-save-parts (X m). This regexp will be used by default
1014 when prompting the user for which type of files to save."
1015   :group 'gnus-summary
1016   :type 'regexp)
1017
1018 (defcustom gnus-read-all-available-headers nil
1019   "Whether Gnus should parse all headers made available to it.
1020 This is mostly relevant for slow backends where the user may
1021 wish to widen the summary buffer to include all headers
1022 that were fetched.  Say, for nnultimate groups."
1023   :group 'gnus-summary
1024   :type '(choice boolean regexp))
1025
1026 (defcustom gnus-summary-muttprint-program "muttprint"
1027   "Command (and optional arguments) used to run Muttprint."
1028   :version "21.3"
1029   :group 'gnus-summary
1030   :type 'string)
1031
1032 (defcustom gnus-article-loose-mime nil
1033   "If non-nil, don't require MIME-Version header.
1034 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1035 supply the MIME-Version header or deliberately strip it From the mail.
1036 Set it to non-nil, Gnus will treat some articles as MIME even if
1037 the MIME-Version header is missed."
1038   :version "21.3"
1039   :type 'boolean
1040   :group 'gnus-article)
1041
1042 ;;; Internal variables
1043
1044 (defvar gnus-summary-display-cache nil)
1045 (defvar gnus-article-mime-handles nil)
1046 (defvar gnus-article-decoded-p nil)
1047 (defvar gnus-article-charset nil)
1048 (defvar gnus-article-ignored-charsets nil)
1049 (defvar gnus-scores-exclude-files nil)
1050 (defvar gnus-page-broken nil)
1051
1052 (defvar gnus-original-article nil)
1053 (defvar gnus-article-internal-prepare-hook nil)
1054 (defvar gnus-newsgroup-process-stack nil)
1055
1056 (defvar gnus-thread-indent-array nil)
1057 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1058 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1059   "Function called to sort the articles within a thread after it has been gathered together.")
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073 (defvar gnus-newsgroup-display nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1087         ?s)
1088     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1099     (?L gnus-tmp-lines ?s)
1100     (?O gnus-tmp-downloaded ?c)
1101     (?I gnus-tmp-indentation ?s)
1102     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1103     (?R gnus-tmp-replied ?c)
1104     (?\[ gnus-tmp-opening-bracket ?c)
1105     (?\] gnus-tmp-closing-bracket ?c)
1106     (?\> (make-string gnus-tmp-level ? ) ?s)
1107     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1108     (?i gnus-tmp-score ?d)
1109     (?z gnus-tmp-score-char ?c)
1110     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1111     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1112     (?U gnus-tmp-unread ?c)
1113     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1114         ?s)
1115     (?t (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1117         ?d)
1118     (?e (gnus-summary-number-of-articles-in-thread
1119          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1120         ?c)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?P (gnus-pick-line-number) ?d)
1123     (?B gnus-tmp-thread-tree-header-string ?s)
1124     (user-date (gnus-user-date
1125                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1126   "An alist of format specifications that can appear in summary lines.
1127 These are paired with what variables they correspond with, along with
1128 the type of the variable (string, integer, character, etc).")
1129
1130 (defvar gnus-summary-dummy-line-format-alist
1131   `((?S gnus-tmp-subject ?s)
1132     (?N gnus-tmp-number ?d)
1133     (?u gnus-tmp-user-defined ?s)))
1134
1135 (defvar gnus-summary-mode-line-format-alist
1136   `((?G gnus-tmp-group-name ?s)
1137     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1138     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1139     (?A gnus-tmp-article-number ?d)
1140     (?Z gnus-tmp-unread-and-unselected ?s)
1141     (?V gnus-version ?s)
1142     (?U gnus-tmp-unread-and-unticked ?d)
1143     (?S gnus-tmp-subject ?s)
1144     (?e gnus-tmp-unselected ?d)
1145     (?u gnus-tmp-user-defined ?s)
1146     (?d (length gnus-newsgroup-dormant) ?d)
1147     (?t (length gnus-newsgroup-marked) ?d)
1148     (?h (length gnus-newsgroup-spam-marked) ?d)
1149     (?r (length gnus-newsgroup-reads) ?d)
1150     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1151     (?E gnus-newsgroup-expunged-tally ?d)
1152     (?s (gnus-current-score-file-nondirectory) ?s)))
1153
1154 (defvar gnus-last-search-regexp nil
1155   "Default regexp for article search command.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-agentized nil
1161   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1162 (defvar gnus-newsgroup-begin nil)
1163 (defvar gnus-newsgroup-end nil)
1164 (defvar gnus-newsgroup-last-rmail nil)
1165 (defvar gnus-newsgroup-last-mail nil)
1166 (defvar gnus-newsgroup-last-folder nil)
1167 (defvar gnus-newsgroup-last-file nil)
1168 (defvar gnus-newsgroup-auto-expire nil)
1169 (defvar gnus-newsgroup-active nil)
1170
1171 (defvar gnus-newsgroup-data nil)
1172 (defvar gnus-newsgroup-data-reverse nil)
1173 (defvar gnus-newsgroup-limit nil)
1174 (defvar gnus-newsgroup-limits nil)
1175
1176 (defvar gnus-newsgroup-unreads nil
1177   "Sorted list of unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-unselected nil
1180   "Sorted list of unselected unread articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-reads nil
1183   "Alist of read articles and article marks in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-expunged-tally nil)
1186
1187 (defvar gnus-newsgroup-marked nil
1188   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1189
1190 (defvar gnus-newsgroup-spam-marked nil
1191   "List of ranges of articles that have been marked as spam.")
1192
1193 (defvar gnus-newsgroup-killed nil
1194   "List of ranges of articles that have been through the scoring process.")
1195
1196 (defvar gnus-newsgroup-cached nil
1197   "Sorted list of articles that come from the article cache.")
1198
1199 (defvar gnus-newsgroup-saved nil
1200   "List of articles that have been saved.")
1201
1202 (defvar gnus-newsgroup-kill-headers nil)
1203
1204 (defvar gnus-newsgroup-replied nil
1205   "List of articles that have been replied to in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-forwarded nil
1208   "List of articles that have been forwarded in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-recent nil
1211   "List of articles that have are recent in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-expirable nil
1214   "Sorted list of articles in the current newsgroup that can be expired.")
1215
1216 (defvar gnus-newsgroup-processable nil
1217   "List of articles in the current newsgroup that can be processed.")
1218
1219 (defvar gnus-newsgroup-downloadable nil
1220   "Sorted list of articles in the current newsgroup that can be processed.")
1221
1222 (defvar gnus-newsgroup-undownloaded nil
1223   "List of articles in the current newsgroup that haven't been downloaded..")
1224
1225 (defvar gnus-newsgroup-unsendable nil
1226   "List of articles in the current newsgroup that won't be sent.")
1227
1228 (defvar gnus-newsgroup-bookmarks nil
1229   "List of articles in the current newsgroup that have bookmarks.")
1230
1231 (defvar gnus-newsgroup-dormant nil
1232   "Sorted list of dormant articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-unseen nil
1235   "List of unseen articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-seen nil
1238   "Range of seen articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-articles nil
1241   "List of articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-scored nil
1244   "List of scored articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-headers nil
1247   "List of article headers in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-threads nil)
1250
1251 (defvar gnus-newsgroup-prepared nil
1252   "Whether the current group has been prepared properly.")
1253
1254 (defvar gnus-newsgroup-ancient nil
1255   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-sparse nil)
1258
1259 (defvar gnus-current-article nil)
1260 (defvar gnus-article-current nil)
1261 (defvar gnus-current-headers nil)
1262 (defvar gnus-have-all-headers nil)
1263 (defvar gnus-last-article nil)
1264 (defvar gnus-newsgroup-history nil)
1265 (defvar gnus-newsgroup-charset nil)
1266 (defvar gnus-newsgroup-ephemeral-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1268
1269 (defvar gnus-article-before-search nil)
1270
1271 (defconst gnus-summary-local-variables
1272   '(gnus-newsgroup-name
1273     gnus-newsgroup-begin gnus-newsgroup-end
1274     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1275     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1276     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1277     gnus-newsgroup-unselected gnus-newsgroup-marked
1278     gnus-newsgroup-spam-marked
1279     gnus-newsgroup-reads gnus-newsgroup-saved
1280     gnus-newsgroup-replied gnus-newsgroup-forwarded
1281     gnus-newsgroup-recent
1282     gnus-newsgroup-expirable
1283     gnus-newsgroup-processable gnus-newsgroup-killed
1284     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1285     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1286     gnus-newsgroup-seen gnus-newsgroup-articles
1287     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1288     gnus-newsgroup-headers gnus-newsgroup-threads
1289     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1290     gnus-current-article gnus-current-headers gnus-have-all-headers
1291     gnus-last-article gnus-article-internal-prepare-hook
1292     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1293     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1294     gnus-thread-expunge-below
1295     gnus-score-alist gnus-current-score-file
1296     (gnus-summary-expunge-below . global)
1297     (gnus-summary-mark-below . global)
1298     (gnus-orphan-score . global)
1299     gnus-newsgroup-active gnus-scores-exclude-files
1300     gnus-newsgroup-history gnus-newsgroup-ancient
1301     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1302     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1303     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1304     (gnus-newsgroup-expunged-tally . 0)
1305     gnus-cache-removable-articles gnus-newsgroup-cached
1306     gnus-newsgroup-data gnus-newsgroup-data-reverse
1307     gnus-newsgroup-limit gnus-newsgroup-limits
1308     gnus-newsgroup-charset gnus-newsgroup-display)
1309   "Variables that are buffer-local to the summary buffers.")
1310
1311 (defvar gnus-newsgroup-variables nil
1312   "A list of variables that have separate values in different newsgroups.
1313 A list of newsgroup (summary buffer) local variables, or cons of
1314 variables and their default values (when the default values are not
1315 nil), that should be made global while the summary buffer is active.
1316 These variables can be used to set variables in the group parameters
1317 while still allowing them to affect operations done in other
1318 buffers. For example:
1319
1320 \(setq gnus-newsgroup-variables
1321      '(message-use-followup-to
1322        (gnus-visible-headers .
1323          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1324 ")
1325
1326 ;; Byte-compiler warning.
1327 ;(eval-when-compile (defvar gnus-article-mode-map))
1328 (eval-when-compile
1329   (let ((features (cons 'gnus-sum features)))
1330     (require 'gnus)
1331     (require 'gnus-agent)
1332     (require 'gnus-art)))
1333
1334 ;; MIME stuff.
1335
1336 (defvar gnus-decode-encoded-word-methods
1337   '(mail-decode-encoded-word-string)
1338   "List of methods used to decode encoded words.
1339
1340 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1341 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1342 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1343 whose names match REGEXP.
1344
1345 For example:
1346 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1347  mail-decode-encoded-word-string
1348  (\"chinese\" . rfc1843-decode-string))")
1349
1350 (defvar gnus-decode-encoded-word-methods-cache nil)
1351
1352 (defun gnus-multi-decode-encoded-word-string (string)
1353   "Apply the functions from `gnus-encoded-word-methods' that match."
1354   (unless (and gnus-decode-encoded-word-methods-cache
1355                (eq gnus-newsgroup-name
1356                    (car gnus-decode-encoded-word-methods-cache)))
1357     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1358     (mapcar (lambda (x)
1359               (if (symbolp x)
1360                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1361                 (if (and gnus-newsgroup-name
1362                          (string-match (car x) gnus-newsgroup-name))
1363                     (nconc gnus-decode-encoded-word-methods-cache
1364                            (list (cdr x))))))
1365             gnus-decode-encoded-word-methods))
1366   (let ((xlist gnus-decode-encoded-word-methods-cache))
1367     (pop xlist)
1368     (while xlist
1369       (setq string (funcall (pop xlist) string))))
1370   string)
1371
1372 ;; Subject simplification.
1373
1374 (defun gnus-simplify-whitespace (str)
1375   "Remove excessive whitespace from STR."
1376   (let ((mystr str))
1377     ;; Multiple spaces.
1378     (while (string-match "[ \t][ \t]+" mystr)
1379       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1380                           " "
1381                           (substring mystr (match-end 0)))))
1382     ;; Leading spaces.
1383     (when (string-match "^[ \t]+" mystr)
1384       (setq mystr (substring mystr (match-end 0))))
1385     ;; Trailing spaces.
1386     (when (string-match "[ \t]+$" mystr)
1387       (setq mystr (substring mystr 0 (match-beginning 0))))
1388     mystr))
1389
1390 (defun gnus-simplify-all-whitespace (str)
1391   "Remove all whitespace from STR."
1392   (let ((mystr str))
1393     (while (string-match "[ \t\n]+" mystr)
1394       (setq mystr (replace-match "" nil nil mystr)))
1395     mystr))
1396
1397 (defsubst gnus-simplify-subject-re (subject)
1398   "Remove \"Re:\" from subject lines."
1399   (if (string-match message-subject-re-regexp subject)
1400       (substring subject (match-end 0))
1401     subject))
1402
1403 (defun gnus-simplify-subject (subject &optional re-only)
1404   "Remove `Re:' and words in parentheses.
1405 If RE-ONLY is non-nil, strip leading `Re:'s only."
1406   (let ((case-fold-search t))           ;Ignore case.
1407     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1408     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove uninteresting prefixes.
1411     (when (and (not re-only)
1412                gnus-simplify-ignored-prefixes
1413                (string-match gnus-simplify-ignored-prefixes subject))
1414       (setq subject (substring subject (match-end 0))))
1415     ;; Remove words in parentheses from end.
1416     (unless re-only
1417       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1418         (setq subject (substring subject 0 (match-beginning 0)))))
1419     ;; Return subject string.
1420     subject))
1421
1422 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1423 ;; all whitespace.
1424 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1425   (goto-char (point-min))
1426   (while (re-search-forward regexp nil t)
1427     (replace-match (or newtext ""))))
1428
1429 (defun gnus-simplify-buffer-fuzzy ()
1430   "Simplify string in the buffer fuzzily.
1431 The string in the accessible portion of the current buffer is simplified.
1432 It is assumed to be a single-line subject.
1433 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1434 matter is removed.  Additional things can be deleted by setting
1435 `gnus-simplify-subject-fuzzy-regexp'."
1436   (let ((case-fold-search t)
1437         (modified-tick))
1438     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1439
1440     (while (not (eq modified-tick (buffer-modified-tick)))
1441       (setq modified-tick (buffer-modified-tick))
1442       (cond
1443        ((listp gnus-simplify-subject-fuzzy-regexp)
1444         (mapcar 'gnus-simplify-buffer-fuzzy-step
1445                 gnus-simplify-subject-fuzzy-regexp))
1446        (gnus-simplify-subject-fuzzy-regexp
1447         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1448       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1449       (gnus-simplify-buffer-fuzzy-step
1450        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1451       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1452
1453     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1454     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1455     (gnus-simplify-buffer-fuzzy-step " $")
1456     (gnus-simplify-buffer-fuzzy-step "^ +")))
1457
1458 (defun gnus-simplify-subject-fuzzy (subject)
1459   "Simplify a subject string fuzzily.
1460 See `gnus-simplify-buffer-fuzzy' for details."
1461   (save-excursion
1462     (gnus-set-work-buffer)
1463     (let ((case-fold-search t))
1464       ;; Remove uninteresting prefixes.
1465       (when (and gnus-simplify-ignored-prefixes
1466                  (string-match gnus-simplify-ignored-prefixes subject))
1467         (setq subject (substring subject (match-end 0))))
1468       (insert subject)
1469       (inline (gnus-simplify-buffer-fuzzy))
1470       (buffer-string))))
1471
1472 (defsubst gnus-simplify-subject-fully (subject)
1473   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1474   (cond
1475    (gnus-simplify-subject-functions
1476     (gnus-map-function gnus-simplify-subject-functions subject))
1477    ((null gnus-summary-gather-subject-limit)
1478     (gnus-simplify-subject-re subject))
1479    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1480     (gnus-simplify-subject-fuzzy subject))
1481    ((numberp gnus-summary-gather-subject-limit)
1482     (gnus-limit-string (gnus-simplify-subject-re subject)
1483                        gnus-summary-gather-subject-limit))
1484    (t
1485     subject)))
1486
1487 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1488   "Check whether two subjects are equal.
1489 If optional argument simple-first is t, first argument is already
1490 simplified."
1491   (cond
1492    ((null simple-first)
1493     (equal (gnus-simplify-subject-fully s1)
1494            (gnus-simplify-subject-fully s2)))
1495    (t
1496     (equal s1
1497            (gnus-simplify-subject-fully s2)))))
1498
1499 (defun gnus-summary-bubble-group ()
1500   "Increase the score of the current group.
1501 This is a handy function to add to `gnus-summary-exit-hook' to
1502 increase the score of each group you read."
1503   (gnus-group-add-score gnus-newsgroup-name))
1504
1505 \f
1506 ;;;
1507 ;;; Gnus summary mode
1508 ;;;
1509
1510 (put 'gnus-summary-mode 'mode-class 'special)
1511
1512 (defvar gnus-article-commands-menu)
1513
1514 (when t
1515   ;; Non-orthogonal keys
1516
1517   (gnus-define-keys gnus-summary-mode-map
1518     " " gnus-summary-next-page
1519     "\177" gnus-summary-prev-page
1520     [delete] gnus-summary-prev-page
1521     [backspace] gnus-summary-prev-page
1522     "\r" gnus-summary-scroll-up
1523     "\M-\r" gnus-summary-scroll-down
1524     "n" gnus-summary-next-unread-article
1525     "p" gnus-summary-prev-unread-article
1526     "N" gnus-summary-next-article
1527     "P" gnus-summary-prev-article
1528     "\M-\C-n" gnus-summary-next-same-subject
1529     "\M-\C-p" gnus-summary-prev-same-subject
1530     "\M-n" gnus-summary-next-unread-subject
1531     "\M-p" gnus-summary-prev-unread-subject
1532     "." gnus-summary-first-unread-article
1533     "," gnus-summary-best-unread-article
1534     "\M-s" gnus-summary-search-article-forward
1535     "\M-r" gnus-summary-search-article-backward
1536     "<" gnus-summary-beginning-of-article
1537     ">" gnus-summary-end-of-article
1538     "j" gnus-summary-goto-article
1539     "^" gnus-summary-refer-parent-article
1540     "\M-^" gnus-summary-refer-article
1541     "u" gnus-summary-tick-article-forward
1542     "!" gnus-summary-tick-article-forward
1543     "U" gnus-summary-tick-article-backward
1544     "d" gnus-summary-mark-as-read-forward
1545     "D" gnus-summary-mark-as-read-backward
1546     "E" gnus-summary-mark-as-expirable
1547     "\M-u" gnus-summary-clear-mark-forward
1548     "\M-U" gnus-summary-clear-mark-backward
1549     "k" gnus-summary-kill-same-subject-and-select
1550     "\C-k" gnus-summary-kill-same-subject
1551     "\M-\C-k" gnus-summary-kill-thread
1552     "\M-\C-l" gnus-summary-lower-thread
1553     "e" gnus-summary-edit-article
1554     "#" gnus-summary-mark-as-processable
1555     "\M-#" gnus-summary-unmark-as-processable
1556     "\M-\C-t" gnus-summary-toggle-threads
1557     "\M-\C-s" gnus-summary-show-thread
1558     "\M-\C-h" gnus-summary-hide-thread
1559     "\M-\C-f" gnus-summary-next-thread
1560     "\M-\C-b" gnus-summary-prev-thread
1561     [(meta down)] gnus-summary-next-thread
1562     [(meta up)] gnus-summary-prev-thread
1563     "\M-\C-u" gnus-summary-up-thread
1564     "\M-\C-d" gnus-summary-down-thread
1565     "&" gnus-summary-execute-command
1566     "c" gnus-summary-catchup-and-exit
1567     "\C-w" gnus-summary-mark-region-as-read
1568     "\C-t" gnus-summary-toggle-truncation
1569     "?" gnus-summary-mark-as-dormant
1570     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1571     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1572     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1573     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1574     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1575     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1576     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1577     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1578     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1579     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1580     "=" gnus-summary-expand-window
1581     "\C-x\C-s" gnus-summary-reselect-current-group
1582     "\M-g" gnus-summary-rescan-group
1583     "w" gnus-summary-stop-page-breaking
1584     "\C-c\C-r" gnus-summary-caesar-message
1585     "f" gnus-summary-followup
1586     "F" gnus-summary-followup-with-original
1587     "C" gnus-summary-cancel-article
1588     "r" gnus-summary-reply
1589     "R" gnus-summary-reply-with-original
1590     "\C-c\C-f" gnus-summary-mail-forward
1591     "o" gnus-summary-save-article
1592     "\C-o" gnus-summary-save-article-mail
1593     "|" gnus-summary-pipe-output
1594     "\M-k" gnus-summary-edit-local-kill
1595     "\M-K" gnus-summary-edit-global-kill
1596     ;; "V" gnus-version
1597     "\C-c\C-d" gnus-summary-describe-group
1598     "q" gnus-summary-exit
1599     "Q" gnus-summary-exit-no-update
1600     "\C-c\C-i" gnus-info-find-node
1601     gnus-mouse-2 gnus-mouse-pick-article
1602     "m" gnus-summary-mail-other-window
1603     "a" gnus-summary-post-news
1604     "i" gnus-summary-news-other-window
1605     "x" gnus-summary-limit-to-unread
1606     "s" gnus-summary-isearch-article
1607     "t" gnus-summary-toggle-header
1608     "g" gnus-summary-show-article
1609     "l" gnus-summary-goto-last-article
1610     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1611     "\C-d" gnus-summary-enter-digest-group
1612     "\M-\C-d" gnus-summary-read-document
1613     "\M-\C-e" gnus-summary-edit-parameters
1614     "\M-\C-a" gnus-summary-customize-parameters
1615     "\C-c\C-b" gnus-bug
1616     "*" gnus-cache-enter-article
1617     "\M-*" gnus-cache-remove-article
1618     "\M-&" gnus-summary-universal-argument
1619     "\C-l" gnus-recenter
1620     "I" gnus-summary-increase-score
1621     "L" gnus-summary-lower-score
1622     "\M-i" gnus-symbolic-argument
1623     "h" gnus-summary-select-article-buffer
1624
1625     "b" gnus-article-view-part
1626     "\M-t" gnus-summary-toggle-display-buttonized
1627
1628     "V" gnus-summary-score-map
1629     "X" gnus-uu-extract-map
1630     "S" gnus-summary-send-map)
1631
1632   ;; Sort of orthogonal keymap
1633   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1634     "t" gnus-summary-tick-article-forward
1635     "!" gnus-summary-tick-article-forward
1636     "d" gnus-summary-mark-as-read-forward
1637     "r" gnus-summary-mark-as-read-forward
1638     "c" gnus-summary-clear-mark-forward
1639     " " gnus-summary-clear-mark-forward
1640     "e" gnus-summary-mark-as-expirable
1641     "x" gnus-summary-mark-as-expirable
1642     "?" gnus-summary-mark-as-dormant
1643     "b" gnus-summary-set-bookmark
1644     "B" gnus-summary-remove-bookmark
1645     "#" gnus-summary-mark-as-processable
1646     "\M-#" gnus-summary-unmark-as-processable
1647     "S" gnus-summary-limit-include-expunged
1648     "C" gnus-summary-catchup
1649     "H" gnus-summary-catchup-to-here
1650     "h" gnus-summary-catchup-from-here
1651     "\C-c" gnus-summary-catchup-all
1652     "k" gnus-summary-kill-same-subject-and-select
1653     "K" gnus-summary-kill-same-subject
1654     "P" gnus-uu-mark-map)
1655
1656   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1657     "c" gnus-summary-clear-above
1658     "u" gnus-summary-tick-above
1659     "m" gnus-summary-mark-above
1660     "k" gnus-summary-kill-below)
1661
1662   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1663     "/" gnus-summary-limit-to-subject
1664     "n" gnus-summary-limit-to-articles
1665     "w" gnus-summary-pop-limit
1666     "s" gnus-summary-limit-to-subject
1667     "a" gnus-summary-limit-to-author
1668     "u" gnus-summary-limit-to-unread
1669     "m" gnus-summary-limit-to-marks
1670     "M" gnus-summary-limit-exclude-marks
1671     "v" gnus-summary-limit-to-score
1672     "*" gnus-summary-limit-include-cached
1673     "D" gnus-summary-limit-include-dormant
1674     "T" gnus-summary-limit-include-thread
1675     "d" gnus-summary-limit-exclude-dormant
1676     "t" gnus-summary-limit-to-age
1677     "." gnus-summary-limit-to-unseen
1678     "x" gnus-summary-limit-to-extra
1679     "p" gnus-summary-limit-to-display-predicate
1680     "E" gnus-summary-limit-include-expunged
1681     "c" gnus-summary-limit-exclude-childless-dormant
1682     "C" gnus-summary-limit-mark-excluded-as-read
1683     "o" gnus-summary-insert-old-articles
1684     "N" gnus-summary-insert-new-articles)
1685
1686   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1687     "n" gnus-summary-next-unread-article
1688     "p" gnus-summary-prev-unread-article
1689     "N" gnus-summary-next-article
1690     "P" gnus-summary-prev-article
1691     "\C-n" gnus-summary-next-same-subject
1692     "\C-p" gnus-summary-prev-same-subject
1693     "\M-n" gnus-summary-next-unread-subject
1694     "\M-p" gnus-summary-prev-unread-subject
1695     "f" gnus-summary-first-unread-article
1696     "b" gnus-summary-best-unread-article
1697     "j" gnus-summary-goto-article
1698     "g" gnus-summary-goto-subject
1699     "l" gnus-summary-goto-last-article
1700     "o" gnus-summary-pop-article)
1701
1702   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1703     "k" gnus-summary-kill-thread
1704     "l" gnus-summary-lower-thread
1705     "i" gnus-summary-raise-thread
1706     "T" gnus-summary-toggle-threads
1707     "t" gnus-summary-rethread-current
1708     "^" gnus-summary-reparent-thread
1709     "s" gnus-summary-show-thread
1710     "S" gnus-summary-show-all-threads
1711     "h" gnus-summary-hide-thread
1712     "H" gnus-summary-hide-all-threads
1713     "n" gnus-summary-next-thread
1714     "p" gnus-summary-prev-thread
1715     "u" gnus-summary-up-thread
1716     "o" gnus-summary-top-thread
1717     "d" gnus-summary-down-thread
1718     "#" gnus-uu-mark-thread
1719     "\M-#" gnus-uu-unmark-thread)
1720
1721   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1722     "g" gnus-summary-prepare
1723     "c" gnus-summary-insert-cached-articles
1724     "d" gnus-summary-insert-dormant-articles)
1725
1726   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1727     "c" gnus-summary-catchup-and-exit
1728     "C" gnus-summary-catchup-all-and-exit
1729     "E" gnus-summary-exit-no-update
1730     "Q" gnus-summary-exit
1731     "Z" gnus-summary-exit
1732     "n" gnus-summary-catchup-and-goto-next-group
1733     "R" gnus-summary-reselect-current-group
1734     "G" gnus-summary-rescan-group
1735     "N" gnus-summary-next-group
1736     "s" gnus-summary-save-newsrc
1737     "P" gnus-summary-prev-group)
1738
1739   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1740     " " gnus-summary-next-page
1741     "n" gnus-summary-next-page
1742     "\177" gnus-summary-prev-page
1743     [delete] gnus-summary-prev-page
1744     "p" gnus-summary-prev-page
1745     "\r" gnus-summary-scroll-up
1746     "\M-\r" gnus-summary-scroll-down
1747     "<" gnus-summary-beginning-of-article
1748     ">" gnus-summary-end-of-article
1749     "b" gnus-summary-beginning-of-article
1750     "e" gnus-summary-end-of-article
1751     "^" gnus-summary-refer-parent-article
1752     "r" gnus-summary-refer-parent-article
1753     "D" gnus-summary-enter-digest-group
1754     "R" gnus-summary-refer-references
1755     "T" gnus-summary-refer-thread
1756     "g" gnus-summary-show-article
1757     "s" gnus-summary-isearch-article
1758     "P" gnus-summary-print-article
1759     "M" gnus-mailing-list-insinuate
1760     "t" gnus-article-babel)
1761
1762   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1763     "b" gnus-article-add-buttons
1764     "B" gnus-article-add-buttons-to-head
1765     "o" gnus-article-treat-overstrike
1766     "e" gnus-article-emphasize
1767     "w" gnus-article-fill-cited-article
1768     "Q" gnus-article-fill-long-lines
1769     "C" gnus-article-capitalize-sentences
1770     "c" gnus-article-remove-cr
1771     "q" gnus-article-de-quoted-unreadable
1772     "6" gnus-article-de-base64-unreadable
1773     "Z" gnus-article-decode-HZ
1774     "h" gnus-article-wash-html
1775     "u" gnus-article-unsplit-urls
1776     "s" gnus-summary-force-verify-and-decrypt
1777     "f" gnus-article-display-x-face
1778     "l" gnus-summary-stop-page-breaking
1779     "r" gnus-summary-caesar-message
1780     "m" gnus-summary-morse-message
1781     "t" gnus-summary-toggle-header
1782     "g" gnus-treat-smiley
1783     "v" gnus-summary-verbose-headers
1784     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1785     "p" gnus-article-verify-x-pgp-sig
1786     "d" gnus-article-treat-dumbquotes
1787     "k" gnus-article-outlook-deuglify-article)
1788
1789   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1790     "a" gnus-article-hide
1791     "h" gnus-article-hide-headers
1792     "b" gnus-article-hide-boring-headers
1793     "s" gnus-article-hide-signature
1794     "c" gnus-article-hide-citation
1795     "C" gnus-article-hide-citation-in-followups
1796     "l" gnus-article-hide-list-identifiers
1797     "p" gnus-article-hide-pgp
1798     "B" gnus-article-strip-banner
1799     "P" gnus-article-hide-pem
1800     "\C-c" gnus-article-hide-citation-maybe)
1801
1802   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1803     "a" gnus-article-highlight
1804     "h" gnus-article-highlight-headers
1805     "c" gnus-article-highlight-citation
1806     "s" gnus-article-highlight-signature)
1807
1808   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1809     "f" gnus-article-treat-fold-headers
1810     "u" gnus-article-treat-unfold-headers
1811     "n" gnus-article-treat-fold-newsgroups)
1812
1813   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1814     "x" gnus-article-display-x-face
1815     "s" gnus-treat-smiley
1816     "D" gnus-article-remove-images
1817     "f" gnus-treat-from-picon
1818     "m" gnus-treat-mail-picon
1819     "n" gnus-treat-newsgroups-picon)
1820
1821   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1822     "w" gnus-article-decode-mime-words
1823     "c" gnus-article-decode-charset
1824     "v" gnus-mime-view-all-parts
1825     "b" gnus-article-view-part)
1826
1827   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1828     "z" gnus-article-date-ut
1829     "u" gnus-article-date-ut
1830     "l" gnus-article-date-local
1831     "p" gnus-article-date-english
1832     "e" gnus-article-date-lapsed
1833     "o" gnus-article-date-original
1834     "i" gnus-article-date-iso8601
1835     "s" gnus-article-date-user)
1836
1837   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1838     "t" gnus-article-remove-trailing-blank-lines
1839     "l" gnus-article-strip-leading-blank-lines
1840     "m" gnus-article-strip-multiple-blank-lines
1841     "a" gnus-article-strip-blank-lines
1842     "A" gnus-article-strip-all-blank-lines
1843     "s" gnus-article-strip-leading-space
1844     "e" gnus-article-strip-trailing-space
1845     "w" gnus-article-remove-leading-whitespace)
1846
1847   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1848     "v" gnus-version
1849     "f" gnus-summary-fetch-faq
1850     "d" gnus-summary-describe-group
1851     "h" gnus-summary-describe-briefly
1852     "i" gnus-info-find-node
1853     "c" gnus-group-fetch-charter
1854     "C" gnus-group-fetch-control)
1855
1856   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1857     "e" gnus-summary-expire-articles
1858     "\M-\C-e" gnus-summary-expire-articles-now
1859     "\177" gnus-summary-delete-article
1860     [delete] gnus-summary-delete-article
1861     [backspace] gnus-summary-delete-article
1862     "m" gnus-summary-move-article
1863     "r" gnus-summary-respool-article
1864     "w" gnus-summary-edit-article
1865     "c" gnus-summary-copy-article
1866     "B" gnus-summary-crosspost-article
1867     "q" gnus-summary-respool-query
1868     "t" gnus-summary-respool-trace
1869     "i" gnus-summary-import-article
1870     "I" gnus-summary-create-article
1871     "p" gnus-summary-article-posted-p)
1872
1873   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1874     "o" gnus-summary-save-article
1875     "m" gnus-summary-save-article-mail
1876     "F" gnus-summary-write-article-file
1877     "r" gnus-summary-save-article-rmail
1878     "f" gnus-summary-save-article-file
1879     "b" gnus-summary-save-article-body-file
1880     "h" gnus-summary-save-article-folder
1881     "v" gnus-summary-save-article-vm
1882     "p" gnus-summary-pipe-output
1883     "P" gnus-summary-muttprint
1884     "s" gnus-soup-add-article)
1885
1886   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1887     "b" gnus-summary-display-buttonized
1888     "m" gnus-summary-repair-multipart
1889     "v" gnus-article-view-part
1890     "o" gnus-article-save-part
1891     "c" gnus-article-copy-part
1892     "C" gnus-article-view-part-as-charset
1893     "e" gnus-article-view-part-externally
1894     "E" gnus-article-encrypt-body
1895     "i" gnus-article-inline-part
1896     "|" gnus-article-pipe-part)
1897
1898   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1899     "p" gnus-summary-mark-as-processable
1900     "u" gnus-summary-unmark-as-processable
1901     "U" gnus-summary-unmark-all-processable
1902     "v" gnus-uu-mark-over
1903     "s" gnus-uu-mark-series
1904     "r" gnus-uu-mark-region
1905     "g" gnus-uu-unmark-region
1906     "R" gnus-uu-mark-by-regexp
1907     "G" gnus-uu-unmark-by-regexp
1908     "t" gnus-uu-mark-thread
1909     "T" gnus-uu-unmark-thread
1910     "a" gnus-uu-mark-all
1911     "b" gnus-uu-mark-buffer
1912     "S" gnus-uu-mark-sparse
1913     "k" gnus-summary-kill-process-mark
1914     "y" gnus-summary-yank-process-mark
1915     "w" gnus-summary-save-process-mark
1916     "i" gnus-uu-invert-processable)
1917
1918   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1919     ;;"x" gnus-uu-extract-any
1920     "m" gnus-summary-save-parts
1921     "u" gnus-uu-decode-uu
1922     "U" gnus-uu-decode-uu-and-save
1923     "s" gnus-uu-decode-unshar
1924     "S" gnus-uu-decode-unshar-and-save
1925     "o" gnus-uu-decode-save
1926     "O" gnus-uu-decode-save
1927     "b" gnus-uu-decode-binhex
1928     "B" gnus-uu-decode-binhex
1929     "p" gnus-uu-decode-postscript
1930     "P" gnus-uu-decode-postscript-and-save)
1931
1932   (gnus-define-keys
1933       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1934     "u" gnus-uu-decode-uu-view
1935     "U" gnus-uu-decode-uu-and-save-view
1936     "s" gnus-uu-decode-unshar-view
1937     "S" gnus-uu-decode-unshar-and-save-view
1938     "o" gnus-uu-decode-save-view
1939     "O" gnus-uu-decode-save-view
1940     "b" gnus-uu-decode-binhex-view
1941     "B" gnus-uu-decode-binhex-view
1942     "p" gnus-uu-decode-postscript-view
1943     "P" gnus-uu-decode-postscript-and-save-view))
1944
1945 (defvar gnus-article-post-menu nil)
1946
1947 (defconst gnus-summary-menu-maxlen 20)
1948
1949 (defun gnus-summary-menu-split (menu)
1950   ;; If we have lots of elements, divide them into groups of 20
1951   ;; and make a pane (or submenu) for each one.
1952   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1953       (let ((menu menu) sublists next
1954             (i 1))
1955         (while menu
1956           ;; Pull off the next gnus-summary-menu-maxlen elements
1957           ;; and make them the next element of sublist.
1958           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1959           (if next
1960               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1961                       nil))
1962           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1963                                              (aref (car (last menu)) 0)) menu)
1964                                sublists))
1965           (setq i (1+ i))
1966           (setq menu next))
1967         (nreverse sublists))
1968     ;; Few elements--put them all in one pane.
1969     menu))
1970
1971 (defun gnus-summary-make-menu-bar ()
1972   (gnus-turn-off-edit-menu 'summary)
1973
1974   (unless (boundp 'gnus-summary-misc-menu)
1975
1976     (easy-menu-define
1977       gnus-summary-kill-menu gnus-summary-mode-map ""
1978       (cons
1979        "Score"
1980        (nconc
1981         (list
1982          ["Customize" gnus-score-customize t])
1983         (gnus-make-score-map 'increase)
1984         (gnus-make-score-map 'lower)
1985         '(("Mark"
1986            ["Kill below" gnus-summary-kill-below t]
1987            ["Mark above" gnus-summary-mark-above t]
1988            ["Tick above" gnus-summary-tick-above t]
1989            ["Clear above" gnus-summary-clear-above t])
1990           ["Current score" gnus-summary-current-score t]
1991           ["Set score" gnus-summary-set-score t]
1992           ["Switch current score file..." gnus-score-change-score-file t]
1993           ["Set mark below..." gnus-score-set-mark-below t]
1994           ["Set expunge below..." gnus-score-set-expunge-below t]
1995           ["Edit current score file" gnus-score-edit-current-scores t]
1996           ["Edit score file" gnus-score-edit-file t]
1997           ["Trace score" gnus-score-find-trace t]
1998           ["Find words" gnus-score-find-favourite-words t]
1999           ["Rescore buffer" gnus-summary-rescore t]
2000           ["Increase score..." gnus-summary-increase-score t]
2001           ["Lower score..." gnus-summary-lower-score t]))))
2002
2003     ;; Define both the Article menu in the summary buffer and the
2004     ;; equivalent Commands menu in the article buffer here for
2005     ;; consistency.
2006     (let ((innards
2007            `(("Hide"
2008               ["All" gnus-article-hide t]
2009               ["Headers" gnus-article-hide-headers t]
2010               ["Signature" gnus-article-hide-signature t]
2011               ["Citation" gnus-article-hide-citation t]
2012               ["List identifiers" gnus-article-hide-list-identifiers t]
2013               ["PGP" gnus-article-hide-pgp t]
2014               ["Banner" gnus-article-strip-banner t]
2015               ["Boring headers" gnus-article-hide-boring-headers t])
2016              ("Highlight"
2017               ["All" gnus-article-highlight t]
2018               ["Headers" gnus-article-highlight-headers t]
2019               ["Signature" gnus-article-highlight-signature t]
2020               ["Citation" gnus-article-highlight-citation t])
2021              ("MIME"
2022               ["Words" gnus-article-decode-mime-words t]
2023               ["Charset" gnus-article-decode-charset t]
2024               ["QP" gnus-article-de-quoted-unreadable t]
2025               ["Base64" gnus-article-de-base64-unreadable t]
2026               ["View MIME buttons" gnus-summary-display-buttonized t]
2027               ["View all" gnus-mime-view-all-parts t]
2028               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2029               ["Encrypt body" gnus-article-encrypt-body t]
2030               ["Extract all parts" gnus-summary-save-parts t])
2031              ("Date"
2032               ["Local" gnus-article-date-local t]
2033               ["ISO8601" gnus-article-date-iso8601 t]
2034               ["UT" gnus-article-date-ut t]
2035               ["Original" gnus-article-date-original t]
2036               ["Lapsed" gnus-article-date-lapsed t]
2037               ["User-defined" gnus-article-date-user t])
2038              ("Display"
2039               ["Remove images" gnus-article-remove-images t]
2040               ["Toggle smiley" gnus-treat-smiley t]
2041               ["Show X-Face" gnus-article-display-x-face t]
2042               ["Show picons in From" gnus-treat-from-picon t]
2043               ["Show picons in mail headers" gnus-treat-mail-picon t]
2044               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2045               ("View as different encoding"
2046                ,@(gnus-summary-menu-split
2047                   (mapcar
2048                    (lambda (cs)
2049                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2050                      ;; forms for menu commands, we should provide intern'ed
2051                      ;; function symbols.
2052                      (let ((command (intern (format "\
2053 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2054                        (fset command
2055                              `(lambda ()
2056                                 (interactive)
2057                                 (let ((gnus-summary-show-article-charset-alist
2058                                        '((1 . ,cs))))
2059                                   (gnus-summary-show-article 1))))
2060                        `[,(symbol-name cs) ,command t]))
2061                    (sort (if (fboundp 'coding-system-list)
2062                              (coding-system-list)
2063                            (mapcar 'car mm-mime-mule-charset-alist))
2064                          'string<)))))
2065              ("Washing"
2066               ("Remove Blanks"
2067                ["Leading" gnus-article-strip-leading-blank-lines t]
2068                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2069                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2070                ["All of the above" gnus-article-strip-blank-lines t]
2071                ["All" gnus-article-strip-all-blank-lines t]
2072                ["Leading space" gnus-article-strip-leading-space t]
2073                ["Trailing space" gnus-article-strip-trailing-space t]
2074                ["Leading space in headers"
2075                 gnus-article-remove-leading-whitespace t])
2076               ["Overstrike" gnus-article-treat-overstrike t]
2077               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2078               ["Emphasis" gnus-article-emphasize t]
2079               ["Word wrap" gnus-article-fill-cited-article t]
2080               ["Fill long lines" gnus-article-fill-long-lines t]
2081               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2082               ["CR" gnus-article-remove-cr t]
2083               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2084               ["Base64" gnus-article-de-base64-unreadable t]
2085               ["Rot 13" gnus-summary-caesar-message
2086                ,@(if (featurep 'xemacs) '(t)
2087                    '(:help "\"Caesar rotate\" article by 13"))]
2088               ["Morse decode" gnus-summary-morse-message t]
2089               ["Unix pipe..." gnus-summary-pipe-message t]
2090               ["Add buttons" gnus-article-add-buttons t]
2091               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2092               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2093               ["Verbose header" gnus-summary-verbose-headers t]
2094               ["Toggle header" gnus-summary-toggle-header t]
2095               ["Unfold headers" gnus-article-treat-unfold-headers t]
2096               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2097               ["Html" gnus-article-wash-html t]
2098               ["URLs" gnus-article-unsplit-urls t]
2099               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2100               ["HZ" gnus-article-decode-HZ t]
2101               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2102               )
2103              ("Output"
2104               ["Save in default format" gnus-summary-save-article
2105                ,@(if (featurep 'xemacs) '(t)
2106                    '(:help "Save article using default method"))]
2107               ["Save in file" gnus-summary-save-article-file
2108                ,@(if (featurep 'xemacs) '(t)
2109                    '(:help "Save article in file"))]
2110               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2111               ["Save in MH folder" gnus-summary-save-article-folder t]
2112               ["Save in VM folder" gnus-summary-save-article-vm t]
2113               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2114               ["Save body in file" gnus-summary-save-article-body-file t]
2115               ["Pipe through a filter" gnus-summary-pipe-output t]
2116               ["Add to SOUP packet" gnus-soup-add-article t]
2117               ["Print with Muttprint" gnus-summary-muttprint t]
2118               ["Print" gnus-summary-print-article t])
2119              ("Backend"
2120               ["Respool article..." gnus-summary-respool-article t]
2121               ["Move article..." gnus-summary-move-article
2122                (gnus-check-backend-function
2123                 'request-move-article gnus-newsgroup-name)]
2124               ["Copy article..." gnus-summary-copy-article t]
2125               ["Crosspost article..." gnus-summary-crosspost-article
2126                (gnus-check-backend-function
2127                 'request-replace-article gnus-newsgroup-name)]
2128               ["Import file..." gnus-summary-import-article t]
2129               ["Create article..." gnus-summary-create-article t]
2130               ["Check if posted" gnus-summary-article-posted-p t]
2131               ["Edit article" gnus-summary-edit-article
2132                (not (gnus-group-read-only-p))]
2133               ["Delete article" gnus-summary-delete-article
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)]
2136               ["Query respool" gnus-summary-respool-query t]
2137               ["Trace respool" gnus-summary-respool-trace t]
2138               ["Delete expirable articles" gnus-summary-expire-articles-now
2139                (gnus-check-backend-function
2140                 'request-expire-articles gnus-newsgroup-name)])
2141              ("Extract"
2142               ["Uudecode" gnus-uu-decode-uu
2143                ,@(if (featurep 'xemacs) '(t)
2144                    '(:help "Decode uuencoded article(s)"))]
2145               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2146               ["Unshar" gnus-uu-decode-unshar t]
2147               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2148               ["Save" gnus-uu-decode-save t]
2149               ["Binhex" gnus-uu-decode-binhex t]
2150               ["Postscript" gnus-uu-decode-postscript t]
2151               ["All MIME parts" gnus-summary-save-parts t])
2152              ("Cache"
2153               ["Enter article" gnus-cache-enter-article t]
2154               ["Remove article" gnus-cache-remove-article t])
2155              ["Translate" gnus-article-babel t]
2156              ["Select article buffer" gnus-summary-select-article-buffer t]
2157              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2158              ["Isearch article..." gnus-summary-isearch-article t]
2159              ["Beginning of the article" gnus-summary-beginning-of-article t]
2160              ["End of the article" gnus-summary-end-of-article t]
2161              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2162              ["Fetch referenced articles" gnus-summary-refer-references t]
2163              ["Fetch current thread" gnus-summary-refer-thread t]
2164              ["Fetch article with id..." gnus-summary-refer-article t]
2165              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2166              ["Redisplay" gnus-summary-show-article t]
2167              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2168       (easy-menu-define
2169         gnus-summary-article-menu gnus-summary-mode-map ""
2170         (cons "Article" innards))
2171
2172       (if (not (keymapp gnus-summary-article-menu))
2173           (easy-menu-define
2174             gnus-article-commands-menu gnus-article-mode-map ""
2175             (cons "Commands" innards))
2176         ;; in Emacs, don't share menu.
2177         (setq gnus-article-commands-menu
2178               (copy-keymap gnus-summary-article-menu))
2179         (define-key gnus-article-mode-map [menu-bar commands]
2180           (cons "Commands" gnus-article-commands-menu))))
2181
2182     (easy-menu-define
2183       gnus-summary-thread-menu gnus-summary-mode-map ""
2184       '("Threads"
2185         ["Find all messages in thread" gnus-summary-refer-thread t]
2186         ["Toggle threading" gnus-summary-toggle-threads t]
2187         ["Hide threads" gnus-summary-hide-all-threads t]
2188         ["Show threads" gnus-summary-show-all-threads t]
2189         ["Hide thread" gnus-summary-hide-thread t]
2190         ["Show thread" gnus-summary-show-thread t]
2191         ["Go to next thread" gnus-summary-next-thread t]
2192         ["Go to previous thread" gnus-summary-prev-thread t]
2193         ["Go down thread" gnus-summary-down-thread t]
2194         ["Go up thread" gnus-summary-up-thread t]
2195         ["Top of thread" gnus-summary-top-thread t]
2196         ["Mark thread as read" gnus-summary-kill-thread t]
2197         ["Lower thread score" gnus-summary-lower-thread t]
2198         ["Raise thread score" gnus-summary-raise-thread t]
2199         ["Rethread current" gnus-summary-rethread-current t]))
2200
2201     (easy-menu-define
2202       gnus-summary-post-menu gnus-summary-mode-map ""
2203       `("Post"
2204         ["Send a message (mail or news)" gnus-summary-post-news
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post an article"))]
2207         ["Followup" gnus-summary-followup
2208          ,@(if (featurep 'xemacs) '(t)
2209              '(:help "Post followup to this article"))]
2210         ["Followup and yank" gnus-summary-followup-with-original
2211          ,@(if (featurep 'xemacs) '(t)
2212              '(:help "Post followup to this article, quoting its contents"))]
2213         ["Supersede article" gnus-summary-supersede-article t]
2214         ["Cancel article" gnus-summary-cancel-article
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Cancel an article you posted"))]
2217         ["Reply" gnus-summary-reply t]
2218         ["Reply and yank" gnus-summary-reply-with-original t]
2219         ["Wide reply" gnus-summary-wide-reply t]
2220         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2221          ,@(if (featurep 'xemacs) '(t)
2222              '(:help "Mail a reply, quoting this article"))]
2223         ["Very wide reply" gnus-summary-very-wide-reply t]
2224         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2225          ,@(if (featurep 'xemacs) '(t)
2226              '(:help "Mail a very wide reply, quoting this article"))]
2227         ["Mail forward" gnus-summary-mail-forward t]
2228         ["Post forward" gnus-summary-post-forward t]
2229         ["Digest and mail" gnus-uu-digest-mail-forward t]
2230         ["Digest and post" gnus-uu-digest-post-forward t]
2231         ["Resend message" gnus-summary-resend-message t]
2232         ["Resend message edit" gnus-summary-resend-message-edit t]
2233         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2234         ["Send a mail" gnus-summary-mail-other-window t]
2235         ["Create a local message" gnus-summary-news-other-window t]
2236         ["Uuencode and post" gnus-uu-post-news
2237          ,@(if (featurep 'xemacs) '(t)
2238              '(:help "Post a uuencoded article"))]
2239         ["Followup via news" gnus-summary-followup-to-mail t]
2240         ["Followup via news and yank"
2241          gnus-summary-followup-to-mail-with-original t]
2242         ;;("Draft"
2243         ;;["Send" gnus-summary-send-draft t]
2244         ;;["Send bounced" gnus-resend-bounced-mail t])
2245         ))
2246
2247     (cond
2248      ((not (keymapp gnus-summary-post-menu))
2249       (setq gnus-article-post-menu gnus-summary-post-menu))
2250      ((not gnus-article-post-menu)
2251       ;; Don't share post menu.
2252       (setq gnus-article-post-menu
2253             (copy-keymap gnus-summary-post-menu))))
2254     (define-key gnus-article-mode-map [menu-bar post]
2255       (cons "Post" gnus-article-post-menu))
2256
2257     (easy-menu-define
2258       gnus-summary-misc-menu gnus-summary-mode-map ""
2259       `("Gnus"
2260         ("Mark Read"
2261          ["Mark as read" gnus-summary-mark-as-read-forward t]
2262          ["Mark same subject and select"
2263           gnus-summary-kill-same-subject-and-select t]
2264          ["Mark same subject" gnus-summary-kill-same-subject t]
2265          ["Catchup" gnus-summary-catchup
2266           ,@(if (featurep 'xemacs) '(t)
2267               '(:help "Mark unread articles in this group as read"))]
2268          ["Catchup all" gnus-summary-catchup-all t]
2269          ["Catchup to here" gnus-summary-catchup-to-here t]
2270          ["Catchup from here" gnus-summary-catchup-from-here t]
2271          ["Catchup region" gnus-summary-mark-region-as-read t]
2272          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2273         ("Mark Various"
2274          ["Tick" gnus-summary-tick-article-forward t]
2275          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2276          ["Remove marks" gnus-summary-clear-mark-forward t]
2277          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2278          ["Set bookmark" gnus-summary-set-bookmark t]
2279          ["Remove bookmark" gnus-summary-remove-bookmark t])
2280         ("Limit to"
2281          ["Marks..." gnus-summary-limit-to-marks t]
2282          ["Subject..." gnus-summary-limit-to-subject t]
2283          ["Author..." gnus-summary-limit-to-author t]
2284          ["Age..." gnus-summary-limit-to-age t]
2285          ["Extra..." gnus-summary-limit-to-extra t]
2286          ["Score..." gnus-summary-limit-to-score t]
2287          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2288          ["Unread" gnus-summary-limit-to-unread t]
2289          ["Unseen" gnus-summary-limit-to-unseen t]
2290          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2291          ["Next articles" gnus-summary-limit-to-articles t]
2292          ["Pop limit" gnus-summary-pop-limit t]
2293          ["Show dormant" gnus-summary-limit-include-dormant t]
2294          ["Hide childless dormant"
2295           gnus-summary-limit-exclude-childless-dormant t]
2296          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2297          ["Hide marked" gnus-summary-limit-exclude-marks t]
2298          ["Show expunged" gnus-summary-limit-include-expunged t])
2299         ("Process Mark"
2300          ["Set mark" gnus-summary-mark-as-processable t]
2301          ["Remove mark" gnus-summary-unmark-as-processable t]
2302          ["Remove all marks" gnus-summary-unmark-all-processable t]
2303          ["Mark above" gnus-uu-mark-over t]
2304          ["Mark series" gnus-uu-mark-series t]
2305          ["Mark region" gnus-uu-mark-region t]
2306          ["Unmark region" gnus-uu-unmark-region t]
2307          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2308          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2309          ["Mark all" gnus-uu-mark-all t]
2310          ["Mark buffer" gnus-uu-mark-buffer t]
2311          ["Mark sparse" gnus-uu-mark-sparse t]
2312          ["Mark thread" gnus-uu-mark-thread t]
2313          ["Unmark thread" gnus-uu-unmark-thread t]
2314          ("Process Mark Sets"
2315           ["Kill" gnus-summary-kill-process-mark t]
2316           ["Yank" gnus-summary-yank-process-mark
2317            gnus-newsgroup-process-stack]
2318           ["Save" gnus-summary-save-process-mark t]))
2319         ("Scroll article"
2320          ["Page forward" gnus-summary-next-page
2321           ,@(if (featurep 'xemacs) '(t)
2322               '(:help "Show next page of article"))]
2323          ["Page backward" gnus-summary-prev-page
2324           ,@(if (featurep 'xemacs) '(t)
2325               '(:help "Show previous page of article"))]
2326          ["Line forward" gnus-summary-scroll-up t])
2327         ("Move"
2328          ["Next unread article" gnus-summary-next-unread-article t]
2329          ["Previous unread article" gnus-summary-prev-unread-article t]
2330          ["Next article" gnus-summary-next-article t]
2331          ["Previous article" gnus-summary-prev-article t]
2332          ["Next unread subject" gnus-summary-next-unread-subject t]
2333          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2334          ["Next article same subject" gnus-summary-next-same-subject t]
2335          ["Previous article same subject" gnus-summary-prev-same-subject t]
2336          ["First unread article" gnus-summary-first-unread-article t]
2337          ["Best unread article" gnus-summary-best-unread-article t]
2338          ["Go to subject number..." gnus-summary-goto-subject t]
2339          ["Go to article number..." gnus-summary-goto-article t]
2340          ["Go to the last article" gnus-summary-goto-last-article t]
2341          ["Pop article off history" gnus-summary-pop-article t])
2342         ("Sort"
2343          ["Sort by number" gnus-summary-sort-by-number t]
2344          ["Sort by author" gnus-summary-sort-by-author t]
2345          ["Sort by subject" gnus-summary-sort-by-subject t]
2346          ["Sort by date" gnus-summary-sort-by-date t]
2347          ["Sort by score" gnus-summary-sort-by-score t]
2348          ["Sort by lines" gnus-summary-sort-by-lines t]
2349          ["Sort by characters" gnus-summary-sort-by-chars t]
2350          ["Randomize" gnus-summary-sort-by-random t]
2351          ["Original sort" gnus-summary-sort-by-original t])
2352         ("Help"
2353          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2354          ["Describe group" gnus-summary-describe-group t]
2355          ["Fetch charter" gnus-group-fetch-charter
2356           ,@(if (featurep 'xemacs) nil
2357               '(:help "Display the charter of the current group"))]
2358          ["Fetch control message" gnus-group-fetch-control
2359           ,@(if (featurep 'xemacs) nil
2360               '(:help "Display the archived control message for the current group"))]
2361          ["Read manual" gnus-info-find-node t])
2362         ("Modes"
2363          ["Pick and read" gnus-pick-mode t]
2364          ["Binary" gnus-binary-mode t])
2365         ("Regeneration"
2366          ["Regenerate" gnus-summary-prepare t]
2367          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2368          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2369          ["Toggle threading" gnus-summary-toggle-threads t])
2370         ["See old articles" gnus-summary-insert-old-articles t]
2371         ["See new articles" gnus-summary-insert-new-articles t]
2372         ["Filter articles..." gnus-summary-execute-command t]
2373         ["Run command on subjects..." gnus-summary-universal-argument t]
2374         ["Search articles forward..." gnus-summary-search-article-forward t]
2375         ["Search articles backward..." gnus-summary-search-article-backward t]
2376         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2377         ["Expand window" gnus-summary-expand-window t]
2378         ["Expire expirable articles" gnus-summary-expire-articles
2379          (gnus-check-backend-function
2380           'request-expire-articles gnus-newsgroup-name)]
2381         ["Edit local kill file" gnus-summary-edit-local-kill t]
2382         ["Edit main kill file" gnus-summary-edit-global-kill t]
2383         ["Edit group parameters" gnus-summary-edit-parameters t]
2384         ["Customize group parameters" gnus-summary-customize-parameters t]
2385         ["Send a bug report" gnus-bug t]
2386         ("Exit"
2387          ["Catchup and exit" gnus-summary-catchup-and-exit
2388           ,@(if (featurep 'xemacs) '(t)
2389               '(:help "Mark unread articles in this group as read, then exit"))]
2390          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2391          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2392          ["Exit group" gnus-summary-exit
2393           ,@(if (featurep 'xemacs) '(t)
2394               '(:help "Exit current group, return to group selection mode"))]
2395          ["Exit group without updating" gnus-summary-exit-no-update t]
2396          ["Exit and goto next group" gnus-summary-next-group t]
2397          ["Exit and goto prev group" gnus-summary-prev-group t]
2398          ["Reselect group" gnus-summary-reselect-current-group t]
2399          ["Rescan group" gnus-summary-rescan-group t]
2400          ["Update dribble" gnus-summary-save-newsrc t])))
2401
2402     (gnus-run-hooks 'gnus-summary-menu-hook)))
2403
2404 (defvar gnus-summary-tool-bar-map nil)
2405
2406 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2407 (defun gnus-summary-make-tool-bar ()
2408   (if (and (fboundp 'tool-bar-add-item-from-menu)
2409            (default-value 'tool-bar-mode)
2410            (not gnus-summary-tool-bar-map))
2411       (setq gnus-summary-tool-bar-map
2412             (let ((tool-bar-map (make-sparse-keymap))
2413                   (load-path (mm-image-load-path)))
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-post-news "post" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-followup "followup" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-reply "reply" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2438               (tool-bar-add-item-from-menu
2439                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2440               (tool-bar-add-item-from-menu
2441                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2442               (tool-bar-add-item-from-menu
2443                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2444               tool-bar-map)))
2445   (if gnus-summary-tool-bar-map
2446       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2447
2448 (defun gnus-score-set-default (var value)
2449   "A version of set that updates the GNU Emacs menu-bar."
2450   (set var value)
2451   ;; It is the message that forces the active status to be updated.
2452   (message ""))
2453
2454 (defun gnus-make-score-map (type)
2455   "Make a summary score map of type TYPE."
2456   (if t
2457       nil
2458     (let ((headers '(("author" "from" string)
2459                      ("subject" "subject" string)
2460                      ("article body" "body" string)
2461                      ("article head" "head" string)
2462                      ("xref" "xref" string)
2463                      ("extra header" "extra" string)
2464                      ("lines" "lines" number)
2465                      ("followups to author" "followup" string)))
2466           (types '((number ("less than" <)
2467                            ("greater than" >)
2468                            ("equal" =))
2469                    (string ("substring" s)
2470                            ("exact string" e)
2471                            ("fuzzy string" f)
2472                            ("regexp" r))))
2473           (perms '(("temporary" (current-time-string))
2474                    ("permanent" nil)
2475                    ("immediate" now)))
2476           header)
2477       (list
2478        (apply
2479         'nconc
2480         (list
2481          (if (eq type 'lower)
2482              "Lower score"
2483            "Increase score"))
2484         (let (outh)
2485           (while headers
2486             (setq header (car headers))
2487             (setq outh
2488                   (cons
2489                    (apply
2490                     'nconc
2491                     (list (car header))
2492                     (let ((ts (cdr (assoc (nth 2 header) types)))
2493                           outt)
2494                       (while ts
2495                         (setq outt
2496                               (cons
2497                                (apply
2498                                 'nconc
2499                                 (list (caar ts))
2500                                 (let ((ps perms)
2501                                       outp)
2502                                   (while ps
2503                                     (setq outp
2504                                           (cons
2505                                            (vector
2506                                             (caar ps)
2507                                             (list
2508                                              'gnus-summary-score-entry
2509                                              (nth 1 header)
2510                                              (if (or (string= (nth 1 header)
2511                                                               "head")
2512                                                      (string= (nth 1 header)
2513                                                               "body"))
2514                                                  ""
2515                                                (list 'gnus-summary-header
2516                                                      (nth 1 header)))
2517                                              (list 'quote (nth 1 (car ts)))
2518                                              (list 'gnus-score-delta-default
2519                                                    nil)
2520                                              (nth 1 (car ps))
2521                                              t)
2522                                             t)
2523                                            outp))
2524                                     (setq ps (cdr ps)))
2525                                   (list (nreverse outp))))
2526                                outt))
2527                         (setq ts (cdr ts)))
2528                       (list (nreverse outt))))
2529                    outh))
2530             (setq headers (cdr headers)))
2531           (list (nreverse outh))))))))
2532
2533 \f
2534
2535 (defun gnus-summary-mode (&optional group)
2536   "Major mode for reading articles.
2537
2538 All normal editing commands are switched off.
2539 \\<gnus-summary-mode-map>
2540 Each line in this buffer represents one article.  To read an
2541 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2542 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2543 respectively.
2544
2545 You can also post articles and send mail from this buffer.  To
2546 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2547 of an article, type `\\[gnus-summary-reply]'.
2548
2549 There are approx. one gazillion commands you can execute in this
2550 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2551
2552 The following commands are available:
2553
2554 \\{gnus-summary-mode-map}"
2555   (interactive)
2556   (kill-all-local-variables)
2557   (when (gnus-visual-p 'summary-menu 'menu)
2558     (gnus-summary-make-menu-bar)
2559     (gnus-summary-make-tool-bar))
2560   (gnus-summary-make-local-variables)
2561   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2562     (gnus-summary-make-local-variables))
2563   (gnus-make-thread-indent-array)
2564   (gnus-simplify-mode-line)
2565   (setq major-mode 'gnus-summary-mode)
2566   (setq mode-name "Summary")
2567   (make-local-variable 'minor-mode-alist)
2568   (use-local-map gnus-summary-mode-map)
2569   (buffer-disable-undo)
2570   (setq buffer-read-only t)             ;Disable modification
2571   (setq truncate-lines t)
2572   (setq selective-display t)
2573   (setq selective-display-ellipses t)   ;Display `...'
2574   (gnus-summary-set-display-table)
2575   (gnus-set-default-directory)
2576   (setq gnus-newsgroup-name group)
2577   (make-local-variable 'gnus-summary-line-format)
2578   (make-local-variable 'gnus-summary-line-format-spec)
2579   (make-local-variable 'gnus-summary-dummy-line-format)
2580   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2581   (make-local-variable 'gnus-summary-mark-positions)
2582   (make-local-hook 'pre-command-hook)
2583   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2584   (gnus-run-hooks 'gnus-summary-mode-hook)
2585   (turn-on-gnus-mailing-list-mode)
2586   (mm-enable-multibyte)
2587   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2588   (gnus-update-summary-mark-positions))
2589
2590 (defun gnus-summary-make-local-variables ()
2591   "Make all the local summary buffer variables."
2592   (let (global)
2593     (dolist (local gnus-summary-local-variables)
2594       (if (consp local)
2595           (progn
2596             (if (eq (cdr local) 'global)
2597                 ;; Copy the global value of the variable.
2598                 (setq global (symbol-value (car local)))
2599               ;; Use the value from the list.
2600               (setq global (eval (cdr local))))
2601             (set (make-local-variable (car local)) global))
2602         ;; Simple nil-valued local variable.
2603         (set (make-local-variable local) nil)))))
2604
2605 (defun gnus-summary-clear-local-variables ()
2606   (let ((locals gnus-summary-local-variables))
2607     (while locals
2608       (if (consp (car locals))
2609           (and (vectorp (caar locals))
2610                (set (caar locals) nil))
2611         (and (vectorp (car locals))
2612              (set (car locals) nil)))
2613       (setq locals (cdr locals)))))
2614
2615 ;; Summary data functions.
2616
2617 (defmacro gnus-data-number (data)
2618   `(car ,data))
2619
2620 (defmacro gnus-data-set-number (data number)
2621   `(setcar ,data ,number))
2622
2623 (defmacro gnus-data-mark (data)
2624   `(nth 1 ,data))
2625
2626 (defmacro gnus-data-set-mark (data mark)
2627   `(setcar (nthcdr 1 ,data) ,mark))
2628
2629 (defmacro gnus-data-pos (data)
2630   `(nth 2 ,data))
2631
2632 (defmacro gnus-data-set-pos (data pos)
2633   `(setcar (nthcdr 2 ,data) ,pos))
2634
2635 (defmacro gnus-data-header (data)
2636   `(nth 3 ,data))
2637
2638 (defmacro gnus-data-set-header (data header)
2639   `(setf (nth 3 ,data) ,header))
2640
2641 (defmacro gnus-data-level (data)
2642   `(nth 4 ,data))
2643
2644 (defmacro gnus-data-unread-p (data)
2645   `(= (nth 1 ,data) gnus-unread-mark))
2646
2647 (defmacro gnus-data-read-p (data)
2648   `(/= (nth 1 ,data) gnus-unread-mark))
2649
2650 (defmacro gnus-data-pseudo-p (data)
2651   `(consp (nth 3 ,data)))
2652
2653 (defmacro gnus-data-find (number)
2654   `(assq ,number gnus-newsgroup-data))
2655
2656 (defmacro gnus-data-find-list (number &optional data)
2657   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2658      (memq (assq ,number bdata)
2659            bdata)))
2660
2661 (defmacro gnus-data-make (number mark pos header level)
2662   `(list ,number ,mark ,pos ,header ,level))
2663
2664 (defun gnus-data-enter (after-article number mark pos header level offset)
2665   (let ((data (gnus-data-find-list after-article)))
2666     (unless data
2667       (error "No such article: %d" after-article))
2668     (setcdr data (cons (gnus-data-make number mark pos header level)
2669                        (cdr data)))
2670     (setq gnus-newsgroup-data-reverse nil)
2671     (gnus-data-update-list (cddr data) offset)))
2672
2673 (defun gnus-data-enter-list (after-article list &optional offset)
2674   (when list
2675     (let ((data (and after-article (gnus-data-find-list after-article)))
2676           (ilist list))
2677       (if (not (or data
2678                    after-article))
2679           (let ((odata gnus-newsgroup-data))
2680             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2681             (when offset
2682               (gnus-data-update-list odata offset)))
2683       ;; Find the last element in the list to be spliced into the main
2684         ;; list.
2685         (while (cdr list)
2686           (setq list (cdr list)))
2687         (if (not data)
2688             (progn
2689               (setcdr list gnus-newsgroup-data)
2690               (setq gnus-newsgroup-data ilist)
2691               (when offset
2692                 (gnus-data-update-list (cdr list) offset)))
2693           (setcdr list (cdr data))
2694           (setcdr data ilist)
2695           (when offset
2696             (gnus-data-update-list (cdr list) offset))))
2697       (setq gnus-newsgroup-data-reverse nil))))
2698
2699 (defun gnus-data-remove (article &optional offset)
2700   (let ((data gnus-newsgroup-data))
2701     (if (= (gnus-data-number (car data)) article)
2702         (progn
2703           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2704                 gnus-newsgroup-data-reverse nil)
2705           (when offset
2706             (gnus-data-update-list gnus-newsgroup-data offset)))
2707       (while (cdr data)
2708         (when (= (gnus-data-number (cadr data)) article)
2709           (setcdr data (cddr data))
2710           (when offset
2711             (gnus-data-update-list (cdr data) offset))
2712           (setq data nil
2713                 gnus-newsgroup-data-reverse nil))
2714         (setq data (cdr data))))))
2715
2716 (defmacro gnus-data-list (backward)
2717   `(if ,backward
2718        (or gnus-newsgroup-data-reverse
2719            (setq gnus-newsgroup-data-reverse
2720                  (reverse gnus-newsgroup-data)))
2721      gnus-newsgroup-data))
2722
2723 (defun gnus-data-update-list (data offset)
2724   "Add OFFSET to the POS of all data entries in DATA."
2725   (setq gnus-newsgroup-data-reverse nil)
2726   (while data
2727     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2728     (setq data (cdr data))))
2729
2730 (defun gnus-summary-article-pseudo-p (article)
2731   "Say whether this article is a pseudo article or not."
2732   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2733
2734 (defmacro gnus-summary-article-sparse-p (article)
2735   "Say whether this article is a sparse article or not."
2736   `(memq ,article gnus-newsgroup-sparse))
2737
2738 (defmacro gnus-summary-article-ancient-p (article)
2739   "Say whether this article is a sparse article or not."
2740   `(memq ,article gnus-newsgroup-ancient))
2741
2742 (defun gnus-article-parent-p (number)
2743   "Say whether this article is a parent or not."
2744   (let ((data (gnus-data-find-list number)))
2745     (and (cdr data)              ; There has to be an article after...
2746          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2747             (gnus-data-level (nth 1 data))))))
2748
2749 (defun gnus-article-children (number)
2750   "Return a list of all children to NUMBER."
2751   (let* ((data (gnus-data-find-list number))
2752          (level (gnus-data-level (car data)))
2753          children)
2754     (setq data (cdr data))
2755     (while (and data
2756                 (= (gnus-data-level (car data)) (1+ level)))
2757       (push (gnus-data-number (car data)) children)
2758       (setq data (cdr data)))
2759     children))
2760
2761 (defmacro gnus-summary-skip-intangible ()
2762   "If the current article is intangible, then jump to a different article."
2763   '(let ((to (get-text-property (point) 'gnus-intangible)))
2764      (and to (gnus-summary-goto-subject to))))
2765
2766 (defmacro gnus-summary-article-intangible-p ()
2767   "Say whether this article is intangible or not."
2768   '(get-text-property (point) 'gnus-intangible))
2769
2770 (defun gnus-article-read-p (article)
2771   "Say whether ARTICLE is read or not."
2772   (not (or (memq article gnus-newsgroup-marked)
2773            (memq article gnus-newsgroup-spam-marked)
2774            (memq article gnus-newsgroup-unreads)
2775            (memq article gnus-newsgroup-unselected)
2776            (memq article gnus-newsgroup-dormant))))
2777
2778 ;; Some summary mode macros.
2779
2780 (defmacro gnus-summary-article-number ()
2781   "The article number of the article on the current line.
2782 If there isn't an article number here, then we return the current
2783 article number."
2784   '(progn
2785      (gnus-summary-skip-intangible)
2786      (or (get-text-property (point) 'gnus-number)
2787          (gnus-summary-last-subject))))
2788
2789 (defmacro gnus-summary-article-header (&optional number)
2790   "Return the header of article NUMBER."
2791   `(gnus-data-header (gnus-data-find
2792                       ,(or number '(gnus-summary-article-number)))))
2793
2794 (defmacro gnus-summary-thread-level (&optional number)
2795   "Return the level of thread that starts with article NUMBER."
2796   `(if (and (eq gnus-summary-make-false-root 'dummy)
2797             (get-text-property (point) 'gnus-intangible))
2798        0
2799      (gnus-data-level (gnus-data-find
2800                        ,(or number '(gnus-summary-article-number))))))
2801
2802 (defmacro gnus-summary-article-mark (&optional number)
2803   "Return the mark of article NUMBER."
2804   `(gnus-data-mark (gnus-data-find
2805                     ,(or number '(gnus-summary-article-number)))))
2806
2807 (defmacro gnus-summary-article-pos (&optional number)
2808   "Return the position of the line of article NUMBER."
2809   `(gnus-data-pos (gnus-data-find
2810                    ,(or number '(gnus-summary-article-number)))))
2811
2812 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2813 (defmacro gnus-summary-article-subject (&optional number)
2814   "Return current subject string or nil if nothing."
2815   `(let ((headers
2816           ,(if number
2817                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2818              '(gnus-data-header (assq (gnus-summary-article-number)
2819                                       gnus-newsgroup-data)))))
2820      (and headers
2821           (vectorp headers)
2822           (mail-header-subject headers))))
2823
2824 (defmacro gnus-summary-article-score (&optional number)
2825   "Return current article score."
2826   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2827                   gnus-newsgroup-scored))
2828        gnus-summary-default-score 0))
2829
2830 (defun gnus-summary-article-children (&optional number)
2831   "Return a list of article numbers that are children of article NUMBER."
2832   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2833          (level (gnus-data-level (car data)))
2834          l children)
2835     (while (and (setq data (cdr data))
2836                 (> (setq l (gnus-data-level (car data))) level))
2837       (and (= (1+ level) l)
2838            (push (gnus-data-number (car data))
2839                  children)))
2840     (nreverse children)))
2841
2842 (defun gnus-summary-article-parent (&optional number)
2843   "Return the article number of the parent of article NUMBER."
2844   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2845                                     (gnus-data-list t)))
2846          (level (gnus-data-level (car data))))
2847     (if (zerop level)
2848         ()                              ; This is a root.
2849       ;; We search until we find an article with a level less than
2850       ;; this one.  That function has to be the parent.
2851       (while (and (setq data (cdr data))
2852                   (not (< (gnus-data-level (car data)) level))))
2853       (and data (gnus-data-number (car data))))))
2854
2855 (defun gnus-unread-mark-p (mark)
2856   "Say whether MARK is the unread mark."
2857   (= mark gnus-unread-mark))
2858
2859 (defun gnus-read-mark-p (mark)
2860   "Say whether MARK is one of the marks that mark as read.
2861 This is all marks except unread, ticked, dormant, and expirable."
2862   (not (or (= mark gnus-unread-mark)
2863            (= mark gnus-ticked-mark)
2864            (= mark gnus-spam-mark)
2865            (= mark gnus-dormant-mark)
2866            (= mark gnus-expirable-mark))))
2867
2868 (defmacro gnus-article-mark (number)
2869   "Return the MARK of article NUMBER.
2870 This macro should only be used when computing the mark the \"first\"
2871 time; i.e., when generating the summary lines.  After that,
2872 `gnus-summary-article-mark' should be used to examine the
2873 marks of articles."
2874   `(cond
2875     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2876     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2877     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2878     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2879     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2880     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2881     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2882     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2883            gnus-ancient-mark))))
2884
2885 ;; Saving hidden threads.
2886
2887 (defmacro gnus-save-hidden-threads (&rest forms)
2888   "Save hidden threads, eval FORMS, and restore the hidden threads."
2889   (let ((config (make-symbol "config")))
2890     `(let ((,config (gnus-hidden-threads-configuration)))
2891        (unwind-protect
2892            (save-excursion
2893              ,@forms)
2894          (gnus-restore-hidden-threads-configuration ,config)))))
2895 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2896 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2897
2898 (defun gnus-data-compute-positions ()
2899   "Compute the positions of all articles."
2900   (setq gnus-newsgroup-data-reverse nil)
2901   (let ((data gnus-newsgroup-data))
2902     (save-excursion
2903       (gnus-save-hidden-threads
2904         (gnus-summary-show-all-threads)
2905         (goto-char (point-min))
2906         (while data
2907           (while (get-text-property (point) 'gnus-intangible)
2908             (forward-line 1))
2909           (gnus-data-set-pos (car data) (+ (point) 3))
2910           (setq data (cdr data))
2911           (forward-line 1))))))
2912
2913 (defun gnus-hidden-threads-configuration ()
2914   "Return the current hidden threads configuration."
2915   (save-excursion
2916     (let (config)
2917       (goto-char (point-min))
2918       (while (search-forward "\r" nil t)
2919         (push (1- (point)) config))
2920       config)))
2921
2922 (defun gnus-restore-hidden-threads-configuration (config)
2923   "Restore hidden threads configuration from CONFIG."
2924   (save-excursion
2925     (let (point buffer-read-only)
2926       (while (setq point (pop config))
2927         (when (and (< point (point-max))
2928                    (goto-char point)
2929                    (eq (char-after) ?\n))
2930           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2931
2932 ;; Various summary mode internalish functions.
2933
2934 (defun gnus-mouse-pick-article (e)
2935   (interactive "e")
2936   (mouse-set-point e)
2937   (gnus-summary-next-page nil t))
2938
2939 (defun gnus-summary-set-display-table ()
2940   "Change the display table.
2941 Odd characters have a tendency to mess
2942 up nicely formatted displays - we make all possible glyphs
2943 display only a single character."
2944
2945   ;; We start from the standard display table, if any.
2946   (let ((table (or (copy-sequence standard-display-table)
2947                    (make-display-table)))
2948         (i 32))
2949     ;; Nix out all the control chars...
2950     (while (>= (setq i (1- i)) 0)
2951       (aset table i [??]))
2952    ;; ... but not newline and cr, of course.  (cr is necessary for the
2953     ;; selective display).
2954     (aset table ?\n nil)
2955     (aset table ?\r nil)
2956     ;; We keep TAB as well.
2957     (aset table ?\t nil)
2958     ;; We nix out any glyphs over 126 that are not set already.
2959     (let ((i 256))
2960       (while (>= (setq i (1- i)) 127)
2961         ;; Only modify if the entry is nil.
2962         (unless (aref table i)
2963           (aset table i [??]))))
2964     (setq buffer-display-table table)))
2965
2966 (defun gnus-summary-set-article-display-arrow (pos)
2967   "Update the overlay arrow to point to line at position POS."
2968   (when (and gnus-summary-display-arrow
2969              (boundp 'overlay-arrow-position)
2970              (boundp 'overlay-arrow-string))
2971     (save-excursion
2972       (goto-char pos)
2973       (beginning-of-line)
2974       (unless overlay-arrow-position
2975         (setq overlay-arrow-position (make-marker)))
2976       (setq overlay-arrow-string "=>"
2977             overlay-arrow-position (set-marker overlay-arrow-position
2978                                                (point)
2979                                                (current-buffer))))))
2980
2981 (defun gnus-summary-buffer-name (group)
2982   "Return the summary buffer name of GROUP."
2983   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2984
2985 (defun gnus-summary-setup-buffer (group)
2986   "Initialize summary buffer."
2987   (let ((buffer (gnus-summary-buffer-name group))
2988         (dead-name (concat "*Dead Summary "
2989                            (gnus-group-decoded-name group) "*")))
2990     ;; If a dead summary buffer exists, we kill it.
2991     (when (gnus-buffer-live-p dead-name)
2992       (gnus-kill-buffer dead-name))
2993     (if (get-buffer buffer)
2994         (progn
2995           (set-buffer buffer)
2996           (setq gnus-summary-buffer (current-buffer))
2997           (not gnus-newsgroup-prepared))
2998       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2999       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3000       (gnus-summary-mode group)
3001       (when gnus-carpal
3002         (gnus-carpal-setup-buffer 'summary))
3003       (unless gnus-single-article-buffer
3004         (make-local-variable 'gnus-article-buffer)
3005         (make-local-variable 'gnus-article-current)
3006         (make-local-variable 'gnus-original-article-buffer))
3007       (setq gnus-newsgroup-name group)
3008       ;; Set any local variables in the group parameters.
3009       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3010       t)))
3011
3012 (defun gnus-set-global-variables ()
3013   "Set the global equivalents of the buffer-local variables.
3014 They are set to the latest values they had.  These reflect the summary
3015 buffer that was in action when the last article was fetched."
3016   (when (eq major-mode 'gnus-summary-mode)
3017     (setq gnus-summary-buffer (current-buffer))
3018     (let ((name gnus-newsgroup-name)
3019           (marked gnus-newsgroup-marked)
3020           (spam gnus-newsgroup-spam-marked)
3021           (unread gnus-newsgroup-unreads)
3022           (headers gnus-current-headers)
3023           (data gnus-newsgroup-data)
3024           (summary gnus-summary-buffer)
3025           (article-buffer gnus-article-buffer)
3026           (original gnus-original-article-buffer)
3027           (gac gnus-article-current)
3028           (reffed gnus-reffed-article-number)
3029           (score-file gnus-current-score-file)
3030           (default-charset gnus-newsgroup-charset)
3031           vlist)
3032       (let ((locals gnus-newsgroup-variables))
3033         (while locals
3034           (if (consp (car locals))
3035               (push (eval (caar locals)) vlist)
3036             (push (eval (car locals)) vlist))
3037           (setq locals (cdr locals)))
3038         (setq vlist (nreverse vlist)))
3039       (save-excursion
3040         (set-buffer gnus-group-buffer)
3041         (setq gnus-newsgroup-name name
3042               gnus-newsgroup-marked marked
3043               gnus-newsgroup-spam-marked spam
3044               gnus-newsgroup-unreads unread
3045               gnus-current-headers headers
3046               gnus-newsgroup-data data
3047               gnus-article-current gac
3048               gnus-summary-buffer summary
3049               gnus-article-buffer article-buffer
3050               gnus-original-article-buffer original
3051               gnus-reffed-article-number reffed
3052               gnus-current-score-file score-file
3053               gnus-newsgroup-charset default-charset)
3054         (let ((locals gnus-newsgroup-variables))
3055           (while locals
3056             (if (consp (car locals))
3057                 (set (caar locals) (pop vlist))
3058               (set (car locals) (pop vlist)))
3059             (setq locals (cdr locals))))
3060         ;; The article buffer also has local variables.
3061         (when (gnus-buffer-live-p gnus-article-buffer)
3062           (set-buffer gnus-article-buffer)
3063           (setq gnus-summary-buffer summary))))))
3064
3065 (defun gnus-summary-article-unread-p (article)
3066   "Say whether ARTICLE is unread or not."
3067   (memq article gnus-newsgroup-unreads))
3068
3069 (defun gnus-summary-first-article-p (&optional article)
3070   "Return whether ARTICLE is the first article in the buffer."
3071   (if (not (setq article (or article (gnus-summary-article-number))))
3072       nil
3073     (eq article (caar gnus-newsgroup-data))))
3074
3075 (defun gnus-summary-last-article-p (&optional article)
3076   "Return whether ARTICLE is the last article in the buffer."
3077   (if (not (setq article (or article (gnus-summary-article-number))))
3078       ;; All non-existent numbers are the last article.  :-)
3079       t
3080     (not (cdr (gnus-data-find-list article)))))
3081
3082 (defun gnus-make-thread-indent-array ()
3083   (let ((n 200))
3084     (unless (and gnus-thread-indent-array
3085                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3086       (setq gnus-thread-indent-array (make-vector 201 "")
3087             gnus-thread-indent-array-level gnus-thread-indent-level)
3088       (while (>= n 0)
3089         (aset gnus-thread-indent-array n
3090               (make-string (* n gnus-thread-indent-level) ? ))
3091         (setq n (1- n))))))
3092
3093 (defun gnus-update-summary-mark-positions ()
3094   "Compute where the summary marks are to go."
3095   (save-excursion
3096     (when (gnus-buffer-exists-p gnus-summary-buffer)
3097       (set-buffer gnus-summary-buffer))
3098     (let ((gnus-replied-mark 129)
3099           (gnus-score-below-mark 130)
3100           (gnus-score-over-mark 130)
3101           (gnus-undownloaded-mark 131)
3102           (spec gnus-summary-line-format-spec)
3103           gnus-visual pos)
3104       (save-excursion
3105         (gnus-set-work-buffer)
3106         (let ((gnus-summary-line-format-spec spec)
3107               (gnus-newsgroup-downloadable '(0)))
3108           (gnus-summary-insert-line
3109            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3110            0 nil t 128 t nil "" nil 1)
3111           (goto-char (point-min))
3112           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3113                                              (- (point) (point-min) 1)))))
3114           (goto-char (point-min))
3115           (push (cons 'replied (and (search-forward "\201" nil t)
3116                                     (- (point) (point-min) 1)))
3117                 pos)
3118           (goto-char (point-min))
3119           (push (cons 'score (and (search-forward "\202" nil t)
3120                                   (- (point) (point-min) 1)))
3121                 pos)
3122           (goto-char (point-min))
3123           (push (cons 'download
3124                       (and (search-forward "\203" nil t)
3125                            (- (point) (point-min) 1)))
3126                 pos)))
3127       (setq gnus-summary-mark-positions pos))))
3128
3129 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3130   "Insert a dummy root in the summary buffer."
3131   (beginning-of-line)
3132   (gnus-add-text-properties
3133    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3134    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3135
3136 (defun gnus-summary-extract-address-component (from)
3137   (or (car (funcall gnus-extract-address-components from))
3138       from))
3139
3140 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3141   (let ((mail-parse-charset gnus-newsgroup-charset)
3142         ; Is it really necessary to do this next part for each summary line?
3143         ; Luckily, doesn't seem to slow things down much.
3144         (mail-parse-ignored-charsets
3145          (save-excursion (set-buffer gnus-summary-buffer)
3146                          gnus-newsgroup-ignored-charsets)))
3147     (or
3148      (and gnus-ignored-from-addresses
3149           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3150           (let ((extra-headers (mail-header-extra header))
3151                 to
3152                 newsgroups)
3153             (cond
3154              ((setq to (cdr (assq 'To extra-headers)))
3155               (concat "-> "
3156                       (inline
3157                         (gnus-summary-extract-address-component
3158                          (funcall gnus-decode-encoded-word-function to)))))
3159              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3160               (concat "=> " newsgroups)))))
3161      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3162
3163 (defun gnus-summary-insert-line (gnus-tmp-header
3164                                  gnus-tmp-level gnus-tmp-current
3165                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3166                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3167                                  &optional gnus-tmp-dummy gnus-tmp-score
3168                                  gnus-tmp-process)
3169   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3170          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3171          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3172          (gnus-tmp-score-char
3173           (if (or (null gnus-summary-default-score)
3174                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3175                       gnus-summary-zcore-fuzz))
3176               ?                         ;Whitespace
3177             (if (< gnus-tmp-score gnus-summary-default-score)
3178                 gnus-score-below-mark gnus-score-over-mark)))
3179          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3180          (gnus-tmp-replied
3181           (cond (gnus-tmp-process gnus-process-mark)
3182                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3183                  gnus-cached-mark)
3184                 (gnus-tmp-replied gnus-replied-mark)
3185                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3186                  gnus-forwarded-mark)
3187                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3188                  gnus-saved-mark)
3189                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3190                  gnus-recent-mark)
3191                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3192                  gnus-unseen-mark)
3193                 (t gnus-no-mark)))
3194          (gnus-tmp-downloaded
3195           (cond (undownloaded 
3196                  gnus-undownloaded-mark)
3197                 (gnus-newsgroup-agentized
3198                  gnus-downloaded-mark)
3199                 (t
3200                  gnus-no-mark)))
3201          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3202          (gnus-tmp-name
3203           (cond
3204            ((string-match "<[^>]+> *$" gnus-tmp-from)
3205             (let ((beg (match-beginning 0)))
3206               (or (and (string-match "^\".+\"" gnus-tmp-from)
3207                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3208                   (substring gnus-tmp-from 0 beg))))
3209            ((string-match "(.+)" gnus-tmp-from)
3210             (substring gnus-tmp-from
3211                        (1+ (match-beginning 0)) (1- (match-end 0))))
3212            (t gnus-tmp-from)))
3213          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3214          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3215          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3216          (buffer-read-only nil))
3217     (when (string= gnus-tmp-name "")
3218       (setq gnus-tmp-name gnus-tmp-from))
3219     (unless (numberp gnus-tmp-lines)
3220       (setq gnus-tmp-lines -1))
3221     (if (= gnus-tmp-lines -1)
3222         (setq gnus-tmp-lines "?")
3223       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3224     (gnus-put-text-property
3225      (point)
3226      (progn (eval gnus-summary-line-format-spec) (point))
3227      'gnus-number gnus-tmp-number)
3228     (when (gnus-visual-p 'summary-highlight 'highlight)
3229       (forward-line -1)
3230       (gnus-run-hooks 'gnus-summary-update-hook)
3231       (forward-line 1))))
3232
3233 (defun gnus-summary-update-line (&optional dont-update)
3234   "Update summary line after change."
3235   (when (and gnus-summary-default-score
3236              (not gnus-summary-inhibit-highlight))
3237     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3238            (article (gnus-summary-article-number))
3239            (score (gnus-summary-article-score article)))
3240       (unless dont-update
3241         (if (and gnus-summary-mark-below
3242                  (< (gnus-summary-article-score)
3243                     gnus-summary-mark-below))
3244             ;; This article has a low score, so we mark it as read.
3245             (when (memq article gnus-newsgroup-unreads)
3246               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3247           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3248             ;; This article was previously marked as read on account
3249             ;; of a low score, but now it has risen, so we mark it as
3250             ;; unread.
3251             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3252         (gnus-summary-update-mark
3253          (if (or (null gnus-summary-default-score)
3254                  (<= (abs (- score gnus-summary-default-score))
3255                      gnus-summary-zcore-fuzz))
3256              ?                          ;Whitespace
3257            (if (< score gnus-summary-default-score)
3258                gnus-score-below-mark gnus-score-over-mark))
3259          'score))
3260       ;; Do visual highlighting.
3261       (when (gnus-visual-p 'summary-highlight 'highlight)
3262         (gnus-run-hooks 'gnus-summary-update-hook)))))
3263
3264 (defvar gnus-tmp-new-adopts nil)
3265
3266 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3267   "Return the number of articles in THREAD.
3268 This may be 0 in some cases -- if none of the articles in
3269 the thread are to be displayed."
3270   (let* ((number
3271          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3272           (cond
3273            ((not (listp thread))
3274             1)
3275            ((and (consp thread) (cdr thread))
3276             (apply
3277              '+ 1 (mapcar
3278                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3279            ((null thread)
3280             1)
3281            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3282             1)
3283            (t 0))))
3284     (when (and level (zerop level) gnus-tmp-new-adopts)
3285       (incf number
3286             (apply '+ (mapcar
3287                        'gnus-summary-number-of-articles-in-thread
3288                        gnus-tmp-new-adopts))))
3289     (if char
3290         (if (> number 1) gnus-not-empty-thread-mark
3291           gnus-empty-thread-mark)
3292       number)))
3293
3294 (defsubst gnus-summary-line-message-size (head)
3295   "Return pretty-printed version of message size.
3296 This function is intended to be used in
3297 `gnus-summary-line-format-alist', which see."
3298   (let ((c (or (mail-header-chars head) -1)))
3299     (cond ((< c 0) "n/a")               ; chars not available
3300           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3301           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3302           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3303           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3304
3305
3306 (defun gnus-summary-set-local-parameters (group)
3307   "Go through the local params of GROUP and set all variable specs in that list."
3308   (let ((params (gnus-group-find-parameter group))
3309         (vars '(quit-config))           ; Ignore quit-config.
3310         elem)
3311     (while params
3312       (setq elem (car params)
3313             params (cdr params))
3314       (and (consp elem)                 ; Has to be a cons.
3315            (consp (cdr elem))           ; The cdr has to be a list.
3316            (symbolp (car elem))         ; Has to be a symbol in there.
3317            (not (memq (car elem) vars))
3318            (ignore-errors               ; So we set it.
3319              (push (car elem) vars)
3320              (make-local-variable (car elem))
3321              (set (car elem) (eval (nth 1 elem))))))))
3322
3323 (defun gnus-summary-read-group (group &optional show-all no-article
3324                                       kill-buffer no-display backward
3325                                       select-articles)
3326   "Start reading news in newsgroup GROUP.
3327 If SHOW-ALL is non-nil, already read articles are also listed.
3328 If NO-ARTICLE is non-nil, no article is selected initially.
3329 If NO-DISPLAY, don't generate a summary buffer."
3330   (let (result)
3331     (while (and group
3332                 (null (setq result
3333                             (let ((gnus-auto-select-next nil))
3334                               (or (gnus-summary-read-group-1
3335                                    group show-all no-article
3336                                    kill-buffer no-display
3337                                    select-articles)
3338                                   (setq show-all nil
3339                                         select-articles nil)))))
3340                 (eq gnus-auto-select-next 'quietly))
3341       (set-buffer gnus-group-buffer)
3342       ;; The entry function called above goes to the next
3343       ;; group automatically, so we go two groups back
3344       ;; if we are searching for the previous group.
3345       (when backward
3346         (gnus-group-prev-unread-group 2))
3347       (if (not (equal group (gnus-group-group-name)))
3348           (setq group (gnus-group-group-name))
3349         (setq group nil)))
3350     result))
3351
3352 (defun gnus-summary-read-group-1 (group show-all no-article
3353                                         kill-buffer no-display
3354                                         &optional select-articles)
3355   ;; Killed foreign groups can't be entered.
3356   ;;  (when (and (not (gnus-group-native-p group))
3357   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3358   ;;    (error "Dead non-native groups can't be entered"))
3359   (gnus-message 5 "Retrieving newsgroup: %s..."
3360                 (gnus-group-decoded-name group))
3361   (let* ((new-group (gnus-summary-setup-buffer group))
3362          (quit-config (gnus-group-quit-config group))
3363          (did-select (and new-group (gnus-select-newsgroup
3364                                      group show-all select-articles))))
3365     (cond
3366      ;; This summary buffer exists already, so we just select it.
3367      ((not new-group)
3368       (gnus-set-global-variables)
3369       (when kill-buffer
3370         (gnus-kill-or-deaden-summary kill-buffer))
3371       (gnus-configure-windows 'summary 'force)
3372       (gnus-set-mode-line 'summary)
3373       (gnus-summary-position-point)
3374       (message "")
3375       t)
3376      ;; We couldn't select this group.
3377      ((null did-select)
3378       (when (and (eq major-mode 'gnus-summary-mode)
3379                  (not (equal (current-buffer) kill-buffer)))
3380         (kill-buffer (current-buffer))
3381         (if (not quit-config)
3382             (progn
3383               ;; Update the info -- marks might need to be removed,
3384               ;; for instance.
3385               (gnus-summary-update-info)
3386               (set-buffer gnus-group-buffer)
3387               (gnus-group-jump-to-group group)
3388               (gnus-group-next-unread-group 1))
3389           (gnus-handle-ephemeral-exit quit-config)))
3390       (let ((grpinfo (gnus-get-info group)))
3391         (if (null (gnus-info-read grpinfo))
3392             (gnus-message 3 "Group %s contains no messages"
3393                           (gnus-group-decoded-name group))
3394           (gnus-message 3 "Can't select group")))
3395       nil)
3396      ;; The user did a `C-g' while prompting for number of articles,
3397      ;; so we exit this group.
3398      ((eq did-select 'quit)
3399       (and (eq major-mode 'gnus-summary-mode)
3400            (not (equal (current-buffer) kill-buffer))
3401            (kill-buffer (current-buffer)))
3402       (when kill-buffer
3403         (gnus-kill-or-deaden-summary kill-buffer))
3404       (if (not quit-config)
3405           (progn
3406             (set-buffer gnus-group-buffer)
3407             (gnus-group-jump-to-group group)
3408             (gnus-group-next-unread-group 1)
3409             (gnus-configure-windows 'group 'force))
3410         (gnus-handle-ephemeral-exit quit-config))
3411       ;; Finally signal the quit.
3412       (signal 'quit nil))
3413      ;; The group was successfully selected.
3414      (t
3415       (gnus-set-global-variables)
3416       ;; Save the active value in effect when the group was entered.
3417       (setq gnus-newsgroup-active
3418             (gnus-copy-sequence
3419              (gnus-active gnus-newsgroup-name)))
3420       ;; You can change the summary buffer in some way with this hook.
3421       (gnus-run-hooks 'gnus-select-group-hook)
3422       (gnus-update-format-specifications
3423        nil 'summary 'summary-mode 'summary-dummy)
3424       (gnus-update-summary-mark-positions)
3425       ;; Do score processing.
3426       (when gnus-use-scoring
3427         (gnus-possibly-score-headers))
3428       ;; Check whether to fill in the gaps in the threads.
3429       (when gnus-build-sparse-threads
3430         (gnus-build-sparse-threads))
3431       ;; Find the initial limit.
3432       (if gnus-show-threads
3433           (if show-all
3434               (let ((gnus-newsgroup-dormant nil))
3435                 (gnus-summary-initial-limit show-all))
3436             (gnus-summary-initial-limit show-all))
3437         ;; When unthreaded, all articles are always shown.
3438         (setq gnus-newsgroup-limit
3439               (mapcar
3440                (lambda (header) (mail-header-number header))
3441                gnus-newsgroup-headers)))
3442       ;; Generate the summary buffer.
3443       (unless no-display
3444         (gnus-summary-prepare))
3445       (when gnus-use-trees
3446         (gnus-tree-open group)
3447         (setq gnus-summary-highlight-line-function
3448               'gnus-tree-highlight-article))
3449       ;; If the summary buffer is empty, but there are some low-scored
3450       ;; articles or some excluded dormants, we include these in the
3451       ;; buffer.
3452       (when (and (zerop (buffer-size))
3453                  (not no-display))
3454         (cond (gnus-newsgroup-dormant
3455                (gnus-summary-limit-include-dormant))
3456               ((and gnus-newsgroup-scored show-all)
3457                (gnus-summary-limit-include-expunged t))))
3458       ;; Function `gnus-apply-kill-file' must be called in this hook.
3459       (gnus-run-hooks 'gnus-apply-kill-hook)
3460       (if (and (zerop (buffer-size))
3461                (not no-display))
3462           (progn
3463             ;; This newsgroup is empty.
3464             (gnus-summary-catchup-and-exit nil t)
3465             (gnus-message 6 "No unread news")
3466             (when kill-buffer
3467               (gnus-kill-or-deaden-summary kill-buffer))
3468             ;; Return nil from this function.
3469             nil)
3470         ;; Hide conversation thread subtrees.  We cannot do this in
3471         ;; gnus-summary-prepare-hook since kill processing may not
3472         ;; work with hidden articles.
3473         (gnus-summary-maybe-hide-threads)
3474         (when kill-buffer
3475           (gnus-kill-or-deaden-summary kill-buffer))
3476         (gnus-summary-auto-select-subject)
3477         ;; Show first unread article if requested.
3478         (if (and (not no-article)
3479                  (not no-display)
3480                  gnus-newsgroup-unreads
3481                  gnus-auto-select-first)
3482             (progn
3483               (gnus-configure-windows 'summary)
3484               (let ((art (gnus-summary-article-number)))
3485                 (unless (or (memq art gnus-newsgroup-undownloaded)
3486                             (memq art gnus-newsgroup-downloadable))
3487                   (gnus-summary-goto-article art))))
3488           ;; Don't select any articles.
3489           (gnus-summary-position-point)
3490           (gnus-configure-windows 'summary 'force)
3491           (gnus-set-mode-line 'summary))
3492         (when (get-buffer-window gnus-group-buffer t)
3493           ;; Gotta use windows, because recenter does weird stuff if
3494           ;; the current buffer ain't the displayed window.
3495           (let ((owin (selected-window)))
3496             (select-window (get-buffer-window gnus-group-buffer t))
3497             (when (gnus-group-goto-group group)
3498               (recenter))
3499             (select-window owin)))
3500         ;; Mark this buffer as "prepared".
3501         (setq gnus-newsgroup-prepared t)
3502         (gnus-run-hooks 'gnus-summary-prepared-hook)
3503         (unless (gnus-ephemeral-group-p group)
3504           (gnus-group-update-group group))
3505         t)))))
3506
3507 (defun gnus-summary-auto-select-subject ()
3508   "Select the subject line on initial group entry."
3509   (goto-char (point-min))
3510   (cond
3511    ((eq gnus-auto-select-subject 'best)
3512     (gnus-summary-best-unread-subject))
3513    ((eq gnus-auto-select-subject 'unread)
3514     (gnus-summary-first-unread-subject))
3515    ((eq gnus-auto-select-subject 'unseen)
3516     (gnus-summary-first-unseen-subject))
3517    ((eq gnus-auto-select-subject 'unseen-or-unread)
3518     (gnus-summary-first-unseen-or-unread-subject))
3519    ((eq gnus-auto-select-subject 'first)
3520     ;; Do nothing.
3521     )
3522    ((gnus-functionp gnus-auto-select-subject)
3523     (funcall gnus-auto-select-subject))))
3524
3525 (defun gnus-summary-prepare ()
3526   "Generate the summary buffer."
3527   (interactive)
3528   (let ((buffer-read-only nil))
3529     (erase-buffer)
3530     (setq gnus-newsgroup-data nil
3531           gnus-newsgroup-data-reverse nil)
3532     (gnus-run-hooks 'gnus-summary-generate-hook)
3533     ;; Generate the buffer, either with threads or without.
3534     (when gnus-newsgroup-headers
3535       (gnus-summary-prepare-threads
3536        (if gnus-show-threads
3537            (gnus-sort-gathered-threads
3538             (funcall gnus-summary-thread-gathering-function
3539                      (gnus-sort-threads
3540                       (gnus-cut-threads (gnus-make-threads)))))
3541          ;; Unthreaded display.
3542          (gnus-sort-articles gnus-newsgroup-headers))))
3543     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3544     ;; Call hooks for modifying summary buffer.
3545     (goto-char (point-min))
3546     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3547
3548 (defsubst gnus-general-simplify-subject (subject)
3549   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3550   (setq subject
3551         (cond
3552          ;; Truncate the subject.
3553          (gnus-simplify-subject-functions
3554           (gnus-map-function gnus-simplify-subject-functions subject))
3555          ((numberp gnus-summary-gather-subject-limit)
3556           (setq subject (gnus-simplify-subject-re subject))
3557           (if (> (length subject) gnus-summary-gather-subject-limit)
3558               (substring subject 0 gnus-summary-gather-subject-limit)
3559             subject))
3560          ;; Fuzzily simplify it.
3561          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3562           (gnus-simplify-subject-fuzzy subject))
3563          ;; Just remove the leading "Re:".
3564          (t
3565           (gnus-simplify-subject-re subject))))
3566
3567   (if (and gnus-summary-gather-exclude-subject
3568            (string-match gnus-summary-gather-exclude-subject subject))
3569       nil                         ; This article shouldn't be gathered
3570     subject))
3571
3572 (defun gnus-summary-simplify-subject-query ()
3573   "Query where the respool algorithm would put this article."
3574   (interactive)
3575   (gnus-summary-select-article)
3576   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3577
3578 (defun gnus-gather-threads-by-subject (threads)
3579   "Gather threads by looking at Subject headers."
3580   (if (not gnus-summary-make-false-root)
3581       threads
3582     (let ((hashtb (gnus-make-hashtable 1024))
3583           (prev threads)
3584           (result threads)
3585           subject hthread whole-subject)
3586       (while threads
3587         (setq subject (gnus-general-simplify-subject
3588                        (setq whole-subject (mail-header-subject
3589                                             (caar threads)))))
3590         (when subject
3591           (if (setq hthread (gnus-gethash subject hashtb))
3592               (progn
3593                 ;; We enter a dummy root into the thread, if we
3594                 ;; haven't done that already.
3595                 (unless (stringp (caar hthread))
3596                   (setcar hthread (list whole-subject (car hthread))))
3597                 ;; We add this new gathered thread to this gathered
3598                 ;; thread.
3599                 (setcdr (car hthread)
3600                         (nconc (cdar hthread) (list (car threads))))
3601                 ;; Remove it from the list of threads.
3602                 (setcdr prev (cdr threads))
3603                 (setq threads prev))
3604             ;; Enter this thread into the hash table.
3605             (gnus-sethash subject
3606                           (if gnus-summary-make-false-root-always
3607                               (progn
3608                                 ;; If you want a dummy root above all
3609                                 ;; threads...
3610                                 (setcar threads (list whole-subject
3611                                                       (car threads)))
3612                                 threads)
3613                             threads)
3614                           hashtb)))
3615         (setq prev threads)
3616         (setq threads (cdr threads)))
3617       result)))
3618
3619 (defun gnus-gather-threads-by-references (threads)
3620   "Gather threads by looking at References headers."
3621   (let ((idhashtb (gnus-make-hashtable 1024))
3622         (thhashtb (gnus-make-hashtable 1024))
3623         (prev threads)
3624         (result threads)
3625         ids references id gthread gid entered ref)
3626     (while threads
3627       (when (setq references (mail-header-references (caar threads)))
3628         (setq id (mail-header-id (caar threads))
3629               ids (inline (gnus-split-references references))
3630               entered nil)
3631         (while (setq ref (pop ids))
3632           (setq ids (delete ref ids))
3633           (if (not (setq gid (gnus-gethash ref idhashtb)))
3634               (progn
3635                 (gnus-sethash ref id idhashtb)
3636                 (gnus-sethash id threads thhashtb))
3637             (setq gthread (gnus-gethash gid thhashtb))
3638             (unless entered
3639               ;; We enter a dummy root into the thread, if we
3640               ;; haven't done that already.
3641               (unless (stringp (caar gthread))
3642                 (setcar gthread (list (mail-header-subject (caar gthread))
3643                                       (car gthread))))
3644               ;; We add this new gathered thread to this gathered
3645               ;; thread.
3646               (setcdr (car gthread)
3647                       (nconc (cdar gthread) (list (car threads)))))
3648             ;; Add it into the thread hash table.
3649             (gnus-sethash id gthread thhashtb)
3650             (setq entered t)
3651             ;; Remove it from the list of threads.
3652             (setcdr prev (cdr threads))
3653             (setq threads prev))))
3654       (setq prev threads)
3655       (setq threads (cdr threads)))
3656     result))
3657
3658 (defun gnus-sort-gathered-threads (threads)
3659   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3660   (let ((result threads))
3661     (while threads
3662       (when (stringp (caar threads))
3663         (setcdr (car threads)
3664                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3665       (setq threads (cdr threads)))
3666     result))
3667
3668 (defun gnus-thread-loop-p (root thread)
3669   "Say whether ROOT is in THREAD."
3670   (let ((stack (list thread))
3671         (infloop 0)
3672         th)
3673     (while (setq thread (pop stack))
3674       (setq th (cdr thread))
3675       (while (and th
3676                   (not (eq (caar th) root)))
3677         (pop th))
3678       (if th
3679           ;; We have found a loop.
3680           (let (ref-dep)
3681             (setcdr thread (delq (car th) (cdr thread)))
3682             (if (boundp (setq ref-dep (intern "none"
3683                                               gnus-newsgroup-dependencies)))
3684                 (setcdr (symbol-value ref-dep)
3685                         (nconc (cdr (symbol-value ref-dep))
3686                                (list (car th))))
3687               (set ref-dep (list nil (car th))))
3688             (setq infloop 1
3689                   stack nil))
3690         ;; Push all the subthreads onto the stack.
3691         (push (cdr thread) stack)))
3692     infloop))
3693
3694 (defun gnus-make-threads ()
3695   "Go through the dependency hashtb and find the roots.  Return all threads."
3696   (let (threads)
3697     (while (catch 'infloop
3698              (mapatoms
3699               (lambda (refs)
3700                 ;; Deal with self-referencing References loops.
3701                 (when (and (car (symbol-value refs))
3702                            (not (zerop
3703                                  (apply
3704                                   '+
3705                                   (mapcar
3706                                    (lambda (thread)
3707                                      (gnus-thread-loop-p
3708                                       (car (symbol-value refs)) thread))
3709                                    (cdr (symbol-value refs)))))))
3710                   (setq threads nil)
3711                   (throw 'infloop t))
3712                 (unless (car (symbol-value refs))
3713                   ;; These threads do not refer back to any other
3714                   ;; articles, so they're roots.
3715                   (setq threads (append (cdr (symbol-value refs)) threads))))
3716               gnus-newsgroup-dependencies)))
3717     threads))
3718
3719 ;; Build the thread tree.
3720 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3721   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3722
3723 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3724 if it was already present.
3725
3726 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3727 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3728 Message-IDs will be renamed to a unique Message-ID before being
3729 entered.
3730
3731 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3732   (let* ((id (mail-header-id header))
3733          (id-dep (and id (intern id dependencies)))
3734          parent-id ref ref-dep ref-header replaced)
3735     ;; Enter this `header' in the `dependencies' table.
3736     (cond
3737      ((not id-dep)
3738       (setq header nil))
3739      ;; The first two cases do the normal part: enter a new `header'
3740      ;; in the `dependencies' table.
3741      ((not (boundp id-dep))
3742       (set id-dep (list header)))
3743      ((null (car (symbol-value id-dep)))
3744       (setcar (symbol-value id-dep) header))
3745
3746      ;; From here the `header' was already present in the
3747      ;; `dependencies' table.
3748      (force-new
3749       ;; Overrides an existing entry;
3750       ;; just set the header part of the entry.
3751       (setcar (symbol-value id-dep) header)
3752       (setq replaced t))
3753
3754      ;; Renames the existing `header' to a unique Message-ID.
3755      ((not gnus-summary-ignore-duplicates)
3756       ;; An article with this Message-ID has already been seen.
3757       ;; We rename the Message-ID.
3758       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3759            (list header))
3760       (mail-header-set-id header id))
3761
3762      ;; The last case ignores an existing entry, except it adds any
3763      ;; additional Xrefs (in case the two articles came from different
3764      ;; servers.
3765      ;; Also sets `header' to `nil' meaning that the `dependencies'
3766      ;; table was *not* modified.
3767      (t
3768       (mail-header-set-xref
3769        (car (symbol-value id-dep))
3770        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3771                    "")
3772                (or (mail-header-xref header) "")))
3773       (setq header nil)))
3774
3775     (when (and header (not replaced))
3776       ;; First check that we are not creating a References loop.
3777       (setq parent-id (gnus-parent-id (mail-header-references header)))
3778       (setq ref parent-id)
3779       (while (and ref
3780                   (setq ref-dep (intern-soft ref dependencies))
3781                   (boundp ref-dep)
3782                   (setq ref-header (car (symbol-value ref-dep))))
3783         (if (string= id ref)
3784             ;; Yuk!  This is a reference loop.  Make the article be a
3785             ;; root article.
3786             (progn
3787               (mail-header-set-references (car (symbol-value id-dep)) "none")
3788               (setq ref nil)
3789               (setq parent-id nil))
3790           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3791       (setq ref-dep (intern (or parent-id "none") dependencies))
3792       (if (boundp ref-dep)
3793           (setcdr (symbol-value ref-dep)
3794                   (nconc (cdr (symbol-value ref-dep))
3795                          (list (symbol-value id-dep))))
3796         (set ref-dep (list nil (symbol-value id-dep)))))
3797     header))
3798
3799 (defun gnus-extract-message-id-from-in-reply-to (string)
3800   (if (string-match "<[^>]+>" string)
3801       (substring string (match-beginning 0) (match-end 0))
3802     nil))
3803
3804 (defun gnus-build-sparse-threads ()
3805   (let ((headers gnus-newsgroup-headers)
3806         (mail-parse-charset gnus-newsgroup-charset)
3807         (gnus-summary-ignore-duplicates t)
3808         header references generation relations
3809         subject child end new-child date)
3810     ;; First we create an alist of generations/relations, where
3811     ;; generations is how much we trust the relation, and the relation
3812     ;; is parent/child.
3813     (gnus-message 7 "Making sparse threads...")
3814     (save-excursion
3815       (nnheader-set-temp-buffer " *gnus sparse threads*")
3816       (while (setq header (pop headers))
3817         (when (and (setq references (mail-header-references header))
3818                    (not (string= references "")))
3819           (insert references)
3820           (setq child (mail-header-id header)
3821                 subject (mail-header-subject header)
3822                 date (mail-header-date header)
3823                 generation 0)
3824           (while (search-backward ">" nil t)
3825             (setq end (1+ (point)))
3826             (when (search-backward "<" nil t)
3827               (setq new-child (buffer-substring (point) end))
3828               (push (list (incf generation)
3829                           child (setq child new-child)
3830                           subject date)
3831                     relations)))
3832           (when child
3833             (push (list (1+ generation) child nil subject) relations))
3834           (erase-buffer)))
3835       (kill-buffer (current-buffer)))
3836     ;; Sort over trustworthiness.
3837     (mapcar
3838      (lambda (relation)
3839        (when (gnus-dependencies-add-header
3840               (make-full-mail-header
3841                gnus-reffed-article-number
3842                (nth 3 relation) "" (or (nth 4 relation) "")
3843                (nth 1 relation)
3844                (or (nth 2 relation) "") 0 0 "")
3845               gnus-newsgroup-dependencies nil)
3846          (push gnus-reffed-article-number gnus-newsgroup-limit)
3847          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3848          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3849                gnus-newsgroup-reads)
3850          (decf gnus-reffed-article-number)))
3851      (sort relations 'car-less-than-car))
3852     (gnus-message 7 "Making sparse threads...done")))
3853
3854 (defun gnus-build-old-threads ()
3855   ;; Look at all the articles that refer back to old articles, and
3856   ;; fetch the headers for the articles that aren't there.  This will
3857   ;; build complete threads - if the roots haven't been expired by the
3858   ;; server, that is.
3859   (let ((mail-parse-charset gnus-newsgroup-charset)
3860         id heads)
3861     (mapatoms
3862      (lambda (refs)
3863        (when (not (car (symbol-value refs)))
3864          (setq heads (cdr (symbol-value refs)))
3865          (while heads
3866            (if (memq (mail-header-number (caar heads))
3867                      gnus-newsgroup-dormant)
3868                (setq heads (cdr heads))
3869              (setq id (symbol-name refs))
3870              (while (and (setq id (gnus-build-get-header id))
3871                          (not (car (gnus-id-to-thread id)))))
3872              (setq heads nil)))))
3873      gnus-newsgroup-dependencies)))
3874
3875 ;; This function has to be called with point after the article number
3876 ;; on the beginning of the line.
3877 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3878   (let ((eol (gnus-point-at-eol))
3879         (buffer (current-buffer))
3880         header references in-reply-to)
3881
3882     ;; overview: [num subject from date id refs chars lines misc]
3883     (unwind-protect
3884         (let (x)
3885           (narrow-to-region (point) eol)
3886           (unless (eobp)
3887             (forward-char))
3888
3889           (setq header
3890                 (make-full-mail-header
3891                  number                 ; number
3892                  (condition-case ()     ; subject
3893                      (funcall gnus-decode-encoded-word-function
3894                               (setq x (nnheader-nov-field)))
3895                    (error x))
3896                  (condition-case ()     ; from
3897                      (funcall gnus-decode-encoded-word-function
3898                               (setq x (nnheader-nov-field)))
3899                    (error x))
3900                  (nnheader-nov-field)   ; date
3901                  (nnheader-nov-read-message-id) ; id
3902                  (setq references (nnheader-nov-field)) ; refs
3903                  (nnheader-nov-read-integer) ; chars
3904                  (nnheader-nov-read-integer) ; lines
3905                  (unless (eobp)
3906                    (if (looking-at "Xref: ")
3907                        (goto-char (match-end 0)))
3908                    (nnheader-nov-field)) ; Xref
3909                  (nnheader-nov-parse-extra)))) ; extra
3910
3911       (widen))
3912
3913     (when (and (string= references "")
3914                (setq in-reply-to (mail-header-extra header))
3915                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3916       (mail-header-set-references
3917        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3918
3919     (when gnus-alter-header-function
3920       (funcall gnus-alter-header-function header))
3921     (gnus-dependencies-add-header header dependencies force-new)))
3922
3923 (defun gnus-build-get-header (id)
3924   "Look through the buffer of NOV lines and find the header to ID.
3925 Enter this line into the dependencies hash table, and return
3926 the id of the parent article (if any)."
3927   (let ((deps gnus-newsgroup-dependencies)
3928         found header)
3929     (prog1
3930         (save-excursion
3931           (set-buffer nntp-server-buffer)
3932           (let ((case-fold-search nil))
3933             (goto-char (point-min))
3934             (while (and (not found)
3935                         (search-forward id nil t))
3936               (beginning-of-line)
3937               (setq found (looking-at
3938                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3939                                    (regexp-quote id))))
3940               (or found (beginning-of-line 2)))
3941             (when found
3942               (beginning-of-line)
3943               (and
3944                (setq header (gnus-nov-parse-line
3945                              (read (current-buffer)) deps))
3946                (gnus-parent-id (mail-header-references header))))))
3947       (when header
3948         (let ((number (mail-header-number header)))
3949           (push number gnus-newsgroup-limit)
3950           (push header gnus-newsgroup-headers)
3951           (if (memq number gnus-newsgroup-unselected)
3952               (progn
3953                 (setq gnus-newsgroup-unreads
3954                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3955                                                number))
3956                 (setq gnus-newsgroup-unselected
3957                       (delq number gnus-newsgroup-unselected)))
3958             (push number gnus-newsgroup-ancient)))))))
3959
3960 (defun gnus-build-all-threads ()
3961   "Read all the headers."
3962   (let ((gnus-summary-ignore-duplicates t)
3963         (mail-parse-charset gnus-newsgroup-charset)
3964         (dependencies gnus-newsgroup-dependencies)
3965         header article)
3966     (save-excursion
3967       (set-buffer nntp-server-buffer)
3968       (let ((case-fold-search nil))
3969         (goto-char (point-min))
3970         (while (not (eobp))
3971           (ignore-errors
3972             (setq article (read (current-buffer))
3973                   header (gnus-nov-parse-line article dependencies)))
3974           (when header
3975             (save-excursion
3976               (set-buffer gnus-summary-buffer)
3977               (push header gnus-newsgroup-headers)
3978               (if (memq (setq article (mail-header-number header))
3979                         gnus-newsgroup-unselected)
3980                   (progn
3981                     (setq gnus-newsgroup-unreads
3982                           (gnus-add-to-sorted-list
3983                            gnus-newsgroup-unreads article))
3984                     (setq gnus-newsgroup-unselected
3985                           (delq article gnus-newsgroup-unselected)))
3986                 (push article gnus-newsgroup-ancient)))
3987             (forward-line 1)))))))
3988
3989 (defun gnus-summary-update-article-line (article header)
3990   "Update the line for ARTICLE using HEADERS."
3991   (let* ((id (mail-header-id header))
3992          (thread (gnus-id-to-thread id)))
3993     (unless thread
3994       (error "Article in no thread"))
3995     ;; Update the thread.
3996     (setcar thread header)
3997     (gnus-summary-goto-subject article)
3998     (let* ((datal (gnus-data-find-list article))
3999            (data (car datal))
4000            (length (when (cdr datal)
4001                      (- (gnus-data-pos data)
4002                         (gnus-data-pos (cadr datal)))))
4003            (buffer-read-only nil)
4004            (level (gnus-summary-thread-level)))
4005       (gnus-delete-line)
4006       (gnus-summary-insert-line
4007        header level nil 
4008        (memq article gnus-newsgroup-undownloaded)
4009        (gnus-article-mark article)
4010        (memq article gnus-newsgroup-replied)
4011        (memq article gnus-newsgroup-expirable)
4012        ;; Only insert the Subject string when it's different
4013        ;; from the previous Subject string.
4014        (if (and
4015             gnus-show-threads
4016             (gnus-subject-equal
4017              (condition-case ()
4018                  (mail-header-subject
4019                   (gnus-data-header
4020                    (cadr
4021                     (gnus-data-find-list
4022                      article
4023                      (gnus-data-list t)))))
4024                ;; Error on the side of excessive subjects.
4025                (error ""))
4026              (mail-header-subject header)))
4027            ""
4028          (mail-header-subject header))
4029        nil (cdr (assq article gnus-newsgroup-scored))
4030        (memq article gnus-newsgroup-processable))
4031       (when length
4032         (gnus-data-update-list
4033          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4034
4035 (defun gnus-summary-update-article (article &optional iheader)
4036   "Update ARTICLE in the summary buffer."
4037   (set-buffer gnus-summary-buffer)
4038   (let* ((header (gnus-summary-article-header article))
4039          (id (mail-header-id header))
4040          (data (gnus-data-find article))
4041          (thread (gnus-id-to-thread id))
4042          (references (mail-header-references header))
4043          (parent
4044           (gnus-id-to-thread
4045            (or (gnus-parent-id
4046                 (when (and references
4047                            (not (equal "" references)))
4048                   references))
4049                "none")))
4050          (buffer-read-only nil)
4051          (old (car thread)))
4052     (when thread
4053       (unless iheader
4054         (setcar thread nil)
4055         (when parent
4056           (delq thread parent)))
4057       (if (gnus-summary-insert-subject id header)
4058           ;; Set the (possibly) new article number in the data structure.
4059           (gnus-data-set-number data (gnus-id-to-article id))
4060         (setcar thread old)
4061         nil))))
4062
4063 (defun gnus-rebuild-thread (id &optional line)
4064   "Rebuild the thread containing ID.
4065 If LINE, insert the rebuilt thread starting on line LINE."
4066   (let ((buffer-read-only nil)
4067         old-pos current thread data)
4068     (if (not gnus-show-threads)
4069         (setq thread (list (car (gnus-id-to-thread id))))
4070       ;; Get the thread this article is part of.
4071       (setq thread (gnus-remove-thread id)))
4072     (setq old-pos (gnus-point-at-bol))
4073     (setq current (save-excursion
4074                     (and (re-search-backward "[\r\n]" nil t)
4075                          (gnus-summary-article-number))))
4076     ;; If this is a gathered thread, we have to go some re-gathering.
4077     (when (stringp (car thread))
4078       (let ((subject (car thread))
4079             roots thr)
4080         (setq thread (cdr thread))
4081         (while thread
4082           (unless (memq (setq thr (gnus-id-to-thread
4083                                    (gnus-root-id
4084                                     (mail-header-id (caar thread)))))
4085                         roots)
4086             (push thr roots))
4087           (setq thread (cdr thread)))
4088         ;; We now have all (unique) roots.
4089         (if (= (length roots) 1)
4090             ;; All the loose roots are now one solid root.
4091             (setq thread (car roots))
4092           (setq thread (cons subject (gnus-sort-threads roots))))))
4093     (let (threads)
4094       ;; We then insert this thread into the summary buffer.
4095       (when line
4096         (goto-char (point-min))
4097         (forward-line (1- line)))
4098       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4099         (if gnus-show-threads
4100             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4101           (gnus-summary-prepare-unthreaded thread))
4102         (setq data (nreverse gnus-newsgroup-data))
4103         (setq threads gnus-newsgroup-threads))
4104       ;; We splice the new data into the data structure.
4105       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4106       ;;!!! then we want to insert at the beginning of the buffer.
4107       ;;!!! That happens to be true with Gnus now, but that may
4108       ;;!!! change in the future.  Perhaps.
4109       (gnus-data-enter-list
4110        (if line nil current) data (- (point) old-pos))
4111       (setq gnus-newsgroup-threads
4112             (nconc threads gnus-newsgroup-threads))
4113       (gnus-data-compute-positions))))
4114
4115 (defun gnus-number-to-header (number)
4116   "Return the header for article NUMBER."
4117   (let ((headers gnus-newsgroup-headers))
4118     (while (and headers
4119                 (not (= number (mail-header-number (car headers)))))
4120       (pop headers))
4121     (when headers
4122       (car headers))))
4123
4124 (defun gnus-parent-headers (in-headers &optional generation)
4125   "Return the headers of the GENERATIONeth parent of HEADERS."
4126   (unless generation
4127     (setq generation 1))
4128   (let ((parent t)
4129         (headers in-headers)
4130         references)
4131     (while (and parent
4132                 (not (zerop generation))
4133                 (setq references (mail-header-references headers)))
4134       (setq headers (if (and references
4135                              (setq parent (gnus-parent-id references)))
4136                         (car (gnus-id-to-thread parent))
4137                       nil))
4138       (decf generation))
4139     (and (not (eq headers in-headers))
4140          headers)))
4141
4142 (defun gnus-id-to-thread (id)
4143   "Return the (sub-)thread where ID appears."
4144   (gnus-gethash id gnus-newsgroup-dependencies))
4145
4146 (defun gnus-id-to-article (id)
4147   "Return the article number of ID."
4148   (let ((thread (gnus-id-to-thread id)))
4149     (when (and thread
4150                (car thread))
4151       (mail-header-number (car thread)))))
4152
4153 (defun gnus-id-to-header (id)
4154   "Return the article headers of ID."
4155   (car (gnus-id-to-thread id)))
4156
4157 (defun gnus-article-displayed-root-p (article)
4158   "Say whether ARTICLE is a root(ish) article."
4159   (let ((level (gnus-summary-thread-level article))
4160         (refs (mail-header-references  (gnus-summary-article-header article)))
4161         particle)
4162     (cond
4163      ((null level) nil)
4164      ((zerop level) t)
4165      ((null refs) t)
4166      ((null (gnus-parent-id refs)) t)
4167      ((and (= 1 level)
4168            (null (setq particle (gnus-id-to-article
4169                                  (gnus-parent-id refs))))
4170            (null (gnus-summary-thread-level particle)))))))
4171
4172 (defun gnus-root-id (id)
4173   "Return the id of the root of the thread where ID appears."
4174   (let (last-id prev)
4175     (while (and id (setq prev (car (gnus-id-to-thread id))))
4176       (setq last-id id
4177             id (gnus-parent-id (mail-header-references prev))))
4178     last-id))
4179
4180 (defun gnus-articles-in-thread (thread)
4181   "Return the list of articles in THREAD."
4182   (cons (mail-header-number (car thread))
4183         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4184
4185 (defun gnus-remove-thread (id &optional dont-remove)
4186   "Remove the thread that has ID in it."
4187   (let (headers thread last-id)
4188     ;; First go up in this thread until we find the root.
4189     (setq last-id (gnus-root-id id)
4190           headers (message-flatten-list (gnus-id-to-thread last-id)))
4191     ;; We have now found the real root of this thread.  It might have
4192     ;; been gathered into some loose thread, so we have to search
4193     ;; through the threads to find the thread we wanted.
4194     (let ((threads gnus-newsgroup-threads)
4195           sub)
4196       (while threads
4197         (setq sub (car threads))
4198         (if (stringp (car sub))
4199             ;; This is a gathered thread, so we look at the roots
4200             ;; below it to find whether this article is in this
4201             ;; gathered root.
4202             (progn
4203               (setq sub (cdr sub))
4204               (while sub
4205                 (when (member (caar sub) headers)
4206                   (setq thread (car threads)
4207                         threads nil
4208                         sub nil))
4209                 (setq sub (cdr sub))))
4210           ;; It's an ordinary thread, so we check it.
4211           (when (eq (car sub) (car headers))
4212             (setq thread sub
4213                   threads nil)))
4214         (setq threads (cdr threads)))
4215       ;; If this article is in no thread, then it's a root.
4216       (if thread
4217           (unless dont-remove
4218             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4219         (setq thread (gnus-id-to-thread last-id)))
4220       (when thread
4221         (prog1
4222             thread                      ; We return this thread.
4223           (unless dont-remove
4224             (if (stringp (car thread))
4225                 (progn
4226                   ;; If we use dummy roots, then we have to remove the
4227                   ;; dummy root as well.
4228                   (when (eq gnus-summary-make-false-root 'dummy)
4229                     ;; We go to the dummy root by going to
4230                     ;; the first sub-"thread", and then one line up.
4231                     (gnus-summary-goto-article
4232                      (mail-header-number (caadr thread)))
4233                     (forward-line -1)
4234                     (gnus-delete-line)
4235                     (gnus-data-compute-positions))
4236                   (setq thread (cdr thread))
4237                   (while thread
4238                     (gnus-remove-thread-1 (car thread))
4239                     (setq thread (cdr thread))))
4240               (gnus-remove-thread-1 thread))))))))
4241
4242 (defun gnus-remove-thread-1 (thread)
4243   "Remove the thread THREAD recursively."
4244   (let ((number (mail-header-number (pop thread)))
4245         d)
4246     (setq thread (reverse thread))
4247     (while thread
4248       (gnus-remove-thread-1 (pop thread)))
4249     (when (setq d (gnus-data-find number))
4250       (goto-char (gnus-data-pos d))
4251       (gnus-summary-show-thread)
4252       (gnus-data-remove
4253        number
4254        (- (gnus-point-at-bol)
4255           (prog1
4256               (1+ (gnus-point-at-eol))
4257             (gnus-delete-line)))))))
4258
4259 (defun gnus-sort-threads-1 (threads func)
4260   (sort (mapcar (lambda (thread)
4261                   (cons (car thread)
4262                         (and (cdr thread)
4263                              (gnus-sort-threads-1 (cdr thread) func))))
4264                 threads) func))
4265
4266 (defun gnus-sort-threads (threads)
4267   "Sort THREADS."
4268   (if (not gnus-thread-sort-functions)
4269       threads
4270     (gnus-message 8 "Sorting threads...")
4271     (let ((max-lisp-eval-depth 5000))
4272       (prog1 (gnus-sort-threads-1
4273          threads
4274          (gnus-make-sort-function gnus-thread-sort-functions))
4275         (gnus-message 8 "Sorting threads...done")))))
4276
4277 (defun gnus-sort-articles (articles)
4278   "Sort ARTICLES."
4279   (when gnus-article-sort-functions
4280     (gnus-message 7 "Sorting articles...")
4281     (prog1
4282         (setq gnus-newsgroup-headers
4283               (sort articles (gnus-make-sort-function
4284                               gnus-article-sort-functions)))
4285       (gnus-message 7 "Sorting articles...done"))))
4286
4287 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4288 (defmacro gnus-thread-header (thread)
4289   "Return header of first article in THREAD.
4290 Note that THREAD must never, ever be anything else than a variable -
4291 using some other form will lead to serious barfage."
4292   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4293   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4294   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4295         (vector thread) 2))
4296
4297 (defsubst gnus-article-sort-by-number (h1 h2)
4298   "Sort articles by article number."
4299   (< (mail-header-number h1)
4300      (mail-header-number h2)))
4301
4302 (defun gnus-thread-sort-by-number (h1 h2)
4303   "Sort threads by root article number."
4304   (gnus-article-sort-by-number
4305    (gnus-thread-header h1) (gnus-thread-header h2)))
4306
4307 (defsubst gnus-article-sort-by-random (h1 h2)
4308   "Sort articles by article number."
4309   (zerop (random 2)))
4310
4311 (defun gnus-thread-sort-by-random (h1 h2)
4312   "Sort threads by root article number."
4313   (gnus-article-sort-by-random
4314    (gnus-thread-header h1) (gnus-thread-header h2)))
4315
4316 (defsubst gnus-article-sort-by-lines (h1 h2)
4317   "Sort articles by article Lines header."
4318   (< (mail-header-lines h1)
4319      (mail-header-lines h2)))
4320
4321 (defun gnus-thread-sort-by-lines (h1 h2)
4322   "Sort threads by root article Lines header."
4323   (gnus-article-sort-by-lines
4324    (gnus-thread-header h1) (gnus-thread-header h2)))
4325
4326 (defsubst gnus-article-sort-by-chars (h1 h2)
4327   "Sort articles by octet length."
4328   (< (mail-header-chars h1)
4329      (mail-header-chars h2)))
4330
4331 (defun gnus-thread-sort-by-chars (h1 h2)
4332   "Sort threads by root article octet length."
4333   (gnus-article-sort-by-chars
4334    (gnus-thread-header h1) (gnus-thread-header h2)))
4335
4336 (defsubst gnus-article-sort-by-author (h1 h2)
4337   "Sort articles by root author."
4338   (string-lessp
4339    (let ((extract (funcall
4340                    gnus-extract-address-components
4341                    (mail-header-from h1))))
4342      (or (car extract) (cadr extract) ""))
4343    (let ((extract (funcall
4344                    gnus-extract-address-components
4345                    (mail-header-from h2))))
4346      (or (car extract) (cadr extract) ""))))
4347
4348 (defun gnus-thread-sort-by-author (h1 h2)
4349   "Sort threads by root author."
4350   (gnus-article-sort-by-author
4351    (gnus-thread-header h1)  (gnus-thread-header h2)))
4352
4353 (defsubst gnus-article-sort-by-subject (h1 h2)
4354   "Sort articles by root subject."
4355   (string-lessp
4356    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4357    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4358
4359 (defun gnus-thread-sort-by-subject (h1 h2)
4360   "Sort threads by root subject."
4361   (gnus-article-sort-by-subject
4362    (gnus-thread-header h1) (gnus-thread-header h2)))
4363
4364 (defsubst gnus-article-sort-by-date (h1 h2)
4365   "Sort articles by root article date."
4366   (time-less-p
4367    (gnus-date-get-time (mail-header-date h1))
4368    (gnus-date-get-time (mail-header-date h2))))
4369
4370 (defun gnus-thread-sort-by-date (h1 h2)
4371   "Sort threads by root article date."
4372   (gnus-article-sort-by-date
4373    (gnus-thread-header h1) (gnus-thread-header h2)))
4374
4375 (defsubst gnus-article-sort-by-score (h1 h2)
4376   "Sort articles by root article score.
4377 Unscored articles will be counted as having a score of zero."
4378   (> (or (cdr (assq (mail-header-number h1)
4379                     gnus-newsgroup-scored))
4380          gnus-summary-default-score 0)
4381      (or (cdr (assq (mail-header-number h2)
4382                     gnus-newsgroup-scored))
4383          gnus-summary-default-score 0)))
4384
4385 (defun gnus-thread-sort-by-score (h1 h2)
4386   "Sort threads by root article score."
4387   (gnus-article-sort-by-score
4388    (gnus-thread-header h1) (gnus-thread-header h2)))
4389
4390 (defun gnus-thread-sort-by-total-score (h1 h2)
4391   "Sort threads by the sum of all scores in the thread.
4392 Unscored articles will be counted as having a score of zero."
4393   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4394
4395 (defun gnus-thread-total-score (thread)
4396   ;; This function find the total score of THREAD.
4397   (cond
4398    ((null thread)
4399     0)
4400    ((consp thread)
4401     (if (stringp (car thread))
4402         (apply gnus-thread-score-function 0
4403                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4404       (gnus-thread-total-score-1 thread)))
4405    (t
4406     (gnus-thread-total-score-1 (list thread)))))
4407
4408 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4409   "Sort threads such that the thread with the most recently arrived article comes first."
4410   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4411
4412 (defun gnus-thread-highest-number (thread)
4413   "Return the highest article number in THREAD."
4414   (apply 'max (mapcar (lambda (header)
4415                         (mail-header-number header))
4416                       (message-flatten-list thread))))
4417
4418 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4419   "Sort threads such that the thread with the most recently dated article comes first."
4420   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4421
4422 (defun gnus-thread-latest-date (thread)
4423   "Return the highest article date in THREAD."
4424   (let ((previous-time 0))
4425     (apply 'max
4426            (mapcar
4427             (lambda (header)
4428               (setq previous-time
4429                     (time-to-seconds
4430                      (condition-case ()
4431                          (mail-header-parse-date (mail-header-date header))
4432                        (error previous-time)))))
4433             (sort
4434              (message-flatten-list thread)
4435              (lambda (h1 h2)
4436                (< (mail-header-number h1)
4437                   (mail-header-number h2))))))))
4438
4439 (defun gnus-thread-total-score-1 (root)
4440   ;; This function find the total score of the thread below ROOT.
4441   (setq root (car root))
4442   (apply gnus-thread-score-function
4443          (or (append
4444               (mapcar 'gnus-thread-total-score
4445                       (cdr (gnus-id-to-thread (mail-header-id root))))
4446               (when (> (mail-header-number root) 0)
4447                 (list (or (cdr (assq (mail-header-number root)
4448                                      gnus-newsgroup-scored))
4449                           gnus-summary-default-score 0))))
4450              (list gnus-summary-default-score)
4451              '(0))))
4452
4453 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4454 (defvar gnus-tmp-prev-subject nil)
4455 (defvar gnus-tmp-false-parent nil)
4456 (defvar gnus-tmp-root-expunged nil)
4457 (defvar gnus-tmp-dummy-line nil)
4458
4459 (eval-when-compile (defvar gnus-tmp-header))
4460 (defun gnus-extra-header (type &optional header)
4461   "Return the extra header of TYPE."
4462   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4463       ""))
4464
4465 (defvar gnus-tmp-thread-tree-header-string "")
4466
4467 (defcustom gnus-sum-thread-tree-root "> "
4468   "With %B spec, used for the root of a thread.
4469 If nil, use subject instead."
4470   :type 'string
4471   :group 'gnus-thread)
4472 (defcustom gnus-sum-thread-tree-single-indent ""
4473   "With %B spec, used for a thread with just one message.
4474 If nil, use subject instead."
4475   :type 'string
4476   :group 'gnus-thread)
4477 (defcustom gnus-sum-thread-tree-vertical "| "
4478   "With %B spec, used for drawing a vertical line."
4479   :type 'string
4480   :group 'gnus-thread)
4481 (defcustom gnus-sum-thread-tree-indent "  "
4482   "With %B spec, used for indenting."
4483   :type 'string
4484   :group 'gnus-thread)
4485 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4486   "With %B spec, used for a leaf with brothers."
4487   :type 'string
4488   :group 'gnus-thread)
4489 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4490   "With %B spec, used for a leaf without brothers."
4491   :type 'string
4492   :group 'gnus-thread)
4493
4494 (defun gnus-summary-prepare-threads (threads)
4495   "Prepare summary buffer from THREADS and indentation LEVEL.
4496 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4497 or a straight list of headers."
4498   (gnus-message 7 "Generating summary...")
4499
4500   (setq gnus-newsgroup-threads threads)
4501   (beginning-of-line)
4502
4503   (let ((gnus-tmp-level 0)
4504         (default-score (or gnus-summary-default-score 0))
4505         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4506         (building-line-count gnus-summary-display-while-building)
4507         (building-count (integerp gnus-summary-display-while-building))
4508         thread number subject stack state gnus-tmp-gathered beg-match
4509         new-roots gnus-tmp-new-adopts thread-end simp-subject
4510         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4511         gnus-tmp-replied gnus-tmp-subject-or-nil
4512         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4513         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4514         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4515         tree-stack)
4516
4517     (setq gnus-tmp-prev-subject nil)
4518
4519     (if (vectorp (car threads))
4520         ;; If this is a straight (sic) list of headers, then a
4521         ;; threaded summary display isn't required, so we just create
4522         ;; an unthreaded one.
4523         (gnus-summary-prepare-unthreaded threads)
4524
4525       ;; Do the threaded display.
4526
4527       (if gnus-summary-display-while-building
4528           (switch-to-buffer (buffer-name)))
4529       (while (or threads stack gnus-tmp-new-adopts new-roots)
4530
4531         (if (and (= gnus-tmp-level 0)
4532                  (or (not stack)
4533                      (= (caar stack) 0))
4534                  (not gnus-tmp-false-parent)
4535                  (or gnus-tmp-new-adopts new-roots))
4536             (if gnus-tmp-new-adopts
4537                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4538                       thread (list (car gnus-tmp-new-adopts))
4539                       gnus-tmp-header (caar thread)
4540                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4541               (when new-roots
4542                 (setq thread (list (car new-roots))
4543                       gnus-tmp-header (caar thread)
4544                       new-roots (cdr new-roots))))
4545
4546           (if threads
4547               ;; If there are some threads, we do them before the
4548               ;; threads on the stack.
4549               (setq thread threads
4550                     gnus-tmp-header (caar thread))
4551             ;; There were no current threads, so we pop something off
4552             ;; the stack.
4553             (setq state (car stack)
4554                   gnus-tmp-level (car state)
4555                   tree-stack (cadr state)
4556                   thread (caddr state)
4557                   stack (cdr stack)
4558                   gnus-tmp-header (caar thread))))
4559
4560         (setq gnus-tmp-false-parent nil)
4561         (setq gnus-tmp-root-expunged nil)
4562         (setq thread-end nil)
4563
4564         (if (stringp gnus-tmp-header)
4565             ;; The header is a dummy root.
4566             (cond
4567              ((eq gnus-summary-make-false-root 'adopt)
4568               ;; We let the first article adopt the rest.
4569               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4570                                                (cddar thread)))
4571               (setq gnus-tmp-gathered
4572                     (nconc (mapcar
4573                             (lambda (h) (mail-header-number (car h)))
4574                             (cddar thread))
4575                            gnus-tmp-gathered))
4576               (setq thread (cons (list (caar thread)
4577                                        (cadar thread))
4578                                  (cdr thread)))
4579               (setq gnus-tmp-level -1
4580                     gnus-tmp-false-parent t))
4581              ((eq gnus-summary-make-false-root 'empty)
4582               ;; We print adopted articles with empty subject fields.
4583               (setq gnus-tmp-gathered
4584                     (nconc (mapcar
4585                             (lambda (h) (mail-header-number (car h)))
4586                             (cddar thread))
4587                            gnus-tmp-gathered))
4588               (setq gnus-tmp-level -1))
4589              ((eq gnus-summary-make-false-root 'dummy)
4590               ;; We remember that we probably want to output a dummy
4591               ;; root.
4592               (setq gnus-tmp-dummy-line gnus-tmp-header)
4593               (setq gnus-tmp-prev-subject gnus-tmp-header))
4594              (t
4595               ;; We do not make a root for the gathered
4596               ;; sub-threads at all.
4597               (setq gnus-tmp-level -1)))
4598
4599           (setq number (mail-header-number gnus-tmp-header)
4600                 subject (mail-header-subject gnus-tmp-header)
4601                 simp-subject (gnus-simplify-subject-fully subject))
4602
4603           (cond
4604            ;; If the thread has changed subject, we might want to make
4605            ;; this subthread into a root.
4606            ((and (null gnus-thread-ignore-subject)
4607                  (not (zerop gnus-tmp-level))
4608                  gnus-tmp-prev-subject
4609                  (not (string= gnus-tmp-prev-subject simp-subject)))
4610             (setq new-roots (nconc new-roots (list (car thread)))
4611                   thread-end t
4612                   gnus-tmp-header nil))
4613            ;; If the article lies outside the current limit,
4614            ;; then we do not display it.
4615            ((not (memq number gnus-newsgroup-limit))
4616             (setq gnus-tmp-gathered
4617                   (nconc (mapcar
4618                           (lambda (h) (mail-header-number (car h)))
4619                           (cdar thread))
4620                          gnus-tmp-gathered))
4621             (setq gnus-tmp-new-adopts (if (cdar thread)
4622                                           (append gnus-tmp-new-adopts
4623                                                   (cdar thread))
4624                                         gnus-tmp-new-adopts)
4625                   thread-end t
4626                   gnus-tmp-header nil)
4627             (when (zerop gnus-tmp-level)
4628               (setq gnus-tmp-root-expunged t)))
4629            ;; Perhaps this article is to be marked as read?
4630            ((and gnus-summary-mark-below
4631                  (< (or (cdr (assq number gnus-newsgroup-scored))
4632                         default-score)
4633                     gnus-summary-mark-below)
4634                  ;; Don't touch sparse articles.
4635                  (not (gnus-summary-article-sparse-p number))
4636                  (not (gnus-summary-article-ancient-p number)))
4637             (setq gnus-newsgroup-unreads
4638                   (delq number gnus-newsgroup-unreads))
4639             (if gnus-newsgroup-auto-expire
4640                 (setq gnus-newsgroup-expirable
4641                       (gnus-add-to-sorted-list
4642                        gnus-newsgroup-expirable number))
4643               (push (cons number gnus-low-score-mark)
4644                     gnus-newsgroup-reads))))
4645
4646           (when gnus-tmp-header
4647             ;; We may have an old dummy line to output before this
4648             ;; article.
4649             (when (and gnus-tmp-dummy-line
4650                        (gnus-subject-equal
4651                         gnus-tmp-dummy-line
4652                         (mail-header-subject gnus-tmp-header)))
4653               (gnus-summary-insert-dummy-line
4654                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4655               (setq gnus-tmp-dummy-line nil))
4656
4657             ;; Compute the mark.
4658             (setq gnus-tmp-unread (gnus-article-mark number))
4659
4660             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4661                                   gnus-tmp-header gnus-tmp-level)
4662                   gnus-newsgroup-data)
4663
4664             ;; Actually insert the line.
4665             (setq
4666              gnus-tmp-subject-or-nil
4667              (cond
4668               ((and gnus-thread-ignore-subject
4669                     gnus-tmp-prev-subject
4670                     (not (string= gnus-tmp-prev-subject simp-subject)))
4671                subject)
4672               ((zerop gnus-tmp-level)
4673                (if (and (eq gnus-summary-make-false-root 'empty)
4674                         (memq number gnus-tmp-gathered)
4675                         gnus-tmp-prev-subject
4676                         (string= gnus-tmp-prev-subject simp-subject))
4677                    gnus-summary-same-subject
4678                  subject))
4679               (t gnus-summary-same-subject)))
4680             (if (and (eq gnus-summary-make-false-root 'adopt)
4681                      (= gnus-tmp-level 1)
4682                      (memq number gnus-tmp-gathered))
4683                 (setq gnus-tmp-opening-bracket ?\<
4684                       gnus-tmp-closing-bracket ?\>)
4685               (setq gnus-tmp-opening-bracket ?\[
4686                     gnus-tmp-closing-bracket ?\]))
4687             (setq
4688              gnus-tmp-indentation
4689              (aref gnus-thread-indent-array gnus-tmp-level)
4690              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4691              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4692                                 gnus-summary-default-score 0)
4693              gnus-tmp-score-char
4694              (if (or (null gnus-summary-default-score)
4695                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4696                          gnus-summary-zcore-fuzz))
4697                  ?                      ;Whitespace
4698                (if (< gnus-tmp-score gnus-summary-default-score)
4699                    gnus-score-below-mark gnus-score-over-mark))
4700              gnus-tmp-replied
4701              (cond ((memq number gnus-newsgroup-processable)
4702                     gnus-process-mark)
4703                    ((memq number gnus-newsgroup-cached)
4704                     gnus-cached-mark)
4705                    ((memq number gnus-newsgroup-replied)
4706                     gnus-replied-mark)
4707                    ((memq number gnus-newsgroup-forwarded)
4708                     gnus-forwarded-mark)
4709                    ((memq number gnus-newsgroup-saved)
4710                     gnus-saved-mark)
4711                    ((memq number gnus-newsgroup-recent)
4712                     gnus-recent-mark)
4713                    ((memq number gnus-newsgroup-unseen)
4714                     gnus-unseen-mark)
4715                    (t gnus-no-mark))
4716              gnus-tmp-downloaded
4717              (cond ((memq number gnus-newsgroup-undownloaded) 
4718                     gnus-undownloaded-mark)
4719                    (gnus-newsgroup-agentized
4720                     gnus-downloaded-mark)
4721                    (t
4722                     gnus-no-mark))
4723              gnus-tmp-from (mail-header-from gnus-tmp-header)
4724              gnus-tmp-name
4725              (cond
4726               ((string-match "<[^>]+> *$" gnus-tmp-from)
4727                (setq beg-match (match-beginning 0))
4728                (or (and (string-match "^\".+\"" gnus-tmp-from)
4729                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4730                    (substring gnus-tmp-from 0 beg-match)))
4731               ((string-match "(.+)" gnus-tmp-from)
4732                (substring gnus-tmp-from
4733                           (1+ (match-beginning 0)) (1- (match-end 0))))
4734               (t gnus-tmp-from))
4735              gnus-tmp-thread-tree-header-string
4736              (cond
4737               ((not gnus-show-threads) "")
4738               ((zerop gnus-tmp-level)
4739                (if (cdar thread)
4740                    (or gnus-sum-thread-tree-root subject)
4741                  (or gnus-sum-thread-tree-single-indent subject)))
4742               (t
4743                (concat (apply 'concat
4744                               (mapcar (lambda (item)
4745                                         (if (= item 1)
4746                                             gnus-sum-thread-tree-vertical
4747                                           gnus-sum-thread-tree-indent))
4748                                       (cdr (reverse tree-stack))))
4749                        (if (nth 1 thread)
4750                            gnus-sum-thread-tree-leaf-with-other
4751                          gnus-sum-thread-tree-single-leaf)))))
4752             (when (string= gnus-tmp-name "")
4753               (setq gnus-tmp-name gnus-tmp-from))
4754             (unless (numberp gnus-tmp-lines)
4755               (setq gnus-tmp-lines -1))
4756             (if (= gnus-tmp-lines -1)
4757                 (setq gnus-tmp-lines "?")
4758               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4759             (gnus-put-text-property
4760              (point)
4761              (progn (eval gnus-summary-line-format-spec) (point))
4762              'gnus-number number)
4763             (when gnus-visual-p
4764               (forward-line -1)
4765               (gnus-run-hooks 'gnus-summary-update-hook)
4766               (forward-line 1))
4767
4768             (setq gnus-tmp-prev-subject simp-subject)))
4769
4770         (when (nth 1 thread)
4771           (push (list (max 0 gnus-tmp-level)
4772                       (copy-list tree-stack)
4773                       (nthcdr 1 thread))
4774                 stack))
4775         (push (if (nth 1 thread) 1 0) tree-stack)
4776         (incf gnus-tmp-level)
4777         (setq threads (if thread-end nil (cdar thread)))
4778         (if gnus-summary-display-while-building
4779             (if building-count
4780                 (progn
4781                   ;; use a set frequency
4782                   (setq building-line-count (1- building-line-count))
4783                   (when (= building-line-count 0)
4784                     (sit-for 0)
4785                     (setq building-line-count
4786                           gnus-summary-display-while-building)))
4787               ;; always
4788               (sit-for 0)))
4789         (unless threads
4790           (setq gnus-tmp-level 0)))))
4791   (gnus-message 7 "Generating summary...done"))
4792
4793 (defun gnus-summary-prepare-unthreaded (headers)
4794   "Generate an unthreaded summary buffer based on HEADERS."
4795   (let (header number mark)
4796
4797     (beginning-of-line)
4798
4799     (while headers
4800       ;; We may have to root out some bad articles...
4801       (when (memq (setq number (mail-header-number
4802                                 (setq header (pop headers))))
4803                   gnus-newsgroup-limit)
4804         ;; Mark article as read when it has a low score.
4805         (when (and gnus-summary-mark-below
4806                    (< (or (cdr (assq number gnus-newsgroup-scored))
4807                           gnus-summary-default-score 0)
4808                       gnus-summary-mark-below)
4809                    (not (gnus-summary-article-ancient-p number)))
4810           (setq gnus-newsgroup-unreads
4811                 (delq number gnus-newsgroup-unreads))
4812           (if gnus-newsgroup-auto-expire
4813               (push number gnus-newsgroup-expirable)
4814             (push (cons number gnus-low-score-mark)
4815                   gnus-newsgroup-reads)))
4816
4817         (setq mark (gnus-article-mark number))
4818         (push (gnus-data-make number mark (1+ (point)) header 0)
4819               gnus-newsgroup-data)
4820         (gnus-summary-insert-line
4821          header 0 number
4822          (memq number gnus-newsgroup-undownloaded)
4823          mark (memq number gnus-newsgroup-replied)
4824          (memq number gnus-newsgroup-expirable)
4825          (mail-header-subject header) nil
4826          (cdr (assq number gnus-newsgroup-scored))
4827          (memq number gnus-newsgroup-processable))))))
4828
4829 (defun gnus-summary-remove-list-identifiers ()
4830   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4831   (let ((regexp (if (consp gnus-list-identifiers)
4832                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4833                   gnus-list-identifiers))
4834         changed subject)
4835     (when regexp
4836       (dolist (header gnus-newsgroup-headers)
4837         (setq subject (mail-header-subject header)
4838               changed nil)
4839         (while (string-match
4840                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4841                 subject)
4842           (setq subject
4843                 (concat (substring subject 0 (match-beginning 2))
4844                         (substring subject (match-end 0)))
4845                 changed t))
4846         (when (and changed
4847                    (string-match
4848                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4849           (setq subject
4850                 (concat (substring subject 0 (match-beginning 1))
4851                         (substring subject (match-end 1)))))
4852         (when changed
4853           (mail-header-set-subject header subject))))))
4854
4855 (defun gnus-fetch-headers (articles)
4856   "Fetch headers of ARTICLES."
4857   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4858     (gnus-message 5 "Fetching headers for %s..." name)
4859     (prog1
4860         (if (eq 'nov
4861                 (setq gnus-headers-retrieved-by
4862                       (gnus-retrieve-headers
4863                        articles gnus-newsgroup-name
4864                        ;; We might want to fetch old headers, but
4865                        ;; not if there is only 1 article.
4866                        (and (or (and
4867                                  (not (eq gnus-fetch-old-headers 'some))
4868                                  (not (numberp gnus-fetch-old-headers)))
4869                                 (> (length articles) 1))
4870                             gnus-fetch-old-headers))))
4871             (gnus-get-newsgroup-headers-xover
4872              articles nil nil gnus-newsgroup-name t)
4873           (gnus-get-newsgroup-headers))
4874       (gnus-message 5 "Fetching headers for %s...done" name))))
4875
4876 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4877   "Select newsgroup GROUP.
4878 If READ-ALL is non-nil, all articles in the group are selected.
4879 If SELECT-ARTICLES, only select those articles from GROUP."
4880   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4881          ;;!!! Dirty hack; should be removed.
4882          (gnus-summary-ignore-duplicates
4883           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4884               t
4885             gnus-summary-ignore-duplicates))
4886          (info (nth 2 entry))
4887          articles fetched-articles cached)
4888
4889     (unless (gnus-check-server
4890              (set (make-local-variable 'gnus-current-select-method)
4891                   (gnus-find-method-for-group group)))
4892       (error "Couldn't open server"))
4893
4894     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4895         (gnus-activate-group group)     ; Or we can activate it...
4896         (progn                          ; Or we bug out.
4897           (when (equal major-mode 'gnus-summary-mode)
4898             (kill-buffer (current-buffer)))
4899           (error "Couldn't activate group %s: %s"
4900                  group (gnus-status-message group))))
4901
4902     (unless (gnus-request-group group t)
4903       (when (equal major-mode 'gnus-summary-mode)
4904         (kill-buffer (current-buffer)))
4905       (error "Couldn't request group %s: %s"
4906              group (gnus-status-message group)))
4907
4908     (setq gnus-newsgroup-name group
4909           gnus-newsgroup-unselected nil
4910           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4911
4912     (let ((display (gnus-group-find-parameter group 'display)))
4913       (setq gnus-newsgroup-display
4914             (cond
4915              ((not (zerop (or (car-safe read-all) 0)))
4916               ;; The user entered the group with C-u SPC/RET, let's show
4917               ;; all articles.
4918               'gnus-not-ignore)
4919              ((eq display 'all)
4920               'gnus-not-ignore)
4921              ((arrayp display)
4922               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4923              ((numberp display)
4924               ;; The following is probably the "correct" solution, but
4925               ;; it makes Gnus fetch all headers and then limit the
4926               ;; articles (which is slow), so instead we hack the
4927               ;; select-articles parameter instead. -- Simon Josefsson
4928               ;; <jas@kth.se>
4929               ;;
4930               ;; (gnus-byte-compile
4931               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4932               ;;                         display)))))
4933               (setq select-articles
4934                     (gnus-uncompress-range
4935                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4936                              (if (> tmp 0)
4937                                  tmp
4938                                1))
4939                            (cdr (gnus-active group)))))
4940               nil)
4941              (t
4942               nil))))
4943
4944     (gnus-summary-setup-default-charset)
4945
4946     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4947     (when (gnus-virtual-group-p group)
4948       (setq cached gnus-newsgroup-cached))
4949
4950     (setq gnus-newsgroup-unreads
4951           (gnus-sorted-ndifference
4952            (gnus-sorted-ndifference gnus-newsgroup-unreads
4953                                     gnus-newsgroup-marked)
4954            gnus-newsgroup-dormant))
4955
4956     (setq gnus-newsgroup-processable nil)
4957
4958     (gnus-update-read-articles group gnus-newsgroup-unreads)
4959
4960     ;; Adjust and set lists of article marks.
4961     (when info
4962       (gnus-adjust-marked-articles info))
4963     (if (setq articles select-articles)
4964         (setq gnus-newsgroup-unselected
4965               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4966       (setq articles (gnus-articles-to-read group read-all)))
4967
4968     (cond
4969      ((null articles)
4970       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4971       'quit)
4972      ((eq articles 0) nil)
4973      (t
4974       ;; Init the dependencies hash table.
4975       (setq gnus-newsgroup-dependencies
4976             (gnus-make-hashtable (length articles)))
4977       (gnus-set-global-variables)
4978       ;; Retrieve the headers and read them in.
4979
4980       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4981
4982       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4983       (when cached
4984         (setq gnus-newsgroup-cached cached))
4985
4986       ;; Suppress duplicates?
4987       (when gnus-suppress-duplicates
4988         (gnus-dup-suppress-articles))
4989
4990       ;; Set the initial limit.
4991       (setq gnus-newsgroup-limit (copy-sequence articles))
4992       ;; Remove canceled articles from the list of unread articles.
4993       (setq fetched-articles
4994             (mapcar (lambda (headers) (mail-header-number headers))
4995                     gnus-newsgroup-headers))
4996       (setq gnus-newsgroup-articles fetched-articles)
4997       (setq gnus-newsgroup-unreads
4998             (gnus-sorted-nintersection
4999              gnus-newsgroup-unreads fetched-articles))
5000       (gnus-compute-unseen-list)
5001
5002       ;; Removed marked articles that do not exist.
5003       (gnus-update-missing-marks
5004        (gnus-sorted-difference articles fetched-articles))
5005       ;; We might want to build some more threads first.
5006       (when (and gnus-fetch-old-headers
5007                  (eq gnus-headers-retrieved-by 'nov))
5008         (if (eq gnus-fetch-old-headers 'invisible)
5009             (gnus-build-all-threads)
5010           (gnus-build-old-threads)))
5011       ;; Let the Gnus agent mark articles as read.
5012       (when gnus-agent
5013         (gnus-agent-get-undownloaded-list))
5014       ;; Remove list identifiers from subject
5015       (when gnus-list-identifiers
5016         (gnus-summary-remove-list-identifiers))
5017       ;; Check whether auto-expire is to be done in this group.
5018       (setq gnus-newsgroup-auto-expire
5019             (gnus-group-auto-expirable-p group))
5020       ;; Set up the article buffer now, if necessary.
5021       (unless gnus-single-article-buffer
5022         (gnus-article-setup-buffer))
5023       ;; First and last article in this newsgroup.
5024       (when gnus-newsgroup-headers
5025         (setq gnus-newsgroup-begin
5026               (mail-header-number (car gnus-newsgroup-headers))
5027               gnus-newsgroup-end
5028               (mail-header-number
5029                (gnus-last-element gnus-newsgroup-headers))))
5030       ;; GROUP is successfully selected.
5031       (or gnus-newsgroup-headers t)))))
5032
5033 (defun gnus-compute-unseen-list ()
5034   ;; The `seen' marks are treated specially.
5035   (if (not gnus-newsgroup-seen)
5036       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5037     (setq gnus-newsgroup-unseen
5038           (gnus-inverse-list-range-intersection
5039            gnus-newsgroup-articles gnus-newsgroup-seen))))
5040
5041 (defun gnus-summary-display-make-predicate (display)
5042   (require 'gnus-agent)
5043   (when (= (length display) 1)
5044     (setq display (car display)))
5045   (unless gnus-summary-display-cache
5046     (dolist (elem (append '((unread . unread)
5047                             (read . read)
5048                             (unseen . unseen))
5049                           gnus-article-mark-lists))
5050       (push (cons (cdr elem)
5051                   (gnus-byte-compile
5052                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5053             gnus-summary-display-cache)))
5054   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5055         (gnus-category-predicate-cache gnus-summary-display-cache))
5056     (gnus-get-predicate display)))
5057
5058 ;; Uses the dynamically bound `number' variable.
5059 (eval-when-compile
5060   (defvar number))
5061 (defun gnus-article-marked-p (type &optional article)
5062   (let ((article (or article number)))
5063     (cond
5064      ((eq type 'tick)
5065       (memq article gnus-newsgroup-marked))
5066      ((eq type 'spam)
5067       (memq article gnus-newsgroup-spam-marked))
5068      ((eq type 'unsend)
5069       (memq article gnus-newsgroup-unsendable))
5070      ((eq type 'undownload)
5071       (memq article gnus-newsgroup-undownloaded))
5072      ((eq type 'download)
5073       (memq article gnus-newsgroup-downloadable))
5074      ((eq type 'unread)
5075       (memq article gnus-newsgroup-unreads))
5076      ((eq type 'read)
5077       (memq article gnus-newsgroup-reads))
5078      ((eq type 'dormant)
5079       (memq article gnus-newsgroup-dormant) )
5080      ((eq type 'expire)
5081       (memq article gnus-newsgroup-expirable))
5082      ((eq type 'reply)
5083       (memq article gnus-newsgroup-replied))
5084      ((eq type 'killed)
5085       (memq article gnus-newsgroup-killed))
5086      ((eq type 'bookmark)
5087       (assq article gnus-newsgroup-bookmarks))
5088      ((eq type 'score)
5089       (assq article gnus-newsgroup-scored))
5090      ((eq type 'save)
5091       (memq article gnus-newsgroup-saved))
5092      ((eq type 'cache)
5093       (memq article gnus-newsgroup-cached))
5094      ((eq type 'forward)
5095       (memq article gnus-newsgroup-forwarded))
5096      ((eq type 'seen)
5097       (not (memq article gnus-newsgroup-unseen)))
5098      ((eq type 'recent)
5099       (memq article gnus-newsgroup-recent))
5100      (t t))))
5101
5102 (defun gnus-articles-to-read (group &optional read-all)
5103   "Find out what articles the user wants to read."
5104   (let* ((display (gnus-group-find-parameter group 'display))
5105          (articles
5106           ;; Select all articles if `read-all' is non-nil, or if there
5107           ;; are no unread articles.
5108           (if (or read-all
5109                   (and (zerop (length gnus-newsgroup-marked))
5110                        (zerop (length gnus-newsgroup-unreads)))
5111                   ;; Fetch all if the predicate is non-nil.
5112                   gnus-newsgroup-display)
5113               ;; We want to select the headers for all the articles in
5114               ;; the group, so we select either all the active
5115               ;; articles in the group, or (if that's nil), the
5116               ;; articles in the cache.
5117               (or
5118                (gnus-uncompress-range (gnus-active group))
5119                (gnus-cache-articles-in-group group))
5120             ;; Select only the "normal" subset of articles.
5121             (gnus-sorted-nunion
5122              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5123              gnus-newsgroup-unreads)))
5124          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5125          (scored (length scored-list))
5126          (number (length articles))
5127          (marked (+ (length gnus-newsgroup-marked)
5128                     (length gnus-newsgroup-dormant)))
5129          (select
5130           (cond
5131            ((numberp read-all)
5132             read-all)
5133            ((numberp gnus-newsgroup-display)
5134             gnus-newsgroup-display)
5135            (t
5136             (condition-case ()
5137                 (cond
5138                  ((and (or (<= scored marked) (= scored number))
5139                        (numberp gnus-large-newsgroup)
5140                        (> number gnus-large-newsgroup))
5141                   (let* ((cursor-in-echo-area nil)
5142                          (initial (gnus-parameter-large-newsgroup-initial
5143                                    gnus-newsgroup-name))
5144                          (input
5145                           (read-string
5146                            (format
5147                             "How many articles from %s (%s %d): "
5148                             (gnus-limit-string
5149                              (gnus-group-decoded-name gnus-newsgroup-name)
5150                              35)
5151                             (if initial "max" "default")
5152                             number)
5153                            (if initial
5154                                (cons (number-to-string initial)
5155                                      0)))))
5156                     (if (string-match "^[ \t]*$" input) number input)))
5157                  ((and (> scored marked) (< scored number)
5158                        (> (- scored number) 20))
5159                   (let ((input
5160                          (read-string
5161                           (format "%s %s (%d scored, %d total): "
5162                                   "How many articles from"
5163                                   (gnus-group-decoded-name group)
5164                                   scored number))))
5165                     (if (string-match "^[ \t]*$" input)
5166                         number input)))
5167                  (t number))
5168               (quit
5169                (message "Quit getting the articles to read")
5170                nil))))))
5171     (setq select (if (stringp select) (string-to-number select) select))
5172     (if (or (null select) (zerop select))
5173         select
5174       (if (and (not (zerop scored)) (<= (abs select) scored))
5175           (progn
5176             (setq articles (sort scored-list '<))
5177             (setq number (length articles)))
5178         (setq articles (copy-sequence articles)))
5179
5180       (when (< (abs select) number)
5181         (if (< select 0)
5182             ;; Select the N oldest articles.
5183             (setcdr (nthcdr (1- (abs select)) articles) nil)
5184           ;; Select the N most recent articles.
5185           (setq articles (nthcdr (- number select) articles))))
5186       (setq gnus-newsgroup-unselected
5187             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5188       (when gnus-alter-articles-to-read-function
5189         (setq gnus-newsgroup-unreads
5190               (sort
5191                (funcall gnus-alter-articles-to-read-function
5192                         gnus-newsgroup-name gnus-newsgroup-unreads)
5193                '<)))
5194       articles)))
5195
5196 (defun gnus-killed-articles (killed articles)
5197   (let (out)
5198     (while articles
5199       (when (inline (gnus-member-of-range (car articles) killed))
5200         (push (car articles) out))
5201       (setq articles (cdr articles)))
5202     out))
5203
5204 (defun gnus-uncompress-marks (marks)
5205   "Uncompress the mark ranges in MARKS."
5206   (let ((uncompressed '(score bookmark))
5207         out)
5208     (while marks
5209       (if (memq (caar marks) uncompressed)
5210           (push (car marks) out)
5211         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5212       (setq marks (cdr marks)))
5213     out))
5214
5215 (defun gnus-article-mark-to-type (mark)
5216   "Return the type of MARK."
5217   (or (cadr (assq mark gnus-article-special-mark-lists))
5218       'list))
5219
5220 (defun gnus-article-unpropagatable-p (mark)
5221   "Return whether MARK should be propagated to backend."
5222   (memq mark gnus-article-unpropagated-mark-lists))
5223
5224 (defun gnus-adjust-marked-articles (info)
5225   "Set all article lists and remove all marks that are no longer valid."
5226   (let* ((marked-lists (gnus-info-marks info))
5227          (active (gnus-active (gnus-info-group info)))
5228          (min (car active))
5229          (max (cdr active))
5230          (types gnus-article-mark-lists)
5231          marks var articles article mark mark-type)
5232
5233     (dolist (marks marked-lists)
5234       (setq mark (car marks)
5235             mark-type (gnus-article-mark-to-type mark)
5236             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5237
5238       ;; We set the variable according to the type of the marks list,
5239       ;; and then adjust the marks to a subset of the active articles.
5240       (cond
5241        ;; Adjust "simple" lists.
5242        ((eq mark-type 'list)
5243         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5244         (when (memq mark '(tick dormant expire reply save))
5245           (while articles
5246             (when (or (< (setq article (pop articles)) min) (> article max))
5247               (set var (delq article (symbol-value var)))))))
5248        ;; Adjust assocs.
5249        ((eq mark-type 'tuple)
5250         (set var (setq articles (cdr marks)))
5251         (when (not (listp (cdr (symbol-value var))))
5252           (set var (list (symbol-value var))))
5253         (when (not (listp (cdr articles)))
5254           (setq articles (list articles)))
5255         (while articles
5256           (when (or (not (consp (setq article (pop articles))))
5257                     (< (car article) min)
5258                     (> (car article) max))
5259             (set var (delq article (symbol-value var))))))
5260        ;; Adjust ranges (sloppily).
5261        ((eq mark-type 'range)
5262         (cond
5263          ((eq mark 'seen)
5264           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5265           ;; It should be (seen (NUM1 . NUM2)).
5266           (when (numberp (cddr marks))
5267             (setcdr marks (list (cdr marks))))
5268           (setq articles (cdr marks))
5269           (while (and articles
5270                       (or (and (consp (car articles))
5271                                (> min (cdar articles)))
5272                           (and (numberp (car articles))
5273                                (> min (car articles)))))
5274             (pop articles))
5275           (set var articles))))))))
5276
5277 (defun gnus-update-missing-marks (missing)
5278   "Go through the list of MISSING articles and remove them from the mark lists."
5279   (when missing
5280     (let (var m)
5281       ;; Go through all types.
5282       (dolist (elem gnus-article-mark-lists)
5283         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5284           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5285           (when (symbol-value var)
5286             ;; This list has articles.  So we delete all missing
5287             ;; articles from it.
5288             (setq m missing)
5289             (while m
5290               (set var (delq (pop m) (symbol-value var))))))))))
5291
5292 (defun gnus-update-marks ()
5293   "Enter the various lists of marked articles into the newsgroup info list."
5294   (let ((types gnus-article-mark-lists)
5295         (info (gnus-get-info gnus-newsgroup-name))
5296         type list newmarked symbol delta-marks)
5297     (when info
5298       ;; Add all marks lists to the list of marks lists.
5299       (while (setq type (pop types))
5300         (setq list (symbol-value
5301                     (setq symbol
5302                           (intern (format "gnus-newsgroup-%s" (car type))))))
5303
5304         (when list
5305           ;; Get rid of the entries of the articles that have the
5306           ;; default score.
5307           (when (and (eq (cdr type) 'score)
5308                      gnus-save-score
5309                      list)
5310             (let* ((arts list)
5311                    (prev (cons nil list))
5312                    (all prev))
5313               (while arts
5314                 (if (or (not (consp (car arts)))
5315                         (= (cdar arts) gnus-summary-default-score))
5316                     (setcdr prev (cdr arts))
5317                   (setq prev arts))
5318                 (setq arts (cdr arts)))
5319               (setq list (cdr all)))))
5320
5321         (when (eq (cdr type) 'seen)
5322           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5323
5324         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5325           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5326
5327         (when (and (gnus-check-backend-function
5328                     'request-set-mark gnus-newsgroup-name)
5329                    (not (gnus-article-unpropagatable-p (cdr type))))
5330           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5331                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5332                  (add (gnus-remove-from-range
5333                        (gnus-copy-sequence list) old)))
5334             (when add
5335               (push (list add 'add (list (cdr type))) delta-marks))
5336             (when del
5337               (push (list del 'del (list (cdr type))) delta-marks))))
5338
5339         (when list
5340           (push (cons (cdr type) list) newmarked)))
5341
5342       (when delta-marks
5343         (unless (gnus-check-group gnus-newsgroup-name)
5344           (error "Can't open server for %s" gnus-newsgroup-name))
5345         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5346
5347       ;; Enter these new marks into the info of the group.
5348       (if (nthcdr 3 info)
5349           (setcar (nthcdr 3 info) newmarked)
5350         ;; Add the marks lists to the end of the info.
5351         (when newmarked
5352           (setcdr (nthcdr 2 info) (list newmarked))))
5353
5354       ;; Cut off the end of the info if there's nothing else there.
5355       (let ((i 5))
5356         (while (and (> i 2)
5357                     (not (nth i info)))
5358           (when (nthcdr (decf i) info)
5359             (setcdr (nthcdr i info) nil)))))))
5360
5361 (defun gnus-set-mode-line (where)
5362   "Set the mode line of the article or summary buffers.
5363 If WHERE is `summary', the summary mode line format will be used."
5364   ;; Is this mode line one we keep updated?
5365   (when (and (memq where gnus-updated-mode-lines)
5366              (symbol-value
5367               (intern (format "gnus-%s-mode-line-format-spec" where))))
5368     (let (mode-string)
5369       (save-excursion
5370         ;; We evaluate this in the summary buffer since these
5371         ;; variables are buffer-local to that buffer.
5372         (set-buffer gnus-summary-buffer)
5373        ;; We bind all these variables that are used in the `eval' form
5374         ;; below.
5375         (let* ((mformat (symbol-value
5376                          (intern
5377                           (format "gnus-%s-mode-line-format-spec" where))))
5378                (gnus-tmp-group-name (gnus-group-decoded-name
5379                                      gnus-newsgroup-name))
5380                (gnus-tmp-article-number (or gnus-current-article 0))
5381                (gnus-tmp-unread gnus-newsgroup-unreads)
5382                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5383                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5384                (gnus-tmp-unread-and-unselected
5385                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5386                             (zerop gnus-tmp-unselected))
5387                        "")
5388                       ((zerop gnus-tmp-unselected)
5389                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5390                       (t (format "{%d(+%d) more}"
5391                                  gnus-tmp-unread-and-unticked
5392                                  gnus-tmp-unselected))))
5393                (gnus-tmp-subject
5394                 (if (and gnus-current-headers
5395                          (vectorp gnus-current-headers))
5396                     (gnus-mode-string-quote
5397                      (mail-header-subject gnus-current-headers))
5398                   ""))
5399                bufname-length max-len
5400                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5401           (setq mode-string (eval mformat))
5402           (setq bufname-length (if (string-match "%b" mode-string)
5403                                    (- (length
5404                                        (buffer-name
5405                                         (if (eq where 'summary)
5406                                             nil
5407                                           (get-buffer gnus-article-buffer))))
5408                                       2)
5409                                  0))
5410           (setq max-len (max 4 (if gnus-mode-non-string-length
5411                                    (- (window-width)
5412                                       gnus-mode-non-string-length
5413                                       bufname-length)
5414                                  (length mode-string))))
5415           ;; We might have to chop a bit of the string off...
5416           (when (> (length mode-string) max-len)
5417             (setq mode-string
5418                   (concat (truncate-string-to-width mode-string (- max-len 3))
5419                           "...")))
5420           ;; Pad the mode string a bit.
5421           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5422       ;; Update the mode line.
5423       (setq mode-line-buffer-identification
5424             (gnus-mode-line-buffer-identification (list mode-string)))
5425       (set-buffer-modified-p t))))
5426
5427 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5428   "Go through the HEADERS list and add all Xrefs to a hash table.
5429 The resulting hash table is returned, or nil if no Xrefs were found."
5430   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5431          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5432          (xref-hashtb (gnus-make-hashtable))
5433          start group entry number xrefs header)
5434     (while headers
5435       (setq header (pop headers))
5436       (when (and (setq xrefs (mail-header-xref header))
5437                  (not (memq (setq number (mail-header-number header))
5438                             unreads)))
5439         (setq start 0)
5440         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5441           (setq start (match-end 0))
5442           (setq group (if prefix
5443                           (concat prefix (substring xrefs (match-beginning 1)
5444                                                     (match-end 1)))
5445                         (substring xrefs (match-beginning 1) (match-end 1))))
5446           (setq number
5447                 (string-to-int (substring xrefs (match-beginning 2)
5448                                           (match-end 2))))
5449           (if (setq entry (gnus-gethash group xref-hashtb))
5450               (setcdr entry (cons number (cdr entry)))
5451             (gnus-sethash group (cons number nil) xref-hashtb)))))
5452     (and start xref-hashtb)))
5453
5454 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5455   "Look through all the headers and mark the Xrefs as read."
5456   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5457         name entry info xref-hashtb idlist method nth4)
5458     (save-excursion
5459       (set-buffer gnus-group-buffer)
5460       (when (setq xref-hashtb
5461                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5462         (mapatoms
5463          (lambda (group)
5464            (unless (string= from-newsgroup (setq name (symbol-name group)))
5465              (setq idlist (symbol-value group))
5466              ;; Dead groups are not updated.
5467              (and (prog1
5468                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5469                             info (nth 2 entry))
5470                     (when (stringp (setq nth4 (gnus-info-method info)))
5471                       (setq nth4 (gnus-server-to-method nth4))))
5472                   ;; Only do the xrefs if the group has the same
5473                   ;; select method as the group we have just read.
5474                   (or (gnus-methods-equal-p
5475                        nth4 (gnus-find-method-for-group from-newsgroup))
5476                       virtual
5477                       (equal nth4 (setq method (gnus-find-method-for-group
5478                                                 from-newsgroup)))
5479                       (and (equal (car nth4) (car method))
5480                            (equal (nth 1 nth4) (nth 1 method))))
5481                   gnus-use-cross-reference
5482                   (or (not (eq gnus-use-cross-reference t))
5483                       virtual
5484                       ;; Only do cross-references on subscribed
5485                       ;; groups, if that is what is wanted.
5486                       (<= (gnus-info-level info) gnus-level-subscribed))
5487                   (gnus-group-make-articles-read name idlist))))
5488          xref-hashtb)))))
5489
5490 (defun gnus-compute-read-articles (group articles)
5491   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5492          (info (nth 2 entry))
5493          (active (gnus-active group))
5494          ninfo)
5495     (when entry
5496       ;; First peel off all invalid article numbers.
5497       (when active
5498         (let ((ids articles)
5499               id first)
5500           (while (setq id (pop ids))
5501             (when (and first (> id (cdr active)))
5502               ;; We'll end up in this situation in one particular
5503               ;; obscure situation.  If you re-scan a group and get
5504               ;; a new article that is cross-posted to a different
5505               ;; group that has not been re-scanned, you might get
5506               ;; crossposted article that has a higher number than
5507               ;; Gnus believes possible.  So we re-activate this
5508               ;; group as well.  This might mean doing the
5509               ;; crossposting thingy will *increase* the number
5510               ;; of articles in some groups.  Tsk, tsk.
5511               (setq active (or (gnus-activate-group group) active)))
5512             (when (or (> id (cdr active))
5513                       (< id (car active)))
5514               (setq articles (delq id articles))))))
5515       ;; If the read list is nil, we init it.
5516       (if (and active
5517                (null (gnus-info-read info))
5518                (> (car active) 1))
5519           (setq ninfo (cons 1 (1- (car active))))
5520         (setq ninfo (gnus-info-read info)))
5521       ;; Then we add the read articles to the range.
5522       (gnus-add-to-range
5523        ninfo (setq articles (sort articles '<))))))
5524
5525 (defun gnus-group-make-articles-read (group articles)
5526   "Update the info of GROUP to say that ARTICLES are read."
5527   (let* ((num 0)
5528          (entry (gnus-gethash group gnus-newsrc-hashtb))
5529          (info (nth 2 entry))
5530          (active (gnus-active group))
5531          range)
5532     (when entry
5533       (setq range (gnus-compute-read-articles group articles))
5534       (save-excursion
5535         (set-buffer gnus-group-buffer)
5536         (gnus-undo-register
5537           `(progn
5538              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5539              (gnus-info-set-read ',info ',(gnus-info-read info))
5540              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5541              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5542              (gnus-group-update-group ,group t))))
5543       ;; Add the read articles to the range.
5544       (gnus-info-set-read info range)
5545       (gnus-request-set-mark group (list (list range 'add '(read))))
5546       ;; Then we have to re-compute how many unread
5547       ;; articles there are in this group.
5548       (when active
5549         (cond
5550          ((not range)
5551           (setq num (- (1+ (cdr active)) (car active))))
5552          ((not (listp (cdr range)))
5553           (setq num (- (cdr active) (- (1+ (cdr range))
5554                                        (car range)))))
5555          (t
5556           (while range
5557             (if (numberp (car range))
5558                 (setq num (1+ num))
5559               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5560             (setq range (cdr range)))
5561           (setq num (- (cdr active) num))))
5562         ;; Update the number of unread articles.
5563         (setcar entry num)
5564         ;; Update the group buffer.
5565         (unless (gnus-ephemeral-group-p group)
5566           (gnus-group-update-group group t))))))
5567
5568 (defvar gnus-newsgroup-none-id 0)
5569
5570 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5571   (let ((cur nntp-server-buffer)
5572         (dependencies
5573          (or dependencies
5574              (save-excursion (set-buffer gnus-summary-buffer)
5575                              gnus-newsgroup-dependencies)))
5576         headers id end ref
5577         (mail-parse-charset gnus-newsgroup-charset)
5578         (mail-parse-ignored-charsets
5579          (save-excursion (condition-case nil
5580                              (set-buffer gnus-summary-buffer)
5581                            (error))
5582                          gnus-newsgroup-ignored-charsets)))
5583     (save-excursion
5584       (set-buffer nntp-server-buffer)
5585       ;; Translate all TAB characters into SPACE characters.
5586       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5587       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5588       (gnus-run-hooks 'gnus-parse-headers-hook)
5589       (let ((case-fold-search t)
5590             in-reply-to header p lines chars)
5591         (goto-char (point-min))
5592         ;; Search to the beginning of the next header.  Error messages
5593         ;; do not begin with 2 or 3.
5594         (while (re-search-forward "^[23][0-9]+ " nil t)
5595           (setq id nil
5596                 ref nil)
5597           ;; This implementation of this function, with nine
5598           ;; search-forwards instead of the one re-search-forward and
5599           ;; a case (which basically was the old function) is actually
5600           ;; about twice as fast, even though it looks messier.  You
5601           ;; can't have everything, I guess.  Speed and elegance
5602           ;; doesn't always go hand in hand.
5603           (setq
5604            header
5605            (vector
5606             ;; Number.
5607             (prog1
5608                 (read cur)
5609               (end-of-line)
5610               (setq p (point))
5611               (narrow-to-region (point)
5612                                 (or (and (search-forward "\n.\n" nil t)
5613                                          (- (point) 2))
5614                                     (point))))
5615             ;; Subject.
5616             (progn
5617               (goto-char p)
5618               (if (search-forward "\nsubject:" nil t)
5619                   (funcall gnus-decode-encoded-word-function
5620                            (nnheader-header-value))
5621                 "(none)"))
5622             ;; From.
5623             (progn
5624               (goto-char p)
5625               (if (search-forward "\nfrom:" nil t)
5626                   (funcall gnus-decode-encoded-word-function
5627                            (nnheader-header-value))
5628                 "(nobody)"))
5629             ;; Date.
5630             (progn
5631               (goto-char p)
5632               (if (search-forward "\ndate:" nil t)
5633                   (nnheader-header-value) ""))
5634             ;; Message-ID.
5635             (progn
5636               (goto-char p)
5637               (setq id (if (re-search-forward
5638                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5639                            ;; We do it this way to make sure the Message-ID
5640                            ;; is (somewhat) syntactically valid.
5641                            (buffer-substring (match-beginning 1)
5642                                              (match-end 1))
5643                          ;; If there was no message-id, we just fake one
5644                          ;; to make subsequent routines simpler.
5645                          (nnheader-generate-fake-message-id))))
5646             ;; References.
5647             (progn
5648               (goto-char p)
5649               (if (search-forward "\nreferences:" nil t)
5650                   (progn
5651                     (setq end (point))
5652                     (prog1
5653                         (nnheader-header-value)
5654                       (setq ref
5655                             (buffer-substring
5656                              (progn
5657                                (end-of-line)
5658                                (search-backward ">" end t)
5659                                (1+ (point)))
5660                              (progn
5661                                (search-backward "<" end t)
5662                                (point))))))
5663                 ;; Get the references from the in-reply-to header if there
5664                 ;; were no references and the in-reply-to header looks
5665                 ;; promising.
5666                 (if (and (search-forward "\nin-reply-to:" nil t)
5667                          (setq in-reply-to (nnheader-header-value))
5668                          (string-match "<[^>]+>" in-reply-to))
5669                     (let (ref2)
5670                       (setq ref (substring in-reply-to (match-beginning 0)
5671                                            (match-end 0)))
5672                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5673                         (setq ref2 (substring in-reply-to (match-beginning 0)
5674                                               (match-end 0)))
5675                         (when (> (length ref2) (length ref))
5676                           (setq ref ref2)))
5677                       ref)
5678                   (setq ref nil))))
5679             ;; Chars.
5680             (progn
5681               (goto-char p)
5682               (if (search-forward "\nchars: " nil t)
5683                   (if (numberp (setq chars (ignore-errors (read cur))))
5684                       chars -1)
5685                 -1))
5686             ;; Lines.
5687             (progn
5688               (goto-char p)
5689               (if (search-forward "\nlines: " nil t)
5690                   (if (numberp (setq lines (ignore-errors (read cur))))
5691                       lines -1)
5692                 -1))
5693             ;; Xref.
5694             (progn
5695               (goto-char p)
5696               (and (search-forward "\nxref:" nil t)
5697                    (nnheader-header-value)))
5698             ;; Extra.
5699             (when gnus-extra-headers
5700               (let ((extra gnus-extra-headers)
5701                     out)
5702                 (while extra
5703                   (goto-char p)
5704                   (when (search-forward
5705                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5706                     (push (cons (car extra) (nnheader-header-value))
5707                           out))
5708                   (pop extra))
5709                 out))))
5710           (when (equal id ref)
5711             (setq ref nil))
5712
5713           (when gnus-alter-header-function
5714             (funcall gnus-alter-header-function header)
5715             (setq id (mail-header-id header)
5716                   ref (gnus-parent-id (mail-header-references header))))
5717
5718           (when (setq header
5719                       (gnus-dependencies-add-header
5720                        header dependencies force-new))
5721             (push header headers))
5722           (goto-char (point-max))
5723           (widen))
5724         (nreverse headers)))))
5725
5726 ;; Goes through the xover lines and returns a list of vectors
5727 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5728                                                   force-new dependencies
5729                                                   group also-fetch-heads)
5730   "Parse the news overview data in the server buffer.
5731 Return a list of headers that match SEQUENCE (see
5732 `nntp-retrieve-headers')."
5733   ;; Get the Xref when the users reads the articles since most/some
5734   ;; NNTP servers do not include Xrefs when using XOVER.
5735   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5736   (let ((mail-parse-charset gnus-newsgroup-charset)
5737         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5738         (cur nntp-server-buffer)
5739         (dependencies (or dependencies gnus-newsgroup-dependencies))
5740         (allp (cond
5741                ((eq gnus-read-all-available-headers t)
5742                 t)
5743                ((stringp gnus-read-all-available-headers)
5744                 (string-match gnus-read-all-available-headers group))
5745                (t
5746                 nil)))
5747         number headers header)
5748     (save-excursion
5749       (set-buffer nntp-server-buffer)
5750       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5751       ;; Allow the user to mangle the headers before parsing them.
5752       (gnus-run-hooks 'gnus-parse-headers-hook)
5753       (goto-char (point-min))
5754       (gnus-parse-without-error
5755         (while (and (or sequence allp)
5756                     (not (eobp)))
5757           (setq number (read cur))
5758           (when (not allp)
5759             (while (and sequence
5760                         (< (car sequence) number))
5761               (setq sequence (cdr sequence))))
5762           (when (and (or allp
5763                          (and sequence
5764                               (eq number (car sequence))))
5765                      (progn
5766                        (setq sequence (cdr sequence))
5767                        (setq header (inline
5768                                       (gnus-nov-parse-line
5769                                        number dependencies force-new)))))
5770             (push header headers))
5771           (forward-line 1)))
5772       ;; A common bug in inn is that if you have posted an article and
5773       ;; then retrieves the active file, it will answer correctly --
5774       ;; the new article is included.  However, a NOV entry for the
5775       ;; article may not have been generated yet, so this may fail.
5776       ;; We work around this problem by retrieving the last few
5777       ;; headers using HEAD.
5778       (if (or (not also-fetch-heads)
5779               (not sequence))
5780           ;; We (probably) got all the headers.
5781           (nreverse headers)
5782         (let ((gnus-nov-is-evil t))
5783           (nconc
5784            (nreverse headers)
5785            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5786              (gnus-get-newsgroup-headers))))))))
5787
5788 (defun gnus-article-get-xrefs ()
5789   "Fill in the Xref value in `gnus-current-headers', if necessary.
5790 This is meant to be called in `gnus-article-internal-prepare-hook'."
5791   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5792                                  gnus-current-headers)))
5793     (or (not gnus-use-cross-reference)
5794         (not headers)
5795         (and (mail-header-xref headers)
5796              (not (string= (mail-header-xref headers) "")))
5797         (let ((case-fold-search t)
5798               xref)
5799           (save-restriction
5800             (nnheader-narrow-to-headers)
5801             (goto-char (point-min))
5802             (when (or (and (not (eobp))
5803                            (eq (downcase (char-after)) ?x)
5804                            (looking-at "Xref:"))
5805                       (search-forward "\nXref:" nil t))
5806               (goto-char (1+ (match-end 0)))
5807               (setq xref (buffer-substring (point)
5808                                            (progn (end-of-line) (point))))
5809               (mail-header-set-xref headers xref)))))))
5810
5811 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5812   "Find article ID and insert the summary line for that article.
5813 OLD-HEADER can either be a header or a line number to insert
5814 the subject line on."
5815   (let* ((line (and (numberp old-header) old-header))
5816          (old-header (and (vectorp old-header) old-header))
5817          (header (cond ((and old-header use-old-header)
5818                         old-header)
5819                        ((and (numberp id)
5820                              (gnus-number-to-header id))
5821                         (gnus-number-to-header id))
5822                        (t
5823                         (gnus-read-header id))))
5824          (number (and (numberp id) id))
5825          d)
5826     (when header
5827       ;; Rebuild the thread that this article is part of and go to the
5828       ;; article we have fetched.
5829       (when (and (not gnus-show-threads)
5830                  old-header)
5831         (when (and number
5832                    (setq d (gnus-data-find (mail-header-number old-header))))
5833           (goto-char (gnus-data-pos d))
5834           (gnus-data-remove
5835            number
5836            (- (gnus-point-at-bol)
5837               (prog1
5838                   (1+ (gnus-point-at-eol))
5839                 (gnus-delete-line))))))
5840       (when old-header
5841         (mail-header-set-number header (mail-header-number old-header)))
5842       (setq gnus-newsgroup-sparse
5843             (delq (setq number (mail-header-number header))
5844                   gnus-newsgroup-sparse))
5845       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5846       (push number gnus-newsgroup-limit)
5847       (gnus-rebuild-thread (mail-header-id header) line)
5848       (gnus-summary-goto-subject number nil t))
5849     (when (and (numberp number)
5850                (> number 0))
5851       ;; We have to update the boundaries even if we can't fetch the
5852       ;; article if ID is a number -- so that the next `P' or `N'
5853       ;; command will fetch the previous (or next) article even
5854       ;; if the one we tried to fetch this time has been canceled.
5855       (when (> number gnus-newsgroup-end)
5856         (setq gnus-newsgroup-end number))
5857       (when (< number gnus-newsgroup-begin)
5858         (setq gnus-newsgroup-begin number))
5859       (setq gnus-newsgroup-unselected
5860             (delq number gnus-newsgroup-unselected)))
5861     ;; Report back a success?
5862     (and header (mail-header-number header))))
5863
5864 ;;; Process/prefix in the summary buffer
5865
5866 (defun gnus-summary-work-articles (n)
5867   "Return a list of articles to be worked upon.
5868 The prefix argument, the list of process marked articles, and the
5869 current article will be taken into consideration."
5870   (save-excursion
5871     (set-buffer gnus-summary-buffer)
5872     (cond
5873      (n
5874       ;; A numerical prefix has been given.
5875       (setq n (prefix-numeric-value n))
5876       (let ((backward (< n 0))
5877             (n (abs (prefix-numeric-value n)))
5878             articles article)
5879         (save-excursion
5880           (while
5881               (and (> n 0)
5882                    (push (setq article (gnus-summary-article-number))
5883                          articles)
5884                    (if backward
5885                        (gnus-summary-find-prev nil article)
5886                      (gnus-summary-find-next nil article)))
5887             (decf n)))
5888         (nreverse articles)))
5889      ((and (gnus-region-active-p) (mark))
5890       (message "region active")
5891       ;; Work on the region between point and mark.
5892       (let ((max (max (point) (mark)))
5893             articles article)
5894         (save-excursion
5895           (goto-char (min (min (point) (mark))))
5896           (while
5897               (and
5898                (push (setq article (gnus-summary-article-number)) articles)
5899                (gnus-summary-find-next nil article)
5900                (< (point) max)))
5901           (nreverse articles))))
5902      (gnus-newsgroup-processable
5903       ;; There are process-marked articles present.
5904       ;; Save current state.
5905       (gnus-summary-save-process-mark)
5906       ;; Return the list.
5907       (reverse gnus-newsgroup-processable))
5908      (t
5909       ;; Just return the current article.
5910       (list (gnus-summary-article-number))))))
5911
5912 (defmacro gnus-summary-iterate (arg &rest forms)
5913   "Iterate over the process/prefixed articles and do FORMS.
5914 ARG is the interactive prefix given to the command.  FORMS will be
5915 executed with point over the summary line of the articles."
5916   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5917     `(let ((,articles (gnus-summary-work-articles ,arg)))
5918        (while ,articles
5919          (gnus-summary-goto-subject (car ,articles))
5920          ,@forms
5921          (pop ,articles)))))
5922
5923 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5924 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5925
5926 (defun gnus-summary-save-process-mark ()
5927   "Push the current set of process marked articles on the stack."
5928   (interactive)
5929   (push (copy-sequence gnus-newsgroup-processable)
5930         gnus-newsgroup-process-stack))
5931
5932 (defun gnus-summary-kill-process-mark ()
5933   "Push the current set of process marked articles on the stack and unmark."
5934   (interactive)
5935   (gnus-summary-save-process-mark)
5936   (gnus-summary-unmark-all-processable))
5937
5938 (defun gnus-summary-yank-process-mark ()
5939   "Pop the last process mark state off the stack and restore it."
5940   (interactive)
5941   (unless gnus-newsgroup-process-stack
5942     (error "Empty mark stack"))
5943   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5944
5945 (defun gnus-summary-process-mark-set (set)
5946   "Make SET into the current process marked articles."
5947   (gnus-summary-unmark-all-processable)
5948   (while set
5949     (gnus-summary-set-process-mark (pop set))))
5950
5951 ;;; Searching and stuff
5952
5953 (defun gnus-summary-search-group (&optional backward use-level)
5954   "Search for next unread newsgroup.
5955 If optional argument BACKWARD is non-nil, search backward instead."
5956   (save-excursion
5957     (set-buffer gnus-group-buffer)
5958     (when (gnus-group-search-forward
5959            backward nil (if use-level (gnus-group-group-level) nil))
5960       (gnus-group-group-name))))
5961
5962 (defun gnus-summary-best-group (&optional exclude-group)
5963   "Find the name of the best unread group.
5964 If EXCLUDE-GROUP, do not go to this group."
5965   (save-excursion
5966     (set-buffer gnus-group-buffer)
5967     (save-excursion
5968       (gnus-group-best-unread-group exclude-group))))
5969
5970 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5971   (if backward (gnus-summary-find-prev)
5972     (let* ((dummy (gnus-summary-article-intangible-p))
5973            (article (or article (gnus-summary-article-number)))
5974            (arts (gnus-data-find-list article))
5975            result)
5976       (when (and (not dummy)
5977                  (or (not gnus-summary-check-current)
5978                      (not unread)
5979                      (not (gnus-data-unread-p (car arts)))))
5980         (setq arts (cdr arts)))
5981       (when (setq result
5982                   (if unread
5983                       (progn
5984                         (while arts
5985                           (when (or (and undownloaded
5986                                          (memq (car arts) gnus-newsgroup-undownloaded))
5987                                     (gnus-data-unread-p (car arts)))
5988                             (setq result (car arts)
5989                                   arts nil))
5990                           (setq arts (cdr arts)))
5991                         result)
5992                     (car arts)))
5993         (goto-char (gnus-data-pos result))
5994         (gnus-data-number result)))))
5995
5996 (defun gnus-summary-find-prev (&optional unread article)
5997   (let* ((eobp (eobp))
5998          (article (or article (gnus-summary-article-number)))
5999          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6000          result)
6001     (when (and (not eobp)
6002                (or (not gnus-summary-check-current)
6003                    (not unread)
6004                    (not (gnus-data-unread-p (car arts)))))
6005       (setq arts (cdr arts)))
6006     (when (setq result
6007                 (if unread
6008                     (progn
6009                       (while arts
6010                         (when (gnus-data-unread-p (car arts))
6011                           (setq result (car arts)
6012                                 arts nil))
6013                         (setq arts (cdr arts)))
6014                       result)
6015                   (car arts)))
6016       (goto-char (gnus-data-pos result))
6017       (gnus-data-number result))))
6018
6019 (defun gnus-summary-find-subject (subject &optional unread backward article)
6020   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6021          (article (or article (gnus-summary-article-number)))
6022          (articles (gnus-data-list backward))
6023          (arts (gnus-data-find-list article articles))
6024          result)
6025     (when (or (not gnus-summary-check-current)
6026               (not unread)
6027               (not (gnus-data-unread-p (car arts))))
6028       (setq arts (cdr arts)))
6029     (while arts
6030       (and (or (not unread)
6031                (gnus-data-unread-p (car arts)))
6032            (vectorp (gnus-data-header (car arts)))
6033            (gnus-subject-equal
6034             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6035            (setq result (car arts)
6036                  arts nil))
6037       (setq arts (cdr arts)))
6038     (and result
6039          (goto-char (gnus-data-pos result))
6040          (gnus-data-number result))))
6041
6042 (defun gnus-summary-search-forward (&optional unread subject backward)
6043   "Search forward for an article.
6044 If UNREAD, look for unread articles.  If SUBJECT, look for
6045 articles with that subject.  If BACKWARD, search backward instead."
6046   (cond (subject (gnus-summary-find-subject subject unread backward))
6047         (backward (gnus-summary-find-prev unread))
6048         (t (gnus-summary-find-next unread))))
6049
6050 (defun gnus-recenter (&optional n)
6051   "Center point in window and redisplay frame.
6052 Also do horizontal recentering."
6053   (interactive "P")
6054   (when (and gnus-auto-center-summary
6055              (not (eq gnus-auto-center-summary 'vertical)))
6056     (gnus-horizontal-recenter))
6057   (recenter n))
6058
6059 (defun gnus-summary-recenter ()
6060   "Center point in the summary window.
6061 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6062 displayed, no centering will be performed."
6063   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6064 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6065   (interactive)
6066   (let* ((top (cond ((< (window-height) 4) 0)
6067                     ((< (window-height) 7) 1)
6068                     (t (if (numberp gnus-auto-center-summary)
6069                            gnus-auto-center-summary
6070                          2))))
6071          (height (1- (window-height)))
6072          (bottom (save-excursion (goto-char (point-max))
6073                                  (forward-line (- height))
6074                                  (point)))
6075          (window (get-buffer-window (current-buffer))))
6076     ;; The user has to want it.
6077     (when gnus-auto-center-summary
6078       (when (get-buffer-window gnus-article-buffer)
6079         ;; Only do recentering when the article buffer is displayed,
6080       ;; Set the window start to either `bottom', which is the biggest
6081         ;; possible valid number, or the second line from the top,
6082         ;; whichever is the least.
6083         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6084           (if (> bottom top-pos)
6085               ;; Keep the second line from the top visible
6086               (set-window-start window top-pos t)
6087             ;; Try to keep the bottom line visible; if it's partially
6088             ;; obscured, either scroll one more line to make it fully
6089             ;; visible, or revert to using TOP-POS.
6090             (save-excursion
6091               (goto-char (point-max))
6092               (forward-line -1)
6093               (let ((last-line-start (point)))
6094                 (goto-char bottom)
6095                 (set-window-start window (point) t)
6096                 (when (not (pos-visible-in-window-p last-line-start window))
6097                   (forward-line 1)
6098                   (set-window-start window (min (point) top-pos) t)))))))
6099       ;; Do horizontal recentering while we're at it.
6100       (when (and (get-buffer-window (current-buffer) t)
6101                  (not (eq gnus-auto-center-summary 'vertical)))
6102         (let ((selected (selected-window)))
6103           (select-window (get-buffer-window (current-buffer) t))
6104           (gnus-summary-position-point)
6105           (gnus-horizontal-recenter)
6106           (select-window selected))))))
6107
6108 (defun gnus-summary-jump-to-group (newsgroup)
6109   "Move point to NEWSGROUP in group mode buffer."
6110   ;; Keep update point of group mode buffer if visible.
6111   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6112       (save-window-excursion
6113         ;; Take care of tree window mode.
6114         (when (get-buffer-window gnus-group-buffer)
6115           (pop-to-buffer gnus-group-buffer))
6116         (gnus-group-jump-to-group newsgroup))
6117     (save-excursion
6118       ;; Take care of tree window mode.
6119       (if (get-buffer-window gnus-group-buffer)
6120           (pop-to-buffer gnus-group-buffer)
6121         (set-buffer gnus-group-buffer))
6122       (gnus-group-jump-to-group newsgroup))))
6123
6124 ;; This function returns a list of article numbers based on the
6125 ;; difference between the ranges of read articles in this group and
6126 ;; the range of active articles.
6127 (defun gnus-list-of-unread-articles (group)
6128   (let* ((read (gnus-info-read (gnus-get-info group)))
6129          (active (or (gnus-active group) (gnus-activate-group group)))
6130          (last (cdr active))
6131          first nlast unread)
6132     ;; If none are read, then all are unread.
6133     (if (not read)
6134         (setq first (car active))
6135       ;; If the range of read articles is a single range, then the
6136       ;; first unread article is the article after the last read
6137       ;; article.  Sounds logical, doesn't it?
6138       (if (and (not (listp (cdr read)))
6139                (or (< (car read) (car active))
6140                    (progn (setq read (list read))
6141                           nil)))
6142           (setq first (max (car active) (1+ (cdr read))))
6143         ;; `read' is a list of ranges.
6144         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6145                                   (caar read)))
6146                   1)
6147           (setq first (car active)))
6148         (while read
6149           (when first
6150             (while (< first nlast)
6151               (push first unread)
6152               (setq first (1+ first))))
6153           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6154           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6155           (setq read (cdr read)))))
6156     ;; And add the last unread articles.
6157     (while (<= first last)
6158       (push first unread)
6159       (setq first (1+ first)))
6160     ;; Return the list of unread articles.
6161     (delq 0 (nreverse unread))))
6162
6163 (defun gnus-list-of-read-articles (group)
6164   "Return a list of unread, unticked and non-dormant articles."
6165   (let* ((info (gnus-get-info group))
6166          (marked (gnus-info-marks info))
6167          (active (gnus-active group)))
6168     (and info active
6169          (gnus-list-range-difference
6170           (gnus-list-range-difference
6171            (gnus-sorted-complement
6172             (gnus-uncompress-range active)
6173             (gnus-list-of-unread-articles group))
6174            (cdr (assq 'dormant marked)))
6175           (cdr (assq 'tick marked))))))
6176
6177 ;; Various summary commands
6178
6179 (defun gnus-summary-select-article-buffer ()
6180   "Reconfigure windows to show article buffer."
6181   (interactive)
6182   (if (not (gnus-buffer-live-p gnus-article-buffer))
6183       (error "There is no article buffer for this summary buffer")
6184     (gnus-configure-windows 'article)
6185     (select-window (get-buffer-window gnus-article-buffer))))
6186
6187 (defun gnus-summary-universal-argument (arg)
6188   "Perform any operation on all articles that are process/prefixed."
6189   (interactive "P")
6190   (let ((articles (gnus-summary-work-articles arg))
6191         func article)
6192     (if (eq
6193          (setq
6194           func
6195           (key-binding
6196            (read-key-sequence
6197             (substitute-command-keys
6198              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6199          'undefined)
6200         (gnus-error 1 "Undefined key")
6201       (save-excursion
6202         (while articles
6203           (gnus-summary-goto-subject (setq article (pop articles)))
6204           (let (gnus-newsgroup-processable)
6205             (command-execute func))
6206           (gnus-summary-remove-process-mark article)))))
6207   (gnus-summary-position-point))
6208
6209 (defun gnus-summary-toggle-truncation (&optional arg)
6210   "Toggle truncation of summary lines.
6211 With arg, turn line truncation on if arg is positive."
6212   (interactive "P")
6213   (setq truncate-lines
6214         (if (null arg) (not truncate-lines)
6215           (> (prefix-numeric-value arg) 0)))
6216   (redraw-display))
6217
6218 (defun gnus-summary-find-uncancelled ()
6219   "Return the number of an uncancelled article.
6220 The current article is considered, then following articles, then previous
6221 articles.  If all articles are cancelled then return a dummy 0."
6222   (let (found)
6223     (dolist (rev '(nil t))
6224       (unless found      ; don't demand the reverse list if we don't need it
6225         (let ((data (gnus-data-find-list
6226                      (gnus-summary-article-number) (gnus-data-list rev))))
6227           (while (and data (not found))
6228             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6229                 (setq found (gnus-data-number (car data))))
6230             (setq data (cdr data))))))
6231     (or found 0)))
6232
6233 (defun gnus-summary-reselect-current-group (&optional all rescan)
6234   "Exit and then reselect the current newsgroup.
6235 The prefix argument ALL means to select all articles."
6236   (interactive "P")
6237   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6238     (error "Ephemeral groups can't be reselected"))
6239   (let ((current-subject (gnus-summary-find-uncancelled))
6240         (group gnus-newsgroup-name))
6241     (setq gnus-newsgroup-begin nil)
6242     (gnus-summary-exit)
6243     ;; We have to adjust the point of group mode buffer because
6244     ;; point was moved to the next unread newsgroup by exiting.
6245     (gnus-summary-jump-to-group group)
6246     (when rescan
6247       (save-excursion
6248         (gnus-group-get-new-news-this-group 1)))
6249     (gnus-group-read-group all t)
6250     (gnus-summary-goto-subject current-subject nil t)))
6251
6252 (defun gnus-summary-rescan-group (&optional all)
6253   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6254   (interactive "P")
6255   (gnus-summary-reselect-current-group all t))
6256
6257 (defun gnus-summary-update-info (&optional non-destructive)
6258   (save-excursion
6259     (let ((group gnus-newsgroup-name))
6260       (when group
6261         (when gnus-newsgroup-kill-headers
6262           (setq gnus-newsgroup-killed
6263                 (gnus-compress-sequence
6264                  (gnus-sorted-union
6265                   (gnus-list-range-intersection
6266                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6267                   gnus-newsgroup-unreads)
6268                  t)))
6269         (unless (listp (cdr gnus-newsgroup-killed))
6270           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6271         (let ((headers gnus-newsgroup-headers))
6272           ;; Set the new ranges of read articles.
6273           (save-excursion
6274             (set-buffer gnus-group-buffer)
6275             (gnus-undo-force-boundary))
6276           (gnus-update-read-articles
6277            group (gnus-sorted-union
6278                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6279           ;; Set the current article marks.
6280           (let ((gnus-newsgroup-scored
6281                  (if (and (not gnus-save-score)
6282                           (not non-destructive))
6283                      nil
6284                    gnus-newsgroup-scored)))
6285             (save-excursion
6286               (gnus-update-marks)))
6287           ;; Do the cross-ref thing.
6288           (when gnus-use-cross-reference
6289             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6290           ;; Do not switch windows but change the buffer to work.
6291           (set-buffer gnus-group-buffer)
6292           (unless (gnus-ephemeral-group-p group)
6293             (gnus-group-update-group group)))))))
6294
6295 (defun gnus-summary-save-newsrc (&optional force)
6296   "Save the current number of read/marked articles in the dribble buffer.
6297 The dribble buffer will then be saved.
6298 If FORCE (the prefix), also save the .newsrc file(s)."
6299   (interactive "P")
6300   (gnus-summary-update-info t)
6301   (if force
6302       (gnus-save-newsrc-file)
6303     (gnus-dribble-save)))
6304
6305 (defun gnus-summary-exit (&optional temporary)
6306   "Exit reading current newsgroup, and then return to group selection mode.
6307 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6308   (interactive)
6309   (gnus-set-global-variables)
6310   (when (gnus-buffer-live-p gnus-article-buffer)
6311     (save-excursion
6312       (set-buffer gnus-article-buffer)
6313       (mm-destroy-parts gnus-article-mime-handles)
6314       ;; Set it to nil for safety reason.
6315       (setq gnus-article-mime-handle-alist nil)
6316       (setq gnus-article-mime-handles nil)))
6317   (gnus-kill-save-kill-buffer)
6318   (gnus-async-halt-prefetch)
6319   (let* ((group gnus-newsgroup-name)
6320          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6321          (gnus-group-is-exiting-p t)
6322          (mode major-mode)
6323          (group-point nil)
6324          (buf (current-buffer)))
6325     (unless quit-config
6326       ;; Do adaptive scoring, and possibly save score files.
6327       (when gnus-newsgroup-adaptive
6328         (gnus-score-adaptive))
6329       (when gnus-use-scoring
6330         (gnus-score-save)))
6331     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6332     ;; If we have several article buffers, we kill them at exit.
6333     (unless gnus-single-article-buffer
6334       (gnus-kill-buffer gnus-original-article-buffer)
6335       (setq gnus-article-current nil))
6336     (when gnus-use-cache
6337       (gnus-cache-possibly-remove-articles)
6338       (gnus-cache-save-buffers))
6339     (gnus-async-prefetch-remove-group group)
6340     (when gnus-suppress-duplicates
6341       (gnus-dup-enter-articles))
6342     (when gnus-use-trees
6343       (gnus-tree-close group))
6344     (when gnus-use-cache
6345       (gnus-cache-write-active))
6346     ;; Remove entries for this group.
6347     (nnmail-purge-split-history (gnus-group-real-name group))
6348     ;; Make all changes in this group permanent.
6349     (unless quit-config
6350       (gnus-run-hooks 'gnus-exit-group-hook)
6351       (gnus-summary-update-info))
6352     (gnus-close-group group)
6353     ;; Make sure where we were, and go to next newsgroup.
6354     (set-buffer gnus-group-buffer)
6355     (unless quit-config
6356       (gnus-group-jump-to-group group))
6357     (gnus-run-hooks 'gnus-summary-exit-hook)
6358     (unless (or quit-config
6359                 ;; If this group has disappeared from the summary
6360                 ;; buffer, don't skip forwards.
6361                 (not (string= group (gnus-group-group-name))))
6362       (gnus-group-next-unread-group 1))
6363     (setq group-point (point))
6364     (if temporary
6365         nil                             ;Nothing to do.
6366       ;; If we have several article buffers, we kill them at exit.
6367       (unless gnus-single-article-buffer
6368         (gnus-kill-buffer gnus-article-buffer)
6369         (gnus-kill-buffer gnus-original-article-buffer)
6370         (setq gnus-article-current nil))
6371       (set-buffer buf)
6372       (if (not gnus-kill-summary-on-exit)
6373           (progn
6374             (gnus-deaden-summary)
6375             (setq mode nil))
6376         ;; We set all buffer-local variables to nil.  It is unclear why
6377         ;; this is needed, but if we don't, buffer-local variables are
6378         ;; not garbage-collected, it seems.  This would the lead to en
6379         ;; ever-growing Emacs.
6380         (gnus-summary-clear-local-variables)
6381         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6382           (gnus-summary-clear-local-variables))
6383         (when (get-buffer gnus-article-buffer)
6384           (bury-buffer gnus-article-buffer))
6385         ;; We clear the global counterparts of the buffer-local
6386         ;; variables as well, just to be on the safe side.
6387         (set-buffer gnus-group-buffer)
6388         (gnus-summary-clear-local-variables)
6389         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6390           (gnus-summary-clear-local-variables)))
6391       (setq gnus-current-select-method gnus-select-method)
6392       (pop-to-buffer gnus-group-buffer)
6393       (if (not quit-config)
6394           (progn
6395             (goto-char group-point)
6396             (gnus-configure-windows 'group 'force))
6397         (gnus-handle-ephemeral-exit quit-config))
6398       ;; Return to group mode buffer.
6399       (when (eq mode 'gnus-summary-mode)
6400         (gnus-kill-buffer buf))
6401       ;; Clear the current group name.
6402       (unless quit-config
6403         (setq gnus-newsgroup-name nil)))))
6404
6405 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6406 (defun gnus-summary-exit-no-update (&optional no-questions)
6407   "Quit reading current newsgroup without updating read article info."
6408   (interactive)
6409   (let* ((group gnus-newsgroup-name)
6410          (gnus-group-is-exiting-p t)
6411          (quit-config (gnus-group-quit-config group)))
6412     (when (or no-questions
6413               gnus-expert-user
6414               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6415       (gnus-async-halt-prefetch)
6416       (mapcar 'funcall
6417               (delq 'gnus-summary-expire-articles
6418                     (copy-sequence gnus-summary-prepare-exit-hook)))
6419       (when (gnus-buffer-live-p gnus-article-buffer)
6420         (save-excursion
6421           (set-buffer gnus-article-buffer)
6422           (mm-destroy-parts gnus-article-mime-handles)
6423           ;; Set it to nil for safety reason.
6424           (setq gnus-article-mime-handle-alist nil)
6425           (setq gnus-article-mime-handles nil)))
6426       ;; If we have several article buffers, we kill them at exit.
6427       (unless gnus-single-article-buffer
6428         (gnus-kill-buffer gnus-article-buffer)
6429         (gnus-kill-buffer gnus-original-article-buffer)
6430         (setq gnus-article-current nil))
6431       (if (not gnus-kill-summary-on-exit)
6432           (gnus-deaden-summary)
6433         (gnus-close-group group)
6434         (gnus-summary-clear-local-variables)
6435         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6436           (gnus-summary-clear-local-variables))
6437         (set-buffer gnus-group-buffer)
6438         (gnus-summary-clear-local-variables)
6439         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6440           (gnus-summary-clear-local-variables))
6441         (when (get-buffer gnus-summary-buffer)
6442           (kill-buffer gnus-summary-buffer)))
6443       (unless gnus-single-article-buffer
6444         (setq gnus-article-current nil))
6445       (when gnus-use-trees
6446         (gnus-tree-close group))
6447       (gnus-async-prefetch-remove-group group)
6448       (when (get-buffer gnus-article-buffer)
6449         (bury-buffer gnus-article-buffer))
6450       ;; Return to the group buffer.
6451       (gnus-configure-windows 'group 'force)
6452       ;; Clear the current group name.
6453       (setq gnus-newsgroup-name nil)
6454       (unless (gnus-ephemeral-group-p group)
6455         (gnus-group-update-group group))
6456       (when (equal (gnus-group-group-name) group)
6457         (gnus-group-next-unread-group 1))
6458       (when quit-config
6459         (gnus-handle-ephemeral-exit quit-config)))))
6460
6461 (defun gnus-handle-ephemeral-exit (quit-config)
6462   "Handle movement when leaving an ephemeral group.
6463 The state which existed when entering the ephemeral is reset."
6464   (if (not (buffer-name (car quit-config)))
6465       (gnus-configure-windows 'group 'force)
6466     (set-buffer (car quit-config))
6467     (cond ((eq major-mode 'gnus-summary-mode)
6468            (gnus-set-global-variables))
6469           ((eq major-mode 'gnus-article-mode)
6470            (save-excursion
6471              ;; The `gnus-summary-buffer' variable may point
6472              ;; to the old summary buffer when using a single
6473              ;; article buffer.
6474              (unless (gnus-buffer-live-p gnus-summary-buffer)
6475                (set-buffer gnus-group-buffer))
6476              (set-buffer gnus-summary-buffer)
6477              (gnus-set-global-variables))))
6478     (if (or (eq (cdr quit-config) 'article)
6479             (eq (cdr quit-config) 'pick))
6480         (progn
6481           ;; The current article may be from the ephemeral group
6482           ;; thus it is best that we reload this article
6483           (gnus-summary-show-article)
6484           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6485               (gnus-configure-windows 'pick 'force)
6486             (gnus-configure-windows (cdr quit-config) 'force)))
6487       (gnus-configure-windows (cdr quit-config) 'force))
6488     (when (eq major-mode 'gnus-summary-mode)
6489       (gnus-summary-next-subject 1 nil t)
6490       (gnus-summary-recenter)
6491       (gnus-summary-position-point))))
6492
6493 ;;; Dead summaries.
6494
6495 (defvar gnus-dead-summary-mode-map nil)
6496
6497 (unless gnus-dead-summary-mode-map
6498   (setq gnus-dead-summary-mode-map (make-keymap))
6499   (suppress-keymap gnus-dead-summary-mode-map)
6500   (substitute-key-definition
6501    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6502   (dolist (key '("\C-d" "\r" "\177" [delete]))
6503     (define-key gnus-dead-summary-mode-map
6504       key 'gnus-summary-wake-up-the-dead))
6505   (dolist (key '("q" "Q"))
6506     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6507
6508 (defvar gnus-dead-summary-mode nil
6509   "Minor mode for Gnus summary buffers.")
6510
6511 (defun gnus-dead-summary-mode (&optional arg)
6512   "Minor mode for Gnus summary buffers."
6513   (interactive "P")
6514   (when (eq major-mode 'gnus-summary-mode)
6515     (make-local-variable 'gnus-dead-summary-mode)
6516     (setq gnus-dead-summary-mode
6517           (if (null arg) (not gnus-dead-summary-mode)
6518             (> (prefix-numeric-value arg) 0)))
6519     (when gnus-dead-summary-mode
6520       (gnus-add-minor-mode
6521        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6522
6523 (defun gnus-deaden-summary ()
6524   "Make the current summary buffer into a dead summary buffer."
6525   ;; Kill any previous dead summary buffer.
6526   (when (and gnus-dead-summary
6527              (buffer-name gnus-dead-summary))
6528     (save-excursion
6529       (set-buffer gnus-dead-summary)
6530       (when gnus-dead-summary-mode
6531         (kill-buffer (current-buffer)))))
6532   ;; Make this the current dead summary.
6533   (setq gnus-dead-summary (current-buffer))
6534   (gnus-dead-summary-mode 1)
6535   (let ((name (buffer-name)))
6536     (when (string-match "Summary" name)
6537       (rename-buffer
6538        (concat (substring name 0 (match-beginning 0)) "Dead "
6539                (substring name (match-beginning 0)))
6540        t)
6541       (bury-buffer))))
6542
6543 (defun gnus-kill-or-deaden-summary (buffer)
6544   "Kill or deaden the summary BUFFER."
6545   (save-excursion
6546     (when (and (buffer-name buffer)
6547                (not gnus-single-article-buffer))
6548       (save-excursion
6549         (set-buffer buffer)
6550         (gnus-kill-buffer gnus-article-buffer)
6551         (gnus-kill-buffer gnus-original-article-buffer)))
6552     (cond
6553      ;; Kill the buffer.
6554      (gnus-kill-summary-on-exit
6555       (when (and gnus-use-trees
6556                  (gnus-buffer-exists-p buffer))
6557         (save-excursion
6558           (set-buffer buffer)
6559           (gnus-tree-close gnus-newsgroup-name)))
6560       (gnus-kill-buffer buffer))
6561      ;; Deaden the buffer.
6562      ((gnus-buffer-exists-p buffer)
6563       (save-excursion
6564         (set-buffer buffer)
6565         (gnus-deaden-summary))))))
6566
6567 (defun gnus-summary-wake-up-the-dead (&rest args)
6568   "Wake up the dead summary buffer."
6569   (interactive)
6570   (gnus-dead-summary-mode -1)
6571   (let ((name (buffer-name)))
6572     (when (string-match "Dead " name)
6573       (rename-buffer
6574        (concat (substring name 0 (match-beginning 0))
6575                (substring name (match-end 0)))
6576        t)))
6577   (gnus-message 3 "This dead summary is now alive again"))
6578
6579 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6580 (defun gnus-summary-fetch-faq (&optional faq-dir)
6581   "Fetch the FAQ for the current group.
6582 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6583 in."
6584   (interactive
6585    (list
6586     (when current-prefix-arg
6587       (completing-read
6588        "FAQ dir: " (and (listp gnus-group-faq-directory)
6589                         (mapcar (lambda (file) (list file))
6590                                 gnus-group-faq-directory))))))
6591   (let (gnus-faq-buffer)
6592     (when (setq gnus-faq-buffer
6593                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6594       (gnus-configure-windows 'summary-faq))))
6595
6596 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6597 (defun gnus-summary-describe-group (&optional force)
6598   "Describe the current newsgroup."
6599   (interactive "P")
6600   (gnus-group-describe-group force gnus-newsgroup-name))
6601
6602 (defun gnus-summary-describe-briefly ()
6603   "Describe summary mode commands briefly."
6604   (interactive)
6605   (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")))
6606
6607 ;; Walking around group mode buffer from summary mode.
6608
6609 (defun gnus-summary-next-group (&optional no-article target-group backward)
6610   "Exit current newsgroup and then select next unread newsgroup.
6611 If prefix argument NO-ARTICLE is non-nil, no article is selected
6612 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6613 previous group instead."
6614   (interactive "P")
6615   ;; Stop pre-fetching.
6616   (gnus-async-halt-prefetch)
6617   (let ((current-group gnus-newsgroup-name)
6618         (current-buffer (current-buffer))
6619         entered)
6620     (while (not entered)
6621       ;; Then we find what group we are supposed to enter.
6622       (set-buffer gnus-group-buffer)
6623       (gnus-group-jump-to-group current-group)
6624       (setq target-group
6625             (or target-group
6626                 (if (eq gnus-keep-same-level 'best)
6627                     (gnus-summary-best-group gnus-newsgroup-name)
6628                   (gnus-summary-search-group backward gnus-keep-same-level))))
6629       (if (not target-group)
6630           ;; There are no further groups, so we return to the group
6631           ;; buffer.
6632           (progn
6633             (gnus-message 5 "Returning to the group buffer")
6634             (setq entered t)
6635             (when (gnus-buffer-live-p current-buffer)
6636               (set-buffer current-buffer)
6637               (gnus-summary-exit))
6638             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6639         ;; We try to enter the target group.
6640         (gnus-group-jump-to-group target-group)
6641         (let ((unreads (gnus-group-group-unread)))
6642           (if (and (or (eq t unreads)
6643                        (and unreads (not (zerop unreads))))
6644                    (progn
6645                      ;; Now we semi-exit this group to update Xrefs
6646                      ;; and all variables.  We can't do a real exit,
6647                      ;; because the window conf must remain the same
6648                      ;; in case the user is prompted for info, and we
6649                      ;; don't want the window conf to change before
6650                      ;; that...
6651                      (when (gnus-buffer-live-p current-buffer)
6652                        (set-buffer current-buffer)
6653                        (gnus-summary-exit t))
6654                      (gnus-summary-read-group
6655                       target-group nil no-article
6656                       (and (buffer-name current-buffer) current-buffer)
6657                       nil backward)))
6658               (setq entered t)
6659             (setq current-group target-group
6660                   target-group nil)))))))
6661
6662 (defun gnus-summary-prev-group (&optional no-article)
6663   "Exit current newsgroup and then select previous unread newsgroup.
6664 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6665   (interactive "P")
6666   (gnus-summary-next-group no-article nil t))
6667
6668 ;; Walking around summary lines.
6669
6670 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6671   "Go to the first unread subject.
6672 If UNREAD is non-nil, go to the first unread article.
6673 Returns the article selected or nil if there are no unread articles."
6674   (interactive "P")
6675   (prog1
6676       (cond
6677        ;; Empty summary.
6678        ((null gnus-newsgroup-data)
6679         (gnus-message 3 "No articles in the group")
6680         nil)
6681        ;; Pick the first article.
6682        ((not unread)
6683         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6684         (gnus-data-number (car gnus-newsgroup-data)))
6685        ;; No unread articles.
6686        ((null gnus-newsgroup-unreads)
6687         (gnus-message 3 "No more unread articles")
6688         nil)
6689        ;; Find the first unread article.
6690        (t
6691         (let ((data gnus-newsgroup-data))
6692           (while (and data
6693                       (and (not (and undownloaded
6694                                      (memq (car data)
6695                                            gnus-newsgroup-undownloaded)))
6696                            (if unseen
6697                                (or (not (memq
6698                                          (gnus-data-number (car data))
6699                                          gnus-newsgroup-unseen))
6700                                    (not (gnus-data-unread-p (car data))))
6701                              (not (gnus-data-unread-p (car data))))))
6702             (setq data (cdr data)))
6703           (when data
6704             (goto-char (gnus-data-pos (car data)))
6705             (gnus-data-number (car data))))))
6706     (gnus-summary-position-point)))
6707
6708 (defun gnus-summary-next-subject (n &optional unread dont-display)
6709   "Go to next N'th summary line.
6710 If N is negative, go to the previous N'th subject line.
6711 If UNREAD is non-nil, only unread articles are selected.
6712 The difference between N and the actual number of steps taken is
6713 returned."
6714   (interactive "p")
6715   (let ((backward (< n 0))
6716         (n (abs n)))
6717     (while (and (> n 0)
6718                 (if backward
6719                     (gnus-summary-find-prev unread)
6720                   (gnus-summary-find-next unread)))
6721       (unless (zerop (setq n (1- n)))
6722         (gnus-summary-show-thread)))
6723     (when (/= 0 n)
6724       (gnus-message 7 "No more%s articles"
6725                     (if unread " unread" "")))
6726     (unless dont-display
6727       (gnus-summary-recenter)
6728       (gnus-summary-position-point))
6729     n))
6730
6731 (defun gnus-summary-next-unread-subject (n)
6732   "Go to next N'th unread summary line."
6733   (interactive "p")
6734   (gnus-summary-next-subject n t))
6735
6736 (defun gnus-summary-prev-subject (n &optional unread)
6737   "Go to previous N'th summary line.
6738 If optional argument UNREAD is non-nil, only unread article is selected."
6739   (interactive "p")
6740   (gnus-summary-next-subject (- n) unread))
6741
6742 (defun gnus-summary-prev-unread-subject (n)
6743   "Go to previous N'th unread summary line."
6744   (interactive "p")
6745   (gnus-summary-next-subject (- n) t))
6746
6747 (defun gnus-summary-goto-subjects (articles)
6748   "Insert the subject header for ARTICLES in the current buffer."
6749   (save-excursion
6750     (dolist (article articles)
6751       (gnus-summary-goto-subject article t)))
6752   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6753   (gnus-summary-position-point))
6754   
6755 (defun gnus-summary-goto-subject (article &optional force silent)
6756   "Go the subject line of ARTICLE.
6757 If FORCE, also allow jumping to articles not currently shown."
6758   (interactive "nArticle number: ")
6759   (unless (numberp article)
6760     (error "Article %s is not a number" article))
6761   (let ((b (point))
6762         (data (gnus-data-find article)))
6763     ;; We read in the article if we have to.
6764     (and (not data)
6765          force
6766          (gnus-summary-insert-subject
6767           article
6768           (if (or (numberp force) (vectorp force)) force)
6769           t)
6770          (setq data (gnus-data-find article)))
6771     (goto-char b)
6772     (if (not data)
6773         (progn
6774           (unless silent
6775             (gnus-message 3 "Can't find article %d" article))
6776           nil)
6777       (let ((pt (gnus-data-pos data)))
6778         (goto-char pt)
6779         (gnus-summary-set-article-display-arrow pt))
6780       (gnus-summary-position-point)
6781       article)))
6782
6783 ;; Walking around summary lines with displaying articles.
6784
6785 (defun gnus-summary-expand-window (&optional arg)
6786   "Make the summary buffer take up the entire Emacs frame.
6787 Given a prefix, will force an `article' buffer configuration."
6788   (interactive "P")
6789   (if arg
6790       (gnus-configure-windows 'article 'force)
6791     (gnus-configure-windows 'summary 'force)))
6792
6793 (defun gnus-summary-display-article (article &optional all-header)
6794   "Display ARTICLE in article buffer."
6795   (when (gnus-buffer-live-p gnus-article-buffer)
6796     (with-current-buffer gnus-article-buffer
6797       (mm-enable-multibyte)))
6798   (gnus-set-global-variables)
6799   (when (gnus-buffer-live-p gnus-article-buffer)
6800     (with-current-buffer gnus-article-buffer
6801       (setq gnus-article-charset gnus-newsgroup-charset)
6802       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6803       (mm-enable-multibyte)))
6804   (if (null article)
6805       nil
6806     (prog1
6807         (if gnus-summary-display-article-function
6808             (funcall gnus-summary-display-article-function article all-header)
6809           (gnus-article-prepare article all-header))
6810       (gnus-run-hooks 'gnus-select-article-hook)
6811       (when (and gnus-current-article
6812                  (not (zerop gnus-current-article)))
6813         (gnus-summary-goto-subject gnus-current-article))
6814       (gnus-summary-recenter)
6815       (when (and gnus-use-trees gnus-show-threads)
6816         (gnus-possibly-generate-tree article)
6817         (gnus-highlight-selected-tree article))
6818       ;; Successfully display article.
6819       (gnus-article-set-window-start
6820        (cdr (assq article gnus-newsgroup-bookmarks))))))
6821
6822 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6823   "Select the current article.
6824 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6825 non-nil, the article will be re-fetched even if it already present in
6826 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6827 be displayed."
6828   ;; Make sure we are in the summary buffer to work around bbdb bug.
6829   (unless (eq major-mode 'gnus-summary-mode)
6830     (set-buffer gnus-summary-buffer))
6831   (let ((article (or article (gnus-summary-article-number)))
6832         (all-headers (not (not all-headers))) ;Must be t or nil.
6833         gnus-summary-display-article-function)
6834     (and (not pseudo)
6835          (gnus-summary-article-pseudo-p article)
6836          (error "This is a pseudo-article"))
6837     (save-excursion
6838       (set-buffer gnus-summary-buffer)
6839       (if (or (and gnus-single-article-buffer
6840                    (or (null gnus-current-article)
6841                        (null gnus-article-current)
6842                        (null (get-buffer gnus-article-buffer))
6843                        (not (eq article (cdr gnus-article-current)))
6844                        (not (equal (car gnus-article-current)
6845                                    gnus-newsgroup-name))))
6846               (and (not gnus-single-article-buffer)
6847                    (or (null gnus-current-article)
6848                        (not (eq gnus-current-article article))))
6849               force)
6850           ;; The requested article is different from the current article.
6851           (progn
6852             (gnus-summary-display-article article all-headers)
6853             (when (gnus-buffer-live-p gnus-article-buffer)
6854               (with-current-buffer gnus-article-buffer
6855                 (if (not gnus-article-decoded-p) ;; a local variable
6856                     (mm-disable-multibyte))))
6857             (gnus-article-set-window-start
6858              (cdr (assq article gnus-newsgroup-bookmarks)))
6859             article)
6860         'old))))
6861
6862 (defun gnus-summary-force-verify-and-decrypt ()
6863   (interactive)
6864   (let ((mm-verify-option 'known)
6865         (mm-decrypt-option 'known)
6866         (gnus-buttonized-mime-types (append (list "multipart/signed"
6867                                                   "multipart/encrypted")
6868                                             gnus-buttonized-mime-types)))
6869     (gnus-summary-select-article nil 'force)))
6870
6871 (defun gnus-summary-set-current-mark (&optional current-mark)
6872   "Obsolete function."
6873   nil)
6874
6875 (defun gnus-summary-next-article (&optional unread subject backward push)
6876   "Select the next article.
6877 If UNREAD, only unread articles are selected.
6878 If SUBJECT, only articles with SUBJECT are selected.
6879 If BACKWARD, the previous article is selected instead of the next."
6880   (interactive "P")
6881   (cond
6882    ;; Is there such an article?
6883    ((and (gnus-summary-search-forward unread subject backward)
6884          (or (gnus-summary-display-article (gnus-summary-article-number))
6885              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6886     (gnus-summary-position-point))
6887    ;; If not, we try the first unread, if that is wanted.
6888    ((and subject
6889          gnus-auto-select-same
6890          (gnus-summary-first-unread-article))
6891     (gnus-summary-position-point)
6892     (gnus-message 6 "Wrapped"))
6893    ;; Try to get next/previous article not displayed in this group.
6894    ((and gnus-auto-extend-newsgroup
6895          (not unread) (not subject))
6896     (gnus-summary-goto-article
6897      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6898      nil (count-lines (point-min) (point))))
6899    ;; Go to next/previous group.
6900    (t
6901     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6902       (gnus-summary-jump-to-group gnus-newsgroup-name))
6903     (let ((cmd last-command-char)
6904           (point
6905            (save-excursion
6906              (set-buffer gnus-group-buffer)
6907              (point)))
6908           (group
6909            (if (eq gnus-keep-same-level 'best)
6910                (gnus-summary-best-group gnus-newsgroup-name)
6911              (gnus-summary-search-group backward gnus-keep-same-level))))
6912       ;; For some reason, the group window gets selected.  We change
6913       ;; it back.
6914       (select-window (get-buffer-window (current-buffer)))
6915       ;; Select next unread newsgroup automagically.
6916       (cond
6917        ((or (not gnus-auto-select-next)
6918             (not cmd))
6919         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6920        ((or (eq gnus-auto-select-next 'quietly)
6921             (and (eq gnus-auto-select-next 'slightly-quietly)
6922                  push)
6923             (and (eq gnus-auto-select-next 'almost-quietly)
6924                  (gnus-summary-last-article-p)))
6925         ;; Select quietly.
6926         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6927             (gnus-summary-exit)
6928           (gnus-message 7 "No more%s articles (%s)..."
6929                         (if unread " unread" "")
6930                         (if group (concat "selecting " group)
6931                           "exiting"))
6932           (gnus-summary-next-group nil group backward)))
6933        (t
6934         (when (gnus-key-press-event-p last-input-event)
6935           (gnus-summary-walk-group-buffer
6936            gnus-newsgroup-name cmd unread backward point))))))))
6937
6938 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6939   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6940                       (?\C-p (gnus-group-prev-unread-group 1))))
6941         (cursor-in-echo-area t)
6942         keve key group ended prompt)
6943     (save-excursion
6944       (set-buffer gnus-group-buffer)
6945       (goto-char start)
6946       (setq group
6947             (if (eq gnus-keep-same-level 'best)
6948                 (gnus-summary-best-group gnus-newsgroup-name)
6949               (gnus-summary-search-group backward gnus-keep-same-level))))
6950     (while (not ended)
6951       (setq prompt
6952             (format
6953              "No more%s articles%s " (if unread " unread" "")
6954              (if (and group
6955                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6956                  (format " (Type %s for %s [%s])"
6957                          (single-key-description cmd) group
6958                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6959                (format " (Type %s to exit %s)"
6960                        (single-key-description cmd)
6961                        gnus-newsgroup-name))))
6962       ;; Confirm auto selection.
6963       (setq key (car (setq keve (gnus-read-event-char prompt)))
6964             ended t)
6965       (cond
6966        ((assq key keystrokes)
6967         (let ((obuf (current-buffer)))
6968           (switch-to-buffer gnus-group-buffer)
6969           (when group
6970             (gnus-group-jump-to-group group))
6971           (eval (cadr (assq key keystrokes)))
6972           (setq group (gnus-group-group-name))
6973           (switch-to-buffer obuf))
6974         (setq ended nil))
6975        ((equal key cmd)
6976         (if (or (not group)
6977                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6978             (gnus-summary-exit)
6979           (gnus-summary-next-group nil group backward)))
6980        (t
6981         (push (cdr keve) unread-command-events))))))
6982
6983 (defun gnus-summary-next-unread-article ()
6984   "Select unread article after current one."
6985   (interactive)
6986   (gnus-summary-next-article
6987    (or (not (eq gnus-summary-goto-unread 'never))
6988        (gnus-summary-last-article-p (gnus-summary-article-number)))
6989    (and gnus-auto-select-same
6990         (gnus-summary-article-subject))))
6991
6992 (defun gnus-summary-prev-article (&optional unread subject)
6993   "Select the article after the current one.
6994 If UNREAD is non-nil, only unread articles are selected."
6995   (interactive "P")
6996   (gnus-summary-next-article unread subject t))
6997
6998 (defun gnus-summary-prev-unread-article ()
6999   "Select unread article before current one."
7000   (interactive)
7001   (gnus-summary-prev-article
7002    (or (not (eq gnus-summary-goto-unread 'never))
7003        (gnus-summary-first-article-p (gnus-summary-article-number)))
7004    (and gnus-auto-select-same
7005         (gnus-summary-article-subject))))
7006
7007 (defun gnus-summary-next-page (&optional lines circular stop)
7008   "Show next page of the selected article.
7009 If at the end of the current article, select the next article.
7010 LINES says how many lines should be scrolled up.
7011
7012 If CIRCULAR is non-nil, go to the start of the article instead of
7013 selecting the next article when reaching the end of the current
7014 article.
7015
7016 If STOP is non-nil, just stop when reaching the end of the message."
7017   (interactive "P")
7018   (setq gnus-summary-buffer (current-buffer))
7019   (gnus-set-global-variables)
7020   (let ((article (gnus-summary-article-number))
7021         (article-window (get-buffer-window gnus-article-buffer t))
7022         endp)
7023     ;; If the buffer is empty, we have no article.
7024     (unless article
7025       (error "No article to select"))
7026     (gnus-configure-windows 'article)
7027     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7028         (if (and (eq gnus-summary-goto-unread 'never)
7029                  (not (gnus-summary-last-article-p article)))
7030             (gnus-summary-next-article)
7031           (gnus-summary-next-unread-article))
7032       (if (or (null gnus-current-article)
7033               (null gnus-article-current)
7034               (/= article (cdr gnus-article-current))
7035               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7036           ;; Selected subject is different from current article's.
7037           (gnus-summary-display-article article)
7038         (when article-window
7039           (gnus-eval-in-buffer-window gnus-article-buffer
7040             (setq endp (gnus-article-next-page lines)))
7041           (when endp
7042             (cond (stop
7043                    (gnus-message 3 "End of message"))
7044                   (circular
7045                    (gnus-summary-beginning-of-article))
7046                   (lines
7047                    (gnus-message 3 "End of message"))
7048                   ((null lines)
7049                    (if (and (eq gnus-summary-goto-unread 'never)
7050                             (not (gnus-summary-last-article-p article)))
7051                        (gnus-summary-next-article)
7052                      (gnus-summary-next-unread-article))))))))
7053     (gnus-summary-recenter)
7054     (gnus-summary-position-point)))
7055
7056 (defun gnus-summary-prev-page (&optional lines move)
7057   "Show previous page of selected article.
7058 Argument LINES specifies lines to be scrolled down.
7059 If MOVE, move to the previous unread article if point is at
7060 the beginning of the buffer."
7061   (interactive "P")
7062   (let ((article (gnus-summary-article-number))
7063         (article-window (get-buffer-window gnus-article-buffer t))
7064         endp)
7065     (gnus-configure-windows 'article)
7066     (if (or (null gnus-current-article)
7067             (null gnus-article-current)
7068             (/= article (cdr gnus-article-current))
7069             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7070         ;; Selected subject is different from current article's.
7071         (gnus-summary-display-article article)
7072       (gnus-summary-recenter)
7073       (when article-window
7074         (gnus-eval-in-buffer-window gnus-article-buffer
7075           (setq endp (gnus-article-prev-page lines)))
7076         (when (and move endp)
7077           (cond (lines
7078                  (gnus-message 3 "Beginning of message"))
7079                 ((null lines)
7080                  (if (and (eq gnus-summary-goto-unread 'never)
7081                           (not (gnus-summary-first-article-p article)))
7082                      (gnus-summary-prev-article)
7083                    (gnus-summary-prev-unread-article))))))))
7084   (gnus-summary-position-point))
7085
7086 (defun gnus-summary-prev-page-or-article (&optional lines)
7087   "Show previous page of selected article.
7088 Argument LINES specifies lines to be scrolled down.
7089 If at the beginning of the article, go to the next article."
7090   (interactive "P")
7091   (gnus-summary-prev-page lines t))
7092
7093 (defun gnus-summary-scroll-up (lines)
7094   "Scroll up (or down) one line current article.
7095 Argument LINES specifies lines to be scrolled up (or down if negative)."
7096   (interactive "p")
7097   (gnus-configure-windows 'article)
7098   (gnus-summary-show-thread)
7099   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7100     (gnus-eval-in-buffer-window gnus-article-buffer
7101       (cond ((> lines 0)
7102              (when (gnus-article-next-page lines)
7103                (gnus-message 3 "End of message")))
7104             ((< lines 0)
7105              (gnus-article-prev-page (- lines))))))
7106   (gnus-summary-recenter)
7107   (gnus-summary-position-point))
7108
7109 (defun gnus-summary-scroll-down (lines)
7110   "Scroll down (or up) one line current article.
7111 Argument LINES specifies lines to be scrolled down (or up if negative)."
7112   (interactive "p")
7113   (gnus-summary-scroll-up (- lines)))
7114
7115 (defun gnus-summary-next-same-subject ()
7116   "Select next article which has the same subject as current one."
7117   (interactive)
7118   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7119
7120 (defun gnus-summary-prev-same-subject ()
7121   "Select previous article which has the same subject as current one."
7122   (interactive)
7123   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7124
7125 (defun gnus-summary-next-unread-same-subject ()
7126   "Select next unread article which has the same subject as current one."
7127   (interactive)
7128   (gnus-summary-next-article t (gnus-summary-article-subject)))
7129
7130 (defun gnus-summary-prev-unread-same-subject ()
7131   "Select previous unread article which has the same subject as current one."
7132   (interactive)
7133   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7134
7135 (defun gnus-summary-first-unread-article ()
7136   "Select the first unread article.
7137 Return nil if there are no unread articles."
7138   (interactive)
7139   (prog1
7140       (when (gnus-summary-first-subject t)
7141         (gnus-summary-show-thread)
7142         (gnus-summary-first-subject t)
7143         (gnus-summary-display-article (gnus-summary-article-number)))
7144     (gnus-summary-position-point)))
7145
7146 (defun gnus-summary-first-unread-subject ()
7147   "Place the point on the subject line of the first unread article.
7148 Return nil if there are no unread articles."
7149   (interactive)
7150   (prog1
7151       (when (gnus-summary-first-subject t)
7152         (gnus-summary-show-thread)
7153         (gnus-summary-first-subject t))
7154     (gnus-summary-position-point)))
7155
7156 (defun gnus-summary-first-unseen-subject ()
7157   "Place the point on the subject line of the first unseen article.
7158 Return nil if there are no unseen articles."
7159   (interactive)
7160   (prog1
7161       (when (gnus-summary-first-subject t t t)
7162         (gnus-summary-show-thread)
7163         (gnus-summary-first-subject t t t))
7164     (gnus-summary-position-point)))
7165
7166 (defun gnus-summary-first-unseen-or-unread-subject ()
7167   "Place the point on the subject line of the first unseen article.
7168 Return nil if there are no unseen articles."
7169   (interactive)
7170   (prog1
7171       (unless (when (gnus-summary-first-subject t t t)
7172                 (gnus-summary-show-thread)
7173                 (gnus-summary-first-subject t t t))
7174         (when (gnus-summary-first-subject t)
7175           (gnus-summary-show-thread)
7176           (gnus-summary-first-subject t)))
7177     (gnus-summary-position-point)))
7178
7179 (defun gnus-summary-first-article ()
7180   "Select the first article.
7181 Return nil if there are no articles."
7182   (interactive)
7183   (prog1
7184       (when (gnus-summary-first-subject)
7185         (gnus-summary-show-thread)
7186         (gnus-summary-first-subject)
7187         (gnus-summary-display-article (gnus-summary-article-number)))
7188     (gnus-summary-position-point)))
7189
7190 (defun gnus-summary-best-unread-article (&optional arg)
7191   "Select the unread article with the highest score.
7192 If given a prefix argument, select the next unread article that has a
7193 score higher than the default score."
7194   (interactive "P")
7195   (let ((article (if arg
7196                      (gnus-summary-better-unread-subject)
7197                    (gnus-summary-best-unread-subject))))
7198     (if article
7199         (gnus-summary-goto-article article)
7200       (error "No unread articles"))))
7201
7202 (defun gnus-summary-best-unread-subject ()
7203   "Select the unread subject with the highest score."
7204   (interactive)
7205   (let ((best -1000000)
7206         (data gnus-newsgroup-data)
7207         article score)
7208     (while data
7209       (and (gnus-data-unread-p (car data))
7210            (> (setq score
7211                     (gnus-summary-article-score (gnus-data-number (car data))))
7212               best)
7213            (setq best score
7214                  article (gnus-data-number (car data))))
7215       (setq data (cdr data)))
7216     (when article
7217       (gnus-summary-goto-subject article))
7218     (gnus-summary-position-point)
7219     article))
7220
7221 (defun gnus-summary-better-unread-subject ()
7222   "Select the first unread subject that has a score over the default score."
7223   (interactive)
7224   (let ((data gnus-newsgroup-data)
7225         article score)
7226     (while (and (setq article (gnus-data-number (car data)))
7227                 (or (gnus-data-read-p (car data))
7228                     (not (> (gnus-summary-article-score article)
7229                             gnus-summary-default-score))))
7230       (setq data (cdr data)))
7231     (when article
7232       (gnus-summary-goto-subject article))
7233     (gnus-summary-position-point)
7234     article))
7235
7236 (defun gnus-summary-last-subject ()
7237   "Go to the last displayed subject line in the group."
7238   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7239     (when article
7240       (gnus-summary-goto-subject article))))
7241
7242 (defun gnus-summary-goto-article (article &optional all-headers force)
7243   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7244 If ALL-HEADERS is non-nil, no header lines are hidden.
7245 If FORCE, go to the article even if it isn't displayed.  If FORCE
7246 is a number, it is the line the article is to be displayed on."
7247   (interactive
7248    (list
7249     (completing-read
7250      "Article number or Message-ID: "
7251      (mapcar (lambda (number) (list (int-to-string number)))
7252              gnus-newsgroup-limit))
7253     current-prefix-arg
7254     t))
7255   (prog1
7256       (if (and (stringp article)
7257                (string-match "@" article))
7258           (gnus-summary-refer-article article)
7259         (when (stringp article)
7260           (setq article (string-to-number article)))
7261         (if (gnus-summary-goto-subject article force)
7262             (gnus-summary-display-article article all-headers)
7263           (gnus-message 4 "Couldn't go to article %s" article) nil))
7264     (gnus-summary-position-point)))
7265
7266 (defun gnus-summary-goto-last-article ()
7267   "Go to the previously read article."
7268   (interactive)
7269   (prog1
7270       (when gnus-last-article
7271         (gnus-summary-goto-article gnus-last-article nil t))
7272     (gnus-summary-position-point)))
7273
7274 (defun gnus-summary-pop-article (number)
7275   "Pop one article off the history and go to the previous.
7276 NUMBER articles will be popped off."
7277   (interactive "p")
7278   (let (to)
7279     (setq gnus-newsgroup-history
7280           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7281     (if to
7282         (gnus-summary-goto-article (car to) nil t)
7283       (error "Article history empty")))
7284   (gnus-summary-position-point))
7285
7286 ;; Summary commands and functions for limiting the summary buffer.
7287
7288 (defun gnus-summary-limit-to-articles (n)
7289   "Limit the summary buffer to the next N articles.
7290 If not given a prefix, use the process marked articles instead."
7291   (interactive "P")
7292   (prog1
7293       (let ((articles (gnus-summary-work-articles n)))
7294         (setq gnus-newsgroup-processable nil)
7295         (gnus-summary-limit articles))
7296     (gnus-summary-position-point)))
7297
7298 (defun gnus-summary-pop-limit (&optional total)
7299   "Restore the previous limit.
7300 If given a prefix, remove all limits."
7301   (interactive "P")
7302   (when total
7303     (setq gnus-newsgroup-limits
7304           (list (mapcar (lambda (h) (mail-header-number h))
7305                         gnus-newsgroup-headers))))
7306   (unless gnus-newsgroup-limits
7307     (error "No limit to pop"))
7308   (prog1
7309       (gnus-summary-limit nil 'pop)
7310     (gnus-summary-position-point)))
7311
7312 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7313   "Limit the summary buffer to articles that have subjects that match a regexp.
7314 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7315   (interactive
7316    (list (read-string (if current-prefix-arg
7317                           "Exclude subject (regexp): "
7318                         "Limit to subject (regexp): "))
7319          nil current-prefix-arg))
7320   (unless header
7321     (setq header "subject"))
7322   (when (not (equal "" subject))
7323     (prog1
7324         (let ((articles (gnus-summary-find-matching
7325                          (or header "subject") subject 'all nil nil
7326                          not-matching)))
7327           (unless articles
7328             (error "Found no matches for \"%s\"" subject))
7329           (gnus-summary-limit articles))
7330       (gnus-summary-position-point))))
7331
7332 (defun gnus-summary-limit-to-author (from &optional not-matching)
7333   "Limit the summary buffer to articles that have authors that match a regexp.
7334 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7335   (interactive
7336    (list (read-string (if current-prefix-arg
7337                           "Exclude author (regexp): "
7338                         "Limit to author (regexp): "))
7339          current-prefix-arg))
7340   (gnus-summary-limit-to-subject from "from" not-matching))
7341
7342 (defun gnus-summary-limit-to-age (age &optional younger-p)
7343   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7344 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7345 articles that are younger than AGE days."
7346   (interactive
7347    (let ((younger current-prefix-arg)
7348          (days-got nil)
7349          days)
7350      (while (not days-got)
7351        (setq days (if younger
7352                       (read-string "Limit to articles younger than (in days, older when negative): ")
7353                     (read-string
7354                      "Limit to articles older than (in days, younger when negative): ")))
7355        (when (> (length days) 0)
7356          (setq days (read days)))
7357        (if (numberp days)
7358            (progn
7359              (setq days-got t)
7360              (if (< days 0)
7361                  (progn
7362                    (setq younger (not younger))
7363                    (setq days (* days -1)))))
7364          (message "Please enter a number.")
7365          (sleep-for 1)))
7366      (list days younger)))
7367   (prog1
7368       (let ((data gnus-newsgroup-data)
7369             (cutoff (days-to-time age))
7370             articles d date is-younger)
7371         (while (setq d (pop data))
7372           (when (and (vectorp (gnus-data-header d))
7373                      (setq date (mail-header-date (gnus-data-header d))))
7374             (setq is-younger (time-less-p
7375                               (time-since (condition-case ()
7376                                               (date-to-time date)
7377                                             (error '(0 0))))
7378                               cutoff))
7379             (when (if younger-p
7380                       is-younger
7381                     (not is-younger))
7382               (push (gnus-data-number d) articles))))
7383         (gnus-summary-limit (nreverse articles)))
7384     (gnus-summary-position-point)))
7385
7386 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7387   "Limit the summary buffer to articles that match an 'extra' header."
7388   (interactive
7389    (let ((header
7390           (intern
7391            (gnus-completing-read-with-default
7392             (symbol-name (car gnus-extra-headers))
7393             (if current-prefix-arg
7394                 "Exclude extra header:"
7395               "Limit extra header:")
7396             (mapcar (lambda (x)
7397                       (cons (symbol-name x) x))
7398                     gnus-extra-headers)
7399             nil
7400             t))))
7401      (list header
7402            (read-string (format "%s header %s (regexp): "
7403                                 (if current-prefix-arg "Exclude" "Limit to")
7404                                 header))
7405            current-prefix-arg)))
7406   (when (not (equal "" regexp))
7407     (prog1
7408         (let ((articles (gnus-summary-find-matching
7409                          (cons 'extra header) regexp 'all nil nil
7410                          not-matching)))
7411           (unless articles
7412             (error "Found no matches for \"%s\"" regexp))
7413           (gnus-summary-limit articles))
7414       (gnus-summary-position-point))))
7415
7416 (defun gnus-summary-limit-to-display-predicate ()
7417   "Limit the summary buffer to the predicated in the `display' group parameter."
7418   (interactive)
7419   (unless gnus-newsgroup-display
7420     (error "There is no `display' group parameter"))
7421   (let (articles)
7422     (dolist (number gnus-newsgroup-articles)
7423       (when (funcall gnus-newsgroup-display)
7424         (push number articles)))
7425     (gnus-summary-limit articles))
7426   (gnus-summary-position-point))
7427
7428 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7429 (make-obsolete
7430  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7431
7432 (defun gnus-summary-limit-to-unread (&optional all)
7433   "Limit the summary buffer to articles that are not marked as read.
7434 If ALL is non-nil, limit strictly to unread articles."
7435   (interactive "P")
7436   (if all
7437       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7438     (gnus-summary-limit-to-marks
7439      ;; Concat all the marks that say that an article is read and have
7440      ;; those removed.
7441      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7442            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7443            gnus-low-score-mark gnus-expirable-mark
7444            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7445            gnus-duplicate-mark gnus-souped-mark)
7446      'reverse)))
7447
7448 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7449 (make-obsolete 'gnus-summary-delete-marked-with
7450                'gnus-summary-limit-exclude-marks)
7451
7452 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7453   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7454 If REVERSE, limit the summary buffer to articles that are marked
7455 with MARKS.  MARKS can either be a string of marks or a list of marks.
7456 Returns how many articles were removed."
7457   (interactive "sMarks: ")
7458   (gnus-summary-limit-to-marks marks t))
7459
7460 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7461   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7462 If REVERSE (the prefix), limit the summary buffer to articles that are
7463 not marked with MARKS.  MARKS can either be a string of marks or a
7464 list of marks.
7465 Returns how many articles were removed."
7466   (interactive "sMarks: \nP")
7467   (prog1
7468       (let ((data gnus-newsgroup-data)
7469             (marks (if (listp marks) marks
7470                      (append marks nil))) ; Transform to list.
7471             articles)
7472         (while data
7473           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7474                   (memq (gnus-data-mark (car data)) marks))
7475             (push (gnus-data-number (car data)) articles))
7476           (setq data (cdr data)))
7477         (gnus-summary-limit articles))
7478     (gnus-summary-position-point)))
7479
7480 (defun gnus-summary-limit-to-score (score)
7481   "Limit to articles with score at or above SCORE."
7482   (interactive "NLimit to articles with score of at least: ")
7483   (let ((data gnus-newsgroup-data)
7484         articles)
7485     (while data
7486       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7487                 score)
7488         (push (gnus-data-number (car data)) articles))
7489       (setq data (cdr data)))
7490     (prog1
7491         (gnus-summary-limit articles)
7492       (gnus-summary-position-point))))
7493
7494 (defun gnus-summary-limit-to-unseen ()
7495   "Limit to unseen articles."
7496   (interactive)
7497   (prog1
7498       (gnus-summary-limit gnus-newsgroup-unseen)
7499     (gnus-summary-position-point)))
7500
7501 (defun gnus-summary-limit-include-thread (id)
7502   "Display all the hidden articles that is in the thread with ID in it.
7503 When called interactively, ID is the Message-ID of the current
7504 article."
7505   (interactive (list (mail-header-id (gnus-summary-article-header))))
7506   (let ((articles (gnus-articles-in-thread
7507                    (gnus-id-to-thread (gnus-root-id id)))))
7508     (prog1
7509         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7510       (gnus-summary-limit-include-matching-articles
7511        "subject"
7512        (regexp-quote (gnus-simplify-subject-re
7513                       (mail-header-subject (gnus-id-to-header id)))))
7514       (gnus-summary-position-point))))
7515
7516 (defun gnus-summary-limit-include-matching-articles (header regexp)
7517   "Display all the hidden articles that have HEADERs that match REGEXP."
7518   (interactive (list (read-string "Match on header: ")
7519                      (read-string "Regexp: ")))
7520   (let ((articles (gnus-find-matching-articles header regexp)))
7521     (prog1
7522         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7523       (gnus-summary-position-point))))
7524
7525 (defun gnus-summary-insert-dormant-articles ()
7526   "Insert all the dormat articles for this group into the current buffer."
7527   (interactive)
7528   (let ((gnus-verbose (max 6 gnus-verbose)))
7529     (if (not gnus-newsgroup-dormant)
7530         (gnus-message 3 "No cached articles for this group")
7531       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7532
7533 (defun gnus-summary-limit-include-dormant ()
7534   "Display all the hidden articles that are marked as dormant.
7535 Note that this command only works on a subset of the articles currently
7536 fetched for this group."
7537   (interactive)
7538   (unless gnus-newsgroup-dormant
7539     (error "There are no dormant articles in this group"))
7540   (prog1
7541       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7542     (gnus-summary-position-point)))
7543
7544 (defun gnus-summary-limit-exclude-dormant ()
7545   "Hide all dormant articles."
7546   (interactive)
7547   (prog1
7548       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7549     (gnus-summary-position-point)))
7550
7551 (defun gnus-summary-limit-exclude-childless-dormant ()
7552   "Hide all dormant articles that have no children."
7553   (interactive)
7554   (let ((data (gnus-data-list t))
7555         articles d children)
7556     ;; Find all articles that are either not dormant or have
7557     ;; children.
7558     (while (setq d (pop data))
7559       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7560                 (and (setq children
7561                            (gnus-article-children (gnus-data-number d)))
7562                      (let (found)
7563                        (while children
7564                          (when (memq (car children) articles)
7565                            (setq children nil
7566                                  found t))
7567                          (pop children))
7568                        found)))
7569         (push (gnus-data-number d) articles)))
7570     ;; Do the limiting.
7571     (prog1
7572         (gnus-summary-limit articles)
7573       (gnus-summary-position-point))))
7574
7575 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7576   "Mark all unread excluded articles as read.
7577 If ALL, mark even excluded ticked and dormants as read."
7578   (interactive "P")
7579   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7580   (let ((articles (gnus-sorted-ndifference
7581                    (sort
7582                     (mapcar (lambda (h) (mail-header-number h))
7583                             gnus-newsgroup-headers)
7584                     '<)
7585                    gnus-newsgroup-limit))
7586         article)
7587     (setq gnus-newsgroup-unreads
7588           (gnus-sorted-intersection gnus-newsgroup-unreads
7589                                     gnus-newsgroup-limit))
7590     (if all
7591         (setq gnus-newsgroup-dormant nil
7592               gnus-newsgroup-marked nil
7593               gnus-newsgroup-reads
7594               (nconc
7595                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7596                gnus-newsgroup-reads))
7597       (while (setq article (pop articles))
7598         (unless (or (memq article gnus-newsgroup-dormant)
7599                     (memq article gnus-newsgroup-marked))
7600           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7601
7602 (defun gnus-summary-limit (articles &optional pop)
7603   (if pop
7604       ;; We pop the previous limit off the stack and use that.
7605       (setq articles (car gnus-newsgroup-limits)
7606             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7607     ;; We use the new limit, so we push the old limit on the stack.
7608     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7609   ;; Set the limit.
7610   (setq gnus-newsgroup-limit articles)
7611   (let ((total (length gnus-newsgroup-data))
7612         (data (gnus-data-find-list (gnus-summary-article-number)))
7613         (gnus-summary-mark-below nil)   ; Inhibit this.
7614         found)
7615     ;; This will do all the work of generating the new summary buffer
7616     ;; according to the new limit.
7617     (gnus-summary-prepare)
7618     ;; Hide any threads, possibly.
7619     (gnus-summary-maybe-hide-threads)
7620     ;; Try to return to the article you were at, or one in the
7621     ;; neighborhood.
7622     (when data
7623       ;; We try to find some article after the current one.
7624       (while data
7625         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7626           (setq data nil
7627                 found t))
7628         (setq data (cdr data))))
7629     (unless found
7630       ;; If there is no data, that means that we were after the last
7631       ;; article.  The same goes when we can't find any articles
7632       ;; after the current one.
7633       (goto-char (point-max))
7634       (gnus-summary-find-prev))
7635     (gnus-set-mode-line 'summary)
7636     ;; We return how many articles were removed from the summary
7637     ;; buffer as a result of the new limit.
7638     (- total (length gnus-newsgroup-data))))
7639
7640 (defsubst gnus-invisible-cut-children (threads)
7641   (let ((num 0))
7642     (while threads
7643       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7644         (incf num))
7645       (pop threads))
7646     (< num 2)))
7647
7648 (defsubst gnus-cut-thread (thread)
7649   "Go forwards in the thread until we find an article that we want to display."
7650   (when (or (eq gnus-fetch-old-headers 'some)
7651             (eq gnus-fetch-old-headers 'invisible)
7652             (numberp gnus-fetch-old-headers)
7653             (eq gnus-build-sparse-threads 'some)
7654             (eq gnus-build-sparse-threads 'more))
7655     ;; Deal with old-fetched headers and sparse threads.
7656     (while (and
7657             thread
7658             (or
7659              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7660              (gnus-summary-article-ancient-p
7661               (mail-header-number (car thread))))
7662             (if (or (<= (length (cdr thread)) 1)
7663                     (eq gnus-fetch-old-headers 'invisible))
7664                 (setq gnus-newsgroup-limit
7665                       (delq (mail-header-number (car thread))
7666                             gnus-newsgroup-limit)
7667                       thread (cadr thread))
7668               (when (gnus-invisible-cut-children (cdr thread))
7669                 (let ((th (cdr thread)))
7670                   (while th
7671                     (if (memq (mail-header-number (caar th))
7672                               gnus-newsgroup-limit)
7673                         (setq thread (car th)
7674                               th nil)
7675                       (setq th (cdr th))))))))))
7676   thread)
7677
7678 (defun gnus-cut-threads (threads)
7679   "Cut off all uninteresting articles from the beginning of threads."
7680   (when (or (eq gnus-fetch-old-headers 'some)
7681             (eq gnus-fetch-old-headers 'invisible)
7682             (numberp gnus-fetch-old-headers)
7683             (eq gnus-build-sparse-threads 'some)
7684             (eq gnus-build-sparse-threads 'more))
7685     (let ((th threads))
7686       (while th
7687         (setcar th (gnus-cut-thread (car th)))
7688         (setq th (cdr th)))))
7689   ;; Remove nixed out threads.
7690   (delq nil threads))
7691
7692 (defun gnus-summary-initial-limit (&optional show-if-empty)
7693   "Figure out what the initial limit is supposed to be on group entry.
7694 This entails weeding out unwanted dormants, low-scored articles,
7695 fetch-old-headers verbiage, and so on."
7696   ;; Most groups have nothing to remove.
7697   (if (or gnus-inhibit-limiting
7698           (and (null gnus-newsgroup-dormant)
7699                (eq gnus-newsgroup-display 'gnus-not-ignore)
7700                (not (eq gnus-fetch-old-headers 'some))
7701                (not (numberp gnus-fetch-old-headers))
7702                (not (eq gnus-fetch-old-headers 'invisible))
7703                (null gnus-summary-expunge-below)
7704                (not (eq gnus-build-sparse-threads 'some))
7705                (not (eq gnus-build-sparse-threads 'more))
7706                (null gnus-thread-expunge-below)
7707                (not gnus-use-nocem)))
7708       ()                                ; Do nothing.
7709     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7710     (setq gnus-newsgroup-limit nil)
7711     (mapatoms
7712      (lambda (node)
7713        (unless (car (symbol-value node))
7714          ;; These threads have no parents -- they are roots.
7715          (let ((nodes (cdr (symbol-value node)))
7716                thread)
7717            (while nodes
7718              (if (and gnus-thread-expunge-below
7719                       (< (gnus-thread-total-score (car nodes))
7720                          gnus-thread-expunge-below))
7721                  (gnus-expunge-thread (pop nodes))
7722                (setq thread (pop nodes))
7723                (gnus-summary-limit-children thread))))))
7724      gnus-newsgroup-dependencies)
7725     ;; If this limitation resulted in an empty group, we might
7726     ;; pop the previous limit and use it instead.
7727     (when (and (not gnus-newsgroup-limit)
7728                show-if-empty)
7729       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7730     gnus-newsgroup-limit))
7731
7732 (defun gnus-summary-limit-children (thread)
7733   "Return 1 if this subthread is visible and 0 if it is not."
7734   ;; First we get the number of visible children to this thread.  This
7735   ;; is done by recursing down the thread using this function, so this
7736   ;; will really go down to a leaf article first, before slowly
7737   ;; working its way up towards the root.
7738   (when thread
7739     (let* ((max-lisp-eval-depth 5000)
7740            (children
7741            (if (cdr thread)
7742                (apply '+ (mapcar 'gnus-summary-limit-children
7743                                  (cdr thread)))
7744              0))
7745           (number (mail-header-number (car thread)))
7746           score)
7747       (if (and
7748            (not (memq number gnus-newsgroup-marked))
7749            (or
7750             ;; If this article is dormant and has absolutely no visible
7751             ;; children, then this article isn't visible.
7752             (and (memq number gnus-newsgroup-dormant)
7753                  (zerop children))
7754             ;; If this is "fetch-old-headered" and there is no
7755             ;; visible children, then we don't want this article.
7756             (and (or (eq gnus-fetch-old-headers 'some)
7757                      (numberp gnus-fetch-old-headers))
7758                  (gnus-summary-article-ancient-p number)
7759                  (zerop children))
7760             ;; If this is "fetch-old-headered" and `invisible', then
7761             ;; we don't want this article.
7762             (and (eq gnus-fetch-old-headers 'invisible)
7763                  (gnus-summary-article-ancient-p number))
7764             ;; If this is a sparsely inserted article with no children,
7765             ;; we don't want it.
7766             (and (eq gnus-build-sparse-threads 'some)
7767                  (gnus-summary-article-sparse-p number)
7768                  (zerop children))
7769             ;; If we use expunging, and this article is really
7770             ;; low-scored, then we don't want this article.
7771             (when (and gnus-summary-expunge-below
7772                        (< (setq score
7773                                 (or (cdr (assq number gnus-newsgroup-scored))
7774                                     gnus-summary-default-score))
7775                           gnus-summary-expunge-below))
7776               ;; We increase the expunge-tally here, but that has
7777               ;; nothing to do with the limits, really.
7778               (incf gnus-newsgroup-expunged-tally)
7779               ;; We also mark as read here, if that's wanted.
7780               (when (and gnus-summary-mark-below
7781                          (< score gnus-summary-mark-below))
7782                 (setq gnus-newsgroup-unreads
7783                       (delq number gnus-newsgroup-unreads))
7784                 (if gnus-newsgroup-auto-expire
7785                     (push number gnus-newsgroup-expirable)
7786                   (push (cons number gnus-low-score-mark)
7787                         gnus-newsgroup-reads)))
7788               t)
7789             ;; Do the `display' group parameter.
7790             (and gnus-newsgroup-display
7791                  (not (funcall gnus-newsgroup-display)))
7792             ;; Check NoCeM things.
7793             (if (and gnus-use-nocem
7794                      (gnus-nocem-unwanted-article-p
7795                       (mail-header-id (car thread))))
7796                 (progn
7797                   (setq gnus-newsgroup-unreads
7798                         (delq number gnus-newsgroup-unreads))
7799                   t))))
7800           ;; Nope, invisible article.
7801           0
7802         ;; Ok, this article is to be visible, so we add it to the limit
7803         ;; and return 1.
7804         (push number gnus-newsgroup-limit)
7805         1))))
7806
7807 (defun gnus-expunge-thread (thread)
7808   "Mark all articles in THREAD as read."
7809   (let* ((number (mail-header-number (car thread))))
7810     (incf gnus-newsgroup-expunged-tally)
7811     ;; We also mark as read here, if that's wanted.
7812     (setq gnus-newsgroup-unreads
7813           (delq number gnus-newsgroup-unreads))
7814     (if gnus-newsgroup-auto-expire
7815         (push number gnus-newsgroup-expirable)
7816       (push (cons number gnus-low-score-mark)
7817             gnus-newsgroup-reads)))
7818   ;; Go recursively through all subthreads.
7819   (mapcar 'gnus-expunge-thread (cdr thread)))
7820
7821 ;; Summary article oriented commands
7822
7823 (defun gnus-summary-refer-parent-article (n)
7824   "Refer parent article N times.
7825 If N is negative, go to ancestor -N instead.
7826 The difference between N and the number of articles fetched is returned."
7827   (interactive "p")
7828   (let ((skip 1)
7829         error header ref)
7830     (when (not (natnump n))
7831       (setq skip (abs n)
7832             n 1))
7833     (while (and (> n 0)
7834                 (not error))
7835       (setq header (gnus-summary-article-header))
7836       (if (and (eq (mail-header-number header)
7837                    (cdr gnus-article-current))
7838                (equal gnus-newsgroup-name
7839                       (car gnus-article-current)))
7840           ;; If we try to find the parent of the currently
7841           ;; displayed article, then we take a look at the actual
7842           ;; References header, since this is slightly more
7843           ;; reliable than the References field we got from the
7844           ;; server.
7845           (save-excursion
7846             (set-buffer gnus-original-article-buffer)
7847             (nnheader-narrow-to-headers)
7848             (unless (setq ref (message-fetch-field "references"))
7849               (setq ref (message-fetch-field "in-reply-to")))
7850             (widen))
7851         (setq ref
7852               ;; It's not the current article, so we take a bet on
7853               ;; the value we got from the server.
7854               (mail-header-references header)))
7855       (if (and ref
7856                (not (equal ref "")))
7857           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7858             (gnus-message 1 "Couldn't find parent"))
7859         (gnus-message 1 "No references in article %d"
7860                       (gnus-summary-article-number))
7861         (setq error t))
7862       (decf n))
7863     (gnus-summary-position-point)
7864     n))
7865
7866 (defun gnus-summary-refer-references ()
7867   "Fetch all articles mentioned in the References header.
7868 Return the number of articles fetched."
7869   (interactive)
7870   (let ((ref (mail-header-references (gnus-summary-article-header)))
7871         (current (gnus-summary-article-number))
7872         (n 0))
7873     (if (or (not ref)
7874             (equal ref ""))
7875         (error "No References in the current article")
7876       ;; For each Message-ID in the References header...
7877       (while (string-match "<[^>]*>" ref)
7878         (incf n)
7879         ;; ... fetch that article.
7880         (gnus-summary-refer-article
7881          (prog1 (match-string 0 ref)
7882            (setq ref (substring ref (match-end 0))))))
7883       (gnus-summary-goto-subject current)
7884       (gnus-summary-position-point)
7885       n)))
7886
7887 (defun gnus-summary-refer-thread (&optional limit)
7888   "Fetch all articles in the current thread.
7889 If LIMIT (the numerical prefix), fetch that many old headers instead
7890 of what's specified by the `gnus-refer-thread-limit' variable."
7891   (interactive "P")
7892   (let ((id (mail-header-id (gnus-summary-article-header)))
7893         (limit (if limit (prefix-numeric-value limit)
7894                  gnus-refer-thread-limit)))
7895     ;; We want to fetch LIMIT *old* headers, but we also have to
7896     ;; re-fetch all the headers in the current buffer, because many of
7897     ;; them may be undisplayed.  So we adjust LIMIT.
7898     (when (numberp limit)
7899       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7900     (unless (eq gnus-fetch-old-headers 'invisible)
7901       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7902       ;; Retrieve the headers and read them in.
7903       (if (eq (gnus-retrieve-headers
7904                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7905               'nov)
7906           (gnus-build-all-threads)
7907         (error "Can't fetch thread from backends that don't support NOV"))
7908       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7909     (gnus-summary-limit-include-thread id)))
7910
7911 (defun gnus-summary-refer-article (message-id)
7912   "Fetch an article specified by MESSAGE-ID."
7913   (interactive "sMessage-ID: ")
7914   (when (and (stringp message-id)
7915              (not (zerop (length message-id))))
7916     ;; Construct the correct Message-ID if necessary.
7917     ;; Suggested by tale@pawl.rpi.edu.
7918     (unless (string-match "^<" message-id)
7919       (setq message-id (concat "<" message-id)))
7920     (unless (string-match ">$" message-id)
7921       (setq message-id (concat message-id ">")))
7922     (let* ((header (gnus-id-to-header message-id))
7923            (sparse (and header
7924                         (gnus-summary-article-sparse-p
7925                          (mail-header-number header))
7926                         (memq (mail-header-number header)
7927                               gnus-newsgroup-limit)))
7928            number)
7929       (cond
7930        ;; If the article is present in the buffer we just go to it.
7931        ((and header
7932              (or (not (gnus-summary-article-sparse-p
7933                        (mail-header-number header)))
7934                  sparse))
7935         (prog1
7936             (gnus-summary-goto-article
7937              (mail-header-number header) nil t)
7938           (when sparse
7939             (gnus-summary-update-article (mail-header-number header)))))
7940        (t
7941         ;; We fetch the article.
7942         (catch 'found
7943           (dolist (gnus-override-method (gnus-refer-article-methods))
7944             (gnus-check-server gnus-override-method)
7945             ;; Fetch the header, and display the article.
7946             (when (setq number (gnus-summary-insert-subject message-id))
7947               (gnus-summary-select-article nil nil nil number)
7948               (throw 'found t)))
7949           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7950
7951 (defun gnus-refer-article-methods ()
7952   "Return a list of referable methods."
7953   (cond
7954    ;; No method, so we default to current and native.
7955    ((null gnus-refer-article-method)
7956     (list gnus-current-select-method gnus-select-method))
7957    ;; Current.
7958    ((eq 'current gnus-refer-article-method)
7959     (list gnus-current-select-method))
7960    ;; List of select methods.
7961    ((not (and (symbolp (car gnus-refer-article-method))
7962               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7963     (let (out)
7964       (dolist (method gnus-refer-article-method)
7965         (push (if (eq 'current method)
7966                   gnus-current-select-method
7967                 method)
7968               out))
7969       (nreverse out)))
7970    ;; One single select method.
7971    (t
7972     (list gnus-refer-article-method))))
7973
7974 (defun gnus-summary-edit-parameters ()
7975   "Edit the group parameters of the current group."
7976   (interactive)
7977   (gnus-group-edit-group gnus-newsgroup-name 'params))
7978
7979 (defun gnus-summary-customize-parameters ()
7980   "Customize the group parameters of the current group."
7981   (interactive)
7982   (gnus-group-customize gnus-newsgroup-name))
7983
7984 (defun gnus-summary-enter-digest-group (&optional force)
7985   "Enter an nndoc group based on the current article.
7986 If FORCE, force a digest interpretation.  If not, try
7987 to guess what the document format is."
7988   (interactive "P")
7989   (let ((conf gnus-current-window-configuration))
7990     (save-excursion
7991       (gnus-summary-select-article))
7992     (setq gnus-current-window-configuration conf)
7993     (let* ((name (format "%s-%d"
7994                          (gnus-group-prefixed-name
7995                           gnus-newsgroup-name (list 'nndoc ""))
7996                          (save-excursion
7997                            (set-buffer gnus-summary-buffer)
7998                            gnus-current-article)))
7999            (ogroup gnus-newsgroup-name)
8000            (params (append (gnus-info-params (gnus-get-info ogroup))
8001                            (list (cons 'to-group ogroup))
8002                            (list (cons 'save-article-group ogroup))))
8003            (case-fold-search t)
8004            (buf (current-buffer))
8005            dig to-address)
8006       (save-excursion
8007         (set-buffer gnus-original-article-buffer)
8008         ;; Have the digest group inherit the main mail address of
8009         ;; the parent article.
8010         (when (setq to-address (or (gnus-fetch-field "reply-to")
8011                                    (gnus-fetch-field "from")))
8012           (setq params (append
8013                         (list (cons 'to-address
8014                                     (funcall gnus-decode-encoded-word-function
8015                                              to-address))))))
8016         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8017         (insert-buffer-substring gnus-original-article-buffer)
8018         ;; Remove lines that may lead nndoc to misinterpret the
8019         ;; document type.
8020         (narrow-to-region
8021          (goto-char (point-min))
8022          (or (search-forward "\n\n" nil t) (point)))
8023         (goto-char (point-min))
8024         (delete-matching-lines "^Path:\\|^From ")
8025         (widen))
8026       (unwind-protect
8027           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8028                     (gnus-newsgroup-ephemeral-ignored-charsets
8029                      gnus-newsgroup-ignored-charsets))
8030                 (gnus-group-read-ephemeral-group
8031                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8032                               (nndoc-article-type
8033                                ,(if force 'mbox 'guess)))
8034                  t nil nil nil
8035                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8036                                                         "ADAPT")))))
8037               ;; Make all postings to this group go to the parent group.
8038               (nconc (gnus-info-params (gnus-get-info name))
8039                      params)
8040             ;; Couldn't select this doc group.
8041             (switch-to-buffer buf)
8042             (gnus-set-global-variables)
8043             (gnus-configure-windows 'summary)
8044             (gnus-message 3 "Article couldn't be entered?"))
8045         (kill-buffer dig)))))
8046
8047 (defun gnus-summary-read-document (n)
8048   "Open a new group based on the current article(s).
8049 This will allow you to read digests and other similar
8050 documents as newsgroups.
8051 Obeys the standard process/prefix convention."
8052   (interactive "P")
8053   (let* ((articles (gnus-summary-work-articles n))
8054          (ogroup gnus-newsgroup-name)
8055          (params (append (gnus-info-params (gnus-get-info ogroup))
8056                          (list (cons 'to-group ogroup))))
8057          article group egroup groups vgroup)
8058     (while (setq article (pop articles))
8059       (setq group (format "%s-%d" gnus-newsgroup-name article))
8060       (gnus-summary-remove-process-mark article)
8061       (when (gnus-summary-display-article article)
8062         (save-excursion
8063           (with-temp-buffer
8064             (insert-buffer-substring gnus-original-article-buffer)
8065             ;; Remove some headers that may lead nndoc to make
8066             ;; the wrong guess.
8067             (message-narrow-to-head)
8068             (goto-char (point-min))
8069             (delete-matching-lines "^\\(Path\\):\\|^From ")
8070             (widen)
8071             (if (setq egroup
8072                       (gnus-group-read-ephemeral-group
8073                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8074                                      (nndoc-article-type guess))
8075                        t nil t))
8076                 (progn
8077             ;; Make all postings to this group go to the parent group.
8078                   (nconc (gnus-info-params (gnus-get-info egroup))
8079                          params)
8080                   (push egroup groups))
8081               ;; Couldn't select this doc group.
8082               (gnus-error 3 "Article couldn't be entered"))))))
8083     ;; Now we have selected all the documents.
8084     (cond
8085      ((not groups)
8086       (error "None of the articles could be interpreted as documents"))
8087      ((gnus-group-read-ephemeral-group
8088        (setq vgroup (format
8089                      "nnvirtual:%s-%s" gnus-newsgroup-name
8090                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8091        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8092        t
8093        (cons (current-buffer) 'summary)))
8094      (t
8095       (error "Couldn't select virtual nndoc group")))))
8096
8097 (defun gnus-summary-isearch-article (&optional regexp-p)
8098   "Do incremental search forward on the current article.
8099 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8100   (interactive "P")
8101   (gnus-summary-select-article)
8102   (gnus-configure-windows 'article)
8103   (gnus-eval-in-buffer-window gnus-article-buffer
8104     (save-restriction
8105       (widen)
8106       (isearch-forward regexp-p))))
8107
8108 (defun gnus-summary-search-article-forward (regexp &optional backward)
8109   "Search for an article containing REGEXP forward.
8110 If BACKWARD, search backward instead."
8111   (interactive
8112    (list (read-string
8113           (format "Search article %s (regexp%s): "
8114                   (if current-prefix-arg "backward" "forward")
8115                   (if gnus-last-search-regexp
8116                       (concat ", default " gnus-last-search-regexp)
8117                     "")))
8118          current-prefix-arg))
8119   (if (string-equal regexp "")
8120       (setq regexp (or gnus-last-search-regexp ""))
8121     (setq gnus-last-search-regexp regexp)
8122     (setq gnus-article-before-search gnus-current-article))
8123   ;; Intentionally set gnus-last-article.
8124   (setq gnus-last-article gnus-article-before-search)
8125   (let ((gnus-last-article gnus-last-article))
8126     (if (gnus-summary-search-article regexp backward)
8127         (gnus-summary-show-thread)
8128       (error "Search failed: \"%s\"" regexp))))
8129
8130 (defun gnus-summary-search-article-backward (regexp)
8131   "Search for an article containing REGEXP backward."
8132   (interactive
8133    (list (read-string
8134           (format "Search article backward (regexp%s): "
8135                   (if gnus-last-search-regexp
8136                       (concat ", default " gnus-last-search-regexp)
8137                     "")))))
8138   (gnus-summary-search-article-forward regexp 'backward))
8139
8140 (defun gnus-summary-search-article (regexp &optional backward)
8141   "Search for an article containing REGEXP.
8142 Optional argument BACKWARD means do search for backward.
8143 `gnus-select-article-hook' is not called during the search."
8144   ;; We have to require this here to make sure that the following
8145   ;; dynamic binding isn't shadowed by autoloading.
8146   (require 'gnus-async)
8147   (require 'gnus-art)
8148   (let ((gnus-select-article-hook nil)  ;Disable hook.
8149         (gnus-article-prepare-hook nil)
8150         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8151         (gnus-use-article-prefetch nil)
8152         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8153         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8154         (sum (current-buffer))
8155         (gnus-display-mime-function nil)
8156         (found nil)
8157         point)
8158     (gnus-save-hidden-threads
8159       (gnus-summary-select-article)
8160       (set-buffer gnus-article-buffer)
8161       (goto-char (window-point (get-buffer-window (current-buffer))))
8162       (when backward
8163         (forward-line -1))
8164       (while (not found)
8165         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8166         (if (if backward
8167                 (re-search-backward regexp nil t)
8168               (re-search-forward regexp nil t))
8169             ;; We found the regexp.
8170             (progn
8171               (setq found 'found)
8172               (beginning-of-line)
8173               (set-window-start
8174                (get-buffer-window (current-buffer))
8175                (point))
8176               (forward-line 1)
8177               (set-window-point
8178                (get-buffer-window (current-buffer))
8179                (point))
8180               (set-buffer sum)
8181               (setq point (point)))
8182           ;; We didn't find it, so we go to the next article.
8183           (set-buffer sum)
8184           (setq found 'not)
8185           (while (eq found 'not)
8186             (if (not (if backward (gnus-summary-find-prev)
8187                        (gnus-summary-find-next)))
8188                 ;; No more articles.
8189                 (setq found t)
8190               ;; Select the next article and adjust point.
8191               (unless (gnus-summary-article-sparse-p
8192                        (gnus-summary-article-number))
8193                 (setq found nil)
8194                 (gnus-summary-select-article)
8195                 (set-buffer gnus-article-buffer)
8196                 (widen)
8197                 (goto-char (if backward (point-max) (point-min))))))))
8198       (gnus-message 7 ""))
8199     ;; Return whether we found the regexp.
8200     (when (eq found 'found)
8201       (goto-char point)
8202       (gnus-summary-show-thread)
8203       (gnus-summary-goto-subject gnus-current-article)
8204       (gnus-summary-position-point)
8205       t)))
8206
8207 (defun gnus-find-matching-articles (header regexp)
8208   "Return a list of all articles that match REGEXP on HEADER.
8209 This search includes all articles in the current group that Gnus has
8210 fetched headers for, whether they are displayed or not."
8211   (let ((articles nil)
8212         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8213         (case-fold-search t))
8214     (dolist (header gnus-newsgroup-headers)
8215       (when (string-match regexp (funcall func header))
8216         (push (mail-header-number header) articles)))
8217     (nreverse articles)))
8218
8219 (defun gnus-summary-find-matching (header regexp &optional backward unread
8220                                           not-case-fold not-matching)
8221   "Return a list of all articles that match REGEXP on HEADER.
8222 The search stars on the current article and goes forwards unless
8223 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8224 If UNREAD is non-nil, only unread articles will
8225 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8226 in the comparisons. If NOT-MATCHING, return a list of all articles that
8227 not match REGEXP on HEADER."
8228   (let ((case-fold-search (not not-case-fold))
8229         articles d func)
8230     (if (consp header)
8231         (if (eq (car header) 'extra)
8232             (setq func
8233                   `(lambda (h)
8234                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8235                          "")))
8236           (error "%s is an invalid header" header))
8237       (unless (fboundp (intern (concat "mail-header-" header)))
8238         (error "%s is not a valid header" header))
8239       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8240     (dolist (d (if (eq backward 'all)
8241                    gnus-newsgroup-data
8242                  (gnus-data-find-list
8243                   (gnus-summary-article-number)
8244                   (gnus-data-list backward))))
8245       (when (and (or (not unread)       ; We want all articles...
8246                      (gnus-data-unread-p d)) ; Or just unreads.
8247                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8248                  (if not-matching
8249                      (not (string-match
8250                            regexp
8251                            (funcall func (gnus-data-header d))))
8252                    (string-match regexp
8253                                  (funcall func (gnus-data-header d)))))
8254         (push (gnus-data-number d) articles))) ; Success!
8255     (nreverse articles)))
8256
8257 (defun gnus-summary-execute-command (header regexp command &optional backward)
8258   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8259 If HEADER is an empty string (or nil), the match is done on the entire
8260 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8261   (interactive
8262    (list (let ((completion-ignore-case t))
8263            (completing-read
8264             "Header name: "
8265             (mapcar (lambda (header) (list (format "%s" header)))
8266                     (append
8267                      '("Number" "Subject" "From" "Lines" "Date"
8268                        "Message-ID" "Xref" "References" "Body")
8269                      gnus-extra-headers))
8270             nil 'require-match))
8271          (read-string "Regexp: ")
8272          (read-key-sequence "Command: ")
8273          current-prefix-arg))
8274   (when (equal header "Body")
8275     (setq header ""))
8276   ;; Hidden thread subtrees must be searched as well.
8277   (gnus-summary-show-all-threads)
8278   ;; We don't want to change current point nor window configuration.
8279   (save-excursion
8280     (save-window-excursion
8281       (let (gnus-visual
8282             gnus-treat-strip-trailing-blank-lines
8283             gnus-treat-strip-leading-blank-lines
8284             gnus-treat-strip-multiple-blank-lines
8285             gnus-treat-hide-boring-headers
8286             gnus-treat-fold-newsgroups
8287             gnus-article-prepare-hook)
8288         (gnus-message 6 "Executing %s..." (key-description command))
8289         ;; We'd like to execute COMMAND interactively so as to give arguments.
8290         (gnus-execute header regexp
8291                       `(call-interactively ',(key-binding command))
8292                       backward)
8293         (gnus-message 6 "Executing %s...done" (key-description command))))))
8294
8295 (defun gnus-summary-beginning-of-article ()
8296   "Scroll the article back to the beginning."
8297   (interactive)
8298   (gnus-summary-select-article)
8299   (gnus-configure-windows 'article)
8300   (gnus-eval-in-buffer-window gnus-article-buffer
8301     (widen)
8302     (goto-char (point-min))
8303     (when gnus-page-broken
8304       (gnus-narrow-to-page))))
8305
8306 (defun gnus-summary-end-of-article ()
8307   "Scroll to the end of the article."
8308   (interactive)
8309   (gnus-summary-select-article)
8310   (gnus-configure-windows 'article)
8311   (gnus-eval-in-buffer-window gnus-article-buffer
8312     (widen)
8313     (goto-char (point-max))
8314     (recenter -3)
8315     (when gnus-page-broken
8316       (gnus-narrow-to-page))))
8317
8318 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8319   "Truncate to LEN and quote all \"(\"'s in STRING."
8320   (gnus-replace-in-string (if (and len (> (length string) len))
8321                               (substring string 0 len)
8322                             string)
8323                           "[()]" "\\\\\\&"))
8324
8325 (defun gnus-summary-print-article (&optional filename n)
8326   "Generate and print a PostScript image of the N next (mail) articles.
8327
8328 If N is negative, print the N previous articles.  If N is nil and articles
8329 have been marked with the process mark, print these instead.
8330
8331 If the optional first argument FILENAME is nil, send the image to the
8332 printer.  If FILENAME is a string, save the PostScript image in a file with
8333 that name.  If FILENAME is a number, prompt the user for the name of the file
8334 to save in."
8335   (interactive (list (ps-print-preprint current-prefix-arg)))
8336   (dolist (article (gnus-summary-work-articles n))
8337     (gnus-summary-select-article nil nil 'pseudo article)
8338     (gnus-eval-in-buffer-window gnus-article-buffer
8339       (gnus-print-buffer))
8340     (gnus-summary-remove-process-mark article))
8341   (ps-despool filename))
8342
8343 (defun gnus-print-buffer ()
8344   (let ((buffer (generate-new-buffer " *print*")))
8345     (unwind-protect
8346         (progn
8347           (copy-to-buffer buffer (point-min) (point-max))
8348           (set-buffer buffer)
8349           (gnus-article-delete-invisible-text)
8350           (gnus-remove-text-with-property 'gnus-decoration)
8351           (when (gnus-visual-p 'article-highlight 'highlight)
8352             ;; Copy-to-buffer doesn't copy overlay.  So redo
8353             ;; highlight.
8354             (let ((gnus-article-buffer buffer))
8355               (gnus-article-highlight-citation t)
8356               (gnus-article-highlight-signature)))
8357           (let ((ps-left-header
8358                  (list
8359                   (concat "("
8360                           (gnus-summary-print-truncate-and-quote
8361                            (mail-header-subject gnus-current-headers)
8362                            66) ")")
8363                   (concat "("
8364                           (gnus-summary-print-truncate-and-quote
8365                            (mail-header-from gnus-current-headers)
8366                            45) ")")))
8367                 (ps-right-header
8368                  (list
8369                   "/pagenumberstring load"
8370                   (concat "("
8371                           (mail-header-date gnus-current-headers) ")"))))
8372             (gnus-run-hooks 'gnus-ps-print-hook)
8373             (save-excursion
8374               (if window-system
8375                   (ps-spool-buffer-with-faces)
8376                 (ps-spool-buffer)))))
8377       (kill-buffer buffer))))
8378
8379 (defun gnus-summary-show-article (&optional arg)
8380   "Force redisplaying of the current article.
8381 If ARG (the prefix) is a number, show the article with the charset
8382 defined in `gnus-summary-show-article-charset-alist', or the charset
8383 input.
8384 If ARG (the prefix) is non-nil and not a number, show the raw article
8385 without any article massaging functions being run.  Normally, the key strokes
8386 are `C-u g'."
8387   (interactive "P")
8388   (cond
8389    ((numberp arg)
8390     (gnus-summary-show-article t)
8391     (let ((gnus-newsgroup-charset
8392            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8393                (mm-read-coding-system
8394                 "View as charset: " ;; actually it is coding system.
8395                 (save-excursion
8396                   (set-buffer gnus-article-buffer)
8397                   (mm-detect-coding-region (point) (point-max))))))
8398           (gnus-newsgroup-ignored-charsets 'gnus-all))
8399       (gnus-summary-select-article nil 'force)
8400       (let ((deps gnus-newsgroup-dependencies)
8401             head header lines)
8402         (save-excursion
8403           (set-buffer gnus-original-article-buffer)
8404           (save-restriction
8405             (message-narrow-to-head)
8406             (setq head (buffer-string))
8407             (goto-char (point-min))
8408             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8409               (goto-char (point-max))
8410               (widen)
8411               (setq lines (1- (count-lines (point) (point-max))))))
8412           (with-temp-buffer
8413             (insert (format "211 %d Article retrieved.\n"
8414                             (cdr gnus-article-current)))
8415             (insert head)
8416             (if lines (insert (format "Lines: %d\n" lines)))
8417             (insert ".\n")
8418             (let ((nntp-server-buffer (current-buffer)))
8419               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8420         (gnus-data-set-header
8421          (gnus-data-find (cdr gnus-article-current))
8422          header)
8423         (gnus-summary-update-article-line
8424          (cdr gnus-article-current) header)
8425         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8426           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8427    ((not arg)
8428     ;; Select the article the normal way.
8429     (gnus-summary-select-article nil 'force))
8430    (t
8431     ;; We have to require this here to make sure that the following
8432     ;; dynamic binding isn't shadowed by autoloading.
8433     (require 'gnus-async)
8434     (require 'gnus-art)
8435     ;; Bind the article treatment functions to nil.
8436     (let ((gnus-have-all-headers t)
8437           gnus-article-prepare-hook
8438           gnus-article-decode-hook
8439           gnus-display-mime-function
8440           gnus-break-pages)
8441       ;; Destroy any MIME parts.
8442       (when (gnus-buffer-live-p gnus-article-buffer)
8443         (save-excursion
8444           (set-buffer gnus-article-buffer)
8445           (mm-destroy-parts gnus-article-mime-handles)
8446           ;; Set it to nil for safety reason.
8447           (setq gnus-article-mime-handle-alist nil)
8448           (setq gnus-article-mime-handles nil)))
8449       (gnus-summary-select-article nil 'force))))
8450   (gnus-summary-goto-subject gnus-current-article)
8451   (gnus-summary-position-point))
8452
8453 (defun gnus-summary-show-raw-article ()
8454   "Show the raw article without any article massaging functions being run."
8455   (interactive)
8456   (gnus-summary-show-article t))
8457
8458 (defun gnus-summary-verbose-headers (&optional arg)
8459   "Toggle permanent full header display.
8460 If ARG is a positive number, turn header display on.
8461 If ARG is a negative number, turn header display off."
8462   (interactive "P")
8463   (setq gnus-show-all-headers
8464         (cond ((or (not (numberp arg))
8465                    (zerop arg))
8466                (not gnus-show-all-headers))
8467               ((natnump arg)
8468                t)))
8469   (gnus-summary-show-article))
8470
8471 (defun gnus-summary-toggle-header (&optional arg)
8472   "Show the headers if they are hidden, or hide them if they are shown.
8473 If ARG is a positive number, show the entire header.
8474 If ARG is a negative number, hide the unwanted header lines."
8475   (interactive "P")
8476   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8477                      (get-buffer-window gnus-article-buffer t))))
8478     (with-current-buffer gnus-article-buffer
8479       (widen)
8480       (article-narrow-to-head)
8481       (let* ((buffer-read-only nil)
8482              (inhibit-point-motion-hooks t)
8483              (hidden (if (numberp arg)
8484                          (>= arg 0)
8485                        (gnus-article-hidden-text-p 'headers)))
8486              s e)
8487         (delete-region (point-min) (point-max))
8488         (with-current-buffer gnus-original-article-buffer
8489           (goto-char (setq s (point-min)))
8490           (setq e (if (search-forward "\n\n" nil t)
8491                       (1- (point))
8492                     (point-max))))
8493         (insert-buffer-substring gnus-original-article-buffer s e)
8494         (article-decode-encoded-words)
8495         (if hidden
8496             (let ((gnus-treat-hide-headers nil)
8497                   (gnus-treat-hide-boring-headers nil))
8498               (gnus-delete-wash-type 'headers)
8499               (gnus-treat-article 'head))
8500           (gnus-treat-article 'head))
8501         (widen)
8502         (if window
8503             (set-window-start window (goto-char (point-min))))
8504         (setq gnus-page-broken
8505               (when gnus-break-pages
8506                 (gnus-narrow-to-page)
8507                 t))
8508         (gnus-set-mode-line 'article)))))
8509
8510 (defun gnus-summary-show-all-headers ()
8511   "Make all header lines visible."
8512   (interactive)
8513   (gnus-summary-toggle-header 1))
8514
8515 (defun gnus-summary-caesar-message (&optional arg)
8516   "Caesar rotate the current article by 13.
8517 The numerical prefix specifies how many places to rotate each letter
8518 forward."
8519   (interactive "P")
8520   (gnus-summary-select-article)
8521   (let ((mail-header-separator ""))
8522     (gnus-eval-in-buffer-window gnus-article-buffer
8523       (save-restriction
8524         (widen)
8525         (let ((start (window-start))
8526               buffer-read-only)
8527           (message-caesar-buffer-body arg)
8528           (set-window-start (get-buffer-window (current-buffer)) start))))))
8529
8530 (autoload 'unmorse-region "morse"
8531   "Convert morse coded text in region to ordinary ASCII text."
8532   t)
8533
8534 (defun gnus-summary-morse-message (&optional arg)
8535   "Morse decode the current article."
8536   (interactive "P")
8537   (gnus-summary-select-article)
8538   (let ((mail-header-separator ""))
8539     (gnus-eval-in-buffer-window gnus-article-buffer
8540       (save-excursion
8541         (save-restriction
8542           (widen)
8543           (let ((pos (window-start))
8544                 buffer-read-only)
8545             (goto-char (point-min))
8546             (when (message-goto-body)
8547               (gnus-narrow-to-body))
8548             (goto-char (point-min))
8549             (while (re-search-forward "·" (point-max) t)
8550               (replace-match "."))
8551             (unmorse-region (point-min) (point-max))
8552             (widen)
8553             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8554
8555 (defun gnus-summary-stop-page-breaking ()
8556   "Stop page breaking in the current article."
8557   (interactive)
8558   (gnus-summary-select-article)
8559   (gnus-eval-in-buffer-window gnus-article-buffer
8560     (widen)
8561     (when (gnus-visual-p 'page-marker)
8562       (let ((buffer-read-only nil))
8563         (gnus-remove-text-with-property 'gnus-prev)
8564         (gnus-remove-text-with-property 'gnus-next))
8565       (setq gnus-page-broken nil))))
8566
8567 (defun gnus-summary-move-article (&optional n to-newsgroup
8568                                             select-method action)
8569   "Move the current article to a different newsgroup.
8570 If N is a positive number, move the N next articles.
8571 If N is a negative number, move the N previous articles.
8572 If N is nil and any articles have been marked with the process mark,
8573 move those articles instead.
8574 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8575 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8576 re-spool using this method.
8577
8578 When called interactively with TO-NEWSGROUP being nil, the value of
8579 the variable `gnus-move-split-methods' is used for finding a default
8580 for the target newsgroup.
8581
8582 For this function to work, both the current newsgroup and the
8583 newsgroup that you want to move to have to support the `request-move'
8584 and `request-accept' functions.
8585
8586 ACTION can be either `move' (the default), `crosspost' or `copy'."
8587   (interactive "P")
8588   (unless action
8589     (setq action 'move))
8590   ;; Check whether the source group supports the required functions.
8591   (cond ((and (eq action 'move)
8592               (not (gnus-check-backend-function
8593                     'request-move-article gnus-newsgroup-name)))
8594          (error "The current group does not support article moving"))
8595         ((and (eq action 'crosspost)
8596               (not (gnus-check-backend-function
8597                     'request-replace-article gnus-newsgroup-name)))
8598          (error "The current group does not support article editing")))
8599   (let ((articles (gnus-summary-work-articles n))
8600         (prefix (if (gnus-check-backend-function
8601                      'request-move-article gnus-newsgroup-name)
8602                     (gnus-group-real-prefix gnus-newsgroup-name)
8603                   ""))
8604         (names '((move "Move" "Moving")
8605                  (copy "Copy" "Copying")
8606                  (crosspost "Crosspost" "Crossposting")))
8607         (copy-buf (save-excursion
8608                     (nnheader-set-temp-buffer " *copy article*")))
8609         art-group to-method new-xref article to-groups)
8610     (unless (assq action names)
8611       (error "Unknown action %s" action))
8612     ;; Read the newsgroup name.
8613     (when (and (not to-newsgroup)
8614                (not select-method))
8615       (if (and gnus-move-split-methods
8616                (not
8617                 (and (memq gnus-current-article articles)
8618                      (gnus-buffer-live-p gnus-original-article-buffer))))
8619           ;; When `gnus-move-split-methods' is non-nil, we have to
8620           ;; select an article to give `gnus-read-move-group-name' an
8621           ;; opportunity to suggest an appropriate default.  However,
8622           ;; we needn't render or mark the article.
8623           (let ((gnus-display-mime-function nil)
8624                 (gnus-article-prepare-hook nil)
8625                 (gnus-mark-article-hook nil))
8626             (gnus-summary-select-article nil nil nil (car articles))))
8627       (setq to-newsgroup
8628             (gnus-read-move-group-name
8629              (cadr (assq action names))
8630              (symbol-value (intern (format "gnus-current-%s-group" action)))
8631              articles prefix))
8632       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8633     (setq to-method (or select-method
8634                         (gnus-server-to-method
8635                          (gnus-group-method to-newsgroup))))
8636     ;; Check the method we are to move this article to...
8637     (unless (gnus-check-backend-function
8638              'request-accept-article (car to-method))
8639       (error "%s does not support article copying" (car to-method)))
8640     (unless (gnus-check-server to-method)
8641       (error "Can't open server %s" (car to-method)))
8642     (gnus-message 6 "%s to %s: %s..."
8643                   (caddr (assq action names))
8644                   (or (car select-method) to-newsgroup) articles)
8645     (while articles
8646       (setq article (pop articles))
8647       (setq
8648        art-group
8649        (cond
8650         ;; Move the article.
8651         ((eq action 'move)
8652          ;; Remove this article from future suppression.
8653          (gnus-dup-unsuppress-article article)
8654          (gnus-request-move-article
8655           article                       ; Article to move
8656           gnus-newsgroup-name           ; From newsgroup
8657           (nth 1 (gnus-find-method-for-group
8658                   gnus-newsgroup-name)) ; Server
8659           (list 'gnus-request-accept-article
8660                 to-newsgroup (list 'quote select-method)
8661                 (not articles) t)       ; Accept form
8662           (not articles)))              ; Only save nov last time
8663         ;; Copy the article.
8664         ((eq action 'copy)
8665          (save-excursion
8666            (set-buffer copy-buf)
8667            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8668              (gnus-request-accept-article
8669               to-newsgroup select-method (not articles) t))))
8670         ;; Crosspost the article.
8671         ((eq action 'crosspost)
8672          (let ((xref (message-tokenize-header
8673                       (mail-header-xref (gnus-summary-article-header article))
8674                       " ")))
8675            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8676                                   ":" (number-to-string article)))
8677            (unless xref
8678              (setq xref (list (system-name))))
8679            (setq new-xref
8680                  (concat
8681                   (mapconcat 'identity
8682                              (delete "Xref:" (delete new-xref xref))
8683                              " ")
8684                   " " new-xref))
8685            (save-excursion
8686              (set-buffer copy-buf)
8687              ;; First put the article in the destination group.
8688              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8689              (when (consp (setq art-group
8690                                 (gnus-request-accept-article
8691                                  to-newsgroup select-method (not articles))))
8692                (setq new-xref (concat new-xref " " (car art-group)
8693                                       ":"
8694                                       (number-to-string (cdr art-group))))
8695                ;; Now we have the new Xrefs header, so we insert
8696                ;; it and replace the new article.
8697                (nnheader-replace-header "Xref" new-xref)
8698                (gnus-request-replace-article
8699                 (cdr art-group) to-newsgroup (current-buffer))
8700                art-group))))))
8701       (cond
8702        ((not art-group)
8703         (gnus-message 1 "Couldn't %s article %s: %s"
8704                       (cadr (assq action names)) article
8705                       (nnheader-get-report (car to-method))))
8706        ((eq art-group 'junk)
8707         (when (eq action 'move)
8708           (gnus-summary-mark-article article gnus-canceled-mark)
8709           (gnus-message 4 "Deleted article %s" article)))
8710        (t
8711         (let* ((pto-group (gnus-group-prefixed-name
8712                            (car art-group) to-method))
8713                (entry
8714                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8715                (info (nth 2 entry))
8716                (to-group (gnus-info-group info))
8717                to-marks)
8718           ;; Update the group that has been moved to.
8719           (when (and info
8720                      (memq action '(move copy)))
8721             (unless (member to-group to-groups)
8722               (push to-group to-groups))
8723
8724             (unless (memq article gnus-newsgroup-unreads)
8725               (push 'read to-marks)
8726               (gnus-info-set-read
8727                info (gnus-add-to-range (gnus-info-read info)
8728                                        (list (cdr art-group)))))
8729
8730             ;; See whether the article is to be put in the cache.
8731             (let ((marks gnus-article-mark-lists)
8732                   (to-article (cdr art-group)))
8733
8734               ;; Enter the article into the cache in the new group,
8735               ;; if that is required.
8736               (when gnus-use-cache
8737                 (gnus-cache-possibly-enter-article
8738                  to-group to-article
8739                  (memq article gnus-newsgroup-marked)
8740                  (memq article gnus-newsgroup-dormant)
8741                  (memq article gnus-newsgroup-unreads)))
8742
8743               (when gnus-preserve-marks
8744                 ;; Copy any marks over to the new group.
8745                 (when (and (equal to-group gnus-newsgroup-name)
8746                            (not (memq article gnus-newsgroup-unreads)))
8747                   ;; Mark this article as read in this group.
8748                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8749                   (setcdr (gnus-active to-group) to-article)
8750                   (setcdr gnus-newsgroup-active to-article))
8751
8752                 (while marks
8753                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8754                     (when (memq article (symbol-value
8755                                          (intern (format "gnus-newsgroup-%s"
8756                                                          (caar marks)))))
8757                       (push (cdar marks) to-marks)
8758                       ;; If the other group is the same as this group,
8759                       ;; then we have to add the mark to the list.
8760                       (when (equal to-group gnus-newsgroup-name)
8761                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8762                              (cons to-article
8763                                    (symbol-value
8764                                     (intern (format "gnus-newsgroup-%s"
8765                                                     (caar marks)))))))
8766                       ;; Copy the marks to other group.
8767                       (gnus-add-marked-articles
8768                        to-group (cdar marks) (list to-article) info)))
8769                   (setq marks (cdr marks)))
8770
8771                 (gnus-request-set-mark
8772                  to-group (list (list (list to-article) 'add to-marks))))
8773
8774               (gnus-dribble-enter
8775                (concat "(gnus-group-set-info '"
8776                        (gnus-prin1-to-string (gnus-get-info to-group))
8777                        ")"))))
8778
8779           ;; Update the Xref header in this article to point to
8780           ;; the new crossposted article we have just created.
8781           (when (eq action 'crosspost)
8782             (save-excursion
8783               (set-buffer copy-buf)
8784               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8785               (nnheader-replace-header "Xref" new-xref)
8786               (gnus-request-replace-article
8787                article gnus-newsgroup-name (current-buffer)))))
8788
8789         ;;;!!!Why is this necessary?
8790         (set-buffer gnus-summary-buffer)
8791
8792         (gnus-summary-goto-subject article)
8793         (when (eq action 'move)
8794           (gnus-summary-mark-article article gnus-canceled-mark))))
8795       (gnus-summary-remove-process-mark article))
8796     ;; Re-activate all groups that have been moved to.
8797     (save-excursion
8798       (set-buffer gnus-group-buffer)
8799       (let ((gnus-group-marked to-groups))
8800         (gnus-group-get-new-news-this-group nil t)))
8801
8802     (gnus-kill-buffer copy-buf)
8803     (gnus-summary-position-point)
8804     (gnus-set-mode-line 'summary)))
8805
8806 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8807   "Move the current article to a different newsgroup.
8808 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8809 When called interactively, if TO-NEWSGROUP is nil, use the value of
8810 the variable `gnus-move-split-methods' for finding a default target
8811 newsgroup.
8812 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8813 re-spool using this method."
8814   (interactive "P")
8815   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8816
8817 (defun gnus-summary-crosspost-article (&optional n)
8818   "Crosspost the current article to some other group."
8819   (interactive "P")
8820   (gnus-summary-move-article n nil nil 'crosspost))
8821
8822 (defcustom gnus-summary-respool-default-method nil
8823   "Default method type for respooling an article.
8824 If nil, use to the current newsgroup method."
8825   :type 'symbol
8826   :group 'gnus-summary-mail)
8827
8828 (defcustom gnus-summary-display-while-building nil
8829   "If not-nil, show and update the summary buffer as it's being built.
8830 If the value is t, update the buffer after every line is inserted.  If
8831 the value is an integer (N), update the display every N lines."
8832   :group 'gnus-thread
8833   :type '(choice (const :tag "off" nil)
8834                  number
8835                  (const :tag "frequently" t)))
8836
8837 (defun gnus-summary-respool-article (&optional n method)
8838   "Respool the current article.
8839 The article will be squeezed through the mail spooling process again,
8840 which means that it will be put in some mail newsgroup or other
8841 depending on `nnmail-split-methods'.
8842 If N is a positive number, respool the N next articles.
8843 If N is a negative number, respool the N previous articles.
8844 If N is nil and any articles have been marked with the process mark,
8845 respool those articles instead.
8846
8847 Respooling can be done both from mail groups and \"real\" newsgroups.
8848 In the former case, the articles in question will be moved from the
8849 current group into whatever groups they are destined to.  In the
8850 latter case, they will be copied into the relevant groups."
8851   (interactive
8852    (list current-prefix-arg
8853          (let* ((methods (gnus-methods-using 'respool))
8854                 (methname
8855                  (symbol-name (or gnus-summary-respool-default-method
8856                                   (car (gnus-find-method-for-group
8857                                         gnus-newsgroup-name)))))
8858                 (method
8859                  (gnus-completing-read-with-default
8860                   methname "What backend do you want to use when respooling?"
8861                   methods nil t nil 'gnus-mail-method-history))
8862                 ms)
8863            (cond
8864             ((zerop (length (setq ms (gnus-servers-using-backend
8865                                       (intern method)))))
8866              (list (intern method) ""))
8867             ((= 1 (length ms))
8868              (car ms))
8869             (t
8870              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8871                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8872                            ms-alist))))))))
8873   (unless method
8874     (error "No method given for respooling"))
8875   (if (assoc (symbol-name
8876               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8877              (gnus-methods-using 'respool))
8878       (gnus-summary-move-article n nil method)
8879     (gnus-summary-copy-article n nil method)))
8880
8881 (defun gnus-summary-import-article (file &optional edit)
8882   "Import an arbitrary file into a mail newsgroup."
8883   (interactive "fImport file: \nP")
8884   (let ((group gnus-newsgroup-name)
8885         (now (current-time))
8886         atts lines group-art)
8887     (unless (gnus-check-backend-function 'request-accept-article group)
8888       (error "%s does not support article importing" group))
8889     (or (file-readable-p file)
8890         (not (file-regular-p file))
8891         (error "Can't read %s" file))
8892     (save-excursion
8893       (set-buffer (gnus-get-buffer-create " *import file*"))
8894       (erase-buffer)
8895       (nnheader-insert-file-contents file)
8896       (goto-char (point-min))
8897       (if (nnheader-article-p)
8898           (save-restriction
8899             (goto-char (point-min))
8900             (search-forward "\n\n" nil t)
8901             (narrow-to-region (point-min) (1- (point)))
8902             (goto-char (point-min))
8903             (unless (re-search-forward "^date:" nil t)
8904               (goto-char (point-max))
8905               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8906        ;; This doesn't look like an article, so we fudge some headers.
8907         (setq atts (file-attributes file)
8908               lines (count-lines (point-min) (point-max)))
8909         (insert "From: " (read-string "From: ") "\n"
8910                 "Subject: " (read-string "Subject: ") "\n"
8911                 "Date: " (message-make-date (nth 5 atts)) "\n"
8912                 "Message-ID: " (message-make-message-id) "\n"
8913                 "Lines: " (int-to-string lines) "\n"
8914                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8915       (setq group-art (gnus-request-accept-article group nil t))
8916       (kill-buffer (current-buffer)))
8917     (setq gnus-newsgroup-active (gnus-activate-group group))
8918     (forward-line 1)
8919     (gnus-summary-goto-article (cdr group-art) nil t)
8920     (when edit
8921       (gnus-summary-edit-article))))
8922
8923 (defun gnus-summary-create-article ()
8924   "Create an article in a mail newsgroup."
8925   (interactive)
8926   (let ((group gnus-newsgroup-name)
8927         (now (current-time))
8928         group-art)
8929     (unless (gnus-check-backend-function 'request-accept-article group)
8930       (error "%s does not support article importing" group))
8931     (save-excursion
8932       (set-buffer (gnus-get-buffer-create " *import file*"))
8933       (erase-buffer)
8934       (goto-char (point-min))
8935       ;; This doesn't look like an article, so we fudge some headers.
8936       (insert "From: " (read-string "From: ") "\n"
8937               "Subject: " (read-string "Subject: ") "\n"
8938               "Date: " (message-make-date now) "\n"
8939               "Message-ID: " (message-make-message-id) "\n")
8940       (setq group-art (gnus-request-accept-article group nil t))
8941       (kill-buffer (current-buffer)))
8942     (setq gnus-newsgroup-active (gnus-activate-group group))
8943     (forward-line 1)
8944     (gnus-summary-goto-article (cdr group-art) nil t)
8945     (gnus-summary-edit-article)))
8946
8947 (defun gnus-summary-article-posted-p ()
8948   "Say whether the current (mail) article is available from news as well.
8949 This will be the case if the article has both been mailed and posted."
8950   (interactive)
8951   (let ((id (mail-header-references (gnus-summary-article-header)))
8952         (gnus-override-method (car (gnus-refer-article-methods))))
8953     (if (gnus-request-head id "")
8954         (gnus-message 2 "The current message was found on %s"
8955                       gnus-override-method)
8956       (gnus-message 2 "The current message couldn't be found on %s"
8957                     gnus-override-method)
8958       nil)))
8959
8960 (defun gnus-summary-expire-articles (&optional now)
8961   "Expire all articles that are marked as expirable in the current group."
8962   (interactive)
8963   (when (gnus-check-backend-function
8964          'request-expire-articles gnus-newsgroup-name)
8965     ;; This backend supports expiry.
8966     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8967            (expirable (if total
8968                           (progn
8969                             ;; We need to update the info for
8970                             ;; this group for `gnus-list-of-read-articles'
8971                             ;; to give us the right answer.
8972                             (gnus-run-hooks 'gnus-exit-group-hook)
8973                             (gnus-summary-update-info)
8974                             (gnus-list-of-read-articles gnus-newsgroup-name))
8975                         (setq gnus-newsgroup-expirable
8976                               (sort gnus-newsgroup-expirable '<))))
8977            (expiry-wait (if now 'immediate
8978                           (gnus-group-find-parameter
8979                            gnus-newsgroup-name 'expiry-wait)))
8980            (nnmail-expiry-target
8981             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8982                 nnmail-expiry-target))
8983            es)
8984       (when expirable
8985         ;; There are expirable articles in this group, so we run them
8986         ;; through the expiry process.
8987         (gnus-message 6 "Expiring articles...")
8988         (unless (gnus-check-group gnus-newsgroup-name)
8989           (error "Can't open server for %s" gnus-newsgroup-name))
8990         ;; The list of articles that weren't expired is returned.
8991         (save-excursion
8992           (if expiry-wait
8993               (let ((nnmail-expiry-wait-function nil)
8994                     (nnmail-expiry-wait expiry-wait))
8995                 (setq es (gnus-request-expire-articles
8996                           expirable gnus-newsgroup-name)))
8997             (setq es (gnus-request-expire-articles
8998                       expirable gnus-newsgroup-name)))
8999           (unless total
9000             (setq gnus-newsgroup-expirable es))
9001           ;; We go through the old list of expirable, and mark all
9002           ;; really expired articles as nonexistent.
9003           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9004             (let ((gnus-use-cache nil))
9005               (dolist (article expirable)
9006                 (when (and (not (memq article es))
9007                            (gnus-data-find article))
9008                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9009         (gnus-message 6 "Expiring articles...done")))))
9010
9011 (defun gnus-summary-expire-articles-now ()
9012   "Expunge all expirable articles in the current group.
9013 This means that *all* articles that are marked as expirable will be
9014 deleted forever, right now."
9015   (interactive)
9016   (or gnus-expert-user
9017       (gnus-yes-or-no-p
9018        "Are you really, really, really sure you want to delete all these messages? ")
9019       (error "Phew!"))
9020   (gnus-summary-expire-articles t))
9021
9022 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9023 (defun gnus-summary-delete-article (&optional n)
9024   "Delete the N next (mail) articles.
9025 This command actually deletes articles.  This is not a marking
9026 command.  The article will disappear forever from your life, never to
9027 return.
9028 If N is negative, delete backwards.
9029 If N is nil and articles have been marked with the process mark,
9030 delete these instead."
9031   (interactive "P")
9032   (unless (gnus-check-backend-function 'request-expire-articles
9033                                        gnus-newsgroup-name)
9034     (error "The current newsgroup does not support article deletion"))
9035   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9036     (error "Couldn't open server"))
9037   ;; Compute the list of articles to delete.
9038   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9039         (nnmail-expiry-target 'delete)
9040         not-deleted)
9041     (if (and gnus-novice-user
9042              (not (gnus-yes-or-no-p
9043                    (format "Do you really want to delete %s forever? "
9044                            (if (> (length articles) 1)
9045                                (format "these %s articles" (length articles))
9046                              "this article")))))
9047         ()
9048       ;; Delete the articles.
9049       (setq not-deleted (gnus-request-expire-articles
9050                          articles gnus-newsgroup-name 'force))
9051       (while articles
9052         (gnus-summary-remove-process-mark (car articles))
9053         ;; The backend might not have been able to delete the article
9054         ;; after all.
9055         (unless (memq (car articles) not-deleted)
9056           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9057         (setq articles (cdr articles)))
9058       (when not-deleted
9059         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9060     (gnus-summary-position-point)
9061     (gnus-set-mode-line 'summary)
9062     not-deleted))
9063
9064 (defun gnus-summary-edit-article (&optional arg)
9065   "Edit the current article.
9066 This will have permanent effect only in mail groups.
9067 If ARG is nil, edit the decoded articles.
9068 If ARG is 1, edit the raw articles.
9069 If ARG is 2, edit the raw articles even in read-only groups.
9070 If ARG is 3, edit the articles with the current handles.
9071 Otherwise, allow editing of articles even in read-only
9072 groups."
9073   (interactive "P")
9074   (let (force raw current-handles)
9075     (cond
9076      ((null arg))
9077      ((eq arg 1)
9078       (setq raw t))
9079      ((eq arg 2)
9080       (setq raw t
9081             force t))
9082      ((eq arg 3)
9083       (setq current-handles
9084             (and (gnus-buffer-live-p gnus-article-buffer)
9085                  (with-current-buffer gnus-article-buffer
9086                    (prog1
9087                        gnus-article-mime-handles
9088                      (setq gnus-article-mime-handles nil))))))
9089      (t
9090       (setq force t)))
9091     (when (and raw (not force)
9092                (member gnus-newsgroup-name '("nndraft:delayed"
9093                                              "nndraft:drafts"
9094                                              "nndraft:queue")))
9095       (error "Can't edit the raw article in group %s"
9096              gnus-newsgroup-name))
9097     (save-excursion
9098       (set-buffer gnus-summary-buffer)
9099       (let ((mail-parse-charset gnus-newsgroup-charset)
9100             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9101         (gnus-set-global-variables)
9102         (when (and (not force)
9103                    (gnus-group-read-only-p))
9104           (error "The current newsgroup does not support article editing"))
9105         (gnus-summary-show-article t)
9106         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9107           (with-current-buffer gnus-article-buffer
9108             (mm-enable-multibyte)))
9109         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9110             (setq raw t))
9111         (gnus-article-edit-article
9112          (if raw 'ignore
9113            `(lambda ()
9114               (let ((mbl mml-buffer-list))
9115                 (setq mml-buffer-list nil)
9116                 (mime-to-mml ,'current-handles)
9117                 (let ((mbl1 mml-buffer-list))
9118                   (setq mml-buffer-list mbl)
9119                   (set (make-local-variable 'mml-buffer-list) mbl1))
9120                 (make-local-hook 'kill-buffer-hook)
9121                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9122          `(lambda (no-highlight)
9123             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9124                   (message-options message-options)
9125                   (message-options-set-recipient)
9126                   (mail-parse-ignored-charsets
9127                    ',gnus-newsgroup-ignored-charsets))
9128               ,(if (not raw) '(progn
9129                                 (mml-to-mime)
9130                                 (mml-destroy-buffers)
9131                                 (remove-hook 'kill-buffer-hook
9132                                              'mml-destroy-buffers t)
9133                                 (kill-local-variable 'mml-buffer-list)))
9134               (gnus-summary-edit-article-done
9135                ,(or (mail-header-references gnus-current-headers) "")
9136                ,(gnus-group-read-only-p)
9137                ,gnus-summary-buffer no-highlight))))))))
9138
9139 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9140
9141 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9142                                                  no-highlight)
9143   "Make edits to the current article permanent."
9144   (interactive)
9145   (save-excursion
9146    ;; The buffer restriction contains the entire article if it exists.
9147     (when (article-goto-body)
9148       (let ((lines (count-lines (point) (point-max)))
9149             (length (- (point-max) (point)))
9150             (case-fold-search t)
9151             (body (copy-marker (point))))
9152         (goto-char (point-min))
9153         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9154           (delete-region (match-beginning 1) (match-end 1))
9155           (insert (number-to-string length)))
9156         (goto-char (point-min))
9157         (when (re-search-forward
9158                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9159           (delete-region (match-beginning 1) (match-end 1))
9160           (insert (number-to-string length)))
9161         (goto-char (point-min))
9162         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9163           (delete-region (match-beginning 1) (match-end 1))
9164           (insert (number-to-string lines))))))
9165   ;; Replace the article.
9166   (let ((buf (current-buffer)))
9167     (with-temp-buffer
9168       (insert-buffer-substring buf)
9169
9170       (if (and (not read-only)
9171                (not (gnus-request-replace-article
9172                      (cdr gnus-article-current) (car gnus-article-current)
9173                      (current-buffer) t)))
9174           (error "Couldn't replace article")
9175         ;; Update the summary buffer.
9176         (if (and references
9177                  (equal (message-tokenize-header references " ")
9178                         (message-tokenize-header
9179                          (or (message-fetch-field "references") "") " ")))
9180             ;; We only have to update this line.
9181             (save-excursion
9182               (save-restriction
9183                 (message-narrow-to-head)
9184                 (let ((head (buffer-string))
9185                       header)
9186                   (with-temp-buffer
9187                     (insert (format "211 %d Article retrieved.\n"
9188                                     (cdr gnus-article-current)))
9189                     (insert head)
9190                     (insert ".\n")
9191                     (let ((nntp-server-buffer (current-buffer)))
9192                       (setq header (car (gnus-get-newsgroup-headers
9193                                          nil t))))
9194                     (save-excursion
9195                       (set-buffer gnus-summary-buffer)
9196                       (gnus-data-set-header
9197                        (gnus-data-find (cdr gnus-article-current))
9198                        header)
9199                       (gnus-summary-update-article-line
9200                        (cdr gnus-article-current) header)
9201                       (if (gnus-summary-goto-subject
9202                            (cdr gnus-article-current) nil t)
9203                           (gnus-summary-update-secondary-mark
9204                            (cdr gnus-article-current))))))))
9205           ;; Update threads.
9206           (set-buffer (or buffer gnus-summary-buffer))
9207           (gnus-summary-update-article (cdr gnus-article-current))
9208           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9209               (gnus-summary-update-secondary-mark
9210                (cdr gnus-article-current))))
9211         ;; Prettify the article buffer again.
9212         (unless no-highlight
9213           (save-excursion
9214             (set-buffer gnus-article-buffer)
9215             ;;;!!! Fix this -- article should be rehighlighted.
9216             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9217             (set-buffer gnus-original-article-buffer)
9218             (gnus-request-article
9219              (cdr gnus-article-current)
9220              (car gnus-article-current) (current-buffer))))
9221         ;; Prettify the summary buffer line.
9222         (when (gnus-visual-p 'summary-highlight 'highlight)
9223           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9224
9225 (defun gnus-summary-edit-wash (key)
9226   "Perform editing command KEY in the article buffer."
9227   (interactive
9228    (list
9229     (progn
9230       (message "%s" (concat (this-command-keys) "- "))
9231       (read-char))))
9232   (message "")
9233   (gnus-summary-edit-article)
9234   (execute-kbd-macro (concat (this-command-keys) key))
9235   (gnus-article-edit-done))
9236
9237 ;;; Respooling
9238
9239 (defun gnus-summary-respool-query (&optional silent trace)
9240   "Query where the respool algorithm would put this article."
9241   (interactive)
9242   (let (gnus-mark-article-hook)
9243     (gnus-summary-select-article)
9244     (save-excursion
9245       (set-buffer gnus-original-article-buffer)
9246       (save-restriction
9247         (message-narrow-to-head)
9248         (let ((groups (nnmail-article-group 'identity trace)))
9249           (unless silent
9250             (if groups
9251                 (message "This message would go to %s"
9252                          (mapconcat 'car groups ", "))
9253               (message "This message would go to no groups"))
9254             groups))))))
9255
9256 (defun gnus-summary-respool-trace ()
9257   "Trace where the respool algorithm would put this article.
9258 Display a buffer showing all fancy splitting patterns which matched."
9259   (interactive)
9260   (gnus-summary-respool-query nil t))
9261
9262 ;; Summary marking commands.
9263
9264 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9265   "Mark articles which has the same subject as read, and then select the next.
9266 If UNMARK is positive, remove any kind of mark.
9267 If UNMARK is negative, tick articles."
9268   (interactive "P")
9269   (when unmark
9270     (setq unmark (prefix-numeric-value unmark)))
9271   (let ((count
9272          (gnus-summary-mark-same-subject
9273           (gnus-summary-article-subject) unmark)))
9274     ;; Select next unread article.  If auto-select-same mode, should
9275     ;; select the first unread article.
9276     (gnus-summary-next-article t (and gnus-auto-select-same
9277                                       (gnus-summary-article-subject)))
9278     (gnus-message 7 "%d article%s marked as %s"
9279                   count (if (= count 1) " is" "s are")
9280                   (if unmark "unread" "read"))))
9281
9282 (defun gnus-summary-kill-same-subject (&optional unmark)
9283   "Mark articles which has the same subject as read.
9284 If UNMARK is positive, remove any kind of mark.
9285 If UNMARK is negative, tick articles."
9286   (interactive "P")
9287   (when unmark
9288     (setq unmark (prefix-numeric-value unmark)))
9289   (let ((count
9290          (gnus-summary-mark-same-subject
9291           (gnus-summary-article-subject) unmark)))
9292     ;; If marked as read, go to next unread subject.
9293     (when (null unmark)
9294       ;; Go to next unread subject.
9295       (gnus-summary-next-subject 1 t))
9296     (gnus-message 7 "%d articles are marked as %s"
9297                   count (if unmark "unread" "read"))))
9298
9299 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9300   "Mark articles with same SUBJECT as read, and return marked number.
9301 If optional argument UNMARK is positive, remove any kinds of marks.
9302 If optional argument UNMARK is negative, mark articles as unread instead."
9303   (let ((count 1))
9304     (save-excursion
9305       (cond
9306        ((null unmark)                   ; Mark as read.
9307         (while (and
9308                 (progn
9309                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9310                   (gnus-summary-show-thread) t)
9311                 (gnus-summary-find-subject subject))
9312           (setq count (1+ count))))
9313        ((> unmark 0)                    ; Tick.
9314         (while (and
9315                 (progn
9316                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9317                   (gnus-summary-show-thread) t)
9318                 (gnus-summary-find-subject subject))
9319           (setq count (1+ count))))
9320        (t                               ; Mark as unread.
9321         (while (and
9322                 (progn
9323                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9324                   (gnus-summary-show-thread) t)
9325                 (gnus-summary-find-subject subject))
9326           (setq count (1+ count)))))
9327       (gnus-set-mode-line 'summary)
9328       ;; Return the number of marked articles.
9329       count)))
9330
9331 (defun gnus-summary-mark-as-processable (n &optional unmark)
9332   "Set the process mark on the next N articles.
9333 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9334 the process mark instead.  The difference between N and the actual
9335 number of articles marked is returned."
9336   (interactive "P")
9337   (if (and (null n) (gnus-region-active-p))
9338       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9339     (setq n (prefix-numeric-value n))
9340     (let ((backward (< n 0))
9341           (n (abs n)))
9342       (while (and
9343               (> n 0)
9344               (if unmark
9345                   (gnus-summary-remove-process-mark
9346                    (gnus-summary-article-number))
9347                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9348               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9349         (setq n (1- n)))
9350       (when (/= 0 n)
9351         (gnus-message 7 "No more articles"))
9352       (gnus-summary-recenter)
9353       (gnus-summary-position-point)
9354       n)))
9355
9356 (defun gnus-summary-unmark-as-processable (n)
9357   "Remove the process mark from the next N articles.
9358 If N is negative, unmark backward instead.  The difference between N and
9359 the actual number of articles unmarked is returned."
9360   (interactive "P")
9361   (gnus-summary-mark-as-processable n t))
9362
9363 (defun gnus-summary-unmark-all-processable ()
9364   "Remove the process mark from all articles."
9365   (interactive)
9366   (save-excursion
9367     (while gnus-newsgroup-processable
9368       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9369   (gnus-summary-position-point))
9370
9371 (defun gnus-summary-add-mark (article type)
9372   "Mark ARTICLE with a mark of TYPE."
9373   (let ((vtype (car (assq type gnus-article-mark-lists)))
9374         var)
9375     (if (not vtype)
9376         (error "No such mark type: %s" type)
9377       (setq var (intern (format "gnus-newsgroup-%s" type)))
9378       (set var (cons article (symbol-value var)))
9379       (if (memq type '(processable cached replied forwarded recent saved))
9380           (gnus-summary-update-secondary-mark article)
9381         ;;; !!! This is bogus.  We should find out what primary
9382         ;;; !!! mark we want to set.
9383         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9384
9385 (defun gnus-summary-mark-as-expirable (n)
9386   "Mark N articles forward as expirable.
9387 If N is negative, mark backward instead.  The difference between N and
9388 the actual number of articles marked is returned."
9389   (interactive "p")
9390   (gnus-summary-mark-forward n gnus-expirable-mark))
9391
9392 (defun gnus-summary-mark-as-spam (n)
9393   "Mark N articles forward as spam.
9394 If N is negative, mark backward instead.  The difference between N and
9395 the actual number of articles marked is returned."
9396   (interactive "p")
9397   (gnus-summary-mark-forward n gnus-spam-mark))
9398
9399 (defun gnus-summary-mark-article-as-replied (article)
9400   "Mark ARTICLE as replied to and update the summary line.
9401 ARTICLE can also be a list of articles."
9402   (interactive (list (gnus-summary-article-number)))
9403   (let ((articles (if (listp article) article (list article))))
9404     (dolist (article articles)
9405       (push article gnus-newsgroup-replied)
9406       (let ((buffer-read-only nil))
9407         (when (gnus-summary-goto-subject article nil t)
9408           (gnus-summary-update-secondary-mark article))))))
9409
9410 (defun gnus-summary-mark-article-as-forwarded (article)
9411   "Mark ARTICLE as forwarded and update the summary line.
9412 ARTICLE can also be a list of articles."
9413   (let ((articles (if (listp article) article (list article))))
9414     (dolist (article articles)
9415       (push article gnus-newsgroup-forwarded)
9416       (let ((buffer-read-only nil))
9417         (when (gnus-summary-goto-subject article nil t)
9418           (gnus-summary-update-secondary-mark article))))))
9419
9420 (defun gnus-summary-set-bookmark (article)
9421   "Set a bookmark in current article."
9422   (interactive (list (gnus-summary-article-number)))
9423   (when (or (not (get-buffer gnus-article-buffer))
9424             (not gnus-current-article)
9425             (not gnus-article-current)
9426             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9427     (error "No current article selected"))
9428   ;; Remove old bookmark, if one exists.
9429   (gnus-pull article gnus-newsgroup-bookmarks)
9430   ;; Set the new bookmark, which is on the form
9431   ;; (article-number . line-number-in-body).
9432   (push
9433    (cons article
9434          (save-excursion
9435            (set-buffer gnus-article-buffer)
9436            (count-lines
9437             (min (point)
9438                  (save-excursion
9439                    (article-goto-body)
9440                    (point)))
9441             (point))))
9442    gnus-newsgroup-bookmarks)
9443   (gnus-message 6 "A bookmark has been added to the current article."))
9444
9445 (defun gnus-summary-remove-bookmark (article)
9446   "Remove the bookmark from the current article."
9447   (interactive (list (gnus-summary-article-number)))
9448   ;; Remove old bookmark, if one exists.
9449   (if (not (assq article gnus-newsgroup-bookmarks))
9450       (gnus-message 6 "No bookmark in current article.")
9451     (gnus-pull article gnus-newsgroup-bookmarks)
9452     (gnus-message 6 "Removed bookmark.")))
9453
9454 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9455 (defun gnus-summary-mark-as-dormant (n)
9456   "Mark N articles forward as dormant.
9457 If N is negative, mark backward instead.  The difference between N and
9458 the actual number of articles marked is returned."
9459   (interactive "p")
9460   (gnus-summary-mark-forward n gnus-dormant-mark))
9461
9462 (defun gnus-summary-set-process-mark (article)
9463   "Set the process mark on ARTICLE and update the summary line."
9464   (setq gnus-newsgroup-processable
9465         (cons article
9466               (delq article gnus-newsgroup-processable)))
9467   (when (gnus-summary-goto-subject article)
9468     (gnus-summary-show-thread)
9469     (gnus-summary-goto-subject article)
9470     (gnus-summary-update-secondary-mark article)))
9471
9472 (defun gnus-summary-remove-process-mark (article)
9473   "Remove the process mark from ARTICLE and update the summary line."
9474   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9475   (when (gnus-summary-goto-subject article)
9476     (gnus-summary-show-thread)
9477     (gnus-summary-goto-subject article)
9478     (gnus-summary-update-secondary-mark article)))
9479
9480 (defun gnus-summary-set-saved-mark (article)
9481   "Set the process mark on ARTICLE and update the summary line."
9482   (push article gnus-newsgroup-saved)
9483   (when (gnus-summary-goto-subject article)
9484     (gnus-summary-update-secondary-mark article)))
9485
9486 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9487   "Mark N articles as read forwards.
9488 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9489 The difference between N and the actual number of articles marked is
9490 returned.
9491 If NO-EXPIRE, auto-expiry will be inhibited."
9492   (interactive "p")
9493   (gnus-summary-show-thread)
9494   (let ((backward (< n 0))
9495         (gnus-summary-goto-unread
9496          (and gnus-summary-goto-unread
9497               (not (eq gnus-summary-goto-unread 'never))
9498               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9499                                     gnus-ticked-mark gnus-dormant-mark)))))
9500         (n (abs n))
9501         (mark (or mark gnus-del-mark)))
9502     (while (and (> n 0)
9503                 (gnus-summary-mark-article nil mark no-expire)
9504                 (zerop (gnus-summary-next-subject
9505                         (if backward -1 1)
9506                         (and gnus-summary-goto-unread
9507                              (not (eq gnus-summary-goto-unread 'never)))
9508                         t)))
9509       (setq n (1- n)))
9510     (when (/= 0 n)
9511       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9512     (gnus-summary-recenter)
9513     (gnus-summary-position-point)
9514     (gnus-set-mode-line 'summary)
9515     n))
9516
9517 (defun gnus-summary-mark-article-as-read (mark)
9518   "Mark the current article quickly as read with MARK."
9519   (let ((article (gnus-summary-article-number)))
9520     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9521     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9522     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9523     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9524     (push (cons article mark) gnus-newsgroup-reads)
9525     ;; Possibly remove from cache, if that is used.
9526     (when gnus-use-cache
9527       (gnus-cache-enter-remove-article article))
9528     ;; Allow the backend to change the mark.
9529     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9530     ;; Check for auto-expiry.
9531     (when (and gnus-newsgroup-auto-expire
9532                (memq mark gnus-auto-expirable-marks))
9533       (setq mark gnus-expirable-mark)
9534       ;; Let the backend know about the mark change.
9535       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9536       (push article gnus-newsgroup-expirable))
9537     ;; Set the mark in the buffer.
9538     (gnus-summary-update-mark mark 'unread)
9539     t))
9540
9541 (defun gnus-summary-mark-article-as-unread (mark)
9542   "Mark the current article quickly as unread with MARK."
9543   (let* ((article (gnus-summary-article-number))
9544          (old-mark (gnus-summary-article-mark article)))
9545     ;; Allow the backend to change the mark.
9546     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9547     (if (eq mark old-mark)
9548         t
9549       (if (<= article 0)
9550           (progn
9551             (gnus-error 1 "Can't mark negative article numbers")
9552             nil)
9553         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9554         (setq gnus-newsgroup-spam-marked
9555               (delq article gnus-newsgroup-spam-marked))
9556         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9557         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9558         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9559         (cond ((= mark gnus-ticked-mark)
9560                (setq gnus-newsgroup-marked
9561                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9562                                               article)))
9563               ((= mark gnus-spam-mark)
9564                (setq gnus-newsgroup-spam-marked
9565                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9566                                               article)))
9567               ((= mark gnus-dormant-mark)
9568                (setq gnus-newsgroup-dormant
9569                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9570                                               article)))
9571               (t
9572                (setq gnus-newsgroup-unreads
9573                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9574                                               article))))
9575         (gnus-pull article gnus-newsgroup-reads)
9576
9577         ;; See whether the article is to be put in the cache.
9578         (and gnus-use-cache
9579              (vectorp (gnus-summary-article-header article))
9580              (save-excursion
9581                (gnus-cache-possibly-enter-article
9582                 gnus-newsgroup-name article
9583                 (= mark gnus-ticked-mark)
9584                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9585
9586         ;; Fix the mark.
9587         (gnus-summary-update-mark mark 'unread)
9588         t))))
9589
9590 (defun gnus-summary-mark-article (&optional article mark no-expire)
9591   "Mark ARTICLE with MARK.  MARK can be any character.
9592 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9593 `??' (dormant) and `?E' (expirable).
9594 If MARK is nil, then the default character `?r' is used.
9595 If ARTICLE is nil, then the article on the current line will be
9596 marked.
9597 If NO-EXPIRE, auto-expiry will be inhibited."
9598   ;; The mark might be a string.
9599   (when (stringp mark)
9600     (setq mark (aref mark 0)))
9601   ;; If no mark is given, then we check auto-expiring.
9602   (when (null mark)
9603     (setq mark gnus-del-mark))
9604   (when (and (not no-expire)
9605              gnus-newsgroup-auto-expire
9606              (memq mark gnus-auto-expirable-marks))
9607     (setq mark gnus-expirable-mark))
9608   (let ((article (or article (gnus-summary-article-number)))
9609         (old-mark (gnus-summary-article-mark article)))
9610     ;; Allow the backend to change the mark.
9611     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9612     (if (eq mark old-mark)
9613         t
9614       (unless article
9615         (error "No article on current line"))
9616       (if (not (if (or (= mark gnus-unread-mark)
9617                        (= mark gnus-ticked-mark)
9618                        (= mark gnus-spam-mark)
9619                        (= mark gnus-dormant-mark))
9620                    (gnus-mark-article-as-unread article mark)
9621                  (gnus-mark-article-as-read article mark)))
9622           t
9623         ;; See whether the article is to be put in the cache.
9624         (and gnus-use-cache
9625              (not (= mark gnus-canceled-mark))
9626              (vectorp (gnus-summary-article-header article))
9627              (save-excursion
9628                (gnus-cache-possibly-enter-article
9629                 gnus-newsgroup-name article
9630                 (= mark gnus-ticked-mark)
9631                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9632
9633         (when (gnus-summary-goto-subject article nil t)
9634           (let ((buffer-read-only nil))
9635             (gnus-summary-show-thread)
9636             ;; Fix the mark.
9637             (gnus-summary-update-mark mark 'unread)
9638             t))))))
9639
9640 (defun gnus-summary-update-secondary-mark (article)
9641   "Update the secondary (read, process, cache) mark."
9642   (gnus-summary-update-mark
9643    (cond ((memq article gnus-newsgroup-processable)
9644           gnus-process-mark)
9645          ((memq article gnus-newsgroup-cached)
9646           gnus-cached-mark)
9647          ((memq article gnus-newsgroup-replied)
9648           gnus-replied-mark)
9649          ((memq article gnus-newsgroup-forwarded)
9650           gnus-forwarded-mark)
9651          ((memq article gnus-newsgroup-saved)
9652           gnus-saved-mark)
9653          ((memq article gnus-newsgroup-recent)
9654           gnus-recent-mark)
9655          ((memq article gnus-newsgroup-unseen)
9656           gnus-unseen-mark)
9657          (t gnus-no-mark))
9658    'replied)
9659   (when (gnus-visual-p 'summary-highlight 'highlight)
9660     (gnus-run-hooks 'gnus-summary-update-hook))
9661   t)
9662
9663 (defun gnus-summary-update-download-mark (article)
9664   "Update the secondary (read, process, cache) mark."
9665   (gnus-summary-update-mark
9666    (cond ((memq article gnus-newsgroup-undownloaded) 
9667           gnus-undownloaded-mark)
9668          (gnus-newsgroup-agentized
9669           gnus-downloaded-mark)
9670          (t
9671           gnus-no-mark))
9672    'download)
9673   (gnus-summary-update-line t)
9674   t)
9675
9676 (defun gnus-summary-update-mark (mark type)
9677   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9678         (buffer-read-only nil))
9679     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9680     (when forward
9681       (when (looking-at "\r")
9682         (incf forward))
9683       (when (<= (+ forward (point)) (point-max))
9684         ;; Go to the right position on the line.
9685         (goto-char (+ forward (point)))
9686         ;; Replace the old mark with the new mark.
9687         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9688         ;; Optionally update the marks by some user rule.
9689         (when (eq type 'unread)
9690           (gnus-data-set-mark
9691            (gnus-data-find (gnus-summary-article-number)) mark)
9692           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9693
9694 (defun gnus-mark-article-as-read (article &optional mark)
9695   "Enter ARTICLE in the pertinent lists and remove it from others."
9696   ;; Make the article expirable.
9697   (let ((mark (or mark gnus-del-mark)))
9698     (setq gnus-newsgroup-expirable
9699           (if (= mark gnus-expirable-mark)
9700               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9701             (delq article gnus-newsgroup-expirable)))
9702     ;; Remove from unread and marked lists.
9703     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9704     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9705     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9706     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9707     (push (cons article mark) gnus-newsgroup-reads)
9708     ;; Possibly remove from cache, if that is used.
9709     (when gnus-use-cache
9710       (gnus-cache-enter-remove-article article))
9711     t))
9712
9713 (defun gnus-mark-article-as-unread (article &optional mark)
9714   "Enter ARTICLE in the pertinent lists and remove it from others."
9715   (let ((mark (or mark gnus-ticked-mark)))
9716     (if (<= article 0)
9717         (progn
9718           (gnus-error 1 "Can't mark negative article numbers")
9719           nil)
9720       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9721             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9722             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9723             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9724             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9725
9726       ;; Unsuppress duplicates?
9727       (when gnus-suppress-duplicates
9728         (gnus-dup-unsuppress-article article))
9729
9730       (cond ((= mark gnus-ticked-mark)
9731              (setq gnus-newsgroup-marked
9732                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9733             ((= mark gnus-spam-mark)
9734              (setq gnus-newsgroup-spam-marked
9735                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9736                                             article)))
9737             ((= mark gnus-dormant-mark)
9738              (setq gnus-newsgroup-dormant
9739                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9740             (t
9741              (setq gnus-newsgroup-unreads
9742                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9743       (gnus-pull article gnus-newsgroup-reads)
9744       t)))
9745
9746 (defalias 'gnus-summary-mark-as-unread-forward
9747   'gnus-summary-tick-article-forward)
9748 (make-obsolete 'gnus-summary-mark-as-unread-forward
9749                'gnus-summary-tick-article-forward)
9750 (defun gnus-summary-tick-article-forward (n)
9751   "Tick N articles forwards.
9752 If N is negative, tick backwards instead.
9753 The difference between N and the number of articles ticked is returned."
9754   (interactive "p")
9755   (gnus-summary-mark-forward n gnus-ticked-mark))
9756
9757 (defalias 'gnus-summary-mark-as-unread-backward
9758   'gnus-summary-tick-article-backward)
9759 (make-obsolete 'gnus-summary-mark-as-unread-backward
9760                'gnus-summary-tick-article-backward)
9761 (defun gnus-summary-tick-article-backward (n)
9762   "Tick N articles backwards.
9763 The difference between N and the number of articles ticked is returned."
9764   (interactive "p")
9765   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9766
9767 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9768 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9769 (defun gnus-summary-tick-article (&optional article clear-mark)
9770   "Mark current article as unread.
9771 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9772 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9773   (interactive)
9774   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9775                                        gnus-ticked-mark)))
9776
9777 (defun gnus-summary-mark-as-read-forward (n)
9778   "Mark N articles as read forwards.
9779 If N is negative, mark backwards instead.
9780 The difference between N and the actual number of articles marked is
9781 returned."
9782   (interactive "p")
9783   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9784
9785 (defun gnus-summary-mark-as-read-backward (n)
9786   "Mark the N articles as read backwards.
9787 The difference between N and the actual number of articles marked is
9788 returned."
9789   (interactive "p")
9790   (gnus-summary-mark-forward
9791    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9792
9793 (defun gnus-summary-mark-as-read (&optional article mark)
9794   "Mark current article as read.
9795 ARTICLE specifies the article to be marked as read.
9796 MARK specifies a string to be inserted at the beginning of the line."
9797   (gnus-summary-mark-article article mark))
9798
9799 (defun gnus-summary-clear-mark-forward (n)
9800   "Clear marks from N articles forward.
9801 If N is negative, clear backward instead.
9802 The difference between N and the number of marks cleared is returned."
9803   (interactive "p")
9804   (gnus-summary-mark-forward n gnus-unread-mark))
9805
9806 (defun gnus-summary-clear-mark-backward (n)
9807   "Clear marks from N articles backward.
9808 The difference between N and the number of marks cleared is returned."
9809   (interactive "p")
9810   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9811
9812 (defun gnus-summary-mark-unread-as-read ()
9813   "Intended to be used by `gnus-summary-mark-article-hook'."
9814   (when (memq gnus-current-article gnus-newsgroup-unreads)
9815     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9816
9817 (defun gnus-summary-mark-read-and-unread-as-read ()
9818   "Intended to be used by `gnus-summary-mark-article-hook'."
9819   (let ((mark (gnus-summary-article-mark)))
9820     (when (or (gnus-unread-mark-p mark)
9821               (gnus-read-mark-p mark))
9822       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9823
9824 (defun gnus-summary-mark-unread-as-ticked ()
9825   "Intended to be used by `gnus-summary-mark-article-hook'."
9826   (when (memq gnus-current-article gnus-newsgroup-unreads)
9827     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9828
9829 (defun gnus-summary-mark-region-as-read (point mark all)
9830   "Mark all unread articles between point and mark as read.
9831 If given a prefix, mark all articles between point and mark as read,
9832 even ticked and dormant ones."
9833   (interactive "r\nP")
9834   (save-excursion
9835     (let (article)
9836       (goto-char point)
9837       (beginning-of-line)
9838       (while (and
9839               (< (point) mark)
9840               (progn
9841                 (when (or all
9842                           (memq (setq article (gnus-summary-article-number))
9843                                 gnus-newsgroup-unreads))
9844                   (gnus-summary-mark-article article gnus-del-mark))
9845                 t)
9846               (gnus-summary-find-next))))))
9847
9848 (defun gnus-summary-mark-below (score mark)
9849   "Mark articles with score less than SCORE with MARK."
9850   (interactive "P\ncMark: ")
9851   (setq score (if score
9852                   (prefix-numeric-value score)
9853                 (or gnus-summary-default-score 0)))
9854   (save-excursion
9855     (set-buffer gnus-summary-buffer)
9856     (goto-char (point-min))
9857     (while
9858         (progn
9859           (and (< (gnus-summary-article-score) score)
9860                (gnus-summary-mark-article nil mark))
9861           (gnus-summary-find-next)))))
9862
9863 (defun gnus-summary-kill-below (&optional score)
9864   "Mark articles with score below SCORE as read."
9865   (interactive "P")
9866   (gnus-summary-mark-below score gnus-killed-mark))
9867
9868 (defun gnus-summary-clear-above (&optional score)
9869   "Clear all marks from articles with score above SCORE."
9870   (interactive "P")
9871   (gnus-summary-mark-above score gnus-unread-mark))
9872
9873 (defun gnus-summary-tick-above (&optional score)
9874   "Tick all articles with score above SCORE."
9875   (interactive "P")
9876   (gnus-summary-mark-above score gnus-ticked-mark))
9877
9878 (defun gnus-summary-mark-above (score mark)
9879   "Mark articles with score over SCORE with MARK."
9880   (interactive "P\ncMark: ")
9881   (setq score (if score
9882                   (prefix-numeric-value score)
9883                 (or gnus-summary-default-score 0)))
9884   (save-excursion
9885     (set-buffer gnus-summary-buffer)
9886     (goto-char (point-min))
9887     (while (and (progn
9888                   (when (> (gnus-summary-article-score) score)
9889                     (gnus-summary-mark-article nil mark))
9890                   t)
9891                 (gnus-summary-find-next)))))
9892
9893 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9894 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9895 (defun gnus-summary-limit-include-expunged (&optional no-error)
9896   "Display all the hidden articles that were expunged for low scores."
9897   (interactive)
9898   (let ((buffer-read-only nil))
9899     (let ((scored gnus-newsgroup-scored)
9900           headers h)
9901       (while scored
9902         (unless (gnus-summary-article-header (caar scored))
9903           (and (setq h (gnus-number-to-header (caar scored)))
9904                (< (cdar scored) gnus-summary-expunge-below)
9905                (push h headers)))
9906         (setq scored (cdr scored)))
9907       (if (not headers)
9908           (when (not no-error)
9909             (error "No expunged articles hidden"))
9910         (goto-char (point-min))
9911         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9912         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9913         (mapcar (lambda (x) (push (mail-header-number x)
9914                                   gnus-newsgroup-limit))
9915                 headers)
9916         (gnus-summary-prepare-unthreaded (nreverse headers))
9917         (goto-char (point-min))
9918         (gnus-summary-position-point)
9919         t))))
9920
9921 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9922   "Mark all unread articles in this newsgroup as read.
9923 If prefix argument ALL is non-nil, ticked and dormant articles will
9924 also be marked as read.
9925 If QUIETLY is non-nil, no questions will be asked.
9926 If TO-HERE is non-nil, it should be a point in the buffer.  All
9927 articles before (after, if REVERSE is set) this point will be marked as read.
9928 Note that this function will only catch up the unread article
9929 in the current summary buffer limitation.
9930 The number of articles marked as read is returned."
9931   (interactive "P")
9932   (prog1
9933       (save-excursion
9934         (when (or quietly
9935                   (not gnus-interactive-catchup) ;Without confirmation?
9936                   gnus-expert-user
9937                   (gnus-y-or-n-p
9938                    (if all
9939                        "Mark absolutely all articles as read? "
9940                      "Mark all unread articles as read? ")))
9941           (if (and not-mark
9942                    (not gnus-newsgroup-adaptive)
9943                    (not gnus-newsgroup-auto-expire)
9944                    (not gnus-suppress-duplicates)
9945                    (or (not gnus-use-cache)
9946                        (eq gnus-use-cache 'passive)))
9947               (progn
9948                 (when all
9949                   (setq gnus-newsgroup-marked nil
9950                         gnus-newsgroup-spam-marked nil
9951                         gnus-newsgroup-dormant nil))
9952                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9953             ;; We actually mark all articles as canceled, which we
9954             ;; have to do when using auto-expiry or adaptive scoring.
9955             (gnus-summary-show-all-threads)
9956             (if (and to-here reverse)
9957                 (progn
9958                   (goto-char to-here)
9959                   (while (and
9960                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9961                           (gnus-summary-find-next (not all) nil nil t))))
9962               (when (gnus-summary-first-subject (not all) t)
9963                 (while (and
9964                         (if to-here (< (point) to-here) t)
9965                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9966                         (gnus-summary-find-next (not all) nil nil t)))))
9967             (gnus-set-mode-line 'summary))
9968           t))
9969     (gnus-summary-position-point)))
9970
9971 (defun gnus-summary-catchup-to-here (&optional all)
9972   "Mark all unticked articles before the current one as read.
9973 If ALL is non-nil, also mark ticked and dormant articles as read."
9974   (interactive "P")
9975   (save-excursion
9976     (gnus-save-hidden-threads
9977       (let ((beg (point)))
9978         ;; We check that there are unread articles.
9979         (when (or all (gnus-summary-find-prev))
9980           (gnus-summary-catchup all t beg)))))
9981   (gnus-summary-position-point))
9982
9983 (defun gnus-summary-catchup-from-here (&optional all)
9984   "Mark all unticked articles after the current one as read.
9985 If ALL is non-nil, also mark ticked and dormant articles as read."
9986   (interactive "P")
9987   (save-excursion
9988     (gnus-save-hidden-threads
9989       (let ((beg (point)))
9990         ;; We check that there are unread articles.
9991         (when (or all (gnus-summary-find-next))
9992           (gnus-summary-catchup all t beg nil t)))))
9993
9994   (gnus-summary-position-point))
9995 (defun gnus-summary-catchup-all (&optional quietly)
9996   "Mark all articles in this newsgroup as read.
9997 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9998 instead, which marks only unread articles as read."
9999   (interactive "P")
10000   (gnus-summary-catchup t quietly))
10001
10002 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10003   "Mark all unread articles in this group as read, then exit.
10004 If prefix argument ALL is non-nil, all articles are marked as read.
10005 If QUIETLY is non-nil, no questions will be asked."
10006   (interactive "P")
10007   (when (gnus-summary-catchup all quietly nil 'fast)
10008     ;; Select next newsgroup or exit.
10009     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10010              (eq gnus-auto-select-next 'quietly))
10011         (gnus-summary-next-group nil)
10012       (gnus-summary-exit))))
10013
10014 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10015   "Mark all articles in this newsgroup as read, and then exit.
10016 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10017 instead, which marks only unread articles as read."
10018   (interactive "P")
10019   (gnus-summary-catchup-and-exit t quietly))
10020
10021 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10022   "Mark all articles in this group as read and select the next group.
10023 If given a prefix, mark all articles, unread as well as ticked, as
10024 read."
10025   (interactive "P")
10026   (save-excursion
10027     (gnus-summary-catchup all))
10028   (gnus-summary-next-group))
10029
10030 ;;;
10031 ;;; with article
10032 ;;;
10033
10034 (defmacro gnus-with-article (article &rest forms)
10035   "Select ARTICLE and perform FORMS in the original article buffer.
10036 Then replace the article with the result."
10037   `(progn
10038      ;; We don't want the article to be marked as read.
10039      (let (gnus-mark-article-hook)
10040        (gnus-summary-select-article t t nil ,article))
10041      (set-buffer gnus-original-article-buffer)
10042      ,@forms
10043      (if (not (gnus-check-backend-function
10044                'request-replace-article (car gnus-article-current)))
10045          (gnus-message 5 "Read-only group; not replacing")
10046        (unless (gnus-request-replace-article
10047                 ,article (car gnus-article-current)
10048                 (current-buffer) t)
10049          (error "Couldn't replace article")))
10050      ;; The cache and backlog have to be flushed somewhat.
10051      (when gnus-keep-backlog
10052        (gnus-backlog-remove-article
10053         (car gnus-article-current) (cdr gnus-article-current)))
10054      (when gnus-use-cache
10055        (gnus-cache-update-article
10056         (car gnus-article-current) (cdr gnus-article-current)))))
10057
10058 (put 'gnus-with-article 'lisp-indent-function 1)
10059 (put 'gnus-with-article 'edebug-form-spec '(form body))
10060
10061 ;; Thread-based commands.
10062
10063 (defun gnus-summary-articles-in-thread (&optional article)
10064   "Return a list of all articles in the current thread.
10065 If ARTICLE is non-nil, return all articles in the thread that starts
10066 with that article."
10067   (let* ((article (or article (gnus-summary-article-number)))
10068          (data (gnus-data-find-list article))
10069          (top-level (gnus-data-level (car data)))
10070          (top-subject
10071           (cond ((null gnus-thread-operation-ignore-subject)
10072                  (gnus-simplify-subject-re
10073                   (mail-header-subject (gnus-data-header (car data)))))
10074                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10075                  (gnus-simplify-subject-fuzzy
10076                   (mail-header-subject (gnus-data-header (car data)))))
10077                 (t nil)))
10078          (end-point (save-excursion
10079                       (if (gnus-summary-go-to-next-thread)
10080                           (point) (point-max))))
10081          articles)
10082     (while (and data
10083                 (< (gnus-data-pos (car data)) end-point))
10084       (when (or (not top-subject)
10085                 (string= top-subject
10086                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10087                              (gnus-simplify-subject-fuzzy
10088                               (mail-header-subject
10089                                (gnus-data-header (car data))))
10090                            (gnus-simplify-subject-re
10091                             (mail-header-subject
10092                              (gnus-data-header (car data)))))))
10093         (push (gnus-data-number (car data)) articles))
10094       (unless (and (setq data (cdr data))
10095                    (> (gnus-data-level (car data)) top-level))
10096         (setq data nil)))
10097     ;; Return the list of articles.
10098     (nreverse articles)))
10099
10100 (defun gnus-summary-rethread-current ()
10101   "Rethread the thread the current article is part of."
10102   (interactive)
10103   (let* ((gnus-show-threads t)
10104          (article (gnus-summary-article-number))
10105          (id (mail-header-id (gnus-summary-article-header)))
10106          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10107     (unless id
10108       (error "No article on the current line"))
10109     (gnus-rebuild-thread id)
10110     (gnus-summary-goto-subject article)))
10111
10112 (defun gnus-summary-reparent-thread ()
10113   "Make the current article child of the marked (or previous) article.
10114
10115 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10116 is non-nil or the Subject: of both articles are the same."
10117   (interactive)
10118   (unless (not (gnus-group-read-only-p))
10119     (error "The current newsgroup does not support article editing"))
10120   (unless (<= (length gnus-newsgroup-processable) 1)
10121     (error "No more than one article may be marked"))
10122   (save-window-excursion
10123     (let ((gnus-article-buffer " *reparent*")
10124           (current-article (gnus-summary-article-number))
10125           ;; First grab the marked article, otherwise one line up.
10126           (parent-article (if (not (null gnus-newsgroup-processable))
10127                               (car gnus-newsgroup-processable)
10128                             (save-excursion
10129                               (if (eq (forward-line -1) 0)
10130                                   (gnus-summary-article-number)
10131                                 (error "Beginning of summary buffer"))))))
10132       (unless (not (eq current-article parent-article))
10133         (error "An article may not be self-referential"))
10134       (let ((message-id (mail-header-id
10135                          (gnus-summary-article-header parent-article))))
10136         (unless (and message-id (not (equal message-id "")))
10137           (error "No message-id in desired parent"))
10138         (gnus-with-article current-article
10139           (save-restriction
10140             (goto-char (point-min))
10141             (message-narrow-to-head)
10142             (if (re-search-forward "^References: " nil t)
10143                 (progn
10144                   (re-search-forward "^[^ \t]" nil t)
10145                   (forward-line -1)
10146                   (end-of-line)
10147                   (insert " " message-id))
10148               (insert "References: " message-id "\n"))))
10149         (set-buffer gnus-summary-buffer)
10150         (gnus-summary-unmark-all-processable)
10151         (gnus-summary-update-article current-article)
10152         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10153             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10154         (gnus-summary-rethread-current)
10155         (gnus-message 3 "Article %d is now the child of article %d"
10156                       current-article parent-article)))))
10157
10158 (defun gnus-summary-toggle-threads (&optional arg)
10159   "Toggle showing conversation threads.
10160 If ARG is positive number, turn showing conversation threads on."
10161   (interactive "P")
10162   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10163     (setq gnus-show-threads
10164           (if (null arg) (not gnus-show-threads)
10165             (> (prefix-numeric-value arg) 0)))
10166     (gnus-summary-prepare)
10167     (gnus-summary-goto-subject current)
10168     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10169     (gnus-summary-position-point)))
10170
10171 (defun gnus-summary-show-all-threads ()
10172   "Show all threads."
10173   (interactive)
10174   (save-excursion
10175     (let ((buffer-read-only nil))
10176       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10177   (gnus-summary-position-point))
10178
10179 (defun gnus-summary-show-thread ()
10180   "Show thread subtrees.
10181 Returns nil if no thread was there to be shown."
10182   (interactive)
10183   (let ((buffer-read-only nil)
10184         (orig (point))
10185         ;; first goto end then to beg, to have point at beg after let
10186         (end (progn (end-of-line) (point)))
10187         (beg (progn (beginning-of-line) (point))))
10188     (prog1
10189         ;; Any hidden lines here?
10190         (search-forward "\r" end t)
10191       (subst-char-in-region beg end ?\^M ?\n t)
10192       (goto-char orig)
10193       (gnus-summary-position-point))))
10194
10195 (defun gnus-summary-maybe-hide-threads ()
10196   "If requested, hide the threads that should be hidden."
10197   (when (and gnus-show-threads
10198              gnus-thread-hide-subtree)
10199     (gnus-summary-hide-all-threads
10200      (if (or (consp gnus-thread-hide-subtree)
10201              (gnus-functionp gnus-thread-hide-subtree))
10202          (gnus-make-predicate gnus-thread-hide-subtree)
10203        nil))))
10204
10205 ;;; Hiding predicates.
10206
10207 (defun gnus-article-unread-p (header)
10208   (memq (mail-header-number header) gnus-newsgroup-unreads))
10209
10210 (defun gnus-article-unseen-p (header)
10211   (memq (mail-header-number header) gnus-newsgroup-unseen))
10212
10213 (defun gnus-map-articles (predicate articles)
10214   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10215   (apply 'gnus-or (mapcar predicate
10216                           (mapcar 'gnus-summary-article-header articles))))
10217
10218 (defun gnus-summary-hide-all-threads (&optional predicate)
10219   "Hide all thread subtrees.
10220 If PREDICATE is supplied, threads that satisfy this predicate
10221 will not be hidden."
10222   (interactive)
10223   (save-excursion
10224     (goto-char (point-min))
10225     (let ((end nil))
10226       (while (not end)
10227         (when (or (not predicate)
10228                   (gnus-map-articles
10229                    predicate (gnus-summary-article-children)))
10230             (gnus-summary-hide-thread))
10231         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10232   (gnus-summary-position-point))
10233
10234 (defun gnus-summary-hide-thread ()
10235   "Hide thread subtrees.
10236 If PREDICATE is supplied, threads that satisfy this predicate
10237 will not be hidden.
10238 Returns nil if no threads were there to be hidden."
10239   (interactive)
10240   (let ((buffer-read-only nil)
10241         (start (point))
10242         (article (gnus-summary-article-number)))
10243     (goto-char start)
10244     ;; Go forward until either the buffer ends or the subthread
10245     ;; ends.
10246     (when (and (not (eobp))
10247                (or (zerop (gnus-summary-next-thread 1 t))
10248                    (goto-char (point-max))))
10249       (prog1
10250           (if (and (> (point) start)
10251                    (search-backward "\n" start t))
10252               (progn
10253                 (subst-char-in-region start (point) ?\n ?\^M)
10254                 (gnus-summary-goto-subject article))
10255             (goto-char start)
10256             nil)))))
10257
10258 (defun gnus-summary-go-to-next-thread (&optional previous)
10259   "Go to the same level (or less) next thread.
10260 If PREVIOUS is non-nil, go to previous thread instead.
10261 Return the article number moved to, or nil if moving was impossible."
10262   (let ((level (gnus-summary-thread-level))
10263         (way (if previous -1 1))
10264         (beg (point)))
10265     (forward-line way)
10266     (while (and (not (eobp))
10267                 (< level (gnus-summary-thread-level)))
10268       (forward-line way))
10269     (if (eobp)
10270         (progn
10271           (goto-char beg)
10272           nil)
10273       (setq beg (point))
10274       (prog1
10275           (gnus-summary-article-number)
10276         (goto-char beg)))))
10277
10278 (defun gnus-summary-next-thread (n &optional silent)
10279   "Go to the same level next N'th thread.
10280 If N is negative, search backward instead.
10281 Returns the difference between N and the number of skips actually
10282 done.
10283
10284 If SILENT, don't output messages."
10285   (interactive "p")
10286   (let ((backward (< n 0))
10287         (n (abs n)))
10288     (while (and (> n 0)
10289                 (gnus-summary-go-to-next-thread backward))
10290       (decf n))
10291     (unless silent
10292       (gnus-summary-position-point))
10293     (when (and (not silent) (/= 0 n))
10294       (gnus-message 7 "No more threads"))
10295     n))
10296
10297 (defun gnus-summary-prev-thread (n)
10298   "Go to the same level previous N'th thread.
10299 Returns the difference between N and the number of skips actually
10300 done."
10301   (interactive "p")
10302   (gnus-summary-next-thread (- n)))
10303
10304 (defun gnus-summary-go-down-thread ()
10305   "Go down one level in the current thread."
10306   (let ((children (gnus-summary-article-children)))
10307     (when children
10308       (gnus-summary-goto-subject (car children)))))
10309
10310 (defun gnus-summary-go-up-thread ()
10311   "Go up one level in the current thread."
10312   (let ((parent (gnus-summary-article-parent)))
10313     (when parent
10314       (gnus-summary-goto-subject parent))))
10315
10316 (defun gnus-summary-down-thread (n)
10317   "Go down thread N steps.
10318 If N is negative, go up instead.
10319 Returns the difference between N and how many steps down that were
10320 taken."
10321   (interactive "p")
10322   (let ((up (< n 0))
10323         (n (abs n)))
10324     (while (and (> n 0)
10325                 (if up (gnus-summary-go-up-thread)
10326                   (gnus-summary-go-down-thread)))
10327       (setq n (1- n)))
10328     (gnus-summary-position-point)
10329     (when (/= 0 n)
10330       (gnus-message 7 "Can't go further"))
10331     n))
10332
10333 (defun gnus-summary-up-thread (n)
10334   "Go up thread N steps.
10335 If N is negative, go down instead.
10336 Returns the difference between N and how many steps down that were
10337 taken."
10338   (interactive "p")
10339   (gnus-summary-down-thread (- n)))
10340
10341 (defun gnus-summary-top-thread ()
10342   "Go to the top of the thread."
10343   (interactive)
10344   (while (gnus-summary-go-up-thread))
10345   (gnus-summary-article-number))
10346
10347 (defun gnus-summary-kill-thread (&optional unmark)
10348   "Mark articles under current thread as read.
10349 If the prefix argument is positive, remove any kinds of marks.
10350 If the prefix argument is negative, tick articles instead."
10351   (interactive "P")
10352   (when unmark
10353     (setq unmark (prefix-numeric-value unmark)))
10354   (let ((articles (gnus-summary-articles-in-thread)))
10355     (save-excursion
10356       ;; Expand the thread.
10357       (gnus-summary-show-thread)
10358       ;; Mark all the articles.
10359       (while articles
10360         (gnus-summary-goto-subject (car articles))
10361         (cond ((null unmark)
10362                (gnus-summary-mark-article-as-read gnus-killed-mark))
10363               ((> unmark 0)
10364                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10365               (t
10366                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10367         (setq articles (cdr articles))))
10368     ;; Hide killed subtrees.
10369     (and (null unmark)
10370          gnus-thread-hide-killed
10371          (gnus-summary-hide-thread))
10372     ;; If marked as read, go to next unread subject.
10373     (when (null unmark)
10374       ;; Go to next unread subject.
10375       (gnus-summary-next-subject 1 t)))
10376   (gnus-set-mode-line 'summary))
10377
10378 ;; Summary sorting commands
10379
10380 (defun gnus-summary-sort-by-number (&optional reverse)
10381   "Sort the summary buffer by article number.
10382 Argument REVERSE means reverse order."
10383   (interactive "P")
10384   (gnus-summary-sort 'number reverse))
10385
10386 (defun gnus-summary-sort-by-random (&optional reverse)
10387   "Randomize the order in the summary buffer.
10388 Argument REVERSE means to randomize in reverse order."
10389   (interactive "P")
10390   (gnus-summary-sort 'random reverse))
10391
10392 (defun gnus-summary-sort-by-author (&optional reverse)
10393   "Sort the summary buffer by author name alphabetically.
10394 If `case-fold-search' is non-nil, case of letters is ignored.
10395 Argument REVERSE means reverse order."
10396   (interactive "P")
10397   (gnus-summary-sort 'author reverse))
10398
10399 (defun gnus-summary-sort-by-subject (&optional reverse)
10400   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10401 If `case-fold-search' is non-nil, case of letters is ignored.
10402 Argument REVERSE means reverse order."
10403   (interactive "P")
10404   (gnus-summary-sort 'subject reverse))
10405
10406 (defun gnus-summary-sort-by-date (&optional reverse)
10407   "Sort the summary buffer by date.
10408 Argument REVERSE means reverse order."
10409   (interactive "P")
10410   (gnus-summary-sort 'date reverse))
10411
10412 (defun gnus-summary-sort-by-score (&optional reverse)
10413   "Sort the summary buffer by score.
10414 Argument REVERSE means reverse order."
10415   (interactive "P")
10416   (gnus-summary-sort 'score reverse))
10417
10418 (defun gnus-summary-sort-by-lines (&optional reverse)
10419   "Sort the summary buffer by the number of lines.
10420 Argument REVERSE means reverse order."
10421   (interactive "P")
10422   (gnus-summary-sort 'lines reverse))
10423
10424 (defun gnus-summary-sort-by-chars (&optional reverse)
10425   "Sort the summary buffer by article length.
10426 Argument REVERSE means reverse order."
10427   (interactive "P")
10428   (gnus-summary-sort 'chars reverse))
10429
10430 (defun gnus-summary-sort-by-original (&optional reverse)
10431   "Sort the summary buffer using the default sorting method.
10432 Argument REVERSE means reverse order."
10433   (interactive "P")
10434   (let* ((buffer-read-only)
10435          (gnus-summary-prepare-hook nil))
10436     ;; We do the sorting by regenerating the threads.
10437     (gnus-summary-prepare)
10438     ;; Hide subthreads if needed.
10439     (gnus-summary-maybe-hide-threads)))
10440
10441 (defun gnus-summary-sort (predicate reverse)
10442   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10443   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10444          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10445          (gnus-thread-sort-functions
10446           (if (not reverse)
10447               thread
10448             `(lambda (t1 t2)
10449                (,thread t2 t1))))
10450          (gnus-sort-gathered-threads-function
10451           gnus-thread-sort-functions)
10452          (gnus-article-sort-functions
10453           (if (not reverse)
10454               article
10455             `(lambda (t1 t2)
10456                (,article t2 t1))))
10457          (buffer-read-only)
10458          (gnus-summary-prepare-hook nil))
10459     ;; We do the sorting by regenerating the threads.
10460     (gnus-summary-prepare)
10461     ;; Hide subthreads if needed.
10462     (gnus-summary-maybe-hide-threads)))
10463
10464 ;; Summary saving commands.
10465
10466 (defun gnus-summary-save-article (&optional n not-saved)
10467   "Save the current article using the default saver function.
10468 If N is a positive number, save the N next articles.
10469 If N is a negative number, save the N previous articles.
10470 If N is nil and any articles have been marked with the process mark,
10471 save those articles instead.
10472 The variable `gnus-default-article-saver' specifies the saver function."
10473   (interactive "P")
10474   (let* ((articles (gnus-summary-work-articles n))
10475          (save-buffer (save-excursion
10476                         (nnheader-set-temp-buffer " *Gnus Save*")))
10477          (num (length articles))
10478          header file)
10479     (dolist (article articles)
10480       (setq header (gnus-summary-article-header article))
10481       (if (not (vectorp header))
10482           ;; This is a pseudo-article.
10483           (if (assq 'name header)
10484               (gnus-copy-file (cdr (assq 'name header)))
10485             (gnus-message 1 "Article %d is unsaveable" article))
10486         ;; This is a real article.
10487         (save-window-excursion
10488           (let ((gnus-display-mime-function nil)
10489                 (gnus-article-prepare-hook nil))
10490             (gnus-summary-select-article t nil nil article)))
10491         (save-excursion
10492           (set-buffer save-buffer)
10493           (erase-buffer)
10494           (insert-buffer-substring gnus-original-article-buffer))
10495         (setq file (gnus-article-save save-buffer file num))
10496         (gnus-summary-remove-process-mark article)
10497         (unless not-saved
10498           (gnus-summary-set-saved-mark article))))
10499     (gnus-kill-buffer save-buffer)
10500     (gnus-summary-position-point)
10501     (gnus-set-mode-line 'summary)
10502     n))
10503
10504 (defun gnus-summary-pipe-output (&optional arg headers)
10505   "Pipe the current article to a subprocess.
10506 If N is a positive number, pipe the N next articles.
10507 If N is a negative number, pipe the N previous articles.
10508 If N is nil and any articles have been marked with the process mark,
10509 pipe those articles instead.
10510 If HEADERS (the symbolic prefix), include the headers, too."
10511   (interactive (gnus-interactive "P\ny"))
10512   (require 'gnus-art)
10513   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10514         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10515     (gnus-summary-save-article arg t))
10516   (let ((buffer (get-buffer "*Shell Command Output*")))
10517     (when (and buffer
10518                (not (zerop (buffer-size buffer))))
10519       (gnus-configure-windows 'pipe))))
10520
10521 (defun gnus-summary-save-article-mail (&optional arg)
10522   "Append the current article to an mail file.
10523 If N is a positive number, save the N next articles.
10524 If N is a negative number, save the N previous articles.
10525 If N is nil and any articles have been marked with the process mark,
10526 save those articles instead."
10527   (interactive "P")
10528   (require 'gnus-art)
10529   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10530     (gnus-summary-save-article arg)))
10531
10532 (defun gnus-summary-save-article-rmail (&optional arg)
10533   "Append the current article to an rmail file.
10534 If N is a positive number, save the N next articles.
10535 If N is a negative number, save the N previous articles.
10536 If N is nil and any articles have been marked with the process mark,
10537 save those articles instead."
10538   (interactive "P")
10539   (require 'gnus-art)
10540   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10541     (gnus-summary-save-article arg)))
10542
10543 (defun gnus-summary-save-article-file (&optional arg)
10544   "Append the current article to a file.
10545 If N is a positive number, save the N next articles.
10546 If N is a negative number, save the N previous articles.
10547 If N is nil and any articles have been marked with the process mark,
10548 save those articles instead."
10549   (interactive "P")
10550   (require 'gnus-art)
10551   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10552     (gnus-summary-save-article arg)))
10553
10554 (defun gnus-summary-write-article-file (&optional arg)
10555   "Write the current article to a file, deleting the previous file.
10556 If N is a positive number, save the N next articles.
10557 If N is a negative number, save the N previous articles.
10558 If N is nil and any articles have been marked with the process mark,
10559 save those articles instead."
10560   (interactive "P")
10561   (require 'gnus-art)
10562   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10563     (gnus-summary-save-article arg)))
10564
10565 (defun gnus-summary-save-article-body-file (&optional arg)
10566   "Append the current article body to a file.
10567 If N is a positive number, save the N next articles.
10568 If N is a negative number, save the N previous articles.
10569 If N is nil and any articles have been marked with the process mark,
10570 save those articles instead."
10571   (interactive "P")
10572   (require 'gnus-art)
10573   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10574     (gnus-summary-save-article arg)))
10575
10576 (defun gnus-summary-muttprint (&optional arg)
10577   "Print the current article using Muttprint.
10578 If N is a positive number, save the N next articles.
10579 If N is a negative number, save the N previous articles.
10580 If N is nil and any articles have been marked with the process mark,
10581 save those articles instead."
10582   (interactive "P")
10583   (require 'gnus-art)
10584   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10585     (gnus-summary-save-article arg t)))
10586
10587 (defun gnus-summary-pipe-message (program)
10588   "Pipe the current article through PROGRAM."
10589   (interactive "sProgram: ")
10590   (gnus-summary-select-article)
10591   (let ((mail-header-separator ""))
10592     (gnus-eval-in-buffer-window gnus-article-buffer
10593       (save-restriction
10594         (widen)
10595         (let ((start (window-start))
10596               buffer-read-only)
10597           (message-pipe-buffer-body program)
10598           (set-window-start (get-buffer-window (current-buffer)) start))))))
10599
10600 (defun gnus-get-split-value (methods)
10601   "Return a value based on the split METHODS."
10602   (let (split-name method result match)
10603     (when methods
10604       (save-excursion
10605         (set-buffer gnus-original-article-buffer)
10606         (save-restriction
10607           (nnheader-narrow-to-headers)
10608           (while (and methods (not split-name))
10609             (goto-char (point-min))
10610             (setq method (pop methods))
10611             (setq match (car method))
10612             (when (cond
10613                    ((stringp match)
10614                     ;; Regular expression.
10615                     (ignore-errors
10616                       (re-search-forward match nil t)))
10617                    ((gnus-functionp match)
10618                     ;; Function.
10619                     (save-restriction
10620                       (widen)
10621                       (setq result (funcall match gnus-newsgroup-name))))
10622                    ((consp match)
10623                     ;; Form.
10624                     (save-restriction
10625                       (widen)
10626                       (setq result (eval match)))))
10627               (setq split-name (cdr method))
10628               (cond ((stringp result)
10629                      (push (expand-file-name
10630                             result gnus-article-save-directory)
10631                            split-name))
10632                     ((consp result)
10633                      (setq split-name (append result split-name)))))))))
10634     (nreverse split-name)))
10635
10636 (defun gnus-valid-move-group-p (group)
10637   (and (boundp group)
10638        (symbol-name group)
10639        (symbol-value group)
10640        (gnus-get-function (gnus-find-method-for-group
10641                            (symbol-name group)) 'request-accept-article t)))
10642
10643 (defun gnus-read-move-group-name (prompt default articles prefix)
10644   "Read a group name."
10645   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10646          (minibuffer-confirm-incomplete nil) ; XEmacs
10647          (prom
10648           (format "%s %s to:"
10649                   prompt
10650                   (if (> (length articles) 1)
10651                       (format "these %d articles" (length articles))
10652                     "this article")))
10653          (to-newsgroup
10654           (cond
10655            ((null split-name)
10656             (gnus-completing-read-with-default
10657              default prom
10658              gnus-active-hashtb
10659              'gnus-valid-move-group-p
10660              nil prefix
10661              'gnus-group-history))
10662            ((= 1 (length split-name))
10663             (gnus-completing-read-with-default
10664              (car split-name) prom
10665              gnus-active-hashtb
10666              'gnus-valid-move-group-p
10667              nil nil
10668              'gnus-group-history))
10669            (t
10670             (gnus-completing-read-with-default
10671              nil prom
10672              (mapcar (lambda (el) (list el))
10673                      (nreverse split-name))
10674              nil nil nil
10675              'gnus-group-history))))
10676          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10677     (when to-newsgroup
10678       (if (or (string= to-newsgroup "")
10679               (string= to-newsgroup prefix))
10680           (setq to-newsgroup default))
10681       (unless to-newsgroup
10682         (error "No group name entered"))
10683       (or (gnus-active to-newsgroup)
10684           (gnus-activate-group to-newsgroup nil nil to-method)
10685           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10686                                      to-newsgroup))
10687               (or (and (gnus-request-create-group to-newsgroup to-method)
10688                        (gnus-activate-group
10689                         to-newsgroup nil nil to-method)
10690                        (gnus-subscribe-group to-newsgroup))
10691                   (error "Couldn't create group %s" to-newsgroup)))
10692           (error "No such group: %s" to-newsgroup)))
10693     to-newsgroup))
10694
10695 (defun gnus-summary-save-parts (type dir n &optional reverse)
10696   "Save parts matching TYPE to DIR.
10697 If REVERSE, save parts that do not match TYPE."
10698   (interactive
10699    (list (read-string "Save parts of type: "
10700                       (or (car gnus-summary-save-parts-type-history)
10701                           gnus-summary-save-parts-default-mime)
10702                       'gnus-summary-save-parts-type-history)
10703          (setq gnus-summary-save-parts-last-directory
10704                (read-file-name "Save to directory: "
10705                                gnus-summary-save-parts-last-directory
10706                                nil t))
10707          current-prefix-arg))
10708   (gnus-summary-iterate n
10709     (let ((gnus-display-mime-function nil)
10710           (gnus-inhibit-treatment t))
10711       (gnus-summary-select-article))
10712     (save-excursion
10713       (set-buffer gnus-article-buffer)
10714       (let ((handles (or gnus-article-mime-handles
10715                          (mm-dissect-buffer nil gnus-article-loose-mime)
10716                          (mm-uu-dissect))))
10717         (when handles
10718           (gnus-summary-save-parts-1 type dir handles reverse)
10719           (unless gnus-article-mime-handles ;; Don't destroy this case.
10720             (mm-destroy-parts handles)))))))
10721
10722 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10723   (if (stringp (car handle))
10724       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10725               (cdr handle))
10726     (when (if reverse
10727               (not (string-match type (mm-handle-media-type handle)))
10728             (string-match type (mm-handle-media-type handle)))
10729       (let ((file (expand-file-name
10730                    (file-name-nondirectory
10731                     (or
10732                      (mail-content-type-get
10733                       (mm-handle-disposition handle) 'filename)
10734                      (concat gnus-newsgroup-name
10735                              "." (number-to-string
10736                                   (cdr gnus-article-current)))))
10737                    dir)))
10738         (unless (file-exists-p file)
10739           (mm-save-part-to-file handle file))))))
10740
10741 ;; Summary extract commands
10742
10743 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10744   (let ((buffer-read-only nil)
10745         (article (gnus-summary-article-number))
10746         after-article b e)
10747     (unless (gnus-summary-goto-subject article)
10748       (error "No such article: %d" article))
10749     (gnus-summary-position-point)
10750     ;; If all commands are to be bunched up on one line, we collect
10751     ;; them here.
10752     (unless gnus-view-pseudos-separately
10753       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10754             files action)
10755         (while ps
10756           (setq action (cdr (assq 'action (car ps))))
10757           (setq files (list (cdr (assq 'name (car ps)))))
10758           (while (and ps (cdr ps)
10759                       (string= (or action "1")
10760                                (or (cdr (assq 'action (cadr ps))) "2")))
10761             (push (cdr (assq 'name (cadr ps))) files)
10762             (setcdr ps (cddr ps)))
10763           (when files
10764             (when (not (string-match "%s" action))
10765               (push " " files))
10766             (push " " files)
10767             (when (assq 'execute (car ps))
10768               (setcdr (assq 'execute (car ps))
10769                       (funcall (if (string-match "%s" action)
10770                                    'format 'concat)
10771                                action
10772                                (mapconcat
10773                                 (lambda (f)
10774                                   (if (equal f " ")
10775                                       f
10776                                     (mm-quote-arg f)))
10777                                 files " ")))))
10778           (setq ps (cdr ps)))))
10779     (if (and gnus-view-pseudos (not not-view))
10780         (while pslist
10781           (when (assq 'execute (car pslist))
10782             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10783                                   (eq gnus-view-pseudos 'not-confirm)))
10784           (setq pslist (cdr pslist)))
10785       (save-excursion
10786         (while pslist
10787           (setq after-article (or (cdr (assq 'article (car pslist)))
10788                                   (gnus-summary-article-number)))
10789           (gnus-summary-goto-subject after-article)
10790           (forward-line 1)
10791           (setq b (point))
10792           (insert "    " (file-name-nondirectory
10793                           (cdr (assq 'name (car pslist))))
10794                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10795           (setq e (point))
10796           (forward-line -1)             ; back to `b'
10797           (gnus-add-text-properties
10798            b (1- e) (list 'gnus-number gnus-reffed-article-number
10799                           gnus-mouse-face-prop gnus-mouse-face))
10800           (gnus-data-enter
10801            after-article gnus-reffed-article-number
10802            gnus-unread-mark b (car pslist) 0 (- e b))
10803           (setq gnus-newsgroup-unreads
10804                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10805                                          gnus-reffed-article-number))
10806           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10807           (setq pslist (cdr pslist)))))))
10808
10809 (defun gnus-pseudos< (p1 p2)
10810   (let ((c1 (cdr (assq 'action p1)))
10811         (c2 (cdr (assq 'action p2))))
10812     (and c1 c2 (string< c1 c2))))
10813
10814 (defun gnus-request-pseudo-article (props)
10815   (cond ((assq 'execute props)
10816          (gnus-execute-command (cdr (assq 'execute props)))))
10817   (let ((gnus-current-article (gnus-summary-article-number)))
10818     (gnus-run-hooks 'gnus-mark-article-hook)))
10819
10820 (defun gnus-execute-command (command &optional automatic)
10821   (save-excursion
10822     (gnus-article-setup-buffer)
10823     (set-buffer gnus-article-buffer)
10824     (setq buffer-read-only nil)
10825     (let ((command (if automatic command
10826                      (read-string "Command: " (cons command 0)))))
10827       (erase-buffer)
10828       (insert "$ " command "\n\n")
10829       (if gnus-view-pseudo-asynchronously
10830           (start-process "gnus-execute" (current-buffer) shell-file-name
10831                          shell-command-switch command)
10832         (call-process shell-file-name nil t nil
10833                       shell-command-switch command)))))
10834
10835 ;; Summary kill commands.
10836
10837 (defun gnus-summary-edit-global-kill (article)
10838   "Edit the \"global\" kill file."
10839   (interactive (list (gnus-summary-article-number)))
10840   (gnus-group-edit-global-kill article))
10841
10842 (defun gnus-summary-edit-local-kill ()
10843   "Edit a local kill file applied to the current newsgroup."
10844   (interactive)
10845   (setq gnus-current-headers (gnus-summary-article-header))
10846   (gnus-group-edit-local-kill
10847    (gnus-summary-article-number) gnus-newsgroup-name))
10848
10849 ;;; Header reading.
10850
10851 (defun gnus-read-header (id &optional header)
10852   "Read the headers of article ID and enter them into the Gnus system."
10853   (let ((group gnus-newsgroup-name)
10854         (gnus-override-method
10855          (or
10856           gnus-override-method
10857           (and (gnus-news-group-p gnus-newsgroup-name)
10858                (car (gnus-refer-article-methods)))))
10859         where)
10860     ;; First we check to see whether the header in question is already
10861     ;; fetched.
10862     (if (stringp id)
10863         ;; This is a Message-ID.
10864         (setq header (or header (gnus-id-to-header id)))
10865       ;; This is an article number.
10866       (setq header (or header (gnus-summary-article-header id))))
10867     (if (and header
10868              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10869         ;; We have found the header.
10870         header
10871       ;; If this is a sparse article, we have to nix out its
10872       ;; previous entry in the thread hashtb.
10873       (when (and header
10874                  (gnus-summary-article-sparse-p (mail-header-number header)))
10875         (let* ((parent (gnus-parent-id (mail-header-references header)))
10876                (thread (and parent (gnus-id-to-thread parent))))
10877           (when thread
10878             (delq (assq header thread) thread))))
10879       ;; We have to really fetch the header to this article.
10880       (save-excursion
10881         (set-buffer nntp-server-buffer)
10882         (when (setq where (gnus-request-head id group))
10883           (nnheader-fold-continuation-lines)
10884           (goto-char (point-max))
10885           (insert ".\n")
10886           (goto-char (point-min))
10887           (insert "211 ")
10888           (princ (cond
10889                   ((numberp id) id)
10890                   ((cdr where) (cdr where))
10891                   (header (mail-header-number header))
10892                   (t gnus-reffed-article-number))
10893                  (current-buffer))
10894           (insert " Article retrieved.\n"))
10895         (if (or (not where)
10896                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10897             ()                          ; Malformed head.
10898           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10899             (when (and (stringp id)
10900                        (not (string= (gnus-group-real-name group)
10901                                      (car where))))
10902               ;; If we fetched by Message-ID and the article came
10903               ;; from a different group, we fudge some bogus article
10904               ;; numbers for this article.
10905               (mail-header-set-number header gnus-reffed-article-number))
10906             (save-excursion
10907               (set-buffer gnus-summary-buffer)
10908               (decf gnus-reffed-article-number)
10909               (gnus-remove-header (mail-header-number header))
10910               (push header gnus-newsgroup-headers)
10911               (setq gnus-current-headers header)
10912               (push (mail-header-number header) gnus-newsgroup-limit)))
10913           header)))))
10914
10915 (defun gnus-remove-header (number)
10916   "Remove header NUMBER from `gnus-newsgroup-headers'."
10917   (if (and gnus-newsgroup-headers
10918            (= number (mail-header-number (car gnus-newsgroup-headers))))
10919       (pop gnus-newsgroup-headers)
10920     (let ((headers gnus-newsgroup-headers))
10921       (while (and (cdr headers)
10922                   (not (= number (mail-header-number (cadr headers)))))
10923         (pop headers))
10924       (when (cdr headers)
10925         (setcdr headers (cddr headers))))))
10926
10927 ;;;
10928 ;;; summary highlights
10929 ;;;
10930
10931 (defun gnus-highlight-selected-summary ()
10932   "Highlight selected article in summary buffer."
10933   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10934   (when gnus-summary-selected-face
10935     (save-excursion
10936       (let* ((beg (progn (beginning-of-line) (point)))
10937              (end (progn (end-of-line) (point)))
10938              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10939              (from (if (get-text-property beg gnus-mouse-face-prop)
10940                        beg
10941                      (or (next-single-property-change
10942                           beg gnus-mouse-face-prop nil end)
10943                          beg)))
10944              (to
10945               (if (= from end)
10946                   (- from 2)
10947                 (or (next-single-property-change
10948                      from gnus-mouse-face-prop nil end)
10949                     end))))
10950         ;; If no mouse-face prop on line we will have to = from = end,
10951         ;; so we highlight the entire line instead.
10952         (when (= (+ to 2) from)
10953           (setq from beg)
10954           (setq to end))
10955         (if gnus-newsgroup-selected-overlay
10956             ;; Move old overlay.
10957             (gnus-move-overlay
10958              gnus-newsgroup-selected-overlay from to (current-buffer))
10959           ;; Create new overlay.
10960           (gnus-overlay-put
10961            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10962            'face gnus-summary-selected-face))))))
10963
10964 (defvar gnus-summary-highlight-line-cached nil)
10965 (defvar gnus-summary-highlight-line-trigger nil)
10966
10967 (defun gnus-summary-highlight-line-0 ()
10968   (if (and (eq gnus-summary-highlight-line-trigger 
10969                gnus-summary-highlight)
10970            gnus-summary-highlight-line-cached)
10971       gnus-summary-highlight-line-cached
10972     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10973           gnus-summary-highlight-line-cached
10974           (let* ((cond (list 'cond))
10975                  (c cond)
10976                  (list gnus-summary-highlight))
10977             (while list
10978               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10979               (setq c (cdr c)
10980                     list (cdr list)))
10981             (gnus-byte-compile (list 'lambda nil cond))))))
10982
10983 (defun gnus-summary-highlight-line ()
10984   "Highlight current line according to `gnus-summary-highlight'."
10985   (let* ((beg (gnus-point-at-bol))
10986          (article (or (gnus-summary-article-number) gnus-current-article))
10987          (score (or (cdr (assq article
10988                                gnus-newsgroup-scored))
10989                     gnus-summary-default-score 0))
10990          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10991          (inhibit-read-only t)
10992          (default gnus-summary-default-score)
10993          (default-high gnus-summary-default-high-score)
10994          (default-low gnus-summary-default-low-score)
10995          (uncached (memq article gnus-newsgroup-undownloaded))
10996          (downloaded (not uncached)))
10997     (let ((face (funcall (gnus-summary-highlight-line-0))))
10998       (unless (eq face (get-text-property beg 'face))
10999         (gnus-put-text-property-excluding-characters-with-faces
11000          beg (gnus-point-at-eol) 'face
11001          (setq face (if (boundp face) (symbol-value face) face)))
11002         (when gnus-summary-highlight-line-function
11003           (funcall gnus-summary-highlight-line-function article face))))))
11004
11005 (defun gnus-update-read-articles (group unread &optional compute)
11006   "Update the list of read articles in GROUP.
11007 UNREAD is a sorted list."
11008   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11009          (entry (gnus-gethash group gnus-newsrc-hashtb))
11010          (info (nth 2 entry))
11011          (prev 1)
11012          read)
11013     (if (or (not info) (not active))
11014         ;; There is no info on this group if it was, in fact,
11015         ;; killed.  Gnus stores no information on killed groups, so
11016         ;; there's nothing to be done.
11017         ;; One could store the information somewhere temporarily,
11018         ;; perhaps...  Hmmm...
11019         ()
11020       ;; Remove any negative articles numbers.
11021       (while (and unread (< (car unread) 0))
11022         (setq unread (cdr unread)))
11023       ;; Remove any expired article numbers
11024       (while (and unread (< (car unread) (car active)))
11025         (setq unread (cdr unread)))
11026       ;; Compute the ranges of read articles by looking at the list of
11027       ;; unread articles.
11028       (while unread
11029         (when (/= (car unread) prev)
11030           (push (if (= prev (1- (car unread))) prev
11031                   (cons prev (1- (car unread))))
11032                 read))
11033         (setq prev (1+ (car unread)))
11034         (setq unread (cdr unread)))
11035       (when (<= prev (cdr active))
11036         (push (cons prev (cdr active)) read))
11037       (setq read (if (> (length read) 1) (nreverse read) read))
11038       (if compute
11039           read
11040         (save-excursion
11041           (let (setmarkundo)
11042             ;; Propagate the read marks to the backend.
11043             (when (gnus-check-backend-function 'request-set-mark group)
11044               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11045                     (add (gnus-remove-from-range read (gnus-info-read info))))
11046                 (when (or add del)
11047                   (unless (gnus-check-group group)
11048                     (error "Can't open server for %s" group))
11049                   (gnus-request-set-mark
11050                    group (delq nil (list (if add (list add 'add '(read)))
11051                                          (if del (list del 'del '(read))))))
11052                   (setq setmarkundo
11053                         `(gnus-request-set-mark
11054                           ,group
11055                           ',(delq nil (list
11056                                        (if del (list del 'add '(read)))
11057                                        (if add (list add 'del '(read))))))))))
11058             (set-buffer gnus-group-buffer)
11059             (gnus-undo-register
11060               `(progn
11061                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11062                  (gnus-info-set-read ',info ',(gnus-info-read info))
11063                  (gnus-get-unread-articles-in-group ',info
11064                                                     (gnus-active ,group))
11065                  (gnus-group-update-group ,group t)
11066                  ,setmarkundo))))
11067         ;; Enter this list into the group info.
11068         (gnus-info-set-read info read)
11069         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11070         (gnus-get-unread-articles-in-group info (gnus-active group))
11071         t))))
11072
11073 (defun gnus-offer-save-summaries ()
11074   "Offer to save all active summary buffers."
11075   (let (buffers)
11076     ;; Go through all buffers and find all summaries.
11077     (dolist (buffer (buffer-list))
11078       (when (and (setq buffer (buffer-name buffer))
11079                  (string-match "Summary" buffer)
11080                  (save-excursion
11081                    (set-buffer buffer)
11082                    ;; We check that this is, indeed, a summary buffer.
11083                    (and (eq major-mode 'gnus-summary-mode)
11084                         ;; Also make sure this isn't bogus.
11085                         gnus-newsgroup-prepared
11086                         ;; Also make sure that this isn't a
11087                         ;; dead summary buffer.
11088                         (not gnus-dead-summary-mode))))
11089         (push buffer buffers)))
11090     ;; Go through all these summary buffers and offer to save them.
11091     (when buffers
11092       (save-excursion
11093         (map-y-or-n-p
11094          "Update summary buffer %s? "
11095          (lambda (buf)
11096            (switch-to-buffer buf)
11097            (gnus-summary-exit))
11098          buffers)))))
11099
11100 (defun gnus-summary-setup-default-charset ()
11101   "Setup newsgroup default charset."
11102   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11103       (setq gnus-newsgroup-charset nil)
11104     (let* ((ignored-charsets
11105             (or gnus-newsgroup-ephemeral-ignored-charsets
11106                 (append
11107                  (and gnus-newsgroup-name
11108                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11109                  gnus-newsgroup-ignored-charsets))))
11110       (setq gnus-newsgroup-charset
11111             (or gnus-newsgroup-ephemeral-charset
11112                 (and gnus-newsgroup-name
11113                      (gnus-parameter-charset gnus-newsgroup-name))
11114                 gnus-default-charset))
11115       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11116            ignored-charsets))))
11117
11118 ;;;
11119 ;;; Mime Commands
11120 ;;;
11121
11122 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11123   "Display the current article buffer fully MIME-buttonized.
11124 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11125 treated as multipart/mixed."
11126   (interactive "P")
11127   (require 'gnus-art)
11128   (let ((gnus-unbuttonized-mime-types nil)
11129         (gnus-mime-display-multipart-as-mixed show-all-parts))
11130     (gnus-summary-show-article)))
11131
11132 (defun gnus-summary-repair-multipart (article)
11133   "Add a Content-Type header to a multipart article without one."
11134   (interactive (list (gnus-summary-article-number)))
11135   (gnus-with-article article
11136     (message-narrow-to-head)
11137     (message-remove-header "Mime-Version")
11138     (goto-char (point-max))
11139     (insert "Mime-Version: 1.0\n")
11140     (widen)
11141     (when (search-forward "\n--" nil t)
11142       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11143         (message-narrow-to-head)
11144         (message-remove-header "Content-Type")
11145         (goto-char (point-max))
11146         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11147                         separator))
11148         (widen))))
11149   (let (gnus-mark-article-hook)
11150     (gnus-summary-select-article t t nil article)))
11151
11152 (defun gnus-summary-toggle-display-buttonized ()
11153   "Toggle the buttonizing of the article buffer."
11154   (interactive)
11155   (require 'gnus-art)
11156   (if (setq gnus-inhibit-mime-unbuttonizing
11157             (not gnus-inhibit-mime-unbuttonizing))
11158       (let ((gnus-unbuttonized-mime-types nil))
11159         (gnus-summary-show-article))
11160     (gnus-summary-show-article)))
11161
11162 ;;;
11163 ;;; Generic summary marking commands
11164 ;;;
11165
11166 (defvar gnus-summary-marking-alist
11167   '((read gnus-del-mark "d")
11168     (unread gnus-unread-mark "u")
11169     (ticked gnus-ticked-mark "!")
11170     (dormant gnus-dormant-mark "?")
11171     (expirable gnus-expirable-mark "e"))
11172   "An alist of names/marks/keystrokes.")
11173
11174 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11175 (defvar gnus-summary-mark-map)
11176
11177 (defun gnus-summary-make-all-marking-commands ()
11178   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11179   (dolist (elem gnus-summary-marking-alist)
11180     (apply 'gnus-summary-make-marking-command elem)))
11181
11182 (defun gnus-summary-make-marking-command (name mark keystroke)
11183   (let ((map (make-sparse-keymap)))
11184     (define-key gnus-summary-generic-mark-map keystroke map)
11185     (dolist (lway `((next "next" next nil "n")
11186                     (next-unread "next unread" next t "N")
11187                     (prev "previous" prev nil "p")
11188                     (prev-unread "previous unread" prev t "P")
11189                     (nomove "" nil nil ,keystroke)))
11190       (let ((func (gnus-summary-make-marking-command-1
11191                    mark (car lway) lway name)))
11192         (setq func (eval func))
11193         (define-key map (nth 4 lway) func)))))
11194
11195 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11196   `(defun ,(intern
11197             (format "gnus-summary-put-mark-as-%s%s"
11198                     name (if (eq way 'nomove)
11199                              ""
11200                            (concat "-" (symbol-name way)))))
11201      (n)
11202      ,(format
11203        "Mark the current article as %s%s.
11204 If N, the prefix, then repeat N times.
11205 If N is negative, move in reverse order.
11206 The difference between N and the actual number of articles marked is
11207 returned."
11208        name (cadr lway))
11209      (interactive "p")
11210      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11211
11212 (defun gnus-summary-generic-mark (n mark move unread)
11213   "Mark N articles with MARK."
11214   (unless (eq major-mode 'gnus-summary-mode)
11215     (error "This command can only be used in the summary buffer"))
11216   (gnus-summary-show-thread)
11217   (let ((nummove
11218          (cond
11219           ((eq move 'next) 1)
11220           ((eq move 'prev) -1)
11221           (t 0))))
11222     (if (zerop nummove)
11223         (setq n 1)
11224       (when (< n 0)
11225         (setq n (abs n)
11226               nummove (* -1 nummove))))
11227     (while (and (> n 0)
11228                 (gnus-summary-mark-article nil mark)
11229                 (zerop (gnus-summary-next-subject nummove unread t)))
11230       (setq n (1- n)))
11231     (when (/= 0 n)
11232       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11233     (gnus-summary-recenter)
11234     (gnus-summary-position-point)
11235     (gnus-set-mode-line 'summary)
11236     n))
11237
11238 (defun gnus-summary-insert-articles (articles)
11239   (when (setq articles
11240               (gnus-sorted-difference articles
11241                                       (mapcar (lambda (h)
11242                                                 (mail-header-number h))
11243                                               gnus-newsgroup-headers)))
11244     (setq gnus-newsgroup-headers
11245           (merge 'list
11246                  gnus-newsgroup-headers
11247                  (gnus-fetch-headers articles)
11248                  'gnus-article-sort-by-number))
11249     ;; Suppress duplicates?
11250     (when gnus-suppress-duplicates
11251       (gnus-dup-suppress-articles))
11252
11253     ;; We might want to build some more threads first.
11254     (when (and gnus-fetch-old-headers
11255                (eq gnus-headers-retrieved-by 'nov))
11256       (if (eq gnus-fetch-old-headers 'invisible)
11257           (gnus-build-all-threads)
11258         (gnus-build-old-threads)))
11259     ;; Let the Gnus agent mark articles as read.
11260     (when gnus-agent
11261       (gnus-agent-get-undownloaded-list))
11262     ;; Remove list identifiers from subject
11263     (when gnus-list-identifiers
11264       (gnus-summary-remove-list-identifiers))
11265     ;; First and last article in this newsgroup.
11266     (when gnus-newsgroup-headers
11267       (setq gnus-newsgroup-begin
11268             (mail-header-number (car gnus-newsgroup-headers))
11269             gnus-newsgroup-end
11270             (mail-header-number
11271              (gnus-last-element gnus-newsgroup-headers))))
11272     (when gnus-use-scoring
11273       (gnus-possibly-score-headers))))
11274
11275 (defun gnus-summary-insert-old-articles (&optional all)
11276   "Insert all old articles in this group.
11277 If ALL is non-nil, already read articles become readable.
11278 If ALL is a number, fetch this number of articles."
11279   (interactive "P")
11280   (prog1
11281       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11282             older len)
11283         (setq older
11284               ;; Some nntp servers lie about their active range.  When
11285               ;; this happens, the active range can be in the millions.
11286               ;; Use a compressed range to avoid creating a huge list.
11287               (gnus-range-difference (list gnus-newsgroup-active) old))
11288         (setq len (gnus-range-length older))
11289         (cond
11290          ((null older) nil)
11291          ((numberp all)
11292           (if (< all len)
11293               (let ((older-range (nreverse older)))
11294                 (setq older nil)
11295
11296                 (while (> all 0)
11297                   (let* ((r (pop older-range))
11298                          (min (if (numberp r) r (car r)))
11299                          (max (if (numberp r) r (cdr r))))
11300                     (while (and (<= min max)
11301                                 (> all 0))
11302                       (push max older)
11303                       (setq all (1- all)
11304                             max (1- max))))))
11305             (setq older (gnus-uncompress-range older))))
11306          (all
11307           (setq older (gnus-uncompress-range older)))
11308          (t
11309           (when (and (numberp gnus-large-newsgroup)
11310                    (> len gnus-large-newsgroup))
11311               (let* ((cursor-in-echo-area nil)
11312                      (initial (gnus-parameter-large-newsgroup-initial
11313                                gnus-newsgroup-name))
11314                      (input
11315                       (read-string
11316                        (format
11317                         "How many articles from %s (%s %d): "
11318                         (gnus-limit-string
11319                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11320                         (if initial "max" "default")
11321                         len)
11322                        (if initial
11323                            (cons (number-to-string initial)
11324                                  0)))))
11325                 (unless (string-match "^[ \t]*$" input)
11326                   (setq all (string-to-number input))
11327                   (if (< all len)
11328                       (let ((older-range (nreverse older)))
11329                         (setq older nil)
11330
11331                         (while (> all 0)
11332                           (let* ((r (pop older-range))
11333                                  (min (if (numberp r) r (car r)))
11334                                  (max (if (numberp r) r (cdr r))))
11335                             (while (and (<= min max)
11336                                         (> all 0))
11337                               (push max older)
11338                               (setq all (1- all)
11339                                     max (1- max))))))))))
11340           (setq older (gnus-uncompress-range older))))
11341         (if (not older)
11342             (message "No old news.")
11343           (gnus-summary-insert-articles older)
11344           (gnus-summary-limit (gnus-sorted-nunion old older))))
11345     (gnus-summary-position-point)))
11346
11347 (defun gnus-summary-insert-new-articles ()
11348   "Insert all new articles in this group."
11349   (interactive)
11350   (prog1
11351       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11352             (old-active gnus-newsgroup-active)
11353             (nnmail-fetched-sources (list t))
11354             i new)
11355         (setq gnus-newsgroup-active
11356               (gnus-activate-group gnus-newsgroup-name 'scan))
11357         (setq i (cdr gnus-newsgroup-active))
11358         (while (> i (cdr old-active))
11359           (push i new)
11360           (decf i))
11361         (if (not new)
11362             (message "No gnus is bad news.")
11363           (gnus-summary-insert-articles new)
11364           (setq gnus-newsgroup-unreads
11365                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11366           (gnus-summary-limit (gnus-sorted-nunion old new))))
11367     (gnus-summary-position-point)))
11368
11369 (gnus-summary-make-all-marking-commands)
11370
11371 (gnus-ems-redefine)
11372
11373 (provide 'gnus-sum)
11374
11375 (run-hooks 'gnus-sum-load-hook)
11376
11377 ;;; gnus-sum.el ends here