(gnus-read-mark-p): added the spam-mark as a
[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-spam-mark)
2859            (= mark gnus-dormant-mark)
2860            (= mark gnus-expirable-mark))))
2861
2862 (defmacro gnus-article-mark (number)
2863   "Return the MARK of article NUMBER.
2864 This macro should only be used when computing the mark the \"first\"
2865 time; i.e., when generating the summary lines.  After that,
2866 `gnus-summary-article-mark' should be used to examine the
2867 marks of articles."
2868   `(cond
2869     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2870     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2871     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2872     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2873     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2874     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2875     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2876     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2877            gnus-ancient-mark))))
2878
2879 ;; Saving hidden threads.
2880
2881 (defmacro gnus-save-hidden-threads (&rest forms)
2882   "Save hidden threads, eval FORMS, and restore the hidden threads."
2883   (let ((config (make-symbol "config")))
2884     `(let ((,config (gnus-hidden-threads-configuration)))
2885        (unwind-protect
2886            (save-excursion
2887              ,@forms)
2888          (gnus-restore-hidden-threads-configuration ,config)))))
2889 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2890 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2891
2892 (defun gnus-data-compute-positions ()
2893   "Compute the positions of all articles."
2894   (setq gnus-newsgroup-data-reverse nil)
2895   (let ((data gnus-newsgroup-data))
2896     (save-excursion
2897       (gnus-save-hidden-threads
2898         (gnus-summary-show-all-threads)
2899         (goto-char (point-min))
2900         (while data
2901           (while (get-text-property (point) 'gnus-intangible)
2902             (forward-line 1))
2903           (gnus-data-set-pos (car data) (+ (point) 3))
2904           (setq data (cdr data))
2905           (forward-line 1))))))
2906
2907 (defun gnus-hidden-threads-configuration ()
2908   "Return the current hidden threads configuration."
2909   (save-excursion
2910     (let (config)
2911       (goto-char (point-min))
2912       (while (search-forward "\r" nil t)
2913         (push (1- (point)) config))
2914       config)))
2915
2916 (defun gnus-restore-hidden-threads-configuration (config)
2917   "Restore hidden threads configuration from CONFIG."
2918   (save-excursion
2919     (let (point buffer-read-only)
2920       (while (setq point (pop config))
2921         (when (and (< point (point-max))
2922                    (goto-char point)
2923                    (eq (char-after) ?\n))
2924           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2925
2926 ;; Various summary mode internalish functions.
2927
2928 (defun gnus-mouse-pick-article (e)
2929   (interactive "e")
2930   (mouse-set-point e)
2931   (gnus-summary-next-page nil t))
2932
2933 (defun gnus-summary-set-display-table ()
2934   "Change the display table.
2935 Odd characters have a tendency to mess
2936 up nicely formatted displays - we make all possible glyphs
2937 display only a single character."
2938
2939   ;; We start from the standard display table, if any.
2940   (let ((table (or (copy-sequence standard-display-table)
2941                    (make-display-table)))
2942         (i 32))
2943     ;; Nix out all the control chars...
2944     (while (>= (setq i (1- i)) 0)
2945       (aset table i [??]))
2946    ;; ... but not newline and cr, of course.  (cr is necessary for the
2947     ;; selective display).
2948     (aset table ?\n nil)
2949     (aset table ?\r nil)
2950     ;; We keep TAB as well.
2951     (aset table ?\t nil)
2952     ;; We nix out any glyphs over 126 that are not set already.
2953     (let ((i 256))
2954       (while (>= (setq i (1- i)) 127)
2955         ;; Only modify if the entry is nil.
2956         (unless (aref table i)
2957           (aset table i [??]))))
2958     (setq buffer-display-table table)))
2959
2960 (defun gnus-summary-set-article-display-arrow (pos)
2961   "Update the overlay arrow to point to line at position POS."
2962   (when (and gnus-summary-display-arrow
2963              (boundp 'overlay-arrow-position)
2964              (boundp 'overlay-arrow-string))
2965     (save-excursion
2966       (goto-char pos)
2967       (beginning-of-line)
2968       (unless overlay-arrow-position
2969         (setq overlay-arrow-position (make-marker)))
2970       (setq overlay-arrow-string "=>"
2971             overlay-arrow-position (set-marker overlay-arrow-position
2972                                                (point)
2973                                                (current-buffer))))))
2974
2975 (defun gnus-summary-buffer-name (group)
2976   "Return the summary buffer name of GROUP."
2977   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2978
2979 (defun gnus-summary-setup-buffer (group)
2980   "Initialize summary buffer."
2981   (let ((buffer (gnus-summary-buffer-name group))
2982         (dead-name (concat "*Dead Summary "
2983                            (gnus-group-decoded-name group) "*")))
2984     ;; If a dead summary buffer exists, we kill it.
2985     (when (gnus-buffer-live-p dead-name)
2986       (gnus-kill-buffer dead-name))
2987     (if (get-buffer buffer)
2988         (progn
2989           (set-buffer buffer)
2990           (setq gnus-summary-buffer (current-buffer))
2991           (not gnus-newsgroup-prepared))
2992       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2993       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2994       (gnus-summary-mode group)
2995       (when gnus-carpal
2996         (gnus-carpal-setup-buffer 'summary))
2997       (unless gnus-single-article-buffer
2998         (make-local-variable 'gnus-article-buffer)
2999         (make-local-variable 'gnus-article-current)
3000         (make-local-variable 'gnus-original-article-buffer))
3001       (setq gnus-newsgroup-name group)
3002       ;; Set any local variables in the group parameters.
3003       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3004       t)))
3005
3006 (defun gnus-set-global-variables ()
3007   "Set the global equivalents of the buffer-local variables.
3008 They are set to the latest values they had.  These reflect the summary
3009 buffer that was in action when the last article was fetched."
3010   (when (eq major-mode 'gnus-summary-mode)
3011     (setq gnus-summary-buffer (current-buffer))
3012     (let ((name gnus-newsgroup-name)
3013           (marked gnus-newsgroup-marked)
3014           (spam gnus-newsgroup-spam-marked)
3015           (unread gnus-newsgroup-unreads)
3016           (headers gnus-current-headers)
3017           (data gnus-newsgroup-data)
3018           (summary gnus-summary-buffer)
3019           (article-buffer gnus-article-buffer)
3020           (original gnus-original-article-buffer)
3021           (gac gnus-article-current)
3022           (reffed gnus-reffed-article-number)
3023           (score-file gnus-current-score-file)
3024           (default-charset gnus-newsgroup-charset)
3025           vlist)
3026       (let ((locals gnus-newsgroup-variables))
3027         (while locals
3028           (if (consp (car locals))
3029               (push (eval (caar locals)) vlist)
3030             (push (eval (car locals)) vlist))
3031           (setq locals (cdr locals)))
3032         (setq vlist (nreverse vlist)))
3033       (save-excursion
3034         (set-buffer gnus-group-buffer)
3035         (setq gnus-newsgroup-name name
3036               gnus-newsgroup-marked marked
3037               gnus-newsgroup-spam-marked spam
3038               gnus-newsgroup-unreads unread
3039               gnus-current-headers headers
3040               gnus-newsgroup-data data
3041               gnus-article-current gac
3042               gnus-summary-buffer summary
3043               gnus-article-buffer article-buffer
3044               gnus-original-article-buffer original
3045               gnus-reffed-article-number reffed
3046               gnus-current-score-file score-file
3047               gnus-newsgroup-charset default-charset)
3048         (let ((locals gnus-newsgroup-variables))
3049           (while locals
3050             (if (consp (car locals))
3051                 (set (caar locals) (pop vlist))
3052               (set (car locals) (pop vlist)))
3053             (setq locals (cdr locals))))
3054         ;; The article buffer also has local variables.
3055         (when (gnus-buffer-live-p gnus-article-buffer)
3056           (set-buffer gnus-article-buffer)
3057           (setq gnus-summary-buffer summary))))))
3058
3059 (defun gnus-summary-article-unread-p (article)
3060   "Say whether ARTICLE is unread or not."
3061   (memq article gnus-newsgroup-unreads))
3062
3063 (defun gnus-summary-first-article-p (&optional article)
3064   "Return whether ARTICLE is the first article in the buffer."
3065   (if (not (setq article (or article (gnus-summary-article-number))))
3066       nil
3067     (eq article (caar gnus-newsgroup-data))))
3068
3069 (defun gnus-summary-last-article-p (&optional article)
3070   "Return whether ARTICLE is the last article in the buffer."
3071   (if (not (setq article (or article (gnus-summary-article-number))))
3072       ;; All non-existent numbers are the last article.  :-)
3073       t
3074     (not (cdr (gnus-data-find-list article)))))
3075
3076 (defun gnus-make-thread-indent-array ()
3077   (let ((n 200))
3078     (unless (and gnus-thread-indent-array
3079                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3080       (setq gnus-thread-indent-array (make-vector 201 "")
3081             gnus-thread-indent-array-level gnus-thread-indent-level)
3082       (while (>= n 0)
3083         (aset gnus-thread-indent-array n
3084               (make-string (* n gnus-thread-indent-level) ? ))
3085         (setq n (1- n))))))
3086
3087 (defun gnus-update-summary-mark-positions ()
3088   "Compute where the summary marks are to go."
3089   (save-excursion
3090     (when (gnus-buffer-exists-p gnus-summary-buffer)
3091       (set-buffer gnus-summary-buffer))
3092     (let ((gnus-replied-mark 129)
3093           (gnus-score-below-mark 130)
3094           (gnus-score-over-mark 130)
3095           (gnus-downloaded-mark 131)
3096           (spec gnus-summary-line-format-spec)
3097           gnus-visual pos)
3098       (save-excursion
3099         (gnus-set-work-buffer)
3100         (let ((gnus-summary-line-format-spec spec)
3101               (gnus-newsgroup-downloadable '(0)))
3102           (gnus-summary-insert-line
3103            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3104            0 nil nil 128 t nil "" nil 1)
3105           (goto-char (point-min))
3106           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3107                                              (- (point) (point-min) 1)))))
3108           (goto-char (point-min))
3109           (push (cons 'replied (and (search-forward "\201" nil t)
3110                                     (- (point) (point-min) 1)))
3111                 pos)
3112           (goto-char (point-min))
3113           (push (cons 'score (and (search-forward "\202" nil t)
3114                                   (- (point) (point-min) 1)))
3115                 pos)
3116           (goto-char (point-min))
3117           (push (cons 'download
3118                       (and (search-forward "\203" nil t)
3119                            (- (point) (point-min) 1)))
3120                 pos)))
3121       (setq gnus-summary-mark-positions pos))))
3122
3123 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3124   "Insert a dummy root in the summary buffer."
3125   (beginning-of-line)
3126   (gnus-add-text-properties
3127    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3128    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3129
3130 (defun gnus-summary-extract-address-component (from)
3131   (or (car (funcall gnus-extract-address-components from))
3132       from))
3133
3134 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3135   (let ((mail-parse-charset gnus-newsgroup-charset)
3136         ; Is it really necessary to do this next part for each summary line?
3137         ; Luckily, doesn't seem to slow things down much.
3138         (mail-parse-ignored-charsets
3139          (save-excursion (set-buffer gnus-summary-buffer)
3140                          gnus-newsgroup-ignored-charsets)))
3141     (or
3142      (and gnus-ignored-from-addresses
3143           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3144           (let ((extra-headers (mail-header-extra header))
3145                 to
3146                 newsgroups)
3147             (cond
3148              ((setq to (cdr (assq 'To extra-headers)))
3149               (concat "-> "
3150                       (inline
3151                         (gnus-summary-extract-address-component
3152                          (funcall gnus-decode-encoded-word-function to)))))
3153              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3154               (concat "=> " newsgroups)))))
3155      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3156
3157 (defun gnus-summary-insert-line (gnus-tmp-header
3158                                  gnus-tmp-level gnus-tmp-current
3159                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3160                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3161                                  &optional gnus-tmp-dummy gnus-tmp-score
3162                                  gnus-tmp-process)
3163   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3164          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3165          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3166          (gnus-tmp-score-char
3167           (if (or (null gnus-summary-default-score)
3168                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3169                       gnus-summary-zcore-fuzz))
3170               ?                         ;Whitespace
3171             (if (< gnus-tmp-score gnus-summary-default-score)
3172                 gnus-score-below-mark gnus-score-over-mark)))
3173          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3174          (gnus-tmp-replied
3175           (cond (gnus-tmp-process gnus-process-mark)
3176                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3177                  gnus-cached-mark)
3178                 (gnus-tmp-replied gnus-replied-mark)
3179                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3180                  gnus-forwarded-mark)
3181                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3182                  gnus-saved-mark)
3183                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3184                  gnus-recent-mark)
3185                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3186                  gnus-unseen-mark)
3187                 (t gnus-no-mark)))
3188          (gnus-tmp-downloaded
3189           (cond (undownloaded 
3190                  gnus-undownloaded-mark)
3191                 (gnus-newsgroup-agentized
3192                  gnus-downloaded-mark)
3193                 (t
3194                  gnus-no-mark)))
3195          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3196          (gnus-tmp-name
3197           (cond
3198            ((string-match "<[^>]+> *$" gnus-tmp-from)
3199             (let ((beg (match-beginning 0)))
3200               (or (and (string-match "^\".+\"" gnus-tmp-from)
3201                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3202                   (substring gnus-tmp-from 0 beg))))
3203            ((string-match "(.+)" gnus-tmp-from)
3204             (substring gnus-tmp-from
3205                        (1+ (match-beginning 0)) (1- (match-end 0))))
3206            (t gnus-tmp-from)))
3207          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3208          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3209          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3210          (buffer-read-only nil))
3211     (when (string= gnus-tmp-name "")
3212       (setq gnus-tmp-name gnus-tmp-from))
3213     (unless (numberp gnus-tmp-lines)
3214       (setq gnus-tmp-lines -1))
3215     (if (= gnus-tmp-lines -1)
3216         (setq gnus-tmp-lines "?")
3217       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3218     (gnus-put-text-property
3219      (point)
3220      (progn (eval gnus-summary-line-format-spec) (point))
3221      'gnus-number gnus-tmp-number)
3222     (when (gnus-visual-p 'summary-highlight 'highlight)
3223       (forward-line -1)
3224       (gnus-run-hooks 'gnus-summary-update-hook)
3225       (forward-line 1))))
3226
3227 (defun gnus-summary-update-line (&optional dont-update)
3228   "Update summary line after change."
3229   (when (and gnus-summary-default-score
3230              (not gnus-summary-inhibit-highlight))
3231     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3232            (article (gnus-summary-article-number))
3233            (score (gnus-summary-article-score article)))
3234       (unless dont-update
3235         (if (and gnus-summary-mark-below
3236                  (< (gnus-summary-article-score)
3237                     gnus-summary-mark-below))
3238             ;; This article has a low score, so we mark it as read.
3239             (when (memq article gnus-newsgroup-unreads)
3240               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3241           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3242             ;; This article was previously marked as read on account
3243             ;; of a low score, but now it has risen, so we mark it as
3244             ;; unread.
3245             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3246         (gnus-summary-update-mark
3247          (if (or (null gnus-summary-default-score)
3248                  (<= (abs (- score gnus-summary-default-score))
3249                      gnus-summary-zcore-fuzz))
3250              ?                          ;Whitespace
3251            (if (< score gnus-summary-default-score)
3252                gnus-score-below-mark gnus-score-over-mark))
3253          'score))
3254       ;; Do visual highlighting.
3255       (when (gnus-visual-p 'summary-highlight 'highlight)
3256         (gnus-run-hooks 'gnus-summary-update-hook)))))
3257
3258 (defvar gnus-tmp-new-adopts nil)
3259
3260 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3261   "Return the number of articles in THREAD.
3262 This may be 0 in some cases -- if none of the articles in
3263 the thread are to be displayed."
3264   (let* ((number
3265          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3266           (cond
3267            ((not (listp thread))
3268             1)
3269            ((and (consp thread) (cdr thread))
3270             (apply
3271              '+ 1 (mapcar
3272                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3273            ((null thread)
3274             1)
3275            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3276             1)
3277            (t 0))))
3278     (when (and level (zerop level) gnus-tmp-new-adopts)
3279       (incf number
3280             (apply '+ (mapcar
3281                        'gnus-summary-number-of-articles-in-thread
3282                        gnus-tmp-new-adopts))))
3283     (if char
3284         (if (> number 1) gnus-not-empty-thread-mark
3285           gnus-empty-thread-mark)
3286       number)))
3287
3288 (defsubst gnus-summary-line-message-size (head)
3289   "Return pretty-printed version of message size.
3290 This function is intended to be used in
3291 `gnus-summary-line-format-alist', which see."
3292   (let ((c (or (mail-header-chars head) -1)))
3293     (cond ((< c 0) "n/a")               ; chars not available
3294           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3295           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3296           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3297           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3298
3299
3300 (defun gnus-summary-set-local-parameters (group)
3301   "Go through the local params of GROUP and set all variable specs in that list."
3302   (let ((params (gnus-group-find-parameter group))
3303         (vars '(quit-config))           ; Ignore quit-config.
3304         elem)
3305     (while params
3306       (setq elem (car params)
3307             params (cdr params))
3308       (and (consp elem)                 ; Has to be a cons.
3309            (consp (cdr elem))           ; The cdr has to be a list.
3310            (symbolp (car elem))         ; Has to be a symbol in there.
3311            (not (memq (car elem) vars))
3312            (ignore-errors               ; So we set it.
3313              (push (car elem) vars)
3314              (make-local-variable (car elem))
3315              (set (car elem) (eval (nth 1 elem))))))))
3316
3317 (defun gnus-summary-read-group (group &optional show-all no-article
3318                                       kill-buffer no-display backward
3319                                       select-articles)
3320   "Start reading news in newsgroup GROUP.
3321 If SHOW-ALL is non-nil, already read articles are also listed.
3322 If NO-ARTICLE is non-nil, no article is selected initially.
3323 If NO-DISPLAY, don't generate a summary buffer."
3324   (let (result)
3325     (while (and group
3326                 (null (setq result
3327                             (let ((gnus-auto-select-next nil))
3328                               (or (gnus-summary-read-group-1
3329                                    group show-all no-article
3330                                    kill-buffer no-display
3331                                    select-articles)
3332                                   (setq show-all nil
3333                                         select-articles nil)))))
3334                 (eq gnus-auto-select-next 'quietly))
3335       (set-buffer gnus-group-buffer)
3336       ;; The entry function called above goes to the next
3337       ;; group automatically, so we go two groups back
3338       ;; if we are searching for the previous group.
3339       (when backward
3340         (gnus-group-prev-unread-group 2))
3341       (if (not (equal group (gnus-group-group-name)))
3342           (setq group (gnus-group-group-name))
3343         (setq group nil)))
3344     result))
3345
3346 (defun gnus-summary-read-group-1 (group show-all no-article
3347                                         kill-buffer no-display
3348                                         &optional select-articles)
3349   ;; Killed foreign groups can't be entered.
3350   ;;  (when (and (not (gnus-group-native-p group))
3351   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3352   ;;    (error "Dead non-native groups can't be entered"))
3353   (gnus-message 5 "Retrieving newsgroup: %s..."
3354                 (gnus-group-decoded-name group))
3355   (let* ((new-group (gnus-summary-setup-buffer group))
3356          (quit-config (gnus-group-quit-config group))
3357          (did-select (and new-group (gnus-select-newsgroup
3358                                      group show-all select-articles))))
3359     (cond
3360      ;; This summary buffer exists already, so we just select it.
3361      ((not new-group)
3362       (gnus-set-global-variables)
3363       (when kill-buffer
3364         (gnus-kill-or-deaden-summary kill-buffer))
3365       (gnus-configure-windows 'summary 'force)
3366       (gnus-set-mode-line 'summary)
3367       (gnus-summary-position-point)
3368       (message "")
3369       t)
3370      ;; We couldn't select this group.
3371      ((null did-select)
3372       (when (and (eq major-mode 'gnus-summary-mode)
3373                  (not (equal (current-buffer) kill-buffer)))
3374         (kill-buffer (current-buffer))
3375         (if (not quit-config)
3376             (progn
3377               ;; Update the info -- marks might need to be removed,
3378               ;; for instance.
3379               (gnus-summary-update-info)
3380               (set-buffer gnus-group-buffer)
3381               (gnus-group-jump-to-group group)
3382               (gnus-group-next-unread-group 1))
3383           (gnus-handle-ephemeral-exit quit-config)))
3384       (let ((grpinfo (gnus-get-info group)))
3385         (if (null (gnus-info-read grpinfo))
3386             (gnus-message 3 "Group %s contains no messages"
3387                           (gnus-group-decoded-name group))
3388           (gnus-message 3 "Can't select group")))
3389       nil)
3390      ;; The user did a `C-g' while prompting for number of articles,
3391      ;; so we exit this group.
3392      ((eq did-select 'quit)
3393       (and (eq major-mode 'gnus-summary-mode)
3394            (not (equal (current-buffer) kill-buffer))
3395            (kill-buffer (current-buffer)))
3396       (when kill-buffer
3397         (gnus-kill-or-deaden-summary kill-buffer))
3398       (if (not quit-config)
3399           (progn
3400             (set-buffer gnus-group-buffer)
3401             (gnus-group-jump-to-group group)
3402             (gnus-group-next-unread-group 1)
3403             (gnus-configure-windows 'group 'force))
3404         (gnus-handle-ephemeral-exit quit-config))
3405       ;; Finally signal the quit.
3406       (signal 'quit nil))
3407      ;; The group was successfully selected.
3408      (t
3409       (gnus-set-global-variables)
3410       ;; Save the active value in effect when the group was entered.
3411       (setq gnus-newsgroup-active
3412             (gnus-copy-sequence
3413              (gnus-active gnus-newsgroup-name)))
3414       ;; You can change the summary buffer in some way with this hook.
3415       (gnus-run-hooks 'gnus-select-group-hook)
3416       (gnus-update-format-specifications
3417        nil 'summary 'summary-mode 'summary-dummy)
3418       (gnus-update-summary-mark-positions)
3419       ;; Do score processing.
3420       (when gnus-use-scoring
3421         (gnus-possibly-score-headers))
3422       ;; Check whether to fill in the gaps in the threads.
3423       (when gnus-build-sparse-threads
3424         (gnus-build-sparse-threads))
3425       ;; Find the initial limit.
3426       (if gnus-show-threads
3427           (if show-all
3428               (let ((gnus-newsgroup-dormant nil))
3429                 (gnus-summary-initial-limit show-all))
3430             (gnus-summary-initial-limit show-all))
3431         ;; When unthreaded, all articles are always shown.
3432         (setq gnus-newsgroup-limit
3433               (mapcar
3434                (lambda (header) (mail-header-number header))
3435                gnus-newsgroup-headers)))
3436       ;; Generate the summary buffer.
3437       (unless no-display
3438         (gnus-summary-prepare))
3439       (when gnus-use-trees
3440         (gnus-tree-open group)
3441         (setq gnus-summary-highlight-line-function
3442               'gnus-tree-highlight-article))
3443       ;; If the summary buffer is empty, but there are some low-scored
3444       ;; articles or some excluded dormants, we include these in the
3445       ;; buffer.
3446       (when (and (zerop (buffer-size))
3447                  (not no-display))
3448         (cond (gnus-newsgroup-dormant
3449                (gnus-summary-limit-include-dormant))
3450               ((and gnus-newsgroup-scored show-all)
3451                (gnus-summary-limit-include-expunged t))))
3452       ;; Function `gnus-apply-kill-file' must be called in this hook.
3453       (gnus-run-hooks 'gnus-apply-kill-hook)
3454       (if (and (zerop (buffer-size))
3455                (not no-display))
3456           (progn
3457             ;; This newsgroup is empty.
3458             (gnus-summary-catchup-and-exit nil t)
3459             (gnus-message 6 "No unread news")
3460             (when kill-buffer
3461               (gnus-kill-or-deaden-summary kill-buffer))
3462             ;; Return nil from this function.
3463             nil)
3464         ;; Hide conversation thread subtrees.  We cannot do this in
3465         ;; gnus-summary-prepare-hook since kill processing may not
3466         ;; work with hidden articles.
3467         (gnus-summary-maybe-hide-threads)
3468         (when kill-buffer
3469           (gnus-kill-or-deaden-summary kill-buffer))
3470         (gnus-summary-auto-select-subject)
3471         ;; Show first unread article if requested.
3472         (if (and (not no-article)
3473                  (not no-display)
3474                  gnus-newsgroup-unreads
3475                  gnus-auto-select-first)
3476             (progn
3477               (gnus-configure-windows 'summary)
3478               (let ((art (gnus-summary-article-number)))
3479                 (unless (or (memq art gnus-newsgroup-undownloaded)
3480                             (memq art gnus-newsgroup-downloadable))
3481                   (gnus-summary-goto-article art))))
3482           ;; Don't select any articles.
3483           (gnus-summary-position-point)
3484           (gnus-configure-windows 'summary 'force)
3485           (gnus-set-mode-line 'summary))
3486         (when (get-buffer-window gnus-group-buffer t)
3487           ;; Gotta use windows, because recenter does weird stuff if
3488           ;; the current buffer ain't the displayed window.
3489           (let ((owin (selected-window)))
3490             (select-window (get-buffer-window gnus-group-buffer t))
3491             (when (gnus-group-goto-group group)
3492               (recenter))
3493             (select-window owin)))
3494         ;; Mark this buffer as "prepared".
3495         (setq gnus-newsgroup-prepared t)
3496         (gnus-run-hooks 'gnus-summary-prepared-hook)
3497         (gnus-group-update-group group)
3498         t)))))
3499
3500 (defun gnus-summary-auto-select-subject ()
3501   "Select the subject line on initial group entry."
3502   (goto-char (point-min))
3503   (cond
3504    ((eq gnus-auto-select-subject 'best)
3505     (gnus-summary-best-unread-subject))
3506    ((eq gnus-auto-select-subject 'unread)
3507     (gnus-summary-first-unread-subject))
3508    ((eq gnus-auto-select-subject 'unseen)
3509     (gnus-summary-first-unseen-subject))
3510    ((eq gnus-auto-select-subject 'unseen-or-unread)
3511     (gnus-summary-first-unseen-or-unread-subject))
3512    ((eq gnus-auto-select-subject 'first)
3513     ;; Do nothing.
3514     )
3515    ((gnus-functionp gnus-auto-select-subject)
3516     (funcall gnus-auto-select-subject))))
3517
3518 (defun gnus-summary-prepare ()
3519   "Generate the summary buffer."
3520   (interactive)
3521   (let ((buffer-read-only nil))
3522     (erase-buffer)
3523     (setq gnus-newsgroup-data nil
3524           gnus-newsgroup-data-reverse nil)
3525     (gnus-run-hooks 'gnus-summary-generate-hook)
3526     ;; Generate the buffer, either with threads or without.
3527     (when gnus-newsgroup-headers
3528       (gnus-summary-prepare-threads
3529        (if gnus-show-threads
3530            (gnus-sort-gathered-threads
3531             (funcall gnus-summary-thread-gathering-function
3532                      (gnus-sort-threads
3533                       (gnus-cut-threads (gnus-make-threads)))))
3534          ;; Unthreaded display.
3535          (gnus-sort-articles gnus-newsgroup-headers))))
3536     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3537     ;; Call hooks for modifying summary buffer.
3538     (goto-char (point-min))
3539     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3540
3541 (defsubst gnus-general-simplify-subject (subject)
3542   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3543   (setq subject
3544         (cond
3545          ;; Truncate the subject.
3546          (gnus-simplify-subject-functions
3547           (gnus-map-function gnus-simplify-subject-functions subject))
3548          ((numberp gnus-summary-gather-subject-limit)
3549           (setq subject (gnus-simplify-subject-re subject))
3550           (if (> (length subject) gnus-summary-gather-subject-limit)
3551               (substring subject 0 gnus-summary-gather-subject-limit)
3552             subject))
3553          ;; Fuzzily simplify it.
3554          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3555           (gnus-simplify-subject-fuzzy subject))
3556          ;; Just remove the leading "Re:".
3557          (t
3558           (gnus-simplify-subject-re subject))))
3559
3560   (if (and gnus-summary-gather-exclude-subject
3561            (string-match gnus-summary-gather-exclude-subject subject))
3562       nil                         ; This article shouldn't be gathered
3563     subject))
3564
3565 (defun gnus-summary-simplify-subject-query ()
3566   "Query where the respool algorithm would put this article."
3567   (interactive)
3568   (gnus-summary-select-article)
3569   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3570
3571 (defun gnus-gather-threads-by-subject (threads)
3572   "Gather threads by looking at Subject headers."
3573   (if (not gnus-summary-make-false-root)
3574       threads
3575     (let ((hashtb (gnus-make-hashtable 1024))
3576           (prev threads)
3577           (result threads)
3578           subject hthread whole-subject)
3579       (while threads
3580         (setq subject (gnus-general-simplify-subject
3581                        (setq whole-subject (mail-header-subject
3582                                             (caar threads)))))
3583         (when subject
3584           (if (setq hthread (gnus-gethash subject hashtb))
3585               (progn
3586                 ;; We enter a dummy root into the thread, if we
3587                 ;; haven't done that already.
3588                 (unless (stringp (caar hthread))
3589                   (setcar hthread (list whole-subject (car hthread))))
3590                 ;; We add this new gathered thread to this gathered
3591                 ;; thread.
3592                 (setcdr (car hthread)
3593                         (nconc (cdar hthread) (list (car threads))))
3594                 ;; Remove it from the list of threads.
3595                 (setcdr prev (cdr threads))
3596                 (setq threads prev))
3597             ;; Enter this thread into the hash table.
3598             (gnus-sethash subject
3599                           (if gnus-summary-make-false-root-always
3600                               (progn
3601                                 ;; If you want a dummy root above all
3602                                 ;; threads...
3603                                 (setcar threads (list whole-subject
3604                                                       (car threads)))
3605                                 threads)
3606                             threads)
3607                           hashtb)))
3608         (setq prev threads)
3609         (setq threads (cdr threads)))
3610       result)))
3611
3612 (defun gnus-gather-threads-by-references (threads)
3613   "Gather threads by looking at References headers."
3614   (let ((idhashtb (gnus-make-hashtable 1024))
3615         (thhashtb (gnus-make-hashtable 1024))
3616         (prev threads)
3617         (result threads)
3618         ids references id gthread gid entered ref)
3619     (while threads
3620       (when (setq references (mail-header-references (caar threads)))
3621         (setq id (mail-header-id (caar threads))
3622               ids (inline (gnus-split-references references))
3623               entered nil)
3624         (while (setq ref (pop ids))
3625           (setq ids (delete ref ids))
3626           (if (not (setq gid (gnus-gethash ref idhashtb)))
3627               (progn
3628                 (gnus-sethash ref id idhashtb)
3629                 (gnus-sethash id threads thhashtb))
3630             (setq gthread (gnus-gethash gid thhashtb))
3631             (unless entered
3632               ;; We enter a dummy root into the thread, if we
3633               ;; haven't done that already.
3634               (unless (stringp (caar gthread))
3635                 (setcar gthread (list (mail-header-subject (caar gthread))
3636                                       (car gthread))))
3637               ;; We add this new gathered thread to this gathered
3638               ;; thread.
3639               (setcdr (car gthread)
3640                       (nconc (cdar gthread) (list (car threads)))))
3641             ;; Add it into the thread hash table.
3642             (gnus-sethash id gthread thhashtb)
3643             (setq entered t)
3644             ;; Remove it from the list of threads.
3645             (setcdr prev (cdr threads))
3646             (setq threads prev))))
3647       (setq prev threads)
3648       (setq threads (cdr threads)))
3649     result))
3650
3651 (defun gnus-sort-gathered-threads (threads)
3652   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3653   (let ((result threads))
3654     (while threads
3655       (when (stringp (caar threads))
3656         (setcdr (car threads)
3657                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3658       (setq threads (cdr threads)))
3659     result))
3660
3661 (defun gnus-thread-loop-p (root thread)
3662   "Say whether ROOT is in THREAD."
3663   (let ((stack (list thread))
3664         (infloop 0)
3665         th)
3666     (while (setq thread (pop stack))
3667       (setq th (cdr thread))
3668       (while (and th
3669                   (not (eq (caar th) root)))
3670         (pop th))
3671       (if th
3672           ;; We have found a loop.
3673           (let (ref-dep)
3674             (setcdr thread (delq (car th) (cdr thread)))
3675             (if (boundp (setq ref-dep (intern "none"
3676                                               gnus-newsgroup-dependencies)))
3677                 (setcdr (symbol-value ref-dep)
3678                         (nconc (cdr (symbol-value ref-dep))
3679                                (list (car th))))
3680               (set ref-dep (list nil (car th))))
3681             (setq infloop 1
3682                   stack nil))
3683         ;; Push all the subthreads onto the stack.
3684         (push (cdr thread) stack)))
3685     infloop))
3686
3687 (defun gnus-make-threads ()
3688   "Go through the dependency hashtb and find the roots.  Return all threads."
3689   (let (threads)
3690     (while (catch 'infloop
3691              (mapatoms
3692               (lambda (refs)
3693                 ;; Deal with self-referencing References loops.
3694                 (when (and (car (symbol-value refs))
3695                            (not (zerop
3696                                  (apply
3697                                   '+
3698                                   (mapcar
3699                                    (lambda (thread)
3700                                      (gnus-thread-loop-p
3701                                       (car (symbol-value refs)) thread))
3702                                    (cdr (symbol-value refs)))))))
3703                   (setq threads nil)
3704                   (throw 'infloop t))
3705                 (unless (car (symbol-value refs))
3706                   ;; These threads do not refer back to any other
3707                   ;; articles, so they're roots.
3708                   (setq threads (append (cdr (symbol-value refs)) threads))))
3709               gnus-newsgroup-dependencies)))
3710     threads))
3711
3712 ;; Build the thread tree.
3713 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3714   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3715
3716 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3717 if it was already present.
3718
3719 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3720 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3721 Message-IDs will be renamed to a unique Message-ID before being
3722 entered.
3723
3724 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3725   (let* ((id (mail-header-id header))
3726          (id-dep (and id (intern id dependencies)))
3727          parent-id ref ref-dep ref-header replaced)
3728     ;; Enter this `header' in the `dependencies' table.
3729     (cond
3730      ((not id-dep)
3731       (setq header nil))
3732      ;; The first two cases do the normal part: enter a new `header'
3733      ;; in the `dependencies' table.
3734      ((not (boundp id-dep))
3735       (set id-dep (list header)))
3736      ((null (car (symbol-value id-dep)))
3737       (setcar (symbol-value id-dep) header))
3738
3739      ;; From here the `header' was already present in the
3740      ;; `dependencies' table.
3741      (force-new
3742       ;; Overrides an existing entry;
3743       ;; just set the header part of the entry.
3744       (setcar (symbol-value id-dep) header)
3745       (setq replaced t))
3746
3747      ;; Renames the existing `header' to a unique Message-ID.
3748      ((not gnus-summary-ignore-duplicates)
3749       ;; An article with this Message-ID has already been seen.
3750       ;; We rename the Message-ID.
3751       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3752            (list header))
3753       (mail-header-set-id header id))
3754
3755      ;; The last case ignores an existing entry, except it adds any
3756      ;; additional Xrefs (in case the two articles came from different
3757      ;; servers.
3758      ;; Also sets `header' to `nil' meaning that the `dependencies'
3759      ;; table was *not* modified.
3760      (t
3761       (mail-header-set-xref
3762        (car (symbol-value id-dep))
3763        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3764                    "")
3765                (or (mail-header-xref header) "")))
3766       (setq header nil)))
3767
3768     (when (and header (not replaced))
3769       ;; First check that we are not creating a References loop.
3770       (setq parent-id (gnus-parent-id (mail-header-references header)))
3771       (setq ref parent-id)
3772       (while (and ref
3773                   (setq ref-dep (intern-soft ref dependencies))
3774                   (boundp ref-dep)
3775                   (setq ref-header (car (symbol-value ref-dep))))
3776         (if (string= id ref)
3777             ;; Yuk!  This is a reference loop.  Make the article be a
3778             ;; root article.
3779             (progn
3780               (mail-header-set-references (car (symbol-value id-dep)) "none")
3781               (setq ref nil)
3782               (setq parent-id nil))
3783           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3784       (setq ref-dep (intern (or parent-id "none") dependencies))
3785       (if (boundp ref-dep)
3786           (setcdr (symbol-value ref-dep)
3787                   (nconc (cdr (symbol-value ref-dep))
3788                          (list (symbol-value id-dep))))
3789         (set ref-dep (list nil (symbol-value id-dep)))))
3790     header))
3791
3792 (defun gnus-extract-message-id-from-in-reply-to (string)
3793   (if (string-match "<[^>]+>" string)
3794       (substring string (match-beginning 0) (match-end 0))
3795     nil))
3796
3797 (defun gnus-build-sparse-threads ()
3798   (let ((headers gnus-newsgroup-headers)
3799         (mail-parse-charset gnus-newsgroup-charset)
3800         (gnus-summary-ignore-duplicates t)
3801         header references generation relations
3802         subject child end new-child date)
3803     ;; First we create an alist of generations/relations, where
3804     ;; generations is how much we trust the relation, and the relation
3805     ;; is parent/child.
3806     (gnus-message 7 "Making sparse threads...")
3807     (save-excursion
3808       (nnheader-set-temp-buffer " *gnus sparse threads*")
3809       (while (setq header (pop headers))
3810         (when (and (setq references (mail-header-references header))
3811                    (not (string= references "")))
3812           (insert references)
3813           (setq child (mail-header-id header)
3814                 subject (mail-header-subject header)
3815                 date (mail-header-date header)
3816                 generation 0)
3817           (while (search-backward ">" nil t)
3818             (setq end (1+ (point)))
3819             (when (search-backward "<" nil t)
3820               (setq new-child (buffer-substring (point) end))
3821               (push (list (incf generation)
3822                           child (setq child new-child)
3823                           subject date)
3824                     relations)))
3825           (when child
3826             (push (list (1+ generation) child nil subject) relations))
3827           (erase-buffer)))
3828       (kill-buffer (current-buffer)))
3829     ;; Sort over trustworthiness.
3830     (mapcar
3831      (lambda (relation)
3832        (when (gnus-dependencies-add-header
3833               (make-full-mail-header
3834                gnus-reffed-article-number
3835                (nth 3 relation) "" (or (nth 4 relation) "")
3836                (nth 1 relation)
3837                (or (nth 2 relation) "") 0 0 "")
3838               gnus-newsgroup-dependencies nil)
3839          (push gnus-reffed-article-number gnus-newsgroup-limit)
3840          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3841          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3842                gnus-newsgroup-reads)
3843          (decf gnus-reffed-article-number)))
3844      (sort relations 'car-less-than-car))
3845     (gnus-message 7 "Making sparse threads...done")))
3846
3847 (defun gnus-build-old-threads ()
3848   ;; Look at all the articles that refer back to old articles, and
3849   ;; fetch the headers for the articles that aren't there.  This will
3850   ;; build complete threads - if the roots haven't been expired by the
3851   ;; server, that is.
3852   (let ((mail-parse-charset gnus-newsgroup-charset)
3853         id heads)
3854     (mapatoms
3855      (lambda (refs)
3856        (when (not (car (symbol-value refs)))
3857          (setq heads (cdr (symbol-value refs)))
3858          (while heads
3859            (if (memq (mail-header-number (caar heads))
3860                      gnus-newsgroup-dormant)
3861                (setq heads (cdr heads))
3862              (setq id (symbol-name refs))
3863              (while (and (setq id (gnus-build-get-header id))
3864                          (not (car (gnus-id-to-thread id)))))
3865              (setq heads nil)))))
3866      gnus-newsgroup-dependencies)))
3867
3868 ;; This function has to be called with point after the article number
3869 ;; on the beginning of the line.
3870 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3871   (let ((eol (gnus-point-at-eol))
3872         (buffer (current-buffer))
3873         header references in-reply-to)
3874
3875     ;; overview: [num subject from date id refs chars lines misc]
3876     (unwind-protect
3877         (let (x)
3878           (narrow-to-region (point) eol)
3879           (unless (eobp)
3880             (forward-char))
3881
3882           (setq header
3883                 (make-full-mail-header
3884                  number                 ; number
3885                  (condition-case ()     ; subject
3886                      (funcall gnus-decode-encoded-word-function
3887                               (setq x (nnheader-nov-field)))
3888                    (error x))
3889                  (condition-case ()     ; from
3890                      (funcall gnus-decode-encoded-word-function
3891                               (setq x (nnheader-nov-field)))
3892                    (error x))
3893                  (nnheader-nov-field)   ; date
3894                  (nnheader-nov-read-message-id) ; id
3895                  (setq references (nnheader-nov-field)) ; refs
3896                  (nnheader-nov-read-integer) ; chars
3897                  (nnheader-nov-read-integer) ; lines
3898                  (unless (eobp)
3899                    (if (looking-at "Xref: ")
3900                        (goto-char (match-end 0)))
3901                    (nnheader-nov-field)) ; Xref
3902                  (nnheader-nov-parse-extra)))) ; extra
3903
3904       (widen))
3905
3906     (when (and (string= references "")
3907                (setq in-reply-to (mail-header-extra header))
3908                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3909       (mail-header-set-references
3910        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3911
3912     (when gnus-alter-header-function
3913       (funcall gnus-alter-header-function header))
3914     (gnus-dependencies-add-header header dependencies force-new)))
3915
3916 (defun gnus-build-get-header (id)
3917   "Look through the buffer of NOV lines and find the header to ID.
3918 Enter this line into the dependencies hash table, and return
3919 the id of the parent article (if any)."
3920   (let ((deps gnus-newsgroup-dependencies)
3921         found header)
3922     (prog1
3923         (save-excursion
3924           (set-buffer nntp-server-buffer)
3925           (let ((case-fold-search nil))
3926             (goto-char (point-min))
3927             (while (and (not found)
3928                         (search-forward id nil t))
3929               (beginning-of-line)
3930               (setq found (looking-at
3931                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3932                                    (regexp-quote id))))
3933               (or found (beginning-of-line 2)))
3934             (when found
3935               (beginning-of-line)
3936               (and
3937                (setq header (gnus-nov-parse-line
3938                              (read (current-buffer)) deps))
3939                (gnus-parent-id (mail-header-references header))))))
3940       (when header
3941         (let ((number (mail-header-number header)))
3942           (push number gnus-newsgroup-limit)
3943           (push header gnus-newsgroup-headers)
3944           (if (memq number gnus-newsgroup-unselected)
3945               (progn
3946                 (setq gnus-newsgroup-unreads
3947                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3948                                                number))
3949                 (setq gnus-newsgroup-unselected
3950                       (delq number gnus-newsgroup-unselected)))
3951             (push number gnus-newsgroup-ancient)))))))
3952
3953 (defun gnus-build-all-threads ()
3954   "Read all the headers."
3955   (let ((gnus-summary-ignore-duplicates t)
3956         (mail-parse-charset gnus-newsgroup-charset)
3957         (dependencies gnus-newsgroup-dependencies)
3958         header article)
3959     (save-excursion
3960       (set-buffer nntp-server-buffer)
3961       (let ((case-fold-search nil))
3962         (goto-char (point-min))
3963         (while (not (eobp))
3964           (ignore-errors
3965             (setq article (read (current-buffer))
3966                   header (gnus-nov-parse-line article dependencies)))
3967           (when header
3968             (save-excursion
3969               (set-buffer gnus-summary-buffer)
3970               (push header gnus-newsgroup-headers)
3971               (if (memq (setq article (mail-header-number header))
3972                         gnus-newsgroup-unselected)
3973                   (progn
3974                     (setq gnus-newsgroup-unreads
3975                           (gnus-add-to-sorted-list
3976                            gnus-newsgroup-unreads article))
3977                     (setq gnus-newsgroup-unselected
3978                           (delq article gnus-newsgroup-unselected)))
3979                 (push article gnus-newsgroup-ancient)))
3980             (forward-line 1)))))))
3981
3982 (defun gnus-summary-update-article-line (article header)
3983   "Update the line for ARTICLE using HEADERS."
3984   (let* ((id (mail-header-id header))
3985          (thread (gnus-id-to-thread id)))
3986     (unless thread
3987       (error "Article in no thread"))
3988     ;; Update the thread.
3989     (setcar thread header)
3990     (gnus-summary-goto-subject article)
3991     (let* ((datal (gnus-data-find-list article))
3992            (data (car datal))
3993            (length (when (cdr datal)
3994                      (- (gnus-data-pos data)
3995                         (gnus-data-pos (cadr datal)))))
3996            (buffer-read-only nil)
3997            (level (gnus-summary-thread-level)))
3998       (gnus-delete-line)
3999       (gnus-summary-insert-line
4000        header level nil 
4001        (memq article gnus-newsgroup-undownloaded)
4002        (gnus-article-mark article)
4003        (memq article gnus-newsgroup-replied)
4004        (memq article gnus-newsgroup-expirable)
4005        ;; Only insert the Subject string when it's different
4006        ;; from the previous Subject string.
4007        (if (and
4008             gnus-show-threads
4009             (gnus-subject-equal
4010              (condition-case ()
4011                  (mail-header-subject
4012                   (gnus-data-header
4013                    (cadr
4014                     (gnus-data-find-list
4015                      article
4016                      (gnus-data-list t)))))
4017                ;; Error on the side of excessive subjects.
4018                (error ""))
4019              (mail-header-subject header)))
4020            ""
4021          (mail-header-subject header))
4022        nil (cdr (assq article gnus-newsgroup-scored))
4023        (memq article gnus-newsgroup-processable))
4024       (when length
4025         (gnus-data-update-list
4026          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4027
4028 (defun gnus-summary-update-article (article &optional iheader)
4029   "Update ARTICLE in the summary buffer."
4030   (set-buffer gnus-summary-buffer)
4031   (let* ((header (gnus-summary-article-header article))
4032          (id (mail-header-id header))
4033          (data (gnus-data-find article))
4034          (thread (gnus-id-to-thread id))
4035          (references (mail-header-references header))
4036          (parent
4037           (gnus-id-to-thread
4038            (or (gnus-parent-id
4039                 (when (and references
4040                            (not (equal "" references)))
4041                   references))
4042                "none")))
4043          (buffer-read-only nil)
4044          (old (car thread)))
4045     (when thread
4046       (unless iheader
4047         (setcar thread nil)
4048         (when parent
4049           (delq thread parent)))
4050       (if (gnus-summary-insert-subject id header)
4051           ;; Set the (possibly) new article number in the data structure.
4052           (gnus-data-set-number data (gnus-id-to-article id))
4053         (setcar thread old)
4054         nil))))
4055
4056 (defun gnus-rebuild-thread (id &optional line)
4057   "Rebuild the thread containing ID.
4058 If LINE, insert the rebuilt thread starting on line LINE."
4059   (let ((buffer-read-only nil)
4060         old-pos current thread data)
4061     (if (not gnus-show-threads)
4062         (setq thread (list (car (gnus-id-to-thread id))))
4063       ;; Get the thread this article is part of.
4064       (setq thread (gnus-remove-thread id)))
4065     (setq old-pos (gnus-point-at-bol))
4066     (setq current (save-excursion
4067                     (and (re-search-backward "[\r\n]" nil t)
4068                          (gnus-summary-article-number))))
4069     ;; If this is a gathered thread, we have to go some re-gathering.
4070     (when (stringp (car thread))
4071       (let ((subject (car thread))
4072             roots thr)
4073         (setq thread (cdr thread))
4074         (while thread
4075           (unless (memq (setq thr (gnus-id-to-thread
4076                                    (gnus-root-id
4077                                     (mail-header-id (caar thread)))))
4078                         roots)
4079             (push thr roots))
4080           (setq thread (cdr thread)))
4081         ;; We now have all (unique) roots.
4082         (if (= (length roots) 1)
4083             ;; All the loose roots are now one solid root.
4084             (setq thread (car roots))
4085           (setq thread (cons subject (gnus-sort-threads roots))))))
4086     (let (threads)
4087       ;; We then insert this thread into the summary buffer.
4088       (when line
4089         (goto-char (point-min))
4090         (forward-line (1- line)))
4091       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4092         (if gnus-show-threads
4093             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4094           (gnus-summary-prepare-unthreaded thread))
4095         (setq data (nreverse gnus-newsgroup-data))
4096         (setq threads gnus-newsgroup-threads))
4097       ;; We splice the new data into the data structure.
4098       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4099       ;;!!! then we want to insert at the beginning of the buffer.
4100       ;;!!! That happens to be true with Gnus now, but that may
4101       ;;!!! change in the future.  Perhaps.
4102       (gnus-data-enter-list
4103        (if line nil current) data (- (point) old-pos))
4104       (setq gnus-newsgroup-threads
4105             (nconc threads gnus-newsgroup-threads))
4106       (gnus-data-compute-positions))))
4107
4108 (defun gnus-number-to-header (number)
4109   "Return the header for article NUMBER."
4110   (let ((headers gnus-newsgroup-headers))
4111     (while (and headers
4112                 (not (= number (mail-header-number (car headers)))))
4113       (pop headers))
4114     (when headers
4115       (car headers))))
4116
4117 (defun gnus-parent-headers (in-headers &optional generation)
4118   "Return the headers of the GENERATIONeth parent of HEADERS."
4119   (unless generation
4120     (setq generation 1))
4121   (let ((parent t)
4122         (headers in-headers)
4123         references)
4124     (while (and parent
4125                 (not (zerop generation))
4126                 (setq references (mail-header-references headers)))
4127       (setq headers (if (and references
4128                              (setq parent (gnus-parent-id references)))
4129                         (car (gnus-id-to-thread parent))
4130                       nil))
4131       (decf generation))
4132     (and (not (eq headers in-headers))
4133          headers)))
4134
4135 (defun gnus-id-to-thread (id)
4136   "Return the (sub-)thread where ID appears."
4137   (gnus-gethash id gnus-newsgroup-dependencies))
4138
4139 (defun gnus-id-to-article (id)
4140   "Return the article number of ID."
4141   (let ((thread (gnus-id-to-thread id)))
4142     (when (and thread
4143                (car thread))
4144       (mail-header-number (car thread)))))
4145
4146 (defun gnus-id-to-header (id)
4147   "Return the article headers of ID."
4148   (car (gnus-id-to-thread id)))
4149
4150 (defun gnus-article-displayed-root-p (article)
4151   "Say whether ARTICLE is a root(ish) article."
4152   (let ((level (gnus-summary-thread-level article))
4153         (refs (mail-header-references  (gnus-summary-article-header article)))
4154         particle)
4155     (cond
4156      ((null level) nil)
4157      ((zerop level) t)
4158      ((null refs) t)
4159      ((null (gnus-parent-id refs)) t)
4160      ((and (= 1 level)
4161            (null (setq particle (gnus-id-to-article
4162                                  (gnus-parent-id refs))))
4163            (null (gnus-summary-thread-level particle)))))))
4164
4165 (defun gnus-root-id (id)
4166   "Return the id of the root of the thread where ID appears."
4167   (let (last-id prev)
4168     (while (and id (setq prev (car (gnus-id-to-thread id))))
4169       (setq last-id id
4170             id (gnus-parent-id (mail-header-references prev))))
4171     last-id))
4172
4173 (defun gnus-articles-in-thread (thread)
4174   "Return the list of articles in THREAD."
4175   (cons (mail-header-number (car thread))
4176         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4177
4178 (defun gnus-remove-thread (id &optional dont-remove)
4179   "Remove the thread that has ID in it."
4180   (let (headers thread last-id)
4181     ;; First go up in this thread until we find the root.
4182     (setq last-id (gnus-root-id id)
4183           headers (message-flatten-list (gnus-id-to-thread last-id)))
4184     ;; We have now found the real root of this thread.  It might have
4185     ;; been gathered into some loose thread, so we have to search
4186     ;; through the threads to find the thread we wanted.
4187     (let ((threads gnus-newsgroup-threads)
4188           sub)
4189       (while threads
4190         (setq sub (car threads))
4191         (if (stringp (car sub))
4192             ;; This is a gathered thread, so we look at the roots
4193             ;; below it to find whether this article is in this
4194             ;; gathered root.
4195             (progn
4196               (setq sub (cdr sub))
4197               (while sub
4198                 (when (member (caar sub) headers)
4199                   (setq thread (car threads)
4200                         threads nil
4201                         sub nil))
4202                 (setq sub (cdr sub))))
4203           ;; It's an ordinary thread, so we check it.
4204           (when (eq (car sub) (car headers))
4205             (setq thread sub
4206                   threads nil)))
4207         (setq threads (cdr threads)))
4208       ;; If this article is in no thread, then it's a root.
4209       (if thread
4210           (unless dont-remove
4211             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4212         (setq thread (gnus-id-to-thread last-id)))
4213       (when thread
4214         (prog1
4215             thread                      ; We return this thread.
4216           (unless dont-remove
4217             (if (stringp (car thread))
4218                 (progn
4219                   ;; If we use dummy roots, then we have to remove the
4220                   ;; dummy root as well.
4221                   (when (eq gnus-summary-make-false-root 'dummy)
4222                     ;; We go to the dummy root by going to
4223                     ;; the first sub-"thread", and then one line up.
4224                     (gnus-summary-goto-article
4225                      (mail-header-number (caadr thread)))
4226                     (forward-line -1)
4227                     (gnus-delete-line)
4228                     (gnus-data-compute-positions))
4229                   (setq thread (cdr thread))
4230                   (while thread
4231                     (gnus-remove-thread-1 (car thread))
4232                     (setq thread (cdr thread))))
4233               (gnus-remove-thread-1 thread))))))))
4234
4235 (defun gnus-remove-thread-1 (thread)
4236   "Remove the thread THREAD recursively."
4237   (let ((number (mail-header-number (pop thread)))
4238         d)
4239     (setq thread (reverse thread))
4240     (while thread
4241       (gnus-remove-thread-1 (pop thread)))
4242     (when (setq d (gnus-data-find number))
4243       (goto-char (gnus-data-pos d))
4244       (gnus-summary-show-thread)
4245       (gnus-data-remove
4246        number
4247        (- (gnus-point-at-bol)
4248           (prog1
4249               (1+ (gnus-point-at-eol))
4250             (gnus-delete-line)))))))
4251
4252 (defun gnus-sort-threads-1 (threads func)
4253   (sort (mapcar (lambda (thread)
4254                   (cons (car thread)
4255                         (and (cdr thread)
4256                              (gnus-sort-threads-1 (cdr thread) func))))
4257                 threads) func))
4258
4259 (defun gnus-sort-threads (threads)
4260   "Sort THREADS."
4261   (if (not gnus-thread-sort-functions)
4262       threads
4263     (gnus-message 8 "Sorting threads...")
4264     (let ((max-lisp-eval-depth 5000))
4265       (prog1 (gnus-sort-threads-1
4266          threads
4267          (gnus-make-sort-function gnus-thread-sort-functions))
4268         (gnus-message 8 "Sorting threads...done")))))
4269
4270 (defun gnus-sort-articles (articles)
4271   "Sort ARTICLES."
4272   (when gnus-article-sort-functions
4273     (gnus-message 7 "Sorting articles...")
4274     (prog1
4275         (setq gnus-newsgroup-headers
4276               (sort articles (gnus-make-sort-function
4277                               gnus-article-sort-functions)))
4278       (gnus-message 7 "Sorting articles...done"))))
4279
4280 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4281 (defmacro gnus-thread-header (thread)
4282   "Return header of first article in THREAD.
4283 Note that THREAD must never, ever be anything else than a variable -
4284 using some other form will lead to serious barfage."
4285   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4286   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4287   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4288         (vector thread) 2))
4289
4290 (defsubst gnus-article-sort-by-number (h1 h2)
4291   "Sort articles by article number."
4292   (< (mail-header-number h1)
4293      (mail-header-number h2)))
4294
4295 (defun gnus-thread-sort-by-number (h1 h2)
4296   "Sort threads by root article number."
4297   (gnus-article-sort-by-number
4298    (gnus-thread-header h1) (gnus-thread-header h2)))
4299
4300 (defsubst gnus-article-sort-by-random (h1 h2)
4301   "Sort articles by article number."
4302   (zerop (random 2)))
4303
4304 (defun gnus-thread-sort-by-random (h1 h2)
4305   "Sort threads by root article number."
4306   (gnus-article-sort-by-random
4307    (gnus-thread-header h1) (gnus-thread-header h2)))
4308
4309 (defsubst gnus-article-sort-by-lines (h1 h2)
4310   "Sort articles by article Lines header."
4311   (< (mail-header-lines h1)
4312      (mail-header-lines h2)))
4313
4314 (defun gnus-thread-sort-by-lines (h1 h2)
4315   "Sort threads by root article Lines header."
4316   (gnus-article-sort-by-lines
4317    (gnus-thread-header h1) (gnus-thread-header h2)))
4318
4319 (defsubst gnus-article-sort-by-chars (h1 h2)
4320   "Sort articles by octet length."
4321   (< (mail-header-chars h1)
4322      (mail-header-chars h2)))
4323
4324 (defun gnus-thread-sort-by-chars (h1 h2)
4325   "Sort threads by root article octet length."
4326   (gnus-article-sort-by-chars
4327    (gnus-thread-header h1) (gnus-thread-header h2)))
4328
4329 (defsubst gnus-article-sort-by-author (h1 h2)
4330   "Sort articles by root author."
4331   (string-lessp
4332    (let ((extract (funcall
4333                    gnus-extract-address-components
4334                    (mail-header-from h1))))
4335      (or (car extract) (cadr extract) ""))
4336    (let ((extract (funcall
4337                    gnus-extract-address-components
4338                    (mail-header-from h2))))
4339      (or (car extract) (cadr extract) ""))))
4340
4341 (defun gnus-thread-sort-by-author (h1 h2)
4342   "Sort threads by root author."
4343   (gnus-article-sort-by-author
4344    (gnus-thread-header h1)  (gnus-thread-header h2)))
4345
4346 (defsubst gnus-article-sort-by-subject (h1 h2)
4347   "Sort articles by root subject."
4348   (string-lessp
4349    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4350    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4351
4352 (defun gnus-thread-sort-by-subject (h1 h2)
4353   "Sort threads by root subject."
4354   (gnus-article-sort-by-subject
4355    (gnus-thread-header h1) (gnus-thread-header h2)))
4356
4357 (defsubst gnus-article-sort-by-date (h1 h2)
4358   "Sort articles by root article date."
4359   (time-less-p
4360    (gnus-date-get-time (mail-header-date h1))
4361    (gnus-date-get-time (mail-header-date h2))))
4362
4363 (defun gnus-thread-sort-by-date (h1 h2)
4364   "Sort threads by root article date."
4365   (gnus-article-sort-by-date
4366    (gnus-thread-header h1) (gnus-thread-header h2)))
4367
4368 (defsubst gnus-article-sort-by-score (h1 h2)
4369   "Sort articles by root article score.
4370 Unscored articles will be counted as having a score of zero."
4371   (> (or (cdr (assq (mail-header-number h1)
4372                     gnus-newsgroup-scored))
4373          gnus-summary-default-score 0)
4374      (or (cdr (assq (mail-header-number h2)
4375                     gnus-newsgroup-scored))
4376          gnus-summary-default-score 0)))
4377
4378 (defun gnus-thread-sort-by-score (h1 h2)
4379   "Sort threads by root article score."
4380   (gnus-article-sort-by-score
4381    (gnus-thread-header h1) (gnus-thread-header h2)))
4382
4383 (defun gnus-thread-sort-by-total-score (h1 h2)
4384   "Sort threads by the sum of all scores in the thread.
4385 Unscored articles will be counted as having a score of zero."
4386   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4387
4388 (defun gnus-thread-total-score (thread)
4389   ;; This function find the total score of THREAD.
4390   (cond
4391    ((null thread)
4392     0)
4393    ((consp thread)
4394     (if (stringp (car thread))
4395         (apply gnus-thread-score-function 0
4396                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4397       (gnus-thread-total-score-1 thread)))
4398    (t
4399     (gnus-thread-total-score-1 (list thread)))))
4400
4401 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4402   "Sort threads such that the thread with the most recently arrived article comes first."
4403   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4404
4405 (defun gnus-thread-highest-number (thread)
4406   "Return the highest article number in THREAD."
4407   (apply 'max (mapcar (lambda (header)
4408                         (mail-header-number header))
4409                       (message-flatten-list thread))))
4410
4411 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4412   "Sort threads such that the thread with the most recently dated article comes first."
4413   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4414
4415 (defun gnus-thread-latest-date (thread)
4416   "Return the highest article date in THREAD."
4417   (let ((previous-time 0))
4418     (apply 'max
4419            (mapcar
4420             (lambda (header)
4421               (setq previous-time
4422                     (time-to-seconds
4423                      (condition-case ()
4424                          (mail-header-parse-date (mail-header-date header))
4425                        (error previous-time)))))
4426             (sort
4427              (message-flatten-list thread)
4428              (lambda (h1 h2)
4429                (< (mail-header-number h1)
4430                   (mail-header-number h2))))))))
4431
4432 (defun gnus-thread-total-score-1 (root)
4433   ;; This function find the total score of the thread below ROOT.
4434   (setq root (car root))
4435   (apply gnus-thread-score-function
4436          (or (append
4437               (mapcar 'gnus-thread-total-score
4438                       (cdr (gnus-id-to-thread (mail-header-id root))))
4439               (when (> (mail-header-number root) 0)
4440                 (list (or (cdr (assq (mail-header-number root)
4441                                      gnus-newsgroup-scored))
4442                           gnus-summary-default-score 0))))
4443              (list gnus-summary-default-score)
4444              '(0))))
4445
4446 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4447 (defvar gnus-tmp-prev-subject nil)
4448 (defvar gnus-tmp-false-parent nil)
4449 (defvar gnus-tmp-root-expunged nil)
4450 (defvar gnus-tmp-dummy-line nil)
4451
4452 (eval-when-compile (defvar gnus-tmp-header))
4453 (defun gnus-extra-header (type &optional header)
4454   "Return the extra header of TYPE."
4455   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4456       ""))
4457
4458 (defvar gnus-tmp-thread-tree-header-string "")
4459
4460 (defcustom gnus-sum-thread-tree-root "> "
4461   "With %B spec, used for the root of a thread.
4462 If nil, use subject instead."
4463   :type 'string
4464   :group 'gnus-thread)
4465 (defcustom gnus-sum-thread-tree-single-indent ""
4466   "With %B spec, used for a thread with just one message.
4467 If nil, use subject instead."
4468   :type 'string
4469   :group 'gnus-thread)
4470 (defcustom gnus-sum-thread-tree-vertical "| "
4471   "With %B spec, used for drawing a vertical line."
4472   :type 'string
4473   :group 'gnus-thread)
4474 (defcustom gnus-sum-thread-tree-indent "  "
4475   "With %B spec, used for indenting."
4476   :type 'string
4477   :group 'gnus-thread)
4478 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4479   "With %B spec, used for a leaf with brothers."
4480   :type 'string
4481   :group 'gnus-thread)
4482 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4483   "With %B spec, used for a leaf without brothers."
4484   :type 'string
4485   :group 'gnus-thread)
4486
4487 (defun gnus-summary-prepare-threads (threads)
4488   "Prepare summary buffer from THREADS and indentation LEVEL.
4489 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4490 or a straight list of headers."
4491   (gnus-message 7 "Generating summary...")
4492
4493   (setq gnus-newsgroup-threads threads)
4494   (beginning-of-line)
4495
4496   (let ((gnus-tmp-level 0)
4497         (default-score (or gnus-summary-default-score 0))
4498         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4499         (building-line-count gnus-summary-display-while-building)
4500         (building-count (integerp gnus-summary-display-while-building))
4501         thread number subject stack state gnus-tmp-gathered beg-match
4502         new-roots gnus-tmp-new-adopts thread-end simp-subject
4503         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4504         gnus-tmp-replied gnus-tmp-subject-or-nil
4505         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4506         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4507         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4508         tree-stack)
4509
4510     (setq gnus-tmp-prev-subject nil)
4511
4512     (if (vectorp (car threads))
4513         ;; If this is a straight (sic) list of headers, then a
4514         ;; threaded summary display isn't required, so we just create
4515         ;; an unthreaded one.
4516         (gnus-summary-prepare-unthreaded threads)
4517
4518       ;; Do the threaded display.
4519
4520       (if gnus-summary-display-while-building
4521           (switch-to-buffer (buffer-name)))
4522       (while (or threads stack gnus-tmp-new-adopts new-roots)
4523
4524         (if (and (= gnus-tmp-level 0)
4525                  (or (not stack)
4526                      (= (caar stack) 0))
4527                  (not gnus-tmp-false-parent)
4528                  (or gnus-tmp-new-adopts new-roots))
4529             (if gnus-tmp-new-adopts
4530                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4531                       thread (list (car gnus-tmp-new-adopts))
4532                       gnus-tmp-header (caar thread)
4533                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4534               (when new-roots
4535                 (setq thread (list (car new-roots))
4536                       gnus-tmp-header (caar thread)
4537                       new-roots (cdr new-roots))))
4538
4539           (if threads
4540               ;; If there are some threads, we do them before the
4541               ;; threads on the stack.
4542               (setq thread threads
4543                     gnus-tmp-header (caar thread))
4544             ;; There were no current threads, so we pop something off
4545             ;; the stack.
4546             (setq state (car stack)
4547                   gnus-tmp-level (car state)
4548                   tree-stack (cadr state)
4549                   thread (caddr state)
4550                   stack (cdr stack)
4551                   gnus-tmp-header (caar thread))))
4552
4553         (setq gnus-tmp-false-parent nil)
4554         (setq gnus-tmp-root-expunged nil)
4555         (setq thread-end nil)
4556
4557         (if (stringp gnus-tmp-header)
4558             ;; The header is a dummy root.
4559             (cond
4560              ((eq gnus-summary-make-false-root 'adopt)
4561               ;; We let the first article adopt the rest.
4562               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4563                                                (cddar thread)))
4564               (setq gnus-tmp-gathered
4565                     (nconc (mapcar
4566                             (lambda (h) (mail-header-number (car h)))
4567                             (cddar thread))
4568                            gnus-tmp-gathered))
4569               (setq thread (cons (list (caar thread)
4570                                        (cadar thread))
4571                                  (cdr thread)))
4572               (setq gnus-tmp-level -1
4573                     gnus-tmp-false-parent t))
4574              ((eq gnus-summary-make-false-root 'empty)
4575               ;; We print adopted articles with empty subject fields.
4576               (setq gnus-tmp-gathered
4577                     (nconc (mapcar
4578                             (lambda (h) (mail-header-number (car h)))
4579                             (cddar thread))
4580                            gnus-tmp-gathered))
4581               (setq gnus-tmp-level -1))
4582              ((eq gnus-summary-make-false-root 'dummy)
4583               ;; We remember that we probably want to output a dummy
4584               ;; root.
4585               (setq gnus-tmp-dummy-line gnus-tmp-header)
4586               (setq gnus-tmp-prev-subject gnus-tmp-header))
4587              (t
4588               ;; We do not make a root for the gathered
4589               ;; sub-threads at all.
4590               (setq gnus-tmp-level -1)))
4591
4592           (setq number (mail-header-number gnus-tmp-header)
4593                 subject (mail-header-subject gnus-tmp-header)
4594                 simp-subject (gnus-simplify-subject-fully subject))
4595
4596           (cond
4597            ;; If the thread has changed subject, we might want to make
4598            ;; this subthread into a root.
4599            ((and (null gnus-thread-ignore-subject)
4600                  (not (zerop gnus-tmp-level))
4601                  gnus-tmp-prev-subject
4602                  (not (string= gnus-tmp-prev-subject simp-subject)))
4603             (setq new-roots (nconc new-roots (list (car thread)))
4604                   thread-end t
4605                   gnus-tmp-header nil))
4606            ;; If the article lies outside the current limit,
4607            ;; then we do not display it.
4608            ((not (memq number gnus-newsgroup-limit))
4609             (setq gnus-tmp-gathered
4610                   (nconc (mapcar
4611                           (lambda (h) (mail-header-number (car h)))
4612                           (cdar thread))
4613                          gnus-tmp-gathered))
4614             (setq gnus-tmp-new-adopts (if (cdar thread)
4615                                           (append gnus-tmp-new-adopts
4616                                                   (cdar thread))
4617                                         gnus-tmp-new-adopts)
4618                   thread-end t
4619                   gnus-tmp-header nil)
4620             (when (zerop gnus-tmp-level)
4621               (setq gnus-tmp-root-expunged t)))
4622            ;; Perhaps this article is to be marked as read?
4623            ((and gnus-summary-mark-below
4624                  (< (or (cdr (assq number gnus-newsgroup-scored))
4625                         default-score)
4626                     gnus-summary-mark-below)
4627                  ;; Don't touch sparse articles.
4628                  (not (gnus-summary-article-sparse-p number))
4629                  (not (gnus-summary-article-ancient-p number)))
4630             (setq gnus-newsgroup-unreads
4631                   (delq number gnus-newsgroup-unreads))
4632             (if gnus-newsgroup-auto-expire
4633                 (setq gnus-newsgroup-expirable
4634                       (gnus-add-to-sorted-list
4635                        gnus-newsgroup-expirable number))
4636               (push (cons number gnus-low-score-mark)
4637                     gnus-newsgroup-reads))))
4638
4639           (when gnus-tmp-header
4640             ;; We may have an old dummy line to output before this
4641             ;; article.
4642             (when (and gnus-tmp-dummy-line
4643                        (gnus-subject-equal
4644                         gnus-tmp-dummy-line
4645                         (mail-header-subject gnus-tmp-header)))
4646               (gnus-summary-insert-dummy-line
4647                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4648               (setq gnus-tmp-dummy-line nil))
4649
4650             ;; Compute the mark.
4651             (setq gnus-tmp-unread (gnus-article-mark number))
4652
4653             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4654                                   gnus-tmp-header gnus-tmp-level)
4655                   gnus-newsgroup-data)
4656
4657             ;; Actually insert the line.
4658             (setq
4659              gnus-tmp-subject-or-nil
4660              (cond
4661               ((and gnus-thread-ignore-subject
4662                     gnus-tmp-prev-subject
4663                     (not (string= gnus-tmp-prev-subject simp-subject)))
4664                subject)
4665               ((zerop gnus-tmp-level)
4666                (if (and (eq gnus-summary-make-false-root 'empty)
4667                         (memq number gnus-tmp-gathered)
4668                         gnus-tmp-prev-subject
4669                         (string= gnus-tmp-prev-subject simp-subject))
4670                    gnus-summary-same-subject
4671                  subject))
4672               (t gnus-summary-same-subject)))
4673             (if (and (eq gnus-summary-make-false-root 'adopt)
4674                      (= gnus-tmp-level 1)
4675                      (memq number gnus-tmp-gathered))
4676                 (setq gnus-tmp-opening-bracket ?\<
4677                       gnus-tmp-closing-bracket ?\>)
4678               (setq gnus-tmp-opening-bracket ?\[
4679                     gnus-tmp-closing-bracket ?\]))
4680             (setq
4681              gnus-tmp-indentation
4682              (aref gnus-thread-indent-array gnus-tmp-level)
4683              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4684              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4685                                 gnus-summary-default-score 0)
4686              gnus-tmp-score-char
4687              (if (or (null gnus-summary-default-score)
4688                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4689                          gnus-summary-zcore-fuzz))
4690                  ?                      ;Whitespace
4691                (if (< gnus-tmp-score gnus-summary-default-score)
4692                    gnus-score-below-mark gnus-score-over-mark))
4693              gnus-tmp-replied
4694              (cond ((memq number gnus-newsgroup-processable)
4695                     gnus-process-mark)
4696                    ((memq number gnus-newsgroup-cached)
4697                     gnus-cached-mark)
4698                    ((memq number gnus-newsgroup-replied)
4699                     gnus-replied-mark)
4700                    ((memq number gnus-newsgroup-forwarded)
4701                     gnus-forwarded-mark)
4702                    ((memq number gnus-newsgroup-saved)
4703                     gnus-saved-mark)
4704                    ((memq number gnus-newsgroup-recent)
4705                     gnus-recent-mark)
4706                    ((memq number gnus-newsgroup-unseen)
4707                     gnus-unseen-mark)
4708                    (t gnus-no-mark))
4709              gnus-tmp-downloaded
4710              (cond ((memq number gnus-newsgroup-undownloaded) 
4711                     gnus-undownloaded-mark)
4712                    (gnus-newsgroup-agentized
4713                     gnus-downloaded-mark)
4714                    (t
4715                     gnus-no-mark))
4716              gnus-tmp-from (mail-header-from gnus-tmp-header)
4717              gnus-tmp-name
4718              (cond
4719               ((string-match "<[^>]+> *$" gnus-tmp-from)
4720                (setq beg-match (match-beginning 0))
4721                (or (and (string-match "^\".+\"" gnus-tmp-from)
4722                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4723                    (substring gnus-tmp-from 0 beg-match)))
4724               ((string-match "(.+)" gnus-tmp-from)
4725                (substring gnus-tmp-from
4726                           (1+ (match-beginning 0)) (1- (match-end 0))))
4727               (t gnus-tmp-from))
4728              gnus-tmp-thread-tree-header-string
4729              (cond
4730               ((not gnus-show-threads) "")
4731               ((zerop gnus-tmp-level)
4732                (if (cdar thread)
4733                    (or gnus-sum-thread-tree-root subject)
4734                  (or gnus-sum-thread-tree-single-indent subject)))
4735               (t
4736                (concat (apply 'concat
4737                               (mapcar (lambda (item)
4738                                         (if (= item 1)
4739                                             gnus-sum-thread-tree-vertical
4740                                           gnus-sum-thread-tree-indent))
4741                                       (cdr (reverse tree-stack))))
4742                        (if (nth 1 thread)
4743                            gnus-sum-thread-tree-leaf-with-other
4744                          gnus-sum-thread-tree-single-leaf)))))
4745             (when (string= gnus-tmp-name "")
4746               (setq gnus-tmp-name gnus-tmp-from))
4747             (unless (numberp gnus-tmp-lines)
4748               (setq gnus-tmp-lines -1))
4749             (if (= gnus-tmp-lines -1)
4750                 (setq gnus-tmp-lines "?")
4751               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4752             (gnus-put-text-property
4753              (point)
4754              (progn (eval gnus-summary-line-format-spec) (point))
4755              'gnus-number number)
4756             (when gnus-visual-p
4757               (forward-line -1)
4758               (gnus-run-hooks 'gnus-summary-update-hook)
4759               (forward-line 1))
4760
4761             (setq gnus-tmp-prev-subject simp-subject)))
4762
4763         (when (nth 1 thread)
4764           (push (list (max 0 gnus-tmp-level)
4765                       (copy-list tree-stack)
4766                       (nthcdr 1 thread))
4767                 stack))
4768         (push (if (nth 1 thread) 1 0) tree-stack)
4769         (incf gnus-tmp-level)
4770         (setq threads (if thread-end nil (cdar thread)))
4771         (if gnus-summary-display-while-building
4772             (if building-count
4773                 (progn
4774                   ;; use a set frequency
4775                   (setq building-line-count (1- building-line-count))
4776                   (when (= building-line-count 0)
4777                     (sit-for 0)
4778                     (setq building-line-count
4779                           gnus-summary-display-while-building)))
4780               ;; always
4781               (sit-for 0)))
4782         (unless threads
4783           (setq gnus-tmp-level 0)))))
4784   (gnus-message 7 "Generating summary...done"))
4785
4786 (defun gnus-summary-prepare-unthreaded (headers)
4787   "Generate an unthreaded summary buffer based on HEADERS."
4788   (let (header number mark)
4789
4790     (beginning-of-line)
4791
4792     (while headers
4793       ;; We may have to root out some bad articles...
4794       (when (memq (setq number (mail-header-number
4795                                 (setq header (pop headers))))
4796                   gnus-newsgroup-limit)
4797         ;; Mark article as read when it has a low score.
4798         (when (and gnus-summary-mark-below
4799                    (< (or (cdr (assq number gnus-newsgroup-scored))
4800                           gnus-summary-default-score 0)
4801                       gnus-summary-mark-below)
4802                    (not (gnus-summary-article-ancient-p number)))
4803           (setq gnus-newsgroup-unreads
4804                 (delq number gnus-newsgroup-unreads))
4805           (if gnus-newsgroup-auto-expire
4806               (push number gnus-newsgroup-expirable)
4807             (push (cons number gnus-low-score-mark)
4808                   gnus-newsgroup-reads)))
4809
4810         (setq mark (gnus-article-mark number))
4811         (push (gnus-data-make number mark (1+ (point)) header 0)
4812               gnus-newsgroup-data)
4813         (gnus-summary-insert-line
4814          header 0 number
4815          (memq number gnus-newsgroup-undownloaded)
4816          mark (memq number gnus-newsgroup-replied)
4817          (memq number gnus-newsgroup-expirable)
4818          (mail-header-subject header) nil
4819          (cdr (assq number gnus-newsgroup-scored))
4820          (memq number gnus-newsgroup-processable))))))
4821
4822 (defun gnus-summary-remove-list-identifiers ()
4823   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4824   (let ((regexp (if (consp gnus-list-identifiers)
4825                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4826                   gnus-list-identifiers))
4827         changed subject)
4828     (when regexp
4829       (dolist (header gnus-newsgroup-headers)
4830         (setq subject (mail-header-subject header)
4831               changed nil)
4832         (while (string-match
4833                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4834                 subject)
4835           (setq subject
4836                 (concat (substring subject 0 (match-beginning 2))
4837                         (substring subject (match-end 0)))
4838                 changed t))
4839         (when (and changed
4840                    (string-match
4841                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4842           (setq subject
4843                 (concat (substring subject 0 (match-beginning 1))
4844                         (substring subject (match-end 1)))))
4845         (when changed
4846           (mail-header-set-subject header subject))))))
4847
4848 (defun gnus-fetch-headers (articles)
4849   "Fetch headers of ARTICLES."
4850   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4851     (gnus-message 5 "Fetching headers for %s..." name)
4852     (prog1
4853         (if (eq 'nov
4854                 (setq gnus-headers-retrieved-by
4855                       (gnus-retrieve-headers
4856                        articles gnus-newsgroup-name
4857                        ;; We might want to fetch old headers, but
4858                        ;; not if there is only 1 article.
4859                        (and (or (and
4860                                  (not (eq gnus-fetch-old-headers 'some))
4861                                  (not (numberp gnus-fetch-old-headers)))
4862                                 (> (length articles) 1))
4863                             gnus-fetch-old-headers))))
4864             (gnus-get-newsgroup-headers-xover
4865              articles nil nil gnus-newsgroup-name t)
4866           (gnus-get-newsgroup-headers))
4867       (gnus-message 5 "Fetching headers for %s...done" name))))
4868
4869 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4870   "Select newsgroup GROUP.
4871 If READ-ALL is non-nil, all articles in the group are selected.
4872 If SELECT-ARTICLES, only select those articles from GROUP."
4873   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4874          ;;!!! Dirty hack; should be removed.
4875          (gnus-summary-ignore-duplicates
4876           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4877               t
4878             gnus-summary-ignore-duplicates))
4879          (info (nth 2 entry))
4880          articles fetched-articles cached)
4881
4882     (unless (gnus-check-server
4883              (set (make-local-variable 'gnus-current-select-method)
4884                   (gnus-find-method-for-group group)))
4885       (error "Couldn't open server"))
4886
4887     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4888         (gnus-activate-group group)     ; Or we can activate it...
4889         (progn                          ; Or we bug out.
4890           (when (equal major-mode 'gnus-summary-mode)
4891             (kill-buffer (current-buffer)))
4892           (error "Couldn't activate group %s: %s"
4893                  group (gnus-status-message group))))
4894
4895     (unless (gnus-request-group group t)
4896       (when (equal major-mode 'gnus-summary-mode)
4897         (kill-buffer (current-buffer)))
4898       (error "Couldn't request group %s: %s"
4899              group (gnus-status-message group)))
4900
4901     (setq gnus-newsgroup-name group
4902           gnus-newsgroup-unselected nil
4903           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4904
4905     (let ((display (gnus-group-find-parameter group 'display)))
4906       (setq gnus-newsgroup-display
4907             (cond
4908              ((not (zerop (or (car-safe read-all) 0)))
4909               ;; The user entered the group with C-u SPC/RET, let's show
4910               ;; all articles.
4911               'gnus-not-ignore)
4912              ((eq display 'all)
4913               'gnus-not-ignore)
4914              ((arrayp display)
4915               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4916              ((numberp display)
4917               ;; The following is probably the "correct" solution, but
4918               ;; it makes Gnus fetch all headers and then limit the
4919               ;; articles (which is slow), so instead we hack the
4920               ;; select-articles parameter instead. -- Simon Josefsson
4921               ;; <jas@kth.se>
4922               ;;
4923               ;; (gnus-byte-compile
4924               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4925               ;;                         display)))))
4926               (setq select-articles
4927                     (gnus-uncompress-range
4928                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4929                              (if (> tmp 0)
4930                                  tmp
4931                                1))
4932                            (cdr (gnus-active group)))))
4933               nil)
4934              (t
4935               nil))))
4936
4937     (gnus-summary-setup-default-charset)
4938
4939     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4940     (when (gnus-virtual-group-p group)
4941       (setq cached gnus-newsgroup-cached))
4942
4943     (setq gnus-newsgroup-unreads
4944           (gnus-sorted-ndifference
4945            (gnus-sorted-ndifference gnus-newsgroup-unreads
4946                                     gnus-newsgroup-marked)
4947            gnus-newsgroup-dormant))
4948
4949     (setq gnus-newsgroup-processable nil)
4950
4951     (gnus-update-read-articles group gnus-newsgroup-unreads)
4952
4953     ;; Adjust and set lists of article marks.
4954     (when info
4955       (gnus-adjust-marked-articles info))
4956     (if (setq articles select-articles)
4957         (setq gnus-newsgroup-unselected
4958               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4959       (setq articles (gnus-articles-to-read group read-all)))
4960
4961     (cond
4962      ((null articles)
4963       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4964       'quit)
4965      ((eq articles 0) nil)
4966      (t
4967       ;; Init the dependencies hash table.
4968       (setq gnus-newsgroup-dependencies
4969             (gnus-make-hashtable (length articles)))
4970       (gnus-set-global-variables)
4971       ;; Retrieve the headers and read them in.
4972
4973       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4974
4975       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4976       (when cached
4977         (setq gnus-newsgroup-cached cached))
4978
4979       ;; Suppress duplicates?
4980       (when gnus-suppress-duplicates
4981         (gnus-dup-suppress-articles))
4982
4983       ;; Set the initial limit.
4984       (setq gnus-newsgroup-limit (copy-sequence articles))
4985       ;; Remove canceled articles from the list of unread articles.
4986       (setq fetched-articles
4987             (mapcar (lambda (headers) (mail-header-number headers))
4988                     gnus-newsgroup-headers))
4989       (setq gnus-newsgroup-articles fetched-articles)
4990       (setq gnus-newsgroup-unreads
4991             (gnus-sorted-nintersection
4992              gnus-newsgroup-unreads fetched-articles))
4993       (gnus-compute-unseen-list)
4994
4995       ;; Removed marked articles that do not exist.
4996       (gnus-update-missing-marks
4997        (gnus-sorted-difference articles fetched-articles))
4998       ;; We might want to build some more threads first.
4999       (when (and gnus-fetch-old-headers
5000                  (eq gnus-headers-retrieved-by 'nov))
5001         (if (eq gnus-fetch-old-headers 'invisible)
5002             (gnus-build-all-threads)
5003           (gnus-build-old-threads)))
5004       ;; Let the Gnus agent mark articles as read.
5005       (when gnus-agent
5006         (gnus-agent-get-undownloaded-list))
5007       ;; Remove list identifiers from subject
5008       (when gnus-list-identifiers
5009         (gnus-summary-remove-list-identifiers))
5010       ;; Check whether auto-expire is to be done in this group.
5011       (setq gnus-newsgroup-auto-expire
5012             (gnus-group-auto-expirable-p group))
5013       ;; Set up the article buffer now, if necessary.
5014       (unless gnus-single-article-buffer
5015         (gnus-article-setup-buffer))
5016       ;; First and last article in this newsgroup.
5017       (when gnus-newsgroup-headers
5018         (setq gnus-newsgroup-begin
5019               (mail-header-number (car gnus-newsgroup-headers))
5020               gnus-newsgroup-end
5021               (mail-header-number
5022                (gnus-last-element gnus-newsgroup-headers))))
5023       ;; GROUP is successfully selected.
5024       (or gnus-newsgroup-headers t)))))
5025
5026 (defun gnus-compute-unseen-list ()
5027   ;; The `seen' marks are treated specially.
5028   (if (not gnus-newsgroup-seen)
5029       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5030     (setq gnus-newsgroup-unseen
5031           (gnus-inverse-list-range-intersection
5032            gnus-newsgroup-articles gnus-newsgroup-seen))))
5033
5034 (defun gnus-summary-display-make-predicate (display)
5035   (require 'gnus-agent)
5036   (when (= (length display) 1)
5037     (setq display (car display)))
5038   (unless gnus-summary-display-cache
5039     (dolist (elem (append '((unread . unread)
5040                             (read . read)
5041                             (unseen . unseen))
5042                           gnus-article-mark-lists))
5043       (push (cons (cdr elem)
5044                   (gnus-byte-compile
5045                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5046             gnus-summary-display-cache)))
5047   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5048         (gnus-category-predicate-cache gnus-summary-display-cache))
5049     (gnus-get-predicate display)))
5050
5051 ;; Uses the dynamically bound `number' variable.
5052 (eval-when-compile
5053   (defvar number))
5054 (defun gnus-article-marked-p (type &optional article)
5055   (let ((article (or article number)))
5056     (cond
5057      ((eq type 'tick)
5058       (memq article gnus-newsgroup-marked))
5059      ((eq type 'spam)
5060       (memq article gnus-newsgroup-spam-marked))
5061      ((eq type 'unsend)
5062       (memq article gnus-newsgroup-unsendable))
5063      ((eq type 'undownload)
5064       (memq article gnus-newsgroup-undownloaded))
5065      ((eq type 'download)
5066       (memq article gnus-newsgroup-downloadable))
5067      ((eq type 'unread)
5068       (memq article gnus-newsgroup-unreads))
5069      ((eq type 'read)
5070       (memq article gnus-newsgroup-reads))
5071      ((eq type 'dormant)
5072       (memq article gnus-newsgroup-dormant) )
5073      ((eq type 'expire)
5074       (memq article gnus-newsgroup-expirable))
5075      ((eq type 'reply)
5076       (memq article gnus-newsgroup-replied))
5077      ((eq type 'killed)
5078       (memq article gnus-newsgroup-killed))
5079      ((eq type 'bookmark)
5080       (assq article gnus-newsgroup-bookmarks))
5081      ((eq type 'score)
5082       (assq article gnus-newsgroup-scored))
5083      ((eq type 'save)
5084       (memq article gnus-newsgroup-saved))
5085      ((eq type 'cache)
5086       (memq article gnus-newsgroup-cached))
5087      ((eq type 'forward)
5088       (memq article gnus-newsgroup-forwarded))
5089      ((eq type 'seen)
5090       (not (memq article gnus-newsgroup-unseen)))
5091      ((eq type 'recent)
5092       (memq article gnus-newsgroup-recent))
5093      (t t))))
5094
5095 (defun gnus-articles-to-read (group &optional read-all)
5096   "Find out what articles the user wants to read."
5097   (let* ((display (gnus-group-find-parameter group 'display))
5098          (articles
5099           ;; Select all articles if `read-all' is non-nil, or if there
5100           ;; are no unread articles.
5101           (if (or read-all
5102                   (and (zerop (length gnus-newsgroup-marked))
5103                        (zerop (length gnus-newsgroup-unreads)))
5104                   ;; Fetch all if the predicate is non-nil.
5105                   gnus-newsgroup-display)
5106               ;; We want to select the headers for all the articles in
5107               ;; the group, so we select either all the active
5108               ;; articles in the group, or (if that's nil), the
5109               ;; articles in the cache.
5110               (or
5111                (gnus-uncompress-range (gnus-active group))
5112                (gnus-cache-articles-in-group group))
5113             ;; Select only the "normal" subset of articles.
5114             (gnus-sorted-nunion
5115              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5116              gnus-newsgroup-unreads)))
5117          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5118          (scored (length scored-list))
5119          (number (length articles))
5120          (marked (+ (length gnus-newsgroup-marked)
5121                     (length gnus-newsgroup-dormant)))
5122          (select
5123           (cond
5124            ((numberp read-all)
5125             read-all)
5126            ((numberp gnus-newsgroup-display)
5127             gnus-newsgroup-display)
5128            (t
5129             (condition-case ()
5130                 (cond
5131                  ((and (or (<= scored marked) (= scored number))
5132                        (numberp gnus-large-newsgroup)
5133                        (> number gnus-large-newsgroup))
5134                   (let* ((cursor-in-echo-area nil)
5135                          (initial (gnus-parameter-large-newsgroup-initial
5136                                    gnus-newsgroup-name))
5137                          (input
5138                           (read-string
5139                            (format
5140                             "How many articles from %s (%s %d): "
5141                             (gnus-limit-string
5142                              (gnus-group-decoded-name gnus-newsgroup-name)
5143                              35)
5144                             (if initial "max" "default")
5145                             number)
5146                            (if initial
5147                                (cons (number-to-string initial)
5148                                      0)))))
5149                     (if (string-match "^[ \t]*$" input) number input)))
5150                  ((and (> scored marked) (< scored number)
5151                        (> (- scored number) 20))
5152                   (let ((input
5153                          (read-string
5154                           (format "%s %s (%d scored, %d total): "
5155                                   "How many articles from"
5156                                   (gnus-group-decoded-name group)
5157                                   scored number))))
5158                     (if (string-match "^[ \t]*$" input)
5159                         number input)))
5160                  (t number))
5161               (quit
5162                (message "Quit getting the articles to read")
5163                nil))))))
5164     (setq select (if (stringp select) (string-to-number select) select))
5165     (if (or (null select) (zerop select))
5166         select
5167       (if (and (not (zerop scored)) (<= (abs select) scored))
5168           (progn
5169             (setq articles (sort scored-list '<))
5170             (setq number (length articles)))
5171         (setq articles (copy-sequence articles)))
5172
5173       (when (< (abs select) number)
5174         (if (< select 0)
5175             ;; Select the N oldest articles.
5176             (setcdr (nthcdr (1- (abs select)) articles) nil)
5177           ;; Select the N most recent articles.
5178           (setq articles (nthcdr (- number select) articles))))
5179       (setq gnus-newsgroup-unselected
5180             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5181       (when gnus-alter-articles-to-read-function
5182         (setq gnus-newsgroup-unreads
5183               (sort
5184                (funcall gnus-alter-articles-to-read-function
5185                         gnus-newsgroup-name gnus-newsgroup-unreads)
5186                '<)))
5187       articles)))
5188
5189 (defun gnus-killed-articles (killed articles)
5190   (let (out)
5191     (while articles
5192       (when (inline (gnus-member-of-range (car articles) killed))
5193         (push (car articles) out))
5194       (setq articles (cdr articles)))
5195     out))
5196
5197 (defun gnus-uncompress-marks (marks)
5198   "Uncompress the mark ranges in MARKS."
5199   (let ((uncompressed '(score bookmark))
5200         out)
5201     (while marks
5202       (if (memq (caar marks) uncompressed)
5203           (push (car marks) out)
5204         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5205       (setq marks (cdr marks)))
5206     out))
5207
5208 (defun gnus-article-mark-to-type (mark)
5209   "Return the type of MARK."
5210   (or (cadr (assq mark gnus-article-special-mark-lists))
5211       'list))
5212
5213 (defun gnus-article-unpropagatable-p (mark)
5214   "Return whether MARK should be propagated to backend."
5215   (memq mark gnus-article-unpropagated-mark-lists))
5216
5217 (defun gnus-adjust-marked-articles (info)
5218   "Set all article lists and remove all marks that are no longer valid."
5219   (let* ((marked-lists (gnus-info-marks info))
5220          (active (gnus-active (gnus-info-group info)))
5221          (min (car active))
5222          (max (cdr active))
5223          (types gnus-article-mark-lists)
5224          marks var articles article mark mark-type)
5225
5226     (dolist (marks marked-lists)
5227       (setq mark (car marks)
5228             mark-type (gnus-article-mark-to-type mark)
5229             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5230
5231       ;; We set the variable according to the type of the marks list,
5232       ;; and then adjust the marks to a subset of the active articles.
5233       (cond
5234        ;; Adjust "simple" lists.
5235        ((eq mark-type 'list)
5236         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5237         (when (memq mark '(tick dormant expire reply save))
5238           (while articles
5239             (when (or (< (setq article (pop articles)) min) (> article max))
5240               (set var (delq article (symbol-value var)))))))
5241        ;; Adjust assocs.
5242        ((eq mark-type 'tuple)
5243         (set var (setq articles (cdr marks)))
5244         (when (not (listp (cdr (symbol-value var))))
5245           (set var (list (symbol-value var))))
5246         (when (not (listp (cdr articles)))
5247           (setq articles (list articles)))
5248         (while articles
5249           (when (or (not (consp (setq article (pop articles))))
5250                     (< (car article) min)
5251                     (> (car article) max))
5252             (set var (delq article (symbol-value var))))))
5253        ;; Adjust ranges (sloppily).
5254        ((eq mark-type 'range)
5255         (cond
5256          ((eq mark 'seen)
5257           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5258           ;; It should be (seen (NUM1 . NUM2)).
5259           (when (numberp (cddr marks))
5260             (setcdr marks (list (cdr marks))))
5261           (setq articles (cdr marks))
5262           (while (and articles
5263                       (or (and (consp (car articles))
5264                                (> min (cdar articles)))
5265                           (and (numberp (car articles))
5266                                (> min (car articles)))))
5267             (pop articles))
5268           (set var articles))))))))
5269
5270 (defun gnus-update-missing-marks (missing)
5271   "Go through the list of MISSING articles and remove them from the mark lists."
5272   (when missing
5273     (let (var m)
5274       ;; Go through all types.
5275       (dolist (elem gnus-article-mark-lists)
5276         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5277           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5278           (when (symbol-value var)
5279             ;; This list has articles.  So we delete all missing
5280             ;; articles from it.
5281             (setq m missing)
5282             (while m
5283               (set var (delq (pop m) (symbol-value var))))))))))
5284
5285 (defun gnus-update-marks ()
5286   "Enter the various lists of marked articles into the newsgroup info list."
5287   (let ((types gnus-article-mark-lists)
5288         (info (gnus-get-info gnus-newsgroup-name))
5289         type list newmarked symbol delta-marks)
5290     (when info
5291       ;; Add all marks lists to the list of marks lists.
5292       (while (setq type (pop types))
5293         (setq list (symbol-value
5294                     (setq symbol
5295                           (intern (format "gnus-newsgroup-%s" (car type))))))
5296
5297         (when list
5298           ;; Get rid of the entries of the articles that have the
5299           ;; default score.
5300           (when (and (eq (cdr type) 'score)
5301                      gnus-save-score
5302                      list)
5303             (let* ((arts list)
5304                    (prev (cons nil list))
5305                    (all prev))
5306               (while arts
5307                 (if (or (not (consp (car arts)))
5308                         (= (cdar arts) gnus-summary-default-score))
5309                     (setcdr prev (cdr arts))
5310                   (setq prev arts))
5311                 (setq arts (cdr arts)))
5312               (setq list (cdr all)))))
5313
5314         (when (eq (cdr type) 'seen)
5315           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5316
5317         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5318           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5319
5320         (when (and (gnus-check-backend-function
5321                     'request-set-mark gnus-newsgroup-name)
5322                    (not (gnus-article-unpropagatable-p (cdr type))))
5323           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5324                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5325                  (add (gnus-remove-from-range
5326                        (gnus-copy-sequence list) old)))
5327             (when add
5328               (push (list add 'add (list (cdr type))) delta-marks))
5329             (when del
5330               (push (list del 'del (list (cdr type))) delta-marks))))
5331
5332         (when list
5333           (push (cons (cdr type) list) newmarked)))
5334
5335       (when delta-marks
5336         (unless (gnus-check-group gnus-newsgroup-name)
5337           (error "Can't open server for %s" gnus-newsgroup-name))
5338         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5339
5340       ;; Enter these new marks into the info of the group.
5341       (if (nthcdr 3 info)
5342           (setcar (nthcdr 3 info) newmarked)
5343         ;; Add the marks lists to the end of the info.
5344         (when newmarked
5345           (setcdr (nthcdr 2 info) (list newmarked))))
5346
5347       ;; Cut off the end of the info if there's nothing else there.
5348       (let ((i 5))
5349         (while (and (> i 2)
5350                     (not (nth i info)))
5351           (when (nthcdr (decf i) info)
5352             (setcdr (nthcdr i info) nil)))))))
5353
5354 (defun gnus-set-mode-line (where)
5355   "Set the mode line of the article or summary buffers.
5356 If WHERE is `summary', the summary mode line format will be used."
5357   ;; Is this mode line one we keep updated?
5358   (when (and (memq where gnus-updated-mode-lines)
5359              (symbol-value
5360               (intern (format "gnus-%s-mode-line-format-spec" where))))
5361     (let (mode-string)
5362       (save-excursion
5363         ;; We evaluate this in the summary buffer since these
5364         ;; variables are buffer-local to that buffer.
5365         (set-buffer gnus-summary-buffer)
5366        ;; We bind all these variables that are used in the `eval' form
5367         ;; below.
5368         (let* ((mformat (symbol-value
5369                          (intern
5370                           (format "gnus-%s-mode-line-format-spec" where))))
5371                (gnus-tmp-group-name (gnus-group-decoded-name
5372                                      gnus-newsgroup-name))
5373                (gnus-tmp-article-number (or gnus-current-article 0))
5374                (gnus-tmp-unread gnus-newsgroup-unreads)
5375                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5376                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5377                (gnus-tmp-unread-and-unselected
5378                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5379                             (zerop gnus-tmp-unselected))
5380                        "")
5381                       ((zerop gnus-tmp-unselected)
5382                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5383                       (t (format "{%d(+%d) more}"
5384                                  gnus-tmp-unread-and-unticked
5385                                  gnus-tmp-unselected))))
5386                (gnus-tmp-subject
5387                 (if (and gnus-current-headers
5388                          (vectorp gnus-current-headers))
5389                     (gnus-mode-string-quote
5390                      (mail-header-subject gnus-current-headers))
5391                   ""))
5392                bufname-length max-len
5393                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5394           (setq mode-string (eval mformat))
5395           (setq bufname-length (if (string-match "%b" mode-string)
5396                                    (- (length
5397                                        (buffer-name
5398                                         (if (eq where 'summary)
5399                                             nil
5400                                           (get-buffer gnus-article-buffer))))
5401                                       2)
5402                                  0))
5403           (setq max-len (max 4 (if gnus-mode-non-string-length
5404                                    (- (window-width)
5405                                       gnus-mode-non-string-length
5406                                       bufname-length)
5407                                  (length mode-string))))
5408           ;; We might have to chop a bit of the string off...
5409           (when (> (length mode-string) max-len)
5410             (setq mode-string
5411                   (concat (truncate-string-to-width mode-string (- max-len 3))
5412                           "...")))
5413           ;; Pad the mode string a bit.
5414           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5415       ;; Update the mode line.
5416       (setq mode-line-buffer-identification
5417             (gnus-mode-line-buffer-identification (list mode-string)))
5418       (set-buffer-modified-p t))))
5419
5420 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5421   "Go through the HEADERS list and add all Xrefs to a hash table.
5422 The resulting hash table is returned, or nil if no Xrefs were found."
5423   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5424          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5425          (xref-hashtb (gnus-make-hashtable))
5426          start group entry number xrefs header)
5427     (while headers
5428       (setq header (pop headers))
5429       (when (and (setq xrefs (mail-header-xref header))
5430                  (not (memq (setq number (mail-header-number header))
5431                             unreads)))
5432         (setq start 0)
5433         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5434           (setq start (match-end 0))
5435           (setq group (if prefix
5436                           (concat prefix (substring xrefs (match-beginning 1)
5437                                                     (match-end 1)))
5438                         (substring xrefs (match-beginning 1) (match-end 1))))
5439           (setq number
5440                 (string-to-int (substring xrefs (match-beginning 2)
5441                                           (match-end 2))))
5442           (if (setq entry (gnus-gethash group xref-hashtb))
5443               (setcdr entry (cons number (cdr entry)))
5444             (gnus-sethash group (cons number nil) xref-hashtb)))))
5445     (and start xref-hashtb)))
5446
5447 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5448   "Look through all the headers and mark the Xrefs as read."
5449   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5450         name entry info xref-hashtb idlist method nth4)
5451     (save-excursion
5452       (set-buffer gnus-group-buffer)
5453       (when (setq xref-hashtb
5454                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5455         (mapatoms
5456          (lambda (group)
5457            (unless (string= from-newsgroup (setq name (symbol-name group)))
5458              (setq idlist (symbol-value group))
5459              ;; Dead groups are not updated.
5460              (and (prog1
5461                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5462                             info (nth 2 entry))
5463                     (when (stringp (setq nth4 (gnus-info-method info)))
5464                       (setq nth4 (gnus-server-to-method nth4))))
5465                   ;; Only do the xrefs if the group has the same
5466                   ;; select method as the group we have just read.
5467                   (or (gnus-methods-equal-p
5468                        nth4 (gnus-find-method-for-group from-newsgroup))
5469                       virtual
5470                       (equal nth4 (setq method (gnus-find-method-for-group
5471                                                 from-newsgroup)))
5472                       (and (equal (car nth4) (car method))
5473                            (equal (nth 1 nth4) (nth 1 method))))
5474                   gnus-use-cross-reference
5475                   (or (not (eq gnus-use-cross-reference t))
5476                       virtual
5477                       ;; Only do cross-references on subscribed
5478                       ;; groups, if that is what is wanted.
5479                       (<= (gnus-info-level info) gnus-level-subscribed))
5480                   (gnus-group-make-articles-read name idlist))))
5481          xref-hashtb)))))
5482
5483 (defun gnus-compute-read-articles (group articles)
5484   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5485          (info (nth 2 entry))
5486          (active (gnus-active group))
5487          ninfo)
5488     (when entry
5489       ;; First peel off all invalid article numbers.
5490       (when active
5491         (let ((ids articles)
5492               id first)
5493           (while (setq id (pop ids))
5494             (when (and first (> id (cdr active)))
5495               ;; We'll end up in this situation in one particular
5496               ;; obscure situation.  If you re-scan a group and get
5497               ;; a new article that is cross-posted to a different
5498               ;; group that has not been re-scanned, you might get
5499               ;; crossposted article that has a higher number than
5500               ;; Gnus believes possible.  So we re-activate this
5501               ;; group as well.  This might mean doing the
5502               ;; crossposting thingy will *increase* the number
5503               ;; of articles in some groups.  Tsk, tsk.
5504               (setq active (or (gnus-activate-group group) active)))
5505             (when (or (> id (cdr active))
5506                       (< id (car active)))
5507               (setq articles (delq id articles))))))
5508       ;; If the read list is nil, we init it.
5509       (if (and active
5510                (null (gnus-info-read info))
5511                (> (car active) 1))
5512           (setq ninfo (cons 1 (1- (car active))))
5513         (setq ninfo (gnus-info-read info)))
5514       ;; Then we add the read articles to the range.
5515       (gnus-add-to-range
5516        ninfo (setq articles (sort articles '<))))))
5517
5518 (defun gnus-group-make-articles-read (group articles)
5519   "Update the info of GROUP to say that ARTICLES are read."
5520   (let* ((num 0)
5521          (entry (gnus-gethash group gnus-newsrc-hashtb))
5522          (info (nth 2 entry))
5523          (active (gnus-active group))
5524          range)
5525     (when entry
5526       (setq range (gnus-compute-read-articles group articles))
5527       (save-excursion
5528         (set-buffer gnus-group-buffer)
5529         (gnus-undo-register
5530           `(progn
5531              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5532              (gnus-info-set-read ',info ',(gnus-info-read info))
5533              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5534              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5535              (gnus-group-update-group ,group t))))
5536       ;; Add the read articles to the range.
5537       (gnus-info-set-read info range)
5538       (gnus-request-set-mark group (list (list range 'add '(read))))
5539       ;; Then we have to re-compute how many unread
5540       ;; articles there are in this group.
5541       (when active
5542         (cond
5543          ((not range)
5544           (setq num (- (1+ (cdr active)) (car active))))
5545          ((not (listp (cdr range)))
5546           (setq num (- (cdr active) (- (1+ (cdr range))
5547                                        (car range)))))
5548          (t
5549           (while range
5550             (if (numberp (car range))
5551                 (setq num (1+ num))
5552               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5553             (setq range (cdr range)))
5554           (setq num (- (cdr active) num))))
5555         ;; Update the number of unread articles.
5556         (setcar entry num)
5557         ;; Update the group buffer.
5558         (gnus-group-update-group group t)))))
5559
5560 (defvar gnus-newsgroup-none-id 0)
5561
5562 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5563   (let ((cur nntp-server-buffer)
5564         (dependencies
5565          (or dependencies
5566              (save-excursion (set-buffer gnus-summary-buffer)
5567                              gnus-newsgroup-dependencies)))
5568         headers id end ref
5569         (mail-parse-charset gnus-newsgroup-charset)
5570         (mail-parse-ignored-charsets
5571          (save-excursion (condition-case nil
5572                              (set-buffer gnus-summary-buffer)
5573                            (error))
5574                          gnus-newsgroup-ignored-charsets)))
5575     (save-excursion
5576       (set-buffer nntp-server-buffer)
5577       ;; Translate all TAB characters into SPACE characters.
5578       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5579       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5580       (gnus-run-hooks 'gnus-parse-headers-hook)
5581       (let ((case-fold-search t)
5582             in-reply-to header p lines chars)
5583         (goto-char (point-min))
5584         ;; Search to the beginning of the next header.  Error messages
5585         ;; do not begin with 2 or 3.
5586         (while (re-search-forward "^[23][0-9]+ " nil t)
5587           (setq id nil
5588                 ref nil)
5589           ;; This implementation of this function, with nine
5590           ;; search-forwards instead of the one re-search-forward and
5591           ;; a case (which basically was the old function) is actually
5592           ;; about twice as fast, even though it looks messier.  You
5593           ;; can't have everything, I guess.  Speed and elegance
5594           ;; doesn't always go hand in hand.
5595           (setq
5596            header
5597            (vector
5598             ;; Number.
5599             (prog1
5600                 (read cur)
5601               (end-of-line)
5602               (setq p (point))
5603               (narrow-to-region (point)
5604                                 (or (and (search-forward "\n.\n" nil t)
5605                                          (- (point) 2))
5606                                     (point))))
5607             ;; Subject.
5608             (progn
5609               (goto-char p)
5610               (if (search-forward "\nsubject:" nil t)
5611                   (funcall gnus-decode-encoded-word-function
5612                            (nnheader-header-value))
5613                 "(none)"))
5614             ;; From.
5615             (progn
5616               (goto-char p)
5617               (if (search-forward "\nfrom:" nil t)
5618                   (funcall gnus-decode-encoded-word-function
5619                            (nnheader-header-value))
5620                 "(nobody)"))
5621             ;; Date.
5622             (progn
5623               (goto-char p)
5624               (if (search-forward "\ndate:" nil t)
5625                   (nnheader-header-value) ""))
5626             ;; Message-ID.
5627             (progn
5628               (goto-char p)
5629               (setq id (if (re-search-forward
5630                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5631                            ;; We do it this way to make sure the Message-ID
5632                            ;; is (somewhat) syntactically valid.
5633                            (buffer-substring (match-beginning 1)
5634                                              (match-end 1))
5635                          ;; If there was no message-id, we just fake one
5636                          ;; to make subsequent routines simpler.
5637                          (nnheader-generate-fake-message-id))))
5638             ;; References.
5639             (progn
5640               (goto-char p)
5641               (if (search-forward "\nreferences:" nil t)
5642                   (progn
5643                     (setq end (point))
5644                     (prog1
5645                         (nnheader-header-value)
5646                       (setq ref
5647                             (buffer-substring
5648                              (progn
5649                                (end-of-line)
5650                                (search-backward ">" end t)
5651                                (1+ (point)))
5652                              (progn
5653                                (search-backward "<" end t)
5654                                (point))))))
5655                 ;; Get the references from the in-reply-to header if there
5656                 ;; were no references and the in-reply-to header looks
5657                 ;; promising.
5658                 (if (and (search-forward "\nin-reply-to:" nil t)
5659                          (setq in-reply-to (nnheader-header-value))
5660                          (string-match "<[^>]+>" in-reply-to))
5661                     (let (ref2)
5662                       (setq ref (substring in-reply-to (match-beginning 0)
5663                                            (match-end 0)))
5664                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5665                         (setq ref2 (substring in-reply-to (match-beginning 0)
5666                                               (match-end 0)))
5667                         (when (> (length ref2) (length ref))
5668                           (setq ref ref2)))
5669                       ref)
5670                   (setq ref nil))))
5671             ;; Chars.
5672             (progn
5673               (goto-char p)
5674               (if (search-forward "\nchars: " nil t)
5675                   (if (numberp (setq chars (ignore-errors (read cur))))
5676                       chars -1)
5677                 -1))
5678             ;; Lines.
5679             (progn
5680               (goto-char p)
5681               (if (search-forward "\nlines: " nil t)
5682                   (if (numberp (setq lines (ignore-errors (read cur))))
5683                       lines -1)
5684                 -1))
5685             ;; Xref.
5686             (progn
5687               (goto-char p)
5688               (and (search-forward "\nxref:" nil t)
5689                    (nnheader-header-value)))
5690             ;; Extra.
5691             (when gnus-extra-headers
5692               (let ((extra gnus-extra-headers)
5693                     out)
5694                 (while extra
5695                   (goto-char p)
5696                   (when (search-forward
5697                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5698                     (push (cons (car extra) (nnheader-header-value))
5699                           out))
5700                   (pop extra))
5701                 out))))
5702           (when (equal id ref)
5703             (setq ref nil))
5704
5705           (when gnus-alter-header-function
5706             (funcall gnus-alter-header-function header)
5707             (setq id (mail-header-id header)
5708                   ref (gnus-parent-id (mail-header-references header))))
5709
5710           (when (setq header
5711                       (gnus-dependencies-add-header
5712                        header dependencies force-new))
5713             (push header headers))
5714           (goto-char (point-max))
5715           (widen))
5716         (nreverse headers)))))
5717
5718 ;; Goes through the xover lines and returns a list of vectors
5719 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5720                                                   force-new dependencies
5721                                                   group also-fetch-heads)
5722   "Parse the news overview data in the server buffer.
5723 Return a list of headers that match SEQUENCE (see
5724 `nntp-retrieve-headers')."
5725   ;; Get the Xref when the users reads the articles since most/some
5726   ;; NNTP servers do not include Xrefs when using XOVER.
5727   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5728   (let ((mail-parse-charset gnus-newsgroup-charset)
5729         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5730         (cur nntp-server-buffer)
5731         (dependencies (or dependencies gnus-newsgroup-dependencies))
5732         (allp (cond
5733                ((eq gnus-read-all-available-headers t)
5734                 t)
5735                ((stringp gnus-read-all-available-headers)
5736                 (string-match gnus-read-all-available-headers group))
5737                (t
5738                 nil)))
5739         number headers header)
5740     (save-excursion
5741       (set-buffer nntp-server-buffer)
5742       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5743       ;; Allow the user to mangle the headers before parsing them.
5744       (gnus-run-hooks 'gnus-parse-headers-hook)
5745       (goto-char (point-min))
5746       (gnus-parse-without-error
5747         (while (and (or sequence allp)
5748                     (not (eobp)))
5749           (setq number (read cur))
5750           (when (not allp)
5751             (while (and sequence
5752                         (< (car sequence) number))
5753               (setq sequence (cdr sequence))))
5754           (when (and (or allp
5755                          (and sequence
5756                               (eq number (car sequence))))
5757                      (progn
5758                        (setq sequence (cdr sequence))
5759                        (setq header (inline
5760                                       (gnus-nov-parse-line
5761                                        number dependencies force-new)))))
5762             (push header headers))
5763           (forward-line 1)))
5764       ;; A common bug in inn is that if you have posted an article and
5765       ;; then retrieves the active file, it will answer correctly --
5766       ;; the new article is included.  However, a NOV entry for the
5767       ;; article may not have been generated yet, so this may fail.
5768       ;; We work around this problem by retrieving the last few
5769       ;; headers using HEAD.
5770       (if (or (not also-fetch-heads)
5771               (not sequence))
5772           ;; We (probably) got all the headers.
5773           (nreverse headers)
5774         (let ((gnus-nov-is-evil t))
5775           (nconc
5776            (nreverse headers)
5777            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5778              (gnus-get-newsgroup-headers))))))))
5779
5780 (defun gnus-article-get-xrefs ()
5781   "Fill in the Xref value in `gnus-current-headers', if necessary.
5782 This is meant to be called in `gnus-article-internal-prepare-hook'."
5783   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5784                                  gnus-current-headers)))
5785     (or (not gnus-use-cross-reference)
5786         (not headers)
5787         (and (mail-header-xref headers)
5788              (not (string= (mail-header-xref headers) "")))
5789         (let ((case-fold-search t)
5790               xref)
5791           (save-restriction
5792             (nnheader-narrow-to-headers)
5793             (goto-char (point-min))
5794             (when (or (and (not (eobp))
5795                            (eq (downcase (char-after)) ?x)
5796                            (looking-at "Xref:"))
5797                       (search-forward "\nXref:" nil t))
5798               (goto-char (1+ (match-end 0)))
5799               (setq xref (buffer-substring (point)
5800                                            (progn (end-of-line) (point))))
5801               (mail-header-set-xref headers xref)))))))
5802
5803 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5804   "Find article ID and insert the summary line for that article.
5805 OLD-HEADER can either be a header or a line number to insert
5806 the subject line on."
5807   (let* ((line (and (numberp old-header) old-header))
5808          (old-header (and (vectorp old-header) old-header))
5809          (header (cond ((and old-header use-old-header)
5810                         old-header)
5811                        ((and (numberp id)
5812                              (gnus-number-to-header id))
5813                         (gnus-number-to-header id))
5814                        (t
5815                         (gnus-read-header id))))
5816          (number (and (numberp id) id))
5817          d)
5818     (when header
5819       ;; Rebuild the thread that this article is part of and go to the
5820       ;; article we have fetched.
5821       (when (and (not gnus-show-threads)
5822                  old-header)
5823         (when (and number
5824                    (setq d (gnus-data-find (mail-header-number old-header))))
5825           (goto-char (gnus-data-pos d))
5826           (gnus-data-remove
5827            number
5828            (- (gnus-point-at-bol)
5829               (prog1
5830                   (1+ (gnus-point-at-eol))
5831                 (gnus-delete-line))))))
5832       (when old-header
5833         (mail-header-set-number header (mail-header-number old-header)))
5834       (setq gnus-newsgroup-sparse
5835             (delq (setq number (mail-header-number header))
5836                   gnus-newsgroup-sparse))
5837       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5838       (push number gnus-newsgroup-limit)
5839       (gnus-rebuild-thread (mail-header-id header) line)
5840       (gnus-summary-goto-subject number nil t))
5841     (when (and (numberp number)
5842                (> number 0))
5843       ;; We have to update the boundaries even if we can't fetch the
5844       ;; article if ID is a number -- so that the next `P' or `N'
5845       ;; command will fetch the previous (or next) article even
5846       ;; if the one we tried to fetch this time has been canceled.
5847       (when (> number gnus-newsgroup-end)
5848         (setq gnus-newsgroup-end number))
5849       (when (< number gnus-newsgroup-begin)
5850         (setq gnus-newsgroup-begin number))
5851       (setq gnus-newsgroup-unselected
5852             (delq number gnus-newsgroup-unselected)))
5853     ;; Report back a success?
5854     (and header (mail-header-number header))))
5855
5856 ;;; Process/prefix in the summary buffer
5857
5858 (defun gnus-summary-work-articles (n)
5859   "Return a list of articles to be worked upon.
5860 The prefix argument, the list of process marked articles, and the
5861 current article will be taken into consideration."
5862   (save-excursion
5863     (set-buffer gnus-summary-buffer)
5864     (cond
5865      (n
5866       ;; A numerical prefix has been given.
5867       (setq n (prefix-numeric-value n))
5868       (let ((backward (< n 0))
5869             (n (abs (prefix-numeric-value n)))
5870             articles article)
5871         (save-excursion
5872           (while
5873               (and (> n 0)
5874                    (push (setq article (gnus-summary-article-number))
5875                          articles)
5876                    (if backward
5877                        (gnus-summary-find-prev nil article)
5878                      (gnus-summary-find-next nil article)))
5879             (decf n)))
5880         (nreverse articles)))
5881      ((and (gnus-region-active-p) (mark))
5882       (message "region active")
5883       ;; Work on the region between point and mark.
5884       (let ((max (max (point) (mark)))
5885             articles article)
5886         (save-excursion
5887           (goto-char (min (min (point) (mark))))
5888           (while
5889               (and
5890                (push (setq article (gnus-summary-article-number)) articles)
5891                (gnus-summary-find-next nil article)
5892                (< (point) max)))
5893           (nreverse articles))))
5894      (gnus-newsgroup-processable
5895       ;; There are process-marked articles present.
5896       ;; Save current state.
5897       (gnus-summary-save-process-mark)
5898       ;; Return the list.
5899       (reverse gnus-newsgroup-processable))
5900      (t
5901       ;; Just return the current article.
5902       (list (gnus-summary-article-number))))))
5903
5904 (defmacro gnus-summary-iterate (arg &rest forms)
5905   "Iterate over the process/prefixed articles and do FORMS.
5906 ARG is the interactive prefix given to the command.  FORMS will be
5907 executed with point over the summary line of the articles."
5908   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5909     `(let ((,articles (gnus-summary-work-articles ,arg)))
5910        (while ,articles
5911          (gnus-summary-goto-subject (car ,articles))
5912          ,@forms
5913          (pop ,articles)))))
5914
5915 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5916 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5917
5918 (defun gnus-summary-save-process-mark ()
5919   "Push the current set of process marked articles on the stack."
5920   (interactive)
5921   (push (copy-sequence gnus-newsgroup-processable)
5922         gnus-newsgroup-process-stack))
5923
5924 (defun gnus-summary-kill-process-mark ()
5925   "Push the current set of process marked articles on the stack and unmark."
5926   (interactive)
5927   (gnus-summary-save-process-mark)
5928   (gnus-summary-unmark-all-processable))
5929
5930 (defun gnus-summary-yank-process-mark ()
5931   "Pop the last process mark state off the stack and restore it."
5932   (interactive)
5933   (unless gnus-newsgroup-process-stack
5934     (error "Empty mark stack"))
5935   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5936
5937 (defun gnus-summary-process-mark-set (set)
5938   "Make SET into the current process marked articles."
5939   (gnus-summary-unmark-all-processable)
5940   (while set
5941     (gnus-summary-set-process-mark (pop set))))
5942
5943 ;;; Searching and stuff
5944
5945 (defun gnus-summary-search-group (&optional backward use-level)
5946   "Search for next unread newsgroup.
5947 If optional argument BACKWARD is non-nil, search backward instead."
5948   (save-excursion
5949     (set-buffer gnus-group-buffer)
5950     (when (gnus-group-search-forward
5951            backward nil (if use-level (gnus-group-group-level) nil))
5952       (gnus-group-group-name))))
5953
5954 (defun gnus-summary-best-group (&optional exclude-group)
5955   "Find the name of the best unread group.
5956 If EXCLUDE-GROUP, do not go to this group."
5957   (save-excursion
5958     (set-buffer gnus-group-buffer)
5959     (save-excursion
5960       (gnus-group-best-unread-group exclude-group))))
5961
5962 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5963   (if backward (gnus-summary-find-prev)
5964     (let* ((dummy (gnus-summary-article-intangible-p))
5965            (article (or article (gnus-summary-article-number)))
5966            (arts (gnus-data-find-list article))
5967            result)
5968       (when (and (not dummy)
5969                  (or (not gnus-summary-check-current)
5970                      (not unread)
5971                      (not (gnus-data-unread-p (car arts)))))
5972         (setq arts (cdr arts)))
5973       (when (setq result
5974                   (if unread
5975                       (progn
5976                         (while arts
5977                           (when (or (and undownloaded
5978                                          (memq (car arts) gnus-newsgroup-undownloaded))
5979                                     (gnus-data-unread-p (car arts)))
5980                             (setq result (car arts)
5981                                   arts nil))
5982                           (setq arts (cdr arts)))
5983                         result)
5984                     (car arts)))
5985         (goto-char (gnus-data-pos result))
5986         (gnus-data-number result)))))
5987
5988 (defun gnus-summary-find-prev (&optional unread article)
5989   (let* ((eobp (eobp))
5990          (article (or article (gnus-summary-article-number)))
5991          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5992          result)
5993     (when (and (not eobp)
5994                (or (not gnus-summary-check-current)
5995                    (not unread)
5996                    (not (gnus-data-unread-p (car arts)))))
5997       (setq arts (cdr arts)))
5998     (when (setq result
5999                 (if unread
6000                     (progn
6001                       (while arts
6002                         (when (gnus-data-unread-p (car arts))
6003                           (setq result (car arts)
6004                                 arts nil))
6005                         (setq arts (cdr arts)))
6006                       result)
6007                   (car arts)))
6008       (goto-char (gnus-data-pos result))
6009       (gnus-data-number result))))
6010
6011 (defun gnus-summary-find-subject (subject &optional unread backward article)
6012   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6013          (article (or article (gnus-summary-article-number)))
6014          (articles (gnus-data-list backward))
6015          (arts (gnus-data-find-list article articles))
6016          result)
6017     (when (or (not gnus-summary-check-current)
6018               (not unread)
6019               (not (gnus-data-unread-p (car arts))))
6020       (setq arts (cdr arts)))
6021     (while arts
6022       (and (or (not unread)
6023                (gnus-data-unread-p (car arts)))
6024            (vectorp (gnus-data-header (car arts)))
6025            (gnus-subject-equal
6026             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6027            (setq result (car arts)
6028                  arts nil))
6029       (setq arts (cdr arts)))
6030     (and result
6031          (goto-char (gnus-data-pos result))
6032          (gnus-data-number result))))
6033
6034 (defun gnus-summary-search-forward (&optional unread subject backward)
6035   "Search forward for an article.
6036 If UNREAD, look for unread articles.  If SUBJECT, look for
6037 articles with that subject.  If BACKWARD, search backward instead."
6038   (cond (subject (gnus-summary-find-subject subject unread backward))
6039         (backward (gnus-summary-find-prev unread))
6040         (t (gnus-summary-find-next unread))))
6041
6042 (defun gnus-recenter (&optional n)
6043   "Center point in window and redisplay frame.
6044 Also do horizontal recentering."
6045   (interactive "P")
6046   (when (and gnus-auto-center-summary
6047              (not (eq gnus-auto-center-summary 'vertical)))
6048     (gnus-horizontal-recenter))
6049   (recenter n))
6050
6051 (defun gnus-summary-recenter ()
6052   "Center point in the summary window.
6053 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6054 displayed, no centering will be performed."
6055   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6056 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6057   (interactive)
6058   (let* ((top (cond ((< (window-height) 4) 0)
6059                     ((< (window-height) 7) 1)
6060                     (t (if (numberp gnus-auto-center-summary)
6061                            gnus-auto-center-summary
6062                          2))))
6063          (height (1- (window-height)))
6064          (bottom (save-excursion (goto-char (point-max))
6065                                  (forward-line (- height))
6066                                  (point)))
6067          (window (get-buffer-window (current-buffer))))
6068     ;; The user has to want it.
6069     (when gnus-auto-center-summary
6070       (when (get-buffer-window gnus-article-buffer)
6071         ;; Only do recentering when the article buffer is displayed,
6072       ;; Set the window start to either `bottom', which is the biggest
6073         ;; possible valid number, or the second line from the top,
6074         ;; whichever is the least.
6075         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6076           (if (> bottom top-pos)
6077               ;; Keep the second line from the top visible
6078               (set-window-start window top-pos t)
6079             ;; Try to keep the bottom line visible; if it's partially
6080             ;; obscured, either scroll one more line to make it fully
6081             ;; visible, or revert to using TOP-POS.
6082             (save-excursion
6083               (goto-char (point-max))
6084               (forward-line -1)
6085               (let ((last-line-start (point)))
6086                 (goto-char bottom)
6087                 (set-window-start window (point) t)
6088                 (when (not (pos-visible-in-window-p last-line-start window))
6089                   (forward-line 1)
6090                   (set-window-start window (min (point) top-pos) t)))))))
6091       ;; Do horizontal recentering while we're at it.
6092       (when (and (get-buffer-window (current-buffer) t)
6093                  (not (eq gnus-auto-center-summary 'vertical)))
6094         (let ((selected (selected-window)))
6095           (select-window (get-buffer-window (current-buffer) t))
6096           (gnus-summary-position-point)
6097           (gnus-horizontal-recenter)
6098           (select-window selected))))))
6099
6100 (defun gnus-summary-jump-to-group (newsgroup)
6101   "Move point to NEWSGROUP in group mode buffer."
6102   ;; Keep update point of group mode buffer if visible.
6103   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6104       (save-window-excursion
6105         ;; Take care of tree window mode.
6106         (when (get-buffer-window gnus-group-buffer)
6107           (pop-to-buffer gnus-group-buffer))
6108         (gnus-group-jump-to-group newsgroup))
6109     (save-excursion
6110       ;; Take care of tree window mode.
6111       (if (get-buffer-window gnus-group-buffer)
6112           (pop-to-buffer gnus-group-buffer)
6113         (set-buffer gnus-group-buffer))
6114       (gnus-group-jump-to-group newsgroup))))
6115
6116 ;; This function returns a list of article numbers based on the
6117 ;; difference between the ranges of read articles in this group and
6118 ;; the range of active articles.
6119 (defun gnus-list-of-unread-articles (group)
6120   (let* ((read (gnus-info-read (gnus-get-info group)))
6121          (active (or (gnus-active group) (gnus-activate-group group)))
6122          (last (cdr active))
6123          first nlast unread)
6124     ;; If none are read, then all are unread.
6125     (if (not read)
6126         (setq first (car active))
6127       ;; If the range of read articles is a single range, then the
6128       ;; first unread article is the article after the last read
6129       ;; article.  Sounds logical, doesn't it?
6130       (if (and (not (listp (cdr read)))
6131                (or (< (car read) (car active))
6132                    (progn (setq read (list read))
6133                           nil)))
6134           (setq first (max (car active) (1+ (cdr read))))
6135         ;; `read' is a list of ranges.
6136         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6137                                   (caar read)))
6138                   1)
6139           (setq first (car active)))
6140         (while read
6141           (when first
6142             (while (< first nlast)
6143               (push first unread)
6144               (setq first (1+ first))))
6145           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6146           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6147           (setq read (cdr read)))))
6148     ;; And add the last unread articles.
6149     (while (<= first last)
6150       (push first unread)
6151       (setq first (1+ first)))
6152     ;; Return the list of unread articles.
6153     (delq 0 (nreverse unread))))
6154
6155 (defun gnus-list-of-read-articles (group)
6156   "Return a list of unread, unticked and non-dormant articles."
6157   (let* ((info (gnus-get-info group))
6158          (marked (gnus-info-marks info))
6159          (active (gnus-active group)))
6160     (and info active
6161          (gnus-list-range-difference
6162           (gnus-list-range-difference
6163            (gnus-sorted-complement
6164             (gnus-uncompress-range active)
6165             (gnus-list-of-unread-articles group))
6166            (cdr (assq 'dormant marked)))
6167           (cdr (assq 'tick marked))))))
6168
6169 ;; Various summary commands
6170
6171 (defun gnus-summary-select-article-buffer ()
6172   "Reconfigure windows to show article buffer."
6173   (interactive)
6174   (if (not (gnus-buffer-live-p gnus-article-buffer))
6175       (error "There is no article buffer for this summary buffer")
6176     (gnus-configure-windows 'article)
6177     (select-window (get-buffer-window gnus-article-buffer))))
6178
6179 (defun gnus-summary-universal-argument (arg)
6180   "Perform any operation on all articles that are process/prefixed."
6181   (interactive "P")
6182   (let ((articles (gnus-summary-work-articles arg))
6183         func article)
6184     (if (eq
6185          (setq
6186           func
6187           (key-binding
6188            (read-key-sequence
6189             (substitute-command-keys
6190              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6191          'undefined)
6192         (gnus-error 1 "Undefined key")
6193       (save-excursion
6194         (while articles
6195           (gnus-summary-goto-subject (setq article (pop articles)))
6196           (let (gnus-newsgroup-processable)
6197             (command-execute func))
6198           (gnus-summary-remove-process-mark article)))))
6199   (gnus-summary-position-point))
6200
6201 (defun gnus-summary-toggle-truncation (&optional arg)
6202   "Toggle truncation of summary lines.
6203 With arg, turn line truncation on if arg is positive."
6204   (interactive "P")
6205   (setq truncate-lines
6206         (if (null arg) (not truncate-lines)
6207           (> (prefix-numeric-value arg) 0)))
6208   (redraw-display))
6209
6210 (defun gnus-summary-find-uncancelled ()
6211   "Return the number of an uncancelled article.
6212 The current article is considered, then following articles, then previous
6213 articles.  If all articles are cancelled then return a dummy 0."
6214   (let (found)
6215     (dolist (rev '(nil t))
6216       (unless found      ; don't demand the reverse list if we don't need it
6217         (let ((data (gnus-data-find-list
6218                      (gnus-summary-article-number) (gnus-data-list rev))))
6219           (while (and data (not found))
6220             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6221                 (setq found (gnus-data-number (car data))))
6222             (setq data (cdr data))))))
6223     (or found 0)))
6224
6225 (defun gnus-summary-reselect-current-group (&optional all rescan)
6226   "Exit and then reselect the current newsgroup.
6227 The prefix argument ALL means to select all articles."
6228   (interactive "P")
6229   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6230     (error "Ephemeral groups can't be reselected"))
6231   (let ((current-subject (gnus-summary-find-uncancelled))
6232         (group gnus-newsgroup-name))
6233     (setq gnus-newsgroup-begin nil)
6234     (gnus-summary-exit)
6235     ;; We have to adjust the point of group mode buffer because
6236     ;; point was moved to the next unread newsgroup by exiting.
6237     (gnus-summary-jump-to-group group)
6238     (when rescan
6239       (save-excursion
6240         (gnus-group-get-new-news-this-group 1)))
6241     (gnus-group-read-group all t)
6242     (gnus-summary-goto-subject current-subject nil t)))
6243
6244 (defun gnus-summary-rescan-group (&optional all)
6245   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6246   (interactive "P")
6247   (gnus-summary-reselect-current-group all t))
6248
6249 (defun gnus-summary-update-info (&optional non-destructive)
6250   (save-excursion
6251     (let ((group gnus-newsgroup-name))
6252       (when group
6253         (when gnus-newsgroup-kill-headers
6254           (setq gnus-newsgroup-killed
6255                 (gnus-compress-sequence
6256                  (gnus-sorted-union
6257                   (gnus-list-range-intersection
6258                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6259                   gnus-newsgroup-unreads)
6260                  t)))
6261         (unless (listp (cdr gnus-newsgroup-killed))
6262           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6263         (let ((headers gnus-newsgroup-headers))
6264           ;; Set the new ranges of read articles.
6265           (save-excursion
6266             (set-buffer gnus-group-buffer)
6267             (gnus-undo-force-boundary))
6268           (gnus-update-read-articles
6269            group (gnus-sorted-union
6270                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6271           ;; Set the current article marks.
6272           (let ((gnus-newsgroup-scored
6273                  (if (and (not gnus-save-score)
6274                           (not non-destructive))
6275                      nil
6276                    gnus-newsgroup-scored)))
6277             (save-excursion
6278               (gnus-update-marks)))
6279           ;; Do the cross-ref thing.
6280           (when gnus-use-cross-reference
6281             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6282           ;; Do not switch windows but change the buffer to work.
6283           (set-buffer gnus-group-buffer)
6284           (unless (gnus-ephemeral-group-p group)
6285             (gnus-group-update-group group)))))))
6286
6287 (defun gnus-summary-save-newsrc (&optional force)
6288   "Save the current number of read/marked articles in the dribble buffer.
6289 The dribble buffer will then be saved.
6290 If FORCE (the prefix), also save the .newsrc file(s)."
6291   (interactive "P")
6292   (gnus-summary-update-info t)
6293   (if force
6294       (gnus-save-newsrc-file)
6295     (gnus-dribble-save)))
6296
6297 (defun gnus-summary-exit (&optional temporary)
6298   "Exit reading current newsgroup, and then return to group selection mode.
6299 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6300   (interactive)
6301   (gnus-set-global-variables)
6302   (when (gnus-buffer-live-p gnus-article-buffer)
6303     (save-excursion
6304       (set-buffer gnus-article-buffer)
6305       (mm-destroy-parts gnus-article-mime-handles)
6306       ;; Set it to nil for safety reason.
6307       (setq gnus-article-mime-handle-alist nil)
6308       (setq gnus-article-mime-handles nil)))
6309   (gnus-kill-save-kill-buffer)
6310   (gnus-async-halt-prefetch)
6311   (let* ((group gnus-newsgroup-name)
6312          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6313          (gnus-group-is-exiting-p t)
6314          (mode major-mode)
6315          (group-point nil)
6316          (buf (current-buffer)))
6317     (unless quit-config
6318       ;; Do adaptive scoring, and possibly save score files.
6319       (when gnus-newsgroup-adaptive
6320         (gnus-score-adaptive))
6321       (when gnus-use-scoring
6322         (gnus-score-save)))
6323     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6324     ;; If we have several article buffers, we kill them at exit.
6325     (unless gnus-single-article-buffer
6326       (gnus-kill-buffer gnus-original-article-buffer)
6327       (setq gnus-article-current nil))
6328     (when gnus-use-cache
6329       (gnus-cache-possibly-remove-articles)
6330       (gnus-cache-save-buffers))
6331     (gnus-async-prefetch-remove-group group)
6332     (when gnus-suppress-duplicates
6333       (gnus-dup-enter-articles))
6334     (when gnus-use-trees
6335       (gnus-tree-close group))
6336     (when gnus-use-cache
6337       (gnus-cache-write-active))
6338     ;; Remove entries for this group.
6339     (nnmail-purge-split-history (gnus-group-real-name group))
6340     ;; Make all changes in this group permanent.
6341     (unless quit-config
6342       (gnus-run-hooks 'gnus-exit-group-hook)
6343       (gnus-summary-update-info))
6344     (gnus-close-group group)
6345     ;; Make sure where we were, and go to next newsgroup.
6346     (set-buffer gnus-group-buffer)
6347     (unless quit-config
6348       (gnus-group-jump-to-group group))
6349     (gnus-run-hooks 'gnus-summary-exit-hook)
6350     (unless (or quit-config
6351                 ;; If this group has disappeared from the summary
6352                 ;; buffer, don't skip forwards.
6353                 (not (string= group (gnus-group-group-name))))
6354       (gnus-group-next-unread-group 1))
6355     (setq group-point (point))
6356     (if temporary
6357         nil                             ;Nothing to do.
6358       ;; If we have several article buffers, we kill them at exit.
6359       (unless gnus-single-article-buffer
6360         (gnus-kill-buffer gnus-article-buffer)
6361         (gnus-kill-buffer gnus-original-article-buffer)
6362         (setq gnus-article-current nil))
6363       (set-buffer buf)
6364       (if (not gnus-kill-summary-on-exit)
6365           (progn
6366             (gnus-deaden-summary)
6367             (setq mode nil))
6368         ;; We set all buffer-local variables to nil.  It is unclear why
6369         ;; this is needed, but if we don't, buffer-local variables are
6370         ;; not garbage-collected, it seems.  This would the lead to en
6371         ;; ever-growing Emacs.
6372         (gnus-summary-clear-local-variables)
6373         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6374           (gnus-summary-clear-local-variables))
6375         (when (get-buffer gnus-article-buffer)
6376           (bury-buffer gnus-article-buffer))
6377         ;; We clear the global counterparts of the buffer-local
6378         ;; variables as well, just to be on the safe side.
6379         (set-buffer gnus-group-buffer)
6380         (gnus-summary-clear-local-variables)
6381         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6382           (gnus-summary-clear-local-variables)))
6383       (setq gnus-current-select-method gnus-select-method)
6384       (pop-to-buffer gnus-group-buffer)
6385       (if (not quit-config)
6386           (progn
6387             (goto-char group-point)
6388             (gnus-configure-windows 'group 'force))
6389         (gnus-handle-ephemeral-exit quit-config))
6390       ;; Return to group mode buffer.
6391       (when (eq mode 'gnus-summary-mode)
6392         (gnus-kill-buffer buf))
6393       ;; Clear the current group name.
6394       (unless quit-config
6395         (setq gnus-newsgroup-name nil)))))
6396
6397 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6398 (defun gnus-summary-exit-no-update (&optional no-questions)
6399   "Quit reading current newsgroup without updating read article info."
6400   (interactive)
6401   (let* ((group gnus-newsgroup-name)
6402          (gnus-group-is-exiting-p t)
6403          (quit-config (gnus-group-quit-config group)))
6404     (when (or no-questions
6405               gnus-expert-user
6406               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6407       (gnus-async-halt-prefetch)
6408       (mapcar 'funcall
6409               (delq 'gnus-summary-expire-articles
6410                     (copy-sequence gnus-summary-prepare-exit-hook)))
6411       (when (gnus-buffer-live-p gnus-article-buffer)
6412         (save-excursion
6413           (set-buffer gnus-article-buffer)
6414           (mm-destroy-parts gnus-article-mime-handles)
6415           ;; Set it to nil for safety reason.
6416           (setq gnus-article-mime-handle-alist nil)
6417           (setq gnus-article-mime-handles nil)))
6418       ;; If we have several article buffers, we kill them at exit.
6419       (unless gnus-single-article-buffer
6420         (gnus-kill-buffer gnus-article-buffer)
6421         (gnus-kill-buffer gnus-original-article-buffer)
6422         (setq gnus-article-current nil))
6423       (if (not gnus-kill-summary-on-exit)
6424           (gnus-deaden-summary)
6425         (gnus-close-group group)
6426         (gnus-summary-clear-local-variables)
6427         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6428           (gnus-summary-clear-local-variables))
6429         (set-buffer gnus-group-buffer)
6430         (gnus-summary-clear-local-variables)
6431         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6432           (gnus-summary-clear-local-variables))
6433         (when (get-buffer gnus-summary-buffer)
6434           (kill-buffer gnus-summary-buffer)))
6435       (unless gnus-single-article-buffer
6436         (setq gnus-article-current nil))
6437       (when gnus-use-trees
6438         (gnus-tree-close group))
6439       (gnus-async-prefetch-remove-group group)
6440       (when (get-buffer gnus-article-buffer)
6441         (bury-buffer gnus-article-buffer))
6442       ;; Return to the group buffer.
6443       (gnus-configure-windows 'group 'force)
6444       ;; Clear the current group name.
6445       (setq gnus-newsgroup-name nil)
6446       (gnus-group-update-group group)
6447       (when (equal (gnus-group-group-name) group)
6448         (gnus-group-next-unread-group 1))
6449       (when quit-config
6450         (gnus-handle-ephemeral-exit quit-config)))))
6451
6452 (defun gnus-handle-ephemeral-exit (quit-config)
6453   "Handle movement when leaving an ephemeral group.
6454 The state which existed when entering the ephemeral is reset."
6455   (if (not (buffer-name (car quit-config)))
6456       (gnus-configure-windows 'group 'force)
6457     (set-buffer (car quit-config))
6458     (cond ((eq major-mode 'gnus-summary-mode)
6459            (gnus-set-global-variables))
6460           ((eq major-mode 'gnus-article-mode)
6461            (save-excursion
6462              ;; The `gnus-summary-buffer' variable may point
6463              ;; to the old summary buffer when using a single
6464              ;; article buffer.
6465              (unless (gnus-buffer-live-p gnus-summary-buffer)
6466                (set-buffer gnus-group-buffer))
6467              (set-buffer gnus-summary-buffer)
6468              (gnus-set-global-variables))))
6469     (if (or (eq (cdr quit-config) 'article)
6470             (eq (cdr quit-config) 'pick))
6471         (progn
6472           ;; The current article may be from the ephemeral group
6473           ;; thus it is best that we reload this article
6474           (gnus-summary-show-article)
6475           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6476               (gnus-configure-windows 'pick 'force)
6477             (gnus-configure-windows (cdr quit-config) 'force)))
6478       (gnus-configure-windows (cdr quit-config) 'force))
6479     (when (eq major-mode 'gnus-summary-mode)
6480       (gnus-summary-next-subject 1 nil t)
6481       (gnus-summary-recenter)
6482       (gnus-summary-position-point))))
6483
6484 ;;; Dead summaries.
6485
6486 (defvar gnus-dead-summary-mode-map nil)
6487
6488 (unless gnus-dead-summary-mode-map
6489   (setq gnus-dead-summary-mode-map (make-keymap))
6490   (suppress-keymap gnus-dead-summary-mode-map)
6491   (substitute-key-definition
6492    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6493   (dolist (key '("\C-d" "\r" "\177" [delete]))
6494     (define-key gnus-dead-summary-mode-map
6495       key 'gnus-summary-wake-up-the-dead))
6496   (dolist (key '("q" "Q"))
6497     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6498
6499 (defvar gnus-dead-summary-mode nil
6500   "Minor mode for Gnus summary buffers.")
6501
6502 (defun gnus-dead-summary-mode (&optional arg)
6503   "Minor mode for Gnus summary buffers."
6504   (interactive "P")
6505   (when (eq major-mode 'gnus-summary-mode)
6506     (make-local-variable 'gnus-dead-summary-mode)
6507     (setq gnus-dead-summary-mode
6508           (if (null arg) (not gnus-dead-summary-mode)
6509             (> (prefix-numeric-value arg) 0)))
6510     (when gnus-dead-summary-mode
6511       (gnus-add-minor-mode
6512        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6513
6514 (defun gnus-deaden-summary ()
6515   "Make the current summary buffer into a dead summary buffer."
6516   ;; Kill any previous dead summary buffer.
6517   (when (and gnus-dead-summary
6518              (buffer-name gnus-dead-summary))
6519     (save-excursion
6520       (set-buffer gnus-dead-summary)
6521       (when gnus-dead-summary-mode
6522         (kill-buffer (current-buffer)))))
6523   ;; Make this the current dead summary.
6524   (setq gnus-dead-summary (current-buffer))
6525   (gnus-dead-summary-mode 1)
6526   (let ((name (buffer-name)))
6527     (when (string-match "Summary" name)
6528       (rename-buffer
6529        (concat (substring name 0 (match-beginning 0)) "Dead "
6530                (substring name (match-beginning 0)))
6531        t)
6532       (bury-buffer))))
6533
6534 (defun gnus-kill-or-deaden-summary (buffer)
6535   "Kill or deaden the summary BUFFER."
6536   (save-excursion
6537     (when (and (buffer-name buffer)
6538                (not gnus-single-article-buffer))
6539       (save-excursion
6540         (set-buffer buffer)
6541         (gnus-kill-buffer gnus-article-buffer)
6542         (gnus-kill-buffer gnus-original-article-buffer)))
6543     (cond
6544      ;; Kill the buffer.
6545      (gnus-kill-summary-on-exit
6546       (when (and gnus-use-trees
6547                  (gnus-buffer-exists-p buffer))
6548         (save-excursion
6549           (set-buffer buffer)
6550           (gnus-tree-close gnus-newsgroup-name)))
6551       (gnus-kill-buffer buffer))
6552      ;; Deaden the buffer.
6553      ((gnus-buffer-exists-p buffer)
6554       (save-excursion
6555         (set-buffer buffer)
6556         (gnus-deaden-summary))))))
6557
6558 (defun gnus-summary-wake-up-the-dead (&rest args)
6559   "Wake up the dead summary buffer."
6560   (interactive)
6561   (gnus-dead-summary-mode -1)
6562   (let ((name (buffer-name)))
6563     (when (string-match "Dead " name)
6564       (rename-buffer
6565        (concat (substring name 0 (match-beginning 0))
6566                (substring name (match-end 0)))
6567        t)))
6568   (gnus-message 3 "This dead summary is now alive again"))
6569
6570 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6571 (defun gnus-summary-fetch-faq (&optional faq-dir)
6572   "Fetch the FAQ for the current group.
6573 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6574 in."
6575   (interactive
6576    (list
6577     (when current-prefix-arg
6578       (completing-read
6579        "FAQ dir: " (and (listp gnus-group-faq-directory)
6580                         (mapcar (lambda (file) (list file))
6581                                 gnus-group-faq-directory))))))
6582   (let (gnus-faq-buffer)
6583     (when (setq gnus-faq-buffer
6584                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6585       (gnus-configure-windows 'summary-faq))))
6586
6587 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6588 (defun gnus-summary-describe-group (&optional force)
6589   "Describe the current newsgroup."
6590   (interactive "P")
6591   (gnus-group-describe-group force gnus-newsgroup-name))
6592
6593 (defun gnus-summary-describe-briefly ()
6594   "Describe summary mode commands briefly."
6595   (interactive)
6596   (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")))
6597
6598 ;; Walking around group mode buffer from summary mode.
6599
6600 (defun gnus-summary-next-group (&optional no-article target-group backward)
6601   "Exit current newsgroup and then select next unread newsgroup.
6602 If prefix argument NO-ARTICLE is non-nil, no article is selected
6603 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6604 previous group instead."
6605   (interactive "P")
6606   ;; Stop pre-fetching.
6607   (gnus-async-halt-prefetch)
6608   (let ((current-group gnus-newsgroup-name)
6609         (current-buffer (current-buffer))
6610         entered)
6611     (while (not entered)
6612       ;; Then we find what group we are supposed to enter.
6613       (set-buffer gnus-group-buffer)
6614       (gnus-group-jump-to-group current-group)
6615       (setq target-group
6616             (or target-group
6617                 (if (eq gnus-keep-same-level 'best)
6618                     (gnus-summary-best-group gnus-newsgroup-name)
6619                   (gnus-summary-search-group backward gnus-keep-same-level))))
6620       (if (not target-group)
6621           ;; There are no further groups, so we return to the group
6622           ;; buffer.
6623           (progn
6624             (gnus-message 5 "Returning to the group buffer")
6625             (setq entered t)
6626             (when (gnus-buffer-live-p current-buffer)
6627               (set-buffer current-buffer)
6628               (gnus-summary-exit))
6629             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6630         ;; We try to enter the target group.
6631         (gnus-group-jump-to-group target-group)
6632         (let ((unreads (gnus-group-group-unread)))
6633           (if (and (or (eq t unreads)
6634                        (and unreads (not (zerop unreads))))
6635                    (progn
6636                      ;; Now we semi-exit this group to update Xrefs
6637                      ;; and all variables.  We can't do a real exit,
6638                      ;; because the window conf must remain the same
6639                      ;; in case the user is prompted for info, and we
6640                      ;; don't want the window conf to change before
6641                      ;; that...
6642                      (when (gnus-buffer-live-p current-buffer)
6643                        (set-buffer current-buffer)
6644                        (gnus-summary-exit t))
6645                      (gnus-summary-read-group
6646                       target-group nil no-article
6647                       (and (buffer-name current-buffer) current-buffer)
6648                       nil backward)))
6649               (setq entered t)
6650             (setq current-group target-group
6651                   target-group nil)))))))
6652
6653 (defun gnus-summary-prev-group (&optional no-article)
6654   "Exit current newsgroup and then select previous unread newsgroup.
6655 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6656   (interactive "P")
6657   (gnus-summary-next-group no-article nil t))
6658
6659 ;; Walking around summary lines.
6660
6661 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6662   "Go to the first unread subject.
6663 If UNREAD is non-nil, go to the first unread article.
6664 Returns the article selected or nil if there are no unread articles."
6665   (interactive "P")
6666   (prog1
6667       (cond
6668        ;; Empty summary.
6669        ((null gnus-newsgroup-data)
6670         (gnus-message 3 "No articles in the group")
6671         nil)
6672        ;; Pick the first article.
6673        ((not unread)
6674         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6675         (gnus-data-number (car gnus-newsgroup-data)))
6676        ;; No unread articles.
6677        ((null gnus-newsgroup-unreads)
6678         (gnus-message 3 "No more unread articles")
6679         nil)
6680        ;; Find the first unread article.
6681        (t
6682         (let ((data gnus-newsgroup-data))
6683           (while (and data
6684                       (and (not (and undownloaded
6685                                      (memq (car data)
6686                                            gnus-newsgroup-undownloaded)))
6687                            (if unseen
6688                                (or (not (memq
6689                                          (gnus-data-number (car data))
6690                                          gnus-newsgroup-unseen))
6691                                    (not (gnus-data-unread-p (car data))))
6692                              (not (gnus-data-unread-p (car data))))))
6693             (setq data (cdr data)))
6694           (when data
6695             (goto-char (gnus-data-pos (car data)))
6696             (gnus-data-number (car data))))))
6697     (gnus-summary-position-point)))
6698
6699 (defun gnus-summary-next-subject (n &optional unread dont-display)
6700   "Go to next N'th summary line.
6701 If N is negative, go to the previous N'th subject line.
6702 If UNREAD is non-nil, only unread articles are selected.
6703 The difference between N and the actual number of steps taken is
6704 returned."
6705   (interactive "p")
6706   (let ((backward (< n 0))
6707         (n (abs n)))
6708     (while (and (> n 0)
6709                 (if backward
6710                     (gnus-summary-find-prev unread)
6711                   (gnus-summary-find-next unread)))
6712       (unless (zerop (setq n (1- n)))
6713         (gnus-summary-show-thread)))
6714     (when (/= 0 n)
6715       (gnus-message 7 "No more%s articles"
6716                     (if unread " unread" "")))
6717     (unless dont-display
6718       (gnus-summary-recenter)
6719       (gnus-summary-position-point))
6720     n))
6721
6722 (defun gnus-summary-next-unread-subject (n)
6723   "Go to next N'th unread summary line."
6724   (interactive "p")
6725   (gnus-summary-next-subject n t))
6726
6727 (defun gnus-summary-prev-subject (n &optional unread)
6728   "Go to previous N'th summary line.
6729 If optional argument UNREAD is non-nil, only unread article is selected."
6730   (interactive "p")
6731   (gnus-summary-next-subject (- n) unread))
6732
6733 (defun gnus-summary-prev-unread-subject (n)
6734   "Go to previous N'th unread summary line."
6735   (interactive "p")
6736   (gnus-summary-next-subject (- n) t))
6737
6738 (defun gnus-summary-goto-subjects (articles)
6739   "Insert the subject header for ARTICLES in the current buffer."
6740   (save-excursion
6741     (dolist (article articles)
6742       (gnus-summary-goto-subject article t)))
6743   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6744   (gnus-summary-position-point))
6745   
6746 (defun gnus-summary-goto-subject (article &optional force silent)
6747   "Go the subject line of ARTICLE.
6748 If FORCE, also allow jumping to articles not currently shown."
6749   (interactive "nArticle number: ")
6750   (unless (numberp article)
6751     (error "Article %s is not a number" article))
6752   (let ((b (point))
6753         (data (gnus-data-find article)))
6754     ;; We read in the article if we have to.
6755     (and (not data)
6756          force
6757          (gnus-summary-insert-subject
6758           article
6759           (if (or (numberp force) (vectorp force)) force)
6760           t)
6761          (setq data (gnus-data-find article)))
6762     (goto-char b)
6763     (if (not data)
6764         (progn
6765           (unless silent
6766             (gnus-message 3 "Can't find article %d" article))
6767           nil)
6768       (let ((pt (gnus-data-pos data)))
6769         (goto-char pt)
6770         (gnus-summary-set-article-display-arrow pt))
6771       (gnus-summary-position-point)
6772       article)))
6773
6774 ;; Walking around summary lines with displaying articles.
6775
6776 (defun gnus-summary-expand-window (&optional arg)
6777   "Make the summary buffer take up the entire Emacs frame.
6778 Given a prefix, will force an `article' buffer configuration."
6779   (interactive "P")
6780   (if arg
6781       (gnus-configure-windows 'article 'force)
6782     (gnus-configure-windows 'summary 'force)))
6783
6784 (defun gnus-summary-display-article (article &optional all-header)
6785   "Display ARTICLE in article buffer."
6786   (when (gnus-buffer-live-p gnus-article-buffer)
6787     (with-current-buffer gnus-article-buffer
6788       (mm-enable-multibyte)))
6789   (gnus-set-global-variables)
6790   (when (gnus-buffer-live-p gnus-article-buffer)
6791     (with-current-buffer gnus-article-buffer
6792       (setq gnus-article-charset gnus-newsgroup-charset)
6793       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6794       (mm-enable-multibyte)))
6795   (if (null article)
6796       nil
6797     (prog1
6798         (if gnus-summary-display-article-function
6799             (funcall gnus-summary-display-article-function article all-header)
6800           (gnus-article-prepare article all-header))
6801       (gnus-run-hooks 'gnus-select-article-hook)
6802       (when (and gnus-current-article
6803                  (not (zerop gnus-current-article)))
6804         (gnus-summary-goto-subject gnus-current-article))
6805       (gnus-summary-recenter)
6806       (when (and gnus-use-trees gnus-show-threads)
6807         (gnus-possibly-generate-tree article)
6808         (gnus-highlight-selected-tree article))
6809       ;; Successfully display article.
6810       (gnus-article-set-window-start
6811        (cdr (assq article gnus-newsgroup-bookmarks))))))
6812
6813 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6814   "Select the current article.
6815 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6816 non-nil, the article will be re-fetched even if it already present in
6817 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6818 be displayed."
6819   ;; Make sure we are in the summary buffer to work around bbdb bug.
6820   (unless (eq major-mode 'gnus-summary-mode)
6821     (set-buffer gnus-summary-buffer))
6822   (let ((article (or article (gnus-summary-article-number)))
6823         (all-headers (not (not all-headers))) ;Must be t or nil.
6824         gnus-summary-display-article-function)
6825     (and (not pseudo)
6826          (gnus-summary-article-pseudo-p article)
6827          (error "This is a pseudo-article"))
6828     (save-excursion
6829       (set-buffer gnus-summary-buffer)
6830       (if (or (and gnus-single-article-buffer
6831                    (or (null gnus-current-article)
6832                        (null gnus-article-current)
6833                        (null (get-buffer gnus-article-buffer))
6834                        (not (eq article (cdr gnus-article-current)))
6835                        (not (equal (car gnus-article-current)
6836                                    gnus-newsgroup-name))))
6837               (and (not gnus-single-article-buffer)
6838                    (or (null gnus-current-article)
6839                        (not (eq gnus-current-article article))))
6840               force)
6841           ;; The requested article is different from the current article.
6842           (progn
6843             (gnus-summary-display-article article all-headers)
6844             (when (gnus-buffer-live-p gnus-article-buffer)
6845               (with-current-buffer gnus-article-buffer
6846                 (if (not gnus-article-decoded-p) ;; a local variable
6847                     (mm-disable-multibyte))))
6848             (gnus-article-set-window-start
6849              (cdr (assq article gnus-newsgroup-bookmarks)))
6850             article)
6851         'old))))
6852
6853 (defun gnus-summary-force-verify-and-decrypt ()
6854   (interactive)
6855   (let ((mm-verify-option 'known)
6856         (mm-decrypt-option 'known)
6857         (gnus-buttonized-mime-types (append (list "multipart/signed"
6858                                                   "multipart/encrypted")
6859                                             gnus-buttonized-mime-types)))
6860     (gnus-summary-select-article nil 'force)))
6861
6862 (defun gnus-summary-set-current-mark (&optional current-mark)
6863   "Obsolete function."
6864   nil)
6865
6866 (defun gnus-summary-next-article (&optional unread subject backward push)
6867   "Select the next article.
6868 If UNREAD, only unread articles are selected.
6869 If SUBJECT, only articles with SUBJECT are selected.
6870 If BACKWARD, the previous article is selected instead of the next."
6871   (interactive "P")
6872   (cond
6873    ;; Is there such an article?
6874    ((and (gnus-summary-search-forward unread subject backward)
6875          (or (gnus-summary-display-article (gnus-summary-article-number))
6876              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6877     (gnus-summary-position-point))
6878    ;; If not, we try the first unread, if that is wanted.
6879    ((and subject
6880          gnus-auto-select-same
6881          (gnus-summary-first-unread-article))
6882     (gnus-summary-position-point)
6883     (gnus-message 6 "Wrapped"))
6884    ;; Try to get next/previous article not displayed in this group.
6885    ((and gnus-auto-extend-newsgroup
6886          (not unread) (not subject))
6887     (gnus-summary-goto-article
6888      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6889      nil (count-lines (point-min) (point))))
6890    ;; Go to next/previous group.
6891    (t
6892     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6893       (gnus-summary-jump-to-group gnus-newsgroup-name))
6894     (let ((cmd last-command-char)
6895           (point
6896            (save-excursion
6897              (set-buffer gnus-group-buffer)
6898              (point)))
6899           (group
6900            (if (eq gnus-keep-same-level 'best)
6901                (gnus-summary-best-group gnus-newsgroup-name)
6902              (gnus-summary-search-group backward gnus-keep-same-level))))
6903       ;; For some reason, the group window gets selected.  We change
6904       ;; it back.
6905       (select-window (get-buffer-window (current-buffer)))
6906       ;; Select next unread newsgroup automagically.
6907       (cond
6908        ((or (not gnus-auto-select-next)
6909             (not cmd))
6910         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6911        ((or (eq gnus-auto-select-next 'quietly)
6912             (and (eq gnus-auto-select-next 'slightly-quietly)
6913                  push)
6914             (and (eq gnus-auto-select-next 'almost-quietly)
6915                  (gnus-summary-last-article-p)))
6916         ;; Select quietly.
6917         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6918             (gnus-summary-exit)
6919           (gnus-message 7 "No more%s articles (%s)..."
6920                         (if unread " unread" "")
6921                         (if group (concat "selecting " group)
6922                           "exiting"))
6923           (gnus-summary-next-group nil group backward)))
6924        (t
6925         (when (gnus-key-press-event-p last-input-event)
6926           (gnus-summary-walk-group-buffer
6927            gnus-newsgroup-name cmd unread backward point))))))))
6928
6929 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6930   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6931                       (?\C-p (gnus-group-prev-unread-group 1))))
6932         (cursor-in-echo-area t)
6933         keve key group ended prompt)
6934     (save-excursion
6935       (set-buffer gnus-group-buffer)
6936       (goto-char start)
6937       (setq group
6938             (if (eq gnus-keep-same-level 'best)
6939                 (gnus-summary-best-group gnus-newsgroup-name)
6940               (gnus-summary-search-group backward gnus-keep-same-level))))
6941     (while (not ended)
6942       (setq prompt
6943             (format
6944              "No more%s articles%s " (if unread " unread" "")
6945              (if (and group
6946                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6947                  (format " (Type %s for %s [%s])"
6948                          (single-key-description cmd) group
6949                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6950                (format " (Type %s to exit %s)"
6951                        (single-key-description cmd)
6952                        gnus-newsgroup-name))))
6953       ;; Confirm auto selection.
6954       (setq key (car (setq keve (gnus-read-event-char prompt)))
6955             ended t)
6956       (cond
6957        ((assq key keystrokes)
6958         (let ((obuf (current-buffer)))
6959           (switch-to-buffer gnus-group-buffer)
6960           (when group
6961             (gnus-group-jump-to-group group))
6962           (eval (cadr (assq key keystrokes)))
6963           (setq group (gnus-group-group-name))
6964           (switch-to-buffer obuf))
6965         (setq ended nil))
6966        ((equal key cmd)
6967         (if (or (not group)
6968                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6969             (gnus-summary-exit)
6970           (gnus-summary-next-group nil group backward)))
6971        (t
6972         (push (cdr keve) unread-command-events))))))
6973
6974 (defun gnus-summary-next-unread-article ()
6975   "Select unread article after current one."
6976   (interactive)
6977   (gnus-summary-next-article
6978    (or (not (eq gnus-summary-goto-unread 'never))
6979        (gnus-summary-last-article-p (gnus-summary-article-number)))
6980    (and gnus-auto-select-same
6981         (gnus-summary-article-subject))))
6982
6983 (defun gnus-summary-prev-article (&optional unread subject)
6984   "Select the article after the current one.
6985 If UNREAD is non-nil, only unread articles are selected."
6986   (interactive "P")
6987   (gnus-summary-next-article unread subject t))
6988
6989 (defun gnus-summary-prev-unread-article ()
6990   "Select unread article before current one."
6991   (interactive)
6992   (gnus-summary-prev-article
6993    (or (not (eq gnus-summary-goto-unread 'never))
6994        (gnus-summary-first-article-p (gnus-summary-article-number)))
6995    (and gnus-auto-select-same
6996         (gnus-summary-article-subject))))
6997
6998 (defun gnus-summary-next-page (&optional lines circular stop)
6999   "Show next page of the selected article.
7000 If at the end of the current article, select the next article.
7001 LINES says how many lines should be scrolled up.
7002
7003 If CIRCULAR is non-nil, go to the start of the article instead of
7004 selecting the next article when reaching the end of the current
7005 article.
7006
7007 If STOP is non-nil, just stop when reaching the end of the message."
7008   (interactive "P")
7009   (setq gnus-summary-buffer (current-buffer))
7010   (gnus-set-global-variables)
7011   (let ((article (gnus-summary-article-number))
7012         (article-window (get-buffer-window gnus-article-buffer t))
7013         endp)
7014     ;; If the buffer is empty, we have no article.
7015     (unless article
7016       (error "No article to select"))
7017     (gnus-configure-windows 'article)
7018     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7019         (if (and (eq gnus-summary-goto-unread 'never)
7020                  (not (gnus-summary-last-article-p article)))
7021             (gnus-summary-next-article)
7022           (gnus-summary-next-unread-article))
7023       (if (or (null gnus-current-article)
7024               (null gnus-article-current)
7025               (/= article (cdr gnus-article-current))
7026               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7027           ;; Selected subject is different from current article's.
7028           (gnus-summary-display-article article)
7029         (when article-window
7030           (gnus-eval-in-buffer-window gnus-article-buffer
7031             (setq endp (gnus-article-next-page lines)))
7032           (when endp
7033             (cond (stop
7034                    (gnus-message 3 "End of message"))
7035                   (circular
7036                    (gnus-summary-beginning-of-article))
7037                   (lines
7038                    (gnus-message 3 "End of message"))
7039                   ((null lines)
7040                    (if (and (eq gnus-summary-goto-unread 'never)
7041                             (not (gnus-summary-last-article-p article)))
7042                        (gnus-summary-next-article)
7043                      (gnus-summary-next-unread-article))))))))
7044     (gnus-summary-recenter)
7045     (gnus-summary-position-point)))
7046
7047 (defun gnus-summary-prev-page (&optional lines move)
7048   "Show previous page of selected article.
7049 Argument LINES specifies lines to be scrolled down.
7050 If MOVE, move to the previous unread article if point is at
7051 the beginning of the buffer."
7052   (interactive "P")
7053   (let ((article (gnus-summary-article-number))
7054         (article-window (get-buffer-window gnus-article-buffer t))
7055         endp)
7056     (gnus-configure-windows 'article)
7057     (if (or (null gnus-current-article)
7058             (null gnus-article-current)
7059             (/= article (cdr gnus-article-current))
7060             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7061         ;; Selected subject is different from current article's.
7062         (gnus-summary-display-article article)
7063       (gnus-summary-recenter)
7064       (when article-window
7065         (gnus-eval-in-buffer-window gnus-article-buffer
7066           (setq endp (gnus-article-prev-page lines)))
7067         (when (and move endp)
7068           (cond (lines
7069                  (gnus-message 3 "Beginning of message"))
7070                 ((null lines)
7071                  (if (and (eq gnus-summary-goto-unread 'never)
7072                           (not (gnus-summary-first-article-p article)))
7073                      (gnus-summary-prev-article)
7074                    (gnus-summary-prev-unread-article))))))))
7075   (gnus-summary-position-point))
7076
7077 (defun gnus-summary-prev-page-or-article (&optional lines)
7078   "Show previous page of selected article.
7079 Argument LINES specifies lines to be scrolled down.
7080 If at the beginning of the article, go to the next article."
7081   (interactive "P")
7082   (gnus-summary-prev-page lines t))
7083
7084 (defun gnus-summary-scroll-up (lines)
7085   "Scroll up (or down) one line current article.
7086 Argument LINES specifies lines to be scrolled up (or down if negative)."
7087   (interactive "p")
7088   (gnus-configure-windows 'article)
7089   (gnus-summary-show-thread)
7090   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7091     (gnus-eval-in-buffer-window gnus-article-buffer
7092       (cond ((> lines 0)
7093              (when (gnus-article-next-page lines)
7094                (gnus-message 3 "End of message")))
7095             ((< lines 0)
7096              (gnus-article-prev-page (- lines))))))
7097   (gnus-summary-recenter)
7098   (gnus-summary-position-point))
7099
7100 (defun gnus-summary-scroll-down (lines)
7101   "Scroll down (or up) one line current article.
7102 Argument LINES specifies lines to be scrolled down (or up if negative)."
7103   (interactive "p")
7104   (gnus-summary-scroll-up (- lines)))
7105
7106 (defun gnus-summary-next-same-subject ()
7107   "Select next article which has the same subject as current one."
7108   (interactive)
7109   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7110
7111 (defun gnus-summary-prev-same-subject ()
7112   "Select previous article which has the same subject as current one."
7113   (interactive)
7114   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7115
7116 (defun gnus-summary-next-unread-same-subject ()
7117   "Select next unread article which has the same subject as current one."
7118   (interactive)
7119   (gnus-summary-next-article t (gnus-summary-article-subject)))
7120
7121 (defun gnus-summary-prev-unread-same-subject ()
7122   "Select previous unread article which has the same subject as current one."
7123   (interactive)
7124   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7125
7126 (defun gnus-summary-first-unread-article ()
7127   "Select the first unread article.
7128 Return nil if there are no unread articles."
7129   (interactive)
7130   (prog1
7131       (when (gnus-summary-first-subject t)
7132         (gnus-summary-show-thread)
7133         (gnus-summary-first-subject t)
7134         (gnus-summary-display-article (gnus-summary-article-number)))
7135     (gnus-summary-position-point)))
7136
7137 (defun gnus-summary-first-unread-subject ()
7138   "Place the point on the subject line of the first unread article.
7139 Return nil if there are no unread articles."
7140   (interactive)
7141   (prog1
7142       (when (gnus-summary-first-subject t)
7143         (gnus-summary-show-thread)
7144         (gnus-summary-first-subject t))
7145     (gnus-summary-position-point)))
7146
7147 (defun gnus-summary-first-unseen-subject ()
7148   "Place the point on the subject line of the first unseen article.
7149 Return nil if there are no unseen articles."
7150   (interactive)
7151   (prog1
7152       (when (gnus-summary-first-subject t t t)
7153         (gnus-summary-show-thread)
7154         (gnus-summary-first-subject t t t))
7155     (gnus-summary-position-point)))
7156
7157 (defun gnus-summary-first-unseen-or-unread-subject ()
7158   "Place the point on the subject line of the first unseen article.
7159 Return nil if there are no unseen articles."
7160   (interactive)
7161   (prog1
7162       (unless (when (gnus-summary-first-subject t t t)
7163                 (gnus-summary-show-thread)
7164                 (gnus-summary-first-subject t t t))
7165         (when (gnus-summary-first-subject t)
7166           (gnus-summary-show-thread)
7167           (gnus-summary-first-subject t)))
7168     (gnus-summary-position-point)))
7169
7170 (defun gnus-summary-first-article ()
7171   "Select the first article.
7172 Return nil if there are no articles."
7173   (interactive)
7174   (prog1
7175       (when (gnus-summary-first-subject)
7176         (gnus-summary-show-thread)
7177         (gnus-summary-first-subject)
7178         (gnus-summary-display-article (gnus-summary-article-number)))
7179     (gnus-summary-position-point)))
7180
7181 (defun gnus-summary-best-unread-article (&optional arg)
7182   "Select the unread article with the highest score.
7183 If given a prefix argument, select the next unread article that has a
7184 score higher than the default score."
7185   (interactive "P")
7186   (let ((article (if arg
7187                      (gnus-summary-better-unread-subject)
7188                    (gnus-summary-best-unread-subject))))
7189     (if article
7190         (gnus-summary-goto-article article)
7191       (error "No unread articles"))))
7192
7193 (defun gnus-summary-best-unread-subject ()
7194   "Select the unread subject with the highest score."
7195   (interactive)
7196   (let ((best -1000000)
7197         (data gnus-newsgroup-data)
7198         article score)
7199     (while data
7200       (and (gnus-data-unread-p (car data))
7201            (> (setq score
7202                     (gnus-summary-article-score (gnus-data-number (car data))))
7203               best)
7204            (setq best score
7205                  article (gnus-data-number (car data))))
7206       (setq data (cdr data)))
7207     (when article
7208       (gnus-summary-goto-subject article))
7209     (gnus-summary-position-point)
7210     article))
7211
7212 (defun gnus-summary-better-unread-subject ()
7213   "Select the first unread subject that has a score over the default score."
7214   (interactive)
7215   (let ((data gnus-newsgroup-data)
7216         article score)
7217     (while (and (setq article (gnus-data-number (car data)))
7218                 (or (gnus-data-read-p (car data))
7219                     (not (> (gnus-summary-article-score article)
7220                             gnus-summary-default-score))))
7221       (setq data (cdr data)))
7222     (when article
7223       (gnus-summary-goto-subject article))
7224     (gnus-summary-position-point)
7225     article))
7226
7227 (defun gnus-summary-last-subject ()
7228   "Go to the last displayed subject line in the group."
7229   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7230     (when article
7231       (gnus-summary-goto-subject article))))
7232
7233 (defun gnus-summary-goto-article (article &optional all-headers force)
7234   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7235 If ALL-HEADERS is non-nil, no header lines are hidden.
7236 If FORCE, go to the article even if it isn't displayed.  If FORCE
7237 is a number, it is the line the article is to be displayed on."
7238   (interactive
7239    (list
7240     (completing-read
7241      "Article number or Message-ID: "
7242      (mapcar (lambda (number) (list (int-to-string number)))
7243              gnus-newsgroup-limit))
7244     current-prefix-arg
7245     t))
7246   (prog1
7247       (if (and (stringp article)
7248                (string-match "@" article))
7249           (gnus-summary-refer-article article)
7250         (when (stringp article)
7251           (setq article (string-to-number article)))
7252         (if (gnus-summary-goto-subject article force)
7253             (gnus-summary-display-article article all-headers)
7254           (gnus-message 4 "Couldn't go to article %s" article) nil))
7255     (gnus-summary-position-point)))
7256
7257 (defun gnus-summary-goto-last-article ()
7258   "Go to the previously read article."
7259   (interactive)
7260   (prog1
7261       (when gnus-last-article
7262         (gnus-summary-goto-article gnus-last-article nil t))
7263     (gnus-summary-position-point)))
7264
7265 (defun gnus-summary-pop-article (number)
7266   "Pop one article off the history and go to the previous.
7267 NUMBER articles will be popped off."
7268   (interactive "p")
7269   (let (to)
7270     (setq gnus-newsgroup-history
7271           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7272     (if to
7273         (gnus-summary-goto-article (car to) nil t)
7274       (error "Article history empty")))
7275   (gnus-summary-position-point))
7276
7277 ;; Summary commands and functions for limiting the summary buffer.
7278
7279 (defun gnus-summary-limit-to-articles (n)
7280   "Limit the summary buffer to the next N articles.
7281 If not given a prefix, use the process marked articles instead."
7282   (interactive "P")
7283   (prog1
7284       (let ((articles (gnus-summary-work-articles n)))
7285         (setq gnus-newsgroup-processable nil)
7286         (gnus-summary-limit articles))
7287     (gnus-summary-position-point)))
7288
7289 (defun gnus-summary-pop-limit (&optional total)
7290   "Restore the previous limit.
7291 If given a prefix, remove all limits."
7292   (interactive "P")
7293   (when total
7294     (setq gnus-newsgroup-limits
7295           (list (mapcar (lambda (h) (mail-header-number h))
7296                         gnus-newsgroup-headers))))
7297   (unless gnus-newsgroup-limits
7298     (error "No limit to pop"))
7299   (prog1
7300       (gnus-summary-limit nil 'pop)
7301     (gnus-summary-position-point)))
7302
7303 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7304   "Limit the summary buffer to articles that have subjects that match a regexp.
7305 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7306   (interactive
7307    (list (read-string (if current-prefix-arg
7308                           "Exclude subject (regexp): "
7309                         "Limit to subject (regexp): "))
7310          nil current-prefix-arg))
7311   (unless header
7312     (setq header "subject"))
7313   (when (not (equal "" subject))
7314     (prog1
7315         (let ((articles (gnus-summary-find-matching
7316                          (or header "subject") subject 'all nil nil
7317                          not-matching)))
7318           (unless articles
7319             (error "Found no matches for \"%s\"" subject))
7320           (gnus-summary-limit articles))
7321       (gnus-summary-position-point))))
7322
7323 (defun gnus-summary-limit-to-author (from &optional not-matching)
7324   "Limit the summary buffer to articles that have authors that match a regexp.
7325 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7326   (interactive
7327    (list (read-string (if current-prefix-arg
7328                           "Exclude author (regexp): "
7329                         "Limit to author (regexp): "))
7330          current-prefix-arg))
7331   (gnus-summary-limit-to-subject from "from" not-matching))
7332
7333 (defun gnus-summary-limit-to-age (age &optional younger-p)
7334   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7335 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7336 articles that are younger than AGE days."
7337   (interactive
7338    (let ((younger current-prefix-arg)
7339          (days-got nil)
7340          days)
7341      (while (not days-got)
7342        (setq days (if younger
7343                       (read-string "Limit to articles younger than (in days, older when negative): ")
7344                     (read-string
7345                      "Limit to articles older than (in days, younger when negative): ")))
7346        (when (> (length days) 0)
7347          (setq days (read days)))
7348        (if (numberp days)
7349            (progn
7350              (setq days-got t)
7351              (if (< days 0)
7352                  (progn
7353                    (setq younger (not younger))
7354                    (setq days (* days -1)))))
7355          (message "Please enter a number.")
7356          (sleep-for 1)))
7357      (list days younger)))
7358   (prog1
7359       (let ((data gnus-newsgroup-data)
7360             (cutoff (days-to-time age))
7361             articles d date is-younger)
7362         (while (setq d (pop data))
7363           (when (and (vectorp (gnus-data-header d))
7364                      (setq date (mail-header-date (gnus-data-header d))))
7365             (setq is-younger (time-less-p
7366                               (time-since (condition-case ()
7367                                               (date-to-time date)
7368                                             (error '(0 0))))
7369                               cutoff))
7370             (when (if younger-p
7371                       is-younger
7372                     (not is-younger))
7373               (push (gnus-data-number d) articles))))
7374         (gnus-summary-limit (nreverse articles)))
7375     (gnus-summary-position-point)))
7376
7377 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7378   "Limit the summary buffer to articles that match an 'extra' header."
7379   (interactive
7380    (let ((header
7381           (intern
7382            (gnus-completing-read-with-default
7383             (symbol-name (car gnus-extra-headers))
7384             (if current-prefix-arg
7385                 "Exclude extra header:"
7386               "Limit extra header:")
7387             (mapcar (lambda (x)
7388                       (cons (symbol-name x) x))
7389                     gnus-extra-headers)
7390             nil
7391             t))))
7392      (list header
7393            (read-string (format "%s header %s (regexp): "
7394                                 (if current-prefix-arg "Exclude" "Limit to")
7395                                 header))
7396            current-prefix-arg)))
7397   (when (not (equal "" regexp))
7398     (prog1
7399         (let ((articles (gnus-summary-find-matching
7400                          (cons 'extra header) regexp 'all nil nil
7401                          not-matching)))
7402           (unless articles
7403             (error "Found no matches for \"%s\"" regexp))
7404           (gnus-summary-limit articles))
7405       (gnus-summary-position-point))))
7406
7407 (defun gnus-summary-limit-to-display-predicate ()
7408   "Limit the summary buffer to the predicated in the `display' group parameter."
7409   (interactive)
7410   (unless gnus-newsgroup-display
7411     (error "There is no `display' group parameter"))
7412   (let (articles)
7413     (dolist (number gnus-newsgroup-articles)
7414       (when (funcall gnus-newsgroup-display)
7415         (push number articles)))
7416     (gnus-summary-limit articles))
7417   (gnus-summary-position-point))
7418
7419 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7420 (make-obsolete
7421  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7422
7423 (defun gnus-summary-limit-to-unread (&optional all)
7424   "Limit the summary buffer to articles that are not marked as read.
7425 If ALL is non-nil, limit strictly to unread articles."
7426   (interactive "P")
7427   (if all
7428       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7429     (gnus-summary-limit-to-marks
7430      ;; Concat all the marks that say that an article is read and have
7431      ;; those removed.
7432      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7433            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7434            gnus-low-score-mark gnus-expirable-mark
7435            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7436            gnus-duplicate-mark gnus-souped-mark)
7437      'reverse)))
7438
7439 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7440 (make-obsolete 'gnus-summary-delete-marked-with
7441                'gnus-summary-limit-exclude-marks)
7442
7443 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7444   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7445 If REVERSE, limit the summary buffer to articles that are marked
7446 with MARKS.  MARKS can either be a string of marks or a list of marks.
7447 Returns how many articles were removed."
7448   (interactive "sMarks: ")
7449   (gnus-summary-limit-to-marks marks t))
7450
7451 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7452   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7453 If REVERSE (the prefix), limit the summary buffer to articles that are
7454 not marked with MARKS.  MARKS can either be a string of marks or a
7455 list of marks.
7456 Returns how many articles were removed."
7457   (interactive "sMarks: \nP")
7458   (prog1
7459       (let ((data gnus-newsgroup-data)
7460             (marks (if (listp marks) marks
7461                      (append marks nil))) ; Transform to list.
7462             articles)
7463         (while data
7464           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7465                   (memq (gnus-data-mark (car data)) marks))
7466             (push (gnus-data-number (car data)) articles))
7467           (setq data (cdr data)))
7468         (gnus-summary-limit articles))
7469     (gnus-summary-position-point)))
7470
7471 (defun gnus-summary-limit-to-score (score)
7472   "Limit to articles with score at or above SCORE."
7473   (interactive "NLimit to articles with score of at least: ")
7474   (let ((data gnus-newsgroup-data)
7475         articles)
7476     (while data
7477       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7478                 score)
7479         (push (gnus-data-number (car data)) articles))
7480       (setq data (cdr data)))
7481     (prog1
7482         (gnus-summary-limit articles)
7483       (gnus-summary-position-point))))
7484
7485 (defun gnus-summary-limit-to-unseen ()
7486   "Limit to unseen articles."
7487   (interactive)
7488   (prog1
7489       (gnus-summary-limit gnus-newsgroup-unseen)
7490     (gnus-summary-position-point)))
7491
7492 (defun gnus-summary-limit-include-thread (id)
7493   "Display all the hidden articles that is in the thread with ID in it.
7494 When called interactively, ID is the Message-ID of the current
7495 article."
7496   (interactive (list (mail-header-id (gnus-summary-article-header))))
7497   (let ((articles (gnus-articles-in-thread
7498                    (gnus-id-to-thread (gnus-root-id id)))))
7499     (prog1
7500         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7501       (gnus-summary-limit-include-matching-articles
7502        "subject"
7503        (regexp-quote (gnus-simplify-subject-re
7504                       (mail-header-subject (gnus-id-to-header id)))))
7505       (gnus-summary-position-point))))
7506
7507 (defun gnus-summary-limit-include-matching-articles (header regexp)
7508   "Display all the hidden articles that have HEADERs that match REGEXP."
7509   (interactive (list (read-string "Match on header: ")
7510                      (read-string "Regexp: ")))
7511   (let ((articles (gnus-find-matching-articles header regexp)))
7512     (prog1
7513         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7514       (gnus-summary-position-point))))
7515
7516 (defun gnus-summary-insert-dormant-articles ()
7517   "Insert all the dormat articles for this group into the current buffer."
7518   (interactive)
7519   (let ((gnus-verbose (max 6 gnus-verbose)))
7520     (if (not gnus-newsgroup-dormant)
7521         (gnus-message 3 "No cached articles for this group")
7522       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7523
7524 (defun gnus-summary-limit-include-dormant ()
7525   "Display all the hidden articles that are marked as dormant.
7526 Note that this command only works on a subset of the articles currently
7527 fetched for this group."
7528   (interactive)
7529   (unless gnus-newsgroup-dormant
7530     (error "There are no dormant articles in this group"))
7531   (prog1
7532       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7533     (gnus-summary-position-point)))
7534
7535 (defun gnus-summary-limit-exclude-dormant ()
7536   "Hide all dormant articles."
7537   (interactive)
7538   (prog1
7539       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7540     (gnus-summary-position-point)))
7541
7542 (defun gnus-summary-limit-exclude-childless-dormant ()
7543   "Hide all dormant articles that have no children."
7544   (interactive)
7545   (let ((data (gnus-data-list t))
7546         articles d children)
7547     ;; Find all articles that are either not dormant or have
7548     ;; children.
7549     (while (setq d (pop data))
7550       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7551                 (and (setq children
7552                            (gnus-article-children (gnus-data-number d)))
7553                      (let (found)
7554                        (while children
7555                          (when (memq (car children) articles)
7556                            (setq children nil
7557                                  found t))
7558                          (pop children))
7559                        found)))
7560         (push (gnus-data-number d) articles)))
7561     ;; Do the limiting.
7562     (prog1
7563         (gnus-summary-limit articles)
7564       (gnus-summary-position-point))))
7565
7566 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7567   "Mark all unread excluded articles as read.
7568 If ALL, mark even excluded ticked and dormants as read."
7569   (interactive "P")
7570   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7571   (let ((articles (gnus-sorted-ndifference
7572                    (sort
7573                     (mapcar (lambda (h) (mail-header-number h))
7574                             gnus-newsgroup-headers)
7575                     '<)
7576                    gnus-newsgroup-limit))
7577         article)
7578     (setq gnus-newsgroup-unreads
7579           (gnus-sorted-intersection gnus-newsgroup-unreads
7580                                     gnus-newsgroup-limit))
7581     (if all
7582         (setq gnus-newsgroup-dormant nil
7583               gnus-newsgroup-marked nil
7584               gnus-newsgroup-reads
7585               (nconc
7586                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7587                gnus-newsgroup-reads))
7588       (while (setq article (pop articles))
7589         (unless (or (memq article gnus-newsgroup-dormant)
7590                     (memq article gnus-newsgroup-marked))
7591           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7592
7593 (defun gnus-summary-limit (articles &optional pop)
7594   (if pop
7595       ;; We pop the previous limit off the stack and use that.
7596       (setq articles (car gnus-newsgroup-limits)
7597             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7598     ;; We use the new limit, so we push the old limit on the stack.
7599     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7600   ;; Set the limit.
7601   (setq gnus-newsgroup-limit articles)
7602   (let ((total (length gnus-newsgroup-data))
7603         (data (gnus-data-find-list (gnus-summary-article-number)))
7604         (gnus-summary-mark-below nil)   ; Inhibit this.
7605         found)
7606     ;; This will do all the work of generating the new summary buffer
7607     ;; according to the new limit.
7608     (gnus-summary-prepare)
7609     ;; Hide any threads, possibly.
7610     (gnus-summary-maybe-hide-threads)
7611     ;; Try to return to the article you were at, or one in the
7612     ;; neighborhood.
7613     (when data
7614       ;; We try to find some article after the current one.
7615       (while data
7616         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7617           (setq data nil
7618                 found t))
7619         (setq data (cdr data))))
7620     (unless found
7621       ;; If there is no data, that means that we were after the last
7622       ;; article.  The same goes when we can't find any articles
7623       ;; after the current one.
7624       (goto-char (point-max))
7625       (gnus-summary-find-prev))
7626     (gnus-set-mode-line 'summary)
7627     ;; We return how many articles were removed from the summary
7628     ;; buffer as a result of the new limit.
7629     (- total (length gnus-newsgroup-data))))
7630
7631 (defsubst gnus-invisible-cut-children (threads)
7632   (let ((num 0))
7633     (while threads
7634       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7635         (incf num))
7636       (pop threads))
7637     (< num 2)))
7638
7639 (defsubst gnus-cut-thread (thread)
7640   "Go forwards in the thread until we find an article that we want to display."
7641   (when (or (eq gnus-fetch-old-headers 'some)
7642             (eq gnus-fetch-old-headers 'invisible)
7643             (numberp gnus-fetch-old-headers)
7644             (eq gnus-build-sparse-threads 'some)
7645             (eq gnus-build-sparse-threads 'more))
7646     ;; Deal with old-fetched headers and sparse threads.
7647     (while (and
7648             thread
7649             (or
7650              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7651              (gnus-summary-article-ancient-p
7652               (mail-header-number (car thread))))
7653             (if (or (<= (length (cdr thread)) 1)
7654                     (eq gnus-fetch-old-headers 'invisible))
7655                 (setq gnus-newsgroup-limit
7656                       (delq (mail-header-number (car thread))
7657                             gnus-newsgroup-limit)
7658                       thread (cadr thread))
7659               (when (gnus-invisible-cut-children (cdr thread))
7660                 (let ((th (cdr thread)))
7661                   (while th
7662                     (if (memq (mail-header-number (caar th))
7663                               gnus-newsgroup-limit)
7664                         (setq thread (car th)
7665                               th nil)
7666                       (setq th (cdr th))))))))))
7667   thread)
7668
7669 (defun gnus-cut-threads (threads)
7670   "Cut off all uninteresting articles from the beginning of threads."
7671   (when (or (eq gnus-fetch-old-headers 'some)
7672             (eq gnus-fetch-old-headers 'invisible)
7673             (numberp gnus-fetch-old-headers)
7674             (eq gnus-build-sparse-threads 'some)
7675             (eq gnus-build-sparse-threads 'more))
7676     (let ((th threads))
7677       (while th
7678         (setcar th (gnus-cut-thread (car th)))
7679         (setq th (cdr th)))))
7680   ;; Remove nixed out threads.
7681   (delq nil threads))
7682
7683 (defun gnus-summary-initial-limit (&optional show-if-empty)
7684   "Figure out what the initial limit is supposed to be on group entry.
7685 This entails weeding out unwanted dormants, low-scored articles,
7686 fetch-old-headers verbiage, and so on."
7687   ;; Most groups have nothing to remove.
7688   (if (or gnus-inhibit-limiting
7689           (and (null gnus-newsgroup-dormant)
7690                (eq gnus-newsgroup-display 'gnus-not-ignore)
7691                (not (eq gnus-fetch-old-headers 'some))
7692                (not (numberp gnus-fetch-old-headers))
7693                (not (eq gnus-fetch-old-headers 'invisible))
7694                (null gnus-summary-expunge-below)
7695                (not (eq gnus-build-sparse-threads 'some))
7696                (not (eq gnus-build-sparse-threads 'more))
7697                (null gnus-thread-expunge-below)
7698                (not gnus-use-nocem)))
7699       ()                                ; Do nothing.
7700     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7701     (setq gnus-newsgroup-limit nil)
7702     (mapatoms
7703      (lambda (node)
7704        (unless (car (symbol-value node))
7705          ;; These threads have no parents -- they are roots.
7706          (let ((nodes (cdr (symbol-value node)))
7707                thread)
7708            (while nodes
7709              (if (and gnus-thread-expunge-below
7710                       (< (gnus-thread-total-score (car nodes))
7711                          gnus-thread-expunge-below))
7712                  (gnus-expunge-thread (pop nodes))
7713                (setq thread (pop nodes))
7714                (gnus-summary-limit-children thread))))))
7715      gnus-newsgroup-dependencies)
7716     ;; If this limitation resulted in an empty group, we might
7717     ;; pop the previous limit and use it instead.
7718     (when (and (not gnus-newsgroup-limit)
7719                show-if-empty)
7720       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7721     gnus-newsgroup-limit))
7722
7723 (defun gnus-summary-limit-children (thread)
7724   "Return 1 if this subthread is visible and 0 if it is not."
7725   ;; First we get the number of visible children to this thread.  This
7726   ;; is done by recursing down the thread using this function, so this
7727   ;; will really go down to a leaf article first, before slowly
7728   ;; working its way up towards the root.
7729   (when thread
7730     (let* ((max-lisp-eval-depth 5000)
7731            (children
7732            (if (cdr thread)
7733                (apply '+ (mapcar 'gnus-summary-limit-children
7734                                  (cdr thread)))
7735              0))
7736           (number (mail-header-number (car thread)))
7737           score)
7738       (if (and
7739            (not (memq number gnus-newsgroup-marked))
7740            (or
7741             ;; If this article is dormant and has absolutely no visible
7742             ;; children, then this article isn't visible.
7743             (and (memq number gnus-newsgroup-dormant)
7744                  (zerop children))
7745             ;; If this is "fetch-old-headered" and there is no
7746             ;; visible children, then we don't want this article.
7747             (and (or (eq gnus-fetch-old-headers 'some)
7748                      (numberp gnus-fetch-old-headers))
7749                  (gnus-summary-article-ancient-p number)
7750                  (zerop children))
7751             ;; If this is "fetch-old-headered" and `invisible', then
7752             ;; we don't want this article.
7753             (and (eq gnus-fetch-old-headers 'invisible)
7754                  (gnus-summary-article-ancient-p number))
7755             ;; If this is a sparsely inserted article with no children,
7756             ;; we don't want it.
7757             (and (eq gnus-build-sparse-threads 'some)
7758                  (gnus-summary-article-sparse-p number)
7759                  (zerop children))
7760             ;; If we use expunging, and this article is really
7761             ;; low-scored, then we don't want this article.
7762             (when (and gnus-summary-expunge-below
7763                        (< (setq score
7764                                 (or (cdr (assq number gnus-newsgroup-scored))
7765                                     gnus-summary-default-score))
7766                           gnus-summary-expunge-below))
7767               ;; We increase the expunge-tally here, but that has
7768               ;; nothing to do with the limits, really.
7769               (incf gnus-newsgroup-expunged-tally)
7770               ;; We also mark as read here, if that's wanted.
7771               (when (and gnus-summary-mark-below
7772                          (< score gnus-summary-mark-below))
7773                 (setq gnus-newsgroup-unreads
7774                       (delq number gnus-newsgroup-unreads))
7775                 (if gnus-newsgroup-auto-expire
7776                     (push number gnus-newsgroup-expirable)
7777                   (push (cons number gnus-low-score-mark)
7778                         gnus-newsgroup-reads)))
7779               t)
7780             ;; Do the `display' group parameter.
7781             (and gnus-newsgroup-display
7782                  (not (funcall gnus-newsgroup-display)))
7783             ;; Check NoCeM things.
7784             (if (and gnus-use-nocem
7785                      (gnus-nocem-unwanted-article-p
7786                       (mail-header-id (car thread))))
7787                 (progn
7788                   (setq gnus-newsgroup-unreads
7789                         (delq number gnus-newsgroup-unreads))
7790                   t))))
7791           ;; Nope, invisible article.
7792           0
7793         ;; Ok, this article is to be visible, so we add it to the limit
7794         ;; and return 1.
7795         (push number gnus-newsgroup-limit)
7796         1))))
7797
7798 (defun gnus-expunge-thread (thread)
7799   "Mark all articles in THREAD as read."
7800   (let* ((number (mail-header-number (car thread))))
7801     (incf gnus-newsgroup-expunged-tally)
7802     ;; We also mark as read here, if that's wanted.
7803     (setq gnus-newsgroup-unreads
7804           (delq number gnus-newsgroup-unreads))
7805     (if gnus-newsgroup-auto-expire
7806         (push number gnus-newsgroup-expirable)
7807       (push (cons number gnus-low-score-mark)
7808             gnus-newsgroup-reads)))
7809   ;; Go recursively through all subthreads.
7810   (mapcar 'gnus-expunge-thread (cdr thread)))
7811
7812 ;; Summary article oriented commands
7813
7814 (defun gnus-summary-refer-parent-article (n)
7815   "Refer parent article N times.
7816 If N is negative, go to ancestor -N instead.
7817 The difference between N and the number of articles fetched is returned."
7818   (interactive "p")
7819   (let ((skip 1)
7820         error header ref)
7821     (when (not (natnump n))
7822       (setq skip (abs n)
7823             n 1))
7824     (while (and (> n 0)
7825                 (not error))
7826       (setq header (gnus-summary-article-header))
7827       (if (and (eq (mail-header-number header)
7828                    (cdr gnus-article-current))
7829                (equal gnus-newsgroup-name
7830                       (car gnus-article-current)))
7831           ;; If we try to find the parent of the currently
7832           ;; displayed article, then we take a look at the actual
7833           ;; References header, since this is slightly more
7834           ;; reliable than the References field we got from the
7835           ;; server.
7836           (save-excursion
7837             (set-buffer gnus-original-article-buffer)
7838             (nnheader-narrow-to-headers)
7839             (unless (setq ref (message-fetch-field "references"))
7840               (setq ref (message-fetch-field "in-reply-to")))
7841             (widen))
7842         (setq ref
7843               ;; It's not the current article, so we take a bet on
7844               ;; the value we got from the server.
7845               (mail-header-references header)))
7846       (if (and ref
7847                (not (equal ref "")))
7848           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7849             (gnus-message 1 "Couldn't find parent"))
7850         (gnus-message 1 "No references in article %d"
7851                       (gnus-summary-article-number))
7852         (setq error t))
7853       (decf n))
7854     (gnus-summary-position-point)
7855     n))
7856
7857 (defun gnus-summary-refer-references ()
7858   "Fetch all articles mentioned in the References header.
7859 Return the number of articles fetched."
7860   (interactive)
7861   (let ((ref (mail-header-references (gnus-summary-article-header)))
7862         (current (gnus-summary-article-number))
7863         (n 0))
7864     (if (or (not ref)
7865             (equal ref ""))
7866         (error "No References in the current article")
7867       ;; For each Message-ID in the References header...
7868       (while (string-match "<[^>]*>" ref)
7869         (incf n)
7870         ;; ... fetch that article.
7871         (gnus-summary-refer-article
7872          (prog1 (match-string 0 ref)
7873            (setq ref (substring ref (match-end 0))))))
7874       (gnus-summary-goto-subject current)
7875       (gnus-summary-position-point)
7876       n)))
7877
7878 (defun gnus-summary-refer-thread (&optional limit)
7879   "Fetch all articles in the current thread.
7880 If LIMIT (the numerical prefix), fetch that many old headers instead
7881 of what's specified by the `gnus-refer-thread-limit' variable."
7882   (interactive "P")
7883   (let ((id (mail-header-id (gnus-summary-article-header)))
7884         (limit (if limit (prefix-numeric-value limit)
7885                  gnus-refer-thread-limit)))
7886     ;; We want to fetch LIMIT *old* headers, but we also have to
7887     ;; re-fetch all the headers in the current buffer, because many of
7888     ;; them may be undisplayed.  So we adjust LIMIT.
7889     (when (numberp limit)
7890       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7891     (unless (eq gnus-fetch-old-headers 'invisible)
7892       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7893       ;; Retrieve the headers and read them in.
7894       (if (eq (gnus-retrieve-headers
7895                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7896               'nov)
7897           (gnus-build-all-threads)
7898         (error "Can't fetch thread from backends that don't support NOV"))
7899       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7900     (gnus-summary-limit-include-thread id)))
7901
7902 (defun gnus-summary-refer-article (message-id)
7903   "Fetch an article specified by MESSAGE-ID."
7904   (interactive "sMessage-ID: ")
7905   (when (and (stringp message-id)
7906              (not (zerop (length message-id))))
7907     ;; Construct the correct Message-ID if necessary.
7908     ;; Suggested by tale@pawl.rpi.edu.
7909     (unless (string-match "^<" message-id)
7910       (setq message-id (concat "<" message-id)))
7911     (unless (string-match ">$" message-id)
7912       (setq message-id (concat message-id ">")))
7913     (let* ((header (gnus-id-to-header message-id))
7914            (sparse (and header
7915                         (gnus-summary-article-sparse-p
7916                          (mail-header-number header))
7917                         (memq (mail-header-number header)
7918                               gnus-newsgroup-limit)))
7919            number)
7920       (cond
7921        ;; If the article is present in the buffer we just go to it.
7922        ((and header
7923              (or (not (gnus-summary-article-sparse-p
7924                        (mail-header-number header)))
7925                  sparse))
7926         (prog1
7927             (gnus-summary-goto-article
7928              (mail-header-number header) nil t)
7929           (when sparse
7930             (gnus-summary-update-article (mail-header-number header)))))
7931        (t
7932         ;; We fetch the article.
7933         (catch 'found
7934           (dolist (gnus-override-method (gnus-refer-article-methods))
7935             (gnus-check-server gnus-override-method)
7936             ;; Fetch the header, and display the article.
7937             (when (setq number (gnus-summary-insert-subject message-id))
7938               (gnus-summary-select-article nil nil nil number)
7939               (throw 'found t)))
7940           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7941
7942 (defun gnus-refer-article-methods ()
7943   "Return a list of referable methods."
7944   (cond
7945    ;; No method, so we default to current and native.
7946    ((null gnus-refer-article-method)
7947     (list gnus-current-select-method gnus-select-method))
7948    ;; Current.
7949    ((eq 'current gnus-refer-article-method)
7950     (list gnus-current-select-method))
7951    ;; List of select methods.
7952    ((not (and (symbolp (car gnus-refer-article-method))
7953               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7954     (let (out)
7955       (dolist (method gnus-refer-article-method)
7956         (push (if (eq 'current method)
7957                   gnus-current-select-method
7958                 method)
7959               out))
7960       (nreverse out)))
7961    ;; One single select method.
7962    (t
7963     (list gnus-refer-article-method))))
7964
7965 (defun gnus-summary-edit-parameters ()
7966   "Edit the group parameters of the current group."
7967   (interactive)
7968   (gnus-group-edit-group gnus-newsgroup-name 'params))
7969
7970 (defun gnus-summary-customize-parameters ()
7971   "Customize the group parameters of the current group."
7972   (interactive)
7973   (gnus-group-customize gnus-newsgroup-name))
7974
7975 (defun gnus-summary-enter-digest-group (&optional force)
7976   "Enter an nndoc group based on the current article.
7977 If FORCE, force a digest interpretation.  If not, try
7978 to guess what the document format is."
7979   (interactive "P")
7980   (let ((conf gnus-current-window-configuration))
7981     (save-excursion
7982       (gnus-summary-select-article))
7983     (setq gnus-current-window-configuration conf)
7984     (let* ((name (format "%s-%d"
7985                          (gnus-group-prefixed-name
7986                           gnus-newsgroup-name (list 'nndoc ""))
7987                          (save-excursion
7988                            (set-buffer gnus-summary-buffer)
7989                            gnus-current-article)))
7990            (ogroup gnus-newsgroup-name)
7991            (params (append (gnus-info-params (gnus-get-info ogroup))
7992                            (list (cons 'to-group ogroup))
7993                            (list (cons 'save-article-group ogroup))))
7994            (case-fold-search t)
7995            (buf (current-buffer))
7996            dig to-address)
7997       (save-excursion
7998         (set-buffer gnus-original-article-buffer)
7999         ;; Have the digest group inherit the main mail address of
8000         ;; the parent article.
8001         (when (setq to-address (or (gnus-fetch-field "reply-to")
8002                                    (gnus-fetch-field "from")))
8003           (setq params (append
8004                         (list (cons 'to-address
8005                                     (funcall gnus-decode-encoded-word-function
8006                                              to-address))))))
8007         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8008         (insert-buffer-substring gnus-original-article-buffer)
8009         ;; Remove lines that may lead nndoc to misinterpret the
8010         ;; document type.
8011         (narrow-to-region
8012          (goto-char (point-min))
8013          (or (search-forward "\n\n" nil t) (point)))
8014         (goto-char (point-min))
8015         (delete-matching-lines "^Path:\\|^From ")
8016         (widen))
8017       (unwind-protect
8018           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8019                     (gnus-newsgroup-ephemeral-ignored-charsets
8020                      gnus-newsgroup-ignored-charsets))
8021                 (gnus-group-read-ephemeral-group
8022                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8023                               (nndoc-article-type
8024                                ,(if force 'mbox 'guess)))
8025                  t nil nil nil
8026                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8027                                                         "ADAPT")))))
8028               ;; Make all postings to this group go to the parent group.
8029               (nconc (gnus-info-params (gnus-get-info name))
8030                      params)
8031             ;; Couldn't select this doc group.
8032             (switch-to-buffer buf)
8033             (gnus-set-global-variables)
8034             (gnus-configure-windows 'summary)
8035             (gnus-message 3 "Article couldn't be entered?"))
8036         (kill-buffer dig)))))
8037
8038 (defun gnus-summary-read-document (n)
8039   "Open a new group based on the current article(s).
8040 This will allow you to read digests and other similar
8041 documents as newsgroups.
8042 Obeys the standard process/prefix convention."
8043   (interactive "P")
8044   (let* ((articles (gnus-summary-work-articles n))
8045          (ogroup gnus-newsgroup-name)
8046          (params (append (gnus-info-params (gnus-get-info ogroup))
8047                          (list (cons 'to-group ogroup))))
8048          article group egroup groups vgroup)
8049     (while (setq article (pop articles))
8050       (setq group (format "%s-%d" gnus-newsgroup-name article))
8051       (gnus-summary-remove-process-mark article)
8052       (when (gnus-summary-display-article article)
8053         (save-excursion
8054           (with-temp-buffer
8055             (insert-buffer-substring gnus-original-article-buffer)
8056             ;; Remove some headers that may lead nndoc to make
8057             ;; the wrong guess.
8058             (message-narrow-to-head)
8059             (goto-char (point-min))
8060             (delete-matching-lines "^\\(Path\\):\\|^From ")
8061             (widen)
8062             (if (setq egroup
8063                       (gnus-group-read-ephemeral-group
8064                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8065                                      (nndoc-article-type guess))
8066                        t nil t))
8067                 (progn
8068             ;; Make all postings to this group go to the parent group.
8069                   (nconc (gnus-info-params (gnus-get-info egroup))
8070                          params)
8071                   (push egroup groups))
8072               ;; Couldn't select this doc group.
8073               (gnus-error 3 "Article couldn't be entered"))))))
8074     ;; Now we have selected all the documents.
8075     (cond
8076      ((not groups)
8077       (error "None of the articles could be interpreted as documents"))
8078      ((gnus-group-read-ephemeral-group
8079        (setq vgroup (format
8080                      "nnvirtual:%s-%s" gnus-newsgroup-name
8081                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8082        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8083        t
8084        (cons (current-buffer) 'summary)))
8085      (t
8086       (error "Couldn't select virtual nndoc group")))))
8087
8088 (defun gnus-summary-isearch-article (&optional regexp-p)
8089   "Do incremental search forward on the current article.
8090 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8091   (interactive "P")
8092   (gnus-summary-select-article)
8093   (gnus-configure-windows 'article)
8094   (gnus-eval-in-buffer-window gnus-article-buffer
8095     (save-restriction
8096       (widen)
8097       (isearch-forward regexp-p))))
8098
8099 (defun gnus-summary-search-article-forward (regexp &optional backward)
8100   "Search for an article containing REGEXP forward.
8101 If BACKWARD, search backward instead."
8102   (interactive
8103    (list (read-string
8104           (format "Search article %s (regexp%s): "
8105                   (if current-prefix-arg "backward" "forward")
8106                   (if gnus-last-search-regexp
8107                       (concat ", default " gnus-last-search-regexp)
8108                     "")))
8109          current-prefix-arg))
8110   (if (string-equal regexp "")
8111       (setq regexp (or gnus-last-search-regexp ""))
8112     (setq gnus-last-search-regexp regexp)
8113     (setq gnus-article-before-search gnus-current-article))
8114   ;; Intentionally set gnus-last-article.
8115   (setq gnus-last-article gnus-article-before-search)
8116   (let ((gnus-last-article gnus-last-article))
8117     (if (gnus-summary-search-article regexp backward)
8118         (gnus-summary-show-thread)
8119       (error "Search failed: \"%s\"" regexp))))
8120
8121 (defun gnus-summary-search-article-backward (regexp)
8122   "Search for an article containing REGEXP backward."
8123   (interactive
8124    (list (read-string
8125           (format "Search article backward (regexp%s): "
8126                   (if gnus-last-search-regexp
8127                       (concat ", default " gnus-last-search-regexp)
8128                     "")))))
8129   (gnus-summary-search-article-forward regexp 'backward))
8130
8131 (defun gnus-summary-search-article (regexp &optional backward)
8132   "Search for an article containing REGEXP.
8133 Optional argument BACKWARD means do search for backward.
8134 `gnus-select-article-hook' is not called during the search."
8135   ;; We have to require this here to make sure that the following
8136   ;; dynamic binding isn't shadowed by autoloading.
8137   (require 'gnus-async)
8138   (require 'gnus-art)
8139   (let ((gnus-select-article-hook nil)  ;Disable hook.
8140         (gnus-article-prepare-hook nil)
8141         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8142         (gnus-use-article-prefetch nil)
8143         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8144         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8145         (sum (current-buffer))
8146         (gnus-display-mime-function nil)
8147         (found nil)
8148         point)
8149     (gnus-save-hidden-threads
8150       (gnus-summary-select-article)
8151       (set-buffer gnus-article-buffer)
8152       (goto-char (window-point (get-buffer-window (current-buffer))))
8153       (when backward
8154         (forward-line -1))
8155       (while (not found)
8156         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8157         (if (if backward
8158                 (re-search-backward regexp nil t)
8159               (re-search-forward regexp nil t))
8160             ;; We found the regexp.
8161             (progn
8162               (setq found 'found)
8163               (beginning-of-line)
8164               (set-window-start
8165                (get-buffer-window (current-buffer))
8166                (point))
8167               (forward-line 1)
8168               (set-window-point
8169                (get-buffer-window (current-buffer))
8170                (point))
8171               (set-buffer sum)
8172               (setq point (point)))
8173           ;; We didn't find it, so we go to the next article.
8174           (set-buffer sum)
8175           (setq found 'not)
8176           (while (eq found 'not)
8177             (if (not (if backward (gnus-summary-find-prev)
8178                        (gnus-summary-find-next)))
8179                 ;; No more articles.
8180                 (setq found t)
8181               ;; Select the next article and adjust point.
8182               (unless (gnus-summary-article-sparse-p
8183                        (gnus-summary-article-number))
8184                 (setq found nil)
8185                 (gnus-summary-select-article)
8186                 (set-buffer gnus-article-buffer)
8187                 (widen)
8188                 (goto-char (if backward (point-max) (point-min))))))))
8189       (gnus-message 7 ""))
8190     ;; Return whether we found the regexp.
8191     (when (eq found 'found)
8192       (goto-char point)
8193       (gnus-summary-show-thread)
8194       (gnus-summary-goto-subject gnus-current-article)
8195       (gnus-summary-position-point)
8196       t)))
8197
8198 (defun gnus-find-matching-articles (header regexp)
8199   "Return a list of all articles that match REGEXP on HEADER.
8200 This search includes all articles in the current group that Gnus has
8201 fetched headers for, whether they are displayed or not."
8202   (let ((articles nil)
8203         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8204         (case-fold-search t))
8205     (dolist (header gnus-newsgroup-headers)
8206       (when (string-match regexp (funcall func header))
8207         (push (mail-header-number header) articles)))
8208     (nreverse articles)))
8209
8210 (defun gnus-summary-find-matching (header regexp &optional backward unread
8211                                           not-case-fold not-matching)
8212   "Return a list of all articles that match REGEXP on HEADER.
8213 The search stars on the current article and goes forwards unless
8214 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8215 If UNREAD is non-nil, only unread articles will
8216 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8217 in the comparisons. If NOT-MATCHING, return a list of all articles that
8218 not match REGEXP on HEADER."
8219   (let ((case-fold-search (not not-case-fold))
8220         articles d func)
8221     (if (consp header)
8222         (if (eq (car header) 'extra)
8223             (setq func
8224                   `(lambda (h)
8225                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8226                          "")))
8227           (error "%s is an invalid header" header))
8228       (unless (fboundp (intern (concat "mail-header-" header)))
8229         (error "%s is not a valid header" header))
8230       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8231     (dolist (d (if (eq backward 'all)
8232                    gnus-newsgroup-data
8233                  (gnus-data-find-list
8234                   (gnus-summary-article-number)
8235                   (gnus-data-list backward))))
8236       (when (and (or (not unread)       ; We want all articles...
8237                      (gnus-data-unread-p d)) ; Or just unreads.
8238                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8239                  (if not-matching
8240                      (not (string-match
8241                            regexp
8242                            (funcall func (gnus-data-header d))))
8243                    (string-match regexp
8244                                  (funcall func (gnus-data-header d)))))
8245         (push (gnus-data-number d) articles))) ; Success!
8246     (nreverse articles)))
8247
8248 (defun gnus-summary-execute-command (header regexp command &optional backward)
8249   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8250 If HEADER is an empty string (or nil), the match is done on the entire
8251 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8252   (interactive
8253    (list (let ((completion-ignore-case t))
8254            (completing-read
8255             "Header name: "
8256             (mapcar (lambda (header) (list (format "%s" header)))
8257                     (append
8258                      '("Number" "Subject" "From" "Lines" "Date"
8259                        "Message-ID" "Xref" "References" "Body")
8260                      gnus-extra-headers))
8261             nil 'require-match))
8262          (read-string "Regexp: ")
8263          (read-key-sequence "Command: ")
8264          current-prefix-arg))
8265   (when (equal header "Body")
8266     (setq header ""))
8267   ;; Hidden thread subtrees must be searched as well.
8268   (gnus-summary-show-all-threads)
8269   ;; We don't want to change current point nor window configuration.
8270   (save-excursion
8271     (save-window-excursion
8272       (let (gnus-visual
8273             gnus-treat-strip-trailing-blank-lines
8274             gnus-treat-strip-leading-blank-lines
8275             gnus-treat-strip-multiple-blank-lines
8276             gnus-treat-hide-boring-headers
8277             gnus-treat-fold-newsgroups
8278             gnus-article-prepare-hook)
8279         (gnus-message 6 "Executing %s..." (key-description command))
8280         ;; We'd like to execute COMMAND interactively so as to give arguments.
8281         (gnus-execute header regexp
8282                       `(call-interactively ',(key-binding command))
8283                       backward)
8284         (gnus-message 6 "Executing %s...done" (key-description command))))))
8285
8286 (defun gnus-summary-beginning-of-article ()
8287   "Scroll the article back to the beginning."
8288   (interactive)
8289   (gnus-summary-select-article)
8290   (gnus-configure-windows 'article)
8291   (gnus-eval-in-buffer-window gnus-article-buffer
8292     (widen)
8293     (goto-char (point-min))
8294     (when gnus-page-broken
8295       (gnus-narrow-to-page))))
8296
8297 (defun gnus-summary-end-of-article ()
8298   "Scroll to the end of the article."
8299   (interactive)
8300   (gnus-summary-select-article)
8301   (gnus-configure-windows 'article)
8302   (gnus-eval-in-buffer-window gnus-article-buffer
8303     (widen)
8304     (goto-char (point-max))
8305     (recenter -3)
8306     (when gnus-page-broken
8307       (gnus-narrow-to-page))))
8308
8309 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8310   "Truncate to LEN and quote all \"(\"'s in STRING."
8311   (gnus-replace-in-string (if (and len (> (length string) len))
8312                               (substring string 0 len)
8313                             string)
8314                           "[()]" "\\\\\\&"))
8315
8316 (defun gnus-summary-print-article (&optional filename n)
8317   "Generate and print a PostScript image of the N next (mail) articles.
8318
8319 If N is negative, print the N previous articles.  If N is nil and articles
8320 have been marked with the process mark, print these instead.
8321
8322 If the optional first argument FILENAME is nil, send the image to the
8323 printer.  If FILENAME is a string, save the PostScript image in a file with
8324 that name.  If FILENAME is a number, prompt the user for the name of the file
8325 to save in."
8326   (interactive (list (ps-print-preprint current-prefix-arg)))
8327   (dolist (article (gnus-summary-work-articles n))
8328     (gnus-summary-select-article nil nil 'pseudo article)
8329     (gnus-eval-in-buffer-window gnus-article-buffer
8330       (gnus-print-buffer))
8331     (gnus-summary-remove-process-mark article))
8332   (ps-despool filename))
8333
8334 (defun gnus-print-buffer ()
8335   (let ((buffer (generate-new-buffer " *print*")))
8336     (unwind-protect
8337         (progn
8338           (copy-to-buffer buffer (point-min) (point-max))
8339           (set-buffer buffer)
8340           (gnus-article-delete-invisible-text)
8341           (gnus-remove-text-with-property 'gnus-decoration)
8342           (when (gnus-visual-p 'article-highlight 'highlight)
8343             ;; Copy-to-buffer doesn't copy overlay.  So redo
8344             ;; highlight.
8345             (let ((gnus-article-buffer buffer))
8346               (gnus-article-highlight-citation t)
8347               (gnus-article-highlight-signature)))
8348           (let ((ps-left-header
8349                  (list
8350                   (concat "("
8351                           (gnus-summary-print-truncate-and-quote
8352                            (mail-header-subject gnus-current-headers)
8353                            66) ")")
8354                   (concat "("
8355                           (gnus-summary-print-truncate-and-quote
8356                            (mail-header-from gnus-current-headers)
8357                            45) ")")))
8358                 (ps-right-header
8359                  (list
8360                   "/pagenumberstring load"
8361                   (concat "("
8362                           (mail-header-date gnus-current-headers) ")"))))
8363             (gnus-run-hooks 'gnus-ps-print-hook)
8364             (save-excursion
8365               (if window-system
8366                   (ps-spool-buffer-with-faces)
8367                 (ps-spool-buffer)))))
8368       (kill-buffer buffer))))
8369
8370 (defun gnus-summary-show-article (&optional arg)
8371   "Force redisplaying of the current article.
8372 If ARG (the prefix) is a number, show the article with the charset
8373 defined in `gnus-summary-show-article-charset-alist', or the charset
8374 input.
8375 If ARG (the prefix) is non-nil and not a number, show the raw article
8376 without any article massaging functions being run.  Normally, the key strokes
8377 are `C-u g'."
8378   (interactive "P")
8379   (cond
8380    ((numberp arg)
8381     (gnus-summary-show-article t)
8382     (let ((gnus-newsgroup-charset
8383            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8384                (mm-read-coding-system
8385                 "View as charset: " ;; actually it is coding system.
8386                 (save-excursion
8387                   (set-buffer gnus-article-buffer)
8388                   (mm-detect-coding-region (point) (point-max))))))
8389           (gnus-newsgroup-ignored-charsets 'gnus-all))
8390       (gnus-summary-select-article nil 'force)
8391       (let ((deps gnus-newsgroup-dependencies)
8392             head header lines)
8393         (save-excursion
8394           (set-buffer gnus-original-article-buffer)
8395           (save-restriction
8396             (message-narrow-to-head)
8397             (setq head (buffer-string))
8398             (goto-char (point-min))
8399             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8400               (goto-char (point-max))
8401               (widen)
8402               (setq lines (1- (count-lines (point) (point-max))))))
8403           (with-temp-buffer
8404             (insert (format "211 %d Article retrieved.\n"
8405                             (cdr gnus-article-current)))
8406             (insert head)
8407             (if lines (insert (format "Lines: %d\n" lines)))
8408             (insert ".\n")
8409             (let ((nntp-server-buffer (current-buffer)))
8410               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8411         (gnus-data-set-header
8412          (gnus-data-find (cdr gnus-article-current))
8413          header)
8414         (gnus-summary-update-article-line
8415          (cdr gnus-article-current) header)
8416         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8417           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8418    ((not arg)
8419     ;; Select the article the normal way.
8420     (gnus-summary-select-article nil 'force))
8421    (t
8422     ;; We have to require this here to make sure that the following
8423     ;; dynamic binding isn't shadowed by autoloading.
8424     (require 'gnus-async)
8425     (require 'gnus-art)
8426     ;; Bind the article treatment functions to nil.
8427     (let ((gnus-have-all-headers t)
8428           gnus-article-prepare-hook
8429           gnus-article-decode-hook
8430           gnus-display-mime-function
8431           gnus-break-pages)
8432       ;; Destroy any MIME parts.
8433       (when (gnus-buffer-live-p gnus-article-buffer)
8434         (save-excursion
8435           (set-buffer gnus-article-buffer)
8436           (mm-destroy-parts gnus-article-mime-handles)
8437           ;; Set it to nil for safety reason.
8438           (setq gnus-article-mime-handle-alist nil)
8439           (setq gnus-article-mime-handles nil)))
8440       (gnus-summary-select-article nil 'force))))
8441   (gnus-summary-goto-subject gnus-current-article)
8442   (gnus-summary-position-point))
8443
8444 (defun gnus-summary-show-raw-article ()
8445   "Show the raw article without any article massaging functions being run."
8446   (interactive)
8447   (gnus-summary-show-article t))
8448
8449 (defun gnus-summary-verbose-headers (&optional arg)
8450   "Toggle permanent full header display.
8451 If ARG is a positive number, turn header display on.
8452 If ARG is a negative number, turn header display off."
8453   (interactive "P")
8454   (setq gnus-show-all-headers
8455         (cond ((or (not (numberp arg))
8456                    (zerop arg))
8457                (not gnus-show-all-headers))
8458               ((natnump arg)
8459                t)))
8460   (gnus-summary-show-article))
8461
8462 (defun gnus-summary-toggle-header (&optional arg)
8463   "Show the headers if they are hidden, or hide them if they are shown.
8464 If ARG is a positive number, show the entire header.
8465 If ARG is a negative number, hide the unwanted header lines."
8466   (interactive "P")
8467   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8468                      (get-buffer-window gnus-article-buffer t))))
8469     (with-current-buffer gnus-article-buffer
8470       (widen)
8471       (article-narrow-to-head)
8472       (let* ((buffer-read-only nil)
8473              (inhibit-point-motion-hooks t)
8474              (hidden (if (numberp arg)
8475                          (>= arg 0)
8476                        (gnus-article-hidden-text-p 'headers)))
8477              s e)
8478         (delete-region (point-min) (point-max))
8479         (with-current-buffer gnus-original-article-buffer
8480           (goto-char (setq s (point-min)))
8481           (setq e (if (search-forward "\n\n" nil t)
8482                       (1- (point))
8483                     (point-max))))
8484         (insert-buffer-substring gnus-original-article-buffer s e)
8485         (article-decode-encoded-words)
8486         (if hidden
8487             (let ((gnus-treat-hide-headers nil)
8488                   (gnus-treat-hide-boring-headers nil))
8489               (gnus-delete-wash-type 'headers)
8490               (gnus-treat-article 'head))
8491           (gnus-treat-article 'head))
8492         (widen)
8493         (if window
8494             (set-window-start window (goto-char (point-min))))
8495         (setq gnus-page-broken
8496               (when gnus-break-pages
8497                 (gnus-narrow-to-page)
8498                 t))
8499         (gnus-set-mode-line 'article)))))
8500
8501 (defun gnus-summary-show-all-headers ()
8502   "Make all header lines visible."
8503   (interactive)
8504   (gnus-summary-toggle-header 1))
8505
8506 (defun gnus-summary-caesar-message (&optional arg)
8507   "Caesar rotate the current article by 13.
8508 The numerical prefix specifies how many places to rotate each letter
8509 forward."
8510   (interactive "P")
8511   (gnus-summary-select-article)
8512   (let ((mail-header-separator ""))
8513     (gnus-eval-in-buffer-window gnus-article-buffer
8514       (save-restriction
8515         (widen)
8516         (let ((start (window-start))
8517               buffer-read-only)
8518           (message-caesar-buffer-body arg)
8519           (set-window-start (get-buffer-window (current-buffer)) start))))))
8520
8521 (autoload 'unmorse-region "morse"
8522   "Convert morse coded text in region to ordinary ASCII text."
8523   t)
8524
8525 (defun gnus-summary-morse-message (&optional arg)
8526   "Morse decode the current article."
8527   (interactive "P")
8528   (gnus-summary-select-article)
8529   (let ((mail-header-separator ""))
8530     (gnus-eval-in-buffer-window gnus-article-buffer
8531       (save-excursion
8532         (save-restriction
8533           (widen)
8534           (let ((pos (window-start))
8535                 buffer-read-only)
8536             (goto-char (point-min))
8537             (when (message-goto-body)
8538               (gnus-narrow-to-body))
8539             (goto-char (point-min))
8540             (while (re-search-forward "·" (point-max) t)
8541               (replace-match "."))
8542             (unmorse-region (point-min) (point-max))
8543             (widen)
8544             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8545
8546 (defun gnus-summary-stop-page-breaking ()
8547   "Stop page breaking in the current article."
8548   (interactive)
8549   (gnus-summary-select-article)
8550   (gnus-eval-in-buffer-window gnus-article-buffer
8551     (widen)
8552     (when (gnus-visual-p 'page-marker)
8553       (let ((buffer-read-only nil))
8554         (gnus-remove-text-with-property 'gnus-prev)
8555         (gnus-remove-text-with-property 'gnus-next))
8556       (setq gnus-page-broken nil))))
8557
8558 (defun gnus-summary-move-article (&optional n to-newsgroup
8559                                             select-method action)
8560   "Move the current article to a different newsgroup.
8561 If N is a positive number, move the N next articles.
8562 If N is a negative number, move the N previous articles.
8563 If N is nil and any articles have been marked with the process mark,
8564 move those articles instead.
8565 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8566 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8567 re-spool using this method.
8568
8569 When called interactively with TO-NEWSGROUP being nil, the value of
8570 the variable `gnus-move-split-methods' is used for finding a default
8571 for the target newsgroup.
8572
8573 For this function to work, both the current newsgroup and the
8574 newsgroup that you want to move to have to support the `request-move'
8575 and `request-accept' functions.
8576
8577 ACTION can be either `move' (the default), `crosspost' or `copy'."
8578   (interactive "P")
8579   (unless action
8580     (setq action 'move))
8581   ;; Check whether the source group supports the required functions.
8582   (cond ((and (eq action 'move)
8583               (not (gnus-check-backend-function
8584                     'request-move-article gnus-newsgroup-name)))
8585          (error "The current group does not support article moving"))
8586         ((and (eq action 'crosspost)
8587               (not (gnus-check-backend-function
8588                     'request-replace-article gnus-newsgroup-name)))
8589          (error "The current group does not support article editing")))
8590   (let ((articles (gnus-summary-work-articles n))
8591         (prefix (if (gnus-check-backend-function
8592                      'request-move-article gnus-newsgroup-name)
8593                     (gnus-group-real-prefix gnus-newsgroup-name)
8594                   ""))
8595         (names '((move "Move" "Moving")
8596                  (copy "Copy" "Copying")
8597                  (crosspost "Crosspost" "Crossposting")))
8598         (copy-buf (save-excursion
8599                     (nnheader-set-temp-buffer " *copy article*")))
8600         art-group to-method new-xref article to-groups)
8601     (unless (assq action names)
8602       (error "Unknown action %s" action))
8603     ;; Read the newsgroup name.
8604     (when (and (not to-newsgroup)
8605                (not select-method))
8606       (if (and gnus-move-split-methods
8607                (not
8608                 (and (memq gnus-current-article articles)
8609                      (gnus-buffer-live-p gnus-original-article-buffer))))
8610           ;; When `gnus-move-split-methods' is non-nil, we have to
8611           ;; select an article to give `gnus-read-move-group-name' an
8612           ;; opportunity to suggest an appropriate default.  However,
8613           ;; we needn't render or mark the article.
8614           (let ((gnus-display-mime-function nil)
8615                 (gnus-article-prepare-hook nil)
8616                 (gnus-mark-article-hook nil))
8617             (gnus-summary-select-article nil nil nil (car articles))))
8618       (setq to-newsgroup
8619             (gnus-read-move-group-name
8620              (cadr (assq action names))
8621              (symbol-value (intern (format "gnus-current-%s-group" action)))
8622              articles prefix))
8623       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8624     (setq to-method (or select-method
8625                         (gnus-server-to-method
8626                          (gnus-group-method to-newsgroup))))
8627     ;; Check the method we are to move this article to...
8628     (unless (gnus-check-backend-function
8629              'request-accept-article (car to-method))
8630       (error "%s does not support article copying" (car to-method)))
8631     (unless (gnus-check-server to-method)
8632       (error "Can't open server %s" (car to-method)))
8633     (gnus-message 6 "%s to %s: %s..."
8634                   (caddr (assq action names))
8635                   (or (car select-method) to-newsgroup) articles)
8636     (while articles
8637       (setq article (pop articles))
8638       (setq
8639        art-group
8640        (cond
8641         ;; Move the article.
8642         ((eq action 'move)
8643          ;; Remove this article from future suppression.
8644          (gnus-dup-unsuppress-article article)
8645          (gnus-request-move-article
8646           article                       ; Article to move
8647           gnus-newsgroup-name           ; From newsgroup
8648           (nth 1 (gnus-find-method-for-group
8649                   gnus-newsgroup-name)) ; Server
8650           (list 'gnus-request-accept-article
8651                 to-newsgroup (list 'quote select-method)
8652                 (not articles) t)       ; Accept form
8653           (not articles)))              ; Only save nov last time
8654         ;; Copy the article.
8655         ((eq action 'copy)
8656          (save-excursion
8657            (set-buffer copy-buf)
8658            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8659              (gnus-request-accept-article
8660               to-newsgroup select-method (not articles) t))))
8661         ;; Crosspost the article.
8662         ((eq action 'crosspost)
8663          (let ((xref (message-tokenize-header
8664                       (mail-header-xref (gnus-summary-article-header article))
8665                       " ")))
8666            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8667                                   ":" (number-to-string article)))
8668            (unless xref
8669              (setq xref (list (system-name))))
8670            (setq new-xref
8671                  (concat
8672                   (mapconcat 'identity
8673                              (delete "Xref:" (delete new-xref xref))
8674                              " ")
8675                   " " new-xref))
8676            (save-excursion
8677              (set-buffer copy-buf)
8678              ;; First put the article in the destination group.
8679              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8680              (when (consp (setq art-group
8681                                 (gnus-request-accept-article
8682                                  to-newsgroup select-method (not articles))))
8683                (setq new-xref (concat new-xref " " (car art-group)
8684                                       ":"
8685                                       (number-to-string (cdr art-group))))
8686                ;; Now we have the new Xrefs header, so we insert
8687                ;; it and replace the new article.
8688                (nnheader-replace-header "Xref" new-xref)
8689                (gnus-request-replace-article
8690                 (cdr art-group) to-newsgroup (current-buffer))
8691                art-group))))))
8692       (cond
8693        ((not art-group)
8694         (gnus-message 1 "Couldn't %s article %s: %s"
8695                       (cadr (assq action names)) article
8696                       (nnheader-get-report (car to-method))))
8697        ((eq art-group 'junk)
8698         (when (eq action 'move)
8699           (gnus-summary-mark-article article gnus-canceled-mark)
8700           (gnus-message 4 "Deleted article %s" article)))
8701        (t
8702         (let* ((pto-group (gnus-group-prefixed-name
8703                            (car art-group) to-method))
8704                (entry
8705                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8706                (info (nth 2 entry))
8707                (to-group (gnus-info-group info))
8708                to-marks)
8709           ;; Update the group that has been moved to.
8710           (when (and info
8711                      (memq action '(move copy)))
8712             (unless (member to-group to-groups)
8713               (push to-group to-groups))
8714
8715             (unless (memq article gnus-newsgroup-unreads)
8716               (push 'read to-marks)
8717               (gnus-info-set-read
8718                info (gnus-add-to-range (gnus-info-read info)
8719                                        (list (cdr art-group)))))
8720
8721             ;; See whether the article is to be put in the cache.
8722             (let ((marks gnus-article-mark-lists)
8723                   (to-article (cdr art-group)))
8724
8725               ;; Enter the article into the cache in the new group,
8726               ;; if that is required.
8727               (when gnus-use-cache
8728                 (gnus-cache-possibly-enter-article
8729                  to-group to-article
8730                  (memq article gnus-newsgroup-marked)
8731                  (memq article gnus-newsgroup-dormant)
8732                  (memq article gnus-newsgroup-unreads)))
8733
8734               (when gnus-preserve-marks
8735                 ;; Copy any marks over to the new group.
8736                 (when (and (equal to-group gnus-newsgroup-name)
8737                            (not (memq article gnus-newsgroup-unreads)))
8738                   ;; Mark this article as read in this group.
8739                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8740                   (setcdr (gnus-active to-group) to-article)
8741                   (setcdr gnus-newsgroup-active to-article))
8742
8743                 (while marks
8744                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8745                     (when (memq article (symbol-value
8746                                          (intern (format "gnus-newsgroup-%s"
8747                                                          (caar marks)))))
8748                       (push (cdar marks) to-marks)
8749                       ;; If the other group is the same as this group,
8750                       ;; then we have to add the mark to the list.
8751                       (when (equal to-group gnus-newsgroup-name)
8752                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8753                              (cons to-article
8754                                    (symbol-value
8755                                     (intern (format "gnus-newsgroup-%s"
8756                                                     (caar marks)))))))
8757                       ;; Copy the marks to other group.
8758                       (gnus-add-marked-articles
8759                        to-group (cdar marks) (list to-article) info)))
8760                   (setq marks (cdr marks)))
8761
8762                 (gnus-request-set-mark
8763                  to-group (list (list (list to-article) 'add to-marks))))
8764
8765               (gnus-dribble-enter
8766                (concat "(gnus-group-set-info '"
8767                        (gnus-prin1-to-string (gnus-get-info to-group))
8768                        ")"))))
8769
8770           ;; Update the Xref header in this article to point to
8771           ;; the new crossposted article we have just created.
8772           (when (eq action 'crosspost)
8773             (save-excursion
8774               (set-buffer copy-buf)
8775               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8776               (nnheader-replace-header "Xref" new-xref)
8777               (gnus-request-replace-article
8778                article gnus-newsgroup-name (current-buffer)))))
8779
8780         ;;;!!!Why is this necessary?
8781         (set-buffer gnus-summary-buffer)
8782
8783         (gnus-summary-goto-subject article)
8784         (when (eq action 'move)
8785           (gnus-summary-mark-article article gnus-canceled-mark))))
8786       (gnus-summary-remove-process-mark article))
8787     ;; Re-activate all groups that have been moved to.
8788     (save-excursion
8789       (set-buffer gnus-group-buffer)
8790       (let ((gnus-group-marked to-groups))
8791         (gnus-group-get-new-news-this-group nil t)))
8792
8793     (gnus-kill-buffer copy-buf)
8794     (gnus-summary-position-point)
8795     (gnus-set-mode-line 'summary)))
8796
8797 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8798   "Move the current article to a different newsgroup.
8799 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8800 When called interactively, if TO-NEWSGROUP is nil, use the value of
8801 the variable `gnus-move-split-methods' for finding a default target
8802 newsgroup.
8803 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8804 re-spool using this method."
8805   (interactive "P")
8806   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8807
8808 (defun gnus-summary-crosspost-article (&optional n)
8809   "Crosspost the current article to some other group."
8810   (interactive "P")
8811   (gnus-summary-move-article n nil nil 'crosspost))
8812
8813 (defcustom gnus-summary-respool-default-method nil
8814   "Default method type for respooling an article.
8815 If nil, use to the current newsgroup method."
8816   :type 'symbol
8817   :group 'gnus-summary-mail)
8818
8819 (defcustom gnus-summary-display-while-building nil
8820   "If not-nil, show and update the summary buffer as it's being built.
8821 If the value is t, update the buffer after every line is inserted.  If
8822 the value is an integer (N), update the display every N lines."
8823   :group 'gnus-thread
8824   :type '(choice (const :tag "off" nil)
8825                  number
8826                  (const :tag "frequently" t)))
8827
8828 (defun gnus-summary-respool-article (&optional n method)
8829   "Respool the current article.
8830 The article will be squeezed through the mail spooling process again,
8831 which means that it will be put in some mail newsgroup or other
8832 depending on `nnmail-split-methods'.
8833 If N is a positive number, respool the N next articles.
8834 If N is a negative number, respool the N previous articles.
8835 If N is nil and any articles have been marked with the process mark,
8836 respool those articles instead.
8837
8838 Respooling can be done both from mail groups and \"real\" newsgroups.
8839 In the former case, the articles in question will be moved from the
8840 current group into whatever groups they are destined to.  In the
8841 latter case, they will be copied into the relevant groups."
8842   (interactive
8843    (list current-prefix-arg
8844          (let* ((methods (gnus-methods-using 'respool))
8845                 (methname
8846                  (symbol-name (or gnus-summary-respool-default-method
8847                                   (car (gnus-find-method-for-group
8848                                         gnus-newsgroup-name)))))
8849                 (method
8850                  (gnus-completing-read-with-default
8851                   methname "What backend do you want to use when respooling?"
8852                   methods nil t nil 'gnus-mail-method-history))
8853                 ms)
8854            (cond
8855             ((zerop (length (setq ms (gnus-servers-using-backend
8856                                       (intern method)))))
8857              (list (intern method) ""))
8858             ((= 1 (length ms))
8859              (car ms))
8860             (t
8861              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8862                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8863                            ms-alist))))))))
8864   (unless method
8865     (error "No method given for respooling"))
8866   (if (assoc (symbol-name
8867               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8868              (gnus-methods-using 'respool))
8869       (gnus-summary-move-article n nil method)
8870     (gnus-summary-copy-article n nil method)))
8871
8872 (defun gnus-summary-import-article (file &optional edit)
8873   "Import an arbitrary file into a mail newsgroup."
8874   (interactive "fImport file: \nP")
8875   (let ((group gnus-newsgroup-name)
8876         (now (current-time))
8877         atts lines group-art)
8878     (unless (gnus-check-backend-function 'request-accept-article group)
8879       (error "%s does not support article importing" group))
8880     (or (file-readable-p file)
8881         (not (file-regular-p file))
8882         (error "Can't read %s" file))
8883     (save-excursion
8884       (set-buffer (gnus-get-buffer-create " *import file*"))
8885       (erase-buffer)
8886       (nnheader-insert-file-contents file)
8887       (goto-char (point-min))
8888       (if (nnheader-article-p)
8889           (save-restriction
8890             (goto-char (point-min))
8891             (search-forward "\n\n" nil t)
8892             (narrow-to-region (point-min) (1- (point)))
8893             (goto-char (point-min))
8894             (unless (re-search-forward "^date:" nil t)
8895               (goto-char (point-max))
8896               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8897        ;; This doesn't look like an article, so we fudge some headers.
8898         (setq atts (file-attributes file)
8899               lines (count-lines (point-min) (point-max)))
8900         (insert "From: " (read-string "From: ") "\n"
8901                 "Subject: " (read-string "Subject: ") "\n"
8902                 "Date: " (message-make-date (nth 5 atts)) "\n"
8903                 "Message-ID: " (message-make-message-id) "\n"
8904                 "Lines: " (int-to-string lines) "\n"
8905                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8906       (setq group-art (gnus-request-accept-article group nil t))
8907       (kill-buffer (current-buffer)))
8908     (setq gnus-newsgroup-active (gnus-activate-group group))
8909     (forward-line 1)
8910     (gnus-summary-goto-article (cdr group-art) nil t)
8911     (when edit
8912       (gnus-summary-edit-article))))
8913
8914 (defun gnus-summary-create-article ()
8915   "Create an article in a mail newsgroup."
8916   (interactive)
8917   (let ((group gnus-newsgroup-name)
8918         (now (current-time))
8919         group-art)
8920     (unless (gnus-check-backend-function 'request-accept-article group)
8921       (error "%s does not support article importing" group))
8922     (save-excursion
8923       (set-buffer (gnus-get-buffer-create " *import file*"))
8924       (erase-buffer)
8925       (goto-char (point-min))
8926       ;; This doesn't look like an article, so we fudge some headers.
8927       (insert "From: " (read-string "From: ") "\n"
8928               "Subject: " (read-string "Subject: ") "\n"
8929               "Date: " (message-make-date now) "\n"
8930               "Message-ID: " (message-make-message-id) "\n")
8931       (setq group-art (gnus-request-accept-article group nil t))
8932       (kill-buffer (current-buffer)))
8933     (setq gnus-newsgroup-active (gnus-activate-group group))
8934     (forward-line 1)
8935     (gnus-summary-goto-article (cdr group-art) nil t)
8936     (gnus-summary-edit-article)))
8937
8938 (defun gnus-summary-article-posted-p ()
8939   "Say whether the current (mail) article is available from news as well.
8940 This will be the case if the article has both been mailed and posted."
8941   (interactive)
8942   (let ((id (mail-header-references (gnus-summary-article-header)))
8943         (gnus-override-method (car (gnus-refer-article-methods))))
8944     (if (gnus-request-head id "")
8945         (gnus-message 2 "The current message was found on %s"
8946                       gnus-override-method)
8947       (gnus-message 2 "The current message couldn't be found on %s"
8948                     gnus-override-method)
8949       nil)))
8950
8951 (defun gnus-summary-expire-articles (&optional now)
8952   "Expire all articles that are marked as expirable in the current group."
8953   (interactive)
8954   (when (gnus-check-backend-function
8955          'request-expire-articles gnus-newsgroup-name)
8956     ;; This backend supports expiry.
8957     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8958            (expirable (if total
8959                           (progn
8960                             ;; We need to update the info for
8961                             ;; this group for `gnus-list-of-read-articles'
8962                             ;; to give us the right answer.
8963                             (gnus-run-hooks 'gnus-exit-group-hook)
8964                             (gnus-summary-update-info)
8965                             (gnus-list-of-read-articles gnus-newsgroup-name))
8966                         (setq gnus-newsgroup-expirable
8967                               (sort gnus-newsgroup-expirable '<))))
8968            (expiry-wait (if now 'immediate
8969                           (gnus-group-find-parameter
8970                            gnus-newsgroup-name 'expiry-wait)))
8971            (nnmail-expiry-target
8972             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8973                 nnmail-expiry-target))
8974            es)
8975       (when expirable
8976         ;; There are expirable articles in this group, so we run them
8977         ;; through the expiry process.
8978         (gnus-message 6 "Expiring articles...")
8979         (unless (gnus-check-group gnus-newsgroup-name)
8980           (error "Can't open server for %s" gnus-newsgroup-name))
8981         ;; The list of articles that weren't expired is returned.
8982         (save-excursion
8983           (if expiry-wait
8984               (let ((nnmail-expiry-wait-function nil)
8985                     (nnmail-expiry-wait expiry-wait))
8986                 (setq es (gnus-request-expire-articles
8987                           expirable gnus-newsgroup-name)))
8988             (setq es (gnus-request-expire-articles
8989                       expirable gnus-newsgroup-name)))
8990           (unless total
8991             (setq gnus-newsgroup-expirable es))
8992           ;; We go through the old list of expirable, and mark all
8993           ;; really expired articles as nonexistent.
8994           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8995             (let ((gnus-use-cache nil))
8996               (dolist (article expirable)
8997                 (when (and (not (memq article es))
8998                            (gnus-data-find article))
8999                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9000         (gnus-message 6 "Expiring articles...done")))))
9001
9002 (defun gnus-summary-expire-articles-now ()
9003   "Expunge all expirable articles in the current group.
9004 This means that *all* articles that are marked as expirable will be
9005 deleted forever, right now."
9006   (interactive)
9007   (or gnus-expert-user
9008       (gnus-yes-or-no-p
9009        "Are you really, really, really sure you want to delete all these messages? ")
9010       (error "Phew!"))
9011   (gnus-summary-expire-articles t))
9012
9013 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9014 (defun gnus-summary-delete-article (&optional n)
9015   "Delete the N next (mail) articles.
9016 This command actually deletes articles.  This is not a marking
9017 command.  The article will disappear forever from your life, never to
9018 return.
9019 If N is negative, delete backwards.
9020 If N is nil and articles have been marked with the process mark,
9021 delete these instead."
9022   (interactive "P")
9023   (unless (gnus-check-backend-function 'request-expire-articles
9024                                        gnus-newsgroup-name)
9025     (error "The current newsgroup does not support article deletion"))
9026   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9027     (error "Couldn't open server"))
9028   ;; Compute the list of articles to delete.
9029   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9030         (nnmail-expiry-target 'delete)
9031         not-deleted)
9032     (if (and gnus-novice-user
9033              (not (gnus-yes-or-no-p
9034                    (format "Do you really want to delete %s forever? "
9035                            (if (> (length articles) 1)
9036                                (format "these %s articles" (length articles))
9037                              "this article")))))
9038         ()
9039       ;; Delete the articles.
9040       (setq not-deleted (gnus-request-expire-articles
9041                          articles gnus-newsgroup-name 'force))
9042       (while articles
9043         (gnus-summary-remove-process-mark (car articles))
9044         ;; The backend might not have been able to delete the article
9045         ;; after all.
9046         (unless (memq (car articles) not-deleted)
9047           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9048         (setq articles (cdr articles)))
9049       (when not-deleted
9050         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9051     (gnus-summary-position-point)
9052     (gnus-set-mode-line 'summary)
9053     not-deleted))
9054
9055 (defun gnus-summary-edit-article (&optional arg)
9056   "Edit the current article.
9057 This will have permanent effect only in mail groups.
9058 If ARG is nil, edit the decoded articles.
9059 If ARG is 1, edit the raw articles.
9060 If ARG is 2, edit the raw articles even in read-only groups.
9061 If ARG is 3, edit the articles with the current handles.
9062 Otherwise, allow editing of articles even in read-only
9063 groups."
9064   (interactive "P")
9065   (let (force raw current-handles)
9066     (cond
9067      ((null arg))
9068      ((eq arg 1)
9069       (setq raw t))
9070      ((eq arg 2)
9071       (setq raw t
9072             force t))
9073      ((eq arg 3)
9074       (setq current-handles
9075             (and (gnus-buffer-live-p gnus-article-buffer)
9076                  (with-current-buffer gnus-article-buffer
9077                    (prog1
9078                        gnus-article-mime-handles
9079                      (setq gnus-article-mime-handles nil))))))
9080      (t
9081       (setq force t)))
9082     (when (and raw (not force)
9083                (member gnus-newsgroup-name '("nndraft:delayed"
9084                                              "nndraft:drafts"
9085                                              "nndraft:queue")))
9086       (error "Can't edit the raw article in group %s"
9087              gnus-newsgroup-name))
9088     (save-excursion
9089       (set-buffer gnus-summary-buffer)
9090       (let ((mail-parse-charset gnus-newsgroup-charset)
9091             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9092         (gnus-set-global-variables)
9093         (when (and (not force)
9094                    (gnus-group-read-only-p))
9095           (error "The current newsgroup does not support article editing"))
9096         (gnus-summary-show-article t)
9097         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9098           (with-current-buffer gnus-article-buffer
9099             (mm-enable-multibyte)))
9100         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9101             (setq raw t))
9102         (gnus-article-edit-article
9103          (if raw 'ignore
9104            `(lambda ()
9105               (let ((mbl mml-buffer-list))
9106                 (setq mml-buffer-list nil)
9107                 (mime-to-mml ,'current-handles)
9108                 (let ((mbl1 mml-buffer-list))
9109                   (setq mml-buffer-list mbl)
9110                   (set (make-local-variable 'mml-buffer-list) mbl1))
9111                 (make-local-hook 'kill-buffer-hook)
9112                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9113          `(lambda (no-highlight)
9114             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9115                   (message-options message-options)
9116                   (message-options-set-recipient)
9117                   (mail-parse-ignored-charsets
9118                    ',gnus-newsgroup-ignored-charsets))
9119               ,(if (not raw) '(progn
9120                                 (mml-to-mime)
9121                                 (mml-destroy-buffers)
9122                                 (remove-hook 'kill-buffer-hook
9123                                              'mml-destroy-buffers t)
9124                                 (kill-local-variable 'mml-buffer-list)))
9125               (gnus-summary-edit-article-done
9126                ,(or (mail-header-references gnus-current-headers) "")
9127                ,(gnus-group-read-only-p)
9128                ,gnus-summary-buffer no-highlight))))))))
9129
9130 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9131
9132 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9133                                                  no-highlight)
9134   "Make edits to the current article permanent."
9135   (interactive)
9136   (save-excursion
9137    ;; The buffer restriction contains the entire article if it exists.
9138     (when (article-goto-body)
9139       (let ((lines (count-lines (point) (point-max)))
9140             (length (- (point-max) (point)))
9141             (case-fold-search t)
9142             (body (copy-marker (point))))
9143         (goto-char (point-min))
9144         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9145           (delete-region (match-beginning 1) (match-end 1))
9146           (insert (number-to-string length)))
9147         (goto-char (point-min))
9148         (when (re-search-forward
9149                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9150           (delete-region (match-beginning 1) (match-end 1))
9151           (insert (number-to-string length)))
9152         (goto-char (point-min))
9153         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9154           (delete-region (match-beginning 1) (match-end 1))
9155           (insert (number-to-string lines))))))
9156   ;; Replace the article.
9157   (let ((buf (current-buffer)))
9158     (with-temp-buffer
9159       (insert-buffer-substring buf)
9160
9161       (if (and (not read-only)
9162                (not (gnus-request-replace-article
9163                      (cdr gnus-article-current) (car gnus-article-current)
9164                      (current-buffer) t)))
9165           (error "Couldn't replace article")
9166         ;; Update the summary buffer.
9167         (if (and references
9168                  (equal (message-tokenize-header references " ")
9169                         (message-tokenize-header
9170                          (or (message-fetch-field "references") "") " ")))
9171             ;; We only have to update this line.
9172             (save-excursion
9173               (save-restriction
9174                 (message-narrow-to-head)
9175                 (let ((head (buffer-string))
9176                       header)
9177                   (with-temp-buffer
9178                     (insert (format "211 %d Article retrieved.\n"
9179                                     (cdr gnus-article-current)))
9180                     (insert head)
9181                     (insert ".\n")
9182                     (let ((nntp-server-buffer (current-buffer)))
9183                       (setq header (car (gnus-get-newsgroup-headers
9184                                          nil t))))
9185                     (save-excursion
9186                       (set-buffer gnus-summary-buffer)
9187                       (gnus-data-set-header
9188                        (gnus-data-find (cdr gnus-article-current))
9189                        header)
9190                       (gnus-summary-update-article-line
9191                        (cdr gnus-article-current) header)
9192                       (if (gnus-summary-goto-subject
9193                            (cdr gnus-article-current) nil t)
9194                           (gnus-summary-update-secondary-mark
9195                            (cdr gnus-article-current))))))))
9196           ;; Update threads.
9197           (set-buffer (or buffer gnus-summary-buffer))
9198           (gnus-summary-update-article (cdr gnus-article-current))
9199           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9200               (gnus-summary-update-secondary-mark
9201                (cdr gnus-article-current))))
9202         ;; Prettify the article buffer again.
9203         (unless no-highlight
9204           (save-excursion
9205             (set-buffer gnus-article-buffer)
9206             ;;;!!! Fix this -- article should be rehighlighted.
9207             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9208             (set-buffer gnus-original-article-buffer)
9209             (gnus-request-article
9210              (cdr gnus-article-current)
9211              (car gnus-article-current) (current-buffer))))
9212         ;; Prettify the summary buffer line.
9213         (when (gnus-visual-p 'summary-highlight 'highlight)
9214           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9215
9216 (defun gnus-summary-edit-wash (key)
9217   "Perform editing command KEY in the article buffer."
9218   (interactive
9219    (list
9220     (progn
9221       (message "%s" (concat (this-command-keys) "- "))
9222       (read-char))))
9223   (message "")
9224   (gnus-summary-edit-article)
9225   (execute-kbd-macro (concat (this-command-keys) key))
9226   (gnus-article-edit-done))
9227
9228 ;;; Respooling
9229
9230 (defun gnus-summary-respool-query (&optional silent trace)
9231   "Query where the respool algorithm would put this article."
9232   (interactive)
9233   (let (gnus-mark-article-hook)
9234     (gnus-summary-select-article)
9235     (save-excursion
9236       (set-buffer gnus-original-article-buffer)
9237       (save-restriction
9238         (message-narrow-to-head)
9239         (let ((groups (nnmail-article-group 'identity trace)))
9240           (unless silent
9241             (if groups
9242                 (message "This message would go to %s"
9243                          (mapconcat 'car groups ", "))
9244               (message "This message would go to no groups"))
9245             groups))))))
9246
9247 (defun gnus-summary-respool-trace ()
9248   "Trace where the respool algorithm would put this article.
9249 Display a buffer showing all fancy splitting patterns which matched."
9250   (interactive)
9251   (gnus-summary-respool-query nil t))
9252
9253 ;; Summary marking commands.
9254
9255 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9256   "Mark articles which has the same subject as read, and then select the next.
9257 If UNMARK is positive, remove any kind of mark.
9258 If UNMARK is negative, tick articles."
9259   (interactive "P")
9260   (when unmark
9261     (setq unmark (prefix-numeric-value unmark)))
9262   (let ((count
9263          (gnus-summary-mark-same-subject
9264           (gnus-summary-article-subject) unmark)))
9265     ;; Select next unread article.  If auto-select-same mode, should
9266     ;; select the first unread article.
9267     (gnus-summary-next-article t (and gnus-auto-select-same
9268                                       (gnus-summary-article-subject)))
9269     (gnus-message 7 "%d article%s marked as %s"
9270                   count (if (= count 1) " is" "s are")
9271                   (if unmark "unread" "read"))))
9272
9273 (defun gnus-summary-kill-same-subject (&optional unmark)
9274   "Mark articles which has the same subject as read.
9275 If UNMARK is positive, remove any kind of mark.
9276 If UNMARK is negative, tick articles."
9277   (interactive "P")
9278   (when unmark
9279     (setq unmark (prefix-numeric-value unmark)))
9280   (let ((count
9281          (gnus-summary-mark-same-subject
9282           (gnus-summary-article-subject) unmark)))
9283     ;; If marked as read, go to next unread subject.
9284     (when (null unmark)
9285       ;; Go to next unread subject.
9286       (gnus-summary-next-subject 1 t))
9287     (gnus-message 7 "%d articles are marked as %s"
9288                   count (if unmark "unread" "read"))))
9289
9290 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9291   "Mark articles with same SUBJECT as read, and return marked number.
9292 If optional argument UNMARK is positive, remove any kinds of marks.
9293 If optional argument UNMARK is negative, mark articles as unread instead."
9294   (let ((count 1))
9295     (save-excursion
9296       (cond
9297        ((null unmark)                   ; Mark as read.
9298         (while (and
9299                 (progn
9300                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9301                   (gnus-summary-show-thread) t)
9302                 (gnus-summary-find-subject subject))
9303           (setq count (1+ count))))
9304        ((> unmark 0)                    ; Tick.
9305         (while (and
9306                 (progn
9307                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9308                   (gnus-summary-show-thread) t)
9309                 (gnus-summary-find-subject subject))
9310           (setq count (1+ count))))
9311        (t                               ; Mark as unread.
9312         (while (and
9313                 (progn
9314                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9315                   (gnus-summary-show-thread) t)
9316                 (gnus-summary-find-subject subject))
9317           (setq count (1+ count)))))
9318       (gnus-set-mode-line 'summary)
9319       ;; Return the number of marked articles.
9320       count)))
9321
9322 (defun gnus-summary-mark-as-processable (n &optional unmark)
9323   "Set the process mark on the next N articles.
9324 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9325 the process mark instead.  The difference between N and the actual
9326 number of articles marked is returned."
9327   (interactive "P")
9328   (if (and (null n) (gnus-region-active-p))
9329       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9330     (setq n (prefix-numeric-value n))
9331     (let ((backward (< n 0))
9332           (n (abs n)))
9333       (while (and
9334               (> n 0)
9335               (if unmark
9336                   (gnus-summary-remove-process-mark
9337                    (gnus-summary-article-number))
9338                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9339               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9340         (setq n (1- n)))
9341       (when (/= 0 n)
9342         (gnus-message 7 "No more articles"))
9343       (gnus-summary-recenter)
9344       (gnus-summary-position-point)
9345       n)))
9346
9347 (defun gnus-summary-unmark-as-processable (n)
9348   "Remove the process mark from the next N articles.
9349 If N is negative, unmark backward instead.  The difference between N and
9350 the actual number of articles unmarked is returned."
9351   (interactive "P")
9352   (gnus-summary-mark-as-processable n t))
9353
9354 (defun gnus-summary-unmark-all-processable ()
9355   "Remove the process mark from all articles."
9356   (interactive)
9357   (save-excursion
9358     (while gnus-newsgroup-processable
9359       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9360   (gnus-summary-position-point))
9361
9362 (defun gnus-summary-add-mark (article type)
9363   "Mark ARTICLE with a mark of TYPE."
9364   (let ((vtype (car (assq type gnus-article-mark-lists)))
9365         var)
9366     (if (not vtype)
9367         (error "No such mark type: %s" type)
9368       (setq var (intern (format "gnus-newsgroup-%s" type)))
9369       (set var (cons article (symbol-value var)))
9370       (if (memq type '(processable cached replied forwarded recent saved))
9371           (gnus-summary-update-secondary-mark article)
9372         ;;; !!! This is bogus.  We should find out what primary
9373         ;;; !!! mark we want to set.
9374         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9375
9376 (defun gnus-summary-mark-as-expirable (n)
9377   "Mark N articles forward as expirable.
9378 If N is negative, mark backward instead.  The difference between N and
9379 the actual number of articles marked is returned."
9380   (interactive "p")
9381   (gnus-summary-mark-forward n gnus-expirable-mark))
9382
9383 (defun gnus-summary-mark-as-spam (n)
9384   "Mark N articles forward as spam.
9385 If N is negative, mark backward instead.  The difference between N and
9386 the actual number of articles marked is returned."
9387   (interactive "p")
9388   (gnus-summary-mark-forward n gnus-spam-mark))
9389
9390 (defun gnus-summary-mark-article-as-replied (article)
9391   "Mark ARTICLE as replied to and update the summary line.
9392 ARTICLE can also be a list of articles."
9393   (interactive (list (gnus-summary-article-number)))
9394   (let ((articles (if (listp article) article (list article))))
9395     (dolist (article articles)
9396       (push article gnus-newsgroup-replied)
9397       (let ((buffer-read-only nil))
9398         (when (gnus-summary-goto-subject article nil t)
9399           (gnus-summary-update-secondary-mark article))))))
9400
9401 (defun gnus-summary-mark-article-as-forwarded (article)
9402   "Mark ARTICLE as forwarded and update the summary line.
9403 ARTICLE can also be a list of articles."
9404   (let ((articles (if (listp article) article (list article))))
9405     (dolist (article articles)
9406       (push article gnus-newsgroup-forwarded)
9407       (let ((buffer-read-only nil))
9408         (when (gnus-summary-goto-subject article nil t)
9409           (gnus-summary-update-secondary-mark article))))))
9410
9411 (defun gnus-summary-set-bookmark (article)
9412   "Set a bookmark in current article."
9413   (interactive (list (gnus-summary-article-number)))
9414   (when (or (not (get-buffer gnus-article-buffer))
9415             (not gnus-current-article)
9416             (not gnus-article-current)
9417             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9418     (error "No current article selected"))
9419   ;; Remove old bookmark, if one exists.
9420   (gnus-pull article gnus-newsgroup-bookmarks)
9421   ;; Set the new bookmark, which is on the form
9422   ;; (article-number . line-number-in-body).
9423   (push
9424    (cons article
9425          (save-excursion
9426            (set-buffer gnus-article-buffer)
9427            (count-lines
9428             (min (point)
9429                  (save-excursion
9430                    (article-goto-body)
9431                    (point)))
9432             (point))))
9433    gnus-newsgroup-bookmarks)
9434   (gnus-message 6 "A bookmark has been added to the current article."))
9435
9436 (defun gnus-summary-remove-bookmark (article)
9437   "Remove the bookmark from the current article."
9438   (interactive (list (gnus-summary-article-number)))
9439   ;; Remove old bookmark, if one exists.
9440   (if (not (assq article gnus-newsgroup-bookmarks))
9441       (gnus-message 6 "No bookmark in current article.")
9442     (gnus-pull article gnus-newsgroup-bookmarks)
9443     (gnus-message 6 "Removed bookmark.")))
9444
9445 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9446 (defun gnus-summary-mark-as-dormant (n)
9447   "Mark N articles forward as dormant.
9448 If N is negative, mark backward instead.  The difference between N and
9449 the actual number of articles marked is returned."
9450   (interactive "p")
9451   (gnus-summary-mark-forward n gnus-dormant-mark))
9452
9453 (defun gnus-summary-set-process-mark (article)
9454   "Set the process mark on ARTICLE and update the summary line."
9455   (setq gnus-newsgroup-processable
9456         (cons article
9457               (delq article gnus-newsgroup-processable)))
9458   (when (gnus-summary-goto-subject article)
9459     (gnus-summary-show-thread)
9460     (gnus-summary-goto-subject article)
9461     (gnus-summary-update-secondary-mark article)))
9462
9463 (defun gnus-summary-remove-process-mark (article)
9464   "Remove the process mark from ARTICLE and update the summary line."
9465   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9466   (when (gnus-summary-goto-subject article)
9467     (gnus-summary-show-thread)
9468     (gnus-summary-goto-subject article)
9469     (gnus-summary-update-secondary-mark article)))
9470
9471 (defun gnus-summary-set-saved-mark (article)
9472   "Set the process mark on ARTICLE and update the summary line."
9473   (push article gnus-newsgroup-saved)
9474   (when (gnus-summary-goto-subject article)
9475     (gnus-summary-update-secondary-mark article)))
9476
9477 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9478   "Mark N articles as read forwards.
9479 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9480 The difference between N and the actual number of articles marked is
9481 returned.
9482 If NO-EXPIRE, auto-expiry will be inhibited."
9483   (interactive "p")
9484   (gnus-summary-show-thread)
9485   (let ((backward (< n 0))
9486         (gnus-summary-goto-unread
9487          (and gnus-summary-goto-unread
9488               (not (eq gnus-summary-goto-unread 'never))
9489               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9490                                     gnus-ticked-mark gnus-dormant-mark)))))
9491         (n (abs n))
9492         (mark (or mark gnus-del-mark)))
9493     (while (and (> n 0)
9494                 (gnus-summary-mark-article nil mark no-expire)
9495                 (zerop (gnus-summary-next-subject
9496                         (if backward -1 1)
9497                         (and gnus-summary-goto-unread
9498                              (not (eq gnus-summary-goto-unread 'never)))
9499                         t)))
9500       (setq n (1- n)))
9501     (when (/= 0 n)
9502       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9503     (gnus-summary-recenter)
9504     (gnus-summary-position-point)
9505     (gnus-set-mode-line 'summary)
9506     n))
9507
9508 (defun gnus-summary-mark-article-as-read (mark)
9509   "Mark the current article quickly as read with MARK."
9510   (let ((article (gnus-summary-article-number)))
9511     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9512     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9513     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9514     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9515     (push (cons article mark) gnus-newsgroup-reads)
9516     ;; Possibly remove from cache, if that is used.
9517     (when gnus-use-cache
9518       (gnus-cache-enter-remove-article article))
9519     ;; Allow the backend to change the mark.
9520     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9521     ;; Check for auto-expiry.
9522     (when (and gnus-newsgroup-auto-expire
9523                (memq mark gnus-auto-expirable-marks))
9524       (setq mark gnus-expirable-mark)
9525       ;; Let the backend know about the mark change.
9526       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9527       (push article gnus-newsgroup-expirable))
9528     ;; Set the mark in the buffer.
9529     (gnus-summary-update-mark mark 'unread)
9530     t))
9531
9532 (defun gnus-summary-mark-article-as-unread (mark)
9533   "Mark the current article quickly as unread with MARK."
9534   (let* ((article (gnus-summary-article-number))
9535          (old-mark (gnus-summary-article-mark article)))
9536     ;; Allow the backend to change the mark.
9537     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9538     (if (eq mark old-mark)
9539         t
9540       (if (<= article 0)
9541           (progn
9542             (gnus-error 1 "Can't mark negative article numbers")
9543             nil)
9544         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9545         (setq gnus-newsgroup-spam-marked
9546               (delq article gnus-newsgroup-spam-marked))
9547         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9548         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9549         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9550         (cond ((= mark gnus-ticked-mark)
9551                (setq gnus-newsgroup-marked
9552                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9553                                               article)))
9554               ((= mark gnus-spam-mark)
9555                (setq gnus-newsgroup-spam-marked
9556                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9557                                               article)))
9558               ((= mark gnus-dormant-mark)
9559                (setq gnus-newsgroup-dormant
9560                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9561                                               article)))
9562               (t
9563                (setq gnus-newsgroup-unreads
9564                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9565                                               article))))
9566         (gnus-pull article gnus-newsgroup-reads)
9567
9568         ;; See whether the article is to be put in the cache.
9569         (and gnus-use-cache
9570              (vectorp (gnus-summary-article-header article))
9571              (save-excursion
9572                (gnus-cache-possibly-enter-article
9573                 gnus-newsgroup-name article
9574                 (= mark gnus-ticked-mark)
9575                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9576
9577         ;; Fix the mark.
9578         (gnus-summary-update-mark mark 'unread)
9579         t))))
9580
9581 (defun gnus-summary-mark-article (&optional article mark no-expire)
9582   "Mark ARTICLE with MARK.  MARK can be any character.
9583 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9584 `??' (dormant) and `?E' (expirable).
9585 If MARK is nil, then the default character `?r' is used.
9586 If ARTICLE is nil, then the article on the current line will be
9587 marked.
9588 If NO-EXPIRE, auto-expiry will be inhibited."
9589   ;; The mark might be a string.
9590   (when (stringp mark)
9591     (setq mark (aref mark 0)))
9592   ;; If no mark is given, then we check auto-expiring.
9593   (when (null mark)
9594     (setq mark gnus-del-mark))
9595   (when (and (not no-expire)
9596              gnus-newsgroup-auto-expire
9597              (memq mark gnus-auto-expirable-marks))
9598     (setq mark gnus-expirable-mark))
9599   (let ((article (or article (gnus-summary-article-number)))
9600         (old-mark (gnus-summary-article-mark article)))
9601     ;; Allow the backend to change the mark.
9602     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9603     (if (eq mark old-mark)
9604         t
9605       (unless article
9606         (error "No article on current line"))
9607       (if (not (if (or (= mark gnus-unread-mark)
9608                        (= mark gnus-ticked-mark)
9609                        (= mark gnus-spam-mark)
9610                        (= mark gnus-dormant-mark))
9611                    (gnus-mark-article-as-unread article mark)
9612                  (gnus-mark-article-as-read article mark)))
9613           t
9614         ;; See whether the article is to be put in the cache.
9615         (and gnus-use-cache
9616              (not (= mark gnus-canceled-mark))
9617              (vectorp (gnus-summary-article-header article))
9618              (save-excursion
9619                (gnus-cache-possibly-enter-article
9620                 gnus-newsgroup-name article
9621                 (= mark gnus-ticked-mark)
9622                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9623
9624         (when (gnus-summary-goto-subject article nil t)
9625           (let ((buffer-read-only nil))
9626             (gnus-summary-show-thread)
9627             ;; Fix the mark.
9628             (gnus-summary-update-mark mark 'unread)
9629             t))))))
9630
9631 (defun gnus-summary-update-secondary-mark (article)
9632   "Update the secondary (read, process, cache) mark."
9633   (gnus-summary-update-mark
9634    (cond ((memq article gnus-newsgroup-processable)
9635           gnus-process-mark)
9636          ((memq article gnus-newsgroup-cached)
9637           gnus-cached-mark)
9638          ((memq article gnus-newsgroup-replied)
9639           gnus-replied-mark)
9640          ((memq article gnus-newsgroup-forwarded)
9641           gnus-forwarded-mark)
9642          ((memq article gnus-newsgroup-saved)
9643           gnus-saved-mark)
9644          ((memq article gnus-newsgroup-recent)
9645           gnus-recent-mark)
9646          ((memq article gnus-newsgroup-unseen)
9647           gnus-unseen-mark)
9648          (t gnus-no-mark))
9649    'replied)
9650   (when (gnus-visual-p 'summary-highlight 'highlight)
9651     (gnus-run-hooks 'gnus-summary-update-hook))
9652   t)
9653
9654 (defun gnus-summary-update-download-mark (article)
9655   "Update the secondary (read, process, cache) mark."
9656   (gnus-summary-update-mark
9657    (cond ((memq article gnus-newsgroup-undownloaded) 
9658           gnus-undownloaded-mark)
9659          (gnus-newsgroup-agentized
9660           gnus-downloaded-mark)
9661          (t
9662           gnus-no-mark))
9663    'download)
9664   (gnus-summary-update-line t)
9665   t)
9666
9667 (defun gnus-summary-update-mark (mark type)
9668   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9669         (buffer-read-only nil))
9670     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9671     (when forward
9672       (when (looking-at "\r")
9673         (incf forward))
9674       (when (<= (+ forward (point)) (point-max))
9675         ;; Go to the right position on the line.
9676         (goto-char (+ forward (point)))
9677         ;; Replace the old mark with the new mark.
9678         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9679         ;; Optionally update the marks by some user rule.
9680         (when (eq type 'unread)
9681           (gnus-data-set-mark
9682            (gnus-data-find (gnus-summary-article-number)) mark)
9683           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9684
9685 (defun gnus-mark-article-as-read (article &optional mark)
9686   "Enter ARTICLE in the pertinent lists and remove it from others."
9687   ;; Make the article expirable.
9688   (let ((mark (or mark gnus-del-mark)))
9689     (setq gnus-newsgroup-expirable
9690           (if (= mark gnus-expirable-mark)
9691               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9692             (delq article gnus-newsgroup-expirable)))
9693     ;; Remove from unread and marked lists.
9694     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9695     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9696     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9697     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9698     (push (cons article mark) gnus-newsgroup-reads)
9699     ;; Possibly remove from cache, if that is used.
9700     (when gnus-use-cache
9701       (gnus-cache-enter-remove-article article))
9702     t))
9703
9704 (defun gnus-mark-article-as-unread (article &optional mark)
9705   "Enter ARTICLE in the pertinent lists and remove it from others."
9706   (let ((mark (or mark gnus-ticked-mark)))
9707     (if (<= article 0)
9708         (progn
9709           (gnus-error 1 "Can't mark negative article numbers")
9710           nil)
9711       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9712             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9713             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9714             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9715             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9716
9717       ;; Unsuppress duplicates?
9718       (when gnus-suppress-duplicates
9719         (gnus-dup-unsuppress-article article))
9720
9721       (cond ((= mark gnus-ticked-mark)
9722              (setq gnus-newsgroup-marked
9723                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9724             ((= mark gnus-spam-mark)
9725              (setq gnus-newsgroup-spam-marked
9726                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9727                                             article)))
9728             ((= mark gnus-dormant-mark)
9729              (setq gnus-newsgroup-dormant
9730                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9731             (t
9732              (setq gnus-newsgroup-unreads
9733                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9734       (gnus-pull article gnus-newsgroup-reads)
9735       t)))
9736
9737 (defalias 'gnus-summary-mark-as-unread-forward
9738   'gnus-summary-tick-article-forward)
9739 (make-obsolete 'gnus-summary-mark-as-unread-forward
9740                'gnus-summary-tick-article-forward)
9741 (defun gnus-summary-tick-article-forward (n)
9742   "Tick N articles forwards.
9743 If N is negative, tick backwards instead.
9744 The difference between N and the number of articles ticked is returned."
9745   (interactive "p")
9746   (gnus-summary-mark-forward n gnus-ticked-mark))
9747
9748 (defalias 'gnus-summary-mark-as-unread-backward
9749   'gnus-summary-tick-article-backward)
9750 (make-obsolete 'gnus-summary-mark-as-unread-backward
9751                'gnus-summary-tick-article-backward)
9752 (defun gnus-summary-tick-article-backward (n)
9753   "Tick N articles backwards.
9754 The difference between N and the number of articles ticked is returned."
9755   (interactive "p")
9756   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9757
9758 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9759 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9760 (defun gnus-summary-tick-article (&optional article clear-mark)
9761   "Mark current article as unread.
9762 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9763 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9764   (interactive)
9765   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9766                                        gnus-ticked-mark)))
9767
9768 (defun gnus-summary-mark-as-read-forward (n)
9769   "Mark N articles as read forwards.
9770 If N is negative, mark backwards instead.
9771 The difference between N and the actual number of articles marked is
9772 returned."
9773   (interactive "p")
9774   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9775
9776 (defun gnus-summary-mark-as-read-backward (n)
9777   "Mark the N articles as read backwards.
9778 The difference between N and the actual number of articles marked is
9779 returned."
9780   (interactive "p")
9781   (gnus-summary-mark-forward
9782    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9783
9784 (defun gnus-summary-mark-as-read (&optional article mark)
9785   "Mark current article as read.
9786 ARTICLE specifies the article to be marked as read.
9787 MARK specifies a string to be inserted at the beginning of the line."
9788   (gnus-summary-mark-article article mark))
9789
9790 (defun gnus-summary-clear-mark-forward (n)
9791   "Clear marks from N articles forward.
9792 If N is negative, clear backward instead.
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-clear-mark-backward (n)
9798   "Clear marks from N articles backward.
9799 The difference between N and the number of marks cleared is returned."
9800   (interactive "p")
9801   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9802
9803 (defun gnus-summary-mark-unread-as-read ()
9804   "Intended to be used by `gnus-summary-mark-article-hook'."
9805   (when (memq gnus-current-article gnus-newsgroup-unreads)
9806     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9807
9808 (defun gnus-summary-mark-read-and-unread-as-read ()
9809   "Intended to be used by `gnus-summary-mark-article-hook'."
9810   (let ((mark (gnus-summary-article-mark)))
9811     (when (or (gnus-unread-mark-p mark)
9812               (gnus-read-mark-p mark))
9813       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9814
9815 (defun gnus-summary-mark-unread-as-ticked ()
9816   "Intended to be used by `gnus-summary-mark-article-hook'."
9817   (when (memq gnus-current-article gnus-newsgroup-unreads)
9818     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9819
9820 (defun gnus-summary-mark-region-as-read (point mark all)
9821   "Mark all unread articles between point and mark as read.
9822 If given a prefix, mark all articles between point and mark as read,
9823 even ticked and dormant ones."
9824   (interactive "r\nP")
9825   (save-excursion
9826     (let (article)
9827       (goto-char point)
9828       (beginning-of-line)
9829       (while (and
9830               (< (point) mark)
9831               (progn
9832                 (when (or all
9833                           (memq (setq article (gnus-summary-article-number))
9834                                 gnus-newsgroup-unreads))
9835                   (gnus-summary-mark-article article gnus-del-mark))
9836                 t)
9837               (gnus-summary-find-next))))))
9838
9839 (defun gnus-summary-mark-below (score mark)
9840   "Mark articles with score less than SCORE with MARK."
9841   (interactive "P\ncMark: ")
9842   (setq score (if score
9843                   (prefix-numeric-value score)
9844                 (or gnus-summary-default-score 0)))
9845   (save-excursion
9846     (set-buffer gnus-summary-buffer)
9847     (goto-char (point-min))
9848     (while
9849         (progn
9850           (and (< (gnus-summary-article-score) score)
9851                (gnus-summary-mark-article nil mark))
9852           (gnus-summary-find-next)))))
9853
9854 (defun gnus-summary-kill-below (&optional score)
9855   "Mark articles with score below SCORE as read."
9856   (interactive "P")
9857   (gnus-summary-mark-below score gnus-killed-mark))
9858
9859 (defun gnus-summary-clear-above (&optional score)
9860   "Clear all marks from articles with score above SCORE."
9861   (interactive "P")
9862   (gnus-summary-mark-above score gnus-unread-mark))
9863
9864 (defun gnus-summary-tick-above (&optional score)
9865   "Tick all articles with score above SCORE."
9866   (interactive "P")
9867   (gnus-summary-mark-above score gnus-ticked-mark))
9868
9869 (defun gnus-summary-mark-above (score mark)
9870   "Mark articles with score over SCORE with MARK."
9871   (interactive "P\ncMark: ")
9872   (setq score (if score
9873                   (prefix-numeric-value score)
9874                 (or gnus-summary-default-score 0)))
9875   (save-excursion
9876     (set-buffer gnus-summary-buffer)
9877     (goto-char (point-min))
9878     (while (and (progn
9879                   (when (> (gnus-summary-article-score) score)
9880                     (gnus-summary-mark-article nil mark))
9881                   t)
9882                 (gnus-summary-find-next)))))
9883
9884 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9885 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9886 (defun gnus-summary-limit-include-expunged (&optional no-error)
9887   "Display all the hidden articles that were expunged for low scores."
9888   (interactive)
9889   (let ((buffer-read-only nil))
9890     (let ((scored gnus-newsgroup-scored)
9891           headers h)
9892       (while scored
9893         (unless (gnus-summary-article-header (caar scored))
9894           (and (setq h (gnus-number-to-header (caar scored)))
9895                (< (cdar scored) gnus-summary-expunge-below)
9896                (push h headers)))
9897         (setq scored (cdr scored)))
9898       (if (not headers)
9899           (when (not no-error)
9900             (error "No expunged articles hidden"))
9901         (goto-char (point-min))
9902         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9903         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9904         (mapcar (lambda (x) (push (mail-header-number x)
9905                                   gnus-newsgroup-limit))
9906                 headers)
9907         (gnus-summary-prepare-unthreaded (nreverse headers))
9908         (goto-char (point-min))
9909         (gnus-summary-position-point)
9910         t))))
9911
9912 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9913   "Mark all unread articles in this newsgroup as read.
9914 If prefix argument ALL is non-nil, ticked and dormant articles will
9915 also be marked as read.
9916 If QUIETLY is non-nil, no questions will be asked.
9917 If TO-HERE is non-nil, it should be a point in the buffer.  All
9918 articles before (after, if REVERSE is set) this point will be marked as read.
9919 Note that this function will only catch up the unread article
9920 in the current summary buffer limitation.
9921 The number of articles marked as read is returned."
9922   (interactive "P")
9923   (prog1
9924       (save-excursion
9925         (when (or quietly
9926                   (not gnus-interactive-catchup) ;Without confirmation?
9927                   gnus-expert-user
9928                   (gnus-y-or-n-p
9929                    (if all
9930                        "Mark absolutely all articles as read? "
9931                      "Mark all unread articles as read? ")))
9932           (if (and not-mark
9933                    (not gnus-newsgroup-adaptive)
9934                    (not gnus-newsgroup-auto-expire)
9935                    (not gnus-suppress-duplicates)
9936                    (or (not gnus-use-cache)
9937                        (eq gnus-use-cache 'passive)))
9938               (progn
9939                 (when all
9940                   (setq gnus-newsgroup-marked nil
9941                         gnus-newsgroup-spam-marked nil
9942                         gnus-newsgroup-dormant nil))
9943                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9944             ;; We actually mark all articles as canceled, which we
9945             ;; have to do when using auto-expiry or adaptive scoring.
9946             (gnus-summary-show-all-threads)
9947             (if (and to-here reverse)
9948                 (progn
9949                   (goto-char to-here)
9950                   (while (and
9951                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9952                           (gnus-summary-find-next (not all) nil nil t))))
9953               (when (gnus-summary-first-subject (not all) t)
9954                 (while (and
9955                         (if to-here (< (point) to-here) t)
9956                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9957                         (gnus-summary-find-next (not all) nil nil t)))))
9958             (gnus-set-mode-line 'summary))
9959           t))
9960     (gnus-summary-position-point)))
9961
9962 (defun gnus-summary-catchup-to-here (&optional all)
9963   "Mark all unticked articles before the current one as read.
9964 If ALL is non-nil, also mark ticked and dormant articles as read."
9965   (interactive "P")
9966   (save-excursion
9967     (gnus-save-hidden-threads
9968       (let ((beg (point)))
9969         ;; We check that there are unread articles.
9970         (when (or all (gnus-summary-find-prev))
9971           (gnus-summary-catchup all t beg)))))
9972   (gnus-summary-position-point))
9973
9974 (defun gnus-summary-catchup-from-here (&optional all)
9975   "Mark all unticked articles after the current one as read.
9976 If ALL is non-nil, also mark ticked and dormant articles as read."
9977   (interactive "P")
9978   (save-excursion
9979     (gnus-save-hidden-threads
9980       (let ((beg (point)))
9981         ;; We check that there are unread articles.
9982         (when (or all (gnus-summary-find-next))
9983           (gnus-summary-catchup all t beg nil t)))))
9984
9985   (gnus-summary-position-point))
9986 (defun gnus-summary-catchup-all (&optional quietly)
9987   "Mark all articles in this newsgroup as read.
9988 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9989 instead, which marks only unread articles as read."
9990   (interactive "P")
9991   (gnus-summary-catchup t quietly))
9992
9993 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9994   "Mark all unread articles in this group as read, then exit.
9995 If prefix argument ALL is non-nil, all articles are marked as read.
9996 If QUIETLY is non-nil, no questions will be asked."
9997   (interactive "P")
9998   (when (gnus-summary-catchup all quietly nil 'fast)
9999     ;; Select next newsgroup or exit.
10000     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10001              (eq gnus-auto-select-next 'quietly))
10002         (gnus-summary-next-group nil)
10003       (gnus-summary-exit))))
10004
10005 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10006   "Mark all articles in this newsgroup as read, and then exit.
10007 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10008 instead, which marks only unread articles as read."
10009   (interactive "P")
10010   (gnus-summary-catchup-and-exit t quietly))
10011
10012 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10013   "Mark all articles in this group as read and select the next group.
10014 If given a prefix, mark all articles, unread as well as ticked, as
10015 read."
10016   (interactive "P")
10017   (save-excursion
10018     (gnus-summary-catchup all))
10019   (gnus-summary-next-group))
10020
10021 ;;;
10022 ;;; with article
10023 ;;;
10024
10025 (defmacro gnus-with-article (article &rest forms)
10026   "Select ARTICLE and perform FORMS in the original article buffer.
10027 Then replace the article with the result."
10028   `(progn
10029      ;; We don't want the article to be marked as read.
10030      (let (gnus-mark-article-hook)
10031        (gnus-summary-select-article t t nil ,article))
10032      (set-buffer gnus-original-article-buffer)
10033      ,@forms
10034      (if (not (gnus-check-backend-function
10035                'request-replace-article (car gnus-article-current)))
10036          (gnus-message 5 "Read-only group; not replacing")
10037        (unless (gnus-request-replace-article
10038                 ,article (car gnus-article-current)
10039                 (current-buffer) t)
10040          (error "Couldn't replace article")))
10041      ;; The cache and backlog have to be flushed somewhat.
10042      (when gnus-keep-backlog
10043        (gnus-backlog-remove-article
10044         (car gnus-article-current) (cdr gnus-article-current)))
10045      (when gnus-use-cache
10046        (gnus-cache-update-article
10047         (car gnus-article-current) (cdr gnus-article-current)))))
10048
10049 (put 'gnus-with-article 'lisp-indent-function 1)
10050 (put 'gnus-with-article 'edebug-form-spec '(form body))
10051
10052 ;; Thread-based commands.
10053
10054 (defun gnus-summary-articles-in-thread (&optional article)
10055   "Return a list of all articles in the current thread.
10056 If ARTICLE is non-nil, return all articles in the thread that starts
10057 with that article."
10058   (let* ((article (or article (gnus-summary-article-number)))
10059          (data (gnus-data-find-list article))
10060          (top-level (gnus-data-level (car data)))
10061          (top-subject
10062           (cond ((null gnus-thread-operation-ignore-subject)
10063                  (gnus-simplify-subject-re
10064                   (mail-header-subject (gnus-data-header (car data)))))
10065                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10066                  (gnus-simplify-subject-fuzzy
10067                   (mail-header-subject (gnus-data-header (car data)))))
10068                 (t nil)))
10069          (end-point (save-excursion
10070                       (if (gnus-summary-go-to-next-thread)
10071                           (point) (point-max))))
10072          articles)
10073     (while (and data
10074                 (< (gnus-data-pos (car data)) end-point))
10075       (when (or (not top-subject)
10076                 (string= top-subject
10077                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10078                              (gnus-simplify-subject-fuzzy
10079                               (mail-header-subject
10080                                (gnus-data-header (car data))))
10081                            (gnus-simplify-subject-re
10082                             (mail-header-subject
10083                              (gnus-data-header (car data)))))))
10084         (push (gnus-data-number (car data)) articles))
10085       (unless (and (setq data (cdr data))
10086                    (> (gnus-data-level (car data)) top-level))
10087         (setq data nil)))
10088     ;; Return the list of articles.
10089     (nreverse articles)))
10090
10091 (defun gnus-summary-rethread-current ()
10092   "Rethread the thread the current article is part of."
10093   (interactive)
10094   (let* ((gnus-show-threads t)
10095          (article (gnus-summary-article-number))
10096          (id (mail-header-id (gnus-summary-article-header)))
10097          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10098     (unless id
10099       (error "No article on the current line"))
10100     (gnus-rebuild-thread id)
10101     (gnus-summary-goto-subject article)))
10102
10103 (defun gnus-summary-reparent-thread ()
10104   "Make the current article child of the marked (or previous) article.
10105
10106 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10107 is non-nil or the Subject: of both articles are the same."
10108   (interactive)
10109   (unless (not (gnus-group-read-only-p))
10110     (error "The current newsgroup does not support article editing"))
10111   (unless (<= (length gnus-newsgroup-processable) 1)
10112     (error "No more than one article may be marked"))
10113   (save-window-excursion
10114     (let ((gnus-article-buffer " *reparent*")
10115           (current-article (gnus-summary-article-number))
10116           ;; First grab the marked article, otherwise one line up.
10117           (parent-article (if (not (null gnus-newsgroup-processable))
10118                               (car gnus-newsgroup-processable)
10119                             (save-excursion
10120                               (if (eq (forward-line -1) 0)
10121                                   (gnus-summary-article-number)
10122                                 (error "Beginning of summary buffer"))))))
10123       (unless (not (eq current-article parent-article))
10124         (error "An article may not be self-referential"))
10125       (let ((message-id (mail-header-id
10126                          (gnus-summary-article-header parent-article))))
10127         (unless (and message-id (not (equal message-id "")))
10128           (error "No message-id in desired parent"))
10129         (gnus-with-article current-article
10130           (save-restriction
10131             (goto-char (point-min))
10132             (message-narrow-to-head)
10133             (if (re-search-forward "^References: " nil t)
10134                 (progn
10135                   (re-search-forward "^[^ \t]" nil t)
10136                   (forward-line -1)
10137                   (end-of-line)
10138                   (insert " " message-id))
10139               (insert "References: " message-id "\n"))))
10140         (set-buffer gnus-summary-buffer)
10141         (gnus-summary-unmark-all-processable)
10142         (gnus-summary-update-article current-article)
10143         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10144             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10145         (gnus-summary-rethread-current)
10146         (gnus-message 3 "Article %d is now the child of article %d"
10147                       current-article parent-article)))))
10148
10149 (defun gnus-summary-toggle-threads (&optional arg)
10150   "Toggle showing conversation threads.
10151 If ARG is positive number, turn showing conversation threads on."
10152   (interactive "P")
10153   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10154     (setq gnus-show-threads
10155           (if (null arg) (not gnus-show-threads)
10156             (> (prefix-numeric-value arg) 0)))
10157     (gnus-summary-prepare)
10158     (gnus-summary-goto-subject current)
10159     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10160     (gnus-summary-position-point)))
10161
10162 (defun gnus-summary-show-all-threads ()
10163   "Show all threads."
10164   (interactive)
10165   (save-excursion
10166     (let ((buffer-read-only nil))
10167       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10168   (gnus-summary-position-point))
10169
10170 (defun gnus-summary-show-thread ()
10171   "Show thread subtrees.
10172 Returns nil if no thread was there to be shown."
10173   (interactive)
10174   (let ((buffer-read-only nil)
10175         (orig (point))
10176         ;; first goto end then to beg, to have point at beg after let
10177         (end (progn (end-of-line) (point)))
10178         (beg (progn (beginning-of-line) (point))))
10179     (prog1
10180         ;; Any hidden lines here?
10181         (search-forward "\r" end t)
10182       (subst-char-in-region beg end ?\^M ?\n t)
10183       (goto-char orig)
10184       (gnus-summary-position-point))))
10185
10186 (defun gnus-summary-maybe-hide-threads ()
10187   "If requested, hide the threads that should be hidden."
10188   (when (and gnus-show-threads
10189              gnus-thread-hide-subtree)
10190     (gnus-summary-hide-all-threads
10191      (if (or (consp gnus-thread-hide-subtree)
10192              (gnus-functionp gnus-thread-hide-subtree))
10193          (gnus-make-predicate gnus-thread-hide-subtree)
10194        nil))))
10195
10196 ;;; Hiding predicates.
10197
10198 (defun gnus-article-unread-p (header)
10199   (memq (mail-header-number header) gnus-newsgroup-unreads))
10200
10201 (defun gnus-article-unseen-p (header)
10202   (memq (mail-header-number header) gnus-newsgroup-unseen))
10203
10204 (defun gnus-map-articles (predicate articles)
10205   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10206   (apply 'gnus-or (mapcar predicate
10207                           (mapcar 'gnus-summary-article-header articles))))
10208
10209 (defun gnus-summary-hide-all-threads (&optional predicate)
10210   "Hide all thread subtrees.
10211 If PREDICATE is supplied, threads that satisfy this predicate
10212 will not be hidden."
10213   (interactive)
10214   (save-excursion
10215     (goto-char (point-min))
10216     (let ((end nil))
10217       (while (not end)
10218         (when (or (not predicate)
10219                   (gnus-map-articles
10220                    predicate (gnus-summary-article-children)))
10221             (gnus-summary-hide-thread))
10222         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10223   (gnus-summary-position-point))
10224
10225 (defun gnus-summary-hide-thread ()
10226   "Hide thread subtrees.
10227 If PREDICATE is supplied, threads that satisfy this predicate
10228 will not be hidden.
10229 Returns nil if no threads were there to be hidden."
10230   (interactive)
10231   (let ((buffer-read-only nil)
10232         (start (point))
10233         (article (gnus-summary-article-number)))
10234     (goto-char start)
10235     ;; Go forward until either the buffer ends or the subthread
10236     ;; ends.
10237     (when (and (not (eobp))
10238                (or (zerop (gnus-summary-next-thread 1 t))
10239                    (goto-char (point-max))))
10240       (prog1
10241           (if (and (> (point) start)
10242                    (search-backward "\n" start t))
10243               (progn
10244                 (subst-char-in-region start (point) ?\n ?\^M)
10245                 (gnus-summary-goto-subject article))
10246             (goto-char start)
10247             nil)))))
10248
10249 (defun gnus-summary-go-to-next-thread (&optional previous)
10250   "Go to the same level (or less) next thread.
10251 If PREVIOUS is non-nil, go to previous thread instead.
10252 Return the article number moved to, or nil if moving was impossible."
10253   (let ((level (gnus-summary-thread-level))
10254         (way (if previous -1 1))
10255         (beg (point)))
10256     (forward-line way)
10257     (while (and (not (eobp))
10258                 (< level (gnus-summary-thread-level)))
10259       (forward-line way))
10260     (if (eobp)
10261         (progn
10262           (goto-char beg)
10263           nil)
10264       (setq beg (point))
10265       (prog1
10266           (gnus-summary-article-number)
10267         (goto-char beg)))))
10268
10269 (defun gnus-summary-next-thread (n &optional silent)
10270   "Go to the same level next N'th thread.
10271 If N is negative, search backward instead.
10272 Returns the difference between N and the number of skips actually
10273 done.
10274
10275 If SILENT, don't output messages."
10276   (interactive "p")
10277   (let ((backward (< n 0))
10278         (n (abs n)))
10279     (while (and (> n 0)
10280                 (gnus-summary-go-to-next-thread backward))
10281       (decf n))
10282     (unless silent
10283       (gnus-summary-position-point))
10284     (when (and (not silent) (/= 0 n))
10285       (gnus-message 7 "No more threads"))
10286     n))
10287
10288 (defun gnus-summary-prev-thread (n)
10289   "Go to the same level previous N'th thread.
10290 Returns the difference between N and the number of skips actually
10291 done."
10292   (interactive "p")
10293   (gnus-summary-next-thread (- n)))
10294
10295 (defun gnus-summary-go-down-thread ()
10296   "Go down one level in the current thread."
10297   (let ((children (gnus-summary-article-children)))
10298     (when children
10299       (gnus-summary-goto-subject (car children)))))
10300
10301 (defun gnus-summary-go-up-thread ()
10302   "Go up one level in the current thread."
10303   (let ((parent (gnus-summary-article-parent)))
10304     (when parent
10305       (gnus-summary-goto-subject parent))))
10306
10307 (defun gnus-summary-down-thread (n)
10308   "Go down thread N steps.
10309 If N is negative, go up instead.
10310 Returns the difference between N and how many steps down that were
10311 taken."
10312   (interactive "p")
10313   (let ((up (< n 0))
10314         (n (abs n)))
10315     (while (and (> n 0)
10316                 (if up (gnus-summary-go-up-thread)
10317                   (gnus-summary-go-down-thread)))
10318       (setq n (1- n)))
10319     (gnus-summary-position-point)
10320     (when (/= 0 n)
10321       (gnus-message 7 "Can't go further"))
10322     n))
10323
10324 (defun gnus-summary-up-thread (n)
10325   "Go up thread N steps.
10326 If N is negative, go down instead.
10327 Returns the difference between N and how many steps down that were
10328 taken."
10329   (interactive "p")
10330   (gnus-summary-down-thread (- n)))
10331
10332 (defun gnus-summary-top-thread ()
10333   "Go to the top of the thread."
10334   (interactive)
10335   (while (gnus-summary-go-up-thread))
10336   (gnus-summary-article-number))
10337
10338 (defun gnus-summary-kill-thread (&optional unmark)
10339   "Mark articles under current thread as read.
10340 If the prefix argument is positive, remove any kinds of marks.
10341 If the prefix argument is negative, tick articles instead."
10342   (interactive "P")
10343   (when unmark
10344     (setq unmark (prefix-numeric-value unmark)))
10345   (let ((articles (gnus-summary-articles-in-thread)))
10346     (save-excursion
10347       ;; Expand the thread.
10348       (gnus-summary-show-thread)
10349       ;; Mark all the articles.
10350       (while articles
10351         (gnus-summary-goto-subject (car articles))
10352         (cond ((null unmark)
10353                (gnus-summary-mark-article-as-read gnus-killed-mark))
10354               ((> unmark 0)
10355                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10356               (t
10357                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10358         (setq articles (cdr articles))))
10359     ;; Hide killed subtrees.
10360     (and (null unmark)
10361          gnus-thread-hide-killed
10362          (gnus-summary-hide-thread))
10363     ;; If marked as read, go to next unread subject.
10364     (when (null unmark)
10365       ;; Go to next unread subject.
10366       (gnus-summary-next-subject 1 t)))
10367   (gnus-set-mode-line 'summary))
10368
10369 ;; Summary sorting commands
10370
10371 (defun gnus-summary-sort-by-number (&optional reverse)
10372   "Sort the summary buffer by article number.
10373 Argument REVERSE means reverse order."
10374   (interactive "P")
10375   (gnus-summary-sort 'number reverse))
10376
10377 (defun gnus-summary-sort-by-random (&optional reverse)
10378   "Randomize the order in the summary buffer.
10379 Argument REVERSE means to randomize in reverse order."
10380   (interactive "P")
10381   (gnus-summary-sort 'random reverse))
10382
10383 (defun gnus-summary-sort-by-author (&optional reverse)
10384   "Sort the summary buffer by author name alphabetically.
10385 If `case-fold-search' is non-nil, case of letters is ignored.
10386 Argument REVERSE means reverse order."
10387   (interactive "P")
10388   (gnus-summary-sort 'author reverse))
10389
10390 (defun gnus-summary-sort-by-subject (&optional reverse)
10391   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10392 If `case-fold-search' is non-nil, case of letters is ignored.
10393 Argument REVERSE means reverse order."
10394   (interactive "P")
10395   (gnus-summary-sort 'subject reverse))
10396
10397 (defun gnus-summary-sort-by-date (&optional reverse)
10398   "Sort the summary buffer by date.
10399 Argument REVERSE means reverse order."
10400   (interactive "P")
10401   (gnus-summary-sort 'date reverse))
10402
10403 (defun gnus-summary-sort-by-score (&optional reverse)
10404   "Sort the summary buffer by score.
10405 Argument REVERSE means reverse order."
10406   (interactive "P")
10407   (gnus-summary-sort 'score reverse))
10408
10409 (defun gnus-summary-sort-by-lines (&optional reverse)
10410   "Sort the summary buffer by the number of lines.
10411 Argument REVERSE means reverse order."
10412   (interactive "P")
10413   (gnus-summary-sort 'lines reverse))
10414
10415 (defun gnus-summary-sort-by-chars (&optional reverse)
10416   "Sort the summary buffer by article length.
10417 Argument REVERSE means reverse order."
10418   (interactive "P")
10419   (gnus-summary-sort 'chars reverse))
10420
10421 (defun gnus-summary-sort-by-original (&optional reverse)
10422   "Sort the summary buffer using the default sorting method.
10423 Argument REVERSE means reverse order."
10424   (interactive "P")
10425   (let* ((buffer-read-only)
10426          (gnus-summary-prepare-hook nil))
10427     ;; We do the sorting by regenerating the threads.
10428     (gnus-summary-prepare)
10429     ;; Hide subthreads if needed.
10430     (gnus-summary-maybe-hide-threads)))
10431
10432 (defun gnus-summary-sort (predicate reverse)
10433   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10434   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10435          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10436          (gnus-thread-sort-functions
10437           (if (not reverse)
10438               thread
10439             `(lambda (t1 t2)
10440                (,thread t2 t1))))
10441          (gnus-sort-gathered-threads-function
10442           gnus-thread-sort-functions)
10443          (gnus-article-sort-functions
10444           (if (not reverse)
10445               article
10446             `(lambda (t1 t2)
10447                (,article t2 t1))))
10448          (buffer-read-only)
10449          (gnus-summary-prepare-hook nil))
10450     ;; We do the sorting by regenerating the threads.
10451     (gnus-summary-prepare)
10452     ;; Hide subthreads if needed.
10453     (gnus-summary-maybe-hide-threads)))
10454
10455 ;; Summary saving commands.
10456
10457 (defun gnus-summary-save-article (&optional n not-saved)
10458   "Save the current article using the default saver function.
10459 If N is a positive number, save the N next articles.
10460 If N is a negative number, save the N previous articles.
10461 If N is nil and any articles have been marked with the process mark,
10462 save those articles instead.
10463 The variable `gnus-default-article-saver' specifies the saver function."
10464   (interactive "P")
10465   (let* ((articles (gnus-summary-work-articles n))
10466          (save-buffer (save-excursion
10467                         (nnheader-set-temp-buffer " *Gnus Save*")))
10468          (num (length articles))
10469          header file)
10470     (dolist (article articles)
10471       (setq header (gnus-summary-article-header article))
10472       (if (not (vectorp header))
10473           ;; This is a pseudo-article.
10474           (if (assq 'name header)
10475               (gnus-copy-file (cdr (assq 'name header)))
10476             (gnus-message 1 "Article %d is unsaveable" article))
10477         ;; This is a real article.
10478         (save-window-excursion
10479           (let ((gnus-display-mime-function nil)
10480                 (gnus-article-prepare-hook nil))
10481             (gnus-summary-select-article t nil nil article)))
10482         (save-excursion
10483           (set-buffer save-buffer)
10484           (erase-buffer)
10485           (insert-buffer-substring gnus-original-article-buffer))
10486         (setq file (gnus-article-save save-buffer file num))
10487         (gnus-summary-remove-process-mark article)
10488         (unless not-saved
10489           (gnus-summary-set-saved-mark article))))
10490     (gnus-kill-buffer save-buffer)
10491     (gnus-summary-position-point)
10492     (gnus-set-mode-line 'summary)
10493     n))
10494
10495 (defun gnus-summary-pipe-output (&optional arg headers)
10496   "Pipe the current article to a subprocess.
10497 If N is a positive number, pipe the N next articles.
10498 If N is a negative number, pipe the N previous articles.
10499 If N is nil and any articles have been marked with the process mark,
10500 pipe those articles instead.
10501 If HEADERS (the symbolic prefix), include the headers, too."
10502   (interactive (gnus-interactive "P\ny"))
10503   (require 'gnus-art)
10504   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10505         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10506     (gnus-summary-save-article arg t))
10507   (let ((buffer (get-buffer "*Shell Command Output*")))
10508     (when (and buffer
10509                (not (zerop (buffer-size buffer))))
10510       (gnus-configure-windows 'pipe))))
10511
10512 (defun gnus-summary-save-article-mail (&optional arg)
10513   "Append the current article to an mail file.
10514 If N is a positive number, save the N next articles.
10515 If N is a negative number, save the N previous articles.
10516 If N is nil and any articles have been marked with the process mark,
10517 save those articles instead."
10518   (interactive "P")
10519   (require 'gnus-art)
10520   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10521     (gnus-summary-save-article arg)))
10522
10523 (defun gnus-summary-save-article-rmail (&optional arg)
10524   "Append the current article to an rmail file.
10525 If N is a positive number, save the N next articles.
10526 If N is a negative number, save the N previous articles.
10527 If N is nil and any articles have been marked with the process mark,
10528 save those articles instead."
10529   (interactive "P")
10530   (require 'gnus-art)
10531   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10532     (gnus-summary-save-article arg)))
10533
10534 (defun gnus-summary-save-article-file (&optional arg)
10535   "Append the current article to a file.
10536 If N is a positive number, save the N next articles.
10537 If N is a negative number, save the N previous articles.
10538 If N is nil and any articles have been marked with the process mark,
10539 save those articles instead."
10540   (interactive "P")
10541   (require 'gnus-art)
10542   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10543     (gnus-summary-save-article arg)))
10544
10545 (defun gnus-summary-write-article-file (&optional arg)
10546   "Write the current article to a file, deleting the previous file.
10547 If N is a positive number, save the N next articles.
10548 If N is a negative number, save the N previous articles.
10549 If N is nil and any articles have been marked with the process mark,
10550 save those articles instead."
10551   (interactive "P")
10552   (require 'gnus-art)
10553   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10554     (gnus-summary-save-article arg)))
10555
10556 (defun gnus-summary-save-article-body-file (&optional arg)
10557   "Append the current article body to a file.
10558 If N is a positive number, save the N next articles.
10559 If N is a negative number, save the N previous articles.
10560 If N is nil and any articles have been marked with the process mark,
10561 save those articles instead."
10562   (interactive "P")
10563   (require 'gnus-art)
10564   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10565     (gnus-summary-save-article arg)))
10566
10567 (defun gnus-summary-muttprint (&optional arg)
10568   "Print the current article using Muttprint.
10569 If N is a positive number, save the N next articles.
10570 If N is a negative number, save the N previous articles.
10571 If N is nil and any articles have been marked with the process mark,
10572 save those articles instead."
10573   (interactive "P")
10574   (require 'gnus-art)
10575   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10576     (gnus-summary-save-article arg t)))
10577
10578 (defun gnus-summary-pipe-message (program)
10579   "Pipe the current article through PROGRAM."
10580   (interactive "sProgram: ")
10581   (gnus-summary-select-article)
10582   (let ((mail-header-separator ""))
10583     (gnus-eval-in-buffer-window gnus-article-buffer
10584       (save-restriction
10585         (widen)
10586         (let ((start (window-start))
10587               buffer-read-only)
10588           (message-pipe-buffer-body program)
10589           (set-window-start (get-buffer-window (current-buffer)) start))))))
10590
10591 (defun gnus-get-split-value (methods)
10592   "Return a value based on the split METHODS."
10593   (let (split-name method result match)
10594     (when methods
10595       (save-excursion
10596         (set-buffer gnus-original-article-buffer)
10597         (save-restriction
10598           (nnheader-narrow-to-headers)
10599           (while (and methods (not split-name))
10600             (goto-char (point-min))
10601             (setq method (pop methods))
10602             (setq match (car method))
10603             (when (cond
10604                    ((stringp match)
10605                     ;; Regular expression.
10606                     (ignore-errors
10607                       (re-search-forward match nil t)))
10608                    ((gnus-functionp match)
10609                     ;; Function.
10610                     (save-restriction
10611                       (widen)
10612                       (setq result (funcall match gnus-newsgroup-name))))
10613                    ((consp match)
10614                     ;; Form.
10615                     (save-restriction
10616                       (widen)
10617                       (setq result (eval match)))))
10618               (setq split-name (cdr method))
10619               (cond ((stringp result)
10620                      (push (expand-file-name
10621                             result gnus-article-save-directory)
10622                            split-name))
10623                     ((consp result)
10624                      (setq split-name (append result split-name)))))))))
10625     (nreverse split-name)))
10626
10627 (defun gnus-valid-move-group-p (group)
10628   (and (boundp group)
10629        (symbol-name group)
10630        (symbol-value group)
10631        (gnus-get-function (gnus-find-method-for-group
10632                            (symbol-name group)) 'request-accept-article t)))
10633
10634 (defun gnus-read-move-group-name (prompt default articles prefix)
10635   "Read a group name."
10636   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10637          (minibuffer-confirm-incomplete nil) ; XEmacs
10638          (prom
10639           (format "%s %s to:"
10640                   prompt
10641                   (if (> (length articles) 1)
10642                       (format "these %d articles" (length articles))
10643                     "this article")))
10644          (to-newsgroup
10645           (cond
10646            ((null split-name)
10647             (gnus-completing-read-with-default
10648              default prom
10649              gnus-active-hashtb
10650              'gnus-valid-move-group-p
10651              nil prefix
10652              'gnus-group-history))
10653            ((= 1 (length split-name))
10654             (gnus-completing-read-with-default
10655              (car split-name) prom
10656              gnus-active-hashtb
10657              'gnus-valid-move-group-p
10658              nil nil
10659              'gnus-group-history))
10660            (t
10661             (gnus-completing-read-with-default
10662              nil prom
10663              (mapcar (lambda (el) (list el))
10664                      (nreverse split-name))
10665              nil nil nil
10666              'gnus-group-history))))
10667          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10668     (when to-newsgroup
10669       (if (or (string= to-newsgroup "")
10670               (string= to-newsgroup prefix))
10671           (setq to-newsgroup default))
10672       (unless to-newsgroup
10673         (error "No group name entered"))
10674       (or (gnus-active to-newsgroup)
10675           (gnus-activate-group to-newsgroup nil nil to-method)
10676           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10677                                      to-newsgroup))
10678               (or (and (gnus-request-create-group to-newsgroup to-method)
10679                        (gnus-activate-group
10680                         to-newsgroup nil nil to-method)
10681                        (gnus-subscribe-group to-newsgroup))
10682                   (error "Couldn't create group %s" to-newsgroup)))
10683           (error "No such group: %s" to-newsgroup)))
10684     to-newsgroup))
10685
10686 (defun gnus-summary-save-parts (type dir n &optional reverse)
10687   "Save parts matching TYPE to DIR.
10688 If REVERSE, save parts that do not match TYPE."
10689   (interactive
10690    (list (read-string "Save parts of type: "
10691                       (or (car gnus-summary-save-parts-type-history)
10692                           gnus-summary-save-parts-default-mime)
10693                       'gnus-summary-save-parts-type-history)
10694          (setq gnus-summary-save-parts-last-directory
10695                (read-file-name "Save to directory: "
10696                                gnus-summary-save-parts-last-directory
10697                                nil t))
10698          current-prefix-arg))
10699   (gnus-summary-iterate n
10700     (let ((gnus-display-mime-function nil)
10701           (gnus-inhibit-treatment t))
10702       (gnus-summary-select-article))
10703     (save-excursion
10704       (set-buffer gnus-article-buffer)
10705       (let ((handles (or gnus-article-mime-handles
10706                          (mm-dissect-buffer nil gnus-article-loose-mime)
10707                          (mm-uu-dissect))))
10708         (when handles
10709           (gnus-summary-save-parts-1 type dir handles reverse)
10710           (unless gnus-article-mime-handles ;; Don't destroy this case.
10711             (mm-destroy-parts handles)))))))
10712
10713 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10714   (if (stringp (car handle))
10715       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10716               (cdr handle))
10717     (when (if reverse
10718               (not (string-match type (mm-handle-media-type handle)))
10719             (string-match type (mm-handle-media-type handle)))
10720       (let ((file (expand-file-name
10721                    (file-name-nondirectory
10722                     (or
10723                      (mail-content-type-get
10724                       (mm-handle-disposition handle) 'filename)
10725                      (concat gnus-newsgroup-name
10726                              "." (number-to-string
10727                                   (cdr gnus-article-current)))))
10728                    dir)))
10729         (unless (file-exists-p file)
10730           (mm-save-part-to-file handle file))))))
10731
10732 ;; Summary extract commands
10733
10734 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10735   (let ((buffer-read-only nil)
10736         (article (gnus-summary-article-number))
10737         after-article b e)
10738     (unless (gnus-summary-goto-subject article)
10739       (error "No such article: %d" article))
10740     (gnus-summary-position-point)
10741     ;; If all commands are to be bunched up on one line, we collect
10742     ;; them here.
10743     (unless gnus-view-pseudos-separately
10744       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10745             files action)
10746         (while ps
10747           (setq action (cdr (assq 'action (car ps))))
10748           (setq files (list (cdr (assq 'name (car ps)))))
10749           (while (and ps (cdr ps)
10750                       (string= (or action "1")
10751                                (or (cdr (assq 'action (cadr ps))) "2")))
10752             (push (cdr (assq 'name (cadr ps))) files)
10753             (setcdr ps (cddr ps)))
10754           (when files
10755             (when (not (string-match "%s" action))
10756               (push " " files))
10757             (push " " files)
10758             (when (assq 'execute (car ps))
10759               (setcdr (assq 'execute (car ps))
10760                       (funcall (if (string-match "%s" action)
10761                                    'format 'concat)
10762                                action
10763                                (mapconcat
10764                                 (lambda (f)
10765                                   (if (equal f " ")
10766                                       f
10767                                     (mm-quote-arg f)))
10768                                 files " ")))))
10769           (setq ps (cdr ps)))))
10770     (if (and gnus-view-pseudos (not not-view))
10771         (while pslist
10772           (when (assq 'execute (car pslist))
10773             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10774                                   (eq gnus-view-pseudos 'not-confirm)))
10775           (setq pslist (cdr pslist)))
10776       (save-excursion
10777         (while pslist
10778           (setq after-article (or (cdr (assq 'article (car pslist)))
10779                                   (gnus-summary-article-number)))
10780           (gnus-summary-goto-subject after-article)
10781           (forward-line 1)
10782           (setq b (point))
10783           (insert "    " (file-name-nondirectory
10784                           (cdr (assq 'name (car pslist))))
10785                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10786           (setq e (point))
10787           (forward-line -1)             ; back to `b'
10788           (gnus-add-text-properties
10789            b (1- e) (list 'gnus-number gnus-reffed-article-number
10790                           gnus-mouse-face-prop gnus-mouse-face))
10791           (gnus-data-enter
10792            after-article gnus-reffed-article-number
10793            gnus-unread-mark b (car pslist) 0 (- e b))
10794           (setq gnus-newsgroup-unreads
10795                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10796                                          gnus-reffed-article-number))
10797           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10798           (setq pslist (cdr pslist)))))))
10799
10800 (defun gnus-pseudos< (p1 p2)
10801   (let ((c1 (cdr (assq 'action p1)))
10802         (c2 (cdr (assq 'action p2))))
10803     (and c1 c2 (string< c1 c2))))
10804
10805 (defun gnus-request-pseudo-article (props)
10806   (cond ((assq 'execute props)
10807          (gnus-execute-command (cdr (assq 'execute props)))))
10808   (let ((gnus-current-article (gnus-summary-article-number)))
10809     (gnus-run-hooks 'gnus-mark-article-hook)))
10810
10811 (defun gnus-execute-command (command &optional automatic)
10812   (save-excursion
10813     (gnus-article-setup-buffer)
10814     (set-buffer gnus-article-buffer)
10815     (setq buffer-read-only nil)
10816     (let ((command (if automatic command
10817                      (read-string "Command: " (cons command 0)))))
10818       (erase-buffer)
10819       (insert "$ " command "\n\n")
10820       (if gnus-view-pseudo-asynchronously
10821           (start-process "gnus-execute" (current-buffer) shell-file-name
10822                          shell-command-switch command)
10823         (call-process shell-file-name nil t nil
10824                       shell-command-switch command)))))
10825
10826 ;; Summary kill commands.
10827
10828 (defun gnus-summary-edit-global-kill (article)
10829   "Edit the \"global\" kill file."
10830   (interactive (list (gnus-summary-article-number)))
10831   (gnus-group-edit-global-kill article))
10832
10833 (defun gnus-summary-edit-local-kill ()
10834   "Edit a local kill file applied to the current newsgroup."
10835   (interactive)
10836   (setq gnus-current-headers (gnus-summary-article-header))
10837   (gnus-group-edit-local-kill
10838    (gnus-summary-article-number) gnus-newsgroup-name))
10839
10840 ;;; Header reading.
10841
10842 (defun gnus-read-header (id &optional header)
10843   "Read the headers of article ID and enter them into the Gnus system."
10844   (let ((group gnus-newsgroup-name)
10845         (gnus-override-method
10846          (or
10847           gnus-override-method
10848           (and (gnus-news-group-p gnus-newsgroup-name)
10849                (car (gnus-refer-article-methods)))))
10850         where)
10851     ;; First we check to see whether the header in question is already
10852     ;; fetched.
10853     (if (stringp id)
10854         ;; This is a Message-ID.
10855         (setq header (or header (gnus-id-to-header id)))
10856       ;; This is an article number.
10857       (setq header (or header (gnus-summary-article-header id))))
10858     (if (and header
10859              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10860         ;; We have found the header.
10861         header
10862       ;; If this is a sparse article, we have to nix out its
10863       ;; previous entry in the thread hashtb.
10864       (when (and header
10865                  (gnus-summary-article-sparse-p (mail-header-number header)))
10866         (let* ((parent (gnus-parent-id (mail-header-references header)))
10867                (thread (and parent (gnus-id-to-thread parent))))
10868           (when thread
10869             (delq (assq header thread) thread))))
10870       ;; We have to really fetch the header to this article.
10871       (save-excursion
10872         (set-buffer nntp-server-buffer)
10873         (when (setq where (gnus-request-head id group))
10874           (nnheader-fold-continuation-lines)
10875           (goto-char (point-max))
10876           (insert ".\n")
10877           (goto-char (point-min))
10878           (insert "211 ")
10879           (princ (cond
10880                   ((numberp id) id)
10881                   ((cdr where) (cdr where))
10882                   (header (mail-header-number header))
10883                   (t gnus-reffed-article-number))
10884                  (current-buffer))
10885           (insert " Article retrieved.\n"))
10886         (if (or (not where)
10887                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10888             ()                          ; Malformed head.
10889           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10890             (when (and (stringp id)
10891                        (not (string= (gnus-group-real-name group)
10892                                      (car where))))
10893               ;; If we fetched by Message-ID and the article came
10894               ;; from a different group, we fudge some bogus article
10895               ;; numbers for this article.
10896               (mail-header-set-number header gnus-reffed-article-number))
10897             (save-excursion
10898               (set-buffer gnus-summary-buffer)
10899               (decf gnus-reffed-article-number)
10900               (gnus-remove-header (mail-header-number header))
10901               (push header gnus-newsgroup-headers)
10902               (setq gnus-current-headers header)
10903               (push (mail-header-number header) gnus-newsgroup-limit)))
10904           header)))))
10905
10906 (defun gnus-remove-header (number)
10907   "Remove header NUMBER from `gnus-newsgroup-headers'."
10908   (if (and gnus-newsgroup-headers
10909            (= number (mail-header-number (car gnus-newsgroup-headers))))
10910       (pop gnus-newsgroup-headers)
10911     (let ((headers gnus-newsgroup-headers))
10912       (while (and (cdr headers)
10913                   (not (= number (mail-header-number (cadr headers)))))
10914         (pop headers))
10915       (when (cdr headers)
10916         (setcdr headers (cddr headers))))))
10917
10918 ;;;
10919 ;;; summary highlights
10920 ;;;
10921
10922 (defun gnus-highlight-selected-summary ()
10923   "Highlight selected article in summary buffer."
10924   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10925   (when gnus-summary-selected-face
10926     (save-excursion
10927       (let* ((beg (progn (beginning-of-line) (point)))
10928              (end (progn (end-of-line) (point)))
10929              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10930              (from (if (get-text-property beg gnus-mouse-face-prop)
10931                        beg
10932                      (or (next-single-property-change
10933                           beg gnus-mouse-face-prop nil end)
10934                          beg)))
10935              (to
10936               (if (= from end)
10937                   (- from 2)
10938                 (or (next-single-property-change
10939                      from gnus-mouse-face-prop nil end)
10940                     end))))
10941         ;; If no mouse-face prop on line we will have to = from = end,
10942         ;; so we highlight the entire line instead.
10943         (when (= (+ to 2) from)
10944           (setq from beg)
10945           (setq to end))
10946         (if gnus-newsgroup-selected-overlay
10947             ;; Move old overlay.
10948             (gnus-move-overlay
10949              gnus-newsgroup-selected-overlay from to (current-buffer))
10950           ;; Create new overlay.
10951           (gnus-overlay-put
10952            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10953            'face gnus-summary-selected-face))))))
10954
10955 (defvar gnus-summary-highlight-line-cached nil)
10956 (defvar gnus-summary-highlight-line-trigger nil)
10957
10958 (defun gnus-summary-highlight-line-0 ()
10959   (if (and (eq gnus-summary-highlight-line-trigger 
10960                gnus-summary-highlight)
10961            gnus-summary-highlight-line-cached)
10962       gnus-summary-highlight-line-cached
10963     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10964           gnus-summary-highlight-line-cached
10965           (let* ((cond (list 'cond))
10966                  (c cond)
10967                  (list gnus-summary-highlight))
10968             (while list
10969               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10970               (setq c (cdr c)
10971                     list (cdr list)))
10972             (gnus-byte-compile (list 'lambda nil cond))))))
10973
10974 (defun gnus-summary-highlight-line ()
10975   "Highlight current line according to `gnus-summary-highlight'."
10976   (let* ((beg (gnus-point-at-bol))
10977          (article (or (gnus-summary-article-number) gnus-current-article))
10978          (score (or (cdr (assq article
10979                                gnus-newsgroup-scored))
10980                     gnus-summary-default-score 0))
10981          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10982          (inhibit-read-only t)
10983          (default gnus-summary-default-score)
10984          (default-high gnus-summary-default-high-score)
10985          (default-low gnus-summary-default-low-score)
10986          (uncached (memq article gnus-newsgroup-undownloaded))
10987          (downloaded (not uncached)))
10988     (let ((face (funcall (gnus-summary-highlight-line-0))))
10989       (unless (eq face (get-text-property beg 'face))
10990         (gnus-put-text-property-excluding-characters-with-faces
10991          beg (gnus-point-at-eol) 'face
10992          (setq face (if (boundp face) (symbol-value face) face)))
10993         (when gnus-summary-highlight-line-function
10994           (funcall gnus-summary-highlight-line-function article face))))))
10995
10996 (defun gnus-update-read-articles (group unread &optional compute)
10997   "Update the list of read articles in GROUP.
10998 UNREAD is a sorted list."
10999   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11000          (entry (gnus-gethash group gnus-newsrc-hashtb))
11001          (info (nth 2 entry))
11002          (prev 1)
11003          read)
11004     (if (or (not info) (not active))
11005         ;; There is no info on this group if it was, in fact,
11006         ;; killed.  Gnus stores no information on killed groups, so
11007         ;; there's nothing to be done.
11008         ;; One could store the information somewhere temporarily,
11009         ;; perhaps...  Hmmm...
11010         ()
11011       ;; Remove any negative articles numbers.
11012       (while (and unread (< (car unread) 0))
11013         (setq unread (cdr unread)))
11014       ;; Remove any expired article numbers
11015       (while (and unread (< (car unread) (car active)))
11016         (setq unread (cdr unread)))
11017       ;; Compute the ranges of read articles by looking at the list of
11018       ;; unread articles.
11019       (while unread
11020         (when (/= (car unread) prev)
11021           (push (if (= prev (1- (car unread))) prev
11022                   (cons prev (1- (car unread))))
11023                 read))
11024         (setq prev (1+ (car unread)))
11025         (setq unread (cdr unread)))
11026       (when (<= prev (cdr active))
11027         (push (cons prev (cdr active)) read))
11028       (setq read (if (> (length read) 1) (nreverse read) read))
11029       (if compute
11030           read
11031         (save-excursion
11032           (let (setmarkundo)
11033             ;; Propagate the read marks to the backend.
11034             (when (gnus-check-backend-function 'request-set-mark group)
11035               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11036                     (add (gnus-remove-from-range read (gnus-info-read info))))
11037                 (when (or add del)
11038                   (unless (gnus-check-group group)
11039                     (error "Can't open server for %s" group))
11040                   (gnus-request-set-mark
11041                    group (delq nil (list (if add (list add 'add '(read)))
11042                                          (if del (list del 'del '(read))))))
11043                   (setq setmarkundo
11044                         `(gnus-request-set-mark
11045                           ,group
11046                           ',(delq nil (list
11047                                        (if del (list del 'add '(read)))
11048                                        (if add (list add 'del '(read))))))))))
11049             (set-buffer gnus-group-buffer)
11050             (gnus-undo-register
11051               `(progn
11052                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11053                  (gnus-info-set-read ',info ',(gnus-info-read info))
11054                  (gnus-get-unread-articles-in-group ',info
11055                                                     (gnus-active ,group))
11056                  (gnus-group-update-group ,group t)
11057                  ,setmarkundo))))
11058         ;; Enter this list into the group info.
11059         (gnus-info-set-read info read)
11060         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11061         (gnus-get-unread-articles-in-group info (gnus-active group))
11062         t))))
11063
11064 (defun gnus-offer-save-summaries ()
11065   "Offer to save all active summary buffers."
11066   (let (buffers)
11067     ;; Go through all buffers and find all summaries.
11068     (dolist (buffer (buffer-list))
11069       (when (and (setq buffer (buffer-name buffer))
11070                  (string-match "Summary" buffer)
11071                  (save-excursion
11072                    (set-buffer buffer)
11073                    ;; We check that this is, indeed, a summary buffer.
11074                    (and (eq major-mode 'gnus-summary-mode)
11075                         ;; Also make sure this isn't bogus.
11076                         gnus-newsgroup-prepared
11077                         ;; Also make sure that this isn't a
11078                         ;; dead summary buffer.
11079                         (not gnus-dead-summary-mode))))
11080         (push buffer buffers)))
11081     ;; Go through all these summary buffers and offer to save them.
11082     (when buffers
11083       (save-excursion
11084         (map-y-or-n-p
11085          "Update summary buffer %s? "
11086          (lambda (buf)
11087            (switch-to-buffer buf)
11088            (gnus-summary-exit))
11089          buffers)))))
11090
11091 (defun gnus-summary-setup-default-charset ()
11092   "Setup newsgroup default charset."
11093   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11094       (setq gnus-newsgroup-charset nil)
11095     (let* ((ignored-charsets
11096             (or gnus-newsgroup-ephemeral-ignored-charsets
11097                 (append
11098                  (and gnus-newsgroup-name
11099                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11100                  gnus-newsgroup-ignored-charsets))))
11101       (setq gnus-newsgroup-charset
11102             (or gnus-newsgroup-ephemeral-charset
11103                 (and gnus-newsgroup-name
11104                      (gnus-parameter-charset gnus-newsgroup-name))
11105                 gnus-default-charset))
11106       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11107            ignored-charsets))))
11108
11109 ;;;
11110 ;;; Mime Commands
11111 ;;;
11112
11113 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11114   "Display the current article buffer fully MIME-buttonized.
11115 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11116 treated as multipart/mixed."
11117   (interactive "P")
11118   (require 'gnus-art)
11119   (let ((gnus-unbuttonized-mime-types nil)
11120         (gnus-mime-display-multipart-as-mixed show-all-parts))
11121     (gnus-summary-show-article)))
11122
11123 (defun gnus-summary-repair-multipart (article)
11124   "Add a Content-Type header to a multipart article without one."
11125   (interactive (list (gnus-summary-article-number)))
11126   (gnus-with-article article
11127     (message-narrow-to-head)
11128     (message-remove-header "Mime-Version")
11129     (goto-char (point-max))
11130     (insert "Mime-Version: 1.0\n")
11131     (widen)
11132     (when (search-forward "\n--" nil t)
11133       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11134         (message-narrow-to-head)
11135         (message-remove-header "Content-Type")
11136         (goto-char (point-max))
11137         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11138                         separator))
11139         (widen))))
11140   (let (gnus-mark-article-hook)
11141     (gnus-summary-select-article t t nil article)))
11142
11143 (defun gnus-summary-toggle-display-buttonized ()
11144   "Toggle the buttonizing of the article buffer."
11145   (interactive)
11146   (require 'gnus-art)
11147   (if (setq gnus-inhibit-mime-unbuttonizing
11148             (not gnus-inhibit-mime-unbuttonizing))
11149       (let ((gnus-unbuttonized-mime-types nil))
11150         (gnus-summary-show-article))
11151     (gnus-summary-show-article)))
11152
11153 ;;;
11154 ;;; Generic summary marking commands
11155 ;;;
11156
11157 (defvar gnus-summary-marking-alist
11158   '((read gnus-del-mark "d")
11159     (unread gnus-unread-mark "u")
11160     (ticked gnus-ticked-mark "!")
11161     (dormant gnus-dormant-mark "?")
11162     (expirable gnus-expirable-mark "e"))
11163   "An alist of names/marks/keystrokes.")
11164
11165 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11166 (defvar gnus-summary-mark-map)
11167
11168 (defun gnus-summary-make-all-marking-commands ()
11169   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11170   (dolist (elem gnus-summary-marking-alist)
11171     (apply 'gnus-summary-make-marking-command elem)))
11172
11173 (defun gnus-summary-make-marking-command (name mark keystroke)
11174   (let ((map (make-sparse-keymap)))
11175     (define-key gnus-summary-generic-mark-map keystroke map)
11176     (dolist (lway `((next "next" next nil "n")
11177                     (next-unread "next unread" next t "N")
11178                     (prev "previous" prev nil "p")
11179                     (prev-unread "previous unread" prev t "P")
11180                     (nomove "" nil nil ,keystroke)))
11181       (let ((func (gnus-summary-make-marking-command-1
11182                    mark (car lway) lway name)))
11183         (setq func (eval func))
11184         (define-key map (nth 4 lway) func)))))
11185
11186 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11187   `(defun ,(intern
11188             (format "gnus-summary-put-mark-as-%s%s"
11189                     name (if (eq way 'nomove)
11190                              ""
11191                            (concat "-" (symbol-name way)))))
11192      (n)
11193      ,(format
11194        "Mark the current article as %s%s.
11195 If N, the prefix, then repeat N times.
11196 If N is negative, move in reverse order.
11197 The difference between N and the actual number of articles marked is
11198 returned."
11199        name (cadr lway))
11200      (interactive "p")
11201      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11202
11203 (defun gnus-summary-generic-mark (n mark move unread)
11204   "Mark N articles with MARK."
11205   (unless (eq major-mode 'gnus-summary-mode)
11206     (error "This command can only be used in the summary buffer"))
11207   (gnus-summary-show-thread)
11208   (let ((nummove
11209          (cond
11210           ((eq move 'next) 1)
11211           ((eq move 'prev) -1)
11212           (t 0))))
11213     (if (zerop nummove)
11214         (setq n 1)
11215       (when (< n 0)
11216         (setq n (abs n)
11217               nummove (* -1 nummove))))
11218     (while (and (> n 0)
11219                 (gnus-summary-mark-article nil mark)
11220                 (zerop (gnus-summary-next-subject nummove unread t)))
11221       (setq n (1- n)))
11222     (when (/= 0 n)
11223       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11224     (gnus-summary-recenter)
11225     (gnus-summary-position-point)
11226     (gnus-set-mode-line 'summary)
11227     n))
11228
11229 (defun gnus-summary-insert-articles (articles)
11230   (when (setq articles
11231               (gnus-sorted-difference articles
11232                                       (mapcar (lambda (h)
11233                                                 (mail-header-number h))
11234                                               gnus-newsgroup-headers)))
11235     (setq gnus-newsgroup-headers
11236           (merge 'list
11237                  gnus-newsgroup-headers
11238                  (gnus-fetch-headers articles)
11239                  'gnus-article-sort-by-number))
11240     ;; Suppress duplicates?
11241     (when gnus-suppress-duplicates
11242       (gnus-dup-suppress-articles))
11243
11244     ;; We might want to build some more threads first.
11245     (when (and gnus-fetch-old-headers
11246                (eq gnus-headers-retrieved-by 'nov))
11247       (if (eq gnus-fetch-old-headers 'invisible)
11248           (gnus-build-all-threads)
11249         (gnus-build-old-threads)))
11250     ;; Let the Gnus agent mark articles as read.
11251     (when gnus-agent
11252       (gnus-agent-get-undownloaded-list))
11253     ;; Remove list identifiers from subject
11254     (when gnus-list-identifiers
11255       (gnus-summary-remove-list-identifiers))
11256     ;; First and last article in this newsgroup.
11257     (when gnus-newsgroup-headers
11258       (setq gnus-newsgroup-begin
11259             (mail-header-number (car gnus-newsgroup-headers))
11260             gnus-newsgroup-end
11261             (mail-header-number
11262              (gnus-last-element gnus-newsgroup-headers))))
11263     (when gnus-use-scoring
11264       (gnus-possibly-score-headers))))
11265
11266 (defun gnus-summary-insert-old-articles (&optional all)
11267   "Insert all old articles in this group.
11268 If ALL is non-nil, already read articles become readable.
11269 If ALL is a number, fetch this number of articles."
11270   (interactive "P")
11271   (prog1
11272       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11273             older len)
11274         (setq older
11275               ;; Some nntp servers lie about their active range.  When
11276               ;; this happens, the active range can be in the millions.
11277               ;; Use a compressed range to avoid creating a huge list.
11278               (gnus-range-difference (list gnus-newsgroup-active) old))
11279         (setq len (gnus-range-length older))
11280         (cond
11281          ((null older) nil)
11282          ((numberp all)
11283           (if (< all len)
11284               (let ((older-range (nreverse older)))
11285                 (setq older nil)
11286
11287                 (while (> all 0)
11288                   (let* ((r (pop older-range))
11289                          (min (if (numberp r) r (car r)))
11290                          (max (if (numberp r) r (cdr r))))
11291                     (while (and (<= min max)
11292                                 (> all 0))
11293                       (push max older)
11294                       (setq all (1- all)
11295                             max (1- max))))))
11296             (setq older (gnus-uncompress-range older))))
11297          (all
11298           (setq older (gnus-uncompress-range older)))
11299          (t
11300           (when (and (numberp gnus-large-newsgroup)
11301                    (> len gnus-large-newsgroup))
11302               (let* ((cursor-in-echo-area nil)
11303                      (initial (gnus-parameter-large-newsgroup-initial
11304                                gnus-newsgroup-name))
11305                      (input
11306                       (read-string
11307                        (format
11308                         "How many articles from %s (%s %d): "
11309                         (gnus-limit-string
11310                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11311                         (if initial "max" "default")
11312                         len)
11313                        (if initial
11314                            (cons (number-to-string initial)
11315                                  0)))))
11316                 (unless (string-match "^[ \t]*$" input)
11317                   (setq all (string-to-number input))
11318                   (if (< all len)
11319                       (let ((older-range (nreverse older)))
11320                         (setq older nil)
11321
11322                         (while (> all 0)
11323                           (let* ((r (pop older-range))
11324                                  (min (if (numberp r) r (car r)))
11325                                  (max (if (numberp r) r (cdr r))))
11326                             (while (and (<= min max)
11327                                         (> all 0))
11328                               (push max older)
11329                               (setq all (1- all)
11330                                     max (1- max))))))))))
11331           (setq older (gnus-uncompress-range older))))
11332         (if (not older)
11333             (message "No old news.")
11334           (gnus-summary-insert-articles older)
11335           (gnus-summary-limit (gnus-sorted-nunion old older))))
11336     (gnus-summary-position-point)))
11337
11338 (defun gnus-summary-insert-new-articles ()
11339   "Insert all new articles in this group."
11340   (interactive)
11341   (prog1
11342       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11343             (old-active gnus-newsgroup-active)
11344             (nnmail-fetched-sources (list t))
11345             i new)
11346         (setq gnus-newsgroup-active
11347               (gnus-activate-group gnus-newsgroup-name 'scan))
11348         (setq i (cdr gnus-newsgroup-active))
11349         (while (> i (cdr old-active))
11350           (push i new)
11351           (decf i))
11352         (if (not new)
11353             (message "No gnus is bad news.")
11354           (gnus-summary-insert-articles new)
11355           (setq gnus-newsgroup-unreads
11356                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11357           (gnus-summary-limit (gnus-sorted-nunion old new))))
11358     (gnus-summary-position-point)))
11359
11360 (gnus-summary-make-all-marking-commands)
11361
11362 (gnus-ems-redefine)
11363
11364 (provide 'gnus-sum)
11365
11366 (run-hooks 'gnus-sum-load-hook)
11367
11368 ;;; gnus-sum.el ends here