3a4bb23a5068453d67c12abcb7196becb065f6fd
[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-uncached-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-uncached-face)
868     (uncached
869      . gnus-summary-normal-uncached-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         ["Toggle threading" gnus-summary-toggle-threads t]
2186         ["Hide threads" gnus-summary-hide-all-threads t]
2187         ["Show threads" gnus-summary-show-all-threads t]
2188         ["Hide thread" gnus-summary-hide-thread t]
2189         ["Show thread" gnus-summary-show-thread t]
2190         ["Go to next thread" gnus-summary-next-thread t]
2191         ["Go to previous thread" gnus-summary-prev-thread t]
2192         ["Go down thread" gnus-summary-down-thread t]
2193         ["Go up thread" gnus-summary-up-thread t]
2194         ["Top of thread" gnus-summary-top-thread t]
2195         ["Mark thread as read" gnus-summary-kill-thread t]
2196         ["Lower thread score" gnus-summary-lower-thread t]
2197         ["Raise thread score" gnus-summary-raise-thread t]
2198         ["Rethread current" gnus-summary-rethread-current t]))
2199
2200     (easy-menu-define
2201       gnus-summary-post-menu gnus-summary-mode-map ""
2202       `("Post"
2203         ["Send a message (mail or news)" gnus-summary-post-news
2204          ,@(if (featurep 'xemacs) '(t)
2205              '(:help "Post an article"))]
2206         ["Followup" gnus-summary-followup
2207          ,@(if (featurep 'xemacs) '(t)
2208              '(:help "Post followup to this article"))]
2209         ["Followup and yank" gnus-summary-followup-with-original
2210          ,@(if (featurep 'xemacs) '(t)
2211              '(:help "Post followup to this article, quoting its contents"))]
2212         ["Supersede article" gnus-summary-supersede-article t]
2213         ["Cancel article" gnus-summary-cancel-article
2214          ,@(if (featurep 'xemacs) '(t)
2215              '(:help "Cancel an article you posted"))]
2216         ["Reply" gnus-summary-reply t]
2217         ["Reply and yank" gnus-summary-reply-with-original t]
2218         ["Wide reply" gnus-summary-wide-reply t]
2219         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2220          ,@(if (featurep 'xemacs) '(t)
2221              '(:help "Mail a reply, quoting this article"))]
2222         ["Very wide reply" gnus-summary-very-wide-reply t]
2223         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2224          ,@(if (featurep 'xemacs) '(t)
2225              '(:help "Mail a very wide reply, quoting this article"))]
2226         ["Mail forward" gnus-summary-mail-forward t]
2227         ["Post forward" gnus-summary-post-forward t]
2228         ["Digest and mail" gnus-uu-digest-mail-forward t]
2229         ["Digest and post" gnus-uu-digest-post-forward t]
2230         ["Resend message" gnus-summary-resend-message t]
2231         ["Resend message edit" gnus-summary-resend-message-edit t]
2232         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2233         ["Send a mail" gnus-summary-mail-other-window t]
2234         ["Create a local message" gnus-summary-news-other-window t]
2235         ["Uuencode and post" gnus-uu-post-news
2236          ,@(if (featurep 'xemacs) '(t)
2237              '(:help "Post a uuencoded article"))]
2238         ["Followup via news" gnus-summary-followup-to-mail t]
2239         ["Followup via news and yank"
2240          gnus-summary-followup-to-mail-with-original t]
2241         ;;("Draft"
2242         ;;["Send" gnus-summary-send-draft t]
2243         ;;["Send bounced" gnus-resend-bounced-mail t])
2244         ))
2245
2246     (cond
2247      ((not (keymapp gnus-summary-post-menu))
2248       (setq gnus-article-post-menu gnus-summary-post-menu))
2249      ((not gnus-article-post-menu)
2250       ;; Don't share post menu.
2251       (setq gnus-article-post-menu
2252             (copy-keymap gnus-summary-post-menu))))
2253     (define-key gnus-article-mode-map [menu-bar post]
2254       (cons "Post" gnus-article-post-menu))
2255
2256     (easy-menu-define
2257       gnus-summary-misc-menu gnus-summary-mode-map ""
2258       `("Gnus"
2259         ("Mark Read"
2260          ["Mark as read" gnus-summary-mark-as-read-forward t]
2261          ["Mark same subject and select"
2262           gnus-summary-kill-same-subject-and-select t]
2263          ["Mark same subject" gnus-summary-kill-same-subject t]
2264          ["Catchup" gnus-summary-catchup
2265           ,@(if (featurep 'xemacs) '(t)
2266               '(:help "Mark unread articles in this group as read"))]
2267          ["Catchup all" gnus-summary-catchup-all t]
2268          ["Catchup to here" gnus-summary-catchup-to-here t]
2269          ["Catchup from here" gnus-summary-catchup-from-here t]
2270          ["Catchup region" gnus-summary-mark-region-as-read t]
2271          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2272         ("Mark Various"
2273          ["Tick" gnus-summary-tick-article-forward t]
2274          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2275          ["Remove marks" gnus-summary-clear-mark-forward t]
2276          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2277          ["Set bookmark" gnus-summary-set-bookmark t]
2278          ["Remove bookmark" gnus-summary-remove-bookmark t])
2279         ("Limit to"
2280          ["Marks..." gnus-summary-limit-to-marks t]
2281          ["Subject..." gnus-summary-limit-to-subject t]
2282          ["Author..." gnus-summary-limit-to-author t]
2283          ["Age..." gnus-summary-limit-to-age t]
2284          ["Extra..." gnus-summary-limit-to-extra t]
2285          ["Score..." gnus-summary-limit-to-score t]
2286          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2287          ["Unread" gnus-summary-limit-to-unread t]
2288          ["Unseen" gnus-summary-limit-to-unseen t]
2289          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2290          ["Next articles" gnus-summary-limit-to-articles t]
2291          ["Pop limit" gnus-summary-pop-limit t]
2292          ["Show dormant" gnus-summary-limit-include-dormant t]
2293          ["Hide childless dormant"
2294           gnus-summary-limit-exclude-childless-dormant t]
2295          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2296          ["Hide marked" gnus-summary-limit-exclude-marks t]
2297          ["Show expunged" gnus-summary-limit-include-expunged t])
2298         ("Process Mark"
2299          ["Set mark" gnus-summary-mark-as-processable t]
2300          ["Remove mark" gnus-summary-unmark-as-processable t]
2301          ["Remove all marks" gnus-summary-unmark-all-processable t]
2302          ["Mark above" gnus-uu-mark-over t]
2303          ["Mark series" gnus-uu-mark-series t]
2304          ["Mark region" gnus-uu-mark-region t]
2305          ["Unmark region" gnus-uu-unmark-region t]
2306          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2307          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2308          ["Mark all" gnus-uu-mark-all t]
2309          ["Mark buffer" gnus-uu-mark-buffer t]
2310          ["Mark sparse" gnus-uu-mark-sparse t]
2311          ["Mark thread" gnus-uu-mark-thread t]
2312          ["Unmark thread" gnus-uu-unmark-thread t]
2313          ("Process Mark Sets"
2314           ["Kill" gnus-summary-kill-process-mark t]
2315           ["Yank" gnus-summary-yank-process-mark
2316            gnus-newsgroup-process-stack]
2317           ["Save" gnus-summary-save-process-mark t]))
2318         ("Scroll article"
2319          ["Page forward" gnus-summary-next-page
2320           ,@(if (featurep 'xemacs) '(t)
2321               '(:help "Show next page of article"))]
2322          ["Page backward" gnus-summary-prev-page
2323           ,@(if (featurep 'xemacs) '(t)
2324               '(:help "Show previous page of article"))]
2325          ["Line forward" gnus-summary-scroll-up t])
2326         ("Move"
2327          ["Next unread article" gnus-summary-next-unread-article t]
2328          ["Previous unread article" gnus-summary-prev-unread-article t]
2329          ["Next article" gnus-summary-next-article t]
2330          ["Previous article" gnus-summary-prev-article t]
2331          ["Next unread subject" gnus-summary-next-unread-subject t]
2332          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2333          ["Next article same subject" gnus-summary-next-same-subject t]
2334          ["Previous article same subject" gnus-summary-prev-same-subject t]
2335          ["First unread article" gnus-summary-first-unread-article t]
2336          ["Best unread article" gnus-summary-best-unread-article t]
2337          ["Go to subject number..." gnus-summary-goto-subject t]
2338          ["Go to article number..." gnus-summary-goto-article t]
2339          ["Go to the last article" gnus-summary-goto-last-article t]
2340          ["Pop article off history" gnus-summary-pop-article t])
2341         ("Sort"
2342          ["Sort by number" gnus-summary-sort-by-number t]
2343          ["Sort by author" gnus-summary-sort-by-author t]
2344          ["Sort by subject" gnus-summary-sort-by-subject t]
2345          ["Sort by date" gnus-summary-sort-by-date t]
2346          ["Sort by score" gnus-summary-sort-by-score t]
2347          ["Sort by lines" gnus-summary-sort-by-lines t]
2348          ["Sort by characters" gnus-summary-sort-by-chars t]
2349          ["Randomize" gnus-summary-sort-by-random t]
2350          ["Original sort" gnus-summary-sort-by-original t])
2351         ("Help"
2352          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2353          ["Describe group" gnus-summary-describe-group t]
2354          ["Fetch charter" gnus-group-fetch-charter
2355           ,@(if (featurep 'xemacs) nil
2356               '(:help "Display the charter of the current group"))]
2357          ["Fetch control message" gnus-group-fetch-control
2358           ,@(if (featurep 'xemacs) nil
2359               '(:help "Display the archived control message for the current group"))]
2360          ["Read manual" gnus-info-find-node t])
2361         ("Modes"
2362          ["Pick and read" gnus-pick-mode t]
2363          ["Binary" gnus-binary-mode t])
2364         ("Regeneration"
2365          ["Regenerate" gnus-summary-prepare t]
2366          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2367          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2368          ["Toggle threading" gnus-summary-toggle-threads t])
2369         ["See old articles" gnus-summary-insert-old-articles t]
2370         ["See new articles" gnus-summary-insert-new-articles t]
2371         ["Filter articles..." gnus-summary-execute-command t]
2372         ["Run command on subjects..." gnus-summary-universal-argument t]
2373         ["Search articles forward..." gnus-summary-search-article-forward t]
2374         ["Search articles backward..." gnus-summary-search-article-backward t]
2375         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2376         ["Expand window" gnus-summary-expand-window t]
2377         ["Expire expirable articles" gnus-summary-expire-articles
2378          (gnus-check-backend-function
2379           'request-expire-articles gnus-newsgroup-name)]
2380         ["Edit local kill file" gnus-summary-edit-local-kill t]
2381         ["Edit main kill file" gnus-summary-edit-global-kill t]
2382         ["Edit group parameters" gnus-summary-edit-parameters t]
2383         ["Customize group parameters" gnus-summary-customize-parameters t]
2384         ["Send a bug report" gnus-bug t]
2385         ("Exit"
2386          ["Catchup and exit" gnus-summary-catchup-and-exit
2387           ,@(if (featurep 'xemacs) '(t)
2388               '(:help "Mark unread articles in this group as read, then exit"))]
2389          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2390          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2391          ["Exit group" gnus-summary-exit
2392           ,@(if (featurep 'xemacs) '(t)
2393               '(:help "Exit current group, return to group selection mode"))]
2394          ["Exit group without updating" gnus-summary-exit-no-update t]
2395          ["Exit and goto next group" gnus-summary-next-group t]
2396          ["Exit and goto prev group" gnus-summary-prev-group t]
2397          ["Reselect group" gnus-summary-reselect-current-group t]
2398          ["Rescan group" gnus-summary-rescan-group t]
2399          ["Update dribble" gnus-summary-save-newsrc t])))
2400
2401     (gnus-run-hooks 'gnus-summary-menu-hook)))
2402
2403 (defvar gnus-summary-tool-bar-map nil)
2404
2405 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2406 (defun gnus-summary-make-tool-bar ()
2407   (if (and (fboundp 'tool-bar-add-item-from-menu)
2408            (default-value 'tool-bar-mode)
2409            (not gnus-summary-tool-bar-map))
2410       (setq gnus-summary-tool-bar-map
2411             (let ((tool-bar-map (make-sparse-keymap))
2412                   (load-path (mm-image-load-path)))
2413               (tool-bar-add-item-from-menu
2414                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-summary-post-news "post" gnus-summary-mode-map)
2419               (tool-bar-add-item-from-menu
2420                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2421               (tool-bar-add-item-from-menu
2422                'gnus-summary-followup "followup" gnus-summary-mode-map)
2423               (tool-bar-add-item-from-menu
2424                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2425               (tool-bar-add-item-from-menu
2426                'gnus-summary-reply "reply" gnus-summary-mode-map)
2427               (tool-bar-add-item-from-menu
2428                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2429               (tool-bar-add-item-from-menu
2430                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2431               (tool-bar-add-item-from-menu
2432                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2433               (tool-bar-add-item-from-menu
2434                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2435               (tool-bar-add-item-from-menu
2436                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2437               (tool-bar-add-item-from-menu
2438                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2439               (tool-bar-add-item-from-menu
2440                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2443               tool-bar-map)))
2444   (if gnus-summary-tool-bar-map
2445       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2446
2447 (defun gnus-score-set-default (var value)
2448   "A version of set that updates the GNU Emacs menu-bar."
2449   (set var value)
2450   ;; It is the message that forces the active status to be updated.
2451   (message ""))
2452
2453 (defun gnus-make-score-map (type)
2454   "Make a summary score map of type TYPE."
2455   (if t
2456       nil
2457     (let ((headers '(("author" "from" string)
2458                      ("subject" "subject" string)
2459                      ("article body" "body" string)
2460                      ("article head" "head" string)
2461                      ("xref" "xref" string)
2462                      ("extra header" "extra" string)
2463                      ("lines" "lines" number)
2464                      ("followups to author" "followup" string)))
2465           (types '((number ("less than" <)
2466                            ("greater than" >)
2467                            ("equal" =))
2468                    (string ("substring" s)
2469                            ("exact string" e)
2470                            ("fuzzy string" f)
2471                            ("regexp" r))))
2472           (perms '(("temporary" (current-time-string))
2473                    ("permanent" nil)
2474                    ("immediate" now)))
2475           header)
2476       (list
2477        (apply
2478         'nconc
2479         (list
2480          (if (eq type 'lower)
2481              "Lower score"
2482            "Increase score"))
2483         (let (outh)
2484           (while headers
2485             (setq header (car headers))
2486             (setq outh
2487                   (cons
2488                    (apply
2489                     'nconc
2490                     (list (car header))
2491                     (let ((ts (cdr (assoc (nth 2 header) types)))
2492                           outt)
2493                       (while ts
2494                         (setq outt
2495                               (cons
2496                                (apply
2497                                 'nconc
2498                                 (list (caar ts))
2499                                 (let ((ps perms)
2500                                       outp)
2501                                   (while ps
2502                                     (setq outp
2503                                           (cons
2504                                            (vector
2505                                             (caar ps)
2506                                             (list
2507                                              'gnus-summary-score-entry
2508                                              (nth 1 header)
2509                                              (if (or (string= (nth 1 header)
2510                                                               "head")
2511                                                      (string= (nth 1 header)
2512                                                               "body"))
2513                                                  ""
2514                                                (list 'gnus-summary-header
2515                                                      (nth 1 header)))
2516                                              (list 'quote (nth 1 (car ts)))
2517                                              (list 'gnus-score-delta-default
2518                                                    nil)
2519                                              (nth 1 (car ps))
2520                                              t)
2521                                             t)
2522                                            outp))
2523                                     (setq ps (cdr ps)))
2524                                   (list (nreverse outp))))
2525                                outt))
2526                         (setq ts (cdr ts)))
2527                       (list (nreverse outt))))
2528                    outh))
2529             (setq headers (cdr headers)))
2530           (list (nreverse outh))))))))
2531
2532 \f
2533
2534 (defun gnus-summary-mode (&optional group)
2535   "Major mode for reading articles.
2536
2537 All normal editing commands are switched off.
2538 \\<gnus-summary-mode-map>
2539 Each line in this buffer represents one article.  To read an
2540 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2541 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2542 respectively.
2543
2544 You can also post articles and send mail from this buffer.  To
2545 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2546 of an article, type `\\[gnus-summary-reply]'.
2547
2548 There are approx. one gazillion commands you can execute in this
2549 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2550
2551 The following commands are available:
2552
2553 \\{gnus-summary-mode-map}"
2554   (interactive)
2555   (kill-all-local-variables)
2556   (when (gnus-visual-p 'summary-menu 'menu)
2557     (gnus-summary-make-menu-bar)
2558     (gnus-summary-make-tool-bar))
2559   (gnus-summary-make-local-variables)
2560   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2561     (gnus-summary-make-local-variables))
2562   (gnus-make-thread-indent-array)
2563   (gnus-simplify-mode-line)
2564   (setq major-mode 'gnus-summary-mode)
2565   (setq mode-name "Summary")
2566   (make-local-variable 'minor-mode-alist)
2567   (use-local-map gnus-summary-mode-map)
2568   (buffer-disable-undo)
2569   (setq buffer-read-only t)             ;Disable modification
2570   (setq truncate-lines t)
2571   (setq selective-display t)
2572   (setq selective-display-ellipses t)   ;Display `...'
2573   (gnus-summary-set-display-table)
2574   (gnus-set-default-directory)
2575   (setq gnus-newsgroup-name group)
2576   (make-local-variable 'gnus-summary-line-format)
2577   (make-local-variable 'gnus-summary-line-format-spec)
2578   (make-local-variable 'gnus-summary-dummy-line-format)
2579   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2580   (make-local-variable 'gnus-summary-mark-positions)
2581   (make-local-hook 'pre-command-hook)
2582   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2583   (gnus-run-hooks 'gnus-summary-mode-hook)
2584   (turn-on-gnus-mailing-list-mode)
2585   (mm-enable-multibyte)
2586   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2587   (gnus-update-summary-mark-positions))
2588
2589 (defun gnus-summary-make-local-variables ()
2590   "Make all the local summary buffer variables."
2591   (let (global)
2592     (dolist (local gnus-summary-local-variables)
2593       (if (consp local)
2594           (progn
2595             (if (eq (cdr local) 'global)
2596                 ;; Copy the global value of the variable.
2597                 (setq global (symbol-value (car local)))
2598               ;; Use the value from the list.
2599               (setq global (eval (cdr local))))
2600             (set (make-local-variable (car local)) global))
2601         ;; Simple nil-valued local variable.
2602         (set (make-local-variable local) nil)))))
2603
2604 (defun gnus-summary-clear-local-variables ()
2605   (let ((locals gnus-summary-local-variables))
2606     (while locals
2607       (if (consp (car locals))
2608           (and (vectorp (caar locals))
2609                (set (caar locals) nil))
2610         (and (vectorp (car locals))
2611              (set (car locals) nil)))
2612       (setq locals (cdr locals)))))
2613
2614 ;; Summary data functions.
2615
2616 (defmacro gnus-data-number (data)
2617   `(car ,data))
2618
2619 (defmacro gnus-data-set-number (data number)
2620   `(setcar ,data ,number))
2621
2622 (defmacro gnus-data-mark (data)
2623   `(nth 1 ,data))
2624
2625 (defmacro gnus-data-set-mark (data mark)
2626   `(setcar (nthcdr 1 ,data) ,mark))
2627
2628 (defmacro gnus-data-pos (data)
2629   `(nth 2 ,data))
2630
2631 (defmacro gnus-data-set-pos (data pos)
2632   `(setcar (nthcdr 2 ,data) ,pos))
2633
2634 (defmacro gnus-data-header (data)
2635   `(nth 3 ,data))
2636
2637 (defmacro gnus-data-set-header (data header)
2638   `(setf (nth 3 ,data) ,header))
2639
2640 (defmacro gnus-data-level (data)
2641   `(nth 4 ,data))
2642
2643 (defmacro gnus-data-unread-p (data)
2644   `(= (nth 1 ,data) gnus-unread-mark))
2645
2646 (defmacro gnus-data-read-p (data)
2647   `(/= (nth 1 ,data) gnus-unread-mark))
2648
2649 (defmacro gnus-data-pseudo-p (data)
2650   `(consp (nth 3 ,data)))
2651
2652 (defmacro gnus-data-find (number)
2653   `(assq ,number gnus-newsgroup-data))
2654
2655 (defmacro gnus-data-find-list (number &optional data)
2656   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2657      (memq (assq ,number bdata)
2658            bdata)))
2659
2660 (defmacro gnus-data-make (number mark pos header level)
2661   `(list ,number ,mark ,pos ,header ,level))
2662
2663 (defun gnus-data-enter (after-article number mark pos header level offset)
2664   (let ((data (gnus-data-find-list after-article)))
2665     (unless data
2666       (error "No such article: %d" after-article))
2667     (setcdr data (cons (gnus-data-make number mark pos header level)
2668                        (cdr data)))
2669     (setq gnus-newsgroup-data-reverse nil)
2670     (gnus-data-update-list (cddr data) offset)))
2671
2672 (defun gnus-data-enter-list (after-article list &optional offset)
2673   (when list
2674     (let ((data (and after-article (gnus-data-find-list after-article)))
2675           (ilist list))
2676       (if (not (or data
2677                    after-article))
2678           (let ((odata gnus-newsgroup-data))
2679             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2680             (when offset
2681               (gnus-data-update-list odata offset)))
2682       ;; Find the last element in the list to be spliced into the main
2683         ;; list.
2684         (while (cdr list)
2685           (setq list (cdr list)))
2686         (if (not data)
2687             (progn
2688               (setcdr list gnus-newsgroup-data)
2689               (setq gnus-newsgroup-data ilist)
2690               (when offset
2691                 (gnus-data-update-list (cdr list) offset)))
2692           (setcdr list (cdr data))
2693           (setcdr data ilist)
2694           (when offset
2695             (gnus-data-update-list (cdr list) offset))))
2696       (setq gnus-newsgroup-data-reverse nil))))
2697
2698 (defun gnus-data-remove (article &optional offset)
2699   (let ((data gnus-newsgroup-data))
2700     (if (= (gnus-data-number (car data)) article)
2701         (progn
2702           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2703                 gnus-newsgroup-data-reverse nil)
2704           (when offset
2705             (gnus-data-update-list gnus-newsgroup-data offset)))
2706       (while (cdr data)
2707         (when (= (gnus-data-number (cadr data)) article)
2708           (setcdr data (cddr data))
2709           (when offset
2710             (gnus-data-update-list (cdr data) offset))
2711           (setq data nil
2712                 gnus-newsgroup-data-reverse nil))
2713         (setq data (cdr data))))))
2714
2715 (defmacro gnus-data-list (backward)
2716   `(if ,backward
2717        (or gnus-newsgroup-data-reverse
2718            (setq gnus-newsgroup-data-reverse
2719                  (reverse gnus-newsgroup-data)))
2720      gnus-newsgroup-data))
2721
2722 (defun gnus-data-update-list (data offset)
2723   "Add OFFSET to the POS of all data entries in DATA."
2724   (setq gnus-newsgroup-data-reverse nil)
2725   (while data
2726     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2727     (setq data (cdr data))))
2728
2729 (defun gnus-summary-article-pseudo-p (article)
2730   "Say whether this article is a pseudo article or not."
2731   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2732
2733 (defmacro gnus-summary-article-sparse-p (article)
2734   "Say whether this article is a sparse article or not."
2735   `(memq ,article gnus-newsgroup-sparse))
2736
2737 (defmacro gnus-summary-article-ancient-p (article)
2738   "Say whether this article is a sparse article or not."
2739   `(memq ,article gnus-newsgroup-ancient))
2740
2741 (defun gnus-article-parent-p (number)
2742   "Say whether this article is a parent or not."
2743   (let ((data (gnus-data-find-list number)))
2744     (and (cdr data)              ; There has to be an article after...
2745          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2746             (gnus-data-level (nth 1 data))))))
2747
2748 (defun gnus-article-children (number)
2749   "Return a list of all children to NUMBER."
2750   (let* ((data (gnus-data-find-list number))
2751          (level (gnus-data-level (car data)))
2752          children)
2753     (setq data (cdr data))
2754     (while (and data
2755                 (= (gnus-data-level (car data)) (1+ level)))
2756       (push (gnus-data-number (car data)) children)
2757       (setq data (cdr data)))
2758     children))
2759
2760 (defmacro gnus-summary-skip-intangible ()
2761   "If the current article is intangible, then jump to a different article."
2762   '(let ((to (get-text-property (point) 'gnus-intangible)))
2763      (and to (gnus-summary-goto-subject to))))
2764
2765 (defmacro gnus-summary-article-intangible-p ()
2766   "Say whether this article is intangible or not."
2767   '(get-text-property (point) 'gnus-intangible))
2768
2769 (defun gnus-article-read-p (article)
2770   "Say whether ARTICLE is read or not."
2771   (not (or (memq article gnus-newsgroup-marked)
2772            (memq article gnus-newsgroup-spam-marked)
2773            (memq article gnus-newsgroup-unreads)
2774            (memq article gnus-newsgroup-unselected)
2775            (memq article gnus-newsgroup-dormant))))
2776
2777 ;; Some summary mode macros.
2778
2779 (defmacro gnus-summary-article-number ()
2780   "The article number of the article on the current line.
2781 If there isn't an article number here, then we return the current
2782 article number."
2783   '(progn
2784      (gnus-summary-skip-intangible)
2785      (or (get-text-property (point) 'gnus-number)
2786          (gnus-summary-last-subject))))
2787
2788 (defmacro gnus-summary-article-header (&optional number)
2789   "Return the header of article NUMBER."
2790   `(gnus-data-header (gnus-data-find
2791                       ,(or number '(gnus-summary-article-number)))))
2792
2793 (defmacro gnus-summary-thread-level (&optional number)
2794   "Return the level of thread that starts with article NUMBER."
2795   `(if (and (eq gnus-summary-make-false-root 'dummy)
2796             (get-text-property (point) 'gnus-intangible))
2797        0
2798      (gnus-data-level (gnus-data-find
2799                        ,(or number '(gnus-summary-article-number))))))
2800
2801 (defmacro gnus-summary-article-mark (&optional number)
2802   "Return the mark of article NUMBER."
2803   `(gnus-data-mark (gnus-data-find
2804                     ,(or number '(gnus-summary-article-number)))))
2805
2806 (defmacro gnus-summary-article-pos (&optional number)
2807   "Return the position of the line of article NUMBER."
2808   `(gnus-data-pos (gnus-data-find
2809                    ,(or number '(gnus-summary-article-number)))))
2810
2811 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2812 (defmacro gnus-summary-article-subject (&optional number)
2813   "Return current subject string or nil if nothing."
2814   `(let ((headers
2815           ,(if number
2816                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2817              '(gnus-data-header (assq (gnus-summary-article-number)
2818                                       gnus-newsgroup-data)))))
2819      (and headers
2820           (vectorp headers)
2821           (mail-header-subject headers))))
2822
2823 (defmacro gnus-summary-article-score (&optional number)
2824   "Return current article score."
2825   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2826                   gnus-newsgroup-scored))
2827        gnus-summary-default-score 0))
2828
2829 (defun gnus-summary-article-children (&optional number)
2830   "Return a list of article numbers that are children of article NUMBER."
2831   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2832          (level (gnus-data-level (car data)))
2833          l children)
2834     (while (and (setq data (cdr data))
2835                 (> (setq l (gnus-data-level (car data))) level))
2836       (and (= (1+ level) l)
2837            (push (gnus-data-number (car data))
2838                  children)))
2839     (nreverse children)))
2840
2841 (defun gnus-summary-article-parent (&optional number)
2842   "Return the article number of the parent of article NUMBER."
2843   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2844                                     (gnus-data-list t)))
2845          (level (gnus-data-level (car data))))
2846     (if (zerop level)
2847         ()                              ; This is a root.
2848       ;; We search until we find an article with a level less than
2849       ;; this one.  That function has to be the parent.
2850       (while (and (setq data (cdr data))
2851                   (not (< (gnus-data-level (car data)) level))))
2852       (and data (gnus-data-number (car data))))))
2853
2854 (defun gnus-unread-mark-p (mark)
2855   "Say whether MARK is the unread mark."
2856   (= mark gnus-unread-mark))
2857
2858 (defun gnus-read-mark-p (mark)
2859   "Say whether MARK is one of the marks that mark as read.
2860 This is all marks except unread, ticked, dormant, and expirable."
2861   (not (or (= mark gnus-unread-mark)
2862            (= mark gnus-ticked-mark)
2863            (= mark gnus-spam-mark)
2864            (= mark gnus-dormant-mark)
2865            (= mark gnus-expirable-mark))))
2866
2867 (defmacro gnus-article-mark (number)
2868   "Return the MARK of article NUMBER.
2869 This macro should only be used when computing the mark the \"first\"
2870 time; i.e., when generating the summary lines.  After that,
2871 `gnus-summary-article-mark' should be used to examine the
2872 marks of articles."
2873   `(cond
2874     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2875     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2876     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2877     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2878     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2879     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2880     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2881     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2882            gnus-ancient-mark))))
2883
2884 ;; Saving hidden threads.
2885
2886 (defmacro gnus-save-hidden-threads (&rest forms)
2887   "Save hidden threads, eval FORMS, and restore the hidden threads."
2888   (let ((config (make-symbol "config")))
2889     `(let ((,config (gnus-hidden-threads-configuration)))
2890        (unwind-protect
2891            (save-excursion
2892              ,@forms)
2893          (gnus-restore-hidden-threads-configuration ,config)))))
2894 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2895 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2896
2897 (defun gnus-data-compute-positions ()
2898   "Compute the positions of all articles."
2899   (setq gnus-newsgroup-data-reverse nil)
2900   (let ((data gnus-newsgroup-data))
2901     (save-excursion
2902       (gnus-save-hidden-threads
2903         (gnus-summary-show-all-threads)
2904         (goto-char (point-min))
2905         (while data
2906           (while (get-text-property (point) 'gnus-intangible)
2907             (forward-line 1))
2908           (gnus-data-set-pos (car data) (+ (point) 3))
2909           (setq data (cdr data))
2910           (forward-line 1))))))
2911
2912 (defun gnus-hidden-threads-configuration ()
2913   "Return the current hidden threads configuration."
2914   (save-excursion
2915     (let (config)
2916       (goto-char (point-min))
2917       (while (search-forward "\r" nil t)
2918         (push (1- (point)) config))
2919       config)))
2920
2921 (defun gnus-restore-hidden-threads-configuration (config)
2922   "Restore hidden threads configuration from CONFIG."
2923   (save-excursion
2924     (let (point buffer-read-only)
2925       (while (setq point (pop config))
2926         (when (and (< point (point-max))
2927                    (goto-char point)
2928                    (eq (char-after) ?\n))
2929           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2930
2931 ;; Various summary mode internalish functions.
2932
2933 (defun gnus-mouse-pick-article (e)
2934   (interactive "e")
2935   (mouse-set-point e)
2936   (gnus-summary-next-page nil t))
2937
2938 (defun gnus-summary-set-display-table ()
2939   "Change the display table.
2940 Odd characters have a tendency to mess
2941 up nicely formatted displays - we make all possible glyphs
2942 display only a single character."
2943
2944   ;; We start from the standard display table, if any.
2945   (let ((table (or (copy-sequence standard-display-table)
2946                    (make-display-table)))
2947         (i 32))
2948     ;; Nix out all the control chars...
2949     (while (>= (setq i (1- i)) 0)
2950       (aset table i [??]))
2951    ;; ... but not newline and cr, of course.  (cr is necessary for the
2952     ;; selective display).
2953     (aset table ?\n nil)
2954     (aset table ?\r nil)
2955     ;; We keep TAB as well.
2956     (aset table ?\t nil)
2957     ;; We nix out any glyphs over 126 that are not set already.
2958     (let ((i 256))
2959       (while (>= (setq i (1- i)) 127)
2960         ;; Only modify if the entry is nil.
2961         (unless (aref table i)
2962           (aset table i [??]))))
2963     (setq buffer-display-table table)))
2964
2965 (defun gnus-summary-set-article-display-arrow (pos)
2966   "Update the overlay arrow to point to line at position POS."
2967   (when (and gnus-summary-display-arrow
2968              (boundp 'overlay-arrow-position)
2969              (boundp 'overlay-arrow-string))
2970     (save-excursion
2971       (goto-char pos)
2972       (beginning-of-line)
2973       (unless overlay-arrow-position
2974         (setq overlay-arrow-position (make-marker)))
2975       (setq overlay-arrow-string "=>"
2976             overlay-arrow-position (set-marker overlay-arrow-position
2977                                                (point)
2978                                                (current-buffer))))))
2979
2980 (defun gnus-summary-buffer-name (group)
2981   "Return the summary buffer name of GROUP."
2982   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2983
2984 (defun gnus-summary-setup-buffer (group)
2985   "Initialize summary buffer."
2986   (let ((buffer (gnus-summary-buffer-name group))
2987         (dead-name (concat "*Dead Summary "
2988                            (gnus-group-decoded-name group) "*")))
2989     ;; If a dead summary buffer exists, we kill it.
2990     (when (gnus-buffer-live-p dead-name)
2991       (gnus-kill-buffer dead-name))
2992     (if (get-buffer buffer)
2993         (progn
2994           (set-buffer buffer)
2995           (setq gnus-summary-buffer (current-buffer))
2996           (not gnus-newsgroup-prepared))
2997       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2998       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2999       (gnus-summary-mode group)
3000       (when gnus-carpal
3001         (gnus-carpal-setup-buffer 'summary))
3002       (unless gnus-single-article-buffer
3003         (make-local-variable 'gnus-article-buffer)
3004         (make-local-variable 'gnus-article-current)
3005         (make-local-variable 'gnus-original-article-buffer))
3006       (setq gnus-newsgroup-name group)
3007       ;; Set any local variables in the group parameters.
3008       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3009       t)))
3010
3011 (defun gnus-set-global-variables ()
3012   "Set the global equivalents of the buffer-local variables.
3013 They are set to the latest values they had.  These reflect the summary
3014 buffer that was in action when the last article was fetched."
3015   (when (eq major-mode 'gnus-summary-mode)
3016     (setq gnus-summary-buffer (current-buffer))
3017     (let ((name gnus-newsgroup-name)
3018           (marked gnus-newsgroup-marked)
3019           (spam gnus-newsgroup-spam-marked)
3020           (unread gnus-newsgroup-unreads)
3021           (headers gnus-current-headers)
3022           (data gnus-newsgroup-data)
3023           (summary gnus-summary-buffer)
3024           (article-buffer gnus-article-buffer)
3025           (original gnus-original-article-buffer)
3026           (gac gnus-article-current)
3027           (reffed gnus-reffed-article-number)
3028           (score-file gnus-current-score-file)
3029           (default-charset gnus-newsgroup-charset)
3030           vlist)
3031       (let ((locals gnus-newsgroup-variables))
3032         (while locals
3033           (if (consp (car locals))
3034               (push (eval (caar locals)) vlist)
3035             (push (eval (car locals)) vlist))
3036           (setq locals (cdr locals)))
3037         (setq vlist (nreverse vlist)))
3038       (save-excursion
3039         (set-buffer gnus-group-buffer)
3040         (setq gnus-newsgroup-name name
3041               gnus-newsgroup-marked marked
3042               gnus-newsgroup-spam-marked spam
3043               gnus-newsgroup-unreads unread
3044               gnus-current-headers headers
3045               gnus-newsgroup-data data
3046               gnus-article-current gac
3047               gnus-summary-buffer summary
3048               gnus-article-buffer article-buffer
3049               gnus-original-article-buffer original
3050               gnus-reffed-article-number reffed
3051               gnus-current-score-file score-file
3052               gnus-newsgroup-charset default-charset)
3053         (let ((locals gnus-newsgroup-variables))
3054           (while locals
3055             (if (consp (car locals))
3056                 (set (caar locals) (pop vlist))
3057               (set (car locals) (pop vlist)))
3058             (setq locals (cdr locals))))
3059         ;; The article buffer also has local variables.
3060         (when (gnus-buffer-live-p gnus-article-buffer)
3061           (set-buffer gnus-article-buffer)
3062           (setq gnus-summary-buffer summary))))))
3063
3064 (defun gnus-summary-article-unread-p (article)
3065   "Say whether ARTICLE is unread or not."
3066   (memq article gnus-newsgroup-unreads))
3067
3068 (defun gnus-summary-first-article-p (&optional article)
3069   "Return whether ARTICLE is the first article in the buffer."
3070   (if (not (setq article (or article (gnus-summary-article-number))))
3071       nil
3072     (eq article (caar gnus-newsgroup-data))))
3073
3074 (defun gnus-summary-last-article-p (&optional article)
3075   "Return whether ARTICLE is the last article in the buffer."
3076   (if (not (setq article (or article (gnus-summary-article-number))))
3077       ;; All non-existent numbers are the last article.  :-)
3078       t
3079     (not (cdr (gnus-data-find-list article)))))
3080
3081 (defun gnus-make-thread-indent-array ()
3082   (let ((n 200))
3083     (unless (and gnus-thread-indent-array
3084                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3085       (setq gnus-thread-indent-array (make-vector 201 "")
3086             gnus-thread-indent-array-level gnus-thread-indent-level)
3087       (while (>= n 0)
3088         (aset gnus-thread-indent-array n
3089               (make-string (* n gnus-thread-indent-level) ? ))
3090         (setq n (1- n))))))
3091
3092 (defun gnus-update-summary-mark-positions ()
3093   "Compute where the summary marks are to go."
3094   (save-excursion
3095     (when (gnus-buffer-exists-p gnus-summary-buffer)
3096       (set-buffer gnus-summary-buffer))
3097     (let ((gnus-replied-mark 129)
3098           (gnus-score-below-mark 130)
3099           (gnus-score-over-mark 130)
3100           (gnus-undownloaded-mark 131)
3101           (spec gnus-summary-line-format-spec)
3102           gnus-visual pos)
3103       (save-excursion
3104         (gnus-set-work-buffer)
3105         (let ((gnus-summary-line-format-spec spec)
3106               (gnus-newsgroup-downloadable '(0)))
3107           (gnus-summary-insert-line
3108            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3109            0 nil t 128 t nil "" nil 1)
3110           (goto-char (point-min))
3111           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3112                                              (- (point) (point-min) 1)))))
3113           (goto-char (point-min))
3114           (push (cons 'replied (and (search-forward "\201" nil t)
3115                                     (- (point) (point-min) 1)))
3116                 pos)
3117           (goto-char (point-min))
3118           (push (cons 'score (and (search-forward "\202" nil t)
3119                                   (- (point) (point-min) 1)))
3120                 pos)
3121           (goto-char (point-min))
3122           (push (cons 'download
3123                       (and (search-forward "\203" nil t)
3124                            (- (point) (point-min) 1)))
3125                 pos)))
3126       (setq gnus-summary-mark-positions pos))))
3127
3128 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3129   "Insert a dummy root in the summary buffer."
3130   (beginning-of-line)
3131   (gnus-add-text-properties
3132    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3133    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3134
3135 (defun gnus-summary-extract-address-component (from)
3136   (or (car (funcall gnus-extract-address-components from))
3137       from))
3138
3139 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3140   (let ((mail-parse-charset gnus-newsgroup-charset)
3141         ; Is it really necessary to do this next part for each summary line?
3142         ; Luckily, doesn't seem to slow things down much.
3143         (mail-parse-ignored-charsets
3144          (save-excursion (set-buffer gnus-summary-buffer)
3145                          gnus-newsgroup-ignored-charsets)))
3146     (or
3147      (and gnus-ignored-from-addresses
3148           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3149           (let ((extra-headers (mail-header-extra header))
3150                 to
3151                 newsgroups)
3152             (cond
3153              ((setq to (cdr (assq 'To extra-headers)))
3154               (concat "-> "
3155                       (inline
3156                         (gnus-summary-extract-address-component
3157                          (funcall gnus-decode-encoded-word-function to)))))
3158              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3159               (concat "=> " newsgroups)))))
3160      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3161
3162 (defun gnus-summary-insert-line (gnus-tmp-header
3163                                  gnus-tmp-level gnus-tmp-current
3164                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3165                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3166                                  &optional gnus-tmp-dummy gnus-tmp-score
3167                                  gnus-tmp-process)
3168   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3169          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3170          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3171          (gnus-tmp-score-char
3172           (if (or (null gnus-summary-default-score)
3173                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3174                       gnus-summary-zcore-fuzz))
3175               ?                         ;Whitespace
3176             (if (< gnus-tmp-score gnus-summary-default-score)
3177                 gnus-score-below-mark gnus-score-over-mark)))
3178          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3179          (gnus-tmp-replied
3180           (cond (gnus-tmp-process gnus-process-mark)
3181                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3182                  gnus-cached-mark)
3183                 (gnus-tmp-replied gnus-replied-mark)
3184                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3185                  gnus-forwarded-mark)
3186                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3187                  gnus-saved-mark)
3188                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3189                  gnus-recent-mark)
3190                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3191                  gnus-unseen-mark)
3192                 (t gnus-no-mark)))
3193          (gnus-tmp-downloaded
3194           (cond (undownloaded 
3195                  gnus-undownloaded-mark)
3196                 (gnus-newsgroup-agentized
3197                  gnus-downloaded-mark)
3198                 (t
3199                  gnus-no-mark)))
3200          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3201          (gnus-tmp-name
3202           (cond
3203            ((string-match "<[^>]+> *$" gnus-tmp-from)
3204             (let ((beg (match-beginning 0)))
3205               (or (and (string-match "^\".+\"" gnus-tmp-from)
3206                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3207                   (substring gnus-tmp-from 0 beg))))
3208            ((string-match "(.+)" gnus-tmp-from)
3209             (substring gnus-tmp-from
3210                        (1+ (match-beginning 0)) (1- (match-end 0))))
3211            (t gnus-tmp-from)))
3212          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3213          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3214          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3215          (buffer-read-only nil))
3216     (when (string= gnus-tmp-name "")
3217       (setq gnus-tmp-name gnus-tmp-from))
3218     (unless (numberp gnus-tmp-lines)
3219       (setq gnus-tmp-lines -1))
3220     (if (= gnus-tmp-lines -1)
3221         (setq gnus-tmp-lines "?")
3222       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3223     (gnus-put-text-property
3224      (point)
3225      (progn (eval gnus-summary-line-format-spec) (point))
3226      'gnus-number gnus-tmp-number)
3227     (when (gnus-visual-p 'summary-highlight 'highlight)
3228       (forward-line -1)
3229       (gnus-run-hooks 'gnus-summary-update-hook)
3230       (forward-line 1))))
3231
3232 (defun gnus-summary-update-line (&optional dont-update)
3233   "Update summary line after change."
3234   (when (and gnus-summary-default-score
3235              (not gnus-summary-inhibit-highlight))
3236     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3237            (article (gnus-summary-article-number))
3238            (score (gnus-summary-article-score article)))
3239       (unless dont-update
3240         (if (and gnus-summary-mark-below
3241                  (< (gnus-summary-article-score)
3242                     gnus-summary-mark-below))
3243             ;; This article has a low score, so we mark it as read.
3244             (when (memq article gnus-newsgroup-unreads)
3245               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3246           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3247             ;; This article was previously marked as read on account
3248             ;; of a low score, but now it has risen, so we mark it as
3249             ;; unread.
3250             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3251         (gnus-summary-update-mark
3252          (if (or (null gnus-summary-default-score)
3253                  (<= (abs (- score gnus-summary-default-score))
3254                      gnus-summary-zcore-fuzz))
3255              ?                          ;Whitespace
3256            (if (< score gnus-summary-default-score)
3257                gnus-score-below-mark gnus-score-over-mark))
3258          'score))
3259       ;; Do visual highlighting.
3260       (when (gnus-visual-p 'summary-highlight 'highlight)
3261         (gnus-run-hooks 'gnus-summary-update-hook)))))
3262
3263 (defvar gnus-tmp-new-adopts nil)
3264
3265 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3266   "Return the number of articles in THREAD.
3267 This may be 0 in some cases -- if none of the articles in
3268 the thread are to be displayed."
3269   (let* ((number
3270          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3271           (cond
3272            ((not (listp thread))
3273             1)
3274            ((and (consp thread) (cdr thread))
3275             (apply
3276              '+ 1 (mapcar
3277                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3278            ((null thread)
3279             1)
3280            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3281             1)
3282            (t 0))))
3283     (when (and level (zerop level) gnus-tmp-new-adopts)
3284       (incf number
3285             (apply '+ (mapcar
3286                        'gnus-summary-number-of-articles-in-thread
3287                        gnus-tmp-new-adopts))))
3288     (if char
3289         (if (> number 1) gnus-not-empty-thread-mark
3290           gnus-empty-thread-mark)
3291       number)))
3292
3293 (defsubst gnus-summary-line-message-size (head)
3294   "Return pretty-printed version of message size.
3295 This function is intended to be used in
3296 `gnus-summary-line-format-alist', which see."
3297   (let ((c (or (mail-header-chars head) -1)))
3298     (cond ((< c 0) "n/a")               ; chars not available
3299           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3300           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3301           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3302           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3303
3304
3305 (defun gnus-summary-set-local-parameters (group)
3306   "Go through the local params of GROUP and set all variable specs in that list."
3307   (let ((params (gnus-group-find-parameter group))
3308         (vars '(quit-config))           ; Ignore quit-config.
3309         elem)
3310     (while params
3311       (setq elem (car params)
3312             params (cdr params))
3313       (and (consp elem)                 ; Has to be a cons.
3314            (consp (cdr elem))           ; The cdr has to be a list.
3315            (symbolp (car elem))         ; Has to be a symbol in there.
3316            (not (memq (car elem) vars))
3317            (ignore-errors               ; So we set it.
3318              (push (car elem) vars)
3319              (make-local-variable (car elem))
3320              (set (car elem) (eval (nth 1 elem))))))))
3321
3322 (defun gnus-summary-read-group (group &optional show-all no-article
3323                                       kill-buffer no-display backward
3324                                       select-articles)
3325   "Start reading news in newsgroup GROUP.
3326 If SHOW-ALL is non-nil, already read articles are also listed.
3327 If NO-ARTICLE is non-nil, no article is selected initially.
3328 If NO-DISPLAY, don't generate a summary buffer."
3329   (let (result)
3330     (while (and group
3331                 (null (setq result
3332                             (let ((gnus-auto-select-next nil))
3333                               (or (gnus-summary-read-group-1
3334                                    group show-all no-article
3335                                    kill-buffer no-display
3336                                    select-articles)
3337                                   (setq show-all nil
3338                                         select-articles nil)))))
3339                 (eq gnus-auto-select-next 'quietly))
3340       (set-buffer gnus-group-buffer)
3341       ;; The entry function called above goes to the next
3342       ;; group automatically, so we go two groups back
3343       ;; if we are searching for the previous group.
3344       (when backward
3345         (gnus-group-prev-unread-group 2))
3346       (if (not (equal group (gnus-group-group-name)))
3347           (setq group (gnus-group-group-name))
3348         (setq group nil)))
3349     result))
3350
3351 (defun gnus-summary-read-group-1 (group show-all no-article
3352                                         kill-buffer no-display
3353                                         &optional select-articles)
3354   ;; Killed foreign groups can't be entered.
3355   ;;  (when (and (not (gnus-group-native-p group))
3356   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3357   ;;    (error "Dead non-native groups can't be entered"))
3358   (gnus-message 5 "Retrieving newsgroup: %s..."
3359                 (gnus-group-decoded-name group))
3360   (let* ((new-group (gnus-summary-setup-buffer group))
3361          (quit-config (gnus-group-quit-config group))
3362          (did-select (and new-group (gnus-select-newsgroup
3363                                      group show-all select-articles))))
3364     (cond
3365      ;; This summary buffer exists already, so we just select it.
3366      ((not new-group)
3367       (gnus-set-global-variables)
3368       (when kill-buffer
3369         (gnus-kill-or-deaden-summary kill-buffer))
3370       (gnus-configure-windows 'summary 'force)
3371       (gnus-set-mode-line 'summary)
3372       (gnus-summary-position-point)
3373       (message "")
3374       t)
3375      ;; We couldn't select this group.
3376      ((null did-select)
3377       (when (and (eq major-mode 'gnus-summary-mode)
3378                  (not (equal (current-buffer) kill-buffer)))
3379         (kill-buffer (current-buffer))
3380         (if (not quit-config)
3381             (progn
3382               ;; Update the info -- marks might need to be removed,
3383               ;; for instance.
3384               (gnus-summary-update-info)
3385               (set-buffer gnus-group-buffer)
3386               (gnus-group-jump-to-group group)
3387               (gnus-group-next-unread-group 1))
3388           (gnus-handle-ephemeral-exit quit-config)))
3389       (let ((grpinfo (gnus-get-info group)))
3390         (if (null (gnus-info-read grpinfo))
3391             (gnus-message 3 "Group %s contains no messages"
3392                           (gnus-group-decoded-name group))
3393           (gnus-message 3 "Can't select group")))
3394       nil)
3395      ;; The user did a `C-g' while prompting for number of articles,
3396      ;; so we exit this group.
3397      ((eq did-select 'quit)
3398       (and (eq major-mode 'gnus-summary-mode)
3399            (not (equal (current-buffer) kill-buffer))
3400            (kill-buffer (current-buffer)))
3401       (when kill-buffer
3402         (gnus-kill-or-deaden-summary kill-buffer))
3403       (if (not quit-config)
3404           (progn
3405             (set-buffer gnus-group-buffer)
3406             (gnus-group-jump-to-group group)
3407             (gnus-group-next-unread-group 1)
3408             (gnus-configure-windows 'group 'force))
3409         (gnus-handle-ephemeral-exit quit-config))
3410       ;; Finally signal the quit.
3411       (signal 'quit nil))
3412      ;; The group was successfully selected.
3413      (t
3414       (gnus-set-global-variables)
3415       ;; Save the active value in effect when the group was entered.
3416       (setq gnus-newsgroup-active
3417             (gnus-copy-sequence
3418              (gnus-active gnus-newsgroup-name)))
3419       ;; You can change the summary buffer in some way with this hook.
3420       (gnus-run-hooks 'gnus-select-group-hook)
3421       (gnus-update-format-specifications
3422        nil 'summary 'summary-mode 'summary-dummy)
3423       (gnus-update-summary-mark-positions)
3424       ;; Do score processing.
3425       (when gnus-use-scoring
3426         (gnus-possibly-score-headers))
3427       ;; Check whether to fill in the gaps in the threads.
3428       (when gnus-build-sparse-threads
3429         (gnus-build-sparse-threads))
3430       ;; Find the initial limit.
3431       (if gnus-show-threads
3432           (if show-all
3433               (let ((gnus-newsgroup-dormant nil))
3434                 (gnus-summary-initial-limit show-all))
3435             (gnus-summary-initial-limit show-all))
3436         ;; When unthreaded, all articles are always shown.
3437         (setq gnus-newsgroup-limit
3438               (mapcar
3439                (lambda (header) (mail-header-number header))
3440                gnus-newsgroup-headers)))
3441       ;; Generate the summary buffer.
3442       (unless no-display
3443         (gnus-summary-prepare))
3444       (when gnus-use-trees
3445         (gnus-tree-open group)
3446         (setq gnus-summary-highlight-line-function
3447               'gnus-tree-highlight-article))
3448       ;; If the summary buffer is empty, but there are some low-scored
3449       ;; articles or some excluded dormants, we include these in the
3450       ;; buffer.
3451       (when (and (zerop (buffer-size))
3452                  (not no-display))
3453         (cond (gnus-newsgroup-dormant
3454                (gnus-summary-limit-include-dormant))
3455               ((and gnus-newsgroup-scored show-all)
3456                (gnus-summary-limit-include-expunged t))))
3457       ;; Function `gnus-apply-kill-file' must be called in this hook.
3458       (gnus-run-hooks 'gnus-apply-kill-hook)
3459       (if (and (zerop (buffer-size))
3460                (not no-display))
3461           (progn
3462             ;; This newsgroup is empty.
3463             (gnus-summary-catchup-and-exit nil t)
3464             (gnus-message 6 "No unread news")
3465             (when kill-buffer
3466               (gnus-kill-or-deaden-summary kill-buffer))
3467             ;; Return nil from this function.
3468             nil)
3469         ;; Hide conversation thread subtrees.  We cannot do this in
3470         ;; gnus-summary-prepare-hook since kill processing may not
3471         ;; work with hidden articles.
3472         (gnus-summary-maybe-hide-threads)
3473         (when kill-buffer
3474           (gnus-kill-or-deaden-summary kill-buffer))
3475         (gnus-summary-auto-select-subject)
3476         ;; Show first unread article if requested.
3477         (if (and (not no-article)
3478                  (not no-display)
3479                  gnus-newsgroup-unreads
3480                  gnus-auto-select-first)
3481             (progn
3482               (gnus-configure-windows 'summary)
3483               (let ((art (gnus-summary-article-number)))
3484                 (unless (or (memq art gnus-newsgroup-undownloaded)
3485                             (memq art gnus-newsgroup-downloadable))
3486                   (gnus-summary-goto-article art))))
3487           ;; Don't select any articles.
3488           (gnus-summary-position-point)
3489           (gnus-configure-windows 'summary 'force)
3490           (gnus-set-mode-line 'summary))
3491         (when (get-buffer-window gnus-group-buffer t)
3492           ;; Gotta use windows, because recenter does weird stuff if
3493           ;; the current buffer ain't the displayed window.
3494           (let ((owin (selected-window)))
3495             (select-window (get-buffer-window gnus-group-buffer t))
3496             (when (gnus-group-goto-group group)
3497               (recenter))
3498             (select-window owin)))
3499         ;; Mark this buffer as "prepared".
3500         (setq gnus-newsgroup-prepared t)
3501         (gnus-run-hooks 'gnus-summary-prepared-hook)
3502         (unless (gnus-ephemeral-group-p group)
3503           (gnus-group-update-group group))
3504         t)))))
3505
3506 (defun gnus-summary-auto-select-subject ()
3507   "Select the subject line on initial group entry."
3508   (goto-char (point-min))
3509   (cond
3510    ((eq gnus-auto-select-subject 'best)
3511     (gnus-summary-best-unread-subject))
3512    ((eq gnus-auto-select-subject 'unread)
3513     (gnus-summary-first-unread-subject))
3514    ((eq gnus-auto-select-subject 'unseen)
3515     (gnus-summary-first-unseen-subject))
3516    ((eq gnus-auto-select-subject 'unseen-or-unread)
3517     (gnus-summary-first-unseen-or-unread-subject))
3518    ((eq gnus-auto-select-subject 'first)
3519     ;; Do nothing.
3520     )
3521    ((gnus-functionp gnus-auto-select-subject)
3522     (funcall gnus-auto-select-subject))))
3523
3524 (defun gnus-summary-prepare ()
3525   "Generate the summary buffer."
3526   (interactive)
3527   (let ((buffer-read-only nil))
3528     (erase-buffer)
3529     (setq gnus-newsgroup-data nil
3530           gnus-newsgroup-data-reverse nil)
3531     (gnus-run-hooks 'gnus-summary-generate-hook)
3532     ;; Generate the buffer, either with threads or without.
3533     (when gnus-newsgroup-headers
3534       (gnus-summary-prepare-threads
3535        (if gnus-show-threads
3536            (gnus-sort-gathered-threads
3537             (funcall gnus-summary-thread-gathering-function
3538                      (gnus-sort-threads
3539                       (gnus-cut-threads (gnus-make-threads)))))
3540          ;; Unthreaded display.
3541          (gnus-sort-articles gnus-newsgroup-headers))))
3542     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3543     ;; Call hooks for modifying summary buffer.
3544     (goto-char (point-min))
3545     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3546
3547 (defsubst gnus-general-simplify-subject (subject)
3548   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3549   (setq subject
3550         (cond
3551          ;; Truncate the subject.
3552          (gnus-simplify-subject-functions
3553           (gnus-map-function gnus-simplify-subject-functions subject))
3554          ((numberp gnus-summary-gather-subject-limit)
3555           (setq subject (gnus-simplify-subject-re subject))
3556           (if (> (length subject) gnus-summary-gather-subject-limit)
3557               (substring subject 0 gnus-summary-gather-subject-limit)
3558             subject))
3559          ;; Fuzzily simplify it.
3560          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3561           (gnus-simplify-subject-fuzzy subject))
3562          ;; Just remove the leading "Re:".
3563          (t
3564           (gnus-simplify-subject-re subject))))
3565
3566   (if (and gnus-summary-gather-exclude-subject
3567            (string-match gnus-summary-gather-exclude-subject subject))
3568       nil                         ; This article shouldn't be gathered
3569     subject))
3570
3571 (defun gnus-summary-simplify-subject-query ()
3572   "Query where the respool algorithm would put this article."
3573   (interactive)
3574   (gnus-summary-select-article)
3575   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3576
3577 (defun gnus-gather-threads-by-subject (threads)
3578   "Gather threads by looking at Subject headers."
3579   (if (not gnus-summary-make-false-root)
3580       threads
3581     (let ((hashtb (gnus-make-hashtable 1024))
3582           (prev threads)
3583           (result threads)
3584           subject hthread whole-subject)
3585       (while threads
3586         (setq subject (gnus-general-simplify-subject
3587                        (setq whole-subject (mail-header-subject
3588                                             (caar threads)))))
3589         (when subject
3590           (if (setq hthread (gnus-gethash subject hashtb))
3591               (progn
3592                 ;; We enter a dummy root into the thread, if we
3593                 ;; haven't done that already.
3594                 (unless (stringp (caar hthread))
3595                   (setcar hthread (list whole-subject (car hthread))))
3596                 ;; We add this new gathered thread to this gathered
3597                 ;; thread.
3598                 (setcdr (car hthread)
3599                         (nconc (cdar hthread) (list (car threads))))
3600                 ;; Remove it from the list of threads.
3601                 (setcdr prev (cdr threads))
3602                 (setq threads prev))
3603             ;; Enter this thread into the hash table.
3604             (gnus-sethash subject
3605                           (if gnus-summary-make-false-root-always
3606                               (progn
3607                                 ;; If you want a dummy root above all
3608                                 ;; threads...
3609                                 (setcar threads (list whole-subject
3610                                                       (car threads)))
3611                                 threads)
3612                             threads)
3613                           hashtb)))
3614         (setq prev threads)
3615         (setq threads (cdr threads)))
3616       result)))
3617
3618 (defun gnus-gather-threads-by-references (threads)
3619   "Gather threads by looking at References headers."
3620   (let ((idhashtb (gnus-make-hashtable 1024))
3621         (thhashtb (gnus-make-hashtable 1024))
3622         (prev threads)
3623         (result threads)
3624         ids references id gthread gid entered ref)
3625     (while threads
3626       (when (setq references (mail-header-references (caar threads)))
3627         (setq id (mail-header-id (caar threads))
3628               ids (inline (gnus-split-references references))
3629               entered nil)
3630         (while (setq ref (pop ids))
3631           (setq ids (delete ref ids))
3632           (if (not (setq gid (gnus-gethash ref idhashtb)))
3633               (progn
3634                 (gnus-sethash ref id idhashtb)
3635                 (gnus-sethash id threads thhashtb))
3636             (setq gthread (gnus-gethash gid thhashtb))
3637             (unless entered
3638               ;; We enter a dummy root into the thread, if we
3639               ;; haven't done that already.
3640               (unless (stringp (caar gthread))
3641                 (setcar gthread (list (mail-header-subject (caar gthread))
3642                                       (car gthread))))
3643               ;; We add this new gathered thread to this gathered
3644               ;; thread.
3645               (setcdr (car gthread)
3646                       (nconc (cdar gthread) (list (car threads)))))
3647             ;; Add it into the thread hash table.
3648             (gnus-sethash id gthread thhashtb)
3649             (setq entered t)
3650             ;; Remove it from the list of threads.
3651             (setcdr prev (cdr threads))
3652             (setq threads prev))))
3653       (setq prev threads)
3654       (setq threads (cdr threads)))
3655     result))
3656
3657 (defun gnus-sort-gathered-threads (threads)
3658   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3659   (let ((result threads))
3660     (while threads
3661       (when (stringp (caar threads))
3662         (setcdr (car threads)
3663                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3664       (setq threads (cdr threads)))
3665     result))
3666
3667 (defun gnus-thread-loop-p (root thread)
3668   "Say whether ROOT is in THREAD."
3669   (let ((stack (list thread))
3670         (infloop 0)
3671         th)
3672     (while (setq thread (pop stack))
3673       (setq th (cdr thread))
3674       (while (and th
3675                   (not (eq (caar th) root)))
3676         (pop th))
3677       (if th
3678           ;; We have found a loop.
3679           (let (ref-dep)
3680             (setcdr thread (delq (car th) (cdr thread)))
3681             (if (boundp (setq ref-dep (intern "none"
3682                                               gnus-newsgroup-dependencies)))
3683                 (setcdr (symbol-value ref-dep)
3684                         (nconc (cdr (symbol-value ref-dep))
3685                                (list (car th))))
3686               (set ref-dep (list nil (car th))))
3687             (setq infloop 1
3688                   stack nil))
3689         ;; Push all the subthreads onto the stack.
3690         (push (cdr thread) stack)))
3691     infloop))
3692
3693 (defun gnus-make-threads ()
3694   "Go through the dependency hashtb and find the roots.  Return all threads."
3695   (let (threads)
3696     (while (catch 'infloop
3697              (mapatoms
3698               (lambda (refs)
3699                 ;; Deal with self-referencing References loops.
3700                 (when (and (car (symbol-value refs))
3701                            (not (zerop
3702                                  (apply
3703                                   '+
3704                                   (mapcar
3705                                    (lambda (thread)
3706                                      (gnus-thread-loop-p
3707                                       (car (symbol-value refs)) thread))
3708                                    (cdr (symbol-value refs)))))))
3709                   (setq threads nil)
3710                   (throw 'infloop t))
3711                 (unless (car (symbol-value refs))
3712                   ;; These threads do not refer back to any other
3713                   ;; articles, so they're roots.
3714                   (setq threads (append (cdr (symbol-value refs)) threads))))
3715               gnus-newsgroup-dependencies)))
3716     threads))
3717
3718 ;; Build the thread tree.
3719 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3720   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3721
3722 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3723 if it was already present.
3724
3725 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3726 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3727 Message-IDs will be renamed to a unique Message-ID before being
3728 entered.
3729
3730 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3731   (let* ((id (mail-header-id header))
3732          (id-dep (and id (intern id dependencies)))
3733          parent-id ref ref-dep ref-header replaced)
3734     ;; Enter this `header' in the `dependencies' table.
3735     (cond
3736      ((not id-dep)
3737       (setq header nil))
3738      ;; The first two cases do the normal part: enter a new `header'
3739      ;; in the `dependencies' table.
3740      ((not (boundp id-dep))
3741       (set id-dep (list header)))
3742      ((null (car (symbol-value id-dep)))
3743       (setcar (symbol-value id-dep) header))
3744
3745      ;; From here the `header' was already present in the
3746      ;; `dependencies' table.
3747      (force-new
3748       ;; Overrides an existing entry;
3749       ;; just set the header part of the entry.
3750       (setcar (symbol-value id-dep) header)
3751       (setq replaced t))
3752
3753      ;; Renames the existing `header' to a unique Message-ID.
3754      ((not gnus-summary-ignore-duplicates)
3755       ;; An article with this Message-ID has already been seen.
3756       ;; We rename the Message-ID.
3757       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3758            (list header))
3759       (mail-header-set-id header id))
3760
3761      ;; The last case ignores an existing entry, except it adds any
3762      ;; additional Xrefs (in case the two articles came from different
3763      ;; servers.
3764      ;; Also sets `header' to `nil' meaning that the `dependencies'
3765      ;; table was *not* modified.
3766      (t
3767       (mail-header-set-xref
3768        (car (symbol-value id-dep))
3769        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3770                    "")
3771                (or (mail-header-xref header) "")))
3772       (setq header nil)))
3773
3774     (when (and header (not replaced))
3775       ;; First check that we are not creating a References loop.
3776       (setq parent-id (gnus-parent-id (mail-header-references header)))
3777       (setq ref parent-id)
3778       (while (and ref
3779                   (setq ref-dep (intern-soft ref dependencies))
3780                   (boundp ref-dep)
3781                   (setq ref-header (car (symbol-value ref-dep))))
3782         (if (string= id ref)
3783             ;; Yuk!  This is a reference loop.  Make the article be a
3784             ;; root article.
3785             (progn
3786               (mail-header-set-references (car (symbol-value id-dep)) "none")
3787               (setq ref nil)
3788               (setq parent-id nil))
3789           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3790       (setq ref-dep (intern (or parent-id "none") dependencies))
3791       (if (boundp ref-dep)
3792           (setcdr (symbol-value ref-dep)
3793                   (nconc (cdr (symbol-value ref-dep))
3794                          (list (symbol-value id-dep))))
3795         (set ref-dep (list nil (symbol-value id-dep)))))
3796     header))
3797
3798 (defun gnus-extract-message-id-from-in-reply-to (string)
3799   (if (string-match "<[^>]+>" string)
3800       (substring string (match-beginning 0) (match-end 0))
3801     nil))
3802
3803 (defun gnus-build-sparse-threads ()
3804   (let ((headers gnus-newsgroup-headers)
3805         (mail-parse-charset gnus-newsgroup-charset)
3806         (gnus-summary-ignore-duplicates t)
3807         header references generation relations
3808         subject child end new-child date)
3809     ;; First we create an alist of generations/relations, where
3810     ;; generations is how much we trust the relation, and the relation
3811     ;; is parent/child.
3812     (gnus-message 7 "Making sparse threads...")
3813     (save-excursion
3814       (nnheader-set-temp-buffer " *gnus sparse threads*")
3815       (while (setq header (pop headers))
3816         (when (and (setq references (mail-header-references header))
3817                    (not (string= references "")))
3818           (insert references)
3819           (setq child (mail-header-id header)
3820                 subject (mail-header-subject header)
3821                 date (mail-header-date header)
3822                 generation 0)
3823           (while (search-backward ">" nil t)
3824             (setq end (1+ (point)))
3825             (when (search-backward "<" nil t)
3826               (setq new-child (buffer-substring (point) end))
3827               (push (list (incf generation)
3828                           child (setq child new-child)
3829                           subject date)
3830                     relations)))
3831           (when child
3832             (push (list (1+ generation) child nil subject) relations))
3833           (erase-buffer)))
3834       (kill-buffer (current-buffer)))
3835     ;; Sort over trustworthiness.
3836     (mapcar
3837      (lambda (relation)
3838        (when (gnus-dependencies-add-header
3839               (make-full-mail-header
3840                gnus-reffed-article-number
3841                (nth 3 relation) "" (or (nth 4 relation) "")
3842                (nth 1 relation)
3843                (or (nth 2 relation) "") 0 0 "")
3844               gnus-newsgroup-dependencies nil)
3845          (push gnus-reffed-article-number gnus-newsgroup-limit)
3846          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3847          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3848                gnus-newsgroup-reads)
3849          (decf gnus-reffed-article-number)))
3850      (sort relations 'car-less-than-car))
3851     (gnus-message 7 "Making sparse threads...done")))
3852
3853 (defun gnus-build-old-threads ()
3854   ;; Look at all the articles that refer back to old articles, and
3855   ;; fetch the headers for the articles that aren't there.  This will
3856   ;; build complete threads - if the roots haven't been expired by the
3857   ;; server, that is.
3858   (let ((mail-parse-charset gnus-newsgroup-charset)
3859         id heads)
3860     (mapatoms
3861      (lambda (refs)
3862        (when (not (car (symbol-value refs)))
3863          (setq heads (cdr (symbol-value refs)))
3864          (while heads
3865            (if (memq (mail-header-number (caar heads))
3866                      gnus-newsgroup-dormant)
3867                (setq heads (cdr heads))
3868              (setq id (symbol-name refs))
3869              (while (and (setq id (gnus-build-get-header id))
3870                          (not (car (gnus-id-to-thread id)))))
3871              (setq heads nil)))))
3872      gnus-newsgroup-dependencies)))
3873
3874 ;; This function has to be called with point after the article number
3875 ;; on the beginning of the line.
3876 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3877   (let ((eol (gnus-point-at-eol))
3878         (buffer (current-buffer))
3879         header references in-reply-to)
3880
3881     ;; overview: [num subject from date id refs chars lines misc]
3882     (unwind-protect
3883         (let (x)
3884           (narrow-to-region (point) eol)
3885           (unless (eobp)
3886             (forward-char))
3887
3888           (setq header
3889                 (make-full-mail-header
3890                  number                 ; number
3891                  (condition-case ()     ; subject
3892                      (funcall gnus-decode-encoded-word-function
3893                               (setq x (nnheader-nov-field)))
3894                    (error x))
3895                  (condition-case ()     ; from
3896                      (funcall gnus-decode-encoded-word-function
3897                               (setq x (nnheader-nov-field)))
3898                    (error x))
3899                  (nnheader-nov-field)   ; date
3900                  (nnheader-nov-read-message-id) ; id
3901                  (setq references (nnheader-nov-field)) ; refs
3902                  (nnheader-nov-read-integer) ; chars
3903                  (nnheader-nov-read-integer) ; lines
3904                  (unless (eobp)
3905                    (if (looking-at "Xref: ")
3906                        (goto-char (match-end 0)))
3907                    (nnheader-nov-field)) ; Xref
3908                  (nnheader-nov-parse-extra)))) ; extra
3909
3910       (widen))
3911
3912     (when (and (string= references "")
3913                (setq in-reply-to (mail-header-extra header))
3914                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3915       (mail-header-set-references
3916        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3917
3918     (when gnus-alter-header-function
3919       (funcall gnus-alter-header-function header))
3920     (gnus-dependencies-add-header header dependencies force-new)))
3921
3922 (defun gnus-build-get-header (id)
3923   "Look through the buffer of NOV lines and find the header to ID.
3924 Enter this line into the dependencies hash table, and return
3925 the id of the parent article (if any)."
3926   (let ((deps gnus-newsgroup-dependencies)
3927         found header)
3928     (prog1
3929         (save-excursion
3930           (set-buffer nntp-server-buffer)
3931           (let ((case-fold-search nil))
3932             (goto-char (point-min))
3933             (while (and (not found)
3934                         (search-forward id nil t))
3935               (beginning-of-line)
3936               (setq found (looking-at
3937                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3938                                    (regexp-quote id))))
3939               (or found (beginning-of-line 2)))
3940             (when found
3941               (beginning-of-line)
3942               (and
3943                (setq header (gnus-nov-parse-line
3944                              (read (current-buffer)) deps))
3945                (gnus-parent-id (mail-header-references header))))))
3946       (when header
3947         (let ((number (mail-header-number header)))
3948           (push number gnus-newsgroup-limit)
3949           (push header gnus-newsgroup-headers)
3950           (if (memq number gnus-newsgroup-unselected)
3951               (progn
3952                 (setq gnus-newsgroup-unreads
3953                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3954                                                number))
3955                 (setq gnus-newsgroup-unselected
3956                       (delq number gnus-newsgroup-unselected)))
3957             (push number gnus-newsgroup-ancient)))))))
3958
3959 (defun gnus-build-all-threads ()
3960   "Read all the headers."
3961   (let ((gnus-summary-ignore-duplicates t)
3962         (mail-parse-charset gnus-newsgroup-charset)
3963         (dependencies gnus-newsgroup-dependencies)
3964         header article)
3965     (save-excursion
3966       (set-buffer nntp-server-buffer)
3967       (let ((case-fold-search nil))
3968         (goto-char (point-min))
3969         (while (not (eobp))
3970           (ignore-errors
3971             (setq article (read (current-buffer))
3972                   header (gnus-nov-parse-line article dependencies)))
3973           (when header
3974             (save-excursion
3975               (set-buffer gnus-summary-buffer)
3976               (push header gnus-newsgroup-headers)
3977               (if (memq (setq article (mail-header-number header))
3978                         gnus-newsgroup-unselected)
3979                   (progn
3980                     (setq gnus-newsgroup-unreads
3981                           (gnus-add-to-sorted-list
3982                            gnus-newsgroup-unreads article))
3983                     (setq gnus-newsgroup-unselected
3984                           (delq article gnus-newsgroup-unselected)))
3985                 (push article gnus-newsgroup-ancient)))
3986             (forward-line 1)))))))
3987
3988 (defun gnus-summary-update-article-line (article header)
3989   "Update the line for ARTICLE using HEADERS."
3990   (let* ((id (mail-header-id header))
3991          (thread (gnus-id-to-thread id)))
3992     (unless thread
3993       (error "Article in no thread"))
3994     ;; Update the thread.
3995     (setcar thread header)
3996     (gnus-summary-goto-subject article)
3997     (let* ((datal (gnus-data-find-list article))
3998            (data (car datal))
3999            (length (when (cdr datal)
4000                      (- (gnus-data-pos data)
4001                         (gnus-data-pos (cadr datal)))))
4002            (buffer-read-only nil)
4003            (level (gnus-summary-thread-level)))
4004       (gnus-delete-line)
4005       (gnus-summary-insert-line
4006        header level nil 
4007        (memq article gnus-newsgroup-undownloaded)
4008        (gnus-article-mark article)
4009        (memq article gnus-newsgroup-replied)
4010        (memq article gnus-newsgroup-expirable)
4011        ;; Only insert the Subject string when it's different
4012        ;; from the previous Subject string.
4013        (if (and
4014             gnus-show-threads
4015             (gnus-subject-equal
4016              (condition-case ()
4017                  (mail-header-subject
4018                   (gnus-data-header
4019                    (cadr
4020                     (gnus-data-find-list
4021                      article
4022                      (gnus-data-list t)))))
4023                ;; Error on the side of excessive subjects.
4024                (error ""))
4025              (mail-header-subject header)))
4026            ""
4027          (mail-header-subject header))
4028        nil (cdr (assq article gnus-newsgroup-scored))
4029        (memq article gnus-newsgroup-processable))
4030       (when length
4031         (gnus-data-update-list
4032          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4033
4034 (defun gnus-summary-update-article (article &optional iheader)
4035   "Update ARTICLE in the summary buffer."
4036   (set-buffer gnus-summary-buffer)
4037   (let* ((header (gnus-summary-article-header article))
4038          (id (mail-header-id header))
4039          (data (gnus-data-find article))
4040          (thread (gnus-id-to-thread id))
4041          (references (mail-header-references header))
4042          (parent
4043           (gnus-id-to-thread
4044            (or (gnus-parent-id
4045                 (when (and references
4046                            (not (equal "" references)))
4047                   references))
4048                "none")))
4049          (buffer-read-only nil)
4050          (old (car thread)))
4051     (when thread
4052       (unless iheader
4053         (setcar thread nil)
4054         (when parent
4055           (delq thread parent)))
4056       (if (gnus-summary-insert-subject id header)
4057           ;; Set the (possibly) new article number in the data structure.
4058           (gnus-data-set-number data (gnus-id-to-article id))
4059         (setcar thread old)
4060         nil))))
4061
4062 (defun gnus-rebuild-thread (id &optional line)
4063   "Rebuild the thread containing ID.
4064 If LINE, insert the rebuilt thread starting on line LINE."
4065   (let ((buffer-read-only nil)
4066         old-pos current thread data)
4067     (if (not gnus-show-threads)
4068         (setq thread (list (car (gnus-id-to-thread id))))
4069       ;; Get the thread this article is part of.
4070       (setq thread (gnus-remove-thread id)))
4071     (setq old-pos (gnus-point-at-bol))
4072     (setq current (save-excursion
4073                     (and (re-search-backward "[\r\n]" nil t)
4074                          (gnus-summary-article-number))))
4075     ;; If this is a gathered thread, we have to go some re-gathering.
4076     (when (stringp (car thread))
4077       (let ((subject (car thread))
4078             roots thr)
4079         (setq thread (cdr thread))
4080         (while thread
4081           (unless (memq (setq thr (gnus-id-to-thread
4082                                    (gnus-root-id
4083                                     (mail-header-id (caar thread)))))
4084                         roots)
4085             (push thr roots))
4086           (setq thread (cdr thread)))
4087         ;; We now have all (unique) roots.
4088         (if (= (length roots) 1)
4089             ;; All the loose roots are now one solid root.
4090             (setq thread (car roots))
4091           (setq thread (cons subject (gnus-sort-threads roots))))))
4092     (let (threads)
4093       ;; We then insert this thread into the summary buffer.
4094       (when line
4095         (goto-char (point-min))
4096         (forward-line (1- line)))
4097       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4098         (if gnus-show-threads
4099             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4100           (gnus-summary-prepare-unthreaded thread))
4101         (setq data (nreverse gnus-newsgroup-data))
4102         (setq threads gnus-newsgroup-threads))
4103       ;; We splice the new data into the data structure.
4104       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4105       ;;!!! then we want to insert at the beginning of the buffer.
4106       ;;!!! That happens to be true with Gnus now, but that may
4107       ;;!!! change in the future.  Perhaps.
4108       (gnus-data-enter-list
4109        (if line nil current) data (- (point) old-pos))
4110       (setq gnus-newsgroup-threads
4111             (nconc threads gnus-newsgroup-threads))
4112       (gnus-data-compute-positions))))
4113
4114 (defun gnus-number-to-header (number)
4115   "Return the header for article NUMBER."
4116   (let ((headers gnus-newsgroup-headers))
4117     (while (and headers
4118                 (not (= number (mail-header-number (car headers)))))
4119       (pop headers))
4120     (when headers
4121       (car headers))))
4122
4123 (defun gnus-parent-headers (in-headers &optional generation)
4124   "Return the headers of the GENERATIONeth parent of HEADERS."
4125   (unless generation
4126     (setq generation 1))
4127   (let ((parent t)
4128         (headers in-headers)
4129         references)
4130     (while (and parent
4131                 (not (zerop generation))
4132                 (setq references (mail-header-references headers)))
4133       (setq headers (if (and references
4134                              (setq parent (gnus-parent-id references)))
4135                         (car (gnus-id-to-thread parent))
4136                       nil))
4137       (decf generation))
4138     (and (not (eq headers in-headers))
4139          headers)))
4140
4141 (defun gnus-id-to-thread (id)
4142   "Return the (sub-)thread where ID appears."
4143   (gnus-gethash id gnus-newsgroup-dependencies))
4144
4145 (defun gnus-id-to-article (id)
4146   "Return the article number of ID."
4147   (let ((thread (gnus-id-to-thread id)))
4148     (when (and thread
4149                (car thread))
4150       (mail-header-number (car thread)))))
4151
4152 (defun gnus-id-to-header (id)
4153   "Return the article headers of ID."
4154   (car (gnus-id-to-thread id)))
4155
4156 (defun gnus-article-displayed-root-p (article)
4157   "Say whether ARTICLE is a root(ish) article."
4158   (let ((level (gnus-summary-thread-level article))
4159         (refs (mail-header-references  (gnus-summary-article-header article)))
4160         particle)
4161     (cond
4162      ((null level) nil)
4163      ((zerop level) t)
4164      ((null refs) t)
4165      ((null (gnus-parent-id refs)) t)
4166      ((and (= 1 level)
4167            (null (setq particle (gnus-id-to-article
4168                                  (gnus-parent-id refs))))
4169            (null (gnus-summary-thread-level particle)))))))
4170
4171 (defun gnus-root-id (id)
4172   "Return the id of the root of the thread where ID appears."
4173   (let (last-id prev)
4174     (while (and id (setq prev (car (gnus-id-to-thread id))))
4175       (setq last-id id
4176             id (gnus-parent-id (mail-header-references prev))))
4177     last-id))
4178
4179 (defun gnus-articles-in-thread (thread)
4180   "Return the list of articles in THREAD."
4181   (cons (mail-header-number (car thread))
4182         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4183
4184 (defun gnus-remove-thread (id &optional dont-remove)
4185   "Remove the thread that has ID in it."
4186   (let (headers thread last-id)
4187     ;; First go up in this thread until we find the root.
4188     (setq last-id (gnus-root-id id)
4189           headers (message-flatten-list (gnus-id-to-thread last-id)))
4190     ;; We have now found the real root of this thread.  It might have
4191     ;; been gathered into some loose thread, so we have to search
4192     ;; through the threads to find the thread we wanted.
4193     (let ((threads gnus-newsgroup-threads)
4194           sub)
4195       (while threads
4196         (setq sub (car threads))
4197         (if (stringp (car sub))
4198             ;; This is a gathered thread, so we look at the roots
4199             ;; below it to find whether this article is in this
4200             ;; gathered root.
4201             (progn
4202               (setq sub (cdr sub))
4203               (while sub
4204                 (when (member (caar sub) headers)
4205                   (setq thread (car threads)
4206                         threads nil
4207                         sub nil))
4208                 (setq sub (cdr sub))))
4209           ;; It's an ordinary thread, so we check it.
4210           (when (eq (car sub) (car headers))
4211             (setq thread sub
4212                   threads nil)))
4213         (setq threads (cdr threads)))
4214       ;; If this article is in no thread, then it's a root.
4215       (if thread
4216           (unless dont-remove
4217             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4218         (setq thread (gnus-id-to-thread last-id)))
4219       (when thread
4220         (prog1
4221             thread                      ; We return this thread.
4222           (unless dont-remove
4223             (if (stringp (car thread))
4224                 (progn
4225                   ;; If we use dummy roots, then we have to remove the
4226                   ;; dummy root as well.
4227                   (when (eq gnus-summary-make-false-root 'dummy)
4228                     ;; We go to the dummy root by going to
4229                     ;; the first sub-"thread", and then one line up.
4230                     (gnus-summary-goto-article
4231                      (mail-header-number (caadr thread)))
4232                     (forward-line -1)
4233                     (gnus-delete-line)
4234                     (gnus-data-compute-positions))
4235                   (setq thread (cdr thread))
4236                   (while thread
4237                     (gnus-remove-thread-1 (car thread))
4238                     (setq thread (cdr thread))))
4239               (gnus-remove-thread-1 thread))))))))
4240
4241 (defun gnus-remove-thread-1 (thread)
4242   "Remove the thread THREAD recursively."
4243   (let ((number (mail-header-number (pop thread)))
4244         d)
4245     (setq thread (reverse thread))
4246     (while thread
4247       (gnus-remove-thread-1 (pop thread)))
4248     (when (setq d (gnus-data-find number))
4249       (goto-char (gnus-data-pos d))
4250       (gnus-summary-show-thread)
4251       (gnus-data-remove
4252        number
4253        (- (gnus-point-at-bol)
4254           (prog1
4255               (1+ (gnus-point-at-eol))
4256             (gnus-delete-line)))))))
4257
4258 (defun gnus-sort-threads-1 (threads func)
4259   (sort (mapcar (lambda (thread)
4260                   (cons (car thread)
4261                         (and (cdr thread)
4262                              (gnus-sort-threads-1 (cdr thread) func))))
4263                 threads) func))
4264
4265 (defun gnus-sort-threads (threads)
4266   "Sort THREADS."
4267   (if (not gnus-thread-sort-functions)
4268       threads
4269     (gnus-message 8 "Sorting threads...")
4270     (let ((max-lisp-eval-depth 5000))
4271       (prog1 (gnus-sort-threads-1
4272          threads
4273          (gnus-make-sort-function gnus-thread-sort-functions))
4274         (gnus-message 8 "Sorting threads...done")))))
4275
4276 (defun gnus-sort-articles (articles)
4277   "Sort ARTICLES."
4278   (when gnus-article-sort-functions
4279     (gnus-message 7 "Sorting articles...")
4280     (prog1
4281         (setq gnus-newsgroup-headers
4282               (sort articles (gnus-make-sort-function
4283                               gnus-article-sort-functions)))
4284       (gnus-message 7 "Sorting articles...done"))))
4285
4286 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4287 (defmacro gnus-thread-header (thread)
4288   "Return header of first article in THREAD.
4289 Note that THREAD must never, ever be anything else than a variable -
4290 using some other form will lead to serious barfage."
4291   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4292   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4293   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4294         (vector thread) 2))
4295
4296 (defsubst gnus-article-sort-by-number (h1 h2)
4297   "Sort articles by article number."
4298   (< (mail-header-number h1)
4299      (mail-header-number h2)))
4300
4301 (defun gnus-thread-sort-by-number (h1 h2)
4302   "Sort threads by root article number."
4303   (gnus-article-sort-by-number
4304    (gnus-thread-header h1) (gnus-thread-header h2)))
4305
4306 (defsubst gnus-article-sort-by-random (h1 h2)
4307   "Sort articles by article number."
4308   (zerop (random 2)))
4309
4310 (defun gnus-thread-sort-by-random (h1 h2)
4311   "Sort threads by root article number."
4312   (gnus-article-sort-by-random
4313    (gnus-thread-header h1) (gnus-thread-header h2)))
4314
4315 (defsubst gnus-article-sort-by-lines (h1 h2)
4316   "Sort articles by article Lines header."
4317   (< (mail-header-lines h1)
4318      (mail-header-lines h2)))
4319
4320 (defun gnus-thread-sort-by-lines (h1 h2)
4321   "Sort threads by root article Lines header."
4322   (gnus-article-sort-by-lines
4323    (gnus-thread-header h1) (gnus-thread-header h2)))
4324
4325 (defsubst gnus-article-sort-by-chars (h1 h2)
4326   "Sort articles by octet length."
4327   (< (mail-header-chars h1)
4328      (mail-header-chars h2)))
4329
4330 (defun gnus-thread-sort-by-chars (h1 h2)
4331   "Sort threads by root article octet length."
4332   (gnus-article-sort-by-chars
4333    (gnus-thread-header h1) (gnus-thread-header h2)))
4334
4335 (defsubst gnus-article-sort-by-author (h1 h2)
4336   "Sort articles by root author."
4337   (string-lessp
4338    (let ((extract (funcall
4339                    gnus-extract-address-components
4340                    (mail-header-from h1))))
4341      (or (car extract) (cadr extract) ""))
4342    (let ((extract (funcall
4343                    gnus-extract-address-components
4344                    (mail-header-from h2))))
4345      (or (car extract) (cadr extract) ""))))
4346
4347 (defun gnus-thread-sort-by-author (h1 h2)
4348   "Sort threads by root author."
4349   (gnus-article-sort-by-author
4350    (gnus-thread-header h1)  (gnus-thread-header h2)))
4351
4352 (defsubst gnus-article-sort-by-subject (h1 h2)
4353   "Sort articles by root subject."
4354   (string-lessp
4355    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4356    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4357
4358 (defun gnus-thread-sort-by-subject (h1 h2)
4359   "Sort threads by root subject."
4360   (gnus-article-sort-by-subject
4361    (gnus-thread-header h1) (gnus-thread-header h2)))
4362
4363 (defsubst gnus-article-sort-by-date (h1 h2)
4364   "Sort articles by root article date."
4365   (time-less-p
4366    (gnus-date-get-time (mail-header-date h1))
4367    (gnus-date-get-time (mail-header-date h2))))
4368
4369 (defun gnus-thread-sort-by-date (h1 h2)
4370   "Sort threads by root article date."
4371   (gnus-article-sort-by-date
4372    (gnus-thread-header h1) (gnus-thread-header h2)))
4373
4374 (defsubst gnus-article-sort-by-score (h1 h2)
4375   "Sort articles by root article score.
4376 Unscored articles will be counted as having a score of zero."
4377   (> (or (cdr (assq (mail-header-number h1)
4378                     gnus-newsgroup-scored))
4379          gnus-summary-default-score 0)
4380      (or (cdr (assq (mail-header-number h2)
4381                     gnus-newsgroup-scored))
4382          gnus-summary-default-score 0)))
4383
4384 (defun gnus-thread-sort-by-score (h1 h2)
4385   "Sort threads by root article score."
4386   (gnus-article-sort-by-score
4387    (gnus-thread-header h1) (gnus-thread-header h2)))
4388
4389 (defun gnus-thread-sort-by-total-score (h1 h2)
4390   "Sort threads by the sum of all scores in the thread.
4391 Unscored articles will be counted as having a score of zero."
4392   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4393
4394 (defun gnus-thread-total-score (thread)
4395   ;; This function find the total score of THREAD.
4396   (cond
4397    ((null thread)
4398     0)
4399    ((consp thread)
4400     (if (stringp (car thread))
4401         (apply gnus-thread-score-function 0
4402                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4403       (gnus-thread-total-score-1 thread)))
4404    (t
4405     (gnus-thread-total-score-1 (list thread)))))
4406
4407 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4408   "Sort threads such that the thread with the most recently arrived article comes first."
4409   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4410
4411 (defun gnus-thread-highest-number (thread)
4412   "Return the highest article number in THREAD."
4413   (apply 'max (mapcar (lambda (header)
4414                         (mail-header-number header))
4415                       (message-flatten-list thread))))
4416
4417 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4418   "Sort threads such that the thread with the most recently dated article comes first."
4419   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4420
4421 (defun gnus-thread-latest-date (thread)
4422   "Return the highest article date in THREAD."
4423   (let ((previous-time 0))
4424     (apply 'max
4425            (mapcar
4426             (lambda (header)
4427               (setq previous-time
4428                     (time-to-seconds
4429                      (condition-case ()
4430                          (mail-header-parse-date (mail-header-date header))
4431                        (error previous-time)))))
4432             (sort
4433              (message-flatten-list thread)
4434              (lambda (h1 h2)
4435                (< (mail-header-number h1)
4436                   (mail-header-number h2))))))))
4437
4438 (defun gnus-thread-total-score-1 (root)
4439   ;; This function find the total score of the thread below ROOT.
4440   (setq root (car root))
4441   (apply gnus-thread-score-function
4442          (or (append
4443               (mapcar 'gnus-thread-total-score
4444                       (cdr (gnus-id-to-thread (mail-header-id root))))
4445               (when (> (mail-header-number root) 0)
4446                 (list (or (cdr (assq (mail-header-number root)
4447                                      gnus-newsgroup-scored))
4448                           gnus-summary-default-score 0))))
4449              (list gnus-summary-default-score)
4450              '(0))))
4451
4452 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4453 (defvar gnus-tmp-prev-subject nil)
4454 (defvar gnus-tmp-false-parent nil)
4455 (defvar gnus-tmp-root-expunged nil)
4456 (defvar gnus-tmp-dummy-line nil)
4457
4458 (eval-when-compile (defvar gnus-tmp-header))
4459 (defun gnus-extra-header (type &optional header)
4460   "Return the extra header of TYPE."
4461   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4462       ""))
4463
4464 (defvar gnus-tmp-thread-tree-header-string "")
4465
4466 (defcustom gnus-sum-thread-tree-root "> "
4467   "With %B spec, used for the root of a thread.
4468 If nil, use subject instead."
4469   :type 'string
4470   :group 'gnus-thread)
4471 (defcustom gnus-sum-thread-tree-single-indent ""
4472   "With %B spec, used for a thread with just one message.
4473 If nil, use subject instead."
4474   :type 'string
4475   :group 'gnus-thread)
4476 (defcustom gnus-sum-thread-tree-vertical "| "
4477   "With %B spec, used for drawing a vertical line."
4478   :type 'string
4479   :group 'gnus-thread)
4480 (defcustom gnus-sum-thread-tree-indent "  "
4481   "With %B spec, used for indenting."
4482   :type 'string
4483   :group 'gnus-thread)
4484 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4485   "With %B spec, used for a leaf with brothers."
4486   :type 'string
4487   :group 'gnus-thread)
4488 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4489   "With %B spec, used for a leaf without brothers."
4490   :type 'string
4491   :group 'gnus-thread)
4492
4493 (defun gnus-summary-prepare-threads (threads)
4494   "Prepare summary buffer from THREADS and indentation LEVEL.
4495 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4496 or a straight list of headers."
4497   (gnus-message 7 "Generating summary...")
4498
4499   (setq gnus-newsgroup-threads threads)
4500   (beginning-of-line)
4501
4502   (let ((gnus-tmp-level 0)
4503         (default-score (or gnus-summary-default-score 0))
4504         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4505         (building-line-count gnus-summary-display-while-building)
4506         (building-count (integerp gnus-summary-display-while-building))
4507         thread number subject stack state gnus-tmp-gathered beg-match
4508         new-roots gnus-tmp-new-adopts thread-end simp-subject
4509         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4510         gnus-tmp-replied gnus-tmp-subject-or-nil
4511         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4512         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4513         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4514         tree-stack)
4515
4516     (setq gnus-tmp-prev-subject nil)
4517
4518     (if (vectorp (car threads))
4519         ;; If this is a straight (sic) list of headers, then a
4520         ;; threaded summary display isn't required, so we just create
4521         ;; an unthreaded one.
4522         (gnus-summary-prepare-unthreaded threads)
4523
4524       ;; Do the threaded display.
4525
4526       (if gnus-summary-display-while-building
4527           (switch-to-buffer (buffer-name)))
4528       (while (or threads stack gnus-tmp-new-adopts new-roots)
4529
4530         (if (and (= gnus-tmp-level 0)
4531                  (or (not stack)
4532                      (= (caar stack) 0))
4533                  (not gnus-tmp-false-parent)
4534                  (or gnus-tmp-new-adopts new-roots))
4535             (if gnus-tmp-new-adopts
4536                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4537                       thread (list (car gnus-tmp-new-adopts))
4538                       gnus-tmp-header (caar thread)
4539                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4540               (when new-roots
4541                 (setq thread (list (car new-roots))
4542                       gnus-tmp-header (caar thread)
4543                       new-roots (cdr new-roots))))
4544
4545           (if threads
4546               ;; If there are some threads, we do them before the
4547               ;; threads on the stack.
4548               (setq thread threads
4549                     gnus-tmp-header (caar thread))
4550             ;; There were no current threads, so we pop something off
4551             ;; the stack.
4552             (setq state (car stack)
4553                   gnus-tmp-level (car state)
4554                   tree-stack (cadr state)
4555                   thread (caddr state)
4556                   stack (cdr stack)
4557                   gnus-tmp-header (caar thread))))
4558
4559         (setq gnus-tmp-false-parent nil)
4560         (setq gnus-tmp-root-expunged nil)
4561         (setq thread-end nil)
4562
4563         (if (stringp gnus-tmp-header)
4564             ;; The header is a dummy root.
4565             (cond
4566              ((eq gnus-summary-make-false-root 'adopt)
4567               ;; We let the first article adopt the rest.
4568               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4569                                                (cddar thread)))
4570               (setq gnus-tmp-gathered
4571                     (nconc (mapcar
4572                             (lambda (h) (mail-header-number (car h)))
4573                             (cddar thread))
4574                            gnus-tmp-gathered))
4575               (setq thread (cons (list (caar thread)
4576                                        (cadar thread))
4577                                  (cdr thread)))
4578               (setq gnus-tmp-level -1
4579                     gnus-tmp-false-parent t))
4580              ((eq gnus-summary-make-false-root 'empty)
4581               ;; We print adopted articles with empty subject fields.
4582               (setq gnus-tmp-gathered
4583                     (nconc (mapcar
4584                             (lambda (h) (mail-header-number (car h)))
4585                             (cddar thread))
4586                            gnus-tmp-gathered))
4587               (setq gnus-tmp-level -1))
4588              ((eq gnus-summary-make-false-root 'dummy)
4589               ;; We remember that we probably want to output a dummy
4590               ;; root.
4591               (setq gnus-tmp-dummy-line gnus-tmp-header)
4592               (setq gnus-tmp-prev-subject gnus-tmp-header))
4593              (t
4594               ;; We do not make a root for the gathered
4595               ;; sub-threads at all.
4596               (setq gnus-tmp-level -1)))
4597
4598           (setq number (mail-header-number gnus-tmp-header)
4599                 subject (mail-header-subject gnus-tmp-header)
4600                 simp-subject (gnus-simplify-subject-fully subject))
4601
4602           (cond
4603            ;; If the thread has changed subject, we might want to make
4604            ;; this subthread into a root.
4605            ((and (null gnus-thread-ignore-subject)
4606                  (not (zerop gnus-tmp-level))
4607                  gnus-tmp-prev-subject
4608                  (not (string= gnus-tmp-prev-subject simp-subject)))
4609             (setq new-roots (nconc new-roots (list (car thread)))
4610                   thread-end t
4611                   gnus-tmp-header nil))
4612            ;; If the article lies outside the current limit,
4613            ;; then we do not display it.
4614            ((not (memq number gnus-newsgroup-limit))
4615             (setq gnus-tmp-gathered
4616                   (nconc (mapcar
4617                           (lambda (h) (mail-header-number (car h)))
4618                           (cdar thread))
4619                          gnus-tmp-gathered))
4620             (setq gnus-tmp-new-adopts (if (cdar thread)
4621                                           (append gnus-tmp-new-adopts
4622                                                   (cdar thread))
4623                                         gnus-tmp-new-adopts)
4624                   thread-end t
4625                   gnus-tmp-header nil)
4626             (when (zerop gnus-tmp-level)
4627               (setq gnus-tmp-root-expunged t)))
4628            ;; Perhaps this article is to be marked as read?
4629            ((and gnus-summary-mark-below
4630                  (< (or (cdr (assq number gnus-newsgroup-scored))
4631                         default-score)
4632                     gnus-summary-mark-below)
4633                  ;; Don't touch sparse articles.
4634                  (not (gnus-summary-article-sparse-p number))
4635                  (not (gnus-summary-article-ancient-p number)))
4636             (setq gnus-newsgroup-unreads
4637                   (delq number gnus-newsgroup-unreads))
4638             (if gnus-newsgroup-auto-expire
4639                 (setq gnus-newsgroup-expirable
4640                       (gnus-add-to-sorted-list
4641                        gnus-newsgroup-expirable number))
4642               (push (cons number gnus-low-score-mark)
4643                     gnus-newsgroup-reads))))
4644
4645           (when gnus-tmp-header
4646             ;; We may have an old dummy line to output before this
4647             ;; article.
4648             (when (and gnus-tmp-dummy-line
4649                        (gnus-subject-equal
4650                         gnus-tmp-dummy-line
4651                         (mail-header-subject gnus-tmp-header)))
4652               (gnus-summary-insert-dummy-line
4653                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4654               (setq gnus-tmp-dummy-line nil))
4655
4656             ;; Compute the mark.
4657             (setq gnus-tmp-unread (gnus-article-mark number))
4658
4659             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4660                                   gnus-tmp-header gnus-tmp-level)
4661                   gnus-newsgroup-data)
4662
4663             ;; Actually insert the line.
4664             (setq
4665              gnus-tmp-subject-or-nil
4666              (cond
4667               ((and gnus-thread-ignore-subject
4668                     gnus-tmp-prev-subject
4669                     (not (string= gnus-tmp-prev-subject simp-subject)))
4670                subject)
4671               ((zerop gnus-tmp-level)
4672                (if (and (eq gnus-summary-make-false-root 'empty)
4673                         (memq number gnus-tmp-gathered)
4674                         gnus-tmp-prev-subject
4675                         (string= gnus-tmp-prev-subject simp-subject))
4676                    gnus-summary-same-subject
4677                  subject))
4678               (t gnus-summary-same-subject)))
4679             (if (and (eq gnus-summary-make-false-root 'adopt)
4680                      (= gnus-tmp-level 1)
4681                      (memq number gnus-tmp-gathered))
4682                 (setq gnus-tmp-opening-bracket ?\<
4683                       gnus-tmp-closing-bracket ?\>)
4684               (setq gnus-tmp-opening-bracket ?\[
4685                     gnus-tmp-closing-bracket ?\]))
4686             (setq
4687              gnus-tmp-indentation
4688              (aref gnus-thread-indent-array gnus-tmp-level)
4689              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4690              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4691                                 gnus-summary-default-score 0)
4692              gnus-tmp-score-char
4693              (if (or (null gnus-summary-default-score)
4694                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4695                          gnus-summary-zcore-fuzz))
4696                  ?                      ;Whitespace
4697                (if (< gnus-tmp-score gnus-summary-default-score)
4698                    gnus-score-below-mark gnus-score-over-mark))
4699              gnus-tmp-replied
4700              (cond ((memq number gnus-newsgroup-processable)
4701                     gnus-process-mark)
4702                    ((memq number gnus-newsgroup-cached)
4703                     gnus-cached-mark)
4704                    ((memq number gnus-newsgroup-replied)
4705                     gnus-replied-mark)
4706                    ((memq number gnus-newsgroup-forwarded)
4707                     gnus-forwarded-mark)
4708                    ((memq number gnus-newsgroup-saved)
4709                     gnus-saved-mark)
4710                    ((memq number gnus-newsgroup-recent)
4711                     gnus-recent-mark)
4712                    ((memq number gnus-newsgroup-unseen)
4713                     gnus-unseen-mark)
4714                    (t gnus-no-mark))
4715              gnus-tmp-downloaded
4716              (cond ((memq number gnus-newsgroup-undownloaded) 
4717                     gnus-undownloaded-mark)
4718                    (gnus-newsgroup-agentized
4719                     gnus-downloaded-mark)
4720                    (t
4721                     gnus-no-mark))
4722              gnus-tmp-from (mail-header-from gnus-tmp-header)
4723              gnus-tmp-name
4724              (cond
4725               ((string-match "<[^>]+> *$" gnus-tmp-from)
4726                (setq beg-match (match-beginning 0))
4727                (or (and (string-match "^\".+\"" gnus-tmp-from)
4728                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4729                    (substring gnus-tmp-from 0 beg-match)))
4730               ((string-match "(.+)" gnus-tmp-from)
4731                (substring gnus-tmp-from
4732                           (1+ (match-beginning 0)) (1- (match-end 0))))
4733               (t gnus-tmp-from))
4734              gnus-tmp-thread-tree-header-string
4735              (cond
4736               ((not gnus-show-threads) "")
4737               ((zerop gnus-tmp-level)
4738                (if (cdar thread)
4739                    (or gnus-sum-thread-tree-root subject)
4740                  (or gnus-sum-thread-tree-single-indent subject)))
4741               (t
4742                (concat (apply 'concat
4743                               (mapcar (lambda (item)
4744                                         (if (= item 1)
4745                                             gnus-sum-thread-tree-vertical
4746                                           gnus-sum-thread-tree-indent))
4747                                       (cdr (reverse tree-stack))))
4748                        (if (nth 1 thread)
4749                            gnus-sum-thread-tree-leaf-with-other
4750                          gnus-sum-thread-tree-single-leaf)))))
4751             (when (string= gnus-tmp-name "")
4752               (setq gnus-tmp-name gnus-tmp-from))
4753             (unless (numberp gnus-tmp-lines)
4754               (setq gnus-tmp-lines -1))
4755             (if (= gnus-tmp-lines -1)
4756                 (setq gnus-tmp-lines "?")
4757               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4758             (gnus-put-text-property
4759              (point)
4760              (progn (eval gnus-summary-line-format-spec) (point))
4761              'gnus-number number)
4762             (when gnus-visual-p
4763               (forward-line -1)
4764               (gnus-run-hooks 'gnus-summary-update-hook)
4765               (forward-line 1))
4766
4767             (setq gnus-tmp-prev-subject simp-subject)))
4768
4769         (when (nth 1 thread)
4770           (push (list (max 0 gnus-tmp-level)
4771                       (copy-list tree-stack)
4772                       (nthcdr 1 thread))
4773                 stack))
4774         (push (if (nth 1 thread) 1 0) tree-stack)
4775         (incf gnus-tmp-level)
4776         (setq threads (if thread-end nil (cdar thread)))
4777         (if gnus-summary-display-while-building
4778             (if building-count
4779                 (progn
4780                   ;; use a set frequency
4781                   (setq building-line-count (1- building-line-count))
4782                   (when (= building-line-count 0)
4783                     (sit-for 0)
4784                     (setq building-line-count
4785                           gnus-summary-display-while-building)))
4786               ;; always
4787               (sit-for 0)))
4788         (unless threads
4789           (setq gnus-tmp-level 0)))))
4790   (gnus-message 7 "Generating summary...done"))
4791
4792 (defun gnus-summary-prepare-unthreaded (headers)
4793   "Generate an unthreaded summary buffer based on HEADERS."
4794   (let (header number mark)
4795
4796     (beginning-of-line)
4797
4798     (while headers
4799       ;; We may have to root out some bad articles...
4800       (when (memq (setq number (mail-header-number
4801                                 (setq header (pop headers))))
4802                   gnus-newsgroup-limit)
4803         ;; Mark article as read when it has a low score.
4804         (when (and gnus-summary-mark-below
4805                    (< (or (cdr (assq number gnus-newsgroup-scored))
4806                           gnus-summary-default-score 0)
4807                       gnus-summary-mark-below)
4808                    (not (gnus-summary-article-ancient-p number)))
4809           (setq gnus-newsgroup-unreads
4810                 (delq number gnus-newsgroup-unreads))
4811           (if gnus-newsgroup-auto-expire
4812               (push number gnus-newsgroup-expirable)
4813             (push (cons number gnus-low-score-mark)
4814                   gnus-newsgroup-reads)))
4815
4816         (setq mark (gnus-article-mark number))
4817         (push (gnus-data-make number mark (1+ (point)) header 0)
4818               gnus-newsgroup-data)
4819         (gnus-summary-insert-line
4820          header 0 number
4821          (memq number gnus-newsgroup-undownloaded)
4822          mark (memq number gnus-newsgroup-replied)
4823          (memq number gnus-newsgroup-expirable)
4824          (mail-header-subject header) nil
4825          (cdr (assq number gnus-newsgroup-scored))
4826          (memq number gnus-newsgroup-processable))))))
4827
4828 (defun gnus-summary-remove-list-identifiers ()
4829   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4830   (let ((regexp (if (consp gnus-list-identifiers)
4831                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4832                   gnus-list-identifiers))
4833         changed subject)
4834     (when regexp
4835       (dolist (header gnus-newsgroup-headers)
4836         (setq subject (mail-header-subject header)
4837               changed nil)
4838         (while (string-match
4839                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4840                 subject)
4841           (setq subject
4842                 (concat (substring subject 0 (match-beginning 2))
4843                         (substring subject (match-end 0)))
4844                 changed t))
4845         (when (and changed
4846                    (string-match
4847                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4848           (setq subject
4849                 (concat (substring subject 0 (match-beginning 1))
4850                         (substring subject (match-end 1)))))
4851         (when changed
4852           (mail-header-set-subject header subject))))))
4853
4854 (defun gnus-fetch-headers (articles)
4855   "Fetch headers of ARTICLES."
4856   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4857     (gnus-message 5 "Fetching headers for %s..." name)
4858     (prog1
4859         (if (eq 'nov
4860                 (setq gnus-headers-retrieved-by
4861                       (gnus-retrieve-headers
4862                        articles gnus-newsgroup-name
4863                        ;; We might want to fetch old headers, but
4864                        ;; not if there is only 1 article.
4865                        (and (or (and
4866                                  (not (eq gnus-fetch-old-headers 'some))
4867                                  (not (numberp gnus-fetch-old-headers)))
4868                                 (> (length articles) 1))
4869                             gnus-fetch-old-headers))))
4870             (gnus-get-newsgroup-headers-xover
4871              articles nil nil gnus-newsgroup-name t)
4872           (gnus-get-newsgroup-headers))
4873       (gnus-message 5 "Fetching headers for %s...done" name))))
4874
4875 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4876   "Select newsgroup GROUP.
4877 If READ-ALL is non-nil, all articles in the group are selected.
4878 If SELECT-ARTICLES, only select those articles from GROUP."
4879   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4880          ;;!!! Dirty hack; should be removed.
4881          (gnus-summary-ignore-duplicates
4882           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4883               t
4884             gnus-summary-ignore-duplicates))
4885          (info (nth 2 entry))
4886          articles fetched-articles cached)
4887
4888     (unless (gnus-check-server
4889              (set (make-local-variable 'gnus-current-select-method)
4890                   (gnus-find-method-for-group group)))
4891       (error "Couldn't open server"))
4892
4893     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4894         (gnus-activate-group group)     ; Or we can activate it...
4895         (progn                          ; Or we bug out.
4896           (when (equal major-mode 'gnus-summary-mode)
4897             (kill-buffer (current-buffer)))
4898           (error "Couldn't activate group %s: %s"
4899                  group (gnus-status-message group))))
4900
4901     (unless (gnus-request-group group t)
4902       (when (equal major-mode 'gnus-summary-mode)
4903         (kill-buffer (current-buffer)))
4904       (error "Couldn't request group %s: %s"
4905              group (gnus-status-message group)))
4906
4907     (setq gnus-newsgroup-name group
4908           gnus-newsgroup-unselected nil
4909           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4910
4911     (let ((display (gnus-group-find-parameter group 'display)))
4912       (setq gnus-newsgroup-display
4913             (cond
4914              ((not (zerop (or (car-safe read-all) 0)))
4915               ;; The user entered the group with C-u SPC/RET, let's show
4916               ;; all articles.
4917               'gnus-not-ignore)
4918              ((eq display 'all)
4919               'gnus-not-ignore)
4920              ((arrayp display)
4921               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4922              ((numberp display)
4923               ;; The following is probably the "correct" solution, but
4924               ;; it makes Gnus fetch all headers and then limit the
4925               ;; articles (which is slow), so instead we hack the
4926               ;; select-articles parameter instead. -- Simon Josefsson
4927               ;; <jas@kth.se>
4928               ;;
4929               ;; (gnus-byte-compile
4930               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4931               ;;                         display)))))
4932               (setq select-articles
4933                     (gnus-uncompress-range
4934                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4935                              (if (> tmp 0)
4936                                  tmp
4937                                1))
4938                            (cdr (gnus-active group)))))
4939               nil)
4940              (t
4941               nil))))
4942
4943     (gnus-summary-setup-default-charset)
4944
4945     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4946     (when (gnus-virtual-group-p group)
4947       (setq cached gnus-newsgroup-cached))
4948
4949     (setq gnus-newsgroup-unreads
4950           (gnus-sorted-ndifference
4951            (gnus-sorted-ndifference gnus-newsgroup-unreads
4952                                     gnus-newsgroup-marked)
4953            gnus-newsgroup-dormant))
4954
4955     (setq gnus-newsgroup-processable nil)
4956
4957     (gnus-update-read-articles group gnus-newsgroup-unreads)
4958
4959     ;; Adjust and set lists of article marks.
4960     (when info
4961       (gnus-adjust-marked-articles info))
4962     (if (setq articles select-articles)
4963         (setq gnus-newsgroup-unselected
4964               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4965       (setq articles (gnus-articles-to-read group read-all)))
4966
4967     (cond
4968      ((null articles)
4969       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4970       'quit)
4971      ((eq articles 0) nil)
4972      (t
4973       ;; Init the dependencies hash table.
4974       (setq gnus-newsgroup-dependencies
4975             (gnus-make-hashtable (length articles)))
4976       (gnus-set-global-variables)
4977       ;; Retrieve the headers and read them in.
4978
4979       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4980
4981       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4982       (when cached
4983         (setq gnus-newsgroup-cached cached))
4984
4985       ;; Suppress duplicates?
4986       (when gnus-suppress-duplicates
4987         (gnus-dup-suppress-articles))
4988
4989       ;; Set the initial limit.
4990       (setq gnus-newsgroup-limit (copy-sequence articles))
4991       ;; Remove canceled articles from the list of unread articles.
4992       (setq fetched-articles
4993             (mapcar (lambda (headers) (mail-header-number headers))
4994                     gnus-newsgroup-headers))
4995       (setq gnus-newsgroup-articles fetched-articles)
4996       (setq gnus-newsgroup-unreads
4997             (gnus-sorted-nintersection
4998              gnus-newsgroup-unreads fetched-articles))
4999       (gnus-compute-unseen-list)
5000
5001       ;; Removed marked articles that do not exist.
5002       (gnus-update-missing-marks
5003        (gnus-sorted-difference articles fetched-articles))
5004       ;; We might want to build some more threads first.
5005       (when (and gnus-fetch-old-headers
5006                  (eq gnus-headers-retrieved-by 'nov))
5007         (if (eq gnus-fetch-old-headers 'invisible)
5008             (gnus-build-all-threads)
5009           (gnus-build-old-threads)))
5010       ;; Let the Gnus agent mark articles as read.
5011       (when gnus-agent
5012         (gnus-agent-get-undownloaded-list))
5013       ;; Remove list identifiers from subject
5014       (when gnus-list-identifiers
5015         (gnus-summary-remove-list-identifiers))
5016       ;; Check whether auto-expire is to be done in this group.
5017       (setq gnus-newsgroup-auto-expire
5018             (gnus-group-auto-expirable-p group))
5019       ;; Set up the article buffer now, if necessary.
5020       (unless gnus-single-article-buffer
5021         (gnus-article-setup-buffer))
5022       ;; First and last article in this newsgroup.
5023       (when gnus-newsgroup-headers
5024         (setq gnus-newsgroup-begin
5025               (mail-header-number (car gnus-newsgroup-headers))
5026               gnus-newsgroup-end
5027               (mail-header-number
5028                (gnus-last-element gnus-newsgroup-headers))))
5029       ;; GROUP is successfully selected.
5030       (or gnus-newsgroup-headers t)))))
5031
5032 (defun gnus-compute-unseen-list ()
5033   ;; The `seen' marks are treated specially.
5034   (if (not gnus-newsgroup-seen)
5035       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5036     (setq gnus-newsgroup-unseen
5037           (gnus-inverse-list-range-intersection
5038            gnus-newsgroup-articles gnus-newsgroup-seen))))
5039
5040 (defun gnus-summary-display-make-predicate (display)
5041   (require 'gnus-agent)
5042   (when (= (length display) 1)
5043     (setq display (car display)))
5044   (unless gnus-summary-display-cache
5045     (dolist (elem (append '((unread . unread)
5046                             (read . read)
5047                             (unseen . unseen))
5048                           gnus-article-mark-lists))
5049       (push (cons (cdr elem)
5050                   (gnus-byte-compile
5051                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5052             gnus-summary-display-cache)))
5053   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5054         (gnus-category-predicate-cache gnus-summary-display-cache))
5055     (gnus-get-predicate display)))
5056
5057 ;; Uses the dynamically bound `number' variable.
5058 (eval-when-compile
5059   (defvar number))
5060 (defun gnus-article-marked-p (type &optional article)
5061   (let ((article (or article number)))
5062     (cond
5063      ((eq type 'tick)
5064       (memq article gnus-newsgroup-marked))
5065      ((eq type 'spam)
5066       (memq article gnus-newsgroup-spam-marked))
5067      ((eq type 'unsend)
5068       (memq article gnus-newsgroup-unsendable))
5069      ((eq type 'undownload)
5070       (memq article gnus-newsgroup-undownloaded))
5071      ((eq type 'download)
5072       (memq article gnus-newsgroup-downloadable))
5073      ((eq type 'unread)
5074       (memq article gnus-newsgroup-unreads))
5075      ((eq type 'read)
5076       (memq article gnus-newsgroup-reads))
5077      ((eq type 'dormant)
5078       (memq article gnus-newsgroup-dormant) )
5079      ((eq type 'expire)
5080       (memq article gnus-newsgroup-expirable))
5081      ((eq type 'reply)
5082       (memq article gnus-newsgroup-replied))
5083      ((eq type 'killed)
5084       (memq article gnus-newsgroup-killed))
5085      ((eq type 'bookmark)
5086       (assq article gnus-newsgroup-bookmarks))
5087      ((eq type 'score)
5088       (assq article gnus-newsgroup-scored))
5089      ((eq type 'save)
5090       (memq article gnus-newsgroup-saved))
5091      ((eq type 'cache)
5092       (memq article gnus-newsgroup-cached))
5093      ((eq type 'forward)
5094       (memq article gnus-newsgroup-forwarded))
5095      ((eq type 'seen)
5096       (not (memq article gnus-newsgroup-unseen)))
5097      ((eq type 'recent)
5098       (memq article gnus-newsgroup-recent))
5099      (t t))))
5100
5101 (defun gnus-articles-to-read (group &optional read-all)
5102   "Find out what articles the user wants to read."
5103   (let* ((display (gnus-group-find-parameter group 'display))
5104          (articles
5105           ;; Select all articles if `read-all' is non-nil, or if there
5106           ;; are no unread articles.
5107           (if (or read-all
5108                   (and (zerop (length gnus-newsgroup-marked))
5109                        (zerop (length gnus-newsgroup-unreads)))
5110                   ;; Fetch all if the predicate is non-nil.
5111                   gnus-newsgroup-display)
5112               ;; We want to select the headers for all the articles in
5113               ;; the group, so we select either all the active
5114               ;; articles in the group, or (if that's nil), the
5115               ;; articles in the cache.
5116               (or
5117                (gnus-uncompress-range (gnus-active group))
5118                (gnus-cache-articles-in-group group))
5119             ;; Select only the "normal" subset of articles.
5120             (gnus-sorted-nunion
5121              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5122              gnus-newsgroup-unreads)))
5123          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5124          (scored (length scored-list))
5125          (number (length articles))
5126          (marked (+ (length gnus-newsgroup-marked)
5127                     (length gnus-newsgroup-dormant)))
5128          (select
5129           (cond
5130            ((numberp read-all)
5131             read-all)
5132            ((numberp gnus-newsgroup-display)
5133             gnus-newsgroup-display)
5134            (t
5135             (condition-case ()
5136                 (cond
5137                  ((and (or (<= scored marked) (= scored number))
5138                        (numberp gnus-large-newsgroup)
5139                        (> number gnus-large-newsgroup))
5140                   (let* ((cursor-in-echo-area nil)
5141                          (initial (gnus-parameter-large-newsgroup-initial
5142                                    gnus-newsgroup-name))
5143                          (input
5144                           (read-string
5145                            (format
5146                             "How many articles from %s (%s %d): "
5147                             (gnus-limit-string
5148                              (gnus-group-decoded-name gnus-newsgroup-name)
5149                              35)
5150                             (if initial "max" "default")
5151                             number)
5152                            (if initial
5153                                (cons (number-to-string initial)
5154                                      0)))))
5155                     (if (string-match "^[ \t]*$" input) number input)))
5156                  ((and (> scored marked) (< scored number)
5157                        (> (- scored number) 20))
5158                   (let ((input
5159                          (read-string
5160                           (format "%s %s (%d scored, %d total): "
5161                                   "How many articles from"
5162                                   (gnus-group-decoded-name group)
5163                                   scored number))))
5164                     (if (string-match "^[ \t]*$" input)
5165                         number input)))
5166                  (t number))
5167               (quit
5168                (message "Quit getting the articles to read")
5169                nil))))))
5170     (setq select (if (stringp select) (string-to-number select) select))
5171     (if (or (null select) (zerop select))
5172         select
5173       (if (and (not (zerop scored)) (<= (abs select) scored))
5174           (progn
5175             (setq articles (sort scored-list '<))
5176             (setq number (length articles)))
5177         (setq articles (copy-sequence articles)))
5178
5179       (when (< (abs select) number)
5180         (if (< select 0)
5181             ;; Select the N oldest articles.
5182             (setcdr (nthcdr (1- (abs select)) articles) nil)
5183           ;; Select the N most recent articles.
5184           (setq articles (nthcdr (- number select) articles))))
5185       (setq gnus-newsgroup-unselected
5186             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5187       (when gnus-alter-articles-to-read-function
5188         (setq gnus-newsgroup-unreads
5189               (sort
5190                (funcall gnus-alter-articles-to-read-function
5191                         gnus-newsgroup-name gnus-newsgroup-unreads)
5192                '<)))
5193       articles)))
5194
5195 (defun gnus-killed-articles (killed articles)
5196   (let (out)
5197     (while articles
5198       (when (inline (gnus-member-of-range (car articles) killed))
5199         (push (car articles) out))
5200       (setq articles (cdr articles)))
5201     out))
5202
5203 (defun gnus-uncompress-marks (marks)
5204   "Uncompress the mark ranges in MARKS."
5205   (let ((uncompressed '(score bookmark))
5206         out)
5207     (while marks
5208       (if (memq (caar marks) uncompressed)
5209           (push (car marks) out)
5210         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5211       (setq marks (cdr marks)))
5212     out))
5213
5214 (defun gnus-article-mark-to-type (mark)
5215   "Return the type of MARK."
5216   (or (cadr (assq mark gnus-article-special-mark-lists))
5217       'list))
5218
5219 (defun gnus-article-unpropagatable-p (mark)
5220   "Return whether MARK should be propagated to backend."
5221   (memq mark gnus-article-unpropagated-mark-lists))
5222
5223 (defun gnus-adjust-marked-articles (info)
5224   "Set all article lists and remove all marks that are no longer valid."
5225   (let* ((marked-lists (gnus-info-marks info))
5226          (active (gnus-active (gnus-info-group info)))
5227          (min (car active))
5228          (max (cdr active))
5229          (types gnus-article-mark-lists)
5230          marks var articles article mark mark-type)
5231
5232     (dolist (marks marked-lists)
5233       (setq mark (car marks)
5234             mark-type (gnus-article-mark-to-type mark)
5235             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5236
5237       ;; We set the variable according to the type of the marks list,
5238       ;; and then adjust the marks to a subset of the active articles.
5239       (cond
5240        ;; Adjust "simple" lists.
5241        ((eq mark-type 'list)
5242         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5243         (when (memq mark '(tick dormant expire reply save))
5244           (while articles
5245             (when (or (< (setq article (pop articles)) min) (> article max))
5246               (set var (delq article (symbol-value var)))))))
5247        ;; Adjust assocs.
5248        ((eq mark-type 'tuple)
5249         (set var (setq articles (cdr marks)))
5250         (when (not (listp (cdr (symbol-value var))))
5251           (set var (list (symbol-value var))))
5252         (when (not (listp (cdr articles)))
5253           (setq articles (list articles)))
5254         (while articles
5255           (when (or (not (consp (setq article (pop articles))))
5256                     (< (car article) min)
5257                     (> (car article) max))
5258             (set var (delq article (symbol-value var))))))
5259        ;; Adjust ranges (sloppily).
5260        ((eq mark-type 'range)
5261         (cond
5262          ((eq mark 'seen)
5263           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5264           ;; It should be (seen (NUM1 . NUM2)).
5265           (when (numberp (cddr marks))
5266             (setcdr marks (list (cdr marks))))
5267           (setq articles (cdr marks))
5268           (while (and articles
5269                       (or (and (consp (car articles))
5270                                (> min (cdar articles)))
5271                           (and (numberp (car articles))
5272                                (> min (car articles)))))
5273             (pop articles))
5274           (set var articles))))))))
5275
5276 (defun gnus-update-missing-marks (missing)
5277   "Go through the list of MISSING articles and remove them from the mark lists."
5278   (when missing
5279     (let (var m)
5280       ;; Go through all types.
5281       (dolist (elem gnus-article-mark-lists)
5282         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5283           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5284           (when (symbol-value var)
5285             ;; This list has articles.  So we delete all missing
5286             ;; articles from it.
5287             (setq m missing)
5288             (while m
5289               (set var (delq (pop m) (symbol-value var))))))))))
5290
5291 (defun gnus-update-marks ()
5292   "Enter the various lists of marked articles into the newsgroup info list."
5293   (let ((types gnus-article-mark-lists)
5294         (info (gnus-get-info gnus-newsgroup-name))
5295         type list newmarked symbol delta-marks)
5296     (when info
5297       ;; Add all marks lists to the list of marks lists.
5298       (while (setq type (pop types))
5299         (setq list (symbol-value
5300                     (setq symbol
5301                           (intern (format "gnus-newsgroup-%s" (car type))))))
5302
5303         (when list
5304           ;; Get rid of the entries of the articles that have the
5305           ;; default score.
5306           (when (and (eq (cdr type) 'score)
5307                      gnus-save-score
5308                      list)
5309             (let* ((arts list)
5310                    (prev (cons nil list))
5311                    (all prev))
5312               (while arts
5313                 (if (or (not (consp (car arts)))
5314                         (= (cdar arts) gnus-summary-default-score))
5315                     (setcdr prev (cdr arts))
5316                   (setq prev arts))
5317                 (setq arts (cdr arts)))
5318               (setq list (cdr all)))))
5319
5320         (when (eq (cdr type) 'seen)
5321           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5322
5323         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5324           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5325
5326         (when (and (gnus-check-backend-function
5327                     'request-set-mark gnus-newsgroup-name)
5328                    (not (gnus-article-unpropagatable-p (cdr type))))
5329           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5330                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5331                  (add (gnus-remove-from-range
5332                        (gnus-copy-sequence list) old)))
5333             (when add
5334               (push (list add 'add (list (cdr type))) delta-marks))
5335             (when del
5336               (push (list del 'del (list (cdr type))) delta-marks))))
5337
5338         (when list
5339           (push (cons (cdr type) list) newmarked)))
5340
5341       (when delta-marks
5342         (unless (gnus-check-group gnus-newsgroup-name)
5343           (error "Can't open server for %s" gnus-newsgroup-name))
5344         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5345
5346       ;; Enter these new marks into the info of the group.
5347       (if (nthcdr 3 info)
5348           (setcar (nthcdr 3 info) newmarked)
5349         ;; Add the marks lists to the end of the info.
5350         (when newmarked
5351           (setcdr (nthcdr 2 info) (list newmarked))))
5352
5353       ;; Cut off the end of the info if there's nothing else there.
5354       (let ((i 5))
5355         (while (and (> i 2)
5356                     (not (nth i info)))
5357           (when (nthcdr (decf i) info)
5358             (setcdr (nthcdr i info) nil)))))))
5359
5360 (defun gnus-set-mode-line (where)
5361   "Set the mode line of the article or summary buffers.
5362 If WHERE is `summary', the summary mode line format will be used."
5363   ;; Is this mode line one we keep updated?
5364   (when (and (memq where gnus-updated-mode-lines)
5365              (symbol-value
5366               (intern (format "gnus-%s-mode-line-format-spec" where))))
5367     (let (mode-string)
5368       (save-excursion
5369         ;; We evaluate this in the summary buffer since these
5370         ;; variables are buffer-local to that buffer.
5371         (set-buffer gnus-summary-buffer)
5372        ;; We bind all these variables that are used in the `eval' form
5373         ;; below.
5374         (let* ((mformat (symbol-value
5375                          (intern
5376                           (format "gnus-%s-mode-line-format-spec" where))))
5377                (gnus-tmp-group-name (gnus-group-decoded-name
5378                                      gnus-newsgroup-name))
5379                (gnus-tmp-article-number (or gnus-current-article 0))
5380                (gnus-tmp-unread gnus-newsgroup-unreads)
5381                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5382                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5383                (gnus-tmp-unread-and-unselected
5384                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5385                             (zerop gnus-tmp-unselected))
5386                        "")
5387                       ((zerop gnus-tmp-unselected)
5388                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5389                       (t (format "{%d(+%d) more}"
5390                                  gnus-tmp-unread-and-unticked
5391                                  gnus-tmp-unselected))))
5392                (gnus-tmp-subject
5393                 (if (and gnus-current-headers
5394                          (vectorp gnus-current-headers))
5395                     (gnus-mode-string-quote
5396                      (mail-header-subject gnus-current-headers))
5397                   ""))
5398                bufname-length max-len
5399                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5400           (setq mode-string (eval mformat))
5401           (setq bufname-length (if (string-match "%b" mode-string)
5402                                    (- (length
5403                                        (buffer-name
5404                                         (if (eq where 'summary)
5405                                             nil
5406                                           (get-buffer gnus-article-buffer))))
5407                                       2)
5408                                  0))
5409           (setq max-len (max 4 (if gnus-mode-non-string-length
5410                                    (- (window-width)
5411                                       gnus-mode-non-string-length
5412                                       bufname-length)
5413                                  (length mode-string))))
5414           ;; We might have to chop a bit of the string off...
5415           (when (> (length mode-string) max-len)
5416             (setq mode-string
5417                   (concat (truncate-string-to-width mode-string (- max-len 3))
5418                           "...")))
5419           ;; Pad the mode string a bit.
5420           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5421       ;; Update the mode line.
5422       (setq mode-line-buffer-identification
5423             (gnus-mode-line-buffer-identification (list mode-string)))
5424       (set-buffer-modified-p t))))
5425
5426 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5427   "Go through the HEADERS list and add all Xrefs to a hash table.
5428 The resulting hash table is returned, or nil if no Xrefs were found."
5429   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5430          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5431          (xref-hashtb (gnus-make-hashtable))
5432          start group entry number xrefs header)
5433     (while headers
5434       (setq header (pop headers))
5435       (when (and (setq xrefs (mail-header-xref header))
5436                  (not (memq (setq number (mail-header-number header))
5437                             unreads)))
5438         (setq start 0)
5439         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5440           (setq start (match-end 0))
5441           (setq group (if prefix
5442                           (concat prefix (substring xrefs (match-beginning 1)
5443                                                     (match-end 1)))
5444                         (substring xrefs (match-beginning 1) (match-end 1))))
5445           (setq number
5446                 (string-to-int (substring xrefs (match-beginning 2)
5447                                           (match-end 2))))
5448           (if (setq entry (gnus-gethash group xref-hashtb))
5449               (setcdr entry (cons number (cdr entry)))
5450             (gnus-sethash group (cons number nil) xref-hashtb)))))
5451     (and start xref-hashtb)))
5452
5453 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5454   "Look through all the headers and mark the Xrefs as read."
5455   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5456         name entry info xref-hashtb idlist method nth4)
5457     (save-excursion
5458       (set-buffer gnus-group-buffer)
5459       (when (setq xref-hashtb
5460                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5461         (mapatoms
5462          (lambda (group)
5463            (unless (string= from-newsgroup (setq name (symbol-name group)))
5464              (setq idlist (symbol-value group))
5465              ;; Dead groups are not updated.
5466              (and (prog1
5467                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5468                             info (nth 2 entry))
5469                     (when (stringp (setq nth4 (gnus-info-method info)))
5470                       (setq nth4 (gnus-server-to-method nth4))))
5471                   ;; Only do the xrefs if the group has the same
5472                   ;; select method as the group we have just read.
5473                   (or (gnus-methods-equal-p
5474                        nth4 (gnus-find-method-for-group from-newsgroup))
5475                       virtual
5476                       (equal nth4 (setq method (gnus-find-method-for-group
5477                                                 from-newsgroup)))
5478                       (and (equal (car nth4) (car method))
5479                            (equal (nth 1 nth4) (nth 1 method))))
5480                   gnus-use-cross-reference
5481                   (or (not (eq gnus-use-cross-reference t))
5482                       virtual
5483                       ;; Only do cross-references on subscribed
5484                       ;; groups, if that is what is wanted.
5485                       (<= (gnus-info-level info) gnus-level-subscribed))
5486                   (gnus-group-make-articles-read name idlist))))
5487          xref-hashtb)))))
5488
5489 (defun gnus-compute-read-articles (group articles)
5490   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5491          (info (nth 2 entry))
5492          (active (gnus-active group))
5493          ninfo)
5494     (when entry
5495       ;; First peel off all invalid article numbers.
5496       (when active
5497         (let ((ids articles)
5498               id first)
5499           (while (setq id (pop ids))
5500             (when (and first (> id (cdr active)))
5501               ;; We'll end up in this situation in one particular
5502               ;; obscure situation.  If you re-scan a group and get
5503               ;; a new article that is cross-posted to a different
5504               ;; group that has not been re-scanned, you might get
5505               ;; crossposted article that has a higher number than
5506               ;; Gnus believes possible.  So we re-activate this
5507               ;; group as well.  This might mean doing the
5508               ;; crossposting thingy will *increase* the number
5509               ;; of articles in some groups.  Tsk, tsk.
5510               (setq active (or (gnus-activate-group group) active)))
5511             (when (or (> id (cdr active))
5512                       (< id (car active)))
5513               (setq articles (delq id articles))))))
5514       ;; If the read list is nil, we init it.
5515       (if (and active
5516                (null (gnus-info-read info))
5517                (> (car active) 1))
5518           (setq ninfo (cons 1 (1- (car active))))
5519         (setq ninfo (gnus-info-read info)))
5520       ;; Then we add the read articles to the range.
5521       (gnus-add-to-range
5522        ninfo (setq articles (sort articles '<))))))
5523
5524 (defun gnus-group-make-articles-read (group articles)
5525   "Update the info of GROUP to say that ARTICLES are read."
5526   (let* ((num 0)
5527          (entry (gnus-gethash group gnus-newsrc-hashtb))
5528          (info (nth 2 entry))
5529          (active (gnus-active group))
5530          range)
5531     (when entry
5532       (setq range (gnus-compute-read-articles group articles))
5533       (save-excursion
5534         (set-buffer gnus-group-buffer)
5535         (gnus-undo-register
5536           `(progn
5537              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5538              (gnus-info-set-read ',info ',(gnus-info-read info))
5539              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5540              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5541              (gnus-group-update-group ,group t))))
5542       ;; Add the read articles to the range.
5543       (gnus-info-set-read info range)
5544       (gnus-request-set-mark group (list (list range 'add '(read))))
5545       ;; Then we have to re-compute how many unread
5546       ;; articles there are in this group.
5547       (when active
5548         (cond
5549          ((not range)
5550           (setq num (- (1+ (cdr active)) (car active))))
5551          ((not (listp (cdr range)))
5552           (setq num (- (cdr active) (- (1+ (cdr range))
5553                                        (car range)))))
5554          (t
5555           (while range
5556             (if (numberp (car range))
5557                 (setq num (1+ num))
5558               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5559             (setq range (cdr range)))
5560           (setq num (- (cdr active) num))))
5561         ;; Update the number of unread articles.
5562         (setcar entry num)
5563         ;; Update the group buffer.
5564         (unless (gnus-ephemeral-group-p group)
5565           (gnus-group-update-group group t))))))
5566
5567 (defvar gnus-newsgroup-none-id 0)
5568
5569 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5570   (let ((cur nntp-server-buffer)
5571         (dependencies
5572          (or dependencies
5573              (save-excursion (set-buffer gnus-summary-buffer)
5574                              gnus-newsgroup-dependencies)))
5575         headers id end ref
5576         (mail-parse-charset gnus-newsgroup-charset)
5577         (mail-parse-ignored-charsets
5578          (save-excursion (condition-case nil
5579                              (set-buffer gnus-summary-buffer)
5580                            (error))
5581                          gnus-newsgroup-ignored-charsets)))
5582     (save-excursion
5583       (set-buffer nntp-server-buffer)
5584       ;; Translate all TAB characters into SPACE characters.
5585       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5586       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5587       (gnus-run-hooks 'gnus-parse-headers-hook)
5588       (let ((case-fold-search t)
5589             in-reply-to header p lines chars)
5590         (goto-char (point-min))
5591         ;; Search to the beginning of the next header.  Error messages
5592         ;; do not begin with 2 or 3.
5593         (while (re-search-forward "^[23][0-9]+ " nil t)
5594           (setq id nil
5595                 ref nil)
5596           ;; This implementation of this function, with nine
5597           ;; search-forwards instead of the one re-search-forward and
5598           ;; a case (which basically was the old function) is actually
5599           ;; about twice as fast, even though it looks messier.  You
5600           ;; can't have everything, I guess.  Speed and elegance
5601           ;; doesn't always go hand in hand.
5602           (setq
5603            header
5604            (vector
5605             ;; Number.
5606             (prog1
5607                 (read cur)
5608               (end-of-line)
5609               (setq p (point))
5610               (narrow-to-region (point)
5611                                 (or (and (search-forward "\n.\n" nil t)
5612                                          (- (point) 2))
5613                                     (point))))
5614             ;; Subject.
5615             (progn
5616               (goto-char p)
5617               (if (search-forward "\nsubject:" nil t)
5618                   (funcall gnus-decode-encoded-word-function
5619                            (nnheader-header-value))
5620                 "(none)"))
5621             ;; From.
5622             (progn
5623               (goto-char p)
5624               (if (search-forward "\nfrom:" nil t)
5625                   (funcall gnus-decode-encoded-word-function
5626                            (nnheader-header-value))
5627                 "(nobody)"))
5628             ;; Date.
5629             (progn
5630               (goto-char p)
5631               (if (search-forward "\ndate:" nil t)
5632                   (nnheader-header-value) ""))
5633             ;; Message-ID.
5634             (progn
5635               (goto-char p)
5636               (setq id (if (re-search-forward
5637                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5638                            ;; We do it this way to make sure the Message-ID
5639                            ;; is (somewhat) syntactically valid.
5640                            (buffer-substring (match-beginning 1)
5641                                              (match-end 1))
5642                          ;; If there was no message-id, we just fake one
5643                          ;; to make subsequent routines simpler.
5644                          (nnheader-generate-fake-message-id))))
5645             ;; References.
5646             (progn
5647               (goto-char p)
5648               (if (search-forward "\nreferences:" nil t)
5649                   (progn
5650                     (setq end (point))
5651                     (prog1
5652                         (nnheader-header-value)
5653                       (setq ref
5654                             (buffer-substring
5655                              (progn
5656                                (end-of-line)
5657                                (search-backward ">" end t)
5658                                (1+ (point)))
5659                              (progn
5660                                (search-backward "<" end t)
5661                                (point))))))
5662                 ;; Get the references from the in-reply-to header if there
5663                 ;; were no references and the in-reply-to header looks
5664                 ;; promising.
5665                 (if (and (search-forward "\nin-reply-to:" nil t)
5666                          (setq in-reply-to (nnheader-header-value))
5667                          (string-match "<[^>]+>" in-reply-to))
5668                     (let (ref2)
5669                       (setq ref (substring in-reply-to (match-beginning 0)
5670                                            (match-end 0)))
5671                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5672                         (setq ref2 (substring in-reply-to (match-beginning 0)
5673                                               (match-end 0)))
5674                         (when (> (length ref2) (length ref))
5675                           (setq ref ref2)))
5676                       ref)
5677                   (setq ref nil))))
5678             ;; Chars.
5679             (progn
5680               (goto-char p)
5681               (if (search-forward "\nchars: " nil t)
5682                   (if (numberp (setq chars (ignore-errors (read cur))))
5683                       chars -1)
5684                 -1))
5685             ;; Lines.
5686             (progn
5687               (goto-char p)
5688               (if (search-forward "\nlines: " nil t)
5689                   (if (numberp (setq lines (ignore-errors (read cur))))
5690                       lines -1)
5691                 -1))
5692             ;; Xref.
5693             (progn
5694               (goto-char p)
5695               (and (search-forward "\nxref:" nil t)
5696                    (nnheader-header-value)))
5697             ;; Extra.
5698             (when gnus-extra-headers
5699               (let ((extra gnus-extra-headers)
5700                     out)
5701                 (while extra
5702                   (goto-char p)
5703                   (when (search-forward
5704                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5705                     (push (cons (car extra) (nnheader-header-value))
5706                           out))
5707                   (pop extra))
5708                 out))))
5709           (when (equal id ref)
5710             (setq ref nil))
5711
5712           (when gnus-alter-header-function
5713             (funcall gnus-alter-header-function header)
5714             (setq id (mail-header-id header)
5715                   ref (gnus-parent-id (mail-header-references header))))
5716
5717           (when (setq header
5718                       (gnus-dependencies-add-header
5719                        header dependencies force-new))
5720             (push header headers))
5721           (goto-char (point-max))
5722           (widen))
5723         (nreverse headers)))))
5724
5725 ;; Goes through the xover lines and returns a list of vectors
5726 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5727                                                   force-new dependencies
5728                                                   group also-fetch-heads)
5729   "Parse the news overview data in the server buffer.
5730 Return a list of headers that match SEQUENCE (see
5731 `nntp-retrieve-headers')."
5732   ;; Get the Xref when the users reads the articles since most/some
5733   ;; NNTP servers do not include Xrefs when using XOVER.
5734   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5735   (let ((mail-parse-charset gnus-newsgroup-charset)
5736         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5737         (cur nntp-server-buffer)
5738         (dependencies (or dependencies gnus-newsgroup-dependencies))
5739         (allp (cond
5740                ((eq gnus-read-all-available-headers t)
5741                 t)
5742                ((stringp gnus-read-all-available-headers)
5743                 (string-match gnus-read-all-available-headers group))
5744                (t
5745                 nil)))
5746         number headers header)
5747     (save-excursion
5748       (set-buffer nntp-server-buffer)
5749       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5750       ;; Allow the user to mangle the headers before parsing them.
5751       (gnus-run-hooks 'gnus-parse-headers-hook)
5752       (goto-char (point-min))
5753       (gnus-parse-without-error
5754         (while (and (or sequence allp)
5755                     (not (eobp)))
5756           (setq number (read cur))
5757           (when (not allp)
5758             (while (and sequence
5759                         (< (car sequence) number))
5760               (setq sequence (cdr sequence))))
5761           (when (and (or allp
5762                          (and sequence
5763                               (eq number (car sequence))))
5764                      (progn
5765                        (setq sequence (cdr sequence))
5766                        (setq header (inline
5767                                       (gnus-nov-parse-line
5768                                        number dependencies force-new)))))
5769             (push header headers))
5770           (forward-line 1)))
5771       ;; A common bug in inn is that if you have posted an article and
5772       ;; then retrieves the active file, it will answer correctly --
5773       ;; the new article is included.  However, a NOV entry for the
5774       ;; article may not have been generated yet, so this may fail.
5775       ;; We work around this problem by retrieving the last few
5776       ;; headers using HEAD.
5777       (if (or (not also-fetch-heads)
5778               (not sequence))
5779           ;; We (probably) got all the headers.
5780           (nreverse headers)
5781         (let ((gnus-nov-is-evil t))
5782           (nconc
5783            (nreverse headers)
5784            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5785              (gnus-get-newsgroup-headers))))))))
5786
5787 (defun gnus-article-get-xrefs ()
5788   "Fill in the Xref value in `gnus-current-headers', if necessary.
5789 This is meant to be called in `gnus-article-internal-prepare-hook'."
5790   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5791                                  gnus-current-headers)))
5792     (or (not gnus-use-cross-reference)
5793         (not headers)
5794         (and (mail-header-xref headers)
5795              (not (string= (mail-header-xref headers) "")))
5796         (let ((case-fold-search t)
5797               xref)
5798           (save-restriction
5799             (nnheader-narrow-to-headers)
5800             (goto-char (point-min))
5801             (when (or (and (not (eobp))
5802                            (eq (downcase (char-after)) ?x)
5803                            (looking-at "Xref:"))
5804                       (search-forward "\nXref:" nil t))
5805               (goto-char (1+ (match-end 0)))
5806               (setq xref (buffer-substring (point)
5807                                            (progn (end-of-line) (point))))
5808               (mail-header-set-xref headers xref)))))))
5809
5810 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5811   "Find article ID and insert the summary line for that article.
5812 OLD-HEADER can either be a header or a line number to insert
5813 the subject line on."
5814   (let* ((line (and (numberp old-header) old-header))
5815          (old-header (and (vectorp old-header) old-header))
5816          (header (cond ((and old-header use-old-header)
5817                         old-header)
5818                        ((and (numberp id)
5819                              (gnus-number-to-header id))
5820                         (gnus-number-to-header id))
5821                        (t
5822                         (gnus-read-header id))))
5823          (number (and (numberp id) id))
5824          d)
5825     (when header
5826       ;; Rebuild the thread that this article is part of and go to the
5827       ;; article we have fetched.
5828       (when (and (not gnus-show-threads)
5829                  old-header)
5830         (when (and number
5831                    (setq d (gnus-data-find (mail-header-number old-header))))
5832           (goto-char (gnus-data-pos d))
5833           (gnus-data-remove
5834            number
5835            (- (gnus-point-at-bol)
5836               (prog1
5837                   (1+ (gnus-point-at-eol))
5838                 (gnus-delete-line))))))
5839       (when old-header
5840         (mail-header-set-number header (mail-header-number old-header)))
5841       (setq gnus-newsgroup-sparse
5842             (delq (setq number (mail-header-number header))
5843                   gnus-newsgroup-sparse))
5844       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5845       (push number gnus-newsgroup-limit)
5846       (gnus-rebuild-thread (mail-header-id header) line)
5847       (gnus-summary-goto-subject number nil t))
5848     (when (and (numberp number)
5849                (> number 0))
5850       ;; We have to update the boundaries even if we can't fetch the
5851       ;; article if ID is a number -- so that the next `P' or `N'
5852       ;; command will fetch the previous (or next) article even
5853       ;; if the one we tried to fetch this time has been canceled.
5854       (when (> number gnus-newsgroup-end)
5855         (setq gnus-newsgroup-end number))
5856       (when (< number gnus-newsgroup-begin)
5857         (setq gnus-newsgroup-begin number))
5858       (setq gnus-newsgroup-unselected
5859             (delq number gnus-newsgroup-unselected)))
5860     ;; Report back a success?
5861     (and header (mail-header-number header))))
5862
5863 ;;; Process/prefix in the summary buffer
5864
5865 (defun gnus-summary-work-articles (n)
5866   "Return a list of articles to be worked upon.
5867 The prefix argument, the list of process marked articles, and the
5868 current article will be taken into consideration."
5869   (save-excursion
5870     (set-buffer gnus-summary-buffer)
5871     (cond
5872      (n
5873       ;; A numerical prefix has been given.
5874       (setq n (prefix-numeric-value n))
5875       (let ((backward (< n 0))
5876             (n (abs (prefix-numeric-value n)))
5877             articles article)
5878         (save-excursion
5879           (while
5880               (and (> n 0)
5881                    (push (setq article (gnus-summary-article-number))
5882                          articles)
5883                    (if backward
5884                        (gnus-summary-find-prev nil article)
5885                      (gnus-summary-find-next nil article)))
5886             (decf n)))
5887         (nreverse articles)))
5888      ((and (gnus-region-active-p) (mark))
5889       (message "region active")
5890       ;; Work on the region between point and mark.
5891       (let ((max (max (point) (mark)))
5892             articles article)
5893         (save-excursion
5894           (goto-char (min (min (point) (mark))))
5895           (while
5896               (and
5897                (push (setq article (gnus-summary-article-number)) articles)
5898                (gnus-summary-find-next nil article)
5899                (< (point) max)))
5900           (nreverse articles))))
5901      (gnus-newsgroup-processable
5902       ;; There are process-marked articles present.
5903       ;; Save current state.
5904       (gnus-summary-save-process-mark)
5905       ;; Return the list.
5906       (reverse gnus-newsgroup-processable))
5907      (t
5908       ;; Just return the current article.
5909       (list (gnus-summary-article-number))))))
5910
5911 (defmacro gnus-summary-iterate (arg &rest forms)
5912   "Iterate over the process/prefixed articles and do FORMS.
5913 ARG is the interactive prefix given to the command.  FORMS will be
5914 executed with point over the summary line of the articles."
5915   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5916     `(let ((,articles (gnus-summary-work-articles ,arg)))
5917        (while ,articles
5918          (gnus-summary-goto-subject (car ,articles))
5919          ,@forms
5920          (pop ,articles)))))
5921
5922 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5923 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5924
5925 (defun gnus-summary-save-process-mark ()
5926   "Push the current set of process marked articles on the stack."
5927   (interactive)
5928   (push (copy-sequence gnus-newsgroup-processable)
5929         gnus-newsgroup-process-stack))
5930
5931 (defun gnus-summary-kill-process-mark ()
5932   "Push the current set of process marked articles on the stack and unmark."
5933   (interactive)
5934   (gnus-summary-save-process-mark)
5935   (gnus-summary-unmark-all-processable))
5936
5937 (defun gnus-summary-yank-process-mark ()
5938   "Pop the last process mark state off the stack and restore it."
5939   (interactive)
5940   (unless gnus-newsgroup-process-stack
5941     (error "Empty mark stack"))
5942   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5943
5944 (defun gnus-summary-process-mark-set (set)
5945   "Make SET into the current process marked articles."
5946   (gnus-summary-unmark-all-processable)
5947   (while set
5948     (gnus-summary-set-process-mark (pop set))))
5949
5950 ;;; Searching and stuff
5951
5952 (defun gnus-summary-search-group (&optional backward use-level)
5953   "Search for next unread newsgroup.
5954 If optional argument BACKWARD is non-nil, search backward instead."
5955   (save-excursion
5956     (set-buffer gnus-group-buffer)
5957     (when (gnus-group-search-forward
5958            backward nil (if use-level (gnus-group-group-level) nil))
5959       (gnus-group-group-name))))
5960
5961 (defun gnus-summary-best-group (&optional exclude-group)
5962   "Find the name of the best unread group.
5963 If EXCLUDE-GROUP, do not go to this group."
5964   (save-excursion
5965     (set-buffer gnus-group-buffer)
5966     (save-excursion
5967       (gnus-group-best-unread-group exclude-group))))
5968
5969 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5970   (if backward (gnus-summary-find-prev)
5971     (let* ((dummy (gnus-summary-article-intangible-p))
5972            (article (or article (gnus-summary-article-number)))
5973            (arts (gnus-data-find-list article))
5974            result)
5975       (when (and (not dummy)
5976                  (or (not gnus-summary-check-current)
5977                      (not unread)
5978                      (not (gnus-data-unread-p (car arts)))))
5979         (setq arts (cdr arts)))
5980       (when (setq result
5981                   (if unread
5982                       (progn
5983                         (while arts
5984                           (when (or (and undownloaded
5985                                          (memq (car arts) gnus-newsgroup-undownloaded))
5986                                     (gnus-data-unread-p (car arts)))
5987                             (setq result (car arts)
5988                                   arts nil))
5989                           (setq arts (cdr arts)))
5990                         result)
5991                     (car arts)))
5992         (goto-char (gnus-data-pos result))
5993         (gnus-data-number result)))))
5994
5995 (defun gnus-summary-find-prev (&optional unread article)
5996   (let* ((eobp (eobp))
5997          (article (or article (gnus-summary-article-number)))
5998          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5999          result)
6000     (when (and (not eobp)
6001                (or (not gnus-summary-check-current)
6002                    (not unread)
6003                    (not (gnus-data-unread-p (car arts)))))
6004       (setq arts (cdr arts)))
6005     (when (setq result
6006                 (if unread
6007                     (progn
6008                       (while arts
6009                         (when (gnus-data-unread-p (car arts))
6010                           (setq result (car arts)
6011                                 arts nil))
6012                         (setq arts (cdr arts)))
6013                       result)
6014                   (car arts)))
6015       (goto-char (gnus-data-pos result))
6016       (gnus-data-number result))))
6017
6018 (defun gnus-summary-find-subject (subject &optional unread backward article)
6019   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6020          (article (or article (gnus-summary-article-number)))
6021          (articles (gnus-data-list backward))
6022          (arts (gnus-data-find-list article articles))
6023          result)
6024     (when (or (not gnus-summary-check-current)
6025               (not unread)
6026               (not (gnus-data-unread-p (car arts))))
6027       (setq arts (cdr arts)))
6028     (while arts
6029       (and (or (not unread)
6030                (gnus-data-unread-p (car arts)))
6031            (vectorp (gnus-data-header (car arts)))
6032            (gnus-subject-equal
6033             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6034            (setq result (car arts)
6035                  arts nil))
6036       (setq arts (cdr arts)))
6037     (and result
6038          (goto-char (gnus-data-pos result))
6039          (gnus-data-number result))))
6040
6041 (defun gnus-summary-search-forward (&optional unread subject backward)
6042   "Search forward for an article.
6043 If UNREAD, look for unread articles.  If SUBJECT, look for
6044 articles with that subject.  If BACKWARD, search backward instead."
6045   (cond (subject (gnus-summary-find-subject subject unread backward))
6046         (backward (gnus-summary-find-prev unread))
6047         (t (gnus-summary-find-next unread))))
6048
6049 (defun gnus-recenter (&optional n)
6050   "Center point in window and redisplay frame.
6051 Also do horizontal recentering."
6052   (interactive "P")
6053   (when (and gnus-auto-center-summary
6054              (not (eq gnus-auto-center-summary 'vertical)))
6055     (gnus-horizontal-recenter))
6056   (recenter n))
6057
6058 (defun gnus-summary-recenter ()
6059   "Center point in the summary window.
6060 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6061 displayed, no centering will be performed."
6062   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6063 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6064   (interactive)
6065   (let* ((top (cond ((< (window-height) 4) 0)
6066                     ((< (window-height) 7) 1)
6067                     (t (if (numberp gnus-auto-center-summary)
6068                            gnus-auto-center-summary
6069                          2))))
6070          (height (1- (window-height)))
6071          (bottom (save-excursion (goto-char (point-max))
6072                                  (forward-line (- height))
6073                                  (point)))
6074          (window (get-buffer-window (current-buffer))))
6075     ;; The user has to want it.
6076     (when gnus-auto-center-summary
6077       (when (get-buffer-window gnus-article-buffer)
6078         ;; Only do recentering when the article buffer is displayed,
6079       ;; Set the window start to either `bottom', which is the biggest
6080         ;; possible valid number, or the second line from the top,
6081         ;; whichever is the least.
6082         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6083           (if (> bottom top-pos)
6084               ;; Keep the second line from the top visible
6085               (set-window-start window top-pos t)
6086             ;; Try to keep the bottom line visible; if it's partially
6087             ;; obscured, either scroll one more line to make it fully
6088             ;; visible, or revert to using TOP-POS.
6089             (save-excursion
6090               (goto-char (point-max))
6091               (forward-line -1)
6092               (let ((last-line-start (point)))
6093                 (goto-char bottom)
6094                 (set-window-start window (point) t)
6095                 (when (not (pos-visible-in-window-p last-line-start window))
6096                   (forward-line 1)
6097                   (set-window-start window (min (point) top-pos) t)))))))
6098       ;; Do horizontal recentering while we're at it.
6099       (when (and (get-buffer-window (current-buffer) t)
6100                  (not (eq gnus-auto-center-summary 'vertical)))
6101         (let ((selected (selected-window)))
6102           (select-window (get-buffer-window (current-buffer) t))
6103           (gnus-summary-position-point)
6104           (gnus-horizontal-recenter)
6105           (select-window selected))))))
6106
6107 (defun gnus-summary-jump-to-group (newsgroup)
6108   "Move point to NEWSGROUP in group mode buffer."
6109   ;; Keep update point of group mode buffer if visible.
6110   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6111       (save-window-excursion
6112         ;; Take care of tree window mode.
6113         (when (get-buffer-window gnus-group-buffer)
6114           (pop-to-buffer gnus-group-buffer))
6115         (gnus-group-jump-to-group newsgroup))
6116     (save-excursion
6117       ;; Take care of tree window mode.
6118       (if (get-buffer-window gnus-group-buffer)
6119           (pop-to-buffer gnus-group-buffer)
6120         (set-buffer gnus-group-buffer))
6121       (gnus-group-jump-to-group newsgroup))))
6122
6123 ;; This function returns a list of article numbers based on the
6124 ;; difference between the ranges of read articles in this group and
6125 ;; the range of active articles.
6126 (defun gnus-list-of-unread-articles (group)
6127   (let* ((read (gnus-info-read (gnus-get-info group)))
6128          (active (or (gnus-active group) (gnus-activate-group group)))
6129          (last (cdr active))
6130          first nlast unread)
6131     ;; If none are read, then all are unread.
6132     (if (not read)
6133         (setq first (car active))
6134       ;; If the range of read articles is a single range, then the
6135       ;; first unread article is the article after the last read
6136       ;; article.  Sounds logical, doesn't it?
6137       (if (and (not (listp (cdr read)))
6138                (or (< (car read) (car active))
6139                    (progn (setq read (list read))
6140                           nil)))
6141           (setq first (max (car active) (1+ (cdr read))))
6142         ;; `read' is a list of ranges.
6143         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6144                                   (caar read)))
6145                   1)
6146           (setq first (car active)))
6147         (while read
6148           (when first
6149             (while (< first nlast)
6150               (push first unread)
6151               (setq first (1+ first))))
6152           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6153           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6154           (setq read (cdr read)))))
6155     ;; And add the last unread articles.
6156     (while (<= first last)
6157       (push first unread)
6158       (setq first (1+ first)))
6159     ;; Return the list of unread articles.
6160     (delq 0 (nreverse unread))))
6161
6162 (defun gnus-list-of-read-articles (group)
6163   "Return a list of unread, unticked and non-dormant articles."
6164   (let* ((info (gnus-get-info group))
6165          (marked (gnus-info-marks info))
6166          (active (gnus-active group)))
6167     (and info active
6168          (gnus-list-range-difference
6169           (gnus-list-range-difference
6170            (gnus-sorted-complement
6171             (gnus-uncompress-range active)
6172             (gnus-list-of-unread-articles group))
6173            (cdr (assq 'dormant marked)))
6174           (cdr (assq 'tick marked))))))
6175
6176 ;; Various summary commands
6177
6178 (defun gnus-summary-select-article-buffer ()
6179   "Reconfigure windows to show article buffer."
6180   (interactive)
6181   (if (not (gnus-buffer-live-p gnus-article-buffer))
6182       (error "There is no article buffer for this summary buffer")
6183     (gnus-configure-windows 'article)
6184     (select-window (get-buffer-window gnus-article-buffer))))
6185
6186 (defun gnus-summary-universal-argument (arg)
6187   "Perform any operation on all articles that are process/prefixed."
6188   (interactive "P")
6189   (let ((articles (gnus-summary-work-articles arg))
6190         func article)
6191     (if (eq
6192          (setq
6193           func
6194           (key-binding
6195            (read-key-sequence
6196             (substitute-command-keys
6197              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6198          'undefined)
6199         (gnus-error 1 "Undefined key")
6200       (save-excursion
6201         (while articles
6202           (gnus-summary-goto-subject (setq article (pop articles)))
6203           (let (gnus-newsgroup-processable)
6204             (command-execute func))
6205           (gnus-summary-remove-process-mark article)))))
6206   (gnus-summary-position-point))
6207
6208 (defun gnus-summary-toggle-truncation (&optional arg)
6209   "Toggle truncation of summary lines.
6210 With arg, turn line truncation on if arg is positive."
6211   (interactive "P")
6212   (setq truncate-lines
6213         (if (null arg) (not truncate-lines)
6214           (> (prefix-numeric-value arg) 0)))
6215   (redraw-display))
6216
6217 (defun gnus-summary-find-uncancelled ()
6218   "Return the number of an uncancelled article.
6219 The current article is considered, then following articles, then previous
6220 articles.  If all articles are cancelled then return a dummy 0."
6221   (let (found)
6222     (dolist (rev '(nil t))
6223       (unless found      ; don't demand the reverse list if we don't need it
6224         (let ((data (gnus-data-find-list
6225                      (gnus-summary-article-number) (gnus-data-list rev))))
6226           (while (and data (not found))
6227             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6228                 (setq found (gnus-data-number (car data))))
6229             (setq data (cdr data))))))
6230     (or found 0)))
6231
6232 (defun gnus-summary-reselect-current-group (&optional all rescan)
6233   "Exit and then reselect the current newsgroup.
6234 The prefix argument ALL means to select all articles."
6235   (interactive "P")
6236   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6237     (error "Ephemeral groups can't be reselected"))
6238   (let ((current-subject (gnus-summary-find-uncancelled))
6239         (group gnus-newsgroup-name))
6240     (setq gnus-newsgroup-begin nil)
6241     (gnus-summary-exit)
6242     ;; We have to adjust the point of group mode buffer because
6243     ;; point was moved to the next unread newsgroup by exiting.
6244     (gnus-summary-jump-to-group group)
6245     (when rescan
6246       (save-excursion
6247         (gnus-group-get-new-news-this-group 1)))
6248     (gnus-group-read-group all t)
6249     (gnus-summary-goto-subject current-subject nil t)))
6250
6251 (defun gnus-summary-rescan-group (&optional all)
6252   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6253   (interactive "P")
6254   (gnus-summary-reselect-current-group all t))
6255
6256 (defun gnus-summary-update-info (&optional non-destructive)
6257   (save-excursion
6258     (let ((group gnus-newsgroup-name))
6259       (when group
6260         (when gnus-newsgroup-kill-headers
6261           (setq gnus-newsgroup-killed
6262                 (gnus-compress-sequence
6263                  (gnus-sorted-union
6264                   (gnus-list-range-intersection
6265                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6266                   gnus-newsgroup-unreads)
6267                  t)))
6268         (unless (listp (cdr gnus-newsgroup-killed))
6269           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6270         (let ((headers gnus-newsgroup-headers))
6271           ;; Set the new ranges of read articles.
6272           (save-excursion
6273             (set-buffer gnus-group-buffer)
6274             (gnus-undo-force-boundary))
6275           (gnus-update-read-articles
6276            group (gnus-sorted-union
6277                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6278           ;; Set the current article marks.
6279           (let ((gnus-newsgroup-scored
6280                  (if (and (not gnus-save-score)
6281                           (not non-destructive))
6282                      nil
6283                    gnus-newsgroup-scored)))
6284             (save-excursion
6285               (gnus-update-marks)))
6286           ;; Do the cross-ref thing.
6287           (when gnus-use-cross-reference
6288             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6289           ;; Do not switch windows but change the buffer to work.
6290           (set-buffer gnus-group-buffer)
6291           (unless (gnus-ephemeral-group-p group)
6292             (gnus-group-update-group group)))))))
6293
6294 (defun gnus-summary-save-newsrc (&optional force)
6295   "Save the current number of read/marked articles in the dribble buffer.
6296 The dribble buffer will then be saved.
6297 If FORCE (the prefix), also save the .newsrc file(s)."
6298   (interactive "P")
6299   (gnus-summary-update-info t)
6300   (if force
6301       (gnus-save-newsrc-file)
6302     (gnus-dribble-save)))
6303
6304 (defun gnus-summary-exit (&optional temporary)
6305   "Exit reading current newsgroup, and then return to group selection mode.
6306 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6307   (interactive)
6308   (gnus-set-global-variables)
6309   (when (gnus-buffer-live-p gnus-article-buffer)
6310     (save-excursion
6311       (set-buffer gnus-article-buffer)
6312       (mm-destroy-parts gnus-article-mime-handles)
6313       ;; Set it to nil for safety reason.
6314       (setq gnus-article-mime-handle-alist nil)
6315       (setq gnus-article-mime-handles nil)))
6316   (gnus-kill-save-kill-buffer)
6317   (gnus-async-halt-prefetch)
6318   (let* ((group gnus-newsgroup-name)
6319          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6320          (gnus-group-is-exiting-p t)
6321          (mode major-mode)
6322          (group-point nil)
6323          (buf (current-buffer)))
6324     (unless quit-config
6325       ;; Do adaptive scoring, and possibly save score files.
6326       (when gnus-newsgroup-adaptive
6327         (gnus-score-adaptive))
6328       (when gnus-use-scoring
6329         (gnus-score-save)))
6330     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6331     ;; If we have several article buffers, we kill them at exit.
6332     (unless gnus-single-article-buffer
6333       (gnus-kill-buffer gnus-original-article-buffer)
6334       (setq gnus-article-current nil))
6335     (when gnus-use-cache
6336       (gnus-cache-possibly-remove-articles)
6337       (gnus-cache-save-buffers))
6338     (gnus-async-prefetch-remove-group group)
6339     (when gnus-suppress-duplicates
6340       (gnus-dup-enter-articles))
6341     (when gnus-use-trees
6342       (gnus-tree-close group))
6343     (when gnus-use-cache
6344       (gnus-cache-write-active))
6345     ;; Remove entries for this group.
6346     (nnmail-purge-split-history (gnus-group-real-name group))
6347     ;; Make all changes in this group permanent.
6348     (unless quit-config
6349       (gnus-run-hooks 'gnus-exit-group-hook)
6350       (gnus-summary-update-info))
6351     (gnus-close-group group)
6352     ;; Make sure where we were, and go to next newsgroup.
6353     (set-buffer gnus-group-buffer)
6354     (unless quit-config
6355       (gnus-group-jump-to-group group))
6356     (gnus-run-hooks 'gnus-summary-exit-hook)
6357     (unless (or quit-config
6358                 ;; If this group has disappeared from the summary
6359                 ;; buffer, don't skip forwards.
6360                 (not (string= group (gnus-group-group-name))))
6361       (gnus-group-next-unread-group 1))
6362     (setq group-point (point))
6363     (if temporary
6364         nil                             ;Nothing to do.
6365       ;; If we have several article buffers, we kill them at exit.
6366       (unless gnus-single-article-buffer
6367         (gnus-kill-buffer gnus-article-buffer)
6368         (gnus-kill-buffer gnus-original-article-buffer)
6369         (setq gnus-article-current nil))
6370       (set-buffer buf)
6371       (if (not gnus-kill-summary-on-exit)
6372           (progn
6373             (gnus-deaden-summary)
6374             (setq mode nil))
6375         ;; We set all buffer-local variables to nil.  It is unclear why
6376         ;; this is needed, but if we don't, buffer-local variables are
6377         ;; not garbage-collected, it seems.  This would the lead to en
6378         ;; ever-growing Emacs.
6379         (gnus-summary-clear-local-variables)
6380         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6381           (gnus-summary-clear-local-variables))
6382         (when (get-buffer gnus-article-buffer)
6383           (bury-buffer gnus-article-buffer))
6384         ;; We clear the global counterparts of the buffer-local
6385         ;; variables as well, just to be on the safe side.
6386         (set-buffer gnus-group-buffer)
6387         (gnus-summary-clear-local-variables)
6388         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6389           (gnus-summary-clear-local-variables)))
6390       (setq gnus-current-select-method gnus-select-method)
6391       (pop-to-buffer gnus-group-buffer)
6392       (if (not quit-config)
6393           (progn
6394             (goto-char group-point)
6395             (gnus-configure-windows 'group 'force))
6396         (gnus-handle-ephemeral-exit quit-config))
6397       ;; Return to group mode buffer.
6398       (when (eq mode 'gnus-summary-mode)
6399         (gnus-kill-buffer buf))
6400       ;; Clear the current group name.
6401       (unless quit-config
6402         (setq gnus-newsgroup-name nil)))))
6403
6404 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6405 (defun gnus-summary-exit-no-update (&optional no-questions)
6406   "Quit reading current newsgroup without updating read article info."
6407   (interactive)
6408   (let* ((group gnus-newsgroup-name)
6409          (gnus-group-is-exiting-p t)
6410          (quit-config (gnus-group-quit-config group)))
6411     (when (or no-questions
6412               gnus-expert-user
6413               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6414       (gnus-async-halt-prefetch)
6415       (mapcar 'funcall
6416               (delq 'gnus-summary-expire-articles
6417                     (copy-sequence gnus-summary-prepare-exit-hook)))
6418       (when (gnus-buffer-live-p gnus-article-buffer)
6419         (save-excursion
6420           (set-buffer gnus-article-buffer)
6421           (mm-destroy-parts gnus-article-mime-handles)
6422           ;; Set it to nil for safety reason.
6423           (setq gnus-article-mime-handle-alist nil)
6424           (setq gnus-article-mime-handles nil)))
6425       ;; If we have several article buffers, we kill them at exit.
6426       (unless gnus-single-article-buffer
6427         (gnus-kill-buffer gnus-article-buffer)
6428         (gnus-kill-buffer gnus-original-article-buffer)
6429         (setq gnus-article-current nil))
6430       (if (not gnus-kill-summary-on-exit)
6431           (gnus-deaden-summary)
6432         (gnus-close-group group)
6433         (gnus-summary-clear-local-variables)
6434         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6435           (gnus-summary-clear-local-variables))
6436         (set-buffer gnus-group-buffer)
6437         (gnus-summary-clear-local-variables)
6438         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6439           (gnus-summary-clear-local-variables))
6440         (when (get-buffer gnus-summary-buffer)
6441           (kill-buffer gnus-summary-buffer)))
6442       (unless gnus-single-article-buffer
6443         (setq gnus-article-current nil))
6444       (when gnus-use-trees
6445         (gnus-tree-close group))
6446       (gnus-async-prefetch-remove-group group)
6447       (when (get-buffer gnus-article-buffer)
6448         (bury-buffer gnus-article-buffer))
6449       ;; Return to the group buffer.
6450       (gnus-configure-windows 'group 'force)
6451       ;; Clear the current group name.
6452       (setq gnus-newsgroup-name nil)
6453       (unless (gnus-ephemeral-group-p group)
6454         (gnus-group-update-group group))
6455       (when (equal (gnus-group-group-name) group)
6456         (gnus-group-next-unread-group 1))
6457       (when quit-config
6458         (gnus-handle-ephemeral-exit quit-config)))))
6459
6460 (defun gnus-handle-ephemeral-exit (quit-config)
6461   "Handle movement when leaving an ephemeral group.
6462 The state which existed when entering the ephemeral is reset."
6463   (if (not (buffer-name (car quit-config)))
6464       (gnus-configure-windows 'group 'force)
6465     (set-buffer (car quit-config))
6466     (cond ((eq major-mode 'gnus-summary-mode)
6467            (gnus-set-global-variables))
6468           ((eq major-mode 'gnus-article-mode)
6469            (save-excursion
6470              ;; The `gnus-summary-buffer' variable may point
6471              ;; to the old summary buffer when using a single
6472              ;; article buffer.
6473              (unless (gnus-buffer-live-p gnus-summary-buffer)
6474                (set-buffer gnus-group-buffer))
6475              (set-buffer gnus-summary-buffer)
6476              (gnus-set-global-variables))))
6477     (if (or (eq (cdr quit-config) 'article)
6478             (eq (cdr quit-config) 'pick))
6479         (progn
6480           ;; The current article may be from the ephemeral group
6481           ;; thus it is best that we reload this article
6482           (gnus-summary-show-article)
6483           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6484               (gnus-configure-windows 'pick 'force)
6485             (gnus-configure-windows (cdr quit-config) 'force)))
6486       (gnus-configure-windows (cdr quit-config) 'force))
6487     (when (eq major-mode 'gnus-summary-mode)
6488       (gnus-summary-next-subject 1 nil t)
6489       (gnus-summary-recenter)
6490       (gnus-summary-position-point))))
6491
6492 ;;; Dead summaries.
6493
6494 (defvar gnus-dead-summary-mode-map nil)
6495
6496 (unless gnus-dead-summary-mode-map
6497   (setq gnus-dead-summary-mode-map (make-keymap))
6498   (suppress-keymap gnus-dead-summary-mode-map)
6499   (substitute-key-definition
6500    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6501   (dolist (key '("\C-d" "\r" "\177" [delete]))
6502     (define-key gnus-dead-summary-mode-map
6503       key 'gnus-summary-wake-up-the-dead))
6504   (dolist (key '("q" "Q"))
6505     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6506
6507 (defvar gnus-dead-summary-mode nil
6508   "Minor mode for Gnus summary buffers.")
6509
6510 (defun gnus-dead-summary-mode (&optional arg)
6511   "Minor mode for Gnus summary buffers."
6512   (interactive "P")
6513   (when (eq major-mode 'gnus-summary-mode)
6514     (make-local-variable 'gnus-dead-summary-mode)
6515     (setq gnus-dead-summary-mode
6516           (if (null arg) (not gnus-dead-summary-mode)
6517             (> (prefix-numeric-value arg) 0)))
6518     (when gnus-dead-summary-mode
6519       (gnus-add-minor-mode
6520        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6521
6522 (defun gnus-deaden-summary ()
6523   "Make the current summary buffer into a dead summary buffer."
6524   ;; Kill any previous dead summary buffer.
6525   (when (and gnus-dead-summary
6526              (buffer-name gnus-dead-summary))
6527     (save-excursion
6528       (set-buffer gnus-dead-summary)
6529       (when gnus-dead-summary-mode
6530         (kill-buffer (current-buffer)))))
6531   ;; Make this the current dead summary.
6532   (setq gnus-dead-summary (current-buffer))
6533   (gnus-dead-summary-mode 1)
6534   (let ((name (buffer-name)))
6535     (when (string-match "Summary" name)
6536       (rename-buffer
6537        (concat (substring name 0 (match-beginning 0)) "Dead "
6538                (substring name (match-beginning 0)))
6539        t)
6540       (bury-buffer))))
6541
6542 (defun gnus-kill-or-deaden-summary (buffer)
6543   "Kill or deaden the summary BUFFER."
6544   (save-excursion
6545     (when (and (buffer-name buffer)
6546                (not gnus-single-article-buffer))
6547       (save-excursion
6548         (set-buffer buffer)
6549         (gnus-kill-buffer gnus-article-buffer)
6550         (gnus-kill-buffer gnus-original-article-buffer)))
6551     (cond
6552      ;; Kill the buffer.
6553      (gnus-kill-summary-on-exit
6554       (when (and gnus-use-trees
6555                  (gnus-buffer-exists-p buffer))
6556         (save-excursion
6557           (set-buffer buffer)
6558           (gnus-tree-close gnus-newsgroup-name)))
6559       (gnus-kill-buffer buffer))
6560      ;; Deaden the buffer.
6561      ((gnus-buffer-exists-p buffer)
6562       (save-excursion
6563         (set-buffer buffer)
6564         (gnus-deaden-summary))))))
6565
6566 (defun gnus-summary-wake-up-the-dead (&rest args)
6567   "Wake up the dead summary buffer."
6568   (interactive)
6569   (gnus-dead-summary-mode -1)
6570   (let ((name (buffer-name)))
6571     (when (string-match "Dead " name)
6572       (rename-buffer
6573        (concat (substring name 0 (match-beginning 0))
6574                (substring name (match-end 0)))
6575        t)))
6576   (gnus-message 3 "This dead summary is now alive again"))
6577
6578 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6579 (defun gnus-summary-fetch-faq (&optional faq-dir)
6580   "Fetch the FAQ for the current group.
6581 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6582 in."
6583   (interactive
6584    (list
6585     (when current-prefix-arg
6586       (completing-read
6587        "FAQ dir: " (and (listp gnus-group-faq-directory)
6588                         (mapcar (lambda (file) (list file))
6589                                 gnus-group-faq-directory))))))
6590   (let (gnus-faq-buffer)
6591     (when (setq gnus-faq-buffer
6592                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6593       (gnus-configure-windows 'summary-faq))))
6594
6595 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6596 (defun gnus-summary-describe-group (&optional force)
6597   "Describe the current newsgroup."
6598   (interactive "P")
6599   (gnus-group-describe-group force gnus-newsgroup-name))
6600
6601 (defun gnus-summary-describe-briefly ()
6602   "Describe summary mode commands briefly."
6603   (interactive)
6604   (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")))
6605
6606 ;; Walking around group mode buffer from summary mode.
6607
6608 (defun gnus-summary-next-group (&optional no-article target-group backward)
6609   "Exit current newsgroup and then select next unread newsgroup.
6610 If prefix argument NO-ARTICLE is non-nil, no article is selected
6611 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6612 previous group instead."
6613   (interactive "P")
6614   ;; Stop pre-fetching.
6615   (gnus-async-halt-prefetch)
6616   (let ((current-group gnus-newsgroup-name)
6617         (current-buffer (current-buffer))
6618         entered)
6619     (while (not entered)
6620       ;; Then we find what group we are supposed to enter.
6621       (set-buffer gnus-group-buffer)
6622       (gnus-group-jump-to-group current-group)
6623       (setq target-group
6624             (or target-group
6625                 (if (eq gnus-keep-same-level 'best)
6626                     (gnus-summary-best-group gnus-newsgroup-name)
6627                   (gnus-summary-search-group backward gnus-keep-same-level))))
6628       (if (not target-group)
6629           ;; There are no further groups, so we return to the group
6630           ;; buffer.
6631           (progn
6632             (gnus-message 5 "Returning to the group buffer")
6633             (setq entered t)
6634             (when (gnus-buffer-live-p current-buffer)
6635               (set-buffer current-buffer)
6636               (gnus-summary-exit))
6637             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6638         ;; We try to enter the target group.
6639         (gnus-group-jump-to-group target-group)
6640         (let ((unreads (gnus-group-group-unread)))
6641           (if (and (or (eq t unreads)
6642                        (and unreads (not (zerop unreads))))
6643                    (progn
6644                      ;; Now we semi-exit this group to update Xrefs
6645                      ;; and all variables.  We can't do a real exit,
6646                      ;; because the window conf must remain the same
6647                      ;; in case the user is prompted for info, and we
6648                      ;; don't want the window conf to change before
6649                      ;; that...
6650                      (when (gnus-buffer-live-p current-buffer)
6651                        (set-buffer current-buffer)
6652                        (gnus-summary-exit t))
6653                      (gnus-summary-read-group
6654                       target-group nil no-article
6655                       (and (buffer-name current-buffer) current-buffer)
6656                       nil backward)))
6657               (setq entered t)
6658             (setq current-group target-group
6659                   target-group nil)))))))
6660
6661 (defun gnus-summary-prev-group (&optional no-article)
6662   "Exit current newsgroup and then select previous unread newsgroup.
6663 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6664   (interactive "P")
6665   (gnus-summary-next-group no-article nil t))
6666
6667 ;; Walking around summary lines.
6668
6669 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6670   "Go to the first unread subject.
6671 If UNREAD is non-nil, go to the first unread article.
6672 Returns the article selected or nil if there are no unread articles."
6673   (interactive "P")
6674   (prog1
6675       (cond
6676        ;; Empty summary.
6677        ((null gnus-newsgroup-data)
6678         (gnus-message 3 "No articles in the group")
6679         nil)
6680        ;; Pick the first article.
6681        ((not unread)
6682         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6683         (gnus-data-number (car gnus-newsgroup-data)))
6684        ;; No unread articles.
6685        ((null gnus-newsgroup-unreads)
6686         (gnus-message 3 "No more unread articles")
6687         nil)
6688        ;; Find the first unread article.
6689        (t
6690         (let ((data gnus-newsgroup-data))
6691           (while (and data
6692                       (and (not (and undownloaded
6693                                      (memq (car data)
6694                                            gnus-newsgroup-undownloaded)))
6695                            (if unseen
6696                                (or (not (memq
6697                                          (gnus-data-number (car data))
6698                                          gnus-newsgroup-unseen))
6699                                    (not (gnus-data-unread-p (car data))))
6700                              (not (gnus-data-unread-p (car data))))))
6701             (setq data (cdr data)))
6702           (when data
6703             (goto-char (gnus-data-pos (car data)))
6704             (gnus-data-number (car data))))))
6705     (gnus-summary-position-point)))
6706
6707 (defun gnus-summary-next-subject (n &optional unread dont-display)
6708   "Go to next N'th summary line.
6709 If N is negative, go to the previous N'th subject line.
6710 If UNREAD is non-nil, only unread articles are selected.
6711 The difference between N and the actual number of steps taken is
6712 returned."
6713   (interactive "p")
6714   (let ((backward (< n 0))
6715         (n (abs n)))
6716     (while (and (> n 0)
6717                 (if backward
6718                     (gnus-summary-find-prev unread)
6719                   (gnus-summary-find-next unread)))
6720       (unless (zerop (setq n (1- n)))
6721         (gnus-summary-show-thread)))
6722     (when (/= 0 n)
6723       (gnus-message 7 "No more%s articles"
6724                     (if unread " unread" "")))
6725     (unless dont-display
6726       (gnus-summary-recenter)
6727       (gnus-summary-position-point))
6728     n))
6729
6730 (defun gnus-summary-next-unread-subject (n)
6731   "Go to next N'th unread summary line."
6732   (interactive "p")
6733   (gnus-summary-next-subject n t))
6734
6735 (defun gnus-summary-prev-subject (n &optional unread)
6736   "Go to previous N'th summary line.
6737 If optional argument UNREAD is non-nil, only unread article is selected."
6738   (interactive "p")
6739   (gnus-summary-next-subject (- n) unread))
6740
6741 (defun gnus-summary-prev-unread-subject (n)
6742   "Go to previous N'th unread summary line."
6743   (interactive "p")
6744   (gnus-summary-next-subject (- n) t))
6745
6746 (defun gnus-summary-goto-subjects (articles)
6747   "Insert the subject header for ARTICLES in the current buffer."
6748   (save-excursion
6749     (dolist (article articles)
6750       (gnus-summary-goto-subject article t)))
6751   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6752   (gnus-summary-position-point))
6753   
6754 (defun gnus-summary-goto-subject (article &optional force silent)
6755   "Go the subject line of ARTICLE.
6756 If FORCE, also allow jumping to articles not currently shown."
6757   (interactive "nArticle number: ")
6758   (unless (numberp article)
6759     (error "Article %s is not a number" article))
6760   (let ((b (point))
6761         (data (gnus-data-find article)))
6762     ;; We read in the article if we have to.
6763     (and (not data)
6764          force
6765          (gnus-summary-insert-subject
6766           article
6767           (if (or (numberp force) (vectorp force)) force)
6768           t)
6769          (setq data (gnus-data-find article)))
6770     (goto-char b)
6771     (if (not data)
6772         (progn
6773           (unless silent
6774             (gnus-message 3 "Can't find article %d" article))
6775           nil)
6776       (let ((pt (gnus-data-pos data)))
6777         (goto-char pt)
6778         (gnus-summary-set-article-display-arrow pt))
6779       (gnus-summary-position-point)
6780       article)))
6781
6782 ;; Walking around summary lines with displaying articles.
6783
6784 (defun gnus-summary-expand-window (&optional arg)
6785   "Make the summary buffer take up the entire Emacs frame.
6786 Given a prefix, will force an `article' buffer configuration."
6787   (interactive "P")
6788   (if arg
6789       (gnus-configure-windows 'article 'force)
6790     (gnus-configure-windows 'summary 'force)))
6791
6792 (defun gnus-summary-display-article (article &optional all-header)
6793   "Display ARTICLE in article buffer."
6794   (when (gnus-buffer-live-p gnus-article-buffer)
6795     (with-current-buffer gnus-article-buffer
6796       (mm-enable-multibyte)))
6797   (gnus-set-global-variables)
6798   (when (gnus-buffer-live-p gnus-article-buffer)
6799     (with-current-buffer gnus-article-buffer
6800       (setq gnus-article-charset gnus-newsgroup-charset)
6801       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6802       (mm-enable-multibyte)))
6803   (if (null article)
6804       nil
6805     (prog1
6806         (if gnus-summary-display-article-function
6807             (funcall gnus-summary-display-article-function article all-header)
6808           (gnus-article-prepare article all-header))
6809       (gnus-run-hooks 'gnus-select-article-hook)
6810       (when (and gnus-current-article
6811                  (not (zerop gnus-current-article)))
6812         (gnus-summary-goto-subject gnus-current-article))
6813       (gnus-summary-recenter)
6814       (when (and gnus-use-trees gnus-show-threads)
6815         (gnus-possibly-generate-tree article)
6816         (gnus-highlight-selected-tree article))
6817       ;; Successfully display article.
6818       (gnus-article-set-window-start
6819        (cdr (assq article gnus-newsgroup-bookmarks))))))
6820
6821 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6822   "Select the current article.
6823 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6824 non-nil, the article will be re-fetched even if it already present in
6825 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6826 be displayed."
6827   ;; Make sure we are in the summary buffer to work around bbdb bug.
6828   (unless (eq major-mode 'gnus-summary-mode)
6829     (set-buffer gnus-summary-buffer))
6830   (let ((article (or article (gnus-summary-article-number)))
6831         (all-headers (not (not all-headers))) ;Must be t or nil.
6832         gnus-summary-display-article-function)
6833     (and (not pseudo)
6834          (gnus-summary-article-pseudo-p article)
6835          (error "This is a pseudo-article"))
6836     (save-excursion
6837       (set-buffer gnus-summary-buffer)
6838       (if (or (and gnus-single-article-buffer
6839                    (or (null gnus-current-article)
6840                        (null gnus-article-current)
6841                        (null (get-buffer gnus-article-buffer))
6842                        (not (eq article (cdr gnus-article-current)))
6843                        (not (equal (car gnus-article-current)
6844                                    gnus-newsgroup-name))))
6845               (and (not gnus-single-article-buffer)
6846                    (or (null gnus-current-article)
6847                        (not (eq gnus-current-article article))))
6848               force)
6849           ;; The requested article is different from the current article.
6850           (progn
6851             (gnus-summary-display-article article all-headers)
6852             (when (gnus-buffer-live-p gnus-article-buffer)
6853               (with-current-buffer gnus-article-buffer
6854                 (if (not gnus-article-decoded-p) ;; a local variable
6855                     (mm-disable-multibyte))))
6856             (gnus-article-set-window-start
6857              (cdr (assq article gnus-newsgroup-bookmarks)))
6858             article)
6859         'old))))
6860
6861 (defun gnus-summary-force-verify-and-decrypt ()
6862   (interactive)
6863   (let ((mm-verify-option 'known)
6864         (mm-decrypt-option 'known)
6865         (gnus-buttonized-mime-types (append (list "multipart/signed"
6866                                                   "multipart/encrypted")
6867                                             gnus-buttonized-mime-types)))
6868     (gnus-summary-select-article nil 'force)))
6869
6870 (defun gnus-summary-set-current-mark (&optional current-mark)
6871   "Obsolete function."
6872   nil)
6873
6874 (defun gnus-summary-next-article (&optional unread subject backward push)
6875   "Select the next article.
6876 If UNREAD, only unread articles are selected.
6877 If SUBJECT, only articles with SUBJECT are selected.
6878 If BACKWARD, the previous article is selected instead of the next."
6879   (interactive "P")
6880   (cond
6881    ;; Is there such an article?
6882    ((and (gnus-summary-search-forward unread subject backward)
6883          (or (gnus-summary-display-article (gnus-summary-article-number))
6884              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6885     (gnus-summary-position-point))
6886    ;; If not, we try the first unread, if that is wanted.
6887    ((and subject
6888          gnus-auto-select-same
6889          (gnus-summary-first-unread-article))
6890     (gnus-summary-position-point)
6891     (gnus-message 6 "Wrapped"))
6892    ;; Try to get next/previous article not displayed in this group.
6893    ((and gnus-auto-extend-newsgroup
6894          (not unread) (not subject))
6895     (gnus-summary-goto-article
6896      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6897      nil (count-lines (point-min) (point))))
6898    ;; Go to next/previous group.
6899    (t
6900     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6901       (gnus-summary-jump-to-group gnus-newsgroup-name))
6902     (let ((cmd last-command-char)
6903           (point
6904            (save-excursion
6905              (set-buffer gnus-group-buffer)
6906              (point)))
6907           (group
6908            (if (eq gnus-keep-same-level 'best)
6909                (gnus-summary-best-group gnus-newsgroup-name)
6910              (gnus-summary-search-group backward gnus-keep-same-level))))
6911       ;; For some reason, the group window gets selected.  We change
6912       ;; it back.
6913       (select-window (get-buffer-window (current-buffer)))
6914       ;; Select next unread newsgroup automagically.
6915       (cond
6916        ((or (not gnus-auto-select-next)
6917             (not cmd))
6918         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6919        ((or (eq gnus-auto-select-next 'quietly)
6920             (and (eq gnus-auto-select-next 'slightly-quietly)
6921                  push)
6922             (and (eq gnus-auto-select-next 'almost-quietly)
6923                  (gnus-summary-last-article-p)))
6924         ;; Select quietly.
6925         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6926             (gnus-summary-exit)
6927           (gnus-message 7 "No more%s articles (%s)..."
6928                         (if unread " unread" "")
6929                         (if group (concat "selecting " group)
6930                           "exiting"))
6931           (gnus-summary-next-group nil group backward)))
6932        (t
6933         (when (gnus-key-press-event-p last-input-event)
6934           (gnus-summary-walk-group-buffer
6935            gnus-newsgroup-name cmd unread backward point))))))))
6936
6937 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6938   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6939                       (?\C-p (gnus-group-prev-unread-group 1))))
6940         (cursor-in-echo-area t)
6941         keve key group ended prompt)
6942     (save-excursion
6943       (set-buffer gnus-group-buffer)
6944       (goto-char start)
6945       (setq group
6946             (if (eq gnus-keep-same-level 'best)
6947                 (gnus-summary-best-group gnus-newsgroup-name)
6948               (gnus-summary-search-group backward gnus-keep-same-level))))
6949     (while (not ended)
6950       (setq prompt
6951             (format
6952              "No more%s articles%s " (if unread " unread" "")
6953              (if (and group
6954                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6955                  (format " (Type %s for %s [%s])"
6956                          (single-key-description cmd) group
6957                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6958                (format " (Type %s to exit %s)"
6959                        (single-key-description cmd)
6960                        gnus-newsgroup-name))))
6961       ;; Confirm auto selection.
6962       (setq key (car (setq keve (gnus-read-event-char prompt)))
6963             ended t)
6964       (cond
6965        ((assq key keystrokes)
6966         (let ((obuf (current-buffer)))
6967           (switch-to-buffer gnus-group-buffer)
6968           (when group
6969             (gnus-group-jump-to-group group))
6970           (eval (cadr (assq key keystrokes)))
6971           (setq group (gnus-group-group-name))
6972           (switch-to-buffer obuf))
6973         (setq ended nil))
6974        ((equal key cmd)
6975         (if (or (not group)
6976                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6977             (gnus-summary-exit)
6978           (gnus-summary-next-group nil group backward)))
6979        (t
6980         (push (cdr keve) unread-command-events))))))
6981
6982 (defun gnus-summary-next-unread-article ()
6983   "Select unread article after current one."
6984   (interactive)
6985   (gnus-summary-next-article
6986    (or (not (eq gnus-summary-goto-unread 'never))
6987        (gnus-summary-last-article-p (gnus-summary-article-number)))
6988    (and gnus-auto-select-same
6989         (gnus-summary-article-subject))))
6990
6991 (defun gnus-summary-prev-article (&optional unread subject)
6992   "Select the article after the current one.
6993 If UNREAD is non-nil, only unread articles are selected."
6994   (interactive "P")
6995   (gnus-summary-next-article unread subject t))
6996
6997 (defun gnus-summary-prev-unread-article ()
6998   "Select unread article before current one."
6999   (interactive)
7000   (gnus-summary-prev-article
7001    (or (not (eq gnus-summary-goto-unread 'never))
7002        (gnus-summary-first-article-p (gnus-summary-article-number)))
7003    (and gnus-auto-select-same
7004         (gnus-summary-article-subject))))
7005
7006 (defun gnus-summary-next-page (&optional lines circular stop)
7007   "Show next page of the selected article.
7008 If at the end of the current article, select the next article.
7009 LINES says how many lines should be scrolled up.
7010
7011 If CIRCULAR is non-nil, go to the start of the article instead of
7012 selecting the next article when reaching the end of the current
7013 article.
7014
7015 If STOP is non-nil, just stop when reaching the end of the message."
7016   (interactive "P")
7017   (setq gnus-summary-buffer (current-buffer))
7018   (gnus-set-global-variables)
7019   (let ((article (gnus-summary-article-number))
7020         (article-window (get-buffer-window gnus-article-buffer t))
7021         endp)
7022     ;; If the buffer is empty, we have no article.
7023     (unless article
7024       (error "No article to select"))
7025     (gnus-configure-windows 'article)
7026     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7027         (if (and (eq gnus-summary-goto-unread 'never)
7028                  (not (gnus-summary-last-article-p article)))
7029             (gnus-summary-next-article)
7030           (gnus-summary-next-unread-article))
7031       (if (or (null gnus-current-article)
7032               (null gnus-article-current)
7033               (/= article (cdr gnus-article-current))
7034               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7035           ;; Selected subject is different from current article's.
7036           (gnus-summary-display-article article)
7037         (when article-window
7038           (gnus-eval-in-buffer-window gnus-article-buffer
7039             (setq endp (gnus-article-next-page lines)))
7040           (when endp
7041             (cond (stop
7042                    (gnus-message 3 "End of message"))
7043                   (circular
7044                    (gnus-summary-beginning-of-article))
7045                   (lines
7046                    (gnus-message 3 "End of message"))
7047                   ((null lines)
7048                    (if (and (eq gnus-summary-goto-unread 'never)
7049                             (not (gnus-summary-last-article-p article)))
7050                        (gnus-summary-next-article)
7051                      (gnus-summary-next-unread-article))))))))
7052     (gnus-summary-recenter)
7053     (gnus-summary-position-point)))
7054
7055 (defun gnus-summary-prev-page (&optional lines move)
7056   "Show previous page of selected article.
7057 Argument LINES specifies lines to be scrolled down.
7058 If MOVE, move to the previous unread article if point is at
7059 the beginning of the buffer."
7060   (interactive "P")
7061   (let ((article (gnus-summary-article-number))
7062         (article-window (get-buffer-window gnus-article-buffer t))
7063         endp)
7064     (gnus-configure-windows 'article)
7065     (if (or (null gnus-current-article)
7066             (null gnus-article-current)
7067             (/= article (cdr gnus-article-current))
7068             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7069         ;; Selected subject is different from current article's.
7070         (gnus-summary-display-article article)
7071       (gnus-summary-recenter)
7072       (when article-window
7073         (gnus-eval-in-buffer-window gnus-article-buffer
7074           (setq endp (gnus-article-prev-page lines)))
7075         (when (and move endp)
7076           (cond (lines
7077                  (gnus-message 3 "Beginning of message"))
7078                 ((null lines)
7079                  (if (and (eq gnus-summary-goto-unread 'never)
7080                           (not (gnus-summary-first-article-p article)))
7081                      (gnus-summary-prev-article)
7082                    (gnus-summary-prev-unread-article))))))))
7083   (gnus-summary-position-point))
7084
7085 (defun gnus-summary-prev-page-or-article (&optional lines)
7086   "Show previous page of selected article.
7087 Argument LINES specifies lines to be scrolled down.
7088 If at the beginning of the article, go to the next article."
7089   (interactive "P")
7090   (gnus-summary-prev-page lines t))
7091
7092 (defun gnus-summary-scroll-up (lines)
7093   "Scroll up (or down) one line current article.
7094 Argument LINES specifies lines to be scrolled up (or down if negative)."
7095   (interactive "p")
7096   (gnus-configure-windows 'article)
7097   (gnus-summary-show-thread)
7098   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7099     (gnus-eval-in-buffer-window gnus-article-buffer
7100       (cond ((> lines 0)
7101              (when (gnus-article-next-page lines)
7102                (gnus-message 3 "End of message")))
7103             ((< lines 0)
7104              (gnus-article-prev-page (- lines))))))
7105   (gnus-summary-recenter)
7106   (gnus-summary-position-point))
7107
7108 (defun gnus-summary-scroll-down (lines)
7109   "Scroll down (or up) one line current article.
7110 Argument LINES specifies lines to be scrolled down (or up if negative)."
7111   (interactive "p")
7112   (gnus-summary-scroll-up (- lines)))
7113
7114 (defun gnus-summary-next-same-subject ()
7115   "Select next article which has the same subject as current one."
7116   (interactive)
7117   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7118
7119 (defun gnus-summary-prev-same-subject ()
7120   "Select previous article which has the same subject as current one."
7121   (interactive)
7122   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7123
7124 (defun gnus-summary-next-unread-same-subject ()
7125   "Select next unread article which has the same subject as current one."
7126   (interactive)
7127   (gnus-summary-next-article t (gnus-summary-article-subject)))
7128
7129 (defun gnus-summary-prev-unread-same-subject ()
7130   "Select previous unread article which has the same subject as current one."
7131   (interactive)
7132   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7133
7134 (defun gnus-summary-first-unread-article ()
7135   "Select the first unread article.
7136 Return nil if there are no unread articles."
7137   (interactive)
7138   (prog1
7139       (when (gnus-summary-first-subject t)
7140         (gnus-summary-show-thread)
7141         (gnus-summary-first-subject t)
7142         (gnus-summary-display-article (gnus-summary-article-number)))
7143     (gnus-summary-position-point)))
7144
7145 (defun gnus-summary-first-unread-subject ()
7146   "Place the point on the subject line of the first unread article.
7147 Return nil if there are no unread articles."
7148   (interactive)
7149   (prog1
7150       (when (gnus-summary-first-subject t)
7151         (gnus-summary-show-thread)
7152         (gnus-summary-first-subject t))
7153     (gnus-summary-position-point)))
7154
7155 (defun gnus-summary-first-unseen-subject ()
7156   "Place the point on the subject line of the first unseen article.
7157 Return nil if there are no unseen articles."
7158   (interactive)
7159   (prog1
7160       (when (gnus-summary-first-subject t t t)
7161         (gnus-summary-show-thread)
7162         (gnus-summary-first-subject t t t))
7163     (gnus-summary-position-point)))
7164
7165 (defun gnus-summary-first-unseen-or-unread-subject ()
7166   "Place the point on the subject line of the first unseen article.
7167 Return nil if there are no unseen articles."
7168   (interactive)
7169   (prog1
7170       (unless (when (gnus-summary-first-subject t t t)
7171                 (gnus-summary-show-thread)
7172                 (gnus-summary-first-subject t t t))
7173         (when (gnus-summary-first-subject t)
7174           (gnus-summary-show-thread)
7175           (gnus-summary-first-subject t)))
7176     (gnus-summary-position-point)))
7177
7178 (defun gnus-summary-first-article ()
7179   "Select the first article.
7180 Return nil if there are no articles."
7181   (interactive)
7182   (prog1
7183       (when (gnus-summary-first-subject)
7184         (gnus-summary-show-thread)
7185         (gnus-summary-first-subject)
7186         (gnus-summary-display-article (gnus-summary-article-number)))
7187     (gnus-summary-position-point)))
7188
7189 (defun gnus-summary-best-unread-article (&optional arg)
7190   "Select the unread article with the highest score.
7191 If given a prefix argument, select the next unread article that has a
7192 score higher than the default score."
7193   (interactive "P")
7194   (let ((article (if arg
7195                      (gnus-summary-better-unread-subject)
7196                    (gnus-summary-best-unread-subject))))
7197     (if article
7198         (gnus-summary-goto-article article)
7199       (error "No unread articles"))))
7200
7201 (defun gnus-summary-best-unread-subject ()
7202   "Select the unread subject with the highest score."
7203   (interactive)
7204   (let ((best -1000000)
7205         (data gnus-newsgroup-data)
7206         article score)
7207     (while data
7208       (and (gnus-data-unread-p (car data))
7209            (> (setq score
7210                     (gnus-summary-article-score (gnus-data-number (car data))))
7211               best)
7212            (setq best score
7213                  article (gnus-data-number (car data))))
7214       (setq data (cdr data)))
7215     (when article
7216       (gnus-summary-goto-subject article))
7217     (gnus-summary-position-point)
7218     article))
7219
7220 (defun gnus-summary-better-unread-subject ()
7221   "Select the first unread subject that has a score over the default score."
7222   (interactive)
7223   (let ((data gnus-newsgroup-data)
7224         article score)
7225     (while (and (setq article (gnus-data-number (car data)))
7226                 (or (gnus-data-read-p (car data))
7227                     (not (> (gnus-summary-article-score article)
7228                             gnus-summary-default-score))))
7229       (setq data (cdr data)))
7230     (when article
7231       (gnus-summary-goto-subject article))
7232     (gnus-summary-position-point)
7233     article))
7234
7235 (defun gnus-summary-last-subject ()
7236   "Go to the last displayed subject line in the group."
7237   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7238     (when article
7239       (gnus-summary-goto-subject article))))
7240
7241 (defun gnus-summary-goto-article (article &optional all-headers force)
7242   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7243 If ALL-HEADERS is non-nil, no header lines are hidden.
7244 If FORCE, go to the article even if it isn't displayed.  If FORCE
7245 is a number, it is the line the article is to be displayed on."
7246   (interactive
7247    (list
7248     (completing-read
7249      "Article number or Message-ID: "
7250      (mapcar (lambda (number) (list (int-to-string number)))
7251              gnus-newsgroup-limit))
7252     current-prefix-arg
7253     t))
7254   (prog1
7255       (if (and (stringp article)
7256                (string-match "@" article))
7257           (gnus-summary-refer-article article)
7258         (when (stringp article)
7259           (setq article (string-to-number article)))
7260         (if (gnus-summary-goto-subject article force)
7261             (gnus-summary-display-article article all-headers)
7262           (gnus-message 4 "Couldn't go to article %s" article) nil))
7263     (gnus-summary-position-point)))
7264
7265 (defun gnus-summary-goto-last-article ()
7266   "Go to the previously read article."
7267   (interactive)
7268   (prog1
7269       (when gnus-last-article
7270         (gnus-summary-goto-article gnus-last-article nil t))
7271     (gnus-summary-position-point)))
7272
7273 (defun gnus-summary-pop-article (number)
7274   "Pop one article off the history and go to the previous.
7275 NUMBER articles will be popped off."
7276   (interactive "p")
7277   (let (to)
7278     (setq gnus-newsgroup-history
7279           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7280     (if to
7281         (gnus-summary-goto-article (car to) nil t)
7282       (error "Article history empty")))
7283   (gnus-summary-position-point))
7284
7285 ;; Summary commands and functions for limiting the summary buffer.
7286
7287 (defun gnus-summary-limit-to-articles (n)
7288   "Limit the summary buffer to the next N articles.
7289 If not given a prefix, use the process marked articles instead."
7290   (interactive "P")
7291   (prog1
7292       (let ((articles (gnus-summary-work-articles n)))
7293         (setq gnus-newsgroup-processable nil)
7294         (gnus-summary-limit articles))
7295     (gnus-summary-position-point)))
7296
7297 (defun gnus-summary-pop-limit (&optional total)
7298   "Restore the previous limit.
7299 If given a prefix, remove all limits."
7300   (interactive "P")
7301   (when total
7302     (setq gnus-newsgroup-limits
7303           (list (mapcar (lambda (h) (mail-header-number h))
7304                         gnus-newsgroup-headers))))
7305   (unless gnus-newsgroup-limits
7306     (error "No limit to pop"))
7307   (prog1
7308       (gnus-summary-limit nil 'pop)
7309     (gnus-summary-position-point)))
7310
7311 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7312   "Limit the summary buffer to articles that have subjects that match a regexp.
7313 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7314   (interactive
7315    (list (read-string (if current-prefix-arg
7316                           "Exclude subject (regexp): "
7317                         "Limit to subject (regexp): "))
7318          nil current-prefix-arg))
7319   (unless header
7320     (setq header "subject"))
7321   (when (not (equal "" subject))
7322     (prog1
7323         (let ((articles (gnus-summary-find-matching
7324                          (or header "subject") subject 'all nil nil
7325                          not-matching)))
7326           (unless articles
7327             (error "Found no matches for \"%s\"" subject))
7328           (gnus-summary-limit articles))
7329       (gnus-summary-position-point))))
7330
7331 (defun gnus-summary-limit-to-author (from &optional not-matching)
7332   "Limit the summary buffer to articles that have authors that match a regexp.
7333 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7334   (interactive
7335    (list (read-string (if current-prefix-arg
7336                           "Exclude author (regexp): "
7337                         "Limit to author (regexp): "))
7338          current-prefix-arg))
7339   (gnus-summary-limit-to-subject from "from" not-matching))
7340
7341 (defun gnus-summary-limit-to-age (age &optional younger-p)
7342   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7343 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7344 articles that are younger than AGE days."
7345   (interactive
7346    (let ((younger current-prefix-arg)
7347          (days-got nil)
7348          days)
7349      (while (not days-got)
7350        (setq days (if younger
7351                       (read-string "Limit to articles younger than (in days, older when negative): ")
7352                     (read-string
7353                      "Limit to articles older than (in days, younger when negative): ")))
7354        (when (> (length days) 0)
7355          (setq days (read days)))
7356        (if (numberp days)
7357            (progn
7358              (setq days-got t)
7359              (if (< days 0)
7360                  (progn
7361                    (setq younger (not younger))
7362                    (setq days (* days -1)))))
7363          (message "Please enter a number.")
7364          (sleep-for 1)))
7365      (list days younger)))
7366   (prog1
7367       (let ((data gnus-newsgroup-data)
7368             (cutoff (days-to-time age))
7369             articles d date is-younger)
7370         (while (setq d (pop data))
7371           (when (and (vectorp (gnus-data-header d))
7372                      (setq date (mail-header-date (gnus-data-header d))))
7373             (setq is-younger (time-less-p
7374                               (time-since (condition-case ()
7375                                               (date-to-time date)
7376                                             (error '(0 0))))
7377                               cutoff))
7378             (when (if younger-p
7379                       is-younger
7380                     (not is-younger))
7381               (push (gnus-data-number d) articles))))
7382         (gnus-summary-limit (nreverse articles)))
7383     (gnus-summary-position-point)))
7384
7385 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7386   "Limit the summary buffer to articles that match an 'extra' header."
7387   (interactive
7388    (let ((header
7389           (intern
7390            (gnus-completing-read-with-default
7391             (symbol-name (car gnus-extra-headers))
7392             (if current-prefix-arg
7393                 "Exclude extra header:"
7394               "Limit extra header:")
7395             (mapcar (lambda (x)
7396                       (cons (symbol-name x) x))
7397                     gnus-extra-headers)
7398             nil
7399             t))))
7400      (list header
7401            (read-string (format "%s header %s (regexp): "
7402                                 (if current-prefix-arg "Exclude" "Limit to")
7403                                 header))
7404            current-prefix-arg)))
7405   (when (not (equal "" regexp))
7406     (prog1
7407         (let ((articles (gnus-summary-find-matching
7408                          (cons 'extra header) regexp 'all nil nil
7409                          not-matching)))
7410           (unless articles
7411             (error "Found no matches for \"%s\"" regexp))
7412           (gnus-summary-limit articles))
7413       (gnus-summary-position-point))))
7414
7415 (defun gnus-summary-limit-to-display-predicate ()
7416   "Limit the summary buffer to the predicated in the `display' group parameter."
7417   (interactive)
7418   (unless gnus-newsgroup-display
7419     (error "There is no `display' group parameter"))
7420   (let (articles)
7421     (dolist (number gnus-newsgroup-articles)
7422       (when (funcall gnus-newsgroup-display)
7423         (push number articles)))
7424     (gnus-summary-limit articles))
7425   (gnus-summary-position-point))
7426
7427 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7428 (make-obsolete
7429  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7430
7431 (defun gnus-summary-limit-to-unread (&optional all)
7432   "Limit the summary buffer to articles that are not marked as read.
7433 If ALL is non-nil, limit strictly to unread articles."
7434   (interactive "P")
7435   (if all
7436       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7437     (gnus-summary-limit-to-marks
7438      ;; Concat all the marks that say that an article is read and have
7439      ;; those removed.
7440      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7441            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7442            gnus-low-score-mark gnus-expirable-mark
7443            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7444            gnus-duplicate-mark gnus-souped-mark)
7445      'reverse)))
7446
7447 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7448 (make-obsolete 'gnus-summary-delete-marked-with
7449                'gnus-summary-limit-exclude-marks)
7450
7451 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7452   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7453 If REVERSE, limit the summary buffer to articles that are marked
7454 with MARKS.  MARKS can either be a string of marks or a list of marks.
7455 Returns how many articles were removed."
7456   (interactive "sMarks: ")
7457   (gnus-summary-limit-to-marks marks t))
7458
7459 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7460   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7461 If REVERSE (the prefix), limit the summary buffer to articles that are
7462 not marked with MARKS.  MARKS can either be a string of marks or a
7463 list of marks.
7464 Returns how many articles were removed."
7465   (interactive "sMarks: \nP")
7466   (prog1
7467       (let ((data gnus-newsgroup-data)
7468             (marks (if (listp marks) marks
7469                      (append marks nil))) ; Transform to list.
7470             articles)
7471         (while data
7472           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7473                   (memq (gnus-data-mark (car data)) marks))
7474             (push (gnus-data-number (car data)) articles))
7475           (setq data (cdr data)))
7476         (gnus-summary-limit articles))
7477     (gnus-summary-position-point)))
7478
7479 (defun gnus-summary-limit-to-score (score)
7480   "Limit to articles with score at or above SCORE."
7481   (interactive "NLimit to articles with score of at least: ")
7482   (let ((data gnus-newsgroup-data)
7483         articles)
7484     (while data
7485       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7486                 score)
7487         (push (gnus-data-number (car data)) articles))
7488       (setq data (cdr data)))
7489     (prog1
7490         (gnus-summary-limit articles)
7491       (gnus-summary-position-point))))
7492
7493 (defun gnus-summary-limit-to-unseen ()
7494   "Limit to unseen articles."
7495   (interactive)
7496   (prog1
7497       (gnus-summary-limit gnus-newsgroup-unseen)
7498     (gnus-summary-position-point)))
7499
7500 (defun gnus-summary-limit-include-thread (id)
7501   "Display all the hidden articles that is in the thread with ID in it.
7502 When called interactively, ID is the Message-ID of the current
7503 article."
7504   (interactive (list (mail-header-id (gnus-summary-article-header))))
7505   (let ((articles (gnus-articles-in-thread
7506                    (gnus-id-to-thread (gnus-root-id id)))))
7507     (prog1
7508         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7509       (gnus-summary-limit-include-matching-articles
7510        "subject"
7511        (regexp-quote (gnus-simplify-subject-re
7512                       (mail-header-subject (gnus-id-to-header id)))))
7513       (gnus-summary-position-point))))
7514
7515 (defun gnus-summary-limit-include-matching-articles (header regexp)
7516   "Display all the hidden articles that have HEADERs that match REGEXP."
7517   (interactive (list (read-string "Match on header: ")
7518                      (read-string "Regexp: ")))
7519   (let ((articles (gnus-find-matching-articles header regexp)))
7520     (prog1
7521         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7522       (gnus-summary-position-point))))
7523
7524 (defun gnus-summary-insert-dormant-articles ()
7525   "Insert all the dormat articles for this group into the current buffer."
7526   (interactive)
7527   (let ((gnus-verbose (max 6 gnus-verbose)))
7528     (if (not gnus-newsgroup-dormant)
7529         (gnus-message 3 "No cached articles for this group")
7530       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7531
7532 (defun gnus-summary-limit-include-dormant ()
7533   "Display all the hidden articles that are marked as dormant.
7534 Note that this command only works on a subset of the articles currently
7535 fetched for this group."
7536   (interactive)
7537   (unless gnus-newsgroup-dormant
7538     (error "There are no dormant articles in this group"))
7539   (prog1
7540       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7541     (gnus-summary-position-point)))
7542
7543 (defun gnus-summary-limit-exclude-dormant ()
7544   "Hide all dormant articles."
7545   (interactive)
7546   (prog1
7547       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7548     (gnus-summary-position-point)))
7549
7550 (defun gnus-summary-limit-exclude-childless-dormant ()
7551   "Hide all dormant articles that have no children."
7552   (interactive)
7553   (let ((data (gnus-data-list t))
7554         articles d children)
7555     ;; Find all articles that are either not dormant or have
7556     ;; children.
7557     (while (setq d (pop data))
7558       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7559                 (and (setq children
7560                            (gnus-article-children (gnus-data-number d)))
7561                      (let (found)
7562                        (while children
7563                          (when (memq (car children) articles)
7564                            (setq children nil
7565                                  found t))
7566                          (pop children))
7567                        found)))
7568         (push (gnus-data-number d) articles)))
7569     ;; Do the limiting.
7570     (prog1
7571         (gnus-summary-limit articles)
7572       (gnus-summary-position-point))))
7573
7574 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7575   "Mark all unread excluded articles as read.
7576 If ALL, mark even excluded ticked and dormants as read."
7577   (interactive "P")
7578   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7579   (let ((articles (gnus-sorted-ndifference
7580                    (sort
7581                     (mapcar (lambda (h) (mail-header-number h))
7582                             gnus-newsgroup-headers)
7583                     '<)
7584                    gnus-newsgroup-limit))
7585         article)
7586     (setq gnus-newsgroup-unreads
7587           (gnus-sorted-intersection gnus-newsgroup-unreads
7588                                     gnus-newsgroup-limit))
7589     (if all
7590         (setq gnus-newsgroup-dormant nil
7591               gnus-newsgroup-marked nil
7592               gnus-newsgroup-reads
7593               (nconc
7594                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7595                gnus-newsgroup-reads))
7596       (while (setq article (pop articles))
7597         (unless (or (memq article gnus-newsgroup-dormant)
7598                     (memq article gnus-newsgroup-marked))
7599           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7600
7601 (defun gnus-summary-limit (articles &optional pop)
7602   (if pop
7603       ;; We pop the previous limit off the stack and use that.
7604       (setq articles (car gnus-newsgroup-limits)
7605             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7606     ;; We use the new limit, so we push the old limit on the stack.
7607     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7608   ;; Set the limit.
7609   (setq gnus-newsgroup-limit articles)
7610   (let ((total (length gnus-newsgroup-data))
7611         (data (gnus-data-find-list (gnus-summary-article-number)))
7612         (gnus-summary-mark-below nil)   ; Inhibit this.
7613         found)
7614     ;; This will do all the work of generating the new summary buffer
7615     ;; according to the new limit.
7616     (gnus-summary-prepare)
7617     ;; Hide any threads, possibly.
7618     (gnus-summary-maybe-hide-threads)
7619     ;; Try to return to the article you were at, or one in the
7620     ;; neighborhood.
7621     (when data
7622       ;; We try to find some article after the current one.
7623       (while data
7624         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7625           (setq data nil
7626                 found t))
7627         (setq data (cdr data))))
7628     (unless found
7629       ;; If there is no data, that means that we were after the last
7630       ;; article.  The same goes when we can't find any articles
7631       ;; after the current one.
7632       (goto-char (point-max))
7633       (gnus-summary-find-prev))
7634     (gnus-set-mode-line 'summary)
7635     ;; We return how many articles were removed from the summary
7636     ;; buffer as a result of the new limit.
7637     (- total (length gnus-newsgroup-data))))
7638
7639 (defsubst gnus-invisible-cut-children (threads)
7640   (let ((num 0))
7641     (while threads
7642       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7643         (incf num))
7644       (pop threads))
7645     (< num 2)))
7646
7647 (defsubst gnus-cut-thread (thread)
7648   "Go forwards in the thread until we find an article that we want to display."
7649   (when (or (eq gnus-fetch-old-headers 'some)
7650             (eq gnus-fetch-old-headers 'invisible)
7651             (numberp gnus-fetch-old-headers)
7652             (eq gnus-build-sparse-threads 'some)
7653             (eq gnus-build-sparse-threads 'more))
7654     ;; Deal with old-fetched headers and sparse threads.
7655     (while (and
7656             thread
7657             (or
7658              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7659              (gnus-summary-article-ancient-p
7660               (mail-header-number (car thread))))
7661             (if (or (<= (length (cdr thread)) 1)
7662                     (eq gnus-fetch-old-headers 'invisible))
7663                 (setq gnus-newsgroup-limit
7664                       (delq (mail-header-number (car thread))
7665                             gnus-newsgroup-limit)
7666                       thread (cadr thread))
7667               (when (gnus-invisible-cut-children (cdr thread))
7668                 (let ((th (cdr thread)))
7669                   (while th
7670                     (if (memq (mail-header-number (caar th))
7671                               gnus-newsgroup-limit)
7672                         (setq thread (car th)
7673                               th nil)
7674                       (setq th (cdr th))))))))))
7675   thread)
7676
7677 (defun gnus-cut-threads (threads)
7678   "Cut off all uninteresting articles from the beginning of threads."
7679   (when (or (eq gnus-fetch-old-headers 'some)
7680             (eq gnus-fetch-old-headers 'invisible)
7681             (numberp gnus-fetch-old-headers)
7682             (eq gnus-build-sparse-threads 'some)
7683             (eq gnus-build-sparse-threads 'more))
7684     (let ((th threads))
7685       (while th
7686         (setcar th (gnus-cut-thread (car th)))
7687         (setq th (cdr th)))))
7688   ;; Remove nixed out threads.
7689   (delq nil threads))
7690
7691 (defun gnus-summary-initial-limit (&optional show-if-empty)
7692   "Figure out what the initial limit is supposed to be on group entry.
7693 This entails weeding out unwanted dormants, low-scored articles,
7694 fetch-old-headers verbiage, and so on."
7695   ;; Most groups have nothing to remove.
7696   (if (or gnus-inhibit-limiting
7697           (and (null gnus-newsgroup-dormant)
7698                (eq gnus-newsgroup-display 'gnus-not-ignore)
7699                (not (eq gnus-fetch-old-headers 'some))
7700                (not (numberp gnus-fetch-old-headers))
7701                (not (eq gnus-fetch-old-headers 'invisible))
7702                (null gnus-summary-expunge-below)
7703                (not (eq gnus-build-sparse-threads 'some))
7704                (not (eq gnus-build-sparse-threads 'more))
7705                (null gnus-thread-expunge-below)
7706                (not gnus-use-nocem)))
7707       ()                                ; Do nothing.
7708     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7709     (setq gnus-newsgroup-limit nil)
7710     (mapatoms
7711      (lambda (node)
7712        (unless (car (symbol-value node))
7713          ;; These threads have no parents -- they are roots.
7714          (let ((nodes (cdr (symbol-value node)))
7715                thread)
7716            (while nodes
7717              (if (and gnus-thread-expunge-below
7718                       (< (gnus-thread-total-score (car nodes))
7719                          gnus-thread-expunge-below))
7720                  (gnus-expunge-thread (pop nodes))
7721                (setq thread (pop nodes))
7722                (gnus-summary-limit-children thread))))))
7723      gnus-newsgroup-dependencies)
7724     ;; If this limitation resulted in an empty group, we might
7725     ;; pop the previous limit and use it instead.
7726     (when (and (not gnus-newsgroup-limit)
7727                show-if-empty)
7728       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7729     gnus-newsgroup-limit))
7730
7731 (defun gnus-summary-limit-children (thread)
7732   "Return 1 if this subthread is visible and 0 if it is not."
7733   ;; First we get the number of visible children to this thread.  This
7734   ;; is done by recursing down the thread using this function, so this
7735   ;; will really go down to a leaf article first, before slowly
7736   ;; working its way up towards the root.
7737   (when thread
7738     (let* ((max-lisp-eval-depth 5000)
7739            (children
7740            (if (cdr thread)
7741                (apply '+ (mapcar 'gnus-summary-limit-children
7742                                  (cdr thread)))
7743              0))
7744           (number (mail-header-number (car thread)))
7745           score)
7746       (if (and
7747            (not (memq number gnus-newsgroup-marked))
7748            (or
7749             ;; If this article is dormant and has absolutely no visible
7750             ;; children, then this article isn't visible.
7751             (and (memq number gnus-newsgroup-dormant)
7752                  (zerop children))
7753             ;; If this is "fetch-old-headered" and there is no
7754             ;; visible children, then we don't want this article.
7755             (and (or (eq gnus-fetch-old-headers 'some)
7756                      (numberp gnus-fetch-old-headers))
7757                  (gnus-summary-article-ancient-p number)
7758                  (zerop children))
7759             ;; If this is "fetch-old-headered" and `invisible', then
7760             ;; we don't want this article.
7761             (and (eq gnus-fetch-old-headers 'invisible)
7762                  (gnus-summary-article-ancient-p number))
7763             ;; If this is a sparsely inserted article with no children,
7764             ;; we don't want it.
7765             (and (eq gnus-build-sparse-threads 'some)
7766                  (gnus-summary-article-sparse-p number)
7767                  (zerop children))
7768             ;; If we use expunging, and this article is really
7769             ;; low-scored, then we don't want this article.
7770             (when (and gnus-summary-expunge-below
7771                        (< (setq score
7772                                 (or (cdr (assq number gnus-newsgroup-scored))
7773                                     gnus-summary-default-score))
7774                           gnus-summary-expunge-below))
7775               ;; We increase the expunge-tally here, but that has
7776               ;; nothing to do with the limits, really.
7777               (incf gnus-newsgroup-expunged-tally)
7778               ;; We also mark as read here, if that's wanted.
7779               (when (and gnus-summary-mark-below
7780                          (< score gnus-summary-mark-below))
7781                 (setq gnus-newsgroup-unreads
7782                       (delq number gnus-newsgroup-unreads))
7783                 (if gnus-newsgroup-auto-expire
7784                     (push number gnus-newsgroup-expirable)
7785                   (push (cons number gnus-low-score-mark)
7786                         gnus-newsgroup-reads)))
7787               t)
7788             ;; Do the `display' group parameter.
7789             (and gnus-newsgroup-display
7790                  (not (funcall gnus-newsgroup-display)))
7791             ;; Check NoCeM things.
7792             (if (and gnus-use-nocem
7793                      (gnus-nocem-unwanted-article-p
7794                       (mail-header-id (car thread))))
7795                 (progn
7796                   (setq gnus-newsgroup-unreads
7797                         (delq number gnus-newsgroup-unreads))
7798                   t))))
7799           ;; Nope, invisible article.
7800           0
7801         ;; Ok, this article is to be visible, so we add it to the limit
7802         ;; and return 1.
7803         (push number gnus-newsgroup-limit)
7804         1))))
7805
7806 (defun gnus-expunge-thread (thread)
7807   "Mark all articles in THREAD as read."
7808   (let* ((number (mail-header-number (car thread))))
7809     (incf gnus-newsgroup-expunged-tally)
7810     ;; We also mark as read here, if that's wanted.
7811     (setq gnus-newsgroup-unreads
7812           (delq number gnus-newsgroup-unreads))
7813     (if gnus-newsgroup-auto-expire
7814         (push number gnus-newsgroup-expirable)
7815       (push (cons number gnus-low-score-mark)
7816             gnus-newsgroup-reads)))
7817   ;; Go recursively through all subthreads.
7818   (mapcar 'gnus-expunge-thread (cdr thread)))
7819
7820 ;; Summary article oriented commands
7821
7822 (defun gnus-summary-refer-parent-article (n)
7823   "Refer parent article N times.
7824 If N is negative, go to ancestor -N instead.
7825 The difference between N and the number of articles fetched is returned."
7826   (interactive "p")
7827   (let ((skip 1)
7828         error header ref)
7829     (when (not (natnump n))
7830       (setq skip (abs n)
7831             n 1))
7832     (while (and (> n 0)
7833                 (not error))
7834       (setq header (gnus-summary-article-header))
7835       (if (and (eq (mail-header-number header)
7836                    (cdr gnus-article-current))
7837                (equal gnus-newsgroup-name
7838                       (car gnus-article-current)))
7839           ;; If we try to find the parent of the currently
7840           ;; displayed article, then we take a look at the actual
7841           ;; References header, since this is slightly more
7842           ;; reliable than the References field we got from the
7843           ;; server.
7844           (save-excursion
7845             (set-buffer gnus-original-article-buffer)
7846             (nnheader-narrow-to-headers)
7847             (unless (setq ref (message-fetch-field "references"))
7848               (setq ref (message-fetch-field "in-reply-to")))
7849             (widen))
7850         (setq ref
7851               ;; It's not the current article, so we take a bet on
7852               ;; the value we got from the server.
7853               (mail-header-references header)))
7854       (if (and ref
7855                (not (equal ref "")))
7856           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7857             (gnus-message 1 "Couldn't find parent"))
7858         (gnus-message 1 "No references in article %d"
7859                       (gnus-summary-article-number))
7860         (setq error t))
7861       (decf n))
7862     (gnus-summary-position-point)
7863     n))
7864
7865 (defun gnus-summary-refer-references ()
7866   "Fetch all articles mentioned in the References header.
7867 Return the number of articles fetched."
7868   (interactive)
7869   (let ((ref (mail-header-references (gnus-summary-article-header)))
7870         (current (gnus-summary-article-number))
7871         (n 0))
7872     (if (or (not ref)
7873             (equal ref ""))
7874         (error "No References in the current article")
7875       ;; For each Message-ID in the References header...
7876       (while (string-match "<[^>]*>" ref)
7877         (incf n)
7878         ;; ... fetch that article.
7879         (gnus-summary-refer-article
7880          (prog1 (match-string 0 ref)
7881            (setq ref (substring ref (match-end 0))))))
7882       (gnus-summary-goto-subject current)
7883       (gnus-summary-position-point)
7884       n)))
7885
7886 (defun gnus-summary-refer-thread (&optional limit)
7887   "Fetch all articles in the current thread.
7888 If LIMIT (the numerical prefix), fetch that many old headers instead
7889 of what's specified by the `gnus-refer-thread-limit' variable."
7890   (interactive "P")
7891   (let ((id (mail-header-id (gnus-summary-article-header)))
7892         (limit (if limit (prefix-numeric-value limit)
7893                  gnus-refer-thread-limit)))
7894     ;; We want to fetch LIMIT *old* headers, but we also have to
7895     ;; re-fetch all the headers in the current buffer, because many of
7896     ;; them may be undisplayed.  So we adjust LIMIT.
7897     (when (numberp limit)
7898       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7899     (unless (eq gnus-fetch-old-headers 'invisible)
7900       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7901       ;; Retrieve the headers and read them in.
7902       (if (eq (gnus-retrieve-headers
7903                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7904               'nov)
7905           (gnus-build-all-threads)
7906         (error "Can't fetch thread from backends that don't support NOV"))
7907       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7908     (gnus-summary-limit-include-thread id)))
7909
7910 (defun gnus-summary-refer-article (message-id)
7911   "Fetch an article specified by MESSAGE-ID."
7912   (interactive "sMessage-ID: ")
7913   (when (and (stringp message-id)
7914              (not (zerop (length message-id))))
7915     ;; Construct the correct Message-ID if necessary.
7916     ;; Suggested by tale@pawl.rpi.edu.
7917     (unless (string-match "^<" message-id)
7918       (setq message-id (concat "<" message-id)))
7919     (unless (string-match ">$" message-id)
7920       (setq message-id (concat message-id ">")))
7921     (let* ((header (gnus-id-to-header message-id))
7922            (sparse (and header
7923                         (gnus-summary-article-sparse-p
7924                          (mail-header-number header))
7925                         (memq (mail-header-number header)
7926                               gnus-newsgroup-limit)))
7927            number)
7928       (cond
7929        ;; If the article is present in the buffer we just go to it.
7930        ((and header
7931              (or (not (gnus-summary-article-sparse-p
7932                        (mail-header-number header)))
7933                  sparse))
7934         (prog1
7935             (gnus-summary-goto-article
7936              (mail-header-number header) nil t)
7937           (when sparse
7938             (gnus-summary-update-article (mail-header-number header)))))
7939        (t
7940         ;; We fetch the article.
7941         (catch 'found
7942           (dolist (gnus-override-method (gnus-refer-article-methods))
7943             (gnus-check-server gnus-override-method)
7944             ;; Fetch the header, and display the article.
7945             (when (setq number (gnus-summary-insert-subject message-id))
7946               (gnus-summary-select-article nil nil nil number)
7947               (throw 'found t)))
7948           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7949
7950 (defun gnus-refer-article-methods ()
7951   "Return a list of referable methods."
7952   (cond
7953    ;; No method, so we default to current and native.
7954    ((null gnus-refer-article-method)
7955     (list gnus-current-select-method gnus-select-method))
7956    ;; Current.
7957    ((eq 'current gnus-refer-article-method)
7958     (list gnus-current-select-method))
7959    ;; List of select methods.
7960    ((not (and (symbolp (car gnus-refer-article-method))
7961               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7962     (let (out)
7963       (dolist (method gnus-refer-article-method)
7964         (push (if (eq 'current method)
7965                   gnus-current-select-method
7966                 method)
7967               out))
7968       (nreverse out)))
7969    ;; One single select method.
7970    (t
7971     (list gnus-refer-article-method))))
7972
7973 (defun gnus-summary-edit-parameters ()
7974   "Edit the group parameters of the current group."
7975   (interactive)
7976   (gnus-group-edit-group gnus-newsgroup-name 'params))
7977
7978 (defun gnus-summary-customize-parameters ()
7979   "Customize the group parameters of the current group."
7980   (interactive)
7981   (gnus-group-customize gnus-newsgroup-name))
7982
7983 (defun gnus-summary-enter-digest-group (&optional force)
7984   "Enter an nndoc group based on the current article.
7985 If FORCE, force a digest interpretation.  If not, try
7986 to guess what the document format is."
7987   (interactive "P")
7988   (let ((conf gnus-current-window-configuration))
7989     (save-excursion
7990       (gnus-summary-select-article))
7991     (setq gnus-current-window-configuration conf)
7992     (let* ((name (format "%s-%d"
7993                          (gnus-group-prefixed-name
7994                           gnus-newsgroup-name (list 'nndoc ""))
7995                          (save-excursion
7996                            (set-buffer gnus-summary-buffer)
7997                            gnus-current-article)))
7998            (ogroup gnus-newsgroup-name)
7999            (params (append (gnus-info-params (gnus-get-info ogroup))
8000                            (list (cons 'to-group ogroup))
8001                            (list (cons 'save-article-group ogroup))))
8002            (case-fold-search t)
8003            (buf (current-buffer))
8004            dig to-address)
8005       (save-excursion
8006         (set-buffer gnus-original-article-buffer)
8007         ;; Have the digest group inherit the main mail address of
8008         ;; the parent article.
8009         (when (setq to-address (or (gnus-fetch-field "reply-to")
8010                                    (gnus-fetch-field "from")))
8011           (setq params (append
8012                         (list (cons 'to-address
8013                                     (funcall gnus-decode-encoded-word-function
8014                                              to-address))))))
8015         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8016         (insert-buffer-substring gnus-original-article-buffer)
8017         ;; Remove lines that may lead nndoc to misinterpret the
8018         ;; document type.
8019         (narrow-to-region
8020          (goto-char (point-min))
8021          (or (search-forward "\n\n" nil t) (point)))
8022         (goto-char (point-min))
8023         (delete-matching-lines "^Path:\\|^From ")
8024         (widen))
8025       (unwind-protect
8026           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8027                     (gnus-newsgroup-ephemeral-ignored-charsets
8028                      gnus-newsgroup-ignored-charsets))
8029                 (gnus-group-read-ephemeral-group
8030                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8031                               (nndoc-article-type
8032                                ,(if force 'mbox 'guess)))
8033                  t nil nil nil
8034                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8035                                                         "ADAPT")))))
8036               ;; Make all postings to this group go to the parent group.
8037               (nconc (gnus-info-params (gnus-get-info name))
8038                      params)
8039             ;; Couldn't select this doc group.
8040             (switch-to-buffer buf)
8041             (gnus-set-global-variables)
8042             (gnus-configure-windows 'summary)
8043             (gnus-message 3 "Article couldn't be entered?"))
8044         (kill-buffer dig)))))
8045
8046 (defun gnus-summary-read-document (n)
8047   "Open a new group based on the current article(s).
8048 This will allow you to read digests and other similar
8049 documents as newsgroups.
8050 Obeys the standard process/prefix convention."
8051   (interactive "P")
8052   (let* ((articles (gnus-summary-work-articles n))
8053          (ogroup gnus-newsgroup-name)
8054          (params (append (gnus-info-params (gnus-get-info ogroup))
8055                          (list (cons 'to-group ogroup))))
8056          article group egroup groups vgroup)
8057     (while (setq article (pop articles))
8058       (setq group (format "%s-%d" gnus-newsgroup-name article))
8059       (gnus-summary-remove-process-mark article)
8060       (when (gnus-summary-display-article article)
8061         (save-excursion
8062           (with-temp-buffer
8063             (insert-buffer-substring gnus-original-article-buffer)
8064             ;; Remove some headers that may lead nndoc to make
8065             ;; the wrong guess.
8066             (message-narrow-to-head)
8067             (goto-char (point-min))
8068             (delete-matching-lines "^\\(Path\\):\\|^From ")
8069             (widen)
8070             (if (setq egroup
8071                       (gnus-group-read-ephemeral-group
8072                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8073                                      (nndoc-article-type guess))
8074                        t nil t))
8075                 (progn
8076             ;; Make all postings to this group go to the parent group.
8077                   (nconc (gnus-info-params (gnus-get-info egroup))
8078                          params)
8079                   (push egroup groups))
8080               ;; Couldn't select this doc group.
8081               (gnus-error 3 "Article couldn't be entered"))))))
8082     ;; Now we have selected all the documents.
8083     (cond
8084      ((not groups)
8085       (error "None of the articles could be interpreted as documents"))
8086      ((gnus-group-read-ephemeral-group
8087        (setq vgroup (format
8088                      "nnvirtual:%s-%s" gnus-newsgroup-name
8089                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8090        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8091        t
8092        (cons (current-buffer) 'summary)))
8093      (t
8094       (error "Couldn't select virtual nndoc group")))))
8095
8096 (defun gnus-summary-isearch-article (&optional regexp-p)
8097   "Do incremental search forward on the current article.
8098 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8099   (interactive "P")
8100   (gnus-summary-select-article)
8101   (gnus-configure-windows 'article)
8102   (gnus-eval-in-buffer-window gnus-article-buffer
8103     (save-restriction
8104       (widen)
8105       (isearch-forward regexp-p))))
8106
8107 (defun gnus-summary-search-article-forward (regexp &optional backward)
8108   "Search for an article containing REGEXP forward.
8109 If BACKWARD, search backward instead."
8110   (interactive
8111    (list (read-string
8112           (format "Search article %s (regexp%s): "
8113                   (if current-prefix-arg "backward" "forward")
8114                   (if gnus-last-search-regexp
8115                       (concat ", default " gnus-last-search-regexp)
8116                     "")))
8117          current-prefix-arg))
8118   (if (string-equal regexp "")
8119       (setq regexp (or gnus-last-search-regexp ""))
8120     (setq gnus-last-search-regexp regexp)
8121     (setq gnus-article-before-search gnus-current-article))
8122   ;; Intentionally set gnus-last-article.
8123   (setq gnus-last-article gnus-article-before-search)
8124   (let ((gnus-last-article gnus-last-article))
8125     (if (gnus-summary-search-article regexp backward)
8126         (gnus-summary-show-thread)
8127       (error "Search failed: \"%s\"" regexp))))
8128
8129 (defun gnus-summary-search-article-backward (regexp)
8130   "Search for an article containing REGEXP backward."
8131   (interactive
8132    (list (read-string
8133           (format "Search article backward (regexp%s): "
8134                   (if gnus-last-search-regexp
8135                       (concat ", default " gnus-last-search-regexp)
8136                     "")))))
8137   (gnus-summary-search-article-forward regexp 'backward))
8138
8139 (defun gnus-summary-search-article (regexp &optional backward)
8140   "Search for an article containing REGEXP.
8141 Optional argument BACKWARD means do search for backward.
8142 `gnus-select-article-hook' is not called during the search."
8143   ;; We have to require this here to make sure that the following
8144   ;; dynamic binding isn't shadowed by autoloading.
8145   (require 'gnus-async)
8146   (require 'gnus-art)
8147   (let ((gnus-select-article-hook nil)  ;Disable hook.
8148         (gnus-article-prepare-hook nil)
8149         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8150         (gnus-use-article-prefetch nil)
8151         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8152         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8153         (sum (current-buffer))
8154         (gnus-display-mime-function nil)
8155         (found nil)
8156         point)
8157     (gnus-save-hidden-threads
8158       (gnus-summary-select-article)
8159       (set-buffer gnus-article-buffer)
8160       (goto-char (window-point (get-buffer-window (current-buffer))))
8161       (when backward
8162         (forward-line -1))
8163       (while (not found)
8164         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8165         (if (if backward
8166                 (re-search-backward regexp nil t)
8167               (re-search-forward regexp nil t))
8168             ;; We found the regexp.
8169             (progn
8170               (setq found 'found)
8171               (beginning-of-line)
8172               (set-window-start
8173                (get-buffer-window (current-buffer))
8174                (point))
8175               (forward-line 1)
8176               (set-window-point
8177                (get-buffer-window (current-buffer))
8178                (point))
8179               (set-buffer sum)
8180               (setq point (point)))
8181           ;; We didn't find it, so we go to the next article.
8182           (set-buffer sum)
8183           (setq found 'not)
8184           (while (eq found 'not)
8185             (if (not (if backward (gnus-summary-find-prev)
8186                        (gnus-summary-find-next)))
8187                 ;; No more articles.
8188                 (setq found t)
8189               ;; Select the next article and adjust point.
8190               (unless (gnus-summary-article-sparse-p
8191                        (gnus-summary-article-number))
8192                 (setq found nil)
8193                 (gnus-summary-select-article)
8194                 (set-buffer gnus-article-buffer)
8195                 (widen)
8196                 (goto-char (if backward (point-max) (point-min))))))))
8197       (gnus-message 7 ""))
8198     ;; Return whether we found the regexp.
8199     (when (eq found 'found)
8200       (goto-char point)
8201       (gnus-summary-show-thread)
8202       (gnus-summary-goto-subject gnus-current-article)
8203       (gnus-summary-position-point)
8204       t)))
8205
8206 (defun gnus-find-matching-articles (header regexp)
8207   "Return a list of all articles that match REGEXP on HEADER.
8208 This search includes all articles in the current group that Gnus has
8209 fetched headers for, whether they are displayed or not."
8210   (let ((articles nil)
8211         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8212         (case-fold-search t))
8213     (dolist (header gnus-newsgroup-headers)
8214       (when (string-match regexp (funcall func header))
8215         (push (mail-header-number header) articles)))
8216     (nreverse articles)))
8217
8218 (defun gnus-summary-find-matching (header regexp &optional backward unread
8219                                           not-case-fold not-matching)
8220   "Return a list of all articles that match REGEXP on HEADER.
8221 The search stars on the current article and goes forwards unless
8222 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8223 If UNREAD is non-nil, only unread articles will
8224 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8225 in the comparisons. If NOT-MATCHING, return a list of all articles that
8226 not match REGEXP on HEADER."
8227   (let ((case-fold-search (not not-case-fold))
8228         articles d func)
8229     (if (consp header)
8230         (if (eq (car header) 'extra)
8231             (setq func
8232                   `(lambda (h)
8233                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8234                          "")))
8235           (error "%s is an invalid header" header))
8236       (unless (fboundp (intern (concat "mail-header-" header)))
8237         (error "%s is not a valid header" header))
8238       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8239     (dolist (d (if (eq backward 'all)
8240                    gnus-newsgroup-data
8241                  (gnus-data-find-list
8242                   (gnus-summary-article-number)
8243                   (gnus-data-list backward))))
8244       (when (and (or (not unread)       ; We want all articles...
8245                      (gnus-data-unread-p d)) ; Or just unreads.
8246                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8247                  (if not-matching
8248                      (not (string-match
8249                            regexp
8250                            (funcall func (gnus-data-header d))))
8251                    (string-match regexp
8252                                  (funcall func (gnus-data-header d)))))
8253         (push (gnus-data-number d) articles))) ; Success!
8254     (nreverse articles)))
8255
8256 (defun gnus-summary-execute-command (header regexp command &optional backward)
8257   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8258 If HEADER is an empty string (or nil), the match is done on the entire
8259 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8260   (interactive
8261    (list (let ((completion-ignore-case t))
8262            (completing-read
8263             "Header name: "
8264             (mapcar (lambda (header) (list (format "%s" header)))
8265                     (append
8266                      '("Number" "Subject" "From" "Lines" "Date"
8267                        "Message-ID" "Xref" "References" "Body")
8268                      gnus-extra-headers))
8269             nil 'require-match))
8270          (read-string "Regexp: ")
8271          (read-key-sequence "Command: ")
8272          current-prefix-arg))
8273   (when (equal header "Body")
8274     (setq header ""))
8275   ;; Hidden thread subtrees must be searched as well.
8276   (gnus-summary-show-all-threads)
8277   ;; We don't want to change current point nor window configuration.
8278   (save-excursion
8279     (save-window-excursion
8280       (let (gnus-visual
8281             gnus-treat-strip-trailing-blank-lines
8282             gnus-treat-strip-leading-blank-lines
8283             gnus-treat-strip-multiple-blank-lines
8284             gnus-treat-hide-boring-headers
8285             gnus-treat-fold-newsgroups
8286             gnus-article-prepare-hook)
8287         (gnus-message 6 "Executing %s..." (key-description command))
8288         ;; We'd like to execute COMMAND interactively so as to give arguments.
8289         (gnus-execute header regexp
8290                       `(call-interactively ',(key-binding command))
8291                       backward)
8292         (gnus-message 6 "Executing %s...done" (key-description command))))))
8293
8294 (defun gnus-summary-beginning-of-article ()
8295   "Scroll the article back to the beginning."
8296   (interactive)
8297   (gnus-summary-select-article)
8298   (gnus-configure-windows 'article)
8299   (gnus-eval-in-buffer-window gnus-article-buffer
8300     (widen)
8301     (goto-char (point-min))
8302     (when gnus-page-broken
8303       (gnus-narrow-to-page))))
8304
8305 (defun gnus-summary-end-of-article ()
8306   "Scroll to the end of the article."
8307   (interactive)
8308   (gnus-summary-select-article)
8309   (gnus-configure-windows 'article)
8310   (gnus-eval-in-buffer-window gnus-article-buffer
8311     (widen)
8312     (goto-char (point-max))
8313     (recenter -3)
8314     (when gnus-page-broken
8315       (gnus-narrow-to-page))))
8316
8317 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8318   "Truncate to LEN and quote all \"(\"'s in STRING."
8319   (gnus-replace-in-string (if (and len (> (length string) len))
8320                               (substring string 0 len)
8321                             string)
8322                           "[()]" "\\\\\\&"))
8323
8324 (defun gnus-summary-print-article (&optional filename n)
8325   "Generate and print a PostScript image of the N next (mail) articles.
8326
8327 If N is negative, print the N previous articles.  If N is nil and articles
8328 have been marked with the process mark, print these instead.
8329
8330 If the optional first argument FILENAME is nil, send the image to the
8331 printer.  If FILENAME is a string, save the PostScript image in a file with
8332 that name.  If FILENAME is a number, prompt the user for the name of the file
8333 to save in."
8334   (interactive (list (ps-print-preprint current-prefix-arg)))
8335   (dolist (article (gnus-summary-work-articles n))
8336     (gnus-summary-select-article nil nil 'pseudo article)
8337     (gnus-eval-in-buffer-window gnus-article-buffer
8338       (gnus-print-buffer))
8339     (gnus-summary-remove-process-mark article))
8340   (ps-despool filename))
8341
8342 (defun gnus-print-buffer ()
8343   (let ((buffer (generate-new-buffer " *print*")))
8344     (unwind-protect
8345         (progn
8346           (copy-to-buffer buffer (point-min) (point-max))
8347           (set-buffer buffer)
8348           (gnus-article-delete-invisible-text)
8349           (gnus-remove-text-with-property 'gnus-decoration)
8350           (when (gnus-visual-p 'article-highlight 'highlight)
8351             ;; Copy-to-buffer doesn't copy overlay.  So redo
8352             ;; highlight.
8353             (let ((gnus-article-buffer buffer))
8354               (gnus-article-highlight-citation t)
8355               (gnus-article-highlight-signature)))
8356           (let ((ps-left-header
8357                  (list
8358                   (concat "("
8359                           (gnus-summary-print-truncate-and-quote
8360                            (mail-header-subject gnus-current-headers)
8361                            66) ")")
8362                   (concat "("
8363                           (gnus-summary-print-truncate-and-quote
8364                            (mail-header-from gnus-current-headers)
8365                            45) ")")))
8366                 (ps-right-header
8367                  (list
8368                   "/pagenumberstring load"
8369                   (concat "("
8370                           (mail-header-date gnus-current-headers) ")"))))
8371             (gnus-run-hooks 'gnus-ps-print-hook)
8372             (save-excursion
8373               (if window-system
8374                   (ps-spool-buffer-with-faces)
8375                 (ps-spool-buffer)))))
8376       (kill-buffer buffer))))
8377
8378 (defun gnus-summary-show-article (&optional arg)
8379   "Force redisplaying of the current article.
8380 If ARG (the prefix) is a number, show the article with the charset
8381 defined in `gnus-summary-show-article-charset-alist', or the charset
8382 input.
8383 If ARG (the prefix) is non-nil and not a number, show the raw article
8384 without any article massaging functions being run.  Normally, the key strokes
8385 are `C-u g'."
8386   (interactive "P")
8387   (cond
8388    ((numberp arg)
8389     (gnus-summary-show-article t)
8390     (let ((gnus-newsgroup-charset
8391            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8392                (mm-read-coding-system
8393                 "View as charset: " ;; actually it is coding system.
8394                 (save-excursion
8395                   (set-buffer gnus-article-buffer)
8396                   (mm-detect-coding-region (point) (point-max))))))
8397           (gnus-newsgroup-ignored-charsets 'gnus-all))
8398       (gnus-summary-select-article nil 'force)
8399       (let ((deps gnus-newsgroup-dependencies)
8400             head header lines)
8401         (save-excursion
8402           (set-buffer gnus-original-article-buffer)
8403           (save-restriction
8404             (message-narrow-to-head)
8405             (setq head (buffer-string))
8406             (goto-char (point-min))
8407             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8408               (goto-char (point-max))
8409               (widen)
8410               (setq lines (1- (count-lines (point) (point-max))))))
8411           (with-temp-buffer
8412             (insert (format "211 %d Article retrieved.\n"
8413                             (cdr gnus-article-current)))
8414             (insert head)
8415             (if lines (insert (format "Lines: %d\n" lines)))
8416             (insert ".\n")
8417             (let ((nntp-server-buffer (current-buffer)))
8418               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8419         (gnus-data-set-header
8420          (gnus-data-find (cdr gnus-article-current))
8421          header)
8422         (gnus-summary-update-article-line
8423          (cdr gnus-article-current) header)
8424         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8425           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8426    ((not arg)
8427     ;; Select the article the normal way.
8428     (gnus-summary-select-article nil 'force))
8429    (t
8430     ;; We have to require this here to make sure that the following
8431     ;; dynamic binding isn't shadowed by autoloading.
8432     (require 'gnus-async)
8433     (require 'gnus-art)
8434     ;; Bind the article treatment functions to nil.
8435     (let ((gnus-have-all-headers t)
8436           gnus-article-prepare-hook
8437           gnus-article-decode-hook
8438           gnus-display-mime-function
8439           gnus-break-pages)
8440       ;; Destroy any MIME parts.
8441       (when (gnus-buffer-live-p gnus-article-buffer)
8442         (save-excursion
8443           (set-buffer gnus-article-buffer)
8444           (mm-destroy-parts gnus-article-mime-handles)
8445           ;; Set it to nil for safety reason.
8446           (setq gnus-article-mime-handle-alist nil)
8447           (setq gnus-article-mime-handles nil)))
8448       (gnus-summary-select-article nil 'force))))
8449   (gnus-summary-goto-subject gnus-current-article)
8450   (gnus-summary-position-point))
8451
8452 (defun gnus-summary-show-raw-article ()
8453   "Show the raw article without any article massaging functions being run."
8454   (interactive)
8455   (gnus-summary-show-article t))
8456
8457 (defun gnus-summary-verbose-headers (&optional arg)
8458   "Toggle permanent full header display.
8459 If ARG is a positive number, turn header display on.
8460 If ARG is a negative number, turn header display off."
8461   (interactive "P")
8462   (setq gnus-show-all-headers
8463         (cond ((or (not (numberp arg))
8464                    (zerop arg))
8465                (not gnus-show-all-headers))
8466               ((natnump arg)
8467                t)))
8468   (gnus-summary-show-article))
8469
8470 (defun gnus-summary-toggle-header (&optional arg)
8471   "Show the headers if they are hidden, or hide them if they are shown.
8472 If ARG is a positive number, show the entire header.
8473 If ARG is a negative number, hide the unwanted header lines."
8474   (interactive "P")
8475   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8476                      (get-buffer-window gnus-article-buffer t))))
8477     (with-current-buffer gnus-article-buffer
8478       (widen)
8479       (article-narrow-to-head)
8480       (let* ((buffer-read-only nil)
8481              (inhibit-point-motion-hooks t)
8482              (hidden (if (numberp arg)
8483                          (>= arg 0)
8484                        (gnus-article-hidden-text-p 'headers)))
8485              s e)
8486         (delete-region (point-min) (point-max))
8487         (with-current-buffer gnus-original-article-buffer
8488           (goto-char (setq s (point-min)))
8489           (setq e (if (search-forward "\n\n" nil t)
8490                       (1- (point))
8491                     (point-max))))
8492         (insert-buffer-substring gnus-original-article-buffer s e)
8493         (article-decode-encoded-words)
8494         (if hidden
8495             (let ((gnus-treat-hide-headers nil)
8496                   (gnus-treat-hide-boring-headers nil))
8497               (gnus-delete-wash-type 'headers)
8498               (gnus-treat-article 'head))
8499           (gnus-treat-article 'head))
8500         (widen)
8501         (if window
8502             (set-window-start window (goto-char (point-min))))
8503         (setq gnus-page-broken
8504               (when gnus-break-pages
8505                 (gnus-narrow-to-page)
8506                 t))
8507         (gnus-set-mode-line 'article)))))
8508
8509 (defun gnus-summary-show-all-headers ()
8510   "Make all header lines visible."
8511   (interactive)
8512   (gnus-summary-toggle-header 1))
8513
8514 (defun gnus-summary-caesar-message (&optional arg)
8515   "Caesar rotate the current article by 13.
8516 The numerical prefix specifies how many places to rotate each letter
8517 forward."
8518   (interactive "P")
8519   (gnus-summary-select-article)
8520   (let ((mail-header-separator ""))
8521     (gnus-eval-in-buffer-window gnus-article-buffer
8522       (save-restriction
8523         (widen)
8524         (let ((start (window-start))
8525               buffer-read-only)
8526           (message-caesar-buffer-body arg)
8527           (set-window-start (get-buffer-window (current-buffer)) start))))))
8528
8529 (autoload 'unmorse-region "morse"
8530   "Convert morse coded text in region to ordinary ASCII text."
8531   t)
8532
8533 (defun gnus-summary-morse-message (&optional arg)
8534   "Morse decode the current article."
8535   (interactive "P")
8536   (gnus-summary-select-article)
8537   (let ((mail-header-separator ""))
8538     (gnus-eval-in-buffer-window gnus-article-buffer
8539       (save-excursion
8540         (save-restriction
8541           (widen)
8542           (let ((pos (window-start))
8543                 buffer-read-only)
8544             (goto-char (point-min))
8545             (when (message-goto-body)
8546               (gnus-narrow-to-body))
8547             (goto-char (point-min))
8548             (while (re-search-forward "·" (point-max) t)
8549               (replace-match "."))
8550             (unmorse-region (point-min) (point-max))
8551             (widen)
8552             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8553
8554 (defun gnus-summary-stop-page-breaking ()
8555   "Stop page breaking in the current article."
8556   (interactive)
8557   (gnus-summary-select-article)
8558   (gnus-eval-in-buffer-window gnus-article-buffer
8559     (widen)
8560     (when (gnus-visual-p 'page-marker)
8561       (let ((buffer-read-only nil))
8562         (gnus-remove-text-with-property 'gnus-prev)
8563         (gnus-remove-text-with-property 'gnus-next))
8564       (setq gnus-page-broken nil))))
8565
8566 (defun gnus-summary-move-article (&optional n to-newsgroup
8567                                             select-method action)
8568   "Move the current article to a different newsgroup.
8569 If N is a positive number, move the N next articles.
8570 If N is a negative number, move the N previous articles.
8571 If N is nil and any articles have been marked with the process mark,
8572 move those articles instead.
8573 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8574 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8575 re-spool using this method.
8576
8577 When called interactively with TO-NEWSGROUP being nil, the value of
8578 the variable `gnus-move-split-methods' is used for finding a default
8579 for the target newsgroup.
8580
8581 For this function to work, both the current newsgroup and the
8582 newsgroup that you want to move to have to support the `request-move'
8583 and `request-accept' functions.
8584
8585 ACTION can be either `move' (the default), `crosspost' or `copy'."
8586   (interactive "P")
8587   (unless action
8588     (setq action 'move))
8589   ;; Check whether the source group supports the required functions.
8590   (cond ((and (eq action 'move)
8591               (not (gnus-check-backend-function
8592                     'request-move-article gnus-newsgroup-name)))
8593          (error "The current group does not support article moving"))
8594         ((and (eq action 'crosspost)
8595               (not (gnus-check-backend-function
8596                     'request-replace-article gnus-newsgroup-name)))
8597          (error "The current group does not support article editing")))
8598   (let ((articles (gnus-summary-work-articles n))
8599         (prefix (if (gnus-check-backend-function
8600                      'request-move-article gnus-newsgroup-name)
8601                     (gnus-group-real-prefix gnus-newsgroup-name)
8602                   ""))
8603         (names '((move "Move" "Moving")
8604                  (copy "Copy" "Copying")
8605                  (crosspost "Crosspost" "Crossposting")))
8606         (copy-buf (save-excursion
8607                     (nnheader-set-temp-buffer " *copy article*")))
8608         art-group to-method new-xref article to-groups)
8609     (unless (assq action names)
8610       (error "Unknown action %s" action))
8611     ;; Read the newsgroup name.
8612     (when (and (not to-newsgroup)
8613                (not select-method))
8614       (if (and gnus-move-split-methods
8615                (not
8616                 (and (memq gnus-current-article articles)
8617                      (gnus-buffer-live-p gnus-original-article-buffer))))
8618           ;; When `gnus-move-split-methods' is non-nil, we have to
8619           ;; select an article to give `gnus-read-move-group-name' an
8620           ;; opportunity to suggest an appropriate default.  However,
8621           ;; we needn't render or mark the article.
8622           (let ((gnus-display-mime-function nil)
8623                 (gnus-article-prepare-hook nil)
8624                 (gnus-mark-article-hook nil))
8625             (gnus-summary-select-article nil nil nil (car articles))))
8626       (setq to-newsgroup
8627             (gnus-read-move-group-name
8628              (cadr (assq action names))
8629              (symbol-value (intern (format "gnus-current-%s-group" action)))
8630              articles prefix))
8631       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8632     (setq to-method (or select-method
8633                         (gnus-server-to-method
8634                          (gnus-group-method to-newsgroup))))
8635     ;; Check the method we are to move this article to...
8636     (unless (gnus-check-backend-function
8637              'request-accept-article (car to-method))
8638       (error "%s does not support article copying" (car to-method)))
8639     (unless (gnus-check-server to-method)
8640       (error "Can't open server %s" (car to-method)))
8641     (gnus-message 6 "%s to %s: %s..."
8642                   (caddr (assq action names))
8643                   (or (car select-method) to-newsgroup) articles)
8644     (while articles
8645       (setq article (pop articles))
8646       (setq
8647        art-group
8648        (cond
8649         ;; Move the article.
8650         ((eq action 'move)
8651          ;; Remove this article from future suppression.
8652          (gnus-dup-unsuppress-article article)
8653          (gnus-request-move-article
8654           article                       ; Article to move
8655           gnus-newsgroup-name           ; From newsgroup
8656           (nth 1 (gnus-find-method-for-group
8657                   gnus-newsgroup-name)) ; Server
8658           (list 'gnus-request-accept-article
8659                 to-newsgroup (list 'quote select-method)
8660                 (not articles) t)       ; Accept form
8661           (not articles)))              ; Only save nov last time
8662         ;; Copy the article.
8663         ((eq action 'copy)
8664          (save-excursion
8665            (set-buffer copy-buf)
8666            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8667              (gnus-request-accept-article
8668               to-newsgroup select-method (not articles) t))))
8669         ;; Crosspost the article.
8670         ((eq action 'crosspost)
8671          (let ((xref (message-tokenize-header
8672                       (mail-header-xref (gnus-summary-article-header article))
8673                       " ")))
8674            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8675                                   ":" (number-to-string article)))
8676            (unless xref
8677              (setq xref (list (system-name))))
8678            (setq new-xref
8679                  (concat
8680                   (mapconcat 'identity
8681                              (delete "Xref:" (delete new-xref xref))
8682                              " ")
8683                   " " new-xref))
8684            (save-excursion
8685              (set-buffer copy-buf)
8686              ;; First put the article in the destination group.
8687              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8688              (when (consp (setq art-group
8689                                 (gnus-request-accept-article
8690                                  to-newsgroup select-method (not articles))))
8691                (setq new-xref (concat new-xref " " (car art-group)
8692                                       ":"
8693                                       (number-to-string (cdr art-group))))
8694                ;; Now we have the new Xrefs header, so we insert
8695                ;; it and replace the new article.
8696                (nnheader-replace-header "Xref" new-xref)
8697                (gnus-request-replace-article
8698                 (cdr art-group) to-newsgroup (current-buffer))
8699                art-group))))))
8700       (cond
8701        ((not art-group)
8702         (gnus-message 1 "Couldn't %s article %s: %s"
8703                       (cadr (assq action names)) article
8704                       (nnheader-get-report (car to-method))))
8705        ((eq art-group 'junk)
8706         (when (eq action 'move)
8707           (gnus-summary-mark-article article gnus-canceled-mark)
8708           (gnus-message 4 "Deleted article %s" article)))
8709        (t
8710         (let* ((pto-group (gnus-group-prefixed-name
8711                            (car art-group) to-method))
8712                (entry
8713                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8714                (info (nth 2 entry))
8715                (to-group (gnus-info-group info))
8716                to-marks)
8717           ;; Update the group that has been moved to.
8718           (when (and info
8719                      (memq action '(move copy)))
8720             (unless (member to-group to-groups)
8721               (push to-group to-groups))
8722
8723             (unless (memq article gnus-newsgroup-unreads)
8724               (push 'read to-marks)
8725               (gnus-info-set-read
8726                info (gnus-add-to-range (gnus-info-read info)
8727                                        (list (cdr art-group)))))
8728
8729             ;; See whether the article is to be put in the cache.
8730             (let ((marks gnus-article-mark-lists)
8731                   (to-article (cdr art-group)))
8732
8733               ;; Enter the article into the cache in the new group,
8734               ;; if that is required.
8735               (when gnus-use-cache
8736                 (gnus-cache-possibly-enter-article
8737                  to-group to-article
8738                  (memq article gnus-newsgroup-marked)
8739                  (memq article gnus-newsgroup-dormant)
8740                  (memq article gnus-newsgroup-unreads)))
8741
8742               (when gnus-preserve-marks
8743                 ;; Copy any marks over to the new group.
8744                 (when (and (equal to-group gnus-newsgroup-name)
8745                            (not (memq article gnus-newsgroup-unreads)))
8746                   ;; Mark this article as read in this group.
8747                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8748                   (setcdr (gnus-active to-group) to-article)
8749                   (setcdr gnus-newsgroup-active to-article))
8750
8751                 (while marks
8752                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8753                     (when (memq article (symbol-value
8754                                          (intern (format "gnus-newsgroup-%s"
8755                                                          (caar marks)))))
8756                       (push (cdar marks) to-marks)
8757                       ;; If the other group is the same as this group,
8758                       ;; then we have to add the mark to the list.
8759                       (when (equal to-group gnus-newsgroup-name)
8760                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8761                              (cons to-article
8762                                    (symbol-value
8763                                     (intern (format "gnus-newsgroup-%s"
8764                                                     (caar marks)))))))
8765                       ;; Copy the marks to other group.
8766                       (gnus-add-marked-articles
8767                        to-group (cdar marks) (list to-article) info)))
8768                   (setq marks (cdr marks)))
8769
8770                 (gnus-request-set-mark
8771                  to-group (list (list (list to-article) 'add to-marks))))
8772
8773               (gnus-dribble-enter
8774                (concat "(gnus-group-set-info '"
8775                        (gnus-prin1-to-string (gnus-get-info to-group))
8776                        ")"))))
8777
8778           ;; Update the Xref header in this article to point to
8779           ;; the new crossposted article we have just created.
8780           (when (eq action 'crosspost)
8781             (save-excursion
8782               (set-buffer copy-buf)
8783               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8784               (nnheader-replace-header "Xref" new-xref)
8785               (gnus-request-replace-article
8786                article gnus-newsgroup-name (current-buffer)))))
8787
8788         ;;;!!!Why is this necessary?
8789         (set-buffer gnus-summary-buffer)
8790
8791         (gnus-summary-goto-subject article)
8792         (when (eq action 'move)
8793           (gnus-summary-mark-article article gnus-canceled-mark))))
8794       (gnus-summary-remove-process-mark article))
8795     ;; Re-activate all groups that have been moved to.
8796     (save-excursion
8797       (set-buffer gnus-group-buffer)
8798       (let ((gnus-group-marked to-groups))
8799         (gnus-group-get-new-news-this-group nil t)))
8800
8801     (gnus-kill-buffer copy-buf)
8802     (gnus-summary-position-point)
8803     (gnus-set-mode-line 'summary)))
8804
8805 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8806   "Move the current article to a different newsgroup.
8807 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8808 When called interactively, if TO-NEWSGROUP is nil, use the value of
8809 the variable `gnus-move-split-methods' for finding a default target
8810 newsgroup.
8811 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8812 re-spool using this method."
8813   (interactive "P")
8814   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8815
8816 (defun gnus-summary-crosspost-article (&optional n)
8817   "Crosspost the current article to some other group."
8818   (interactive "P")
8819   (gnus-summary-move-article n nil nil 'crosspost))
8820
8821 (defcustom gnus-summary-respool-default-method nil
8822   "Default method type for respooling an article.
8823 If nil, use to the current newsgroup method."
8824   :type 'symbol
8825   :group 'gnus-summary-mail)
8826
8827 (defcustom gnus-summary-display-while-building nil
8828   "If not-nil, show and update the summary buffer as it's being built.
8829 If the value is t, update the buffer after every line is inserted.  If
8830 the value is an integer (N), update the display every N lines."
8831   :group 'gnus-thread
8832   :type '(choice (const :tag "off" nil)
8833                  number
8834                  (const :tag "frequently" t)))
8835
8836 (defun gnus-summary-respool-article (&optional n method)
8837   "Respool the current article.
8838 The article will be squeezed through the mail spooling process again,
8839 which means that it will be put in some mail newsgroup or other
8840 depending on `nnmail-split-methods'.
8841 If N is a positive number, respool the N next articles.
8842 If N is a negative number, respool the N previous articles.
8843 If N is nil and any articles have been marked with the process mark,
8844 respool those articles instead.
8845
8846 Respooling can be done both from mail groups and \"real\" newsgroups.
8847 In the former case, the articles in question will be moved from the
8848 current group into whatever groups they are destined to.  In the
8849 latter case, they will be copied into the relevant groups."
8850   (interactive
8851    (list current-prefix-arg
8852          (let* ((methods (gnus-methods-using 'respool))
8853                 (methname
8854                  (symbol-name (or gnus-summary-respool-default-method
8855                                   (car (gnus-find-method-for-group
8856                                         gnus-newsgroup-name)))))
8857                 (method
8858                  (gnus-completing-read-with-default
8859                   methname "What backend do you want to use when respooling?"
8860                   methods nil t nil 'gnus-mail-method-history))
8861                 ms)
8862            (cond
8863             ((zerop (length (setq ms (gnus-servers-using-backend
8864                                       (intern method)))))
8865              (list (intern method) ""))
8866             ((= 1 (length ms))
8867              (car ms))
8868             (t
8869              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8870                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8871                            ms-alist))))))))
8872   (unless method
8873     (error "No method given for respooling"))
8874   (if (assoc (symbol-name
8875               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8876              (gnus-methods-using 'respool))
8877       (gnus-summary-move-article n nil method)
8878     (gnus-summary-copy-article n nil method)))
8879
8880 (defun gnus-summary-import-article (file &optional edit)
8881   "Import an arbitrary file into a mail newsgroup."
8882   (interactive "fImport file: \nP")
8883   (let ((group gnus-newsgroup-name)
8884         (now (current-time))
8885         atts lines group-art)
8886     (unless (gnus-check-backend-function 'request-accept-article group)
8887       (error "%s does not support article importing" group))
8888     (or (file-readable-p file)
8889         (not (file-regular-p file))
8890         (error "Can't read %s" file))
8891     (save-excursion
8892       (set-buffer (gnus-get-buffer-create " *import file*"))
8893       (erase-buffer)
8894       (nnheader-insert-file-contents file)
8895       (goto-char (point-min))
8896       (if (nnheader-article-p)
8897           (save-restriction
8898             (goto-char (point-min))
8899             (search-forward "\n\n" nil t)
8900             (narrow-to-region (point-min) (1- (point)))
8901             (goto-char (point-min))
8902             (unless (re-search-forward "^date:" nil t)
8903               (goto-char (point-max))
8904               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8905        ;; This doesn't look like an article, so we fudge some headers.
8906         (setq atts (file-attributes file)
8907               lines (count-lines (point-min) (point-max)))
8908         (insert "From: " (read-string "From: ") "\n"
8909                 "Subject: " (read-string "Subject: ") "\n"
8910                 "Date: " (message-make-date (nth 5 atts)) "\n"
8911                 "Message-ID: " (message-make-message-id) "\n"
8912                 "Lines: " (int-to-string lines) "\n"
8913                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8914       (setq group-art (gnus-request-accept-article group nil t))
8915       (kill-buffer (current-buffer)))
8916     (setq gnus-newsgroup-active (gnus-activate-group group))
8917     (forward-line 1)
8918     (gnus-summary-goto-article (cdr group-art) nil t)
8919     (when edit
8920       (gnus-summary-edit-article))))
8921
8922 (defun gnus-summary-create-article ()
8923   "Create an article in a mail newsgroup."
8924   (interactive)
8925   (let ((group gnus-newsgroup-name)
8926         (now (current-time))
8927         group-art)
8928     (unless (gnus-check-backend-function 'request-accept-article group)
8929       (error "%s does not support article importing" group))
8930     (save-excursion
8931       (set-buffer (gnus-get-buffer-create " *import file*"))
8932       (erase-buffer)
8933       (goto-char (point-min))
8934       ;; This doesn't look like an article, so we fudge some headers.
8935       (insert "From: " (read-string "From: ") "\n"
8936               "Subject: " (read-string "Subject: ") "\n"
8937               "Date: " (message-make-date now) "\n"
8938               "Message-ID: " (message-make-message-id) "\n")
8939       (setq group-art (gnus-request-accept-article group nil t))
8940       (kill-buffer (current-buffer)))
8941     (setq gnus-newsgroup-active (gnus-activate-group group))
8942     (forward-line 1)
8943     (gnus-summary-goto-article (cdr group-art) nil t)
8944     (gnus-summary-edit-article)))
8945
8946 (defun gnus-summary-article-posted-p ()
8947   "Say whether the current (mail) article is available from news as well.
8948 This will be the case if the article has both been mailed and posted."
8949   (interactive)
8950   (let ((id (mail-header-references (gnus-summary-article-header)))
8951         (gnus-override-method (car (gnus-refer-article-methods))))
8952     (if (gnus-request-head id "")
8953         (gnus-message 2 "The current message was found on %s"
8954                       gnus-override-method)
8955       (gnus-message 2 "The current message couldn't be found on %s"
8956                     gnus-override-method)
8957       nil)))
8958
8959 (defun gnus-summary-expire-articles (&optional now)
8960   "Expire all articles that are marked as expirable in the current group."
8961   (interactive)
8962   (when (gnus-check-backend-function
8963          'request-expire-articles gnus-newsgroup-name)
8964     ;; This backend supports expiry.
8965     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8966            (expirable (if total
8967                           (progn
8968                             ;; We need to update the info for
8969                             ;; this group for `gnus-list-of-read-articles'
8970                             ;; to give us the right answer.
8971                             (gnus-run-hooks 'gnus-exit-group-hook)
8972                             (gnus-summary-update-info)
8973                             (gnus-list-of-read-articles gnus-newsgroup-name))
8974                         (setq gnus-newsgroup-expirable
8975                               (sort gnus-newsgroup-expirable '<))))
8976            (expiry-wait (if now 'immediate
8977                           (gnus-group-find-parameter
8978                            gnus-newsgroup-name 'expiry-wait)))
8979            (nnmail-expiry-target
8980             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8981                 nnmail-expiry-target))
8982            es)
8983       (when expirable
8984         ;; There are expirable articles in this group, so we run them
8985         ;; through the expiry process.
8986         (gnus-message 6 "Expiring articles...")
8987         (unless (gnus-check-group gnus-newsgroup-name)
8988           (error "Can't open server for %s" gnus-newsgroup-name))
8989         ;; The list of articles that weren't expired is returned.
8990         (save-excursion
8991           (if expiry-wait
8992               (let ((nnmail-expiry-wait-function nil)
8993                     (nnmail-expiry-wait expiry-wait))
8994                 (setq es (gnus-request-expire-articles
8995                           expirable gnus-newsgroup-name)))
8996             (setq es (gnus-request-expire-articles
8997                       expirable gnus-newsgroup-name)))
8998           (unless total
8999             (setq gnus-newsgroup-expirable es))
9000           ;; We go through the old list of expirable, and mark all
9001           ;; really expired articles as nonexistent.
9002           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9003             (let ((gnus-use-cache nil))
9004               (dolist (article expirable)
9005                 (when (and (not (memq article es))
9006                            (gnus-data-find article))
9007                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9008         (gnus-message 6 "Expiring articles...done")))))
9009
9010 (defun gnus-summary-expire-articles-now ()
9011   "Expunge all expirable articles in the current group.
9012 This means that *all* articles that are marked as expirable will be
9013 deleted forever, right now."
9014   (interactive)
9015   (or gnus-expert-user
9016       (gnus-yes-or-no-p
9017        "Are you really, really, really sure you want to delete all these messages? ")
9018       (error "Phew!"))
9019   (gnus-summary-expire-articles t))
9020
9021 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9022 (defun gnus-summary-delete-article (&optional n)
9023   "Delete the N next (mail) articles.
9024 This command actually deletes articles.  This is not a marking
9025 command.  The article will disappear forever from your life, never to
9026 return.
9027 If N is negative, delete backwards.
9028 If N is nil and articles have been marked with the process mark,
9029 delete these instead."
9030   (interactive "P")
9031   (unless (gnus-check-backend-function 'request-expire-articles
9032                                        gnus-newsgroup-name)
9033     (error "The current newsgroup does not support article deletion"))
9034   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9035     (error "Couldn't open server"))
9036   ;; Compute the list of articles to delete.
9037   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9038         (nnmail-expiry-target 'delete)
9039         not-deleted)
9040     (if (and gnus-novice-user
9041              (not (gnus-yes-or-no-p
9042                    (format "Do you really want to delete %s forever? "
9043                            (if (> (length articles) 1)
9044                                (format "these %s articles" (length articles))
9045                              "this article")))))
9046         ()
9047       ;; Delete the articles.
9048       (setq not-deleted (gnus-request-expire-articles
9049                          articles gnus-newsgroup-name 'force))
9050       (while articles
9051         (gnus-summary-remove-process-mark (car articles))
9052         ;; The backend might not have been able to delete the article
9053         ;; after all.
9054         (unless (memq (car articles) not-deleted)
9055           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9056         (setq articles (cdr articles)))
9057       (when not-deleted
9058         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9059     (gnus-summary-position-point)
9060     (gnus-set-mode-line 'summary)
9061     not-deleted))
9062
9063 (defun gnus-summary-edit-article (&optional arg)
9064   "Edit the current article.
9065 This will have permanent effect only in mail groups.
9066 If ARG is nil, edit the decoded articles.
9067 If ARG is 1, edit the raw articles.
9068 If ARG is 2, edit the raw articles even in read-only groups.
9069 If ARG is 3, edit the articles with the current handles.
9070 Otherwise, allow editing of articles even in read-only
9071 groups."
9072   (interactive "P")
9073   (let (force raw current-handles)
9074     (cond
9075      ((null arg))
9076      ((eq arg 1)
9077       (setq raw t))
9078      ((eq arg 2)
9079       (setq raw t
9080             force t))
9081      ((eq arg 3)
9082       (setq current-handles
9083             (and (gnus-buffer-live-p gnus-article-buffer)
9084                  (with-current-buffer gnus-article-buffer
9085                    (prog1
9086                        gnus-article-mime-handles
9087                      (setq gnus-article-mime-handles nil))))))
9088      (t
9089       (setq force t)))
9090     (when (and raw (not force)
9091                (member gnus-newsgroup-name '("nndraft:delayed"
9092                                              "nndraft:drafts"
9093                                              "nndraft:queue")))
9094       (error "Can't edit the raw article in group %s"
9095              gnus-newsgroup-name))
9096     (save-excursion
9097       (set-buffer gnus-summary-buffer)
9098       (let ((mail-parse-charset gnus-newsgroup-charset)
9099             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9100         (gnus-set-global-variables)
9101         (when (and (not force)
9102                    (gnus-group-read-only-p))
9103           (error "The current newsgroup does not support article editing"))
9104         (gnus-summary-show-article t)
9105         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9106           (with-current-buffer gnus-article-buffer
9107             (mm-enable-multibyte)))
9108         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9109             (setq raw t))
9110         (gnus-article-edit-article
9111          (if raw 'ignore
9112            `(lambda ()
9113               (let ((mbl mml-buffer-list))
9114                 (setq mml-buffer-list nil)
9115                 (mime-to-mml ,'current-handles)
9116                 (let ((mbl1 mml-buffer-list))
9117                   (setq mml-buffer-list mbl)
9118                   (set (make-local-variable 'mml-buffer-list) mbl1))
9119                 (make-local-hook 'kill-buffer-hook)
9120                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9121          `(lambda (no-highlight)
9122             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9123                   (message-options message-options)
9124                   (message-options-set-recipient)
9125                   (mail-parse-ignored-charsets
9126                    ',gnus-newsgroup-ignored-charsets))
9127               ,(if (not raw) '(progn
9128                                 (mml-to-mime)
9129                                 (mml-destroy-buffers)
9130                                 (remove-hook 'kill-buffer-hook
9131                                              'mml-destroy-buffers t)
9132                                 (kill-local-variable 'mml-buffer-list)))
9133               (gnus-summary-edit-article-done
9134                ,(or (mail-header-references gnus-current-headers) "")
9135                ,(gnus-group-read-only-p)
9136                ,gnus-summary-buffer no-highlight))))))))
9137
9138 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9139
9140 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9141                                                  no-highlight)
9142   "Make edits to the current article permanent."
9143   (interactive)
9144   (save-excursion
9145    ;; The buffer restriction contains the entire article if it exists.
9146     (when (article-goto-body)
9147       (let ((lines (count-lines (point) (point-max)))
9148             (length (- (point-max) (point)))
9149             (case-fold-search t)
9150             (body (copy-marker (point))))
9151         (goto-char (point-min))
9152         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9153           (delete-region (match-beginning 1) (match-end 1))
9154           (insert (number-to-string length)))
9155         (goto-char (point-min))
9156         (when (re-search-forward
9157                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9158           (delete-region (match-beginning 1) (match-end 1))
9159           (insert (number-to-string length)))
9160         (goto-char (point-min))
9161         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9162           (delete-region (match-beginning 1) (match-end 1))
9163           (insert (number-to-string lines))))))
9164   ;; Replace the article.
9165   (let ((buf (current-buffer)))
9166     (with-temp-buffer
9167       (insert-buffer-substring buf)
9168
9169       (if (and (not read-only)
9170                (not (gnus-request-replace-article
9171                      (cdr gnus-article-current) (car gnus-article-current)
9172                      (current-buffer) t)))
9173           (error "Couldn't replace article")
9174         ;; Update the summary buffer.
9175         (if (and references
9176                  (equal (message-tokenize-header references " ")
9177                         (message-tokenize-header
9178                          (or (message-fetch-field "references") "") " ")))
9179             ;; We only have to update this line.
9180             (save-excursion
9181               (save-restriction
9182                 (message-narrow-to-head)
9183                 (let ((head (buffer-string))
9184                       header)
9185                   (with-temp-buffer
9186                     (insert (format "211 %d Article retrieved.\n"
9187                                     (cdr gnus-article-current)))
9188                     (insert head)
9189                     (insert ".\n")
9190                     (let ((nntp-server-buffer (current-buffer)))
9191                       (setq header (car (gnus-get-newsgroup-headers
9192                                          nil t))))
9193                     (save-excursion
9194                       (set-buffer gnus-summary-buffer)
9195                       (gnus-data-set-header
9196                        (gnus-data-find (cdr gnus-article-current))
9197                        header)
9198                       (gnus-summary-update-article-line
9199                        (cdr gnus-article-current) header)
9200                       (if (gnus-summary-goto-subject
9201                            (cdr gnus-article-current) nil t)
9202                           (gnus-summary-update-secondary-mark
9203                            (cdr gnus-article-current))))))))
9204           ;; Update threads.
9205           (set-buffer (or buffer gnus-summary-buffer))
9206           (gnus-summary-update-article (cdr gnus-article-current))
9207           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9208               (gnus-summary-update-secondary-mark
9209                (cdr gnus-article-current))))
9210         ;; Prettify the article buffer again.
9211         (unless no-highlight
9212           (save-excursion
9213             (set-buffer gnus-article-buffer)
9214             ;;;!!! Fix this -- article should be rehighlighted.
9215             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9216             (set-buffer gnus-original-article-buffer)
9217             (gnus-request-article
9218              (cdr gnus-article-current)
9219              (car gnus-article-current) (current-buffer))))
9220         ;; Prettify the summary buffer line.
9221         (when (gnus-visual-p 'summary-highlight 'highlight)
9222           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9223
9224 (defun gnus-summary-edit-wash (key)
9225   "Perform editing command KEY in the article buffer."
9226   (interactive
9227    (list
9228     (progn
9229       (message "%s" (concat (this-command-keys) "- "))
9230       (read-char))))
9231   (message "")
9232   (gnus-summary-edit-article)
9233   (execute-kbd-macro (concat (this-command-keys) key))
9234   (gnus-article-edit-done))
9235
9236 ;;; Respooling
9237
9238 (defun gnus-summary-respool-query (&optional silent trace)
9239   "Query where the respool algorithm would put this article."
9240   (interactive)
9241   (let (gnus-mark-article-hook)
9242     (gnus-summary-select-article)
9243     (save-excursion
9244       (set-buffer gnus-original-article-buffer)
9245       (save-restriction
9246         (message-narrow-to-head)
9247         (let ((groups (nnmail-article-group 'identity trace)))
9248           (unless silent
9249             (if groups
9250                 (message "This message would go to %s"
9251                          (mapconcat 'car groups ", "))
9252               (message "This message would go to no groups"))
9253             groups))))))
9254
9255 (defun gnus-summary-respool-trace ()
9256   "Trace where the respool algorithm would put this article.
9257 Display a buffer showing all fancy splitting patterns which matched."
9258   (interactive)
9259   (gnus-summary-respool-query nil t))
9260
9261 ;; Summary marking commands.
9262
9263 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9264   "Mark articles which has the same subject as read, and then select the next.
9265 If UNMARK is positive, remove any kind of mark.
9266 If UNMARK is negative, tick articles."
9267   (interactive "P")
9268   (when unmark
9269     (setq unmark (prefix-numeric-value unmark)))
9270   (let ((count
9271          (gnus-summary-mark-same-subject
9272           (gnus-summary-article-subject) unmark)))
9273     ;; Select next unread article.  If auto-select-same mode, should
9274     ;; select the first unread article.
9275     (gnus-summary-next-article t (and gnus-auto-select-same
9276                                       (gnus-summary-article-subject)))
9277     (gnus-message 7 "%d article%s marked as %s"
9278                   count (if (= count 1) " is" "s are")
9279                   (if unmark "unread" "read"))))
9280
9281 (defun gnus-summary-kill-same-subject (&optional unmark)
9282   "Mark articles which has the same subject as read.
9283 If UNMARK is positive, remove any kind of mark.
9284 If UNMARK is negative, tick articles."
9285   (interactive "P")
9286   (when unmark
9287     (setq unmark (prefix-numeric-value unmark)))
9288   (let ((count
9289          (gnus-summary-mark-same-subject
9290           (gnus-summary-article-subject) unmark)))
9291     ;; If marked as read, go to next unread subject.
9292     (when (null unmark)
9293       ;; Go to next unread subject.
9294       (gnus-summary-next-subject 1 t))
9295     (gnus-message 7 "%d articles are marked as %s"
9296                   count (if unmark "unread" "read"))))
9297
9298 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9299   "Mark articles with same SUBJECT as read, and return marked number.
9300 If optional argument UNMARK is positive, remove any kinds of marks.
9301 If optional argument UNMARK is negative, mark articles as unread instead."
9302   (let ((count 1))
9303     (save-excursion
9304       (cond
9305        ((null unmark)                   ; Mark as read.
9306         (while (and
9307                 (progn
9308                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9309                   (gnus-summary-show-thread) t)
9310                 (gnus-summary-find-subject subject))
9311           (setq count (1+ count))))
9312        ((> unmark 0)                    ; Tick.
9313         (while (and
9314                 (progn
9315                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9316                   (gnus-summary-show-thread) t)
9317                 (gnus-summary-find-subject subject))
9318           (setq count (1+ count))))
9319        (t                               ; Mark as unread.
9320         (while (and
9321                 (progn
9322                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9323                   (gnus-summary-show-thread) t)
9324                 (gnus-summary-find-subject subject))
9325           (setq count (1+ count)))))
9326       (gnus-set-mode-line 'summary)
9327       ;; Return the number of marked articles.
9328       count)))
9329
9330 (defun gnus-summary-mark-as-processable (n &optional unmark)
9331   "Set the process mark on the next N articles.
9332 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9333 the process mark instead.  The difference between N and the actual
9334 number of articles marked is returned."
9335   (interactive "P")
9336   (if (and (null n) (gnus-region-active-p))
9337       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9338     (setq n (prefix-numeric-value n))
9339     (let ((backward (< n 0))
9340           (n (abs n)))
9341       (while (and
9342               (> n 0)
9343               (if unmark
9344                   (gnus-summary-remove-process-mark
9345                    (gnus-summary-article-number))
9346                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9347               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9348         (setq n (1- n)))
9349       (when (/= 0 n)
9350         (gnus-message 7 "No more articles"))
9351       (gnus-summary-recenter)
9352       (gnus-summary-position-point)
9353       n)))
9354
9355 (defun gnus-summary-unmark-as-processable (n)
9356   "Remove the process mark from the next N articles.
9357 If N is negative, unmark backward instead.  The difference between N and
9358 the actual number of articles unmarked is returned."
9359   (interactive "P")
9360   (gnus-summary-mark-as-processable n t))
9361
9362 (defun gnus-summary-unmark-all-processable ()
9363   "Remove the process mark from all articles."
9364   (interactive)
9365   (save-excursion
9366     (while gnus-newsgroup-processable
9367       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9368   (gnus-summary-position-point))
9369
9370 (defun gnus-summary-add-mark (article type)
9371   "Mark ARTICLE with a mark of TYPE."
9372   (let ((vtype (car (assq type gnus-article-mark-lists)))
9373         var)
9374     (if (not vtype)
9375         (error "No such mark type: %s" type)
9376       (setq var (intern (format "gnus-newsgroup-%s" type)))
9377       (set var (cons article (symbol-value var)))
9378       (if (memq type '(processable cached replied forwarded recent saved))
9379           (gnus-summary-update-secondary-mark article)
9380         ;;; !!! This is bogus.  We should find out what primary
9381         ;;; !!! mark we want to set.
9382         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9383
9384 (defun gnus-summary-mark-as-expirable (n)
9385   "Mark N articles forward as expirable.
9386 If N is negative, mark backward instead.  The difference between N and
9387 the actual number of articles marked is returned."
9388   (interactive "p")
9389   (gnus-summary-mark-forward n gnus-expirable-mark))
9390
9391 (defun gnus-summary-mark-as-spam (n)
9392   "Mark N articles forward as spam.
9393 If N is negative, mark backward instead.  The difference between N and
9394 the actual number of articles marked is returned."
9395   (interactive "p")
9396   (gnus-summary-mark-forward n gnus-spam-mark))
9397
9398 (defun gnus-summary-mark-article-as-replied (article)
9399   "Mark ARTICLE as replied to and update the summary line.
9400 ARTICLE can also be a list of articles."
9401   (interactive (list (gnus-summary-article-number)))
9402   (let ((articles (if (listp article) article (list article))))
9403     (dolist (article articles)
9404       (push article gnus-newsgroup-replied)
9405       (let ((buffer-read-only nil))
9406         (when (gnus-summary-goto-subject article nil t)
9407           (gnus-summary-update-secondary-mark article))))))
9408
9409 (defun gnus-summary-mark-article-as-forwarded (article)
9410   "Mark ARTICLE as forwarded and update the summary line.
9411 ARTICLE can also be a list of articles."
9412   (let ((articles (if (listp article) article (list article))))
9413     (dolist (article articles)
9414       (push article gnus-newsgroup-forwarded)
9415       (let ((buffer-read-only nil))
9416         (when (gnus-summary-goto-subject article nil t)
9417           (gnus-summary-update-secondary-mark article))))))
9418
9419 (defun gnus-summary-set-bookmark (article)
9420   "Set a bookmark in current article."
9421   (interactive (list (gnus-summary-article-number)))
9422   (when (or (not (get-buffer gnus-article-buffer))
9423             (not gnus-current-article)
9424             (not gnus-article-current)
9425             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9426     (error "No current article selected"))
9427   ;; Remove old bookmark, if one exists.
9428   (gnus-pull article gnus-newsgroup-bookmarks)
9429   ;; Set the new bookmark, which is on the form
9430   ;; (article-number . line-number-in-body).
9431   (push
9432    (cons article
9433          (save-excursion
9434            (set-buffer gnus-article-buffer)
9435            (count-lines
9436             (min (point)
9437                  (save-excursion
9438                    (article-goto-body)
9439                    (point)))
9440             (point))))
9441    gnus-newsgroup-bookmarks)
9442   (gnus-message 6 "A bookmark has been added to the current article."))
9443
9444 (defun gnus-summary-remove-bookmark (article)
9445   "Remove the bookmark from the current article."
9446   (interactive (list (gnus-summary-article-number)))
9447   ;; Remove old bookmark, if one exists.
9448   (if (not (assq article gnus-newsgroup-bookmarks))
9449       (gnus-message 6 "No bookmark in current article.")
9450     (gnus-pull article gnus-newsgroup-bookmarks)
9451     (gnus-message 6 "Removed bookmark.")))
9452
9453 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9454 (defun gnus-summary-mark-as-dormant (n)
9455   "Mark N articles forward as dormant.
9456 If N is negative, mark backward instead.  The difference between N and
9457 the actual number of articles marked is returned."
9458   (interactive "p")
9459   (gnus-summary-mark-forward n gnus-dormant-mark))
9460
9461 (defun gnus-summary-set-process-mark (article)
9462   "Set the process mark on ARTICLE and update the summary line."
9463   (setq gnus-newsgroup-processable
9464         (cons article
9465               (delq article gnus-newsgroup-processable)))
9466   (when (gnus-summary-goto-subject article)
9467     (gnus-summary-show-thread)
9468     (gnus-summary-goto-subject article)
9469     (gnus-summary-update-secondary-mark article)))
9470
9471 (defun gnus-summary-remove-process-mark (article)
9472   "Remove the process mark from ARTICLE and update the summary line."
9473   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9474   (when (gnus-summary-goto-subject article)
9475     (gnus-summary-show-thread)
9476     (gnus-summary-goto-subject article)
9477     (gnus-summary-update-secondary-mark article)))
9478
9479 (defun gnus-summary-set-saved-mark (article)
9480   "Set the process mark on ARTICLE and update the summary line."
9481   (push article gnus-newsgroup-saved)
9482   (when (gnus-summary-goto-subject article)
9483     (gnus-summary-update-secondary-mark article)))
9484
9485 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9486   "Mark N articles as read forwards.
9487 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9488 The difference between N and the actual number of articles marked is
9489 returned.
9490 If NO-EXPIRE, auto-expiry will be inhibited."
9491   (interactive "p")
9492   (gnus-summary-show-thread)
9493   (let ((backward (< n 0))
9494         (gnus-summary-goto-unread
9495          (and gnus-summary-goto-unread
9496               (not (eq gnus-summary-goto-unread 'never))
9497               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9498                                     gnus-ticked-mark gnus-dormant-mark)))))
9499         (n (abs n))
9500         (mark (or mark gnus-del-mark)))
9501     (while (and (> n 0)
9502                 (gnus-summary-mark-article nil mark no-expire)
9503                 (zerop (gnus-summary-next-subject
9504                         (if backward -1 1)
9505                         (and gnus-summary-goto-unread
9506                              (not (eq gnus-summary-goto-unread 'never)))
9507                         t)))
9508       (setq n (1- n)))
9509     (when (/= 0 n)
9510       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9511     (gnus-summary-recenter)
9512     (gnus-summary-position-point)
9513     (gnus-set-mode-line 'summary)
9514     n))
9515
9516 (defun gnus-summary-mark-article-as-read (mark)
9517   "Mark the current article quickly as read with MARK."
9518   (let ((article (gnus-summary-article-number)))
9519     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9520     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9521     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9522     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9523     (push (cons article mark) gnus-newsgroup-reads)
9524     ;; Possibly remove from cache, if that is used.
9525     (when gnus-use-cache
9526       (gnus-cache-enter-remove-article article))
9527     ;; Allow the backend to change the mark.
9528     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9529     ;; Check for auto-expiry.
9530     (when (and gnus-newsgroup-auto-expire
9531                (memq mark gnus-auto-expirable-marks))
9532       (setq mark gnus-expirable-mark)
9533       ;; Let the backend know about the mark change.
9534       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9535       (push article gnus-newsgroup-expirable))
9536     ;; Set the mark in the buffer.
9537     (gnus-summary-update-mark mark 'unread)
9538     t))
9539
9540 (defun gnus-summary-mark-article-as-unread (mark)
9541   "Mark the current article quickly as unread with MARK."
9542   (let* ((article (gnus-summary-article-number))
9543          (old-mark (gnus-summary-article-mark article)))
9544     ;; Allow the backend to change the mark.
9545     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9546     (if (eq mark old-mark)
9547         t
9548       (if (<= article 0)
9549           (progn
9550             (gnus-error 1 "Can't mark negative article numbers")
9551             nil)
9552         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9553         (setq gnus-newsgroup-spam-marked
9554               (delq article gnus-newsgroup-spam-marked))
9555         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9556         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9557         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9558         (cond ((= mark gnus-ticked-mark)
9559                (setq gnus-newsgroup-marked
9560                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9561                                               article)))
9562               ((= mark gnus-spam-mark)
9563                (setq gnus-newsgroup-spam-marked
9564                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9565                                               article)))
9566               ((= mark gnus-dormant-mark)
9567                (setq gnus-newsgroup-dormant
9568                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9569                                               article)))
9570               (t
9571                (setq gnus-newsgroup-unreads
9572                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9573                                               article))))
9574         (gnus-pull article gnus-newsgroup-reads)
9575
9576         ;; See whether the article is to be put in the cache.
9577         (and gnus-use-cache
9578              (vectorp (gnus-summary-article-header article))
9579              (save-excursion
9580                (gnus-cache-possibly-enter-article
9581                 gnus-newsgroup-name article
9582                 (= mark gnus-ticked-mark)
9583                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9584
9585         ;; Fix the mark.
9586         (gnus-summary-update-mark mark 'unread)
9587         t))))
9588
9589 (defun gnus-summary-mark-article (&optional article mark no-expire)
9590   "Mark ARTICLE with MARK.  MARK can be any character.
9591 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9592 `??' (dormant) and `?E' (expirable).
9593 If MARK is nil, then the default character `?r' is used.
9594 If ARTICLE is nil, then the article on the current line will be
9595 marked.
9596 If NO-EXPIRE, auto-expiry will be inhibited."
9597   ;; The mark might be a string.
9598   (when (stringp mark)
9599     (setq mark (aref mark 0)))
9600   ;; If no mark is given, then we check auto-expiring.
9601   (when (null mark)
9602     (setq mark gnus-del-mark))
9603   (when (and (not no-expire)
9604              gnus-newsgroup-auto-expire
9605              (memq mark gnus-auto-expirable-marks))
9606     (setq mark gnus-expirable-mark))
9607   (let ((article (or article (gnus-summary-article-number)))
9608         (old-mark (gnus-summary-article-mark article)))
9609     ;; Allow the backend to change the mark.
9610     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9611     (if (eq mark old-mark)
9612         t
9613       (unless article
9614         (error "No article on current line"))
9615       (if (not (if (or (= mark gnus-unread-mark)
9616                        (= mark gnus-ticked-mark)
9617                        (= mark gnus-spam-mark)
9618                        (= mark gnus-dormant-mark))
9619                    (gnus-mark-article-as-unread article mark)
9620                  (gnus-mark-article-as-read article mark)))
9621           t
9622         ;; See whether the article is to be put in the cache.
9623         (and gnus-use-cache
9624              (not (= mark gnus-canceled-mark))
9625              (vectorp (gnus-summary-article-header article))
9626              (save-excursion
9627                (gnus-cache-possibly-enter-article
9628                 gnus-newsgroup-name article
9629                 (= mark gnus-ticked-mark)
9630                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9631
9632         (when (gnus-summary-goto-subject article nil t)
9633           (let ((buffer-read-only nil))
9634             (gnus-summary-show-thread)
9635             ;; Fix the mark.
9636             (gnus-summary-update-mark mark 'unread)
9637             t))))))
9638
9639 (defun gnus-summary-update-secondary-mark (article)
9640   "Update the secondary (read, process, cache) mark."
9641   (gnus-summary-update-mark
9642    (cond ((memq article gnus-newsgroup-processable)
9643           gnus-process-mark)
9644          ((memq article gnus-newsgroup-cached)
9645           gnus-cached-mark)
9646          ((memq article gnus-newsgroup-replied)
9647           gnus-replied-mark)
9648          ((memq article gnus-newsgroup-forwarded)
9649           gnus-forwarded-mark)
9650          ((memq article gnus-newsgroup-saved)
9651           gnus-saved-mark)
9652          ((memq article gnus-newsgroup-recent)
9653           gnus-recent-mark)
9654          ((memq article gnus-newsgroup-unseen)
9655           gnus-unseen-mark)
9656          (t gnus-no-mark))
9657    'replied)
9658   (when (gnus-visual-p 'summary-highlight 'highlight)
9659     (gnus-run-hooks 'gnus-summary-update-hook))
9660   t)
9661
9662 (defun gnus-summary-update-download-mark (article)
9663   "Update the secondary (read, process, cache) mark."
9664   (gnus-summary-update-mark
9665    (cond ((memq article gnus-newsgroup-undownloaded) 
9666           gnus-undownloaded-mark)
9667          (gnus-newsgroup-agentized
9668           gnus-downloaded-mark)
9669          (t
9670           gnus-no-mark))
9671    'download)
9672   (gnus-summary-update-line t)
9673   t)
9674
9675 (defun gnus-summary-update-mark (mark type)
9676   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9677         (buffer-read-only nil))
9678     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9679     (when forward
9680       (when (looking-at "\r")
9681         (incf forward))
9682       (when (<= (+ forward (point)) (point-max))
9683         ;; Go to the right position on the line.
9684         (goto-char (+ forward (point)))
9685         ;; Replace the old mark with the new mark.
9686         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9687         ;; Optionally update the marks by some user rule.
9688         (when (eq type 'unread)
9689           (gnus-data-set-mark
9690            (gnus-data-find (gnus-summary-article-number)) mark)
9691           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9692
9693 (defun gnus-mark-article-as-read (article &optional mark)
9694   "Enter ARTICLE in the pertinent lists and remove it from others."
9695   ;; Make the article expirable.
9696   (let ((mark (or mark gnus-del-mark)))
9697     (setq gnus-newsgroup-expirable
9698           (if (= mark gnus-expirable-mark)
9699               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9700             (delq article gnus-newsgroup-expirable)))
9701     ;; Remove from unread and marked lists.
9702     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9703     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9704     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9705     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9706     (push (cons article mark) gnus-newsgroup-reads)
9707     ;; Possibly remove from cache, if that is used.
9708     (when gnus-use-cache
9709       (gnus-cache-enter-remove-article article))
9710     t))
9711
9712 (defun gnus-mark-article-as-unread (article &optional mark)
9713   "Enter ARTICLE in the pertinent lists and remove it from others."
9714   (let ((mark (or mark gnus-ticked-mark)))
9715     (if (<= article 0)
9716         (progn
9717           (gnus-error 1 "Can't mark negative article numbers")
9718           nil)
9719       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9720             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9721             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9722             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9723             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9724
9725       ;; Unsuppress duplicates?
9726       (when gnus-suppress-duplicates
9727         (gnus-dup-unsuppress-article article))
9728
9729       (cond ((= mark gnus-ticked-mark)
9730              (setq gnus-newsgroup-marked
9731                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9732             ((= mark gnus-spam-mark)
9733              (setq gnus-newsgroup-spam-marked
9734                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9735                                             article)))
9736             ((= mark gnus-dormant-mark)
9737              (setq gnus-newsgroup-dormant
9738                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9739             (t
9740              (setq gnus-newsgroup-unreads
9741                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9742       (gnus-pull article gnus-newsgroup-reads)
9743       t)))
9744
9745 (defalias 'gnus-summary-mark-as-unread-forward
9746   'gnus-summary-tick-article-forward)
9747 (make-obsolete 'gnus-summary-mark-as-unread-forward
9748                'gnus-summary-tick-article-forward)
9749 (defun gnus-summary-tick-article-forward (n)
9750   "Tick N articles forwards.
9751 If N is negative, tick backwards instead.
9752 The difference between N and the number of articles ticked is returned."
9753   (interactive "p")
9754   (gnus-summary-mark-forward n gnus-ticked-mark))
9755
9756 (defalias 'gnus-summary-mark-as-unread-backward
9757   'gnus-summary-tick-article-backward)
9758 (make-obsolete 'gnus-summary-mark-as-unread-backward
9759                'gnus-summary-tick-article-backward)
9760 (defun gnus-summary-tick-article-backward (n)
9761   "Tick N articles backwards.
9762 The difference between N and the number of articles ticked is returned."
9763   (interactive "p")
9764   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9765
9766 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9767 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9768 (defun gnus-summary-tick-article (&optional article clear-mark)
9769   "Mark current article as unread.
9770 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9771 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9772   (interactive)
9773   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9774                                        gnus-ticked-mark)))
9775
9776 (defun gnus-summary-mark-as-read-forward (n)
9777   "Mark N articles as read forwards.
9778 If N is negative, mark backwards instead.
9779 The difference between N and the actual number of articles marked is
9780 returned."
9781   (interactive "p")
9782   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9783
9784 (defun gnus-summary-mark-as-read-backward (n)
9785   "Mark the N articles as read backwards.
9786 The difference between N and the actual number of articles marked is
9787 returned."
9788   (interactive "p")
9789   (gnus-summary-mark-forward
9790    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9791
9792 (defun gnus-summary-mark-as-read (&optional article mark)
9793   "Mark current article as read.
9794 ARTICLE specifies the article to be marked as read.
9795 MARK specifies a string to be inserted at the beginning of the line."
9796   (gnus-summary-mark-article article mark))
9797
9798 (defun gnus-summary-clear-mark-forward (n)
9799   "Clear marks from N articles forward.
9800 If N is negative, clear backward instead.
9801 The difference between N and the number of marks cleared is returned."
9802   (interactive "p")
9803   (gnus-summary-mark-forward n gnus-unread-mark))
9804
9805 (defun gnus-summary-clear-mark-backward (n)
9806   "Clear marks from N articles backward.
9807 The difference between N and the number of marks cleared is returned."
9808   (interactive "p")
9809   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9810
9811 (defun gnus-summary-mark-unread-as-read ()
9812   "Intended to be used by `gnus-summary-mark-article-hook'."
9813   (when (memq gnus-current-article gnus-newsgroup-unreads)
9814     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9815
9816 (defun gnus-summary-mark-read-and-unread-as-read ()
9817   "Intended to be used by `gnus-summary-mark-article-hook'."
9818   (let ((mark (gnus-summary-article-mark)))
9819     (when (or (gnus-unread-mark-p mark)
9820               (gnus-read-mark-p mark))
9821       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9822
9823 (defun gnus-summary-mark-unread-as-ticked ()
9824   "Intended to be used by `gnus-summary-mark-article-hook'."
9825   (when (memq gnus-current-article gnus-newsgroup-unreads)
9826     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9827
9828 (defun gnus-summary-mark-region-as-read (point mark all)
9829   "Mark all unread articles between point and mark as read.
9830 If given a prefix, mark all articles between point and mark as read,
9831 even ticked and dormant ones."
9832   (interactive "r\nP")
9833   (save-excursion
9834     (let (article)
9835       (goto-char point)
9836       (beginning-of-line)
9837       (while (and
9838               (< (point) mark)
9839               (progn
9840                 (when (or all
9841                           (memq (setq article (gnus-summary-article-number))
9842                                 gnus-newsgroup-unreads))
9843                   (gnus-summary-mark-article article gnus-del-mark))
9844                 t)
9845               (gnus-summary-find-next))))))
9846
9847 (defun gnus-summary-mark-below (score mark)
9848   "Mark articles with score less than SCORE with MARK."
9849   (interactive "P\ncMark: ")
9850   (setq score (if score
9851                   (prefix-numeric-value score)
9852                 (or gnus-summary-default-score 0)))
9853   (save-excursion
9854     (set-buffer gnus-summary-buffer)
9855     (goto-char (point-min))
9856     (while
9857         (progn
9858           (and (< (gnus-summary-article-score) score)
9859                (gnus-summary-mark-article nil mark))
9860           (gnus-summary-find-next)))))
9861
9862 (defun gnus-summary-kill-below (&optional score)
9863   "Mark articles with score below SCORE as read."
9864   (interactive "P")
9865   (gnus-summary-mark-below score gnus-killed-mark))
9866
9867 (defun gnus-summary-clear-above (&optional score)
9868   "Clear all marks from articles with score above SCORE."
9869   (interactive "P")
9870   (gnus-summary-mark-above score gnus-unread-mark))
9871
9872 (defun gnus-summary-tick-above (&optional score)
9873   "Tick all articles with score above SCORE."
9874   (interactive "P")
9875   (gnus-summary-mark-above score gnus-ticked-mark))
9876
9877 (defun gnus-summary-mark-above (score mark)
9878   "Mark articles with score over SCORE with MARK."
9879   (interactive "P\ncMark: ")
9880   (setq score (if score
9881                   (prefix-numeric-value score)
9882                 (or gnus-summary-default-score 0)))
9883   (save-excursion
9884     (set-buffer gnus-summary-buffer)
9885     (goto-char (point-min))
9886     (while (and (progn
9887                   (when (> (gnus-summary-article-score) score)
9888                     (gnus-summary-mark-article nil mark))
9889                   t)
9890                 (gnus-summary-find-next)))))
9891
9892 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9893 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9894 (defun gnus-summary-limit-include-expunged (&optional no-error)
9895   "Display all the hidden articles that were expunged for low scores."
9896   (interactive)
9897   (let ((buffer-read-only nil))
9898     (let ((scored gnus-newsgroup-scored)
9899           headers h)
9900       (while scored
9901         (unless (gnus-summary-article-header (caar scored))
9902           (and (setq h (gnus-number-to-header (caar scored)))
9903                (< (cdar scored) gnus-summary-expunge-below)
9904                (push h headers)))
9905         (setq scored (cdr scored)))
9906       (if (not headers)
9907           (when (not no-error)
9908             (error "No expunged articles hidden"))
9909         (goto-char (point-min))
9910         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9911         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9912         (mapcar (lambda (x) (push (mail-header-number x)
9913                                   gnus-newsgroup-limit))
9914                 headers)
9915         (gnus-summary-prepare-unthreaded (nreverse headers))
9916         (goto-char (point-min))
9917         (gnus-summary-position-point)
9918         t))))
9919
9920 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9921   "Mark all unread articles in this newsgroup as read.
9922 If prefix argument ALL is non-nil, ticked and dormant articles will
9923 also be marked as read.
9924 If QUIETLY is non-nil, no questions will be asked.
9925 If TO-HERE is non-nil, it should be a point in the buffer.  All
9926 articles before (after, if REVERSE is set) this point will be marked as read.
9927 Note that this function will only catch up the unread article
9928 in the current summary buffer limitation.
9929 The number of articles marked as read is returned."
9930   (interactive "P")
9931   (prog1
9932       (save-excursion
9933         (when (or quietly
9934                   (not gnus-interactive-catchup) ;Without confirmation?
9935                   gnus-expert-user
9936                   (gnus-y-or-n-p
9937                    (if all
9938                        "Mark absolutely all articles as read? "
9939                      "Mark all unread articles as read? ")))
9940           (if (and not-mark
9941                    (not gnus-newsgroup-adaptive)
9942                    (not gnus-newsgroup-auto-expire)
9943                    (not gnus-suppress-duplicates)
9944                    (or (not gnus-use-cache)
9945                        (eq gnus-use-cache 'passive)))
9946               (progn
9947                 (when all
9948                   (setq gnus-newsgroup-marked nil
9949                         gnus-newsgroup-spam-marked nil
9950                         gnus-newsgroup-dormant nil))
9951                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9952             ;; We actually mark all articles as canceled, which we
9953             ;; have to do when using auto-expiry or adaptive scoring.
9954             (gnus-summary-show-all-threads)
9955             (if (and to-here reverse)
9956                 (progn
9957                   (goto-char to-here)
9958                   (while (and
9959                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9960                           (gnus-summary-find-next (not all) nil nil t))))
9961               (when (gnus-summary-first-subject (not all) t)
9962                 (while (and
9963                         (if to-here (< (point) to-here) t)
9964                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9965                         (gnus-summary-find-next (not all) nil nil t)))))
9966             (gnus-set-mode-line 'summary))
9967           t))
9968     (gnus-summary-position-point)))
9969
9970 (defun gnus-summary-catchup-to-here (&optional all)
9971   "Mark all unticked articles before the current one as read.
9972 If ALL is non-nil, also mark ticked and dormant articles as read."
9973   (interactive "P")
9974   (save-excursion
9975     (gnus-save-hidden-threads
9976       (let ((beg (point)))
9977         ;; We check that there are unread articles.
9978         (when (or all (gnus-summary-find-prev))
9979           (gnus-summary-catchup all t beg)))))
9980   (gnus-summary-position-point))
9981
9982 (defun gnus-summary-catchup-from-here (&optional all)
9983   "Mark all unticked articles after the current one as read.
9984 If ALL is non-nil, also mark ticked and dormant articles as read."
9985   (interactive "P")
9986   (save-excursion
9987     (gnus-save-hidden-threads
9988       (let ((beg (point)))
9989         ;; We check that there are unread articles.
9990         (when (or all (gnus-summary-find-next))
9991           (gnus-summary-catchup all t beg nil t)))))
9992
9993   (gnus-summary-position-point))
9994 (defun gnus-summary-catchup-all (&optional quietly)
9995   "Mark all articles in this newsgroup as read.
9996 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9997 instead, which marks only unread articles as read."
9998   (interactive "P")
9999   (gnus-summary-catchup t quietly))
10000
10001 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10002   "Mark all unread articles in this group as read, then exit.
10003 If prefix argument ALL is non-nil, all articles are marked as read.
10004 If QUIETLY is non-nil, no questions will be asked."
10005   (interactive "P")
10006   (when (gnus-summary-catchup all quietly nil 'fast)
10007     ;; Select next newsgroup or exit.
10008     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10009              (eq gnus-auto-select-next 'quietly))
10010         (gnus-summary-next-group nil)
10011       (gnus-summary-exit))))
10012
10013 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10014   "Mark all articles in this newsgroup as read, and then exit.
10015 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10016 instead, which marks only unread articles as read."
10017   (interactive "P")
10018   (gnus-summary-catchup-and-exit t quietly))
10019
10020 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10021   "Mark all articles in this group as read and select the next group.
10022 If given a prefix, mark all articles, unread as well as ticked, as
10023 read."
10024   (interactive "P")
10025   (save-excursion
10026     (gnus-summary-catchup all))
10027   (gnus-summary-next-group))
10028
10029 ;;;
10030 ;;; with article
10031 ;;;
10032
10033 (defmacro gnus-with-article (article &rest forms)
10034   "Select ARTICLE and perform FORMS in the original article buffer.
10035 Then replace the article with the result."
10036   `(progn
10037      ;; We don't want the article to be marked as read.
10038      (let (gnus-mark-article-hook)
10039        (gnus-summary-select-article t t nil ,article))
10040      (set-buffer gnus-original-article-buffer)
10041      ,@forms
10042      (if (not (gnus-check-backend-function
10043                'request-replace-article (car gnus-article-current)))
10044          (gnus-message 5 "Read-only group; not replacing")
10045        (unless (gnus-request-replace-article
10046                 ,article (car gnus-article-current)
10047                 (current-buffer) t)
10048          (error "Couldn't replace article")))
10049      ;; The cache and backlog have to be flushed somewhat.
10050      (when gnus-keep-backlog
10051        (gnus-backlog-remove-article
10052         (car gnus-article-current) (cdr gnus-article-current)))
10053      (when gnus-use-cache
10054        (gnus-cache-update-article
10055         (car gnus-article-current) (cdr gnus-article-current)))))
10056
10057 (put 'gnus-with-article 'lisp-indent-function 1)
10058 (put 'gnus-with-article 'edebug-form-spec '(form body))
10059
10060 ;; Thread-based commands.
10061
10062 (defun gnus-summary-articles-in-thread (&optional article)
10063   "Return a list of all articles in the current thread.
10064 If ARTICLE is non-nil, return all articles in the thread that starts
10065 with that article."
10066   (let* ((article (or article (gnus-summary-article-number)))
10067          (data (gnus-data-find-list article))
10068          (top-level (gnus-data-level (car data)))
10069          (top-subject
10070           (cond ((null gnus-thread-operation-ignore-subject)
10071                  (gnus-simplify-subject-re
10072                   (mail-header-subject (gnus-data-header (car data)))))
10073                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10074                  (gnus-simplify-subject-fuzzy
10075                   (mail-header-subject (gnus-data-header (car data)))))
10076                 (t nil)))
10077          (end-point (save-excursion
10078                       (if (gnus-summary-go-to-next-thread)
10079                           (point) (point-max))))
10080          articles)
10081     (while (and data
10082                 (< (gnus-data-pos (car data)) end-point))
10083       (when (or (not top-subject)
10084                 (string= top-subject
10085                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10086                              (gnus-simplify-subject-fuzzy
10087                               (mail-header-subject
10088                                (gnus-data-header (car data))))
10089                            (gnus-simplify-subject-re
10090                             (mail-header-subject
10091                              (gnus-data-header (car data)))))))
10092         (push (gnus-data-number (car data)) articles))
10093       (unless (and (setq data (cdr data))
10094                    (> (gnus-data-level (car data)) top-level))
10095         (setq data nil)))
10096     ;; Return the list of articles.
10097     (nreverse articles)))
10098
10099 (defun gnus-summary-rethread-current ()
10100   "Rethread the thread the current article is part of."
10101   (interactive)
10102   (let* ((gnus-show-threads t)
10103          (article (gnus-summary-article-number))
10104          (id (mail-header-id (gnus-summary-article-header)))
10105          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10106     (unless id
10107       (error "No article on the current line"))
10108     (gnus-rebuild-thread id)
10109     (gnus-summary-goto-subject article)))
10110
10111 (defun gnus-summary-reparent-thread ()
10112   "Make the current article child of the marked (or previous) article.
10113
10114 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10115 is non-nil or the Subject: of both articles are the same."
10116   (interactive)
10117   (unless (not (gnus-group-read-only-p))
10118     (error "The current newsgroup does not support article editing"))
10119   (unless (<= (length gnus-newsgroup-processable) 1)
10120     (error "No more than one article may be marked"))
10121   (save-window-excursion
10122     (let ((gnus-article-buffer " *reparent*")
10123           (current-article (gnus-summary-article-number))
10124           ;; First grab the marked article, otherwise one line up.
10125           (parent-article (if (not (null gnus-newsgroup-processable))
10126                               (car gnus-newsgroup-processable)
10127                             (save-excursion
10128                               (if (eq (forward-line -1) 0)
10129                                   (gnus-summary-article-number)
10130                                 (error "Beginning of summary buffer"))))))
10131       (unless (not (eq current-article parent-article))
10132         (error "An article may not be self-referential"))
10133       (let ((message-id (mail-header-id
10134                          (gnus-summary-article-header parent-article))))
10135         (unless (and message-id (not (equal message-id "")))
10136           (error "No message-id in desired parent"))
10137         (gnus-with-article current-article
10138           (save-restriction
10139             (goto-char (point-min))
10140             (message-narrow-to-head)
10141             (if (re-search-forward "^References: " nil t)
10142                 (progn
10143                   (re-search-forward "^[^ \t]" nil t)
10144                   (forward-line -1)
10145                   (end-of-line)
10146                   (insert " " message-id))
10147               (insert "References: " message-id "\n"))))
10148         (set-buffer gnus-summary-buffer)
10149         (gnus-summary-unmark-all-processable)
10150         (gnus-summary-update-article current-article)
10151         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10152             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10153         (gnus-summary-rethread-current)
10154         (gnus-message 3 "Article %d is now the child of article %d"
10155                       current-article parent-article)))))
10156
10157 (defun gnus-summary-toggle-threads (&optional arg)
10158   "Toggle showing conversation threads.
10159 If ARG is positive number, turn showing conversation threads on."
10160   (interactive "P")
10161   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10162     (setq gnus-show-threads
10163           (if (null arg) (not gnus-show-threads)
10164             (> (prefix-numeric-value arg) 0)))
10165     (gnus-summary-prepare)
10166     (gnus-summary-goto-subject current)
10167     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10168     (gnus-summary-position-point)))
10169
10170 (defun gnus-summary-show-all-threads ()
10171   "Show all threads."
10172   (interactive)
10173   (save-excursion
10174     (let ((buffer-read-only nil))
10175       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10176   (gnus-summary-position-point))
10177
10178 (defun gnus-summary-show-thread ()
10179   "Show thread subtrees.
10180 Returns nil if no thread was there to be shown."
10181   (interactive)
10182   (let ((buffer-read-only nil)
10183         (orig (point))
10184         ;; first goto end then to beg, to have point at beg after let
10185         (end (progn (end-of-line) (point)))
10186         (beg (progn (beginning-of-line) (point))))
10187     (prog1
10188         ;; Any hidden lines here?
10189         (search-forward "\r" end t)
10190       (subst-char-in-region beg end ?\^M ?\n t)
10191       (goto-char orig)
10192       (gnus-summary-position-point))))
10193
10194 (defun gnus-summary-maybe-hide-threads ()
10195   "If requested, hide the threads that should be hidden."
10196   (when (and gnus-show-threads
10197              gnus-thread-hide-subtree)
10198     (gnus-summary-hide-all-threads
10199      (if (or (consp gnus-thread-hide-subtree)
10200              (gnus-functionp gnus-thread-hide-subtree))
10201          (gnus-make-predicate gnus-thread-hide-subtree)
10202        nil))))
10203
10204 ;;; Hiding predicates.
10205
10206 (defun gnus-article-unread-p (header)
10207   (memq (mail-header-number header) gnus-newsgroup-unreads))
10208
10209 (defun gnus-article-unseen-p (header)
10210   (memq (mail-header-number header) gnus-newsgroup-unseen))
10211
10212 (defun gnus-map-articles (predicate articles)
10213   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10214   (apply 'gnus-or (mapcar predicate
10215                           (mapcar 'gnus-summary-article-header articles))))
10216
10217 (defun gnus-summary-hide-all-threads (&optional predicate)
10218   "Hide all thread subtrees.
10219 If PREDICATE is supplied, threads that satisfy this predicate
10220 will not be hidden."
10221   (interactive)
10222   (save-excursion
10223     (goto-char (point-min))
10224     (let ((end nil))
10225       (while (not end)
10226         (when (or (not predicate)
10227                   (gnus-map-articles
10228                    predicate (gnus-summary-article-children)))
10229             (gnus-summary-hide-thread))
10230         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10231   (gnus-summary-position-point))
10232
10233 (defun gnus-summary-hide-thread ()
10234   "Hide thread subtrees.
10235 If PREDICATE is supplied, threads that satisfy this predicate
10236 will not be hidden.
10237 Returns nil if no threads were there to be hidden."
10238   (interactive)
10239   (let ((buffer-read-only nil)
10240         (start (point))
10241         (article (gnus-summary-article-number)))
10242     (goto-char start)
10243     ;; Go forward until either the buffer ends or the subthread
10244     ;; ends.
10245     (when (and (not (eobp))
10246                (or (zerop (gnus-summary-next-thread 1 t))
10247                    (goto-char (point-max))))
10248       (prog1
10249           (if (and (> (point) start)
10250                    (search-backward "\n" start t))
10251               (progn
10252                 (subst-char-in-region start (point) ?\n ?\^M)
10253                 (gnus-summary-goto-subject article))
10254             (goto-char start)
10255             nil)))))
10256
10257 (defun gnus-summary-go-to-next-thread (&optional previous)
10258   "Go to the same level (or less) next thread.
10259 If PREVIOUS is non-nil, go to previous thread instead.
10260 Return the article number moved to, or nil if moving was impossible."
10261   (let ((level (gnus-summary-thread-level))
10262         (way (if previous -1 1))
10263         (beg (point)))
10264     (forward-line way)
10265     (while (and (not (eobp))
10266                 (< level (gnus-summary-thread-level)))
10267       (forward-line way))
10268     (if (eobp)
10269         (progn
10270           (goto-char beg)
10271           nil)
10272       (setq beg (point))
10273       (prog1
10274           (gnus-summary-article-number)
10275         (goto-char beg)))))
10276
10277 (defun gnus-summary-next-thread (n &optional silent)
10278   "Go to the same level next N'th thread.
10279 If N is negative, search backward instead.
10280 Returns the difference between N and the number of skips actually
10281 done.
10282
10283 If SILENT, don't output messages."
10284   (interactive "p")
10285   (let ((backward (< n 0))
10286         (n (abs n)))
10287     (while (and (> n 0)
10288                 (gnus-summary-go-to-next-thread backward))
10289       (decf n))
10290     (unless silent
10291       (gnus-summary-position-point))
10292     (when (and (not silent) (/= 0 n))
10293       (gnus-message 7 "No more threads"))
10294     n))
10295
10296 (defun gnus-summary-prev-thread (n)
10297   "Go to the same level previous N'th thread.
10298 Returns the difference between N and the number of skips actually
10299 done."
10300   (interactive "p")
10301   (gnus-summary-next-thread (- n)))
10302
10303 (defun gnus-summary-go-down-thread ()
10304   "Go down one level in the current thread."
10305   (let ((children (gnus-summary-article-children)))
10306     (when children
10307       (gnus-summary-goto-subject (car children)))))
10308
10309 (defun gnus-summary-go-up-thread ()
10310   "Go up one level in the current thread."
10311   (let ((parent (gnus-summary-article-parent)))
10312     (when parent
10313       (gnus-summary-goto-subject parent))))
10314
10315 (defun gnus-summary-down-thread (n)
10316   "Go down thread N steps.
10317 If N is negative, go up instead.
10318 Returns the difference between N and how many steps down that were
10319 taken."
10320   (interactive "p")
10321   (let ((up (< n 0))
10322         (n (abs n)))
10323     (while (and (> n 0)
10324                 (if up (gnus-summary-go-up-thread)
10325                   (gnus-summary-go-down-thread)))
10326       (setq n (1- n)))
10327     (gnus-summary-position-point)
10328     (when (/= 0 n)
10329       (gnus-message 7 "Can't go further"))
10330     n))
10331
10332 (defun gnus-summary-up-thread (n)
10333   "Go up thread N steps.
10334 If N is negative, go down instead.
10335 Returns the difference between N and how many steps down that were
10336 taken."
10337   (interactive "p")
10338   (gnus-summary-down-thread (- n)))
10339
10340 (defun gnus-summary-top-thread ()
10341   "Go to the top of the thread."
10342   (interactive)
10343   (while (gnus-summary-go-up-thread))
10344   (gnus-summary-article-number))
10345
10346 (defun gnus-summary-kill-thread (&optional unmark)
10347   "Mark articles under current thread as read.
10348 If the prefix argument is positive, remove any kinds of marks.
10349 If the prefix argument is negative, tick articles instead."
10350   (interactive "P")
10351   (when unmark
10352     (setq unmark (prefix-numeric-value unmark)))
10353   (let ((articles (gnus-summary-articles-in-thread)))
10354     (save-excursion
10355       ;; Expand the thread.
10356       (gnus-summary-show-thread)
10357       ;; Mark all the articles.
10358       (while articles
10359         (gnus-summary-goto-subject (car articles))
10360         (cond ((null unmark)
10361                (gnus-summary-mark-article-as-read gnus-killed-mark))
10362               ((> unmark 0)
10363                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10364               (t
10365                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10366         (setq articles (cdr articles))))
10367     ;; Hide killed subtrees.
10368     (and (null unmark)
10369          gnus-thread-hide-killed
10370          (gnus-summary-hide-thread))
10371     ;; If marked as read, go to next unread subject.
10372     (when (null unmark)
10373       ;; Go to next unread subject.
10374       (gnus-summary-next-subject 1 t)))
10375   (gnus-set-mode-line 'summary))
10376
10377 ;; Summary sorting commands
10378
10379 (defun gnus-summary-sort-by-number (&optional reverse)
10380   "Sort the summary buffer by article number.
10381 Argument REVERSE means reverse order."
10382   (interactive "P")
10383   (gnus-summary-sort 'number reverse))
10384
10385 (defun gnus-summary-sort-by-random (&optional reverse)
10386   "Randomize the order in the summary buffer.
10387 Argument REVERSE means to randomize in reverse order."
10388   (interactive "P")
10389   (gnus-summary-sort 'random reverse))
10390
10391 (defun gnus-summary-sort-by-author (&optional reverse)
10392   "Sort the summary buffer by author name alphabetically.
10393 If `case-fold-search' is non-nil, case of letters is ignored.
10394 Argument REVERSE means reverse order."
10395   (interactive "P")
10396   (gnus-summary-sort 'author reverse))
10397
10398 (defun gnus-summary-sort-by-subject (&optional reverse)
10399   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10400 If `case-fold-search' is non-nil, case of letters is ignored.
10401 Argument REVERSE means reverse order."
10402   (interactive "P")
10403   (gnus-summary-sort 'subject reverse))
10404
10405 (defun gnus-summary-sort-by-date (&optional reverse)
10406   "Sort the summary buffer by date.
10407 Argument REVERSE means reverse order."
10408   (interactive "P")
10409   (gnus-summary-sort 'date reverse))
10410
10411 (defun gnus-summary-sort-by-score (&optional reverse)
10412   "Sort the summary buffer by score.
10413 Argument REVERSE means reverse order."
10414   (interactive "P")
10415   (gnus-summary-sort 'score reverse))
10416
10417 (defun gnus-summary-sort-by-lines (&optional reverse)
10418   "Sort the summary buffer by the number of lines.
10419 Argument REVERSE means reverse order."
10420   (interactive "P")
10421   (gnus-summary-sort 'lines reverse))
10422
10423 (defun gnus-summary-sort-by-chars (&optional reverse)
10424   "Sort the summary buffer by article length.
10425 Argument REVERSE means reverse order."
10426   (interactive "P")
10427   (gnus-summary-sort 'chars reverse))
10428
10429 (defun gnus-summary-sort-by-original (&optional reverse)
10430   "Sort the summary buffer using the default sorting method.
10431 Argument REVERSE means reverse order."
10432   (interactive "P")
10433   (let* ((buffer-read-only)
10434          (gnus-summary-prepare-hook nil))
10435     ;; We do the sorting by regenerating the threads.
10436     (gnus-summary-prepare)
10437     ;; Hide subthreads if needed.
10438     (gnus-summary-maybe-hide-threads)))
10439
10440 (defun gnus-summary-sort (predicate reverse)
10441   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10442   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10443          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10444          (gnus-thread-sort-functions
10445           (if (not reverse)
10446               thread
10447             `(lambda (t1 t2)
10448                (,thread t2 t1))))
10449          (gnus-sort-gathered-threads-function
10450           gnus-thread-sort-functions)
10451          (gnus-article-sort-functions
10452           (if (not reverse)
10453               article
10454             `(lambda (t1 t2)
10455                (,article t2 t1))))
10456          (buffer-read-only)
10457          (gnus-summary-prepare-hook nil))
10458     ;; We do the sorting by regenerating the threads.
10459     (gnus-summary-prepare)
10460     ;; Hide subthreads if needed.
10461     (gnus-summary-maybe-hide-threads)))
10462
10463 ;; Summary saving commands.
10464
10465 (defun gnus-summary-save-article (&optional n not-saved)
10466   "Save the current article using the default saver function.
10467 If N is a positive number, save the N next articles.
10468 If N is a negative number, save the N previous articles.
10469 If N is nil and any articles have been marked with the process mark,
10470 save those articles instead.
10471 The variable `gnus-default-article-saver' specifies the saver function."
10472   (interactive "P")
10473   (let* ((articles (gnus-summary-work-articles n))
10474          (save-buffer (save-excursion
10475                         (nnheader-set-temp-buffer " *Gnus Save*")))
10476          (num (length articles))
10477          header file)
10478     (dolist (article articles)
10479       (setq header (gnus-summary-article-header article))
10480       (if (not (vectorp header))
10481           ;; This is a pseudo-article.
10482           (if (assq 'name header)
10483               (gnus-copy-file (cdr (assq 'name header)))
10484             (gnus-message 1 "Article %d is unsaveable" article))
10485         ;; This is a real article.
10486         (save-window-excursion
10487           (let ((gnus-display-mime-function nil)
10488                 (gnus-article-prepare-hook nil))
10489             (gnus-summary-select-article t nil nil article)))
10490         (save-excursion
10491           (set-buffer save-buffer)
10492           (erase-buffer)
10493           (insert-buffer-substring gnus-original-article-buffer))
10494         (setq file (gnus-article-save save-buffer file num))
10495         (gnus-summary-remove-process-mark article)
10496         (unless not-saved
10497           (gnus-summary-set-saved-mark article))))
10498     (gnus-kill-buffer save-buffer)
10499     (gnus-summary-position-point)
10500     (gnus-set-mode-line 'summary)
10501     n))
10502
10503 (defun gnus-summary-pipe-output (&optional arg headers)
10504   "Pipe the current article to a subprocess.
10505 If N is a positive number, pipe the N next articles.
10506 If N is a negative number, pipe the N previous articles.
10507 If N is nil and any articles have been marked with the process mark,
10508 pipe those articles instead.
10509 If HEADERS (the symbolic prefix), include the headers, too."
10510   (interactive (gnus-interactive "P\ny"))
10511   (require 'gnus-art)
10512   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10513         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10514     (gnus-summary-save-article arg t))
10515   (let ((buffer (get-buffer "*Shell Command Output*")))
10516     (when (and buffer
10517                (not (zerop (buffer-size buffer))))
10518       (gnus-configure-windows 'pipe))))
10519
10520 (defun gnus-summary-save-article-mail (&optional arg)
10521   "Append the current article to an mail file.
10522 If N is a positive number, save the N next articles.
10523 If N is a negative number, save the N previous articles.
10524 If N is nil and any articles have been marked with the process mark,
10525 save those articles instead."
10526   (interactive "P")
10527   (require 'gnus-art)
10528   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10529     (gnus-summary-save-article arg)))
10530
10531 (defun gnus-summary-save-article-rmail (&optional arg)
10532   "Append the current article to an rmail file.
10533 If N is a positive number, save the N next articles.
10534 If N is a negative number, save the N previous articles.
10535 If N is nil and any articles have been marked with the process mark,
10536 save those articles instead."
10537   (interactive "P")
10538   (require 'gnus-art)
10539   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10540     (gnus-summary-save-article arg)))
10541
10542 (defun gnus-summary-save-article-file (&optional arg)
10543   "Append the current article to a file.
10544 If N is a positive number, save the N next articles.
10545 If N is a negative number, save the N previous articles.
10546 If N is nil and any articles have been marked with the process mark,
10547 save those articles instead."
10548   (interactive "P")
10549   (require 'gnus-art)
10550   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10551     (gnus-summary-save-article arg)))
10552
10553 (defun gnus-summary-write-article-file (&optional arg)
10554   "Write the current article to a file, deleting the previous file.
10555 If N is a positive number, save the N next articles.
10556 If N is a negative number, save the N previous articles.
10557 If N is nil and any articles have been marked with the process mark,
10558 save those articles instead."
10559   (interactive "P")
10560   (require 'gnus-art)
10561   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10562     (gnus-summary-save-article arg)))
10563
10564 (defun gnus-summary-save-article-body-file (&optional arg)
10565   "Append the current article body to a file.
10566 If N is a positive number, save the N next articles.
10567 If N is a negative number, save the N previous articles.
10568 If N is nil and any articles have been marked with the process mark,
10569 save those articles instead."
10570   (interactive "P")
10571   (require 'gnus-art)
10572   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10573     (gnus-summary-save-article arg)))
10574
10575 (defun gnus-summary-muttprint (&optional arg)
10576   "Print the current article using Muttprint.
10577 If N is a positive number, save the N next articles.
10578 If N is a negative number, save the N previous articles.
10579 If N is nil and any articles have been marked with the process mark,
10580 save those articles instead."
10581   (interactive "P")
10582   (require 'gnus-art)
10583   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10584     (gnus-summary-save-article arg t)))
10585
10586 (defun gnus-summary-pipe-message (program)
10587   "Pipe the current article through PROGRAM."
10588   (interactive "sProgram: ")
10589   (gnus-summary-select-article)
10590   (let ((mail-header-separator ""))
10591     (gnus-eval-in-buffer-window gnus-article-buffer
10592       (save-restriction
10593         (widen)
10594         (let ((start (window-start))
10595               buffer-read-only)
10596           (message-pipe-buffer-body program)
10597           (set-window-start (get-buffer-window (current-buffer)) start))))))
10598
10599 (defun gnus-get-split-value (methods)
10600   "Return a value based on the split METHODS."
10601   (let (split-name method result match)
10602     (when methods
10603       (save-excursion
10604         (set-buffer gnus-original-article-buffer)
10605         (save-restriction
10606           (nnheader-narrow-to-headers)
10607           (while (and methods (not split-name))
10608             (goto-char (point-min))
10609             (setq method (pop methods))
10610             (setq match (car method))
10611             (when (cond
10612                    ((stringp match)
10613                     ;; Regular expression.
10614                     (ignore-errors
10615                       (re-search-forward match nil t)))
10616                    ((gnus-functionp match)
10617                     ;; Function.
10618                     (save-restriction
10619                       (widen)
10620                       (setq result (funcall match gnus-newsgroup-name))))
10621                    ((consp match)
10622                     ;; Form.
10623                     (save-restriction
10624                       (widen)
10625                       (setq result (eval match)))))
10626               (setq split-name (cdr method))
10627               (cond ((stringp result)
10628                      (push (expand-file-name
10629                             result gnus-article-save-directory)
10630                            split-name))
10631                     ((consp result)
10632                      (setq split-name (append result split-name)))))))))
10633     (nreverse split-name)))
10634
10635 (defun gnus-valid-move-group-p (group)
10636   (and (boundp group)
10637        (symbol-name group)
10638        (symbol-value group)
10639        (gnus-get-function (gnus-find-method-for-group
10640                            (symbol-name group)) 'request-accept-article t)))
10641
10642 (defun gnus-read-move-group-name (prompt default articles prefix)
10643   "Read a group name."
10644   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10645          (minibuffer-confirm-incomplete nil) ; XEmacs
10646          (prom
10647           (format "%s %s to:"
10648                   prompt
10649                   (if (> (length articles) 1)
10650                       (format "these %d articles" (length articles))
10651                     "this article")))
10652          (to-newsgroup
10653           (cond
10654            ((null split-name)
10655             (gnus-completing-read-with-default
10656              default prom
10657              gnus-active-hashtb
10658              'gnus-valid-move-group-p
10659              nil prefix
10660              'gnus-group-history))
10661            ((= 1 (length split-name))
10662             (gnus-completing-read-with-default
10663              (car split-name) prom
10664              gnus-active-hashtb
10665              'gnus-valid-move-group-p
10666              nil nil
10667              'gnus-group-history))
10668            (t
10669             (gnus-completing-read-with-default
10670              nil prom
10671              (mapcar (lambda (el) (list el))
10672                      (nreverse split-name))
10673              nil nil nil
10674              'gnus-group-history))))
10675          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10676     (when to-newsgroup
10677       (if (or (string= to-newsgroup "")
10678               (string= to-newsgroup prefix))
10679           (setq to-newsgroup default))
10680       (unless to-newsgroup
10681         (error "No group name entered"))
10682       (or (gnus-active to-newsgroup)
10683           (gnus-activate-group to-newsgroup nil nil to-method)
10684           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10685                                      to-newsgroup))
10686               (or (and (gnus-request-create-group to-newsgroup to-method)
10687                        (gnus-activate-group
10688                         to-newsgroup nil nil to-method)
10689                        (gnus-subscribe-group to-newsgroup))
10690                   (error "Couldn't create group %s" to-newsgroup)))
10691           (error "No such group: %s" to-newsgroup)))
10692     to-newsgroup))
10693
10694 (defun gnus-summary-save-parts (type dir n &optional reverse)
10695   "Save parts matching TYPE to DIR.
10696 If REVERSE, save parts that do not match TYPE."
10697   (interactive
10698    (list (read-string "Save parts of type: "
10699                       (or (car gnus-summary-save-parts-type-history)
10700                           gnus-summary-save-parts-default-mime)
10701                       'gnus-summary-save-parts-type-history)
10702          (setq gnus-summary-save-parts-last-directory
10703                (read-file-name "Save to directory: "
10704                                gnus-summary-save-parts-last-directory
10705                                nil t))
10706          current-prefix-arg))
10707   (gnus-summary-iterate n
10708     (let ((gnus-display-mime-function nil)
10709           (gnus-inhibit-treatment t))
10710       (gnus-summary-select-article))
10711     (save-excursion
10712       (set-buffer gnus-article-buffer)
10713       (let ((handles (or gnus-article-mime-handles
10714                          (mm-dissect-buffer nil gnus-article-loose-mime)
10715                          (mm-uu-dissect))))
10716         (when handles
10717           (gnus-summary-save-parts-1 type dir handles reverse)
10718           (unless gnus-article-mime-handles ;; Don't destroy this case.
10719             (mm-destroy-parts handles)))))))
10720
10721 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10722   (if (stringp (car handle))
10723       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10724               (cdr handle))
10725     (when (if reverse
10726               (not (string-match type (mm-handle-media-type handle)))
10727             (string-match type (mm-handle-media-type handle)))
10728       (let ((file (expand-file-name
10729                    (file-name-nondirectory
10730                     (or
10731                      (mail-content-type-get
10732                       (mm-handle-disposition handle) 'filename)
10733                      (concat gnus-newsgroup-name
10734                              "." (number-to-string
10735                                   (cdr gnus-article-current)))))
10736                    dir)))
10737         (unless (file-exists-p file)
10738           (mm-save-part-to-file handle file))))))
10739
10740 ;; Summary extract commands
10741
10742 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10743   (let ((buffer-read-only nil)
10744         (article (gnus-summary-article-number))
10745         after-article b e)
10746     (unless (gnus-summary-goto-subject article)
10747       (error "No such article: %d" article))
10748     (gnus-summary-position-point)
10749     ;; If all commands are to be bunched up on one line, we collect
10750     ;; them here.
10751     (unless gnus-view-pseudos-separately
10752       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10753             files action)
10754         (while ps
10755           (setq action (cdr (assq 'action (car ps))))
10756           (setq files (list (cdr (assq 'name (car ps)))))
10757           (while (and ps (cdr ps)
10758                       (string= (or action "1")
10759                                (or (cdr (assq 'action (cadr ps))) "2")))
10760             (push (cdr (assq 'name (cadr ps))) files)
10761             (setcdr ps (cddr ps)))
10762           (when files
10763             (when (not (string-match "%s" action))
10764               (push " " files))
10765             (push " " files)
10766             (when (assq 'execute (car ps))
10767               (setcdr (assq 'execute (car ps))
10768                       (funcall (if (string-match "%s" action)
10769                                    'format 'concat)
10770                                action
10771                                (mapconcat
10772                                 (lambda (f)
10773                                   (if (equal f " ")
10774                                       f
10775                                     (mm-quote-arg f)))
10776                                 files " ")))))
10777           (setq ps (cdr ps)))))
10778     (if (and gnus-view-pseudos (not not-view))
10779         (while pslist
10780           (when (assq 'execute (car pslist))
10781             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10782                                   (eq gnus-view-pseudos 'not-confirm)))
10783           (setq pslist (cdr pslist)))
10784       (save-excursion
10785         (while pslist
10786           (setq after-article (or (cdr (assq 'article (car pslist)))
10787                                   (gnus-summary-article-number)))
10788           (gnus-summary-goto-subject after-article)
10789           (forward-line 1)
10790           (setq b (point))
10791           (insert "    " (file-name-nondirectory
10792                           (cdr (assq 'name (car pslist))))
10793                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10794           (setq e (point))
10795           (forward-line -1)             ; back to `b'
10796           (gnus-add-text-properties
10797            b (1- e) (list 'gnus-number gnus-reffed-article-number
10798                           gnus-mouse-face-prop gnus-mouse-face))
10799           (gnus-data-enter
10800            after-article gnus-reffed-article-number
10801            gnus-unread-mark b (car pslist) 0 (- e b))
10802           (setq gnus-newsgroup-unreads
10803                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10804                                          gnus-reffed-article-number))
10805           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10806           (setq pslist (cdr pslist)))))))
10807
10808 (defun gnus-pseudos< (p1 p2)
10809   (let ((c1 (cdr (assq 'action p1)))
10810         (c2 (cdr (assq 'action p2))))
10811     (and c1 c2 (string< c1 c2))))
10812
10813 (defun gnus-request-pseudo-article (props)
10814   (cond ((assq 'execute props)
10815          (gnus-execute-command (cdr (assq 'execute props)))))
10816   (let ((gnus-current-article (gnus-summary-article-number)))
10817     (gnus-run-hooks 'gnus-mark-article-hook)))
10818
10819 (defun gnus-execute-command (command &optional automatic)
10820   (save-excursion
10821     (gnus-article-setup-buffer)
10822     (set-buffer gnus-article-buffer)
10823     (setq buffer-read-only nil)
10824     (let ((command (if automatic command
10825                      (read-string "Command: " (cons command 0)))))
10826       (erase-buffer)
10827       (insert "$ " command "\n\n")
10828       (if gnus-view-pseudo-asynchronously
10829           (start-process "gnus-execute" (current-buffer) shell-file-name
10830                          shell-command-switch command)
10831         (call-process shell-file-name nil t nil
10832                       shell-command-switch command)))))
10833
10834 ;; Summary kill commands.
10835
10836 (defun gnus-summary-edit-global-kill (article)
10837   "Edit the \"global\" kill file."
10838   (interactive (list (gnus-summary-article-number)))
10839   (gnus-group-edit-global-kill article))
10840
10841 (defun gnus-summary-edit-local-kill ()
10842   "Edit a local kill file applied to the current newsgroup."
10843   (interactive)
10844   (setq gnus-current-headers (gnus-summary-article-header))
10845   (gnus-group-edit-local-kill
10846    (gnus-summary-article-number) gnus-newsgroup-name))
10847
10848 ;;; Header reading.
10849
10850 (defun gnus-read-header (id &optional header)
10851   "Read the headers of article ID and enter them into the Gnus system."
10852   (let ((group gnus-newsgroup-name)
10853         (gnus-override-method
10854          (or
10855           gnus-override-method
10856           (and (gnus-news-group-p gnus-newsgroup-name)
10857                (car (gnus-refer-article-methods)))))
10858         where)
10859     ;; First we check to see whether the header in question is already
10860     ;; fetched.
10861     (if (stringp id)
10862         ;; This is a Message-ID.
10863         (setq header (or header (gnus-id-to-header id)))
10864       ;; This is an article number.
10865       (setq header (or header (gnus-summary-article-header id))))
10866     (if (and header
10867              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10868         ;; We have found the header.
10869         header
10870       ;; If this is a sparse article, we have to nix out its
10871       ;; previous entry in the thread hashtb.
10872       (when (and header
10873                  (gnus-summary-article-sparse-p (mail-header-number header)))
10874         (let* ((parent (gnus-parent-id (mail-header-references header)))
10875                (thread (and parent (gnus-id-to-thread parent))))
10876           (when thread
10877             (delq (assq header thread) thread))))
10878       ;; We have to really fetch the header to this article.
10879       (save-excursion
10880         (set-buffer nntp-server-buffer)
10881         (when (setq where (gnus-request-head id group))
10882           (nnheader-fold-continuation-lines)
10883           (goto-char (point-max))
10884           (insert ".\n")
10885           (goto-char (point-min))
10886           (insert "211 ")
10887           (princ (cond
10888                   ((numberp id) id)
10889                   ((cdr where) (cdr where))
10890                   (header (mail-header-number header))
10891                   (t gnus-reffed-article-number))
10892                  (current-buffer))
10893           (insert " Article retrieved.\n"))
10894         (if (or (not where)
10895                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10896             ()                          ; Malformed head.
10897           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10898             (when (and (stringp id)
10899                        (not (string= (gnus-group-real-name group)
10900                                      (car where))))
10901               ;; If we fetched by Message-ID and the article came
10902               ;; from a different group, we fudge some bogus article
10903               ;; numbers for this article.
10904               (mail-header-set-number header gnus-reffed-article-number))
10905             (save-excursion
10906               (set-buffer gnus-summary-buffer)
10907               (decf gnus-reffed-article-number)
10908               (gnus-remove-header (mail-header-number header))
10909               (push header gnus-newsgroup-headers)
10910               (setq gnus-current-headers header)
10911               (push (mail-header-number header) gnus-newsgroup-limit)))
10912           header)))))
10913
10914 (defun gnus-remove-header (number)
10915   "Remove header NUMBER from `gnus-newsgroup-headers'."
10916   (if (and gnus-newsgroup-headers
10917            (= number (mail-header-number (car gnus-newsgroup-headers))))
10918       (pop gnus-newsgroup-headers)
10919     (let ((headers gnus-newsgroup-headers))
10920       (while (and (cdr headers)
10921                   (not (= number (mail-header-number (cadr headers)))))
10922         (pop headers))
10923       (when (cdr headers)
10924         (setcdr headers (cddr headers))))))
10925
10926 ;;;
10927 ;;; summary highlights
10928 ;;;
10929
10930 (defun gnus-highlight-selected-summary ()
10931   "Highlight selected article in summary buffer."
10932   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10933   (when gnus-summary-selected-face
10934     (save-excursion
10935       (let* ((beg (progn (beginning-of-line) (point)))
10936              (end (progn (end-of-line) (point)))
10937              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10938              (from (if (get-text-property beg gnus-mouse-face-prop)
10939                        beg
10940                      (or (next-single-property-change
10941                           beg gnus-mouse-face-prop nil end)
10942                          beg)))
10943              (to
10944               (if (= from end)
10945                   (- from 2)
10946                 (or (next-single-property-change
10947                      from gnus-mouse-face-prop nil end)
10948                     end))))
10949         ;; If no mouse-face prop on line we will have to = from = end,
10950         ;; so we highlight the entire line instead.
10951         (when (= (+ to 2) from)
10952           (setq from beg)
10953           (setq to end))
10954         (if gnus-newsgroup-selected-overlay
10955             ;; Move old overlay.
10956             (gnus-move-overlay
10957              gnus-newsgroup-selected-overlay from to (current-buffer))
10958           ;; Create new overlay.
10959           (gnus-overlay-put
10960            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10961            'face gnus-summary-selected-face))))))
10962
10963 (defvar gnus-summary-highlight-line-cached nil)
10964 (defvar gnus-summary-highlight-line-trigger nil)
10965
10966 (defun gnus-summary-highlight-line-0 ()
10967   (if (and (eq gnus-summary-highlight-line-trigger 
10968                gnus-summary-highlight)
10969            gnus-summary-highlight-line-cached)
10970       gnus-summary-highlight-line-cached
10971     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10972           gnus-summary-highlight-line-cached
10973           (let* ((cond (list 'cond))
10974                  (c cond)
10975                  (list gnus-summary-highlight))
10976             (while list
10977               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10978               (setq c (cdr c)
10979                     list (cdr list)))
10980             (gnus-byte-compile (list 'lambda nil cond))))))
10981
10982 (defun gnus-summary-highlight-line ()
10983   "Highlight current line according to `gnus-summary-highlight'."
10984   (let* ((beg (gnus-point-at-bol))
10985          (article (or (gnus-summary-article-number) gnus-current-article))
10986          (score (or (cdr (assq article
10987                                gnus-newsgroup-scored))
10988                     gnus-summary-default-score 0))
10989          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10990          (inhibit-read-only t)
10991          (default gnus-summary-default-score)
10992          (default-high gnus-summary-default-high-score)
10993          (default-low gnus-summary-default-low-score)
10994          (uncached (memq article gnus-newsgroup-undownloaded))
10995          (downloaded (not uncached)))
10996     (let ((face (funcall (gnus-summary-highlight-line-0))))
10997       (unless (eq face (get-text-property beg 'face))
10998         (gnus-put-text-property-excluding-characters-with-faces
10999          beg (gnus-point-at-eol) 'face
11000          (setq face (if (boundp face) (symbol-value face) face)))
11001         (when gnus-summary-highlight-line-function
11002           (funcall gnus-summary-highlight-line-function article face))))))
11003
11004 (defun gnus-update-read-articles (group unread &optional compute)
11005   "Update the list of read articles in GROUP.
11006 UNREAD is a sorted list."
11007   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11008          (entry (gnus-gethash group gnus-newsrc-hashtb))
11009          (info (nth 2 entry))
11010          (prev 1)
11011          read)
11012     (if (or (not info) (not active))
11013         ;; There is no info on this group if it was, in fact,
11014         ;; killed.  Gnus stores no information on killed groups, so
11015         ;; there's nothing to be done.
11016         ;; One could store the information somewhere temporarily,
11017         ;; perhaps...  Hmmm...
11018         ()
11019       ;; Remove any negative articles numbers.
11020       (while (and unread (< (car unread) 0))
11021         (setq unread (cdr unread)))
11022       ;; Remove any expired article numbers
11023       (while (and unread (< (car unread) (car active)))
11024         (setq unread (cdr unread)))
11025       ;; Compute the ranges of read articles by looking at the list of
11026       ;; unread articles.
11027       (while unread
11028         (when (/= (car unread) prev)
11029           (push (if (= prev (1- (car unread))) prev
11030                   (cons prev (1- (car unread))))
11031                 read))
11032         (setq prev (1+ (car unread)))
11033         (setq unread (cdr unread)))
11034       (when (<= prev (cdr active))
11035         (push (cons prev (cdr active)) read))
11036       (setq read (if (> (length read) 1) (nreverse read) read))
11037       (if compute
11038           read
11039         (save-excursion
11040           (let (setmarkundo)
11041             ;; Propagate the read marks to the backend.
11042             (when (gnus-check-backend-function 'request-set-mark group)
11043               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11044                     (add (gnus-remove-from-range read (gnus-info-read info))))
11045                 (when (or add del)
11046                   (unless (gnus-check-group group)
11047                     (error "Can't open server for %s" group))
11048                   (gnus-request-set-mark
11049                    group (delq nil (list (if add (list add 'add '(read)))
11050                                          (if del (list del 'del '(read))))))
11051                   (setq setmarkundo
11052                         `(gnus-request-set-mark
11053                           ,group
11054                           ',(delq nil (list
11055                                        (if del (list del 'add '(read)))
11056                                        (if add (list add 'del '(read))))))))))
11057             (set-buffer gnus-group-buffer)
11058             (gnus-undo-register
11059               `(progn
11060                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11061                  (gnus-info-set-read ',info ',(gnus-info-read info))
11062                  (gnus-get-unread-articles-in-group ',info
11063                                                     (gnus-active ,group))
11064                  (gnus-group-update-group ,group t)
11065                  ,setmarkundo))))
11066         ;; Enter this list into the group info.
11067         (gnus-info-set-read info read)
11068         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11069         (gnus-get-unread-articles-in-group info (gnus-active group))
11070         t))))
11071
11072 (defun gnus-offer-save-summaries ()
11073   "Offer to save all active summary buffers."
11074   (let (buffers)
11075     ;; Go through all buffers and find all summaries.
11076     (dolist (buffer (buffer-list))
11077       (when (and (setq buffer (buffer-name buffer))
11078                  (string-match "Summary" buffer)
11079                  (save-excursion
11080                    (set-buffer buffer)
11081                    ;; We check that this is, indeed, a summary buffer.
11082                    (and (eq major-mode 'gnus-summary-mode)
11083                         ;; Also make sure this isn't bogus.
11084                         gnus-newsgroup-prepared
11085                         ;; Also make sure that this isn't a
11086                         ;; dead summary buffer.
11087                         (not gnus-dead-summary-mode))))
11088         (push buffer buffers)))
11089     ;; Go through all these summary buffers and offer to save them.
11090     (when buffers
11091       (save-excursion
11092         (map-y-or-n-p
11093          "Update summary buffer %s? "
11094          (lambda (buf)
11095            (switch-to-buffer buf)
11096            (gnus-summary-exit))
11097          buffers)))))
11098
11099 (defun gnus-summary-setup-default-charset ()
11100   "Setup newsgroup default charset."
11101   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11102       (setq gnus-newsgroup-charset nil)
11103     (let* ((ignored-charsets
11104             (or gnus-newsgroup-ephemeral-ignored-charsets
11105                 (append
11106                  (and gnus-newsgroup-name
11107                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11108                  gnus-newsgroup-ignored-charsets))))
11109       (setq gnus-newsgroup-charset
11110             (or gnus-newsgroup-ephemeral-charset
11111                 (and gnus-newsgroup-name
11112                      (gnus-parameter-charset gnus-newsgroup-name))
11113                 gnus-default-charset))
11114       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11115            ignored-charsets))))
11116
11117 ;;;
11118 ;;; Mime Commands
11119 ;;;
11120
11121 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11122   "Display the current article buffer fully MIME-buttonized.
11123 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11124 treated as multipart/mixed."
11125   (interactive "P")
11126   (require 'gnus-art)
11127   (let ((gnus-unbuttonized-mime-types nil)
11128         (gnus-mime-display-multipart-as-mixed show-all-parts))
11129     (gnus-summary-show-article)))
11130
11131 (defun gnus-summary-repair-multipart (article)
11132   "Add a Content-Type header to a multipart article without one."
11133   (interactive (list (gnus-summary-article-number)))
11134   (gnus-with-article article
11135     (message-narrow-to-head)
11136     (message-remove-header "Mime-Version")
11137     (goto-char (point-max))
11138     (insert "Mime-Version: 1.0\n")
11139     (widen)
11140     (when (search-forward "\n--" nil t)
11141       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11142         (message-narrow-to-head)
11143         (message-remove-header "Content-Type")
11144         (goto-char (point-max))
11145         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11146                         separator))
11147         (widen))))
11148   (let (gnus-mark-article-hook)
11149     (gnus-summary-select-article t t nil article)))
11150
11151 (defun gnus-summary-toggle-display-buttonized ()
11152   "Toggle the buttonizing of the article buffer."
11153   (interactive)
11154   (require 'gnus-art)
11155   (if (setq gnus-inhibit-mime-unbuttonizing
11156             (not gnus-inhibit-mime-unbuttonizing))
11157       (let ((gnus-unbuttonized-mime-types nil))
11158         (gnus-summary-show-article))
11159     (gnus-summary-show-article)))
11160
11161 ;;;
11162 ;;; Generic summary marking commands
11163 ;;;
11164
11165 (defvar gnus-summary-marking-alist
11166   '((read gnus-del-mark "d")
11167     (unread gnus-unread-mark "u")
11168     (ticked gnus-ticked-mark "!")
11169     (dormant gnus-dormant-mark "?")
11170     (expirable gnus-expirable-mark "e"))
11171   "An alist of names/marks/keystrokes.")
11172
11173 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11174 (defvar gnus-summary-mark-map)
11175
11176 (defun gnus-summary-make-all-marking-commands ()
11177   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11178   (dolist (elem gnus-summary-marking-alist)
11179     (apply 'gnus-summary-make-marking-command elem)))
11180
11181 (defun gnus-summary-make-marking-command (name mark keystroke)
11182   (let ((map (make-sparse-keymap)))
11183     (define-key gnus-summary-generic-mark-map keystroke map)
11184     (dolist (lway `((next "next" next nil "n")
11185                     (next-unread "next unread" next t "N")
11186                     (prev "previous" prev nil "p")
11187                     (prev-unread "previous unread" prev t "P")
11188                     (nomove "" nil nil ,keystroke)))
11189       (let ((func (gnus-summary-make-marking-command-1
11190                    mark (car lway) lway name)))
11191         (setq func (eval func))
11192         (define-key map (nth 4 lway) func)))))
11193
11194 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11195   `(defun ,(intern
11196             (format "gnus-summary-put-mark-as-%s%s"
11197                     name (if (eq way 'nomove)
11198                              ""
11199                            (concat "-" (symbol-name way)))))
11200      (n)
11201      ,(format
11202        "Mark the current article as %s%s.
11203 If N, the prefix, then repeat N times.
11204 If N is negative, move in reverse order.
11205 The difference between N and the actual number of articles marked is
11206 returned."
11207        name (cadr lway))
11208      (interactive "p")
11209      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11210
11211 (defun gnus-summary-generic-mark (n mark move unread)
11212   "Mark N articles with MARK."
11213   (unless (eq major-mode 'gnus-summary-mode)
11214     (error "This command can only be used in the summary buffer"))
11215   (gnus-summary-show-thread)
11216   (let ((nummove
11217          (cond
11218           ((eq move 'next) 1)
11219           ((eq move 'prev) -1)
11220           (t 0))))
11221     (if (zerop nummove)
11222         (setq n 1)
11223       (when (< n 0)
11224         (setq n (abs n)
11225               nummove (* -1 nummove))))
11226     (while (and (> n 0)
11227                 (gnus-summary-mark-article nil mark)
11228                 (zerop (gnus-summary-next-subject nummove unread t)))
11229       (setq n (1- n)))
11230     (when (/= 0 n)
11231       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11232     (gnus-summary-recenter)
11233     (gnus-summary-position-point)
11234     (gnus-set-mode-line 'summary)
11235     n))
11236
11237 (defun gnus-summary-insert-articles (articles)
11238   (when (setq articles
11239               (gnus-sorted-difference articles
11240                                       (mapcar (lambda (h)
11241                                                 (mail-header-number h))
11242                                               gnus-newsgroup-headers)))
11243     (setq gnus-newsgroup-headers
11244           (merge 'list
11245                  gnus-newsgroup-headers
11246                  (gnus-fetch-headers articles)
11247                  'gnus-article-sort-by-number))
11248     ;; Suppress duplicates?
11249     (when gnus-suppress-duplicates
11250       (gnus-dup-suppress-articles))
11251
11252     ;; We might want to build some more threads first.
11253     (when (and gnus-fetch-old-headers
11254                (eq gnus-headers-retrieved-by 'nov))
11255       (if (eq gnus-fetch-old-headers 'invisible)
11256           (gnus-build-all-threads)
11257         (gnus-build-old-threads)))
11258     ;; Let the Gnus agent mark articles as read.
11259     (when gnus-agent
11260       (gnus-agent-get-undownloaded-list))
11261     ;; Remove list identifiers from subject
11262     (when gnus-list-identifiers
11263       (gnus-summary-remove-list-identifiers))
11264     ;; First and last article in this newsgroup.
11265     (when gnus-newsgroup-headers
11266       (setq gnus-newsgroup-begin
11267             (mail-header-number (car gnus-newsgroup-headers))
11268             gnus-newsgroup-end
11269             (mail-header-number
11270              (gnus-last-element gnus-newsgroup-headers))))
11271     (when gnus-use-scoring
11272       (gnus-possibly-score-headers))))
11273
11274 (defun gnus-summary-insert-old-articles (&optional all)
11275   "Insert all old articles in this group.
11276 If ALL is non-nil, already read articles become readable.
11277 If ALL is a number, fetch this number of articles."
11278   (interactive "P")
11279   (prog1
11280       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11281             older len)
11282         (setq older
11283               ;; Some nntp servers lie about their active range.  When
11284               ;; this happens, the active range can be in the millions.
11285               ;; Use a compressed range to avoid creating a huge list.
11286               (gnus-range-difference (list gnus-newsgroup-active) old))
11287         (setq len (gnus-range-length older))
11288         (cond
11289          ((null older) nil)
11290          ((numberp all)
11291           (if (< all len)
11292               (let ((older-range (nreverse older)))
11293                 (setq older nil)
11294
11295                 (while (> all 0)
11296                   (let* ((r (pop older-range))
11297                          (min (if (numberp r) r (car r)))
11298                          (max (if (numberp r) r (cdr r))))
11299                     (while (and (<= min max)
11300                                 (> all 0))
11301                       (push max older)
11302                       (setq all (1- all)
11303                             max (1- max))))))
11304             (setq older (gnus-uncompress-range older))))
11305          (all
11306           (setq older (gnus-uncompress-range older)))
11307          (t
11308           (when (and (numberp gnus-large-newsgroup)
11309                    (> len gnus-large-newsgroup))
11310               (let* ((cursor-in-echo-area nil)
11311                      (initial (gnus-parameter-large-newsgroup-initial
11312                                gnus-newsgroup-name))
11313                      (input
11314                       (read-string
11315                        (format
11316                         "How many articles from %s (%s %d): "
11317                         (gnus-limit-string
11318                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11319                         (if initial "max" "default")
11320                         len)
11321                        (if initial
11322                            (cons (number-to-string initial)
11323                                  0)))))
11324                 (unless (string-match "^[ \t]*$" input)
11325                   (setq all (string-to-number input))
11326                   (if (< all len)
11327                       (let ((older-range (nreverse older)))
11328                         (setq older nil)
11329
11330                         (while (> all 0)
11331                           (let* ((r (pop older-range))
11332                                  (min (if (numberp r) r (car r)))
11333                                  (max (if (numberp r) r (cdr r))))
11334                             (while (and (<= min max)
11335                                         (> all 0))
11336                               (push max older)
11337                               (setq all (1- all)
11338                                     max (1- max))))))))))
11339           (setq older (gnus-uncompress-range older))))
11340         (if (not older)
11341             (message "No old news.")
11342           (gnus-summary-insert-articles older)
11343           (gnus-summary-limit (gnus-sorted-nunion old older))))
11344     (gnus-summary-position-point)))
11345
11346 (defun gnus-summary-insert-new-articles ()
11347   "Insert all new articles in this group."
11348   (interactive)
11349   (prog1
11350       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11351             (old-active gnus-newsgroup-active)
11352             (nnmail-fetched-sources (list t))
11353             i new)
11354         (setq gnus-newsgroup-active
11355               (gnus-activate-group gnus-newsgroup-name 'scan))
11356         (setq i (cdr gnus-newsgroup-active))
11357         (while (> i (cdr old-active))
11358           (push i new)
11359           (decf i))
11360         (if (not new)
11361             (message "No gnus is bad news.")
11362           (gnus-summary-insert-articles new)
11363           (setq gnus-newsgroup-unreads
11364                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11365           (gnus-summary-limit (gnus-sorted-nunion old new))))
11366     (gnus-summary-position-point)))
11367
11368 (gnus-summary-make-all-marking-commands)
11369
11370 (gnus-ems-redefine)
11371
11372 (provide 'gnus-sum)
11373
11374 (run-hooks 'gnus-sum-load-hook)
11375
11376 ;;; gnus-sum.el ends here