(gnus-gcc-externalize-attachments): Fix typo.
[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
1329 ;; MIME stuff.
1330
1331 (defvar gnus-decode-encoded-word-methods
1332   '(mail-decode-encoded-word-string)
1333   "List of methods used to decode encoded words.
1334
1335 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1336 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1337 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1338 whose names match REGEXP.
1339
1340 For example:
1341 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1342  mail-decode-encoded-word-string
1343  (\"chinese\" . rfc1843-decode-string))")
1344
1345 (defvar gnus-decode-encoded-word-methods-cache nil)
1346
1347 (defun gnus-multi-decode-encoded-word-string (string)
1348   "Apply the functions from `gnus-encoded-word-methods' that match."
1349   (unless (and gnus-decode-encoded-word-methods-cache
1350                (eq gnus-newsgroup-name
1351                    (car gnus-decode-encoded-word-methods-cache)))
1352     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1353     (mapcar (lambda (x)
1354               (if (symbolp x)
1355                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1356                 (if (and gnus-newsgroup-name
1357                          (string-match (car x) gnus-newsgroup-name))
1358                     (nconc gnus-decode-encoded-word-methods-cache
1359                            (list (cdr x))))))
1360             gnus-decode-encoded-word-methods))
1361   (let ((xlist gnus-decode-encoded-word-methods-cache))
1362     (pop xlist)
1363     (while xlist
1364       (setq string (funcall (pop xlist) string))))
1365   string)
1366
1367 ;; Subject simplification.
1368
1369 (defun gnus-simplify-whitespace (str)
1370   "Remove excessive whitespace from STR."
1371   (let ((mystr str))
1372     ;; Multiple spaces.
1373     (while (string-match "[ \t][ \t]+" mystr)
1374       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1375                           " "
1376                           (substring mystr (match-end 0)))))
1377     ;; Leading spaces.
1378     (when (string-match "^[ \t]+" mystr)
1379       (setq mystr (substring mystr (match-end 0))))
1380     ;; Trailing spaces.
1381     (when (string-match "[ \t]+$" mystr)
1382       (setq mystr (substring mystr 0 (match-beginning 0))))
1383     mystr))
1384
1385 (defun gnus-simplify-all-whitespace (str)
1386   "Remove all whitespace from STR."
1387   (let ((mystr str))
1388     (while (string-match "[ \t\n]+" mystr)
1389       (setq mystr (replace-match "" nil nil mystr)))
1390     mystr))
1391
1392 (defsubst gnus-simplify-subject-re (subject)
1393   "Remove \"Re:\" from subject lines."
1394   (if (string-match message-subject-re-regexp subject)
1395       (substring subject (match-end 0))
1396     subject))
1397
1398 (defun gnus-simplify-subject (subject &optional re-only)
1399   "Remove `Re:' and words in parentheses.
1400 If RE-ONLY is non-nil, strip leading `Re:'s only."
1401   (let ((case-fold-search t))           ;Ignore case.
1402     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1403     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1404       (setq subject (substring subject (match-end 0))))
1405     ;; Remove uninteresting prefixes.
1406     (when (and (not re-only)
1407                gnus-simplify-ignored-prefixes
1408                (string-match gnus-simplify-ignored-prefixes subject))
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove words in parentheses from end.
1411     (unless re-only
1412       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1413         (setq subject (substring subject 0 (match-beginning 0)))))
1414     ;; Return subject string.
1415     subject))
1416
1417 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1418 ;; all whitespace.
1419 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1420   (goto-char (point-min))
1421   (while (re-search-forward regexp nil t)
1422     (replace-match (or newtext ""))))
1423
1424 (defun gnus-simplify-buffer-fuzzy ()
1425   "Simplify string in the buffer fuzzily.
1426 The string in the accessible portion of the current buffer is simplified.
1427 It is assumed to be a single-line subject.
1428 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1429 matter is removed.  Additional things can be deleted by setting
1430 `gnus-simplify-subject-fuzzy-regexp'."
1431   (let ((case-fold-search t)
1432         (modified-tick))
1433     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1434
1435     (while (not (eq modified-tick (buffer-modified-tick)))
1436       (setq modified-tick (buffer-modified-tick))
1437       (cond
1438        ((listp gnus-simplify-subject-fuzzy-regexp)
1439         (mapcar 'gnus-simplify-buffer-fuzzy-step
1440                 gnus-simplify-subject-fuzzy-regexp))
1441        (gnus-simplify-subject-fuzzy-regexp
1442         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1443       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1444       (gnus-simplify-buffer-fuzzy-step
1445        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1446       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1447
1448     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1449     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1450     (gnus-simplify-buffer-fuzzy-step " $")
1451     (gnus-simplify-buffer-fuzzy-step "^ +")))
1452
1453 (defun gnus-simplify-subject-fuzzy (subject)
1454   "Simplify a subject string fuzzily.
1455 See `gnus-simplify-buffer-fuzzy' for details."
1456   (save-excursion
1457     (gnus-set-work-buffer)
1458     (let ((case-fold-search t))
1459       ;; Remove uninteresting prefixes.
1460       (when (and gnus-simplify-ignored-prefixes
1461                  (string-match gnus-simplify-ignored-prefixes subject))
1462         (setq subject (substring subject (match-end 0))))
1463       (insert subject)
1464       (inline (gnus-simplify-buffer-fuzzy))
1465       (buffer-string))))
1466
1467 (defsubst gnus-simplify-subject-fully (subject)
1468   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1469   (cond
1470    (gnus-simplify-subject-functions
1471     (gnus-map-function gnus-simplify-subject-functions subject))
1472    ((null gnus-summary-gather-subject-limit)
1473     (gnus-simplify-subject-re subject))
1474    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1475     (gnus-simplify-subject-fuzzy subject))
1476    ((numberp gnus-summary-gather-subject-limit)
1477     (gnus-limit-string (gnus-simplify-subject-re subject)
1478                        gnus-summary-gather-subject-limit))
1479    (t
1480     subject)))
1481
1482 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1483   "Check whether two subjects are equal.
1484 If optional argument simple-first is t, first argument is already
1485 simplified."
1486   (cond
1487    ((null simple-first)
1488     (equal (gnus-simplify-subject-fully s1)
1489            (gnus-simplify-subject-fully s2)))
1490    (t
1491     (equal s1
1492            (gnus-simplify-subject-fully s2)))))
1493
1494 (defun gnus-summary-bubble-group ()
1495   "Increase the score of the current group.
1496 This is a handy function to add to `gnus-summary-exit-hook' to
1497 increase the score of each group you read."
1498   (gnus-group-add-score gnus-newsgroup-name))
1499
1500 \f
1501 ;;;
1502 ;;; Gnus summary mode
1503 ;;;
1504
1505 (put 'gnus-summary-mode 'mode-class 'special)
1506
1507 (defvar gnus-article-commands-menu)
1508
1509 (when t
1510   ;; Non-orthogonal keys
1511
1512   (gnus-define-keys gnus-summary-mode-map
1513     " " gnus-summary-next-page
1514     "\177" gnus-summary-prev-page
1515     [delete] gnus-summary-prev-page
1516     [backspace] gnus-summary-prev-page
1517     "\r" gnus-summary-scroll-up
1518     "\M-\r" gnus-summary-scroll-down
1519     "n" gnus-summary-next-unread-article
1520     "p" gnus-summary-prev-unread-article
1521     "N" gnus-summary-next-article
1522     "P" gnus-summary-prev-article
1523     "\M-\C-n" gnus-summary-next-same-subject
1524     "\M-\C-p" gnus-summary-prev-same-subject
1525     "\M-n" gnus-summary-next-unread-subject
1526     "\M-p" gnus-summary-prev-unread-subject
1527     "." gnus-summary-first-unread-article
1528     "," gnus-summary-best-unread-article
1529     "\M-s" gnus-summary-search-article-forward
1530     "\M-r" gnus-summary-search-article-backward
1531     "<" gnus-summary-beginning-of-article
1532     ">" gnus-summary-end-of-article
1533     "j" gnus-summary-goto-article
1534     "^" gnus-summary-refer-parent-article
1535     "\M-^" gnus-summary-refer-article
1536     "u" gnus-summary-tick-article-forward
1537     "!" gnus-summary-tick-article-forward
1538     "U" gnus-summary-tick-article-backward
1539     "d" gnus-summary-mark-as-read-forward
1540     "D" gnus-summary-mark-as-read-backward
1541     "E" gnus-summary-mark-as-expirable
1542     "\M-u" gnus-summary-clear-mark-forward
1543     "\M-U" gnus-summary-clear-mark-backward
1544     "k" gnus-summary-kill-same-subject-and-select
1545     "\C-k" gnus-summary-kill-same-subject
1546     "\M-\C-k" gnus-summary-kill-thread
1547     "\M-\C-l" gnus-summary-lower-thread
1548     "e" gnus-summary-edit-article
1549     "#" gnus-summary-mark-as-processable
1550     "\M-#" gnus-summary-unmark-as-processable
1551     "\M-\C-t" gnus-summary-toggle-threads
1552     "\M-\C-s" gnus-summary-show-thread
1553     "\M-\C-h" gnus-summary-hide-thread
1554     "\M-\C-f" gnus-summary-next-thread
1555     "\M-\C-b" gnus-summary-prev-thread
1556     [(meta down)] gnus-summary-next-thread
1557     [(meta up)] gnus-summary-prev-thread
1558     "\M-\C-u" gnus-summary-up-thread
1559     "\M-\C-d" gnus-summary-down-thread
1560     "&" gnus-summary-execute-command
1561     "c" gnus-summary-catchup-and-exit
1562     "\C-w" gnus-summary-mark-region-as-read
1563     "\C-t" gnus-summary-toggle-truncation
1564     "?" gnus-summary-mark-as-dormant
1565     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1566     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1567     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1568     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1569     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1570     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1571     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1572     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1573     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1574     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1575     "=" gnus-summary-expand-window
1576     "\C-x\C-s" gnus-summary-reselect-current-group
1577     "\M-g" gnus-summary-rescan-group
1578     "w" gnus-summary-stop-page-breaking
1579     "\C-c\C-r" gnus-summary-caesar-message
1580     "f" gnus-summary-followup
1581     "F" gnus-summary-followup-with-original
1582     "C" gnus-summary-cancel-article
1583     "r" gnus-summary-reply
1584     "R" gnus-summary-reply-with-original
1585     "\C-c\C-f" gnus-summary-mail-forward
1586     "o" gnus-summary-save-article
1587     "\C-o" gnus-summary-save-article-mail
1588     "|" gnus-summary-pipe-output
1589     "\M-k" gnus-summary-edit-local-kill
1590     "\M-K" gnus-summary-edit-global-kill
1591     ;; "V" gnus-version
1592     "\C-c\C-d" gnus-summary-describe-group
1593     "q" gnus-summary-exit
1594     "Q" gnus-summary-exit-no-update
1595     "\C-c\C-i" gnus-info-find-node
1596     gnus-mouse-2 gnus-mouse-pick-article
1597     "m" gnus-summary-mail-other-window
1598     "a" gnus-summary-post-news
1599     "i" gnus-summary-news-other-window
1600     "x" gnus-summary-limit-to-unread
1601     "s" gnus-summary-isearch-article
1602     "t" gnus-summary-toggle-header
1603     "g" gnus-summary-show-article
1604     "l" gnus-summary-goto-last-article
1605     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1606     "\C-d" gnus-summary-enter-digest-group
1607     "\M-\C-d" gnus-summary-read-document
1608     "\M-\C-e" gnus-summary-edit-parameters
1609     "\M-\C-a" gnus-summary-customize-parameters
1610     "\C-c\C-b" gnus-bug
1611     "*" gnus-cache-enter-article
1612     "\M-*" gnus-cache-remove-article
1613     "\M-&" gnus-summary-universal-argument
1614     "\C-l" gnus-recenter
1615     "I" gnus-summary-increase-score
1616     "L" gnus-summary-lower-score
1617     "\M-i" gnus-symbolic-argument
1618     "h" gnus-summary-select-article-buffer
1619
1620     "b" gnus-article-view-part
1621     "\M-t" gnus-summary-toggle-display-buttonized
1622
1623     "V" gnus-summary-score-map
1624     "X" gnus-uu-extract-map
1625     "S" gnus-summary-send-map)
1626
1627   ;; Sort of orthogonal keymap
1628   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1629     "t" gnus-summary-tick-article-forward
1630     "!" gnus-summary-tick-article-forward
1631     "d" gnus-summary-mark-as-read-forward
1632     "r" gnus-summary-mark-as-read-forward
1633     "c" gnus-summary-clear-mark-forward
1634     " " gnus-summary-clear-mark-forward
1635     "e" gnus-summary-mark-as-expirable
1636     "x" gnus-summary-mark-as-expirable
1637     "?" gnus-summary-mark-as-dormant
1638     "b" gnus-summary-set-bookmark
1639     "B" gnus-summary-remove-bookmark
1640     "#" gnus-summary-mark-as-processable
1641     "\M-#" gnus-summary-unmark-as-processable
1642     "S" gnus-summary-limit-include-expunged
1643     "C" gnus-summary-catchup
1644     "H" gnus-summary-catchup-to-here
1645     "h" gnus-summary-catchup-from-here
1646     "\C-c" gnus-summary-catchup-all
1647     "k" gnus-summary-kill-same-subject-and-select
1648     "K" gnus-summary-kill-same-subject
1649     "P" gnus-uu-mark-map)
1650
1651   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1652     "c" gnus-summary-clear-above
1653     "u" gnus-summary-tick-above
1654     "m" gnus-summary-mark-above
1655     "k" gnus-summary-kill-below)
1656
1657   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1658     "/" gnus-summary-limit-to-subject
1659     "n" gnus-summary-limit-to-articles
1660     "w" gnus-summary-pop-limit
1661     "s" gnus-summary-limit-to-subject
1662     "a" gnus-summary-limit-to-author
1663     "u" gnus-summary-limit-to-unread
1664     "m" gnus-summary-limit-to-marks
1665     "M" gnus-summary-limit-exclude-marks
1666     "v" gnus-summary-limit-to-score
1667     "*" gnus-summary-limit-include-cached
1668     "D" gnus-summary-limit-include-dormant
1669     "T" gnus-summary-limit-include-thread
1670     "d" gnus-summary-limit-exclude-dormant
1671     "t" gnus-summary-limit-to-age
1672     "." gnus-summary-limit-to-unseen
1673     "x" gnus-summary-limit-to-extra
1674     "p" gnus-summary-limit-to-display-predicate
1675     "E" gnus-summary-limit-include-expunged
1676     "c" gnus-summary-limit-exclude-childless-dormant
1677     "C" gnus-summary-limit-mark-excluded-as-read
1678     "o" gnus-summary-insert-old-articles
1679     "N" gnus-summary-insert-new-articles)
1680
1681   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1682     "n" gnus-summary-next-unread-article
1683     "p" gnus-summary-prev-unread-article
1684     "N" gnus-summary-next-article
1685     "P" gnus-summary-prev-article
1686     "\C-n" gnus-summary-next-same-subject
1687     "\C-p" gnus-summary-prev-same-subject
1688     "\M-n" gnus-summary-next-unread-subject
1689     "\M-p" gnus-summary-prev-unread-subject
1690     "f" gnus-summary-first-unread-article
1691     "b" gnus-summary-best-unread-article
1692     "j" gnus-summary-goto-article
1693     "g" gnus-summary-goto-subject
1694     "l" gnus-summary-goto-last-article
1695     "o" gnus-summary-pop-article)
1696
1697   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1698     "k" gnus-summary-kill-thread
1699     "l" gnus-summary-lower-thread
1700     "i" gnus-summary-raise-thread
1701     "T" gnus-summary-toggle-threads
1702     "t" gnus-summary-rethread-current
1703     "^" gnus-summary-reparent-thread
1704     "s" gnus-summary-show-thread
1705     "S" gnus-summary-show-all-threads
1706     "h" gnus-summary-hide-thread
1707     "H" gnus-summary-hide-all-threads
1708     "n" gnus-summary-next-thread
1709     "p" gnus-summary-prev-thread
1710     "u" gnus-summary-up-thread
1711     "o" gnus-summary-top-thread
1712     "d" gnus-summary-down-thread
1713     "#" gnus-uu-mark-thread
1714     "\M-#" gnus-uu-unmark-thread)
1715
1716   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1717     "g" gnus-summary-prepare
1718     "c" gnus-summary-insert-cached-articles
1719     "d" gnus-summary-insert-dormant-articles)
1720
1721   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1722     "c" gnus-summary-catchup-and-exit
1723     "C" gnus-summary-catchup-all-and-exit
1724     "E" gnus-summary-exit-no-update
1725     "Q" gnus-summary-exit
1726     "Z" gnus-summary-exit
1727     "n" gnus-summary-catchup-and-goto-next-group
1728     "R" gnus-summary-reselect-current-group
1729     "G" gnus-summary-rescan-group
1730     "N" gnus-summary-next-group
1731     "s" gnus-summary-save-newsrc
1732     "P" gnus-summary-prev-group)
1733
1734   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1735     " " gnus-summary-next-page
1736     "n" gnus-summary-next-page
1737     "\177" gnus-summary-prev-page
1738     [delete] gnus-summary-prev-page
1739     "p" gnus-summary-prev-page
1740     "\r" gnus-summary-scroll-up
1741     "\M-\r" gnus-summary-scroll-down
1742     "<" gnus-summary-beginning-of-article
1743     ">" gnus-summary-end-of-article
1744     "b" gnus-summary-beginning-of-article
1745     "e" gnus-summary-end-of-article
1746     "^" gnus-summary-refer-parent-article
1747     "r" gnus-summary-refer-parent-article
1748     "D" gnus-summary-enter-digest-group
1749     "R" gnus-summary-refer-references
1750     "T" gnus-summary-refer-thread
1751     "g" gnus-summary-show-article
1752     "s" gnus-summary-isearch-article
1753     "P" gnus-summary-print-article
1754     "M" gnus-mailing-list-insinuate
1755     "t" gnus-article-babel)
1756
1757   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1758     "b" gnus-article-add-buttons
1759     "B" gnus-article-add-buttons-to-head
1760     "o" gnus-article-treat-overstrike
1761     "e" gnus-article-emphasize
1762     "w" gnus-article-fill-cited-article
1763     "Q" gnus-article-fill-long-lines
1764     "C" gnus-article-capitalize-sentences
1765     "c" gnus-article-remove-cr
1766     "q" gnus-article-de-quoted-unreadable
1767     "6" gnus-article-de-base64-unreadable
1768     "Z" gnus-article-decode-HZ
1769     "h" gnus-article-wash-html
1770     "u" gnus-article-unsplit-urls
1771     "s" gnus-summary-force-verify-and-decrypt
1772     "f" gnus-article-display-x-face
1773     "l" gnus-summary-stop-page-breaking
1774     "r" gnus-summary-caesar-message
1775     "m" gnus-summary-morse-message
1776     "t" gnus-summary-toggle-header
1777     "g" gnus-treat-smiley
1778     "v" gnus-summary-verbose-headers
1779     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1780     "p" gnus-article-verify-x-pgp-sig
1781     "d" gnus-article-treat-dumbquotes
1782     "k" gnus-article-outlook-deuglify-article)
1783
1784   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1785     "a" gnus-article-hide
1786     "h" gnus-article-hide-headers
1787     "b" gnus-article-hide-boring-headers
1788     "s" gnus-article-hide-signature
1789     "c" gnus-article-hide-citation
1790     "C" gnus-article-hide-citation-in-followups
1791     "l" gnus-article-hide-list-identifiers
1792     "p" gnus-article-hide-pgp
1793     "B" gnus-article-strip-banner
1794     "P" gnus-article-hide-pem
1795     "\C-c" gnus-article-hide-citation-maybe)
1796
1797   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1798     "a" gnus-article-highlight
1799     "h" gnus-article-highlight-headers
1800     "c" gnus-article-highlight-citation
1801     "s" gnus-article-highlight-signature)
1802
1803   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1804     "f" gnus-article-treat-fold-headers
1805     "u" gnus-article-treat-unfold-headers
1806     "n" gnus-article-treat-fold-newsgroups)
1807
1808   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1809     "x" gnus-article-display-x-face
1810     "s" gnus-treat-smiley
1811     "D" gnus-article-remove-images
1812     "f" gnus-treat-from-picon
1813     "m" gnus-treat-mail-picon
1814     "n" gnus-treat-newsgroups-picon)
1815
1816   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1817     "w" gnus-article-decode-mime-words
1818     "c" gnus-article-decode-charset
1819     "v" gnus-mime-view-all-parts
1820     "b" gnus-article-view-part)
1821
1822   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1823     "z" gnus-article-date-ut
1824     "u" gnus-article-date-ut
1825     "l" gnus-article-date-local
1826     "p" gnus-article-date-english
1827     "e" gnus-article-date-lapsed
1828     "o" gnus-article-date-original
1829     "i" gnus-article-date-iso8601
1830     "s" gnus-article-date-user)
1831
1832   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1833     "t" gnus-article-remove-trailing-blank-lines
1834     "l" gnus-article-strip-leading-blank-lines
1835     "m" gnus-article-strip-multiple-blank-lines
1836     "a" gnus-article-strip-blank-lines
1837     "A" gnus-article-strip-all-blank-lines
1838     "s" gnus-article-strip-leading-space
1839     "e" gnus-article-strip-trailing-space
1840     "w" gnus-article-remove-leading-whitespace)
1841
1842   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1843     "v" gnus-version
1844     "f" gnus-summary-fetch-faq
1845     "d" gnus-summary-describe-group
1846     "h" gnus-summary-describe-briefly
1847     "i" gnus-info-find-node
1848     "c" gnus-group-fetch-charter
1849     "C" gnus-group-fetch-control)
1850
1851   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1852     "e" gnus-summary-expire-articles
1853     "\M-\C-e" gnus-summary-expire-articles-now
1854     "\177" gnus-summary-delete-article
1855     [delete] gnus-summary-delete-article
1856     [backspace] gnus-summary-delete-article
1857     "m" gnus-summary-move-article
1858     "r" gnus-summary-respool-article
1859     "w" gnus-summary-edit-article
1860     "c" gnus-summary-copy-article
1861     "B" gnus-summary-crosspost-article
1862     "q" gnus-summary-respool-query
1863     "t" gnus-summary-respool-trace
1864     "i" gnus-summary-import-article
1865     "I" gnus-summary-create-article
1866     "p" gnus-summary-article-posted-p)
1867
1868   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1869     "o" gnus-summary-save-article
1870     "m" gnus-summary-save-article-mail
1871     "F" gnus-summary-write-article-file
1872     "r" gnus-summary-save-article-rmail
1873     "f" gnus-summary-save-article-file
1874     "b" gnus-summary-save-article-body-file
1875     "h" gnus-summary-save-article-folder
1876     "v" gnus-summary-save-article-vm
1877     "p" gnus-summary-pipe-output
1878     "P" gnus-summary-muttprint
1879     "s" gnus-soup-add-article)
1880
1881   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1882     "b" gnus-summary-display-buttonized
1883     "m" gnus-summary-repair-multipart
1884     "v" gnus-article-view-part
1885     "o" gnus-article-save-part
1886     "c" gnus-article-copy-part
1887     "C" gnus-article-view-part-as-charset
1888     "e" gnus-article-view-part-externally
1889     "E" gnus-article-encrypt-body
1890     "i" gnus-article-inline-part
1891     "|" gnus-article-pipe-part)
1892
1893   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1894     "p" gnus-summary-mark-as-processable
1895     "u" gnus-summary-unmark-as-processable
1896     "U" gnus-summary-unmark-all-processable
1897     "v" gnus-uu-mark-over
1898     "s" gnus-uu-mark-series
1899     "r" gnus-uu-mark-region
1900     "g" gnus-uu-unmark-region
1901     "R" gnus-uu-mark-by-regexp
1902     "G" gnus-uu-unmark-by-regexp
1903     "t" gnus-uu-mark-thread
1904     "T" gnus-uu-unmark-thread
1905     "a" gnus-uu-mark-all
1906     "b" gnus-uu-mark-buffer
1907     "S" gnus-uu-mark-sparse
1908     "k" gnus-summary-kill-process-mark
1909     "y" gnus-summary-yank-process-mark
1910     "w" gnus-summary-save-process-mark
1911     "i" gnus-uu-invert-processable)
1912
1913   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1914     ;;"x" gnus-uu-extract-any
1915     "m" gnus-summary-save-parts
1916     "u" gnus-uu-decode-uu
1917     "U" gnus-uu-decode-uu-and-save
1918     "s" gnus-uu-decode-unshar
1919     "S" gnus-uu-decode-unshar-and-save
1920     "o" gnus-uu-decode-save
1921     "O" gnus-uu-decode-save
1922     "b" gnus-uu-decode-binhex
1923     "B" gnus-uu-decode-binhex
1924     "p" gnus-uu-decode-postscript
1925     "P" gnus-uu-decode-postscript-and-save)
1926
1927   (gnus-define-keys
1928       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1929     "u" gnus-uu-decode-uu-view
1930     "U" gnus-uu-decode-uu-and-save-view
1931     "s" gnus-uu-decode-unshar-view
1932     "S" gnus-uu-decode-unshar-and-save-view
1933     "o" gnus-uu-decode-save-view
1934     "O" gnus-uu-decode-save-view
1935     "b" gnus-uu-decode-binhex-view
1936     "B" gnus-uu-decode-binhex-view
1937     "p" gnus-uu-decode-postscript-view
1938     "P" gnus-uu-decode-postscript-and-save-view))
1939
1940 (defvar gnus-article-post-menu nil)
1941
1942 (defconst gnus-summary-menu-maxlen 20)
1943
1944 (defun gnus-summary-menu-split (menu)
1945   ;; If we have lots of elements, divide them into groups of 20
1946   ;; and make a pane (or submenu) for each one.
1947   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1948       (let ((menu menu) sublists next
1949             (i 1))
1950         (while menu
1951           ;; Pull off the next gnus-summary-menu-maxlen elements
1952           ;; and make them the next element of sublist.
1953           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1954           (if next
1955               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1956                       nil))
1957           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1958                                              (aref (car (last menu)) 0)) menu)
1959                                sublists))
1960           (setq i (1+ i))
1961           (setq menu next))
1962         (nreverse sublists))
1963     ;; Few elements--put them all in one pane.
1964     menu))
1965
1966 (defun gnus-summary-make-menu-bar ()
1967   (gnus-turn-off-edit-menu 'summary)
1968
1969   (unless (boundp 'gnus-summary-misc-menu)
1970
1971     (easy-menu-define
1972       gnus-summary-kill-menu gnus-summary-mode-map ""
1973       (cons
1974        "Score"
1975        (nconc
1976         (list
1977          ["Customize" gnus-score-customize t])
1978         (gnus-make-score-map 'increase)
1979         (gnus-make-score-map 'lower)
1980         '(("Mark"
1981            ["Kill below" gnus-summary-kill-below t]
1982            ["Mark above" gnus-summary-mark-above t]
1983            ["Tick above" gnus-summary-tick-above t]
1984            ["Clear above" gnus-summary-clear-above t])
1985           ["Current score" gnus-summary-current-score t]
1986           ["Set score" gnus-summary-set-score t]
1987           ["Switch current score file..." gnus-score-change-score-file t]
1988           ["Set mark below..." gnus-score-set-mark-below t]
1989           ["Set expunge below..." gnus-score-set-expunge-below t]
1990           ["Edit current score file" gnus-score-edit-current-scores t]
1991           ["Edit score file" gnus-score-edit-file t]
1992           ["Trace score" gnus-score-find-trace t]
1993           ["Find words" gnus-score-find-favourite-words t]
1994           ["Rescore buffer" gnus-summary-rescore t]
1995           ["Increase score..." gnus-summary-increase-score t]
1996           ["Lower score..." gnus-summary-lower-score t]))))
1997
1998     ;; Define both the Article menu in the summary buffer and the
1999     ;; equivalent Commands menu in the article buffer here for
2000     ;; consistency.
2001     (let ((innards
2002            `(("Hide"
2003               ["All" gnus-article-hide t]
2004               ["Headers" gnus-article-hide-headers t]
2005               ["Signature" gnus-article-hide-signature t]
2006               ["Citation" gnus-article-hide-citation t]
2007               ["List identifiers" gnus-article-hide-list-identifiers t]
2008               ["PGP" gnus-article-hide-pgp t]
2009               ["Banner" gnus-article-strip-banner t]
2010               ["Boring headers" gnus-article-hide-boring-headers t])
2011              ("Highlight"
2012               ["All" gnus-article-highlight t]
2013               ["Headers" gnus-article-highlight-headers t]
2014               ["Signature" gnus-article-highlight-signature t]
2015               ["Citation" gnus-article-highlight-citation t])
2016              ("MIME"
2017               ["Words" gnus-article-decode-mime-words t]
2018               ["Charset" gnus-article-decode-charset t]
2019               ["QP" gnus-article-de-quoted-unreadable t]
2020               ["Base64" gnus-article-de-base64-unreadable t]
2021               ["View MIME buttons" gnus-summary-display-buttonized t]
2022               ["View all" gnus-mime-view-all-parts t]
2023               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2024               ["Encrypt body" gnus-article-encrypt-body t]
2025               ["Extract all parts" gnus-summary-save-parts t])
2026              ("Date"
2027               ["Local" gnus-article-date-local t]
2028               ["ISO8601" gnus-article-date-iso8601 t]
2029               ["UT" gnus-article-date-ut t]
2030               ["Original" gnus-article-date-original t]
2031               ["Lapsed" gnus-article-date-lapsed t]
2032               ["User-defined" gnus-article-date-user t])
2033              ("Display"
2034               ["Remove images" gnus-article-remove-images t]
2035               ["Toggle smiley" gnus-treat-smiley t]
2036               ["Show X-Face" gnus-article-display-x-face t]
2037               ["Show picons in From" gnus-treat-from-picon t]
2038               ["Show picons in mail headers" gnus-treat-mail-picon t]
2039               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2040               ("View as different encoding"
2041                ,@(gnus-summary-menu-split
2042                   (mapcar
2043                    (lambda (cs)
2044                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2045                      ;; forms for menu commands, we should provide intern'ed
2046                      ;; function symbols.
2047                      (let ((command (intern (format "\
2048 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2049                        (fset command
2050                              `(lambda ()
2051                                 (interactive)
2052                                 (let ((gnus-summary-show-article-charset-alist
2053                                        '((1 . ,cs))))
2054                                   (gnus-summary-show-article 1))))
2055                        `[,(symbol-name cs) ,command t]))
2056                    (sort (if (fboundp 'coding-system-list)
2057                              (coding-system-list)
2058                            (mapcar 'car mm-mime-mule-charset-alist))
2059                          'string<)))))
2060              ("Washing"
2061               ("Remove Blanks"
2062                ["Leading" gnus-article-strip-leading-blank-lines t]
2063                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2064                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2065                ["All of the above" gnus-article-strip-blank-lines t]
2066                ["All" gnus-article-strip-all-blank-lines t]
2067                ["Leading space" gnus-article-strip-leading-space t]
2068                ["Trailing space" gnus-article-strip-trailing-space t]
2069                ["Leading space in headers"
2070                 gnus-article-remove-leading-whitespace t])
2071               ["Overstrike" gnus-article-treat-overstrike t]
2072               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2073               ["Emphasis" gnus-article-emphasize t]
2074               ["Word wrap" gnus-article-fill-cited-article t]
2075               ["Fill long lines" gnus-article-fill-long-lines t]
2076               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2077               ["CR" gnus-article-remove-cr t]
2078               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2079               ["Base64" gnus-article-de-base64-unreadable t]
2080               ["Rot 13" gnus-summary-caesar-message
2081                ,@(if (featurep 'xemacs) '(t)
2082                    '(:help "\"Caesar rotate\" article by 13"))]
2083               ["Morse decode" gnus-summary-morse-message t]
2084               ["Unix pipe..." gnus-summary-pipe-message t]
2085               ["Add buttons" gnus-article-add-buttons t]
2086               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2087               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2088               ["Verbose header" gnus-summary-verbose-headers t]
2089               ["Toggle header" gnus-summary-toggle-header t]
2090               ["Unfold headers" gnus-article-treat-unfold-headers t]
2091               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2092               ["Html" gnus-article-wash-html t]
2093               ["URLs" gnus-article-unsplit-urls t]
2094               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2095               ["HZ" gnus-article-decode-HZ t]
2096               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2097               )
2098              ("Output"
2099               ["Save in default format" gnus-summary-save-article
2100                ,@(if (featurep 'xemacs) '(t)
2101                    '(:help "Save article using default method"))]
2102               ["Save in file" gnus-summary-save-article-file
2103                ,@(if (featurep 'xemacs) '(t)
2104                    '(:help "Save article in file"))]
2105               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2106               ["Save in MH folder" gnus-summary-save-article-folder t]
2107               ["Save in VM folder" gnus-summary-save-article-vm t]
2108               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2109               ["Save body in file" gnus-summary-save-article-body-file t]
2110               ["Pipe through a filter" gnus-summary-pipe-output t]
2111               ["Add to SOUP packet" gnus-soup-add-article t]
2112               ["Print with Muttprint" gnus-summary-muttprint t]
2113               ["Print" gnus-summary-print-article t])
2114              ("Backend"
2115               ["Respool article..." gnus-summary-respool-article t]
2116               ["Move article..." gnus-summary-move-article
2117                (gnus-check-backend-function
2118                 'request-move-article gnus-newsgroup-name)]
2119               ["Copy article..." gnus-summary-copy-article t]
2120               ["Crosspost article..." gnus-summary-crosspost-article
2121                (gnus-check-backend-function
2122                 'request-replace-article gnus-newsgroup-name)]
2123               ["Import file..." gnus-summary-import-article t]
2124               ["Create article..." gnus-summary-create-article t]
2125               ["Check if posted" gnus-summary-article-posted-p t]
2126               ["Edit article" gnus-summary-edit-article
2127                (not (gnus-group-read-only-p))]
2128               ["Delete article" gnus-summary-delete-article
2129                (gnus-check-backend-function
2130                 'request-expire-articles gnus-newsgroup-name)]
2131               ["Query respool" gnus-summary-respool-query t]
2132               ["Trace respool" gnus-summary-respool-trace t]
2133               ["Delete expirable articles" gnus-summary-expire-articles-now
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)])
2136              ("Extract"
2137               ["Uudecode" gnus-uu-decode-uu
2138                ,@(if (featurep 'xemacs) '(t)
2139                    '(:help "Decode uuencoded article(s)"))]
2140               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2141               ["Unshar" gnus-uu-decode-unshar t]
2142               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2143               ["Save" gnus-uu-decode-save t]
2144               ["Binhex" gnus-uu-decode-binhex t]
2145               ["Postscript" gnus-uu-decode-postscript t]
2146               ["All MIME parts" gnus-summary-save-parts t])
2147              ("Cache"
2148               ["Enter article" gnus-cache-enter-article t]
2149               ["Remove article" gnus-cache-remove-article t])
2150              ["Translate" gnus-article-babel t]
2151              ["Select article buffer" gnus-summary-select-article-buffer t]
2152              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2153              ["Isearch article..." gnus-summary-isearch-article t]
2154              ["Beginning of the article" gnus-summary-beginning-of-article t]
2155              ["End of the article" gnus-summary-end-of-article t]
2156              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2157              ["Fetch referenced articles" gnus-summary-refer-references t]
2158              ["Fetch current thread" gnus-summary-refer-thread t]
2159              ["Fetch article with id..." gnus-summary-refer-article t]
2160              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2161              ["Redisplay" gnus-summary-show-article t]
2162              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2163       (easy-menu-define
2164         gnus-summary-article-menu gnus-summary-mode-map ""
2165         (cons "Article" innards))
2166
2167       (if (not (keymapp gnus-summary-article-menu))
2168           (easy-menu-define
2169             gnus-article-commands-menu gnus-article-mode-map ""
2170             (cons "Commands" innards))
2171         ;; in Emacs, don't share menu.
2172         (setq gnus-article-commands-menu
2173               (copy-keymap gnus-summary-article-menu))
2174         (define-key gnus-article-mode-map [menu-bar commands]
2175           (cons "Commands" gnus-article-commands-menu))))
2176
2177     (easy-menu-define
2178       gnus-summary-thread-menu gnus-summary-mode-map ""
2179       '("Threads"
2180         ["Toggle threading" gnus-summary-toggle-threads t]
2181         ["Hide threads" gnus-summary-hide-all-threads t]
2182         ["Show threads" gnus-summary-show-all-threads t]
2183         ["Hide thread" gnus-summary-hide-thread t]
2184         ["Show thread" gnus-summary-show-thread t]
2185         ["Go to next thread" gnus-summary-next-thread t]
2186         ["Go to previous thread" gnus-summary-prev-thread t]
2187         ["Go down thread" gnus-summary-down-thread t]
2188         ["Go up thread" gnus-summary-up-thread t]
2189         ["Top of thread" gnus-summary-top-thread t]
2190         ["Mark thread as read" gnus-summary-kill-thread t]
2191         ["Lower thread score" gnus-summary-lower-thread t]
2192         ["Raise thread score" gnus-summary-raise-thread t]
2193         ["Rethread current" gnus-summary-rethread-current t]))
2194
2195     (easy-menu-define
2196       gnus-summary-post-menu gnus-summary-mode-map ""
2197       `("Post"
2198         ["Send a message (mail or news)" gnus-summary-post-news
2199          ,@(if (featurep 'xemacs) '(t)
2200              '(:help "Post an article"))]
2201         ["Followup" gnus-summary-followup
2202          ,@(if (featurep 'xemacs) '(t)
2203              '(:help "Post followup to this article"))]
2204         ["Followup and yank" gnus-summary-followup-with-original
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post followup to this article, quoting its contents"))]
2207         ["Supersede article" gnus-summary-supersede-article t]
2208         ["Cancel article" gnus-summary-cancel-article
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Cancel an article you posted"))]
2211         ["Reply" gnus-summary-reply t]
2212         ["Reply and yank" gnus-summary-reply-with-original t]
2213         ["Wide reply" gnus-summary-wide-reply t]
2214         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Mail a reply, quoting this article"))]
2217         ["Very wide reply" gnus-summary-very-wide-reply t]
2218         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Mail a very wide reply, quoting this article"))]
2221         ["Mail forward" gnus-summary-mail-forward t]
2222         ["Post forward" gnus-summary-post-forward t]
2223         ["Digest and mail" gnus-uu-digest-mail-forward t]
2224         ["Digest and post" gnus-uu-digest-post-forward t]
2225         ["Resend message" gnus-summary-resend-message t]
2226         ["Resend message edit" gnus-summary-resend-message-edit t]
2227         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2228         ["Send a mail" gnus-summary-mail-other-window t]
2229         ["Create a local message" gnus-summary-news-other-window t]
2230         ["Uuencode and post" gnus-uu-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post a uuencoded article"))]
2233         ["Followup via news" gnus-summary-followup-to-mail t]
2234         ["Followup via news and yank"
2235          gnus-summary-followup-to-mail-with-original t]
2236         ;;("Draft"
2237         ;;["Send" gnus-summary-send-draft t]
2238         ;;["Send bounced" gnus-resend-bounced-mail t])
2239         ))
2240
2241     (cond
2242      ((not (keymapp gnus-summary-post-menu))
2243       (setq gnus-article-post-menu gnus-summary-post-menu))
2244      ((not gnus-article-post-menu)
2245       ;; Don't share post menu.
2246       (setq gnus-article-post-menu
2247             (copy-keymap gnus-summary-post-menu))))
2248     (define-key gnus-article-mode-map [menu-bar post]
2249       (cons "Post" gnus-article-post-menu))
2250
2251     (easy-menu-define
2252       gnus-summary-misc-menu gnus-summary-mode-map ""
2253       `("Gnus"
2254         ("Mark Read"
2255          ["Mark as read" gnus-summary-mark-as-read-forward t]
2256          ["Mark same subject and select"
2257           gnus-summary-kill-same-subject-and-select t]
2258          ["Mark same subject" gnus-summary-kill-same-subject t]
2259          ["Catchup" gnus-summary-catchup
2260           ,@(if (featurep 'xemacs) '(t)
2261               '(:help "Mark unread articles in this group as read"))]
2262          ["Catchup all" gnus-summary-catchup-all t]
2263          ["Catchup to here" gnus-summary-catchup-to-here t]
2264          ["Catchup from here" gnus-summary-catchup-from-here t]
2265          ["Catchup region" gnus-summary-mark-region-as-read t]
2266          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2267         ("Mark Various"
2268          ["Tick" gnus-summary-tick-article-forward t]
2269          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2270          ["Remove marks" gnus-summary-clear-mark-forward t]
2271          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2272          ["Set bookmark" gnus-summary-set-bookmark t]
2273          ["Remove bookmark" gnus-summary-remove-bookmark t])
2274         ("Limit to"
2275          ["Marks..." gnus-summary-limit-to-marks t]
2276          ["Subject..." gnus-summary-limit-to-subject t]
2277          ["Author..." gnus-summary-limit-to-author t]
2278          ["Age..." gnus-summary-limit-to-age t]
2279          ["Extra..." gnus-summary-limit-to-extra t]
2280          ["Score..." gnus-summary-limit-to-score t]
2281          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2282          ["Unread" gnus-summary-limit-to-unread t]
2283          ["Unseen" gnus-summary-limit-to-unseen t]
2284          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2285          ["Next articles" gnus-summary-limit-to-articles t]
2286          ["Pop limit" gnus-summary-pop-limit t]
2287          ["Show dormant" gnus-summary-limit-include-dormant t]
2288          ["Hide childless dormant"
2289           gnus-summary-limit-exclude-childless-dormant t]
2290          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2291          ["Hide marked" gnus-summary-limit-exclude-marks t]
2292          ["Show expunged" gnus-summary-limit-include-expunged t])
2293         ("Process Mark"
2294          ["Set mark" gnus-summary-mark-as-processable t]
2295          ["Remove mark" gnus-summary-unmark-as-processable t]
2296          ["Remove all marks" gnus-summary-unmark-all-processable t]
2297          ["Mark above" gnus-uu-mark-over t]
2298          ["Mark series" gnus-uu-mark-series t]
2299          ["Mark region" gnus-uu-mark-region t]
2300          ["Unmark region" gnus-uu-unmark-region t]
2301          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2302          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2303          ["Mark all" gnus-uu-mark-all t]
2304          ["Mark buffer" gnus-uu-mark-buffer t]
2305          ["Mark sparse" gnus-uu-mark-sparse t]
2306          ["Mark thread" gnus-uu-mark-thread t]
2307          ["Unmark thread" gnus-uu-unmark-thread t]
2308          ("Process Mark Sets"
2309           ["Kill" gnus-summary-kill-process-mark t]
2310           ["Yank" gnus-summary-yank-process-mark
2311            gnus-newsgroup-process-stack]
2312           ["Save" gnus-summary-save-process-mark t]))
2313         ("Scroll article"
2314          ["Page forward" gnus-summary-next-page
2315           ,@(if (featurep 'xemacs) '(t)
2316               '(:help "Show next page of article"))]
2317          ["Page backward" gnus-summary-prev-page
2318           ,@(if (featurep 'xemacs) '(t)
2319               '(:help "Show previous page of article"))]
2320          ["Line forward" gnus-summary-scroll-up t])
2321         ("Move"
2322          ["Next unread article" gnus-summary-next-unread-article t]
2323          ["Previous unread article" gnus-summary-prev-unread-article t]
2324          ["Next article" gnus-summary-next-article t]
2325          ["Previous article" gnus-summary-prev-article t]
2326          ["Next unread subject" gnus-summary-next-unread-subject t]
2327          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2328          ["Next article same subject" gnus-summary-next-same-subject t]
2329          ["Previous article same subject" gnus-summary-prev-same-subject t]
2330          ["First unread article" gnus-summary-first-unread-article t]
2331          ["Best unread article" gnus-summary-best-unread-article t]
2332          ["Go to subject number..." gnus-summary-goto-subject t]
2333          ["Go to article number..." gnus-summary-goto-article t]
2334          ["Go to the last article" gnus-summary-goto-last-article t]
2335          ["Pop article off history" gnus-summary-pop-article t])
2336         ("Sort"
2337          ["Sort by number" gnus-summary-sort-by-number t]
2338          ["Sort by author" gnus-summary-sort-by-author t]
2339          ["Sort by subject" gnus-summary-sort-by-subject t]
2340          ["Sort by date" gnus-summary-sort-by-date t]
2341          ["Sort by score" gnus-summary-sort-by-score t]
2342          ["Sort by lines" gnus-summary-sort-by-lines t]
2343          ["Sort by characters" gnus-summary-sort-by-chars t]
2344          ["Randomize" gnus-summary-sort-by-random t]
2345          ["Original sort" gnus-summary-sort-by-original t])
2346         ("Help"
2347          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2348          ["Describe group" gnus-summary-describe-group t]
2349          ["Fetch charter" gnus-group-fetch-charter
2350           ,@(if (featurep 'xemacs) nil
2351               '(:help "Display the charter of the current group"))]
2352          ["Fetch control message" gnus-group-fetch-control
2353           ,@(if (featurep 'xemacs) nil
2354               '(:help "Display the archived control message for the current group"))]
2355          ["Read manual" gnus-info-find-node t])
2356         ("Modes"
2357          ["Pick and read" gnus-pick-mode t]
2358          ["Binary" gnus-binary-mode t])
2359         ("Regeneration"
2360          ["Regenerate" gnus-summary-prepare t]
2361          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2362          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2363          ["Toggle threading" gnus-summary-toggle-threads t])
2364         ["See old articles" gnus-summary-insert-old-articles t]
2365         ["See new articles" gnus-summary-insert-new-articles t]
2366         ["Filter articles..." gnus-summary-execute-command t]
2367         ["Run command on subjects..." gnus-summary-universal-argument t]
2368         ["Search articles forward..." gnus-summary-search-article-forward t]
2369         ["Search articles backward..." gnus-summary-search-article-backward t]
2370         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2371         ["Expand window" gnus-summary-expand-window t]
2372         ["Expire expirable articles" gnus-summary-expire-articles
2373          (gnus-check-backend-function
2374           'request-expire-articles gnus-newsgroup-name)]
2375         ["Edit local kill file" gnus-summary-edit-local-kill t]
2376         ["Edit main kill file" gnus-summary-edit-global-kill t]
2377         ["Edit group parameters" gnus-summary-edit-parameters t]
2378         ["Customize group parameters" gnus-summary-customize-parameters t]
2379         ["Send a bug report" gnus-bug t]
2380         ("Exit"
2381          ["Catchup and exit" gnus-summary-catchup-and-exit
2382           ,@(if (featurep 'xemacs) '(t)
2383               '(:help "Mark unread articles in this group as read, then exit"))]
2384          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2385          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2386          ["Exit group" gnus-summary-exit
2387           ,@(if (featurep 'xemacs) '(t)
2388               '(:help "Exit current group, return to group selection mode"))]
2389          ["Exit group without updating" gnus-summary-exit-no-update t]
2390          ["Exit and goto next group" gnus-summary-next-group t]
2391          ["Exit and goto prev group" gnus-summary-prev-group t]
2392          ["Reselect group" gnus-summary-reselect-current-group t]
2393          ["Rescan group" gnus-summary-rescan-group t]
2394          ["Update dribble" gnus-summary-save-newsrc t])))
2395
2396     (gnus-run-hooks 'gnus-summary-menu-hook)))
2397
2398 (defvar gnus-summary-tool-bar-map nil)
2399
2400 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2401 (defun gnus-summary-make-tool-bar ()
2402   (if (and (fboundp 'tool-bar-add-item-from-menu)
2403            (default-value 'tool-bar-mode)
2404            (not gnus-summary-tool-bar-map))
2405       (setq gnus-summary-tool-bar-map
2406             (let ((tool-bar-map (make-sparse-keymap))
2407                   (load-path (mm-image-load-path)))
2408               (tool-bar-add-item-from-menu
2409                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2410               (tool-bar-add-item-from-menu
2411                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2412               (tool-bar-add-item-from-menu
2413                'gnus-summary-post-news "post" gnus-summary-mode-map)
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-followup "followup" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-reply "reply" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2438               tool-bar-map)))
2439   (if gnus-summary-tool-bar-map
2440       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2441
2442 (defun gnus-score-set-default (var value)
2443   "A version of set that updates the GNU Emacs menu-bar."
2444   (set var value)
2445   ;; It is the message that forces the active status to be updated.
2446   (message ""))
2447
2448 (defun gnus-make-score-map (type)
2449   "Make a summary score map of type TYPE."
2450   (if t
2451       nil
2452     (let ((headers '(("author" "from" string)
2453                      ("subject" "subject" string)
2454                      ("article body" "body" string)
2455                      ("article head" "head" string)
2456                      ("xref" "xref" string)
2457                      ("extra header" "extra" string)
2458                      ("lines" "lines" number)
2459                      ("followups to author" "followup" string)))
2460           (types '((number ("less than" <)
2461                            ("greater than" >)
2462                            ("equal" =))
2463                    (string ("substring" s)
2464                            ("exact string" e)
2465                            ("fuzzy string" f)
2466                            ("regexp" r))))
2467           (perms '(("temporary" (current-time-string))
2468                    ("permanent" nil)
2469                    ("immediate" now)))
2470           header)
2471       (list
2472        (apply
2473         'nconc
2474         (list
2475          (if (eq type 'lower)
2476              "Lower score"
2477            "Increase score"))
2478         (let (outh)
2479           (while headers
2480             (setq header (car headers))
2481             (setq outh
2482                   (cons
2483                    (apply
2484                     'nconc
2485                     (list (car header))
2486                     (let ((ts (cdr (assoc (nth 2 header) types)))
2487                           outt)
2488                       (while ts
2489                         (setq outt
2490                               (cons
2491                                (apply
2492                                 'nconc
2493                                 (list (caar ts))
2494                                 (let ((ps perms)
2495                                       outp)
2496                                   (while ps
2497                                     (setq outp
2498                                           (cons
2499                                            (vector
2500                                             (caar ps)
2501                                             (list
2502                                              'gnus-summary-score-entry
2503                                              (nth 1 header)
2504                                              (if (or (string= (nth 1 header)
2505                                                               "head")
2506                                                      (string= (nth 1 header)
2507                                                               "body"))
2508                                                  ""
2509                                                (list 'gnus-summary-header
2510                                                      (nth 1 header)))
2511                                              (list 'quote (nth 1 (car ts)))
2512                                              (list 'gnus-score-delta-default
2513                                                    nil)
2514                                              (nth 1 (car ps))
2515                                              t)
2516                                             t)
2517                                            outp))
2518                                     (setq ps (cdr ps)))
2519                                   (list (nreverse outp))))
2520                                outt))
2521                         (setq ts (cdr ts)))
2522                       (list (nreverse outt))))
2523                    outh))
2524             (setq headers (cdr headers)))
2525           (list (nreverse outh))))))))
2526
2527 \f
2528
2529 (defun gnus-summary-mode (&optional group)
2530   "Major mode for reading articles.
2531
2532 All normal editing commands are switched off.
2533 \\<gnus-summary-mode-map>
2534 Each line in this buffer represents one article.  To read an
2535 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2536 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2537 respectively.
2538
2539 You can also post articles and send mail from this buffer.  To
2540 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2541 of an article, type `\\[gnus-summary-reply]'.
2542
2543 There are approx. one gazillion commands you can execute in this
2544 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2545
2546 The following commands are available:
2547
2548 \\{gnus-summary-mode-map}"
2549   (interactive)
2550   (kill-all-local-variables)
2551   (when (gnus-visual-p 'summary-menu 'menu)
2552     (gnus-summary-make-menu-bar)
2553     (gnus-summary-make-tool-bar))
2554   (gnus-summary-make-local-variables)
2555   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2556     (gnus-summary-make-local-variables))
2557   (gnus-make-thread-indent-array)
2558   (gnus-simplify-mode-line)
2559   (setq major-mode 'gnus-summary-mode)
2560   (setq mode-name "Summary")
2561   (make-local-variable 'minor-mode-alist)
2562   (use-local-map gnus-summary-mode-map)
2563   (buffer-disable-undo)
2564   (setq buffer-read-only t)             ;Disable modification
2565   (setq truncate-lines t)
2566   (setq selective-display t)
2567   (setq selective-display-ellipses t)   ;Display `...'
2568   (gnus-summary-set-display-table)
2569   (gnus-set-default-directory)
2570   (setq gnus-newsgroup-name group)
2571   (make-local-variable 'gnus-summary-line-format)
2572   (make-local-variable 'gnus-summary-line-format-spec)
2573   (make-local-variable 'gnus-summary-dummy-line-format)
2574   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2575   (make-local-variable 'gnus-summary-mark-positions)
2576   (make-local-hook 'pre-command-hook)
2577   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2578   (gnus-run-hooks 'gnus-summary-mode-hook)
2579   (turn-on-gnus-mailing-list-mode)
2580   (mm-enable-multibyte)
2581   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2582   (gnus-update-summary-mark-positions))
2583
2584 (defun gnus-summary-make-local-variables ()
2585   "Make all the local summary buffer variables."
2586   (let (global)
2587     (dolist (local gnus-summary-local-variables)
2588       (if (consp local)
2589           (progn
2590             (if (eq (cdr local) 'global)
2591                 ;; Copy the global value of the variable.
2592                 (setq global (symbol-value (car local)))
2593               ;; Use the value from the list.
2594               (setq global (eval (cdr local))))
2595             (set (make-local-variable (car local)) global))
2596         ;; Simple nil-valued local variable.
2597         (set (make-local-variable local) nil)))))
2598
2599 (defun gnus-summary-clear-local-variables ()
2600   (let ((locals gnus-summary-local-variables))
2601     (while locals
2602       (if (consp (car locals))
2603           (and (vectorp (caar locals))
2604                (set (caar locals) nil))
2605         (and (vectorp (car locals))
2606              (set (car locals) nil)))
2607       (setq locals (cdr locals)))))
2608
2609 ;; Summary data functions.
2610
2611 (defmacro gnus-data-number (data)
2612   `(car ,data))
2613
2614 (defmacro gnus-data-set-number (data number)
2615   `(setcar ,data ,number))
2616
2617 (defmacro gnus-data-mark (data)
2618   `(nth 1 ,data))
2619
2620 (defmacro gnus-data-set-mark (data mark)
2621   `(setcar (nthcdr 1 ,data) ,mark))
2622
2623 (defmacro gnus-data-pos (data)
2624   `(nth 2 ,data))
2625
2626 (defmacro gnus-data-set-pos (data pos)
2627   `(setcar (nthcdr 2 ,data) ,pos))
2628
2629 (defmacro gnus-data-header (data)
2630   `(nth 3 ,data))
2631
2632 (defmacro gnus-data-set-header (data header)
2633   `(setf (nth 3 ,data) ,header))
2634
2635 (defmacro gnus-data-level (data)
2636   `(nth 4 ,data))
2637
2638 (defmacro gnus-data-unread-p (data)
2639   `(= (nth 1 ,data) gnus-unread-mark))
2640
2641 (defmacro gnus-data-read-p (data)
2642   `(/= (nth 1 ,data) gnus-unread-mark))
2643
2644 (defmacro gnus-data-pseudo-p (data)
2645   `(consp (nth 3 ,data)))
2646
2647 (defmacro gnus-data-find (number)
2648   `(assq ,number gnus-newsgroup-data))
2649
2650 (defmacro gnus-data-find-list (number &optional data)
2651   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2652      (memq (assq ,number bdata)
2653            bdata)))
2654
2655 (defmacro gnus-data-make (number mark pos header level)
2656   `(list ,number ,mark ,pos ,header ,level))
2657
2658 (defun gnus-data-enter (after-article number mark pos header level offset)
2659   (let ((data (gnus-data-find-list after-article)))
2660     (unless data
2661       (error "No such article: %d" after-article))
2662     (setcdr data (cons (gnus-data-make number mark pos header level)
2663                        (cdr data)))
2664     (setq gnus-newsgroup-data-reverse nil)
2665     (gnus-data-update-list (cddr data) offset)))
2666
2667 (defun gnus-data-enter-list (after-article list &optional offset)
2668   (when list
2669     (let ((data (and after-article (gnus-data-find-list after-article)))
2670           (ilist list))
2671       (if (not (or data
2672                    after-article))
2673           (let ((odata gnus-newsgroup-data))
2674             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2675             (when offset
2676               (gnus-data-update-list odata offset)))
2677       ;; Find the last element in the list to be spliced into the main
2678         ;; list.
2679         (while (cdr list)
2680           (setq list (cdr list)))
2681         (if (not data)
2682             (progn
2683               (setcdr list gnus-newsgroup-data)
2684               (setq gnus-newsgroup-data ilist)
2685               (when offset
2686                 (gnus-data-update-list (cdr list) offset)))
2687           (setcdr list (cdr data))
2688           (setcdr data ilist)
2689           (when offset
2690             (gnus-data-update-list (cdr list) offset))))
2691       (setq gnus-newsgroup-data-reverse nil))))
2692
2693 (defun gnus-data-remove (article &optional offset)
2694   (let ((data gnus-newsgroup-data))
2695     (if (= (gnus-data-number (car data)) article)
2696         (progn
2697           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2698                 gnus-newsgroup-data-reverse nil)
2699           (when offset
2700             (gnus-data-update-list gnus-newsgroup-data offset)))
2701       (while (cdr data)
2702         (when (= (gnus-data-number (cadr data)) article)
2703           (setcdr data (cddr data))
2704           (when offset
2705             (gnus-data-update-list (cdr data) offset))
2706           (setq data nil
2707                 gnus-newsgroup-data-reverse nil))
2708         (setq data (cdr data))))))
2709
2710 (defmacro gnus-data-list (backward)
2711   `(if ,backward
2712        (or gnus-newsgroup-data-reverse
2713            (setq gnus-newsgroup-data-reverse
2714                  (reverse gnus-newsgroup-data)))
2715      gnus-newsgroup-data))
2716
2717 (defun gnus-data-update-list (data offset)
2718   "Add OFFSET to the POS of all data entries in DATA."
2719   (setq gnus-newsgroup-data-reverse nil)
2720   (while data
2721     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2722     (setq data (cdr data))))
2723
2724 (defun gnus-summary-article-pseudo-p (article)
2725   "Say whether this article is a pseudo article or not."
2726   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2727
2728 (defmacro gnus-summary-article-sparse-p (article)
2729   "Say whether this article is a sparse article or not."
2730   `(memq ,article gnus-newsgroup-sparse))
2731
2732 (defmacro gnus-summary-article-ancient-p (article)
2733   "Say whether this article is a sparse article or not."
2734   `(memq ,article gnus-newsgroup-ancient))
2735
2736 (defun gnus-article-parent-p (number)
2737   "Say whether this article is a parent or not."
2738   (let ((data (gnus-data-find-list number)))
2739     (and (cdr data)              ; There has to be an article after...
2740          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2741             (gnus-data-level (nth 1 data))))))
2742
2743 (defun gnus-article-children (number)
2744   "Return a list of all children to NUMBER."
2745   (let* ((data (gnus-data-find-list number))
2746          (level (gnus-data-level (car data)))
2747          children)
2748     (setq data (cdr data))
2749     (while (and data
2750                 (= (gnus-data-level (car data)) (1+ level)))
2751       (push (gnus-data-number (car data)) children)
2752       (setq data (cdr data)))
2753     children))
2754
2755 (defmacro gnus-summary-skip-intangible ()
2756   "If the current article is intangible, then jump to a different article."
2757   '(let ((to (get-text-property (point) 'gnus-intangible)))
2758      (and to (gnus-summary-goto-subject to))))
2759
2760 (defmacro gnus-summary-article-intangible-p ()
2761   "Say whether this article is intangible or not."
2762   '(get-text-property (point) 'gnus-intangible))
2763
2764 (defun gnus-article-read-p (article)
2765   "Say whether ARTICLE is read or not."
2766   (not (or (memq article gnus-newsgroup-marked)
2767            (memq article gnus-newsgroup-spam-marked)
2768            (memq article gnus-newsgroup-unreads)
2769            (memq article gnus-newsgroup-unselected)
2770            (memq article gnus-newsgroup-dormant))))
2771
2772 ;; Some summary mode macros.
2773
2774 (defmacro gnus-summary-article-number ()
2775   "The article number of the article on the current line.
2776 If there isn't an article number here, then we return the current
2777 article number."
2778   '(progn
2779      (gnus-summary-skip-intangible)
2780      (or (get-text-property (point) 'gnus-number)
2781          (gnus-summary-last-subject))))
2782
2783 (defmacro gnus-summary-article-header (&optional number)
2784   "Return the header of article NUMBER."
2785   `(gnus-data-header (gnus-data-find
2786                       ,(or number '(gnus-summary-article-number)))))
2787
2788 (defmacro gnus-summary-thread-level (&optional number)
2789   "Return the level of thread that starts with article NUMBER."
2790   `(if (and (eq gnus-summary-make-false-root 'dummy)
2791             (get-text-property (point) 'gnus-intangible))
2792        0
2793      (gnus-data-level (gnus-data-find
2794                        ,(or number '(gnus-summary-article-number))))))
2795
2796 (defmacro gnus-summary-article-mark (&optional number)
2797   "Return the mark of article NUMBER."
2798   `(gnus-data-mark (gnus-data-find
2799                     ,(or number '(gnus-summary-article-number)))))
2800
2801 (defmacro gnus-summary-article-pos (&optional number)
2802   "Return the position of the line of article NUMBER."
2803   `(gnus-data-pos (gnus-data-find
2804                    ,(or number '(gnus-summary-article-number)))))
2805
2806 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2807 (defmacro gnus-summary-article-subject (&optional number)
2808   "Return current subject string or nil if nothing."
2809   `(let ((headers
2810           ,(if number
2811                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2812              '(gnus-data-header (assq (gnus-summary-article-number)
2813                                       gnus-newsgroup-data)))))
2814      (and headers
2815           (vectorp headers)
2816           (mail-header-subject headers))))
2817
2818 (defmacro gnus-summary-article-score (&optional number)
2819   "Return current article score."
2820   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2821                   gnus-newsgroup-scored))
2822        gnus-summary-default-score 0))
2823
2824 (defun gnus-summary-article-children (&optional number)
2825   "Return a list of article numbers that are children of article NUMBER."
2826   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2827          (level (gnus-data-level (car data)))
2828          l children)
2829     (while (and (setq data (cdr data))
2830                 (> (setq l (gnus-data-level (car data))) level))
2831       (and (= (1+ level) l)
2832            (push (gnus-data-number (car data))
2833                  children)))
2834     (nreverse children)))
2835
2836 (defun gnus-summary-article-parent (&optional number)
2837   "Return the article number of the parent of article NUMBER."
2838   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2839                                     (gnus-data-list t)))
2840          (level (gnus-data-level (car data))))
2841     (if (zerop level)
2842         ()                              ; This is a root.
2843       ;; We search until we find an article with a level less than
2844       ;; this one.  That function has to be the parent.
2845       (while (and (setq data (cdr data))
2846                   (not (< (gnus-data-level (car data)) level))))
2847       (and data (gnus-data-number (car data))))))
2848
2849 (defun gnus-unread-mark-p (mark)
2850   "Say whether MARK is the unread mark."
2851   (= mark gnus-unread-mark))
2852
2853 (defun gnus-read-mark-p (mark)
2854   "Say whether MARK is one of the marks that mark as read.
2855 This is all marks except unread, ticked, dormant, and expirable."
2856   (not (or (= mark gnus-unread-mark)
2857            (= mark gnus-ticked-mark)
2858            (= mark gnus-dormant-mark)
2859            (= mark gnus-expirable-mark))))
2860
2861 (defmacro gnus-article-mark (number)
2862   "Return the MARK of article NUMBER.
2863 This macro should only be used when computing the mark the \"first\"
2864 time; i.e., when generating the summary lines.  After that,
2865 `gnus-summary-article-mark' should be used to examine the
2866 marks of articles."
2867   `(cond
2868     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2869     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2870     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2871     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2872     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2873     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2874     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2875     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2876            gnus-ancient-mark))))
2877
2878 ;; Saving hidden threads.
2879
2880 (defmacro gnus-save-hidden-threads (&rest forms)
2881   "Save hidden threads, eval FORMS, and restore the hidden threads."
2882   (let ((config (make-symbol "config")))
2883     `(let ((,config (gnus-hidden-threads-configuration)))
2884        (unwind-protect
2885            (save-excursion
2886              ,@forms)
2887          (gnus-restore-hidden-threads-configuration ,config)))))
2888 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2889 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2890
2891 (defun gnus-data-compute-positions ()
2892   "Compute the positions of all articles."
2893   (setq gnus-newsgroup-data-reverse nil)
2894   (let ((data gnus-newsgroup-data))
2895     (save-excursion
2896       (gnus-save-hidden-threads
2897         (gnus-summary-show-all-threads)
2898         (goto-char (point-min))
2899         (while data
2900           (while (get-text-property (point) 'gnus-intangible)
2901             (forward-line 1))
2902           (gnus-data-set-pos (car data) (+ (point) 3))
2903           (setq data (cdr data))
2904           (forward-line 1))))))
2905
2906 (defun gnus-hidden-threads-configuration ()
2907   "Return the current hidden threads configuration."
2908   (save-excursion
2909     (let (config)
2910       (goto-char (point-min))
2911       (while (search-forward "\r" nil t)
2912         (push (1- (point)) config))
2913       config)))
2914
2915 (defun gnus-restore-hidden-threads-configuration (config)
2916   "Restore hidden threads configuration from CONFIG."
2917   (save-excursion
2918     (let (point buffer-read-only)
2919       (while (setq point (pop config))
2920         (when (and (< point (point-max))
2921                    (goto-char point)
2922                    (eq (char-after) ?\n))
2923           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2924
2925 ;; Various summary mode internalish functions.
2926
2927 (defun gnus-mouse-pick-article (e)
2928   (interactive "e")
2929   (mouse-set-point e)
2930   (gnus-summary-next-page nil t))
2931
2932 (defun gnus-summary-set-display-table ()
2933   "Change the display table.
2934 Odd characters have a tendency to mess
2935 up nicely formatted displays - we make all possible glyphs
2936 display only a single character."
2937
2938   ;; We start from the standard display table, if any.
2939   (let ((table (or (copy-sequence standard-display-table)
2940                    (make-display-table)))
2941         (i 32))
2942     ;; Nix out all the control chars...
2943     (while (>= (setq i (1- i)) 0)
2944       (aset table i [??]))
2945    ;; ... but not newline and cr, of course.  (cr is necessary for the
2946     ;; selective display).
2947     (aset table ?\n nil)
2948     (aset table ?\r nil)
2949     ;; We keep TAB as well.
2950     (aset table ?\t nil)
2951     ;; We nix out any glyphs over 126 that are not set already.
2952     (let ((i 256))
2953       (while (>= (setq i (1- i)) 127)
2954         ;; Only modify if the entry is nil.
2955         (unless (aref table i)
2956           (aset table i [??]))))
2957     (setq buffer-display-table table)))
2958
2959 (defun gnus-summary-set-article-display-arrow (pos)
2960   "Update the overlay arrow to point to line at position POS."
2961   (when (and gnus-summary-display-arrow
2962              (boundp 'overlay-arrow-position)
2963              (boundp 'overlay-arrow-string))
2964     (save-excursion
2965       (goto-char pos)
2966       (beginning-of-line)
2967       (unless overlay-arrow-position
2968         (setq overlay-arrow-position (make-marker)))
2969       (setq overlay-arrow-string "=>"
2970             overlay-arrow-position (set-marker overlay-arrow-position
2971                                                (point)
2972                                                (current-buffer))))))
2973
2974 (defun gnus-summary-buffer-name (group)
2975   "Return the summary buffer name of GROUP."
2976   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2977
2978 (defun gnus-summary-setup-buffer (group)
2979   "Initialize summary buffer."
2980   (let ((buffer (gnus-summary-buffer-name group))
2981         (dead-name (concat "*Dead Summary "
2982                            (gnus-group-decoded-name group) "*")))
2983     ;; If a dead summary buffer exists, we kill it.
2984     (when (gnus-buffer-live-p dead-name)
2985       (gnus-kill-buffer dead-name))
2986     (if (get-buffer buffer)
2987         (progn
2988           (set-buffer buffer)
2989           (setq gnus-summary-buffer (current-buffer))
2990           (not gnus-newsgroup-prepared))
2991       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2992       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2993       (gnus-summary-mode group)
2994       (when gnus-carpal
2995         (gnus-carpal-setup-buffer 'summary))
2996       (unless gnus-single-article-buffer
2997         (make-local-variable 'gnus-article-buffer)
2998         (make-local-variable 'gnus-article-current)
2999         (make-local-variable 'gnus-original-article-buffer))
3000       (setq gnus-newsgroup-name group)
3001       ;; Set any local variables in the group parameters.
3002       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3003       t)))
3004
3005 (defun gnus-set-global-variables ()
3006   "Set the global equivalents of the buffer-local variables.
3007 They are set to the latest values they had.  These reflect the summary
3008 buffer that was in action when the last article was fetched."
3009   (when (eq major-mode 'gnus-summary-mode)
3010     (setq gnus-summary-buffer (current-buffer))
3011     (let ((name gnus-newsgroup-name)
3012           (marked gnus-newsgroup-marked)
3013           (spam gnus-newsgroup-spam-marked)
3014           (unread gnus-newsgroup-unreads)
3015           (headers gnus-current-headers)
3016           (data gnus-newsgroup-data)
3017           (summary gnus-summary-buffer)
3018           (article-buffer gnus-article-buffer)
3019           (original gnus-original-article-buffer)
3020           (gac gnus-article-current)
3021           (reffed gnus-reffed-article-number)
3022           (score-file gnus-current-score-file)
3023           (default-charset gnus-newsgroup-charset)
3024           vlist)
3025       (let ((locals gnus-newsgroup-variables))
3026         (while locals
3027           (if (consp (car locals))
3028               (push (eval (caar locals)) vlist)
3029             (push (eval (car locals)) vlist))
3030           (setq locals (cdr locals)))
3031         (setq vlist (nreverse vlist)))
3032       (save-excursion
3033         (set-buffer gnus-group-buffer)
3034         (setq gnus-newsgroup-name name
3035               gnus-newsgroup-marked marked
3036               gnus-newsgroup-spam-marked spam
3037               gnus-newsgroup-unreads unread
3038               gnus-current-headers headers
3039               gnus-newsgroup-data data
3040               gnus-article-current gac
3041               gnus-summary-buffer summary
3042               gnus-article-buffer article-buffer
3043               gnus-original-article-buffer original
3044               gnus-reffed-article-number reffed
3045               gnus-current-score-file score-file
3046               gnus-newsgroup-charset default-charset)
3047         (let ((locals gnus-newsgroup-variables))
3048           (while locals
3049             (if (consp (car locals))
3050                 (set (caar locals) (pop vlist))
3051               (set (car locals) (pop vlist)))
3052             (setq locals (cdr locals))))
3053         ;; The article buffer also has local variables.
3054         (when (gnus-buffer-live-p gnus-article-buffer)
3055           (set-buffer gnus-article-buffer)
3056           (setq gnus-summary-buffer summary))))))
3057
3058 (defun gnus-summary-article-unread-p (article)
3059   "Say whether ARTICLE is unread or not."
3060   (memq article gnus-newsgroup-unreads))
3061
3062 (defun gnus-summary-first-article-p (&optional article)
3063   "Return whether ARTICLE is the first article in the buffer."
3064   (if (not (setq article (or article (gnus-summary-article-number))))
3065       nil
3066     (eq article (caar gnus-newsgroup-data))))
3067
3068 (defun gnus-summary-last-article-p (&optional article)
3069   "Return whether ARTICLE is the last article in the buffer."
3070   (if (not (setq article (or article (gnus-summary-article-number))))
3071       ;; All non-existent numbers are the last article.  :-)
3072       t
3073     (not (cdr (gnus-data-find-list article)))))
3074
3075 (defun gnus-make-thread-indent-array ()
3076   (let ((n 200))
3077     (unless (and gnus-thread-indent-array
3078                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3079       (setq gnus-thread-indent-array (make-vector 201 "")
3080             gnus-thread-indent-array-level gnus-thread-indent-level)
3081       (while (>= n 0)
3082         (aset gnus-thread-indent-array n
3083               (make-string (* n gnus-thread-indent-level) ? ))
3084         (setq n (1- n))))))
3085
3086 (defun gnus-update-summary-mark-positions ()
3087   "Compute where the summary marks are to go."
3088   (save-excursion
3089     (when (gnus-buffer-exists-p gnus-summary-buffer)
3090       (set-buffer gnus-summary-buffer))
3091     (let ((gnus-replied-mark 129)
3092           (gnus-score-below-mark 130)
3093           (gnus-score-over-mark 130)
3094           (gnus-downloaded-mark 131)
3095           (spec gnus-summary-line-format-spec)
3096           gnus-visual pos)
3097       (save-excursion
3098         (gnus-set-work-buffer)
3099         (let ((gnus-summary-line-format-spec spec)
3100               (gnus-newsgroup-downloadable '(0)))
3101           (gnus-summary-insert-line
3102            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3103            0 nil nil 128 t nil "" nil 1)
3104           (goto-char (point-min))
3105           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3106                                              (- (point) (point-min) 1)))))
3107           (goto-char (point-min))
3108           (push (cons 'replied (and (search-forward "\201" nil t)
3109                                     (- (point) (point-min) 1)))
3110                 pos)
3111           (goto-char (point-min))
3112           (push (cons 'score (and (search-forward "\202" nil t)
3113                                   (- (point) (point-min) 1)))
3114                 pos)
3115           (goto-char (point-min))
3116           (push (cons 'download
3117                       (and (search-forward "\203" nil t)
3118                            (- (point) (point-min) 1)))
3119                 pos)))
3120       (setq gnus-summary-mark-positions pos))))
3121
3122 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3123   "Insert a dummy root in the summary buffer."
3124   (beginning-of-line)
3125   (gnus-add-text-properties
3126    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3127    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3128
3129 (defun gnus-summary-extract-address-component (from)
3130   (or (car (funcall gnus-extract-address-components from))
3131       from))
3132
3133 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3134   (let ((mail-parse-charset gnus-newsgroup-charset)
3135         ; Is it really necessary to do this next part for each summary line?
3136         ; Luckily, doesn't seem to slow things down much.
3137         (mail-parse-ignored-charsets
3138          (save-excursion (set-buffer gnus-summary-buffer)
3139                          gnus-newsgroup-ignored-charsets)))
3140     (or
3141      (and gnus-ignored-from-addresses
3142           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3143           (let ((extra-headers (mail-header-extra header))
3144                 to
3145                 newsgroups)
3146             (cond
3147              ((setq to (cdr (assq 'To extra-headers)))
3148               (concat "-> "
3149                       (inline
3150                         (gnus-summary-extract-address-component
3151                          (funcall gnus-decode-encoded-word-function to)))))
3152              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3153               (concat "=> " newsgroups)))))
3154      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3155
3156 (defun gnus-summary-insert-line (gnus-tmp-header
3157                                  gnus-tmp-level gnus-tmp-current
3158                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3159                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3160                                  &optional gnus-tmp-dummy gnus-tmp-score
3161                                  gnus-tmp-process)
3162   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3163          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3164          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3165          (gnus-tmp-score-char
3166           (if (or (null gnus-summary-default-score)
3167                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3168                       gnus-summary-zcore-fuzz))
3169               ?                         ;Whitespace
3170             (if (< gnus-tmp-score gnus-summary-default-score)
3171                 gnus-score-below-mark gnus-score-over-mark)))
3172          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3173          (gnus-tmp-replied
3174           (cond (gnus-tmp-process gnus-process-mark)
3175                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3176                  gnus-cached-mark)
3177                 (gnus-tmp-replied gnus-replied-mark)
3178                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3179                  gnus-forwarded-mark)
3180                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3181                  gnus-saved-mark)
3182                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3183                  gnus-recent-mark)
3184                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3185                  gnus-unseen-mark)
3186                 (t gnus-no-mark)))
3187          (gnus-tmp-downloaded
3188           (cond (undownloaded 
3189                  gnus-undownloaded-mark)
3190                 (gnus-newsgroup-agentized
3191                  gnus-downloaded-mark)
3192                 (t
3193                  gnus-no-mark)))
3194          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3195          (gnus-tmp-name
3196           (cond
3197            ((string-match "<[^>]+> *$" gnus-tmp-from)
3198             (let ((beg (match-beginning 0)))
3199               (or (and (string-match "^\".+\"" gnus-tmp-from)
3200                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3201                   (substring gnus-tmp-from 0 beg))))
3202            ((string-match "(.+)" gnus-tmp-from)
3203             (substring gnus-tmp-from
3204                        (1+ (match-beginning 0)) (1- (match-end 0))))
3205            (t gnus-tmp-from)))
3206          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3207          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3208          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3209          (buffer-read-only nil))
3210     (when (string= gnus-tmp-name "")
3211       (setq gnus-tmp-name gnus-tmp-from))
3212     (unless (numberp gnus-tmp-lines)
3213       (setq gnus-tmp-lines -1))
3214     (if (= gnus-tmp-lines -1)
3215         (setq gnus-tmp-lines "?")
3216       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3217     (gnus-put-text-property
3218      (point)
3219      (progn (eval gnus-summary-line-format-spec) (point))
3220      'gnus-number gnus-tmp-number)
3221     (when (gnus-visual-p 'summary-highlight 'highlight)
3222       (forward-line -1)
3223       (gnus-run-hooks 'gnus-summary-update-hook)
3224       (forward-line 1))))
3225
3226 (defun gnus-summary-update-line (&optional dont-update)
3227   "Update summary line after change."
3228   (when (and gnus-summary-default-score
3229              (not gnus-summary-inhibit-highlight))
3230     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3231            (article (gnus-summary-article-number))
3232            (score (gnus-summary-article-score article)))
3233       (unless dont-update
3234         (if (and gnus-summary-mark-below
3235                  (< (gnus-summary-article-score)
3236                     gnus-summary-mark-below))
3237             ;; This article has a low score, so we mark it as read.
3238             (when (memq article gnus-newsgroup-unreads)
3239               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3240           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3241             ;; This article was previously marked as read on account
3242             ;; of a low score, but now it has risen, so we mark it as
3243             ;; unread.
3244             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3245         (gnus-summary-update-mark
3246          (if (or (null gnus-summary-default-score)
3247                  (<= (abs (- score gnus-summary-default-score))
3248                      gnus-summary-zcore-fuzz))
3249              ?                          ;Whitespace
3250            (if (< score gnus-summary-default-score)
3251                gnus-score-below-mark gnus-score-over-mark))
3252          'score))
3253       ;; Do visual highlighting.
3254       (when (gnus-visual-p 'summary-highlight 'highlight)
3255         (gnus-run-hooks 'gnus-summary-update-hook)))))
3256
3257 (defvar gnus-tmp-new-adopts nil)
3258
3259 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3260   "Return the number of articles in THREAD.
3261 This may be 0 in some cases -- if none of the articles in
3262 the thread are to be displayed."
3263   (let* ((number
3264          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3265           (cond
3266            ((not (listp thread))
3267             1)
3268            ((and (consp thread) (cdr thread))
3269             (apply
3270              '+ 1 (mapcar
3271                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3272            ((null thread)
3273             1)
3274            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3275             1)
3276            (t 0))))
3277     (when (and level (zerop level) gnus-tmp-new-adopts)
3278       (incf number
3279             (apply '+ (mapcar
3280                        'gnus-summary-number-of-articles-in-thread
3281                        gnus-tmp-new-adopts))))
3282     (if char
3283         (if (> number 1) gnus-not-empty-thread-mark
3284           gnus-empty-thread-mark)
3285       number)))
3286
3287 (defsubst gnus-summary-line-message-size (head)
3288   "Return pretty-printed version of message size.
3289 This function is intended to be used in
3290 `gnus-summary-line-format-alist', which see."
3291   (let ((c (or (mail-header-chars head) -1)))
3292     (cond ((< c 0) "n/a")               ; chars not available
3293           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3294           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3295           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3296           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3297
3298
3299 (defun gnus-summary-set-local-parameters (group)
3300   "Go through the local params of GROUP and set all variable specs in that list."
3301   (let ((params (gnus-group-find-parameter group))
3302         (vars '(quit-config))           ; Ignore quit-config.
3303         elem)
3304     (while params
3305       (setq elem (car params)
3306             params (cdr params))
3307       (and (consp elem)                 ; Has to be a cons.
3308            (consp (cdr elem))           ; The cdr has to be a list.
3309            (symbolp (car elem))         ; Has to be a symbol in there.
3310            (not (memq (car elem) vars))
3311            (ignore-errors               ; So we set it.
3312              (push (car elem) vars)
3313              (make-local-variable (car elem))
3314              (set (car elem) (eval (nth 1 elem))))))))
3315
3316 (defun gnus-summary-read-group (group &optional show-all no-article
3317                                       kill-buffer no-display backward
3318                                       select-articles)
3319   "Start reading news in newsgroup GROUP.
3320 If SHOW-ALL is non-nil, already read articles are also listed.
3321 If NO-ARTICLE is non-nil, no article is selected initially.
3322 If NO-DISPLAY, don't generate a summary buffer."
3323   (let (result)
3324     (while (and group
3325                 (null (setq result
3326                             (let ((gnus-auto-select-next nil))
3327                               (or (gnus-summary-read-group-1
3328                                    group show-all no-article
3329                                    kill-buffer no-display
3330                                    select-articles)
3331                                   (setq show-all nil
3332                                         select-articles nil)))))
3333                 (eq gnus-auto-select-next 'quietly))
3334       (set-buffer gnus-group-buffer)
3335       ;; The entry function called above goes to the next
3336       ;; group automatically, so we go two groups back
3337       ;; if we are searching for the previous group.
3338       (when backward
3339         (gnus-group-prev-unread-group 2))
3340       (if (not (equal group (gnus-group-group-name)))
3341           (setq group (gnus-group-group-name))
3342         (setq group nil)))
3343     result))
3344
3345 (defun gnus-summary-read-group-1 (group show-all no-article
3346                                         kill-buffer no-display
3347                                         &optional select-articles)
3348   ;; Killed foreign groups can't be entered.
3349   ;;  (when (and (not (gnus-group-native-p group))
3350   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3351   ;;    (error "Dead non-native groups can't be entered"))
3352   (gnus-message 5 "Retrieving newsgroup: %s..."
3353                 (gnus-group-decoded-name group))
3354   (let* ((new-group (gnus-summary-setup-buffer group))
3355          (quit-config (gnus-group-quit-config group))
3356          (did-select (and new-group (gnus-select-newsgroup
3357                                      group show-all select-articles))))
3358     (cond
3359      ;; This summary buffer exists already, so we just select it.
3360      ((not new-group)
3361       (gnus-set-global-variables)
3362       (when kill-buffer
3363         (gnus-kill-or-deaden-summary kill-buffer))
3364       (gnus-configure-windows 'summary 'force)
3365       (gnus-set-mode-line 'summary)
3366       (gnus-summary-position-point)
3367       (message "")
3368       t)
3369      ;; We couldn't select this group.
3370      ((null did-select)
3371       (when (and (eq major-mode 'gnus-summary-mode)
3372                  (not (equal (current-buffer) kill-buffer)))
3373         (kill-buffer (current-buffer))
3374         (if (not quit-config)
3375             (progn
3376               ;; Update the info -- marks might need to be removed,
3377               ;; for instance.
3378               (gnus-summary-update-info)
3379               (set-buffer gnus-group-buffer)
3380               (gnus-group-jump-to-group group)
3381               (gnus-group-next-unread-group 1))
3382           (gnus-handle-ephemeral-exit quit-config)))
3383       (let ((grpinfo (gnus-get-info group)))
3384         (if (null (gnus-info-read grpinfo))
3385             (gnus-message 3 "Group %s contains no messages"
3386                           (gnus-group-decoded-name group))
3387           (gnus-message 3 "Can't select group")))
3388       nil)
3389      ;; The user did a `C-g' while prompting for number of articles,
3390      ;; so we exit this group.
3391      ((eq did-select 'quit)
3392       (and (eq major-mode 'gnus-summary-mode)
3393            (not (equal (current-buffer) kill-buffer))
3394            (kill-buffer (current-buffer)))
3395       (when kill-buffer
3396         (gnus-kill-or-deaden-summary kill-buffer))
3397       (if (not quit-config)
3398           (progn
3399             (set-buffer gnus-group-buffer)
3400             (gnus-group-jump-to-group group)
3401             (gnus-group-next-unread-group 1)
3402             (gnus-configure-windows 'group 'force))
3403         (gnus-handle-ephemeral-exit quit-config))
3404       ;; Finally signal the quit.
3405       (signal 'quit nil))
3406      ;; The group was successfully selected.
3407      (t
3408       (gnus-set-global-variables)
3409       ;; Save the active value in effect when the group was entered.
3410       (setq gnus-newsgroup-active
3411             (gnus-copy-sequence
3412              (gnus-active gnus-newsgroup-name)))
3413       ;; You can change the summary buffer in some way with this hook.
3414       (gnus-run-hooks 'gnus-select-group-hook)
3415       (gnus-update-format-specifications
3416        nil 'summary 'summary-mode 'summary-dummy)
3417       (gnus-update-summary-mark-positions)
3418       ;; Do score processing.
3419       (when gnus-use-scoring
3420         (gnus-possibly-score-headers))
3421       ;; Check whether to fill in the gaps in the threads.
3422       (when gnus-build-sparse-threads
3423         (gnus-build-sparse-threads))
3424       ;; Find the initial limit.
3425       (if gnus-show-threads
3426           (if show-all
3427               (let ((gnus-newsgroup-dormant nil))
3428                 (gnus-summary-initial-limit show-all))
3429             (gnus-summary-initial-limit show-all))
3430         ;; When unthreaded, all articles are always shown.
3431         (setq gnus-newsgroup-limit
3432               (mapcar
3433                (lambda (header) (mail-header-number header))
3434                gnus-newsgroup-headers)))
3435       ;; Generate the summary buffer.
3436       (unless no-display
3437         (gnus-summary-prepare))
3438       (when gnus-use-trees
3439         (gnus-tree-open group)
3440         (setq gnus-summary-highlight-line-function
3441               'gnus-tree-highlight-article))
3442       ;; If the summary buffer is empty, but there are some low-scored
3443       ;; articles or some excluded dormants, we include these in the
3444       ;; buffer.
3445       (when (and (zerop (buffer-size))
3446                  (not no-display))
3447         (cond (gnus-newsgroup-dormant
3448                (gnus-summary-limit-include-dormant))
3449               ((and gnus-newsgroup-scored show-all)
3450                (gnus-summary-limit-include-expunged t))))
3451       ;; Function `gnus-apply-kill-file' must be called in this hook.
3452       (gnus-run-hooks 'gnus-apply-kill-hook)
3453       (if (and (zerop (buffer-size))
3454                (not no-display))
3455           (progn
3456             ;; This newsgroup is empty.
3457             (gnus-summary-catchup-and-exit nil t)
3458             (gnus-message 6 "No unread news")
3459             (when kill-buffer
3460               (gnus-kill-or-deaden-summary kill-buffer))
3461             ;; Return nil from this function.
3462             nil)
3463         ;; Hide conversation thread subtrees.  We cannot do this in
3464         ;; gnus-summary-prepare-hook since kill processing may not
3465         ;; work with hidden articles.
3466         (gnus-summary-maybe-hide-threads)
3467         (when kill-buffer
3468           (gnus-kill-or-deaden-summary kill-buffer))
3469         (gnus-summary-auto-select-subject)
3470         ;; Show first unread article if requested.
3471         (if (and (not no-article)
3472                  (not no-display)
3473                  gnus-newsgroup-unreads
3474                  gnus-auto-select-first)
3475             (progn
3476               (gnus-configure-windows 'summary)
3477               (let ((art (gnus-summary-article-number)))
3478                 (unless (or (memq art gnus-newsgroup-undownloaded)
3479                             (memq art gnus-newsgroup-downloadable))
3480                   (gnus-summary-goto-article art))))
3481           ;; Don't select any articles.
3482           (gnus-summary-position-point)
3483           (gnus-configure-windows 'summary 'force)
3484           (gnus-set-mode-line 'summary))
3485         (when (get-buffer-window gnus-group-buffer t)
3486           ;; Gotta use windows, because recenter does weird stuff if
3487           ;; the current buffer ain't the displayed window.
3488           (let ((owin (selected-window)))
3489             (select-window (get-buffer-window gnus-group-buffer t))
3490             (when (gnus-group-goto-group group)
3491               (recenter))
3492             (select-window owin)))
3493         ;; Mark this buffer as "prepared".
3494         (setq gnus-newsgroup-prepared t)
3495         (gnus-run-hooks 'gnus-summary-prepared-hook)
3496         t)))))
3497
3498 (defun gnus-summary-auto-select-subject ()
3499   "Select the subject line on initial group entry."
3500   (goto-char (point-min))
3501   (cond
3502    ((eq gnus-auto-select-subject 'best)
3503     (gnus-summary-best-unread-subject))
3504    ((eq gnus-auto-select-subject 'unread)
3505     (gnus-summary-first-unread-subject))
3506    ((eq gnus-auto-select-subject 'unseen)
3507     (gnus-summary-first-unseen-subject))
3508    ((eq gnus-auto-select-subject 'unseen-or-unread)
3509     (gnus-summary-first-unseen-or-unread-subject))
3510    ((eq gnus-auto-select-subject 'first)
3511     ;; Do nothing.
3512     )
3513    ((gnus-functionp gnus-auto-select-subject)
3514     (funcall gnus-auto-select-subject))))
3515
3516 (defun gnus-summary-prepare ()
3517   "Generate the summary buffer."
3518   (interactive)
3519   (let ((buffer-read-only nil))
3520     (erase-buffer)
3521     (setq gnus-newsgroup-data nil
3522           gnus-newsgroup-data-reverse nil)
3523     (gnus-run-hooks 'gnus-summary-generate-hook)
3524     ;; Generate the buffer, either with threads or without.
3525     (when gnus-newsgroup-headers
3526       (gnus-summary-prepare-threads
3527        (if gnus-show-threads
3528            (gnus-sort-gathered-threads
3529             (funcall gnus-summary-thread-gathering-function
3530                      (gnus-sort-threads
3531                       (gnus-cut-threads (gnus-make-threads)))))
3532          ;; Unthreaded display.
3533          (gnus-sort-articles gnus-newsgroup-headers))))
3534     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3535     ;; Call hooks for modifying summary buffer.
3536     (goto-char (point-min))
3537     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3538
3539 (defsubst gnus-general-simplify-subject (subject)
3540   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3541   (setq subject
3542         (cond
3543          ;; Truncate the subject.
3544          (gnus-simplify-subject-functions
3545           (gnus-map-function gnus-simplify-subject-functions subject))
3546          ((numberp gnus-summary-gather-subject-limit)
3547           (setq subject (gnus-simplify-subject-re subject))
3548           (if (> (length subject) gnus-summary-gather-subject-limit)
3549               (substring subject 0 gnus-summary-gather-subject-limit)
3550             subject))
3551          ;; Fuzzily simplify it.
3552          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3553           (gnus-simplify-subject-fuzzy subject))
3554          ;; Just remove the leading "Re:".
3555          (t
3556           (gnus-simplify-subject-re subject))))
3557
3558   (if (and gnus-summary-gather-exclude-subject
3559            (string-match gnus-summary-gather-exclude-subject subject))
3560       nil                         ; This article shouldn't be gathered
3561     subject))
3562
3563 (defun gnus-summary-simplify-subject-query ()
3564   "Query where the respool algorithm would put this article."
3565   (interactive)
3566   (gnus-summary-select-article)
3567   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3568
3569 (defun gnus-gather-threads-by-subject (threads)
3570   "Gather threads by looking at Subject headers."
3571   (if (not gnus-summary-make-false-root)
3572       threads
3573     (let ((hashtb (gnus-make-hashtable 1024))
3574           (prev threads)
3575           (result threads)
3576           subject hthread whole-subject)
3577       (while threads
3578         (setq subject (gnus-general-simplify-subject
3579                        (setq whole-subject (mail-header-subject
3580                                             (caar threads)))))
3581         (when subject
3582           (if (setq hthread (gnus-gethash subject hashtb))
3583               (progn
3584                 ;; We enter a dummy root into the thread, if we
3585                 ;; haven't done that already.
3586                 (unless (stringp (caar hthread))
3587                   (setcar hthread (list whole-subject (car hthread))))
3588                 ;; We add this new gathered thread to this gathered
3589                 ;; thread.
3590                 (setcdr (car hthread)
3591                         (nconc (cdar hthread) (list (car threads))))
3592                 ;; Remove it from the list of threads.
3593                 (setcdr prev (cdr threads))
3594                 (setq threads prev))
3595             ;; Enter this thread into the hash table.
3596             (gnus-sethash subject
3597                           (if gnus-summary-make-false-root-always
3598                               (progn
3599                                 ;; If you want a dummy root above all
3600                                 ;; threads...
3601                                 (setcar threads (list whole-subject
3602                                                       (car threads)))
3603                                 threads)
3604                             threads)
3605                           hashtb)))
3606         (setq prev threads)
3607         (setq threads (cdr threads)))
3608       result)))
3609
3610 (defun gnus-gather-threads-by-references (threads)
3611   "Gather threads by looking at References headers."
3612   (let ((idhashtb (gnus-make-hashtable 1024))
3613         (thhashtb (gnus-make-hashtable 1024))
3614         (prev threads)
3615         (result threads)
3616         ids references id gthread gid entered ref)
3617     (while threads
3618       (when (setq references (mail-header-references (caar threads)))
3619         (setq id (mail-header-id (caar threads))
3620               ids (inline (gnus-split-references references))
3621               entered nil)
3622         (while (setq ref (pop ids))
3623           (setq ids (delete ref ids))
3624           (if (not (setq gid (gnus-gethash ref idhashtb)))
3625               (progn
3626                 (gnus-sethash ref id idhashtb)
3627                 (gnus-sethash id threads thhashtb))
3628             (setq gthread (gnus-gethash gid thhashtb))
3629             (unless entered
3630               ;; We enter a dummy root into the thread, if we
3631               ;; haven't done that already.
3632               (unless (stringp (caar gthread))
3633                 (setcar gthread (list (mail-header-subject (caar gthread))
3634                                       (car gthread))))
3635               ;; We add this new gathered thread to this gathered
3636               ;; thread.
3637               (setcdr (car gthread)
3638                       (nconc (cdar gthread) (list (car threads)))))
3639             ;; Add it into the thread hash table.
3640             (gnus-sethash id gthread thhashtb)
3641             (setq entered t)
3642             ;; Remove it from the list of threads.
3643             (setcdr prev (cdr threads))
3644             (setq threads prev))))
3645       (setq prev threads)
3646       (setq threads (cdr threads)))
3647     result))
3648
3649 (defun gnus-sort-gathered-threads (threads)
3650   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3651   (let ((result threads))
3652     (while threads
3653       (when (stringp (caar threads))
3654         (setcdr (car threads)
3655                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3656       (setq threads (cdr threads)))
3657     result))
3658
3659 (defun gnus-thread-loop-p (root thread)
3660   "Say whether ROOT is in THREAD."
3661   (let ((stack (list thread))
3662         (infloop 0)
3663         th)
3664     (while (setq thread (pop stack))
3665       (setq th (cdr thread))
3666       (while (and th
3667                   (not (eq (caar th) root)))
3668         (pop th))
3669       (if th
3670           ;; We have found a loop.
3671           (let (ref-dep)
3672             (setcdr thread (delq (car th) (cdr thread)))
3673             (if (boundp (setq ref-dep (intern "none"
3674                                               gnus-newsgroup-dependencies)))
3675                 (setcdr (symbol-value ref-dep)
3676                         (nconc (cdr (symbol-value ref-dep))
3677                                (list (car th))))
3678               (set ref-dep (list nil (car th))))
3679             (setq infloop 1
3680                   stack nil))
3681         ;; Push all the subthreads onto the stack.
3682         (push (cdr thread) stack)))
3683     infloop))
3684
3685 (defun gnus-make-threads ()
3686   "Go through the dependency hashtb and find the roots.  Return all threads."
3687   (let (threads)
3688     (while (catch 'infloop
3689              (mapatoms
3690               (lambda (refs)
3691                 ;; Deal with self-referencing References loops.
3692                 (when (and (car (symbol-value refs))
3693                            (not (zerop
3694                                  (apply
3695                                   '+
3696                                   (mapcar
3697                                    (lambda (thread)
3698                                      (gnus-thread-loop-p
3699                                       (car (symbol-value refs)) thread))
3700                                    (cdr (symbol-value refs)))))))
3701                   (setq threads nil)
3702                   (throw 'infloop t))
3703                 (unless (car (symbol-value refs))
3704                   ;; These threads do not refer back to any other
3705                   ;; articles, so they're roots.
3706                   (setq threads (append (cdr (symbol-value refs)) threads))))
3707               gnus-newsgroup-dependencies)))
3708     threads))
3709
3710 ;; Build the thread tree.
3711 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3712   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3713
3714 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3715 if it was already present.
3716
3717 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3718 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3719 Message-IDs will be renamed to a unique Message-ID before being
3720 entered.
3721
3722 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3723   (let* ((id (mail-header-id header))
3724          (id-dep (and id (intern id dependencies)))
3725          parent-id ref ref-dep ref-header replaced)
3726     ;; Enter this `header' in the `dependencies' table.
3727     (cond
3728      ((not id-dep)
3729       (setq header nil))
3730      ;; The first two cases do the normal part: enter a new `header'
3731      ;; in the `dependencies' table.
3732      ((not (boundp id-dep))
3733       (set id-dep (list header)))
3734      ((null (car (symbol-value id-dep)))
3735       (setcar (symbol-value id-dep) header))
3736
3737      ;; From here the `header' was already present in the
3738      ;; `dependencies' table.
3739      (force-new
3740       ;; Overrides an existing entry;
3741       ;; just set the header part of the entry.
3742       (setcar (symbol-value id-dep) header)
3743       (setq replaced t))
3744
3745      ;; Renames the existing `header' to a unique Message-ID.
3746      ((not gnus-summary-ignore-duplicates)
3747       ;; An article with this Message-ID has already been seen.
3748       ;; We rename the Message-ID.
3749       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3750            (list header))
3751       (mail-header-set-id header id))
3752
3753      ;; The last case ignores an existing entry, except it adds any
3754      ;; additional Xrefs (in case the two articles came from different
3755      ;; servers.
3756      ;; Also sets `header' to `nil' meaning that the `dependencies'
3757      ;; table was *not* modified.
3758      (t
3759       (mail-header-set-xref
3760        (car (symbol-value id-dep))
3761        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3762                    "")
3763                (or (mail-header-xref header) "")))
3764       (setq header nil)))
3765
3766     (when (and header (not replaced))
3767       ;; First check that we are not creating a References loop.
3768       (setq parent-id (gnus-parent-id (mail-header-references header)))
3769       (setq ref parent-id)
3770       (while (and ref
3771                   (setq ref-dep (intern-soft ref dependencies))
3772                   (boundp ref-dep)
3773                   (setq ref-header (car (symbol-value ref-dep))))
3774         (if (string= id ref)
3775             ;; Yuk!  This is a reference loop.  Make the article be a
3776             ;; root article.
3777             (progn
3778               (mail-header-set-references (car (symbol-value id-dep)) "none")
3779               (setq ref nil)
3780               (setq parent-id nil))
3781           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3782       (setq ref-dep (intern (or parent-id "none") dependencies))
3783       (if (boundp ref-dep)
3784           (setcdr (symbol-value ref-dep)
3785                   (nconc (cdr (symbol-value ref-dep))
3786                          (list (symbol-value id-dep))))
3787         (set ref-dep (list nil (symbol-value id-dep)))))
3788     header))
3789
3790 (defun gnus-extract-message-id-from-in-reply-to (string)
3791   (if (string-match "<[^>]+>" string)
3792       (substring string (match-beginning 0) (match-end 0))
3793     nil))
3794
3795 (defun gnus-build-sparse-threads ()
3796   (let ((headers gnus-newsgroup-headers)
3797         (mail-parse-charset gnus-newsgroup-charset)
3798         (gnus-summary-ignore-duplicates t)
3799         header references generation relations
3800         subject child end new-child date)
3801     ;; First we create an alist of generations/relations, where
3802     ;; generations is how much we trust the relation, and the relation
3803     ;; is parent/child.
3804     (gnus-message 7 "Making sparse threads...")
3805     (save-excursion
3806       (nnheader-set-temp-buffer " *gnus sparse threads*")
3807       (while (setq header (pop headers))
3808         (when (and (setq references (mail-header-references header))
3809                    (not (string= references "")))
3810           (insert references)
3811           (setq child (mail-header-id header)
3812                 subject (mail-header-subject header)
3813                 date (mail-header-date header)
3814                 generation 0)
3815           (while (search-backward ">" nil t)
3816             (setq end (1+ (point)))
3817             (when (search-backward "<" nil t)
3818               (setq new-child (buffer-substring (point) end))
3819               (push (list (incf generation)
3820                           child (setq child new-child)
3821                           subject date)
3822                     relations)))
3823           (when child
3824             (push (list (1+ generation) child nil subject) relations))
3825           (erase-buffer)))
3826       (kill-buffer (current-buffer)))
3827     ;; Sort over trustworthiness.
3828     (mapcar
3829      (lambda (relation)
3830        (when (gnus-dependencies-add-header
3831               (make-full-mail-header
3832                gnus-reffed-article-number
3833                (nth 3 relation) "" (or (nth 4 relation) "")
3834                (nth 1 relation)
3835                (or (nth 2 relation) "") 0 0 "")
3836               gnus-newsgroup-dependencies nil)
3837          (push gnus-reffed-article-number gnus-newsgroup-limit)
3838          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3839          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3840                gnus-newsgroup-reads)
3841          (decf gnus-reffed-article-number)))
3842      (sort relations 'car-less-than-car))
3843     (gnus-message 7 "Making sparse threads...done")))
3844
3845 (defun gnus-build-old-threads ()
3846   ;; Look at all the articles that refer back to old articles, and
3847   ;; fetch the headers for the articles that aren't there.  This will
3848   ;; build complete threads - if the roots haven't been expired by the
3849   ;; server, that is.
3850   (let ((mail-parse-charset gnus-newsgroup-charset)
3851         id heads)
3852     (mapatoms
3853      (lambda (refs)
3854        (when (not (car (symbol-value refs)))
3855          (setq heads (cdr (symbol-value refs)))
3856          (while heads
3857            (if (memq (mail-header-number (caar heads))
3858                      gnus-newsgroup-dormant)
3859                (setq heads (cdr heads))
3860              (setq id (symbol-name refs))
3861              (while (and (setq id (gnus-build-get-header id))
3862                          (not (car (gnus-id-to-thread id)))))
3863              (setq heads nil)))))
3864      gnus-newsgroup-dependencies)))
3865
3866 ;; This function has to be called with point after the article number
3867 ;; on the beginning of the line.
3868 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3869   (let ((eol (gnus-point-at-eol))
3870         (buffer (current-buffer))
3871         header references in-reply-to)
3872
3873     ;; overview: [num subject from date id refs chars lines misc]
3874     (unwind-protect
3875         (let (x)
3876           (narrow-to-region (point) eol)
3877           (unless (eobp)
3878             (forward-char))
3879
3880           (setq header
3881                 (make-full-mail-header
3882                  number                 ; number
3883                  (condition-case ()     ; subject
3884                      (funcall gnus-decode-encoded-word-function
3885                               (setq x (nnheader-nov-field)))
3886                    (error x))
3887                  (condition-case ()     ; from
3888                      (funcall gnus-decode-encoded-word-function
3889                               (setq x (nnheader-nov-field)))
3890                    (error x))
3891                  (nnheader-nov-field)   ; date
3892                  (nnheader-nov-read-message-id) ; id
3893                  (setq references (nnheader-nov-field)) ; refs
3894                  (nnheader-nov-read-integer) ; chars
3895                  (nnheader-nov-read-integer) ; lines
3896                  (unless (eobp)
3897                    (if (looking-at "Xref: ")
3898                        (goto-char (match-end 0)))
3899                    (nnheader-nov-field)) ; Xref
3900                  (nnheader-nov-parse-extra)))) ; extra
3901
3902       (widen))
3903
3904     (when (and (string= references "")
3905                (setq in-reply-to (mail-header-extra header))
3906                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3907       (mail-header-set-references
3908        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3909
3910     (when gnus-alter-header-function
3911       (funcall gnus-alter-header-function header))
3912     (gnus-dependencies-add-header header dependencies force-new)))
3913
3914 (defun gnus-build-get-header (id)
3915   "Look through the buffer of NOV lines and find the header to ID.
3916 Enter this line into the dependencies hash table, and return
3917 the id of the parent article (if any)."
3918   (let ((deps gnus-newsgroup-dependencies)
3919         found header)
3920     (prog1
3921         (save-excursion
3922           (set-buffer nntp-server-buffer)
3923           (let ((case-fold-search nil))
3924             (goto-char (point-min))
3925             (while (and (not found)
3926                         (search-forward id nil t))
3927               (beginning-of-line)
3928               (setq found (looking-at
3929                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3930                                    (regexp-quote id))))
3931               (or found (beginning-of-line 2)))
3932             (when found
3933               (beginning-of-line)
3934               (and
3935                (setq header (gnus-nov-parse-line
3936                              (read (current-buffer)) deps))
3937                (gnus-parent-id (mail-header-references header))))))
3938       (when header
3939         (let ((number (mail-header-number header)))
3940           (push number gnus-newsgroup-limit)
3941           (push header gnus-newsgroup-headers)
3942           (if (memq number gnus-newsgroup-unselected)
3943               (progn
3944                 (setq gnus-newsgroup-unreads
3945                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3946                                                number))
3947                 (setq gnus-newsgroup-unselected
3948                       (delq number gnus-newsgroup-unselected)))
3949             (push number gnus-newsgroup-ancient)))))))
3950
3951 (defun gnus-build-all-threads ()
3952   "Read all the headers."
3953   (let ((gnus-summary-ignore-duplicates t)
3954         (mail-parse-charset gnus-newsgroup-charset)
3955         (dependencies gnus-newsgroup-dependencies)
3956         header article)
3957     (save-excursion
3958       (set-buffer nntp-server-buffer)
3959       (let ((case-fold-search nil))
3960         (goto-char (point-min))
3961         (while (not (eobp))
3962           (ignore-errors
3963             (setq article (read (current-buffer))
3964                   header (gnus-nov-parse-line article dependencies)))
3965           (when header
3966             (save-excursion
3967               (set-buffer gnus-summary-buffer)
3968               (push header gnus-newsgroup-headers)
3969               (if (memq (setq article (mail-header-number header))
3970                         gnus-newsgroup-unselected)
3971                   (progn
3972                     (setq gnus-newsgroup-unreads
3973                           (gnus-add-to-sorted-list
3974                            gnus-newsgroup-unreads article))
3975                     (setq gnus-newsgroup-unselected
3976                           (delq article gnus-newsgroup-unselected)))
3977                 (push article gnus-newsgroup-ancient)))
3978             (forward-line 1)))))))
3979
3980 (defun gnus-summary-update-article-line (article header)
3981   "Update the line for ARTICLE using HEADERS."
3982   (let* ((id (mail-header-id header))
3983          (thread (gnus-id-to-thread id)))
3984     (unless thread
3985       (error "Article in no thread"))
3986     ;; Update the thread.
3987     (setcar thread header)
3988     (gnus-summary-goto-subject article)
3989     (let* ((datal (gnus-data-find-list article))
3990            (data (car datal))
3991            (length (when (cdr datal)
3992                      (- (gnus-data-pos data)
3993                         (gnus-data-pos (cadr datal)))))
3994            (buffer-read-only nil)
3995            (level (gnus-summary-thread-level)))
3996       (gnus-delete-line)
3997       (gnus-summary-insert-line
3998        header level nil 
3999        (memq article gnus-newsgroup-undownloaded)
4000        (gnus-article-mark article)
4001        (memq article gnus-newsgroup-replied)
4002        (memq article gnus-newsgroup-expirable)
4003        ;; Only insert the Subject string when it's different
4004        ;; from the previous Subject string.
4005        (if (and
4006             gnus-show-threads
4007             (gnus-subject-equal
4008              (condition-case ()
4009                  (mail-header-subject
4010                   (gnus-data-header
4011                    (cadr
4012                     (gnus-data-find-list
4013                      article
4014                      (gnus-data-list t)))))
4015                ;; Error on the side of excessive subjects.
4016                (error ""))
4017              (mail-header-subject header)))
4018            ""
4019          (mail-header-subject header))
4020        nil (cdr (assq article gnus-newsgroup-scored))
4021        (memq article gnus-newsgroup-processable))
4022       (when length
4023         (gnus-data-update-list
4024          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4025
4026 (defun gnus-summary-update-article (article &optional iheader)
4027   "Update ARTICLE in the summary buffer."
4028   (set-buffer gnus-summary-buffer)
4029   (let* ((header (gnus-summary-article-header article))
4030          (id (mail-header-id header))
4031          (data (gnus-data-find article))
4032          (thread (gnus-id-to-thread id))
4033          (references (mail-header-references header))
4034          (parent
4035           (gnus-id-to-thread
4036            (or (gnus-parent-id
4037                 (when (and references
4038                            (not (equal "" references)))
4039                   references))
4040                "none")))
4041          (buffer-read-only nil)
4042          (old (car thread)))
4043     (when thread
4044       (unless iheader
4045         (setcar thread nil)
4046         (when parent
4047           (delq thread parent)))
4048       (if (gnus-summary-insert-subject id header)
4049           ;; Set the (possibly) new article number in the data structure.
4050           (gnus-data-set-number data (gnus-id-to-article id))
4051         (setcar thread old)
4052         nil))))
4053
4054 (defun gnus-rebuild-thread (id &optional line)
4055   "Rebuild the thread containing ID.
4056 If LINE, insert the rebuilt thread starting on line LINE."
4057   (let ((buffer-read-only nil)
4058         old-pos current thread data)
4059     (if (not gnus-show-threads)
4060         (setq thread (list (car (gnus-id-to-thread id))))
4061       ;; Get the thread this article is part of.
4062       (setq thread (gnus-remove-thread id)))
4063     (setq old-pos (gnus-point-at-bol))
4064     (setq current (save-excursion
4065                     (and (re-search-backward "[\r\n]" nil t)
4066                          (gnus-summary-article-number))))
4067     ;; If this is a gathered thread, we have to go some re-gathering.
4068     (when (stringp (car thread))
4069       (let ((subject (car thread))
4070             roots thr)
4071         (setq thread (cdr thread))
4072         (while thread
4073           (unless (memq (setq thr (gnus-id-to-thread
4074                                    (gnus-root-id
4075                                     (mail-header-id (caar thread)))))
4076                         roots)
4077             (push thr roots))
4078           (setq thread (cdr thread)))
4079         ;; We now have all (unique) roots.
4080         (if (= (length roots) 1)
4081             ;; All the loose roots are now one solid root.
4082             (setq thread (car roots))
4083           (setq thread (cons subject (gnus-sort-threads roots))))))
4084     (let (threads)
4085       ;; We then insert this thread into the summary buffer.
4086       (when line
4087         (goto-char (point-min))
4088         (forward-line (1- line)))
4089       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4090         (if gnus-show-threads
4091             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4092           (gnus-summary-prepare-unthreaded thread))
4093         (setq data (nreverse gnus-newsgroup-data))
4094         (setq threads gnus-newsgroup-threads))
4095       ;; We splice the new data into the data structure.
4096       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4097       ;;!!! then we want to insert at the beginning of the buffer.
4098       ;;!!! That happens to be true with Gnus now, but that may
4099       ;;!!! change in the future.  Perhaps.
4100       (gnus-data-enter-list
4101        (if line nil current) data (- (point) old-pos))
4102       (setq gnus-newsgroup-threads
4103             (nconc threads gnus-newsgroup-threads))
4104       (gnus-data-compute-positions))))
4105
4106 (defun gnus-number-to-header (number)
4107   "Return the header for article NUMBER."
4108   (let ((headers gnus-newsgroup-headers))
4109     (while (and headers
4110                 (not (= number (mail-header-number (car headers)))))
4111       (pop headers))
4112     (when headers
4113       (car headers))))
4114
4115 (defun gnus-parent-headers (in-headers &optional generation)
4116   "Return the headers of the GENERATIONeth parent of HEADERS."
4117   (unless generation
4118     (setq generation 1))
4119   (let ((parent t)
4120         (headers in-headers)
4121         references)
4122     (while (and parent
4123                 (not (zerop generation))
4124                 (setq references (mail-header-references headers)))
4125       (setq headers (if (and references
4126                              (setq parent (gnus-parent-id references)))
4127                         (car (gnus-id-to-thread parent))
4128                       nil))
4129       (decf generation))
4130     (and (not (eq headers in-headers))
4131          headers)))
4132
4133 (defun gnus-id-to-thread (id)
4134   "Return the (sub-)thread where ID appears."
4135   (gnus-gethash id gnus-newsgroup-dependencies))
4136
4137 (defun gnus-id-to-article (id)
4138   "Return the article number of ID."
4139   (let ((thread (gnus-id-to-thread id)))
4140     (when (and thread
4141                (car thread))
4142       (mail-header-number (car thread)))))
4143
4144 (defun gnus-id-to-header (id)
4145   "Return the article headers of ID."
4146   (car (gnus-id-to-thread id)))
4147
4148 (defun gnus-article-displayed-root-p (article)
4149   "Say whether ARTICLE is a root(ish) article."
4150   (let ((level (gnus-summary-thread-level article))
4151         (refs (mail-header-references  (gnus-summary-article-header article)))
4152         particle)
4153     (cond
4154      ((null level) nil)
4155      ((zerop level) t)
4156      ((null refs) t)
4157      ((null (gnus-parent-id refs)) t)
4158      ((and (= 1 level)
4159            (null (setq particle (gnus-id-to-article
4160                                  (gnus-parent-id refs))))
4161            (null (gnus-summary-thread-level particle)))))))
4162
4163 (defun gnus-root-id (id)
4164   "Return the id of the root of the thread where ID appears."
4165   (let (last-id prev)
4166     (while (and id (setq prev (car (gnus-id-to-thread id))))
4167       (setq last-id id
4168             id (gnus-parent-id (mail-header-references prev))))
4169     last-id))
4170
4171 (defun gnus-articles-in-thread (thread)
4172   "Return the list of articles in THREAD."
4173   (cons (mail-header-number (car thread))
4174         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4175
4176 (defun gnus-remove-thread (id &optional dont-remove)
4177   "Remove the thread that has ID in it."
4178   (let (headers thread last-id)
4179     ;; First go up in this thread until we find the root.
4180     (setq last-id (gnus-root-id id)
4181           headers (message-flatten-list (gnus-id-to-thread last-id)))
4182     ;; We have now found the real root of this thread.  It might have
4183     ;; been gathered into some loose thread, so we have to search
4184     ;; through the threads to find the thread we wanted.
4185     (let ((threads gnus-newsgroup-threads)
4186           sub)
4187       (while threads
4188         (setq sub (car threads))
4189         (if (stringp (car sub))
4190             ;; This is a gathered thread, so we look at the roots
4191             ;; below it to find whether this article is in this
4192             ;; gathered root.
4193             (progn
4194               (setq sub (cdr sub))
4195               (while sub
4196                 (when (member (caar sub) headers)
4197                   (setq thread (car threads)
4198                         threads nil
4199                         sub nil))
4200                 (setq sub (cdr sub))))
4201           ;; It's an ordinary thread, so we check it.
4202           (when (eq (car sub) (car headers))
4203             (setq thread sub
4204                   threads nil)))
4205         (setq threads (cdr threads)))
4206       ;; If this article is in no thread, then it's a root.
4207       (if thread
4208           (unless dont-remove
4209             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4210         (setq thread (gnus-id-to-thread last-id)))
4211       (when thread
4212         (prog1
4213             thread                      ; We return this thread.
4214           (unless dont-remove
4215             (if (stringp (car thread))
4216                 (progn
4217                   ;; If we use dummy roots, then we have to remove the
4218                   ;; dummy root as well.
4219                   (when (eq gnus-summary-make-false-root 'dummy)
4220                     ;; We go to the dummy root by going to
4221                     ;; the first sub-"thread", and then one line up.
4222                     (gnus-summary-goto-article
4223                      (mail-header-number (caadr thread)))
4224                     (forward-line -1)
4225                     (gnus-delete-line)
4226                     (gnus-data-compute-positions))
4227                   (setq thread (cdr thread))
4228                   (while thread
4229                     (gnus-remove-thread-1 (car thread))
4230                     (setq thread (cdr thread))))
4231               (gnus-remove-thread-1 thread))))))))
4232
4233 (defun gnus-remove-thread-1 (thread)
4234   "Remove the thread THREAD recursively."
4235   (let ((number (mail-header-number (pop thread)))
4236         d)
4237     (setq thread (reverse thread))
4238     (while thread
4239       (gnus-remove-thread-1 (pop thread)))
4240     (when (setq d (gnus-data-find number))
4241       (goto-char (gnus-data-pos d))
4242       (gnus-summary-show-thread)
4243       (gnus-data-remove
4244        number
4245        (- (gnus-point-at-bol)
4246           (prog1
4247               (1+ (gnus-point-at-eol))
4248             (gnus-delete-line)))))))
4249
4250 (defun gnus-sort-threads-1 (threads func)
4251   (sort (mapcar (lambda (thread)
4252                   (cons (car thread)
4253                         (and (cdr thread)
4254                              (gnus-sort-threads-1 (cdr thread) func))))
4255                 threads) func))
4256
4257 (defun gnus-sort-threads (threads)
4258   "Sort THREADS."
4259   (if (not gnus-thread-sort-functions)
4260       threads
4261     (gnus-message 8 "Sorting threads...")
4262     (let ((max-lisp-eval-depth 5000))
4263       (prog1 (gnus-sort-threads-1
4264          threads
4265          (gnus-make-sort-function gnus-thread-sort-functions))
4266         (gnus-message 8 "Sorting threads...done")))))
4267
4268 (defun gnus-sort-articles (articles)
4269   "Sort ARTICLES."
4270   (when gnus-article-sort-functions
4271     (gnus-message 7 "Sorting articles...")
4272     (prog1
4273         (setq gnus-newsgroup-headers
4274               (sort articles (gnus-make-sort-function
4275                               gnus-article-sort-functions)))
4276       (gnus-message 7 "Sorting articles...done"))))
4277
4278 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4279 (defmacro gnus-thread-header (thread)
4280   "Return header of first article in THREAD.
4281 Note that THREAD must never, ever be anything else than a variable -
4282 using some other form will lead to serious barfage."
4283   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4284   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4285   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4286         (vector thread) 2))
4287
4288 (defsubst gnus-article-sort-by-number (h1 h2)
4289   "Sort articles by article number."
4290   (< (mail-header-number h1)
4291      (mail-header-number h2)))
4292
4293 (defun gnus-thread-sort-by-number (h1 h2)
4294   "Sort threads by root article number."
4295   (gnus-article-sort-by-number
4296    (gnus-thread-header h1) (gnus-thread-header h2)))
4297
4298 (defsubst gnus-article-sort-by-random (h1 h2)
4299   "Sort articles by article number."
4300   (zerop (random 2)))
4301
4302 (defun gnus-thread-sort-by-random (h1 h2)
4303   "Sort threads by root article number."
4304   (gnus-article-sort-by-random
4305    (gnus-thread-header h1) (gnus-thread-header h2)))
4306
4307 (defsubst gnus-article-sort-by-lines (h1 h2)
4308   "Sort articles by article Lines header."
4309   (< (mail-header-lines h1)
4310      (mail-header-lines h2)))
4311
4312 (defun gnus-thread-sort-by-lines (h1 h2)
4313   "Sort threads by root article Lines header."
4314   (gnus-article-sort-by-lines
4315    (gnus-thread-header h1) (gnus-thread-header h2)))
4316
4317 (defsubst gnus-article-sort-by-chars (h1 h2)
4318   "Sort articles by octet length."
4319   (< (mail-header-chars h1)
4320      (mail-header-chars h2)))
4321
4322 (defun gnus-thread-sort-by-chars (h1 h2)
4323   "Sort threads by root article octet length."
4324   (gnus-article-sort-by-chars
4325    (gnus-thread-header h1) (gnus-thread-header h2)))
4326
4327 (defsubst gnus-article-sort-by-author (h1 h2)
4328   "Sort articles by root author."
4329   (string-lessp
4330    (let ((extract (funcall
4331                    gnus-extract-address-components
4332                    (mail-header-from h1))))
4333      (or (car extract) (cadr extract) ""))
4334    (let ((extract (funcall
4335                    gnus-extract-address-components
4336                    (mail-header-from h2))))
4337      (or (car extract) (cadr extract) ""))))
4338
4339 (defun gnus-thread-sort-by-author (h1 h2)
4340   "Sort threads by root author."
4341   (gnus-article-sort-by-author
4342    (gnus-thread-header h1)  (gnus-thread-header h2)))
4343
4344 (defsubst gnus-article-sort-by-subject (h1 h2)
4345   "Sort articles by root subject."
4346   (string-lessp
4347    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4348    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4349
4350 (defun gnus-thread-sort-by-subject (h1 h2)
4351   "Sort threads by root subject."
4352   (gnus-article-sort-by-subject
4353    (gnus-thread-header h1) (gnus-thread-header h2)))
4354
4355 (defsubst gnus-article-sort-by-date (h1 h2)
4356   "Sort articles by root article date."
4357   (time-less-p
4358    (gnus-date-get-time (mail-header-date h1))
4359    (gnus-date-get-time (mail-header-date h2))))
4360
4361 (defun gnus-thread-sort-by-date (h1 h2)
4362   "Sort threads by root article date."
4363   (gnus-article-sort-by-date
4364    (gnus-thread-header h1) (gnus-thread-header h2)))
4365
4366 (defsubst gnus-article-sort-by-score (h1 h2)
4367   "Sort articles by root article score.
4368 Unscored articles will be counted as having a score of zero."
4369   (> (or (cdr (assq (mail-header-number h1)
4370                     gnus-newsgroup-scored))
4371          gnus-summary-default-score 0)
4372      (or (cdr (assq (mail-header-number h2)
4373                     gnus-newsgroup-scored))
4374          gnus-summary-default-score 0)))
4375
4376 (defun gnus-thread-sort-by-score (h1 h2)
4377   "Sort threads by root article score."
4378   (gnus-article-sort-by-score
4379    (gnus-thread-header h1) (gnus-thread-header h2)))
4380
4381 (defun gnus-thread-sort-by-total-score (h1 h2)
4382   "Sort threads by the sum of all scores in the thread.
4383 Unscored articles will be counted as having a score of zero."
4384   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4385
4386 (defun gnus-thread-total-score (thread)
4387   ;; This function find the total score of THREAD.
4388   (cond
4389    ((null thread)
4390     0)
4391    ((consp thread)
4392     (if (stringp (car thread))
4393         (apply gnus-thread-score-function 0
4394                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4395       (gnus-thread-total-score-1 thread)))
4396    (t
4397     (gnus-thread-total-score-1 (list thread)))))
4398
4399 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4400   "Sort threads such that the thread with the most recently arrived article comes first."
4401   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4402
4403 (defun gnus-thread-highest-number (thread)
4404   "Return the highest article number in THREAD."
4405   (apply 'max (mapcar (lambda (header)
4406                         (mail-header-number header))
4407                       (message-flatten-list thread))))
4408
4409 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4410   "Sort threads such that the thread with the most recently dated article comes first."
4411   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4412
4413 (defun gnus-thread-latest-date (thread)
4414   "Return the highest article date in THREAD."
4415   (let ((previous-time 0))
4416     (apply 'max
4417            (mapcar
4418             (lambda (header)
4419               (setq previous-time
4420                     (time-to-seconds
4421                      (condition-case ()
4422                          (mail-header-parse-date (mail-header-date header))
4423                        (error previous-time)))))
4424             (sort
4425              (message-flatten-list thread)
4426              (lambda (h1 h2)
4427                (< (mail-header-number h1)
4428                   (mail-header-number h2))))))))
4429
4430 (defun gnus-thread-total-score-1 (root)
4431   ;; This function find the total score of the thread below ROOT.
4432   (setq root (car root))
4433   (apply gnus-thread-score-function
4434          (or (append
4435               (mapcar 'gnus-thread-total-score
4436                       (cdr (gnus-id-to-thread (mail-header-id root))))
4437               (when (> (mail-header-number root) 0)
4438                 (list (or (cdr (assq (mail-header-number root)
4439                                      gnus-newsgroup-scored))
4440                           gnus-summary-default-score 0))))
4441              (list gnus-summary-default-score)
4442              '(0))))
4443
4444 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4445 (defvar gnus-tmp-prev-subject nil)
4446 (defvar gnus-tmp-false-parent nil)
4447 (defvar gnus-tmp-root-expunged nil)
4448 (defvar gnus-tmp-dummy-line nil)
4449
4450 (eval-when-compile (defvar gnus-tmp-header))
4451 (defun gnus-extra-header (type &optional header)
4452   "Return the extra header of TYPE."
4453   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4454       ""))
4455
4456 (defvar gnus-tmp-thread-tree-header-string "")
4457
4458 (defcustom gnus-sum-thread-tree-root "> "
4459   "With %B spec, used for the root of a thread.
4460 If nil, use subject instead."
4461   :type 'string
4462   :group 'gnus-thread)
4463 (defcustom gnus-sum-thread-tree-single-indent ""
4464   "With %B spec, used for a thread with just one message.
4465 If nil, use subject instead."
4466   :type 'string
4467   :group 'gnus-thread)
4468 (defcustom gnus-sum-thread-tree-vertical "| "
4469   "With %B spec, used for drawing a vertical line."
4470   :type 'string
4471   :group 'gnus-thread)
4472 (defcustom gnus-sum-thread-tree-indent "  "
4473   "With %B spec, used for indenting."
4474   :type 'string
4475   :group 'gnus-thread)
4476 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4477   "With %B spec, used for a leaf with brothers."
4478   :type 'string
4479   :group 'gnus-thread)
4480 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4481   "With %B spec, used for a leaf without brothers."
4482   :type 'string
4483   :group 'gnus-thread)
4484
4485 (defun gnus-summary-prepare-threads (threads)
4486   "Prepare summary buffer from THREADS and indentation LEVEL.
4487 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4488 or a straight list of headers."
4489   (gnus-message 7 "Generating summary...")
4490
4491   (setq gnus-newsgroup-threads threads)
4492   (beginning-of-line)
4493
4494   (let ((gnus-tmp-level 0)
4495         (default-score (or gnus-summary-default-score 0))
4496         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4497         (building-line-count gnus-summary-display-while-building)
4498         (building-count (integerp gnus-summary-display-while-building))
4499         thread number subject stack state gnus-tmp-gathered beg-match
4500         new-roots gnus-tmp-new-adopts thread-end simp-subject
4501         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4502         gnus-tmp-replied gnus-tmp-subject-or-nil
4503         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4504         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4505         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4506         tree-stack)
4507
4508     (setq gnus-tmp-prev-subject nil)
4509
4510     (if (vectorp (car threads))
4511         ;; If this is a straight (sic) list of headers, then a
4512         ;; threaded summary display isn't required, so we just create
4513         ;; an unthreaded one.
4514         (gnus-summary-prepare-unthreaded threads)
4515
4516       ;; Do the threaded display.
4517
4518       (if gnus-summary-display-while-building
4519           (switch-to-buffer (buffer-name)))
4520       (while (or threads stack gnus-tmp-new-adopts new-roots)
4521
4522         (if (and (= gnus-tmp-level 0)
4523                  (or (not stack)
4524                      (= (caar stack) 0))
4525                  (not gnus-tmp-false-parent)
4526                  (or gnus-tmp-new-adopts new-roots))
4527             (if gnus-tmp-new-adopts
4528                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4529                       thread (list (car gnus-tmp-new-adopts))
4530                       gnus-tmp-header (caar thread)
4531                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4532               (when new-roots
4533                 (setq thread (list (car new-roots))
4534                       gnus-tmp-header (caar thread)
4535                       new-roots (cdr new-roots))))
4536
4537           (if threads
4538               ;; If there are some threads, we do them before the
4539               ;; threads on the stack.
4540               (setq thread threads
4541                     gnus-tmp-header (caar thread))
4542             ;; There were no current threads, so we pop something off
4543             ;; the stack.
4544             (setq state (car stack)
4545                   gnus-tmp-level (car state)
4546                   tree-stack (cadr state)
4547                   thread (caddr state)
4548                   stack (cdr stack)
4549                   gnus-tmp-header (caar thread))))
4550
4551         (setq gnus-tmp-false-parent nil)
4552         (setq gnus-tmp-root-expunged nil)
4553         (setq thread-end nil)
4554
4555         (if (stringp gnus-tmp-header)
4556             ;; The header is a dummy root.
4557             (cond
4558              ((eq gnus-summary-make-false-root 'adopt)
4559               ;; We let the first article adopt the rest.
4560               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4561                                                (cddar thread)))
4562               (setq gnus-tmp-gathered
4563                     (nconc (mapcar
4564                             (lambda (h) (mail-header-number (car h)))
4565                             (cddar thread))
4566                            gnus-tmp-gathered))
4567               (setq thread (cons (list (caar thread)
4568                                        (cadar thread))
4569                                  (cdr thread)))
4570               (setq gnus-tmp-level -1
4571                     gnus-tmp-false-parent t))
4572              ((eq gnus-summary-make-false-root 'empty)
4573               ;; We print adopted articles with empty subject fields.
4574               (setq gnus-tmp-gathered
4575                     (nconc (mapcar
4576                             (lambda (h) (mail-header-number (car h)))
4577                             (cddar thread))
4578                            gnus-tmp-gathered))
4579               (setq gnus-tmp-level -1))
4580              ((eq gnus-summary-make-false-root 'dummy)
4581               ;; We remember that we probably want to output a dummy
4582               ;; root.
4583               (setq gnus-tmp-dummy-line gnus-tmp-header)
4584               (setq gnus-tmp-prev-subject gnus-tmp-header))
4585              (t
4586               ;; We do not make a root for the gathered
4587               ;; sub-threads at all.
4588               (setq gnus-tmp-level -1)))
4589
4590           (setq number (mail-header-number gnus-tmp-header)
4591                 subject (mail-header-subject gnus-tmp-header)
4592                 simp-subject (gnus-simplify-subject-fully subject))
4593
4594           (cond
4595            ;; If the thread has changed subject, we might want to make
4596            ;; this subthread into a root.
4597            ((and (null gnus-thread-ignore-subject)
4598                  (not (zerop gnus-tmp-level))
4599                  gnus-tmp-prev-subject
4600                  (not (string= gnus-tmp-prev-subject simp-subject)))
4601             (setq new-roots (nconc new-roots (list (car thread)))
4602                   thread-end t
4603                   gnus-tmp-header nil))
4604            ;; If the article lies outside the current limit,
4605            ;; then we do not display it.
4606            ((not (memq number gnus-newsgroup-limit))
4607             (setq gnus-tmp-gathered
4608                   (nconc (mapcar
4609                           (lambda (h) (mail-header-number (car h)))
4610                           (cdar thread))
4611                          gnus-tmp-gathered))
4612             (setq gnus-tmp-new-adopts (if (cdar thread)
4613                                           (append gnus-tmp-new-adopts
4614                                                   (cdar thread))
4615                                         gnus-tmp-new-adopts)
4616                   thread-end t
4617                   gnus-tmp-header nil)
4618             (when (zerop gnus-tmp-level)
4619               (setq gnus-tmp-root-expunged t)))
4620            ;; Perhaps this article is to be marked as read?
4621            ((and gnus-summary-mark-below
4622                  (< (or (cdr (assq number gnus-newsgroup-scored))
4623                         default-score)
4624                     gnus-summary-mark-below)
4625                  ;; Don't touch sparse articles.
4626                  (not (gnus-summary-article-sparse-p number))
4627                  (not (gnus-summary-article-ancient-p number)))
4628             (setq gnus-newsgroup-unreads
4629                   (delq number gnus-newsgroup-unreads))
4630             (if gnus-newsgroup-auto-expire
4631                 (setq gnus-newsgroup-expirable
4632                       (gnus-add-to-sorted-list
4633                        gnus-newsgroup-expirable number))
4634               (push (cons number gnus-low-score-mark)
4635                     gnus-newsgroup-reads))))
4636
4637           (when gnus-tmp-header
4638             ;; We may have an old dummy line to output before this
4639             ;; article.
4640             (when (and gnus-tmp-dummy-line
4641                        (gnus-subject-equal
4642                         gnus-tmp-dummy-line
4643                         (mail-header-subject gnus-tmp-header)))
4644               (gnus-summary-insert-dummy-line
4645                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4646               (setq gnus-tmp-dummy-line nil))
4647
4648             ;; Compute the mark.
4649             (setq gnus-tmp-unread (gnus-article-mark number))
4650
4651             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4652                                   gnus-tmp-header gnus-tmp-level)
4653                   gnus-newsgroup-data)
4654
4655             ;; Actually insert the line.
4656             (setq
4657              gnus-tmp-subject-or-nil
4658              (cond
4659               ((and gnus-thread-ignore-subject
4660                     gnus-tmp-prev-subject
4661                     (not (string= gnus-tmp-prev-subject simp-subject)))
4662                subject)
4663               ((zerop gnus-tmp-level)
4664                (if (and (eq gnus-summary-make-false-root 'empty)
4665                         (memq number gnus-tmp-gathered)
4666                         gnus-tmp-prev-subject
4667                         (string= gnus-tmp-prev-subject simp-subject))
4668                    gnus-summary-same-subject
4669                  subject))
4670               (t gnus-summary-same-subject)))
4671             (if (and (eq gnus-summary-make-false-root 'adopt)
4672                      (= gnus-tmp-level 1)
4673                      (memq number gnus-tmp-gathered))
4674                 (setq gnus-tmp-opening-bracket ?\<
4675                       gnus-tmp-closing-bracket ?\>)
4676               (setq gnus-tmp-opening-bracket ?\[
4677                     gnus-tmp-closing-bracket ?\]))
4678             (setq
4679              gnus-tmp-indentation
4680              (aref gnus-thread-indent-array gnus-tmp-level)
4681              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4682              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4683                                 gnus-summary-default-score 0)
4684              gnus-tmp-score-char
4685              (if (or (null gnus-summary-default-score)
4686                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4687                          gnus-summary-zcore-fuzz))
4688                  ?                      ;Whitespace
4689                (if (< gnus-tmp-score gnus-summary-default-score)
4690                    gnus-score-below-mark gnus-score-over-mark))
4691              gnus-tmp-replied
4692              (cond ((memq number gnus-newsgroup-processable)
4693                     gnus-process-mark)
4694                    ((memq number gnus-newsgroup-cached)
4695                     gnus-cached-mark)
4696                    ((memq number gnus-newsgroup-replied)
4697                     gnus-replied-mark)
4698                    ((memq number gnus-newsgroup-forwarded)
4699                     gnus-forwarded-mark)
4700                    ((memq number gnus-newsgroup-saved)
4701                     gnus-saved-mark)
4702                    ((memq number gnus-newsgroup-recent)
4703                     gnus-recent-mark)
4704                    ((memq number gnus-newsgroup-unseen)
4705                     gnus-unseen-mark)
4706                    (t gnus-no-mark))
4707              gnus-tmp-downloaded
4708              (cond ((memq number gnus-newsgroup-undownloaded) 
4709                     gnus-undownloaded-mark)
4710                    (gnus-newsgroup-agentized
4711                     gnus-downloaded-mark)
4712                    (t
4713                     gnus-no-mark))
4714              gnus-tmp-from (mail-header-from gnus-tmp-header)
4715              gnus-tmp-name
4716              (cond
4717               ((string-match "<[^>]+> *$" gnus-tmp-from)
4718                (setq beg-match (match-beginning 0))
4719                (or (and (string-match "^\".+\"" gnus-tmp-from)
4720                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4721                    (substring gnus-tmp-from 0 beg-match)))
4722               ((string-match "(.+)" gnus-tmp-from)
4723                (substring gnus-tmp-from
4724                           (1+ (match-beginning 0)) (1- (match-end 0))))
4725               (t gnus-tmp-from))
4726              gnus-tmp-thread-tree-header-string
4727              (cond
4728               ((not gnus-show-threads) "")
4729               ((zerop gnus-tmp-level)
4730                (if (cdar thread)
4731                    (or gnus-sum-thread-tree-root subject)
4732                  (or gnus-sum-thread-tree-single-indent subject)))
4733               (t
4734                (concat (apply 'concat
4735                               (mapcar (lambda (item)
4736                                         (if (= item 1)
4737                                             gnus-sum-thread-tree-vertical
4738                                           gnus-sum-thread-tree-indent))
4739                                       (cdr (reverse tree-stack))))
4740                        (if (nth 1 thread)
4741                            gnus-sum-thread-tree-leaf-with-other
4742                          gnus-sum-thread-tree-single-leaf)))))
4743             (when (string= gnus-tmp-name "")
4744               (setq gnus-tmp-name gnus-tmp-from))
4745             (unless (numberp gnus-tmp-lines)
4746               (setq gnus-tmp-lines -1))
4747             (if (= gnus-tmp-lines -1)
4748                 (setq gnus-tmp-lines "?")
4749               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4750             (gnus-put-text-property
4751              (point)
4752              (progn (eval gnus-summary-line-format-spec) (point))
4753              'gnus-number number)
4754             (when gnus-visual-p
4755               (forward-line -1)
4756               (gnus-run-hooks 'gnus-summary-update-hook)
4757               (forward-line 1))
4758
4759             (setq gnus-tmp-prev-subject simp-subject)))
4760
4761         (when (nth 1 thread)
4762           (push (list (max 0 gnus-tmp-level)
4763                       (copy-list tree-stack)
4764                       (nthcdr 1 thread))
4765                 stack))
4766         (push (if (nth 1 thread) 1 0) tree-stack)
4767         (incf gnus-tmp-level)
4768         (setq threads (if thread-end nil (cdar thread)))
4769         (if gnus-summary-display-while-building
4770             (if building-count
4771                 (progn
4772                   ;; use a set frequency
4773                   (setq building-line-count (1- building-line-count))
4774                   (when (= building-line-count 0)
4775                     (sit-for 0)
4776                     (setq building-line-count
4777                           gnus-summary-display-while-building)))
4778               ;; always
4779               (sit-for 0)))
4780         (unless threads
4781           (setq gnus-tmp-level 0)))))
4782   (gnus-message 7 "Generating summary...done"))
4783
4784 (defun gnus-summary-prepare-unthreaded (headers)
4785   "Generate an unthreaded summary buffer based on HEADERS."
4786   (let (header number mark)
4787
4788     (beginning-of-line)
4789
4790     (while headers
4791       ;; We may have to root out some bad articles...
4792       (when (memq (setq number (mail-header-number
4793                                 (setq header (pop headers))))
4794                   gnus-newsgroup-limit)
4795         ;; Mark article as read when it has a low score.
4796         (when (and gnus-summary-mark-below
4797                    (< (or (cdr (assq number gnus-newsgroup-scored))
4798                           gnus-summary-default-score 0)
4799                       gnus-summary-mark-below)
4800                    (not (gnus-summary-article-ancient-p number)))
4801           (setq gnus-newsgroup-unreads
4802                 (delq number gnus-newsgroup-unreads))
4803           (if gnus-newsgroup-auto-expire
4804               (push number gnus-newsgroup-expirable)
4805             (push (cons number gnus-low-score-mark)
4806                   gnus-newsgroup-reads)))
4807
4808         (setq mark (gnus-article-mark number))
4809         (push (gnus-data-make number mark (1+ (point)) header 0)
4810               gnus-newsgroup-data)
4811         (gnus-summary-insert-line
4812          header 0 number
4813          (memq number gnus-newsgroup-undownloaded)
4814          mark (memq number gnus-newsgroup-replied)
4815          (memq number gnus-newsgroup-expirable)
4816          (mail-header-subject header) nil
4817          (cdr (assq number gnus-newsgroup-scored))
4818          (memq number gnus-newsgroup-processable))))))
4819
4820 (defun gnus-summary-remove-list-identifiers ()
4821   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4822   (let ((regexp (if (consp gnus-list-identifiers)
4823                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4824                   gnus-list-identifiers))
4825         changed subject)
4826     (when regexp
4827       (dolist (header gnus-newsgroup-headers)
4828         (setq subject (mail-header-subject header)
4829               changed nil)
4830         (while (string-match
4831                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4832                 subject)
4833           (setq subject
4834                 (concat (substring subject 0 (match-beginning 2))
4835                         (substring subject (match-end 0)))
4836                 changed t))
4837         (when (and changed
4838                    (string-match
4839                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4840           (setq subject
4841                 (concat (substring subject 0 (match-beginning 1))
4842                         (substring subject (match-end 1)))))
4843         (when changed
4844           (mail-header-set-subject header subject))))))
4845
4846 (defun gnus-fetch-headers (articles)
4847   "Fetch headers of ARTICLES."
4848   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4849     (gnus-message 5 "Fetching headers for %s..." name)
4850     (prog1
4851         (if (eq 'nov
4852                 (setq gnus-headers-retrieved-by
4853                       (gnus-retrieve-headers
4854                        articles gnus-newsgroup-name
4855                        ;; We might want to fetch old headers, but
4856                        ;; not if there is only 1 article.
4857                        (and (or (and
4858                                  (not (eq gnus-fetch-old-headers 'some))
4859                                  (not (numberp gnus-fetch-old-headers)))
4860                                 (> (length articles) 1))
4861                             gnus-fetch-old-headers))))
4862             (gnus-get-newsgroup-headers-xover
4863              articles nil nil gnus-newsgroup-name t)
4864           (gnus-get-newsgroup-headers))
4865       (gnus-message 5 "Fetching headers for %s...done" name))))
4866
4867 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4868   "Select newsgroup GROUP.
4869 If READ-ALL is non-nil, all articles in the group are selected.
4870 If SELECT-ARTICLES, only select those articles from GROUP."
4871   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4872          ;;!!! Dirty hack; should be removed.
4873          (gnus-summary-ignore-duplicates
4874           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4875               t
4876             gnus-summary-ignore-duplicates))
4877          (info (nth 2 entry))
4878          articles fetched-articles cached)
4879
4880     (unless (gnus-check-server
4881              (set (make-local-variable 'gnus-current-select-method)
4882                   (gnus-find-method-for-group group)))
4883       (error "Couldn't open server"))
4884
4885     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4886         (gnus-activate-group group)     ; Or we can activate it...
4887         (progn                          ; Or we bug out.
4888           (when (equal major-mode 'gnus-summary-mode)
4889             (kill-buffer (current-buffer)))
4890           (error "Couldn't activate group %s: %s"
4891                  group (gnus-status-message group))))
4892
4893     (unless (gnus-request-group group t)
4894       (when (equal major-mode 'gnus-summary-mode)
4895         (kill-buffer (current-buffer)))
4896       (error "Couldn't request group %s: %s"
4897              group (gnus-status-message group)))
4898
4899     (setq gnus-newsgroup-name group
4900           gnus-newsgroup-unselected nil
4901           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4902
4903     (let ((display (gnus-group-find-parameter group 'display)))
4904       (setq gnus-newsgroup-display
4905             (cond
4906              ((not (zerop (or (car-safe read-all) 0)))
4907               ;; The user entered the group with C-u SPC/RET, let's show
4908               ;; all articles.
4909               'gnus-not-ignore)
4910              ((eq display 'all)
4911               'gnus-not-ignore)
4912              ((arrayp display)
4913               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4914              ((numberp display)
4915               ;; The following is probably the "correct" solution, but
4916               ;; it makes Gnus fetch all headers and then limit the
4917               ;; articles (which is slow), so instead we hack the
4918               ;; select-articles parameter instead. -- Simon Josefsson
4919               ;; <jas@kth.se>
4920               ;;
4921               ;; (gnus-byte-compile
4922               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4923               ;;                         display)))))
4924               (setq select-articles
4925                     (gnus-uncompress-range
4926                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4927                              (if (> tmp 0)
4928                                  tmp
4929                                1))
4930                            (cdr (gnus-active group)))))
4931               nil)
4932              (t
4933               nil))))
4934
4935     (gnus-summary-setup-default-charset)
4936
4937     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4938     (when (gnus-virtual-group-p group)
4939       (setq cached gnus-newsgroup-cached))
4940
4941     (setq gnus-newsgroup-unreads
4942           (gnus-sorted-ndifference
4943            (gnus-sorted-ndifference gnus-newsgroup-unreads
4944                                     gnus-newsgroup-marked)
4945            gnus-newsgroup-dormant))
4946
4947     (setq gnus-newsgroup-processable nil)
4948
4949     (gnus-update-read-articles group gnus-newsgroup-unreads)
4950
4951     ;; Adjust and set lists of article marks.
4952     (when info
4953       (gnus-adjust-marked-articles info))
4954     (if (setq articles select-articles)
4955         (setq gnus-newsgroup-unselected
4956               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4957       (setq articles (gnus-articles-to-read group read-all)))
4958
4959     (cond
4960      ((null articles)
4961       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4962       'quit)
4963      ((eq articles 0) nil)
4964      (t
4965       ;; Init the dependencies hash table.
4966       (setq gnus-newsgroup-dependencies
4967             (gnus-make-hashtable (length articles)))
4968       (gnus-set-global-variables)
4969       ;; Retrieve the headers and read them in.
4970
4971       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4972
4973       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4974       (when cached
4975         (setq gnus-newsgroup-cached cached))
4976
4977       ;; Suppress duplicates?
4978       (when gnus-suppress-duplicates
4979         (gnus-dup-suppress-articles))
4980
4981       ;; Set the initial limit.
4982       (setq gnus-newsgroup-limit (copy-sequence articles))
4983       ;; Remove canceled articles from the list of unread articles.
4984       (setq fetched-articles
4985             (mapcar (lambda (headers) (mail-header-number headers))
4986                     gnus-newsgroup-headers))
4987       (setq gnus-newsgroup-articles fetched-articles)
4988       (setq gnus-newsgroup-unreads
4989             (gnus-sorted-nintersection
4990              gnus-newsgroup-unreads fetched-articles))
4991       (gnus-compute-unseen-list)
4992
4993       ;; Removed marked articles that do not exist.
4994       (gnus-update-missing-marks
4995        (gnus-sorted-difference articles fetched-articles))
4996       ;; We might want to build some more threads first.
4997       (when (and gnus-fetch-old-headers
4998                  (eq gnus-headers-retrieved-by 'nov))
4999         (if (eq gnus-fetch-old-headers 'invisible)
5000             (gnus-build-all-threads)
5001           (gnus-build-old-threads)))
5002       ;; Let the Gnus agent mark articles as read.
5003       (when gnus-agent
5004         (gnus-agent-get-undownloaded-list))
5005       ;; Remove list identifiers from subject
5006       (when gnus-list-identifiers
5007         (gnus-summary-remove-list-identifiers))
5008       ;; Check whether auto-expire is to be done in this group.
5009       (setq gnus-newsgroup-auto-expire
5010             (gnus-group-auto-expirable-p group))
5011       ;; Set up the article buffer now, if necessary.
5012       (unless gnus-single-article-buffer
5013         (gnus-article-setup-buffer))
5014       ;; First and last article in this newsgroup.
5015       (when gnus-newsgroup-headers
5016         (setq gnus-newsgroup-begin
5017               (mail-header-number (car gnus-newsgroup-headers))
5018               gnus-newsgroup-end
5019               (mail-header-number
5020                (gnus-last-element gnus-newsgroup-headers))))
5021       ;; GROUP is successfully selected.
5022       (or gnus-newsgroup-headers t)))))
5023
5024 (defun gnus-compute-unseen-list ()
5025   ;; The `seen' marks are treated specially.
5026   (if (not gnus-newsgroup-seen)
5027       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5028     (setq gnus-newsgroup-unseen
5029           (gnus-inverse-list-range-intersection
5030            gnus-newsgroup-articles gnus-newsgroup-seen))))
5031
5032 (defun gnus-summary-display-make-predicate (display)
5033   (require 'gnus-agent)
5034   (when (= (length display) 1)
5035     (setq display (car display)))
5036   (unless gnus-summary-display-cache
5037     (dolist (elem (append '((unread . unread)
5038                             (read . read)
5039                             (unseen . unseen))
5040                           gnus-article-mark-lists))
5041       (push (cons (cdr elem)
5042                   (gnus-byte-compile
5043                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5044             gnus-summary-display-cache)))
5045   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5046         (gnus-category-predicate-cache gnus-summary-display-cache))
5047     (gnus-get-predicate display)))
5048
5049 ;; Uses the dynamically bound `number' variable.
5050 (eval-when-compile
5051   (defvar number))
5052 (defun gnus-article-marked-p (type &optional article)
5053   (let ((article (or article number)))
5054     (cond
5055      ((eq type 'tick)
5056       (memq article gnus-newsgroup-marked))
5057      ((eq type 'spam)
5058       (memq article gnus-newsgroup-spam-marked))
5059      ((eq type 'unsend)
5060       (memq article gnus-newsgroup-unsendable))
5061      ((eq type 'undownload)
5062       (memq article gnus-newsgroup-undownloaded))
5063      ((eq type 'download)
5064       (memq article gnus-newsgroup-downloadable))
5065      ((eq type 'unread)
5066       (memq article gnus-newsgroup-unreads))
5067      ((eq type 'read)
5068       (memq article gnus-newsgroup-reads))
5069      ((eq type 'dormant)
5070       (memq article gnus-newsgroup-dormant) )
5071      ((eq type 'expire)
5072       (memq article gnus-newsgroup-expirable))
5073      ((eq type 'reply)
5074       (memq article gnus-newsgroup-replied))
5075      ((eq type 'killed)
5076       (memq article gnus-newsgroup-killed))
5077      ((eq type 'bookmark)
5078       (assq article gnus-newsgroup-bookmarks))
5079      ((eq type 'score)
5080       (assq article gnus-newsgroup-scored))
5081      ((eq type 'save)
5082       (memq article gnus-newsgroup-saved))
5083      ((eq type 'cache)
5084       (memq article gnus-newsgroup-cached))
5085      ((eq type 'forward)
5086       (memq article gnus-newsgroup-forwarded))
5087      ((eq type 'seen)
5088       (not (memq article gnus-newsgroup-unseen)))
5089      ((eq type 'recent)
5090       (memq article gnus-newsgroup-recent))
5091      (t t))))
5092
5093 (defun gnus-articles-to-read (group &optional read-all)
5094   "Find out what articles the user wants to read."
5095   (let* ((display (gnus-group-find-parameter group 'display))
5096          (articles
5097           ;; Select all articles if `read-all' is non-nil, or if there
5098           ;; are no unread articles.
5099           (if (or read-all
5100                   (and (zerop (length gnus-newsgroup-marked))
5101                        (zerop (length gnus-newsgroup-unreads)))
5102                   ;; Fetch all if the predicate is non-nil.
5103                   gnus-newsgroup-display)
5104               ;; We want to select the headers for all the articles in
5105               ;; the group, so we select either all the active
5106               ;; articles in the group, or (if that's nil), the
5107               ;; articles in the cache.
5108               (or
5109                (gnus-uncompress-range (gnus-active group))
5110                (gnus-cache-articles-in-group group))
5111             ;; Select only the "normal" subset of articles.
5112             (gnus-sorted-nunion
5113              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5114              gnus-newsgroup-unreads)))
5115          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5116          (scored (length scored-list))
5117          (number (length articles))
5118          (marked (+ (length gnus-newsgroup-marked)
5119                     (length gnus-newsgroup-dormant)))
5120          (select
5121           (cond
5122            ((numberp read-all)
5123             read-all)
5124            ((numberp gnus-newsgroup-display)
5125             gnus-newsgroup-display)
5126            (t
5127             (condition-case ()
5128                 (cond
5129                  ((and (or (<= scored marked) (= scored number))
5130                        (numberp gnus-large-newsgroup)
5131                        (> number gnus-large-newsgroup))
5132                   (let* ((cursor-in-echo-area nil)
5133                          (initial (gnus-parameter-large-newsgroup-initial
5134                                    gnus-newsgroup-name))
5135                          (input
5136                           (read-string
5137                            (format
5138                             "How many articles from %s (%s %d): "
5139                             (gnus-limit-string
5140                              (gnus-group-decoded-name gnus-newsgroup-name)
5141                              35)
5142                             (if initial "max" "default")
5143                             number)
5144                            (if initial
5145                                (cons (number-to-string initial)
5146                                      0)))))
5147                     (if (string-match "^[ \t]*$" input) number input)))
5148                  ((and (> scored marked) (< scored number)
5149                        (> (- scored number) 20))
5150                   (let ((input
5151                          (read-string
5152                           (format "%s %s (%d scored, %d total): "
5153                                   "How many articles from"
5154                                   (gnus-group-decoded-name group)
5155                                   scored number))))
5156                     (if (string-match "^[ \t]*$" input)
5157                         number input)))
5158                  (t number))
5159               (quit
5160                (message "Quit getting the articles to read")
5161                nil))))))
5162     (setq select (if (stringp select) (string-to-number select) select))
5163     (if (or (null select) (zerop select))
5164         select
5165       (if (and (not (zerop scored)) (<= (abs select) scored))
5166           (progn
5167             (setq articles (sort scored-list '<))
5168             (setq number (length articles)))
5169         (setq articles (copy-sequence articles)))
5170
5171       (when (< (abs select) number)
5172         (if (< select 0)
5173             ;; Select the N oldest articles.
5174             (setcdr (nthcdr (1- (abs select)) articles) nil)
5175           ;; Select the N most recent articles.
5176           (setq articles (nthcdr (- number select) articles))))
5177       (setq gnus-newsgroup-unselected
5178             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5179       (when gnus-alter-articles-to-read-function
5180         (setq gnus-newsgroup-unreads
5181               (sort
5182                (funcall gnus-alter-articles-to-read-function
5183                         gnus-newsgroup-name gnus-newsgroup-unreads)
5184                '<)))
5185       articles)))
5186
5187 (defun gnus-killed-articles (killed articles)
5188   (let (out)
5189     (while articles
5190       (when (inline (gnus-member-of-range (car articles) killed))
5191         (push (car articles) out))
5192       (setq articles (cdr articles)))
5193     out))
5194
5195 (defun gnus-uncompress-marks (marks)
5196   "Uncompress the mark ranges in MARKS."
5197   (let ((uncompressed '(score bookmark))
5198         out)
5199     (while marks
5200       (if (memq (caar marks) uncompressed)
5201           (push (car marks) out)
5202         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5203       (setq marks (cdr marks)))
5204     out))
5205
5206 (defun gnus-article-mark-to-type (mark)
5207   "Return the type of MARK."
5208   (or (cadr (assq mark gnus-article-special-mark-lists))
5209       'list))
5210
5211 (defun gnus-article-unpropagatable-p (mark)
5212   "Return whether MARK should be propagated to backend."
5213   (memq mark gnus-article-unpropagated-mark-lists))
5214
5215 (defun gnus-adjust-marked-articles (info)
5216   "Set all article lists and remove all marks that are no longer valid."
5217   (let* ((marked-lists (gnus-info-marks info))
5218          (active (gnus-active (gnus-info-group info)))
5219          (min (car active))
5220          (max (cdr active))
5221          (types gnus-article-mark-lists)
5222          marks var articles article mark mark-type)
5223
5224     (dolist (marks marked-lists)
5225       (setq mark (car marks)
5226             mark-type (gnus-article-mark-to-type mark)
5227             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5228
5229       ;; We set the variable according to the type of the marks list,
5230       ;; and then adjust the marks to a subset of the active articles.
5231       (cond
5232        ;; Adjust "simple" lists.
5233        ((eq mark-type 'list)
5234         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5235         (when (memq mark '(tick dormant expire reply save))
5236           (while articles
5237             (when (or (< (setq article (pop articles)) min) (> article max))
5238               (set var (delq article (symbol-value var)))))))
5239        ;; Adjust assocs.
5240        ((eq mark-type 'tuple)
5241         (set var (setq articles (cdr marks)))
5242         (when (not (listp (cdr (symbol-value var))))
5243           (set var (list (symbol-value var))))
5244         (when (not (listp (cdr articles)))
5245           (setq articles (list articles)))
5246         (while articles
5247           (when (or (not (consp (setq article (pop articles))))
5248                     (< (car article) min)
5249                     (> (car article) max))
5250             (set var (delq article (symbol-value var))))))
5251        ;; Adjust ranges (sloppily).
5252        ((eq mark-type 'range)
5253         (cond
5254          ((eq mark 'seen)
5255           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5256           ;; It should be (seen (NUM1 . NUM2)).
5257           (when (numberp (cddr marks))
5258             (setcdr marks (list (cdr marks))))
5259           (setq articles (cdr marks))
5260           (while (and articles
5261                       (or (and (consp (car articles))
5262                                (> min (cdar articles)))
5263                           (and (numberp (car articles))
5264                                (> min (car articles)))))
5265             (pop articles))
5266           (set var articles))))))))
5267
5268 (defun gnus-update-missing-marks (missing)
5269   "Go through the list of MISSING articles and remove them from the mark lists."
5270   (when missing
5271     (let (var m)
5272       ;; Go through all types.
5273       (dolist (elem gnus-article-mark-lists)
5274         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5275           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5276           (when (symbol-value var)
5277             ;; This list has articles.  So we delete all missing
5278             ;; articles from it.
5279             (setq m missing)
5280             (while m
5281               (set var (delq (pop m) (symbol-value var))))))))))
5282
5283 (defun gnus-update-marks ()
5284   "Enter the various lists of marked articles into the newsgroup info list."
5285   (let ((types gnus-article-mark-lists)
5286         (info (gnus-get-info gnus-newsgroup-name))
5287         type list newmarked symbol delta-marks)
5288     (when info
5289       ;; Add all marks lists to the list of marks lists.
5290       (while (setq type (pop types))
5291         (setq list (symbol-value
5292                     (setq symbol
5293                           (intern (format "gnus-newsgroup-%s" (car type))))))
5294
5295         (when list
5296           ;; Get rid of the entries of the articles that have the
5297           ;; default score.
5298           (when (and (eq (cdr type) 'score)
5299                      gnus-save-score
5300                      list)
5301             (let* ((arts list)
5302                    (prev (cons nil list))
5303                    (all prev))
5304               (while arts
5305                 (if (or (not (consp (car arts)))
5306                         (= (cdar arts) gnus-summary-default-score))
5307                     (setcdr prev (cdr arts))
5308                   (setq prev arts))
5309                 (setq arts (cdr arts)))
5310               (setq list (cdr all)))))
5311
5312         (when (eq (cdr type) 'seen)
5313           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5314
5315         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5316           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5317
5318         (when (and (gnus-check-backend-function
5319                     'request-set-mark gnus-newsgroup-name)
5320                    (not (gnus-article-unpropagatable-p (cdr type))))
5321           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5322                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5323                  (add (gnus-remove-from-range
5324                        (gnus-copy-sequence list) old)))
5325             (when add
5326               (push (list add 'add (list (cdr type))) delta-marks))
5327             (when del
5328               (push (list del 'del (list (cdr type))) delta-marks))))
5329
5330         (when list
5331           (push (cons (cdr type) list) newmarked)))
5332
5333       (when delta-marks
5334         (unless (gnus-check-group gnus-newsgroup-name)
5335           (error "Can't open server for %s" gnus-newsgroup-name))
5336         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5337
5338       ;; Enter these new marks into the info of the group.
5339       (if (nthcdr 3 info)
5340           (setcar (nthcdr 3 info) newmarked)
5341         ;; Add the marks lists to the end of the info.
5342         (when newmarked
5343           (setcdr (nthcdr 2 info) (list newmarked))))
5344
5345       ;; Cut off the end of the info if there's nothing else there.
5346       (let ((i 5))
5347         (while (and (> i 2)
5348                     (not (nth i info)))
5349           (when (nthcdr (decf i) info)
5350             (setcdr (nthcdr i info) nil)))))))
5351
5352 (defun gnus-set-mode-line (where)
5353   "Set the mode line of the article or summary buffers.
5354 If WHERE is `summary', the summary mode line format will be used."
5355   ;; Is this mode line one we keep updated?
5356   (when (and (memq where gnus-updated-mode-lines)
5357              (symbol-value
5358               (intern (format "gnus-%s-mode-line-format-spec" where))))
5359     (let (mode-string)
5360       (save-excursion
5361         ;; We evaluate this in the summary buffer since these
5362         ;; variables are buffer-local to that buffer.
5363         (set-buffer gnus-summary-buffer)
5364        ;; We bind all these variables that are used in the `eval' form
5365         ;; below.
5366         (let* ((mformat (symbol-value
5367                          (intern
5368                           (format "gnus-%s-mode-line-format-spec" where))))
5369                (gnus-tmp-group-name (gnus-group-decoded-name
5370                                      gnus-newsgroup-name))
5371                (gnus-tmp-article-number (or gnus-current-article 0))
5372                (gnus-tmp-unread gnus-newsgroup-unreads)
5373                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5374                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5375                (gnus-tmp-unread-and-unselected
5376                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5377                             (zerop gnus-tmp-unselected))
5378                        "")
5379                       ((zerop gnus-tmp-unselected)
5380                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5381                       (t (format "{%d(+%d) more}"
5382                                  gnus-tmp-unread-and-unticked
5383                                  gnus-tmp-unselected))))
5384                (gnus-tmp-subject
5385                 (if (and gnus-current-headers
5386                          (vectorp gnus-current-headers))
5387                     (gnus-mode-string-quote
5388                      (mail-header-subject gnus-current-headers))
5389                   ""))
5390                bufname-length max-len
5391                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5392           (setq mode-string (eval mformat))
5393           (setq bufname-length (if (string-match "%b" mode-string)
5394                                    (- (length
5395                                        (buffer-name
5396                                         (if (eq where 'summary)
5397                                             nil
5398                                           (get-buffer gnus-article-buffer))))
5399                                       2)
5400                                  0))
5401           (setq max-len (max 4 (if gnus-mode-non-string-length
5402                                    (- (window-width)
5403                                       gnus-mode-non-string-length
5404                                       bufname-length)
5405                                  (length mode-string))))
5406           ;; We might have to chop a bit of the string off...
5407           (when (> (length mode-string) max-len)
5408             (setq mode-string
5409                   (concat (truncate-string-to-width mode-string (- max-len 3))
5410                           "...")))
5411           ;; Pad the mode string a bit.
5412           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5413       ;; Update the mode line.
5414       (setq mode-line-buffer-identification
5415             (gnus-mode-line-buffer-identification (list mode-string)))
5416       (set-buffer-modified-p t))))
5417
5418 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5419   "Go through the HEADERS list and add all Xrefs to a hash table.
5420 The resulting hash table is returned, or nil if no Xrefs were found."
5421   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5422          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5423          (xref-hashtb (gnus-make-hashtable))
5424          start group entry number xrefs header)
5425     (while headers
5426       (setq header (pop headers))
5427       (when (and (setq xrefs (mail-header-xref header))
5428                  (not (memq (setq number (mail-header-number header))
5429                             unreads)))
5430         (setq start 0)
5431         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5432           (setq start (match-end 0))
5433           (setq group (if prefix
5434                           (concat prefix (substring xrefs (match-beginning 1)
5435                                                     (match-end 1)))
5436                         (substring xrefs (match-beginning 1) (match-end 1))))
5437           (setq number
5438                 (string-to-int (substring xrefs (match-beginning 2)
5439                                           (match-end 2))))
5440           (if (setq entry (gnus-gethash group xref-hashtb))
5441               (setcdr entry (cons number (cdr entry)))
5442             (gnus-sethash group (cons number nil) xref-hashtb)))))
5443     (and start xref-hashtb)))
5444
5445 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5446   "Look through all the headers and mark the Xrefs as read."
5447   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5448         name entry info xref-hashtb idlist method nth4)
5449     (save-excursion
5450       (set-buffer gnus-group-buffer)
5451       (when (setq xref-hashtb
5452                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5453         (mapatoms
5454          (lambda (group)
5455            (unless (string= from-newsgroup (setq name (symbol-name group)))
5456              (setq idlist (symbol-value group))
5457              ;; Dead groups are not updated.
5458              (and (prog1
5459                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5460                             info (nth 2 entry))
5461                     (when (stringp (setq nth4 (gnus-info-method info)))
5462                       (setq nth4 (gnus-server-to-method nth4))))
5463                   ;; Only do the xrefs if the group has the same
5464                   ;; select method as the group we have just read.
5465                   (or (gnus-methods-equal-p
5466                        nth4 (gnus-find-method-for-group from-newsgroup))
5467                       virtual
5468                       (equal nth4 (setq method (gnus-find-method-for-group
5469                                                 from-newsgroup)))
5470                       (and (equal (car nth4) (car method))
5471                            (equal (nth 1 nth4) (nth 1 method))))
5472                   gnus-use-cross-reference
5473                   (or (not (eq gnus-use-cross-reference t))
5474                       virtual
5475                       ;; Only do cross-references on subscribed
5476                       ;; groups, if that is what is wanted.
5477                       (<= (gnus-info-level info) gnus-level-subscribed))
5478                   (gnus-group-make-articles-read name idlist))))
5479          xref-hashtb)))))
5480
5481 (defun gnus-compute-read-articles (group articles)
5482   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5483          (info (nth 2 entry))
5484          (active (gnus-active group))
5485          ninfo)
5486     (when entry
5487       ;; First peel off all invalid article numbers.
5488       (when active
5489         (let ((ids articles)
5490               id first)
5491           (while (setq id (pop ids))
5492             (when (and first (> id (cdr active)))
5493               ;; We'll end up in this situation in one particular
5494               ;; obscure situation.  If you re-scan a group and get
5495               ;; a new article that is cross-posted to a different
5496               ;; group that has not been re-scanned, you might get
5497               ;; crossposted article that has a higher number than
5498               ;; Gnus believes possible.  So we re-activate this
5499               ;; group as well.  This might mean doing the
5500               ;; crossposting thingy will *increase* the number
5501               ;; of articles in some groups.  Tsk, tsk.
5502               (setq active (or (gnus-activate-group group) active)))
5503             (when (or (> id (cdr active))
5504                       (< id (car active)))
5505               (setq articles (delq id articles))))))
5506       ;; If the read list is nil, we init it.
5507       (if (and active
5508                (null (gnus-info-read info))
5509                (> (car active) 1))
5510           (setq ninfo (cons 1 (1- (car active))))
5511         (setq ninfo (gnus-info-read info)))
5512       ;; Then we add the read articles to the range.
5513       (gnus-add-to-range
5514        ninfo (setq articles (sort articles '<))))))
5515
5516 (defun gnus-group-make-articles-read (group articles)
5517   "Update the info of GROUP to say that ARTICLES are read."
5518   (let* ((num 0)
5519          (entry (gnus-gethash group gnus-newsrc-hashtb))
5520          (info (nth 2 entry))
5521          (active (gnus-active group))
5522          range)
5523     (when entry
5524       (setq range (gnus-compute-read-articles group articles))
5525       (save-excursion
5526         (set-buffer gnus-group-buffer)
5527         (gnus-undo-register
5528           `(progn
5529              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5530              (gnus-info-set-read ',info ',(gnus-info-read info))
5531              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5532              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5533              (gnus-group-update-group ,group t))))
5534       ;; Add the read articles to the range.
5535       (gnus-info-set-read info range)
5536       (gnus-request-set-mark group (list (list range 'add '(read))))
5537       ;; Then we have to re-compute how many unread
5538       ;; articles there are in this group.
5539       (when active
5540         (cond
5541          ((not range)
5542           (setq num (- (1+ (cdr active)) (car active))))
5543          ((not (listp (cdr range)))
5544           (setq num (- (cdr active) (- (1+ (cdr range))
5545                                        (car range)))))
5546          (t
5547           (while range
5548             (if (numberp (car range))
5549                 (setq num (1+ num))
5550               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5551             (setq range (cdr range)))
5552           (setq num (- (cdr active) num))))
5553         ;; Update the number of unread articles.
5554         (setcar entry num)
5555         ;; Update the group buffer.
5556         (gnus-group-update-group group t)))))
5557
5558 (defvar gnus-newsgroup-none-id 0)
5559
5560 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5561   (let ((cur nntp-server-buffer)
5562         (dependencies
5563          (or dependencies
5564              (save-excursion (set-buffer gnus-summary-buffer)
5565                              gnus-newsgroup-dependencies)))
5566         headers id end ref
5567         (mail-parse-charset gnus-newsgroup-charset)
5568         (mail-parse-ignored-charsets
5569          (save-excursion (condition-case nil
5570                              (set-buffer gnus-summary-buffer)
5571                            (error))
5572                          gnus-newsgroup-ignored-charsets)))
5573     (save-excursion
5574       (set-buffer nntp-server-buffer)
5575       ;; Translate all TAB characters into SPACE characters.
5576       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5577       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5578       (gnus-run-hooks 'gnus-parse-headers-hook)
5579       (let ((case-fold-search t)
5580             in-reply-to header p lines chars)
5581         (goto-char (point-min))
5582         ;; Search to the beginning of the next header.  Error messages
5583         ;; do not begin with 2 or 3.
5584         (while (re-search-forward "^[23][0-9]+ " nil t)
5585           (setq id nil
5586                 ref nil)
5587           ;; This implementation of this function, with nine
5588           ;; search-forwards instead of the one re-search-forward and
5589           ;; a case (which basically was the old function) is actually
5590           ;; about twice as fast, even though it looks messier.  You
5591           ;; can't have everything, I guess.  Speed and elegance
5592           ;; doesn't always go hand in hand.
5593           (setq
5594            header
5595            (vector
5596             ;; Number.
5597             (prog1
5598                 (read cur)
5599               (end-of-line)
5600               (setq p (point))
5601               (narrow-to-region (point)
5602                                 (or (and (search-forward "\n.\n" nil t)
5603                                          (- (point) 2))
5604                                     (point))))
5605             ;; Subject.
5606             (progn
5607               (goto-char p)
5608               (if (search-forward "\nsubject:" nil t)
5609                   (funcall gnus-decode-encoded-word-function
5610                            (nnheader-header-value))
5611                 "(none)"))
5612             ;; From.
5613             (progn
5614               (goto-char p)
5615               (if (search-forward "\nfrom:" nil t)
5616                   (funcall gnus-decode-encoded-word-function
5617                            (nnheader-header-value))
5618                 "(nobody)"))
5619             ;; Date.
5620             (progn
5621               (goto-char p)
5622               (if (search-forward "\ndate:" nil t)
5623                   (nnheader-header-value) ""))
5624             ;; Message-ID.
5625             (progn
5626               (goto-char p)
5627               (setq id (if (re-search-forward
5628                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5629                            ;; We do it this way to make sure the Message-ID
5630                            ;; is (somewhat) syntactically valid.
5631                            (buffer-substring (match-beginning 1)
5632                                              (match-end 1))
5633                          ;; If there was no message-id, we just fake one
5634                          ;; to make subsequent routines simpler.
5635                          (nnheader-generate-fake-message-id))))
5636             ;; References.
5637             (progn
5638               (goto-char p)
5639               (if (search-forward "\nreferences:" nil t)
5640                   (progn
5641                     (setq end (point))
5642                     (prog1
5643                         (nnheader-header-value)
5644                       (setq ref
5645                             (buffer-substring
5646                              (progn
5647                                (end-of-line)
5648                                (search-backward ">" end t)
5649                                (1+ (point)))
5650                              (progn
5651                                (search-backward "<" end t)
5652                                (point))))))
5653                 ;; Get the references from the in-reply-to header if there
5654                 ;; were no references and the in-reply-to header looks
5655                 ;; promising.
5656                 (if (and (search-forward "\nin-reply-to:" nil t)
5657                          (setq in-reply-to (nnheader-header-value))
5658                          (string-match "<[^>]+>" in-reply-to))
5659                     (let (ref2)
5660                       (setq ref (substring in-reply-to (match-beginning 0)
5661                                            (match-end 0)))
5662                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5663                         (setq ref2 (substring in-reply-to (match-beginning 0)
5664                                               (match-end 0)))
5665                         (when (> (length ref2) (length ref))
5666                           (setq ref ref2)))
5667                       ref)
5668                   (setq ref nil))))
5669             ;; Chars.
5670             (progn
5671               (goto-char p)
5672               (if (search-forward "\nchars: " nil t)
5673                   (if (numberp (setq chars (ignore-errors (read cur))))
5674                       chars -1)
5675                 -1))
5676             ;; Lines.
5677             (progn
5678               (goto-char p)
5679               (if (search-forward "\nlines: " nil t)
5680                   (if (numberp (setq lines (ignore-errors (read cur))))
5681                       lines -1)
5682                 -1))
5683             ;; Xref.
5684             (progn
5685               (goto-char p)
5686               (and (search-forward "\nxref:" nil t)
5687                    (nnheader-header-value)))
5688             ;; Extra.
5689             (when gnus-extra-headers
5690               (let ((extra gnus-extra-headers)
5691                     out)
5692                 (while extra
5693                   (goto-char p)
5694                   (when (search-forward
5695                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5696                     (push (cons (car extra) (nnheader-header-value))
5697                           out))
5698                   (pop extra))
5699                 out))))
5700           (when (equal id ref)
5701             (setq ref nil))
5702
5703           (when gnus-alter-header-function
5704             (funcall gnus-alter-header-function header)
5705             (setq id (mail-header-id header)
5706                   ref (gnus-parent-id (mail-header-references header))))
5707
5708           (when (setq header
5709                       (gnus-dependencies-add-header
5710                        header dependencies force-new))
5711             (push header headers))
5712           (goto-char (point-max))
5713           (widen))
5714         (nreverse headers)))))
5715
5716 ;; Goes through the xover lines and returns a list of vectors
5717 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5718                                                   force-new dependencies
5719                                                   group also-fetch-heads)
5720   "Parse the news overview data in the server buffer.
5721 Return a list of headers that match SEQUENCE (see
5722 `nntp-retrieve-headers')."
5723   ;; Get the Xref when the users reads the articles since most/some
5724   ;; NNTP servers do not include Xrefs when using XOVER.
5725   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5726   (let ((mail-parse-charset gnus-newsgroup-charset)
5727         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5728         (cur nntp-server-buffer)
5729         (dependencies (or dependencies gnus-newsgroup-dependencies))
5730         (allp (cond
5731                ((eq gnus-read-all-available-headers t)
5732                 t)
5733                ((stringp gnus-read-all-available-headers)
5734                 (string-match gnus-read-all-available-headers group))
5735                (t
5736                 nil)))
5737         number headers header)
5738     (save-excursion
5739       (set-buffer nntp-server-buffer)
5740       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5741       ;; Allow the user to mangle the headers before parsing them.
5742       (gnus-run-hooks 'gnus-parse-headers-hook)
5743       (goto-char (point-min))
5744       (gnus-parse-without-error
5745         (while (and (or sequence allp)
5746                     (not (eobp)))
5747           (setq number (read cur))
5748           (when (not allp)
5749             (while (and sequence
5750                         (< (car sequence) number))
5751               (setq sequence (cdr sequence))))
5752           (when (and (or allp
5753                          (and sequence
5754                               (eq number (car sequence))))
5755                      (progn
5756                        (setq sequence (cdr sequence))
5757                        (setq header (inline
5758                                       (gnus-nov-parse-line
5759                                        number dependencies force-new)))))
5760             (push header headers))
5761           (forward-line 1)))
5762       ;; A common bug in inn is that if you have posted an article and
5763       ;; then retrieves the active file, it will answer correctly --
5764       ;; the new article is included.  However, a NOV entry for the
5765       ;; article may not have been generated yet, so this may fail.
5766       ;; We work around this problem by retrieving the last few
5767       ;; headers using HEAD.
5768       (if (or (not also-fetch-heads)
5769               (not sequence))
5770           ;; We (probably) got all the headers.
5771           (nreverse headers)
5772         (let ((gnus-nov-is-evil t))
5773           (nconc
5774            (nreverse headers)
5775            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5776              (gnus-get-newsgroup-headers))))))))
5777
5778 (defun gnus-article-get-xrefs ()
5779   "Fill in the Xref value in `gnus-current-headers', if necessary.
5780 This is meant to be called in `gnus-article-internal-prepare-hook'."
5781   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5782                                  gnus-current-headers)))
5783     (or (not gnus-use-cross-reference)
5784         (not headers)
5785         (and (mail-header-xref headers)
5786              (not (string= (mail-header-xref headers) "")))
5787         (let ((case-fold-search t)
5788               xref)
5789           (save-restriction
5790             (nnheader-narrow-to-headers)
5791             (goto-char (point-min))
5792             (when (or (and (not (eobp))
5793                            (eq (downcase (char-after)) ?x)
5794                            (looking-at "Xref:"))
5795                       (search-forward "\nXref:" nil t))
5796               (goto-char (1+ (match-end 0)))
5797               (setq xref (buffer-substring (point)
5798                                            (progn (end-of-line) (point))))
5799               (mail-header-set-xref headers xref)))))))
5800
5801 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5802   "Find article ID and insert the summary line for that article.
5803 OLD-HEADER can either be a header or a line number to insert
5804 the subject line on."
5805   (let* ((line (and (numberp old-header) old-header))
5806          (old-header (and (vectorp old-header) old-header))
5807          (header (cond ((and old-header use-old-header)
5808                         old-header)
5809                        ((and (numberp id)
5810                              (gnus-number-to-header id))
5811                         (gnus-number-to-header id))
5812                        (t
5813                         (gnus-read-header id))))
5814          (number (and (numberp id) id))
5815          d)
5816     (when header
5817       ;; Rebuild the thread that this article is part of and go to the
5818       ;; article we have fetched.
5819       (when (and (not gnus-show-threads)
5820                  old-header)
5821         (when (and number
5822                    (setq d (gnus-data-find (mail-header-number old-header))))
5823           (goto-char (gnus-data-pos d))
5824           (gnus-data-remove
5825            number
5826            (- (gnus-point-at-bol)
5827               (prog1
5828                   (1+ (gnus-point-at-eol))
5829                 (gnus-delete-line))))))
5830       (when old-header
5831         (mail-header-set-number header (mail-header-number old-header)))
5832       (setq gnus-newsgroup-sparse
5833             (delq (setq number (mail-header-number header))
5834                   gnus-newsgroup-sparse))
5835       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5836       (push number gnus-newsgroup-limit)
5837       (gnus-rebuild-thread (mail-header-id header) line)
5838       (gnus-summary-goto-subject number nil t))
5839     (when (and (numberp number)
5840                (> number 0))
5841       ;; We have to update the boundaries even if we can't fetch the
5842       ;; article if ID is a number -- so that the next `P' or `N'
5843       ;; command will fetch the previous (or next) article even
5844       ;; if the one we tried to fetch this time has been canceled.
5845       (when (> number gnus-newsgroup-end)
5846         (setq gnus-newsgroup-end number))
5847       (when (< number gnus-newsgroup-begin)
5848         (setq gnus-newsgroup-begin number))
5849       (setq gnus-newsgroup-unselected
5850             (delq number gnus-newsgroup-unselected)))
5851     ;; Report back a success?
5852     (and header (mail-header-number header))))
5853
5854 ;;; Process/prefix in the summary buffer
5855
5856 (defun gnus-summary-work-articles (n)
5857   "Return a list of articles to be worked upon.
5858 The prefix argument, the list of process marked articles, and the
5859 current article will be taken into consideration."
5860   (save-excursion
5861     (set-buffer gnus-summary-buffer)
5862     (cond
5863      (n
5864       ;; A numerical prefix has been given.
5865       (setq n (prefix-numeric-value n))
5866       (let ((backward (< n 0))
5867             (n (abs (prefix-numeric-value n)))
5868             articles article)
5869         (save-excursion
5870           (while
5871               (and (> n 0)
5872                    (push (setq article (gnus-summary-article-number))
5873                          articles)
5874                    (if backward
5875                        (gnus-summary-find-prev nil article)
5876                      (gnus-summary-find-next nil article)))
5877             (decf n)))
5878         (nreverse articles)))
5879      ((and (gnus-region-active-p) (mark))
5880       (message "region active")
5881       ;; Work on the region between point and mark.
5882       (let ((max (max (point) (mark)))
5883             articles article)
5884         (save-excursion
5885           (goto-char (min (min (point) (mark))))
5886           (while
5887               (and
5888                (push (setq article (gnus-summary-article-number)) articles)
5889                (gnus-summary-find-next nil article)
5890                (< (point) max)))
5891           (nreverse articles))))
5892      (gnus-newsgroup-processable
5893       ;; There are process-marked articles present.
5894       ;; Save current state.
5895       (gnus-summary-save-process-mark)
5896       ;; Return the list.
5897       (reverse gnus-newsgroup-processable))
5898      (t
5899       ;; Just return the current article.
5900       (list (gnus-summary-article-number))))))
5901
5902 (defmacro gnus-summary-iterate (arg &rest forms)
5903   "Iterate over the process/prefixed articles and do FORMS.
5904 ARG is the interactive prefix given to the command.  FORMS will be
5905 executed with point over the summary line of the articles."
5906   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5907     `(let ((,articles (gnus-summary-work-articles ,arg)))
5908        (while ,articles
5909          (gnus-summary-goto-subject (car ,articles))
5910          ,@forms
5911          (pop ,articles)))))
5912
5913 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5914 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5915
5916 (defun gnus-summary-save-process-mark ()
5917   "Push the current set of process marked articles on the stack."
5918   (interactive)
5919   (push (copy-sequence gnus-newsgroup-processable)
5920         gnus-newsgroup-process-stack))
5921
5922 (defun gnus-summary-kill-process-mark ()
5923   "Push the current set of process marked articles on the stack and unmark."
5924   (interactive)
5925   (gnus-summary-save-process-mark)
5926   (gnus-summary-unmark-all-processable))
5927
5928 (defun gnus-summary-yank-process-mark ()
5929   "Pop the last process mark state off the stack and restore it."
5930   (interactive)
5931   (unless gnus-newsgroup-process-stack
5932     (error "Empty mark stack"))
5933   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5934
5935 (defun gnus-summary-process-mark-set (set)
5936   "Make SET into the current process marked articles."
5937   (gnus-summary-unmark-all-processable)
5938   (while set
5939     (gnus-summary-set-process-mark (pop set))))
5940
5941 ;;; Searching and stuff
5942
5943 (defun gnus-summary-search-group (&optional backward use-level)
5944   "Search for next unread newsgroup.
5945 If optional argument BACKWARD is non-nil, search backward instead."
5946   (save-excursion
5947     (set-buffer gnus-group-buffer)
5948     (when (gnus-group-search-forward
5949            backward nil (if use-level (gnus-group-group-level) nil))
5950       (gnus-group-group-name))))
5951
5952 (defun gnus-summary-best-group (&optional exclude-group)
5953   "Find the name of the best unread group.
5954 If EXCLUDE-GROUP, do not go to this group."
5955   (save-excursion
5956     (set-buffer gnus-group-buffer)
5957     (save-excursion
5958       (gnus-group-best-unread-group exclude-group))))
5959
5960 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5961   (if backward (gnus-summary-find-prev)
5962     (let* ((dummy (gnus-summary-article-intangible-p))
5963            (article (or article (gnus-summary-article-number)))
5964            (arts (gnus-data-find-list article))
5965            result)
5966       (when (and (not dummy)
5967                  (or (not gnus-summary-check-current)
5968                      (not unread)
5969                      (not (gnus-data-unread-p (car arts)))))
5970         (setq arts (cdr arts)))
5971       (when (setq result
5972                   (if unread
5973                       (progn
5974                         (while arts
5975                           (when (or (and undownloaded
5976                                          (memq (car arts) gnus-newsgroup-undownloaded))
5977                                     (gnus-data-unread-p (car arts)))
5978                             (setq result (car arts)
5979                                   arts nil))
5980                           (setq arts (cdr arts)))
5981                         result)
5982                     (car arts)))
5983         (goto-char (gnus-data-pos result))
5984         (gnus-data-number result)))))
5985
5986 (defun gnus-summary-find-prev (&optional unread article)
5987   (let* ((eobp (eobp))
5988          (article (or article (gnus-summary-article-number)))
5989          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5990          result)
5991     (when (and (not eobp)
5992                (or (not gnus-summary-check-current)
5993                    (not unread)
5994                    (not (gnus-data-unread-p (car arts)))))
5995       (setq arts (cdr arts)))
5996     (when (setq result
5997                 (if unread
5998                     (progn
5999                       (while arts
6000                         (when (gnus-data-unread-p (car arts))
6001                           (setq result (car arts)
6002                                 arts nil))
6003                         (setq arts (cdr arts)))
6004                       result)
6005                   (car arts)))
6006       (goto-char (gnus-data-pos result))
6007       (gnus-data-number result))))
6008
6009 (defun gnus-summary-find-subject (subject &optional unread backward article)
6010   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6011          (article (or article (gnus-summary-article-number)))
6012          (articles (gnus-data-list backward))
6013          (arts (gnus-data-find-list article articles))
6014          result)
6015     (when (or (not gnus-summary-check-current)
6016               (not unread)
6017               (not (gnus-data-unread-p (car arts))))
6018       (setq arts (cdr arts)))
6019     (while arts
6020       (and (or (not unread)
6021                (gnus-data-unread-p (car arts)))
6022            (vectorp (gnus-data-header (car arts)))
6023            (gnus-subject-equal
6024             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6025            (setq result (car arts)
6026                  arts nil))
6027       (setq arts (cdr arts)))
6028     (and result
6029          (goto-char (gnus-data-pos result))
6030          (gnus-data-number result))))
6031
6032 (defun gnus-summary-search-forward (&optional unread subject backward)
6033   "Search forward for an article.
6034 If UNREAD, look for unread articles.  If SUBJECT, look for
6035 articles with that subject.  If BACKWARD, search backward instead."
6036   (cond (subject (gnus-summary-find-subject subject unread backward))
6037         (backward (gnus-summary-find-prev unread))
6038         (t (gnus-summary-find-next unread))))
6039
6040 (defun gnus-recenter (&optional n)
6041   "Center point in window and redisplay frame.
6042 Also do horizontal recentering."
6043   (interactive "P")
6044   (when (and gnus-auto-center-summary
6045              (not (eq gnus-auto-center-summary 'vertical)))
6046     (gnus-horizontal-recenter))
6047   (recenter n))
6048
6049 (defun gnus-summary-recenter ()
6050   "Center point in the summary window.
6051 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6052 displayed, no centering will be performed."
6053   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6054 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6055   (interactive)
6056   (let* ((top (cond ((< (window-height) 4) 0)
6057                     ((< (window-height) 7) 1)
6058                     (t (if (numberp gnus-auto-center-summary)
6059                            gnus-auto-center-summary
6060                          2))))
6061          (height (1- (window-height)))
6062          (bottom (save-excursion (goto-char (point-max))
6063                                  (forward-line (- height))
6064                                  (point)))
6065          (window (get-buffer-window (current-buffer))))
6066     ;; The user has to want it.
6067     (when gnus-auto-center-summary
6068       (when (get-buffer-window gnus-article-buffer)
6069         ;; Only do recentering when the article buffer is displayed,
6070       ;; Set the window start to either `bottom', which is the biggest
6071         ;; possible valid number, or the second line from the top,
6072         ;; whichever is the least.
6073         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6074           (if (> bottom top-pos)
6075               ;; Keep the second line from the top visible
6076               (set-window-start window top-pos t)
6077             ;; Try to keep the bottom line visible; if it's partially
6078             ;; obscured, either scroll one more line to make it fully
6079             ;; visible, or revert to using TOP-POS.
6080             (save-excursion
6081               (goto-char (point-max))
6082               (forward-line -1)
6083               (let ((last-line-start (point)))
6084                 (goto-char bottom)
6085                 (set-window-start window (point) t)
6086                 (when (not (pos-visible-in-window-p last-line-start window))
6087                   (forward-line 1)
6088                   (set-window-start window (min (point) top-pos) t)))))))
6089       ;; Do horizontal recentering while we're at it.
6090       (when (and (get-buffer-window (current-buffer) t)
6091                  (not (eq gnus-auto-center-summary 'vertical)))
6092         (let ((selected (selected-window)))
6093           (select-window (get-buffer-window (current-buffer) t))
6094           (gnus-summary-position-point)
6095           (gnus-horizontal-recenter)
6096           (select-window selected))))))
6097
6098 (defun gnus-summary-jump-to-group (newsgroup)
6099   "Move point to NEWSGROUP in group mode buffer."
6100   ;; Keep update point of group mode buffer if visible.
6101   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6102       (save-window-excursion
6103         ;; Take care of tree window mode.
6104         (when (get-buffer-window gnus-group-buffer)
6105           (pop-to-buffer gnus-group-buffer))
6106         (gnus-group-jump-to-group newsgroup))
6107     (save-excursion
6108       ;; Take care of tree window mode.
6109       (if (get-buffer-window gnus-group-buffer)
6110           (pop-to-buffer gnus-group-buffer)
6111         (set-buffer gnus-group-buffer))
6112       (gnus-group-jump-to-group newsgroup))))
6113
6114 ;; This function returns a list of article numbers based on the
6115 ;; difference between the ranges of read articles in this group and
6116 ;; the range of active articles.
6117 (defun gnus-list-of-unread-articles (group)
6118   (let* ((read (gnus-info-read (gnus-get-info group)))
6119          (active (or (gnus-active group) (gnus-activate-group group)))
6120          (last (cdr active))
6121          first nlast unread)
6122     ;; If none are read, then all are unread.
6123     (if (not read)
6124         (setq first (car active))
6125       ;; If the range of read articles is a single range, then the
6126       ;; first unread article is the article after the last read
6127       ;; article.  Sounds logical, doesn't it?
6128       (if (and (not (listp (cdr read)))
6129                (or (< (car read) (car active))
6130                    (progn (setq read (list read))
6131                           nil)))
6132           (setq first (max (car active) (1+ (cdr read))))
6133         ;; `read' is a list of ranges.
6134         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6135                                   (caar read)))
6136                   1)
6137           (setq first (car active)))
6138         (while read
6139           (when first
6140             (while (< first nlast)
6141               (push first unread)
6142               (setq first (1+ first))))
6143           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6144           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6145           (setq read (cdr read)))))
6146     ;; And add the last unread articles.
6147     (while (<= first last)
6148       (push first unread)
6149       (setq first (1+ first)))
6150     ;; Return the list of unread articles.
6151     (delq 0 (nreverse unread))))
6152
6153 (defun gnus-list-of-read-articles (group)
6154   "Return a list of unread, unticked and non-dormant articles."
6155   (let* ((info (gnus-get-info group))
6156          (marked (gnus-info-marks info))
6157          (active (gnus-active group)))
6158     (and info active
6159          (gnus-list-range-difference
6160           (gnus-list-range-difference
6161            (gnus-sorted-complement
6162             (gnus-uncompress-range active)
6163             (gnus-list-of-unread-articles group))
6164            (cdr (assq 'dormant marked)))
6165           (cdr (assq 'tick marked))))))
6166
6167 ;; Various summary commands
6168
6169 (defun gnus-summary-select-article-buffer ()
6170   "Reconfigure windows to show article buffer."
6171   (interactive)
6172   (if (not (gnus-buffer-live-p gnus-article-buffer))
6173       (error "There is no article buffer for this summary buffer")
6174     (gnus-configure-windows 'article)
6175     (select-window (get-buffer-window gnus-article-buffer))))
6176
6177 (defun gnus-summary-universal-argument (arg)
6178   "Perform any operation on all articles that are process/prefixed."
6179   (interactive "P")
6180   (let ((articles (gnus-summary-work-articles arg))
6181         func article)
6182     (if (eq
6183          (setq
6184           func
6185           (key-binding
6186            (read-key-sequence
6187             (substitute-command-keys
6188              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6189          'undefined)
6190         (gnus-error 1 "Undefined key")
6191       (save-excursion
6192         (while articles
6193           (gnus-summary-goto-subject (setq article (pop articles)))
6194           (let (gnus-newsgroup-processable)
6195             (command-execute func))
6196           (gnus-summary-remove-process-mark article)))))
6197   (gnus-summary-position-point))
6198
6199 (defun gnus-summary-toggle-truncation (&optional arg)
6200   "Toggle truncation of summary lines.
6201 With arg, turn line truncation on if arg is positive."
6202   (interactive "P")
6203   (setq truncate-lines
6204         (if (null arg) (not truncate-lines)
6205           (> (prefix-numeric-value arg) 0)))
6206   (redraw-display))
6207
6208 (defun gnus-summary-find-uncancelled ()
6209   "Return the number of an uncancelled article.
6210 The current article is considered, then following articles, then previous
6211 articles.  If all articles are cancelled then return a dummy 0."
6212   (let (found)
6213     (dolist (rev '(nil t))
6214       (unless found      ; don't demand the reverse list if we don't need it
6215         (let ((data (gnus-data-find-list
6216                      (gnus-summary-article-number) (gnus-data-list rev))))
6217           (while (and data (not found))
6218             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6219                 (setq found (gnus-data-number (car data))))
6220             (setq data (cdr data))))))
6221     (or found 0)))
6222
6223 (defun gnus-summary-reselect-current-group (&optional all rescan)
6224   "Exit and then reselect the current newsgroup.
6225 The prefix argument ALL means to select all articles."
6226   (interactive "P")
6227   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6228     (error "Ephemeral groups can't be reselected"))
6229   (let ((current-subject (gnus-summary-find-uncancelled))
6230         (group gnus-newsgroup-name))
6231     (setq gnus-newsgroup-begin nil)
6232     (gnus-summary-exit)
6233     ;; We have to adjust the point of group mode buffer because
6234     ;; point was moved to the next unread newsgroup by exiting.
6235     (gnus-summary-jump-to-group group)
6236     (when rescan
6237       (save-excursion
6238         (gnus-group-get-new-news-this-group 1)))
6239     (gnus-group-read-group all t)
6240     (gnus-summary-goto-subject current-subject nil t)))
6241
6242 (defun gnus-summary-rescan-group (&optional all)
6243   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6244   (interactive "P")
6245   (gnus-summary-reselect-current-group all t))
6246
6247 (defun gnus-summary-update-info (&optional non-destructive)
6248   (save-excursion
6249     (let ((group gnus-newsgroup-name))
6250       (when group
6251         (when gnus-newsgroup-kill-headers
6252           (setq gnus-newsgroup-killed
6253                 (gnus-compress-sequence
6254                  (gnus-sorted-union
6255                   (gnus-list-range-intersection
6256                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6257                   gnus-newsgroup-unreads)
6258                  t)))
6259         (unless (listp (cdr gnus-newsgroup-killed))
6260           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6261         (let ((headers gnus-newsgroup-headers))
6262           ;; Set the new ranges of read articles.
6263           (save-excursion
6264             (set-buffer gnus-group-buffer)
6265             (gnus-undo-force-boundary))
6266           (gnus-update-read-articles
6267            group (gnus-sorted-union
6268                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6269           ;; Set the current article marks.
6270           (let ((gnus-newsgroup-scored
6271                  (if (and (not gnus-save-score)
6272                           (not non-destructive))
6273                      nil
6274                    gnus-newsgroup-scored)))
6275             (save-excursion
6276               (gnus-update-marks)))
6277           ;; Do the cross-ref thing.
6278           (when gnus-use-cross-reference
6279             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6280           ;; Do not switch windows but change the buffer to work.
6281           (set-buffer gnus-group-buffer)
6282           (unless (gnus-ephemeral-group-p group)
6283             (gnus-group-update-group group)))))))
6284
6285 (defun gnus-summary-save-newsrc (&optional force)
6286   "Save the current number of read/marked articles in the dribble buffer.
6287 The dribble buffer will then be saved.
6288 If FORCE (the prefix), also save the .newsrc file(s)."
6289   (interactive "P")
6290   (gnus-summary-update-info t)
6291   (if force
6292       (gnus-save-newsrc-file)
6293     (gnus-dribble-save)))
6294
6295 (defun gnus-summary-exit (&optional temporary)
6296   "Exit reading current newsgroup, and then return to group selection mode.
6297 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6298   (interactive)
6299   (gnus-set-global-variables)
6300   (when (gnus-buffer-live-p gnus-article-buffer)
6301     (save-excursion
6302       (set-buffer gnus-article-buffer)
6303       (mm-destroy-parts gnus-article-mime-handles)
6304       ;; Set it to nil for safety reason.
6305       (setq gnus-article-mime-handle-alist nil)
6306       (setq gnus-article-mime-handles nil)))
6307   (gnus-kill-save-kill-buffer)
6308   (gnus-async-halt-prefetch)
6309   (let* ((group gnus-newsgroup-name)
6310          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6311          (mode major-mode)
6312          (group-point nil)
6313          (buf (current-buffer)))
6314     (unless quit-config
6315       ;; Do adaptive scoring, and possibly save score files.
6316       (when gnus-newsgroup-adaptive
6317         (gnus-score-adaptive))
6318       (when gnus-use-scoring
6319         (gnus-score-save)))
6320     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6321     ;; If we have several article buffers, we kill them at exit.
6322     (unless gnus-single-article-buffer
6323       (gnus-kill-buffer gnus-original-article-buffer)
6324       (setq gnus-article-current nil))
6325     (when gnus-use-cache
6326       (gnus-cache-possibly-remove-articles)
6327       (gnus-cache-save-buffers))
6328     (gnus-async-prefetch-remove-group group)
6329     (when gnus-suppress-duplicates
6330       (gnus-dup-enter-articles))
6331     (when gnus-use-trees
6332       (gnus-tree-close group))
6333     (when gnus-use-cache
6334       (gnus-cache-write-active))
6335     ;; Remove entries for this group.
6336     (nnmail-purge-split-history (gnus-group-real-name group))
6337     ;; Make all changes in this group permanent.
6338     (unless quit-config
6339       (gnus-run-hooks 'gnus-exit-group-hook)
6340       (gnus-summary-update-info))
6341     (gnus-close-group group)
6342     ;; Make sure where we were, and go to next newsgroup.
6343     (set-buffer gnus-group-buffer)
6344     (unless quit-config
6345       (gnus-group-jump-to-group group))
6346     (gnus-run-hooks 'gnus-summary-exit-hook)
6347     (unless (or quit-config
6348                 ;; If this group has disappeared from the summary
6349                 ;; buffer, don't skip forwards.
6350                 (not (string= group (gnus-group-group-name))))
6351       (gnus-group-next-unread-group 1))
6352     (setq group-point (point))
6353     (if temporary
6354         nil                             ;Nothing to do.
6355       ;; If we have several article buffers, we kill them at exit.
6356       (unless gnus-single-article-buffer
6357         (gnus-kill-buffer gnus-article-buffer)
6358         (gnus-kill-buffer gnus-original-article-buffer)
6359         (setq gnus-article-current nil))
6360       (set-buffer buf)
6361       (if (not gnus-kill-summary-on-exit)
6362           (progn
6363             (gnus-deaden-summary)
6364             (setq mode nil))
6365        ;; We set all buffer-local variables to nil.  It is unclear why
6366         ;; this is needed, but if we don't, buffer-local variables are
6367         ;; not garbage-collected, it seems.  This would the lead to en
6368         ;; ever-growing Emacs.
6369         (gnus-summary-clear-local-variables)
6370         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6371           (gnus-summary-clear-local-variables))
6372         (when (get-buffer gnus-article-buffer)
6373           (bury-buffer gnus-article-buffer))
6374         ;; We clear the global counterparts of the buffer-local
6375         ;; variables as well, just to be on the safe side.
6376         (set-buffer gnus-group-buffer)
6377         (gnus-summary-clear-local-variables)
6378         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6379           (gnus-summary-clear-local-variables)))
6380       (setq gnus-current-select-method gnus-select-method)
6381       (pop-to-buffer gnus-group-buffer)
6382       (if (not quit-config)
6383           (progn
6384             (goto-char group-point)
6385             (gnus-configure-windows 'group 'force))
6386         (gnus-handle-ephemeral-exit quit-config))
6387       ;; Return to group mode buffer.
6388       (when (eq mode 'gnus-summary-mode)
6389         (gnus-kill-buffer buf))
6390       ;; Clear the current group name.
6391       (unless quit-config
6392         (setq gnus-newsgroup-name nil)))))
6393
6394 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6395 (defun gnus-summary-exit-no-update (&optional no-questions)
6396   "Quit reading current newsgroup without updating read article info."
6397   (interactive)
6398   (let* ((group gnus-newsgroup-name)
6399          (quit-config (gnus-group-quit-config group)))
6400     (when (or no-questions
6401               gnus-expert-user
6402               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6403       (gnus-async-halt-prefetch)
6404       (mapcar 'funcall
6405               (delq 'gnus-summary-expire-articles
6406                     (copy-sequence gnus-summary-prepare-exit-hook)))
6407       (when (gnus-buffer-live-p gnus-article-buffer)
6408         (save-excursion
6409           (set-buffer gnus-article-buffer)
6410           (mm-destroy-parts gnus-article-mime-handles)
6411           ;; Set it to nil for safety reason.
6412           (setq gnus-article-mime-handle-alist nil)
6413           (setq gnus-article-mime-handles nil)))
6414       ;; If we have several article buffers, we kill them at exit.
6415       (unless gnus-single-article-buffer
6416         (gnus-kill-buffer gnus-article-buffer)
6417         (gnus-kill-buffer gnus-original-article-buffer)
6418         (setq gnus-article-current nil))
6419       (if (not gnus-kill-summary-on-exit)
6420           (gnus-deaden-summary)
6421         (gnus-close-group group)
6422         (gnus-summary-clear-local-variables)
6423         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6424           (gnus-summary-clear-local-variables))
6425         (set-buffer gnus-group-buffer)
6426         (gnus-summary-clear-local-variables)
6427         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6428           (gnus-summary-clear-local-variables))
6429         (when (get-buffer gnus-summary-buffer)
6430           (kill-buffer gnus-summary-buffer)))
6431       (unless gnus-single-article-buffer
6432         (setq gnus-article-current nil))
6433       (when gnus-use-trees
6434         (gnus-tree-close group))
6435       (gnus-async-prefetch-remove-group group)
6436       (when (get-buffer gnus-article-buffer)
6437         (bury-buffer gnus-article-buffer))
6438       ;; Return to the group buffer.
6439       (gnus-configure-windows 'group 'force)
6440       ;; Clear the current group name.
6441       (setq gnus-newsgroup-name nil)
6442       (when (equal (gnus-group-group-name) group)
6443         (gnus-group-next-unread-group 1))
6444       (when quit-config
6445         (gnus-handle-ephemeral-exit quit-config)))))
6446
6447 (defun gnus-handle-ephemeral-exit (quit-config)
6448   "Handle movement when leaving an ephemeral group.
6449 The state which existed when entering the ephemeral is reset."
6450   (if (not (buffer-name (car quit-config)))
6451       (gnus-configure-windows 'group 'force)
6452     (set-buffer (car quit-config))
6453     (cond ((eq major-mode 'gnus-summary-mode)
6454            (gnus-set-global-variables))
6455           ((eq major-mode 'gnus-article-mode)
6456            (save-excursion
6457              ;; The `gnus-summary-buffer' variable may point
6458              ;; to the old summary buffer when using a single
6459              ;; article buffer.
6460              (unless (gnus-buffer-live-p gnus-summary-buffer)
6461                (set-buffer gnus-group-buffer))
6462              (set-buffer gnus-summary-buffer)
6463              (gnus-set-global-variables))))
6464     (if (or (eq (cdr quit-config) 'article)
6465             (eq (cdr quit-config) 'pick))
6466         (progn
6467           ;; The current article may be from the ephemeral group
6468           ;; thus it is best that we reload this article
6469           (gnus-summary-show-article)
6470           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6471               (gnus-configure-windows 'pick 'force)
6472             (gnus-configure-windows (cdr quit-config) 'force)))
6473       (gnus-configure-windows (cdr quit-config) 'force))
6474     (when (eq major-mode 'gnus-summary-mode)
6475       (gnus-summary-next-subject 1 nil t)
6476       (gnus-summary-recenter)
6477       (gnus-summary-position-point))))
6478
6479 ;;; Dead summaries.
6480
6481 (defvar gnus-dead-summary-mode-map nil)
6482
6483 (unless gnus-dead-summary-mode-map
6484   (setq gnus-dead-summary-mode-map (make-keymap))
6485   (suppress-keymap gnus-dead-summary-mode-map)
6486   (substitute-key-definition
6487    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6488   (dolist (key '("\C-d" "\r" "\177" [delete]))
6489     (define-key gnus-dead-summary-mode-map
6490       key 'gnus-summary-wake-up-the-dead))
6491   (dolist (key '("q" "Q"))
6492     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6493
6494 (defvar gnus-dead-summary-mode nil
6495   "Minor mode for Gnus summary buffers.")
6496
6497 (defun gnus-dead-summary-mode (&optional arg)
6498   "Minor mode for Gnus summary buffers."
6499   (interactive "P")
6500   (when (eq major-mode 'gnus-summary-mode)
6501     (make-local-variable 'gnus-dead-summary-mode)
6502     (setq gnus-dead-summary-mode
6503           (if (null arg) (not gnus-dead-summary-mode)
6504             (> (prefix-numeric-value arg) 0)))
6505     (when gnus-dead-summary-mode
6506       (gnus-add-minor-mode
6507        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6508
6509 (defun gnus-deaden-summary ()
6510   "Make the current summary buffer into a dead summary buffer."
6511   ;; Kill any previous dead summary buffer.
6512   (when (and gnus-dead-summary
6513              (buffer-name gnus-dead-summary))
6514     (save-excursion
6515       (set-buffer gnus-dead-summary)
6516       (when gnus-dead-summary-mode
6517         (kill-buffer (current-buffer)))))
6518   ;; Make this the current dead summary.
6519   (setq gnus-dead-summary (current-buffer))
6520   (gnus-dead-summary-mode 1)
6521   (let ((name (buffer-name)))
6522     (when (string-match "Summary" name)
6523       (rename-buffer
6524        (concat (substring name 0 (match-beginning 0)) "Dead "
6525                (substring name (match-beginning 0)))
6526        t)
6527       (bury-buffer))))
6528
6529 (defun gnus-kill-or-deaden-summary (buffer)
6530   "Kill or deaden the summary BUFFER."
6531   (save-excursion
6532     (when (and (buffer-name buffer)
6533                (not gnus-single-article-buffer))
6534       (save-excursion
6535         (set-buffer buffer)
6536         (gnus-kill-buffer gnus-article-buffer)
6537         (gnus-kill-buffer gnus-original-article-buffer)))
6538     (cond
6539      ;; Kill the buffer.
6540      (gnus-kill-summary-on-exit
6541       (when (and gnus-use-trees
6542                  (gnus-buffer-exists-p buffer))
6543         (save-excursion
6544           (set-buffer buffer)
6545           (gnus-tree-close gnus-newsgroup-name)))
6546       (gnus-kill-buffer buffer))
6547      ;; Deaden the buffer.
6548      ((gnus-buffer-exists-p buffer)
6549       (save-excursion
6550         (set-buffer buffer)
6551         (gnus-deaden-summary))))))
6552
6553 (defun gnus-summary-wake-up-the-dead (&rest args)
6554   "Wake up the dead summary buffer."
6555   (interactive)
6556   (gnus-dead-summary-mode -1)
6557   (let ((name (buffer-name)))
6558     (when (string-match "Dead " name)
6559       (rename-buffer
6560        (concat (substring name 0 (match-beginning 0))
6561                (substring name (match-end 0)))
6562        t)))
6563   (gnus-message 3 "This dead summary is now alive again"))
6564
6565 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6566 (defun gnus-summary-fetch-faq (&optional faq-dir)
6567   "Fetch the FAQ for the current group.
6568 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6569 in."
6570   (interactive
6571    (list
6572     (when current-prefix-arg
6573       (completing-read
6574        "FAQ dir: " (and (listp gnus-group-faq-directory)
6575                         (mapcar (lambda (file) (list file))
6576                                 gnus-group-faq-directory))))))
6577   (let (gnus-faq-buffer)
6578     (when (setq gnus-faq-buffer
6579                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6580       (gnus-configure-windows 'summary-faq))))
6581
6582 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6583 (defun gnus-summary-describe-group (&optional force)
6584   "Describe the current newsgroup."
6585   (interactive "P")
6586   (gnus-group-describe-group force gnus-newsgroup-name))
6587
6588 (defun gnus-summary-describe-briefly ()
6589   "Describe summary mode commands briefly."
6590   (interactive)
6591   (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")))
6592
6593 ;; Walking around group mode buffer from summary mode.
6594
6595 (defun gnus-summary-next-group (&optional no-article target-group backward)
6596   "Exit current newsgroup and then select next unread newsgroup.
6597 If prefix argument NO-ARTICLE is non-nil, no article is selected
6598 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6599 previous group instead."
6600   (interactive "P")
6601   ;; Stop pre-fetching.
6602   (gnus-async-halt-prefetch)
6603   (let ((current-group gnus-newsgroup-name)
6604         (current-buffer (current-buffer))
6605         entered)
6606     (while (not entered)
6607       ;; Then we find what group we are supposed to enter.
6608       (set-buffer gnus-group-buffer)
6609       (gnus-group-jump-to-group current-group)
6610       (setq target-group
6611             (or target-group
6612                 (if (eq gnus-keep-same-level 'best)
6613                     (gnus-summary-best-group gnus-newsgroup-name)
6614                   (gnus-summary-search-group backward gnus-keep-same-level))))
6615       (if (not target-group)
6616           ;; There are no further groups, so we return to the group
6617           ;; buffer.
6618           (progn
6619             (gnus-message 5 "Returning to the group buffer")
6620             (setq entered t)
6621             (when (gnus-buffer-live-p current-buffer)
6622               (set-buffer current-buffer)
6623               (gnus-summary-exit))
6624             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6625         ;; We try to enter the target group.
6626         (gnus-group-jump-to-group target-group)
6627         (let ((unreads (gnus-group-group-unread)))
6628           (if (and (or (eq t unreads)
6629                        (and unreads (not (zerop unreads))))
6630                    (progn
6631                      ;; Now we semi-exit this group to update Xrefs
6632                      ;; and all variables.  We can't do a real exit,
6633                      ;; because the window conf must remain the same
6634                      ;; in case the user is prompted for info, and we
6635                      ;; don't want the window conf to change before
6636                      ;; that...
6637                      (when (gnus-buffer-live-p current-buffer)
6638                        (set-buffer current-buffer)
6639                        (gnus-summary-exit t))
6640                      (gnus-summary-read-group
6641                       target-group nil no-article
6642                       (and (buffer-name current-buffer) current-buffer)
6643                       nil backward)))
6644               (setq entered t)
6645             (setq current-group target-group
6646                   target-group nil)))))))
6647
6648 (defun gnus-summary-prev-group (&optional no-article)
6649   "Exit current newsgroup and then select previous unread newsgroup.
6650 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6651   (interactive "P")
6652   (gnus-summary-next-group no-article nil t))
6653
6654 ;; Walking around summary lines.
6655
6656 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6657   "Go to the first unread subject.
6658 If UNREAD is non-nil, go to the first unread article.
6659 Returns the article selected or nil if there are no unread articles."
6660   (interactive "P")
6661   (prog1
6662       (cond
6663        ;; Empty summary.
6664        ((null gnus-newsgroup-data)
6665         (gnus-message 3 "No articles in the group")
6666         nil)
6667        ;; Pick the first article.
6668        ((not unread)
6669         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6670         (gnus-data-number (car gnus-newsgroup-data)))
6671        ;; No unread articles.
6672        ((null gnus-newsgroup-unreads)
6673         (gnus-message 3 "No more unread articles")
6674         nil)
6675        ;; Find the first unread article.
6676        (t
6677         (let ((data gnus-newsgroup-data))
6678           (while (and data
6679                       (and (not (and undownloaded
6680                                      (memq (car data) gnus-newsgroup-undownloaded)))
6681                            (if unseen
6682                                (or (not (memq
6683                                          (gnus-data-number (car data))
6684                                          gnus-newsgroup-unseen))
6685                                    (not (gnus-data-unread-p (car data))))
6686                              (not (gnus-data-unread-p (car data))))))
6687             (setq data (cdr data)))
6688           (when data
6689             (goto-char (gnus-data-pos (car data)))
6690             (gnus-data-number (car data))))))
6691     (gnus-summary-position-point)))
6692
6693 (defun gnus-summary-next-subject (n &optional unread dont-display)
6694   "Go to next N'th summary line.
6695 If N is negative, go to the previous N'th subject line.
6696 If UNREAD is non-nil, only unread articles are selected.
6697 The difference between N and the actual number of steps taken is
6698 returned."
6699   (interactive "p")
6700   (let ((backward (< n 0))
6701         (n (abs n)))
6702     (while (and (> n 0)
6703                 (if backward
6704                     (gnus-summary-find-prev unread)
6705                   (gnus-summary-find-next unread)))
6706       (unless (zerop (setq n (1- n)))
6707         (gnus-summary-show-thread)))
6708     (when (/= 0 n)
6709       (gnus-message 7 "No more%s articles"
6710                     (if unread " unread" "")))
6711     (unless dont-display
6712       (gnus-summary-recenter)
6713       (gnus-summary-position-point))
6714     n))
6715
6716 (defun gnus-summary-next-unread-subject (n)
6717   "Go to next N'th unread summary line."
6718   (interactive "p")
6719   (gnus-summary-next-subject n t))
6720
6721 (defun gnus-summary-prev-subject (n &optional unread)
6722   "Go to previous N'th summary line.
6723 If optional argument UNREAD is non-nil, only unread article is selected."
6724   (interactive "p")
6725   (gnus-summary-next-subject (- n) unread))
6726
6727 (defun gnus-summary-prev-unread-subject (n)
6728   "Go to previous N'th unread summary line."
6729   (interactive "p")
6730   (gnus-summary-next-subject (- n) t))
6731
6732 (defun gnus-summary-goto-subjects (articles)
6733   "Insert the subject header for ARTICLES in the current buffer."
6734   (save-excursion
6735     (dolist (article articles)
6736       (gnus-summary-goto-subject articles t)))
6737   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6738   (gnus-summary-position-point))
6739   
6740 (defun gnus-summary-goto-subject (article &optional force silent)
6741   "Go the subject line of ARTICLE.
6742 If FORCE, also allow jumping to articles not currently shown."
6743   (interactive "nArticle number: ")
6744   (unless (numberp article)
6745     (error "Article %s is not a number" article))
6746   (let ((b (point))
6747         (data (gnus-data-find article)))
6748     ;; We read in the article if we have to.
6749     (and (not data)
6750          force
6751          (gnus-summary-insert-subject
6752           article
6753           (if (or (numberp force) (vectorp force)) force)
6754           t)
6755          (setq data (gnus-data-find article)))
6756     (goto-char b)
6757     (if (not data)
6758         (progn
6759           (unless silent
6760             (gnus-message 3 "Can't find article %d" article))
6761           nil)
6762       (let ((pt (gnus-data-pos data)))
6763         (goto-char pt)
6764         (gnus-summary-set-article-display-arrow pt))
6765       (gnus-summary-position-point)
6766       article)))
6767
6768 ;; Walking around summary lines with displaying articles.
6769
6770 (defun gnus-summary-expand-window (&optional arg)
6771   "Make the summary buffer take up the entire Emacs frame.
6772 Given a prefix, will force an `article' buffer configuration."
6773   (interactive "P")
6774   (if arg
6775       (gnus-configure-windows 'article 'force)
6776     (gnus-configure-windows 'summary 'force)))
6777
6778 (defun gnus-summary-display-article (article &optional all-header)
6779   "Display ARTICLE in article buffer."
6780   (when (gnus-buffer-live-p gnus-article-buffer)
6781     (with-current-buffer gnus-article-buffer
6782       (mm-enable-multibyte)))
6783   (gnus-set-global-variables)
6784   (when (gnus-buffer-live-p gnus-article-buffer)
6785     (with-current-buffer gnus-article-buffer
6786       (setq gnus-article-charset gnus-newsgroup-charset)
6787       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6788       (mm-enable-multibyte)))
6789   (if (null article)
6790       nil
6791     (prog1
6792         (if gnus-summary-display-article-function
6793             (funcall gnus-summary-display-article-function article all-header)
6794           (gnus-article-prepare article all-header))
6795       (gnus-run-hooks 'gnus-select-article-hook)
6796       (when (and gnus-current-article
6797                  (not (zerop gnus-current-article)))
6798         (gnus-summary-goto-subject gnus-current-article))
6799       (gnus-summary-recenter)
6800       (when (and gnus-use-trees gnus-show-threads)
6801         (gnus-possibly-generate-tree article)
6802         (gnus-highlight-selected-tree article))
6803       ;; Successfully display article.
6804       (gnus-article-set-window-start
6805        (cdr (assq article gnus-newsgroup-bookmarks))))))
6806
6807 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6808   "Select the current article.
6809 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6810 non-nil, the article will be re-fetched even if it already present in
6811 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6812 be displayed."
6813   ;; Make sure we are in the summary buffer to work around bbdb bug.
6814   (unless (eq major-mode 'gnus-summary-mode)
6815     (set-buffer gnus-summary-buffer))
6816   (let ((article (or article (gnus-summary-article-number)))
6817         (all-headers (not (not all-headers))) ;Must be t or nil.
6818         gnus-summary-display-article-function)
6819     (and (not pseudo)
6820          (gnus-summary-article-pseudo-p article)
6821          (error "This is a pseudo-article"))
6822     (save-excursion
6823       (set-buffer gnus-summary-buffer)
6824       (if (or (and gnus-single-article-buffer
6825                    (or (null gnus-current-article)
6826                        (null gnus-article-current)
6827                        (null (get-buffer gnus-article-buffer))
6828                        (not (eq article (cdr gnus-article-current)))
6829                        (not (equal (car gnus-article-current)
6830                                    gnus-newsgroup-name))))
6831               (and (not gnus-single-article-buffer)
6832                    (or (null gnus-current-article)
6833                        (not (eq gnus-current-article article))))
6834               force)
6835           ;; The requested article is different from the current article.
6836           (progn
6837             (gnus-summary-display-article article all-headers)
6838             (when (gnus-buffer-live-p gnus-article-buffer)
6839               (with-current-buffer gnus-article-buffer
6840                 (if (not gnus-article-decoded-p) ;; a local variable
6841                     (mm-disable-multibyte))))
6842             (gnus-article-set-window-start
6843              (cdr (assq article gnus-newsgroup-bookmarks)))
6844             article)
6845         'old))))
6846
6847 (defun gnus-summary-force-verify-and-decrypt ()
6848   (interactive)
6849   (let ((mm-verify-option 'known)
6850         (mm-decrypt-option 'known)
6851         (gnus-buttonized-mime-types (append (list "multipart/signed"
6852                                                   "multipart/encrypted")
6853                                             gnus-buttonized-mime-types)))
6854     (gnus-summary-select-article nil 'force)))
6855
6856 (defun gnus-summary-set-current-mark (&optional current-mark)
6857   "Obsolete function."
6858   nil)
6859
6860 (defun gnus-summary-next-article (&optional unread subject backward push)
6861   "Select the next article.
6862 If UNREAD, only unread articles are selected.
6863 If SUBJECT, only articles with SUBJECT are selected.
6864 If BACKWARD, the previous article is selected instead of the next."
6865   (interactive "P")
6866   (cond
6867    ;; Is there such an article?
6868    ((and (gnus-summary-search-forward unread subject backward)
6869          (or (gnus-summary-display-article (gnus-summary-article-number))
6870              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6871     (gnus-summary-position-point))
6872    ;; If not, we try the first unread, if that is wanted.
6873    ((and subject
6874          gnus-auto-select-same
6875          (gnus-summary-first-unread-article))
6876     (gnus-summary-position-point)
6877     (gnus-message 6 "Wrapped"))
6878    ;; Try to get next/previous article not displayed in this group.
6879    ((and gnus-auto-extend-newsgroup
6880          (not unread) (not subject))
6881     (gnus-summary-goto-article
6882      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6883      nil (count-lines (point-min) (point))))
6884    ;; Go to next/previous group.
6885    (t
6886     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6887       (gnus-summary-jump-to-group gnus-newsgroup-name))
6888     (let ((cmd last-command-char)
6889           (point
6890            (save-excursion
6891              (set-buffer gnus-group-buffer)
6892              (point)))
6893           (group
6894            (if (eq gnus-keep-same-level 'best)
6895                (gnus-summary-best-group gnus-newsgroup-name)
6896              (gnus-summary-search-group backward gnus-keep-same-level))))
6897       ;; For some reason, the group window gets selected.  We change
6898       ;; it back.
6899       (select-window (get-buffer-window (current-buffer)))
6900       ;; Select next unread newsgroup automagically.
6901       (cond
6902        ((or (not gnus-auto-select-next)
6903             (not cmd))
6904         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6905        ((or (eq gnus-auto-select-next 'quietly)
6906             (and (eq gnus-auto-select-next 'slightly-quietly)
6907                  push)
6908             (and (eq gnus-auto-select-next 'almost-quietly)
6909                  (gnus-summary-last-article-p)))
6910         ;; Select quietly.
6911         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6912             (gnus-summary-exit)
6913           (gnus-message 7 "No more%s articles (%s)..."
6914                         (if unread " unread" "")
6915                         (if group (concat "selecting " group)
6916                           "exiting"))
6917           (gnus-summary-next-group nil group backward)))
6918        (t
6919         (when (gnus-key-press-event-p last-input-event)
6920           (gnus-summary-walk-group-buffer
6921            gnus-newsgroup-name cmd unread backward point))))))))
6922
6923 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6924   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6925                       (?\C-p (gnus-group-prev-unread-group 1))))
6926         (cursor-in-echo-area t)
6927         keve key group ended prompt)
6928     (save-excursion
6929       (set-buffer gnus-group-buffer)
6930       (goto-char start)
6931       (setq group
6932             (if (eq gnus-keep-same-level 'best)
6933                 (gnus-summary-best-group gnus-newsgroup-name)
6934               (gnus-summary-search-group backward gnus-keep-same-level))))
6935     (while (not ended)
6936       (setq prompt
6937             (format
6938              "No more%s articles%s " (if unread " unread" "")
6939              (if (and group
6940                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6941                  (format " (Type %s for %s [%s])"
6942                          (single-key-description cmd) group
6943                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6944                (format " (Type %s to exit %s)"
6945                        (single-key-description cmd)
6946                        gnus-newsgroup-name))))
6947       ;; Confirm auto selection.
6948       (setq key (car (setq keve (gnus-read-event-char prompt)))
6949             ended t)
6950       (cond
6951        ((assq key keystrokes)
6952         (let ((obuf (current-buffer)))
6953           (switch-to-buffer gnus-group-buffer)
6954           (when group
6955             (gnus-group-jump-to-group group))
6956           (eval (cadr (assq key keystrokes)))
6957           (setq group (gnus-group-group-name))
6958           (switch-to-buffer obuf))
6959         (setq ended nil))
6960        ((equal key cmd)
6961         (if (or (not group)
6962                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6963             (gnus-summary-exit)
6964           (gnus-summary-next-group nil group backward)))
6965        (t
6966         (push (cdr keve) unread-command-events))))))
6967
6968 (defun gnus-summary-next-unread-article ()
6969   "Select unread article after current one."
6970   (interactive)
6971   (gnus-summary-next-article
6972    (or (not (eq gnus-summary-goto-unread 'never))
6973        (gnus-summary-last-article-p (gnus-summary-article-number)))
6974    (and gnus-auto-select-same
6975         (gnus-summary-article-subject))))
6976
6977 (defun gnus-summary-prev-article (&optional unread subject)
6978   "Select the article after the current one.
6979 If UNREAD is non-nil, only unread articles are selected."
6980   (interactive "P")
6981   (gnus-summary-next-article unread subject t))
6982
6983 (defun gnus-summary-prev-unread-article ()
6984   "Select unread article before current one."
6985   (interactive)
6986   (gnus-summary-prev-article
6987    (or (not (eq gnus-summary-goto-unread 'never))
6988        (gnus-summary-first-article-p (gnus-summary-article-number)))
6989    (and gnus-auto-select-same
6990         (gnus-summary-article-subject))))
6991
6992 (defun gnus-summary-next-page (&optional lines circular stop)
6993   "Show next page of the selected article.
6994 If at the end of the current article, select the next article.
6995 LINES says how many lines should be scrolled up.
6996
6997 If CIRCULAR is non-nil, go to the start of the article instead of
6998 selecting the next article when reaching the end of the current
6999 article.
7000
7001 If STOP is non-nil, just stop when reaching the end of the message."
7002   (interactive "P")
7003   (setq gnus-summary-buffer (current-buffer))
7004   (gnus-set-global-variables)
7005   (let ((article (gnus-summary-article-number))
7006         (article-window (get-buffer-window gnus-article-buffer t))
7007         endp)
7008     ;; If the buffer is empty, we have no article.
7009     (unless article
7010       (error "No article to select"))
7011     (gnus-configure-windows 'article)
7012     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7013         (if (and (eq gnus-summary-goto-unread 'never)
7014                  (not (gnus-summary-last-article-p article)))
7015             (gnus-summary-next-article)
7016           (gnus-summary-next-unread-article))
7017       (if (or (null gnus-current-article)
7018               (null gnus-article-current)
7019               (/= article (cdr gnus-article-current))
7020               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7021           ;; Selected subject is different from current article's.
7022           (gnus-summary-display-article article)
7023         (when article-window
7024           (gnus-eval-in-buffer-window gnus-article-buffer
7025             (setq endp (gnus-article-next-page lines)))
7026           (when endp
7027             (cond (stop
7028                    (gnus-message 3 "End of message"))
7029                   (circular
7030                    (gnus-summary-beginning-of-article))
7031                   (lines
7032                    (gnus-message 3 "End of message"))
7033                   ((null lines)
7034                    (if (and (eq gnus-summary-goto-unread 'never)
7035                             (not (gnus-summary-last-article-p article)))
7036                        (gnus-summary-next-article)
7037                      (gnus-summary-next-unread-article))))))))
7038     (gnus-summary-recenter)
7039     (gnus-summary-position-point)))
7040
7041 (defun gnus-summary-prev-page (&optional lines move)
7042   "Show previous page of selected article.
7043 Argument LINES specifies lines to be scrolled down.
7044 If MOVE, move to the previous unread article if point is at
7045 the beginning of the buffer."
7046   (interactive "P")
7047   (let ((article (gnus-summary-article-number))
7048         (article-window (get-buffer-window gnus-article-buffer t))
7049         endp)
7050     (gnus-configure-windows 'article)
7051     (if (or (null gnus-current-article)
7052             (null gnus-article-current)
7053             (/= article (cdr gnus-article-current))
7054             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7055         ;; Selected subject is different from current article's.
7056         (gnus-summary-display-article article)
7057       (gnus-summary-recenter)
7058       (when article-window
7059         (gnus-eval-in-buffer-window gnus-article-buffer
7060           (setq endp (gnus-article-prev-page lines)))
7061         (when (and move endp)
7062           (cond (lines
7063                  (gnus-message 3 "Beginning of message"))
7064                 ((null lines)
7065                  (if (and (eq gnus-summary-goto-unread 'never)
7066                           (not (gnus-summary-first-article-p article)))
7067                      (gnus-summary-prev-article)
7068                    (gnus-summary-prev-unread-article))))))))
7069   (gnus-summary-position-point))
7070
7071 (defun gnus-summary-prev-page-or-article (&optional lines)
7072   "Show previous page of selected article.
7073 Argument LINES specifies lines to be scrolled down.
7074 If at the beginning of the article, go to the next article."
7075   (interactive "P")
7076   (gnus-summary-prev-page lines t))
7077
7078 (defun gnus-summary-scroll-up (lines)
7079   "Scroll up (or down) one line current article.
7080 Argument LINES specifies lines to be scrolled up (or down if negative)."
7081   (interactive "p")
7082   (gnus-configure-windows 'article)
7083   (gnus-summary-show-thread)
7084   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7085     (gnus-eval-in-buffer-window gnus-article-buffer
7086       (cond ((> lines 0)
7087              (when (gnus-article-next-page lines)
7088                (gnus-message 3 "End of message")))
7089             ((< lines 0)
7090              (gnus-article-prev-page (- lines))))))
7091   (gnus-summary-recenter)
7092   (gnus-summary-position-point))
7093
7094 (defun gnus-summary-scroll-down (lines)
7095   "Scroll down (or up) one line current article.
7096 Argument LINES specifies lines to be scrolled down (or up if negative)."
7097   (interactive "p")
7098   (gnus-summary-scroll-up (- lines)))
7099
7100 (defun gnus-summary-next-same-subject ()
7101   "Select next article which has the same subject as current one."
7102   (interactive)
7103   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7104
7105 (defun gnus-summary-prev-same-subject ()
7106   "Select previous article which has the same subject as current one."
7107   (interactive)
7108   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7109
7110 (defun gnus-summary-next-unread-same-subject ()
7111   "Select next unread article which has the same subject as current one."
7112   (interactive)
7113   (gnus-summary-next-article t (gnus-summary-article-subject)))
7114
7115 (defun gnus-summary-prev-unread-same-subject ()
7116   "Select previous unread article which has the same subject as current one."
7117   (interactive)
7118   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7119
7120 (defun gnus-summary-first-unread-article ()
7121   "Select the first unread article.
7122 Return nil if there are no unread articles."
7123   (interactive)
7124   (prog1
7125       (when (gnus-summary-first-subject t)
7126         (gnus-summary-show-thread)
7127         (gnus-summary-first-subject t)
7128         (gnus-summary-display-article (gnus-summary-article-number)))
7129     (gnus-summary-position-point)))
7130
7131 (defun gnus-summary-first-unread-subject ()
7132   "Place the point on the subject line of the first unread article.
7133 Return nil if there are no unread articles."
7134   (interactive)
7135   (prog1
7136       (when (gnus-summary-first-subject t)
7137         (gnus-summary-show-thread)
7138         (gnus-summary-first-subject t))
7139     (gnus-summary-position-point)))
7140
7141 (defun gnus-summary-first-unseen-subject ()
7142   "Place the point on the subject line of the first unseen article.
7143 Return nil if there are no unseen articles."
7144   (interactive)
7145   (prog1
7146       (when (gnus-summary-first-subject t t t)
7147         (gnus-summary-show-thread)
7148         (gnus-summary-first-subject t t t))
7149     (gnus-summary-position-point)))
7150
7151 (defun gnus-summary-first-unseen-or-unread-subject ()
7152   "Place the point on the subject line of the first unseen article.
7153 Return nil if there are no unseen articles."
7154   (interactive)
7155   (prog1
7156       (unless (when (gnus-summary-first-subject t t t)
7157                 (gnus-summary-show-thread)
7158                 (gnus-summary-first-subject t t t))
7159         (when (gnus-summary-first-subject t)
7160           (gnus-summary-show-thread)
7161           (gnus-summary-first-subject t)))
7162     (gnus-summary-position-point)))
7163
7164 (defun gnus-summary-first-article ()
7165   "Select the first article.
7166 Return nil if there are no articles."
7167   (interactive)
7168   (prog1
7169       (when (gnus-summary-first-subject)
7170         (gnus-summary-show-thread)
7171         (gnus-summary-first-subject)
7172         (gnus-summary-display-article (gnus-summary-article-number)))
7173     (gnus-summary-position-point)))
7174
7175 (defun gnus-summary-best-unread-article (&optional arg)
7176   "Select the unread article with the highest score.
7177 If given a prefix argument, select the next unread article that has a
7178 score higher than the default score."
7179   (interactive "P")
7180   (let ((article (if arg
7181                      (gnus-summary-better-unread-subject)
7182                    (gnus-summary-best-unread-subject))))
7183     (if article
7184         (gnus-summary-goto-article article)
7185       (error "No unread articles"))))
7186
7187 (defun gnus-summary-best-unread-subject ()
7188   "Select the unread subject with the highest score."
7189   (interactive)
7190   (let ((best -1000000)
7191         (data gnus-newsgroup-data)
7192         article score)
7193     (while data
7194       (and (gnus-data-unread-p (car data))
7195            (> (setq score
7196                     (gnus-summary-article-score (gnus-data-number (car data))))
7197               best)
7198            (setq best score
7199                  article (gnus-data-number (car data))))
7200       (setq data (cdr data)))
7201     (when article
7202       (gnus-summary-goto-subject article))
7203     (gnus-summary-position-point)
7204     article))
7205
7206 (defun gnus-summary-better-unread-subject ()
7207   "Select the first unread subject that has a score over the default score."
7208   (interactive)
7209   (let ((data gnus-newsgroup-data)
7210         article score)
7211     (while (and (setq article (gnus-data-number (car data)))
7212                 (or (gnus-data-read-p (car data))
7213                     (not (> (gnus-summary-article-score article)
7214                             gnus-summary-default-score))))
7215       (setq data (cdr data)))
7216     (when article
7217       (gnus-summary-goto-subject article))
7218     (gnus-summary-position-point)
7219     article))
7220
7221 (defun gnus-summary-last-subject ()
7222   "Go to the last displayed subject line in the group."
7223   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7224     (when article
7225       (gnus-summary-goto-subject article))))
7226
7227 (defun gnus-summary-goto-article (article &optional all-headers force)
7228   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7229 If ALL-HEADERS is non-nil, no header lines are hidden.
7230 If FORCE, go to the article even if it isn't displayed.  If FORCE
7231 is a number, it is the line the article is to be displayed on."
7232   (interactive
7233    (list
7234     (completing-read
7235      "Article number or Message-ID: "
7236      (mapcar (lambda (number) (list (int-to-string number)))
7237              gnus-newsgroup-limit))
7238     current-prefix-arg
7239     t))
7240   (prog1
7241       (if (and (stringp article)
7242                (string-match "@" article))
7243           (gnus-summary-refer-article article)
7244         (when (stringp article)
7245           (setq article (string-to-number article)))
7246         (if (gnus-summary-goto-subject article force)
7247             (gnus-summary-display-article article all-headers)
7248           (gnus-message 4 "Couldn't go to article %s" article) nil))
7249     (gnus-summary-position-point)))
7250
7251 (defun gnus-summary-goto-last-article ()
7252   "Go to the previously read article."
7253   (interactive)
7254   (prog1
7255       (when gnus-last-article
7256         (gnus-summary-goto-article gnus-last-article nil t))
7257     (gnus-summary-position-point)))
7258
7259 (defun gnus-summary-pop-article (number)
7260   "Pop one article off the history and go to the previous.
7261 NUMBER articles will be popped off."
7262   (interactive "p")
7263   (let (to)
7264     (setq gnus-newsgroup-history
7265           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7266     (if to
7267         (gnus-summary-goto-article (car to) nil t)
7268       (error "Article history empty")))
7269   (gnus-summary-position-point))
7270
7271 ;; Summary commands and functions for limiting the summary buffer.
7272
7273 (defun gnus-summary-limit-to-articles (n)
7274   "Limit the summary buffer to the next N articles.
7275 If not given a prefix, use the process marked articles instead."
7276   (interactive "P")
7277   (prog1
7278       (let ((articles (gnus-summary-work-articles n)))
7279         (setq gnus-newsgroup-processable nil)
7280         (gnus-summary-limit articles))
7281     (gnus-summary-position-point)))
7282
7283 (defun gnus-summary-pop-limit (&optional total)
7284   "Restore the previous limit.
7285 If given a prefix, remove all limits."
7286   (interactive "P")
7287   (when total
7288     (setq gnus-newsgroup-limits
7289           (list (mapcar (lambda (h) (mail-header-number h))
7290                         gnus-newsgroup-headers))))
7291   (unless gnus-newsgroup-limits
7292     (error "No limit to pop"))
7293   (prog1
7294       (gnus-summary-limit nil 'pop)
7295     (gnus-summary-position-point)))
7296
7297 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7298   "Limit the summary buffer to articles that have subjects that match a regexp.
7299 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7300   (interactive
7301    (list (read-string (if current-prefix-arg
7302                           "Exclude subject (regexp): "
7303                         "Limit to subject (regexp): "))
7304          nil current-prefix-arg))
7305   (unless header
7306     (setq header "subject"))
7307   (when (not (equal "" subject))
7308     (prog1
7309         (let ((articles (gnus-summary-find-matching
7310                          (or header "subject") subject 'all nil nil
7311                          not-matching)))
7312           (unless articles
7313             (error "Found no matches for \"%s\"" subject))
7314           (gnus-summary-limit articles))
7315       (gnus-summary-position-point))))
7316
7317 (defun gnus-summary-limit-to-author (from &optional not-matching)
7318   "Limit the summary buffer to articles that have authors that match a regexp.
7319 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7320   (interactive
7321    (list (read-string (if current-prefix-arg
7322                           "Exclude author (regexp): "
7323                         "Limit to author (regexp): "))
7324          current-prefix-arg))
7325   (gnus-summary-limit-to-subject from "from" not-matching))
7326
7327 (defun gnus-summary-limit-to-age (age &optional younger-p)
7328   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7329 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7330 articles that are younger than AGE days."
7331   (interactive
7332    (let ((younger current-prefix-arg)
7333          (days-got nil)
7334          days)
7335      (while (not days-got)
7336        (setq days (if younger
7337                       (read-string "Limit to articles younger than (in days, older when negative): ")
7338                     (read-string
7339                      "Limit to articles older than (in days, younger when negative): ")))
7340        (when (> (length days) 0)
7341          (setq days (read days)))
7342        (if (numberp days)
7343            (progn
7344              (setq days-got t)
7345              (if (< days 0)
7346                  (progn
7347                    (setq younger (not younger))
7348                    (setq days (* days -1)))))
7349          (message "Please enter a number.")
7350          (sleep-for 1)))
7351      (list days younger)))
7352   (prog1
7353       (let ((data gnus-newsgroup-data)
7354             (cutoff (days-to-time age))
7355             articles d date is-younger)
7356         (while (setq d (pop data))
7357           (when (and (vectorp (gnus-data-header d))
7358                      (setq date (mail-header-date (gnus-data-header d))))
7359             (setq is-younger (time-less-p
7360                               (time-since (condition-case ()
7361                                               (date-to-time date)
7362                                             (error '(0 0))))
7363                               cutoff))
7364             (when (if younger-p
7365                       is-younger
7366                     (not is-younger))
7367               (push (gnus-data-number d) articles))))
7368         (gnus-summary-limit (nreverse articles)))
7369     (gnus-summary-position-point)))
7370
7371 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7372   "Limit the summary buffer to articles that match an 'extra' header."
7373   (interactive
7374    (let ((header
7375           (intern
7376            (gnus-completing-read-with-default
7377             (symbol-name (car gnus-extra-headers))
7378             (if current-prefix-arg
7379                 "Exclude extra header:"
7380               "Limit extra header:")
7381             (mapcar (lambda (x)
7382                       (cons (symbol-name x) x))
7383                     gnus-extra-headers)
7384             nil
7385             t))))
7386      (list header
7387            (read-string (format "%s header %s (regexp): "
7388                                 (if current-prefix-arg "Exclude" "Limit to")
7389                                 header))
7390            current-prefix-arg)))
7391   (when (not (equal "" regexp))
7392     (prog1
7393         (let ((articles (gnus-summary-find-matching
7394                          (cons 'extra header) regexp 'all nil nil
7395                          not-matching)))
7396           (unless articles
7397             (error "Found no matches for \"%s\"" regexp))
7398           (gnus-summary-limit articles))
7399       (gnus-summary-position-point))))
7400
7401 (defun gnus-summary-limit-to-display-predicate ()
7402   "Limit the summary buffer to the predicated in the `display' group parameter."
7403   (interactive)
7404   (unless gnus-newsgroup-display
7405     (error "There is no `display' group parameter"))
7406   (let (articles)
7407     (dolist (number gnus-newsgroup-articles)
7408       (when (funcall gnus-newsgroup-display)
7409         (push number articles)))
7410     (gnus-summary-limit articles))
7411   (gnus-summary-position-point))
7412
7413 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7414 (make-obsolete
7415  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7416
7417 (defun gnus-summary-limit-to-unread (&optional all)
7418   "Limit the summary buffer to articles that are not marked as read.
7419 If ALL is non-nil, limit strictly to unread articles."
7420   (interactive "P")
7421   (if all
7422       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7423     (gnus-summary-limit-to-marks
7424      ;; Concat all the marks that say that an article is read and have
7425      ;; those removed.
7426      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7427            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7428            gnus-low-score-mark gnus-expirable-mark
7429            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7430            gnus-duplicate-mark gnus-souped-mark)
7431      'reverse)))
7432
7433 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7434 (make-obsolete 'gnus-summary-delete-marked-with
7435                'gnus-summary-limit-exclude-marks)
7436
7437 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7438   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7439 If REVERSE, limit the summary buffer to articles that are marked
7440 with MARKS.  MARKS can either be a string of marks or a list of marks.
7441 Returns how many articles were removed."
7442   (interactive "sMarks: ")
7443   (gnus-summary-limit-to-marks marks t))
7444
7445 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7446   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7447 If REVERSE (the prefix), limit the summary buffer to articles that are
7448 not marked with MARKS.  MARKS can either be a string of marks or a
7449 list of marks.
7450 Returns how many articles were removed."
7451   (interactive "sMarks: \nP")
7452   (prog1
7453       (let ((data gnus-newsgroup-data)
7454             (marks (if (listp marks) marks
7455                      (append marks nil))) ; Transform to list.
7456             articles)
7457         (while data
7458           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7459                   (memq (gnus-data-mark (car data)) marks))
7460             (push (gnus-data-number (car data)) articles))
7461           (setq data (cdr data)))
7462         (gnus-summary-limit articles))
7463     (gnus-summary-position-point)))
7464
7465 (defun gnus-summary-limit-to-score (score)
7466   "Limit to articles with score at or above SCORE."
7467   (interactive "NLimit to articles with score of at least: ")
7468   (let ((data gnus-newsgroup-data)
7469         articles)
7470     (while data
7471       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7472                 score)
7473         (push (gnus-data-number (car data)) articles))
7474       (setq data (cdr data)))
7475     (prog1
7476         (gnus-summary-limit articles)
7477       (gnus-summary-position-point))))
7478
7479 (defun gnus-summary-limit-to-unseen ()
7480   "Limit to unseen articles."
7481   (interactive)
7482   (prog1
7483       (gnus-summary-limit gnus-newsgroup-unseen)
7484     (gnus-summary-position-point)))
7485
7486 (defun gnus-summary-limit-include-thread (id)
7487   "Display all the hidden articles that is in the thread with ID in it.
7488 When called interactively, ID is the Message-ID of the current
7489 article."
7490   (interactive (list (mail-header-id (gnus-summary-article-header))))
7491   (let ((articles (gnus-articles-in-thread
7492                    (gnus-id-to-thread (gnus-root-id id)))))
7493     (prog1
7494         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7495       (gnus-summary-limit-include-matching-articles
7496        "subject"
7497        (regexp-quote (gnus-simplify-subject-re
7498                       (mail-header-subject (gnus-id-to-header id)))))
7499       (gnus-summary-position-point))))
7500
7501 (defun gnus-summary-limit-include-matching-articles (header regexp)
7502   "Display all the hidden articles that have HEADERs that match REGEXP."
7503   (interactive (list (read-string "Match on header: ")
7504                      (read-string "Regexp: ")))
7505   (let ((articles (gnus-find-matching-articles header regexp)))
7506     (prog1
7507         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7508       (gnus-summary-position-point))))
7509
7510 (defun gnus-summary-insert-dormant-articles ()
7511   "Insert all the dormat articles for this group into the current buffer."
7512   (interactive)
7513   (let ((gnus-verbose (max 6 gnus-verbose)))
7514     (if (not gnus-newsgroup-dormant)
7515         (gnus-message 3 "No cached articles for this group")
7516       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7517
7518 (defun gnus-summary-limit-include-dormant ()
7519   "Display all the hidden articles that are marked as dormant.
7520 Note that this command only works on a subset of the articles currently
7521 fetched for this group."
7522   (interactive)
7523   (unless gnus-newsgroup-dormant
7524     (error "There are no dormant articles in this group"))
7525   (prog1
7526       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7527     (gnus-summary-position-point)))
7528
7529 (defun gnus-summary-limit-exclude-dormant ()
7530   "Hide all dormant articles."
7531   (interactive)
7532   (prog1
7533       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7534     (gnus-summary-position-point)))
7535
7536 (defun gnus-summary-limit-exclude-childless-dormant ()
7537   "Hide all dormant articles that have no children."
7538   (interactive)
7539   (let ((data (gnus-data-list t))
7540         articles d children)
7541     ;; Find all articles that are either not dormant or have
7542     ;; children.
7543     (while (setq d (pop data))
7544       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7545                 (and (setq children
7546                            (gnus-article-children (gnus-data-number d)))
7547                      (let (found)
7548                        (while children
7549                          (when (memq (car children) articles)
7550                            (setq children nil
7551                                  found t))
7552                          (pop children))
7553                        found)))
7554         (push (gnus-data-number d) articles)))
7555     ;; Do the limiting.
7556     (prog1
7557         (gnus-summary-limit articles)
7558       (gnus-summary-position-point))))
7559
7560 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7561   "Mark all unread excluded articles as read.
7562 If ALL, mark even excluded ticked and dormants as read."
7563   (interactive "P")
7564   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7565   (let ((articles (gnus-sorted-ndifference
7566                    (sort
7567                     (mapcar (lambda (h) (mail-header-number h))
7568                             gnus-newsgroup-headers)
7569                     '<)
7570                    gnus-newsgroup-limit))
7571         article)
7572     (setq gnus-newsgroup-unreads
7573           (gnus-sorted-intersection gnus-newsgroup-unreads
7574                                     gnus-newsgroup-limit))
7575     (if all
7576         (setq gnus-newsgroup-dormant nil
7577               gnus-newsgroup-marked nil
7578               gnus-newsgroup-reads
7579               (nconc
7580                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7581                gnus-newsgroup-reads))
7582       (while (setq article (pop articles))
7583         (unless (or (memq article gnus-newsgroup-dormant)
7584                     (memq article gnus-newsgroup-marked))
7585           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7586
7587 (defun gnus-summary-limit (articles &optional pop)
7588   (if pop
7589       ;; We pop the previous limit off the stack and use that.
7590       (setq articles (car gnus-newsgroup-limits)
7591             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7592     ;; We use the new limit, so we push the old limit on the stack.
7593     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7594   ;; Set the limit.
7595   (setq gnus-newsgroup-limit articles)
7596   (let ((total (length gnus-newsgroup-data))
7597         (data (gnus-data-find-list (gnus-summary-article-number)))
7598         (gnus-summary-mark-below nil)   ; Inhibit this.
7599         found)
7600     ;; This will do all the work of generating the new summary buffer
7601     ;; according to the new limit.
7602     (gnus-summary-prepare)
7603     ;; Hide any threads, possibly.
7604     (gnus-summary-maybe-hide-threads)
7605     ;; Try to return to the article you were at, or one in the
7606     ;; neighborhood.
7607     (when data
7608       ;; We try to find some article after the current one.
7609       (while data
7610         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7611           (setq data nil
7612                 found t))
7613         (setq data (cdr data))))
7614     (unless found
7615       ;; If there is no data, that means that we were after the last
7616       ;; article.  The same goes when we can't find any articles
7617       ;; after the current one.
7618       (goto-char (point-max))
7619       (gnus-summary-find-prev))
7620     (gnus-set-mode-line 'summary)
7621     ;; We return how many articles were removed from the summary
7622     ;; buffer as a result of the new limit.
7623     (- total (length gnus-newsgroup-data))))
7624
7625 (defsubst gnus-invisible-cut-children (threads)
7626   (let ((num 0))
7627     (while threads
7628       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7629         (incf num))
7630       (pop threads))
7631     (< num 2)))
7632
7633 (defsubst gnus-cut-thread (thread)
7634   "Go forwards in the thread until we find an article that we want to display."
7635   (when (or (eq gnus-fetch-old-headers 'some)
7636             (eq gnus-fetch-old-headers 'invisible)
7637             (numberp gnus-fetch-old-headers)
7638             (eq gnus-build-sparse-threads 'some)
7639             (eq gnus-build-sparse-threads 'more))
7640     ;; Deal with old-fetched headers and sparse threads.
7641     (while (and
7642             thread
7643             (or
7644              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7645              (gnus-summary-article-ancient-p
7646               (mail-header-number (car thread))))
7647             (if (or (<= (length (cdr thread)) 1)
7648                     (eq gnus-fetch-old-headers 'invisible))
7649                 (setq gnus-newsgroup-limit
7650                       (delq (mail-header-number (car thread))
7651                             gnus-newsgroup-limit)
7652                       thread (cadr thread))
7653               (when (gnus-invisible-cut-children (cdr thread))
7654                 (let ((th (cdr thread)))
7655                   (while th
7656                     (if (memq (mail-header-number (caar th))
7657                               gnus-newsgroup-limit)
7658                         (setq thread (car th)
7659                               th nil)
7660                       (setq th (cdr th))))))))))
7661   thread)
7662
7663 (defun gnus-cut-threads (threads)
7664   "Cut off all uninteresting articles from the beginning of threads."
7665   (when (or (eq gnus-fetch-old-headers 'some)
7666             (eq gnus-fetch-old-headers 'invisible)
7667             (numberp gnus-fetch-old-headers)
7668             (eq gnus-build-sparse-threads 'some)
7669             (eq gnus-build-sparse-threads 'more))
7670     (let ((th threads))
7671       (while th
7672         (setcar th (gnus-cut-thread (car th)))
7673         (setq th (cdr th)))))
7674   ;; Remove nixed out threads.
7675   (delq nil threads))
7676
7677 (defun gnus-summary-initial-limit (&optional show-if-empty)
7678   "Figure out what the initial limit is supposed to be on group entry.
7679 This entails weeding out unwanted dormants, low-scored articles,
7680 fetch-old-headers verbiage, and so on."
7681   ;; Most groups have nothing to remove.
7682   (if (or gnus-inhibit-limiting
7683           (and (null gnus-newsgroup-dormant)
7684                (eq gnus-newsgroup-display 'gnus-not-ignore)
7685                (not (eq gnus-fetch-old-headers 'some))
7686                (not (numberp gnus-fetch-old-headers))
7687                (not (eq gnus-fetch-old-headers 'invisible))
7688                (null gnus-summary-expunge-below)
7689                (not (eq gnus-build-sparse-threads 'some))
7690                (not (eq gnus-build-sparse-threads 'more))
7691                (null gnus-thread-expunge-below)
7692                (not gnus-use-nocem)))
7693       ()                                ; Do nothing.
7694     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7695     (setq gnus-newsgroup-limit nil)
7696     (mapatoms
7697      (lambda (node)
7698        (unless (car (symbol-value node))
7699          ;; These threads have no parents -- they are roots.
7700          (let ((nodes (cdr (symbol-value node)))
7701                thread)
7702            (while nodes
7703              (if (and gnus-thread-expunge-below
7704                       (< (gnus-thread-total-score (car nodes))
7705                          gnus-thread-expunge-below))
7706                  (gnus-expunge-thread (pop nodes))
7707                (setq thread (pop nodes))
7708                (gnus-summary-limit-children thread))))))
7709      gnus-newsgroup-dependencies)
7710     ;; If this limitation resulted in an empty group, we might
7711     ;; pop the previous limit and use it instead.
7712     (when (and (not gnus-newsgroup-limit)
7713                show-if-empty)
7714       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7715     gnus-newsgroup-limit))
7716
7717 (defun gnus-summary-limit-children (thread)
7718   "Return 1 if this subthread is visible and 0 if it is not."
7719   ;; First we get the number of visible children to this thread.  This
7720   ;; is done by recursing down the thread using this function, so this
7721   ;; will really go down to a leaf article first, before slowly
7722   ;; working its way up towards the root.
7723   (when thread
7724     (let* ((max-lisp-eval-depth 5000)
7725            (children
7726            (if (cdr thread)
7727                (apply '+ (mapcar 'gnus-summary-limit-children
7728                                  (cdr thread)))
7729              0))
7730           (number (mail-header-number (car thread)))
7731           score)
7732       (if (and
7733            (not (memq number gnus-newsgroup-marked))
7734            (or
7735             ;; If this article is dormant and has absolutely no visible
7736             ;; children, then this article isn't visible.
7737             (and (memq number gnus-newsgroup-dormant)
7738                  (zerop children))
7739             ;; If this is "fetch-old-headered" and there is no
7740             ;; visible children, then we don't want this article.
7741             (and (or (eq gnus-fetch-old-headers 'some)
7742                      (numberp gnus-fetch-old-headers))
7743                  (gnus-summary-article-ancient-p number)
7744                  (zerop children))
7745             ;; If this is "fetch-old-headered" and `invisible', then
7746             ;; we don't want this article.
7747             (and (eq gnus-fetch-old-headers 'invisible)
7748                  (gnus-summary-article-ancient-p number))
7749             ;; If this is a sparsely inserted article with no children,
7750             ;; we don't want it.
7751             (and (eq gnus-build-sparse-threads 'some)
7752                  (gnus-summary-article-sparse-p number)
7753                  (zerop children))
7754             ;; If we use expunging, and this article is really
7755             ;; low-scored, then we don't want this article.
7756             (when (and gnus-summary-expunge-below
7757                        (< (setq score
7758                                 (or (cdr (assq number gnus-newsgroup-scored))
7759                                     gnus-summary-default-score))
7760                           gnus-summary-expunge-below))
7761               ;; We increase the expunge-tally here, but that has
7762               ;; nothing to do with the limits, really.
7763               (incf gnus-newsgroup-expunged-tally)
7764               ;; We also mark as read here, if that's wanted.
7765               (when (and gnus-summary-mark-below
7766                          (< score gnus-summary-mark-below))
7767                 (setq gnus-newsgroup-unreads
7768                       (delq number gnus-newsgroup-unreads))
7769                 (if gnus-newsgroup-auto-expire
7770                     (push number gnus-newsgroup-expirable)
7771                   (push (cons number gnus-low-score-mark)
7772                         gnus-newsgroup-reads)))
7773               t)
7774             ;; Do the `display' group parameter.
7775             (and gnus-newsgroup-display
7776                  (not (funcall gnus-newsgroup-display)))
7777             ;; Check NoCeM things.
7778             (if (and gnus-use-nocem
7779                      (gnus-nocem-unwanted-article-p
7780                       (mail-header-id (car thread))))
7781                 (progn
7782                   (setq gnus-newsgroup-unreads
7783                         (delq number gnus-newsgroup-unreads))
7784                   t))))
7785           ;; Nope, invisible article.
7786           0
7787         ;; Ok, this article is to be visible, so we add it to the limit
7788         ;; and return 1.
7789         (push number gnus-newsgroup-limit)
7790         1))))
7791
7792 (defun gnus-expunge-thread (thread)
7793   "Mark all articles in THREAD as read."
7794   (let* ((number (mail-header-number (car thread))))
7795     (incf gnus-newsgroup-expunged-tally)
7796     ;; We also mark as read here, if that's wanted.
7797     (setq gnus-newsgroup-unreads
7798           (delq number gnus-newsgroup-unreads))
7799     (if gnus-newsgroup-auto-expire
7800         (push number gnus-newsgroup-expirable)
7801       (push (cons number gnus-low-score-mark)
7802             gnus-newsgroup-reads)))
7803   ;; Go recursively through all subthreads.
7804   (mapcar 'gnus-expunge-thread (cdr thread)))
7805
7806 ;; Summary article oriented commands
7807
7808 (defun gnus-summary-refer-parent-article (n)
7809   "Refer parent article N times.
7810 If N is negative, go to ancestor -N instead.
7811 The difference between N and the number of articles fetched is returned."
7812   (interactive "p")
7813   (let ((skip 1)
7814         error header ref)
7815     (when (not (natnump n))
7816       (setq skip (abs n)
7817             n 1))
7818     (while (and (> n 0)
7819                 (not error))
7820       (setq header (gnus-summary-article-header))
7821       (if (and (eq (mail-header-number header)
7822                    (cdr gnus-article-current))
7823                (equal gnus-newsgroup-name
7824                       (car gnus-article-current)))
7825           ;; If we try to find the parent of the currently
7826           ;; displayed article, then we take a look at the actual
7827           ;; References header, since this is slightly more
7828           ;; reliable than the References field we got from the
7829           ;; server.
7830           (save-excursion
7831             (set-buffer gnus-original-article-buffer)
7832             (nnheader-narrow-to-headers)
7833             (unless (setq ref (message-fetch-field "references"))
7834               (setq ref (message-fetch-field "in-reply-to")))
7835             (widen))
7836         (setq ref
7837               ;; It's not the current article, so we take a bet on
7838               ;; the value we got from the server.
7839               (mail-header-references header)))
7840       (if (and ref
7841                (not (equal ref "")))
7842           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7843             (gnus-message 1 "Couldn't find parent"))
7844         (gnus-message 1 "No references in article %d"
7845                       (gnus-summary-article-number))
7846         (setq error t))
7847       (decf n))
7848     (gnus-summary-position-point)
7849     n))
7850
7851 (defun gnus-summary-refer-references ()
7852   "Fetch all articles mentioned in the References header.
7853 Return the number of articles fetched."
7854   (interactive)
7855   (let ((ref (mail-header-references (gnus-summary-article-header)))
7856         (current (gnus-summary-article-number))
7857         (n 0))
7858     (if (or (not ref)
7859             (equal ref ""))
7860         (error "No References in the current article")
7861       ;; For each Message-ID in the References header...
7862       (while (string-match "<[^>]*>" ref)
7863         (incf n)
7864         ;; ... fetch that article.
7865         (gnus-summary-refer-article
7866          (prog1 (match-string 0 ref)
7867            (setq ref (substring ref (match-end 0))))))
7868       (gnus-summary-goto-subject current)
7869       (gnus-summary-position-point)
7870       n)))
7871
7872 (defun gnus-summary-refer-thread (&optional limit)
7873   "Fetch all articles in the current thread.
7874 If LIMIT (the numerical prefix), fetch that many old headers instead
7875 of what's specified by the `gnus-refer-thread-limit' variable."
7876   (interactive "P")
7877   (let ((id (mail-header-id (gnus-summary-article-header)))
7878         (limit (if limit (prefix-numeric-value limit)
7879                  gnus-refer-thread-limit)))
7880     ;; We want to fetch LIMIT *old* headers, but we also have to
7881     ;; re-fetch all the headers in the current buffer, because many of
7882     ;; them may be undisplayed.  So we adjust LIMIT.
7883     (when (numberp limit)
7884       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7885     (unless (eq gnus-fetch-old-headers 'invisible)
7886       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7887       ;; Retrieve the headers and read them in.
7888       (if (eq (gnus-retrieve-headers
7889                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7890               'nov)
7891           (gnus-build-all-threads)
7892         (error "Can't fetch thread from backends that don't support NOV"))
7893       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7894     (gnus-summary-limit-include-thread id)))
7895
7896 (defun gnus-summary-refer-article (message-id)
7897   "Fetch an article specified by MESSAGE-ID."
7898   (interactive "sMessage-ID: ")
7899   (when (and (stringp message-id)
7900              (not (zerop (length message-id))))
7901     ;; Construct the correct Message-ID if necessary.
7902     ;; Suggested by tale@pawl.rpi.edu.
7903     (unless (string-match "^<" message-id)
7904       (setq message-id (concat "<" message-id)))
7905     (unless (string-match ">$" message-id)
7906       (setq message-id (concat message-id ">")))
7907     (let* ((header (gnus-id-to-header message-id))
7908            (sparse (and header
7909                         (gnus-summary-article-sparse-p
7910                          (mail-header-number header))
7911                         (memq (mail-header-number header)
7912                               gnus-newsgroup-limit)))
7913            number)
7914       (cond
7915        ;; If the article is present in the buffer we just go to it.
7916        ((and header
7917              (or (not (gnus-summary-article-sparse-p
7918                        (mail-header-number header)))
7919                  sparse))
7920         (prog1
7921             (gnus-summary-goto-article
7922              (mail-header-number header) nil t)
7923           (when sparse
7924             (gnus-summary-update-article (mail-header-number header)))))
7925        (t
7926         ;; We fetch the article.
7927         (catch 'found
7928           (dolist (gnus-override-method (gnus-refer-article-methods))
7929             (gnus-check-server gnus-override-method)
7930             ;; Fetch the header, and display the article.
7931             (when (setq number (gnus-summary-insert-subject message-id))
7932               (gnus-summary-select-article nil nil nil number)
7933               (throw 'found t)))
7934           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7935
7936 (defun gnus-refer-article-methods ()
7937   "Return a list of referable methods."
7938   (cond
7939    ;; No method, so we default to current and native.
7940    ((null gnus-refer-article-method)
7941     (list gnus-current-select-method gnus-select-method))
7942    ;; Current.
7943    ((eq 'current gnus-refer-article-method)
7944     (list gnus-current-select-method))
7945    ;; List of select methods.
7946    ((not (and (symbolp (car gnus-refer-article-method))
7947               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7948     (let (out)
7949       (dolist (method gnus-refer-article-method)
7950         (push (if (eq 'current method)
7951                   gnus-current-select-method
7952                 method)
7953               out))
7954       (nreverse out)))
7955    ;; One single select method.
7956    (t
7957     (list gnus-refer-article-method))))
7958
7959 (defun gnus-summary-edit-parameters ()
7960   "Edit the group parameters of the current group."
7961   (interactive)
7962   (gnus-group-edit-group gnus-newsgroup-name 'params))
7963
7964 (defun gnus-summary-customize-parameters ()
7965   "Customize the group parameters of the current group."
7966   (interactive)
7967   (gnus-group-customize gnus-newsgroup-name))
7968
7969 (defun gnus-summary-enter-digest-group (&optional force)
7970   "Enter an nndoc group based on the current article.
7971 If FORCE, force a digest interpretation.  If not, try
7972 to guess what the document format is."
7973   (interactive "P")
7974   (let ((conf gnus-current-window-configuration))
7975     (save-excursion
7976       (gnus-summary-select-article))
7977     (setq gnus-current-window-configuration conf)
7978     (let* ((name (format "%s-%d"
7979                          (gnus-group-prefixed-name
7980                           gnus-newsgroup-name (list 'nndoc ""))
7981                          (save-excursion
7982                            (set-buffer gnus-summary-buffer)
7983                            gnus-current-article)))
7984            (ogroup gnus-newsgroup-name)
7985            (params (append (gnus-info-params (gnus-get-info ogroup))
7986                            (list (cons 'to-group ogroup))
7987                            (list (cons 'save-article-group ogroup))))
7988            (case-fold-search t)
7989            (buf (current-buffer))
7990            dig to-address)
7991       (save-excursion
7992         (set-buffer gnus-original-article-buffer)
7993         ;; Have the digest group inherit the main mail address of
7994         ;; the parent article.
7995         (when (setq to-address (or (gnus-fetch-field "reply-to")
7996                                    (gnus-fetch-field "from")))
7997           (setq params (append
7998                         (list (cons 'to-address
7999                                     (funcall gnus-decode-encoded-word-function
8000                                              to-address))))))
8001         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8002         (insert-buffer-substring gnus-original-article-buffer)
8003         ;; Remove lines that may lead nndoc to misinterpret the
8004         ;; document type.
8005         (narrow-to-region
8006          (goto-char (point-min))
8007          (or (search-forward "\n\n" nil t) (point)))
8008         (goto-char (point-min))
8009         (delete-matching-lines "^Path:\\|^From ")
8010         (widen))
8011       (unwind-protect
8012           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8013                     (gnus-newsgroup-ephemeral-ignored-charsets
8014                      gnus-newsgroup-ignored-charsets))
8015                 (gnus-group-read-ephemeral-group
8016                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8017                               (nndoc-article-type
8018                                ,(if force 'mbox 'guess)))
8019                  t nil nil nil
8020                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8021                                                         "ADAPT")))))
8022               ;; Make all postings to this group go to the parent group.
8023               (nconc (gnus-info-params (gnus-get-info name))
8024                      params)
8025             ;; Couldn't select this doc group.
8026             (switch-to-buffer buf)
8027             (gnus-set-global-variables)
8028             (gnus-configure-windows 'summary)
8029             (gnus-message 3 "Article couldn't be entered?"))
8030         (kill-buffer dig)))))
8031
8032 (defun gnus-summary-read-document (n)
8033   "Open a new group based on the current article(s).
8034 This will allow you to read digests and other similar
8035 documents as newsgroups.
8036 Obeys the standard process/prefix convention."
8037   (interactive "P")
8038   (let* ((articles (gnus-summary-work-articles n))
8039          (ogroup gnus-newsgroup-name)
8040          (params (append (gnus-info-params (gnus-get-info ogroup))
8041                          (list (cons 'to-group ogroup))))
8042          article group egroup groups vgroup)
8043     (while (setq article (pop articles))
8044       (setq group (format "%s-%d" gnus-newsgroup-name article))
8045       (gnus-summary-remove-process-mark article)
8046       (when (gnus-summary-display-article article)
8047         (save-excursion
8048           (with-temp-buffer
8049             (insert-buffer-substring gnus-original-article-buffer)
8050             ;; Remove some headers that may lead nndoc to make
8051             ;; the wrong guess.
8052             (message-narrow-to-head)
8053             (goto-char (point-min))
8054             (delete-matching-lines "^\\(Path\\):\\|^From ")
8055             (widen)
8056             (if (setq egroup
8057                       (gnus-group-read-ephemeral-group
8058                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8059                                      (nndoc-article-type guess))
8060                        t nil t))
8061                 (progn
8062             ;; Make all postings to this group go to the parent group.
8063                   (nconc (gnus-info-params (gnus-get-info egroup))
8064                          params)
8065                   (push egroup groups))
8066               ;; Couldn't select this doc group.
8067               (gnus-error 3 "Article couldn't be entered"))))))
8068     ;; Now we have selected all the documents.
8069     (cond
8070      ((not groups)
8071       (error "None of the articles could be interpreted as documents"))
8072      ((gnus-group-read-ephemeral-group
8073        (setq vgroup (format
8074                      "nnvirtual:%s-%s" gnus-newsgroup-name
8075                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8076        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8077        t
8078        (cons (current-buffer) 'summary)))
8079      (t
8080       (error "Couldn't select virtual nndoc group")))))
8081
8082 (defun gnus-summary-isearch-article (&optional regexp-p)
8083   "Do incremental search forward on the current article.
8084 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8085   (interactive "P")
8086   (gnus-summary-select-article)
8087   (gnus-configure-windows 'article)
8088   (gnus-eval-in-buffer-window gnus-article-buffer
8089     (save-restriction
8090       (widen)
8091       (isearch-forward regexp-p))))
8092
8093 (defun gnus-summary-search-article-forward (regexp &optional backward)
8094   "Search for an article containing REGEXP forward.
8095 If BACKWARD, search backward instead."
8096   (interactive
8097    (list (read-string
8098           (format "Search article %s (regexp%s): "
8099                   (if current-prefix-arg "backward" "forward")
8100                   (if gnus-last-search-regexp
8101                       (concat ", default " gnus-last-search-regexp)
8102                     "")))
8103          current-prefix-arg))
8104   (if (string-equal regexp "")
8105       (setq regexp (or gnus-last-search-regexp ""))
8106     (setq gnus-last-search-regexp regexp)
8107     (setq gnus-article-before-search gnus-current-article))
8108   ;; Intentionally set gnus-last-article.
8109   (setq gnus-last-article gnus-article-before-search)
8110   (let ((gnus-last-article gnus-last-article))
8111     (if (gnus-summary-search-article regexp backward)
8112         (gnus-summary-show-thread)
8113       (error "Search failed: \"%s\"" regexp))))
8114
8115 (defun gnus-summary-search-article-backward (regexp)
8116   "Search for an article containing REGEXP backward."
8117   (interactive
8118    (list (read-string
8119           (format "Search article backward (regexp%s): "
8120                   (if gnus-last-search-regexp
8121                       (concat ", default " gnus-last-search-regexp)
8122                     "")))))
8123   (gnus-summary-search-article-forward regexp 'backward))
8124
8125 (defun gnus-summary-search-article (regexp &optional backward)
8126   "Search for an article containing REGEXP.
8127 Optional argument BACKWARD means do search for backward.
8128 `gnus-select-article-hook' is not called during the search."
8129   ;; We have to require this here to make sure that the following
8130   ;; dynamic binding isn't shadowed by autoloading.
8131   (require 'gnus-async)
8132   (require 'gnus-art)
8133   (let ((gnus-select-article-hook nil)  ;Disable hook.
8134         (gnus-article-prepare-hook nil)
8135         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8136         (gnus-use-article-prefetch nil)
8137         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8138         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8139         (sum (current-buffer))
8140         (gnus-display-mime-function nil)
8141         (found nil)
8142         point)
8143     (gnus-save-hidden-threads
8144       (gnus-summary-select-article)
8145       (set-buffer gnus-article-buffer)
8146       (goto-char (window-point (get-buffer-window (current-buffer))))
8147       (when backward
8148         (forward-line -1))
8149       (while (not found)
8150         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8151         (if (if backward
8152                 (re-search-backward regexp nil t)
8153               (re-search-forward regexp nil t))
8154             ;; We found the regexp.
8155             (progn
8156               (setq found 'found)
8157               (beginning-of-line)
8158               (set-window-start
8159                (get-buffer-window (current-buffer))
8160                (point))
8161               (forward-line 1)
8162               (set-window-point
8163                (get-buffer-window (current-buffer))
8164                (point))
8165               (set-buffer sum)
8166               (setq point (point)))
8167           ;; We didn't find it, so we go to the next article.
8168           (set-buffer sum)
8169           (setq found 'not)
8170           (while (eq found 'not)
8171             (if (not (if backward (gnus-summary-find-prev)
8172                        (gnus-summary-find-next)))
8173                 ;; No more articles.
8174                 (setq found t)
8175               ;; Select the next article and adjust point.
8176               (unless (gnus-summary-article-sparse-p
8177                        (gnus-summary-article-number))
8178                 (setq found nil)
8179                 (gnus-summary-select-article)
8180                 (set-buffer gnus-article-buffer)
8181                 (widen)
8182                 (goto-char (if backward (point-max) (point-min))))))))
8183       (gnus-message 7 ""))
8184     ;; Return whether we found the regexp.
8185     (when (eq found 'found)
8186       (goto-char point)
8187       (gnus-summary-show-thread)
8188       (gnus-summary-goto-subject gnus-current-article)
8189       (gnus-summary-position-point)
8190       t)))
8191
8192 (defun gnus-find-matching-articles (header regexp)
8193   "Return a list of all articles that match REGEXP on HEADER.
8194 This search includes all articles in the current group that Gnus has
8195 fetched headers for, whether they are displayed or not."
8196   (let ((articles nil)
8197         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8198         (case-fold-search t))
8199     (dolist (header gnus-newsgroup-headers)
8200       (when (string-match regexp (funcall func header))
8201         (push (mail-header-number header) articles)))
8202     (nreverse articles)))
8203
8204 (defun gnus-summary-find-matching (header regexp &optional backward unread
8205                                           not-case-fold not-matching)
8206   "Return a list of all articles that match REGEXP on HEADER.
8207 The search stars on the current article and goes forwards unless
8208 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8209 If UNREAD is non-nil, only unread articles will
8210 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8211 in the comparisons. If NOT-MATCHING, return a list of all articles that
8212 not match REGEXP on HEADER."
8213   (let ((case-fold-search (not not-case-fold))
8214         articles d func)
8215     (if (consp header)
8216         (if (eq (car header) 'extra)
8217             (setq func
8218                   `(lambda (h)
8219                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8220                          "")))
8221           (error "%s is an invalid header" header))
8222       (unless (fboundp (intern (concat "mail-header-" header)))
8223         (error "%s is not a valid header" header))
8224       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8225     (dolist (d (if (eq backward 'all)
8226                    gnus-newsgroup-data
8227                  (gnus-data-find-list
8228                   (gnus-summary-article-number)
8229                   (gnus-data-list backward))))
8230       (when (and (or (not unread)       ; We want all articles...
8231                      (gnus-data-unread-p d)) ; Or just unreads.
8232                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8233                  (if not-matching
8234                      (not (string-match
8235                            regexp
8236                            (funcall func (gnus-data-header d))))
8237                    (string-match regexp
8238                                  (funcall func (gnus-data-header d)))))
8239         (push (gnus-data-number d) articles))) ; Success!
8240     (nreverse articles)))
8241
8242 (defun gnus-summary-execute-command (header regexp command &optional backward)
8243   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8244 If HEADER is an empty string (or nil), the match is done on the entire
8245 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8246   (interactive
8247    (list (let ((completion-ignore-case t))
8248            (completing-read
8249             "Header name: "
8250             (mapcar (lambda (header) (list (format "%s" header)))
8251                     (append
8252                      '("Number" "Subject" "From" "Lines" "Date"
8253                        "Message-ID" "Xref" "References" "Body")
8254                      gnus-extra-headers))
8255             nil 'require-match))
8256          (read-string "Regexp: ")
8257          (read-key-sequence "Command: ")
8258          current-prefix-arg))
8259   (when (equal header "Body")
8260     (setq header ""))
8261   ;; Hidden thread subtrees must be searched as well.
8262   (gnus-summary-show-all-threads)
8263   ;; We don't want to change current point nor window configuration.
8264   (save-excursion
8265     (save-window-excursion
8266       (let (gnus-visual
8267             gnus-treat-strip-trailing-blank-lines
8268             gnus-treat-strip-leading-blank-lines
8269             gnus-treat-strip-multiple-blank-lines
8270             gnus-treat-hide-boring-headers
8271             gnus-treat-fold-newsgroups
8272             gnus-article-prepare-hook)
8273         (gnus-message 6 "Executing %s..." (key-description command))
8274         ;; We'd like to execute COMMAND interactively so as to give arguments.
8275         (gnus-execute header regexp
8276                       `(call-interactively ',(key-binding command))
8277                       backward)
8278         (gnus-message 6 "Executing %s...done" (key-description command))))))
8279
8280 (defun gnus-summary-beginning-of-article ()
8281   "Scroll the article back to the beginning."
8282   (interactive)
8283   (gnus-summary-select-article)
8284   (gnus-configure-windows 'article)
8285   (gnus-eval-in-buffer-window gnus-article-buffer
8286     (widen)
8287     (goto-char (point-min))
8288     (when gnus-page-broken
8289       (gnus-narrow-to-page))))
8290
8291 (defun gnus-summary-end-of-article ()
8292   "Scroll to the end of the article."
8293   (interactive)
8294   (gnus-summary-select-article)
8295   (gnus-configure-windows 'article)
8296   (gnus-eval-in-buffer-window gnus-article-buffer
8297     (widen)
8298     (goto-char (point-max))
8299     (recenter -3)
8300     (when gnus-page-broken
8301       (gnus-narrow-to-page))))
8302
8303 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8304   "Truncate to LEN and quote all \"(\"'s in STRING."
8305   (gnus-replace-in-string (if (and len (> (length string) len))
8306                               (substring string 0 len)
8307                             string)
8308                           "[()]" "\\\\\\&"))
8309
8310 (defun gnus-summary-print-article (&optional filename n)
8311   "Generate and print a PostScript image of the N next (mail) articles.
8312
8313 If N is negative, print the N previous articles.  If N is nil and articles
8314 have been marked with the process mark, print these instead.
8315
8316 If the optional first argument FILENAME is nil, send the image to the
8317 printer.  If FILENAME is a string, save the PostScript image in a file with
8318 that name.  If FILENAME is a number, prompt the user for the name of the file
8319 to save in."
8320   (interactive (list (ps-print-preprint current-prefix-arg)))
8321   (dolist (article (gnus-summary-work-articles n))
8322     (gnus-summary-select-article nil nil 'pseudo article)
8323     (gnus-eval-in-buffer-window gnus-article-buffer
8324       (gnus-print-buffer))
8325     (gnus-summary-remove-process-mark article))
8326   (ps-despool filename))
8327
8328 (defun gnus-print-buffer ()
8329   (let ((buffer (generate-new-buffer " *print*")))
8330     (unwind-protect
8331         (progn
8332           (copy-to-buffer buffer (point-min) (point-max))
8333           (set-buffer buffer)
8334           (gnus-article-delete-invisible-text)
8335           (gnus-remove-text-with-property 'gnus-decoration)
8336           (when (gnus-visual-p 'article-highlight 'highlight)
8337             ;; Copy-to-buffer doesn't copy overlay.  So redo
8338             ;; highlight.
8339             (let ((gnus-article-buffer buffer))
8340               (gnus-article-highlight-citation t)
8341               (gnus-article-highlight-signature)))
8342           (let ((ps-left-header
8343                  (list
8344                   (concat "("
8345                           (gnus-summary-print-truncate-and-quote
8346                            (mail-header-subject gnus-current-headers)
8347                            66) ")")
8348                   (concat "("
8349                           (gnus-summary-print-truncate-and-quote
8350                            (mail-header-from gnus-current-headers)
8351                            45) ")")))
8352                 (ps-right-header
8353                  (list
8354                   "/pagenumberstring load"
8355                   (concat "("
8356                           (mail-header-date gnus-current-headers) ")"))))
8357             (gnus-run-hooks 'gnus-ps-print-hook)
8358             (save-excursion
8359               (if window-system
8360                   (ps-spool-buffer-with-faces)
8361                 (ps-spool-buffer)))))
8362       (kill-buffer buffer))))
8363
8364 (defun gnus-summary-show-article (&optional arg)
8365   "Force redisplaying of the current article.
8366 If ARG (the prefix) is a number, show the article with the charset
8367 defined in `gnus-summary-show-article-charset-alist', or the charset
8368 input.
8369 If ARG (the prefix) is non-nil and not a number, show the raw article
8370 without any article massaging functions being run.  Normally, the key strokes
8371 are `C-u g'."
8372   (interactive "P")
8373   (cond
8374    ((numberp arg)
8375     (gnus-summary-show-article t)
8376     (let ((gnus-newsgroup-charset
8377            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8378                (mm-read-coding-system
8379                 "View as charset: " ;; actually it is coding system.
8380                 (save-excursion
8381                   (set-buffer gnus-article-buffer)
8382                   (mm-detect-coding-region (point) (point-max))))))
8383           (gnus-newsgroup-ignored-charsets 'gnus-all))
8384       (gnus-summary-select-article nil 'force)
8385       (let ((deps gnus-newsgroup-dependencies)
8386             head header lines)
8387         (save-excursion
8388           (set-buffer gnus-original-article-buffer)
8389           (save-restriction
8390             (message-narrow-to-head)
8391             (setq head (buffer-string))
8392             (goto-char (point-min))
8393             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8394               (goto-char (point-max))
8395               (widen)
8396               (setq lines (1- (count-lines (point) (point-max))))))
8397           (with-temp-buffer
8398             (insert (format "211 %d Article retrieved.\n"
8399                             (cdr gnus-article-current)))
8400             (insert head)
8401             (if lines (insert (format "Lines: %d\n" lines)))
8402             (insert ".\n")
8403             (let ((nntp-server-buffer (current-buffer)))
8404               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8405         (gnus-data-set-header
8406          (gnus-data-find (cdr gnus-article-current))
8407          header)
8408         (gnus-summary-update-article-line
8409          (cdr gnus-article-current) header)
8410         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8411           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8412    ((not arg)
8413     ;; Select the article the normal way.
8414     (gnus-summary-select-article nil 'force))
8415    (t
8416     ;; We have to require this here to make sure that the following
8417     ;; dynamic binding isn't shadowed by autoloading.
8418     (require 'gnus-async)
8419     (require 'gnus-art)
8420     ;; Bind the article treatment functions to nil.
8421     (let ((gnus-have-all-headers t)
8422           gnus-article-prepare-hook
8423           gnus-article-decode-hook
8424           gnus-display-mime-function
8425           gnus-break-pages)
8426       ;; Destroy any MIME parts.
8427       (when (gnus-buffer-live-p gnus-article-buffer)
8428         (save-excursion
8429           (set-buffer gnus-article-buffer)
8430           (mm-destroy-parts gnus-article-mime-handles)
8431           ;; Set it to nil for safety reason.
8432           (setq gnus-article-mime-handle-alist nil)
8433           (setq gnus-article-mime-handles nil)))
8434       (gnus-summary-select-article nil 'force))))
8435   (gnus-summary-goto-subject gnus-current-article)
8436   (gnus-summary-position-point))
8437
8438 (defun gnus-summary-show-raw-article ()
8439   "Show the raw article without any article massaging functions being run."
8440   (interactive)
8441   (gnus-summary-show-article t))
8442
8443 (defun gnus-summary-verbose-headers (&optional arg)
8444   "Toggle permanent full header display.
8445 If ARG is a positive number, turn header display on.
8446 If ARG is a negative number, turn header display off."
8447   (interactive "P")
8448   (setq gnus-show-all-headers
8449         (cond ((or (not (numberp arg))
8450                    (zerop arg))
8451                (not gnus-show-all-headers))
8452               ((natnump arg)
8453                t)))
8454   (gnus-summary-show-article))
8455
8456 (defun gnus-summary-toggle-header (&optional arg)
8457   "Show the headers if they are hidden, or hide them if they are shown.
8458 If ARG is a positive number, show the entire header.
8459 If ARG is a negative number, hide the unwanted header lines."
8460   (interactive "P")
8461   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8462                      (get-buffer-window gnus-article-buffer t))))
8463     (with-current-buffer gnus-article-buffer
8464       (widen)
8465       (article-narrow-to-head)
8466       (let* ((buffer-read-only nil)
8467              (inhibit-point-motion-hooks t)
8468              (hidden (if (numberp arg)
8469                          (>= arg 0)
8470                        (gnus-article-hidden-text-p 'headers)))
8471              s e)
8472         (delete-region (point-min) (point-max))
8473         (with-current-buffer gnus-original-article-buffer
8474           (goto-char (setq s (point-min)))
8475           (setq e (if (search-forward "\n\n" nil t)
8476                       (1- (point))
8477                     (point-max))))
8478         (insert-buffer-substring gnus-original-article-buffer s e)
8479         (article-decode-encoded-words)
8480         (if hidden
8481             (let ((gnus-treat-hide-headers nil)
8482                   (gnus-treat-hide-boring-headers nil))
8483               (gnus-delete-wash-type 'headers)
8484               (gnus-treat-article 'head))
8485           (gnus-treat-article 'head))
8486         (widen)
8487         (if window
8488             (set-window-start window (goto-char (point-min))))
8489         (setq gnus-page-broken
8490               (when gnus-break-pages
8491                 (gnus-narrow-to-page)
8492                 t))
8493         (gnus-set-mode-line 'article)))))
8494
8495 (defun gnus-summary-show-all-headers ()
8496   "Make all header lines visible."
8497   (interactive)
8498   (gnus-summary-toggle-header 1))
8499
8500 (defun gnus-summary-caesar-message (&optional arg)
8501   "Caesar rotate the current article by 13.
8502 The numerical prefix specifies how many places to rotate each letter
8503 forward."
8504   (interactive "P")
8505   (gnus-summary-select-article)
8506   (let ((mail-header-separator ""))
8507     (gnus-eval-in-buffer-window gnus-article-buffer
8508       (save-restriction
8509         (widen)
8510         (let ((start (window-start))
8511               buffer-read-only)
8512           (message-caesar-buffer-body arg)
8513           (set-window-start (get-buffer-window (current-buffer)) start))))))
8514
8515 (autoload 'unmorse-region "morse"
8516   "Convert morse coded text in region to ordinary ASCII text."
8517   t)
8518
8519 (defun gnus-summary-morse-message (&optional arg)
8520   "Morse decode the current article."
8521   (interactive "P")
8522   (gnus-summary-select-article)
8523   (let ((mail-header-separator ""))
8524     (gnus-eval-in-buffer-window gnus-article-buffer
8525       (save-excursion
8526         (save-restriction
8527           (widen)
8528           (let ((pos (window-start))
8529                 buffer-read-only)
8530             (goto-char (point-min))
8531             (when (message-goto-body)
8532               (gnus-narrow-to-body))
8533             (goto-char (point-min))
8534             (while (re-search-forward "·" (point-max) t)
8535               (replace-match "."))
8536             (unmorse-region (point-min) (point-max))
8537             (widen)
8538             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8539
8540 (defun gnus-summary-stop-page-breaking ()
8541   "Stop page breaking in the current article."
8542   (interactive)
8543   (gnus-summary-select-article)
8544   (gnus-eval-in-buffer-window gnus-article-buffer
8545     (widen)
8546     (when (gnus-visual-p 'page-marker)
8547       (let ((buffer-read-only nil))
8548         (gnus-remove-text-with-property 'gnus-prev)
8549         (gnus-remove-text-with-property 'gnus-next))
8550       (setq gnus-page-broken nil))))
8551
8552 (defun gnus-summary-move-article (&optional n to-newsgroup
8553                                             select-method action)
8554   "Move the current article to a different newsgroup.
8555 If N is a positive number, move the N next articles.
8556 If N is a negative number, move the N previous articles.
8557 If N is nil and any articles have been marked with the process mark,
8558 move those articles instead.
8559 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8560 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8561 re-spool using this method.
8562
8563 When called interactively with TO-NEWSGROUP being nil, the value of
8564 the variable `gnus-move-split-methods' is used for finding a default
8565 for the target newsgroup.
8566
8567 For this function to work, both the current newsgroup and the
8568 newsgroup that you want to move to have to support the `request-move'
8569 and `request-accept' functions.
8570
8571 ACTION can be either `move' (the default), `crosspost' or `copy'."
8572   (interactive "P")
8573   (unless action
8574     (setq action 'move))
8575   ;; Check whether the source group supports the required functions.
8576   (cond ((and (eq action 'move)
8577               (not (gnus-check-backend-function
8578                     'request-move-article gnus-newsgroup-name)))
8579          (error "The current group does not support article moving"))
8580         ((and (eq action 'crosspost)
8581               (not (gnus-check-backend-function
8582                     'request-replace-article gnus-newsgroup-name)))
8583          (error "The current group does not support article editing")))
8584   (let ((articles (gnus-summary-work-articles n))
8585         (prefix (if (gnus-check-backend-function
8586                      'request-move-article gnus-newsgroup-name)
8587                     (gnus-group-real-prefix gnus-newsgroup-name)
8588                   ""))
8589         (names '((move "Move" "Moving")
8590                  (copy "Copy" "Copying")
8591                  (crosspost "Crosspost" "Crossposting")))
8592         (copy-buf (save-excursion
8593                     (nnheader-set-temp-buffer " *copy article*")))
8594         art-group to-method new-xref article to-groups)
8595     (unless (assq action names)
8596       (error "Unknown action %s" action))
8597     ;; Read the newsgroup name.
8598     (when (and (not to-newsgroup)
8599                (not select-method))
8600       (if (and gnus-move-split-methods
8601                (not
8602                 (and (memq gnus-current-article articles)
8603                      (gnus-buffer-live-p gnus-original-article-buffer))))
8604           ;; When `gnus-move-split-methods' is non-nil, we have to
8605           ;; select an article to give `gnus-read-move-group-name' an
8606           ;; opportunity to suggest an appropriate default.  However,
8607           ;; we needn't render or mark the article.
8608           (let ((gnus-display-mime-function nil)
8609                 (gnus-article-prepare-hook nil)
8610                 (gnus-mark-article-hook nil))
8611             (gnus-summary-select-article nil nil nil (car articles))))
8612       (setq to-newsgroup
8613             (gnus-read-move-group-name
8614              (cadr (assq action names))
8615              (symbol-value (intern (format "gnus-current-%s-group" action)))
8616              articles prefix))
8617       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8618     (setq to-method (or select-method
8619                         (gnus-server-to-method
8620                          (gnus-group-method to-newsgroup))))
8621     ;; Check the method we are to move this article to...
8622     (unless (gnus-check-backend-function
8623              'request-accept-article (car to-method))
8624       (error "%s does not support article copying" (car to-method)))
8625     (unless (gnus-check-server to-method)
8626       (error "Can't open server %s" (car to-method)))
8627     (gnus-message 6 "%s to %s: %s..."
8628                   (caddr (assq action names))
8629                   (or (car select-method) to-newsgroup) articles)
8630     (while articles
8631       (setq article (pop articles))
8632       (setq
8633        art-group
8634        (cond
8635         ;; Move the article.
8636         ((eq action 'move)
8637          ;; Remove this article from future suppression.
8638          (gnus-dup-unsuppress-article article)
8639          (gnus-request-move-article
8640           article                       ; Article to move
8641           gnus-newsgroup-name           ; From newsgroup
8642           (nth 1 (gnus-find-method-for-group
8643                   gnus-newsgroup-name)) ; Server
8644           (list 'gnus-request-accept-article
8645                 to-newsgroup (list 'quote select-method)
8646                 (not articles) t)       ; Accept form
8647           (not articles)))              ; Only save nov last time
8648         ;; Copy the article.
8649         ((eq action 'copy)
8650          (save-excursion
8651            (set-buffer copy-buf)
8652            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8653              (gnus-request-accept-article
8654               to-newsgroup select-method (not articles) t))))
8655         ;; Crosspost the article.
8656         ((eq action 'crosspost)
8657          (let ((xref (message-tokenize-header
8658                       (mail-header-xref (gnus-summary-article-header article))
8659                       " ")))
8660            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8661                                   ":" (number-to-string article)))
8662            (unless xref
8663              (setq xref (list (system-name))))
8664            (setq new-xref
8665                  (concat
8666                   (mapconcat 'identity
8667                              (delete "Xref:" (delete new-xref xref))
8668                              " ")
8669                   " " new-xref))
8670            (save-excursion
8671              (set-buffer copy-buf)
8672              ;; First put the article in the destination group.
8673              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8674              (when (consp (setq art-group
8675                                 (gnus-request-accept-article
8676                                  to-newsgroup select-method (not articles))))
8677                (setq new-xref (concat new-xref " " (car art-group)
8678                                       ":"
8679                                       (number-to-string (cdr art-group))))
8680                ;; Now we have the new Xrefs header, so we insert
8681                ;; it and replace the new article.
8682                (nnheader-replace-header "Xref" new-xref)
8683                (gnus-request-replace-article
8684                 (cdr art-group) to-newsgroup (current-buffer))
8685                art-group))))))
8686       (cond
8687        ((not art-group)
8688         (gnus-message 1 "Couldn't %s article %s: %s"
8689                       (cadr (assq action names)) article
8690                       (nnheader-get-report (car to-method))))
8691        ((eq art-group 'junk)
8692         (when (eq action 'move)
8693           (gnus-summary-mark-article article gnus-canceled-mark)
8694           (gnus-message 4 "Deleted article %s" article)))
8695        (t
8696         (let* ((pto-group (gnus-group-prefixed-name
8697                            (car art-group) to-method))
8698                (entry
8699                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8700                (info (nth 2 entry))
8701                (to-group (gnus-info-group info))
8702                to-marks)
8703           ;; Update the group that has been moved to.
8704           (when (and info
8705                      (memq action '(move copy)))
8706             (unless (member to-group to-groups)
8707               (push to-group to-groups))
8708
8709             (unless (memq article gnus-newsgroup-unreads)
8710               (push 'read to-marks)
8711               (gnus-info-set-read
8712                info (gnus-add-to-range (gnus-info-read info)
8713                                        (list (cdr art-group)))))
8714
8715             ;; See whether the article is to be put in the cache.
8716             (let ((marks gnus-article-mark-lists)
8717                   (to-article (cdr art-group)))
8718
8719               ;; Enter the article into the cache in the new group,
8720               ;; if that is required.
8721               (when gnus-use-cache
8722                 (gnus-cache-possibly-enter-article
8723                  to-group to-article
8724                  (memq article gnus-newsgroup-marked)
8725                  (memq article gnus-newsgroup-dormant)
8726                  (memq article gnus-newsgroup-unreads)))
8727
8728               (when gnus-preserve-marks
8729                 ;; Copy any marks over to the new group.
8730                 (when (and (equal to-group gnus-newsgroup-name)
8731                            (not (memq article gnus-newsgroup-unreads)))
8732                   ;; Mark this article as read in this group.
8733                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8734                   (setcdr (gnus-active to-group) to-article)
8735                   (setcdr gnus-newsgroup-active to-article))
8736
8737                 (while marks
8738                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8739                     (when (memq article (symbol-value
8740                                          (intern (format "gnus-newsgroup-%s"
8741                                                          (caar marks)))))
8742                       (push (cdar marks) to-marks)
8743                       ;; If the other group is the same as this group,
8744                       ;; then we have to add the mark to the list.
8745                       (when (equal to-group gnus-newsgroup-name)
8746                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8747                              (cons to-article
8748                                    (symbol-value
8749                                     (intern (format "gnus-newsgroup-%s"
8750                                                     (caar marks)))))))
8751                       ;; Copy the marks to other group.
8752                       (gnus-add-marked-articles
8753                        to-group (cdar marks) (list to-article) info)))
8754                   (setq marks (cdr marks)))
8755
8756                 (gnus-request-set-mark
8757                  to-group (list (list (list to-article) 'add to-marks))))
8758
8759               (gnus-dribble-enter
8760                (concat "(gnus-group-set-info '"
8761                        (gnus-prin1-to-string (gnus-get-info to-group))
8762                        ")"))))
8763
8764           ;; Update the Xref header in this article to point to
8765           ;; the new crossposted article we have just created.
8766           (when (eq action 'crosspost)
8767             (save-excursion
8768               (set-buffer copy-buf)
8769               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8770               (nnheader-replace-header "Xref" new-xref)
8771               (gnus-request-replace-article
8772                article gnus-newsgroup-name (current-buffer)))))
8773
8774         ;;;!!!Why is this necessary?
8775         (set-buffer gnus-summary-buffer)
8776
8777         (gnus-summary-goto-subject article)
8778         (when (eq action 'move)
8779           (gnus-summary-mark-article article gnus-canceled-mark))))
8780       (gnus-summary-remove-process-mark article))
8781     ;; Re-activate all groups that have been moved to.
8782     (save-excursion
8783       (set-buffer gnus-group-buffer)
8784       (let ((gnus-group-marked to-groups))
8785         (gnus-group-get-new-news-this-group nil t)))
8786
8787     (gnus-kill-buffer copy-buf)
8788     (gnus-summary-position-point)
8789     (gnus-set-mode-line 'summary)))
8790
8791 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8792   "Move the current article to a different newsgroup.
8793 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8794 When called interactively, if TO-NEWSGROUP is nil, use the value of
8795 the variable `gnus-move-split-methods' for finding a default target
8796 newsgroup.
8797 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8798 re-spool using this method."
8799   (interactive "P")
8800   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8801
8802 (defun gnus-summary-crosspost-article (&optional n)
8803   "Crosspost the current article to some other group."
8804   (interactive "P")
8805   (gnus-summary-move-article n nil nil 'crosspost))
8806
8807 (defcustom gnus-summary-respool-default-method nil
8808   "Default method type for respooling an article.
8809 If nil, use to the current newsgroup method."
8810   :type 'symbol
8811   :group 'gnus-summary-mail)
8812
8813 (defcustom gnus-summary-display-while-building nil
8814   "If not-nil, show and update the summary buffer as it's being built.
8815 If the value is t, update the buffer after every line is inserted.  If
8816 the value is an integer (N), update the display every N lines."
8817   :group 'gnus-thread
8818   :type '(choice (const :tag "off" nil)
8819                  number
8820                  (const :tag "frequently" t)))
8821
8822 (defun gnus-summary-respool-article (&optional n method)
8823   "Respool the current article.
8824 The article will be squeezed through the mail spooling process again,
8825 which means that it will be put in some mail newsgroup or other
8826 depending on `nnmail-split-methods'.
8827 If N is a positive number, respool the N next articles.
8828 If N is a negative number, respool the N previous articles.
8829 If N is nil and any articles have been marked with the process mark,
8830 respool those articles instead.
8831
8832 Respooling can be done both from mail groups and \"real\" newsgroups.
8833 In the former case, the articles in question will be moved from the
8834 current group into whatever groups they are destined to.  In the
8835 latter case, they will be copied into the relevant groups."
8836   (interactive
8837    (list current-prefix-arg
8838          (let* ((methods (gnus-methods-using 'respool))
8839                 (methname
8840                  (symbol-name (or gnus-summary-respool-default-method
8841                                   (car (gnus-find-method-for-group
8842                                         gnus-newsgroup-name)))))
8843                 (method
8844                  (gnus-completing-read-with-default
8845                   methname "What backend do you want to use when respooling?"
8846                   methods nil t nil 'gnus-mail-method-history))
8847                 ms)
8848            (cond
8849             ((zerop (length (setq ms (gnus-servers-using-backend
8850                                       (intern method)))))
8851              (list (intern method) ""))
8852             ((= 1 (length ms))
8853              (car ms))
8854             (t
8855              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8856                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8857                            ms-alist))))))))
8858   (unless method
8859     (error "No method given for respooling"))
8860   (if (assoc (symbol-name
8861               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8862              (gnus-methods-using 'respool))
8863       (gnus-summary-move-article n nil method)
8864     (gnus-summary-copy-article n nil method)))
8865
8866 (defun gnus-summary-import-article (file &optional edit)
8867   "Import an arbitrary file into a mail newsgroup."
8868   (interactive "fImport file: \nP")
8869   (let ((group gnus-newsgroup-name)
8870         (now (current-time))
8871         atts lines group-art)
8872     (unless (gnus-check-backend-function 'request-accept-article group)
8873       (error "%s does not support article importing" group))
8874     (or (file-readable-p file)
8875         (not (file-regular-p file))
8876         (error "Can't read %s" file))
8877     (save-excursion
8878       (set-buffer (gnus-get-buffer-create " *import file*"))
8879       (erase-buffer)
8880       (nnheader-insert-file-contents file)
8881       (goto-char (point-min))
8882       (if (nnheader-article-p)
8883           (save-restriction
8884             (goto-char (point-min))
8885             (search-forward "\n\n" nil t)
8886             (narrow-to-region (point-min) (1- (point)))
8887             (goto-char (point-min))
8888             (unless (re-search-forward "^date:" nil t)
8889               (goto-char (point-max))
8890               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8891        ;; This doesn't look like an article, so we fudge some headers.
8892         (setq atts (file-attributes file)
8893               lines (count-lines (point-min) (point-max)))
8894         (insert "From: " (read-string "From: ") "\n"
8895                 "Subject: " (read-string "Subject: ") "\n"
8896                 "Date: " (message-make-date (nth 5 atts)) "\n"
8897                 "Message-ID: " (message-make-message-id) "\n"
8898                 "Lines: " (int-to-string lines) "\n"
8899                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8900       (setq group-art (gnus-request-accept-article group nil t))
8901       (kill-buffer (current-buffer)))
8902     (setq gnus-newsgroup-active (gnus-activate-group group))
8903     (forward-line 1)
8904     (gnus-summary-goto-article (cdr group-art) nil t)
8905     (when edit
8906       (gnus-summary-edit-article))))
8907
8908 (defun gnus-summary-create-article ()
8909   "Create an article in a mail newsgroup."
8910   (interactive)
8911   (let ((group gnus-newsgroup-name)
8912         (now (current-time))
8913         group-art)
8914     (unless (gnus-check-backend-function 'request-accept-article group)
8915       (error "%s does not support article importing" group))
8916     (save-excursion
8917       (set-buffer (gnus-get-buffer-create " *import file*"))
8918       (erase-buffer)
8919       (goto-char (point-min))
8920       ;; This doesn't look like an article, so we fudge some headers.
8921       (insert "From: " (read-string "From: ") "\n"
8922               "Subject: " (read-string "Subject: ") "\n"
8923               "Date: " (message-make-date now) "\n"
8924               "Message-ID: " (message-make-message-id) "\n")
8925       (setq group-art (gnus-request-accept-article group nil t))
8926       (kill-buffer (current-buffer)))
8927     (setq gnus-newsgroup-active (gnus-activate-group group))
8928     (forward-line 1)
8929     (gnus-summary-goto-article (cdr group-art) nil t)
8930     (gnus-summary-edit-article)))
8931
8932 (defun gnus-summary-article-posted-p ()
8933   "Say whether the current (mail) article is available from news as well.
8934 This will be the case if the article has both been mailed and posted."
8935   (interactive)
8936   (let ((id (mail-header-references (gnus-summary-article-header)))
8937         (gnus-override-method (car (gnus-refer-article-methods))))
8938     (if (gnus-request-head id "")
8939         (gnus-message 2 "The current message was found on %s"
8940                       gnus-override-method)
8941       (gnus-message 2 "The current message couldn't be found on %s"
8942                     gnus-override-method)
8943       nil)))
8944
8945 (defun gnus-summary-expire-articles (&optional now)
8946   "Expire all articles that are marked as expirable in the current group."
8947   (interactive)
8948   (when (gnus-check-backend-function
8949          'request-expire-articles gnus-newsgroup-name)
8950     ;; This backend supports expiry.
8951     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8952            (expirable (if total
8953                           (progn
8954                             ;; We need to update the info for
8955                             ;; this group for `gnus-list-of-read-articles'
8956                             ;; to give us the right answer.
8957                             (gnus-run-hooks 'gnus-exit-group-hook)
8958                             (gnus-summary-update-info)
8959                             (gnus-list-of-read-articles gnus-newsgroup-name))
8960                         (setq gnus-newsgroup-expirable
8961                               (sort gnus-newsgroup-expirable '<))))
8962            (expiry-wait (if now 'immediate
8963                           (gnus-group-find-parameter
8964                            gnus-newsgroup-name 'expiry-wait)))
8965            (nnmail-expiry-target
8966             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8967                 nnmail-expiry-target))
8968            es)
8969       (when expirable
8970         ;; There are expirable articles in this group, so we run them
8971         ;; through the expiry process.
8972         (gnus-message 6 "Expiring articles...")
8973         (unless (gnus-check-group gnus-newsgroup-name)
8974           (error "Can't open server for %s" gnus-newsgroup-name))
8975         ;; The list of articles that weren't expired is returned.
8976         (save-excursion
8977           (if expiry-wait
8978               (let ((nnmail-expiry-wait-function nil)
8979                     (nnmail-expiry-wait expiry-wait))
8980                 (setq es (gnus-request-expire-articles
8981                           expirable gnus-newsgroup-name)))
8982             (setq es (gnus-request-expire-articles
8983                       expirable gnus-newsgroup-name)))
8984           (unless total
8985             (setq gnus-newsgroup-expirable es))
8986           ;; We go through the old list of expirable, and mark all
8987           ;; really expired articles as nonexistent.
8988           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8989             (let ((gnus-use-cache nil))
8990               (dolist (article expirable)
8991                 (when (and (not (memq article es))
8992                            (gnus-data-find article))
8993                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8994         (gnus-message 6 "Expiring articles...done")))))
8995
8996 (defun gnus-summary-expire-articles-now ()
8997   "Expunge all expirable articles in the current group.
8998 This means that *all* articles that are marked as expirable will be
8999 deleted forever, right now."
9000   (interactive)
9001   (or gnus-expert-user
9002       (gnus-yes-or-no-p
9003        "Are you really, really, really sure you want to delete all these messages? ")
9004       (error "Phew!"))
9005   (gnus-summary-expire-articles t))
9006
9007 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9008 (defun gnus-summary-delete-article (&optional n)
9009   "Delete the N next (mail) articles.
9010 This command actually deletes articles.  This is not a marking
9011 command.  The article will disappear forever from your life, never to
9012 return.
9013 If N is negative, delete backwards.
9014 If N is nil and articles have been marked with the process mark,
9015 delete these instead."
9016   (interactive "P")
9017   (unless (gnus-check-backend-function 'request-expire-articles
9018                                        gnus-newsgroup-name)
9019     (error "The current newsgroup does not support article deletion"))
9020   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9021     (error "Couldn't open server"))
9022   ;; Compute the list of articles to delete.
9023   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9024         (nnmail-expiry-target 'delete)
9025         not-deleted)
9026     (if (and gnus-novice-user
9027              (not (gnus-yes-or-no-p
9028                    (format "Do you really want to delete %s forever? "
9029                            (if (> (length articles) 1)
9030                                (format "these %s articles" (length articles))
9031                              "this article")))))
9032         ()
9033       ;; Delete the articles.
9034       (setq not-deleted (gnus-request-expire-articles
9035                          articles gnus-newsgroup-name 'force))
9036       (while articles
9037         (gnus-summary-remove-process-mark (car articles))
9038         ;; The backend might not have been able to delete the article
9039         ;; after all.
9040         (unless (memq (car articles) not-deleted)
9041           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9042         (setq articles (cdr articles)))
9043       (when not-deleted
9044         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9045     (gnus-summary-position-point)
9046     (gnus-set-mode-line 'summary)
9047     not-deleted))
9048
9049 (defun gnus-summary-edit-article (&optional arg)
9050   "Edit the current article.
9051 This will have permanent effect only in mail groups.
9052 If ARG is nil, edit the decoded articles.
9053 If ARG is 1, edit the raw articles.
9054 If ARG is 2, edit the raw articles even in read-only groups.
9055 If ARG is 3, edit the articles with the current handles.
9056 Otherwise, allow editing of articles even in read-only
9057 groups."
9058   (interactive "P")
9059   (let (force raw current-handles)
9060     (cond
9061      ((null arg))
9062      ((eq arg 1)
9063       (setq raw t))
9064      ((eq arg 2)
9065       (setq raw t
9066             force t))
9067      ((eq arg 3)
9068       (setq current-handles
9069             (and (gnus-buffer-live-p gnus-article-buffer)
9070                  (with-current-buffer gnus-article-buffer
9071                    (prog1
9072                        gnus-article-mime-handles
9073                      (setq gnus-article-mime-handles nil))))))
9074      (t
9075       (setq force t)))
9076     (when (and raw (not force)
9077                (member gnus-newsgroup-name '("nndraft:delayed"
9078                                              "nndraft:drafts"
9079                                              "nndraft:queue")))
9080       (error "Can't edit the raw article in group %s"
9081              gnus-newsgroup-name))
9082     (save-excursion
9083       (set-buffer gnus-summary-buffer)
9084       (let ((mail-parse-charset gnus-newsgroup-charset)
9085             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9086         (gnus-set-global-variables)
9087         (when (and (not force)
9088                    (gnus-group-read-only-p))
9089           (error "The current newsgroup does not support article editing"))
9090         (gnus-summary-show-article t)
9091         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9092           (with-current-buffer gnus-article-buffer
9093             (mm-enable-multibyte)))
9094         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9095             (setq raw t))
9096         (gnus-article-edit-article
9097          (if raw 'ignore
9098            `(lambda ()
9099               (let ((mbl mml-buffer-list))
9100                 (setq mml-buffer-list nil)
9101                 (mime-to-mml ,'current-handles)
9102                 (let ((mbl1 mml-buffer-list))
9103                   (setq mml-buffer-list mbl)
9104                   (set (make-local-variable 'mml-buffer-list) mbl1))
9105                 (make-local-hook 'kill-buffer-hook)
9106                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9107          `(lambda (no-highlight)
9108             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9109                   (message-options message-options)
9110                   (message-options-set-recipient)
9111                   (mail-parse-ignored-charsets
9112                    ',gnus-newsgroup-ignored-charsets))
9113               ,(if (not raw) '(progn
9114                                 (mml-to-mime)
9115                                 (mml-destroy-buffers)
9116                                 (remove-hook 'kill-buffer-hook
9117                                              'mml-destroy-buffers t)
9118                                 (kill-local-variable 'mml-buffer-list)))
9119               (gnus-summary-edit-article-done
9120                ,(or (mail-header-references gnus-current-headers) "")
9121                ,(gnus-group-read-only-p)
9122                ,gnus-summary-buffer no-highlight))))))))
9123
9124 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9125
9126 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9127                                                  no-highlight)
9128   "Make edits to the current article permanent."
9129   (interactive)
9130   (save-excursion
9131    ;; The buffer restriction contains the entire article if it exists.
9132     (when (article-goto-body)
9133       (let ((lines (count-lines (point) (point-max)))
9134             (length (- (point-max) (point)))
9135             (case-fold-search t)
9136             (body (copy-marker (point))))
9137         (goto-char (point-min))
9138         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9139           (delete-region (match-beginning 1) (match-end 1))
9140           (insert (number-to-string length)))
9141         (goto-char (point-min))
9142         (when (re-search-forward
9143                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9144           (delete-region (match-beginning 1) (match-end 1))
9145           (insert (number-to-string length)))
9146         (goto-char (point-min))
9147         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9148           (delete-region (match-beginning 1) (match-end 1))
9149           (insert (number-to-string lines))))))
9150   ;; Replace the article.
9151   (let ((buf (current-buffer)))
9152     (with-temp-buffer
9153       (insert-buffer-substring buf)
9154
9155       (if (and (not read-only)
9156                (not (gnus-request-replace-article
9157                      (cdr gnus-article-current) (car gnus-article-current)
9158                      (current-buffer) t)))
9159           (error "Couldn't replace article")
9160         ;; Update the summary buffer.
9161         (if (and references
9162                  (equal (message-tokenize-header references " ")
9163                         (message-tokenize-header
9164                          (or (message-fetch-field "references") "") " ")))
9165             ;; We only have to update this line.
9166             (save-excursion
9167               (save-restriction
9168                 (message-narrow-to-head)
9169                 (let ((head (buffer-string))
9170                       header)
9171                   (with-temp-buffer
9172                     (insert (format "211 %d Article retrieved.\n"
9173                                     (cdr gnus-article-current)))
9174                     (insert head)
9175                     (insert ".\n")
9176                     (let ((nntp-server-buffer (current-buffer)))
9177                       (setq header (car (gnus-get-newsgroup-headers
9178                                          nil t))))
9179                     (save-excursion
9180                       (set-buffer gnus-summary-buffer)
9181                       (gnus-data-set-header
9182                        (gnus-data-find (cdr gnus-article-current))
9183                        header)
9184                       (gnus-summary-update-article-line
9185                        (cdr gnus-article-current) header)
9186                       (if (gnus-summary-goto-subject
9187                            (cdr gnus-article-current) nil t)
9188                           (gnus-summary-update-secondary-mark
9189                            (cdr gnus-article-current))))))))
9190           ;; Update threads.
9191           (set-buffer (or buffer gnus-summary-buffer))
9192           (gnus-summary-update-article (cdr gnus-article-current))
9193           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9194               (gnus-summary-update-secondary-mark
9195                (cdr gnus-article-current))))
9196         ;; Prettify the article buffer again.
9197         (unless no-highlight
9198           (save-excursion
9199             (set-buffer gnus-article-buffer)
9200             ;;;!!! Fix this -- article should be rehighlighted.
9201             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9202             (set-buffer gnus-original-article-buffer)
9203             (gnus-request-article
9204              (cdr gnus-article-current)
9205              (car gnus-article-current) (current-buffer))))
9206         ;; Prettify the summary buffer line.
9207         (when (gnus-visual-p 'summary-highlight 'highlight)
9208           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9209
9210 (defun gnus-summary-edit-wash (key)
9211   "Perform editing command KEY in the article buffer."
9212   (interactive
9213    (list
9214     (progn
9215       (message "%s" (concat (this-command-keys) "- "))
9216       (read-char))))
9217   (message "")
9218   (gnus-summary-edit-article)
9219   (execute-kbd-macro (concat (this-command-keys) key))
9220   (gnus-article-edit-done))
9221
9222 ;;; Respooling
9223
9224 (defun gnus-summary-respool-query (&optional silent trace)
9225   "Query where the respool algorithm would put this article."
9226   (interactive)
9227   (let (gnus-mark-article-hook)
9228     (gnus-summary-select-article)
9229     (save-excursion
9230       (set-buffer gnus-original-article-buffer)
9231       (save-restriction
9232         (message-narrow-to-head)
9233         (let ((groups (nnmail-article-group 'identity trace)))
9234           (unless silent
9235             (if groups
9236                 (message "This message would go to %s"
9237                          (mapconcat 'car groups ", "))
9238               (message "This message would go to no groups"))
9239             groups))))))
9240
9241 (defun gnus-summary-respool-trace ()
9242   "Trace where the respool algorithm would put this article.
9243 Display a buffer showing all fancy splitting patterns which matched."
9244   (interactive)
9245   (gnus-summary-respool-query nil t))
9246
9247 ;; Summary marking commands.
9248
9249 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9250   "Mark articles which has the same subject as read, and then select the next.
9251 If UNMARK is positive, remove any kind of mark.
9252 If UNMARK is negative, tick articles."
9253   (interactive "P")
9254   (when unmark
9255     (setq unmark (prefix-numeric-value unmark)))
9256   (let ((count
9257          (gnus-summary-mark-same-subject
9258           (gnus-summary-article-subject) unmark)))
9259     ;; Select next unread article.  If auto-select-same mode, should
9260     ;; select the first unread article.
9261     (gnus-summary-next-article t (and gnus-auto-select-same
9262                                       (gnus-summary-article-subject)))
9263     (gnus-message 7 "%d article%s marked as %s"
9264                   count (if (= count 1) " is" "s are")
9265                   (if unmark "unread" "read"))))
9266
9267 (defun gnus-summary-kill-same-subject (&optional unmark)
9268   "Mark articles which has the same subject as read.
9269 If UNMARK is positive, remove any kind of mark.
9270 If UNMARK is negative, tick articles."
9271   (interactive "P")
9272   (when unmark
9273     (setq unmark (prefix-numeric-value unmark)))
9274   (let ((count
9275          (gnus-summary-mark-same-subject
9276           (gnus-summary-article-subject) unmark)))
9277     ;; If marked as read, go to next unread subject.
9278     (when (null unmark)
9279       ;; Go to next unread subject.
9280       (gnus-summary-next-subject 1 t))
9281     (gnus-message 7 "%d articles are marked as %s"
9282                   count (if unmark "unread" "read"))))
9283
9284 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9285   "Mark articles with same SUBJECT as read, and return marked number.
9286 If optional argument UNMARK is positive, remove any kinds of marks.
9287 If optional argument UNMARK is negative, mark articles as unread instead."
9288   (let ((count 1))
9289     (save-excursion
9290       (cond
9291        ((null unmark)                   ; Mark as read.
9292         (while (and
9293                 (progn
9294                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9295                   (gnus-summary-show-thread) t)
9296                 (gnus-summary-find-subject subject))
9297           (setq count (1+ count))))
9298        ((> unmark 0)                    ; Tick.
9299         (while (and
9300                 (progn
9301                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9302                   (gnus-summary-show-thread) t)
9303                 (gnus-summary-find-subject subject))
9304           (setq count (1+ count))))
9305        (t                               ; Mark as unread.
9306         (while (and
9307                 (progn
9308                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9309                   (gnus-summary-show-thread) t)
9310                 (gnus-summary-find-subject subject))
9311           (setq count (1+ count)))))
9312       (gnus-set-mode-line 'summary)
9313       ;; Return the number of marked articles.
9314       count)))
9315
9316 (defun gnus-summary-mark-as-processable (n &optional unmark)
9317   "Set the process mark on the next N articles.
9318 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9319 the process mark instead.  The difference between N and the actual
9320 number of articles marked is returned."
9321   (interactive "P")
9322   (if (and (null n) (gnus-region-active-p))
9323       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9324     (setq n (prefix-numeric-value n))
9325     (let ((backward (< n 0))
9326           (n (abs n)))
9327       (while (and
9328               (> n 0)
9329               (if unmark
9330                   (gnus-summary-remove-process-mark
9331                    (gnus-summary-article-number))
9332                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9333               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9334         (setq n (1- n)))
9335       (when (/= 0 n)
9336         (gnus-message 7 "No more articles"))
9337       (gnus-summary-recenter)
9338       (gnus-summary-position-point)
9339       n)))
9340
9341 (defun gnus-summary-unmark-as-processable (n)
9342   "Remove the process mark from the next N articles.
9343 If N is negative, unmark backward instead.  The difference between N and
9344 the actual number of articles unmarked is returned."
9345   (interactive "P")
9346   (gnus-summary-mark-as-processable n t))
9347
9348 (defun gnus-summary-unmark-all-processable ()
9349   "Remove the process mark from all articles."
9350   (interactive)
9351   (save-excursion
9352     (while gnus-newsgroup-processable
9353       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9354   (gnus-summary-position-point))
9355
9356 (defun gnus-summary-add-mark (article type)
9357   "Mark ARTICLE with a mark of TYPE."
9358   (let ((vtype (car (assq type gnus-article-mark-lists)))
9359         var)
9360     (if (not vtype)
9361         (error "No such mark type: %s" type)
9362       (setq var (intern (format "gnus-newsgroup-%s" type)))
9363       (set var (cons article (symbol-value var)))
9364       (if (memq type '(processable cached replied forwarded recent saved))
9365           (gnus-summary-update-secondary-mark article)
9366         ;;; !!! This is bogus.  We should find out what primary
9367         ;;; !!! mark we want to set.
9368         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9369
9370 (defun gnus-summary-mark-as-expirable (n)
9371   "Mark N articles forward as expirable.
9372 If N is negative, mark backward instead.  The difference between N and
9373 the actual number of articles marked is returned."
9374   (interactive "p")
9375   (gnus-summary-mark-forward n gnus-expirable-mark))
9376
9377 (defun gnus-summary-mark-as-spam (n)
9378   "Mark N articles forward as spam.
9379 If N is negative, mark backward instead.  The difference between N and
9380 the actual number of articles marked is returned."
9381   (interactive "p")
9382   (gnus-summary-mark-forward n gnus-spam-mark))
9383
9384 (defun gnus-summary-mark-article-as-replied (article)
9385   "Mark ARTICLE as replied to and update the summary line.
9386 ARTICLE can also be a list of articles."
9387   (interactive (list (gnus-summary-article-number)))
9388   (let ((articles (if (listp article) article (list article))))
9389     (dolist (article articles)
9390       (push article gnus-newsgroup-replied)
9391       (let ((buffer-read-only nil))
9392         (when (gnus-summary-goto-subject article nil t)
9393           (gnus-summary-update-secondary-mark article))))))
9394
9395 (defun gnus-summary-mark-article-as-forwarded (article)
9396   "Mark ARTICLE as forwarded and update the summary line.
9397 ARTICLE can also be a list of articles."
9398   (let ((articles (if (listp article) article (list article))))
9399     (dolist (article articles)
9400       (push article gnus-newsgroup-forwarded)
9401       (let ((buffer-read-only nil))
9402         (when (gnus-summary-goto-subject article nil t)
9403           (gnus-summary-update-secondary-mark article))))))
9404
9405 (defun gnus-summary-set-bookmark (article)
9406   "Set a bookmark in current article."
9407   (interactive (list (gnus-summary-article-number)))
9408   (when (or (not (get-buffer gnus-article-buffer))
9409             (not gnus-current-article)
9410             (not gnus-article-current)
9411             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9412     (error "No current article selected"))
9413   ;; Remove old bookmark, if one exists.
9414   (gnus-pull article gnus-newsgroup-bookmarks)
9415   ;; Set the new bookmark, which is on the form
9416   ;; (article-number . line-number-in-body).
9417   (push
9418    (cons article
9419          (save-excursion
9420            (set-buffer gnus-article-buffer)
9421            (count-lines
9422             (min (point)
9423                  (save-excursion
9424                    (article-goto-body)
9425                    (point)))
9426             (point))))
9427    gnus-newsgroup-bookmarks)
9428   (gnus-message 6 "A bookmark has been added to the current article."))
9429
9430 (defun gnus-summary-remove-bookmark (article)
9431   "Remove the bookmark from the current article."
9432   (interactive (list (gnus-summary-article-number)))
9433   ;; Remove old bookmark, if one exists.
9434   (if (not (assq article gnus-newsgroup-bookmarks))
9435       (gnus-message 6 "No bookmark in current article.")
9436     (gnus-pull article gnus-newsgroup-bookmarks)
9437     (gnus-message 6 "Removed bookmark.")))
9438
9439 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9440 (defun gnus-summary-mark-as-dormant (n)
9441   "Mark N articles forward as dormant.
9442 If N is negative, mark backward instead.  The difference between N and
9443 the actual number of articles marked is returned."
9444   (interactive "p")
9445   (gnus-summary-mark-forward n gnus-dormant-mark))
9446
9447 (defun gnus-summary-set-process-mark (article)
9448   "Set the process mark on ARTICLE and update the summary line."
9449   (setq gnus-newsgroup-processable
9450         (cons article
9451               (delq article gnus-newsgroup-processable)))
9452   (when (gnus-summary-goto-subject article)
9453     (gnus-summary-show-thread)
9454     (gnus-summary-goto-subject article)
9455     (gnus-summary-update-secondary-mark article)))
9456
9457 (defun gnus-summary-remove-process-mark (article)
9458   "Remove the process mark from ARTICLE and update the summary line."
9459   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9460   (when (gnus-summary-goto-subject article)
9461     (gnus-summary-show-thread)
9462     (gnus-summary-goto-subject article)
9463     (gnus-summary-update-secondary-mark article)))
9464
9465 (defun gnus-summary-set-saved-mark (article)
9466   "Set the process mark on ARTICLE and update the summary line."
9467   (push article gnus-newsgroup-saved)
9468   (when (gnus-summary-goto-subject article)
9469     (gnus-summary-update-secondary-mark article)))
9470
9471 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9472   "Mark N articles as read forwards.
9473 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9474 The difference between N and the actual number of articles marked is
9475 returned.
9476 If NO-EXPIRE, auto-expiry will be inhibited."
9477   (interactive "p")
9478   (gnus-summary-show-thread)
9479   (let ((backward (< n 0))
9480         (gnus-summary-goto-unread
9481          (and gnus-summary-goto-unread
9482               (not (eq gnus-summary-goto-unread 'never))
9483               (not (memq mark (list gnus-unread-mark
9484                                     gnus-ticked-mark gnus-dormant-mark)))))
9485         (n (abs n))
9486         (mark (or mark gnus-del-mark)))
9487     (while (and (> n 0)
9488                 (gnus-summary-mark-article nil mark no-expire)
9489                 (zerop (gnus-summary-next-subject
9490                         (if backward -1 1)
9491                         (and gnus-summary-goto-unread
9492                              (not (eq gnus-summary-goto-unread 'never)))
9493                         t)))
9494       (setq n (1- n)))
9495     (when (/= 0 n)
9496       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9497     (gnus-summary-recenter)
9498     (gnus-summary-position-point)
9499     (gnus-set-mode-line 'summary)
9500     n))
9501
9502 (defun gnus-summary-mark-article-as-read (mark)
9503   "Mark the current article quickly as read with MARK."
9504   (let ((article (gnus-summary-article-number)))
9505     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9506     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9507     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9508     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9509     (push (cons article mark) gnus-newsgroup-reads)
9510     ;; Possibly remove from cache, if that is used.
9511     (when gnus-use-cache
9512       (gnus-cache-enter-remove-article article))
9513     ;; Allow the backend to change the mark.
9514     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9515     ;; Check for auto-expiry.
9516     (when (and gnus-newsgroup-auto-expire
9517                (memq mark gnus-auto-expirable-marks))
9518       (setq mark gnus-expirable-mark)
9519       ;; Let the backend know about the mark change.
9520       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9521       (push article gnus-newsgroup-expirable))
9522     ;; Set the mark in the buffer.
9523     (gnus-summary-update-mark mark 'unread)
9524     t))
9525
9526 (defun gnus-summary-mark-article-as-unread (mark)
9527   "Mark the current article quickly as unread with MARK."
9528   (let* ((article (gnus-summary-article-number))
9529          (old-mark (gnus-summary-article-mark article)))
9530     ;; Allow the backend to change the mark.
9531     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9532     (if (eq mark old-mark)
9533         t
9534       (if (<= article 0)
9535           (progn
9536             (gnus-error 1 "Can't mark negative article numbers")
9537             nil)
9538         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9539         (setq gnus-newsgroup-spam-marked
9540               (delq article gnus-newsgroup-spam-marked))
9541         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9542         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9543         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9544         (cond ((= mark gnus-ticked-mark)
9545                (setq gnus-newsgroup-marked
9546                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9547                                               article)))
9548               ((= mark gnus-spam-mark)
9549                (setq gnus-newsgroup-spam-marked
9550                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9551                                               article)))
9552               ((= mark gnus-dormant-mark)
9553                (setq gnus-newsgroup-dormant
9554                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9555                                               article)))
9556               (t
9557                (setq gnus-newsgroup-unreads
9558                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9559                                               article))))
9560         (gnus-pull article gnus-newsgroup-reads)
9561
9562         ;; See whether the article is to be put in the cache.
9563         (and gnus-use-cache
9564              (vectorp (gnus-summary-article-header article))
9565              (save-excursion
9566                (gnus-cache-possibly-enter-article
9567                 gnus-newsgroup-name article
9568                 (= mark gnus-ticked-mark)
9569                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9570
9571         ;; Fix the mark.
9572         (gnus-summary-update-mark mark 'unread)
9573         t))))
9574
9575 (defun gnus-summary-mark-article (&optional article mark no-expire)
9576   "Mark ARTICLE with MARK.  MARK can be any character.
9577 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9578 `??' (dormant) and `?E' (expirable).
9579 If MARK is nil, then the default character `?r' is used.
9580 If ARTICLE is nil, then the article on the current line will be
9581 marked.
9582 If NO-EXPIRE, auto-expiry will be inhibited."
9583   ;; The mark might be a string.
9584   (when (stringp mark)
9585     (setq mark (aref mark 0)))
9586   ;; If no mark is given, then we check auto-expiring.
9587   (when (null mark)
9588     (setq mark gnus-del-mark))
9589   (when (and (not no-expire)
9590              gnus-newsgroup-auto-expire
9591              (memq mark gnus-auto-expirable-marks))
9592     (setq mark gnus-expirable-mark))
9593   (let ((article (or article (gnus-summary-article-number)))
9594         (old-mark (gnus-summary-article-mark article)))
9595     ;; Allow the backend to change the mark.
9596     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9597     (if (eq mark old-mark)
9598         t
9599       (unless article
9600         (error "No article on current line"))
9601       (if (not (if (or (= mark gnus-unread-mark)
9602                        (= mark gnus-ticked-mark)
9603                        (= mark gnus-spam-mark)
9604                        (= mark gnus-dormant-mark))
9605                    (gnus-mark-article-as-unread article mark)
9606                  (gnus-mark-article-as-read article mark)))
9607           t
9608         ;; See whether the article is to be put in the cache.
9609         (and gnus-use-cache
9610              (not (= mark gnus-canceled-mark))
9611              (vectorp (gnus-summary-article-header article))
9612              (save-excursion
9613                (gnus-cache-possibly-enter-article
9614                 gnus-newsgroup-name article
9615                 (= mark gnus-ticked-mark)
9616                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9617
9618         (when (gnus-summary-goto-subject article nil t)
9619           (let ((buffer-read-only nil))
9620             (gnus-summary-show-thread)
9621             ;; Fix the mark.
9622             (gnus-summary-update-mark mark 'unread)
9623             t))))))
9624
9625 (defun gnus-summary-update-secondary-mark (article)
9626   "Update the secondary (read, process, cache) mark."
9627   (gnus-summary-update-mark
9628    (cond ((memq article gnus-newsgroup-processable)
9629           gnus-process-mark)
9630          ((memq article gnus-newsgroup-cached)
9631           gnus-cached-mark)
9632          ((memq article gnus-newsgroup-replied)
9633           gnus-replied-mark)
9634          ((memq article gnus-newsgroup-forwarded)
9635           gnus-forwarded-mark)
9636          ((memq article gnus-newsgroup-saved)
9637           gnus-saved-mark)
9638          ((memq article gnus-newsgroup-recent)
9639           gnus-recent-mark)
9640          ((memq article gnus-newsgroup-unseen)
9641           gnus-unseen-mark)
9642          (t gnus-no-mark))
9643    'replied)
9644   (when (gnus-visual-p 'summary-highlight 'highlight)
9645     (gnus-run-hooks 'gnus-summary-update-hook))
9646   t)
9647
9648 (defun gnus-summary-update-download-mark (article)
9649   "Update the secondary (read, process, cache) mark."
9650   (gnus-summary-update-mark
9651    (cond ((memq article gnus-newsgroup-undownloaded) 
9652           gnus-undownloaded-mark)
9653          (gnus-newsgroup-agentized
9654           gnus-downloaded-mark)
9655          (t
9656           gnus-no-mark))
9657    'download)
9658   (gnus-summary-update-line t)
9659   t)
9660
9661 (defun gnus-summary-update-mark (mark type)
9662   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9663         (buffer-read-only nil))
9664     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9665     (when forward
9666       (when (looking-at "\r")
9667         (incf forward))
9668       (when (<= (+ forward (point)) (point-max))
9669         ;; Go to the right position on the line.
9670         (goto-char (+ forward (point)))
9671         ;; Replace the old mark with the new mark.
9672         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9673         ;; Optionally update the marks by some user rule.
9674         (when (eq type 'unread)
9675           (gnus-data-set-mark
9676            (gnus-data-find (gnus-summary-article-number)) mark)
9677           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9678
9679 (defun gnus-mark-article-as-read (article &optional mark)
9680   "Enter ARTICLE in the pertinent lists and remove it from others."
9681   ;; Make the article expirable.
9682   (let ((mark (or mark gnus-del-mark)))
9683     (setq gnus-newsgroup-expirable
9684           (if (= mark gnus-expirable-mark)
9685               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9686             (delq article gnus-newsgroup-expirable)))
9687     ;; Remove from unread and marked lists.
9688     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9689     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9690     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9691     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9692     (push (cons article mark) gnus-newsgroup-reads)
9693     ;; Possibly remove from cache, if that is used.
9694     (when gnus-use-cache
9695       (gnus-cache-enter-remove-article article))
9696     t))
9697
9698 (defun gnus-mark-article-as-unread (article &optional mark)
9699   "Enter ARTICLE in the pertinent lists and remove it from others."
9700   (let ((mark (or mark gnus-ticked-mark)))
9701     (if (<= article 0)
9702         (progn
9703           (gnus-error 1 "Can't mark negative article numbers")
9704           nil)
9705       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9706             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9707             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9708             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9709             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9710
9711       ;; Unsuppress duplicates?
9712       (when gnus-suppress-duplicates
9713         (gnus-dup-unsuppress-article article))
9714
9715       (cond ((= mark gnus-ticked-mark)
9716              (setq gnus-newsgroup-marked
9717                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9718             ((= mark gnus-spam-mark)
9719              (setq gnus-newsgroup-spam-marked
9720                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9721                                             article)))
9722             ((= mark gnus-dormant-mark)
9723              (setq gnus-newsgroup-dormant
9724                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9725             (t
9726              (setq gnus-newsgroup-unreads
9727                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9728       (gnus-pull article gnus-newsgroup-reads)
9729       t)))
9730
9731 (defalias 'gnus-summary-mark-as-unread-forward
9732   'gnus-summary-tick-article-forward)
9733 (make-obsolete 'gnus-summary-mark-as-unread-forward
9734                'gnus-summary-tick-article-forward)
9735 (defun gnus-summary-tick-article-forward (n)
9736   "Tick N articles forwards.
9737 If N is negative, tick backwards instead.
9738 The difference between N and the number of articles ticked is returned."
9739   (interactive "p")
9740   (gnus-summary-mark-forward n gnus-ticked-mark))
9741
9742 (defalias 'gnus-summary-mark-as-unread-backward
9743   'gnus-summary-tick-article-backward)
9744 (make-obsolete 'gnus-summary-mark-as-unread-backward
9745                'gnus-summary-tick-article-backward)
9746 (defun gnus-summary-tick-article-backward (n)
9747   "Tick N articles backwards.
9748 The difference between N and the number of articles ticked is returned."
9749   (interactive "p")
9750   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9751
9752 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9753 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9754 (defun gnus-summary-tick-article (&optional article clear-mark)
9755   "Mark current article as unread.
9756 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9757 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9758   (interactive)
9759   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9760                                        gnus-ticked-mark)))
9761
9762 (defun gnus-summary-mark-as-read-forward (n)
9763   "Mark N articles as read forwards.
9764 If N is negative, mark backwards instead.
9765 The difference between N and the actual number of articles marked is
9766 returned."
9767   (interactive "p")
9768   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9769
9770 (defun gnus-summary-mark-as-read-backward (n)
9771   "Mark the N articles as read backwards.
9772 The difference between N and the actual number of articles marked is
9773 returned."
9774   (interactive "p")
9775   (gnus-summary-mark-forward
9776    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9777
9778 (defun gnus-summary-mark-as-read (&optional article mark)
9779   "Mark current article as read.
9780 ARTICLE specifies the article to be marked as read.
9781 MARK specifies a string to be inserted at the beginning of the line."
9782   (gnus-summary-mark-article article mark))
9783
9784 (defun gnus-summary-clear-mark-forward (n)
9785   "Clear marks from N articles forward.
9786 If N is negative, clear backward instead.
9787 The difference between N and the number of marks cleared is returned."
9788   (interactive "p")
9789   (gnus-summary-mark-forward n gnus-unread-mark))
9790
9791 (defun gnus-summary-clear-mark-backward (n)
9792   "Clear marks from N articles backward.
9793 The difference between N and the number of marks cleared is returned."
9794   (interactive "p")
9795   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9796
9797 (defun gnus-summary-mark-unread-as-read ()
9798   "Intended to be used by `gnus-summary-mark-article-hook'."
9799   (when (memq gnus-current-article gnus-newsgroup-unreads)
9800     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9801
9802 (defun gnus-summary-mark-read-and-unread-as-read ()
9803   "Intended to be used by `gnus-summary-mark-article-hook'."
9804   (let ((mark (gnus-summary-article-mark)))
9805     (when (or (gnus-unread-mark-p mark)
9806               (gnus-read-mark-p mark))
9807       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9808
9809 (defun gnus-summary-mark-unread-as-ticked ()
9810   "Intended to be used by `gnus-summary-mark-article-hook'."
9811   (when (memq gnus-current-article gnus-newsgroup-unreads)
9812     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9813
9814 (defun gnus-summary-mark-region-as-read (point mark all)
9815   "Mark all unread articles between point and mark as read.
9816 If given a prefix, mark all articles between point and mark as read,
9817 even ticked and dormant ones."
9818   (interactive "r\nP")
9819   (save-excursion
9820     (let (article)
9821       (goto-char point)
9822       (beginning-of-line)
9823       (while (and
9824               (< (point) mark)
9825               (progn
9826                 (when (or all
9827                           (memq (setq article (gnus-summary-article-number))
9828                                 gnus-newsgroup-unreads))
9829                   (gnus-summary-mark-article article gnus-del-mark))
9830                 t)
9831               (gnus-summary-find-next))))))
9832
9833 (defun gnus-summary-mark-below (score mark)
9834   "Mark articles with score less than SCORE with MARK."
9835   (interactive "P\ncMark: ")
9836   (setq score (if score
9837                   (prefix-numeric-value score)
9838                 (or gnus-summary-default-score 0)))
9839   (save-excursion
9840     (set-buffer gnus-summary-buffer)
9841     (goto-char (point-min))
9842     (while
9843         (progn
9844           (and (< (gnus-summary-article-score) score)
9845                (gnus-summary-mark-article nil mark))
9846           (gnus-summary-find-next)))))
9847
9848 (defun gnus-summary-kill-below (&optional score)
9849   "Mark articles with score below SCORE as read."
9850   (interactive "P")
9851   (gnus-summary-mark-below score gnus-killed-mark))
9852
9853 (defun gnus-summary-clear-above (&optional score)
9854   "Clear all marks from articles with score above SCORE."
9855   (interactive "P")
9856   (gnus-summary-mark-above score gnus-unread-mark))
9857
9858 (defun gnus-summary-tick-above (&optional score)
9859   "Tick all articles with score above SCORE."
9860   (interactive "P")
9861   (gnus-summary-mark-above score gnus-ticked-mark))
9862
9863 (defun gnus-summary-mark-above (score mark)
9864   "Mark articles with score over SCORE with MARK."
9865   (interactive "P\ncMark: ")
9866   (setq score (if score
9867                   (prefix-numeric-value score)
9868                 (or gnus-summary-default-score 0)))
9869   (save-excursion
9870     (set-buffer gnus-summary-buffer)
9871     (goto-char (point-min))
9872     (while (and (progn
9873                   (when (> (gnus-summary-article-score) score)
9874                     (gnus-summary-mark-article nil mark))
9875                   t)
9876                 (gnus-summary-find-next)))))
9877
9878 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9879 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9880 (defun gnus-summary-limit-include-expunged (&optional no-error)
9881   "Display all the hidden articles that were expunged for low scores."
9882   (interactive)
9883   (let ((buffer-read-only nil))
9884     (let ((scored gnus-newsgroup-scored)
9885           headers h)
9886       (while scored
9887         (unless (gnus-summary-article-header (caar scored))
9888           (and (setq h (gnus-number-to-header (caar scored)))
9889                (< (cdar scored) gnus-summary-expunge-below)
9890                (push h headers)))
9891         (setq scored (cdr scored)))
9892       (if (not headers)
9893           (when (not no-error)
9894             (error "No expunged articles hidden"))
9895         (goto-char (point-min))
9896         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9897         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9898         (mapcar (lambda (x) (push (mail-header-number x)
9899                                   gnus-newsgroup-limit))
9900                 headers)
9901         (gnus-summary-prepare-unthreaded (nreverse headers))
9902         (goto-char (point-min))
9903         (gnus-summary-position-point)
9904         t))))
9905
9906 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9907   "Mark all unread articles in this newsgroup as read.
9908 If prefix argument ALL is non-nil, ticked and dormant articles will
9909 also be marked as read.
9910 If QUIETLY is non-nil, no questions will be asked.
9911 If TO-HERE is non-nil, it should be a point in the buffer.  All
9912 articles before (after, if REVERSE is set) this point will be marked as read.
9913 Note that this function will only catch up the unread article
9914 in the current summary buffer limitation.
9915 The number of articles marked as read is returned."
9916   (interactive "P")
9917   (prog1
9918       (save-excursion
9919         (when (or quietly
9920                   (not gnus-interactive-catchup) ;Without confirmation?
9921                   gnus-expert-user
9922                   (gnus-y-or-n-p
9923                    (if all
9924                        "Mark absolutely all articles as read? "
9925                      "Mark all unread articles as read? ")))
9926           (if (and not-mark
9927                    (not gnus-newsgroup-adaptive)
9928                    (not gnus-newsgroup-auto-expire)
9929                    (not gnus-suppress-duplicates)
9930                    (or (not gnus-use-cache)
9931                        (eq gnus-use-cache 'passive)))
9932               (progn
9933                 (when all
9934                   (setq gnus-newsgroup-marked nil
9935                         gnus-newsgroup-spam-marked nil
9936                         gnus-newsgroup-dormant nil))
9937                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9938             ;; We actually mark all articles as canceled, which we
9939             ;; have to do when using auto-expiry or adaptive scoring.
9940             (gnus-summary-show-all-threads)
9941             (if (and to-here reverse)
9942                 (progn
9943                   (goto-char to-here)
9944                   (while (and
9945                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9946                           (gnus-summary-find-next (not all) nil nil t))))
9947               (when (gnus-summary-first-subject (not all) t)
9948                 (while (and
9949                         (if to-here (< (point) to-here) t)
9950                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9951                         (gnus-summary-find-next (not all) nil nil t)))))
9952             (gnus-set-mode-line 'summary))
9953           t))
9954     (gnus-summary-position-point)))
9955
9956 (defun gnus-summary-catchup-to-here (&optional all)
9957   "Mark all unticked articles before the current one as read.
9958 If ALL is non-nil, also mark ticked and dormant articles as read."
9959   (interactive "P")
9960   (save-excursion
9961     (gnus-save-hidden-threads
9962       (let ((beg (point)))
9963         ;; We check that there are unread articles.
9964         (when (or all (gnus-summary-find-prev))
9965           (gnus-summary-catchup all t beg)))))
9966   (gnus-summary-position-point))
9967
9968 (defun gnus-summary-catchup-from-here (&optional all)
9969   "Mark all unticked articles after the current one as read.
9970 If ALL is non-nil, also mark ticked and dormant articles as read."
9971   (interactive "P")
9972   (save-excursion
9973     (gnus-save-hidden-threads
9974       (let ((beg (point)))
9975         ;; We check that there are unread articles.
9976         (when (or all (gnus-summary-find-next))
9977           (gnus-summary-catchup all t beg nil t)))))
9978
9979   (gnus-summary-position-point))
9980 (defun gnus-summary-catchup-all (&optional quietly)
9981   "Mark all articles in this newsgroup as read.
9982 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9983 instead, which marks only unread articles as read."
9984   (interactive "P")
9985   (gnus-summary-catchup t quietly))
9986
9987 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9988   "Mark all unread articles in this group as read, then exit.
9989 If prefix argument ALL is non-nil, all articles are marked as read.
9990 If QUIETLY is non-nil, no questions will be asked."
9991   (interactive "P")
9992   (when (gnus-summary-catchup all quietly nil 'fast)
9993     ;; Select next newsgroup or exit.
9994     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9995              (eq gnus-auto-select-next 'quietly))
9996         (gnus-summary-next-group nil)
9997       (gnus-summary-exit))))
9998
9999 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10000   "Mark all articles in this newsgroup as read, and then exit.
10001 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10002 instead, which marks only unread articles as read."
10003   (interactive "P")
10004   (gnus-summary-catchup-and-exit t quietly))
10005
10006 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10007   "Mark all articles in this group as read and select the next group.
10008 If given a prefix, mark all articles, unread as well as ticked, as
10009 read."
10010   (interactive "P")
10011   (save-excursion
10012     (gnus-summary-catchup all))
10013   (gnus-summary-next-group))
10014
10015 ;;;
10016 ;;; with article
10017 ;;;
10018
10019 (defmacro gnus-with-article (article &rest forms)
10020   "Select ARTICLE and perform FORMS in the original article buffer.
10021 Then replace the article with the result."
10022   `(progn
10023      ;; We don't want the article to be marked as read.
10024      (let (gnus-mark-article-hook)
10025        (gnus-summary-select-article t t nil ,article))
10026      (set-buffer gnus-original-article-buffer)
10027      ,@forms
10028      (if (not (gnus-check-backend-function
10029                'request-replace-article (car gnus-article-current)))
10030          (gnus-message 5 "Read-only group; not replacing")
10031        (unless (gnus-request-replace-article
10032                 ,article (car gnus-article-current)
10033                 (current-buffer) t)
10034          (error "Couldn't replace article")))
10035      ;; The cache and backlog have to be flushed somewhat.
10036      (when gnus-keep-backlog
10037        (gnus-backlog-remove-article
10038         (car gnus-article-current) (cdr gnus-article-current)))
10039      (when gnus-use-cache
10040        (gnus-cache-update-article
10041         (car gnus-article-current) (cdr gnus-article-current)))))
10042
10043 (put 'gnus-with-article 'lisp-indent-function 1)
10044 (put 'gnus-with-article 'edebug-form-spec '(form body))
10045
10046 ;; Thread-based commands.
10047
10048 (defun gnus-summary-articles-in-thread (&optional article)
10049   "Return a list of all articles in the current thread.
10050 If ARTICLE is non-nil, return all articles in the thread that starts
10051 with that article."
10052   (let* ((article (or article (gnus-summary-article-number)))
10053          (data (gnus-data-find-list article))
10054          (top-level (gnus-data-level (car data)))
10055          (top-subject
10056           (cond ((null gnus-thread-operation-ignore-subject)
10057                  (gnus-simplify-subject-re
10058                   (mail-header-subject (gnus-data-header (car data)))))
10059                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10060                  (gnus-simplify-subject-fuzzy
10061                   (mail-header-subject (gnus-data-header (car data)))))
10062                 (t nil)))
10063          (end-point (save-excursion
10064                       (if (gnus-summary-go-to-next-thread)
10065                           (point) (point-max))))
10066          articles)
10067     (while (and data
10068                 (< (gnus-data-pos (car data)) end-point))
10069       (when (or (not top-subject)
10070                 (string= top-subject
10071                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10072                              (gnus-simplify-subject-fuzzy
10073                               (mail-header-subject
10074                                (gnus-data-header (car data))))
10075                            (gnus-simplify-subject-re
10076                             (mail-header-subject
10077                              (gnus-data-header (car data)))))))
10078         (push (gnus-data-number (car data)) articles))
10079       (unless (and (setq data (cdr data))
10080                    (> (gnus-data-level (car data)) top-level))
10081         (setq data nil)))
10082     ;; Return the list of articles.
10083     (nreverse articles)))
10084
10085 (defun gnus-summary-rethread-current ()
10086   "Rethread the thread the current article is part of."
10087   (interactive)
10088   (let* ((gnus-show-threads t)
10089          (article (gnus-summary-article-number))
10090          (id (mail-header-id (gnus-summary-article-header)))
10091          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10092     (unless id
10093       (error "No article on the current line"))
10094     (gnus-rebuild-thread id)
10095     (gnus-summary-goto-subject article)))
10096
10097 (defun gnus-summary-reparent-thread ()
10098   "Make the current article child of the marked (or previous) article.
10099
10100 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10101 is non-nil or the Subject: of both articles are the same."
10102   (interactive)
10103   (unless (not (gnus-group-read-only-p))
10104     (error "The current newsgroup does not support article editing"))
10105   (unless (<= (length gnus-newsgroup-processable) 1)
10106     (error "No more than one article may be marked"))
10107   (save-window-excursion
10108     (let ((gnus-article-buffer " *reparent*")
10109           (current-article (gnus-summary-article-number))
10110           ;; First grab the marked article, otherwise one line up.
10111           (parent-article (if (not (null gnus-newsgroup-processable))
10112                               (car gnus-newsgroup-processable)
10113                             (save-excursion
10114                               (if (eq (forward-line -1) 0)
10115                                   (gnus-summary-article-number)
10116                                 (error "Beginning of summary buffer"))))))
10117       (unless (not (eq current-article parent-article))
10118         (error "An article may not be self-referential"))
10119       (let ((message-id (mail-header-id
10120                          (gnus-summary-article-header parent-article))))
10121         (unless (and message-id (not (equal message-id "")))
10122           (error "No message-id in desired parent"))
10123         (gnus-with-article current-article
10124           (save-restriction
10125             (goto-char (point-min))
10126             (message-narrow-to-head)
10127             (if (re-search-forward "^References: " nil t)
10128                 (progn
10129                   (re-search-forward "^[^ \t]" nil t)
10130                   (forward-line -1)
10131                   (end-of-line)
10132                   (insert " " message-id))
10133               (insert "References: " message-id "\n"))))
10134         (set-buffer gnus-summary-buffer)
10135         (gnus-summary-unmark-all-processable)
10136         (gnus-summary-update-article current-article)
10137         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10138             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10139         (gnus-summary-rethread-current)
10140         (gnus-message 3 "Article %d is now the child of article %d"
10141                       current-article parent-article)))))
10142
10143 (defun gnus-summary-toggle-threads (&optional arg)
10144   "Toggle showing conversation threads.
10145 If ARG is positive number, turn showing conversation threads on."
10146   (interactive "P")
10147   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10148     (setq gnus-show-threads
10149           (if (null arg) (not gnus-show-threads)
10150             (> (prefix-numeric-value arg) 0)))
10151     (gnus-summary-prepare)
10152     (gnus-summary-goto-subject current)
10153     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10154     (gnus-summary-position-point)))
10155
10156 (defun gnus-summary-show-all-threads ()
10157   "Show all threads."
10158   (interactive)
10159   (save-excursion
10160     (let ((buffer-read-only nil))
10161       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10162   (gnus-summary-position-point))
10163
10164 (defun gnus-summary-show-thread ()
10165   "Show thread subtrees.
10166 Returns nil if no thread was there to be shown."
10167   (interactive)
10168   (let ((buffer-read-only nil)
10169         (orig (point))
10170         ;; first goto end then to beg, to have point at beg after let
10171         (end (progn (end-of-line) (point)))
10172         (beg (progn (beginning-of-line) (point))))
10173     (prog1
10174         ;; Any hidden lines here?
10175         (search-forward "\r" end t)
10176       (subst-char-in-region beg end ?\^M ?\n t)
10177       (goto-char orig)
10178       (gnus-summary-position-point))))
10179
10180 (defun gnus-summary-maybe-hide-threads ()
10181   "If requested, hide the threads that should be hidden."
10182   (when (and gnus-show-threads
10183              gnus-thread-hide-subtree)
10184     (gnus-summary-hide-all-threads
10185      (if (or (consp gnus-thread-hide-subtree)
10186              (gnus-functionp gnus-thread-hide-subtree))
10187          (gnus-make-predicate gnus-thread-hide-subtree)
10188        nil))))
10189
10190 ;;; Hiding predicates.
10191
10192 (defun gnus-article-unread-p (header)
10193   (memq (mail-header-number header) gnus-newsgroup-unreads))
10194
10195 (defun gnus-article-unseen-p (header)
10196   (memq (mail-header-number header) gnus-newsgroup-unseen))
10197
10198 (defun gnus-map-articles (predicate articles)
10199   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10200   (apply 'gnus-or (mapcar predicate
10201                           (mapcar 'gnus-summary-article-header articles))))
10202
10203 (defun gnus-summary-hide-all-threads (&optional predicate)
10204   "Hide all thread subtrees.
10205 If PREDICATE is supplied, threads that satisfy this predicate
10206 will not be hidden."
10207   (interactive)
10208   (save-excursion
10209     (goto-char (point-min))
10210     (let ((end nil))
10211       (while (not end)
10212         (when (or (not predicate)
10213                   (gnus-map-articles
10214                    predicate (gnus-summary-article-children)))
10215             (gnus-summary-hide-thread))
10216         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10217   (gnus-summary-position-point))
10218
10219 (defun gnus-summary-hide-thread ()
10220   "Hide thread subtrees.
10221 If PREDICATE is supplied, threads that satisfy this predicate
10222 will not be hidden.
10223 Returns nil if no threads were there to be hidden."
10224   (interactive)
10225   (let ((buffer-read-only nil)
10226         (start (point))
10227         (article (gnus-summary-article-number)))
10228     (goto-char start)
10229     ;; Go forward until either the buffer ends or the subthread
10230     ;; ends.
10231     (when (and (not (eobp))
10232                (or (zerop (gnus-summary-next-thread 1 t))
10233                    (goto-char (point-max))))
10234       (prog1
10235           (if (and (> (point) start)
10236                    (search-backward "\n" start t))
10237               (progn
10238                 (subst-char-in-region start (point) ?\n ?\^M)
10239                 (gnus-summary-goto-subject article))
10240             (goto-char start)
10241             nil)))))
10242
10243 (defun gnus-summary-go-to-next-thread (&optional previous)
10244   "Go to the same level (or less) next thread.
10245 If PREVIOUS is non-nil, go to previous thread instead.
10246 Return the article number moved to, or nil if moving was impossible."
10247   (let ((level (gnus-summary-thread-level))
10248         (way (if previous -1 1))
10249         (beg (point)))
10250     (forward-line way)
10251     (while (and (not (eobp))
10252                 (< level (gnus-summary-thread-level)))
10253       (forward-line way))
10254     (if (eobp)
10255         (progn
10256           (goto-char beg)
10257           nil)
10258       (setq beg (point))
10259       (prog1
10260           (gnus-summary-article-number)
10261         (goto-char beg)))))
10262
10263 (defun gnus-summary-next-thread (n &optional silent)
10264   "Go to the same level next N'th thread.
10265 If N is negative, search backward instead.
10266 Returns the difference between N and the number of skips actually
10267 done.
10268
10269 If SILENT, don't output messages."
10270   (interactive "p")
10271   (let ((backward (< n 0))
10272         (n (abs n)))
10273     (while (and (> n 0)
10274                 (gnus-summary-go-to-next-thread backward))
10275       (decf n))
10276     (unless silent
10277       (gnus-summary-position-point))
10278     (when (and (not silent) (/= 0 n))
10279       (gnus-message 7 "No more threads"))
10280     n))
10281
10282 (defun gnus-summary-prev-thread (n)
10283   "Go to the same level previous N'th thread.
10284 Returns the difference between N and the number of skips actually
10285 done."
10286   (interactive "p")
10287   (gnus-summary-next-thread (- n)))
10288
10289 (defun gnus-summary-go-down-thread ()
10290   "Go down one level in the current thread."
10291   (let ((children (gnus-summary-article-children)))
10292     (when children
10293       (gnus-summary-goto-subject (car children)))))
10294
10295 (defun gnus-summary-go-up-thread ()
10296   "Go up one level in the current thread."
10297   (let ((parent (gnus-summary-article-parent)))
10298     (when parent
10299       (gnus-summary-goto-subject parent))))
10300
10301 (defun gnus-summary-down-thread (n)
10302   "Go down thread N steps.
10303 If N is negative, go up instead.
10304 Returns the difference between N and how many steps down that were
10305 taken."
10306   (interactive "p")
10307   (let ((up (< n 0))
10308         (n (abs n)))
10309     (while (and (> n 0)
10310                 (if up (gnus-summary-go-up-thread)
10311                   (gnus-summary-go-down-thread)))
10312       (setq n (1- n)))
10313     (gnus-summary-position-point)
10314     (when (/= 0 n)
10315       (gnus-message 7 "Can't go further"))
10316     n))
10317
10318 (defun gnus-summary-up-thread (n)
10319   "Go up thread N steps.
10320 If N is negative, go down instead.
10321 Returns the difference between N and how many steps down that were
10322 taken."
10323   (interactive "p")
10324   (gnus-summary-down-thread (- n)))
10325
10326 (defun gnus-summary-top-thread ()
10327   "Go to the top of the thread."
10328   (interactive)
10329   (while (gnus-summary-go-up-thread))
10330   (gnus-summary-article-number))
10331
10332 (defun gnus-summary-kill-thread (&optional unmark)
10333   "Mark articles under current thread as read.
10334 If the prefix argument is positive, remove any kinds of marks.
10335 If the prefix argument is negative, tick articles instead."
10336   (interactive "P")
10337   (when unmark
10338     (setq unmark (prefix-numeric-value unmark)))
10339   (let ((articles (gnus-summary-articles-in-thread)))
10340     (save-excursion
10341       ;; Expand the thread.
10342       (gnus-summary-show-thread)
10343       ;; Mark all the articles.
10344       (while articles
10345         (gnus-summary-goto-subject (car articles))
10346         (cond ((null unmark)
10347                (gnus-summary-mark-article-as-read gnus-killed-mark))
10348               ((> unmark 0)
10349                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10350               (t
10351                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10352         (setq articles (cdr articles))))
10353     ;; Hide killed subtrees.
10354     (and (null unmark)
10355          gnus-thread-hide-killed
10356          (gnus-summary-hide-thread))
10357     ;; If marked as read, go to next unread subject.
10358     (when (null unmark)
10359       ;; Go to next unread subject.
10360       (gnus-summary-next-subject 1 t)))
10361   (gnus-set-mode-line 'summary))
10362
10363 ;; Summary sorting commands
10364
10365 (defun gnus-summary-sort-by-number (&optional reverse)
10366   "Sort the summary buffer by article number.
10367 Argument REVERSE means reverse order."
10368   (interactive "P")
10369   (gnus-summary-sort 'number reverse))
10370
10371 (defun gnus-summary-sort-by-random (&optional reverse)
10372   "Randomize the order in the summary buffer.
10373 Argument REVERSE means to randomize in reverse order."
10374   (interactive "P")
10375   (gnus-summary-sort 'random reverse))
10376
10377 (defun gnus-summary-sort-by-author (&optional reverse)
10378   "Sort the summary buffer by author name alphabetically.
10379 If `case-fold-search' is non-nil, case of letters is ignored.
10380 Argument REVERSE means reverse order."
10381   (interactive "P")
10382   (gnus-summary-sort 'author reverse))
10383
10384 (defun gnus-summary-sort-by-subject (&optional reverse)
10385   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10386 If `case-fold-search' is non-nil, case of letters is ignored.
10387 Argument REVERSE means reverse order."
10388   (interactive "P")
10389   (gnus-summary-sort 'subject reverse))
10390
10391 (defun gnus-summary-sort-by-date (&optional reverse)
10392   "Sort the summary buffer by date.
10393 Argument REVERSE means reverse order."
10394   (interactive "P")
10395   (gnus-summary-sort 'date reverse))
10396
10397 (defun gnus-summary-sort-by-score (&optional reverse)
10398   "Sort the summary buffer by score.
10399 Argument REVERSE means reverse order."
10400   (interactive "P")
10401   (gnus-summary-sort 'score reverse))
10402
10403 (defun gnus-summary-sort-by-lines (&optional reverse)
10404   "Sort the summary buffer by the number of lines.
10405 Argument REVERSE means reverse order."
10406   (interactive "P")
10407   (gnus-summary-sort 'lines reverse))
10408
10409 (defun gnus-summary-sort-by-chars (&optional reverse)
10410   "Sort the summary buffer by article length.
10411 Argument REVERSE means reverse order."
10412   (interactive "P")
10413   (gnus-summary-sort 'chars reverse))
10414
10415 (defun gnus-summary-sort-by-original (&optional reverse)
10416   "Sort the summary buffer using the default sorting method.
10417 Argument REVERSE means reverse order."
10418   (interactive "P")
10419   (let* ((buffer-read-only)
10420          (gnus-summary-prepare-hook nil))
10421     ;; We do the sorting by regenerating the threads.
10422     (gnus-summary-prepare)
10423     ;; Hide subthreads if needed.
10424     (gnus-summary-maybe-hide-threads)))
10425
10426 (defun gnus-summary-sort (predicate reverse)
10427   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10428   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10429          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10430          (gnus-thread-sort-functions
10431           (if (not reverse)
10432               thread
10433             `(lambda (t1 t2)
10434                (,thread t2 t1))))
10435          (gnus-sort-gathered-threads-function
10436           gnus-thread-sort-functions)
10437          (gnus-article-sort-functions
10438           (if (not reverse)
10439               article
10440             `(lambda (t1 t2)
10441                (,article t2 t1))))
10442          (buffer-read-only)
10443          (gnus-summary-prepare-hook nil))
10444     ;; We do the sorting by regenerating the threads.
10445     (gnus-summary-prepare)
10446     ;; Hide subthreads if needed.
10447     (gnus-summary-maybe-hide-threads)))
10448
10449 ;; Summary saving commands.
10450
10451 (defun gnus-summary-save-article (&optional n not-saved)
10452   "Save the current article using the default saver function.
10453 If N is a positive number, save the N next articles.
10454 If N is a negative number, save the N previous articles.
10455 If N is nil and any articles have been marked with the process mark,
10456 save those articles instead.
10457 The variable `gnus-default-article-saver' specifies the saver function."
10458   (interactive "P")
10459   (let* ((articles (gnus-summary-work-articles n))
10460          (save-buffer (save-excursion
10461                         (nnheader-set-temp-buffer " *Gnus Save*")))
10462          (num (length articles))
10463          header file)
10464     (dolist (article articles)
10465       (setq header (gnus-summary-article-header article))
10466       (if (not (vectorp header))
10467           ;; This is a pseudo-article.
10468           (if (assq 'name header)
10469               (gnus-copy-file (cdr (assq 'name header)))
10470             (gnus-message 1 "Article %d is unsaveable" article))
10471         ;; This is a real article.
10472         (save-window-excursion
10473           (let ((gnus-display-mime-function nil)
10474                 (gnus-article-prepare-hook nil))
10475             (gnus-summary-select-article t nil nil article)))
10476         (save-excursion
10477           (set-buffer save-buffer)
10478           (erase-buffer)
10479           (insert-buffer-substring gnus-original-article-buffer))
10480         (setq file (gnus-article-save save-buffer file num))
10481         (gnus-summary-remove-process-mark article)
10482         (unless not-saved
10483           (gnus-summary-set-saved-mark article))))
10484     (gnus-kill-buffer save-buffer)
10485     (gnus-summary-position-point)
10486     (gnus-set-mode-line 'summary)
10487     n))
10488
10489 (defun gnus-summary-pipe-output (&optional arg headers)
10490   "Pipe the current article to a subprocess.
10491 If N is a positive number, pipe the N next articles.
10492 If N is a negative number, pipe the N previous articles.
10493 If N is nil and any articles have been marked with the process mark,
10494 pipe those articles instead.
10495 If HEADERS (the symbolic prefix), include the headers, too."
10496   (interactive (gnus-interactive "P\ny"))
10497   (require 'gnus-art)
10498   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10499         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10500     (gnus-summary-save-article arg t))
10501   (let ((buffer (get-buffer "*Shell Command Output*")))
10502     (when (and buffer
10503                (not (zerop (buffer-size buffer))))
10504       (gnus-configure-windows 'pipe))))
10505
10506 (defun gnus-summary-save-article-mail (&optional arg)
10507   "Append the current article to an mail file.
10508 If N is a positive number, save the N next articles.
10509 If N is a negative number, save the N previous articles.
10510 If N is nil and any articles have been marked with the process mark,
10511 save those articles instead."
10512   (interactive "P")
10513   (require 'gnus-art)
10514   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10515     (gnus-summary-save-article arg)))
10516
10517 (defun gnus-summary-save-article-rmail (&optional arg)
10518   "Append the current article to an rmail file.
10519 If N is a positive number, save the N next articles.
10520 If N is a negative number, save the N previous articles.
10521 If N is nil and any articles have been marked with the process mark,
10522 save those articles instead."
10523   (interactive "P")
10524   (require 'gnus-art)
10525   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10526     (gnus-summary-save-article arg)))
10527
10528 (defun gnus-summary-save-article-file (&optional arg)
10529   "Append the current article to a file.
10530 If N is a positive number, save the N next articles.
10531 If N is a negative number, save the N previous articles.
10532 If N is nil and any articles have been marked with the process mark,
10533 save those articles instead."
10534   (interactive "P")
10535   (require 'gnus-art)
10536   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10537     (gnus-summary-save-article arg)))
10538
10539 (defun gnus-summary-write-article-file (&optional arg)
10540   "Write the current article to a file, deleting the previous file.
10541 If N is a positive number, save the N next articles.
10542 If N is a negative number, save the N previous articles.
10543 If N is nil and any articles have been marked with the process mark,
10544 save those articles instead."
10545   (interactive "P")
10546   (require 'gnus-art)
10547   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10548     (gnus-summary-save-article arg)))
10549
10550 (defun gnus-summary-save-article-body-file (&optional arg)
10551   "Append the current article body to a file.
10552 If N is a positive number, save the N next articles.
10553 If N is a negative number, save the N previous articles.
10554 If N is nil and any articles have been marked with the process mark,
10555 save those articles instead."
10556   (interactive "P")
10557   (require 'gnus-art)
10558   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10559     (gnus-summary-save-article arg)))
10560
10561 (defun gnus-summary-muttprint (&optional arg)
10562   "Print the current article using Muttprint.
10563 If N is a positive number, save the N next articles.
10564 If N is a negative number, save the N previous articles.
10565 If N is nil and any articles have been marked with the process mark,
10566 save those articles instead."
10567   (interactive "P")
10568   (require 'gnus-art)
10569   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10570     (gnus-summary-save-article arg t)))
10571
10572 (defun gnus-summary-pipe-message (program)
10573   "Pipe the current article through PROGRAM."
10574   (interactive "sProgram: ")
10575   (gnus-summary-select-article)
10576   (let ((mail-header-separator ""))
10577     (gnus-eval-in-buffer-window gnus-article-buffer
10578       (save-restriction
10579         (widen)
10580         (let ((start (window-start))
10581               buffer-read-only)
10582           (message-pipe-buffer-body program)
10583           (set-window-start (get-buffer-window (current-buffer)) start))))))
10584
10585 (defun gnus-get-split-value (methods)
10586   "Return a value based on the split METHODS."
10587   (let (split-name method result match)
10588     (when methods
10589       (save-excursion
10590         (set-buffer gnus-original-article-buffer)
10591         (save-restriction
10592           (nnheader-narrow-to-headers)
10593           (while (and methods (not split-name))
10594             (goto-char (point-min))
10595             (setq method (pop methods))
10596             (setq match (car method))
10597             (when (cond
10598                    ((stringp match)
10599                     ;; Regular expression.
10600                     (ignore-errors
10601                       (re-search-forward match nil t)))
10602                    ((gnus-functionp match)
10603                     ;; Function.
10604                     (save-restriction
10605                       (widen)
10606                       (setq result (funcall match gnus-newsgroup-name))))
10607                    ((consp match)
10608                     ;; Form.
10609                     (save-restriction
10610                       (widen)
10611                       (setq result (eval match)))))
10612               (setq split-name (cdr method))
10613               (cond ((stringp result)
10614                      (push (expand-file-name
10615                             result gnus-article-save-directory)
10616                            split-name))
10617                     ((consp result)
10618                      (setq split-name (append result split-name)))))))))
10619     (nreverse split-name)))
10620
10621 (defun gnus-valid-move-group-p (group)
10622   (and (boundp group)
10623        (symbol-name group)
10624        (symbol-value group)
10625        (gnus-get-function (gnus-find-method-for-group
10626                            (symbol-name group)) 'request-accept-article t)))
10627
10628 (defun gnus-read-move-group-name (prompt default articles prefix)
10629   "Read a group name."
10630   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10631          (minibuffer-confirm-incomplete nil) ; XEmacs
10632          (prom
10633           (format "%s %s to:"
10634                   prompt
10635                   (if (> (length articles) 1)
10636                       (format "these %d articles" (length articles))
10637                     "this article")))
10638          (to-newsgroup
10639           (cond
10640            ((null split-name)
10641             (gnus-completing-read-with-default
10642              default prom
10643              gnus-active-hashtb
10644              'gnus-valid-move-group-p
10645              nil prefix
10646              'gnus-group-history))
10647            ((= 1 (length split-name))
10648             (gnus-completing-read-with-default
10649              (car split-name) prom
10650              gnus-active-hashtb
10651              'gnus-valid-move-group-p
10652              nil nil
10653              'gnus-group-history))
10654            (t
10655             (gnus-completing-read-with-default
10656              nil prom
10657              (mapcar (lambda (el) (list el))
10658                      (nreverse split-name))
10659              nil nil nil
10660              'gnus-group-history))))
10661          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10662     (when to-newsgroup
10663       (if (or (string= to-newsgroup "")
10664               (string= to-newsgroup prefix))
10665           (setq to-newsgroup default))
10666       (unless to-newsgroup
10667         (error "No group name entered"))
10668       (or (gnus-active to-newsgroup)
10669           (gnus-activate-group to-newsgroup nil nil to-method)
10670           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10671                                      to-newsgroup))
10672               (or (and (gnus-request-create-group to-newsgroup to-method)
10673                        (gnus-activate-group
10674                         to-newsgroup nil nil to-method)
10675                        (gnus-subscribe-group to-newsgroup))
10676                   (error "Couldn't create group %s" to-newsgroup)))
10677           (error "No such group: %s" to-newsgroup)))
10678     to-newsgroup))
10679
10680 (defun gnus-summary-save-parts (type dir n &optional reverse)
10681   "Save parts matching TYPE to DIR.
10682 If REVERSE, save parts that do not match TYPE."
10683   (interactive
10684    (list (read-string "Save parts of type: "
10685                       (or (car gnus-summary-save-parts-type-history)
10686                           gnus-summary-save-parts-default-mime)
10687                       'gnus-summary-save-parts-type-history)
10688          (setq gnus-summary-save-parts-last-directory
10689                (read-file-name "Save to directory: "
10690                                gnus-summary-save-parts-last-directory
10691                                nil t))
10692          current-prefix-arg))
10693   (gnus-summary-iterate n
10694     (let ((gnus-display-mime-function nil)
10695           (gnus-inhibit-treatment t))
10696       (gnus-summary-select-article))
10697     (save-excursion
10698       (set-buffer gnus-article-buffer)
10699       (let ((handles (or gnus-article-mime-handles
10700                          (mm-dissect-buffer nil gnus-article-loose-mime)
10701                          (mm-uu-dissect))))
10702         (when handles
10703           (gnus-summary-save-parts-1 type dir handles reverse)
10704           (unless gnus-article-mime-handles ;; Don't destroy this case.
10705             (mm-destroy-parts handles)))))))
10706
10707 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10708   (if (stringp (car handle))
10709       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10710               (cdr handle))
10711     (when (if reverse
10712               (not (string-match type (mm-handle-media-type handle)))
10713             (string-match type (mm-handle-media-type handle)))
10714       (let ((file (expand-file-name
10715                    (file-name-nondirectory
10716                     (or
10717                      (mail-content-type-get
10718                       (mm-handle-disposition handle) 'filename)
10719                      (concat gnus-newsgroup-name
10720                              "." (number-to-string
10721                                   (cdr gnus-article-current)))))
10722                    dir)))
10723         (unless (file-exists-p file)
10724           (mm-save-part-to-file handle file))))))
10725
10726 ;; Summary extract commands
10727
10728 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10729   (let ((buffer-read-only nil)
10730         (article (gnus-summary-article-number))
10731         after-article b e)
10732     (unless (gnus-summary-goto-subject article)
10733       (error "No such article: %d" article))
10734     (gnus-summary-position-point)
10735     ;; If all commands are to be bunched up on one line, we collect
10736     ;; them here.
10737     (unless gnus-view-pseudos-separately
10738       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10739             files action)
10740         (while ps
10741           (setq action (cdr (assq 'action (car ps))))
10742           (setq files (list (cdr (assq 'name (car ps)))))
10743           (while (and ps (cdr ps)
10744                       (string= (or action "1")
10745                                (or (cdr (assq 'action (cadr ps))) "2")))
10746             (push (cdr (assq 'name (cadr ps))) files)
10747             (setcdr ps (cddr ps)))
10748           (when files
10749             (when (not (string-match "%s" action))
10750               (push " " files))
10751             (push " " files)
10752             (when (assq 'execute (car ps))
10753               (setcdr (assq 'execute (car ps))
10754                       (funcall (if (string-match "%s" action)
10755                                    'format 'concat)
10756                                action
10757                                (mapconcat
10758                                 (lambda (f)
10759                                   (if (equal f " ")
10760                                       f
10761                                     (mm-quote-arg f)))
10762                                 files " ")))))
10763           (setq ps (cdr ps)))))
10764     (if (and gnus-view-pseudos (not not-view))
10765         (while pslist
10766           (when (assq 'execute (car pslist))
10767             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10768                                   (eq gnus-view-pseudos 'not-confirm)))
10769           (setq pslist (cdr pslist)))
10770       (save-excursion
10771         (while pslist
10772           (setq after-article (or (cdr (assq 'article (car pslist)))
10773                                   (gnus-summary-article-number)))
10774           (gnus-summary-goto-subject after-article)
10775           (forward-line 1)
10776           (setq b (point))
10777           (insert "    " (file-name-nondirectory
10778                           (cdr (assq 'name (car pslist))))
10779                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10780           (setq e (point))
10781           (forward-line -1)             ; back to `b'
10782           (gnus-add-text-properties
10783            b (1- e) (list 'gnus-number gnus-reffed-article-number
10784                           gnus-mouse-face-prop gnus-mouse-face))
10785           (gnus-data-enter
10786            after-article gnus-reffed-article-number
10787            gnus-unread-mark b (car pslist) 0 (- e b))
10788           (setq gnus-newsgroup-unreads
10789                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10790                                          gnus-reffed-article-number))
10791           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10792           (setq pslist (cdr pslist)))))))
10793
10794 (defun gnus-pseudos< (p1 p2)
10795   (let ((c1 (cdr (assq 'action p1)))
10796         (c2 (cdr (assq 'action p2))))
10797     (and c1 c2 (string< c1 c2))))
10798
10799 (defun gnus-request-pseudo-article (props)
10800   (cond ((assq 'execute props)
10801          (gnus-execute-command (cdr (assq 'execute props)))))
10802   (let ((gnus-current-article (gnus-summary-article-number)))
10803     (gnus-run-hooks 'gnus-mark-article-hook)))
10804
10805 (defun gnus-execute-command (command &optional automatic)
10806   (save-excursion
10807     (gnus-article-setup-buffer)
10808     (set-buffer gnus-article-buffer)
10809     (setq buffer-read-only nil)
10810     (let ((command (if automatic command
10811                      (read-string "Command: " (cons command 0)))))
10812       (erase-buffer)
10813       (insert "$ " command "\n\n")
10814       (if gnus-view-pseudo-asynchronously
10815           (start-process "gnus-execute" (current-buffer) shell-file-name
10816                          shell-command-switch command)
10817         (call-process shell-file-name nil t nil
10818                       shell-command-switch command)))))
10819
10820 ;; Summary kill commands.
10821
10822 (defun gnus-summary-edit-global-kill (article)
10823   "Edit the \"global\" kill file."
10824   (interactive (list (gnus-summary-article-number)))
10825   (gnus-group-edit-global-kill article))
10826
10827 (defun gnus-summary-edit-local-kill ()
10828   "Edit a local kill file applied to the current newsgroup."
10829   (interactive)
10830   (setq gnus-current-headers (gnus-summary-article-header))
10831   (gnus-group-edit-local-kill
10832    (gnus-summary-article-number) gnus-newsgroup-name))
10833
10834 ;;; Header reading.
10835
10836 (defun gnus-read-header (id &optional header)
10837   "Read the headers of article ID and enter them into the Gnus system."
10838   (let ((group gnus-newsgroup-name)
10839         (gnus-override-method
10840          (or
10841           gnus-override-method
10842           (and (gnus-news-group-p gnus-newsgroup-name)
10843                (car (gnus-refer-article-methods)))))
10844         where)
10845     ;; First we check to see whether the header in question is already
10846     ;; fetched.
10847     (if (stringp id)
10848         ;; This is a Message-ID.
10849         (setq header (or header (gnus-id-to-header id)))
10850       ;; This is an article number.
10851       (setq header (or header (gnus-summary-article-header id))))
10852     (if (and header
10853              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10854         ;; We have found the header.
10855         header
10856       ;; If this is a sparse article, we have to nix out its
10857       ;; previous entry in the thread hashtb.
10858       (when (and header
10859                  (gnus-summary-article-sparse-p (mail-header-number header)))
10860         (let* ((parent (gnus-parent-id (mail-header-references header)))
10861                (thread (and parent (gnus-id-to-thread parent))))
10862           (when thread
10863             (delq (assq header thread) thread))))
10864       ;; We have to really fetch the header to this article.
10865       (save-excursion
10866         (set-buffer nntp-server-buffer)
10867         (when (setq where (gnus-request-head id group))
10868           (nnheader-fold-continuation-lines)
10869           (goto-char (point-max))
10870           (insert ".\n")
10871           (goto-char (point-min))
10872           (insert "211 ")
10873           (princ (cond
10874                   ((numberp id) id)
10875                   ((cdr where) (cdr where))
10876                   (header (mail-header-number header))
10877                   (t gnus-reffed-article-number))
10878                  (current-buffer))
10879           (insert " Article retrieved.\n"))
10880         (if (or (not where)
10881                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10882             ()                          ; Malformed head.
10883           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10884             (when (and (stringp id)
10885                        (not (string= (gnus-group-real-name group)
10886                                      (car where))))
10887               ;; If we fetched by Message-ID and the article came
10888               ;; from a different group, we fudge some bogus article
10889               ;; numbers for this article.
10890               (mail-header-set-number header gnus-reffed-article-number))
10891             (save-excursion
10892               (set-buffer gnus-summary-buffer)
10893               (decf gnus-reffed-article-number)
10894               (gnus-remove-header (mail-header-number header))
10895               (push header gnus-newsgroup-headers)
10896               (setq gnus-current-headers header)
10897               (push (mail-header-number header) gnus-newsgroup-limit)))
10898           header)))))
10899
10900 (defun gnus-remove-header (number)
10901   "Remove header NUMBER from `gnus-newsgroup-headers'."
10902   (if (and gnus-newsgroup-headers
10903            (= number (mail-header-number (car gnus-newsgroup-headers))))
10904       (pop gnus-newsgroup-headers)
10905     (let ((headers gnus-newsgroup-headers))
10906       (while (and (cdr headers)
10907                   (not (= number (mail-header-number (cadr headers)))))
10908         (pop headers))
10909       (when (cdr headers)
10910         (setcdr headers (cddr headers))))))
10911
10912 ;;;
10913 ;;; summary highlights
10914 ;;;
10915
10916 (defun gnus-highlight-selected-summary ()
10917   "Highlight selected article in summary buffer."
10918   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10919   (when gnus-summary-selected-face
10920     (save-excursion
10921       (let* ((beg (progn (beginning-of-line) (point)))
10922              (end (progn (end-of-line) (point)))
10923              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10924              (from (if (get-text-property beg gnus-mouse-face-prop)
10925                        beg
10926                      (or (next-single-property-change
10927                           beg gnus-mouse-face-prop nil end)
10928                          beg)))
10929              (to
10930               (if (= from end)
10931                   (- from 2)
10932                 (or (next-single-property-change
10933                      from gnus-mouse-face-prop nil end)
10934                     end))))
10935         ;; If no mouse-face prop on line we will have to = from = end,
10936         ;; so we highlight the entire line instead.
10937         (when (= (+ to 2) from)
10938           (setq from beg)
10939           (setq to end))
10940         (if gnus-newsgroup-selected-overlay
10941             ;; Move old overlay.
10942             (gnus-move-overlay
10943              gnus-newsgroup-selected-overlay from to (current-buffer))
10944           ;; Create new overlay.
10945           (gnus-overlay-put
10946            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10947            'face gnus-summary-selected-face))))))
10948
10949 (defvar gnus-summary-highlight-line-cached nil)
10950 (defvar gnus-summary-highlight-line-trigger nil)
10951
10952 (defun gnus-summary-highlight-line-0 ()
10953   (if (and (eq gnus-summary-highlight-line-trigger 
10954                gnus-summary-highlight)
10955            gnus-summary-highlight-line-cached)
10956       gnus-summary-highlight-line-cached
10957     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10958           gnus-summary-highlight-line-cached
10959           (let* ((cond (list 'cond))
10960                  (c cond)
10961                  (list gnus-summary-highlight))
10962             (while list
10963               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10964               (setq c (cdr c)
10965                     list (cdr list)))
10966             (gnus-byte-compile (list 'lambda nil cond))))))
10967
10968 (defun gnus-summary-highlight-line ()
10969   "Highlight current line according to `gnus-summary-highlight'."
10970   (let* ((beg (gnus-point-at-bol))
10971          (article (or (gnus-summary-article-number) gnus-current-article))
10972          (score (or (cdr (assq article
10973                                gnus-newsgroup-scored))
10974                     gnus-summary-default-score 0))
10975          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10976          (inhibit-read-only t)
10977          (default gnus-summary-default-score)
10978          (default-high gnus-summary-default-high-score)
10979          (default-low gnus-summary-default-low-score)
10980          (uncached (memq article gnus-newsgroup-undownloaded))
10981          (downloaded (not uncached)))
10982     (let ((face (funcall (gnus-summary-highlight-line-0))))
10983       (unless (eq face (get-text-property beg 'face))
10984         (gnus-put-text-property-excluding-characters-with-faces
10985          beg (gnus-point-at-eol) 'face
10986          (setq face (if (boundp face) (symbol-value face) face)))
10987         (when gnus-summary-highlight-line-function
10988           (funcall gnus-summary-highlight-line-function article face))))))
10989
10990 (defun gnus-update-read-articles (group unread &optional compute)
10991   "Update the list of read articles in GROUP.
10992 UNREAD is a sorted list."
10993   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10994          (entry (gnus-gethash group gnus-newsrc-hashtb))
10995          (info (nth 2 entry))
10996          (prev 1)
10997          read)
10998     (if (or (not info) (not active))
10999         ;; There is no info on this group if it was, in fact,
11000         ;; killed.  Gnus stores no information on killed groups, so
11001         ;; there's nothing to be done.
11002         ;; One could store the information somewhere temporarily,
11003         ;; perhaps...  Hmmm...
11004         ()
11005       ;; Remove any negative articles numbers.
11006       (while (and unread (< (car unread) 0))
11007         (setq unread (cdr unread)))
11008       ;; Remove any expired article numbers
11009       (while (and unread (< (car unread) (car active)))
11010         (setq unread (cdr unread)))
11011       ;; Compute the ranges of read articles by looking at the list of
11012       ;; unread articles.
11013       (while unread
11014         (when (/= (car unread) prev)
11015           (push (if (= prev (1- (car unread))) prev
11016                   (cons prev (1- (car unread))))
11017                 read))
11018         (setq prev (1+ (car unread)))
11019         (setq unread (cdr unread)))
11020       (when (<= prev (cdr active))
11021         (push (cons prev (cdr active)) read))
11022       (setq read (if (> (length read) 1) (nreverse read) read))
11023       (if compute
11024           read
11025         (save-excursion
11026           (let (setmarkundo)
11027             ;; Propagate the read marks to the backend.
11028             (when (gnus-check-backend-function 'request-set-mark group)
11029               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11030                     (add (gnus-remove-from-range read (gnus-info-read info))))
11031                 (when (or add del)
11032                   (unless (gnus-check-group group)
11033                     (error "Can't open server for %s" group))
11034                   (gnus-request-set-mark
11035                    group (delq nil (list (if add (list add 'add '(read)))
11036                                          (if del (list del 'del '(read))))))
11037                   (setq setmarkundo
11038                         `(gnus-request-set-mark
11039                           ,group
11040                           ',(delq nil (list
11041                                        (if del (list del 'add '(read)))
11042                                        (if add (list add 'del '(read))))))))))
11043             (set-buffer gnus-group-buffer)
11044             (gnus-undo-register
11045               `(progn
11046                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11047                  (gnus-info-set-read ',info ',(gnus-info-read info))
11048                  (gnus-get-unread-articles-in-group ',info
11049                                                     (gnus-active ,group))
11050                  (gnus-group-update-group ,group t)
11051                  ,setmarkundo))))
11052         ;; Enter this list into the group info.
11053         (gnus-info-set-read info read)
11054         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11055         (gnus-get-unread-articles-in-group info (gnus-active group))
11056         t))))
11057
11058 (defun gnus-offer-save-summaries ()
11059   "Offer to save all active summary buffers."
11060   (let (buffers)
11061     ;; Go through all buffers and find all summaries.
11062     (dolist (buffer (buffer-list))
11063       (when (and (setq buffer (buffer-name buffer))
11064                  (string-match "Summary" buffer)
11065                  (save-excursion
11066                    (set-buffer buffer)
11067                    ;; We check that this is, indeed, a summary buffer.
11068                    (and (eq major-mode 'gnus-summary-mode)
11069                         ;; Also make sure this isn't bogus.
11070                         gnus-newsgroup-prepared
11071                         ;; Also make sure that this isn't a
11072                         ;; dead summary buffer.
11073                         (not gnus-dead-summary-mode))))
11074         (push buffer buffers)))
11075     ;; Go through all these summary buffers and offer to save them.
11076     (when buffers
11077       (save-excursion
11078         (map-y-or-n-p
11079          "Update summary buffer %s? "
11080          (lambda (buf)
11081            (switch-to-buffer buf)
11082            (gnus-summary-exit))
11083          buffers)))))
11084
11085 (defun gnus-summary-setup-default-charset ()
11086   "Setup newsgroup default charset."
11087   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11088       (setq gnus-newsgroup-charset nil)
11089     (let* ((ignored-charsets
11090             (or gnus-newsgroup-ephemeral-ignored-charsets
11091                 (append
11092                  (and gnus-newsgroup-name
11093                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11094                  gnus-newsgroup-ignored-charsets))))
11095       (setq gnus-newsgroup-charset
11096             (or gnus-newsgroup-ephemeral-charset
11097                 (and gnus-newsgroup-name
11098                      (gnus-parameter-charset gnus-newsgroup-name))
11099                 gnus-default-charset))
11100       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11101            ignored-charsets))))
11102
11103 ;;;
11104 ;;; Mime Commands
11105 ;;;
11106
11107 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11108   "Display the current article buffer fully MIME-buttonized.
11109 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11110 treated as multipart/mixed."
11111   (interactive "P")
11112   (require 'gnus-art)
11113   (let ((gnus-unbuttonized-mime-types nil)
11114         (gnus-mime-display-multipart-as-mixed show-all-parts))
11115     (gnus-summary-show-article)))
11116
11117 (defun gnus-summary-repair-multipart (article)
11118   "Add a Content-Type header to a multipart article without one."
11119   (interactive (list (gnus-summary-article-number)))
11120   (gnus-with-article article
11121     (message-narrow-to-head)
11122     (message-remove-header "Mime-Version")
11123     (goto-char (point-max))
11124     (insert "Mime-Version: 1.0\n")
11125     (widen)
11126     (when (search-forward "\n--" nil t)
11127       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11128         (message-narrow-to-head)
11129         (message-remove-header "Content-Type")
11130         (goto-char (point-max))
11131         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11132                         separator))
11133         (widen))))
11134   (let (gnus-mark-article-hook)
11135     (gnus-summary-select-article t t nil article)))
11136
11137 (defun gnus-summary-toggle-display-buttonized ()
11138   "Toggle the buttonizing of the article buffer."
11139   (interactive)
11140   (require 'gnus-art)
11141   (if (setq gnus-inhibit-mime-unbuttonizing
11142             (not gnus-inhibit-mime-unbuttonizing))
11143       (let ((gnus-unbuttonized-mime-types nil))
11144         (gnus-summary-show-article))
11145     (gnus-summary-show-article)))
11146
11147 ;;;
11148 ;;; Generic summary marking commands
11149 ;;;
11150
11151 (defvar gnus-summary-marking-alist
11152   '((read gnus-del-mark "d")
11153     (unread gnus-unread-mark "u")
11154     (ticked gnus-ticked-mark "!")
11155     (dormant gnus-dormant-mark "?")
11156     (expirable gnus-expirable-mark "e"))
11157   "An alist of names/marks/keystrokes.")
11158
11159 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11160 (defvar gnus-summary-mark-map)
11161
11162 (defun gnus-summary-make-all-marking-commands ()
11163   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11164   (dolist (elem gnus-summary-marking-alist)
11165     (apply 'gnus-summary-make-marking-command elem)))
11166
11167 (defun gnus-summary-make-marking-command (name mark keystroke)
11168   (let ((map (make-sparse-keymap)))
11169     (define-key gnus-summary-generic-mark-map keystroke map)
11170     (dolist (lway `((next "next" next nil "n")
11171                     (next-unread "next unread" next t "N")
11172                     (prev "previous" prev nil "p")
11173                     (prev-unread "previous unread" prev t "P")
11174                     (nomove "" nil nil ,keystroke)))
11175       (let ((func (gnus-summary-make-marking-command-1
11176                    mark (car lway) lway name)))
11177         (setq func (eval func))
11178         (define-key map (nth 4 lway) func)))))
11179
11180 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11181   `(defun ,(intern
11182             (format "gnus-summary-put-mark-as-%s%s"
11183                     name (if (eq way 'nomove)
11184                              ""
11185                            (concat "-" (symbol-name way)))))
11186      (n)
11187      ,(format
11188        "Mark the current article as %s%s.
11189 If N, the prefix, then repeat N times.
11190 If N is negative, move in reverse order.
11191 The difference between N and the actual number of articles marked is
11192 returned."
11193        name (cadr lway))
11194      (interactive "p")
11195      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11196
11197 (defun gnus-summary-generic-mark (n mark move unread)
11198   "Mark N articles with MARK."
11199   (unless (eq major-mode 'gnus-summary-mode)
11200     (error "This command can only be used in the summary buffer"))
11201   (gnus-summary-show-thread)
11202   (let ((nummove
11203          (cond
11204           ((eq move 'next) 1)
11205           ((eq move 'prev) -1)
11206           (t 0))))
11207     (if (zerop nummove)
11208         (setq n 1)
11209       (when (< n 0)
11210         (setq n (abs n)
11211               nummove (* -1 nummove))))
11212     (while (and (> n 0)
11213                 (gnus-summary-mark-article nil mark)
11214                 (zerop (gnus-summary-next-subject nummove unread t)))
11215       (setq n (1- n)))
11216     (when (/= 0 n)
11217       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11218     (gnus-summary-recenter)
11219     (gnus-summary-position-point)
11220     (gnus-set-mode-line 'summary)
11221     n))
11222
11223 (defun gnus-summary-insert-articles (articles)
11224   (when (setq articles
11225               (gnus-sorted-difference articles
11226                                       (mapcar (lambda (h)
11227                                                 (mail-header-number h))
11228                                               gnus-newsgroup-headers)))
11229     (setq gnus-newsgroup-headers
11230           (merge 'list
11231                  gnus-newsgroup-headers
11232                  (gnus-fetch-headers articles)
11233                  'gnus-article-sort-by-number))
11234     ;; Suppress duplicates?
11235     (when gnus-suppress-duplicates
11236       (gnus-dup-suppress-articles))
11237
11238     ;; We might want to build some more threads first.
11239     (when (and gnus-fetch-old-headers
11240                (eq gnus-headers-retrieved-by 'nov))
11241       (if (eq gnus-fetch-old-headers 'invisible)
11242           (gnus-build-all-threads)
11243         (gnus-build-old-threads)))
11244     ;; Let the Gnus agent mark articles as read.
11245     (when gnus-agent
11246       (gnus-agent-get-undownloaded-list))
11247     ;; Remove list identifiers from subject
11248     (when gnus-list-identifiers
11249       (gnus-summary-remove-list-identifiers))
11250     ;; First and last article in this newsgroup.
11251     (when gnus-newsgroup-headers
11252       (setq gnus-newsgroup-begin
11253             (mail-header-number (car gnus-newsgroup-headers))
11254             gnus-newsgroup-end
11255             (mail-header-number
11256              (gnus-last-element gnus-newsgroup-headers))))
11257     (when gnus-use-scoring
11258       (gnus-possibly-score-headers))))
11259
11260 (defun gnus-summary-insert-old-articles (&optional all)
11261   "Insert all old articles in this group.
11262 If ALL is non-nil, already read articles become readable.
11263 If ALL is a number, fetch this number of articles."
11264   (interactive "P")
11265   (prog1
11266       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11267             older len)
11268         (setq older
11269               ;; Some nntp servers lie about their active range.  When
11270               ;; this happens, the active range can be in the millions.
11271               ;; Use a compressed range to avoid creating a huge list.
11272               (gnus-range-difference (list gnus-newsgroup-active) old))
11273         (setq len (gnus-range-length older))
11274         (cond
11275          ((null older) nil)
11276          ((numberp all)
11277           (if (< all len)
11278               (let ((older-range (nreverse older)))
11279                 (setq older nil)
11280
11281                 (while (> all 0)
11282                   (let* ((r (pop older-range))
11283                          (min (if (numberp r) r (car r)))
11284                          (max (if (numberp r) r (cdr r))))
11285                     (while (and (<= min max)
11286                                 (> all 0))
11287                       (push max older)
11288                       (setq all (1- all)
11289                             max (1- max))))))
11290             (setq older (gnus-uncompress-range older))))
11291          (all
11292           (setq older (gnus-uncompress-range older)))
11293          (t
11294           (when (and (numberp gnus-large-newsgroup)
11295                    (> len gnus-large-newsgroup))
11296               (let* ((cursor-in-echo-area nil)
11297                      (initial (gnus-parameter-large-newsgroup-initial
11298                                gnus-newsgroup-name))
11299                      (input
11300                       (read-string
11301                        (format
11302                         "How many articles from %s (%s %d): "
11303                         (gnus-limit-string
11304                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11305                         (if initial "max" "default")
11306                         len)
11307                        (if initial
11308                            (cons (number-to-string initial)
11309                                  0)))))
11310                 (unless (string-match "^[ \t]*$" input)
11311                   (setq all (string-to-number input))
11312                   (if (< all len)
11313                       (let ((older-range (nreverse older)))
11314                         (setq older nil)
11315
11316                         (while (> all 0)
11317                           (let* ((r (pop older-range))
11318                                  (min (if (numberp r) r (car r)))
11319                                  (max (if (numberp r) r (cdr r))))
11320                             (while (and (<= min max)
11321                                         (> all 0))
11322                               (push max older)
11323                               (setq all (1- all)
11324                                     max (1- max))))))))))
11325           (setq older (gnus-uncompress-range older))))
11326         (if (not older)
11327             (message "No old news.")
11328           (gnus-summary-insert-articles older)
11329           (gnus-summary-limit (gnus-sorted-nunion old older))))
11330     (gnus-summary-position-point)))
11331
11332 (defun gnus-summary-insert-new-articles ()
11333   "Insert all new articles in this group."
11334   (interactive)
11335   (prog1
11336       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11337             (old-active gnus-newsgroup-active)
11338             (nnmail-fetched-sources (list t))
11339             i new)
11340         (setq gnus-newsgroup-active
11341               (gnus-activate-group gnus-newsgroup-name 'scan))
11342         (setq i (cdr gnus-newsgroup-active))
11343         (while (> i (cdr old-active))
11344           (push i new)
11345           (decf i))
11346         (if (not new)
11347             (message "No gnus is bad news.")
11348           (gnus-summary-insert-articles new)
11349           (setq gnus-newsgroup-unreads
11350                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11351           (gnus-summary-limit (gnus-sorted-nunion old new))))
11352     (gnus-summary-position-point)))
11353
11354 (gnus-summary-make-all-marking-commands)
11355
11356 (gnus-ems-redefine)
11357
11358 (provide 'gnus-sum)
11359
11360 (run-hooks 'gnus-sum-load-hook)
11361
11362 ;;; gnus-sum.el ends here